-
Notifications
You must be signed in to change notification settings - Fork 5
/
calculate_se_sp_rapidtest_streamlit.py
469 lines (365 loc) · 16.9 KB
/
calculate_se_sp_rapidtest_streamlit.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# CALCULATE Specificity and sensitivity rapidtests
# René Smit, 20 april 2022, MIT LICENSE
# from fnmatch import fnmatchcase
from tabulate import tabulate
# import matplotlib.pyplot as plt
import streamlit as st
import math
from statsmodels.stats.proportion import proportion_confint
import scipy.stats as scist
def simple_asymptotic(x,n, Za2):
# Simple Asymptotic = Wald
# The simple asymptotic formula is based on the normal approximation to the binomial distribution.
# The approximation is close only for very large sample sizes.
p = x/n
SE = math.sqrt((p*(1-p))/n ) #https://www2.ccrb.cuhk.edu.hk/stat/confidence%20interval/Diagnostic%20Statistic.htm#Formula
c1 = round( p - Za2*SE,3)
c2 = round ( p + Za2*SE,3)
return c1, c2
def calculate_confint(x, n, alpha=0.05):
# https://www.statology.org/binomial-confidence-interval-python/
# https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval
# normal : asymptotic normal approximation
# agresti_coull : Agresti-Coull interval
# beta : Clopper-Pearson interval based on Beta distribution
# wilson : Wilson Score interval
# jeffreys : Jeffreys Bayesian Interval
# binom_test : Numerical inversion of binom_test
# VEEL ONDERZOEKEN LATEN BETA ZIEN
# Flowflex gebruikt agresti-coull
#thaise test onbekend
methods = ["normal", "agresti_coull" , "beta", "wilson", "jeffreys"] # , "binom_test"]
for m in methods:
try:
a,b = proportion_confint(count=x, nobs=n, method=m)
st.write (f"{m} - [{round(a*100,1)},{round(b*100,1)}]")
except:
st.write(f"Problem with {m}")
def binomial_ci(x, n, alpha=0.05):
# sens c1,c2 = binomial_ci(tp,(tp+fn), alpha=0.05) #exact (Clopper-Pearson)
# spec c1y,c2y = binomial_ci(tn,(fp+tn), alpha=0.05) # exact (Clopper-Pearson)
# The following gives exact (Clopper-Pearson) interval for binomial distribution in a simple way.
# https://stackoverflow.com/questions/13059011/is-there-any-python-function-library-for-calculate-binomial-confidence-intervals
# x is number of successes, n is number of trials
from scipy import stats
if x==0:
c1 = 0
else:
c1 = round(stats.beta.interval(1-alpha, x,n-x+1)[0],1)
if x==n:
c2=1
else:
c2 = round(stats.beta.interval(1-alpha, x+1,n-x)[1],1)
return c1, c2
# https://www.frontiersin.org/articles/10.3389/fpubh.2013.00039/full
# https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4958484/
# https://andrewpwheeler.com/2020/11/30/confidence-intervals-around-proportions/
def calculate_se_sp( tp, fp, fn, tn, alpha ):
"""Calculte the Sensitivity and Specificity with it CI's
Returns:
se, sp, acc: sensitivity and specificity
"""
se = tp/(tp+fn)*100
sp = tn/(fp+tn)*100
acc = (tp + tn) /(tp+fn+fp+tn)*100
# SE sensitivity = square root [sensitivity – (1-sensitivity)]/n sensitivity) https://www.ncbi.nlm.nih.gov/books/NBK305683/
SE_sp = math.sqrt((sp/100*(1-sp/100))/(fp+tn) ) #/(tp+fn) #https://www2.ccrb.cuhk.edu.hk/stat/confidence%20interval/Diagnostic%20Statistic.htm#Formula
col1,col2,col3 = st.columns(3)
with col1:
st.subheader("Sensitivity")
se = calulate_value_and_ci ("Sensitivity", tp,(tp+fn), alpha)
with col2:
st.subheader(f"Specificity")
sp = calulate_value_and_ci ("Specificity", tn,(fp+tn), alpha)
with col3:
st.subheader(f" Accuracy")
acc = calulate_value_and_ci ("Accuracy",(tp + tn), (tp+fn+fp+tn),alpha)
return se, sp, acc
def calulate_value_and_ci(what, m,n, alpha):
Za2 = scist.norm.ppf(1-(alpha/2))
c1,c2 = simple_asymptotic(m,n, Za2)
bin_c1,bin_c2 = binomial_ci(m,n, alpha=alpha) #exact (Clopper-Pearson)
# https://www2.ccrb.cuhk.edu.hk/stat/confidence%20interval/Diagnostic%20Statistic.htm
# https://www.medcalc.org/calc/diagnostic_test.php
# https%3A%2F%2Fncss-wpengine.netdna-ssl.com%2Fwp-content%2Fthemes%2Fncss%2Fpdf%2FProcedures%2FPASS%2FConfidence_Intervals_for_One-Sample_Sensitivity_and_Specificity.pdf
value = m/(n)*100
st.write(f"{what} = {value} %")
st.write(f"Simple Asymptotic (Wald):")
st.write (f"[{round(c1*100,1)} - {round(c2*100,1)}]")
st.write(f"exact (Clopper-Pearson) = beta):") #geeft andere waarde
st.write(f"[{round(bin_c1*100,1)} - {round(bin_c2*100,1)}]")
calculate_confint(m,n, alpha=alpha)
st.write("")
# https://www2.ccrb.cuhk.edu.hk/stat/confidence%20interval/Diagnostic%20Statistic.htm
# The Specificity is 0.91 and the 95% C.I. is (0.89746, 0.92254).
# The Positive Predictive Value (PPV) is 0.1 and the 95% C.I. is (0.05842, 0.14158).
# The Negative Predictive Value (NPV) is 0.99454 and the 95% C.I. is (0.99116, 0.99791).
# The Pre-Test Probability is 0.01478.
# The Likelihood Ratio Positive (LR+) is 7.40741 and the 95% C.I. is (5.54896, 9.88828).
# The Positive Post-Test Probability is 0.1.
# The Likelihood Ratio Negative (LR-) is 0.3663 and the 95% C.I. is (0.22079, 0.60771).
# The Negative Post-Test Probability is 0.00546.
return value
def calculate(se, sp, prevalentie, number_of_tested_people, output):
"""calculates the values. Made as a function to be able to make graphs
with various prevalences
https://www.ntvg.nl/artikelen/het-gebruik-van-de-coronazelftest-perspectief
Args:
test (list): list with the name, sensitivity and specificity of the test
prevalentie (float): prevalentie of the virus
number_of_tested_people (int)): how many people to test
output (boolean): Shows the output as text or not
Returns:
fdr,for_, pos, fpr - see code
"""
sensitivity = se/100
specificity = sp/100
total_healthy = (100 - prevalentie) * number_of_tested_people/100
total_sick = (prevalentie/100) * number_of_tested_people
true_negative = round((total_healthy * specificity),0)
false_negative = round((total_sick * (1 - sensitivity)),0)
true_positive = round((total_sick * sensitivity),0)
false_positive= round((total_healthy * (1 - specificity)),0)
# print (true_positive)
# print (f"{specificity=}")
# print (f"{total_healthy=}")
# print (f"{false_positive=}")
#true_positive_bayes = round (100* (sensitivity * prevalentie) / ((sensitivity * prevalentie) + ((1-specificity)* (1-prevalentie) )),2)
try:
fdr = round(100*false_positive/(false_positive+ true_positive),4)
#print (fdr)
except:
fdr = 0
acc = round(100*((true_positive+true_negative)/number_of_tested_people),4)
for_ = round(100*false_negative/(false_negative + true_negative),4)
try:
fpr = round(100*false_positive/(false_positive+ true_negative),4)
except:
fpr = 0
try:
fnr = round(100*(false_negative/(true_positive+ false_negative)),4)
except:
fnr = 0
pos = round(100*((true_positive + false_positive)/number_of_tested_people),4)
try:
ppv = round((true_positive/(true_positive+false_positive)*100),3)
except:
ppv = 0
npv = round((true_negative/(false_negative+true_negative)*100),3)
tpr =round(100*true_positive/(true_positive+false_negative),3) # equal to Se
try:
tnr = round(100*true_negative/(false_positive+true_negative),3) # equal to Sp
except:
tnr = 0
a, b,c,d = ("PPV - "+ str(ppv)), ("FDR - " + str(fdr)), ("FOR - " + str(for_)), ("NPV - " + str(npv))
e,f,g,h = ("TPR/Se - " + str(tpr)), ("FPR - " + str(fpr)), ("FNR - " + str(fnr)), ("TNR, Sp - " + str(tnr))
data = [
[
"Result: 'sick' (+) ",
true_positive,
false_positive,
false_positive+ true_positive,
],
[
"Result: 'healthy' (-)",
false_negative,
true_negative,
true_negative + false_negative,
],
[
"Total",
(false_negative + true_positive),
(true_negative + false_positive),
(true_positive + false_positive + false_negative + true_negative),
],
]
data2 = [
[
"Result: 'sick' (+) ",
a ,
b,
ppv+fdr
],
[
"Result: 'healthy' (-)",
c,
d,
for_+npv
],
]
data3= [
[
"Result: 'sick' (+) ",
e ,
f
],
[
"Result: 'healthy' (-)",
g,
h
],
[
"Total",
round(tpr+fnr),
round(fpr+ tnr)
],
]
if output:
st.subheader("Extra calculations")
st.text (f"Number of tested people : {number_of_tested_people}")
st.text (f"Prevalentie testpopulatie = {round(prevalentie,2)}% (= {round(number_of_tested_people*prevalentie/100)} 'sick' persons)")
st.text(f"specificity : {sp} - sensitivity : {se}\n")
st.text(tabulate(data, headers=["#", "'Person is\nSick' (+)\nSensitivity (TPR)", "'Person is\nHealthy' (-)\nSpecificity (TNR)", "Total"]))
st.text(f"True positive : {round(true_positive)}")
st.text(f"True negative : {round(true_negative)}")
st.text(f"False positive : {round(false_positive)}")
st.text(f"False negative : {round(false_negative)}")
st.text(
f"Positive predictive value (PPV) : {ppv} % \n(Chance of being really 'sick' when tested 'sick')"
)
st.text(
f"Negative predictive value (NPV) : {npv} % \n(Chance of being really 'healthy' when you are tested 'healthy')"
)
st.text(
f"False Positive rate (FPR / α / Type 1 error) : {fpr} % \n(Chance of being tested 'sick' while being 'healthy' - probability of false alarm)" )
st.text(
f"False Negative rate (FNR/ β / type II error : {fnr} % \n(Chance of being tested 'healty' while being 'sick' - miss rate)" )
st.text(
f"False Discovery rate (FDR) : {fdr} % \n(Chance of being not 'sick' while you are tested 'sick')" )
st.text(
f"False Omission Rate (FOR) : {for_} % \n(Chance of being 'sick' when you are tested 'healthy')" )
# st.text(
# f"True positivity rate (Bayes): {true_positive_bayes} % ") # TOFIX
# if true_positive_bayes!= (100-fdr):
# st.text (f"DIFFERENCE !")
# st.text (100-fdr-true_positive_bayes)
#
st.text(
f"Accuracy : {acc} % ")
st.text(
f"Chance to be tested positive (true & false) : {pos} %\n\n"
)
#
st.text("\n\n")
#
st.text(tabulate(data2, headers=["%", "'Person is\nSick' (+)", "'Person is\nHealthy' (-)", "Total"]))
st.text("\n\n")
st.text(tabulate(data3, headers=["%", "'Person is\nSick' (+)", "'Person is\nHealthy' (-)"]))
st.text("--------------------------------------------------------------------------")
return fdr,for_, pos, fpr
def interface_test_results():
col1, col2,col3,col4 = st.columns(4)
with col1:
st.write("")
with col2:
st.write("PCR +")
with col3:
st.write("PCR -")
with col3:
st.write("Total")
col1, col2,col3,col4 = st.columns(4)
with col1:
st.write("")
st.write("")
st.write("")
st.write("Rapid +")
with col2:
tp = (st.number_input('True positive',None,None, 98))
with col3:
fp = (st.number_input('False positive',None,None, 0))
with col4:
st.write("")
st.write("")
st.write("")
st.write(tp+fp)
col1, col2,col3,col4 = st.columns(4)
with col1:
st.write("")
st.write("")
st.write("")
st.write("Rapid -")
with col2:
fn = (st.number_input('False negative',None,None, 2))
with col3:
tn = (st.number_input('True Negative',None,None, 210))
with col4:
st.write("")
st.write("")
st.write("")
st.write(fn+tn)
col1, col2,col3,col4 = st.columns(4)
with col1:
st.write("")
with col2:
st.write(tp+fn)
with col3:
st.write(fp+tn)
with col4:
st.write(tp+fn+fp+tn )
return tp,fp,fn,tn
def interface_left_bar():
prevalentie = st.sidebar.number_input('prevalence testpopulation in %',0.0,100.0, 1.0, format="%.4f")
number_of_tested_people = (st.sidebar.number_input('Number of tested people',None,None, 100_000))
alpha = (st.sidebar.number_input('Alpha',None,None, 0.05))
st.sidebar.write("Attention: too small numbers give erorrs (Division by zero)")
return prevalentie,number_of_tested_people, alpha
def main():
st.title(" Calculating Sensitivity and Specificity of Rapid tests")
what_to_do = st.sidebar.selectbox("What to do [Sens./Spec. | Diseas prob.]", ["se_sp", "bayes"],0)
if what_to_do == "se_sp":
main_se_sp()
else:
main_bayes()
def main_bayes():
# https://chatgpt.com/c/66f06f0e-acd0-8004-9990-8cd0ec0d268f
# Function to calculate the probability using Bayes' Theorem
def calculate_probability(prevalence, false_positive_rate, sensitivity):
# Convert percentages to decimal for calculations
prevalence /= 100
false_positive_rate /= 100
sensitivity /= 100
# Calculate P(Pos)
P_pos = (sensitivity * prevalence) + (false_positive_rate * (1 - prevalence))
# Calculate P(Disease | Pos)
P_disease_given_pos = (sensitivity * prevalence) / P_pos
# Convert result back to percentage
return P_disease_given_pos * 100
# Streamlit app
st.subheader("Disease Probability Calculator")
# Calculate probability when the button is pressed
# Inputs from user
prevalence = st.sidebar.number_input("Prevalence of Disease (%)", min_value=0.0, max_value=100.0, value=0.1, step=0.01)
false_positive_rate = st.sidebar.number_input("False Positive Rate (%)", min_value=0.0, max_value=100.0, value=5.0, step=0.1)
sensitivity = st.sidebar.number_input("Sensitivity of the Test (%)", min_value=0.0, max_value=100.0, value=100.0, step=0.1)
probability = calculate_probability(prevalence, false_positive_rate, sensitivity)
st.info(f"The probability that a person actually has the disease given a positive test result is **{probability:.2f}%**.")
# Explanation of the problem
st.markdown(r""" ### Problem Explanation:""")
st.markdown(r""" Suppose you are conducting a test for a disease with a certain **prevalence** in the population. The test has a **false positive rate** (the probability that a healthy person tests positive) and a **sensitivity** (the probability that a sick person tests positive). Given a positive test result, we want to know the probability that the person actually has the disease.""")
st.markdown(r"""""")
st.markdown(r""" We use **Bayes' Theorem** to compute this:""")
st.markdown(r"""""")
st.latex(r""" P(\text{{Disease}} | \text{{Positive}}) = \frac{P(\text{{Positive}} | \text{{Disease}}) \times P(\text{{Disease}})}{P(\text{{Positive}})}""")
st.markdown(r"""""")
st.write(" Where:")
st.write("- P(Positive | Disease) is the sensitivity of the test.")
st.write("- P(Disease) is the prevalence of the disease.")
st.write("- P(Positive | No Disease) is the false positive rate.")
st.write("- P(Positive) is the overall probability of a positive test, which combines both true positives and false positives.")
st.markdown(r""" """)
st.latex(r""" P(\text{{Positive}}) = P(\text{{Positive}} | \text{{Disease}}) \times P(\text{{Disease}}) + P(\text{{Positive}} | \text{{No Disease}}) \times P(\text{{No Disease}})""")
st.markdown(r"""""")
st.markdown(r""" We will calculate the probability that a person who tests positive actually has the disease using these inputs.""")
st.markdown(r""" """)
# Additional explanation on the result
st.markdown("""
#### Additional Explanation:
The result tells you the likelihood that the person has the disease if they test positive. Even with a high sensitivity, if the prevalence of the disease is very low and the false positive rate is not negligible, the actual probability that a positive result indicates disease can still be quite low. This is because, in a large population of healthy people, even a small false positive rate can lead to many false positives.
""")
def main_se_sp():
tp, fp, fn, tn = interface_test_results()
prevalentie, number_of_tested_people, alpha = interface_left_bar()
se,sp, acc = calculate_se_sp(tp, fp, fn, tn, alpha)
fdr, for_, pos, fpr = calculate( se,sp, prevalentie, number_of_tested_people, True)
if __name__ == "__main__":
main()