-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparserHospitalizations.py
522 lines (399 loc) · 20.1 KB
/
parserHospitalizations.py
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
from datetime import datetime, timedelta
from classes import Event, EventType
from variablesFiles import *
from auxFunctionsNormDate import *
from alphabets import abcCap
from dateNormalizer import isLeapyear
from durationCalculator import *
########################################
# AUXILIARY CLASS FOR HOSPITALIZATIONS #
########################################
class HospitalizationAux:
def __init__(self, id, episode, start, end):
self.id = id
self.start = start
self.end = end
self.episode = episode
self.location = None # ID is saved
self.service = None # Name is saved
#############################################
# READING INPUT FILES AS HospitalizationAux #
#############################################
def parseHospitalization_InBed(dicHospsAux, dicEpisodes, minYearN, maxYearN, minYear, maxYear): # This function must be executed before "parseHospitalization_InService()"
nameFile = setFileOutput(filesName["ev_hosp_loc"], minYearN, maxYearN)
with open(nameFile) as file:
lastHosp = None
lastHospEnd = None
for lineString in file:
lineString = str(lineString)
if lineString[0].isnumeric():
line = lineString.split(',')
start_str = line[6]
start_hosp = datetime.strptime(start_str, '%Y-%m-%d %H:%M:%S')
end_str = line[7]
end_str = end_str[:-1]
end = datetime.strptime(end_str, '%Y-%m-%d %H:%M:%S')
idEp = int(line[2])
if start_hosp.year >= minYear and end.year <= maxYear and idEp in dicEpisodes:
idWard = int(line[5])
# It may happen that the first hospitalization does not begin at the same time as the episode to which it belongs -> We keep the Minor date
start_ep = dicEpisodes[idEp].start
if lastHosp is not None:
if lastHosp.id != idEp:
if start_ep <= start_hosp:
start = start_ep
else:
start = start_hosp
dicEpisodes[idEp].start = start
else:
start = start_hosp
else:
if start_ep <= start_hosp:
start = start_ep
else:
start = start_hosp
dicEpisodes[idEp].start = start
# It may happen that the last hospitalization does not end at the same time as the episode to which it belongs -> We keep the older date
if lastHosp is not None:
if lastHosp.id != idEp:
lastEpEnd = dicEpisodes[lastHosp.id].end
if lastEpEnd >= lastHospEnd:
lastHosp.end = lastEpEnd
else:
lastHosp.end = lastHospEnd
dicEpisodes[lastHosp.id].end = lastHospEnd
hosp = HospitalizationAux(idEp, idEp, start, end)
hosp.location = idWard
lastHosp = hosp
lastHospEnd = end
if idEp in dicHospsAux:
dicHospsAux[idEp].append(hosp)
else:
dicHospsAux[idEp] = []
dicHospsAux[idEp].append(hosp)
lastHosp.end = dicEpisodes[lastHosp.id].end
file.close()
def parseHospitalization_InService(dicHospsAux, dicEpisodes, minYearN, maxYearN, minYear):
nameFile = setFileOutput(filesName["ev_hosp_serv"], minYearN, maxYearN)
with open(nameFile) as file:
lastHosp = None
for lineString in file:
lineString = str(lineString)
if lineString[0].isnumeric():
line = lineString.split(',')
start_str = line[3]
start_hosp = datetime.strptime(start_str, '%Y-%m-%d %H:%M:%S')
idEp = int(line[2])
if start_hosp.year >= minYear and idEp in dicEpisodes:
nameService = line[4]
nameService = nameService[:-1]
# It may happen that the first hospitalization does not begin at the same time as the episode to which it belongs -> We keep the Minor date
start_ep = dicEpisodes[idEp].start
if lastHosp is not None:
if lastHosp.id != idEp:
if start_ep <= start_hosp:
start = start_ep
else:
start = start_hosp
dicEpisodes[idEp].start = start
if idEp in dicHospsAux:
dicHospsAux[idEp][0].start = start
else:
if start_ep <= start_hosp:
start = start_ep
else:
start = start_hosp
dicEpisodes[idEp].start = start
if idEp in dicHospsAux:
dicHospsAux[idEp][0].start = start
hosp = HospitalizationAux(idEp, idEp, start, None)
hosp.service = nameService
if lastHosp is not None:
if lastHosp.episode == idEp:
lastHosp.end = start
else:
lastHosp.end = dicEpisodes[lastHosp.episode].end
if idEp in dicHospsAux:
dicHospsAux[idEp].append(hosp)
else:
dicHospsAux[idEp] = []
dicHospsAux[idEp].append(hosp)
lastHosp = hosp
lastHosp.end = dicEpisodes[lastHosp.episode].end
file.close()
#########################################
# POST-PROCESSING OF HospitalizationAux #
#########################################
def deleteEpisodesWithout_LocsOrServs(dicEpisodes, dicHospsAux, dicPatients):
for key in dicHospsAux.copy():
nLocs = 0
nServs = 0
ep = dicHospsAux[key]
for h in ep:
if h.location is not None:
nLocs += 1
elif h.service is not None:
nServs += 1
# Episodes are removed
if nLocs == 0 or nServs == 0:
# Their Patients are removed
patId = dicEpisodes[key].patient
pat = dicPatients[patId]
i = 0
continuing = True
while i<len(pat.episodes) and continuing:
epAux = pat.episodes[i]
if epAux.id == key:
continuing = False
else:
i += 1
dicPatients[patId].episodes.pop(i)
# They are removed from the dictionaries
dicEpisodes.pop(key)
dicHospsAux.pop(key)
def unionHospLocSameLocation(dicHospsAux):
for keyEp,ep in dicHospsAux.items():
listRes = []
hospBefore = None
for hosp in ep:
if hospBefore is not None:
if hosp.location is not None:
if hosp.location == hospBefore.location:
lenListaRes = len(listRes)
listRes[lenListaRes-1].end = hosp.end
else:
listRes.append(hosp)
else:
listRes.append(hosp)
else:
listRes.append(hosp)
hospBefore = hosp
dicHospsAux[keyEp] = listRes
def unionHospLocSameService(dicHospsAux):
for keyEp,ep in dicHospsAux.items():
listRes = []
hospBefore = None
for hosp in ep:
if hospBefore is not None:
if hosp.service is not None:
if hosp.service == hospBefore.service:
lenListaRes = len(listRes)
listRes[lenListaRes-1].end = hosp.end
else:
listRes.append(hosp)
else:
listRes.append(hosp)
else:
listRes.append(hosp)
hospBefore = hosp
dicHospsAux[keyEp] = listRes
######################################
# CREATION OF HOSPITALIZATION EVENTS #
######################################
def getIdHosp(idSimple, indLetter):
indLetter += 1
if indLetter >= len(abcCap):
indAux = indLetter%len(abcCap)
nExtraTimes = indLetter // len(abcCap)
letras = "{}".format(abcCap[indAux])
for i in range(nExtraTimes):
letras += "{}".format(abcCap[indAux])
idHosp = "{}{}".format(idSimple, letras)
else:
idHosp = "{}{}".format(idSimple, abcCap[indLetter])
return idHosp, indLetter
def createHospitalizations(dicHospsAux, dicEpisodes):
for ep in dicHospsAux.values():
hospsLoc = []
hospsServ = []
for h in ep:
if h.location is not None:
hospsLoc.append(h)
elif h.service is not None:
hospsServ.append(h)
episode = dicEpisodes[hospsLoc[0].id]
indLetter = -1
i = 0
for hl in hospsLoc:
continuar = True
while continuar and i<len(hospsServ):
hs = hospsServ[i]
if hl.start == hs.start:
if hl.end == hs.end or hl.end < hs.end:
# Create Hospitalization that starts and ends with hl.start and hl.endend
idHosp, indLetter = getIdHosp(hl.id, indLetter)
hosp = Event(idHosp, episode.description, hl.start, hl.end, hl.location, hs.service, episode.id, EventType.Hospitalization)
dicEpisodes[hl.id].events.append(hosp)
else: #hl.end > hs.end:
# Create Hospitalization that starts and ends with hs.start and hs.end
idHosp, indLetter = getIdHosp(hl.id, indLetter)
hosp = Event(idHosp, episode.description, hs.start, hs.end, hl.location, hs.service, episode.id, EventType.Hospitalization)
dicEpisodes[hl.id].events.append(hosp)
elif hl.start < hs.start:
if hl.end == hs.end or hl.end < hs.end:
# Create Hospitalization that starts and ends with hs.start and hl.end
idHosp, indLetter = getIdHosp(hl.id, indLetter)
hosp = Event(idHosp, episode.description, hs.start, hl.end, hl.location, hs.service, episode.id, EventType.Hospitalization)
dicEpisodes[hl.id].events.append(hosp)
else: # hl.end > hs.end
# Create Hospitalization that starts and ends with hs.start and hs.end
idHosp, indLetter = getIdHosp(hl.id, indLetter)
hosp = Event(idHosp, episode.description, hs.start, hs.end, hs.location, hs.service, episode.id, EventType.Hospitalization)
dicEpisodes[hl.id].events.append(hosp)
else: #elif hl.start > hs.start:
if hl.end == hs.end or hl.end < hs.end:
# Create Hospitalization that starts and ends with hl.start and hl.end
idHosp, indLetter = getIdHosp(hl.id, indLetter)
hosp = Event(idHosp, episode.description, hl.start, hl.end, hl.location, hs.service, episode.id, EventType.Hospitalization)
dicEpisodes[hl.id].events.append(hosp)
else: # hl.end > hs.end:
# Create Hospitalization that starts and ends with hl.start and hs.end
idHosp, indLetter = getIdHosp(hl.id, indLetter)
hosp = Event(idHosp, episode.description, hl.start, hs.end, hl.location, hs.service, episode.id, EventType.Hospitalization)
dicEpisodes[hl.id].events.append(hosp)
if hl.end <= hs.end:
continuar = False
if hl.end >= hs.end:
i += 1
##########################################
# POST-PROCESSING HOSPITALIZATION EVENTS #
##########################################
def correctJumpBetweenYears(dicEpisodes):
for keyEp in dicEpisodes.keys():
ep = dicEpisodes[keyEp]
if ep.start > ep.end:
daysSubt = 365
if isLeapyear(ep.start.year):
daysSubt = 366
dicEpisodes[keyEp].start = ep.start - timedelta(days=daysSubt)
for i in range(len(ep.events)):
ev = ep.events[i]
if ev.start > ep.end:
dicEpisodes[keyEp].events[i].start = ev.start - timedelta(days=daysSubt)
elif ev.end > ep.end:
dicEpisodes[keyEp].events[i].end = ev.end - timedelta(days=daysSubt)
def deleteEpEvOutFilterYear(dicEpisodes, dicPatients, minYear, maxYear):
epIdDelete = []
for ep in dicEpisodes.values():
if ep.start.year < minYear or ep.end.year > maxYear:
epIdDelete.append(ep.id)
# Removing Episodes
for epId in epIdDelete:
# Removing its Patient
patId = dicEpisodes[epId].patient
pat = dicPatients[patId]
i = 0
continuar = True
while i<len(pat.episodes) and continuar:
epAux = pat.episodes[i]
if epAux.id == epId:
continuar = False
else:
i += 1
dicPatients[patId].episodes.pop(i)
# Removing from the dictionary
dicEpisodes.pop(epId)
def deleteHospsDurationCero(dicEpisodes):
for keyEp in dicEpisodes.keys():
ep = dicEpisodes[keyEp]
listEvDurationMoreZero = []
for ev in ep.events:
if ev.type is EventType.Hospitalization:
duration_in_m = getDurationInMinutes(ev.start, ev.end)
if duration_in_m != 0:
listEvDurationMoreZero.append(ev)
else:
listEvDurationMoreZero.append(ev)
dicEpisodes[keyEp].events = listEvDurationMoreZero
# The HU are added to the Hospitalizations of each Episode so that:
# - All Hosps within an Episode that have been attended by a Service have the same HU
# - But those from the next Episode must be attended by other HU -> This is a rotation of HUs between Episodes
def changeServiceByUH(dicEpisodes, dicServices):
# Since in 'transfers' file the Services were represented by their Abbreviation, we create this dictionary to go from Abbreviation to Id
dicServsAux = {}
for serv in dicServices.values():
dicServsAux[serv.abrev] = serv.id
# Array to know for each Service (with more than 1 HU) which is the next HU that should be used
servKeys = list(dicServices.keys())
servsUsedIndHU = {}
for keyServ in servKeys:
if len(dicServices[keyServ].hospUnits) > 1:
servsUsedIndHU[keyServ] = 0
# HU are added to the Hospitalizations of each Episode
for keyEp,ep in dicEpisodes.items():
servicesUsed = []
husUsed = []
for i in range(len(ep.events)):
hosp = ep.events[i]
if hosp.type is EventType.Hospitalization:
nameServ = dicEpisodes[keyEp].events[i].service
idServ = dicServsAux[nameServ]
dicEpisodes[keyEp].events[i].service = idServ # The Abbreviation is changed to the Id
if idServ not in servicesUsed:
servicesUsed.append(idServ)
serv = dicServices[idServ]
if len(serv.hospUnits) == 1:
husUsed.append(serv.hospUnits[0])
huId = serv.hospUnits[0]
else:
indHU = servsUsedIndHU[idServ]
huId = serv.hospUnits[indHU]
husUsed.append(huId)
indHU = (indHU+1)%len(serv.hospUnits) # Updated for next Episode
servsUsedIndHU[idServ] = indHU
else:
indServ = servicesUsed.index(idServ)
huId = husUsed[indServ]
dicEpisodes[keyEp].events[i].hospUnit = huId
def renameHospitalizations(dicEpisodes): # TODO: It does not take into account that there may be IDs with double letters, such as 123AA, 123ABA... but this type of ID should not be reached, as it would mean that the same Episode would have more than 26 Hospitalizations
for keyEp in dicEpisodes.keys():
ep = dicEpisodes[keyEp]
countHosp = -1
for i in range(len(ep.events)):
h = ep.events[i]
if h.type is EventType.Hospitalization:
countHosp += 1
letter = h.id[-1:]
if letter != abcCap[countHosp]:
idHosp = h.id[:-1]
idHosp += abcCap[countHosp]
dicEpisodes[keyEp].events[i].id = idHosp
def updateEpisodesToPatients(dicEpisodes, dicPatients):
dicPatEp = {}
for ep in dicEpisodes.values():
idPat = ep.patient
if idPat in dicPatEp:
dicPatEp[idPat].append(ep)
else:
dicPatEp[idPat] = []
dicPatEp[idPat].append(ep)
for idPat in dicPatEp.keys():
dicPatients[idPat].episodes = dicPatEp[idPat]
################
# FUNCION MAIN #
################
def parseHospitalizations(dicEpisodes, dicServices, dicPatients, minYearN, maxYearN, minYear, maxYear):
# Auxiliary hospitalizations
dicHospsAux = {}
parseHospitalization_InBed(dicHospsAux, dicEpisodes, minYearN, maxYearN, minYear, maxYear)
parseHospitalization_InService(dicHospsAux, dicEpisodes, minYearN, maxYearN, minYear)
# There are Episodes that only have Services or Locs
deleteEpisodesWithout_LocsOrServs(dicEpisodes, dicHospsAux, dicPatients)
# There are Hospitalizations that happen in the same Ward than the previous one
#unionHospLocSameLocation(dicHospsAux) # This will be a change of Bed
# There are Hospitalizations that happen in the same Service than the previous one
unionHospLocSameService(dicHospsAux)
# Creation of Hospitalization Events
createHospitalizations(dicHospsAux, dicEpisodes)
# There may be Hospitalizations with duration = 0
deleteHospsDurationCero(dicEpisodes)
# There are Episodes/Events that end "before" their start due to the normalization of the years
correctJumpBetweenYears(dicEpisodes)
# After correcting the years, it may happen that the start or end of the Episodes are outside the defined range
deleteEpEvOutFilterYear(dicEpisodes, dicPatients, minYear, maxYear)
# There may again be Hospitalizations with duration = 0
deleteHospsDurationCero(dicEpisodes)
# What is in hosp.hospUnits are Service ids, not Hospitalization Unit ids
changeServiceByUH(dicEpisodes, dicServices)
# Update the letters of the Hospitalization IDs
renameHospitalizations(dicEpisodes)