# dmcguire81/explainable-automated-medical-coding architecture

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

- Interactive diagram: https://gitdiagram.com/dmcguire81/explainable-automated-medical-coding
- Repository: https://github.com/dmcguire81/explainable-automated-medical-coding

## Overview

1. This repository centers on an explainable multi-label text classification pipeline for medical coding. The primary production path is the HLAN family: Hierarchical Label-wise Attention Network, with variants for HLAN, HA-GRU, and HAN. The rest of the repo supports training, evaluation, inference visualization, and artifact storage.

2. The main runtime is a TensorFlow 1.x training/inference stack under `HLAN/`. `HAN_train.py` is the entry point for experiments and configuration, `HAN_model_dynamic.py` contains the model graph and per-label attention computation, and `data_util_gensim.py` prepares text and label inputs. `model_predict_util.py` powers prediction-time postprocessing for the notebook demo.

3. The model boundary is important: the network consumes tokenized clinical notes and emits per-label scores plus attention weights that are later rendered as explanations. The README calls out `inference_per_label()` in `HAN_model_dynamic.py` as the key computation graph for the label-wise attention mechanism.

4. Data enters the system from dataset files under `datasets/`, which are expected to be preformatted as note text plus a multi-label target list. The training code supports multiple dataset presets, including MIMIC-III, MIMIC-III-50, and a shielding-specific variant, with dataset-specific paths and hyperparameters selected in `HAN_train.py`.

5. Preprocessing and vocabulary/label caching are separated from model code. The `cache_vocabulary_label_pik/` directory stores serialized vocabulary and label objects used to keep training and demo runs consistent, while `datasets/README.md` and related notebooks document the file layout and splits.

6. Label semantics are strengthened through the `knowledge_bases/` folder. It contains ICD description files and an ICD-9 hierarchy graph, which are used for label subsumption relations and hierarchical evaluation support rather than as a live service dependency.

7. A major architectural feature is label embedding initialization. The `embeddings/` folder holds or generates Gensim word2vec embeddings for both words and labels, and `HAN_train.py` loads these embeddings to initialize the final classifier layer so labels start from semantic vectors instead of random weights.

8. Training produces model checkpoints and logs under `checkpoints/`, with the README indicating pretrained HLAN, HA-GRU, and HAN artifacts are stored there. The code also supports repeated runs, early stopping, GPU execution, and TensorBoard logging, so the training subsystem is both experiment-oriented and reproducible.

9. Inference for humans is exposed through `demo_HLAN_viz.ipynb`. That notebook loads a trained checkpoint, runs prediction utilities, and writes attention visualizations to `explanations/` as Excel sheets. This is the main explainability boundary: prediction output is transformed into token-level or sentence-level attention displays for inspection.

10. Evaluation is split into standard metrics and hierarchical metrics. `multi_level_eval.py` and `evaluation_setup.py` incorporate CoPHE-based hierarchical evaluation, while the rest of the training flow still computes conventional multi-label classification metrics. This means the repo has both flat and ontology-aware evaluation paths.

11. The repository also contains a results archive for the HealTAC 2020 experiments. `results-HealTAC 2020/` stores CSV/XLSX outputs for CNN, CNN+attention, Bi-GRU, and BERT baselines, which are separate from the TensorFlow HLAN training path but useful as comparison artifacts.

12. External dependencies materially shape the architecture: Python 3, TensorFlow 1.x, Gensim, NLTK, spaCy 2.x, scikit-learn, and TFLearn are part of the HLAN stack, while the baseline result folders reflect older PyTorch/CAML and BERT-related tooling. The repo is therefore a research codebase with one main HLAN pipeline plus side-by-side baseline outputs and supporting assets.

Main entry points: [`HLAN/HAN_train.py`](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/HAN_train.py) (Train), [`results-HealTAC 2020/readme.md`](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/HEAD/results-HealTAC%202020/readme.md) (HealTAC), [`HLAN/demo_HLAN_viz.ipynb`](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/demo_HLAN_viz.ipynb) (Demo notebook), [`HLAN/data_util_gensim.py`](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/data_util_gensim.py) (Data prep)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_hlan_runtime["HLAN runtime"]
  node_train["Train<br/>experiment entry<br/>[HAN_train.py]"]
  node_model["Attention model<br/>TF graph"]
  node_data_util["Data prep<br/>preprocessing"]
  node_predict_util["Predict utils<br/>inference helper"]
  node_eval_setup["Eval setup<br/>evaluation config"]
  node_hier_eval["Hier eval<br/>ontology metrics"]
  node_demo["Demo notebook<br/>viz notebook"]
end

