Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从io.BytesIO流加载numpy.load_Python_Numpy_Azure Storage - Fatal编程技术网

Python 从io.BytesIO流加载numpy.load

Python 从io.BytesIO流加载numpy.load,python,numpy,azure-storage,Python,Numpy,Azure Storage,我将numpy阵列保存在Azure Blob存储中,并将它们加载到如下流: stream = io.BytesIO() store.get_blob_to_stream(container, 'cat.npy', stream) 我从stream.getvalue()中知道流包含用于重建数组的元数据。这是前150个字节: b"\x93NUMPY\x01\x00v\x00{'descr': '|u1', 'fortran_order': False, 'shape': (720, 1280, 3

我将numpy阵列保存在Azure Blob存储中,并将它们加载到如下流:

stream = io.BytesIO()
store.get_blob_to_stream(container, 'cat.npy', stream)
我从
stream.getvalue()
中知道流包含用于重建数组的元数据。这是前150个字节:

b"\x93NUMPY\x01\x00v\x00{'descr': '|u1', 'fortran_order': False, 'shape': (720, 1280, 3), }                                                  \n\xc1\xb0\x94\xc2\xb1\x95\xc3\xb2\x96\xc4\xb3\x97\xc5\xb4\x98\xc6\xb5\x99\xc7\xb6\x9a\xc7"
是否可以使用
numpy.load
或其他简单方法加载字节流

我可以将阵列保存到磁盘并从磁盘加载,但出于几个原因,我希望避免这样做


编辑:只是强调一下,输出需要是一个numpy数组,其形状和数据类型在流的前128个字节中指定。

我尝试了几种方法来实现您的需求

这是我的示例代码

from azure.storage.blob.baseblobservice import BaseBlobService
import numpy as np

account_name = '<your account name>'
account_key = '<your account key>'
container_name = '<your container name>'
blob_name = '<your blob name>'

blob_service = BaseBlobService(
    account_name=account_name,
    account_key=account_key
)
样本2。通过
BytesIO

import io
stream = io.BytesIO()
blob_service.get_blob_to_stream(container_name, blob_name, stream)
dat = np.frombuffer(stream.getbuffer())
print('from BytesIO', dat)
样本3。使用
numpy.fromfile
with打开带有sas令牌的blob url,它实际上会将blob文件下载到本地文件系统中

ds = np.DataSource()
# ds = np.DataSource(None)  # use with temporary file
# ds = np.DataSource(path) # use with path like `data/`
f = ds.open(url_with_sas)
dat = np.fromfile(f)
print('from DataSource', dat)

我认为示例1和示例2更适合您。

我尝试使用几种方法来实现您的需求

这是我的示例代码

from azure.storage.blob.baseblobservice import BaseBlobService
import numpy as np

account_name = '<your account name>'
account_key = '<your account key>'
container_name = '<your container name>'
blob_name = '<your blob name>'

blob_service = BaseBlobService(
    account_name=account_name,
    account_key=account_key
)
样本2。通过
BytesIO

import io
stream = io.BytesIO()
blob_service.get_blob_to_stream(container_name, blob_name, stream)
dat = np.frombuffer(stream.getbuffer())
print('from BytesIO', dat)
样本3。使用
numpy.fromfile
with打开带有sas令牌的blob url,它实际上会将blob文件下载到本地文件系统中

ds = np.DataSource()
# ds = np.DataSource(None)  # use with temporary file
# ds = np.DataSource(path) # use with path like `data/`
f = ds.open(url_with_sas)
dat = np.fromfile(f)
print('from DataSource', dat)

我认为示例1和示例2更适合您。

这是我提出的一种有点粗糙的方法,它基本上只从前128个字节获取元数据:

def load_npy_from_stream(stream_):
    """Experimental, may not work!

    :param stream_: io.BytesIO() object obtained by e.g. calling BlockBlobService().get_blob_to_stream() containing
        the binary stream of a standard format .npy file.
    :return: numpy.ndarray
    """
    stream_.seek(0)
    prefix_ = stream_.read(128)  # first 128 bytes seem to be the metadata
    dict_string = re.search('\{(.*?)\}', prefix_[1:].decode())[0]
    metadata_dict = eval(dict_string)

    array = np.frombuffer(stream_.read(), dtype=metadata_dict['descr']).reshape(metadata_dict['shape'])

    return array

可能会以多种方式失败,但如果有人想尝试一下,我会把它贴在这里。我将用它运行测试,并会回来,因为我知道的更多。

这是我提出的一种有点黑客的方式,基本上只是从前128个字节获取元数据:

def load_npy_from_stream(stream_):
    """Experimental, may not work!

    :param stream_: io.BytesIO() object obtained by e.g. calling BlockBlobService().get_blob_to_stream() containing
        the binary stream of a standard format .npy file.
    :return: numpy.ndarray
    """
    stream_.seek(0)
    prefix_ = stream_.read(128)  # first 128 bytes seem to be the metadata
    dict_string = re.search('\{(.*?)\}', prefix_[1:].decode())[0]
    metadata_dict = eval(dict_string)

    array = np.frombuffer(stream_.read(), dtype=metadata_dict['descr']).reshape(metadata_dict['shape'])

    return array

可能会以多种方式失败,但如果有人想尝试一下,我会把它贴在这里。我将用这个运行测试,并会回来,因为我知道更多。

当涉及到np.savez时,上述解决方案通常需要工作

上载到存储: 从存储器下载:
当涉及到np.savez时,上述解决方案通常需要工作

上载到存储: 从存储器下载:
如果您将它保存到磁盘,您将如何加载它?@hpaulj by
numpy.load()
load
接受一个像object这样的打开文件。好的,是的,无法工作……如果您将它保存到磁盘,您将如何加载它?@hpaulj by
numpy.load()
load
接受一个像object这样的打开文件。好的,是的,无法让它工作…嘿,非常感谢你的回复,但我想我忘了强调一下,我特别需要一个numpy数组,其形状和数据类型与
流.getvalue()中的头128字节相同
np.frombuffer(stream.read())
实际上可以正常工作!嘿,非常感谢您的回复,但我想我忘了强调一下,我特别需要一个numpy数组,其形状和数据类型与
stream.getvalue()
前128个字节相同
np.frombuffer(stream.read())
实际上可以正常工作!