| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
use std::collections::{BTreeMap, BTreeSet, HashMap}; |
| 65 |
use std::path::Path; |
| 66 |
|
| 67 |
use audiofiles_core::analysis::config::AnalysisConfig; |
| 68 |
use audiofiles_core::analysis::exemplar::{self, DEFAULT_K, DEFAULT_REVIEW_THRESHOLD}; |
| 69 |
use audiofiles_core::analysis::features::FEATURE_VERSION; |
| 70 |
use audiofiles_core::rules::{RuleContext, RuleField}; |
| 71 |
use audiofiles_core::{rules, starter_rules}; |
| 72 |
|
| 73 |
use crate::families::{self, LabelSpace}; |
| 74 |
use crate::labelled; |
| 75 |
use crate::report::Report; |
| 76 |
use crate::rows::{self, Row}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
const BAR: f64 = 0.02; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#[derive(Clone, PartialEq, Eq)] |
| 97 |
enum Answer { |
| 98 |
Silent, |
| 99 |
Tag(String), |
| 100 |
} |
| 101 |
|
| 102 |
impl Answer { |
| 103 |
fn of(index: &exemplar::ExemplarIndex, vector: &[f64], k: usize) -> Self { |
| 104 |
index |
| 105 |
.score(vector, k, None) |
| 106 |
.first() |
| 107 |
.filter(|s| s.score >= DEFAULT_REVIEW_THRESHOLD) |
| 108 |
.map_or(Self::Silent, |s| Self::Tag(s.tag.clone())) |
| 109 |
} |
| 110 |
|
| 111 |
fn tag(&self) -> Option<&str> { |
| 112 |
match self { |
| 113 |
Self::Silent => None, |
| 114 |
Self::Tag(t) => Some(t), |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
type Answers = Vec<Answer>; |
| 121 |
|
| 122 |
|
| 123 |
struct Churn { |
| 124 |
|
| 125 |
both: usize, |
| 126 |
|
| 127 |
flips: usize, |
| 128 |
|
| 129 |
appeared: usize, |
| 130 |
|
| 131 |
vanished: usize, |
| 132 |
} |
| 133 |
|
| 134 |
impl Churn { |
| 135 |
fn between(before: &Answers, after: &Answers) -> Self { |
| 136 |
let mut churn = Self { |
| 137 |
both: 0, |
| 138 |
flips: 0, |
| 139 |
appeared: 0, |
| 140 |
vanished: 0, |
| 141 |
}; |
| 142 |
for (x, y) in before.iter().zip(after) { |
| 143 |
match (x.tag(), y.tag()) { |
| 144 |
(Some(was), Some(now)) => { |
| 145 |
churn.both += 1; |
| 146 |
if was != now { |
| 147 |
churn.flips += 1; |
| 148 |
} |
| 149 |
} |
| 150 |
(None, Some(_)) => churn.appeared += 1, |
| 151 |
(Some(_), None) => churn.vanished += 1, |
| 152 |
(None, None) => {} |
| 153 |
} |
| 154 |
} |
| 155 |
churn |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
fn flip_rate(&self) -> Option<f64> { |
| 161 |
(self.both > 0).then(|| self.flips as f64 / self.both as f64) |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
fn comparable_flip_rate(&self) -> Option<f64> { |
| 167 |
self.flip_rate().filter(|_| self.both >= MIN_COMPARABLE) |
| 168 |
} |
| 169 |
|
| 170 |
fn passes(&self) -> bool { |
| 171 |
self.flip_rate().is_some_and(|r| r <= BAR) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
const MIN_COMPARABLE: usize = 30; |
| 184 |
|
| 185 |
fn pct(v: Option<f64>) -> String { |
| 186 |
v.map_or_else(|| "-".to_string(), |x| format!("{:.1}%", x * 100.0)) |
| 187 |
} |
| 188 |
|
| 189 |
fn round4(v: f64) -> f64 { |
| 190 |
(v * 10_000.0).round() / 10_000.0 |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
fn answers(index: &exemplar::ExemplarIndex, probe: &[&Row], k: usize) -> Answers { |
| 195 |
probe |
| 196 |
.iter() |
| 197 |
.map(|r| Answer::of(index, &r.vector, k)) |
| 198 |
.collect() |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
fn run_variant(local: &[&Row], imported: &[&Row], probe: &[&Row], k: usize, what: &str) -> Answers { |
| 204 |
let db = match rows::mixed_db(local, imported) { |
| 205 |
Ok(db) => db, |
| 206 |
Err(e) => { |
| 207 |
eprintln!("{what}: {e}"); |
| 208 |
std::process::exit(1); |
| 209 |
} |
| 210 |
}; |
| 211 |
let index = match exemplar::build_index(&db) { |
| 212 |
Ok(i) => i, |
| 213 |
Err(e) => { |
| 214 |
eprintln!("{what}: build_index: {e}"); |
| 215 |
std::process::exit(1); |
| 216 |
} |
| 217 |
}; |
| 218 |
answers(&index, probe, k) |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
fn take_class<'a>(pool: &[&'a Row], class: &str, n: usize) -> Vec<&'a Row> { |
| 229 |
pool.iter() |
| 230 |
.filter(|r| r.truth.as_deref() == Some(class)) |
| 231 |
.take(n) |
| 232 |
.copied() |
| 233 |
.collect() |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
fn halve<'a>(pool: &[&'a Row]) -> (Vec<&'a Row>, Vec<&'a Row>) { |
| 239 |
let mut seen: HashMap<&str, usize> = HashMap::new(); |
| 240 |
let mut a = Vec::new(); |
| 241 |
let mut b = Vec::new(); |
| 242 |
for r in pool { |
| 243 |
let key = r.truth.as_deref().unwrap_or(""); |
| 244 |
let n = seen.entry(key).or_default(); |
| 245 |
if (*n).is_multiple_of(2) { |
| 246 |
a.push(*r); |
| 247 |
} else { |
| 248 |
b.push(*r); |
| 249 |
} |
| 250 |
*n += 1; |
| 251 |
} |
| 252 |
(a, b) |
| 253 |
} |
| 254 |
|
| 255 |
pub(crate) fn run( |
| 256 |
corpus: &Path, |
| 257 |
vault: &Path, |
| 258 |
config: &AnalysisConfig, |
| 259 |
k: usize, |
| 260 |
probe_denominator: usize, |
| 261 |
space: LabelSpace, |
| 262 |
) { |
| 263 |
println!("━━━ CLASSIFIER LAYER STABILITY ━━━"); |
| 264 |
println!(); |
| 265 |
println!(" corpus {}", corpus.display()); |
| 266 |
println!(" scratch {}", vault.display()); |
| 267 |
println!(" features v{FEATURE_VERSION}"); |
| 268 |
println!(" k {k}"); |
| 269 |
println!(" labels {}", space.describe()); |
| 270 |
println!( |
| 271 |
" answer top-scoring tag at score >= {DEFAULT_REVIEW_THRESHOLD} (the review\n \ |
| 272 |
threshold), else silent. Under suggest-only that line is what decides\n \ |
| 273 |
whether the user ever sees the answer." |
| 274 |
); |
| 275 |
println!(); |
| 276 |
println!( |
| 277 |
" Bar: per-run family-label flip rate <= {:.0}% on the fixed probe set, at", |
| 278 |
BAR * 100.0 |
| 279 |
); |
| 280 |
println!(" the deployment weight, counting only flips between two suggested answers."); |
| 281 |
println!(" Silent <-> suggested is reported as churn and is not a flip: the queue got"); |
| 282 |
println!(" longer or shorter, it did not contradict itself."); |
| 283 |
println!(); |
| 284 |
println!(" Flip rate and error rate are independent. A consistently wrong answer is"); |
| 285 |
println!(" perfectly stable, so nothing here can be read off the accuracy figures and"); |
| 286 |
println!(" nothing here substitutes for them."); |
| 287 |
println!(); |
| 288 |
|
| 289 |
let built = match labelled::build_vault(corpus, vault, config) { |
| 290 |
Ok(v) => v, |
| 291 |
Err(e) => { |
| 292 |
eprintln!("corpus: {e}"); |
| 293 |
std::process::exit(1); |
| 294 |
} |
| 295 |
}; |
| 296 |
let (all, dropped) = match rows::load_rows(&built.db, space) { |
| 297 |
Ok(r) => r, |
| 298 |
Err(e) => { |
| 299 |
eprintln!("reading the vault back: {e}"); |
| 300 |
std::process::exit(1); |
| 301 |
} |
| 302 |
}; |
| 303 |
if dropped.unprojectable > 0 { |
| 304 |
println!( |
| 305 |
" {} row(s) carry no label in this space ({}) and are excluded from", |
| 306 |
dropped.unprojectable, |
| 307 |
dropped |
| 308 |
.origins |
| 309 |
.iter() |
| 310 |
.cloned() |
| 311 |
.collect::<Vec<_>>() |
| 312 |
.join(", ") |
| 313 |
); |
| 314 |
println!(" every index and every probe:"); |
| 315 |
println!("{}", families::DROPPED_NOTE); |
| 316 |
println!(); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
let fold_of = rows::assign_folds(&all, probe_denominator); |
| 324 |
let probe: Vec<&Row> = all |
| 325 |
.iter() |
| 326 |
.zip(&fold_of) |
| 327 |
.filter(|(_, f)| **f == Some(0)) |
| 328 |
.map(|(r, _)| r) |
| 329 |
.collect(); |
| 330 |
let pool: Vec<&Row> = all |
| 331 |
.iter() |
| 332 |
.zip(&fold_of) |
| 333 |
.filter(|(_, f)| **f != Some(0)) |
| 334 |
.map(|(r, _)| r) |
| 335 |
.collect(); |
| 336 |
|
| 337 |
let classes: Vec<String> = all |
| 338 |
.iter() |
| 339 |
.filter_map(|r| r.truth.clone()) |
| 340 |
.collect::<BTreeSet<_>>() |
| 341 |
.into_iter() |
| 342 |
.collect(); |
| 343 |
|
| 344 |
println!( |
| 345 |
" {} row(s) scoreable: {} held out as the fixed probe, {} in the pool every", |
| 346 |
all.len(), |
| 347 |
probe.len(), |
| 348 |
pool.len() |
| 349 |
); |
| 350 |
println!(" index is drawn from. No probe sample is ever an exemplar."); |
| 351 |
for c in &classes { |
| 352 |
let in_probe = probe |
| 353 |
.iter() |
| 354 |
.filter(|r| r.truth.as_deref() == Some(c.as_str())) |
| 355 |
.count(); |
| 356 |
let in_pool = pool |
| 357 |
.iter() |
| 358 |
.filter(|r| r.truth.as_deref() == Some(c.as_str())) |
| 359 |
.count(); |
| 360 |
println!( |
| 361 |
" {:<14} probe {:>4} pool {:>4}", |
| 362 |
families::label_for(space, c), |
| 363 |
in_probe, |
| 364 |
in_pool |
| 365 |
); |
| 366 |
} |
| 367 |
println!(); |
| 368 |
|
| 369 |
if probe.is_empty() || pool.is_empty() { |
| 370 |
eprintln!("nothing to measure: probe or pool is empty"); |
| 371 |
std::process::exit(1); |
| 372 |
} |
| 373 |
|
| 374 |
let mut report = Report::new("layer-stability"); |
| 375 |
report.set("label_space", format!("{space:?}")); |
| 376 |
report.set("k", k); |
| 377 |
report.set("feat_version", FEATURE_VERSION); |
| 378 |
report.set("bar_flip_rate", BAR); |
| 379 |
report.set("review_threshold", DEFAULT_REVIEW_THRESHOLD); |
| 380 |
report.set("import_weight", rows::IMPORT_WEIGHT); |
| 381 |
report.set("probe", probe.len()); |
| 382 |
report.set("pool", pool.len()); |
| 383 |
|
| 384 |
review_threshold_note(&pool, &probe, &classes, k, space, &mut report); |
| 385 |
let miss = value_add_population(&probe, &mut report); |
| 386 |
let m1 = composition(&pool, &probe, &classes, k, space, &mut report); |
| 387 |
let m2 = size(&pool, &probe, &classes, k, &mut report); |
| 388 |
let m3 = deployment_shape( |
| 389 |
&pool, |
| 390 |
&probe, |
| 391 |
k, |
| 392 |
space, |
| 393 |
"3. DEPLOYMENT SHAPE", |
| 394 |
"deploy", |
| 395 |
&mut report, |
| 396 |
); |
| 397 |
let m3b = value_add_deployment(&pool, &miss, k, space, &mut report); |
| 398 |
let m4 = feedback(&pool, k, space, &mut report); |
| 399 |
|
| 400 |
let mut outcomes = vec![m1, m2, m3]; |
| 401 |
outcomes.extend(m3b); |
| 402 |
outcomes.push(m4); |
| 403 |
verdict(&outcomes, space, &classes, &mut report); |
| 404 |
report.write(); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
fn review_threshold_note( |
| 428 |
pool: &[&Row], |
| 429 |
probe: &[&Row], |
| 430 |
classes: &[String], |
| 431 |
k: usize, |
| 432 |
space: LabelSpace, |
| 433 |
report: &mut Report, |
| 434 |
) { |
| 435 |
println!("━━━ IS THE REVIEW THRESHOLD BINDING? ━━━"); |
| 436 |
println!(); |
| 437 |
|
| 438 |
let full = run_variant(pool, &[], probe, k, "full pool"); |
| 439 |
let silent = full.iter().filter(|a| a.tag().is_none()).count(); |
| 440 |
println!( |
| 441 |
" Against the full pool, {} of {} probe samples fall below the {} review", |
| 442 |
silent, |
| 443 |
probe.len(), |
| 444 |
DEFAULT_REVIEW_THRESHOLD |
| 445 |
); |
| 446 |
println!(" threshold and stay out of the queue."); |
| 447 |
println!(); |
| 448 |
|
| 449 |
report.set("silent_at_full_pool", silent); |
| 450 |
report.set("classes", classes.len()); |
| 451 |
|
| 452 |
if silent == 0 && classes.len() == 2 { |
| 453 |
println!(" Zero, and it is arithmetic rather than luck. A score is a tag's share of"); |
| 454 |
println!(" the neighbourhood's kernel weight; with every exemplar carrying exactly one"); |
| 455 |
println!(" of two tags the two shares sum to 1, so the larger is always at or above"); |
| 456 |
println!(" 0.5. On this corpus `silent` is unreachable and the threshold decides"); |
| 457 |
println!(" nothing."); |
| 458 |
println!(); |
| 459 |
println!(" What that costs the measurements below, stated plainly:"); |
| 460 |
println!(); |
| 461 |
println!(" - The queue can only change its CONTENTS, never its LENGTH. The churn"); |
| 462 |
println!(" columns are structurally zero and prove nothing."); |
| 463 |
println!(" - 'The layer answers where the user's labels do not' is identically zero"); |
| 464 |
println!(" for every layer, good or useless. Measurement 3 therefore reads the"); |
| 465 |
println!(" layer's contribution off the answer and its correctness instead."); |
| 466 |
println!(); |
| 467 |
println!(" Both resolve at three classes or more, so this is a limit of the drums-"); |
| 468 |
println!(" only corpus and not a property of the layer. Phase C is where it lifts."); |
| 469 |
report.set("review_threshold_binding", false); |
| 470 |
} else { |
| 471 |
report.set("review_threshold_binding", true); |
| 472 |
} |
| 473 |
println!(); |
| 474 |
let _ = space; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
struct Outcome { |
| 479 |
name: &'static str, |
| 480 |
|
| 481 |
worst: Option<f64>, |
| 482 |
note: String, |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
fn value_add_population<'a>(probe: &[&'a Row], report: &mut Report) -> Vec<&'a Row> { |
| 503 |
println!("━━━ THE VALUE-ADD POPULATION ━━━"); |
| 504 |
println!(); |
| 505 |
|
| 506 |
let Some(name_rules) = filename_rules() else { |
| 507 |
println!(" could not seed the starter rules; skipped"); |
| 508 |
println!(); |
| 509 |
return Vec::new(); |
| 510 |
}; |
| 511 |
|
| 512 |
let mut hit = 0usize; |
| 513 |
let mut miss: Vec<&Row> = Vec::new(); |
| 514 |
for r in probe { |
| 515 |
let ctx = RuleContext { |
| 516 |
name: r.name.clone(), |
| 517 |
..RuleContext::default() |
| 518 |
}; |
| 519 |
if name_rules |
| 520 |
.iter() |
| 521 |
.any(|rule| rules::rule_matches(rule, &ctx)) |
| 522 |
{ |
| 523 |
hit += 1; |
| 524 |
} else { |
| 525 |
miss.push(r); |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
let rate = hit as f64 / probe.len() as f64; |
| 530 |
println!( |
| 531 |
" {} of {} probe samples ({}) already carry a filename a starter rule fires on.", |
| 532 |
hit, |
| 533 |
probe.len(), |
| 534 |
pct(Some(rate)) |
| 535 |
); |
| 536 |
println!( |
| 537 |
" The layer's job is the other {}: libraries whose filenames say nothing.", |
| 538 |
miss.len() |
| 539 |
); |
| 540 |
println!(" This counts only rules reading the filename, and only whether one FIRES —"); |
| 541 |
println!(" not whether it fires correctly. It is a different quantity from the 97.7%"); |
| 542 |
println!(" in `af-browse-axes`, which is an accuracy over the whole corpus, so the two"); |
| 543 |
println!(" are not comparable and the gap between them is not a regression."); |
| 544 |
println!(); |
| 545 |
if miss.len() < MIN_COMPARABLE { |
| 546 |
println!(" Too thin to carry a flip rate, and thin by construction: this corpus's"); |
| 547 |
println!(" folder labels were derived from these same filenames, so a rule-miss here"); |
| 548 |
println!(" is close to a corpus artifact. Every number below is therefore measured on"); |
| 549 |
println!(" the population the rules already answer, which is the easy half."); |
| 550 |
println!(" Phase C (24cd7747) is where this lifts."); |
| 551 |
println!(); |
| 552 |
report.set("rule_hit", hit); |
| 553 |
report.set("rule_miss", miss.len()); |
| 554 |
report.set("rule_hit_rate", round4(rate)); |
| 555 |
return Vec::new(); |
| 556 |
} |
| 557 |
println!(" Large enough to carry a rate. Measurement 3 is re-run on it below, because"); |
| 558 |
println!(" a layer that is stable and useful only where the filename already said the"); |
| 559 |
println!(" answer is stable and useful nowhere that matters."); |
| 560 |
println!(); |
| 561 |
|
| 562 |
report.set("rule_hit", hit); |
| 563 |
report.set("rule_miss", miss.len()); |
| 564 |
report.set("rule_hit_rate", round4(rate)); |
| 565 |
miss |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
fn filename_rules() -> Option<Vec<rules::Rule>> { |
| 577 |
let db = audiofiles_core::db::Database::open_in_memory().ok()?; |
| 578 |
starter_rules::seed(&db).ok()?; |
| 579 |
let all = rules::list_rules(&db).ok()?; |
| 580 |
Some( |
| 581 |
all.into_iter() |
| 582 |
.filter(|r| { |
| 583 |
!r.conditions.is_empty() && r.conditions.iter().all(|c| c.field == RuleField::Name) |
| 584 |
}) |
| 585 |
.collect(), |
| 586 |
) |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
fn composition( |
| 605 |
pool: &[&Row], |
| 606 |
probe: &[&Row], |
| 607 |
classes: &[String], |
| 608 |
k: usize, |
| 609 |
space: LabelSpace, |
| 610 |
report: &mut Report, |
| 611 |
) -> Outcome { |
| 612 |
println!("━━━ 1. COMPOSITION: same size, different mix ━━━"); |
| 613 |
println!(); |
| 614 |
|
| 615 |
let per_class: Vec<usize> = classes |
| 616 |
.iter() |
| 617 |
.map(|c| { |
| 618 |
pool.iter() |
| 619 |
.filter(|r| r.truth.as_deref() == Some(c.as_str())) |
| 620 |
.count() |
| 621 |
}) |
| 622 |
.collect(); |
| 623 |
let smallest = per_class.iter().copied().min().unwrap_or(0); |
| 624 |
if smallest < 20 || classes.len() < 2 { |
| 625 |
println!(" the pool cannot supply two differently-shaped indexes of a common size"); |
| 626 |
println!(); |
| 627 |
return Outcome { |
| 628 |
name: "composition", |
| 629 |
worst: None, |
| 630 |
note: "not runnable on this corpus".into(), |
| 631 |
}; |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
let minor = smallest / 4; |
| 637 |
let major = smallest; |
| 638 |
let total = major + minor; |
| 639 |
|
| 640 |
let mut variants: Vec<(String, Vec<&Row>)> = Vec::new(); |
| 641 |
let balanced_each = total / classes.len(); |
| 642 |
variants.push(( |
| 643 |
"balanced".into(), |
| 644 |
classes |
| 645 |
.iter() |
| 646 |
.flat_map(|c| take_class(pool, c, balanced_each)) |
| 647 |
.collect(), |
| 648 |
)); |
| 649 |
for heavy in classes { |
| 650 |
let mut v = Vec::new(); |
| 651 |
for c in classes { |
| 652 |
let n = if c == heavy { |
| 653 |
major |
| 654 |
} else { |
| 655 |
minor / (classes.len() - 1).max(1) |
| 656 |
}; |
| 657 |
v.extend(take_class(pool, c, n)); |
| 658 |
} |
| 659 |
variants.push((format!("{}-heavy", families::label_for(space, heavy)), v)); |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
for (label, origin) in origin_variants(pool, classes) { |
| 664 |
let mut v = Vec::new(); |
| 665 |
for c in classes { |
| 666 |
let want = if Some(c.as_str()) == origin.class.as_deref() { |
| 667 |
pool.iter() |
| 668 |
.filter(|r| r.truth.as_deref() == Some(c.as_str()) && r.origin == origin.folder) |
| 669 |
.take(balanced_each) |
| 670 |
.copied() |
| 671 |
.collect() |
| 672 |
} else { |
| 673 |
take_class(pool, c, balanced_each) |
| 674 |
}; |
| 675 |
v.extend(want); |
| 676 |
} |
| 677 |
variants.push((label, v)); |
| 678 |
} |
| 679 |
|
| 680 |
println!(" Each index holds one class mix at a common size; the probe set never moves."); |
| 681 |
println!(); |
| 682 |
println!(" {:<22} {:>8} class mix", "variant", "n"); |
| 683 |
println!(" {}", "─".repeat(72)); |
| 684 |
let mut computed: Vec<(String, Answers)> = Vec::new(); |
| 685 |
for (name, exemplars) in &variants { |
| 686 |
let mix: Vec<String> = classes |
| 687 |
.iter() |
| 688 |
.map(|c| { |
| 689 |
let n = exemplars |
| 690 |
.iter() |
| 691 |
.filter(|r| r.truth.as_deref() == Some(c.as_str())) |
| 692 |
.count(); |
| 693 |
format!("{} {}", families::label_for(space, c), n) |
| 694 |
}) |
| 695 |
.collect(); |
| 696 |
println!(" {:<22} {:>8} {}", name, exemplars.len(), mix.join(", ")); |
| 697 |
let a = run_variant(exemplars, &[], probe, k, name); |
| 698 |
computed.push((name.clone(), a)); |
| 699 |
} |
| 700 |
println!(); |
| 701 |
|
| 702 |
let worst = print_pairwise(&computed, report, "composition"); |
| 703 |
Outcome { |
| 704 |
name: "composition", |
| 705 |
worst, |
| 706 |
note: format!("{} variants", computed.len()), |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
struct OriginVariant { |
| 712 |
class: Option<String>, |
| 713 |
folder: String, |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
fn origin_variants(pool: &[&Row], classes: &[String]) -> Vec<(String, OriginVariant)> { |
| 720 |
let mut out = Vec::new(); |
| 721 |
for c in classes { |
| 722 |
let mut folders: BTreeMap<&str, usize> = BTreeMap::new(); |
| 723 |
for r in pool |
| 724 |
.iter() |
| 725 |
.filter(|r| r.truth.as_deref() == Some(c.as_str())) |
| 726 |
{ |
| 727 |
*folders.entry(r.origin.as_str()).or_default() += 1; |
| 728 |
} |
| 729 |
if folders.len() < 2 { |
| 730 |
continue; |
| 731 |
} |
| 732 |
for (folder, n) in folders { |
| 733 |
if n < 20 { |
| 734 |
continue; |
| 735 |
} |
| 736 |
out.push(( |
| 737 |
format!("{folder}-only"), |
| 738 |
OriginVariant { |
| 739 |
class: Some(c.clone()), |
| 740 |
folder: folder.to_string(), |
| 741 |
}, |
| 742 |
)); |
| 743 |
} |
| 744 |
} |
| 745 |
out |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
fn print_pairwise(computed: &[(String, Answers)], report: &mut Report, key: &str) -> Option<f64> { |
| 750 |
println!(" Pairwise flip rate (both suggested, tag changed):"); |
| 751 |
println!(); |
| 752 |
println!( |
| 753 |
" {:<22} {:<22} {:>7} {:>8} {:>9} {:>9}", |
| 754 |
"a", "b", "both", "flips", "rate", "churn" |
| 755 |
); |
| 756 |
println!(" {}", "─".repeat(84)); |
| 757 |
|
| 758 |
let mut worst: Option<f64> = None; |
| 759 |
for i in 0..computed.len() { |
| 760 |
for j in (i + 1)..computed.len() { |
| 761 |
let c = Churn::between(&computed[i].1, &computed[j].1); |
| 762 |
let rate = c.flip_rate(); |
| 763 |
if let Some(r) = c.comparable_flip_rate() { |
| 764 |
worst = Some(worst.map_or(r, |w: f64| w.max(r))); |
| 765 |
} |
| 766 |
println!( |
| 767 |
" {:<22} {:<22} {:>7} {:>8} {:>9} {:>9}", |
| 768 |
computed[i].0, |
| 769 |
computed[j].0, |
| 770 |
c.both, |
| 771 |
c.flips, |
| 772 |
pct(rate), |
| 773 |
format!("+{} -{}", c.appeared, c.vanished) |
| 774 |
); |
| 775 |
} |
| 776 |
} |
| 777 |
println!(); |
| 778 |
match worst { |
| 779 |
Some(w) => { |
| 780 |
println!( |
| 781 |
" worst pairwise flip rate {} ({} the {:.0}% bar)", |
| 782 |
pct(Some(w)), |
| 783 |
if w <= BAR { "within" } else { "OVER" }, |
| 784 |
BAR * 100.0 |
| 785 |
); |
| 786 |
report.set(&format!("{key}_worst_flip_rate"), round4(w)); |
| 787 |
} |
| 788 |
None => println!(" no pair had a suggested answer in common; nothing to compare"), |
| 789 |
} |
| 790 |
println!(); |
| 791 |
worst |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
fn size( |
| 804 |
pool: &[&Row], |
| 805 |
probe: &[&Row], |
| 806 |
classes: &[String], |
| 807 |
k: usize, |
| 808 |
report: &mut Report, |
| 809 |
) -> Outcome { |
| 810 |
println!("━━━ 2. SIZE: same mix, growing index ━━━"); |
| 811 |
println!(); |
| 812 |
println!(" Each index is a prefix of the next: a library accretes, it does not"); |
| 813 |
println!(" resample. Read the consecutive column — the pairwise-with-full column is"); |
| 814 |
println!(" the same information seen from the end state."); |
| 815 |
println!(); |
| 816 |
|
| 817 |
const FRACTIONS: &[f64] = &[0.1, 0.2, 0.4, 0.6, 0.8, 1.0]; |
| 818 |
let mut computed: Vec<(String, Answers, usize)> = Vec::new(); |
| 819 |
for f in FRACTIONS { |
| 820 |
let exemplars: Vec<&Row> = classes |
| 821 |
.iter() |
| 822 |
.flat_map(|c| { |
| 823 |
let have = pool |
| 824 |
.iter() |
| 825 |
.filter(|r| r.truth.as_deref() == Some(c.as_str())) |
| 826 |
.count(); |
| 827 |
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] |
| 828 |
take_class(pool, c, (have as f64 * f).round() as usize) |
| 829 |
}) |
| 830 |
.collect(); |
| 831 |
if exemplars.is_empty() { |
| 832 |
continue; |
| 833 |
} |
| 834 |
let label = format!("{:.0}%", f * 100.0); |
| 835 |
let a = run_variant(&exemplars, &[], probe, k, &label); |
| 836 |
computed.push((label, a, exemplars.len())); |
| 837 |
} |
| 838 |
if computed.len() < 2 { |
| 839 |
println!(" the pool is too small to sweep"); |
| 840 |
println!(); |
| 841 |
return Outcome { |
| 842 |
name: "size", |
| 843 |
worst: None, |
| 844 |
note: "not runnable on this corpus".into(), |
| 845 |
}; |
| 846 |
} |
| 847 |
|
| 848 |
println!( |
| 849 |
" {:<8} {:>8} {:>8} {:>8} {:>10} {:>12}", |
| 850 |
"index", "n", "queued", "flips", "vs prev", "vs full" |
| 851 |
); |
| 852 |
println!(" {}", "─".repeat(60)); |
| 853 |
|
| 854 |
let full = &computed[computed.len() - 1].1; |
| 855 |
let mut worst_consecutive: Option<f64> = None; |
| 856 |
for (i, (label, a, n)) in computed.iter().enumerate() { |
| 857 |
let queued = a.iter().filter(|x| x.tag().is_some()).count(); |
| 858 |
let prev = (i > 0).then(|| Churn::between(&computed[i - 1].1, a)); |
| 859 |
let vs_full = Churn::between(a, full); |
| 860 |
if let Some(r) = prev.as_ref().and_then(Churn::comparable_flip_rate) { |
| 861 |
worst_consecutive = Some(worst_consecutive.map_or(r, |w: f64| w.max(r))); |
| 862 |
} |
| 863 |
println!( |
| 864 |
" {:<8} {:>8} {:>8} {:>8} {:>10} {:>12}", |
| 865 |
label, |
| 866 |
n, |
| 867 |
queued, |
| 868 |
prev.as_ref().map_or(0, |c| c.flips), |
| 869 |
prev.as_ref() |
| 870 |
.map_or_else(|| "-".to_string(), |c| pct(c.flip_rate())), |
| 871 |
pct(vs_full.flip_rate()), |
| 872 |
); |
| 873 |
report.set( |
| 874 |
&format!("size_{}_queued", label.trim_end_matches('%')), |
| 875 |
queued, |
| 876 |
); |
| 877 |
} |
| 878 |
println!(); |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
let settles = (0..computed.len() - 1).find(|&i| { |
| 884 |
(i..computed.len() - 1).all(|j| Churn::between(&computed[j].1, &computed[j + 1].1).passes()) |
| 885 |
}); |
| 886 |
let settle_note = settles.map_or_else( |
| 887 |
|| "never settles".to_string(), |
| 888 |
|i| format!("settles at {} exemplars", computed[i].2), |
| 889 |
); |
| 890 |
match settles { |
| 891 |
Some(i) => { |
| 892 |
println!( |
| 893 |
" Settles at {} ({} exemplars): every step from there stays within the bar.", |
| 894 |
computed[i].0, computed[i].2 |
| 895 |
); |
| 896 |
report.set("size_settles_at", computed[i].2); |
| 897 |
} |
| 898 |
None => { |
| 899 |
println!(" Never settles: no index size after which every step stays within the"); |
| 900 |
println!(" bar. On this pool that is a statement about the pool as much as the"); |
| 901 |
println!(" layer — the largest index here is still small."); |
| 902 |
} |
| 903 |
} |
| 904 |
println!(); |
| 905 |
if let Some(w) = worst_consecutive { |
| 906 |
report.set("size_worst_consecutive_flip_rate", round4(w)); |
| 907 |
} |
| 908 |
|
| 909 |
Outcome { |
| 910 |
name: "size", |
| 911 |
worst: worst_consecutive, |
| 912 |
note: format!("{} steps, {settle_note}", computed.len()), |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
fn deployment_shape( |
| 938 |
pool: &[&Row], |
| 939 |
probe: &[&Row], |
| 940 |
k: usize, |
| 941 |
space: LabelSpace, |
| 942 |
population: &str, |
| 943 |
key_prefix: &str, |
| 944 |
report: &mut Report, |
| 945 |
) -> Outcome { |
| 946 |
println!( |
| 947 |
"━━━ {population}: user labels at 1.0, layer at {} ━━━", |
| 948 |
rows::IMPORT_WEIGHT |
| 949 |
); |
| 950 |
println!(); |
| 951 |
|
| 952 |
let (layer_rows, user_pool) = halve(pool); |
| 953 |
println!( |
| 954 |
" The pool splits stratified into a simulated imported layer ({} exemplars,\n \ |
| 955 |
imported at {}) and a user pool ({} labels, weight 1.0) the user's own\n \ |
| 956 |
library is drawn from. Same class mix on both sides.", |
| 957 |
layer_rows.len(), |
| 958 |
rows::IMPORT_WEIGHT, |
| 959 |
user_pool.len() |
| 960 |
); |
| 961 |
println!(); |
| 962 |
|
| 963 |
const USER_FRACTIONS: &[f64] = &[0.0, 0.05, 0.1, 0.25, 0.5, 1.0]; |
| 964 |
let classes: Vec<String> = user_pool |
| 965 |
.iter() |
| 966 |
.filter_map(|r| r.truth.clone()) |
| 967 |
.collect::<BTreeSet<_>>() |
| 968 |
.into_iter() |
| 969 |
.collect(); |
| 970 |
|
| 971 |
println!( |
| 972 |
" {:<8} {:>7} {:>9} {:>9} {:>8} {:>9} {:>9} {:>7}", |
| 973 |
"user", "labels", "vs prev", "vs layer", "differs", "mixed ok", "user ok", "delta" |
| 974 |
); |
| 975 |
println!(" {}", "─".repeat(74)); |
| 976 |
|
| 977 |
let mut computed: Vec<(String, Answers)> = Vec::new(); |
| 978 |
let mut worst_consecutive: Option<f64> = None; |
| 979 |
let mut last_value_add: Option<(usize, f64)> = None; |
| 980 |
|
| 981 |
for f in USER_FRACTIONS { |
| 982 |
let user: Vec<&Row> = classes |
| 983 |
.iter() |
| 984 |
.flat_map(|c| { |
| 985 |
let refs: Vec<&Row> = user_pool.clone(); |
| 986 |
let have = refs |
| 987 |
.iter() |
| 988 |
.filter(|r| r.truth.as_deref() == Some(c.as_str())) |
| 989 |
.count(); |
| 990 |
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] |
| 991 |
let n = (have as f64 * f).round() as usize; |
| 992 |
take_class(&refs, c, n) |
| 993 |
}) |
| 994 |
.collect(); |
| 995 |
|
| 996 |
let label = format!("{:.0}%", f * 100.0); |
| 997 |
let mixed = run_variant(&user, &layer_rows, probe, k, &label); |
| 998 |
|
| 999 |
|
| 1000 |
let user_only = run_variant(&user, &[], probe, k, &format!("{label} user-only")); |
| 1001 |
|
| 1002 |
let queued = mixed.iter().filter(|a| a.tag().is_some()).count(); |
| 1003 |
let prev = computed.last().map(|(_, a)| Churn::between(a, &mixed)); |
| 1004 |
if let Some(r) = prev.as_ref().and_then(Churn::comparable_flip_rate) { |
| 1005 |
worst_consecutive = Some(worst_consecutive.map_or(r, |w: f64| w.max(r))); |
| 1006 |
} |
| 1007 |
let vs_layer_only = computed |
| 1008 |
.first() |
| 1009 |
.map(|(_, a)| Churn::between(a, &mixed)) |
| 1010 |
.and_then(|c| c.flip_rate()); |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
let differs = (0..probe.len()) |
| 1021 |
.filter(|&i| mixed[i].tag() != user_only[i].tag()) |
| 1022 |
.count(); |
| 1023 |
let accuracy = |a: &Answers| { |
| 1024 |
a.iter() |
| 1025 |
.zip(probe) |
| 1026 |
.filter(|(x, r)| x.tag().is_some() && x.tag() == r.truth.as_deref()) |
| 1027 |
.count() as f64 |
| 1028 |
/ probe.len() as f64 |
| 1029 |
}; |
| 1030 |
let mixed_ok = accuracy(&mixed); |
| 1031 |
let user_ok = accuracy(&user_only); |
| 1032 |
last_value_add = Some((differs, mixed_ok - user_ok)); |
| 1033 |
|
| 1034 |
println!( |
| 1035 |
" {:<8} {:>7} {:>9} {:>9} {:>8} {:>9} {:>9} {:>+7.1}", |
| 1036 |
label, |
| 1037 |
user.len(), |
| 1038 |
prev.as_ref() |
| 1039 |
.map_or_else(|| "-".to_string(), |c| pct(c.flip_rate())), |
| 1040 |
pct(vs_layer_only), |
| 1041 |
differs, |
| 1042 |
pct(Some(mixed_ok)), |
| 1043 |
pct(Some(user_ok)), |
| 1044 |
(mixed_ok - user_ok) * 100.0, |
| 1045 |
); |
| 1046 |
|
| 1047 |
let key = label.trim_end_matches('%'); |
| 1048 |
report.set(&format!("{key_prefix}_user{key}_queued"), queued); |
| 1049 |
report.set(&format!("{key_prefix}_user{key}_differs"), differs); |
| 1050 |
report.set( |
| 1051 |
&format!("{key_prefix}_user{key}_mixed_accuracy"), |
| 1052 |
round4(mixed_ok), |
| 1053 |
); |
| 1054 |
report.set( |
| 1055 |
&format!("{key_prefix}_user{key}_user_accuracy"), |
| 1056 |
round4(user_ok), |
| 1057 |
); |
| 1058 |
|
| 1059 |
computed.push((label, mixed)); |
| 1060 |
} |
| 1061 |
println!(); |
| 1062 |
println!(" 'differs' is probe samples where the mixed index and the same user labels"); |
| 1063 |
println!(" alone give different answers; 'delta' is what the layer's presence does to"); |
| 1064 |
println!(" accuracy, in points. Together they are the layer's contribution surviving"); |
| 1065 |
println!(" contact with a user's own data."); |
| 1066 |
println!(); |
| 1067 |
println!(" The 0% row is the layer alone, which is the cold-start case and the only row"); |
| 1068 |
println!(" where 'user ok' is not a real alternative: a user with no labels has nothing"); |
| 1069 |
println!(" to fall back on."); |
| 1070 |
println!(); |
| 1071 |
|
| 1072 |
if let Some((differs, delta)) = last_value_add { |
| 1073 |
println!( |
| 1074 |
" At a full user library the layer changes {differs} answer(s) and moves accuracy\n \ |
| 1075 |
by {:+.1} points.", |
| 1076 |
delta * 100.0 |
| 1077 |
); |
| 1078 |
if differs == 0 { |
| 1079 |
println!(); |
| 1080 |
println!(" Zero is the strong version of the cold-start reading: once the user has"); |
| 1081 |
println!(" their own labels the layer is not merely adding little, it is changing"); |
| 1082 |
println!(" nothing at all. Worth shipping for the first week, and the documentation"); |
| 1083 |
println!(" has to say that rather than describe an ongoing contribution."); |
| 1084 |
} else if delta.abs() < 0.005 { |
| 1085 |
println!(); |
| 1086 |
println!(" Answers move and accuracy does not. That is the worst shape available:"); |
| 1087 |
println!(" the user re-reads a queue for no gain."); |
| 1088 |
} |
| 1089 |
println!(); |
| 1090 |
} |
| 1091 |
|
| 1092 |
if let Some(w) = worst_consecutive { |
| 1093 |
report.set( |
| 1094 |
&format!("{key_prefix}_worst_consecutive_flip_rate"), |
| 1095 |
round4(w), |
| 1096 |
); |
| 1097 |
println!( |
| 1098 |
" worst consecutive flip rate at the deployment weight {} ({} the {:.0}% bar)", |
| 1099 |
pct(Some(w)), |
| 1100 |
if w <= BAR { "within" } else { "OVER" }, |
| 1101 |
BAR * 100.0 |
| 1102 |
); |
| 1103 |
println!(); |
| 1104 |
} |
| 1105 |
|
| 1106 |
let _ = space; |
| 1107 |
Outcome { |
| 1108 |
name: "deployment shape", |
| 1109 |
worst: worst_consecutive, |
| 1110 |
note: format!("layer {} + user pool {}", layer_rows.len(), user_pool.len()), |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
fn value_add_deployment( |
| 1122 |
pool: &[&Row], |
| 1123 |
miss: &[&Row], |
| 1124 |
k: usize, |
| 1125 |
space: LabelSpace, |
| 1126 |
report: &mut Report, |
| 1127 |
) -> Option<Outcome> { |
| 1128 |
if miss.is_empty() { |
| 1129 |
return None; |
| 1130 |
} |
| 1131 |
println!( |
| 1132 |
" Probe restricted to the {} samples no filename rule answers.", |
| 1133 |
miss.len() |
| 1134 |
); |
| 1135 |
println!(); |
| 1136 |
let mut o = deployment_shape( |
| 1137 |
pool, |
| 1138 |
miss, |
| 1139 |
k, |
| 1140 |
space, |
| 1141 |
"3b. DEPLOYMENT SHAPE, VALUE-ADD POPULATION ONLY", |
| 1142 |
"valueadd", |
| 1143 |
report, |
| 1144 |
); |
| 1145 |
o.name = "value-add deployment"; |
| 1146 |
Some(o) |
| 1147 |
} |
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
fn feedback(pool: &[&Row], k: usize, space: LabelSpace, report: &mut Report) -> Outcome { |
| 1174 |
println!("━━━ 4. ACCEPTED-TAG FEEDBACK: the queue rewrites itself ━━━"); |
| 1175 |
println!(); |
| 1176 |
|
| 1177 |
let (layer_rows, user_pool) = halve(pool); |
| 1178 |
|
| 1179 |
|
| 1180 |
let seed_n = (user_pool.len() / 20).max(4); |
| 1181 |
let seed: Vec<&Row> = user_pool.iter().take(seed_n).copied().collect(); |
| 1182 |
let library: Vec<&Row> = user_pool.iter().skip(seed_n).copied().collect(); |
| 1183 |
let batch = (library.len() / 8).max(1); |
| 1184 |
|
| 1185 |
println!( |
| 1186 |
" Layer {} at {}, user seed {} at 1.0, library {} unlabelled. Each round", |
| 1187 |
layer_rows.len(), |
| 1188 |
rows::IMPORT_WEIGHT, |
| 1189 |
seed.len(), |
| 1190 |
library.len() |
| 1191 |
); |
| 1192 |
println!(" accepts the {batch} highest-scoring suggestions and rebuilds the index."); |
| 1193 |
println!(); |
| 1194 |
|
| 1195 |
if library.len() < 20 { |
| 1196 |
println!(" library too small to work through"); |
| 1197 |
println!(); |
| 1198 |
return Outcome { |
| 1199 |
name: "feedback", |
| 1200 |
worst: None, |
| 1201 |
note: "not runnable on this corpus".into(), |
| 1202 |
}; |
| 1203 |
} |
| 1204 |
|
| 1205 |
let mut worst: Option<f64> = None; |
| 1206 |
for accept_all in [true, false] { |
| 1207 |
let policy = if accept_all { |
| 1208 |
"accept-all" |
| 1209 |
} else { |
| 1210 |
"accept-correct" |
| 1211 |
}; |
| 1212 |
println!(" {policy}:"); |
| 1213 |
println!(); |
| 1214 |
println!( |
| 1215 |
" {:<7} {:>8} {:>8} {:>8} {:>9} {:>9}", |
| 1216 |
"round", "labels", "open", "queued", "flips", "rate" |
| 1217 |
); |
| 1218 |
println!(" {}", "─".repeat(56)); |
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
let mut accepted: Vec<&Row> = Vec::new(); |
| 1224 |
let mut open: Vec<&Row> = library.clone(); |
| 1225 |
let mut previous: Option<HashMap<&str, Answer>> = None; |
| 1226 |
let mut changes: HashMap<&str, usize> = HashMap::new(); |
| 1227 |
let mut round = 0usize; |
| 1228 |
|
| 1229 |
while !open.is_empty() { |
| 1230 |
let local: Vec<&Row> = seed.iter().chain(&accepted).copied().collect(); |
| 1231 |
let db = match rows::mixed_db(&local, &layer_rows) { |
| 1232 |
Ok(db) => db, |
| 1233 |
Err(e) => { |
| 1234 |
eprintln!("{policy} round {round}: {e}"); |
| 1235 |
std::process::exit(1); |
| 1236 |
} |
| 1237 |
}; |
| 1238 |
let index = match exemplar::build_index(&db) { |
| 1239 |
Ok(i) => i, |
| 1240 |
Err(e) => { |
| 1241 |
eprintln!("{policy} round {round}: build_index: {e}"); |
| 1242 |
std::process::exit(1); |
| 1243 |
} |
| 1244 |
}; |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
let scored: Vec<(&Row, Option<exemplar::TagScore>)> = open |
| 1249 |
.iter() |
| 1250 |
.map(|r| { |
| 1251 |
let top = index |
| 1252 |
.score(&r.vector, k, None) |
| 1253 |
.into_iter() |
| 1254 |
.next() |
| 1255 |
.filter(|s| s.score >= DEFAULT_REVIEW_THRESHOLD); |
| 1256 |
(*r, top) |
| 1257 |
}) |
| 1258 |
.collect(); |
| 1259 |
|
| 1260 |
let now: HashMap<&str, Answer> = scored |
| 1261 |
.iter() |
| 1262 |
.map(|(r, top)| { |
| 1263 |
( |
| 1264 |
r.hash.as_str(), |
| 1265 |
top.as_ref() |
| 1266 |
.map_or(Answer::Silent, |s| Answer::Tag(s.tag.clone())), |
| 1267 |
) |
| 1268 |
}) |
| 1269 |
.collect(); |
| 1270 |
let queued = now.values().filter(|a| a.tag().is_some()).count(); |
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
let (both, flips) = previous.as_ref().map_or((0, 0), |before| { |
| 1276 |
let mut b = 0; |
| 1277 |
let mut f = 0; |
| 1278 |
for (hash, after) in &now { |
| 1279 |
let Some(prev) = before.get(hash) else { |
| 1280 |
continue; |
| 1281 |
}; |
| 1282 |
if let (Some(p), Some(q)) = (prev.tag(), after.tag()) { |
| 1283 |
b += 1; |
| 1284 |
if p != q { |
| 1285 |
f += 1; |
| 1286 |
*changes.entry(*hash).or_default() += 1; |
| 1287 |
} |
| 1288 |
} |
| 1289 |
} |
| 1290 |
(b, f) |
| 1291 |
}); |
| 1292 |
let rate = (both > 0).then(|| flips as f64 / both as f64); |
| 1293 |
|
| 1294 |
|
| 1295 |
let thin = both > 0 && both < MIN_COMPARABLE; |
| 1296 |
if let Some(r) = rate.filter(|_| round > 0 && !thin) { |
| 1297 |
worst = Some(worst.map_or(r, |w: f64| w.max(r))); |
| 1298 |
} |
| 1299 |
|
| 1300 |
println!( |
| 1301 |
" {:<7} {:>8} {:>8} {:>8} {:>9} {:>9}{}", |
| 1302 |
round, |
| 1303 |
local.len(), |
| 1304 |
open.len(), |
| 1305 |
queued, |
| 1306 |
flips, |
| 1307 |
rate.map_or_else(|| "-".to_string(), |r| pct(Some(r))), |
| 1308 |
if thin { " (thin)" } else { "" } |
| 1309 |
); |
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
let mut candidates: Vec<(&Row, &exemplar::TagScore)> = scored |
| 1315 |
.iter() |
| 1316 |
.filter_map(|(r, top)| top.as_ref().map(|s| (*r, s))) |
| 1317 |
.filter(|(r, s)| accept_all || Some(s.tag.as_str()) == r.truth.as_deref()) |
| 1318 |
.collect(); |
| 1319 |
candidates.sort_by(|a, b| b.1.score.total_cmp(&a.1.score)); |
| 1320 |
let taking: Vec<&str> = candidates |
| 1321 |
.iter() |
| 1322 |
.take(batch) |
| 1323 |
.map(|(r, _)| r.hash.as_str()) |
| 1324 |
.collect(); |
| 1325 |
if taking.is_empty() { |
| 1326 |
println!(" nothing left above the review threshold; the queue is dry"); |
| 1327 |
break; |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
let taken: BTreeSet<&str> = taking.iter().copied().collect(); |
| 1334 |
for (r, s) in candidates |
| 1335 |
.iter() |
| 1336 |
.filter(|(r, _)| taken.contains(r.hash.as_str())) |
| 1337 |
{ |
| 1338 |
accepted.push(accepted_row(r, &s.tag)); |
| 1339 |
} |
| 1340 |
open.retain(|r| !taken.contains(r.hash.as_str())); |
| 1341 |
previous = Some(now); |
| 1342 |
round += 1; |
| 1343 |
if round > 20 { |
| 1344 |
break; |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
let oscillating = changes.values().filter(|n| **n > 1).count(); |
| 1349 |
println!(); |
| 1350 |
println!( |
| 1351 |
" (thin) marks a round with fewer than {MIN_COMPARABLE} entries still open. Those rates" |
| 1352 |
); |
| 1353 |
println!(" are one sample either way and do not decide the verdict."); |
| 1354 |
println!(" {oscillating} sample(s) changed answer more than once across the run."); |
| 1355 |
if oscillating == 0 { |
| 1356 |
println!(" No oscillation: a sample that moved, moved once and stayed."); |
| 1357 |
} else { |
| 1358 |
println!(" Oscillation is the failure a decaying average hides: the mean flip"); |
| 1359 |
println!(" rate can fall while individual entries keep swapping back."); |
| 1360 |
} |
| 1361 |
println!(); |
| 1362 |
report.set( |
| 1363 |
&format!("feedback_{}_oscillating", policy.replace('-', "_")), |
| 1364 |
oscillating, |
| 1365 |
); |
| 1366 |
} |
| 1367 |
|
| 1368 |
if let Some(w) = worst { |
| 1369 |
report.set("feedback_worst_flip_rate", round4(w)); |
| 1370 |
} |
| 1371 |
let _ = space; |
| 1372 |
Outcome { |
| 1373 |
name: "feedback", |
| 1374 |
worst, |
| 1375 |
note: format!("batch {batch}"), |
| 1376 |
} |
| 1377 |
} |
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
fn accepted_row(r: &Row, tag: &str) -> &'static Row { |
| 1387 |
Box::leak(Box::new(Row { |
| 1388 |
hash: r.hash.clone(), |
| 1389 |
vector: r.vector.clone(), |
| 1390 |
tags: vec![tag.to_string()], |
| 1391 |
truth: r.truth.clone(), |
| 1392 |
origin: r.origin.clone(), |
| 1393 |
name: r.name.clone(), |
| 1394 |
})) |
| 1395 |
} |
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
fn verdict(outcomes: &[Outcome], space: LabelSpace, classes: &[String], report: &mut Report) { |
| 1400 |
println!("━━━ VERDICT ━━━"); |
| 1401 |
println!(); |
| 1402 |
println!(" {:<20} {:>12}", "measurement", "worst flip"); |
| 1403 |
println!(" {}", "─".repeat(64)); |
| 1404 |
|
| 1405 |
let mut failed = Vec::new(); |
| 1406 |
let mut unrun = Vec::new(); |
| 1407 |
for o in outcomes { |
| 1408 |
let state = match o.worst { |
| 1409 |
Some(w) if w <= BAR => "within the bar", |
| 1410 |
Some(_) => { |
| 1411 |
failed.push(o.name); |
| 1412 |
"OVER THE BAR" |
| 1413 |
} |
| 1414 |
None => { |
| 1415 |
unrun.push(o.name); |
| 1416 |
"not measured" |
| 1417 |
} |
| 1418 |
}; |
| 1419 |
println!( |
| 1420 |
" {:<20} {:>12} {state} ({})", |
| 1421 |
o.name, |
| 1422 |
o.worst.map_or_else(|| "-".to_string(), |w| pct(Some(w))), |
| 1423 |
o.note |
| 1424 |
); |
| 1425 |
} |
| 1426 |
println!(); |
| 1427 |
|
| 1428 |
report.set("failed_measurements", failed.len()); |
| 1429 |
report.set("unrun_measurements", unrun.len()); |
| 1430 |
|
| 1431 |
if failed.is_empty() && unrun.is_empty() { |
| 1432 |
println!(" PASS on every measurement that ran."); |
| 1433 |
} else if failed.is_empty() { |
| 1434 |
println!( |
| 1435 |
" PASS on what ran; {} not measurable on this corpus: {}.", |
| 1436 |
unrun.len(), |
| 1437 |
unrun.join(", ") |
| 1438 |
); |
| 1439 |
} else { |
| 1440 |
println!(" FAIL: {}.", failed.join(", ")); |
| 1441 |
} |
| 1442 |
println!(); |
| 1443 |
|
| 1444 |
|
| 1445 |
let (covered, uncovered) = families::covered_families(classes); |
| 1446 |
if space != LabelSpace::Instrument && !uncovered.is_empty() { |
| 1447 |
println!( |
| 1448 |
" SCOPE: this corpus reaches {} of 7 families ({}). It says nothing about", |
| 1449 |
covered.len(), |
| 1450 |
covered.join(", ") |
| 1451 |
); |
| 1452 |
println!( |
| 1453 |
" {}, and a stability number measured over two", |
| 1454 |
uncovered.join(", ") |
| 1455 |
); |
| 1456 |
println!(" classes is an easier question than the shipped layer will face: fewer"); |
| 1457 |
println!(" classes means fewer things an answer can flip to. Read every figure above"); |
| 1458 |
println!(" as a floor on the flip rate, not an estimate of it."); |
| 1459 |
println!(); |
| 1460 |
} |
| 1461 |
println!(" And the standing caveat: this measures whether the answer is the SAME, not"); |
| 1462 |
println!(" whether it is RIGHT. The two are independent. layer-eval is the other half."); |
| 1463 |
println!(); |
| 1464 |
} |
| 1465 |
|
| 1466 |
|
| 1467 |
pub(crate) fn probe_denominator_from_env() -> usize { |
| 1468 |
std::env::var("AF_BENCH_STABILITY_PROBE") |
| 1469 |
.ok() |
| 1470 |
.and_then(|v| v.parse().ok()) |
| 1471 |
.filter(|n| *n >= 2) |
| 1472 |
.unwrap_or(5) |
| 1473 |
} |
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
pub(crate) fn k_from_env() -> usize { |
| 1483 |
std::env::var("AF_BENCH_EVAL_K") |
| 1484 |
.ok() |
| 1485 |
.and_then(|v| v.split(',').next()?.trim().parse().ok()) |
| 1486 |
.filter(|k| *k > 0) |
| 1487 |
.unwrap_or(DEFAULT_K) |
| 1488 |
} |
| 1489 |
|
| 1490 |
#[cfg(test)] |
| 1491 |
mod tests { |
| 1492 |
use super::*; |
| 1493 |
|
| 1494 |
fn ans(tags: &[Option<&str>]) -> Answers { |
| 1495 |
tags.iter() |
| 1496 |
.map(|t| t.map_or(Answer::Silent, |x| Answer::Tag(x.into()))) |
| 1497 |
.collect() |
| 1498 |
} |
| 1499 |
|
| 1500 |
#[test] |
| 1501 |
fn churn_separates_a_flip_from_a_queue_getting_longer() { |
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
let a = ans(&[Some("low"), Some("low"), None, Some("low")]); |
| 1506 |
let b = ans(&[Some("low"), Some("bright"), Some("low"), None]); |
| 1507 |
let c = Churn::between(&a, &b); |
| 1508 |
assert_eq!(c.both, 2); |
| 1509 |
assert_eq!(c.flips, 1); |
| 1510 |
assert_eq!(c.appeared, 1); |
| 1511 |
assert_eq!(c.vanished, 1); |
| 1512 |
assert_eq!(c.flip_rate(), Some(0.5)); |
| 1513 |
} |
| 1514 |
|
| 1515 |
#[test] |
| 1516 |
fn no_overlapping_queue_is_not_a_flip_rate_of_zero() { |
| 1517 |
|
| 1518 |
|
| 1519 |
let a = ans(&[Some("low"), None]); |
| 1520 |
let b = ans(&[None, Some("low")]); |
| 1521 |
let c = Churn::between(&a, &b); |
| 1522 |
assert_eq!(c.flip_rate(), None); |
| 1523 |
assert!(!c.passes(), "an unmeasurable pair must not pass"); |
| 1524 |
} |
| 1525 |
|
| 1526 |
#[test] |
| 1527 |
fn the_review_threshold_is_what_gates_an_answer() { |
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
const { |
| 1533 |
assert!(DEFAULT_REVIEW_THRESHOLD < exemplar::DEFAULT_AUTO_THRESHOLD); |
| 1534 |
} |
| 1535 |
} |
| 1536 |
|
| 1537 |
#[test] |
| 1538 |
fn halving_keeps_the_class_mix_on_both_sides() { |
| 1539 |
let rows: Vec<Row> = (0..10) |
| 1540 |
.map(|i| Row { |
| 1541 |
hash: format!("h{i}"), |
| 1542 |
vector: Vec::new(), |
| 1543 |
tags: Vec::new(), |
| 1544 |
truth: Some(if i < 6 { "low" } else { "bright" }.to_string()), |
| 1545 |
origin: String::new(), |
| 1546 |
name: String::new(), |
| 1547 |
}) |
| 1548 |
.collect(); |
| 1549 |
let refs: Vec<&Row> = rows.iter().collect(); |
| 1550 |
let (a, b) = halve(&refs); |
| 1551 |
let low = |v: &[&Row]| { |
| 1552 |
v.iter() |
| 1553 |
.filter(|r| r.truth.as_deref() == Some("low")) |
| 1554 |
.count() |
| 1555 |
}; |
| 1556 |
assert_eq!(low(&a), 3); |
| 1557 |
assert_eq!(low(&b), 3); |
| 1558 |
assert_eq!(a.len() + b.len(), 10); |
| 1559 |
} |
| 1560 |
|
| 1561 |
#[test] |
| 1562 |
fn take_class_is_a_prefix_so_the_size_sweep_nests() { |
| 1563 |
|
| 1564 |
|
| 1565 |
let rows: Vec<Row> = (0..6) |
| 1566 |
.map(|i| Row { |
| 1567 |
hash: format!("h{i}"), |
| 1568 |
vector: Vec::new(), |
| 1569 |
tags: Vec::new(), |
| 1570 |
truth: Some("low".to_string()), |
| 1571 |
origin: String::new(), |
| 1572 |
name: String::new(), |
| 1573 |
}) |
| 1574 |
.collect(); |
| 1575 |
let refs: Vec<&Row> = rows.iter().collect(); |
| 1576 |
let small = take_class(&refs, "low", 2); |
| 1577 |
let big = take_class(&refs, "low", 4); |
| 1578 |
assert_eq!(small.len(), 2); |
| 1579 |
assert_eq!(big.len(), 4); |
| 1580 |
assert!( |
| 1581 |
small.iter().zip(&big).all(|(s, b)| std::ptr::eq(*s, *b)), |
| 1582 |
"the smaller index must be a prefix of the larger" |
| 1583 |
); |
| 1584 |
} |
| 1585 |
|
| 1586 |
#[test] |
| 1587 |
fn the_filename_rules_are_name_only() { |
| 1588 |
|
| 1589 |
|
| 1590 |
let rules = filename_rules().expect("the starter pack seeds"); |
| 1591 |
assert!(!rules.is_empty()); |
| 1592 |
for r in &rules { |
| 1593 |
assert!( |
| 1594 |
r.conditions.iter().all(|c| c.field == RuleField::Name), |
| 1595 |
"{} reads a field other than the name", |
| 1596 |
r.name |
| 1597 |
); |
| 1598 |
} |
| 1599 |
} |
| 1600 |
} |
| 1601 |
|