amep.continuum.identify_clusters#

amep.continuum.identify_clusters(dfield: ndarray, scale: float = 1.0, pbc: bool = True, cutoff: float = 1.0, threshold: float = 0.5, method: str = 'threshold') tuple[ndarray, ndarray]#

Identify clusters in a continuum field. Uses either the watershed algorithm or a threshold method. The threshold method creates a black and white image by separating pixels with values below a threshold and those with values above. Then it indexes each continous pixel group of the same value with a number. The watershed method is taken from scikit image.

Parameters:
  • dfield (np.ndarray of shape (N,M)) – Two dimensional density field.

  • scale (float, optional) – The scale of the field. This is needed to improve the segmentation done in the watershed algorithm. This keyword is ignored when using method=’threshold’. The default is 1.0. Can also be set to negative value to make bubble detection accesible.

  • pbc (bool, optional) – Whether to use periodic boundary conditions. Default is True.

  • cutoff (float, optional) – Caps the highest field values to the ratio of the data extent. Default value 1.0 means no cut, 0.9 means the highest and lowest 10% of the field get cut off. Needed for clusters with multiple local minima. Combats oversegmentation. This keyword is ignored when using method=’threshold’. The default is 1.0.

  • threshold (float, optional) – Relative threshold for method=’threshold’. Searches for connected regions in which the relative values are larger than the threshold, i.e., larger than threshold*(np.max(dfield)-np.min(dfield)). This keyword is ignored if method=’watershed’. The default is 0.5.

  • method (str, optional) – Chooses between ‘watershed’ and ‘threshold’ method. The default is ‘threshold’.

Returns:

  • ids (np.ndarray) – An array of the cluster IDs, starting from 0.

  • labels (np.ndarray of shape (N,M)) – Array of the same shape as the input field, where each pixel or grid point is labeled with the cluster ID it belongs to.

Examples

>>> import amep
>>> import numpy as np
>>> X, Y = np.meshgrid(
...     np.linspace(0, np.pi, 101), np.linspace(0, np.pi, 101)
... )
>>> dfield = np.cos(2*np.pi*X)*np.cos(2*np.pi*Y)
>>> ids, labels = amep.continuum.identify_clusters(
...     dfield, scale=1.0, cutoff=0.8, threshold=0.2,
...     method="threshold", pbc=True
... )
>>> print("Cluster ids: ", ids)
Cluster ids:  [ 0  1  2  3  5  6  7  8  9 10 12 13 14 15 16 17 19 20 21]
>>> fig, axs = amep.plot.new(ncols=2, figsize=(7, 3))
>>> mp1 = amep.plot.field(axs[0], dfield, X, Y, cmap="viridis")
>>> cax1 = amep.plot.add_colorbar(fig, axs[0], mp1, label=r"$\rho$")
>>> axs[0].set_title("density field")
>>> axs[0].set_xlabel(r"$x$")
>>> axs[0].set_ylabel(r"$y$")
>>> mp2 = amep.plot.field(axs[1], labels, X, Y, cmap="nipy_spectral")
>>> cax2 = amep.plot.add_colorbar(fig, axs[1], mp2, label="cluster id")
>>> axs[1].set_title("clusters")
>>> axs[1].set_xlabel(r"$x$")
>>> axs[1].set_ylabel(r"$y$")
>>> fig.savefig("./figures/continuum/continuum-identify_clusters.png")
../_images/continuum-identify_clusters.png