Skip to content

Commit 8bcbd19

Browse files
Docker mimic iv postgres (MIT-LCP#1757)
* added scripts for dockerizing mimic-iv with postgres * update * made db name consistent with concepts scripts * rework docker and add duckdb docker build * update to use build_mimic.sh convenience script * add CI to test docker builds of psql/duckdb * name both docker compose jobs to avoid conflicts in CI * use bind mount to infer user ID for file ownership --------- Co-authored-by: Alistair Johnson <alistairewj@gmail.com>
1 parent e1d477f commit 8bcbd19

24 files changed

Lines changed: 1084 additions & 162 deletions

‎.github/actions/load-mimic-duckdb/action.yml‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "Load MIMIC-IV demo into DuckDB"
2-
description: "Builds a DuckDB database from the demo hosp/icu data using import_duckdb.sh. Requires the duckdb CLI on PATH."
2+
description: "Builds a DuckDB database from the demo hosp/icu data using build_mimic.sh. Requires the duckdb CLI on PATH."
33

44
inputs:
55
mimic_data_dir:
@@ -18,11 +18,17 @@ runs:
1818
shell: bash
1919
# Resolve paths at shell time so $GITHUB_WORKSPACE points at the
2020
# container-mounted path.
21-
# `echo n` answers the overwrite prompt defensively.
21+
#
22+
# Load only. The concepts are transpiled and built by concepts-duckdb.yml
23+
# after this action runs, and the row counts are checked by the calling
24+
# workflow, so build_mimic.sh must not do either here.
25+
env:
26+
MIMIC_MAKE_CONCEPTS: "false"
27+
MIMIC_VALIDATE: "false"
2228
run: |
2329
data_dir="${{ inputs.mimic_data_dir }}"
2430
data_dir="${data_dir:-$GITHUB_WORKSPACE}"
2531
db_file="${{ inputs.db_file }}"
2632
db_file="${db_file:-$GITHUB_WORKSPACE/mimic4.db}"
2733
cd "$GITHUB_WORKSPACE/mimic-iv/buildmimic/duckdb"
28-
echo n | ./import_duckdb.sh "$data_dir" "$db_file"
34+
./build_mimic.sh "$data_dir" "$db_file"

‎.github/actions/load-mimic-psql/action.yml‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "Load MIMIC-IV demo into PostgreSQL"
2-
description: "Creates the MIMIC-IV schemas and loads the demo hosp/icu data. Requires psql on PATH and PG* connection env vars."
2+
description: "Builds the MIMIC-IV schemas, data, constraints and indexes from the demo hosp/icu data using build_mimic.sh. Requires psql on PATH and PG* connection env vars."
33

44
inputs:
55
mimic_data_dir:
@@ -15,9 +15,11 @@ runs:
1515
# Resolve the data dir at shell time so $GITHUB_WORKSPACE points at the
1616
# container-mounted path. The github.workspace context yields the host
1717
# path, which does not exist inside a container job.
18+
env:
19+
# Load only - we validate/build concepts elsewhere.
20+
MIMIC_MAKE_CONCEPTS: "false"
21+
MIMIC_VALIDATE: "false"
1822
run: |
1923
data_dir="${{ inputs.mimic_data_dir }}"
2024
data_dir="${data_dir:-$GITHUB_WORKSPACE}"
21-
psql -q -v ON_ERROR_STOP=1 -f "$GITHUB_WORKSPACE/mimic-iv/buildmimic/postgres/create.sql"
22-
psql -q -v ON_ERROR_STOP=1 -v mimic_data_dir="$data_dir" \
23-
-f "$GITHUB_WORKSPACE/mimic-iv/buildmimic/postgres/load_gz.sql"
25+
"$GITHUB_WORKSPACE/mimic-iv/buildmimic/postgres/build_mimic.sh" "$data_dir"

‎.github/actions/setup-duckdb/action.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ inputs:
55
version:
66
description: "DuckDB release version to install."
77
required: false
8-
default: "1.1.3"
8+
# Keep in step with DUCKDB_VERSION in mimic-iv/buildmimic/duckdb/docker.
9+
default: "1.4.5"
910

1011
runs:
1112
using: "composite"

‎.github/workflows/docker-build.yml‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Build and run the containerized MIMIC-IV builds against the demo dataset.
2+
name: docker build
3+
4+
on:
5+
pull_request:
6+
paths:
7+
- 'mimic-iv/buildmimic/postgres/**'
8+
- 'mimic-iv/buildmimic/duckdb/**'
9+
- 'mimic-iv/buildmimic/download_data.sh'
10+
- '.github/workflows/docker-build.yml'
11+
push:
12+
branches:
13+
- main
14+
paths:
15+
- 'mimic-iv/buildmimic/postgres/**'
16+
- 'mimic-iv/buildmimic/duckdb/**'
17+
- '.github/workflows/docker-build.yml'
18+
19+
# Cancel superseded runs when a PR is pushed again.
20+
concurrency:
21+
group: docker-build-${{ github.event.pull_request.number || github.ref }}
22+
cancel-in-progress: true
23+
24+
permissions:
25+
contents: read
26+
27+
jobs:
28+
build:
29+
runs-on: ubuntu-latest
30+
31+
defaults:
32+
run:
33+
# Name bash explicitly to get `-eo pipefail`.
34+
shell: bash
35+
36+
env:
37+
DUCKDB_DOCKER: mimic-iv/buildmimic/duckdb/docker
38+
POSTGRES_DOCKER: mimic-iv/buildmimic/postgres/docker
39+
MIMIC_DATA_DIR: ${{ github.workspace }}
40+
MIMIC_OUTPUT_DIR: ${{ github.workspace }}/duckdb-out
41+
42+
steps:
43+
- uses: actions/checkout@v7
44+
- uses: ./.github/actions/download-demo
45+
- uses: ./.github/actions/setup-duckdb
46+
47+
- name: Build both images
48+
run: |
49+
docker compose -f "$DUCKDB_DOCKER/docker-compose.yml" build
50+
docker compose -f "$POSTGRES_DOCKER/docker-compose.yml" build
51+
52+
# --exit-code-from implies --abort-on-container-exit and returns the build
53+
# job's own status, so a failed build fails the step.
54+
- name: Build MIMIC-IV in DuckDB
55+
run: |
56+
mkdir -p "$MIMIC_OUTPUT_DIR"
57+
docker compose -f "$DUCKDB_DOCKER/docker-compose.yml" \
58+
up --exit-code-from mimic-build | tee duckdb.log
59+
if grep -F -q "FAILED" duckdb.log; then
60+
echo "::error::DuckDB container row-count validation failed:"
61+
grep -F "FAILED" duckdb.log
62+
exit 1
63+
fi
64+
65+
- name: Read the built database with the runner's own duckdb
66+
run: |
67+
rows=$(duckdb "$MIMIC_OUTPUT_DIR/mimic4.db" -noheader -list \
68+
-c "SELECT count(*) FROM mimiciv_derived.sepsis3")
69+
echo "mimiciv_derived.sepsis3: ${rows} rows"
70+
if [ -z "${rows}" ] || [ "${rows}" -eq 0 ]; then
71+
echo "::error::The concepts are missing from the container-built database."
72+
exit 1
73+
fi
74+
75+
- name: Build MIMIC-IV in PostgreSQL
76+
run: |
77+
docker compose -f "$POSTGRES_DOCKER/docker-compose.yml" \
78+
up --exit-code-from mimic-build | tee psql.log
79+
if grep -F -q "FAILED" psql.log; then
80+
echo "::error::PostgreSQL container row-count validation failed:"
81+
grep -F "FAILED" psql.log
82+
exit 1
83+
fi
84+
85+
- name: Tear down
86+
if: always()
87+
run: |
88+
docker compose -f "$DUCKDB_DOCKER/docker-compose.yml" down -v || true
89+
docker compose -f "$POSTGRES_DOCKER/docker-compose.yml" down -v || true

‎.gitignore‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
## Local docker-postgres raw data (do not commit large CSVs)
2-
mimic-iv/docker-postgres/mimic-data/
1+
## Raw data downloaded for local builds (do not commit large CSVs)
2+
mimic-data/
33

44
## Allow example env, ignore real env
5-
mimic-iv/docker-postgres/.env
5+
.env
6+
!.env.example
67

78
# duckdb / sqlite db files
89
*.db
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"name": "subject_id",
4+
"type": "INT64",
5+
"mode": "REQUIRED"
6+
}
7+
]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
# Download MIMIC-IV from PhysioNet into a directory any of the builds can use.
3+
#
4+
# Usage: ./download_data.sh [destination] [physionet-username]
5+
#
6+
# destination where to put the data (default ./mimic-data)
7+
# physionet-username also read from $PHYSIONET_USER, otherwise prompted for
8+
#
9+
# Requires a PhysioNet account credentialed for MIMIC-IV. The password is always
10+
# requested interactively, so it never reaches the process list or shell history.
11+
#
12+
# Shared by the postgres and duckdb builds: both want the same hosp/ and icu/
13+
# layout, so there is one copy of this here rather than one per engine.
14+
#
15+
# This is a convenience only. If you already have the data, point MIMIC_DATA_DIR
16+
# at it instead; any directory with hosp/ and icu/ subfolders will do.
17+
set -euo pipefail
18+
19+
# The build scripts target the current release of MIMIC-IV.
20+
readonly MIMIC_VERSION="3.1"
21+
22+
DEST="${1:-./mimic-data}"
23+
USERNAME="${2:-${PHYSIONET_USER:-}}"
24+
25+
if [ -z "${USERNAME}" ]; then
26+
read -rp "PhysioNet username: " USERNAME
27+
fi
28+
29+
echo "Downloading MIMIC-IV v${MIMIC_VERSION} to ${DEST}"
30+
31+
# -nH --cut-dirs=4 strips physionet.org/files/mimiciv/<version>/ from the paths,
32+
# leaving the hosp/ and icu/ subfolders directly under ${DEST}. That is the
33+
# layout buildmimic/postgres/load_gz.sql expects.
34+
wget -r -N -c -np -nH --cut-dirs=4 \
35+
-A '*.csv.gz' \
36+
-P "${DEST}" \
37+
--user "${USERNAME}" --ask-password \
38+
"https://physionet.org/files/mimiciv/${MIMIC_VERSION}/"
39+
40+
echo "Downloaded $(find "${DEST}" -name '*.csv.gz' | wc -l | tr -d ' ') files to ${DEST}"

