[2d4573]: / ehrkit / classes.py

Download this file

215 lines (178 with data), 6.7 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
"""Class definitions used in ehrkit.py"""
from datetime import date
class Patient:
"""Patient with biographical and medical history data
Attributes:
id (int): unique patient ID
sex (str): patient sex ('M' or 'F')
dob (datetime): date of birth
dod (datetime): date of death
alive (bool): True if patient alive, else False
"""
def __init__(self, data):
# Biographical Data:
if "id" in data: #int
self.id = data["id"] #should we use pid in case of conflict with python keyword?
else:
self.id = None
if "sex" in data:
self.sex = data["sex"]
else:
self.sex = None
if "dob" in data:
self.dob = data["dob"]
else:
self.dob = None
if "dod" in data:
self.dod = data["dod"]
else:
self.dod = None
if "alive" in data:
self.alive = data["alive"]
else:
self.alive = None
### MEDICAL DATA: ###
# Current patient diagnoses (Array of diagnosis objects)
if "diagnosis" in data:
self.diagnosis = data["diagnosis"]
else:
self.diagnosis = None
# Prescriptions patient is currently taking (Array of prescription objects)
if "current_prescriptions" in data:
self.prescriptions = data["current_prescriptions"]
else:
self.prescriptions = None
# Primary care provider (Foreign Key on Provider object):
if "primary_provider" in data:
self.primary_provider = data["primary_provider"]
else:
self.primary_provider = None
### NOTEEVENTS data(list of nltk separated sentences):###
if "Text" in data:
self.note_events = data["Text"]
else:
self.note_events = None
### MEDICAL HISTORY: ###
# 1.) Past prescriptions (Array of prescription objects)
if "past_prescriptions" in data:
self.prescriptHistory = data["past_prescriptions"]
else:
self.prescriptHistory = None
# 2.) Past diagnoses (Array of diagnosis objects)
if "past_diagnoses" in data:
self.diagHistory = data["past_diagnoses"]
else:
self.diagHistory = None
# 3) Procedures (Array of procedure objects)
if "procedures" in data:
self.procedures = data["procedures"]
else:
self.procedures = None
def addNE(self, textlist):
"""List of all note events for patient, in tuple format (docID, content).
"""
self.note_events = textlist
def addPrescriptions(self, textlist):
self.prescriptions = textlist
def diagnose(self, disease, start = None, end = None):
"""Adds diagnosis to Patient history:"""
data = {"name": disease, "id": None, "start": start, "end": end}
newDiag = Diagnosis(data)
if not self.diagnosis: self.diagnosis = []
self.diagnosis.append(newDiag)
def add_procedure(self, procedure):
"""Adds procedure to Patient history"""
data = {"name": procedure}
new_proc = Procedure(data)
if not self.procedures: self.procedures = []
self.procedures.append(new_proc)
def age(self):
"""Compute patient age using birthdate"""
today = date.today()
birthdate = self.dob
return today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
# Disease Object
class Disease:
def __init__(self, data):
self.name = data["name"]
# Foreign key for diagnosis object
self.id = data["id"]
# Symptoms — array of strings
self.symptoms = data["symptoms"]
self.abbreviation = data["abbreviations"]
# Diagnosis Object
# - Foreign Key on Disease Object
class Diagnosis:
def __init__(self, data):
# Foreign key on disease object
self.name = data["name"]
# Date of diagnosis
self.start = data["start"]
# End date, if patient cured:
self.end = data["end"]
# Prescription object
class Prescription:
def __init__(self, data):
# Generic or Brand Name or Compound Name:
self.name = data["name"]
# Is there a standardized drug ID?
# This is important b/c patient could have
# been prescribed a generic
self.id = data["id"]
#Foreign key on patient object
self.patient_id = data["patient_id"]
self.indication = data["indication"]
# How often medicine is taken:
self.freq = data["frequency"]
#Unit of time for frequency (daily, weekly, monthly)
self.freq = data["frequency_unit"]
# Numerical quantity of dose — float
self.dosage = data["dosage"]
# Units of dosage (e.g. mg, ml, etc.) — str
self.units = data["dosage_units"]
self.start = data["start_date"]
self.end = data["end_date"]
# Not sure if need to record drug manufacturer — str
self.manufacturer = data["manufacturer"]
# Foreign key on Provider() object for prescriber:
self.prescriber_id = data["prescriber_id"]
# Length of prescription in days:
def duration(self):
if self.start == None and self.end == None:
return None
if self.end == None:
end = date.today()
else:
end = self.end
duration = end - self.start
return duration.days
# Unsure about this class; is it too generic for our data?
class Procedure:
def __init__(self, data):
# Can be a foreign key on charge list,
# enabling us to capture procedure cost data
self.name = data["name"]
# Medical facility class:
class Facility:
def __init__(self, data):
# Unique facility ID:
self.id = data["id"]
self.name = data["name"]
self.state = data["state"]
self.city = data["city"]
self.address = data["address"]
# For instance "hospital", "out-patient clinic", etc.
self.type = data["type"]
# Healthcare provider class:
class Provider:
def __init__(self, data):
# Unique physician ID:
self.id = data["id"]
# Primary facility for physician, foreign key on Facility objects
self.facility_id = data["facility_id"]
self.first_name = data["first_name"]
self.last_name = data["last_name"]
# Provider type: RN, EMT, MD, etc.
self.type = data["type"]
# Provider title:
self.title = data["title"]