-
Notifications
You must be signed in to change notification settings - Fork 145
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
fold_events keyword validation #837
fold_events keyword validation #837
Conversation
Hello @spranav1205! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:
Comment last updated at 2024-09-19 13:09:28 UTC |
Hello @spranav1205, thanks a lot for your contribution! Although this solution is pretty neat, I think there is a leaner solution to this, that we use in other parts of the code but did not get around to use here. You PR is a good opportunity to refresh some of this old code! |
Yes, I believe this approach would work well, and it would definitely be more systematic. Should I go ahead and implement it? Additionally, I noticed that |
stingray/pulse/pulsar.py
Outdated
# 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 = dict.pop(opts, "gti", None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, sorry, I think you can just use opts.pop("gti", None)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made the changes
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #837 +/- ##
===========================================
- Coverage 96.53% 78.76% -17.78%
===========================================
Files 48 48
Lines 9257 9285 +28
===========================================
- Hits 8936 7313 -1623
- Misses 321 1972 +1651 ☔ View full report in Codecov by Sentry. |
@spranav1205 there is a small remaining issue that makes some tests fail, do you thing you can address it? |
Yes, I'll look into that. And the other thing too. |
@matteobachetti I noticed that the failing test cases are due to the
You can review the relevant test code here: GitHub Permalink. What would you like me to do? Also, I made the changelog. Please let me know if there's any mistake or anything else I should take care of while contributing. |
@spranav1205 If I read this correctly, the |
test_plot_profile_errorbars, test_plot_profile_existing_ax
I have addressed the test cases. I think it should be fine now. |
stingray/pulse/pulsar.py
Outdated
|
||
if opts: | ||
raise ValueError( | ||
f"Unidentified keys: {opts.keys()} \n Please refer to the description of the function for optional parameters." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unidentified keyword to fold_events: {opts.keys()}
etc. would probably be more informative
@spranav1205 one last request: a test that the error is really raised, to be added to
Here, |
I have made the changes. Should I add another test or a more generic one, or does this suffice? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spranav1205 I suggested a couple of further modifications to the test, below. This makes the error cleaner (just the keys, without the confusing dict_keys
syntax).
After this, I will accept and merge.
Thanks for this very useful addition to Stingray!
stingray/pulse/pulsar.py
Outdated
|
||
if opts: | ||
raise ValueError( | ||
f"Unidentified keyword(s) to fold_events: {opts.keys()} \n Please refer to the description of the function for optional parameters." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f"Unidentified keyword(s) to fold_events: {opts.keys()} \n Please refer to the description of the function for optional parameters." | |
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." |
stingray/pulse/tests/test_search.py
Outdated
@@ -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="Unidentified keyword(s) to fold_events: dict_keys(['fdot'])" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ValueError, match="Unidentified keyword(s) to fold_events: dict_keys(['fdot'])" | |
ValueError, match="Unidentified keyword(s) to fold_events: fdot, fddot" |
stingray/pulse/tests/test_search.py
Outdated
with pytest.raises( | ||
ValueError, match="Unidentified keyword(s) to fold_events: dict_keys(['fdot'])" | ||
): | ||
phase, prof, _ = fold_events(self.event_times, self.pulse_frequency, fdot=12) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
phase, prof, _ = fold_events(self.event_times, self.pulse_frequency, fdot=12) | |
phase, prof, _ = fold_events(self.event_times, self.pulse_frequency, fdot=12, fddot=34) |
I made the changes. Thanks for letting me work on this, it was my first open-source contribution. You've been a great help. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The match regex isn't working, probably better to simplify it to the point it's specific (it contains the correct keywords) but it does not contain problematic characters (e.g. escapes)
stingray/pulse/tests/test_search.py
Outdated
def test_search_wrong_key_fails(self): | ||
with pytest.raises( | ||
ValueError, | ||
match="Unidentified keyword(s) to fold_events: fdot, fddot \n Please refer to the description of the function for optional parameters.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
match="Unidentified keyword(s) to fold_events: fdot, fddot \n Please refer to the description of the function for optional parameters.", | |
match="Unidentified keyword(s) to fold_events: fdot, fddot", |
Its better to match the smallest possible part of the regex that makes it really specific. Try to run it locally in your computer, with the --run-slow
option, to verify that it works
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The brackets were causing that issue. I fixed it and the test is working on my PC.
Two checks are failing. It says the tests do not cover line 281.
Line 281 is raise ValueError(
. But the test case covers it so I am not sure why the check is failing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @spranav1205 !
0b1783a
Relevant Issue(s)/PR(s)
Issue: fold_events accepts keywords and ignores them (#835)
Overview of Implemented Solution
I implemented a function to verify if the keywords passed to
fold_events
match the optional parameters.Description
I created a function called
validate_key_in_list
. This function takes two arguments:key
andoptional_parameters_list
. It checks if thekey
is present in theoptional_parameters_list
. If thekey
is not found in the list, the user receives a warning. Additionally, I have included an optionalValueError
, which is commented out for now.Note: This is my first contribution to the project. 😊