Custom Callbacks for Prediction#
Chemprop’s predict subcommand can be invoked with the --callback <cb> flag, allowing for the execution of custom code during the prediction process.
Interpretability Callbacks#
Myerson Values#
A Myerson explainer calculates and saves Myerson explanations during a predict call using the myerson package.
It can be invoked by passing --callback myerson to the chemprop predict command.
The explanations take the form of node attributions and are saved as a compressed NumPy archive (.npz file). Each molecule’s explanation is saved as a separate array within the archive (e.g., arr_0, arr_1, etc.).
Each array will be a 1D or 2D NumPy array of shape num_atoms (for regression or binary classification) or num_atoms x num_labels (for multi-label classification) containing the explanation for one molecule. Alternatively, the explanations can be saved as a JSON file by setting the save_as_json parameter to true.
When saved as a JSON file, the output is a list of explanations, where each explanation corresponds to a molecule. For 2D explanations (multi-class), each inner list represents a column (i.e., attributions for a specific class across all atoms).
By default, molecules with more than 20 nodes will use a sampling explainer instead of the exact explainer.
This threshold is controlled by the sampling_threshold parameter, which can be set using the --callback-params flag, which expects a JSON string.
Myerson Explanations are not supported for atom or bond level predictions. Furthermore, they are only implemented for models using a BinaryClassificationFFN or RegressionFFN.
chemprop predict --test-path tests/data/smis.csv --model-paths tests/data/example_model_v2_regression_mol.ckpt --callback myerson
chemprop predict --test-path tests/data/smis.csv --model-paths tests/data/example_model_v2_regression_mol.ckpt --callback myerson --callback-params '{"sampling_threshold": 25}'
chemprop predict --test-path tests/data/smis.csv --model-paths tests/data/example_model_v2_regression_mol.ckpt --callback myerson --callback-params '{"save_as_json": true}'