PartSegImage

class PartSegImage.BaseImageWriter[source]

Bases: ABC

class PartSegImage.Channel(value)[source]

Bases: object

This class is introduced to distinguish numerical algorithm parameter from choose channel. In autogenerated interface field with this type limits input values to number of current image channels

property value: str | int

Value stored in this class

class PartSegImage.CziImageReader(callback_function=None)[source]

Bases: BaseImageReaderBuffer

This class is to read data from czi files. Masks will be treated as TIFF.

image_class

alias of Image

read(image_path, mask_path=None, ext=None)[source]

Main function to read image. If ext is not set then it may be deduced from path to file. If BytesIO is given and non default data file type is needed then ext need to be set

Parameters:
  • image_path (Union[str, BytesIO, Path]) – path to image or buffer

  • mask_path – path to mask or buffer

  • ext – extension if need to decide algorithm, if absent and image_path is path then should be deduced from path

Return type:

Image

Returns:

image structure

classmethod read_image(image_path, mask_path=None, callback_function=None, default_spacing=None)

read image file with optional mask file

Parameters:
  • image_path (Union[str, Path, BytesIO]) – path or opened file contains image

  • mask_path

  • callback_function (Optional[Callable]) – function for provide information about progress in reading file (for progressbar)

  • default_spacing (Optional[Tuple[float, float, float]]) – used if file do not contains information about spacing (or metadata format is not supported)

Return type:

Image

Returns:

image

classmethod return_order()

Order to which image axes should be rearranged before pass to image_class constructor. Default is image_class.return_order

Return type:

str

classmethod update_array_shape(array, axes)[source]

Rearrange order of array axes to get proper internal axes order

Parameters:
  • array (ndarray) – array to reorder

  • axes (str) – current order of array axes as string like “TZYXC”

class PartSegImage.GenericImageReader(callback_function=None)[source]

Bases: BaseImageReaderBuffer

This class try to decide which method use base on path

image_class

alias of Image

read(image_path, mask_path=None, ext=None)[source]

Main function to read image. If ext is not set then it may be deduced from path to file. If BytesIO is given and non default data file type is needed then ext need to be set

Parameters:
  • image_path (Union[str, BytesIO, Path]) – path to image or buffer

  • mask_path – path to mask or buffer

  • ext – extension if need to decide algorithm, if absent and image_path is path then should be deduced from path

Return type:

Image

Returns:

image structure

classmethod read_image(image_path, mask_path=None, callback_function=None, default_spacing=None)

read image file with optional mask file

Parameters:
  • image_path (Union[str, Path, BytesIO]) – path or opened file contains image

  • mask_path

  • callback_function (Optional[Callable]) – function for provide information about progress in reading file (for progressbar)

  • default_spacing (Optional[Tuple[float, float, float]]) – used if file do not contains information about spacing (or metadata format is not supported)

Return type:

Image

Returns:

image

classmethod return_order()

Order to which image axes should be rearranged before pass to image_class constructor. Default is image_class.return_order

Return type:

str

classmethod update_array_shape(array, axes)

Rearrange order of array axes to get proper internal axes order

Parameters:
  • array (ndarray) – array to reorder

  • axes (str) – current order of array axes as string like “TZYXC”

class PartSegImage.IMAGEJImageWriter[source]

Bases: BaseImageWriter

class for saving TIFF images

classmethod save(image, save_path, compression='')[source]

Save image as tiff to path or buffer :type image: Image :param image: image for save :type save_path: Union[str, BytesIO, Path] :param save_path: save location

classmethod save_mask(image, save_path, compression='')[source]

Save mask connected to image as tiff to path or buffer :type image: Image :param image: mast is obtain with Image.get_mask_for_save() :type save_path: Union[str, Path] :param save_path: save location

class PartSegImage.Image(data, image_spacing, file_path=None, mask=None, default_coloring=None, ranges=None, channel_names=None, axes_order=None, shift=None, name='')[source]

Bases: object

Base class for Images used in PartSeg

