File size: 36,590 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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
"""Regression tests for experiment orchestration scripts."""

from __future__ import annotations

import importlib.util
import json
import os
import sys
import time
import types
from pathlib import Path

import pandas as pd
import pytest

from negbiodb.db import connect, create_database, refresh_all_pairs
from negbiodb.export import _resolve_split_id, export_negative_dataset

ROOT = Path(__file__).resolve().parent.parent
MIGRATIONS_DIR = ROOT / "migrations"


def _load_script_module(name: str, rel_path: str):
    path = ROOT / rel_path
    spec = importlib.util.spec_from_file_location(name, path)
    module = importlib.util.module_from_spec(spec)
    assert spec is not None and spec.loader is not None
    spec.loader.exec_module(module)
    return module


@pytest.fixture
def migrated_db(tmp_path):
    db_path = tmp_path / "test.db"
    create_database(db_path, MIGRATIONS_DIR)
    return db_path


def _populate_small_db(conn, n_compounds=3, n_targets=2):
    for i in range(1, n_compounds + 1):
        conn.execute(
            """INSERT INTO compounds
            (canonical_smiles, inchikey, inchikey_connectivity)
            VALUES (?, ?, ?)""",
            (f"C{i}", f"KEY{i:011d}SA-N", f"KEY{i:011d}"),
        )
    for j in range(1, n_targets + 1):
        conn.execute(
            "INSERT INTO targets (uniprot_accession, amino_acid_sequence) VALUES (?, ?)",
            (f"P{j:05d}", f"SEQ{j}"),
        )
    for i in range(1, n_compounds + 1):
        for j in range(1, n_targets + 1):
            conn.execute(
                """INSERT INTO negative_results
                (compound_id, target_id, result_type, confidence_tier,
                 activity_type, activity_value, activity_unit,
                 inactivity_threshold, source_db, source_record_id,
                 extraction_method, publication_year)
                VALUES (?, ?, 'hard_negative', 'silver',
                        'IC50', 20000.0, 'nM',
                        10000.0, 'chembl', ?, 'database_direct', ?)""",
                (i, j, f"C:{i}:{j}", 2015 + i),
            )
    refresh_all_pairs(conn)
    conn.commit()


class TestExportVersioning:
    def test_export_uses_latest_split_without_row_duplication(self, migrated_db, tmp_path):
        with connect(migrated_db) as conn:
            _populate_small_db(conn)
            pair_ids = [row[0] for row in conn.execute(
                "SELECT pair_id FROM compound_target_pairs ORDER BY pair_id"
            )]
            conn.execute(
                """INSERT INTO split_definitions
                (split_name, split_strategy, random_seed, train_ratio, val_ratio, test_ratio)
                VALUES ('random_v1', 'random', 1, 0.7, 0.1, 0.2)"""
            )
            old_id = conn.execute(
                "SELECT split_id FROM split_definitions WHERE split_name = 'random_v1'"
            ).fetchone()[0]
            conn.executemany(
                "INSERT INTO split_assignments (pair_id, split_id, fold) VALUES (?, ?, 'train')",
                [(pair_id, old_id) for pair_id in pair_ids],
            )

            conn.execute(
                """INSERT INTO split_definitions
                (split_name, split_strategy, random_seed, train_ratio, val_ratio, test_ratio)
                VALUES ('random_v2', 'random', 2, 0.7, 0.1, 0.2)"""
            )
            new_id = conn.execute(
                "SELECT split_id FROM split_definitions WHERE split_name = 'random_v2'"
            ).fetchone()[0]
            conn.executemany(
                "INSERT INTO split_assignments (pair_id, split_id, fold) VALUES (?, ?, 'val')",
                [(pair_id, new_id) for pair_id in pair_ids],
            )
            conn.commit()

        result = export_negative_dataset(migrated_db, tmp_path / "exports", split_strategies=["random"])
        df = pd.read_parquet(result["parquet_path"])

        assert len(df) == len(pair_ids)
        assert set(df["split_random"]) == {"val"}

    def test_split_resolution_prefers_version_suffix_over_insert_order(self, migrated_db):
        with connect(migrated_db) as conn:
            conn.execute(
                """INSERT INTO split_definitions
                (split_name, split_strategy, version, random_seed, train_ratio, val_ratio, test_ratio)
                VALUES ('random_v2', 'random', '2.0', 2, 0.7, 0.1, 0.2)"""
            )
            newer_semantic_id = conn.execute(
                "SELECT split_id FROM split_definitions WHERE split_name = 'random_v2'"
            ).fetchone()[0]
            conn.execute(
                """INSERT INTO split_definitions
                (split_name, split_strategy, version, random_seed, train_ratio, val_ratio, test_ratio)
                VALUES ('random_backfill', 'random', '1.0', 1, 0.7, 0.1, 0.2)"""
            )
            conn.commit()

            assert _resolve_split_id(conn, "random") == newer_semantic_id


