API

Import DrEvalPy using

import drevalpy as dep

Subpackages

DrEvalPy consists of three major subpackages (datasets, which also contains the split providers, models, and visualization):

  • Datasets

  • Models

  • Visualization

Other functions

Major functions for running the experiment

Main module for running the drug response prediction experiment.

drevalpy.experiment.NORMALIZATION_BASELINE = 'NaiveMeanEffectsPredictor'

Baseline that defines the reference scale of all Normalized * metrics. It is always run and is therefore never trained on a transformed response, see drug_response_experiment.

drevalpy.experiment.RESPONSE_TRANSFORMATION_FILE = 'response_transformation.pkl'

File name under which train_final_model stores the fitted response transformation next to the model.

drevalpy.experiment.consolidate_single_drug_model_predictions(models, n_cv_splits, results_path, cross_study_datasets, randomization_mode=None, n_trials_robustness=0, out_path='')

Consolidate single drug model predictions into a single file.

Parameters:
  • models (list[type[DRPModel]]) – list of model classes to compare, e.g., [SimpleNeuralNetwork, RandomForest]

  • n_cv_splits (int) – number of cross-validation splits, e.g., 5

  • results_path (str) – path to the results directory, e.g., results/

  • cross_study_datasets (list[str]) – list of cross-study datasets, e.g., [CCLE, GDSC1]

  • randomization_mode (list[str] | None) – list of randomization modes, e.g., [“SVCC”, “SVRC”]

  • n_trials_robustness (int) – number of robustness trials, e.g., 10

  • out_path (str) – for the package, this is the same as results_path. For the pipeline, this is empty because it will be stored in the work directory.

Return type:

None

drevalpy.experiment.cross_study_prediction(dataset, model, test_mode, train_dataset, path_data, early_stopping_dataset, response_transformation, path_out, split_index, single_drug_id=None)

Run the drug response prediction experiment on a cross-study dataset to assess the generalizability of the model.

Parameters:
  • dataset (DrugResponseDataset) – cross-study dataset, e.g., GDSC1 if trained on GDSC2

  • model (DRPModel) – model to use, e.g, SimpleNeuralNetwork

  • test_mode (str) – test mode one of “LPO”, “LCO”, “LDO” (leave-pair-out, leave-cell-line-out, leave-drug-out)

  • train_dataset (DrugResponseDataset) – training dataset, e.g., GDSC2

  • path_data (str) – path to the data directory, e.g., data/

  • early_stopping_dataset (DrugResponseDataset | None) – early stopping dataset

  • response_transformation (TransformerMixin | None) – normalizer to use for the response data, e.g., StandardScaler

  • path_out (str) – path to the output directory, e.g., results/

  • split_index (int) – index of the split

  • single_drug_id (str | None) – drug id to use for single drug models None for global models

Raises:

ValueError – if feature loading fails, if the test mode is invalid, or if LTO and no tissues are supplied.

Return type:

None

drevalpy.experiment.drug_response_experiment(models, response_data, baselines=None, response_transformation=None, run_id='', test_mode='LPO', hpam_optimization_metric='RMSE', n_cv_splits=5, multiprocessing=False, randomization_mode=None, randomization_type='permutation', cross_study_datasets=None, n_trials_robustness=0, path_out='results/', overwrite=False, path_data='data', model_checkpoint_dir='TEMPORARY', hyperparameter_tuning=True, final_model_on_full_data=False, wandb_project=None, custom_splitter=None, custom_split_name=None, n_jobs=1)

Run the drug response prediction experiment. Save results to disc.

