Skip to content

Commit a0af19c

Browse files
authored
Merge pull request #2018 from MIT-LCP/alistair/transpile
Transpile BigQuery to Postgres/duckdb; handful of concept fixes
2 parents 5706978 + 6b88178 commit a0af19c

111 files changed

Lines changed: 2221 additions & 1134 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/lint_sqlfluff.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
with:
1515
python-version: "3.12"
1616
- name: install sqlfluff
17-
run: "pip install sqlfluff==3.5.0"
17+
run: "pip install sqlfluff==4.1.0"
1818
- name: Get changed files
1919
id: get_file_changes
2020
uses: trilom/file-changes-action@v1.2.4

.gitignore

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
## Local docker-postgres raw data (do not commit large CSVs)
2+
mimic-iv/docker-postgres/mimic-data/
3+
4+
## Allow example env, ignore real env
5+
mimic-iv/docker-postgres/.env
6+
7+
# duckdb / sqlite db files
8+
*.db
9+
10+
# credentials
11+
key.json
12+
113
# Byte-compiled / optimized / DLL files
214
__pycache__/
315
*.py[cod]
@@ -60,4 +72,4 @@ target/
6072
.DS_Store
6173

6274
# PyCharm / JetBrains
63-
.idea
75+
.idea

.sqlfluff

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ capitalisation_policy = upper
1818
extended_capitalisation_policy = upper
1919

2020
[sqlfluff:rules:capitalisation.identifiers]
21-
extended_capitalisation_policy = lower
21+
extended_capitalisation_policy = lower
22+
23+
# Boolean/null literals (TRUE, FALSE, NULL) upper case
24+
[sqlfluff:rules:capitalisation.literals]
25+
capitalisation_policy = upper

README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ The various MIMIC databases are available on Google Cloud Platform (GCP) and Ama
1010

1111
## Navigating this repository
1212

13-
This repository contains code for five databases on PhysioNet:
13+
This repository contains code for the following databases on PhysioNet:
1414

