#!/usr/bin/env python3 """WP44 — publication figures, batch 1 (corpus and volume). Every figure is generated from result files; none is drawn by hand. Run on the compute node: python figures/make_figures.py --root ~/wp44_uqo → figures/*.pdf + .png, and figures/FIGURES.json (inventory).""" import argparse, json, os, sys import numpy as np, pandas as pd, matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import style style.apply() INV = [] def reg(name, caption, sources): INV.append({"file": name, "caption": caption, "sources": sources}) def fig_prisma(R, out): P = json.load(open(f"{R}/data/metadata/prisma_counts.json")); fig, ax = plt.subplots(figsize=(7.2, 6.2)); ax.axis("off"); ax.grid(False) boxes = [("Identified (OpenAlex works with a CS topic, 2018–2026, snapshot 2026-10-14)", P["identified"]["openalex_total_records_loaded"]), ("Outside window (E1)", -P["excluded_sequential"]["e1_outside_window"]), ("Non-research types (E2)", -P["excluded_sequential"]["e2_type"]), ("XPAC expansion records, not DBLP-linked (E3)", -P["excluded_sequential"]["e3_xpac"]), ("No title (E4)", -P["excluded_sequential"]["e4_no_title"]), ("Preprint duplicates merged into the published version (E5)", -P["excluded_sequential"]["e5_duplicate"]), ("Generic deposit platforms without DBLP/arXiv support (E7)", -P["excluded_sequential"].get("e7_generic_deposit", 0)), ("After exclusions", P["after_exclusions"]), ("CS core — mapping corpus (DBLP ∨ primary CS ∨ CS topic ∧ arXiv cs.*)", P["cs_core_eligible"]), ("CS penumbra — secondary CS topic only (sensitivity set)", P["cs_penumbra_eligible_otherwise"])] y = 0.95 for label, n in boxes: excl = n < 0; txt = f"{abs(n):,}" ax.text(0.02 if not excl else 0.40, y, (label if not excl else "− " + label) + f"\n{txt}", fontsize=8.2, va="top", ha="left", bbox=dict(boxstyle="round,pad=0.4", fc="white", ec=style.AXIS if not excl else style.GRID, lw=0.8), transform=ax.transAxes, color=style.INK) y -= 0.095 ax.set_title("PRISMA-style flow of the WP44 mapping corpus", loc="left") style.save(fig, f"{out}/fig_prisma_flow.pdf"); reg("fig_prisma_flow", "PRISMA-style flow of record identification, exclusion and the final mapping corpus.", ["data/metadata/prisma_counts.json"]) def fig_yearly_volume(R, out): d = pd.read_csv(f"{R}/results/bibliometrics/volume_yearly.csv"); d = d[d.year <= 2026] fig, ax = plt.subplots(figsize=(6.8, 3.4)) cols = [("n", "All eligible works"), ("n_dblp", "DBLP-indexed"), ("n_cs_primary", "Primary field = CS"), ("n_arxiv", "arXiv version linked")] for i, (c, lab) in enumerate(cols): ax.plot(d.year, d[c] / 1e3, marker="o", label=lab, color=style.CATEGORICAL[i]) ax.annotate(lab, (d.year.iloc[-1], d[c].iloc[-1] / 1e3), xytext=(4, 0), textcoords="offset points", fontsize=7, color=style.INK2, va="center") ax.axvspan(2026, 2026.6, color=style.GRID, alpha=0.5, lw=0); ax.text(2026.3, ax.get_ylim()[0] + 0.02 * ax.get_ylim()[1], "2026\n(partial)", fontsize=7, color=style.INK2, va="bottom", ha="center") ax.set_ylabel("Works (thousands)"); ax.set_xlabel(""); ax.set_xlim(2017.7, 2027.4); ax.legend(loc="upper left") ax.set_title("Eligible computer-science works per publication year, by membership", loc="left") style.save(fig, f"{out}/fig_volume_yearly.pdf"); reg("fig_volume_yearly", "Eligible works per publication year by source membership (2026 incomplete).", ["results/bibliometrics/volume_yearly.csv"]) def fig_monthly(R, out): d = pd.read_csv(f"{R}/results/bibliometrics/volume_monthly.csv") m = d.groupby("first_month")["n"].sum().reset_index(); m = m[(m.first_month >= "2018-01") & (m.first_month <= "2026-10")] # month-precision only: records dated YYYY-01-01 are year-precision and are excluded from monthly series (A-002); here approximated by dropping January spikes is NOT done: # volume_monthly is built from first_month of all eligible works; the precise month-only series comes from category_counts_monthly (n_month_precision). We plot both when available. x = pd.to_datetime(m.first_month + "-01"); fig, ax = plt.subplots(figsize=(6.8, 3.2)) ax.plot(x, m.n / 1e3, color=style.CATEGORICAL[0], lw=1.2, label="All eligible works (first appearance month)") style.event_line(ax, pd.Timestamp("2022-11-30")) ax.set_ylabel("Works per month (thousands)"); ax.set_title("Monthly first appearances of eligible works (January spikes = year-precision dates)", loc="left") ax.legend(loc="upper left"); style.save(fig, f"{out}/fig_volume_monthly_raw.pdf") reg("fig_volume_monthly_raw", "Monthly first appearances; January spikes reflect year-only publication dates and motivate the month-precision filter.", ["results/bibliometrics/volume_monthly.csv"]) def fig_types_sources(R, out): d = pd.read_csv(f"{R}/results/bibliometrics/volume_yearly.csv"); d = d[d.year <= 2026] fig, axes = plt.subplots(1, 2, figsize=(7.2, 3.0)) ax = axes[0] for i, (c, lab) in enumerate([("n_article", "Journal article"), ("n_conf", "Conference paper"), ("n_preprint", "Preprint")]): ax.plot(d.year, 100 * d[c] / d.n, marker="o", label=lab, color=style.CATEGORICAL[i]) ax.set_ylabel("Share of eligible works (%)"); ax.set_title("Document types", loc="left"); ax.legend() ax = axes[1] for i, (c, lab) in enumerate([("n_abstract", "Abstract available"), ("n_oa", "Open access"), ("n_arxiv", "arXiv linked")]): ax.plot(d.year, 100 * d[c] / d.n, marker="o", label=lab, color=style.CATEGORICAL[i + 3]) ax.set_title("Metadata completeness and openness", loc="left"); ax.legend(); ax.set_ylabel("Share of eligible works (%)") style.save(fig, f"{out}/fig_types_openness.pdf"); reg("fig_types_openness", "Document-type composition and metadata completeness by year.", ["results/bibliometrics/volume_yearly.csv"]) def fig_collab(R, out): p = f"{R}/results/bibliometrics/team_size_yearly.csv" if not os.path.exists(p): return d = pd.read_csv(p); d = d[d.year <= 2026]; ind = pd.read_csv(f"{R}/results/bibliometrics/industry_share_yearly.csv") fig, axes = plt.subplots(1, 2, figsize=(7.2, 3.0)) ax = axes[0]; ax.plot(d.year, d.mean_authors, marker="o", color=style.CATEGORICAL[0], label="Mean authors per work"); ax.set_ylabel("Authors"); ax.set_title("Team size", loc="left"); ax.legend() ax = axes[1]; ax.plot(d.year, 100 * d.intl / d.n, marker="o", color=style.CATEGORICAL[1], label="≥ 2 countries (all works)") ax.plot(ind.year, 100 * ind.share_company, marker="o", color=style.CATEGORICAL[2], label="Company affiliation (works with affiliations)") ax.set_ylabel("Share (%)"); ax.set_title("International and industry participation", loc="left"); ax.legend() style.save(fig, f"{out}/fig_collaboration.pdf"); reg("fig_collaboration", "Team size, international co-authorship and industry participation by year.", ["results/bibliometrics/team_size_yearly.csv", "results/bibliometrics/industry_share_yearly.csv"]) def main(): ap = argparse.ArgumentParser(); ap.add_argument("--root", required=True); a = ap.parse_args(); out = f"{a.root}/figures"; os.makedirs(out, exist_ok=True) for f in (fig_prisma, fig_yearly_volume, fig_monthly, fig_types_sources, fig_collab) + _BATCH2 + _BATCH3: try: f(a.root, out); print("ok", f.__name__) except Exception as e: print("FAIL", f.__name__, e) json.dump(INV, open(f"{out}/FIGURES.json", "w"), indent=1) # ---------------- batch 2: citations and knowledge flows (generated from results/bibliometrics + results/graph) ---------------- def fig_citation_inequality(R, out): p = f"{R}/results/bibliometrics/citation_windows_yearly.csv" if not os.path.exists(p): return d = pd.read_csv(p); d = d[d.year <= 2023] # c36 requires three full years of citation data fig, axes = plt.subplots(1, 2, figsize=(7.2, 3.0)) ax = axes[0]; ax.plot(d.year, d.mean_c36, marker="o", color=style.CATEGORICAL[0], label="Mean c36"); ax.plot(d.year, d.p99_c36 / 10, marker="o", color=style.CATEGORICAL[1], label="p99 c36 ÷ 10") ax.set_title("Fixed-window citations (36 months)", loc="left"); ax.set_ylabel("Citations"); ax.legend() ax = axes[1]; ax.plot(d.year, d.gini_c36, marker="o", color=style.CATEGORICAL[2], label="Gini of c36"); ax.plot(d.year, d.share_uncited_36, marker="o", color=style.CATEGORICAL[3], label="Share uncited at 36 months") ax.set_title("Citation inequality", loc="left"); ax.set_ylim(0, 1); ax.legend() style.save(fig, f"{out}/fig_citation_inequality.pdf"); reg("fig_citation_inequality", "Fixed-window citation levels and inequality by publication year (years with complete 36-month windows).", ["results/bibliometrics/citation_windows_yearly.csv"]) def fig_field_inflows(R, out): p = f"{R}/results/graph/field_flows_yearly.csv" if not os.path.exists(p): return d = pd.read_csv(p); d = d[(d.citing_year >= 2018) & (d.citing_year <= 2026)] names = {17: "CS outside the corpus (mostly pre-2018)", 26: "Mathematics", 22: "Engineering", 31: "Physics & astronomy", 27: "Medicine", 33: "Social sciences", 13: "Biochem./genetics", 18: "Decision sciences", 28: "Neuroscience", 20: "Economics & finance", 32: "Psychology", 12: "Arts & humanities", 36: "Health professions", 11: "Agricultural & biological", 23: "Environmental science", 16: "Chemistry", 25: "Materials science", 14: "Business & management"} tot = d.groupby("citing_year").n.sum(); ext = d[d.kind == "external"]; internal = d[d.kind == "internal"].groupby("citing_year").n.sum() / tot * 100 top = ext.groupby("cited_field_id").n.sum().nlargest(7).index fig, ax = plt.subplots(figsize=(6.8, 3.6)) ax.plot(internal.index, internal.values, marker="o", color=style.INK2, label="Inside the corpus (CS, 2018–2026)", lw=1.4) ax.annotate("inside corpus", (internal.index[-1], internal.values[-1]), xytext=(3, 0), textcoords="offset points", fontsize=6.5, color=style.INK2, va="center") for i, f in enumerate(top): s = ext[ext.cited_field_id == f].groupby("citing_year").n.sum() / tot * 100 ax.plot(s.index, s.values, marker="o", color=style.CATEGORICAL[i % 8], label=names.get(int(f), f"field {int(f)}"), lw=1.2) if s.values[-1] > 5: ax.annotate(names.get(int(f), str(int(f))), (s.index[-1], s.values[-1]), xytext=(3, 0), textcoords="offset points", fontsize=6.5, color=style.INK2, va="center") ax.set_ylabel("Share of all resolved references (%)"); ax.set_title("Where the references of the CS corpus point, by citing year", loc="left"); ax.legend(ncol=2, fontsize=6.5) style.save(fig, f"{out}/fig_field_inflows.pdf"); reg("fig_field_inflows", "Share of references pointing outside the corpus, by cited OpenAlex field and citing year.", ["results/graph/field_flows_yearly.csv"]) def fig_cited_age(R, out): p = f"{R}/results/graph/cited_age_yearly.csv" if not os.path.exists(p): return d = pd.read_csv(p); d = d[(d.age >= 0) & (d.age <= 30)] fig, ax = plt.subplots(figsize=(6.8, 3.2)) for i, y in enumerate([2018, 2020, 2022, 2024, 2026]): g = d[d.citing_year == y]; s = g.n / g.n.sum() ax.plot(g.age, s * 100, color=style.SEQUENTIAL[2 + 2 * i], label=str(y), lw=1.3) ax.set_xlabel("Age of cited work (years)"); ax.set_ylabel("Share of references (%)"); ax.set_title("Reference age distribution by citing year", loc="left"); ax.legend(title="Citing year") style.save(fig, f"{out}/fig_cited_age.pdf"); reg("fig_cited_age", "Distribution of the age of cited works for selected citing years.", ["results/graph/cited_age_yearly.csv"]) def fig_venue_hhi(R, out): p = f"{R}/results/bibliometrics/venue_hhi_yearly.csv" if not os.path.exists(p): return d = pd.read_csv(p); d = d[d.year <= 2026] fig, axes = plt.subplots(1, 2, figsize=(7.2, 3.0)) axes[0].plot(d.year, d.n_sources, marker="o", color=style.CATEGORICAL[0]); axes[0].set_title("Distinct sources (venues) per year", loc="left") axes[1].plot(d.year, d.hhi * 1e4, marker="o", color=style.CATEGORICAL[1], label="HHI × 10⁴"); axes[1].plot(d.year, d.top1_share * 100, marker="o", color=style.CATEGORICAL[2], label="Top source share (%)"); axes[1].set_title("Venue concentration", loc="left"); axes[1].legend() style.save(fig, f"{out}/fig_venue_concentration.pdf"); reg("fig_venue_concentration", "Number of distinct venues and venue concentration (HHI, top-source share) by year.", ["results/bibliometrics/venue_hhi_yearly.csv"]) def fig_countries(R, out): p = f"{R}/results/bibliometrics/countries_yearly.csv" if not os.path.exists(p): return d = pd.read_csv(p); d = d[d.year <= 2026]; tot = d.groupby("year").n_frac.sum(); top = d.groupby("country").n_frac.sum().nlargest(8).index fig, ax = plt.subplots(figsize=(6.8, 3.4)) for i, c in enumerate(top): s = d[d.country == c].set_index("year").n_frac / tot * 100 ax.plot(s.index, s.values, marker="o", color=style.CATEGORICAL[i], label=c, lw=1.2) if i < 4: ax.annotate(c, (s.index[-1], s.values[-1]), xytext=(3, 0), textcoords="offset points", fontsize=7, color=style.INK2, va="center") ax.set_ylabel("Share of fractional output (%)"); ax.set_title("Country shares of eligible CS output (fractional counting, top 8)", loc="left"); ax.legend(ncol=4, fontsize=7) style.save(fig, f"{out}/fig_country_shares.pdf"); reg("fig_country_shares", "Country shares of output with fractional counting, top eight countries.", ["results/bibliometrics/countries_yearly.csv"]) _BATCH2 = (fig_citation_inequality, fig_field_inflows, fig_cited_age, fig_venue_hhi, fig_countries) # ---------------- batch 3: structural change (results/econometrics) ---------------- def fig_its_total(R, out): p = f"{R}/results/econometrics/fit_total.csv" if not os.path.exists(p): return d = pd.read_csv(p); x = pd.to_datetime(d.month + "-01"); res = json.load(open(f"{R}/results/econometrics/its_results.json"))["series"]["total"] fig, ax = plt.subplots(figsize=(6.8, 3.3)) ax.plot(x, d.y / 1e3, color=style.CATEGORICAL[0], lw=1.0, label="Observed (month-precision records)") ax.plot(x, d.fitted / 1e3, color=style.CATEGORICAL[1], lw=1.6, label="ITS fit (log-linear, seasonal, HAC)") style.event_line(ax, pd.Timestamp("2022-11-30")) its = res["its_chatgpt"] ax.text(0.02, 0.97, f"level change {its['level_change']:+.3f} (p = {its['level_p']:.2f}; placebo p = {res['placebo_chatgpt']['level_t_placebo_p']:.2f})\nslope change {its['slope_change']:+.4f}/month (p = {its['slope_p']:.3f}; placebo p = {res['placebo_chatgpt']['slope_t_placebo_p']:.2f})", transform=ax.transAxes, fontsize=7, va="top", color=style.INK2) ax.set_ylabel("Works per month (thousands)"); ax.set_title("Interrupted time series of monthly CS output at the 2022-11 reference date", loc="left"); ax.legend(loc="center left", bbox_to_anchor=(0.0, 0.62)) style.save(fig, f"{out}/fig_its_total.pdf"); reg("fig_its_total", "Monthly output with the interrupted-time-series fit; level and slope changes with HAC and placebo p-values.", ["results/econometrics/fit_total.csv", "results/econometrics/its_results.json"]) def fig_placebo_total(R, out): p = f"{R}/results/econometrics/placebo_total.csv" if not os.path.exists(p): return d = pd.read_csv(p); d["event"] = pd.to_datetime(d.event) fig, axes = plt.subplots(1, 2, figsize=(7.2, 3.0)) for ax, col, lab in [(axes[0], "level_t", "Level-change t-statistic"), (axes[1], "slope_t", "Slope-change t-statistic")]: ax.plot(d.event, d[col], color=style.CATEGORICAL[0], lw=1.2); ax.axhline(0, color=style.AXIS, lw=0.6) tv = d[d.event == pd.Timestamp("2022-12-01")] if len(tv): ax.plot(tv.event, tv[col], "o", color=style.CATEGORICAL[1], ms=6); ax.annotate("2022-12", (tv.event.iloc[0], tv[col].iloc[0]), xytext=(4, 4), textcoords="offset points", fontsize=7, color=style.INK2) ax.set_title(lab, loc="left"); ax.set_xlabel("Placebo event month") style.save(fig, f"{out}/fig_placebo_total.pdf"); reg("fig_placebo_total", "Placebo distribution of ITS t-statistics over every candidate event month; the true event is highlighted.", ["results/econometrics/placebo_total.csv"]) _BATCH3 = (fig_its_total, fig_placebo_total) if __name__ == "__main__": main()