playnano.processing.pipeline module

The ProcessingPipeline class, which runs processing steps on an AFMImageStack.

A pipeline runs masks, 2D filters, video processing and stack edits in order. It stores each step’s output in stack.processed (or stack.masks for masks) and records what was done in stack.provenance["processing"], and the software environment in stack.provenance["environment"].

class playnano.processing.pipeline.ProcessingPipeline(stack: AFMImageStack)[source]

Bases: object

Run a sequence of processing steps on an AFMImageStack and record provenance.

Steps are added with add_filter(), add_mask() and clear_mask(), and run in order by run(). Each step is a name and keyword arguments; the name decides what kind of step it is:

  • "clear": removes the current mask.

  • A mask (see mask_generators): makes a boolean mask, stored in stack.masks. Masks made one after another are combined with logical OR until the next clear.

  • A 2D filter (see filters): applied to every frame. While a mask is active, the masked version from masked_filters is used if there is one.

  • A video processing step (see video_processing): applied to the whole stack.

  • A stack edit (see stack_edit): removes frames and the matching frame metadata. It also clears the current mask.

  • A method of AFMImageStack, or a filter or video processing plugin registered under the playnano.filters or playnano.video_processing entry point group.

Parameters:

stack (AFMImageStack) – The stack to process. It is changed in place by run().

See also

playnano.afm_stack.AFMImageStack.apply

Run a list of steps in one call.

Examples

>>> from playnano.afm_stack import AFMImageStack
>>> from playnano.processing.pipeline import ProcessingPipeline
>>> stack = AFMImageStack.load_data("tests/resources/sample_0.h5-jpk")
>>> pipeline = ProcessingPipeline(stack)
>>> (pipeline.add_filter("remove_plane")
...          .add_mask("mask_threshold", threshold=1.0)
...          .add_filter("polynomial_flatten", order=2)
...          .clear_mask())
<playnano.processing.pipeline.ProcessingPipeline object at ...>
>>> result = pipeline.run()
>>> sorted(stack.processed)
['raw', 'step_1_remove_plane', 'step_3_polynomial_flatten']
>>> sorted(stack.masks)
['step_2_mask_threshold']
add_filter(filter_name: str, **kwargs) → ProcessingPipeline[source]

Add a processing step: a 2D filter, video processing step, stack edit or plugin.

Parameters:
  • filter_name (str) – Name of the step, for example "remove_plane" or "align_frames".

  • **kwargs – Arguments for the step, for example order=2.

Returns:

This pipeline, so calls can be chained.

Return type:

ProcessingPipeline

Notes

If a mask is active when a 2D filter runs, the masked version of the filter is used if there is one; otherwise the filter is applied to the whole frame. The step is identified by its name when the pipeline runs, so this does the same as add_mask().

add_mask(mask_name: str, **kwargs) → ProcessingPipeline[source]

Add a mask step.

Parameters:
  • mask_name (str) – Name of the mask, for example "mask_threshold".

  • **kwargs – Arguments for the mask function, for example threshold=1.0.

Returns:

This pipeline, so calls can be chained.

Return type:

ProcessingPipeline

Notes

If a mask is already active, the new mask is combined with it by logical OR. The step is identified by its name when the pipeline runs, so this does the same as add_filter(); the two names only make pipelines easier to read.

clear_mask() → ProcessingPipeline[source]

Add a step that removes the current mask.

Filters after this step are applied to whole frames until a new mask is added.

Returns:

This pipeline, so calls can be chained.

Return type:

ProcessingPipeline

run() → ndarray[source]

Run the steps in order on the stack.

Before the first step, the current data is saved as stack.processed["raw"] if there is no such entry yet. After the last step the result replaces stack.data.

Each step’s output is stored under a key "step_<n>_<name>", where n counts from 1:

  • 2D filters and video processing steps: in stack.processed.

  • Stack edits: in stack.processed, always named "step_<n>_drop_frames".

  • Masks: in stack.masks. A mask combined with an earlier one is named after both, for example "step_3_mask_threshold_mask_below_threshold".

The provenance of each run replaces that of any earlier run:

  • stack.provenance["processing"]["steps"]: one record per step, with its index, name, parameters, timestamp, step type, version, function module, the key of its output (processed_key or mask_key) and a summary of the output’s shape and dtype. Video processing steps also store their metadata.

  • stack.provenance["processing"]["keys_by_name"]: step name to the keys of its outputs, in order.

  • stack.provenance["environment"]: Python, platform and package versions.

Returns:

The processed stack, also stored in stack.data.

Return type:

numpy.ndarray

Raises:
  • ValueError – If a step name is not recognised.

  • Exception – Any error raised by a step is logged and raised again; steps already run keep their outputs.