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.
@@ -0,0 +1,101 @@
1
+ """Shared helpers for analytical reconstruction algorithms."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+ from typing import Any, Callable
7
+
8
+ import math
9
+ import mlx.core as mx
10
+ import numpy as np
11
+
12
+
13
+ AnalyticalWeighting = Callable[[mx.array], mx.array]
14
+ AnalyticalFilter = Callable[[mx.array], mx.array]
15
+ AnalyticalBackProjector = Callable[[mx.array], mx.array]
16
+
17
+
18
+ @dataclass
19
+ class AnalyticalReconstructionParameters:
20
+ """Common parameters for analytical reconstruction algorithms."""
21
+
22
+ detector_spacing: float = 1.0
23
+ voxel_spacing: float = 1.0
24
+ enforce_positivity: bool = True
25
+ normalization_scale: float | None = None
26
+ dtype: Any = mx.float32
27
+
28
+
29
+ def _as_array(value, dtype=None):
30
+ """Convert arbitrary array-like inputs to an MLX array."""
31
+ if dtype is None:
32
+ return mx.array(value)
33
+ return mx.array(value, dtype=dtype)
34
+
35
+
36
+ def _positive_and_scale(reconstruction, *, enforce_positivity, normalization_scale):
37
+ """Apply optional positivity and multiplicative normalization."""
38
+ reconstruction = mx.array(reconstruction)
39
+ if enforce_positivity:
40
+ reconstruction = mx.maximum(reconstruction, 0.0)
41
+ if normalization_scale is not None:
42
+ reconstruction = reconstruction * float(normalization_scale)
43
+ return reconstruction
44
+
45
+
46
+ def default_angular_normalization(num_views: int) -> float:
47
+ """Standard circular-scan angular normalization used in the examples."""
48
+ if int(num_views) <= 0:
49
+ raise ValueError("num_views must be positive.")
50
+ return math.pi / float(num_views)
51
+
52
+
53
+ def ramp_filter_2d(sinogram):
54
+ """Apply a ramp filter along the detector axis of a 2D sinogram."""
55
+ sino_np = np.asarray(sinogram, dtype=np.float32)
56
+ if sino_np.ndim != 2:
57
+ raise ValueError(f"Expected a 2D sinogram, got shape {sino_np.shape!r}.")
58
+
59
+ num_detectors = sino_np.shape[1]
60
+ freqs = np.fft.fftfreq(num_detectors)
61
+ # Use the standard discrete ramp for FFT frequencies in cycles / sample.
62
+ ramp = (2.0 * np.abs(freqs)).astype(np.float32)
63
+ sino_fft = np.fft.fft(sino_np, axis=1)
64
+ filtered = np.real(np.fft.ifft(sino_fft * ramp[None, :], axis=1)).astype(np.float32)
65
+ return mx.array(filtered)
66
+
67
+
68
+ def ramp_filter_3d(sinogram):
69
+ """Apply a ramp filter along the detector-u axis of a 3D cone sinogram."""
70
+ sino_np = np.asarray(sinogram, dtype=np.float32)
71
+ if sino_np.ndim != 3:
72
+ raise ValueError(f"Expected a 3D sinogram, got shape {sino_np.shape!r}.")
73
+
74
+ num_detectors_u = sino_np.shape[1]
75
+ freqs = np.fft.fftfreq(num_detectors_u)
76
+ ramp = (2.0 * np.abs(freqs)).astype(np.float32).reshape(1, num_detectors_u, 1)
77
+ sino_fft = np.fft.fft(sino_np, axis=1)
78
+ filtered = np.real(np.fft.ifft(sino_fft * ramp, axis=1)).astype(np.float32)
79
+ return mx.array(filtered)
80
+
81
+
82
+ def ramp_filter(sinogram, axis=1):
83
+ """Apply a ramp filter along an arbitrary detector axis."""
84
+ sino_np = np.asarray(sinogram, dtype=np.float32)
85
+ if sino_np.ndim < 2:
86
+ raise ValueError(f"Expected sinogram with at least 2 dimensions, got shape {sino_np.shape!r}.")
87
+
88
+ axis = int(axis)
89
+ if axis < 0:
90
+ axis += sino_np.ndim
91
+ if axis < 0 or axis >= sino_np.ndim:
92
+ raise ValueError(f"Invalid filter axis {axis} for sinogram with ndim={sino_np.ndim}.")
93
+
94
+ detector_count = sino_np.shape[axis]
95
+ freqs = np.fft.fftfreq(detector_count)
96
+ ramp = (2.0 * np.abs(freqs)).astype(np.float32)
97
+ shape = [1] * sino_np.ndim
98
+ shape[axis] = detector_count
99
+ sino_fft = np.fft.fft(sino_np, axis=axis)
100
+ filtered = np.real(np.fft.ifft(sino_fft * ramp.reshape(shape), axis=axis)).astype(np.float32)
101
+ return mx.array(filtered)