diffct-mlx 1.0.1__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.
- diffct_mlx/__about__.py +3 -0
- diffct_mlx/__init__.py +223 -0
- diffct_mlx/constants.py +38 -0
- diffct_mlx/geometry.py +802 -0
- diffct_mlx/kernels/__init__.py +39 -0
- diffct_mlx/kernels/cone_beam.py +708 -0
- diffct_mlx/kernels/fan_beam.py +502 -0
- diffct_mlx/kernels/parallel_beam.py +431 -0
- diffct_mlx/phantoms/__init__.py +5 -0
- diffct_mlx/phantoms/shepp_logan.py +118 -0
- diffct_mlx/projectors.py +1084 -0
- diffct_mlx/real_measured_data_helper.py +601 -0
- diffct_mlx/reconstruction_algorithms/__init__.py +85 -0
- diffct_mlx/reconstruction_algorithms/_analytic.py +101 -0
- diffct_mlx/reconstruction_algorithms/_core.py +872 -0
- diffct_mlx/reconstruction_algorithms/asd_pocs.py +114 -0
- diffct_mlx/reconstruction_algorithms/awtv_pocs.py +106 -0
- diffct_mlx/reconstruction_algorithms/cases.py +988 -0
- diffct_mlx/reconstruction_algorithms/dart.py +424 -0
- diffct_mlx/reconstruction_algorithms/fbp.py +69 -0
- diffct_mlx/reconstruction_algorithms/fdk.py +67 -0
- diffct_mlx/reconstruction_algorithms/sart.py +66 -0
- diffct_mlx/reconstruction_algorithms/sirt.py +64 -0
- diffct_mlx/reconstruction_algorithms/tv_pocs.py +85 -0
- diffct_mlx/regularizers.py +122 -0
- diffct_mlx/tv_gradients.py +74 -0
- diffct_mlx/utils.py +64 -0
- diffct_mlx-1.0.1.dist-info/METADATA +315 -0
- diffct_mlx-1.0.1.dist-info/RECORD +31 -0
- diffct_mlx-1.0.1.dist-info/WHEEL +4 -0
- diffct_mlx-1.0.1.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,872 @@
|
|
|
1
|
+
"""Core helpers for geometry-agnostic iterative reconstruction algorithms."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from functools import lru_cache
|
|
7
|
+
from typing import Any, Callable, Sequence
|
|
8
|
+
|
|
9
|
+
import mlx.core as mx
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
ArrayLike = Any
|
|
14
|
+
ForwardProjector = Callable[[mx.array, int], mx.array]
|
|
15
|
+
BackProjector = Callable[[mx.array, int], mx.array]
|
|
16
|
+
SARTDebugCallback = Callable[[dict[str, Any]], None]
|
|
17
|
+
_MATERIALIZE_INTERVAL = 16
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class ReconstructionParameters:
|
|
22
|
+
"""Parameters shared by SART and the ART-TV family."""
|
|
23
|
+
|
|
24
|
+
volume_shape: tuple[int, ...]
|
|
25
|
+
iteration_count: int
|
|
26
|
+
sart_iteration_count: int = 1
|
|
27
|
+
iterative_update_method: str = "sart"
|
|
28
|
+
pixel_extreme_values: tuple[float, float] = (0.0, float("inf"))
|
|
29
|
+
voxel_extreme_values: tuple[float, float] = (-float("inf"), float("inf"))
|
|
30
|
+
initial_volume: ArrayLike | None = None
|
|
31
|
+
enforce_positivity: bool = True
|
|
32
|
+
positivity_mode: str = "per_iteration"
|
|
33
|
+
projection_order: Sequence[int] | None = None
|
|
34
|
+
shuffle_projection_order: bool = True
|
|
35
|
+
projection_order_seed: int = 0
|
|
36
|
+
sart_debug_callback: SARTDebugCallback | None = None
|
|
37
|
+
raylength_thresholding: bool = True
|
|
38
|
+
raylength_quantile: float = 1e-3
|
|
39
|
+
raylength_epsilon: float = 5e-4
|
|
40
|
+
preserve_unmasked_computed_projection: bool = False
|
|
41
|
+
detector_border_u: int = 0
|
|
42
|
+
detector_border_v: int = 0
|
|
43
|
+
volume_border_width: int = 0
|
|
44
|
+
volume_support_mask: ArrayLike | None = None
|
|
45
|
+
volume_support_mask_mode: str = "always"
|
|
46
|
+
voxel_sensitivity_normalization: bool = True
|
|
47
|
+
projection_chunk_size: int | None = None
|
|
48
|
+
projection_subset_count: int = 1
|
|
49
|
+
projection_weights: Sequence[float] | None = None
|
|
50
|
+
normalized_sart_relaxation: float = 0.9
|
|
51
|
+
backprojection_scale: float | None = 1.0
|
|
52
|
+
dtype: Any = mx.float32
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass
|
|
56
|
+
class RegularizationParameters:
|
|
57
|
+
"""Parameters shared by TV-based regularization steps."""
|
|
58
|
+
|
|
59
|
+
reg_iteration_count: int = 20
|
|
60
|
+
alpha: float = 0.2
|
|
61
|
+
tv_eps: float = 1e-6
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def as_mx_array(value: ArrayLike, dtype: Any = mx.float32) -> mx.array:
|
|
65
|
+
"""Convert a NumPy/MLX input to an MLX array."""
|
|
66
|
+
return mx.array(value, dtype=dtype)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def normalize_measured_projections(
|
|
70
|
+
measured_projections: Sequence[ArrayLike],
|
|
71
|
+
dtype: Any,
|
|
72
|
+
) -> list[mx.array]:
|
|
73
|
+
"""Convert measured projections once up front."""
|
|
74
|
+
return [as_mx_array(projection, dtype=dtype) for projection in measured_projections]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def validate_reconstruction_inputs(
|
|
78
|
+
params: ReconstructionParameters,
|
|
79
|
+
measured_projections: Sequence[ArrayLike],
|
|
80
|
+
) -> None:
|
|
81
|
+
"""Validate the generic reconstruction inputs."""
|
|
82
|
+
if not measured_projections:
|
|
83
|
+
raise ValueError("At least one measured projection is required.")
|
|
84
|
+
if not params.volume_shape:
|
|
85
|
+
raise ValueError("volume_shape must not be empty.")
|
|
86
|
+
if any(int(dim) <= 0 for dim in params.volume_shape):
|
|
87
|
+
raise ValueError(f"volume_shape must be positive in every axis, got {params.volume_shape!r}.")
|
|
88
|
+
if params.iteration_count <= 0:
|
|
89
|
+
raise ValueError("iteration_count must be positive.")
|
|
90
|
+
if params.sart_iteration_count <= 0:
|
|
91
|
+
raise ValueError("sart_iteration_count must be positive.")
|
|
92
|
+
if params.iterative_update_method not in {"sart", "sirt", "normalized_sart"}:
|
|
93
|
+
raise ValueError("iterative_update_method must be one of: 'sart', 'sirt', 'normalized_sart'.")
|
|
94
|
+
if params.positivity_mode not in {"per_iteration", "final", "none"}:
|
|
95
|
+
raise ValueError("positivity_mode must be one of: 'per_iteration', 'final', 'none'.")
|
|
96
|
+
if params.raylength_quantile < 0.0 or params.raylength_quantile >= 1.0:
|
|
97
|
+
raise ValueError("raylength_quantile must be in [0, 1).")
|
|
98
|
+
if params.raylength_epsilon <= 0.0:
|
|
99
|
+
raise ValueError("raylength_epsilon must be positive.")
|
|
100
|
+
if params.detector_border_u < 0 or params.detector_border_v < 0:
|
|
101
|
+
raise ValueError("detector border widths must be non-negative.")
|
|
102
|
+
if params.volume_border_width < 0:
|
|
103
|
+
raise ValueError("volume border width must be non-negative.")
|
|
104
|
+
if params.volume_support_mask is not None and tuple(np.shape(params.volume_support_mask)) != tuple(params.volume_shape):
|
|
105
|
+
raise ValueError(
|
|
106
|
+
"volume_support_mask shape does not match volume_shape: "
|
|
107
|
+
f"{tuple(np.shape(params.volume_support_mask))!r} vs {tuple(params.volume_shape)!r}."
|
|
108
|
+
)
|
|
109
|
+
if params.volume_support_mask_mode not in {"always", "final", "none"}:
|
|
110
|
+
raise ValueError("volume_support_mask_mode must be one of: 'always', 'final', 'none'.")
|
|
111
|
+
if params.projection_chunk_size is not None and params.projection_chunk_size <= 0:
|
|
112
|
+
raise ValueError("projection_chunk_size must be positive when specified.")
|
|
113
|
+
if params.projection_subset_count <= 0:
|
|
114
|
+
raise ValueError("projection_subset_count must be positive.")
|
|
115
|
+
if params.projection_weights is not None and len(params.projection_weights) != len(measured_projections):
|
|
116
|
+
raise ValueError(
|
|
117
|
+
"projection_weights must contain one entry per measured projection: "
|
|
118
|
+
f"expected {len(measured_projections)}, got {len(params.projection_weights)}."
|
|
119
|
+
)
|
|
120
|
+
if params.normalized_sart_relaxation <= 0.0:
|
|
121
|
+
raise ValueError("normalized_sart_relaxation must be positive.")
|
|
122
|
+
if params.initial_volume is not None and tuple(np.shape(params.initial_volume)) != tuple(params.volume_shape):
|
|
123
|
+
raise ValueError(
|
|
124
|
+
"initial_volume shape does not match volume_shape: "
|
|
125
|
+
f"{tuple(np.shape(params.initial_volume))!r} vs {tuple(params.volume_shape)!r}."
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def projection_order(params: ReconstructionParameters, num_projections: int) -> tuple[int, ...]:
|
|
130
|
+
"""Resolve and validate the per-iteration projection traversal order."""
|
|
131
|
+
if params.projection_order is None:
|
|
132
|
+
if not params.shuffle_projection_order:
|
|
133
|
+
return tuple(range(num_projections))
|
|
134
|
+
rng = np.random.default_rng(int(params.projection_order_seed))
|
|
135
|
+
order = np.arange(num_projections, dtype=np.int32)
|
|
136
|
+
rng.shuffle(order)
|
|
137
|
+
return tuple(int(index) for index in order)
|
|
138
|
+
|
|
139
|
+
order = tuple(int(index) for index in params.projection_order)
|
|
140
|
+
if len(order) != num_projections:
|
|
141
|
+
raise ValueError(
|
|
142
|
+
"projection_order must contain one entry per measured projection: "
|
|
143
|
+
f"expected {num_projections}, got {len(order)}."
|
|
144
|
+
)
|
|
145
|
+
invalid = [index for index in order if index < 0 or index >= num_projections]
|
|
146
|
+
if invalid:
|
|
147
|
+
raise ValueError(f"projection_order contains invalid indices: {invalid!r}.")
|
|
148
|
+
return order
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def projection_subsets(
|
|
152
|
+
order: Sequence[int],
|
|
153
|
+
subset_count: int,
|
|
154
|
+
) -> tuple[tuple[int, ...], ...]:
|
|
155
|
+
"""Split an ordered traversal into interleaved ordered subsets."""
|
|
156
|
+
order_tuple = tuple(int(index) for index in order)
|
|
157
|
+
if not order_tuple:
|
|
158
|
+
return tuple()
|
|
159
|
+
subset_count = max(1, min(int(subset_count), len(order_tuple)))
|
|
160
|
+
if subset_count == 1:
|
|
161
|
+
return (order_tuple,)
|
|
162
|
+
return tuple(
|
|
163
|
+
tuple(order_tuple[offset::subset_count])
|
|
164
|
+
for offset in range(subset_count)
|
|
165
|
+
if order_tuple[offset::subset_count]
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def initialize_volume(params: ReconstructionParameters) -> tuple[mx.array, mx.array, mx.array]:
|
|
170
|
+
"""Create the reconstruction, ones, and zero reference volumes."""
|
|
171
|
+
if params.initial_volume is None:
|
|
172
|
+
volume = mx.zeros(params.volume_shape, dtype=params.dtype)
|
|
173
|
+
else:
|
|
174
|
+
volume = as_mx_array(params.initial_volume, dtype=params.dtype)
|
|
175
|
+
ones_volume = mx.ones(params.volume_shape, dtype=params.dtype)
|
|
176
|
+
zero_volume = mx.zeros(params.volume_shape, dtype=params.dtype)
|
|
177
|
+
return volume, ones_volume, zero_volume
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def clamp_volume(
|
|
181
|
+
volume: mx.array,
|
|
182
|
+
voxel_extreme_values: tuple[float, float],
|
|
183
|
+
*,
|
|
184
|
+
enforce_positivity: bool,
|
|
185
|
+
) -> mx.array:
|
|
186
|
+
"""Apply configured bounds to a reconstruction volume."""
|
|
187
|
+
lower, upper = voxel_extreme_values
|
|
188
|
+
if enforce_positivity:
|
|
189
|
+
lower = max(lower, 0.0)
|
|
190
|
+
if np.isfinite(lower):
|
|
191
|
+
volume = mx.maximum(volume, float(lower))
|
|
192
|
+
if np.isfinite(upper):
|
|
193
|
+
volume = mx.minimum(volume, float(upper))
|
|
194
|
+
return volume
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def should_enforce_positivity(
|
|
198
|
+
params: ReconstructionParameters,
|
|
199
|
+
*,
|
|
200
|
+
stage: str,
|
|
201
|
+
) -> bool:
|
|
202
|
+
"""Resolve whether positivity should be enforced at the given stage."""
|
|
203
|
+
if not params.enforce_positivity:
|
|
204
|
+
return False
|
|
205
|
+
if params.positivity_mode == "none":
|
|
206
|
+
return False
|
|
207
|
+
if params.positivity_mode == "final":
|
|
208
|
+
return stage == "final"
|
|
209
|
+
return stage in {"iteration", "regularization", "final"}
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def clamp_reconstruction_volume(
|
|
213
|
+
volume: mx.array,
|
|
214
|
+
params: ReconstructionParameters,
|
|
215
|
+
*,
|
|
216
|
+
stage: str,
|
|
217
|
+
) -> mx.array:
|
|
218
|
+
"""Clamp a reconstruction volume according to the configured positivity policy."""
|
|
219
|
+
volume = clamp_volume(
|
|
220
|
+
volume,
|
|
221
|
+
params.voxel_extreme_values,
|
|
222
|
+
enforce_positivity=should_enforce_positivity(params, stage=stage),
|
|
223
|
+
)
|
|
224
|
+
volume = apply_volume_border_mask(volume, params.volume_border_width)
|
|
225
|
+
if should_apply_volume_support_mask(params, stage=stage):
|
|
226
|
+
return apply_volume_support_mask(volume, params.volume_support_mask)
|
|
227
|
+
return volume
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def apply_volume_border_mask(volume: mx.array, border_width: int) -> mx.array:
|
|
231
|
+
"""Suppress voxels at the reconstruction-volume border."""
|
|
232
|
+
border_width = int(border_width)
|
|
233
|
+
if border_width <= 0:
|
|
234
|
+
return volume
|
|
235
|
+
shape = tuple(int(dimension) for dimension in volume.shape)
|
|
236
|
+
if not shape:
|
|
237
|
+
return volume
|
|
238
|
+
mask = volume_border_keep_mask(shape, border_width)
|
|
239
|
+
return mx.where(mask, volume, 0.0)
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def apply_volume_support_mask(volume: mx.array, support_mask: ArrayLike | None) -> mx.array:
|
|
243
|
+
"""Suppress voxels outside the configured reconstruction support."""
|
|
244
|
+
if support_mask is None:
|
|
245
|
+
return volume
|
|
246
|
+
mask = support_mask if hasattr(support_mask, "shape") else as_mx_array(support_mask, dtype=mx.float32)
|
|
247
|
+
return mx.where(mask > 0.0, volume, 0.0)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def should_apply_volume_support_mask(
|
|
251
|
+
params: ReconstructionParameters,
|
|
252
|
+
*,
|
|
253
|
+
stage: str,
|
|
254
|
+
) -> bool:
|
|
255
|
+
"""Resolve whether the configured reconstruction support should be enforced."""
|
|
256
|
+
if params.volume_support_mask is None:
|
|
257
|
+
return False
|
|
258
|
+
if params.volume_support_mask_mode == "none":
|
|
259
|
+
return False
|
|
260
|
+
if params.volume_support_mask_mode == "final":
|
|
261
|
+
return stage == "final"
|
|
262
|
+
return True
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
@lru_cache(maxsize=32)
|
|
266
|
+
def volume_border_keep_mask(shape: tuple[int, ...], border_width: int) -> mx.array:
|
|
267
|
+
"""Cache a keep-mask that removes a fixed-width border from a volume."""
|
|
268
|
+
mask_np = np.ones(shape, dtype=bool)
|
|
269
|
+
for axis, axis_size in enumerate(shape):
|
|
270
|
+
trim = min(int(border_width), axis_size)
|
|
271
|
+
if trim <= 0:
|
|
272
|
+
continue
|
|
273
|
+
leading = [slice(None)] * len(shape)
|
|
274
|
+
trailing = [slice(None)] * len(shape)
|
|
275
|
+
leading[axis] = slice(0, trim)
|
|
276
|
+
trailing[axis] = slice(axis_size - trim, axis_size)
|
|
277
|
+
mask_np[tuple(leading)] = False
|
|
278
|
+
mask_np[tuple(trailing)] = False
|
|
279
|
+
return mx.array(mask_np)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def print_progress(iteration: int, iteration_count: int) -> None:
|
|
283
|
+
"""Print a small textual progress bar."""
|
|
284
|
+
iteration_number = iteration + 1
|
|
285
|
+
finished = int(10.0 * iteration_number / iteration_count)
|
|
286
|
+
remaining = max(0, 10 - finished)
|
|
287
|
+
progress = ("█" * finished) + ("-" * remaining)
|
|
288
|
+
print(
|
|
289
|
+
f"\rProgress: |{progress}| Finished Iterations: {iteration_number} out of {iteration_count}",
|
|
290
|
+
end="",
|
|
291
|
+
flush=True,
|
|
292
|
+
)
|
|
293
|
+
if iteration_number == iteration_count:
|
|
294
|
+
print()
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def scalar_norm(value: mx.array) -> float:
|
|
298
|
+
"""Return an MLX norm as a Python float."""
|
|
299
|
+
return float(np.asarray(mx.linalg.norm(value)))
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def projection_residual_norm(
|
|
303
|
+
volume: mx.array,
|
|
304
|
+
measured_projection: mx.array,
|
|
305
|
+
forward_project: ForwardProjector,
|
|
306
|
+
projection_index: int,
|
|
307
|
+
) -> float:
|
|
308
|
+
"""Compute the residual norm for one measured projection."""
|
|
309
|
+
computed_projection = forward_project(volume, projection_index)
|
|
310
|
+
return scalar_norm(measured_projection - computed_projection)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def threshold_small_raylengths(
|
|
314
|
+
raylength_projection: mx.array,
|
|
315
|
+
quantile: float,
|
|
316
|
+
) -> mx.array:
|
|
317
|
+
"""Drop the smallest positive raylength values to improve SART stability."""
|
|
318
|
+
if quantile <= 0.0:
|
|
319
|
+
return raylength_projection
|
|
320
|
+
raylength_np = np.asarray(raylength_projection)
|
|
321
|
+
positive_values = raylength_np[raylength_np > 0.0]
|
|
322
|
+
if positive_values.size == 0:
|
|
323
|
+
return raylength_projection
|
|
324
|
+
threshold = float(np.quantile(positive_values, quantile))
|
|
325
|
+
mask = (raylength_projection > 0.0) & (raylength_projection <= threshold)
|
|
326
|
+
return mx.where(mask, 0.0, raylength_projection)
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def prepare_raylength_projection(
|
|
330
|
+
raylength_projection: mx.array,
|
|
331
|
+
params: ReconstructionParameters,
|
|
332
|
+
) -> mx.array:
|
|
333
|
+
"""Apply the configured raylength preprocessing once per projection."""
|
|
334
|
+
if not params.raylength_thresholding:
|
|
335
|
+
return raylength_projection
|
|
336
|
+
return threshold_small_raylengths(raylength_projection, params.raylength_quantile)
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def compute_sart_correction(
|
|
340
|
+
measured_projection: mx.array,
|
|
341
|
+
computed_projection: mx.array,
|
|
342
|
+
raylength_projection: mx.array,
|
|
343
|
+
params: ReconstructionParameters,
|
|
344
|
+
) -> mx.array:
|
|
345
|
+
"""Compute the SART correction image for one measured projection."""
|
|
346
|
+
clipped_projection = prepare_measured_projection(measured_projection, computed_projection, params)
|
|
347
|
+
epsilon = float(params.raylength_epsilon)
|
|
348
|
+
mask = raylength_projection > epsilon
|
|
349
|
+
numerator = clipped_projection - computed_projection
|
|
350
|
+
denominator = mx.maximum(raylength_projection, epsilon)
|
|
351
|
+
normalized_update = numerator / denominator
|
|
352
|
+
normalized_update = mx.where(mask, normalized_update, 0.0)
|
|
353
|
+
return apply_detector_border_mask(normalized_update, params, fill_value=0.0)
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def compute_detector_update_mask(
|
|
357
|
+
raylength_projection: mx.array,
|
|
358
|
+
params: ReconstructionParameters,
|
|
359
|
+
) -> mx.array:
|
|
360
|
+
"""Return detector pixels that participate in an iterative update."""
|
|
361
|
+
valid = mx.where(raylength_projection > float(params.raylength_epsilon), 1.0, 0.0)
|
|
362
|
+
return apply_detector_border_mask(valid, params, fill_value=0.0)
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def normalize_backprojection_by_sensitivity(
|
|
366
|
+
backprojection_volume: mx.array,
|
|
367
|
+
sensitivity_volume: mx.array,
|
|
368
|
+
params: ReconstructionParameters,
|
|
369
|
+
) -> mx.array:
|
|
370
|
+
"""Divide voxel updates by the local ray-coverage sensitivity."""
|
|
371
|
+
if not params.voxel_sensitivity_normalization:
|
|
372
|
+
return backprojection_volume
|
|
373
|
+
epsilon = float(params.raylength_epsilon)
|
|
374
|
+
return mx.where(
|
|
375
|
+
sensitivity_volume > epsilon,
|
|
376
|
+
backprojection_volume / mx.maximum(sensitivity_volume, epsilon),
|
|
377
|
+
0.0,
|
|
378
|
+
)
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def prepare_measured_projection(
|
|
382
|
+
measured_projection: mx.array,
|
|
383
|
+
computed_projection: mx.array,
|
|
384
|
+
params: ReconstructionParameters,
|
|
385
|
+
) -> mx.array:
|
|
386
|
+
"""Apply measurement clipping before the SART normalization step."""
|
|
387
|
+
lower, upper = params.pixel_extreme_values
|
|
388
|
+
clipped_projection = measured_projection
|
|
389
|
+
if np.isfinite(upper):
|
|
390
|
+
clipped_projection = mx.where(clipped_projection > float(upper), computed_projection, clipped_projection)
|
|
391
|
+
if np.isfinite(lower):
|
|
392
|
+
clipped_projection = mx.where(clipped_projection < float(lower), 0.0, clipped_projection)
|
|
393
|
+
return clipped_projection
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def apply_detector_border_mask(
|
|
397
|
+
value: mx.array,
|
|
398
|
+
params: ReconstructionParameters,
|
|
399
|
+
*,
|
|
400
|
+
fill_value: float = 0.0,
|
|
401
|
+
) -> mx.array:
|
|
402
|
+
"""Suppress detector-border pixels in 1D or 2D projection arrays."""
|
|
403
|
+
border_u = int(params.detector_border_u)
|
|
404
|
+
border_v = int(params.detector_border_v)
|
|
405
|
+
if border_u <= 0 and border_v <= 0:
|
|
406
|
+
return value
|
|
407
|
+
|
|
408
|
+
shape = tuple(int(dimension) for dimension in value.shape)
|
|
409
|
+
if len(shape) not in {1, 2}:
|
|
410
|
+
return value
|
|
411
|
+
mask = detector_border_keep_mask(shape, border_u, border_v)
|
|
412
|
+
return mx.where(mask, value, fill_value)
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
@lru_cache(maxsize=64)
|
|
416
|
+
def detector_border_keep_mask(
|
|
417
|
+
shape: tuple[int, ...],
|
|
418
|
+
border_u: int,
|
|
419
|
+
border_v: int,
|
|
420
|
+
) -> mx.array:
|
|
421
|
+
"""Cache detector-border keep masks on the MLX side."""
|
|
422
|
+
mask_np = np.ones(shape, dtype=bool)
|
|
423
|
+
if len(shape) == 1:
|
|
424
|
+
if border_u > 0:
|
|
425
|
+
trim_u = min(border_u, shape[0])
|
|
426
|
+
mask_np[:trim_u] = False
|
|
427
|
+
mask_np[-trim_u:] = False
|
|
428
|
+
return mx.array(mask_np)
|
|
429
|
+
|
|
430
|
+
trim_u = min(border_u, shape[0]) if border_u > 0 else 0
|
|
431
|
+
trim_v = min(border_v, shape[1]) if border_v > 0 else 0
|
|
432
|
+
if trim_u > 0:
|
|
433
|
+
mask_np[:trim_u, :] = False
|
|
434
|
+
mask_np[-trim_u:, :] = False
|
|
435
|
+
if trim_v > 0:
|
|
436
|
+
mask_np[:, :trim_v] = False
|
|
437
|
+
mask_np[:, -trim_v:] = False
|
|
438
|
+
return mx.array(mask_np)
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
def _array_stats(name: str, value: mx.array) -> dict[str, float]:
|
|
442
|
+
"""Return compact numeric stats for one MLX array."""
|
|
443
|
+
value_np = np.asarray(value)
|
|
444
|
+
return {
|
|
445
|
+
f"{name}_min": float(np.min(value_np)),
|
|
446
|
+
f"{name}_max": float(np.max(value_np)),
|
|
447
|
+
f"{name}_mean": float(np.mean(value_np)),
|
|
448
|
+
f"{name}_norm": float(np.linalg.norm(value_np)),
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def _raylength_stats(raylength_projection: mx.array) -> dict[str, float]:
|
|
453
|
+
"""Return SART-relevant stats for the raylength projection."""
|
|
454
|
+
raylength_np = np.asarray(raylength_projection)
|
|
455
|
+
positive_values = raylength_np[raylength_np > 0.0]
|
|
456
|
+
if positive_values.size == 0:
|
|
457
|
+
return {
|
|
458
|
+
"raylength_nonzero_count": 0.0,
|
|
459
|
+
"raylength_positive_min": 0.0,
|
|
460
|
+
"raylength_positive_p001": 0.0,
|
|
461
|
+
"raylength_positive_mean": 0.0,
|
|
462
|
+
"raylength_positive_max": 0.0,
|
|
463
|
+
}
|
|
464
|
+
return {
|
|
465
|
+
"raylength_nonzero_count": float(positive_values.size),
|
|
466
|
+
"raylength_positive_min": float(np.min(positive_values)),
|
|
467
|
+
"raylength_positive_p001": float(np.quantile(positive_values, 1e-3)),
|
|
468
|
+
"raylength_positive_mean": float(np.mean(positive_values)),
|
|
469
|
+
"raylength_positive_max": float(np.max(positive_values)),
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
def projection_slice_ranges(
|
|
474
|
+
projection_count: int,
|
|
475
|
+
params: ReconstructionParameters,
|
|
476
|
+
) -> tuple[tuple[int, int], ...]:
|
|
477
|
+
"""Resolve contiguous projection slices for optional chunked projector calls."""
|
|
478
|
+
chunk_size = params.projection_chunk_size
|
|
479
|
+
if chunk_size is None or chunk_size <= 1:
|
|
480
|
+
return tuple((index, index + 1) for index in range(projection_count))
|
|
481
|
+
return tuple(
|
|
482
|
+
(start, min(start + int(chunk_size), projection_count))
|
|
483
|
+
for start in range(0, projection_count, int(chunk_size))
|
|
484
|
+
)
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
def forward_project_views(
|
|
488
|
+
volume: mx.array,
|
|
489
|
+
forward_project: ForwardProjector,
|
|
490
|
+
projection_count: int,
|
|
491
|
+
params: ReconstructionParameters,
|
|
492
|
+
) -> list[mx.array]:
|
|
493
|
+
"""Project a fixed volume over all views, optionally using contiguous chunks."""
|
|
494
|
+
project_slice = getattr(forward_project, "project_slice", None)
|
|
495
|
+
if project_slice is None or params.projection_chunk_size is None or params.projection_chunk_size <= 1:
|
|
496
|
+
return [forward_project(volume, projection_index) for projection_index in range(projection_count)]
|
|
497
|
+
|
|
498
|
+
projections: list[mx.array] = [None] * projection_count # type: ignore[list-item]
|
|
499
|
+
for start, stop in projection_slice_ranges(projection_count, params):
|
|
500
|
+
chunk_projection = project_slice(volume, start, stop)
|
|
501
|
+
for offset, projection_index in enumerate(range(start, stop)):
|
|
502
|
+
projections[projection_index] = chunk_projection[offset]
|
|
503
|
+
return projections
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
def precompute_raylength_projections(
|
|
507
|
+
ones_volume: mx.array,
|
|
508
|
+
forward_project: ForwardProjector,
|
|
509
|
+
params: ReconstructionParameters,
|
|
510
|
+
projection_count: int,
|
|
511
|
+
) -> dict[int, mx.array]:
|
|
512
|
+
"""Cache per-view raylength projections for one iterative sweep call."""
|
|
513
|
+
projected_ones = forward_project_views(
|
|
514
|
+
ones_volume,
|
|
515
|
+
forward_project,
|
|
516
|
+
projection_count,
|
|
517
|
+
params,
|
|
518
|
+
)
|
|
519
|
+
raylengths: dict[int, mx.array] = {}
|
|
520
|
+
for projection_index in range(projection_count):
|
|
521
|
+
prepared_projection = prepare_raylength_projection(projected_ones[int(projection_index)], params)
|
|
522
|
+
mx.eval(prepared_projection)
|
|
523
|
+
raylengths[int(projection_index)] = prepared_projection
|
|
524
|
+
return raylengths
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
def precompute_sensitivity_volumes(
|
|
528
|
+
raylength_projections: dict[int, mx.array],
|
|
529
|
+
back_project: BackProjector,
|
|
530
|
+
params: ReconstructionParameters,
|
|
531
|
+
projection_count: int,
|
|
532
|
+
) -> dict[int, mx.array] | None:
|
|
533
|
+
"""Precompute per-view voxel-coverage sensitivity volumes for all projections.
|
|
534
|
+
|
|
535
|
+
WARNING: stores one full-resolution volume per projection view. Memory cost is
|
|
536
|
+
projection_count × volume_voxels × 4 bytes — use only when projection_count is
|
|
537
|
+
small (e.g. a few dozen views) or the volume is small. For large scans (hundreds
|
|
538
|
+
of views, 384³+ volumes) call compute_voxel_sensitivity_volume per-view on the
|
|
539
|
+
fly instead, as SART does.
|
|
540
|
+
"""
|
|
541
|
+
if not params.voxel_sensitivity_normalization:
|
|
542
|
+
return None
|
|
543
|
+
sensitivity_volumes: dict[int, mx.array] = {}
|
|
544
|
+
for projection_index in range(projection_count):
|
|
545
|
+
sv = compute_voxel_sensitivity_volume(
|
|
546
|
+
raylength_projections[int(projection_index)],
|
|
547
|
+
back_project,
|
|
548
|
+
projection_index,
|
|
549
|
+
params,
|
|
550
|
+
)
|
|
551
|
+
sensitivity_volumes[int(projection_index)] = sv
|
|
552
|
+
return sensitivity_volumes
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
def compute_voxel_sensitivity_volume(
|
|
556
|
+
raylength_projection: mx.array,
|
|
557
|
+
back_project: BackProjector,
|
|
558
|
+
projection_index: int,
|
|
559
|
+
params: ReconstructionParameters,
|
|
560
|
+
) -> mx.array | None:
|
|
561
|
+
"""Compute the voxel-coverage normalization volume for one projection."""
|
|
562
|
+
if not params.voxel_sensitivity_normalization:
|
|
563
|
+
return None
|
|
564
|
+
detector_mask = compute_detector_update_mask(raylength_projection, params)
|
|
565
|
+
sensitivity_volume = back_project(detector_mask, int(projection_index))
|
|
566
|
+
sensitivity_volume = apply_volume_border_mask(sensitivity_volume, params.volume_border_width)
|
|
567
|
+
mx.eval(sensitivity_volume)
|
|
568
|
+
return sensitivity_volume
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
def resolve_backprojection_scale(params: ReconstructionParameters) -> float:
|
|
572
|
+
"""Resolve the multiplicative update scale for the configured iterative mode."""
|
|
573
|
+
if params.backprojection_scale is not None:
|
|
574
|
+
return float(params.backprojection_scale)
|
|
575
|
+
if params.iterative_update_method == "normalized_sart":
|
|
576
|
+
return float(params.normalized_sart_relaxation)
|
|
577
|
+
return 1.0
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
def resolve_projection_weights(
|
|
581
|
+
measured_projections: Sequence[mx.array],
|
|
582
|
+
params: ReconstructionParameters,
|
|
583
|
+
) -> tuple[float, ...]:
|
|
584
|
+
"""Resolve per-view quadrature weights for iterative updates."""
|
|
585
|
+
if params.projection_weights is None:
|
|
586
|
+
return tuple(1.0 for _ in range(len(measured_projections)))
|
|
587
|
+
return tuple(float(value) for value in params.projection_weights)
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
def run_sart_sweeps(
|
|
591
|
+
volume: mx.array,
|
|
592
|
+
measured_projections: Sequence[mx.array],
|
|
593
|
+
ones_volume: mx.array,
|
|
594
|
+
forward_project: ForwardProjector,
|
|
595
|
+
back_project: BackProjector,
|
|
596
|
+
params: ReconstructionParameters,
|
|
597
|
+
*,
|
|
598
|
+
beta: float = 1.0,
|
|
599
|
+
outer_iteration_index: int = 0,
|
|
600
|
+
) -> mx.array:
|
|
601
|
+
"""Run the configured number of SART sweeps over all provided projections."""
|
|
602
|
+
scale = resolve_backprojection_scale(params)
|
|
603
|
+
order = projection_order(params, len(measured_projections))
|
|
604
|
+
raylength_projections = precompute_raylength_projections(
|
|
605
|
+
ones_volume,
|
|
606
|
+
forward_project,
|
|
607
|
+
params,
|
|
608
|
+
len(measured_projections),
|
|
609
|
+
)
|
|
610
|
+
for sweep_index in range(params.sart_iteration_count):
|
|
611
|
+
for step_index, projection_index in enumerate(order):
|
|
612
|
+
measured_projection = measured_projections[projection_index]
|
|
613
|
+
computed_projection = forward_project(volume, projection_index)
|
|
614
|
+
raylength_projection = raylength_projections[int(projection_index)]
|
|
615
|
+
correction_image = compute_sart_correction(
|
|
616
|
+
measured_projection=measured_projection,
|
|
617
|
+
computed_projection=computed_projection,
|
|
618
|
+
raylength_projection=raylength_projection,
|
|
619
|
+
params=params,
|
|
620
|
+
)
|
|
621
|
+
backprojection_volume = back_project(correction_image, projection_index)
|
|
622
|
+
if params.voxel_sensitivity_normalization:
|
|
623
|
+
sensitivity_volume = compute_voxel_sensitivity_volume(
|
|
624
|
+
raylength_projection,
|
|
625
|
+
back_project,
|
|
626
|
+
int(projection_index),
|
|
627
|
+
params,
|
|
628
|
+
)
|
|
629
|
+
backprojection_volume = normalize_backprojection_by_sensitivity(
|
|
630
|
+
backprojection_volume,
|
|
631
|
+
sensitivity_volume,
|
|
632
|
+
params,
|
|
633
|
+
)
|
|
634
|
+
volume = volume + (float(beta) * scale * backprojection_volume)
|
|
635
|
+
volume = clamp_reconstruction_volume(volume, params, stage="sart_update")
|
|
636
|
+
if params.sart_debug_callback is not None:
|
|
637
|
+
debug_stats = {
|
|
638
|
+
"outer_iteration": float(outer_iteration_index),
|
|
639
|
+
"sart_sweep": float(sweep_index),
|
|
640
|
+
"projection_index": float(projection_index),
|
|
641
|
+
"beta": float(beta),
|
|
642
|
+
"backprojection_scale": scale,
|
|
643
|
+
**_array_stats("measured", measured_projection),
|
|
644
|
+
**_array_stats("computed", computed_projection),
|
|
645
|
+
**_raylength_stats(raylength_projection),
|
|
646
|
+
**_array_stats("correction", correction_image),
|
|
647
|
+
**_array_stats("backprojection", backprojection_volume),
|
|
648
|
+
**_array_stats("volume", volume),
|
|
649
|
+
}
|
|
650
|
+
params.sart_debug_callback(debug_stats)
|
|
651
|
+
if (step_index + 1) % _MATERIALIZE_INTERVAL == 0:
|
|
652
|
+
mx.eval(volume)
|
|
653
|
+
# Materialize the updated volume after each sweep to keep the MLX/Metal
|
|
654
|
+
# graph bounded on large 3D measured-data runs.
|
|
655
|
+
mx.eval(volume)
|
|
656
|
+
return volume
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
def run_sirt_sweeps(
|
|
660
|
+
volume: mx.array,
|
|
661
|
+
measured_projections: Sequence[mx.array],
|
|
662
|
+
ones_volume: mx.array,
|
|
663
|
+
forward_project: ForwardProjector,
|
|
664
|
+
back_project: BackProjector,
|
|
665
|
+
params: ReconstructionParameters,
|
|
666
|
+
*,
|
|
667
|
+
beta: float = 1.0,
|
|
668
|
+
outer_iteration_index: int = 0,
|
|
669
|
+
) -> mx.array:
|
|
670
|
+
"""Run the configured number of SIRT sweeps over all provided projections."""
|
|
671
|
+
scale = resolve_backprojection_scale(params)
|
|
672
|
+
order = projection_order(params, len(measured_projections))
|
|
673
|
+
raylength_projections = precompute_raylength_projections(
|
|
674
|
+
ones_volume,
|
|
675
|
+
forward_project,
|
|
676
|
+
params,
|
|
677
|
+
len(measured_projections),
|
|
678
|
+
)
|
|
679
|
+
projection_count = float(len(order))
|
|
680
|
+
# Aggregate sensitivity volume is geometry-only; compute once before sweeps.
|
|
681
|
+
sirt_sensitivity: mx.array | None = None
|
|
682
|
+
if params.voxel_sensitivity_normalization:
|
|
683
|
+
sirt_sensitivity = mx.zeros(params.volume_shape, dtype=params.dtype)
|
|
684
|
+
for projection_index in range(len(measured_projections)):
|
|
685
|
+
sv = compute_voxel_sensitivity_volume(
|
|
686
|
+
raylength_projections[int(projection_index)],
|
|
687
|
+
back_project,
|
|
688
|
+
projection_index,
|
|
689
|
+
params,
|
|
690
|
+
)
|
|
691
|
+
sirt_sensitivity = sirt_sensitivity + sv
|
|
692
|
+
mx.eval(sirt_sensitivity)
|
|
693
|
+
for sweep_index in range(params.sart_iteration_count):
|
|
694
|
+
reference_volume = volume
|
|
695
|
+
accumulated_backprojection = mx.zeros_like(volume)
|
|
696
|
+
for step_index, projection_index in enumerate(order):
|
|
697
|
+
measured_projection = measured_projections[projection_index]
|
|
698
|
+
computed_projection = forward_project(reference_volume, projection_index)
|
|
699
|
+
raylength_projection = raylength_projections[int(projection_index)]
|
|
700
|
+
correction_image = compute_sart_correction(
|
|
701
|
+
measured_projection=measured_projection,
|
|
702
|
+
computed_projection=computed_projection,
|
|
703
|
+
raylength_projection=raylength_projection,
|
|
704
|
+
params=params,
|
|
705
|
+
)
|
|
706
|
+
backprojection_volume = back_project(correction_image, projection_index)
|
|
707
|
+
accumulated_backprojection = accumulated_backprojection + backprojection_volume
|
|
708
|
+
if params.sart_debug_callback is not None:
|
|
709
|
+
debug_stats = {
|
|
710
|
+
"outer_iteration": float(outer_iteration_index),
|
|
711
|
+
"sart_sweep": float(sweep_index),
|
|
712
|
+
"projection_index": float(projection_index),
|
|
713
|
+
"beta": float(beta),
|
|
714
|
+
"backprojection_scale": scale,
|
|
715
|
+
"iterative_update_method": 1.0,
|
|
716
|
+
**_array_stats("measured", measured_projection),
|
|
717
|
+
**_array_stats("computed", computed_projection),
|
|
718
|
+
**_raylength_stats(raylength_projection),
|
|
719
|
+
**_array_stats("correction", correction_image),
|
|
720
|
+
**_array_stats("backprojection", backprojection_volume),
|
|
721
|
+
**_array_stats("volume", reference_volume),
|
|
722
|
+
}
|
|
723
|
+
params.sart_debug_callback(debug_stats)
|
|
724
|
+
if (step_index + 1) % _MATERIALIZE_INTERVAL == 0:
|
|
725
|
+
mx.eval(accumulated_backprojection)
|
|
726
|
+
if sirt_sensitivity is not None:
|
|
727
|
+
averaged_backprojection = normalize_backprojection_by_sensitivity(
|
|
728
|
+
accumulated_backprojection,
|
|
729
|
+
sirt_sensitivity,
|
|
730
|
+
params,
|
|
731
|
+
)
|
|
732
|
+
else:
|
|
733
|
+
averaged_backprojection = accumulated_backprojection / projection_count
|
|
734
|
+
volume = volume + (float(beta) * scale * averaged_backprojection)
|
|
735
|
+
volume = clamp_reconstruction_volume(volume, params, stage="sart_update")
|
|
736
|
+
mx.eval(volume)
|
|
737
|
+
return volume
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
def run_normalized_sart_sweeps(
|
|
741
|
+
volume: mx.array,
|
|
742
|
+
measured_projections: Sequence[mx.array],
|
|
743
|
+
ones_volume: mx.array,
|
|
744
|
+
forward_project: ForwardProjector,
|
|
745
|
+
back_project: BackProjector,
|
|
746
|
+
params: ReconstructionParameters,
|
|
747
|
+
*,
|
|
748
|
+
beta: float = 1.0,
|
|
749
|
+
outer_iteration_index: int = 0,
|
|
750
|
+
) -> mx.array:
|
|
751
|
+
"""Run LEAP-style normalized SART updates over full passes or ordered subsets."""
|
|
752
|
+
scale = resolve_backprojection_scale(params)
|
|
753
|
+
order = projection_order(params, len(measured_projections))
|
|
754
|
+
projection_weights = resolve_projection_weights(measured_projections, params)
|
|
755
|
+
subsets = projection_subsets(order, params.projection_subset_count)
|
|
756
|
+
raylength_projections = precompute_raylength_projections(
|
|
757
|
+
ones_volume,
|
|
758
|
+
forward_project,
|
|
759
|
+
params,
|
|
760
|
+
len(measured_projections),
|
|
761
|
+
)
|
|
762
|
+
epsilon = float(params.raylength_epsilon)
|
|
763
|
+
|
|
764
|
+
for sweep_index in range(params.sart_iteration_count):
|
|
765
|
+
for subset_index, subset in enumerate(subsets):
|
|
766
|
+
reference_volume = volume
|
|
767
|
+
accumulated_backprojection = mx.zeros_like(volume)
|
|
768
|
+
accumulated_sensitivity = mx.zeros_like(volume)
|
|
769
|
+
computed_projections = None
|
|
770
|
+
if len(subset) == len(order):
|
|
771
|
+
computed_projections = forward_project_views(
|
|
772
|
+
reference_volume,
|
|
773
|
+
forward_project,
|
|
774
|
+
len(measured_projections),
|
|
775
|
+
params,
|
|
776
|
+
)
|
|
777
|
+
for step_index, projection_index in enumerate(subset):
|
|
778
|
+
projection_weight = float(projection_weights[int(projection_index)])
|
|
779
|
+
measured_projection = measured_projections[projection_index]
|
|
780
|
+
if computed_projections is None:
|
|
781
|
+
computed_projection = forward_project(reference_volume, projection_index)
|
|
782
|
+
else:
|
|
783
|
+
computed_projection = computed_projections[int(projection_index)]
|
|
784
|
+
raylength_projection = raylength_projections[int(projection_index)]
|
|
785
|
+
correction_image = compute_sart_correction(
|
|
786
|
+
measured_projection=measured_projection,
|
|
787
|
+
computed_projection=computed_projection,
|
|
788
|
+
raylength_projection=raylength_projection,
|
|
789
|
+
params=params,
|
|
790
|
+
)
|
|
791
|
+
backprojection_volume = back_project(correction_image, projection_index)
|
|
792
|
+
accumulated_backprojection = accumulated_backprojection + (projection_weight * backprojection_volume)
|
|
793
|
+
detector_mask = compute_detector_update_mask(raylength_projection, params)
|
|
794
|
+
accumulated_sensitivity = accumulated_sensitivity + (
|
|
795
|
+
projection_weight * back_project(detector_mask, projection_index)
|
|
796
|
+
)
|
|
797
|
+
if params.sart_debug_callback is not None:
|
|
798
|
+
debug_stats = {
|
|
799
|
+
"outer_iteration": float(outer_iteration_index),
|
|
800
|
+
"sart_sweep": float(sweep_index),
|
|
801
|
+
"projection_index": float(projection_index),
|
|
802
|
+
"subset_index": float(subset_index),
|
|
803
|
+
"subset_size": float(len(subset)),
|
|
804
|
+
"projection_weight": projection_weight,
|
|
805
|
+
"beta": float(beta),
|
|
806
|
+
"backprojection_scale": scale,
|
|
807
|
+
"iterative_update_method": 2.0,
|
|
808
|
+
**_array_stats("measured", measured_projection),
|
|
809
|
+
**_array_stats("computed", computed_projection),
|
|
810
|
+
**_raylength_stats(raylength_projection),
|
|
811
|
+
**_array_stats("correction", correction_image),
|
|
812
|
+
**_array_stats("backprojection", backprojection_volume),
|
|
813
|
+
**_array_stats("volume", reference_volume),
|
|
814
|
+
}
|
|
815
|
+
params.sart_debug_callback(debug_stats)
|
|
816
|
+
if (step_index + 1) % _MATERIALIZE_INTERVAL == 0:
|
|
817
|
+
mx.eval(accumulated_backprojection, accumulated_sensitivity)
|
|
818
|
+
normalized_update = mx.where(
|
|
819
|
+
accumulated_sensitivity > epsilon,
|
|
820
|
+
accumulated_backprojection / mx.maximum(accumulated_sensitivity, epsilon),
|
|
821
|
+
0.0,
|
|
822
|
+
)
|
|
823
|
+
volume = volume + (float(beta) * scale * normalized_update)
|
|
824
|
+
volume = clamp_reconstruction_volume(volume, params, stage="sart_update")
|
|
825
|
+
mx.eval(volume)
|
|
826
|
+
return volume
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
def run_iterative_sweeps(
|
|
830
|
+
volume: mx.array,
|
|
831
|
+
measured_projections: Sequence[mx.array],
|
|
832
|
+
ones_volume: mx.array,
|
|
833
|
+
forward_project: ForwardProjector,
|
|
834
|
+
back_project: BackProjector,
|
|
835
|
+
params: ReconstructionParameters,
|
|
836
|
+
*,
|
|
837
|
+
beta: float = 1.0,
|
|
838
|
+
outer_iteration_index: int = 0,
|
|
839
|
+
) -> mx.array:
|
|
840
|
+
"""Dispatch to the configured iterative sweep method."""
|
|
841
|
+
if params.iterative_update_method == "normalized_sart":
|
|
842
|
+
return run_normalized_sart_sweeps(
|
|
843
|
+
volume=volume,
|
|
844
|
+
measured_projections=measured_projections,
|
|
845
|
+
ones_volume=ones_volume,
|
|
846
|
+
forward_project=forward_project,
|
|
847
|
+
back_project=back_project,
|
|
848
|
+
params=params,
|
|
849
|
+
beta=beta,
|
|
850
|
+
outer_iteration_index=outer_iteration_index,
|
|
851
|
+
)
|
|
852
|
+
if params.iterative_update_method == "sirt":
|
|
853
|
+
return run_sirt_sweeps(
|
|
854
|
+
volume=volume,
|
|
855
|
+
measured_projections=measured_projections,
|
|
856
|
+
ones_volume=ones_volume,
|
|
857
|
+
forward_project=forward_project,
|
|
858
|
+
back_project=back_project,
|
|
859
|
+
params=params,
|
|
860
|
+
beta=beta,
|
|
861
|
+
outer_iteration_index=outer_iteration_index,
|
|
862
|
+
)
|
|
863
|
+
return run_sart_sweeps(
|
|
864
|
+
volume=volume,
|
|
865
|
+
measured_projections=measured_projections,
|
|
866
|
+
ones_volume=ones_volume,
|
|
867
|
+
forward_project=forward_project,
|
|
868
|
+
back_project=back_project,
|
|
869
|
+
params=params,
|
|
870
|
+
beta=beta,
|
|
871
|
+
outer_iteration_index=outer_iteration_index,
|
|
872
|
+
)
|