forked from RHIT-CSSE333/project-s1g1-hospital
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataImportRun.py
More file actions
332 lines (274 loc) · 11.3 KB
/
dataImportRun.py
File metadata and controls
332 lines (274 loc) · 11.3 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
from datetime import datetime
import pandas as pd
import pyodbc
class Person_cl:
def __init__(self, fName, minit, lName, dob, id = None):
self.fName = fName
self.lName = lName
self.minit = minit
self.dob = dob
self.id = id
def setID(self, newID):
self.id = newID
def getID(self):
return self.id
class Hospital_cl:
def __init__(self, hName, hAddress, id = None):
self.hName = hName
self.hAddress = hAddress
self.id = id
def getID(self):
return self.id
class Medicine_cl:
def __init__(self, mName, id = None):
self.mName = mName
self.id = id
def getID(self):
return self.id
class Diagnosis_cl:
def __init__(self, dName, id = None):
self.dName = dName
self.id = id
def getID(self):
return self.id
class Symptom_cl:
def __init__(self, sName, id = None):
self.sName = sName
self.id = id
def getID(self):
return self.id
###################################################################################################################################
server = 'golem.csse.rose-hulman.edu'
database = 'Hospital'
username = 'hospitalAdmin'
password = 'Password123'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=' +
server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cnxn.setdecoding(pyodbc.SQL_CHAR, encoding='latin1')
cnxn.setencoding('latin1')
cursor = cnxn.cursor()
file_path = 'dataSource.csv'
df = pd.read_csv(file_path)
def format_date(date_str):
try:
return datetime.strptime(date_str, '%m/%d/%Y').strftime('%Y-%m-%d')
except ValueError:
return None
def yes_no(value):
if value.lower() == 'yes':
return 'true'
elif value.lower() == 'no':
return 'false'
else:
return None
def get_scope_identity(cursor):
cursor.execute("SELECT @@IDENTITY AS ID")
# cursor.execute("SELECT SCOPE_IDENTITY() AS ID;")
result = cursor.fetchone()
if result:
return result[0]
else:
return None
def is_med_valid(value):
if value.lower() == "nothing":
return False
else:
return True
def do_data(df, cursor):
for index, row in df.iterrows():
person_list = []
hospital_list = []
medicine_list = []
diagnosis_list = []
symptom_list = []
pat_fName = row['Patient Name'].split()[0]
pat_minit = None
pat_lName = row['Patient Name'].split()[-1]
pat_dob = format_date(row['Patient DOB'])
if row['Patient Name'].split()[1] != "":
pat_minit = row['Patient Name'].split()[1]
pat_id = None
pro_fName = row['Provider Name'].split()[0]
pro_minit = None
if row['Provider Name'].split()[1] != "":
pro_minit = row['Provider Name'].split()[1]
pro_lName = row['Provider Name'].split()[-1]
pro_id = None
pro_dob = format_date(row['Provider DOB'])
pro_can = yes_no(row['Can Prescribe'])
pro_spec = row['Provider Speciality']
hos_name = row['Hospital Name']
hos_addr = str(row['Hospital Address'])
hos_id = None
med_valid = is_med_valid(str(row['Medicine Name']))
if med_valid:
med_name = str(row['Medicine Name'])
else:
med_name = ''
med_dose = row['Medicine Dose']
med_id = None
diag_name = row['Diagnosis Name']
diag_freq = row['Diagnosis Frequency']
diag_occ = row['Diagnosis Occurance']
diag_id = None
sym_name = row['Symptoms']
sym_id = None
###################################################################################################################################
print("row")
# INSERT TO PAITENT/PERSON
cursor.execute("""
IF NOT EXISTS (SELECT 1 FROM person WHERE FirstName = ? AND LastName = ? AND DOB = ?)
BEGIN
INSERT INTO person (FirstName, LastName, DOB)
VALUES (?, ?, ?)
END
""", pat_fName, pat_lName, pat_dob,
pat_fName, pat_lName, pat_dob)
pat_id = get_scope_identity(cursor)
if pat_id:
person_list.append(Person_cl(pat_fName, pat_lName, pat_dob, pat_id))
else:
for pat in person_list:
if pat.fName == pat_fName and pat.lName == pat_lName and pat_dob == pat.dob:
pat_id = pat.id
#INSERT INTO PAITENT
if pat_id:
cursor.execute("""
IF NOT EXISTS (SELECT 1 FROM patients WHERE id = ?)
BEGIN
INSERT INTO patients (ID) VALUES (?)
END
""", pat_id, pat_id)
###################################################################################################################################
#INSERT INTO PERSON/PROVIDER
cursor.execute("""
IF NOT EXISTS (SELECT 1 FROM person WHERE FirstName = ? AND LastName = ? AND DOB = ?)
BEGIN
INSERT INTO person (FirstName, LastName, DOB)
VALUES (?, ?, ?)
END
""", pro_fName, pro_lName, pro_dob,
pro_fName, pro_lName, pro_dob)
pro_id = get_scope_identity(cursor)
if pro_id:
person_list.append(Person_cl(pro_fName, pro_minit, pro_lName, pro_dob, pro_id))
else:
for pro in person_list:
if pro.fName == pro_fName and pro.lName == pro_lName and pro_dob == pro.dob:
pro_id = pro.id
# INESRT INTO PROVIDER
if pro_id:
cursor.execute("""
IF NOT EXISTS (SELECT 1 FROM providers WHERE ID = ?)
BEGIN
INSERT INTO providers (ID, Speciality, CanPrescribe)
VALUES (?, ?, ?)
END
""", pro_id, pro_id, pro_spec, pro_can)
###################################################################################################################################
# INSERT INTO DIAGNOSIS
cursor.execute("""
IF NOT EXISTS (SELECT 1 FROM diagnosis WHERE Name = ?)
BEGIN
INSERT INTO diagnosis (Name)
VALUES (?)
END
""", diag_name, diag_name)
diag_id = get_scope_identity(cursor)
if diag_id:
diagnosis_list.append(Diagnosis_cl(diag_name, diag_id))
else:
for diag in medicine_list:
if diag.dName == diag_name:
diag_id = diag.id
###################################################################################################################################
# INSERT INTO SYMPTOMS
cursor.execute("""
IF NOT EXISTS (SELECT 1 FROM symptoms WHERE Name = ?)
BEGIN
INSERT INTO symptoms (Name)
VALUES (?)
END
""", sym_name, sym_name)
sym_id = get_scope_identity(cursor)
if sym_id:
symptom_list.append(Symptom_cl(sym_name, sym_id))
else:
for sym in symptom_list:
if sym.sName == sym_name:
sym_id = sym.id
###################################################################################################################################
# INSERT INTO EXHIBITS
if sym_id and pat_id:
cursor.execute("""
IF NOT EXISTS (SELECT 1 FROM exhibits WHERE PatientID = ? AND SymptomID = ?)
BEGIN
INSERT INTO exhibits (PatientID, SymptomID)
VALUES (?, ?)
END
""", pat_id, sym_id, pat_id, sym_id)
###################################################################################################################################
# INSERT INTO HOSPITAL
cursor.execute("""
IF NOT EXISTS (SELECT 1 FROM hospital WHERE Name = ? AND Address = ?)
INSERT INTO hospital (Name, Address) VALUES (?, ?)
""",
hos_name, hos_addr, hos_name, hos_addr)
hos_id = get_scope_identity(cursor)
if hos_id:
hospital_list.append(Hospital_cl(hos_name, str(hos_addr), hos_id))
else:
for hos in hospital_list:
if hos.hName == hos_name:
hos_id = hos_id
###################################################################################################################################
# INSERT INTO TAKESCAREOF
if pro_id and pat_id:
cursor.execute("""
INSERT INTO takesCareOf (ProviderID, PatientID, HospitalID, DateOfVisit)
VALUES (?, ?, ?, ?)
""", pro_id, pat_id, hos_id, diag_occ)
###################################################################################################################################
if med_valid:
# INSERT INTO MEDICINE
cursor.execute("""
IF NOT EXISTS (SELECT 1 FROM medicine WHERE Name = ?)
BEGIN
INSERT INTO medicine (name) VALUES (?)
END
"""
, med_name, med_name)
med_id = get_scope_identity(cursor)
if med_id:
medicine_list.append(Medicine_cl(med_name, med_id))
else:
for med in medicine_list:
if med.mName == med_name:
med_id = med.id
###################################################################################################################################
# INSERT INTO PRESCRIBES
if pat_id and med_valid and pro_id and med_id:
cursor.execute("""
IF NOT EXISTS (SELECT 1 FROM prescribes WHERE PatientID = ? AND MedicineID = ? AND ProviderID = ? AND Dose = ?)
BEGIN
INSERT INTO prescribes (PatientID, MedicineID, ProviderID, Dose)
VALUES (?, ?, ?, ?)
END
""", pat_id, med_id, pro_id, med_dose,
pat_id, med_id, pro_id, med_dose)
###################################################################################################################################
# INSERT INTO HAS
if pat_id and diag_id:
cursor.execute("""
IF NOT EXISTS(SELECT 1 FROM has WHERE PatientID = ? AND DiagnosisID = ?)
BEGIN
INSERT INTO has(PatientID, DiagnosisID, Occurrence, Frequency) VALUES (?, ?, ?, ?)
END
""", pat_id, diag_id, pat_id, diag_id, diag_occ, diag_freq)
# Call the function
do_data(df, cursor)
# Commit and close
cnxn.commit()
cursor.close()
cnxn.close()
###################################################################################################################################