tests.py 6.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
import unittest
import pyfstat
import numpy as np
import os


class TestWriter(unittest.TestCase):

    def test_make_cff(self):
        label = "Test"
        Writer = pyfstat.Writer(label, outdir='TestData')
        Writer.make_cff()
        self.assertTrue(os.path.isfile('./TestData/Test.cff'))

    def test_run_makefakedata(self):
        label = "Test"
        Writer = pyfstat.Writer(label, outdir='TestData')
        Writer.make_cff()
        Writer.run_makefakedata()
        self.assertTrue(os.path.isfile(
            './TestData/H-4800_H1_1800SFT_Test-700000000-8640000.sft'))

    def test_makefakedata_usecached(self):
        label = "Test"
        Writer = pyfstat.Writer(label, outdir='TestData')
        if os.path.isfile(Writer.sft_filepath):
            os.remove(Writer.sft_filepath)
        Writer.run_makefakedata()
        time_first = os.path.getmtime(Writer.sft_filepath)
        Writer.run_makefakedata()
        time_second = os.path.getmtime(Writer.sft_filepath)
        self.assertTrue(time_first == time_second)
        os.system('touch {}'.format(Writer.config_file_name))
        Writer.run_makefakedata()
        time_third = os.path.getmtime(Writer.sft_filepath)
        self.assertFalse(time_first == time_third)


class TestBaseSearchClass(unittest.TestCase):
    def test_shift_matrix(self):
        BSC = pyfstat.BaseSearchClass()
        dT = 10
        a = BSC.shift_matrix(4, dT)
        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(
                thetaB, BSC.shift_coefficients(thetaA, dT)))

    def test_shift_coefficients_loop(self):
        BSC = pyfstat.BaseSearchClass()
        thetaA = np.array([10., 1e2, 10., 1e2])
        dT = 1e1
        thetaB = BSC.shift_coefficients(thetaA, dT)
        self.assertTrue(
            np.allclose(
                thetaA, BSC.shift_coefficients(thetaB, -dT),
                rtol=1e-9, atol=1e-9))


Gregory Ashton's avatar
Gregory Ashton committed
78
class TestComputeFstat(unittest.TestCase):
79
80
81
82
83
84
85
86
    label = "Test"
    outdir = 'TestData'

    def test_run_computefstatistic_single_point(self):
        Writer = pyfstat.Writer(self.label, outdir=self.outdir)
        Writer.make_data()
        predicted_FS = Writer.predict_fstat()

Gregory Ashton's avatar
Gregory Ashton committed
87
88
        search = pyfstat.ComputeFstat(tref=Writer.tref, sftlabel=Writer.label,
                                      sftdir=Writer.outdir)
89
        FS = search.run_computefstatistic_single_point(Writer.tstart,
Gregory Ashton's avatar
Gregory Ashton committed
90
                                                       Writer.tend,
91
92
93
94
95
96
97
98
                                                       Writer.F0,
                                                       Writer.F1,
                                                       Writer.F2,
                                                       Writer.Alpha,
                                                       Writer.Delta)
        print predicted_FS, FS
        self.assertTrue(np.abs(predicted_FS-FS)/FS < 0.1)

Gregory Ashton's avatar
Gregory Ashton committed
99
100
101
102
103

class TestSemiCoherentGlitchSearch(unittest.TestCase):
    label = "Test"
    outdir = 'TestData'

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    def test_compute_nglitch_fstat(self):
        duration = 100*86400
        dtglitch = 100*43200
        delta_F0 = 0
        Writer = pyfstat.Writer(self.label, outdir=self.outdir,
                                duration=duration, dtglitch=dtglitch,
                                delta_F0=delta_F0)

        Writer.make_data()

        search = pyfstat.SemiCoherentGlitchSearch(
            label=Writer.label, outdir=Writer.outdir, tref=Writer.tref,
            tstart=Writer.tstart, tend=Writer.tend, nglitch=1)

        FS = search.compute_nglitch_fstat(Writer.F0, Writer.F1, Writer.F2,
                                          Writer.Alpha, Writer.Delta,
                                          Writer.delta_F0, Writer.delta_F1,
                                          search.tstart+dtglitch)

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

        Writer.tend = tstart + dtglitch
        FSA = Writer.predict_fstat()

        Writer.tstart = tstart + dtglitch
        Writer.tend = tend
        FSB = Writer.predict_fstat()

        predicted_FS = (FSA + FSB)

        print(predicted_FS, FS)
Gregory Ashton's avatar
Gregory Ashton committed
137
        self.assertTrue(np.abs((FS - predicted_FS))/predicted_FS < 0.3)
138
139


Gregory Ashton's avatar
Gregory Ashton committed
140
class TestMCMCSearch(unittest.TestCase):
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    label = "MCMCTest"
    outdir = 'TestData'

    def test_fully_coherent(self):
        h0 = 1e-24
        sqrtSX = 1e-22
        F0 = 30
        F1 = -1e-10
        F2 = 0
        tstart = 700000000
        duration = 100 * 86400
        tend = tstart + duration
        Alpha = 5e-3
        Delta = 1.2
        tref = tstart
        dtglitch = duration
        delta_F0 = 0
        Writer = pyfstat.Writer(F0=F0, F1=F1, F2=F2, label=self.label,
                                h0=h0, sqrtSX=sqrtSX,
                                outdir=self.outdir, tstart=tstart,
                                Alpha=Alpha, Delta=Delta, tref=tref,
                                duration=duration, dtglitch=dtglitch,
                                delta_F0=delta_F0, Band=4)

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

Gregory Ashton's avatar
Gregory Ashton committed
168
        theta = {'F0': {'type': 'norm', 'loc': F0, 'scale': np.abs(1e-9*F0)},
169
170
171
                 'F1': {'type': 'norm', 'loc': F1, 'scale': np.abs(1e-9*F1)},
                 'F2': F2, 'Alpha': Alpha, 'Delta': Delta}

Gregory Ashton's avatar
Gregory Ashton committed
172
173
        search = pyfstat.MCMCSearch(
            label=self.label, outdir=self.outdir, theta_prior=theta, tref=tref,
174
175
176
177
178
179
180
181
182
            sftlabel=self.label, sftdir=self.outdir,
            tstart=tstart, tend=tend, nsteps=[100, 100], nwalkers=100,
            ntemps=1)
        search.run()
        search.plot_corner(add_prior=True)
        _, FS = search.get_max_twoF()

        print('Predicted twoF is {} while recovered is {}'.format(
                predicted_FS, FS))
Gregory Ashton's avatar
Gregory Ashton committed
183
184
        self.assertTrue(
            FS > predicted_FS or np.abs((FS-predicted_FS))/predicted_FS < 0.3)
185
186
187
188


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