def test_ratio(self, array, ratio): rat = array.ratio(ratio) array_meth = getattr(array, ratio) utils.assert_quantity_sub_equal(rat, array / array_meth(axis=0)) with pytest.raises(ValueError): array.ratio('blah')
def test_get(self, losc_16384): # get using datafind (maybe) try: ts = self.TEST_CLASS.get(FIND_CHANNEL, *LOSC_GW150914_SEGMENT, frametype_match='C01\Z') except (ImportError, RuntimeError) as e: pytest.skip(str(e)) utils.assert_quantity_sub_equal(ts, losc_16384, exclude=['name', 'channel', 'unit']) # get using NDS2 (if datafind could have been used to start with) try: dfs = os.environ.pop('LIGO_DATAFIND_SERVER') except KeyError: dfs = None else: ts2 = self.TEST_CLASS.get(FIND_CHANNEL, *LOSC_GW150914_SEGMENT) utils.assert_quantity_sub_equal(ts, ts2, exclude=['channel', 'unit']) finally: if dfs is not None: os.environ['LIGO_DATAFIND_SERVER'] = dfs
def test_new(self): """Test `gwpy.timeseries.TimeSeriesBase` constructor """ array = super(TestTimeSeriesBase, self).test_new() # check time-domain metadata assert array.epoch == GPS_EPOCH assert array.sample_rate == units.Quantity(1, 'Hertz') assert array.dt == units.Quantity(1, 'second') # check handling of epoch vs t0 a = self.create(epoch=10) b = self.create(t0=10) utils.assert_quantity_sub_equal(a, b) with pytest.raises(ValueError) as exc: self.TEST_CLASS(self.data, epoch=1, t0=1) assert str(exc.value) == 'give only one of epoch or t0' # check handling of sample_rate vs dt a = self.create(sample_rate=100) b = self.create(dt=0.01) utils.assert_quantity_sub_equal(a, b) with pytest.raises(ValueError) as exc: self.TEST_CLASS(self.data, sample_rate=1, dt=1) assert str(exc.value) == 'give only one of sample_rate or dt'
def test_copy(self, instance): copy = instance.copy() assert isinstance(copy, self.TEST_CLASS) for key in copy: assert not numpy.shares_memory(copy[key].value, instance[key].value) utils.assert_quantity_sub_equal(copy[key], instance[key])
def test_read_write_gwf(self, instance): with tempfile.NamedTemporaryFile(suffix='.gwf') as f: instance.write(f.name) new = self.TEST_CLASS.read(f.name, instance.keys()) for key in new: utils.assert_quantity_sub_equal(new[key], instance[key], exclude=['channel'])
def test_read_write_hdf5(self, instance): with tempfile.NamedTemporaryFile(suffix='.hdf5') as f: instance.write(f.name, overwrite=True) new = self.TEST_CLASS.read(f.name, instance.keys()) for key in new: utils.assert_quantity_sub_equal(new[key], instance[key]) # check auto-detection of names new = self.TEST_CLASS.read(f.name) for key in new: utils.assert_quantity_sub_equal(new[key], instance[key])
def test_average_fft(self, losc): # test all defaults fs = losc.average_fft() utils.assert_quantity_sub_equal(fs, losc.detrend().fft()) # test fftlength fs = losc.average_fft(fftlength=0.5) assert fs.size == 0.5 * losc.sample_rate.value // 2 + 1 assert fs.df == 2 * units.Hertz fs = losc.average_fft(fftlength=0.4, overlap=0.2)
def test_getitem(self, array): utils.assert_quantity_sub_equal( array[0::2, 0], self.TEST_CLASS._rowclass( array.value[0::2, 0], x0=array.x0, dx=array.dx*2, name=array.name, unit=array.unit, channel=array.channel, epoch=array.epoch, ), ) with pytest.raises(NotImplementedError) as exc: array[0, ::2] assert str(exc.value) == 'cannot slice SpectralVariance across bins'
def test_csd(self, losc): # test all defaults fs = losc.csd(losc) utils.assert_quantity_sub_equal(fs, losc.psd(), exclude=['name']) # test fftlength fs = losc.csd(losc, fftlength=0.5) assert fs.size == 0.5 * losc.sample_rate.value // 2 + 1 assert fs.df == 2 * units.Hertz # test overlap losc.csd(losc, fftlength=0.4, overlap=0.2)
def test_event_rates(self, table): rate = table.event_rate(1) assert isinstance(rate, TimeSeries) assert rate.sample_rate == 1 * units.Hz # repeat with object dtype try: from lal import LIGOTimeGPS except ImportError: return lgps = list(map(LIGOTimeGPS, table['time'])) t2 = type(table)(data=[lgps], names=['time']) rate2 = t2.event_rate(1, start=table['time'].min()) utils.assert_quantity_sub_equal(rate, rate2)
def test_getitem(self, array, create_kwargs): array = self.create(name='test_getitem', **create_kwargs) # test element returns as quantity element = array[0, 0] assert element == array[0][0] assert isinstance(element, units.Quantity) utils.assert_quantity_equal(element, array.value[0, 0] * array.unit) # test column slice returns as _columnclass utils.assert_quantity_sub_equal(array[2], array[2, :]) column = array[0, 0::2] utils.assert_quantity_sub_equal(column, self.TEST_CLASS._columnclass( array.value[0, 0::2], x0=array.y0, dx=array.dy*2, name=array.name, channel=array.channel, unit=array.unit, epoch=array.epoch)) # test row slice returns as _rowclass row = array[1:10:3, 0] utils.assert_array_equal(row.value, array.value[1:10:3, 0]) utils.assert_quantity_sub_equal(row, self.TEST_CLASS._rowclass( array.value[1:10:3, 0], x0=array.x0+array.dx, dx=array.dx*3, name=array.name, channel=array.channel, unit=array.unit), exclude=['epoch']) # test dual slice returns type(self) with metadata subarray = array[1:5:2, 1:5:2] utils.assert_quantity_sub_equal(subarray, self.TEST_CLASS( array.value[1:5:2, 1:5:2], x0=array.x0+array.dx, dx=array.dx*2, y0=array.y0+array.dy, dy=array.dy*2, name=array.name, channel=array.channel, unit=array.unit), exclude=['epoch'])
def test_new(self): super(TestSpectrogram, self).test_new() # check handling of epoch vs t0 a = self.create(epoch=10) b = self.create(t0=10) utils.assert_quantity_sub_equal(a, b) with pytest.raises(ValueError) as exc: self.TEST_CLASS(self.data, epoch=1, t0=1) assert str(exc.value) == 'give only one of epoch or t0' # check times times = numpy.arange(self.data.shape[0]) a = self.create(times=times) utils.assert_quantity_equal(a.times, times * units.second)
def test_fetch(self): ts = self.create(name='X1:TEST', t0=1000000000, unit='m') nds_buffer = mocks.nds2_buffer_from_timeseries(ts) nds_connection = mocks.nds2_connection(buffers=[nds_buffer]) with mock.patch('nds2.connection') as mock_connection, \ mock.patch('nds2.buffer', nds_buffer): mock_connection.return_value = nds_connection # use verbose=True to hit more lines ts2 = self.TEST_CLASS.fetch('X1:TEST', *ts.span, verbose=True) # check open connection works ts2 = self.TEST_CLASS.fetch('X1:TEST', *ts.span, verbose=True, connection=nds_connection) utils.assert_quantity_sub_equal(ts, ts2, exclude=['channel'])
def test_crop_frequencies(self): array = self.create(f0=0, df=1) # test simple array2 = array.crop_frequencies() utils.assert_quantity_sub_equal(array, array2) # test normal array2 = array.crop_frequencies(2, 5) utils.assert_array_equal(array2.value, array.value[:, 2:5]) assert array2.f0 == 2 * units.Hertz assert array2.df == array.df # test warnings with pytest.warns(UserWarning): array.crop_frequencies(array.yspan[0] - 1, array.yspan[1]) with pytest.warns(UserWarning): array.crop_frequencies(array.yspan[0], array.yspan[1] + 1)
def test_psd(self, losc): # test all defaults fs = losc.psd() assert isinstance(fs, FrequencySeries) assert fs.size == losc.size // 2 + 1 assert fs.f0 == 0 * units.Hz assert fs.df == 1 / losc.duration assert fs.channel is losc.channel assert fs.unit == losc.unit**2 / units.Hz # test fftlength fs = losc.psd(fftlength=0.5) assert fs.size == 0.5 * losc.sample_rate.value // 2 + 1 assert fs.df == 2 * units.Hz # test overlap fs = losc.psd(fftlength=0.4, overlap=0.2) # test default overlap fs2 = losc.psd(fftlength=.4) utils.assert_quantity_sub_equal(fs, fs2) # test methods losc.psd(fftlength=0.4, overlap=0.2, method='welch') losc.psd(fftlength=0.4, method='bartlett') try: losc.psd(fftlength=0.4, overlap=0.2, method='lal-welch') losc.psd(fftlength=0.4, method='lal-bartlett') losc.psd(fftlength=0.4, overlap=0.2, method='median-mean') losc.psd(fftlength=0.4, overlap=0.2, method='median') except ImportError as e: pass else: # test LAL method with window specification losc.psd(fftlength=0.4, overlap=0.2, method='median-mean', window='hann') # test LAL method with non-canonical window specification losc.psd(fftlength=0.4, overlap=0.2, method='median-mean', window='hanning') # test check for at least two averages (defaults to single FFT) with pytest.raises(ValueError) as e: losc.psd(method='median-mean')
def test_getitem(self, array): utils.assert_quantity_sub_equal( array[0::2, 0], self.TEST_CLASS._rowclass( array.value[0::2, 0], x0=array.x0, dx=array.dx * 2, name=array.name, unit=array.unit, channel=array.channel, epoch=array.epoch, ), ) with pytest.raises(NotImplementedError) as exc: array[0, ::2] assert str(exc.value) == 'cannot slice SpectralVariance across bins'
def test_spectrogram2(self, losc): # test defaults sg = losc.spectrogram2(1) utils.assert_quantity_sub_equal(sg, losc.spectrogram(1)) # test fftlength sg = losc.spectrogram2(0.5) assert sg.shape == (8, 0.5 * losc.sample_rate.value // 2 + 1) assert sg.df == 2 * units.Hertz assert sg.dt == 0.5 * units.second # test overlap sg = losc.spectrogram2(fftlength=0.25, overlap=0.24) assert sg.shape == (399, 0.25 * losc.sample_rate.value // 2 + 1) assert sg.df == 4 * units.Hertz # note: bizarre stride length because 4096/100 gets rounded assert sg.dt == 0.010009765625 * units.second
def test_to_from_pycbc(self, array): from pycbc.types import TimeSeries as PyCBCTimeSeries # test default conversion pycbcts = array.to_pycbc() assert isinstance(pycbcts, PyCBCTimeSeries) nptest.assert_array_equal(array.value, pycbcts.data) assert array.t0.value == pycbcts.start_time assert array.dt.value == pycbcts.delta_t # go back and check we get back what we put in in the first place a2 = type(array).from_pycbc(pycbcts) utils.assert_quantity_sub_equal(array, a2, exclude=['name', 'unit', 'channel']) # test copy=False a2 = type(array).from_pycbc(array.to_pycbc(copy=False), copy=False) assert numpy.shares_memory(array.value, a2.value)
def test_find(self, losc_16384): ts = self.TEST_CLASS.find(FIND_CHANNEL, *LOSC_GW150914_SEGMENT, frametype=FIND_FRAMETYPE) utils.assert_quantity_sub_equal(ts, losc_16384, exclude=['name', 'channel', 'unit']) # test observatory ts2 = self.TEST_CLASS.find(FIND_CHANNEL, *LOSC_GW150914_SEGMENT, frametype=FIND_FRAMETYPE, observatory=FIND_CHANNEL[0]) utils.assert_quantity_sub_equal(ts, ts2) with pytest.raises(RuntimeError): self.TEST_CLASS.find(FIND_CHANNEL, *LOSC_GW150914_SEGMENT, frametype=FIND_FRAMETYPE, observatory='X')
def test_append(self, instance): # test appending from empty (with and without copy) for copy in (True, False): new = type(instance)() new.append(instance, copy=copy) for key in new: assert numpy.shares_memory(new[key].value, instance[key].value) is not copy utils.assert_quantity_sub_equal(new[key], instance[key]) # create copy of dict that is contiguous new = type(instance)() for key in instance: a = instance[key] new[key] = type(a)([1, 2, 3, 4, 5], x0=a.xspan[1], dx=a.dx, dtype=a.dtype) # append and test b = instance.copy() b.append(new) for key in b: utils.assert_array_equal( b[key].value, numpy.concatenate((instance[key].value, new[key].value))) # create copy of dict that is discontiguous new = type(instance)() for key in instance: a = instance[key] new[key] = type(a)([1, 2, 3, 4, 5], x0=a.xspan[1], dx=a.dx, dtype=a.dtype) # check error with pytest.raises(ValueError): instance.append(new) # check padding works (don't validate too much, that is tested # elsewhere) b = instance.copy() b.append(new, pad=0)
def test_to_from_lal(self, array): import lal # check that to + from returns the same array lalts = array.to_lal() a2 = type(array).from_lal(lalts) utils.assert_quantity_sub_equal(array, a2, exclude=['name', 'channel']) assert a2.name is '' # test copy=False a2 = type(array).from_lal(lalts, copy=False) assert numpy.shares_memory(a2.value, lalts.data.data) # test units array.override_unit('undef') with pytest.warns(UserWarning): lalts = array.to_lal() assert lalts.sampleUnits == lal.DimensionlessUnit a2 = self.TEST_CLASS.from_lal(lalts) assert a2.unit is units.dimensionless_unscaled
def test_fetch_open_data(self, losc, format): try: ts = self.TEST_CLASS.fetch_open_data(LOSC_IFO, *LOSC_GW150914_SEGMENT, format=format) except URLError as e: pytest.skip(str(e)) utils.assert_quantity_sub_equal(ts, losc, exclude=['name', 'unit']) # try again with 16384 Hz data ts = self.TEST_CLASS.fetch_open_data(LOSC_IFO, *LOSC_GW150914_SEGMENT, format=format, sample_rate=16384) assert ts.sample_rate == 16384 * units.Hz # make sure errors happen with pytest.raises(ValueError) as exc: self.TEST_CLASS.fetch_open_data(LOSC_IFO, 0, 1, format=format) assert str(exc.value) == ( "Cannot find a LOSC dataset for %s covering [0, 1)" % LOSC_IFO)
def test_to_from_pycbc(self, array): from pycbc.types import FrequencySeries as PyCBCFrequencySeries array.epoch = 0 # test default conversion pycbcfs = array.to_pycbc() assert isinstance(pycbcfs, PyCBCFrequencySeries) utils.assert_array_equal(array.value, pycbcfs.data) assert array.f0.value == 0 * units.Hz assert array.df.value == pycbcfs.delta_f assert array.epoch.gps == pycbcfs.epoch # go back and check we get back what we put in in the first place a2 = type(array).from_pycbc(pycbcfs) utils.assert_quantity_sub_equal( array, a2, exclude=['name', 'unit', 'channel']) # test copy=False a2 = type(array).from_pycbc(array.to_pycbc(copy=False), copy=False) assert shares_memory(array.value, a2.value)
def test_read_write_hdf5(self, ext): array = self.create() array.channel = 'X1:TEST-CHANNEL' with tempfile.NamedTemporaryFile(suffix='.%s' % ext) as f: # check array with no name fails with pytest.raises(ValueError) as exc: array.write(f.name, overwrite=True) assert str(exc.value).startswith('Cannot determine HDF5 path') array.name = 'TEST' # write array (with auto-identify) array.write(f.name, overwrite=True) # check reading gives the same data (with/without auto-identify) ts = type(array).read(f.name, format='hdf5') utils.assert_quantity_sub_equal(array, ts) ts = type(array).read(f.name) utils.assert_quantity_sub_equal(array, ts) # check that we can't then write the same data again with pytest.raises(IOError): array.write(f.name) with pytest.raises(RuntimeError): array.write(f.name, append=True) # check reading with start/end works start, end = array.span.contract(25) t = type(array).read(f, start=start, end=end) utils.assert_quantity_sub_equal(t, array.crop(start, end))
def test_q_transform(self, losc): # test simple q-transform qspecgram = losc.q_transform(method='welch', fftlength=2) assert isinstance(qspecgram, Spectrogram) assert qspecgram.shape == (4000, 2403) assert qspecgram.q == 5.65685424949238 nptest.assert_almost_equal(qspecgram.value.max(), 146.61970478954652) # test whitening args asd = losc.asd(2, 1) qsg2 = losc.q_transform(method='welch', whiten=asd) utils.assert_quantity_sub_equal(qspecgram, qsg2) asd = losc.asd(.5, .25) qsg2 = losc.q_transform(method='welch', whiten=asd) qsg3 = losc.q_transform(method='welch', fftlength=.5, overlap=.25) utils.assert_quantity_sub_equal(qsg2, qsg3) # make sure frequency too high presents warning with pytest.warns(UserWarning): qspecgram = losc.q_transform(method='welch', frange=(0, 10000)) nptest.assert_almost_equal(qspecgram.yspan[1], 1291.5316316157107) # test other normalisations work (or don't) q2 = losc.q_transform(method='welch', norm='median') utils.assert_quantity_sub_equal(qspecgram, q2) losc.q_transform(method='welch', norm='mean') losc.q_transform(method='welch', norm=False) with pytest.raises(ValueError): losc.q_transform(method='welch', norm='blah')
def test_q_transform(self, losc): # test simple q-transform qspecgram = losc.q_transform(method='welch', fftlength=2) assert isinstance(qspecgram, Spectrogram) assert qspecgram.shape == (4000, 2403) assert qspecgram.q == 5.65685424949238 nptest.assert_almost_equal(qspecgram.value.max(), 58.696743273955391) # test whitening args asd = losc.asd(2, 1) qsg2 = losc.q_transform(method='welch', whiten=asd) utils.assert_quantity_sub_equal(qspecgram, qsg2) asd = losc.asd(.5, .25) qsg2 = losc.q_transform(method='welch', whiten=asd) qsg3 = losc.q_transform(method='welch', fftlength=.5, overlap=.25) utils.assert_quantity_sub_equal(qsg2, qsg3) # make sure frequency too high presents warning with pytest.warns(UserWarning): qspecgram = losc.q_transform(method='welch', frange=(0, 10000)) nptest.assert_almost_equal(qspecgram.yspan[1], 1291.5316316157107)
def test_to_from_lal(self, array): import lal array.epoch = 0 # check that to + from returns the same array lalts = array.to_lal() a2 = type(array).from_lal(lalts) utils.assert_quantity_sub_equal(array, a2, exclude=['name', 'channel']) assert a2.name == array.name # test copy=False a2 = type(array).from_lal(lalts, copy=False) assert shares_memory(a2.value, lalts.data.data) # test units array.override_unit('undef') with pytest.warns(UserWarning): lalts = array.to_lal() assert lalts.sampleUnits == lal.DimensionlessUnit a2 = self.TEST_CLASS.from_lal(lalts) assert a2.unit is units.dimensionless_unscaled
def test_to_from_pycbc(self, array): from pycbc.types import FrequencySeries as PyCBCFrequencySeries array.epoch = 0 # test default conversion pycbcfs = array.to_pycbc() assert isinstance(pycbcfs, PyCBCFrequencySeries) utils.assert_array_equal(array.value, pycbcfs.data) assert array.f0.value == 0 * units.Hz assert array.df.value == pycbcfs.delta_f assert array.epoch.gps == pycbcfs.epoch # go back and check we get back what we put in in the first place a2 = type(array).from_pycbc(pycbcfs) utils.assert_quantity_sub_equal(array, a2, exclude=['name', 'unit', 'channel']) # test copy=False a2 = type(array).from_pycbc(array.to_pycbc(copy=False), copy=False) assert numpy.shares_memory(array.value, a2.value)
def test_crop_frequencies(self): array = self.create(f0=0, df=1) # test simple array2 = array.crop_frequencies() utils.assert_quantity_sub_equal(array, array2) assert numpy.may_share_memory(array.value, array2.value) # test normal array2 = array.crop_frequencies(2, 5) utils.assert_array_equal(array2.value, array.value[:, 2:5]) assert array2.f0 == 2 * units.Hertz assert array2.df == array.df # test copy array2 = array.crop_frequencies(copy=True) assert not numpy.may_share_memory(array.value, array2.value) # test warnings with pytest.warns(UserWarning): array.crop_frequencies(array.yspan[0]-1, array.yspan[1]) with pytest.warns(UserWarning): array.crop_frequencies(array.yspan[0], array.yspan[1]+1)
def test_csd_spectrogram(self, losc): # test defaults sg = losc.csd_spectrogram(losc, 1) assert isinstance(sg, Spectrogram) assert sg.shape == (4, losc.sample_rate.value // 2 + 1) assert sg.f0 == 0 * units.Hz assert sg.df == 1 * units.Hz assert sg.channel is losc.channel assert sg.unit == losc.unit**2 / units.Hertz assert sg.epoch == losc.epoch assert sg.span == losc.span # check the same result as CSD losc1 = losc[:int(losc.sample_rate.value)] csd = losc1.csd(losc1) utils.assert_quantity_sub_equal(sg[0], csd, exclude=['name', 'epoch']) # test fftlength sg = losc.csd_spectrogram(losc, 1, fftlength=0.5) assert sg.shape == (4, 0.5 * losc.sample_rate.value // 2 + 1) assert sg.df == 2 * units.Hertz assert sg.dt == 1 * units.second # test overlap sg = losc.csd_spectrogram(losc, 0.5, fftlength=0.25, overlap=0.125) assert sg.shape == (8, 0.25 * losc.sample_rate.value // 2 + 1) assert sg.df == 4 * units.Hertz assert sg.dt == 0.5 * units.second # test multiprocessing sg2 = losc.csd_spectrogram(losc, 0.5, fftlength=0.25, overlap=0.125, nproc=2) utils.assert_quantity_sub_equal(sg, sg2)
def test_crop(self, instance): """Test :meth:`TimeSeriesBaseDict.crop` """ a = instance.crop(10, 20) for key in a: utils.assert_quantity_sub_equal(a[key], instance[key].crop(10, 20))
def test_pickle(self, array): # check pickle-unpickle yields unchanged data pkl = array.dumps() a2 = pickle.loads(pkl) utils.assert_quantity_sub_equal(array, a2)
def test_copy(self, array): utils.assert_quantity_sub_equal(array, array.copy())
def test_percentile(self): array = self.create(name='Test') a2 = array.percentile(50) utils.assert_quantity_sub_equal(array.median(axis=0), a2, exclude=('name',)) assert a2.name == 'Test: 50th percentile'
def test_spectrogram(self, losc): # test defaults sg = losc.spectrogram(1) # defaults to 50% overlap for 'hann' window assert isinstance(sg, Spectrogram) assert sg.shape == (4, losc.sample_rate.value // 2 + 1) assert sg.f0 == 0 * units.Hz assert sg.df == 1 * units.Hz assert sg.channel is losc.channel assert sg.unit == losc.unit**2 / units.Hz assert sg.epoch == losc.epoch assert sg.span == losc.span # check the same result as PSD psd = losc[:int(losc.sample_rate.value)].psd() # FIXME: epoch should not be excluded here (probably) utils.assert_quantity_sub_equal(sg[0], psd, exclude=['epoch']) # test fftlength sg = losc.spectrogram(1, fftlength=0.5) assert sg.shape == (4, 0.5 * losc.sample_rate.value // 2 + 1) assert sg.df == 2 * units.Hertz assert sg.dt == 1 * units.second # test overlap sg = losc.spectrogram(0.5, fftlength=0.25, overlap=0.125) assert sg.shape == (8, 0.25 * losc.sample_rate.value // 2 + 1) assert sg.df == 4 * units.Hertz assert sg.dt == 0.5 * units.second # test multiprocessing sg2 = losc.spectrogram(0.5, fftlength=0.25, overlap=0.125, nproc=2) utils.assert_quantity_sub_equal(sg, sg2) # test methods sg = losc.spectrogram(0.5, fftlength=0.25, method='welch') assert sg.shape == (8, 0.25 * losc.sample_rate.value // 2 + 1) assert sg.df == 4 * units.Hertz assert sg.dt == 0.5 * units.second sg = losc.spectrogram(0.5, fftlength=0.25, method='bartlett') assert sg.shape == (8, 0.25 * losc.sample_rate.value // 2 + 1) assert sg.df == 4 * units.Hertz assert sg.dt == 0.5 * units.second try: sg = losc.spectrogram(0.5, fftlength=0.25, method='lal-welch') except ImportError: pass else: assert sg.shape == (8, 0.25 * losc.sample_rate.value // 2 + 1) assert sg.df == 4 * units.Hertz assert sg.dt == 0.5 * units.second sg = losc.spectrogram(0.5, fftlength=0.25, method='median-mean') assert sg.shape == (8, 0.25 * losc.sample_rate.value // 2 + 1) assert sg.df == 4 * units.Hertz assert sg.dt == 0.5 * units.second sg = losc.spectrogram(0.5, fftlength=0.25, method='median') assert sg.shape == (8, 0.25 * losc.sample_rate.value // 2 + 1) assert sg.df == 4 * units.Hertz assert sg.dt == 0.5 * units.second # check that `cross` keyword gets deprecated properly # TODO: removed before 1.0 release with pytest.warns(DeprecationWarning) as wng: out = losc.spectrogram(0.5, fftlength=.25, cross=losc) assert '`cross` keyword argument has been deprecated' in \ wng[0].message.args[0] utils.assert_quantity_sub_equal( out, losc.csd_spectrogram(losc, 0.5, fftlength=.25))
def test_asd(self, losc): fs = losc.asd() utils.assert_quantity_sub_equal(fs, losc.psd()**(1 / 2.))
def test_zpk(self, array): zpk = [], [1], 1 utils.assert_quantity_sub_equal( array.zpk(*zpk), array.filter(*zpk, analog=True))
def test_read_pycbc_live(self): import h5py table = self.create( 100, names=['a', 'b', 'c', 'chisq', 'd', 'e', 'f', 'mass1', 'mass2', 'snr']) loudest = (table['snr'] > 500).nonzero()[0] psd = FrequencySeries(random.randn(1000), df=1) fp = os.path.join(tempfile.mkdtemp(), 'X1-Live-0-0.hdf') try: # write table in pycbc_live format (by hand) with h5py.File(fp, 'w') as h5f: group = h5f.create_group('X1') for col in table.columns: group.create_dataset(data=table[col], name=col) group.create_dataset('loudest', data=loudest) group.create_dataset('psd', data=psd.value) group['psd'].attrs['delta_f'] = psd.df.to('Hz').value # check that we can read t2 = self.TABLE.read(fp) utils.assert_table_equal(table, t2) # and check metadata was recorded correctly assert t2.meta['ifo'] == 'X1' # check keyword arguments result in same table t2 = self.TABLE.read(fp, format='hdf5.pycbc_live') utils.assert_table_equal(table, t2) t2 = self.TABLE.read(fp, format='hdf5.pycbc_live', ifo='X1') # assert loudest works t2 = self.TABLE.read(fp, loudest=True) utils.assert_table_equal(table.filter('snr > 500'), t2) # check extended_metadata=True works (default) t2 = self.TABLE.read(fp, extended_metadata=True) utils.assert_table_equal(table, t2) utils.assert_array_equal(t2.meta['loudest'], loudest) utils.assert_quantity_sub_equal( t2.meta['psd'], psd, exclude=['name', 'channel', 'unit', 'epoch']) # check extended_metadata=False works t2 = self.TABLE.read(fp, extended_metadata=False) assert t2.meta == {'ifo': 'X1'} # double-check that loudest and extended_metadata=False work t2 = self.TABLE.read(fp, loudest=True, extended_metadata=False) utils.assert_table_equal(table.filter('snr > 500'), t2) assert t2.meta == {'ifo': 'X1'} # add another IFO, then assert that reading the table without # specifying the IFO fails with h5py.File(fp) as h5f: h5f.create_group('Z1') with pytest.raises(ValueError) as exc: self.TABLE.read(fp) assert str(exc.value).startswith( 'PyCBC live HDF5 file contains dataset groups') # but check that we can still read the original t2 = self.TABLE.read(fp, format='hdf5.pycbc_live', ifo='X1') utils.assert_table_equal(table, t2) # assert processed colums works t2 = self.TABLE.read(fp, ifo='X1', columns=['mchirp', 'new_snr']) mchirp = (table['mass1'] * table['mass2']) ** (3/5.) / ( table['mass1'] + table['mass2']) ** (1/5.) utils.assert_array_equal(t2['mchirp'], mchirp) # test with selection t2 = self.TABLE.read(fp, format='hdf5.pycbc_live', ifo='X1', selection='snr>.5') utils.assert_table_equal(filter_table(table, 'snr>.5'), t2) finally: if os.path.isdir(os.path.dirname(fp)): shutil.rmtree(os.path.dirname(fp))
def test_copy(self, instance): a = instance.copy() assert type(a) is type(instance) for x, y in zip(instance, a): utils.assert_quantity_sub_equal(x, y) assert not numpy.shares_memory(x.value, y.value)
def test_read_write_gwf(self, api): array = self.create(name='TEST') # map API to format name if api is None: fmt = 'gwf' else: fmt = 'gwf.%s' % api # test basic write/read try: utils.test_read_write(array, fmt, extension='gwf', read_args=[array.name], assert_equal=utils.assert_quantity_sub_equal, assert_kw={'exclude': ['channel']}) except ImportError as e: pytest.skip(str(e)) # test read keyword arguments suffix = '-%d-%d.gwf' % (array.t0.value, array.duration.value) with tempfile.NamedTemporaryFile(prefix='GWpy-', suffix=suffix) as f: array.write(f.name) def read_(**kwargs): return type(array).read(f, array.name, format='gwf', **kwargs) # test start, end start, end = array.span.contract(10) t = read_(start=start, end=end) utils.assert_quantity_sub_equal(t, array.crop(start, end), exclude=['channel']) assert t.span == (start, end) t = read_(start=start) utils.assert_quantity_sub_equal(t, array.crop(start=start), exclude=['channel']) t = read_(end=end) utils.assert_quantity_sub_equal(t, array.crop(end=end), exclude=['channel']) # test dtype t = read_(dtype='float32') assert t.dtype is numpy.dtype('float32') t = read_(dtype={f.name: 'float64'}) assert t.dtype is numpy.dtype('float64') # check errors with pytest.raises((ValueError, RuntimeError)): read_(start=array.span[1]) with pytest.raises((ValueError, RuntimeError)): read_(end=array.span[0] - 1) # check old format prints a deprecation warning if api: with pytest.warns(DeprecationWarning): type(array).read(f, array.name, format=api) # check reading from cache try: from glue.lal import Cache except ImportError: pass else: a2 = self.create(name='TEST', t0=array.span[1], dt=array.dx) suffix = '-%d-%d.gwf' % (a2.t0.value, a2.duration.value) with tempfile.NamedTemporaryFile(prefix='GWpy-', suffix=suffix) as f2: a2.write(f2.name) cache = Cache.from_urls([f.name, f2.name], coltype=int) comb = type(array).read(cache, 'TEST', format=fmt, nproc=2) utils.assert_quantity_sub_equal(comb, array.append( a2, inplace=False), exclude=['channel'])