Repository navigation
fixes NDVI with target_band #428
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
Changes from 1 commit
f6531a6
5fe3030
f9399e0
bf5080e
d9b44b0
1d21a31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import numpy as np | ||
| import xarray as xr | ||
|
|
||
| from openeo_processes_dask.process_implementations.data_model import RasterCube | ||
|
|
@@ -12,6 +13,47 @@ | |
| __all__ = ["ndvi"] | ||
|
|
||
|
|
||
| def _dummy_value(dtype): | ||
| """A sensible 'missing' fill value for a given numpy dtype.""" | ||
| if np.issubdtype(dtype, np.floating): | ||
| return np.nan | ||
| if np.issubdtype(dtype, np.datetime64): | ||
| return np.datetime64("NaT") | ||
| if np.issubdtype(dtype, np.timedelta64): | ||
| return np.timedelta64("NaT") | ||
| if np.issubdtype(dtype, np.integer): | ||
| return -1 # ints can't hold NaN | ||
| if np.issubdtype(dtype, np.bool_): | ||
| return False | ||
| return "" # str / object | ||
|
|
||
|
|
||
| def _add_missing_coords(target, reference, dim): | ||
| """ | ||
| Ensure `target` has every coordinate that `reference` has. | ||
| Missing coordinates are created with a dummy value | ||
| """ | ||
| missing = {} | ||
| for name, coord in reference.coords.items(): | ||
| if name in target.coords: | ||
| continue | ||
|
|
||
| fill = _dummy_value(coord.dtype) | ||
|
|
||
| if coord.dims == (): # scalar coordinate | ||
|
jonas-hurst marked this conversation as resolved.
Resolved
|
||
| missing[name] = ((), np.array(fill, dtype=coord.dtype)) | ||
|
|
||
| elif coord.dims == (dim,): # coordinate along concat dim | ||
| missing[name] = (dim, np.full(target.sizes[dim], fill, dtype=coord.dtype)) | ||
|
|
||
| elif all(d in target.dims for d in coord.dims): # other dims | ||
| shape = tuple(target.sizes[d] for d in coord.dims) | ||
| missing[name] = (coord.dims, np.full(shape, fill, dtype=coord.dtype)) | ||
|
jonas-hurst marked this conversation as resolved.
Resolved
|
||
| # else: dims don't exist in target -> can't sensibly create it, skip | ||
|
|
||
| return target.assign_coords(**missing) if missing else target | ||
|
|
||
|
|
||
| def ndvi(data: RasterCube, nir="nir", red="red", target_band=None): | ||
| if len(data.openeo.band_dims) == 0: | ||
| raise DimensionAmbiguous( | ||
|
|
@@ -54,6 +96,10 @@ def ndvi(data: RasterCube, nir="nir", red="red", target_band=None): | |
| if target_band in data.coords: | ||
| raise BandExists("A band with the specified target name exists.") | ||
| nd = nd.expand_dims(band_dim).assign_coords({band_dim: [target_band]}) | ||
| nd = xr.merge([data, nd]) | ||
|
|
||
| # add potentially missing coords from data to nd, so that xr.concat works | ||
| nd = _add_missing_coords(nd, data, band_dim) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are 4 consecutive assignments to a variable called "nd" (which I assume is short for normalized difference), and it seems unlikely that all 4 are the normalized difference. If you rewrite this as: thing = _add_missing_coords(nd.expand_dims(band_dim).assign_coords({band_dim: [target_band]}), data, band_dim)
nd = xr.concat([data, thing], dim=band_dim)we can reduce that to 2 consecutive assignments, and the surrounding code still works. (I don't know what "thing" should be called instead, but you probably know what it is and can name it properly since you're in the process of modifying this code.)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disagree. Normalized difference is first computed, then dims and coords are changed to match the original datacube to which it will be added. |
||
| nd = xr.concat([data, nd], dim=band_dim) | ||
|
|
||
| nd.attrs = data.attrs | ||
| return nd | ||
Uh oh!
There was an error while loading. Please reload this page.