Skip to content

Commit

Permalink
Merge pull request #837 from spranav1205/Event_folding_#835
Browse files Browse the repository at this point in the history
fold_events keyword validation
  • Loading branch information
matteobachetti authored Sep 19, 2024
2 parents 0baee86 + e5f0653 commit 0b1783a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
3 changes: 3 additions & 0 deletions docs/changes/837.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The ``fold_events`` function now checks if the keyword arguments (`kwargs`) are in the list of optional parameters.
If any unidentified keys are present, it raises a `ValueError`.
This fix ensures that the function only accepts valid optional parameters and provides a clear error message for unsupported keys.
22 changes: 14 additions & 8 deletions stingray/pulse/pulsar.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ def pulse_phase(times, *frequency_derivatives, **opts):
"""

ph0 = _default_value_if_no_key(opts, "ph0", 0)
to_1 = _default_value_if_no_key(opts, "to_1", True)
ph0 = opts.pop("ph0", 0)
to_1 = opts.pop("to_1", True)
ph = ph0

for i_f, f in enumerate(frequency_derivatives):
Expand Down Expand Up @@ -263,18 +263,24 @@ def fold_events(times, *frequency_derivatives, **opts):
profile_err : array of floats
The uncertainties on the pulse profile
"""
mode = _default_value_if_no_key(opts, "mode", "ef")
nbin = _default_value_if_no_key(opts, "nbin", 16)
weights = _default_value_if_no_key(opts, "weights", 1)

mode = opts.pop("mode", "ef")
nbin = opts.pop("nbin", 16)
weights = opts.pop("weights", 1)
# If no key is passed, *or gti is None*, defaults to the
# initial and final event
gti = _default_value_if_no_key(opts, "gti", None)
gti = opts.pop("gti", None)
if gti is None:
gti = [[times[0], times[-1]]]
# Be safe if gtis are a list
gti = np.asanyarray(gti)
ref_time = _default_value_if_no_key(opts, "ref_time", 0)
expocorr = _default_value_if_no_key(opts, "expocorr", False)
ref_time = opts.pop("ref_time", 0)
expocorr = opts.pop("expocorr", False)

if opts:
raise ValueError(
f"Unidentified keyword(s) to fold_events: {', '.join([k for k in opts.keys()])} \n Please refer to the description of the function for optional parameters."
)

if not isinstance(weights, Iterable):
weights *= np.ones(len(times))
Expand Down
10 changes: 8 additions & 2 deletions stingray/pulse/tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ def test_plot_phaseogram_direct(self):
plt.savefig("phaseogram_direct.png")
plt.close(plt.gcf())

def test_search_wrong_key_fails(self):
with pytest.raises(
ValueError, match=r"Unidentified keyword\(s\) to fold_events: fdot, fddot"
):
phase, prof, _ = fold_events(self.event_times, self.pulse_frequency, fdot=12, fddot=34)

def test_plot_profile(self):
phase, prof, _ = fold_events(self.event_times, self.pulse_frequency)
ax = plot_profile(phase, prof)
Expand All @@ -120,15 +126,15 @@ def test_plot_profile(self):
def test_plot_profile_existing_ax(self):
fig = plt.figure("Pulse profile")
ax = plt.subplot()
phase, prof, _ = fold_events(self.event_times, self.pulse_frequency, ax=ax)
phase, prof, _ = fold_events(self.event_times, self.pulse_frequency)
ax = plot_profile(phase, prof, ax=ax)
plt.savefig("profile_existing_ax.png")
plt.close(fig)

def test_plot_profile_errorbars(self):
fig = plt.figure("Pulse profile")
ax = plt.subplot()
phase, prof, err = fold_events(self.event_times, self.pulse_frequency, ax=ax)
phase, prof, err = fold_events(self.event_times, self.pulse_frequency)

ax = plot_profile(phase, prof, err=err, ax=ax)
plt.savefig("profile_errorbars.png")
Expand Down

0 comments on commit 0b1783a

Please sign in to comment.