Step 3 helper functions

Contains a set of functions used to assist with the preprocessing and computation steps in step3_atlas_registration.py

class step3_help.ClassHack(*args, **kwargs)

Bases: object

This class provides methods that my unpickling requires, but doesn’t do anything

delaxes(*args, **kwargs)
pick(*args, **kwargs)
class step3_help.Unpickler(file, *, fix_imports=True, encoding='ASCII', errors='strict', buffers=())

Bases: Unpickler

An unpickler which can ignore old matplotlib figures stored in a dictionary

find_class(module, name)

Return an object from a specified module.

If necessary, the module will be imported. Subclasses may override this method (e.g. to restrict unpickling of arbitrary classes and functions).

This method is called whenever a class or a function object is needed. Both arguments passed are str objects.

step3_help.atlas_to_cp_mask(atlas_vis, region_ids)

Convert an integer-valued segmentation of the mouse brain into a ternary mask (0:background, 1:brain (-CP), 2: CP)

Parameters:

atlas_visarray of ints

An array of size [X,Y,Z] where each element contains the subregion ID of the corresponding voxel in the mouse brain

region_idsarray of ints

An array containing all of the region IDs corresponding to the CP

Returns:

all_non_cparray of ints

An array of size [X,Y,Z] where each element corresponds to the type of data at each voxel in the ternarized mouse brain

step3_help.backup_unpickle(reg_file)

A function used to load the low-to-high registration transform file

Parameters:

reg_file (str) – A file path pointing to the location of a low-to-high registration transform file (.npz)

Returns:

out – A dictionary containing the data stored in reg_file

Return type:

dict

step3_help.compute_all_dice_scores(atlas_vis, manual_vis)

Compute the Dice score between the registered atlas segmentation and a manual anatomical segmentation for the CP and for the whole brain

Parameters:
  • atlas_vis (array of ints) – An array of size [X,Y,Z] where each element corresponds to the type of data at each voxel in the ternarized mouse brain; Registered atlas segmentation

  • manual_vis (array of ints) – An array of size [X,Y,Z] where each element corresponds to the type of data at each voxel in the ternarized mouse brain; Manual anatomical segmentation

Returns:

  • dice_cp (float) – The dice score when comparing the registered atlas segmentation and a manual anatomical segmentation for the CP

  • dice_wb (float) – The dice score when comparing the registered atlas segmentation and a manual anatomical segmentation for the whole brain

step3_help.compute_all_hausdorff_scores(atlas_bound_coords, manual_bound_coords)

Generate a NxM matrix of Euclidean distances corresponding to the Euclidean distances between a set of N points and another set of M points, extract the N+M Hausdorff distances, and return several performance metrics corresponding to the similarity of the N points from atlas_bound_coords and manual_bound_coords.

Parameters:
  • atlas_bound_coords (array of float) – A 3xN array containing the x,y,z coordinates for all of the points defining a regional boundary in the registered mouse brain atlas

  • manual_bound_coords (array of float) – A 3xM array containing the x,y,z coordinates for all of the points defining a regional boundary in the manual anatomical segmentation

Returns:

  • h_dist (float) – The Hausdorff Score; i.e. the maximum of all the Hausdorff distances

  • h_dist_50 (float) – The 50th percentile from all of the Hausdorff distances

  • h_dist_95 (float) – The 95th percentile from all of the Hausdorff distances

  • all_min_dist (array of float) – A [N+M] array of all the Hausdorff distances

step3_help.compute_dice_score(atlas_vis, manual_vis)

Compute the Dice score between the registered atlas segmentation and a manual anatomical segmentation for the CP and for the whole brain

Parameters:
  • atlas_vis (array of ints) – An array of size [X,Y,Z] where each element corresponds to the type of data at each voxel in a binarized mouse brain; Registered atlas segmentation

  • manual_vis (array of ints) – An array of size [X,Y,Z] where each element corresponds to the type of data at each voxel in a binarized mouse brain; Manual anatomical segmentation

  • Returns

  • ========

  • out (float) – The dice score computed between atlas_vis and manual_vis

