MIMIC-III tutorial

This notebook shows examples of how to use the cycquery tool on MIMIC-III v1.4.

Each query is limit to 100 rows (for quick results).

  • First, setup the MIMIC-III database according to the instructions in mimic-code.

  • The database is assumed to be hosted using postgres. Update the config parameters such as username and password, passed to MIMICIIIQuerier accordingly.

Imports and instantiate MIMICIIIQuerier

[1]:
"""MIMICIII tutorial."""

import cycquery.ops as qo
from cycquery import MIMICIIIQuerier


querier = MIMICIIIQuerier(
    dbms="postgresql",
    port=5432,
    host="localhost",
    database="mimiciii",
    user="postgres",
    password="pwd",
)
# List all custom table methods.
querier.list_custom_tables()
2024-11-20 05:43:22,707 INFO cycquery.orm    - Database setup, ready to run queries!
[1]:
['chartevents', 'diagnoses', 'labevents']

Example 1. Get all male patients with a mortality outcome.

[2]:
ops = qo.Sequential(
    qo.ConditionEquals("expire_flag", 1),
    qo.ConditionEquals("gender", "M"),
)
patients = querier.mimiciii.patients()
patients = patients.ops(ops).run(limit=100)
print(f"{len(patients)} rows extracted!")
2024-11-20 05:43:29,122 INFO cycquery.orm    - Query returned successfully!
2024-11-20 05:43:29,124 INFO cycquery.utils.profile - Finished executing function run_query in 0.371992 s
100 rows extracted!

Example 2. Get all female patient encounters with diagnoses (gastroenteritis in ICD-9 long title).

[3]:
patients = querier.mimiciii.patients()
patients = patients.ops(qo.ConditionEquals("gender", "F"))
admissions = querier.mimiciii.admissions()
patient_admissions = patients.join(
    join_table=admissions,
    on="subject_id",
)
diagnoses = querier.diagnoses()
diagnoses = diagnoses.ops(qo.ConditionSubstring("long_title", "gastroenteritis"))
patient_admissions_diagnoses = patient_admissions.join(
    join_table=diagnoses,
    on=["subject_id", "hadm_id"],
).run(limit=100)
print(f"{len(patient_admissions_diagnoses)} rows extracted!")
2024-11-20 05:43:39,240 INFO cycquery.orm    - Query returned successfully!
2024-11-20 05:43:39,242 INFO cycquery.utils.profile - Finished executing function run_query in 10.067851 s
100 rows extracted!

Example 3. Get potassium lab tests for female patients.

[4]:
patients = querier.mimiciii.patients()
patients = patients.ops(qo.ConditionEquals("gender", "F"))
labs = querier.labevents()
labs = labs.ops(qo.ConditionEquals("label", "potassium"))
patient_labs = patients.join(labs, on="subject_id").run(limit=100)
print(f"{len(patient_labs)} rows extracted!")
2024-11-20 05:43:39,628 INFO cycquery.orm    - Query returned successfully!
2024-11-20 05:43:39,630 INFO cycquery.utils.profile - Finished executing function run_query in 0.337543 s
100 rows extracted!

Example 4. Get AaDO2 carevue chart events for male patients that have a valuenum of less than 20.

[5]:
chartevents_ops = qo.Sequential(
    qo.ConditionEquals("dbsource", "carevue"),
    qo.ConditionEquals("label", "AaDO2"),
    qo.ConditionLessThan("valuenum", 20),
)
patients = querier.mimiciii.patients()
patients = patients.ops(qo.ConditionEquals("gender", "M"))
chart_events = querier.chartevents()
chart_events = chart_events.ops(chartevents_ops)
patient_chart_events = patients.join(chart_events, on="subject_id").run(limit=100)
print(f"{len(patient_chart_events)} rows extracted!")
2024-11-20 06:17:57,933 INFO cycquery.orm    - Query returned successfully!
2024-11-20 06:17:57,937 INFO cycquery.utils.profile - Finished executing function run_query in 2058.240027 s
6 rows extracted!