forked from yoyolicoris/kamui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
248 lines (222 loc) · 8.4 KB
/
utils.py
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
import numpy as np
import numpy.typing as npt
from typing import Tuple, Iterable, Union
__all__ = ["get_2d_edges_and_simplices", "get_3d_edges_and_simplices", "prepare_weights"]
def get_2d_edges_and_simplices(
shape: Tuple[int, int], cyclical_axis: Union[int, Tuple[int, int]] = ()
) -> Tuple[np.ndarray, Iterable[Iterable[int]]]:
"""
Compute the edges and simplices for a 2D grid.
Parameters
----------
shape : Tuple[int, int]
The shape of the grid.
cyclical_axis : Union[int, Tuple[int, int]], optional
The axis/axes that should be treated as cyclical. Defaults to ().
Returns
-------
Tuple[np.ndarray, Iterable[Iterable[int]]]
A tuple containing the edges and simplices of the grid.
"""
nodes = np.arange(np.prod(shape)).reshape(shape)
if type(cyclical_axis) is int:
cyclical_axis = (cyclical_axis,)
# if the axis length <= 2, then the axis is already cyclical
cyclical_axis = tuple(filter(lambda ax: shape[ax] > 2, cyclical_axis))
edges = np.concatenate(
(
np.stack([nodes[:, :-1].ravel(), nodes[:, 1:].ravel()], axis=1),
np.stack([nodes[:-1, :].ravel(), nodes[1:, :].ravel()], axis=1),
)
+ tuple(
np.stack(
[
np.take(nodes, [0], axis=ax).ravel(),
np.take(nodes, [-1], axis=ax).ravel(),
],
axis=1,
)
for ax in cyclical_axis
),
axis=0,
)
simplices = np.stack(
(
nodes[:-1, :-1].ravel(),
nodes[1:, :-1].ravel(),
nodes[1:, 1:].ravel(),
nodes[:-1, 1:].ravel(),
),
axis=1,
).tolist()
if len(cyclical_axis) > 0:
pairs = [
(
np.squeeze(np.take(nodes, [0], axis=ax), axis=ax),
np.squeeze(np.take(nodes, [-1], axis=ax), axis=ax),
)
for ax in cyclical_axis
]
simplices += np.concatenate(
tuple(
np.stack(
(
x[:-1],
y[:-1],
y[1:],
x[1:],
),
axis=1,
)
for x, y in pairs
),
axis=0,
).tolist()
return edges, simplices
def get_3d_edges_and_simplices(
shape: Tuple[int, int, int], cyclical_axis: Union[int, Tuple[int, int]] = ()
) -> Tuple[np.ndarray, Iterable[Iterable[int]]]:
"""
Compute the edges and simplices for a 3D grid.
Parameters
----------
shape : Tuple[int, int, int]
The shape of the grid.
cyclical_axis : Union[int, Tuple[int, int]], optional
The axis/axes that should be treated as cyclical. Defaults to ().
Returns
-------
Tuple[np.ndarray, Iterable[Iterable[int]]]
A tuple containing the edges and simplices of the grid.
"""
nodes = np.arange(np.prod(shape)).reshape(shape)
if type(cyclical_axis) is int:
cyclical_axis = (cyclical_axis,)
cyclical_axis = tuple(filter(lambda ax: shape[ax] > 2, cyclical_axis))
edges = np.concatenate(
(
np.stack([nodes[:, :-1, :].ravel(), nodes[:, 1:, :].ravel()], axis=1),
np.stack([nodes[:-1, :, :].ravel(), nodes[1:, :, :].ravel()], axis=1),
np.stack([nodes[:, :, :-1].ravel(), nodes[:, :, 1:].ravel()], axis=1),
)
+ tuple(
np.stack(
[
np.take(nodes, [0], axis=ax).ravel(),
np.take(nodes, [-1], axis=ax).ravel(),
],
axis=1,
)
for ax in cyclical_axis
),
axis=0,
)
simplices = np.concatenate(
(
np.stack(
(
nodes[:-1, :-1, :].ravel(),
nodes[1:, :-1, :].ravel(),
nodes[1:, 1:, :].ravel(),
nodes[:-1, 1:, :].ravel(),
),
axis=1,
),
np.stack(
(
nodes[:, :-1, :-1].ravel(),
nodes[:, 1:, :-1].ravel(),
nodes[:, 1:, 1:].ravel(),
nodes[:, :-1, 1:].ravel(),
),
axis=1,
),
np.stack(
(
nodes[:-1, :, :-1].ravel(),
nodes[:-1, :, 1:].ravel(),
nodes[1:, :, 1:].ravel(),
nodes[1:, :, :-1].ravel(),
),
axis=1,
),
),
axis=0,
).tolist()
if len(cyclical_axis) > 0:
simplices += np.concatenate(
sum(
(
(
np.stack(
(
x[1:, :].ravel(),
y[1:, :].ravel(),
y[:-1, :].ravel(),
x[:-1, :].ravel(),
),
axis=1,
),
np.stack(
(
x[:, 1:].ravel(),
y[:, 1:].ravel(),
y[:, :-1].ravel(),
x[:, :-1].ravel(),
),
axis=1,
),
)
for x, y in [
(
np.squeeze(np.take(nodes, [0], axis=ax), axis=ax),
np.squeeze(np.take(nodes, [-1], axis=ax), axis=ax),
)
for ax in cyclical_axis
]
),
(),
),
axis=0,
).tolist()
return edges, simplices
def prepare_weights(weights: npt.NDArray, edges: npt.NDArray[np.int_], smoothing: float = 0.1,
merging_method: str = 'mean') -> npt.NDArray[np.float_]:
"""Prepare weights for `calculate_m` and `calculate_k` functions.
Assume the weights are the same shape as the phases to be unwrapped.
Scale the weights from 0 to 1. Pick the weights corresponding to the phase pairs connected by the edges.
Compute the mean/max/min (depending on the `merging_method`) of each of those pairs to give a weight for each edge.
Args:
weights : Array of weights of shapr corresponding to the original phases array shape.
edges : Edges connecting the phases. Shape: (M, 2), where M is the number of edges.
smoothing : A positive value in range [0, 1). This is the minimal value of the rescaled weights
where they are defined. If smoothing > 0, the value of 0 is reserved for places where
the weights are originally NaN. If smoothing == 0, 0 will be used for both NaN weights
and smallest non-NaN ones.
merging_method : Way of combining two phase weights into a single edge weight.
Returns:
Array of weights for the edges, shape: (M,). Rescaled to [0, 1].
"""
if not 0 <= smoothing < 1:
raise ValueError(
"`smoothing` should be a value between 0 (inclusive) and 1 (non inclusive); got " + str(smoothing))
# scale the weights from 0 to 1
weights = weights - np.nanmin(weights)
current_max = np.nanmax(weights)
if not current_max:
# current maximum is 0, which means all weights originally had the same value, now 0; replace everything with 1
weights += 1
else:
weights /= current_max
weights *= (1 - smoothing)
weights += smoothing
# pick the weights corresponding to the phases connected by the edges
# and use `merging_method` to get one weight for each edge
allowed_merging_methods = ['min', 'max', 'mean']
if merging_method not in allowed_merging_methods:
raise ValueError(
"`merging_method` should be one of: " + ', '.join(merging_method) + '; got ' + str(merging_method))
weights_for_edges = getattr(np, merging_method)(weights.ravel()[edges], axis=1)
# make sure there are no NaNs in the weights; replace any with 0s
weights_for_edges[np.isnan(weights_for_edges)] = 0
return weights_for_edges