### Install ChemicalX via pip Source: https://github.com/astrazeneca/chemicalx/blob/main/docs/source/notes/installation.md Standard installation command for the ChemicalX library. ```shell $ pip install chemicalx ``` -------------------------------- ### Install ChemicalX in Development Mode Source: https://github.com/astrazeneca/chemicalx/blob/main/CONTRIBUTING.md Install ChemicalX in development mode, which also sets up pre-commit hooks. ```bash ./dev_setup.sh ``` -------------------------------- ### Install ChemicalX Dependencies Source: https://github.com/astrazeneca/chemicalx/blob/main/README.md Install required PyTorch packages and ChemicalX. Replace ${CUDA} with the appropriate environment identifier. ```sh pip install torch-scatter -f https://pytorch-geometric.com/whl/torch-1.10.0+${CUDA}.html pip install torchdrug pip install chemicalx ``` -------------------------------- ### Install ChemicalX with PyTorch 1.10.0 Source: https://github.com/astrazeneca/chemicalx/blob/main/docs/source/notes/installation.md Install required torch-scatter binaries and the ChemicalX package for PyTorch 1.10.0 environments. ```shell $ pip install torch-scatter -f https://pytorch-geometric.com/whl/torch-1.10.0+${CUDA}.html $ pip install torchdrug $ pip install chemicalx ``` -------------------------------- ### Verify ChemicalX Version Source: https://github.com/astrazeneca/chemicalx/blob/main/docs/source/notes/installation.md Check the currently installed version of the ChemicalX package. ```shell $ pip freeze | grep chemicalx ``` -------------------------------- ### Upgrade ChemicalX Source: https://github.com/astrazeneca/chemicalx/blob/main/docs/source/notes/installation.md Update an existing installation of ChemicalX to the latest version. ```shell $ pip install chemicalx --upgrade ``` -------------------------------- ### Build Documentation Source: https://github.com/astrazeneca/chemicalx/blob/main/CONTRIBUTING.md Build the project documentation to ensure it renders correctly. ```bash tox -e docs ``` -------------------------------- ### Train and Evaluate Models with ChemicalX Pipeline Source: https://github.com/astrazeneca/chemicalx/blob/main/README.md Initialize a model and dataset, then execute the training pipeline to generate performance metrics. ```python from chemicalx import pipeline from chemicalx.models import DeepSynergy from chemicalx.data import DrugCombDB model = DeepSynergy(context_channels=112, drug_channels=256) dataset = DrugCombDB() results = pipeline( dataset=dataset, model=model, # Data arguments batch_size=5120, context_features=True, drug_features=True, drug_molecules=False, # Training arguments epochs=100, ) # Outputs information about the AUC-ROC, etc. to the console. results.summarize() ``` -------------------------------- ### Implementing a custom training loop Source: https://context7.com/astrazeneca/chemicalx/llms.txt Shows how to manually manage the training and evaluation process using ChemicalX models and PyTorch components. ```python import torch from chemicalx.data import DrugCombDB from chemicalx.models import DeepSynergy # Setup device = torch.device("cuda" if torch.cuda.is_available() else "cpu") dataset = DrugCombDB() model = DeepSynergy( context_channels=dataset.context_channels, drug_channels=dataset.drug_channels, ).to(device) optimizer = torch.optim.Adam(model.parameters(), lr=0.001) loss_fn = torch.nn.BCELoss() # Get generators train_gen, test_gen = dataset.get_generators( batch_size=512, context_features=True, drug_features=True, drug_molecules=False, ) # Training loop model.train() for epoch in range(10): epoch_loss = 0.0 for batch in train_gen: batch = batch.to(device) optimizer.zero_grad() # Unpack batch using model's unpack method inputs = model.unpack(batch) predictions = model(*inputs) loss = loss_fn(predictions, batch.labels) loss.backward() optimizer.step() epoch_loss += loss.item() print(f"Epoch {epoch+1}: Loss = {epoch_loss/len(train_gen):.4f}") # Evaluation model.eval() all_predictions = [] all_labels = [] with torch.no_grad(): for batch in test_gen: batch = batch.to(device) inputs = model.unpack(batch) predictions = model(*inputs) all_predictions.extend(predictions.cpu().numpy().flatten()) all_labels.extend(batch.labels.cpu().numpy().flatten()) from sklearn.metrics import roc_auc_score auc = roc_auc_score(all_labels, all_predictions) print(f"Test AUC-ROC: {auc:.4f}") ``` -------------------------------- ### Run Tests Source: https://github.com/astrazeneca/chemicalx/blob/main/README.md Execute the test suite using tox. ```sh $ tox -e py ``` -------------------------------- ### Initialize and Train MatchMaker Model Source: https://context7.com/astrazeneca/chemicalx/llms.txt Sets up the MatchMaker model for drug synergy prediction, utilizing both drug and context features. ```python from chemicalx import pipeline from chemicalx.data import DrugCombDB from chemicalx.models import MatchMaker dataset = DrugCombDB() model = MatchMaker( context_channels=dataset.context_channels, drug_channels=dataset.drug_channels, input_hidden_channels=32, middle_hidden_channels=32, final_hidden_channels=32, out_channels=1, dropout_rate=0.5, ) results = pipeline( dataset=dataset, model=model, batch_size=5120, epochs=100, context_features=True, drug_features=True, drug_molecules=False, metrics=["roc_auc"], ) results.summarize() ``` -------------------------------- ### Run Unit Tests Source: https://github.com/astrazeneca/chemicalx/blob/main/CONTRIBUTING.md Execute the unit tests for the project using tox. Ensure complete test coverage. ```bash tox -e py ``` -------------------------------- ### Lint Documentation with Doc8 Source: https://github.com/astrazeneca/chemicalx/blob/main/CONTRIBUTING.md Check documentation for style and formatting errors using Doc8. ```bash tox -e doc8 ``` -------------------------------- ### Load and Summarize DrugCombDB Dataset Source: https://context7.com/astrazeneca/chemicalx/llms.txt Initializes the DrugCombDB dataset and retrieves feature dimensions for model configuration. ```python from chemicalx.data import DrugCombDB dataset = DrugCombDB() # Get dataset statistics dataset.summarize() # Output: # Name: DrugCombDB # Contexts: 112 # Context Feature Size: 954 # Drugs: 764 # Drug Feature Size: 256 # Triples: 191049 # Access feature dimensions for model initialization print(f"Context channels: {dataset.context_channels}") print(f"Drug channels: {dataset.drug_channels}") print(f"Number of drugs: {dataset.num_drugs}") print(f"Number of contexts: {dataset.num_contexts}") # Get batch generators for training/testing train_gen, test_gen = dataset.get_generators( batch_size=512, context_features=True, drug_features=True, drug_molecules=False, train_size=0.8, random_state=42, ) ``` -------------------------------- ### Load Datasets with Dataset Loaders Source: https://github.com/astrazeneca/chemicalx/blob/main/docs/source/notes/introduction.md Initialize a dataset loader and retrieve feature sets and labeled triples. ```python from chemicalx.data import DrugCombDB loader = DrugCombDB() context_set = loader.get_context_features() drug_set = loader.get_drug_features() triples = loader.get_labeled_triples() ``` -------------------------------- ### Load DrugCombDB and Initialize BatchGenerator Source: https://github.com/astrazeneca/chemicalx/blob/main/docs/source/notes/tutorial.md Instantiates the DrugCombDB loader and configures a BatchGenerator for training data with specified batch size and feature settings. ```python from chemicalx.data import DrugCombDB, BatchGenerator loader = DrugCombDB() context_set = loader.get_context_features() drug_set = loader.get_drug_features() triples = loader.get_labeled_triples() train, test = triples.train_test_split(train_size=0.5) generator = BatchGenerator(batch_size=1024, context_features=True, drug_features=True, drug_molecules=False, context_feature_set=context_set, drug_feature_set=drug_set, labeled_triples=train) ``` -------------------------------- ### Configure BatchGenerator for Training Source: https://context7.com/astrazeneca/chemicalx/llms.txt Initializes a BatchGenerator to produce DrugPairBatch objects, including GPU transfer support. ```python from chemicalx.data import DrugCombDB, BatchGenerator dataset = DrugCombDB() labeled_triples = dataset.get_labeled_triples() generator = BatchGenerator( batch_size=512, context_features=True, drug_features=True, drug_molecules=False, context_feature_set=dataset.get_context_features(), drug_feature_set=dataset.get_drug_features(), labeled_triples=labeled_triples, ) print(f"Number of batches: {len(generator)}") for batch in generator: # batch is a DrugPairBatch dataclass print(f"Labels shape: {batch.labels.shape}") print(f"Left drug features: {batch.drug_features_left.shape}") print(f"Right drug features: {batch.drug_features_right.shape}") print(f"Context features: {batch.context_features.shape}") # Move to GPU if available batch = batch.to("cuda") break ``` -------------------------------- ### Accessing DrugPairBatch components Source: https://context7.com/astrazeneca/chemicalx/llms.txt Demonstrates how to iterate through data generators and access batch components like features, labels, and molecular graphs. ```python from chemicalx.data import DrugCombDB import torch dataset = DrugCombDB() train_gen, test_gen = dataset.get_generators( batch_size=256, context_features=True, drug_features=True, drug_molecules=True, train_size=0.8, ) for batch in train_gen: # Access batch components print(f"Identifiers: {batch.identifiers.columns.tolist()}") # ['drug_1', 'drug_2', 'context', 'label'] print(f"Drug features left: {batch.drug_features_left.shape}") print(f"Drug features right: {batch.drug_features_right.shape}") print(f"Context features: {batch.context_features.shape}") print(f"Labels: {batch.labels.shape}") # Molecular graphs (when drug_molecules=True) if batch.drug_molecules_left is not None: print(f"Left molecules: {batch.drug_molecules_left}") # Move entire batch to device device = torch.device("cuda" if torch.cuda.is_available() else "cpu") batch = batch.to(device) break ``` -------------------------------- ### Iterate TwoSides Dataset Batches Source: https://context7.com/astrazeneca/chemicalx/llms.txt Demonstrates manual iteration over batches from the TwoSides dataset. ```python from chemicalx.data import TwoSides dataset = TwoSides() dataset.summarize() # Get feature sets directly context_features = dataset.get_context_features() drug_features = dataset.get_drug_features() # Iterate through batches manually generator = dataset.get_generator( batch_size=256, context_features=True, drug_features=True, drug_molecules=False, ) for batch in generator: print(f"Batch labels shape: {batch.labels.shape}") print(f"Drug features left: {batch.drug_features_left.shape}") break ``` -------------------------------- ### Configuring CASTERSupervisedLoss Source: https://context7.com/astrazeneca/chemicalx/llms.txt Initializes the CASTER loss function with specific regularization coefficients for training. ```python from chemicalx.loss import CASTERSupervisedLoss import torch loss_fn = CASTERSupervisedLoss( recon_loss_coeff=0.1, # Reconstruction loss coefficient proj_coeff=0.1, # Projection loss coefficient lambda1=0.01, # L1 regularization for sparse code lambda2=0.1, # Frobenius norm regularization ) # The loss function expects a tuple from CASTER.forward(): # (prediction_scores, reconstructed, dictionary_encoded, # dictionary_features_latent, drug_pair_features_latent, drug_pair_features) # and target labels # Example usage in training # model_output = model(drug_pair_features) # loss = loss_fn(model_output, batch.labels) ``` -------------------------------- ### Clone the Repository Source: https://github.com/astrazeneca/chemicalx/blob/main/CONTRIBUTING.md Clone your forked repository to your local machine. Replace `` with your actual GitHub username. ```bash git clone git@github.com:/chemicalx.git ``` -------------------------------- ### Initialize and Train MR-GNN Model Source: https://context7.com/astrazeneca/chemicalx/llms.txt Sets up the MR-GNN model, which requires drug_molecules to be enabled for graph-based processing. ```python from chemicalx import pipeline from chemicalx.data import DrugCombDB from chemicalx.models import MRGNN dataset = DrugCombDB() model = MRGNN( molecule_channels=69, # TorchDrug default node features hidden_channels=32, # Graph convolutional filters middle_channels=16, # Bottleneck layer neurons layer_count=4, # Number of GCN/RNN blocks out_channels=1, # Output dimension ) results = pipeline( dataset=dataset, model=model, optimizer_kwargs=dict(lr=0.01, weight_decay=1e-7), batch_size=1024, epochs=10, context_features=True, drug_features=True, drug_molecules=True, # Required for graph-based models ) results.summarize() ``` -------------------------------- ### Check Documentation Coverage Source: https://github.com/astrazeneca/chemicalx/blob/main/CONTRIBUTING.md Assess the coverage of docstrings within the project's documentation. ```bash tox -e docstr-coverage ``` -------------------------------- ### Train DeepSynergy Model Source: https://github.com/astrazeneca/chemicalx/blob/main/docs/source/notes/tutorial.md Sets up the DeepSynergy model, optimizer, and loss function, then performs a single training epoch over the generated batches. ```python import torch from chemicalx.models import DeepSynergy model = DeepSynergy(context_channels=112, drug_channels=256) optimizer = torch.optim.Adam(model.parameters()) model.train() loss = torch.nn.BCELoss() for batch in generator: optimizer.zero_grad() prediction = model(batch.context_features, batch.drug_features_left, batch.drug_features_right) loss_value = loss(prediction, batch.labels) loss_value.backward() optimizer.step() ``` -------------------------------- ### Check Code Quality with Flake8 Source: https://github.com/astrazeneca/chemicalx/blob/main/CONTRIBUTING.md Verify code quality and style compliance using Flake8. ```bash tox -e flake8 ``` -------------------------------- ### Initialize and Train CASTER Model Source: https://context7.com/astrazeneca/chemicalx/llms.txt Configures the CASTER model, which requires the CASTERSupervisedLoss function for training. ```python from chemicalx import pipeline from chemicalx.data import DrugCombDB from chemicalx.loss import CASTERSupervisedLoss from chemicalx.models import CASTER dataset = DrugCombDB() model = CASTER( drug_channels=dataset.drug_channels, encoder_hidden_channels=32, encoder_output_channels=32, decoder_hidden_channels=32, hidden_channels=32, out_hidden_channels=32, out_channels=1, lambda3=1e-5, # Dictionary encoder regularization magnifying_factor=100, # Input magnification for predictor ) results = pipeline( dataset=dataset, model=model, loss_cls=CASTERSupervisedLoss, # Required custom loss function batch_size=5120, epochs=100, context_features=False, drug_features=True, drug_molecules=False, metrics=["roc_auc"], ) results.summarize() ``` -------------------------------- ### Initialize and Train SSI-DDI Model Source: https://context7.com/astrazeneca/chemicalx/llms.txt Configures the SSI-DDI model using graph attention networks for substructure-level interaction modeling. ```python from chemicalx import pipeline from chemicalx.data import DrugCombDB from chemicalx.models import SSIDDI dataset = DrugCombDB() model = SSIDDI( molecule_channels=69, # TorchDrug default node features hidden_channels=(32, 32), # Hidden channels per block head_number=(2, 2), # Attention heads per block ) results = pipeline( dataset=dataset, model=model, batch_size=1024, epochs=10, context_features=False, drug_features=False, drug_molecules=True, # Uses molecular graphs metrics=["roc_auc"], ) results.summarize() ``` -------------------------------- ### Check Typing with Mypy Source: https://github.com/astrazeneca/chemicalx/blob/main/CONTRIBUTING.md Ensure type consistency and correctness by running Mypy. ```bash tox -e mypy ``` -------------------------------- ### Train and Evaluate Models with ChemicalX Pipelines Source: https://github.com/astrazeneca/chemicalx/blob/main/docs/source/notes/introduction.md Use the pipeline function to execute training and evaluation on a dataset with a specified model. Ensure the model and dataset are correctly initialized before passing them to the pipeline. ```python from chemicalx import pipeline from chemicalx.models import DeepSynergy from chemicalx.data import DrugCombDB model = DeepSynergy(context_channels=112, drug_channels=256) dataset = DrugCombDB() results = pipeline(dataset=dataset, model=model, batch_size=1024, context_features=True, drug_features=True, drug_molecules=False, labels=True, epochs=100) results.summarize() results.save("~/test_results/") ``` -------------------------------- ### Create a New Branch Source: https://github.com/astrazeneca/chemicalx/blob/main/CONTRIBUTING.md Create and checkout a new branch for your changes. The branch name must include the issue number. ```bash git checkout main git branch git checkout ``` -------------------------------- ### Push Branch to Origin Source: https://github.com/astrazeneca/chemicalx/blob/main/CONTRIBUTING.md Push your local branch to the remote repository to prepare for a pull request. ```bash git push origin ``` -------------------------------- ### Initialize and Train DeepDDI Model Source: https://context7.com/astrazeneca/chemicalx/llms.txt Configures the DeepDDI model for drug-drug interaction prediction using structural similarity profiles. ```python from chemicalx import pipeline from chemicalx.data import DrugbankDDI from chemicalx.models import DeepDDI dataset = DrugbankDDI() model = DeepDDI( drug_channels=dataset.drug_channels, # Number of drug features hidden_channels=2048, # Hidden layer neurons hidden_layers_num=9, # Number of hidden layers out_channels=1, # Output dimension ) results = pipeline( dataset=dataset, model=model, batch_size=5120, epochs=100, context_features=False, # DeepDDI doesn't use context features drug_features=True, drug_molecules=False, metrics=["roc_auc"], ) results.summarize() ``` -------------------------------- ### Process DrugbankDDI Dataset Source: https://context7.com/astrazeneca/chemicalx/llms.txt Loads the DrugbankDDI dataset and performs a train/test split on labeled interaction triples. ```python from chemicalx.data import DrugbankDDI dataset = DrugbankDDI() dataset.summarize() # Access labeled triples labeled_triples = dataset.get_labeled_triples() print(f"Total triples: {labeled_triples.get_labeled_triple_count()}") print(f"Unique drugs: {labeled_triples.get_drug_count()}") print(f"Positive rate: {labeled_triples.get_positive_rate():.2%}") # Train/test split train_triples, test_triples = labeled_triples.train_test_split( train_size=0.8, random_state=42 ) ``` -------------------------------- ### Evaluate Model and Generate Predictions Source: https://github.com/astrazeneca/chemicalx/blob/main/docs/source/notes/tutorial.md Switches the model to evaluation mode and processes the test set to generate and store synergy predictions in a pandas DataFrame. ```python import pandas as pd model.eval() generator.labeled_triples = test predictions = [] for batch in generator: prediction = model(batch.context_features, batch.drug_features_left, batch.drug_features_right) prediction = prediction.detach().cpu().numpy() identifiers = batch.identifiers identifiers["prediction"] = prediction predictions.append(identifiers) predictions = pd.concat(predictions) ``` -------------------------------- ### Save Model Results Source: https://github.com/astrazeneca/chemicalx/blob/main/README.md Persist model metadata, losses, and evaluation results to a specified directory. ```python results.save("~/test_results/") ``` -------------------------------- ### Train and evaluate models with the pipeline API Source: https://context7.com/astrazeneca/chemicalx/llms.txt Use the pipeline function to handle data loading, training, and metric computation for drug pair scoring models. ```python from chemicalx import pipeline from chemicalx.data import DrugCombDB from chemicalx.models import DeepSynergy # Initialize dataset and model dataset = DrugCombDB() model = DeepSynergy( context_channels=dataset.context_channels, drug_channels=dataset.drug_channels ) # Run the training and evaluation pipeline results = pipeline( dataset=dataset, model=model, batch_size=5120, epochs=100, context_features=True, drug_features=True, drug_molecules=False, metrics=["roc_auc"], ) # Display evaluation results results.summarize() # Output: # Metric Value # -------- ------- # roc_auc 0.8234 # Save model and results to disk results.save("~/my_experiment_results/") ``` -------------------------------- ### Cite ChemicalX in BibTeX Source: https://github.com/astrazeneca/chemicalx/blob/main/README.md Use this BibTeX entry to cite the ChemicalX library in academic research. ```bibtex @inproceedings{10.1145/3534678.3539023, author = {Rozemberczki, Benedek and Hoyt, Charles Tapley and Gogleva, Anna and Grabowski, Piotr and Karis, Klas and Lamov, Andrej and Nikolov, Andriy and Nilsson, Sebastian and Ughetto, Michael and Wang, Yu and Derr, Tyler and Gyori, Benjamin M.}, title = {ChemicalX: A Deep Learning Library for Drug Pair Scoring}, year = {2022}, isbn = {9781450393850}, publisher = {Association for Computing Machinery}, address = {New York, NY, USA}, url = {https://doi.org/10.1145/3534678.3539023}, doi = {10.1145/3534678.3539023}, booktitle = {Proceedings of the 28th ACM SIGKDD Conference on Knowledge Discovery and Data Mining}, pages = {3819–3828}, numpages = {10}, keywords = {chemistry, neural networks, deep learning}, location = {Washington DC, USA}, series = {KDD '22} } ``` -------------------------------- ### Manage LabeledTriples Data Structure Source: https://context7.com/astrazeneca/chemicalx/llms.txt Creates and manipulates LabeledTriples objects from lists or DataFrames, supporting statistical queries and dataset merging. ```python from chemicalx.data import LabeledTriples import pandas as pd # Create from list of tuples data = [ ("DrugA", "DrugB", "CellLine1", 1.0), ("DrugA", "DrugC", "CellLine1", 0.0), ("DrugB", "DrugC", "CellLine2", 1.0), ] triples = LabeledTriples(data) # Or from DataFrame df = pd.DataFrame(data, columns=["drug_1", "drug_2", "context", "label"]) triples = LabeledTriples(df) # Query statistics print(f"Drug count: {triples.get_drug_count()}") print(f"Context count: {triples.get_context_count()}") print(f"Combination count: {triples.get_combination_count()}") print(f"Positive count: {triples.get_positive_count()}") print(f"Negative count: {triples.get_negative_count()}") print(f"Positive rate: {triples.get_positive_rate():.2%}") # Split for training train, test = triples.train_test_split(train_size=0.8, random_state=42) # Combine datasets combined = train + test combined.drop_duplicates() ``` -------------------------------- ### Configure the DeepSynergy model Source: https://context7.com/astrazeneca/chemicalx/llms.txt DeepSynergy is a fully connected neural network for synergy prediction; configure its architecture using specific channel and dropout parameters. ```python from chemicalx import pipeline from chemicalx.data import DrugCombDB from chemicalx.models import DeepSynergy dataset = DrugCombDB() model = DeepSynergy( context_channels=dataset.context_channels, # Number of biological context features drug_channels=dataset.drug_channels, # Number of drug features input_hidden_channels=32, # First hidden layer size middle_hidden_channels=32, # Middle hidden layer size final_hidden_channels=32, # Final hidden layer size out_channels=1, # Output dimension dropout_rate=0.5, # Dropout probability ) results = pipeline( dataset=dataset, model=model, batch_size=5120, epochs=100, context_features=True, drug_features=True, drug_molecules=False, metrics=["roc_auc"], ) results.summarize() ``` -------------------------------- ### Access training outcomes with the Result class Source: https://context7.com/astrazeneca/chemicalx/llms.txt The Result dataclass stores trained models, predictions, loss history, and metrics, providing methods for persistence and analysis. ```python from chemicalx import pipeline, Result from chemicalx.data import DrugbankDDI from chemicalx.models import DeepDDI dataset = DrugbankDDI() model = DeepDDI(drug_channels=dataset.drug_channels, hidden_layers_num=2) results: Result = pipeline( dataset=dataset, model=model, batch_size=5120, epochs=50, context_features=False, drug_features=True, drug_molecules=False, metrics=["roc_auc", "mse"], ) # Access result attributes print(f"Training time: {results.train_time:.2f}s") print(f"Evaluation time: {results.evaluation_time:.2f}s") print(f"Final loss: {results.losses[-1]:.4f}") print(f"Metrics: {results.metrics}") # Access predictions DataFrame predictions_df = results.predictions print(predictions_df.head()) # Output columns: drug_1, drug_2, context, label, prediction # Save everything to directory results.save("/path/to/output/") # Creates: model.pkl, results.json ``` -------------------------------- ### Cite ChemicalX Source: https://github.com/astrazeneca/chemicalx/blob/main/docs/source/notes/introduction.md BibTeX citation for the ChemicalX library. ```latex >@article{chemicalx, arxivId = {2202.05240}, author = {Rozemberczki, Benedek and Hoyt, Charles Tapley and Gogleva, Anna and Grabowski, Piotr and Karis, Klas and Lamov, Andrej and Nikolov, Andriy and Nilsson, Sebastian and Ughetto, Michael and Wang, Yu and Derr, Tyler and Gyori, Benjamin M}, month = {feb}, title = {{ChemicalX: A Deep Learning Library for Drug Pair Scoring}}, url = {http://arxiv.org/abs/2202.05240}, year = {2022} } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.