#!/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()