【问题标题】:ibm_boto3 compatibility issue with scikit-learn on Mac OSibm_boto3 在 Mac OS 上与 scikit-learn 的兼容性问题
【发布时间】:2018-10-15 09:11:44
【问题描述】:

我有一个使用 scikit-learn 的 Python 3.6 应用程序,部署到 IBM Cloud (Cloud Foundry)。它工作正常。我的本地开发环境是 Mac OS High Sierra。

最近,我向应用程序添加了 IBM Cloud Object Storage 功能 (ibm_boto3)。 COS 功能本身运行良好。我可以使用ibm_boto3 库上传、下载、列出和删除对象。

奇怪的是,应用程序中使用 scikit-learn 的部分现在冻结了。

如果我注释掉 ibm_boto3 import 语句(和相应的代码),scikit-learn 代码可以正常工作。

更令人困惑的是,这个问题只发生在运行 OS X 的本地开发机器上。当应用程序部署到 IBM Cloud 时,它运行良好——scikit-learnibm_boto3 可以很好地并行运行。

此时我们唯一的假设是ibm_boto3 库以某种方式在scikit-learn 中显示了一个已知问题(请参阅this -- 当numpy 在操作系统上使用加速器时,K-means 算法的并行版本被破坏X)。 请注意,我们只有在将ibm_boto3 添加到项目后才会遇到此问题。

但是,在部署到 IBM Cloud 之前,我们需要能够在 localhost 上进行测试。在 Mac OS 上 ibm_boto3scikit-learn 之间是否存在任何已知的兼容性问题?

关于我们如何在开发机器上避免这种情况的任何建议?

干杯。

【问题讨论】:

    标签: python scikit-learn object-storage ibm-cloud-storage


    【解决方案1】:

    到目前为止,没有任何已知的兼容性问题。 :)

    在某些时候,OSX 附带的 vanilla SSL 库存在一些问题,但如果您能够读取和写入数据,这不是问题。

    你在使用HMAC credentials吗?如果是这样,我很好奇如果您使用原始的 boto3 库而不是 IBM 分支,这种行为是否还会继续。

    这里有一个简单的例子,展示了如何将pandas 与原始boto3 一起使用:

    import boto3  # package used to connect to IBM COS using the S3 API
    import io  # python package used to stream data
    import pandas as pd  # lightweight data analysis package
    
    access_key = '<access key>'
    secret_key = '<secret key>'
    pub_endpoint = 'https://s3-api.us-geo.objectstorage.softlayer.net'
    pvt_endpoint = 'https://s3-api.us-geo.objectstorage.service.networklayer.com'
    bucket = 'demo'  # the bucket holding the objects being worked on.
    object_key = 'demo-data'  # the name of the data object being analyzed.
    result_key = 'demo-data-results'  # the name of the output data object.
    
    
    # First, we need to open a session and create a client that can connect to IBM COS.
    # This client needs to know where to connect, the credentials to use,
    # and what signature protocol to use for authentication. The endpoint
    # can be specified to be public or private.
    cos = boto3.client('s3', endpoint_url=pub_endpoint,
                       aws_access_key_id=access_key,
                       aws_secret_access_key=secret_key,
                       region_name='us',
                       config=boto3.session.Config(signature_version='s3v4'))
    
    # Since we've already uploaded the dataset to be worked on into cloud storage,
    # now we just need to identify which object we want to use. This creates a JSON
    # representation of request's response headers.
    obj = cos.get_object(Bucket=bucket, Key=object_key)
    
    # Now, because this is all REST API based, the actual contents of the file are
    # transported in the request body, so we need to identify where to find the
    # data stream containing the actual CSV file we want to analyze.
    data = obj['Body'].read()
    
    # Now we can read that data stream into a pandas dataframe.
    df = pd.read_csv(io.BytesIO(data))
    
    # This is just a trivial example, but we'll take that dataframe and just
    # create a JSON document that contains the mean values for each column.
    output = df.mean(axis=0, numeric_only=True).to_json()
    
    # Now we can write that JSON file to COS as a new object in the same bucket.
    cos.put_object(Bucket=bucket, Key=result_key, Body=output)
    

    【讨论】:

    • 同意——我想这是一个边缘案例,感谢您调查它。不,我们没有使用 HMAC 凭据。您能否指出一个将 HMAC 凭证与原始 boto3 库一起使用的示例?我很乐意尝试并报告我的发现。
    • 谢谢...我将尝试结合scikit-learn 库并分享我的发现。顺便说一句,这是否意味着 boto3 库与 IBM COS 一起开箱即用?这是连接到 IBM COS 的受支持/推荐的方式吗?我想ibm_boto3 解决了特定的提供商级别差异,只是不确定它们是什么。
    • 在解决此问题时,我注释掉了我正在使用的所有ibm_boto3 代码,然后逐行添加回来。我能够将其隔离到 requests.get() 调用以获取 COS 端点。我无法解释为什么这会破坏scikit-learn,并且仅在 MacOS 上。我不认为这与ibm_boto3 有任何关系。一定是scikit-learn 的错误。我会接受答案,这样我们就可以结束这个问题。非常感谢您的帮助!
    • 很高兴为您提供帮助,也很高兴您成功了。是的,COS大部分与 S3 API 兼容,因此几乎任何可以与 AWS S3 通信的工具也可以与 COS 通信。IBM 分支和原始库之间的关键差异是支持创建使用 API 密钥和服务实例 ID 而不是访问/秘密密钥的客户端,支持自定义 IAM 令牌管理器,以及用于创建使用 Key Protect 管理加密的存储桶的一些 IBM 特定 API。很快就会有对使用 Aspera 进行文件传输和其他一些好处的额外支持。
    猜你喜欢
    • 2013-02-04
    • 2021-05-02
    • 2017-08-20
    • 2020-03-09
    • 2019-06-03
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    相关资源
    最近更新 更多