@@ -990,53 +990,64 @@ def _set_weights_quadratic_multiband(self, quadratic_matrix, basis_idxs):
990990 def save_weights (self , filename , format = 'hdf5' ):
991991 """
992992 Save ROQ weights into a single file.
993- Support for json format was removed in :code:`v2.5 `, only hdf5 is supported.
993+ Support for json format was removed in :code:`v2.6 `, only hdf5 and npz are supported.
994994
995995 Parameters
996996 ==========
997997 filename : str
998998 The name of the file to save the weights to.
999- format : str (deprecated)
1000- This argument has no effect after removing support for json weight files .
999+ format : str
1000+ The format to save the weight in, should be in :code:`hdf5, npz` .
10011001 """
1002- if format != 'hdf5' :
1002+ if format not in [ 'hdf5' , 'npz' ] :
10031003 raise IOError (f"Format { format } not recognized." )
10041004 if format not in filename :
10051005 filename += "." + format
10061006 logger .info (f"Saving ROQ weights to { filename } " )
1007- import h5py
1008- with h5py .File (filename , 'w' ) as f :
1009- f .create_dataset ('time_samples' , data = self .weights ['time_samples' ])
1007+ if format == 'npz' :
1008+ if self .number_of_bases_linear > 1 or self .number_of_bases_quadratic > 1 :
1009+ raise ValueError (f'Format { format } not compatible with multiple bases' )
1010+ weights = dict ()
1011+ weights ['time_samples' ] = self .weights ['time_samples' ]
10101012 for basis_type in ['linear' , 'quadratic' ]:
1011- key = f'prior_range_{ basis_type } '
1012- if key in self .weights :
1013- grp = f .create_group (key )
1014- for param_name in self .weights [key ]:
1015- grp .create_dataset (
1016- param_name , data = self .weights [key ][param_name ])
1017- key = f'frequency_nodes_{ basis_type } '
1018- if key in self .weights :
1019- grp = f .create_group (key )
1020- for i in range (len (self .weights [key ])):
1021- grp .create_dataset (
1022- str (i ), data = self .weights [key ][i ])
10231013 for ifo in self .interferometers :
1024- key = f"{ ifo .name } _{ basis_type } "
1025- grp = f .create_group (key )
1026- for i in range (len (self .weights [key ])):
1027- grp .create_dataset (
1028- str (i ), data = self .weights [key ][i ])
1029-
1014+ key = f'{ ifo .name } _{ basis_type } '
1015+ weights [key ] = self .weights [key ][0 ]
1016+ np .savez (filename , ** weights )
1017+ else :
1018+ import h5py
1019+ with h5py .File (filename , 'w' ) as f :
1020+ f .create_dataset ('time_samples' ,
1021+ data = self .weights ['time_samples' ])
1022+ for basis_type in ['linear' , 'quadratic' ]:
1023+ key = f'prior_range_{ basis_type } '
1024+ if key in self .weights :
1025+ grp = f .create_group (key )
1026+ for param_name in self .weights [key ]:
1027+ grp .create_dataset (
1028+ param_name , data = self .weights [key ][param_name ])
1029+ key = f'frequency_nodes_{ basis_type } '
1030+ if key in self .weights :
1031+ grp = f .create_group (key )
1032+ for i in range (len (self .weights [key ])):
1033+ grp .create_dataset (
1034+ str (i ), data = self .weights [key ][i ])
1035+ for ifo in self .interferometers :
1036+ key = f"{ ifo .name } _{ basis_type } "
1037+ grp = f .create_group (key )
1038+ for i in range (len (self .weights [key ])):
1039+ grp .create_dataset (
1040+ str (i ), data = self .weights [key ][i ])
10301041 def load_weights (self , filename , format = None ):
10311042 """
1032- Load ROQ weights. Support for json format was removed in :code:`v2.2 `.
1043+ Load ROQ weights. Support for json format was removed in :code:`v2.6 `.
10331044
10341045 Parameters
10351046 ==========
10361047 filename : str
10371048 The name of the file to save the weights to.
1038- format : str (deprecated)
1039- This argument is no longer used after removing json support .
1049+ format : str
1050+ The format of the weight file, should be in :code:`hdf5, npz` .
10401051
10411052 Returns
10421053 =======
@@ -1045,29 +1056,41 @@ def load_weights(self, filename, format=None):
10451056 """
10461057 if format is None :
10471058 format = filename .split ("." )[- 1 ]
1048- if format != "hdf5" :
1049- raise IOError (f"Format { format } not recognized." )
1059+ if format == "json" :
1060+ import warnings
1061+
1062+ warnings .warn (
1063+ "json format for ROQ weights is deprecated, use hdf5 instead." ,
1064+ DeprecationWarning
1065+ )
10501066 logger .info (f"Loading ROQ weights from { filename } " )
1051- weights = dict ()
1052- import h5py
1053- with h5py .File (filename , 'r' ) as f :
1054- weights ['time_samples' ] = f ['time_samples' ][()]
1067+ if format == "npz" :
1068+ weights = dict (np .load (filename ))
10551069 for basis_type in ['linear' , 'quadratic' ]:
1056- key = f'prior_range_{ basis_type } '
1057- if key in f :
1058- idxs_in_prior_range , selected_prior_ranges = \
1059- self ._select_prior_ranges (f [key ])
1060- weights [key ] = selected_prior_ranges
1061- else :
1062- idxs_in_prior_range = [0 ]
1063- key = f'frequency_nodes_{ basis_type } '
1064- if key in f :
1065- weights [key ] = [f [key ][str (i )][()]
1066- for i in idxs_in_prior_range ]
10671070 for ifo in self .interferometers :
1068- key = f"{ ifo .name } _{ basis_type } "
1069- weights [key ] = [f [key ][str (i )][()]
1070- for i in idxs_in_prior_range ]
1071+ key = f'{ ifo .name } _{ basis_type } '
1072+ weights [key ] = [weights [key ]]
1073+ else :
1074+ weights = dict ()
1075+ import h5py
1076+ with h5py .File (filename , 'r' ) as f :
1077+ weights ['time_samples' ] = f ['time_samples' ][()]
1078+ for basis_type in ['linear' , 'quadratic' ]:
1079+ key = f'prior_range_{ basis_type } '
1080+ if key in f :
1081+ idxs_in_prior_range , selected_prior_ranges = \
1082+ self ._select_prior_ranges (f [key ])
1083+ weights [key ] = selected_prior_ranges
1084+ else :
1085+ idxs_in_prior_range = [0 ]
1086+ key = f'frequency_nodes_{ basis_type } '
1087+ if key in f :
1088+ weights [key ] = [f [key ][str (i )][()]
1089+ for i in idxs_in_prior_range ]
1090+ for ifo in self .interferometers :
1091+ key = f"{ ifo .name } _{ basis_type } "
1092+ weights [key ] = [f [key ][str (i )][()]
1093+ for i in idxs_in_prior_range ]
10711094 return weights
10721095
10731096 def _get_time_resolution (self ):
0 commit comments