ibl_alignment_gui.loaders.histology_loader

Functions

_build_slice_loader

Pick the right SliceLoader by inspecting the histology directory.

build_anatomical_atlas

Build a BrainAtlasAnatomical from the registration pipeline NRRD files in histology_path.

download_histology_data

Download histology data from flatiron server if not already cached locally.

make_slice_loader

Return the appropriate SliceLoader for the given folder.

tif2nrrd

Convert a tif image to nrrd format if the nrrd does not already exist.

Classes

AnatomicalSliceLoader

SliceLoader for histology registered in original anatomical (non-CCF) space.

ImageSpacePaths

Paths to the NRRD files produced by the histology registration pipeline.

LazySliceDict

Dict of histology slice Bunches that loads channels from disk on first access.

NrrdSliceLoader

SliceLoader for histology in the NRRD format.

SliceLoader

Abstract base class for loading histology slices.

TiffSliceLoader

SliceLoader for histology in TIFF format (e.g. brainreg outputs).

class ibl_alignment_gui.loaders.histology_loader.AnatomicalSliceLoader(file_path, brain_atlas)[source]

Bases: SliceLoader

SliceLoader for histology registered in original anatomical (non-CCF) space.

Expects a folder produced by the histology registration pipeline containing: ccf_in_*.nrrd, labels_in_*.nrrd, histology_registration_pipeline.nrrd, histology_registration.nrrd, and optionally Ex_*_Em_*.nrrd channel files.

The BrainAtlasAnatomical built from these files works in the physical space of the anatomical images (mm, RAS). Coordinates passed to get_slices must therefore be in that same anatomical physical space, not in Allen CCF space.

Parameters:
  • file_path (Path) – Folder containing the registration pipeline NRRD outputs.

  • brain_atlas (BrainAtlas) – Unused; accepted to satisfy the SliceLoader interface and the make_slice_loader factory signature.

get_paths()[source]

Resolve the anatomical-space volume paths and record them on the loader.

Return type:

None

load_volume(vol_path)[source]

Read a channel NRRD, reorient to IRP, and return as a numpy array.

Return type:

ndarray

class ibl_alignment_gui.loaders.histology_loader.ImageSpacePaths(atlas_image_path, atlas_labels_path, pipeline_image_path, histology_image_path, other_channel_paths=<factory>)[source]

Bases: object

Paths to the NRRD files produced by the histology registration pipeline.

All of the files live in a single folder.

atlas_image_pathPath

CCF template warped into anatomical space (ccf_in_*.nrrd).

atlas_labels_pathPath

CCF labels warped into anatomical space (labels_in_*.nrrd).

pipeline_image_pathPath

Pipeline reference image used by the registration (histology_registration_pipeline.nrrd).

histology_image_pathPath

Main registered histology channel (histology_registration.nrrd).

other_channel_pathslist[Path]

Any additional fluorescence channels matching Ex_*_Em_*.nrrd.

atlas_image_path: Path
atlas_labels_path: Path
classmethod from_folder(input_path)[source]

Discover all required files in input_path and return an ImageSpacePaths.

Raises StopIteration if any required file is missing.

Return type:

ImageSpacePaths

histology_image_path: Path
other_channel_paths: list[Path]
pipeline_image_path: Path
class ibl_alignment_gui.loaders.histology_loader.LazySliceDict(eager_data, lazy_callbacks)[source]

Bases: dict

Dict of histology slice Bunches that loads channels from disk on first access.

Eager entries (CCF, Annotation) are populated immediately. Lazy entries (histology channels) are stored as None placeholders until a key is accessed, at which point the registered callback loads and caches the slice.

get(key, default=None)[source]

Return the slice for key, or default when it is not present.

class ibl_alignment_gui.loaders.histology_loader.NrrdSliceLoader(file_path, brain_atlas)[source]

Bases: SliceLoader

SliceLoader for histology in the NRRD format.

Parameters:
  • file_path (Path) – Directory containing .nrrd files.

  • brain_atlas (AllenAtlas) – Brain atlas for alignment.

get_paths()[source]

Load histology file paths with predefined color channel suffixes.

Return type:

None

load_volume(vol_path)[source]

Load a volume using AllenAtlas.

Parameters:

vol_path (Path) – A path to histology volume.

Returns:

Loaded image volume.

Return type:

np.ndarray

class ibl_alignment_gui.loaders.histology_loader.SliceLoader(file_path, brain_atlas)[source]

Bases: ABC

Abstract base class for loading histology slices.

