magmap.io.np_io module#

Import/export for Numpy-based archives such as .npy and .npz formats.

class magmap.io.np_io.Image5d(img=None, path_img=None, path_meta=None, img_io=None)[source]#

Bases: object

Main image storage.

Variables:
  • img – 5D Numpy array in the format t,z,y,x,c; defaults to None.

  • path_img – Path from which img was loaded; defaults to None.

  • path_meta – Path from which metadata for img was loaded; defaults to None.

  • img_io – I/O source for image5d array; defaults to None.

  • subimg_offset – Sub-image offset in z,y,x; defaults to None.

  • subimg_size – Sub-image size in z,y,x; defaults to None.

  • meta – Image metadata dictionary; defaults to None.

rgb: bool#

True if image is RGB(A); defaults to False.

magmap.io.np_io.find_scaling(img_path, scaled_shape=None, scale=None, load_size=None)[source]#

Find scaling between two images.

Scaling can be computed to translate blob coordinates into another space, such as a downsampled image. These compressed coordinates can be used to generate a heat map of blobs.

Parameters:
  • img_path (str) – Base path to image.

  • scaled_shape (Optional[Sequence[int]], default: None) – Shape of image to calculate scaling factor if this factor cannot be found from a transposed file’s metadata; defaults to None.

  • scale (Optional[float], default: None) – Scalar scaling factor, used to find a rescaled file; defaults to None. To find a resized file instead, set an atlas profile with the resizing factor.

  • load_size (Optional[Sequence[int]], default: None) – Size of image to load in x, y, z, typically given by an atlas profile and used to identify the path of the scaled image to load; defaults to None.

Return type:

Tuple[Sequence[float], Sequence[float]]

Returns:

Tuple of sequence of scaling factors to a scaled or resized image, or None if not loaded or given, and the resolutions of the full-sized image found based on img_path.

magmap.io.np_io.get_num_channels(image5d)[source]#

Get the number of channels in a 5D image.

Parameters:

image5d (np.ndarray) – Numpy arry in the order, t,z,y,x[,c].

Returns:

Number of channels inferred based on the presence and length of the 5th dimension.

Return type:

int

magmap.io.np_io.img_to_blobs_path(path)[source]#

Get the blobs path associated with an image or user-supplied.

The user-supplied blobs path stored in magmap.io.config.load_data takes precedence over path.

Parameters:

path (str) – Image base path, without extension or MagellanMapper suffixes.

Returns:

Default MagellanMapper blobs path based on image path, or the config path if it is a string.

Return type:

str

magmap.io.np_io.read_np_archive(archive)[source]#

Load Numpy archive file into a dictionary, skipping any values that cannot be loaded.

Parameters:

archive – Loaded Numpy archive.

Returns:

Dictionary with keys and values corresponding to that of the Numpy archive, skipping any values that could not be loaded such as those that would require pickling when not allowed.

magmap.io.np_io.read_tif(path, img5d=None)[source]#

Read TIF files with Tifffile with lazy access through memory mapping.

Parameters:
  • path (str) – Path to file.

  • img5d (Optional[Image5d], default: None) – Image5d storage class; defaults to None.

Return type:

Tuple[Image5d, Dict[MetaKeys, Any]]

Returns:

Image5d storage instance and dictionary of extracted metadata.

magmap.io.np_io.setup_images(path, series=None, offset=None, size=None, proc_type=None, allow_import=True, fallback_main_img=True, bg_atlas=None, labels_ref_path=None)[source]#

Sets up an image and all associated images and metadata.

Paths for related files such as registered images will generally be constructed from path. If config.prefix is set, it will be used in place of path for registered labels.

Parameters:
  • path (str) – Path to image from which MagellanMapper-style paths will be generated.

  • series (Optional[int], default: None) – Image series number; defaults to None.

  • offset (Optional[Sequence[int]], default: None) – Sub-image offset given in z,y,x; defaults to None.

  • size (Optional[Sequence[int]], default: None) – Sub-image shape given in z,y,x; defaults to None.

  • proc_type (Optional[ProcessTypes], default: None) – Processing type.

  • allow_import (bool, default: True) – True to allow importing the image if it cannot be loaded; defaults to True.

  • fallback_main_img (bool, default: True) – True to fall back to loading a registered image if possible if the main image could not be loaded; defaults to True.

  • bg_atlas (Optional[BrainGlobeAtlas], default: None) – BrainGlobe atlas; defaults to None. If provided, the images and labels reference will be extracted from the atlas instead of loaded from path.

  • labels_ref_path (Optional[str], default: None) – Path to labels reference file. Defaults to None, in which case :att:`config.load_labels` and any loaded labels metadata will be used.

magmap.io.np_io.write_raw_file(arr, path)[source]#

Write an array to a RAW data file.

The array will be output directly to disk through a memmapped object.

Parameters:
  • arr (np.ndarray) – Array to write.

  • path (str) – Output path.

magmap.io.np_io.write_tif(image5d, path, **kwargs)[source]#

Write a NumPy array to TIF files.

Each channel will be exported to a separate file.

Parameters:
  • image5d (ndarray) – NumPy array in t, z, y, x, c dimension order.

  • path (Union[str, Path]) – Base output path. If image5d has multiple channels, they will be exported to files with _ch_<n> appended just before the extension.

  • kwargs (Any) – Arguments passed to tifffile.imwrite().