Coverage for pesummary/tests/strain_test.py: 100.0%

43 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-02 08:42 +0000

1# Licensed under an MIT style license -- see LICENSE.md 

2 

3from pesummary.gw.file.strain import StrainData 

4from gwpy.timeseries import TimeSeries 

5import numpy as np 

6import matplotlib.figure 

7 

8__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"] 

9 

10 

11class TestStrainData(object): 

12 """Class to test the pesummary.gw.file.strain.StrainData class 

13 """ 

14 def test_fetch_open_frame(self): 

15 """test that the StrainData.fetch_open_frame works as expected 

16 """ 

17 import requests 

18 pesummary_data = StrainData.fetch_open_frame( 

19 "GW190412", IFO="L1", duration=32, sampling_rate=4096., 

20 channel="L1:GWOSC-4KHZ_R1_STRAIN", format="hdf5" 

21 ) 

22 N = len(pesummary_data) 

23 np.testing.assert_almost_equal(N * pesummary_data.dt.value, 32.) 

24 np.testing.assert_almost_equal(1. / pesummary_data.dt.value, 4096.) 

25 assert pesummary_data.IFO == "L1" 

26 _data = requests.get( 

27 "https://www.gw-openscience.org/eventapi/html/GWTC-2/GW190412/v3/" 

28 "L-L1_GWOSC_4KHZ_R1-1239082247-32.gwf" 

29 ) 

30 with open("L-L1_GWOSC_4KHZ_R1-1239082247-32.gwf", "wb") as f: 

31 f.write(_data.content) 

32 data2 = TimeSeries.read( 

33 "L-L1_GWOSC_4KHZ_R1-1239082247-32.gwf", 

34 channel="L1:GWOSC-4KHZ_R1_STRAIN" 

35 ) 

36 np.testing.assert_almost_equal(pesummary_data.value, data2.value) 

37 np.testing.assert_almost_equal( 

38 pesummary_data.times.value, data2.times.value 

39 ) 

40 

41 def test_fetch_open_data(self): 

42 """Test that the gwpy methods are still the same 

43 """ 

44 args = ["L1", 1126259446, 1126259478] 

45 pesummary_data = StrainData.fetch_open_data(*args) 

46 gwpy_data = TimeSeries.fetch_open_data(*args) 

47 np.testing.assert_almost_equal(pesummary_data.value, gwpy_data.value) 

48 np.testing.assert_almost_equal( 

49 pesummary_data.times.value, gwpy_data.times.value 

50 ) 

51 assert isinstance(pesummary_data.gwpy, TimeSeries) 

52 np.testing.assert_almost_equal( 

53 pesummary_data.gwpy.value, gwpy_data.value 

54 ) 

55 np.testing.assert_almost_equal( 

56 pesummary_data.gwpy.times.value, gwpy_data.times.value 

57 ) 

58 assert pesummary_data.IFO == "L1" 

59 assert list(pesummary_data.strain_dict.keys()) == ["L1"] 

60 np.testing.assert_almost_equal( 

61 pesummary_data.strain_dict["L1"].value, gwpy_data.value 

62 ) 

63 np.testing.assert_almost_equal( 

64 pesummary_data.strain_dict["L1"].times.value, gwpy_data.times.value 

65 ) 

66 

67 def test_plots(self): 

68 """Test that the plotting methods work as expected 

69 """ 

70 args = ["L1", 1126259446, 1126259478] 

71 pesummary_data = StrainData.fetch_open_data(*args) 

72 fig = pesummary_data.plot(type="td") 

73 assert isinstance(fig, matplotlib.figure.Figure) 

74 fig = pesummary_data.plot(type="fd") 

75 assert isinstance(fig, matplotlib.figure.Figure) 

76 fig = pesummary_data.plot(1126259446 + 20., type="omegascan") 

77 assert isinstance(fig, matplotlib.figure.Figure) 

78 fig = pesummary_data.plot(type="spectrogram") 

79 assert isinstance(fig, matplotlib.figure.Figure)