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

Make sure no cast warning is issued when saving #149

Merged
merged 2 commits into from
Dec 16, 2023

Conversation

mraspaud
Copy link
Member

This PR ensures no cast warning is raised when saving an image that contains nans to int.

Includes a refactor of one big test method.

  • Tests added (for all bug fixes or enhancements)
  • Tests passed (for all non-documentation changes)
  • Passes git diff origin/master **/*py | flake8 --diff (remove if you did not edit any Python files)

Includes a refactor of one big test method
@mraspaud mraspaud added the bug label Oct 24, 2023
@mraspaud mraspaud requested a review from djhoese October 24, 2023 09:53
@mraspaud mraspaud self-assigned this Oct 24, 2023
@codecov
Copy link

codecov bot commented Oct 24, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (6617ff7) 91.23% compared to head (f21c984) 91.60%.
Report is 52 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #149      +/-   ##
==========================================
+ Coverage   91.23%   91.60%   +0.37%     
==========================================
  Files          11       12       +1     
  Lines        3867     4004     +137     
==========================================
+ Hits         3528     3668     +140     
+ Misses        339      336       -3     
Flag Coverage Δ
unittests 91.60% <100.00%> (+0.37%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@coveralls
Copy link

coveralls commented Oct 24, 2023

Coverage Status

coverage: 91.838% (+0.4%) from 91.479%
when pulling f21c984 on mraspaud:fix-cast-warning
into 6617ff7 on pytroll:main.

Copy link
Member

@djhoese djhoese left a comment

Choose a reason for hiding this comment

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

If it comes down to it, I'm OK merging this as is. However, I had one inline comment and if you have any time I'd like a review of methods like:

https://github.com/mraspaud/trollimage/blob/5a8519d4b6df9ab67b1f432e1d4b1d92fea351bc/trollimage/xrimage.py#L824-L837

And see if something in those should be updated to do this fillna there.

Otherwise, could we get something in the docstring (maybe the main save at least?) about the behavior of NaNs/NA handling? Normally I wouldn't mind for such a small change, but as mentioned on slack this is really defining a previously undefined behavior so it'd be nice to document it.

@@ -683,6 +683,8 @@ def _scale_to_dtype(self, data, dtype, fill_value=None):
data = data.clip(0, 1) * scale + offset
attrs.setdefault('enhancement_history', list()).append({'scale': scale, 'offset': offset})
data = data.round()
if fill_value is None:
data = data.fillna(np.iinfo(dtype).min)
Copy link
Member

Choose a reason for hiding this comment

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

Correct me if I'm wrong, but while looking for where this _scale_to_dtype method is used I noticed there is a spot where right after this method is called the _replace_fill_value method is called and I think when the second argument (ifill) is passed as None it performs the equivalent of what you've done here. We may want to reuse that method. However, you'd still have to do the iinfo so maybe this isn't worth it.

@djhoese
Copy link
Member

djhoese commented Dec 9, 2023

This PR just got more interesting to me. I have a local branch where I'm trying to remove all test warnings. I ran into the ones fixed here and remembered this PR existed. What were you thoughts on my comment(s)?

@mraspaud
Copy link
Member Author

What were you thoughts on my comment(s)?

They make sense, but I didn't have the time to investigate further yet.

@djhoese djhoese mentioned this pull request Dec 15, 2023
5 tasks
@djhoese
Copy link
Member

djhoese commented Dec 16, 2023

I spent a lot of time looking at this and how the scale method is called in different ways and I couldn't find a way to refactor this stuff without starting from scratch. At first I was able to reuse some of the logic, but ran into issues when this fillna that is being added is for integer output but to provide a fill value (the np.iinfo(dtype).min) means for floating point input data we scale it with the fill value in mind (ex. scale 1-255 but leave 0 for fill value only). This particular fillna addition is not meant to have an effect on the data output so it means I/we can't reuse the existing methods. So I left @mraspaud's code as-is.

I did make a change to how the test was working. When working out what was failing I got annoyed that the existing warning check was happening at the end of the test function rather than the actual .save call that was producing it. It now does it at the specific line.

@djhoese djhoese merged commit cb62b3b into pytroll:main Dec 16, 2023
25 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants