Clustering

Clustering algorithms to find events with the same energy/position.

DBSCAN and OPTICS return a variable number of clusters, meaning lines with similar energies merge into the same group. To accommodate this, a minimum number of clusters can be specified. K-Means is then applied to separate the lines.

Algorithms here should be a child of the scikit-learn’s ClusterMixin.

class x_ray_imager_bagriff.identify_lines._cluster.KMeansMinMixin

Mixin to guarantee a min number of clusters.

If too few clusters are found, K-means splits all events, other than noise, into the min number of groups.

__init__(min_clusters, **kwargs)

Initialize the k-means clustering object.

Parameters:
  • min_clusters (int) – Minimum number of clusters returned. Sets the threshold to apply k-means.

  • **kwargs – Passed to the KMeans constructor. See sklearn.cluster.KMeans for more details.

Return type:

None

fit_min(X, y=None)

Check if the min number of clusters is met, and apply K-means if not.

This should be called in the subclass after that clustering is completed. For example:

super().fit(X, y, **kwargs)
self.fit_min(X, y, **kwargs)
Parameters:
  • X (NDArray[Any]) – The data to cluster. Shape should be (n_samples, n_features).

  • y (Any) – Passed to the clustering fit(), but not used. Kept for consistency with Scikit-learn’s clustering algorithms.

class x_ray_imager_bagriff.identify_lines._cluster.MinDBSCAN

DBSCAN with a minimum number of clusters.

__init__(min_clusters, kmeans_kwargs=None, **kwargs)

Initialize DBSCAN and set min clusters returned.

Parameters:
  • min_clusters (int) – Minimum number of clusters returned, passed to the KMeansMinMixin.

  • kmeans_kwargs (dict | None) – Keyword arguments passed to KMeansMinMixin to the sklearn.cluster.KMeans constructor.

  • **kwargs – Passed to the DBSCAN constructor. See sklearn.cluster.DBSCAN for more details.

Return type:

None

fit(X, y=None, sample_weight=None)

Fit DBSCAN and check if the min number of clusters is met.

Parameters:
  • X (NDArray[Any]) – The data to cluster. Shape should be (n_samples, n_features).

  • y (Any) – Passed to the clustering fit(), but not used. Kept for consistency with Scikit-learn’s clustering algorithms.

  • sample_weight (NDArray[Any] | None) – Passed to the clustering fit(), but not used. Kept for consistency with Scikit-learn’s clustering algorithms.

Return type:

MinDBSCAN

set_fit_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

  • self (MinDBSCAN)

Returns:

self – The updated object.

Return type:

object

class x_ray_imager_bagriff.identify_lines._cluster.MinOPTICS

OPTICS with a minimum number of clusters.

__init__(min_clusters, kmeans_kwargs=None, **kwargs)

Initialize OPTICS and set min clusters returned.

Parameters:
  • min_clusters (int) – Minimum number of clusters returned, passed to the KMeansMinMixin.

  • kmeans_kwargs (dict | None) – Keyword arguments passed to KMeansMinMixin to the sklearn.cluster.KMeans constructor.

  • min_cluster_size – int > 1 or float between 0 and 1. Minimum number of samples in an OPTICS cluster, expressed as an absolute number or a fraction of the number of samples. Not just used for xi.

  • **kwargs – Passed to the OPTICS constructor. See sklearn.cluster.OPTICS for more details.

Return type:

None

fit(X, y=None, **kwargs)

Fit OPTICS and check if the min number of clusters is met.

Parameters:
  • X (NDArray[Any]) – The data to cluster. Shape should be (n_samples, n_features).

  • y (Any) – Passed to the clustering fit(), but not used. Kept for consistency with Scikit-learn’s clustering algorithms.

  • sample_weight – Passed to the clustering fit(), but not used. Kept for consistency with Scikit-learn’s clustering algorithms.