-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcat_slice.py
More file actions
34 lines (27 loc) · 914 Bytes
/
cat_slice.py
File metadata and controls
34 lines (27 loc) · 914 Bytes
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
import numpy as np
from typing import Union, Sequence, Any
class CatSlice(np.ndarray):
def __new__(
cls,
start: int = 0,
stop: Union[int, None] = None,
step: int = 1,
input_array: Sequence = [Any],
):
if stop is not None:
obj = np.arange(start=start, stop=stop, step=step).view(cls)
else:
obj = np.asarray(input_array).view(cls)
return obj
def __array_finalize__(self, obj):
if obj is None:
return
def __add__(self, s):
result = CatSlice(input_array=np.unique(np.concatenate((self, s))))
return result
def __mul__(self, s):
return np.ix_(self, s)
def __pow__(self, value):
assert value > 0, f"CatSlice.__pow__: pow value must positive integer: {value}"
prod_list = [self.copy() for i in range(value)]
return np.ix_(*prod_list)