-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexpr.py
More file actions
560 lines (468 loc) · 20.7 KB
/
Copy pathexpr.py
File metadata and controls
560 lines (468 loc) · 20.7 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
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
import numpy as np
from .. import backend
from ..dtypes import _INDEX
from . import lib, utils
from .descriptor import lookup as descriptor_lookup
from .utils import _CArray, output_type
class _AllIndices:
__slots__ = "_carg", "name", "_expr_name"
def __init__(self):
self._carg = lib.GrB_ALL
self.name = "GrB_ALL"
self._expr_name = ":"
def __reduce__(self):
return "_ALL_INDICES"
_ALL_INDICES = _AllIndices()
class AxisIndex:
__slots__ = "size", "index", "cscalar", "dimsize"
def __init__(self, size, index, cscalar, dimsize):
self.size = size
self.index = index
self.cscalar = cscalar
self.dimsize = dimsize
@property
def _carg(self):
return self.index._carg
@property
def name(self):
return self.index.name
@property
def _expr_name(self):
if self.size is None:
return f"{self.index.value}"
idx = self._py_index()
if type(idx) is slice:
rv = f"{'' if idx.start is None else idx.start}:{'' if idx.stop is None else idx.stop}"
if idx.step is not None:
return f"{rv}:{idx.step}"
return rv
if idx.size < 6:
return f"[{', '.join(map(str, idx))}]"
return f"[{', '.join(map(str, idx[:3]))}, ...]"
def _py_index(self):
"""Convert resolved index back into a valid Python index."""
if self.size is None:
return self.index.value
if self.index is _ALL_INDICES:
return slice(None)
if backend != "suitesparse":
return self.index.array
from .slice import _gxb_backwards, _gxb_range, _gxb_stride
if self.cscalar is _gxb_backwards:
start, stop, step = self.index.array.tolist()
size = self.dimsize
stop -= size + 1
step = -step
elif self.cscalar is _gxb_range:
start, stop = self.index.array.tolist()
step = None
stop += 1
elif self.cscalar is _gxb_stride:
start, stop, step = self.index.array.tolist()
stop += 1
else:
return self.index.array
if (
start == 0
and (step is None or step > 0)
or start == self.dimsize - 1
and step is not None
and step < 0
):
start = None
if (
stop == self.dimsize
and (step is None or step > 0)
or stop == -self.dimsize - 1
and step is not None
and step < 0
):
stop = None
return slice(start, stop, step)
class IndexerResolver:
__slots__ = "obj", "indices", "shape", "__weakref__"
def __init__(self, obj, indices):
self.obj = obj
if indices is Ellipsis:
from .scalar import _as_scalar
from .vector import Vector
if type(obj) is Vector:
self.indices = [
AxisIndex(
obj._size,
_ALL_INDICES,
_as_scalar(obj._size, _INDEX, is_cscalar=True),
obj._size,
)
]
self.shape = (obj._size,)
else:
self.indices = [
AxisIndex(
obj._nrows,
_ALL_INDICES,
_as_scalar(obj._nrows, _INDEX, is_cscalar=True),
obj._nrows,
),
AxisIndex(
obj._ncols,
_ALL_INDICES,
_as_scalar(obj._ncols, _INDEX, is_cscalar=True),
obj._ncols,
),
]
self.shape = (obj._nrows, obj._ncols)
else:
self.indices = self.parse_indices(indices, obj.shape)
self.shape = tuple(index.size for index in self.indices if index.size is not None)
@property
def is_single_element(self):
return not self.shape
@property
def py_indices(self):
if self.obj.ndim > 1:
return tuple(index._py_index() for index in self.indices)
return self.indices[0]._py_index()
def parse_indices(self, indices, shape):
"""Returns
-------
[(rows, rowsize), (cols, colsize)] for Matrix
[(idx, idx_size)] for Vector
Within each tuple, if the index is of type int, the size will be None
"""
if len(shape) == 1:
if type(indices) is tuple:
raise TypeError(f"Index for {type(self.obj).__name__} cannot be a tuple")
# Convert to tuple for consistent processing
indices = (indices,)
elif type(indices) is not tuple or len(indices) != 2:
raise TypeError(f"Index for {type(self.obj).__name__} must be a 2-tuple")
out = []
for i, idx in enumerate(indices):
typ = output_type(idx)
if typ is tuple:
raise TypeError(
f"Index in position {i} cannot be a tuple; must use slice or list or int"
)
out.append(self.parse_index(idx, typ, shape[i]))
return out
def parse_index(self, index, typ, size):
from .scalar import _as_scalar
if np.issubdtype(typ, np.integer):
if index >= size:
raise IndexError(f"Index out of range: index={index}, size={size}")
if np.issubdtype(typ, np.signedinteger) and index < 0:
index = index + size
if index < 0:
raise IndexError(f"Index out of range: index={index - size}, size={size}")
return AxisIndex(None, _as_scalar(int(index), _INDEX, is_cscalar=True), None, size)
if typ is list:
pass
elif typ is slice:
from .slice import slice_to_index
return slice_to_index(index, size)
elif typ is np.ndarray:
if len(index.shape) != 1:
raise TypeError(f"Invalid number of dimensions for index: {len(index.shape)}")
if not np.issubdtype(index.dtype, np.integer):
raise TypeError(f"Invalid dtype for index: {index.dtype}")
if np.issubdtype(index.dtype, np.signedinteger):
is_negative = index < 0
if is_negative.any():
index = np.where(is_negative, index + size, index)
if (index < 0).any():
bad_index = index[index < 0][0] - size
raise IndexError(f"Index out of range: index={bad_index}, size={size}")
return AxisIndex(
len(index), _CArray(index), _as_scalar(len(index), _INDEX, is_cscalar=True), size
)
else:
from .scalar import Scalar
if typ is Scalar:
if not np.issubdtype(index.dtype.np_type, np.integer):
raise TypeError(f"An integer is required for indexing. Got: {index.dtype}")
value = int(index)
if np.issubdtype(index.dtype.np_type, np.signedinteger) and value < 0:
value = value + size
if value < 0:
raise IndexError(f"Index out of range: index={value - size}, size={size}")
return AxisIndex(None, _as_scalar(value, _INDEX, is_cscalar=True), None, size)
from .matrix import Matrix, TransposedMatrix
from .vector import Vector
if typ is Vector or typ is Matrix:
raise TypeError(
f"Invalid type for index: {typ.__name__}.\n"
"If you want to apply a mask, perhaps do something like "
f"`x.dup(mask={index.name}.S)`.\n"
"If you want to assign with a mask, perhaps do something like "
f"`x(mask={index.name}.S) << value`."
)
if typ is TransposedMatrix:
raise TypeError(f"Invalid type for index: {typ.__name__}.")
try:
index = list(index)
except Exception:
from .mask import Mask
if isinstance(index, Mask):
raise TypeError(
f"Invalid type for index: {typ.__name__}.\n"
"If you want to apply a mask, perhaps do something like "
f"`x.dup(mask={index.name})`.\n"
"If you want to assign with a mask, perhaps do something like "
f"`x(mask={index.name}) << value`."
) from None
raise TypeError(
f"Invalid type for index: {typ}; unable to convert to list"
) from None
return self.parse_index(np.array(index), np.ndarray, size)
def get_index(self, dim):
"""Return a new IndexerResolver with index for the selected dimension."""
rv = object.__new__(IndexerResolver)
rv.obj = self.obj
rv.indices = (self.indices[dim],)
return rv
class Assigner:
__slots__ = "updater", "resolved_indexes", "is_submask", "__weakref__"
def __init__(self, updater, resolved_indexes, *, is_submask):
# We could check here whether mask dimensions match index dimensions.
# We could also check for valid `updater.kwargs` if `resolved_indexes.is_single_element`.
self.updater = updater
self.resolved_indexes = resolved_indexes
self.is_submask = is_submask
if updater.kwargs.get("input_mask") is not None:
raise TypeError("`input_mask` argument may only be used for extract")
def update(self, expr):
# Occurs when user calls `C[index](...).update(expr)` or `C(...)[index].update(expr)`
self.updater._setitem(self.resolved_indexes, expr, is_submask=self.is_submask)
def __lshift__(self, expr):
# Occurs when user calls `C[index](...) << expr` or `C(...)[index] << expr`
self.updater._setitem(self.resolved_indexes, expr, is_submask=self.is_submask)
def __eq__(self, other):
raise TypeError(f"__eq__ not defined for objects of type {type(self)}.")
__hash__ = None
def __bool__(self):
raise TypeError(f"__bool__ not defined for objects of type {type(self)}.")
class AmbiguousAssignOrExtract:
__slots__ = "parent", "resolved_indexes", "_value", "__weakref__"
_is_scalar = False
def __init__(self, parent, resolved_indexes):
self.parent = parent
self.resolved_indexes = resolved_indexes
self._value = None
def __call__(self, *args, **kwargs):
# Occurs when user calls `C[index](params)`
# Reverse the call order so we can parse the call args and kwargs
updater = self.parent(*args, **kwargs)
is_submask = updater.kwargs.get("mask") is not None
return Assigner(updater, self.resolved_indexes, is_submask=is_submask)
def __lshift__(self, expr, **opts):
# Occurs when user calls `C[index] << expr`
self.update(expr, **opts)
def update(self, expr, **opts):
# Occurs when user calls `C[index].update(expr)`
if getattr(self.parent, "_is_transposed", False):
raise TypeError("'TransposedMatrix' object does not support item assignment")
Updater(self.parent, opts=opts)._setitem(self.resolved_indexes, expr, is_submask=False)
def new(self, dtype=None, *, mask=None, input_mask=None, name=None, **opts):
"""Force extraction of the indexes into a new object.
dtype and mask are the only controllable parameters.
"""
if input_mask is not None:
if mask is not None:
raise TypeError("mask and input_mask arguments cannot both be given")
from .base import _check_mask
input_mask = _check_mask(input_mask, self.parent)
mask = self._input_mask_to_mask(input_mask, **opts)
delayed_extractor = self.parent._prep_for_extract(self.resolved_indexes)
return delayed_extractor.new(dtype, mask=mask, name=name, **opts)
def _extract_delayed(self):
"""Return an Expression object, treating this as an extract call."""
return self.parent._prep_for_extract(self.resolved_indexes)
def _input_mask_to_mask(self, input_mask, **opts):
from .vector import Vector
if type(input_mask.parent) is Vector and type(self.parent) is not Vector:
rowidx, colidx = self.resolved_indexes.indices
if rowidx.size is None:
if self.parent._ncols != input_mask.parent._size:
raise ValueError(
"Size of `input_mask` Vector does not match ncols of Matrix:\n"
f"{self.parent.name}.ncols != {input_mask.parent.name}.size --> "
f"{self.parent._ncols} != {input_mask.parent._size}"
)
mask_expr = input_mask.parent._prep_for_extract(self.resolved_indexes.get_index(1))
elif colidx.size is None:
if self.parent._nrows != input_mask.parent._size:
raise ValueError(
"Size of `input_mask` Vector does not match nrows of Matrix:\n"
f"{self.parent.name}.nrows != {input_mask.parent.name}.size --> "
f"{self.parent._nrows} != {input_mask.parent._size}"
)
mask_expr = input_mask.parent._prep_for_extract(self.resolved_indexes.get_index(0))
else:
raise TypeError(
"Got Vector `input_mask` when extracting a submatrix from a Matrix. "
"Vector `input_mask` with a Matrix (or TransposedMatrix) input is "
"only valid when extracting from a single row or column."
)
elif input_mask.parent.shape != self.parent.shape:
if type(self.parent) is Vector:
attr = "size"
shape1 = self.parent._size
shape2 = input_mask.parent._size
else:
attr = "shape"
shape1 = self.parent.shape
shape2 = input_mask.parent.shape
raise ValueError(
f"{attr.capitalize()} of `input_mask` does not match {attr} of input:\n"
f"{self.parent.name}.{attr} != {input_mask.parent.name}.{attr} --> "
f"{shape1} != {shape2}"
)
else:
mask_expr = input_mask.parent._prep_for_extract(self.resolved_indexes)
mask_value = mask_expr.new(name="mask_temp", **opts)
return type(input_mask)(mask_value)
def _repr_html_(self):
from . import formatting
return formatting.format_index_expression_html(self)
def __repr__(self):
from . import formatting
return formatting.format_index_expression(self)
def _format_expr(self):
indices = ", ".join(index._expr_name for index in self.resolved_indexes.indices)
return f"{self.parent.name}[{indices}]"
def _format_expr_html(self):
indices = ", ".join(index._expr_name for index in self.resolved_indexes.indices)
return f"{self.parent._name_html}[{indices}]"
@property
def dtype(self):
return self.parent.dtype
class Updater:
__slots__ = "parent", "kwargs", "opts", "__weakref__"
def __init__(self, parent, *, opts, **kwargs):
self.parent = parent
self.kwargs = kwargs
self.opts = opts
def __getitem__(self, keys):
# Occurs when user calls `C(params)[index]`
# Need something prepared to receive `<<` or `.update()`
if self.parent._is_scalar:
raise TypeError("Indexing not supported for Scalars")
resolved_indexes = IndexerResolver(self.parent, keys)
return Assigner(self, resolved_indexes, is_submask=False)
def _setitem(self, resolved_indexes, obj, *, is_submask):
# Occurs when user calls C(params)[index] = expr
if resolved_indexes.is_single_element and not self.kwargs:
# Fast path using assignElement
if self.opts:
# Ignore opts for now
desc = descriptor_lookup(**self.opts) # noqa: F841 (keep desc in scope for context)
self.parent._assign_element(resolved_indexes, obj)
else:
mask = self.kwargs.get("mask")
replace = self.kwargs.get("replace", False)
expr, alt_mask = self.parent._prep_for_assign(
resolved_indexes,
obj,
mask,
is_submask,
replace,
self.opts,
)
if alt_mask is mask:
self.update(expr)
else:
kwargs = dict(self.kwargs, mask=alt_mask)
Updater(self.parent, opts=self.opts, **kwargs).update(expr)
def __setitem__(self, keys, obj):
if self.parent._is_scalar:
raise TypeError("Indexing not supported for Scalars")
resolved_indexes = IndexerResolver(self.parent, keys)
self._setitem(resolved_indexes, obj, is_submask=False)
def __delitem__(self, keys):
# Occurs when user calls `del C(params)[index]`
if self.parent._is_scalar:
raise TypeError("Indexing not supported for Scalars")
resolved_indexes = IndexerResolver(self.parent, keys)
if resolved_indexes.is_single_element:
self.parent._delete_element(resolved_indexes)
else:
# Delete selection by assigning an empty scalar
from .scalar import Scalar
scalar = Scalar(
self.parent.dtype, is_cscalar=False, name="s_empty" # pragma: is_grbscalar
)
self._setitem(resolved_indexes, scalar, is_submask=False)
def __lshift__(self, expr):
# Occurs when user calls `C(params) << expr`
self.parent._update(expr, opts=self.opts, **self.kwargs)
def update(self, expr):
# Occurs when user calls `C(params).update(expr)`
self.parent._update(expr, opts=self.opts, **self.kwargs)
def __eq__(self, other):
raise TypeError(f"__eq__ not defined for objects of type {type(self)}.")
__hash__ = None
def __bool__(self):
raise TypeError(f"__bool__ not defined for objects of type {type(self)}.")
class InfixExprBase:
__slots__ = "left", "right", "_expr", "__weakref__"
_is_scalar = False
def __init__(self, left, right):
self.left = left
self.right = right
self._expr = None
def new(self, dtype=None, *, mask=None, name=None, **opts):
if (
mask is None
and self._expr is not None
and self._expr._value is not None
and (dtype is None or self._expr._value.dtype == dtype)
):
rv = self._expr._value
if name is not None:
rv.name = name
self._expr._value = None
return rv
expr = self._to_expr()
return expr.new(dtype, mask=mask, name=name, **opts)
def _to_expr(self):
if self._expr is None:
# Rely on the default operator for `x @ y`
self._expr = getattr(self.left, self.method_name)(self.right)
return self._expr
def _get_value(self, attr=None, default=None):
expr = self._to_expr()
return expr._get_value(attr=attr, default=default)
def _format_expr(self):
return f"{self.left.name} {self._infix} {self.right.name}"
def _format_expr_html(self):
return f"{self.left._name_html} {self._infix} {self.right._name_html}"
def _repr_html_(self):
from . import formatting
if self.output_type.__name__ == "VectorExpression":
return formatting.format_vector_infix_expression_html(self)
if self.output_type.__name__ == "MatrixExpression":
return formatting.format_matrix_infix_expression_html(self)
return formatting.format_scalar_infix_expression_html(self)
def __repr__(self):
from . import formatting
if self.output_type.__name__ == "VectorExpression":
return formatting.format_vector_infix_expression(self)
if self.output_type.__name__ == "MatrixExpression":
return formatting.format_matrix_infix_expression(self)
return formatting.format_scalar_infix_expression(self)
@property
def dtype(self):
return self._to_expr().dtype
@property
def _value(self):
if self._expr is None:
return None
return self._expr._value
@_value.setter
def _value(self, val):
self._to_expr()._value = val
# Mistakes
utils._output_types[AmbiguousAssignOrExtract] = AmbiguousAssignOrExtract
utils._output_types[Assigner] = Assigner
utils._output_types[Updater] = Updater