magmap.atlas.edge_seg module

Re-segment atlases based on edge detections.

magmap.atlas.edge_seg.edge_aware_segmentation(path_atlas, atlas_profile, show=True, atlas=True, suffix=None, exclude_labels=None, mirror_mult=-1)[source]

Segment an atlas using its previously generated edge map.

Labels may not match their own underlying atlas image well, particularly in the orthogonal directions in which the labels were not constructed. To improve alignment between the labels and the atlas itself, register the labels to an automated, roughly segmented version of the atlas. The goal is to improve the labels’ alignment so that the atlas/labels combination can be used for another form of automated segmentation by registering them to experimental brains via :func:register.

Edge files are assumed to have been generated by :func:make_edge_images.

Parameters:
  • path_atlas (str) – Path to the fixed file, typically the atlas file with stained sections. The corresponding edge and labels files will be loaded based on this path.

  • atlas_profile (AtlasProfile) – Atlas profile.

  • show (bool, default: True) – True if the output images should be displayed; defaults to True.

  • atlas (bool, default: True) – True if the primary image is an atlas, which is assumed to be symmetrical. False if the image is an experimental/sample image, in which case segmentation will be performed on the full images, and stats will not be performed.

  • suffix (Optional[str], default: None) – Modifier to append to end of path_atlas basename for registered image files that were output to a modified name; defaults to None. If atlas is True, suffix will only be applied to saved files, with files still loaded based on the original path.

  • exclude_labels (Optional[DataFrame], default: None) – Sequence of labels to exclude from the segmentation; defaults to None.

  • mirror_mult (int, default: -1) – Multiplier for mirrored labels; defaults to -1 to make mirrored labels the inverse of their source labels.

magmap.atlas.edge_seg.edge_distances(labels, atlas_edge=None, path=None, spacing=None)[source]

Measure the distance between edge images.

Parameters:
  • labels – Labels image as Numpy array.

  • atlas_edge (default: None) – Image as a Numpy array of the atlas reduced to its edges. Defaults to None to load from the corresponding registered file path based on path.

  • path (default: None) – Path from which to load atlas_edge if it is None.

  • spacing (default: None) – Grid spacing sequence of same length as number of image axis dimensions; defaults to None.

Returns:

An image array of the same shape as labels_edge with label edge values replaced by corresponding distance values.

magmap.atlas.edge_seg.erode_labels(labels_img_np, erosion, erosion_frac=None, mirrored=True, mirror_mult=-1)[source]

Erode labels image for use as markers or a map of the interior.

Parameters:
  • labels_img_np (np.ndarray) – Numpy image array of labels in z,y,x format.

  • erosion (dict) – Dictionary of erosion filter settings from profiles.RegKeys to pass to segmenter.labels_to_markers_erosion().

  • erosion_frac (int) – Target erosion fraction; defaults to None.

  • mirrored (bool) – True if the primary image mirrored/symmatrical, in which case erosion will only be performed one symmetric half and mirrored to the other half. If False or no symmetry is found, such as unmirrored atlases or experimental/sample images, erosion will be performed on the full image.

  • mirror_mult (int) – Multiplier for mirrored labels; defaults to -1 to make mirrored labels the inverse of their source labels.

Returns:

The eroded labels as a new array of same shape as that of labels_img_np and a data frame of erosion stats.

Return type:

np.ndarray, pd.DataFrame

magmap.atlas.edge_seg.make_edge_images(path_img, show=True, atlas=True, suffix=None, path_atlas_dir=None)[source]

Make edge-detected atlas and associated labels images.

The atlas is assumed to be a sample (eg microscopy) image on which an edge-detection filter will be applied. The labels image is assumed to be an annotated image whose edges will be found by obtaining the borders of all separate labels. Atlas and labels images can be set in config.reg_suffixes. If the labels image suffix is an empty string (config.reg_suffixes[annotation=""]) and path_atlas_dir is not used, no labels image will be used.

Parameters:
  • path_img – Path to the image atlas. The labels image will be found as a corresponding, registered image, unless path_atlas_dir is given.

  • show (bool) – True if the output images should be displayed; defaults to True.

  • atlas (default: True) – True if the primary image is an atlas, which is assumed to be symmetrical. False if the image is an experimental/sample image, in which case erosion will be performed on the full images, and stats will not be performed.

  • suffix (default: None) – Modifier to append to end of path_img basename for registered image files that were output to a modified name; defaults to None.

  • path_atlas_dir (default: None) – Path to atlas directory to use labels from that directory rather than from labels image registered to path_img, such as when the sample image is registered to an atlas rather than the other way around. Typically coupled with suffix to compare same sample against different labels. Defaults to None, in which case the labels image is loaded from the registered labels image to path_img.

magmap.atlas.edge_seg.make_sub_segmented_labels(img_path, suffix=None)[source]

Divide each label based on anatomical borders to create a sub-segmented image.

The segmented labels image will be loaded, or if not available, the non-segmented labels will be loaded instead.

Parameters:
  • img_path – Path to main image from which registered images will be loaded.

  • suffix (default: None) – Modifier to append to end of img_path basename for registered image files that were output to a modified name; defaults to None.

Returns:

Sub-segmented image as a Numpy array of the same shape as the image at img_path.

magmap.atlas.edge_seg.merge_atlas_segmentations(img_paths, show=True, atlas=True, suffix=None)[source]

Merge atlas segmentations for a list of files as a multiprocessing wrapper for :func:merge_atlas_segmentations, after which edge image post-processing is performed separately since it contains tasks also performed in multiprocessing.

Parameters:
  • img_paths (List[str]) – Sequence of image paths to load.

  • show (bool) – True if the output images should be displayed; defaults to True.

  • atlas (bool) – True if the image is an atlas; defaults to True.

  • suffix (str) – Modifier to append to end of img_path basename for registered image files that were output to a modified name; defaults to None.