Anvil Reference

Anvil is a workflow engine integrated into openadmet_models that allows users to define a human-readable model specification to reproducibly train and evaluate machine learning models. This is to facilitate large-scale reproducible training and comparisons across different datasets, models, and featurizations. In addition, anvil allows for the training of model ensembles, that can be easily used in downstream applications such as active learning.

Anvil is built around the concept of a “recipe” - a YAML file that specifies all the components of a machine learning workflow, including data loading, featurization, model architecture, training parameters, and evaluation metrics. By defining a recipe, users can easily reproduce experiments, share workflows with others, and systematically explore different modeling approaches. Anvil also makes our (OpenADMET Team) lives easier by handling the boilerplate code associated with setting up and running machine learning experiments, saving us from a twisted jungle of scripts and configuration files.

A full list of available models, featurizers, trainers, and evaluators can be found in the OpenADMET API documentation. Additionally, we maintain a list of canonical recipes we use in production here.

How to Use Anvil

To initiate the anvil workflow, a recipe yaml file must be provided. There are many configuration options available. Each workflow consists of four main sections: metadata, data, procedure, and report.

This guide should help you navigate the anvil workflow and understand the parameters you can set, their types, and how they interact across models and trainers.

Metadata

The metadata section provides essential information about the workflow, such as authorship, versioning, and descriptive tags. This section ensures that workflows are well-documented and easily identifiable. Many of these fields are purely descriptive and do not affect the workflow’s execution.

metadata:
  authors: Author Name
  email: author@email.org
  biotargets: [target_1, target_2]
  build_number: 0
  description: description of run
  driver: driver_name
  name: workflow_name
  tag: main_tag
  tags: [sub_tag_1, sub_tag_2]
  version: v1

Parameters

Name

Type

Description

authors

str | list[str]

Author(s) of the workflow.

email

str

Contact email.

biotargets

list[str]

List of biological targets associated with the workflow.

build_number

int

Iteration number of the workflow.

description

str

Short description of the workflow.

driver

str

Backend framework for the workflow (e.g., pytorch or sklearn or pytorch_ensemble).

name

str

Workflow name.

tag

str

Main tag for the workflow.

tags

list[str]

Additional tags associated with the workflow description.

version

str

Version of the metadata schema. (Currently must be set to v1).

Data

The data section defines how input data is loaded and which columns are used for modeling. You must specify the dataset location, input column, target columns, and optional preprocessing steps. The data loader can read from remote locations as well as local files.

Reading from a local file requires specifying the path to the dataset file in the resource field. Supported file types include CSV, and Parquet. If using resource your dataset will be split into training, validation, and test sets using the specified splitter in the procedure section.

Alternatively, you can also provide separate files for training, validation, and test sets by using the train_resource, val_resource, and test_resource fields, respectively.

data:
  type: intake
  resource: PATH_TO_DATASET.parquet
  input_col: COLUMN_NAME
  target_cols:
  - target_column_name1
  - target_column_name2
  dropna: false

A more advanced option is to use an Intake catalog to manage datasets. This is done by specifying a YAML file in the resource field and the catalog entry in the cat_entry field. This allows for more flexible dataset management, especially when dealing with multiple datasets or complex data sources.

data:
  type: intake
  resource: PATH_TO_CATALOG.yaml
  cat_entry: catalog_entry_name
  input_col: COLUMN_NAME
  target_cols:
  - target_column_name1
  - target_column_name2
  dropna: false

Pulling data from a remote location is also possible by specifying a URL in the resource field.

data:
  type: intake
  resource: https://path_to_your_data/dataset.parquet
  input_col: COLUMN_NAME
  target_cols:
  - target_column_name1
  - target_column_name2
  dropna: false

An example of using train, validation, and test resources:

data:
  type: intake
  train_resource: PATH_TO_TRAIN_DATASET.parquet
  val_resource: PATH_TO_VAL_DATASET.parquet
  test_resource: PATH_TO_TEST_DATASET.parquet
  input_col: COLUMN_NAME
  target_cols:
  - target_column_name1
  - target_column_name2
  dropna: false

Parameters

