Extending MONAI Physio

MONAI Physio is a beta toolkit for creating personalized physiological digital twins. Prefer small, explicit extensions that match the current class boundaries over large compatibility layers.

Where to Start

  • Workflows: add or modify complete pipelines.

  • CLI wrappers: expose workflow classes for repeatable command-line use.

  • Utilities: add focused image, contour, transform, or USD helpers.

  • Experiments: keep exploratory research in experiments/. Experiments document prior and ongoing research used to define the toolkit, but they are not examples for users or developers.

Runtime Class Pattern

Workflows, and the registration classes they compose, split their configuration three ways:

  • ``__init__`` takes what defines the instance or stays the same across sequential runs: the segmenter or registrar to use, a trained model, a template, a target.

  • ``set_<name>(value) -> None`` setters take the optional settings. Each validates its value and raises ValueError for a bad one; __init__ assigns the default. A constructor argument that also has a setter is routed through it, so the validation lives in one place.

  • ``process()`` (or register()) takes only the inputs specific to one run: the image, mesh or manifest to process and where to write the results.

import logging

from monai_physio import MONAIPhysioBase

class MyWorkflow(MONAIPhysioBase):
    def __init__(self, segmenter: Segmenter, log_level: int | str = logging.INFO):
        super().__init__(class_name=self.__class__.__name__, log_level=log_level)
        self.segmenter = segmenter
        self.smoothing_iterations = 30

    def set_smoothing_iterations(self, smoothing_iterations: int) -> None:
        if smoothing_iterations < 0:
            raise ValueError("smoothing_iterations must be >= 0")
        self.smoothing_iterations = smoothing_iterations

    def process(self, input_image: itk.Image) -> dict[str, Any]:
        self.log_info("Segmenting with %d smoothing iterations", self.smoothing_iterations)
        return {"labelmap": self.segmenter.segment(input_image)["labelmap"]}

CLI Wrapper Pattern

import argparse
import sys

from my_module import MyWorkflow

def main() -> int:
    parser = argparse.ArgumentParser(description="Run my workflow")
    parser.add_argument("--input-file", required=True)
    args = parser.parse_args()

    workflow = MyWorkflow(input_file=args.input_file)
    workflow.process()
    return 0

if __name__ == "__main__":
    sys.exit(main())

Documentation Requirements

  • Update docstrings for changed public methods.

  • Update or add examples that use the actual current API.

  • Run graphify update . after public API changes to refresh the knowledge graph (see AI Assistants).

  • Avoid documenting planned APIs as if they are installed.

Testing Requirements

Prefer real data: request the session fixtures (test_directories, download_test_data, test_images), which download it on first use. Real data exercises the preprocessing, resampling, dtype and world-frame paths that synthetic toy volumes bypass. Use synthetic ITK images or small PyVista meshes only for a pure unit, such as axis arithmetic, or when real data would push the test into a slow, GPU or Simpleware bucket it does not belong in. Mark tests that need a GPU, a slow runtime, or a licensed Simpleware install with requires_gpu, slow, or requires_simpleware.

py -m pytest tests/ -v

See Also