Workflow Development Guide

Workflow classes coordinate multiple processing steps behind a stable Python API and, where useful, an installed CLI command.

Current Workflow Mapping

CLI command

Workflow class

monai-physio-convert-image-to-usd

monai_physio.WorkflowConvertImageToUSD

monai-physio-convert-image-to-vtk

monai_physio.WorkflowConvertImageToVTK

monai-physio-convert-vtk-to-usd

monai_physio.WorkflowConvertVTKToUSD

monai-physio-create-statistical-model

monai_physio.WorkflowCreateStatisticalModel

monai-physio-fit-statistical-model-to-patient

monai_physio.WorkflowFitStatisticalModelToPatient

monai-physio-reconstruct-4d

monai_physio.WorkflowReconstruct4D

monai-physio-train-physicsnemo

monai_physio.WorkflowTrainPhysicsNeMo

monai-physio-infer-physicsnemo

monai_physio.WorkflowInferPhysicsNeMo

monai-physio-convert-image-4d-to-3d

monai_physio.ConvertImage4DTo3D (a converter, not a workflow)

monai-physio-download-data

monai_physio.DownloadData (a utility, not a workflow)

monai-physio-visualize-pca-modes

Reads a pca_model.json directly; no workflow class

That is all eleven installed commands. Two workflow classes have no CLI wrapper: monai_physio.WorkflowFinetuneICONRegistration and monai_physio.WorkflowEvaluateMovement.

Time-series memory

After WorkflowInferMovement.set_return_transforms(False), process_time_series writes the same images and meshes while releasing dense transforms and working image buffers after each stage. The default True retains all transforms for callers that need them. Both modes return output paths and stage_meshes for evaluation and USD animation, and report progress across the complete series. Memory for these meshes still grows with the number of stages; dense-field memory in the False mode depends on the size of one stage’s working grid.

Use the original CT as reference_image even when fitting used a smaller grid. Repeated one-stage calls are unnecessary when only saved outputs are needed.

Workflow Example

from pathlib import Path

import itk

from monai_physio import RegisterImagesICON, WorkflowConvertImageToUSD

frame_files = sorted(Path("data/Slicer-Heart-CT").glob("slice_???.mha"))
time_series_images = [itk.imread(str(path)) for path in frame_files]

workflow = WorkflowConvertImageToUSD(
    registration_method=RegisterImagesICON(),
)

results = workflow.process(
    time_series_images=time_series_images,
    reference_image=time_series_images[0],
    output_directory="./results",
    usd_project_name="patient_001",
)

Selecting a fitting grid

WorkflowFitStatisticalModelToPatient accepts optional constructor arguments fitting_crop_margin_mm and fitting_spacing_mm. Cropping happens before isotropic resampling, avoiding a full-volume fine-grid allocation. The crop uses the nonzero patient labelmap, which must share the input image grid. Provide an anatomy-specific labelmap so neighboring organs do not contribute to PCA fitting.

For an initial lung configuration to validate on your data:

fit = WorkflowFitStatisticalModelToPatient(
    template_model=mean_lung_model,
)
fit.set_fitting_crop_margin_mm(40.0)
fit.set_fitting_spacing_mm(1.5)
fit.prepare_patient(
    patient_models=[patient_lung_surface],
    patient_image=ct,
    patient_labelmap=lung_labelmap,
)

These values are examples, not heart or lung defaults. Omitting both options preserves existing behavior. ProcessImages.crop_to_mask is also available independently; it retains surrounding tissue and pixel type, unlike the labelmap-only crop_to_nonzero with its added zero padding.

With either option enabled, patient labels are sampled by nearest neighbor on the fitting grid before constructing distance maps. Label IDs and interior-label exclusions retain their meanings. Registration grids include patient and aligned template surfaces plus physical registration margins, including when a crop touches the input boundary. The source labelmap is retained and sampled directly onto expanded registration grids, preserving anatomy outside the CT extent. Intermediate template labelmaps also use expanded grids, allowing later registration stages to move anatomy back inside the crop without losing labels. Meshes remain in patient coordinates. The original CT remains available as fit.original_patient_image for full-resolution motion warping; fitting stages, including intensity refinement, use fit.patient_image.

Coarser sampling can remove small structures and change PCA coefficients and motion. Compare lung and heart surface errors, coefficients, and predicted motion against the full-resolution configuration before selecting a grid for a new dataset. Agreement with another fit is not accuracy against ground truth.

Adding a Workflow

  1. Inherit from monai_physio.MONAIPhysioBase.

  2. Keep the constructor explicit and typed.

  3. Use self.log_info() and self.log_debug() for runtime status.

  4. Keep file I/O behavior predictable and documented. A workflow that returns its result, such as the aorta flow workflows, does no file I/O itself; a visualize_* class, such as monai_physio.aorta.AortaVisualizeFlow, writes the result’s files and plots.

  5. Add a CLI wrapper only when the workflow is useful from the command line.

  6. Add focused tests using synthetic data where possible.

  7. Run graphify update . after public API changes - methods added, modified, or removed.

Risk Areas

Changes at the ITK-to-PyVista boundary, time-series transform direction, or LPS-to-USD-Y-up coordinate conversion are high risk and should include focused tests plus visual or metadata validation.

See Also