Name

Type

Description

resource

str

Path to dataset file. Allowed filetypes: YAML, CSV, parquet. Can also be a URL to a remote file.

train_resource

Optional[str]

Path to training dataset file. Allowed filetypes: CSV, parquet. Can also be a URL to a remote file.

val_resource

Optional[str]

Path to validation dataset file. Allowed filetypes: CSV, parquet. Can also be a URL to a remote file.

test_resource

Optional[str]

Path to test dataset file. Allowed filetypes: CSV, parquet. Can also be a URL to a remote file.

type

str, default: intake

Loader type. Must be intake. Uses the Intake data catalog system to read datasets.

input_col

str

Column name containing molecular input.

target_cols

Union[str, list[str]]

Name(s) of the target column(s) for the model to predict.

dropna

Optional[bool]

Whether to drop rows with missing values (NaN) in the input or target columns.

cat_entry

Optional[str]

Used when resource is a YAML file, to specify which catalog entry to load.

anvil_dir

Optional[str]

Allows for resource to point to a directory path. Useful for flexible dataset locations.

Procedure

The procedure section is the core of the workflow, where the data is transformed, models are defined, data splits are configured, and training parameters are set.

  • Featurization: Defines how molecular data is transformed into numerical representations using various available featurizers, specified in the feat subsection.

  • Transform: Optional post-featurization transforms applied to the featurized matrices, specified in the transform subsection.

  • Model: Specifies the model to be used, including loading from saved model weights, under the model subsection.

  • Splitting: Configures how the dataset is divided into training, validation, and test sets using assigned splitter, defined in the split subsection.

  • Training: Sets up the training process, including the trainer type and training parameters, under the train subsection.

Each subsection provides examples and parameter descriptions to help you configure the workflow according to your requirements.

Featurization

The feat section is used to specify among a variety of featurizers, which map molecular data into suitable input formats for the specified model. Below are the available options. Each featurizer has its own set of parameters which can be found in the linked OpenADMET API documentation.

In general we follow the design pattern that all deep learning featurizers return a PyTorch DataLoader as input, while traditional machine learning models return a a 2D NumPy array or pandas DataFrame.

Featurizer

Description

ChemPropFeaturizer

Converts SMILES strings into a ChemProp compatible PyTorch DataLoader.

CheMeleonEmbeddingFeaturizer

Extracts pooled MPNN embeddings from the pretrained CheMeleon foundation model as a 2D NumPy array.

MonroeFeaturizer

Extracts graph-level embeddings from the pretrained Monroe foundation model as a 2D NumPy array. Requires the monroe package, which installs from GitHub rather than PyPI. Embeddings are stochastic across runs, so features must be materialized once and reused.

DescriptorFeaturizer

Uses the molfeat library to compute molecular descriptors.

FingerprintFeaturizer

Uses the molfeat library to compute molecular fingerprints.

FeatureConcatenator

Combines multiple featurizers into a single feature array.

TrainedModelFeaturizer

Emits the predictions of an already-trained Anvil model as features.

For example, featurization for a traditional machine learning model using fingerprints is easily done by specifying the FingerprintFeaturizer.

feat:
  type: FingerprintFeaturizer
  params:
    fp_type: ecfp:4
    radius: 2

You can also combine multiple traditional ML featurizers using the FeatureConcatenator. Here we combine RDKit 2D descriptors and ECFP4 fingerprints. Each entry in featurizers takes the same type/params shape as the feat section itself. At least two are required. Featurizers of the same type must be disambiguated by specifying alias (e.g. two FingerprintFeaturizer entries with aliases “ecfp4” and “rdkit”).

feat:
  type: FeatureConcatenator
  params:
    featurizers:
      - type: DescriptorFeaturizer
        params:
          descr_type: desc2d
      - type: FingerprintFeaturizer
        params:
          fp_type: ecfp:4
          radius: 2
          n_bits: 2048

An already-trained model can itself be a featurizer, via the TrainedModelFeaturizer. The referenced model is frozen: it is loaded from disk, run in inference mode, and never refitted, so its predictions become feature columns for a downstream model. This lets a model trained on an abundant low-fidelity endpoint, such as primary-screen log2 fold change, inform a model trained on a scarce high-fidelity one, such as dose-response pEC50.

