Automated Medical Coding
Contribute to ahmedjemaa-tech/medical-coding-reproducibility development by creating an account on GitHub.
ahmedjemaa-tech
README
⚕️Automated Medical Coding on MIMIC-III and MIMIC-IV: A Critical Review and Replicability Study
Official source code repository for the SIGIR 2023 paper Automated Medical Coding on MIMIC-III and MIMIC-IV: A Critical Review and Replicability Study
@inproceedings{edinAutomatedMedicalCoding2023,
address = {Taipei, Taiwan},
title = {Automated {Medical} {Coding} on {MIMIC}-{III} and {MIMIC}-{IV}: {A} {Critical} {Review} and {Replicability} {Study}},
isbn = {978-1-4503-9408-6},
shorttitle = {Automated {Medical} {Coding} on {MIMIC}-{III} and {MIMIC}-{IV}},
doi = {10.1145/3539618.3591918},
booktitle = {Proceedings of the 46th {International} {ACM} {SIGIR} {Conference} on {Research} and {Development} in {Information} {Retrieval}},
publisher = {ACM Press},
author = {Edin, Joakim and Junge, Alexander and Havtorn, Jakob D. and Borgholt, Lasse and Maistro, Maria and Ruotsalo, Tuukka and Maaløe, Lars},
year = {2023}
}
Update
We released a new paper and repository for explainable medical coding. The new repository offers the following:
- Explainability: Multiple feature attribution methods and metrics for multi-label classification.
- Implementation of a modified PLM-ICD: We have fixed the problem of PLM-ICD occasionally collapsing during training.
- Huggingface Datasets: we implemented MIMIC-III, IV, and MDACE as HuggingFace datasets.
- Inference code: We provide code for inference without needing the training dataset. The new repository no longer supports CNN, Bi-GRU, CAML, LAAT, and MultiResCNN.
Also, check out my blog post criticizing popular ideas in automated medical coding. I think it will be interesting for most researchers in the field
Introduction
Automatic medical coding is the task of automatically assigning diagnosis and procedure codes based on discharge summaries from electronic health records. This repository contains the code used in the paper Automated medical coding on MIMIC-III and MIMIC-IV: A Critical Review and Replicability Study. The repository contains code for training and evaluating medical coding models and new splits for MIMIC-III and the newly released MIMIC-IV. The following models have been implemented:
The splits are found in files/data
. The splits are described in the paper.
How to reproduce results
Setup Conda environment
- Create a conda environement
conda create -n coding python=3.10
- Install the packages
pip install . -e
Prepare MIMIC-III
This code has been developed on MIMIC-III v1.4.
- Download the MIMIC-III data into your preferred location
path/to/mimiciii
. Please note that you need to complete training to acces the data. The training is free, but takes a couple of hours. - link to data access - Open the file
src/settings.py
- Change the variable
DOWNLOAD_DIRECTORY_MIMICIII
to the path of your downloaded datapath/to/mimiciii
- If you want to use the MIMIC-III full and MIMIC-III 50 from the Explainable Prediction of Medical Codes from Clinical Text you need to run
python prepare_data/prepare_mimiciii_mullenbach.py
- If you want to use MIMIC-III clean from our paper you need to run
python prepare_data/prepare_mimiciii.py
Prepare MIMIC-IV
This code has been developed on MIMIC-IV and MIMIC-IV v2.2.
- Download MIMIC-IV and MIMIC-IV-NOTE into your preferred location
path/to/mimiciv
andpath/to/mimiciv-note
. Please note that you need to complete training to acces the data. The training is free, but takes a couple of hours. - mimiciv and mimiciv-note - Open the file
src/settings.py
- Change the variable
DOWNLOAD_DIRECTORY_MIMICIV
to the path of your downloaded datapath/to/mimiciv
- Change the variable
DOWNLOAD_DIRECTORY_MIMICIV_NOTE
to the path of your downloaded datapath/to/mimiciv-note
- Run
python prepare_data/prepare_mimiciv.py
Before running experiments
- Create a weights and biases account. It is possible to run the experiments without wandb.
- Download the model checkpoints and unzip it. Please note that these model weights can't be used commercially due to the MIMIC License.
- If you want to train PLM-ICD, you need to download RoBERTa-base-PM-M3-Voc, unzip it and change the
model_path
parameter inconfigs/model/plm_icd.yaml
andconfigs/text_transform /huggingface.yaml
to the path of the download.
Running experiments
Training
You can run any experiment found in configs/experiment
. Here are some examples:
- Train PLM-ICD on MIMIC-III clean on GPU 0:
python main.py experiment=mimiciii_clean/plm_icd gpu=0
- Train CAML on MIMIC-III full on GPU 6:
python main.py experiment=mimiciii_full/caml gpu=6
- Train LAAT on MIMIC-IV ICD-9 full on GPU 6:
python main.py experiment=mimiciv_icd9/laat gpu=6
- Train LAAT on MIMIC-IV ICD-9 full on GPU 6 without weights and biases:
python main.py experiment=mimiciv_icd9/laat gpu=6 callbacks=no_wandb trainer.print_metrics=true
Evaluation
If you just want to evaluate the models using the provided model_checkpoints you need to do set trainer.epochs=0
and provide the path to the models checkpoint load_model=path/to/model_checkpoint
. Make sure you the correct model-checkpoint with the correct configs.
Example:
Evaluate PLM-ICD on MIMIC-IV ICD-10 on GPU 1: python main.py experiment=mimiciv_icd10/plm_icd gpu=1 load_model=path/to/model_checkpoints/mimiciv_icd10/plm_icd trainer.epochs=0
Overview of the repository
configs
We use Hydra for configurations. The condigs for every experiment is found in configs/experiments
. Furthermore, the configuration for the sweeps are found in configs/sweeps
. We used Weights and Biases Sweeps for most of our experiments.
files
This is where the images and data is stored.
notebooks
The directory only contains one notebook used for the code analysis. The notebook is not aimed to be used by others, but is included for others to validate our data analysis.
prepare_data
The directory contains all the code for preparing the datasets and generating splits.
reports
This is the code used to generate the plots and tables used in the paper. The code uses the Weights and Biases API to fetch the experiment results. The code is not usable by others, but was included for the possibility to validate our figures and tables.
src
This is were the code for running the experiments is found.
tests
The directory contains the unit tests
My setup
I ran the experiments on one RTX 2080 Ti 11GB per experiment. I had 128 GB RAM on my machine.
⚠️ Known issues
- LAAT and PLM-ICD are unstable. The loss will sometimes diverge during training. The issue seems to be overflow in the softmax function in the label-wise attention. Using batch norm or layer norm before the softmax function might solve the issue. We did not try to fix the issue as we didn't want to change the original method during our reproducibility.
- The code was only tested on a server with 128 GB RAM. A user with 32 GB RAM reported issues fitting MIMIC-IV into memory.
- There is an error in the collate function in the Huggingface dataset. The attention mask is being padded with 1s instead of 0s. I have not fixed this issue because I want people to be able to reproduce the results from the paper.
Acknowledgement
Thank you Sotiris Lamprinidis for providing an efficient implementation of our multi-label stratification algorithm and some data preprocessing helper functions.
Recommended Servers
AIO-MCP Server
🚀 All-in-one MCP server with AI search, RAG, and multi-service integrations (GitLab/Jira/Confluence/YouTube) for AI-enhanced development workflows. Folk from
Persistent Knowledge Graph
An implementation of persistent memory for Claude using a local knowledge graph, allowing the AI to remember information about users across conversations with customizable storage location.
React MCP
react-mcp integrates with Claude Desktop, enabling the creation and modification of React apps based on user prompts

Any OpenAI Compatible API Integrations
Integrate Claude with Any OpenAI SDK Compatible Chat Completion API - OpenAI, Perplexity, Groq, xAI, PyroPrompts and more.
Exa MCP
A Model Context Protocol server that enables AI assistants like Claude to perform real-time web searches using the Exa AI Search API in a safe and controlled manner.
AI 图像生成服务
可用于cursor 集成 mcp server
Web Research Server
A Model Context Protocol server that enables Claude to perform web research by integrating Google search, extracting webpage content, and capturing screenshots.
MySQL Server
Allows AI assistants to list tables, read data, and execute SQL queries through a controlled interface, making database exploration and analysis safer and more structured.
Browser Use (used by Deploya.dev)
AI-driven browser automation server that implements the Model Context Protocol to enable natural language control of web browsers for tasks like navigation, form filling, and visual interaction.
Aindreyway Codex Keeper
Serves as a guardian of development knowledge, providing AI assistants with curated access to latest documentation and best practices.