magmap.io.export_regions module

Region labels export to data frames and CSV files.

Convert regions from ontology files or atlases to data frames.

magmap.io.export_regions.export_common_labels(img_paths, output_path)[source]

Export data frame combining all label IDs from the given atlases, showing the presence of labels in each atlas.

Parameters:
  • img_paths – Image paths from which to load the corresponding labels images.

  • output_path – Path to export data frame to .csv.

Returns:

Data frame with label IDs as indices, column for each atlas, and cells where 1 indicates that the given atlas has the corresponding label.

magmap.io.export_regions.export_region_ids(labels_ref_lookup, path, level=None, drawn_labels_only=False)[source]

Export region IDs from annotation reference reverse mapped dictionary.

Parameters:
  • labels_ref_lookup (Dict) – The labels reference lookup, assumed to be an OrderedDict generated by ontology.create_reverse_lookup() to look up by ID while preserving key order to ensure that parents of any child will be reached prior to the child.

  • path (str) – Path to output CSV file; if does not end with .csv, it will be added.

  • level (Optional[int], default: None) – Level at which to find parent for each label. If None, each the immediate parent will be retrieved for each label.

  • drawn_labels_only (bool, default: False) – True to export only the drawn labels for atlas labels in the same folder as labels_ref_lookup. The RGB values used for the currently loaded atlas will also be shown in a corresponding Excel file (requires openpyxl and Jinja2 packages). If False, the full set of labels in labels_ref_lookup is used.

Return type:

DataFrame

Returns:

Pandas data frame of the region IDs and corresponding names.

magmap.io.export_regions.export_region_network(labels_ref_lookup, path)[source]

Export region network file showing relationships among regions according to the SIF specification.

See http://manual.cytoscape.org/en/stable/Supported_Network_File_Formats.html#sif-format for file format information.

Parameters:
  • labels_ref_lookup – The labels reference lookup, assumed to be an OrderedDict generated by ontology.create_reverse_lookup() to look up by ID while preserving key order to ensure that parents of any child will be reached prior to the child.

  • path – Path to output SIF file; if does not end with .sif, it will be added.

magmap.io.export_regions.make_density_image(img_path, scale=None, shape=None, suffix=None, labels_img_sitk=None, channel=None, matches=None, atlas_profile=None, include=None)[source]

Make a density image based on associated blobs.

Uses the size and/or resolutions of the original image stored in the blobs if available to determine scaling between the blobs and the output image. Otherwise, attempts to load the original image or at least its metadata. The voxel sizes for the blobs is determined by giving an output image or shape.

If matches is given, a heat map will be generated for each set of channels given in the dictionary. Otherwise, if the loaded blobs file has intensity-based colocalizations, a heat map will be generated for each combination of channels.

Parameters:
  • img_path (str) – Path to image, which will be used to indentify the blobs file.

  • scale (Optional[float], default: None) – Scaling factor between the blobs’ space and the output space; defaults to None to use the register. Scaling is found by magmap.np_io.find_scaling().

  • shape (Optional[Sequence[int]], default: None) – Output shape. Defaults to None, in which case the shape will match labels_img_sitk.

  • suffix (Optional[str], 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.

  • labels_img_sitk (Optional[Image], default: None) – Labels image. Defaults to None, in which case a registered labels image will be loaded.

  • channel (Optional[Sequence[int]], default: None) – Sequence of channels to include in density image. For multiple channels, blobs from all these channels are combined into one heatmap. Defaults to None to use all channels.

  • matches (Optional[Dict[Tuple[int, int], BlobMatch]], default: None) – Dictionary of channel combinations to blob matches; defaults to None.

  • atlas_profile (Optional[AtlasProfile], default: None) – Atlas profile, used for scaling; defaults to None.

  • include (Optional[Sequence[int]], default: None) – Sequence of blob confirmed flags to include; defaults to None, in which case all flags will be included.

Return type:

Tuple[ndarray, str]

Returns:

Tuple of the density image as a Numpy array in the same shape as the opened image and the original and img_path to track such as for multiprocessing.

magmap.io.export_regions.make_density_images_mp(img_paths, scale=None, shape=None, suffix=None, channel=None)[source]

Make density images for a list of files as a multiprocessing wrapper for :func:make_density_image

Parameters:
  • img_paths (List[str]) – Sequence of image paths, which will be used to indentify the blob files.

  • scale (int, float) – Rescaling factor as a scalar value. If set, the corresponding image for this factor will be opened. If None, the full size image will be used. Defaults to None.

  • shape (List[int]) – Sequence of target shape defining the voxels for the density map; defaults to None.

  • 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.

  • channel (List[int]) – Sequence of channels to include in density image; defaults to None to use all channels.

magmap.io.export_regions.make_labels_level_img(img_path, level, prefix=None, show=False)[source]

Replace labels in an image with their parents at the given level.

Parents will be determined from the labels ontology file, which will be loaded from the image’s labels metadata if available. This file can also be set in :config:`load_labels`. Labels that do not fall within a parent at that level will remain in place.

Also generates edge images.

Parameters:
  • img_path (Optional[str]) – Path to the base image from which the corresponding registered image will be found. Can be None, where the globally set up image stored in magmap.settings.config will be used instead. If so, prefix must be given to specify the output path.

  • level (int) – Ontological level at which to group child labels.

  • prefix (Optional[str], default: None) – Start of path for output image; defaults to None to use img_path instead.

  • show (bool, default: False) – True to show the images after generating them; defaults to False.

Return type:

Dict[str, Image]

Returns:

Dictionary of registered image suffix to SimpleITK image.

Raises:

ValueError

magmap.io.export_regions.map_metric_to_labels_img(img_path, df_path, meas, fn_avg, prefix=None, show=False, level=None, meas_path_name=None, col_wt=None)[source]

Replace labels in an image with metric values.

Can calculate the differences in metrics for each given region between two conditions or simply show a single set of metric values.

Parameters:
  • img_path (str) – Path to the base image from which the corresponding registered image will be found.

  • df_path (str) – Path to data frame with metrics for the labels.

  • meas (str) – Name of colum in data frame with the chosen measurement.

  • fn_avg (Callable) – Function to apply to the set of measurements, such as a mean. Can be None if df_path points to a stats file from which to extract metrics directly in :meth:vols.map_meas_to_labels.

  • prefix (Optional[str], default: None) – Start of path for output image; defaults to None to use img_path instead.

  • show (bool, default: False) – True to show the images after generating them.

  • level (Optional[int], default: None) – Ontological level at which to look up and show labels. Assume that labels level image corresponding to this value has already been generated by :meth:make_labels_level_img. Defaults to None to use only drawn labels.

  • meas_path_name (Optional[str], default: None) – Name to use in place of meas in output path.

  • col_wt (str) – Name of column to use for weighting.