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 nodata casting consistent based on new array type #531

Merged
merged 9 commits into from Apr 9, 2024

Conversation

rhugonnet
Copy link
Contributor

@rhugonnet rhugonnet commented Mar 29, 2024

This PR fixes the fact that set_nodata was circumvented during from_array() creation, making the behaviour fully consistent for checking nodata input and setting.
Additionally, for practicality, it adds a cast_nodata boolean argument to from_array() defaulting to True which re-casts the input nodata to the default of the array type, if the nodata value was incompatible with the input array type.

The impact of this last change was actually much less of a bigger than I thought, because all integer nodata values are compatible with floating array types and the other direction rarely happens. There is almost no NumPy operations that convert from float to int (even operations like floordiv keep the output a float even if the output of the operation could technically be integers without loss of information, for instance).

So this mostly addresses the cases where the user is copying the data with a new array that has different data type (fairly common), or provided a non-compatible nodata value directly to from_array (less common).

Resolves #517
Resolves #516

TO-DO:

  • Use set_nodata() in from_array section of __init__(),
  • Add optional casting of nodata for new array type in Raster.copy(),
  • Add automatic casting of nodata for NumPy operations.
  • Add tests of new behaviour.

@rhugonnet rhugonnet changed the title Make nodata casting consistent based on expected new array type, including NumPy/Python operation Make nodata casting consistent based on new array type including NumPy operations Mar 29, 2024
@rhugonnet rhugonnet changed the title Make nodata casting consistent based on new array type including NumPy operations Make nodata casting consistent based on new array type Mar 30, 2024
@rhugonnet rhugonnet requested a review from adehecq March 30, 2024 03:21
geoutils/raster/satimg.py Outdated Show resolved Hide resolved
tests/test_raster.py Outdated Show resolved Hide resolved
Copy link
Member

@adehecq adehecq left a comment

Choose a reason for hiding this comment

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

All good ! There's just a block of code that was deleted but I don't understand why.

@rhugonnet rhugonnet merged commit 2b6ec0c into GlacioHack:main Apr 9, 2024
13 checks passed
@rhugonnet rhugonnet deleted the nodata_casting branch April 9, 2024 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants