diffct-mlx 2.1.0__tar.gz → 2.3.0__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 (128) hide show
  1. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/CHANGELOG.md +105 -1
  2. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/PKG-INFO +18 -3
  3. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/README.md +15 -1
  4. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/kernels/cone_footprint.py +20 -3
  5. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/kernels/fan_footprint.py +10 -8
  6. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/kernels/parallel_footprint.py +4 -4
  7. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/__init__.py +6 -1
  8. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/backend/metal/kernels/cone_beam.py +17 -3
  9. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/backend/metal/kernels/fan_beam.py +8 -8
  10. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/backend/metal/kernels/parallel_beam.py +4 -4
  11. diffct_mlx-2.3.0/diffct_mlx/conventions.py +83 -0
  12. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/operators.py +80 -22
  13. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/physics/simulate.py +7 -2
  14. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/cases.py +72 -12
  15. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/docs/source/conf.py +1 -1
  16. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/pyproject.toml +3 -2
  17. diffct_mlx-2.3.0/tests/test_conventions.py +190 -0
  18. diffct_mlx-2.3.0/tests/test_footprint_amplitude.py +173 -0
  19. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_quantitative_fdk.py +53 -0
  20. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/.github/workflows/docs.yml +0 -0
  21. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/.github/workflows/publish.yml +0 -0
  22. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/.github/workflows/release.yml +0 -0
  23. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/.gitignore +0 -0
  24. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/ATTRIBUTION.md +0 -0
  25. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/LICENSE +0 -0
  26. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/__init__.py +0 -0
  27. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/analytical.py +0 -0
  28. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/constants.py +0 -0
  29. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/differentiable.py +0 -0
  30. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/footprint.py +0 -0
  31. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/geometry.py +0 -0
  32. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/kernels/__init__.py +0 -0
  33. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/kernels/cone_beam.py +0 -0
  34. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/kernels/fan_beam.py +0 -0
  35. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/kernels/parallel_beam.py +0 -0
  36. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/projectors.py +0 -0
  37. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct/utils.py +0 -0
  38. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/analytical.py +0 -0
  39. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/backend/__init__.py +0 -0
  40. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/backend/_mlx.py +0 -0
  41. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/backend/_torch.py +0 -0
  42. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/backend/metal/__init__.py +0 -0
  43. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/backend/metal/constants.py +0 -0
  44. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/backend/metal/geometry.py +0 -0
  45. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/backend/metal/kernels/__init__.py +0 -0
  46. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/backend/metal/projectors.py +0 -0
  47. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/backend/metal/utils.py +0 -0
  48. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/calibration.py +0 -0
  49. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/filters.py +0 -0
  50. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/functionals.py +0 -0
  51. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/geometry.py +0 -0
  52. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/orchestration/__init__.py +0 -0
  53. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/orchestration/out_of_core.py +0 -0
  54. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/phantoms/__init__.py +0 -0
  55. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/phantoms/engine.py +0 -0
  56. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/phantoms/shepp_logan.py +0 -0
  57. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/physics/__init__.py +0 -0
  58. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/physics/corrections.py +0 -0
  59. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/physics/data/spectra.npz +0 -0
  60. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/physics/pipeline.py +0 -0
  61. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/physics/spectra.py +0 -0
  62. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/projectors.py +0 -0
  63. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/real_measured_data_helper.py +0 -0
  64. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/rebinning.py +0 -0
  65. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/__init__.py +0 -0
  66. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/_analytic.py +0 -0
  67. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/_core.py +0 -0
  68. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/_solver.py +0 -0
  69. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/asd_pocs.py +0 -0
  70. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/awtv_pocs.py +0 -0
  71. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/dart.py +0 -0
  72. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/fbp.py +0 -0
  73. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/fdk.py +0 -0
  74. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/sart.py +0 -0
  75. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/sirt.py +0 -0
  76. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/solvers.py +0 -0
  77. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/reconstruction_algorithms/tv_pocs.py +0 -0
  78. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/regularizers.py +0 -0
  79. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/diffct_mlx/tv_gradients.py +0 -0
  80. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/docs/Makefile +0 -0
  81. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/docs/source/_static/.gitkeep +0 -0
  82. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/docs/source/api.rst +0 -0
  83. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/docs/source/examples.rst +0 -0
  84. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/docs/source/fbp_fan_example.rst +0 -0
  85. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/docs/source/fbp_parallel_example.rst +0 -0
  86. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/docs/source/fdk_cone_example.rst +0 -0
  87. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/docs/source/getting_started.rst +0 -0
  88. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/docs/source/index.rst +0 -0
  89. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/docs/source/iterative_reco_cone_example.rst +0 -0
  90. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/docs/source/iterative_reco_fan_example.rst +0 -0
  91. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/docs/source/iterative_reco_parallel_example.rst +0 -0
  92. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/examples/circular_trajectory/fbp_fan.py +0 -0
  93. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/examples/circular_trajectory/fbp_parallel.py +0 -0
  94. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/examples/circular_trajectory/fdk_cone.py +0 -0
  95. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/examples/circular_trajectory/iterative_reco_cone.py +0 -0
  96. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/examples/circular_trajectory/iterative_reco_fan.py +0 -0
  97. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/examples/circular_trajectory/iterative_reco_parallel.py +0 -0
  98. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/examples/non_circular_trajectory/iterative_reco_cone.py +0 -0
  99. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/examples/non_circular_trajectory/iterative_reco_fan.py +0 -0
  100. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/examples/non_circular_trajectory/iterative_reco_parallel.py +0 -0
  101. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/examples/plot_trajectory.py +0 -0
  102. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/pytest.ini +0 -0
  103. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/requirements.txt +0 -0
  104. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/__init__.py +0 -0
  105. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/_phantoms.py +0 -0
  106. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/benchmarks/README.md +0 -0
  107. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/benchmarks/__init__.py +0 -0
  108. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/benchmarks/_common.py +0 -0
  109. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/benchmarks/test_bench_cone.py +0 -0
  110. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/benchmarks/test_bench_fan.py +0 -0
  111. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/benchmarks/test_bench_parallel.py +0 -0
  112. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_adjoint_inner_product.py +0 -0
  113. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_cone_projector_autograd.py +0 -0
  114. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_cuda_smoke.py +0 -0
  115. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_diffct_mlx_extensions.py +0 -0
  116. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_fbp_fan_accuracy.py +0 -0
  117. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_fbp_fan_offsets.py +0 -0
  118. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_fbp_parallel_accuracy.py +0 -0
  119. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_fdk_cone_accuracy.py +0 -0
  120. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_fdk_cone_offsets.py +0 -0
  121. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_gradcheck.py +0 -0
  122. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_known_operator_gradflow.py +0 -0
  123. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_metal_kernel_static.py +0 -0
  124. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_ramp_filter_windows.py +0 -0
  125. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_regressions.py +0 -0
  126. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_siddon_centering.py +0 -0
  127. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tests/test_weights.py +0 -0
  128. {diffct_mlx-2.1.0 → diffct_mlx-2.3.0}/tools/generate_spectra.py +0 -0
