Skip to main content

AlphaGrammar: Grammar-based molecular representation learning

Project description

AlphaGrammar

AlphaGrammar is a grammar-based molecular representation learning framework. It learns a hyperedge-replacement grammar (HRG) over molecular graphs via Monte Carlo Tree Search (MCMC) with a learned agent.

Installation

pip install -e /path/to/AlphaGrammar_pkg

Or from PyPI (once published):

pip install alphagrammar

Pretrained models

After installation, copy the pretrained model files to the package data directory:

DATA_DIR=$(python -c "import alphagrammar, os; print(os.path.join(os.path.dirname(alphagrammar.__file__), 'data'))")
cp /path/to/AlphaGrammar/ckpts/vocab_epoch5.pkl $DATA_DIR/
cp /path/to/AlphaGrammar/ckpts/best_agent_epoch0_R0.0000.pkl $DATA_DIR/
cp /path/to/AlphaGrammar/GCN/supervised_contextpred.pth $DATA_DIR/

Usage

Command-line interface

# Parse a single SMILES string (rollout mode)
alphagrammar parse "CCO"

# Parse from a file (one SMILES per line)
alphagrammar parse molecules.smi

# Parse with Bolinas parser (10-second timeout per molecule)
alphagrammar parse "CCO" --timeout 10

# Use only top-100 rules from vocab
alphagrammar parse "CCO" --vocab_size 100

Python API

from alphagrammar import MoleculeDataset, _collate_mol_batch, RuleStats
from alphagrammar.grammar_generation import MCMC_sampling
from alphagrammar.agent import Agent
import torch, pickle

# Load pretrained models
with open("data/vocab_epoch5.pkl", "rb") as f:
    rule_stats = pickle.load(f)

agent = Agent(feat_dim=300, hidden_size=256)
agent.load_state_dict(torch.load("data/best_agent_epoch0_R0.0000.pkl"))
agent.eval()

# Build input dataset
dataset = MoleculeDataset(["CCO", "c1ccccc1"], GNN_model_path="data/supervised_contextpred.pth")
batch = [dataset[i] for i in range(len(dataset))]
input_graphs_dict = _collate_mol_batch(batch)

# Run MCMC sampling
results, rules_per_mol, sequential_steps = MCMC_sampling(
    "output_dir", agent, input_graphs_dict, MCMC_size=1, debug=True
)

Package structure

src/alphagrammar/
├── __init__.py          # Public API
├── cli.py               # argparse CLI entry point
├── core.py              # Core functions (bolinas_evaluate, MoleculeDataset, RuleStats, ...)
├── grammar_generation.py# MCMC sampling and grammar generation
├── agent.py             # Neural agent (policy network)
├── hrg_td_parser_undirected.py  # Bolinas HRG parser
├── private/             # Internal hypergraph / grammar data structures
├── fuseprop/            # Molecular fragmentation utilities
├── GCN/                 # Graph neural network feature extraction
├── data/                # Pretrained model files (copy here after install)
└── commands/
    └── parse.py         # 'alphagrammar parse' subcommand

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alphagrammar-0.1.2.tar.gz (42.9 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alphagrammar-0.1.2-py3-none-any.whl (43.4 MB view details)

Uploaded Python 3

File details

Details for the file alphagrammar-0.1.2.tar.gz.

File metadata

  • Download URL: alphagrammar-0.1.2.tar.gz
  • Upload date:
  • Size: 42.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for alphagrammar-0.1.2.tar.gz
Algorithm Hash digest
SHA256 eed870039411496cdd6a4d6a898ab552e5fc9005cd46cb5e11070732919e8fd7
MD5 afe0e508548726f2f66b89fbe1b15c43
BLAKE2b-256 5fc582de0a1cb5d0d21da72bcd2f77bbef8ac3df19a7602d88f1e8f0c86e8801

See more details on using hashes here.

File details

Details for the file alphagrammar-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: alphagrammar-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 43.4 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for alphagrammar-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cac09d4033eff26d3f1db28f38bfef93d225b7ed4c552874ea62dfe70ff97d92
MD5 10b7698950120fabacc74e921e971275
BLAKE2b-256 55c71ac075b6829bba8893611a5360ce31a42fdbbd6f93fcbb22e08581cc5b13

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page