feat:
  type: TrainedModelFeaturizer
  params:
    model_dir: /path/to/pxr_log2fc_model

The pretrained model brings its own featurizer, so the TrainedModelFeaturizer takes SMILES rather than features. Molecules that the pretrained featurizer cannot parse are reported through the returned index array, the same as any other featurizer.

The featurizer emits one column per target column of the pretrained model. Setting return_std appends a second block holding the standard deviation across ensemble members, doubling the width. Only an ensemble has a standard deviation, so setting it against a single model raises when the recipe is parsed.

For example, to combine pretrained models for different endpoints, set an alias in each entry’s params.

feat:
  type: FeatureConcatenator
  params:
    featurizers:
      - type: CheMeleonEmbeddingFeaturizer
      - type: TrainedModelFeaturizer
        params:
          alias: log2fc
          model_dir: models/chemprop_log2fc
      - type: TrainedModelFeaturizer
        params:
          alias: htchem
          model_dir: models/chemprop_htchem

A per-block transform is configured against the aliases:

n_components:
  CheMeleonEmbeddingFeaturizer: 256
  log2fc: null
  htchem: null

Block names must be unique across the whole concatenator, including blocks from a nested FeatureConcatenator. Duplicates are reported when the recipe is parsed.

For deep learning models, architectures require specific featurizers to prepare the data in the correct format. As an example, the ChemPropFeaturizer is selected for ChemProp-family models.

feat:
  type: ChemPropFeaturizer
  params: {}

Transform

The optional transform section applies specified transformations to the feature matrices, after featurization and before training. Transforms are fitted on the train partition only, then applied to the validation, test, and inference features, so learned statistics (imputer means, PCA loadings) never see held-out data. The fitted transforms are saved next to the model and re-applied automatically at inference time.

A single transform is given as a mapping, and a sequence of transforms is given as a list applied in order. Transforms are supported by the scikit-learn workflow (tabular-input models such as LGBM, XGBoost, and TabPFN); the deep learning workflow rejects them because its featurizer emits DataLoaders rather than feature matrices.

Available transforms:

Transform

Description

ImputeTransform

Imputes missing values with the fitted imputer mean, median, most frequent, or constant value.

PCATransform

Reduces feature dimensionality with principal component analysis, over the whole matrix or per feature block.

Example: reduce a fingerprint featurizer to 256 PCA components before training.

procedure:
  feat:
    type: FingerprintFeaturizer
    params:
      fp_type: "ecfp:4"
  transform:
    type: PCATransform
    params:
      n_components: 256
      random_seed: 42

Per-block PCA: give n_components as a mapping from featurizer name (or alias) to component count. A shared imputation step runs first, then each block is reduced by a separate PCA to the count given for it.

procedure:
  feat:
    type: FeatureConcatenator
    params:
      featurizers:
        - type: FingerprintFeaturizer
          params:
            fp_type: "ecfp:4"
        - type: DescriptorFeaturizer
          params:
            descr_type: desc2d
  transform:
    - type: ImputeTransform
      params:
        strategy: median
    - type: PCATransform
      params:
        n_components:
          FingerprintFeaturizer: 256
          DescriptorFeaturizer: 32
        random_seed: 42

That recipe featurizes to 2223 columns: 223 from DescriptorFeaturizer and 2000 from FingerprintFeaturizer at its default bit count. The median imputer fills missing descriptor values, then each block is reduced separately, and the model sees 32 + 256 = 288 columns.

Blocks are ordered by block name, not by the order the recipe lists them, so the descriptor block comes first here even though FingerprintFeaturizer is written above it.

Give a block null instead of a count to pass it through unreduced, which reduces the wide block while the other reaches the model as it was featurized. That suits a block whose columns carry meaning one by one, where a PCA would rotate them into linear combinations and a full-rank entry would do so without reducing the width at all. Passthrough is stated rather than obtained by omitting the key, so the exact-match check still catches a mistyped block name.

