Grid Search Module¶
Hyperparameter optimization for leech models.
Overview¶
The gridsearch module provides utilities for optimizing chunk context parameters.
GridSearchConfig
dataclass
¶
GridSearchConfig(train_data_path: Path, val_data_path: Path | None, model_name: str, output_dir: Path, left_contexts: list[int], right_contexts: list[int], cfg: TrainConfig = TrainConfig(), kmer_context: int = 5, device: str = 'cuda', seed: int | None = None, dwell_offsets: list[int] | None = None, n_parallel: int = 1, num_workers: int = 0, selection_metric: str = 'auto')
Configuration for chunk context grid search.
The training recipe (epochs, learning_rate, motif, augmentation, aux
heads, ...) lives in cfg: TrainConfig (#270) instead of being
redeclared here as ~30 individual fields -- this class only adds what a
grid search needs beyond one training recipe: which geometry to sweep and
how to run the sweep.
Attributes:
| Name | Type | Description |
|---|---|---|
train_data_path |
Path
|
Path to training chunks or BAM/POD5 for preparation |
val_data_path |
Path | None
|
Path to validation chunks or BAM/POD5 |
model_name |
str
|
Model architecture to use |
output_dir |
Path
|
Base output directory for all grid results |
left_contexts |
list[int]
|
List of left context sizes to test |
right_contexts |
list[int]
|
List of right context sizes to test |
cfg |
TrainConfig
|
Training recipe shared by every grid point, except where run_grid_point derives a point-specific override (left_context, right_context, the resolved selection_metric/pos_weight) via dataclasses.replace |
kmer_context |
int
|
K-mer context for sequence encoding |
device |
str
|
Device for training |
seed |
int | None
|
Random seed |
dwell_offsets |
list[int] | None
|
Dwell offset values to sweep (None = [0]) |
n_parallel |
int
|
Number of grid points to run concurrently |
num_workers |
int
|
DataLoader workers per grid point |
selection_metric |
str
|
Grid-point ranking criterion ("auto" mirrors the training checkpoint criterion: val_f1 for multiclass, val_auc for binary) |
run_grid_search
¶
Run grid search over chunk contexts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
GridSearchConfig
|
Grid search configuration |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to grid search summary CSV |
Source code in src/leech/gridsearch.py
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 | |
parse_context_grid
¶
parse_context_grid(context_grid: str | None = None, left_contexts: str | None = None, right_contexts: str | None = None) -> tuple[list[int], list[int]]
Parse context grid strings into integer lists.
Supports range syntax (start:stop:step), comma-separated lists,
and single values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_grid
|
str | None
|
Fallback context values when left/right not provided (e.g., "200,500,1000" or "200:1000:200") |
None
|
left_contexts
|
str | None
|
Override left contexts, or None to use context_grid |
None
|
right_contexts
|
str | None
|
Override right contexts, or None to use context_grid |
None
|
Returns:
| Type | Description |
|---|---|
tuple[list[int], list[int]]
|
Tuple of (left_contexts_list, right_contexts_list) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If context_grid is None and either left_contexts or right_contexts is also None |
Examples:
>>> parse_context_grid("200,500,1000")
([200, 500, 1000], [200, 500, 1000])
>>> parse_context_grid("200:1000:200")
([200, 400, 600, 800, 1000], [200, 400, 600, 800, 1000])
>>> parse_context_grid("200,500", left_contexts="100,200", right_contexts="300,400")
([100, 200], [300, 400])
>>> parse_context_grid(left_contexts="100,200", right_contexts="300,400")
([100, 200], [300, 400])
Source code in src/leech/gridsearch.py
Example Usage¶
For more details, see the Grid Search Guide.