Skip to content

Commit 0e772f3

Browse files
committed
wip: updating classification user-guide
1 parent f967b26 commit 0e772f3

File tree

5 files changed

+680
-42
lines changed

5 files changed

+680
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,4 @@ dmypy.json
130130
.pyre/
131131
.DS_Store
132132
/test_tiles_output
133+
*.TIF*

examples/user_guide/5_Classification.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@
365365
"name": "python",
366366
"nbconvert_exporter": "python",
367367
"pygments_lexer": "ipython3",
368-
"version": "3.6.10"
368+
"version": "3.8.3"
369369
},
370370
"nbTranslate": {
371371
"displayLangs": [

examples/user_guide/8_Remote_Sensing.ipynb

Lines changed: 645 additions & 9 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aws s3 cp s3://landsat-pds/L8/003/017/LC80030172015001LGN00/ . --recursive

xrspatial/multispectral.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
from numba import cuda
66

7-
import datashader as ds
8-
97
from PIL import Image
108

119
from xarray import DataArray
@@ -27,7 +25,7 @@ class cupy(object):
2725

2826
@ngjit
2927
def _avri_cpu(nir_data, red_data, blue_data):
30-
out = np.zeros_like(nir_data)
28+
out = np.zeros_like(nir_data, dtype='f4')
3129
rows, cols = nir_data.shape
3230
for y in range(0, rows):
3331
for x in range(0, cols):
@@ -79,7 +77,8 @@ def _arvi_dask_cupy(nir_data, red_data, blue_data):
7977
return out
8078

8179

82-
def arvi(nir_agg: DataArray, red_agg: DataArray, blue_agg: DataArray, name='arvi'):
80+
def arvi(nir_agg: DataArray, red_agg: DataArray, blue_agg: DataArray,
81+
name='arvi'):
8382
"""Computes Atmospherically Resistant Vegetation Index
8483
8584
Parameters
@@ -121,7 +120,7 @@ def arvi(nir_agg: DataArray, red_agg: DataArray, blue_agg: DataArray, name='arvi
121120
# EVI -------------
122121
@ngjit
123122
def _evi_cpu(nir_data, red_data, blue_data, c1, c2, soil_factor, gain):
124-
out = np.zeros_like(nir_data)
123+
out = np.zeros_like(nir_data, dtype='f4')
125124
rows, cols = nir_data.shape
126125
for y in range(0, rows):
127126
for x in range(0, cols):
@@ -159,7 +158,8 @@ def _evi_cupy(nir_data, red_data, blue_data, c1, c2, soil_factor, gain):
159158
griddim, blockdim = cuda_args(nir_data.shape)
160159
out = cupy.empty(nir_data.shape, dtype='f4')
161160
out[:] = cupy.nan
162-
_evi_gpu[griddim, blockdim](nir_data, red_data, blue_data, c1, c2, soil_factor, gain, out)
161+
args = (nir_data, red_data, blue_data, c1, c2, soil_factor, gain, out)
162+
_evi_gpu[griddim, blockdim](*args)
163163
return out
164164

165165

@@ -231,8 +231,9 @@ def evi(nir_agg: DataArray, red_agg: DataArray, blue_agg: DataArray,
231231
dask_func=_evi_dask,
232232
cupy_func=_evi_cupy,
233233
dask_cupy_func=_evi_dask_cupy)
234-
235-
out = mapper(red_agg)(nir_agg.data, red_agg.data, blue_agg.data, c1, c2, soil_factor, gain)
234+
235+
out = mapper(red_agg)(nir_agg.data, red_agg.data, blue_agg.data, c1, c2,
236+
soil_factor, gain)
236237

237238
return DataArray(out,
238239
name=name,
@@ -244,7 +245,7 @@ def evi(nir_agg: DataArray, red_agg: DataArray, blue_agg: DataArray,
244245
# GCI -------------
245246
@ngjit
246247
def _gci_cpu(nir_data, green_data):
247-
out = np.zeros_like(nir_data)
248+
out = np.zeros_like(nir_data, dtype='f4')
248249
rows, cols = nir_data.shape
249250
for y in range(0, rows):
250251
for x in range(0, cols):
@@ -314,7 +315,7 @@ def gci(nir_agg: DataArray, green_agg: DataArray, name='gci'):
314315
dask_func=_gci_dask,
315316
cupy_func=_gci_cupy,
316317
dask_cupy_func=_gci_dask_cupy)
317-
318+
318319
out = mapper(nir_agg)(nir_agg.data, green_agg.data)
319320

320321
return DataArray(out,
@@ -354,7 +355,7 @@ def nbr(nir_agg: DataArray, swir2_agg: DataArray, name='nbr'):
354355
dask_func=_run_normalized_ratio_dask,
355356
cupy_func=_run_normalized_ratio_cupy,
356357
dask_cupy_func=_run_normalized_ratio_dask_cupy)
357-
358+
358359
out = mapper(nir_agg)(nir_agg.data, swir2_agg.data)
359360

360361
return DataArray(out,
@@ -401,7 +402,7 @@ def nbr2(swir1_agg: DataArray, swir2_agg: DataArray, name='nbr'):
401402
dask_func=_run_normalized_ratio_dask,
402403
cupy_func=_run_normalized_ratio_cupy,
403404
dask_cupy_func=_run_normalized_ratio_dask_cupy)
404-
405+
405406
out = mapper(swir1_agg)(swir1_agg.data, swir2_agg.data)
406407

407408
return DataArray(out,
@@ -432,14 +433,14 @@ def ndvi(nir_agg: DataArray, red_agg: DataArray, name='ndvi'):
432433
http://ceholden.github.io/open-geo-tutorial/python/chapter_2_indices.html
433434
"""
434435

435-
validate_arrays(red_agg, nir_agg)
436+
validate_arrays(nir_agg, red_agg)
436437

437438
mapper = ArrayTypeFunctionMapping(numpy_func=_normalized_ratio_cpu,
438439
dask_func=_run_normalized_ratio_dask,
439440
cupy_func=_run_normalized_ratio_cupy,
440441
dask_cupy_func=_run_normalized_ratio_dask_cupy)
441-
442-
out = mapper(red_agg)(nir_agg.data, red_agg.data)
442+
443+
out = mapper(nir_agg)(nir_agg.data, red_agg.data)
443444

444445
return DataArray(out,
445446
name=name,
@@ -481,7 +482,7 @@ def ndmi(nir_agg: DataArray, swir1_agg: DataArray, name='ndmi'):
481482
dask_func=_run_normalized_ratio_dask,
482483
cupy_func=_run_normalized_ratio_cupy,
483484
dask_cupy_func=_run_normalized_ratio_dask_cupy)
484-
485+
485486
out = mapper(nir_agg)(nir_agg.data, swir1_agg.data)
486487

487488
return DataArray(out,
@@ -493,13 +494,12 @@ def ndmi(nir_agg: DataArray, swir1_agg: DataArray, name='ndmi'):
493494

494495
@ngjit
495496
def _normalized_ratio_cpu(arr1, arr2):
496-
out = np.zeros_like(arr1)
497+
out = np.zeros_like(arr1, dtype='f4')
497498
rows, cols = arr1.shape
498499
for y in range(0, rows):
499500
for x in range(0, cols):
500501
val1 = arr1[y, x]
501502
val2 = arr2[y, x]
502-
503503
numerator = val1 - val2
504504
denominator = val1 + val2
505505

@@ -550,7 +550,7 @@ def _run_normalized_ratio_dask_cupy(arr1, arr2):
550550

551551
@ngjit
552552
def _savi_cpu(nir_data, red_data, soil_factor):
553-
out = np.zeros_like(nir_data)
553+
out = np.zeros_like(nir_data, dtype='f4')
554554
rows, cols = nir_data.shape
555555
for y in range(0, rows):
556556
for x in range(0, cols):
@@ -563,6 +563,7 @@ def _savi_cpu(nir_data, red_data, soil_factor):
563563

564564
return out
565565

566+
566567
@cuda.jit
567568
def _savi_gpu(nir_data, red_data, soil_factor, out):
568569
y, x = cuda.grid(2)
@@ -602,7 +603,8 @@ def _savi_dask_cupy(nir_data, red_data, soil_factor):
602603

603604

604605
# SAVI -------------
605-
def savi(nir_agg: DataArray, red_agg: DataArray, soil_factor:float=1.0, name:str='savi'):
606+
def savi(nir_agg: DataArray, red_agg: DataArray,
607+
soil_factor: float = 1.0, name: str = 'savi'):
606608
"""Returns Soil Adjusted Vegetation Index (SAVI).
607609
608610
Parameters
@@ -632,14 +634,11 @@ def savi(nir_agg: DataArray, red_agg: DataArray, soil_factor:float=1.0, name:str
632634
if not -1.0 <= soil_factor <= 1.0:
633635
raise ValueError("soil factor must be between [-1.0, 1.0]")
634636

635-
nir_data = nir_agg.data
636-
red_data = red_agg.data
637-
638637
mapper = ArrayTypeFunctionMapping(numpy_func=_savi_cpu,
639638
dask_func=_savi_dask,
640639
cupy_func=_savi_cupy,
641640
dask_cupy_func=_savi_dask_cupy)
642-
641+
643642
out = mapper(red_agg)(nir_agg.data, red_agg.data, soil_factor)
644643

645644
return DataArray(out,
@@ -652,7 +651,7 @@ def savi(nir_agg: DataArray, red_agg: DataArray, soil_factor:float=1.0, name:str
652651
# SIPI -------------
653652
@ngjit
654653
def _sipi_cpu(nir_data, red_data, blue_data):
655-
out = np.zeros_like(nir_data)
654+
out = np.zeros_like(nir_data, dtype='f4')
656655
rows, cols = nir_data.shape
657656
for y in range(0, rows):
658657
for x in range(0, cols):
@@ -703,7 +702,8 @@ def _sipi_dask_cupy(nir_data, red_data, blue_data):
703702
return out
704703

705704

706-
def sipi(nir_agg: DataArray, red_agg: DataArray, blue_agg: DataArray, name='sipi'):
705+
def sipi(nir_agg: DataArray, red_agg: DataArray, blue_agg: DataArray,
706+
name='sipi'):
707707
"""Computes Structure Insensitive Pigment Index which helpful
708708
in early disease detection
709709
@@ -731,7 +731,7 @@ def sipi(nir_agg: DataArray, red_agg: DataArray, blue_agg: DataArray, name='sipi
731731
dask_func=_sipi_dask,
732732
cupy_func=_sipi_cupy,
733733
dask_cupy_func=_sipi_dask_cupy)
734-
734+
735735
out = mapper(red_agg)(nir_agg.data, red_agg.data, blue_agg.data)
736736

737737
return DataArray(out,
@@ -744,7 +744,7 @@ def sipi(nir_agg: DataArray, red_agg: DataArray, blue_agg: DataArray, name='sipi
744744
# EBBI -------------
745745
@ngjit
746746
def _ebbi_cpu(red_data, swir_data, tir_data):
747-
out = np.zeros_like(red_data)
747+
out = np.zeros_like(red_data, dtype='f4')
748748
rows, cols = red_data.shape
749749
for y in range(0, rows):
750750
for x in range(0, cols):
@@ -795,7 +795,8 @@ def _ebbi_dask_cupy(red_data, swir_data, tir_data):
795795
return out
796796

797797

798-
def ebbi(red_agg: DataArray, swir_agg: DataArray, tir_agg: DataArray, name='ebbi'):
798+
def ebbi(red_agg: DataArray, swir_agg: DataArray, tir_agg: DataArray,
799+
name='ebbi'):
799800
"""Computes Enhanced Built-Up and Bareness Index
800801
801802
Parameters
@@ -822,7 +823,7 @@ def ebbi(red_agg: DataArray, swir_agg: DataArray, tir_agg: DataArray, name='ebbi
822823
dask_func=_ebbi_dask,
823824
cupy_func=_ebbi_cupy,
824825
dask_cupy_func=_ebbi_dask_cupy)
825-
826+
826827
out = mapper(red_agg)(red_agg.data, swir_agg.data, tir_agg.data)
827828

828829
return DataArray(out,
@@ -852,9 +853,8 @@ def _normalize_data(agg, pixel_max=255.0):
852853
return out
853854

854855

855-
def bands_to_img(r, g, b, nodata=1):
856+
def true_color(r, g, b, nodata=1):
856857
h, w = r.shape
857-
r, g, b = [ds.utils.orient_array(img) for img in (r, g, b)]
858858

859859
data = np.zeros((h, w, 4), dtype=np.uint8)
860860
data[:, :, 0] = (_normalize_data(r)).astype(np.uint8)

0 commit comments

Comments
 (0)