diff --git a/docs/changes/837.bugfix.rst b/docs/changes/837.bugfix.rst new file mode 100644 index 000000000..bdebe104b --- /dev/null +++ b/docs/changes/837.bugfix.rst @@ -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. diff --git a/stingray/pulse/pulsar.py b/stingray/pulse/pulsar.py index 4e84ab8bd..fce5abc41 100644 --- a/stingray/pulse/pulsar.py +++ b/stingray/pulse/pulsar.py @@ -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): @@ -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)) diff --git a/stingray/pulse/tests/test_search.py b/stingray/pulse/tests/test_search.py index 749cd1934..1e35071e5 100644 --- a/stingray/pulse/tests/test_search.py +++ b/stingray/pulse/tests/test_search.py @@ -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) @@ -120,7 +126,7 @@ 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) @@ -128,7 +134,7 @@ def test_plot_profile_existing_ax(self): 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")