# taotao917/machine-learning-design-patterns-detection architecture

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

- Interactive diagram: https://gitdiagram.com/taotao917/machine-learning-design-patterns-detection
- Repository: https://github.com/taotao917/machine-learning-design-patterns-detection

## Overview

#### Purpose and execution model
This is a Python 3.10+ batch ML pipeline that classifies free text into Machine Learning Design Pattern (MLDP) categories using TensorFlow/Keras TextCNN. It is operated from the repository root through module entry points: `scripts/build_tokenizer.py`, `scripts/train.py`, `scripts/evaluate.py`, and `scripts/explain_lime.py`. Central behavior is configured by `configs/textcnn.yaml`.

#### Input data and label boundary
Ready-to-run supervised splits are TSV-like, one `text\tlabel_id` record per line: `data/train_data.txt` and `data/val_data.txt`. Class IDs are 0-based and may be translated to display names through `data/labels/id_to_name.json`. Raw-data acquisition is optional and isolated in `scripts/download_data.sh`; training consumes prepared splits rather than requiring external collection.

#### Tokenization and vectorization stage
`scripts/build_tokenizer.py` fits and persists the text representation used by later stages. The data abstraction in `utils/data_io.py` loads records, encodes labels, and vectorizes text for either whitespace tokenization or a Hugging Face tokenizer mode. Tokenizer mode, vocabulary limits, token normalization, and sequence length originate in `configs/textcnn.yaml`; generated tokenizer/statistics artifacts form the compatibility boundary between training, evaluation, and explanation.

#### TextCNN model boundary
`models/textcnn.py` defines the serializable classifier: embedding input, parallel multi-width Conv1D feature extractors, global max pooling, concatenation, dropout, and softmax classification. It also provides `ReverseTemporal`, supporting a reversed temporal stream. Model width, embedding size, filter sizes, dropout, regularization, and number of classes are configuration-controlled in `configs/textcnn.yaml`. Consumers loading saved models must register this custom layer.

#### Training orchestration
`scripts/train.py` combines configured data/vectorization with the TextCNN model and executes Keras training. It applies reproducibility controls from `utils/seed.py`, training callbacks from `utils/callbacks.py`, and optional imbalance strategies—balanced class weights or focal loss—under config control. The run writes a timestamped model checkpoint plus resolved config and metrics under the configured `checkpoints/textcnn/` root.

#### Evaluation and reporting
`scripts/evaluate.py` loads a saved checkpoint, reconstructs the matching tokenizer/vectorization path, and scores the validation split. Metrics and visual reporting are centralized in `utils/metrics.py`; evaluation produces classification reports and confusion-matrix/per-class PRF figures below the configured `results/` root. This is an offline reporting flow, not an inference service.

#### Explainability flow
`scripts/explain_lime.py` loads the trained model and tokenizer and exposes a raw-text prediction wrapper to LIME. It can select a validation example or accept direct text, then emits token-attribution HTML artifacts under results. The explanation boundary is intentionally raw text: LIME tokenization is aligned with the whitespace tokenizer’s code-token preservation behavior, while model-facing vectorization remains delegated to `utils/data_io.py`.

#### Runtime and dependencies
The primary runtime is Python with TensorFlow/Keras for model construction, training, persistence, and inference. Supporting dependencies declared in `requirements.txt` include data/plotting and LIME capabilities; optional Hugging Face tokenizer support is accommodated by `utils/data_io.py`. The only described external service is optional Google Drive download via `scripts/download_data.sh`; the normal pipeline runs entirely from repository-local data and generated artifacts.

