wren-common 0.0.0.dev0__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wren_common/__init__.py +0 -0
- wren_common/py.typed +0 -0
- wren_common/tests.py +138 -0
- wren_common/types.py +10 -0
- wren_common-0.0.0.dev0.dist-info/METADATA +5 -0
- wren_common-0.0.0.dev0.dist-info/RECORD +7 -0
- wren_common-0.0.0.dev0.dist-info/WHEEL +5 -0
wren_common/__init__.py
ADDED
|
File without changes
|
wren_common/py.typed
ADDED
|
File without changes
|
wren_common/tests.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import math
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from wren_common.types import NDArray
|
|
6
|
+
|
|
7
|
+
rng = np.random.default_rng(0)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
type TestTensor = NDArray[np.float64]
|
|
11
|
+
type TestTensorPair = tuple[TestTensor, TestTensor]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def arange_tensor(*shape: int) -> TestTensor:
|
|
15
|
+
"""Create a tensor of given shape using np.arange."""
|
|
16
|
+
return np.arange(1, math.prod(shape) + 1, dtype=np.float64).reshape(shape)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
TEST_TENSORS: list[TestTensor] = [
|
|
20
|
+
tensor.astype(np.float64)
|
|
21
|
+
for tensor in [
|
|
22
|
+
# ---- 2D ----
|
|
23
|
+
arange_tensor(3, 3),
|
|
24
|
+
rng.random((5, 3)),
|
|
25
|
+
np.array([x % 2 == 0 for x in range(16)]).reshape(4, 4),
|
|
26
|
+
# ---- 3D ----
|
|
27
|
+
arange_tensor(4, 5, 5),
|
|
28
|
+
rng.normal(size=(3, 4, 6)),
|
|
29
|
+
# ---- 4D ----
|
|
30
|
+
arange_tensor(6, 5, 4, 3),
|
|
31
|
+
10e50 * np.ones((3, 4, 3, 5)),
|
|
32
|
+
# ---- 5D ----
|
|
33
|
+
arange_tensor(4, 3, 4, 5, 3),
|
|
34
|
+
-10e-5 * arange_tensor(4, 3, 4, 5, 3),
|
|
35
|
+
# ---- 6D ----
|
|
36
|
+
arange_tensor(4, 3, 4, 3, 3, 6),
|
|
37
|
+
]
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# NOTE: each pair of tensors must have the same shape
|
|
42
|
+
TEST_PAIR_TENSORS: list[TestTensorPair] = [
|
|
43
|
+
(a.astype(np.float64), b.astype(np.float64))
|
|
44
|
+
for a, b in [
|
|
45
|
+
# ---- 2D ----
|
|
46
|
+
(
|
|
47
|
+
arange_tensor(3, 4),
|
|
48
|
+
rng.integers(0, 10, size=(3, 4)),
|
|
49
|
+
),
|
|
50
|
+
# ---- 3D ----
|
|
51
|
+
(arange_tensor(2, 3, 4), np.ones((2, 3, 4))),
|
|
52
|
+
# ---- 4D ----
|
|
53
|
+
(
|
|
54
|
+
arange_tensor(2, 3, 4, 5),
|
|
55
|
+
rng.normal(size=(2, 3, 4, 5)),
|
|
56
|
+
),
|
|
57
|
+
# ---- 5D ----
|
|
58
|
+
(
|
|
59
|
+
np.zeros((2, 2, 3, 4, 3)),
|
|
60
|
+
arange_tensor(2, 2, 3, 4, 3),
|
|
61
|
+
),
|
|
62
|
+
# ---- 6D ----
|
|
63
|
+
(
|
|
64
|
+
arange_tensor(2, 3, 2, 3, 2, 4),
|
|
65
|
+
rng.integers(0, 5, size=(2, 3, 2, 3, 2, 4)),
|
|
66
|
+
),
|
|
67
|
+
# ---- 7D ----
|
|
68
|
+
(
|
|
69
|
+
arange_tensor(2, 2, 3, 2, 4, 3, 2),
|
|
70
|
+
np.ones((2, 2, 3, 2, 4, 3, 2)),
|
|
71
|
+
),
|
|
72
|
+
# ---- 8D ----
|
|
73
|
+
(
|
|
74
|
+
arange_tensor(2, 3, 2, 4, 2, 3, 2, 3),
|
|
75
|
+
rng.random((2, 3, 2, 4, 2, 3, 2, 3)),
|
|
76
|
+
),
|
|
77
|
+
]
|
|
78
|
+
]
|
|
79
|
+
|
|
80
|
+
for a, b in TEST_PAIR_TENSORS:
|
|
81
|
+
assert a.shape == b.shape
|
|
82
|
+
assert a.dtype == b.dtype
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
SMALL_TEST_SCALARS: tuple[float, ...] = (1, -1, math.pi)
|
|
86
|
+
TEST_SCALARS: tuple[float, ...] = (1, 2, 0.5, -1, 0, math.pi, -math.e, 1e30, -1e30)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def tensor_interior[DType: np.number](
|
|
90
|
+
tensor: NDArray[DType], order: int = 1
|
|
91
|
+
) -> NDArray[DType]:
|
|
92
|
+
"""
|
|
93
|
+
Return the interior of a given tensor as a 1D array.
|
|
94
|
+
|
|
95
|
+
Parameters
|
|
96
|
+
----------
|
|
97
|
+
tensor : NDArray
|
|
98
|
+
The tensor of which the interior is to be taken.
|
|
99
|
+
order : int, optional
|
|
100
|
+
The number of elements to be removed from each direction in each dimension.
|
|
101
|
+
|
|
102
|
+
Returns
|
|
103
|
+
-------
|
|
104
|
+
NDArray
|
|
105
|
+
The one dimensional array containing all the elements of the interior in the
|
|
106
|
+
input tensor.
|
|
107
|
+
|
|
108
|
+
"""
|
|
109
|
+
mask = np.zeros_like(tensor, dtype=bool)
|
|
110
|
+
interior_slices = tuple(slice(order, -order) for _ in range(tensor.ndim))
|
|
111
|
+
mask[interior_slices] = True
|
|
112
|
+
return tensor[mask]
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def tensor_boundary[DType: np.number](
|
|
116
|
+
tensor: NDArray[DType], order: int = 1
|
|
117
|
+
) -> NDArray[DType]:
|
|
118
|
+
"""
|
|
119
|
+
Return the boundary of a given tensor as a 1D array.
|
|
120
|
+
|
|
121
|
+
Parameters
|
|
122
|
+
----------
|
|
123
|
+
tensor : NDArray
|
|
124
|
+
The tensor of which the boundary is to be taken.
|
|
125
|
+
order : int, optional
|
|
126
|
+
The number of elements to be kept from each direction in each dimension.
|
|
127
|
+
|
|
128
|
+
Returns
|
|
129
|
+
-------
|
|
130
|
+
NDArray
|
|
131
|
+
The one dimensional array containing all the elements of boundary in the
|
|
132
|
+
input tensor.
|
|
133
|
+
|
|
134
|
+
"""
|
|
135
|
+
mask = np.ones_like(tensor, dtype=bool)
|
|
136
|
+
interior_slices = tuple(slice(order, -order) for _ in range(tensor.ndim))
|
|
137
|
+
mask[interior_slices] = False
|
|
138
|
+
return tensor[mask]
|
wren_common/types.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from typing import SupportsIndex
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
type Vector[T: np.floating] = np.ndarray[tuple[int], np.dtype[T]]
|
|
6
|
+
type Matrix[T: np.floating] = np.ndarray[tuple[int, int], np.dtype[T]]
|
|
7
|
+
type NDArray[T: np.number] = np.ndarray[tuple[int, ...], np.dtype[T]]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
type Index1D = SupportsIndex | slice[SupportsIndex | None]
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
wren_common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
wren_common/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
wren_common/tests.py,sha256=LkPpbeP58nERFAugRb88JfdVdK0sAENZnLbt0KbLu0o,3705
|
|
4
|
+
wren_common/types.py,sha256=lMTu7s07XqXD30vXXln4XgKsrXyw9DxW63UBI_iXqTg,322
|
|
5
|
+
wren_common-0.0.0.dev0.dist-info/METADATA,sha256=hO5futHwhNPSHSBbzN-CVo8SEKGOZAwoYCCgZufswCE,127
|
|
6
|
+
wren_common-0.0.0.dev0.dist-info/WHEEL,sha256=VX-VJ7c6dw9Ge3EqJIbA6W3pOUbz24SnnGGFNr55jY4,105
|
|
7
|
+
wren_common-0.0.0.dev0.dist-info/RECORD,,
|