Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for get_phase_lag and get_mean_phase_difference #714

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions docs/changes/714.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added unit tests for get_mean_phase_difference and get_phase_lag in stingray/spectroscopy.py
48 changes: 44 additions & 4 deletions stingray/tests/test_spectroscopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,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
Expand Down Expand Up @@ -395,3 +391,47 @@ def test_compute_rms():

with pytest.raises(ValueError):
spec.compute_rms(cs, model, criteria="filter")


def test_get_mean_phase_difference():
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused by the phases here - it looks like both are -pi/4. Should they not be -pi/4 for one, and +pi/4 for the other, if you want a difference of pi/2?

counts = counts_qpo + counts_harmonic

lc = Lightcurve(time, counts, dt=dt)
cs = Crossspectrum(lc, lc)

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)

assert np.isclose(avg_psi, np.pi / 2, 1e-5)
assert np.isclose(stddev, np.pi / 2, 1e-5)


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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above - it looks like the phases here are the same. I think I am also misunderstanding something, but should cap_phi_1 and cap_phi_2 not be pi/4 then, and small_psi 0?

counts = counts_qpo + counts_harmonic

lc = Lightcurve(time, counts, dt=dt)
cs = Crossspectrum(lc, lc)

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)

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)
Loading