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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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
from __future__ import annotations
import copy
from enum import Enum
from packaging.version import Version
import numpy as np
from datashader.datashape import dshape, isnumeric, Record, Option
from datashader.datashape import coretypes as ct
from toolz import concat, unique
import xarray as xr

from datashader.antialias import AntialiasCombination, AntialiasStage2
from datashader.utils import isminus1, isnull
from numba import cuda as nb_cuda

try:
    from datashader.transfer_functions._cuda_utils import (
        cuda_atomic_nanmin, cuda_atomic_nanmax, cuda_args, cuda_row_min_in_place,
        cuda_nanmax_n_in_place_4d, cuda_nanmax_n_in_place_3d,
        cuda_nanmin_n_in_place_4d, cuda_nanmin_n_in_place_3d,
        cuda_row_max_n_in_place_4d, cuda_row_max_n_in_place_3d,
        cuda_row_min_n_in_place_4d, cuda_row_min_n_in_place_3d, cuda_shift_and_insert,
    )
except ImportError:
    (cuda_atomic_nanmin, cuda_atomic_nanmax, cuda_args, cuda_row_min_in_place,
        cuda_nanmax_n_in_place_4d, cuda_nanmax_n_in_place_3d,
        cuda_nanmin_n_in_place_4d, cuda_nanmin_n_in_place_3d,
        cuda_row_max_n_in_place_4d, cuda_row_max_n_in_place_3d,
        cuda_row_min_n_in_place_4d, cuda_row_min_n_in_place_3d, cuda_shift_and_insert,
    ) = None, None, None, None, None, None, None, None, None, None, None, None, None

try:
    import cudf
    import cupy as cp
except Exception:
    cudf = cp = None

from .utils import (
    Expr, ngjit, nansum_missing, nanmax_in_place, nansum_in_place, row_min_in_place,
    nanmax_n_in_place_4d, nanmax_n_in_place_3d, nanmin_n_in_place_4d, nanmin_n_in_place_3d,
    row_max_n_in_place_4d, row_max_n_in_place_3d, row_min_n_in_place_4d, row_min_n_in_place_3d,
    shift_and_insert,
)


class SpecialColumn(Enum):
    """
    Internally datashader identifies the columns required by the user's
    Reductions and extracts them from the supplied source (e.g. DataFrame) to
    pass through the dynamically-generated append function in compiler.py and
    end up as arguments to the Reduction._append* functions. Each column is
    a string name or a SpecialColumn. A column of None is used in Reduction
    classes to denote that no column is required.
    """
    RowIndex = 1


class UsesCudaMutex(Enum):
    """
    Enum that encapsulates the need for a Reduction to use a CUDA mutex to
    operate correctly on a GPU. Possible values:

    No: the Reduction append_cuda function is atomic and no mutex is required.
    Local: Reduction append_cuda needs wrapping in a mutex.
    Global: the overall compiled append function needs wrapping in a mutex.
    """
    No = 0
    Local = 1
    Global = 2


class Preprocess(Expr):
    """Base clase for preprocessing steps."""
    def __init__(self, column: str | SpecialColumn | None):
        self.column = column

    @property
    def inputs(self):
        return (self.column,)

    @property
    def nan_check_column(self):
        return None


class extract(Preprocess):
    """Extract a column from a dataframe as a numpy array of values."""
    def apply(self, df, cuda):
        if self.column is SpecialColumn.RowIndex:
            attr_name = "_datashader_row_offset"
            if isinstance(df, xr.Dataset):
                row_offset = df.attrs[attr_name]
                row_length = df.attrs["_datashader_row_length"]
            else:
                attrs = getattr(df, "attrs", None)
                row_offset = getattr(attrs or df, attr_name, 0)
                row_length = len(df)

        if cudf and isinstance(df, cudf.DataFrame):
            if self.column is SpecialColumn.RowIndex:
                return cp.arange(row_offset, row_offset + row_length, dtype=np.int64)

            if df[self.column].dtype.kind == 'f':
                nullval = np.nan
            else:
                nullval = 0
            if Version(cudf.__version__) >= Version("22.02"):
                return df[self.column].to_cupy(na_value=nullval)
            return cp.array(df[self.column].to_gpu_array(fillna=nullval))
        elif self.column is SpecialColumn.RowIndex:
            if cuda:
                return cp.arange(row_offset, row_offset + row_length, dtype=np.int64)
            else:
                return np.arange(row_offset, row_offset + row_length, dtype=np.int64)
        elif isinstance(df, xr.Dataset):
            if cuda and not isinstance(df[self.column].data, cp.ndarray):
                return cp.asarray(df[self.column])
            else:
                return df[self.column].data
        else:
            return df[self.column].values


class CategoryPreprocess(Preprocess):
    """Base class for categorizing preprocessors."""
    @property
    def cat_column(self):
        """Returns name of categorized column"""
        return self.column

    def categories(self, input_dshape):
        """Returns list of categories corresponding to input shape"""
        raise NotImplementedError("categories not implemented")

    def validate(self, in_dshape):
        """Validates input shape"""
        raise NotImplementedError("validate not implemented")

    def apply(self, df, cuda):
        """Applies preprocessor to DataFrame and returns array"""
        raise NotImplementedError("apply not implemented")


class category_codes(CategoryPreprocess):
    """
    Extract just the category codes from a categorical column.

    To create a new type of categorizer, derive a subclass from this
    class or one of its subclasses, implementing ``__init__``,
    ``_hashable_inputs``, ``categories``, ``validate``, and ``apply``.

    See the implementation of ``category_modulo`` in ``reductions.py``
    for an example.
    """
    def categories(self, input_dshape):
        return input_dshape.measure[self.column].categories

    def validate(self, in_dshape):
        if self.column not in in_dshape.dict:
            raise ValueError("specified column not found")
        if not isinstance(in_dshape.measure[self.column], ct.Categorical):
            raise ValueError("input must be categorical")

    def apply(self, df, cuda):
        if cudf and isinstance(df, cudf.DataFrame):
            if Version(cudf.__version__) >= Version("22.02"):
                return df[self.column].cat.codes.to_cupy()
            return df[self.column].cat.codes.to_gpu_array()
        else:
            return df[self.column].cat.codes.values

class category_modulo(category_codes):
    """
    A variation on category_codes that assigns categories using an integer column, modulo a base.
    Category is computed as (column_value - offset)%modulo.
    """

    # couldn't find anything in the datashape docs about how to check if a CType is an integer, so
    # just define a big set
    IntegerTypes = {ct.bool_, ct.uint8, ct.uint16, ct.uint32, ct.uint64, ct.int8, ct.int16,
                    ct.int32, ct.int64}

    def __init__(self, column, modulo, offset=0):
        super().__init__(column)
        self.offset = offset
        self.modulo = modulo

    def _hashable_inputs(self):
        return super()._hashable_inputs() + (self.offset, self.modulo)

    def categories(self, in_dshape):
        return list(range(self.modulo))

    def validate(self, in_dshape):
        if self.column not in in_dshape.dict:
            raise ValueError("specified column not found")
        if in_dshape.measure[self.column] not in self.IntegerTypes:
            raise ValueError("input must be an integer column")

    def apply(self, df, cuda):
        result = (df[self.column] - self.offset) % self.modulo
        if cudf and isinstance(df, cudf.Series):
            if Version(cudf.__version__) >= Version("22.02"):
                return result.to_cupy()
            return result.to_gpu_array()
        else:
            return result.values

class category_binning(category_modulo):
    """
    A variation on category_codes that assigns categories by binning a continuous-valued column.
    The number of categories returned is always nbins+1.
    The last category (nbin) is for NaNs in the data column, as well as for values under/over the
    binned interval (when include_under or include_over is False).

    Parameters
    ----------
    column:   column to use
    lower:    lower bound of first bin
    upper:    upper bound of last bin
    nbins:     number of bins
    include_under: if True, values below bin 0 are assigned to category 0
    include_over:  if True, values above the last bin (nbins-1) are assigned to category nbin-1
    """

    def __init__(self, column, lower, upper, nbins, include_under=True, include_over=True):
        super().__init__(column, nbins + 1)  # +1 category for NaNs and clipped values
        self.bin0 = lower
        self.binsize = (upper - lower) / float(nbins)
        self.nbins = nbins
        self.bin_under = 0 if include_under else nbins
        self.bin_over  = nbins-1 if include_over else nbins

    def _hashable_inputs(self):
        return super()._hashable_inputs() + (self.bin0, self.binsize, self.bin_under, self.bin_over)

    def validate(self, in_dshape):
        if self.column not in in_dshape.dict:
            raise ValueError("specified column not found")

    def apply(self, df, cuda):
        if cudf and isinstance(df, cudf.DataFrame):
            if Version(cudf.__version__) >= Version("22.02"):
                values = df[self.column].to_cupy(na_value=cp.nan)
            else:
                values = cp.array(df[self.column].to_gpu_array(fillna=True))
            nan_values = cp.isnan(values)
        else:
            values = df[self.column].to_numpy()
            nan_values = np.isnan(values)

        index_float = (values - self.bin0) / self.binsize
        # NaN values are corrected below, so set them to zero to avoid warnings when
        # converting from float to int.
        index_float[nan_values] = 0
        index = index_float.astype(int)
        index[index < 0] = self.bin_under
        index[index >= self.nbins] = self.bin_over
        index[nan_values] = self.nbins
        return index


