matcha.datamodules.pretraining.graph_pretraining_datamodule
Graph pretraining DataModule for supervised multi-task learning on graphs.
Provides a datamodule that extends GraphDataModule to handle both molecule-level and atom-level labels for pretraining graph neural networks such as GIN. The user supplies external per-atom labels (e.g. partial charges, electronegativity, or any computed atom-level property) alongside molecule-level labels. The model then learns to predict both simultaneously.
Classes
Graph DataModule for multi-task pretraining with atom-level and molecule-level labels. |
Module Contents
- class matcha.datamodules.pretraining.graph_pretraining_datamodule.GraphPretrainingDataModule(scale_y_graph: bool = False, scale_y_node: bool = False, laplacian_k: int = 10, rwse_k: int = 20, elstatic_k: int = 0, distmat_k: int = 0, rrwp_k: int = 20, compute_distances: bool = True, num_virtual_nodes: int = 0, init_virtual_nodes: bool = False, batch_size: int = 256, num_workers: int = 0, augment_resonance: bool = False)[source]
Bases:
matcha.datamodules.classic.graph_datamodule.GraphDataModuleGraph DataModule for multi-task pretraining with atom-level and molecule-level labels.
Extends
GraphDataModuleto accept user-provided per-atom labels alongside molecule-level labels. This enables supervised pretraining of graph neural networks (e.g.GINPretraining) that jointly predict atom-level targets (such as partial charges, SASA, electronegativity, etc.) and molecule-level targets (such as logP, molecular weight, etc.).The atom-level labels are not derived from the graph’s own atom features — they are externally computed properties supplied by the user.
Example usage:
dm = GraphPretrainingDataModule() # y_node is a list of arrays, one per molecule, each of shape (num_atoms, T) # y_graph is a numpy array of shape (N, G) dataset = dm.featurize( mol_list=mols, y_graph=y_graph, y_node=y_node, )
- Parameters:
scale_y_graph (bool) – whether to fit a scaler on the molecule-level targets during training, defaults to False
All other parameters are inherited from
GraphDataModule.- params
- export_to_classic() matcha.datamodules.classic.graph_datamodule.GraphDataModule[source]
Return a
GraphDataModulethat mirrors the current state.The exported instance inherits all graph-specific settings (positional encoding dimensions, virtual nodes, etc.) so that it can be used directly for downstream (non-pretraining) training or inference.
- Return GraphDataModule:
a classic graph datamodule with the same state
- generate_features(mol_list: list[rdkit.Chem.rdchem.Mol], y_graph: numpy.ndarray | None = None, y_node: list[numpy.ndarray] | None = None, n_jobs: int | None = None) torch.utils.data.StackDataset[source]
Generate unscaled graph features with atom-level and molecule-level labels.
- Parameters:
mol_list – list of N RDKit molecules
y_graph – array
(N, G)of molecule-level targets, or Noney_node – list of N arrays, each
(A_i, T)of atom-level targets, or Nonen_jobs – number of parallel workers (None = auto)
- Returns:
StackDatasetwith keysgraphandy_graph(atom-level labels are stored on eachData.y_node)
- featurize(mol_list: list[rdkit.Chem.rdchem.Mol], y_graph: numpy.ndarray | None = None, y_node: list[numpy.ndarray] | None = None, is_training: bool = True, n_jobs: int | None = None) torch.utils.data.StackDataset[source]
Generate a dataset ready for graph pretraining.
Processes molecules alongside their molecule-level and atom-level labels into a
StackDatasetthat can be consumed byBaseGraphPretrainingModel.- Parameters:
mol_list – list of N RDKit molecules
y_graph – array
(N, G)of molecule-level targetsy_node – list of N arrays, each
(A_i, T)of atom-level targetsis_training – whether to fit the Y scaler (only affects
y_graphscaling whenscale_y_graph=True)n_jobs – number of parallel workers (None = auto)
- Returns:
StackDatasetwith keysgraphandy_graph
- fit(dataset: torch.utils.data.StackDataset) None[source]
Fit scalers on labels if scaling is enabled.
- Parameters:
dataset – StackDataset produced by
generate_features()
- transform(dataset: torch.utils.data.StackDataset) torch.utils.data.StackDataset[source]
Scale labels if scaling is enabled.
- Parameters:
dataset – StackDataset to transform in-place
- Returns:
the same dataset with scaled labels
- collate_fn(data: list[dict]) dict[source]
Collate a list of samples into a pretraining batch.
Produces the batch format expected by
BaseGraphPretrainingModel:graph: batched PyG graph (y_nodeis auto-concatenated byBatch.from_data_listas a node-level attribute)y_node:[total_nodes_in_batch, T]y_graph:[batch_size, G]
- Parameters:
data – list of dicts from the
StackDataset- Returns:
dict with keys
graph,y_node,y_graph
- state_dict() dict[source]
Serialise state for MLFlow logging.
- Returns:
dict containing ID, params, and fitted scalers
- load_state_dict(state_dict: dict)[source]
Restore state from a previously serialised dict.
- Parameters:
state_dict – dict produced by
state_dict()
- classmethod dummy()[source]
Create a dummy instance with default parameters.
- Returns:
a new
GraphPretrainingDataModulewith default settings