diff --git a/bilby/gw/detector.py b/bilby/gw/detector.py index 07ca348a6ea0ea528e0f2571c2bfe37b3598d69f..f645a9632e43e8aeba1c6d4c8cac7072c1edd96b 100644 --- a/bilby/gw/detector.py +++ b/bilby/gw/detector.py @@ -219,6 +219,9 @@ class InterferometerList(list): label: str, optional Output file name, is 'ifo_list' if not given otherwise """ + 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) @@ -234,6 +237,9 @@ class InterferometerList(list): Name of the hdf5 file without the .'h5' """ + 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') if res.__class__ == list: res = cls(res) @@ -1644,24 +1650,13 @@ class Interferometer(object): label: str, optional 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.' + 'Use Python 3 instead.') if label is None: label = self.name utils.check_directory_exists_and_if_not_mkdir('outdir') - - if sys.version_info[0] < 3: - serialisable = self.create_python2_serializable() - else: - serialisable = self - - dd.io.save('./' + outdir + '/' + label + '.h5', serialisable) - - def create_python2_serializable(self): - serialisable = self.__dict__ - serialisable['power_spectral_density'] = self.power_spectral_density.__dict__ - serialisable['calibration_model'] = self.strain_data.__dict__ - serialisable['strain_data'] = self.strain_data.__dict__ - serialisable['strain_data']['_times_and_frequencies'] = self.strain_data._times_and_frequencies - return serialisable + dd.io.save('./' + outdir + '/' + label + '.h5', self) @classmethod def from_hdf5(cls, path, label): @@ -1675,27 +1670,12 @@ class Interferometer(object): Name of the hdf5 file without the .'h5' """ - res = dd.io.load('./' + path + '/' + label + '.h5') if sys.version_info[0] < 3: - psd = PowerSpectralDensity(frequency_array=res['power_spectral_density']['frequency_array'], - psd_array=res['power_spectral_density']['psd_array'], - asd_array=res['power_spectral_density']['asd_array'], - psd_file=res['power_spectral_density']['psd_file'], - asd_file=res['power_spectral_density']['asd_file']) - strain = InterferometerStrainData(minimum_frequency=res['_strain_data']['minimum_frequency'], - maximum_frequency=res['_strain_data']['maximum_frequency'], - roll_off=res['_strain_data']['roll_off']) - strain.window_factor = res['_strain_data']['window_factor'] - strain._times_and_frequencies._frequency_array = res['_strain_data']['_frequency_array'] - strain._times_and_frequencies._time_array = res['_strain_data']['time_array'] - strain._times_and_frequencies._sampling_frequency = res['_strain_data']['_time_domain_strain'] - - strain._frequency_domain_strain = res['_strain_data']['_frequency_domain_strain'] - strain._time_domain_strain = res['_strain_data']['_time_domain_strain'] - - + raise NotImplementedError('Pickling of Interferometer is not supported in Python 2.' + 'Use Python 3 instead.') + res = dd.io.load('./' + path + '/' + label + '.h5') if res.__class__ != cls: - raise TypeError('The loaded object is not a InterferometerList') + raise TypeError('The loaded object is not a Interferometer') return res diff --git a/test/detector_test.py b/test/detector_test.py index e46d63e97ec89d8a17529217a5b70c35487a45cb..b830fd5463d6823d0611b1c8f61b07bb40d7cba9 100644 --- a/test/detector_test.py +++ b/test/detector_test.py @@ -8,6 +8,7 @@ from mock import patch import numpy as np import scipy.signal.windows import os +import sys from shutil import rmtree import logging import deepdish as dd @@ -355,15 +356,23 @@ class TestInterferometer(unittest.TestCase): self.assertEqual(expected, repr(self.ifo)) def test_to_and_from_hdf5_loading(self): - self.ifo.to_hdf5(outdir='outdir', label='test') - recovered_ifo = bilby.gw.detector.Interferometer.from_hdf5(path='outdir', label='test') - self.assertEqual(self.ifo, recovered_ifo) + if sys.version_info[0] < 3: + with self.assertRaises(NotImplementedError): + 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') + self.assertEqual(self.ifo, recovered_ifo) def test_to_and_from_hdf5_wrong_class(self): - bilby.core.utils.check_directory_exists_and_if_not_mkdir('outdir') - dd.io.save('./outdir/psd.h5', self.power_spectral_density) - with self.assertRaises(TypeError): - bilby.gw.detector.Interferometer.from_hdf5(path='outdir', label='psd') + if sys.version_info[0] < 3: + with self.assertRaises(NotImplementedError): + dd.io.save('./outdir/psd.h5', self.power_spectral_density) + else: + bilby.core.utils.check_directory_exists_and_if_not_mkdir('outdir') + dd.io.save('./outdir/psd.h5', self.power_spectral_density) + with self.assertRaises(TypeError): + bilby.gw.detector.Interferometer.from_hdf5(path='outdir', label='psd') class TestInterferometerEquals(unittest.TestCase): @@ -1059,14 +1068,22 @@ class TestInterferometerList(unittest.TestCase): self.assertListEqual([self.ifo1.name, new_ifo.name, self.ifo2.name], names) def test_to_and_from_hdf5_loading(self): - self.ifo_list.to_hdf5(outdir='outdir', label='test') - recovered_ifo = bilby.gw.detector.InterferometerList.from_hdf5(path='outdir', label='test') - self.assertListEqual(self.ifo_list, recovered_ifo) + if sys.version_info[0] < 3: + with self.assertRaises(NotImplementedError): + 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') + self.assertListEqual(self.ifo_list, recovered_ifo) def test_to_and_from_hdf5_wrong_class(self): - dd.io.save('./outdir/psd.h5', self.ifo_list[0].power_spectral_density) - with self.assertRaises(TypeError): - bilby.gw.detector.InterferometerList.from_hdf5(path='outdir', label='psd') + if sys.version_info[0] < 3: + with self.assertRaises(NotImplementedError): + dd.io.save('./outdir/psd.h5', self.ifo_list[0].power_spectral_density) + else: + dd.io.save('./outdir/psd.h5', self.ifo_list[0].power_spectral_density) + with self.assertRaises(TypeError): + bilby.gw.detector.InterferometerList.from_hdf5(path='outdir', label='psd') class TestPowerSpectralDensityWithoutFiles(unittest.TestCase):