Parameters:
  • data (Union[List[ndarray], ndarray]) – 5-dim array with order: time, z, y, x, channel

  • image_spacing (Tuple[Union[float, int], ...]) – spacing for z, y, x

  • file_path – path to image on disc

  • mask (Optional[ndarray]) – mask array in shape z,y,x

  • default_coloring – default colormap - not used yet

  • ranges – default ranges for channels

  • channel_names – labels for channels

  • axes_order (Optional[str]) – allow to create Image object form data with different axes order, or missed axes

Variables:

~.axis_order (str) – internal order of axes

It is prepared for subclassing with changed internal order. Eg:

>>> class ImageJImage(Image):
>>>     axis_order = "TZCYX"
static calc_index_to_frame(array_axis, important_axis)[source]

calculate in which axis frame should be added

Parameters:
  • array_axis (str) – list of image axis

  • important_axis (str) – list of framed axis

Return type:

List[int]

Returns:

list of indices to add frame.

property channel_pos: int

Channel axis. Need to have ‘C’ in axis_order

property channels: int

number of image channels

clip_array(array, **kwargs)[source]

Clip array by axis. Axis is selected by single letter from axis_order

Parameters:
  • array (ndarray) – array to clip

  • kwargs (Union[int, slice]) – mapping from axis to position or slice on this axis

Return type:

ndarray

Returns:

clipped array

cut_image(cut_area, replace_mask=False, frame=2, zero_out_cut_area=True)[source]

Create new image base on mask or list of slices :param bool replace_mask: if cut area is represented by mask array, then in result image the mask is set base on cut_area if cur_area is np.ndarray :param typing.Union[np.ndarray, typing.Iterable[slice]] cut_area: area to cut. Defined with slices or mask :param int frame: additional frame around cut_area :param bool zero_out_cut_area: :rtype: Image :return: Image

property dtype: dtype

dtype of image array

fit_array_to_image(array)[source]

Change shape of array with inserting single dimensional entries

Parameters:

array (np.ndarray) – array to be fitted

Return type:

ndarray

Returns:

reshaped array with added missing 1 in shape

Raises:

ValueError – if cannot fit array

fit_mask_to_image(array)[source]

call fit_array_to_image() and then relabel and change type to minimal which fit all information

Return type:

ndarray

classmethod get_array_axis_positions()[source]
Returns:

dict with mapping axis to its position for array fitted to image

Return type:

dict

classmethod get_axis_positions()[source]
Returns:

dict with mapping axis to its position

Return type:

dict

get_channel(num)[source]

Alias for get_sub_data() with argument c=num

Parameters:

num (int | str | Channel) – channel num or name to be extracted

Returns:

given channel array

Return type:

numpy.ndarray

get_data_by_axis(**kwargs)[source]

Get part of data extracted by sub axis. Axis is selected by single letter from axis_order

Parameters:

kwargs – axis list with

Returns:

Return type:

get_dimension_letters()[source]
Return type:

str

Returns:

letters which indicates non trivial dimensions

get_dimension_number()[source]

return number of nontrivial dimensions

Return type:

int

get_image_for_save()[source]
Return type:

ndarray

Returns:

numpy array in imagej tiff order axes

get_layer(time, stack)[source]

return single layer contains data for all channel

Parameters:
  • time (int) – time coordinate. For images with not time use 0.

  • stack (int) – “z coordinate. For time data use 0.

Return type:

ndarray

Returns:

get_mask_for_save()[source]
Return type:

Optional[ndarray]

Returns:

if image has mask then return mask with axes in proper order

get_ranges()[source]

image brightness ranges for each channel

Return type:

List[Tuple[float, float]]

get_um_shift()[source]

image spacing in micrometers

Return type:

Tuple[Union[float, int], ...]

get_um_spacing()[source]

image spacing in micrometers

Return type:

Tuple[Union[float, int], ...]

property has_mask: bool

check if image is masked

property is_2d: bool

Check if image z and time dimension are equal to 1. Equivalent to: image.layers == 1 and image.times == 1

property is_stack: bool

check if image contain 3d data

property is_time: bool

check if image contains time data

property layers: int

z-dim of image

merge(image, axis)[source]

Produce new image merging image data along given axis. All metadata are obtained from self.

Parameters:
  • image (Image) – Image to be merged

  • axis (str)

Returns:

New image produced from merge

Return type:

Image

property plane_shape: Tuple[int, int]

y,x size of image

classmethod reorder_axes(array, axes)[source]

reorder axes to internal storage format

Parameters:
  • array (np.ndarray) – array to have changed order of axes

  • axes (str) – axes order

Return type:

ndarray

Returns:

array with correct order of axes

set_mask(mask, axes=None)[source]

Set mask for image, check if it has proper shape.

Parameters:
  • mask (Optional[ndarray]) – mask in same shape like image. May not contains 1 dim axes.

  • axes (Optional[str]) – order of axes in mask, use if different than return_order

Raises:

ValueError – on wrong shape

set_spacing(value)[source]

set image spacing

property shape

Whole image shape. order of axes my change. Current order is in return_order

property spacing: Tuple[float | int, ...]

image spacing

property stack_pos: int

Stack axis. Need to have ‘Z’ in axis_order

substitute(data=None, image_spacing=None, file_path=None, mask=<object object>, default_coloring=None, ranges=None, channel_names=None)[source]

Create copy of image with substitution of not None elements

Return type:

Image

swap_time_and_stack()[source]

Swap time and stack axes. For example my be used to convert time image in 3d image.

property time_pos

Time axis. Need to have ‘T’ in axis_order

property times: int

number of time frames

property voxel_size: Tuple[float | int, ...]

alias for spacing

class PartSegImage.ImageWriter[source]

Bases: BaseImageWriter

class for saving TIFF images

classmethod save(image, save_path, compression='ADOBE_DEFLATE')[source]

Save image as tiff to path or buffer

Parameters:
classmethod save_mask(image, save_path, compression='ADOBE_DEFLATE')[source]

Save mask connected to image as tiff to path or buffer

Parameters:
class PartSegImage.ObsepImageReader(callback_function=None)[source]

Bases: BaseImageReader

image_class

alias of Image

read(image_path, mask_path=None, ext=None)[source]

Main function to read image. If ext is not set then it may be deduced from path to file. If BytesIO is given and non default data file type is needed then ext need to be set

Parameters:
  • image_path (Union[str, Path]) – path to image or buffer

  • mask_path – path to mask or buffer

  • ext – extension to decide algorithm, if absent and image_path is path then should be deduced from path

Return type:

Image

Returns:

image structure

classmethod read_image(image_path, mask_path=None, callback_function=None, default_spacing=None)

read image file with optional mask file

Parameters:
  • image_path (Union[str, Path]) – path or opened file contains image

  • mask_path

  • callback_function (Optional[Callable]) – function for provide information about progress in reading file (for progressbar)

  • default_spacing (Optional[Tuple[float, float, float]]) – used if file do not contains information about spacing (or metadata format is not supported)

Return type:

Image

Returns:

image

classmethod return_order()

Order to which image axes should be rearranged before pass to image_class constructor. Default is image_class.return_order

Return type:

str

classmethod update_array_shape(array, axes)

Rearrange order of array axes to get proper internal axes order

Parameters:
  • array (ndarray) – array to reorder

  • axes (str) – current order of array axes as string like “TZYXC”

class PartSegImage.OifImagReader(callback_function=None)[source]

Bases: BaseImageReader

image_class

alias of Image

read(image_path, mask_path=None, ext=None)[source]

Main function to read image. If ext is not set then it may be deduced from path to file. If BytesIO is given and non default data file type is needed then ext need to be set

Parameters:
  • image_path (Union[str, Path]) – path to image or buffer

  • mask_path – path to mask or buffer

  • ext – extension to decide algorithm, if absent and image_path is path then should be deduced from path

Return type:

Image

Returns:

image structure

classmethod read_image(image_path, mask_path=None, callback_function=None, default_spacing=None)

read image file with optional mask file

Parameters:
  • image_path (Union[str, Path]) – path or opened file contains image

  • mask_path

  • callback_function (Optional[Callable]) – function for provide information about progress in reading file (for progressbar)

  • default_spacing (Optional[Tuple[float, float, float]]) – used if file do not contains information about spacing (or metadata format is not supported)

Return type:

Image

Returns:

image

classmethod return_order()

Order to which image axes should be rearranged before pass to image_class constructor. Default is image_class.return_order

Return type:

str

