neurotorchmz.utils.image

Contents

neurotorchmz.utils.image#

Classes

AxisImage(img, axis[, name])

A class that supports lazy loading and caching of subimages derived from an main image by calculating for example the mean, median, std, min or max over an given axis.

FunctionType(*values)

A simple wrapper to encode the type of a image function with an priority

ImageObject([conv_cache_size, ...])

A class for holding a) the image provided in form an three dimensional numpy array (time, y, x) and b) the derived images and properties, for example the difference Image (img_diff).

ImageProperties(img)

A class that supports lazy loading and caching of image properties like mean, median, std, min, max and clippedMin (=np.min(0, self.min)) Returns scalars (except for the img property, where it returns the image used to initializate this object.

ImageView(*values)

Collapse a 3D image (t, y, x) into a smaller dimension

Exceptions

AlreadyLoadingError

Already loading an Image into an ImageObject

ImageObjectError

ImageObject Error

ImageShapeError([shape])

The image has an invalid shape

NoImageError

There is no img or img_diff loaded

UnsupportedExtensionError

Raised when the extension of the image is not supported

UnsupportedImageError([msg, file_name, ...])

The image is unsupported

exception neurotorchmz.utils.image.AlreadyLoadingError#

Bases: ImageObjectError

Already loading an Image into an ImageObject

class neurotorchmz.utils.image.AxisImage(img: ndarray | None, axis: tuple, name: str | None = None)#

Bases: object

A class that supports lazy loading and caching of subimages derived from an main image by calculating for example the mean, median, std, min or max over an given axis. Note that the axis specifies the axis which should be kept. Returns the image or the ImageProperties

Example for the axis: An AxisImage for an 3D Image (t, y, x) with argument axis=0 will calculate the mean (median, std, min, max) for each pixel. Providing axis=(1,2) will calculate the same for each image frame.

property axis: tuple#
copy() AxisImage#
property image: ndarray | None#
property image_props: ImageProperties#

Returns the properties of the original image

property max_image: ndarray | None#

Maximum image over the specified axis

property max_props: ImageProperties#
property mean_image: ndarray | None#

Mean image over the specified axis

property mean_normed_image: ndarray | None#

Normalized Mean image (max value is garanter to be 255 or 0) over the specified axis

property mean_normed_props: ImageProperties#
property mean_props: ImageProperties#

Returns the mean image properties as float image

property median_image: ndarray | None#

Median image over the specified axis

property median_props: ImageProperties#
property min_image: ndarray | None#

Minimum image over the specified axis

property min_props: ImageProperties#
property name: str | None#
property std_image: ndarray | None#

Std image over the specified axis

property std_normed_image: ndarray | None#

Normalized Std image (max value is garanter to be 255 or 0) over the specified axis

property std_normed_props: ImageProperties#
property std_props: ImageProperties#
class neurotorchmz.utils.image.FunctionType(*values)#

Bases: Enum

A simple wrapper to encode the type of a image function with an priority

PRE_FUNCTION = 30#
T = 10#
XY = 20#
class neurotorchmz.utils.image.ImageObject(conv_cache_size: int = 1, diff_conv_cache_size: int = 3)#

Bases: Serializable

A class for holding a) the image provided in form an three dimensional numpy array (time, y, x) and b) the derived images and properties, for example the difference Image (img_diff). All properties are lazy loaded, i. e. they are calculated on first access

SUPPORTED_EXPORT_EXTENSIONS = [('Lossless compressed Tiff', ('*.tiff', '*.tif'))]#
static _is_valid_image_stack(image: Any) bool#
clear()#

Resets the ImageObject and clears all stored images and metadata

clear_cache(full_clear: bool = False) None#

Clears caches of unsused convolutions

classmethod deserialize(serialize_dict: dict, **kwargs) Self#

Deserialize the given dict into a class object

export_img(path: Path) None#

Export the current img

export_img_diff(path: Path) None#

Export the current img_diff

get_functions_identifier(functions: list[tuple[str, Callable[[...], AxisImage], bool, FunctionType | int]]) str#
property img: ndarray | None#

Get or set the image. Note that setting to a new value will remove the old diff image

Raises:

UnsupportedImageError – The image is not a valid image stack

property img_diff: ndarray | None#

Get or set the diff image. Note that setting to a new value will remove the old image

Raises:

UnsupportedImageError – The image is not a valid image stack

img_diff_frame_props(frame: int) ImageProperties#

Returns the image diff properties for a given frame

property img_diff_functions: list[tuple[str, Callable[[AxisImage, AxisImage], AxisImage], bool, FunctionType | int]]#

Returns the current list of img_diff functions. An image function is intended to modify an image by accepting the image and image diff as parameters (both as AxisImage) and returning a new axis image. The functions inside the list are called first to last with the result from the predecessor (first with self.img_diff_raw). Every list entry must have an name (str), an Callable (img AxisImage, img_diff AxisImage) -> AxisImage, the cache flag (bool) and priority (int) The priority is used to sort the entries and should be set to zero on default

