Comparaison automatisée d'algorithmes de clustering pour données de spatial proteomics / cytométrie
Project description
spatial-cluster-compare
Automated clustering algorithm comparison pipeline for spatial proteomics, cytometry data, or any numerical feature table.
Originally an analysis notebook, restructured into a tested, reusable Python package.
Table of contents
- Why this package
- Part of MACSima_Spatial-Omics-Pipeline
- What the pipeline does
- Installation
- Quick usage
- Advanced usage (step by step)
- Expected data format
- Roadmap
Why this package
Choosing a clustering algorithm is often an arbitrary decision: KMeans gets picked by default, parameters get tuned until the result "looks right," and the choice is rarely backed by an objective comparison. For biological data in particular where clusters are expected to reflect real cell populations this matters: the wrong algorithm or the wrong number of clusters can produce plausible-looking but biologically meaningless groups.
spatial-cluster-compare runs several clustering algorithms on the same data,
scores each one on multiple complementary metrics (including bootstrap
stability, not just a single internal index), and picks the best method
through a transparent, reproducible composite score — instead of relying on a
single, unverified default choice.
Part of MACSima_Spatial-Omics-Pipeline
spatial-cluster-compare is one of the packages of
MACSima_Spatial-Omics-Pipeline,
which centralizes the protocols (detailed, step-by-step documentation) and
notebooks (original, standalone analyses) for the whole pipeline.
| Resource | Link |
|---|---|
| Protocol (detailed documentation, data format, formulas) | protocols/02_spatial_cluster_compare.md |
| Original notebook | notebooks/cluster/02_spatial_cluster_compare.ipynb |
What the pipeline does
- Clusterability test (Hopkins statistic) before/after normalization
- Conditional normalization: StandardScaler applied only if it improves cluster structure
- Dimensionality reduction (PCA, configurable explained variance)
- Automatic selection of k via a composite score (Silhouette, Davies-Bouldin, Calinski-Harabasz)
- Multi-algorithm benchmark: KMeans, Agglomerative, Spectral, GMM, DBSCAN (eps auto-estimated via the elbow method)
- Bootstrap stability (mean ARI) for each method
- Automatic selection of the best method based on a normalized composite score
- Visualizations: comparative bar chart of metrics, mean-expression heatmap per cluster
Installation
- Python 3.9+
- Dependencies (
numpy,pandas,scikit-learn,scipy,matplotlib,seaborn) are installed automatically.
pip install spatial-cluster-compare
For a local/development install:
pip install -e .
Quick usage
The fastest way to get a result: load your data, run the full comparison, plot it.
import pandas as pd
from spatial_cluster_compare import compare_clusters, plot_comparison_bars, plot_cluster_heatmap
data = pd.read_csv("Cluster_ImmuneCell.csv")
result = compare_clusters(data)
print(result.results_df)
print(f"Best method: {result.best_method}")
plot_comparison_bars(result.results_df)
plot_cluster_heatmap(result.X_original, result.best_labels, method_name=result.best_method)
Advanced usage (step by step)
For control over each stage of the pipeline (scaling, PCA, choice of k, benchmark) instead of the single compare_clusters call above:
from spatial_cluster_compare import (
auto_scale, find_best_k, run_benchmark, select_best_method
)
from sklearn.decomposition import PCA
X = data.select_dtypes(include=["float64", "int64"])
X_best, report = auto_scale(X)
pca = PCA(n_components=0.9)
X_reduced = pca.fit_transform(X_best)
optimal_k, k_results = find_best_k(X_reduced, range(2, 10))
results_df, labels_per_method = run_benchmark(X_reduced, optimal_k=optimal_k)
best_method, best_labels = select_best_method(results_df, labels_per_method)
Expected data format
- One row per cell, one column per marker/descriptor. Numeric columns are used automatically as clustering features; non-numeric columns (ID, ROI name...) are ignored automatically.
- Drop non-biological numeric columns before calling
compare_clusters(e.g.Cell_ID, centroid coordinates), or they'll be wrongly included:data = data.drop(columns=["Cell_ID", "Centroid X", "Centroid Y"])
- Missing values (NaN) aren't handled automatically — clean (
data.dropna()) or impute beforehand. - Dataset size: plan for at least a few hundred cells for a reliable Hopkins statistic and ARI bootstrap. Spectral Clustering can get slow beyond ~10-20k rows.
Full details (MACSiQView export settings, column naming pitfalls, biological rationale for each step) are in the protocol.
Roadmap
- Automatic PDF/figure export of the comparison report
- Support for multi-batch datasets (clustering comparison across runs)
- CLI (
spatial-cluster-compare run data.csv)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spatial_cluster_compare-0.1.2.tar.gz.
File metadata
- Download URL: spatial_cluster_compare-0.1.2.tar.gz
- Upload date:
- Size: 222.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10a51238a91bc1fef006e48ef7ab07a8809db2e0116b17b153b1c6b280ece121
|
|
| MD5 |
a69159b409e2d6e62aa7e56d04fd2b49
|
|
| BLAKE2b-256 |
e6d4d8e3ab51686c4357a50457779062b9e53a04eca059620055e1fd53f45db7
|
Provenance
The following attestation bundles were made for spatial_cluster_compare-0.1.2.tar.gz:
Publisher:
publish.yml on mathisbouvet/spatial-cluster-compare
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spatial_cluster_compare-0.1.2.tar.gz -
Subject digest:
10a51238a91bc1fef006e48ef7ab07a8809db2e0116b17b153b1c6b280ece121 - Sigstore transparency entry: 1985049220
- Sigstore integration time:
-
Permalink:
mathisbouvet/spatial-cluster-compare@ed0efbb47982150d329214a9177ebbd3880383cf -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/mathisbouvet
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ed0efbb47982150d329214a9177ebbd3880383cf -
Trigger Event:
push
-
Statement type:
File details
Details for the file spatial_cluster_compare-0.1.2-py3-none-any.whl.
File metadata
- Download URL: spatial_cluster_compare-0.1.2-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0fdc21efe30832e7166554433b701318450b20227faedda85dcc8215aa1f0d02
|
|
| MD5 |
896ef102e3bbf3fc9b78af0d6c9f1466
|
|
| BLAKE2b-256 |
4b7bdd90ebed8a83d5044b3944393d66cde220a500ba711bfc2b81e82bb312e0
|
Provenance
The following attestation bundles were made for spatial_cluster_compare-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on mathisbouvet/spatial-cluster-compare
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spatial_cluster_compare-0.1.2-py3-none-any.whl -
Subject digest:
0fdc21efe30832e7166554433b701318450b20227faedda85dcc8215aa1f0d02 - Sigstore transparency entry: 1985049317
- Sigstore integration time:
-
Permalink:
mathisbouvet/spatial-cluster-compare@ed0efbb47982150d329214a9177ebbd3880383cf -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/mathisbouvet
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ed0efbb47982150d329214a9177ebbd3880383cf -
Trigger Event:
push
-
Statement type: