Skip to content

Commit c07b7a9

Browse files
committed
fix suspicion of infection to use positive culture, if it exists, indep of ties
1 parent 5a45342 commit c07b7a9

3 files changed

Lines changed: 30 additions & 12 deletions

File tree

mimic-iv/concepts/sepsis/suspicion_of_infection.sql

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ WITH ab_tbl AS (
1010
, DATETIME_TRUNC(abx.starttime, DAY) AS antibiotic_date
1111
, abx.stoptime AS antibiotic_stoptime
1212
-- create a unique identifier for each patient antibiotic
13+
-- hadm_id/stay_id tiebreak keeps the numbering deterministic when
14+
-- multiple antibiotics share the same start/stop time and name
1315
, ROW_NUMBER() OVER
1416
(
1517
PARTITION BY subject_id
16-
ORDER BY starttime, stoptime, antibiotic
18+
ORDER BY starttime, stoptime, antibiotic, hadm_id, stay_id
1719
) AS ab_id
1820
FROM `physionet-data.mimiciv_derived.antibiotic` abx
1921
)
@@ -57,10 +59,18 @@ WITH ab_tbl AS (
5759
-- before this abx
5860
-- this ensures each antibiotic is only matched to a single culture
5961
-- and consequently we have 1 row per antibiotic
62+
-- among cultures at the same time point, prefer a positive culture
63+
-- (positiveculture DESC) so that any positive culture yields
64+
-- positive_culture = 1, per the suspicion of infection criteria; the
65+
-- micro_specimen_id tiebreak makes the selection deterministic
6066
, ROW_NUMBER() OVER
6167
(
6268
PARTITION BY ab_tbl.subject_id, ab_tbl.ab_id
63-
ORDER BY me72.chartdate, me72.charttime NULLS LAST
69+
ORDER BY
70+
me72.chartdate
71+
, me72.charttime NULLS LAST
72+
, me72.positiveculture DESC
73+
, me72.micro_specimen_id
6474
) AS micro_seq
6575
FROM ab_tbl
6676
-- abx taken after culture, but no more than 72 hours after
@@ -104,10 +114,18 @@ WITH ab_tbl AS (
104114
-- before this abx
105115
-- this ensures each antibiotic is only matched to a single culture
106116
-- and consequently we have 1 row per antibiotic
117+
-- among cultures at the same time point, prefer a positive culture
118+
-- (positiveculture DESC) so that any positive culture yields
119+
-- positive_culture = 1, per the suspicion of infection criteria; the
120+
-- micro_specimen_id tiebreak makes the selection deterministic
107121
, ROW_NUMBER() OVER
108122
(
109123
PARTITION BY ab_tbl.subject_id, ab_tbl.ab_id
110-
ORDER BY me24.chartdate, me24.charttime NULLS LAST
124+
ORDER BY
125+
me24.chartdate
126+
, me24.charttime NULLS LAST
127+
, me24.positiveculture DESC
128+
, me24.micro_specimen_id
111129
) AS micro_seq
112130
FROM ab_tbl
113131
-- culture in subsequent 24 hours

mimic-iv/concepts_duckdb/sepsis/suspicion_of_infection.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ WITH ab_tbl AS (
1111
abx.stoptime AS antibiotic_stoptime,
1212
ROW_NUMBER() OVER (
1313
PARTITION BY subject_id
14-
ORDER BY starttime NULLS FIRST, stoptime NULLS FIRST, antibiotic NULLS FIRST
14+
ORDER BY starttime NULLS FIRST, stoptime NULLS FIRST, antibiotic NULLS FIRST, hadm_id NULLS FIRST, stay_id NULLS FIRST
1515
) AS ab_id
1616
FROM mimiciv_derived.antibiotic AS abx
1717
), me AS (
@@ -44,7 +44,7 @@ WITH ab_tbl AS (
4444
me72.spec_type_desc AS last72_specimen,
4545
ROW_NUMBER() OVER (
4646
PARTITION BY ab_tbl.subject_id, ab_tbl.ab_id
47-
ORDER BY me72.chartdate NULLS FIRST, me72.charttime
47+
ORDER BY me72.chartdate NULLS FIRST, me72.charttime, me72.positiveculture DESC, me72.micro_specimen_id NULLS FIRST
4848
) AS micro_seq
4949
FROM ab_tbl
5050
LEFT JOIN me AS me72
@@ -73,7 +73,7 @@ WITH ab_tbl AS (
7373
me24.spec_type_desc AS next24_specimen,
7474
ROW_NUMBER() OVER (
7575
PARTITION BY ab_tbl.subject_id, ab_tbl.ab_id
76-
ORDER BY me24.chartdate NULLS FIRST, me24.charttime
76+
ORDER BY me24.chartdate NULLS FIRST, me24.charttime, me24.positiveculture DESC, me24.micro_specimen_id NULLS FIRST
7777
) AS micro_seq
7878
FROM ab_tbl
7979
LEFT JOIN me AS me24

mimic-iv/concepts_postgres/sepsis/suspicion_of_infection.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ WITH ab_tbl AS (
99
abx.antibiotic,
1010
abx.starttime AS antibiotic_time, /* date is used to match microbiology cultures with only date available */
1111
DATE_TRUNC('day', abx.starttime) AS antibiotic_date,
12-
abx.stoptime AS antibiotic_stoptime, /* create a unique identifier for each patient antibiotic */
12+
abx.stoptime AS antibiotic_stoptime, /* create a unique identifier for each patient antibiotic */ /* hadm_id/stay_id tiebreak keeps the numbering deterministic when */ /* multiple antibiotics share the same start/stop time and name */
1313
ROW_NUMBER() OVER (
1414
PARTITION BY subject_id
15-
ORDER BY starttime NULLS FIRST, stoptime NULLS FIRST, antibiotic NULLS FIRST
15+
ORDER BY starttime NULLS FIRST, stoptime NULLS FIRST, antibiotic NULLS FIRST, hadm_id NULLS FIRST, stay_id NULLS FIRST
1616
) AS ab_id
1717
FROM mimiciv_derived.antibiotic AS abx
1818
), me AS (
@@ -42,10 +42,10 @@ WITH ab_tbl AS (
4242
me72.micro_specimen_id,
4343
COALESCE(me72.charttime, CAST(me72.chartdate AS TIMESTAMP)) AS last72_charttime,
4444
me72.positiveculture AS last72_positiveculture,
45-
me72.spec_type_desc AS last72_specimen, /* we will use this partition to select the earliest culture */ /* before this abx */ /* this ensures each antibiotic is only matched to a single culture */ /* and consequently we have 1 row per antibiotic */
45+
me72.spec_type_desc AS last72_specimen, /* we will use this partition to select the earliest culture */ /* before this abx */ /* this ensures each antibiotic is only matched to a single culture */ /* and consequently we have 1 row per antibiotic */ /* among cultures at the same time point, prefer a positive culture */ /* (positiveculture DESC) so that any positive culture yields */ /* positive_culture = 1, per the suspicion of infection criteria; the */ /* micro_specimen_id tiebreak makes the selection deterministic */
4646
ROW_NUMBER() OVER (
4747
PARTITION BY ab_tbl.subject_id, ab_tbl.ab_id
48-
ORDER BY me72.chartdate NULLS FIRST, me72.charttime
48+
ORDER BY me72.chartdate NULLS FIRST, me72.charttime, me72.positiveculture DESC NULLS LAST, me72.micro_specimen_id NULLS FIRST
4949
) AS micro_seq
5050
FROM ab_tbl
5151
/* abx taken after culture, but no more than 72 hours after */
@@ -72,10 +72,10 @@ WITH ab_tbl AS (
7272
me24.micro_specimen_id,
7373
COALESCE(me24.charttime, CAST(me24.chartdate AS TIMESTAMP)) AS next24_charttime,
7474
me24.positiveculture AS next24_positiveculture,
75-
me24.spec_type_desc AS next24_specimen, /* we will use this partition to select the earliest culture */ /* before this abx */ /* this ensures each antibiotic is only matched to a single culture */ /* and consequently we have 1 row per antibiotic */
75+
me24.spec_type_desc AS next24_specimen, /* we will use this partition to select the earliest culture */ /* before this abx */ /* this ensures each antibiotic is only matched to a single culture */ /* and consequently we have 1 row per antibiotic */ /* among cultures at the same time point, prefer a positive culture */ /* (positiveculture DESC) so that any positive culture yields */ /* positive_culture = 1, per the suspicion of infection criteria; the */ /* micro_specimen_id tiebreak makes the selection deterministic */
7676
ROW_NUMBER() OVER (
7777
PARTITION BY ab_tbl.subject_id, ab_tbl.ab_id
78-
ORDER BY me24.chartdate NULLS FIRST, me24.charttime
78+
ORDER BY me24.chartdate NULLS FIRST, me24.charttime, me24.positiveculture DESC NULLS LAST, me24.micro_specimen_id NULLS FIRST
7979
) AS micro_seq
8080
FROM ab_tbl
8181
/* culture in subsequent 24 hours */

0 commit comments

Comments
 (0)