-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_batch_matching.rs
More file actions
140 lines (120 loc) · 5.86 KB
/
debug_batch_matching.rs
File metadata and controls
140 lines (120 loc) · 5.86 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#![allow(dead_code)]
use crate::analysis::matching::{ChordMatcher, MatchOutcome, Voicing};
use crate::utils::run_samples::{run_root_batch, RunOptions};
use crate::processing::peak_detection_bands::PeakData;
use crate::utils::logging::{write_summary_md, MatchRecord, FileLogger};
use crate::utils::chord_diagrams::{render_diagram, PathMode};
use crate::utils::weights::MatcherCfg;
use std::collections::HashMap;
pub fn run_chord_matching_batch(root: &str) -> Vec<MatchRecord> {
let opts = RunOptions {
top_n: 15, // allow room for filtering
threshold_percentile: 90.0,
enable_bands: true,
verbose: false,
plot: false,
};
let results: HashMap<String, Vec<PeakData>> = match run_root_batch(root, &opts) {
Ok(data) => data,
Err(e) => {
eprintln!("❌ Batch failed: {}", e);
return vec![];
}
};
// build matcher
let mut matcher = ChordMatcher::from_cfg(MatcherCfg::load());
matcher.top_n = 7; // truncation post-filtering
matcher.matched_top_n = 5; // take n highest scores
matcher.logger = FileLogger::boxed(); // <-- enables full logging
// use crate::utils::logging::Logger;
// matcher.logger = Logger::disabled(); // <-- turn logs off
let mut total = 0;
let mut correct = 0;
let mut records: Vec<MatchRecord> = Vec::new();
let mut summary_rows: Vec<String> = Vec::new();
println!("\n🎸 Chord Matching Batch for Root: {}", root);
for (label, peaks) in results.iter() {
let expected = label.trim().to_ascii_lowercase();
// ─────────────────── run matcher ────────────────────────
let prediction = matcher.identify_chord(
label, &expected, peaks, Voicing::Barre, true
);
// ───────────────── render chord diagram (Certain only) ──────────────
if let MatchOutcome::Certain { name, .. } = &prediction.outcome {
// ignore errors, just log
if let Err(e) = render_diagram(label, name, true, PathMode::Group) {
eprintln!("⚠️ diagram render failed for {label}: {e}");
}
}
// ─────────────────── unpack outcome ─────────────────────
let (predicted, details_opt) = match prediction.outcome {
MatchOutcome::Certain { name, details, .. } => (name, Some(details)),
MatchOutcome::Ambiguous { name, second, .. } =>
(format!("{name}/{second}"), None),
MatchOutcome::NoMatch =>
("none".to_string(), None),
};
let predicted_lc = predicted.to_ascii_lowercase();
let is_match = expected == predicted_lc;
total += 1;
if is_match { correct += 1; }
// ─────────────────── collect CSV record ─────────────────
if let Some(details) = details_opt.as_ref() {
records.push(MatchRecord {
label: label.to_string(),
predicted: predicted_lc.clone(),
is_match,
base_score: details.base_score,
amplitude_bonus: details.amplitude_bonus,
penalty_extra: details.penalty_extra,
repetition_bonus: details.repetition_bonus,
size_bonus: details.size_bonus,
missing_notes_penalty: details.missing_notes_penalty,
total_score: details.total_score,
seventh_score: details.seventh_score,
matched_count: details.matched.len(),
extraneous_count: details.extraneous.len(),
bass_note: details.bass_note.clone(),
chord_template: format!("{:?}", details.chord_template),
matched: format!("{:?}", details.matched),
extraneous: format!("{:?}", details.extraneous),
detected: format!("{:?}", details.detected),
});
// full log only for “Certain”
matcher.logger.as_ref().log_match_summary(
root,
label,
&predicted,
details,
is_match
).ok();
} else {
// placeholder row (zeros) for Ambiguous / NoMatch
records.push(MatchRecord {
label: label.to_string(),
predicted: predicted_lc.clone(),
is_match,
base_score: 0.0, amplitude_bonus: 0.0, penalty_extra: 0.0,
repetition_bonus: 0.0, size_bonus: 0.0, seventh_score: 0.0,
missing_notes_penalty: 0.0, total_score: 0.0,
matched_count: 0, extraneous_count: 0,
bass_note: "-".into(),
chord_template: "[]".into(), matched: "[]".into(),
extraneous: "[]".into(), detected: "[]".into(),
});
}
// ─────────────────── CLI summary row ────────────────────
let status = if is_match { "✅" } else { "❌" };
summary_rows.push(format!(
"| {:<12} | {:<12} | {} |",
expected, predicted, status
));
}
// Write a Markdown summary and finish
if let Err(e) = write_summary_md(root, &summary_rows, total, correct) {
eprintln!("❌ Failed to write markdown summary for {}: {e}", root);
};
println!("Summary: {}/{} correct ({:.1}%)", correct, total, 100.0 * correct as f32 / total as f32);
records.sort_by(|a, b| a.label.to_lowercase().cmp(&b.label.to_lowercase()));
records
}