MotionCorrect ======================================== .. py:class:: petpal.preproc.motion_corr.MotionCorrect(image_loader: Optional[petpal.io.image.ImageLoader] = None, table_saver: Optional[petpal.io.table.TableSaver] = None) Bases: :py:obj:`petpal.preproc.register.RegisterBase` Run windowed motion correction on an image and save the result. :ivar image_loader: :func:`~petpal.io.image.ImageLoader` instance or injectable replacement :ivar table_saver: :func:`~petpal.io.table.TableSaver` instance or injectable replacement :ivar input_img: (ants.ANTsImage) Dynamic PET image :ivar target_img: (ants.ANTsImage) Static target image :ivar scan_timing: :func:`~petpal.utils.scan_timing.ScanTimingInfo` Dynamic PET scan timing. :ivar half_life: (float) Half life of the PET tracer in seconds. :ivar: reg_kwargs: (dict) Keyword arguments passed on to :py:func:`~ants.registration` .. py:method:: window_index_pairs(window_duration: float = 300) -> numpy.ndarray The pair of indices corresponding to each window in the image. :param window_duration: Scan will be divided into windows of this duration in seconds. :type window_duration: float :returns: *window_index_pairs (np.ndarray)* -- Array of start and end frame indices for each window. .. seealso:: - :meth:`~petpal.utils.scan_timing.get_window_index_pairs_from_durations` .. py:method:: window_target_img(start_index: int, end_index: int) -> ants.ANTsImage Calculates the sum over frames in the target image within the provided time window. :param start_index: Index for the frame that the window begins on. :type start_index: int :param end_index: Index for the frame that the window ends on. :type end_index: int :returns: *window_img (ants.ANTsImage)* -- Sum of frames in the input image between `start_index` and `end_index`. .. py:method:: ants_xfm_to_rigid_pars(ants_xfm: ants.ANTsTransform) :staticmethod: Convert an ants transform object to six parameters (3 translation, 3 rotation) and the center reference point. .. py:method:: register_windows(window_duration: float = 300, transform_type: str = 'DenseRigid') -> list[ants.ANTsTransform] Run motion correction on the input image to the target image. Creates "windows" by summing over frames with total length equal to `window_duration` and registering the window to the target image. Returns the calculated transforms for each frame. :param window_duration: Duration of each window to sum over. :type window_duration: float :returns: *window_xfm_stack (list[ants.ANTsTransform])* -- The transform to apply to each frame calculated based on the window the frame is in. .. py:method:: apply_motion_correction(frame_xfms: list[ants.ANTsTransform]) -> ants.ANTsImage Apply transforms to input image. :param frame_xfms: Transforms for each frame in the input image. :type frame_xfms: list[ants.ANTsTransform] :returns: *moco_img (ants.ANTsImage)* -- Motion corrected dynamic PET image. .. py:method:: plot_motion(frame_xfm_pars: pandas.DataFrame, out_plot_path: str) Plot the six motion parameters in rigid transformations over time ('motionogram'). :param frame_xfm_pars: The six motion parameters for each frame, as well as the center coordinates. :type frame_xfm_pars: pd.DataFrame :param out_plot_path: Path to where motion plot is saved, typically a .png file. :type out_plot_path: str .. py:method:: save_xfm_parameters(frame_xfms: list[ants.ANTsTransform], filename: str, transform_type: str) Save frame transform parameters as a table. :param frame_xfms: Rigid transform parameters ordered as rotation, translation, centerpoint, then X, Y, Z axis, totalling 9 parameters for each frame. :type frame_xfms: np.ndarray :param filename: Path to where table will be saved, including extension. :type filename: str :raises ValueError: If transform type does not containt 'Rigid'. Saving transform parameters is currently only available for rigid transforms. .. py:method:: __call__(input_image_path: str, out_image_path: str, motion_target_path: str, window_duration: float = 300, transform_type: str = 'DenseRigid', **reg_kwargs) -> ants.ANTsImage Motion correct a dynamic PET image. Divides image into segments of duration in seconds `window_duration` and register each frame to a target image, using the same transformation on for every frame in each window. :param input_image_path: Path to dynamic PET image. :type input_image_path: str :param out_image_path: Path to which motion corrected image is saved. :type out_image_path: str :param motion_target_path: Path to motion target image, a static target representing the dynamic PET image. See :py:func:`~petpal.preproc.motion_target.determine_motion_target`. :type motion_target_path: str :param transform_type: Type of transform used in ants.registration. See https://antspyx.readthedocs.io/en/latest/registration.html. Default DenseRigid. :type transform_type: str :param window_duration: Duration of each window in seconds. Default 300. :type window_duration: float :param reg_kwargs: Keyword arguments to pass on to the registration function. See :py:func:`~ants.registration`. :type reg_kwargs: keyword arguments :returns: *moco_img (ants.ANTsImage)* -- Motion corrected dynamic PET image. .. py:property:: default_reg_kwargs :type: dict Default registration arguments passed on to :py:func:`~ants.registration`. .. py:method:: set_reg_kwargs(**reg_kwargs) Modify the registration arguments passed on to :py:func:`~ants.registration`. .. py:method:: set_input_scan_properties(input_image_path: str) Load input image and get half life and scan timing. Set as MotionCorrect attributes. :param input_image_path: Path to dynamic PET image. :type input_image_path: str .. py:method:: set_target_img(motion_target_path: str) Get the motion target, load it as an image, and set as an attribute. :param input_image_path: Path to dynamic PET image. :type input_image_path: str :param motion_target_option: Option for motion target. See :meth:`~petpal.preproc.motion_target.determine_motion_target.` for details. :type motion_target_option: str | tuple