class TestPrepareExpData:
    def test_skip_exp4_does_not_require_pairs_parquet(self, tmp_path):
        module = _load_script_module("prepare_exp_data_test", "scripts/prepare_exp_data.py")

        data_dir = tmp_path / "exports"
        data_dir.mkdir()
        db_path = tmp_path / "negbiodb.db"
        db_path.write_text("")
        pd.DataFrame({"smiles": ["CC"], "inchikey": ["A" * 27], "uniprot_id": ["P00001"], "target_sequence": ["SEQ"], "Y": [1], "split_random": ["train"]}).to_parquet(data_dir / "negbiodb_m1_balanced.parquet")
        pd.DataFrame({"smiles": ["CC"], "inchikey": ["A" * 27], "uniprot_id": ["P00001"], "target_sequence": ["SEQ"]}).to_parquet(data_dir / "chembl_positives_pchembl6.parquet")

        rc = module.main([
            "--data-dir", str(data_dir),
            "--db", str(db_path),
            "--skip-exp1",
            "--skip-exp4",
        ])

        assert rc == 0

    def test_exp4_only_does_not_require_db_or_positives_and_builds_full_task_ddb(self, tmp_path):
        module = _load_script_module("prepare_exp_data_test_exp4_only", "scripts/prepare_exp_data.py")

        data_dir = tmp_path / "exports"
        data_dir.mkdir()
        rows = []
        for i in range(10):
            rows.append({
                "smiles": f"P{i}",
                "inchikey": f"POS{i:011d}ABCDEFGHIJKLM",
                "uniprot_id": "P00001",
                "target_sequence": "SEQ",
                "Y": 1,
                "split_random": "train",
                "split_cold_compound": "train",
                "split_cold_target": "train",
            })
        for i in range(10):
            rows.append({
                "smiles": f"N{i}",
                "inchikey": f"NEG{i:011d}ABCDEFGHIJKLM",
                "uniprot_id": "P00002" if i < 5 else "P00003",
                "target_sequence": "SEQ",
                "Y": 0,
                "split_random": "test",
                "split_cold_compound": "test",
                "split_cold_target": "test",
            })
        pd.DataFrame(rows).to_parquet(data_dir / "negbiodb_m1_balanced.parquet")

        rc = module.main([
            "--data-dir", str(data_dir),
            "--db", str(tmp_path / "missing.db"),
            "--skip-exp1",
        ])

        assert rc == 0
        ddb = pd.read_parquet(data_dir / "negbiodb_m1_balanced_ddb.parquet")
        pos = ddb[ddb["Y"] == 1]
        assert "split_degree_balanced" in ddb.columns
        assert set(pos["split_degree_balanced"]) != {"train"}


class TestTrainBaselineHelpers:
    def test_build_run_name_includes_dataset_and_seed(self):
        module = _load_script_module("train_baseline_test_names", "scripts/train_baseline.py")
        run_name = module._build_run_name("deepdta", "balanced", "random", "negbiodb", 7)
        assert run_name == "deepdta_balanced_random_negbiodb_seed7"

    def test_resolve_dataset_file_rejects_invalid_ddb_combo(self):
        module = _load_script_module("train_baseline_test_resolve", "scripts/train_baseline.py")
        assert module._resolve_dataset_file("balanced", "ddb", "uniform_random") is None
        assert module._resolve_dataset_file("balanced", "ddb", "negbiodb") == "negbiodb_m1_balanced_ddb.parquet"
        assert module._resolve_dataset_file("realistic", "random", "uniform_random") is None
        assert module._resolve_dataset_file("realistic", "random", "degree_matched") is None

    def test_prepare_graph_cache_backfills_missing_smiles(self, tmp_path):
        torch = pytest.importorskip("torch")
        module = _load_script_module("train_baseline_test_cache", "scripts/train_baseline.py")

        parquet_path = tmp_path / "m1.parquet"
        pd.DataFrame({"smiles": ["CC", "CCC"]}).to_parquet(parquet_path)
        cache_path = tmp_path / "graph_cache.pt"
        torch.save({"CC": {"graph": "cached"}}, cache_path)

        fake_graph_module = types.SimpleNamespace(
            smiles_to_graph=lambda smiles: {"graph": smiles}
        )
        original = sys.modules.get("negbiodb.models.graphdta")
        sys.modules["negbiodb.models.graphdta"] = fake_graph_module
        try:
            cache = module._prepare_graph_cache(parquet_path, cache_path)
        finally:
            if original is None:
                del sys.modules["negbiodb.models.graphdta"]
            else:
                sys.modules["negbiodb.models.graphdta"] = original

        assert set(cache) == {"CC", "CCC"}
        saved = torch.load(cache_path, weights_only=False)
        assert set(saved) == {"CC", "CCC"}

    def test_main_rejects_realistic_ddb(self, tmp_path):
        module = _load_script_module("train_baseline_test_ddb", "scripts/train_baseline.py")
        rc = module.main([
            "--model", "deepdta",
            "--split", "ddb",
            "--negative", "negbiodb",
            "--dataset", "realistic",
            "--data_dir", str(tmp_path),
            "--output_dir", str(tmp_path / "results"),
        ])
        assert rc == 1

    def test_main_rejects_realistic_uniform_random(self, tmp_path):
        module = _load_script_module("train_baseline_test_realistic_control", "scripts/train_baseline.py")
        rc = module.main([
            "--model", "deepdta",
            "--split", "random",
            "--negative", "uniform_random",
            "--dataset", "realistic",
            "--data_dir", str(tmp_path),
            "--output_dir", str(tmp_path / "results"),
        ])
        assert rc == 1

    def test_main_writes_results_with_dataset_and_seed_in_run_name(self, tmp_path, monkeypatch):
        module = _load_script_module("train_baseline_test_main", "scripts/train_baseline.py")

        data_dir = tmp_path / "exports"
        output_dir = tmp_path / "results"
        data_dir.mkdir()
        pd.DataFrame({
            "smiles": ["CC", "CCC", "CCCC"],
            "target_sequence": ["SEQ", "SEQ", "SEQ"],
            "Y": [1, 0, 0],
            "split_random": ["train", "val", "test"],
        }).to_parquet(data_dir / "negbiodb_m1_balanced.parquet")

        fake_torch = types.SimpleNamespace(
            device=lambda name: name,
            cuda=types.SimpleNamespace(is_available=lambda: False),
            backends=types.SimpleNamespace(mps=types.SimpleNamespace(is_available=lambda: False)),
        )
        original_torch = sys.modules.get("torch")
        sys.modules["torch"] = fake_torch

        class DummyDataset:
            def __init__(self, parquet_path, split_col, fold, model, graph_cache):
                self.fold = fold
                self.items = {"train": [1], "val": [1], "test": [1]}[fold]

            def __len__(self):
                return len(self.items)

        class DummyModel:
            def to(self, device):
                return self

            def parameters(self):
                return []

        monkeypatch.setattr(module, "set_seed", lambda seed: None)
        monkeypatch.setattr(module, "DTIDataset", DummyDataset)
        monkeypatch.setattr(module, "make_dataloader", lambda dataset, batch_size, shuffle, device: [dataset.fold])
        monkeypatch.setattr(module, "build_model", lambda model_type: DummyModel())

        def fake_train(model, train_loader, val_loader, epochs, patience, lr, output_dir, device):
            (output_dir / "best.pt").write_text("checkpoint")
            (output_dir / "training_log.csv").write_text("epoch\n1\n")
            return 0.123

        monkeypatch.setattr(module, "train", fake_train)
        monkeypatch.setattr(module, "evaluate", lambda model, test_loader, checkpoint_path, device: {"log_auc": 0.5})

        try:
            rc = module.main([
                "--model", "deepdta",
                "--split", "random",
                "--negative", "negbiodb",
                "--dataset", "balanced",
                "--seed", "9",
                "--data_dir", str(data_dir),
                "--output_dir", str(output_dir),
            ])
        finally:
            if original_torch is None:
                del sys.modules["torch"]
            else:
                sys.modules["torch"] = original_torch

        assert rc == 0
        run_dir = output_dir / "deepdta_balanced_random_negbiodb_seed9"
        results = json.loads((run_dir / "results.json").read_text())
        assert results["run_name"] == "deepdta_balanced_random_negbiodb_seed9"
        assert results["dataset"] == "balanced"
        assert results["seed"] == 9
        assert (run_dir / "best.pt").exists()

    def test_main_writes_strict_json_with_null_for_nan(self, tmp_path, monkeypatch):
        module = _load_script_module("train_baseline_test_nan_json", "scripts/train_baseline.py")

        data_dir = tmp_path / "exports"
        output_dir = tmp_path / "results"
        data_dir.mkdir()
        pd.DataFrame({
            "smiles": ["CC", "CCC", "CCCC"],
            "target_sequence": ["SEQ", "SEQ", "SEQ"],
            "Y": [1, 0, 0],
            "split_random": ["train", "val", "test"],
        }).to_parquet(data_dir / "negbiodb_m1_balanced.parquet")

        fake_torch = types.SimpleNamespace(
            device=lambda name: name,
            cuda=types.SimpleNamespace(is_available=lambda: False),
            backends=types.SimpleNamespace(mps=types.SimpleNamespace(is_available=lambda: False)),
        )
        original_torch = sys.modules.get("torch")
        sys.modules["torch"] = fake_torch

        class DummyDataset:
            def __init__(self, parquet_path, split_col, fold, model, graph_cache):
                self.fold = fold
                self.items = {"train": [1], "val": [1], "test": [1]}[fold]

            def __len__(self):
                return len(self.items)

        class DummyModel:
            def to(self, device):
                return self

            def parameters(self):
                return []

        monkeypatch.setattr(module, "set_seed", lambda seed: None)
        monkeypatch.setattr(module, "DTIDataset", DummyDataset)
        monkeypatch.setattr(module, "make_dataloader", lambda dataset, batch_size, shuffle, device: [dataset.fold])
        monkeypatch.setattr(module, "build_model", lambda model_type: DummyModel())

        def fake_train(model, train_loader, val_loader, epochs, patience, lr, output_dir, device):
            (output_dir / "best.pt").write_text("checkpoint")
            (output_dir / "training_log.csv").write_text("epoch\n1\n")
            return float("nan")

        monkeypatch.setattr(module, "train", fake_train)
        monkeypatch.setattr(module, "evaluate", lambda model, test_loader, checkpoint_path, device: {"log_auc": float("nan")})

        try:
            rc = module.main([
                "--model", "deepdta",
                "--split", "random",
                "--negative", "negbiodb",
                "--dataset", "balanced",
                "--data_dir", str(data_dir),
                "--output_dir", str(output_dir),
            ])
        finally:
            if original_torch is None:
                del sys.modules["torch"]
            else:
                sys.modules["torch"] = original_torch

        assert rc == 0
        result_text = (output_dir / "deepdta_balanced_random_negbiodb_seed42" / "results.json").read_text()
        assert "\"best_val_log_auc\": null" in result_text
        assert "\"log_auc\": null" in result_text
        assert "NaN" not in result_text


