From 5736caa8fd4fa0eb5435a9e6d11ea0a165abc17e Mon Sep 17 00:00:00 2001 From: Zhenwen Dai Date: Tue, 11 Nov 2014 15:41:35 +0000 Subject: [PATCH 1/2] add the function of saving all the parameters into a HDF5 file --- GPy/core/parameterization/parameter_core.py | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/GPy/core/parameterization/parameter_core.py b/GPy/core/parameterization/parameter_core.py index 4048fbae..a8f11606 100644 --- a/GPy/core/parameterization/parameter_core.py +++ b/GPy/core/parameterization/parameter_core.py @@ -1009,3 +1009,28 @@ class Parameterizable(OptimizationHandlable): updates get passed through. See :py:function:``GPy.core.param.Observable.add_observer`` """ pass + + def save_params_H5(self, filename): + """ + Save all the model parameters into a HDF5 file. + """ + from . import Param + from ...util.misc import param_to_array + def gather_params(self, plist): + if isinstance(self,Param): + plist.append(self) + 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!' + From 0018b5e583f9b8ad661ab0650e3577489ee655d9 Mon Sep 17 00:00:00 2001 From: Zhenwen Dai Date: Tue, 11 Nov 2014 15:44:50 +0000 Subject: [PATCH 2/2] rename the save_params_H5 function to be a general function save which can potentially support other file format --- GPy/core/parameterization/parameter_core.py | 27 +++++++++++---------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/GPy/core/parameterization/parameter_core.py b/GPy/core/parameterization/parameter_core.py index a8f11606..6add95b0 100644 --- a/GPy/core/parameterization/parameter_core.py +++ b/GPy/core/parameterization/parameter_core.py @@ -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!'