"""Shared contract of the model-to-model registration classes.
:class:`RegisterModelsBase` holds what every model registration has in common:
the fixed (target) model, the registered model, the two transforms between the
models, and the dictionary a registration returns. Concrete classes implement
the :meth:`RegisterModelsBase.registration_method` hook; the public
:meth:`RegisterModelsBase.register` calls it with the moving model, keeps the
results, and returns them. The same pattern as
:class:`~monai_physio.RegisterImagesBase`.
Configuration follows the library's workflow convention: what defines the
registration (the fixed model and, where needed, its reference grid) is given
to ``__init__``; optional settings have ``set_*`` methods; ``register`` takes
only the moving model, the input that changes from one call to the next.
"""
import logging
from typing import Any, Optional
import itk
import pyvista as pv
from .monai_physio_base import MONAIPhysioBase
from .process_transforms import ProcessTransforms
[docs]
class RegisterModelsBase(MONAIPhysioBase):
"""Base class for registering a moving model to a fixed model.
Subclasses implement :meth:`registration_method`, which returns at least
``registered_model``, ``moving_to_fixed_transform`` and
``fixed_to_moving_transform``. :meth:`register` stores those on the
instance and returns them, with any further keys the hook adds.
Attributes:
fixed_model (Optional[pv.DataSet]): Target model.
moving_model (Optional[pv.DataSet]): Model of the latest registration.
registered_model (Optional[pv.DataSet]): The moving model aligned to the
fixed model.
moving_to_fixed_transform (Optional[itk.Transform]): Result transform
from the moving to the fixed side.
fixed_to_moving_transform (Optional[itk.Transform]): Result transform
from the fixed to the moving side.
transform_tools (ProcessTransforms): Transform utilities.
"""
[docs]
def __init__(
self,
fixed_model: Optional[pv.DataSet],
log_level: int | str = logging.INFO,
) -> None:
"""Initialize the shared registration state.
Args:
fixed_model: Target model the moving model is registered to.
log_level: Logging level. Default: logging.INFO
"""
super().__init__(class_name=self.__class__.__name__, log_level=log_level)
self.fixed_model = fixed_model
self.transform_tools = ProcessTransforms(log_level=log_level)
self.moving_model: Optional[pv.DataSet] = None
self.registered_model: Optional[pv.DataSet] = None
self.moving_to_fixed_transform: Optional[itk.Transform] = None
self.fixed_to_moving_transform: Optional[itk.Transform] = None
def _clear_results(self) -> None:
"""Forget the results of the previous registration."""
self.registered_model = None
self.moving_to_fixed_transform = None
self.fixed_to_moving_transform = None
[docs]
def set_fixed_model(self, fixed_model: pv.DataSet) -> None:
"""Replace the target model and forget the previous results.
Args:
fixed_model: New target model.
"""
self.fixed_model = fixed_model
self._clear_results()
[docs]
def registration_method(self, moving_model: pv.DataSet) -> dict[str, Any]:
"""Register ``moving_model`` to the fixed model; implemented by subclasses.
Args:
moving_model: Model to align to the fixed model.
Returns:
Dictionary with at least ``registered_model``,
``moving_to_fixed_transform`` and ``fixed_to_moving_transform``.
Raises:
NotImplementedError: Always, in the base class.
"""
raise NotImplementedError(
f"{self.__class__.__name__} must implement registration_method()."
)
[docs]
def register(self, moving_model: pv.DataSet) -> dict[str, Any]:
"""Register ``moving_model`` to the fixed model.
Calls :meth:`registration_method` and keeps its standard results on
the instance.
Args:
moving_model: Model to align to the fixed model.
Returns:
The hook's dictionary: ``registered_model``,
``moving_to_fixed_transform``, ``fixed_to_moving_transform`` and
any keys the subclass adds.
"""
self._clear_results()
self.moving_model = moving_model
result = self.registration_method(moving_model)
self.registered_model = result["registered_model"]
self.moving_to_fixed_transform = result["moving_to_fixed_transform"]
self.fixed_to_moving_transform = result["fixed_to_moving_transform"]
return result