# satvik8954/generics architecture

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

- Interactive diagram: https://gitdiagram.com/satvik8954/generics
- Repository: https://github.com/satvik8954/generics

## Overview

#### Purpose and Entry Points
ExciPick is a PyTorch pipeline for multilabel prediction of inactive pharmaceutical ingredients from an API’s molecular properties, dose/per-unit context, administration route, and dosage form. Operational entry points are `compute_features.py`, `preprocess.py`, `training.py`, `test.py`, and likely inference via `predict.py`.

#### Source Data and Molecular Feature Stage
FDA SPL/NDC formulation records enter from `Data/f3.csv`. `compute_features.py` uses RDKit to transform API SMILES into 20 fixed molecular descriptors and persists them in `Data/api_features.csv`. This isolates chemistry-specific dependency and feature extraction from model training.

#### Preprocessing and Dataset Contract
`preprocess.py` parses formulation excipient labels, joins descriptor rows, normalizes dose (including log-normalized mg), and creates categorical vocabularies for excipients, route, dosage form, and per-unit information. Its processed artifact is consumed by splitting/training rather than raw CSVs. `dataset.py` defines the PyTorch dataset/batch-facing representation, establishing the boundary between tabular preprocessing and tensor training.

#### Leakage-Resistant Split Stage
`split.py` partitions formulations by API chemical-similarity clusters (Morgan fingerprint/Tanimoto-based per README), rather than random records. This keeps chemically related APIs from spanning train, validation, and test partitions. Training produces `test_data.pkl`, preserving the held-out evaluation input.

#### Neural Model Assembly
`model/FULL_MODEL.py` composes four learned stages:
- `model/api_encoder.py`: 20-descriptor API vector through a 20→256→128 MLP.
- `model/strength_encoder.py`: dose and per-unit encoding into a 64-dimensional representation.
- `model/context_fusion.py`: combines API and strength encodings with learned route and dosage-form embeddings into a 192-dimensional context.
- `model/excipient_scorer.py`: scores the context against the complete excipient vocabulary, producing one score per excipient.

This is a shared-context, multi-label ranking/classification design: a single formulation context is evaluated across all candidate excipients.

#### Training, Checkpointing, and Evaluation
`training.py` orchestrates cluster-aware splits, PyTorch optimization (Adam per README), validation, and best-checkpoint selection, saving `best_model.pt` by validation loss. `test.py` reloads the trained system and evaluates held-out data; it also has a smoke-test path. `metrics.py` evaluates ranked multilabel outputs using Precision, Recall, F1, and Jaccard at K.

#### Runtime and External Dependencies
The runtime is Python 3.10+ with PyTorch, optionally CUDA with CPU fallback. RDKit is required for descriptor generation and similarity-aware splitting; pandas/numpy support tabular transformations, and scikit-learn supports ML preprocessing/splitting-related work. Central model dimensions, optimization settings, device choice, and top-K defaults reside in `config.py`. Primary persisted boundaries are CSV source/features, preprocessing artifacts, `test_data.pkl`, and `best_model.pt`.

