File size: 8,321 Bytes
9542666 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #!/usr/bin/env python3
"""Generate a self-contained interactive paper-influence graph (D3 force layout).
Nodes are papers (and registered influences); a directed edge A->B means paper A
`builds-on` paper B, so B *influenced* A. Node size = in-degree (how many papers
build on it = its influence); node color = publication year. Only edges whose
both endpoints are in-graph notes are drawn. One HTML file, D3 from CDN; drag,
scroll-zoom, hover for the paper slug (hubs are labelled always).
python3 _scripts/make_influence_graph.py --out reports/influence-all.html
python3 _scripts/make_influence_graph.py --since 2021 --embed --out reports/influence-2021.html
Flags: --since YEAR (keep only edges with both endpoints published >= YEAR),
--embed (strip header, for <iframe>), --title, --out. Companion to
make_graph.py / make_author_graph.py; see skills/visualize-graph/SKILL.md.
"""
import argparse
import glob
import json
import os
import re
from collections import Counter
VAULT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def build(since):
files = glob.glob(os.path.join(VAULT, "papers", "*.md")) + \
glob.glob(os.path.join(VAULT, "influences", "*.md"))
alln, year = set(), {}
for f in files:
s = os.path.basename(f)[:-3]
alln.add(s)
m = re.search(r"^year:\s*(\d{4})", open(f, encoding="utf-8").read(), re.M)
year[s] = int(m.group(1)) if m else None
edges = []
for f in files:
s = os.path.basename(f)[:-3]
for mm in re.finditer(r"(?<![a-z])builds-on::\s*\[\[([^\]]+)\]\]", open(f, encoding="utf-8").read()):
t = mm.group(1)
if t in alln:
edges.append((s, t))
if since:
edges = [(s, t) for s, t in edges if (year.get(s) or 0) >= since and (year.get(t) or 0) >= since]
indeg = Counter(t for _, t in edges)
used = {n for e in edges for n in e}
nodes = [{"id": n, "label": n, "deg": indeg.get(n, 0), "year": year.get(n) or 0}
for n in used]
links = [{"source": s, "target": t} for s, t in edges]
yrs = [n["year"] for n in nodes if n["year"]]
return {"nodes": nodes, "links": links, "ymin": min(yrs) if yrs else 2016,
"ymax": max(yrs) if yrs else 2026}
HEADER = '''<div id="hd"><h1>{title}</h1>
<p>A directed edge A→B means paper A builds on paper B, so B influenced A. Node size = how many papers build on it (its influence); color = year. Drag, scroll to zoom, hover for the paper.</p></div>
'''
TEMPLATE = '''<!doctype html>
<html lang="en"><head>
<meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1"/>
<title>{title}</title>
<script src="https://cdn.jsdelivr.net/npm/d3@7/dist/d3.min.js"></script>
<style>
:root{{--bg:#fbfaf8;--ink:#1a1a1f;--muted:#6b6b78;--line:#e7e4de;}}
*{{box-sizing:border-box}} html,body{{margin:0;height:100%}}
body{{background:var(--bg);color:var(--ink);font:15px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;overflow:hidden}}
#hd{{position:fixed;top:0;left:0;right:0;padding:14px 20px;z-index:5;pointer-events:none}}
#hd h1{{font:600 19px/1.2 Georgia,serif;margin:0}}
#hd p{{margin:3px 0 0;color:var(--muted);font-size:13px;max-width:680px}}
#legend{{position:fixed;bottom:16px;left:16px;background:#fffdfaee;border:1px solid var(--line);border-radius:10px;padding:11px 13px;font-size:12.5px;z-index:5;box-shadow:0 2px 10px #0000000d}}
#legend b{{display:block;margin:0 0 6px;font-size:11px;letter-spacing:.05em;text-transform:uppercase;color:var(--muted)}}
#legend .row{{display:flex;align-items:center;gap:8px;margin:4px 0}}
#net{{width:100vw;height:100vh;cursor:grab}}
.link{{stroke:#c3bdb0;stroke-opacity:.55}}
.node circle{{stroke:#fff;stroke-width:1}}
.lbl{{font-size:11px;fill:var(--ink);pointer-events:none;paint-order:stroke;stroke:#fbfaf8;stroke-width:3px;stroke-linejoin:round}}
</style></head><body>
{header}<div id="legend">
<b>Node size = influence</b>
<div class="row"><svg width="46" height="18"><circle cx="8" cy="9" r="3.5" fill="#888"/><circle cx="32" cy="9" r="10" fill="#888"/></svg>papers building on it</div>
<b style="margin-top:8px">Color = year</b>
<div class="row"><svg width="120" height="12"><defs><linearGradient id="yg"><stop offset="0%" stop-color="{cold}"/><stop offset="100%" stop-color="{hot}"/></linearGradient></defs><rect width="120" height="8" y="2" fill="url(#yg)"/></svg></div>
<div class="row" style="justify-content:space-between;color:var(--muted);font-size:11px"><span>{ymin}</span><span>{ymax}</span></div>
</div>
<svg id="net"><defs><marker id="arrow" viewBox="0 -5 10 10" refX="8" refY="0" markerWidth="5" markerHeight="5" orient="auto"><path d="M0,-4L8,0L0,4" fill="#b3ac9d"/></marker></defs></svg>
<script>
const data={data};
const color=d3.scaleSequential(d3.interpolateViridis).domain([data.ymin,data.ymax]);
const R=d=>3+2.3*Math.sqrt(d.deg), LBL_MIN=4;
const svg=d3.select("#net"),W=innerWidth,H=innerHeight,g=svg.append("g");
const zoom=d3.zoom().scaleExtent([0.12,4]).on("zoom",e=>g.attr("transform",e.transform));svg.call(zoom);
const link=g.append("g").selectAll("line").data(data.links).join("line")
.attr("class","link").attr("marker-end","url(#arrow)");
const node=g.append("g").selectAll("g").data(data.nodes).join("g").attr("class","node");
node.append("circle").attr("r",R).attr("fill",d=>d.year?color(d.year):"#bbb");
node.append("title").text(d=>d.label+" · "+d.deg+" build on it · "+(d.year||"?"));
node.append("text").attr("class","lbl").attr("x",d=>R(d)+3).attr("y",4)
.style("opacity",d=>d.deg>=LBL_MIN?1:0).text(d=>d.label);
node.on("mouseover",function(){{d3.select(this).select("text").style("opacity",1)}})
.on("mouseout",function(e,d){{d3.select(this).select("text").style("opacity",d.deg>=LBL_MIN?1:0)}});
node.call(d3.drag()
.on("start",(e,d)=>{{if(!e.active)sim.alphaTarget(.3).restart();d.fx=d.x;d.fy=d.y}})
.on("drag",(e,d)=>{{d.fx=e.x;d.fy=e.y}})
.on("end",(e,d)=>{{if(!e.active)sim.alphaTarget(0);d.fx=null;d.fy=null}}));
function ticked(){{
link.attr("x1",d=>d.source.x).attr("y1",d=>d.source.y)
.attr("x2",d=>{{const dx=d.target.x-d.source.x,dy=d.target.y-d.source.y,l=Math.hypot(dx,dy)||1;return d.target.x-dx/l*(R(d.target)+4)}})
.attr("y2",d=>{{const dx=d.target.x-d.source.x,dy=d.target.y-d.source.y,l=Math.hypot(dx,dy)||1;return d.target.y-dy/l*(R(d.target)+4)}});
node.attr("transform",d=>`translate(${{d.x}},${{d.y}})`);
}}
const sim=d3.forceSimulation(data.nodes)
.force("link",d3.forceLink(data.links).id(d=>d.id).distance(46).strength(.35))
.force("charge",d3.forceManyBody().strength(-160).distanceMax(600))
.force("x",d3.forceX(W/2).strength(.05)).force("y",d3.forceY(H/2).strength(.05))
.force("collide",d3.forceCollide(d=>R(d)+4))
.stop();
sim.tick(400); ticked(); fit(); sim.on("tick",ticked);
function fit(){{
const xs=data.nodes.map(d=>d.x),ys=data.nodes.map(d=>d.y);
const x0=Math.min(...xs),x1=Math.max(...xs),y0=Math.min(...ys),y1=Math.max(...ys);
const w=(x1-x0)||1,h=(y1-y0)||1,k=Math.min(2.5,0.92/Math.max(w/W,h/H));
svg.call(zoom.transform,d3.zoomIdentity.translate(W/2-k*(x0+x1)/2,H/2-k*(y0+y1)/2).scale(k));
}}
</script></body></html>'''
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--since", type=int, default=0)
ap.add_argument("--embed", action="store_true")
ap.add_argument("--title", default=None)
ap.add_argument("--out", default="reports/influence-graph.html")
a = ap.parse_args()
title = a.title or (f"Paper influence — builds-on, since {a.since}" if a.since
else "Paper influence — builds-on, all time")
data = build(a.since)
cold = "#440154" # viridis ends, for the legend gradient
hot = "#fde725"
header = "" if a.embed else HEADER.format(title=title)
html = TEMPLATE.format(title=title, header=header, data=json.dumps(data),
ymin=data["ymin"], ymax=data["ymax"], cold=cold, hot=hot)
out = a.out if os.path.isabs(a.out) else os.path.join(VAULT, a.out)
os.makedirs(os.path.dirname(out), exist_ok=True)
open(out, "w", encoding="utf-8").write(html)
print(f"wrote {out}: {len(data['nodes'])} nodes, {len(data['links'])} builds-on edges")
if __name__ == "__main__":
main()
|