本文整理匯總了Python中numpy.lib.format.read_magic方法的典型用法代碼示例。如果您正苦於以下問題:Python format.read_magic方法的具體用法?Python format.read_magic怎麽用?Python format.read_magic使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy.lib.format
的用法示例。
在下文中一共展示了format.read_magic方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_read_magic
# 需要導入模塊: from numpy.lib import format [as 別名]
# 或者: from numpy.lib.format import read_magic [as 別名]
def test_read_magic():
s1 = BytesIO()
s2 = BytesIO()
arr = np.ones((3, 6), dtype=float)
format.write_array(s1, arr, version=(1, 0))
format.write_array(s2, arr, version=(2, 0))
s1.seek(0)
s2.seek(0)
version1 = format.read_magic(s1)
version2 = format.read_magic(s2)
assert_(version1 == (1, 0))
assert_(version2 == (2, 0))
assert_(s1.tell() == format.MAGIC_LEN)
assert_(s2.tell() == format.MAGIC_LEN)
示例2: test_read_magic_bad_magic
# 需要導入模塊: from numpy.lib import format [as 別名]
# 或者: from numpy.lib.format import read_magic [as 別名]
def test_read_magic_bad_magic():
for magic in malformed_magic:
f = BytesIO(magic)
yield raises(ValueError)(format.read_magic), f
示例3: _get_info
# 需要導入模塊: from numpy.lib import format [as 別名]
# 或者: from numpy.lib.format import read_magic [as 別名]
def _get_info(self):
from numpy.lib import format
with self.f as fp:
version = format.read_magic(fp)
format._check_version(version)
shape, fortran_order, dtype = format._read_array_header(fp, version)
self.shape = shape
self.dtype = dtype
self.order = 'F' if fortran_order else 'C'
self.offset = fp.tell()
示例4: _read_header
# 需要導入模塊: from numpy.lib import format [as 別名]
# 或者: from numpy.lib.format import read_magic [as 別名]
def _read_header(self):
with open(self.path, 'rb') as fp:
version = format.read_magic(fp)
try:
format._check_version(version)
except ValueError:
raise ValueError('Invalid file format.')
header_data = format._read_array_header(fp, version)
self.shape, self.fortran_order, self.dtype = header_data
示例5: load_npy_file
# 需要導入模塊: from numpy.lib import format [as 別名]
# 或者: from numpy.lib.format import read_magic [as 別名]
def load_npy_file(path, block_size):
""" Loads a file in npy format (must be 2-dimensional).
Parameters
----------
path : str
Path to the npy file.
block_size : tuple (int, int)
Block size of the resulting ds-array.
Returns
-------
x : ds-array
"""
try:
fid = open(path, "rb")
version = format.read_magic(fid)
format._check_version(version)
shape, fortran_order, dtype = format._read_array_header(fid, version)
if fortran_order:
raise ValueError("Fortran order not supported for npy files")
if len(shape) != 2:
raise ValueError("Array is not 2-dimensional")
if block_size[0] > shape[0] or block_size[1] > shape[1]:
raise ValueError("Block size is larger than the array")
blocks = []
n_blocks = int(ceil(shape[1] / block_size[1]))
for i in range(0, shape[0], block_size[0]):
read_count = min(block_size[0], shape[0] - i)
read_size = int(read_count * shape[1] * dtype.itemsize)
data = fid.read(read_size)
out_blocks = [object() for _ in range(n_blocks)]
_read_from_buffer(data, dtype, shape[1], block_size[1], out_blocks)
blocks.append(out_blocks)
return Array(blocks=blocks, top_left_shape=block_size,
reg_shape=block_size, shape=shape, sparse=False)
finally:
fid.close()