Parameters:
  • models (list[type[DRPModel]]) – list of model classes to compare

  • baselines (list[type[DRPModel]] | None) – list of baseline models. No randomization or robustness tests are run for the baseline models.

  • response_data (DrugResponseDataset) – drug response dataset

  • response_transformation (TransformerMixin | None) – normalizer to use for the response data. The NaiveMeanEffectsPredictor is exempt from it because it defines the reference scale of the Normalized * metrics.

  • hpam_optimization_metric (str) – metric to use for hyperparameter optimization (i.e., for selecting the best model on the validation set)

  • n_cv_splits (int) – number of cross-validation splits

  • multiprocessing (bool) – whether to use multiprocessing. This requires Ray to be installed.

  • randomization_mode (list[str] | None) –

    list of randomization modes to do. Modes: SVCC, SVRC, SVCD, SVRD Can be a list of randomization tests e.g. ‘SVCC SVCD’. Default is None, which means no randomization tests are run.

    • SVCC: Single View Constant for Cell Lines: in this mode, one experiment is done for every cell line view

      the model uses (e.g. gene expression, mutation, …). For each experiment one cell line view is held constant while the others are randomized.

    • SVRC Single View Random for Cell Lines: in this mode, one experiment is done for every cell line view the

      model uses (e.g. gene expression, mutation, …). For each experiment one cell line view is randomized while the others are held constant.

    • SVCD: Single View Constant for Drugs: in this mode, one experiment is done for every drug view the model

      uses (e.g. fingerprints, target_information, …). For each experiment one drug view is held constant while the others are randomized.

    • SVRD: Single View Random for Drugs: in this mode, one experiment is done for every drug view the model uses

      (e.g. gene expression, target_information, …). For each experiment one drug view is randomized while the others are held constant.

  • randomization_type (str) –

    type of randomization to use. Choose from “permutation” and “invariant”. Default is “permutation”.

    • ”permutation”: permute the features over the instances, keeping the distribution of the features the same

      but dissolving the relationship to the target

    • ”invariant”: the features are permuted in a way that a key characteristic of the feature is kept. In case of

      matrices, this is the mean and standard deviation of the feature view for this instance, for networks it is the degree distribution.

  • cross_study_datasets (list[DrugResponseDataset] | None) – list of datasets for the cross-study prediction. The trained model is assessed for its generalization to these datasets. Default is None, which means no cross-study prediction is run.

  • n_trials_robustness (int) – number of trials to run for the robustness test. The robustness test is a test where models are retrained multiple times with varying seeds. Default is 0, which means no robustness test is run.

  • path_out (str) – path to the output directory

  • run_id (str) – identifier to save the results

  • test_mode (str) – test mode one of “LPO”, “LCO”, “LTO”, “LDO” (leave-pair-out, leave-cell-line-out, leave-tissue-out, leave-drug-out)

  • overwrite (bool) – whether to overwrite existing results

  • path_data (str) – path to the data directory, usually data/

  • model_checkpoint_dir (str) – directory to save model checkpoints. If “TEMPORARY”, a temporary directory is created.

  • hyperparameter_tuning – whether to run in debug mode - if False, only select first hyperparameter set

  • final_model_on_full_data (bool) – if True, a final/production model is saved in the results directory. If hyperparameter_tuning is true, the final model is produced according to the hyperparameter tuning procedure which was evaluated in the nested cross validation.

  • wandb_project (str | None) – if provided, enables wandb logging for all DRPModel instances throughout training. All hyperparameters and metrics will be logged to the specified wandb project.

  • custom_splitter (Callable[[DrugResponseDataset, SplitParams], list[dict[str, Any]]] | str | Path | None) – optional path to a Python script or callable implementing create_splits. When provided, built-in split_dataset is skipped and test_mode selects validation checks.

  • custom_split_name (str | None) – optional result-directory label when using a custom splitter. Defaults to test_mode when omitted.

  • n_jobs (int) – number of drugs a single drug model is trained for in parallel. Cannot be combined with multiprocessing. On a GPU, the drugs are trained sequentially.

Raises:

ValueError – if no cv splits are found or if multiprocessing is combined with n_jobs > 1

Return type:

None

drevalpy.experiment.generate_data_saving_path(model_name, drug_id, result_path, suffix)

Generate a path to save data to.

For single drug models, the path is result_path/model_name/drugs/drug_id/suffix. For all others, it is result_path/model_name/suffix.

Parameters:
  • model_name – model name

  • drug_id – drug id

  • result_path – path to the results directory

  • suffix – suffix to add to the path, e.g., “predictions”, “best_hpams”, “randomization”, “robustness”

Return type:

str

Returns:

path to save data to

drevalpy.experiment.get_datasets_from_cv_split(split, model_class, model_name, drug_id=None)

Get train, validation, (early stopping), and test datasets from the CV split.

Returns copies of the datasets to prevent in-place modifications (e.g., add_rows, reduce_to) from affecting the original split data used by subsequent models.

Parameters:
Return type:

tuple[DrugResponseDataset, DrugResponseDataset, DrugResponseDataset | None, DrugResponseDataset]

Returns:

tuple of train, validation, (early stopping), and test datasets (as copies)

drevalpy.experiment.get_randomization_test_views(model, randomization_mode)

Get the views to use for the randomization tests.

  • For SVCC, a single cell line view (e.g., gene expression) is held constant while the others are randomized.

  • For SVCD, a single drug view (e.g., fingerprints) is held constant while the others are randomized.

  • For SVRC, a single cell line view is randomized while the others are held constant.

  • For SVRD, a single drug view is randomized while the others are held constant.

Parameters:
  • model (DRPModel) – model to use, e.g., SimpleNeuralNetwork

  • randomization_mode (list[str]) – list of randomization modes to do, e.g., [“SVCC”, “SVRC”]

Return type:

dict[str, list[str]]

Returns:

dictionary of randomization test views

drevalpy.experiment.hpam_tune(model, train_dataset, validation_dataset, hpam_set, early_stopping_dataset=None, response_transformation=None, metric='RMSE', path_data='data', model_checkpoint_dir='TEMPORARY', *, split_index=None, wandb_project=None, wandb_base_config=None)

Tune the hyperparameters for the given model in an iterative manner.

