How chenhsieh/algoverse-jeyashree-ajay-scpm works
1. What this repo is
- A Python/PyTorch pipeline for black-box adversarial evaluation of CodonBERT on viral vs host CDS classification.
- The core idea is to keep the translated protein unchanged while attacking the classifier via synonymous codon substitutions.
2. Main architectural boundary
- There is a clean split between:
- Data preparation (src/data_prep.py)
- Model training/inference (src/train.py, src/codonbert_model.py)
- Adversarial search (src/ga.py, src/attack_run.py)
- Interpretation/analysis (src/interpret.py, src/embed_shift.py, src/compare_embed_shift.py)
- Iterative adversarial training (src/adv_train.py)
- This makes the repository more of a research pipeline than a single application.
3. Data sources and leakage control
- Input data comes from CodonBERT’s pretrain/eval CSVs under vendor/codonbert/data/pre-train/.
- data_prep.py constructs disjoint train/val/test/attack splits with explicit anti-leakage checks:
- train/val only from train_samples.csv
- test from eval.csv
- attack pool from viral sequences in eval.csv, disjoint from test
- It deduplicates by both ID and sequence, and excludes attack IDs from test.
4. Model subsystem
- src/codonbert_model.py is the central model abstraction.
- It likely wraps:
- Codon tokenization / sequence encoding
- the CodonBERT encoder
- the classifier head on top of the [CLS] embedding
- pair-sequence utilities for later interpretation
- The README indicates the encoder is frozen during standard head training.
5. Training path
- src/train.py trains the classification head on the prepared splits.
- The runtime is standard Python with PyTorch-style device selection; the repo defaults to Apple MPS, but supports CUDA/CPU.
- Checkpoints are written under checkpoints/, and experiment-specific artifacts are tagged for reuse by downstream scripts.
6. Attack subsystem
- src/ga.py implements the genetic algorithm that searches over synonymous codon swaps.
- The search constraint is biologically important: it preserves the amino-acid sequence while attempting to flip the model from viral → host.
- src/attack_run.py orchestrates the attack loop, measures attack success rate, and emits JSON/curve artifacts into results/.
7. Interpretation subsystem
- src/interpret.py analyzes successful adversarial examples after the attack.
- The README describes three main explanation views:
- pair-sequence STP on original vs adversarial sequences
- CLS embedding shift
- attention
- This is a post-hoc analysis stage that consumes the attack outputs and the trained checkpoint.
8. Iterative adversarial training loop
- src/adv_train.py closes the loop by alternating between:
- training on the current dataset
- attacking the model
- adding adversarial examples back into training
- This corresponds to the “baseline + N attack/retrain rounds” workflow shown in the README and the results/adv_train_* artifacts.
9. Shared biological utilities
- src/codons.py holds the genetic-code logic: codon-to-amino-acid mappings and synonym enumeration.
- This is the key constraint layer that keeps the attack in the synonymous mutation boundary.
- src/common.py likely contains shared plumbing such as argument parsing, logging, checkpoint paths, and helper utilities used across scripts.
10. Analysis outputs and experiment artifacts
- results/ stores run products such as:
- attack history JSON
- generation curves CSVs
- embedding/confidence shift JSON
- iterative adversarial training histories
- checkpoints/ stores model weights, while data/ is reserved for prepared datasets.
- These directories form the output boundary of the pipeline: preprocessing feeds training, training feeds attacks, attacks feed interpretation and iterative retraining.
11. Repository structure for a diagram
- A good architecture diagram should show a left-to-right flow:
- vendor data + codon model assets
- → data preparation / split enforcement
- → classifier training
- → GA-based synonym attack
- → interpretation and analysis
- → optional adversarial retraining loop
- The key external dependency is the CodonBERT model and CSVs under vendor/, while all derived artifacts are written to data/, checkpoints/, and results/.
Main entry points: attack_run.py Run attack · adv_train.py Adv train · data_prep.py Prep splits · train.py Train head
Components 17
External assets
Bundled CodonBERT data and model inputs used as the starting point.
- Vendor data · CodonBERT pretrain/eval CSV inputs and related bundled resources.
vendor
Data prep
Split construction and leakage control for train/val/test/attack sets.
- Prep splits · Builds disjoint train/val/test/attack splits with anti-leakage checks.
src/data_prep.py
Model subsystem
CodonBERT wrapper and shared utilities for classification.
- Train head · Trains the classifier head on prepared splits and writes checkpoints.
src/train.py - CodonBERT · Central model abstraction for encoding, classification, and paired sequence handling.
src/codonbert_model.py - Codon map · Genetic-code mappings and synonymous codon enumeration.
src/codons.py - Shared utils · Cross-cutting plumbing such as configuration, logging, and utility functions.
src/common.py
Adversarial search
Genetic search over synonymous codon substitutions and attack execution.
- GA search · Genetic algorithm over synonymous codon swaps for adversarial candidates.
src/ga.py - Run attack · Orchestrates attack execution, success metrics, and result emission.
src/attack_run.py
Analysis
Post-hoc interpretation of successful adversarial examples.
- Interpret · Post-hoc analysis of successful attacks and model behavior.
src/interpret.py - Embed shift · Computes CLS embedding shift and confidence shift summaries.
src/embed_shift.py - Compare shifts · Compares embedding-shift outputs across runs or conditions.
src/compare_embed_shift.py
Adversarial retraining
Iterative train-attack-retrain workflow.
- Adv train · Alternates training, attacking, and augmenting with adversarial examples.
src/adv_train.py
Artifacts
Prepared datasets, checkpoints, and experiment outputs.
- Prepared data · Derived datasets and split artifacts.
data - Checkpoints · Saved model weights and training snapshots.
checkpoints - Results · Attack histories, curves, interpretation outputs, and retraining logs.
results - Attack curves · Representative generation-curve CSV outputs from attack runs.
results/attack_attack40_b60_generation_curve.csv - Iter history · Representative iterative adversarial-training history output.
results/adv_train_adv_iter_full/history.json
Connections 22
- Vendor data → Prep splits: source data · CodonBERT CSVs feed split construction.
- Prep splits → Prepared data: writes · Prepared splits are stored under the data directory.
- Prep splits → Train head: feeds · Train and validation splits are consumed for head training.
- Prep splits → Run attack: attack pool · Attack candidates are selected from the prepared disjoint pool.
- Train head → CodonBERT: uses · Training runs through the central model wrapper.
- CodonBERT → Codon map: constrains · Synonymous mutation logic depends on codon-to-amino-acid mapping.
- Train head → Checkpoints: saves · Trained weights and snapshots are written to checkpoints.
- CodonBERT → Shared utils: shares · Common utilities support model/runtime plumbing.
- Run attack → GA search: searches · Attack execution delegates candidate generation to the GA.
- GA search → Codon map: preserves · Search is restricted to synonymous codon substitutions.
- Run attack → CodonBERT: queries · Attack scoring uses the trained CodonBERT classifier.
- Run attack → Results: writes · Attack metrics and histories are emitted into results.
- Run attack → Attack curves: emits · Generation curves are one representative attack artifact.
- Interpret → Results: reads · Interpretation consumes attack outputs and checkpoints.
- Interpret → Embed shift: invokes · Embedding-shift analysis is part of the interpretation stage.
- Embed shift → Results: writes · Shift summaries are stored as results artifacts.
- Compare shifts → Embed shift: compares · Comparison operates on embedding-shift outputs.
- Adv train → Train head: loops · Each round retrains on the current augmented dataset.
- Adv train → Run attack: loops · Each round attacks the current model to mine adversarial examples.
- Adv train → Iter history: writes · Iterative training history is stored in results.
- Adv train → Results: writes · Round artifacts and cumulative outputs land in results.
- Adv train → Checkpoints: updates · Retraining refreshes checkpoints for downstream use.