playnano.analysis.pipeline module

Run a sequence of analysis modules on an AFM image stack.

AnalysisPipeline runs analysis modules (subclasses of AnalysisModule) in order and records their outputs and provenance. It is what playnano analyze uses.

class playnano.analysis.pipeline.AnalysisPipeline[source]

Bases: object

Run a list of analysis modules on a stack, in order.

Add steps with add(), then call run(). Each step names an analysis module, either a built-in one (see playnano.analysis.BUILTIN_ANALYSIS_MODULES) or a plugin registered under the playnano.analysis entry point group. Each step’s outputs are stored in stack.analysis["step_<n>_<module_name>"], and its parameters, time and module version in stack.provenance["analysis"].

steps

The steps added, as (module_name, params) pairs.

Type:

list of tuple

Examples

Detect a particle that moves across three frames, then link it into a track:

>>> import numpy as np
>>> from playnano.afm_stack import AFMImageStack
>>> from playnano.analysis.pipeline import AnalysisPipeline
>>> data = np.zeros((3, 64, 64))
>>> for t in range(3):
...     data[t, 10 + t : 16 + t, 10:16] = 3.0
>>> stack = AFMImageStack(
...     data,
...     pixel_size_nm=2.0,
...     channel="height_trace",
...     file_path="demo.h5-jpk",
...     frame_metadata=[{"timestamp": 0.5 * t} for t in range(3)],
... )
>>> pipeline = AnalysisPipeline()
>>> pipeline.add("feature_detection", mask_fn="mask_threshold", threshold=1.0)
>>> pipeline.add("particle_tracking", max_distance=5)
>>> record = pipeline.run(stack)
>>> list(record["analysis"])
['step_1_feature_detection', 'step_2_particle_tracking']
>>> record["analysis"]["step_1_feature_detection"]["summary"]
{'total_frames': 3, 'total_features': 3, 'avg_features_per_frame': 1.0}
>>> record["analysis"]["step_2_particle_tracking"]["n_tracks"]
1
add(module_name: str, **params) → None[source]

Add a step that runs an analysis module.

Parameters:
  • module_name (str) – Name of the analysis module, for example "feature_detection". It is not checked until run().

  • **params – Keyword arguments passed to the module’s run method. If mask_fn is a str, it is replaced by the masking function of that name from playnano.processing.mask_generators when the step runs.

Examples

>>> pipeline = AnalysisPipeline()
>>> pipeline.add("feature_detection", mask_fn="mask_threshold", threshold=1.0)
>>> pipeline.add("particle_tracking", max_distance=5)
>>> pipeline.steps[1]
('particle_tracking', {'max_distance': 5})
clear() → None[source]

Remove all steps, so the pipeline can be set up again.

run(stack: AFMImageStack, log_to: str | None = None) → dict[str, Any][source]

Run every step on a stack and return the results.

Each step’s module receives the stack, the step’s parameters and previous_results: a dict mapping the name of each module run so far to its latest outputs. Results from an earlier call of run are discarded: stack.analysis and stack.provenance["analysis"] are replaced.

Parameters:
  • stack (AFMImageStack) – The stack to analyse.

  • log_to (str, optional) – If given, also write the record to this JSON file, creating missing folders. Arrays are summarised in the file (shape, dtype, minimum, maximum and mean), not written in full; see sanitize_analysis_for_logging(). Default is None.

Returns:

The analysis record, with these keys:

"environment"

Python, platform and package versions; the same dict as stack.provenance["environment"], which is only set if empty.

"analysis"

The outputs of each step, keyed "step_<n>_<module_name>", with n counting from 1 and spaces in the name replaced by _. The same dict as stack.analysis.

"provenance"

The same dict as stack.provenance["analysis"], with:

  • "steps": one dict per step, with index, name, params (as passed to the module, so a mask_fn name appears as the function), timestamp (ISO 8601, UTC), version (the module’s version, or None) and analysis_key.

  • "results_by_name": each module name mapped to a list with one {"analysis_key": ..., "outputs": ...} dict per time it ran.

  • "frame_times": the stack’s frame times, in seconds.

Return type:

dict

Raises:
  • ValueError – If a module name is not found, or mask_fn names no masking function.

  • RuntimeError – If a module declares requires and none of those modules ran before it, or a plugin module fails to load.

  • TypeError – If a plugin is not an AnalysisModule.

  • Exception – Any error raised by a module is logged and raised again.

playnano.analysis.pipeline.AnalysisRecord

a dict with the keys "environment", "analysis" and "provenance", described there.

Type:

Type of the record AnalysisPipeline.run() returns

alias of dict[str, Any]