File size: 28,605 Bytes
6d1bbc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
"""Tests for negbiodb.metrics — ML evaluation metrics."""

from __future__ import annotations

import numpy as np
import pytest

from negbiodb.metrics import (
    auprc,
    auroc,
    bedroc,
    compute_all_metrics,
    enrichment_factor,
    evaluate_splits,
    log_auc,
    mcc,
    save_results,
    summarize_runs,
)


# ------------------------------------------------------------------
# Fixtures
# ------------------------------------------------------------------

@pytest.fixture
def perfect_data():
    """Perfect classifier: actives score=1.0, inactives score=0.0."""
    y_true = np.array([1] * 50 + [0] * 950)
    y_score = np.array([1.0] * 50 + [0.0] * 950)
    return y_true, y_score


@pytest.fixture
def random_data():
    """Random classifier: uniform random scores, ~5% prevalence."""
    rng = np.random.RandomState(42)
    y_true = np.zeros(1000, dtype=int)
    y_true[:50] = 1
    rng.shuffle(y_true)
    y_score = rng.uniform(0, 1, 1000)
    return y_true, y_score


@pytest.fixture
def good_data():
    """Good classifier: actives biased high, inactives biased low."""
    rng = np.random.RandomState(42)
    y_true = np.array([1] * 100 + [0] * 900)
    y_score = np.concatenate([
        rng.beta(5, 2, 100),   # actives: skewed high
        rng.beta(2, 5, 900),   # inactives: skewed low
    ])
    return y_true, y_score


# ------------------------------------------------------------------
# Input validation
# ------------------------------------------------------------------

class TestInputValidation:
    def test_mismatched_lengths(self):
        with pytest.raises(ValueError, match="same length"):
            auroc(np.array([0, 1]), np.array([0.5]))

    def test_too_few_samples(self):
        with pytest.raises(ValueError, match="at least 2"):
            auroc(np.array([1]), np.array([0.5]))

    def test_non_binary_labels(self):
        with pytest.raises(ValueError, match="only 0 and 1"):
            auroc(np.array([0, 1, 2]), np.array([0.1, 0.5, 0.9]))

    def test_nan_in_scores(self):
        with pytest.raises(ValueError, match="NaN or Inf"):
            auroc(np.array([0, 1]), np.array([0.5, float("nan")]))

    def test_inf_in_scores(self):
        with pytest.raises(ValueError, match="NaN or Inf"):
            auroc(np.array([0, 1]), np.array([0.5, float("inf")]))

    def test_accepts_lists(self):
        """Python lists should be accepted (not just numpy arrays)."""
        result = auroc([0, 0, 1, 1], [0.1, 0.2, 0.8, 0.9])
        assert result == 1.0

    def test_accepts_pandas_series(self):
        """Pandas Series should be accepted."""
        import pandas as pd
        y_true = pd.Series([0, 0, 1, 1])
        y_score = pd.Series([0.1, 0.2, 0.8, 0.9])
        result = auroc(y_true, y_score)
        assert result == 1.0


# ------------------------------------------------------------------
# AUROC
# ------------------------------------------------------------------

class TestAUROC:
    def test_perfect(self, perfect_data):
        y_true, y_score = perfect_data
        assert auroc(y_true, y_score) == 1.0

    def test_random_near_half(self, random_data):
        y_true, y_score = random_data
        result = auroc(y_true, y_score)
        assert 0.3 < result < 0.7  # random should be around 0.5

    def test_single_class_returns_nan(self):
        y_true = np.zeros(100)
        y_score = np.random.RandomState(0).uniform(0, 1, 100)
        with pytest.warns(match="only one class"):
            result = auroc(y_true, y_score)
        assert np.isnan(result)


# ------------------------------------------------------------------
# AUPRC
# ------------------------------------------------------------------

class TestAUPRC:
    def test_perfect(self, perfect_data):
        y_true, y_score = perfect_data
        assert auprc(y_true, y_score) == 1.0

    def test_good_better_than_random(self, good_data):
        y_true, y_score = good_data
        result = auprc(y_true, y_score)
        random_baseline = y_true.sum() / len(y_true)  # = 0.1
        assert result > random_baseline

    def test_single_class_returns_nan(self):
        y_true = np.ones(100)
        y_score = np.random.RandomState(0).uniform(0, 1, 100)
        with pytest.warns(match="only one class"):
            result = auprc(y_true, y_score)
        assert np.isnan(result)


# ------------------------------------------------------------------
# MCC
# ------------------------------------------------------------------

class TestMCC:
    def test_perfect(self, perfect_data):
        y_true, y_score = perfect_data
        assert mcc(y_true, y_score) == 1.0

    def test_random_near_zero(self, random_data):
        y_true, y_score = random_data
        result = mcc(y_true, y_score)
        assert -0.2 < result < 0.2  # random should be near 0

    def test_custom_threshold(self):
        y_true = np.array([0, 0, 1, 1])
        y_score = np.array([0.1, 0.3, 0.6, 0.8])
        # Threshold 0.5: pred=[0,0,1,1] → MCC=1.0
        assert mcc(y_true, y_score, threshold=0.5) == 1.0
        # Threshold 0.05: pred=[1,1,1,1] → all same class → NaN
        with pytest.warns(match="all predictions are the same"):
            result = mcc(y_true, y_score, threshold=0.05)
        assert np.isnan(result)

    def test_single_class_returns_nan(self):
        y_true = np.zeros(100)
        y_score = np.random.RandomState(0).uniform(0, 1, 100)
        with pytest.warns(match="only one class"):
            result = mcc(y_true, y_score)
        assert np.isnan(result)


# ------------------------------------------------------------------
# LogAUC
# ------------------------------------------------------------------

class TestLogAUC:
    def test_perfect_near_one(self, perfect_data):
        """Perfect classifier should have LogAUC close to 1.0."""
        y_true, y_score = perfect_data
        result = log_auc(y_true, y_score)
        assert result > 0.99

    def test_random_baseline(self, random_data):
        """Random classifier should be near the random baseline."""
        y_true, y_score = random_data
        result = log_auc(y_true, y_score)
        # Random baseline for [0.001, 0.1] is ~0.0215
        assert 0.0 < result < 0.15

    def test_good_between(self, good_data):
        """Good classifier between random baseline and perfect."""
        y_true, y_score = good_data
        result = log_auc(y_true, y_score)
        assert 0.3 < result < 1.0

    def test_custom_fpr_range(self, good_data):
        """Different FPR range should give different value."""
        y_true, y_score = good_data
        r1 = log_auc(y_true, y_score, fpr_range=(0.001, 0.1))
        r2 = log_auc(y_true, y_score, fpr_range=(0.01, 0.5))
        assert r1 != r2

    def test_single_class_returns_nan(self):
        y_true = np.zeros(100)
        y_score = np.random.RandomState(0).uniform(0, 1, 100)
        with pytest.warns(match="only one class"):
            result = log_auc(y_true, y_score)
        assert np.isnan(result)

    def test_monotonicity_with_quality(self):
        """Better classifier should have higher LogAUC."""
        rng = np.random.RandomState(123)
        y_true = np.array([1] * 50 + [0] * 950)

        # Good: actives scored high
        y_good = np.concatenate([rng.beta(8, 2, 50), rng.beta(2, 8, 950)])
        # Weak: slight separation
        y_weak = np.concatenate([rng.beta(3, 2, 50), rng.beta(2, 3, 950)])

        assert log_auc(y_true, y_good) > log_auc(y_true, y_weak)

    def test_normalized_range(self, good_data):
        """LogAUC should be in [0, 1] for valid inputs."""
        y_true, y_score = good_data
        result = log_auc(y_true, y_score)
        assert 0.0 <= result <= 1.0

    def test_worst_classifier_below_random(self):
        """Worst classifier (actives at bottom) should score below random baseline."""
        rng = np.random.RandomState(99)
        y_true = np.array([1] * 50 + [0] * 950)
        # Reverse ranking: inactives get high scores, actives get low scores
        y_score = np.concatenate([rng.uniform(0.0, 0.3, 50), rng.uniform(0.7, 1.0, 950)])
        result = log_auc(y_true, y_score)
        # Random baseline ~0.0215; worst classifier should be well below
        assert result < 0.01


# ------------------------------------------------------------------
# BEDROC
# ------------------------------------------------------------------

class TestBEDROC:
    def test_perfect_near_one(self, perfect_data):
        """Perfect classifier should have BEDROC close to 1.0."""
        y_true, y_score = perfect_data
        result = bedroc(y_true, y_score, alpha=20.0)
        assert result > 0.99

    def test_random_near_ra(self, random_data):
        """Random classifier BEDROC should be near active fraction (ra)."""
        y_true, y_score = random_data
        ra = y_true.sum() / len(y_true)  # 0.05
        result = bedroc(y_true, y_score, alpha=20.0)
        # Random should be roughly near ra, allow wide margin
        assert 0.0 < result < 0.3

    def test_good_between(self, good_data):
        """Good classifier between random and perfect."""
        y_true, y_score = good_data
        result = bedroc(y_true, y_score, alpha=20.0)
        ra = y_true.sum() / len(y_true)
        assert ra < result < 1.0

    def test_different_alpha(self, good_data):
        """Higher alpha emphasizes top ranks more."""
        y_true, y_score = good_data
        b20 = bedroc(y_true, y_score, alpha=20.0)
        b80 = bedroc(y_true, y_score, alpha=80.0)
        # Both should be valid BEDROC scores
        assert 0.0 < b20 <= 1.0
        assert 0.0 < b80 <= 1.0

    def test_no_actives_returns_nan(self):
        y_true = np.zeros(100)
        y_score = np.random.RandomState(0).uniform(0, 1, 100)
        with pytest.warns(match="no actives"):
            result = bedroc(y_true, y_score)
        assert np.isnan(result)

    def test_all_actives_returns_one(self):
        """When all samples are active, BEDROC should be 1.0."""
        y_true = np.ones(100)
        y_score = np.random.RandomState(0).uniform(0, 1, 100)
        result = bedroc(y_true, y_score)
        assert result == 1.0

    def test_accepts_lists(self):
        result = bedroc([1, 0, 0, 0, 0], [0.9, 0.1, 0.2, 0.3, 0.4])
        assert 0.0 < result <= 1.0


# ------------------------------------------------------------------
# Cross-validation with hand-computed values
# ------------------------------------------------------------------

class TestCrossValidation:
    def test_bedroc_actives_at_top(self):
        """Actives ranked at top should give high BEDROC."""
        # 2 actives at ranks 1 and 2 out of 10
        y_true = np.array([1, 1, 0, 0, 0, 0, 0, 0, 0, 0])
        y_score = np.array([1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1])
        result = bedroc(y_true, y_score, alpha=20.0)
        assert result > 0.8  # actives at top → high BEDROC

    def test_bedroc_actives_at_bottom(self):
        """Actives ranked at bottom should give low BEDROC."""
        y_true = np.array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1])
        y_score = np.array([1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1])
        result = bedroc(y_true, y_score, alpha=20.0)
        assert result < 0.2  # actives at bottom → low BEDROC

    def test_log_auc_perfect_small(self):
        """Small perfect classifier: LogAUC should be 1.0."""
        # 5 actives scored [1.0, 0.9, 0.8, 0.7, 0.6]
        # 95 inactives scored [0.0-0.5] range
        y_true = np.array([1] * 5 + [0] * 95)
        y_score = np.concatenate([
            np.linspace(1.0, 0.6, 5),
            np.linspace(0.5, 0.0, 95),
        ])
        result = log_auc(y_true, y_score)
        assert result > 0.95  # near-perfect separation


# ------------------------------------------------------------------
# Enrichment Factor
# ------------------------------------------------------------------

class TestEnrichmentFactor:
    def test_perfect_ef1(self, perfect_data):
        """Perfect classifier: all 50 actives in top 1% (=10 slots).
        EF@1% = (10/10) / (50/1000) = 1.0/0.05 = 20.0"""
        y_true, y_score = perfect_data
        result = enrichment_factor(y_true, y_score, percentage=1.0)
        assert result == 20.0

    def test_perfect_ef5(self, perfect_data):
        """Perfect classifier: all 50 actives in top 5% (=50 slots).
        EF@5% = (50/50) / (50/1000) = 1.0/0.05 = 20.0"""
        y_true, y_score = perfect_data
        result = enrichment_factor(y_true, y_score, percentage=5.0)
        assert result == 20.0

    def test_random_near_one(self, random_data):
        """Random classifier EF should be near 1.0."""
        y_true, y_score = random_data
        result = enrichment_factor(y_true, y_score, percentage=5.0)
        assert 0.0 < result < 3.0  # allow variance

    def test_ef100_always_one(self, good_data):
        """EF@100% is always exactly 1.0."""
        y_true, y_score = good_data
        result = enrichment_factor(y_true, y_score, percentage=100.0)
        assert result == pytest.approx(1.0)

    def test_no_actives_returns_nan(self):
        y_true = np.zeros(100)
        y_score = np.random.RandomState(0).uniform(0, 1, 100)
        with pytest.warns(match="no actives"):
            result = enrichment_factor(y_true, y_score)
        assert np.isnan(result)

    def test_ef_monotonicity(self, good_data):
        """For a good classifier, EF@1% >= EF@5% >= EF@10%."""
        y_true, y_score = good_data
        ef1 = enrichment_factor(y_true, y_score, percentage=1.0)
        ef5 = enrichment_factor(y_true, y_score, percentage=5.0)
        ef10 = enrichment_factor(y_true, y_score, percentage=10.0)
        # Good classifiers should concentrate actives at the top
        assert ef1 >= ef5 >= ef10

    def test_all_same_score(self):
        """When all scores are identical, EF should be near 1.0."""
        y_true = np.array([1] * 10 + [0] * 90)
        y_score = np.full(100, 0.5)
        result = enrichment_factor(y_true, y_score, percentage=10.0)
        assert result == pytest.approx(1.0)

    def test_invalid_percentage(self):
        with pytest.raises(ValueError, match="percentage must be in"):
            enrichment_factor(np.array([0, 1]), np.array([0.1, 0.9]), percentage=0.0)
        with pytest.raises(ValueError, match="percentage must be in"):
            enrichment_factor(np.array([0, 1]), np.array([0.1, 0.9]), percentage=-5.0)


# ------------------------------------------------------------------
# Additional edge cases (from 3-agent review)
# ------------------------------------------------------------------

class TestLogAUCEdgeCases:
    def test_all_same_score(self):
        """All same scores → near random baseline (~0.0215)."""
        y_true = np.array([1] * 50 + [0] * 950)
        y_score = np.full(1000, 0.5)
        result = log_auc(y_true, y_score)
        assert 0.0 < result < 0.1

    def test_invalid_fpr_range_zero(self):
        with pytest.raises(ValueError, match="lower bound must be > 0"):
            log_auc(np.array([0, 1]), np.array([0.1, 0.9]), fpr_range=(0.0, 1.0))

    def test_invalid_fpr_range_inverted(self):
        with pytest.raises(ValueError, match="lower must be < upper"):
            log_auc(np.array([0, 1]), np.array([0.1, 0.9]), fpr_range=(0.5, 0.1))

    def test_invalid_fpr_range_upper_gt_one(self):
        with pytest.raises(ValueError, match="upper bound must be <= 1.0"):
            log_auc(np.array([0, 1]), np.array([0.1, 0.9]), fpr_range=(0.001, 1.5))


class TestBEDROCEdgeCases:
    def test_all_same_score(self):
        """All same scores → BEDROC near ra (random)."""
        y_true = np.array([1] * 50 + [0] * 950)
        y_score = np.full(1000, 0.5)
        result = bedroc(y_true, y_score)
        ra = 50 / 1000  # 0.05
        assert result == pytest.approx(ra, abs=0.25)

    def test_invalid_alpha(self):
        with pytest.raises(ValueError, match="alpha must be > 0"):
            bedroc(np.array([0, 1]), np.array([0.1, 0.9]), alpha=0.0)
        with pytest.raises(ValueError, match="alpha must be > 0"):
            bedroc(np.array([0, 1]), np.array([0.1, 0.9]), alpha=-5.0)

    def test_extreme_alpha_large(self):
        """Very large alpha should not crash (numerical stability)."""
        y_true = np.array([1] * 10 + [0] * 90)
        rng = np.random.RandomState(42)
        y_score = np.concatenate([rng.beta(5, 2, 10), rng.beta(2, 5, 90)])
        result = bedroc(y_true, y_score, alpha=500.0)
        assert 0.0 <= result <= 1.0

    def test_extreme_alpha_1500(self):
        """alpha=1500 previously caused sinh/cosh overflow → NaN."""
        y_true = np.array([1] * 10 + [0] * 90)
        rng = np.random.RandomState(42)
        y_score = np.concatenate([rng.beta(5, 2, 10), rng.beta(2, 5, 90)])
        result = bedroc(y_true, y_score, alpha=1500.0)
        assert np.isfinite(result)
        assert 0.0 <= result <= 1.0

    def test_small_alpha_no_crash(self):
        """Very small alpha (1e-20) is numerically degenerate (formula is 0/0
        as alpha→0). Should return NaN with UserWarning, not crash or produce -1e15."""
        y_true = np.array([1] * 10 + [0] * 90)
        y_score = np.concatenate([np.ones(10), np.zeros(90)])
        with pytest.warns(UserWarning, match="non-finite"):
            result = bedroc(y_true, y_score, alpha=1e-20)
        assert np.isnan(result)

    def test_exactly_one_active(self):
        """BEDROC with exactly 1 active should compute without error."""
        rng = np.random.RandomState(7)
        y_true = np.zeros(100, dtype=int)
        y_true[0] = 1  # 1 active
        y_score = rng.uniform(0, 1, 100)
        y_score[0] = 1.0  # active ranked first
        result = bedroc(y_true, y_score, alpha=20.0)
        assert np.isfinite(result)
        assert 0.0 <= result <= 1.0
        # Active is ranked first → high BEDROC
        assert result > 0.5


# ------------------------------------------------------------------
# Convenience functions
# ------------------------------------------------------------------

class TestComputeAllMetrics:
    def test_returns_all_keys(self, good_data):
        y_true, y_score = good_data
        result = compute_all_metrics(y_true, y_score)
        expected_keys = {"auroc", "auprc", "mcc", "log_auc", "bedroc", "ef_1pct", "ef_5pct"}
        assert set(result.keys()) == expected_keys

    def test_values_in_range(self, good_data):
        y_true, y_score = good_data
        result = compute_all_metrics(y_true, y_score)
        assert 0.0 <= result["auroc"] <= 1.0
        assert 0.0 <= result["auprc"] <= 1.0
        assert 0.0 <= result["log_auc"] <= 1.0
        assert 0.0 <= result["bedroc"] <= 1.0
        assert result["ef_1pct"] >= 0.0
        assert result["ef_5pct"] >= 0.0

    def test_matches_individual(self, good_data):
        y_true, y_score = good_data
        result = compute_all_metrics(y_true, y_score)
        assert result["auroc"] == auroc(y_true, y_score)
        assert result["auprc"] == auprc(y_true, y_score)
        assert result["log_auc"] == log_auc(y_true, y_score)
        assert result["bedroc"] == bedroc(y_true, y_score)


class TestSummarizeRuns:
    def test_mean_std(self):
        runs = [
            {"auroc": 0.8, "auprc": 0.6},
            {"auroc": 0.9, "auprc": 0.7},
            {"auroc": 0.85, "auprc": 0.65},
        ]
        result = summarize_runs(runs)
        assert result["auroc"]["mean"] == pytest.approx(0.85, abs=1e-10)
        assert result["auprc"]["mean"] == pytest.approx(0.65, abs=1e-10)
        assert result["auroc"]["std"] > 0

    def test_single_run_zero_std(self):
        runs = [{"auroc": 0.8, "auprc": 0.6}]
        result = summarize_runs(runs)
        assert result["auroc"]["mean"] == 0.8
        assert result["auroc"]["std"] == 0.0

    def test_empty_raises(self):
        with pytest.raises(ValueError, match="non-empty"):
            summarize_runs([])


class TestSaveResults:
    def test_json_roundtrip(self, tmp_path):
        import json
        metrics = {"auroc": 0.85, "auprc": 0.7, "mcc": float("nan")}
        path = tmp_path / "results.json"
        save_results(metrics, path, format="json")
        with open(path) as f:
            loaded = json.load(f)
        assert loaded["auroc"] == 0.85
        assert loaded["mcc"] is None  # NaN → null in JSON

    def test_csv_output(self, tmp_path):
        import pandas as pd
        metrics = [
            {"auroc": 0.85, "auprc": 0.7},
            {"auroc": 0.90, "auprc": 0.75},
        ]
        path = tmp_path / "results.csv"
        save_results(metrics, path, format="csv")
        df = pd.read_csv(path)
        assert len(df) == 2
        assert list(df.columns) == ["auroc", "auprc"]

    def test_invalid_format(self, tmp_path):
        with pytest.raises(ValueError, match="format must be"):
            save_results({}, tmp_path / "x.txt", format="xml")

    def test_csv_nested_dict_flattens(self, tmp_path):
        """summarize_runs output (nested dict) should flatten to CSV columns."""
        import pandas as pd
        nested = {"auroc": {"mean": 0.85, "std": 0.01}, "auprc": {"mean": 0.7, "std": 0.02}}
        path = tmp_path / "summary.csv"
        save_results(nested, path, format="csv")
        df = pd.read_csv(path)
        assert "auroc_mean" in df.columns
        assert "auprc_std" in df.columns
        assert df["auroc_mean"].iloc[0] == pytest.approx(0.85)

    def test_csv_list_of_nested_dicts_flattens(self, tmp_path):
        """List of nested dicts should also flatten for CSV."""
        import pandas as pd
        nested_list = [
            {"auroc": {"mean": 0.85, "std": 0.01}},
            {"auroc": {"mean": 0.90, "std": 0.02}},
        ]
        path = tmp_path / "list_nested.csv"
        save_results(nested_list, path, format="csv")
        df = pd.read_csv(path)
        assert "auroc_mean" in df.columns
        assert len(df) == 2
        assert df["auroc_mean"].iloc[1] == pytest.approx(0.90)


# ------------------------------------------------------------------
# evaluate_splits
# ------------------------------------------------------------------

class TestEvaluateSplits:
    def test_per_fold_keys(self):
        """Each fold should have all 7 metric keys with finite values."""
        rng = np.random.RandomState(42)
        y_true = np.array([1] * 30 + [0] * 70)
        y_score = np.concatenate([rng.beta(5, 2, 30), rng.beta(2, 5, 70)])
        # Shuffle so both folds get actives
        idx = rng.permutation(100)
        y_true, y_score = y_true[idx], y_score[idx]
        splits = np.array(["train"] * 50 + ["test"] * 50)
        result = evaluate_splits(y_true, y_score, splits)
        assert set(result.keys()) == {"train", "test"}
        for fold_metrics in result.values():
            expected_keys = {"auroc", "auprc", "mcc", "log_auc", "bedroc", "ef_1pct", "ef_5pct"}
            assert set(fold_metrics.keys()) == expected_keys
            assert np.isfinite(fold_metrics["auroc"])

    def test_fold_independence(self):
        """Each fold result should match independent compute_all_metrics."""
        rng = np.random.RandomState(42)
        y_true = np.array([1] * 30 + [0] * 70)
        y_score = np.concatenate([rng.beta(5, 2, 30), rng.beta(2, 5, 70)])
        idx = rng.permutation(100)
        y_true, y_score = y_true[idx], y_score[idx]
        splits = np.array(["a"] * 50 + ["b"] * 50)
        result = evaluate_splits(y_true, y_score, splits)
        # Verify each fold matches direct computation
        for fold, sl in [("a", slice(0, 50)), ("b", slice(50, 100))]:
            expected = compute_all_metrics(y_true[sl], y_score[sl])
            for key in expected:
                assert result[fold][key] == pytest.approx(expected[key], nan_ok=True)

    def test_mismatched_split_labels_length(self):
        with pytest.raises(ValueError, match="same length"):
            evaluate_splits(np.array([0, 1, 0]), np.array([0.1, 0.9, 0.5]),
                            np.array(["a", "b"]))

    def test_degenerate_fold_returns_nan(self):
        """Fold with <2 samples should return all NaN, not crash."""
        y_true = np.array([0, 1, 0, 1])
        y_score = np.array([0.1, 0.9, 0.2, 0.8])
        splits = np.array(["a", "a", "a", "b"])  # fold "b" has 1 sample
        with pytest.warns(match="fold 'b' has 1 sample"):
            result = evaluate_splits(y_true, y_score, splits)
        assert all(np.isnan(v) for v in result["b"].values())

    def test_fold_with_zero_actives_returns_nan(self):
        """Fold with ≥2 samples but 0 actives should return all NaN."""
        rng = np.random.RandomState(42)
        y_true = np.array([1] * 10 + [0] * 90)
        y_score = rng.uniform(0, 1, 100)
        # fold "a": 10 actives + 40 inactives; fold "b": 0 actives + 50 inactives
        splits = np.array(["a"] * 50 + ["b"] * 50)
        with pytest.warns(match="only one class"):
            result = evaluate_splits(y_true, y_score, splits)
        assert all(np.isnan(v) for v in result["b"].values())
        # fold "a" should still compute normally
        assert np.isfinite(result["a"]["auroc"])

    def test_integer_fold_labels(self):
        """evaluate_splits should accept integer fold labels (converted to str keys)."""
        rng = np.random.RandomState(42)
        y_true = np.array([1] * 30 + [0] * 70)
        y_score = np.concatenate([rng.beta(5, 2, 30), rng.beta(2, 5, 70)])
        idx = rng.permutation(100)
        y_true, y_score = y_true[idx], y_score[idx]
        splits = np.array([0] * 50 + [1] * 50)
        result = evaluate_splits(y_true, y_score, splits)
        assert "0" in result and "1" in result


# ------------------------------------------------------------------
# Additional review-driven tests
# ------------------------------------------------------------------

class TestReviewFindings:
    def test_compute_all_single_class(self):
        """All-zeros y_true → all NaN, no crash."""
        y_true = np.zeros(100)
        y_score = np.random.RandomState(0).uniform(0, 1, 100)
        with pytest.warns(UserWarning):
            result = compute_all_metrics(y_true, y_score)
        assert len(result) == 7
        assert all(np.isnan(v) for v in result.values())

    def test_summarize_runs_with_nan(self):
        """NaN values in some runs should be skipped by nanmean."""
        runs = [
            {"auroc": 0.8, "auprc": 0.6},
            {"auroc": float("nan"), "auprc": 0.7},
            {"auroc": 0.9, "auprc": float("nan")},
        ]
        result = summarize_runs(runs)
        assert result["auroc"]["mean"] == pytest.approx(0.85)
        assert result["auprc"]["mean"] == pytest.approx(0.65)

    def test_summarize_runs_inconsistent_keys(self):
        """Inconsistent keys should raise ValueError."""
        runs = [{"auroc": 0.8, "auprc": 0.6}, {"auroc": 0.9}]
        with pytest.raises(ValueError, match="inconsistent keys"):
            summarize_runs(runs)


class TestEndToEnd:
    def test_pipeline_json(self, tmp_path, good_data):
        """compute_all_metrics -> summarize_runs -> save_results (JSON)."""
        import json
        y_true, y_score = good_data
        runs = [compute_all_metrics(y_true, y_score + offset)
                for offset in [0.0, 0.01, -0.01]]
        summary = summarize_runs(runs)
        path = tmp_path / "pipeline.json"
        save_results(summary, path, format="json")
        with open(path) as f:
            loaded = json.load(f)
        assert "auroc" in loaded
        assert "mean" in loaded["auroc"]
        assert isinstance(loaded["auroc"]["mean"], float)

    def test_pipeline_csv(self, tmp_path, good_data):
        """compute_all_metrics -> summarize_runs -> save_results (CSV)."""
        import pandas as pd
        y_true, y_score = good_data
        runs = [compute_all_metrics(y_true, y_score)]
        summary = summarize_runs(runs)
        path = tmp_path / "pipeline.csv"
        save_results(summary, path, format="csv")
        df = pd.read_csv(path)
        assert "auroc_mean" in df.columns
        assert "auroc_std" in df.columns

    def test_save_np_integer(self, tmp_path):
        """save_results should handle np.integer values in JSON."""
        import json
        metrics = {"n_samples": np.int64(1000), "auroc": np.float64(0.85)}
        path = tmp_path / "np_types.json"
        save_results(metrics, path, format="json")
        with open(path) as f:
            loaded = json.load(f)
        assert loaded["n_samples"] == 1000
        assert isinstance(loaded["n_samples"], int)