tests.py 12.1 KB
Newer Older
1
2
3
import unittest
import numpy as np
import os
Gregory Ashton's avatar
Gregory Ashton committed
4
5
import shutil
import pyfstat
6
import lalpulsar
Gregory Ashton's avatar
Gregory Ashton committed
7

8

Gregory Ashton's avatar
Gregory Ashton committed
9
class Test(unittest.TestCase):
Gregory Ashton's avatar
Gregory Ashton committed
10
11
    outdir = 'TestData'

Gregory Ashton's avatar
Gregory Ashton committed
12
    @classmethod
Gregory Ashton's avatar
Gregory Ashton committed
13
14
15
    def setUpClass(self):
        if os.path.isdir(self.outdir):
            shutil.rmtree(self.outdir)
16

Gregory Ashton's avatar
Gregory Ashton committed
17
    @classmethod
Gregory Ashton's avatar
Gregory Ashton committed
18
19
20
    def tearDownClass(self):
        if os.path.isdir(self.outdir):
            shutil.rmtree(self.outdir)
Gregory Ashton's avatar
Gregory Ashton committed
21
22
23


class TestWriter(Test):
Gregory Ashton's avatar
Gregory Ashton committed
24
    label = "TestWriter"
25
26

    def test_make_cff(self):
Gregory Ashton's avatar
Gregory Ashton committed
27
        Writer = pyfstat.Writer(self.label, outdir=self.outdir)
28
        Writer.make_cff()
29
30
        self.assertTrue(os.path.isfile(
            './{}/{}.cff'.format(self.outdir, self.label)))
31
32

    def test_run_makefakedata(self):
Gregory Ashton's avatar
Gregory Ashton committed
33
        Writer = pyfstat.Writer(self.label, outdir=self.outdir, duration=86400)
34
35
36
        Writer.make_cff()
        Writer.run_makefakedata()
        self.assertTrue(os.path.isfile(
37
38
            './{}/H-48_H1_1800SFT_TestWriter-700000000-86400.sft'
            .format(self.outdir)))
39
40

    def test_makefakedata_usecached(self):
Gregory Ashton's avatar
Gregory Ashton committed
41
        Writer = pyfstat.Writer(self.label, outdir=self.outdir, duration=86400)
42
43
        if os.path.isfile(Writer.sftfilepath):
            os.remove(Writer.sftfilepath)
Gregory Ashton's avatar
Gregory Ashton committed
44
        Writer.make_cff()
45
        Writer.run_makefakedata()
46
        time_first = os.path.getmtime(Writer.sftfilepath)
47
        Writer.run_makefakedata()
48
        time_second = os.path.getmtime(Writer.sftfilepath)
49
50
51
        self.assertTrue(time_first == time_second)
        os.system('touch {}'.format(Writer.config_file_name))
        Writer.run_makefakedata()
52
        time_third = os.path.getmtime(Writer.sftfilepath)
53
54
55
        self.assertFalse(time_first == time_third)


Gregory Ashton's avatar
Gregory Ashton committed
56
57
58
59
60
61
class TestBunch(Test):
    def test_bunch(self):
        b = pyfstat.core.Bunch(dict(x=10))
        self.assertTrue(b.x == 10)


62
class Test_par(Test):
Gregory Ashton's avatar
Gregory Ashton committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    label = 'TestPar'

    def test(self):
        os.system('mkdir {}'.format(self.outdir))
        os.system(
            'echo "x=100\ny=10" > {}/{}.par'.format(self.outdir, self.label))

        par = pyfstat.core.read_par(
            '{}/{}.par'.format(self.outdir, self.label), return_type='Bunch')
        self.assertTrue(par.x == 100)
        self.assertTrue(par.y == 10)

        par = pyfstat.core.read_par(outdir=self.outdir, label=self.label,
                                    return_type='dict')
        self.assertTrue(par['x'] == 100)
        self.assertTrue(par['y'] == 10)
79
        os.system('rm -r {}'.format(self.outdir))
Gregory Ashton's avatar
Gregory Ashton committed
80
81


Gregory Ashton's avatar
Gregory Ashton committed
82
class TestBaseSearchClass(Test):
83
84
85
    def test_shift_matrix(self):
        BSC = pyfstat.BaseSearchClass()
        dT = 10
86
        a = BSC._shift_matrix(4, dT)
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
        b = np.array([[1, 2*np.pi*dT, 2*np.pi*dT**2/2.0, 2*np.pi*dT**3/6.0],
                      [0, 1, dT, dT**2/2.0],
                      [0, 0, 1, dT],
                      [0, 0, 0, 1]])
        self.assertTrue(np.array_equal(a, b))

    def test_shift_coefficients(self):
        BSC = pyfstat.BaseSearchClass()
        thetaA = np.array([10., 1e2, 10., 1e2])
        dT = 100

        # Calculate the 'long' way
        thetaB = np.zeros(len(thetaA))
        thetaB[3] = thetaA[3]
        thetaB[2] = thetaA[2] + thetaA[3]*dT
        thetaB[1] = thetaA[1] + thetaA[2]*dT + .5*thetaA[3]*dT**2
        thetaB[0] = thetaA[0] + 2*np.pi*(thetaA[1]*dT + .5*thetaA[2]*dT**2
                                         + thetaA[3]*dT**3 / 6.0)

        self.assertTrue(
            np.array_equal(
108
                thetaB, BSC._shift_coefficients(thetaA, dT)))
109
110
111
112
113

    def test_shift_coefficients_loop(self):
        BSC = pyfstat.BaseSearchClass()
        thetaA = np.array([10., 1e2, 10., 1e2])
        dT = 1e1
114
        thetaB = BSC._shift_coefficients(thetaA, dT)
115
116
        self.assertTrue(
            np.allclose(
117
                thetaA, BSC._shift_coefficients(thetaB, -dT),
118
119
120
                rtol=1e-9, atol=1e-9))


Gregory Ashton's avatar
Gregory Ashton committed
121
class TestComputeFstat(Test):
Gregory Ashton's avatar
Gregory Ashton committed
122
    label = "TestComputeFstat"
123
124

    def test_run_computefstatistic_single_point(self):
Gregory Ashton's avatar
Gregory Ashton committed
125
        Writer = pyfstat.Writer(self.label, outdir=self.outdir, duration=86400,
126
                                h0=1, sqrtSX=1, detectors='H1,L1')
127
128
129
        Writer.make_data()
        predicted_FS = Writer.predict_fstat()

130
131
132
133
134
135
136
137
138
139
140
        search_H1 = pyfstat.ComputeFstat(
            tref=Writer.tref, detectors='H1',
            sftfilepattern='{}/*{}*sft'.format(Writer.outdir, Writer.label),
            SSBprec=lalpulsar.SSBPREC_RELATIVISTIC)
        FS = search_H1.get_fullycoherent_twoF(
            Writer.tstart, Writer.tend, Writer.F0, Writer.F1, Writer.F2,
            Writer.Alpha, Writer.Delta)
        self.assertTrue(np.abs(predicted_FS-FS)/FS < 0.2)

        search_H1L1 = pyfstat.ComputeFstat(
            tref=Writer.tref, detectors='H1,L1',
141
            sftfilepattern='{}/*{}*sft'.format(Writer.outdir, Writer.label))
142
        FS = search_H1L1.get_fullycoherent_twoF(
143
144
            Writer.tstart, Writer.tend, Writer.F0, Writer.F1, Writer.F2,
            Writer.Alpha, Writer.Delta)
Gregory Ashton's avatar
Gregory Ashton committed
145
        self.assertTrue(np.abs(predicted_FS-FS)/FS < 0.2)
146

147
    def run_computefstatistic_single_point_no_noise(self):
Gregory Ashton's avatar
Gregory Ashton committed
148
        Writer = pyfstat.Writer(self.label, outdir=self.outdir, add_noise=False,
Gregory Ashton's avatar
Gregory Ashton committed
149
                                duration=86400, h0=1, sqrtSX=1)
150
151
152
153
154
        Writer.make_data()
        predicted_FS = Writer.predict_fstat()

        search = pyfstat.ComputeFstat(
            tref=Writer.tref, assumeSqrtSX=1,
155
            sftfilepattern='{}/*{}*sft'.format(Writer.outdir, Writer.label))
156
157
158
        FS = search.get_fullycoherent_twoF(
            Writer.tstart, Writer.tend, Writer.F0, Writer.F1, Writer.F2,
            Writer.Alpha, Writer.Delta)
159
160
        print predicted_FS, FS
        self.assertTrue(np.abs(predicted_FS-FS)/FS < 0.2)
Gregory Ashton's avatar
Gregory Ashton committed
161

162
    def test_injectSources(self):
Gregory Ashton's avatar
Gregory Ashton committed
163
        Writer = pyfstat.Writer(self.label, outdir=self.outdir, add_noise=False,
Gregory Ashton's avatar
Gregory Ashton committed
164
                                duration=86400, h0=1, sqrtSX=1)
