Channel affinity (share of the artist's DR plays per station before t, never played there) and genre fit (DR genre x station) in the per-station rows; Best week: any date range up to 12 months
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+25
-4
@@ -153,6 +153,26 @@ pub fn season_feat(p: &Panel, t: usize, h: usize, win: usize) -> [f64; SEASON] {
|
||||
[v]
|
||||
}
|
||||
|
||||
/// Channel affinity and genre fit (per-channel rows only): the share of the artist's DR plays that
|
||||
/// were on this station group (all-time top tracks, minus panel plays after week t), the log of
|
||||
/// those plays, "never played here", and how much DR plays the artist's genre on this group.
|
||||
pub const AFF: usize = 4;
|
||||
pub const AFF_NAMES: &[&str] = &["share of the artist's DR plays on this station (before t)", "log DR plays on this station (before t)",
|
||||
"never played on this station (before t)", "genre fit for this station"];
|
||||
pub fn aff_feats(p: &Panel, a: usize, c: usize, t: usize) -> [f64; AFF] {
|
||||
if off("aff") { return [0.0; AFF]; }
|
||||
// Plays after week t (the future, as seen from t) taken out of the all-time counts.
|
||||
let after = |g: usize| -> f64 {
|
||||
let cs: Vec<usize> = (0..C).filter(|&k| group(k) == g).collect();
|
||||
let n = cs.len() as f64;
|
||||
(t + 1..p.w()).map(|w| cs.iter().map(|&k| p.y(a, k, w)).sum::<f64>() / n).sum()
|
||||
};
|
||||
let before: [f64; GROUPS] = std::array::from_fn(|g| (p.alltime[a][g] - after(g)).max(0.0));
|
||||
let tot: f64 = before.iter().sum();
|
||||
let g = group(c);
|
||||
[if tot > 0.0 { before[g] / tot } else { 0.0 }, before[g].ln_1p(), (before[g] == 0.0) as i32 as f64, p.genre_fit[a][g]]
|
||||
}
|
||||
|
||||
/// Release covariates (Deezer discography), appended to both row kinds.
|
||||
pub const REL: usize = 6;
|
||||
pub const REL_NAMES: &[&str] = &["discography known", "release recency 4w", "release recency 12w", "log releases last 52w",
|
||||
@@ -190,7 +210,7 @@ pub const EXTRA: &[&str] = &["in rotation on channel now", "in rotation anywhere
|
||||
"run on channel = 1 week", "run = 2 weeks", "run = 3-5 weeks", "run >= 6 weeks",
|
||||
"near threshold (>= half of tau, not in rotation)", "plays / tau (capped 3, /3)",
|
||||
"weeks since in rotation on channel (/12)"];
|
||||
pub const PC: usize = P + 10 + REL + WAVE + LONG + NMF + SEASON;
|
||||
pub const PC: usize = P + 10 + REL + WAVE + LONG + NMF + SEASON + AFF;
|
||||
|
||||
/// Artist-level ("any channel") rows.
|
||||
pub const ANY_NAMES: &[&str] = &[
|
||||
@@ -267,8 +287,9 @@ pub fn row_channel(p: &Panel, r: &Rot, a: usize, c: usize, t: usize, h: usize, s
|
||||
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 + LONG..PC - SEASON].copy_from_slice(&nmf_feats(p, a, t, h, sc));
|
||||
out[PC - SEASON..PC].copy_from_slice(&season_feat(p, t, h, sc.win));
|
||||
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));
|
||||
}
|
||||
|
||||
pub fn row_any(p: &Panel, r: &Rot, a: usize, t: usize, h: usize, sc: Scenario, out: &mut [f64]) {
|
||||
@@ -551,7 +572,7 @@ pub fn backtest(p: &Panel, tau: &[u32; C], horizons: &[usize], start: usize, md:
|
||||
for (n, b) in ANY_NAMES.iter().zip(&m.any) { writeln!(md, "| {n} | {b:+.3} |")?; }
|
||||
for (n, b) in REL_NAMES.iter().chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_NAMES).zip(&m.any[21..]) { writeln!(md, "| {n} | {b:+.3} |")?; }
|
||||
writeln!(md, "\n| Per-channel feature | Coefficient |\n|---|---|")?;
|
||||
for (n, b) in features::NAMES.iter().chain(EXTRA).chain(REL_NAMES).chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_NAMES).zip(&m.channel) { writeln!(md, "| {n} | {b:+.3} |")?; }
|
||||
for (n, b) in features::NAMES.iter().chain(EXTRA).chain(REL_NAMES).chain(WAVE_NAMES).chain(LONG_NAMES).chain(NMF_NAMES).chain(SEASON_NAMES).chain(AFF_NAMES).zip(&m.channel) { writeln!(md, "| {n} | {b:+.3} |")?; }
|
||||
writeln!(md)?;
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
+70
-1
@@ -81,6 +81,12 @@ pub struct Panel {
|
||||
pub listed_on: Vec<Vec<NaiveDate>>,
|
||||
/// New Music Friday Denmark placements per artist: (Friday, position if known), sorted.
|
||||
pub nmf_dk: Vec<Vec<(NaiveDate, Option<i64>)>>,
|
||||
/// All-time DR plays per station group (P3, P4, P5, P6, P8) from the artist page's most played
|
||||
/// tracks (P4: the national count, or the regional average when only regions are given).
|
||||
pub alltime: Vec<[f64; GROUPS]>,
|
||||
/// How much DR plays the artist's genre on each station group, relative to all music: ln of
|
||||
/// (genre's share of plays on the group / the group's share of all plays); 0 = genre unknown.
|
||||
pub genre_fit: Vec<[f64; GROUPS]>,
|
||||
/// 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],
|
||||
@@ -151,7 +157,8 @@ impl Panel {
|
||||
let (archive_rate, listed_on) = load_long_history(db, &id_of, &names, artists.len())?;
|
||||
let nmf_dk = load_nmf_dk(db, &id_of, &artists)?;
|
||||
let season = load_season(db)?;
|
||||
Ok(Panel { weeks, artists, y, chart, list_tau, releases, archive_rate, listed_on, nmf_dk, season })
|
||||
let (alltime, genre_fit) = load_affinity(db, &id_of, artists.len())?;
|
||||
Ok(Panel { weeks, artists, y, chart, list_tau, releases, archive_rate, listed_on, nmf_dk, season, alltime, genre_fit })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,3 +343,65 @@ fn load_season(db: &Db) -> Result<[f64; 54]> {
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Broad genre from DR's description ("Sanger (Afrobeats/Pop/R&B)" -> the first genre that maps).
|
||||
pub fn genre_bucket(description: &str) -> Option<usize> {
|
||||
let inner = description.split('(').nth(1)?.split(')').next()?;
|
||||
for tok in inner.split('/') {
|
||||
let t = tok.trim().to_lowercase();
|
||||
let b = if ["hip hop", "hiphop", "rap", "grime", "trap", "drill"].iter().any(|k| t.contains(k)) { 1 }
|
||||
else if ["jazz", "fusion", "bebop", "swing"].iter().any(|k| t.contains(k)) { 4 }
|
||||
else if ["klassisk", "filmmusik", "musical", "opera", "kor"].iter().any(|k| t.contains(k)) { 7 }
|
||||
else if ["r&b", "soul", "funk", "gospel", "afrobeat", "dancehall", "reggae"].iter().any(|k| t.contains(k)) { 5 }
|
||||
else if ["house", "techno", "elektronisk", "electronica", "electro", "dubstep", "dance", "disco"].iter().any(|k| t.contains(k)) { 3 }
|
||||
else if ["rock", "punk", "metal", "indie", "new wave", "blues", "grunge"].iter().any(|k| t.contains(k)) { 2 }
|
||||
else if ["folk", "country", "vise", "singer"].iter().any(|k| t.contains(k)) { 6 }
|
||||
else if ["latin", "world", "verdens"].iter().any(|k| t.contains(k)) { 8 }
|
||||
else if ["pop", "dansktop", "schlager", "underholdning"].iter().any(|k| t.contains(k)) { 0 }
|
||||
else { continue };
|
||||
return Some(b);
|
||||
}
|
||||
None
|
||||
}
|
||||
const GENRES: usize = 9;
|
||||
|
||||
fn load_affinity(db: &Db, id_of: &HashMap<String, usize>, n: usize) -> Result<(Vec<[f64; GROUPS]>, Vec<[f64; GROUPS]>)> {
|
||||
let mut alltime = vec![[0.0f64; GROUPS]; n];
|
||||
let mut genre: Vec<Option<usize>> = vec![None; n];
|
||||
for r in db.conn.prepare("SELECT artist_id, raw FROM dr_artists")?.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))? {
|
||||
let (id, raw) = r?;
|
||||
let Some(&a) = id_of.get(&id) else { continue };
|
||||
let v: Value = serde_json::from_str(&raw).unwrap_or(Value::Null);
|
||||
genre[a] = v["artist"]["description"].as_str().and_then(genre_bucket);
|
||||
let (mut p4_nat, mut p4_reg, mut n_reg) = (0.0, 0.0, 0.0);
|
||||
for t in v["mostPlayed"].as_array().into_iter().flatten() {
|
||||
for s in t["statisticsByChannels"].as_array().into_iter().flatten() {
|
||||
let cnt = s["count"].as_f64().unwrap_or(0.0);
|
||||
match s["channel"]["slug"].as_str().unwrap_or("") {
|
||||
"p3" => alltime[a][0] += cnt, "p5" => alltime[a][2] += cnt, "p6beat" => alltime[a][3] += cnt, "p8jazz" => alltime[a][4] += cnt,
|
||||
"p4" => p4_nat += cnt,
|
||||
s if s.starts_with("p4") => { p4_reg += cnt; n_reg += 1.0; }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
alltime[a][1] = p4_nat.max(if n_reg > 0.0 { p4_reg / 10.0 } else { 0.0 });
|
||||
}
|
||||
// Genre x station group: share of the genre's plays on each group vs the group's share overall.
|
||||
let mut by = [[0.0f64; GROUPS]; GENRES];
|
||||
let mut all = [0.0f64; GROUPS];
|
||||
for a in 0..n {
|
||||
for g in 0..GROUPS { all[g] += alltime[a][g]; if let Some(b) = genre[a] { by[b][g] += alltime[a][g]; } }
|
||||
}
|
||||
let tot: f64 = all.iter().sum();
|
||||
let mut fit = vec![[0.0f64; GROUPS]; n];
|
||||
for a in 0..n {
|
||||
let Some(b) = genre[a] else { continue };
|
||||
let tb: f64 = by[b].iter().sum();
|
||||
if tb <= 0.0 || tot <= 0.0 { continue; }
|
||||
for g in 0..GROUPS {
|
||||
fit[a][g] = (((by[b][g] + 1.0) / (tb + 5.0)) / ((all[g] + 1.0) / (tot + 5.0))).ln().clamp(-4.0, 2.0);
|
||||
}
|
||||
}
|
||||
Ok((alltime, fit))
|
||||
}
|
||||
|
||||
+7
-1
@@ -326,8 +326,14 @@ fn bestweek(st: &State, q: &HashMap<String, String>) -> Result<Reply> {
|
||||
let nmf = match q.get("nmf").map(String::as_str) { Some("yes") => Some(true), Some("no") => Some(false), _ => None };
|
||||
let p = &m.panel;
|
||||
let t0 = p.w() - 1;
|
||||
// Release weeks between `from` and `to` (default: the next 26), at most 53 weeks, from next week on.
|
||||
let first = p.weeks[t0] + chrono::Duration::weeks(1);
|
||||
let date = |k: &str| q.get(k).and_then(|s| chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d").ok());
|
||||
let wk = |d: chrono::NaiveDate| ((d - first).num_days().div_euclid(7)).max(0) as usize;
|
||||
let h0 = date("from").map(wk).unwrap_or(0);
|
||||
let h1 = date("to").map(wk).unwrap_or(h0 + CALENDAR - 1).max(h0).min(h0 + 52);
|
||||
let groups = [("P3", vec![0usize]), ("P4", (0..C).filter(|&c| crate::model::panel::is_p4(c)).collect()), ("P5", vec![11]), ("P6", vec![12]), ("P8", vec![13])];
|
||||
let weeks: Vec<Value> = (0..CALENDAR).map(|h| {
|
||||
let weeks: Vec<Value> = (h0..=h1).map(|h| {
|
||||
let start = p.weeks[t0] + chrono::Duration::weeks(h as i64 + 1);
|
||||
let xmas = crate::model::features::christmas(p, t0 + h + 1);
|
||||
match new_song(m, a, h, win, nmf) {
|
||||
|
||||
+14
-3
@@ -167,6 +167,8 @@ svg text { fill: var(--muted); font-size: 11px; }
|
||||
<div class="ctl">Station
|
||||
<select id="bw-ch"><option value="any">Any DR station</option><option value="P3">P3</option><option value="P4">P4 (best region)</option><option value="P5">P5</option><option value="P6">P6 Beat</option><option value="P8">P8 Jazz</option></select>
|
||||
</div>
|
||||
<div class="ctl">From <input type="date" id="bw-from"></div>
|
||||
<div class="ctl">To <input type="date" id="bw-to"></div>
|
||||
<div class="ctl">Added within
|
||||
<select id="bw-w"><option value="2">2 weeks</option><option value="4">4 weeks</option><option value="6">6 weeks</option><option value="8" selected>8 weeks</option><option value="10">10 weeks</option><option value="12">12 weeks</option></select>
|
||||
</div>
|
||||
@@ -471,7 +473,16 @@ async function loadBest() {
|
||||
if (!bestId) return;
|
||||
const w = $('bw-w').value, nmf = $('bw-nmf').value, ch = $('bw-ch').value;
|
||||
$('bw-out').innerHTML = '<p class="empty">Loading…</p>';
|
||||
const r = await fetch(`${BASE}/api/bestweek?id=${encodeURIComponent(bestId)}&w=${w}&nmf=${nmf}`);
|
||||
// Dates: from next week, at most 12 months.
|
||||
const next = windowStart(0);
|
||||
if (!$('bw-from').value) $('bw-from').value = next;
|
||||
if ($('bw-from').value < next) $('bw-from').value = next;
|
||||
const maxTo = new Date($('bw-from').value + 'T00:00:00Z'); maxTo.setUTCFullYear(maxTo.getUTCFullYear() + 1);
|
||||
if (!$('bw-to').value) { const d = new Date($('bw-from').value + 'T00:00:00Z'); d.setUTCDate(d.getUTCDate() + 7 * 25); $('bw-to').value = d.toISOString().slice(0, 10); }
|
||||
if ($('bw-to').value > maxTo.toISOString().slice(0, 10)) $('bw-to').value = maxTo.toISOString().slice(0, 10);
|
||||
if ($('bw-to').value < $('bw-from').value) $('bw-to').value = $('bw-from').value;
|
||||
$('bw-from').min = next; $('bw-to').min = $('bw-from').value; $('bw-to').max = maxTo.toISOString().slice(0, 10);
|
||||
const r = await fetch(`${BASE}/api/bestweek?id=${encodeURIComponent(bestId)}&w=${w}&nmf=${nmf}&from=${$('bw-from').value}&to=${$('bw-to').value}`);
|
||||
if (r.status === 503) { $('bw-out').innerHTML = '<p class="empty">The model is still loading on the server; try again in a minute.</p>'; return; }
|
||||
const b = await r.json();
|
||||
if (b.error) { $('bw-out').innerHTML = `<p class="empty">${esc(b.error)}</p>`; return; }
|
||||
@@ -489,10 +500,10 @@ async function loadBest() {
|
||||
<div class="cal">${b.weeks.map(x => { const v = val(x); return `<div class="day ${x.too_soon ? 'soon' : 'lvl' + level(v)} ${x === best && spread >= 0.03 ? 'best' : ''}" title="Release the week of ${fmtDate(x.week)}">
|
||||
<div class="d">Week of ${fmtDate(x.week)}</div><div class="v">${x.too_soon ? 'too soon' : pct(v)}</div>${x.christmas ? '<span class="tag" title="Christmas weeks">🎄</span>' : ''}</div>`; }).join('')}</div>
|
||||
<p class="note">Each tile: if ${esc(b.name)} releases a new song that week, the chance DR puts it into rotation within ${w} weeks. ${b.last_release ? `Their last release: ${fmtDate(b.last_release)}; weeks less than 6 weeks after it are "too soon" (the model only learned from releases with a clear gap).` : ''} The new song's NMF Denmark placement: ${nmf === 'auto' ? `as often as their releases were placed (${pct(b.p_nmf)})` : nmf === 'yes' ? 'placed' : 'not placed'}. What moves the numbers from week to week: time since their last release and last DR add, Christmas, and their NMF history; their current airplay is taken as it is now.</p>`
|
||||
: `<h2>${esc(b.name)}</h2><p class="empty">No week in the next 26 has an estimate.</p>`;
|
||||
: `<h2>${esc(b.name)}</h2><p class="empty">No week in this period has an estimate.</p>`;
|
||||
}
|
||||
$('bw-q').addEventListener('change', () => { const id = artistByName.get($('bw-q').value); if (id) { bestId = id; loadBest(); } });
|
||||
['bw-w', 'bw-nmf', 'bw-ch'].forEach(id => $(id).addEventListener('change', loadBest));
|
||||
['bw-w', 'bw-nmf', 'bw-ch', 'bw-from', 'bw-to'].forEach(id => $(id).addEventListener('change', loadBest));
|
||||
$('q').addEventListener('input', () => { shown = 100; render(); });
|
||||
$('more').addEventListener('click', () => { shown += 100; render(); });
|
||||
$('rows').addEventListener('click', e => { const tr = e.target.closest('tr[data-id]'); if (tr) showArtist(tr.dataset.id); });
|
||||
|
||||
Reference in New Issue
Block a user