NMF Denmark in the add model (placed in the window, top 10, history), scenario type (release / NMF) for the ranking and the what-if; NMF Denmark export

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 11:43:52 +00:00
co-authored by Claude Opus 5.5
parent e6b080246b
commit e94601b997
6 changed files with 268 additions and 151 deletions
+9 -4
View File
@@ -187,6 +187,8 @@ fn fold(e: &mut Value, target: &str, ch: &str, prob: Option<f64>, mean: Option<f
"add_any" => { e["add_any"] = r3(prob); }
"add" => { e["add"][ch] = r3(prob); }
"add_any_rel" => { e["add_any_rel"] = r3(prob); }
"add_any_nmf" => { e["add_any_nmf"] = r3(prob); }
"p_nmf" => { e["p_nmf"] = r3(prob); }
"add_rel" => { if !e["add_rel"].is_object() { e["add_rel"] = json!({}); } e["add_rel"][ch] = r3(prob); }
"p_release" => { e["p_release"] = r3(prob); }
"now" => { e["now"][ch] = json!({"on": prob.unwrap_or(0.0) > 0.5, "plays": mean}); }
@@ -234,7 +236,7 @@ fn rank(st: &State, h: usize) -> Result<Reply> {
}
let mut rows: HashMap<String, Value> = HashMap::new();
let mut st_ = c.prepare("SELECT key, target, prob, mean, lo, hi, target_week FROM forecasts WHERE made_at = ?1 AND model_version = ?2
AND ((target IN ('rotation', 'any') AND horizon = ?3) OR (target IN ('add', 'add_any', 'add_rel', 'add_any_rel', 'p_release') AND horizon = ?4) OR target = 'now')")?;
AND ((target IN ('rotation', 'any') AND horizon = ?3) OR (target IN ('add', 'add_any', 'add_rel', 'add_any_rel', 'add_any_nmf', 'p_nmf', 'p_release') AND horizon = ?4) OR target = 'now')")?;
let it = st_.query_map(params![made, VERSION, h.max(1) as i64, h as i64], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, Option<f64>>(2)?,
r.get::<_, Option<f64>>(3)?, r.get::<_, Option<f64>>(4)?, r.get::<_, Option<f64>>(5)?, r.get::<_, String>(6)?)))?;
let mut window = String::new();
@@ -299,7 +301,7 @@ fn artist(st: &State, id: &str) -> Result<Reply> {
let ch = key.split_once(':').map(|x| x.1).unwrap_or("");
if target == "now" { fold(&mut now, "now", ch, prob, mean, lo, hi); continue; }
let e = byh.entry(h).or_insert_with(|| json!({"h": h, "p": {}, "plays": {}, "add": {}}));
if target.starts_with("add") || target == "p_release" { e["window_start"] = json!(week); } else { e["week"] = json!(week); }
if target.starts_with("add") || target.starts_with("p_") { e["window_start"] = json!(week); } else { e["week"] = json!(week); }
fold(e, &target, ch, prob, mean, lo, hi);
}
now_out = now["now"].clone();
@@ -357,11 +359,13 @@ fn whatif(st: &State, q: &HashMap<String, String>) -> Result<Reply> {
let (t40, air) = (num("t40"), num("air"));
// The scenario: a release in the window (default) or none.
let release = q.get("release").map(|v| v != "no").unwrap_or(true);
// ...and whether that release is placed on New Music Friday Denmark (default: no).
let nmf = q.get("nmf").is_some_and(|v| v == "yes");
let over = move |x: &mut [f64], idx: [usize; 4]| set_chart(x, idx, entry, t40, air);
let run = |overrides: bool| -> Value {
let f: Option<&dyn Fn(&mut [f64], [usize; 4])> = if overrides { Some(&over) } else { None };
let (add_c, add_any) = am.predict(p, &m.rot, a, t0, Some(release), f);
let (add_c, add_any) = am.predict(p, &m.rot, a, t0, crate::model::adds::Scenario::new(release, release && nmf), f);
let mut x = vec![0.0; P];
let mut mu = [0.0; C];
for c in 0..C {
@@ -383,7 +387,8 @@ fn whatif(st: &State, q: &HashMap<String, String>) -> Result<Reply> {
};
let now: serde_json::Map<String, Value> = (0..C).filter(|&c| m.rot.on(a, c, t0)).map(|c| (CHANNELS[c].to_string(), json!(p.y(a, c, t0)))).collect();
ok(json!({
"h": h, "release": release, "p_release": crate::model::adds::p_release(p, a, t0),
"h": h, "release": release, "nmf": nmf, "p_release": crate::model::adds::p_release(p, a, t0), "p_nmf": crate::model::adds::p_nmf(p, a, t0),
"nmf_history": p.nmf_dk[a].iter().rev().take(8).map(|(d, pos)| json!({"friday": d.to_string(), "position": pos})).collect::<Vec<_>>(),
"window_start": (p.weeks[t0] + chrono::Duration::weeks(h as i64 + 1)).to_string(),
"current": {"t40": chart.t40_rank, "air": chart.air_rank,
"entry": if chart.weeks_since_entry == u16::MAX { Value::Null } else { json!(chart.weeks_since_entry) },