Novelty Clustering¶
novelentitymatcher.novelty.clustering.base
¶
Abstract contract for clustering backends.
Classes¶
ClusteringBackend
¶
Bases: ABC
Abstract contract for clustering backends.
Functions¶
fit_predict(embeddings, min_cluster_size=5, **kwargs)
abstractmethod
¶
Fit and predict cluster labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embeddings
|
ndarray
|
Input embeddings (n_samples, dim). |
required |
min_cluster_size
|
int
|
Minimum points to form a cluster. |
5
|
**kwargs
|
Any
|
Backend-specific parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
(labels, probabilities, info_dict) |
ndarray
|
|
dict[str, Any]
|
|
tuple[ndarray, ndarray, dict[str, Any]]
|
|
Source code in src/novelentitymatcher/novelty/clustering/base.py
novelentitymatcher.novelty.clustering.backends
¶
Concrete clustering backend implementations and registry.
Classes¶
ClusteringBackendRegistry
¶
Registry for clustering backends.
HDBSCANBackend(min_samples=5, cluster_selection_epsilon=0.0, metric='cosine', prediction_data=True)
¶
Bases: ClusteringBackend
HDBSCAN clustering backend.
Source code in src/novelentitymatcher/novelty/clustering/backends.py
SOPTICSBackend(min_samples=5, metric='cosine')
¶
UMAPHDBSCANBackend(min_samples=5, cluster_selection_epsilon=0.0, n_neighbors=15, umap_dim=10, umap_metric='cosine', prediction_data=True)
¶
Bases: ClusteringBackend
UMAP preprocessing followed by HDBSCAN clustering backend.
Source code in src/novelentitymatcher/novelty/clustering/backends.py
Functions¶
novelentitymatcher.novelty.clustering.scalable
¶
Scalable density-based clustering for novelty detection.
Supports HDBSCAN, sOPTICS (accelerated), and UMAP-preprocessed clustering for handling up to 1M scale with subquadratic runtime.
Classes¶
ScalableClusterer(backend='auto', min_cluster_size=5, min_samples=5, cluster_selection_epsilon=0.0, n_neighbors=15, umap_dim=10, umap_metric='cosine', prediction_data=True)
¶
Wrapper for scalable density-based clustering.
Supports: - HDBSCAN: Standard hierarchical DBSCAN (best for <100K points) - sOPTICS: LSH-accelerated OPTICS (for 100K-1M points) - UMAP+HDBSCAN: UMAP dimensionality reduction before HDBSCAN - Auto: Automatic backend selection based on dataset size
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
str
|
Clustering backend ('hdbscan', 'soptics', 'umap_hdbscan', 'auto') |
'auto'
|
min_cluster_size
|
int
|
Minimum points to form a cluster. |
5
|
min_samples
|
int
|
Min samples for core distance (OPTICS). |
5
|
cluster_selection_epsilon
|
float
|
Distance threshold for cluster selection. |
0.0
|
n_neighbors
|
int
|
Neighbors for UMAP (if used). |
15
|
umap_dim
|
int
|
Target dimensionality for UMAP preprocessing. |
10
|
umap_metric
|
str
|
Metric for UMAP. |
'cosine'
|
prediction_data
|
bool
|
Whether to compute prediction_data for HDBSCAN. |
True
|
Source code in src/novelentitymatcher/novelty/clustering/scalable.py
Attributes¶
labels
property
¶
Get cluster labels.
probabilities
property
¶
Get cluster membership probabilities.
Functions¶
fit_predict(embeddings, metric='cosine')
¶
Fit clusterer and predict labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embeddings
|
ndarray
|
Input embeddings (n_samples, dim) |
required |
metric
|
str
|
Distance metric ('cosine', 'euclidean', 'precomputed') |
'cosine'
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray, dict[str, Any]]
|
Tuple of (cluster_labels, probabilities, validation_info) |
Source code in src/novelentitymatcher/novelty/clustering/scalable.py
fit(embeddings, metric='cosine')
¶
Fit the clusterer (alias for compatibility).
get_cluster_members(cluster_id)
¶
Get indices of members in a specific cluster.
Source code in src/novelentitymatcher/novelty/clustering/scalable.py
get_noise_points()
¶
Get indices of noise points (label = -1).
Functions¶
compute_cluster_quality(embeddings, labels, known_embeddings=None, metric='cosine')
¶
Compute quality metrics for discovered clusters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embeddings
|
ndarray
|
Cluster member embeddings (n_cluster, dim) |
required |
labels
|
ndarray
|
Cluster labels for all points (n_total,) |
required |
known_embeddings
|
ndarray | None
|
Optional known entity embeddings for ratio calculation |
None
|
metric
|
str
|
Distance metric |
'cosine'
|
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary with quality metrics: |
dict[str, float]
|
|
dict[str, float]
|
|
dict[str, float]
|
|
dict[str, float]
|
|
Source code in src/novelentitymatcher/novelty/clustering/scalable.py
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 | |
validate_novel_cluster(cluster_embeddings, known_embeddings, cohesion_threshold=0.45, known_ratio_threshold=0.4, min_cluster_size=5, metric='cosine')
¶
Validate that a cluster represents truly novel entities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cluster_embeddings
|
ndarray
|
Embeddings of cluster members |
required |
known_embeddings
|
ndarray
|
Embeddings of known entities |
required |
cohesion_threshold
|
float
|
Max avg pairwise distance within cluster |
0.45
|
known_ratio_threshold
|
float
|
Max fraction that should be close to known |
0.4
|
min_cluster_size
|
int
|
Minimum required members |
5
|
metric
|
str
|
Distance metric |
'cosine'
|
Returns:
| Type | Description |
|---|---|
tuple[bool, float]
|
Tuple of (is_valid_novel, validation_score) |
Source code in src/novelentitymatcher/novelty/clustering/scalable.py
novelentitymatcher.novelty.clustering.params
¶
Pydantic parameter models for clustering backends.
Provides clean, validated configuration objects for each clustering backend, supporting benchmark sweeps over parameter combinations.
novelentitymatcher.novelty.clustering.validation
¶
Cluster validation logic for novelty detection.
This module provides utilities for validating clustering results and assessing cluster quality for novelty detection.
Classes¶
ClusterValidator(min_cohesion_threshold=0.45, min_persistence_threshold=0.1)
¶
Validates clustering results for novelty detection.
Provides metrics and validation methods to assess cluster quality and determine if samples represent novel clusters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_cohesion_threshold
|
float
|
Minimum cohesion for valid clusters |
0.45
|
min_persistence_threshold
|
float
|
Minimum persistence for valid clusters |
0.1
|
Source code in src/novelentitymatcher/novelty/clustering/validation.py
Functions¶
compute_cohesion(embeddings, labels, cluster_id)
¶
Compute cluster cohesion (compactness).
Cohesion is the average pairwise similarity within a cluster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embeddings
|
ndarray
|
All embeddings |
required |
labels
|
ndarray
|
Cluster labels for each embedding |
required |
cluster_id
|
int
|
Cluster to compute cohesion for |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cohesion score (0-1, higher = more compact) |
Source code in src/novelentitymatcher/novelty/clustering/validation.py
compute_separation(embeddings, labels, cluster_id)
¶
Compute cluster separation (distinctiveness from other clusters).
Separation is the minimum average distance to another cluster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embeddings
|
ndarray
|
All embeddings |
required |
labels
|
ndarray
|
Cluster labels for each embedding |
required |
cluster_id
|
int
|
Cluster to compute separation for |
required |
Returns:
| Type | Description |
|---|---|
float
|
Separation score (0-1, higher = more separated) |
Source code in src/novelentitymatcher/novelty/clustering/validation.py
is_valid_cluster(embeddings, labels, cluster_id, min_size=5)
¶
Determine if a cluster is valid (stable and meaningful).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embeddings
|
ndarray
|
All embeddings |
required |
labels
|
ndarray
|
Cluster labels |
required |
cluster_id
|
int
|
Cluster to validate |
required |
min_size
|
int
|
Minimum number of samples for valid cluster |
5
|
Returns:
| Type | Description |
|---|---|
bool
|
True if cluster is valid |
Source code in src/novelentitymatcher/novelty/clustering/validation.py
get_cluster_statistics(embeddings, labels)
¶
Compute statistics for all clusters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embeddings
|
ndarray
|
All embeddings |
required |
labels
|
ndarray
|
Cluster labels |
required |
Returns:
| Type | Description |
|---|---|
dict[int, dict[str, float]]
|
Dict mapping cluster_id to statistics dict |