diffct-mlx 2.0.0__tar.gz → 2.1.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 (125) hide show
  1. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/.github/workflows/publish.yml +3 -2
  2. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/CHANGELOG.md +72 -3
  3. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/PKG-INFO +82 -39
  4. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/README.md +81 -38
  5. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/footprint.py +60 -9
  6. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/kernels/__init__.py +2 -0
  7. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/kernels/cone_beam.py +242 -0
  8. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/projectors.py +38 -1
  9. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/__init__.py +3 -1
  10. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/backend/metal/kernels/cone_beam.py +28 -31
  11. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/backend/metal/projectors.py +5 -3
  12. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/operators.py +10 -3
  13. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/physics/simulate.py +14 -1
  14. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/__init__.py +2 -0
  15. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/_analytic.py +64 -22
  16. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/cases.py +152 -1
  17. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/fbp.py +3 -1
  18. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/fdk.py +3 -1
  19. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/docs/source/api.rst +16 -1
  20. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/docs/source/conf.py +1 -1
  21. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/docs/source/getting_started.rst +14 -3
  22. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/docs/source/index.rst +14 -12
  23. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/pyproject.toml +1 -1
  24. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_known_operator_gradflow.py +109 -8
  25. diffct_mlx-2.1.0/tests/test_metal_kernel_static.py +34 -0
  26. diffct_mlx-2.1.0/tests/test_quantitative_fdk.py +147 -0
  27. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/.github/workflows/docs.yml +0 -0
  28. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/.github/workflows/release.yml +0 -0
  29. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/.gitignore +0 -0
  30. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/ATTRIBUTION.md +0 -0
  31. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/LICENSE +0 -0
  32. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/__init__.py +0 -0
  33. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/analytical.py +0 -0
  34. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/constants.py +0 -0
  35. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/differentiable.py +0 -0
  36. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/geometry.py +0 -0
  37. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/kernels/cone_footprint.py +0 -0
  38. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/kernels/fan_beam.py +0 -0
  39. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/kernels/fan_footprint.py +0 -0
  40. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/kernels/parallel_beam.py +0 -0
  41. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/kernels/parallel_footprint.py +0 -0
  42. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct/utils.py +0 -0
  43. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/analytical.py +0 -0
  44. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/backend/__init__.py +0 -0
  45. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/backend/_mlx.py +0 -0
  46. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/backend/_torch.py +0 -0
  47. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/backend/metal/__init__.py +0 -0
  48. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/backend/metal/constants.py +0 -0
  49. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/backend/metal/geometry.py +0 -0
  50. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/backend/metal/kernels/__init__.py +0 -0
  51. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/backend/metal/kernels/fan_beam.py +0 -0
  52. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/backend/metal/kernels/parallel_beam.py +0 -0
  53. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/backend/metal/utils.py +0 -0
  54. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/calibration.py +0 -0
  55. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/filters.py +0 -0
  56. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/functionals.py +0 -0
  57. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/geometry.py +0 -0
  58. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/orchestration/__init__.py +0 -0
  59. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/orchestration/out_of_core.py +0 -0
  60. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/phantoms/__init__.py +0 -0
  61. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/phantoms/engine.py +0 -0
  62. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/phantoms/shepp_logan.py +0 -0
  63. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/physics/__init__.py +0 -0
  64. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/physics/corrections.py +0 -0
  65. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/physics/data/spectra.npz +0 -0
  66. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/physics/pipeline.py +0 -0
  67. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/physics/spectra.py +0 -0
  68. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/projectors.py +0 -0
  69. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/real_measured_data_helper.py +0 -0
  70. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/rebinning.py +0 -0
  71. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/_core.py +0 -0
  72. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/_solver.py +0 -0
  73. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/asd_pocs.py +0 -0
  74. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/awtv_pocs.py +0 -0
  75. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/dart.py +0 -0
  76. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/sart.py +0 -0
  77. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/sirt.py +0 -0
  78. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/solvers.py +0 -0
  79. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/reconstruction_algorithms/tv_pocs.py +0 -0
  80. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/regularizers.py +0 -0
  81. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/diffct_mlx/tv_gradients.py +0 -0
  82. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/docs/Makefile +0 -0
  83. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/docs/source/_static/.gitkeep +0 -0
  84. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/docs/source/examples.rst +0 -0
  85. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/docs/source/fbp_fan_example.rst +0 -0
  86. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/docs/source/fbp_parallel_example.rst +0 -0
  87. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/docs/source/fdk_cone_example.rst +0 -0
  88. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/docs/source/iterative_reco_cone_example.rst +0 -0
  89. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/docs/source/iterative_reco_fan_example.rst +0 -0
  90. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/docs/source/iterative_reco_parallel_example.rst +0 -0
  91. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/examples/circular_trajectory/fbp_fan.py +0 -0
  92. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/examples/circular_trajectory/fbp_parallel.py +0 -0
  93. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/examples/circular_trajectory/fdk_cone.py +0 -0
  94. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/examples/circular_trajectory/iterative_reco_cone.py +0 -0
  95. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/examples/circular_trajectory/iterative_reco_fan.py +0 -0
  96. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/examples/circular_trajectory/iterative_reco_parallel.py +0 -0
  97. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/examples/non_circular_trajectory/iterative_reco_cone.py +0 -0
  98. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/examples/non_circular_trajectory/iterative_reco_fan.py +0 -0
  99. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/examples/non_circular_trajectory/iterative_reco_parallel.py +0 -0
  100. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/examples/plot_trajectory.py +0 -0
  101. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/pytest.ini +0 -0
  102. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/requirements.txt +0 -0
  103. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/__init__.py +0 -0
  104. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/_phantoms.py +0 -0
  105. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/benchmarks/README.md +0 -0
  106. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/benchmarks/__init__.py +0 -0
  107. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/benchmarks/_common.py +0 -0
  108. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/benchmarks/test_bench_cone.py +0 -0
  109. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/benchmarks/test_bench_fan.py +0 -0
  110. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/benchmarks/test_bench_parallel.py +0 -0
  111. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_adjoint_inner_product.py +0 -0
  112. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_cone_projector_autograd.py +0 -0
  113. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_cuda_smoke.py +0 -0
  114. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_diffct_mlx_extensions.py +0 -0
  115. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_fbp_fan_accuracy.py +0 -0
  116. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_fbp_fan_offsets.py +0 -0
  117. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_fbp_parallel_accuracy.py +0 -0
  118. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_fdk_cone_accuracy.py +0 -0
  119. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_fdk_cone_offsets.py +0 -0
  120. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_gradcheck.py +0 -0
  121. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_ramp_filter_windows.py +0 -0
  122. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_regressions.py +0 -0
  123. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_siddon_centering.py +0 -0
  124. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tests/test_weights.py +0 -0
  125. {diffct_mlx-2.0.0 → diffct_mlx-2.1.0}/tools/generate_spectra.py +0 -0
