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): 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") 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["identifier"] = "" for idx, row in df.iterrows(): df['identifier'][idx] = row["deviceId"] + ", " + row["sol"] + ", " + row["wavelength"] df_max = df.groupby(['deviceId', "sol"]).max() df_max['identifier'] = "" df_min = df.groupby(['deviceId', "sol"]).min() df_min['identifier'] = "" for group, row in df_max.iterrows(): df_max["identifier"][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["identifier"][group] = group[0] + ", " + group[1] + ", " + row["wavelength"] # TODO: combine 427 mean and 555 mean using df.groupby(['identifier'])[["Abs 427", "Abs555"]].mean() df_427nm_mean = df.groupby(['identifier'])["Abs 427"].mean().to_frame() df_427nm_mean.rename(columns={'Abs 427': "Abs427_mean"}, inplace = True) df_555nm_mean = df.groupby(['identifier'])["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="identifier") df_describe = df.groupby(['deviceId', "sol"]).describe() df_describe.to_excel(writer, sheet_name="describe") df_max_min = df_max.merge(df_min, on="identifier") df_max_min_mean = df_max_min.merge(df_mean, on="identifier") df_max_min_mean_ref = df_max_min_mean.merge(df_ref, on="identifier") 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', "identifier", "precision_427nm", "precision_555nm", "accuracy_427nm", "accuracy_555nm"]].apply(lambda x: x) 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() == 'Windows': path_delim = '\\' else: path_delim = '/' report_file = rootdir + path_delim + 'report.xlsx' print(report_file) perform_calculations(rootdir, path_delim, report_file)