Metadata-Version: 2.4
Name: ssmdkmeans
Version: 1.1.0
Summary: SSMD k-means: discovering separable subgroups and their number, with a report (figure and tables) for your own data
Author-email: Xiaohua Douglas Zhang <douglas.zhang@uky.edu>
Maintainer-email: Xiaohua Douglas Zhang <douglas.zhang@uky.edu>
License: MIT
Project-URL: Homepage, https://github.com/xhdouglas
Keywords: clustering,k-means,SSMD,number of clusters,precision medicine,subgroups
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.23
Requires-Dist: scipy>=1.9
Requires-Dist: pandas>=1.5
Requires-Dist: scikit-learn>=1.2
Requires-Dist: matplotlib>=3.6
Requires-Dist: joblib>=1.2
Provides-Extra: fast
Requires-Dist: numba>=0.57; extra == "fast"
Provides-Extra: excel
Requires-Dist: openpyxl>=3.0; extra == "excel"
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Provides-Extra: all
Requires-Dist: numba>=0.57; extra == "all"
Requires-Dist: openpyxl>=3.0; extra == "all"
Dynamic: license-file

# ssmdkmeans (Python)

**SSMD k-means: discovering separable subgroups and their number**

Author: Xiaohua Douglas Zhang (Department of Biostatistics, College of Public Health, University of Kentucky;
douglas.zhang@uky.edu) · Version 1.1.0 · December 1, 2026 · License: MIT

SSMD k-means clusters data so that every pair of subgroups is separated by an optimal linear score, measured by the
strictly standardized mean difference (SSMD). It can be used in two ways:

* **Path 1:** form a preset number of subgroups and report which pairs can be discriminated.
* **Path 2:** find the largest number of subgroups whose every pair reaches a preset SSMD *c*, such as 3, with
  K̂ = max{K : J(K) ≥ c}. When no partition qualifies, it concludes that there are no subgroups.

J(K) is the minimum over pairs of clusters of the maximized SSMD. It is compared with a no-cluster reference
distribution, simulated with the same algorithm for the data set's own *n* and *p*.

This package applies the method to your own data set and writes a figure and tables. Only SSMD k-means is run; no
other clustering method is used.

> Zhang, X. D. *SSMD k-Means: Discovering Separable Subgroups and Their Number in Precision Medicine.*

## Installation

```bash
pip install ssmdkmeans                  # from PyPI, once available
pip install ./ssmdkmeans                # from this folder
pip install "./ssmdkmeans[all]"         # with numba (faster refinement, identical results) and openpyxl (Excel)
```

Requires Python ≥ 3.9 with numpy, scipy, pandas, scikit-learn, matplotlib and joblib.

## Quick start

```python
import pandas as pd
from ssmdkmeans import ssmd_report

df = pd.read_csv("my_data.csv")

# (1) data with known groups: figure like Figures 4 and 5 of the paper, and tables like Tables 3-5
rep = ssmd_report(df.drop(columns="group"), df["group"], name="My data", outdir="my_report")

# (2) data without known groups: figure like Figures 7 and 8
rep = ssmd_report(df, name="My data", outdir="my_report")
rep = ssmd_report(df, name="My data", K_preset=3)            # also show Path 1 with 3 subgroups
rep = ssmd_report(df, name="My data", external=markers)      # plus variables not used in forming the subgroups (Figure 8D)

print(rep)                    # summary of Path 1 and Path 2
rep.tables["path2"]           # every table is a pandas DataFrame
rep.figure                    # the matplotlib figure
```

With `outdir` given, the figure, tables and summary are written to that folder. Without it (the default), nothing
is written and the results are returned in `rep`. Random numbers come from local generators, so the global NumPy
random state is never changed.

From the command line:

```bash
ssmdkmeans-report my_data.csv --groups group --name "My data" --jobs -1
ssmdkmeans-report my_data.csv --external ESR1 PGR KRT5 --kpreset 3
```

There are two worked examples, which use data from the paper: `examples/example_known_groups.py` (dermatology data)
and `examples/example_no_known_groups.py` (diabetes data). `load_example()` loads these data sets.

## The figure (`<name>_figure.png` and `.pdf`)

