eclipse-ms 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ jobs:
9
+ test:
10
+ runs-on: ubuntu-latest
11
+ strategy:
12
+ matrix:
13
+ python-version: ["3.10", "3.11", "3.12"]
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+ - uses: actions/setup-python@v5
17
+ with:
18
+ python-version: ${{ matrix.python-version }}
19
+ - name: Install (core + dev, no TF on CI)
20
+ run: |
21
+ python -m pip install --upgrade pip
22
+ # Install runtime deps without TensorFlow to keep CI fast; the
23
+ # TF-dependent tests skip automatically when TF is absent.
24
+ pip install numpy pandas scikit-learn platformdirs tqdm pytest
25
+ pip install -e . --no-deps
26
+ - name: Lint
27
+ run: |
28
+ pip install ruff
29
+ ruff check src tests
30
+ - name: Run tests
31
+ run: pytest -q
@@ -0,0 +1,43 @@
1
+ name: Publish to PyPI
2
+
3
+ # Publishes to PyPI when you push a version tag (e.g. v0.1.0) or create a
4
+ # GitHub Release. Uses PyPI Trusted Publishing (OIDC) — no API token needed.
5
+ # One-time setup: on PyPI, add a "trusted publisher" for this repo pointing at
6
+ # this workflow (https://docs.pypi.org/trusted-publishers/).
7
+
8
+ on:
9
+ release:
10
+ types: [published]
11
+ push:
12
+ tags: ["v*"]
13
+
14
+ jobs:
15
+ build:
16
+ runs-on: ubuntu-latest
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+ - uses: actions/setup-python@v5
20
+ with:
21
+ python-version: "3.11"
22
+ - name: Build sdist and wheel
23
+ run: |
24
+ python -m pip install --upgrade pip build
25
+ python -m build
26
+ - uses: actions/upload-artifact@v4
27
+ with:
28
+ name: dist
29
+ path: dist/
30
+
31
+ publish:
32
+ needs: build
33
+ runs-on: ubuntu-latest
34
+ environment: pypi
35
+ permissions:
36
+ id-token: write # required for trusted publishing
37
+ steps:
38
+ - uses: actions/download-artifact@v4
39
+ with:
40
+ name: dist
41
+ path: dist/
42
+ - name: Publish
43
+ uses: pypa/gh-action-pypi-publish@release/v1
Binary file
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Frederique Vilenne, Piotr Prostko, Dirk Valkenborg
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,152 @@
1
+ Metadata-Version: 2.4
2
+ Name: eclipse-ms
3
+ Version: 0.1.2
4
+ Summary: Conditional spectrum autoencoder and clustering for MS/MS dark-proteome discovery
5
+ Project-URL: Homepage, https://github.com/VilenneFrederique/ECLIPSE
6
+ Project-URL: Repository, https://github.com/VilenneFrederique/ECLIPSE
7
+ Project-URL: Issues, https://github.com/VilenneFrederique/ECLIPSE/issues
8
+ Author-email: Frederique Vilenne <frederique.vilenne@uhasselt.be>
9
+ License: MIT License
10
+
11
+ Copyright (c) 2026 Frederique Vilenne, Piotr Prostko, Dirk Valkenborg
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ of this software and associated documentation files (the "Software"), to deal
15
+ in the Software without restriction, including without limitation the rights
16
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ copies of the Software, and to permit persons to whom the Software is
18
+ furnished to do so, subject to the following conditions:
19
+
20
+ The above copyright notice and this permission notice shall be included in all
21
+ copies or substantial portions of the Software.
22
+
23
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
+ SOFTWARE.
30
+ License-File: LICENSE
31
+ Keywords: autoencoder,clustering,deep learning,mass spectrometry,proteomics
32
+ Classifier: License :: OSI Approved :: MIT License
33
+ Classifier: Operating System :: OS Independent
34
+ Classifier: Programming Language :: Python :: 3
35
+ Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
36
+ Requires-Python: >=3.10
37
+ Requires-Dist: numpy>=1.23
38
+ Requires-Dist: pandas>=1.5
39
+ Requires-Dist: platformdirs>=3.0
40
+ Requires-Dist: scikit-learn>=1.2
41
+ Requires-Dist: tensorflow>=2.13
42
+ Requires-Dist: tqdm>=4.64
43
+ Provides-Extra: dev
44
+ Requires-Dist: build>=1.0; extra == 'dev'
45
+ Requires-Dist: pytest>=7.0; extra == 'dev'
46
+ Requires-Dist: ruff>=0.1; extra == 'dev'
47
+ Provides-Extra: hdbscan
48
+ Requires-Dist: hdbscan>=0.8.33; extra == 'hdbscan'
49
+ Provides-Extra: mzml
50
+ Requires-Dist: pyteomics>=4.6; extra == 'mzml'
51
+ Provides-Extra: train
52
+ Requires-Dist: joblib>=1.2; extra == 'train'
53
+ Requires-Dist: pyteomics>=4.6; extra == 'train'
54
+ Requires-Dist: regex>=2023.0; extra == 'train'
55
+ Requires-Dist: tqdm>=4.64; extra == 'train'
56
+ Provides-Extra: viz
57
+ Requires-Dist: matplotlib>=3.6; extra == 'viz'
58
+ Requires-Dist: umap-learn>=0.5; extra == 'viz'
59
+ Description-Content-Type: text/markdown
60
+
61
+ # ECLIPSE
62
+
63
+ **ECLIPSE** embeds MS/MS spectra with a conditional transformer autoencoder
64
+ and clusters the latent space to discover candidate novel ("dark proteome")
65
+ peptides. Spectra are conditioned on precursor *m/z*, charge, and ion mobility,
66
+ so same-peptide spectra land close together in latent space.
67
+
68
+ The trained model is large (~650 MB), so it is **not** shipped on PyPI. The pip
69
+ package contains the code; the weights are hosted externally and downloaded +
70
+ cached on first use. Because embedding only needs the **encoder**, the default
71
+ download is encoder-only — roughly half the full model.
72
+
73
+ ## Installation
74
+
75
+ ```bash
76
+ pip install eclipse-ms # core: load model, embed, cluster
77
+ pip install "eclipse-ms[hdbscan]" # add HDBSCAN clustering
78
+ pip install "eclipse-ms[viz]" # add plotting (matplotlib, umap)
79
+ pip install "eclipse-ms[mzml]" # add pyteomics for mzML
80
+ pip install "eclipse-ms[train]" # everything needed to retrain
81
+ ```
82
+
83
+ TensorFlow is a core dependency (the encoder needs it) but is imported lazily —
84
+ `import eclipse_ms` does not load TensorFlow until you actually build or load a
85
+ model.
86
+
87
+ ## Getting the weights
88
+
89
+ `load_encoder()` resolves the model files in this order:
90
+
91
+ 1. `ECLIPSE_MODEL_DIR` — set this to a folder holding the weights (handy on
92
+ an HPC node where you already have them):
93
+ ```bash
94
+ export ECLIPSE_MODEL_DIR=/path/to/weights
95
+ ```
96
+ 2. the local cache (downloaded once, then reused);
97
+ 3. download from the GitHub release (the URLs are configured in
98
+ `eclipse_ms.modelhub.REGISTRY`).
99
+
100
+ With no setup, option 3 runs automatically on first use. You can also bypass
101
+ the registry with explicit local paths:
102
+
103
+ ```python
104
+ from eclipse_ms import load_encoder
105
+ encoder = load_encoder(weights="encoder.weights.h5", config="encoder_config.json")
106
+ ```
107
+
108
+ ## Quick start
109
+
110
+ ```python
111
+ import numpy as np
112
+ from eclipse_ms import load_encoder, embed_raw_spectra, cluster_latents, score_clusters
113
+
114
+ encoder = load_encoder() # downloads/caches the encoder on first call
115
+
116
+ # raw peak lists + precursor info (lists aligned by index)
117
+ latents = embed_raw_spectra(
118
+ encoder,
119
+ mz_list, intensity_list,
120
+ precursor_mz, charge, ion_mobility,
121
+ )
122
+
123
+ labels, info = cluster_latents(latents, method="hdbscan", min_cluster_size=5)
124
+ scores = score_clusters(latents, labels)
125
+ print(info["n_clusters"], "clusters")
126
+ ```
127
+
128
+ Command line:
129
+
130
+ ```bash
131
+ eclipse embed -i parquet_dir/ -o latents.npy
132
+ eclipse cluster -i latents.npy -o clusters/ --method hdbscan
133
+ ```
134
+
135
+ ## Repository layout
136
+
137
+ ```
138
+ src/eclipse_ms/ installable package (model, embed, cluster, consensus, CLI)
139
+ training/ NOT installed: HPC data-prep, training, and export scripts
140
+ export_encoder.py run once to create the slim encoder assets to publish
141
+ reference/ the original monolithic scripts, kept verbatim
142
+ tests/ run without TensorFlow or weights (model tests auto-skip)
143
+ ```
144
+
145
+ ## Citation
146
+ Publication Pending!
147
+
148
+ Vilenne, Frédérique & Valkenborg Dirk. Clustering the Dark Proteome: A Deep Learning Approach to Novel Peptide Discovery in Immunopeptidomics (2026)
149
+
150
+ ## License
151
+
152
+ MIT © 2026 Frédérique Vilenne, Dirk Valkenborg
@@ -0,0 +1,92 @@
1
+ # ECLIPSE
2
+
3
+ **ECLIPSE** embeds MS/MS spectra with a conditional transformer autoencoder
4
+ and clusters the latent space to discover candidate novel ("dark proteome")
5
+ peptides. Spectra are conditioned on precursor *m/z*, charge, and ion mobility,
6
+ so same-peptide spectra land close together in latent space.
7
+
8
+ The trained model is large (~650 MB), so it is **not** shipped on PyPI. The pip
9
+ package contains the code; the weights are hosted externally and downloaded +
10
+ cached on first use. Because embedding only needs the **encoder**, the default
11
+ download is encoder-only — roughly half the full model.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ pip install eclipse-ms # core: load model, embed, cluster
17
+ pip install "eclipse-ms[hdbscan]" # add HDBSCAN clustering
18
+ pip install "eclipse-ms[viz]" # add plotting (matplotlib, umap)
19
+ pip install "eclipse-ms[mzml]" # add pyteomics for mzML
20
+ pip install "eclipse-ms[train]" # everything needed to retrain
21
+ ```
22
+
23
+ TensorFlow is a core dependency (the encoder needs it) but is imported lazily —
24
+ `import eclipse_ms` does not load TensorFlow until you actually build or load a
25
+ model.
26
+
27
+ ## Getting the weights
28
+
29
+ `load_encoder()` resolves the model files in this order:
30
+
31
+ 1. `ECLIPSE_MODEL_DIR` — set this to a folder holding the weights (handy on
32
+ an HPC node where you already have them):
33
+ ```bash
34
+ export ECLIPSE_MODEL_DIR=/path/to/weights
35
+ ```
36
+ 2. the local cache (downloaded once, then reused);
37
+ 3. download from the GitHub release (the URLs are configured in
38
+ `eclipse_ms.modelhub.REGISTRY`).
39
+
40
+ With no setup, option 3 runs automatically on first use. You can also bypass
41
+ the registry with explicit local paths:
42
+
43
+ ```python
44
+ from eclipse_ms import load_encoder
45
+ encoder = load_encoder(weights="encoder.weights.h5", config="encoder_config.json")
46
+ ```
47
+
48
+ ## Quick start
49
+
50
+ ```python
51
+ import numpy as np
52
+ from eclipse_ms import load_encoder, embed_raw_spectra, cluster_latents, score_clusters
53
+
54
+ encoder = load_encoder() # downloads/caches the encoder on first call
55
+
56
+ # raw peak lists + precursor info (lists aligned by index)
57
+ latents = embed_raw_spectra(
58
+ encoder,
59
+ mz_list, intensity_list,
60
+ precursor_mz, charge, ion_mobility,
61
+ )
62
+
63
+ labels, info = cluster_latents(latents, method="hdbscan", min_cluster_size=5)
64
+ scores = score_clusters(latents, labels)
65
+ print(info["n_clusters"], "clusters")
66
+ ```
67
+
68
+ Command line:
69
+
70
+ ```bash
71
+ eclipse embed -i parquet_dir/ -o latents.npy
72
+ eclipse cluster -i latents.npy -o clusters/ --method hdbscan
73
+ ```
74
+
75
+ ## Repository layout
76
+
77
+ ```
78
+ src/eclipse_ms/ installable package (model, embed, cluster, consensus, CLI)
79
+ training/ NOT installed: HPC data-prep, training, and export scripts
80
+ export_encoder.py run once to create the slim encoder assets to publish
81
+ reference/ the original monolithic scripts, kept verbatim
82
+ tests/ run without TensorFlow or weights (model tests auto-skip)
83
+ ```
84
+
85
+ ## Citation
86
+ Publication Pending!
87
+
88
+ Vilenne, Frédérique & Valkenborg Dirk. Clustering the Dark Proteome: A Deep Learning Approach to Novel Peptide Discovery in Immunopeptidomics (2026)
89
+
90
+ ## License
91
+
92
+ MIT © 2026 Frédérique Vilenne, Dirk Valkenborg
@@ -0,0 +1,57 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "eclipse-ms"
7
+ version = "0.1.2"
8
+ description = "Conditional spectrum autoencoder and clustering for MS/MS dark-proteome discovery"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = { file = "LICENSE" }
12
+ authors = [
13
+ { name = "Frederique Vilenne", email = "frederique.vilenne@uhasselt.be" },
14
+ ]
15
+ keywords = ["mass spectrometry", "proteomics", "autoencoder", "clustering", "deep learning"]
16
+ classifiers = [
17
+ "Programming Language :: Python :: 3",
18
+ "License :: OSI Approved :: MIT License",
19
+ "Operating System :: OS Independent",
20
+ "Topic :: Scientific/Engineering :: Bio-Informatics",
21
+ ]
22
+
23
+ # Core install: everything needed to LOAD the model, EMBED spectra, and CLUSTER.
24
+ # TensorFlow is heavy; it stays a hard dependency because the encoder needs it.
25
+ dependencies = [
26
+ "numpy>=1.23",
27
+ "pandas>=1.5",
28
+ "scikit-learn>=1.2",
29
+ "tensorflow>=2.13",
30
+ "platformdirs>=3.0",
31
+ "tqdm>=4.64",
32
+ ]
33
+
34
+ [project.optional-dependencies]
35
+ hdbscan = ["hdbscan>=0.8.33"]
36
+ viz = ["matplotlib>=3.6", "umap-learn>=0.5"]
37
+ mzml = ["pyteomics>=4.6"]
38
+ # Everything required to (re)train and to run the HPC data-prep scripts.
39
+ train = ["pyteomics>=4.6", "regex>=2023.0", "joblib>=1.2", "tqdm>=4.64"]
40
+ dev = ["pytest>=7.0", "ruff>=0.1", "build>=1.0"]
41
+
42
+ [project.urls]
43
+ Homepage = "https://github.com/VilenneFrederique/ECLIPSE"
44
+ Repository = "https://github.com/VilenneFrederique/ECLIPSE"
45
+ Issues = "https://github.com/VilenneFrederique/ECLIPSE/issues"
46
+
47
+ [project.scripts]
48
+ eclipse = "eclipse_ms.cli:main"
49
+
50
+ [tool.hatch.build.targets.wheel]
51
+ packages = ["src/eclipse_ms"]
52
+
53
+ [tool.pytest.ini_options]
54
+ testpaths = ["tests"]
55
+
56
+ [tool.ruff]
57
+ line-length = 100
@@ -0,0 +1,11 @@
1
+ {
2
+ "n_bins": 3200,
3
+ "patch_size": 16,
4
+ "embed_dim": 256,
5
+ "num_heads": 8,
6
+ "num_layers": 4,
7
+ "ff_dim": 512,
8
+ "latent_dim": 256,
9
+ "cond_dim": 8,
10
+ "dropout": 0.1
11
+ }
@@ -0,0 +1,45 @@
1
+ """ECLIPSE: conditional spectrum autoencoder + clustering for MS/MS.
2
+
3
+ Importing this package does NOT import TensorFlow; TF is loaded lazily the
4
+ first time you build or load a model (e.g. ``load_encoder``) or call
5
+ ``embed_spectra``. This keeps imports fast and lets the clustering / consensus
6
+ utilities be used in TF-free environments.
7
+
8
+ The Keras model classes live in ``eclipse_ms.models`` (importing that submodule
9
+ does import TensorFlow).
10
+ """
11
+
12
+ from .config import COND_DIM, Config
13
+ from .preprocessing import bin_spectrum_numpy, build_cond_vector, preprocess
14
+ from .cluster import cluster_latents, score_clusters
15
+ from .consensus import generate_consensus_spectrum, write_mzml
16
+ from .modelhub import (
17
+ REGISTRY,
18
+ cache_dir,
19
+ get_model_file,
20
+ load_autoencoder,
21
+ load_encoder,
22
+ )
23
+ from .embed import embed_raw_spectra, embed_spectra
24
+
25
+ __version__ = "0.1.2"
26
+
27
+ __all__ = [
28
+ "__version__",
29
+ "Config",
30
+ "COND_DIM",
31
+ "bin_spectrum_numpy",
32
+ "build_cond_vector",
33
+ "preprocess",
34
+ "load_encoder",
35
+ "load_autoencoder",
36
+ "get_model_file",
37
+ "cache_dir",
38
+ "REGISTRY",
39
+ "embed_spectra",
40
+ "embed_raw_spectra",
41
+ "cluster_latents",
42
+ "score_clusters",
43
+ "generate_consensus_spectrum",
44
+ "write_mzml",
45
+ ]
@@ -0,0 +1,107 @@
1
+ """ECLIPSE command-line interface.
2
+
3
+ Subcommands:
4
+ embed Bin + encode spectra from parquet to a latents .npy
5
+ cluster Cluster a latents .npy into cluster labels
6
+ consensus Build consensus spectra (mzML) from clusters
7
+
8
+ Training and HPC data-prep live in the repo's ``training/`` scripts, not in the
9
+ installed package.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import argparse
15
+ import glob
16
+ import json
17
+ import os
18
+
19
+ import numpy as np
20
+
21
+
22
+ def _cmd_embed(args):
23
+ import pandas as pd
24
+
25
+ from .config import Config
26
+ from .embed import embed_raw_spectra
27
+ from .modelhub import load_encoder
28
+
29
+ encoder = load_encoder(weights=args.weights, config=args.config)
30
+
31
+ files = sorted(glob.glob(os.path.join(args.input, "*.parquet")))
32
+ print(f"Found {len(files)} parquet files")
33
+
34
+ mz, inten, pmz, charge, im = [], [], [], [], []
35
+ for fp in files:
36
+ df = pd.read_parquet(fp)
37
+ for _, row in df.iterrows():
38
+ mz.append(row["mz_array"])
39
+ inten.append(row["intensity_array"])
40
+ pmz.append(float(row.get("precursor_mz", 0.0)))
41
+ charge.append(int(row.get("precursor_charge", 2)))
42
+ im.append(float(row.get("ion_mobility", 0.0)))
43
+ if args.max_spectra and len(mz) >= args.max_spectra:
44
+ break
45
+ if args.max_spectra and len(mz) >= args.max_spectra:
46
+ break
47
+
48
+ latents = embed_raw_spectra(encoder, mz, inten, pmz, charge, im, Config, args.batch_size)
49
+ os.makedirs(os.path.dirname(args.output) or ".", exist_ok=True)
50
+ np.save(args.output, latents)
51
+ print(f"Saved {latents.shape} latents to {args.output}")
52
+
53
+
54
+ def _cmd_cluster(args):
55
+ from .cluster import cluster_latents, score_clusters
56
+
57
+ latents = np.load(args.input)
58
+ labels, info = cluster_latents(
59
+ latents, method=args.method, min_cluster_size=args.min_cluster_size
60
+ )
61
+ os.makedirs(args.output, exist_ok=True)
62
+ np.save(os.path.join(args.output, "cluster_labels.npy"), labels)
63
+ with open(os.path.join(args.output, "cluster_info.json"), "w") as f:
64
+ json.dump(info, f, indent=2)
65
+ scores = score_clusters(latents, labels)
66
+ scores.to_csv(os.path.join(args.output, "cluster_scores.csv"), index=False)
67
+ print(f"Clusters: {info.get('n_clusters')}, noise: {info.get('n_noise', 0)}")
68
+ print(f"Wrote labels, info, and scores to {args.output}")
69
+
70
+
71
+ def _cmd_consensus(args):
72
+ print(
73
+ "Consensus generation needs spectra grouped by cluster. See "
74
+ "eclipse_ms.consensus.generate_consensus_spectrum and the example in "
75
+ "training/consensus_reference.py for the full pipeline."
76
+ )
77
+
78
+
79
+ def main(argv=None):
80
+ p = argparse.ArgumentParser(prog="eclipse", description="ECLIPSE")
81
+ sub = p.add_subparsers(dest="command", required=True)
82
+
83
+ pe = sub.add_parser("embed", help="Encode spectra to latents")
84
+ pe.add_argument("-i", "--input", required=True, help="Parquet directory")
85
+ pe.add_argument("-o", "--output", required=True, help="Output latents .npy")
86
+ pe.add_argument("--weights", default=None, help="Local encoder weights (.h5)")
87
+ pe.add_argument("--config", default=None, help="Local encoder config (.json)")
88
+ pe.add_argument("--batch-size", type=int, default=256)
89
+ pe.add_argument("--max-spectra", type=int, default=None)
90
+ pe.set_defaults(func=_cmd_embed)
91
+
92
+ pc = sub.add_parser("cluster", help="Cluster latents")
93
+ pc.add_argument("-i", "--input", required=True, help="latents .npy")
94
+ pc.add_argument("-o", "--output", required=True, help="Output directory")
95
+ pc.add_argument("--method", choices=["hdbscan", "kmeans"], default="hdbscan")
96
+ pc.add_argument("--min-cluster-size", type=int, default=5)
97
+ pc.set_defaults(func=_cmd_cluster)
98
+
99
+ pk = sub.add_parser("consensus", help="Consensus spectra from clusters")
100
+ pk.set_defaults(func=_cmd_consensus)
101
+
102
+ args = p.parse_args(argv)
103
+ args.func(args)
104
+
105
+
106
+ if __name__ == "__main__":
107
+ main()
@@ -0,0 +1,105 @@
1
+ """Cluster latent vectors and score the resulting clusters."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import time
6
+ from typing import Tuple
7
+
8
+ import numpy as np
9
+
10
+
11
+ def cluster_latents(
12
+ latents: np.ndarray,
13
+ method: str = "hdbscan",
14
+ min_cluster_size: int = 5,
15
+ pca_dims: int = 100,
16
+ random_state: int = 42,
17
+ ) -> Tuple[np.ndarray, dict]:
18
+ """Cluster latent vectors with HDBSCAN (preferred) or MiniBatchKMeans.
19
+
20
+ Reduces dimensionality with PCA first when ``n_dims > pca_dims``. Falls back
21
+ to KMeans if HDBSCAN is not installed.
22
+
23
+ Returns ``(labels, info)`` where ``labels`` is ``-1`` for HDBSCAN noise.
24
+ """
25
+ from sklearn.decomposition import PCA
26
+
27
+ n_samples, n_dims = latents.shape
28
+ info = {"method": method, "n_samples": int(n_samples)}
29
+
30
+ if n_dims > pca_dims:
31
+ pca = PCA(n_components=pca_dims, random_state=random_state)
32
+ reduced = pca.fit_transform(latents)
33
+ info["pca_variance_explained"] = float(pca.explained_variance_ratio_.sum())
34
+ else:
35
+ reduced = latents
36
+
37
+ if method == "hdbscan":
38
+ try:
39
+ import hdbscan
40
+ except ImportError:
41
+ print("hdbscan not installed (`pip install eclipse-ms[hdbscan]`); using KMeans.")
42
+ method = "kmeans"
43
+
44
+ if method == "hdbscan":
45
+ import hdbscan
46
+
47
+ start = time.time()
48
+ clusterer = hdbscan.HDBSCAN(
49
+ min_cluster_size=min_cluster_size,
50
+ min_samples=3,
51
+ metric="euclidean",
52
+ cluster_selection_method="eom",
53
+ core_dist_n_jobs=-1,
54
+ )
55
+ labels = clusterer.fit_predict(reduced)
56
+ info["time"] = time.time() - start
57
+ info["n_clusters"] = len(set(labels)) - (1 if -1 in labels else 0)
58
+ info["n_noise"] = int((labels == -1).sum())
59
+
60
+ elif method == "kmeans":
61
+ from sklearn.cluster import MiniBatchKMeans
62
+
63
+ n_clusters = max(2, min(10000, n_samples // 10))
64
+ start = time.time()
65
+ kmeans = MiniBatchKMeans(
66
+ n_clusters=n_clusters, batch_size=1024, random_state=random_state, n_init=3
67
+ )
68
+ labels = kmeans.fit_predict(reduced)
69
+ info["time"] = time.time() - start
70
+ info["n_clusters"] = len(set(labels))
71
+ info["n_noise"] = 0
72
+ else:
73
+ raise ValueError(f"Unknown method: {method}")
74
+
75
+ return labels, info
76
+
77
+
78
+ def score_clusters(latents: np.ndarray, labels: np.ndarray) -> "pd.DataFrame": # noqa: F821
79
+ """Lightweight per-cluster quality scores from latent geometry.
80
+
81
+ For each non-noise cluster, reports size and intra-cluster cohesion
82
+ (mean distance to centroid; smaller = tighter).
83
+ """
84
+ import pandas as pd
85
+
86
+ rows = []
87
+ for c in sorted(set(labels)):
88
+ if c == -1:
89
+ continue
90
+ idx = np.where(labels == c)[0]
91
+ pts = latents[idx]
92
+ centroid = pts.mean(axis=0)
93
+ dists = np.linalg.norm(pts - centroid, axis=1)
94
+ rows.append(
95
+ {
96
+ "cluster": int(c),
97
+ "size": int(len(idx)),
98
+ "cohesion_mean_dist": float(dists.mean()),
99
+ "cohesion_std_dist": float(dists.std()),
100
+ }
101
+ )
102
+ df = pd.DataFrame(rows)
103
+ if not df.empty:
104
+ df = df.sort_values("size", ascending=False).reset_index(drop=True)
105
+ return df