/
reductions.py
436 lines (335 loc) · 11.3 KB
/
reductions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
from __future__ import absolute_import, division, print_function
import numpy as np
from datashape import dshape, isnumeric, Record, Option
from datashape import coretypes as ct
from toolz import concat, unique
import xarray as xr
from .core import Expr
from .utils import ngjit
class Preprocess(Expr):
"""Base clase for preprocessing steps."""
def __init__(self, column):
self.column = column
@property
def inputs(self):
return (self.column,)
class extract(Preprocess):
"""Extract a column from a dataframe as a numpy array of values."""
def apply(self, df):
return df[self.column].values
class category_codes(Preprocess):
"""Extract just the category codes from a categorical column."""
def apply(self, df):
return df[self.column].cat.codes.values
class Reduction(Expr):
"""Base class for per-bin reductions."""
def __init__(self, column):
self.column = column
def validate(self, in_dshape):
if not isnumeric(in_dshape.measure[self.column]):
raise ValueError("input must be numeric")
def out_dshape(self, in_dshape):
return self._dshape
@property
def inputs(self):
return (extract(self.column),)
@property
def _bases(self):
return (self,)
@property
def _temps(self):
return ()
def _build_create(self, dshape):
return self._create
def _build_append(self, dshape):
return self._append
def _build_combine(self, dshape):
return self._combine
def _build_finalize(self, dshape):
return self._finalize
class OptionalFieldReduction(Reduction):
"""Base class for things like ``count`` or ``any``"""
def __init__(self, column=None):
self.column = column
@property
def inputs(self):
return (extract(self.column),) if self.column else ()
def validate(self, in_dshape):
pass
def _build_append(self, dshape):
return self._append if self.column is None else self._append_non_na
@staticmethod
def _finalize(bases, **kwargs):
return xr.DataArray(bases[0], **kwargs)
class count(OptionalFieldReduction):
"""Count elements in each bin.
Parameters
----------
column : str, optional
If provided, only counts elements in ``column`` that are not ``NaN``.
Otherwise, counts every element.
"""
_dshape = dshape(ct.int32)
@staticmethod
@ngjit
def _append(x, y, agg):
agg[y, x] += 1
@staticmethod
@ngjit
def _append_non_na(x, y, agg, field):
if not np.isnan(field):
agg[y, x] += 1
@staticmethod
def _create(shape):
return np.zeros(shape, dtype='i4')
@staticmethod
def _combine(aggs):
return aggs.sum(axis=0, dtype='i4')
class any(OptionalFieldReduction):
"""Whether any elements in ``column`` map to each bin.
Parameters
----------
column : str, optional
If provided, only elements in ``column`` that are ``NaN`` are skipped.
"""
_dshape = dshape(ct.bool_)
@staticmethod
@ngjit
def _append(x, y, agg):
agg[y, x] = True
@staticmethod
@ngjit
def _append_non_na(x, y, agg, field):
if not np.isnan(field):
agg[y, x] = True
@staticmethod
def _create(shape):
return np.zeros(shape, dtype='bool')
@staticmethod
def _combine(aggs):
return aggs.sum(axis=0, dtype='bool')
class FloatingReduction(Reduction):
"""Base classes for reductions that always have floating-point dtype."""
_dshape = dshape(Option(ct.float64))
@staticmethod
def _create(shape):
return np.full(shape, np.nan, dtype='f8')
@staticmethod
def _finalize(bases, **kwargs):
return xr.DataArray(bases[0], **kwargs)
class WeightedReduction(FloatingReduction):
"""FloatingReduction, to be interpolated along each rasterized primitive.
"""
pass
class sum(FloatingReduction):
"""Sum 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.
"""
@staticmethod
@ngjit
def _append(x, y, agg, field):
if not np.isnan(field):
if np.isnan(agg[y, x]):
agg[y, x] = field
else:
agg[y, x] += field
@staticmethod
def _combine(aggs):
missing_vals = np.isnan(aggs)
all_empty = np.bitwise_and.reduce(missing_vals, axis=0)
set_to_zero = missing_vals & ~all_empty
return np.where(set_to_zero, 0, aggs).sum(axis=0)
class wsum(WeightedReduction, sum):
"""Sum of all elements in ``column``, to be interpolated along each
rasterized primitive.
Parameters
----------
column : str, optional
Name of the column to aggregate over. Column data type must be numeric.
If provided, only elements in ``column`` that are ``NaN`` are skipped.
"""
pass
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.
"""
@property
def _temps(self):
return (sum(self.column), count(self.column))
@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 np.isnan(field):
if count == 0:
m2[y, x] = 0
else:
u1 = np.float64(sum) / count
u = np.float64(sum + field) / (count + 1)
m2[y, x] += (field - u1) * (field - u)
@staticmethod
def _combine(Ms, sums, ns):
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.
"""
@staticmethod
@ngjit
def _append(x, y, agg, field):
if np.isnan(agg[y, x]):
agg[y, x] = field
elif agg[y, x] > field:
agg[y, x] = field
@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.
"""
@staticmethod
@ngjit
def _append(x, y, agg, field):
if np.isnan(agg[y, x]):
agg[y, x] = field
elif agg[y, x] < field:
agg[y, x] = field
@staticmethod
def _combine(aggs):
return np.nanmax(aggs, axis=0)
class count_cat(Reduction):
"""Count of all elements in ``column``, grouped by category.
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 validate(self, in_dshape):
if not isinstance(in_dshape.measure[self.column], ct.Categorical):
raise ValueError("input must be categorical")
def out_dshape(self, input_dshape):
cats = input_dshape.measure[self.column].categories
return dshape(Record([(c, ct.int32) for c in cats]))
@property
def inputs(self):
return (category_codes(self.column),)
def _build_create(self, out_dshape):
n_cats = len(out_dshape.measure.fields)
return lambda shape: np.zeros(shape + (n_cats,), dtype='i4')
@staticmethod
@ngjit
def _append(x, y, agg, field):
agg[y, x, field] += 1
@staticmethod
def _combine(aggs):
return aggs.sum(axis=0, dtype='i4')
def _build_finalize(self, dshape):
cats = list(dshape[self.column].categories)
def finalize(bases, **kwargs):
dims = kwargs['dims'] + [self.column]
coords = kwargs['coords'] + [cats]
return xr.DataArray(bases[0], dims=dims, coords=coords)
return finalize
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.
"""
_dshape = dshape(Option(ct.float64))
@property
def _bases(self):
return (sum(self.column), count(self.column))
@staticmethod
def _finalize(bases, **kwargs):
sums, counts = bases
with np.errstate(divide='ignore', invalid='ignore'):
x = sums/counts
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.
"""
_dshape = dshape(Option(ct.float64))
@property
def _bases(self):
return (sum(self.column), count(self.column), m2(self.column))
@staticmethod
def _finalize(bases, **kwargs):
sums, counts, m2s = bases
with np.errstate(divide='ignore', invalid='ignore'):
x = m2s/counts
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.
"""
_dshape = dshape(Option(ct.float64))
@property
def _bases(self):
return (sum(self.column), count(self.column), m2(self.column))
@staticmethod
def _finalize(bases, **kwargs):
sums, counts, m2s = bases
with np.errstate(divide='ignore', invalid='ignore'):
x = np.sqrt(m2s/counts)
return xr.DataArray(x, **kwargs)
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'))
"""
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 validate(self, input_dshape):
for v in self.values:
v.validate(input_dshape)
def out_dshape(self, in_dshape):
return dshape(Record([(k, v.out_dshape(in_dshape)) for (k, v)
in zip(self.keys, self.values)]))
@property
def inputs(self):
return tuple(unique(concat(v.inputs for v in self.values)))