rename the save_params_H5 function to be a general function save which can potentially support other file format

This commit is contained in:
Zhenwen Dai 2014-11-11 15:44:50 +00:00
parent 5736caa8fd
commit 0018b5e583

View file

@ -1010,9 +1010,9 @@ class Parameterizable(OptimizationHandlable):
"""
pass
def save_params_H5(self, filename):
def save(self, filename, ftype='HDF5'):
"""
Save all the model parameters into a HDF5 file.
Save all the model parameters into a file (HDF5 by default).
"""
from . import Param
from ...util.misc import param_to_array
@ -1022,15 +1022,16 @@ class Parameterizable(OptimizationHandlable):
plist = []
self.traverse(gather_params, plist)
names = self.parameter_names(adjust_for_printing=True)
try:
import h5py
f = h5py.File(filename,'w')
for p,n in zip(plist,names):
n = n.replace('.','_')
p = param_to_array(p)
d = f.create_dataset(n,p.shape,dtype=p.dtype)
d[:] = p
f.close()
except:
raise 'Fails to write the parameters into a HDF5 file!'
if ftype=='HDF5':
try:
import h5py
f = h5py.File(filename,'w')
for p,n in zip(plist,names):
n = n.replace('.','_')
p = param_to_array(p)
d = f.create_dataset(n,p.shape,dtype=p.dtype)
d[:] = p
f.close()
except:
raise 'Fails to write the parameters into a HDF5 file!'