-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
262 lines (214 loc) · 7.32 KB
/
app.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from random import randint
from time import strftime
from flask import Flask, render_template, flash, request, Response
from wtforms import Form, TextField, TextAreaField, validators, StringField, SubmitField
# plotting
import json
import plotly
import plotly.graph_objs as go
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = 'SjdnUends821Jsdlkvxh391ksdODnejdDw'
class BayesForm(Form):
specificity = TextField('Specificity:', validators=[validators.required()])
sensitivity = TextField('Sensitivity:', validators=[validators.required()])
prevalence = TextField('Prevalence:', validators=[validators.required()])
threshold = TextField('Probability Threshold:', validators=[validators.required()])
@app.route("/", methods=['GET', 'POST'])
def bayes():
form = BayesForm(request.form)
prob = None
prob_2 = None
prob_3 = None
global specificity, sensitivity, prevalence, threshold
threshold = None
bar = None
sens_line = None
specs_line = None
#print(form.errors)
if request.method == 'POST':
if form.validate():
specificity_v=request.form['specificity']
sensitivity_v=request.form['sensitivity']
prevalence_v=request.form['prevalence']
threshold_v=request.form['threshold']
flash('Your values are: Specificity {} Sensitivity {} Prevalence {} Threshold {}'.format(specificity_v, sensitivity_v, prevalence_v, threshold_v))
"""
Computes the posterior using Bayes' rule
"""
specificity = float(specificity_v)
sensitivity = float(sensitivity_v)
prevalence = float(prevalence_v)
threshold = float(threshold_v)
p_user = prevalence
p_non_user = 1-prevalence
p_pos_user = sensitivity
p_neg_user = specificity
p_pos_non_user = 1-float(specificity_v)
num = p_pos_user*p_user
den = p_pos_user*p_user+p_pos_non_user*p_non_user
prob = num/den
if prob > float(threshold_v):
print("The test-taker could be an user")
else:
print("The test-taker may not be an user")
bar = create_graph()
sens_line = create_sens_graph()
specs_line = create_spec_graph()
prob_2 = posterior(sensitivity=sensitivity,specificity=specificity,prevalence=prob, threshold=threshold)
prob_3 = posterior(sensitivity=sensitivity,specificity=specificity,prevalence=prob_2, threshold=threshold)
else:
flash('Error: All Fields are Required')
return render_template('index.html', form=form, value=prob, threshold=threshold, plot=bar, sens=sens_line, specs=specs_line, probTwo=prob_2, probThree=prob_3)
def posterior(specificity, sensitivity, prevalence, threshold):
p_user = prevalence
p_non_user = 1-prevalence
p_pos_user = sensitivity
p_neg_user = specificity
p_pos_non_user = 1-specificity
num = p_pos_user*p_user
den = p_pos_user*p_user+p_pos_non_user*p_non_user
prob = num/den
return prob
@app.route("/", methods=['POST'])
def plot_png():
# logic
bar = create_graph()
print('Here the bar')
print(bar)
return render_template('index.html', plot=bar)
def create_graph():
probs=[]
prevs=[]
for prev in [i*0.001 for i in range(1,51,2)]:
prevs.append(prev*100)
prob = posterior(sensitivity=sensitivity,specificity=specificity,prevalence=prev, threshold=threshold)
probs.append(prob)
data = [
go.Scatter(
x=prevs,
y=probs,
mode='lines+markers'
)
]
layout = go.Layout(
title=go.layout.Title(
text='Probability of True Positive with Prevalence Rate',
xref='paper',
x=0
),
xaxis=go.layout.XAxis(
title=go.layout.xaxis.Title(
text='Prevalence(%)',
font=dict(
family='Roboto',
size=18,
color='#7f7f7f'
)
)
),
yaxis=go.layout.YAxis(
title=go.layout.yaxis.Title(
text='Probability of a True Positive',
font=dict(
family='Roboto',
size=18,
color='#7f7f7f'
)
)
)
)
fig = go.Figure(data=data, layout=layout)
graph = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return graph
def create_sens_graph():
probs=[]
sens=[]
for sen in [i*0.001+0.95 for i in range(1,50,2)]:
sens.append(sen)
prob = posterior(sensitivity=sen,specificity=specificity,prevalence=prevalence, threshold=threshold)
probs.append(prob)
data = [
go.Scatter(
x=sens,
y=probs,
mode='lines+markers'
)
]
layout = go.Layout(
title=go.layout.Title(
text='Probability of True Positive with Test Sensitivity',
xref='paper',
x=0
),
xaxis=go.layout.XAxis(
title=go.layout.xaxis.Title(
text='Sensitivity(%)',
font=dict(
family='Roboto',
size=18,
color='#7f7f7f'
)
)
),
yaxis=go.layout.YAxis(
title=go.layout.yaxis.Title(
text='Probability of a True Positive',
font=dict(
family='Roboto',
size=18,
color='#7f7f7f'
)
)
)
)
fig = go.Figure(data=data, layout=layout)
graph = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return graph
def create_spec_graph():
probs=[]
specs=[]
for spec in [i*0.001+0.95 for i in range(1,50,2)]:
specs.append(spec)
prob = posterior(sensitivity=sensitivity,specificity=spec,prevalence=prevalence, threshold=threshold)
probs.append(prob)
data = [
go.Scatter(
x=specs,
y=probs,
mode='lines+markers'
)
]
layout = go.Layout(
title=go.layout.Title(
text='Probability of True Positive with Test Specificity',
xref='paper',
x=0
),
xaxis=go.layout.XAxis(
title=go.layout.xaxis.Title(
text='Specificity(%)',
font=dict(
family='Roboto',
size=18,
color='#7f7f7f'
)
)
),
yaxis=go.layout.YAxis(
title=go.layout.yaxis.Title(
text='Probability of a True Positive',
font=dict(
family='Roboto',
size=18,
color='#7f7f7f'
)
)
)
)
fig = go.Figure(data=data, layout=layout)
graph = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return graph
if __name__ == "__main__":
app.run(port=3000, debug=True)