ibl_alignment_gui.loaders.histology_loader

Functions

_build_slice_loader

Pick the right SliceLoader by inspecting the histology directory.

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

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