-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocess_notes.py
231 lines (206 loc) · 6.76 KB
/
preprocess_notes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python
"""
This script processes each MIMIC note replacing redacted and obvious information
with appropriate tokens.
"""
import re
from datetime import datetime
def redacorator(func):
"""
Decorator for replace functions which passes both the original match and
lower-case version of it to the replace functions. It also checks for
empty redacted information.
"""
def _replace(match):
ori = match.group()
text = match.group().strip().lower()
return func(text, ori)
return _replace
"""
All replace functions take in the original text and a lower-cased version
of it. If the seeking part of the text is found, a replacement token is
is returned, if not the original text is returned.
"""
@redacorator
def replace_names(text, ori):
r = ori
if 'name' in text:
r = 'xxname'
if 'last' in text:
if 'doctor' in text:
r = 'xxdocln'
else:
r = 'xxln'
elif 'first' in text:
if 'doctor' in text:
r = 'xxdocfn'
else:
r = 'xxfn'
elif 'initials' in text:
r = 'xxinit'
return r
@redacorator
def replace_places(text, ori):
r = ori
if 'hospital' in text:
r = 'xxhosp'
elif ('company' in text) or ('university/college' in text):
r = 'xxwork'
elif 'location' in text:
r = 'xxloc'
elif 'country' in text:
r = 'xxcntry'
elif 'state' in text:
r = 'xxstate'
elif ('address' in text) or ('po box' in text):
r = 'xxaddr'
return r
@redacorator
def replace_dates(text, ori):
r = ori
if re.search(r'\d{4}-\d{0,2}-\d{0,2}', text):
r = 'xxdate'
elif (re.search(r'\d{0,2}-\d{0,2}', text)) or (re.search(r'\d{0,2}\/\d{0,2}', text)) or ('month/day' in text):
r = 'xxmmdd'
elif 'year' in text or re.search(r'\b\d{4}\b', text):
r = 'xxyear'
elif 'month' in text:
r = 'xxmnth'
elif 'holiday' in text:
r = 'xxhols'
elif 'date range' in text:
r = 'xxdtrnge'
return r
@redacorator
def replace_identifiers(text, ori):
r = ori
if ('numeric identifier' in text) or ('pager number' in text):
r = 'xxpager'
elif '(radiology)' in text:
r = 'xxradclip'
elif 'social security number' in text:
r = 'xxssn'
elif 'medical record number' in text:
r = 'xxmrno'
elif 'age over 90' in text:
r = 'xxage90'
elif 'serial number' in text:
r = 'xxsno'
elif 'unit number' in text:
r = 'xxunitno'
elif 'md number' in text:
r = 'xxmdno'
elif 'telephone/fax' in text:
r = 'xxph'
elif 'provider number' in text:
r = 'xxpno'
elif 'job number' in text:
r = 'xxjobno'
elif 'dictator info' in text:
r = 'xxdicinfo'
elif 'contact info' in text:
r = 'xxcntinfo'
elif 'attending info' in text:
r = 'xxattinfo'
return r
@redacorator
def replace_digits(text, ori):
r = ori
if re.search(r'\d\d\d', text):
r = 'xx3digit'
elif re.search(r'\d\d', text):
r = 'xx2digit'
elif re.search(r'\d', text):
r = 'xx1digit'
return r
def replace_redacted(text):
"""
Function that compiles the redacted pattern and calls all the replace functions
"""
pat = re.compile(r'\[\*\*(.*?)\*\*\]', re.IGNORECASE)
# replace name types
text = pat.sub(replace_names, text)
# replace place types
text = pat.sub(replace_places, text)
# replace person identifier types
text = pat.sub(replace_identifiers, text)
# replace date types
text = pat.sub(replace_dates, text)
# replace remaining digits
text = pat.sub(replace_digits, text)
return text
@redacorator
def replace_time(text, ori):
"""
Replace times with divided up tokens representing the hour.
E.g., 8:20 AM is replaced by t_forenoon
Replace 2-digit redacted information that precedes time identifier with
a generic token
E.g., [**84**] AM is replaced by t_hour
"""
r = ori
if '**' in text:
r = 'xxhour'
else:
try:
# handle exceptions with custom rules
f, s = text.split()
s = 'am' if s[0] == 'a' else 'pm'
l, r = f.split(':')
if l == '' or l == '00':
if r == '':
r = str(0).zfill(2)
l = str(12)
if int(l) > 12:
l = str(int(l) % 12)
f = ':'.join([l, r])
text = ' '.join([f, s])
d = datetime.strptime(text, '%I:%M %p')
if d.hour >= 0 and d.hour < 4:
r = 'xxmidngt'
elif d.hour >= 4 and d.hour < 8:
r = 'xxdawn'
elif d.hour >= 8 and d.hour < 12:
r = 'xxfore'
elif d.hour >= 12 and d.hour < 16:
r = 'xxafter'
elif d.hour >=16 and d.hour <20:
r = 'xxdusk'
else:
r = 'xxngt'
except ValueError:
pass
return r
def replace_misc(text):
"""
Replaces certain obvious easy to process items in the notes for helping
downstream modeling
"""
# replace different types of "year old" with
# matches: y.o., y/o, years old. year old, yearold
text = re.sub(r'-?\byears? ?-?old\b|\by(?:o|r)*[ ./-]*o(?:ld)?\b', ' yo', text, flags=re.IGNORECASE)
# Does the same thing as above but copied from https://arxiv.org/abs/1808.02622v1
text = re.sub(r'(\d+)\s*(year\s*old|y.\s*o.|yo|year\s*old|year-old|-year-old|-year old)', r'\1 yo', text, flags=re.IGNORECASE)
# replaces yr, yr's, yrs with years
text = re.sub(r'\byr[\'s]*\b', 'years', text, re.IGNORECASE)
# replace Pt and pt with patient, and IN/OUT/OT PT with patient
# Note: PT also refers to physical therapy and physical therapist
text = re.sub(r'\b[P|p]t.?|\b(IN|OU?T) PT\b', 'patient ', text)
# replace sex with consistant token
text = re.sub(r'\b(gentlman|male|man|m|M)(?!\S)\b', 'male', text)
text = re.sub(r'\b(female|woman|f|F)(?!\S)\b', 'female', text)
# replace time types
text = re.sub(r'\d{0,2}:\d{0,2} \b[A|P]\.?M\.?\b', replace_time, text, flags=re.IGNORECASE)
text = re.sub(r'\[\*\*(\d{2})\*\*\] \b[a|p].?m.?\b', replace_time, text, flags=re.IGNORECASE)
# finally remove leftover redacted stuff (mostly empty)
text = re.sub(r'\[\*\*(.*?)\*\*\]', '', text, flags=re.IGNORECASE)
return text
def preprocess_note(text):
"""
Master function to processes all the notes
"""
# replace redacted info with tokens
text = replace_redacted(text)
# misc scrubbing
text = replace_misc(text)
return text