‎mimic-iv/buildmimic/duckdb/README.md‎

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ which you can obtain by either installing
2424
[Windows Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10)
2525
or [Cygwin](https://www.cygwin.com/).
2626

27+
If you would rather not install DuckDB at all, the [docker](docker) folder
28+
builds the same database in a container with a pinned DuckDB version.
29+
2730
## Set-up
2831

2932
### Quick overview
3033

3134
1. [Install](https://duckdb.org/docs/installation/) the CLI version of DuckDB
32-
2. [Download](https://physionet.org/content/mimiciv/2.0) the MIMIC-IV files
35+
2. [Download](https://physionet.org/content/mimiciv/) the MIMIC-IV files
3336
3. Create DuckDB database and load data
3437

3538
### Install DuckDB
@@ -41,6 +44,9 @@ the CLI version of DuckDB.
4144
You will need to place the `duckdb` binary in a folder on your environment path,
4245
e.g. `/usr/local/bin`.
4346

47+
These scripts are built and tested against the 1.4.x LTS line (currently
48+
1.4.5), which is what CI uses.
49+
4450
### Download MIMIC-IV files
4551

4652
Download the CSV files for [MIMIC-IV](https://physionet.org/content/mimiciv/)
@@ -63,43 +69,38 @@ mimic_data_dir
6369

6470
The CSV files can be uncompressed (end in `.csv`) or compressed (end in `.csv.gz`).
6571

66-
The easiest way to download them is to open a terminal then run:
72+
The easiest way to download them is to use the shared download script, which
73+
wraps `wget` and puts the files in the layout the build expects:
6774

68-
```
69-
wget -r -N -c -np --user YOURUSERNAME --ask-password https://physionet.org/files/mimiciv/2.2/
75+
```sh
76+
../download_data.sh ./mimic-data YOURUSERNAME
7077
```
7178

72-
Replace `YOURUSERNAME` with your physionet username.
73-
74-
This will make you `mimic_data_dir` be `physionet.org/files/mimiciv/2.2`.
79+
Replace `YOURUSERNAME` with your physionet username; you will be prompted for
80+
the password. This makes your `mimic_data_dir` be `./mimic-data`.
7581

7682
# Create DuckDB database and load data
7783

7884
The last step requires creating a DuckDB database and
7985
loading the data into it.
8086

81-
You can do all of this with one shell script, `import_duckdb.sh`,
87+
You can do all of this with one shell script, `build_mimic.sh`,
8288
located in this repository.
8389

84-
See the help for it below:
85-
8690
```sh
87-
$ ./import_duckdb.sh -h
88-
./import_duckdb.sh:
89-
USAGE: ./import_duckdb.sh mimic_data_dir [output_db]
90-
WHERE:
91-
mimic_data_dir directory that contains csv.gz or csv files
92-
output_db: optional filename for duckdb file (default: mimic4.db)
93-
$
91+
$ ./build_mimic.sh -h
92+
Usage: build_mimic.sh <mimic_data_dir> [output_db]
93+
mimic_data_dir the directory containing the hosp/ and icu/ subfolders
94+
output_db filename for the duckdb file (default mimic4.db)
9495
```
9596

9697
Here's an example invocation that will make the database in the default "mimic4.db":
9798

9899
```sh
99-
$ ./import_duckdb.sh physionet.org/files/mimiciv/2.2
100+
$ ./build_mimic.sh ./mimic-data
100101

101102
<... output of script snipped ...>
102-
Successfully finished loading data into mimic4.db.
103+
MIMIC-IV build complete: /path/to/mimic4.db
103104

104105
$ ls -lh mimic4.db
105106
-rw-rw-r--. 1 myuser mygroup 93G May 26 16:11 mimic4.db
@@ -109,6 +110,15 @@ The script will print out progress as it goes.
109110
Be patient, this can take minutes to hours to load
110111
depending on your computer's configuration.
111112

113+
Beyond loading the data, it also derives the concepts from
114+
[concepts_duckdb](../../concepts_duckdb) into the `mimiciv_derived` schema and
115+
checks the loaded tables against known row counts. Set `MIMIC_MAKE_CONCEPTS` or
116+
`MIMIC_VALIDATE` to `false` to skip either.
117+
118+
Each step is recorded in a `mimiciv_build_progress` table inside the database
119+
file, and tables that already hold rows are skipped, so re-running the script
120+
after an interruption resumes rather than starting over.
121+
112122
* It took 16m25s on a Fedora 34 workstation with duckdb v 0.2.6, a btrfs filesystem with ztsd level 1 compression, AMD Ryzen 3900X, 32 GB RAM, Samsung 970 Evo NVMe SSD.
113123
* It took ~10m on a Mac M1 Max 2021, 32 GB RAM.
114124

0 commit comments

Comments
 (0)