L2G Trainer
gentropy.method.l2g.trainer.LocusToGeneTrainer
dataclass
¶
Modelling of what is the most likely causal gene associated with a given locus.
Source code in src/gentropy/method/l2g/trainer.py
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 |
|
cross_validate(wandb_run_name: str | None = None, parameter_grid: dict[str, Any] | None = None, n_splits: int = 5, random_state: int = 42) -> None
¶
Log results of cross validation and hyperparameter tuning with W&B Sweeps. Metrics for every combination of hyperparameters will be logged to W&B for comparison.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wandb_run_name
|
str | None
|
Name of the W&B run. Unless this is provided, the model will not be logged to W&B. |
None
|
parameter_grid
|
dict[str, Any] | None
|
Dictionary containing the hyperparameters to sweep over. The keys are the hyperparameter names, and the values are dictionaries containing the values to sweep over. |
None
|
n_splits
|
int
|
Number of folds the data is splitted in. The model is trained and evaluated |
5
|
random_state
|
int
|
Random seed for reproducibility. Defaults to 42. |
42
|
Source code in src/gentropy/method/l2g/trainer.py
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 |
|
evaluate(y_true: np.ndarray, y_pred: np.ndarray, y_pred_proba: np.ndarray) -> dict[str, float]
staticmethod
¶
Evaluate the model on a test set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true
|
ndarray
|
True labels |
required |
y_pred
|
ndarray
|
Predicted labels |
required |
y_pred_proba
|
ndarray
|
Predicted probabilities for the positive class |
required |
Returns:
Type | Description |
---|---|
dict[str, float]
|
dict[str, float]: Dictionary of evaluation metrics |
Source code in src/gentropy/method/l2g/trainer.py
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 |
|
fit() -> LocusToGeneModel
¶
Fit the pipeline to the feature matrix dataframe.
Returns:
Name | Type | Description |
---|---|---|
LocusToGeneModel |
LocusToGeneModel
|
Fitted model |
Raises:
Type | Description |
---|---|
ValueError
|
Train data not set, nothing to fit. |
AssertionError
|
If x_train or y_train are empty matrices |
Source code in src/gentropy/method/l2g/trainer.py
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 |
|
hierarchical_split(data_df: pd.DataFrame, test_size: float = 0.15, verbose: bool = True, random_state: int = 777) -> tuple[pd.DataFrame, pd.DataFrame]
staticmethod
¶
Implements hierarchical splitting strategy to prevent data leakage.
Strategy: 1. Split positives by geneId groups 2. Further split by studyLocusId within each gene group 3. Augment splits with corresponding negatives based on studyLocusId
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_df
|
DataFrame
|
Input dataframe with goldStandardSet column (1=positive, 0=negative) |
required |
test_size
|
float
|
Proportion of data for test set. Defaults to 0.15 |
0.15
|
verbose
|
bool
|
Print splitting statistics |
True
|
random_state
|
int
|
Random seed for reproducibility. Defaults to 777 |
777
|
Returns:
Type | Description |
---|---|
tuple[DataFrame, DataFrame]
|
tuple[pd.DataFrame, pd.DataFrame]: Training and test dataframes |
Source code in src/gentropy/method/l2g/trainer.py
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 |
|
log_plot_image_to_wandb(title: str, plot: Axes) -> None
¶
Accepts a plot object, and saves the fig to PNG to then log it in W&B.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title
|
str
|
Title of the plot. |
required |
plot
|
Axes
|
Shap plot to log. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
Run not set, cannot log to W&B. |
Source code in src/gentropy/method/l2g/trainer.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
|
log_to_terminal(eval_id: str, metrics: dict[str, Any]) -> None
¶
Log metrics to terminal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eval_id
|
str
|
Name of the evaluation set |
required |
metrics
|
dict[str, Any]
|
Model metrics |
required |
Source code in src/gentropy/method/l2g/trainer.py
260 261 262 263 264 265 266 267 268 269 270 |
|
log_to_wandb(wandb_run_name: str) -> None
¶
Log evaluation results and feature importance to W&B to compare between different L2G runs.
Dashboard is available at https://wandb.ai/open-targets/gentropy-locus-to-gene?nw=nwuseropentargets Credentials to access W&B are available at the OT central login sheet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wandb_run_name
|
str
|
Name of the W&B run |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If dependencies are not available. |
AssertionError
|
If x_train or y_train are empty matrices |
Source code in src/gentropy/method/l2g/trainer.py
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 |
|
train(wandb_run_name: str | None = None, test_size: float = 0.15, cross_validate: bool = True, n_splits: int = 5, hyperparameter_grid: dict[str, Any] | None = None) -> LocusToGeneModel
¶
Train the Locus to Gene model.
If cross_validation is set to True, we implement the following strategy: 1. Create held-out test set 2. Perform cross-validation on training set 3. Train final model on full training set 4. Evaluate once on test set
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wandb_run_name
|
str | None
|
Name of the W&B run. Unless this is provided, the model will not be logged to W&B. |
None
|
test_size
|
float
|
Proportion of the test set |
0.15
|
cross_validate
|
bool
|
Whether to run cross-validation. Defaults to True. |
True
|
n_splits(int)
|
Number of folds the data is splitted in. The model is trained and evaluated |
required | |
hyperparameter_grid
|
dict[str, Any] | None
|
Hyperparameter grid to sweep over. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
LocusToGeneModel |
LocusToGeneModel
|
Fitted model |
Source code in src/gentropy/method/l2g/trainer.py
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 |
|