-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp3.txt
More file actions
716 lines (642 loc) · 36.3 KB
/
app3.txt
File metadata and controls
716 lines (642 loc) · 36.3 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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
import uvicorn
import base64
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import random
from statsmodels.stats.power import TTestIndPower
from collections import Counter
from fastapi import FastAPI
from simulation import simulation
#from power_analysis import power_analysis
from fastapi.responses import StreamingResponse,HTMLResponse
from fastapi import FastAPI, UploadFile,File
from pydantic import BaseModel
import io
import requests
#from flask import Flask, request
app = FastAPI()
@app.get('/')
def index():
return {'message': 'Hello, World'}
@app.post('/simulate_dataset')
def create_dataset2(data:simulation):
"""
User Defined Inputs:
1.size=Population Sample size
2.percentage_alcoholism/depression/tobacco=Percentage of the the respective condition in the population
3.percentage_alcoholism_depression(and other dual variable overlaps)= Percentage of the overlap of respective two condtions in the populations
4.percentage_tobacco_alcoholism_depression= Percentage of the overlap of all 3 cases in the population
5.age, gender, bmi, edu: A list consisting of the percentages of distribution in the various categorical buckets for the following variables:
eg; age=[18,60,35,15]:min,max,mean,std dev
age:4 elements; gender: 2 buckets; bmi: 3 buckets; edu: 4 buckets
6.Treatment_noth/treatment_1_conditions=The percentage of good treatment outcomes for the population with the following number of conditions
"""
print(type(data.dict))
try:
data= data.dict()
size = data['size']
percentage_alc_only = data['percentage_alc_only']
percentage_dep_only = data['percentage_dep_only']
percentage_tobacco_only = data['percentage_tobacco_only']
percentage_alc_dep = data['percentage_alc_dep']
percentage_alc_tobacco = data['percentage_alc_tobacco']
percentage_dep_tobacco = data['percentage_dep_tobacco']
percentage_tobacco_alcoholism_depression = data['percentage_tobacco_alcoholism_depression']
treatment_noth = data['treatment_noth']
treatment_1_conditions = data['treatment_1_conditions']
treatment_2_conditions = data['treatment_2_conditions']
treatment_3_conditions = data['treatment_3_conditions']
male= data['male']
female=data['female']
low_bmi=data['low_bmi']
normal_bmi=data['normal_bmi']
high_bmi=data['high_bmi']
l1_edu=data['l1_edu']
l2_edu=data['l2_edu']
l3_edu=data['l3_edu']
l4_edu=data['l4_edu']
min_age=data['min_age']
max_age=data['max_age']
mean_age=data['mean_age']
sd_age=data['sd_age']
seed=data['seed']
gender=list()
bmi=list()
age=list()
edu=list()
gender.extend([male,female])
bmi.extend([low_bmi,normal_bmi,high_bmi])
edu.extend([l1_edu,l2_edu,l3_edu,l4_edu])
age.extend([min_age,max_age,mean_age,sd_age])
zeros=1-(percentage_alc_only + percentage_dep_only + percentage_tobacco_only + percentage_alc_dep + percentage_alc_tobacco + percentage_dep_tobacco + percentage_tobacco_alcoholism_depression)
print("zeros:{}".format(zeros))
np.random.seed(2020)
condition=np.random.choice(8,size,p=[zeros, percentage_alc_only, percentage_dep_only,percentage_alc_dep,percentage_tobacco_only,percentage_alc_tobacco, percentage_dep_tobacco, percentage_tobacco_alcoholism_depression])
alcoholism=np.where((condition == 1) | (condition==3) | (condition==5) | (condition==7),1,0)
depression=np.where((condition == 2) | (condition==3) | (condition==6) | (condition==7),1,0)
alc_only=np.where(condition == 1,1,0)
dep_only=np.where(condition == 2,1,0)
tobacco_only=np.where(condition == 4,1,0)
alc_dep=np.where(condition == 3,1,0)
alc_tobacco=np.where(condition == 5,1,0)
dep_tobacco=np.where(condition == 6,1,0)
tobacco=np.where((condition == 4) | (condition==5) | (condition==6) | (condition==7),1,0)
all_three=np.where(condition == 7,1,0)
age_1= np.random.normal(loc=age[2], scale=age[3], size=size)
age_1=np.clip(age_1, a_min=age[0], a_max=age[1])
age_1 = np.round(age_1).astype(int)
sex=np.random.choice(2,size,p=[gender[0],gender[1]])
body_mass=np.random.choice(3,size,p=[bmi[0],bmi[1],bmi[2]])
education=np.random.choice(4,size,p=[edu[0],edu[1],edu[2],edu[3]])
cavitation = np.random.choice(2,size,p=[0.5,0.5])
ttd = np.random.normal(loc=7, scale=3, size=size)
df = pd.DataFrame(
{
'idx': np.arange(1, size+1),
'age': age_1,
'gender': sex,
'bmi': body_mass,
'education': education,
'cavitation': cavitation,
'TTD': ttd,
'alcoholism': alcoholism,
'depression': depression,
'tobacco': tobacco,
'alcohol_only':alc_only,
'depression_only':dep_only,
'tobacco_only': tobacco_only,
'alcoholism+depression':alc_dep,
'alcoholism+tobacco':alc_tobacco,
'depression+tobacco':dep_tobacco,
'tobacco+alcohol+smoking':all_three
}
)
intervention_arr=[]
choices_alc_only=['NAlc','A']
choices_dep_only=['ND','D']
choices_tobacco_only=['NT','T']
choices_alc_dep_only=['NAD','AD']
choices_alc_tobacco_only=['NAT','AT']
choices_dep_tobacco_only=['NDT','DT']
choices_all_3=['NADT','ADT']
random.seed(seed)
weights=[0.5,0.5]
for i in range(size):
if(df['alcohol_only'][i]==1):
#intervention_arr.append(np.random.binomial(1,0.5,size=1))
intervention_arr.append(random.choices(choices_alc_only,weights=weights)[0])
if(df['depression_only'][i]==1):
intervention_arr.append(random.choices(choices_dep_only,weights=weights)[0])
if(df['tobacco_only'][i]==1):
intervention_arr.append(random.choices(choices_tobacco_only,weights=weights)[0])
if(df['alcoholism+depression'][i]==1):
intervention_arr.append(random.choices(choices_alc_dep_only,weights=weights)[0])
if(df['alcoholism+tobacco'][i]==1):
intervention_arr.append(random.choices(choices_alc_tobacco_only,weights=weights)[0])
if(df['depression+tobacco'][i]==1):
intervention_arr.append(random.choices(choices_dep_tobacco_only,weights=weights)[0])
if(df['tobacco+alcohol+smoking'][i]==1):
intervention_arr.append(random.choices(choices_all_3,weights=weights)[0])
if(df['alcoholism'][i]==0 and df['depression'][i]==0 and df['tobacco'][i]==0):
intervention_arr.append('UNAFFECTED')
df['Intervention'] = intervention_arr
#print(np.unique(intervention_arr,return_councentage_ts=True))
df['treatment_outcomes'] = " "
treatment_outcomes_single_ni = []
treatment_outcomes_two_ni = []
treatment_outcomes_three_ni = []
treatment_outcomes_i = []
#treatment_outcomes_noth = []
list_noth = list(np.where(df['Intervention'] == 'UNAFFECTED')[0])
values_noth = np.random.choice(2,len(list_noth),p=[1-treatment_noth,treatment_noth])
for i in range(len(list_noth)):
df.loc[list_noth[i],"treatment_outcomes"] = values_noth[i]
list_single_ni = list(np.where((df['Intervention'] == 'NAlc') | (df['Intervention'] == 'ND') | (df['Intervention'] == 'NT'))[0])
values_single_ni = np.random.choice(2,len(list_single_ni),p=[1-treatment_1_conditions,treatment_1_conditions])
for i in range(len(list_single_ni)):
df.loc[list_single_ni[i],"treatment_outcomes"] = values_single_ni[i]
list_two_ni = list(np.where((df['Intervention'] == 'NAD') | (df['Intervention'] == 'NDT') | (df['Intervention'] == 'NAT'))[0])
values_two_ni = np.random.choice(2,len(list_two_ni),p=[1-treatment_2_conditions,treatment_2_conditions])
for i in range(len(list_two_ni)):
df.loc[list_two_ni[i],"treatment_outcomes"] = values_two_ni[i]
list_three_ni = list(np.where(df['Intervention'] == 'NADT')[0])
values_three_ni = np.random.choice(2,len(list_three_ni),p=[1-treatment_3_conditions,treatment_3_conditions])
for i in range(len(list_three_ni)):
df.loc[list_three_ni[i],"treatment_outcomes"] = values_three_ni[i]
list_single_inter=list(np.where((df['Intervention'] == 'A') | (df['Intervention'] == 'D') | (df['Intervention'] == 'T'))[0])
s_int=(1-treatment_1_conditions)/2
values_single_inter = np.random.choice(2,len(list_single_inter),p=[s_int,treatment_1_conditions+s_int])
for i in range(len(list_single_inter)):
df.loc[list_single_inter[i],"treatment_outcomes"] = values_single_inter[i]
list_double_inter=list(np.where((df['Intervention'] == 'AD') | (df['Intervention'] == 'DT') | (df['Intervention'] == 'AT'))[0])
d_int=(1-treatment_2_conditions)/2
values_double_inter = np.random.choice(2,len(list_double_inter),p=[d_int,treatment_2_conditions + d_int])
for i in range(len(list_double_inter)):
df.loc[list_double_inter[i],"treatment_outcomes"] = values_double_inter[i]
list_triple_inter=list(np.where((df['Intervention'] == 'ADT'))[0])
t_int = (1-treatment_3_conditions)/2
values_triple_inter = np.random.choice(2,len(list_triple_inter),p=[t_int,treatment_3_conditions+ t_int])
for i in range(len(list_triple_inter)):
df.loc[list_triple_inter[i],"treatment_outcomes"] = values_triple_inter[i]
return StreamingResponse(
iter([df.to_csv(index=False)]),
media_type="text/csv",
headers={"Content-Disposition": f"attachment; filename=init_dataset.csv"})
except Exception as ex:
print(ex)
def create_dataset(size,percentage_alc_only, percentage_dep_only, percentage_tobacco_only,percentage_alc_dep, percentage_alc_tobacco,percentage_dep_tobacco,
percentage_tobacco_alcoholism_depression,treatment_noth,treatment_1_conditions,treatment_2_conditions,treatment_3_conditions,
gender,bmi,edu,seed,age):
"""
User Defined Inputs:
1.size=Population Sample size
2.percentage_alcoholism/depression/tobacco=Percentage of the the respective condition in the population
3.percentage_alcoholism_depression(and other dual variable overlaps)= Percentage of the overlap of respective two condtions in the populations
4.percentage_tobacco_alcoholism_depression= Percentage of the overlap of all 3 cases in the population
5.gender, bmi, edu: A list consisting of the percentages of distribution in the various categorical buckets for the following variables:
eg;
buckets; gender: 2 buckets; bmi: 3 buckets; edu: 4 buckets
6.age has 4 parameters - a_min, a_max, mean, std dev
6.Treatment_noth/treatment_1_conditions=The percentage of good treatment outcomes for the population with the following number of conditions
"""
zeros=1-(percentage_alc_only + percentage_dep_only + percentage_tobacco_only + percentage_alc_dep + percentage_alc_tobacco + percentage_dep_tobacco + percentage_tobacco_alcoholism_depression)
print("zeros:{}".format(zeros))
np.random.seed(2020)
condition=np.random.choice(8,size,p=[zeros, percentage_alc_only, percentage_dep_only,percentage_alc_dep,percentage_tobacco_only,percentage_alc_tobacco, percentage_dep_tobacco, percentage_tobacco_alcoholism_depression])
alcoholism=np.where((condition == 1) | (condition==3) | (condition==5) | (condition==7),1,0)
depression=np.where((condition == 2) | (condition==3) | (condition==6) | (condition==7),1,0)
alc_only=np.where(condition == 1,1,0)
dep_only=np.where(condition == 2,1,0)
tobacco_only=np.where(condition == 4,1,0)
alc_dep=np.where(condition == 3,1,0)
alc_tobacco=np.where(condition == 5,1,0)
dep_tobacco=np.where(condition == 6,1,0)
tobacco=np.where((condition == 4) | (condition==5) | (condition==6) | (condition==7),1,0)
all_three=np.where(condition == 7,1,0)
age_1= np.random.normal(loc=age[2], scale=age[3], size=size)
age_1=np.clip(age_1, a_min=age[0], a_max=age[1])
age_1 = np.round(age_1).astype(int)
sex=np.random.choice(2,size,p=[gender[0],gender[1]])
body_mass=np.random.choice(3,size,p=[bmi[0],bmi[1],bmi[2]])
education=np.random.choice(4,size,p=[edu[0],edu[1],edu[2],edu[3]])
cavitation = np.random.choice(2,size,p=[0.5,0.5])
ttd = np.random.normal(loc=7, scale=3, size=size)
df = pd.DataFrame(
{
'idx': np.arange(1, size+1),
'age': age_1,
'gender': sex,
'bmi': body_mass,
'education': education,
'cavitation': cavitation,
'TTD': ttd,
'alcoholism': alcoholism,
'depression': depression,
'tobacco': tobacco,
'alcohol_only':alc_only,
'depression_only':dep_only,
'tobacco_only': tobacco_only,
'alcoholism+depression':alc_dep,
'alcoholism+tobacco':alc_tobacco,
'depression+tobacco':dep_tobacco,
'tobacco+alcohol+smoking':all_three
}
)
intervention_arr=[]
choices_alc_only=['NAlc','A']
choices_dep_only=['ND','D']
choices_tobacco_only=['NT','T']
choices_alc_dep_only=['NAD','AD']
choices_alc_tobacco_only=['NAT','AT']
choices_dep_tobacco_only=['NDT','DT']
choices_all_3=['NADT','ADT']
random.seed(seed)
weights=[0.5,0.5]
for i in range(size):
if(df['alcohol_only'][i]==1):
#intervention_arr.append(np.random.binomial(1,0.5,size=1))
intervention_arr.append(random.choices(choices_alc_only,weights=weights)[0])
if(df['depression_only'][i]==1):
intervention_arr.append(random.choices(choices_dep_only,weights=weights)[0])
if(df['tobacco_only'][i]==1):
intervention_arr.append(random.choices(choices_tobacco_only,weights=weights)[0])
if(df['alcoholism+depression'][i]==1):
intervention_arr.append(random.choices(choices_alc_dep_only,weights=weights)[0])
if(df['alcoholism+tobacco'][i]==1):
intervention_arr.append(random.choices(choices_alc_tobacco_only,weights=weights)[0])
if(df['depression+tobacco'][i]==1):
intervention_arr.append(random.choices(choices_dep_tobacco_only,weights=weights)[0])
if(df['tobacco+alcohol+smoking'][i]==1):
intervention_arr.append(random.choices(choices_all_3,weights=weights)[0])
if(df['alcoholism'][i]==0 and df['depression'][i]==0 and df['tobacco'][i]==0):
intervention_arr.append('UNAFFECTED')
df['Intervention'] = intervention_arr
#print(np.unique(intervention_arr,return_councentage_ts=True))
df['treatment_outcomes'] = " "
treatment_outcomes_single_ni = []
treatment_outcomes_two_ni = []
treatment_outcomes_three_ni = []
treatment_outcomes_i = []
#treatment_outcomes_noth = []
list_noth = list(np.where(df['Intervention'] == 'UNAFFECTED')[0])
values_noth = np.random.choice(2,len(list_noth),p=[1-treatment_noth,treatment_noth])
for i in range(len(list_noth)):
df.loc[list_noth[i],"treatment_outcomes"] = values_noth[i]
list_single_ni = list(np.where((df['Intervention'] == 'NAlc') | (df['Intervention'] == 'ND') | (df['Intervention'] == 'NT'))[0])
values_single_ni = np.random.choice(2,len(list_single_ni),p=[1-treatment_1_conditions,treatment_1_conditions])
for i in range(len(list_single_ni)):
df.loc[list_single_ni[i],"treatment_outcomes"] = values_single_ni[i]
list_two_ni = list(np.where((df['Intervention'] == 'NAD') | (df['Intervention'] == 'NDT') | (df['Intervention'] == 'NAT'))[0])
values_two_ni = np.random.choice(2,len(list_two_ni),p=[1-treatment_2_conditions,treatment_2_conditions])
for i in range(len(list_two_ni)):
df.loc[list_two_ni[i],"treatment_outcomes"] = values_two_ni[i]
list_three_ni = list(np.where(df['Intervention'] == 'NADT')[0])
values_three_ni = np.random.choice(2,len(list_three_ni),p=[1-treatment_3_conditions,treatment_3_conditions])
for i in range(len(list_three_ni)):
df.loc[list_three_ni[i],"treatment_outcomes"] = values_three_ni[i]
#list_i = list(np.where((df['Intervention'] == 'A') | (df['Intervention'] == 'D') | (df['Intervention'] == 'T') | (df['Intervention'] == 'AD') | (df['Intervention'] == 'AT') | (df['Intervention'] == 'DT') | (df['Intervention'] == 'ADT'))[0])
#values_i = np.random.choice(2,len(list_i),p=[1-treatment_intervention,treatment_intervention])
#for i in range(len(list_i)):
# df.loc[list_i[i],"treatment_outcomes"] = values_i[i]
list_single_inter=list(np.where((df['Intervention'] == 'A') | (df['Intervention'] == 'D') | (df['Intervention'] == 'T'))[0])
s_int=(1-treatment_1_conditions)/2
values_single_inter = np.random.choice(2,len(list_single_inter),p=[s_int,treatment_1_conditions+s_int])
for i in range(len(list_single_inter)):
df.loc[list_single_inter[i],"treatment_outcomes"] = values_single_inter[i]
list_double_inter=list(np.where((df['Intervention'] == 'AD') | (df['Intervention'] == 'DT') | (df['Intervention'] == 'AT'))[0])
d_int=(1-treatment_2_conditions)/2
values_double_inter = np.random.choice(2,len(list_double_inter),p=[d_int,treatment_2_conditions + d_int])
for i in range(len(list_double_inter)):
df.loc[list_double_inter[i],"treatment_outcomes"] = values_double_inter[i]
list_triple_inter=list(np.where((df['Intervention'] == 'ADT'))[0])
t_int = (1-treatment_3_conditions)/2
values_triple_inter = np.random.choice(2,len(list_triple_inter),p=[t_int,treatment_3_conditions+ t_int])
for i in range(len(list_triple_inter)):
df.loc[list_triple_inter[i],"treatment_outcomes"] = values_triple_inter[i]
return df
#@app.post('/calc_power')
def calculate_power(x:str,y:str,file:UploadFile=File(...)):
""" x = value of treatment variable (gives us idea of the population - NAlc, a, nt, t, adt, etc)
y = value of control variable """
try:
df = pd.read_csv(file.file)
file.file.close
#print(df.head())
power_analysis=TTestIndPower()
treatment_arr=[]
control_arr=[]
treatment_locs=np.where((df['Intervention']==x))
control_locs=np.where((df['Intervention']==y))
for i in treatment_locs:
treatment_arr.append(df['treatment_outcomes'].iloc[i])
for j in control_locs:
control_arr.append(df['treatment_outcomes'].iloc[j])
l1 = len(treatment_arr[0])
l2 = len(control_arr[0])
index_treatment=np.arange(0,l1)
index_control=np.arange(0,l2)
treatment_df=pd.DataFrame({'idx':index_treatment,"Treatment":treatment_arr[0]})
control_df=pd.DataFrame({'idx':index_control,"Control":control_arr[0]})
mu1=treatment_df['Treatment'].mean()
mu2=control_df['Control'].mean()
std1=treatment_df['Treatment'].std()
std2=control_df['Control'].std()
s = np.sqrt(((l1 - 1) * std1 + (l2 - 1) * std2) / (l1 + l2 - 2))
d = (mu1 - mu2) / s #cohen's effect size
eff = round(d,2)
p = power_analysis.power(effect_size=eff,alpha=0.05,nobs1=l1,ratio=(l1/l2),alternative='two-sided')
return p
except Exception as ex:
print(ex)
def calculate_power1(x, y, df):
power_analysis=TTestIndPower()
treatment_arr=[]
control_arr=[]
treatment_locs=np.where((df['Intervention']==x))
control_locs=np.where((df['Intervention']==y))
for i in treatment_locs:
treatment_arr.append(df['treatment_outcomes'].iloc[i])
for j in control_locs:
control_arr.append(df['treatment_outcomes'].iloc[j])
l1 = len(treatment_arr[0])
l2 = len(control_arr[0])
index_treatment=np.arange(0,l1)
index_control=np.arange(0,l2)
treatment_df=pd.DataFrame({'idx':index_treatment,"Treatment":treatment_arr[0]})
control_df=pd.DataFrame({'idx':index_control,"Control":control_arr[0]})
mu1=treatment_df['Treatment'].mean()
mu2=control_df['Control'].mean()
std1=treatment_df['Treatment'].std()
std2=control_df['Control'].std()
s = np.sqrt(((l1 - 1) * std1 + (l2 - 1) * std2) / (l1 + l2 - 2))
d = (mu1 - mu2) / s #cohen's effect size
eff = round(d,2)
p = power_analysis.power(effect_size=eff,alpha=0.05,nobs1=l1,ratio=(l1/l2),alternative='two-sided')
return p
@app.post('/check_power')
async def checks_power(file:UploadFile=File(...)):
''' Calculates the power for each risk factor group in the population '''
try:
dfx = pd.read_csv(file.file)
file.file.close
sample_sizes={}
sample_sizes['Alcohol']=calculate_power1('A','NAlc',dfx)
sample_sizes['Depression']=calculate_power1('D','ND',dfx)
sample_sizes['Tobacco']=calculate_power1('T','NT',dfx)
sample_sizes['Alcohol-Depression']=calculate_power1('AD','NAD',dfx)
sample_sizes['Alcohol-Tobacco']=calculate_power1('AT','NAT',dfx)
sample_sizes['Depression-Tobacco']=calculate_power1('DT','NDT',dfx)
sample_sizes['Alcohol-Depression-Tobacco']=calculate_power1('ADT','NADT',dfx)
#for i in sample_sizes.values():
all_above_threshold = all(value >= 0.8 for value in sample_sizes.values())
print(all_above_threshold)
print(sample_sizes.values())
return {'Current Power for groups':list(sample_sizes.items())}
except Exception as ex:
print(ex)
# ch
# eck_sample_sizes(df_2)
@app.get("/find_ideal_samples", response_class=HTMLResponse)
async def get_links():
urls = [
"https://colab.research.google.com/drive/1CUXdWtmOlpdMTCxPU-l-p80N2H4K-KFh?usp=sharing",
"https://drive.google.com/drive/folders/1AsRdkL3UpydRKNGkUqwth0yZia4P9_1K?usp=sharing",
]
website_contents = ""
for url in urls:
website_contents += f"<a href='{url}' style='color: blue;'>{url}</a><br><br>"
return website_contents
def try_sample_sizes3(dfx):
sample_sizes = {}
sample_sizes['Alcohol'] = calculate_power1('A', 'NAlc', dfx)
sample_sizes['Depression'] = calculate_power1('D', 'ND', dfx)
sample_sizes['Tobacco'] = calculate_power1('T', 'NT', dfx)
sample_sizes['Alcohol-Depression'] = calculate_power1('AD', 'NAD', dfx)
sample_sizes['Alcohol-Tobacco'] = calculate_power1('AT', 'NAT', dfx)
sample_sizes['Depression-Tobacco'] = calculate_power1('DT', 'NDT', dfx)
sample_sizes['Alcohol-Depression-Tobacco'] = calculate_power1('ADT', 'NADT', dfx)
size = dfx.shape[0]
#total_power = sum(sample_sizes.values())
print(sample_sizes.values())
all_above_threshold = all(value >= 0.79 for value in sample_sizes.values())
print(all_above_threshold)
#print(type(dfx))
print(type(dfx.shape[0]))
if not all_above_threshold:
if sample_sizes['Alcohol'] < 0.79:
size += 100
elif sample_sizes['Depression'] < 0.79:
size += 100
elif sample_sizes['Tobacco'] < 0.79:
size += 100
elif sample_sizes['Alcohol-Depression'] < 0.79:
size += 80
elif sample_sizes['Alcohol-Tobacco'] < 0.79:
size += 80
elif sample_sizes['Depression-Tobacco'] < 0.79:
size += 80
elif sample_sizes['Alcohol-Depression-Tobacco'] < 0.79:
size += 20
ratio_alc_only = round(dfx['Intervention'].value_counts(normalize=True)['A']+dfx['Intervention'].value_counts(normalize=True)['NAlc'],2)
ratio_dep_only = round(dfx['Intervention'].value_counts(normalize=True)['D']+dfx['Intervention'].value_counts(normalize=True)['ND'],2)
ratio_tob_only = round(dfx['Intervention'].value_counts(normalize=True)['T']+dfx['Intervention'].value_counts(normalize=True)['NT'],2)
ratio_at = round(dfx['Intervention'].value_counts(normalize=True)['AT']+dfx['Intervention'].value_counts(normalize=True)['NAT'],2)
ratio_ad = round(dfx['Intervention'].value_counts(normalize=True)['AD']+dfx['Intervention'].value_counts(normalize=True)['NAD'],2)
ratio_dt = round(dfx['Intervention'].value_counts(normalize=True)['DT']+dfx['Intervention'].value_counts(normalize=True)['NDT'],2)
ratio_adt = round(dfx['Intervention'].value_counts(normalize=True)['ADT']+dfx['Intervention'].value_counts(normalize=True)['NADT'],2)
#print(dfx['Intervention'].value_counts(normalize=True))
print(dfx.shape[0])
#dfx = create_dataset2(size, 0.08, 0.08, 0.08, 0.04, 0.04, 0.04, 0.03, 0.9, 0.80, 0.70, 0.60, gender=[0.5, 0.5], bmi=[0.2,0.5,0.3],edu=[0.1,0.2,0.2,0.5],seed=52)
print('inside growing')
dfx = create_dataset(size, ratio_alc_only, ratio_dep_only, ratio_tob_only, ratio_ad, ratio_at, ratio_dt, ratio_adt, 0.95, 0.90, 0.85, 0.80, gender=[0.5, 0.5], bmi=[0.2,0.5,0.3],edu=[0.1,0.2,0.2,0.5],age=[18,60,35,15],seed=52)
dfx = try_sample_sizes3(dfx)
while all(value > 0.85 for value in sample_sizes.values()):
print('inside shrinking')
#for group in sample_sizes.keys:
#if sample_sizes[group] > 0.95:
# size -= 1500
if sample_sizes['Alcohol'] > 0.95:
size -= 1500
elif sample_sizes['Tobacco'] > 0.95:
size -= 1500
elif sample_sizes['Depression'] > 0.95:
size -= 1500
ratio_alc_only = round(dfx['Intervention'].value_counts(normalize=True)['A']+dfx['Intervention'].value_counts(normalize=True)['NAlc'],2)
ratio_dep_only = round(dfx['Intervention'].value_counts(normalize=True)['D']+dfx['Intervention'].value_counts(normalize=True)['ND'],2)
ratio_tob_only = round(dfx['Intervention'].value_counts(normalize=True)['T']+dfx['Intervention'].value_counts(normalize=True)['NT'],2)
ratio_at = round(dfx['Intervention'].value_counts(normalize=True)['AT']+dfx['Intervention'].value_counts(normalize=True)['NAT'],2)
ratio_ad = round(dfx['Intervention'].value_counts(normalize=True)['AD']+dfx['Intervention'].value_counts(normalize=True)['NAD'],2)
ratio_dt = round(dfx['Intervention'].value_counts(normalize=True)['DT']+dfx['Intervention'].value_counts(normalize=True)['NDT'],2)
ratio_adt = round(dfx['Intervention'].value_counts(normalize=True)['ADT']+dfx['Intervention'].value_counts(normalize=True)['NADT'],2)
dfx = create_dataset(size, ratio_alc_only, ratio_dep_only, ratio_tob_only, ratio_ad, ratio_at, ratio_dt, ratio_adt, 0.95, 0.90, 0.85, 0.80, gender=[0.5, 0.5], bmi=[0.2,0.5,0.3],edu=[0.1,0.2,0.2,0.5],age=[18,60,35,15],seed=52)
#dfx = create_dataset2(size, 0.08, 0.08, 0.08, 0.04, 0.04, 0.04, 0.03, 0.9, 0.80, 0.70, 0.60, gender=[0.5, 0.5], bmi=[0.2,0.5,0.3],edu=[0.1,0.2,0.2,0.5],seed=52)
dfx = try_sample_sizes3(dfx)
return dfx
#@app.post('/try_samp_sizes')
def process_file(file:UploadFile = File(...)):
try:
dfx=pd.read_csv(file.file)
dfx1 = try_sample_sizes3(dfx)
#csvx = dfx.to_csv(index = False)
return StreamingResponse(
iter([dfx1.to_csv(index=False)]),
media_type="text/csv",
headers={"Content-Disposition": f"attachment; filename=resim_screening.csv"})
except Exception as ex:
print(ex)
def try_sample_sizes4(file:UploadFile=File(...)):
try:
dfx = pd.read_csv(file.file)
file.file.close
#dfx = pd.read_csv(io.BytesIO(contents), encoding='utf-8')
#file.file.close
sample_sizes = {}
sample_sizes['Alcohol'] = calculate_power1('A', 'NAlc', dfx)
sample_sizes['Depression'] = calculate_power1('D', 'ND', dfx)
sample_sizes['Tobacco'] = calculate_power1('T', 'NT', dfx)
sample_sizes['Alcohol-Depression'] = calculate_power1('AD', 'NAD', dfx)
sample_sizes['Alcohol-Tobacco'] = calculate_power1('AT', 'NAT', dfx)
sample_sizes['Depression-Tobacco'] = calculate_power1('DT', 'NDT', dfx)
sample_sizes['Alcohol-Depression-Tobacco'] = calculate_power1('ADT', 'NADT', dfx)
size = dfx.shape[0]
#total_power = sum(sample_sizes.values())
all_above_threshold = all(value >= 0.79 for value in sample_sizes.values())
if not all_above_threshold:
if sample_sizes['Alcohol'] < 0.79:
size += 100
elif sample_sizes['Depression'] < 0.79:
size += 100
elif sample_sizes['Tobacco'] < 0.79:
size += 100
elif sample_sizes['Alcohol-Depression'] < 0.79:
size += 80
elif sample_sizes['Alcohol-Tobacco'] < 0.79:
size += 80
elif sample_sizes['Depression-Tobacco'] < 0.79:
size += 80
elif sample_sizes['Alcohol-Depression-Tobacco'] < 0.79:
size += 20
ratio_alc_only = round(dfx['Intervention'].value_counts(normalize=True)['A']+dfx['Intervention'].value_counts(normalize=True)['NAlc'],2)
ratio_dep_only = round(dfx['Intervention'].value_counts(normalize=True)['D']+dfx['Intervention'].value_counts(normalize=True)['ND'],2)
ratio_tob_only = round(dfx['Intervention'].value_counts(normalize=True)['T']+dfx['Intervention'].value_counts(normalize=True)['NT'],2)
ratio_at = round(dfx['Intervention'].value_counts(normalize=True)['AT']+dfx['Intervention'].value_counts(normalize=True)['NAT'],2)
ratio_ad = round(dfx['Intervention'].value_counts(normalize=True)['AD']+dfx['Intervention'].value_counts(normalize=True)['NAD'],2)
ratio_dt = round(dfx['Intervention'].value_counts(normalize=True)['DT']+dfx['Intervention'].value_counts(normalize=True)['NDT'],2)
ratio_adt = round(dfx['Intervention'].value_counts(normalize=True)['ADT']+dfx['Intervention'].value_counts(normalize=True)['NADT'],2)
#dfx = create_dataset2(size, 0.08, 0.08, 0.08, 0.04, 0.04, 0.04, 0.03, 0.9, 0.80, 0.70, 0.60, gender=[0.5, 0.5], bmi=[0.2,0.5,0.3],edu=[0.1,0.2,0.2,0.5],seed=52)
dfx = create_dataset(size, ratio_alc_only, ratio_dep_only, ratio_tob_only, ratio_ad, ratio_at, ratio_dt, ratio_adt, 0.95, 0.90, 0.85, 0.80, gender=[0.5, 0.5], bmi=[0.2,0.5,0.3],edu=[0.1,0.2,0.2,0.5],seed=52,age=[18,60,35,15])
return try_sample_sizes3(dfx)
while all(value > 0.85 for value in sample_sizes.values()):
print('inside shrinking')
#for group in sample_sizes.keys:
#if sample_sizes[group] > 0.95:
# size -= 1500
if sample_sizes['Alcohol'] > 0.95:
size -= 1500
elif sample_sizes['Tobacco'] > 0.95:
size -= 1500
elif sample_sizes['Depression'] > 0.95:
size -= 1500
#if any(value < 0.80 for value in sample_sizes.values()):
ratio_alc_only = round(dfx['Intervention'].value_counts(normalize=True)['A']+dfx['Intervention'].value_counts(normalize=True)['NAlc'],2)
ratio_dep_only = round(dfx['Intervention'].value_counts(normalize=True)['D']+dfx['Intervention'].value_counts(normalize=True)['ND'],2)
ratio_tob_only = round(dfx['Intervention'].value_counts(normalize=True)['T']+dfx['Intervention'].value_counts(normalize=True)['NT'],2)
ratio_at = round(dfx['Intervention'].value_counts(normalize=True)['AT']+dfx['Intervention'].value_counts(normalize=True)['NAT'],2)
ratio_ad = round(dfx['Intervention'].value_counts(normalize=True)['AD']+dfx['Intervention'].value_counts(normalize=True)['NAD'],2)
ratio_dt = round(dfx['Intervention'].value_counts(normalize=True)['DT']+dfx['Intervention'].value_counts(normalize=True)['NDT'],2)
ratio_adt = round(dfx['Intervention'].value_counts(normalize=True)['ADT']+dfx['Intervention'].value_counts(normalize=True)['NADT'],2)
dfx = create_dataset(size, ratio_alc_only, ratio_dep_only, ratio_tob_only, ratio_ad, ratio_at, ratio_dt, ratio_adt, 0.95, 0.90, 0.85, 0.80, gender=[0.5, 0.5], bmi=[0.2,0.5,0.3],edu=[0.1,0.2,0.2,0.5],seed=52,age=[18,60,35,15])
#dfx = create_dataset2(size, 0.08, 0.08, 0.08, 0.04, 0.04, 0.04, 0.03, 0.9, 0.80, 0.70, 0.60, gender=[0.5, 0.5], bmi=[0.2,0.5,0.3],edu=[0.1,0.2,0.2,0.5],seed=52)
return try_sample_sizes3(dfx)
#return dfx
return StreamingResponse(
iter([dfx.to_csv(index=False)]),
media_type="text/csv",
headers={"Content-Disposition": f"attachment; filename=resim_screening.csv"})
except Exception as ex:
print(ex)
@app.post('/clinical_dataset')
async def clinical_ss(file:UploadFile=File(...)):
''' Brings the power of each subgroup in the dataset close to 80% by tweaking their sizes.'''
try:
#def create_clinical_tb(dfx):
# Get desired counts for each intervention group
dfx = pd.read_csv(file.file)
file.file.close
alc_only = round((dfx["Intervention"].value_counts()['A'] + dfx["Intervention"].value_counts()['NAlc'])/6)
dep_only = round((dfx["Intervention"].value_counts()['D'] + dfx["Intervention"].value_counts()['ND'])/6)
tob_only = round((dfx["Intervention"].value_counts()['T'] + dfx["Intervention"].value_counts()['NT'])/6)
alc_dep_only = round((dfx["Intervention"].value_counts()['AD'] + dfx["Intervention"].value_counts()['NAD'])/6)
alc_tob_only = round((dfx["Intervention"].value_counts()['AT'] + dfx["Intervention"].value_counts()['NAT'])/6)
dep_tob_only = round((dfx["Intervention"].value_counts()['DT'] + dfx["Intervention"].value_counts()['NDT'])/6)
alc_dep_tob_only = round((dfx["Intervention"].value_counts()['ADT'] + dfx["Intervention"].value_counts()['NADT'])/6)
#unaff = round((dfx["Intervention"].value_counts()['UNAFFECTED'])/3)
my_count = [alc_only, dep_only, tob_only, alc_dep_only, alc_tob_only, dep_tob_only, alc_dep_tob_only ]
count = max(my_count)
# Get current counts for each intervention group
# curr_counts = dfx["Intervention"].value_counts()
curr_alc_only = dfx[dfx['Intervention'] == 'A']
curr_dep_only = dfx[dfx['Intervention'] == 'D']
curr_tob_only = dfx[dfx['Intervention'] == 'T']
curr_alc_dep_only = dfx[dfx['Intervention'] == 'AD']
curr_alc_tob_only = dfx[dfx['Intervention'] == 'AT']
curr_dep_tob_only = dfx[dfx['Intervention'] == 'DT']
curr_alc_dep_tob_only = dfx[dfx['Intervention'] == 'ADT']
curr_Nalc_only = dfx[dfx['Intervention'] == 'NAlc']
curr_Ndep_only = dfx[dfx['Intervention'] == 'ND']
curr_Ntob_only = dfx[dfx['Intervention'] == 'NT']
curr_Nalc_dep_only = dfx[dfx['Intervention'] == 'NAD']
curr_Nalc_tob_only = dfx[dfx['Intervention'] == 'NAT']
curr_Ndep_tob_only = dfx[dfx['Intervention'] == 'NDT']
curr_Nalc_dep_tob_only = dfx[dfx['Intervention'] == 'NADT']
curr_unaff = dfx[dfx['Intervention'] == 'UNAFFECTED']
# Update Alcohol Only
new = curr_alc_only.sample(n=count)
dfx = dfx.drop(curr_alc_only.index.difference(new.index))
new = curr_Nalc_only.sample(n=count)
dfx = dfx.drop(curr_Nalc_only.index.difference(new.index))
# Update Depression Only
new = curr_dep_only.sample(n=count)
dfx = dfx.drop(curr_dep_only.index.difference(new.index))
new = curr_Ndep_only.sample(n=count)
dfx = dfx.drop(curr_Ndep_only.index.difference(new.index))
# Update Tobacco Only
new = curr_tob_only.sample(n=count)
dfx = dfx.drop(curr_tob_only.index.difference(new.index))
new = curr_Ntob_only.sample(n=count)
dfx = dfx.drop(curr_Ntob_only.index.difference(new.index))
# Update Alcohol-Depression Only
new = curr_alc_dep_only.sample(n=count)
dfx = dfx.drop(curr_alc_dep_only.index.difference(new.index))
new = curr_Nalc_dep_only.sample(n=count)
dfx = dfx.drop(curr_Nalc_dep_only.index.difference(new.index))
# Update Depression-Tobacco Only
new = curr_dep_tob_only.sample(n=count)
dfx = dfx.drop(curr_dep_tob_only.index.difference(new.index))
new = curr_Ndep_tob_only.sample(n=count)
dfx = dfx.drop(curr_Ndep_tob_only.index.difference(new.index))
# Update Alcohol-Tobacco Only
new = curr_alc_tob_only.sample(n=count)
dfx = dfx.drop(curr_alc_tob_only.index.difference(new.index))
new = curr_Nalc_tob_only.sample(n=count)
dfx = dfx.drop(curr_Nalc_tob_only.index.difference(new.index))
# Update Alcohol-Depression-Tobacco Only
new = curr_alc_dep_tob_only.sample(n=count)
dfx = dfx.drop(curr_alc_dep_tob_only.index.difference(new.index))
new = curr_Nalc_dep_tob_only.sample(n=count)
dfx = dfx.drop(curr_Nalc_dep_tob_only.index.difference(new.index))
# Update Unaffected
new = curr_unaff.sample(n=count)
dfx = dfx.drop(curr_unaff.index.difference(new.index))
size=str(dfx.shape[0])
print(size)
return StreamingResponse(iter([dfx.to_csv(index=False)]),
media_type="text/csv",
headers={"Content-Disposition": f"attachment; filename=clinical_samples_dataset.csv","Clinical-Dataset-Sample-Size":size})
except Exception as ex:
print(ex)
if __name__ == '__main__':
uvicorn.run(app, host='127.0.0.1', port=8000)