DiabeTAPP / predict_page.py
d142111015's picture
Update predict_page.py
3ee9f05 verified
Raw History Blame Contribute Delete
5.32 kB
# -*- coding: utf-8 -*-
"""
Created on Tue May 9 16:14:55 2023
@author: Henry Kha
"""
# !pip install altair
# pip install --upgrade altair
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import xgboost
from xgboost import XGBClassifier
import pickle
from sklearn import preprocessing
model_path = "FINETUNED_best_xgb.ubj"
model = XGBClassifier()
model.load_model(model_path)
# model_path = "best_xgb.sav"
# def loading_model(model_path=model_path):
# with open(model_path, 'rb') as file:
# # model = XGBClassifier()
# model = pickle.load(file)
# # model = model.best_estimator_
# return model
# model = loading_model()
datapath = "noddpairname.csv"
df = pd.read_csv(datapath)
ddp_encoder = preprocessing.LabelEncoder()
ddp_encoder.fit(df['DDPairs'])
df['DDPairs']= ddp_encoder.transform(df['DDPairs'])
interpretation = "classes_of_ddis.csv"
pred_interpretation = pd.read_csv(interpretation)
classes_encoder = preprocessing.LabelEncoder()
classes_encoder.fit(pred_interpretation['DDI'])
pred_interpretation['DDI_encoder']= classes_encoder.transform(pred_interpretation['DDI'])
def print_output(interpretation=interpretation):
pred_interpretation = pd.read_csv(interpretation)
printed_output = pred_interpretation[pred_interpretation["class"]==y_pred.astype(int)[0]]["DDI"].iloc[0].replace("drug_a",DRUG).replace("drug_b",INTERACTING_DRUG)
return printed_output
b = tuple(pd.read_csv("drugs.csv")["interacting"].values.tolist())
svg_file = "AIBIOMED_logo.svg"
def show_predict_page():
# st.title("DiabeTAPP")
st.markdown("<h1 style='text-align: center; color: blue;'>DiabeTAPP</h1>", unsafe_allow_html=True)
st.markdown("""
<style>
.stApp {
background-color: white;
}
</style>
""", unsafe_allow_html=True)
# Add explanatory text
st.write("### HOW TO USE THIS INTERFACE?")
st.write("""
**Scenario**: Checking for interactions between **Metformin** (the oral diabetes drug) and **Furosemide** (the query drug).
- **Step 1**: Select **Metformin** as the diabetes drug in the first list below, entitled "THE ORAL DIABETES DRUG LIST".
- **Step 2**: Select **Furosemide** as the query drug in the second list below, entitled "THE QUERY DRUG LIST".
- **Step 3**: Click the box entitled "Please Click Here to Check Interactions" to view the predictive interaction(s) between the two drugs.
- **Step 4**: The potential interaction of the two drugs is: "The therapeutic efficacy of Metformin can be decreased when used in combination with Furosemide."
""")
st.write("""**!!! WARNING !!!**: The interactions shown between two specific drugs are predictions made by DiabeTAPP. If no interactions are found ("No Interactions Found"), it does not guarantee that no interactions exist. Always consult with a healthcare professional for accurate advice.""")
st.write("""**Caution**: If some drugs cannot be found in both lists, please consult a clinician for accurate information.""")
DRUGs = (
'Acarbose',
'Acetohexamide',
'Alogliptin',
'Benfluorex',
'Buformin',
'Canagliflozin',
'Carbutamide',
'Chlorpropamide',
'Dapagliflozin',
'Empagliflozin',
'Ertugliflozin',
'Evogliptin',
'Gemigliptin',
'Glibornuride',
'Gliclazide',
'Glipizide',
'Gliquidone',
'Glisoxepide',
'Glyburide',
'Glymidine',
'Ipragliflozin',
'Linagliptin',
'Lobeglitazone',
'Luseogliflozin',
'Metahexamide',
'Metformin',
'Miglitol',
'Mitiglinide',
'Phenformin',
'Pioglitazone',
'Repaglinide',
'Rosiglitazone',
'Rosuvastatin',
'Saxagliptin',
'Sitagliptin',
'Sotagliflozin',
'Teneligliptin',
'Tolazamide',
'Tolbutamide',
'Troglitazone',
'Vildagliptin',
'Voglibose'
)
INTERACTING_DRUGs = b
DRUG = st.selectbox("**THE ORAL DIABETES DRUG LIST** -- Please select or enter an oral diabetes drug in the list below to check for interactions: ", DRUGs)
INTERACTING_DRUG = st.selectbox("**THE QUERY DRUG LIST** -- Please select or enter a drug name from the list below to check for interactions with the selected diabetes drug: ",INTERACTING_DRUGs)
ok = st.button("Please Click Here to Check Interactions")
if ok:
try:
DRUG, INTERACTING_DRUG = np.array([[DRUG,INTERACTING_DRUG]])[0]
com = DRUG+"_"+INTERACTING_DRUG
g = ddp_encoder.transform([com]).tolist()[0]
g = df[df["DDPairs"]==g]
g = g.drop("DDPairs",axis=1)
y_pred = model.predict(g)
output = classes_encoder.inverse_transform([y_pred]).tolist()[0]
output = output.replace("drug_a",DRUG).replace("drug_b",INTERACTING_DRUG)
#idx = np.where(df==com)
st.subheader(f'Interaction(s) Found: \n {output}')
except ValueError:
st.subheader("No Interactions Found")
st.write("""**Thank you for using our application.**""")