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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
"""

Comprehensive evaluation benchmarks for TouchGrass music modules.



This script evaluates:

1. Tab & Chord Generation accuracy

2. Music Theory knowledge

3. Ear Training interval identification

4. EQ Adapter emotion detection

5. Songwriting coherence and creativity

"""

import argparse
import json
import torch
from pathlib import Path
from typing import Dict, List, Any
from tqdm import tqdm

# Import TouchGrass modules
from TouchGrass.models.tab_chord_module import TabChordModule
from TouchGrass.models.music_theory_module import MusicTheoryModule
from TouchGrass.models.ear_training_module import EarTrainingModule
from TouchGrass.models.eq_adapter import MusicEQAdapter
from TouchGrass.models.songwriting_module import SongwritingModule


class MusicModuleEvaluator:
    """Evaluator for all TouchGrass music modules."""

    def __init__(self, device: str = "cpu", d_model: int = 768):
        self.device = device
        self.d_model = d_model
        self.results = {}

        # Initialize modules
        self.tab_chord = TabChordModule(d_model=d_model).to(device)
        self.music_theory = MusicTheoryModule(d_model=d_model).to(device)
        self.ear_training = EarTrainingModule(d_model=d_model).to(device)
        self.eq_adapter = MusicEQAdapter(d_model=d_model).to(device)
        self.songwriting = SongwritingModule(d_model=d_model).to(device)

        # Set all modules to eval mode
        self._set_eval_mode()

    def _set_eval_mode(self):
        """Set all modules to evaluation mode."""
        self.tab_chord.eval()
        self.music_theory.eval()
        self.ear_training.eval()
        self.eq_adapter.eval()
        self.songwriting.eval()

    def evaluate_all(self, test_data_path: str = None) -> Dict[str, Any]:
        """Run all evaluations and return comprehensive results."""
        print("=" * 60)
        print("TouchGrass Music Module Evaluation")
        print("=" * 60)

        # Run individual module evaluations
        self.results["tab_chord"] = self.evaluate_tab_chord()
        print(f"✓ Tab & Chord: {self.results['tab_chord']['accuracy']:.2%}")

        self.results["music_theory"] = self.evaluate_music_theory()
        print(f"✓ Music Theory: {self.results['music_theory']['accuracy']:.2%}")

        self.results["ear_training"] = self.evaluate_ear_training()
        print(f"✓ Ear Training: {self.results['ear_training']['accuracy']:.2%}")

        self.results["eq_adapter"] = self.evaluate_eq_adapter()
        print(f"✓ EQ Adapter: {self.results['eq_adapter']['accuracy']:.2%}")

        self.results["songwriting"] = self.evaluate_songwriting()
        print(f"✓ Songwriting: {self.results['songwriting']['coherence_score']:.2%}")

        # Calculate overall score
        scores = [
            self.results["tab_chord"]["accuracy"],
            self.results["music_theory"]["accuracy"],
            self.results["ear_training"]["accuracy"],
            self.results["eq_adapter"]["accuracy"],
            self.results["songwriting"]["coherence_score"]
        ]
        self.results["overall_score"] = sum(scores) / len(scores)
        print(f"\nOverall Score: {self.results['overall_score']:.2%}")

        return self.results

    def evaluate_tab_chord(self) -> Dict[str, Any]:
        """Evaluate Tab & Chord Generation module."""
        print("\n[1] Evaluating Tab & Chord Module...")

        test_cases = [
            # (string_indices, fret_indices, expected_valid)
            (torch.tensor([[0, 1, 2]]), torch.tensor([[0, 3, 5]]), True),   # Open strings and frets
            (torch.tensor([[5, 4, 3, 2, 1, 0]]), torch.tensor([[1, 1, 2, 2, 3, 3]]), True),  # F chord shape
            (torch.tensor([[0, 0, 0]]), torch.tensor([[0, 0, 0]]), True),  # All open
            (torch.tensor([[0, 0, 0]]), torch.tensor([[1, 1, 1]]), True),  # All 1st fret
        ]

        correct = 0
        total = len(test_cases)

        for string_indices, fret_indices, expected_valid in test_cases:
            batch_size, seq_len = string_indices.shape
            hidden_states = torch.randn(batch_size, seq_len, self.d_model)

            with torch.no_grad():
                output = self.tab_chord(hidden_states, string_indices, fret_indices)
                validator_score = output["tab_validator"].mean().item()

                # If expected valid, validator should be > 0.5
                # If expected invalid, validator should be < 0.5
                predicted_valid = validator_score > 0.5
                if predicted_valid == expected_valid:
                    correct += 1

        accuracy = correct / total if total > 0 else 0.0

        return {
            "accuracy": accuracy,
            "correct": correct,
            "total": total
        }

    def evaluate_music_theory(self) -> Dict[str, Any]:
        """Evaluate Music Theory Engine."""
        print("\n[2] Evaluating Music Theory Module...")

        tests = [
            ("scale_c_major", self._test_scale_c_major),
            ("scale_a_minor", self._test_scale_a_minor),
            ("chord_functions", self._test_chord_functions),
            ("circle_of_fifths", self._test_circle_of_fifths),
            ("interval_conversion", self._test_interval_conversion),
        ]

        results = {}
        for name, test_func in tests:
            score = test_func()
            results[name] = score
            print(f"  - {name}: {score:.2%}")

        avg_accuracy = sum(results.values()) / len(results) if results else 0.0
        return {
            "accuracy": avg_accuracy,
            "detailed": results
        }

    def _test_scale_c_major(self) -> float:
        """Test C major scale generation."""
        scale = self.music_theory.get_scale_from_key("C", "major")
        expected = ["C", "D", "E", "F", "G", "A", "B"]
        return 1.0 if scale == expected else 0.0

    def _test_scale_a_minor(self) -> float:
        """Test A natural minor scale."""
        scale = self.music_theory.get_scale_from_key("A", "natural_minor")
        expected = ["A", "B", "C", "D", "E", "F", "G"]
        return 1.0 if scale == expected else 0.0

    def _test_chord_functions(self) -> float:
        """Test chord function detection in C major."""
        tests = [
            ("C", "major", "C", "I"),
            ("F", "major", "C", "IV"),
            ("G", "major", "C", "V"),
            ("D", "minor", "C", "ii"),
            ("E", "minor", "C", "iii"),
            ("A", "minor", "C", "vi"),
            ("B", "dim", "C", "vii°"),
        ]

        correct = 0
        for root, chord_type, key, expected in tests:
            result = self.music_theory.detect_chord_function(root, chord_type, key)
            if result == expected:
                correct += 1

        return correct / len(tests)

    def _test_circle_of_fifths(self) -> float:
        """Test circle of fifths generation."""
        circle = self.music_theory.get_circle_of_fifths()
        # Should have 12 keys
        if len(circle) != 12:
            return 0.0
        # Should contain all major keys
        expected_keys = {"C", "G", "D", "A", "E", "B", "F#", "Db", "Ab", "Eb", "Bb", "F"}
        return 1.0 if set(circle) == expected_keys else 0.0

    def _test_interval_conversion(self) -> float:
        """Test interval name to semitone conversion."""
        tests = [
            (0, "P1"), (1, "m2"), (2, "M2"), (3, "m3"), (4, "M3"),
            (5, "P4"), (6, "TT"), (7, "P5"), (8, "m6"), (9, "M6"),
            (10, "m7"), (11, "M7"), (12, "P8")
        ]

        correct = 0
        for semitones, expected_name in tests:
            name = self.music_theory.semitones_to_interval(semitones)
            if name == expected_name:
                correct += 1

        return correct / len(tests)

    def evaluate_ear_training(self) -> Dict[str, Any]:
        """Evaluate Ear Training module."""
        print("\n[3] Evaluating Ear Training Module...")

        tests = [
            ("interval_names", self._test_interval_names),
            ("interval_to_semitones", self._test_interval_to_semitones),
            ("solfege_syllables", self._test_solfege_syllables),
            ("song_references", self._test_song_references),
        ]

        results = {}
        for name, test_func in tests:
            score = test_func()
            results[name] = score
            print(f"  - {name}: {score:.2%}")

        avg_accuracy = sum(results.values()) / len(results) if results else 0.0
        return {
            "accuracy": avg_accuracy,
            "detailed": results
        }

    def _test_interval_names(self) -> float:
        """Test interval name retrieval."""
        tests = [
            (0, "P1"), (2, "M2"), (4, "M3"), (5, "P4"),
            (7, "P5"), (9, "M6"), (11, "M7"), (12, "P8")
        ]

        correct = 0
        for semitones, expected in tests:
            name = self.ear_training.get_interval_name(semitones)
            if name == expected:
                correct += 1

        return correct / len(tests)

    def _test_interval_to_semitones(self) -> float:
        """Test interval name to semitone conversion."""
        tests = [
            ("P1", 0), ("M2", 2), ("M3", 4), ("P4", 5),
            ("P5", 7), ("M6", 9), ("M7", 11), ("P8", 12)
        ]

        correct = 0
        for name, expected_semitones in tests:
            semitones = self.ear_training.name_to_interval(name)
            if semitones == expected_semitones:
                correct += 1

        return correct / len(tests)

    def _test_solfege_syllables(self) -> float:
        """Test solfege syllable generation."""
        c_major = self.ear_training.get_solfege_syllables("C", "major")
        expected = ["Do", "Re", "Mi", "Fa", "So", "La", "Ti", "Do"]

        return 1.0 if c_major == expected else 0.0

    def _test_song_references(self) -> float:
        """Test that song references exist for common intervals."""
        common_intervals = ["P5", "M3", "m3", "P4", "M2"]
        correct = 0

        for interval in common_intervals:
            refs = self.ear_training.get_song_reference(interval)
            if len(refs) > 0:
                correct += 1

        return correct / len(common_intervals)

    def evaluate_eq_adapter(self) -> Dict[str, Any]:
        """Evaluate EQ Adapter emotion detection."""
        print("\n[4] Evaluating EQ Adapter...")

        tests = [
            ("frustration_range", self._test_frustration_range),
            ("emotion_classifier_output", self._test_emotion_classifier),
            ("encouragement_output", self._test_encouragement_output),
            ("simplification_output", self._test_simplification_output),
        ]

        results = {}
        for name, test_func in tests:
            score = test_func()
            results[name] = score
            print(f"  - {name}: {score:.2%}")

        avg_accuracy = sum(results.values()) / len(results) if results else 0.0
        return {
            "accuracy": avg_accuracy,
            "detailed": results
        }

    def _test_frustration_range(self) -> float:
        """Test that frustration scores are in [0, 1]."""
        batch_size, seq_len = 2, 5
        hidden_states = torch.randn(batch_size, seq_len, self.d_model)

        with torch.no_grad():
            output = self.eq_adapter(hidden_states)
            frustration = output["frustration"]

            # All values should be between 0 and 1
            in_range = ((frustration >= 0) & (frustration <= 1)).all().item()
            return 1.0 if in_range else 0.0

    def _test_emotion_classifier(self) -> float:
        """Test emotion classifier output shape."""
        batch_size, seq_len = 2, 5
        hidden_states = torch.randn(batch_size, seq_len, self.d_model)

        with torch.no_grad():
            output = self.eq_adapter(hidden_states)
            emotion = output["emotion"]

            # Should have 4 emotion classes
            correct_shape = emotion.shape == (batch_size, seq_len, 4)
            return 1.0 if correct_shape else 0.0

    def _test_encouragement_output(self) -> float:
        """Test that encouragement output is produced."""
        batch_size, seq_len = 2, 5
        hidden_states = torch.randn(batch_size, seq_len, self.d_model)

        with torch.no_grad():
            output = self.eq_adapter(hidden_states)
            has_encouragement = "encouragement" in output
            correct_shape = output["encouragement"].shape[0] == batch_size

            return 1.0 if has_encouragement and correct_shape else 0.0

    def _test_simplification_output(self) -> float:
        """Test that simplification output matches input shape."""
        batch_size, seq_len = 2, 5
        hidden_states = torch.randn(batch_size, seq_len, self.d_model)

        with torch.no_grad():
            output = self.eq_adapter(hidden_states)
            correct_shape = output["simplification"].shape == hidden_states.shape
            return 1.0 if correct_shape else 0.0

    def evaluate_songwriting(self) -> Dict[str, Any]:
        """Evaluate Song Writing module."""
        print("\n[5] Evaluating Songwriting Module...")

        tests = [
            ("progression_generation", self._test_progression_generation),
            ("mood_classifier", self._test_mood_classifier),
            ("genre_classifier", self._test_genre_classifier),
            ("hook_generation", self._test_hook_generation),
            ("production_suggestions", self._test_production_suggestions),
        ]

        results = {}
        for name, test_func in tests:
            score = test_func()
            results[name] = score
            print(f"  - {name}: {score:.2%}")

        avg_accuracy = sum(results.values()) / len(results) if results else 0.0
        return {
            "coherence_score": avg_accuracy,
            "detailed": results
        }

    def _test_progression_generation(self) -> float:
        """Test chord progression generation."""
        try:
            progression = self.songwriting.suggest_progression(
                mood="happy", genre="pop", num_chords=4, key="C"
            )
            # Should return list of tuples
            if not isinstance(progression, list):
                return 0.0
            if len(progression) != 4:
                return 0.0
            if not all(isinstance(p, tuple) and len(p) == 2 for p in progression):
                return 0.0
            return 1.0
        except Exception:
            return 0.0

    def _test_mood_classifier(self) -> float:
        """Test mood classifier output."""
        batch_size, seq_len = 2, 5
        hidden_states = torch.randn(batch_size, seq_len, self.d_model)
        chord_ids = torch.randint(0, 24, (batch_size, seq_len))

        with torch.no_grad():
            output = self.songwriting(hidden_states, chord_ids)
            mood = output["mood"]

            # Should have at least 8 moods
            correct_shape = mood.shape[-1] >= 8
            return 1.0 if correct_shape else 0.0

    def _test_genre_classifier(self) -> float:
        """Test genre classifier output."""
        batch_size, seq_len = 2, 5
        hidden_states = torch.randn(batch_size, seq_len, self.d_model)
        chord_ids = torch.randint(0, 24, (batch_size, seq_len))

        with torch.no_grad():
            output = self.songwriting(hidden_states, chord_ids)
            genre = output["genre"]

            # Should have at least 8 genres
            correct_shape = genre.shape[-1] >= 8
            return 1.0 if correct_shape else 0.0

    def _test_hook_generation(self) -> float:
        """Test hook generation."""
        try:
            hook = self.songwriting.generate_hook(
                theme="freedom", genre="pop", key="C"
            )
            # Should return dict with hook text
            if not isinstance(hook, dict):
                return 0.0
            if "hook" not in hook:
                return 0.0
            if not isinstance(hook["hook"], str):
                return 0.0
            if len(hook["hook"]) == 0:
                return 0.0
            return 1.0
        except Exception:
            return 0.0

    def _test_production_suggestions(self) -> float:
        """Test production element suggestions."""
        try:
            production = self.songwriting.suggest_production(
                genre="rock", mood="energetic", bpm=120
            )
            # Should return dict with elements or suggestions
            if not isinstance(production, dict):
                return 0.0
            has_elements = "elements" in production or "suggestions" in production
            return 1.0 if has_elements else 0.0
        except Exception:
            return 0.0

    def save_results(self, output_path: str):
        """Save evaluation results to JSON file."""
        output_path = Path(output_path)
        output_path.parent.mkdir(parents=True, exist_ok=True)

        with open(output_path, 'w', encoding='utf-8') as f:
            json.dump(self.results, f, indent=2)

        print(f"\n✓ Results saved to {output_path}")


def main():
    parser = argparse.ArgumentParser(description="Evaluate TouchGrass music modules")
    parser.add_argument("--device", type=str, default="cpu", help="Device to use (cpu or cuda)")
    parser.add_argument("--d_model", type=int, default=768, help="Model dimension")
    parser.add_argument("--output", type=str, default="benchmarks/results/music_module_eval.json",
                       help="Output path for results")
    parser.add_argument("--seed", type=int, default=42, help="Random seed")

    args = parser.parse_args()

    # Set random seed
    torch.manual_seed(args.seed)

    # Create evaluator
    evaluator = MusicModuleEvaluator(device=args.device, d_model=args.d_model)

    # Run evaluation
    results = evaluator.evaluate_all()

    # Save results
    evaluator.save_results(args.output)

    print("\n" + "=" * 60)
    print("Evaluation complete!")
    print("=" * 60)


if __name__ == "__main__":
    main()