add device qc reportinG
This commit is contained in:
122
scripts/device_qc_report.py
Normal file
122
scripts/device_qc_report.py
Normal file
@@ -0,0 +1,122 @@
|
||||
import pandas as pd
|
||||
import openpyxl
|
||||
import xlsxwriter
|
||||
import os
|
||||
import platform
|
||||
import numpy as np
|
||||
pd.options.mode.chained_assignment = None # default='warn'
|
||||
pd.options.display.float_format = '{:.4f}'.format
|
||||
|
||||
precision_tolerance = 0.009
|
||||
accuracy_tolerance = 0.02
|
||||
|
||||
def perform_calculations(curdir, path_delim, report_file):
|
||||
# print(curdir + path_delim + "hemocube_qc_09_08_2023_data.xlsx")
|
||||
df = pd.read_excel(curdir + path_delim + "hemocube_qc_09_08_2023_data.xlsx", sheet_name="qc_data")
|
||||
df_ref = pd.read_excel(curdir + path_delim + "hemocube_qc_09_08_2023_data.xlsx", sheet_name="reference_values")
|
||||
|
||||
# df_ref['device - con'] = ""
|
||||
# for idx, row in df_ref.iterrows():
|
||||
# df_ref["device - con"][idx] = str(row["deviceId"]) + ", " + row["sol"] + ", " + row["wavelength"]
|
||||
print(df_ref)
|
||||
|
||||
writer = pd.ExcelWriter(report_file, engine = 'xlsxwriter')
|
||||
|
||||
# TODO rename led1Average to Abs 427 and led2Average to Abs555
|
||||
df = df.drop(["led1Sample", "deviceRatio", "led1Buffer", "testTime", "led2Sample", "led2Buffer"], axis=1)
|
||||
|
||||
# duplicate columns
|
||||
df["device"] = df["deviceId"]
|
||||
df["device - con"] = ""
|
||||
for idx, row in df.iterrows():
|
||||
df['device - con'][idx] = row["deviceId"] + ", " + row["sol"] + ", " + row["wavelength"]
|
||||
|
||||
# print(df)
|
||||
df_max = df.groupby(['deviceId', "sol"]).max()
|
||||
df_max['device - con'] = ""
|
||||
# print(df_max.iloc[1])
|
||||
|
||||
df_min = df.groupby(['deviceId', "sol"]).min()
|
||||
df_min['device - con'] = ""
|
||||
|
||||
for group, row in df_max.iterrows():
|
||||
df_max["device - con"][group] = group[0] + ", " + group[1] + ", " + row["wavelength"]
|
||||
|
||||
df_max.to_excel(writer, sheet_name="max", index=False)
|
||||
|
||||
for group, row in df_min.iterrows():
|
||||
df_min["device - con"][group] = group[0] + ", " + group[1] + ", " + row["wavelength"]
|
||||
|
||||
# TODO: combine 427 mean and 555 mean using df.groupby(['device - con'])[["Abs 427", "Abs555"]].mean()
|
||||
df_427nm_mean = df.groupby(['device - con'])["Abs 427"].mean().to_frame()
|
||||
df_427nm_mean.rename(columns={'Abs 427': "Abs427_mean"}, inplace = True)
|
||||
|
||||
df_555nm_mean = df.groupby(['device - con'])["Abs555"].mean().to_frame()
|
||||
df_555nm_mean.rename(columns={'Abs555': "Abs555_mean"}, inplace = True)
|
||||
|
||||
# df_mean = df.copy()
|
||||
df_mean = df_427nm_mean.merge(df_555nm_mean, on="device - con")
|
||||
|
||||
df_describe = df.groupby(['deviceId', "sol"]).describe()
|
||||
# print(df.groupby(['deviceId', "sol"]).describe())
|
||||
df_describe.to_excel(writer, sheet_name="describe")
|
||||
|
||||
# for group, row in df_describe.iterrows():
|
||||
# df_mean["Abs555_mean"] = row["Abs 427"]["mean"]
|
||||
# print(df_mean)
|
||||
|
||||
df_max_min = df_max.merge(df_min, on="device - con")
|
||||
df_max_min_mean = df_max_min.merge(df_mean, on="device - con")
|
||||
df_max_min_mean_ref = df_max_min_mean.merge(df_ref, on="device - con")
|
||||
|
||||
df_min.to_excel(writer, sheet_name="min", index=False)
|
||||
df_max_min_mean_ref.to_excel(writer, sheet_name="df_max_min_mean_ref", index=False)
|
||||
|
||||
df_result = df_max_min.copy()
|
||||
df_result["precision_427nm"] = df_max_min["Abs 427_x"] - df_max_min["Abs 427_y"]
|
||||
df_result["precision_555nm"] = df_max_min["Abs555_x"] - df_max_min["Abs555_y"]
|
||||
df_result["accuracy_427nm"] = df_max_min_mean_ref["ref_device_abs"] - df_max_min_mean_ref["Abs427_mean"]
|
||||
df_result["accuracy_555nm"] = df_max_min_mean_ref["ref_device_abs"] - df_max_min_mean_ref["Abs555_mean"]
|
||||
|
||||
df_result = df_result.drop(["Abs 427_x", "Abs555_x", "wavelength_x", "Abs 427_y", "Abs555_y", "wavelength_y", "device_y"], axis=1)
|
||||
df_result = df_result.groupby('device_x')[['device_x', "device - con", "precision_427nm", "precision_555nm", "accuracy_427nm", "accuracy_555nm"]].apply(lambda x: x)
|
||||
# print(df_result)
|
||||
|
||||
# for idx, row in df_result.iterrows():
|
||||
# print(idx, row)
|
||||
# df_result["precision_427nm"][1] = df_max.loc["Abs 427"][1] + df_min["Abs 427"][1]
|
||||
|
||||
df_result.to_excel(writer, sheet_name="precision", index=False)
|
||||
workbook = writer.book
|
||||
worksheet = writer.sheets["precision"]
|
||||
format1 = workbook.add_format({"num_format": "#,##0.00000"})
|
||||
worksheet.set_column(180, 1, 35, format1)
|
||||
|
||||
# Add a header format.
|
||||
header_format = workbook.add_format(
|
||||
{
|
||||
"bold": True,
|
||||
"text_wrap": True,
|
||||
"valign": "top",
|
||||
"fg_color": "#D7E4BC",
|
||||
"border": 1,
|
||||
}
|
||||
)
|
||||
|
||||
# Write the column headers with the defined format.
|
||||
for col_num, value in enumerate(df_result.columns.values):
|
||||
worksheet.write(0, col_num, value, header_format)
|
||||
|
||||
writer.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
rootdir = os.getcwd()
|
||||
path_delim = ''
|
||||
if platform.system() == 'Darwin':
|
||||
path_delim = '/'
|
||||
else:
|
||||
path_delim = '\\'
|
||||
report_file = rootdir + path_delim + 'report.xlsx'
|
||||
print(report_file)
|
||||
|
||||
perform_calculations(rootdir, path_delim, report_file)
|
||||
Reference in New Issue
Block a user