Repository navigation
Honor a shifted detector in 2d parallel beam on the CPU backend - #1730
GuoCheng24 wants to merge 3 commits into
Conversation
ASTRA's non-vector 'parallel' geometry is parametrized by a pixel width, a pixel count and the angles, so it cannot represent a detector that is shifted away from the origin. `astra_projection_geometry` built it unconditionally for 2d parallel beam, which meant the offset in the detector partition was dropped without a warning: the projection came out identical to the unshifted one and read as robustness (odlgroupgh-359, open since 2016; ASTRA has supported 'parallel_vec' since >1.8.3). The 'cuda' branch stopped being affected when it moved to 'parallel3d_vec', so the two backends currently disagree: the same geometry gives different physics depending on `astra_impl`, silently. This restores agreement. 'parallel_vec' is used only when the detector is actually off-center, so a centered detector keeps the original code path and its numerical results. If the detector is shifted and ASTRA is too old for 'parallel_vec', a NotImplementedError now says so rather than discarding the shift. Tests: `test_detector_shifts_2d` covers fan beam only, which reaches ASTRA through 'fanflat_vec' and was never affected, so nothing caught this. Adds `test_detector_shifts_2d_parallel`, which asserts the shift reaches the projector at all before checking that the projection is shifted by exactly the expected number of bins, and `test_proj_geom_parallel_2d_shifted_detector` plus a `astra_parallel_2d_geom_to_vec` case in `test_geom_to_vec`. All three fail on the unpatched code.
|
Thanks for the contribution. This seems like it fills a feature that was missing mostly due to oversight, and should be included. We definitely don't want a requested shift to be silently ignored! A couple of details should be considered though:
|
|
Thanks — both points taken, and the second one is now measured rather than argued. The centered-detector special case is gone. You were right to push back; I kept the old path out of caution, not evidence. Measured on ASTRA 2.5.0 (Shepp–Logan 256², 360 angles, 363 bins, detector partition exactly centered),
So: not a performance question at all, and the accuracy question resolves as float32 accumulation order rather than geometry. Regressing the residual on the detector-direction gradient — the shape it would have if the two geometries disagreed by a sub-pixel offset — gives an implied shift of −8.4e-07 pixels (−0.85 nm on a 1.01 mm pixel) with R² = 0.0000, residual mean −6.9e-10, correlation with the gradient −0.005. For scale, a real half-pixel shift changes the sinogram by 1.4e-02, i.e. 340× more than the difference between the two code paths. There is no use case I can construct that needs the old numbers, so The two geometry tests that asserted On the CUDA path and keeping the two backends in one place — I agree, and it looks achievable now: ASTRA 2.5.0's CUDA projector accepts the 2D So |
Drops the centered-detector special case from the first version of this patch, per review. Measured on ASTRA 2.5.0 (Shepp-Logan 256^2, 360 angles, 363 bins, exactly centered detector), 'parallel' against 'parallel_vec': proj type max |delta| / max sinogram mean rel. runtime linear 6.2e-04 5.9e-07 1.02x line 6.5e-04 7.7e-07 1.02x strip 6.2e-04 4.8e-06 1.01x The difference is float32 accumulation order, not geometry: regressing the residual on the detector-direction gradient gives an implied sub-pixel shift of -8.4e-07 pixels (-0.85 nm on a 1.01 mm pixel) with R^2 = 0.0000, residual mean -6.9e-10 and correlation with the gradient -0.005. For scale, a real half-pixel shift changes the sinogram by 1.4e-02, i.e. 340x more. So there is no accuracy or performance reason to keep the old path, and no discontinuity to introduce. If ASTRA is too old for 'parallel_vec', 2d parallel beam now raises NotImplementedError instead of silently dropping any shift.
|
Alright, this looks like very meticulous work - assuming the numbers are correct, which I still need to double-check. The unconditional version in Regarding the Cuda path, yes it's probably better to do that in a separate PR. |
|
Done — On whether the fallback is needed at all, two facts that may let you delete it:
So I am happy either way — the fallback is eleven lines and costs nothing, or it goes and the floor becomes explicit. Your call; I have left it in. The new test reaches that branch by reporting the feature as absent, since no installable ASTRA does. It fails on the cpu backend when the fallback is removed, and asserts the shifted case raises rather than quietly returning a Full Understood on the CUDA path being a separate PR. I will open it with the same before/after treatment: the And please do double-check the numbers; every figure in the previous comment is one command on a stock ASTRA 2.5.0, and I would rather you find a mistake than take my word for it. |
…hift there Per review: use 'parallel_vec' when ASTRA provides it, otherwise fall back to the non-vector 'parallel' geometry, which can only express a centered detector, and raise NotImplementedError rather than flatten a shift it cannot represent. This drops the version floor the unconditional version would have introduced. On the floor itself, in case it makes the fallback unnecessary: odl's own feature table puts 'par2d_vec_geometry' at '>1.8.3', not >=2.0 - the entry cites astra-toolbox#54 and says it landed in the release after 1.8.3, i.e. ASTRA 1.9 (2019). And pyproject.toml already asks for astra-toolbox>=2.4 in the 'all' extra, so nothing odl installs itself can reach the fallback. It is only for a user who brings an ASTRA from 2018 or earlier. The new test reaches that branch by reporting the feature as absent, since no ASTRA anyone would install can: with the fallback removed it fails on the cpu backend, and the shifted case must raise rather than return a 'parallel' geometry.
|
Before opening the CUDA PR I measured what it would cost and how far it reaches, and the second part is larger than the geometry function suggests — so this is a scope question for you rather than a patch. The numbers say the change is safe. Comparing the current path (
The adjoint ratio is identical to six decimals in every case, and forward sums agree to 1.000000, so But it is not a geometry swap. so the 2d branch of And fan beam 2d has the same shape ( That is a bigger change to a core backend than I would push on you unasked, and it moves every 2d CUDA user by 1e-4, so: do you want it as one PR covering parallel and fan beam together, parallel only, or not at all for now? I am happy to do any of the three — the measurement above is the part I wanted you to have either way, and every figure in it is one script on a stock ASTRA 2.5.0. |
Fixes #359.
What goes wrong
For a 2D parallel-beam geometry on
astra_impl='cpu',astra_projection_geometrybuilds ASTRA's non-vector'parallel'geometry, which is parametrized by a pixel width, a pixel count and the angles only. A detector partition that is not centered on the origin therefore loses its offset with no warning: the projection comes out identical to the unshifted one. TheTODO: change to parallel_vec when availableabove that branch predates ASTRA 1.8.3, which addedparallel_vec;astra_projectoralready accepts it.The
'cuda'branch stopped being affected when it moved to'parallel3d_vec', so on current master the two backends disagree on the same geometry object. Measured on master with a Shepp–Logan phantom, 128², 200 angles, 183 bins, detector shifted by 1 and 4 bins (relative L∞ change of the sinogram):astra_cpuastra_cudaskimageWhat this changes
astra_parallel_2d_geom_to_vec, the parallel-beam sibling ofastra_conebeam_2d_geom_to_vec, with the samerot_minus_90convention.'cpu'branch,'parallel_vec'is used only when the detector is actually off-center (|shift| > 1e-6 pixel). A centered detector keeps the existing code path, so nobody's numbers move: with the patch the fulltomosuite goes from 458 passed / 26 skipped to 467 / 28 — exactly the new tests, no existing test changes status. If the detector is shifted and ASTRA is too old forparallel_vec, aNotImplementedErrornow says so instead of dropping the shift.After the patch
astra_cpugives the same 5.500e-01 / 9.804e-01 asastra_cuda; the two agree to 8e-4 at every offset tried, which is the same figure they show at zero offset.Tests
test_detector_shifts_2dexists but usesFanBeamGeometry, which reaches ASTRA through'fanflat_vec'and was never affected, so nothing caught this. Added:test_detector_shifts_2d_parallel(ray_trafo_test): asserts the shift reaches the projector at all — on the unpatched code this fails withassert 0.0 > 0.04, the two sinograms being bit-identical — then that the projection is shifted by exactlykbins and that back-projection is unaffected. Runs onastra_cpu,astra_cudaandskimage(numpy spaces).test_proj_geom_parallel_2d_shifted_detectorand a parallel-2d case intest_geom_to_vec(astra_setup_test).All three fail on the unpatched code and pass with it. Found while building an operator-mismatch test bed on LoDoPaB, where a guard requiring every mismatch axis to change the sinogram reported a centre-of-rotation change of exactly 0.0.