Home > Software design >  How to save data for each loop?
How to save data for each loop?

Time:05-07

I want to save the dictionary in Csv or mat file for each loop, the code is from the link FHN equations

   import scipy.io as sio
    trajectory_nonauto = {} 
    file_out = f"out_{i}.mat"
    for i, param in enumerate(step_sc):
        flow = partial(non_autonomous_fitzhugh_nagumo, **param)
        for j, ic in enumerate(initial_conditions):
            trajectory_nonauto[i, j] = scipy.integrate.odeint(flow,y0=ic,t=time_span)
            sio.savemat(file_out, { trajectory_nonauto[i, j]})
            data = trajectory_nonauto
            print(trajectory_nonauto)

I was getting this error

TypeError: unhashable type: 'numpy.ndarray

at sio.savemat

CodePudding user response:

By the looks of it savemat takes a dict as its second argument.

The thing you are trying to pass, { trajectory_nonauto[i, j]}, is not a dict.

The error you are seeing is because {x} creates a set with element x, and all elements of a set must be hashable.

CodePudding user response:

instead of sio.savemat(file_out, { trajectory_nonauto[i, j]})

try sio.savemat(file_out, dict(enumerate(trajectory_nonauto[i, j])))

  • Related