TIF#

TIF module provides interface to read and write tif image files.

Note

This module relies on the tifffile library.

class BaseMetadataParser(source, metadata, info_categories)[source]#

Bases: object

parse()[source]#
parse_date()[source]#
parse_description()[source]#
parse_order()[source]#
parse_overlap()[source]#
parse_pixel_metadata()[source]#

Parse only the pixel metadata (i.e. shape and order) from the OME metadata.

Returns

parse_resolution()[source]#
parse_shape()[source]#
parse_tile_configuration()[source]#
update_info(name, keys, mdict, astype)[source]#
property pixels_metadata#
class ClearMapMetadataParser(source, metadata, info_categories)[source]#

Bases: BaseMetadataParser

Parser for ClearMap metadata.

Typically, this is tuned for the metadata of the ClearMap generated tif files which might lack some of the metadata fields but where the axes order is bound to be ‘XYZ’.

parse_order()[source]#
class ImageJMetadataParser(source, metadata, info_categories)[source]#

Bases: BaseMetadataParser

parse_info_field()[source]#
property pixels_metadata#
class OMEMetadataParser(source, metadata, info_categories)[source]#

Bases: BaseMetadataParser

Parser for OME metadata. Typically, this is tuned for the metadata of the OME-TIFF files generated by either the UltraMicroscopeII or the Blaze light-sheet microscopes from LaVision BioTec (now Miltenyi Biotec).

parse_overlap()[source]#
parse_tile_configuration()[source]#
property pixels_metadata#
class Source(location, series=0, multi_file=False)[source]#

Bases: Source

Class to handle a tif file source

Note

Its assumed that the image data is stored in a series of the tif file.

Warning

It is also assumed that the last 3 dimensions are the image dimensions in the order z,y,x.

as_buffer()[source]#
as_memmap()[source]#
as_real()[source]#
as_virtual()[source]#

Return virtual source without array data to pass in parallel processing.

Returns

sourceSource class

The source class without array data.

get_raw_metadata_dictionary()[source]#
metadata(info=('shape', 'resolution', 'overlap'))[source]#

Returns metadata from this tif file.

Arguments

infolist, ‘all’ or None

Optional list of keywords, if ‘all’ return full tif metadata, if None return default set info.

Returns

metadatadict

Dictionary with the metadata.

to_clearmap_order(array)[source]#
property array#
property axes_order#
property dtype#

The data type of the source.

Returns

dtypedtype

The data type of the source.

property element_strides#

The strides of the array elements.

Returns

stridestuple

Strides of the array elements.

Note

The strides of the elements module itemsize instead of bytes.

property is_clearmap#
property location#

The location where the data of the source is stored.

Returns

locationstr or None

Returns the location of the data source or None if this source lives in memory only.

property name#

The name of this source.

Returns

namestr

Name of this source.

property pages_mode#
series#

Attribute whose value is computed on first access.

property series_mode#
property shape#

The shape of the source.

Returns

shapetuple

The shape of the source.

property tif_shape#
class VirtualSource(source=None, shape=None, dtype=None, order=None, location=None, name=None)[source]#

Bases: VirtualSource

as_buffer()[source]#
as_real()[source]#
as_virtual()[source]#

Return virtual source without array data to pass in parallel processing.

Returns

sourceSource class

The source class without array data.

property name#

The name of this source.

Returns

namestr

Name of this source.

array_to_tif(array, source_order='ZYX')[source]#
create(location=None, shape=None, dtype=None, mode=None, as_source=True, **kwargs)[source]#

Create a memory map.

Arguments

locationstr

The filename of the memory mapped array.

shapetuple or None

The shape of the memory map to create.

dtypedtype

The data type of the memory map.

mode‘r’, ‘w’, ‘w+’, None

The mode to open the memory map.

as_sourcebool

If True, return as Source class.

Returns

memmapnp.memmap

The memory map.

Note

By default memmaps are initialized as fortran contiguous if order is None.

is_tif(source)[source]#

Checks if this is a TIF source

map_axes(source_order, dest_order, ndim=None)[source]#

Generate a mapping of axes from source order to destination order. The mapping is a tuple of integers that can be used with np.transpose to reorder the axes of an array.

Parameters:
  • source_order (str) – The order of the axes in the source array (e.g., ‘ZYX’).

  • dest_order (str) – The desired order of axes in the output array (e.g., ‘XYZ’).

Returns

tuple

A tuple of integers that can be used with np.transpose to reorder the axes of an array.

read(source, slicing=None, sink=None, **args)[source]#

Read data from a tif file.

Arguments

sourcestr or TIF class

The name of the tif file or a TIF class.

slicingslice, Slice or None

An optional sub-slice to consider.

Returns

dataarray

The image data in the tif file.

reorder_shape(shape, order, dest_order)[source]#

Rearrange the shape tuple to match the given order.

Parameters:
  • shape (tuple) – The original shape of the array

  • order (str) – The order of the axes in the array (e.g., ‘ZYX’ or ‘CZYX’)

  • dest_order (str) – The desired order of axes in the output array (e.g., ‘XYZ’ or ‘XYZC’)

Returns

tuple

The shape rearranged to match the given order

shape_from_tif(shape, order='XYZ', dest_order='ZYX')[source]#

Rearrange the shape tuple to match the ‘XYZ’ (or ‘XYZC’) order based on the given axes order.

Parameters:
  • shape (tuple) – The original shape of the array.

  • order (str) – The order of the axes in the array (e.g., ‘ZYX’ or ‘CZYX’). Default is ‘XYZ’ (Default tiff order).

  • dest_order (str) – The desired order of axes in the output array (e.g., ‘XYZ’ or ‘XYZC’). Default is ‘ZYX’ (ClearMap array order).

Returns

tuple

The shape rearranged to match the ‘XYZ’ (or ‘XYZC’) order.

shape_to_tif(shape, order='ZYX', dest_order='XYZ')[source]#

Rearrange the shape tuple to match the ‘XYZ’ (or ‘XYZC’) order based on the given axes order.

Parameters:
  • shape (tuple) – The original shape of the array.

  • order (str) – The order of the axes in the array (e.g., ‘ZYX’ or ‘CZYX’). Default is ‘ZYX’ (ClearMap array order).

  • dest_order (str) – The desired order of axes in the output array (e.g., ‘XYZ’ or ‘XYZC’). Default is ‘XYZ’ (Default tiff order).

Returns

tuple

The shape rearranged to match the ‘XYZ’ (or ‘XYZC’) order.

tif_to_clearmap_order(array, array_order='XYZ')[source]#

Transpose the axes on array to the ClearMap order (‘ZYX’)

Parameters:
  • array (np.ndarray) – The array to transpose

  • array_order (str) – The order of the input array (with letters representing the axes)

Returns

The transposed array

transpose_array(array, source_order, dest_order)[source]#

Transpose the axes on array from source_order to dest_order.

Parameters:
  • array (np.ndarray) – The array to transpose

  • source_order (str) – The order of the axes in the array (with letters representing the axes)

  • dest_order (str) – The desired order of axes in the output array (with letters representing the axes)

Returns

The transposed array

write(sink, data, **args)[source]#

Write data to a tif file

Arguments

sinkstr

The name of the tif file.

Returns

sinkstr

The name of the tif file.