diffct-mlx 1.0.2__tar.gz → 1.0.3__tar.gz

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.
Files changed (32) hide show
  1. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/PKG-INFO +54 -1
  2. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/README.md +53 -0
  3. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/__about__.py +1 -1
  4. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/kernels/__init__.py +2 -0
  5. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/kernels/cone_beam.py +163 -0
  6. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/projectors.py +87 -5
  7. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/.gitignore +0 -0
  8. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/LICENSE +0 -0
  9. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/__init__.py +0 -0
  10. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/constants.py +0 -0
  11. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/geometry.py +0 -0
  12. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/kernels/fan_beam.py +0 -0
  13. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/kernels/parallel_beam.py +0 -0
  14. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/phantoms/__init__.py +0 -0
  15. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/phantoms/shepp_logan.py +0 -0
  16. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/real_measured_data_helper.py +0 -0
  17. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/reconstruction_algorithms/__init__.py +0 -0
  18. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/reconstruction_algorithms/_analytic.py +0 -0
  19. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/reconstruction_algorithms/_core.py +0 -0
  20. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/reconstruction_algorithms/asd_pocs.py +0 -0
  21. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/reconstruction_algorithms/awtv_pocs.py +0 -0
  22. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/reconstruction_algorithms/cases.py +0 -0
  23. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/reconstruction_algorithms/dart.py +0 -0
  24. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/reconstruction_algorithms/fbp.py +0 -0
  25. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/reconstruction_algorithms/fdk.py +0 -0
  26. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/reconstruction_algorithms/sart.py +0 -0
  27. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/reconstruction_algorithms/sirt.py +0 -0
  28. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/reconstruction_algorithms/tv_pocs.py +0 -0
  29. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/regularizers.py +0 -0
  30. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/tv_gradients.py +0 -0
  31. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/diffct_mlx/utils.py +0 -0
  32. {diffct_mlx-1.0.2 → diffct_mlx-1.0.3}/pyproject.toml +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: diffct_mlx
3
- Version: 1.0.2
3
+ Version: 1.0.3
4
4
  Summary: Differentiable CT reconstruction for Apple Silicon using MLX and custom Metal kernels
5
5
  Project-URL: Homepage, https://github.com/Linda-SophieSchneider/diffct_arbit
6
6
  Project-URL: Repository, https://github.com/Linda-SophieSchneider/diffct_arbit
