Node and Edge Featurizers
Relevant source files
The following files were used as context for generating this wiki page:
Node and Edge Featurizers are core utilities in DGL-LifeSci designed to transform chemical structures (typically RDKit molecule objects) into numerical tensors suitable for Graph Neural Networks (GNNs). These featurizers map chemical properties—such as atom types, bond degrees, aromaticity, and hybridization—into feature vectors that populate the ndata (node data) and edata (edge data) fields of a DGLGraph.
The system is built on a modular architecture using base classes that allow for the concatenation of multiple descriptor functions. This enables the library to support various established featurization schemes used in literature, including Canonical, AttentiveFP, Weave, and Pre-trained models, while also providing the flexibility to define custom featurization logic for specific research tasks.
Sources: README.md:65-88, python/dgllife/utils/featurizers.py:843-855
Core Architecture and Base Classes
The featurization framework relies on three primary components: descriptor functions, the ConcatFeaturizer for grouping descriptors, and base featurizer classes that iterate over molecular components.
Descriptor Functions
Descriptors are low-level functions with the signature func(data_type) -> list. They extract specific properties from RDKit Atom or Bond objects.
- Node Descriptors: Functions like
atom_type_one_hot,atom_degree, andatom_formal_charge. - Edge Descriptors: Functions like
bond_type_one_hot,bond_is_conjugated, andbond_stereo_one_hot.
ConcatFeaturizer
This class serves as a wrapper to combine multiple descriptor functions into a single feature vector. It ensures that the resulting features follow the order of the functions provided in the input list.
Sources: python/dgllife/utils/featurizers.py:787-814
Base Featurizers
The BaseAtomFeaturizer and BaseBondFeaturizer classes handle the iteration over all atoms or bonds in a molecule. They apply the assigned descriptor functions and stack the results into PyTorch tensors.
The diagram illustrates the relationship between base featurizers and the concatenation utility.
Sources: python/dgllife/utils/featurizers.py:816-841, python/dgllife/utils/featurizers.py:1181-1221
Atom Featurization Schemes
DGL-LifeSci provides several pre-configured atom featurizers based on common GNN architectures.
| Featurizer | Key Features Included | Feature Size |
|---|---|---|
| Canonical | Atom type, degree, implicit valence, formal charge, radical electrons, hybridization, aromaticity, total Hs. | 74 |
| AttentiveFP | Specific atom types (B to At), degree (0-5), formal charge, radical electrons, hybridization, aromaticity, total Hs, chirality. | 39 |
| Weave | Atom type, chirality, formal/partial charge, aromaticity, hybridization, H-bond donor/acceptor, ring sizes (3-8). | 27 |
| Pretrain | Atomic number, chirality type (for pre-trained GIN models). | 2 (int64) |
Functional Logic of BaseAtomFeaturizer
The BaseAtomFeaturizer assumes a one-to-one mapping between atom index $i$ in the RDKit molecule and node index $i$ in the DGLGraph. It outputs a dictionary where keys are feature names (e.g., 'h') and values are tensors of shape $(N, M)$, where $N$ is the number of atoms and $M$ is the concatenated feature length.
Sources: python/dgllife/utils/featurizers.py:917-943, python/dgllife/utils/featurizers.py:1082-1110, python/dgllife/utils/featurizers.py:1124-1145
Bond Featurization and Self-Loops
Edge featurizers handle bond properties and can optionally manage self-loop features for GNNs that require them.
Bond Descriptors
Standard bond featurization includes one-hot encoding of bond types (SINGLE, DOUBLE, TRIPLE, AROMATIC) and stereo configurations.
Self-Loop Handling
The BaseBondFeaturizer includes a self_loop parameter. When set to True:
- An additional binary column is added to the feature vector to indicate if an edge is a self-loop.
- Bond features are zeroed out for self-loop edges, except for the identity column.
- If a molecule has zero bonds (e.g., a single atom), the featurizer can still generate self-loop features to ensure graph connectivity.
Sources: python/dgllife/utils/featurizers.py:1260-1285, python/dgllife/utils/featurizers.py:1310-1325
Logic flow for bond featurization with optional self-loop identity columns.
Specialized Featurizers for Datasets
Specific research tasks or datasets often require unique featurization logic implemented outside the standard Canonical classes.
Reaction Prediction (USPTO)
For reaction center prediction, the library defines specialized featurizers that distinguish a wider array of atom types (62 types) and include formal charges to identify reactive sites. It also uses atom_pair_featurizer to compute features for every possible pair of atoms in a complete graph, identifying bond formation or cleavage.
Sources: python/dgllife/data/uspto.py:44-77
Quantum Chemistry (Alchemy)
The Alchemy dataset uses a specialized node featurizer that incorporates:
- Chemical feature family information (H-bond Donor/Acceptor) using RDKit's
ChemicalFeatures. - Atomic positions and Euclidean distances for edges, stored in
edata['distance'].
Sources: python/dgllife/data/alchemy.py:30-67
Analysis Utilities
The analyze_mols utility uses a simplified featurization summary to compute frequencies of atomic and bond descriptors across a collection of molecules, providing statistical insights into dataset composition.
Sources: python/dgllife/utils/analysis.py:27-66
Summary of Data Flow
The featurization process acts as the bridge between raw chemical strings and the graph data structure.
The sequence illustrates how molecular properties are extracted and transformed into graph data.
Sources: tests/utils/test_mol_to_graph.py:32-60, README.md:73-88
Node and edge featurizers provide the essential numerical foundation for DGL-LifeSci. By abstracting molecular property extraction into modular components, the library supports a diverse range of chemical representations, from simple atomic numbers for pre-training to complex stereochemical and electronic descriptors for property prediction.