transformation_graph_v01 module

class transformation_graph_v01.Graph(adj=[], spaces={})[source]

Bases: object

graph object with nodes and edges representing image spaces and transformations between them.

spaces

Integer keys map to the space name and the domain of the corresponding image space.

Type

dict

adj

Adjacency list. List of dictionaries holding the transforms needed to map between connecting spaces.

Type

list

BFS(src, target, v, pred, dist)[source]

Breadth first search

a modified version of BFS that stores predecessor of each vertex in array pred and its distance from source in array dist

Parameters
  • src (int) – int value given by corresponding src in spaces dict

  • target (int) – int value given by corresponding target in spaces dict

  • v (int) – length of spaces dict

  • pred (list of ints) – stores predecessor of vertex i at pred[i]

  • dist (list of ints) – stores distance (by number of vertices) of vertex i from source vertex

Returns

bool – True if a path from src to target is found and False otherwise

add_edge(transforms, src_space, target_space)[source]
add_space(space_name, x=[])[source]
map_image(src_space, image, target_space, transforms, xy_shift=None, **kwargs)[source]

Map an image from source space to target space.

Parameters
  • src_space (str) – name of source space

  • image (array) –

  • target_space (str) – name of target space

  • transforms (list of emlddmm Transform objects) –

  • xy_shift (torch Tensor) – R^2 vector. Applies a translation in xy for reconstructing in registered space.

  • kwargs (dict) – keword args to be passed to emlddmm interpolation, which will be passed to torch grid sample

Returns

image (array) – transformed image data

Note

The xy_shift optional argument is necessary when reconstructing in registered space or when the origins of the source and target space are far apart, e.g. one defines the origin at bregma and the other at the image center. The problem arises because the xy translation is contained entirely in the 2D affine transforms

map_image_(src_space, image, target_space, transforms, xy_shift=None)[source]

Map an image from source space to target space.

Parameters
  • src_space (str) – name of source space

  • image (array) –

  • target_space (str) – name of target space

  • transforms (list of emlddmm Transform objects) –

  • xy_shift (torch Tensor) – R^2 vector. Applies a translation in xy for reconstructing in registered space.

Returns

image (array) – transformed image data

Note

The xy_shift optional argument is necessary when reconstructing in registered space or when the origins of the source and target space are far apart, e.g. one defines the origin at bregma and the other at the image center. The problem arises because the xy translation is contained entirely in the 2D affine transforms

map_points(src_space, transforms, xy_shift=None, slice_locations=None)[source]

Applies a sequence of transforms to points in source space.

Parameters
  • srs_space (str) – name of the source space

  • transforms (list of emlddmm Transform objects) –

  • xy_shift (torch Tensor) – R^2 vector. Applies a translation in xy for reconstructing in registered space.

Returns

X (torch tensor) – transformed points

Note

The xy_shift optional argument is necessary when reconstructing in registered space or when the origins of the source and target space are far apart, e.g. one defines the origin at bregma and the other at the image center. The problem arises because the xy translation is contained entirely in the 2D affine transforms.

Note

From daniel. Mapping from 2D slices to anything is fine. The shape will always be the 2D slices. This is used when mapping imaging data to a 2D slice. Mapping to a 2D slice is harder. But I think we can do it somehow with nearest neighbor interpolation.

map_points_(src_space, transforms, xy_shift=None)[source]

Applies a sequence of transforms to points in source space. If mapping to an image series, maps to the registered domain.

Parameters
  • srs_space (str) – name of the source space

  • transforms (list of emlddmm Transform objects) –

  • xy_shift (torch Tensor) – R^2 vector. Applies a translation in xy for reconstructing in registered space.

Returns

X (torch tensor) – transformed points

Note

The xy_shift optional argument is necessary when reconstructing in registered space or when the origins of the source and target space are far apart, e.g. one defines the origin at bregma and the other at the image center. The problem arises because the xy translation is contained entirely in the 2D affine transforms.

merge(new_graph)[source]

Merge two graphs

Parameters

new_graph (emlddmm Graph object) –

Returns

graph (emlddmm Graph object) – Current graph merged with the new graph.

shortest_path(src, target)[source]

Find Shortest Path

