From 33e837e771e07936c808fb3aeb3540525691718c Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Mon, 16 Nov 2026 16:18:05 +0000 Subject: plot: the scree plot of a matrix PCA, which GN2 draws client-side GN2 renders a scree plot with Plotly in the browser, from the explained variance ratios its matrix page computed. This is the same figure as a file: `POST /plot/pca` takes the matrix reply (with `"pca": true`) as-is and returns image/svg+xml, like every other plot endpoint -- the plot is of the numbers that were computed, nothing is recomputed. scripts/plot-pca.py draws a bar per component (its share of the variance, GN2's number), a cumulative line with the 80%-of-variance crossing annotated (what a reader actually uses to choose how many components to keep), the eigenvalue in each bar's tooltip, and the traits the PCA had to drop for gaps in the matrix -- the one thing GN2 hides by mutating its shared-sample list. `kind=N` on the request caps how many components are drawn, since a 200-trait matrix would otherwise draw 200 bars. Also: the monitor's SVG judge now understands a binary reply (the client hands back content type and size, not the bytes), and `plot-pca` is a check of its own: it asks the matrix for a PCA and posts the reply to the plotter. Deployed: PCA_PLOTTER is pinned in env.sh (the code default is a relative path, which only resolves when the service's working directory is the checkout). plot-pca and plot-genotypes both ok. --- scripts/plot-pca.py | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100755 scripts/plot-pca.py (limited to 'scripts/plot-pca.py') diff --git a/scripts/plot-pca.py b/scripts/plot-pca.py new file mode 100755 index 0000000..67bcd3f --- /dev/null +++ b/scripts/plot-pca.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python3 +"""Scree plot of a GeneNetwork4 correlation-matrix PCA, as one SVG panel. + +Input is the JSON response of POST /compute/correlation/matrix with +`"pca": true` (saved as-is) -- the same convention as plot-gemma.py and +plot-bnw.py: the plot is of the numbers that were computed, nothing here +recomputes anything. + +Bars are each component's share of the total variance (the explained variance +ratio, which is what GN2's scree plot shows); the line is the cumulative share, +which is what a reader actually uses to decide how many components to keep. + + python3 plot-pca.py matrix.json -o scree.svg --trait collection-name + +Standard library only; writes plain SVG (light/dark aware). +""" +import argparse +import json +import sys + +W, H = 760, 420 +L, R, T, B = 64, 118, 78, 62 + + +def pca_of(doc): + """The pca block, from either the envelope or a bare reply.""" + data = doc.get("data") if isinstance(doc, dict) else None + for candidate in (data, doc): + if isinstance(candidate, dict) and isinstance(candidate.get("pca"), dict): + return candidate["pca"], candidate + return None, data if isinstance(data, dict) else {} + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.split("\n")[0]) + ap.add_argument("result", help="saved /compute/correlation/matrix response (JSON), pca: true") + ap.add_argument("-o", "--out", default="pca_scree.svg") + ap.add_argument("--trait", default="", help="collection label for the title") + ap.add_argument("--title", default=None, help="override the title text") + ap.add_argument("--max-components", type=int, default=20, + help="draw at most this many components (default 20)") + a = ap.parse_args() + + doc = json.load(open(a.result)) + pca, data = pca_of(doc) + if not pca or not pca.get("components"): + sys.exit(f"{a.result}: no pca.components -- post the matrix reply with \"pca\": true") + components = pca["components"][: a.max_components] + ratios = [float(c.get("explained_variance_ratio") or 0.0) for c in components] + values = [float(c.get("eigenvalue") or 0.0) for c in components] + kept = len(data.get("traits") or []) - len(pca.get("dropped") or []) + + pct = [r * 100.0 for r in ratios] + cumulative, running = [], 0.0 + for p in pct: + running += p + cumulative.append(running) + + label = a.trait or "correlation matrix" + title = a.title or f"{label}: PCA of the correlation matrix" + n_show = len(pct) + keep = next((i + 1 for i, c in enumerate(cumulative) if c >= 80.0), n_show) + sub = (f"{kept} trait(s), {len(pca['components'])} component(s)" + + (f", {len(pca['dropped'])} dropped for gaps" if pca.get("dropped") else "") + + f" ยท {keep} component(s) reach {min(cumulative[-1], max(cumulative[keep - 1], 80.0)):.0f}%" + if cumulative else f"{kept} trait(s)") + desc = (f"Scree plot: {n_show} of {len(pca['components'])} components, " + f"eigenvalues {values[0]:.3f} down to {values[-1]:.3f}, " + f"cumulative variance {cumulative[-1]:.1f}%.") + + pw, ph = W - L - R, H - T - B + ymax = max(pct) * 1.15 if pct else 1.0 + bar = pw / max(n_show, 1) + + def Y(v): + return T + ph - (v / ymax) * ph if ymax else T + ph + + def Ycum(v): + return T + ph - (v / 100.0) * ph + + s = [f''' +{title} +{desc} + + +{title} +{sub}'''] + + for v in range(0, int(ymax) + 1, max(1, int(ymax / 5) or 1)): + y = Y(v) + s.append(f'') + s.append(f'{v}%') + s.append(f'% of variance') + + for index, value in enumerate(pct): + x = L + index * bar + bar * 0.15 + width = bar * 0.7 + y = Y(value) + s.append(f'PC{index + 1}: {value:.2f}% of variance ' + f'(eigenvalue {values[index]:.4f})') + if index < 12 or index % 2 == 0: + s.append(f'PC{index + 1}') + + # Right axis: cumulative variance, 0-100%. + for v in (0, 25, 50, 75, 100): + y = Ycum(v) + s.append(f'{v}%') + s.append(f'cumulative') + points = " ".join( + f"{L + index * bar + bar / 2:.1f},{Ycum(c):.1f}" for index, c in enumerate(cumulative) + ) + s.append(f'') + for index, c in enumerate(cumulative): + if index + 1 in (keep, len(cumulative)): + y = Ycum(c) + s.append(f'') + s.append(f'PC{index + 1}: {c:.0f}% cumulative') + + if pca.get("dropped"): + names = ", ".join(d.get("trait_name", "?") for d in pca["dropped"][:4]) + more = "" if len(pca["dropped"]) <= 4 else f" and {len(pca['dropped']) - 4} more" + s.append(f'dropped for gaps: {names}{more}') + else: + s.append(f'no trait was dropped: every cell of the matrix was defined') + + s.append("") + open(a.out, "w").write("\n".join(s) + "\n") + print(f"{a.out}: {n_show} component(s), {cumulative[-1]:.1f}% cumulative variance, {kept} trait(s)") + + +if __name__ == "__main__": + main() -- cgit 1.4.1