@@ -9,6 +9,108 @@ and the project follows [Semantic Versioning](https://semver.org/).
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [2.3.0] - 2026-11-25
13
+
14
+ ### Added
15
+
16
+ - `detector_convention=` and `line_integrals=` on `make_*_operator` and
17
+ `make_*_operators`. `detector_convention="centered"` reads `det_center` as
18
+ the geometric panel centre (pixel `i` at `(i − (N−1)/2)·du`) instead of
19
+ the kernel-native `(i − N/2)·du`. `line_integrals="physical"` returns
20
+ `A @ x` in µ·mm, and `A.T` scales alike. Together they reproduce the
21
+ conventions of DiffCT-MLX 1.x and upstream diffct ≥ 2.0. Defaults are
22
+ unchanged. New module `diffct_mlx.conventions` with
23
+ `kernel_detector_center` for direct projector and FBP/FDK calls.
24
+ - `ProjectionOperator.line_integrals` and `.to_physical`; `simulate_scan`
25
+ uses them, so physical-unit operators are not rescaled twice.
26
+
27
+ ### Fixed
28
+
29
+ - The `[cuda]` extra pins `numpy<2.5`. numba-cuda calls `np.row_stack`,
30
+ which numpy 2.5 removed, so a fresh install failed at import.
31
+
32
+ ### Tests
33
+
34
+ - `test_cone_footprint_amplitude_holds_along_the_ray` pins the cone footprint
35
+ amplitude for an object 25 mm off the isocentre along the ray, in the
36
+ SID 500 / SDD 900 geometry. DiffCT-MLX 1.x fails it by about 10 %: it
37
+ weighted each voxel with the cell size at the voxel's own depth instead of
38
+ the true magnification. A centred object hides that error.
39
+
40
+ ### Documentation
41
+
42
+ - README states the detector-pixel convention and how it differs from
43
+ upstream diffct ≥ 2.0, which moved to centred pixels and physical units.
44
+
45
+ ## [2.2.0] - 2026-11-18
46
+
47
+ ### Fixed
48
+
49
+ - **Footprint projectors return line integrals.** The separable-footprint
50
+ kernels (parallel, fan, cone; forward, adjoint and the sparse cone adjoint;
51
+ Metal and CUDA) distributed every voxel with weights that sum to one over
52
+ the detector cells it touches (`overlap / footprint_width`). The output was
53
+ therefore not a line integral: the cone sinogram scaled with
54
+ `(detector cell size at the isocentre / voxel size)**2` (0.21x for a 0.28 mm
55
+ cell on 0.6 mm voxels), and the parallel and fan sinograms carried an
56
+ additional view-angle factor `1 / max(|cos|, |sin|)` of up to 1.41x at 45
57
+ degrees plus the factor `detector cell / voxel`. The kernels now use a
58
+ mass-conserving amplitude (parallel: `1 / det_spacing_vox`; fan:
59
+ `t * |r| / (|r.n| * det_spacing_vox)`; cone:
60
+ `vs^2 * t^2 * |r| / (|r.n| * du * dv)` with local magnification `t`), so a
61
+ footprint sinogram is a voxel-unit line integral averaged over the detector
62
+ cell and agrees with the Siddon projector to within 1 % for any detector
63
+ spacing, voxel spacing and magnification
64
+ (`tests/test_footprint_amplitude.py`). The forward/adjoint pairs remain
65
+ exact adjoints.
66
+
67
+ ### Changed
68
+
69
+ - **Numerical output of every `*_footprint` projector and of operators built
70
+ with `projector_mode="footprint"` (the default) changes** by the factors
71
+ above. Consequences:
72
+ - Sensitivity-normalised SART (`SARTParameters` default) is unaffected when
73
+ data and operator come from the same footprint pair. Reconstructing
74
+ line-integral data (Siddon simulation, measured data) with the footprint
75
+ pair now yields the correct amplitude; before, the built-in fan and cone
76
+ cases came out 1.4x and 2.4x too large.
77
+ - Physical noise or beam-hardening models applied to footprint sinograms
78
+ (`exp(-p)`) now see the correct attenuation.
79
+ - Unnormalised iterative updates (`voxel_sensitivity_normalization=False`)
80
+ need a `backprojection_scale`: the adjoint of ones is now
81
+ `(vs * t / du)^2` per view instead of one. The case default 0.215 remains
82
+ stable for the built-in cone case.
83
+ - Results that mixed footprint sinograms with absolute thresholds or fixed
84
+ step sizes tuned on 2.1.0 and earlier are not comparable across this
85
+ change.
86
+
87
+
88
+ ### Known issues
89
+
90
+ - The quantitative FDK path (`reconstruct_case_fdk`, `fdk_back_project`) is
91
+ **torch/CUDA-only**: it drives the vendored `cone_weighted_backproject`
92
+ gather kernel, which has no Metal counterpart yet. On MLX the cone cases
93
+ fall back to the legacy calibrated path (synthetic cases) or report
94
+ `supports_fdk=False` (measured/npy). Porting needs a Metal FDK gather
95
+ kernel with `(FOD/U)^2` weighting plus a `cone_weighted_backproject`
96
+ equivalent exposed through the backend's `analytical` module — same
97
+ structure as the CUDA one (`diffct/kernels/cone_beam.py::
98
+ _cone_3d_fdk_backproject_kernel`, wired via
99
+ `diffct_mlx/reconstruction_algorithms/cases.py::_quantitative_fdk_operators`).
100
+
101
+ ### Notes
102
+
103
+ - Acceptance tests hardened per the FDK review: object-size sweep
104
+ (25/50/75 % FOV, amplitude 0.90–1.08×) and detector-pitch invariance
105
+ (du ∈ {1.0, 0.556, 0.278} mm within 4 % of each other). A proposed
106
+ du-free DFT-bin ramp (`|fftfreq|` without factor 2 and without
107
+ `sample_spacing`) was evaluated against these and rejected: it scales
108
+ the reconstruction by ~`du/2` (0.484/0.269/0.134× across the sweep —
109
+ invisible only at `du = 1`). The physical filtering carries
110
+ `Q = IFFT(FFT(g)·|f_k|)/du`; the quantitative path's
111
+ `sample_spacing`/constants implement exactly that and are
112
+ pitch-invariant (0.966–0.968× across the sweep).
113
+
12
114
  ## [2.1.0] - 2026-07-16
13
115
 
14
116
  ### Added
@@ -407,7 +509,9 @@ necessary for the arbitrary-trajectory kernel API:
407
509
  when the detector is not plane-aligned to the voxel axes) and will
