forked from shwetakadupccm/sql_operations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpccm_db_data_to_be_curated_code_sk.py
350 lines (333 loc) · 14.3 KB
/
pccm_db_data_to_be_curated_code_sk.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
import os
import sqlite3
import pandas as pd
import numpy as np
import itertools
import re
import pccm_db_curation.pccm_db_variable_dictonaries_sk as p_dict
from sqlalchemy import create_engine
# folder = 'D:/Shweta/pccm_db'
# file = 'PCCM_BreastCancerDB_2021_02_22.db'
#
# def get_data(folder, file, table_name):
# path_db = os.path.join(folder, file)
# conn = sqlite3.connect(path_db)
# sql_stat = 'SELECT * FROM ' + table_name
# df = pd.read_sql(sql_stat, conn)
# return df
#
# dat = get_data(folder, file, 'patient_information_history')
#
# def get_value_from_key(vocab_dict, value):
# id_pos = [value in value_list for value_list in (vocab_dict.values())]
# key_reqd = list(itertools.compress(vocab_dict.keys(), id_pos))
# return key_reqd
#
# key_reqd = get_value_from_key(physical_activity_dict, 'walked')
#
#
# def cleaned_and_get_key_value(defined_dict_variable, val):
# split_val = val.split()
# lst = []
# for value in split_val:
# cleaned_value = re.sub('[^a-zA-Z]', '', value)
# cleaned_value = cleaned_value.lower()
# key_reqd = get_value_from_key(defined_dict_variable, cleaned_value)
# if key_reqd is not None:
# key_reqd_str = '; '.join(key_reqd)
# lst.append(key_reqd_str)
# while ('' in lst):
# lst.remove('')
# else:
# lst.append(key_reqd)
# return lst
#
# def replace_values_by_dict_keys(defined_dict_variable, df, variable_name):
# variable_values = df[variable_name].str.lower()
# dict_values = variable_values.to_dict()
# changed_values = []
# for val in dict_values.values():
# if val is not None:
# vocab_type = get_value_from_key(defined_dict_variable, val)
# if len(vocab_type) != 0:
# changed_values.append(', '.join([str(elem) for elem in vocab_type]))
# else:
# lst = cleaned_and_get_key_value(defined_dict_variable, val)
# changed_values.append(', '.join([str(elem) for elem in lst]))
# else:
# changed_values.append('data_not_available')
# df[variable_name] = changed_values
# df.replace(to_replace = '', value = 'data_to_be_curated', inplace = True)
# return df, changed_values
#
#
# def curation_of_table(table_dat, curation_cols):
# old_cols = table_dat.columns
# for col in old_cols:
# if col in curation_cols.keys():
# defined_dict = p_dict.column_names_info(col)
# replace_values_by_dict_keys(defined_dict, table_dat, col)
# return table_dat
#
# curation_cols = {'type_physical_activity': 'physical_activity_dict',
# 'diet': 'diet_dict',
# 'menopause_status': 'menopause_status_dict',
# # 'age_at_menopause_yrs': 'age_at_menopause_yrs_dict',
# 'current_breast_cancer_detected_by': 'current_breast_cancer_detected_by_dict',
# 'lb_symptoms': 'rb_lb_symptoms_dict',
# 'rb_symptoms': 'rb_lb_symptoms_dict',
# 'patient_metastasis_symptoms': 'patient_metastasis_symptoms_dict'}
#
# def pccm_db_curation(folder, file):
# path_db = os.path.join(folder, file)
# conn = sqlite3.connect(path_db)
# sql_stat = "SELECT * FROM sqlite_master WHERE TYPE = 'table'"
# tables = pd.read_sql(sql_stat, conn)
# tabs = tables['name']
# table_idx = [0, 4, 5, 15, 18, 20, 23]
# engine = create_engine('sqlite:///D://Shweta//pccm_db//PCCM_BreastCancerDB_2021_02_22.db')
# sqlite_connection = engine.connect()
#
# for tab in tabs[table_idx]:
# table_dat = get_data(folder, file, tab)
# curation_cols = p_dict.curation_cols(tab)
# curated_table = curation_of_table(table_dat, curation_cols)
# sqlite_table = 'curated' + '_' + tab
# curated_table.to_sql(sqlite_table, sqlite_connection, if_exists='fail')
#
# def data_to_be_curated(folder, file):
# path_db = os.path.join(folder, file)
# conn = sqlite3.connect(path_db)
# sql_stat = "SELECT * FROM sqlite_master WHERE TYPE = 'table'"
# tables = pd.read_sql(sql_stat, conn)
# tabs = tables['name']
# table_idx = [25, 26, 27, 28, 29, 30, 31]
#
# writer = pd.ExcelWriter('D:/Shweta/pccm_db/2021_05_30_pccm_db_data_to_be_curated_sk.xlsx',
# engine='xlsxwriter')
#
# for tab in tabs[table_idx]:
# tab_dat = pd.read_sql('SELECT * FROM' + ' ' + tab, conn)
# tab_cols = tab_dat.columns
# # print(tab_cols)
# curation_dat_tab = pd.DataFrame()
# for col in tab_cols:
# col_dat = tab_dat[['file_number', col]]
# # print(col_dat)
# curation_dat = col_dat[col_dat[col] == 'data_to_be_curated']
# # print(curation_dat)
# if curation_dat.empty:
# continue
# curation_dat_tab = pd.concat([curation_dat_tab, curation_dat], axis=1)
# curation_dat_tab.to_excel(writer, sheet_name=tab[0:31], index=False)
# writer.save()
#
# ## get_indexes
# patient_info = get_data(folder, file, 'patient_information_history')
# curated_patient_info = get_data(folder, file, 'curated_patient_information_history')
#
# def get_index_of_error_values(df, value='data_to_be_curated'):
# positions = list()
# result = df.isin([value])
# series_obj = result.any()
# col_names = list(series_obj[series_obj == True].index)
# print(col_names)
# for col in col_names:
# rows = list(result[col][result[col] == True].index)
# for row in rows:
# positions.append((row, col))
# return positions
#
# positions = get_index_of_error_values(curated_patient_info, 'data_to_be_curated')
#
# ##
#
# def get_error_values(old_df, positions_info_lst):
# error_val_info = []
# for positions_info in positions_info_lst:
# index = positions_info[0]
# col_name = positions_info[1]
# col_dat = old_df.loc[:, col_name]
# col_value = col_dat.iloc[index]
# file_number = old_df['file_number']
# file_number_error_val = file_number.iloc[index]
# output_lst = np.append(col_name, col_value)
# final_output_lst = np.append(file_number_error_val, output_lst)
# error_val_info.append(final_output_lst)
# output_df = pd.DataFrame(error_val_info, columns=['file_number', 'variable_name', 'error_value'])
# return output_df
#
#
# df = get_error_values(patient_info, positions)
#
# ##
#
# def data_to_be_curated_df(folder, file):
# path_db = os.path.join(folder, file)
# conn = sqlite3.connect(path_db)
# sql_stat = "SELECT * FROM sqlite_master WHERE TYPE = 'table'"
# tables = pd.read_sql(sql_stat, conn)
# tabs = tables['name']
# table_idx = [25, 26, 27, 28, 29, 30, 31]
#
# writer = pd.ExcelWriter('D:/Shweta/pccm_db/output_df/2021_06_08_pccm_db_data_to_be_curated_sk.xlsx',
# engine='xlsxwriter')
#
# df = pd.DataFrame(columns= ['file_number', 'variable_name', 'error_value', 'table_name'])
# for tab in tabs[table_idx]:
# curated_tab_dat = pd.read_sql('SELECT * FROM' + ' ' + tab, conn)
# tab_name_str = tab[8:]
# old_tab_dat = pd.read_sql('SELECT * FROM' + ' ' + tab_name_str, conn)
# positions = get_index_of_error_values(curated_tab_dat, 'data_to_be_curated')
# error_df = get_error_values(old_tab_dat, positions)
# error_df_shape = error_df.shape
# table_name = pd.Series(np.repeat(tab, error_df_shape[0]))
# final_error_df = pd.concat([error_df, table_name], axis=1)
# final_error_df.columns = ['file_number', 'variable_name', 'error_value', 'table_name']
# df = pd.concat([df, final_error_df])
# df = df.sort_values('file_number')
# error_df.to_excel(writer, sheet_name=tab[0:31], index= False)
# writer.save()
# return df
#
# df = data_to_be_curated_df(folder, file)
# df.to_excel('D:/Shweta/pccm_db/output_df/2021_06_09_pccm_db_data_to_be_curated_info_sk.xlsx', index=False)
#
#
###
class PccmDbCuration:
def __init__(self, folder, file):
self.folder = folder
self.file = file
def get_data(self, table_name):
path_db = os.path.join(self.folder, self.file)
conn = sqlite3.connect(path_db)
sql_stat = 'SELECT * FROM ' + table_name
df = pd.read_sql(sql_stat, conn)
return df
@staticmethod
def get_value_from_key(vocab_dict, value):
id_pos = [value in value_list for value_list in (vocab_dict.values())]
key_reqd = list(itertools.compress(vocab_dict.keys(), id_pos))
return key_reqd
@staticmethod
def cleaned_and_get_key_value(defined_dict_variable, val):
split_val = re.split(';|:|,| ', val)
lst = []
for value in split_val:
cleaned_value = re.sub('[^a-zA-Z]', '', value)
cleaned_value = cleaned_value.lower()
key_reqd = self.get_value_from_key(defined_dict_variable, cleaned_value)
if key_reqd is not None:
key_reqd_str = '; '.join(key_reqd)
lst.append(key_reqd_str)
while ('' in lst):
lst.remove('')
else:
lst.append(key_reqd)
return lst
@staticmethod
def replace_values_by_dict_keys(defined_dict_variable, df, variable_name):
variable_values = df[variable_name].str.lower()
dict_values = variable_values.to_dict()
changed_values = []
for val in dict_values.values():
if val is not None:
vocab_type = self.get_value_from_key(defined_dict_variable, val)
if len(vocab_type) != 0:
changed_values.append(', '.join([str(elem) for elem in vocab_type]))
else:
lst = self.cleaned_and_get_key_value(defined_dict_variable, val)
changed_values.append(', '.join([str(elem) for elem in lst]))
else:
changed_values.append('data_not_available')
df[variable_name] = changed_values
df.replace(to_replace='', value='data_to_be_curated', inplace=True)
return df, changed_values
@staticmethod
def curation_of_table(table_dat, curation_cols):
old_cols = table_dat.columns
for col in old_cols:
if col in curation_cols.keys():
defined_dict = p_dict.column_names_info(col)
self.replace_values_by_dict_keys(defined_dict, table_dat, col)
return table_dat
def pccm_db_curation(self):
path_db = os.path.join(self.folder, self.file)
conn = sqlite3.connect(path_db)
sql_stat = "SELECT * FROM sqlite_master WHERE TYPE = 'table'"
tables = pd.read_sql(sql_stat, conn)
tabs = tables['name']
table_idx = [0, 4, 5, 15, 18, 20, 23]
engine = create_engine('sqlite:///D://Shweta//pccm_db//PCCM_BreastCancerDB_2021_02_22.db')
sqlite_connection = engine.connect()
for tab in tabs[table_idx]:
table_dat = self.get_data(self.folder, self.file, tab)
curation_cols = p_dict.curation_cols(tab)
curated_table = self.curation_of_table(table_dat, curation_cols)
sqlite_table = 'curated' + '_' + tab
curated_table.to_sql(sqlite_table, sqlite_connection, if_exists='fail')
def drop_table(self):
path_db = os.path.join(self.folder, self.file)
conn = sqlite3.connect(path_db)
sql_stat = "SELECT * FROM sqlite_master WHERE TYPE = 'table'"
tables = pd.read_sql(sql_stat, conn)
tabs = tables['name']
table_idx = [0, 4, 5, 15, 18, 20, 23]
for tab in tabs[table_idx]:
tab_name = 'curated' + '_' + tab
print(tab_name)
drop_stat = 'DROP TABLE' + ' ' + tab_name
conn.execute(drop_stat)
@staticmethod
def get_index_of_error_values(df, value='data_to_be_curated'):
positions = list()
result = df.isin([value])
series_obj = result.any()
col_names = list(series_obj[series_obj == True].index)
print(col_names)
for col in col_names:
rows = list(result[col][result[col] == True].index)
for row in rows:
positions.append((row, col))
return positions
@staticmethod
def get_error_values(old_df, positions_info_lst):
error_val_info = []
for positions_info in positions_info_lst:
index = positions_info[0]
col_name = positions_info[1]
col_dat = old_df.loc[:, col_name]
col_value = col_dat.iloc[index]
file_number = old_df['file_number']
file_number_error_val = file_number.iloc[index]
output_lst = np.append(col_name, col_value)
final_output_lst = np.append(file_number_error_val, output_lst)
error_val_info.append(final_output_lst)
output_df = pd.DataFrame(error_val_info, columns=['file_number', 'variable_name', 'error_value'])
return output_df
def data_to_be_curated_df(self, value='data_to_be_curated'):
path_db = os.path.join(self.folder, self.file)
conn = sqlite3.connect(path_db)
sql_stat = "SELECT * FROM sqlite_master WHERE TYPE = 'table'"
tables = pd.read_sql(sql_stat, conn)
tabs = tables['name']
table_idx = [25, 26, 27, 28, 29, 30, 31]
writer = pd.ExcelWriter('D:/Shweta/pccm_db/output_df/2021_06_08_pccm_db_data_to_be_curated_sk.xlsx',
engine='xlsxwriter')
df = pd.DataFrame(columns=['file_number', 'variable_name', 'error_value', 'table_name'])
for tab in tabs[table_idx]:
curated_tab_dat = pd.read_sql('SELECT * FROM' + ' ' + tab, conn)
tab_name_str = tab[8:]
old_tab_dat = pd.read_sql('SELECT * FROM' + ' ' + tab_name_str, conn)
positions = self.get_index_of_error_values(curated_tab_dat, value)
error_df = self.get_error_values(old_tab_dat, positions)
error_df_shape = error_df.shape
table_name = pd.Series(np.repeat(tab, error_df_shape[0]))
final_error_df = pd.concat([error_df, table_name], axis=1)
final_error_df.columns = ['file_number', 'variable_name', 'error_value', 'table_name']
df = pd.concat([df, final_error_df])
df = df.sort_values('file_number')
error_df.to_excel(writer, sheet_name=tab[0:31], index=False)
writer.save()
return df