Finds the shortest path between target and src in the adjacency list and prints its length

Parameters
  • src (int) – src value given by corresponding source in spaces dict

  • target (int) – int value given by corresponding source in spaces dict

Returns

path (list of ints) – path from target to src using integer values of the adjacency list vertices. Integers can be converted to space names by the spaces dict.

Example

>>> adj = [{1: ('outputs/MRI/HIST_REGISTERED_to_MRI/', 'f')},
...        {2: ('outputs/CCF/MRI_to_CCF/', 'f'), 0: ('outputs/MRI/HIST_REGISTERED_to_MRI/', 'b')},
...        {1: ('outputs/CCF/MRI_to_CCF/', 'b')},
...        {}]
>>> path = find_shortest_path(adj, 0, 2, 4)
Shortest path length is: 2
>>> print(path)
[0,1,2]
>>> path = transformation_graph.find_shortest_path(adj, 0, 3, 4)
Given target and source are not connected
transforms(path)[source]

Returns a list of transforms following a path.

Parameters

path (list) – A path as returned by the self.shortest_path function.

Returns

transforms (list) – A list of Transform objects defined in emlddmm.py.

transformation_graph_v01.graph_reconstruct(graph, out, I, target_space, target_fnames=[])[source]

Apply Transformation

Applies affine matrix and velocity field transforms to map source points to target points. Saves displacement field from source points to target points (i.e. difference between transformed coordinates and input coordinates), and determinant of Jacobian for 3d source spaces. Also saves transformed image in vtk format.

Parameters
  • graph (emlddmm Graph object) –

  • out (str) – path to registration outputs parent directory

  • I (emlddmm Image) –

  • target_space (str) – name of the space to which image I will be transformed.

  • target_fnames (list) – list of file names; only necessary if target is a series of 2d slices.

Todo

Check why the registered space histology is not working. (march 27, 2023) I think the issue is that there is actually no time to do it. If I say to reconstruct one space to itself, then it says not connected and gives an error. There needs to be another way.

transformation_graph_v01.graph_reconstruct_(graph, out, I, target_space, target_fnames=[])[source]

Apply Transformation

Applies affine matrix and velocity field transforms to map source points to target points. Saves displacement field from source points to target points (i.e. difference between transformed coordinates and input coordinates), and determinant of Jacobian for 3d source spaces. Also saves transformed image in vtk format.

Parameters
  • graph (emlddmm Graph object) –

  • out (str) – path to registration outputs parent directory

  • I (emlddmm Image) –

  • target_space (str) – name of the space to which image I will be transformed.

  • target_fnames (list) – list of file names; only necessary if target is a series of 2d slices.

Todo

Check why the registered space histology is not working. (march 27, 2023) I think the issue is that there is actually no time to do it. If I say to reconstruct one space to itself, then it says not connected and gives an error. There needs to be another way.

transformation_graph_v01.main()[source]

Main

Main function for parsing input arguments, calculating registrations and applying transformations.

Example

$ python transformation_graph.py –infile GDMInput.json

transformation_graph_v01.run_registrations(reg_list)[source]

Run Registrations

Runs a sequence of registrations given by reg_list. Saves transforms, qc images, reconstructed images, displacement fields, and determinant of Jacobian of displacements. Also builds and writes out the transform graph.

Parameters

reg_list (list of dicts) – each dict in reg_list specifies the source image path, target image path, source and target space names, registration configuration settings, and output directory.

Returns

reg_graph (emlddmm graph)

Example

>>> reg_list = [{'registration':[['CCF','average_template_50'],['MRI','masked']],
                 'source': '/path/to/average_template_50.vtk',
                 'target': '/path/to/HR_NIHxCSHL_50um_14T_M1_masked.vtk',
                 'config': '/path/to/configMD816_MR_to_CCF.json',
                 'output': 'outputs/example_output'},
                {'registration':[['MRI','masked'], ['HIST','Nissl']],
                 'source': '/path/to/HR_NIHxCSHL_50um_14T_M1_masked.vtk',
                 'target': '/path/to/MD816_STIF',
                 'config': '/path/to/configMD816_Nissl_to_MR.json',
                 'output': 'outputs/example_output'}]
>>> run_registrations(reg_list)