本文整理汇总了Python中numpy.lib.format.write_array函数的典型用法代码示例。如果您正苦于以下问题:Python write_array函数的具体用法?Python write_array怎么用?Python write_array使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了write_array函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: savez
def savez(file, *args, **kwds):
__doc__ = numpy.savez.__doc__
import zipfile
from numpy.lib import format
if isinstance(file, basestring):
if not file.endswith('.npz'):
file = file + '.npz'
namedict = kwds
for i, val in enumerate(args):
key = 'arr_%d' % i
if key in namedict.keys():
raise ValueError, "Cannot use un-named variables and keyword %s" % key
namedict[key] = val
zip = zipfile.ZipFile(file, mode="w")
# Place to write temporary .npy files
# before storing them in the zip. We need to path this to have a working
# function in parallel !
import tempfile
direc = tempfile.mkdtemp()
for key, val in namedict.iteritems():
fname = key + '.npy'
filename = os.path.join(direc, fname)
fid = open(filename, 'wb')
format.write_array(fid, numpy.asanyarray(val))
fid.close()
zip.write(filename, arcname=fname)
zip.close()
shutil.rmtree(direc)
开发者ID:jakobj,项目名称:PyNN,代码行数:33,代码来源:files.py
示例2: _pickle_array
def _pickle_array(arr):
arr = arr.view(np.ndarray)
buf = BytesIO()
write_array(buf, arr)
return buf.getvalue()
开发者ID:bkandel,项目名称:pandas,代码行数:7,代码来源:pickle.py
示例3: roundtrip_truncated
def roundtrip_truncated(arr):
f = BytesIO()
format.write_array(f, arr)
#BytesIO is one byte short
f2 = BytesIO(f.getvalue()[0:-1])
arr2 = format.read_array(f2)
return arr2
开发者ID:dyao-vu,项目名称:meta-core,代码行数:7,代码来源:test_format.py
示例4: test_memmap_roundtrip
def test_memmap_roundtrip():
# Fixme: test crashes nose on windows.
if not (sys.platform == 'win32' or sys.platform == 'cygwin'):
for arr in basic_arrays + record_arrays:
if arr.dtype.hasobject:
# Skip these since they can't be mmap'ed.
continue
# Write it out normally and through mmap.
nfn = os.path.join(tempdir, 'normal.npy')
mfn = os.path.join(tempdir, 'memmap.npy')
fp = open(nfn, 'wb')
try:
format.write_array(fp, arr)
finally:
fp.close()
fortran_order = (
arr.flags.f_contiguous and not arr.flags.c_contiguous)
ma = format.open_memmap(mfn, mode='w+', dtype=arr.dtype,
shape=arr.shape, fortran_order=fortran_order)
ma[...] = arr
del ma
# Check that both of these files' contents are the same.
fp = open(nfn, 'rb')
normal_bytes = fp.read()
fp.close()
fp = open(mfn, 'rb')
memmap_bytes = fp.read()
fp.close()
yield assert_equal_, normal_bytes, memmap_bytes
# Check that reading the file using memmap works.
ma = format.open_memmap(nfn, mode='r')
del ma
开发者ID:dyao-vu,项目名称:meta-core,代码行数:35,代码来源:test_format.py
示例5: test_memmap_roundtrip
def test_memmap_roundtrip():
# XXX: test crashes nose on windows. Fix this
if not (sys.platform == "win32" or sys.platform == "cygwin"):
for arr in basic_arrays + record_arrays:
if arr.dtype.hasobject:
# Skip these since they can't be mmap'ed.
continue
# Write it out normally and through mmap.
nfn = os.path.join(tempdir, "normal.npy")
mfn = os.path.join(tempdir, "memmap.npy")
fp = open(nfn, "wb")
try:
format.write_array(fp, arr)
finally:
fp.close()
fortran_order = arr.flags.f_contiguous and not arr.flags.c_contiguous
ma = format.open_memmap(mfn, mode="w+", dtype=arr.dtype, shape=arr.shape, fortran_order=fortran_order)
ma[...] = arr
del ma
# Check that both of these files' contents are the same.
fp = open(nfn, "rb")
normal_bytes = fp.read()
fp.close()
fp = open(mfn, "rb")
memmap_bytes = fp.read()
fp.close()
yield assert_equal, normal_bytes, memmap_bytes
# Check that reading the file using memmap works.
ma = format.open_memmap(nfn, mode="r")
# yield assert_array_equal, ma, arr
del ma
开发者ID:rlamy,项目名称:numpy,代码行数:34,代码来源:test_format.py
示例6: savez
def savez(self, *args, **kwds):
import os
import numpy.lib.format as format
namedict = kwds
for val in args:
key = 'arr_%d' % self.i
if key in namedict.keys():
raise ValueError(
"Cannot use un-named variables and keyword %s" % key)
namedict[key] = val
self.i += 1
try:
for key, val in namedict.iteritems():
fname = key + '.npy'
fid = open(self.tmpfile, 'wb')
try:
format.write_array(fid, np.asanyarray(val))
fid.close()
fid = None
self.zip.write(self.tmpfile, arcname=fname)
finally:
if fid:
fid.close()
finally:
os.remove(self.tmpfile)
开发者ID:hplgit,项目名称:fdm-book,代码行数:27,代码来源:Savez.py
示例7: test_read_array_header_2_0
def test_read_array_header_2_0():
s = BytesIO()
arr = np.ones((3, 6), dtype=float)
format.write_array(s, arr, version=(2, 0))
s.seek(format.MAGIC_LEN)
shape, fortran, dtype = format.read_array_header_2_0(s)
assert_((shape, fortran, dtype) == ((3, 6), False, float))
开发者ID:dyao-vu,项目名称:meta-core,代码行数:10,代码来源:test_format.py
示例8: save
def save(file, iarray, metafile=None, version=(1, 0)):
"""Save a info array to a .npy file and a metadata file.
Similar to the numpy.save function.
Parameters
----------
file: file handle or str
File or file name to write the array to in .npy format.
iarray: InfoArray object or array with similar interface
Array to be written to file with meta data.
metafile: str
File name for the meta data. The `info` attribute of `iarray` will be
written here. Default is None, where the it is
assumed to be the file name associated with `file` with ".meta"
appended.
"""
# Restrict to version (1,0) because we've only written write_header for
# this version.
if version != (1, 0):
raise ValueError("Only version (1,0) is safe from this function.")
# Make sure that the meta data will be representable as a string.
infostring = repr(iarray.info)
try:
safe_eval(infostring)
except SyntaxError:
raise ValueError
# Save the array in .npy format.
if isinstance(file, basestring):
fid = open(file, "wb")
else:
fid = file
npfor.write_array(fid, iarray, version=version)
# Figure out what the filename for the meta data should be.
if metafile is None:
try:
fname = file.name
except AttributeError:
fname = file
metafile = fname + ".meta"
# Save the meta data.
info_fid = open(metafile, 'w')
try:
info_fid.write(infostring)
finally:
info_fid.close()
开发者ID:eric-switzer,项目名称:algebra_base,代码行数:52,代码来源:file_io.py
示例9: _savez
def _savez(file, args, kwds, compress):
# Import is postponed to here since zipfile depends on gzip, an optional
# component of the so-called standard library.
import zipfile
# Import deferred for startup time improvement
import tempfile
if isinstance(file, basestring):
if not file.endswith(".npz"):
file = file + ".npz"
namedict = kwds
for i, val in enumerate(args):
key = "arr_%d" % i
if key in namedict.keys():
msg = "Cannot use un-named variables and keyword %s" % key
raise ValueError, msg
namedict[key] = val
if compress:
compression = zipfile.ZIP_DEFLATED
else:
compression = zipfile.ZIP_STORED
zip = zipfile_factory(file, mode="w", compression=compression)
# Stage arrays in a temporary file on disk, before writing to zip.
fd, tmpfile = tempfile.mkstemp(suffix="-numpy.npy")
os.close(fd)
try:
for key, val in namedict.iteritems():
fname = key + ".npy"
fid = open(tmpfile, "wb")
try:
format.write_array(fid, numpy.asanyarray(val))
fid.close()
fid = None
zip.write(tmpfile, arcname=fname)
finally:
if fid:
fid.close()
finally:
os.remove(tmpfile)
zip.close()
开发者ID:pankajp,项目名称:pysph,代码行数:46,代码来源:utils.py
示例10: test_version_2_0
def test_version_2_0():
f = BytesIO()
# requires more than 2 byte for header
dt = [(("%d" % i) * 100, float) for i in range(500)]
d = np.ones(1000, dtype=dt)
format.write_array(f, d, version=(2, 0))
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', UserWarning)
format.write_array(f, d)
assert_(w[0].category is UserWarning)
f.seek(0)
n = format.read_array(f)
assert_array_equal(d, n)
# 1.0 requested but data cannot be saved this way
assert_raises(ValueError, format.write_array, f, d, (1, 0))
开发者ID:dyao-vu,项目名称:meta-core,代码行数:18,代码来源:test_format.py
示例11: test_read_magic
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)
开发者ID:dyao-vu,项目名称:meta-core,代码行数:20,代码来源:test_format.py
示例12: _savez
def _savez(file, args, kwds, compress):
if isinstance(file, basestring):
if not file.endswith('.npz'):
file = file + '.npz'
namedict = kwds
for i, val in enumerate(args):
key = 'arr_%d' % i
if key in namedict.keys():
msg = "Cannot use un-named variables and keyword %s" % key
raise ValueError, msg
namedict[key] = val
if compress:
compression = zipfile.ZIP_DEFLATED
else:
compression = zipfile.ZIP_STORED
zip = zipfile_factory(file, mode="w", compression=compression)
# Stage arrays in a temporary file on disk, before writing to zip.
fd, tmpfile = tempfile.mkstemp(suffix='-numpy.npy')
os.close(fd)
try:
for key, val in namedict.iteritems():
fname = key + '.npy'
fid = open(tmpfile, 'wb')
try:
format.write_array(fid, numpy.asanyarray(val))
fid.close()
fid = None
zip.write(tmpfile, arcname=fname)
finally:
if fid:
fid.close()
finally:
os.remove(tmpfile)
zip.close()
开发者ID:pushkargodbole,项目名称:GPUPySPH,代码行数:39,代码来源:utils.py
示例13: test_write_version_1_0
def test_write_version_1_0():
f = StringIO()
arr = np.arange(1)
# These should pass.
format.write_array(f, arr, version=(1, 0))
format.write_array(f, arr)
# These should all fail.
bad_versions = [(1, 1), (0, 0), (0, 1), (2, 0), (2, 2), (255, 255)]
for version in bad_versions:
try:
format.write_array(f, arr, version=version)
except ValueError:
pass
else:
raise AssertionError("we should have raised a ValueError for the bad version %r" % (version,))
开发者ID:rlamy,项目名称:numpy,代码行数:16,代码来源:test_format.py
示例14: toSparse
def toSparse(source, idx2label, csr = False):
"""
Convert intra-chromosomal contact matrices to sparse ones.
Parameters
----------
source : str
Hdf5 file name.
idx2label : dict
A dictionary for conversion between zero-based indices and
string chromosome labels.
csr : bool
Whether to use CSR (Compressed Row Storage) format or not.
"""
import zipfile, tempfile
from numpy.lib.format import write_array
from scipy import sparse
lib = h5dict(source, mode = 'r')
## Uniform numpy-structured-array format
itype = np.dtype({'names':['bin1', 'bin2', 'IF'],
'formats':[np.int, np.int, np.float]})
## Create a Zip file in NPZ case
if not csr:
output = source.replace('.hm', '-sparse.npz')
else:
output = source.replace('.hm', '-csrsparse.npz')
Zip = zipfile.ZipFile(output, mode = 'w', allowZip64 = True)
fd, tmpfile = tempfile.mkstemp(suffix = '-numpy.npy')
os.close(fd)
log.log(21, 'Sparse Matrices will be saved to %s', output)
log.log(21, 'Only intra-chromosomal matrices will be taken into account')
log.log(21, 'Coverting ...')
count = 0
for i in lib:
if (i != 'resolution') and (len(set(i.split())) == 1):
# Used for the dict-like key
key = idx2label[int(i.split()[0])]
log.log(21, 'Chromosome %s ...', key)
# 2D-Matrix
H = lib[i]
if not csr:
# Triangle Array
Triu = np.triu(H)
# Sparse Matrix in Memory
x, y = np.nonzero(Triu)
values = Triu[x, y]
temp = np.zeros(values.size, dtype = itype)
temp['bin1'] = x
temp['bin2'] = y
temp['IF'] = values
else:
temp = sparse.triu(H, format = 'csr')
fname = key + '.npy'
fid = open(tmpfile, 'wb')
try:
write_array(fid, np.asanyarray(temp))
fid.close()
fid = None
Zip.write(tmpfile, arcname = fname)
finally:
if fid:
fid.close()
log.log(21, 'Done!')
count += 1
# Store the resolution information
if 'resolution' in lib:
fname = 'resolution.npy'
fid = open(tmpfile, 'wb')
try:
write_array(fid, np.asanyarray(lib['resolution']))
fid.close()
fid = None
Zip.write(tmpfile, arcname = fname)
finally:
if fid:
fid.close()
if count == 0:
log.warning('Empty source file!')
os.remove(tmpfile)
Zip.close()
开发者ID:yuanbaowen521,项目名称:HiC_pipeline,代码行数:99,代码来源:utilities.py
示例15: roundtrip_randsize
def roundtrip_randsize(arr):
f = BytesIO()
format.write_array(f, arr)
f2 = BytesIOSRandomSize(f.getvalue())
arr2 = format.read_array(f2)
return arr2
开发者ID:dyao-vu,项目名称:meta-core,代码行数:6,代码来源:test_format.py
示例16: roundtrip
def roundtrip(arr):
f = BytesIO()
format.write_array(f, arr)
f2 = BytesIO(f.getvalue())
arr2 = format.read_array(f2)
return arr2
开发者ID:dyao-vu,项目名称:meta-core,代码行数:6,代码来源:test_format.py
示例17: to_string
def to_string(arr):
f = StringIO()
format.write_array(f, arr)
s = f.getvalue()
return s
开发者ID:patykov,项目名称:SD_2016.1,代码行数:5,代码来源:server.py
示例18: test_write_version
def test_write_version():
f = BytesIO()
arr = np.arange(1)
# These should pass.
format.write_array(f, arr, version=(1, 0))
format.write_array(f, arr)
format.write_array(f, arr, version=None)
format.write_array(f, arr)
format.write_array(f, arr, version=(2, 0))
format.write_array(f, arr)
# These should all fail.
bad_versions = [
(1, 1),
(0, 0),
(0, 1),
(2, 2),
(255, 255),
]
for version in bad_versions:
with assert_raises_regex(ValueError,
'we only support format version.*'):
format.write_array(f, arr, version=version)
开发者ID:ovillellas,项目名称:numpy,代码行数:25,代码来源:test_format.py
示例19: roundtrip
def roundtrip(arr):
f = BytesIO()
format.write_array(f, arr)
f2 = BytesIO(f.getvalue())
arr2 = format.read_array(f2, allow_pickle=True)
return arr2
开发者ID:anntzer,项目名称:numpy,代码行数:6,代码来源:test_format.py
示例20: toString
def toString(data):
f= StringIO()
format.write_array(f,data)
return f.getvalue()
开发者ID:LuisJavierMontielArenas,项目名称:Redes2016,代码行数:4,代码来源:Cliente.py
注:本文中的numpy.lib.format.write_array函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论