This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
TPTBox is a Python package for processing BIDS-compliant medical imaging datasets (CT, MRI), with a focus on torso/spine analysis. It provides NIfTI I/O, reorientation/resampling, Points of Interest (POI) computation for vertebrae, 2D/3D snapshots, registration, and segmentation integrations (SPINEPS, nnU-Net).
pip install poetry
poetry install --with dev# All tests
pytest unit_tests/
# Single test file
pytest unit_tests/test_nii.py
# Single test function
pytest unit_tests/test_nii.py::test_function_name
# With coverage
coverage run --source=TPTBox -m pytest unit_tests/# Lint (auto-fix where possible)
ruff check . --fix
# Format
ruff format .
# Both (mirrors pre-commit behavior)
pre-commit run --all-filesflake8 . --count --select=E9,F63,F7,F82 --show-source --statisticsNII (TPTBox/core/nii_wrapper.py) — Central class wrapping nibabel NIfTI images. Handles reorientation, resampling, affine transforms, boolean masking, and mathematical operations. Nearly all image operations in the package go through NII. Math operations live in nii_wrapper_math.py as mixins.
POI (TPTBox/core/poi.py) — Points of Interest container mapping (vertebra_id, subregion_id) → 3D coordinate. Coordinates can be in voxel or world space. POI computation strategies live in TPTBox/core/poi_fun/.
BIDS_FILE / BIDS_Global_info (TPTBox/core/bids_files.py) — BIDS dataset navigator. BIDS_Global_info scans a dataset root, while BIDS_FILE represents a single file parsed according to BIDS naming entities. Constants for BIDS key/value vocabulary are in bids_constants.py.
Location / Vertebra_Instance (TPTBox/core/vert_constants.py) — Enum-like constants for vertebral anatomy (vertebra IDs, subregion labels). Used as keys into POI objects throughout the codebase.
TPTBox/
├── core/ # NII, POI, BIDS parsing, numpy utilities, vertebra constants
│ └── poi_fun/ # POI calculation strategies and serialization
├── spine/ # Spine-specific: 2D snapshots, statistics (distances, angles)
├── segmentation/ # SPINEPS integration, nnU-Net inference, defacing
├── registration/ # ANTs rigid/deformable, DeepALI deep learning registration
├── mesh3D/ # 3D mesh generation and rendering from segmentations
├── stitching/ # Multi-station image stitching
└── logger/ # Logging infrastructure (Logger, Print_Logger, No_Logger)
Public API is re-exported from TPTBox/__init__.py. All major classes and utility functions are importable directly from TPTBox.
NII+POIare used together constantly: compute POIs from segmentationNIIs, then use POIs to guide further processing.BIDS_Global_infoiterates subjects/sessions; each subject has aSubject_ContainerwithBIDS_FILEentries;BIDS_FILE.get_nii()returns aNII.vert_constants.pydefines the shared label space (Locationenum) that ties segmentation labels to POI keys to snapshot rendering.- External tool integrations (SPINEPS, nnU-Net, ANTs, DeepALI) are optional; their imports are guarded so the core works without them.
Tests live in unit_tests/ (not TPTBox/tests/). TPTBox/tests/ contains test utilities and sample data (CT/MRI NIfTIs) used by the unit tests. Some generated test files are very large (>20K LOC) — they are autogenerated and should not be edited by hand.
- Line length: 140 characters
- Formatter: Ruff (Black-compatible, double quotes)
- Target Python: 3.10+ syntax, but the package supports 3.9–3.14
- Naming: Ruff N-rules are largely ignored — mixed-case class/method names are acceptable in this codebase (medical domain conventions)
- Complexity: McCabe max=20; research code legitimately has deep branching
from __future__ import annotationsis used widely for forward references