Main entry points: [`training.py`](https://github.com/satvik8954/generics/blob/main/training.py) (Training orchestration), [`config.py`](https://github.com/satvik8954/generics/blob/main/config.py) (Central configuration), [`dataset.py`](https://github.com/satvik8954/generics/blob/main/dataset.py) (Tensor dataset contract), [`split.py`](https://github.com/satvik8954/generics/blob/main/split.py) (Chemical-cluster split)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data pipeline"]
  node_source_records[("FDA SPL/NDC records<br/>CSV source<br/>[f3.csv]")]
  node_feature_compute["Molecular descriptors<br/>RDKit feature job"]
  node_api_features[("API feature table<br/>CSV artifact<br/>[api_features.csv]")]
  node_preprocess["Formulation preprocessing<br/>tabular pipeline<br/>[preprocess.py]"]
  node_dataset["Tensor dataset contract<br/>PyTorch dataset<br/>[dataset.py]"]
  node_split["Chemical-cluster split<br/>RDKit split stage<br/>[split.py]"]
  node_heldout_data["Held-out test data<br/>pickle artifact"]
end

subgraph group_model["Neural model"]
  node_api_encoder["API encoder<br/>20→256→128 MLP<br/>[api_encoder.py]"]
  node_strength_encoder["Strength encoder<br/>dose/per-unit encoder"]
  node_context_fusion["Context fusion<br/>embedding fusion<br/>[context_fusion.py]"]
  node_excipient_scorer["Excipient scorer<br/>multilabel scorer"]
  node_full_model["Full model assembly<br/>PyTorch model<br/>[FULL_MODEL.py]"]
end

subgraph group_runtime["Training and evaluation"]
  node_training["Training orchestration<br/>PyTorch training<br/>[training.py]"]
  node_checkpoint["Best model checkpoint<br/>model artifact"]
  node_evaluation["Held-out evaluation<br/>test runtime<br/>[test.py]"]
  node_metrics["Top-K metrics<br/>ranking metrics<br/>[metrics.py]"]
  node_inference["Prediction runtime<br/>inference entry point<br/>[predict.py]"]
  node_config["Central configuration<br/>runtime settings<br/>[config.py]"]
end

node_source_records -->|"API SMILES"| node_feature_compute
node_feature_compute -->|"writes descriptors"| node_api_features
node_source_records -->|"formulation fields"| node_preprocess
node_api_features -->|"descriptor join"| node_preprocess
node_preprocess -->|"processed records and vocabularies"| node_dataset
node_dataset -->|"formulations"| node_split
node_split -->|"train/validation partitions"| node_training
node_split -->|"persists test partition"| node_heldout_data
node_api_encoder -->|"128-d API representation"| node_context_fusion
node_strength_encoder -->|"64-d strength representation"| node_context_fusion
node_context_fusion -->|"192-d context"| node_excipient_scorer
node_excipient_scorer -->|"candidate excipient scores"| node_full_model
node_dataset -->|"tensor batches"| node_training
node_full_model -->|"model architecture"| node_training
node_training -->|"best validation loss"| node_checkpoint
node_heldout_data -->|"held-out inputs"| node_evaluation
node_checkpoint -->|"reloads model"| node_evaluation
node_evaluation -->|"ranked multilabel outputs"| node_metrics
node_checkpoint -->|"loads trained model"| node_inference
node_config -.->|"optimization and device settings"| node_training
node_config -.->|"model dimensions"| node_full_model
node_config -.->|"top-K defaults"| node_metrics

click node_source_records "https://github.com/satvik8954/generics/blob/main/Data/f3.csv"
click node_feature_compute "https://github.com/satvik8954/generics/blob/main/compute_features.py"
click node_api_features "https://github.com/satvik8954/generics/blob/main/Data/api_features.csv"
click node_preprocess "https://github.com/satvik8954/generics/blob/main/preprocess.py"
click node_dataset "https://github.com/satvik8954/generics/blob/main/dataset.py"
click node_split "https://github.com/satvik8954/generics/blob/main/split.py"
click node_api_encoder "https://github.com/satvik8954/generics/blob/main/model/api_encoder.py"
click node_strength_encoder "https://github.com/satvik8954/generics/blob/main/model/strength_encoder.py"
click node_context_fusion "https://github.com/satvik8954/generics/blob/main/model/context_fusion.py"
click node_excipient_scorer "https://github.com/satvik8954/generics/blob/main/model/excipient_scorer.py"
click node_full_model "https://github.com/satvik8954/generics/blob/main/model/FULL_MODEL.py"
click node_training "https://github.com/satvik8954/generics/blob/main/training.py"
click node_evaluation "https://github.com/satvik8954/generics/blob/main/test.py"
click node_metrics "https://github.com/satvik8954/generics/blob/main/metrics.py"
click node_inference "https://github.com/satvik8954/generics/blob/main/predict.py"
click node_config "https://github.com/satvik8954/generics/blob/main/config.py"

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_source_records,node_feature_compute,node_api_features,node_preprocess,node_dataset,node_split,node_heldout_data toneBlue
class node_api_encoder,node_strength_encoder,node_context_fusion,node_excipient_scorer,node_full_model toneAmber
class node_training,node_checkpoint,node_evaluation,node_metrics,node_inference,node_config toneMint
```

## Components

### Data pipeline

- [FDA SPL/NDC records](https://github.com/satvik8954/generics/blob/main/Data/f3.csv): CSV source · Formulation records with API, excipient, dose, route, and dosage-form data. · `Data/f3.csv`
- [Molecular descriptors](https://github.com/satvik8954/generics/blob/main/compute_features.py): RDKit feature job · Computes 20 fixed descriptors from API SMILES. · `compute_features.py`
- [API feature table](https://github.com/satvik8954/generics/blob/main/Data/api_features.csv): CSV artifact · `Data/api_features.csv`
- [Formulation preprocessing](https://github.com/satvik8954/generics/blob/main/preprocess.py): tabular pipeline · Parses labels, joins descriptors, normalizes dose, and builds vocabularies. · `preprocess.py`
- [Tensor dataset contract](https://github.com/satvik8954/generics/blob/main/dataset.py): PyTorch dataset · Converts processed formulation data into batch-facing tensors. · `dataset.py`
- [Chemical-cluster split](https://github.com/satvik8954/generics/blob/main/split.py): RDKit split stage · Partitions API formulations by Morgan fingerprint and Tanimoto similarity clusters. · `split.py`
- **Held-out test data**: pickle artifact · Persisted test partition produced during training.

### Neural model

- [API encoder](https://github.com/satvik8954/generics/blob/main/model/api_encoder.py): 20→256→128 MLP · `model/api_encoder.py`
- [Strength encoder](https://github.com/satvik8954/generics/blob/main/model/strength_encoder.py): dose/per-unit encoder · Produces a 64-dimensional strength representation. · `model/strength_encoder.py`
- [Context fusion](https://github.com/satvik8954/generics/blob/main/model/context_fusion.py): embedding fusion · Combines API and strength features with route and dosage-form embeddings. · `model/context_fusion.py`
- [Excipient scorer](https://github.com/satvik8954/generics/blob/main/model/excipient_scorer.py): multilabel scorer · Scores the fused context against the complete excipient vocabulary. · `model/excipient_scorer.py`
- [Full model assembly](https://github.com/satvik8954/generics/blob/main/model/FULL_MODEL.py): PyTorch model · Assembles the shared-context multilabel predictor. · `model/FULL_MODEL.py`

### Training and evaluation

- [Training orchestration](https://github.com/satvik8954/generics/blob/main/training.py): PyTorch training · Optimizes, validates, and selects the best checkpoint. · `training.py`
- **Best model checkpoint**: model artifact · Selected by validation loss.
- [Held-out evaluation](https://github.com/satvik8954/generics/blob/main/test.py): test runtime · Reloads the model and evaluates held-out data; includes a smoke-test path. · `test.py`
- [Top-K metrics](https://github.com/satvik8954/generics/blob/main/metrics.py): ranking metrics · Computes Precision, Recall, F1, and Jaccard at K. · `metrics.py`
- [Prediction runtime](https://github.com/satvik8954/generics/blob/main/predict.py): inference entry point · `predict.py`
- [Central configuration](https://github.com/satvik8954/generics/blob/main/config.py): runtime settings · Defines dimensions, optimization, device, and top-K defaults. · `config.py`

## Connections

- FDA SPL/NDC records → Molecular descriptors: API SMILES
- Molecular descriptors → API feature table: writes descriptors
- FDA SPL/NDC records → Formulation preprocessing: formulation fields
- API feature table → Formulation preprocessing: descriptor join
- Formulation preprocessing → Tensor dataset contract: processed records and vocabularies
- Tensor dataset contract → Chemical-cluster split: formulations
- Chemical-cluster split → Training orchestration: train/validation partitions
- Chemical-cluster split → Held-out test data: persists test partition
- API encoder → Context fusion: 128-d API representation
- Strength encoder → Context fusion: 64-d strength representation
- Context fusion → Excipient scorer: 192-d context
- Excipient scorer → Full model assembly: candidate excipient scores
- Tensor dataset contract → Training orchestration: tensor batches
- Full model assembly → Training orchestration: model architecture
- Training orchestration → Best model checkpoint: best validation loss
- Held-out test data → Held-out evaluation: held-out inputs
- Best model checkpoint → Held-out evaluation: reloads model
- Held-out evaluation → Top-K metrics: ranked multilabel outputs
- Best model checkpoint → Prediction runtime: loads trained model
- Central configuration → Training orchestration: optimization and device settings
- Central configuration → Full model assembly: model dimensions
- Central configuration → Top-K metrics: top-K defaults