@@ -1,8 +1,9 @@
1
1
  name: Publish to PyPI
2
2
 
3
3
  on:
4
- release:
5
- types: [published]
4
+ push:
5
+ tags:
6
+ - 'v*'
6
7
  workflow_dispatch:
7
8
 
8
9
  jobs:
@@ -9,6 +9,75 @@ and the project follows [Semantic Versioning](https://semver.org/).
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [2.1.0] - 2026-07-16
13
+
14
+ ### Added
15
+
16
+ - **Quantitative FDK** (amplitude-true attenuation values) for all cone
17
+ cases, incl. measured data (`supports_fdk=True` on the torch backend) via
18
+ `reconstruct_case_fdk` and the new case fields `fdk_filter` /
19
+ `fdk_back_project`: cosine weights → per-view trapezoidal angular weights
20
+ (actual source angles) → zero-padded physical ramp (`|f|/du`) → voxel-driven
21
+ `(SID/U)²`-weighted FDK gather backprojection with the analytic
22
+ `SDD/(2π·SID)` constant. Sinogram units handled per source (measured −log
23
+ data are physical µ·mm; raw Siddon output is voxel-unit → ×`voxel_spacing`).
24
+ Verified on a known-µ cylinder at real camera geometry (du 0.556 mm, voxel
25
+ 0.278 mm, magnification 2, 75 % FOV object): recovers 0.99× true µ where the
26
+ legacy calibrated path gives 0.47× (`tests/test_quantitative_fdk.py`).
27
+ - `ramp_filter`/`ramp_filter_2d`/`ramp_filter_3d` accept `pad_factor`
28
+ (zero-padding against circular-convolution wrap-around — the unpadded ramp
29
+ biases objects by up to 0.40× at 75 % FOV coverage, object-size-DEPENDENT,
30
+ so no constant can compensate) and `sample_spacing` (physical `|f|/du`
31
+ scaling).
32
+
33
+ ### Fixed
34
+
35
+ - PyPI publishing now triggers directly from `v*` tag pushes. GitHub suppresses
36
+ follow-up workflows for releases created with `GITHUB_TOKEN`, so the former
37
+ `release.published` trigger required a manual dispatch.
38
+ - `simulate_scan` fed voxel-unit line integrals into the physical
39
+ intensity/beam-hardening chain (`exp(-p)`, polychromatic nonlinearity) —
40
+ silently wrong for `voxel_spacing != 1`. It now rescales by the operator's
41
+ voxel spacing (new `ProjectionOperator.voxel_spacing` metadata, propagated
42
+ by `.subset`; explicit `voxel_spacing=` override) and returns physical,
43
+ scanner-like sinograms. Unchanged at `voxel_spacing = 1`.
44
+ - `ReconstructionCase.voxel_spacing` documents the unit contract: iterative
45
+ reconstruction of physical (measured) sinograms yields attenuation per
46
+ voxel — divide by `voxel_spacing` for 1/mm; the quantitative FDK path
47
+ returns 1/mm directly.
48
+
49
+ ### Changed
50
+
51
+ - `main` is the single development line; the redundant `cuda` branch has been
52
+ retired and the README no longer describes branch-specific APIs.
53
+ - `FBPParameters`/`FDKParameters` gained `pad_factor` (default **2**) and
54
+ route their `detector_spacing` into the ramp's `sample_spacing`. Analytic
55
+ reconstructions therefore differ slightly (less interior depression) from
56
+ earlier unpadded results; set `pad_factor=1` to reproduce legacy output.
57
+ The legacy case constants remain valid for the synthetic unit-spacing cases.
58
+
59
+ ## [2.0.1] - 2026-07-15
60
+
61
+ ### Added
62
+
63
+ - **Analytic geometry gradients for the cone Siddon projector (CUDA and
64
+ Metal)**:
65
+ `_cone_3d_geometry_grad_kernel` computes closed-form per-view gradients
66
+ for `src_pos`, `det_center`, `det_u_vec` and `det_v_vec` in a single
67
+ kernel pass (endpoint derivatives of the trilinearly smoothed line
68
+ integral, with the projected moment accumulated by the same segment
69
+ quadrature as the gradient terms). Default for cone geometry gradients
70
+ (`DIFFCT_GEOMETRY_VJP=fd` forces finite differences). The CUDA path is
71
+ validated against an exact torch-autograd `grid_sample` reference
72
+ (cos > 0.998, norm ratio within 1%) and is ~4× faster than FD; the Metal
73
+ port uses the same center-based sampling, projected-moment quadrature and
74
+ world/voxel unit conversion.
75
+ - **Footprint forward projectors are geometry-trainable** (parallel, fan,
76
+ cone): finite-difference VJPs for their geometry arrays, so the operator
77
+ layer's default `projector_mode="footprint"` — and pipelines built on it,
78
+ e.g. differentiable view-coverage objectives — receive source/detector
79
+ gradients. Backprojectors remain data-only.
80
+
12
81
  ## [2.0.0] - 2026-07-10
13
82
 
14
83
  First stable release of the unified package. Includes everything in
@@ -16,8 +85,6 @@ First stable release of the unified package. Includes everything in
16
85
  on Apple Silicon (the last open item of the unification) and the Torch
17
86
  backend gains trainable projector geometry.
18
87
 
19
- ### Added
20
-
21
88
  - **Trainable geometry on the Torch backend**: the Siddon projectors
22
89
  (parallel/fan/cone) return finite-difference VJPs for their per-view
23
90
  geometry arrays (`src_pos`, `det_center`/`det_origin`, `det_u_vec`,
@@ -340,5 +407,7 @@ necessary for the arbitrary-trajectory kernel API:
340
407
  when the detector is not plane-aligned to the voxel axes) and will
341
408
  be tackled in a dedicated follow-up.
342
409
 
343
- [Unreleased]: https://github.com/Linda-SophieSchneider/DiffCT-MLX/compare/v2.0.0...HEAD
410
+ [Unreleased]: https://github.com/Linda-SophieSchneider/DiffCT-MLX/compare/v2.1.0...HEAD
411
+ [2.1.0]: https://github.com/Linda-SophieSchneider/DiffCT-MLX/compare/v2.0.1...v2.1.0
412
+ [2.0.1]: https://github.com/Linda-SophieSchneider/DiffCT-MLX/compare/v2.0.0...v2.0.1
344
413
  [2.0.0]: https://github.com/Linda-SophieSchneider/DiffCT-MLX/releases/tag/v2.0.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: diffct-mlx
3
- Version: 2.0.0
3
+ Version: 2.1.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
@@ -26,8 +26,7 @@ Description-Content-Type: text/markdown
26
26
  # diffct: Differentiable Computed Tomography Operators
27
27
 
28
28
  > [!NOTE]
29
- > **This is the `cuda` branch of [DiffCT-MLX](https://github.com/Linda-SophieSchneider/DiffCT-MLX), adapted from the original [diffct](https://github.com/sypsyp97) by Yipeng Sun (Apache-2.0).**
30
- > It is based on diffct's arbitrary-trajectory (`dev`) line, whose module layout and per-view projector convention match the MLX port. This branch hosts the unified **auto-backend** package `diffct_mlx` (MLX/Metal on Apple Silicon, Torch/numba-CUDA elsewhere) so the same code runs on both. Maintained by [Linda-Sophie Schneider](https://github.com/Linda-SophieSchneider).
29
+ > **[DiffCT-MLX](https://github.com/Linda-SophieSchneider/DiffCT-MLX) — the unified auto-backend package `diffct_mlx`** (MLX/Metal on Apple Silicon, Torch/numba-CUDA elsewhere), so the same code runs on both. The Torch/CUDA engine is adapted from the original [diffct](https://github.com/sypsyp97) by Yipeng Sun (Apache-2.0), based on its arbitrary-trajectory (`dev`) line. Maintained by [Linda-Sophie Schneider](https://github.com/Linda-SophieSchneider).
31
30
  > See [ATTRIBUTION.md](ATTRIBUTION.md) for full provenance and license details.
32
31
 
33
32
  [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg?style=flat-square)](https://opensource.org/licenses/Apache-2.0)
@@ -48,13 +47,12 @@ custom). Built for optimization and deep-learning integration.
48
47
  [Linda-Sophie Schneider](https://github.com/Linda-SophieSchneider) at
49
48
  [Linda-SophieSchneider/DiffCT-MLX](https://github.com/Linda-SophieSchneider/DiffCT-MLX).
50
49
 
51
- ## 🧭 Using this branch — the unified `diffct_mlx` API
50
+ ## 🧭 The unified `diffct_mlx` API
52
51
 
53
- This `cuda` branch adds **`diffct_mlx`**, a single API that auto-selects its
52
+ **`diffct_mlx`** is a single API that auto-selects its
54
53
  compute backend at import — **Torch / numba-CUDA** on NVIDIA GPUs, **Apple MLX**
55
54
  on Apple Silicon — so the *same* script runs unchanged on both. It mirrors the
56
- public API of the [DiffCT-MLX `main`](https://github.com/Linda-SophieSchneider/DiffCT-MLX)
57
- (Apple) package name-for-name.
55
+ same public API on both backends name-for-name.
58
56
 
59
57
  ```bash
60
58
  pip install "diffct-mlx[cuda]" # NVIDIA GPUs (Torch + numba-CUDA)
@@ -80,11 +78,11 @@ reco = dct.reconstruct_fbp(
80
78
 
81
79
  **Parity & status.** FBP/FDK, SART/SIRT, TV-/ASD-/AwTV-POCS, DART, phantoms,
82
80
  trajectory generators and the measured-data helpers are all available and were
83
- verified on NVIDIA GPUs. The **MLX backend** is wired in
84
- (`diffct_mlx/backend/metal/` vendors the Metal kernels and `mx.custom_function`
85
- projectors; the `xp` namespace is attribute-identical to the torch one, pinned
86
- by a static parity test) — runtime validation on Apple-Silicon hardware is the
87
- remaining step.
81
+ verified on NVIDIA GPUs. The **MLX backend** is runtime-validated on Apple
82
+ Silicon; its vendored Metal kernels use `mx.custom_function` projectors and
83
+ the same centered Siddon/geometry-gradient conventions as CUDA. The
84
+ backend-neutral `xp` namespace is attribute-identical to the torch one, pinned
85
+ by a static parity test.
88
86
 
89
87
  ### Projectors: Siddon vs. separable footprint
90
88
 
@@ -117,10 +115,49 @@ evaluation**: `cone_backward_footprint(..., indices=idx)` computes only the give
117
115
  flattened `(D, H, W)` voxels and returns a 1-D vector (useful for masked / DART
118
116
  subproblems).
119
117
 
118
+ ### Quantitative FDK (amplitude-true reconstruction)
119
+
120
+ The legacy case FDK path was a *synthetic-geometry calibration*, not physics:
121
+ an unpadded DFT-bin ramp (circular-convolution wrap-around depresses large
122
+ objects with an object-size-dependent bias — down to 0.40× at 75 % FOV
123
+ coverage), the constant `π·SID/(2·SDD·N)` (only valid for `du = dv = voxel
124
+ = 1`), and the Siddon adjoint (not an FDK backprojector). On measured data
125
+ these stack to ~0.1–0.5× of the true attenuation.
126
+
127
+ The quantitative path fixes all three: cosine pre-weights → per-view
128
+ trapezoidal angular weights (actual source angles, non-uniform trajectories
129
+ included) → zero-padded **physical** ramp (`|f|/du`, `pad_factor=2`) → the
130
+ voxel-driven `(SID/U)²`-weighted FDK gather backprojector with its analytic
131
+ `SDD/(2π·SID)` constant. Verified: a cylinder of known µ at real camera
132
+ geometry (du 0.556 mm, voxel 0.278 mm, magnification 2, object filling 75 %
133
+ of the FOV) reconstructs to **0.99×** the true µ (legacy path: 0.47×).
134
+
135
+ ```python
136
+ case = dct.build_measured_cone_3d_case(cfg) # supports_fdk=True on CUDA now
137
+ vol_mu = dct.reconstruct_case_fdk(case) # true attenuation units (1/mm)
138
+ ```
139
+
140
+ All cone cases (synthetic, measured TIFF, npy) carry the quantitative
141
+ operators (`case.fdk_weight` / `fdk_filter` / `fdk_back_project`); sinogram
142
+ units are handled per source (measured −log data are physical; raw Siddon
143
+ projector output is voxel-unit and rescaled by `voxel_spacing`). The ramp
144
+ filter itself now accepts `pad_factor` and `sample_spacing`, and
145
+ `FBPParameters`/`FDKParameters` default to `pad_factor=2`.
146
+
147
+ > **Unit convention.** The projectors integrate in **voxel units**: physical
148
+ > line integrals are `(A @ x) * voxel_spacing`. Operators built via
149
+ > `make_*_operator` carry `A.voxel_spacing` as metadata, and the boundaries
150
+ > where physical units matter handle the conversion automatically —
151
+ > `simulate_scan` rescales before the `exp(-p)` / beam-hardening physics (and
152
+ > returns physical, scanner-like sinograms), and the quantitative FDK path
153
+ > encodes it per data source. One consequence to know: **iterative**
154
+ > reconstruction of measured (physical) data converges to attenuation *per
155
+ > voxel*; divide by `case.voxel_spacing` for µ in 1/mm.
156
+
120
157
  ### Operators, solvers, regularizers, physics & simulation
121
158
 
122
- The `cuda` branch adds a composable, differentiable toolkit on top of the
123
- projectors (all GPU-resident, verified on NVIDIA GPUs):
159
+ The unified package provides a composable, differentiable toolkit on top of
160
+ the projectors (all GPU-resident, verified on NVIDIA GPUs):
124
161
 
125
162
  - **Operators** (`diffct_mlx.operators`) — a `LinearOperator` algebra: build a
126
163
  projector as `A = dct.make_cone_3d_operator(...)`, then `A @ x`, `A.T @ y`,
@@ -190,20 +227,31 @@ sizes, Plug-and-Play priors). Verified by `tests/test_known_operator_gradflow.py
190
227
  when the input carries gradients, so unrolled TV gradient steps are
191
228
  second-order differentiable (mirrors `mx.grad` composability on MLX).
192
229
 
193
- **Trainable geometry (pose/trajectory optimization).** The Torch Siddon
194
- projectors (parallel/fan/cone) also provide gradients w.r.t. their per-view
195
- geometry arrays (`src_pos`, `det_center`/`det_origin`, `det_u_vec`,
196
- `det_v_vec`, `ray_dir`) via finite-difference VJPs: computed only for inputs
197
- with `requires_grad` (the data-only path pays nothing), two forward passes per
198
- geometry component, step size chosen relative to each array's magnitude (works
199
- for mm- and m-scale setups alike). Semantics to know: Siddon ray weights are
200
- piecewise-linear in the geometry, so these are *smoothed subgradients* — exact
201
- against a matched-step contraction (pinned by tests), descent-quality in
202
- practice (see the pose-recovery test), but not second-order differentiable.
203
- On MLX, the cone projector has the equivalent VJP (`src_pos` by default, all
204
- four arrays with `DIFFCT_GEOMETRY_VJP=1`). The **footprint** family stays
205
- data-only, and stochastic simulation ops (`add_poisson_noise`) are not
206
- differentiable.
230
+ **Trainable geometry (pose/trajectory optimization).** All Torch forward
231
+ projectors provide gradients w.r.t. their per-view geometry arrays
232
+ (`src_pos`, `det_center`/`det_origin`, `det_u_vec`, `det_v_vec`, `ray_dir`),
233
+ computed only for inputs with `requires_grad` (the data-only path pays
234
+ nothing):
235
+
236
+ - **Cone Siddon** uses an **analytic geometry kernel** (default): one kernel
237
+ pass yields closed-form gradients for all four geometry arrays — the
238
+ per-ray endpoint derivatives of the trilinearly smoothed line integral,
239
+ validated against an exact torch-autograd reference (cos > 0.998) and ~4×
240
+ faster than finite differences. Set `DIFFCT_GEOMETRY_VJP=fd` to force the
241
+ FD path instead.
242
+ - **Fan/parallel Siddon and all footprint forwards** use finite-difference
243
+ VJPs (two forward passes per geometry component, magnitude-relative step —
244
+ mm- and m-scale setups both work). The operator layer's default
245
+ `projector_mode="footprint"` is therefore geometry-trainable too.
246
+
247
+ Semantics to know: the discrete forwards are piecewise-linear in the
248
+ geometry, so all of these are *smoothed* gradients — descent-quality in
249
+ practice (see the pose-recovery tests), but not second-order differentiable.
250
+ Backprojectors remain data-only, and stochastic simulation ops
251
+ (`add_poisson_noise`) are not differentiable. On MLX, cone Siddon uses the
252
+ same analytic endpoint-gradient formulation by default, including gradients
253
+ for all four geometry arrays. Set `DIFFCT_GEOMETRY_VJP=fd` for the legacy
254
+ finite-difference `src_pos` fallback.
207
255
 
208
256
  **Recommended step-size parametrization for unrolled networks.** A free
209
257
  trainable step can wander into the divergent regime, where positivity clamps
@@ -255,23 +303,18 @@ to ~1e-6, 2-GPU == 1-GPU exact, 2048³ cone-FDK end-to-end with a bounded
255
303
  working set. This path is CUDA-only (it drives the numba-CUDA kernels
256
304
  directly).
257
305
 
258
- ## 🔀 Branches
259
-
260
- ### `main` — unified auto-backend package
261
- The unified `diffct_mlx` package described above: one API, two engines
262
- (Torch/numba-CUDA and MLX/Metal), auto-selected at import. This is the branch
263
- to use on every platform.
306
+ ## 🔀 Branches & releases
264
307
 
265
- ### `cuda` — development line of the unified package
266
- Where the unification work happens before it lands on `main`; after a merge
267
- the two branches are identical.
308
+ All development happens on **`main`** (the former `cuda` development branch
309
+ has been merged and retired). Versioned releases are tagged and published to
310
+ PyPI via CI (trusted publishing); `gh-pages` hosts the built documentation.
268
311
 
269
312
  Kernels take per-view ``(src_pos, det_center, det_u_vec[, det_v_vec])``
270
313
  arrays instead of closed-form ``sdd / sid / beta`` scalars, so **spiral,
271
314
  saddle, sinusoidal, laminography, or any user-supplied trajectory** works
272
315
  without touching the kernels — on both backends.
273
316
 
274
- ⚠️ **Note:** not published to PyPI yet. If you find any bugs please
317
+ If you find any bugs please
275
318
  [raise an issue](https://github.com/Linda-SophieSchneider/DiffCT-MLX/issues).
276
319
 
277
320
  ## ✨ Features
@@ -433,7 +476,7 @@ pip install -e .
433
476
  ### Running the tests
434
477
 
435
478
  ```bash
436
- pytest tests/ -q # 97 tests, ~5 s
479
+ pytest tests/ -q # core suite (backend-specific tests may skip)
437
480
  pytest tests/benchmarks/ --benchmark-only # opt-in perf suite
438
481
  ```
439
482
 
@@ -1,8 +1,7 @@
1
1
  # diffct: Differentiable Computed Tomography Operators
2
2
 
3
3
  > [!NOTE]
4
- > **This is the `cuda` branch of [DiffCT-MLX](https://github.com/Linda-SophieSchneider/DiffCT-MLX), adapted from the original [diffct](https://github.com/sypsyp97) by Yipeng Sun (Apache-2.0).**
5
- > It is based on diffct's arbitrary-trajectory (`dev`) line, whose module layout and per-view projector convention match the MLX port. This branch hosts the unified **auto-backend** package `diffct_mlx` (MLX/Metal on Apple Silicon, Torch/numba-CUDA elsewhere) so the same code runs on both. Maintained by [Linda-Sophie Schneider](https://github.com/Linda-SophieSchneider).
4
+ > **[DiffCT-MLX](https://github.com/Linda-SophieSchneider/DiffCT-MLX) — the unified auto-backend package `diffct_mlx`** (MLX/Metal on Apple Silicon, Torch/numba-CUDA elsewhere), so the same code runs on both. The Torch/CUDA engine is adapted from the original [diffct](https://github.com/sypsyp97) by Yipeng Sun (Apache-2.0), based on its arbitrary-trajectory (`dev`) line. Maintained by [Linda-Sophie Schneider](https://github.com/Linda-SophieSchneider).
6
5
  > See [ATTRIBUTION.md](ATTRIBUTION.md) for full provenance and license details.
7
6
 
8
7
  [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg?style=flat-square)](https://opensource.org/licenses/Apache-2.0)
@@ -23,13 +22,12 @@ custom). Built for optimization and deep-learning integration.
23
22
  [Linda-Sophie Schneider](https://github.com/Linda-SophieSchneider) at
24
23
  [Linda-SophieSchneider/DiffCT-MLX](https://github.com/Linda-SophieSchneider/DiffCT-MLX).
25
24
 
26
- ## 🧭 Using this branch — the unified `diffct_mlx` API
25
+ ## 🧭 The unified `diffct_mlx` API
27
26
 
28
- This `cuda` branch adds **`diffct_mlx`**, a single API that auto-selects its
27
+ **`diffct_mlx`** is a single API that auto-selects its
29
28
  compute backend at import — **Torch / numba-CUDA** on NVIDIA GPUs, **Apple MLX**
30
29
  on Apple Silicon — so the *same* script runs unchanged on both. It mirrors the
31
- public API of the [DiffCT-MLX `main`](https://github.com/Linda-SophieSchneider/DiffCT-MLX)
32
- (Apple) package name-for-name.
30
+ same public API on both backends name-for-name.
33
31
 
34
32
  ```bash
35
33
  pip install "diffct-mlx[cuda]" # NVIDIA GPUs (Torch + numba-CUDA)
@@ -55,11 +53,11 @@ reco = dct.reconstruct_fbp(
55
53
 
56
54
  **Parity & status.** FBP/FDK, SART/SIRT, TV-/ASD-/AwTV-POCS, DART, phantoms,
57
55
  trajectory generators and the measured-data helpers are all available and were
58
- verified on NVIDIA GPUs. The **MLX backend** is wired in
59
- (`diffct_mlx/backend/metal/` vendors the Metal kernels and `mx.custom_function`
60
- projectors; the `xp` namespace is attribute-identical to the torch one, pinned
61
- by a static parity test) — runtime validation on Apple-Silicon hardware is the
62
- remaining step.
56
+ verified on NVIDIA GPUs. The **MLX backend** is runtime-validated on Apple
57
+ Silicon; its vendored Metal kernels use `mx.custom_function` projectors and
58
+ the same centered Siddon/geometry-gradient conventions as CUDA. The
59
+ backend-neutral `xp` namespace is attribute-identical to the torch one, pinned
60
+ by a static parity test.
63
61
 
64
62
  ### Projectors: Siddon vs. separable footprint
65
63
 
@@ -92,10 +90,49 @@ evaluation**: `cone_backward_footprint(..., indices=idx)` computes only the give
92
90
  flattened `(D, H, W)` voxels and returns a 1-D vector (useful for masked / DART
93
91
  subproblems).
94
92
 
93
+ ### Quantitative FDK (amplitude-true reconstruction)
94
+
95
+ The legacy case FDK path was a *synthetic-geometry calibration*, not physics:
96
+ an unpadded DFT-bin ramp (circular-convolution wrap-around depresses large
97
+ objects with an object-size-dependent bias — down to 0.40× at 75 % FOV
98
+ coverage), the constant `π·SID/(2·SDD·N)` (only valid for `du = dv = voxel
99
+ = 1`), and the Siddon adjoint (not an FDK backprojector). On measured data
100
+ these stack to ~0.1–0.5× of the true attenuation.
101
+
102
+ The quantitative path fixes all three: cosine pre-weights → per-view
103
+ trapezoidal angular weights (actual source angles, non-uniform trajectories
104
+ included) → zero-padded **physical** ramp (`|f|/du`, `pad_factor=2`) → the
105
+ voxel-driven `(SID/U)²`-weighted FDK gather backprojector with its analytic
106
+ `SDD/(2π·SID)` constant. Verified: a cylinder of known µ at real camera
107
+ geometry (du 0.556 mm, voxel 0.278 mm, magnification 2, object filling 75 %
108
+ of the FOV) reconstructs to **0.99×** the true µ (legacy path: 0.47×).
109
+
110
+ ```python
111
+ case = dct.build_measured_cone_3d_case(cfg) # supports_fdk=True on CUDA now
112
+ vol_mu = dct.reconstruct_case_fdk(case) # true attenuation units (1/mm)
113
+ ```
114
+
115
+ All cone cases (synthetic, measured TIFF, npy) carry the quantitative
116
+ operators (`case.fdk_weight` / `fdk_filter` / `fdk_back_project`); sinogram
117
+ units are handled per source (measured −log data are physical; raw Siddon
118
+ projector output is voxel-unit and rescaled by `voxel_spacing`). The ramp
119
+ filter itself now accepts `pad_factor` and `sample_spacing`, and
120
+ `FBPParameters`/`FDKParameters` default to `pad_factor=2`.
121
+
122
+ > **Unit convention.** The projectors integrate in **voxel units**: physical
123
+ > line integrals are `(A @ x) * voxel_spacing`. Operators built via
124
+ > `make_*_operator` carry `A.voxel_spacing` as metadata, and the boundaries
125
+ > where physical units matter handle the conversion automatically —
126
+ > `simulate_scan` rescales before the `exp(-p)` / beam-hardening physics (and
127
+ > returns physical, scanner-like sinograms), and the quantitative FDK path
128
+ > encodes it per data source. One consequence to know: **iterative**
129
+ > reconstruction of measured (physical) data converges to attenuation *per
130
+ > voxel*; divide by `case.voxel_spacing` for µ in 1/mm.
131
+
95
132
  ### Operators, solvers, regularizers, physics & simulation
96
133
 
97
- The `cuda` branch adds a composable, differentiable toolkit on top of the
98
- projectors (all GPU-resident, verified on NVIDIA GPUs):
134
+ The unified package provides a composable, differentiable toolkit on top of
135
+ the projectors (all GPU-resident, verified on NVIDIA GPUs):
99
136
 
100
137
  - **Operators** (`diffct_mlx.operators`) — a `LinearOperator` algebra: build a
101
138
  projector as `A = dct.make_cone_3d_operator(...)`, then `A @ x`, `A.T @ y`,
@@ -165,20 +202,31 @@ sizes, Plug-and-Play priors). Verified by `tests/test_known_operator_gradflow.py
165
202
  when the input carries gradients, so unrolled TV gradient steps are
166
203
  second-order differentiable (mirrors `mx.grad` composability on MLX).
167
204
 
168
- **Trainable geometry (pose/trajectory optimization).** The Torch Siddon
169
- projectors (parallel/fan/cone) also provide gradients w.r.t. their per-view
170
- geometry arrays (`src_pos`, `det_center`/`det_origin`, `det_u_vec`,
171
- `det_v_vec`, `ray_dir`) via finite-difference VJPs: computed only for inputs
172
- with `requires_grad` (the data-only path pays nothing), two forward passes per
173
- geometry component, step size chosen relative to each array's magnitude (works
174
- for mm- and m-scale setups alike). Semantics to know: Siddon ray weights are
175
- piecewise-linear in the geometry, so these are *smoothed subgradients* — exact
176
- against a matched-step contraction (pinned by tests), descent-quality in
177
- practice (see the pose-recovery test), but not second-order differentiable.
178
- On MLX, the cone projector has the equivalent VJP (`src_pos` by default, all
179
- four arrays with `DIFFCT_GEOMETRY_VJP=1`). The **footprint** family stays
180
- data-only, and stochastic simulation ops (`add_poisson_noise`) are not
181
- differentiable.
205
+ **Trainable geometry (pose/trajectory optimization).** All Torch forward
206
+ projectors provide gradients w.r.t. their per-view geometry arrays
207
+ (`src_pos`, `det_center`/`det_origin`, `det_u_vec`, `det_v_vec`, `ray_dir`),
208
+ computed only for inputs with `requires_grad` (the data-only path pays
209
+ nothing):
210
+
211
+ - **Cone Siddon** uses an **analytic geometry kernel** (default): one kernel
212
+ pass yields closed-form gradients for all four geometry arrays — the
213
+ per-ray endpoint derivatives of the trilinearly smoothed line integral,
214
+ validated against an exact torch-autograd reference (cos > 0.998) and ~4×
215
+ faster than finite differences. Set `DIFFCT_GEOMETRY_VJP=fd` to force the
216
+ FD path instead.
217
+ - **Fan/parallel Siddon and all footprint forwards** use finite-difference
218
+ VJPs (two forward passes per geometry component, magnitude-relative step —
219
+ mm- and m-scale setups both work). The operator layer's default
220
+ `projector_mode="footprint"` is therefore geometry-trainable too.
221
+
222
+ Semantics to know: the discrete forwards are piecewise-linear in the
223
+ geometry, so all of these are *smoothed* gradients — descent-quality in
224
+ practice (see the pose-recovery tests), but not second-order differentiable.
225
+ Backprojectors remain data-only, and stochastic simulation ops
226
+ (`add_poisson_noise`) are not differentiable. On MLX, cone Siddon uses the
227
+ same analytic endpoint-gradient formulation by default, including gradients
228
+ for all four geometry arrays. Set `DIFFCT_GEOMETRY_VJP=fd` for the legacy
229
+ finite-difference `src_pos` fallback.
182
230
 
183
231
  **Recommended step-size parametrization for unrolled networks.** A free
184
232
  trainable step can wander into the divergent regime, where positivity clamps
@@ -230,23 +278,18 @@ to ~1e-6, 2-GPU == 1-GPU exact, 2048³ cone-FDK end-to-end with a bounded
230
278
  working set. This path is CUDA-only (it drives the numba-CUDA kernels
231
279
  directly).
232
280
 
233
- ## 🔀 Branches
234
-
235
- ### `main` — unified auto-backend package
236
- The unified `diffct_mlx` package described above: one API, two engines
237
- (Torch/numba-CUDA and MLX/Metal), auto-selected at import. This is the branch
238
- to use on every platform.
281
+ ## 🔀 Branches & releases
239
282
 
240
- ### `cuda` — development line of the unified package
241
- Where the unification work happens before it lands on `main`; after a merge
242
- the two branches are identical.
283
+ All development happens on **`main`** (the former `cuda` development branch
284
+ has been merged and retired). Versioned releases are tagged and published to
285
+ PyPI via CI (trusted publishing); `gh-pages` hosts the built documentation.
243
286
 
244
287
  Kernels take per-view ``(src_pos, det_center, det_u_vec[, det_v_vec])``
245
288
  arrays instead of closed-form ``sdd / sid / beta`` scalars, so **spiral,
246
289
  saddle, sinusoidal, laminography, or any user-supplied trajectory** works
247
290
  without touching the kernels — on both backends.
248
291
 
249
- ⚠️ **Note:** not published to PyPI yet. If you find any bugs please
292
+ If you find any bugs please
250
293
  [raise an issue](https://github.com/Linda-SophieSchneider/DiffCT-MLX/issues).
251
294
 
252
295
  ## ✨ Features
@@ -408,7 +451,7 @@ pip install -e .
408
451
  ### Running the tests
409
452
 
410
453
  ```bash
411
- pytest tests/ -q # 97 tests, ~5 s
454
+ pytest tests/ -q # core suite (backend-specific tests may skip)
412
455
  pytest tests/benchmarks/ --benchmark-only # opt-in perf suite
413
456
  ```
414
457
 
@@ -8,6 +8,11 @@ drop-in replacements. Currently: 2D parallel beam (fan / cone follow).
8
8
  import torch
9
9
 
10
10
  from .constants import _DTYPE
11
+ from .projectors import (
12
+ _FD_REL_EPS_POS,
13
+ _FD_REL_EPS_VEC,
14
+ _fd_geometry_grads,
15
+ )
11
16
  from .utils import (
12
17
  DeviceManager,
13
18
  TorchCUDABridge,
@@ -64,13 +69,14 @@ class ParallelFootprintProjectorFunction(torch.autograd.Function):
64
69
  _DTYPE(detector_spacing), d_ray, d_do, d_du, cx, cy, _DTYPE(voxel_spacing)
65
70
  )
66
71
 
67
- ctx.save_for_backward(ray_dir, det_origin, det_u_vec)
72
+ # image saved for the (optional) finite-difference geometry gradients.
73
+ ctx.save_for_backward(image, ray_dir, det_origin, det_u_vec)
68
74
  ctx.intermediate = (num_detectors, detector_spacing, Ny, Nx, voxel_spacing)
69
75
  return sinogram
70
76
 
71
77
  @staticmethod
72
78
  def backward(ctx, grad_sinogram):
73
- ray_dir, det_origin, det_u_vec = ctx.saved_tensors
79
+ image, ray_dir, det_origin, det_u_vec = ctx.saved_tensors
74
80
  num_detectors, detector_spacing, Ny, Nx, voxel_spacing = ctx.intermediate
75
81
  device = DeviceManager.get_device(grad_sinogram)
76
82
  grad_sinogram = _to_f32_contig(DeviceManager.ensure_device(grad_sinogram, device))
@@ -94,7 +100,21 @@ class ParallelFootprintProjectorFunction(torch.autograd.Function):
94
100
  d_grad_sino, n_views, num_detectors, d_img_grad, Nx, Ny,
95
101
  _DTYPE(detector_spacing), d_ray, d_do, d_du, cx, cy, _DTYPE(voxel_spacing)
96
102
  )
97
- return grad_image, None, None, None, None, None, None
103
+
104
+ grad_ray = grad_do = grad_duv = None
105
+ if any(ctx.needs_input_grad[1:4]):
106
+ def _launch(ray_d, det_o, det_uv):
107
+ with torch.no_grad():
108
+ return ParallelFootprintProjectorFunction.apply(
109
+ image, ray_d, det_o, det_uv,
110
+ num_detectors, detector_spacing, voxel_spacing)
111
+
112
+ grad_ray, grad_do, grad_duv = _fd_geometry_grads(
113
+ _launch, [ray_dir, det_origin, det_u_vec],
114
+ ctx.needs_input_grad[1:4], grad_sinogram,
115
+ [_FD_REL_EPS_VEC, _FD_REL_EPS_POS, _FD_REL_EPS_VEC])
116
+
117
+ return grad_image, grad_ray, grad_do, grad_duv, None, None, None
98
118
 
99
119
 
100
120
  class ParallelFootprintBackprojectorFunction(torch.autograd.Function):
@@ -189,13 +209,14 @@ class FanFootprintProjectorFunction(torch.autograd.Function):
189
209
  _DTYPE(detector_spacing), d_src, d_dc, d_du, cx, cy, _DTYPE(voxel_spacing)
190
210
  )
191
211
 
192
- ctx.save_for_backward(src_pos, det_center, det_u_vec)
212
+ # image saved for the (optional) finite-difference geometry gradients.
213
+ ctx.save_for_backward(image, src_pos, det_center, det_u_vec)
193
214
  ctx.intermediate = (num_detectors, detector_spacing, Ny, Nx, voxel_spacing)
194
215
  return sinogram
195
216
 
196
217
  @staticmethod
197
218
  def backward(ctx, grad_sinogram):
198
- src_pos, det_center, det_u_vec = ctx.saved_tensors
219
+ image, src_pos, det_center, det_u_vec = ctx.saved_tensors
199
220
  num_detectors, detector_spacing, Ny, Nx, voxel_spacing = ctx.intermediate
200
221
  device = DeviceManager.get_device(grad_sinogram)
201
222
  grad_sinogram = _to_f32_contig(DeviceManager.ensure_device(grad_sinogram, device))
@@ -219,7 +240,20 @@ class FanFootprintProjectorFunction(torch.autograd.Function):
219
240
  d_grad_sino, n_views, num_detectors, d_img_grad, Nx, Ny,
220
241
  _DTYPE(detector_spacing), d_src, d_dc, d_du, cx, cy, _DTYPE(voxel_spacing)
221
242
  )
222
- return grad_image, None, None, None, None, None, None
243
+ grad_src = grad_dc = grad_duv = None
244
+ if any(ctx.needs_input_grad[1:4]):
245
+ def _launch(src_p, det_c, det_uv):
246
+ with torch.no_grad():
247
+ return FanFootprintProjectorFunction.apply(
248
+ image, src_p, det_c, det_uv,
249
+ num_detectors, detector_spacing, voxel_spacing)
250
+
251
+ grad_src, grad_dc, grad_duv = _fd_geometry_grads(
252
+ _launch, [src_pos, det_center, det_u_vec],
253
+ ctx.needs_input_grad[1:4], grad_sinogram,
254
+ [_FD_REL_EPS_POS, _FD_REL_EPS_POS, _FD_REL_EPS_VEC])
255
+
256
+ return grad_image, grad_src, grad_dc, grad_duv, None, None, None
223
257
 
224
258
 
225
259
  class FanFootprintBackprojectorFunction(torch.autograd.Function):
@@ -318,13 +352,14 @@ class ConeFootprintProjectorFunction(torch.autograd.Function):
318
352
  cx, cy, cz, _DTYPE(voxel_spacing)
319
353
  )
320
354
 
321
- ctx.save_for_backward(src_pos, det_center, det_u_vec, det_v_vec)
355
+ # volume saved for the (optional) finite-difference geometry gradients.
356
+ ctx.save_for_backward(volume, src_pos, det_center, det_u_vec, det_v_vec)
322
357
  ctx.intermediate = (D, H, W, det_u, det_v, du, dv, voxel_spacing)
323
358
  return sino
324
359
 
325
360
  @staticmethod
326
361
  def backward(ctx, grad_sino):
327
- src_pos, det_center, det_u_vec, det_v_vec = ctx.saved_tensors
362
+ volume, src_pos, det_center, det_u_vec, det_v_vec = ctx.saved_tensors
328
363
  D, H, W, det_u, det_v, du, dv, voxel_spacing = ctx.intermediate
329
364
  device = DeviceManager.get_device(grad_sino)
330
365
  grad_sino = _to_f32_contig(DeviceManager.ensure_device(grad_sino, device))
@@ -352,7 +387,23 @@ class ConeFootprintProjectorFunction(torch.autograd.Function):
352
387
  cx, cy, cz, _DTYPE(voxel_spacing)
353
388
  )
354
389
  grad_volume = grad_vol_perm.permute(2, 1, 0).contiguous() # (D, H, W)
355
- return grad_volume, None, None, None, None, None, None, None, None, None
390
+ grad_src = grad_dc = grad_duv = grad_dvv = None
391
+ if any(ctx.needs_input_grad[1:5]):
392
+ # Finite-difference geometry VJPs of the footprint forward model
393
+ # (the separable footprint is piecewise-smooth in the geometry, so
394
+ # central differences give well-behaved smoothed gradients).
395
+ def _launch(src_p, det_c, det_uv, det_vv):
396
+ with torch.no_grad():
397
+ return ConeFootprintProjectorFunction.apply(
398
+ volume, src_p, det_c, det_uv, det_vv,
399
+ det_u, det_v, du, dv, voxel_spacing)
400
+
401
+ grad_src, grad_dc, grad_duv, grad_dvv = _fd_geometry_grads(
402
+ _launch, [src_pos, det_center, det_u_vec, det_v_vec],
403
+ ctx.needs_input_grad[1:5], grad_sino,
404
+ [_FD_REL_EPS_POS, _FD_REL_EPS_POS, _FD_REL_EPS_VEC, _FD_REL_EPS_VEC])
405
+
406
+ return grad_volume, grad_src, grad_dc, grad_duv, grad_dvv, None, None, None, None, None
356
407
 
357
408
 
358
409
  class ConeFootprintBackprojectorFunction(torch.autograd.Function):
@@ -20,6 +20,7 @@ from .cone_beam import (
20
20
  _cone_3d_forward_kernel,
21
21
  _cone_3d_backward_kernel,
22
22
  _cone_3d_fdk_backproject_kernel,
23
+ _cone_3d_geometry_grad_kernel,
23
24
  )
24
25
 
25
26
  __all__ = [
@@ -32,4 +33,5 @@ __all__ = [
32
33
  '_cone_3d_forward_kernel',
33
34
  '_cone_3d_backward_kernel',
34
35
  '_cone_3d_fdk_backproject_kernel',
36
+ '_cone_3d_geometry_grad_kernel',
35
37
  ]