Save transforms and models as batch

Hi

I have a series of models, indexed from 1 to 10.
I use the model registration module to register model 1 to all the subsequent models.
Then I want to save the transformation matrices as .mat files from 1-10.

Could someone pls let me know how to create a loop within the python console

  1. To apply model registration to each model (fixed=model 01, moving =2:10)
  2. Generate transformation matrix for each iteration
  3. save each matrix as . mat file.

Thanks very much

Here is what ChatGPT-3.5 suggests, looks at least reasonable and may get you started:

import os
import slicer
from slicer.util import NodeModifyTransform

# Set up the file path for saving the .mat files
output_dir = "/path/to/output/directory/"

# Set the fixed model to model 1
fixed_model = slicer.util.getNode("Model_01")

# Loop over the moving models (models 2-10)
for i in range(2, 11):
    # Set the moving model
    moving_model = slicer.util.getNode("Model_{:02d}".format(i))
    
    # Set up the model registration node
    reg_node = slicer.vtkMRMLLinearTransformNode()
    slicer.mrmlScene.AddNode(reg_node)
    reg_node.SetName("Registration_{:02d}".format(i))
    
    # Apply model registration
    logic = slicer.modules.modelregistration.logic()
    logic.SetModeToRigid()
    logic.RegisterModels(fixed_model, moving_model, reg_node)
    
    # Save the transformation matrix as a .mat file
    output_path = os.path.join(output_dir, "Transformation_{:02d}.mat".format(i))
    NodeModifyTransform.SaveMatrixToMatFile(reg_node, output_path)

Thanks ever so much

I tried this,

Always get
Traceback (most recent call last):
File “”, line 1, in
AttributeError: ‘SlicerBaseLogicPython.vtkSlicerScriptedLoadableMod’ object has no attribute ‘registerModel’

I have tried ‘registerModel’ and ‘registerModels’ both
Gives the same error

Any thought please

Thanks very much

Hi Melanie, here is what GPT-4 suggests. I can not test the code right now, but I hope it brings you closer to achieving your goal.

Me:
asking your question.

GPT-4:
This code assumes that you’ve already loaded your models and their respective nodes are available in the scene.

Before running the code snippet, make sure you’ve installed and enabled the SlicerElastix extension, as it provides the necessary registration tools.

import os

# Set the output directory for the transformation matrices
output_dir = 'path/to/your/output/directory'
fixed_node = slicer.util.getNode('model_01')

for i in range(2, 11):
    moving_node = slicer.util.getNode(f'model_{i:02d}')

    # Set up the registration parameters
    parameter_set = slicer.vtkMRMLScriptedModuleNode()
    slicer.mrmlScene.AddNode(parameter_set)
    parameter_set.SetName(f'Registration_Parameters_{i:02d}')
    parameter_set.SetParameter('fixed', fixed_node.GetID())
    parameter_set.SetParameter('moving', moving_node.GetID())

    # Perform the registration
    elastix_logic = slicer.modules.elastix.logic()
    elastix_logic.RegisterVolumes(parameter_set)

    # Get the output transformation node
    transform_node_id = parameter_set.GetParameter('outputTransform')
    transform_node = slicer.mrmlScene.GetNodeByID(transform_node_id)

    # Save the transformation matrix as a .mat file
    output_filename = os.path.join(output_dir, f'transformation_{i:02d}.mat')
    slicer.util.saveNode(transform_node, output_filename)

    # Remove the parameter set and transformation nodes from the scene
    slicer.mrmlScene.RemoveNode(transform_node)
    slicer.mrmlScene.RemoveNode(parameter_set)

This code snippet performs the following steps:

  1. Sets the output directory for the transformation matrices.
  2. Loops through the models from 2 to 10.
  3. Sets up the registration parameters.
  4. Performs the registration using the Elastix registration module.
  5. Retrieves the output transformation node.
  6. Saves the transformation matrix as a .mat file.
  7. Removes the parameter set and transformation nodes from the scene to keep it clean.

Don’t forget to replace 'path/to/your/output/directory' with the actual path where you want to save the transformation matrices.

Thaks so much,

I will trial it

Thanks again