procedure:
  feat:
    type: FeatureConcatenator
    params:
      featurizers:
        - type: FingerprintFeaturizer
          params:
            fp_type: "ecfp:4"
        - type: DescriptorFeaturizer
          params:
            descr_type: desc2d
  transform:
    - type: ImputeTransform
      params:
        strategy: median
    - type: PCATransform
      params:
        n_components:
          FingerprintFeaturizer: 256
          DescriptorFeaturizer: null
        random_seed: 42

Model

The model section specifies the model to be used in the workflow. It allows you to define the type of model, its parameters, and any additional configurations required for training and evaluation. Each model type has its own set of options, enabling customization to suit specific tasks and datasets. Refer to the linked OpenADMET API documentation for detailed information on each model’s implementation and usage.

Model Type

Description

ChemPropModel

ChemProp Message Passing Neural Network. Also, used when implementing Chemeleon.

CatBoostClassifierModel

Gradient boosting on decision trees for classification using CatBoost.

CatBoostRegressorModel

Gradient boosting on decision trees for regression using CatBoost.

LGBMClassifierModel

LightGBM classifier.

LGBMRegressorModel

LightGBM regressor.

XGBClassifierModel

XGBoost classifier model implementation

XGBRegressorModel

XGBoost regressor model implementation

RFClassifierModel

scikit-learn Random Forest classifier .

RFRegressorModel

scikit-learn Random Forest regressor .

TabICLClassifierModel

TabICL v2 classification model using the tabicl package.

TabICLRegressorModel

TabICL v2 regression model using the tabicl package.

TabPFNClassifierModel

TabPFN classification model using the basic tabpfn implementation.

TabPFNRegressorModel

TabPFN regression model using the basic tabpfn implementation.

TabPFNPostHocClassifierModel

TabPFN classification model using tabpfn-extensions with posthoc ensembling.

TabPFNPostHocRegressorModel

TabPFN regression model using tabpfn-extensions with posthoc ensembling.

DummyClassifierModel

scikit-learn Dummy Classifier for baseline comparisons.

DummyRegressorModel

scikit-learn Dummy Regressor for baseline comparisons.

SVMClassifierModel

scikit-learn Support Vector Machine classifier (SVC) .

SVMRegressorModel

scikit-learn Support Vector Machine regressor (SVR) .

Example
model:
  type: ChemPropModel
  params:
    depth: 4
    ffn_hidden_dim: 1024
    ffn_hidden_num_layers: 4
    message_hidden_dim: 2048
    dropout: 0.2
    batch_norm: True
    messages: bond
    n_tasks: 1
    from_chemeleon: False

Deep learning models can also be configured to load pretrained weights during initialization, enabling finetuning on new datasets. Classic machine learning models do not have weights or state to load, requiring complete retraining with new data concatenated to the original training set. Paths to a saved model are specified with the serial_path and param_path fields in the model section. Note that any model parameters defined in the params field will be overridden by those loaded from the saved model.

model:
  type: ChemPropModel
  serial_path: PATH_TO_SAVED_MODEL/model.pth
  param_path: PATH_TO_SAVED_MODEL/model_params.json

Finally, select components of the deep learning model can be frozen during training to prevent their weights from being updated. This is done by specifying the freeze_weights field in the model section. Here, the message passing layers are frozen, while batch normalization and feedforward network layers remain trainable.

model:
  type: ChemPropModel
  serial_path: PATH_TO_SAVED_MODEL/model.pth
  param_path: PATH_TO_SAVED_MODEL/model_params.json
  freeze_weights:
    message_passing: true
    batch_norm: false
    ffn_layers: 0

Freezable components vary by model architecture. Please refer to the specific model documentation for details on which parts can be frozen.

Splitting

The split section defines how the dataset is divided into training, validation, and test sets. You can choose from different splitter types, each with its own parameters to control the splitting behavior.

Splitter

Description

ShuffleSplitter

Randomly shuffles and splits the dataset into training, validation, and test sets based on specified proportions.

ScaffoldSplitter

Splits the dataset based on molecular scaffolds to ensure that similar compounds are grouped together in the same set.