408
510
  be tackled in a dedicated follow-up.
409
511
 
410
- [Unreleased]: https://github.com/Linda-SophieSchneider/DiffCT-MLX/compare/v2.1.0...HEAD
512
+ [Unreleased]: https://github.com/Linda-SophieSchneider/DiffCT-MLX/compare/v2.3.0...HEAD
513
+ [2.3.0]: https://github.com/Linda-SophieSchneider/DiffCT-MLX/compare/v2.2.0...v2.3.0
514
+ [2.2.0]: https://github.com/Linda-SophieSchneider/DiffCT-MLX/compare/v2.1.0...v2.2.0
411
515
  [2.1.0]: https://github.com/Linda-SophieSchneider/DiffCT-MLX/compare/v2.0.1...v2.1.0
412
516
  [2.0.1]: https://github.com/Linda-SophieSchneider/DiffCT-MLX/compare/v2.0.0...v2.0.1
413
517
  [2.0.0]: https://github.com/Linda-SophieSchneider/DiffCT-MLX/releases/tag/v2.0.0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: diffct-mlx
3
- Version: 2.1.0
3
+ Version: 2.3.0
4
4
  Summary: Differentiable computed tomography with an auto-selecting backend (Torch/CUDA or Apple MLX)
5
5
  Project-URL: Homepage, https://github.com/Linda-SophieSchneider/DiffCT-MLX
6
6
  Project-URL: Upstream, https://github.com/sypsyp97/diffct
@@ -16,6 +16,7 @@ Requires-Python: >=3.9
16
16
  Requires-Dist: numpy
17
17
  Provides-Extra: cuda
18
18
  Requires-Dist: numba-cuda; extra == 'cuda'
19
+ Requires-Dist: numpy<2.5; extra == 'cuda'
19
20
  Requires-Dist: torch; extra == 'cuda'
20
21
  Provides-Extra: mlx
21
22
  Requires-Dist: mlx; extra == 'mlx'
@@ -145,7 +146,10 @@ filter itself now accepts `pad_factor` and `sample_spacing`, and
145
146
  `FBPParameters`/`FDKParameters` default to `pad_factor=2`.
146
147
 
147
148
  > **Unit convention.** The projectors integrate in **voxel units**: physical
148
- > line integrals are `(A @ x) * voxel_spacing`. Operators built via
149
+ > line integrals are `(A @ x) * voxel_spacing`. This holds for both projector
150
+ > modes: since 2.2.0 the footprint kernels are mass-conserving and agree with
151
+ > the Siddon kernels in amplitude (earlier versions scaled footprint output
152
+ > with the detector-cell-to-voxel size ratio). Operators built via
149
153
  > `make_*_operator` carry `A.voxel_spacing` as metadata, and the boundaries
