code: sync from git
Browse files- code/README.md +2 -1
- code/hfeco/metrics.py +20 -11
code/README.md
CHANGED
|
@@ -40,7 +40,8 @@ eval/ 分类准确率评估(独立标注 + 对比)
|
|
| 40 |
```
|
| 41 |
raw/<family>/YYYY-MM-DD.parquet 每天一份,只追加不修改;每日下载在读取时由相邻两份相减得到。
|
| 42 |
HF 的下载计数每天批量刷新一次(约 08:40–13:30 UTC 之间,零星仓库会提前动):
|
| 43 |
-
|
|
|
|
| 44 |
labels/<family>/latest.parquet 当前分类结果,带规则版本号,可随时重算
|
| 45 |
cache/<family>/enrich.jsonl 每个仓库的证据,按仓库 sha 缓存,没变的仓库不重复读
|
| 46 |
cache/authors.json 作者头像和显示名
|
|
|
|
| 40 |
```
|
| 41 |
raw/<family>/YYYY-MM-DD.parquet 每天一份,只追加不修改;每日下载在读取时由相邻两份相减得到。
|
| 42 |
HF 的下载计数每天批量刷新一次(约 08:40–13:30 UTC 之间,零星仓库会提前动):
|
| 43 |
+
总增量不到"平常一天"(30 天下载 / 30)1/5 的快照算还没刷新,当天不出数(不是 0),
|
| 44 |
+
差值算进下一份;差值除以两次快照之间经过的刷新次数(按每天 11:00 UTC 算,至少 1)
|
| 45 |
labels/<family>/latest.parquet 当前分类结果,带规则版本号,可随时重算
|
| 46 |
cache/<family>/enrich.jsonl 每个仓库的证据,按仓库 sha 缓存,没变的仓库不重复读
|
| 47 |
cache/authors.json 作者头像和显示名
|
code/hfeco/metrics.py
CHANGED
|
@@ -9,6 +9,9 @@ REL_ORDER = ["adapter", "finetune", "quantized", "repackage", "merge", "mirror",
|
|
| 9 |
HF_ORDER = ["adapter", "finetune", "merge", "quantized"]
|
| 10 |
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
def daily_downloads(raw):
|
| 13 |
"""Per repo and day: downloads per day since the previous snapshot.
|
| 14 |
|
|
@@ -16,29 +19,35 @@ def daily_downloads(raw):
|
|
| 16 |
that day (its all-time total is not counted as one day's downloads).
|
| 17 |
|
| 18 |
HF refreshes its download counters in a daily batch (a few repos trickle in between), so a
|
| 19 |
-
snapshot taken before the next refresh nearly repeats the previous one
|
| 20 |
-
|
| 21 |
-
than zero, and what did trickle in counts with the next
|
| 22 |
-
|
|
|
|
| 23 |
"""
|
| 24 |
cols = ["repo_id", "kind", "day", "fetched_at", "downloads_all_time"]
|
| 25 |
df = raw[raw.kind != "space"][cols + (["downloads_30d"] if "downloads_30d" in raw else [])].copy()
|
| 26 |
-
df["t"] = pd.to_datetime(df.fetched_at)
|
| 27 |
df = df.sort_values(["kind", "repo_id", "t"])
|
| 28 |
delta = df.downloads_all_time - df.groupby(["kind", "repo_id"]).downloads_all_time.shift()
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if stale:
|
| 34 |
df = df[[(k, d) not in stale for k, d in zip(df.kind, df.day)]]
|
| 35 |
|
| 36 |
g = df.groupby(["kind", "repo_id"])
|
| 37 |
df["prev"] = g.downloads_all_time.shift()
|
| 38 |
-
|
|
|
|
| 39 |
delta = df.downloads_all_time - df.prev
|
| 40 |
df["negative"] = delta < 0
|
| 41 |
-
df["daily"] = (delta.clip(lower=0) /
|
| 42 |
return df[["kind", "repo_id", "day", "daily", "negative"]]
|
| 43 |
|
| 44 |
|
|
|
|
| 9 |
HF_ORDER = ["adapter", "finetune", "merge", "quantized"]
|
| 10 |
|
| 11 |
|
| 12 |
+
REFRESH_UTC_HOUR = 11 # HF's daily counter refresh lands somewhere around 08:40-13:30 UTC
|
| 13 |
+
|
| 14 |
+
|
| 15 |
def daily_downloads(raw):
|
| 16 |
"""Per repo and day: downloads per day since the previous snapshot.
|
| 17 |
|
|
|
|
| 19 |
that day (its all-time total is not counted as one day's downloads).
|
| 20 |
|
| 21 |
HF refreshes its download counters in a daily batch (a few repos trickle in between), so a
|
| 22 |
+
snapshot taken before the next refresh nearly repeats the previous one. When the counters
|
| 23 |
+
moved by less than a fifth of a usual day (the repos' 30-day downloads / 30), the snapshot is
|
| 24 |
+
dropped: that day has no value rather than zero, and what did trickle in counts with the next
|
| 25 |
+
one. A difference is divided by the number of refreshes between the two snapshots (taken to
|
| 26 |
+
land at REFRESH_UTC_HOUR each day), at least one.
|
| 27 |
"""
|
| 28 |
cols = ["repo_id", "kind", "day", "fetched_at", "downloads_all_time"]
|
| 29 |
df = raw[raw.kind != "space"][cols + (["downloads_30d"] if "downloads_30d" in raw else [])].copy()
|
| 30 |
+
df["t"] = pd.to_datetime(df.fetched_at, utc=True, format="ISO8601")
|
| 31 |
df = df.sort_values(["kind", "repo_id", "t"])
|
| 32 |
delta = df.downloads_all_time - df.groupby(["kind", "repo_id"]).downloads_all_time.shift()
|
| 33 |
+
if "downloads_30d" in df:
|
| 34 |
+
usual = df.downloads_30d.fillna(0) / 30
|
| 35 |
+
else:
|
| 36 |
+
usual = df.downloads_all_time.notna().astype(float) # no 30-day figure: one per repo
|
| 37 |
+
snap = pd.DataFrame({"kind": df.kind, "day": df.day, "seen": delta.notna(),
|
| 38 |
+
"moved": delta.clip(lower=0).fillna(0), "usual": usual.where(delta.notna(), 0)})
|
| 39 |
+
snap = snap.groupby(["kind", "day"])[["seen", "moved", "usual"]].sum()
|
| 40 |
+
stale = set(snap[(snap.seen > 0) & (snap.moved < 0.2 * snap.usual)].index)
|
| 41 |
if stale:
|
| 42 |
df = df[[(k, d) not in stale for k, d in zip(df.kind, df.day)]]
|
| 43 |
|
| 44 |
g = df.groupby(["kind", "repo_id"])
|
| 45 |
df["prev"] = g.downloads_all_time.shift()
|
| 46 |
+
shift = pd.Timedelta(hours=REFRESH_UTC_HOUR)
|
| 47 |
+
refreshes = ((df.t - shift).dt.floor("D") - (g.t.shift() - shift).dt.floor("D")).dt.days.clip(lower=1)
|
| 48 |
delta = df.downloads_all_time - df.prev
|
| 49 |
df["negative"] = delta < 0
|
| 50 |
+
df["daily"] = (delta.clip(lower=0) / refreshes).where(df.prev.notna())
|
| 51 |
return df[["kind", "repo_id", "day", "daily", "negative"]]
|
| 52 |
|
| 53 |
|