Search code examples
pythonnumpyloadmodulenotfounderrornpz-file

Load npz file complains about "ModuleNotFoundError"


I am saving some data to a npz file and then load it. But when I try to access a part of it, depending on where I run the loading script, it complains about ModuleNotFoundError (and the module name is a file where the data was saved was using, but are not required to what I am loading now). What am I missing? How does load complains about imports or modules that were at the script where I saved the data?

Example code for the save:

import signals_generator.simulate as sig_simulator
(...)
def gen_sig(file_path, freq_in, freq_audio, bpm_s, time_tt, var_fac):

freq = freq_in
dec_i, dec_q, trigger_times, ecg_labels, all_hr, time_all, sig = sig_simulator.get_gen_signal_to_eval(bpm_s,
                                                                                                      time_tt, freq,
                                                                                                      variation_factor=var_fac)
sig_t = []

sig_t.append(sig.real)
sig_t.append(sig.imag)
sig_t.append(trigger_times[TriggerSource.SYSTOLE])
# plt.plot(sig_t[0])
# plt.plot(sig_t[1])
# plt.show()


scope_trigger = np.ones_like(sig_t[0]) * (-2)
scope_trigger[200:400] = 2

total_s = int(freq_audio * len(test_signal[0]) / freq_in)
filt_sig = sy.resample(test_signal, total_s, axis=1)


# add dict with settings
settings = {"BPM": [], "TOTAL_TIME": [], "FREQ": [], "DEFAULT_BPM_VARIATION_FACTOR": []}
settings['BPM'].append(bpm_s)
settings['TOTAL_TIME'].append(time_tt)
settings['FREQ'].append(freq)
settings['DEFAULT_BPM_VARIATION_FACTOR'].append(var_fac)


np.savez(file_path, SETTINGS=settings, OFSDM_I=dec_i, OFSDM_Q=dec_q, OUT_SIG_T=filt_sig, TRIGGER_TIMES=trigger_times, ECG_LABELS=ecg_labels, ALL_HR=all_hr,
         TIME_ALL=time_all, SIG=sig)

To load:

import numpy as np
tp = r"..\saved_data"
data_gen_t = np.load(tp + ".npz",
               allow_pickle=True)
temp_trigger_times_t = dict(enumerate(data_gen_t['TRIGGER_TIMES'].flatten(), 1))

The problem is, even if I do:

data_gen_t['TRIGGER_TIMES']

I get the error:

(...)
array = pickle.load(fp, **pickle_kwargs)
ModuleNotFoundError: No module named 'signals_generator'

And the "signals_generator" is a file that was on the imports of the file where I saved.

If I run this for other place, it even looks like it finds the signals_generator but then complains about includes of that file. But what does the load has to do with the scripts that generated the saved data?

File "C:\Python\Python39\lib\site-packages\numpy\lib\format.py", line 748, in read_array
array = pickle.load(fp, **pickle_kwargs)
File "C:\username\signals_generator\simulate.py", line 3, in 
<module>
from beattrain import BeatTrain
ModuleNotFoundError: No module named 'beattrain'

Also, the trigger_times is a dict of lists.

Any hint on what might be the problem/fix?

Thank you!


Solution

  • The trigger_times saved uses a class of IntEnum for the elements at its dictionary, so I guess that it might be making it dependent due to pickle (I have no idea how it works on the background). Now before saving I change the element of the dictionary from the class enum to a string, and now it works.

    My conclusion: Saving the npz this way might keep dependencies with the files where the data was generated if they use more complex info.