150
154
  > where physical units matter handle the conversion automatically —
151
155
  > `simulate_scan` rescales before the `exp(-p)` / beam-hardening physics (and
@@ -153,6 +157,17 @@ filter itself now accepts `pad_factor` and `sample_spacing`, and
153
157
  > encodes it per data source. One consequence to know: **iterative**
154
158
  > reconstruction of measured (physical) data converges to attenuation *per
155
159
  > voxel*; divide by `case.voxel_spacing` for µ in 1/mm.
160
+ >
161
+ > **Detector convention.** The kernels place detector pixel `i` at
162
+ > `det_center + (i − N/2)·du` (the diffct `dev` line they come from), half a
163
+ > pixel off the geometric panel centre. Upstream diffct ≥ 2.0, DiffCT-MLX 1.x
164
+ > and scanner geometry files use `(i − (N−1)/2)·du` and physical line
165
+ > integrals. To work in that convention, build operators with
166
+ > `detector_convention="centered", line_integrals="physical"`
167
+ > (`make_*_operator` and `make_*_operators`); `A.T` scales alike and stays
168
+ > the exact adjoint, and reconstructions then come out in 1/mm. For direct
169
+ > projector or FBP/FDK calls, `dct.kernel_detector_center(...)` converts a
170
+ > panel-centre `det_center` (shift by `+du/2·u + dv/2·v`).
156
171
 
157
172
  ### Operators, solvers, regularizers, physics & simulation
158
173
 
@@ -120,7 +120,10 @@ filter itself now accepts `pad_factor` and `sample_spacing`, and
120
120
  `FBPParameters`/`FDKParameters` default to `pad_factor=2`.
121
121
 
122
122
  > **Unit convention.** The projectors integrate in **voxel units**: physical
123
- > line integrals are `(A @ x) * voxel_spacing`. Operators built via
123
+ > line integrals are `(A @ x) * voxel_spacing`. This holds for both projector
124
+ > modes: since 2.2.0 the footprint kernels are mass-conserving and agree with
125
+ > the Siddon kernels in amplitude (earlier versions scaled footprint output
126
+ > with the detector-cell-to-voxel size ratio). Operators built via
124
127
  > `make_*_operator` carry `A.voxel_spacing` as metadata, and the boundaries
125
128
  > where physical units matter handle the conversion automatically —
126
129
  > `simulate_scan` rescales before the `exp(-p)` / beam-hardening physics (and
@@ -128,6 +131,17 @@ filter itself now accepts `pad_factor` and `sample_spacing`, and
128
131
  > encodes it per data source. One consequence to know: **iterative**
129
132
  > reconstruction of measured (physical) data converges to attenuation *per
130
133
  > voxel*; divide by `case.voxel_spacing` for µ in 1/mm.
134
+ >
135
+ > **Detector convention.** The kernels place detector pixel `i` at
136
+ > `det_center + (i − N/2)·du` (the diffct `dev` line they come from), half a
137
+ > pixel off the geometric panel centre. Upstream diffct ≥ 2.0, DiffCT-MLX 1.x
138
+ > and scanner geometry files use `(i − (N−1)/2)·du` and physical line
139
+ > integrals. To work in that convention, build operators with
140
+ > `detector_convention="centered", line_integrals="physical"`
141
+ > (`make_*_operator` and `make_*_operators`); `A.T` scales alike and stays
142
+ > the exact adjoint, and reconstructions then come out in 1/mm. For direct
143
+ > projector or FBP/FDK calls, `dct.kernel_detector_center(...)` converts a
144
+ > panel-centre `det_center` (shift by `+du/2·u + dv/2·v`).
131
145
 
132
146
  ### Operators, solvers, regularizers, physics & simulation
133
147
 
@@ -118,7 +118,12 @@ def _cone_3d_footprint_forward_kernel(
118
118
  fp_u_hi = u0 + half_u
119
119
  fp_v_lo = v0 - half_v
120
120
  fp_v_hi = v0 + half_v
121
- scaled_val = val * voxel_spacing
121
+ # Mass-conserving footprint amplitude: the output is a voxel-unit line integral
122
+ # averaged over the detector cell, on the same scale as the Siddon projector.
123
+ # A voxel of volume vs^3 spreads over (vs * t)^2 * ray_len / |denom| of detector area.
124
+ ray_len = math.sqrt(rx * rx + ry * ry + rz * rz)
125
+ amp = (voxel_spacing * voxel_spacing / (du * dv)) * t * t * ray_len * abs(inv_denom)
126
+ scaled_val = val * amp
122
127
 
123
128
  for iu in range(iu_min, iu_max + 1):
124
129
  pixel_u = (np.float32(iu) - center_u) * du
@@ -235,6 +240,12 @@ def _cone_3d_footprint_backward_kernel(
235
240
  fp_v_lo = v0 - half_v
236
241
  fp_v_hi = v0 + half_v
237
242
 
243
+ # Mass-conserving footprint amplitude: the output is a voxel-unit line integral
244
+ # averaged over the detector cell, on the same scale as the Siddon projector.
245
+ # A voxel of volume vs^3 spreads over (vs * t)^2 * ray_len / |denom| of detector area.
246
+ ray_len = math.sqrt(rx * rx + ry * ry + rz * rz)
247
+ amp = (voxel_spacing * voxel_spacing / (du * dv)) * t * t * ray_len * abs(inv_denom)
248
+
238
249
  for iu in range(iu_min, iu_max + 1):
239
250
  pixel_u = (np.float32(iu) - center_u) * du
240
251
  overlap_u = min(pixel_u + _HALF * du, fp_u_hi) - max(pixel_u - _HALF * du, fp_u_lo)
@@ -247,7 +258,7 @@ def _cone_3d_footprint_backward_kernel(
247
258
  if overlap_v <= eps:
248
259
  continue
249
260
  wv = overlap_v / width_v
250
- accum += d_sino[iview, iu, iv] * wu * wv * voxel_spacing
261
+ accum += d_sino[iview, iu, iv] * wu * wv * amp
251
262
 
252
263
  d_vol[ix, iy, iz] = accum
253
264
 
@@ -359,6 +370,12 @@ def _cone_3d_footprint_backward_sparse_kernel(
359
370
  fp_v_lo = v0 - half_v
360
371
  fp_v_hi = v0 + half_v
361
372
 
373
+ # Mass-conserving footprint amplitude: the output is a voxel-unit line integral
374
+ # averaged over the detector cell, on the same scale as the Siddon projector.
375
+ # A voxel of volume vs^3 spreads over (vs * t)^2 * ray_len / |denom| of detector area.
376
+ ray_len = math.sqrt(rx * rx + ry * ry + rz * rz)
377
+ amp = (voxel_spacing * voxel_spacing / (du * dv)) * t * t * ray_len * abs(inv_denom)
378
+
362
379
  for iu in range(iu_min, iu_max + 1):
363
380
  pixel_u = (np.float32(iu) - center_u) * du
364
381
  overlap_u = min(pixel_u + _HALF * du, fp_u_hi) - max(pixel_u - _HALF * du, fp_u_lo)
@@ -371,6 +388,6 @@ def _cone_3d_footprint_backward_sparse_kernel(
371
388
  if overlap_v <= eps:
372
389
  continue
373
390
  wv = overlap_v / width_v
374
- accum += d_sino[iview, iu, iv] * wu * wv * voxel_spacing
391
+ accum += d_sino[iview, iu, iv] * wu * wv * amp
375
392
 
376
393
  d_out[isample] = accum
@@ -76,9 +76,10 @@ def _fan_2d_footprint_forward_kernel(
76
76
  ray_len = math.sqrt(rx * rx + ry * ry)
77
77
  if ray_len <= eps:
78
78
  continue
79
- dir_x = rx / ray_len
80
- dir_y = ry / ray_len
81
- l_phi = _ONE / max(max(abs(dir_x), abs(dir_y)), eps)
79
+ # Mass-conserving footprint amplitude: the output is a voxel-unit line integral
80
+ # averaged over the detector cell, on the same scale as the Siddon projector.
81
+ # (local magnification t, oblique detector incidence ray_len / |denom|).
82
+ amp = t * ray_len / (abs(denom) * det_spacing_vox)
82
83
 
83
84
  idet_min = int(math.floor(u0 / det_spacing_vox + center_u - support_u / det_spacing_vox))
84
85
  idet_max = int(math.ceil(u0 / det_spacing_vox + center_u + support_u / det_spacing_vox))
@@ -89,7 +90,7 @@ def _fan_2d_footprint_forward_kernel(
89
90
 
90
91
  fp_lo = u0 - half_u
91
92
  fp_hi = u0 + half_u
92
- scaled_val = val * l_phi
93
+ scaled_val = val * amp
93
94
 
94
95
  for idet in range(idet_min, idet_max + 1):
95
96
  pixel_u = (np.float32(idet) - center_u) * det_spacing_vox
@@ -160,9 +161,10 @@ def _fan_2d_footprint_backward_kernel(
160
161
  ray_len = math.sqrt(rx * rx + ry * ry)
161
162
  if ray_len <= eps:
162
163
  continue
163
- dir_x = rx / ray_len
164
- dir_y = ry / ray_len
165
- l_phi = _ONE / max(max(abs(dir_x), abs(dir_y)), eps)
164
+ # Mass-conserving footprint amplitude: the output is a voxel-unit line integral
165
+ # averaged over the detector cell, on the same scale as the Siddon projector.
166
+ # (local magnification t, oblique detector incidence ray_len / |denom|).
167
+ amp = t * ray_len / (abs(denom) * det_spacing_vox)
166
168
 
167
169
  idet_min = int(math.floor(u0 / det_spacing_vox + center_u - support_u / det_spacing_vox))
168
170
  idet_max = int(math.ceil(u0 / det_spacing_vox + center_u + support_u / det_spacing_vox))
@@ -182,6 +184,6 @@ def _fan_2d_footprint_backward_kernel(
182
184
  if overlap <= eps:
183
185
  continue
184
186
  weight = overlap / width_u
185
- accum += d_sino[iang, idet] * weight * l_phi
187
+ accum += d_sino[iang, idet] * weight * amp
186
188
 
187
189
  d_image[iy, ix] = accum
@@ -37,7 +37,6 @@ def _parallel_2d_footprint_forward_kernel(
37
37
  u_vec_x = d_det_u_vec[iang, 0]
38
38
  u_vec_y = d_det_u_vec[iang, 1]
39
39
 
40
- l_phi = _ONE / max(max(abs(dir_x), abs(dir_y)), eps)
41
40
  half_u = _HALF * (abs(u_vec_x) + abs(u_vec_y))
42
41
  half_u = max(half_u, _HALF * det_spacing_vox)
43
42
  width_u = max(half_u + half_u, det_spacing_vox)
@@ -63,7 +62,9 @@ def _parallel_2d_footprint_forward_kernel(
63
62
 
64
63
  fp_lo = u0 - half_u
65
64
  fp_hi = u0 + half_u
66
- scaled_val = val * l_phi
65
+ # Mass-conserving footprint amplitude: the output is a voxel-unit line integral
66
+ # averaged over the detector cell, on the same scale as the Siddon projector.
67
+ scaled_val = val / det_spacing_vox
67
68
 
68
69
  for idet in range(idet_min, idet_max + 1):
69
70
  pixel_u = (np.float32(idet) - center_u) * det_spacing_vox
@@ -102,7 +103,6 @@ def _parallel_2d_footprint_backward_kernel(
102
103
  u_vec_x = d_det_u_vec[iang, 0]
103
104
  u_vec_y = d_det_u_vec[iang, 1]
104
105
 
105
- l_phi = _ONE / max(max(abs(dir_x), abs(dir_y)), eps)
106
106
  half_u = _HALF * (abs(u_vec_x) + abs(u_vec_y))
107
107
  half_u = max(half_u, _HALF * det_spacing_vox)
108
108
  width_u = max(half_u + half_u, det_spacing_vox)
@@ -130,6 +130,6 @@ def _parallel_2d_footprint_backward_kernel(
130
130
  if overlap <= eps:
131
131
  continue
132
132
  weight = overlap / width_u
133
- accum += d_sino[iang, idet] * weight * l_phi
133
+ accum += d_sino[iang, idet] * weight / det_spacing_vox
134
134
 
135
135
  d_image[iy, ix] = accum
@@ -38,6 +38,8 @@ from .projectors import (
38
38
  cone_backward_footprint,
39
39
  )
40
40
  from . import geometry
41
+ from . import conventions
42
+ from .conventions import kernel_detector_center, line_integral_scale
41
43
  from .geometry import __all__ as _geometry_all
42
44
 
43
45
  # Re-export trajectory generators at top level (mirrors the MLX API).
@@ -200,11 +202,14 @@ from .tv_gradients import (
200
202
  awtv_gradient,
201
203
  )
202
204
 
203
- __version__ = "2.1.0"
205
+ __version__ = "2.3.0"
204
206
 
205
207
  __all__ = [
206
208
  "backend",
207
209
  "geometry",
210
+ "conventions",
211
+ "kernel_detector_center",
212
+ "line_integral_scale",
208
213
  # Projector functions
209
214
  "parallel_forward",
210
215
  "parallel_backward",
@@ -460,7 +460,11 @@ _CONE_3D_FOOTPRINT_FORWARD_SOURCE = """
460
460
  float fp_u_hi = u0 + half_u;
461
461
  float fp_v_lo = v0 - half_v;
462
462
  float fp_v_hi = v0 + half_v;
463
- float scaled_val = val * voxel_spacing;
463
+ // Mass-conserving footprint amplitude: voxel-unit line integral averaged over the
464
+ // detector cell, on the same scale as the Siddon projector.
465
+ float ray_len = metal::sqrt(rx * rx + ry * ry + rz * rz);
466
+ float amp = (voxel_spacing * voxel_spacing / (du * dv)) * t * t * ray_len * metal::abs(inv_denom);
467
+ float scaled_val = val * amp;
464
468
 
465
469
  for (int iu = iu_min; iu <= iu_max; ++iu) {
466
470
  float pixel_u = ((float)iu - center_u) * du;
@@ -617,6 +621,11 @@ _CONE_3D_FOOTPRINT_BACKWARD_SOURCE = """
617
621
  float fp_v_lo = v0 - half_v;
618
622
  float fp_v_hi = v0 + half_v;
619
623
 
624
+ // Mass-conserving footprint amplitude: voxel-unit line integral averaged over the
625
+ // detector cell, on the same scale as the Siddon projector.
626
+ float ray_len = metal::sqrt(rx * rx + ry * ry + rz * rz);
627
+ float amp = (voxel_spacing * voxel_spacing / (du * dv)) * t * t * ray_len * metal::abs(inv_denom);
628
+
620
629
  for (int iu = iu_min; iu <= iu_max; ++iu) {
621
630
  float pixel_u = ((float)iu - center_u) * du;
622
631
  float pixel_u_lo = pixel_u - 0.5f * du;
@@ -632,7 +641,7 @@ _CONE_3D_FOOTPRINT_BACKWARD_SOURCE = """
632
641
  float overlap_v = metal::min(pixel_v_hi, fp_v_hi) - metal::max(pixel_v_lo, fp_v_lo);
633
642
  if (overlap_v <= eps) continue;
634
643
  float wv = overlap_v / width_v;
635
- accum += sino[iview * n_u * n_v + iu * n_v + iv] * wu * wv * voxel_spacing;
644
+ accum += sino[iview * n_u * n_v + iu * n_v + iv] * wu * wv * amp;
636
645
  }
637
646
  }
638
647
  }
@@ -770,6 +779,11 @@ _CONE_3D_FOOTPRINT_BACKWARD_SPARSE_SOURCE = """
770
779
  float fp_v_lo = v0 - half_v;
771
780
  float fp_v_hi = v0 + half_v;
772
781
 
782
+ // Mass-conserving footprint amplitude: voxel-unit line integral averaged over the
783
+ // detector cell, on the same scale as the Siddon projector.
784
+ float ray_len = metal::sqrt(rx * rx + ry * ry + rz * rz);
785
+ float amp = (voxel_spacing * voxel_spacing / (du * dv)) * t * t * ray_len * metal::abs(inv_denom);
786
+
773
787
  for (int iu = iu_min; iu <= iu_max; ++iu) {
774
788
  float pixel_u = ((float)iu - center_u) * du;
775
789
  float pixel_u_lo = pixel_u - 0.5f * du;
@@ -785,7 +799,7 @@ _CONE_3D_FOOTPRINT_BACKWARD_SPARSE_SOURCE = """
785
799
  float overlap_v = metal::min(pixel_v_hi, fp_v_hi) - metal::max(pixel_v_lo, fp_v_lo);
786
800
  if (overlap_v <= eps) continue;
787
801
  float wv = overlap_v / width_v;
788
- accum += sino[iview * n_u * n_v + iu * n_v + iv] * wu * wv * voxel_spacing;
802
+ accum += sino[iview * n_u * n_v + iu * n_v + iv] * wu * wv * amp;
789
803
  }
790
804
  }
791
805
  }
@@ -333,9 +333,9 @@ _FAN_2D_FOOTPRINT_FORWARD_SOURCE = """
333
333
 
334
334
  float ray_len = metal::sqrt(rx * rx + ry * ry);
335
335
  if (ray_len <= eps) continue;
336
- float dir_x = rx / ray_len;
337
- float dir_y = ry / ray_len;
338
- float l_phi = 1.0f / metal::max(metal::max(metal::abs(dir_x), metal::abs(dir_y)), eps);
336
+ // Mass-conserving footprint amplitude: voxel-unit line integral averaged over the
337
+ // detector cell, on the same scale as the Siddon projector.
338
+ float amp = t * ray_len / (metal::abs(denom) * det_spacing_vox);
339
339
 
340
340
  int idet_min = (int)metal::floor(u0 / det_spacing_vox + center_u - support_u / det_spacing_vox);
341
341
  int idet_max = (int)metal::ceil(u0 / det_spacing_vox + center_u + support_u / det_spacing_vox);
@@ -344,7 +344,7 @@ _FAN_2D_FOOTPRINT_FORWARD_SOURCE = """
344
344
 
345
345
  float fp_lo = u0 - half_u;
346
346
  float fp_hi = u0 + half_u;
347
- float scaled_val = val * l_phi;
347
+ float scaled_val = val * amp;
348
348
 
349
349
  for (int idet = idet_min; idet <= idet_max; ++idet) {
350
350
  float pixel_u = ((float)idet - center_u) * det_spacing_vox;
@@ -431,9 +431,9 @@ _FAN_2D_FOOTPRINT_BACKWARD_SOURCE = """
431
431
 
432
432
  float ray_len = metal::sqrt(rx * rx + ry * ry);
433
433
  if (ray_len <= eps) continue;
434
- float dir_x = rx / ray_len;
435
- float dir_y = ry / ray_len;
436
- float l_phi = 1.0f / metal::max(metal::max(metal::abs(dir_x), metal::abs(dir_y)), eps);
434
+ // Mass-conserving footprint amplitude: voxel-unit line integral averaged over the
435
+ // detector cell, on the same scale as the Siddon projector.
436
+ float amp = t * ray_len / (metal::abs(denom) * det_spacing_vox);
437
437
 
438
438
  int idet_min = (int)metal::floor(u0 / det_spacing_vox + center_u - support_u / det_spacing_vox);
439
439
  int idet_max = (int)metal::ceil(u0 / det_spacing_vox + center_u + support_u / det_spacing_vox);
@@ -450,7 +450,7 @@ _FAN_2D_FOOTPRINT_BACKWARD_SOURCE = """
450
450
  float overlap = metal::min(pixel_hi, fp_hi) - metal::max(pixel_lo, fp_lo);
451
451
  if (overlap <= eps) continue;
452
452
  float weight = overlap / width_u;
453
- accum += sino[iang * n_det + idet] * weight * l_phi;
453
+ accum += sino[iang * n_det + idet] * weight * amp;
454
454
  }
455
455
  }
456
456
 
@@ -274,7 +274,6 @@ _PARALLEL_2D_FOOTPRINT_FORWARD_SOURCE = """
274
274
  float u_vec_x = det_u_vec[iang * 2 + 0];
275
275
  float u_vec_y = det_u_vec[iang * 2 + 1];
276
276
 
277
- float l_phi = 1.0f / metal::max(metal::max(metal::abs(dir_x), metal::abs(dir_y)), eps);
278
277
  float half_u = 0.5f * (metal::abs(u_vec_x) + metal::abs(u_vec_y));
279
278
  half_u = metal::max(half_u, 0.5f * det_spacing_vox);
280
279
  float width_u = metal::max(2.0f * half_u, det_spacing_vox);
@@ -297,7 +296,9 @@ _PARALLEL_2D_FOOTPRINT_FORWARD_SOURCE = """
297
296
 
298
297
  float fp_lo = u0 - half_u;
299
298
  float fp_hi = u0 + half_u;
300
- float scaled_val = val * l_phi;
299
+ // Mass-conserving footprint amplitude: voxel-unit line integral averaged over the
300
+ // detector cell, on the same scale as the Siddon projector.
301
+ float scaled_val = val / det_spacing_vox;
301
302
 
302
303
  for (int idet = idet_min; idet <= idet_max; ++idet) {
303
304
  float pixel_u = ((float)idet - center_u) * det_spacing_vox;
@@ -352,7 +353,6 @@ _PARALLEL_2D_FOOTPRINT_BACKWARD_SOURCE = """
352
353
  float u_vec_x = det_u_vec[iang * 2 + 0];
353
354
  float u_vec_y = det_u_vec[iang * 2 + 1];
354
355
 
355
- float l_phi = 1.0f / metal::max(metal::max(metal::abs(dir_x), metal::abs(dir_y)), eps);
356
356
  float half_u = 0.5f * (metal::abs(u_vec_x) + metal::abs(u_vec_y));
357
357
  half_u = metal::max(half_u, 0.5f * det_spacing_vox);
358
358
  float width_u = metal::max(2.0f * half_u, det_spacing_vox);
@@ -377,7 +377,7 @@ _PARALLEL_2D_FOOTPRINT_BACKWARD_SOURCE = """
377
377
  float overlap = metal::min(pixel_hi, fp_hi) - metal::max(pixel_lo, fp_lo);
378
378
  if (overlap <= eps) continue;
379
379
  float weight = overlap / width_u;
380
- accum += sino[iang * n_det + idet] * weight * l_phi;
380
+ accum += sino[iang * n_det + idet] * weight / det_spacing_vox;
381
381
  }
382
382
  }
383
383
 
@@ -0,0 +1,83 @@
1
+ """Detector and unit conventions of the projectors, and converters between them.
2
+
3
+ The kernels on both backends (Torch/CUDA and MLX/Metal) share the conventions
4
+ of the upstream diffct ``dev`` line they were imported from (v1.3.3.dev0):
5
+
6
+ * **Detector pixels** — pixel ``i`` of an ``N``-pixel row sits at
7
+ ``det_center + (i - N/2) * du * u``. ``det_center`` is therefore the centre of
8
+ pixel ``N/2``, half a pixel off the geometric centre of the panel
9
+ (``"kernel"`` convention).
10
+ * **Line integrals** — returned in voxel units. Multiply by ``voxel_spacing``
11
+ to get physical line integrals (``mu * mm``).
12
+
13
+ Upstream diffct >= 2.0.0, DiffCT-MLX 1.x and scanner geometry files instead
14
+ treat ``det_center`` as the geometric panel centre, with pixel ``i`` at
15
+ ``(i - (N - 1)/2) * du`` (``"centered"`` convention), and return physical
16
+ line integrals. The operator factories take
17
+ ``detector_convention=`` and ``line_integrals=`` to select either convention;
18
+ :func:`kernel_detector_center` does the detector conversion for direct calls
19
+ of the low-level projectors and the analytical FBP/FDK.
20
+ """
21
+
22
+ from __future__ import annotations
23
+
24
+ from typing import Any
25
+
26
+ __all__ = [
27
+ "DETECTOR_CONVENTIONS",
28
+ "LINE_INTEGRAL_UNITS",
29
+ "kernel_detector_center",
30
+ "line_integral_scale",
31
+ ]
32
+
33
+ #: ``"kernel"``: pixel i at (i - N/2)·du (kernel native, diffct dev line).
34
+ #: ``"centered"``: pixel i at (i - (N-1)/2)·du (det_center = panel centre).
35
+ DETECTOR_CONVENTIONS = ("kernel", "centered")
36
+
37
+ #: ``"voxel"``: line integrals in voxel units (kernel native).
38
+ #: ``"physical"``: line integrals in the physical unit of ``voxel_spacing``.
39
+ LINE_INTEGRAL_UNITS = ("voxel", "physical")
40
+
41
+
42
+ def _check(value: str, allowed: tuple[str, ...], name: str) -> str:
43
+ key = str(value).strip().lower()
44
+ if key not in allowed:
45
+ raise ValueError(f"{name} must be one of {allowed}, got {value!r}")
46
+ return key
47
+
48
+
49
+ def kernel_detector_center(
50
+ det_center: Any,
51
+ det_u_vec: Any,
52
+ du: float,
53
+ det_v_vec: Any = None,
54
+ dv: float | None = None,
55
+ *,
56
+ convention: str = "centered",
57
+ ) -> Any:
58
+ """Return the ``det_center`` the kernels need for a given detector convention.
59
+
60
+ ``det_center`` is interpreted in ``convention``. For ``"centered"`` (the
61
+ geometric panel centre, pixel ``i`` at ``(i - (N-1)/2)·du``) the result is
62
+ shifted by ``+du/2`` along ``u`` and, for a 2D detector, ``+dv/2`` along
63
+ ``v``, so the kernels' ``(i - N/2)·du`` grid lands on the same pixel
64
+ centres. For ``"kernel"`` it is returned unchanged.
65
+
66
+ Works for numpy, torch and MLX arrays (plain arithmetic, autograd-safe).
67
+ For a parallel beam pass ``det_origin`` as ``det_center``.
68
+ """
69
+ key = _check(convention, DETECTOR_CONVENTIONS, "detector_convention")
70
+ if key == "kernel":
71
+ return det_center
72
+ shifted = det_center + (0.5 * float(du)) * det_u_vec
73
+ if det_v_vec is not None:
74
+ if dv is None:
75
+ raise ValueError("dv is required when det_v_vec is given")
76
+ shifted = shifted + (0.5 * float(dv)) * det_v_vec
77
+ return shifted
78
+
79
+
80
+ def line_integral_scale(line_integrals: str, voxel_spacing: float) -> float:
81
+ """Factor that turns kernel output (voxel units) into ``line_integrals`` units."""
82
+ key = _check(line_integrals, LINE_INTEGRAL_UNITS, "line_integrals")
83
+ return float(voxel_spacing) if key == "physical" else 1.0