-
Notifications
You must be signed in to change notification settings - Fork 4
adds single drug elastic net #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
df25c32
adds single drug elastic net
PascalIversen 3501198
better late than never
PascalIversen f7d50d1
fixes mypy
PascalIversen 0967071
fixes tzpeguard
PascalIversen cd6a08d
fix tests
PascalIversen ae91483
Merge branch 'development' into SingleDrugElastic
PascalIversen 127b94c
pre-commit fix isort
PascalIversen 830b6c4
data fix
PascalIversen eb492bc
dataset length test
PascalIversen bc2127e
proteomics removed from multiomics
PascalIversen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| """SingleDrugElasticNet and SingleDrugProteomicsElasticNet classes. Fit an Elastic net for each drug seperately.""" | ||
|
|
||
| import numpy as np | ||
| from sklearn.linear_model import ElasticNet | ||
|
|
||
| from ...datasets.dataset import DrugResponseDataset, FeatureDataset | ||
| from ..utils import load_and_reduce_gene_features | ||
| from .sklearn_models import SklearnModel | ||
|
|
||
|
|
||
| class SingleDrugElasticNet(SklearnModel): | ||
| """SingleDrugElasticNet class.""" | ||
|
|
||
| is_single_drug_model = True | ||
| drug_views = [] | ||
| cell_line_views = ["gene_expression"] | ||
| early_stopping = False | ||
|
|
||
| def build_model(self, hyperparameters): | ||
| """ | ||
| Builds the model from hyperparameters. | ||
|
|
||
| :param hyperparameters: Elastic net hyperparameters | ||
| """ | ||
| self.model = ElasticNet(**hyperparameters) | ||
|
|
||
| @classmethod | ||
| def get_model_name(cls) -> str: | ||
| """ | ||
| Returns the model name. | ||
|
|
||
| :returns: SingleDrugElasticNet | ||
| """ | ||
| return "SingleDrugElasticNet" | ||
|
|
||
| def train( | ||
| self, | ||
| output: DrugResponseDataset, | ||
| cell_line_input: FeatureDataset, | ||
| drug_input: FeatureDataset | None = None, | ||
| output_earlystopping: DrugResponseDataset | None = None, | ||
| model_checkpoint_dir: str = "checkpoints", | ||
| ) -> None: | ||
| """ | ||
| Trains the model; the number of features is the number of fingerprints. | ||
|
|
||
| :param output: training dataset containing the response output | ||
| :param cell_line_input: training dataset containing gene expression data | ||
| :param drug_input: not needed | ||
| :param output_earlystopping: not needed | ||
| :param model_checkpoint_dir: not needed as checkpoints are not saved | ||
| :raises ValueError: if drug_input is not None | ||
| """ | ||
| if drug_input is not None: | ||
| raise ValueError("SingleDrugElasticNet does not support drug_input!") | ||
|
|
||
| if len(output) > 0: | ||
| x = self.get_concatenated_features( | ||
| cell_line_view="gene_expression", | ||
| drug_view=None, | ||
| cell_line_ids_output=output.cell_line_ids, | ||
| drug_ids_output=output.drug_ids, | ||
| cell_line_input=cell_line_input, | ||
| drug_input=None, | ||
| ) | ||
| self.model.fit(x, output.response) | ||
| else: | ||
| print("No training data provided, will predict NA.") | ||
| self.model = None | ||
|
|
||
| def predict( | ||
| self, | ||
| cell_line_ids: np.ndarray, | ||
| drug_ids: np.ndarray, | ||
| cell_line_input: FeatureDataset, | ||
| drug_input: FeatureDataset | None = None, | ||
| ) -> np.ndarray: | ||
| """ | ||
| Predicts the drug response for the given cell lines. | ||
|
|
||
| :param cell_line_ids: cell line ids | ||
| :param drug_ids: drug ids, not needed here | ||
| :param cell_line_input: cell line input | ||
| :param drug_input: drug input, not needed here | ||
| :returns: predicted drug response | ||
| :raises ValueError: if drug_input is not None | ||
| """ | ||
| if drug_input is not None: | ||
| raise ValueError("drug_input is not needed.") | ||
|
|
||
| if self.model is None: | ||
| print("No training data was available, predicting NA.") | ||
| return np.array([np.nan] * len(cell_line_ids)) | ||
| x = self.get_concatenated_features( | ||
| cell_line_view="gene_expression", | ||
| drug_view=None, | ||
| cell_line_ids_output=cell_line_ids, | ||
| drug_ids_output=drug_ids, | ||
| cell_line_input=cell_line_input, | ||
| drug_input=None, | ||
| ) | ||
| return self.model.predict(x) | ||
|
|
||
| def load_drug_features(self, data_path, dataset_name): | ||
| """ | ||
| Load drug features. Not needed for SingleDrugElasticNet. | ||
|
|
||
| :param data_path: path to the data | ||
| :param dataset_name: name of the dataset | ||
| :returns: None | ||
| """ | ||
| return None | ||
|
|
||
|
|
||
| class SingleDrugProteomicsElasticNet(SingleDrugElasticNet): | ||
| """SingleDrugProteomicsElasticNet class.""" | ||
|
|
||
| cell_line_views = ["proteomics"] | ||
| is_single_drug_model = True | ||
|
|
||
| @classmethod | ||
| def get_model_name(cls) -> str: | ||
| """ | ||
| Returns the model name. | ||
|
|
||
| :returns: SingleDrugProteomicsElasticNet | ||
| """ | ||
| return "SingleDrugProteomicsElasticNet" | ||
|
|
||
| def load_cell_line_features(self, data_path: str, dataset_name: str) -> FeatureDataset: | ||
| """ | ||
| Loads the proteomics data. | ||
|
|
||
| :param data_path: path to the data | ||
| :param dataset_name: name of the dataset | ||
| :returns: proteomics data | ||
| """ | ||
| return load_and_reduce_gene_features( | ||
| feature_type="proteomics", | ||
| gene_list=None, | ||
| data_path=data_path, | ||
| dataset_name=dataset_name, | ||
| ) | ||
|
|
||
| def load_drug_features(self, data_path, dataset_name): | ||
| """ | ||
| Load drug features. Not needed for SingleDrugProteomicsElasticNet. | ||
|
|
||
| :param data_path: path to the data | ||
| :param dataset_name: name of the dataset | ||
| :returns: None | ||
| """ | ||
| return None | ||
|
|
||
| def train( | ||
| self, | ||
| output: DrugResponseDataset, | ||
| cell_line_input: FeatureDataset, | ||
| drug_input: FeatureDataset | None = None, | ||
| output_earlystopping: DrugResponseDataset | None = None, | ||
| model_checkpoint_dir: str = "checkpoints", | ||
| ) -> None: | ||
| """ | ||
| Trains the model; the number of features is the number of fingerprints. | ||
|
|
||
| :param output: training dataset containing the response output | ||
| :param cell_line_input: training dataset containing gene expression data | ||
| :param drug_input: not needed | ||
| :param output_earlystopping: not needed | ||
| :param model_checkpoint_dir: not needed as checkpoints are not saved | ||
| :raises ValueError: if drug_input is not None | ||
| """ | ||
| if drug_input is not None: | ||
| raise ValueError("SingleDrugElasticNet does not support drug_input!") | ||
|
|
||
| if len(output) > 0: | ||
| x = self.get_concatenated_features( | ||
| cell_line_view="proteomics", | ||
| drug_view=None, | ||
| cell_line_ids_output=output.cell_line_ids, | ||
| drug_ids_output=output.drug_ids, | ||
| cell_line_input=cell_line_input, | ||
| drug_input=None, | ||
| ) | ||
| self.model.fit(x, output.response) | ||
| else: | ||
| print("No training data provided, will predict NA.") | ||
| self.model = None | ||
|
|
||
| def predict( | ||
| self, | ||
| cell_line_ids: np.ndarray, | ||
| drug_ids: np.ndarray, | ||
| cell_line_input: FeatureDataset, | ||
| drug_input: FeatureDataset | None = None, | ||
| ) -> np.ndarray: | ||
| """ | ||
| Predicts the drug response for the given cell lines. | ||
|
|
||
| :param cell_line_ids: cell line ids | ||
| :param drug_ids: drug ids, not needed here | ||
| :param cell_line_input: cell line input | ||
| :param drug_input: drug input, not needed here | ||
| :returns: predicted drug response | ||
| :raises ValueError: if drug_input is not None | ||
| """ | ||
| if drug_input is not None: | ||
| raise ValueError("drug_input is not needed.") | ||
|
|
||
| if self.model is None: | ||
| print("No training data was available, predicting NA.") | ||
| return np.array([np.nan] * len(cell_line_ids)) | ||
| x = self.get_concatenated_features( | ||
| cell_line_view="proteomics", | ||
| drug_view=None, | ||
| cell_line_ids_output=cell_line_ids, | ||
| drug_ids_output=drug_ids, | ||
| cell_line_input=cell_line_input, | ||
| drug_input=None, | ||
| ) | ||
| return self.model.predict(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.