165
166
167
168
169
170
171
172
        Writer.make_cff()
        injectSources = Writer.config_file_name

        search = pyfstat.ComputeFstat(
            tref=Writer.tref, assumeSqrtSX=1, injectSources=injectSources,
            minCoverFreq=28, maxCoverFreq=32, minStartTime=Writer.tstart,
            maxStartTime=Writer.tstart+Writer.duration,
            detectors=Writer.detectors)
173
        FS_from_file = search.get_fullycoherent_twoF(
174
175
            Writer.tstart, Writer.tend, Writer.F0, Writer.F1, Writer.F2,
            Writer.Alpha, Writer.Delta)
176
177
        Writer.make_data()
        predicted_FS = Writer.predict_fstat()
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
        self.assertTrue(np.abs(predicted_FS-FS_from_file)/FS_from_file < 0.2)

        injectSourcesdict = pyfstat.core.read_par(Writer.config_file_name)
        injectSourcesdict['F0'] = injectSourcesdict['Freq']
        injectSourcesdict['F1'] = injectSourcesdict['f1dot']
        injectSourcesdict['F2'] = injectSourcesdict['f2dot']
        search = pyfstat.ComputeFstat(
            tref=Writer.tref, assumeSqrtSX=1, injectSources=injectSourcesdict,
            minCoverFreq=28, maxCoverFreq=32, minStartTime=Writer.tstart,
            maxStartTime=Writer.tstart+Writer.duration,
            detectors=Writer.detectors)
        FS_from_dict = search.get_fullycoherent_twoF(
            Writer.tstart, Writer.tend, Writer.F0, Writer.F1, Writer.F2,
            Writer.Alpha, Writer.Delta)
        self.assertTrue(FS_from_dict == FS_from_file)


class TestSemiCoherentSearch(Test):
    label = "TestSemiCoherentSearch"

    def test_get_semicoherent_twoF(self):
        duration = 10*86400
        Writer = pyfstat.Writer(
            self.label, outdir=self.outdir, duration=duration, h0=1, sqrtSX=1)
        Writer.make_data()

        search = pyfstat.SemiCoherentSearch(
            label=self.label, outdir=self.outdir, nsegs=2,
            sftfilepattern='{}/*{}*sft'.format(Writer.outdir, Writer.label),
            tref=Writer.tref, minStartTime=Writer.tstart,
            maxStartTime=Writer.tend)

        search.get_semicoherent_twoF(
            Writer.F0, Writer.F1, Writer.F2, Writer.Alpha, Writer.Delta,
            record_segments=True)

        # Compute the predicted semi-coherent Fstat
        minStartTime = Writer.tstart
        maxStartTime = Writer.tend

        Writer.maxStartTime = minStartTime + duration / 2.0
        FSA = Writer.predict_fstat()

        Writer.tstart = minStartTime + duration / 2.0
        Writer.tend = maxStartTime
        FSB = Writer.predict_fstat()

        FSs = np.array([FSA, FSB])
        diffs = (np.array(search.detStat_per_segment) - FSs) / FSs
        self.assertTrue(np.all(diffs < 0.3))

    def test_get_semicoherent_BSGL(self):
        duration = 10*86400
        Writer = pyfstat.Writer(
            self.label, outdir=self.outdir, duration=duration,
            detectors='H1,L1')
        Writer.make_data()

        search = pyfstat.SemiCoherentSearch(
            label=self.label, outdir=self.outdir, nsegs=2,
            sftfilepattern='{}/*{}*sft'.format(Writer.outdir, Writer.label),
            tref=Writer.tref, minStartTime=Writer.tstart,
            maxStartTime=Writer.tend, BSGL=True)

        BSGL = search.get_semicoherent_twoF(
            Writer.F0, Writer.F1, Writer.F2, Writer.Alpha, Writer.Delta,
            record_segments=True)
        self.assertTrue(BSGL > 0)
246
247


Gregory Ashton's avatar
Gregory Ashton committed
248
class TestSemiCoherentGlitchSearch(Test):
Gregory Ashton's avatar
Gregory Ashton committed
249
    label = "TestSemiCoherentGlitchSearch"
Gregory Ashton's avatar
Gregory Ashton committed
250

251
    def test_get_semicoherent_nglitch_twoF(self):
Gregory Ashton's avatar
Gregory Ashton committed
252
253
        duration = 10*86400
        dtglitch = .5*duration
254
        delta_F0 = 0
Gregory Ashton's avatar
Gregory Ashton committed
255
256
        h0 = 1
        sqrtSX = 1
