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

7

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

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

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


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

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

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

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


Gregory Ashton's avatar
Gregory Ashton committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class TestBunch(Test):
    def test_bunch(self):
        b = pyfstat.core.Bunch(dict(x=10))
        self.assertTrue(b.x == 10)


class TestPar(Test):
    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)
        os.system('rm -r TestData')


Gregory Ashton's avatar
Gregory Ashton committed
79
class TestBaseSearchClass(Test):
80
81
82
    def test_shift_matrix(self):
        BSC = pyfstat.BaseSearchClass()
        dT = 10
83
        a = BSC._shift_matrix(4, dT)
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        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(
105
                thetaB, BSC._shift_coefficients(thetaA, dT)))
106
107
108
109
110

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


Gregory Ashton's avatar
Gregory Ashton committed
118
class TestComputeFstat(Test):
Gregory Ashton's avatar
Gregory Ashton committed
119
    label = "TestComputeFstat"
120
121

    def test_run_computefstatistic_single_point(self):
Gregory Ashton's avatar
Gregory Ashton committed
122
        Writer = pyfstat.Writer(self.label, outdir=self.outdir, duration=86400,
Gregory Ashton's avatar
Gregory Ashton committed
123
                                h0=1, sqrtSX=1)
124
125
126
        Writer.make_data()
        predicted_FS = Writer.predict_fstat()

127
128
        search = pyfstat.ComputeFstat(
            tref=Writer.tref,
129
            sftfilepattern='{}/*{}*sft'.format(Writer.outdir, Writer.label))
130
131
132
        FS = search.get_fullycoherent_twoF(
            Writer.tstart, Writer.tend, Writer.F0, Writer.F1, Writer.F2,
            Writer.Alpha, Writer.Delta)
133
        print predicted_FS, FS
Gregory Ashton's avatar
Gregory Ashton committed
134
        self.assertTrue(np.abs(predicted_FS-FS)/FS < 0.2)
135

136
    def run_computefstatistic_single_point_no_noise(self):
Gregory Ashton's avatar
Gregory Ashton committed
137
        Writer = pyfstat.Writer(self.label, outdir=self.outdir, add_noise=False,
Gregory Ashton's avatar
Gregory Ashton committed
138
                                duration=86400, h0=1, sqrtSX=1)
139
140
141
142
143
        Writer.make_data()
        predicted_FS = Writer.predict_fstat()

        search = pyfstat.ComputeFstat(
            tref=Writer.tref, assumeSqrtSX=1,
144
            sftfilepattern='{}/*{}*sft'.format(Writer.outdir, Writer.label))
145
146
147
        FS = search.get_fullycoherent_twoF(
            Writer.tstart, Writer.tend, Writer.F0, Writer.F1, Writer.F2,
            Writer.Alpha, Writer.Delta)
148
149
        print predicted_FS, FS
        self.assertTrue(np.abs(predicted_FS-FS)/FS < 0.2)
Gregory Ashton's avatar
Gregory Ashton committed
150

151
    def test_injectSources_from_file(self):
Gregory Ashton's avatar
Gregory Ashton committed
152
        Writer = pyfstat.Writer(self.label, outdir=self.outdir, add_noise=False,
Gregory Ashton's avatar
Gregory Ashton committed
153
                                duration=86400, h0=1, sqrtSX=1)
154
155
156
157
158
159
160
161
        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)
162
163
164
        FS = search.get_fullycoherent_twoF(
            Writer.tstart, Writer.tend, Writer.F0, Writer.F1, Writer.F2,
            Writer.Alpha, Writer.Delta)
165
166
167
168
169
170
        Writer.make_data()
        predicted_FS = Writer.predict_fstat()
        print predicted_FS, FS
        self.assertTrue(np.abs(predicted_FS-FS)/FS < 0.2)


Gregory Ashton's avatar
Gregory Ashton committed
171
class TestSemiCoherentGlitchSearch(Test):
Gregory Ashton's avatar
Gregory Ashton committed
172
    label = "TestSemiCoherentGlitchSearch"
Gregory Ashton's avatar
Gregory Ashton committed
173

174
    def test_get_semicoherent_nglitch_twoF(self):
Gregory Ashton's avatar
Gregory Ashton committed
175
176
        duration = 10*86400
        dtglitch = .5*duration
177
        delta_F0 = 0
