|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +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). |
| 8 | + |
| 9 | +## Commands |
| 10 | + |
| 11 | +### Installation |
| 12 | +```bash |
| 13 | +pip install poetry |
| 14 | +poetry install --with dev |
| 15 | +``` |
| 16 | + |
| 17 | +### Running Tests |
| 18 | +```bash |
| 19 | +# All tests |
| 20 | +pytest unit_tests/ |
| 21 | + |
| 22 | +# Single test file |
| 23 | +pytest unit_tests/test_nii.py |
| 24 | + |
| 25 | +# Single test function |
| 26 | +pytest unit_tests/test_nii.py::test_function_name |
| 27 | + |
| 28 | +# With coverage |
| 29 | +coverage run --source=TPTBox -m pytest unit_tests/ |
| 30 | +``` |
| 31 | + |
| 32 | +### Linting & Formatting |
| 33 | +```bash |
| 34 | +# Lint (auto-fix where possible) |
| 35 | +ruff check . --fix |
| 36 | + |
| 37 | +# Format |
| 38 | +ruff format . |
| 39 | + |
| 40 | +# Both (mirrors pre-commit behavior) |
| 41 | +pre-commit run --all-files |
| 42 | +``` |
| 43 | + |
| 44 | +### CI Linting (as run in GitHub Actions) |
| 45 | +```bash |
| 46 | +flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics |
| 47 | +``` |
| 48 | + |
| 49 | +## Architecture |
| 50 | + |
| 51 | +### Core Abstractions |
| 52 | + |
| 53 | +**`NII`** (`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. |
| 54 | + |
| 55 | +**`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/`. |
| 56 | + |
| 57 | +**`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`. |
| 58 | + |
| 59 | +**`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. |
| 60 | + |
| 61 | +### Package Layout |
| 62 | + |
| 63 | +``` |
| 64 | +TPTBox/ |
| 65 | +├── core/ # NII, POI, BIDS parsing, numpy utilities, vertebra constants |
| 66 | +│ └── poi_fun/ # POI calculation strategies and serialization |
| 67 | +├── spine/ # Spine-specific: 2D snapshots, statistics (distances, angles) |
| 68 | +├── segmentation/ # SPINEPS integration, nnU-Net inference, defacing |
| 69 | +├── registration/ # ANTs rigid/deformable, DeepALI deep learning registration |
| 70 | +├── mesh3D/ # 3D mesh generation and rendering from segmentations |
| 71 | +├── stitching/ # Multi-station image stitching |
| 72 | +└── logger/ # Logging infrastructure (Logger, Print_Logger, No_Logger) |
| 73 | +``` |
| 74 | + |
| 75 | +Public API is re-exported from `TPTBox/__init__.py`. All major classes and utility functions are importable directly from `TPTBox`. |
| 76 | + |
| 77 | +### Key Relationships |
| 78 | + |
| 79 | +- `NII` + `POI` are used together constantly: compute POIs from segmentation `NII`s, then use POIs to guide further processing. |
| 80 | +- `BIDS_Global_info` iterates subjects/sessions; each subject has a `Subject_Container` with `BIDS_FILE` entries; `BIDS_FILE.get_nii()` returns a `NII`. |
| 81 | +- `vert_constants.py` defines the shared label space (`Location` enum) that ties segmentation labels to POI keys to snapshot rendering. |
| 82 | +- External tool integrations (SPINEPS, nnU-Net, ANTs, DeepALI) are optional; their imports are guarded so the core works without them. |
| 83 | + |
| 84 | +### Test Layout |
| 85 | + |
| 86 | +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. |
| 87 | + |
| 88 | +## Code Style |
| 89 | + |
| 90 | +- **Line length**: 140 characters |
| 91 | +- **Formatter**: Ruff (Black-compatible, double quotes) |
| 92 | +- **Target Python**: 3.10+ syntax, but the package supports 3.9–3.14 |
| 93 | +- **Naming**: Ruff N-rules are largely ignored — mixed-case class/method names are acceptable in this codebase (medical domain conventions) |
| 94 | +- **Complexity**: McCabe max=20; research code legitimately has deep branching |
| 95 | +- `from __future__ import annotations` is used widely for forward references |
0 commit comments