This repository was archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreQC.py
More file actions
346 lines (257 loc) · 10.8 KB
/
CoreQC.py
File metadata and controls
346 lines (257 loc) · 10.8 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import os
import os.path as osp
import numpy as np
import pandas as pd
import re
LOG_NAME = r''
LOG_DIR = r''
class CoreQC:
__VERSION__ = 1.0
WELCOME = f"""
CoreQC
v{__VERSION__}
Quality Check of recall logs.
Developer: Jakub Pitera
______________________________________________________________________
Single mode: Enter path of the .csv log into the prompt below.
Bulk mode: Alternatively, enter the path of the directory.
All logs found inside that directory tree (including the subfolders) will be checked.
CoreQC will proofread logs and notify about any errors or warnings.
Finally, decide if logs should be split into parts.
______________________________________________________________________
"""
def __init__(self, ):
print(CoreQC.WELCOME)
while True:
self.main()
def main(self):
"""Main loop of the tool. Controls the flow. Prepare vars, ask users for input and calls methods."""
# Prepare vars
self.clear_logs()
self.warnings = []
self.errors = []
# Ask user for filepath
filepath = input("Enter path: ")
assert osp.exists(filepath), "Invalid filepath."
# Import settings
self.load_settings()
# Set QC mode- single or bulk
if osp.isfile(filepath):
self.mode = 'single'
self.load_single(filepath)
elif osp.isdir(filepath):
self.mode = 'bulk'
self.load_bulk(filepath)
# Perform QC pipeline on each log
for log in self.logs:
print(f"\n# {osp.relpath(log, filepath)}")
self.run_qc(log)
# Split logs
split = self.ask_split()
if split:
for log in self.logs:
self.split_export(log)
# Save output to .txt
self.save_report()
input("\nFinished.")
def run_qc(self, log):
"""Controls flow of QC pipeline"""
# Prepare variables
self.clear_vars()
# Load log from csv
self.log_df = pd.read_csv(log, encoding_errors='ignore')
# Run QC pipeline
self.clean_log()
self.qc_metadata()
if not self.invalid_testtype:
self.qc_data()
self.qc_depth()
def load_settings(self):
"""Sets settings, templates and valid mnemonics from QC_Settings.xlsx"""
print("\nReading QC_Settings.xlsx")
xl = pd.ExcelFile('QC_Settings.xlsx')
general = xl.parse(sheet_name='GENERAL')
template = xl.parse(sheet_name='TEMPLATE')
mnemonics = {}
for sheet in xl.sheet_names[2:]:
df = xl.parse(sheet_name=sheet)
df['MNEMONIC'] = df['MNEMONIC'].str.upper()
df['UNIT'] = df['UNIT'].str.upper()
if len(df['MNEMONIC'].unique()) != len(df['MNEMONIC']):
self.raise_warning(sheet, 'mnemonic are not unique in QC_Settings.xlsx', type='', message='')
df.drop_duplicates(subset='MNEMONIC', inplace=True)
df.set_index('MNEMONIC', inplace=True)
mnemonics[sheet] = df
self.general = general
self.template = template
self.mnemonics = mnemonics
self.metadata_cols = 7
self.data_col_cap = 10
self.data_cols_segment = 8
self.log_indicator = '.CSV'
del (xl)
def load_single(self, filepath):
"""Loads single log if a filepath to single log has been provided"""
assert filepath.upper().endswith(self.log_indicator), "Not a .csv"
self.logs.add(filepath)
def load_bulk(self, directory):
"""Loads all logs from the directory tree"""
for (root, _, files) in os.walk(directory):
for f in files:
if f.upper().endswith(self.log_indicator):
self.logs.add(os.path.join(root, f))
print(f"\nLoaded {len(self.logs)} logs.")
def clear_logs(self):
self.logs = set()
def clear_vars(self):
self.log_df = None
self.lab_name = None
self.test_type = None
self.invalid_testtype = False
self.sample_type = None
self.test_date = None
def clean_log(self):
"""Perform basic cleaning on the log"""
df = self.log_df.copy()
# Drop empty rows
df.dropna(how='all', inplace=True)
# Clean headers
df.rename(columns=lambda x: str(x).strip().upper(), inplace=True)
# Clean units row
df.iloc[0] = df.iloc[0].str.strip('()[]').str.upper()
self.log_df = df.copy()
@staticmethod
def ask_split():
"""Wraps user input regaridng the split"""
split = ''
while split not in ('y', 'n'):
split = input("\nSplit logs? (y / n) ").lower()
if split == 'y':
split = True
else:
split = False
return split
def qc_metadata(self):
"""Perform QC on metadata"""
df = self.log_df.iloc[:, :self.metadata_cols]
template = self.template
# Print test_type for reference
print(f"CREP_TESTTYPE : {df.loc[1,'CREP_TESTTYPE']}")
# Check metadata mnemonics and units
for i in range(self.metadata_cols):
df_col = df.columns[i]
template_col = template.columns[i]
df_unit = df.iloc[0, i]
template_unit = template.iloc[0, i]
if df_col != template_col:
self.raise_error('', df_col, type='mnemonic')
elif df_unit != template_unit:
self.raise_error(df_col, df_unit, type='unit')
lab_name = df.loc[1, 'CREP_LAB_NAME']
test_type = df.loc[1, 'CREP_TESTTYPE']
sample_type = df.loc[1, 'CREP_SAMPLETYPE']
test_date = df.loc[1, 'CREP_TEST_DATE']
# Check lab name
if lab_name not in self.general['LAB_NAME'].values:
self.raise_warning('CREP_LAB_NAME', lab_name)
# Check test type
if test_type not in self.general['TEST_TYPE'].values:
self.invalid_testtype = True
self.raise_error('CREP_TESTTYPE', test_type)
# Check sample type
if sample_type not in self.general['SAMPLE_TYPE'].values:
self.raise_error('CREP_SAMPLE_TYPE', sample_type)
# Check test date
pattern = ("^\d{2}-(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)-\d{4}$")
flag = re.match(pattern, test_date)
if flag is None:
self.raise_error('CREP_TEST_DATE', test_date)
self.lab_name = lab_name
self.test_type = test_type
self.sample_type = sample_type
self.test_date = test_date
def qc_data(self):
"""Perform QC on data"""
df = self.log_df.iloc[:, self.metadata_cols:]
mnems = self.mnemonics[self.test_type]
# Check if all mnemonics are unique
if not len(df.columns.unique()) == len(df.columns):
self.raise_error('', '', message='MNEMONICS ARE NOT UNIQUE!')
return
for col in df.columns:
colx = re.sub("[0-9.]+", "XXXX", col)
# Check menmonics
if colx not in mnems.index:
self.raise_error(col, '', type='mnemonic')
continue
# Store column units as variables
df_unit = df.loc[0, col]
mnems_unit = mnems.loc[colx, 'UNIT']
# # If QC_Settings.xlsx has duplicated mnemonics
# if isinstance(mnems_unit, pd.Series):
# mnems_unit = mnems_unit.iloc[0]
# print(mnems_unit)
# self.raise_warning(colx, '', message='mnemonic is duplicated in QC_Settings.xlsx')
# Check units
if df_unit != mnems_unit:
self.raise_error(col, df_unit, type='unit')
# Check values range
try:
df_min = float(df.loc[1:, col].min())
df_max = float(df.loc[1:, col].max())
except TypeError:
continue
except ValueError:
continue
if mnems.loc[colx, 'MIN'] is not np.nan \
and df_min < mnems.loc[colx, 'MIN']:
self.raise_warning(col, df_min, type='min value', message='is not in expected range')
if mnems.loc[colx, 'MAX'] is not np.nan \
and df_max > mnems.loc[colx, 'MAX']:
self.raise_warning(col, df_max, type='max value', message='is not in expected range')
def qc_depth(self):
"""Checks for duplicates in DEPTH and increments by 0.00001 if any"""
df = self.log_df.copy()
duplicated = df['DEPTH'].notna() & df['DEPTH'].duplicated()
if duplicated.any():
print(f"Incrementing {duplicated.sum()} duplicated DEPTHs")
while duplicated.any():
df['DEPTH'].loc[duplicated] = df['DEPTH'].loc[duplicated].astype(float) + 0.00001
duplicated = df['DEPTH'].notna() & df['DEPTH'].duplicated()
self.log_df = df.copy()
def split_export(self, log):
"""Splits log into parts."""
# Load log from csv
self.log_df = pd.read_csv(log, encoding_errors='ignore')
df = self.log_df
# Save cleaned file before splitting
df.to_csv(log, index=False)
if df.shape[1] < self.metadata_cols + self.data_col_cap:
return
# Split to parts
i = 1
first_col = self.metadata_cols
total_cols = df.shape[1]
while first_col < total_cols:
last_col = first_col + self.data_cols_segment
savename = f'{log.upper().removesuffix(".CSV")}_{i}.csv'
if last_col < total_cols:
df.iloc[:, np.r_[:self.metadata_cols, first_col:last_col]].to_csv(savename, index=False)
else:
df.iloc[:, np.r_[:self.metadata_cols, first_col:total_cols]].to_csv(savename, index=False)
first_col += self.data_cols_segment
i += 1
def save_report(self):
pass
def raise_warning(self, item, value, type='value', message='is not recognized', sep=' '):
"""Raises warning notification"""
msg = f"WARNING: {item}{sep}{value}{sep}{type}{sep}{message}."
print(msg)
self.warnings.append(msg)
def raise_error(self, item, value, type='value', message='is invalid', sep=' '):
"""Raises error notification"""
msg = f"ERROR: {item}{sep}{value}{sep}{type}{sep}{message}!"
print(msg)
self.errors.append(msg)
if __name__ == "__main__":
qc = CoreQC()