Skip to content

Fix lookup table enhancement removing transparency (NaNs)#3377

Merged
mraspaud merged 5 commits intopytroll:mainfrom
Grukank:snow-age-transparency
May 4, 2026
Merged

Fix lookup table enhancement removing transparency (NaNs)#3377
mraspaud merged 5 commits intopytroll:mainfrom
Grukank:snow-age-transparency

Conversation

@Grukank
Copy link
Copy Markdown
Contributor

@Grukank Grukank commented Apr 13, 2026

In colormap.py in Enhancements, preserves the NaN values lost during lookup and replaces them again where they were lost. For SMHI only relevant for snow_age.

  • Tests added/updated
  • Add your name to AUTHORS.md if not there already

@Grukank Grukank marked this pull request as ready for review April 13, 2026 12:36
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.34%. Comparing base (999c6f3) to head (4c5ade6).
⚠️ Report is 31 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3377   +/-   ##
=======================================
  Coverage   96.33%   96.34%           
=======================================
  Files         466      466           
  Lines       59119    59160   +41     
=======================================
+ Hits        56953    56995   +42     
+ Misses       2166     2165    -1     
Flag Coverage Δ
behaviourtests 3.58% <0.00%> (-0.01%) ⬇️
unittests 96.42% <100.00%> (+<0.01%) ⬆️

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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

lut = luts[:, index] if len(luts.shape) == 2 else luts
band_data = band_data.clip(0, lut.size - 1)
# Convert to uint8, with NaN/null values changed into 0
band_data = np.nan_to_num(band_data).astype(np.uint8)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Your changes are similar to the lines here that were added a while ago. This was done in #3315 by @avalentino to avoid warnings on NaN data and an error on one particular architecture. Your new solution should be fine to handle this as well if re-implemented here. My main question is, is this something we want the user to be able to control? Mainly, should the user be able to have NaN map to a value in the LUT? Technically that's what the nan_to_num is allowing but the proposed solution is ensuring that NaNs always remain NaNs.

Regardless, the proposed solution seems like something that could be handled inside this _lookup_table function and would therefore have better performance as it happens in parallel per-chunk of the dask array. With your code moved into this function or rather re-implemented with pure-numpy functions/masking/indexing then this nan_to_num line could probably be removed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree that it would be better to have it in _lookup_table instead, so I'll move it/re-implement it there.

Regarding your question about the user mapping NaN to a (custom?) value in the LUT, after speaking about it with @mraspaud, it could be useful but I'm not so sure if it's needed, but in any case it's a bit outside of the scope of this pull request. It would also be a bit more complicated to implement and would change the functionality a bit too much (atleast for right here, right now).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've rewritten this comment at least 3 times now because I kept coming up with a different way of implementing this, but I think I've decided none of those ways were better than what you have here. So 👍

Neened to keep nan_to_num to avoid warning: "RuntimeWarning: invalid value encountered in cast"
@djhoese djhoese changed the title Enable transparency for Snow Age Fix lookup table enhancement removing transparency (NaNs) Apr 20, 2026
@mraspaud mraspaud merged commit 2bd33b8 into pytroll:main May 4, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants