/
types.py
710 lines (671 loc) · 20.5 KB
/
types.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
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
import numpy as np
from geopandas import GeoDataFrame
from semantique import exceptions
DTYPE_MAPPING = {
"b": ["binary"],
"i": ["continuous", "discrete", "ordinal", "nominal"],
"u": ["continuous", "discrete", "ordinal", "nominal"],
"f": ["continuous"],
"M": ["datetime"],
"O": ["ordinal", "nominal"],
"U": ["ordinal", "nominal"]
}
""":obj:`dict` : Mapping between numpy data types and semantique value types.
A semantique value type describes what kind of data an array contains. It differs
from the very technical, computer-oriented `numpy dtype`_ categorization, which
contains e.g. :obj:`int`, :obj:`float`, etc. Instead, the semantique value type
describes data on a more general, statistical level. Each numpy data type can
be mapped to one or semantique value types.
In this mapping, numpy data types are expressed by their `.kind` property. Each
numpy data type may be mapped to more than one semantique value type, meaning
that it can represent any of these value types.
.. _numpy dtype:
https://numpy.org/doc/stable/reference/arrays.dtypes.html
"""
TYPE_PROMOTION_MANUALS = {
"not": {
"binary": "binary",
"__preserve_labels__": 1
},
"is_missing": {
"binary": "binary",
"continuous": "binary",
"discrete": "binary",
"nominal": "binary",
"ordinal": "binary",
"coords": "binary",
"datetime": "binary",
"__preserve_labels__": 0
},
"not_missing": {
"binary": "binary",
"continuous": "binary",
"discrete": "binary",
"nominal": "binary",
"ordinal": "binary",
"coords": "binary",
"datetime": "binary",
"__preserve_labels__": 0
},
"absolute": {
"continuous": "continuous",
"discrete": "discrete",
"__preserve_labels__": 0
},
"ceiling": {
"continuous": "discrete",
"discrete": "discrete",
"__preserve_labels__": 0
},
"cosine": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"cosecant": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"cotangent": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"cube_root": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"exponential": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"floor": {
"continuous": "discrete",
"discrete": "discrete",
"__preserve_labels__": 0
},
"natural_logarithm": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"secant": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"sine": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"square_root": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"tangent": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"to_degrees": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"to_radians": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"add": {
"binary": {"binary": "discrete"},
"continuous": {"continuous": "continuous", "discrete": "continuous"},
"discrete": {"continuous": "continuous", "discrete": "discrete"},
"__preserve_labels__": 0
},
"divide": {
"continuous": {"continuous": "continuous", "discrete": "continuous"},
"discrete": {"continuous": "continuous", "discrete": "continuous"},
"__preserve_labels__": 0
},
"multiply": {
"continuous": {"continuous": "continuous", "discrete": "continuous"},
"discrete": {"continuous": "continuous", "discrete": "discrete"},
"__preserve_labels__": 0
},
"power": {
"continuous": {"continuous": "continuous", "discrete": "continuous"},
"discrete": {"continuous": "continuous", "discrete": "discrete"},
"__preserve_labels__": 0
},
"subtract": {
"continuous": {"continuous": "continuous", "discrete": "continuous"},
"discrete": {"continuous": "continuous", "discrete": "discrete"},
"__preserve_labels__": 0
},
"normalized_difference": {
"continuous": {"continuous": "continuous", "discrete": "continuous"},
"discrete": {"continuous": "continuous", "discrete": "continuous"},
"__preserve_labels__": 0
},
"and": {
"binary": {"binary": "binary"},
"__preserve_labels__": 1
},
"or": {
"binary": {"binary": "binary"},
"__preserve_labels__": 1
},
"exclusive_or": {
"binary": {"binary": "binary"},
"__preserve_labels__": 1
},
"equal": {
"binary": {"binary": "binary"},
"continuous": {"continuous": "binary", "discrete": "binary"},
"discrete": {"continuous": "binary", "discrete": "binary"},
"nominal": {"nominal": "binary"},
"ordinal": {"ordinal": "binary"},
"coords": {"coords": "binary"},
"datetime": {"datetime": "binary"},
"__preserve_labels__": 0
},
"in": {
"binary": {"binary": "binary"},
"continuous": {"continuous": "binary", "discrete": "binary"},
"discrete": {"continuous": "binary", "discrete": "binary"},
"nominal": {"nominal": "binary"},
"ordinal": {"ordinal": "binary"},
"coords": {"coords": "binary"},
"datetime": {"datetime": "binary"},
"__preserve_labels__": 0
},
"not_equal": {
"binary": {"binary": "binary"},
"continuous": {"continuous": "binary", "discrete": "binary"},
"discrete": {"continuous": "binary", "discrete": "binary"},
"nominal": {"nominal": "binary"},
"ordinal": {"ordinal": "binary"},
"coords": {"coords": "binary"},
"datetime": {"datetime": "binary"},
"__preserve_labels__": 0
},
"not_in": {
"binary": {"binary": "binary"},
"continuous": {"continuous": "binary", "discrete": "binary"},
"discrete": {"continuous": "binary", "discrete": "binary"},
"nominal": {"nominal": "binary"},
"ordinal": {"ordinal": "binary"},
"coords": {"coords": "binary"},
"datetime": {"datetime": "binary"},
"__preserve_labels__": 0
},
"greater": {
"binary": {"binary": "binary",},
"continuous": {"continuous": "binary", "discrete": "binary"},
"discrete": {"continuous": "binary", "discrete": "binary"},
"ordinal": {"ordinal": "binary"},
"datetime": {"datetime": "binary"},
"__preserve_labels__": 0
},
"greater_equal": {
"binary": {"binary": "binary",},
"continuous": {"continuous": "binary", "discrete": "binary"},
"discrete": {"continuous": "binary", "discrete": "binary"},
"ordinal": {"ordinal": "binary"},
"datetime": {"datetime": "binary"},
"__preserve_labels__": 0
},
"less": {
"binary": {"binary": "binary",},
"continuous": {"continuous": "binary", "discrete": "binary"},
"discrete": {"continuous": "binary", "discrete": "binary"},
"ordinal": {"ordinal": "binary"},
"datetime": {"datetime": "binary"},
"__preserve_labels__": 0
},
"less_equal": {
"binary": {"binary": "binary",},
"continuous": {"continuous": "binary", "discrete": "binary"},
"discrete": {"continuous": "binary", "discrete": "binary"},
"ordinal": {"ordinal": "binary"},
"datetime": {"datetime": "binary"},
"__preserve_labels__": 0
},
"intersects": {
"coords": {"coords": "binary", "geometry": "binary"},
"__preserve_labels__": 0
},
"after": {
"datetime": {"datetime": "binary"},
"__preserve_labels__": 0
},
"before": {
"datetime": {"datetime": "binary"},
"__preserve_labels__": 0
},
"during": {
"datetime": {"datetime": "binary"},
"__preserve_labels__": 0
},
"mean": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"median": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"mode": {
"binary": "binary",
"continuous": "continuous",
"discrete": "discrete",
"nominal": "nominal",
"ordinal": "ordinal",
"coords": "coords",
"datetime": "datetime",
"__preserve_labels__": 1
},
"max": {
"binary": "binary",
"continuous": "continuous",
"discrete": "discrete",
"ordinal": "ordinal",
"datetime": "datetime",
"__preserve_labels__": 1
},
"min": {
"binary": "binary",
"continuous": "continuous",
"discrete": "discrete",
"ordinal": "ordinal",
"datetime": "datetime",
"__preserve_labels__": 1
},
"range": {
"continuous": "continuous",
"discrete": "discrete",
"datetime": "continuous",
"__preserve_labels__": 0
},
"n": {
"binary": "discrete",
"continuous": "discrete",
"discrete": "discrete",
"nominal": "discrete",
"ordinal": "discrete",
"coords": "discrete",
"datetime": "discrete",
"__preserve_labels__": 0
},
"product": {
"continuous": "continuous",
"discrete": "discrete",
"__preserve_labels__": 0
},
"standard_deviation": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"sum": {
"binary": "binary",
"continuous": "continuous",
"discrete": "discrete",
"__preserve_labels__": 0
},
"variance": {
"continuous": "continuous",
"discrete": "continuous",
"__preserve_labels__": 0
},
"all": {
"binary": "binary",
"__preserve_labels__": 1
},
"any": {
"binary": "binary",
"__preserve_labels__": 1
},
"none": {
"binary": "binary",
"__preserve_labels__": 1
},
"count": {
"binary": "discrete",
"__preserve_labels__": 0
},
"percentage": {
"binary": "continuous",
"__preserve_labels__": 0
},
"first": {
"binary": "binary",
"continuous": "continuous",
"discrete": "discrete",
"nominal": "nominal",
"ordinal": "ordinal",
"datetime": "datetime",
"__preserve_labels__": 1
},
"last": {
"binary": "binary",
"continuous": "continuous",
"discrete": "discrete",
"nominal": "nominal",
"ordinal": "ordinal",
"coords": "coords",
"datetime": "datetime",
"__preserve_labels__": 1
},
"assign": {
"binary": {
"binary": "binary",
"continuous": "continuous",
"discrete": "discrete",
"nominal": "nominal",
"ordinal": "ordinal",
"coords": "coords",
"datetime": "datetime"
},
"continuous": {
"binary": "binary",
"continuous": "continuous",
"discrete": "discrete",
"nominal": "nominal",
"ordinal": "ordinal",
"coords": "coords",
"datetime": "datetime"
},
"discrete": {
"binary": "binary",
"continuous": "continuous",
"discrete": "discrete",
"nominal": "nominal",
"ordinal": "ordinal",
"coords": "coords",
"datetime": "datetime"
},
"nominal": {
"binary": "binary",
"continuous": "continuous",
"discrete": "discrete",
"nominal": "nominal",
"ordinal": "ordinal",
"coords": "coords",
"datetime": "datetime"
},
"ordinal": {
"binary": "binary",
"continuous": "continuous",
"discrete": "discrete",
"nominal": "nominal",
"ordinal": "ordinal",
"coords": "coords",
"datetime": "datetime"
},
"datetime": {
"binary": "binary",
"continuous": "continuous",
"discrete": "discrete",
"nominal": "nominal",
"ordinal": "ordinal",
"coords": "coords",
"datetime": "datetime"
},
"coords": {
"binary": "binary",
"continuous": "continuous",
"discrete": "discrete",
"nominal": "nominal",
"ordinal": "ordinal",
"coords": "coords",
"datetime": "datetime"
},
"__preserve_labels__": 2
},
"assign_at": {
"binary": {"binary": "binary"},
"continuous": {"continuous": "continuous", "discrete": "continuous"},
"discrete": {"continuous": "continuous", "discrete": "discrete"},
"nominal": {"nominal": "nominal"},
"ordinal": {"ordinal": "ordinal"},
"datetime": {"datetime": "datetime"},
"coords": {"coords": "coords"},
"__preserve_labels__": 0
}
}
""":obj:`dict` : Type promotion manuals for all built-in operators and reducers.
Whenever applying actions to an array, its value type might change. For
example, when evaluating an expression (e.g. when evaluating an expression
involving a comparison operator the resulting values are always binary) or
applying a reducer (e.g. when counting the number of "true" values in a binary
array the resulting values are discrete). This is called type promotion.
For each built-in operator and reducer function, this dictionary stores a
manual that defines which value types are accepted as input, and what the value
type of the output should be.
For reducers and univariate operators, a type promotion manual is always a
dictionary with as keys the supported input value types and as values the
output value type. For bivariate operators, the type promotion manuals have
an extra layer. The first layer of keys refers to the value type of the first
operand and the second layer of keys to the value type of the second operand.
"""
def get_value_type(x):
"""Determine the value type of an object.
A semantique value type describes what kind of data an array contains. It
differs from the very technical, computer-oriented `numpy dtype`_
categorization, which contains e.g. :obj:`int`, :obj:`float`, etc. Instead,
the semantique value type describes data on a more general, statistical
level. Currently it makes a distinction between five main value types:
``continuous`` and ``discrete`` for quantitative data and ``nominal``,
``ordinal`` and ``binary`` for qualitative data. Additional value
types exist for spatio-temporal data: ``datetime`` for timestamps,
``coords`` for spatial coordinate tuples, and ``geometry`` for spatial
geometries stored in :obj:`geopandas.GeoDataFrame` objects.
Parameters
----------
x:
Object to determine the value type of.
Returns
-------
:obj:`list`
A list containing the determined value type. If the object may be one of
multiple value types this list will have multiple elements. If no value
type could be determined for the given object, the function will return
:obj:`None`.
.. _numpy dtype:
https://numpy.org/doc/stable/reference/arrays.dtypes.html
"""
try:
vtype = x.sq.value_type
except AttributeError:
try:
vtype = x.obj.sq.value_type
except AttributeError:
if isinstance(x, GeoDataFrame):
vtype = "geometry"
else:
x = np.array(x)
vtype = None
if vtype is None:
dtype = x.dtype.kind
try:
vtype = DTYPE_MAPPING[dtype]
except KeyError:
pass
else:
if not isinstance(vtype, (list, tuple)):
vtype = [vtype]
return vtype
def get_value_labels(x):
"""Obtain the value labels of an object.
Value labels are character strings that describe the meaning of the values in
an array. They are mainly used for categorical data, in which integer
indices are the value in the array, and the labels store the name of each
category.
Parameters
----------
x:
Object to obtain the value labels from.
Returns
--------
:obj:`dict`
The obtained value labels as a dictionary containing value-label pairs.
If the object does not have value labels stored, the function will return
:obj:`None`.
"""
try:
vlabs = x.sq.value_labels
except AttributeError:
try:
vlabs = x.obj.sq.value_labels
except AttributeError:
vlabs = None
return vlabs
class TypePromoter:
"""Worker that takes care of promoting value types during an operation.
Whenever applying an operation to an array, its value type might change.
For example, when evaluating an expression (e.g. when evaluating an expression
involving a comparison operator the resulting values are always binary) or
applying a reducer (e.g. when counting the number of "true" values in a binary
array the resulting values are discrete). This is called type promotion.
This worker takes care of the type promotion during a specified operation. It
can check if the value types of the operands are supported by the operation,
and if yes, determine the value type of the output of the operation.
Parameters
----------
*operands:
The operands of the operation. Reducer functions and univariate operators
always have a single operand, while bivariate operators have two operands.
function : :obj:`str`, optional
Name of the operation.
manual : :obj:`dict`, optional
Type promotion manual of the operation. If :obj:`None`, the worker will try
to obtain the manual from the built-in :data:`TYPE_PROMOTION_MANUALS`
dictionary, using the operation name (see the ``function`` parameter) as
search key.
"""
def __init__(self, *operands, function = None, manual = None):
self._operands = operands
self._function = function
self._manual = manual
self._input_types = None
self._output_type = None
self._input_labels = None
self._output_labels = None
@property
def manual(self):
""":obj:`dict`: The type promotion manual of the operation."""
if self._manual is None:
try:
self._manual = TYPE_PROMOTION_MANUALS[self._function]
except KeyError:
pass
return self._manual
@property
def input_types(self):
""":obj:`list`: The value types of the operands."""
if self._input_types is None:
self._input_types = [get_value_type(x) for x in self._operands]
return self._input_types
@property
def output_type(self):
""":obj:`str`: The value type of the output."""
if self._output_type is None:
self.check()
return self._output_type
@property
def input_labels(self):
""":obj:`list` of `:obj:`dict`: The value labels of the operands."""
if self._input_labels is None:
self._input_labels = [get_value_labels(x) for x in self._operands]
return self._input_labels
@property
def output_labels(self):
""":obj:`dict`: The value labels of the output."""
if self._output_labels is None:
try:
preserve_labels = self._manual["__preserve_labels__"]
except KeyError:
preserve_labels = 0
if preserve_labels:
self._output_labels = self.input_labels[preserve_labels - 1]
return self._output_labels
def check(self):
"""Check if the operation supports the operand value type(s).
Specific operations may only be applicable to specific value types. For
example, :func:`semantique.processor.reducers.any_` is only supported for
arrays containing binary values, and
:func:`semantique.processor.operators.sum_` is only supported for operands
that are both quantitative (i.e. continuous or discrete).
This method obtains the value type of the operand/operands and uses the type
promotion manual to determine if the operation supports this value type/the
combination of these value types.
Raises
-------
:obj:`exceptions.InvalidValueTypeError`
If the operation does not support the operand value type(s).
:obj:`ValueError`
If the type promotion manual is missing.
"""
intypes = self.input_types
outtype = None
manual = self.manual
if manual is None:
raise ValueError(
f"No type promotion manual defined for function '{self._function}'"
)
if len(intypes) == 1:
for x in intypes[0]:
try:
outtype = manual[x]
break
except KeyError:
continue
else:
if len(intypes[0]) == 1 and intypes[0][0] in intypes[1]:
try:
xtype = intypes[0][0]
outtype = manual[xtype][xtype]
except KeyError:
pass
if outtype is None:
combs = [(x, y) for x in intypes[0] for y in intypes[1]]
for ref in combs:
try:
outtype = manual[ref[0]][ref[1]]
break
except KeyError:
continue
if outtype is None:
raise exceptions.InvalidValueTypeError(
f"Unsupported operand value type(s) for '{self._function}': {intypes}"
)
self._output_type = outtype
def promote(self, obj):
"""Promote the value type of the operation output.
Parameters
----------
obj : :obj:`xarray.DataArray`
The output of the operation.
Returns
--------
obj : :obj:`xarray.DataArray`
The same object with an updated value type property.
"""
# Set type.
obj.sq.value_type = self.output_type
# Set labels.
labs = self.output_labels
if labs is None:
del obj.sq.value_labels
else:
obj.sq.value_labels = labs
return obj