saladday1 commited on
Commit
3e83256
·
verified ·
1 Parent(s): 4bc9886

code: sync from git

Browse files
Files changed (1) hide show
  1. code/hfeco/metrics.py +11 -8
code/hfeco/metrics.py CHANGED
@@ -15,18 +15,21 @@ def daily_downloads(raw):
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
 
 
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 (a few repos trickle in between), so a
19
+ snapshot taken before the next refresh nearly repeats the previous one: when under a fifth of
20
+ the repos with recent downloads moved, the snapshot is dropped, that day has no value rather
21
+ than zero, and what did trickle in counts with the next one. A difference is spread over the
22
+ whole days between the two snapshots, at least one, since it holds at least one refresh.
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
+ recent = df.downloads_30d.fillna(0).gt(0) if "downloads_30d" in df else True
30
+ snap = pd.DataFrame({"kind": df.kind, "day": df.day, "active": delta.notna() & recent, "moved": delta.fillna(0).ne(0)})
31
+ snap = snap.groupby(["kind", "day"])[["active", "moved"]].sum()
32
+ stale = set(snap[(snap.active > 0) & (snap.moved < 0.2 * snap.active)].index)
33
  if stale:
34
  df = df[[(k, d) not in stale for k, d in zip(df.kind, df.day)]]
35