deepthiaj/Electro_oneAPI
3
1import streamlit as st2import pandas as pd 3import pickle4import xgboost as xgb5import numpy as np6import sklearn 7from sklearn.metrics import confusion_matrix, classification_report8import seaborn as sns9import matplotlib.pyplot as plt10from io import StringIO11from scipy import signal12import daal4py as d4p13import time14from sklearn.model_selection import train_test_split15 16st.title("Automated Diagnosis of Heart Disease from Electro-Cardiogram")17st.write('This is a prototype for checking heart health condition. The performance of the model has been achieved using XGboost ML algorithm.')18st.write('Please select the data and the model from the dropdown menu on the left panel to see the working of this prototype.')19 20st.divider()21 22enc_dat = pd.read_csv("PTB_ECGencoded_dat.csv")23 24# Split the dataset into features (X) and target (y)25X = enc_dat.iloc[:, :-1].values # Features (all columns except the last one)26y = enc_dat.iloc[:, -1].values # Target (last column "diagnosis")27# Map the existing class labels to the expected class values28class_mapping = {0: 0, 1: 1, 3: 2, 4: 3, 6: 4, 7: 5}29mapped_labels = np.array([class_mapping[label] for label in y])30 31# split data into train and test sets32seed = 733test_size = 0.3334X_train, X_test, y_train, y_test = train_test_split(X, mapped_labels, test_size=test_size, random_state=seed)35 36# Define the model parameters37model_params = {38 'objective': 'multi:softmax',39 'num_class': 6, 40 'random_state': 4241}42 43# Create and train the XGBoost model44xgb_model = xgb.XGBClassifier(**model_params)45eval_set = [(X_test, y_test)]46xgb_model.fit(X_train, y_train, early_stopping_rounds=10, eval_set=eval_set, verbose=True)47# DAAL model48daal_model = d4p.get_gbt_model_from_xgboost(xgb_model.get_booster())49 50 51st.subheader("Performance evaluation of the Automated Diagnosis Model")52 53 54if st.button('ECG analysis of Patient001'):55 # patient001_signal_analysis() to visualize data analysis of single patient upon a button click56 st.write('give plots and heart rate analysis. Please upload ECG signal data in specified format below for analysis')57 # refer PTB website for format58 # call preprocessing module59 # call ecg_analysis()60 61st.divider()62 # # Evaluate the model on the entire dataset63 64# XGBoost prediction (for accuracy comparison)65t0 = time.time()66y_pred = xgb_model.predict(X_test)67t1 = time.time()68xgb_errors_count = np.count_nonzero(y_pred - np.ravel(y_test))69 70xgb_total = t1-t071st.write("Prediction time using XGBoost model is ", xgb_total)72accuracy = np.sum(y_pred == y_test) / len(y_test) # Calculate accuracy73 # print(f"Accuracy: {accuracy}")74acc = (accuracy / 1) * 10075st.write("The accuracy of the diagnosis report is: ", acc, "%")76 77 78st.divider()79 80 # # Evaluate the model on the entire dataset81 # y_pred = loaded_model.predict(X)82 83 # # Calculate evaluation metrics84classification_metrics = classification_report(y_test, y_pred, output_dict=True)85st.caption(":blue[Classification Metrics]")86# classification_metrics = [classification_metrics]87# cm = classification_metrics.insert(0,'metrics')88st.table(classification_metrics)89# st.json(classification_metrics)90st.write("1: Myocardial infarction, 2: Bundle branch block, 3: Dysrhythmia , 4: Valvular heart disease, 5: Myocarditis")91 92st.divider()93 # # Calculate confusion matrix94confusion_mat = confusion_matrix(y_test, y_pred)95# st.write("Confusion matrix:")96 97 # # Plot confusion matrix98plt.figure(figsize=(10, 8))99htmap = sns.heatmap(confusion_mat, annot=True, fmt="d", cmap="Blues")100plt.title("Confusion Matrix")101plt.xlabel("Predicted Class")102plt.ylabel("True Class")103plt.show()104htmap = htmap.figure105st.pyplot(htmap)106 107 108st.divider()109 # Format signal info & preprocessing module for generating X[0] to diagnose from an external input data & give a dropbox to enter a single patient ecg data in .dat and .hea format110 111# Make a faster prediction with oneDAL112n_classes = 6113# daal_prediction = d4p.gbt_classification_prediction(nClasses = n_classes).compute(X, daal_model).prediction114# daal4py prediction for increased performance115daal_predict_algo = d4p.gbt_classification_prediction(116 nClasses=n_classes,117 resultsToEvaluate="computeClassLabels",118 fptype='float'119)120t0 = time.time()121daal_prediction = daal_predict_algo.compute(X_test, daal_model)122t1 = time.time()123daal_errors_count = np.count_nonzero(np.ravel(daal_prediction.prediction) - np.ravel(y_test))124 125d4p_total = t1-t0126st.write("Prediction time using DAAL model is ", xgb_total)127 128 129# # List all results that you need by placing '|' between them130# predict_algo = d4p.gbt_classification_prediction(nClasses = n_classes, resultsToEvaluate = "computeClassLabels|computeClassProbabilities")131# daal_prediction = predict_algo.compute(X, daal_model)132# # Get probabilities:133# probabilities = daal_prediction.probabilities134# st.write(probabilities)135# # Get labels:136# labels = daal_prediction.prediction137# st.write(labels)138 139# assert np.absolute(xgb_errors_count - daal_errors_count) = 0140y_test = np.ravel(y_test)141daal_prediction = np.ravel(daal_prediction.prediction)142xgb_prediction = y_pred143 144st.subheader("Accuracy & Performance Comparison: XGBoots Prediction vs. Daal4py Prediction")145st.write("No accuracy loss!")146st.write("\nXGBoost prediction results (first 10 rows):\n", xgb_prediction[0:10])147st.write("\ndaal4py prediction results (first 10 rows):\n", daal_prediction[0:10])148st.write("\nGround truth (first 10 rows):\n", y_test[0:10])149 150st.write("XGBoost errors count:", xgb_errors_count)151st.write("XGBoost accuracy score:", 1 - xgb_errors_count / xgb_prediction.shape[0])152 153st.write("\ndaal4py errors count:", daal_errors_count)154st.write("daal4py accuracy score:", 1 - daal_errors_count / daal_prediction.shape[0])155 156st.write("\n XGBoost Prediction Time:", xgb_total)157st.write("\n daal4py Prediction Time:", d4p_total)158# st.write("\nAll looks good!")159 160 161st.subheader("Visualizations")162st.write("Performance")163left = [1,2]164pred_times = [xgb_total, d4p_total]165tick_label = ['XGBoost Prediction', 'daal4py Prediction']166# plt.bar(left, pred_times, tick_label = tick_label, width = 0.5, color = ['red', 'blue'])167plt.xlabel('Prediction Method'); plt.ylabel('time,s'); plt.title('Prediction time,s')168plt.show()169# plt0 = plt0.figure170# st.pyplot(plt0)171st.bar_chart(pred_times)172st.write("speedup:",xgb_total/d4p_total)173st.write("Accuracy")174left = [1,2]175 176 177xgb_acc = 1 - xgb_errors_count / xgb_prediction.shape[0]178d4p_acc = 1 - daal_errors_count / daal_prediction.shape[0]179pred_acc = [xgb_acc, d4p_acc]180tick_label = ['XGBoost Prediction', 'daal4py Prediction']181# plt.bar(left, pred_acc, tick_label = tick_label, width = 0.5, color = ['red', 'blue'])182plt.xlabel('Prediction Method')183plt.ylabel('accuracy, %') 184plt.title('Prediction Accuracy, %')185plt.show()186# plt1 = plt1.figure187# st.pyplot(plt1)188st.bar_chart(pred_acc)189st.write("Accuracy Difference",xgb_acc-d4p_acc)190 191st.divider()192 193 194 195 196patient_enc_data = {"Patient001":X[0],"Patient002":X[100],"Patient003":X[200],"Patient004":X[50],"Patient005":X[40],"Patient006":X[30],"Patient007":X[20],"Patient008":X[10],"Patient009":X[60],"Patient010":X[110],"Patient011":X[120],"Patient012":X[130],"Patient013":X[140],"Patient014":X[150],"Patient015":X[160],"Patient016":X[170],"Patient017":X[180],"Patient018":X[190],"Patient019":X[210],"Patient020":X[220],"Patient021":X[21],"Patient022":X[22],"Patient023":X[23],"Patient024":X[24],"Patient025":X[25],"Patient026":X[26],"Patient027":X[27],"Patient028":X[28],"Patient029":X[29],"Patient030":X[31],"Patient031":X[41],"Patient032":X[42],"Patient033":X[43],"Patient034":X[44],"Patient035":X[45],"Patient036":X[46],"Patient037":X[47],"Patient038":X[48],"Patient039":X[49],"Patient040":X[51],"Patient41":X[61],"Patient042":X[62],"Patient043":X[63],"Patient044":X[64],"Patient045":X[65],"Patient046":X[66],"Patient047":X[67],"Patient048":X[68],"Patient049":X[69],"Patient050":X[71], }197patient_ecg_sel = st.selectbox( "Select a ECG of a patient from the list", list(patient_enc_data.keys()))198 199 200 201 202def ecg_analysis(ecg_test_data):203 204 # Classify the test data point205 predicted_class = xgb_model.predict(np.array([ecg_test_data]))206 207 208 st.subheader("Diagnosis Report")209 210 211 if predicted_class[0] == 0:212 st.write("Sorry, We cannot give your diagnosis report at the moment. Kindly consult a doctor in person.")213 elif predicted_class[0] == 1:214 st.write("You are diagnosed with Myocardial infarction.")215 st.write("Kindly consult a doctor to take the necessary treatment.")216 elif predicted_class[0] == 2:217 st.write("You are diagnosed with Bundle branch block.")218 st.write("Kindly consult a doctor to take the necessary treatment.")219 elif predicted_class[0] == 3:220 st.write("You are diagnosed with Dysrhythmia.")221 st.write("Kindly take consult a doctor to the necessary treatment.")222 elif predicted_class[0] == 4:223 st.write("You are diagnosed with Valvular heart disease.") 224 st.write("Kindly consult a doctor to take the necessary treatment.")225 elif predicted_class[0] == 5:226 st.write("You are diagnosed with Myocarditis.") 227 st.write("Kindly consult a doctor to take the necessary treatment.")228 else:229 st.write("Sorry, We cannot give your diagnosis report at the moment. Kindly consult a doctor in person.")230 231 232 233if st.button("Analyze Raw ECG"):234# # if new_data:235# # new_patient_data_preprocessing()236# # else:237 ecg_train_dat = pd.read_csv("PTB_ECGdata.csv")238 diagnosis_counts = ecg_train_dat["diagnosis"].value_counts()239 st.bar_chart(diagnosis_counts)240 241def new_patient_data_preprocessing(new_data):242 243 # code to preprocess .dat and .hea files from PTB ecg database, check one from ptb xl as external new data & convert it into .csv & encode to pass it as an argument to call ecg_analysis function244 st.write('')245 246# st.write("")247uploaded_file = st.file_uploader("Upload ECG file")248if uploaded_file is not None:249 250 # Can be used wherever a "file-like" object is accepted:251 dataframe = pd.read_csv(uploaded_file)252 st.write(dataframe[:1])253 new_patient_data_preprocessing(dataframe)254 255if st.button("Check Heart health"):256 ecg_test_data = patient_enc_data[patient_ecg_sel]257 st.write("Diagnosis report of", patient_ecg_sel)258 # st_profile_report(ecg_test_data)259 ecg_analysis(ecg_test_data)260else:261 st.write("Diagnosis report of Patient001")262 ecg_test_data = X[0] 263 ecg_analysis(ecg_test_data)264 265 266 267 