pyfli.phasor.phasorS.phasor_clustering#

Convert phasor images to feature matrices and back for PCA, clustering and LDA.

Functions

_as_stack(a)

_cluster_colors(n)

add_label_legend(ax, colors[, title, loc])

Legend of colors (from labels_to_rgb()) on ax, placed at loc.

align_labels(predicted, reference)

Rename cluster ids so they best match reference labels (Hungarian assignment on the contingency table).

cluster_summary(G1, S1, label_map, phasor[, ...])

Per-cluster summary of a first-harmonic phasor image: pixel count, mean G / S, the phase and modulation lifetimes of the cluster-mean phasor -- computed with the given pyfli.phasor.phasorS.PhasorAnalyzer (compute_lifetime / compute_modulation_lifetime) -- and the mean of every map in param_maps (e.g. the simulator's ground-truth maps) inside the cluster.

clustering_accuracy(predicted, reference)

Fraction of pixels whose (aligned) cluster matches the reference label.

image_to_labels(label_map, pixels)

Read a label (e.g. the ground-truth region id) for every pixel in pixels.

labels_to_image(labels, pixels, shape[, fill])

Place per-pixel labels at their pixels coordinates in an image of shape.

labels_to_rgb(label_map[, background])

(H, W, 3) RGB image with one categorical color per label (background black), and the {label: color} legend.

phasor_feature_matrix(G, S[, mask, ...])

One feature row per pixel from multi-harmonic phasor coordinates.

plot_explained_variance(ratio[, ax, title])

Bar chart of per-component explained-variance ratio with the cumulative curve.

plot_scores(scores[, labels, ax, ...])

Scatter of the first two columns of scores (PCA or LDA projections), colored by labels when given.

phasor_feature_matrix(G, S, mask=None, harmonics=(1,), polar=False, extra_maps=None)[source]#

One feature row per pixel from multi-harmonic phasor coordinates.

Parameters:
  • G (np.ndarray) – Phasor coordinates, (n_harmonics, H, W) or (H, W).

  • S (np.ndarray) – Phasor coordinates, (n_harmonics, H, W) or (H, W).

  • mask (np.ndarray | None) – Pixels to use ((H, W) boolean); default all. Pixels with any non-finite feature are dropped as well.

  • harmonics (iterable of int) – 1-based harmonics to include.

  • polar (bool) – Use phase atan2(S, G) and modulation sqrt(G^2 + S^2) instead of G / S.

  • extra_maps (dict[str, np.ndarray] | None) – Additional (H, W) maps appended as features (e.g. {"log_intensity": np.log1p(decay.sum(-1))}).

Returns:

(X, pixels, names): features (n_pixels, n_features), the (row, col) coordinates of every row, and the feature names.

Return type:

tuple[np.ndarray, np.ndarray, list[str]]

labels_to_image(labels, pixels, shape, fill=-1)[source]#

Place per-pixel labels at their pixels coordinates in an image of shape.

Parameters:
Return type:

ndarray

image_to_labels(label_map, pixels)[source]#

Read a label (e.g. the ground-truth region id) for every pixel in pixels.

Parameters:
Return type:

ndarray

align_labels(predicted, reference)[source]#

Rename cluster ids so they best match reference labels (Hungarian assignment on the contingency table). Cluster ids are arbitrary; this makes them comparable with ground truth. Returns (aligned_labels, mapping); clusters without a partner keep their own id.

Parameters:
Return type:

tuple[ndarray, dict]

clustering_accuracy(predicted, reference)[source]#

Fraction of pixels whose (aligned) cluster matches the reference label.

Parameters:
Return type:

float

cluster_summary(G1, S1, label_map, phasor, param_maps=None, background=-1, intensity=None)[source]#

Per-cluster summary of a first-harmonic phasor image: pixel count, mean G / S, the phase and modulation lifetimes of the cluster-mean phasor – computed with the given pyfli.phasor.phasorS.PhasorAnalyzer (compute_lifetime / compute_modulation_lifetime) – and the mean of every map in param_maps (e.g. the simulator’s ground-truth maps) inside the cluster. Pixels labelled background are ignored.

By default every pixel counts equally. Pass the photon-count image as intensity to weight the mean phasor by photons instead: it is then exactly the phasor of the cluster’s summed decay (phasors add with intensity weights).

Parameters:
Return type:

DataFrame

labels_to_rgb(label_map, background=-1)[source]#

(H, W, 3) RGB image with one categorical color per label (background black), and the {label: color} legend. Pass the image as colors= to pyfli.phasor.phasorS.PhasorAnalyzer.plot_phasor_diagram() to color the phasor points by cluster, or show it with imshow.

Parameters:
Return type:

tuple[ndarray, dict]

add_label_legend(ax, colors, title=None, loc='upper right')[source]#

Legend of colors (from labels_to_rgb()) on ax, placed at loc.

Parameters:
Return type:

Any

plot_scores(scores, labels=None, ax=None, axis_names=('component 1', 'component 2'), title='Scores', max_points=20000, seed=0)[source]#

Scatter of the first two columns of scores (PCA or LDA projections), colored by labels when given. A single-column projection is drawn as per-label histograms.

Parameters:
Return type:

Any

plot_explained_variance(ratio, ax=None, title='Explained variance')[source]#

Bar chart of per-component explained-variance ratio with the cumulative curve.

Parameters:
Return type:

Any