CorpusCallosum.utils.mapping_helpers

CorpusCallosum.utils.mapping_helpers.apply_transform_to_pt(pts, T, inv=False)[source]

Apply homogeneous transformation matrix to points.

Parameters:
ptsnp.ndarray

Point coordinates to transform, shape (3,) or (3, N).

Tnp.ndarray

4x4 homogeneous transformation matrix.

invbool, default=False

If True, applies inverse of transformation.

Returns:
np.ndarray

Transformed point coordinates, shape (3,) or (3, N).

CorpusCallosum.utils.mapping_helpers.apply_transform_to_volume(orig_image, interp_vox2vox, save_vox2ras=None, header=None, output_path=None, output_size=None, order=1)[source]

Apply transformation to a volume and save the result.

Parameters:
orig_imagenibabelImage

Input volume.

interp_vox2voxnp.ndarray

Transformation matrix to apply to the data, this is from input-to-output space.

save_vox2rasAffineMatrix4x4, optional

The vox2ras matrix of the output image, only relevant if output_path is given.

headernibabelHeader, optional

Header for the output image, only relevant if output_path is given, if None will default to orig_image header.

output_pathstr or Path, optional

If output_path is provided, saves the result under this path using the dtype of header (or orig_image).

output_sizenp.ndarray, optional

Size of output volume, uses input size by default None.

orderint, default=1

Order of interpolation.

Returns:
np.ndarray

Transformed volume data of shape output_size and type float.

Notes

Uses scipy.ndimage.affine_transform for the transformation, and inverts vox2vox internally as required by affine_transform.

CorpusCallosum.utils.mapping_helpers.calc_mapping_to_standard_space(orig, ac_coords_3d, pc_coords_3d, orig_fsaverage_vox2vox)[source]

Get transformations to map image to standard space.

Parameters:
orignibabelImage

Original image.

ac_coords_3dnp.ndarray

AC coordinates in 3D space.

pc_coords_3dnp.ndarray

PC coordinates in 3D space.

orig_fsaverage_vox2voxAffineMatrix4x4

Transformation matrix from original to fsaverage space.

Returns:
standardized_to_orig_vox2voxAffineMatrix4x4

The vox2vox transformation matrix from standard space to original space.

ac_coords_standardizedVector3d

AC coordinates in standard space.

pc_coords_standardizedVector3d

PC coordinates in standard space.

ac_coords_origVector3d

AC coordinates in original space.

pc_coords_origVector3d

PC coordinates in original space.

CorpusCallosum.utils.mapping_helpers.correct_nodding(ac_pt, pc_pt)[source]

Calculate rotation matrix to correct head nodding.

Calculates rotation matrix to align AC-PC line with posterior direction, correcting for head nodding based on AC-PC line orientation.

Parameters:
ac_ptVector2d

2D coordinates of the anterior commissure point.

pc_ptVector2d

2D coordinates of the posterior commissure point.

Returns:
RotationMatrix

3x3 rotation matrix to align AC-PC line with posterior direction.

CorpusCallosum.utils.mapping_helpers.make_affine(simpleITKImage)[source]

Create an affine transformation matrix from a SimpleITK image.

Parameters:
simpleITKImagesitk.Image

Input SimpleITK image.

Returns:
np.ndarray

4x4 affine transformation matrix in RAS coordinates.

Notes

The function: 1. Gets affine transform in LPS coordinates 2. Converts to RAS coordinates to match nibabel 3. Returns the final 4x4 transformation matrix

CorpusCallosum.utils.mapping_helpers.make_midplane_affine(orig_affine, slices_to_analyze=1, offset=4)[source]

Create affine transformation matrix for midplane slices.

Parameters:
orig_affineAffineMatrix4x4

Original image affine matrix (4x4).

slices_to_analyzeint, default=1

Number of slices to analyze around midplane.

offsetint, default=4

Additional offset in x direction.

Returns:
AffineMatrix4x4

4x4 affine matrix for midplane slices.

CorpusCallosum.utils.mapping_helpers.map_softlabels_to_orig(cc_fn_softlabels, orig, orig2slab_vox2vox, cc_subseg_midslice=None, orig2midslice_vox2vox=None, orig_space_segmentation_path=None)[source]

Map soft labels back to original image space and apply post-processing.

Parameters:
cc_fn_softlabelsnp.ndarray

Soft label predictions of shape (H, W, D, C=3).

orignibabelImage

Original image.

orig2slab_vox2voxAffineMatrix4x4

The vox2vox transformation matrix from orig to the slab.

cc_subseg_midslicenp.ndarray, optional

Mask for subdividing regions of shape (H, D) (only paired with orig2midslice_vox2vox).

orig2midslice_vox2voxAffineMatrix4x4, optional

The vox2vox transformation matrix from orig to the midslice (only paired with cc_subseg_midslice).

orig_space_segmentation_pathstr or Path, optional

Path to save segmentation in original space.

Returns:
np.ndarray

Final segmentation in original image space.

Notes

The function: 1. Transforms background, cc, and fornix label channels separately. 2. Transform CC subsegmentation from midslice to orig and paint into segmentation if cc_subseg_midslice is passed. 4. Saves result to orig_space_segmentation_path if passed.