Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 13 additions & 39 deletions bilby/gw/likelihood/roq.py
Original file line number Diff line number Diff line change
@@ -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
)
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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
=======
Expand All @@ -1078,24 +1057,19 @@ 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.")
Comment thread
ColmTalbot marked this conversation as resolved.
if format == "json":
import warnings

warnings.warn(
"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}'
Expand Down
26 changes: 15 additions & 11 deletions test/gw/likelihood_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -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)
Expand Down
Loading