subgraph group_support_assets["Support assets"]
  node_root_readme["README<br/>project docs<br/>[README.md]"]
  node_datasets_readme["Datasets<br/>dataset docs<br/>[README.md]"]
  node_cache_readme["Cache docs<br/>[README.md]"]
  node_embed_word["Word emb<br/>embedding script"]
  node_embed_code["Code emb<br/>embedding script"]
  node_kb_graph[("ICD graph<br/>knowledge base")]
  node_kb_sub[("ICD subsum<br/>knowledge base<br/>[kb-icd-sub.csv]")]
  node_checkpoints_readme["Checkpoints<br/>artifact docs<br/>[README.md]"]
  node_explanations_readme["Explanations<br/>artifact docs<br/>[README.md]"]
end

subgraph group_archives["Results archive"]
  node_heal_readme["HealTAC<br/>results archive<br/>[readme.md]"]
  node_cnn_readme["CNN results<br/>baseline archive<br/>[README.md]"]
  node_cnnatt_readme["CNN+att<br/>baseline archive<br/>[README.md]"]
  node_bigru_readme["Bi-GRU<br/>baseline archive<br/>[README.md]"]
  node_bert_readme["BERT results<br/>baseline archive<br/>[README.md]"]
end

node_root_readme -->|"describes"| node_train
node_root_readme -->|"documents"| node_demo
node_train -->|"loads"| node_data_util
node_train -->|"builds"| node_model
node_train -->|"uses"| node_embed_word
node_train -->|"uses"| node_embed_code
node_train -->|"uses"| node_kb_graph
node_train -->|"uses"| node_kb_sub
node_train -->|"writes"| node_checkpoints_readme
node_model -->|"emits"| node_hier_eval
node_data_util -->|"reads"| node_datasets_readme
node_data_util -->|"reads"| node_cache_readme
node_demo -->|"uses"| node_predict_util
node_demo -->|"loads"| node_model
node_demo -->|"writes"| node_explanations_readme
node_eval_setup -->|"configures"| node_hier_eval
node_eval_setup -->|"uses"| node_kb_graph
node_heal_readme -->|"indexes"| node_cnn_readme
node_heal_readme -->|"indexes"| node_cnnatt_readme
node_heal_readme -->|"indexes"| node_bigru_readme
node_heal_readme -->|"indexes"| node_bert_readme