Gregory Ashton's avatar
Gregory Ashton committed
178
179
        h0 = 1
        sqrtSX = 1
180
        Writer = pyfstat.GlitchWriter(
Gregory Ashton's avatar
Gregory Ashton committed
181
            self.label, outdir=self.outdir, duration=duration, dtglitch=dtglitch,
Gregory Ashton's avatar
Gregory Ashton committed
182
            delta_F0=delta_F0, sqrtSX=sqrtSX, h0=h0)
183
184
185
186

        Writer.make_data()

        search = pyfstat.SemiCoherentGlitchSearch(
Gregory Ashton's avatar
Gregory Ashton committed
187
            label=self.label, outdir=self.outdir,
188
            sftfilepattern='{}/*{}*sft'.format(Writer.outdir, Writer.label),
189
190
            tref=Writer.tref, minStartTime=Writer.tstart,
            maxStartTime=Writer.tend, nglitch=1)
191

192
193
194
        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)
195
196

        # Compute the predicted semi-coherent glitch Fstat
197
198
        minStartTime = Writer.tstart
        maxStartTime = Writer.tend
199

200
        Writer.maxStartTime = minStartTime + dtglitch
201
202
        FSA = Writer.predict_fstat()

203
204
        Writer.tstart = minStartTime + dtglitch
        Writer.tend = maxStartTime
205
206
        FSB = Writer.predict_fstat()

207
        print FSA, FSB
208
209
210
        predicted_FS = (FSA + FSB)

        print(predicted_FS, FS)
Gregory Ashton's avatar
Gregory Ashton committed
211
        self.assertTrue(np.abs((FS - predicted_FS))/predicted_FS < 0.3)
212
213


Gregory Ashton's avatar
Gregory Ashton committed
214
class TestMCMCSearch(Test):
Gregory Ashton's avatar
Gregory Ashton committed
215
    label = "TestMCMCSearch"
216
217

    def test_fully_coherent(self):
Gregory Ashton's avatar
Gregory Ashton committed
218
219
        h0 = 1
        sqrtSX = 1
220
221
222
        F0 = 30
        F1 = -1e-10
        F2 = 0
223
        minStartTime = 700000000
Gregory Ashton's avatar
Gregory Ashton committed
224
        duration = 1 * 86400
225
        maxStartTime = minStartTime + duration
226
227
        Alpha = 5e-3
        Delta = 1.2
228
        tref = minStartTime
229
230
        Writer = pyfstat.Writer(F0=F0, F1=F1, F2=F2, label=self.label,
                                h0=h0, sqrtSX=sqrtSX,
Gregory Ashton's avatar
Gregory Ashton committed
231
                                outdir=self.outdir, tstart=minStartTime,
232
                                Alpha=Alpha, Delta=Delta, tref=tref,
233
                                duration=duration,
234
                                Band=4)
235
236
237
238

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

Gregory Ashton's avatar
Gregory Ashton committed
239
240
        theta = {'F0': {'type': 'norm', 'loc': F0, 'scale': np.abs(1e-10*F0)},
                 'F1': {'type': 'norm', 'loc': F1, 'scale': np.abs(1e-10*F1)},
241
242
                 'F2': F2, 'Alpha': Alpha, 'Delta': Delta}

Gregory Ashton's avatar
Gregory Ashton committed
243
        search = pyfstat.MCMCSearch(
Gregory Ashton's avatar
Gregory Ashton committed
244
            label=self.label, outdir=self.outdir, theta_prior=theta, tref=tref,
245
            sftfilepattern='{}/*{}*sft'.format(Writer.outdir, Writer.label),
246
            minStartTime=minStartTime, maxStartTime=maxStartTime,
247
            nsteps=[100, 100], nwalkers=100, ntemps=2, log10beta_min=-1)
Gregory Ashton's avatar
Gregory Ashton committed
248
        search.setup_burnin_convergence_testing()
249
        search.run(create_plots=False)
250
251
252
253
        _, FS = search.get_max_twoF()

        print('Predicted twoF is {} while recovered is {}'.format(
                predicted_FS, FS))
Gregory Ashton's avatar
Gregory Ashton committed
254
255
        self.assertTrue(
            FS > predicted_FS or np.abs((FS-predicted_FS))/predicted_FS < 0.3)
256
257
258
259


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