-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
223 lines (177 loc) · 9.36 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import pandas as pd
# import matplotlib.pyplot as plt
# import seaborn as sns
import streamlit as st
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.preprocessing import StandardScaler
import time
class Util:
def __init__(self, file_path = 'G:\liver-disease-detection-main\data\Dataset.csv'):
self.features = ['Age',
'Gender',
'Total_Bilirubin',
'Direct_Bilirubin',
'Alkaline_Phosphotase',
'Alanine_Aminotransferase',
'Aspartate_Aminotransferase',
'Total_Proteins',
'Albumin',
'Albumin_and_Globulin_Ratio']
self.target_col = 'Diagnosis'
self.file_path = file_path
def preprocess(self, df):
# Rename columns for diagnosis classes
df.rename(columns={'Dataset': 'Diagnosis'}, inplace=True)
df[self.target_col] = df[self.target_col].apply(lambda x:1 if x==1 else 0)
# Fill null values
mean_ratio = df['Albumin_and_Globulin_Ratio'].mean()
df = df.fillna(mean_ratio)
# Convert categorical to numerical column
df['Gender'] = df['Gender'].apply(lambda x:1 if x=='Male' else 0)
return df
@st.cache
def get_data(self):
df = pd.read_csv(self.file_path)
# preprocess data
df = self.preprocess(df)
return df
@st.cache
def split_data(self, df):
X = df[self.features]
y = df[self.target_col]
# Scaling the feature columns
# scaler = StandardScaler().fit(X)
# X_scaled = scaler.transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=21)
return X_train, X_test, y_train, y_test
@st.cache
def build_model(self, X, y):
model = LogisticRegression()
print("Fitting the model")
model.fit(X, y)
return model
def compute_accuracy(self, model, X_test, y_test):
y_pred = model.predict(X_test)
return accuracy_score(y_test, y_pred)*100
def predict(model, X):
prediction = model.predict(X)
return prediction
def input_data_fields(self, overwrite_vals=None):
default_vals = {'Age': 17,
'Gender': 1,
'Total_Bilirubin': 0.9,
'Direct_Bilirubin': 0.3,
'Alkaline_Phosphotase': 202,
'Alanine_Aminotransferase': 22,
'Aspartate_Aminotransferase': 19,
'Total_Proteins': 7.4,
'Albumin': 4.1,
'Albumin_and_Globulin_Ratio': 1.2}
col1, col2 = st.columns(2)
age = col1.number_input("Age",
min_value=None,
step=5,
value=default_vals['Age'],
help="In the United States, the average age at onset of liver cancer is 63 years.")
gender = col2.selectbox('Gender',
('Male', 'Female'),
index=default_vals['Gender'],
help="Men are more likely to develop liver cancer than women, by a ratio of 2 to 1.")
total_bilirubin = col1.number_input("Total_Bilirubin (mg/dL)",
min_value=None,
step=0.5,
value=default_vals['Total_Bilirubin'],
help="It is normal to have some bilirubin in the blood. A normal level is: 0.1 to 1.2 mg/dL (1.71 to 20.5 µmol/L)")
direct_bilirubin = col2.number_input("Direct_Bilirubin (mg/dL)",
min_value=None,
step=0.5,
value=default_vals['Direct_Bilirubin'],
help="Normal level for Direct (also called conjugated) bilirubin is less than 0.3 mg/dL.")
alkaline_phosphotase = col1.number_input("Alkaline_Phosphotase (IU/L)",
min_value=None,
step=10,
value=default_vals['Alkaline_Phosphotase'],
help="The normal range is 44 to 147 international units per liter (IU/L).")
alanine_aminotransferase = col2.number_input("Alanine_Aminotransferase (U/L)",
min_value=None,
step=5,
value=default_vals['Alanine_Aminotransferase'],
help="The normal range is 4 to 36 U/L.")
aspartate_aminotransferase = col1.number_input("Aspartate_Aminotransferase (U/L)",
min_value=None,
step=5,
value=default_vals['Aspartate_Aminotransferase'],
help="The normal range is 8 to 33 U/L.")
total_proteins = col2.number_input("Total_Proteins (g/dL)",
min_value=None,
step=0.5,
value=default_vals['Total_Proteins'],
help="The normal range is 6.0 to 8.3 grams per deciliter (g/dL) or 60 to 83 g/L.")
albumin = col1.number_input("Albumin (G/dL)",
min_value=None,
step=0.5,
value=default_vals['Albumin'],
help="The normal range is 3.4 to 5.4 g/dL (34 to 54 g/L).")
albumin_and_globulin_ratio = col2.number_input("Albumin_and_Globulin_Ratio",
min_value=None,
step=0.2,
value=default_vals['Albumin_and_Globulin_Ratio'],
help="The normal range for albumin/globulin ratio is over 1 , usually around 1 to 2.")
gender = 0 if gender == "Male" else 1
return {'Age': age,
'Gender': gender,
'Total_Bilirubin': total_bilirubin,
'Direct_Bilirubin': direct_bilirubin,
'Alkaline_Phosphotase': alkaline_phosphotase,
'Alanine_Aminotransferase': alanine_aminotransferase,
'Aspartate_Aminotransferase': aspartate_aminotransferase,
'Total_Proteins': total_proteins,
'Albumin': albumin,
'Albumin_and_Globulin_Ratio': albumin_and_globulin_ratio}
def form_functions(self, model):
with st.form("my_form"):
get_values = self.input_data_fields()
submitted = st.form_submit_button("Submit", type="primary")
if submitted:
data_values = pd.DataFrame([get_values])
# Get predictions
with st.spinner('Making prediction...'):
time.sleep(3)
prediction = model.predict(data_values)
print("Prediction: ", prediction[0])
prediction_msg = "The supplied values suggest that the patient does not have a liver disease." if prediction == 0 else "The supplied values suggest that the patient has a liver disease. It is suggested to provide critical emphasis on diagnosing further symptoms of the patient. "
st.subheader("Diagnosis:")
if prediction == 0:
print("Success")
st.success(prediction_msg)
else:
st.error(prediction_msg)
def sample_data(self, df):
test_data = df.drop('Diagnosis', axis=1).to_dict(orient='records')
return test_data
def page_footer(self):
footer="""<style>
a:link , a:visited{
color: blue;
background-color: transparent;
text-decoration: underline;
}
a:hover, a:active {
color: red;
background-color: transparent;
text-decoration: underline;
}
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: white;
color: black;
text-align: center;
}
</style><div class="footer"><p>Developed by <a style='display: block; text-align: center;' target="_blank">Shivansh Raghuwanshi Final Project</a></p></div>
"""
return footer