add reclassification script
This commit is contained in:
98
scripts/recompute_class_blind.py
Normal file
98
scripts/recompute_class_blind.py
Normal file
@@ -0,0 +1,98 @@
|
||||
import pandas as pd
|
||||
import os
|
||||
import numpy as np
|
||||
import re
|
||||
import math
|
||||
|
||||
curdir = os.getcwd()
|
||||
path_delim = '/'
|
||||
df = pd.read_excel("data/_tmp_data_21_02_2024_17_29.xlsx", sheet_name="data")
|
||||
|
||||
def device_ratio_classification(ratio):
|
||||
try:
|
||||
if ratio is not None:
|
||||
if 0.16 <= ratio <= 0.23:
|
||||
return "Normal"
|
||||
if 0.23 <= ratio <= 0.25:
|
||||
return "Negative Borderline"
|
||||
if 0.25 <= ratio <= 0.31:
|
||||
return "Sickle Cell Trait"
|
||||
if 0.31 <= ratio <= 0.36:
|
||||
return "Positive for Sickle Cell. HPLC for Confirmation"
|
||||
if 0.36 <= ratio <= 0.7:
|
||||
return "Sickle Cell Disease"
|
||||
else:
|
||||
return "Invalid"
|
||||
except Exception as e:
|
||||
return "Error"
|
||||
return "Invalid"
|
||||
|
||||
def find_result_with_borderline_method_1(device_ratio, device_ratio_class, borderline_metric):
|
||||
try:
|
||||
if device_ratio is not None and borderline_metric is not None:
|
||||
if device_ratio_class == "Negative Borderline":
|
||||
return "Borderline. Normal" if borderline_metric >= 2.4 else "Borderline. Sickle Cell Trait"
|
||||
elif device_ratio_class == "Positive for Sickle Cell. HPLC for Confirmation":
|
||||
return "Borderline. Sickle Cell Trait" if borderline_metric >= 1.34 else "Borderline. Sickle Cell Disease"
|
||||
except Exception as e:
|
||||
handle_exception(e)
|
||||
return "Error"
|
||||
return str(device_ratio_class)
|
||||
|
||||
def device_ratio_borderline_thresholds_br_method_2(ratio):
|
||||
try:
|
||||
if ratio is not None:
|
||||
if 0.11 <= ratio <= 0.237:
|
||||
return "Normal"
|
||||
if 0.237 <= ratio <= 0.242:
|
||||
return "Negative Borderline"
|
||||
if 0.242 <= ratio <= 0.318:
|
||||
return "Sickle Cell Trait"
|
||||
if 0.318 <= ratio <= 0.356:
|
||||
return "Positive for Sickle Cell. HPLC for Confirmation"
|
||||
if 0.356 <= ratio <= 0.7:
|
||||
return "Sickle Cell Disease"
|
||||
else:
|
||||
return "Invalid"
|
||||
except Exception as e:
|
||||
handle_exception(e)
|
||||
return "Error"
|
||||
return "Invalid"
|
||||
|
||||
def reclassify_with_borderline_method_2(device_ratio, device_ratio_class, led2_average):
|
||||
try:
|
||||
if device_ratio is not None and led2_average is not None:
|
||||
if device_ratio_class == "Negative Borderline":
|
||||
return "Borderline. Normal" if led2_average >= 0.15 else "Borderline. Sickle Cell Trait"
|
||||
elif device_ratio_class == "Positive for Sickle Cell. HPLC for Confirmation":
|
||||
return "Borderline. Sickle Cell Trait" if led2_average >= 0.19 else "Borderline. Sickle Cell Disease"
|
||||
except Exception as e:
|
||||
handle_exception(e)
|
||||
return "Error"
|
||||
return str(device_ratio_class)
|
||||
|
||||
def handle_exception(exception):
|
||||
pass
|
||||
|
||||
df["tmpDeviceRatioClass1"] = df['deviceRatio'].apply(device_ratio_classification)
|
||||
df["borderlineMetric1"] = (df['led1Average'] - df['led2Average']) / df['deviceRatio']
|
||||
df['newDeviceRatioClass1'] = df.apply(lambda row: find_result_with_borderline_method_1(
|
||||
row['deviceRatio'],
|
||||
row['tmpDeviceRatioClass1'],
|
||||
row['borderlineMetric1']
|
||||
), axis=1)
|
||||
|
||||
df["tmpDeviceRatioClass2"] = df['deviceRatio'].apply(device_ratio_borderline_thresholds_br_method_2)
|
||||
df['newDeviceRatioClass2'] = df.apply(lambda row: reclassify_with_borderline_method_2(
|
||||
row['deviceRatio'],
|
||||
row['tmpDeviceRatioClass2'],
|
||||
row['led2Average']
|
||||
), axis=1)
|
||||
|
||||
# clean columns
|
||||
df = df[["_id", "name", "classificationResult", "newDeviceRatioClass1", "newDeviceRatioClass2", "deviceRatio", "led1Average", "led2Average"]]
|
||||
print(df)
|
||||
|
||||
writer = pd.ExcelWriter(curdir + path_delim + "data/output.xlsx", engine = 'openpyxl')
|
||||
df.to_excel(writer, sheet_name = 'op', index=False)
|
||||
writer.close()
|
||||
Reference in New Issue
Block a user