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

Overflow error with NDVI calculation #726

Closed
jessjaco opened this issue Jul 3, 2022 · 2 comments · Fixed by #755
Closed

Overflow error with NDVI calculation #726

jessjaco opened this issue Jul 3, 2022 · 2 comments · Fixed by #755
Assignees
Labels
bug Something isn't working high-priority

Comments

@jessjaco
Copy link

jessjaco commented Jul 3, 2022

Describe the bug
ndvi is giving incorrect results when input arrays are unsigned integer due to overflow in subtraction.

Expected behavior
Consider the following

import numpy as np
import xarray as xr
import xrspatial

a = xr.DataArray(np.array([[1,1,1],[1,1,1]], dtype='uint16'))
b = xr.DataArray(np.array([[0,1,2],[0,1,2]], dtype='uint16'))
xrspatial.ndvi(a,b)

Results:

<xarray.DataArray 'ndvi' (dim_0: 2, dim_1: 3)>
array([[1.000000e+00, 0.000000e+00, 6.148915e+18],
       [1.000000e+00, 0.000000e+00, 6.148915e+18]], dtype=float32)
Dimensions without coordinates: dim_0, dim_1

Values in third column are due to overflow of unsigned integer during subtraction, see numpy/numpy#21237.
Using the above data, this can be illustrated by

>>> a[0,2] - b[0,2]
<xarray.DataArray ()>
array(65535, dtype=uint16)

A solution could be to call np.subtract directly in e.g. _normalized_ratio_cpu, though that may not be robust to all cases. I think there are some alternatives in the numpy issues linked above

>>> np.subtract(a[0,2], b[0,2], dtype='int32')
<xarray.DataArray ()>
array(-1, dtype=int32)

@jessjaco jessjaco added the bug Something isn't working label Jul 3, 2022
@brendancol
Copy link
Contributor

@thuydotm can you take a look at this?

@thuydotm thuydotm moved this to 📋 Backlog in Xarray-Spatial Dec 20, 2022
@thuydotm thuydotm moved this from 📋 Backlog to 🔖 Ready in Xarray-Spatial Jan 12, 2023
@brendancol
Copy link
Contributor

@thuydotm let's put this as high-priority

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working high-priority
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants