Base
Module: base.py
This module provides the abstract base class for all clustering implementations in SciREX. It defines shared functionality for: - Data preparation (loading from CSV and standard scaling) - Clustering metric computation (silhouette, calinski-harabasz, davies-bouldin) - 2D plotting using PCA for visualization
Classes:
Name | Description |
---|---|
Clustering |
Abstract base class that outlines common behavior for clustering algorithms. |
Dependencies
- numpy, pandas, matplotlib, sklearn
- abc, pathlib, time, typing (for structural and type support)
Key Features
- Consistent interface for loading and preparing data
- Standard approach to computing and returning clustering metrics
- PCA-based 2D plotting routine for visualizing clusters in two dimensions
- Enforces subclasses to implement
fit
andget_model_params
Version Info
- 28/Dec/2024: Initial version
Clustering
Bases: ABC
Abstract base class for clustering algorithms in the SciREX library.
This class provides
- A consistent interface for loading and preparing data
- A standard approach to computing and returning clustering metrics
- A PCA-based 2D plotting routine for visualizing clusters
Subclasses must
- Implement the
fit(X: np.ndarray) -> None
method, which should populateself.labels
. - Implement the
get_model_params() -> Dict[str, Any]
method, which returns a dict of model parameters for logging/debugging.
Attributes:
Name | Type | Description |
---|---|---|
model_type |
str
|
The name or identifier of the clustering model (e.g., "kmeans", "dbscan"). |
random_state |
int
|
Random seed for reproducibility. |
labels |
Optional[ndarray]
|
Array of cluster labels assigned to each sample after fitting. |
plots_dir |
Path
|
Directory where cluster plots will be saved. |
Source code in scirex/core/ml/unsupervised/clustering/base.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
|
__init__(model_type, random_state=42)
Initialize the base clustering class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_type
|
str
|
A string identifier for the clustering algorithm (e.g. "kmeans", "dbscan", etc.). |
required |
random_state
|
int
|
Seed for reproducibility where applicable. Defaults to 42. |
42
|
Source code in scirex/core/ml/unsupervised/clustering/base.py
fit(X)
abstractmethod
Fit the clustering model on a preprocessed dataset, assigning labels to self.labels
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
ndarray
|
A 2D array of shape (n_samples, n_features) containing the data to be clustered. |
required |
Subclasses must implement this method. After fitting the model,
self.labels
should be set to an array of cluster labels of shape (n_samples,).
Source code in scirex/core/ml/unsupervised/clustering/base.py
get_model_params()
abstractmethod
Return model parameters for logging or debugging.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: A dictionary containing key model parameters and potentially any learned attributes (e.g. number of clusters). |
Source code in scirex/core/ml/unsupervised/clustering/base.py
plots(X, labels)
Create a 2D scatter plot of clusters using PCA for dimensionality reduction.
Steps
- If X has >=2 features, run PCA to reduce it to 2 components.
- If X has only 1 feature, it is zero-padded to form a 2D embedding for plotting.
- Each unique cluster label is plotted with a distinct color.
- The figure is saved in
self.plots_dir
ascluster_plot_{self.model_type}.png
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
ndarray
|
Data array of shape (n_samples, n_features). |
required |
labels
|
ndarray
|
Cluster labels for each sample. |
required |
Returns:
Type | Description |
---|---|
Tuple[Figure, Path]
|
Tuple[Figure, Path]: - The matplotlib Figure object. - The path where the figure was saved (plot_path). |
Notes
Subclasses typically do not override this method. Instead, they rely on the base implementation for consistent plotting.
Source code in scirex/core/ml/unsupervised/clustering/base.py
prepare_data(path)
Load and preprocess data from a CSV file, returning a scaled NumPy array.
This method
- Reads the CSV file into a pandas DataFrame.
- Drops rows containing NaN values.
- Selects only numeric columns from the DataFrame.
- Scales these features using scikit-learn's StandardScaler.
- Returns the scaled values as a NumPy array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Filepath to the CSV data file. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A 2D array of shape (n_samples, n_features) containing standardized numeric data. |
Raises:
Type | Description |
---|---|
ValueError
|
If no numeric columns are found in the data. |
Source code in scirex/core/ml/unsupervised/clustering/base.py
run(data=None, path=None)
Run the complete clustering pipeline: data loading/preprocessing, fitting the model, and computing standard clustering metrics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Optional[ndarray]
|
Preprocessed data array of shape (n_samples, n_features). |
None
|
path
|
Optional[str]
|
Path to a CSV file from which to read data.
If |
None
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: A dictionary with the following keys:
- "params" (Dict[str, Any]): Model parameters from |
Raises:
Type | Description |
---|---|
ValueError
|
If neither |