@@ -43,6 +43,7 @@ This is the Apple Silicon port of [diffct](https://github.com/sypsyp97/diffct),
43
43
  - **Differentiable:** End-to-end gradient propagation using `mx.custom_function` with custom VJPs
44
44
  - **Siddon Ray-Tracing:** Bilinear (2D) and trilinear (3D) interpolation for accurate projection
45
45
  - **Atomic Backprojection:** Thread-safe gradient accumulation using Metal atomic operations
46
+ - **Sparse Cone Backprojection:** Sample-only cone-beam footprint backprojection via `indices=...`
46
47
 
47
48
  ## Supported Geometries
48
49
 
@@ -191,6 +192,56 @@ reco = diffct_mlx.cone_backward(
191
192
  )
192
193
  ```
193
194
 
195
+ ### Sparse Cone-Backprojection Sampling
196
+
197
+ `cone_backward_footprint` supports sample-only evaluation for cone-beam filtered
198
+ backprojection. This is useful when you need values at a subset of voxels
199
+ instead of the full volume, for example for sparse slice probing or cache
200
+ construction workflows.
201
+
202
+ ```python
203
+ import numpy as np
204
+ import mlx.core as mx
205
+ import diffct_mlx
206
+ from diffct_mlx.reconstruction_algorithms._analytic import ramp_filter_3d
207
+
208
+ volume = mx.ones((32, 32, 32), dtype=mx.float32)
209
+ src, det_c, det_u, det_v = diffct_mlx.circular_trajectory_3d(
210
+ n_views=60, sid=120.0, sdd=200.0
211
+ )
212
+
213
+ sino = diffct_mlx.cone_forward_footprint(
214
+ volume, src, det_c, det_u, det_v,
215
+ det_u=48, det_v=48, du=1.0, dv=1.0, voxel_spacing=1.0,
216
+ )
217
+ sino_filt = ramp_filter_3d(sino)
218
+
219
+ mid = volume.shape[0] // 2
220
+ yy, xx = np.meshgrid(
221
+ np.arange(volume.shape[1]),
222
+ np.arange(volume.shape[2]),
223
+ indexing="ij",
224
+ )
225
+ indices = (
226
+ mid * volume.shape[1] * volume.shape[2]
227
+ + yy.reshape(-1) * volume.shape[2]
228
+ + xx.reshape(-1)
229
+ )
230
+
231
+ slice_samples = diffct_mlx.cone_backward_footprint(
232
+ sino_filt, src, det_c, det_u, det_v,
233
+ D=32, H=32, W=32, du=1.0, dv=1.0, voxel_spacing=1.0,
234
+ indices=indices,
235
+ )
236
+
237
+ # Returns a 1D vector in the same order as `indices`.
238
+ centre_slice = slice_samples.reshape(32, 32)
239
+ ```
240
+
241
+ Sparse mode preserves the dense footprint backprojector's values exactly at the
242
+ requested locations. Indices must be unique flat indices into the row-major
243
+ `(D, H, W)` volume layout.
244
+
194
245
  ## API Reference
195
246
 
196
247
  ### Projectors
@@ -203,6 +254,8 @@ reco = diffct_mlx.cone_backward(
203
254
  | `fan_backward(sinogram, src_pos, det_center, det_u_vec, ...)` | 2D fan beam backprojection |
204
255
  | `cone_forward(volume, src_pos, det_center, det_u_vec, det_v_vec, ...)` | 3D cone beam forward projection |
205
256
  | `cone_backward(sinogram, src_pos, det_center, det_u_vec, det_v_vec, ...)` | 3D cone beam backprojection |
257
+ | `cone_forward_footprint(volume, src_pos, det_center, det_u_vec, det_v_vec, ...)` | 3D matched-footprint cone forward projection |
258
+ | `cone_backward_footprint(sinogram, src_pos, det_center, det_u_vec, det_v_vec, ..., indices=None)` | 3D matched-footprint cone backprojection, optionally sparse |
206
259
 
207
260
  ### Iterative Reconstruction Algorithms
208
261
 
@@ -13,6 +13,7 @@ This is the Apple Silicon port of [diffct](https://github.com/sypsyp97/diffct),
13
13
  - **Differentiable:** End-to-end gradient propagation using `mx.custom_function` with custom VJPs
14
14
  - **Siddon Ray-Tracing:** Bilinear (2D) and trilinear (3D) interpolation for accurate projection
15
15
  - **Atomic Backprojection:** Thread-safe gradient accumulation using Metal atomic operations
16
+ - **Sparse Cone Backprojection:** Sample-only cone-beam footprint backprojection via `indices=...`
16
17
 
17
18
  ## Supported Geometries
18
19
 
@@ -161,6 +162,56 @@ reco = diffct_mlx.cone_backward(
161
162
  )
162
163
  ```
163
164
 
165
+ ### Sparse Cone-Backprojection Sampling
166
+
167
+ `cone_backward_footprint` supports sample-only evaluation for cone-beam filtered
168
+ backprojection. This is useful when you need values at a subset of voxels
169
+ instead of the full volume, for example for sparse slice probing or cache
170
+ construction workflows.
171
+
172
+ ```python
173
+ import numpy as np
174
+ import mlx.core as mx
175
+ import diffct_mlx
176
+ from diffct_mlx.reconstruction_algorithms._analytic import ramp_filter_3d
177
+
178
+ volume = mx.ones((32, 32, 32), dtype=mx.float32)
179
+ src, det_c, det_u, det_v = diffct_mlx.circular_trajectory_3d(
180
+ n_views=60, sid=120.0, sdd=200.0
181
+ )
182
+
183
+ sino = diffct_mlx.cone_forward_footprint(
184
+ volume, src, det_c, det_u, det_v,
185
+ det_u=48, det_v=48, du=1.0, dv=1.0, voxel_spacing=1.0,
186
+ )
187
+ sino_filt = ramp_filter_3d(sino)
188
+
189
+ mid = volume.shape[0] // 2
190
+ yy, xx = np.meshgrid(
191
+ np.arange(volume.shape[1]),
192
+ np.arange(volume.shape[2]),
193
+ indexing="ij",
194
+ )
195
+ indices = (
196
+ mid * volume.shape[1] * volume.shape[2]
197
+ + yy.reshape(-1) * volume.shape[2]
198
+ + xx.reshape(-1)
199
+ )
200
+
201
+ slice_samples = diffct_mlx.cone_backward_footprint(
202
+ sino_filt, src, det_c, det_u, det_v,
203
+ D=32, H=32, W=32, du=1.0, dv=1.0, voxel_spacing=1.0,
204
+ indices=indices,
205
+ )
206
+
207
+ # Returns a 1D vector in the same order as `indices`.
208
+ centre_slice = slice_samples.reshape(32, 32)
209
+ ```
210
+
211
+ Sparse mode preserves the dense footprint backprojector's values exactly at the
212
+ requested locations. Indices must be unique flat indices into the row-major
213
+ `(D, H, W)` volume layout.
214
+
164
215
  ## API Reference
165
216
 
166
217
  ### Projectors
@@ -173,6 +224,8 @@ reco = diffct_mlx.cone_backward(
173
224
  | `fan_backward(sinogram, src_pos, det_center, det_u_vec, ...)` | 2D fan beam backprojection |
174
225
  | `cone_forward(volume, src_pos, det_center, det_u_vec, det_v_vec, ...)` | 3D cone beam forward projection |
175
226
  | `cone_backward(sinogram, src_pos, det_center, det_u_vec, det_v_vec, ...)` | 3D cone beam backprojection |
227
+ | `cone_forward_footprint(volume, src_pos, det_center, det_u_vec, det_v_vec, ...)` | 3D matched-footprint cone forward projection |
228
+ | `cone_backward_footprint(sinogram, src_pos, det_center, det_u_vec, det_v_vec, ..., indices=None)` | 3D matched-footprint cone backprojection, optionally sparse |
176
229
 
177
230
  ### Iterative Reconstruction Algorithms
178
231
 
@@ -1,3 +1,3 @@
1
1
  """Package metadata for diffct_mlx."""
2
2
 
3
- __version__ = "1.0.2"
3
+ __version__ = "1.0.3"
@@ -21,6 +21,7 @@ from .cone_beam import (
21
21
  cone_3d_backward_kernel,
22
22
  cone_3d_footprint_forward_kernel,
23
23
  cone_3d_footprint_backward_kernel,
24
+ cone_3d_footprint_backward_sparse_kernel,
24
25
  cone_3d_geometry_grad_kernel,
25
26
  )
26
27
 
@@ -37,5 +38,6 @@ __all__ = [
37
38
  'cone_3d_backward_kernel',
38
39
  'cone_3d_footprint_forward_kernel',
39
40
  'cone_3d_footprint_backward_kernel',
41
+ 'cone_3d_footprint_backward_sparse_kernel',
40
42
  'cone_3d_geometry_grad_kernel',
41
43
  ]
@@ -707,6 +707,169 @@ cone_3d_footprint_backward_kernel = mx.fast.metal_kernel(
707
707
  source=_CONE_3D_FOOTPRINT_BACKWARD_SOURCE,
708
708
  )
709
709
 
710
+ _CONE_3D_FOOTPRINT_BACKWARD_SPARSE_SOURCE = """
711
+ uint isample = thread_position_in_grid.x;
712
+
713
+ int n_samples = sparse_params[0];
714
+ int n_views = params[0];
715
+ int n_u = params[1];
716
+ int n_v = params[2];
717
+ int Nx = params[3];
718
+ int Ny = params[4];
719
+ int Nz = params[5];
720
+
721
+ float du = fparams[0];
722
+ float dv = fparams[1];
723
+ float cx = fparams[2];
724
+ float cy = fparams[3];
725
+ float cz = fparams[4];
726
+ float voxel_spacing = fparams[5];
727
+
728
+ if ((int)isample >= n_samples) return;
729
+
730
+ int flat_idx = indices[(int)isample];
731
+ int ix = flat_idx % Nx;
732
+ int iy = (flat_idx / Nx) % Ny;
733
+ int iz = flat_idx / (Nx * Ny);
734
+
735
+ float eps = """ + _EPSILON_STR + """;
736
+ float center_u = ((float)n_u - 1.0f) * 0.5f;
737
+ float center_v = ((float)n_v - 1.0f) * 0.5f;
738
+
739
+ float px = (((float)ix + 0.5f) - cx) * voxel_spacing;
740
+ float py = (((float)iy + 0.5f) - cy) * voxel_spacing;
741
+ float pz = (((float)iz + 0.5f) - cz) * voxel_spacing;
742
+
743
+ float accum = 0.0f;
744
+
745
+ for (int iview = 0; iview < n_views; ++iview) {
746
+ float src_x = src_pos[iview * 3 + 0];
747
+ float src_y = src_pos[iview * 3 + 1];
748
+ float src_z = src_pos[iview * 3 + 2];
749
+
750
+ float det_cx = det_center_arr[iview * 3 + 0];
751
+ float det_cy = det_center_arr[iview * 3 + 1];
752
+ float det_cz = det_center_arr[iview * 3 + 2];
753
+
754
+ float u_vec_x = det_u_vec_arr[iview * 3 + 0];
755
+ float u_vec_y = det_u_vec_arr[iview * 3 + 1];
756
+ float u_vec_z = det_u_vec_arr[iview * 3 + 2];
757
+
758
+ float v_vec_x = det_v_vec_arr[iview * 3 + 0];
759
+ float v_vec_y = det_v_vec_arr[iview * 3 + 1];
760
+ float v_vec_z = det_v_vec_arr[iview * 3 + 2];
761
+
762
+ float n_x = u_vec_y * v_vec_z - u_vec_z * v_vec_y;
763
+ float n_y = u_vec_z * v_vec_x - u_vec_x * v_vec_z;
764
+ float n_z = u_vec_x * v_vec_y - u_vec_y * v_vec_x;
765
+
766
+ float plane_dx = det_cx - src_x;
767
+ float plane_dy = det_cy - src_y;
768
+ float plane_dz = det_cz - src_z;
769
+ float plane_dist = plane_dx * n_x + plane_dy * n_y + plane_dz * n_z;
770
+ if (metal::abs(plane_dist) <= eps) continue;
771
+
772
+ float rx = px - src_x;
773
+ float ry = py - src_y;
774
+ float rz = pz - src_z;
775
+ float denom = rx * n_x + ry * n_y + rz * n_z;
776
+ if (metal::abs(denom) <= eps) continue;
777
+
778
+ float t = plane_dist / denom;
779
+ if (t <= 0.0f) continue;
780
+
781
+ float qx = src_x + t * rx;
782
+ float qy = src_y + t * ry;
783
+ float qz = src_z + t * rz;
784
+
785
+ float rel_x = qx - det_cx;
786
+ float rel_y = qy - det_cy;
787
+ float rel_z = qz - det_cz;
788
+ float u0 = rel_x * u_vec_x + rel_y * u_vec_y + rel_z * u_vec_z;
789
+ float v0 = rel_x * v_vec_x + rel_y * v_vec_y + rel_z * v_vec_z;
790
+
791
+ float ru = rx * u_vec_x + ry * u_vec_y + rz * u_vec_z;
792
+ float rv = rx * v_vec_x + ry * v_vec_y + rz * v_vec_z;
793
+ float inv_denom = 1.0f / denom;
794
+
795
+ float grad_ux = t * u_vec_x - t * ru * inv_denom * n_x;
796
+ float grad_uy = t * u_vec_y - t * ru * inv_denom * n_y;
797
+ float grad_uz = t * u_vec_z - t * ru * inv_denom * n_z;
798
+
799
+ float grad_vx = t * v_vec_x - t * rv * inv_denom * n_x;
800
+ float grad_vy = t * v_vec_y - t * rv * inv_denom * n_y;
801
+ float grad_vz = t * v_vec_z - t * rv * inv_denom * n_z;
802
+
803
+ float half_u = 0.5f * voxel_spacing * (
804
+ metal::abs(grad_ux) + metal::abs(grad_uy) + metal::abs(grad_uz)
805
+ );
806
+ float half_v = 0.5f * voxel_spacing * (
807
+ metal::abs(grad_vx) + metal::abs(grad_vy) + metal::abs(grad_vz)
808
+ );
809
+ half_u = metal::max(half_u, 0.5f * du);
810
+ half_v = metal::max(half_v, 0.5f * dv);
811
+
812
+ float width_u = metal::max(2.0f * half_u, du);
813
+ float width_v = metal::max(2.0f * half_v, dv);
814
+
815
+ float support_u = half_u + 0.5f * du;
816
+ float support_v = half_v + 0.5f * dv;
817
+
818
+ int iu_min = (int)metal::floor((u0 - support_u) / du + center_u);
819
+ int iu_max = (int)metal::ceil((u0 + support_u) / du + center_u);
820
+ int iv_min = (int)metal::floor((v0 - support_v) / dv + center_v);
821
+ int iv_max = (int)metal::ceil((v0 + support_v) / dv + center_v);
822
+
823
+ iu_min = metal::max(0, iu_min);
824
+ iv_min = metal::max(0, iv_min);
825
+ iu_max = metal::min(n_u - 1, iu_max);
826
+ iv_max = metal::min(n_v - 1, iv_max);
827
+
828
+ float fp_u_lo = u0 - half_u;
829
+ float fp_u_hi = u0 + half_u;
830
+ float fp_v_lo = v0 - half_v;
831
+ float fp_v_hi = v0 + half_v;
832
+
833
+ for (int iu = iu_min; iu <= iu_max; ++iu) {
834
+ float pixel_u = ((float)iu - center_u) * du;
835
+ float pixel_u_lo = pixel_u - 0.5f * du;
836
+ float pixel_u_hi = pixel_u + 0.5f * du;
837
+ float overlap_u = metal::min(pixel_u_hi, fp_u_hi) - metal::max(pixel_u_lo, fp_u_lo);
838
+ if (overlap_u <= eps) continue;
839
+ float wu = overlap_u / width_u;
840
+
841
+ for (int iv = iv_min; iv <= iv_max; ++iv) {
842
+ float pixel_v = ((float)iv - center_v) * dv;
843
+ float pixel_v_lo = pixel_v - 0.5f * dv;
844
+ float pixel_v_hi = pixel_v + 0.5f * dv;
845
+ float overlap_v = metal::min(pixel_v_hi, fp_v_hi) - metal::max(pixel_v_lo, fp_v_lo);
846
+ if (overlap_v <= eps) continue;
847
+ float wv = overlap_v / width_v;
848
+ accum += sino[iview * n_u * n_v + iu * n_v + iv] * wu * wv * voxel_spacing;
849
+ }
850
+ }
851
+ }
852
+
853
+ grad_sparse[(int)isample] = accum;
854
+ """
855
+
856
+ cone_3d_footprint_backward_sparse_kernel = mx.fast.metal_kernel(
857
+ name="cone_3d_footprint_backward_sparse",
858
+ input_names=[
859
+ "sino",
860
+ "src_pos",
861
+ "det_center_arr",
862
+ "det_u_vec_arr",
863
+ "det_v_vec_arr",
864
+ "indices",
865
+ "sparse_params",
866
+ "params",
867
+ "fparams",
868
+ ],
869
+ output_names=["grad_sparse"],
870
+ source=_CONE_3D_FOOTPRINT_BACKWARD_SPARSE_SOURCE,
871
+ )
872
+
710
873
  # ============================================================================
711
874
  # 3D Cone Beam Analytical Geometry Gradient Kernel
712
875
  #
@@ -26,6 +26,7 @@ from .kernels import (
26
26
  cone_3d_backward_kernel,
27
27
  cone_3d_footprint_forward_kernel,
28
28
  cone_3d_footprint_backward_kernel,
29
+ cone_3d_footprint_backward_sparse_kernel,
29
30
  cone_3d_geometry_grad_kernel,
30
31
  )
31
32
 
@@ -39,6 +40,38 @@ def _as_mx_float_array(value):
39
40
  return mx.array(value, dtype=_MX_DTYPE)
40
41
 
41
42
 
43
+ def _normalize_sparse_indices(indices, D, H, W):
44
+ """Validate sparse backprojection indices and return an ``int32`` vector."""
45
+ if indices is None:
46
+ return None
47
+
48
+ indices = np.asarray(indices)
49
+ if indices.ndim != 1:
50
+ raise ValueError("indices must be a 1D array of flat voxel indices.")
51
+ if not np.issubdtype(indices.dtype, np.integer):
52
+ raise TypeError("indices must contain integer values.")
53
+
54
+ total_voxels = int(D) * int(H) * int(W)
55
+ if total_voxels > np.iinfo(np.int32).max:
56
+ raise ValueError("Sparse backprojection currently supports at most int32-addressable volumes.")
57
+
58
+ if np.any(indices < 0):
59
+ raise ValueError("indices contains negative values.")
60
+ if np.any(indices >= total_voxels):
61
+ raise ValueError("indices contains out of bounds values.")
62
+ if np.unique(indices).size != indices.size:
63
+ raise ValueError("indices contains duplicate values.")
64
+
65
+ return indices.astype(np.int32, copy=False)
66
+
67
+
68
+ def _scatter_sparse_cotangent(cotangent, indices, D, H, W):
69
+ """Expand a sparse cotangent vector into the dense ``(D, H, W)`` volume layout."""
70
+ dense = np.zeros(int(D) * int(H) * int(W), dtype=np.float32)
71
+ dense[indices] = np.asarray(cotangent, dtype=np.float32).reshape(-1)
72
+ return mx.array(dense.reshape(int(D), int(H), int(W)), dtype=_MX_DTYPE)
73
+
74
+
42
75
  @lru_cache(maxsize=32)
43
76
  def _projector_params_2d(
44
77
  n_ang: int,
@@ -962,7 +995,7 @@ def _cone_forward_footprint_impl(volume, src_pos, det_center, det_u_vec, det_v_v
962
995
 
963
996
 
964
997
  def _cone_backward_footprint_impl(sinogram, src_pos, det_center, det_u_vec, det_v_vec,
965
- D, H, W, du, dv, voxel_spacing=1.0):
998
+ D, H, W, du, dv, voxel_spacing=1.0, indices=None):
966
999
  """Adjoint of the approximate separable-footprint cone-beam forward projector."""
967
1000
  sinogram = _as_mx_float_array(sinogram)
968
1001
  src_pos = _as_mx_float_array(src_pos)
@@ -972,6 +1005,7 @@ def _cone_backward_footprint_impl(sinogram, src_pos, det_center, det_u_vec, det_
972
1005
 
973
1006
  n_views, n_u, n_v = sinogram.shape
974
1007
  Nx, Ny, Nz = int(W), int(H), int(D)
1008
+ sparse_indices = _normalize_sparse_indices(indices, D, H, W)
975
1009
 
976
1010
  cx = float(Nx * 0.5)
977
1011
  cy = float(Ny * 0.5)
@@ -992,6 +1026,33 @@ def _cone_backward_footprint_impl(sinogram, src_pos, det_center, det_u_vec, det_
992
1026
  float(voxel_spacing),
993
1027
  )
994
1028
 
1029
+ if sparse_indices is not None:
1030
+ if sparse_indices.size == 0:
1031
+ return mx.zeros((0,), dtype=sinogram.dtype)
1032
+
1033
+ sparse_indices_mx = mx.array(sparse_indices, dtype=mx.int32)
1034
+ sparse_params = mx.array([int(sparse_indices.size)], dtype=mx.int32)
1035
+ grid, tg = _grid_3d(int(sparse_indices.size), 1, 1)
1036
+
1037
+ outputs = cone_3d_footprint_backward_sparse_kernel(
1038
+ inputs=[
1039
+ sinogram,
1040
+ src_pos,
1041
+ det_center,
1042
+ det_u_vec,
1043
+ det_v_vec,
1044
+ sparse_indices_mx,
1045
+ sparse_params,
1046
+ params,
1047
+ fparams,
1048
+ ],
1049
+ output_shapes=[(int(sparse_indices.size),)],
1050
+ output_dtypes=[_MX_DTYPE],
1051
+ grid=grid,
1052
+ threadgroup=tg,
1053
+ )
1054
+ return outputs[0]
1055
+
995
1056
  grid, tg = _grid_3d(Nx, Ny, Nz)
996
1057
 
997
1058
  outputs = cone_3d_footprint_backward_kernel(
@@ -1182,8 +1243,14 @@ def cone_forward_footprint_vjp(primals, cotangent, _):
1182
1243
 
1183
1244
  @mx.custom_function
1184
1245
  def cone_backward_footprint(sinogram, src_pos, det_center, det_u_vec, det_v_vec,
1185
- D=128, H=128, W=128, du=1.0, dv=1.0, voxel_spacing=1.0):
1186
- """Adjoint of the matched voxel-footprint cone-beam forward projector."""
1246
+ D=128, H=128, W=128, du=1.0, dv=1.0, voxel_spacing=1.0,
1247
+ indices=None):
1248
+ """Adjoint of the matched voxel-footprint cone-beam forward projector.
1249
+
1250
+ When ``indices`` is provided, the operator evaluates only those flattened
1251
+ ``(D, H, W)`` voxel locations and returns a 1D vector in the caller's
1252
+ requested order instead of the full dense volume.
1253
+ """
1187
1254
  return _cone_backward_footprint_impl(
1188
1255
  sinogram,
1189
1256
  src_pos,
@@ -1196,20 +1263,35 @@ def cone_backward_footprint(sinogram, src_pos, det_center, det_u_vec, det_v_vec,
1196
1263
  du,
1197
1264
  dv,
1198
1265
  voxel_spacing,
1266
+ indices,
1199
1267
  )
1200
1268
 
1201
1269
 
1202
1270
  @cone_backward_footprint.vjp
1203
1271
  def cone_backward_footprint_vjp(primals, cotangent, _):
1204
1272
  sinogram, src_pos, det_center, det_u_vec, det_v_vec = primals[:5]
1273
+ D = int(primals[5]) if len(primals) > 5 else 128
1274
+ H = int(primals[6]) if len(primals) > 6 else 128
1275
+ W = int(primals[7]) if len(primals) > 7 else 128
1205
1276
  det_u = int(sinogram.shape[1])
1206
1277
  det_v = int(sinogram.shape[2])
1207
1278
  du = primals[8] if len(primals) > 8 else 1.0
1208
1279
  dv = primals[9] if len(primals) > 9 else 1.0
1209
1280
  voxel_spacing = primals[10] if len(primals) > 10 else 1.0
1281
+ indices = primals[11] if len(primals) > 11 else None
1282
+
1283
+ dense_cotangent = cotangent
1284
+ if indices is not None:
1285
+ dense_cotangent = _scatter_sparse_cotangent(
1286
+ cotangent,
1287
+ _normalize_sparse_indices(indices, D, H, W),
1288
+ D,
1289
+ H,
1290
+ W,
1291
+ )
1210
1292
 
1211
1293
  grad_sinogram = _cone_forward_footprint_impl(
1212
- cotangent,
1294
+ dense_cotangent,
1213
1295
  src_pos,
1214
1296
  det_center,
1215
1297
  det_u_vec,
@@ -1223,4 +1305,4 @@ def cone_backward_footprint_vjp(primals, cotangent, _):
1223
1305
  return (grad_sinogram,
1224
1306
  mx.zeros_like(src_pos), mx.zeros_like(det_center),
1225
1307
  mx.zeros_like(det_u_vec), mx.zeros_like(det_v_vec),
1226
- None, None, None, None, None, None)
1308
+ None, None, None, None, None, None, None)
File without changes
File without changes
File without changes