Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Load a NumPy array from disk, either from a .npy file or from a .7z file. No need to include extension
def load_ndarray(filename):
"""Load a NumPy array from disk into memory, with extension .npy or .7z. If no extension is
included in the argument, first assume it's .npy, then .7z.
Args:
filename (str): Name of the NumPy array (in disk).
Returns:
ndarray
"""
if filename.endswith('.npy'):
compress = False
status = 'uncompressed' # Everything OK!
elif filename.endswith('.7z'):
compress = True
status = 'compressed'
else:
file_npy = filename + '.npy'
if file_npy in os.listdir():
filename = file_npy
compress = False
status = 'uncompressed'
else:
file_7z = filename + '.7z'
if file_7z in os.listdir():
filename = file_7z
compress = True
status = 'compressed'
else:
raise FileNotFoundError
# ---------------------------------
size = os.stat(filename).st_size
print('Loading {0:,} [{1}] bytes from disk... File: {2}'
.format(size, status, filename))
if compress:
if shutil.which('7z') is None:
raise FileNotFoundError('7z not found on the PATH!')
subprocess.Popen('7z e ' + filename + ' -mmt', shell=True).wait()
ndarray = np.load(filename[:-3] + '.npy')
else:
ndarray = np.load(filename)
print('Succesfully loaded {0!s}-array ({1:,} bytes) from {2}!'
.format(ndarray.shape, size, filename))
return ndarray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment