Skip to content

Commit

Permalink
Change the tests. To implement the np.asset_almots_equal function, to…
Browse files Browse the repository at this point in the history
… compare the numpy arrays.
  • Loading branch information
Cristian Pozo Gonzalez committed Oct 10, 2024
1 parent 6b363e8 commit 85b4335
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ptiming_ana/phaseogram/pulsar_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def set_config(self, configuration_file):
P3_limits=conf["phase_regions"]["P3"],
)

if conf["phase_binning"]["custom_binning"]:
if not conf["phase_binning"]["custom_binning"]:
self.setBinning(
conf["phase_binning"]["nbins"],
xmin=conf["phase_binning"]["xmin"],
Expand Down
Binary file removed tests/files/fit_result_tutorial.pkl
Binary file not shown.
Binary file removed tests/files/peak_statistics_tutorial.pkl
Binary file not shown.
Binary file removed tests/files/periodicity_statistics_tutorial.pkl
Binary file not shown.
52 changes: 42 additions & 10 deletions tests/test_tutorial.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest
from ptiming_ana.phaseogram import PulsarAnalysis
from astropy.io import fits
import pandas as pd
import numpy as np


Expand Down Expand Up @@ -34,31 +33,64 @@ def test_load_config_file(self):
h.setListsInput(phases, times, energies / 1e6, tel="fermi", energy_units="TeV")
h.get_results = False

self.assertEquals(h_config_from_file, h)
self.assertEqual(h_config_from_file, h)

def test_run_analysis_phaseogram(self):
h = PulsarAnalysis()
h.set_config("tests/files/config_test.yaml")
h.run()
results = h.show_Presults()

expected_result = (
pd.read_pickle("tests/files/peak_statistics_tutorial.pkl"),
pd.read_pickle("tests/files/periodicity_statistics_tutorial.pkl"),
peak_stadistics_data_expec = np.array(
[
[108.5913594, 58.00656799, 110.16571886, 14.3384333],
[5959.23714286, 2677.27142857, 8636.50857143, 1126.74285714],
[84.22028603, 62.20823982, 105.93665554, 81.25025086],
[6969.0, 3734.0, 10703.0, 4884.0],
[1009.76285714, 1056.72857143, 2066.49142857, 3757.25714286],
[451.84108843, 241.36129211, 458.39188858, 59.661223],
[187.53441252, 82.35892672, 189.98591475, 18.38185577],
]
)

pd.testing.assert_frame_equal(results[0], expected_result[0])
pd.testing.assert_frame_equal(results[1], expected_result[1])
periodicity_statistics_data_expec = np.array(
[
[26977.43375174, 25992.17369182, 28101.4067069],
[0.0, 0.0, 0.0],
[np.inf, np.inf, np.inf],
]
)

np.testing.assert_almost_equal(
results[0].to_numpy(), peak_stadistics_data_expec, decimal=5
)
np.testing.assert_almost_equal(
results[1].to_numpy(), periodicity_statistics_data_expec, decimal=5
)

def test_run_analysis_fitting(self):
h = PulsarAnalysis()
h.set_config("tests/files/config_test.yaml")
h.run()

self.assertEquals(h.fit_model, "dgaussian")
self.assertEqual(h.fit_model, "dgaussian")
results = h.show_fit_results()
expected_result = pd.read_pickle("tests/files/fit_result_tutorial.pkl")
pd.testing.assert_frame_equal(results, expected_result)

fit_result_expected = np.array(
[
[0.9924233191795764, 0.0015660661377770266],
[0.026304542387059753, 0.0014698372893777872],
[1.3763409922947634, 0.004402295450743174],
[0.0509859742803803, 0.004192067589437329],
[466.4375, 0.0],
[200.23728941446313, 10.903033719301062],
[145.13336608138428, 11.04167277916415],
]
)

np.testing.assert_almost_equal(
results.to_numpy()[:, 1:], fit_result_expected, decimal=5
)


if __name__ == "__main__":
Expand Down

0 comments on commit 85b4335

Please sign in to comment.