# wangpinggl/treqs architecture

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

- Interactive diagram: https://gitdiagram.com/wangpinggl/treqs
- Repository: https://github.com/wangpinggl/treqs

## Overview

TREQS is a PyTorch text-to-SQL system for questions about MIMIC-III electronic medical records. The principal workflow takes tokenized natural-language questions and generates SQL through a seq2seq model; the README describes training, validation, testing, and query evaluation. A separate data-preparation branch builds sampled MIMICSQL tables from MIMIC-III data. The source excerpts show model construction and parts of batch processing, but not the full decoding or evaluation path, so those links are limited to documented or directly evidenced relationships.

Main entry points: [`model.py`](https://github.com/wangpinggl/treqs/blob/master/model.py) (Seq2SQL model), [`main.py`](https://github.com/wangpinggl/treqs/blob/master/main.py) (Task entry point), [`seq2sql/model_seq2seq_base.py`](https://github.com/wangpinggl/treqs/blob/master/seq2sql/model_seq2seq_base.py) (Seq2seq engine), [`LeafNATS/data/seq2sql/process_batch_cqa_v1.py`](https://github.com/wangpinggl/treqs/blob/master/LeafNATS/data/seq2sql/process_batch_cqa_v1.py) (SQL batch processor)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Model runtime"]
  node_main["Task entry point<br/>[main.py]"]
  node_model["Seq2SQL model<br/>[model.py]"]
  node_base["Seq2seq engine"]
  node_embedding["Token embeddings<br/>[nats_embedding.py]"]
  node_encoder["RNN encoder<br/>[encoder_rnn.py]"]
  node_bridge["Encoder bridge"]
  node_attention_encoder["Encoder attention"]
  node_attention_decoder["Decoder attention"]
  node_word_copy["Word copying<br/>[word_copy.py]"]
  node_sql_result["Generated SQL"]
end

subgraph group_data["Question and data"]
  node_batch_data["SQL batch processor"]
  node_vocab["Vocabulary utilities<br/>[utils.py]"]
  node_mimicsql["MIMICSQL questions"]
end

subgraph group_medical["Medical data preparation"]
  node_mimic_source["MIMIC-III source data"]
  node_db_builder["Database preparation"]
  node_table_builder["Clinical table builders<br/>[process_tables.py]"]
  node_mimic_db[("Sampled patient database")]
end

subgraph group_evaluation["Evaluation"]
  node_evaluation_utils["Query evaluation<br/>[utils.py]"]
  node_eval_script["Generation evaluation<br/>[eval_pyrouge_v2.py]"]
end

node_user(("Question author"))
node_eval_user(("Evaluation operator"))

node_user -->|"submits task"| node_main
node_main -->|"instantiates"| node_model
node_model -->|"inherits from"| node_base
node_model -->|"processes batches"| node_batch_data
node_base -->|"builds vocabulary"| node_vocab
node_model -->|"constructs and uses"| node_embedding
node_model -->|"constructs and calls"| node_encoder
node_model -->|"calls"| node_bridge
node_model -->|"constructs"| node_attention_encoder
node_model -->|"constructs"| node_attention_decoder
node_base -->|"uses for decoding"| node_word_copy
node_mimicsql -.->|"provides tokenized questions"| node_batch_data
node_batch_data -.->|"provides tensors"| node_model
node_model -.->|"generates"| node_sql_result
node_mimic_source -->|"supplies records"| node_table_builder
node_db_builder -->|"calls builders"| node_table_builder
node_db_builder -->|"writes sampled tables"| node_mimic_db
node_eval_user -.->|"runs query evaluation"| node_evaluation_utils
node_main -->|"dispatches evaluate"| node_eval_script
node_sql_result -.->|"evaluated against"| node_evaluation_utils
node_mimic_db -.->|"provides database"| node_evaluation_utils

click node_main "https://github.com/wangpinggl/treqs/blob/master/main.py"
click node_model "https://github.com/wangpinggl/treqs/blob/master/model.py"
click node_base "https://github.com/wangpinggl/treqs/blob/master/seq2sql/model_seq2seq_base.py"
click node_embedding "https://github.com/wangpinggl/treqs/blob/master/LeafNATS/modules/embedding/nats_embedding.py"
click node_encoder "https://github.com/wangpinggl/treqs/blob/master/LeafNATS/modules/encoder/encoder_rnn.py"
click node_bridge "https://github.com/wangpinggl/treqs/blob/master/LeafNATS/modules/encoder2decoder/nats_encoder2decoder.py"
click node_attention_encoder "https://github.com/wangpinggl/treqs/blob/master/LeafNATS/modules/attention/nats_attention_encoder.py"
click node_attention_decoder "https://github.com/wangpinggl/treqs/blob/master/LeafNATS/modules/attention/nats_attention_decoder.py"
click node_word_copy "https://github.com/wangpinggl/treqs/blob/master/LeafNATS/modules/decoding/word_copy.py"
click node_batch_data "https://github.com/wangpinggl/treqs/blob/master/LeafNATS/data/seq2sql/process_batch_cqa_v1.py"
click node_vocab "https://github.com/wangpinggl/treqs/blob/master/LeafNATS/data/utils.py"
click node_db_builder "https://github.com/wangpinggl/treqs/blob/master/evaluation/process_mimic_db.py"
click node_table_builder "https://github.com/wangpinggl/treqs/blob/master/evaluation/process_mimic_db/process_tables.py"
click node_evaluation_utils "https://github.com/wangpinggl/treqs/blob/master/evaluation/utils.py"
click node_eval_script "https://github.com/wangpinggl/treqs/blob/master/LeafNATS/eval_scripts/eval_pyrouge_v2.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_main,node_model,node_base,node_embedding,node_encoder,node_bridge,node_attention_encoder,node_attention_decoder,node_word_copy,node_sql_result toneBlue
class node_batch_data,node_vocab,node_mimicsql toneAmber
class node_mimic_source,node_db_builder,node_table_builder,node_mimic_db toneMint
class node_evaluation_utils,node_eval_script toneRose
class node_user,node_eval_user toneIndigo
```

## Components

### Model runtime

- [Task entry point](https://github.com/wangpinggl/treqs/blob/master/main.py): `main.py`
- [Seq2SQL model](https://github.com/wangpinggl/treqs/blob/master/model.py): `model.py`
- [Seq2seq engine](https://github.com/wangpinggl/treqs/blob/master/seq2sql/model_seq2seq_base.py): `seq2sql/model_seq2seq_base.py`
- [Token embeddings](https://github.com/wangpinggl/treqs/blob/master/LeafNATS/modules/embedding/nats_embedding.py): `LeafNATS/modules/embedding/nats_embedding.py`
- [RNN encoder](https://github.com/wangpinggl/treqs/blob/master/LeafNATS/modules/encoder/encoder_rnn.py): `LeafNATS/modules/encoder/encoder_rnn.py`
- [Encoder bridge](https://github.com/wangpinggl/treqs/blob/master/LeafNATS/modules/encoder2decoder/nats_encoder2decoder.py): `LeafNATS/modules/encoder2decoder/nats_encoder2decoder.py`
- [Encoder attention](https://github.com/wangpinggl/treqs/blob/master/LeafNATS/modules/attention/nats_attention_encoder.py): `LeafNATS/modules/attention/nats_attention_encoder.py`
- [Decoder attention](https://github.com/wangpinggl/treqs/blob/master/LeafNATS/modules/attention/nats_attention_decoder.py): `LeafNATS/modules/attention/nats_attention_decoder.py`
- [Word copying](https://github.com/wangpinggl/treqs/blob/master/LeafNATS/modules/decoding/word_copy.py): `LeafNATS/modules/decoding/word_copy.py`
- **Generated SQL**

### Question and data

- [SQL batch processor](https://github.com/wangpinggl/treqs/blob/master/LeafNATS/data/seq2sql/process_batch_cqa_v1.py): `LeafNATS/data/seq2sql/process_batch_cqa_v1.py`
- [Vocabulary utilities](https://github.com/wangpinggl/treqs/blob/master/LeafNATS/data/utils.py): `LeafNATS/data/utils.py`
- **MIMICSQL questions**

### Medical data preparation

- **MIMIC-III source data**
- [Database preparation](https://github.com/wangpinggl/treqs/blob/master/evaluation/process_mimic_db.py): `evaluation/process_mimic_db.py`
- [Clinical table builders](https://github.com/wangpinggl/treqs/blob/master/evaluation/process_mimic_db/process_tables.py): `evaluation/process_mimic_db/process_tables.py`
- **Sampled patient database**

### Evaluation

- [Query evaluation](https://github.com/wangpinggl/treqs/blob/master/evaluation/utils.py): `evaluation/utils.py`
- [Generation evaluation](https://github.com/wangpinggl/treqs/blob/master/LeafNATS/eval_scripts/eval_pyrouge_v2.py): `LeafNATS/eval_scripts/eval_pyrouge_v2.py`

### Other components

- **Question author**
- **Evaluation operator**

## Connections

- Question author → Task entry point: submits task (evidence: `main.py`)
- Task entry point → Seq2SQL model: instantiates (evidence: `main.py`)
- Seq2SQL model → Seq2seq engine: inherits from (evidence: `model.py`)
- Seq2SQL model → SQL batch processor: processes batches (evidence: `model.py`)
- Seq2seq engine → Vocabulary utilities: builds vocabulary (evidence: `seq2sql/model_seq2seq_base.py`)
- Seq2SQL model → Token embeddings: constructs and uses (evidence: `model.py`)
- Seq2SQL model → RNN encoder: constructs and calls (evidence: `model.py`)
- Seq2SQL model → Encoder bridge: calls (evidence: `model.py`)
- Seq2SQL model → Encoder attention: constructs (evidence: `model.py`)
- Seq2SQL model → Decoder attention: constructs (evidence: `model.py`)
- Seq2seq engine → Word copying: uses for decoding (evidence: `seq2sql/model_seq2seq_base.py`)
- MIMICSQL questions → SQL batch processor: provides tokenized questions
- SQL batch processor → Seq2SQL model: provides tensors (evidence: `model.py`)
- Seq2SQL model → Generated SQL: generates
- MIMIC-III source data → Clinical table builders: supplies records (evidence: `evaluation/process_mimic_db/process_tables.py`)
- Database preparation → Clinical table builders: calls builders (evidence: `evaluation/process_mimic_db.py`)
- Database preparation → Sampled patient database: writes sampled tables (evidence: `evaluation/process_mimic_db.py`)
- Evaluation operator → Query evaluation: runs query evaluation
- Task entry point → Generation evaluation: dispatches evaluate (evidence: `main.py`)
- Generated SQL → Query evaluation: evaluated against
- Sampled patient database → Query evaluation: provides database
