Skip to content

Commit 8e57cb9

Browse files
authored
ENH: Refactor naming and add statistical model CLIs (#27)
* ENH: Refactor naming and add statistical model CLIs - Rename heart_model_to_patient → fit_statistical_model_to_patient (workflow, CLI, docs: register_heart_model_to_patient → fit_statistical_model_to_patient) - Rename segment_chest_base → segment_anatomy_base - Add create_statistical_model CLI and workflow (+ docs) - Add visualize_pca_modes CLI - Update docs, API references, and dependent modules * ENH: Enable clipping top of heart when using Simpleware * ENH: Initial version of trimming heart models to base rep. * BUG: Address copilot and mypy (import-not-found) errors
1 parent a291bbf commit 8e57cb9

42 files changed

Lines changed: 2296 additions & 1202 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,14 @@ print(f"PhysioMotion4D version: {physiomotion4d.__version__}")
8888

8989
- **Workflow Classes**: Complete end-to-end pipeline processors
9090
- `WorkflowConvertHeartGatedCTToUSD`: Heart-gated CT to USD processing workflow
91-
- `WorkflowRegisterHeartModelToPatient`: Model-to-patient registration workflow
91+
- `WorkflowCreateStatisticalModel`: Create PCA statistical shape model from sample meshes
92+
- `WorkflowFitStatisticalModelToPatient`: Model-to-patient registration workflow
9293
- **Segmentation Classes**: Multiple AI-based chest segmentation implementations
9394
- `SegmentChestTotalSegmentator`: TotalSegmentator-based segmentation
9495
- `SegmentChestVista3D`: VISTA-3D model-based segmentation
9596
- `SegmentChestVista3DNIM`: NVIDIA NIM version of VISTA-3D
9697
- `SegmentChestEnsemble`: Ensemble segmentation combining multiple methods
97-
- `SegmentChestBase`: Base class for custom segmentation methods
98+
- `SegmentAnatomyBase`: Base class for custom segmentation methods
9899
- **Registration Classes**: Multiple registration methods for different use cases
99100
- Image-to-Image Registration:
100101
- `RegisterImagesICON`: Deep learning-based registration using Icon algorithm
@@ -156,21 +157,42 @@ physiomotion4d-heart-gated-ct cardiac.nrrd \
156157

157158
For Python API usage and advanced customization, see the examples below or refer to the CLI implementation in `src/physiomotion4d/cli/`.
158159

160+
#### Create Statistical Model
161+
162+
Build a PCA statistical shape model from sample meshes aligned to a reference:
163+
164+
```bash
165+
# From a directory of sample meshes
166+
physiomotion4d-create-statistical-model \
167+
--sample-meshes-dir ./input_meshes \
168+
--reference-mesh average_mesh.vtk \
169+
--output-dir ./pca_output
170+
171+
# With custom PCA components
172+
physiomotion4d-create-statistical-model \
173+
--sample-meshes-dir ./meshes \
174+
--reference-mesh average_mesh.vtk \
175+
--output-dir ./pca_output \
176+
--pca-components 20
177+
```
178+
179+
Outputs: `pca_mean_surface.vtp`, `pca_mean.vtu` (if reference is volumetric), and `pca_model.json`.
180+
159181
#### Heart Model to Patient Registration
160182

161183
Register a generic heart model to patient-specific data:
162184

163185
```bash
164186
# Basic registration
165-
physiomotion4d-register-heart-model \
187+
physiomotion4d-fit-statistical-model-to-patient \
166188
--template-model heart_model.vtu \
167189
--template-labelmap heart_labelmap.nii.gz \
168190
--patient-models lv.vtp rv.vtp myo.vtp \
169191
--patient-image patient_ct.nii.gz \
170192
--output-dir ./results
171193

172194
# With PCA shape fitting
173-
physiomotion4d-register-heart-model \
195+
physiomotion4d-fit-statistical-model-to-patient \
174196
--template-model heart_model.vtu \
175197
--template-labelmap heart_labelmap.nii.gz \
176198
--patient-models lv.vtp rv.vtp myo.vtp \
@@ -203,7 +225,7 @@ final_usd = processor.process()
203225
### Python API - Model to Patient Registration
204226

205227
```python
206-
from physiomotion4d import WorkflowRegisterHeartModelToPatient
228+
from physiomotion4d import WorkflowFitStatisticalModelToPatient
207229
import pyvista as pv
208230
import itk
209231

@@ -213,7 +235,7 @@ patient_surfaces = [pv.read("lv.stl"), pv.read("rv.stl")]
213235
reference_image = itk.imread("patient_ct.nii.gz")
214236

215237
# Initialize and run workflow
216-
workflow = WorkflowRegisterHeartModelToPatient(
238+
workflow = WorkflowFitStatisticalModelToPatient(
217239
moving_mesh=model_mesh,
218240
fixed_meshes=patient_surfaces,
219241
fixed_image=reference_image
@@ -352,13 +374,13 @@ PhysioMotion4D provides standardized logging through the `PhysioMotion4DBase` cl
352374

353375
```python
354376
import logging
355-
from physiomotion4d import WorkflowRegisterHeartModelToPatient, PhysioMotion4DBase
377+
from physiomotion4d import WorkflowFitStatisticalModelToPatient, PhysioMotion4DBase
356378

357379
# Control logging level globally for all classes
358380
PhysioMotion4DBase.set_log_level(logging.DEBUG)
359381

360382
# Or filter to show logs from specific classes only
361-
PhysioMotion4DBase.set_log_classes(["WorkflowRegisterHeartModelToPatient", "RegisterModelsPCA"])
383+
PhysioMotion4DBase.set_log_classes(["WorkflowFitStatisticalModelToPatient", "RegisterModelsPCA"])
362384

363385
# Show all classes again
364386
PhysioMotion4DBase.set_log_all_classes()
@@ -436,7 +458,7 @@ Advanced registration between generic anatomical models and patient-specific dat
436458
- **`heart_model_to_model_registration_pca.ipynb`**: PCA-based statistical shape model registration
437459
- **`heart_model_to_patient.ipynb`**: Complete model-to-patient registration workflow
438460

439-
Uses the `WorkflowRegisterHeartModelToPatient` class for three-stage registration:
461+
Uses the `WorkflowFitStatisticalModelToPatient` class for three-stage registration:
440462
1. ICP-based rough alignment
441463
2. Mask-to-mask deformable registration
442464
3. Optional PCA-constrained shape fitting

docs/api/index.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ By Category
7676

7777
**Workflows**
7878
* :class:`~physiomotion4d.WorkflowConvertHeartGatedCTToUSD` - Heart CT to USD
79-
* :class:`~physiomotion4d.WorkflowRegisterHeartModelToPatient` - Heart model registration
79+
* :class:`~physiomotion4d.WorkflowCreateStatisticalModel` - Create PCA statistical shape model
80+
* :class:`~physiomotion4d.WorkflowFitStatisticalModelToPatient` - Heart model registration
8081

8182
**Segmentation**
82-
* :class:`~physiomotion4d.SegmentChestBase` - Base segmentation class
83+
* :class:`~physiomotion4d.SegmentAnatomyBase` - Base segmentation class
8384
* :class:`~physiomotion4d.SegmentChestTotalSegmentator` - TotalSegmentator
8485
* :class:`~physiomotion4d.SegmentChestVista3D` - VISTA-3D model
8586
* :class:`~physiomotion4d.SegmentChestVista3DNIM` - VISTA-3D NIM

docs/api/segmentation/base.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Abstract base class for all segmentation methods.
99
Class Reference
1010
===============
1111

12-
.. autoclass:: SegmentChestBase
12+
.. autoclass:: SegmentAnatomyBase
1313
:members:
1414
:undoc-members:
1515
:show-inheritance:
@@ -18,7 +18,7 @@ Class Reference
1818
Overview
1919
========
2020

21-
:class:`SegmentChestBase` provides the foundation for all segmentation implementations in PhysioMotion4D. It defines the common interface and shared functionality that all segmentation methods must implement.
21+
:class:`SegmentAnatomyBase` provides the foundation for all segmentation implementations in PhysioMotion4D. It defines the common interface and shared functionality that all segmentation methods must implement.
2222

2323
**Key Responsibilities**:
2424
* Define standard segmentation interface
@@ -87,17 +87,17 @@ These methods are provided by the base class:
8787
Creating Custom Segmentation Classes
8888
=====================================
8989

90-
To create a new segmentation method, inherit from :class:`SegmentChestBase`:
90+
To create a new segmentation method, inherit from :class:`SegmentAnatomyBase`:
9191

9292
Basic Implementation
9393
--------------------
9494

9595
.. code-block:: python
9696
97-
from physiomotion4d import SegmentChestBase
97+
from physiomotion4d import SegmentAnatomyBase
9898
import numpy as np
9999
100-
class CustomSegmentator(SegmentChestBase):
100+
class CustomSegmentator(SegmentAnatomyBase):
101101
"""Custom segmentation implementation."""
102102
103103
def __init__(self, param1=None, verbose=False):
@@ -150,7 +150,7 @@ With Custom Post-Processing
150150

151151
.. code-block:: python
152152
153-
class CustomSegmentator(SegmentChestBase):
153+
class CustomSegmentator(SegmentAnatomyBase):
154154
"""Segmentator with custom post-processing."""
155155
156156
def post_process(self, labelmap):
@@ -243,7 +243,7 @@ Validate that required structures are present:
243243

244244
.. code-block:: python
245245
246-
class ValidatingSegmentator(SegmentChestBase):
246+
class ValidatingSegmentator(SegmentAnatomyBase):
247247
"""Segmentator with validation."""
248248
249249
def segment(self, image_path):
@@ -268,7 +268,7 @@ Track segmentation progress for long operations:
268268

269269
.. code-block:: python
270270
271-
class ProgressSegmentator(SegmentChestBase):
271+
class ProgressSegmentator(SegmentAnatomyBase):
272272
"""Segmentator with progress tracking."""
273273
274274
def segment(self, image_path):

docs/api/segmentation/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ PhysioMotion4D supports multiple segmentation approaches:
1616
* **VISTA-3D NIM**: NVIDIA Inference Microservice version
1717
* **Ensemble**: Combine multiple methods for improved accuracy
1818

19-
All segmentation classes inherit from :class:`SegmentChestBase` and provide consistent interfaces.
19+
All segmentation classes inherit from :class:`SegmentAnatomyBase` and provide consistent interfaces.
2020

2121
Quick Links
2222
===========

docs/api/workflows.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Heart Gated CT to USD
4949
Heart Model to Patient Registration
5050
------------------------------------
5151

52-
.. autoclass:: WorkflowRegisterHeartModelToPatient
52+
.. autoclass:: WorkflowFitStatisticalModelToPatient
5353
:members:
5454
:undoc-members:
5555
:show-inheritance:
@@ -67,9 +67,9 @@ Heart Model to Patient Registration
6767

6868
.. code-block:: python
6969
70-
from physiomotion4d import WorkflowRegisterHeartModelToPatient
70+
from physiomotion4d import WorkflowFitStatisticalModelToPatient
7171
72-
workflow = WorkflowRegisterHeartModelToPatient(
72+
workflow = WorkflowFitStatisticalModelToPatient(
7373
model_file="heart_template.vtk",
7474
patient_image="patient_ct.nrrd",
7575
output_directory="./registration_results",
@@ -221,7 +221,7 @@ Combine multiple workflows:
221221
heart_result = heart_workflow.process()
222222
223223
# Second workflow: Register model
224-
registration_workflow = WorkflowRegisterHeartModelToPatient(
224+
registration_workflow = WorkflowFitStatisticalModelToPatient(
225225
model_file=heart_result['model'],
226226
patient_image=patient_ct,
227227
output_directory="./registration_output"

docs/architecture.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Core Components
3131

3232
2. **Segmentation Module**
3333

34-
* Base class: :class:`SegmentChestBase`
34+
* Base class: :class:`SegmentAnatomyBase`
3535
* Implementations: TotalSegmentator, VISTA-3D, Ensemble
3636

3737
3. **Registration Module**
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
====================================
2+
Create Statistical Model
3+
====================================
4+
5+
Overview
6+
========
7+
8+
The ``physiomotion4d-create-statistical-model`` command-line tool builds a PCA
9+
(Principal Component Analysis) statistical shape model from a sample of meshes
10+
aligned to a reference mesh. This mirrors the pipeline in the
11+
Heart-Create_Statistical_Model experiment notebooks.
12+
13+
The workflow:
14+
15+
1. **Extract surfaces** from sample and reference meshes
16+
2. **ICP alignment**: Affine align each sample surface to the reference surface
17+
3. **Deformable registration**: ANTs SyN to establish dense correspondence
18+
4. **Correspondence**: Build aligned shapes with reference topology
19+
5. **PCA**: Compute mean shape and principal components
20+
21+
Outputs written to the output directory:
22+
23+
* ``pca_mean_surface.vtp`` — Mean shape as a surface (PolyData)
24+
* ``pca_mean.vtu`` — Reference volume mesh in mean space (only if reference is volumetric)
25+
* ``pca_model.json`` — PCA model (eigenvalues, components) for use with
26+
:class:`physiomotion4d.WorkflowFitStatisticalModelToPatient` or
27+
:class:`physiomotion4d.RegisterModelsPCA`
28+
29+
Installation
30+
============
31+
32+
The script is installed with PhysioMotion4D:
33+
34+
.. code-block:: bash
35+
36+
pip install physiomotion4d
37+
38+
Quick Start
39+
===========
40+
41+
Basic Usage
42+
-----------
43+
44+
Create a PCA model from a directory of sample meshes and a reference mesh:
45+
46+
.. code-block:: bash
47+
48+
physiomotion4d-create-statistical-model \
49+
--sample-meshes-dir ./input_meshes \
50+
--reference-mesh average_mesh.vtk \
51+
--output-dir ./pca_output
52+
53+
Explicit Sample List
54+
--------------------
55+
56+
Provide sample mesh paths explicitly instead of a directory:
57+
58+
.. code-block:: bash
59+
60+
physiomotion4d-create-statistical-model \
61+
--sample-meshes 01.vtk 02.vtk 03.vtu 04.vtp \
62+
--reference-mesh average_mesh.vtk \
63+
--output-dir ./pca_output
64+
65+
With Custom Parameters
66+
----------------------
67+
68+
.. code-block:: bash
69+
70+
physiomotion4d-create-statistical-model \
71+
--sample-meshes-dir ./meshes \
72+
--reference-mesh average_mesh.vtk \
73+
--output-dir ./pca_output \
74+
--pca-components 20
75+
76+
Command-Line Arguments
77+
======================
78+
79+
Required Arguments
80+
------------------
81+
82+
``--sample-meshes-dir DIR`` or ``--sample-meshes PATH [PATH ...]``
83+
Either a directory containing sample mesh files (``.vtk``, ``.vtu``, ``.vtp``)
84+
or a list of paths to sample meshes. One of these is required.
85+
86+
``--reference-mesh PATH``
87+
Path to the reference mesh. Its surface is used as the alignment target for
88+
all samples.
89+
90+
``--output-dir DIR``
91+
Output directory. Writes ``pca_mean_surface.vtp``, ``pca_mean.vtu`` (if
92+
reference is volumetric), and ``pca_model.json``.
93+
94+
Optional Arguments
95+
------------------
96+
97+
``--pca-components N``
98+
Number of PCA components to retain (default: 15).
99+
100+
See :class:`physiomotion4d.WorkflowCreateStatisticalModel` for the full API and
101+
additional parameters (e.g. ``reference_spatial_resolution``,
102+
``reference_buffer_factor``) that can be exposed in future CLI versions.

docs/cli_scripts/heart_model_to_patient.rst renamed to docs/cli_scripts/fit_statistical_model_to_patient.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Heart Model to Patient Registration
55
Overview
66
========
77

8-
The ``physiomotion4d-register-heart-model`` command-line tool registers generic anatomical heart models to patient-specific imaging data and surface models. This workflow enables:
8+
The ``physiomotion4d-fit-statistical-model-to-patient`` command-line tool registers generic anatomical heart models to patient-specific imaging data and surface models. This workflow enables:
99

1010
* Patient-specific anatomical modeling from generic templates
1111
* Multi-stage registration combining ICP, PCA, and deformable methods
@@ -38,7 +38,7 @@ Register a generic heart model to patient data:
3838

3939
.. code-block:: bash
4040
41-
physiomotion4d-register-heart-model \
41+
physiomotion4d-fit-statistical-model-to-patient \
4242
--template-model heart_model.vtu \
4343
--template-labelmap heart_labelmap.nii.gz \
4444
--patient-models lv.vtp rv.vtp myo.vtp \
@@ -52,7 +52,7 @@ Include statistical shape model fitting:
5252

5353
.. code-block:: bash
5454
55-
physiomotion4d-register-heart-model \
55+
physiomotion4d-fit-statistical-model-to-patient \
5656
--template-model heart_model.vtu \
5757
--template-labelmap heart_labelmap.nii.gz \
5858
--patient-models lv.vtp rv.vtp myo.vtp \
@@ -82,7 +82,7 @@ Required Arguments
8282
``--output-dir DIR``
8383
Output directory for results
8484

85-
See :class:`physiomotion4d.WorkflowRegisterHeartModelToPatient` for API documentation.
85+
See :class:`physiomotion4d.WorkflowFitStatisticalModelToPatient` for API documentation.
8686

8787
Template Labelmap Configuration
8888
--------------------------------
@@ -141,7 +141,7 @@ Example 1: Basic Registration
141141

142142
.. code-block:: bash
143143
144-
physiomotion4d-register-heart-model \
144+
physiomotion4d-fit-statistical-model-to-patient \
145145
--template-model heart_model.vtu \
146146
--template-labelmap heart_labelmap.nii.gz \
147147
--patient-models lv.vtp rv.vtp myo.vtp \
@@ -153,7 +153,7 @@ Example 2: PCA-Based Registration
153153

154154
.. code-block:: bash
155155
156-
physiomotion4d-register-heart-model \
156+
physiomotion4d-fit-statistical-model-to-patient \
157157
--template-model heart_model.vtu \
158158
--template-labelmap heart_labelmap.nii.gz \
159159
--patient-models lv.vtp rv.vtp \

0 commit comments

Comments
 (0)