Skip to content

Python proton CT plugin: tigre.plugins.pct.pCTCubicSpline - #986

Merged
AnderBiguri merged 5 commits into
plugins/pct-matlabfrom
plugins/pct-python
Nov 28, 2026
Merged

AnderBiguri merged 5 commits into
plugins/pct-matlabfrom
plugins/pct-python

Conversation

@AnderBiguri

@AnderBiguri AnderBiguri commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

What

The Python version of the proton CT plugin from #985, so proton radiographies work in both languages with the same function. Stacked on #985 (base plugins/pct-matlab); retarget to master once #985 is merged.

  • tigre.plugins.pct.pCTCubicSpline(posIn, posOut, dirIn, dirOut, Wepl, eIn, geo): Cython binding of the shared CUDA code in Common/CUDA/plugins/pCT. Positions and directions as (2, N) (row 0 x, row 1 y) or a 2N vector; geo.dDetector/sDetector in (y, x) like other Python geometry; result indexed [y, x], NaN where no proton crossed. Any numeric input type.
  • Not built by default. TIGRE_PLUGINS=pct pip install . (or all). TIGRE_PCT_PIXEL=0.5 / TIGRE_PCT_INTERCEPTS=10,220,100 set the intercept vector lengths, as Compile('pCT','-pct_pixel',…) does; tigre.plugins.pct.INTERCEPTS reports them. Importing tigre.plugins.pct without the build raises an ImportError saying how to build it. Non-CUDA backends skip the plugin with a message.
  • MATLAB pCTCubicSpline.m: the same function in MATLAB (converts to single, takes N-by-2), used by pRad_demo.m and the MATLAB test. pCTCubicSpline_mex is unchanged.
  • Header: host entry points visible to the C++ compiler (the Cython .cpp includes it); device declarations and the pi/eps macros only under __CUDACC__.
  • Demo Python/demos/pRad_demo.py (needs a source checkout for the data file); docs page with Python/MATLAB tabs; the demo is listed but not run when the site is built.
  • MANIFEST.in/pyproject.toml: plugin sources in the sdist; .gitignore for the generated .cpp.
  • Also (all of Python TIGRE, not only the plugin): mexPrintf in Common/CUDA/TIGRE_common.cpp no longer prefixes every C++/CUDA message with Common/CUDA/TIGRE_common.cpp (9): (the location of its own PRINT_HERE(""), never the caller). This also removes the Python build's zero-length format string warning. Checked in the sandbox: build has no warnings, messages print without the prefix.
  • CUDA fixes (both .cu files): a CUDA error no longer leaves the call's GPU buffers and host histograms allocated (tigre::ReleaseOnError, as in core); cone beam no longer leaks dSourceDist on every call; detector pixel indices use floorf instead of int() truncation, so protons up to one pixel below the first pixel are no longer counted in it; buffer sizes in size_t.
  • CompilePCT.m removed (deprecated in Proton CT becomes a MATLAB plugin (Compile pCT), with input and launch fixes #985); use Compile pCT.

Tests

  • Python/tests/test_pct_plugin.py: the same case as MATLAB/Tests/test_pCT_plugin.m (100 straight protons, 4×4, each pixel its mean WEPL 10+ix+4*iy), which also pins the orientation of the result. Marked gpu, skipped when the plugin is not built. One test.
  • No CI job: hosted runners have no GPU, and the plugin is opt-in.

What was and was not tested

Written by an AI agent (Claude).

  • Sandbox, Linux, nvcc 13.5, no GPU: TIGRE_PLUGINS=pct TIGRE_PCT_PIXEL=0.5 python setup.py build_ext builds and links _pct (intercepts 5/110/100); the module imports; the input checks raise the expected ValueErrors; without a GPU the CUDA error comes back as a RuntimeError instead of crashing. The test is skipped without the build. Docs build with sphinx-build -W.
  • Ander, Windows, Python 3.11 (Anaconda), TIGRE_PLUGINS=pct TIGRE_CUDA_ARCH=native pip install .: builds; pytest Python/tests/test_pct_plugin.py passes; pRad_demo.py runs.
  • Python vs MATLAB on the demo data (217,188 protons, 80×80): same NaN pixels, max abs difference 1.2e-6 (float32 rounding from the GPU's atomic adds), using MATLAB pRad_demo (now through pCTCubicSpline.m) on this branch.
  • The CUDA fixes and the CompilePCT removal came after the checks above: compiled with nvcc 13.5 in the sandbox (no warnings), and re-run by Ander on the GPU (Windows) at 517d282: test_pct_plugin.py and test_pCT_plugin.m pass, Python and MATLAB pRad_demo agree.
  • Not run: Linux with a GPU. (Cone beam is not needed: proton CT here is parallel beam.)

🤖 Generated with Claude Code

https://claude.ai/code/session_01L6n1VHyq4uNorM3CAiBUPo

- Cython binding (Python/tigre/plugins/pct/_pct.pyx) of the shared CUDA code;
  built only with TIGRE_PLUGINS=pct (or all); TIGRE_PCT_PIXEL and
  TIGRE_PCT_INTERCEPTS set the intercept vector lengths like Compile('pCT',...).
  Importing it without the build raises an ImportError saying how to build it.
- pCTCubicSpline(posIn, posOut, dirIn, dirOut, Wepl, eIn, geo): same name and
  arguments as MATLAB; geo detector fields in (y, x) like other Python TIGRE
  geometry; result indexed [y, x].
- MATLAB pCTCubicSpline.m: same function, converts inputs to single and takes
  N-by-2 positions; the demo and test use it.
- Header: host entry points visible to the C++ compiler, device code only for
  nvcc.
- Demo Python/demos/pRad_demo.py, test Python/tests/test_pct_plugin.py (same
  case as the MATLAB test), docs with Python and MATLAB tabs, changelog.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L6n1VHyq4uNorM3CAiBUPo
pytest 9's importorskip skips only ModuleNotFoundError; the plugin raises a
plain ImportError with build instructions, so collection failed in CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L6n1VHyq4uNorM3CAiBUPo
@AnderBiguri
AnderBiguri marked this pull request as ready for review November 28, 2026 17:53
mexPrintf in the Python build called PRINT_HERE(""), which printed the
location of that line in TIGRE_common.cpp, not of the caller, before every
message. Also removes the build's zero-length format string warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L6n1VHyq4uNorM3CAiBUPo
- A CUDA error used to leave every buffer of the call allocated (and the host
  histograms); now released with tigre::ReleaseOnError, so Python can catch
  the error and carry on.
- Cone beam never freed dSourceDist: a small GPU leak on every call.
- Detector pixel indices used int(), which truncates towards zero, so protons
  up to one pixel below the first pixel were counted in it; now floorf.
- Buffer sizes in size_t (2*N*4 bytes overflowed int above 268M protons).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L6n1VHyq4uNorM3CAiBUPo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L6n1VHyq4uNorM3CAiBUPo
@AnderBiguri
AnderBiguri added this pull request to stack #988 November 28, 2026 18:17
@AnderBiguri
AnderBiguri merged commit 3bb0550 into master Nov 28, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants