-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
ibis.py
546 lines (471 loc) · 18.4 KB
/
ibis.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
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
import sys
from collections.abc import Iterable
from functools import lru_cache
import numpy as np
from packaging.version import Version
from .. import util
from ..element import Element
from ..ndmapping import NdMapping, item_check, sorted_context
from .interface import DataError, Interface
from .util import cached
@lru_cache
def ibis_version():
import ibis
return Version(ibis.__version__)
@lru_cache
def ibis4():
return ibis_version() >= Version("4.0")
@lru_cache
def ibis5():
return ibis_version() >= Version("5.0")
class IbisInterface(Interface):
types = ()
datatype = "ibis"
default_partitions = 100
zero_indexed_backend_modules = [
'ibis.backends.omniscidb.client',
]
# the rowid is needed until ibis updates versions
@classmethod
def has_rowid(cls):
import ibis.expr.operations
return hasattr(ibis.expr.operations, "RowID")
@classmethod
def is_rowid_zero_indexed(cls, data):
try:
from ibis.client import find_backends, validate_backends
(backend,) = validate_backends(list(find_backends(data)))
except ImportError:
backend = data._find_backend()
return type(backend).__module__ in cls.zero_indexed_backend_modules
@classmethod
def loaded(cls):
return "ibis" in sys.modules
@classmethod
def applies(cls, obj):
if not cls.loaded():
return False
from ibis.expr.types import Expr
return isinstance(obj, Expr)
@classmethod
def init(cls, eltype, data, keys, values):
params = eltype.param.objects()
index = params["kdims"]
columns = params["vdims"]
if isinstance(index.bounds[1], int):
ndim = min([index.bounds[1], len(index.default)])
else:
ndim = None
nvdim = columns.bounds[1] if isinstance(columns.bounds[1], int) else None
if keys and values is None:
values = [c for c in data.columns if c not in keys]
elif values and keys is None:
keys = [c for c in data.columns if c not in values][:ndim]
elif keys is None:
keys = list(data.columns[:ndim])
if values is None:
values = [
key
for key in data.columns[ndim : ((ndim + nvdim) if nvdim else None)]
if key not in keys
]
elif keys == [] and values is None:
values = list(data.columns[: nvdim if nvdim else None])
return data, dict(kdims=keys, vdims=values), {}
@classmethod
def validate(cls, dataset, vdims=True):
dim_types = 'all' if vdims else 'key'
dimensions = dataset.dimensions(dim_types, label='name')
cols = list(dataset.data.columns)
not_found = [d for d in dimensions if d not in cols]
if not_found:
raise DataError("Supplied data does not contain specified "
"dimensions, the following dimensions were "
f"not found: {not_found!r}", cls)
@classmethod
def compute(cls, dataset):
return dataset.clone(dataset.data.execute())
@classmethod
def persist(cls, dataset):
return cls.compute(dataset)
@classmethod
@cached
def length(self, dataset):
# Get the length by counting the length of an empty query.
if ibis4():
return dataset.data.count().execute()
else:
return dataset.data[[]].count().execute()
@classmethod
@cached
def nonzero(cls, dataset):
# Make an empty query to see if a row is returned.
if ibis4():
return bool(len(dataset.data.head(1).execute()))
else:
return bool(len(dataset.data[[]].head(1).execute()))
@classmethod
@cached
def range(cls, dataset, dimension):
dimension = dataset.get_dimension(dimension, strict=True)
if cls.dtype(dataset, dimension).kind in 'SUO':
return None, None
if dimension.nodata is not None:
return Interface.range(dataset, dimension)
column = dataset.data[dimension.name]
return tuple(
dataset.data.aggregate([column.min(), column.max()]).execute().values[0, :]
)
@classmethod
@cached
def values(
cls,
dataset,
dimension,
expanded=True,
flat=True,
compute=True,
keep_index=False,
):
import ibis
dimension = dataset.get_dimension(dimension, strict=True)
data = dataset.data[dimension.name]
if (
ibis_version() > Version("3")
and isinstance(data, ibis.expr.types.AnyColumn)
and not expanded
):
data = dataset.data[[dimension.name]]
if not expanded:
data = data.distinct()
return data if keep_index or not compute else data.execute().values.flatten()
@classmethod
def histogram(cls, expr, bins, density=True, weights=None):
bins = np.asarray(bins)
bins = [int(v) if bins.dtype.kind in 'iu' else float(v) for v in bins]
binned = expr.bucket(bins).name('bucket')
hist = np.zeros(len(bins)-1)
if ibis4():
hist_bins = binned.value_counts().order_by('bucket').execute()
else:
# sort_by will be removed in Ibis 5.0
hist_bins = binned.value_counts().sort_by('bucket').execute()
metric_name = 'bucket_count' if ibis5() else 'count'
for b, v in zip(hist_bins['bucket'], hist_bins[metric_name]):
if np.isnan(b):
continue
hist[int(b)] = v
if weights is not None:
raise NotImplementedError("Weighted histograms currently "
"not implemented for IbisInterface.")
if density:
hist = hist/expr.count().execute()/np.diff(bins)
return hist, bins
@classmethod
@cached
def shape(cls, dataset):
return cls.length(dataset), len(dataset.data.columns)
@classmethod
@cached
def dtype(cls, dataset, dimension):
dimension = dataset.get_dimension(dimension)
return dataset.data.head(0).execute().dtypes[dimension.name]
dimension_type = dtype
@classmethod
def sort(cls, dataset, by=None, reverse=False):
if by is None:
by = []
if ibis_version() >= Version("6.0"):
import ibis
order = ibis.desc if reverse else ibis.asc
return dataset.data.order_by([order(dataset.get_dimension(x).name) for x in by])
elif ibis4():
# Tuple syntax will be removed in Ibis 7.0:
# https://github.com/ibis-project/ibis/pull/6082
return dataset.data.order_by([(dataset.get_dimension(x).name, not reverse) for x in by])
else:
# sort_by will be removed in Ibis 5.0
return dataset.data.sort_by([(dataset.get_dimension(x).name, not reverse) for x in by])
@classmethod
def redim(cls, dataset, dimensions):
return dataset.data.mutate(
**{v.name: dataset.data[k] for k, v in dimensions.items()}
)
@classmethod
def reindex(cls, dataset, kdims=None, vdims=None):
return dataset.data
@classmethod
def _index_ibis_table(cls, data):
import ibis
if not cls.has_rowid():
raise ValueError(
f"iloc expressions are not supported for ibis version {ibis.__version__}."
)
if "hv_row_id__" in data.columns:
return data
if ibis4():
return data.mutate(hv_row_id__=ibis.row_number())
elif cls.is_rowid_zero_indexed(data):
return data.mutate(hv_row_id__=data.rowid())
else:
return data.mutate(hv_row_id__=data.rowid() - 1)
@classmethod
def iloc(cls, dataset, index):
rows, columns = index
scalar = all(map(util.isscalar, index))
if isinstance(columns, slice):
columns = [x.name for x in dataset.dimensions()[columns]]
elif np.isscalar(columns):
columns = [dataset.get_dimension(columns).name]
else:
columns = [dataset.get_dimension(d).name for d in columns]
data = cls._index_ibis_table(dataset.data[columns])
if scalar:
return (
data.filter(data.hv_row_id__ == rows)[columns]
.head(1)
.execute()
.iloc[0, 0]
)
if isinstance(rows, slice):
# We should use a pseudo column for the row number but i think that is still awaiting
# a pr on ibis
if any(x is not None for x in (rows.start, rows.stop, rows.step)):
predicates = []
if rows.start:
predicates += [data.hv_row_id__ >= rows.start]
if rows.stop:
predicates += [data.hv_row_id__ < rows.stop]
data = data.filter(predicates)
else:
if not isinstance(rows, Iterable):
rows = [rows]
data = data.filter([data.hv_row_id__.isin(rows)])
if ibis4():
return data.drop("hv_row_id__")
else:
# Passing a sequence of fields to `drop` will be removed in Ibis 5.0
return data.drop(["hv_row_id__"])
@classmethod
def unpack_scalar(cls, dataset, data):
"""
Given a dataset object and data in the appropriate format for
the interface, return a simple scalar.
"""
if ibis4():
count = data.count().execute()
else:
count = data[[]].count().execute()
if len(data.columns) > 1 or count != 1:
return data
return data.execute().iat[0, 0]
@classmethod
def groupby(cls, dataset, dimensions, container_type, group_type, **kwargs):
# aggregate the necessary dimensions
index_dims = [dataset.get_dimension(d, strict=True) for d in dimensions]
element_dims = [kdim for kdim in dataset.kdims if kdim not in index_dims]
group_kwargs = {}
if group_type != "raw" and issubclass(group_type, Element):
group_kwargs = dict(util.get_param_values(dataset), kdims=element_dims)
group_kwargs.update(kwargs)
group_kwargs["dataset"] = dataset.dataset
group_by = [d.name for d in index_dims]
# execute a query against the table to find the unique groups.
if ibis4():
groups = dataset.data.group_by(group_by).aggregate().execute()
else:
# groupby will be removed in Ibis 5.0
groups = dataset.data.groupby(group_by).aggregate().execute()
# filter each group based on the predicate defined.
data = [
(
tuple(s.values.tolist()),
group_type(
dataset.data.filter(
[dataset.data[k] == v for k, v in s.to_dict().items()]
),
**group_kwargs
),
)
for i, s in groups.iterrows()
]
if issubclass(container_type, NdMapping):
with item_check(False), sorted_context(False):
return container_type(data, kdims=index_dims)
else:
return container_type(data)
@classmethod
def assign(cls, dataset, new_data):
return dataset.data.mutate(**new_data)
@classmethod
def add_dimension(cls, dataset, dimension, dim_pos, values, vdim):
import ibis
data = dataset.data
if dimension.name not in data.columns:
if not isinstance(values, ibis.Expr) and not np.isscalar(values):
raise ValueError(f"Cannot assign {type(values).__name__} type as a Ibis table column, "
"expecting either ibis.Expr or scalar.")
data = data.mutate(**{dimension.name: values})
return data
@classmethod
@cached
def isscalar(cls, dataset, dim):
return (
dataset.data[dataset.get_dimension(dim, strict=True).name]
.distinct()
.count()
.compute()
== 1
)
@classmethod
def select(cls, dataset, selection_mask=None, **selection):
if selection_mask is None:
selection_mask = cls.select_mask(dataset, selection)
indexed = cls.indexed(dataset, selection)
data = dataset.data
if isinstance(selection_mask, np.ndarray):
data = cls._index_ibis_table(data)
if selection_mask.dtype == np.dtype("bool"):
selection_mask = np.where(selection_mask)[0]
data = data.filter(
data["hv_row_id__"].isin(list(map(int, selection_mask)))
)
if ibis4():
data = data.drop("hv_row_id__")
else:
# Passing a sequence of fields to `drop` will be removed in Ibis 5.0
data = data.drop(["hv_row_id__"])
elif selection_mask is not None and not (isinstance(selection_mask, list) and not selection_mask):
data = data.filter(selection_mask)
if indexed and data.count().execute() == 1 and len(dataset.vdims) == 1:
return data[dataset.vdims[0].name].execute().iloc[0]
return data
@classmethod
def select_mask(cls, dataset, selection):
import ibis
predicates = []
for dim, object in selection.items():
if isinstance(object, tuple):
object = slice(*object)
alias = dataset.get_dimension(dim).name
column = dataset.data[alias]
if isinstance(object, slice):
if object.start is not None:
# Workaround for dask issue #3392
bound = util.numpy_scalar_to_python(object.start)
predicates.append(bound <= column)
if object.stop is not None:
bound = util.numpy_scalar_to_python(object.stop)
predicates.append(column < bound)
elif isinstance(object, (set, list)):
# rowid conditions
condition = None
for id in object:
predicate = column == id
condition = (
predicate if condition is None else condition | predicate
)
if condition is not None:
predicates.append(condition)
elif callable(object):
predicates.append(object(column))
elif isinstance(object, ibis.Expr):
predicates.append(object)
else:
predicates.append(column == object)
return predicates
@classmethod
def _select_mask_neighbor(cls, dataset, selection):
"""Runs select mask and expand the True values to include its neighbors
Example
select_mask = [False, False, True, True, False, False]
select_mask_neighbor = [False, True, True, True, True, False]
"""
raise NotImplementedError
@classmethod
def sample(cls, dataset, samples=None):
import ibis
if samples is None:
samples = []
dims = dataset.dimensions()
data = dataset.data
if all(util.isscalar(s) or len(s) == 1 for s in samples):
items = [s[0] if isinstance(s, tuple) else s for s in samples]
return data[data[dims[0].name].isin(items)]
predicates = None
for sample in samples:
if util.isscalar(sample):
sample = [sample]
if not sample:
continue
predicate = None
for i, v in enumerate(sample):
p = data[dims[i].name] == ibis.literal(util.numpy_scalar_to_python(v))
if predicate is None:
predicate = p
else:
predicate &= p
if predicates is None:
predicates = predicate
else:
predicates |= predicate
return data if predicates is None else data.filter(predicates)
@classmethod
def aggregate(cls, dataset, dimensions, function, **kwargs):
import ibis.expr.operations
data = dataset.data
columns = [d.name for d in dataset.kdims if d in dimensions]
values = dataset.dimensions("value", label="name")
new = data[columns + values]
function = {
np.min: ibis.expr.operations.Min,
np.nanmin: ibis.expr.operations.Min,
np.max: ibis.expr.operations.Max,
np.nanmax: ibis.expr.operations.Max,
np.mean: ibis.expr.operations.Mean,
np.nanmean: ibis.expr.operations.Mean,
np.std: ibis.expr.operations.StandardDev,
np.nanstd: ibis.expr.operations.StandardDev,
np.sum: ibis.expr.operations.Sum,
np.nansum: ibis.expr.operations.Sum,
np.var: ibis.expr.operations.Variance,
np.nanvar: ibis.expr.operations.Variance,
len: ibis.expr.operations.Count,
}.get(function, function)
if len(dimensions):
if ibis4():
selection = new.group_by(columns)
else:
# groupby will be removed in Ibis 5.0
selection = new.groupby(columns)
if function is np.count_nonzero:
aggregation = selection.aggregate(
**{
x: ibis.expr.operations.Count(new[x], where=new[x] != 0).to_expr()
for x in new.columns
if x not in columns
}
)
else:
aggregation = selection.aggregate(
**{
x: function(new[x]).to_expr()
for x in new.columns
if x not in columns
}
)
else:
aggregation = new.aggregate(
**{x: function(new[x]).to_expr() for x in new.columns}
)
dropped = [x for x in values if x not in data.columns]
return aggregation, dropped
@classmethod
@cached
def mask(cls, dataset, mask, mask_value=np.nan):
raise NotImplementedError('Mask is not implemented for IbisInterface.')
@classmethod
@cached
def dframe(cls, dataset, dimensions):
return dataset.data[dimensions].execute()
Interface.register(IbisInterface)