-
Notifications
You must be signed in to change notification settings - Fork 0
/
QCExe.py
3059 lines (2623 loc) · 111 KB
/
QCExe.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
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
# In[1]:
#!/usr/bin/env python
# coding: utf-8
#import pandas for dataframes, import seaborn for graphs, import csv, import os for file handling
get_ipython().run_line_magic('matplotlib', 'inline')
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning) #suppress futurewarnings from matplotlib
import pandas as pd
import seaborn as sns
import os
import webbrowser
from scipy import stats as st
import csv
from csv import writer
from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
sns.set_theme(style="darkgrid")
sns.set(font_scale = 1.5)
global df
csv_name = 'pipeline_outputs_SUB_10-15-2021.csv'#INPUT csv name here
df = pd.read_csv(csv_name) #import csv
df_data = pd.read_csv("data.csv") #second data sheet
df_data.drop(['BMI','Sex','Age'], axis=1,inplace =True)
df = df.merge(df_data, on="subject_id", how = 'left')
list_outliers = {} #global list of outliers for later use
study_name = "SUB" #INPUT study name
import dateutil.parser as dparser
date = dparser.parse(csv_name,fuzzy=True).strftime("%m/%d/%Y") #date updated (mm/dd/yyyy), extracted from csv_name
<<<<<<< HEAD
# In[23]:
#Heatmap
# corr = df.corr()
# corr_thresh = corr[(corr>.975) & (corr<1)]
# corr_thresh.fillna(value = 0,inplace=True)
# for col in corr_thresh:
# counter = len(corr_thresh[col])
# for item in corr_thresh[col]:
# if item ==0:
# counter-=1
# if counter == 0:
# corr_thresh.drop(col,axis=1,inplace=True)
# corr_thresh.drop(col,axis=0,inplace=True)
# corr2 = corr_thresh.corr()
# fig, ax = plt.subplots(figsize=(75,75))
# ax = sns.heatmap(corr2)
# fig1 = ax.get_figure()
# fig1.savefig('heatmap_95.png') #save each plot in local folder
# plt.close(fig1)
# fig, ax = plt.subplots(figsize=(100,100))
# ax = sns.heatmap(corr)
# fig2 = ax.get_figure()
# fig2.savefig('heatmap_total.png') #save each plot in local folder
# plt.close(fig2)
=======
>>>>>>> 1c8a9f085f1a281c7b21cc13b9e4b521a4f3dd74
# In[24]:
#drop unneeded columns - FOR OTHER STUDIES, UNCOMMENT
# col_name=""
#--------------------------------------------------------------------------------------------------------------------
# with open('') as f: #INPUT variables_to_remove here (ensure file in local folder, or insert path)
# col_name = f.read()
# col_toRemove = col_name.split(", ")
# for col in col_toRemove:
# if col in df.columns:
# del df[col]
#drop unneeded columns ---> FOR USE WITH PAUL'S OUTPUTS ONLY, COMMENT OUT FOR OTHER STUDIES
for col in df.columns:
if (('GlobalEfficiency' in col) or ('MaximizedModularity' in col)
or ('MeanClusteringCoeff' in col) or ('MeanTotalStrength'in col)
or ('NetworkCharacteristic' in col) or ('TotalStrength' in col)
or ('dummyrest' in col) or ('session_id' in col) or ('subject_id' in col)
or ('dummy_rest' in col) or ('file_name' in col) or ('1back' in col)
or ('acq_id' in col) or ('anatomical_zstat1' in col)):
del df[col]
#filter id columns to include only integers
for id in df['id0'].dropna():
if(len(id)>3):
idnum=id[-3:]
df['id0']=df['id0'].replace(id,idnum)
# In[25]:
#create csv file for data
def write(var, col, slope, intercept, r_value, p_value, std_err, slope2=-1, intercept2=-1, r_value2=-1,
p_value2=-1, std_err2=-1, flag = False):
fname = str(var) + "_data.csv" #csv name
if(flag):
#clear csv file to update values if first time called
f = open(fname, "w+")
f.close()
title = [] #placeholder for title row
if(not isinstance(slope2,np.ndarray) and slope2 == -1): #if second set of data not given
equation = "y="+str(slope)+"*x+"+str(intercept)
values = [col,slope,intercept,r_value**2,p_value,std_err,equation]
title = ["Variable","Slope","Intercept","R^2","P_value","Std_err", "Equation"]
else: #if second set of data is given
equation = "y="+str(slope)+"*x+"+str(intercept)
equation2 = "y="+str(slope2)+"*x+"+str(intercept2)
if(var == 'MLR'): #MLR title and values are different than the rest
values = ["",col,slope,intercept,r_value**2,equation,
"",col,slope2,intercept2,r_value2**2,equation2]
title = ["Male","Variable","Slope (Age, BMI)","Intercept","R^2", "Equation",
"Female","Variable","Slope (Age, BMI)","Intercept","R^2", "Equation"]
else:
values = ["",col,slope,intercept,r_value**2,p_value,std_err,equation,
"",col,slope2,intercept2,r_value2**2,p_value2,std_err2,equation2]
title = ["Male","Variable","Slope","Intercept","R^2","p_value","Std_err", "Equation",
"Female","Variable","Slope","Intercept","R^2","p_value","Std_err", "Equation"]
with open(fname, 'a', newline='') as f_object: #open in append mode
writer_object = writer(f_object)
if(flag): #If first row, add titles
writer_object.writerow(title)
writer_object.writerow(values)
f_object.close()
# In[26]:
#SAY
#create Age and Sex/BMI and BMIbySex graphs
plt.rcParams.update({'font.size': 30})
def createGraph(col,AgeSex):
try:
std_line = df[col].std()
#create std lines, palette for colors, legend
std1 = pd.DataFrame(np.full((len(df['id0']),1),std_line+df[col].mean()),columns = ['value'])
std1 = pd.concat([df['id0'],std1],axis = 1)
std2 = pd.DataFrame(np.full((len(df['id0']),1),-1*std_line+df[col].mean()),columns = ['value'])
std2 = pd.concat([df['id0'],std2],axis = 1)
std3 = pd.DataFrame(np.full((len(df['id0']),1),2*std_line+df[col].mean()),columns = ['value'])
std3 = pd.concat([df['id0'],std3],axis = 1)
std4 = pd.DataFrame(np.full((len(df['id0']),1),-2*std_line+df[col].mean()),columns = ['value'])
std4 = pd.concat([df['id0'],std4],axis = 1)
d_std = pd.concat([std1,std2,std3,std4],axis= 0,keys=['std1', 'std2','std3','std4']).reset_index()
d_std = d_std.rename(columns={'level_0': 'line', 'level_1': 'i'})
pal = ['magenta','magenta','cyan','cyan']
ax = sns.lineplot(data = d_std,x = 'id0',y = 'value',hue = 'line', palette = pal, legend = False)
ax = sns.scatterplot(x='id0',y=col, data=df)
ax.set_xlabel('id0',fontsize=14)
ax.set_ylabel(col, fontsize=14)
ax.set_xticklabels(df['id0'],rotation=270)
fig = ax.get_figure()
fig.legend(labels=["+\u03C3","-\u03C3","+2\u03C3","-2\u03C3"])
fig.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)
fig.set_size_inches(8,6)
if(AgeSex):
fig.savefig(col+'.png') #save each plot in local folder
plt.close(fig)
if(AgeSex):
#variable over age, linear regression
ax = sns.jointplot(x='Age',y=col, data=df,kind="reg", truncate=False,
color="m", height=7)
# ax.set_xticklabels(df['Age'],rotation=270)
fig1 = ax.fig
fig1.savefig(col+'_Age.png') #save each plot in local folder
plt.close(fig1)
#variable over sex, linear regression
pal = dict(Male="#6495ED", Female="#F08080")
ax = sns.lmplot(x='Age',y=col, col="Sex", hue="Sex", data=df,
palette=pal, y_jitter=.02, truncate=False)
#ax.set_xticklabels(df['Sex'],rotation=270)
fig2 = ax.fig
fig2.set_size_inches(12,6)
fig2.savefig(col+'_Sex.png') #save each plot in local folder
plt.close(fig2)
else:
ax = sns.jointplot(x='BMI',y=col, data=df,kind="reg", truncate=False,
color="g", height=7)
# ax.set_xticklabels(df['Age'],rotation=270)
fig3 = ax.fig
fig3.set_size_inches(8,6)
fig3.savefig(col+'_BMI.png') #save each plot in local folder
plt.close(fig3)
#variable over BMI by sex, linear regression
pal = dict(Male="#6495ED", Female="#F08080")
ax = sns.lmplot(x='BMI',y=col, col="Sex", hue="Sex", data=df,
palette=pal, y_jitter=.02, truncate=False)
#ax.set_xticklabels(df['Sex'],rotation=270)
fig2 = ax.fig
fig2.set_size_inches(12,6)
fig2.savefig(col+'_BxS.png') #save each plot in local folder
plt.close(fig2)
except TypeError:
print('Graph not generated for: '+col)
except ValueError:
print("value: "+col)
# In[27]:
#SAY
#create Behvaioral Data graphs
def createBehavioralGraph(col,cols):
for var in cols:
try:
#variable over age, linear regression
ax = sns.jointplot(x=str(var),y=col, data=df,kind="reg", truncate=False,
color="m", height=7)
fig1 = ax.fig
fig1.set_size_inches(8,6)
fig1.savefig(col+'_'+var+'.png') #save each plot in local folder
plt.close(fig1)
#variable over sex, linear regression
pal = dict(Male="#6495ED", Female="#F08080")
ax = sns.lmplot(x=str(var),y=col, col="Sex", hue="Sex", data=df,
palette=pal, y_jitter=.02, truncate=False)
fig2 = ax.fig
fig2.set_size_inches(12,6)
fig2.savefig(col+'_'+var+'_Sex.png') #save each plot in local folder
plt.close(fig2)
except TypeError:
print('Graph not generated for: '+col)
except ValueError:
print("value: "+col)
# In[39]:
#3D Graph Generation
def create3DGraph(col, r_value1, r_value2, mlr1, mlr2):
try:
#Male
min1 = df_temp_Male['Age'].min()
min2 = df_temp_Male['BMI'].min()
max1 = df_temp_Male['Age'].max()
max2 = df_temp_Male['BMI'].max()
fig = plt.figure()
fig.set_size_inches(12, 6)
ax = fig.add_subplot(121,projection='3d')
ax.scatter(df_temp_Male['Age'],df_temp_Male['BMI'],df_temp_Male[col],color = 'blue')
ax.set_title(col + ":\n Male (R^2 = "+str(round(r_value1,5))+")")
ax.set_xlabel("Age")
ax.set_ylabel("BMI")
ax.set_zlabel(col)
ax.view_init(30,60)
#graph MLR (3D Regression)
line1 = np.linspace(min1, max1, 30)
line2 = np.linspace(min2, max2, 30)
xx_pred, yy_pred = np.meshgrid(line1, line2)
model = np.array([xx_pred.flatten(), yy_pred.flatten()]).T
predicted = mlr1.predict(model)
ax.plot_trisurf(xx_pred.flatten(), yy_pred.flatten(), predicted, color=(0,0,0,.3), edgecolor=(0,0,0,.2))
#Female
min1 = df_temp_Female['Age'].min()
min2 = df_temp_Female['BMI'].min()
max1 = df_temp_Female['Age'].max()
max2 = df_temp_Female['BMI'].max()
ax = fig.add_subplot(122,projection='3d')
ax.scatter(df_temp_Female['Age'],df_temp_Female['BMI'],df_temp_Female[col],color = 'red')
ax.set_title(col + ":\n Female (R^2 = "+str(round(r_value2,5))+")")
ax.set_xlabel("Age")
ax.set_ylabel("BMI")
ax.set_zlabel(col)
ax.view_init(30,60)
#graph MLR (3D Regression)
line1 = np.linspace(min1, max1, 30) # range of porosity values
line2 = np.linspace(min2, max2, 30) # range of brittleness values
xx_pred, yy_pred = np.meshgrid(line1, line2)
model = np.array([xx_pred.flatten(), yy_pred.flatten()]).T
predicted2 = mlr2.predict(model)
ax.plot_trisurf(xx_pred.flatten(), yy_pred.flatten(), predicted2, color=(0,0,0,.3), edgecolor=(0,0,0,.2))
plt.savefig(col+'_3D.png') #save each plot in local folder
plt.close(fig)
except ValueError:
print(col)
except TypeError:
print(col+" TypeError")
# In[29]:
first = True
#Make data tables
df_temp = df.copy(deep=True)
for i,col in enumerate(df.columns.drop(['id0','id0.1','id0.2','id0.3','id0.4','id0.5','id0.6','id0.7','run_id','Age','Sex', 'BMI', 'datetime'])):
#create temporary dataframes for regression equations
graphedAgeSex = False #Flag to check if graph has already been generated
graphedBMI = False #Flag to check if graph has already been generated
AgeSex = True #Boolean for whether Age and Sex graphs or BMI and BxS graphs should be generated
df_temp_Age = df_temp[df_temp['Age'].notna()]
df_temp_Age = df_temp_Age[df_temp_Age[col].notna()]
df_temp_Sex = df_temp[df_temp['Sex'].notna()]
df_temp_Male = df_temp_Sex.copy(deep=True)
df_temp_Male.drop(df_temp_Male.loc[df_temp_Male['Sex']=='Female'].index, inplace=True)
df_temp_Male = df_temp_Male[df_temp_Male[col].notna()]
df_temp_Female = df_temp_Sex.copy(deep=True)
df_temp_Female.drop(df_temp_Female.loc[df_temp_Female['Sex']=='Male'].index, inplace=True)
df_temp_Female = df_temp_Female[df_temp_Female[col].notna()]
df_temp_BMI = df_temp[df_temp['BMI'].notna()]
df_temp_BMI = df_temp_BMI[df_temp_BMI[col].notna()]
df_temp_MaleMLR = df_temp_Male.copy(deep=True)
df_temp_MaleMLR = df_temp_MaleMLR[df_temp_MaleMLR['Age'].notna()]
df_temp_MaleMLR = df_temp_MaleMLR[df_temp_MaleMLR['BMI'].notna()]
df_temp_FemaleMLR = df_temp_Female.copy(deep=True)
df_temp_FemaleMLR = df_temp_FemaleMLR[df_temp_FemaleMLR['Age'].notna()]
df_temp_FemaleMLR = df_temp_FemaleMLR[df_temp_FemaleMLR['BMI'].notna()]
#Age
if(df_temp_Age.size != 0):
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp_Age['Age'],df_temp_Age[col])
write('Age', col,slope, intercept, r_value, p_value, std_err,flag = first)
if(p_value<.05 and not graphedAgeSex):
createGraph(col,AgeSex)
graphedAgeSex = True
#Sex
if(df_temp_Male.size != 0 and df_temp_Female.size != 0 ):
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp_Male['Age'],df_temp_Male[col])
slope2, intercept2, r_value2, p_value2, std_err2 = st.linregress(df_temp_Female['Age'],df_temp_Female[col])
write('Sex', col, slope, intercept, r_value, p_value, std_err,
slope2, intercept2, r_value2, p_value2, std_err2,flag = first)
if((p_value<.05 or p_value2<.05) and not graphedAgeSex):
createGraph(col, AgeSex)
graphedAgeSex = True
#BMI
if(df_temp_BMI.size != 0):
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp_BMI['BMI'],df_temp_BMI[col])
write('BMI', col,slope, intercept, r_value, p_value, std_err,flag = first)
if(p_value<.05 and not graphedBMI):
createGraph(col, not AgeSex)
graphedBMI = True
#BxS
if(df_temp_Male.size != 0 and df_temp_Female.size != 0 ):
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp_Male['BMI'],df_temp_Male[col])
slope2, intercept2, r_value2, p_value2, std_err2 = st.linregress(df_temp_Female['BMI'],df_temp_Female[col])
write('BxS', col, slope, intercept, r_value, p_value, std_err,
slope2, intercept2, r_value2, p_value2, std_err2,flag = first)
if((p_value<.05 or p_value2<.05) and not graphedBMI):
createGraph(col, not AgeSex)
graphedBMI = True
#Multiple Linear Regression, split by sex
try:
#Male
mlr1 = LinearRegression()
mlr1.fit(df_temp_MaleMLR[['Age', 'BMI']], df_temp_MaleMLR[col])
intercept = mlr1.intercept_
slope = mlr1.coef_
r_value = mlr1.score(df_temp_MaleMLR[['Age', 'BMI']],df_temp_MaleMLR[col])
#Female
mlr2 = LinearRegression()
mlr2.fit(df_temp_FemaleMLR[['Age', 'BMI']], df_temp_FemaleMLR[col])
intercept2 = mlr2.intercept_
slope2 = mlr2.coef_
r_value2 = mlr2.score(df_temp_FemaleMLR[['Age', 'BMI']],df_temp_FemaleMLR[col])
write('MLR', col, slope, intercept, r_value, 0, 0, slope2, intercept2, r_value2, flag = first) #0,0 for p_value, stdErr
if(r_value>0.1 or r_value2>0.1): #filter graph generation by R^2>0.1
create3DGraph(col, r_value, r_value2, mlr1, mlr2)
except ValueError:
print(col)
first = False
# In[30]:
first = True
#Make data tables for measurements
for i,col in enumerate(df.columns.drop(['id0','id0.1','id0.2','id0.3','id0.4','id0.5','id0.6','id0.7','run_id','Age','Sex', 'BMI', 'datetime'])):
#create temporary dataframes for regression equations
df_temp = df.copy(deep=True)
df_temp = df_temp[df_temp[col].notna()]
df_temp = df_temp[df_temp['peakvo2_ml_gxt'].notna()]
df_temp = df_temp[df_temp['fft_4step_t1'].notna()]
df_temp = df_temp[df_temp['fft_4step_t2'].notna()]
df_temp = df_temp[df_temp['fft_stair_ds_tester1'].notna()]
df_temp = df_temp[df_temp['fft_stair_us_tester1'].notna()]
df_temp_Sex = df_temp[df_temp['Sex'].notna()]
df_temp_Male = df_temp_Sex.copy(deep=True)
df_temp_Male.drop(df_temp_Male.loc[df_temp_Male['Sex']=='Female'].index, inplace=True)
df_temp_Female = df_temp_Sex.copy(deep=True)
df_temp_Female.drop(df_temp_Female.loc[df_temp_Female['Sex']=='Male'].index, inplace=True)
cols = []
if(df_temp.size != 0):
#vo2
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp['peakvo2_ml_gxt'], df_temp[col])
write('V_O2', col,slope, intercept, r_value, p_value, std_err,flag = first)
if(p_value<.05):
cols.append('peakvo2_ml_gxt')
#four1
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp['fft_4step_t1'],df_temp[col])
write('four1', col,slope, intercept, r_value, p_value, std_err,flag = first)
if(p_value<.05 ):
cols.append('fft_4step_t1')
#four2
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp['fft_4step_t2'],df_temp[col])
write('four2', col,slope, intercept, r_value, p_value, std_err,flag = first)
if(p_value<.05 ):
cols.append('fft_4step_t2')
#stairDown
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp['fft_stair_ds_tester1'],df_temp[col])
write('stairDown', col,slope, intercept, r_value, p_value, std_err,flag = first)
if(p_value<.05 ):
cols.append('fft_stair_ds_tester1')
#stairUp
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp['fft_stair_us_tester1'],df_temp[col])
write('stairUp', col,slope, intercept, r_value, p_value, std_err,flag = first)
if(p_value<.05 ):
cols.append('fft_stair_us_tester1')
if(df_temp_Male.size != 0 and df_temp_Female.size != 0 ):
#vo2
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp_Male['peakvo2_ml_gxt'],df_temp_Male[col])
slope2, intercept2, r_value2, p_value2, std_err2 = st.linregress(df_temp_Female['peakvo2_ml_gxt'],df_temp_Female[col])
write('V_O2_Sex', col, slope, intercept, r_value, p_value, std_err,
slope2, intercept2, r_value2, p_value2, std_err2,flag = first)
if (p_value<.05 or p_value2<.05) and 'peakvo2_ml_gxt' not in cols:
cols.append('peakvo2_ml_gxt')
#four1
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp_Male['fft_4step_t1'],df_temp_Male[col])
slope2, intercept2, r_value2, p_value2, std_err2 = st.linregress(df_temp_Female['fft_4step_t1'],df_temp_Female[col])
write('four1_Sex', col, slope, intercept, r_value, p_value, std_err,
slope2, intercept2, r_value2, p_value2, std_err2,flag = first)
if(p_value<.05 or p_value2<.05) and 'fft_4step_t1' not in cols:
cols.append('fft_4step_t1')
#four2
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp_Male['fft_4step_t2'],df_temp_Male[col])
slope2, intercept2, r_value2, p_value2, std_err2 = st.linregress(df_temp_Female['fft_4step_t2'],df_temp_Female[col])
write('four2_Sex', col, slope, intercept, r_value, p_value, std_err,
slope2, intercept2, r_value2, p_value2, std_err2,flag = first)
if(p_value<.05 or p_value2<.05) and 'fft_4step_t2' not in cols:
cols.append('fft_4step_t2')
#stairDown
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp_Male['fft_stair_ds_tester1'],df_temp_Male[col])
slope2, intercept2, r_value2, p_value2, std_err2 = st.linregress(df_temp_Female['fft_stair_ds_tester1'],df_temp_Female[col])
write('stairDown_Sex', col, slope, intercept, r_value, p_value, std_err,
slope2, intercept2, r_value2, p_value2, std_err2,flag = first)
if(p_value<.05 or p_value2<.05) and 'fft_stair_ds_tester1' not in cols:
cols.append('fft_stair_ds_tester1')
#stairUp
slope, intercept, r_value, p_value, std_err = st.linregress(df_temp_Male['fft_stair_us_tester1'],df_temp_Male[col])
slope2, intercept2, r_value2, p_value2, std_err2 = st.linregress(df_temp_Female['fft_stair_us_tester1'],df_temp_Female[col])
write('stairUp_Sex', col, slope, intercept, r_value, p_value, std_err,
slope2, intercept2, r_value2, p_value2, std_err2, flag = first)
if(p_value<.05 or p_value2<.05) and 'fft_stair_us_tester1' not in cols:
cols.append('fft_stair_us_tester1')
createBehavioralGraph(col,cols)
first = False
# In[31]:
#create descriptions for different variables using keywords, return corresponding string
def desc(name):
if('snrd' in name):
return 'Dietrich’s SNR using air background as reference:<span style="color: green"> ↑</span>'
elif('snr' in name or 'tsnr' in name):
return 'Signal-to-Noise ratio:<span style="color: green"> ↑</span>'
elif('cnr' in name):
return 'Contrast-to-noise ratio:<span style="color: green"> ↑</span>'
elif('fwhm' in name):
return 'Full-width half maximum estimations:<span style="color: green"> ↓</span>'
elif('qi2' in name):
return 'Goodness of fit of a noise model into the background noise: <span style="color: green">↓</span>'
elif('cjv' in name):
return 'Coefficient of joint variation:<span style="color: green"> ↓</span>'
elif(name.startswith('efc')):
return """Entropy focus criterion:
<span style="color: green"> ↓ more uniform distribution, less noisy</span>"""
elif('fber' in name):
return 'Foreground-background energy ratio:<span style="color: green"> ↑</span>'
elif('qi1' in name):
return 'Segmentation using mathematical morphology:<span style="color: green"> ↓</span>'
elif('inu' in name):
return 'Intensity non-uniformity estimate measurements:<span style="color: green"> ~1</span>'
elif('pve' in name):
return 'Partial volume errors:<span style="color: green"> ↓</span>'
elif('wm2max' in name):
return 'White-matter to maximum intensity ratio:<span style="color: green"> [0.6, 0.8]</span>'
elif('icv' in name):
return 'Intracranial volume fractions: <span style="color: green"> “should move within a normative range”</span>'
elif('rpve' in name):
return 'Residual partial volume errors:<span style="color: green"> ↓</span>'
elif('fd' in name):
return 'Framewise displacement:<span style="color: green"> ↓</span>'
elif('dvars' in name):
return 'Temporal derivative of timecourses RMS variance over voxels:<span style="color: green"> ↓</span>'
elif('gsr' in name):
return 'Ghost-to-signal Ratio:<span style="color: green"> ↓</span>'
elif('gcor' in name):
return 'Global correlation :<span style="color: green"> ↓</span>'
elif('spikes' in name):
return 'High frequency and global intensity :<span style="color: green"> ↓ (lessvolumes to remove if filtering)</span>'
elif('aor' in name):
return 'AFNI’s outlier ratio: mean fraction of outliers per fMRI volume'
elif('aqi' in name):
return 'AFNI’s quality index: mean quality index'
elif('coregCrossCorr' in name):
return 'Cross correlation:<span style="color: green"> ↑</span>'
elif('CoregJaccard' in name):
return 'Jaccard index:<span style="color: green"> ↑</span>'
elif('CoregDice' in name):
return 'Dice index:<span style="color: green"> ↑</span>'
elif('CoregCoverage' in name):
return 'Coverage index:<span style="color: green"> ↑</span>'
elif('regCrossCorr' in name):
return 'Cross correlation:<span style="color: green"> ↑</span>'
elif('regJaccard' in name):
return 'Jaccard index:<span style="color: green"> ↑</span>'
elif('regDice' in name):
return 'Dice index:<span style="color: green"> ↑</span>'
elif('regCoverage' in name):
return 'Coverage index:<span style="color: green"> ↑</span>'
elif('normCrossCorr' in name):
return 'Cross correlation:<span style="color: green"> ↑</span>'
elif('normJaccard' in name):
return 'Jaccard index:<span style="color: green"> ↑</span>'
elif('normDice' in name):
return 'Dice index:<span style="color: green"> ↑</span>'
elif('normCoverage' in name):
return 'Coverage index:<span style="color: green"> ↑</span>'
elif('relMeanRMSMotion' in name):
return 'Mean value of RMS motion:<span style="color: green"> ↓</span>'
elif('relMaxRMSMotion' in name):
return 'Maximum value of RMS motion:<span style="color: green"> ↓</span>'
elif('nSpikesFD' in name):
return 'Number of spikes per FD:<span style="color: green"> ↓</span>'
elif('nspikesDV' in name):
return 'Number of spikes per DV:<span style="color: green"> ↓</span>'
elif('pctSpikesDV' in name):
return 'Percentage of spikes per DV:<span style="color: green"> ↓</span>'
elif('pctSpikesFD' in name):
return 'Percentage of spikes per DV:<span style="color: green"> ↓/span>'
elif('meanDV' in name):
return 'Mean DVARS:<span style="color: green"> ↓</span>'
elif('motionDVCorrInit' in name):
return 'Correlation of RMS and DVARS before regression:<span style="color: green"> ↓</span>'
elif('motionDVCorrFinal' in name):
return 'Correlation of RMS and DVARS after regression :<span style="color: green"> ↓ lower than init</span>'
elif('nNuisanceParameters' in name):
return 'Total number of nuisance Parameters in addition to custom regressors:<span style="color: green"> ↓</span> (confound regression model-dependent)'
elif('nVolCensored' in name):
return 'Total number of volume(s) censored:<span style="color: green"> ↓</span>'
elif('estimatedLostTemporalDOF' in name):
return 'Total degree of freedom lost:<span style="color: green"> ↓</span>'
elif('mean_fd' in name):
return 'Mean framewise displacement:<span style="color: green"> ↓</span>'
elif('max_fd' in name):
return 'Maximum framewise displacement:<span style="color: green"> ↓</span>'
elif('max_translation' in name):
return '<span style="color: green"> ↓</span>'
elif('max_rotation' in name):
return '<span style="color: green"> ↓</span>'
elif('max_rel_translation' in name):
return 'Maxima of derivative of max_translation:<span style="color: green"> ↓</span>'
elif('max_rel_rotation' in name):
return 'Maxima of derivative of max_rotation:<span style="color: green"> ↓</span>'
elif('t1_dice_distance' in name):
return '<span style="color: green"> ↑</span>'
elif('mni_dice_distance' in name):
return '<span style="color: green"> ↑</span>'
elif('raw_incoherence_index ' in name):
return '<span style="color: green"> ↓</span>'
elif('raw_coherence_index ' in name):
return '<span style="color: green"> ↑</span>'
elif('t1_incoherence_index ' in name):
return '<span style="color: green"> ↓</span>'
elif('t1_coherence_index ' in name):
return '<span style="color: green"> ↑</span>'
elif('num_bad_slices' in name):
return '<span style="color: green"> ↓</span>'
elif('raw_dimension' in name):
return 'Should match protocol field of view'
elif('raw_voxel_size' in name):
return 'Should match protocol resolution'
elif('raw_max_b' in name):
return 'Should match protocol maximum b'
elif('raw_neighbor_corr' in name):
return 'Neighboring DWI Correlation (NDC)'
elif('raw_num_directions' in name):
return 'Should match protocol number of directions for dwi scan'
elif('t1_dimension' in name):
return 'Preprocessed space field of view'
elif('t1_voxel_size' in name):
return 'Preprocessed space resolution controlled by --output_resolution value'
if('t1_max_b' in name):
return 'Equal to raw_max_b'
elif('t1_neighbor_corr' in name):
return 'Equal to raw_neighbor_corr'
elif('t1_num_directions' in name):
return 'Equal to raw_num_directions'
else:
return ""
# In[32]:
def mean(name):
return str(df[name].mean())
# In[12]:
def median(name):
return str(df[name].median())
# In[13]:
def std(name):
return str(df[name].std())
# In[14]:
def rnge(name):
return str(df[name].max() - df[name].min())
# In[15]:
def outliers(name):
flag = False
outs = ""
mean_ = float(mean(name))
std_ = float(std(name))
for i in df.index:
if((mean_-2*std_)>df[name][i] or df[name][i]>(mean_+2*std_)):
if(flag):
outs += ", "+ str(df['id0'][i])
if df['id0'][i] in list_outliers:
list_outliers[df['id0'][i]].append(name)
else:
list_outliers[df['id0'][i]] = [name]
else:
outs += str(df['id0'][i])
flag = True
if df['id0'][i] in list_outliers:
list_outliers[df['id0'][i]].append(name)
else:
list_outliers[df['id0'][i]] = [name]
return outs
# In[16]:
def get_outliers():
output = ""
#remove nan key to sort keys in ascending order
removed = 0;
for key in list_outliers.keys():
if type(key)==float:
removed=key
break
#add nan key entry to return string
output += "<div class = outliers><br><h2>"+str(removed) + "</h2><h3> (" +str(len(list_outliers.get(removed)))+" occurences):</h3><br>"
flag = False
for item in list_outliers.get(removed):
if flag:
output += ", "+ str(item)
else:
output += str(item)
flag = True
outliers = {k: list_outliers[k] for k in list_outliers if type(k)==str}
for key in sorted(outliers):
output += "<div class = outliers><br><h2>"+str(key) + "</h2><h3> (" +str(len(list_outliers.get(key)))+" occurences):</h3><br>"
flag = False
for item in outliers.get(key):
if flag:
output += ", "+ str(item)
else:
output += str(item)
flag = True
output += "</div>"
output += "</div>"
return output
# In[33]:
#List of graph tags
tags = ["Sex","Age","BMI","BxS","_3D"]
# In[34]:
#CSS - In-document styling sheet for each site
#dropdown menu from https://www.w3schools.com/howto/howto_css_dropdown.asp
css = """ body{
margin:0;
}
.row {
display: flex;
}
.column {
flex: 50%;
padding: 5px;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
.image1 {
display: flex;
justify-content: center;
}
.navbar {
overflow: hidden;
background-color: #13294b;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.active{
background-color: #E84A27;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: #13294b;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #E8E9EA;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #13294b;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: white;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #E8E9EA;
}
.dropdown:hover .dropdown-content {
display: block;
}
#searchbar{
padding:13px;
border-radius: 10px;
}
input[type=text] {
width: 7%;
-webkit-transition: width 0.15s ease-in-out;
transition: width 0.15s ease-in-out;
}
input[type=text]:focus {
width: 15%;
}
"""
# In[41]:
list_outliers.clear()
main_name = study_name+"QCGraphs.html"
f = open(main_name,'w') #create QCGraphs in local folder
grphs = ""
for filename in os.listdir(os.getcwd()):
if filename.endswith(".png") and filename != "Illini_icon.png" and filename != "heatmap_total.png" and filename != "heatmap_95.png":
#descriptive stats
cont = True
name = filename[:len(filename) - 4]
#Check if Age and Sex Graphs exist; if not, should be in BMI page
if not(os.path.exists(name+"_Age.png") or os.path.exists(name+"_Sex.png")):
continue
exclude = ["_Age", "_Sex","_BMI", "_BxS", "_peakvo2_ml_gxt", "_fft_4step_t1", "_fft_4step_t2", "_fft_stair_ds_tester1", "_fft_stair_us_tester1" ]
#Check if png should not be on this page
for item in exclude:
if (item in name):
cont = False
if not cont:
continue
_stats = """<table style="width:50% ">
<tr>
<th colspan="2">"""+name+"""</th>
</tr>
<tr>
<td>Mean</td>
<td>"""+mean(name)+"""</td>
</tr>
<tr>
<td>Median</td>
<td>"""+median(name)+"""</td>
</tr>
<tr>
<td>Std.</td>
<td>"""+std(name)+"""</td>
</tr>
<tr>
<td>Range</td>
<td>"""+rnge(name)+"""</td>
</tr>
</table>"""
#Insert sets of 3 graphs in template to ensure graph stays with corresponding age and sex graphs
grphs += "<div class=row>"
grphs += ("<div class=column><h2 style='text-align:center'>"+name+"_Age</h2><div class='image1'><img src="+ name
+"_Age.png>\n</div></i></p></div>")
grphs+= ("<div class=column><h2 style='text-align:center'>"+name+"</h2><div class='image1'><img src="+ filename+
" >\n</div><br><p font-style=italic align=center><i>"+_stats+
"<br><font size='+2'>" +str(desc(name))+
"</font><br>Outliers: "+outliers(name)+"</i></p></div>")
grphs += ("<div class=column><h2 style='text-align:center'>"+name+"_Sex</h2><div class='image1'><img src="+ name +
"_Sex.png>\n</div></i></p></div>")
grphs += "</div>"
else:
continue
#linked stylesheet for caret symbol for dropdown
code = """<html><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>"""+css+"""</style>
<meta name="robots" content="noindex">
<head>
<div class="navbar">
<a class="active" href="""+study_name+"""QCGraphs.html>QC Graphs</a>
<a href="""+study_name+"""nbs_qsiGraphs.html> NBS QSI </a>
<a href="""+study_name+"""nbs_RSFC.html> NBS RSFC</a>
<a href="""+study_name+"""func_restGraphs.html> Functional Resting-State</a>
<a href="""+study_name+"""diff_qcGraphs.html> Diffusion QC </a>
<a href="""+study_name+"""t1wGraphs.html> T1 Weighted </a>
<a href="""+study_name+"""t2wGraphs.html> T2 Weighted </a>
<div class="dropdown">
<button class="dropbtn">Behavioral Graphs
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="""+study_name+"""vo2Graphs.html> Peak VO2 </a>
<a href="""+study_name+"""fourStep1Graphs.html> FourStep 1 </a>
<a href="""+study_name+"""fourStep2Graphs.html> FourStep 2 </a>
<a href="""+study_name+"""stairDownGraphs.html> Stair-Down </a>
<a href="""+study_name+"""stairUpGraphs.html> Stair-Up </a>
</div>
</div>
<a href="""+study_name+"""outliers.html>Outliers</a>
<a href="""+study_name+"""data.html>Data</a>
<a href="""+study_name+"""about.html>About</a>
<img src="Illini_icon.png" width = 3.5% style="float:right">
<input id="searchbar" onkeyup="Search_var()" type="text" name="search" placeholder="Search...">
</div>
</head>
"""
f.write(code)
#insert graphs
code2 = """
<body style="background-color:#f0f0f0">
<div style = "text-align: center; vertical-align: middle;">
<h1>Last Updated: """+date+"""</h1>
</div>
"""+grphs+"""</body>
<script>
function Search_var() {
let input = document.getElementById('searchbar').value
input=input.toLowerCase();
let x = document.getElementsByClassName('row');
for (i = 0; i < x.length; i++) {
if (!x[i].innerHTML.toLowerCase().includes(input)) {
x[i].style.display="none";
}
else {
x[i].style.display="";
}
}
}
</script>
</html>"""
f.write(code2)
f.close()
#open html file
QCfile = 'file:///'+os.getcwd()+'/' + main_name
webbrowser.open_new_tab(QCfile)
# In[36]: