#!/usr/bin/env python3
import json
import matplotlib; matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np, collections
d=json.load(open("corpus.json"))["records"]
by=collections.defaultdict(list)
for r in d:
    if 2015<=r["year"]<=2026: by[r["year"]].append(len(r["mesh"]))
ys=sorted(by); m=[np.mean(by[y]) for y in ys]
fig,ax=plt.subplots(1,2,figsize=(12,4.8))
ax[0].plot(ys,m,"o-",color="#c0392b"); ax[0].axvline(2019.5,ls="--",c="k",lw=1)
ax[0].text(2019.6,max(m)*0.97,"period split",fontsize=8)
ax[0].set_xlabel("year"); ax[0].set_ylabel("MeSH terms per article")
ax[0].set_title("Indexing completeness drifts:\nthis alone deflates every term's rate in the later period")
ax[0].grid(alpha=.3)
r=json.load(open("results.json")); null=np.array(r["null_distribution"])
ax[1].hist(null,bins=np.arange(0,max(null.max(),6)+2)-.5,color="#95a5a6",edgecolor="white")
ax[1].axvline(r["n_significant"],color="#c0392b",lw=2.5)
ax[1].text(r["n_significant"]*0.55,ax[1].get_ylim()[1]*.6,f"observed = {r['n_significant']}",color="#c0392b",fontsize=10)
ax[1].set_xlabel("significant MeSH terms"); ax[1].set_ylabel("permutations")
ax[1].set_title(f"Year-shuffle null (n={len(null)}): the statistics are sound —\nthe problem is the data, not the test")
plt.tight_layout(); plt.savefig("figs/indexing_drift.png",dpi=150); print("figs/indexing_drift.png 已生成")