Main entry points: [`scripts/train.py`](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/scripts/train.py) (Train model), [`scripts/evaluate.py`](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/scripts/evaluate.py) (Evaluate checkpoint), [`scripts/explain_lime.py`](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/scripts/explain_lime.py) (LIME explanations), [`configs/textcnn.yaml`](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/configs/textcnn.yaml) (TextCNN configuration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data &amp; representation"]
  node_raw_download["Optional data download<br/>shell acquisition<br/>[download_data.sh]"]
  node_train_split["Training split<br/>supervised TSV<br/>[train_data.txt]"]
  node_validation_split["Validation split<br/>supervised TSV<br/>[val_data.txt]"]
  node_label_names["Label display names<br/>label map<br/>[id_to_name.json]"]
  node_data_io["Data loading &amp; vectorization<br/>Python module<br/>[data_io.py]"]
  node_tokenizer_build["Build tokenizer<br/>batch entry point<br/>[build_tokenizer.py]"]
  node_tokenizer_artifacts[("Tokenizer &amp; statistics<br/>generated artifacts")]
end

subgraph group_training["Training"]
  node_textcnn["TextCNN classifier<br/>Keras model<br/>[textcnn.py]"]
  node_train["Train model<br/>batch entry point<br/>[train.py]"]
  node_seed["Reproducibility controls<br/>Python module<br/>[seed.py]"]
  node_callbacks["Training callbacks<br/>Python module<br/>[callbacks.py]"]
  node_checkpoints[("Model checkpoints<br/>run artifacts")]
end

subgraph group_analysis["Offline analysis"]
  node_evaluate["Evaluate checkpoint<br/>batch entry point<br/>[evaluate.py]"]
  node_metrics["Metrics &amp; plots<br/>Python module<br/>[metrics.py]"]
  node_lime["LIME explanations<br/>batch entry point<br/>[explain_lime.py]"]
  node_results[("Reports &amp; explanations<br/>offline artifacts")]
end

node_config["TextCNN configuration<br/>YAML config<br/>[textcnn.yaml]"]
node_dependencies["Python dependencies<br/>dependency manifest<br/>[requirements.txt]"]

node_raw_download -.->|"prepares"| node_train_split
node_raw_download -.->|"prepares"| node_validation_split
node_config -->|"tokenizer settings"| node_tokenizer_build
node_train_split -->|"fit text"| node_tokenizer_build
node_tokenizer_build -->|"persists"| node_tokenizer_artifacts
node_config -->|"vectorization settings"| node_data_io
node_data_io -->|"loads/saves representation"| node_tokenizer_artifacts
node_config -->|"architecture settings"| node_textcnn
node_train_split -->|"supervised records"| node_train
node_data_io -->|"vectorized inputs"| node_train
node_tokenizer_artifacts -->|"shared representation"| node_train
node_textcnn -->|"builds classifier"| node_train
node_seed -->|"sets seeds"| node_train
node_callbacks -->|"applies callbacks"| node_train
node_train -->|"writes model and run metadata"| node_checkpoints
node_checkpoints -->|"loads saved model"| node_evaluate
node_validation_split -->|"scores"| node_evaluate
node_tokenizer_artifacts -->|"reconstructs inputs"| node_evaluate
node_evaluate -->|"metrics inputs"| node_metrics
node_label_names -->|"class names"| node_metrics
node_metrics -->|"writes reports and figures"| node_results
node_checkpoints -->|"loads model"| node_lime
node_tokenizer_artifacts -->|"vectorizes predictions"| node_lime
node_validation_split -.->|"optional example"| node_lime
node_lime -->|"writes attribution HTML"| node_results
node_dependencies -.->|"runtime"| node_train
node_dependencies -.->|"runtime"| node_evaluate
node_dependencies -.->|"runtime"| node_lime

click node_config "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/configs/textcnn.yaml"
click node_raw_download "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/scripts/download_data.sh"
click node_train_split "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/data/train_data.txt"
click node_validation_split "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/data/val_data.txt"
click node_label_names "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/data/labels/id_to_name.json"
click node_data_io "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/utils/data_io.py"
click node_tokenizer_build "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/scripts/build_tokenizer.py"
click node_textcnn "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/models/textcnn.py"
click node_train "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/scripts/train.py"
click node_seed "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/utils/seed.py"
click node_callbacks "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/utils/callbacks.py"
click node_evaluate "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/scripts/evaluate.py"
click node_metrics "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/utils/metrics.py"
click node_lime "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/scripts/explain_lime.py"
click node_dependencies "https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/requirements.txt"

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_raw_download,node_train_split,node_validation_split,node_label_names,node_data_io,node_tokenizer_build,node_tokenizer_artifacts toneBlue
class node_textcnn,node_train,node_seed,node_callbacks,node_checkpoints toneAmber
class node_evaluate,node_metrics,node_lime,node_results toneMint
class node_config,node_dependencies toneNeutral
```

## Components

### Data & representation

- [Optional data download](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/scripts/download_data.sh): shell acquisition · Optionally downloads raw data from Google Drive. · `scripts/download_data.sh`
- [Training split](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/data/train_data.txt): supervised TSV · One text and numeric label per record. · `data/train_data.txt`
- [Validation split](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/data/val_data.txt): supervised TSV · Used for evaluation and optional explanation selection. · `data/val_data.txt`
- [Label display names](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/data/labels/id_to_name.json): label map · Maps zero-based class IDs to names. · `data/labels/id_to_name.json`
- [Data loading & vectorization](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/utils/data_io.py): Python module · Loads records, encodes labels, and supports whitespace or Hugging Face tokenizers. · `utils/data_io.py`
- [Build tokenizer](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/scripts/build_tokenizer.py): batch entry point · Fits and persists the shared text representation. · `scripts/build_tokenizer.py`
- **Tokenizer & statistics**: generated artifacts · Compatibility boundary for training, evaluation, and explanation.

### Training

- [TextCNN classifier](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/models/textcnn.py): Keras model · Embedding, parallel Conv1D/max-pool features, dropout, and softmax; includes ReverseTemporal. · `models/textcnn.py`
- [Train model](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/scripts/train.py): batch entry point · Runs configured Keras training and optional imbalance handling. · `scripts/train.py`
- [Reproducibility controls](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/utils/seed.py): Python module · `utils/seed.py`
- [Training callbacks](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/utils/callbacks.py): Python module · `utils/callbacks.py`
- **Model checkpoints**: run artifacts · Timestamped saved model, resolved config, and training metrics.

### Offline analysis

- [Evaluate checkpoint](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/scripts/evaluate.py): batch entry point · Scores the validation split using matching vectorization. · `scripts/evaluate.py`
- [Metrics & plots](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/utils/metrics.py): Python module · Creates reports, confusion matrices, and per-class PRF figures. · `utils/metrics.py`
- [LIME explanations](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/scripts/explain_lime.py): batch entry point · Explains raw-text predictions while delegating model vectorization. · `scripts/explain_lime.py`
- **Reports & explanations**: offline artifacts · Evaluation figures, reports, and token-attribution HTML.

### Other components

- [TextCNN configuration](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/configs/textcnn.yaml): YAML config · Controls vectorization, model, imbalance, and run settings. · `configs/textcnn.yaml`
- [Python dependencies](https://github.com/taotao917/machine-learning-design-patterns-detection/blob/main/requirements.txt): dependency manifest · TensorFlow/Keras, plotting, LIME, and optional Hugging Face support. · `requirements.txt`

## Connections

- Optional data download → Training split: prepares
- Optional data download → Validation split: prepares
- TextCNN configuration → Build tokenizer: tokenizer settings
- Training split → Build tokenizer: fit text
- Build tokenizer → Tokenizer & statistics: persists
- TextCNN configuration → Data loading & vectorization: vectorization settings
- Data loading & vectorization → Tokenizer & statistics: loads/saves representation
- TextCNN configuration → TextCNN classifier: architecture settings
- Training split → Train model: supervised records
- Data loading & vectorization → Train model: vectorized inputs
- Tokenizer & statistics → Train model: shared representation
- TextCNN classifier → Train model: builds classifier
- Reproducibility controls → Train model: sets seeds
- Training callbacks → Train model: applies callbacks
- Train model → Model checkpoints: writes model and run metadata
- Model checkpoints → Evaluate checkpoint: loads saved model
- Validation split → Evaluate checkpoint: scores
- Tokenizer & statistics → Evaluate checkpoint: reconstructs inputs
- Evaluate checkpoint → Metrics & plots: metrics inputs
- Label display names → Metrics & plots: class names
- Metrics & plots → Reports & explanations: writes reports and figures
- Model checkpoints → LIME explanations: loads model
- Tokenizer & statistics → LIME explanations: vectorizes predictions
- Validation split → LIME explanations: optional example
- LIME explanations → Reports & explanations: writes attribution HTML
- Python dependencies → Train model: runtime
- Python dependencies → Evaluate checkpoint: runtime
- Python dependencies → LIME explanations: runtime