Parameters:
  • model (DRPModel) – model to use

  • train_dataset (DrugResponseDataset) – training dataset

  • validation_dataset (DrugResponseDataset) – validation dataset

  • hpam_set (list[dict]) – hyperparameters to tune

  • early_stopping_dataset (DrugResponseDataset | None) – early stopping dataset

  • response_transformation (TransformerMixin | None) – normalizer to use for the response data

  • metric (str) – metric to evaluate which model is the best

  • path_data (str) – path to the data directory, e.g., data/

  • model_checkpoint_dir (str) – directory to save model checkpoints

  • split_index (int | None) – optional CV split index, used for naming wandb runs

  • wandb_project (str | None) – optional wandb project name; if provided, enables per-trial wandb runs

  • wandb_base_config (dict[str, Any] | None) – optional base config dict to include in each wandb run

Return type:

dict

Returns:

best hyperparameters

Raises:

AssertionError – if hpam_set is empty

drevalpy.experiment.hpam_tune_raytune(model, train_dataset, validation_dataset, early_stopping_dataset, hpam_set, response_transformation=None, metric='RMSE', ray_path='raytune', path_data='data', model_checkpoint_dir='TEMPORARY')

Tune the hyperparameters for the given model using Ray Tune. Ray[tune] must be installed.

Parameters:
  • model (DRPModel) – model to use

  • train_dataset (DrugResponseDataset) – training dataset

  • validation_dataset (DrugResponseDataset) – validation dataset

  • early_stopping_dataset (DrugResponseDataset | None) – early stopping dataset

  • hpam_set (list[dict]) – hyperparameters to tune

  • response_transformation (TransformerMixin | None) – normalizer for response data

  • metric (str) – evaluation metric

  • ray_path (str) – path to the raytune directory

  • path_data (str) – path to data directory, e.g., data/

  • model_checkpoint_dir (str) – directory for model checkpoints

Return type:

dict

Returns:

best hyperparameters

Raises:

ValueError – if best_result is None

drevalpy.experiment.load_features(model, path_data, dataset)

Load and reduce cell line and drug features for a given dataset.

Parameters:
  • model (DRPModel) – model to use, e.g., SimpleNeuralNetwork

  • path_data (str) – path to the data directory, e.g., data/

  • dataset (DrugResponseDataset) – dataset to load features for, e.g., GDSC2

Return type:

tuple[FeatureDataset, FeatureDataset | None]

Returns:

tuple of cell line and, potentially, drug features

drevalpy.experiment.make_train_val_split(dataset, test_mode, val_ratio=0.1, random_state=42)

Split a dataset into train and validation sets according to the test mode and desired ratio.

Parameters:
  • dataset (DrugResponseDataset) – full dataset to split

  • test_mode (str) – one of “LPO”, “LCO”, “LDO”, “LTO”

  • val_ratio (float) – approximate fraction of data to use for validation

  • random_state (int) – random seed

Return type:

tuple[DrugResponseDataset, DrugResponseDataset]

Returns:

(train_dataset, validation_dataset)

Raises:

ValueError – if no tissue information is provided for the DrugResponseDataset

drevalpy.experiment.prepare_response_splits(response_data, *, split_path, result_path, split_label, test_mode, n_cv_splits, overwrite, result_folder_exists, custom_splitter=None, validation_ratio=0.1, random_state=42, split_early_stopping=True)

Create, load, or reuse CV splits for an experiment run.

Parameters:
  • response_data (DrugResponseDataset) – dataset whose splits are created or loaded

  • split_path (str) – directory for persisted split CSV files

  • result_path (str) – experiment result directory

  • split_label (str) – directory label under the dataset results folder

  • test_mode (str) – built-in split mode or validation mode for custom splits

  • n_cv_splits (int) – number of CV splits for built-in splitting

  • overwrite (bool) – whether to replace existing results and splits

  • result_folder_exists (bool) – whether result_path already exists

  • custom_splitter (Callable[[DrugResponseDataset, SplitParams], list[dict[str, Any]]] | str | Path | None) – optional script path or callable for custom splits

  • validation_ratio (float) – validation fraction for built-in splitting

  • random_state (int) – random seed for built-in splitting

  • split_early_stopping (bool) – whether to derive early-stopping roles

Return type:

int

Returns:

number of splits available after preparation

drevalpy.experiment.randomization_test(randomization_test_views, model, hpam_set, path_data, train_dataset, test_dataset, early_stopping_dataset, path_out, split_index, randomization_type='permutation', response_transformation=None, model_checkpoint_dir='TEMPORARY')

Run randomization tests for the given model and dataset.

Parameters:
  • randomization_test_views (dict[str, list[str]]) – views to use for the randomization tests. Key is the name of the randomization test and the value is a list of views to randomize e.g. {“randomize_genomics”: [“copy_number_var”, “mutation”], “methylation_only”: [“gene_expression”, “copy_number_var”, “mutation”]}”

  • model (DRPModel) – model to evaluate

  • hpam_set (dict) – hyperparameters to use

  • path_data (str) – path to the data directory

  • train_dataset (DrugResponseDataset) – training dataset

  • test_dataset (DrugResponseDataset) – test dataset

  • early_stopping_dataset (DrugResponseDataset | None) – early stopping dataset

  • path_out (str) – path to the output directory

  • split_index (int) – index of the split

  • randomization_type (str) – type of randomization to use. Choose from “permutation”, “invariant”. Default is “permutation” which permutes the features over the instances, keeping the distribution of the features the same but dissolving the relationship to the target. invariant randomization is done in a way that a key characteristic of the feature is preserved. In case of matrices, this is the mean and standard deviation of the feature view for this instance, for networks it is the degree distribution.

  • response_transformation (TransformerMixin | None) – sklearn.preprocessing scaler like StandardScaler or MinMaxScaler to use to scale the target

  • model_checkpoint_dir (str) – directory to save model checkpoints

