diff --git a/bilby/gw/detector.py b/bilby/gw/detector.py index f645a9632e43e8aeba1c6d4c8cac7072c1edd96b..32a81925881701d59007fd2f8c3ca06be1d73416 100644 --- a/bilby/gw/detector.py +++ b/bilby/gw/detector.py @@ -209,38 +209,42 @@ class InterferometerList(list): super(InterferometerList, self).insert(index, interferometer) self._check_interferometers() + @staticmethod + def _hdf5_filename_from_outdir_label(outdir, label): + return os.path.join(outdir, label + '.h5') + def to_hdf5(self, outdir='outdir', label='ifo_list'): """ Saves the object to a hdf5 file - Attributes + Parameters ---------- outdir: str, optional - Output directory name of the file. Will be created if it does not exist yet. + Output directory name of the file label: str, optional - Output file name, is 'ifo_list' if not given otherwise + Output file name, is 'ifo_list' if not given otherwise. A list of + the included interferometers will be appended. """ if sys.version_info[0] < 3: raise NotImplementedError('Pickling of InterferometerList is not supported in Python 2.' 'Use Python 3 instead.') - utils.check_directory_exists_and_if_not_mkdir('outdir') - dd.io.save('./' + outdir + '/' + label + '.h5', self) + label = label + '_' + ''.join(ifo.name for ifo in self) + utils.check_directory_exists_and_if_not_mkdir(outdir) + dd.io.save(self._hdf5_filename_from_outdir_label(outdir, label), self) @classmethod - def from_hdf5(cls, path, label): + def from_hdf5(cls, filename=None): """ Loads in an InterferometerList object from an hdf5 file - Attributes + Parameters ---------- - path: str - Path to the hdf5 file. - label: str - Name of the hdf5 file without the .'h5' + filename: str + If given, try to load from this filename """ if sys.version_info[0] < 3: raise NotImplementedError('Pickling of InterferometerList is not supported in Python 2.' 'Use Python 3 instead.') - res = dd.io.load('./' + path + '/' + label + '.h5') + res = dd.io.load(filename) if res.__class__ == list: res = cls(res) if res.__class__ != cls: @@ -1640,15 +1644,19 @@ class Interferometer(object): fig.savefig( '{}/{}_{}_time_domain_data.png'.format(outdir, self.name, label)) + @staticmethod + def _hdf5_filename_from_outdir_label(outdir, label): + return os.path.join(outdir, label + '.h5') + def to_hdf5(self, outdir='outdir', label=None): - """ Saves the object to a hdf5 file + """ Save the object to a hdf5 file Attributes ---------- outdir: str, optional - Output directory name of the file. Will be created if it does not exist yet. + Output directory name of the file, defaults to 'outdir'. label: str, optional - Output file name, is self.name if not given otherwise + Output file name, is self.name if not given otherwise. """ if sys.version_info[0] < 3: raise NotImplementedError('Pickling of Interferometer is not supported in Python 2.' @@ -1656,26 +1664,26 @@ class Interferometer(object): if label is None: label = self.name utils.check_directory_exists_and_if_not_mkdir('outdir') - dd.io.save('./' + outdir + '/' + label + '.h5', self) + filename = self._hdf5_filename_from_outdir_label(outdir, label) + dd.io.save(filename, self) @classmethod - def from_hdf5(cls, path, label): + def from_hdf5(cls, filename=None): """ Loads in an Interferometer object from an hdf5 file - Attributes + Parameters ---------- - path: str - Path to the hdf5 file. - label: str - Name of the hdf5 file without the .'h5' + filename: str + If given, try to load from this filename """ if sys.version_info[0] < 3: raise NotImplementedError('Pickling of Interferometer is not supported in Python 2.' 'Use Python 3 instead.') - res = dd.io.load('./' + path + '/' + label + '.h5') + + res = dd.io.load(filename) if res.__class__ != cls: - raise TypeError('The loaded object is not a Interferometer') + raise TypeError('The loaded object is not an Interferometer') return res diff --git a/test/detector_test.py b/test/detector_test.py index ff7f81f0587fe5e70b701662b383d57a800699a2..2edbeca291d208d339e131d3c3ec278ba0dce809 100644 --- a/test/detector_test.py +++ b/test/detector_test.py @@ -361,7 +361,8 @@ class TestInterferometer(unittest.TestCase): self.ifo.to_hdf5(outdir='outdir', label='test') else: self.ifo.to_hdf5(outdir='outdir', label='test') - recovered_ifo = bilby.gw.detector.Interferometer.from_hdf5(path='outdir', label='test') + filename = self.ifo._hdf5_filename_from_outdir_label(outdir='outdir', label='test') + recovered_ifo = bilby.gw.detector.Interferometer.from_hdf5(filename) self.assertEqual(self.ifo, recovered_ifo) def test_to_and_from_hdf5_wrong_class(self): @@ -370,8 +371,9 @@ class TestInterferometer(unittest.TestCase): else: bilby.core.utils.check_directory_exists_and_if_not_mkdir('outdir') dd.io.save('./outdir/psd.h5', self.power_spectral_density) + filename = self.ifo._hdf5_filename_from_outdir_label(outdir='outdir', label='psd') with self.assertRaises(TypeError): - bilby.gw.detector.Interferometer.from_hdf5(path='outdir', label='psd') + bilby.gw.detector.Interferometer.from_hdf5(filename) class TestInterferometerEquals(unittest.TestCase): @@ -1072,7 +1074,8 @@ class TestInterferometerList(unittest.TestCase): self.ifo_list.to_hdf5(outdir='outdir', label='test') else: self.ifo_list.to_hdf5(outdir='outdir', label='test') - recovered_ifo = bilby.gw.detector.InterferometerList.from_hdf5(path='outdir', label='test') + filename = 'outdir/test_name1name2.h5' + recovered_ifo = bilby.gw.detector.InterferometerList.from_hdf5(filename) self.assertListEqual(self.ifo_list, recovered_ifo) def test_to_and_from_hdf5_wrong_class(self): @@ -1080,8 +1083,10 @@ class TestInterferometerList(unittest.TestCase): pass else: dd.io.save('./outdir/psd.h5', self.ifo_list[0].power_spectral_density) + filename = self.ifo_list._hdf5_filename_from_outdir_label( + outdir='outdir', label='psd') with self.assertRaises(TypeError): - bilby.gw.detector.InterferometerList.from_hdf5(path='outdir', label='psd') + bilby.gw.detector.InterferometerList.from_hdf5(filename) class TestPowerSpectralDensityWithoutFiles(unittest.TestCase):