class TestEvalCheckpoint:
    def test_eval_checkpoint_uses_ddb_dataset_and_current_run_name(self, tmp_path, monkeypatch):
        module = _load_script_module("eval_checkpoint_test_main", "scripts/eval_checkpoint.py")

        data_dir = tmp_path / "exports"
        output_dir = tmp_path / "results"
        data_dir.mkdir()
        pd.DataFrame({
            "smiles": ["CC", "CCC", "CCCC"],
            "target_sequence": ["SEQ", "SEQ", "SEQ"],
            "Y": [1, 0, 0],
            "split_degree_balanced": ["train", "val", "test"],
        }).to_parquet(data_dir / "negbiodb_m1_balanced_ddb.parquet")

        run_dir = output_dir / "deepdta_balanced_ddb_negbiodb_seed9"
        run_dir.mkdir(parents=True)
        (run_dir / "best.pt").write_text("checkpoint")
        (run_dir / "training_log.csv").write_text("epoch,val_log_auc\n1,0.7\n")

        fake_torch = types.SimpleNamespace(
            device=lambda name: name,
            cuda=types.SimpleNamespace(is_available=lambda: False),
            backends=types.SimpleNamespace(mps=types.SimpleNamespace(is_available=lambda: False)),
        )
        original_torch = sys.modules.get("torch")
        sys.modules["torch"] = fake_torch

        seen: dict[str, object] = {}

        class DummyDataset:
            def __init__(self, parquet_path, split_col, fold, model, graph_cache):
                seen["parquet_name"] = parquet_path.name
                seen["split_col"] = split_col
                self.fold = fold
                self.items = {"train": [1], "val": [1], "test": [1]}[fold]

            def __len__(self):
                return len(self.items)

        class DummyModel:
            def to(self, device):
                seen["device"] = device
                return self

        monkeypatch.setattr(module.baseline, "DTIDataset", DummyDataset)
        monkeypatch.setattr(module.baseline, "make_dataloader", lambda dataset, batch_size, shuffle, device: [dataset.fold])
        monkeypatch.setattr(module.baseline, "build_model", lambda model_type: DummyModel())

        def fake_evaluate(model, test_loader, checkpoint_path, device):
            seen["checkpoint"] = checkpoint_path
            return {"log_auc": 0.5}

        monkeypatch.setattr(module.baseline, "evaluate", fake_evaluate)

        try:
            rc = module.main([
                "--model", "deepdta",
                "--split", "ddb",
                "--negative", "negbiodb",
                "--dataset", "balanced",
                "--seed", "9",
                "--data_dir", str(data_dir),
                "--output_dir", str(output_dir),
            ])
        finally:
            if original_torch is None:
                del sys.modules["torch"]
            else:
                sys.modules["torch"] = original_torch

        assert rc == 0
        assert seen["parquet_name"] == "negbiodb_m1_balanced_ddb.parquet"
        assert seen["split_col"] == "split_degree_balanced"
        assert seen["checkpoint"] == run_dir / "best.pt"
        results = json.loads((run_dir / "results.json").read_text())
        assert results["run_name"] == "deepdta_balanced_ddb_negbiodb_seed9"
        assert results["best_val_log_auc"] == pytest.approx(0.7)

    def test_eval_checkpoint_rejects_realistic_ddb(self, tmp_path):
        module = _load_script_module("eval_checkpoint_test_ddb", "scripts/eval_checkpoint.py")
        rc = module.main([
            "--model", "deepdta",
            "--split", "ddb",
            "--negative", "negbiodb",
            "--dataset", "realistic",
            "--data_dir", str(tmp_path),
            "--output_dir", str(tmp_path / "results"),
        ])
        assert rc == 1

    def test_eval_checkpoint_falls_back_to_legacy_run_directory(self, tmp_path, monkeypatch):
        module = _load_script_module("eval_checkpoint_test_legacy", "scripts/eval_checkpoint.py")

        data_dir = tmp_path / "exports"
        output_dir = tmp_path / "results"
        data_dir.mkdir()
        pd.DataFrame({
            "smiles": ["CC", "CCC", "CCCC"],
            "target_sequence": ["SEQ", "SEQ", "SEQ"],
            "Y": [1, 0, 0],
            "split_random": ["train", "val", "test"],
        }).to_parquet(data_dir / "negbiodb_m1_balanced.parquet")

        run_dir = output_dir / "deepdta_random_negbiodb"
        run_dir.mkdir(parents=True)
        (run_dir / "best.pt").write_text("checkpoint")
        (run_dir / "training_log.csv").write_text("epoch,val_log_auc\n1,0.8\n")

        fake_torch = types.SimpleNamespace(
            device=lambda name: name,
            cuda=types.SimpleNamespace(is_available=lambda: False),
            backends=types.SimpleNamespace(mps=types.SimpleNamespace(is_available=lambda: False)),
        )
        original_torch = sys.modules.get("torch")
        sys.modules["torch"] = fake_torch

        class DummyDataset:
            def __init__(self, parquet_path, split_col, fold, model, graph_cache):
                self.fold = fold
                self.items = {"train": [1], "val": [1], "test": [1]}[fold]

            def __len__(self):
                return len(self.items)

        class DummyModel:
            def to(self, device):
                return self

        monkeypatch.setattr(module.baseline, "DTIDataset", DummyDataset)
        monkeypatch.setattr(module.baseline, "make_dataloader", lambda dataset, batch_size, shuffle, device: [dataset.fold])
        monkeypatch.setattr(module.baseline, "build_model", lambda model_type: DummyModel())
        monkeypatch.setattr(module.baseline, "evaluate", lambda model, test_loader, checkpoint_path, device: {"log_auc": 0.5})

        try:
            rc = module.main([
                "--model", "deepdta",
                "--split", "random",
                "--negative", "negbiodb",
                "--dataset", "balanced",
                "--seed", "42",
                "--data_dir", str(data_dir),
                "--output_dir", str(output_dir),
            ])
        finally:
            if original_torch is None:
                del sys.modules["torch"]
            else:
                sys.modules["torch"] = original_torch

        assert rc == 0
        results = json.loads((run_dir / "results.json").read_text())
        assert results["run_name"] == "deepdta_random_negbiodb"
        assert results["best_val_log_auc"] == pytest.approx(0.8)


