1.mmic-iv与mimic-iii的区别之一就是前者分了3个模块,mimic_icu,mimic_hosp和mimic_core, 而在学习过程中我们还可以观察到另外一个模块mimic_derived, 而且这个模块仅存在与谷歌云的mimic数据库中, 本地建立的数据库中虽然有这个模块,但是没有内容(需要自己根据官方提供的代码生成,也是现在我们要查看的内容)。
由于mimic_derived 参与了很多内容的查询,熟悉这个模块的内容还是很有必要的,但是官方对这个模块缺乏详细的介绍。今天通过官方提供的源代码查看理解mimic_derived中各个表格的内容,记录在这里,供大家参考。
注:仅包含了作者认为重要的表格,并非全部。
- age,为患者入院(admission)时的年龄,存储在age列。
-- This query calculates the age of a patient on admission to the hospital.
-- The columns of the table patients: anchor_age, anchor_year, anchor_year_group
-- provide information regarding the actual patient year for the patient admission,
-- and the patient's age at that time.
-- anchor_year is a shifted year for the patient.
-- anchor_year_group is a range of years - the patient's anchor_year occurred during this range.
-- anchor_age is the patient's age in the anchor_year.
-- Example: a patient has an anchor_year of 2153,
-- anchor_year_group of 2008 - 2010, and an anchor_age of 60.
-- The year 2153 for the patient corresponds to 2008, 2009, or 2010.
-- The patient was 60 in the shifted year of 2153, i.e. they were 60 in 2008, 2009, or 2010.
-- A patient admission in 2154 will occur in 2009-2011,
-- an admission in 2155 will occur in 2010-2012, and so on.
-- Therefore, the age of a patient = hospital admission time - anchor_year + anchor_age
SELECT
ad.subject_id
, ad.hadm_id
, ad.admittime
, pa.anchor_age
, pa.anchor_year
, DATETIME_DIFF(ad.admittime, DATETIME(pa.anchor_year, 1, 1, 0, 0, 0), YEAR) + pa.anchor_age AS age
FROM `physionet-data.mimic_core.admissions` ad
INNER JOIN `physionet-data.mimic_core.patients` pa
ON ad.subject_id = pa.subject_id
;
- 令人惊喜的发现derived中的一个表格icustay_detail提供了多种结局的数据, 包括是否发生院内死亡(hospital_expire_flag)、住院时间(los_hospital)、住ICU时间(los_icu)以及是否是首次进入院,是否是首次进入ICU等,为提取部分患者结局变量提供了方便。
SELECT ie.subject_id, ie.hadm_id, ie.stay_id
-- patient level factors
, pat.gender, pat.dod
-- hospital level factors
, adm.admittime, adm.dischtime
, DATETIME_DIFF(adm.dischtime, adm.admittime, DAY) as los_hospital
, DATETIME_DIFF(adm.admittime, DATETIME(pat.anchor_year, 1, 1, 0, 0, 0), YEAR) + pat.anchor_age as admission_age
, adm.ethnicity
, adm.hospital_expire_flag
, DENSE_RANK() OVER (PARTITION BY adm.subject_id ORDER BY adm.admittime) AS hospstay_seq
, CASE
WHEN DENSE_RANK() OVER (PARTITION BY adm.subject_id ORDER BY adm.admittime) = 1 THEN True
ELSE False END AS first_hosp_stay
-- icu level factors
, ie.intime as icu_intime, ie.outtime as icu_outtime
, ROUND(DATETIME_DIFF(ie.outtime, ie.intime, HOUR)/24.0, 2) as los_icu
, DENSE_RANK() OVER (PARTITION BY ie.hadm_id ORDER BY ie.intime) AS icustay_seq
-- first ICU stay *for the current hospitalization*
, CASE
WHEN DENSE_RANK() OVER (PARTITION BY ie.hadm_id ORDER BY ie.intime) = 1 THEN True
ELSE False END AS first_icu_stay
FROM `physionet-data.mimic_icu.icustays` ie
INNER JOIN `physionet-data.mimic_core.admissions` adm
ON ie.hadm_id = adm.hadm_id
INNER JOIN `physionet-data.mimic_core.patients` pat
ON ie.subject_id = pat.subject_id
- weight_duration, ICU期间体重的变化,体重是反应患者营养状况的重要因素,常被纳入到预测因素中。
-- This query extracts weights for adult ICU patients with start/stop times
-- if an admission weight is given, then this is assigned from intime to outtime
WITH wt_stg as
(
SELECT
c.stay_id
, c.charttime
, case when c.itemid = 226512 then 'admit'
else 'daily' end as weight_type
-- TODO: eliminate obvious outliers if there is a reasonable weight
, c.valuenum as weight
FROM `physionet-data.mimic_icu.chartevents` c
WHERE c.valuenum IS NOT NULL
AND c.itemid in
(
226512 -- Admit Wt
, 224639 -- Daily Weight
)
AND c.valuenum > 0
)
-- assign ascending row number
, wt_stg1 as
(
select
stay_id
, charttime
, weight_type
, weight
, ROW_NUMBER() OVER (partition by stay_id, weight_type order by charttime) as rn
from wt_stg
WHERE weight IS NOT NULL
)
-- change charttime to intime for the first admission weight recorded
, wt_stg2 AS
(
SELECT
wt_stg1.stay_id
, ie.intime, ie.outtime
, wt_stg1.weight_type
, case when wt_stg1.weight_type = 'admit' and wt_stg1.rn = 1
then DATETIME_SUB(ie.intime, INTERVAL '2' HOUR)
else wt_stg1.charttime end as starttime
, wt_stg1.weight
from wt_stg1
INNER JOIN `physionet-data.mimic_icu.icustays` ie
on ie.stay_id = wt_stg1.stay_id
)
, wt_stg3 as
(
select
stay_id
, intime, outtime
, starttime
, coalesce(
LEAD(starttime) OVER (PARTITION BY stay_id ORDER BY starttime),
DATETIME_ADD(outtime, INTERVAL '2' HOUR)
) as endtime
, weight
, weight_type
from wt_stg2
)
-- this table is the start/stop times from admit/daily weight in charted data
, wt1 as
(
select
stay_id
, starttime
, coalesce(endtime,
LEAD(starttime) OVER (partition by stay_id order by starttime),
-- impute ICU discharge as the end of the final weight measurement
-- plus a 2 hour "fuzziness" window
DATETIME_ADD(outtime, INTERVAL '2' HOUR)
) as endtime
, weight
, weight_type
from wt_stg3
)
-- if the intime for the patient is < the first charted daily weight
-- then we will have a "gap" at the start of their stay
-- to prevent this, we look for these gaps and backfill the first weight
-- this adds (153255-149657)=3598 rows, meaning this fix helps for up to 3598 stay_id
, wt_fix as
(
select ie.stay_id
-- we add a 2 hour "fuzziness" window
, DATETIME_SUB(ie.intime, INTERVAL '2' HOUR) as starttime
, wt.starttime as endtime
, wt.weight
, wt.weight_type
from `physionet-data.mimic_icu.icustays` ie
inner join
-- the below subquery returns one row for each unique stay_id
-- the row contains: the first starttime and the corresponding weight
(
SELECT wt1.stay_id, wt1.starttime, wt1.weight
, weight_type
, ROW_NUMBER() OVER (PARTITION BY wt1.stay_id ORDER BY wt1.starttime) as rn
FROM wt1
) wt
ON ie.stay_id = wt.stay_id
AND wt.rn = 1
and ie.intime < wt.starttime
)
-- add the backfill rows to the main weight table
SELECT
wt1.stay_id
, wt1.starttime
, wt1.endtime
, wt1.weight
, wt1.weight_type
FROM wt1
UNION ALL
SELECT
wt_fix.stay_id
, wt_fix.starttime
, wt_fix.endtime
, wt_fix.weight
, wt_fix.weight_type
FROM wt_fix;
- bg (blood gas), 血气分析,代码注释中说来源是hosp模块的labevents,部分指标也和ICU相关。有个地方存疑,血气指标皆取了最大值,不知道这是否符合医学常识, 注释中解释为“simply collapse rows”(仅是为了压缩列),不一定符合实际中的需求,注意。
-- The aim of this query is to pivot entries related to blood gases
-- which were found in LABEVENTS
WITH bg AS
(
select
-- specimen_id only ever has 1 measurement for each itemid
-- so, we may simply collapse rows using MAX()
MAX(subject_id) AS subject_id
, MAX(hadm_id) AS hadm_id
, MAX(charttime) AS charttime
-- specimen_id *may* have different storetimes, so this is taking the latest
, MAX(storetime) AS storetime
, le.specimen_id
, MAX(CASE WHEN itemid = 52028 THEN value ELSE NULL END) AS specimen
, MAX(CASE WHEN itemid = 50801 THEN valuenum ELSE NULL END) AS aado2
, MAX(CASE WHEN itemid = 50802 THEN valuenum ELSE NULL END) AS baseexcess
, MAX(CASE WHEN itemid = 50803 THEN valuenum ELSE NULL END) AS bicarbonate
, MAX(CASE WHEN itemid = 50804 THEN valuenum ELSE NULL END) AS totalco2
, MAX(CASE WHEN itemid = 50805 THEN valuenum ELSE NULL END) AS carboxyhemoglobin
, MAX(CASE WHEN itemid = 50806 THEN valuenum ELSE NULL END) AS chloride
, MAX(CASE WHEN itemid = 50808 THEN valuenum ELSE NULL END) AS calcium
, MAX(CASE WHEN itemid = 50809 and valuenum <= 10000 THEN valuenum ELSE NULL END) AS glucose
, MAX(CASE WHEN itemid = 50810 and valuenum <= 100 THEN valuenum ELSE NULL END) AS hematocrit
, MAX(CASE WHEN itemid = 50811 THEN valuenum ELSE NULL END) AS hemoglobin
, MAX(CASE WHEN itemid = 50813 and valuenum <= 10000 THEN valuenum ELSE NULL END) AS lactate
, MAX(CASE WHEN itemid = 50814 THEN valuenum ELSE NULL END) AS methemoglobin
, MAX(CASE WHEN itemid = 50815 THEN valuenum ELSE NULL END) AS o2flow
-- fix a common unit conversion error for fio2
-- atmospheric o2 is 20.89%, so any value <= 20 is unphysiologic
-- usually this is a misplaced O2 flow measurement
, MAX(CASE WHEN itemid = 50816 THEN
CASE
WHEN valuenum > 20 AND valuenum <= 100 THEN valuenum
WHEN valuenum > 0.2 AND valuenum <= 1.0 THEN valuenum*100.0
ELSE NULL END
ELSE NULL END) AS fio2
, MAX(CASE WHEN itemid = 50817 AND valuenum <= 100 THEN valuenum ELSE NULL END) AS so2
, MAX(CASE WHEN itemid = 50818 THEN valuenum ELSE NULL END) AS pco2
, MAX(CASE WHEN itemid = 50819 THEN valuenum ELSE NULL END) AS peep
, MAX(CASE WHEN itemid = 50820 THEN valuenum ELSE NULL END) AS ph
, MAX(CASE WHEN itemid = 50821 THEN valuenum ELSE NULL END) AS po2
, MAX(CASE WHEN itemid = 50822 THEN valuenum ELSE NULL END) AS potassium
, MAX(CASE WHEN itemid = 50823 THEN valuenum ELSE NULL END) AS requiredo2
, MAX(CASE WHEN itemid = 50824 THEN valuenum ELSE NULL END) AS sodium
, MAX(CASE WHEN itemid = 50825 THEN valuenum ELSE NULL END) AS temperature
, MAX(CASE WHEN itemid = 50807 THEN value ELSE NULL END) AS comments
FROM mimic_hosp.labevents le
where le.ITEMID in
-- blood gases
(
52028 -- specimen
, 50801 -- aado2
, 50802 -- base excess
, 50803 -- bicarb
, 50804 -- calc tot co2
, 50805 -- carboxyhgb
, 50806 -- chloride
-- , 52390 -- chloride, WB CL-
, 50807 -- comments
, 50808 -- free calcium
, 50809 -- glucose
, 50810 -- hct
, 50811 -- hgb
, 50813 -- lactate
, 50814 -- methemoglobin
, 50815 -- o2 flow
, 50816 -- fio2
, 50817 -- o2 sat
, 50818 -- pco2
, 50819 -- peep
, 50820 -- pH
, 50821 -- pO2
, 50822 -- potassium
-- , 52408 -- potassium, WB K+
, 50823 -- required O2
, 50824 -- sodium
-- , 52411 -- sodium, WB NA +
, 50825 -- temperature
)
GROUP BY le.specimen_id
)
, stg_spo2 as
(
select subject_id, charttime
-- avg here is just used to group SpO2 by charttime
, AVG(valuenum) as SpO2
FROM mimic_icu.chartevents
where ITEMID = 220277 -- O2 saturation pulseoxymetry
and valuenum > 0 and valuenum
3006



被折叠的 条评论
为什么被折叠?