class category_values(CategoryPreprocess):
    """Extract a category and a value column from a dataframe as (2,N) numpy array of values."""
    def __init__(self, categorizer, value_column):
        super().__init__(value_column)
        self.categorizer = categorizer

    @property
    def inputs(self):
        return (self.categorizer.column, self.column)

    @property
    def cat_column(self):
        """Returns name of categorized column"""
        return self.categorizer.column

    def categories(self, input_dshape):
        return self.categorizer.categories

    def validate(self, in_dshape):
        return self.categorizer.validate(in_dshape)

    def apply(self, df, cuda):
        a = self.categorizer.apply(df, cuda)
        if cudf and isinstance(df, cudf.DataFrame):
            import cupy
            if self.column == SpecialColumn.RowIndex:
                nullval = -1
            elif df[self.column].dtype.kind == 'f':
                nullval = np.nan
            else:
                nullval = 0
            a = cupy.asarray(a)
            if self.column == SpecialColumn.RowIndex:
                b = extract(SpecialColumn.RowIndex).apply(df, cuda)
            elif Version(cudf.__version__) >= Version("22.02"):
                b = df[self.column].to_cupy(na_value=nullval)
            else:
                b = cupy.asarray(df[self.column].fillna(nullval))
            return cupy.stack((a, b), axis=-1)
        else:
            if self.column == SpecialColumn.RowIndex:
                b = extract(SpecialColumn.RowIndex).apply(df, cuda)
            else:
                b = df[self.column].values
            return np.stack((a, b), axis=-1)


class Reduction(Expr):
    """Base class for per-bin reductions."""
    def __init__(self, column: str | SpecialColumn | None=None):
        self.column = column
        self._nan_check_column = None

    @property
    def nan_check_column(self):
        if self._nan_check_column is not None:
            return extract(self._nan_check_column)
        else:
            return None

    def uses_cuda_mutex(self) -> UsesCudaMutex:
        """Return ``True`` if this Reduction needs to use a CUDA mutex to
        ensure that it is threadsafe across CUDA threads.

        If the CUDA append functions are all atomic (i.e. using functions from
        the numba.cuda.atomic module) then this is ``False``, otherwise it is
        ``True``.
        """
        return UsesCudaMutex.No

    def uses_row_index(self, cuda, partitioned):
        """Return ``True`` if this Reduction uses a row index virtual column.

        For some reductions the order of the rows of supplied data is
        important. These include ``first`` and ``last`` reductions as well as
        ``where`` reductions that return a row index. In some situations the
        order is intrinsic such as ``first`` reductions that are processed
        sequentially (i.e. on a CPU without using Dask) and no extra column is
        required. But in situations of parallel processing (using a GPU or
        Dask) extra information is needed that is provided by a row index
        virtual column.

        Returning ``True`` from this function will cause a row index column to
        be created and passed to the ``append`` functions in the usual manner.
        """
        return False

    def validate(self, in_dshape):
        if self.column == SpecialColumn.RowIndex:
            return
        if self.column not in in_dshape.dict:
            raise ValueError("specified column not found")
        if not isnumeric(in_dshape.measure[self.column]):
            raise ValueError("input must be numeric")

    @property
    def inputs(self):
        return (extract(self.column),)

    def is_categorical(self):
        """Return ``True`` if this is or contains a categorical reduction."""
        return False

    def is_where(self):
        """Return ``True`` if this is a ``where`` reduction or directly wraps
        a where reduction."""
        return False

    def _antialias_requires_2_stages(self):
        # Return True if this Reduction must be processed with 2 stages,
        # False if it doesn't matter.
        # Overridden in derived classes as appropriate.
        return False

    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        # Only called if using antialiased lines. Overridden in derived classes.
        # Returns a tuple containing an item for each constituent reduction.
        # Each item is (AntialiasCombination, zero_value)).
        raise NotImplementedError(f"{type(self)}._antialias_stage_2 is not defined")

    def _build_bases(self, cuda, partitioned):
        return (self,)

    def _build_combine_temps(self, cuda, partitioned):
        # Temporaries (i.e. not returned to user) that are reductions, the
        # aggs of which are passed to the combine() function but not the
        # append() functions, as opposed to _build_temps() which are passed
        # to both append() and combine().
        return ()

    def _build_temps(self, cuda=False):
        # Temporaries (i.e. not returned to user) that are reductions, the
        # aggs of which are passed to both append() and combine() functions.
        return ()

    def _build_create(self, required_dshape):
        fields = getattr(required_dshape.measure, "fields", None)
        if fields is not None and len(required_dshape.measure.fields) > 0:
            # If more than one field then they all have the same dtype so can just take the first.
            first_field = required_dshape.measure.fields[0]
            required_dshape = dshape(first_field[1])

        if isinstance(required_dshape, Option):
            required_dshape = dshape(required_dshape.ty)

        if required_dshape == dshape(ct.bool_):
            return self._create_bool
        elif required_dshape == dshape(ct.float32):
            return self._create_float32_nan
        elif required_dshape == dshape(ct.float64):
            return self._create_float64_nan
        elif required_dshape == dshape(ct.int64):
            return self._create_int64
        elif required_dshape == dshape(ct.uint32):
            return self._create_uint32
        else:
            raise NotImplementedError(f"Unexpected dshape {dshape}")

    def _build_append(self, dshape, schema, cuda, antialias, self_intersect):
        if cuda:
            if antialias and self.column is None:
                return self._append_no_field_antialias_cuda
            elif antialias:
                return self._append_antialias_cuda
            elif self.column is None:
                return self._append_no_field_cuda
            else:
                return self._append_cuda
        else:
            if antialias and self.column is None:
                return self._append_no_field_antialias
            elif antialias:
                return self._append_antialias
            elif self.column is None:
                return self._append_no_field
            else:
                return self._append

    def _build_combine(self, dshape, antialias, cuda, partitioned, categorical = False):
        return self._combine

    def _build_finalize(self, dshape):
        return self._finalize

    @staticmethod
    def _create_bool(shape, array_module):
        return array_module.zeros(shape, dtype='bool')

    @staticmethod
    def _create_float32_nan(shape, array_module):
        return array_module.full(shape, array_module.nan, dtype='f4')

    @staticmethod
    def _create_float64_nan(shape, array_module):
        return array_module.full(shape, array_module.nan, dtype='f8')

    @staticmethod
    def _create_float64_empty(shape, array_module):
        return array_module.empty(shape, dtype='f8')

    @staticmethod
    def _create_float64_zero(shape, array_module):
        return array_module.zeros(shape, dtype='f8')

    @staticmethod
    def _create_int64(shape, array_module):
        return array_module.full(shape, -1, dtype='i8')

    @staticmethod
    def _create_uint32(shape, array_module):
        return array_module.zeros(shape, dtype='u4')


class OptionalFieldReduction(Reduction):
    """Base class for things like ``count`` or ``any`` for which the field is optional"""
    def __init__(self, column=None):
        super().__init__(column)

    @property
    def inputs(self):
        return (extract(self.column),) if self.column is not None else ()

    def validate(self, in_dshape):
        if self.column is not None:
            super().validate(in_dshape)

    @staticmethod
    def _finalize(bases, cuda=False, **kwargs):
        return xr.DataArray(bases[0], **kwargs)


class SelfIntersectingOptionalFieldReduction(OptionalFieldReduction):
    """
    Base class for optional field reductions for which self-intersecting
    geometry may or may not be desirable.
    Ignored if not using antialiasing.
    """
    def __init__(self, column=None, self_intersect=True):
        super().__init__(column)
        self.self_intersect = self_intersect

    def _antialias_requires_2_stages(self):
        return not self.self_intersect

    def _build_append(self, dshape, schema, cuda, antialias, self_intersect):
        if antialias and not self_intersect:
            # append functions specific to antialiased lines without self_intersect
            if cuda:
                if self.column is None:
                    return self._append_no_field_antialias_cuda_not_self_intersect
                else:
                    return self._append_antialias_cuda_not_self_intersect
            else:
                if self.column is None:
                    return self._append_no_field_antialias_not_self_intersect
                else:
                    return self._append_antialias_not_self_intersect

        # Fall back to base class implementation
        return super()._build_append(dshape, schema, cuda, antialias, self_intersect)

    def _hashable_inputs(self):
        # Reductions with different self_intersect attributes much have different hashes otherwise
        # toolz.memoize will treat them as the same to give incorrect results.
        return super()._hashable_inputs() + (self.self_intersect,)