Return type:

None

drevalpy.experiment.randomize_train_predict(view, test_name, randomization_type, randomization_test_file, model, hpam_set, path_data, train_dataset, test_dataset, early_stopping_dataset, model_checkpoint_dir='TEMPORARY', response_transformation=None)

Randomize the features for a given view and run the model.

Parameters:
  • view (str) – view to randomize, e.g., gene_expression

  • test_name (str) – name of the randomization test, e.g., SVRC_gene_expression

  • randomization_type (str) – type of randomization to use, e.g., permutation

  • randomization_test_file (str) – file to save the results to

  • model (DRPModel) – model to evaluate

  • hpam_set (dict) – hyperparameters to use

  • path_data (str) – path to the data directory

  • train_dataset (DrugResponseDataset) – training dataset

  • test_dataset (DrugResponseDataset) – test dataset

  • early_stopping_dataset (DrugResponseDataset | None) – early stopping dataset

  • model_checkpoint_dir (str) – directory to save model checkpoints

  • response_transformation (TransformerMixin | None) – sklearn.preprocessing scaler like StandardScaler or MinMaxScaler to use to scale

Return type:

None

drevalpy.experiment.robustness_test(n_trials, model, hpam_set, path_data, train_dataset, test_dataset, early_stopping_dataset, path_out, split_index, response_transformation=None, model_checkpoint_dir='TEMPORARY')

Run robustness tests for the given model and dataset.

This will run the model n times with different random seeds to get a distribution of the results.

Parameters:
  • n_trials (int) – number of trials to run

  • model (DRPModel) – model to evaluate

  • hpam_set (dict) – hyperparameters to use

  • path_data (str) – path to the data directory

  • train_dataset (DrugResponseDataset) – training dataset

  • test_dataset (DrugResponseDataset) – test dataset

  • early_stopping_dataset (DrugResponseDataset | None) – early stopping dataset

  • path_out (str) – path to the output directory

  • split_index (int) – index of the split

  • response_transformation (TransformerMixin | None) – sklearn.preprocessing scaler like StandardScaler or MinMaxScaler to use to scale the target

  • model_checkpoint_dir (str) – directory to save model checkpoints, if “TEMPORARY”: temporary directory is used

drevalpy.experiment.robustness_train_predict(trial, trial_file, train_dataset, test_dataset, early_stopping_dataset, model, hpam_set, path_data, response_transformation=None, model_checkpoint_dir='TEMPORARY')

Train and predict for the robustness test.

Parameters:
  • trial (int) – trial number

  • trial_file (str) – file to save the results to

  • train_dataset (DrugResponseDataset) – training dataset

  • test_dataset (DrugResponseDataset) – test dataset

  • early_stopping_dataset (DrugResponseDataset | None) – early stopping dataset

  • model (DRPModel) – model to evaluate

  • hpam_set (dict) – hyperparameters to use

  • path_data (str) – path to the data directory, e.g., data/

  • response_transformation (TransformerMixin | None) – sklearn.preprocessing scaler like StandardScaler or MinMaxScaler to use to scale

  • model_checkpoint_dir (str) – directory to save model checkpoints. If “TEMPORARY”, a temporary directory is created.

Return type:

None

drevalpy.experiment.run_per_drug(func, drug_args, n_jobs=1)

Run func(drug_id, *args) for every drug, e.g., to train a single drug model per drug.

The drugs are distributed over n_jobs processes. An error for one drug is raised for all.

Parameters:
  • func (Callable[..., Any]) – function to run per drug, called as func(drug_id, *drug_args[drug_id])

  • drug_args (dict[str, tuple]) – {drug_id: arguments of func for this drug}, e.g., the output of split_by_drug

  • n_jobs (int) – number of drugs to run in parallel

Return type:

dict[str, Any]

Returns:

{drug_id: return value of func}

drevalpy.experiment.seed_everything(seed=42)

Seed python random, numpy, torch (CPU + CUDA), and PYTHONHASHSEED.

Call once at the top of a run. The dataset/model code uses local np.random.default_rng instances for its own randomness, so this exists to lock down everything else (torch op order, sklearn fallbacks, library-internal RNG, hash randomization).

Parameters:

seed (int) – base seed value

Return type:

None

drevalpy.experiment.split_by_drug(datasets, drug_ids)

Split each dataset into one dataset per drug, e.g., a CV split to train a single drug model per drug.

Splitting once in the main process avoids sending the data of all drugs to every worker of run_per_drug.

Parameters:
Return type:

