From 09dd77ee632229daa2cb8ae61d03cce08151ec05 Mon Sep 17 00:00:00 2001 From: neo-anderson-7 Date: Sun, 2 Apr 2023 15:03:47 +0530 Subject: [PATCH 1/4] Manual Test added. Can be improved --- stingray/tests/test_spectroscopy.py | 32 +++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/stingray/tests/test_spectroscopy.py b/stingray/tests/test_spectroscopy.py index 360e9c6f7..7177bcdb7 100644 --- a/stingray/tests/test_spectroscopy.py +++ b/stingray/tests/test_spectroscopy.py @@ -229,10 +229,6 @@ def test_ccf(self): assert np.all(np.isclose(ccf_norm, avg_seg_ccf, atol=0.01)) assert np.all(np.isclose(error_ccf, np.zeros(shape=error_ccf.shape), atol=0.01)) - def test_get_mean_phase_difference(self): - _, a, b, _ = spec.get_parameters(self.ref_aps.lc1.counts, self.ref_aps.lc1.dt, self.model) - assert a == b - def test_load_lc_fits(): dt = 0.1 @@ -388,3 +384,31 @@ def test_compute_rms(): with pytest.raises(ValueError): spec.compute_rms(cs, model, criteria="filter") + +def test_get_mean_phase_difference(): + counts = [10, 20, 30, 20, 10, 20, 30, 20] + dt = 0.1 + lc = Lightcurve(time=np.arange(len(counts))*dt, counts=counts, dt=dt) + + cs = Crossspectrum(lc, lc) + model = models.Lorentz1D(x_0=2.0) + models.Lorentz1D(x_0=4.0) + + avg_psi, stddev = spec.get_mean_phase_difference(cs, model) + tol = 1e-5 + assert np.isclose(avg_psi, 0.0, tol) + assert np.isclose(stddev, 0.0, tol) + +def test_get_phase_lag(): + counts = [10, 20, 30, 20, 10, 20, 30, 20] + dt = 0.1 + lc = Lightcurve(time=np.arange(len(counts))*dt, counts=counts, dt=dt) + + cs = Crossspectrum(lc, lc) + model = models.Lorentz1D(x_0=2.0) + models.Lorentz1D(x_0=4.0) + + cap_phi_1, cap_phi_2, small_psi = spec.get_phase_lag(cs, model) + + tol = 1e-5 + assert np.isclose(cap_phi_1, 1.5708, tol) + assert np.isclose(cap_phi_2, 3.1416, tol) + assert np.isclose(small_psi, 0.0, tol) From 0c07a217a5c784fe1401adb62ba01fffddc7aa95 Mon Sep 17 00:00:00 2001 From: capy-on-caffeine Date: Mon, 1 Apr 2024 09:54:25 +0530 Subject: [PATCH 2/4] Black formatting --- stingray/tests/test_spectroscopy.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stingray/tests/test_spectroscopy.py b/stingray/tests/test_spectroscopy.py index c8097d200..a4bb50808 100644 --- a/stingray/tests/test_spectroscopy.py +++ b/stingray/tests/test_spectroscopy.py @@ -392,10 +392,11 @@ def test_compute_rms(): with pytest.raises(ValueError): spec.compute_rms(cs, model, criteria="filter") + def test_get_mean_phase_difference(): counts = [10, 20, 30, 20, 10, 20, 30, 20] dt = 0.1 - lc = Lightcurve(time=np.arange(len(counts))*dt, counts=counts, dt=dt) + lc = Lightcurve(time=np.arange(len(counts)) * dt, counts=counts, dt=dt) cs = Crossspectrum(lc, lc) model = models.Lorentz1D(x_0=2.0) + models.Lorentz1D(x_0=4.0) @@ -405,10 +406,11 @@ def test_get_mean_phase_difference(): assert np.isclose(avg_psi, 0.0, tol) assert np.isclose(stddev, 0.0, tol) + def test_get_phase_lag(): counts = [10, 20, 30, 20, 10, 20, 30, 20] dt = 0.1 - lc = Lightcurve(time=np.arange(len(counts))*dt, counts=counts, dt=dt) + lc = Lightcurve(time=np.arange(len(counts)) * dt, counts=counts, dt=dt) cs = Crossspectrum(lc, lc) model = models.Lorentz1D(x_0=2.0) + models.Lorentz1D(x_0=4.0) From e191a885a78197ec9fec32367e6ef961a001a1a5 Mon Sep 17 00:00:00 2001 From: capy-on-caffeine Date: Sun, 14 Apr 2024 18:21:06 +0530 Subject: [PATCH 3/4] improved get_mean_phase_difference, get_phase_lag --- stingray/tests/test_spectroscopy.py | 50 +++++++++++++++++++---------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/stingray/tests/test_spectroscopy.py b/stingray/tests/test_spectroscopy.py index a4bb50808..9bb349617 100644 --- a/stingray/tests/test_spectroscopy.py +++ b/stingray/tests/test_spectroscopy.py @@ -394,30 +394,46 @@ def test_compute_rms(): def test_get_mean_phase_difference(): - counts = [10, 20, 30, 20, 10, 20, 30, 20] - dt = 0.1 - lc = Lightcurve(time=np.arange(len(counts)) * dt, counts=counts, dt=dt) - + dt = 0.01 + time = np.arange(0, 100, dt) + qpo_freq = 0.1 + harmonic_freq = 2 * qpo_freq + counts_qpo = 100 * np.sin(2 * np.pi * qpo_freq * time - np.pi / 4) + counts_harmonic = 50 * np.sin(2 * np.pi * harmonic_freq * time - np.pi / 4) + counts = counts_qpo + counts_harmonic + + lc = Lightcurve(time, counts, dt=dt) cs = Crossspectrum(lc, lc) - model = models.Lorentz1D(x_0=2.0) + models.Lorentz1D(x_0=4.0) + model = models.Lorentz1D(x_0=qpo_freq, amplitude=100) + models.Lorentz1D( + x_0=harmonic_freq, amplitude=50 + ) avg_psi, stddev = spec.get_mean_phase_difference(cs, model) - tol = 1e-5 - assert np.isclose(avg_psi, 0.0, tol) - assert np.isclose(stddev, 0.0, tol) + assert np.isclose(avg_psi, np.pi / 2, 1e-5) + assert np.isclose(stddev, np.pi / 2, 1e-5) -def test_get_phase_lag(): - counts = [10, 20, 30, 20, 10, 20, 30, 20] - dt = 0.1 - lc = Lightcurve(time=np.arange(len(counts)) * dt, counts=counts, dt=dt) +def test_get_phase_lag(): + dt = 0.01 + time = np.arange(0, 100, dt) + qpo_freq = 0.1 + harmonic_freq = 2 * qpo_freq + counts_qpo = 100 * np.sin(2 * np.pi * qpo_freq * time + np.pi / 4) + counts_harmonic = 50 * np.sin(2 * np.pi * harmonic_freq * time + np.pi / 4) + counts = counts_qpo + counts_harmonic + + lc = Lightcurve(time, counts, dt=dt) cs = Crossspectrum(lc, lc) - model = models.Lorentz1D(x_0=2.0) + models.Lorentz1D(x_0=4.0) + + qpo_phase = np.pi / 4 + harmonic_phase = np.pi / 4 + model = models.Lorentz1D(x_0=qpo_freq) + models.Lorentz1D( + x_0=harmonic_freq + ) cap_phi_1, cap_phi_2, small_psi = spec.get_phase_lag(cs, model) - tol = 1e-5 - assert np.isclose(cap_phi_1, 1.5708, tol) - assert np.isclose(cap_phi_2, 3.1416, tol) - assert np.isclose(small_psi, 0.0, tol) + assert np.isclose(cap_phi_1, np.pi / 2, atol=1e-2) + assert np.isclose(cap_phi_2, 2 * np.pi, atol=1e-2) + assert np.isclose(small_psi, np.pi / 2, atol=1e-2) \ No newline at end of file From 6f576f9ff0fcc2b9db050ed6197d8c6c6c84dc1e Mon Sep 17 00:00:00 2001 From: capy-on-caffeine Date: Sun, 14 Apr 2024 18:47:59 +0530 Subject: [PATCH 4/4] formatting and changelog --- docs/changes/714.feature.rst | 1 + stingray/tests/test_spectroscopy.py | 6 ++---- 2 files changed, 3 insertions(+), 4 deletions(-) create mode 100644 docs/changes/714.feature.rst diff --git a/docs/changes/714.feature.rst b/docs/changes/714.feature.rst new file mode 100644 index 000000000..cea7eaddb --- /dev/null +++ b/docs/changes/714.feature.rst @@ -0,0 +1 @@ +Added unit tests for get_mean_phase_difference and get_phase_lag in stingray/spectroscopy.py \ No newline at end of file diff --git a/stingray/tests/test_spectroscopy.py b/stingray/tests/test_spectroscopy.py index 9bb349617..099bd073d 100644 --- a/stingray/tests/test_spectroscopy.py +++ b/stingray/tests/test_spectroscopy.py @@ -428,12 +428,10 @@ def test_get_phase_lag(): qpo_phase = np.pi / 4 harmonic_phase = np.pi / 4 - model = models.Lorentz1D(x_0=qpo_freq) + models.Lorentz1D( - x_0=harmonic_freq - ) + model = models.Lorentz1D(x_0=qpo_freq) + models.Lorentz1D(x_0=harmonic_freq) cap_phi_1, cap_phi_2, small_psi = spec.get_phase_lag(cs, model) assert np.isclose(cap_phi_1, np.pi / 2, atol=1e-2) assert np.isclose(cap_phi_2, 2 * np.pi, atol=1e-2) - assert np.isclose(small_psi, np.pi / 2, atol=1e-2) \ No newline at end of file + assert np.isclose(small_psi, np.pi / 2, atol=1e-2)