MaxDissimilaritySplitter

Splits the dataset based on maximum dissimilarity between training, validation, and test sets, promoting diversity between sets.

PerimeterSplitter

Splits the dataset by selecting compounds at the periphery of the chemical space, ensuring that edge cases are included in the test set.

Example
split:
  type: ShuffleSplitter
  params:
    train_size: 0.7
    val_size: 0.1
    test_size: 0.2
    random_state: 42

Training

The train section configures the training process for the selected model. It allows you to specify the trainer type and various training parameters to control the training workflow.

Trainer

Description

LightningTrainer

Trainer for deep learning models using PyTorch Lightning.

SKLearnBasicTrainer

Basic trainer for sklearn models.

SKLearnGridSearchTrainer

Trainer that performs hyperparameter tuning using specifically grid search for sklearn models (GridSearchCV).

SKLearnSearchTrainer

Trainer that performs hyperparameter tuning using specified search object for sklearn models.

Example
train:
  type: LightningTrainer
  params:
    accelerator: gpu
    early_stopping: true
    early_stopping_patience: 10
    early_stopping_mode: min
    early_stopping_min_delta: 0.001
    max_epochs: 50
    monitor_metric: val_loss
    use_wandb: false
    wandb_project: demos

Ensemble

There is also an optional ensemble section that allows you to specify if you want to train an ensemble of models. You can define the number of models in the ensemble and the calibration method to be used. Currently we only offer a CommitteeRegressor to measure disagreement among the models in the ensemble as the standard deviation of their predictions.

Models can also be calibrated after training using a scaling factor method to improve uncertainty estimates. This functionality is provided by the uncertainty_toolbox package. See UncertaintyMetrics for more details.

Example
ensemble:
  type: CommitteeRegressor
  n_models: 10
  calibration_method: scaling-factor

For deep learning ensembles, each model in the ensemble can load its own pretrained weights by specifying a list of paths in the serial_paths and param_paths fields in the ensemble section.

ensemble:
  type: CommitteeRegressor
  n_models: 10
  calibration_method: scaling-factor
  serial_paths:
  - PATH_TO_SAVED_MODEL_1/model.pth
  - PATH_TO_SAVED_MODEL_2/model.pth
  - ...
  param_paths:
  - PATH_TO_SAVED_MODEL_1/model_params.json
  - PATH_TO_SAVED_MODEL_2/model_params.json
  - ...

Report

The report section specifies the evaluations to be performed after training the model. You can choose from various evaluation types, each with its own parameters to customize the output. Regression models are only compatible with RegressionMetrics and similarly classification models only with ClassificationMetrics.

Importantly, the report section also allows for cross-validation to be performed to evaluate the robustness of the model. Note that cross-validation can be computationally expensive, especially for deep learning models.

Evaluation

Description

RegressionMetrics

Computes regression statistics.

RegressionPlots

Generates plots of predicted vs true values for regression tasks.

ClassificationMetrics

Computes classification statistics.

ClassificationPlots

Generates plots such as ROC and precision-recall curves for classification tasks.

SKLearnRepeatedKFoldCrossValidation

Performs repeated K-Fold cross-validation for sklearn models. It should be noted that performing cross-validation can be computationally expensive.

PytorchLightningRepeatedKFoldCrossValidation

Performs repeated K-Fold cross-validation for PyTorch Lightning models. It should be noted that performing cross-validation can be computationally expensive.

PosthocBinaryMetrics

Compute posthoc binary metrics. Intended to be used for regression-based models to calculate precision and recall metrics for user-input cutoffs. Not intended for binary models.

UncertaintyMetrics

Evaluate uncertainty metrics using uncertainty_toolbox.

UncertaintyPlots

Generates uncertainty plots.

Example

report:
  eval:
  - type: RegressionMetrics
    params: {}
  - type: PytorchLightningRepeatedKFoldCrossValidation
    params:
      axes_labels:
      - True pIC50
      - Predicted pIC50
      n_repeats: 5
      n_splits: 2
      random_state: 42
      pXC50: true
      title: True vs Predicted pIC50 on test set