dict[str, dict[str, DrugResponseDataset | None]]

Returns:

{drug_id: {name: dataset reduced to this drug}}

drevalpy.experiment.split_early_stopping(validation_dataset, test_mode)

Split the validation dataset into a validation and early stopping dataset.

Parameters:
  • validation_dataset (DrugResponseDataset) – validation dataset

  • test_mode (str) – test mode one of “LPO”, “LCO”, “LDO” (leave-pair-out, leave-cell-line-out, leave-drug-out)

Return type:

tuple[DrugResponseDataset, DrugResponseDataset]

Returns:

tuple of validation and early stopping datasets

drevalpy.experiment.train_and_evaluate(model, hpams, path_data, train_dataset, validation_dataset, early_stopping_dataset=None, response_transformation=None, metric='RMSE', model_checkpoint_dir='TEMPORARY')

Train and evaluate the model, i.e., call train_and_predict() and then evaluate().

Parameters:
  • model (DRPModel) – model to use

  • hpams (dict[str, Any]) – hyperparameters to use

  • path_data (str) – path to the data directory

  • train_dataset (DrugResponseDataset) – training dataset

  • validation_dataset (DrugResponseDataset) – validation dataset

  • early_stopping_dataset (DrugResponseDataset | None) – early stopping dataset

  • response_transformation (TransformerMixin | None) – normalizer to use for the response data

  • metric (str) – metric to evaluate the model on

  • model_checkpoint_dir (str) – directory to save model checkpoints

Return type:

dict[str, float]

Returns:

dictionary of the evaluation results, e.g., {“RMSE”: 0.1}

drevalpy.experiment.train_and_predict(model, hpams, path_data, train_dataset, prediction_dataset, early_stopping_dataset=None, response_transformation=None, cl_features=None, drug_features=None, model_checkpoint_dir='TEMPORARY')

Train the model and predict the response for the prediction dataset.

Parameters:
  • model (DRPModel) – model to use, e.g., SimpleNeuralNetwork

  • hpams (dict) – hyperparameters to use

  • path_data (str) – path to the data directory, e.g., data/

  • train_dataset (DrugResponseDataset) – training dataset

  • prediction_dataset (DrugResponseDataset) – prediction dataset

  • early_stopping_dataset (DrugResponseDataset | None) – early stopping dataset, optional

  • response_transformation (TransformerMixin | None) – normalizer to use for the response data, e.g., StandardScaler

  • cl_features (FeatureDataset | None) – cell line features

  • drug_features (FeatureDataset | None) – drug features

  • model_checkpoint_dir (str) – directory for model checkpoints, if “TEMPORARY”, checkpoints are not saved. Default is “TEMPORARY”

Return type:

DrugResponseDataset

Returns:

prediction dataset with predictions

Raises:

ValueError – if train_dataset does not have a dataset_name

drevalpy.experiment.train_final_model(model_class, full_dataset, response_transformation, path_data, model_checkpoint_dir, metric, final_model_path, drug_id=None, test_mode='LCO', val_ratio=0.1, hyperparameter_tuning=True)

Final Production Model Training.

Tune a final model on the full data set using a validation split that reflects intended generalization. No test set is used here. The performance during the nested CV is a pessimistic estimate of the final model performance. The validation split strategy is determined by test_mode: - LCO: generalization to unseen cell lines (e.g., personalized medicine) - LDO: generalization to new drugs (e.g., drug repurposing) - LTO: generalization to new tissues - LPO: general (pair-level) prediction

Parameters:
  • model_class (type[DRPModel]) – model to use

  • full_dataset (DrugResponseDataset) – full training dataset (union of outer folds)

  • response_transformation (TransformerMixin | None) – sklearn scaler used for response normalization, None for no transformation

  • path_data (str) – path to data directory

  • model_checkpoint_dir (str) – checkpoint dir for intermediate tuning models

  • metric (str) – metric for tuning, e.g., “RMSE”

  • final_model_path (str) – path to final_model save directory

  • drug_id (str | None) – drug id for single drug models. The dataset is reduced to this drug, analogous to get_datasets_from_cv_split, because a single drug model is fitted per drug.

  • test_mode (str) – split logic for validation (LCO, LDO, LTO, LPO)

  • val_ratio (float) – validation size ratio

  • hyperparameter_tuning (bool) – whether to perform hyperparameter tuning

Raises:

ValueError – if a single drug model is trained without a drug id

Return type:

None

Evaluation functions

Functions for evaluating model performance.

drevalpy.evaluation.evaluate(dataset, metric)

Evaluates the model on the given dataset.

Parameters:
  • dataset (DrugResponseDataset) – dataset to evaluate on

  • metric (list[str] | str) – evaluation metric(s) (one or a list of “MSE”, “RMSE”, “MAE”, “R^2”, “Pearson”, “spearman”, “kendall”)

Returns:

evaluation metric

Raises:

AssertionError – if metric is not in AVAILABLE

drevalpy.evaluation.get_mode(metric)

Get whether the optimum value of the metric is the minimum or maximum.

Parameters:

metric (str) – metric, e.g., RMSE

Returns:

whether the optimum value of the metric is the minimum or maximum

Raises:

ValueError – if the metric is not in MINIMIZATION_METRICS or MAXIMIZATION_METRICS

drevalpy.evaluation.kendall(y_pred, y_true)

Computes the kendall tau correlation between predictions and response.

Parameters:
Return type:

float

Returns:

kendall tau correlation float

Raises:

AssertionError – if predictions and response do not have the same length

drevalpy.evaluation.pearson(y_pred, y_true)

Computes the pearson correlation between predictions and response.

Parameters:
Return type:

float

Returns:

pearson correlation float

Raises:

AssertionError – if predictions and response do not have the same length

drevalpy.evaluation.spearman(y_pred, y_true)

Computes the spearman correlation between predictions and response.

Parameters:
Return type:

float

Returns:

spearman correlation float

Raises:

AssertionError – if predictions and response do not have the same length

Utility functions

Utility functions for the evaluation pipeline.

drevalpy.utils.check_arguments(args)

Check the validity of the arguments for the evaluation pipeline.

Parameters:

args – arguments passed from the command line

Raises:
  • AssertionError – if any of the arguments is invalid

  • ValueError – if the number of cross-validation splits or curve_curator_cores is less than 1

  • FileNotFoundError – if a custom dataset name was specified and the input file could not be found.

Return type:

None

drevalpy.utils.get_datasets(dataset_name, cross_study_datasets, path_data='data', measure='response', curve_curator=False, cores=1, normalize=False, clean_min_responders=None, clean_min_responder_frac=None)

Load the response data and cross-study datasets.

Parameters:
  • dataset_name (str) – The name of the dataset to load. Can be one of (‘GDSC1’, ‘GDSC2’, ‘CCLE’, CTRPv1’, ‘CTRPv2’, ‘TOYv1’, ‘TOYv2’) to download provided datasets, or any other name to use a custom datasets.

  • cross_study_datasets (list) – list of cross-study datasets. CurveCurator is not applicable to these. If you wish to provide custom cross_study_datasets, you have to invoke curve fitting manually using drevalpy.datasets.curvecurator.fit_curves

  • path_data (str) – The parent path in which custom or downloaded datasets should be located, or in which raw viability data is to be found for fitting with CurveCurator (see param curve_curator for details). The location of the datasets are resolved by <path_data>/<dataset_name>/<dataset_name>.csv.

  • measure (str) – The name of the column containing the measure to predict, default = “response”. If curve_curator is True, this measure is appended with “_curvecurator”, e.g. “response_curvecurator” to distinguish between measures provided by the original source of a dataset, or the measures fit by CurveCurator.

  • curve_curator (bool) – If True, the measure is appended with “_curvecurator”. If a custom dataset_name was provided, this will invoke the fitting procedure of raw viability data, which is expected to exist at <path_data>/<dataset_name>/<dataset_name>_raw.csv. The fitted dataset will be stored in the same folder, in a file called <dataset_name>.csv

  • cores (int) – Number of cores to use for CurveCurator fitting. Only used when curve_curator is True, default = 1

  • normalize (bool) – Whether to normalize the response values to [0, 1] for curvecurator. Default = False. Only used for custom datasets when curve_curator is True.

  • clean_min_responders (int | None) – If set, keep only drugs with at least this many reproducible responder curves in the main dataset (see drevalpy.datasets.loader.load_dataset). Cross-study datasets are left unfiltered.

  • clean_min_responder_frac (float | None) – Fraction-based alternative to clean_min_responders (see drevalpy.datasets.loader.load_dataset). Set at most one of the two.

Return type:

tuple[DrugResponseDataset, list[DrugResponseDataset] | None]

Returns:

response data and, potentially, cross-study datasets

drevalpy.utils.get_response_transformation(response_transformation)

Get the skelarn response transformation object of choice.

Users can choose from “None”, “standard”, “minmax”, “robust”, “drug_mean”, “drug_tissue_mean”.

While the first three are global monotone rescalings, “drug_mean” subtracts the per-drug mean of the training fold (target residualization) and adds it back to the predictions. “drug_tissue_mean” does the same per drug and tissue, falling back to the per-drug mean for (drug, tissue) combinations that did not occur in the training fold.

Parameters:

response_transformation (str | None) – response transformation to apply

Return type:

TransformerMixin | None

Returns:

response transformation object

Raises:

ValueError – if the response transformation is not recognized

drevalpy.utils.main(args)

Main function to run the drug response evaluation pipeline.

Parameters:

args – passed from command line

Return type:

None

Response transformations

Group-aware response transformations (conditional-mean residualization).

The transformations shipped with drevalpy (“standard”, “minmax”, “robust”) are global monotone rescalings of the response. Because the evaluation metrics of interest are rank-based (Spearman) and additionally mean-centered per drug and per cell line (“normalized” metrics), such global rescalings cannot change the reported scores.

GroupMeanCenterer is different: it subtracts a conditional mean (by default the per-drug mean, estimated on the training fold only) from the training target, so the model spends its capacity on the residual drug x cell line structure instead of on the drug main effect. At prediction time the group mean is added back, so predictions are returned on the original response scale.

The group can be built from more than one field, e.g. drug and tissue. The fields are ordered from coarse to fine, and the estimated means are nested: a sample whose (drug, tissue) combination was not seen during fit falls back to the mean of its drug, and only a sample whose drug is unknown as well falls back to the global training mean.

class drevalpy.response_transformation.GroupMeanCenterer(group_fields=('drug_ids',))

Bases: BaseEstimator, TransformerMixin

Subtract the mean response of the group a sample belongs to (usually its drug).

The class follows the sklearn transformer protocol, but its fit/transform/ inverse_transform methods accept an additional groups argument. The requires_groups class attribute tells DrugResponseDataset to supply the columns named in group_fields for that argument.

With more than one group field the means are nested, from the most specific combination down to the global mean: with group_fields=("drug_ids", "tissue") a sample is centered on the mean of its (drug, tissue) combination if that combination occurred during fit, otherwise on the mean of its drug, otherwise on the global training mean. This keeps the transformer safe for LDO (unseen drugs), LTO (unseen tissues, where it degenerates to per-drug centering) and cross-study prediction.

Parameters:

group_fields (tuple[str, ...])

fit(X, y=None, groups=None)

Estimate the global mean and the mean of every group and parent group.

Parameters:
  • X – response values, shape (n,) or (n, 1)

  • y – ignored, present for sklearn compatibility

  • groups – group label per sample, shape (n,) or (n, n_group_fields). If None, only the global mean is estimated and the transformer degenerates to plain mean-centering.

Return type:

GroupMeanCenterer

Returns:

the fitted transformer

inverse_transform(X, groups=None)

Add the group mean back, returning values on the original response scale.

Parameters:
  • X – residual values

  • groups – group label per sample, e.g. drug ids

Return type:

ndarray

Returns:

values on the original scale, same shape as X

level_keys_: list[ndarray]

keys per nesting level, most specific level first

level_means_: list[ndarray]

mean per key, aligned with level_keys_

requires_groups = True
set_fit_request(*, groups='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters

groupsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for groups parameter in fit.

Returns

selfobject

The updated object.

Parameters:
Return type:

GroupMeanCenterer

set_inverse_transform_request(*, groups='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the inverse_transform method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to inverse_transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to inverse_transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters

groupsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for groups parameter in inverse_transform.

Returns

selfobject

The updated object.

Parameters:
Return type:

GroupMeanCenterer

set_transform_request(*, groups='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the transform method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters

groupsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for groups parameter in transform.

Returns

selfobject

The updated object.

Parameters:
Return type:

GroupMeanCenterer

transform(X, groups=None)

Subtract the group mean.

Parameters:
  • X – response or prediction values

  • groups – group label per sample, e.g. drug ids

Return type:

ndarray

Returns:

residuals, same shape as X

Pipeline function decorator

Decorator to mark a function as a pipeline function.

drevalpy.pipeline_function.pipeline_function(func)

Decorator to mark a function as a pipeline function.

Parameters:

func – function to decorate

Returns:

function with custom attribute

Command line interface

Root drevalpy command (full experiment pipeline).

drevalpy.cli.pipeline.register_pipeline_callback(app)

Register the default callback that runs the full pipeline when no subcommand is given.

Return type:

None

Parameters:

app (Typer)

For the nf-core/drugresponseeval subworkflow run_cv.

drevalpy.cli_run_cv.run_cv_split(*, response, n_cv_splits, test_mode='LPO', validation_ratio=0.1, seed=42, custom_splitter_path=None)

Split pickled response data into CV fold pickles.

Return type:

None

Parameters:
  • response (str)

  • n_cv_splits (int)

  • test_mode (str)

  • validation_ratio (float)

  • seed (int)

  • custom_splitter_path (str | None)

drevalpy.cli_run_cv.run_evaluate_and_find_max(*, model_name, split_id, hpam_yamls, pred_datas, optim_metric='RMSE')

Pick the best hyperparameter YAML for one CV split, for single drug models per drug.

Return type:

None

Parameters:
drevalpy.cli_run_cv.run_hpam_split(*, model_name, hyperparameter_tuning=False)

Write one YAML per hyperparameter combination for a model.

Return type:

None

Parameters:
  • model_name (str)

  • hyperparameter_tuning (bool)

drevalpy.cli_run_cv.run_load_response(*, response_dataset, cross_study_dataset=False, measure='LN_IC50_curvecurator', clean_min_responders=None, clean_min_responder_frac=None)

Load drug response CSV and pickle a DrugResponseDataset.

If clean_min_responders or clean_min_responder_frac is set, whole drugs failing the responder criterion are removed (needs a curve-curated CSV with a Regulation column). The dataset keeps its base name so that downstream steps still find its feature files.

Return type:

None

Parameters:
  • response_dataset (str)

  • cross_study_dataset (bool)

  • measure (str)

  • clean_min_responders (int | None)

  • clean_min_responder_frac (float | None)

drevalpy.cli_run_cv.run_train_and_predict_cv(*, model_name, path_data='data', test_mode='LPO', hyperparameters, cv_data, response_transformation='None', model_checkpoint_dir='TEMPORARY', n_jobs=1)

Train on a CV split and pickle validation predictions, for single drug models a {drug_id: predictions} dict.

Return type:

None

Parameters:
  • model_name (str)

  • path_data (str)

  • test_mode (str)

  • hyperparameters (str)

  • cv_data (str)

  • response_transformation (str)

  • model_checkpoint_dir (str)

  • n_jobs (int)

For the nf-core/drugresponseeval subworkflow model_testing.

drevalpy.cli_model_testing.run_collect_results(*, outfiles, path_data='data')

Collect parallel Nextflow evaluation outputs into merged CSVs.

Return type:

None

Parameters:
drevalpy.cli_model_testing.run_consolidate_results(*, run_id, test_mode='LPO', model_name, outdir_path, n_cv_splits, cross_study_datasets=None, randomization_modes='[None]', n_trials_robustness=0)

Consolidate single-drug model prediction outputs.

Return type:

None

Parameters:
  • run_id (str)

  • test_mode (str)

  • model_name (str)

  • outdir_path (str)

  • n_cv_splits (int)

  • cross_study_datasets (list[str] | None)

  • randomization_modes (str)

  • n_trials_robustness (int)

drevalpy.cli_model_testing.run_evaluate_test_results(*, test_mode='LPO', model_name, pred_file)

Evaluate test predictions and write metric CSVs.

Return type:

None

Parameters:
  • test_mode (str)

  • model_name (str)

  • pred_file (str)

drevalpy.cli_model_testing.run_final_split(*, response, model_name, path_data='data', test_mode='LPO', val_ratio=0.1)

Create train/validation/early-stopping pickles for a final production model.

For single drug models, each pickle is a {drug_id: dataset} dict, split per drug.

Return type:

None

Parameters:
drevalpy.cli_model_testing.run_randomization_split(*, model_name, randomization_mode)

Create randomization test view YAML files for a model.

Return type:

None

Parameters:
  • model_name (str)

  • randomization_mode (str)

drevalpy.cli_model_testing.run_train_and_predict_final(*, mode='full', model_name, split_id, split_dataset_path, hyperparameters_path, response_transformation='None', test_mode='LPO', path_data='data', randomization_views_path=None, randomization_type='permutation', robustness_trial=None, cross_study_datasets=None, model_checkpoint_dir='TEMPORARY', n_jobs=1)

Train and predict on the CV test set (full, randomization, or robustness mode).

Single drug models are trained per drug, n_jobs drugs in parallel. Their predictions are saved per drug and consolidated over all drugs.

Return type:

None

Parameters:
  • mode (str)

  • model_name (str)

  • split_id (str)

  • split_dataset_path (str)

  • hyperparameters_path (str)

  • response_transformation (str)

  • test_mode (str)

  • path_data (str)

  • randomization_views_path (str | None)

  • randomization_type (str)

  • robustness_trial (int | None)

  • cross_study_datasets (list[str] | None)

  • model_checkpoint_dir (str)

  • n_jobs (int)

drevalpy.cli_model_testing.run_train_final_model(*, train_data, val_data, early_stopping_data, response_transformation='None', model_name, path_data='data', model_checkpoint_dir='TEMPORARY', best_hpam_combi, n_jobs=1)

Train and save the final production model. Single drug models: n_jobs drugs in parallel.

Return type:

None

Parameters:
  • train_data (str)

  • val_data (str)

  • early_stopping_data (str)

  • response_transformation (str)

  • model_name (str)

  • path_data (str)

  • model_checkpoint_dir (str)

  • best_hpam_combi (str)

  • n_jobs (int)

drevalpy.cli_model_testing.run_tune_final_model(*, train_data, val_data, early_stopping_data, model_name, hpam_combi, response_transformation='None', path_data='data', model_checkpoint_dir='TEMPORARY', n_jobs=1)

Tune hyperparameters for the final model on full data. Single drug models: n_jobs drugs in parallel.

Return type:

None

Parameters:
  • train_data (str)

  • val_data (str)

  • early_stopping_data (str)

  • model_name (str)

  • hpam_combi (str)

  • response_transformation (str)

  • path_data (str)

  • model_checkpoint_dir (str)

  • n_jobs (int)

For the nf-core/drugresponseeval subworkflow preprocess_custom.

drevalpy.cli_preprocess_custom.run_postprocess_viability(*, dataset_name, path_data='./')

Postprocess CurveCurator output into a single dataset CSV.

Return type:

None

Parameters:
  • dataset_name (str)

  • path_data (str)

drevalpy.cli_preprocess_custom.run_preprocess_raw_viability(*, path_data='./data', dataset_name, cores=4)

Preprocess raw viability data with CurveCurator.

Return type:

None

Parameters:
  • path_data (str)

  • dataset_name (str)

  • cores (int)