# chenhsieh/algoverse-jeyashree-ajay-scpm architecture

> Architecture diagram of the GitHub repository chenhsieh/algoverse-jeyashree-ajay-scpm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chenhsieh/algoverse-jeyashree-ajay-scpm
- Repository: https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm

## Overview

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: [`src/attack_run.py`](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/attack_run.py) (Run attack), [`src/adv_train.py`](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/adv_train.py) (Adv train), [`src/data_prep.py`](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/data_prep.py) (Prep splits), [`src/train.py`](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/train.py) (Train head)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_external["External assets"]
  node_vendor_assets[("Vendor data<br/>dataset assets")]
end

subgraph group_prep["Data prep"]
  node_data_prep["Prep splits<br/>pipeline script<br/>[data_prep.py]"]
end

subgraph group_model["Model subsystem"]
  node_train["Train head<br/>training script<br/>[train.py]"]
  node_codonbert_model["CodonBERT<br/>model wrapper<br/>[codonbert_model.py]"]
  node_codons["Codon map<br/>bio rules<br/>[codons.py]"]
  node_common["Shared utils<br/>shared helpers<br/>[common.py]"]
end

subgraph group_attack["Adversarial search"]
  node_ga["GA search<br/>search engine<br/>[ga.py]"]
  node_attack_run["Run attack<br/>attack driver<br/>[attack_run.py]"]
end

subgraph group_analysis["Analysis"]
  node_interpret["Interpret<br/>analysis script<br/>[interpret.py]"]
  node_embed_shift["Embed shift<br/>analysis module<br/>[embed_shift.py]"]
  node_compare_embed_shift["Compare shifts<br/>analysis module"]
end

subgraph group_retrain["Adversarial retraining"]
  node_adv_train["Adv train<br/>iterative loop<br/>[adv_train.py]"]
end

subgraph group_artifacts["Artifacts"]
  node_data_dir[("Prepared data<br/>output directory")]
  node_checkpoints_dir[("Checkpoints<br/>output directory")]
  node_results_dir[("Results<br/>output directory")]
  node_attack_curve["Attack curves<br/>run artifact"]
  node_adv_history["Iter history<br/>run artifact<br/>[history.json]"]
end

node_vendor_assets -->|"source data"| node_data_prep
node_data_prep -->|"writes"| node_data_dir
node_data_prep -->|"feeds"| node_train
node_data_prep -->|"attack pool"| node_attack_run
node_train -->|"uses"| node_codonbert_model
node_codonbert_model -->|"constrains"| node_codons
node_train -->|"saves"| node_checkpoints_dir
node_codonbert_model -->|"shares"| node_common
node_attack_run -->|"searches"| node_ga
node_ga -->|"preserves"| node_codons
node_attack_run -->|"queries"| node_codonbert_model
node_attack_run -->|"writes"| node_results_dir
node_attack_run -->|"emits"| node_attack_curve
node_interpret -->|"reads"| node_results_dir
node_interpret -->|"invokes"| node_embed_shift
node_embed_shift -->|"writes"| node_results_dir
node_compare_embed_shift -->|"compares"| node_embed_shift
node_adv_train -->|"loops"| node_train
node_adv_train -->|"loops"| node_attack_run
node_adv_train -->|"writes"| node_adv_history
node_adv_train -->|"writes"| node_results_dir
node_adv_train -->|"updates"| node_checkpoints_dir

click node_vendor_assets "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/tree/main/vendor"
click node_data_prep "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/data_prep.py"
click node_train "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/train.py"
click node_codonbert_model "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/codonbert_model.py"
click node_codons "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/codons.py"
click node_common "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/common.py"
click node_ga "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/ga.py"
click node_attack_run "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/attack_run.py"
click node_interpret "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/interpret.py"
click node_embed_shift "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/embed_shift.py"
click node_compare_embed_shift "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/compare_embed_shift.py"
click node_adv_train "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/adv_train.py"
click node_data_dir "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/tree/main/data"
click node_checkpoints_dir "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/tree/main/checkpoints"
click node_results_dir "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/tree/main/results"
click node_attack_curve "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/results/attack_attack40_b60_generation_curve.csv"
click node_adv_history "https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/results/adv_train_adv_iter_full/history.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_vendor_assets,node_data_dir,node_checkpoints_dir,node_results_dir,node_attack_curve,node_adv_history toneBlue
class node_data_prep toneAmber
class node_train,node_codonbert_model,node_codons,node_common toneMint
class node_ga,node_attack_run toneRose
class node_interpret,node_embed_shift,node_compare_embed_shift toneIndigo
class node_adv_train toneTeal
```

## Components

### External assets

Bundled CodonBERT data and model inputs used as the starting point.

- [Vendor data](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/tree/main/vendor): dataset assets · 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](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/data_prep.py): pipeline script · 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](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/train.py): training script · Trains the classifier head on prepared splits and writes checkpoints. · `src/train.py`
- [CodonBERT](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/codonbert_model.py): model wrapper · Central model abstraction for encoding, classification, and paired sequence handling. · `src/codonbert_model.py`
- [Codon map](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/codons.py): bio rules · Genetic-code mappings and synonymous codon enumeration. · `src/codons.py`
- [Shared utils](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/common.py): shared helpers · 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](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/ga.py): search engine · Genetic algorithm over synonymous codon swaps for adversarial candidates. · `src/ga.py`
- [Run attack](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/attack_run.py): attack driver · Orchestrates attack execution, success metrics, and result emission. · `src/attack_run.py`

### Analysis

Post-hoc interpretation of successful adversarial examples.

- [Interpret](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/interpret.py): analysis script · Post-hoc analysis of successful attacks and model behavior. · `src/interpret.py`
- [Embed shift](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/embed_shift.py): analysis module · Computes CLS embedding shift and confidence shift summaries. · `src/embed_shift.py`
- [Compare shifts](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/compare_embed_shift.py): analysis module · Compares embedding-shift outputs across runs or conditions. · `src/compare_embed_shift.py`

### Adversarial retraining

Iterative train-attack-retrain workflow.

- [Adv train](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/src/adv_train.py): iterative loop · Alternates training, attacking, and augmenting with adversarial examples. · `src/adv_train.py`

### Artifacts

Prepared datasets, checkpoints, and experiment outputs.

- [Prepared data](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/tree/main/data): output directory · Derived datasets and split artifacts. · `data`
- [Checkpoints](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/tree/main/checkpoints): output directory · Saved model weights and training snapshots. · `checkpoints`
- [Results](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/tree/main/results): output directory · Attack histories, curves, interpretation outputs, and retraining logs. · `results`
- [Attack curves](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/results/attack_attack40_b60_generation_curve.csv): run artifact · Representative generation-curve CSV outputs from attack runs. · `results/attack_attack40_b60_generation_curve.csv`
- [Iter history](https://github.com/chenhsieh/algoverse-jeyashree-ajay-scpm/blob/main/results/adv_train_adv_iter_full/history.json): run artifact · Representative iterative adversarial-training history output. · `results/adv_train_adv_iter_full/history.json`

## Connections

- 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.
