|
a |
|
b/ehrkit/classes.py |
|
|
1 |
"""Class definitions used in ehrkit.py""" |
|
|
2 |
from datetime import date |
|
|
3 |
|
|
|
4 |
class Patient: |
|
|
5 |
"""Patient with biographical and medical history data |
|
|
6 |
|
|
|
7 |
Attributes: |
|
|
8 |
id (int): unique patient ID |
|
|
9 |
sex (str): patient sex ('M' or 'F') |
|
|
10 |
dob (datetime): date of birth |
|
|
11 |
dod (datetime): date of death |
|
|
12 |
alive (bool): True if patient alive, else False |
|
|
13 |
""" |
|
|
14 |
|
|
|
15 |
def __init__(self, data): |
|
|
16 |
# Biographical Data: |
|
|
17 |
if "id" in data: #int |
|
|
18 |
self.id = data["id"] #should we use pid in case of conflict with python keyword? |
|
|
19 |
else: |
|
|
20 |
self.id = None |
|
|
21 |
|
|
|
22 |
if "sex" in data: |
|
|
23 |
self.sex = data["sex"] |
|
|
24 |
else: |
|
|
25 |
self.sex = None |
|
|
26 |
|
|
|
27 |
if "dob" in data: |
|
|
28 |
self.dob = data["dob"] |
|
|
29 |
else: |
|
|
30 |
self.dob = None |
|
|
31 |
|
|
|
32 |
if "dod" in data: |
|
|
33 |
self.dod = data["dod"] |
|
|
34 |
else: |
|
|
35 |
self.dod = None |
|
|
36 |
|
|
|
37 |
if "alive" in data: |
|
|
38 |
self.alive = data["alive"] |
|
|
39 |
else: |
|
|
40 |
self.alive = None |
|
|
41 |
|
|
|
42 |
### MEDICAL DATA: ### |
|
|
43 |
# Current patient diagnoses (Array of diagnosis objects) |
|
|
44 |
if "diagnosis" in data: |
|
|
45 |
self.diagnosis = data["diagnosis"] |
|
|
46 |
else: |
|
|
47 |
self.diagnosis = None |
|
|
48 |
# Prescriptions patient is currently taking (Array of prescription objects) |
|
|
49 |
if "current_prescriptions" in data: |
|
|
50 |
self.prescriptions = data["current_prescriptions"] |
|
|
51 |
else: |
|
|
52 |
self.prescriptions = None |
|
|
53 |
# Primary care provider (Foreign Key on Provider object): |
|
|
54 |
if "primary_provider" in data: |
|
|
55 |
self.primary_provider = data["primary_provider"] |
|
|
56 |
else: |
|
|
57 |
self.primary_provider = None |
|
|
58 |
### NOTEEVENTS data(list of nltk separated sentences):### |
|
|
59 |
if "Text" in data: |
|
|
60 |
self.note_events = data["Text"] |
|
|
61 |
else: |
|
|
62 |
self.note_events = None |
|
|
63 |
|
|
|
64 |
|
|
|
65 |
### MEDICAL HISTORY: ### |
|
|
66 |
# 1.) Past prescriptions (Array of prescription objects) |
|
|
67 |
if "past_prescriptions" in data: |
|
|
68 |
self.prescriptHistory = data["past_prescriptions"] |
|
|
69 |
else: |
|
|
70 |
self.prescriptHistory = None |
|
|
71 |
# 2.) Past diagnoses (Array of diagnosis objects) |
|
|
72 |
if "past_diagnoses" in data: |
|
|
73 |
self.diagHistory = data["past_diagnoses"] |
|
|
74 |
else: |
|
|
75 |
self.diagHistory = None |
|
|
76 |
# 3) Procedures (Array of procedure objects) |
|
|
77 |
if "procedures" in data: |
|
|
78 |
self.procedures = data["procedures"] |
|
|
79 |
else: |
|
|
80 |
self.procedures = None |
|
|
81 |
def addNE(self, textlist): |
|
|
82 |
"""List of all note events for patient, in tuple format (docID, content). |
|
|
83 |
""" |
|
|
84 |
self.note_events = textlist |
|
|
85 |
|
|
|
86 |
def addPrescriptions(self, textlist): |
|
|
87 |
self.prescriptions = textlist |
|
|
88 |
|
|
|
89 |
def diagnose(self, disease, start = None, end = None): |
|
|
90 |
"""Adds diagnosis to Patient history:""" |
|
|
91 |
data = {"name": disease, "id": None, "start": start, "end": end} |
|
|
92 |
newDiag = Diagnosis(data) |
|
|
93 |
if not self.diagnosis: self.diagnosis = [] |
|
|
94 |
self.diagnosis.append(newDiag) |
|
|
95 |
|
|
|
96 |
def add_procedure(self, procedure): |
|
|
97 |
"""Adds procedure to Patient history""" |
|
|
98 |
data = {"name": procedure} |
|
|
99 |
new_proc = Procedure(data) |
|
|
100 |
if not self.procedures: self.procedures = [] |
|
|
101 |
self.procedures.append(new_proc) |
|
|
102 |
|
|
|
103 |
def age(self): |
|
|
104 |
"""Compute patient age using birthdate""" |
|
|
105 |
today = date.today() |
|
|
106 |
birthdate = self.dob |
|
|
107 |
return today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day)) |
|
|
108 |
|
|
|
109 |
|
|
|
110 |
# Disease Object |
|
|
111 |
|
|
|
112 |
class Disease: |
|
|
113 |
|
|
|
114 |
def __init__(self, data): |
|
|
115 |
self.name = data["name"] |
|
|
116 |
# Foreign key for diagnosis object |
|
|
117 |
self.id = data["id"] |
|
|
118 |
# Symptoms — array of strings |
|
|
119 |
self.symptoms = data["symptoms"] |
|
|
120 |
self.abbreviation = data["abbreviations"] |
|
|
121 |
|
|
|
122 |
# Diagnosis Object |
|
|
123 |
# - Foreign Key on Disease Object |
|
|
124 |
|
|
|
125 |
class Diagnosis: |
|
|
126 |
|
|
|
127 |
def __init__(self, data): |
|
|
128 |
# Foreign key on disease object |
|
|
129 |
self.name = data["name"] |
|
|
130 |
# Date of diagnosis |
|
|
131 |
self.start = data["start"] |
|
|
132 |
# End date, if patient cured: |
|
|
133 |
self.end = data["end"] |
|
|
134 |
|
|
|
135 |
# Prescription object |
|
|
136 |
|
|
|
137 |
class Prescription: |
|
|
138 |
|
|
|
139 |
def __init__(self, data): |
|
|
140 |
# Generic or Brand Name or Compound Name: |
|
|
141 |
self.name = data["name"] |
|
|
142 |
# Is there a standardized drug ID? |
|
|
143 |
# This is important b/c patient could have |
|
|
144 |
# been prescribed a generic |
|
|
145 |
self.id = data["id"] |
|
|
146 |
#Foreign key on patient object |
|
|
147 |
self.patient_id = data["patient_id"] |
|
|
148 |
self.indication = data["indication"] |
|
|
149 |
# How often medicine is taken: |
|
|
150 |
self.freq = data["frequency"] |
|
|
151 |
#Unit of time for frequency (daily, weekly, monthly) |
|
|
152 |
self.freq = data["frequency_unit"] |
|
|
153 |
# Numerical quantity of dose — float |
|
|
154 |
self.dosage = data["dosage"] |
|
|
155 |
# Units of dosage (e.g. mg, ml, etc.) — str |
|
|
156 |
self.units = data["dosage_units"] |
|
|
157 |
self.start = data["start_date"] |
|
|
158 |
self.end = data["end_date"] |
|
|
159 |
# Not sure if need to record drug manufacturer — str |
|
|
160 |
self.manufacturer = data["manufacturer"] |
|
|
161 |
# Foreign key on Provider() object for prescriber: |
|
|
162 |
self.prescriber_id = data["prescriber_id"] |
|
|
163 |
|
|
|
164 |
# Length of prescription in days: |
|
|
165 |
def duration(self): |
|
|
166 |
if self.start == None and self.end == None: |
|
|
167 |
return None |
|
|
168 |
|
|
|
169 |
if self.end == None: |
|
|
170 |
end = date.today() |
|
|
171 |
else: |
|
|
172 |
end = self.end |
|
|
173 |
|
|
|
174 |
duration = end - self.start |
|
|
175 |
|
|
|
176 |
return duration.days |
|
|
177 |
|
|
|
178 |
# Unsure about this class; is it too generic for our data? |
|
|
179 |
|
|
|
180 |
class Procedure: |
|
|
181 |
|
|
|
182 |
def __init__(self, data): |
|
|
183 |
# Can be a foreign key on charge list, |
|
|
184 |
# enabling us to capture procedure cost data |
|
|
185 |
self.name = data["name"] |
|
|
186 |
|
|
|
187 |
|
|
|
188 |
# Medical facility class: |
|
|
189 |
class Facility: |
|
|
190 |
|
|
|
191 |
def __init__(self, data): |
|
|
192 |
# Unique facility ID: |
|
|
193 |
self.id = data["id"] |
|
|
194 |
self.name = data["name"] |
|
|
195 |
self.state = data["state"] |
|
|
196 |
self.city = data["city"] |
|
|
197 |
self.address = data["address"] |
|
|
198 |
# For instance "hospital", "out-patient clinic", etc. |
|
|
199 |
self.type = data["type"] |
|
|
200 |
|
|
|
201 |
# Healthcare provider class: |
|
|
202 |
class Provider: |
|
|
203 |
|
|
|
204 |
def __init__(self, data): |
|
|
205 |
# Unique physician ID: |
|
|
206 |
self.id = data["id"] |
|
|
207 |
# Primary facility for physician, foreign key on Facility objects |
|
|
208 |
self.facility_id = data["facility_id"] |
|
|
209 |
self.first_name = data["first_name"] |
|
|
210 |
self.last_name = data["last_name"] |
|
|
211 |
# Provider type: RN, EMT, MD, etc. |
|
|
212 |
self.type = data["type"] |
|
|
213 |
# Provider title: |
|
|
214 |
self.title = data["title"] |