BrainLine

Data Helper Functions

Apply Ilastik and Validate Models

class brainlit.BrainLine.ApplyIlastik(ilastk_path: str, project_path: str, brains_path: str, brains: list)

Applies ilastik to subvolumes for the purpose of validating machine learning algorithms.

Parameters:
  • ilastk_path (str) -- Path to ilastik executable.

  • project_path (str) -- Path to ilastik project.

  • brains_path (str) -- Path to directory that contains brain samples subdirectories.

  • brains (list) -- List of brain sample names.

ilastk_path

Path to ilastik executable.

Type:

str

project_path

Path to ilastik project.

Type:

str

brains_path

Path to directory that contains brain samples subdirectories.

Type:

str

brains

List of brain sample names.

Type:

list

move_results(self)

Move results from process_subvols to a new subfolder.

process_subvols(self)

Apply ilastik to all validation subvolumes of the specified brain ids in the specified directory

brainlit.BrainLine.plot_results(data_dir: str, brain_ids: list, object_type: str, positive_channel: int, doubles: list = [], show_plot: bool = True)

Plot precision recall curve for a specified brain.

Parameters:
  • data_dir (str) -- Path to directory where brain subvolumes are stored.

  • brain_id (str) -- Brain id to examine (brain2paths key from _data.py file).

  • object_type (str) -- soma or axon, the type of data to examine.

  • positive_channel (int) -- Channel that represents neuron in the predictions.

  • doubles (list, optional) -- Filenames of soma subvolumes that contain two somas, if applicable. Defaults to [].

  • show_plot (bool, optional) -- Whether to run pyplot, useful for pytests when figures should not be displayed. Defaults to True.

Raises:

ValueError -- _description_

Returns:

Best f-score across all thresholds. float: Threshold that yields the best validation f-score.

Return type:

float

brainlit.BrainLine.examine_threshold(data_dir: str, brain_id: str, threshold: float, object_type: str, positive_channel: int, doubles: list = [], show_plot: bool = True)

Display results in napari of all subvolumes that were below some performance threshold, at a given threshold.

Parameters:
  • data_dir (str) -- Path to directory where brain subvolumes are stored.

  • brain_id (str) -- Brain ID to examine (from _data.py file).

  • threshold (float) -- Threshold to examine.

  • object_type (str) -- soma or axon, the data type being examined.

  • positive_channel (int) -- 0 or 1, Channel that represents neuron in the predictions.

  • doubles (list, optional) -- Filenames of soma subvolumes that contain two somas, if applicable. Defaults to [].

  • show_plot (bool, optional) -- Whether to run napari, useful for pytests when figures should not be displayed. Defaults to True.

Raises:
  • ValueError -- If object_type is neither axon nor soma

  • ValueError -- If positive_channel is not 0 or 1.

class brainlit.BrainLine.ApplyIlastik_LargeImage(ilastik_path: str, ilastik_project: str, ncpu: int, data_file: str, results_dir: Union[str, Path] = None)

Apply ilastik to large image, where chunking is necessary.

Parameters:
  • ilastk_path (str) -- Path to ilastik executable.

  • ilastik_project (str) -- Path to ilastik project.

  • ncpu (int) -- Number of cpus to use for applying ilastik in parallel.

  • object_type (str) -- Soma for soma detection or axon for axon segmentaiton.

  • results_dir -- (str or Path): For soma detection, the directory to write detection results.

ilastk_path

Path to ilastik executable.

Type:

str

ilastik_project

Path to ilastik project.

Type:

str

ncpu

Number of cpus to use for applying ilastik in parallel.

Type:

int

object_type

Soma for soma detection or axon for axon segmentaiton.

Type:

str

results_dir

(Path): For soma detection, the directory to write detection results.

apply_ilastik_parallel(self, brain_id: str, layer_names: list, threshold: float, data_dir: str, chunk_size: list, max_coords: list = [- 1, - 1, - 1], min_coords: list = [- 1, - 1, - 1])

Apply ilastik to large brain, in parallel.

Parameters:
  • brain_id (str) -- Brain ID (key in brain2paths in _data.py file).

  • layer_names (list) -- Precomputed layer names to be appended to the base path.

  • threshold (float) -- Threshold for the ilastik predictor.

  • data_dir (str or Path) -- Path to directory where downloaded data will be temporarily stored.

  • chunk_size (list) -- Size of chunks to be used for parallel application of ilastik.

  • max_coords (list, optional) -- Upper bound of bounding box on which to apply ilastk (i.e. does not apply ilastik beyond these bounds). Defaults to [-1, -1, -1].

  • min_coords (list, optional) -- Lower bound of bounding box on which to apply ilastk (i.e. does not apply ilastik beyond these bounds). Defaults to [-1, -1, -1].

collect_axon_results(self, brain_id: str, ng_layer_name: str)

Generate neuroglancer link with the axon_mask segmentation. Intended for use after apply_ilastik_parallel

Parameters:
  • brain_id (str) -- ID to process.

  • ng_layer_name (str) -- Name of neuroglancer layer in val_info URL with image data.