click node_root_readme "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/README.md"
click node_train "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/HAN_train.py"
click node_model "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/HAN_model_dynamic.py"
click node_data_util "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/data_util_gensim.py"
click node_predict_util "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/model_predict_util.py"
click node_eval_setup "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/evaluation_setup.py"
click node_hier_eval "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/multi_level_eval.py"
click node_demo "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/demo_HLAN_viz.ipynb"
click node_datasets_readme "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/datasets/README.md"
click node_cache_readme "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/cache_vocabulary_label_pik/README.md"
click node_embed_word "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/embeddings/train_word_embedding.py"
click node_embed_code "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/embeddings/train_code_embedding.py"
click node_kb_graph "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/knowledge_bases/icd9_graph_desc.json"
click node_kb_sub "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/knowledge_bases/kb-icd-sub.csv"
click node_checkpoints_readme "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/checkpoints/README.md"
click node_explanations_readme "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/explanations/README.md"
click node_heal_readme "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/results-HealTAC 2020/readme.md"
click node_cnn_readme "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/results-HealTAC 2020/CNN results/README.md"
click node_cnnatt_readme "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/results-HealTAC 2020/CNN+att results/README.md"
click node_bigru_readme "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/results-HealTAC 2020/Bi-GRU results/README.md"
click node_bert_readme "https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/results-HealTAC 2020/BERT results/README.md"

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_train,node_model,node_data_util,node_predict_util,node_eval_setup,node_hier_eval,node_demo toneBlue
class node_root_readme,node_datasets_readme,node_cache_readme,node_embed_word,node_embed_code,node_kb_graph,node_kb_sub,node_checkpoints_readme,node_explanations_readme toneAmber
class node_heal_readme,node_cnn_readme,node_cnnatt_readme,node_bigru_readme,node_bert_readme toneMint
```

## Components

### HLAN runtime

TensorFlow 1.x training, inference, attention explanation, and evaluation for medical coding.

- [Train](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/HAN_train.py): experiment entry · Main experiment runner and configuration for HLAN, HA-GRU, and HAN. · `HLAN/HAN_train.py`
- [Attention model](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/HAN_model_dynamic.py): TF graph · Model definition and label-wise attention computation used for prediction and explanations. · `HLAN/HAN_model_dynamic.py`
- [Data prep](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/data_util_gensim.py): preprocessing · Tokenization, input assembly, and label/text loading for training and evaluation. · `HLAN/data_util_gensim.py`
- [Predict utils](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/model_predict_util.py): inference helper · Prediction-time postprocessing used by the demo notebook. · `HLAN/model_predict_util.py`
- [Eval setup](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/evaluation_setup.py): evaluation config · Evaluation wiring, including hierarchical and standard metric setup. · `HLAN/evaluation_setup.py`
- [Hier eval](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/multi_level_eval.py): ontology metrics · Multi-level hierarchical evaluation against the ICD knowledge structure. · `HLAN/multi_level_eval.py`
- [Demo notebook](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/HLAN/demo_HLAN_viz.ipynb): viz notebook · Human-facing notebook that loads checkpoints and renders attention explanations. · `HLAN/demo_HLAN_viz.ipynb`

### Support assets

Cached vocabularies, datasets, embeddings, label knowledge, and output folders used by the pipeline.

- [README](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/README.md): project docs · Top-level overview of the explainable medical coding pipeline. · `README.md`
- [Datasets](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/datasets/README.md): dataset docs · Dataset layout, splits, and preprocessing expectations. · `datasets/README.md`
- [Cache docs](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/cache_vocabulary_label_pik/README.md): cache docs · Serialized vocabulary and label cache used to keep runs consistent. · `cache_vocabulary_label_pik/README.md`
- [Word emb](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/embeddings/train_word_embedding.py): embedding script · Builds or trains word embeddings used to initialize token representations. · `embeddings/train_word_embedding.py`
- [Code emb](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/embeddings/train_code_embedding.py): embedding script · Builds or trains label/code embeddings used to seed the classifier layer. · `embeddings/train_code_embedding.py`
- [ICD graph](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/knowledge_bases/icd9_graph_desc.json): knowledge base · ICD-9 hierarchy and description graph used for label relations. · `knowledge_bases/icd9_graph_desc.json`
- [ICD subsum](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/knowledge_bases/kb-icd-sub.csv): knowledge base · ICD subsumption table for hierarchical label support. · `knowledge_bases/kb-icd-sub.csv`
- [Checkpoints](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/checkpoints/README.md): artifact docs · Stored trained HLAN-family checkpoints and experiment artifacts. · `checkpoints/README.md`
- [Explanations](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/master/explanations/README.md): artifact docs · Attention visualization outputs exported for inspection. · `explanations/README.md`

### Results archive

Stored HealTAC 2020 baseline outputs and comparison artifacts.

- [HealTAC](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/HEAD/results-HealTAC%202020/readme.md): results archive · Archived comparison results from the HealTAC 2020 experiments. · `results-HealTAC 2020/readme.md`
- [CNN results](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/HEAD/results-HealTAC%202020/CNN%20results/README.md): baseline archive · CNN baseline outputs and metrics for full and 50-label settings. · `results-HealTAC 2020/CNN results/README.md`
- [CNN+att](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/HEAD/results-HealTAC%202020/CNN%2Batt%20results/README.md): baseline archive · Attention CNN baseline outputs and metrics. · `results-HealTAC 2020/CNN+att results/README.md`
- [Bi-GRU](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/HEAD/results-HealTAC%202020/Bi-GRU%20results/README.md): baseline archive · Bi-GRU baseline outputs and metrics. · `results-HealTAC 2020/Bi-GRU results/README.md`
- [BERT results](https://github.com/dmcguire81/explainable-automated-medical-coding/blob/HEAD/results-HealTAC%202020/BERT%20results/README.md): baseline archive · BERT baseline outputs and metrics. · `results-HealTAC 2020/BERT results/README.md`

## Connections

- README → Train: describes · Repository overview points to the main experiment pipeline.
- README → Demo notebook: documents · README also frames the explainability demo.
- Train → Data prep: loads · Training depends on preprocessing and input assembly.
- Train → Attention model: builds · Training instantiates the attention network graph.
- Train → Word emb: uses · Word embeddings initialize or seed text representations.
- Train → Code emb: uses · Label/code embeddings initialize the classifier side.
- Train → ICD graph: uses · Knowledge graph informs hierarchical label handling.
- Train → ICD subsum: uses · Subsumption data supports ontology-aware training/eval.
- Train → Checkpoints: writes · Runs produce checkpoints and logs under the artifact folder.
- Attention model → Hier eval: emits · Label-wise scores and attention weights feed evaluation and explanation.
- Data prep → Datasets: reads · Dataset files and formatting conventions originate here.
- Data prep → Cache docs: reads · Cached vocabulary/label objects keep runs consistent.
- Demo notebook → Predict utils: uses · Notebook prediction flow relies on postprocessing helpers.
- Demo notebook → Attention model: loads · Demo loads a trained model checkpoint for inference.
- Demo notebook → Explanations: writes · Attention visualizations are exported for human review.
- Eval setup → Hier eval: configures · Evaluation setup routes into hierarchical metrics.
- Eval setup → ICD graph: uses · Ontology structure supports hierarchical scoring.
- HealTAC → CNN results: indexes · Archive includes baseline result folders for comparison.
- HealTAC → CNN+att: indexes · Archive includes baseline result folders for comparison.
- HealTAC → Bi-GRU: indexes · Archive includes baseline result folders for comparison.
- HealTAC → BERT results: indexes · Archive includes baseline result folders for comparison.
