about summary refs log tree commit diff
path: root/scripts/plot-pca.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/plot-pca.py')
-rwxr-xr-xscripts/plot-pca.py138
1 files changed, 138 insertions, 0 deletions
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'''<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {W} {H}" width="{W}" height="{H}" role="img" aria-labelledby="t d" font-family="system-ui,-apple-system,Segoe UI,sans-serif">
+<title id="t">{title}</title>
+<desc id="d">{desc}</desc>
+<style>
+:root{{--surface:#fcfcfb;--ink:#0b0b0b;--ink2:#52514e;--grid:#e6e5e1;--s1:#2a6fdb;--s2:#eb6834}}
+@media (prefers-color-scheme:dark){{:root{{--surface:#1a1a19;--ink:#f0efec;--ink2:#c3c2b7;--grid:#333330;--s1:#5b8def;--s2:#d95926}}}}
+text{{fill:var(--ink2);font-size:12px}} .ttl{{fill:var(--ink);font-size:16px;font-weight:600}}
+</style>
+<rect width="{W}" height="{H}" fill="var(--surface)"/>
+<text class="ttl" x="{L}" y="26">{title}</text>
+<text x="{L}" y="46">{sub}</text>''']
+
+    for v in range(0, int(ymax) + 1, max(1, int(ymax / 5) or 1)):
+        y = Y(v)
+        s.append(f'<line x1="{L}" x2="{W - R}" y1="{y:.1f}" y2="{y:.1f}" stroke="var(--grid)" stroke-width="1"/>')
+        s.append(f'<text x="{L - 8}" y="{y + 4:.1f}" text-anchor="end">{v}%</text>')
+    s.append(f'<text transform="translate(16 {T + ph / 2}) rotate(-90)" text-anchor="middle">% of variance</text>')
+
+    for index, value in enumerate(pct):
+        x = L + index * bar + bar * 0.15
+        width = bar * 0.7
+        y = Y(value)
+        s.append(f'<rect x="{x:.1f}" y="{y:.1f}" width="{width:.1f}" height="{T + ph - y:.1f}" '
+                 f'fill="var(--s1)"><title>PC{index + 1}: {value:.2f}% of variance '
+                 f'(eigenvalue {values[index]:.4f})</title></rect>')
+        if index < 12 or index % 2 == 0:
+            s.append(f'<text x="{x + width / 2:.1f}" y="{T + ph + 16}" text-anchor="middle">PC{index + 1}</text>')
+
+    # Right axis: cumulative variance, 0-100%.
+    for v in (0, 25, 50, 75, 100):
+        y = Ycum(v)
+        s.append(f'<text x="{W - R + 10}" y="{y + 4:.1f}">{v}%</text>')
+    s.append(f'<text transform="translate({W - 26} {T + ph / 2}) rotate(90)" text-anchor="middle">cumulative</text>')
+    points = " ".join(
+        f"{L + index * bar + bar / 2:.1f},{Ycum(c):.1f}" for index, c in enumerate(cumulative)
+    )
+    s.append(f'<polyline points="{points}" fill="none" stroke="var(--s2)" stroke-width="2" stroke-linejoin="round"/>')
+    for index, c in enumerate(cumulative):
+        if index + 1 in (keep, len(cumulative)):
+            y = Ycum(c)
+            s.append(f'<line x1="{L}" x2="{W - R}" y1="{y:.1f}" y2="{y:.1f}" stroke="var(--s2)" '
+                     f'stroke-width="1" stroke-dasharray="4 4" opacity="0.7"/>')
+            s.append(f'<text x="{W - R - 6}" y="{y - 5:.1f}" text-anchor="end">PC{index + 1}: {c:.0f}% cumulative</text>')
+
+    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'<text x="{L}" y="{H - 12}">dropped for gaps: {names}{more}</text>')
+    else:
+        s.append(f'<text x="{L}" y="{H - 12}">no trait was dropped: every cell of the matrix was defined</text>')
+
+    s.append("</svg>")
+    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()