Elastix manager module
This module takes care of the section to section alignment. It imports libraries that contain the code from the elastix command line tools: https://elastix.lumc.nl/ The libraries are contained within the SimpleITK-SimpleElastix library
- class library.image_manipulation.elastix_manager.ElastixManager(LOGFILE_PATH)
Bases:
library.image_manipulation.file_logger.FileLogger
Class for generating, storing and applying transformations within stack alignment [with the Elastix package] All methods relate to aligning images in stack
- align_elastix(fixed_index, moving_index, use_points=False)
Aligns two images using the Elastix registration algorithm.
- Args:
fixed_index (int): The index of the fixed image. moving_index (int): The index of the moving image. use_points (bool, optional): Whether to use corresponding points for registration. Defaults to False.
- Returns:
tuple: A tuple containing the rotation angle (R), translation in the x-axis (x), translation in the y-axis (y), and the registration metric.
- Raises:
AssertionError: If the number of fixed points does not match the number of moving points.
- align_elastix_with_affine(fixed_index, moving_index)
- align_images(transforms)
function to align a set of images with a with the transformations between them given Note: image alignment is memory intensive (but all images are same size) 6 factor of est. RAM per image for clean/transform needs firmed up but safe
- Parameters
(dict) (transforms) – dictionary of transformations indexed by id of moving sections
- align_section_masks(animal, transforms)
function that can be used to align the masks used for cleaning the image. This not run as part of the pipeline, but is used to create the 3d shell around a certain brain
- Parameters
animal – (str) Animal ID
transforms – (array): 3*3 transformation array
- apply_full_transformations(channel=1)
Calculate and store the rigid transformation using elastix. Align CH3 from CH1
- create_affine_transformations()
- create_dir2dir_transformations()
Calculate and store the rigid transformation using elastix. Align CH3 from CH1 TODO this needs to be fixed to account for the channel variable name
- create_web_friendly_sections()
A function to create section PNG files for the database portal.
- create_within_stack_transformations()
Calculate and store the rigid transformation using elastix. The transformations are calculated from the next image to the previous This is done in a simple loop with no workers. Usually takes up to an hour to run for a stack. It only needs to be run once for each brain.
- static get_metric(logpath)
- get_rotation_center()
return a rotation center for finding the parameters of a transformation from the transformation matrix use channel 1 thumbnail cropped images to find the center
- Return list
list of x and y for rotation center that set as the midpoint of the section that is in the middle of the stack
- get_transformations()
After the elastix job is done, this fetches the rotation, xshift and yshift from the DB
- Parameters
animal – the animal
- Returns
a dictionary of key=filename, value = coordinates
- load_elastix_transformation(animal, moving_index)
loading the elastix transformation from the database
- Parameters
animal – (str) Animal ID
moving_index – (int) index of moving section
- Return array
2*2 roatation matrix, float: x translation, float: y translation
- static parameter_elastix_parameter_file_to_dict(filename)
- start_image_alignment()
- align the full resolution tif images with the transformations provided.
All the sections are aligned to the middle sections, the transformation of a given section to the middle section is the composite of the transformation from the given section through all the intermediate sections to the middle sections.
- Parameters
transforms – (dict): dictionary of transformations that are index by the id of moving sections
- transform_image(img, T)
- transform_save_image(infile, outfile, T)
- update_within_stack_transformations()
Takes the existing transformations and aligned images and improves the transformations Elastix needs the points to be in files so we need to write the data from the DB to the filesystem. Each section will have it’s own point file. We usually create 3 points per section, so there will be a point file containing 3 ponts for each section.