dcnum.segm.segmenter

Attributes

cv2

ndi

STRUCTURING_ELEMENT

Exceptions

SegmenterNotApplicableError

Initialize self. See help(type(self)) for accurate signature.

Classes

Segmenter

Base segmenter class

Functions

assert_labels(arr)

Make sure a labeling array is returned

get_segmenters()

get_available_segmenters()

Return dictionary of available segmenters

Module Contents

dcnum.segm.segmenter.cv2
dcnum.segm.segmenter.ndi
dcnum.segm.segmenter.STRUCTURING_ELEMENT
exception dcnum.segm.segmenter.SegmenterNotApplicableError(segmenter_class, reasons_list)[source]

Bases: BaseException

Initialize self. See help(type(self)) for accurate signature.

reasons_list
segmenter_class
class dcnum.segm.segmenter.Segmenter(*, kwargs_mask: dict | None = None, debug: bool = False, **kwargs)[source]

Bases: abc.ABC

Base segmenter class

This is the base segmenter class for the multiprocessing operation segmenter segmenter_mpo.MPOSegmenter (multiple subprocesses are spawned and each of them works on a queue of images) and the single-threaded operation segmenter segmenter_sto.STOSegmenter (e.g. for batch segmentation on a GPU).

Parameters:
  • kwargs_mask (dict) – Keyword arguments for mask post-processing (see process_labels)

  • debug (bool) – Enable debugging mode (e.g. CPU segmenter runs in one thread)

  • kwargs – Additional, optional keyword arguments for segment_batch.

hardware_processor = 'none'

Required hardware (“cpu” or “gpu”) defined in first-level subclass.

mask_postprocessing = True

Whether to enable mask post-processing. If disabled, you should make sure that your mask is properly defined and cleaned or you have to call process_labels in your segment_algorithm implementation.

mask_default_kwargs

Default keyword arguments for mask post-processing. See process_labels for available options.

requires_background_correction = False

Whether the segmenter requires a background-corrected image

debug = False
logger
kwargs

custom keyword arguments for the subclassing segmenter

kwargs_mask

keyword arguments for mask post-processing

static get_border(shape)[source]

Cached boolean image with outer pixels set to True

static get_disk(radius)[source]

Cached skimage.morphology.disk(radius)

get_ppid()[source]

Return a unique segmentation pipeline identifier

The pipeline identifier is universally applicable and must be backwards-compatible (future versions of dcnum will correctly acknowledge the ID).

The segmenter pipeline ID is defined as:

KEY:KW_APPROACH:KW_MASK

Where KEY is e.g. “legacy” or “watershed”, and KW_APPROACH is a list of keyword arguments for segment_algorithm, e.g.:

thresh=-6^blur=0

which may be abbreviated to:

t=-6^b=0

KW_MASK represents keyword arguments for process_labels.

classmethod get_ppid_code()[source]

The unique code/name of this segmenter class

classmethod get_ppid_from_ppkw(kwargs, kwargs_mask=None)[source]

Return the pipeline ID from given keyword arguments

See also

get_ppid

Same method for class instances

static get_ppkw_from_ppid(segm_ppid)[source]

Return keyword arguments for this pipeline identifier

static is_available()[source]

Subclasses may override this method e.g. if dependencies exist

log_info(logger)[source]

Allow segmenter to write informative log messages

static process_labels(labels, *, clear_border: bool = True, fill_holes: bool = True, closing_disk: int = 5)[source]

Post-process retrieved mask image

This is an optional convenience method that is called for each subclass individually. To enable mask post-processing, set mask_postprocessing=True in the subclass and specify default mask_default_kwargs.

Parameters:
  • labels (2d uint16 or boolean ndarray) – Labeled input (contains blobs consisting of unique numbers)

  • clear_border (bool) – clear the image boarder using an equivalent of skimage.segmentation.clear_border()

  • fill_holes (bool) – binary-fill-holes in the binary mask image using scipy.ndimage.binary_fill_holes()

  • closing_disk (int or None) – if > 0, perform a binary closing with a disk of that radius in pixels

static segment_algorithm(image) numpy.ndarray[source]
Abstractmethod:

The segmentation algorithm implemented in the subclass

Perform segmentation and return boolean mask image

segment_algorithm_wrapper()[source]

Wraps self.segment_algorithm to only accept an image

The static method self.segment_algorithm may optionally accept keyword arguments self.kwargs. This wrapper returns the wrapped method that only accepts the image as an argument. This makes sense if you want to unify

abstractmethod segment_batch(images, bg_off=None)[source]

Return the bollean mask for an entire batch

This is implemented in the MPO and STO segmenters.

segment_batch_with_labeling(images, bg_off=None)[source]

Return the processed label image for an image batch

This is intended for testing only. The logic pipeline uses UniversalWorkers to perform labeling and label processing.

segment_chunk(chunk: int, slot_list: list)[source]

Segment the image data of one ChunkSlot

This is a wrapper for segment_batch. The image information are extracted from the chunk_slot.image/chunk_slot.image_corr properties and the boolean mask arrays are written to ``chunk_slot.mask`.

Parameters:

chunk_slot (ChunkSlot) – The data chunk to perform segmentation on

Returns:

mask – The chunk_slot.mask numpy view on the shared mask array.

Return type:

np.array

abstractmethod segment_single(image, bg_off: float | None = None)[source]

Return the boolean mask for one image

This is implemented in the MPO and STO segmenters.

segment_single_with_labeling(image, bg_off=None)[source]

Return the processed label image for one image

This is intended for testing only. The logic pipeline uses UniversalWorkers to perform labeling and label processing.

close()[source]

Subclasses can implement clean-up code here

classmethod validate_applicability(segmenter_kwargs: dict, meta: dict | None = None, logs: dict | None = None)[source]

Validate the applicability of this segmenter for a dataset

Parameters:
  • segmenter_kwargs (dict) – Keyword arguments for the segmenter

  • meta (dict) – Dictionary of metadata from an hdf5_data.HDF5Data instance

  • logs (dict) – Dictionary of logs from an hdf5_data.HDF5Data instance

Returns:

applicable – True if the segmenter is applicable to the dataset

Return type:

bool

Raises:

SegmenterNotApplicableError – If the segmenter is not applicable to the dataset

dcnum.segm.segmenter.assert_labels(arr)[source]

Make sure a labeling array is returned

If arr``is a boolean array, it is processed with `ndi.label.

dcnum.segm.segmenter.get_segmenters()[source]
dcnum.segm.segmenter.get_available_segmenters()[source]

Return dictionary of available segmenters