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 | class LocusToGeneStep:
"""Locus to gene step."""
def __init__(
self,
session: Session,
*,
run_mode: str,
hyperparameters: dict[str, Any],
download_from_hub: bool,
cross_validate: bool,
train_on_full_dataset: bool,
credible_set_path: str | None = None,
feature_matrix_path: str | None = None,
wandb_run_name: str | None = None,
model_path: str = "opentargets/locus_to_gene",
features_list: list[str] | None = None,
predictions_path: str | None = None,
l2g_threshold: float = 0.05,
hf_hub_repo_id: str = "locus_to_gene",
hf_model_commit_message: str = "chore: update model",
hf_model_version: str | None = None,
explain_predictions: bool = False,
hf_credentials_path: str | None = None,
wandb_credentials_path: str | None = None,
train_parquet_path: str | None = None,
test_parquet_path: str | None = None,
) -> None:
"""Initialise the step and run the logic based on mode.
Args:
session (Session): Session object that contains the Spark session
run_mode (str): Run mode, either 'train' or 'predict'
hyperparameters (dict[str, Any]): Hyperparameters for the model
download_from_hub (bool): Whether to download the model from Hugging Face Hub
cross_validate (bool): Whether to run cross validation (5-fold by default) to train the model.
train_on_full_dataset (bool): Whether to retrain the final saved model on the full dataset (train + held-out) after evaluation. Follows the standard practice of reporting honest held-out metrics while ensuring the deployed model benefits from all available labelled data.
credible_set_path (str | None): Path to the credible set dataset. Required for predict mode; unused in train mode when pre-split parquets are provided.
feature_matrix_path (str | None): Path to the L2G feature matrix input dataset. Required for predict mode; unused in train mode when pre-split parquets are provided.
wandb_run_name (str | None): Name of the run to track model training in Weights and Biases
model_path (str): Path to the model. It can be either in the filesystem or the name on the Hugging Face Hub (in the form of username/repo_name).
features_list (list[str] | None): List of features to use to train the model
predictions_path (str | None): Path to the L2G predictions output dataset
l2g_threshold (float): An optional threshold for the L2G score to filter predictions. A threshold of 0.05 is recommended.
hf_hub_repo_id (str): Hugging Face Hub repository handle in ``username/repo_name`` format. Used to download the model when ``download_from_hub`` is ``True`` (predict mode) and to upload the trained model (train mode).
hf_model_commit_message (str): Commit message when we upload the model to the Hugging Face Hub
hf_model_version (str | None): Tag, branch, or commit hash to download the model from the Hub. Defaults to latest commit when provided None.
explain_predictions (bool): Whether to extract SHAP importances for the L2G predictions. This is computationally expensive.
hf_credentials_path (str | None): Optional path to the Hugging Face Hub credentials JSON file. If not provided, the HF_TOKEN environment variable will be used.
wandb_credentials_path (str | None): Optional path to the Weights and Biases credentials JSON file. If not provided, the WANDB_API_KEY environment variable will be used.
train_parquet_path (str | None): Path to the training split parquet produced by ``LocusToGeneTrainTestSplitStep``. Required in train mode.
test_parquet_path (str | None): Path to the test split parquet produced by ``LocusToGeneTrainTestSplitStep``. Required in train mode.
Raises:
ValueError: If run_mode is not 'train' or 'predict'
Note: One can fetch the credentials for HF Hub and W&B from environment variables or from JSON files. The JSON file for HF Hub should contain a
field "HF_TOKEN" with the Hugging Face API token as value, while the JSON file for W&B should contain a field "WANDB_API_KEY" with the W&B API key as value.
#### Credential files
Example of credentials JSON file content for Hugging Face Hub:
```json
{
"HF_TOKEN": "your_hugging_face_api_token"
}
```
Example of credentials JSON file content for W&B:
```json
{
"WANDB_API_KEY": "your_wandb_api_key"
}
```
"""
if run_mode not in ["train", "predict"]:
raise ValueError(
f"run_mode must be one of 'train' or 'predict', got {run_mode}"
)
# Common parameters
self.session = session
self.run_mode = run_mode
self.features_list = list(features_list) if features_list else None
# Train io
self.train_parquet_path = train_parquet_path
self.test_parquet_path = test_parquet_path
self.train_on_full_dataset = train_on_full_dataset
# Train parameters
self.hyperparameters = dict(hyperparameters)
self.cross_validate = cross_validate
# External resource parameters
self.hf_hub_repo_id = hf_hub_repo_id
self.hf_model_commit_message = hf_model_commit_message
self.hf_model_version = hf_model_version
self.hf_credentials_path = hf_credentials_path
self.wandb_run_name = wandb_run_name
self.wandb_credentials_path = wandb_credentials_path
# Predict io
self.download_from_hub = download_from_hub
self.model_path = model_path
self.predictions_path = predictions_path
# Predict parameters
self.l2g_threshold = l2g_threshold
self.explain_predictions = explain_predictions
if run_mode == "predict":
if not credible_set_path:
raise ValueError("credible_set_path is required for predict mode.")
if not feature_matrix_path:
raise ValueError("feature_matrix_path is required for predict mode.")
self.credible_set = StudyLocus.from_parquet(
session, credible_set_path, recursiveFileLookup=True
)
self.feature_matrix = L2GFeatureMatrix(
_df=session.load_data(feature_matrix_path, "parquet"),
)
if download_from_hub:
if not self.hf_hub_repo_id:
raise ValueError(
"hf_hub_repo_id must be provided when download_from_hub is True"
)
self.model_path = HuggingFaceModelRepoHandle(
handle=self.hf_hub_repo_id
).handle
self.run_predict()
elif run_mode == "train":
self.run_train()
def run_predict(self) -> None:
"""Run the prediction step.
Raises:
ValueError: If predictions_path is not provided for prediction mode
"""
hf_token = None
if self.download_from_hub:
hf_hub_credentials = HuggingFaceHubCredentials.read(
self.hf_credentials_path
)
hf_token = hf_hub_credentials.HF_TOKEN
if not self.predictions_path:
raise ValueError("predictions_path must be provided for prediction mode")
predictions = (
L2GPrediction.from_credible_set(
self.session,
self.credible_set,
self.feature_matrix,
model_path=self.model_path,
features_list=self.features_list,
hf_token=hf_token,
hf_model_version=self.hf_model_version,
download_from_hub=self.download_from_hub,
)
.filter(f.col("score") >= self.l2g_threshold)
.add_features(
self.feature_matrix,
)
)
if self.explain_predictions:
predictions = predictions.explain()
predictions.df.coalesce(self.session.output_partitions).write.mode(
self.session.write_mode
).parquet(self.predictions_path)
self.session.logger.info("L2G predictions saved successfully.")
def run_train(self) -> None:
"""Run the training step.
Raises:
ValueError: If features list or presplit parquet paths are not provided.
"""
if self.features_list is None:
raise ValueError("Features list is required for model training.")
if not (self.train_parquet_path and self.test_parquet_path):
raise ValueError(
"train_parquet_path and test_parquet_path are required for model training. "
"Run LocusToGeneTrainTestSplitStep first."
)
# Initialize access to weights and biases
if self.wandb_run_name:
wandb_credentials = WandbCredentials.read(self.wandb_credentials_path)
wandb_login(key=wandb_credentials.WANDB_API_KEY.get_secret_value())
# Initialize access to Hugging Face Hub
hf_token = None
if self.hf_hub_repo_id and self.hf_model_commit_message:
hf_hub_credentials = HuggingFaceHubCredentials.read(
self.hf_credentials_path
)
hf_token = hf_hub_credentials.HF_TOKEN
# Instantiate classifier and train model
l2g_model = LocusToGeneModel(
model=XGBClassifier(random_state=777, eval_metric="aucpr"),
hyperparameters=self.hyperparameters,
features_list=self.features_list,
)
# Load presplit data produced by LocusToGeneTrainTestSplitStep.
# Persist so toPandas() (for XGBoost) and the union (for HF Hub metadata) share the cache.
train_sdf = self.session.spark.read.parquet(self.train_parquet_path).persist()
test_sdf = self.session.spark.read.parquet(self.test_parquet_path).persist()
# Reconstruct L2GFeatureMatrix for model metadata (column order, HF Hub upload).
# Labels are decoded back to strings because export_to_hugging_face_hub calls
# generate_train_test_split, which expects string-encoded labels.
label_map = f.create_map(f.lit(0), f.lit("negative"), f.lit(1), f.lit("positive"))
feature_matrix = L2GFeatureMatrix(
_df=train_sdf.union(test_sdf).withColumn(
"goldStandardSet",
f.coalesce(
label_map[f.col("goldStandardSet").cast("int")],
f.col("goldStandardSet").cast("string"),
),
),
with_gold_standard=True,
).select_features(self.features_list)
presplit_train_df = train_sdf.toPandas()
presplit_test_df = test_sdf.toPandas()
train_sdf.unpersist()
test_sdf.unpersist()
# Run the training
trained_model = LocusToGeneTrainer(
model=l2g_model, feature_matrix=feature_matrix
).train(
wandb_run_name=self.wandb_run_name,
cross_validate=self.cross_validate,
train_on_full_dataset=self.train_on_full_dataset,
presplit_train_df=presplit_train_df,
presplit_test_df=presplit_test_df,
)
# Export the model
if trained_model.training_data and trained_model.model and self.model_path:
trained_model.save(self.model_path)
if self.hf_hub_repo_id and self.hf_model_commit_message and hf_token:
trained_model.export_to_hugging_face_hub(
self.model_path.split("/")[-1],
hf_token=hf_token,
feature_matrix=trained_model.training_data,
repo_id=self.hf_hub_repo_id,
commit_message=self.hf_model_commit_message,
)
|