Repository navigation
fix: float comparison in Sentinel-2 saturation - #12
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
read_datacube detected saturation by comparing the float32 cube with the float64 TOA value of the saturated DN. NumPy 1 cast that scalar to float32; NumPy 2 compares in float64, where the rounded band values no longer match, so saturated pixels were not flagged. The saturation layers feed whiteness, the potential cloud test, the absolute snow test and the NDVI/NDSI fix in the land variability probability. Compute saturation from the DN inside read_band instead, so no float precision is involved. Sentinel2.read_band now takes the same arguments as Landsat.read_band and likewise returns the image with its saturated pixels. This changes masks on scenes with saturated visible pixels when running NumPy 2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Installs the locked test environment with pixi on Linux, so a pull request also fails if pixi.lock has drifted from pixi.toml. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Closes https://github.com/NASA-IMPACT/hls_development/issues/441
The Fmask code from GERSL has no library versions included in the installation details, but anecodtally it seems like it was developed on very old versions, including
numpy<2. NumPy v2 was released in June, 2024, so anyone following their conda-based install details in recent years would get a version of NumPy v2.This major version difference is problematic because of a change in NumPy v2 regarding type promotion rules following NEP-50. The code does a comparison of band data in
float32against a (Python-dtype)float, so in NumPy v2 a type promotion occurs and saturation comparison checks fail.To give a brief example:
This doesn't happen in the Landsat code path because the saturation check was done in units of DNs (integers, before scaling) rather than in (float) units of TOA reflectance.
What I did
I fixed this by mimicking the Landsat code path:
saturation: bool = Falseto theSentinel2.read_bandmethod and when True return a boolean array of the saturation check done in units of DNsread_bandcallsI added unit tests for this use case and set up the repo to run the unit tests in CI for PRs and merges to
main.Once this is merged I'll create an issue upstream and perhaps include a PR if the merge isn't too gnarly. Alas the GERSL repository has switched off of the
mainbranch and created an entirely new Git history in theirdevelopbranch, making synchronization more difficult than it needs to be.