-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcancer_prediction.py
More file actions
165 lines (135 loc) · 5.34 KB
/
cancer_prediction.py
File metadata and controls
165 lines (135 loc) · 5.34 KB
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
import streamlit as st
import pandas as pd
import numpy as np
import pickle
from model import main
import plotly.graph_objects as go
from model.main import get_clean_data
from model.main import columns_max_min
from model.main import NeuralNet
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu" )
def add_sidebar():
columns = columns_max_min()
slider_label = []
categories = []
# list(set(categories))
for col, value in columns.items():
split = col.split("_")
firstpart = " ".join(split[:-1])
categories.append(firstpart)
slider_name = str(firstpart + " ("+split[-1]+")")
label = (slider_name.title(), col)
slider_label.append(label)
input_dict = {}
for label, key in slider_label:
input_dict[key] = st.sidebar.slider(
label,
min_value=float(0),
max_value=float(columns[key][0]),
value=float(columns[key][1])
)
# st.write(input_dict)
# list(categories) = set(categories)
return input_dict, categories
def get_scaled_values( input_dict):
data = get_clean_data()
indep = data.drop(columns=['diagnosis'], axis= 1)
scaled_dict = {}
for key,value in input_dict.items():
max_value = indep[key].max()
min_value = indep[key].min()
scaled_value = (value-min_value)/(max_value-min_value)
scaled_dict[key] = scaled_value
return scaled_dict
def get_radar_chart(input_data, categories):
input_data = get_scaled_values(input_data)
categories = list(set(categories))
cate = []
for cat in categories:
cate.append(cat.title())
categories = cate
fig = go.Figure()
fig.add_trace(go.Scatterpolar(
r=[input_data['radius_mean'], input_data['texture_mean'], input_data['perimeter_mean'],
input_data['area_mean'], input_data['smoothness_mean'], input_data['compactness_mean'],
input_data['concavity_mean'], input_data['concave points_mean'],
input_data['symmetry_mean'], input_data['fractal_dimension_mean']],
theta=categories,
fill='toself',
name='Mean'
))
fig.add_trace(go.Scatterpolar(
r=[input_data['radius_se'], input_data['texture_se'], input_data['perimeter_se'],
input_data['area_se'], input_data['smoothness_se'], input_data['compactness_se'],
input_data['concavity_se'], input_data['concave points_se'],
input_data['symmetry_se'], input_data['fractal_dimension_se']],
theta=categories,
fill='toself',
name='Standard Error'
))
fig.add_trace(go.Scatterpolar(
r=[input_data['radius_worst'], input_data['texture_worst'], input_data['perimeter_worst'],
input_data['area_worst'], input_data['smoothness_worst'], input_data['compactness_worst'],
input_data['concavity_worst'], input_data['concave points_worst'],
input_data['symmetry_worst'], input_data['fractal_dimension_worst']],
theta=categories,
fill='toself',
name='Worst'
))
fig.update_layout(
polar=dict(
radialaxis=dict(
visible=True,
range=[0, 1]
)),
showlegend=True
)
return fig
def add_prediction(input_data):
model = pickle.load(open("resources/model.pkl", "rb"))
scaler = pickle.load(open("resources/scaler.pkl", "rb"))
input_array = np.array(list(input_data.values())).reshape(1,-1)
X = scaler.transform(input_array)
X_tran = torch.tensor(X, dtype=torch.float32).to(device)
model.eval()
with torch.no_grad():
outputs = model(X_tran)
predicted = outputs.round()
if predicted.item() == 0.0:
st.write("<span class='diagnosis benign_cls'>Benign</span>",unsafe_allow_html=True)
st.write("The probability of being benign: ","\n", round(1.00 - outputs.item(),2))
st.write("The probability of being malignanat: ","\n", round(outputs.item(),2))
else:
st.write("<span class='diagnosis malignant_cls'>Malignant</span>",unsafe_allow_html=True)
st.write("The probability of being benign: ","\n", round(1.00 - outputs.item(),2))
st.write("The probability of being malignanat: ","\n", round(outputs.item(),2))
def main():
st.set_page_config(
page_title= "Breast Cancer Prediction",
page_icon= "⚕️",
layout="wide",
initial_sidebar_state="expanded"
)
with open("assets/style.css") as f:
st.markdown("<style>{}</style>".format(f.read()), unsafe_allow_html=True)
input_data, categories = add_sidebar()
# st.write(input_data)
# st.write("Categories:")
# st.write(set(categories))
with st.container():
st.title("Breast Cancer Predictor")
st.write("This app predicts using a neural network model whether the breast mass is benign or malignant based on the measurements.")
st.write("You can update the measurements using the slideers in the sidebar.")
col1, col2= st.columns([3,1])
with col1:
fig_chart = get_radar_chart(input_data, categories)
st.plotly_chart(fig_chart)
# st.divider()
with col2:
st.subheader("Cell Cluster Prediction")
add_prediction(input_data)
# with col3:
# st.write("this is third col")
if __name__ == '__main__':
main()