Skip to content

Commit b02d5f8

Browse files
authored
set unit for hotspots output (#686)
1 parent 77d3168 commit b02d5f8

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

Diff for: xrspatial/focal.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
from functools import partial
23
from math import isnan
34

@@ -740,7 +741,10 @@ def hotspots(raster, kernel):
740741
)
741742
out = mapper(raster)(raster, kernel)
742743

744+
attrs = copy.deepcopy(raster.attrs)
745+
attrs['unit'] = '%'
746+
743747
return DataArray(out,
744748
coords=raster.coords,
745749
dims=raster.dims,
746-
attrs=raster.attrs)
750+
attrs=attrs)

Diff for: xrspatial/tests/test_focal.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -421,14 +421,30 @@ def test_hotspots_numpy(data_hotspots):
421421
data, kernel, expected_result = data_hotspots
422422
numpy_agg = create_test_raster(data)
423423
numpy_hotspots = hotspots(numpy_agg, kernel)
424-
general_output_checks(numpy_agg, numpy_hotspots, expected_result)
424+
general_output_checks(numpy_agg, numpy_hotspots, expected_result, verify_attrs=False)
425+
# validate attrs
426+
assert numpy_hotspots.shape == numpy_agg.shape
427+
assert numpy_hotspots.dims == numpy_agg.dims
428+
for coord in numpy_agg.coords:
429+
np.testing.assert_allclose(
430+
numpy_hotspots[coord].data, numpy_agg[coord].data, equal_nan=True
431+
)
432+
assert numpy_hotspots.attrs['unit'] == '%'
425433

426434

427435
def test_hotspots_dask_numpy(data_hotspots):
428436
data, kernel, expected_result = data_hotspots
429437
dask_numpy_agg = create_test_raster(data, backend='dask')
430438
dask_numpy_hotspots = hotspots(dask_numpy_agg, kernel)
431-
general_output_checks(dask_numpy_agg, dask_numpy_hotspots, expected_result)
439+
general_output_checks(dask_numpy_agg, dask_numpy_hotspots, expected_result, verify_attrs=False)
440+
# validate attrs
441+
assert dask_numpy_hotspots.shape == dask_numpy_agg.shape
442+
assert dask_numpy_hotspots.dims == dask_numpy_agg.dims
443+
for coord in dask_numpy_agg.coords:
444+
np.testing.assert_allclose(
445+
dask_numpy_hotspots[coord].data, dask_numpy_agg[coord].data, equal_nan=True
446+
)
447+
assert dask_numpy_hotspots.attrs['unit'] == '%'
432448

433449

434450
@cuda_and_cupy_available

0 commit comments

Comments
 (0)