BrainLine

Data Helper Functions

brainlit.BrainLine.download_subvolumes(data_dir: str, brain_id: str, layer_names: list, dataset_to_save: str, data_file: str)[source]

Download subvolumes around a set of manually marked points for validation of machine learning model.

Parameters
  • data_dir (str) -- Path to directory where subvolumes will be saved.

  • brain_id (str) -- Brain ID key in brain2paths dictionary from soma_data or axon_data/

  • layer_names (list) -- List of precomputed layer names associated with the brain_id, ordered by primary signal channel (e.g. antibody), background channel, and secondary signal channel (e.g. endogenous fluorescence).

  • dataset_to_save (str) -- val or train - specifies which set of subvolumes should be downloaded, if applicable.

  • data_file (str) -- path to json file with data information.

Raises

ValueError -- If object_type is not soma or axon

brainlit.BrainLine.json_to_points(url, round=False)[source]

Extract points from a neuroglancer url.

Parameters
  • url (str) -- url to neuroglancer state (that was posted to neurodata json server).

  • round (bool, optional) -- whether to round coordinates to integers. Defaults to False.

Returns

Keys are names of point layers and values are lists of points from that layer.

Return type

dict

Apply Ilastik and Validate Models

class brainlit.BrainLine.ApplyIlastik(ilastik_path: str, project_path: str, brains_path: str, brains: list)[source]

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

Parameters
  • ilastik_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

Path

brains

List of brain sample names.

Type

list

move_results(self)[source]

Move results from process_subvols to a new subfolder.

process_subvols(self, dset: str = 'val', ncpu: int = 6)[source]

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

Parameters

ncpu (int, optional) -- Number of cpus to use for segmentation. Defaults to 6.

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

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, dset: str = 'val', doubles: list = [], show_plot: bool = True)[source]

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)[source]

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, min_coords: list = [- 1, - 1, - 1], max_coords: list = [- 1, - 1, - 1])[source]

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.

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

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

collect_axon_results(self, brain_id: str, ng_layer_name: str)[source]

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)[source]

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, ontology_file: str, fixes_file: str = None, show_plots: bool = True, bootstrap: int = 0, shuffle: bool = False)[source]

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')[source]

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, plot_type: str = 'napari')[source]

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 views should be a hemisphere, in which case detections from the other side are mirrored. Defaults to False.

  • plot_type (str, optional) -- Whether to plot via napari ('napari') or pyplot ('plt').

Returns

pyplot figure only if plot_type=='plt'.

Return type

pyplot.figure.Figure

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

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, ontology_file: str, fixes_file: str = None, show_plots: bool = True)[source]

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')[source]

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)[source]

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)[source]

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.