-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathutils.py
More file actions
499 lines (453 loc) · 15.8 KB
/
Copy pathutils.py
File metadata and controls
499 lines (453 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
from types import BuiltinFunctionType, FunctionType, ModuleType
from ... import (
backend,
binary,
config,
indexbinary,
indexunary,
monoid,
op,
select,
semiring,
unary,
)
from ...dtypes import UINT64, lookup_dtype, unify
from ..expr import InfixExprBase
from .base import (
_SS_OPERATORS,
OpBase,
OpPath,
ParameterizedUdf,
TypedOpBase,
_builtin_to_op,
_hasop,
find_opclass,
)
from .binary import BinaryOp
from .indexbinary import IndexBinaryOp
from .indexunary import IndexUnaryOp
from .monoid import Monoid
from .select import SelectOp
from .semiring import Semiring
from .unary import UnaryOp
# Now initialize all the things!
try:
UnaryOp._initialize()
IndexUnaryOp._initialize()
SelectOp._initialize()
BinaryOp._initialize()
Monoid._initialize()
Semiring._initialize()
# IndexBinaryOp has no built-ins, but ``_initialize`` still needs to
# set ``cls._initialized = True``. Without that, user ``register_new``
# calls hit the "not initialized" branch and mistakenly add user op
# names to ``_STANDARD_OPERATOR_NAMES``. Pickle then emits a string
# reference that won't resolve in a fresh process. Cross-process pickle
# for user-registered IBOs (and bound IBOs derived from them) requires
# the tuple-form ``__reduce__`` that re-registers the function in the
# child.
IndexBinaryOp._initialize()
except Exception: # pragma: no cover (debug)
# Exceptions here can often get ignored by Python
import traceback
traceback.print_exc()
raise
def get_typed_op(op, dtype, dtype2=None, *, is_left_scalar=False, is_right_scalar=False, kind=None):
if isinstance(op, OpBase):
# UDTs always get compiled
if op._is_udt:
return op._compile_udt(dtype, dtype2)
# Single dtype is simple lookup
if dtype2 is None:
return op[dtype]
# Handle special cases such as first and second (may have UDTs)
if op._custom_dtype is not None and (rv := op._custom_dtype(op, dtype, dtype2)) is not None:
return rv
# Generic case: try to unify the two dtypes
try:
return op[
unify(dtype, dtype2, is_left_scalar=is_left_scalar, is_right_scalar=is_right_scalar)
]
except (TypeError, AttributeError):
# Failure to unify implies a dtype is UDT; some builtin operators can handle UDTs
if op.is_positional:
return op[UINT64]
if op._udt_types is None:
raise
return op._compile_udt(dtype, dtype2)
if isinstance(op, ParameterizedUdf):
op = op() # Use default parameters of parameterized UDFs
return get_typed_op(
op,
dtype,
dtype2,
is_left_scalar=is_left_scalar,
is_right_scalar=is_right_scalar,
kind=kind,
)
if isinstance(op, TypedOpBase):
return op
from .agg import Aggregator, TypedAggregator
if isinstance(op, Aggregator):
# agg._any_dtype basically serves the same purpose as op._custom_dtype
if op._any_dtype is not None and op._any_dtype is not True:
return op[op._any_dtype]
return op[dtype]
if isinstance(op, TypedAggregator):
return op
if isinstance(op, str):
if kind == "unary":
op = unary_from_string(op)
elif kind == "select":
op = select_from_string(op)
elif kind == "binary":
op = binary_from_string(op)
elif kind == "monoid":
op = monoid_from_string(op)
elif kind == "semiring":
op = semiring_from_string(op)
elif kind == "binary|aggregator":
try:
op = binary_from_string(op)
except ValueError:
try:
op = aggregator_from_string(op)
except ValueError:
raise ValueError(
f"Unknown binary or aggregator string: {op!r}. Example usage: '+[int]'"
) from None
else:
raise ValueError(
f"Unable to get op from string {op!r}. `kind=` argument must be provided as "
'"unary", "binary", "monoid", "semiring", "indexunary", "select", '
'or "binary|aggregator".'
)
return get_typed_op(
op,
dtype,
dtype2,
is_left_scalar=is_left_scalar,
is_right_scalar=is_right_scalar,
kind=kind,
)
if isinstance(op, FunctionType):
if kind == "unary":
op = UnaryOp.register_anonymous(op, is_udt=True)
return op._compile_udt(dtype, dtype2)
if kind.startswith("binary"):
op = BinaryOp.register_anonymous(op, is_udt=True)
return op._compile_udt(dtype, dtype2)
if isinstance(op, BuiltinFunctionType) and op in _builtin_to_op:
return get_typed_op(
_builtin_to_op[op],
dtype,
dtype2,
is_left_scalar=is_left_scalar,
is_right_scalar=is_right_scalar,
kind=kind,
)
raise TypeError(f"Unable to get typed operator from object with type {type(op)}")
def _get_typed_op_from_exprs(op, left, right, *, kind=None):
if isinstance(left, InfixExprBase):
left_op = _get_typed_op_from_exprs(op, left.left, left.right, kind=kind)
left_dtype = left_op.type
else:
left_op = None
left_dtype = left.dtype
if isinstance(right, InfixExprBase):
right_op = _get_typed_op_from_exprs(op, right.left, right.right, kind=kind)
if right_op is left_op:
return right_op
right_dtype = right_op.type2
else:
right_dtype = right.dtype
return get_typed_op(
op,
left_dtype,
right_dtype,
is_left_scalar=left._is_scalar,
is_right_scalar=right._is_scalar,
kind=kind,
)
def get_semiring(monoid, binaryop, name=None):
"""Get or create a Semiring object from a monoid and binaryop.
If either are typed, then the returned semiring will also be typed.
See Also
--------
semiring.register_anonymous
semiring.register_new
semiring.from_string
"""
monoid, opclass = find_opclass(monoid)
switched = False
if opclass == "BinaryOp" and monoid.monoid is not None:
switched = True
monoid = monoid.monoid
elif opclass != "Monoid":
raise TypeError(f"Expected a Monoid for the monoid argument. Got type: {type(monoid)}")
binaryop, opclass = find_opclass(binaryop)
if opclass == "Monoid":
if switched:
raise TypeError(
"Got a BinaryOp for the monoid argument and a Monoid for the binaryop argument. "
"Are the arguments switched? Hint: you can do `mymonoid.binaryop` to get the "
"binaryop from a monoid."
)
binaryop = binaryop.binaryop
elif opclass != "BinaryOp":
raise TypeError(
f"Expected a BinaryOp for the binaryop argument. Got type: {type(binaryop)}"
)
if isinstance(monoid, Monoid):
monoid_type = None
else:
monoid_type = monoid.type
monoid = monoid.parent
if isinstance(binaryop, BinaryOp):
binary_type = None
else:
binary_type = binaryop.type
binaryop = binaryop.parent
if monoid._anonymous or binaryop._anonymous:
rv = Semiring.register_anonymous(monoid, binaryop, name=name)
else:
*monoid_prefix, monoid_name = monoid.name.rsplit(".", 1)
*binary_prefix, binary_name = binaryop.name.rsplit(".", 1)
if (
monoid_prefix
and binary_prefix
and monoid_prefix == binary_prefix
or config.get("mapnumpy")
and (
monoid_prefix == ["numpy"]
and not binary_prefix
or binary_prefix == ["numpy"]
and not monoid_prefix
)
or backend == "suitesparse"
and binary_name in _SS_OPERATORS
):
canonical_name = (
".".join(monoid_prefix or binary_prefix) + f".{monoid_name}_{binary_name}"
)
else:
canonical_name = f"{monoid.name}_{binaryop.name}".replace(".", "_")
if name is None:
name = canonical_name
module, funcname = Semiring._remove_nesting(canonical_name, strict=False)
rv = (
getattr(module, funcname)
if funcname in module.__dict__ or funcname in module._delayed
else getattr(module, "_deprecated", {}).get(funcname)
)
if rv is None and name != canonical_name:
module, funcname = Semiring._remove_nesting(name, strict=False)
rv = (
getattr(module, funcname)
if funcname in module.__dict__ or funcname in module._delayed
else getattr(module, "_deprecated", {}).get(funcname)
)
if rv is None:
rv = Semiring.register_new(canonical_name, monoid, binaryop)
elif rv.monoid is not monoid or rv.binaryop is not binaryop: # pragma: no cover
# It's not the object we expect (can this happen?)
rv = Semiring.register_anonymous(monoid, binaryop, name=name)
if name != canonical_name:
module, funcname = Semiring._remove_nesting(name, strict=False)
if not _hasop(module, funcname): # pragma: no branch (safety)
setattr(module, funcname, rv)
if binary_type is not None:
return rv[binary_type]
if monoid_type is not None:
return rv[monoid_type]
return rv
unary.register_new = UnaryOp.register_new
unary.register_anonymous = UnaryOp.register_anonymous
indexbinary.register_new = IndexBinaryOp.register_new
indexbinary.register_anonymous = IndexBinaryOp.register_anonymous
indexunary.register_new = IndexUnaryOp.register_new
indexunary.register_anonymous = IndexUnaryOp.register_anonymous
select.register_new = SelectOp.register_new
select.register_anonymous = SelectOp.register_anonymous
binary.register_new = BinaryOp.register_new
binary.register_anonymous = BinaryOp.register_anonymous
monoid.register_new = Monoid.register_new
monoid.register_anonymous = Monoid.register_anonymous
semiring.register_new = Semiring.register_new
semiring.register_anonymous = Semiring.register_anonymous
semiring.get_semiring = get_semiring
select._binary_to_select.update(
{
binary.eq: select.valueeq,
binary.ne: select.valuene,
binary.le: select.valuele,
binary.lt: select.valuelt,
binary.ge: select.valuege,
binary.gt: select.valuegt,
binary.iseq: select.valueeq,
binary.isne: select.valuene,
binary.isle: select.valuele,
binary.islt: select.valuelt,
binary.isge: select.valuege,
binary.isgt: select.valuegt,
}
)
_builtin_to_op.update(
{
abs: unary.abs,
max: binary.max,
min: binary.min,
# Maybe someday: all, any, pow, sum
}
)
_str_to_unary = {
"-": unary.ainv,
"~": unary.lnot,
}
_str_to_select = {
"<": select.valuelt,
">": select.valuegt,
"<=": select.valuele,
">=": select.valuege,
"!=": select.valuene,
"==": select.valueeq,
"col<=": select.colle,
"col>": select.colgt,
"row<=": select.rowle,
"row>": select.rowgt,
"index<=": select.indexle,
"index>": select.indexgt,
}
_str_to_binary = {
"<": binary.lt,
">": binary.gt,
"<=": binary.le,
">=": binary.ge,
"!=": binary.ne,
"==": binary.eq,
"+": binary.plus,
"-": binary.minus,
"*": binary.times,
"/": binary.truediv,
"//": "floordiv",
"%": "numpy.mod",
"**": binary.pow,
"&": binary.land,
"|": binary.lor,
"^": binary.lxor,
}
_str_to_monoid = {
"==": monoid.eq,
"+": monoid.plus,
"*": monoid.times,
"&": monoid.land,
"|": monoid.lor,
"^": monoid.lxor,
}
def _from_string(string, module, mapping, example):
s = string.lower().strip()
base, *dtype = s.split("[")
if len(dtype) > 1:
name = module.__name__.split(".")[-1]
raise ValueError(
f'Bad {name} string: {string!r}. Contains too many "[". Example usage: {example!r}'
)
if dtype:
dtype = dtype[0]
if not dtype.endswith("]"):
name = module.__name__.split(".")[-1]
raise ValueError(
f'Bad {name} string: {string!r}. Datatype specification does not end with "]". '
f"Example usage: {example!r}"
)
dtype = lookup_dtype(dtype[:-1])
if "]" in base:
name = module.__name__.split(".")[-1]
raise ValueError(
f'Bad {name} string: {string!r}. "]" not matched by "[". Example usage: {example!r}'
)
if base in mapping:
op = mapping[base]
if isinstance(op, str):
op = mapping[base] = module.from_string(op)
elif hasattr(module, base):
op = getattr(module, base)
elif hasattr(module, "numpy") and hasattr(module.numpy, base):
op = getattr(module.numpy, base)
else:
*paths, attr = base.split(".")
op = None
cur = module
for path in paths:
cur = getattr(cur, path, None)
if not isinstance(cur, (OpPath, ModuleType)):
cur = None
break
op = getattr(cur, attr, None)
if op is None:
name = module.__name__.split(".")[-1]
raise ValueError(f"Unknown {name} string: {string!r}. Example usage: {example!r}")
if dtype:
op = op[dtype]
return op
def unary_from_string(string):
return _from_string(string, unary, _str_to_unary, "abs[int]")
def indexunary_from_string(string):
# "select" is a variant of IndexUnary, so the string abbreviations in
# _str_to_select are appropriate to reuse here
return _from_string(string, indexunary, _str_to_select, "row_index")
def select_from_string(string):
return _from_string(string, select, _str_to_select, "tril")
def binary_from_string(string):
return _from_string(string, binary, _str_to_binary, "+[int]")
def monoid_from_string(string):
return _from_string(string, monoid, _str_to_monoid, "+[int]")
def semiring_from_string(string):
split = string.split(".")
if len(split) == 1:
try:
return _from_string(string, semiring, {}, "min.+[int]")
except Exception:
pass
if len(split) != 2:
raise ValueError(
f"Bad semiring string: {string!r}. "
'The monoid and binaryop should be separated by exactly one period, ".". '
"Example usage: min.+[int]"
)
cur_monoid = monoid_from_string(split[0])
cur_binary = binary_from_string(split[1])
return get_semiring(cur_monoid, cur_binary)
def op_from_string(string):
for func in [
# Note: order matters here
unary_from_string,
binary_from_string,
monoid_from_string,
semiring_from_string,
indexunary_from_string,
select_from_string,
aggregator_from_string,
]:
try:
return func(string)
except Exception:
pass
raise ValueError(f"Unknown op string: {string!r}. Example usage: 'abs[int]'")
unary.from_string = unary_from_string
indexunary.from_string = indexunary_from_string
select.from_string = select_from_string
binary.from_string = binary_from_string
monoid.from_string = monoid_from_string
semiring.from_string = semiring_from_string
op.from_string = op_from_string
_str_to_agg = {
"+": "sum",
"*": "prod",
"&": "all",
"|": "any",
}
def aggregator_from_string(string):
return _from_string(string, agg, _str_to_agg, "sum[int]")
from ... import agg # noqa: E402 isort:skip
agg.from_string = aggregator_from_string