Chemprop Python API Modeling
Use this sub-skill when a user wants to write or repair Python code that uses Chemprop directly for molecular property prediction. Prefer Python API patterns here; route pure chemprop train, chemprop predict, chemprop fingerprint, chemprop convert, or chemprop hpopt commands to CLI-oriented sub-skills. Route reaction/MolAtomBond task constraints to specialized-molecular-tasks and uncertainty-specific interpretation to uncertainty-advanced.
Core Workflow
- Create Chemprop datapoints from RDKit molecules or SMILES.
- Wrap them in
MoleculeDataset, ReactionDataset, MulticomponentDataset, or MolAtomBondDataset as appropriate.
- Build dataloaders with
chemprop.data.build_dataloader; keep shuffle=False for validation, test, and prediction loaders.
- Wire a model from message passing, aggregation, and predictor modules.
- Train or predict with
lightning.pytorch.Trainer.
- Save portable Chemprop model files with
chemprop.models.save_model; reload with chemprop.models.load_model or class load_from_file helpers.
import numpy as np
from lightning import pytorch as pl
from chemprop import data, models, nn
smiles = ["CCO", "CCN", "c1ccccc1"]
y = np.array([[0.1], [0.2], [0.3]], dtype=float)
dset = data.MoleculeDataset([
data.MoleculeDatapoint.from_smi(smi, target) for smi, target in zip(smiles, y)
])
loader = data.build_dataloader(dset, batch_size=2, shuffle=True)
mp = nn.BondMessagePassing(d_h=64, depth=2)
agg = nn.MeanAggregation()
predictor = nn.RegressionFFN(input_dim=mp.output_dim, n_tasks=1, hidden_dim=64)
model = models.MPNN(mp, agg, predictor, metrics=[nn.RMSE(), nn.MAE()])
trainer = pl.Trainer(accelerator="cpu", devices=1, logger=False, enable_checkpointing=False, max_epochs=1)
trainer.fit(model, loader)
References
references/api-reference.md: public classes, constructors, registries, model save/load, and batch signatures.
references/python-workflows.md: end-to-end recipes for training, prediction, scaling, ensembling, and checkpoint use.
references/model-components.md: component compatibility notes for message passing, aggregation, predictors, losses, metrics, transforms, and dataloaders.
references/troubleshooting.md: common Python API failures and fixes.
scripts/chemprop_api_smoke.py: self-contained CPU smoke script for minimal MPNN training, prediction, and save/load validation.
Key Guardrails
- Keep predictor
input_dim equal to the feature dimension produced by model.fingerprint; for plain MPNN this is usually message_passing.output_dim plus any post-aggregation X_d width.
- Use regression predictors with regression targets, binary predictors with binary labels, multiclass predictors with integer class targets and
n_classes, and spectral predictors with spectrum-shaped targets.
- Use
UnscaleTransform on predictors for scaled regression targets, ScaleTransform on X_d/V_d, and GraphTransform on extra atom/bond features.
- Build prediction loaders with
shuffle=False; Lightning warns and user-facing row order becomes ambiguous if prediction data is shuffled.
- Preserve target names by passing
output_columns to save_model and reading them back with chemprop.models.utils.load_output_columns when needed.
1---2name: python-api-modeling3description: "Build Chemprop v2 Python API workflows for data objects,4 dataloaders, MPNN model wiring, Lightning training/prediction, scaling5 transforms, metrics/losses, ensembling, and save/load."6disable-model-invocation: true7metadata:8 source: https://skillmd.com/skills/vectorspacelab/python-api-modeling9 disco-role: operating10license: NOASSERTION11---1213# Chemprop Python API Modeling1415Use this sub-skill when a user wants to write or repair Python code that uses Chemprop directly for molecular property prediction. Prefer Python API patterns here; route pure `chemprop train`, `chemprop predict`, `chemprop fingerprint`, `chemprop convert`, or `chemprop hpopt` commands to CLI-oriented sub-skills. Route reaction/MolAtomBond task constraints to `specialized-molecular-tasks` and uncertainty-specific interpretation to `uncertainty-advanced`.1617## Core Workflow18191. Create Chemprop datapoints from RDKit molecules or SMILES.202. Wrap them in `MoleculeDataset`, `ReactionDataset`, `MulticomponentDataset`, or `MolAtomBondDataset` as appropriate.213. Build dataloaders with `chemprop.data.build_dataloader`; keep `shuffle=False` for validation, test, and prediction loaders.224. Wire a model from message passing, aggregation, and predictor modules.235. Train or predict with `lightning.pytorch.Trainer`.246. Save portable Chemprop model files with `chemprop.models.save_model`; reload with `chemprop.models.load_model` or class `load_from_file` helpers.2526```python27import numpy as np28from lightning import pytorch as pl29from chemprop import data, models, nn3031smiles = ["CCO", "CCN", "c1ccccc1"]32y = np.array([[0.1], [0.2], [0.3]], dtype=float)33dset = data.MoleculeDataset([34 data.MoleculeDatapoint.from_smi(smi, target) for smi, target in zip(smiles, y)35])36loader = data.build_dataloader(dset, batch_size=2, shuffle=True)3738mp = nn.BondMessagePassing(d_h=64, depth=2)39agg = nn.MeanAggregation()40predictor = nn.RegressionFFN(input_dim=mp.output_dim, n_tasks=1, hidden_dim=64)41model = models.MPNN(mp, agg, predictor, metrics=[nn.RMSE(), nn.MAE()])4243trainer = pl.Trainer(accelerator="cpu", devices=1, logger=False, enable_checkpointing=False, max_epochs=1)44trainer.fit(model, loader)45```4647## References4849- `references/api-reference.md`: public classes, constructors, registries, model save/load, and batch signatures.50- `references/python-workflows.md`: end-to-end recipes for training, prediction, scaling, ensembling, and checkpoint use.51- `references/model-components.md`: component compatibility notes for message passing, aggregation, predictors, losses, metrics, transforms, and dataloaders.52- `references/troubleshooting.md`: common Python API failures and fixes.53- `scripts/chemprop_api_smoke.py`: self-contained CPU smoke script for minimal MPNN training, prediction, and save/load validation.5455## Key Guardrails5657- Keep predictor `input_dim` equal to the feature dimension produced by `model.fingerprint`; for plain `MPNN` this is usually `message_passing.output_dim` plus any post-aggregation `X_d` width.58- Use regression predictors with regression targets, binary predictors with binary labels, multiclass predictors with integer class targets and `n_classes`, and spectral predictors with spectrum-shaped targets.59- Use `UnscaleTransform` on predictors for scaled regression targets, `ScaleTransform` on `X_d`/`V_d`, and `GraphTransform` on extra atom/bond features.60- Build prediction loaders with `shuffle=False`; Lightning warns and user-facing row order becomes ambiguous if prediction data is shuffled.61- Preserve target names by passing `output_columns` to `save_model` and reading them back with `chemprop.models.utils.load_output_columns` when needed.