|
a |
|
b/symptoms_neg_pos_finder.py |
|
|
1 |
import re |
|
|
2 |
import loader |
|
|
3 |
import build_graphs |
|
|
4 |
import extract_data |
|
|
5 |
|
|
|
6 |
prefix = '(neither|nor|no|denies|not|do not think is|has not felt|far less|did not|without|does not|less|-|(-)|neg|neg for|negative|improved|depressed|negative for|no complaints of)' |
|
|
7 |
suffix = '(:[ \t]*N|:[ \t]*n|:[ \t]*ok)' |
|
|
8 |
|
|
|
9 |
def getSymptomsRegexes(): |
|
|
10 |
symptoms_regexes = {} |
|
|
11 |
with open("symptoms_regex_final.txt") as f: |
|
|
12 |
content = f.readlines() |
|
|
13 |
for line in content: |
|
|
14 |
name, regex = line.strip().split(': ', 1) |
|
|
15 |
symptoms_regexes[name] = [re.compile(regex), re.compile(prefix + ' ' + regex), re.compile(regex + suffix)] |
|
|
16 |
return symptoms_regexes |
|
|
17 |
|
|
|
18 |
def main(): |
|
|
19 |
empi = "FAKE_EMPI_385" # testing a single patient |
|
|
20 |
symptoms_regexes = getSymptomsRegexes() |
|
|
21 |
person = loader.get_patient_by_EMPI(empi) |
|
|
22 |
operation_date = build_graphs.get_operation_date(person) |
|
|
23 |
note_types = ['Car', 'Lno'] |
|
|
24 |
person_pos_history = {} |
|
|
25 |
person_neg_history = {} |
|
|
26 |
sec_per_day = 24 * 60 * 60 |
|
|
27 |
for note_type in note_types: |
|
|
28 |
print 'Examining ' + note_type + ' Notes for Patient ' + empi |
|
|
29 |
date_key = extract_data.get_date_key(note_type) |
|
|
30 |
if note_type in person.keys() and date_key != None: |
|
|
31 |
for i in range(len(person[note_type])): |
|
|
32 |
print '\tNote' + str(i) |
|
|
33 |
doc = person[note_type][i] |
|
|
34 |
date = extract_data.parse_date(doc[date_key]) |
|
|
35 |
if date != None: |
|
|
36 |
delta_days = (date - operation_date).total_seconds() / sec_per_day |
|
|
37 |
for sym in symptoms_regexes: |
|
|
38 |
normal, neg_pre, neg_suff = [bool(x.search(doc['free_text'])) for x in symptoms_regexes[sym]] |
|
|
39 |
if neg_pre or neg_suff: |
|
|
40 |
if sym in person_neg_history: |
|
|
41 |
person_neg_history[sym].append(delta_days) |
|
|
42 |
else: |
|
|
43 |
person_neg_history[sym] = [delta_days] |
|
|
44 |
print '\t\tNegative,' + sym + ',' + str(delta_days) |
|
|
45 |
elif normal: |
|
|
46 |
if sym in person_pos_history: |
|
|
47 |
person_pos_history[sym].append(delta_days) |
|
|
48 |
else: |
|
|
49 |
person_pos_history[sym] = [delta_days] |
|
|
50 |
print '\t\tPositive,' + sym + ',' + str(delta_days) |
|
|
51 |
return person_pos_history, person_neg_history |
|
|
52 |
|
|
|
53 |
if __name__ == '__main__': |
|
|
54 |
pos, neg = main() |
|
|
55 |
print pos |