classmethod update_array_shape(array, axes)

Rearrange order of array axes to get proper internal axes order

Parameters:
  • array (ndarray) – array to reorder

  • axes (str) – current order of array axes as string like “TZYXC”

exception PartSegImage.TiffFileException[source]

Bases: Exception

exception raised if reading tiff file fails. Created for distinguish exceptions which should reported as warning message (not for report)

add_note()

Exception.add_note(note) – add a note to the exception

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class PartSegImage.TiffImageReader(callback_function=None)[source]

Bases: BaseImageReaderBuffer

TIFF/LSM files reader. Base reading with BaseImageReader.read_image()

image_file: tifffile.TiffFile mask_file: tifffile.TiffFile

static decode_int(val)[source]

This function split 32 bits int on 4 8-bits ints

Parameters:

val (int) – value to decode

Returns:

list of four numbers with values from [0, 255]

image_class

alias of Image

read(image_path, mask_path=None, ext=None)[source]

Read tiff image from tiff_file

Return type:

Image

classmethod read_image(image_path, mask_path=None, callback_function=None, default_spacing=None)

read image file with optional mask file

Parameters:
  • image_path (Union[str, Path, BytesIO]) – path or opened file contains image

  • mask_path

  • callback_function (Optional[Callable]) – function for provide information about progress in reading file (for progressbar)

  • default_spacing (Optional[Tuple[float, float, float]]) – used if file do not contains information about spacing (or metadata format is not supported)

Return type:

Image

Returns:

image

classmethod return_order()

Order to which image axes should be rearranged before pass to image_class constructor. Default is image_class.return_order

Return type:

str

classmethod update_array_shape(array, axes)

Rearrange order of array axes to get proper internal axes order

Parameters:
  • array (ndarray) – array to reorder

  • axes (str) – current order of array axes as string like “TZYXC”

static verify_mask(mask_file, image_file)[source]

verify if mask fit to image. Raise ValueError exception on error :return:

PartSegImage.image_reader

class PartSegImage.image_reader.BaseImageReader(callback_function=None)[source]

Bases: object

Base class for reading image using Christopher Gholike libraries

Variables:

~.image_class (Type[Image]) – image class to return

image_class

alias of Image

abstract read(image_path, mask_path=None, ext=None)[source]

Main function to read image. If ext is not set then it may be deduced from path to file. If BytesIO is given and non default data file type is needed then ext need to be set

Parameters:
  • image_path (Union[str, Path]) – path to image or buffer

  • mask_path – path to mask or buffer

  • ext – extension to decide algorithm, if absent and image_path is path then should be deduced from path

Return type:

Image

Returns:

image structure

classmethod read_image(image_path, mask_path=None, callback_function=None, default_spacing=None)[source]

read image file with optional mask file

Parameters:
  • image_path (Union[str, Path]) – path or opened file contains image

  • mask_path

  • callback_function (Optional[Callable]) – function for provide information about progress in reading file (for progressbar)

  • default_spacing (Optional[Tuple[float, float, float]]) – used if file do not contains information about spacing (or metadata format is not supported)

Return type:

Image

Returns:

image

classmethod return_order()[source]

Order to which image axes should be rearranged before pass to image_class constructor. Default is image_class.return_order

Return type:

str

classmethod update_array_shape(array, axes)[source]

Rearrange order of array axes to get proper internal axes order

Parameters:
  • array (ndarray) – array to reorder

  • axes (str) – current order of array axes as string like “TZYXC”

class PartSegImage.image_reader.BaseImageReaderBuffer(callback_function=None)[source]

Bases: BaseImageReader

abstract read(image_path, mask_path=None, ext=None)[source]

Main function to read image. If ext is not set then it may be deduced from path to file. If BytesIO is given and non default data file type is needed then ext need to be set

Parameters:
  • image_path (Union[str, Path, BytesIO]) – path to image or buffer

  • mask_path – path to mask or buffer

  • ext – extension if need to decide algorithm, if absent and image_path is path then should be deduced from path

Return type:

Image

Returns:

image structure

classmethod read_image(image_path, mask_path=None, callback_function=None, default_spacing=None)[source]

read image file with optional mask file

