Graph Representations¶
molfp.graph
¶
Molecular graph representations for deep learning.
Converts molecules to atom/bond feature matrices and adjacency structures suitable for graph neural networks (GCN, GAT, MPNN, SchNet, etc.).
Framework-agnostic core (NumPy only), with optional converters for PyTorch Geometric and DGL.
GraphTransformer
¶
Bases: BaseEstimator, TransformerMixin
sklearn-compatible transformer for molecular graph features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enriched
|
if True, use enriched atom features with Morgan hashes, functional group membership, and neighbor counts. |
False
|
|
type
|
graph type — "standard", "distance", "line", "fragment". |
'standard'
|
For feature selection, use standalone functions after transforming: X = molfp.GraphTransformer(enriched=True).fit_transform(smiles) result = molfp.select_features(X, y, k=500, threshold=0.01)
mol_graph(input_data, *, column=None, enriched=False, type='standard')
¶
Convert molecule(s) to graph representation(s) for deep learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_data
|
molecule input (str, Mol, list, Series, DataFrame). |
required | |
column
|
column name for DataFrame input. |
None
|
|
enriched
|
if True, add Morgan hashes, functional group membership, and neighbor counts per atom (432 dims vs 20 base dims). Only applies to type="standard" and type="distance". |
False
|
|
type
|
graph type: "standard" — atoms=nodes, bonds=edges (GCN, GAT, GIN) "distance" — atoms=nodes, all pairs=edges with 3D distance (SchNet, DimeNet) "line" — bonds=nodes, bond-bond connections (D-MPNN, Chemprop) "fragment" — functional groups=nodes (hierarchical GNNs) |
'standard'
|
Returns:
| Type | Description |
|---|---|
|
Single molecule: dict with atom_features, edge_index, edge_features. |
|
|
Multiple molecules: list of dicts. |
to_pyg(graph_dict, y=None)
¶
Convert a graph dict to a PyTorch Geometric Data object.
Requires: pip install torch torch_geometric
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_dict
|
output of mol_graph() for a single molecule. |
required | |
y
|
optional label (int, float, or tensor). |
None
|
Returns:
| Type | Description |
|---|---|
|
torch_geometric.data.Data |
graphs_to_pyg(graph_list, y=None, batch_size=32, shuffle=False)
¶
Convert a list of graph dicts to a PyTorch Geometric DataLoader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_list
|
list of graph dicts from mol_graph(). |
required | |
y
|
optional list/array of labels (same length as graph_list). |
None
|
|
batch_size
|
batch size for DataLoader. |
32
|
|
shuffle
|
whether to shuffle the DataLoader. |
False
|
Returns:
| Type | Description |
|---|---|
|
torch_geometric.loader.DataLoader |
to_dgl(graph_dict)
¶
Convert a graph dict to a DGL graph.
Requires: pip install dgl torch
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_dict
|
output of mol_graph() for a single molecule. |
required |
Returns:
| Type | Description |
|---|---|
|
dgl.DGLGraph with ndata['feat'] and edata['feat'] |
atom_feature_names()
¶
Return ordered list of atom feature names.
edge_feature_names()
¶
Return ordered list of edge feature names.
graph_to_vector(graph_dict)
¶
Convert a graph dict to a fixed-length feature vector.
Uses chemistry-aware aggregation to preserve more structural information than simple mean/max pooling. Designed for sklearn classifiers. Adapts to both base (20-dim) and enriched (432-dim) atom features.
graph_vectors(input_data, *, column=None, enriched=False, type='standard')
¶
Convert molecule(s) to fixed-length graph feature vectors for sklearn.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_data
|
SMILES string, list, Series, or DataFrame. |
required | |
column
|
column name for DataFrame input. |
None
|
|
enriched
|
if True, use enriched atom features. |
False
|
|
type
|
graph type — "standard", "distance", "line", "fragment". |
'standard'
|
Returns:
| Type | Description |
|---|---|
|
Single molecule: numpy array |
|
|
Multiple molecules: numpy array (n_samples, n_features) |