本文整理汇总了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()