Subclasses must implement the get_paths and load_volume methods.

Parameters:
  • file_path (Path) – Directory containing histology files.

  • brain_atlas (AllenAtlas) – Reference brain atlas.

abstractmethod get_paths()[source]

Locate and store relevant histology file paths in self.hist_paths.

Return type:

None

get_slices(xyz)[source]

Generate slice images for CCF, annotation, and histology channels.

CCF and Annotation are computed immediately (atlas arrays are already in memory). Histology channel volumes are loaded from disk only when their key is first accessed in the returned dict.

Parameters:

xyz (np.ndarray) – n x 3 array of xyz coordinates along the probe track.

Returns:

Keys: ‘CCF’, ‘Annotation’, and one key per entry in hist_paths. Each value is a Bunch with ‘slice’ (2D array), ‘scale’, and ‘offset’.

Return type:

LazySliceDict

abstractmethod load_volume(vol_path)[source]

Load a 3D volume from a file.

Parameters:

vol_path (Path) – A path to the volume file.

Returns:

Loaded 3D image volume.

Return type:

np.ndarray

class ibl_alignment_gui.loaders.histology_loader.TiffSliceLoader(file_path, brain_atlas)[source]

Bases: SliceLoader

SliceLoader for histology in TIFF format (e.g. brainreg outputs).

Detects brainreg’s standard C0 (green) / C1 (red) channel suffixes first, then falls back to the NRRD loader’s GR / RD substring rules so manually-named TIFFs also load.

Parameters:
  • file_path (Path) – Directory containing .tif / .tiff files.

  • brain_atlas (AllenAtlas) – Brain atlas for alignment.

get_paths()[source]

Locate histology TIFFs and store paths keyed by display label.

Return type:

None

load_volume(vol_path)[source]

Load a TIFF and reorient to AllenAtlas (AP, ML, DV) convention.

Parameters:

vol_path (Path) – A path to a histology TIFF volume.

Returns:

Loaded volume with shape (AP, ML, DV) ready for slicing by SliceLoader.get_slice().

Return type:

np.ndarray

Notes

Brainreg’s downsampled_standard_brain_C*.tiff are 25 µm isotropic in Allen CCF space, with sitk.GetArrayFromImage axis order (AP, DV, ML). The AllenAtlas convention is (AP, ML, DV), so a single axis swap suffices — no flips required.

ibl_alignment_gui.loaders.histology_loader.build_anatomical_atlas(histology_path)[source]

Build a BrainAtlasAnatomical from the registration pipeline NRRD files in histology_path.

Parameters:

histology_path (Path) – Folder containing ccf_in_*.nrrd, labels_in_*.nrrd, and histology_registration_pipeline.nrrd.

Return type:

BrainAtlasAnatomical

ibl_alignment_gui.loaders.histology_loader.download_histology_data(subject, laboratory)[source]

Download histology data from flatiron server if not already cached locally.

Parameters:
  • subject (str) – Subject name

  • laboratory (str) – Laboratory name

Return type:

tuple[list[Path], Path] | tuple[None, Path]

Returns:

  • path_to_files (list[Path] or None) – List of paths to downloaded or cached nrrd files, or None if not found.

  • cache_dir (Path) – Directory where files are cached.

ibl_alignment_gui.loaders.histology_loader.make_slice_loader(file_path, brain_atlas, space='ccf')[source]

Return the appropriate SliceLoader for the given folder.

Parameters:
  • file_path (Path or None) – Folder containing histology files. When None, no histology volumes are loaded and only the atlas template and annotation slices are available.

  • brain_atlas (BrainAtlas) – Brain atlas passed to the loader (used directly by NrrdSliceLoader; ignored by AnatomicalSliceLoader which builds its own atlas from the folder files).

  • space ({'ccf', 'anatomical'}) – Which loader to use. ‘ccf’ returns a NrrdSliceLoader operating in Allen CCF space; ‘anatomical’ returns an AnatomicalSliceLoader operating in the original image space. Matches the histology.space field in the alignment YAML.

Returns:

NrrdSliceLoader for ‘ccf’, AnatomicalSliceLoader for ‘anatomical’. A NrrdSliceLoader with no histology volumes when file_path is None.

Return type:

SliceLoader

ibl_alignment_gui.loaders.histology_loader.tif2nrrd(path_to_image)[source]

Convert a tif image to nrrd format if the nrrd does not already exist.

Parameters:

path_to_image (str or Path) – The path to the tif file.

Returns:

path_to_nrrd – The path to the nrrd file.

Return type:

Path