class TestCollectResults:
    def test_load_results_prefers_canonical_run_name_for_duplicate_settings(self, tmp_path):
        module = _load_script_module("collect_results_test_dedup", "scripts/collect_results.py")
        results_base = tmp_path / "results" / "baselines"

        legacy_dir = results_base / "deepdta_random_negbiodb"
        legacy_dir.mkdir(parents=True)
        (legacy_dir / "results.json").write_text(json.dumps({
            "run_name": "deepdta_random_negbiodb",
            "model": "deepdta",
            "split": "random",
            "negative": "negbiodb",
            "dataset": "balanced",
            "seed": 42,
            "n_train": 1,
            "n_val": 1,
            "n_test": 1,
            "best_val_log_auc": 0.1,
            "test_metrics": {"log_auc": 0.1},
        }))

        canonical_dir = results_base / "deepdta_balanced_random_negbiodb_seed42"
        canonical_dir.mkdir(parents=True)
        (canonical_dir / "results.json").write_text(json.dumps({
            "run_name": "deepdta_balanced_random_negbiodb_seed42",
            "model": "deepdta",
            "split": "random",
            "negative": "negbiodb",
            "dataset": "balanced",
            "seed": 42,
            "n_train": 1,
            "n_val": 1,
            "n_test": 1,
            "best_val_log_auc": 0.9,
            "test_metrics": {"log_auc": 0.9},
        }))

        loaded = module.load_results(results_base)
        assert len(loaded) == 1
        assert loaded.iloc[0]["log_auc"] == pytest.approx(0.9)

    def test_load_results_drops_stale_ddb_runs_by_checkpoint_mtime(self, tmp_path):
        module = _load_script_module("collect_results_test_stale_ddb", "scripts/collect_results.py")
        results_base = tmp_path / "results" / "baselines"
        ddb_reference = tmp_path / "exports" / "negbiodb_m1_balanced_ddb.parquet"
        ddb_reference.parent.mkdir(parents=True)
        ddb_reference.write_text("ddb")

        base_time = time.time() - 1000
        os.utime(ddb_reference, (base_time + 100, base_time + 100))

        stale_dir = results_base / "deepdta_balanced_ddb_negbiodb_seed42"
        stale_dir.mkdir(parents=True)
        stale_json = stale_dir / "results.json"
        stale_json.write_text(json.dumps({
            "run_name": "deepdta_balanced_ddb_negbiodb_seed42",
            "model": "deepdta",
            "split": "ddb",
            "negative": "negbiodb",
            "dataset": "balanced",
            "seed": 42,
            "n_train": 1,
            "n_val": 1,
            "n_test": 1,
            "best_val_log_auc": 0.1,
            "test_metrics": {"log_auc": 0.1},
        }))
        stale_best = stale_dir / "best.pt"
        stale_best.write_text("checkpoint")
        os.utime(stale_best, (base_time, base_time))
        os.utime(stale_json, (base_time + 200, base_time + 200))

        fresh_dir = results_base / "deepdta_balanced_ddb_negbiodb_seed43"
        fresh_dir.mkdir(parents=True)
        fresh_json = fresh_dir / "results.json"
        fresh_json.write_text(json.dumps({
            "run_name": "deepdta_balanced_ddb_negbiodb_seed43",
            "model": "deepdta",
            "split": "ddb",
            "negative": "negbiodb",
            "dataset": "balanced",
            "seed": 43,
            "n_train": 1,
            "n_val": 1,
            "n_test": 1,
            "best_val_log_auc": 0.9,
            "test_metrics": {"log_auc": 0.9},
        }))
        fresh_best = fresh_dir / "best.pt"
        fresh_best.write_text("checkpoint")
        os.utime(fresh_best, (base_time + 200, base_time + 200))

        loaded = module.load_results(results_base, ddb_reference=ddb_reference)
        assert len(loaded) == 1
        assert int(loaded.iloc[0]["seed"]) == 43
        assert loaded.iloc[0]["log_auc"] == pytest.approx(0.9)

    def test_filter_results_by_dataset_and_seed(self):
        module = _load_script_module("collect_results_test_filter", "scripts/collect_results.py")
        df = pd.DataFrame([
            {"model": "deepdta", "dataset": "balanced", "seed": 1, "split": "random", "negative": "negbiodb"},
            {"model": "deepdta", "dataset": "balanced", "seed": 2, "split": "random", "negative": "negbiodb"},
            {"model": "deepdta", "dataset": "realistic", "seed": 1, "split": "random", "negative": "negbiodb"},
        ])

        filtered = module.filter_results(df, dataset="balanced", seeds=[2])
        assert len(filtered) == 1
        assert filtered.iloc[0]["dataset"] == "balanced"
        assert int(filtered.iloc[0]["seed"]) == 2

    def test_filter_results_by_all_axes(self):
        module = _load_script_module("collect_results_test_filter_axes", "scripts/collect_results.py")
        df = pd.DataFrame([
            {"model": "deepdta", "dataset": "balanced", "seed": 1, "split": "random", "negative": "negbiodb"},
            {"model": "graphdta", "dataset": "balanced", "seed": 1, "split": "random", "negative": "negbiodb"},
            {"model": "deepdta", "dataset": "balanced", "seed": 1, "split": "cold_target", "negative": "negbiodb"},
            {"model": "deepdta", "dataset": "balanced", "seed": 1, "split": "random", "negative": "uniform_random"},
        ])

        filtered = module.filter_results(
            df,
            dataset="balanced",
            seeds=[1],
            models=["deepdta"],
            splits=["random"],
            negatives=["negbiodb"],
        )
        assert len(filtered) == 1
        row = filtered.iloc[0]
        assert row["model"] == "deepdta"
        assert row["split"] == "random"
        assert row["negative"] == "negbiodb"

    def test_build_table1_preserves_dataset_and_seed(self):
        module = _load_script_module("collect_results_test_table", "scripts/collect_results.py")
        df = pd.DataFrame([
            {
                "model": "deepdta",
                "split": "random",
                "negative": "negbiodb",
                "dataset": "balanced",
                "seed": 42,
                "n_test": 10,
                "log_auc": 0.5,
                "auprc": 0.4,
                "bedroc": 0.3,
                "ef_1pct": 1.0,
                "ef_5pct": 2.0,
                "mcc": 0.1,
                "auroc": 0.6,
            }
        ])

        table = module.build_table1(df)
        assert list(table.columns[:5]) == ["model", "dataset", "seed", "split", "negative"]

    def test_aggregate_over_seeds_computes_mean_std_and_counts(self):
        module = _load_script_module("collect_results_test_aggregate", "scripts/collect_results.py")
        df = pd.DataFrame([
            {
                "model": "deepdta", "dataset": "balanced", "seed": 1, "split": "random", "negative": "negbiodb",
                "n_test": 10, "log_auc": 0.4, "auprc": 0.3, "bedroc": 0.2, "ef_1pct": 1.0, "ef_5pct": 2.0, "mcc": 0.1, "auroc": 0.6,
            },
            {
                "model": "deepdta", "dataset": "balanced", "seed": 2, "split": "random", "negative": "negbiodb",
                "n_test": 12, "log_auc": 0.6, "auprc": 0.5, "bedroc": 0.4, "ef_1pct": 3.0, "ef_5pct": 4.0, "mcc": 0.3, "auroc": 0.8,
            },
        ])

        agg = module.aggregate_over_seeds(df)
        assert len(agg) == 1
        row = agg.iloc[0]
        assert int(row["n_seeds"]) == 2
        assert row["log_auc_mean"] == pytest.approx(0.5)
        assert row["n_test_mean"] == pytest.approx(11.0)
        assert "log_auc_std" in agg.columns

    def test_format_aggregated_markdown_includes_mean_std(self):
        module = _load_script_module("collect_results_test_agg_md", "scripts/collect_results.py")
        agg = pd.DataFrame([
            {
                "model": "deepdta",
                "dataset": "balanced",
                "split": "random",
                "negative": "negbiodb",
                "n_seeds": 2,
                "log_auc_mean": 0.5,
                "log_auc_std": 0.1,
                "auprc_mean": 0.4,
                "auprc_std": 0.05,
                "bedroc_mean": 0.3,
                "bedroc_std": 0.02,
                "ef_1pct_mean": 1.0,
                "ef_1pct_std": 0.1,
                "ef_5pct_mean": 2.0,
                "ef_5pct_std": 0.2,
                "mcc_mean": 0.1,
                "mcc_std": 0.01,
                "auroc_mean": 0.6,
                "auroc_std": 0.03,
            }
        ])

        md = module.format_aggregated_markdown(agg)
        assert "0.500 +/- 0.100" in md
        assert "| **Seeds** |" in md

    def test_summarize_exp1_groups_by_dataset(self):
        module = _load_script_module("collect_results_test_summary", "scripts/collect_results.py")
        df = pd.DataFrame([
            {"model": "deepdta", "dataset": "balanced", "negative": "negbiodb", "seed": 1, "log_auc": 0.50},
            {"model": "deepdta", "dataset": "balanced", "negative": "uniform_random", "seed": 1, "log_auc": 0.60},
            {"model": "deepdta", "dataset": "balanced", "negative": "degree_matched", "seed": 1, "log_auc": 0.55},
            {"model": "deepdta", "dataset": "realistic", "negative": "negbiodb", "seed": 2, "log_auc": 0.40},
            {"model": "deepdta", "dataset": "realistic", "negative": "uniform_random", "seed": 2, "log_auc": 0.44},
            {"model": "deepdta", "dataset": "realistic", "negative": "degree_matched", "seed": 2, "log_auc": 0.42},
        ])

        summary = module.summarize_exp1(df)
        assert "Dataset=balanced" in summary
        assert "Dataset=realistic" in summary

    def test_summarize_exp1_uses_only_matched_seeds(self):
        module = _load_script_module("collect_results_test_matched_seeds", "scripts/collect_results.py")
        df = pd.DataFrame([
            {"model": "deepdta", "dataset": "balanced", "negative": "negbiodb", "seed": 1, "log_auc": 0.50},
            {"model": "deepdta", "dataset": "balanced", "negative": "uniform_random", "seed": 1, "log_auc": 0.60},
            {"model": "deepdta", "dataset": "balanced", "negative": "degree_matched", "seed": 1, "log_auc": 0.55},
            {"model": "deepdta", "dataset": "balanced", "negative": "negbiodb", "seed": 2, "log_auc": 0.10},
            {"model": "deepdta", "dataset": "balanced", "negative": "uniform_random", "seed": 3, "log_auc": 0.90},
            {"model": "deepdta", "dataset": "balanced", "negative": "degree_matched", "seed": 4, "log_auc": 0.95},
        ])

        summary = module.summarize_exp1(df)
        assert "[n=1]" in summary
        assert "NegBioDB=0.500" in summary

    def test_summarize_exp1_aggregated_uses_only_matched_seeds(self):
        module = _load_script_module("collect_results_test_agg_matched_seeds", "scripts/collect_results.py")
        df = pd.DataFrame([
            {"model": "deepdta", "dataset": "balanced", "negative": "negbiodb", "seed": 1, "log_auc": 0.50},
            {"model": "deepdta", "dataset": "balanced", "negative": "uniform_random", "seed": 1, "log_auc": 0.60},
            {"model": "deepdta", "dataset": "balanced", "negative": "degree_matched", "seed": 1, "log_auc": 0.55},
            {"model": "deepdta", "dataset": "balanced", "negative": "negbiodb", "seed": 2, "log_auc": 0.10},
            {"model": "deepdta", "dataset": "balanced", "negative": "uniform_random", "seed": 3, "log_auc": 0.90},
            {"model": "deepdta", "dataset": "balanced", "negative": "degree_matched", "seed": 4, "log_auc": 0.95},
        ])

        summary = module.summarize_exp1_aggregated(df)
        assert "[n=1]" in summary
        assert "NegBioDB=0.500" in summary
        assert "uniform_random=0.600" in summary
        assert "0.900" not in summary

    def test_main_returns_error_when_filters_remove_all_rows(self, tmp_path):
        module = _load_script_module("collect_results_test_main_filter", "scripts/collect_results.py")
        results_dir = tmp_path / "results" / "baselines" / "run1"
        results_dir.mkdir(parents=True)
        (results_dir / "results.json").write_text(json.dumps({
            "model": "deepdta",
            "split": "random",
            "negative": "negbiodb",
            "dataset": "balanced",
            "seed": 1,
            "n_train": 1,
            "n_val": 1,
            "n_test": 1,
            "best_val_log_auc": 0.1,
            "test_metrics": {"log_auc": 0.2},
        }))

        rc = module.main([
            "--results-dir", str(tmp_path / "results" / "baselines"),
            "--out", str(tmp_path / "out"),
            "--dataset", "realistic",
        ])
        assert rc == 1

    def test_main_writes_aggregated_csv(self, tmp_path):
        module = _load_script_module("collect_results_test_main_agg", "scripts/collect_results.py")
        results_base = tmp_path / "results" / "baselines"
        for seed, log_auc in [(1, 0.2), (2, 0.4)]:
            run_dir = results_base / f"run_{seed}"
            run_dir.mkdir(parents=True, exist_ok=True)
            (run_dir / "results.json").write_text(json.dumps({
                "model": "deepdta",
                "split": "random",
                "negative": "negbiodb",
                "dataset": "balanced",
                "seed": seed,
                "n_train": 1,
                "n_val": 1,
                "n_test": 1,
                "best_val_log_auc": 0.1,
                "test_metrics": {
                    "log_auc": log_auc,
                    "auprc": 0.1,
                    "bedroc": 0.1,
                    "ef_1pct": 1.0,
                    "ef_5pct": 1.0,
                    "mcc": 0.1,
                    "auroc": 0.1,
                },
            }))

        out_dir = tmp_path / "out"
        rc = module.main([
            "--results-dir", str(results_base),
            "--out", str(out_dir),
            "--aggregate-seeds",
        ])
        assert rc == 0
        assert (out_dir / "table1_aggregated.csv").exists()
        assert (out_dir / "table1_aggregated.md").exists()