-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy path_interpolate.py
More file actions
857 lines (755 loc) · 24.7 KB
/
_interpolate.py
File metadata and controls
857 lines (755 loc) · 24.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
"""Per-backend resampling via numba JIT (nearest/bilinear) or map_coordinates (cubic)."""
from __future__ import annotations
import math
import numpy as np
from numba import njit
try:
from numba import cuda as _cuda
_HAS_CUDA = True
except ImportError:
_HAS_CUDA = False
_RESAMPLING_ORDERS = {
'nearest': 0,
'bilinear': 1,
'cubic': 3,
}
def _validate_resampling(resampling):
if resampling not in _RESAMPLING_ORDERS:
raise ValueError(
f"resampling must be one of {list(_RESAMPLING_ORDERS)}, "
f"got {resampling!r}"
)
return _RESAMPLING_ORDERS[resampling]
# ---------------------------------------------------------------------------
# Numba kernels for nearest and bilinear resampling
# ---------------------------------------------------------------------------
@njit(nogil=True, cache=True)
def _resample_nearest_jit(src, row_coords, col_coords, nodata):
"""Nearest-neighbor resampling with NaN propagation."""
h_out, w_out = row_coords.shape
sh, sw = src.shape
out = np.empty((h_out, w_out), dtype=np.float64)
for i in range(h_out):
for j in range(w_out):
r = row_coords[i, j]
c = col_coords[i, j]
if r < -1.0 or r > sh or c < -1.0 or c > sw:
out[i, j] = nodata
continue
ri = int(r + 0.5)
ci = int(c + 0.5)
if ri < 0:
ri = 0
if ri >= sh:
ri = sh - 1
if ci < 0:
ci = 0
if ci >= sw:
ci = sw - 1
v = src[ri, ci]
# NaN check: works for float64
if v != v:
out[i, j] = nodata
else:
out[i, j] = v
return out
@njit(nogil=True, cache=True)
def _resample_cubic_jit(src, row_coords, col_coords, nodata):
"""Catmull-Rom cubic resampling with NaN-aware fallback to bilinear.
Separable: interpolate 4 row-slices along columns, then combine
along rows. When any of the 16 neighbors is NaN, falls back to
bilinear with weight renormalization (matching GDAL behavior).
"""
h_out, w_out = row_coords.shape
sh, sw = src.shape
out = np.empty((h_out, w_out), dtype=np.float64)
for i in range(h_out):
for j in range(w_out):
r = row_coords[i, j]
c = col_coords[i, j]
if r < -1.0 or r > sh or c < -1.0 or c > sw:
out[i, j] = nodata
continue
r0 = int(np.floor(r))
c0 = int(np.floor(c))
fr = r - r0
fc = c - c0
# Catmull-Rom column weights (a = -0.5)
fc2 = fc * fc
fc3 = fc2 * fc
wc0 = -0.5 * fc3 + fc2 - 0.5 * fc
wc1 = 1.5 * fc3 - 2.5 * fc2 + 1.0
wc2 = -1.5 * fc3 + 2.0 * fc2 + 0.5 * fc
wc3 = 0.5 * fc3 - 0.5 * fc2
# Catmull-Rom row weights
fr2 = fr * fr
fr3 = fr2 * fr
wr0 = -0.5 * fr3 + fr2 - 0.5 * fr
wr1 = 1.5 * fr3 - 2.5 * fr2 + 1.0
wr2 = -1.5 * fr3 + 2.0 * fr2 + 0.5 * fr
wr3 = 0.5 * fr3 - 0.5 * fr2
val = 0.0
has_nan = False
for di in range(4):
ri = r0 - 1 + di
ric = ri
if ric < 0:
ric = 0
elif ric >= sh:
ric = sh - 1
# Interpolate along columns for this row
rv = 0.0
for dj in range(4):
cj = c0 - 1 + dj
cjc = cj
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv: # NaN check
has_nan = True
break
if dj == 0:
rv = sv * wc0
elif dj == 1:
rv += sv * wc1
elif dj == 2:
rv += sv * wc2
else:
rv += sv * wc3
if has_nan:
break
if di == 0:
val = rv * wr0
elif di == 1:
val += rv * wr1
elif di == 2:
val += rv * wr2
else:
val += rv * wr3
if not has_nan:
out[i, j] = val
else:
# Fall back to bilinear with weight renormalization
r1 = r0 + 1
c1 = c0 + 1
dr = r - r0
dc = c - c0
w00 = (1.0 - dr) * (1.0 - dc)
w01 = (1.0 - dr) * dc
w10 = dr * (1.0 - dc)
w11 = dr * dc
accum = 0.0
wsum = 0.0
if 0 <= r0 < sh and 0 <= c0 < sw:
v = src[r0, c0]
if v == v:
accum += w00 * v
wsum += w00
if 0 <= r0 < sh and 0 <= c1 < sw:
v = src[r0, c1]
if v == v:
accum += w01 * v
wsum += w01
if 0 <= r1 < sh and 0 <= c0 < sw:
v = src[r1, c0]
if v == v:
accum += w10 * v
wsum += w10
if 0 <= r1 < sh and 0 <= c1 < sw:
v = src[r1, c1]
if v == v:
accum += w11 * v
wsum += w11
if wsum > 1e-10:
out[i, j] = accum / wsum
else:
out[i, j] = nodata
return out
@njit(nogil=True, cache=True)
def _resample_bilinear_jit(src, row_coords, col_coords, nodata):
"""Bilinear resampling matching GDAL's weight-renormalization approach.
When a neighbor is out-of-bounds or NaN, its weight is excluded and
the result is renormalized from the remaining valid neighbors. This
matches GDAL's GWKBilinearResample4Sample behavior.
"""
h_out, w_out = row_coords.shape
sh, sw = src.shape
out = np.empty((h_out, w_out), dtype=np.float64)
for i in range(h_out):
for j in range(w_out):
r = row_coords[i, j]
c = col_coords[i, j]
if r < -1.0 or r > sh or c < -1.0 or c > sw:
out[i, j] = nodata
continue
r0 = int(np.floor(r))
c0 = int(np.floor(c))
r1 = r0 + 1
c1 = c0 + 1
dr = r - r0
dc = c - c0
w00 = (1.0 - dr) * (1.0 - dc)
w01 = (1.0 - dr) * dc
w10 = dr * (1.0 - dc)
w11 = dr * dc
accum = 0.0
wsum = 0.0
# Accumulate only valid, in-bounds neighbors
if 0 <= r0 < sh and 0 <= c0 < sw:
v = src[r0, c0]
if v == v: # not NaN
accum += w00 * v
wsum += w00
if 0 <= r0 < sh and 0 <= c1 < sw:
v = src[r0, c1]
if v == v:
accum += w01 * v
wsum += w01
if 0 <= r1 < sh and 0 <= c0 < sw:
v = src[r1, c0]
if v == v:
accum += w10 * v
wsum += w10
if 0 <= r1 < sh and 0 <= c1 < sw:
v = src[r1, c1]
if v == v:
accum += w11 * v
wsum += w11
if wsum > 1e-10:
out[i, j] = accum / wsum
else:
out[i, j] = nodata
return out
# ---------------------------------------------------------------------------
# Public numpy resampler
# ---------------------------------------------------------------------------
def _resample_numpy(source_window, src_row_coords, src_col_coords,
resampling='bilinear', nodata=np.nan):
"""Resample a numpy source window at fractional pixel coordinates.
Uses numba JIT for nearest and bilinear (fast path), falls back to
scipy.ndimage.map_coordinates for cubic.
Parameters
----------
source_window : ndarray (H_src, W_src)
src_row_coords, src_col_coords : ndarray (H_out, W_out)
resampling : str
nodata : float
Returns
-------
ndarray (H_out, W_out)
"""
order = _validate_resampling(resampling)
is_integer = np.issubdtype(source_window.dtype, np.integer)
work = source_window.astype(np.float64) if is_integer else source_window
# Ensure float64 and contiguous for numba
if work.dtype != np.float64:
work = work.astype(np.float64)
work = np.ascontiguousarray(work)
rc = np.ascontiguousarray(src_row_coords, dtype=np.float64)
cc = np.ascontiguousarray(src_col_coords, dtype=np.float64)
nd = float(nodata)
if order == 0:
result = _resample_nearest_jit(work, rc, cc, nd)
if is_integer:
info = np.iinfo(source_window.dtype)
result = np.clip(np.round(result), info.min, info.max).astype(source_window.dtype)
return result
if order == 1:
result = _resample_bilinear_jit(work, rc, cc, nd)
if is_integer:
info = np.iinfo(source_window.dtype)
result = np.clip(np.round(result), info.min, info.max).astype(source_window.dtype)
return result
# Cubic: numba Catmull-Rom (handles NaN inline, no extra passes)
result = _resample_cubic_jit(work, rc, cc, nd)
if is_integer:
info = np.iinfo(source_window.dtype)
result = np.clip(np.round(result), info.min, info.max).astype(source_window.dtype)
return result
# ---------------------------------------------------------------------------
# CUDA resampling kernels
# ---------------------------------------------------------------------------
if _HAS_CUDA:
@_cuda.jit
def _resample_nearest_cuda(src, row_coords, col_coords, out, nodata):
"""Nearest-neighbor resampling kernel (CUDA)."""
i, j = _cuda.grid(2)
h_out = out.shape[0]
w_out = out.shape[1]
if i >= h_out or j >= w_out:
return
sh = src.shape[0]
sw = src.shape[1]
r = row_coords[i, j]
c = col_coords[i, j]
if r < -1.0 or r > sh or c < -1.0 or c > sw:
out[i, j] = nodata
return
ri = int(r + 0.5)
ci = int(c + 0.5)
if ri < 0:
ri = 0
if ri >= sh:
ri = sh - 1
if ci < 0:
ci = 0
if ci >= sw:
ci = sw - 1
v = src[ri, ci]
# NaN check
if v != v:
out[i, j] = nodata
else:
out[i, j] = v
@_cuda.jit
def _resample_bilinear_cuda(src, row_coords, col_coords, out, nodata):
"""Bilinear resampling kernel (CUDA), GDAL-matching renormalization."""
i, j = _cuda.grid(2)
h_out = out.shape[0]
w_out = out.shape[1]
if i >= h_out or j >= w_out:
return
sh = src.shape[0]
sw = src.shape[1]
r = row_coords[i, j]
c = col_coords[i, j]
if r < -1.0 or r > sh or c < -1.0 or c > sw:
out[i, j] = nodata
return
r0 = int(math.floor(r))
c0 = int(math.floor(c))
r1 = r0 + 1
c1 = c0 + 1
dr = r - r0
dc = c - c0
w00 = (1.0 - dr) * (1.0 - dc)
w01 = (1.0 - dr) * dc
w10 = dr * (1.0 - dc)
w11 = dr * dc
accum = 0.0
wsum = 0.0
if 0 <= r0 < sh and 0 <= c0 < sw:
v = src[r0, c0]
if v == v:
accum += w00 * v
wsum += w00
if 0 <= r0 < sh and 0 <= c1 < sw:
v = src[r0, c1]
if v == v:
accum += w01 * v
wsum += w01
if 0 <= r1 < sh and 0 <= c0 < sw:
v = src[r1, c0]
if v == v:
accum += w10 * v
wsum += w10
if 0 <= r1 < sh and 0 <= c1 < sw:
v = src[r1, c1]
if v == v:
accum += w11 * v
wsum += w11
if wsum > 1e-10:
out[i, j] = accum / wsum
else:
out[i, j] = nodata
@_cuda.jit
def _resample_cubic_cuda(src, row_coords, col_coords, out, nodata):
"""Catmull-Rom cubic resampling kernel (CUDA)."""
i, j = _cuda.grid(2)
h_out = out.shape[0]
w_out = out.shape[1]
if i >= h_out or j >= w_out:
return
sh = src.shape[0]
sw = src.shape[1]
r = row_coords[i, j]
c = col_coords[i, j]
if r < -1.0 or r > sh or c < -1.0 or c > sw:
out[i, j] = nodata
return
r0 = int(math.floor(r))
c0 = int(math.floor(c))
fr = r - r0
fc = c - c0
# Catmull-Rom column weights (a = -0.5)
fc2 = fc * fc
fc3 = fc2 * fc
wc0 = -0.5 * fc3 + fc2 - 0.5 * fc
wc1 = 1.5 * fc3 - 2.5 * fc2 + 1.0
wc2 = -1.5 * fc3 + 2.0 * fc2 + 0.5 * fc
wc3 = 0.5 * fc3 - 0.5 * fc2
# Catmull-Rom row weights
fr2 = fr * fr
fr3 = fr2 * fr
wr0 = -0.5 * fr3 + fr2 - 0.5 * fr
wr1 = 1.5 * fr3 - 2.5 * fr2 + 1.0
wr2 = -1.5 * fr3 + 2.0 * fr2 + 0.5 * fr
wr3 = 0.5 * fr3 - 0.5 * fr2
val = 0.0
has_nan = False
# Row 0
ric = r0 - 1
if ric < 0:
ric = 0
elif ric >= sh:
ric = sh - 1
# Unrolled column loop for row 0
cjc = c0 - 1
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv0 = sv * wc0
cjc = c0
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv0 += sv * wc1
cjc = c0 + 1
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv0 += sv * wc2
cjc = c0 + 2
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv0 += sv * wc3
val = rv0 * wr0
# Row 1
if not has_nan:
ric = r0
if ric < 0:
ric = 0
elif ric >= sh:
ric = sh - 1
cjc = c0 - 1
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv1 = sv * wc0
cjc = c0
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv1 += sv * wc1
cjc = c0 + 1
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv1 += sv * wc2
cjc = c0 + 2
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv1 += sv * wc3
val += rv1 * wr1
# Row 2
if not has_nan:
ric = r0 + 1
if ric < 0:
ric = 0
elif ric >= sh:
ric = sh - 1
cjc = c0 - 1
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv2 = sv * wc0
cjc = c0
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv2 += sv * wc1
cjc = c0 + 1
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv2 += sv * wc2
cjc = c0 + 2
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv2 += sv * wc3
val += rv2 * wr2
# Row 3
if not has_nan:
ric = r0 + 2
if ric < 0:
ric = 0
elif ric >= sh:
ric = sh - 1
cjc = c0 - 1
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv3 = sv * wc0
cjc = c0
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv3 += sv * wc1
cjc = c0 + 1
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv3 += sv * wc2
cjc = c0 + 2
if cjc < 0:
cjc = 0
elif cjc >= sw:
cjc = sw - 1
sv = src[ric, cjc]
if sv != sv:
has_nan = True
if not has_nan:
rv3 += sv * wc3
val += rv3 * wr3
if not has_nan:
out[i, j] = val
else:
# Fall back to bilinear with weight renormalization
r1b = r0 + 1
c1b = c0 + 1
dr = r - r0
dc = c - c0
w00 = (1.0 - dr) * (1.0 - dc)
w01 = (1.0 - dr) * dc
w10 = dr * (1.0 - dc)
w11 = dr * dc
accum = 0.0
wsum = 0.0
if 0 <= r0 < sh and 0 <= c0 < sw:
v = src[r0, c0]
if v == v:
accum += w00 * v
wsum += w00
if 0 <= r0 < sh and 0 <= c1b < sw:
v = src[r0, c1b]
if v == v:
accum += w01 * v
wsum += w01
if 0 <= r1b < sh and 0 <= c0 < sw:
v = src[r1b, c0]
if v == v:
accum += w10 * v
wsum += w10
if 0 <= r1b < sh and 0 <= c1b < sw:
v = src[r1b, c1b]
if v == v:
accum += w11 * v
wsum += w11
if wsum > 1e-10:
out[i, j] = accum / wsum
else:
out[i, j] = nodata
# ---------------------------------------------------------------------------
# Native CuPy resampler using CUDA kernels
# ---------------------------------------------------------------------------
def _resample_cupy_native(source_window, src_row_coords, src_col_coords,
resampling='bilinear', nodata=np.nan):
"""Resample using custom CUDA kernels (all data stays on GPU).
Unlike ``_resample_cupy`` which uses ``cupyx.scipy.ndimage.map_coordinates``,
this function uses hand-written CUDA kernels that match the Numba CPU
kernels exactly, including inline NaN handling.
Parameters
----------
source_window : cupy.ndarray (H_src, W_src)
src_row_coords, src_col_coords : cupy.ndarray (H_out, W_out)
resampling : str
nodata : float
Returns
-------
cupy.ndarray (H_out, W_out)
"""
if not _HAS_CUDA:
raise RuntimeError("numba.cuda is required for _resample_cupy_native")
import cupy as cp
order = _validate_resampling(resampling)
is_integer = cp.issubdtype(source_window.dtype, cp.integer)
if is_integer:
work = source_window.astype(cp.float64)
else:
work = source_window
if work.dtype != cp.float64:
work = work.astype(cp.float64)
# Ensure inputs are CuPy arrays
if not isinstance(src_row_coords, cp.ndarray):
src_row_coords = cp.asarray(src_row_coords)
if not isinstance(src_col_coords, cp.ndarray):
src_col_coords = cp.asarray(src_col_coords)
rc = cp.ascontiguousarray(src_row_coords, dtype=cp.float64)
cc = cp.ascontiguousarray(src_col_coords, dtype=cp.float64)
# Convert sentinel nodata to NaN so kernels can detect it
if not np.isnan(nodata):
work = work.copy()
work[work == nodata] = cp.nan
h_out, w_out = rc.shape
out = cp.empty((h_out, w_out), dtype=cp.float64)
nd = float(nodata)
# Launch configuration: (16, 16) thread blocks
threads_per_block = (16, 16)
blocks_per_grid = (
(h_out + threads_per_block[0] - 1) // threads_per_block[0],
(w_out + threads_per_block[1] - 1) // threads_per_block[1],
)
if order == 0:
_resample_nearest_cuda[blocks_per_grid, threads_per_block](
work, rc, cc, out, nd
)
if is_integer:
info = cp.iinfo(source_window.dtype)
out = cp.clip(cp.round(out), info.min, info.max).astype(
source_window.dtype
)
return out
if order == 1:
_resample_bilinear_cuda[blocks_per_grid, threads_per_block](
work, rc, cc, out, nd
)
if is_integer:
info = cp.iinfo(source_window.dtype)
out = cp.clip(cp.round(out), info.min, info.max).astype(
source_window.dtype
)
return out
# Cubic
_resample_cubic_cuda[blocks_per_grid, threads_per_block](
work, rc, cc, out, nd
)
if is_integer:
info = cp.iinfo(source_window.dtype)
out = cp.clip(cp.round(out), info.min, info.max).astype(
source_window.dtype
)
return out
# ---------------------------------------------------------------------------
# CuPy resampler (uses cupyx.scipy.ndimage.map_coordinates)
# ---------------------------------------------------------------------------
def _resample_cupy(source_window, src_row_coords, src_col_coords,
resampling='bilinear', nodata=np.nan):
"""CuPy equivalent of ``_resample_numpy``.
Control grid is on CPU (pyproj is CPU-only); coordinates are
transferred to GPU for interpolation.
"""
import cupy as cp
from cupyx.scipy.ndimage import map_coordinates
order = _validate_resampling(resampling)
is_integer = cp.issubdtype(source_window.dtype, cp.integer)
if is_integer:
work = source_window.astype(cp.float64)
else:
work = source_window
# Transfer coordinate arrays to GPU
if not isinstance(src_row_coords, cp.ndarray):
src_row_coords = cp.asarray(src_row_coords)
if not isinstance(src_col_coords, cp.ndarray):
src_col_coords = cp.asarray(src_col_coords)
if cp.issubdtype(work.dtype, cp.floating):
nan_mask = cp.isnan(work)
has_nan = bool(nan_mask.any())
else:
nan_mask = None
has_nan = False
if has_nan:
if not is_integer:
work = work.copy()
work[nan_mask] = 0.0
coords = cp.array([src_row_coords.ravel(), src_col_coords.ravel()])
result = map_coordinates(
work, coords, order=order, mode='constant', cval=0.0
).reshape(src_row_coords.shape)
h, w = source_window.shape
oob = (
(src_row_coords < -1.0) | (src_row_coords > h) |
(src_col_coords < -1.0) | (src_col_coords > w)
)
if has_nan:
nan_weight = map_coordinates(
nan_mask.astype(cp.float64), coords,
order=order, mode='constant', cval=1.0
).reshape(src_row_coords.shape)
oob = oob | (nan_weight > 0.1)
result[oob] = nodata
if is_integer:
info = cp.iinfo(source_window.dtype)
result = cp.clip(cp.round(result), info.min, info.max).astype(
source_window.dtype
)
return result