playnano.analysis.modules.k_means_clustering module

Analysis module that groups detected features into k clusters with k-means.

class playnano.analysis.modules.k_means_clustering.KMeansClusteringModule[source]

Bases: AnalysisModule

Group detected features from all frames into k clusters with k-means.

Pipeline name: "k_means_clustering". It needs the output of feature_detection from an earlier step.

Each detected feature becomes one point. By default its coordinates are the feature’s centroid, (row, col) in pixels, with the frame time in seconds added as a third coordinate. With normalise (the default), each coordinate is rescaled to the range 0 to 1 before clustering.

Every point is put in one of k clusters, so that points are as close as possible to their cluster’s centre. The random seed is fixed (random_state=42), so results are repeatable. To find the number of clusters from the data, use x_means_clustering or dbscan_clustering.

Examples

Two particles over 4 frames, one moving to the right:

>>> import numpy as np
>>> from playnano.afm_stack import AFMImageStack
>>> from playnano.analysis.modules.feature_detection import (
...     FeatureDetectionModule,
... )
>>> data = np.zeros((4, 64, 64))
>>> for t in range(4):
...     data[t, 10:16, 10 + t : 16 + t] = 3.0
...     data[t, 40:46, 40:46] = 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(4)],
... )
>>> detected = FeatureDetectionModule().run(
...     stack, mask_fn="mask_threshold", threshold=1.0
... )
>>> previous = {"feature_detection": detected}
>>> from playnano.analysis.modules.k_means_clustering import (
...     KMeansClusteringModule,
... )
>>> result = KMeansClusteringModule().run(stack, previous, k=2)
>>> result["summary"]
{'n_clusters': 2, 'members_per_cluster': {0: 4, 1: 4}}
>>> result["cluster_centers"].round(2)
array([[12.5 , 14.  ,  0.75],
       [42.5 , 42.5 ,  0.75]])

See also

sklearn.cluster.KMeans

The clustering used.

property name: str

Name of this module in a pipeline, "k_means_clustering".

requires = ['feature_detection', 'log_blob_detection']
run(stack, previous_results: dict[str, Any] | None = None, *, detection_module: str = 'feature_detection', coord_key: str = 'features_per_frame', coord_columns: Sequence[str] = ('centroid_x', 'centroid_y'), use_time: bool = True, k: int, normalise: bool = True, time_weight: float | None = None, **kmeans_kwargs: Any) → dict[str, Any][source]

Cluster the detected features into k clusters with k-means.

Parameters:
  • stack (AFMImageStack) – The stack; used for the frame times.

  • previous_results (dict) – Outputs of earlier steps. Must hold a detection output.

  • detection_module (str, optional) – Name of the detection step to use. If it has not run, log_blob_detection is tried, then feature_detection. Default is "feature_detection".

  • coord_key (str, optional) – Key of the per-frame feature lists in the detection output. Default is "features_per_frame".

  • coord_columns (list or tuple of str, optional) – Keys of each feature’s coordinates. If a feature lacks them, the first two values of its centroid, (row, col), are used instead. feature_detection has no centroid_x or centroid_y, so this is what happens with the default. Default is ("centroid_x", "centroid_y").

  • use_time (bool, optional) – Add each feature’s frame time, in seconds, as a third coordinate (only when there are two coordinates). Default is True.

  • k (int) – Number of clusters. Required; must not be more than the number of points.

  • normalise (bool, optional) – Rescale each coordinate to the range 0 to 1 (minimum to maximum over all points) before clustering. Default is True.

  • time_weight (float, optional) – Multiply the rescaled time coordinate by this, so time counts more (above 1) or less (below 1) than position. Must be positive. Only used with normalise and a time coordinate. Default is None (no weighting).

  • **kmeans_kwargs – Passed to sklearn.cluster.KMeans, for example n_init. random_state cannot be passed; it is fixed at 42.

Returns:

"clusters"

One dict per cluster, with id (int), and for each member point frames (frame index), point_indices (index in that frame’s feature list) and coords (the point’s coordinates as clustered, so rescaled if normalise).

"cluster_centers"

Array of shape (n_clusters, n_coordinates): the centre of each cluster, in the original units (pixels, and seconds for time).

"summary"

n_clusters and members_per_cluster (cluster ID to number of points).

Return type:

dict

Raises:
  • RuntimeError – If previous_results holds no detection output.

  • KeyError – If the detection output lacks coord_key, or a feature has neither coord_columns nor centroid.

  • ValueError – If k is more than the number of points.

version = '0.1.0'