1515
- [MIMIC-III](https://physionet.org/content/mimiciii/) - critical care data for patients admitted to ICUs at the BIDMC between 2001 - 2012
1616
- [MIMIC-IV](https://physionet.org/content/mimiciv/) - hospital and critical care data for patients admitted to the ED or ICU between 2008 - 2019
17+
- [MIMIC-IV-Note](https://physionet.org/content/mimic-iv-note) - deidentified free-text clinical notes
1718
- [MIMIC-IV-ED](https://physionet.org/content/mimic-iv-ed/) - emergency department data for individuals attending the ED between 2011 - 2019
1819
- MIMIC-IV Waveforms (TBD) - this dataset has yet to be published.
1920
- [MIMIC-CXR](https://physionet.org/content/mimic-cxr/) - chest x-ray imaging and deidentified free-text radiology reports for patients admitted to the ED from 2012 - 2016
2021

2122
The repository contains one top-level folder containing community developed code for each datasets:
2223

2324
- [mimic-iii](/mimic-iii) - build scripts for MIMIC-III, derived concepts which are available on the `physionet-data.mimiciii_derived` dataset on BigQuery, and tutorials.
24-
- [mimic-iv](/mimic-iv) - build scripts for MIMIC-IV, derived concepts which are available on the `physionet-data.mimic_derived` dataset on BigQuery, and tutorials.
25+
- [mimic-iv](/mimic-iv) - build scripts for MIMIC-IV, derived concepts which are available on release-specific datasets such as `physionet-data.mimiciv_3_1_derived` on BigQuery, and tutorials.
26+
- [mimic-iv-note](/mimic-iv-note) - build scripts
2527
- [mimic-iv-cxr](/mimic-iv-cxr) - code for loading and analyzing both dicom (mimic-iv-cxr/dcm) and text (mimic-iv-cxr/txt) data. In order to clearly indicate that MIMIC-CXR can be linked with MIMIC-IV, we have named this folder mimic-iv-cxr, and any references to MIMIC-CXR / MIMIC-IV-CXR are interchangeable.
2628
- [mimic-iv-ed](/mimic-iv-ed) - build scripts for MIMIC-IV-ED.
2729
- mimic-iv-waveforms - TBD
@@ -32,10 +34,41 @@ Each subfolder has a README with further detail regarding its content.
3234

3335
MIMIC-III is available on AWS (and MIMIC-IV will be available in the future). Use the below Launch Stack button to deploy access to the MIMIC-III dataset into your AWS account. This will give you real-time access to the MIMIC-III data in your AWS account without having to download a copy of the MIMIC-III dataset. It will also deploy a Jupyter Notebook with access to the content of this GitHub repository in your AWS account. Prior to launching this, please login to the [MIMIC PhysioNet website](https://mimic.mit.edu/), [input your AWS account number](https://physionet.org/settings/cloud/), and [request access to the MIMIC-III Clinical Database on AWS](https://physionet.org/projects/mimiciii/1.4/request_access/2).
3436

35-
To start this deployment, click the Launch Stack button. On the first screen, the template link has already been specified, so just click next. On the second screen, provide a Stack name (letters and numbers) and click next, on the third screen, just click next. On the forth screen, at the bottom, there is a box that says **I acknowledge that AWS CloudFormation might create IAM resources.**. Check that box, and then click **Create**. Once the Stack has complete deploying, look at the **Outputs** tab of the AWS CloudFormation console for links to your Juypter Notebooks instance.
37+
To start this deployment, click the Launch Stack button. On the first screen, the template link has already been specified, so just click next. On the second screen, provide a Stack name (letters and numbers) and click next, on the third screen, just click next. On the forth screen, at the bottom, there is a box that says **I acknowledge that AWS CloudFormation might create IAM resources.**. Check that box, and then click **Create**. Once the Stack has complete deploying, look at the **Outputs** tab of the AWS CloudFormation console for links to your Jupyter Notebooks instance.
3638

3739
[![cloudformation-launch-stack](/mimic-iii/buildmimic/aws-athena/cloudformation-launch-stack.png)](https://console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/new?stackName=MIMIC&templateURL=https://aws-bigdata-blog.s3.amazonaws.com/artifacts/biomedical-informatics-studies/mimic-iii-athena.yaml)
3840

41+
## mimic_utils package
42+
43+
This package contains utilities for working with the MIMIC datasets; primarily transpiling SQL code.
44+
45+
### Installation
46+
47+
```bash
48+
pip install -e ".[test]"
49+
```
50+
51+
### Development
52+
53+
The repository includes a locked dependency file (`requirements-lock.txt`) generated by `pip-compile` for reproducible builds on Python 3.9. To use it:
54+
55+
```bash
56+
pip install -r requirements-lock.txt
57+
pip install -e . --no-deps
58+
```
59+
60+
To update the lock file after changing dependencies in `pyproject.toml`:
61+
62+
```bash
63+
pip-compile pyproject.toml --output-file requirements-lock.txt --all-extras --strip-extras --upgrade
64+
```
65+
66+
### Running tests
67+
68+
```bash
69+
pytest tests/
70+
```
71+
3972
## Other useful tools
4073

4174
* [Bloatectomy](https://github.com/MIT-LCP/bloatectomy) ([paper](https://github.com/MIT-LCP/bloatectomy/blob/master/paper/paper.md)) - A python based package for removing duplicate text in clinical notes
@@ -72,7 +105,7 @@ Our team has worked hard to create and share the MIMIC datasets. We encourage yo
72105
* Commit your changes to the forked repository.
73106
* Submit a pull request to the [MIMIC code repository](https://github.com/MIT-LCP/mimic-code), using the method described at: https://help.github.com/articles/using-pull-requests/
74107

75-
We encourage users to share concepts they have extracted by writing code which generates a materialized view. These materialized views can then be used by researchers around the world to speed up data extraction. For example, ventilation durations can be acquired by creating the ventdurations view in [concepts/durations/ventilation_durations.sql](https://github.com/MIT-LCP/mimic-code/tree/new_consol/mimic-iii/concepts/durations/ventilation_durations.sql).
108+
We encourage users to share concepts they have extracted by writing code which generates a table. These derived tables can then be used by researchers around the world to speed up data extraction. See the [mimic-iv/concepts](mimic-iv/concepts/) folder for examples.
76109

77110
### License
78111

README_mimic_utils.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

mimic-iv-note/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# MIMIC-IV-Note
2+
3+
* [buildmimic](/mimic-iv-note/buildmimic) - Scripts to build MIMIC-IV-Note in various database systems
4+
* [postgres](/mimic-iv-note/buildmimic/postgres) - PostgreSQL (v10+)
5+
* [duckdb](/mimic-iv-note/buildmimic/duckdb) - DuckDB (serverless, single-file)
6+
* [bigquery](/mimic-iv-note/buildmimic/bigquery) - BigQuery schemas
7+
8+
MIMIC-IV-Note contains deidentified free-text clinical notes for patients in MIMIC-IV, including discharge summaries and radiology reports. It is a separate download from MIMIC-IV and is available on [PhysioNet](https://physionet.org/content/mimic-iv-note/).
9+
10+
There are currently no derived concept scripts for MIMIC-IV-Note.
11+
12+
## Building the database
13+
14+
### PostgreSQL
15+
16+
```sh
17+
psql -d mimiciv -f mimic-iv-note/buildmimic/postgres/create.sql
18+
psql -d mimiciv -v ON_ERROR_STOP=1 -v mimic_data_dir=<path to mimic-iv-note>/note \
19+
-f mimic-iv-note/buildmimic/postgres/load_gz.sql
20+
```
21+
22+
See the [postgres README](/mimic-iv-note/buildmimic/postgres/README.md) for full details.
23+
24+
### DuckDB
25+
26+
```sh
27+
cd mimic-iv-note/buildmimic/duckdb
28+
./import_duckdb.sh <path to mimic-iv-note>
29+
```
30+
31+
See the [duckdb README](/mimic-iv-note/buildmimic/duckdb/README.md) for full details.

mimic-iv/README.md

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The BigQuery [physionet-data.mimic_derived](https://console.cloud.google.com/big
2323
These concepts assume the output schema is `mimiciv_derived`. If you would like a different schema, you will need to make a few edits to the scripts.
2424

2525
All concepts are originally written in the **BigQuery Standard SQL Dialect**. A Python package is used to convert these BigQuery scripts into other dialects such as PostgreSQL.
26-
These scripts have been converted to PostgreSQL by a script. To generate the concepts in PostgreSQL, see the [MIMIC-IV postgresql concepts subfolder](/mimic-iv/concepts/postgres).
26+
These scripts have been converted to PostgreSQL and DuckDB by a transpilation script. To generate the concepts in PostgreSQL, see the [MIMIC-IV postgresql concepts subfolder](/mimic-iv/concepts_postgres).
2727
[See below for how scripts in non-bigquery dialects were generated](#transpile).
2828

2929
### BigQuery
@@ -39,29 +39,70 @@ bq query --use_legacy_sql=False --replace --destination_table=my_bigquery_datase
3939

4040
### PostgreSQL
4141

42-
The [postgres](/mimic-iv/concepts_postgres) folder contains concepts in a PostgreSQL compatible dialect.
42+
The [concepts_postgres](/mimic-iv/concepts_postgres) folder contains concepts in a PostgreSQL compatible dialect.
43+
To run them against a local PostgreSQL database:
44+
45+
```sh
46+
psql -d mimiciv -f mimic-iv/concepts_postgres/postgres-make-concepts.sql
47+
```
4348

4449
### DuckDB
4550

46-
The [duckdb](/mimic-iv/concepts_duckdb) folder contains concepts in a DuckDB compatible dialect.
51+
The [concepts_duckdb](/mimic-iv/concepts_duckdb) folder contains concepts in a DuckDB compatible dialect.
52+
To load all derived concepts into an existing DuckDB database (e.g. one built from CSV files):
53+
54+
```python
55+
import duckdb
56+
57+
con = duckdb.connect("~/data/mimic-iv.duckdb")
58+
con.execute("SET search_path = mimiciv_derived")
59+
60+
import os
61+
from pathlib import Path
62+
concepts_dir = Path("mimic-iv/concepts_duckdb")
63+
duckdb_sql = (concepts_dir / "duckdb.sql").read_text()
64+
for line in duckdb_sql.splitlines():
65+
line = line.strip()
66+
if line.startswith(".read "):
67+
sql = (concepts_dir / line[6:]).read_text()
68+
con.execute(sql)
69+
```
70+
71+
Or from the duckdb CLI:
72+
73+
```sh
74+
duckdb ~/data/mimic-iv.duckdb -c "SET search_path = mimiciv_derived" \
75+
< mimic-iv/concepts_duckdb/duckdb.sql
76+
```
4777

4878
## Transpile
4979

50-
The Python package [sqlglot](https://sqlglot.com/) is used to convert from concepts in BigQuery syntax to the other syntaxes ("transpile").
51-
This package parses the SQL into an abstract syntax tree (AST), after which it can be re-written into a specific dialect.
52-
Not all functions are supported by sqlglot, so a helper package was written which adds support for the missing functions.
53-
Most of this process is done the [transpile.py](/src/mimic_utils/transpile.py) file.
80+
The Python package [sqlglot](https://sqlglot.com/) is used to convert concepts from BigQuery SQL to other dialects ("transpile").
81+
It parses the SQL into an abstract syntax tree (AST) and re-writes it for the target dialect.
82+
The `mimic_utils` helper package handles functions not natively supported by sqlglot; see [transpile.py](/src/mimic_utils/transpile.py).
83+
84+
**The transpiled files are already committed** to `concepts_postgres` and `concepts_duckdb`.
85+
You only need to re-run transpilation if you modify the source BigQuery SQL in `concepts/`.
86+
87+
First install the package from the repo root:
5488

55-
An entrypoint is provided for convenience. To transpile a single file, run:
89+
```sh
90+
pip install -e .
91+
```
92+
93+
To transpile a single file:
5694

5795
```sh
58-
# convert_file <source_file> <destination_file> --destination_dialect <dialect>
96+
# mimic_utils convert_file <source_file> <destination_file> [--destination_dialect postgres|duckdb]
5997
mimic_utils convert_file mimic-iv/concepts/demographics/age.sql age.sql --destination_dialect duckdb
6098
```
6199

62-
To transpile all files in a folder, run:
100+
To re-transpile all concepts at once:
63101

64102
```sh
65-
# convert_folder <source_folder> <destination_folder> --destination_dialect <dialect>
103+
# To DuckDB:
66104
mimic_utils convert_folder mimic-iv/concepts mimic-iv/concepts_duckdb --destination_dialect duckdb
105+
106+
# To PostgreSQL:
107+
mimic_utils convert_folder mimic-iv/concepts mimic-iv/concepts_postgres --destination_dialect postgres
67108
```

0 commit comments

Comments
 (0)