Skip to content

Commit

Permalink
update unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
tuoyl committed Jul 20, 2023
1 parent fcd5487 commit b55e062
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 4 deletions.
28 changes: 28 additions & 0 deletions tests/data/Vela.par
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
PSRJ J0534+2200
RAJ 05:34:31.973 8.1267817736559476629e-4895
DECJ +22:00:52.06 3.1242079228685550361e-4884
F0 11.183908501919459511 0 0
F1 -1.5607051596431583974e-11 0 0
F2 1.2164543201957172106e-21 0 0
PEPOCH 59650
POSEPOCH 54760 4.2026289288901168828e-4933
DMEPOCH 54760
START 58066.177875391477251 1
FINISH 58116.307778270281737 1
TZRMJD 58088.399365291575503
TZRFRQ 1000
TZRSITE bat
TRES 39.799
EPHVER 5
CLK TT(TAI)
MODE 1
UNITS TCB
TIMEEPH IF99
DILATEFREQ Y
PLANET_SHAPIRO Y
T2CMETHOD IAU2000B
NE_SW 4.000
CORRECT_TROPOSPHERE N
EPHEM DE405
NITS 1
NTOA 249
CHI2R 1.5028 241
JUMP freq 999 1001 -0.0008124159772934 1
JUMP freq 1999 2001 -1.4639986440894e-05 1
JUMP NAME LE -0.00088 1 1
23 changes: 19 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
from tatpulsar.pulse import barycor
import unittest

from tatpulsar.pulse import *
from tatpulsar.data import Profile
help(Profile)
class TestAlias(unittest.TestCase):
def test_alias(self):
try:
from tatpulsar.pulse import barycor
except Exception as e:
self.fail(f"Failed to call the function via alias, error: {e}")
self.assertTrue(callable(barycor), "Failed: barycor is not a function")

# Test readpar Class
from tatpulsar.utils import readpar
self.assertTrue(callable(readpar), "Failed: readpar is not a function")

# Test Profile class
from tatpulsar.data import Profile
self.assertTrue(callable(Profile), "Failed: readpar is not a function")

if __name__ == '__main__':
unittest.main()
50 changes: 50 additions & 0 deletions tests/test_gti.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import unittest
import numpy as np
from tatpulsar.utils.gti import gti_intersection, gti_union

class TestGtiMethods(unittest.TestCase):
def test_gti_intersection(self):
gti1 = [[1, 3], [4, 6]]
gti2 = [[2, 5]]
result = gti_intersection(gti1, gti2)
expected_result = [[2, 3], [4, 5]]
self.assertEqual(result, expected_result, "Failed: gti_intersection is not working as expected")

# Test if the GTIs are array
gti1 = np.array([[1, 3], [4, 6]])
gti2 = np.array([[2, 5]])
result = gti_intersection(gti1, gti2)
expected_result = [[2, 3], [4, 5]]
self.assertEqual(result, expected_result, "Failed: gti_intersection is not working as expected")

# Test if the GTIs is 1d list
gti1 = [1, 3]
gti2 = [2, 5]
result = gti_intersection(gti1, gti2)
expected_result = [[2, 3]]
self.assertEqual(result, expected_result, "Failed: gti_intersection is not working as expected")

def test_gti_union(self):
gti1 = [[1, 3], [4, 6]]
gti2 = [[2, 5]]
result = gti_union(gti1, gti2)
expected_result = [[1, 6]]
self.assertEqual(result, expected_result, "Failed: gti_union is not working as expected")

# Test if the GTIs are array
gti1 = np.array([[1, 3], [4, 6]])
gti2 = np.array([[2, 5]])
result = gti_union(gti1, gti2)
expected_result = [[1, 6]]
self.assertEqual(result, expected_result, "Failed: gti_union is not working as expected")

# Test if the GTIs is 1d list
gti1 = [1, 3]
gti2 = [2, 5]
result = gti_union(gti1, gti2)
expected_result = [[1, 5]]
self.assertEqual(result, expected_result, "Failed: gti_intersection is not working as expected")

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

41 changes: 41 additions & 0 deletions tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,46 @@ def test_hist(self):
pro = phihist(phi, nbins=20)
self.assertEqual(pro.size, 20)

def test_rebin_nbins(self):
cnt = np.random.rand(100)
pro = Profile(cnt, cycles=1)

nbins = 5
pro.rebin(nbins=nbins)
self.assertEqual(pro.counts.size, nbins, "Failed: counts size mismatch")
self.assertEqual(pro.error.size, nbins, "Failed: error size mismatch")
self.assertEqual(pro.phase.size, nbins, "Failed: phase size mismatch")

def test_rebin_factor(self):
cnt = np.random.rand(100)
pro = Profile(cnt, cycles=1)

factor = 2
pro.rebin(factor=factor)
self.assertEqual(pro.counts.size, cnt.size//factor, "Failed: counts size mismatch")
self.assertEqual(pro.error.size, cnt.size//factor, "Failed: error size mismatch")
self.assertEqual(pro.phase.size, cnt.size//factor, "Failed: phase size mismatch")

# ---
cnt = np.random.rand(100)
pro = Profile(cnt, cycles=1)

new_pro = pro.rebin(factor=factor, return_profile=True)
self.assertEqual(new_pro.counts.size, cnt.size//factor, "Failed: counts size mismatch")
self.assertEqual(new_pro.error.size, cnt.size//factor, "Failed: error size mismatch")
self.assertEqual(new_pro.phase.size, cnt.size//factor, "Failed: phase size mismatch")
self.assertEqual(new_pro.counts.size, pro.size//factor, "Failed: counts size mismatch")
self.assertEqual(new_pro.error.size, pro.size//factor, "Failed: error size mismatch")
self.assertEqual(new_pro.phase.size, pro.size//factor, "Failed: phase size mismatch")

def test_draw_random_pulse(self):
from tatpulsar.data.profile import draw_random_pulse
np.random.seed(19930727)
pro = draw_random_pulse(nbins=100, baseline=1000, pulsefrac=0.2)
self.assertTrue(isinstance(pro, Profile), "The output of pulse drawing function is not Profile object")
self.assertIsInstance(pro.significance, float, "Failed: significance calculated is not a float")
self.assertIsInstance(pro.chisq, float, "Failed: chisquare of profile calculated is not a float")
self.assertEqual(pro.dof, pro.size - 1, "Failed: d.o.f. of profile calculated is not (binsize - 1)")

if __name__ == "__main__":
unittest.main()
18 changes: 18 additions & 0 deletions tests/test_readpar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest
import numpy as np
from tatpulsar.utils import readpar

class TestReadpar(unittest.TestCase):
def test_readpar(self):
eph = readpar("./tests/data/Vela.par")
par_to_test = ['F0', "F1", "F2", "PEPOCH"]
value_to_varify = [11.183908501919459511, -1.5607051596431583974e-11, 1.2164543201957172106e-21,
59650]

for par, val in zip(par_to_test, value_to_varify):
print(par, getattr(eph, par).value, val)
try:
np.testing.assert_array_almost_equal(getattr(eph, par).value, val)
except Exception as e:
self.fail(f"{e}: The Ephemeris reading is incorrect")

0 comments on commit b55e062

Please sign in to comment.