step3_help.get_target_ids(atlas_labels_path)

Return a list of the region IDs from Yongsoo Kim’s atlas corresponding to all of the subregions within the CP

Parameters:

atlas_labels_path (str) – The file path pointing to the location of a csv file containing all of the region IDs for Yongsoo Kim’s mouse brain atlas

Returns:

region_ids – A list of integers corresponding to the regions within the CP region of the mouse brain

Return type:

list of int

step3_help.get_xyzoff(fname, brain, neuron_dir, skip=0)

Compute the offset in soma coordinates using the .swc file located at fname.

Parameters:
  • fname (str) – A string pointing to the file to load

  • brain (str) – The name of the dataset in which the neuron lies; Used to determine how to compute the offset from the file

  • neuron_dir (str) – The directory in which fname lies; Used to determine how to compute the offset from the file

  • skip (int) – Number of lines to skip at the beginning. (default 0)

Returns:

xyzoff – the x, y, z offsets between fname and first line of the .swc file stored at fname

Return type:

Array of ints

step3_help.load_swc(fname, skip=0)

load an swc file.

Parameters:
  • fname (str) – A string pointing to the file to load

  • skip (int) – Number of lines to skip at the beginning. (default 0)

Returns:

  • V (numpy array of float) – An array storing vertices

  • E (numpy array of int) – An array storing edges. they are ordered as parent -> this sample

  • R (numpy array of float) – An arrow storing radii

step3_help.npz_to_Nifti1(npzPath, outPath, affine, key=None, saveImage=False)

Convert an (.npz) file into a Nifti1 (.nii.gz) file using the provided affine matrix

Parameters:
  • npzPath (str) – A file path pointing to an npz file containing image data

  • outPath (str) – A file path pointing to the desired output location for the converted Nifti1 file

  • affine (List of float) – A 4x4 affine matrix used to initialzie the Nifti1 Image

  • key (str) – Default - None; The key corresponding to the image data stored at ‘npzPath’. If None, use the first key.

  • saveImage (bool) – Default - False; If True, save the Nifti1 file at outPath

step3_help.plot_all_slices(atlas_mask, curr_mask, low_img_path, outdir, outdir_fname)

Plot the integer-valued masks from the registered mouse brain atlas and the manual anatomical segmentation over the corresponding microscopy image.

Parameters:
  • atlas_mask (array of int) – An array of size [X,Y,Z] defining an integer-valued image of the registered mouse brain atlas

  • curr_mask (array of int) – An array of size [X,Y,Z] defining an integer-valued image of manual anatomical segmentation

  • low_img_path (str) – A file path pointing to the location of the corresponding low resolution image used to generate both masks

  • outdir (str) – A file path poining to the directory where the plot should be saved

  • outdir_fname (str)

step3_help.str_to_list(mat)

A function which takes a list formatted as a string and converts it to a list of the correct shape

Parameters:

mat (str) – A string of the form ‘[[a],…[b,c]]’ where each element in the list is a single or tuple consisting of strings containing only alphanumeric characters which correspond to the id of each slice

Returns:

mat – A list containing the elements of the input, now formatted as a list of singles and tuples with alphanumeric strings for each element

Return type:

list

step3_help.ternary_to_boundary(ternary_mask)

Convert a 3D ternarized image into the boundaries between regions 0,1+2 and 0+1,2.

Parameters:

ternary_mask (array of int) – An array of size [X,Y,Z] where each element corresponds to the type of data at each voxel in the ternarized mouse brain

Returns:

  • cp_3d_bound (array of int) – An array of size [X,Y,Z] where each element is either 1 if it is along the boundary of the CP and 0 otherwise

  • wb_3d_bound (array of int) – An array of size [X,Y,Z] where each element is either 1 if it is along the outer boundary of the cortex and 0 otherwise