def save(file, arr): """ Save an array to a binary file in NumPy format. Parameters ---------- f : file or string File or filename to which the data is saved. If the filename does not already have a ``.npy`` extension, it is added. x : array_like Array data. Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> np.save(outfile, x) >>> outfile.seek(0) >>> np.load(outfile) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ if isinstance(file, basestring): if not file.endswith('.npy'): file = file + '.npy' fid = open(file, "wb") else: fid = file arr = np.asanyarray(arr) format.write_array(fid, arr)
def savez(file, *args, **kwds): """ Save several arrays into an .npz file format which is a zipped-archive of arrays If keyword arguments are given, then filenames are taken from the keywords. If arguments are passed in with no keywords, then stored file names are arr_0, arr_1, etc. Parameters ---------- file : string File name of .npz file. args : Arguments Function arguments. kwds : Keyword arguments Keywords. """ # Import is postponed to here since zipfile depends on gzip, an optional # component of the so-called standard library. import zipfile 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 import tempfile direc = tempfile.gettempdir() todel = [] for key, val in namedict.iteritems(): fname = key + '.npy' filename = os.path.join(direc, fname) todel.append(filename) fid = open(filename,'wb') format.write_array(fid, np.asanyarray(val)) fid.close() zip.write(filename, arcname=fname) zip.close() for name in todel: os.remove(name)
def savez(file, *args, **kwds): """ Save several arrays into an .npz file format which is a zipped-archive of arrays If keyword arguments are given, then filenames are taken from the keywords. If arguments are passed in with no keywords, then stored file names are arr_0, arr_1, etc. Parameters ---------- file : string File name of .npz file. args : Arguments Function arguments. kwds : Keyword arguments Keywords. """ # Import is postponed to here since zipfile depends on gzip, an optional # component of the so-called standard library. import zipfile 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 import tempfile direc = tempfile.gettempdir() todel = [] for key, val in namedict.iteritems(): fname = key + '.npy' filename = os.path.join(direc, fname) todel.append(filename) fid = open(filename, 'wb') format.write_array(fid, np.asanyarray(val)) fid.close() zip.write(filename, arcname=fname) zip.close() for name in todel: os.remove(name)
def save(file, arr): """ Save an array to a binary file in NumPy ``.npy`` format. Parameters ---------- file : file or string File or filename to which the data is saved. If the filename does not already have a ``.npy`` extension, it is added. arr : array_like Array data to be saved. See Also -------- savez : Save several arrays into a .npz compressed archive savetxt, load Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> np.save(outfile, x) >>> outfile.seek(0) # only necessary in this example (with tempfile) >>> np.load(outfile) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ if isinstance(file, basestring): if not file.endswith('.npy'): file = file + '.npy' fid = open(file, "wb") else: fid = file arr = np.asanyarray(arr) format.write_array(fid, arr)
def save(file, arr): """Save an array to a binary file (a string or file-like object). If the file is a string, then if it does not have the .npy extension, it is appended and a file open. Data is saved to the open file in NumPy-array format Examples -------- import numpy as np ... np.save('myfile', a) a = np.load('myfile.npy') """ if isinstance(file, str): if not file.endswith('.npy'): file = file + '.npy' fid = open(file, "wb") else: fid = file arr = np.asanyarray(arr) format.write_array(fid, arr)
def savez(file, *args, **kwds): """ Save several arrays into a single, compressed file with extension ".npz" If keyword arguments are given, the names for variables assigned to the keywords are the keyword names (not the variable names in the caller). If arguments are passed in with no keywords, the corresponding variable names are arr_0, arr_1, etc. Parameters ---------- file : Either the filename (string) or an open file (file-like object) If file is a string, it names the output file. ".npz" will be appended if it is not already there. args : Arguments Any function arguments other than the file name are variables to save. Since it is not possible for Python to know their names outside the savez function, they will be saved with names "arr_0", "arr_1", and so on. These arguments can be any expression. kwds : Keyword arguments All keyword=value pairs cause the value to be saved with the name of the keyword. See Also -------- save : Save a single array to a binary file in NumPy format savetxt : Save an array to a file as plain text Notes ----- The .npz file format is a zipped archive of files named after the variables they contain. Each file contains one variable in .npy format. """ # Import is postponed to here since zipfile depends on gzip, an optional # component of the so-called standard library. import zipfile 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 import tempfile direc = tempfile.gettempdir() todel = [] for key, val in namedict.iteritems(): fname = key + '.npy' filename = os.path.join(direc, fname) todel.append(filename) fid = open(filename,'wb') format.write_array(fid, np.asanyarray(val)) fid.close() zip.write(filename, arcname=fname) zip.close() for name in todel: os.remove(name)
def savez(file, *args, **kwds): """ Save several arrays into a single, compressed file with extension ".npz" If keyword arguments are given, the names for variables assigned to the keywords are the keyword names (not the variable names in the caller). If arguments are passed in with no keywords, the corresponding variable names are arr_0, arr_1, etc. Parameters ---------- file : Either the filename (string) or an open file (file-like object) If file is a string, it names the output file. ".npz" will be appended if it is not already there. args : Arguments Any function arguments other than the file name are variables to save. Since it is not possible for Python to know their names outside the savez function, they will be saved with names "arr_0", "arr_1", and so on. These arguments can be any expression. kwds : Keyword arguments All keyword=value pairs cause the value to be saved with the name of the keyword. See Also -------- save : Save a single array to a binary file in NumPy format savetxt : Save an array to a file as plain text Notes ----- The .npz file format is a zipped archive of files named after the variables they contain. Each file contains one variable in .npy format. """ # Import is postponed to here since zipfile depends on gzip, an optional # component of the so-called standard library. import zipfile 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_factory(file, mode="w") # Place to write temporary .npy files # before storing them in the zip import tempfile direc = tempfile.gettempdir() todel = [] for key, val in namedict.iteritems(): fname = key + '.npy' filename = os.path.join(direc, fname) todel.append(filename) fid = open(filename, 'wb') format.write_array(fid, np.asanyarray(val)) fid.close() zip.write(filename, arcname=fname) zip.close() for name in todel: os.remove(name)
def savez(file, *args, **kwds): """ Save several arrays into a single, compressed file with extension ".npz" If keyword arguments are given, the names for variables assigned to the keywords are the keyword names (not the variable names in the caller). If arguments are passed in with no keywords, the corresponding variable names are arr_0, arr_1, etc. Parameters ---------- file : Either the filename (string) or an open file (file-like object) If file is a string, it names the output file. ".npz" will be appended if it is not already there. args : Arguments Any function arguments other than the file name are variables to save. Since it is not possible for Python to know their names outside the savez function, they will be saved with names "arr_0", "arr_1", and so on. These arguments can be any expression. kwds : Keyword arguments All keyword=value pairs cause the value to be saved with the name of the keyword. See Also -------- save : Save a single array to a binary file in NumPy format savetxt : Save an array to a file as plain text Notes ----- The .npz file format is a zipped archive of files named after the variables they contain. Each file contains one variable in .npy format. Examples -------- >>> x = np.random.random((3, 3)) >>> y = np.zeros((3, 2)) >>> np.savez('data', x=x, y=y) """ # 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(): raise ValueError, "Cannot use un-named variables and keyword %s" % key namedict[key] = val zip = zipfile.ZipFile(file, mode="w") # 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, np.asanyarray(val)) fid.close() fid = None zip.write(tmpfile, arcname=fname) finally: if fid: fid.close() finally: os.remove(tmpfile) zip.close()