-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAggregate_Raw_v3.py
369 lines (338 loc) · 16.8 KB
/
Aggregate_Raw_v3.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
import os
import pandas as pd
import numpy as np
import data_prep
import data_prep as prep # custom module for file cleaning
import path as PT
import locale
locale.setlocale(locale.LC_TIME, "en_SG") # singapore
# to indicate whether want to save output as csv, along with the default parquet output.
# no CSV output while equal to 0. other value will result in csv output
require_csv = 0
"""
# Step 1 - combine inflight data
# # scan file log
filename_db = 'Combined_inflight.parquet.gzip'
filename_csv = 'Combined_inflight.csv'
filename_log = 'inflight_file_log.xlsx'
df_file_log = pd.read_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
list_file_log = df_file_log['File'].tolist()
# load the existing database, drop prelim data. if DB not found, reset to empty DB, and reset the filelist to empty
try:
df_inflight = pd.read_parquet(PT.path_wip_output + filename_db)
df_inflight = df_inflight.loc[df_inflight['prelim_flag'] != 'Y']
df_inflight.drop_duplicates(inplace=True)
except:
df_inflight = pd.DataFrame()
list_file_log = []
# scan the files in the folder and compare with file log, to extract delta file list for extraction
list_all_files = [file for file in os.listdir(PT.path_inflight_data) if file.__contains__('.zip')]
files = [item for item in list_all_files if item not in list_file_log]
print("Inflight files to merge: ", files, end='\n')
if len(files) != 0:
# extraction for delta files only. Only update the filelist if prelim_flag is N/n
for file in files:
print(file, end='\n')
prelim_flag = data_prep.prelim_flag_enter_validation()
if prelim_flag.upper() != 'Y':
list_file_log.append(file)
# print(list_file_log,end='\n')
current_data = prep.clean_raw_extraction_zip(PT.path_inflight_data, file, PT.path_wip_output, PT.path_lookup)
current_data['prelim_flag'] = prelim_flag.upper()
df_inflight = pd.concat([df_inflight, current_data])
df_inflight.drop_duplicates(inplace=True)
# convert file list to df and write back to the log file
df_file_log = pd.DataFrame(list_file_log, columns=['File'])
df_file_log.to_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
if require_csv != 0:
df_inflight.to_csv(PT.path_wip_output + filename_csv, index=False)
df_inflight.to_parquet(PT.path_wip_output + filename_db, compression='gzip', index=False)
else:
pass
print("Inflight data: ", df_inflight.shape, 'Current data include below extraction files as final:')
print(df_file_log)
print("Step 1 successfully completed - Inflight data done")
# Step 2 - combine discharge data
# scan file log
filename_db = 'Combined_disch.parquet.gzip'
filename_csv = 'Combined_disch.csv'
filename_log = 'disch_file_log.xlsx'
df_file_log = pd.read_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
list_file_log = df_file_log['File'].tolist()
# load the existing database, drop prelim data. if DB not found, reset to empty DB, and reset the filelist to empty
try:
df_dc = pd.read_parquet(PT.path_wip_output + filename_db)
df_dc = df_dc.loc[df_dc['prelim_flag'] != 'Y']
df_dc.drop_duplicates(inplace=True)
except:
df_dc = pd.DataFrame()
list_file_log = []
# scan the files in the folder and compare with file log, to extract delta file list for extraction
list_all_files = [file for file in os.listdir(PT.path_discharge_data) if file.__contains__('.zip')]
files = [item for item in list_all_files if item not in list_file_log]
print("Discharge data files to merge: ", files, end='\n')
if len(files) != 0:
# extraction for delta files only. Only update the filelist if prelim_flag is N/n
for file in files:
print(file, end='\n')
prelim_flag = data_prep.prelim_flag_enter_validation()
if prelim_flag.upper() != 'Y':
list_file_log.append(file)
# print(list_file_log)
current_data = prep.clean_raw_extraction_zip(PT.path_discharge_data, file, PT.path_wip_output, PT.path_lookup)
current_data['prelim_flag'] = prelim_flag.upper()
df_dc = pd.concat([df_dc, current_data])
df_dc.drop_duplicates(inplace=True)
# convert file list to df and write back to the log file
df_file_log = pd.DataFrame(list_file_log, columns=['File'])
df_file_log.to_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
# to avoid the encoding error
df_dc['Postal'] = df_dc['Postal'].str.slice(0, 6)
df_dc['Disch_Type'] = df_dc['Disch_Type'].str.slice(0, 2)
if require_csv != 0:
df_dc.to_csv(PT.path_wip_output + filename_csv, index=False)
df_dc.to_parquet(PT.path_wip_output + filename_db, compression='gzip', index=False)
else:
pass
print("Discharge data: ", df_dc.shape, 'Current data include below extraction files as final:')
print(df_file_log)
print("Step 2 successfully completed - discharge data done")
# Step 3 - combine admission data
# scan file log
filename_db = 'Combined_adm.parquet.gzip'
filename_csv = 'Combined_adm.csv'
filename_log = 'adm_file_log.xlsx'
df_file_log = pd.read_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
list_file_log = df_file_log['File'].tolist()
# load the existing database, drop prelim data. if DB not found, reset to empty DB, and reset the filelist to empty
try:
df_adm = pd.read_parquet(PT.path_wip_output + filename_db)
df_adm = df_adm.loc[df_adm['prelim_flag'] != 'Y']
df_adm.drop_duplicates(inplace=True)
except:
df_adm = pd.DataFrame()
list_file_log = []
# scan the files in the folder and compare with file log, to extract delta file list for extraction
list_all_files = [file for file in os.listdir(PT.path_admission_data) if file.__contains__('.zip')]
files = [item for item in list_all_files if item not in list_file_log]
print("Admission data files to merge: ", files, end='\n')
if len(files) != 0:
# extraction for delta files only. Only update the filelist if prelim_flag is N/n
for file in files:
print(file, end='\n')
prelim_flag = data_prep.prelim_flag_enter_validation()
if prelim_flag.upper() != 'Y':
list_file_log.append(file)
# print(list_file_log)
current_data = prep.clean_raw_extraction_zip(PT.path_admission_data, file, PT.path_wip_output, PT.path_lookup)
current_data['prelim_flag'] = prelim_flag.upper()
df_adm = pd.concat([df_adm, current_data])
df_adm.drop_duplicates(inplace=True)
# convert file list to df and write back to the log file
df_file_log = pd.DataFrame(list_file_log, columns=['File'])
df_file_log.to_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
# to avoid the encoding error
df_adm['Postal_Code'] = df_adm['Postal_Code'].str.slice(0, 6)
if require_csv != 0:
df_adm.to_csv(PT.path_wip_output + filename_csv, index=False)
df_adm.to_parquet(PT.path_wip_output + filename_db, compression='gzip', index=False)
else:
pass
print("admission data: ", df_adm.shape, 'Current data include below extraction files as final:')
print(df_file_log)
print("Step 3 successfully completed - admission data done")
# Step 4 - combine procedure data
# scan file log
filename_db = 'Combined_procedure.parquet.gzip'
filename_csv = 'Combined_procedure.csv'
filename_log = 'procedure_file_log.xlsx'
df_file_log = pd.read_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
list_file_log = df_file_log['File'].tolist()
# load the existing database, drop prelim data. if DB not found, reset to empty DB, and reset the filelist to empty
try:
df_procedure = pd.read_parquet(PT.path_wip_output + filename_db)
df_procedure = df_procedure.loc[df_procedure['prelim_flag'] != 'Y']
df_procedure.drop_duplicates(inplace=True)
except:
df_procedure = pd.DataFrame()
list_file_log = []
# scan the files in the folder and compare with file log, to extract delta file list for extraction
list_all_files = [file for file in os.listdir(PT.path_procedure_data) if file.__contains__('.zip')]
files = [item for item in list_all_files if item not in list_file_log]
print("Procedure data files to merge: ", files, end='\n')
if len(files) != 0:
# extraction for delta files only. Only update the filelist if prelim_flag is N/n
for file in files:
print(file, end='\n')
prelim_flag = data_prep.prelim_flag_enter_validation()
if prelim_flag.upper() != 'Y':
list_file_log.append(file)
# print(list_file_log)
current_data = prep.clean_raw_extraction_zip(PT.path_procedure_data, file, PT.path_wip_output, PT.path_lookup)
current_data['prelim_flag'] = prelim_flag.upper()
df_procedure = pd.concat([df_procedure, current_data])
df_procedure.drop_duplicates(inplace=True)
# convert file list to df and write back to the log file
df_file_log = pd.DataFrame(list_file_log, columns=['File'])
df_file_log.to_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
if require_csv != 0:
df_procedure.to_csv(PT.path_wip_output + filename_csv, index=False)
df_procedure.to_parquet(PT.path_wip_output + filename_db, compression='gzip', index=False)
else:
pass
print("procedure data: ", df_procedure.shape, 'Current data include below extraction files as final:')
print(df_file_log)
print("Step 4 successfully completed - procedure data done")
# # Step 5 - combine UCC data
# scan file log
filename_db = 'Combined_UCC.parquet.gzip'
filename_csv = 'Combined_UCC.csv'
filename_log = 'ucc_file_log.xlsx'
df_file_log = pd.read_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
list_file_log = df_file_log['File'].tolist()
# load the existing database, drop prelim data. if DB not found, reset to empty DB, and reset the filelist to empty
try:
df_UCC = pd.read_parquet(PT.path_wip_output + filename_db)
df_UCC = df_UCC.loc[df_UCC['prelim_flag'] != 'Y']
df_UCC.drop_duplicates(inplace=True)
except:
df_UCC = pd.DataFrame()
list_file_log = []
# scan the files in the folder and compare with file log, to extract delta file list for extraction
list_all_files = [file for file in os.listdir(PT.path_UCC_data) if file.__contains__('.zip')]
files = [item for item in list_all_files if item not in list_file_log]
print("UCC data files to merge: ", files, end='\n')
if len(files) != 0:
# extraction for delta files only. Only update the filelist if prelim_flag is N/n
for file in files:
print(file, end='\n')
prelim_flag = data_prep.prelim_flag_enter_validation()
if prelim_flag.upper() != 'Y':
list_file_log.append(file)
# print(list_file_log)
current_data = prep.clean_raw_extraction_zip(PT.path_UCC_data, file, PT.path_wip_output, PT.path_lookup)
current_data['prelim_flag'] = prelim_flag.upper()
df_UCC = pd.concat([df_UCC, current_data])
df_UCC.drop_duplicates(inplace=True)
# convert file list to df and write back to the log file
df_file_log = pd.DataFrame(list_file_log, columns=['File'])
df_file_log.to_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
# to avoid the encoding error
df_UCC['Case_End_Type_Code'] = df_UCC['Case_End_Type_Code'].str.slice(0, 2)
if require_csv != 0:
df_UCC.to_csv(PT.path_wip_output + filename_csv, index=False)
df_UCC.to_parquet(PT.path_wip_output + filename_db, compression='gzip', index=False)
else:
pass
print("UCC data: ", df_UCC.shape, 'Current data include below extraction files as final:')
print(df_file_log)
print("Step 5 successfully completed - UCC data done")
"""
#
# # # Step 6 - combine SOC datan
# # # scan file log
# filename_db = 'Combined_SOC.parquet.gzip'
# filename_csv = 'Combined_SOC.csv'
# filename_log = 'soc_file_log.xlsx'
# df_file_log = pd.read_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
# list_file_log = df_file_log['File'].tolist()
#
# # load the existing database, drop prelim data. if DB not found, reset to empty DB, and reset the filelist to empty
# try:
# df_SOC = pd.read_parquet(PT.path_wip_output + filename_db)
# df_SOC_clean = df_SOC
# print("Initial shape: ", df_SOC.shape)
# df_SOC = df_SOC.loc[df_SOC['prelim_flag'] != 'Y']
# df_SOC = df_SOC.loc[df_SOC['Status'] != 'P']
#
# except:
# df_SOC = pd.DataFrame()
# list_file_log = []
# # scan the files in the folder and compare with file log, to extract delta file list for extraction
# list_all_files = [file for file in os.listdir(PT.path_SOC_data) if file.__contains__('.zip')]
# files = [item for item in list_all_files if item not in list_file_log]
# print("SOC data files to merge: ", files, end='\n')
#
# if len(files) != 0:
# # extraction for delta files only. Only update the filelist if prelim_flag is N/n
# for file in files:
# print(file, end='\n')
# prelim_flag = data_prep.prelim_flag_enter_validation()
# if prelim_flag.upper() != 'Y':
# list_file_log.append(file)
# print(list_file_log)
# current_data = prep.clean_raw_extraction_zip(PT.path_SOC_data, file, PT.path_wip_output, PT.path_lookup)
# current_data['prelim_flag'] = prelim_flag.upper()
# df_SOC = pd.concat([df_SOC, current_data])
#
# df_SOC.drop_duplicates(subset=['Case_No', 'Visit_Date', 'Visit_Time', 'Visit_No'],
# inplace=True)
# # convert file list to df and write back to the log file
# df_file_log = pd.DataFrame(list_file_log, columns=['File'])
# df_file_log.to_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
#
# # to avoid the encoding error
# df_SOC['Postal_Code'] = df_SOC['Postal_Code'].str.slice(0, 6)
# if require_csv != 0:
# df_SOC.to_csv(PT.path_wip_output + filename_csv, index=False)
# df_SOC.to_parquet(PT.path_wip_output + filename_db, compression='gzip', index=False)
#
# else:
# df_SOC_clean.drop_duplicates(subset=['Case_No', 'Visit_Date', 'Visit_Time', 'Visit_No'],
# inplace=True)
# df_SOC_clean.to_parquet(PT.path_wip_output + filename_db, compression='gzip', index=False)
# print("After clean the DB: ", df_SOC_clean.shape)
# print("SOC data: ", df_SOC.shape, 'Current data include below extraction files as final:')
# print(df_file_log)
# print("Step 6 successfully completed - SOC data done")
#######################################################################
# # Step 7 - combine SOC data with plan
# scan file log
filename_db = 'Combined_SOC_plan.parquet.gzip'
filename_csv = 'Combined_SOC_plan.csv'
filename_log = 'soc_file_log_plan.xlsx'
df_file_log = pd.read_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
list_file_log = df_file_log['File'].tolist()
# load the existing database, drop prelim data. if DB not found, reset to empty DB, and reset the filelist to empty
try:
df_SOC = pd.read_parquet(PT.path_wip_output + filename_db)
df_SOC_clean = df_SOC
print("Initial shape: ", df_SOC.shape)
df_SOC = df_SOC.loc[df_SOC['prelim_flag'] != 'Y']
df_SOC = df_SOC.loc[df_SOC['Status'] != 'P']
except:
df_SOC = pd.DataFrame()
list_file_log = []
# scan the files in the folder and compare with file log, to extract delta file list for extraction
list_all_files = [file for file in os.listdir(PT.path_SOC_data_plan) if file.__contains__('.zip')]
files = [item for item in list_all_files if item not in list_file_log]
print("SOC data files to merge: ", files, end='\n')
if len(files) != 0:
# extraction for delta files only. Only update the filelist if prelim_flag is N/n
for file in files:
print(file, end='\n')
prelim_flag = data_prep.prelim_flag_enter_validation()
if prelim_flag.upper() != 'Y':
list_file_log.append(file)
print(list_file_log)
current_data = prep.clean_raw_extraction_zip(PT.path_SOC_data_plan, file, PT.path_wip_output, PT.path_lookup)
current_data['prelim_flag'] = prelim_flag.upper()
df_SOC = pd.concat([df_SOC, current_data])
df_SOC.drop_duplicates(subset=['Case_No', 'Visit_Date', 'Visit_No'],
inplace=True)
# convert file list to df and write back to the log file
df_file_log = pd.DataFrame(list_file_log, columns=['File'])
df_file_log.to_excel(PT.path_wip_output_log + filename_log, sheet_name="file_log")
# to avoid the encoding error
df_SOC['Postal_Code'] = df_SOC['Postal_Code'].str.slice(0, 6)
if require_csv != 0:
df_SOC.to_csv(PT.path_wip_output + filename_csv, index=False)
df_SOC.to_parquet(PT.path_wip_output + filename_db, compression='gzip', index=False)
else:
df_SOC_clean.drop_duplicates(subset=['Case_No', 'Visit_Date', 'Visit_No'],
inplace=True)
df_SOC_clean.to_parquet(PT.path_wip_output + filename_db, compression='gzip', index=False)
print("After clean the DB: ", df_SOC_clean.shape)
print("SOC data: ", df_SOC.shape, 'Current data include below extraction files as final:')
print(df_file_log)
print("Step 7 successfully completed - SOC plan data done")