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

Bases: object

Class for generating, storing and applying transformations within stack alignment [with the Elastix package] All methods relate to aligning images in stack

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_images_elastix(fixed_index: str, moving_index: str) tuple[float, float, float, float]

Aligns two images using the Elastix registration algorithm with GPU acceleration. expected to replace ‘align_elastix’ (TESTING) with gpu realign took 171.49 seconds. without gpu realign took 1 hour(s) and 46 minute(s). with gpu realign took 1 hour(s) and 46 minute(s).

Args:

fixed_index (str due to filename extraction): The index of the fixed image. moving_index (str due to filename extraction): The index of the moving image. iteration (int): if > 0, we are re-aligning

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_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

cleanup_fiducials()
create_affine_transformations()
create_fiducial_points()

Yanks the fiducial points from the database and writes them to a file

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. If cuda and GPU is available, we will use it, otherwise don’t. Home computers may not have a GPU

get_alignment_status()

Determines the alignment status of image files for a given channel and downsample level.

The method checks the existence of directories and counts the number of files in the ‘aligned’ and ‘realigned’ directories. It compares these counts with the expected counts retrieved from the SQL controller to determine which input directory to use for neuroglancer.

Returns:
str: The alignment status, either ‘ALIGNED’ or ‘REALIGNED’. If neither condition is met,

returns None.

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(iteration)

After the elastix job is done, this fetches the rotation, xshift and yshift from the DB If it is full resolution, it will fetch both iterations and combine them. :param animal: the animal :return: a dictionary of key=filename, value = coordinates

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 #THIS ARGUMENT NOT ACTUALLY PASSED TO METHOD

transform_image(img, T)
transform_save_image(infile, outfile, T)