| | Data with known groups (Figures 4, 5) | Data without known groups (Figures 7, 8) |
|---|---|---|
| **A** | First two principal components, coloured by known group | First two principal components, coloured by Path 2 subgroup (grey if none) |
| **B** | J(K) with the no-cluster reference (mean ± 2 SD), the threshold *c*, Path 1 (square) and Path 2 (circle) | Same |
| **C** | Maximized SSMD between every pair of known groups (orange: below *c*) | The Path 2 subgroups on their maximized-SSMD direction; the closest pair when K̂ ≥ 3 |
| **D, E, …** | Consecutive pairs of the Path 2 subgroups (1 vs 2, 2 vs 3, …), each on its maximized-SSMD direction. If K̂ = 1, the Path 1 subgroups are shown instead. | Consecutive pairs of the Path 2 subgroups when K̂ ≥ 3. If K̂ = 1 and `K_preset` is given, the Path 1 subgroups are shown instead. |
| **last** | Variables given in `external`, by subgroup (box plots) | Same (as Figure 8D) |

Subgroups are numbered by the known group to which most of their members belong, then by size. Without known
groups, they are numbered by size. In the pair panels, colour shows the subgroup and marker shape shows the known
group. The horizontal axis is scaled so that the distance between the two subgroup means equals their SSMD.

## The tables (`<name>_<table>.csv`, and all in `<name>_tables.xlsx`)

| Table | Contents |
|---|---|
| `summary` | *n*, *p*, the number of known groups, *c*, the Path 1 K and its J, K̂ and its minimum SSMD, and the 95th percentile of max<sub>K</sub> J(K) in the no-cluster reference |
| `known_groups` | Separation of the known groups (as Table 3): the minimum maximized SSMD, the closest pair, AUC = Φ(min SSMD) and the 10-fold cross-validated minimum SSMD |
| `known_ssmd_matrix` | Maximized SSMD between every pair of known groups (panel C) |
| `J_path` | For each K: J(K), whether it reaches *c*, the cluster sizes, and the reference mean, SD and 95th percentile. With known groups, also ARI and homogeneity. |
| `path1` | Path 1 with K preset (as Table 4, SSMD k-means only): J(K), the sizes, ARI, homogeneity, and the SSMD between the known groups computed in the same way |
| `path2` | Path 2 (as Table 5, SSMD k-means only): K̂, the minimum pairwise SSMD, the sizes, and (with known groups) ARI and homogeneity |
| `pairwise_ssmd_path1`, `pairwise_ssmd_path2` | Maximized SSMD between every pair of subgroups |
| `crosstab_path1`, `crosstab_path2` | Subgroups by known groups |
| `cluster_profile` | Mean (SD) of each variable by subgroup |
| `external_by_cluster` | Summary of each `external` variable by subgroup, with the Kruskal–Wallis *p*-value |
| `assignments` | Known group and subgroup of every observation, for every K |
| `summary.txt` | Text summary |

## Main options of `ssmd_report`

| Option | Default | Meaning |
|---|---|---|
| `K_max` | 8 | The largest K examined. If K̂ = K_max, increase it (Section 2.4 of the paper). |
| `c` | 3 | SSMD threshold of Path 2. An SSMD of 3 corresponds to an AUC of 0.999 for two normal subgroups. |
| `K_preset` | number of known groups | K for Path 1 |
| `n_init` | 30 | Starts in each of the four affine-invariant coordinate systems (120 starts in all) |
| `B` | 60 | Simulated no-cluster data sets for the reference; 0 skips it. This is the slowest step: use `n_jobs=-1`. |
| `pca` | None | Form the subgroups from the first `pca` principal components. Every subgroup needs more than *p* observations, so use this when *p* is large relative to *n*. |
| `pca_standardize` | True | Standardize the variables before computing principal components (for `pca` and for panel A) |
| `short_names`, `group_order` | | Labels and order of the known groups in the figure |
| `external`, `external_scale` | | Variables not used in forming the subgroups, shown on their own scale (`"raw"`) or standardized (`"z"`) |
| `outdir`, `formats`, `dpi` | None (no files), png + pdf, 300 | Output |

Lower-level functions are `ssmd_kmeans(X, K)`, `ssmd_path(X, K_max, c)`, `pairwise_ssmd(X, labels)`,
`null_reference(n, p)` and `known_group_separation(X, y)`.

Rows with missing values are dropped.

**Running time.** On a laptop, SSMD k-means for K = 2 to 8 takes about 20–30 seconds for 366 observations with
*p* = 5. Each simulated reference data set takes about as long, and `n_jobs` spreads them across cores.

## Example data

* `dermatology`: UCI Machine Learning Repository (https://archive.ics.uci.edu/dataset/33/dermatology), CC BY 4.0;
  Guvenir, Demiroz and Ilter (1998), *Artificial Intelligence in Medicine*, 13, 147–165.
* `diabetes`: Efron, Hastie, Johnstone and Tibshirani (2004), *The Annals of Statistics*, 32, 407–499.

## Tests

```bash
pip install "./ssmdkmeans[test]"
pytest ssmdkmeans/tests
```
