diff --git a/bilby/gw/likelihood/roq.py b/bilby/gw/likelihood/roq.py index 8f13a7ae3..493ee66c5 100644 --- a/bilby/gw/likelihood/roq.py +++ b/bilby/gw/likelihood/roq.py @@ -1,10 +1,7 @@ -import json - import numpy as np from .base import GravitationalWaveTransient -from ...core.utils import BilbyJsonEncoder, decode_bilby_json from ...core.utils import ( logger, create_frequency_series, speed_of_light, radius_of_earth ) @@ -992,32 +989,22 @@ def _set_weights_quadratic_multiband(self, quadratic_matrix, basis_idxs): def save_weights(self, filename, format='hdf5'): """ - Save ROQ weights into a single file. format should be npz, or hdf5. - For weights from multiple bases, hdf5 is only the possible option. - Support for json format is deprecated as of :code:`v2.1` and will be - removed in :code:`v2.2`, another method should be used by default. + Save ROQ weights into a single file. + Support for json format was removed in :code:`v2.7`, only hdf5 and npz are supported. Parameters ========== filename : str The name of the file to save the weights to. format : str - The format to save the data to, this should be one of - :code:`"hdf5"`, :code:`"npz"`, default=:code:`"hdf5"`. + The format to save the weight in, should be in :code:`hdf5, npz`. """ - if format not in ['json', 'npz', 'hdf5']: + if format not in ['hdf5', 'npz']: raise IOError(f"Format {format} not recognized.") - if format == "json": - import warnings - - warnings.warn( - "json format for ROQ weights is deprecated, use hdf5 instead.", - DeprecationWarning - ) if format not in filename: filename += "." + format logger.info(f"Saving ROQ weights to {filename}") - if format == 'json' or format == 'npz': + if format == 'npz': if self.number_of_bases_linear > 1 or self.number_of_bases_quadratic > 1: raise ValueError(f'Format {format} not compatible with multiple bases') weights = dict() @@ -1026,11 +1013,7 @@ def save_weights(self, filename, format='hdf5'): for ifo in self.interferometers: key = f'{ifo.name}_{basis_type}' weights[key] = self.weights[key][0] - if format == 'json': - with open(filename, 'w') as file: - json.dump(weights, file, indent=2, cls=BilbyJsonEncoder) - else: - np.savez(filename, **weights) + np.savez(filename, **weights) else: import h5py with h5py.File(filename, 'w') as f: @@ -1058,18 +1041,14 @@ def save_weights(self, filename, format='hdf5'): def load_weights(self, filename, format=None): """ - Load ROQ weights. format should be json, npz, or hdf5. - json or npz file is assumed to contain weights from a single basis. - Support for json format is deprecated as of :code:`v2.1` and will be - removed in :code:`v2.2`, another method should be used by default. + Load ROQ weights. Support for json format was removed in :code:`v2.7`. Parameters ========== filename : str The name of the file to save the weights to. format : str - The format to save the data to, this should be one of - :code:`"hdf5"`, :code:`"npz"`, default=:code:`"hdf5"`. + The format of the weight file, should be in :code:`hdf5, npz`. Returns ======= @@ -1078,8 +1057,6 @@ def load_weights(self, filename, format=None): """ if format is None: format = filename.split(".")[-1] - if format not in ["json", "npz", "hdf5"]: - raise IOError(f"Format {format} not recognized.") if format == "json": import warnings @@ -1087,15 +1064,12 @@ def load_weights(self, filename, format=None): "json format for ROQ weights is deprecated, use hdf5 instead.", DeprecationWarning ) + elif format not in ["npz", "hdf5"]: + raise IOError(f"Format {format} not recognized.") + logger.info(f"Loading ROQ weights from {filename}") - if format == "json" or format == "npz": - # Old file format assumed to contain only a single basis - if format == "json": - with open(filename, 'r') as file: - weights = json.load(file, object_hook=decode_bilby_json) - else: - # Wrap in dict to load data into memory - weights = dict(np.load(filename)) + if format == "npz": + weights = dict(np.load(filename)) for basis_type in ['linear', 'quadratic']: for ifo in self.interferometers: key = f'{ifo.name}_{basis_type}' diff --git a/test/gw/likelihood_test.py b/test/gw/likelihood_test.py index 84298e1d9..76a74c708 100644 --- a/test/gw/likelihood_test.py +++ b/test/gw/likelihood_test.py @@ -1053,6 +1053,12 @@ def test_out_single_basis(self, format): likelihood.save_weights(filename, format=format) self.assertTrue(os.path.exists(filename)) + def test_saving_wrong_format_fails(self): + likelihood = self.create_likelihood_single_basis() + filename = 'weights.json' + with self.assertRaises(IOError): + likelihood.save_weights(filename, format='json') + @parameterized.expand(['npz', 'hdf5']) def test_in_single_basis(self, format): likelihood = self.create_likelihood_single_basis() @@ -1068,18 +1074,16 @@ def test_in_single_basis(self, format): @parameterized.expand([(False, ), (True, )]) def test_out_multiple_bases(self, multiband): - format = 'hdf5' - filename = f'weights.{format}' + filename = 'weights.hdf5' likelihood = self.create_likelihood_multiple_bases(multiband) - likelihood.save_weights(filename, format=format) + likelihood.save_weights(filename, format='hdf5') self.assertTrue(os.path.exists(filename)) @parameterized.expand([(False, ), (True, )]) def test_in_multiple_bases(self, multiband): - format = 'hdf5' - filename = f'weights.{format}' + filename = 'weights.hdf5' likelihood = self.create_likelihood_multiple_bases(multiband) - likelihood.save_weights(filename, format=format) + likelihood.save_weights(filename, format='hdf5') likelihood_from_weights = bilby.gw.likelihood.ROQGravitationalWaveTransient( interferometers=likelihood.interferometers, priors=likelihood.priors, @@ -1088,15 +1092,15 @@ def test_in_multiple_bases(self, multiband): ) self.check_weights_are_same(likelihood, likelihood_from_weights) - @parameterized.expand(product(['npz', 'json'], [False, True])) - def test_out_multiple_bases_inconsistent_format(self, format, multiband): - "npz or json format is not compatible with multiple bases" + @parameterized.expand([(False, ), (True, )]) + def test_out_multiple_bases_inconsistent_format(self, multiband): + "npz format is not compatible with multiple bases" likelihood = self.create_likelihood_multiple_bases(multiband) with self.assertRaises(ValueError): - likelihood.save_weights('weights', format=format) + likelihood.save_weights('weights', format="npz") def tearDown(self): - for format in ['npz', 'json', 'hdf5']: + for format in ['npz', 'hdf5']: filename = f'weights.{format}' if os.path.exists(filename): os.remove(filename)