From 5c02e8a71098bf0e1be2e882738bb5997f7d0846 Mon Sep 17 00:00:00 2001 From: moritsbw-cpu Date: Tue, 17 Nov 2026 13:02:08 +0000 Subject: [PATCH] Add model: quiet former favourite flag (DR archive 2019-24), subgroup scores in the event backtest Co-Authored-By: Claude Opus 5.5 --- docs/BACKTEST-EVENTS.md | 141 +++++++++++++++++++++------------------- src/model/adds.rs | 36 ++++++++-- src/model/mod.rs | 1 + src/model/panel.rs | 17 ++++- 4 files changed, 122 insertions(+), 73 deletions(-) diff --git a/docs/BACKTEST-EVENTS.md b/docs/BACKTEST-EVENTS.md index 0e161c7..1cf73a6 100644 --- a/docs/BACKTEST-EVENTS.md +++ b/docs/BACKTEST-EVENTS.md @@ -1,94 +1,103 @@ # Backtest: if the artist releases a new song, will DR add it? -Events: an artist knowable at the time releases something (Deezer) in week t0+1, with no other release in the 6 weeks before (so an add can't be the previous song's); target: a DR add (entering rotation after 4+ weeks out, or plays jumping well above the artist's own 8-week baseline and staying up) within 2 week(s) from the release week. The model knows the artist's state the week before; pre-release rows blend the new song's NMF Denmark placement from the artist's history. "Release timing only" = the add rate by weeks since the artist's previous release. 3193 release events scored, 63 (2.0%) got an add on some DR channel. +Events: an artist knowable at the time releases something (Deezer) in week t0+1, with no other release in the 6 weeks before (so an add can't be the previous song's); target: a DR add (entering rotation after 4+ weeks out, or plays jumping well above the artist's own 8-week baseline and staying up) within 2 week(s) from the release week. The model knows the artist's state the week before; pre-release rows blend the new song's NMF Denmark placement from the artist's history. "Release timing only" = the add rate by weeks since the artist's previous release. 3269 release events scored, 63 (1.9%) got an add on some DR channel. | Model | Log-loss, per channel | PR-AUC, per channel | Log-loss, any channel | PR-AUC, any channel | |---|---|---|---|---| -| rate by in-rotation-now | 0.0245 | 0.015 | 0.0941 | 0.062 | -| rate by last-12-weeks level | 0.0174 | 0.082 | 0.0738 | 0.140 | -| release timing only | 0.0267 | 0.003 | 0.0982 | 0.021 | -| model, calibrated on the last 6 origins (pre-release) | 0.0183 | 0.080 | 0.0748 | 0.170 | -| model, raw (pre-release) | 0.0182 | 0.091 | 0.0696 | 0.233 | -| model + pooled Platt (pre-release) | 0.0177 | 0.094 | 0.0704 | 0.224 | -| model + pooled isotonic (pre-release) | 0.0176 | 0.091 | 0.0706 | 0.235 | -| model, NMF known (post-release) | 0.0184 | 0.085 | 0.0750 | 0.163 | +| rate by in-rotation-now | 0.0240 | 0.014 | 0.0923 | 0.062 | +| rate by last-12-weeks level | 0.0170 | 0.082 | 0.0721 | 0.140 | +| release timing only | 0.0262 | 0.003 | 0.0964 | 0.020 | +| model, calibrated on the last 6 origins (pre-release) | 0.0179 | 0.085 | 0.0723 | 0.179 | +| model, raw (pre-release) | 0.0178 | 0.097 | 0.0676 | 0.245 | +| model + pooled Platt (pre-release) | 0.0172 | 0.098 | 0.0683 | 0.232 | +| model + pooled isotonic (pre-release) | 0.0175 | 0.088 | 0.0689 | 0.252 | +| model, NMF known (post-release) | 0.0180 | 0.090 | 0.0723 | 0.170 | Skill (1 − log-loss ÷ baseline log-loss; 90% interval from a bootstrap over blocks of 8 forecast weeks): | Model | Per channel vs last-12-weeks | Per channel vs release timing | Any channel vs last-12-weeks | Any channel vs release timing | |---|---|---|---|---| -| model, calibrated on the last 6 origins (pre-release) | -0.053 (-0.131 to +0.002) | +0.313 (+0.257 to +0.378) | -0.013 (-0.017 to +0.048) | +0.239 (+0.226 to +0.288) | -| model + pooled Platt (pre-release) | -0.015 (-0.053 to +0.020) | +0.338 (+0.286 to +0.399) | +0.046 (-0.008 to +0.107) | +0.283 (+0.234 to +0.334) | -| model + pooled isotonic (pre-release) | -0.010 (-0.095 to +0.039) | +0.342 (+0.234 to +0.430) | +0.044 (-0.011 to +0.100) | +0.281 (+0.223 to +0.335) | +| model, calibrated on the last 6 origins (pre-release) | -0.054 (-0.109 to -0.010) | +0.315 (+0.264 to +0.373) | -0.002 (-0.006 to +0.060) | +0.250 (+0.237 to +0.299) | +| model + pooled Platt (pre-release) | -0.010 (-0.041 to +0.026) | +0.343 (+0.296 to +0.404) | +0.053 (+0.000 to +0.115) | +0.291 (+0.243 to +0.343) | +| model + pooled isotonic (pre-release) | -0.028 (-0.082 to +0.022) | +0.331 (+0.248 to +0.418) | +0.045 (-0.000 to +0.098) | +0.286 (+0.228 to +0.338) | Calibration, any channel: | Predicted | model, calibrated on the last 6 origins (pre-release): rows / mean predicted / observed | model + pooled Platt (pre-release): rows / mean predicted / observed | model + pooled isotonic (pre-release): rows / mean predicted / observed | |---|---|---|---| -| 0.0–0.1 | 3081 / 0.015 / 0.013 | 3033 / 0.011 / 0.010 | 2979 / 0.009 / 0.008 | -| 0.1–0.2 | 82 / 0.136 / 0.207 | 107 / 0.140 / 0.168 | 169 / 0.135 / 0.160 | -| 0.2–0.3 | 16 / 0.245 / 0.312 | 20 / 0.239 / 0.300 | 16 / 0.238 / 0.188 | -| 0.3–0.4 | 6 / 0.316 / 0.333 | 12 / 0.340 / 0.250 | 8 / 0.344 / 0.375 | -| 0.4–0.5 | 4 / 0.451 / 0.000 | 13 / 0.437 / 0.231 | 8 / 0.436 / 0.250 | -| 0.5–0.6 | 2 / 0.533 / 0.000 | 6 / 0.566 / 0.667 | 4 / 0.563 / 0.250 | -| 0.6–0.7 | 2 / 0.636 / 0.000 | 1 / 0.605 / 0.000 | 2 / 0.620 / 0.000 | -| 0.7–0.8 | — | — | 5 / 0.775 / 0.600 | -| 0.8–0.9 | — | 1 / 0.818 / 0.000 | 2 / 0.827 / 0.500 | +| 0.0–0.1 | 3159 / 0.014 / 0.012 | 3108 / 0.011 / 0.009 | 3100 / 0.009 / 0.009 | +| 0.1–0.2 | 80 / 0.140 / 0.212 | 103 / 0.140 / 0.165 | 105 / 0.137 / 0.152 | +| 0.2–0.3 | 15 / 0.259 / 0.200 | 23 / 0.227 / 0.217 | 33 / 0.227 / 0.273 | +| 0.3–0.4 | 10 / 0.337 / 0.400 | 17 / 0.345 / 0.353 | 9 / 0.353 / 0.222 | +| 0.4–0.5 | 2 / 0.440 / 0.000 | 8 / 0.438 / 0.250 | 7 / 0.445 / 0.429 | +| 0.5–0.6 | 1 / 0.517 / 0.000 | 5 / 0.534 / 0.200 | 4 / 0.561 / 0.000 | +| 0.6–0.7 | 2 / 0.658 / 0.000 | 4 / 0.631 / 0.750 | 5 / 0.644 / 0.200 | +| 0.7–0.8 | — | — | 1 / 0.709 / 1.000 | +| 0.8–0.9 | — | 1 / 0.813 / 0.000 | 5 / 0.839 / 0.600 | Calibration, per channel: | Predicted | model, calibrated on the last 6 origins (pre-release): rows / mean predicted / observed | model + pooled Platt (pre-release): rows / mean predicted / observed | model + pooled isotonic (pre-release): rows / mean predicted / observed | |---|---|---|---| -| 0.0–0.1 | 44463 / 0.004 / 0.003 | 44305 / 0.002 / 0.003 | 44334 / 0.003 / 0.003 | -| 0.1–0.2 | 125 / 0.136 / 0.128 | 229 / 0.137 / 0.105 | 278 / 0.123 / 0.090 | -| 0.2–0.3 | 69 / 0.259 / 0.159 | 86 / 0.242 / 0.093 | 30 / 0.245 / 0.067 | -| 0.3–0.4 | 21 / 0.337 / 0.048 | 42 / 0.346 / 0.071 | 35 / 0.358 / 0.057 | -| 0.4–0.5 | 8 / 0.456 / 0.000 | 28 / 0.454 / 0.143 | 13 / 0.448 / 0.308 | -| 0.5–0.6 | 11 / 0.527 / 0.000 | 6 / 0.540 / 0.500 | 6 / 0.546 / 0.500 | -| 0.6–0.7 | 3 / 0.658 / 0.333 | 4 / 0.634 / 0.250 | 1 / 0.697 / 0.000 | -| 0.7–0.8 | 2 / 0.745 / 0.000 | 2 / 0.770 / 0.000 | 5 / 0.754 / 0.200 | +| 0.0–0.1 | 45525 / 0.003 / 0.003 | 45392 / 0.002 / 0.003 | 45368 / 0.003 / 0.003 | +| 0.1–0.2 | 126 / 0.135 / 0.135 | 182 / 0.133 / 0.082 | 301 / 0.124 / 0.083 | +| 0.2–0.3 | 39 / 0.247 / 0.256 | 95 / 0.246 / 0.168 | 36 / 0.242 / 0.056 | +| 0.3–0.4 | 54 / 0.340 / 0.037 | 54 / 0.333 / 0.093 | 32 / 0.341 / 0.094 | +| 0.4–0.5 | 8 / 0.467 / 0.125 | 25 / 0.461 / 0.160 | 10 / 0.447 / 0.400 | +| 0.5–0.6 | 9 / 0.533 / 0.000 | 12 / 0.521 / 0.250 | 8 / 0.554 / 0.375 | +| 0.6–0.7 | 1 / 0.657 / 0.000 | 4 / 0.660 / 0.000 | 7 / 0.650 / 0.000 | +| 0.7–0.8 | 4 / 0.754 / 0.250 | 2 / 0.730 / 0.500 | 4 / 0.734 / 0.250 | + + +Quiet artists (out of rotation everywhere for 26 weeks) releasing, any channel: + +| Group | Events | Adds | Rate | Mean predicted (raw) | Mean predicted (isotonic) | Log-loss (isotonic) | PR-AUC (raw) | +|---|---|---|---|---|---|---|---| +| former favourites (4+ weeks on DR 2019-24) | 326 | 7 | 0.021 | 0.025 | 0.030 | 0.1028 | 0.095 | +| other quiet artists | 2616 | 15 | 0.006 | 0.007 | 0.008 | 0.0313 | 0.126 | | Any-channel feature (all events) | Coefficient | |---|---| -| intercept | -5.682 | -| log DR plays t | +0.672 | -| log DR plays 4w | +0.859 | -| log DR plays 12w | -0.429 | -| log max DR plays 12w | +0.201 | +| intercept | -5.988 | +| log DR plays t | +0.677 | +| log DR plays 4w | +0.897 | +| log DR plays 12w | -0.425 | +| log max DR plays 12w | +0.233 | | in rotation anywhere now | -0.575 | -| station groups in rotation now (/5) | -0.131 | -| current run anywhere (/12) | -1.254 | -| weeks since in rotation anywhere (/12) | -0.225 | -| Track Top-40 score | +0.602 | -| Airplay Top-20 score | -0.736 | -| chart entry recency 6w | +0.145 | -| chart entry recency 26w | -0.668 | -| log Track Top-40 hits | -0.292 | -| log Airplay Top-20 hits | -0.136 | -| Danish | +0.821 | -| group | -0.081 | -| female | -0.185 | -| career years (/50) | +0.645 | -| debut unknown | +0.056 | +| station groups in rotation now (/5) | -0.137 | +| current run anywhere (/12) | -1.280 | +| weeks since in rotation anywhere (/12) | -0.016 | +| Track Top-40 score | +0.603 | +| Airplay Top-20 score | -0.700 | +| chart entry recency 6w | +0.172 | +| chart entry recency 26w | -0.684 | +| log Track Top-40 hits | -0.283 | +| log Airplay Top-20 hits | -0.147 | +| Danish | +0.830 | +| group | -0.078 | +| female | -0.173 | +| career years (/50) | +0.646 | +| debut unknown | +0.019 | | Christmas in window | +0.000 | | discography known | -0.000 | -| release recency 4w | -0.154 | -| release recency 12w | -1.148 | -| log releases last 52w | -0.286 | +| release recency 4w | -0.157 | +| release recency 12w | -1.118 | +| log releases last 52w | -0.285 | | releases in the window | -0.000 | | releases in window x not in rotation now | +0.575 | -| DR: weeks since last add 13-25 | -0.412 | -| DR: last add 26+ weeks ago | -0.142 | -| DR: no add in the panel yet | -0.811 | -| Airplay Top-20: last new song 13-25 weeks ago | +0.833 | -| Airplay Top-20: 0.5-1.25x own usual gap | -0.222 | -| Airplay Top-20: last new song 2+ years ago | +0.215 | -| log archive plays per 100 sampled hours (2012-21) | +0.591 | -| log DR lists with the artist before t (2023-) | +0.160 | -| DR list recency 26w | -0.506 | -| DR lists known before t at all | -0.279 | -| NMF Denmark in the window | +0.923 | -| NMF Denmark top 10 in the window | +0.249 | -| log NMF Denmark placements last 52w | +0.276 | -| share of releases on NMF Denmark (2 years) | +0.002 | -| season index of the window (log, IFPI 2002-24) | -0.071 | +| DR: weeks since last add 13-25 | -0.493 | +| DR: last add 26+ weeks ago | -0.099 | +| DR: no add in the panel yet | -0.708 | +| Airplay Top-20: last new song 13-25 weeks ago | +0.826 | +| Airplay Top-20: 0.5-1.25x own usual gap | -0.176 | +| Airplay Top-20: last new song 2+ years ago | +0.334 | +| log archive plays per 100 sampled hours (2012-21) | +0.618 | +| log DR lists with the artist before t (2023-) | +0.156 | +| DR list recency 26w | -0.607 | +| DR lists known before t at all | -0.210 | +| quiet former favourite (4+ weeks on DR 2019-24, out of rotation 26w) | -0.665 | +| NMF Denmark in the window | +0.943 | +| NMF Denmark top 10 in the window | +0.202 | +| log NMF Denmark placements last 52w | +0.251 | +| share of releases on NMF Denmark (2 years) | +0.020 | +| season index of the window (log, IFPI 2002-24) | -0.049 | diff --git a/src/model/adds.rs b/src/model/adds.rs index bc8f2db..014fb2d 100644 --- a/src/model/adds.rs +++ b/src/model/adds.rs @@ -148,17 +148,27 @@ pub fn nmf_feats(p: &Panel, a: usize, t: usize, h: usize, sc: Scenario) -> [f64; } /// Long-history covariates: DR's 2012–2021 archive and DR's real rotation lists before week t. -pub const LONG: usize = 4; +pub const LONG: usize = 5; pub const LONG_NAMES: &[&str] = &["log archive plays per 100 sampled hours (2012-21)", "log DR lists with the artist before t (2023-)", - "DR list recency 26w", "DR lists known before t at all"]; + "DR list recency 26w", "DR lists known before t at all", + "quiet former favourite (4+ weeks on DR 2019-24, out of rotation 26w)"]; -pub fn long_feats(p: &Panel, a: usize, t: usize, shift: usize) -> [f64; LONG] { +/// Not in rotation anywhere in the 26 weeks to t (as far as the panel goes back). +fn quiet(r: &Rot, a: usize, t: usize) -> bool { (t.saturating_sub(25)..=t).all(|w| !r.any(a, w)) } + +pub fn long_feats(p: &Panel, r: &Rot, a: usize, t: usize, _h: usize, sc: Scenario) -> [f64; LONG] { if off("long") { return [0.0; LONG]; } + let shift = sc.shift; + // The comeback case: a former favourite (archive pages 2019-24, before the panel) out of + // rotation for half a year. One bounded flag: a continuous favourite score next to its + // interaction with "quiet" fitted two large opposing coefficients on one panel year. + let fav = !off("fav") && p.fav_weeks[a] >= 4.0 && quiet(r, a, t); let end_t = p.weeks[t] + chrono::Duration::days(6); // A list published on a Friday is known from then. let before: Vec<&chrono::NaiveDate> = p.listed_on[a].iter().filter(|d| **d <= end_t).collect(); let since = before.last().map(|d| (end_t - **d).num_days() as f64 / 7.0 + shift as f64); - [p.archive_rate[a].ln_1p(), (before.len() as f64).ln_1p(), since.map(|s| (-s / 26.0).exp()).unwrap_or(0.0), (!before.is_empty()) as i32 as f64] + [p.archive_rate[a].ln_1p(), (before.len() as f64).ln_1p(), since.map(|s| (-s / 26.0).exp()).unwrap_or(0.0), (!before.is_empty()) as i32 as f64, + fav as i32 as f64] } /// Season: the mean log season index over the window's weeks (new Airplay Top-20 songs by week of @@ -345,7 +355,7 @@ pub fn row_channel(p: &Panel, r: &Rot, a: usize, c: usize, t: usize, h: usize, s f(!on && ratio >= 0.5), ratio.min(3.0) / 3.0, since]); out[P + 10..P + 10 + REL].copy_from_slice(&rel_feats(p, a, t, h, sc, on)); out[P + 10 + REL..P + 10 + REL + WAVE].copy_from_slice(&wave_feats(p, r, a, t, sc.shift)); - out[P + 10 + REL + WAVE..P + 10 + REL + WAVE + LONG].copy_from_slice(&long_feats(p, a, t, sc.shift)); + out[P + 10 + REL + WAVE..P + 10 + REL + WAVE + LONG].copy_from_slice(&long_feats(p, r, a, t, h, sc)); out[P + 10 + REL + WAVE + LONG..PC - SEASON - AFF].copy_from_slice(&nmf_feats(p, a, t, h, sc)); out[PC - SEASON - AFF..PC - AFF].copy_from_slice(&season_feat(p, t, h, sc.win)); out[PC - AFF..PC].copy_from_slice(&aff_feats(p, a, c, t)); @@ -374,7 +384,7 @@ pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, sc: Scenario, o out[..21].copy_from_slice(&v); out[21..21 + REL].copy_from_slice(&rel_feats(p, a, t, h, sc, r.any(a, t))); out[21 + REL..21 + REL + WAVE].copy_from_slice(&wave_feats(p, r, a, t, sc.shift)); - out[21 + REL + WAVE..21 + REL + WAVE + LONG].copy_from_slice(&long_feats(p, a, t, sc.shift)); + out[21 + REL + WAVE..21 + REL + WAVE + LONG].copy_from_slice(&long_feats(p, r, a, t, h, sc)); out[21 + REL + WAVE + LONG..PA - SEASON].copy_from_slice(&nmf_feats(p, a, t, h, sc)); out[PA - SEASON..PA].copy_from_slice(&season_feat(p, t, h, sc.win)); } @@ -779,6 +789,9 @@ pub fn backtest_events(p: &Panel, tau: &[u32; C], start: usize, win: usize, md: let (mut pool_c, mut pool_a) = (Pool(Vec::new()), Pool(Vec::new())); let lvl = |m: f64| ((m.ln_1p() / 0.5) as usize).min(7); let level = |a: usize, t: usize| (0..LAGS).map(|i| p.group_total(a, t - i, None)).sum::() / LAGS as f64; + // Release events by quiet artists (out of rotation 26 weeks): former favourites (4+ weeks on DR 2019-24) or not. + let mut favs: Vec<(f64, f64, bool)> = Vec::new(); + let mut quiet_other: Vec<(f64, f64, bool)> = Vec::new(); for t0 in start..p.w().saturating_sub(win + 1) { let m = AddModel::fit_events_win(p, &r, t0, win); // Baselines from the training events. Release timing only: by weeks since the previous release. @@ -823,6 +836,7 @@ pub fn backtest_events(p: &Panel, tau: &[u32; C], start: usize, win: usize, md: s_a.add(&[by_rot[on][0] / by_rot[on][1], by_lvl[lvl(level(a, t0))][0] / by_lvl[lvl(level(a, t0))][1], tma[0] / tma[1], q * pan + (1.0 - q) * par, raw, pl_a.apply(raw), iso_a.apply(raw), pax], y); pool_a.0.push((t0, raw, y)); + if quiet(&r, a, t0) { if p.fav_weeks[a] >= 4.0 { favs.push((raw, iso_a.apply(raw), y)); } else { quiet_other.push((raw, iso_a.apply(raw), y)); } } for c in 0..C { let yc = r.add_in(a, c, t0, 0, win); let onc = r.on(a, c, t0) as usize; @@ -840,6 +854,16 @@ pub fn backtest_events(p: &Panel, tau: &[u32; C], start: usize, win: usize, md: writeln!(md, "Events: an artist knowable at the time releases something (Deezer) in week t0+1, with no other release in the {CLEAN_WEEKS} weeks before (so an add can't be the previous song's); target: a DR add (entering rotation after {GAP}+ weeks out, or plays jumping well above the artist's own 8-week baseline and staying up) within {win} week(s) from the release week. The model knows the artist's state the week before; pre-release rows blend the new song's NMF Denmark placement from the artist's history. \"Release timing only\" = the add rate by weeks since the artist's previous release. {} release events scored, {} ({:.1}%) got an add on some DR channel.\n", s_a.n, s_a.hits, 100.0 * s_a.hits / s_a.n.max(1.0))?; table(md, &s_c, &s_a, &mut rows, win, "event_")?; + writeln!(md, "\nQuiet artists (out of rotation everywhere for 26 weeks) releasing, any channel:\n\n| Group | Events | Adds | Rate | Mean predicted (raw) | Mean predicted (isotonic) | Log-loss (isotonic) | PR-AUC (raw) |\n|---|---|---|---|---|---|---|---|")?; + for (name, v) in [("former favourites (4+ weeks on DR 2019-24)", &favs), ("other quiet artists", &quiet_other)] { + let n = v.len().max(1) as f64; + let hits = v.iter().filter(|x| x.2).count(); + writeln!(md, "| {name} | {} | {hits} | {:.3} | {:.3} | {:.3} | {:.4} | {:.3} |", v.len(), hits as f64 / n, + v.iter().map(|x| x.0).sum::() / n, v.iter().map(|x| x.1).sum::() / n, + v.iter().map(|x| ll(x.1.clamp(1e-6, 1.0 - 1e-6), x.2)).sum::() / n, + average_precision(v.iter().map(|x| (x.0, x.2)).collect()))?; + } + writeln!(md)?; let m = AddModel::fit_events_win(p, &r, p.w() - 1, win); writeln!(md, "| Any-channel feature (all events) | Coefficient |\n|---|---|")?; for (n, b) in ANY_NAMES.iter().chain(REL_NAMES).chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_NAMES).zip(&m.any) { writeln!(md, "| {n} | {b:+.3} |")?; } diff --git a/src/model/mod.rs b/src/model/mod.rs index 71a8557..50763a6 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -115,6 +115,7 @@ pub fn run(db: &Db, args: &[String]) -> Result<()> { } Some("events") => { if let Some(r) = arg(args, "--rule") { let _ = adds::ADD_RULE.set(r); } + if let Some(w) = arg(args, "--without") { let _ = adds::WITHOUT.set(w.split(',').map(str::to_string).collect()); } let tau = common::thresholds(&p, 26); let mut md = String::new(); let win: usize = arg(args, "--w").and_then(|w| w.parse().ok()).unwrap_or(2); diff --git a/src/model/panel.rs b/src/model/panel.rs index 34b32bf..6a8a075 100644 --- a/src/model/panel.rs +++ b/src/model/panel.rs @@ -92,6 +92,8 @@ pub struct Panel { /// The first week (index) at which the artist was knowable: their first release, DR play, /// chart entry, DR list, archived play or NMF placement (0 = before the panel; w() = never). pub first_known: Vec, + /// Weeks with 20+ DR plays (all channels) on the archived artist pages, 2019-24 (before the panel). + pub fav_weeks: Vec, /// Season index per ISO week (1..=53): new songs reaching the Airplay Top-20 that week of the /// year, relative to an average week, 2002–2024 (before the DR panel), smoothed over 5 weeks. pub season: [f64; 54], @@ -187,12 +189,25 @@ impl Panel { let nmf_dk = load_nmf_dk(db, &id_of, &artists)?; let season = load_season(db)?; let (alltime, genre_fit) = load_affinity(db, &id_of, artists.len())?; - let mut p = Panel { weeks, artists, y, chart, list_tau, releases, archive_rate, listed_on, nmf_dk, season, alltime, genre_fit, first_known: Vec::new() }; + let mut p = Panel { weeks, artists, y, chart, list_tau, releases, archive_rate, listed_on, nmf_dk, season, alltime, genre_fit, first_known: Vec::new(), fav_weeks: Vec::new() }; + p.fav_weeks = load_fav_weeks(db, &id_of, p.a(), p.weeks[0])?; p.first_known = first_known(db, &p, &id_of)?; Ok(p) } } +fn load_fav_weeks(db: &Db, id_of: &HashMap, n: usize, start: NaiveDate) -> Result> { + let mut out = vec![0.0; n]; + let has: i64 = db.conn.query_row("SELECT count(*) FROM sqlite_master WHERE name = 'dr_artist_history'", [], |r| r.get(0))?; + if has == 0 { return Ok(out); } + for r in db.conn.prepare("SELECT artist_id, count(*) FROM dr_artist_history WHERE plays >= 20 AND week_start < ?1 GROUP BY artist_id")? + .query_map([start.to_string()], |r| Ok((r.get::<_, String>(0)?, r.get::<_, i64>(1)?)))? { + let (id, k) = r?; + if let Some(&a) = id_of.get(&id) { out[a] = k as f64; } + } + Ok(out) +} + /// Lower-case letters and digits (æ, ø, å kept), for matching names. pub fn key(s: &str) -> String { s.to_lowercase().chars().filter(|c| c.is_alphanumeric()).collect()