Skip to content

Commit b44bbdc

Browse files
ColmTalbotmj-will
andauthored
MAINT: remove unsupported roq json weight file format (#945)
* MAINT: remove unsupported roq json weight file format * BUG: CI fixes * DEP: restore npz weight format * TYPO: add missing line * TST: fix broken test * Revert some changes raised in review * Update removal version in docs Co-authored-by: Michael J. Williams <michaeljw1@googlemail.com> * DOC: fix json roq weight removal version --------- Co-authored-by: Michael J. Williams <michaeljw1@googlemail.com>
1 parent 16508fc commit b44bbdc

2 files changed

Lines changed: 28 additions & 50 deletions

File tree

bilby/gw/likelihood/roq.py

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11

2-
import json
3-
42
import numpy as np
53

64
from .base import GravitationalWaveTransient
7-
from ...core.utils import BilbyJsonEncoder, decode_bilby_json
85
from ...core.utils import (
96
logger, create_frequency_series, speed_of_light, radius_of_earth
107
)
@@ -992,32 +989,22 @@ def _set_weights_quadratic_multiband(self, quadratic_matrix, basis_idxs):
992989

993990
def save_weights(self, filename, format='hdf5'):
994991
"""
995-
Save ROQ weights into a single file. format should be npz, or hdf5.
996-
For weights from multiple bases, hdf5 is only the possible option.
997-
Support for json format is deprecated as of :code:`v2.1` and will be
998-
removed in :code:`v2.2`, another method should be used by default.
992+
Save ROQ weights into a single file.
993+
Support for json format was removed in :code:`v2.7`, only hdf5 and npz are supported.
999994
1000995
Parameters
1001996
==========
1002997
filename : str
1003998
The name of the file to save the weights to.
1004999
format : str
1005-
The format to save the data to, this should be one of
1006-
:code:`"hdf5"`, :code:`"npz"`, default=:code:`"hdf5"`.
1000+
The format to save the weight in, should be in :code:`hdf5, npz`.
10071001
"""
1008-
if format not in ['json', 'npz', 'hdf5']:
1002+
if format not in ['hdf5', 'npz']:
10091003
raise IOError(f"Format {format} not recognized.")
1010-
if format == "json":
1011-
import warnings
1012-
1013-
warnings.warn(
1014-
"json format for ROQ weights is deprecated, use hdf5 instead.",
1015-
DeprecationWarning
1016-
)
10171004
if format not in filename:
10181005
filename += "." + format
10191006
logger.info(f"Saving ROQ weights to {filename}")
1020-
if format == 'json' or format == 'npz':
1007+
if format == 'npz':
10211008
if self.number_of_bases_linear > 1 or self.number_of_bases_quadratic > 1:
10221009
raise ValueError(f'Format {format} not compatible with multiple bases')
10231010
weights = dict()
@@ -1026,11 +1013,7 @@ def save_weights(self, filename, format='hdf5'):
10261013
for ifo in self.interferometers:
10271014
key = f'{ifo.name}_{basis_type}'
10281015
weights[key] = self.weights[key][0]
1029-
if format == 'json':
1030-
with open(filename, 'w') as file:
1031-
json.dump(weights, file, indent=2, cls=BilbyJsonEncoder)
1032-
else:
1033-
np.savez(filename, **weights)
1016+
np.savez(filename, **weights)
10341017
else:
10351018
import h5py
10361019
with h5py.File(filename, 'w') as f:
@@ -1058,18 +1041,14 @@ def save_weights(self, filename, format='hdf5'):
10581041

10591042
def load_weights(self, filename, format=None):
10601043
"""
1061-
Load ROQ weights. format should be json, npz, or hdf5.
1062-
json or npz file is assumed to contain weights from a single basis.
1063-
Support for json format is deprecated as of :code:`v2.1` and will be
1064-
removed in :code:`v2.2`, another method should be used by default.
1044+
Load ROQ weights. Support for json format was removed in :code:`v2.7`.
10651045
10661046
Parameters
10671047
==========
10681048
filename : str
10691049
The name of the file to save the weights to.
10701050
format : str
1071-
The format to save the data to, this should be one of
1072-
:code:`"hdf5"`, :code:`"npz"`, default=:code:`"hdf5"`.
1051+
The format of the weight file, should be in :code:`hdf5, npz`.
10731052
10741053
Returns
10751054
=======
@@ -1078,24 +1057,19 @@ def load_weights(self, filename, format=None):
10781057
"""
10791058
if format is None:
10801059
format = filename.split(".")[-1]
1081-
if format not in ["json", "npz", "hdf5"]:
1082-
raise IOError(f"Format {format} not recognized.")
10831060
if format == "json":
10841061
import warnings
10851062

10861063
warnings.warn(
10871064
"json format for ROQ weights is deprecated, use hdf5 instead.",
10881065
DeprecationWarning
10891066
)
1067+
elif format not in ["npz", "hdf5"]:
1068+
raise IOError(f"Format {format} not recognized.")
1069+
10901070
logger.info(f"Loading ROQ weights from {filename}")
1091-
if format == "json" or format == "npz":
1092-
# Old file format assumed to contain only a single basis
1093-
if format == "json":
1094-
with open(filename, 'r') as file:
1095-
weights = json.load(file, object_hook=decode_bilby_json)
1096-
else:
1097-
# Wrap in dict to load data into memory
1098-
weights = dict(np.load(filename))
1071+
if format == "npz":
1072+
weights = dict(np.load(filename))
10991073
for basis_type in ['linear', 'quadratic']:
11001074
for ifo in self.interferometers:
11011075
key = f'{ifo.name}_{basis_type}'

test/gw/likelihood_test.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,12 @@ def test_out_single_basis(self, format):
10531053
likelihood.save_weights(filename, format=format)
10541054
self.assertTrue(os.path.exists(filename))
10551055

1056+
def test_saving_wrong_format_fails(self):
1057+
likelihood = self.create_likelihood_single_basis()
1058+
filename = 'weights.json'
1059+
with self.assertRaises(IOError):
1060+
likelihood.save_weights(filename, format='json')
1061+
10561062
@parameterized.expand(['npz', 'hdf5'])
10571063
def test_in_single_basis(self, format):
10581064
likelihood = self.create_likelihood_single_basis()
@@ -1068,18 +1074,16 @@ def test_in_single_basis(self, format):
10681074

10691075
@parameterized.expand([(False, ), (True, )])
10701076
def test_out_multiple_bases(self, multiband):
1071-
format = 'hdf5'
1072-
filename = f'weights.{format}'
1077+
filename = 'weights.hdf5'
10731078
likelihood = self.create_likelihood_multiple_bases(multiband)
1074-
likelihood.save_weights(filename, format=format)
1079+
likelihood.save_weights(filename, format='hdf5')
10751080
self.assertTrue(os.path.exists(filename))
10761081

10771082
@parameterized.expand([(False, ), (True, )])
10781083
def test_in_multiple_bases(self, multiband):
1079-
format = 'hdf5'
1080-
filename = f'weights.{format}'
1084+
filename = 'weights.hdf5'
10811085
likelihood = self.create_likelihood_multiple_bases(multiband)
1082-
likelihood.save_weights(filename, format=format)
1086+
likelihood.save_weights(filename, format='hdf5')
10831087
likelihood_from_weights = bilby.gw.likelihood.ROQGravitationalWaveTransient(
10841088
interferometers=likelihood.interferometers,
10851089
priors=likelihood.priors,
@@ -1088,15 +1092,15 @@ def test_in_multiple_bases(self, multiband):
10881092
)
10891093
self.check_weights_are_same(likelihood, likelihood_from_weights)
10901094

1091-
@parameterized.expand(product(['npz', 'json'], [False, True]))
1092-
def test_out_multiple_bases_inconsistent_format(self, format, multiband):
1093-
"npz or json format is not compatible with multiple bases"
1095+
@parameterized.expand([(False, ), (True, )])
1096+
def test_out_multiple_bases_inconsistent_format(self, multiband):
1097+
"npz format is not compatible with multiple bases"
10941098
likelihood = self.create_likelihood_multiple_bases(multiband)
10951099
with self.assertRaises(ValueError):
1096-
likelihood.save_weights('weights', format=format)
1100+
likelihood.save_weights('weights', format="npz")
10971101

10981102
def tearDown(self):
1099-
for format in ['npz', 'json', 'hdf5']:
1103+
for format in ['npz', 'hdf5']:
11001104
filename = f'weights.{format}'
11011105
if os.path.exists(filename):
11021106
os.remove(filename)

0 commit comments

Comments
 (0)