From 4f0e1d818dc26d3fec6f0b606df854e74c000f77 Mon Sep 17 00:00:00 2001 From: thuydotm Date: Thu, 19 Jan 2023 15:11:33 +0700 Subject: [PATCH 1/3] gpu_bin: set out to cupy.nan if no bin found --- xrspatial/classify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xrspatial/classify.py b/xrspatial/classify.py index ec18245d..7253beab 100644 --- a/xrspatial/classify.py +++ b/xrspatial/classify.py @@ -209,7 +209,7 @@ def _gpu_bin(data, bins, new_values): if val_bin > -1: out = new_values[val_bin] else: - out = np.nan + out = cupy.nan return out From ff52a02c6ddcd32c76411fa7d92b7d86cb572544 Mon Sep 17 00:00:00 2001 From: thuydotm Date: Thu, 19 Jan 2023 15:26:51 +0700 Subject: [PATCH 2/3] binary search gpu_bin --- xrspatial/classify.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/xrspatial/classify.py b/xrspatial/classify.py index 7253beab..240da4fa 100644 --- a/xrspatial/classify.py +++ b/xrspatial/classify.py @@ -194,17 +194,23 @@ def _gpu_bin(data, bins, new_values): val_bin = -1 # find bin - for b in range(0, nbins): - - # first bin - if b == 0: - if val <= bins[b]: - val_bin = b - break - else: - if val > bins[b - 1] and val <= bins[b]: - val_bin = b - break + if cupy.isfinite(val): + if val <= bins[0]: + val_bin = 0 + elif val <= bins[nbins - 1]: + start = 0 + end = nbins - 1 + mid = (end + start) // 2 + while start <= end: + if bins[mid] < val: + start = mid + 1 + elif val > bins[mid - 1]: + break + else: + end = mid - 1 + mid = (end + start) // 2 + + val_bin = mid if val_bin > -1: out = new_values[val_bin] From b6c683b5d532d559e807b1c9f952257df34c5276 Mon Sep 17 00:00:00 2001 From: thuydotm Date: Thu, 19 Jan 2023 15:40:23 +0700 Subject: [PATCH 3/3] ignore checking for nan --- xrspatial/classify.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/xrspatial/classify.py b/xrspatial/classify.py index 240da4fa..ce2cd29b 100644 --- a/xrspatial/classify.py +++ b/xrspatial/classify.py @@ -194,23 +194,22 @@ def _gpu_bin(data, bins, new_values): val_bin = -1 # find bin - if cupy.isfinite(val): - if val <= bins[0]: - val_bin = 0 - elif val <= bins[nbins - 1]: - start = 0 - end = nbins - 1 + if val <= bins[0]: + val_bin = 0 + elif val <= bins[nbins - 1]: + start = 0 + end = nbins - 1 + mid = (end + start) // 2 + while start <= end: + if bins[mid] < val: + start = mid + 1 + elif val > bins[mid - 1]: + break + else: + end = mid - 1 mid = (end + start) // 2 - while start <= end: - if bins[mid] < val: - start = mid + 1 - elif val > bins[mid - 1]: - break - else: - end = mid - 1 - mid = (end + start) // 2 - - val_bin = mid + + val_bin = mid if val_bin > -1: out = new_values[val_bin]