[de07e6]: / src / Preporcessor / preprocess_clinical_notes.py

Download this file

528 lines (417 with data), 17.6 kB

  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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
import sys
import nltk
import re
import os
import pandas as pd
import utils
INPUT_FILEPATH = "/mnt/cbib/EOSC4Cancer/synthetic_data"
PREPROCESSED_OUTPUT_FILEPATH = "../data/preprocessed_data/patient_notes/"
# remove characters encodings if present
chars = {
'\xc2\x82' : ',', # High code comma
'\xc2\x84' : ',,', # High code double comma
'\xc2\x85' : '...', # Tripple dot
'\xc2\x88' : '^', # High carat
'\xc2\x91' : '\x27', # Forward single quote
'\xc2\x92' : '\x27', # Reverse single quote
'\xc2\x93' : '\x22', # Forward double quote
'\xc2\x94' : '\x22', # Reverse double quote
'\xc2\x95' : ' ',
'\xc2\x96' : '-', # High hyphen
'\xc2\x97' : '--', # Double hyphen
'\xc2\x99' : ' ',
'\xc2\xa0' : ' ',
'\xc2\xa6' : '|', # Split vertical bar
'\xc2\xab' : '<<', # Double less than
'\xc2\xbb' : '>>', # Double greater than
'\xc2\xbc' : '1/4', # one quarter
'\xc2\xbd' : '1/2', # one half
'\xc2\xbe' : '3/4', # three quarters
'\xca\xbf' : '\x27', # c-single quote
'\xcc\xa8' : '', # modifier - under curve
'\xcc\xb1' : '', # modifier - under line
'\x95' : ' ',
}
def replace_chars(match):
char = match.group(0)
return chars[char]
def remove_special_chars_encoding(text):
pattern = '(' + '|'.join(chars.keys()) + ')'
return re.sub(pattern, replace_chars, text)
def add_space_after_parenthesis(text):
# Use regular expression to find ")" followed by a letter and insert a space
result = re.sub(r'\)([a-zA-Z])', r') \1', text)
return result
def tokenize_clinical_note(id_list, source_directory=INPUT_FILEPATH):
data = []
for _id in id_list:
with open(source_directory + "/" + "%s"%_id + "/" + "clinical_note.txt") as f:
text = f.read()
phi_tags = re.findall('(\[\*\*.*?\*\*\])', text)
for i,tag in enumerate(phi_tags):
text = text.replace(tag, ' __PHI_%d__ ' % i)
text = text.replace(tag, '__PHI__')
'''
Thoughts & Strategies
- If a newline happens in between matching parens, then ignore newline
- Identify prose v nonprose. Use nltk.sent_tokenize on prose
- not perfect, but it is pretty good with ignoring mid-sentence newlines
- section header: "\n------ Protected Section ------\n"
- If you can detect a bulleted list, then those are sentences
- sometimes the bullets are hyphens "-", sometimes numbers "1."
- ecg reports are very short & seem to be exclusively prose
- echo reports are VERY structured. definitely useful to do a echo-specific one
- nursing notes have a linear structure. sections are easily identifiable
- nursing_other notes are typically very short (and parse-able)
- radiology reports have lots of "________________________" sections
- seems like they always have "MEDICAL CONDITION" and "FINAL REPORT" sections
- found list example where "1." is its own line and its info is own next line :/
- could be rehab-specific for notes
- strong consistency format in social_work
'''
# break into many segments
segments = mimic_sent_tokenize_rules(text)
# run nltk tokenizer on these segments to split prose
sents = []
for segment in segments:
s = nltk.sent_tokenize(segment)
sents += s
# put the PHI back
for i in range(len(sents)):
tags = re.findall('(__PHI_(\d+)__)', sents[i])
for tag,ind in tags:
sents[i] = sents[i].replace(tag, phi_tags[int(ind)])
most_recent_title = None
most_recent_inline_title = None
for sentence in sents:
sentence = remove_full_stops_and_commas(sentence)
if len(sentence) > 1:
is_sentence_title = is_title(sentence)
is_sentence_inline_title = is_inline_title(sentence)
field = None
if is_sentence_title:
most_recent_title = sentence
field = sentence[:-1]
sentence = "___"
elif is_sentence_inline_title:
most_recent_inline_title = sentence.split(":", 1)[0]
field = most_recent_inline_title
sentence = sentence.split(":", 1)[1]
else:
# Check for the most recent title or inline title encountered
if most_recent_title:
field = most_recent_title[:-1]
elif most_recent_inline_title:
field = most_recent_inline_title
data.append((_id, field, sentence.replace("___", ""), is_sentence_title, is_sentence_inline_title))
else:
continue
# Create a DataFrame with 'Sentence' and 'IsTitle' columns
df = pd.DataFrame(data, columns=['id', 'field', 'sentence', 'IsTitle', 'IsInlineTitle'])
df.to_csv(PREPROCESSED_OUTPUT_FILEPATH + "%s_preprocessed.csv"%id_list[0])
return df
def mimic_sent_tokenize_rules(text):
# long sections are OBVIOUSLY different sentences
text = re.sub('---+', '\n\n-----\n\n', text)
text = re.sub('___+', '\n\n_____\n\n', text)
text = re.sub('\n\n+', '\n\n', text)
text = text.replace("___", "")
text = re.sub(r'(?<!\n)\n(?!\n)(?=[A-Z])', '\n\n', text)
text = re.sub(r' ', ' ', text)
text = add_space_after_parenthesis(text)
text = remove_special_chars_encoding(text)
segments = re.split(r'\n\n+', re.sub(r':\n', ':\n\n', text))
# strategy: break down segments and chip away structure until just prose.
# once you have prose, use nltk.sent_tokenize()
### Separate section headers ###
new_segments = []
# deal with this one edge case (multiple headers per line) up front
m1 = re.match('(Admission Date:) (.*) (Discharge Date:) (.*)', segments[0])
if m1:
new_segments += list(map(strip,m1.groups()))
segments = segments[1:]
m2 = re.match('(Date of Birth:) (.*) (Sex:) (.*)' , segments[0])
if m2:
new_segments += list(map(strip,m2.groups()))
segments = segments[1:]
for segment in segments:
# find all section headers
possible_headers = re.findall('\n([A-Z][^\n:]+:)', '\n'+segment)
#assert len(possible_headers) < 2, str(possible_headers)
headers = []
for h in possible_headers:
#print('cand=[%s]' % h
if is_title(h.strip()):
#print('\tYES=[%s]' % h
headers.append(h.strip())
# split text into new segments, delimiting on these headers
for h in headers:
h = h.strip()
# split this segment into 3 smaller segments
ind = segment.index(h)
prefix = segment[:ind].strip()
rest = segment[ ind+len(h):].strip()
# add the prefix (potentially empty)
if len(prefix) > 0:
new_segments.append(prefix.strip())
# add the header
new_segments.append(h)
# remove the prefix from processing (very unlikely to be empty)
segment = rest.strip()
# add the final piece (aka what comes after all headers are processed)
if len(segment) > 0:
new_segments.append(segment.strip())
# copy over the new list of segments (further segmented than original segments)
segments = list(new_segments)
new_segments = []
### Low-hanging fruit: "_____" is a delimiter
for segment in segments:
subsections = segment.split('\n_____\n')
new_segments.append(subsections[0])
for ss in subsections[1:]:
new_segments.append('_____')
new_segments.append(ss)
segments = list(new_segments)
new_segments = []
### Low-hanging fruit: "-----" is a delimiter
for segment in segments:
subsections = segment.split('\n-----\n')
new_segments.append(subsections[0])
for ss in subsections[1:]:
new_segments.append('-----')
new_segments.append(ss)
segments = list(new_segments)
new_segments = []
'''
for segment in segments:
print('------------START------------')
print(segment)
print('-------------END-------------')
print
exit()
'''
### Separate enumerated lists ###
for segment in segments:
if not re.search('\n\s*\d+\.', '\n'+segment):
new_segments.append(segment)
continue
'''
print('------------START------------')
print(segment)
print('-------------END-------------')
print
'''
# generalizes in case the list STARTS this section
segment = '\n'+segment
# determine whether this segment contains a bulleted list (assumes i,i+1,...,n)
start = int(re.search('\n\s*(\d+)\.', '\n'+segment).groups()[0])
n = start
while re.search('\n\s*%d.'%n,segment):
n += 1
n -= 1
# no bulleted list
if n < 1:
new_segments.append(segment)
continue
'''
print('------------START------------')
print(segment)
print('-------------END-------------')
print(start,n)
print
'''
# break each list into its own line
# challenge: not clear how to tell when the list ends if more text happens next
for i in range(start,n+1):
matching_text = re.search('(\n\s*\d+\.)',segment)
if matching_text:
matching_text = matching_text.groups()[0]
prefix = segment[:segment.index(matching_text) ].strip()
segment = segment[ segment.index(matching_text):].strip()
else:
continue
if len(prefix)>0:
new_segments.append(prefix)
if len(segment)>0:
new_segments.append(segment)
segments = list(new_segments)
new_segments = []
'''
TODO: Big Challenge
There is so much variation in what makes a list. Intuitively, I can tell it's a
list because it shows repeated structure (often following a header)
Examples of some lists (with numbers & symptoms changed around to noise)
Past Medical History:
-- Hyperlipidemia
-- lactose intolerance
-- Hypertension
Physical Exam:
Vitals - T 82.2 BP 123/23 HR 73 R 21 75% on 2L NC
General - well appearing male, sitting up in chair in NAD
Neck - supple, JVP elevated to angle of jaw
CV - distant heart sounds, RRR, faint __PHI_43__ murmur at
Labs:
__PHI_10__ 12:00PM BLOOD WBC-8.8 RBC-8.88* Hgb-88.8* Hct-88.8*
MCV-88 MCH-88.8 MCHC-88.8 RDW-88.8* Plt Ct-888
__PHI_14__ 04:54AM BLOOD WBC-8.8 RBC-8.88* Hgb-88.8* Hct-88.8*
MCV-88 MCH-88.8 MCHC-88.8 RDW-88.8* Plt Ct-888
__PHI_23__ 03:33AM BLOOD WBC-8.8 RBC-8.88* Hgb-88.8* Hct-88.8*
MCV-88 MCH-88.8 MCHC-88.8 RDW-88.8* Plt Ct-888
__PHI_109__ 03:06AM BLOOD WBC-8.8 RBC-8.88* Hgb-88.8* Hct-88.8*
MCV-88 MCH-88.8 MCHC-88.8 RDW-88.8* Plt Ct-888
__PHI_1__ 05:09AM BLOOD WBC-8.8 RBC-8.88* Hgb-88.8* Hct-88.8*
MCV-88 MCH-88.8 MCHC-88.8 RDW-88.8* Plt Ct-888
__PHI_26__ 04:53AM BLOOD WBC-8.8 RBC-8.88* Hgb-88.8* Hct-88.8*
MCV-88 MCH-88.8 MCHC-88.8 RDW-88.8* Plt Ct-888
__PHI_301__ 05:30AM BLOOD WBC-8.8 RBC-8.88* Hgb-88.8* Hct-88.8*
MCV-88 MCH-88.8 MCHC-88.8 RDW-88.8* Plt Ct-888
Medications on Admission:
Allopurinol 100 mg DAILY
Aspirin 250 mg DAILY
Atorvastatin 10 mg DAILY
Glimepiride 1 mg once a week.
Hexavitamin DAILY
Lasix 50mg M-W-F; 60mg T-Th-Sat-Sun
Metoprolol 12.5mg TID
Prilosec OTC 20 mg once a day
Verapamil 120 mg SR DAILY
'''
### Remove lines with inline titles from larger segments (clearly nonprose)
for segment in segments:
'''
With: __PHI_6__, MD __PHI_5__
Building: De __PHI_45__ Building (__PHI_32__ Complex) __PHI_87__
Campus: WEST
'''
lines = segment.split('\n')
buf = []
for i in range(len(lines)):
if is_inline_title(lines[i]):
if len(buf) > 0:
new_segments.append('\n'.join(buf))
buf = []
buf.append(lines[i])
if len(buf) > 0:
new_segments.append('\n'.join(buf))
segments = list(new_segments)
new_segments = []
# Going to put one-liner answers with their sections
# (aka A A' B B' C D D' --> AA' BB' C DD' )
N = len(segments)
for i in range(len(segments)):
# avoid segfaults
if i==0:
new_segments.append(segments[i])
continue
if segments[i].count('\n') == 0 and \
is_title(segments[i-1]) and \
not is_title(segments[i ]):
if (i == N-1) or is_title(segments[i+1]):
new_segments = new_segments[:-1]
new_segments.append(segments[i-1] + ' ' + segments[i])
else:
new_segments.append(segments[i].replace("\n", " "))
segments = list(new_segments)
new_segments = []
'''
Should do some kind of regex to find "TEST: value" in segments?
Indication: Source of embolism.
BP (mm Hg): 145/89
HR (bpm): 80
Note: I made a temporary hack that fixes this particular problem.
We'll see how it shakes out
'''
'''
Separate ALL CAPS lines (Warning... is there ever prose that can be all caps?)
'''
'''
for segment in segments:
print('------------START------------')
print(segment)
print('-------------END-------------')
print
exit()
'''
return segments
def strip(s):
return s.strip()
def is_inline_title(text):
m = re.search('^([a-zA-Z ]+:) ', text)
if not m:
return False
return is_title(m.groups()[0])
def remove_full_stops_and_commas(text):
text = text.replace(',', '')
if text.endswith('.'):
return text[:-1] # Return text without the last character (full stop)
return text
def drop_leading_character(sentence, regex_patterns):
"""
Drop leading characters from a sentence based on regex patterns.
This function takes a sentence and a list of regular expression (regex) patterns. It iterates over the regex patterns, and for each
pattern, it drops the leading character from the sentence if there is a match. The loop continues until no more matches are found
for any of the patterns. The resulting sentence is then stripped of leading and trailing whitespaces.
Parameters:
sentence (str): The input sentence from which leading characters will be dropped.
regex_patterns (list): A list of regular expression patterns to match against the leading characters.
Returns:
str: The sentence with leading characters dropped.
Example:
sentence = "A. This is a sample sentence."
regex_patterns = [r"^[A-Z]\.", r"^\d+\."]
drop_leading_character(sentence, regex_patterns)
# Output: "This is a sample sentence."
"""
for pattern in regex_patterns:
while True:
match = re.match(pattern, sentence)
if match:
# Drop the leading character by substituting it with an empty string,
# but only replace the first occurrence
sentence = re.sub(pattern, '', sentence, count=1).strip()
else:
# If no more matches found, exit the loop
break
return sentence.strip()
stopwords = set(['of', 'on', 'or'])
def is_title(text):
if not text.endswith(':'):
return False
text = text[:-1]
# be a little loose here... can tighten if it causes errors
text = re.sub('(\([^\)]*?\))', '', text)
# Are all non-stopwords capitalized?
for word in text.split():
if word in stopwords: continue
if not word[0].isupper():
return False
# I noticed this is a common issue (non-title aapears at beginning of line)
if text == 'Disp':
return False
# optionally: could assert that it is less than 6 tokens
return True
def main():
'''
# read text file from command line
if len(sys.argv) != 2:
print(>>sys.stderr, '\n\tusage: python %s <txt-file>\n' % sys.argv[0]
exit(1)
mimic_note_file = sys.argv[1]
with open(mimic_note_file, 'r') as f:
text = f.read()
# tokenize
sents = tokenize_clinical_note(text)
'''
for mimic_note_file in sys.argv[1:]:
print(mimic_note_file)
with open(mimic_note_file, 'r') as f:
text = f.read()
# tokenize
sents = tokenize_clinical_note(text)
for sent in sents:
print('-'*40)
print(sent)
print('='*40)
print('\n\n')
if __name__ == '__main__':
main()