property img_diff_props: ImageProperties#

Returns the diff image properties (e.g. median, mean or maximum)

property img_diff_raw: ndarray | None#
img_diff_view(mode: ImageView, fn_list: list[tuple[str, Callable[[AxisImage, AxisImage], AxisImage], bool, FunctionType | int]] | Literal['default'] | None = None, cache: bool = True) AxisImage#

Returns a view of the current image given an ImageView mode

img_frame_props(frame: int) ImageProperties#

Returns the image properties for a given frame

property img_functions: list[tuple[str, Callable[[AxisImage], AxisImage], bool, FunctionType | int]]#

Returns the current list of image functions. An image function is intended to modify an image by accepting an AxisImage and returning a new axis image. The functions inside the list are called first to last with the result from the predecessor (first with self.img_raw). Every list entry must have an name (str), an Callable (AxisImage) -> AxisImage, the cache flag (bool) and priority (int). The priority is used to sort the entries and should be set to zero on default

property img_props: ImageProperties#

Returns the image properties (e.g. median, mean or maximum)

property img_raw: ndarray | None#

Returns the image without any convolutions applied

property img_signed: ndarray | None#

Returns a numpy view with a signed datatype (e.g. for calculating the diffImage).

img_view(mode: ImageView, fn_list: list[tuple[str, Callable[[AxisImage], AxisImage], bool, FunctionType | int]] | Literal['default'] | None = None, cache: bool = True) AxisImage#

Returns a view of the current image given an ImageView mode

invalidate_functions() None#

This function should be called when changes to the underlying img_functions and img_diff_functions objects were performed

property metadata: dict[str, Any] | None#
property name: str#

Get or set the name of the ImageObject

property name_without_extension: str#
open_file(path: Path | str, precompute: bool = False, run_async: bool = True) Task#

Open an image using a given path.

Parameters:
  • path (Path|str) – The path to the image file

  • precompute (bool) – Controls if the loaded image is also precomputed

  • run_async (bool) – Controls if the precomputation runs in a different thread

Returns Task:

The task object of this task

Raises:
  • AlreadyLoading – There is already a task working on this ImageObject

  • FileNotFoundError – Can’t find the file

  • UnsupportedImageError – The image is unsupported or has an error

  • ImageShapeError – The image has an invalid shape

property path: Path | None#

Returns the path of the current ImageObject or None if not originating from a file

precompute_image(task_continue: bool = False, run_async: bool = True) Task#

Precalculate the image views to prevent stuttering during runtime.

Parameters:
  • task_continue (bool) – This function supports the continuation of an existing task (for example from opening an image file)

  • async_mode (bool) – Controls if the precomputation runs in a different thread. Has no effect when task_continue is set

Returns Task:

The task object of this task

Raises:

AlreadyLoading – There is already a task working on this ImageObject

serialize(**kwargs) dict#

Serialize the current class object into a dict

set_image_precompute(img: ndarray, name: str | None = None, name_without_extension: str | None = None, run_async: bool = True) Task#

Set a new image with a given name and run precompute_image() on it.

Parameters:
  • img (np.ndarray) – The image

  • name (str) – Name of the image

  • async_mode (bool) – Controls if the precomputation runs in a different thread

Returns Task:

The task object of this task

Raises:

AlreadyLoading – There is already a task working on this ImageObject

property signal_obj: SignalObject#
sort_functions() None#
exception neurotorchmz.utils.image.ImageObjectError#

Bases: Exception

ImageObject Error

class neurotorchmz.utils.image.ImageProperties(img: ndarray | None)#

Bases: object

A class that supports lazy loading and caching of image properties like mean, median, std, min, max and clippedMin (=np.min(0, self.min)) Returns scalars (except for the img property, where it returns the image used to initializate this object.

property img: ndarray | None#
property max: floating | None#
property mean: floating | None#
property median: floating | None#
property min: floating | None#
property minClipped: floating | None#
property std: floating | None#
exception neurotorchmz.utils.image.ImageShapeError(shape: tuple | None = None, *args)#

Bases: ImageObjectError

The image has an invalid shape

class neurotorchmz.utils.image.ImageView(*values)#

Bases: Enum

Collapse a 3D image (t, y, x) into a smaller dimension

DEFAULT = (0, 1, 2)#

DEFAULT: Returns the original 3D image

SPATIAL = (0,)#

SPATIAL: Removes the temporal component and creates a 2D image

TEMPORAL = (1, 2)#

TEMPORAL: Removes the spatial component and creates an 1D time series

exception neurotorchmz.utils.image.NoImageError#

Bases: ImageObjectError

There is no img or img_diff loaded

exception neurotorchmz.utils.image.UnsupportedExtensionError#

Bases: ImageObjectError

Raised when the extension of the image is not supported

exception neurotorchmz.utils.image.UnsupportedImageError(msg: str | None = None, file_name: str | None = None, exception: Exception | None = None, *args)#

Bases: ImageObjectError

The image is unsupported