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:
objectRun a list of analysis modules on a stack, in order.
Add steps with
add(), then callrun(). Each step names an analysis module, either a built-in one (seeplaynano.analysis.BUILTIN_ANALYSIS_MODULES) or a plugin registered under theplaynano.analysisentry point group. Each step’s outputs are stored instack.analysis["step_<n>_<module_name>"], and its parameters, time and module version instack.provenance["analysis"].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 untilrun().**params – Keyword arguments passed to the module’s
runmethod. Ifmask_fnis a str, it is replaced by the masking function of that name fromplaynano.processing.mask_generatorswhen 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})
- 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 ofrunare discarded:stack.analysisandstack.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>", withncounting from 1 and spaces in the name replaced by_. The same dict asstack.analysis."provenance"The same dict as
stack.provenance["analysis"], with:"steps": one dict per step, withindex,name,params(as passed to the module, so amask_fnname appears as the function),timestamp(ISO 8601, UTC),version(the module’sversion, or None) andanalysis_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:
- Raises:
ValueError – If a module name is not found, or
mask_fnnames no masking function.RuntimeError – If a module declares
requiresand 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