class count(SelfIntersectingOptionalFieldReduction):
    """Count elements in each bin, returning the result as a uint32, or a
    float32 if using antialiasing.

    Parameters
    ----------
    column : str, optional
        If provided, only counts elements in ``column`` that are not ``NaN``.
        Otherwise, counts every element.
    """
    def out_dshape(self, in_dshape, antialias, cuda, partitioned):
        return dshape(ct.float32) if antialias else dshape(ct.uint32)

    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        if self_intersect:
            return (AntialiasStage2(AntialiasCombination.SUM_1AGG, array_module.nan),)
        else:
            return (AntialiasStage2(AntialiasCombination.SUM_2AGG, array_module.nan),)

    # CPU append functions
    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        if not isnull(field):
            agg[y, x] += 1
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        if not isnull(field):
            if isnull(agg[y, x]):
                agg[y, x] = aa_factor - prev_aa_factor
            else:
                agg[y, x] += aa_factor - prev_aa_factor
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias_not_self_intersect(x, y, agg, field, aa_factor, prev_aa_factor):
        if not isnull(field):
            if isnull(agg[y, x]) or aa_factor > agg[y, x]:
                agg[y, x] = aa_factor
                return 0
        return -1

    @staticmethod
    @ngjit
    def _append_no_field(x, y, agg):
        agg[y, x] += 1
        return 0

    @staticmethod
    @ngjit
    def _append_no_field_antialias(x, y, agg, aa_factor, prev_aa_factor):
        if isnull(agg[y, x]):
            agg[y, x] = aa_factor - prev_aa_factor
        else:
            agg[y, x] += aa_factor - prev_aa_factor
        return 0

    @staticmethod
    @ngjit
    def _append_no_field_antialias_not_self_intersect(x, y, agg, aa_factor, prev_aa_factor):
        if isnull(agg[y, x]) or aa_factor > agg[y, x]:
            agg[y, x] = aa_factor
            return 0
        return -1

    # GPU append functions
    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_antialias_cuda(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*aa_factor
        if not isnull(value):
            old = cuda_atomic_nanmax(agg, (y, x), value)
            if isnull(old) or old < value:
                return 0
        return -1

    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_no_field_antialias_cuda_not_self_intersect(x, y, agg, aa_factor, prev_aa_factor):
        if not isnull(aa_factor):
            old = cuda_atomic_nanmax(agg, (y, x), aa_factor)
            if isnull(old) or old < aa_factor:
                return 0
        return -1

    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_cuda(x, y, agg, field):
        if not isnull(field):
            nb_cuda.atomic.add(agg, (y, x), 1)
            return 0
        return -1

    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_no_field_antialias_cuda(x, y, agg, aa_factor, prev_aa_factor):
        if not isnull(aa_factor):
            old = cuda_atomic_nanmax(agg, (y, x), aa_factor)
            if isnull(old) or old < aa_factor:
                return 0
        return -1

    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_no_field_cuda(x, y, agg):
        nb_cuda.atomic.add(agg, (y, x), 1)
        return 0

    def _build_combine(self, dshape, antialias, cuda, partitioned, categorical = False):
        if antialias:
            return self._combine_antialias
        else:
            return self._combine

    @staticmethod
    def _combine(aggs):
        return aggs.sum(axis=0, dtype='u4')

    @staticmethod
    def _combine_antialias(aggs):
        ret = aggs[0]
        for i in range(1, len(aggs)):
            nansum_in_place(ret, aggs[i])
        return ret


class _count_ignore_antialiasing(count):
    """Count reduction but ignores antialiasing. Used by mean reduction.
    """
    def out_dshape(self, in_dshape, antialias, cuda, partitioned):
        return dshape(ct.uint32)

    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        if self_intersect:
            return (AntialiasStage2(AntialiasCombination.SUM_1AGG, 0),)
        else:
            return (AntialiasStage2(AntialiasCombination.SUM_2AGG, 0),)

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        if not isnull(field) and prev_aa_factor == 0.0:
            agg[y, x] += 1
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias_not_self_intersect(x, y, agg, field, aa_factor, prev_aa_factor):
        if not isnull(field) and prev_aa_factor == 0.0:
            agg[y, x] += 1
            return 0
        return -1


class by(Reduction):
    """Apply the provided reduction separately per category.

    Parameters
    ----------
    cats: str or CategoryPreprocess instance
        Name of column to aggregate over, or a categorizer object that returns categories.
        Resulting aggregate has an outer dimension axis along the categories present.
    reduction : Reduction
        Per-category reduction function.
    """
    def __init__(self, cat_column, reduction=count()):
        super().__init__()

        # set basic categorizer
        if isinstance(cat_column, CategoryPreprocess):
            self.categorizer = cat_column
        elif isinstance(cat_column, str):
            self.categorizer = category_codes(cat_column)
        else:
            raise TypeError("first argument must be a column name or a CategoryPreprocess instance")

        self.column = self.categorizer.column # for backwards compatibility with count_cat

        self.columns = (self.categorizer.column,)
        if (columns := getattr(reduction, 'columns', None)) is not None:
            # Must reverse columns (from where reduction) so that val_column property
            # is the column that is returned to the user.
            self.columns += columns[::-1]
        else:
            self.columns += (getattr(reduction, 'column', None),)

        self.reduction = reduction
        # if a value column is supplied, set category_values preprocessor
        if self.val_column is not None:
            self.preprocess = category_values(self.categorizer, self.val_column)
        else:
            self.preprocess = self.categorizer

    def __hash__(self):
        return hash((type(self), self._hashable_inputs(), self.categorizer._hashable_inputs(),
                     self.reduction))

    def _build_temps(self, cuda=False):
        return tuple(by(self.categorizer, tmp) for tmp in self.reduction._build_temps(cuda))

    @property
    def cat_column(self):
        return self.columns[0]

    @property
    def val_column(self):
        return self.columns[1]

    def validate(self, in_dshape):
        self.preprocess.validate(in_dshape)
        self.reduction.validate(in_dshape)

    def out_dshape(self, input_dshape, antialias, cuda, partitioned):
        cats = self.categorizer.categories(input_dshape)
        red_shape = self.reduction.out_dshape(input_dshape, antialias, cuda, partitioned)
        return dshape(Record([(c, red_shape) for c in cats]))

    @property
    def inputs(self):
        return (self.preprocess,)

    def is_categorical(self):
        return True

    def is_where(self):
        return self.reduction.is_where()

    @property
    def nan_check_column(self):
        return self.reduction.nan_check_column

    def uses_cuda_mutex(self) -> UsesCudaMutex:
        return self.reduction.uses_cuda_mutex()

    def uses_row_index(self, cuda, partitioned):
        return self.reduction.uses_row_index(cuda, partitioned)

    def _antialias_requires_2_stages(self):
        return self.reduction._antialias_requires_2_stages()

    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        ret = self.reduction._antialias_stage_2(self_intersect, array_module)
        return (AntialiasStage2(combination=ret[0].combination,
                                zero=ret[0].zero,
                                n_reduction=ret[0].n_reduction,
                                categorical=True),)

    def _build_create(self, required_dshape):
        n_cats = len(required_dshape.measure.fields)
        return lambda shape, array_module: self.reduction._build_create(
            required_dshape)(shape + (n_cats,), array_module)

    def _build_bases(self, cuda, partitioned):
        bases = self.reduction._build_bases(cuda, partitioned)
        if len(bases) == 1 and bases[0] is self:
            return bases
        return tuple(by(self.categorizer, base) for base in bases)

    def _build_append(self, dshape, schema, cuda, antialias, self_intersect):
        return self.reduction._build_append(dshape, schema, cuda, antialias, self_intersect)

    def _build_combine(self, dshape, antialias, cuda, partitioned, categorical = False):
        return self.reduction._build_combine(dshape, antialias, cuda, partitioned, True)

    def _build_combine_temps(self, cuda, partitioned):
        return self.reduction._build_combine_temps(cuda, partitioned)

    def _build_finalize(self, dshape):
        cats = list(self.categorizer.categories(dshape))

        def finalize(bases, cuda=False, **kwargs):
            # Return a modified copy of kwargs. Cannot modify supplied kwargs as it
            # may be used by multiple reductions, e.g. if a summary reduction.
            kwargs = copy.deepcopy(kwargs)
            kwargs['dims'] += [self.cat_column]
            kwargs['coords'][self.cat_column] = cats
            return self.reduction._build_finalize(dshape)(bases, cuda=cuda, **kwargs)

        return finalize

class any(OptionalFieldReduction):
    """Whether any elements in ``column`` map to each bin.

    Parameters
    ----------
    column : str, optional
        If provided, any elements in ``column`` that are ``NaN`` are skipped.
    """
    def out_dshape(self, in_dshape, antialias, cuda, partitioned):
        return dshape(ct.float32) if antialias else dshape(ct.bool_)

    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        return (AntialiasStage2(AntialiasCombination.MAX, array_module.nan),)

    # CPU append functions
    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        if not isnull(field):
            agg[y, x] = True
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        if not isnull(field):
            if isnull(agg[y, x]) or aa_factor > agg[y, x]:
                agg[y, x] = aa_factor
                return 0
        return -1

    @staticmethod
    @ngjit
    def _append_no_field(x, y, agg):
        agg[y, x] = True
        return 0

    @staticmethod
    @ngjit
    def _append_no_field_antialias(x, y, agg, aa_factor, prev_aa_factor):
        if isnull(agg[y, x]) or aa_factor > agg[y, x]:
            agg[y, x] = aa_factor
            return 0
        return -1

    # GPU append functions
    _append_cuda =_append
    _append_no_field_cuda = _append_no_field

    def _build_combine(self, dshape, antialias, cuda, partitioned, categorical = False):
        if antialias:
            return self._combine_antialias
        else:
            return self._combine

    @staticmethod
    def _combine(aggs):
        return aggs.sum(axis=0, dtype='bool')

    @staticmethod
    def _combine_antialias(aggs):
        ret = aggs[0]
        for i in range(1, len(aggs)):
            nanmax_in_place(ret, aggs[i])
        return ret


class _upsample(Reduction):
    """"Special internal class used for upsampling"""
    def out_dshape(self, in_dshape, antialias, cuda, partitioned):
        return dshape(Option(ct.float64))

    @staticmethod
    def _finalize(bases, cuda=False, **kwargs):
        return xr.DataArray(bases[0], **kwargs)

    @property
    def inputs(self):
        return (extract(self.column),)

    def _build_create(self, required_dshape):
        # Use uninitialized memory, the upsample function must explicitly set unused
        # values to nan
        return self._create_float64_empty

    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        # not called, the upsample function must set agg directly
        pass

    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_cuda(x, y, agg, field):
        # not called, the upsample function must set agg directly
        pass

    @staticmethod
    def _combine(aggs):
        return np.nanmax(aggs, axis=0)


class FloatingReduction(Reduction):
    """Base classes for reductions that always have floating-point dtype."""
    def out_dshape(self, in_dshape, antialias, cuda, partitioned):
        return dshape(Option(ct.float64))

    @staticmethod
    def _finalize(bases, cuda=False, **kwargs):
        return xr.DataArray(bases[0], **kwargs)


class _sum_zero(FloatingReduction):
    """Sum of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
    """
    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        if self_intersect:
            return (AntialiasStage2(AntialiasCombination.SUM_1AGG, 0),)
        else:
            return (AntialiasStage2(AntialiasCombination.SUM_2AGG, 0),)

    def _build_create(self, required_dshape):
        return self._create_float64_zero

    # CPU append functions.
    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        if not isnull(field):
            # agg[y, x] cannot be null as initialised to zero.
            agg[y, x] += field
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*(aa_factor - prev_aa_factor)
        if not isnull(value):
            # agg[y, x] cannot be null as initialised to zero.
            agg[y, x] += value
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias_not_self_intersect(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*aa_factor
        if not isnull(value) and value > agg[y, x]:
            # agg[y, x] cannot be null as initialised to zero.
            agg[y, x] = value
            return 0
        return -1

    # GPU append functions
    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_cuda(x, y, agg, field):
        if not isnull(field):
            nb_cuda.atomic.add(agg, (y, x), field)
            return 0
        return -1

    @staticmethod
    def _combine(aggs):
        return aggs.sum(axis=0, dtype='f8')


class SelfIntersectingFloatingReduction(FloatingReduction):
    """
    Base class for floating reductions for which self-intersecting geometry
    may or may not be desirable.
    Ignored if not using antialiasing.
    """
    def __init__(self, column=None, self_intersect=True):
        super().__init__(column)
        self.self_intersect = self_intersect

    def _antialias_requires_2_stages(self):
        return not self.self_intersect

    def _build_append(self, dshape, schema, cuda, antialias, self_intersect):
        if antialias and not self_intersect:
            if cuda:
                raise NotImplementedError("SelfIntersectingOptionalFieldReduction")
            else:
                if self.column is None:
                    return self._append_no_field_antialias_not_self_intersect
                else:
                    return self._append_antialias_not_self_intersect

        return super()._build_append(dshape, schema, cuda, antialias, self_intersect)

    def _hashable_inputs(self):
        # Reductions with different self_intersect attributes much have different hashes otherwise
        # toolz.memoize will treat them as the same to give incorrect results.
        return super()._hashable_inputs() + (self.self_intersect,)


class sum(SelfIntersectingFloatingReduction):
    """Sum of all elements in ``column``.

    Elements of resulting aggregate are nan if they are not updated.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    """
    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        if self_intersect:
            return (AntialiasStage2(AntialiasCombination.SUM_1AGG, array_module.nan),)
        else:
            return (AntialiasStage2(AntialiasCombination.SUM_2AGG, array_module.nan),)

    def _build_bases(self, cuda, partitioned):
        if cuda:
            return (_sum_zero(self.column), any(self.column))
        else:
            return (self,)

    # CPU append functions
    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        if not isnull(field):
            if isnull(agg[y, x]):
                agg[y, x] = field
            else:
                agg[y, x] += field
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*(aa_factor - prev_aa_factor)
        if not isnull(value):
            if isnull(agg[y, x]):
                agg[y, x] = value
            else:
                agg[y, x] += value
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias_not_self_intersect(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*aa_factor
        if not isnull(value):
            if isnull(agg[y, x]) or value > agg[y, x]:
                agg[y, x] = value
                return 0
        return -1

    @staticmethod
    def _combine(aggs):
        return nansum_missing(aggs, axis=0)

    @staticmethod
    def _finalize(bases, cuda=False, **kwargs):
        if cuda:
            sums, anys = bases
            x = np.where(anys, sums, np.nan)
            return xr.DataArray(x, **kwargs)
        else:
            return xr.DataArray(bases[0], **kwargs)


class m2(FloatingReduction):
    """Sum of square differences from the mean of all elements in ``column``.

    Intermediate value for computing ``var`` and ``std``, not intended to be
    used on its own.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    """
    def uses_cuda_mutex(self) -> UsesCudaMutex:
        return UsesCudaMutex.Global

    def _build_append(self, dshape, schema, cuda, antialias, self_intersect):
        return super(m2, self)._build_append(dshape, schema, cuda, antialias, self_intersect)

    def _build_create(self, required_dshape):
        return self._create_float64_zero

    def _build_temps(self, cuda=False):
        return (_sum_zero(self.column), count(self.column))

    # CPU append functions
    @staticmethod
    @ngjit
    def _append(x, y, m2, field, sum, count):
        # sum & count are the results of sum[y, x], count[y, x] before being
        # updated by field
        if not isnull(field):
            if count > 0:
                u1 = np.float64(sum) / count
                u = np.float64(sum + field) / (count + 1)
                m2[y, x] += (field - u1) * (field - u)
                return 0
        return -1

    # GPU append functions
    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_cuda(x, y, m2, field, sum, count):
        # sum & count are the results of sum[y, x], count[y, x] before being
        # updated by field
        if not isnull(field):
            if count > 0:
                u1 = np.float64(sum) / count
                u = np.float64(sum + field) / (count + 1)
                m2[y, x] += (field - u1) * (field - u)
                return 0
        return -1

    @staticmethod
    def _combine(Ms, sums, ns):
        with np.errstate(divide='ignore', invalid='ignore'):
            mu = np.nansum(sums, axis=0) / ns.sum(axis=0)
            return np.nansum(Ms + ns*(sums/ns - mu)**2, axis=0)


class min(FloatingReduction):
    """Minimum value of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    """
    def _antialias_requires_2_stages(self):
        return True

    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        return (AntialiasStage2(AntialiasCombination.MIN, array_module.nan),)

    # CPU append functions
    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        if not isnull(field) and (isnull(agg[y, x]) or agg[y, x] > field):
            agg[y, x] = field
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*aa_factor
        if not isnull(value) and (isnull(agg[y, x]) or value > agg[y, x]):
            agg[y, x] = value
            return 0
        return -1

    # GPU append functions
    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_cuda(x, y, agg, field):
        if not isnull(field):
            old = cuda_atomic_nanmin(agg, (y, x), field)
            if isnull(old) or old > field:
                return 0
        return -1

    @staticmethod
    def _combine(aggs):
        return np.nanmin(aggs, axis=0)


class max(FloatingReduction):
    """Maximum value of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    """
    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        return (AntialiasStage2(AntialiasCombination.MAX, array_module.nan),)

    # CPU append functions
    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        if not isnull(field) and (isnull(agg[y, x]) or agg[y, x] < field):
            agg[y, x] = field
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*aa_factor
        if not isnull(value) and (isnull(agg[y, x]) or value > agg[y, x]):
            agg[y, x] = value
            return 0
        return -1

    # GPU append functions
    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_antialias_cuda(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*aa_factor
        if not isnull(value):
            old = cuda_atomic_nanmax(agg, (y, x), value)
            if isnull(old) or old < value:
                return 0
        return -1

    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_cuda(x, y, agg, field):
        if not isnull(field):
            old = cuda_atomic_nanmax(agg, (y, x), field)
            if isnull(old) or old < field:
                return 0
        return -1

    @staticmethod
    def _combine(aggs):
        return np.nanmax(aggs, axis=0)


class count_cat(by):
    """Count of all elements in ``column``, grouped by category.
    Alias for `by(...,count())`, for backwards compatibility.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be
        categorical. Resulting aggregate has a outer dimension axis along the
        categories present.
    """
    def __init__(self, column):
        super(count_cat, self).__init__(column, count())


class mean(Reduction):
    """Mean of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    """
    def _build_bases(self, cuda, partitioned):
        return (_sum_zero(self.column), _count_ignore_antialiasing(self.column))

    @staticmethod
    def _finalize(bases, cuda=False, **kwargs):
        sums, counts = bases
        with np.errstate(divide='ignore', invalid='ignore'):
            x = np.where(counts > 0, sums/counts, np.nan)
        return xr.DataArray(x, **kwargs)


class var(Reduction):
    """Variance of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    """
    def _build_bases(self, cuda, partitioned):
        return (_sum_zero(self.column), count(self.column), m2(self.column))

    @staticmethod
    def _finalize(bases, cuda=False, **kwargs):
        sums, counts, m2s = bases
        with np.errstate(divide='ignore', invalid='ignore'):
            x = np.where(counts > 0, m2s / counts, np.nan)
        return xr.DataArray(x, **kwargs)


class std(Reduction):
    """Standard Deviation of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    """
    def _build_bases(self, cuda, partitioned):
        return (_sum_zero(self.column), count(self.column), m2(self.column))

    @staticmethod
    def _finalize(bases, cuda=False, **kwargs):
        sums, counts, m2s = bases
        with np.errstate(divide='ignore', invalid='ignore'):
            x = np.where(counts > 0, np.sqrt(m2s / counts), np.nan)
        return xr.DataArray(x, **kwargs)


class _first_or_last(Reduction):
    """Abstract base class of first and last reductions.
    """
    def out_dshape(self, in_dshape, antialias, cuda, partitioned):
        return dshape(ct.float64)

    def uses_row_index(self, cuda, partitioned):
        return cuda or partitioned

    def _antialias_requires_2_stages(self):
        return True

    def _build_bases(self, cuda, partitioned):
        if self.uses_row_index(cuda, partitioned):
            row_index_selector = self._create_row_index_selector()
            wrapper = where(selector=row_index_selector, lookup_column=self.column)
            wrapper._nan_check_column = self.column
            # where reduction is always preceded by its selector reduction
            return row_index_selector._build_bases(cuda, partitioned) + (wrapper,)
        else:
            return super()._build_bases(cuda, partitioned)

    @staticmethod
    def _combine(aggs):
        # Dask combine is handled by a where reduction using a row index.
        # Hence this can only ever be called if npartitions == 1 in which case len(aggs) == 1.
        if len(aggs) > 1:
            raise RuntimeError("_combine should never be called with more than one agg")
        return aggs[0]

    def _create_row_index_selector(self):
        pass

    @staticmethod
    def _finalize(bases, cuda=False, **kwargs):
        # Note returning the last of the bases which is correct regardless of whether
        # this is a simple reduction (with a single base) or a compound where reduction
        # (with 2 bases, the second of which is the where reduction).
        return xr.DataArray(bases[-1], **kwargs)


class first(_first_or_last):
    """First value encountered in ``column``.

    Useful for categorical data where an actual value must always be returned,
    not an average or other numerical calculation.

    Currently only supported for rasters, externally to this class.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. If the data type is floating point,
        ``NaN`` values in the column are skipped.
    """
    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        return (AntialiasStage2(AntialiasCombination.FIRST, array_module.nan),)

    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        if not isnull(field) and isnull(agg[y, x]):
            agg[y, x] = field
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*aa_factor
        if not isnull(value) and (isnull(agg[y, x]) or value > agg[y, x]):
            agg[y, x] = value
            return 0
        return -1

    def _create_row_index_selector(self):
        return _min_row_index()


class last(_first_or_last):
    """Last value encountered in ``column``.

    Useful for categorical data where an actual value must always be returned,
    not an average or other numerical calculation.

    Currently only supported for rasters, externally to this class.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. If the data type is floating point,
        ``NaN`` values in the column are skipped.
    """
    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        return (AntialiasStage2(AntialiasCombination.LAST, array_module.nan),)

    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        if not isnull(field):
            agg[y, x] = field
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*aa_factor
        if not isnull(value) and (isnull(agg[y, x]) or value > agg[y, x]):
            agg[y, x] = value
            return 0
        return -1

    def _create_row_index_selector(self):
        return _max_row_index()


class FloatingNReduction(OptionalFieldReduction):
    def __init__(self, column=None, n=1):
        super().__init__(column)
        self.n = n if n >= 1 else 1

    def out_dshape(self, in_dshape, antialias, cuda, partitioned):
        return dshape(ct.float64)

    def _add_finalize_kwargs(self, **kwargs):
        # Add the new dimension and coordinate.
        n_name = "n"
        n_values = np.arange(self.n)

        # Return a modified copy of kwargs. Cannot modify supplied kwargs as it
        # may be used by multiple reductions, e.g. if a summary reduction.
        kwargs = copy.deepcopy(kwargs)
        kwargs['dims'] += [n_name]
        kwargs['coords'][n_name] = n_values
        return kwargs

    def _build_create(self, required_dshape):
        return lambda shape, array_module: super(FloatingNReduction, self)._build_create(
            required_dshape)(shape + (self.n,), array_module)

    def _build_finalize(self, dshape):
        def finalize(bases, cuda=False, **kwargs):
            kwargs = self._add_finalize_kwargs(**kwargs)
            return self._finalize(bases, cuda=cuda, **kwargs)

        return finalize

    def _hashable_inputs(self):
        return super()._hashable_inputs() + (self.n,)


class _first_n_or_last_n(FloatingNReduction):
    """Abstract base class of first_n and last_n reductions.
    """
    def uses_row_index(self, cuda, partitioned):
        return cuda or partitioned

    def _antialias_requires_2_stages(self):
        return True

    def _build_bases(self, cuda, partitioned):
        if self.uses_row_index(cuda, partitioned):
            row_index_selector = self._create_row_index_selector()
            wrapper = where(selector=row_index_selector, lookup_column=self.column)
            wrapper._nan_check_column = self.column
            # where reduction is always preceded by its selector reduction
            return row_index_selector._build_bases(cuda, partitioned) + (wrapper,)
        else:
            return super()._build_bases(cuda, partitioned)

    @staticmethod
    def _combine(aggs):
        # Dask combine is handled by a where reduction using a row index.
        # Hence this can only ever be called if npartitions == 1 in which case len(aggs) == 1.
        if len(aggs) > 1:
            raise RuntimeError("_combine should never be called with more than one agg")
        return aggs[0]

    def _create_row_index_selector(self):
        pass

    @staticmethod
    def _finalize(bases, cuda=False, **kwargs):
        # Note returning the last of the bases which is correct regardless of whether
        # this is a simple reduction (with a single base) or a compound where reduction
        # (with 2 bases, the second of which is the where reduction).
        return xr.DataArray(bases[-1], **kwargs)


class first_n(_first_n_or_last_n):
    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        return (AntialiasStage2(AntialiasCombination.FIRST, array_module.nan, n_reduction=True),)

    # CPU append functions
    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        if not isnull(field):
            # Check final value first for quick abort.
            n = agg.shape[2]
            if not isnull(agg[y, x, n-1]):
                return -1

            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            for i in range(n):
                if isnull(agg[y, x, i]):
                    # Nothing to shift.
                    agg[y, x, i] = field
                    return i
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*aa_factor
        if not isnull(value):
            # Check final value first for quick abort.
            n = agg.shape[2]
            if not isnull(agg[y, x, n-1]):
                return -1

            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            for i in range(n):
                if isnull(agg[y, x, i]):
                    # Nothing to shift.
                    agg[y, x, i] = value
                    return i
        return -1

    def _create_row_index_selector(self):
        return _min_n_row_index(n=self.n)


class last_n(_first_n_or_last_n):
    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        return (AntialiasStage2(AntialiasCombination.LAST, array_module.nan, n_reduction=True),)

    # CPU append functions
    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        if not isnull(field):
            # Always inserts at front of agg's third dimension.
            shift_and_insert(agg[y, x], field, 0)
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*aa_factor
        if not isnull(value):
            # Always inserts at front of agg's third dimension.
            shift_and_insert(agg[y, x], value, 0)
            return 0
        return -1

    def _create_row_index_selector(self):
        return _max_n_row_index(n=self.n)


class max_n(FloatingNReduction):
    def uses_cuda_mutex(self) -> UsesCudaMutex:
        return UsesCudaMutex.Local

    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        return (AntialiasStage2(AntialiasCombination.MAX, array_module.nan, n_reduction=True),)

    # CPU append functions
    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        if not isnull(field):
            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            n = agg.shape[2]
            for i in range(n):
                if isnull(agg[y, x, i]) or field > agg[y, x, i]:
                    shift_and_insert(agg[y, x], field, i)
                    return i
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*aa_factor
        if not isnull(value):
            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            n = agg.shape[2]
            for i in range(n):
                if isnull(agg[y, x, i]) or value > agg[y, x, i]:
                    shift_and_insert(agg[y, x], value, i)
                    return i
        return -1

    # GPU append functions
    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_cuda(x, y, agg, field):
        if not isnull(field):
            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            n = agg.shape[2]
            for i in range(n):
                if isnull(agg[y, x, i]) or field > agg[y, x, i]:
                    cuda_shift_and_insert(agg[y, x], field, i)
                    return i
        return -1

    def _build_combine(self, dshape, antialias, cuda, partitioned, categorical = False):
        if cuda:
            return self._combine_cuda
        else:
            return self._combine

    @staticmethod
    def _combine(aggs):
        ret = aggs[0]
        for i in range(1, len(aggs)):
            if ret.ndim == 3:  # ndim is either 3 (ny, nx, n) or 4 (ny, nx, ncat, n)
                nanmax_n_in_place_3d(aggs[0], aggs[i])
            else:
                nanmax_n_in_place_4d(aggs[0], aggs[i])
        return ret

    @staticmethod
    def _combine_cuda(aggs):
        ret = aggs[0]
        kernel_args = cuda_args(ret.shape[:-1])
        for i in range(1, len(aggs)):
            if ret.ndim == 3:  # ndim is either 3 (ny, nx, n) or 4 (ny, nx, ncat, n)
                cuda_nanmax_n_in_place_3d[kernel_args](aggs[0], aggs[i])
            else:
                cuda_nanmax_n_in_place_4d[kernel_args](aggs[0], aggs[i])
        return ret


class min_n(FloatingNReduction):
    def uses_cuda_mutex(self) -> UsesCudaMutex:
        return UsesCudaMutex.Local

    def _antialias_requires_2_stages(self):
        return True

    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        return (AntialiasStage2(AntialiasCombination.MIN, array_module.nan, n_reduction=True),)

    # CPU append functions
    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        if not isnull(field):
            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            n = agg.shape[2]
            for i in range(n):
                if isnull(agg[y, x, i]) or field < agg[y, x, i]:
                    shift_and_insert(agg[y, x], field, i)
                    return i
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        value = field*aa_factor
        if not isnull(value):
            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            n = agg.shape[2]
            for i in range(n):
                if isnull(agg[y, x, i]) or value < agg[y, x, i]:
                    shift_and_insert(agg[y, x], value, i)
                    return i
        return -1

    # GPU append functions
    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_cuda(x, y, agg, field):
        if not isnull(field):
            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            n = agg.shape[2]
            for i in range(n):
                if isnull(agg[y, x, i]) or field < agg[y, x, i]:
                    cuda_shift_and_insert(agg[y, x], field, i)
                    return i
        return -1

    def _build_combine(self, dshape, antialias, cuda, partitioned, categorical = False):
        if cuda:
            return self._combine_cuda
        else:
            return self._combine

    @staticmethod
    def _combine(aggs):
        ret = aggs[0]
        for i in range(1, len(aggs)):
            if ret.ndim == 3:  # ndim is either 3 (ny, nx, n) or 4 (ny, nx, ncat, n)
                nanmin_n_in_place_3d(aggs[0], aggs[i])
            else:
                nanmin_n_in_place_4d(aggs[0], aggs[i])
        return ret

    @staticmethod
    def _combine_cuda(aggs):
        ret = aggs[0]
        kernel_args = cuda_args(ret.shape[:-1])
        for i in range(1, len(aggs)):
            if ret.ndim == 3:  # ndim is either 3 (ny, nx, n) or 4 (ny, nx, ncat, n)
                cuda_nanmin_n_in_place_3d[kernel_args](aggs[0], aggs[i])
            else:
                cuda_nanmin_n_in_place_4d[kernel_args](aggs[0], aggs[i])
        return ret


class mode(Reduction):
    """Mode (most common value) of all the values encountered in ``column``.

    Useful for categorical data where an actual value must always be returned,
    not an average or other numerical calculation.

    Currently only supported for rasters, externally to this class.
    Implementing it for other glyph types would be difficult due to potentially
    unbounded data storage requirements to store indefinite point or line
    data per pixel.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. If the data type is floating point,
        ``NaN`` values in the column are skipped.
    """
    def out_dshape(self, in_dshape, antialias, cuda, partitioned):
        return dshape(Option(ct.float64))

    @staticmethod
    def _append(x, y, agg):
        raise NotImplementedError("mode is currently implemented only for rasters")

    @staticmethod
    def _combine(aggs):
        raise NotImplementedError("mode is currently implemented only for rasters")

    @staticmethod
    def _finalize(bases, **kwargs):
        raise NotImplementedError("mode is currently implemented only for rasters")


class where(FloatingReduction):
    """
    Returns values from a ``lookup_column`` corresponding to a ``selector``
    reduction that is applied to some other column.

    If ``lookup_column`` is ``None`` then it uses the index of the row in the
    DataFrame instead of a named column. This is returned as an int64
    aggregation with -1 used to denote no value.

    Examples
    --------
    >>> canvas.line(df, 'x', 'y', agg=ds.where(ds.max("value"), "other"))  # doctest: +SKIP

    This returns the values of the "other" column that correspond to the
    maximum of the "value" column in each bin.

    Parameters
    ----------
    selector: Reduction
        Reduction used to select the values of the ``lookup_column`` which are
        returned by this ``where`` reduction.

    lookup_column : str | None
        Column containing values that are returned from this ``where``
        reduction, or ``None`` to return row indexes instead.
    """
    def __init__(self, selector: Reduction, lookup_column: str | None=None):
        if not isinstance(selector, (first, first_n, last, last_n, max, max_n, min, min_n,
                                     _max_or_min_row_index, _max_n_or_min_n_row_index)):
            raise TypeError(
                "selector can only be a first, first_n, last, last_n, "
                "max, max_n, min or min_n reduction")
        if lookup_column is None:
            lookup_column = SpecialColumn.RowIndex
        super().__init__(lookup_column)
        self.selector = selector
        # List of all column names that this reduction uses.
        self.columns = (selector.column, lookup_column)

    def __hash__(self):
        return hash((type(self), self._hashable_inputs(), self.selector))

    def is_where(self):
        return True

    def out_dshape(self, input_dshape, antialias, cuda, partitioned):
        if self.column == SpecialColumn.RowIndex:
            return dshape(ct.int64)
        else:
            return dshape(ct.float64)

    def uses_cuda_mutex(self) -> UsesCudaMutex:
        return UsesCudaMutex.Local

    def uses_row_index(self, cuda, partitioned):
        return (self.column == SpecialColumn.RowIndex or
                self.selector.uses_row_index(cuda, partitioned))

    def validate(self, in_dshape):
        if self.column != SpecialColumn.RowIndex:
            super().validate(in_dshape)
        self.selector.validate(in_dshape)
        if self.column != SpecialColumn.RowIndex and self.column == self.selector.column:
            raise ValueError("where and its contained reduction cannot use the same column")

    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        ret = self.selector._antialias_stage_2(self_intersect, array_module)
        if self.column == SpecialColumn.RowIndex:
            # Override antialiased zero value when returning integer row index.
            ret = (AntialiasStage2(combination=ret[0].combination,
                                   zero=-1,
                                   n_reduction=ret[0].n_reduction),)
        return ret

    # CPU append functions
    # All where._append* functions have an extra argument which is the update index.
    # For 3D aggs like max_n, this is the index of insertion in the final dimension,
    # and the previous values from this index upwards are shifted along to make room
    # for the new value.
    @staticmethod
    @ngjit
    def _append(x, y, agg, field, update_index):
        if agg.ndim > 2:
            shift_and_insert(agg[y, x], field, update_index)
        else:
            agg[y, x] = field
        return update_index

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor, update_index):
        # Ignore aa_factor.
        if agg.ndim > 2:
            shift_and_insert(agg[y, x], field, update_index)
        else:
            agg[y, x] = field

    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_antialias_cuda(x, y, agg, field, aa_factor, prev_aa_factor, update_index):
        # Ignore aa_factor
        if agg.ndim > 2:
            cuda_shift_and_insert(agg[y, x], field, update_index)
        else:
            agg[y, x] = field
        return update_index

    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_cuda(x, y, agg, field, update_index):
        if agg.ndim > 2:
            cuda_shift_and_insert(agg[y, x], field, update_index)
        else:
            agg[y, x] = field
        return update_index

    def _build_append(self, dshape, schema, cuda, antialias, self_intersect):
        # If self.column is SpecialColumn.RowIndex then append function is passed a
        # 'field' argument which is the row index.
        if cuda:
            if antialias:
                return self._append_antialias_cuda
            else:
                return self._append_cuda
        else:
            if antialias:
                return self._append_antialias
            else:
                return self._append

    def _build_bases(self, cuda, partitioned):
        selector = self.selector
        if isinstance(selector, (_first_or_last, _first_n_or_last_n)) and \
                selector.uses_row_index(cuda, partitioned):
            # Need to swap out the selector with an equivalent row index selector
            row_index_selector = selector._create_row_index_selector()
            if self.column == SpecialColumn.RowIndex:
                # If selector uses a row index and this where returns the same row index,
                # can just swap out this where reduction with the row_index_selector.
                row_index_selector._nan_check_column = self.selector.column
                return row_index_selector._build_bases(cuda, partitioned)
            else:
                new_where = where(row_index_selector, self.column)
                new_where._nan_check_column = self.selector.column
                return row_index_selector._build_bases(cuda, partitioned) + \
                    new_where._build_bases(cuda, partitioned)
        else:
            return selector._build_bases(cuda, partitioned) + \
                super()._build_bases(cuda, partitioned)

    def _combine_callback(self, cuda, partitioned, categorical):
        # Used by:
        # 1) where._build_combine()) below, the usual mechanism for combining aggs from
        #    different dask partitions.
        # 2) make_antialias_stage_2_functions() in compiler.py to perform stage 2 combine
        #    of antialiased aggs.
        selector = self.selector
        is_n_reduction = isinstance(selector, FloatingNReduction)
        if cuda:
            append = selector._append_cuda
        else:
            append = selector._append

        # If the selector uses a row_index then selector_aggs will be int64 with -1
        # representing missing data. Otherwise missing data is NaN.
        invalid = isminus1 if self.selector.uses_row_index(cuda, partitioned) else isnull

        @ngjit
        def combine_cpu_2d(aggs, selector_aggs):
            ny, nx = aggs[0].shape
            for y in range(ny):
                for x in range(nx):
                    value = selector_aggs[1][y, x]
                    if not invalid(value) and append(x, y, selector_aggs[0], value) >= 0:
                        aggs[0][y, x] = aggs[1][y, x]

        @ngjit
        def combine_cpu_3d(aggs, selector_aggs):
            ny, nx, ncat = aggs[0].shape
            for y in range(ny):
                for x in range(nx):
                    for cat in range(ncat):
                        value = selector_aggs[1][y, x, cat]
                        if not invalid(value) and append(x, y, selector_aggs[0][:, :, cat],
                                                         value) >= 0:
                            aggs[0][y, x, cat] = aggs[1][y, x, cat]

        @ngjit
        def combine_cpu_n_3d(aggs, selector_aggs):
            ny, nx, n = aggs[0].shape
            for y in range(ny):
                for x in range(nx):
                    for i in range(n):
                        value = selector_aggs[1][y, x, i]
                        if invalid(value):
                            break
                        update_index = append(x, y, selector_aggs[0], value)
                        if update_index < 0:
                            break
                        shift_and_insert(aggs[0][y, x], aggs[1][y, x, i], update_index)

        @ngjit
        def combine_cpu_n_4d(aggs, selector_aggs):
            ny, nx, ncat, n = aggs[0].shape
            for y in range(ny):
                for x in range(nx):
                    for cat in range(ncat):
                        for i in range(n):
                            value = selector_aggs[1][y, x, cat, i]
                            if invalid(value):
                                break
                            update_index = append(x, y, selector_aggs[0][:, :, cat, :], value)
                            if update_index < 0:
                                break
                            shift_and_insert(aggs[0][y, x, cat], aggs[1][y, x, cat, i],
                                             update_index)

        @nb_cuda.jit
        def combine_cuda_2d(aggs, selector_aggs):
            ny, nx = aggs[0].shape
            x, y = nb_cuda.grid(2)
            if x < nx and y < ny:
                value = selector_aggs[1][y, x]
                if not invalid(value) and append(x, y, selector_aggs[0], value) >= 0:
                    aggs[0][y, x] = aggs[1][y, x]

        @nb_cuda.jit
        def combine_cuda_3d(aggs, selector_aggs):
            ny, nx, ncat = aggs[0].shape
            x, y, cat = nb_cuda.grid(3)
            if x < nx and y < ny and cat < ncat:
                value = selector_aggs[1][y, x, cat]
                if not invalid(value) and append(x, y, selector_aggs[0][:, :, cat], value) >= 0:
                    aggs[0][y, x, cat] = aggs[1][y, x, cat]

        @nb_cuda.jit
        def combine_cuda_n_3d(aggs, selector_aggs):
            ny, nx, n = aggs[0].shape
            x, y = nb_cuda.grid(2)
            if x < nx and y < ny:
                for i in range(n):
                    value = selector_aggs[1][y, x, i]
                    if invalid(value):
                        break
                    update_index = append(x, y, selector_aggs[0], value)
                    if update_index < 0:
                        break
                    cuda_shift_and_insert(aggs[0][y, x], aggs[1][y, x, i], update_index)

        @nb_cuda.jit
        def combine_cuda_n_4d(aggs, selector_aggs):
            ny, nx, ncat, n = aggs[0].shape
            x, y, cat = nb_cuda.grid(3)
            if x < nx and y < ny and cat < ncat:
                for i in range(n):
                    value = selector_aggs[1][y, x, cat, i]
                    if invalid(value):
                        break
                    update_index = append(x, y, selector_aggs[0][:, :, cat, :], value)
                    if update_index < 0:
                        break
                    cuda_shift_and_insert(aggs[0][y, x, cat], aggs[1][y, x, cat, i], update_index)

        if is_n_reduction:
            # ndim is either 3 (ny, nx, n) or 4 (ny, nx, ncat, n)
            if cuda:
                return combine_cuda_n_4d if categorical else combine_cuda_n_3d
            else:
                return combine_cpu_n_4d if categorical else combine_cpu_n_3d
        else:
            # ndim is either 2 (ny, nx) or 3 (ny, nx, ncat)
            if cuda:
                return combine_cuda_3d if categorical else combine_cuda_2d
            else:
                return combine_cpu_3d if categorical else combine_cpu_2d

    def _build_combine(self, dshape, antialias, cuda, partitioned, categorical = False):
        combine = self._combine_callback(cuda, partitioned, categorical)

        def wrapped_combine(aggs, selector_aggs):
            if len(aggs) == 1:
                pass
            elif cuda:
                assert len(aggs) == 2
                is_n_reduction = isinstance(self.selector, FloatingNReduction)
                shape = aggs[0].shape[:-1] if is_n_reduction else aggs[0].shape
                combine[cuda_args(shape)](aggs, selector_aggs)
            else:
                for i in range(1, len(aggs)):
                    combine((aggs[0], aggs[i]), (selector_aggs[0], selector_aggs[i]))

            return aggs[0], selector_aggs[0]

        return wrapped_combine

    def _build_combine_temps(self, cuda, partitioned):
        return (self.selector,)

    def _build_create(self, required_dshape):
        # Return a function that when called with a shape creates an agg array
        # of the required type (numpy/cupy) and dtype.
        if isinstance(self.selector, FloatingNReduction):
            # This specialisation isn't ideal but Reduction classes do not
            # store information about the required extra dimension.
            return lambda shape, array_module: super(where, self)._build_create(
                required_dshape)(shape + (self.selector.n,), array_module)
        else:
            return super()._build_create(required_dshape)

    def _build_finalize(self, dshape):
        if isinstance(self.selector, FloatingNReduction):
            add_finalize_kwargs = self.selector._add_finalize_kwargs
        else:
            add_finalize_kwargs = None

        def finalize(bases, cuda=False, **kwargs):
            if add_finalize_kwargs is not None:
                kwargs = add_finalize_kwargs(**kwargs)

            return xr.DataArray(bases[-1], **kwargs)

        return finalize


class summary(Expr):
    """A collection of named reductions.

    Computes all aggregates simultaneously, output is stored as a
    ``xarray.Dataset``.

    Examples
    --------
    A reduction for computing the mean of column "a", and the sum of column "b"
    for each bin, all in a single pass.

    >>> import datashader as ds
    >>> red = ds.summary(mean_a=ds.mean('a'), sum_b=ds.sum('b'))

    Notes
    -----
    A single pass of the source dataset using antialiased lines can either be
    performed using a single-stage aggregation (e.g. ``self_intersect=True``)
    or two stages (``self_intersect=False``). If a ``summary`` contains a
    ``count`` or ``sum`` reduction with ``self_intersect=False``, or any of
    ``first``, ``last`` or ``min``, then the antialiased line pass will be
    performed in two stages.
    """
    def __init__(self, **kwargs):
        ks, vs = zip(*sorted(kwargs.items()))
        self.keys = ks
        self.values = vs

    def __hash__(self):
        return hash((type(self), tuple(self.keys), tuple(self.values)))

    def is_categorical(self):
        for v in self.values:
            if v.is_categorical():
                return True
        return False

    def uses_row_index(self, cuda, partitioned):
        for v in self.values:
            if v.uses_row_index(cuda, partitioned):
                return True
        return False

    def validate(self, input_dshape):
        for v in self.values:
            v.validate(input_dshape)

        # Check that any included FloatingNReductions have the same n values.
        n_values = []
        for v in self.values:
            if isinstance(v, where):
                v = v.selector
            if isinstance(v, FloatingNReduction):
                n_values.append(v.n)
        if len(np.unique(n_values)) > 1:
            raise ValueError(
                "Using multiple FloatingNReductions with different n values is not supported")

    @property
    def inputs(self):
        return tuple(unique(concat(v.inputs for v in self.values)))


class _max_or_min_row_index(OptionalFieldReduction):
    """Abstract base class of max and min row_index reductions.
    """
    def __init__(self):
        super().__init__(column=SpecialColumn.RowIndex)

    def out_dshape(self, in_dshape, antialias, cuda, partitioned):
        return dshape(ct.int64)

    def uses_row_index(self, cuda, partitioned):
        return True


class _max_row_index(_max_or_min_row_index):
    """Max reduction operating on row index.

    This is a private class as it is not intended to be used explicitly in
    user code. It is primarily purpose is to support the use of ``last``
    reductions using dask and/or CUDA.
    """
    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        return (AntialiasStage2(AntialiasCombination.MAX, -1),)

    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        # field is int64 row index
        if field > agg[y, x]:
            agg[y, x] = field
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        # field is int64 row index
        # Ignore aa_factor
        if field > agg[y, x]:
            agg[y, x] = field
            return 0
        return -1

    # GPU append functions
    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_cuda(x, y, agg, field):
        # field is int64 row index
        if field != -1:
            old = nb_cuda.atomic.max(agg, (y, x), field)
            if old < field:
                return 0
        return -1

    @staticmethod
    def _combine(aggs):
        # Maximum ignoring -1 values
        # Works for CPU and GPU
        ret = aggs[0]
        for i in range(1, len(aggs)):
            # Works with numpy or cupy arrays
            np.maximum(ret, aggs[i], out=ret)
        return ret


class _min_row_index(_max_or_min_row_index):
    """Min reduction operating on row index.

    This is a private class as it is not intended to be used explicitly in
    user code. It is primarily purpose is to support the use of ``first``
    reductions using dask and/or CUDA.
    """
    def _antialias_requires_2_stages(self):
        return True

    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        return (AntialiasStage2(AntialiasCombination.MIN, -1),)

    def uses_cuda_mutex(self) -> UsesCudaMutex:
        return UsesCudaMutex.Local

    # CPU append functions
    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        # field is int64 row index
        if field != -1 and (agg[y, x] == -1 or field < agg[y, x]):
            agg[y, x] = field
            return 0
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        # field is int64 row index
        # Ignore aa_factor
        if field != -1 and (agg[y, x] == -1 or field < agg[y, x]):
            agg[y, x] = field
            return 0
        return -1

    # GPU append functions
    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_cuda(x, y, agg, field):
        # field is int64 row index
        # Always uses cuda mutex so this does not need to be atomic
        if field != -1 and (agg[y, x] == -1 or field < agg[y, x]):
            agg[y, x] = field
            return 0
        return -1

    def _build_combine(self, dshape, antialias, cuda, partitioned, categorical = False):
        if cuda:
            return self._combine_cuda
        else:
            return self._combine

    @staticmethod
    def _combine(aggs):
        # Minimum ignoring -1 values
        ret = aggs[0]
        for i in range(1, len(aggs)):
            # Can take 2d (ny, nx) or 3d (ny, nx, ncat) arrays.
            row_min_in_place(ret, aggs[i])
        return ret

    @staticmethod
    def _combine_cuda(aggs):
        ret = aggs[0]
        if len(aggs) > 1:
            if ret.ndim == 2:  # ndim is either 2 (ny, nx) or 3 (ny, nx, ncat)
                # 3d view of each agg
                aggs = [cp.expand_dims(agg, 2) for agg in aggs]
            kernel_args = cuda_args(ret.shape[:3])
            for i in range(1, len(aggs)):
                cuda_row_min_in_place[kernel_args](aggs[0], aggs[i])
        return ret


class _max_n_or_min_n_row_index(FloatingNReduction):
    """Abstract base class of max_n and min_n row_index reductions.
    """
    def __init__(self, n=1):
        super().__init__(column=SpecialColumn.RowIndex)
        self.n = n if n >= 1 else 1

    def out_dshape(self, in_dshape, antialias, cuda, partitioned):
        return dshape(ct.int64)

    def uses_cuda_mutex(self) -> UsesCudaMutex:
        return UsesCudaMutex.Local

    def uses_row_index(self, cuda, partitioned):
        return True

    def _build_combine(self, dshape, antialias, cuda, partitioned, categorical = False):
        if cuda:
            return self._combine_cuda
        else:
            return self._combine


class _max_n_row_index(_max_n_or_min_n_row_index):
    """Max_n reduction operating on row index.

    This is a private class as it is not intended to be used explicitly in
    user code. It is primarily purpose is to support the use of ``last_n``
    reductions using dask and/or CUDA.
    """
    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        return (AntialiasStage2(AntialiasCombination.MAX, -1, n_reduction=True),)

    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        # field is int64 row index
        if field != -1:
            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            n = agg.shape[2]
            for i in range(n):
                if agg[y, x, i] == -1 or field > agg[y, x, i]:
                    shift_and_insert(agg[y, x], field, i)
                    return i
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        # field is int64 row index
        # Ignoring aa_factor
        if field != -1:
            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            n = agg.shape[2]
            for i in range(n):
                if agg[y, x, i] == -1 or field > agg[y, x, i]:
                    # Bump previous values along to make room for new value.
                    for j in range(n-1, i, -1):
                        agg[y, x, j] = agg[y, x, j-1]
                    agg[y, x, i] = field
                    return i
        return -1

    # GPU append functions
    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_cuda(x, y, agg, field):
        # field is int64 row index
        # Always uses cuda mutex so this does not need to be atomic
        if field != -1:
            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            n = agg.shape[2]
            for i in range(n):
                if agg[y, x, i] == -1 or field > agg[y, x, i]:
                    cuda_shift_and_insert(agg[y, x], field, i)
                    return i
        return -1

    @staticmethod
    def _combine(aggs):
        ret = aggs[0]
        if len(aggs) > 1:
            if ret.ndim == 3:  # ndim is either 3 (ny, nx, n) or 4 (ny, nx, ncat, n)
                row_max_n_in_place_3d(aggs[0], aggs[1])
            else:
                row_max_n_in_place_4d(aggs[0], aggs[1])
        return ret

    @staticmethod
    def _combine_cuda(aggs):
        ret = aggs[0]
        if len(aggs) > 1:
            kernel_args = cuda_args(ret.shape[:-1])
            if ret.ndim == 3:  # ndim is either 3 (ny, nx, n) or 4 (ny, nx, ncat, n)
                cuda_row_max_n_in_place_3d[kernel_args](aggs[0], aggs[1])
            else:
                cuda_row_max_n_in_place_4d[kernel_args](aggs[0], aggs[1])
        return ret


class _min_n_row_index(_max_n_or_min_n_row_index):
    """Min_n reduction operating on row index.

    This is a private class as it is not intended to be used explicitly in
    user code. It is primarily purpose is to support the use of ``first_n``
    reductions using dask and/or CUDA.
    """
    def _antialias_requires_2_stages(self):
        return True

    def _antialias_stage_2(self, self_intersect, array_module) -> tuple[AntialiasStage2]:
        return (AntialiasStage2(AntialiasCombination.MIN, -1, n_reduction=True),)

    @staticmethod
    @ngjit
    def _append(x, y, agg, field):
        # field is int64 row index
        if field != -1:
            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            n = agg.shape[2]
            for i in range(n):
                if agg[y, x, i] == -1 or field < agg[y, x, i]:
                    shift_and_insert(agg[y, x], field, i)
                    return i
        return -1

    @staticmethod
    @ngjit
    def _append_antialias(x, y, agg, field, aa_factor, prev_aa_factor):
        # field is int64 row index
        # Ignoring aa_factor
        if field != -1:
            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            n = agg.shape[2]
            for i in range(n):
                if agg[y, x, i] == -1 or field < agg[y, x, i]:
                    shift_and_insert(agg[y, x], field, i)
                    return i
        return -1

    @staticmethod
    @nb_cuda.jit(device=True)
    def _append_cuda(x, y, agg, field):
        # field is int64 row index
        # Always uses cuda mutex so this does not need to be atomic
        if field != -1:
            # Linear walk along stored values.
            # Could do binary search instead but not expecting n to be large.
            n = agg.shape[2]
            for i in range(n):
                if agg[y, x, i] == -1 or field < agg[y, x, i]:
                    cuda_shift_and_insert(agg[y, x], field, i)
                    return i
        return -1

    @staticmethod
    def _combine(aggs):
        ret = aggs[0]
        if len(aggs) > 1:
            if ret.ndim == 3:  # ndim is either 3 (ny, nx, n) or 4 (ny, nx, ncat, n)
                row_min_n_in_place_3d(aggs[0], aggs[1])
            else:
                row_min_n_in_place_4d(aggs[0], aggs[1])
        return ret

    @staticmethod
    def _combine_cuda(aggs):
        ret = aggs[0]
        if len(aggs) > 1:
            kernel_args = cuda_args(ret.shape[:-1])
            if ret.ndim == 3:  # ndim is either 3 (ny, nx, n) or 4 (ny, nx, ncat, n)
                cuda_row_min_n_in_place_3d[kernel_args](aggs[0], aggs[1])
            else:
                cuda_row_min_n_in_place_4d[kernel_args](aggs[0], aggs[1])

        return ret


__all__ = list(set([_k for _k,_v in locals().items()
                    if isinstance(_v,type) and (issubclass(_v,Reduction) or _v is summary)
                    and _v not in [Reduction, OptionalFieldReduction,
                                   FloatingReduction, m2]])) + \
                    ['category_modulo', 'category_binning']