py4D-browser-flexloader 0.0.5__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.
- py4d_browser_flexloader-0.0.5.dist-info/METADATA +113 -0
- py4d_browser_flexloader-0.0.5.dist-info/RECORD +10 -0
- py4d_browser_flexloader-0.0.5.dist-info/WHEEL +5 -0
- py4d_browser_flexloader-0.0.5.dist-info/licenses/LICENSE.txt +675 -0
- py4d_browser_flexloader-0.0.5.dist-info/top_level.txt +1 -0
- py4d_browser_plugin/flexloader/__init__.py +10 -0
- py4d_browser_plugin/flexloader/backends.py +219 -0
- py4d_browser_plugin/flexloader/dialog.py +359 -0
- py4d_browser_plugin/flexloader/flexloader.py +116 -0
- py4d_browser_plugin/flexloader/selection.py +287 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from math import isqrt
|
|
5
|
+
from typing import Mapping, Sequence
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
ROLE_FIXED = "Fixed Index"
|
|
11
|
+
ROLE_SCAN_Y = "Scan Y"
|
|
12
|
+
ROLE_SCAN_X = "Scan X"
|
|
13
|
+
ROLE_FLATTENED_SCAN = "Flattened Scan"
|
|
14
|
+
ROLE_DETECTOR_Y = "Detector Y"
|
|
15
|
+
ROLE_DETECTOR_X = "Detector X"
|
|
16
|
+
|
|
17
|
+
DATACUBE_ROLES = (ROLE_SCAN_Y, ROLE_SCAN_X, ROLE_DETECTOR_Y, ROLE_DETECTOR_X)
|
|
18
|
+
ALL_ROLES = (ROLE_FIXED, ROLE_FLATTENED_SCAN, *DATACUBE_ROLES)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True)
|
|
22
|
+
class DimensionSelection:
|
|
23
|
+
roles: tuple[str, ...]
|
|
24
|
+
fixed_indices: tuple[int, ...]
|
|
25
|
+
scan_shapes: tuple[tuple[int, int] | None, ...] = ()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class PreviewArrays:
|
|
30
|
+
diffraction: np.ndarray
|
|
31
|
+
axial_bf: np.ndarray
|
|
32
|
+
description: str
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def default_roles(ndim: int) -> list[str]:
|
|
36
|
+
roles = [ROLE_FIXED] * ndim
|
|
37
|
+
if ndim >= 4:
|
|
38
|
+
start = ndim - 4
|
|
39
|
+
for offset, role in enumerate(DATACUBE_ROLES):
|
|
40
|
+
roles[start + offset] = role
|
|
41
|
+
elif ndim == 3:
|
|
42
|
+
roles = [ROLE_FLATTENED_SCAN, ROLE_DETECTOR_Y, ROLE_DETECTOR_X]
|
|
43
|
+
return roles
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def default_scan_shape(size: int) -> tuple[int, int]:
|
|
47
|
+
for scan_y in range(isqrt(size), 0, -1):
|
|
48
|
+
if size % scan_y == 0:
|
|
49
|
+
return scan_y, size // scan_y
|
|
50
|
+
return 1, size
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def normalize_scan_shapes(
|
|
54
|
+
ndim: int, scan_shapes: Sequence[tuple[int, int] | None]
|
|
55
|
+
) -> tuple[tuple[int, int] | None, ...]:
|
|
56
|
+
if not scan_shapes:
|
|
57
|
+
return tuple(None for _ in range(ndim))
|
|
58
|
+
if len(scan_shapes) != ndim:
|
|
59
|
+
raise ValueError("Scan-shape count must match dataset dimensionality.")
|
|
60
|
+
return tuple(scan_shapes)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def validate_selection(shape: Sequence[int], selection: DimensionSelection) -> None:
|
|
64
|
+
if len(shape) != len(selection.roles):
|
|
65
|
+
raise ValueError("Role count must match dataset dimensionality.")
|
|
66
|
+
if len(shape) != len(selection.fixed_indices):
|
|
67
|
+
raise ValueError("Fixed-index count must match dataset dimensionality.")
|
|
68
|
+
scan_shapes = normalize_scan_shapes(len(shape), selection.scan_shapes)
|
|
69
|
+
|
|
70
|
+
assigned = [role for role in selection.roles if role != ROLE_FIXED]
|
|
71
|
+
has_flattened_scan = ROLE_FLATTENED_SCAN in assigned
|
|
72
|
+
required_roles = (
|
|
73
|
+
(ROLE_FLATTENED_SCAN, ROLE_DETECTOR_Y, ROLE_DETECTOR_X)
|
|
74
|
+
if has_flattened_scan
|
|
75
|
+
else DATACUBE_ROLES
|
|
76
|
+
)
|
|
77
|
+
missing = [role for role in required_roles if role not in assigned]
|
|
78
|
+
duplicated = sorted({role for role in assigned if assigned.count(role) > 1})
|
|
79
|
+
|
|
80
|
+
if missing:
|
|
81
|
+
raise ValueError(f"Missing required dimension role(s): {', '.join(missing)}.")
|
|
82
|
+
if duplicated:
|
|
83
|
+
raise ValueError(f"Duplicate dimension role(s): {', '.join(duplicated)}.")
|
|
84
|
+
if has_flattened_scan and (
|
|
85
|
+
ROLE_SCAN_Y in assigned or ROLE_SCAN_X in assigned
|
|
86
|
+
):
|
|
87
|
+
raise ValueError(
|
|
88
|
+
"Use either Flattened Scan or separate Scan Y/Scan X roles, not both."
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
for axis, (size, role, fixed_index) in enumerate(
|
|
92
|
+
zip(shape, selection.roles, selection.fixed_indices)
|
|
93
|
+
):
|
|
94
|
+
if role not in ALL_ROLES:
|
|
95
|
+
raise ValueError(f"Unsupported role for axis {axis}: {role}.")
|
|
96
|
+
if role == ROLE_FIXED and not 0 <= fixed_index < size:
|
|
97
|
+
raise ValueError(
|
|
98
|
+
f"Fixed index {fixed_index} is outside axis {axis} with size {size}."
|
|
99
|
+
)
|
|
100
|
+
if role == ROLE_FLATTENED_SCAN:
|
|
101
|
+
scan_shape = scan_shapes[axis]
|
|
102
|
+
if scan_shape is None:
|
|
103
|
+
raise ValueError("Flattened Scan requires Scan Y and Scan X factors.")
|
|
104
|
+
scan_y, scan_x = scan_shape
|
|
105
|
+
if scan_y <= 0 or scan_x <= 0:
|
|
106
|
+
raise ValueError("Scan Y and Scan X factors must be positive.")
|
|
107
|
+
if scan_y * scan_x != size:
|
|
108
|
+
raise ValueError(
|
|
109
|
+
f"Scan Y x Scan X must equal flattened scan size {size}."
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def load_datacube_array(array, selection: DimensionSelection) -> np.ndarray:
|
|
114
|
+
shape = tuple(int(size) for size in array.shape)
|
|
115
|
+
validate_selection(shape, selection)
|
|
116
|
+
scan_shapes = normalize_scan_shapes(len(shape), selection.scan_shapes)
|
|
117
|
+
|
|
118
|
+
indexer = []
|
|
119
|
+
kept_roles = []
|
|
120
|
+
flattened_axis = None
|
|
121
|
+
flattened_scan_shape = None
|
|
122
|
+
for axis, (role, fixed_index) in enumerate(
|
|
123
|
+
zip(selection.roles, selection.fixed_indices)
|
|
124
|
+
):
|
|
125
|
+
if role == ROLE_FIXED:
|
|
126
|
+
indexer.append(int(fixed_index))
|
|
127
|
+
else:
|
|
128
|
+
indexer.append(slice(None))
|
|
129
|
+
if role == ROLE_FLATTENED_SCAN:
|
|
130
|
+
flattened_axis = len(kept_roles)
|
|
131
|
+
flattened_scan_shape = scan_shapes[axis]
|
|
132
|
+
kept_roles.extend((ROLE_SCAN_Y, ROLE_SCAN_X))
|
|
133
|
+
else:
|
|
134
|
+
kept_roles.append(role)
|
|
135
|
+
|
|
136
|
+
subset = np.asarray(array[tuple(indexer)])
|
|
137
|
+
if flattened_axis is not None:
|
|
138
|
+
scan_y, scan_x = flattened_scan_shape
|
|
139
|
+
subset = np.reshape(
|
|
140
|
+
subset,
|
|
141
|
+
(
|
|
142
|
+
*subset.shape[:flattened_axis],
|
|
143
|
+
scan_y,
|
|
144
|
+
scan_x,
|
|
145
|
+
*subset.shape[flattened_axis + 1 :],
|
|
146
|
+
),
|
|
147
|
+
)
|
|
148
|
+
permutation = [kept_roles.index(role) for role in DATACUBE_ROLES]
|
|
149
|
+
if permutation != list(range(4)):
|
|
150
|
+
subset = np.transpose(subset, permutation)
|
|
151
|
+
return np.asarray(subset)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def load_preview_arrays(array, selection: DimensionSelection) -> PreviewArrays:
|
|
155
|
+
shape = tuple(int(size) for size in array.shape)
|
|
156
|
+
validate_selection(shape, selection)
|
|
157
|
+
scan_shape = _mapped_datacube_shape(shape, selection)
|
|
158
|
+
scan_y, scan_x, detector_y, detector_x = scan_shape
|
|
159
|
+
|
|
160
|
+
scan_y_center = scan_y // 2
|
|
161
|
+
scan_x_center = scan_x // 2
|
|
162
|
+
detector_y_center = detector_y // 2
|
|
163
|
+
detector_x_center = detector_x // 2
|
|
164
|
+
|
|
165
|
+
diffraction = _read_mapped_slice(
|
|
166
|
+
array,
|
|
167
|
+
selection,
|
|
168
|
+
{
|
|
169
|
+
ROLE_SCAN_Y: scan_y_center,
|
|
170
|
+
ROLE_SCAN_X: scan_x_center,
|
|
171
|
+
ROLE_DETECTOR_Y: slice(None),
|
|
172
|
+
ROLE_DETECTOR_X: slice(None),
|
|
173
|
+
},
|
|
174
|
+
output_roles=(ROLE_DETECTOR_Y, ROLE_DETECTOR_X),
|
|
175
|
+
)
|
|
176
|
+
axial_bf = _read_mapped_slice(
|
|
177
|
+
array,
|
|
178
|
+
selection,
|
|
179
|
+
{
|
|
180
|
+
ROLE_SCAN_Y: slice(None),
|
|
181
|
+
ROLE_SCAN_X: slice(None),
|
|
182
|
+
ROLE_DETECTOR_Y: detector_y_center,
|
|
183
|
+
ROLE_DETECTOR_X: detector_x_center,
|
|
184
|
+
},
|
|
185
|
+
output_roles=(ROLE_SCAN_Y, ROLE_SCAN_X),
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
description = (
|
|
189
|
+
f"DP: scan center ({scan_y_center}, {scan_x_center}); "
|
|
190
|
+
f"Axial BF: detector center ({detector_y_center}, {detector_x_center})"
|
|
191
|
+
)
|
|
192
|
+
return PreviewArrays(
|
|
193
|
+
diffraction=np.asarray(diffraction),
|
|
194
|
+
axial_bf=np.asarray(axial_bf),
|
|
195
|
+
description=description,
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _mapped_datacube_shape(
|
|
200
|
+
shape: Sequence[int], selection: DimensionSelection
|
|
201
|
+
) -> tuple[int, int, int, int]:
|
|
202
|
+
scan_shapes = normalize_scan_shapes(len(shape), selection.scan_shapes)
|
|
203
|
+
role_sizes: dict[str, int] = {}
|
|
204
|
+
for axis, (size, role) in enumerate(zip(shape, selection.roles)):
|
|
205
|
+
if role == ROLE_FLATTENED_SCAN:
|
|
206
|
+
scan_shape = scan_shapes[axis]
|
|
207
|
+
role_sizes[ROLE_SCAN_Y], role_sizes[ROLE_SCAN_X] = scan_shape
|
|
208
|
+
elif role != ROLE_FIXED:
|
|
209
|
+
role_sizes[role] = int(size)
|
|
210
|
+
return tuple(role_sizes[role] for role in DATACUBE_ROLES)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _read_mapped_slice(
|
|
214
|
+
array,
|
|
215
|
+
selection: DimensionSelection,
|
|
216
|
+
target_by_role: Mapping[str, int | slice],
|
|
217
|
+
output_roles: Sequence[str],
|
|
218
|
+
) -> np.ndarray:
|
|
219
|
+
shape = tuple(int(size) for size in array.shape)
|
|
220
|
+
scan_shapes = normalize_scan_shapes(len(shape), selection.scan_shapes)
|
|
221
|
+
|
|
222
|
+
indexer = []
|
|
223
|
+
kept_roles = []
|
|
224
|
+
flattened_axis = None
|
|
225
|
+
flattened_scan_shape = None
|
|
226
|
+
|
|
227
|
+
for axis, (role, fixed_index) in enumerate(
|
|
228
|
+
zip(selection.roles, selection.fixed_indices)
|
|
229
|
+
):
|
|
230
|
+
if role == ROLE_FIXED:
|
|
231
|
+
indexer.append(int(fixed_index))
|
|
232
|
+
elif role == ROLE_FLATTENED_SCAN:
|
|
233
|
+
scan_y_target = target_by_role[ROLE_SCAN_Y]
|
|
234
|
+
scan_x_target = target_by_role[ROLE_SCAN_X]
|
|
235
|
+
_, scan_x = scan_shapes[axis]
|
|
236
|
+
if isinstance(scan_y_target, slice) and isinstance(scan_x_target, slice):
|
|
237
|
+
indexer.append(slice(None))
|
|
238
|
+
flattened_axis = len(kept_roles)
|
|
239
|
+
flattened_scan_shape = scan_shapes[axis]
|
|
240
|
+
kept_roles.extend((ROLE_SCAN_Y, ROLE_SCAN_X))
|
|
241
|
+
elif isinstance(scan_y_target, int) and isinstance(scan_x_target, int):
|
|
242
|
+
indexer.append(scan_y_target * scan_x + scan_x_target)
|
|
243
|
+
else:
|
|
244
|
+
raise ValueError(
|
|
245
|
+
"Flattened Scan preview requires both scan axes to be sliced "
|
|
246
|
+
"or both scan axes to be fixed."
|
|
247
|
+
)
|
|
248
|
+
else:
|
|
249
|
+
target = target_by_role[role]
|
|
250
|
+
indexer.append(target)
|
|
251
|
+
if isinstance(target, slice):
|
|
252
|
+
kept_roles.append(role)
|
|
253
|
+
|
|
254
|
+
subset = np.asarray(array[tuple(indexer)])
|
|
255
|
+
if flattened_axis is not None:
|
|
256
|
+
scan_y, scan_x = flattened_scan_shape
|
|
257
|
+
subset = np.reshape(
|
|
258
|
+
subset,
|
|
259
|
+
(
|
|
260
|
+
*subset.shape[:flattened_axis],
|
|
261
|
+
scan_y,
|
|
262
|
+
scan_x,
|
|
263
|
+
*subset.shape[flattened_axis + 1 :],
|
|
264
|
+
),
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
permutation = [kept_roles.index(role) for role in output_roles]
|
|
268
|
+
if permutation != list(range(len(output_roles))):
|
|
269
|
+
subset = np.transpose(subset, permutation)
|
|
270
|
+
return np.asarray(subset)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def selection_from_mapping(
|
|
274
|
+
ndim: int,
|
|
275
|
+
role_by_axis: Mapping[int, str],
|
|
276
|
+
fixed_by_axis: Mapping[int, int],
|
|
277
|
+
scan_shape_by_axis: Mapping[int, tuple[int, int]] | None = None,
|
|
278
|
+
) -> DimensionSelection:
|
|
279
|
+
roles = []
|
|
280
|
+
fixed_indices = []
|
|
281
|
+
scan_shapes = []
|
|
282
|
+
scan_shape_by_axis = scan_shape_by_axis or {}
|
|
283
|
+
for axis in range(ndim):
|
|
284
|
+
roles.append(role_by_axis.get(axis, ROLE_FIXED))
|
|
285
|
+
fixed_indices.append(int(fixed_by_axis.get(axis, 0)))
|
|
286
|
+
scan_shapes.append(scan_shape_by_axis.get(axis))
|
|
287
|
+
return DimensionSelection(tuple(roles), tuple(fixed_indices), tuple(scan_shapes))
|