Parameters:
  • image_path (Union[str, Path, BytesIO]) – path or opened file contains image

  • mask_path

  • callback_function (Optional[Callable]) – function for provide information about progress in reading file (for progressbar)

  • default_spacing (Optional[Tuple[float, float, float]]) – used if file do not contains information about spacing (or metadata format is not supported)

Return type:

Image

Returns:

image

class PartSegImage.image_reader.CziImageReader(callback_function=None)[source]

Bases: BaseImageReaderBuffer

This class is to read data from czi files. Masks will be treated as TIFF.

read(image_path, mask_path=None, ext=None)[source]

Main function to read image. If ext is not set then it may be deduced from path to file. If BytesIO is given and non default data file type is needed then ext need to be set

Parameters:
  • image_path (Union[str, BytesIO, Path]) – path to image or buffer

  • mask_path – path to mask or buffer

  • ext – extension if need to decide algorithm, if absent and image_path is path then should be deduced from path

Return type:

Image

Returns:

image structure

classmethod update_array_shape(array, axes)[source]

Rearrange order of array axes to get proper internal axes order

Parameters:
  • array (ndarray) – array to reorder

  • axes (str) – current order of array axes as string like “TZYXC”

class PartSegImage.image_reader.GenericImageReader(callback_function=None)[source]

Bases: BaseImageReaderBuffer

This class try to decide which method use base on path

read(image_path, mask_path=None, ext=None)[source]

Main function to read image. If ext is not set then it may be deduced from path to file. If BytesIO is given and non default data file type is needed then ext need to be set

Parameters:
  • image_path (Union[str, BytesIO, Path]) – path to image or buffer

  • mask_path – path to mask or buffer

  • ext – extension if need to decide algorithm, if absent and image_path is path then should be deduced from path

Return type:

Image

Returns:

image structure

class PartSegImage.image_reader.ObsepImageReader(callback_function=None)[source]

Bases: BaseImageReader

read(image_path, mask_path=None, ext=None)[source]

Main function to read image. If ext is not set then it may be deduced from path to file. If BytesIO is given and non default data file type is needed then ext need to be set

Parameters:
  • image_path (Union[str, Path]) – path to image or buffer

  • mask_path – path to mask or buffer

  • ext – extension to decide algorithm, if absent and image_path is path then should be deduced from path

Return type:

Image

Returns:

image structure

class PartSegImage.image_reader.OifImagReader(callback_function=None)[source]

Bases: BaseImageReader

read(image_path, mask_path=None, ext=None)[source]

Main function to read image. If ext is not set then it may be deduced from path to file. If BytesIO is given and non default data file type is needed then ext need to be set

Parameters:
  • image_path (Union[str, Path]) – path to image or buffer

  • mask_path – path to mask or buffer

  • ext – extension to decide algorithm, if absent and image_path is path then should be deduced from path

Return type:

Image

Returns:

image structure

exception PartSegImage.image_reader.TiffFileException[source]

Bases: Exception

exception raised if reading tiff file fails. Created for distinguish exceptions which should reported as warning message (not for report)

class PartSegImage.image_reader.TiffImageReader(callback_function=None)[source]

Bases: BaseImageReaderBuffer

TIFF/LSM files reader. Base reading with BaseImageReader.read_image()

image_file: tifffile.TiffFile mask_file: tifffile.TiffFile

static decode_int(val)[source]

This function split 32 bits int on 4 8-bits ints

Parameters:

val (int) – value to decode

Returns:

list of four numbers with values from [0, 255]

read(image_path, mask_path=None, ext=None)[source]

Read tiff image from tiff_file

Return type:

Image

static verify_mask(mask_file, image_file)[source]

verify if mask fit to image. Raise ValueError exception on error :return:

PartSegImage.image_reader.name_to_scalar = {'\\u00B5m': 1e-06, 'cal': 0.025400000000000002, 'centimeter': 0.01, 'cm': 0.01, 'micron': 1e-06, 'millimeter': 0.001, 'mm': 0.001, 'nanometer': 1e-09, 'nm': 1e-09, 'picometer': 1e-24, 'pm': 1e-12, 'um': 1e-06, 'µm': 1e-06}

dict with known names of scalar to scalar value. Some may be missed