257
        Writer = pyfstat.GlitchWriter(
Gregory Ashton's avatar
Gregory Ashton committed
258
            self.label, outdir=self.outdir, duration=duration, dtglitch=dtglitch,
Gregory Ashton's avatar
Gregory Ashton committed
259
            delta_F0=delta_F0, sqrtSX=sqrtSX, h0=h0)
260
261
262
263

        Writer.make_data()

        search = pyfstat.SemiCoherentGlitchSearch(
Gregory Ashton's avatar
Gregory Ashton committed
264
            label=self.label, outdir=self.outdir,
265
            sftfilepattern='{}/*{}*sft'.format(Writer.outdir, Writer.label),
266
267
            tref=Writer.tref, minStartTime=Writer.tstart,
            maxStartTime=Writer.tend, nglitch=1)
268

269
270
271
        FS = search.get_semicoherent_nglitch_twoF(
            Writer.F0, Writer.F1, Writer.F2, Writer.Alpha, Writer.Delta,
            Writer.delta_F0, Writer.delta_F1, search.minStartTime+dtglitch)
272
273

        # Compute the predicted semi-coherent glitch Fstat
274
275
        minStartTime = Writer.tstart
        maxStartTime = Writer.tend
276

277
        Writer.maxStartTime = minStartTime + dtglitch
278
279
        FSA = Writer.predict_fstat()

280
281
        Writer.tstart = minStartTime + dtglitch
        Writer.tend = maxStartTime
282
283
        FSB = Writer.predict_fstat()

284
        print FSA, FSB
285
286
287
        predicted_FS = (FSA + FSB)

        print(predicted_FS, FS)
Gregory Ashton's avatar
Gregory Ashton committed
288
        self.assertTrue(np.abs((FS - predicted_FS))/predicted_FS < 0.3)
289
290


Gregory Ashton's avatar
Gregory Ashton committed
291
class TestMCMCSearch(Test):
Gregory Ashton's avatar
Gregory Ashton committed
292
    label = "TestMCMCSearch"
293
294

    def test_fully_coherent(self):
Gregory Ashton's avatar
Gregory Ashton committed
295
296
        h0 = 1
        sqrtSX = 1
297
298
299
        F0 = 30
        F1 = -1e-10
        F2 = 0
300
        minStartTime = 700000000
Gregory Ashton's avatar
Gregory Ashton committed
301
        duration = 1 * 86400
302
        maxStartTime = minStartTime + duration
303
304
        Alpha = 5e-3
        Delta = 1.2
305
        tref = minStartTime
306
307
        Writer = pyfstat.Writer(F0=F0, F1=F1, F2=F2, label=self.label,
                                h0=h0, sqrtSX=sqrtSX,
Gregory Ashton's avatar
Gregory Ashton committed
308
                                outdir=self.outdir, tstart=minStartTime,
309
                                Alpha=Alpha, Delta=Delta, tref=tref,
310
                                duration=duration,
311
                                Band=4)
312
313
314
315

        Writer.make_data()
        predicted_FS = Writer.predict_fstat()

Gregory Ashton's avatar
Gregory Ashton committed
316
317
        theta = {'F0': {'type': 'norm', 'loc': F0, 'scale': np.abs(1e-10*F0)},
                 'F1': {'type': 'norm', 'loc': F1, 'scale': np.abs(1e-10*F1)},
318
319
                 'F2': F2, 'Alpha': Alpha, 'Delta': Delta}

Gregory Ashton's avatar
Gregory Ashton committed
320
        search = pyfstat.MCMCSearch(
Gregory Ashton's avatar
Gregory Ashton committed
321
            label=self.label, outdir=self.outdir, theta_prior=theta, tref=tref,
322
            sftfilepattern='{}/*{}*sft'.format(Writer.outdir, Writer.label),
323
            minStartTime=minStartTime, maxStartTime=maxStartTime,
324
            nsteps=[100, 100], nwalkers=100, ntemps=2, log10beta_min=-1)
Gregory Ashton's avatar
Gregory Ashton committed
325
        search.setup_burnin_convergence_testing()
326
        search.run(create_plots=False)
327
328
329
330
        _, FS = search.get_max_twoF()

        print('Predicted twoF is {} while recovered is {}'.format(
                predicted_FS, FS))
Gregory Ashton's avatar
Gregory Ashton committed
331
332
        self.assertTrue(
            FS > predicted_FS or np.abs((FS-predicted_FS))/predicted_FS < 0.3)
333
334
335
336


if __name__ == '__main__':
    unittest.main()