|
| 1 | +from pytensor.graph.basic import Apply, Constant, Variable |
| 2 | +from pytensor.scalar.basic import discrete_dtypes |
| 3 | +from pytensor.tensor.basic import as_tensor |
| 4 | +from pytensor.tensor.type_other import NoneTypeT, SliceType, make_slice |
| 5 | +from pytensor.xtensor.basic import XOp |
| 6 | +from pytensor.xtensor.type import XTensorType, as_xtensor, xtensor |
| 7 | + |
| 8 | + |
| 9 | +def as_idx_variable(idx): |
| 10 | + if idx is None or (isinstance(idx, Variable) and isinstance(idx.type, NoneTypeT)): |
| 11 | + raise TypeError("XTensors do not support None (np.newaxis)") |
| 12 | + idx |
| 13 | + if isinstance(idx, slice): |
| 14 | + idx = make_slice(idx) |
| 15 | + elif isinstance(idx, Variable) and isinstance(idx.type, SliceType): |
| 16 | + pass |
| 17 | + else: |
| 18 | + # Must be integer indices, we already counted for None and slices |
| 19 | + try: |
| 20 | + idx = as_xtensor(idx) |
| 21 | + except TypeError: |
| 22 | + idx = as_tensor(idx) |
| 23 | + if idx.type.dtype not in discrete_dtypes: |
| 24 | + raise TypeError("Numerical indices must be integers or boolean") |
| 25 | + if idx.type.dtype == "bool" and idx.type.ndim == 0: |
| 26 | + raise NotImplementedError("Scalar boolean indices not supported") |
| 27 | + return idx |
| 28 | + |
| 29 | + |
| 30 | +def get_static_slice_length(slc: Variable, dim_length: None | int) -> int | None: |
| 31 | + if dim_length is None: |
| 32 | + return None |
| 33 | + if isinstance(slc, Constant): |
| 34 | + d = slc.data |
| 35 | + start, stop, step = d.start, d.stop, d.step |
| 36 | + elif slc.owner is None: |
| 37 | + # It's a root variable no way of knowing what we're getting |
| 38 | + return None |
| 39 | + else: |
| 40 | + # It's a MakeSliceOp |
| 41 | + start, stop, step = slice.owner.inputs |
| 42 | + if isinstance(start, Constant): |
| 43 | + start = start.data |
| 44 | + else: |
| 45 | + return None |
| 46 | + if isinstance(stop, Constant): |
| 47 | + stop = stop.data |
| 48 | + else: |
| 49 | + return None |
| 50 | + if isinstance(step, Constant): |
| 51 | + step = step.data |
| 52 | + else: |
| 53 | + return None |
| 54 | + return len(range(*slice(start, stop, step).indices(dim_length))) |
| 55 | + |
| 56 | + |
| 57 | +class Index(XOp): |
| 58 | + __props__ = () |
| 59 | + |
| 60 | + def make_node(self, x, *idxs): |
| 61 | + x = as_xtensor(x) |
| 62 | + idxs = [as_idx_variable(idx) for idx in idxs] |
| 63 | + |
| 64 | + x_ndim = x.type.ndim |
| 65 | + x_dims = x.type.dims |
| 66 | + x_shape = x.type.shape |
| 67 | + out_dims = [] |
| 68 | + out_shape = [] |
| 69 | + for i, idx in enumerate(idxs): |
| 70 | + if i == x_ndim: |
| 71 | + raise IndexError("Too many indices") |
| 72 | + if isinstance(idx.type, XTensorType): |
| 73 | + raise NotImplementedError( |
| 74 | + "Indexing with XTensorType not yet supported." |
| 75 | + ) |
| 76 | + if isinstance(idx.type, SliceType): |
| 77 | + out_dims.append(x_dims[i]) |
| 78 | + out_shape.append(get_static_slice_length(idx, x_shape[i])) |
| 79 | + |
| 80 | + else: # TensorType |
| 81 | + if idx.type.ndim == 0: |
| 82 | + # Scalar, dimension is dropped |
| 83 | + pass |
| 84 | + elif idx.type.ndim == 1: |
| 85 | + out_dims.append(x_dims[i]) |
| 86 | + out_shape.append(idx.type.shape[0]) |
| 87 | + else: |
| 88 | + # Same error that xarray raises |
| 89 | + raise IndexError( |
| 90 | + "Unlabeled multi-dimensional array cannot be used for indexing" |
| 91 | + ) |
| 92 | + |
| 93 | + output = xtensor(dtype=x.type.dtype, shape=out_shape, dims=out_dims) |
| 94 | + return Apply(self, [x], [output]) |
| 95 | + |
| 96 | + |
| 97 | +index = Index() |
0 commit comments