Search code examples
numpynpz-file

Load multiple numpy arrays from npz file in multiple arrays in automated way


I have a npz file that looks like this.

data = np.load('Data_5_iteration.npz') # Load data from file
keys = list(data.keys()) # all keys in the dictionary
print(keys)
(base) hell@hell:~$ ['nodes', 'temperature', 'max_iter', 'best_error_vec', 'best_u_pred', 'final_error_vec', 'final_u_pred', 'best_iter', 'PDE_loss_array', 'BC_loss_array', 'total_loss_array']

I want to store all these numpy arrays in different arrays with the same name as in the list without writing them line by line.

For example, I don't want to write:

nodes = data[keys[0]]
temperature = data[keys[1]]
max_iter = data[keys[2]]
best_error_vec = data[keys[3]]
best_u_pred = data[keys[4]]
final_error_vec = data[keys[5]]
final_u_pred = data[keys[6]]
best_iter = data[keys[7]]
PDE_loss_array = data[keys[8]]
BC_loss_array = data[keys[9]]
total_loss_array = data[keys[10]]

Can I do this with some automated way?


Solution

  • Using a sample npz I can get a list or dict:

    In [42]: with np.load('data.npz') as f:
        ...:     alist = [(key,f[key]) for key in f]
        ...: 
    In [43]: alist
    Out[43]: [('fone', array(['t1', 't2', 't3'], dtype='<U2')), ('nval', array([1, 2, 3]))]
    In [44]: with np.load('data.npz') as f:
        ...:     adict = {key:f[key] for key in f}
        ...: 
    In [45]: adict
    Out[45]: {'fone': array(['t1', 't2', 't3'], dtype='<U2'), 'nval': array([1, 2, 3])}
    

    The list could be unpacked with:

    In [46]: fone, nval = alist
    In [47]: fone
    Out[47]: ('fone', array(['t1', 't2', 't3'], dtype='<U2'))
    

    We can also use the dict approach to set variables in the global or local namespace, but that's not encouraged in Python.