EZSolver is a generative model for reaction mechanism prediction that leverages a bidirectional flow matching and bidirectional beam search to predict polar enzymatic reaction mechanisms.
- Flow Matching: Models transition pathways of Bond-Electron (BE) matrices from reactant states to product states.
- Bidirectional Beam Search: Simulates pathways from both the reactant front (forward search) and the product front (backward search) simultaneously, running both
exo(forward prediction) andendo(inverse prediction) at each step to find intersecting intermediate states and reconstruct the complete mechanism. - Physical & Chemical Constraints: Integrates rigorous chemistry filters (octet rule checks, biochemical charge sanity, radical suppression) and 2D atom-mapping masks to prune chemically invalid intermediates.
- Interactive Review Interface: Built-in Jupyter Notebook with a custom
ipywidgetsUI to visualize molecular structures (via RDKit SVG rendering) and label predicted mechanism steps as accurate, plausible, or retrieved.
EZSolver/
โโโ .gitignore # Git ignore file configuration
โโโ checkpoints/ # Model checkpoints folder (place FlowER.pt here)
โโโ EZSolver.py # Main entry point for predicting reaction mechanisms
โโโ EZFlow.py # Evaluation script to compute single-step prediction
โโโ LICENSE # MIT License file
โโโ train.py # Model training script
โโโ settings.py # Configuration & hyperparameter parser (Args class)
โโโ requirements.txt # Python package dependencies list
โโโ review_mechanisms.ipynb # Jupyter Notebook for visual inspection of results
โโโ plot_accuracy.py # Utility for plotting model evaluation accuracies
โโโ run_train.sh # Shell script to start the training pipeline
โโโ run_EZFlow.sh # Shell script to run model evaluation
โโโ run_EZSolver.sh # Shell script to execute mechanism pathfinding
โโโ data/ # Reaction dataset and curation folder
โ โโโ data_curation/ # Raw dataset processing scripts
โ โ โโโ map.py # Atom mapping & polar/radical classification script
โ โ โโโ mmff.py # Force field energy minimization & direction classification script
โ โโโ EZFlow/ # Training/evaluation reaction files
โ โโโ EZSolver/ # Test reaction database JSONs
โโโ utils/ # Utilities folder
โโโ predict_utils.py # Distributed inference helper functions
โโโ data_utils.py # BE matrices operations and datasets
โโโ physical_filters.py # Octet rule, charge sanity, and chemistry evaluation rules
โโโ train_utils.py # Noam scheduler and setup loggers
โโโ map_utils.py # Atom mapping utilities
โโโ rounding.py # Quantization / rounding methods for BE matricesThis project requires Python 3.10.
You can set up a clean Python virtual environment using venv or Conda:
conda create -n ezsolver python=3.10 -y
conda activate ezsolverAll dependencies, including PyTorch configured for CUDA 11.8 and chemistry libraries, can be installed directly via pip:
pip install -r requirements.txtThe pre-trained model checkpoints (such as FlowER.pt) can be downloaded from Hugging Face:
๐ LunHsin/EZSolver on Hugging Face Repository
(Note: Model descriptions can be viewed directly on the Hugging Face model repository page.)
Please download the checkpoint file and place it inside the existing checkpoints/ directory.
The repository contains several datasets under data/ configured for different training and evaluation purposes:
1. Training & Evaluation Datasets (data/EZFlow/)
EZFlow/:train.txt/val.txt: Mapped reaction elementary steps datasets without MMFF energy classification.train_exo.txt/val_exo.txt: Mapped reaction elementary steps datasets with MMFF energy classification (ensuring exothermic directionality).test.txt: The test split of 186 elementary steps, which serves as the test file for evaluating single-step prediction accuracy (e.g. viarun_EZFlow.sh).
OOD_Benchmark/:- Datasets specifically curated for training comparison against baseline models EZMechanism and Mechfind.
2. Mechanism Search Evaluation Datasets (data/EZSolver/)
The JSON files in this directory are target reactions used to evaluate EZSolver's multi-step mechanism pathfinding:
EZSolver_test.json: The Out-of-Distribution (OOD) task dataset established in this work to evaluate mechanism prediction.OOD_Benchmark.json: The benchmark dataset used to compare EZSolver's mechanism pathfinding performance against EZMechanism and Mechfind.
To start training the conditional flow matching model on reaction datasets, run:
sh run_train.shConfigure your data paths (e.g. TRAIN_FILE, VAL_FILE) and hyperparameters (e.g., learning rate LR, batch size, etc.) directly in run_train.sh or through setting environment variables.
To evaluate the top-k prediction performance of the flow matching model for both standard (exo) and inverse (endo) reaction directions:
sh run_EZFlow.shDetails & Parameters:
- Input test file: Configured via
TEST_FILE(default:data/EZFlow/EZFlow/test.txt- a text file containing reaction SMILES). - Model checkpoint: Loaded via
LOAD_FROM(default:checkpoints/EZFlow.pt). - Key parameters:
SAMPLE_SIZE(default200): Number of ODE integration steps / trajectory samples generated per reaction.SIGMA(default0.08): Standard deviation for flow matching training/inference.
- Output files:
- A success log summarizing top-k accuracy statistics of both directions, saved to
${RESULT_PATH}/{SAMPLE_SIZE}_prediction.txt(default:EZFlow_results/{SAMPLE_SIZE}_prediction.txt). - Detailed execution log saved in
logs/ezflow/.
- A success log summarizing top-k accuracy statistics of both directions, saved to
To perform a complete bidirectional search on multi-step reactions and reconstruct the pathway of intermediate states:
sh run_EZSolver.shDetails & Workflow:
- Script executed:
EZSolver.py(which runs local evaluation and search algorithm). - Workflow:
- Load Inputs: Reads a JSON database of target chemical reactions from the path in
REACTIONS(default:data/EZSolver/EZSolver_test.json). - Bidirectional Search: Simulates reaction pathways from both ends simultaneously. For both the reactant front (forward search) and the product front (backward search), the solver runs both
exo(standard flow) andendo(inverse flow) at each step to find intersecting intermediate states. - Pruning & Filtering: Evaluates intermediates and penalizes paths violating chemical constraints (like octet rules, invalid radical formations, strained ring formation) and 2D masks.
- Output Results: Reconstructs and sorts the matched paths by length and score, and exports them to a JSON file at
MECHANISM_RESULTS(default:EZSolver_results/20.json).
- Load Inputs: Reads a JSON database of target chemical reactions from the path in
- Configurable variables in
run_EZSolver.sh:LOAD_FROM: Path to the model checkpoint.ptfile (e.g.checkpoints/EZFlow.pt).BEAM_SIZE(default10): Beam search width for pathway tracking.MAX_DEPTH(default4): Maximum search depth (maximum number of steps).SAMPLE_SIZE(default160): Candidate sample size generated for each intermediate step.
The repository includes review_mechanisms.ipynb to visually inspect and annotate search results using an interactive browser-based UI (ipywidgets + rdkit).
- Configure File Path (Cell 2):
- Modify the
JSON_FILE = Path("EZSolver_results/your_result.json")line to target your solved JSON file (e.g.,EZSolver_results/EZSolver_20.json). - The notebook automatically determines the sample name, sets up column mappings (
top1andtop5), and designates the output toyour_result_results.csv.
- Modify the
- Initialize / Load CSV (Cell 3):
- Run the cell to load any existing annotations. It imports previous labels (fully compatible with legacy column names), pre-fills
nonefor invalid reactions, and populates thetimecolumn with the execution times in seconds.
- Run the cell to load any existing annotations. It imports previous labels (fully compatible with legacy column names), pre-fills
- Compile Helpers & Launch UI (Cells 4 & 5):
- Run these cells to render the molecular SVG grid and the control dashboard.
โ Accurate/~ Plausible/โฉ Retrieved: Clicking these buttons labels the current mechanism candidate, immediately auto-saves the CSV file, and jumps to the next unlabeled reaction.โช Next/โ Back: Manually navigate forward or backward to review reactions.โ Prev Cand/โ Next Cand: If the beam search found multiple candidate pathways for a reaction, use these to cycle through and inspect different predicted mechanism candidates.
Before training or evaluation, raw reaction datasets can be curated, atom-mapped, and direction-classified using the scripts inside data/data_curation/.
1. Atom Mapping & Classification (map.py)
To map atoms (including hydrogen mapping and proton transfers) and classify reactions by chemistry (Polar vs. Radical) and elements (metal vs. non-metal):
python map.py- Input: Reads MCSA_reactions.json.
- Process: Uses
rxnmapperto generate attention-guided atom mappings, checks mapping consistency, maps hydrogens/protons, and determines if reactions are radical/polar or metal-containing. - Output: Saves the complete mapped dictionary to mapped_reactions.json and splits them into category JSONs in the mapped_reactions/ directory (e.g.,
Polar_NM.json,Polar_M.json,Radical_NM.json,Radical_M.json).
2. Thermodynamic Classification (mmff.py)
To classify the reaction directions (Exothermic vs. Endothermic) using molecular force field energy calculations:
python mmff.py- Input: Reads the JSON files inside the mapped_reactions/ directory.
-
Process:
- Evaluation: Automatically splits disconnected components in reaction mixtures to calculate energies individually and sum them up (avoiding slow 3D conformation searches on disconnected complexes).
- Unsupported Atom Filtering: Skips conformation embedding for unsupported metal-containing complexes to avoid massive CPU overhead.
-
Direction Classification: Computes MMFF94 force field energy changes (
$\Delta E = E_{\text{product}} - E_{\text{reactant}}$ ) to classify reaction directions.
-
Output: Writes training-formatted lists of reaction SMILES (with trailing
|format) into the same folder:-
*_fwd.txt: Reactions that are exothermic ($\Delta E < 0$ ). -
*_bwd.txt: Reactions that are endothermic ($\Delta E > 0$ ). -
*_combined.txt: Flipped backward reactions combined with forward reactions to keep all pathways exothermic.
-
This project is licensed under the MIT License - see the LICENSE file for details.