I'm trying to store the results of a fit I made using the lmfit package for python in an hdf5 file using the h5py package for python.
Currently I find myself recreating the structure of the data object by hand (i.e. loop over all keys in dictionary, get values and save them).
I have the feeling there has to be a more efficient/pythonic way of saving such an object in an hdf5 file similar to how a pickle of an object would work.
Could anyone help me find a way to efficiently store the information contained in an lmfit.model.ModelFit or lmfit.parameter.Parameters object in an hdf5 file?
edited to show currently used.
def add_analysis_datagroup_to_file(self, group_name='Analysis'):
try:
self.analysis_group= self.f.create_group(group_name)
except ValueError:
print 'Datagroup name "%s" already exists in hdf5 file' %group_name
self.analysis_group = self.f[group_name]
def save_fitted_parameters(self, fit_results=None):
if fit_results is None:
fit_results = self.fit_results
try:
fit_grp = self.analysis_group.create_group('Fitted Params')
except:
fit_grp = self.analysis_group['Fitted Params']
for parname, par in self.fit_results.params.iteritems():
try:
par_group = fit_grp.create_group(parname)
except:
par_group = fit_grp[parname]
par_dict = vars(par)
for val_name, val in par_dict.iteritems():
if val_name == '_val':
val_name = 'value'
if val_name == 'correl' and val is not None:
try:
correl_group = par_group.create_group(val_name)
except:
correl_group = par_group[val_name]
for cor_name, cor_val in val.iteritems():
correl_group.attrs.create(name=cor_name, data=cor_val)
else:
try:
par_group.attrs.create(name=val_name, data=val)
except:
pass