collect_soma_results(self, brain_id: str)

Combine all soma detections and post to neuroglancer. Intended for use after apply_ilastik_parallel.

Parameters:

brain_id (str) -- ID to process.

Results Analysis

class brainlit.BrainLine.SomaDistribution(brain_ids: list, data_file: str, show_plots: bool = True)

Object to generate various analysis images for results from a set of brain IDs. An implementation of BrainDistribution class.

Parameters:
  • brain_ids (list) -- List of brain IDs (keys of data json file).

  • data_files (str) -- Path to json file that contains information about samples.

  • show_plots (bool) -- Whether to show plots, only works if you have graphics access.

brain2paths

Information about different data samples.

Type:

dict

show_plots

Whether to show plots, only works if you have graphics access.

Type:

bool

atlas_points

Key - sample ID, Value - coordinates of soma detection.

Type:

dict

id_to_regioncounts

Key - sample ID, Value - dictionary of detection counts by region.

Type:

dict

region_graph

Graph of region hierarchy with soma detection counts as node attributes.

Type:

nx.DiGraph

brainrender_somas(self, subtype_colors: dict, brain_region: str = 'DR')

Generate brainrender viewer with soma detections.

Parameters:
  • subtype_colors (_type_) -- Mapping of subtypes (in soma_data.py file) to colors for soma plotting.

  • brain_region (str, optional) -- Brain region to display with the detections (e.g. dorsal raphe nucleus). Defaults to "DR".

napari_coronal_section(self, z: int, subtype_colors: dict, symbols: list = ['o', '+', '^', 'vbar'], fold_on: bool = False)

Generate napari view with allen atlas and points of soma detections.

Parameters:
  • z (int) -- index of coronal slice in Allen atlas.

  • subtype_colors (dict) -- Mapping of subtypes (in soma_data.py file) to colors for soma plotting.

  • symbols (list) -- Napari point symbols to use for different samples of the same subtype. Defaults to ["o", "+", "^", "vbar"].

  • fold_on (bool, optional) -- Whether napari views should be a hemisphere, in which case detections from the other side are mirrored. Defaults to False.

region_barchart(self, regions: list, composite_regions: dict = {}, normalize_region: int = - 1)

Generate bar charts comparing soma detection counts between regions.

Parameters:
  • regions (list) -- List of Allen atlas brain region IDs to display data for (ID's found here: http://api.brain-map.org/api/v2/structure_graph_download/1.json)

  • composite_regions (dict, optional) -- Mapping from a custom composite region (str, e.g. "Amygdala") to a set of regions that compose it (list of ints e.g. [131, 295, 319, 780]). Defaults to {}.

  • normalize_region (int, optional) -- Region ID to normalize data for the normalized bar chart. Defaults to -1.

class brainlit.BrainLine.AxonDistribution(brain_ids: list, data_file: str, regional_distribution_dir: str, show_plots: bool = True)

Generates visualizations of results of axon segmentations of a set of brains. Implements BrainDistribution.

Parameters:
  • brain_ids (list) -- List of brain IDs (keys of data json file).

  • data_files (str) -- Path to json file that contains information about samples.

  • regional_distribution_dir (str) -- Path to directory with pkl files that countain segmentation results by region.

  • show_plots (bool) -- Whether to show plots, only works if you have graphics access.

regional_distribution_dir

Path to directory with pkl files that countain segmentation results by region.

Type:

str

region_graph

Graph of region hierarchy with segmentation results as node attributes.

Type:

nx.DiGraph

total_axon_vols

Key - sample ID Value - Total volume of segmented axon.

Type:

dict

brain2paths

Information about different data samples.

Type:

dict

show_plots

Whether to show plots, only works if you have graphics access.

Type:

bool

brainrender_axons(self, subtype_colors: dict, brain_region: str = 'DR')

Generate brainrender view to show axon segmentations.

Parameters:

subtype_colors (dict) -- Mapping of subtype to color to be used in visualization.

napari_coronal_section(self, z: int, subtype_colors: dict, fold_on: bool = False)

Generate napari viewer with allen parcellation and heat map of axon segmentations.

Parameters:
  • z (int) -- Index of coronal slice of allen atlas.

  • subtype_colors (dict) -- Mapping of subtype to color to be used in visualization.

  • fold_on (bool, optional) -- Whether to plot a single hemisphere, with results from other side mirrored. Defaults to False.

region_barchart(self, regions: list, composite_regions: dict = {}, normalize_region: int = - 1)

Generate bar charts with statistical tests to compare segmentations between brains.

Parameters:
  • regions (list) -- List of Allen atlas brain region IDs to display data for (ID's found here: http://api.brain-map.org/api/v2/structure_graph_download/1.json)

  • composite_regions (dict, optional) -- Mapping from a custom composite region (str, e.g. "Amygdala") to a set of regions that compose it (list of ints e.g. [131, 295, 319, 780]). Defaults to {}.

  • normalize_region (int, optional) -- Region ID to normalize data for the normalized bar chart. Defaults to -1.