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

DM-22727: Add numpy warnings catch to DiaCalculationPlugins #70

Merged
merged 1 commit into from Jan 9, 2020

Conversation

morriscb
Copy link
Contributor

@morriscb morriscb commented Jan 6, 2020

No description provided.

Copy link
Contributor

@isullivan isullivan left a comment

Choose a reason for hiding this comment

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

This looks fine as-is, but I think this would be a good use for a decorator, e.g. @catchNumpyWarnings. If you set it up so you can pass strings of the warnings to ignore to it, then you could simply add the decorator to each function instead of duplicating the with np.warnings.catch_warnings(): block everywhere.

@@ -559,6 +573,7 @@ def calculate(self,
**kwargs
Any additional keyword arguments that may be passed to the plugin.
"""

Copy link
Contributor

Choose a reason for hiding this comment

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

No blank line after a function docstring.

return pd.Series(dict((tileName, pTile)
for tileName, pTile in zip(pTileNames, pTiles)))
with np.warnings.catch_warnings():
np.warnings.filterwarnings('ignore', r'All-NaN slice encountered')
Copy link
Member

Choose a reason for hiding this comment

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

Can we please use double quotes to be consistent with the rest of the file? Also, why is this a raw string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. The raw string is based on what I saw in examples both online and in our own code: https://github.com/lsst/pipe_tasks/blob/448f008be835c65bd1e4254d7fddb65aacc18f68/python/lsst/pipe/tasks/functors.py#L541

Copy link
Member

Choose a reason for hiding this comment

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

Odd. It can't be doing anything useful can it? Sounds like cargo cult 😄

@morriscb
Copy link
Contributor Author

morriscb commented Jan 7, 2020

Hey, @isullivan can you take a look at the new commit and see if that works for you?

@jdswinbank
Copy link
Contributor

With apologies for the drive-by comment —

  • I don't think there's anything Numpy-specific about this, is there? numpy.warnings is just an alias for warnings. As such, I'd suggest changing the import, function name, etc, to avoid tying it to Numpy.
  • It seems confusing to use warnings.catch_warnings, which is a context manager, without a with statement. I'm about to get on a plane so I can't check if this works (and maybe I missed something), but it makes me nervous.
  • I know you discussed this with Tim, above, but... if you don't need raw strings, please don't use them. Even if other people have elsewhere!

def decoratorCatchWarnings(func):
@functools.wraps(func)
def wrapperCatchWarnings(*args, **kwargs):
np.warnings.catch_warnings()
Copy link
Contributor

Choose a reason for hiding this comment

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

Make this with np.warnings.catch_warnings(): and indent the for loop and return statement below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this. Definitely a bug I introduced while re-writing this.

@isullivan
Copy link
Contributor

As written, this swallows the specified warnings globally, so you definitely want to still use with in your decorator.
As John and Tim mentioned, you don't need the raw strings here.

@morriscb
Copy link
Contributor Author

morriscb commented Jan 8, 2020

Verified the changes are working on the ap_verify CI dataset. Will submit another Jenkins just in case. Mind having one last look @isullivan?

@isullivan
Copy link
Contributor

My linter still complains about the blank line on 589, which you should remove. Otherwise, it looks good.

Fix linting.

Create decorator for cathing numpy warnings.

move to python warnings.

Fix context bug.

Remove blank line
@morriscb morriscb merged commit d66c66d into master Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants