diffct-mlx 2.1.0__tar.gz → 2.2.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 (126) hide show
  1. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/CHANGELOG.md +69 -0
  2. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/PKG-INFO +6 -3
  3. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/README.md +4 -1
  4. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/kernels/cone_footprint.py +20 -3
  5. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/kernels/fan_footprint.py +10 -8
  6. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/kernels/parallel_footprint.py +4 -4
  7. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/__init__.py +1 -1
  8. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/backend/metal/kernels/cone_beam.py +17 -3
  9. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/backend/metal/kernels/fan_beam.py +8 -8
  10. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/backend/metal/kernels/parallel_beam.py +4 -4
  11. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/docs/source/conf.py +1 -1
  12. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/pyproject.toml +1 -1
  13. diffct_mlx-2.2.0/tests/test_footprint_amplitude.py +150 -0
  14. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_quantitative_fdk.py +53 -0
  15. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/.github/workflows/docs.yml +0 -0
  16. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/.github/workflows/publish.yml +0 -0
  17. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/.github/workflows/release.yml +0 -0
  18. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/.gitignore +0 -0
  19. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/ATTRIBUTION.md +0 -0
  20. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/LICENSE +0 -0
  21. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/__init__.py +0 -0
  22. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/analytical.py +0 -0
  23. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/constants.py +0 -0
  24. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/differentiable.py +0 -0
  25. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/footprint.py +0 -0
  26. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/geometry.py +0 -0
  27. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/kernels/__init__.py +0 -0
  28. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/kernels/cone_beam.py +0 -0
  29. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/kernels/fan_beam.py +0 -0
  30. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/kernels/parallel_beam.py +0 -0
  31. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/projectors.py +0 -0
  32. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct/utils.py +0 -0
  33. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/analytical.py +0 -0
  34. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/backend/__init__.py +0 -0
  35. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/backend/_mlx.py +0 -0
  36. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/backend/_torch.py +0 -0
  37. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/backend/metal/__init__.py +0 -0
  38. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/backend/metal/constants.py +0 -0
  39. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/backend/metal/geometry.py +0 -0
  40. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/backend/metal/kernels/__init__.py +0 -0
  41. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/backend/metal/projectors.py +0 -0
  42. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/backend/metal/utils.py +0 -0
  43. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/calibration.py +0 -0
  44. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/filters.py +0 -0
  45. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/functionals.py +0 -0
  46. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/geometry.py +0 -0
  47. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/operators.py +0 -0
  48. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/orchestration/__init__.py +0 -0
  49. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/orchestration/out_of_core.py +0 -0
  50. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/phantoms/__init__.py +0 -0
  51. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/phantoms/engine.py +0 -0
  52. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/phantoms/shepp_logan.py +0 -0
  53. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/physics/__init__.py +0 -0
  54. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/physics/corrections.py +0 -0
  55. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/physics/data/spectra.npz +0 -0
  56. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/physics/pipeline.py +0 -0
  57. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/physics/simulate.py +0 -0
  58. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/physics/spectra.py +0 -0
  59. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/projectors.py +0 -0
  60. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/real_measured_data_helper.py +0 -0
  61. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/rebinning.py +0 -0
  62. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/__init__.py +0 -0
  63. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/_analytic.py +0 -0
  64. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/_core.py +0 -0
  65. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/_solver.py +0 -0
  66. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/asd_pocs.py +0 -0
  67. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/awtv_pocs.py +0 -0
  68. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/cases.py +0 -0
  69. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/dart.py +0 -0
  70. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/fbp.py +0 -0
  71. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/fdk.py +0 -0
  72. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/sart.py +0 -0
  73. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/sirt.py +0 -0
  74. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/solvers.py +0 -0
  75. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/reconstruction_algorithms/tv_pocs.py +0 -0
  76. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/regularizers.py +0 -0
  77. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/diffct_mlx/tv_gradients.py +0 -0
  78. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/docs/Makefile +0 -0
  79. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/docs/source/_static/.gitkeep +0 -0
  80. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/docs/source/api.rst +0 -0
  81. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/docs/source/examples.rst +0 -0
  82. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/docs/source/fbp_fan_example.rst +0 -0
  83. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/docs/source/fbp_parallel_example.rst +0 -0
  84. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/docs/source/fdk_cone_example.rst +0 -0
  85. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/docs/source/getting_started.rst +0 -0
  86. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/docs/source/index.rst +0 -0
  87. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/docs/source/iterative_reco_cone_example.rst +0 -0
  88. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/docs/source/iterative_reco_fan_example.rst +0 -0
  89. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/docs/source/iterative_reco_parallel_example.rst +0 -0
  90. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/examples/circular_trajectory/fbp_fan.py +0 -0
  91. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/examples/circular_trajectory/fbp_parallel.py +0 -0
  92. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/examples/circular_trajectory/fdk_cone.py +0 -0
  93. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/examples/circular_trajectory/iterative_reco_cone.py +0 -0
  94. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/examples/circular_trajectory/iterative_reco_fan.py +0 -0
  95. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/examples/circular_trajectory/iterative_reco_parallel.py +0 -0
  96. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/examples/non_circular_trajectory/iterative_reco_cone.py +0 -0
  97. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/examples/non_circular_trajectory/iterative_reco_fan.py +0 -0
  98. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/examples/non_circular_trajectory/iterative_reco_parallel.py +0 -0
  99. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/examples/plot_trajectory.py +0 -0
  100. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/pytest.ini +0 -0
  101. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/requirements.txt +0 -0
  102. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/__init__.py +0 -0
  103. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/_phantoms.py +0 -0
  104. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/benchmarks/README.md +0 -0
  105. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/benchmarks/__init__.py +0 -0
  106. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/benchmarks/_common.py +0 -0
  107. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/benchmarks/test_bench_cone.py +0 -0
  108. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/benchmarks/test_bench_fan.py +0 -0
  109. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/benchmarks/test_bench_parallel.py +0 -0
  110. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_adjoint_inner_product.py +0 -0
  111. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_cone_projector_autograd.py +0 -0
  112. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_cuda_smoke.py +0 -0
  113. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_diffct_mlx_extensions.py +0 -0
  114. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_fbp_fan_accuracy.py +0 -0
  115. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_fbp_fan_offsets.py +0 -0
  116. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_fbp_parallel_accuracy.py +0 -0
  117. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_fdk_cone_accuracy.py +0 -0
  118. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_fdk_cone_offsets.py +0 -0
  119. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_gradcheck.py +0 -0
  120. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_known_operator_gradflow.py +0 -0
  121. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_metal_kernel_static.py +0 -0
  122. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_ramp_filter_windows.py +0 -0
  123. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_regressions.py +0 -0
  124. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_siddon_centering.py +0 -0
  125. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tests/test_weights.py +0 -0
  126. {diffct_mlx-2.1.0 → diffct_mlx-2.2.0}/tools/generate_spectra.py +0 -0
@@ -9,6 +9,75 @@ and the project follows [Semantic Versioning](https://semver.org/).
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [2.2.0] - 2026-11-18
13
+
14
+ ### Fixed
15
+
16
+ - **Footprint projectors return line integrals.** The separable-footprint
17
+ kernels (parallel, fan, cone; forward, adjoint and the sparse cone adjoint;
18
+ Metal and CUDA) distributed every voxel with weights that sum to one over
19
+ the detector cells it touches (`overlap / footprint_width`). The output was
20
+ therefore not a line integral: the cone sinogram scaled with
21
+ `(detector cell size at the isocentre / voxel size)**2` (0.21x for a 0.28 mm
22
+ cell on 0.6 mm voxels), and the parallel and fan sinograms carried an
23
+ additional view-angle factor `1 / max(|cos|, |sin|)` of up to 1.41x at 45
24
+ degrees plus the factor `detector cell / voxel`. The kernels now use a
25
+ mass-conserving amplitude (parallel: `1 / det_spacing_vox`; fan:
26
+ `t * |r| / (|r.n| * det_spacing_vox)`; cone:
27
+ `vs^2 * t^2 * |r| / (|r.n| * du * dv)` with local magnification `t`), so a
28
+ footprint sinogram is a voxel-unit line integral averaged over the detector
29
+ cell and agrees with the Siddon projector to within 1 % for any detector
30
+ spacing, voxel spacing and magnification
31
+ (`tests/test_footprint_amplitude.py`). The forward/adjoint pairs remain
32
+ exact adjoints.
33
+
34
+ ### Changed
35
+
36
+ - **Numerical output of every `*_footprint` projector and of operators built
37
+ with `projector_mode="footprint"` (the default) changes** by the factors
38
+ above. Consequences:
39
+ - Sensitivity-normalised SART (`SARTParameters` default) is unaffected when
40
+ data and operator come from the same footprint pair. Reconstructing
41
+ line-integral data (Siddon simulation, measured data) with the footprint
42
+ pair now yields the correct amplitude; before, the built-in fan and cone
43
+ cases came out 1.4x and 2.4x too large.
44
+ - Physical noise or beam-hardening models applied to footprint sinograms
45
+ (`exp(-p)`) now see the correct attenuation.
46
+ - Unnormalised iterative updates (`voxel_sensitivity_normalization=False`)
47
+ need a `backprojection_scale`: the adjoint of ones is now
48
+ `(vs * t / du)^2` per view instead of one. The case default 0.215 remains
49
+ stable for the built-in cone case.
50
+ - Results that mixed footprint sinograms with absolute thresholds or fixed
51
+ step sizes tuned on 2.1.0 and earlier are not comparable across this
52
+ change.
53
+
54
+
55
+ ### Known issues
56
+
57
+ - The quantitative FDK path (`reconstruct_case_fdk`, `fdk_back_project`) is
58
+ **torch/CUDA-only**: it drives the vendored `cone_weighted_backproject`
59
+ gather kernel, which has no Metal counterpart yet. On MLX the cone cases
60
+ fall back to the legacy calibrated path (synthetic cases) or report
61
+ `supports_fdk=False` (measured/npy). Porting needs a Metal FDK gather
62
+ kernel with `(FOD/U)^2` weighting plus a `cone_weighted_backproject`
63
+ equivalent exposed through the backend's `analytical` module — same
64
+ structure as the CUDA one (`diffct/kernels/cone_beam.py::
65
+ _cone_3d_fdk_backproject_kernel`, wired via
66
+ `diffct_mlx/reconstruction_algorithms/cases.py::_quantitative_fdk_operators`).
67
+
68
+ ### Notes
69
+
70
+ - Acceptance tests hardened per the FDK review: object-size sweep
71
+ (25/50/75 % FOV, amplitude 0.90–1.08×) and detector-pitch invariance
72
+ (du ∈ {1.0, 0.556, 0.278} mm within 4 % of each other). A proposed
73
+ du-free DFT-bin ramp (`|fftfreq|` without factor 2 and without
74
+ `sample_spacing`) was evaluated against these and rejected: it scales
75
+ the reconstruction by ~`du/2` (0.484/0.269/0.134× across the sweep —
76
+ invisible only at `du = 1`). The physical filtering carries
77
+ `Q = IFFT(FFT(g)·|f_k|)/du`; the quantitative path's
78
+ `sample_spacing`/constants implement exactly that and are
79
+ pitch-invariant (0.966–0.968× across the sweep).
80
+
12
81
  ## [2.1.0] - 2026-07-16
13
82
 
14
83
  ### Added
@@ -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.2.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
@@ -145,7 +145,10 @@ filter itself now accepts `pad_factor` and `sample_spacing`, and
145
145
  `FBPParameters`/`FDKParameters` default to `pad_factor=2`.
146
146
 
147
147
  > **Unit convention.** The projectors integrate in **voxel units**: physical
148
- > line integrals are `(A @ x) * voxel_spacing`. Operators built via
148
+ > line integrals are `(A @ x) * voxel_spacing`. This holds for both projector
149
+ > modes: since 2.2.0 the footprint kernels are mass-conserving and agree with
150
+ > the Siddon kernels in amplitude (earlier versions scaled footprint output
151
+ > with the detector-cell-to-voxel size ratio). Operators built via
149
152
  > `make_*_operator` carry `A.voxel_spacing` as metadata, and the boundaries
150
153
  > where physical units matter handle the conversion automatically —
151
154
  > `simulate_scan` rescales before the `exp(-p)` / beam-hardening physics (and
@@ -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
@@ -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
@@ -200,7 +200,7 @@ from .tv_gradients import (
200
200
  awtv_gradient,
201
201
  )
202
202
 
203
- __version__ = "2.1.0"
203
+ __version__ = "2.2.0"
204
204
 
205
205
  __all__ = [
206
206
  "backend",
@@ -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
 
@@ -9,7 +9,7 @@
9
9
  project = 'DiffCT-MLX'
10
10
  copyright = '2025-2026, Linda-Sophie Schneider; upstream diffct (c) Yipeng Sun'
11
11
  author = 'Linda-Sophie Schneider'
12
- release = '2.1.0'
12
+ release = '2.2.0'
13
13
 
14
14
  # -- General configuration ---------------------------------------------------
15
15
  # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "diffct-mlx"
7
- version = "2.1.0"
7
+ version = "2.2.0"
8
8
  description = "Differentiable computed tomography with an auto-selecting backend (Torch/CUDA or Apple MLX)"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -0,0 +1,150 @@
1
+ """Amplitude contract of the separable-footprint projectors.
2
+
3
+ The footprint kernels used to distribute every voxel with weights that sum to
4
+ one over the detector cells it touches (``overlap / footprint_width``). The
5
+ result was not a line integral: the cone output scaled with
6
+ ``(detector cell size at the isocentre / voxel size)**2``, and the parallel and
7
+ fan outputs additionally carried a view-angle dependent factor of up to
8
+ ``sqrt(2)``. The kernels now use a mass-conserving amplitude, so a footprint
9
+ sinogram is a voxel-unit line integral averaged over the detector cell, on the
10
+ same scale as the Siddon projector, for any detector spacing, voxel spacing and
11
+ magnification. These tests pin that contract and the adjointness of the pairs.
12
+
13
+ Backend-neutral: runs on MLX (Apple Silicon) and torch/CUDA alike.
14
+ """
15
+
16
+ import math
17
+
18
+ import numpy as np
19
+ import pytest
20
+
21
+ import diffct_mlx as dct
22
+ from diffct_mlx.backend import xp, active as _b
23
+
24
+
25
+ def _skip_if_unusable():
26
+ if dct.backend == "torch":
27
+ import torch
28
+
29
+ if not torch.cuda.is_available():
30
+ pytest.skip("CUDA device required")
31
+
32
+
33
+ def _disc(n, voxel_spacing, radius_mm):
34
+ ax = (np.arange(n, dtype=np.float32) - 0.5 * (n - 1)) * voxel_spacing
35
+ yy, xx = np.meshgrid(ax, ax, indexing="ij")
36
+ return xp.array(((xx * xx + yy * yy) <= radius_mm**2).astype(np.float32))
37
+
38
+
39
+ def _sphere(n, voxel_spacing, radius_mm, z_offset_mm=0.0):
40
+ ax = (np.arange(n, dtype=np.float32) - 0.5 * (n - 1)) * voxel_spacing
41
+ zz, yy, xx = np.meshgrid(ax, ax, ax, indexing="ij")
42
+ inside = (xx * xx + yy * yy + (zz - z_offset_mm) ** 2) <= radius_mm**2
43
+ return xp.array(inside.astype(np.float32))
44
+
45
+
46
+ def _per_view_mass_ratio(footprint, siddon):
47
+ f = _b.to_numpy(footprint).astype(np.float64)
48
+ s = _b.to_numpy(siddon).astype(np.float64)
49
+ axes = tuple(range(1, f.ndim))
50
+ return f.sum(axis=axes) / s.sum(axis=axes)
51
+
52
+
53
+ def _central_ratio(footprint, siddon, half=2):
54
+ f = _b.to_numpy(footprint).astype(np.float64)
55
+ s = _b.to_numpy(siddon).astype(np.float64)
56
+ sl = [slice(None)] + [slice(n // 2 - half, n // 2 + half) for n in f.shape[1:]]
57
+ axes = tuple(range(1, f.ndim))
58
+ return f[tuple(sl)].mean(axis=axes) / s[tuple(sl)].mean(axis=axes)
59
+
60
+
61
+ # (image size, voxel spacing, detector cells, detector spacing)
62
+ _CASES_2D = [(128, 1.0, 192, 1.0), (128, 1.0, 384, 0.5), (128, 1.0, 96, 2.0), (256, 0.5, 384, 0.5)]
63
+
64
+
65
+ @pytest.mark.parametrize("n,vs,n_det,ds", _CASES_2D)
66
+ def test_parallel_footprint_matches_siddon_amplitude(n, vs, n_det, ds):
67
+ _skip_if_unusable()
68
+ img = _disc(n, vs, 20.0)
69
+ ray, det_o, det_u = dct.circular_trajectory_2d_parallel(8) # 0, 22.5, 45, ... degrees
70
+ sid = dct.parallel_forward(img, ray, det_o, det_u, n_det, ds, vs)
71
+ fpt = dct.parallel_forward_footprint(img, ray, det_o, det_u, n_det, ds, vs)
72
+ assert np.allclose(_per_view_mass_ratio(fpt, sid), 1.0, atol=0.01)
73
+ assert np.allclose(_central_ratio(fpt, sid), 1.0, atol=0.02)
74
+
75
+
76
+ @pytest.mark.parametrize("n,vs,n_det,ds", [(128, 1.0, 256, 1.0), (128, 1.0, 512, 0.5), (128, 1.0, 128, 2.0), (256, 0.5, 512, 0.5)])
77
+ def test_fan_footprint_matches_siddon_amplitude(n, vs, n_det, ds):
78
+ _skip_if_unusable()
79
+ img = _disc(n, vs, 20.0)
80
+ src, det_c, det_u = dct.circular_trajectory_2d_fan(8, 500.0, 900.0)
81
+ sid = dct.fan_forward(img, src, det_c, det_u, n_det, ds, vs)
82
+ fpt = dct.fan_forward_footprint(img, src, det_c, det_u, n_det, ds, vs)
83
+ assert np.allclose(_per_view_mass_ratio(fpt, sid), 1.0, atol=0.01)
84
+ assert np.allclose(_central_ratio(fpt, sid), 1.0, atol=0.02)
85
+
86
+
87
+ # (volume size, voxel spacing, sid, sdd, detector cells, detector spacing)
88
+ _CASES_3D = [
89
+ (96, 1.0, 500.0, 900.0, 192, 0.5), # detector cell at isocentre much smaller than a voxel
90
+ (96, 1.0, 500.0, 900.0, 64, 2.5), # ... larger than a voxel
91
+ (128, 0.6, 500.0, 900.0, 128, 1.0), # non-unit voxel spacing
92
+ (96, 0.8, 250.0, 1000.0, 160, 2.0), # magnification 4
93
+ ]
94
+
95
+
96
+ @pytest.mark.parametrize("n,vs,sid,sdd,n_det,ds", _CASES_3D)
97
+ def test_cone_footprint_matches_siddon_amplitude(n, vs, sid, sdd, n_det, ds):
98
+ _skip_if_unusable()
99
+ vol = _sphere(n, vs, 20.0)
100
+ src, det_c, det_u, det_v = dct.circular_trajectory_3d(6, sid, sdd, start_angle=0.13)
101
+ sidd = dct.cone_forward(vol, src, det_c, det_u, det_v, n_det, n_det, ds, ds, vs)
102
+ fpt = dct.cone_forward_footprint(vol, src, det_c, det_u, det_v, n_det, n_det, ds, ds, vs)
103
+ assert np.allclose(_per_view_mass_ratio(fpt, sidd), 1.0, atol=0.01)
104
+ assert np.allclose(_central_ratio(fpt, sidd), 1.0, atol=0.02)
105
+ # absolute scale: a central chord of 40 mm is 40 / vs in voxel units
106
+ centre = float(_central_ratio(fpt, sidd)[0] * _b.to_numpy(sidd)[0, n_det // 2 - 2 : n_det // 2 + 2, n_det // 2 - 2 : n_det // 2 + 2].mean())
107
+ assert abs(centre * vs / 40.0 - 1.0) < 0.03
108
+
109
+
110
+ def test_cone_footprint_amplitude_holds_off_axis():
111
+ """Oblique incidence on the flat detector (large cone angle, off-centre object)."""
112
+ _skip_if_unusable()
113
+ n, vs = 96, 1.0
114
+ vol = _sphere(n, vs, 12.0, z_offset_mm=28.0)
115
+ src, det_c, det_u, det_v = dct.circular_trajectory_3d(6, 150.0, 300.0, start_angle=0.13)
116
+ sidd = dct.cone_forward(vol, src, det_c, det_u, det_v, 256, 256, 1.0, 1.0, vs)
117
+ fpt = dct.cone_forward_footprint(vol, src, det_c, det_u, det_v, 256, 256, 1.0, 1.0, vs)
118
+ assert np.allclose(_per_view_mass_ratio(fpt, sidd), 1.0, atol=0.015)
119
+
120
+
121
+ def _rel_adjoint_error(Ax, y, x, Aty):
122
+ lhs = float((Ax * y).sum())
123
+ rhs = float((x * Aty).sum())
124
+ assert math.isfinite(lhs) and abs(lhs) > 0
125
+ return abs(lhs - rhs) / abs(lhs)
126
+
127
+
128
+ def test_footprint_pairs_stay_adjoint_for_general_spacings():
129
+ _skip_if_unusable()
130
+ rng = np.random.default_rng(0)
131
+ for vs, ds in ((1.0, 1.0), (0.7, 1.3), (1.4, 0.5)):
132
+ x2 = xp.array(rng.standard_normal((40, 40)).astype(np.float32))
133
+ y2 = xp.array(rng.standard_normal((9, 72)).astype(np.float32))
134
+ ray, det_o, det_u = dct.circular_trajectory_2d_parallel(9)
135
+ err = _rel_adjoint_error(
136
+ dct.parallel_forward_footprint(x2, ray, det_o, det_u, 72, ds, vs), y2,
137
+ x2, dct.parallel_backward_footprint(y2, ray, det_o, det_u, ds, 40, 40, vs))
138
+ assert err < 5e-4
139
+ src, det_c, det_u = dct.circular_trajectory_2d_fan(9, 200.0, 400.0)
140
+ err = _rel_adjoint_error(
141
+ dct.fan_forward_footprint(x2, src, det_c, det_u, 72, ds, vs), y2,
142
+ x2, dct.fan_backward_footprint(y2, src, det_c, det_u, ds, 40, 40, vs))
143
+ assert err < 5e-4
144
+ x3 = xp.array(rng.standard_normal((24, 24, 24)).astype(np.float32))
145
+ y3 = xp.array(rng.standard_normal((7, 40, 40)).astype(np.float32))
146
+ src, det_c, det_u, det_v = dct.circular_trajectory_3d(7, 200.0, 400.0)
147
+ err = _rel_adjoint_error(
148
+ dct.cone_forward_footprint(x3, src, det_c, det_u, det_v, 40, 40, ds, ds, vs), y3,
149
+ x3, dct.cone_backward_footprint(y3, src, det_c, det_u, det_v, 24, 24, 24, ds, ds, vs))
150
+ assert err < 5e-4
@@ -145,3 +145,56 @@ def test_simulate_scan_uses_physical_line_integrals():
145
145
  p_phys = (A @ phantom) * vs
146
146
  ref = apply_beam_hardening(p_phys, spec, mu_e)
147
147
  assert float((sino - ref).abs().max()) < 1e-4
148
+
149
+
150
+ @pytest.mark.cuda
151
+ def test_quantitative_fdk_object_size_sweep():
152
+ """The wrap-around bias was object-size-DEPENDENT; the quantitative path
153
+ must stay amplitude-true across object sizes (25/50/75 % of the FOV)."""
154
+ _skip_if_no_cuda()
155
+ n, num_views, vs, du_ = 64, 120, 0.278, 0.556
156
+ det = 200
157
+ mu = 0.05
158
+ src, dc, duv, dvv = dct.circular_trajectory_3d(num_views, 300.0, 600.0)
159
+ zz, yy, xx = torch.meshgrid(*[torch.arange(n, device=DEV, dtype=torch.float32)] * 3,
160
+ indexing="ij")
161
+ r = torch.sqrt((xx - n / 2 + 0.5) ** 2 + (yy - n / 2 + 0.5) ** 2)
162
+ zmask = (zz - n / 2 + 0.5).abs() <= 0.35 * n
163
+
164
+ for frac in (0.125, 0.25, 0.375): # 25 / 50 / 75 % of the FOV width
165
+ cyl = ((r <= frac * n) & zmask).float() * mu
166
+ sino = dct.cone_forward(cyl, src, dc, duv, dvv, det, det, du_, du_, vs) * vs
167
+ qw, qf, qb = _quantitative_fdk_operators(
168
+ src, dc, duv, dvv, (n, n, n), (det, det), du_, du_, vs, sinogram_scale=1.0)
169
+ rec = qb(qf(qw(sino)))
170
+ interior = (r <= 0.6 * frac * n) & ((zz - n / 2 + 0.5).abs() <= 0.2 * n)
171
+ ratio = float(rec[interior].median()) / mu
172
+ assert 0.90 < ratio < 1.08, (frac, ratio)
173
+
174
+
175
+ @pytest.mark.cuda
176
+ def test_quantitative_fdk_detector_pitch_invariance():
177
+ """The reconstructed amplitude must not depend on the detector pitch
178
+ (a du-free DFT-bin ramp scales the result by ~du/2 — evaluated and
179
+ rejected; the physical ramp |f|/du plus the analytic constants is
180
+ pitch-invariant)."""
181
+ _skip_if_no_cuda()
182
+ n, num_views, vs, mu = 64, 120, 0.278, 0.05
183
+ zz, yy, xx = torch.meshgrid(*[torch.arange(n, device=DEV, dtype=torch.float32)] * 3,
184
+ indexing="ij")
185
+ r = torch.sqrt((xx - n / 2 + 0.5) ** 2 + (yy - n / 2 + 0.5) ** 2)
186
+ cyl = ((r <= 0.375 * n) & ((zz - n / 2 + 0.5).abs() <= 0.35 * n)).float() * mu
187
+ src, dc, duv, dvv = dct.circular_trajectory_3d(num_views, 300.0, 600.0)
188
+ interior = (r <= 0.25 * n) & ((zz - n / 2 + 0.5).abs() <= 0.2 * n)
189
+
190
+ ratios = []
191
+ for du_ in (1.0, 0.556, 0.278):
192
+ det = int(n * vs * 2 / du_ * 1.35 / 2) * 2
193
+ sino = dct.cone_forward(cyl, src, dc, duv, dvv, det, det, du_, du_, vs) * vs
194
+ qw, qf, qb = _quantitative_fdk_operators(
195
+ src, dc, duv, dvv, (n, n, n), (det, det), du_, du_, vs, sinogram_scale=1.0)
196
+ rec = qb(qf(qw(sino)))
197
+ ratios.append(float(rec[interior].median()) / mu)
198
+
199
+ assert all(0.90 < x < 1.08 for x in ratios), ratios
200
+ assert max(ratios) - min(ratios) < 0.04, ratios # pitch-invariant
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes