Custom CSV Data Configuration
Relevant source files
The following files were used as context for generating this wiki page:
- examples/property_prediction/csv_data_configuration/README.md
- python/dgllife/data/csv_dataset.py
- examples/property_prediction/csv_data_configuration/classification_inference.py
- examples/property_prediction/csv_data_configuration/regression_inference.py
- python/dgllife/utils/analysis.py
- python/dgllife/data/esol.py
- python/dgllife/data/muv.py
Custom CSV Data Configuration provides a flexible framework within DGL-LifeSci for prototyping Graph Neural Network (GNN) models using user-supplied molecular datasets. It allows developers to transition from raw CSV files containing SMILES strings and properties to fully featurized DGLGraph objects suitable for property prediction tasks such as regression and multi-label binary classification.
The system encompasses data preparation, optional molecular analysis, automated graph construction, and specialized handling for training and inference phases. By leveraging the MoleculeCSVDataset class, it standardizes the pipeline for loading, featurizing, and caching molecular data, ensuring reproducibility across different GNN architectures.
Sources: examples/property_prediction/csv_data_configuration/README.md:1-10, python/dgllife/data/csv_dataset.py:18-30
Data Infrastructure and Dataset Loading
The core of the custom data configuration is the MoleculeCSVDataset class. This class serves as a general template for loading molecular data from a pandas.DataFrame. It handles the conversion of SMILES strings into DGLGraphs and manages binary masks for label existence, which is critical for datasets with missing values.
The MoleculeCSVDataset Class
This class automates several pre-processing steps:
- Graph Construction: Converts SMILES to
DGLGraphinstances using a user-specifiedsmiles_to_graphcallable. - Featurization: Applies node and edge featurizers to generate initial representations.
- Caching: Saves constructed graphs to a
.binfile to avoid redundant computations in subsequent runs. - Masking: Automatically identifies
NaNvalues in the CSV and creates a binary mask for loss computation.
Sources: python/dgllife/data/csv_dataset.py:18-65, python/dgllife/data/csv_dataset.py:126-146
Data Flow Diagram
The following diagram illustrates the lifecycle of a molecule from a CSV record to a featurized graph stored in the dataset.
Molecular Data Analysis
Before modeling, the system provides utilities to analyze the chemical space of the CSV dataset. This is primarily handled by the analyze_mols function, which filters invalid molecules and computes descriptive statistics for atoms, bonds, and rings.
Analysis Components
| Statistic Type | Descriptors Analyzed |
|---|---|
| General | Number of atoms, bonds, and rings per molecule |
| Atom-level | Type, degree, valence (explicit/implicit), hybridization, formal charge, aromaticity |
| Bond-level | Type (Single/Double/Triple/Aromatic), conjugation, stereo configuration |
Sources: python/dgllife/utils/analysis.py:23-58, python/dgllife/utils/analysis.py:91-110
The analysis results are typically exported to valid_canonical_smiles.txt and a summary.txt file containing mean and standard deviation values for molecular properties.
Sources: python/dgllife/utils/analysis.py:165-185
Training and Inference Configuration
The framework supports distinct workflows for training models and running inference on new, unlabeled data.
Training Configuration
Training scripts (e.g., regression_train.py, classification_train.py) accept various arguments to define the GNN architecture and data split strategies.
- Split Strategies: Supports
scaffold(Bemis-Murcko) andrandomsplits. - Model Options: Includes
GCN,GAT,Weave,MPNN,AttentiveFP, and various pre-trainedGINmodels. - Featurizers: Defaults to
canonicalatom and bond featurizers, but allows specialized ones likeattentivefp.
Sources: examples/property_prediction/csv_data_configuration/README.md:37-85, examples/property_prediction/csv_data_configuration/README.md:123-150
Inference Logic
Inference is performed using UnlabeledSMILES, which inherits graph construction logic but ignores property labels. It supports reading from .csv, .csv.gz, or .txt files.
Implementation Examples
CSV Dataset Initialization
The following snippet demonstrates how a specific dataset like ESOL leverages the base MoleculeCSVDataset to configure its custom columns and tasks.
# From python/dgllife/data/esol.py:108-121
super(ESOL, self).__init__(df=df,
smiles_to_graph=smiles_to_graph,
node_featurizer=node_featurizer,
edge_featurizer=edge_featurizer,
smiles_column='smiles',
cache_file_path=cache_file_path,
task_names=['measured log solubility in mols per litre'],
load=load,
log_every=log_every,
init_mask=False,
n_jobs=n_jobs)
Sources: python/dgllife/data/esol.py:108-121
Binary Classification Task Weighting
For imbalanced classification datasets (like MUV), the system provides a utility to compute positive sample weights to be used in the loss function.
# From python/dgllife/data/csv_dataset.py:236-241
def task_pos_weights(self, indices):
task_pos_weights = torch.ones(self.labels.shape[1])
num_pos = F.sum(self.labels[indices], dim=0)
num_indices = F.sum(self.mask[indices], dim=0)
task_pos_weights[num_pos > 0] = ((num_indices - num_pos) / num_pos)[num_pos > 0]
return task_pos_weights
Sources: python/dgllife/data/csv_dataset.py:236-241
Conclusion
Custom CSV Data Configuration in DGL-LifeSci standardizes the transition from flat molecular data files to graph-based deep learning inputs. By providing robust utilities for analysis, flexible featurization, and efficient caching through the MoleculeCSVDataset class, it enables rapid prototyping and scaling of molecular property prediction models.
Sources: examples/property_prediction/csv_data_configuration/README.md:1-5, python/dgllife/data/csv_dataset.py:18-30