-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspatial_corr_ridgeplot_func.py
More file actions
600 lines (480 loc) · 24.5 KB
/
Copy pathspatial_corr_ridgeplot_func.py
File metadata and controls
600 lines (480 loc) · 24.5 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
import numpy as np
import pandas as pd
import os
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerLine2D
from scipy import stats
from scipy.io import loadmat
from scipy.stats import rankdata
from scipy.stats import spearmanr
def spatial_corr_ridgeplot(base,outpath,pipelines,atlases,namechangedict,fc_handle,simpleplot,corr_type):
'''
Function to prepare and, optionally, run the C-PAC workflow
Parameters
----------
base : string
the base direcotory, the ROI data will be in base +'/ROI_Schaefer' + atlas + '/' + pipelinename
pipelines : list of strings
list of the pipelines to do correlaitons
atlases : list
list of atlases to ues, for example ['200','600','1000']
namechangedict : dictionary
keys are the pipeline names, values are the name to change to
fc_handle : string
how to hand the spatial correlation, it can be '','Scale','Ranking'
simpleplot : boolean
flag to indicate the combination of pipelines, True only plot the correlation betwene the first pipeline and the rest.
corr_type: string
which correlation to do: concordance, spearman, or pearson
Returns:
None, but save figure out.
-------
workflow :
'''
# the order of ccs subjects correspondign to cpac nad ndmg
# cpac: 1-2, 3-14, 15-30
# ccs : 1-2, 19-30, 3-18
#\u221a/data2/Projects/Lei/ndmg/Resting_Preprocessing/hnu/ndmg_out/func/roi-timeseries/CPAC200_res-2x2x2/sub-0025427_task-rest_bold_CPAC200_res-2x2x2_variant-mean_timeseries.npz
def upper_tri_masking(A):
m = A.shape[0]
r = np.arange(m)
mask = r[:,None] < r
return A[mask]
def upper_tri_indexing(A):
m = A.shape[0]
r,c = np.triu_indices(m,1)
return A[r,c]
# concordance_correlation_coefficient from https://github.com/stylianos-kampakis/supervisedPCA-Python/blob/master/Untitled.py
def concordance_correlation_coefficient(y_true, y_pred,
sample_weight=None,
multioutput='uniform_average'):
"""Concordance correlation coefficient.
The concordance correlation coefficient is a measure of inter-rater agreement.
It measures the deviation of the relationship between predicted and true values
from the 45 degree angle.
Read more: https://en.wikipedia.org/wiki/Concordance_correlation_coefficient
Original paper: Lawrence, I., and Kuei Lin. "A concordance correlation coefficient to evaluate reproducibility." Biometrics (1989): 255-268.
Parameters
----------
y_true : array-like of shape = (n_samples) or (n_samples, n_outputs)
Ground truth (correct) target values.
y_pred : array-like of shape = (n_samples) or (n_samples, n_outputs)
Estimated target values.
Returns
-------
loss : A float in the range [-1,1]. A value of 1 indicates perfect agreement
between the true and the predicted values.
Examples
--------
>>> from sklearn.metrics import concordance_correlation_coefficient
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> concordance_correlation_coefficient(y_true, y_pred)
0.97678916827853024
"""
cor=np.corrcoef(y_true,y_pred)[0][1]
mean_true=np.mean(y_true)
mean_pred=np.mean(y_pred)
var_true=np.var(y_true)
var_pred=np.var(y_pred)
sd_true=np.std(y_true)
sd_pred=np.std(y_pred)
numerator=2*cor*sd_true*sd_pred
denominator=var_true+var_pred+(mean_true-mean_pred)**2
return numerator/denominator
def ridgeplot(df,atlases,outfile):
#plt.subplots(figsize=(10,20))
show_label=False
# Initialize the FacetGrid object
x=[]
for i in np.unique(df['g']):
x.append(np.median(df[df['g']==i]['x']))
roworder=np.unique(df['g'])[np.argsort(x)[::-1]]
# for colour and row name match
with sns.plotting_context(font_scale=5):
g1 = sns.FacetGrid(df, row="g", hue="g", aspect=len(pipelines)*(len(pipelines)-1)/2, height=2)
default_rowname=g1.row_names
sortidx=[]
for i in default_rowname:
sortidx.append(list(roworder).index(i))
#pal = sns.cubehelix_palette(len(np.unique(df['g'])), rot=-.25, light=.7)
#pal = np.asarray(sns.cubehelix_palette(len(np.unique(df['g'])), rot=.9, light=.7))
#pal = np.asarray(sns.color_palette("purple", len(np.unique(df['g']))))
pal = np.asarray(sns.cubehelix_palette(len(np.unique(df['g'])), start=.5, rot=-.75,light=0.7))
#pal=pal[sortidx][::-1]
pal=pal[::-1]
pal=pal[sortidx]
with sns.plotting_context(font_scale=5):
#g = sns.FacetGrid(df, sharey=True, row="g", hue="g", row_order=roworder, aspect=len(pipelines)*(len(pipelines)-1)/4, height=2*10/(len(pipelines)*(len(pipelines)-1)/2), palette=pal)
#g = sns.FacetGrid(df, sharey=True, row="g", hue="g", row_order=roworder, aspect=5, height=2, palette=pal)
g = sns.FacetGrid(df, sharey=False, row="g", hue="g", row_order=roworder, aspect=5, height=2, palette=pal)
#g = sns.FacetGrid(df, sharey=True, row="g", hue="g", row_order=roworder, aspect=10/2.8, height=2.8, palette=pal)
#g = sns.FacetGrid(df, row="g", hue="g", aspect=10, height=2, palette=pal)
# Draw the densities in a few steps
#g.map(sns.kdeplot, "x",hist=False)
#g.map(sns.kdeplot, "x",shade=True)
if len(atlases)==1:
g.map(sns.kdeplot, "x",shade=True,color='red')
if len(atlases)==2:
g.map(sns.kdeplot, "x",shade=True,color='red')
g.map(sns.kdeplot, "xx",shade=True,color='blue')
if len(atlases)==3:
g.map(sns.kdeplot, "x",shade=True,color='red')
g.map(sns.kdeplot, "xx",shade=True,color='blue')
g.map(sns.kdeplot, "xxx",shade=True,color='black')
#g.map(sns.kdeplot, df2['x'],shade=True,color='blue')
lw_value=3
g.map(plt.axvline, x=1, lw=lw_value, clip_on=False,color=(134/256.0,250/256.0,167/256.0))
g.map(plt.axvline, x=0.9, lw=lw_value, clip_on=False,color=(128/256.0,171/256.0,69/256.0))
g.map(plt.axvline, x=0.8, lw=lw_value, clip_on=False,color=(128/256.0,126/256.0,38/256.0))
g.map(plt.axvline, x=0.7, lw=lw_value, clip_on=False,color=(246/256.0,192/256.0,66/256.0))
g.map(plt.axvline, x=0.6, lw=lw_value, clip_on=False,color=(192/256.0,98/256.0,43/256.0))
#g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2, bw=.2)
g.map(plt.axhline, y=0, lw=2, clip_on=False)
# Define and use a simple function to label the plot in axes coordinates
def label(x, color, label):
ax = plt.gca()
ax.text(0, .2, label, fontweight="bold", color='black',fontsize=35,
ha="left", va="center", transform=ax.transAxes)
#ax.text(0, .2, label, fontweight="bold", color=color,fontsize=25,
# ha="left", va="center", transform=ax.transAxes)
if show_label==True:
g.map(label, "x")
# Set the subplots to overlap
#g.fig.subplots_adjust(hspace=-.6)
# change the x axis label size
ax = plt.gca()
ax.tick_params(axis = 'both', which = 'major', labelsize = 35)
# Remove axes details that don't play well with overlap
g.set(xlim=(0, 1))
g.set_titles("")
g.set(xlabel='')
g.set(yticks=[])
g.despine(bottom=True, left=True)
if len(os.path.basename(outfile))>140:
outfile=os.path.dirname(outfile)+ '/' + os.path.basename(outfile)[0:130]+'.png'
g.savefig(outfile)
return [x for _,x in sorted(zip(sortidx,default_rowname))]
#plt.cla()
#plt.subplots(figsize=(10,8))
# define matrix for mean and median
value_median=pd.DataFrame(np.zeros(((len(pipelines)*(len(pipelines)-1))/2,4)), columns=['Pipelines','Atlas200','Atlas600','Atlas1000'])
value_std=pd.DataFrame(np.zeros(((len(pipelines)*(len(pipelines)-1))/2,4)), columns=['Pipelines','Atlas200','Atlas600','Atlas1000'])
value_quartile=pd.DataFrame(np.zeros(((len(pipelines)*(len(pipelines)-1))/2,13)), columns=['Pipelines','25_Atlas200','50_Atlas200','75_Atlas200','100_Atlas200','25_Atlas600','50_Atlas600','75_Atlas600','100_Atlas600','25_Atlas1000','50_Atlas1000','75_Atlas1000','100_Atlas1000'])
# np.percentile(data, 25, interpolation = 'midpoint')
#for atlas in ['200','600','1000']:
atlas_idx=0
for atlas in atlases:
num_idx=(len(pipelines)*(len(pipelines)-1))/2
color_palette = sns.color_palette("Paired",num_idx)
for i in range(0,len(pipelines)):
for j in range(i+1,len(pipelines)):
p1=pipelines[i]
p2=pipelines[j]
pp='sc_' + p1 + '_' + p2
locals()[pp]=[]
#for i in range(1,31):
basesub=25426
for i in range(1,31):
if basesub+i == 25430:
continue
stop=0
for xxx in pipelines:
fodlercontent=os.listdir(base +'/ROI_Schaefer' + atlas + '/' + xxx)
if str(i+basesub) not in str(fodlercontent):
print(xxx + 'not in')
print(i+basesub)
stop=1
if stop==1:
continue
print(i)
# put them all together, load each pipeline file and calcuate calrelaiton and give it a different name.
for pl in pipelines:
datafolder = base +'/ROI_Schaefer' + atlas + '/' + pl
# cpacdefault24
#/data3/cnl/fmriprep/Lei_working/CPAC_XCP/CPAC_aggre_output/sub-0025427a_ses-1_bandpassed_demeaned_filtered_antswarp_cc200.1D
cpacfile=datafolder + '/sub-00' + str(basesub+i) + 'a.1D'
data=np.genfromtxt(cpacfile)
if data.shape[0] != 295:
idx= data.shape[0]-295
data=data[idx:,]
data=data.transpose()
data_corr=np.corrcoef(data)
tmp_corr_tri=upper_tri_indexing(data_corr)
locals()[pl+'_corr_tri']=tmp_corr_tri
# (df.a - df.a.mean())/df.a.std(ddof=0)
def spatial_correlation(corr_a,corr_b,sc,corr_type):
if fc_handle=='Scale':
print('here')
corr_a[np.isnan(corr_a)]=0
corr_b[np.isnan(corr_b)]=0
corr_a= (corr_a - corr_a.mean())/corr_a.std(ddof=0)
corr_b= (corr_b - corr_b.mean())/corr_b.std(ddof=0)
if fc_handle == 'Ranking':
corr_a = rankdata(corr_a)
corr_b = rankdata(corr_b)
x=np.isnan(corr_a) | np.isnan(corr_b)
corr_a_new=corr_a[~x]
corr_b_new=corr_b[~x]
if corr_type == "spearman":
sc.append(spearmanr(corr_a_new,corr_b_new)[0])
elif corr_type == "concordance":
sc.append(concordance_correlation_coefficient(corr_a_new,corr_b_new))
else:
sc.append(np.corrcoef(corr_a_new,corr_b_new)[0,1])
return sc
### do correlaiton between pipelines
num_idx=(len(pipelines)*(len(pipelines)-1))/2
color_palette = sns.color_palette("Paired",num_idx)
for i in range(0,len(pipelines)):
for j in range(i+1,len(pipelines)):
p1=pipelines[i]
corr1= locals()[p1 + '_corr_tri']
p2=pipelines[j]
corr2= locals()[p2 + '_corr_tri']
pp='sc_' + p1 + '_' + p2
locals()[pp] = spatial_correlation(corr1,corr2,locals()[pp],corr_type)
idx=0
num_idx=(len(pipelines)*(len(pipelines)-1))/2
color_palette = sns.color_palette("Paired",num_idx)
df_all=pd.DataFrame(columns = ['x','g'])
if simpleplot == True:
plotrange=1
else:
plotrange=len(pipelines)
for i in range(0,plotrange):
for j in range(i+1,len(pipelines)):
print(idx)
p1=pipelines[i]
p2=pipelines[j]
pp1='sc_' + p1 + '_' + p2
pp2='sc_' + p2 + '_' + p1
if pp1 in locals():
pp = locals()[pp1]
print(pp1)
elif pp2 in locals():
pp = locals()[pp2]
print(pp2)
#pn1=p1.replace('newcpac','cpac:xcp').replace('defaultcpac','cpac:default')
#pn2=p2.replace('newcpac','cpac:xcp').replace('defaultcpac','cpac:default')
pn1=p1
pn2=p2
for key in namechangedict:
pn1=pn1.replace(key,namechangedict[key])
#pn1=pn1.replace('cpac','CPAC:fmriprep')
for key in namechangedict:
pn2=pn2.replace(key,namechangedict[key])
#pn2=pn2.replace('cpac','CPAC:fmriprep')
print(pp)
# get median and std
print(pp1+str(np.median(pp))+str(np.std(pp)))
value_median['Pipelines'][idx] = pp1
value_median['Atlas'+atlas][idx] = np.median(pp)
value_std['Pipelines'][idx] = pp1
value_std['Atlas'+atlas][idx] = np.std(pp)
value_quartile['Pipelines'][idx] = pp1
for pct_val in [25,50,75,100]:
value_quartile[str(pct_val)+'_Atlas'+atlas][idx] = np.percentile(pp, pct_val, interpolation = 'midpoint')
tmp=pd.DataFrame(pp, columns=['x'])
tmp['g']=pn1+' - '+pn2
df_all=pd.concat([df_all,tmp])
idx +=1
#ridgeplot(df_all,os.path.dirname(base) + '/figures/Ridgeplot_spatial_corr_'+corr_type+'_'+'-'.join(pipelines)[0:90]+'_'+atlas+'.png')
# put multipel atlas in one redge plot.
if atlas_idx==0:
df_ridge =df_all
elif atlas_idx==1:
df_ridge['xx']=df_all['x']
elif atlas_idx==2:
df_ridge['xxx']=df_all['x']
atlas_idx += 1
print(value_median)
value_median.to_csv(os.path.dirname(base) + '/figures/Ridgeplot_spatial_corr_'+corr_type+'_'+'-'.join(pipelines)[0:90]+'_Median.csv')
value_std.to_csv(os.path.dirname(base) + '/figures/Ridgeplot_spatial_corr_'+corr_type+'_'+'-'.join(pipelines)[0:90]+'_std.csv')
value_quartile.to_csv(os.path.dirname(base) + '/figures/Ridgeplot_spatial_corr_'+corr_type+'_'+'-'.join(pipelines)[0:90]+'_quartile.csv')
plotnameorder=ridgeplot(df_ridge,atlases,os.path.dirname(base) + '/figures/Ridgeplot_spatial_corr_'+corr_type+'_'+'-'.join(pipelines)[0:90]+'_'+atlas+'.png')
return plotnameorder
def ICC_ridgeplot(base,outpath,pipelines,atlases,namechangedict,simpleplot,plotnameorder):
# cpac_default_off_nuisance_ccs_ICC.csv
def ridgeplot(df,atlases,outfile,plotnameorder):
show_label=False
#plt.subplots(figsize=(10,20))
# Initialize the FacetGrid object
x=[]
for i in np.unique(df['g']):
x.append(np.median(df[df['g']==i]['x']))
roworder=np.unique(df['g'])[np.argsort(x)[::-1]]
# for colour and row name match
with sns.plotting_context(font_scale=5):
g1 = sns.FacetGrid(df, row="g", hue="g", aspect=len(pipelines)*(len(pipelines)-1)/2, height=2)
default_rowname=g1.row_names
if plotnameorder:
default_rowname=plotnameorder
sortidx=[]
for i in default_rowname:
sortidx.append(list(roworder).index(i))
#pal = sns.cubehelix_palette(len(np.unique(df['g'])), rot=-.25, light=.7)
#pal = np.asarray(sns.cubehelix_palette(len(np.unique(df['g'])), rot=.9, light=.7))
#pal = np.asarray(sns.color_palette("purple", len(np.unique(df['g']))))
pal = np.asarray(sns.cubehelix_palette(len(np.unique(df['g'])), start=.5, rot=-.75,light=0.7))
#pal=pal[sortidx][::-1]
pal=pal[::-1]
pal=pal[sortidx]
with sns.plotting_context(font_scale=5):
#g = sns.FacetGrid(df, row="g", hue="g", row_order=roworder, aspect=len(pipelines)*(len(pipelines)-1)/4, height=2*10/(len(pipelines)*(len(pipelines)-1)/2), palette=pal)
#g = sns.FacetGrid(df, sharey=True,row='g',hue="atlas", row_order=roworder, aspect=len(pipelines)*(len(pipelines)-1)/4, height=2*10/(len(pipelines)*(len(pipelines)-1)/2), palette=['black','red','blue'])
#g = sns.FacetGrid(df, sharey=True, row="g", hue="atlas", row_order=roworder, aspect=5, height=2, palette=['black','red','blue'])
g = sns.FacetGrid(df, sharey=False, row="g", hue="atlas", row_order=roworder, aspect=5, height=2, palette=['black','red','blue'])
#g = sns.FacetGrid(df, sharey=True, row="g", hue="atlas", row_order=roworder, aspect=1.07, height=2.8, palette=['black','red','blue'])
# Draw the densities in a few steps
#g.map(sns.kdeplot, "x",hist=False)
if len(atlases) ==1:
g.map(sns.kdeplot, "x",shade=True,color='red')
else:
g.map(sns.kdeplot, "x",shade=True)
#.add_legend()
#g.map(sns.kdeplot, df2['x'],shade=True,color='blue')
lw_value=3
g.map(plt.axvline, x=1, lw=lw_value, clip_on=False,color=(134/256.0,250/256.0,167/256.0))
g.map(plt.axvline, x=0.9, lw=lw_value, clip_on=False,color=(128/256.0,171/256.0,69/256.0))
g.map(plt.axvline, x=0.8, lw=lw_value, clip_on=False,color=(128/256.0,126/256.0,38/256.0))
g.map(plt.axvline, x=0.7, lw=lw_value, clip_on=False,color=(246/256.0,192/256.0,66/256.0))
g.map(plt.axvline, x=0.6, lw=lw_value, clip_on=False,color=(192/256.0,98/256.0,43/256.0))
#g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2, bw=.2)
g.map(plt.axhline, y=0, lw=2, clip_on=False)
# Define and use a simple function to label the plot in axes coordinates
def label(x, color, label):
ax = plt.gca()
ax.text(0, .2, label, fontweight="bold", color='black',fontsize=35,
ha="left", va="center", transform=ax.transAxes)
#ax.text(0, .2, label, fontweight="bold", color=color,fontsize=25,
# ha="left", va="center", transform=ax.transAxes)
if show_label==True:
g.map(label, "x")
# Set the subplots to overlap
#g.fig.subplots_adjust(hspace=-.6)
# change the x axis label size
ax = plt.gca()
ax.tick_params(axis = 'both', which = 'major', labelsize = 35)
# Remove axes details that don't play well with overlap
g.set(xlim=(0, 1))
g.set_titles("")
g.set(xlabel='')
g.set(yticks=[])
g.despine(bottom=True, left=True)
if len(os.path.basename(outfile))>140:
outfile=os.path.dirname(outfile)+ '/' + os.path.basename(outfile)[0:130]+'.png'
g.savefig(outfile)
value_median=pd.DataFrame(np.zeros(((len(pipelines)*(len(pipelines)-1))/2,4)), columns=['Pipelines','Atlas200','Atlas600','Atlas1000'])
value_std=pd.DataFrame(np.zeros(((len(pipelines)*(len(pipelines)-1))/2,4)), columns=['Pipelines','Atlas200','Atlas600','Atlas1000'])
value_quartile=pd.DataFrame(np.zeros(((len(pipelines)*(len(pipelines)-1))/2,13)), columns=['Pipelines','25_Atlas200','50_Atlas200','75_Atlas200','100_Atlas200','25_Atlas600','50_Atlas600','75_Atlas600','100_Atlas600','25_Atlas1000','50_Atlas1000','75_Atlas1000','100_Atlas1000'])
atlas_idx=0
for atlas in atlases:
print(atlas)
df_all=pd.DataFrame(columns = ['x','g'])
idx=0
if simpleplot == True:
plotrange=1
else:
plotrange=len(pipelines)
for i in range(0,plotrange):
for j in range(i+1,len(pipelines)):
p1=pipelines[i]
p2=pipelines[j]
p1=p1.replace('ABCD','abcd')
p2=p2.replace('ABCD','abcd')
pp1= base+ '/ICC_Schaefer' + atlas + '/' + p1 + '_' + p2 + '_ICC.csv'
pp2= base+ '/ICC_Schaefer' + atlas + '/' + p2 + '_' + p1 + '_ICC.csv'
print(pp1)
print(pp2)
if os.path.isfile(pp1):
tmp=pd.read_csv(pp1,header=None,names=['x'])
print(pp1)
elif os.path.isfile(pp2):
tmp=pd.read_csv(pp2,header=None,names=['x'])
print(pp1)
pn1=p1
pn2=p2
for key in namechangedict:
pn1=pn1.replace(key,namechangedict[key])
#pn1=pn1.replace('cpac','CPAC:fmriprep')
for key in namechangedict:
pn2=pn2.replace(key,namechangedict[key])
#pn2=pn2.replace('cpac','CPAC:fmriprep')
#print(pp)
# get median and std
print(pp1+str(np.median(tmp))+str(np.std(tmp)))
value_median['Pipelines'][idx] = p1+'_'+p2
value_median['Atlas'+atlas][idx] = np.median(tmp)
value_std['Pipelines'][idx] = p1+'_'+p2
value_std['Atlas'+atlas][idx] = np.std(tmp)
value_quartile['Pipelines'][idx] = p1+'_'+p2
for pct_val in [25,50,75, 100]:
value_quartile[str(pct_val)+'_Atlas'+atlas][idx] = np.percentile(tmp, pct_val, interpolation = 'midpoint')
#tmp=pd.DataFrame(pp, columns=['x'])
tmp['g']=pn1+' - '+pn2
df_all=pd.concat([df_all,tmp])
print(df_all.head(3))
idx += 1
# put multipel atlas in one redge plot.
if atlas=='200':
df_ridge_1=df_all
df_ridge_1['atlas']='200'
elif atlas=='600':
df_ridge_2=df_all
df_ridge_2['atlas']='600'
elif atlas=='1000':
df_ridge_3=df_all
df_ridge_3['atlas']='1000'
#df_ridge_1.reset_index(drop=True, inplace=True)
if len(atlases)==1:
df=pd.concat([df_ridge_1])
if len(atlases)==2:
df=pd.concat([df_ridge_1])
df=pd.concat([df_ridge_1,df_ridge_2])
if len(atlases)==3:
df=pd.concat([df_ridge_1])
df=pd.concat([df_ridge_1,df_ridge_2])
df=pd.concat([df_ridge_1,df_ridge_2,df_ridge_3])
#df=pd.concat([df_ridge_1,df_ridge_2,df_ridge_3])
df = df[df['x'] != 0]
#df_ridge_2.reset_index(drop=True, inplace=True)
#df_ridge=pd.concat([df_ridge_1,df_ridge_2], ignore_index=True, axis=1)
#df_ridge=df_ridge.fillna(-9999)
#df_ridge.columns=['x','g','xx','gg']
print(value_median)
value_median.to_csv(os.path.dirname(base) + '/figures/Ridgeplot_ICC_'+'_'+'-'.join(pipelines)[0:90]+'_Median.csv')
value_std.to_csv(os.path.dirname(base) + '/figures/Ridgeplot_ICC_'+'_'+'-'.join(pipelines)[0:90]+'_std.csv')
value_quartile.to_csv(os.path.dirname(base) + '/figures/Ridgeplot_ICC_'+'_'+'-'.join(pipelines)[0:90]+'_quartile.csv')
ridgeplot(df,atlases,outpath + '/Ridgeplot_ICC_'+'_'+'-'.join(pipelines)[0:90]+'_'+atlas+'.png',plotnameorder)
### Example
'''
fc_handle=''
corr_type='spearman'
simpleplot=False
mulitple_atlas=False
if mulitple_atlas:
atlases=['200','600','1000']
else:
atlases=['200',]
base='/Users/leiai/projects/CPAC_fmriprep/Reliability_discriminibility/Finalizing/Minimal/ROI'
#pipelines=['cpac_fmriprep','abcd','fmriprep_1','fmriprep_MNI2004_2mm','dpabi','ccs']
#pipelines=['cpac_default_off_nuisance','abcd','fmriprep_1','fmriprep_MNI2004_2mm','dpabi','ccs']
pipelines=['cpac_fmriprep','cpac_default_off_nuisance','fmriprep_1','fmriprep_MNI2004_2mm','ccs']
# this is the name chang for the final plot.
namechangedict={'Upenn_cpacfmriprep':'fMRIPrep:UPenn',
'abcd':'ABCD',
'dpabi':'DPARSF',
'fmriprep_fs':'fmriprep:freesurfer',
'cpac_fnirt':'CPAC:fmriprep(fnirt)',
'cpac_default_fnirt':'CPAC:default(fnirt)',
'cpac_default_off_nuisance':'CPAC:Default',
'cpac_fmriprep':'CPAC:fMRIPrep',
'cpac_fmriprep_MNI2004':'CPAC:fmriprep',
'ccs':'CCS',
'fmriprep_1':'fMRIPrep',
'fmriprep_MNI2004_2mm':'fMRIPrep(MNI2004-2mm)'
}
spatial_corr_ridgeplot(base,base.replace('ROI','figures'),pipelines,atlases,namechangedict,fc_handle,simpleplot,corr_type)
'''