saladday1 commited on
Commit
b357068
·
verified ·
1 Parent(s): ef5d141

code: sync from git

Browse files
Files changed (2) hide show
  1. code/hfeco/metrics.py +22 -5
  2. code/hfeco/org.py +1 -1
code/hfeco/metrics.py CHANGED
@@ -10,24 +10,41 @@ HF_ORDER = ["adapter", "finetune", "merge", "quantized"]
10
 
11
 
12
  def daily_downloads(raw):
13
- """Per repo and day: downloads since the previous snapshot, per 24h.
14
 
15
  The first time a repo is seen it has no previous value, so it contributes nothing
16
  that day (its all-time total is not counted as one day's downloads).
 
 
 
 
 
17
  """
18
  df = raw[raw.kind != "space"][["repo_id", "kind", "day", "fetched_at", "downloads_all_time"]].copy()
19
  df["t"] = pd.to_datetime(df.fetched_at)
20
  df = df.sort_values(["kind", "repo_id", "t"])
 
 
 
 
 
 
 
21
  g = df.groupby(["kind", "repo_id"])
22
  df["prev"] = g.downloads_all_time.shift()
23
- df["prev_t"] = g.t.shift()
24
- hours = (df.t - df.prev_t).dt.total_seconds() / 3600
25
  delta = df.downloads_all_time - df.prev
26
  df["negative"] = delta < 0
27
- df["daily"] = (delta.clip(lower=0) * 24 / hours).where(df.prev.notna())
28
  return df[["kind", "repo_id", "day", "daily", "negative"]]
29
 
30
 
 
 
 
 
 
 
31
  def build(raw, labels, hf_tree_counts, fam, authors=None):
32
  """The dashboard's data file. The Space may become public, so this is a whitelist of
33
  what the page renders: no evidence, confidence, declaration status or flags, and no
@@ -39,7 +56,7 @@ def build(raw, labels, hf_tree_counts, fam, authors=None):
39
  cur = raw[raw.day == today].merge(lab, on=["kind", "repo_id"], how="left")
40
  dd = daily_downloads(raw)
41
  dd = dd[dd.kind == "model"]
42
- last = dd[dd.day == today].set_index("repo_id").daily.to_dict()
43
  famdf = cur[cur.family.fillna(False).astype(bool)]
44
 
45
  out = {"today": today, "snapshot_days": days,
 
10
 
11
 
12
  def daily_downloads(raw):
13
+ """Per repo and day: downloads per day since the previous snapshot.
14
 
15
  The first time a repo is seen it has no previous value, so it contributes nothing
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, so a snapshot taken before the next
19
+ refresh repeats the previous one: such a snapshot (no counter moved) is dropped, and that day
20
+ has no value rather than zero. A difference is spread over the whole days between the two
21
+ snapshots, at least one, since it holds at least one refresh.
22
  """
23
  df = raw[raw.kind != "space"][["repo_id", "kind", "day", "fetched_at", "downloads_all_time"]].copy()
24
  df["t"] = pd.to_datetime(df.fetched_at)
25
  df = df.sort_values(["kind", "repo_id", "t"])
26
+ delta = df.downloads_all_time - df.groupby(["kind", "repo_id"]).downloads_all_time.shift()
27
+ snap = pd.DataFrame({"kind": df.kind, "day": df.day, "seen": delta.notna(), "moved": delta.fillna(0).ne(0)})
28
+ snap = snap.groupby(["kind", "day"]).agg(seen=("seen", "any"), moved=("moved", "any"))
29
+ stale = set(snap[snap.seen & ~snap.moved].index)
30
+ if stale:
31
+ df = df[[(k, d) not in stale for k, d in zip(df.kind, df.day)]]
32
+
33
  g = df.groupby(["kind", "repo_id"])
34
  df["prev"] = g.downloads_all_time.shift()
35
+ days = ((df.t - g.t.shift()).dt.total_seconds() / 86400).round().clip(lower=1)
 
36
  delta = df.downloads_all_time - df.prev
37
  df["negative"] = delta < 0
38
+ df["daily"] = (delta.clip(lower=0) / days).where(df.prev.notna())
39
  return df[["kind", "repo_id", "day", "daily", "negative"]]
40
 
41
 
42
+ def latest_daily(dd):
43
+ """repo_id -> its most recent daily downloads (the last snapshot may have been dropped)."""
44
+ d = dd.dropna(subset=["daily"]).sort_values("day")
45
+ return d.groupby("repo_id").daily.last().to_dict()
46
+
47
+
48
  def build(raw, labels, hf_tree_counts, fam, authors=None):
49
  """The dashboard's data file. The Space may become public, so this is a whitelist of
50
  what the page renders: no evidence, confidence, declaration status or flags, and no
 
56
  cur = raw[raw.day == today].merge(lab, on=["kind", "repo_id"], how="left")
57
  dd = daily_downloads(raw)
58
  dd = dd[dd.kind == "model"]
59
+ last = latest_daily(dd)
60
  famdf = cur[cur.family.fillna(False).astype(bool)]
61
 
62
  out = {"today": today, "snapshot_days": days,
code/hfeco/org.py CHANGED
@@ -189,7 +189,7 @@ def build(rels, found, deep_data, who):
189
  days = sorted(snaps.day.unique())
190
  today = days[-1]
191
  d1 = metrics.daily_downloads(snaps.assign(kind="model"))
192
- last = d1[d1.day == today].set_index("repo_id").daily.to_dict()
193
  comm_all = pd.DataFrame(list(found.values())) if found else pd.DataFrame(
194
  columns=["repo_id", "release", "up", "depth", "relation", "created_at", "downloads_30d",
195
  "downloads_all_time", "likes", "nsfw"])
 
189
  days = sorted(snaps.day.unique())
190
  today = days[-1]
191
  d1 = metrics.daily_downloads(snaps.assign(kind="model"))
192
+ last = metrics.latest_daily(d1)
193
  comm_all = pd.DataFrame(list(found.values())) if found else pd.DataFrame(
194
  columns=["repo_id", "release", "up", "depth", "relation", "created_at", "downloads_30d",
195
  "downloads_all_time", "likes", "nsfw"])