-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplain_multi.py
1484 lines (1234 loc) · 66.8 KB
/
explain_multi.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
import numpy as np
import statistics
from itertools import combinations
import math
import cv2
import copy
import json
import io
import time
import os
from argparse import ArgumentParser
from glob import glob
import csv
import whitematteranalysis as wma
import sys
from vanilla_gradient import VanillaGradient as SaliencyMap
from guided_backprop import GuidedBackprop
import torch
import torch.nn as nn
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from sklearn.metrics import davies_bouldin_score as DB
from sklearn.metrics import silhouette_score as SS
from sklearn.metrics import calinski_harabasz_score as CH
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans, HDBSCAN, AgglomerativeClustering
torch.manual_seed(0)
np.random.seed(0)
def plot_saliency_grid(avg_over_subjects, metric_names, category_names, output_dir):
mpl.use('TkAgg'), plt.close('all'), plt.clf()
if not os.path.exists(OUTPUT_BASE):
os.makedirs(OUTPUT_BASE)
# Compute the category means, so that we can sort by their average
category_averages = np.swapaxes(np.array([np.mean(avg_over_subjects[:,:2,:],1), np.mean(avg_over_subjects[:,2:,:],1)]),0,1)
column_means = np.mean(category_averages,axis=(1,2))
sorted_indices = np.argsort(column_means)
for task_id in range(avg_over_subjects.shape[1]):
plt.imshow(avg_over_subjects[:,task_id,:].T[:,sorted_indices])
plt.tick_params(axis='both', which='both', bottom=False, left=False, labelbottom=False, labelleft=False)
plt.xlabel('Cluster')
plt.ylabel('dMRI Feature')
plt.title(metric_names[task_id])
plt.savefig(output_dir + '/saliency_task_'+metric_names[task_id]+'.png', bbox_inches='tight', dpi=300)
plt.clf()
for i, category_name in enumerate(['Motor', 'Cognitive']):
plt.imshow(category_averages[:,i,:].T[:,sorted_indices])
plt.tick_params(axis='both', which='both', bottom=False, left=False, labelbottom=False, labelleft=False)
plt.xlabel('Cluster')
plt.ylabel('dMRI Feature')
plt.title(category_name)
plt.savefig(output_dir + '/saliency_category_'+category_name+'.png', bbox_inches='tight', dpi=300)
plt.clf()
# Compute difference
diffs = np.abs(category_averages[:,0,:] - category_averages[:,1,:]).T
# Sort by mean of motor/cognitive for each feature
diffs = diffs[:, sorted_indices]
plt.imshow(diffs, cmap='gray')
plt.tick_params(axis='both', which='both', bottom=False, left=False, labelbottom=False, labelleft=False)
plt.xlabel('Cluster')
plt.ylabel('dMRI Feature')
plt.title('Absolute Difference (Motor - Cognitive)')
plt.savefig(output_dir + '/saliency_category_diffs.png', bbox_inches='tight', dpi=300)
plt.clf()
def plot_per_category_saliency(all_scores, tract_names, categories, cluster_names, output_dir, tract_order=['ICP', 'MCP', 'SCP', 'IP', 'PF'], ymax=None):
# num_folds x num_metrics x num_subjects_per_fold x num_clusters x num_features
category_scores = {'motor': [], 'cognitive': []}
task_scores = np.mean(all_scores, axis=(2,4)) # num_folds x num_metrics x num_clusters
for i, category in enumerate(categories):
task_score = task_scores[:,i,:]
category_scores[category].append(task_score) # num_folds x num_clusters
# now category_scores['motor'] is of shape: num_metrics_of_motor x num_folds x num_clusters
averaged_category_scores = {}
for category in category_scores.keys():
averaged_category_scores[category] = np.mean(np.array(category_scores[category]),0)
category_scores = averaged_category_scores # now category_scores['motor'] is: num_folds x num_clusters
# tract -> cluster scores
tract_scores = {tract: [[], []] for tract in set(tract_names)}
for i, tract_name in enumerate(tract_names):
motor, cognitive = category_scores['motor'][:,i], category_scores['cognitive'][:,i]
tract_scores[tract_name][0].append(motor)
tract_scores[tract_name][1].append(cognitive)
# now: tract_scores['MCP'][0] is of shape: num_clusters x num_folds
averaged_tract_scores = {tract: [[], []] for tract in tract_scores.keys()}
for tract in tract_scores.keys():
averaged_tract_scores[tract][0] = np.mean(np.array(tract_scores[tract][0]),0)
averaged_tract_scores[tract][1] = np.mean(np.array(tract_scores[tract][1]),0)
tract_scores = averaged_tract_scores
# now: tract_scores[tract][0] is of shape: num_folds
xlabels = tract_order
series_labels = ['Motor', 'Cognitive']
series_data = np.array([[np.mean(np.array(tract_scores[tract][0])), np.mean(np.array(tract_scores[tract][1]))] for tract in xlabels]).T
series_errors = np.array([[np.std(np.array(tract_scores[tract][0])), np.std(np.array(tract_scores[tract][1]))] for tract in xlabels]).T
bar_graph(xlabels, series_labels, series_data, series_errors, 'Saliency', output_dir=output_dir, fn='tract_categories', title=None, ymax=ymax)
# xlabels = ['x1', 'x2', 'x3']
# series_labels = [seriesA, seriesB, ...]
# series_data = [[x1,x2,x3], [x1,x2,x3], ...]
def bar_graph(xlabels, series_labels, series_data, errors, ylabel, output_dir, fn, title=None, ymax=None):
mpl.use('TkAgg'), plt.close('all'), plt.clf()
n_groups, n_series = len(series_data[0]), len(xlabels)
barWidth = 0.4
index = np.arange(n_groups)
# Plotting
for i, series in enumerate(series_data):
label = series_labels[i]
plt.bar(index + i*barWidth, series, barWidth, label=label, yerr=errors[i])
# Add labels and title
plt.ylabel(ylabel)
if title is not None:
plt.title(title)
# Add legend
if ymax is not None:
plt.ylim(0,ymax)
plt.xticks(index+barWidth/2, xlabels)
plt.legend()
# Show the plot
plt.savefig(output_dir + '/' + fn + '.png', bbox_inches='tight', dpi=300)
plt.clf()
def parcel_analysis(labels, scores, task_names, cluster_names, feature_names, tract_names, categories, output_dir, tract_order=['ICP', 'MCP', 'SCP', 'IP', 'PF'], print_parcels=False):
saliency_values = scores.copy()
mpl.use('TkAgg'), plt.close('all'), plt.clf()
if not os.path.exists(output_dir):
os.makedirs(output_dir)
bar_colors = ['purple', 'cyan', 'gold', 'red']
# Create the dictionaries
parcels = {label: [] for label in set(labels)}
parcel_keys = list(sorted(parcels.keys()))
for i, label in enumerate(labels):
parcels[label].append([scores[i], tract_names[i], cluster_names[i]])
# 1. For each parcel, we want the mean saliency for each feature
feature_means = []
feature_stdevs = []
for parcel in parcel_keys:
curr_scores = np.mean(np.array([item[0] for item in parcels[parcel]]),axis=1)
feature_means.append(np.mean(curr_scores,0))
feature_stdevs.append(np.std(curr_scores,0))
ymax = math.ceil(max([max(feature_means[i])+max(feature_stdevs[i]) for i in range(len(feature_means))]) *10) / 10.0
for i in range(len(feature_means)):
curr_parcel = feature_means[i]
curr_err = feature_stdevs[i]
plt.rcParams['font.size'] = 16
plt.title('Parcel ' + str(i))
plt.bar([feature_num for feature_num in range(len(curr_parcel))], curr_parcel, yerr=curr_err)
plt.xticks([feature_num for feature_num in range(len(curr_parcel))], feature_names, rotation=90)
plt.ylabel('Mean Saliency')
plt.ylim(0,ymax)
plt.savefig(output_dir + '/feature_means_parcel_'+str(i)+'.png', bbox_inches='tight', dpi=300)
plt.rcParams['font.size'] = 12
plt.clf()
# we want a 20 x num_items array
feature_data = np.array(feature_means).T
plot_violin(list(feature_data), output_dir, 'feature_means_violin', None, 'Saliency', 0, ymax, feature_names, hbar=False)
plt.boxplot(feature_data.T)
plt.xticks([feature_num for feature_num in range(len(curr_parcel))], feature_names, rotation=90)
plt.ylabel('Saliency')
plt.ylim(0,0.4)
plt.savefig(output_dir + '/feature_means_boxplot.png', bbox_inches='tight', dpi=300)
plt.clf()
feature_means_means = [sum(item)/len(item) for item in feature_means]
# 2. For a given parcel, we want the mean overall saliency
overall_means = []
overall_stdevs = []
for parcel in parcel_keys:
curr_scores = np.mean(np.array([item[0] for item in parcels[parcel]]), axis=(1,2))
overall_means.append(np.mean(curr_scores))
overall_stdevs.append(np.std(curr_scores))
plt.rcParams['font.size'] = 16
bar = plt.bar([i for i in range(len(overall_means))], overall_means, yerr=overall_stdevs)
plt.xticks([i for i in range(len(overall_means))], ['Parcel ' + str(i+1) for i in range(len(overall_means))])
plt.ylabel('Saliency')
plt.savefig(output_dir + '/overall_means_parcels.png', bbox_inches='tight', dpi=300)
plt.rcParams['font.size'] = 12
plt.clf()
# 3. For a given parcel, we want the mean saliency for each task
task_means = []
task_stdevs = []
for parcel in parcel_keys:
curr_scores = np.mean(np.array([item[0] for item in parcels[parcel]]),axis=2)
task_means.append(np.mean(curr_scores,0))
task_stdevs.append(np.std(curr_scores,0))
ymax = math.ceil(max([max(task_means[i])+max(task_stdevs[i]) for i in range(len(task_means))]) *10) / 10.0
for i in range(len(task_means)):
curr_parcel = task_means[i]
curr_err = task_stdevs[i]
plt.rcParams['font.size'] = 16
plt.title('Parcel ' + str(i))
plt.bar([task_num for task_num in range(len(curr_parcel))], curr_parcel, yerr=curr_err)
plt.xticks([task_num for task_num in range(len(curr_parcel))], task_names)
plt.ylim(0,ymax)
plt.ylabel('Saliency')
plt.savefig(output_dir + '/task_means_parcel_'+str(i)+'.png', bbox_inches='tight', dpi=300)
plt.rcParams['font.size'] = 12
plt.clf()
# 4. For a given parcel, we want the mean saliency for each category (motor/cognitive)
# parcels[parcel] = [scores, tract_names, cluster_names]
category_means = []
category_stdevs = []
for parcel in parcel_keys:
task_scores = np.mean(np.array([item[0] for item in parcels[parcel]]),axis=2)
motor_scores = np.mean(task_scores[:,:2],1)
cognitive_scores = np.mean(task_scores[:,2:],1)
curr_scores = np.array([motor_scores, cognitive_scores]).T
category_means.append(np.mean(curr_scores,0))
category_stdevs.append(np.std(curr_scores,0))
ymax = math.ceil(max([max(category_means[i])+max(category_stdevs[i]) for i in range(len(category_means))]) *10) / 10.0
for i in range(len(category_means)):
plt.rcParams['font.size'] = 18
curr_parcel = category_means[i]
curr_err = category_stdevs[i]
plt.title('Parcel ' + str(i))
bar = plt.bar([category_num for category_num in range(len(curr_parcel))], curr_parcel, yerr=curr_err)
plt.xticks([category_num for category_num in range(len(curr_parcel))], ['Motor', 'Cognitive'])
plt.ylim(0,ymax)
plt.ylabel('Saliency')
plt.savefig(output_dir + '/category_means_parcel_'+str(i)+'.png', bbox_inches='tight', dpi=300)
plt.rcParams['font.size'] = 12
plt.clf()
# 4.1 Plot each category as a series
xlabels = ['Parcel '+str(k+1) for k in parcel_keys]
series_labels = ['Motor', 'Cognitive']
series_data = [[category_means[i][0] for i in range(len(category_means))], [category_means[i][1] for i in range(len(category_means))]]
series_errors = [[category_stdevs[i][0] for i in range(len(category_stdevs))], [category_stdevs[i][1] for i in range(len(category_stdevs))]]
bar_graph(xlabels, series_labels, series_data, series_errors, 'Saliency', output_dir=output_dir, fn='category_means', title=None, ymax=0.35)
# 5. For a given parcel, we want a count of how many of each white matter tract belong to it
parcel_freqs = []
for parcel in parcel_keys:
tracts = [item[1] for item in parcels[parcel]]
freq = {tract: 0 for tract in tract_names}
total = 0
for tract in tracts:
freq[tract] += 1
total += 1
distr = {tract: 100*freq[tract]/total for tract in freq.keys()}
parcel_freqs.append(distr)
ymax = math.ceil(max([max(distr.values()) for distr in parcel_freqs]) / 10) * 10
for parcel_id in range(len(parcel_keys)):
distr = parcel_freqs[parcel_id]
plt.title('Parcel ' + str(parcel_id))
plt.bar([i for i in range(len(distr.keys()))], [distr[tract] for tract in tract_order])
plt.xticks([i for i in range(len(distr.keys()))], tract_order)
plt.ylim(0,ymax)
plt.ylabel('Percentage of parcel')
plt.savefig(output_dir + '/tract_distr_parcel_'+str(parcel_id)+'.png', bbox_inches='tight', dpi=300)
plt.clf()
# 6. For a given tract, plot its parcel distribution
tract_clusters = {tract: [] for tract in set(tract_names)}
cluster_parcels = {}
for i in range(len(labels)):
tract_clusters[tract_names[i]].append(cluster_names[i])
cluster_parcels[cluster_names[i]] = labels[i]
tract_freqs = []
for tract in tract_order:
parcels_within = [cluster_parcels[cluster] for cluster in tract_clusters[tract]]
counts = {parcel: 0 for parcel in parcel_keys}
for parcel in parcels_within:
counts[parcel] += 1
distr = {parcel: 100*counts[parcel]/len(parcels_within) for parcel in counts}
tract_freqs.append(distr)
ymax = math.ceil(max([max(distr.values()) for distr in tract_freqs]) / 10) * 10
for tract_id in range(len(tract_order)):
distr = tract_freqs[tract_id]
plt.title(tract_order[tract_id])
plt.bar([i for i in range(len(distr.keys()))], [distr[parcel] for parcel in parcel_keys])
plt.xticks([i for i in range(len(distr.keys()))], [str(k) for k in parcel_keys])
plt.ylim(0,ymax)
plt.ylabel('Percentage of tract')
plt.xlabel('Parcel')
plt.savefig(output_dir + '/parcel_distr_tract_'+tract_order[tract_id]+'.png', bbox_inches='tight', dpi=300)
plt.clf()
if print_parcels:
for tract in tract_clusters:
print(tract)
for parcel in parcel_keys:
print('Parcel ' + str(parcel))
for cluster_name in tract_clusters[tract]:
if cluster_parcels[cluster_name] == parcel:
print(cluster_name)
print('--')
# 7. For a given tract, we want the average motor/cognitive score for it's consistuent parcels.
# i.e. For all parcels, store the mean motor/cognitve scores. Then assign this value to all
# clusters within that parcel. Then find the averages of these vectors for each tract.
cluster_parcel_vectors = {}
for i, cluster in enumerate(cluster_names):
parcel = cluster_parcels[cluster]
cluster_parcel_vectors[cluster] = category_means[parcel]
tract_mean_vectors = []
tract_std_vectors = []
all_cat_scores = []
for tract in tract_order:
clusters_in_tract = tract_clusters[tract]
scores = [cluster_parcel_vectors[cluster] for cluster in clusters_in_tract]
all_cat_scores.append(scores)
tract_mean_vectors.append(np.mean(np.array(scores),0))
tract_std_vectors.append(np.std(np.array(scores),0))
ymax = math.ceil(max([max(tract_mean_vectors[i])+max(tract_std_vectors[i]) for i in range(len(tract_mean_vectors))]) *10) / 10.0
for tract_id in range(len(tract_order)):
score, error = tract_mean_vectors[tract_id], tract_std_vectors[tract_id]
plt.title(tract_order[tract_id])
plt.bar([i for i in range(len(score))], score, yerr=error)
plt.xticks([i for i in range(len(score))], ['Motor', 'Cognitive'])
plt.ylim(0,ymax)
plt.ylabel('Saliency')
plt.savefig(output_dir + '/parcel_mean_category_'+tract_order[tract_id]+'.png', bbox_inches='tight', dpi=300)
plt.clf()
parcel_tracts = {} # parcel_tracts[parcel][tract] = [clusterA, clusterB, ...]
tract_clusters = {tract_name: [] for tract_name in tract_order}
cluster_parcel = {cluster_names[i]: labels[i] for i in range(len(cluster_names))}
cluster_scores = {cluster_names[i]: np.mean(saliency_values[i],axis=1) for i in range(len(cluster_names))}
for parcel in parcel_keys:
if parcel not in parcel_tracts:
parcel_tracts[parcel] = {}
for tract in sorted(list(set(tract_names))):
if tract not in parcel_tracts[parcel]:
parcel_tracts[parcel][tract] = []
for i in range(len(cluster_names)):
parcel = labels[i]
cluster_name = cluster_names[i]
tract = tract_names[i]
parcel_tracts[parcel][tract].append(cluster_name)
tract_clusters[tract].append(cluster_name)
parcel_tract_scores = {}
for parcel in parcel_tracts.keys():
if parcel not in parcel_tract_scores:
parcel_tract_scores[parcel] = {}
for tract in parcel_tracts[parcel].keys():
if len(parcel_tracts[parcel][tract]) == 0:
continue
mean_score = np.mean(np.array([[(cluster_scores[cluster_name][0]+cluster_scores[cluster_name][1])/2, (cluster_scores[cluster_name][2]+cluster_scores[cluster_name][3])/2] for cluster_name in parcel_tracts[parcel][tract]]),axis=0)
parcel_tract_scores[parcel][tract] = mean_score
for tract in tract_order:
scores = []
clusters_included = []
for cluster_name in tract_clusters[tract]:
parcel = cluster_parcels[cluster_name]
score = parcel_tract_scores[parcel][tract] # get the score for this parcel/tract combo
scores.append(score)
clusters_included.append(cluster_name)
motor_cog_score = np.array([(x[0]-x[1])/(x[0]+x[1]) for x in scores])
motor_cog_score = motor_cog_score / np.max(np.abs(motor_cog_score))
motor_cog_score = motor_cog_score/2+0.5
gen_model(motor_cog_score, 'tract', tract+'_mixed', clusters_included, 'bwr', opacity=0.1, output_dir=output_dir+'/vis')
# Expects shape: 5 x 53 x 20
def plot_tract_saliencies(scores, tract_names, output_dir, output_fn, tract_order=['ICP', 'MCP', 'SCP', 'IP', 'PF']):
mpl.use('TkAgg'), plt.close('all'), plt.clf()
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Divide into tracts
tract_scores = {tract_name: [] for tract_name in set(tract_names)}
for i, tract in enumerate(tract_names):
tract_scores[tract].append(scores[:,i,:])
# Convert mean/stdev for each tract
means = {}
stdevs = {}
for tract in tract_scores.keys():
# average over all the individual clusters, and the 20 features
base_score = np.mean(np.array(tract_scores[tract]),axis=(0,2))
# average over the folds
mean = np.mean(base_score,axis=0)
stdev = np.std(base_score,axis=0)
means[tract] = mean
stdevs[tract] = stdev
means_plot, stdevs_plot, names_plot = [], [], []
for tract in tract_order:
#for tract in sorted(tract_scores.keys()):
names_plot.append(tract)
means_plot.append(means[tract])
stdevs_plot.append(stdevs[tract])
print(np.array(means_plot).shape)
plt.bar([i for i in range(len(names_plot))], means_plot, yerr=stdevs_plot)
plt.xticks([i for i in range(len(names_plot))],names_plot)
plt.ylabel('Saliency')
plt.savefig(output_dir + '/'+output_fn+'.png', bbox_inches='tight', dpi=300)
plt.clf()
# Expects a N x num_features vector, to which it will calculate the
# mean and stadard deviation over N
def plot_feature_saliencies(data, names, output_dir, output_fn, title=None, ymax=None):
mpl.use('TkAgg'), plt.close('all')
if not os.path.exists(output_dir):
os.makedirs(output_dir)
feature_colors = []
base_names = {'FA1': 'red', 'FA2': 'orange', 'NoS': 'green', 'NoP': 'purple', 'Trace1': 'blue', 'Trace2': 'gold'}
for feature_name in names:
feature_colors.append(base_names[feature_name.split('.')[0]])
stat_colors = []
stat_type = {'Min': 'red', 'Max': 'orange', 'Median': 'green', 'Mean': 'purple', 'Variance': 'blue'}
for feature_name in names:
if feature_name == 'NoS':
stat_colors.append('gold')
elif feature_name == 'NoP':
stat_colors.append('pink')
else:
stat_colors.append(stat_type[feature_name.split('.')[-1]])
means, stdevs = np.mean(data,0), np.std(data,0)
print('Feature Means ' + title)
print(np.mean(means[:5]), np.mean(means[5:10]), means[10], means[11], np.mean(means[12:16]), np.mean(means[16:]))
print('Stat Means ' + title)
print('Min, Max, Median, Mean, Variance')
print(np.mean(means[np.array([0,5,12,16])]), np.mean(means[np.array([1,6,13,17])]), np.mean(means[np.array([2,7,14,18])]), np.mean(means[np.array([3,8,15,19])]), np.mean(means[np.array([4,9])]))
if ymax is not None:
plt.ylim(0,ymax)
bar = plt.bar([i for i in range(len(names))], means, yerr=stdevs)
plt.xticks([i for i in range(len(names))],names,rotation=90)
plt.ylabel('Saliency')
if title is not None:
plt.title(title)
plt.savefig(output_dir + '/'+output_fn+'.png', bbox_inches='tight', dpi=300)
# Plot with feature colors
for i in range(len(bar)):
bar[i].set_color(feature_colors[i])
plt.savefig(output_dir + '/'+output_fn+'_feature_colors.png', bbox_inches='tight', dpi=300)
# Plot with stat colours
for i in range(len(bar)):
bar[i].set_color(stat_colors[i])
plt.savefig(output_dir + '/'+output_fn+'_stat_colors.png', bbox_inches='tight', dpi=300)
plt.clf()
def concatenate_images(fn1, fn2, output_dir, output_fn):
im1, im2 = cv2.imread(fn1), cv2.imread(fn2)
concat = np.hstack((im1, im2))
cv2.imwrite(output_dir + '/' + output_fn + '.png', concat)
def average_hemispheres(scores, cluster_names, tract_names):
unique_cluster_ids = sorted(list(set([x.split('.')[-1] for x in cluster_names])))
cluster_ids = [x.split('.')[-1] for x in cluster_names]
# Create a list of cluster types
cluster_types = []
for cluster_id in unique_cluster_ids:
index = cluster_ids.index(cluster_id) # find the first match, this is enough for now
if 'commissural.' in cluster_names[index]:
cluster_types.append('commissural')
else:
cluster_types.append('hemisphere')
new_scores = []
new_tract_names = []
for i, cluster_id in enumerate(unique_cluster_ids):
# Determine all indexes of the current cluster_id
all_locations = []
for index, cluster_name in enumerate(cluster_names):
if cluster_id in cluster_name:
all_locations.append(index)
# Either take directly or average the hemispheres, and store the result
if cluster_types[i] == 'commissural':
new_scores.append(scores[all_locations[0]])
new_tract_names.append(tract_names[all_locations[0]])
if len(all_locations) != 1:
print('ERROR: Invalid number of cluster names.')
sys.exit()
else:
mean = (scores[all_locations[0]] + scores[all_locations[1]])/2
new_scores.append(mean)
scores[all_locations[1]] = mean
new_tract_names.append(tract_names[all_locations[0]])
if len(all_locations) != 2:
print('ERROR: Invalid number of cluster names.')
sys.exit()
return new_scores, unique_cluster_ids, new_tract_names
def write_to_file(s, output_dir, fn):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
with open(output_dir + '/' + fn, 'w') as f:
f.write(s)
class TransformerBlock(nn.Module):
def __init__(self, d_model, nhead, num_layers, dim_feedforward, dropout_rate=0):
super(TransformerBlock, self).__init__()
self.multi_head_attention = nn.MultiheadAttention(d_model, nhead)
self.layer_norm1 = nn.LayerNorm(d_model)
self.feed_forward = nn.Sequential(
nn.Linear(d_model, dim_feedforward),
nn.ReLU(),
nn.Dropout(dropout_rate),
nn.Linear(dim_feedforward, d_model)
)
self.layer_norm2 = nn.LayerNorm(d_model)
self.dropout1 = nn.Dropout(dropout_rate)
self.dropout2 = nn.Dropout(dropout_rate)
def forward(self, x):
# Multi-Head Attention
attn_output, _ = self.multi_head_attention(self.layer_norm1(x), self.layer_norm1(x), self.layer_norm1(x))
x = x + self.dropout1(attn_output)
# Feed Forward
ff_output = self.feed_forward(self.layer_norm2(x))
x = x + self.dropout2(ff_output)
return x
class FC(nn.Module):
def __init__(self, input_channels=1, num_classes=1, dropout=90):
super(FC, self).__init__()
self.decoder = nn.Sequential(
nn.Linear(input_channels, 4096),
nn.ReLU(),
nn.Dropout(dropout/100),
nn.Linear(4096, 2048),
nn.ReLU(),
nn.Dropout(dropout/100),
nn.Linear(2048, 1024),
nn.ReLU(),
nn.Dropout(dropout/100),
nn.Linear(1024, 512),
nn.ReLU(),
nn.Dropout(dropout/100),
nn.Linear(512, 256),
nn.ReLU(),
nn.Dropout(dropout/100),
nn.Linear(256, 128),
nn.ReLU(),
nn.Dropout(dropout/100),
nn.Linear(128, num_classes),
)
def forward(self, x):
x = self.decoder(x)
return x
class CNN(nn.Module):
def __init__(self, input_channels=1, num_classes=1, dropout=90):
super(CNN, self).__init__()
self.encoder = nn.Sequential(
nn.Conv1d(in_channels=1, out_channels=16, kernel_size=5, stride=1, padding=2),
nn.ReLU(),
nn.Dropout(dropout/100),
nn.Conv1d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=2),
nn.ReLU(),
nn.Dropout(dropout/100),
nn.Conv1d(in_channels=32, out_channels=64, kernel_size=5, stride=1, padding=2),
nn.ReLU(),
nn.Dropout(dropout/100),
nn.Conv1d(in_channels=64, out_channels=100, kernel_size=5, stride=1, padding=2),
)
self.decoder = nn.Sequential(
nn.Linear(100 * input_channels, 100),
nn.Dropout(dropout/100),
nn.Linear(100, 64),
nn.Dropout(dropout/100),
nn.Linear(64, num_classes),
)
def forward(self, x):
# Input shape: batch_size x 500
# Reshape for 1D-CNN: batch_size x 1 x 500
x = x.unsqueeze(1)
x = self.encoder(x)
x = x.view(x.size(0), -1) # flatten
x = self.decoder(x)
return x
class TransformerModel(nn.Module):
def __init__(self, input_channels=1940, num_classes=1, dropout=90, d_model=512, nhead=8, num_layers=6,dim_feedforward=2048):
super(TransformerModel, self).__init__()
self.embedding = nn.Linear(input_channels, d_model)
self.transformer_blocks = nn.ModuleList(
[TransformerBlock(d_model, nhead, num_layers, dim_feedforward, dropout/100) for _ in range(num_layers)]
)
self.dense_layers = nn.Sequential(
nn.Linear(d_model, d_model),
# Add more dense layers if necessary
nn.Dropout(dropout/100),
nn.Linear(d_model, 11)
)
self.dropout = nn.Dropout(dropout/100)
def forward(self, x):
x = self.embedding(x)
x = x.unsqueeze(0) # Add a batch dimension
for block in self.transformer_blocks:
x = block(x)
x = self.dense_layers(x)
return x.squeeze(0)
class CustomDataset(torch.utils.data.Dataset):
def __init__(self, folds, dataset_dir='/Users/aritchetchenian/Desktop/graph_cnn', metric='TPVT', mean=None, stdev=None):
# Data structure for storing all items
self.subjects = []
all_names = ['Endurance_AgeAdj', 'GaitSpeed_Comp', 'Dexterity_AgeAdj', 'Strength_AgeAdj', 'PicSeq_AgeAdj', 'CardSort_AgeAdj', 'Flanker_AgeAdj', 'ReadEng_AgeAdj', 'PicVocab_AgeAdj', 'ProcSpeed_AgeAdj', 'ListSort_AgeAdj']
self.labels = [get_labels_mapping('./csvs/' + name + '.csv') for name in all_names]
# Gather subject IDs
subject_ids = []
for fold in folds:
subject_ids += ['/'.join(x.replace('\\', '/').split('/')[-2:]) for x in sorted(glob(dataset_dir + '/fold'+str(fold)+'/*.npy'))]
subject_ids.sort()
# Calculate the mean/stdev
if mean is None:
data = np.array([np.load(dataset_dir + '/' + subject) for subject in subject_ids])
self.mean, self.stdev = np.mean(data,axis=0), np.std(data,axis=0)
else:
self.mean, self.stdev = mean, stdev
for subject in subject_ids:
total_label = np.array([self.labels[i][subject.split('/')[-1].split('.')[0]] for i in range(len(self.labels))])
input_vector = np.load(dataset_dir + '/' + subject)
# standardisation
input_vector = (input_vector - self.mean) / (self.stdev + 0.00001)
self.subjects.append([torch.from_numpy(input_vector).float(), torch.tensor(total_label).float()])
def __getitem__(self, idx):
return self.subjects[idx]
def get_stats(self):
return [self.mean, self.stdev]
def __len__(self):
return len(self.subjects)
def gen_parcel_stats(parcel_labels, input_vectors, metrics, tract_names, categories, output_dir):
unique_labels = sorted(list(set(parcel_labels)))
unique_categories = sorted(list(set(categories)))
if not os.path.exists(output_dir):
os.makedirs(output_dir)
mpl.use('TkAgg')
plt.close('all')
# Calculate tract ownerships (e.g. parcel 0 is 20% SCP, 30% ICP, etc.)
for label in unique_labels:
counts = {}
for tract_name in sorted(list(set(tract_names))):
counts[tract_name] = 0
total_count = 0
for i in range(len(parcel_labels)):
if parcel_labels[i] == label:
counts[tract_names[i]] += 1
total_count += 1
plt.clf()
plt.figure(figsize=(10,6))
plt.bar([tract_name for tract_name in sorted(list(set(tract_names)))], [100*counts[tract_name]/total_count for tract_name in sorted(list(set(tract_names)))])
plt.xticks(fontsize=45)
plt.savefig(output_dir + '/'+str(label)+'_tract_distr.png', bbox_inches='tight')
# Calculate mean saliency of each task for each label (e.g. parcel 0 has 0.1 saliency for strength, etc.)
for label in unique_labels:
vectors = []
for i in range(len(parcel_labels)):
curr_label = parcel_labels[i]
input_vector = input_vectors[i]
if curr_label == label:
vectors.append(input_vector)
vectors_mean = np.mean(np.array(vectors),0)
vectors_stdev = np.std(np.array(vectors),0)
# Plot the mean saliency for each task
plt.close('all')
plt.clf()
plt.figure(figsize=(10,6))
plt.bar([metric for metric in metrics], vectors_mean, yerr=vectors_stdev, capsize=1)
plt.xticks(rotation=10, fontsize=20)
plt.xlabel('Metric')
plt.ylabel('Mean Saliency')
plt.ylim(0,1)
plt.title('Mean Saliencies Per Cluster')
plt.savefig(output_dir + '/mean_task_saliencies_'+str(label)+'.png', bbox_inches='tight')
# Plot the mean saliency for each category
category_means = [[] for category in unique_categories]
for i, category in enumerate(categories):
category_index = unique_categories.index(category)
category_means[category_index].append(vectors_mean[i])
category_means = [sum(x)/len(x) for x in category_means]
plt.clf()
plt.figure(figsize=(10,6))
plt.bar([category for category in unique_categories], category_means)
plt.xticks(fontsize=34)
plt.ylabel('Mean Saliency')
plt.ylim(0,1)
plt.title('Mean Saliencies Per Cluster')
plt.savefig(output_dir + '/mean_category_saliencies_'+str(label)+'.png', bbox_inches='tight')
plt.figure(figsize=(10,6))
plt.bar([category for category in unique_categories], category_means)
plt.xticks(fontsize=34)
plt.xlabel('Category')
plt.ylabel('Mean Saliency')
plt.title('Mean Saliencies Per Cluster')
plt.savefig(output_dir + '/mean_category_saliencies_yaxis_'+str(label)+'.png', bbox_inches='tight')
def cosine(a,b):
dot = np.dot(a,b)
normA = np.linalg.norm(a)
normB = np.linalg.norm(b)
return dot / (normA * normB)
def mean_cosine_similarity(all_folds):
all_sims = []
for a,b in combinations(list(all_folds),2):
sim = cosine(a,b)
all_sims.append(sim)
return np.mean(np.array(all_sims)), np.std(np.array(all_sims))
# Given a set of scores (num_clusters x num_categories), and an assignment of categories
# Return the mean score for each cluster for each category
# i.e. input is 97 cluster scores for each of strength, dexterity, TPVT, Flanker
# then output would be 97 cluster scores for motor and cognitive
def get_category_scores(scores, categories):
# Initialise
category_scores = {}
for category in set(categories):
category_scores[category] = []
# Aggregate
for i in range(len(categories)):
category = categories[i]
mean_scores = scores[:,i]
category_scores[category].append(mean_scores)
# Summarise
for category in set(categories):
category_scores[category] = sum(category_scores[category]) / len(category_scores[category])
return category_scores
def split_into_tracts(labels, cluster_names, tract_names):
new_labels = [[] for i in range(len(set(cluster_names)))]
norm_new_labels = [[] for i in range(len(set(cluster_names)))]
new_clusters = [[] for i in range(len(set(cluster_names)))]
tract_order = sorted([x for x in set(tract_names)])
# Globally normalise the labels
norm_labels = norm(labels)
for i in range(len(labels)):
label = labels[i]
norm_label = norm_labels[i]
name = cluster_names[i]
tract = tract_names[i]
pos = tract_order.index(tract)
norm_new_labels[pos].append(norm_label)
new_labels[pos].append(label)
new_clusters[pos].append(name)
return norm_new_labels, new_labels, new_clusters, tract_order
def get_freq(items, possible_labels):
freq = {}
for item in sorted(possible_labels):
freq[item] = 0
for item in items:
freq[item] += 1
return freq
def vis_tracts(labels, cluster_names, tract_names, output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
norm_new_labels, new_labels, new_clusters, tract_order = split_into_tracts(labels, cluster_names, tract_names)
# Generate a model for each tract
for tract_num in range(len(tract_order)):
gen_model(norm_new_labels[tract_num], 'cluster ID', tract_order[tract_num], new_clusters[tract_num], 'rainbow', opacity=0.1, output_dir=output_dir)
# Plot the k-means label distribution for each tract
freqs = []
tract_labels = []
for tract_num in range(len(tract_order)):
all_labels = new_labels[tract_num]
freq = get_freq(all_labels, list(set(labels)))
sorted_freq = [freq[x] for x in sorted(list(freq.keys()))]
freqs.append(sorted_freq)
tract_labels.append(tract_order[tract_num])
freq_labels = sorted(list(freq.keys()))
mpl.use('TkAgg')
plt.close('all')
plt.clf()
num_elements = len(tract_labels)
num_components = len(freqs[0])
bar_width = 0.2
index = np.arange(num_elements)
fig, ax = plt.subplots()
for i, component in enumerate(freq_labels):
component_values = [freq[component] for freq in freqs]
bars = plt.bar(index + i * bar_width, component_values, width=bar_width, label=f'KMeans Cluster {component}')
# Add some text for labels, title, and axes ticks
plt.xlabel('Tracts')
plt.ylabel('Frequency')
plt.title('Frequency of Components for Each Element')
plt.xticks(index + bar_width, tract_labels)
plt.legend()
plt.savefig(output_dir + '/kmeans_tract_distribution.png', bbox_inches='tight')
def vis_kmeans_clusters(labels, cluster_names, output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Get the names of the clusters in each parcel
per_cluster_names = [[] for i in range(len(set(labels)))]
for i, label in enumerate(labels):
name = cluster_names[i]
per_cluster_names[label].append(name)
# Generate the .mrml files
for kmeans_cluster_id, val in enumerate(sorted(list(set(norm(labels))))):
gen_model([val for i in range(len(per_cluster_names[kmeans_cluster_id]))], 'per_cluster', str(kmeans_cluster_id), per_cluster_names[kmeans_cluster_id], 'rainbow', opacity=0.1, output_dir=output_dir)
def tract_to_id(names):
unique_names = []
for item in names:
if item not in unique_names:
unique_names.append(item)
maps = [i for i in range(len(unique_names))]
ids = []
for name in names:
name_index = unique_names.index(name)
ids.append(maps[name_index])
return ids
def get_cluster_mean(salience, num_features):
return np.mean(salience.reshape(-1, num_features), axis=1)
def norm(x):
if np.min(x) == np.max(x):
print('Min and Max are the same: %.2f' % (np.min(x)))
return x
return (x-np.min(x)) / (np.max(x)-np.min(x))
# Return a subject->label mapping for float labels
def get_labels_mapping(f):
result = {}
with open(f, newline='') as csvfile:
reader = csv.reader(csvfile)
next(reader)
for row in reader:
result[row[0]] = float(row[1])
return result
def adjacent_values(vals, q1, q3):
upper_adjacent_value = q3 + (q3 - q1) * 1.5
upper_adjacent_value = np.clip(upper_adjacent_value, q3, vals[-1])
lower_adjacent_value = q1 - (q3 - q1) * 1.5
lower_adjacent_value = np.clip(lower_adjacent_value, vals[0], q1)
return lower_adjacent_value, upper_adjacent_value
# Given an array: num_violins x num_clusters, plot a violin plot
def plot_violin(scores, output_dir, output_fn, title, ylabel, ymin, ymax, labels, hbar=True):
if type(scores) != type([]):
print('ERROR: type must be list.')
sys.exit()
mpl.use('TkAgg'), plt.close('all'), plt.clf()
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Do the actual plotting
plt.violinplot(scores, showmeans=False, showextrema=False, showmedians=False)
quartile1, medians, quartile3 = np.percentile(scores, [25, 50, 75], axis=1)
whiskers = np.array([adjacent_values(sorted_array, q1, q3) for sorted_array, q1, q3 in zip(scores, quartile1, quartile3)])
whiskers_min, whiskers_max = whiskers[:, 0], whiskers[:, 1]
inds = np.arange(1, len(medians) + 1)
plt.scatter(inds, medians, marker='o', color='white', s=6, zorder=3)
plt.vlines(inds, quartile1, quartile3, color='k', linestyle='-', lw=3)
if title is not None:
plt.title(title)
plt.ylabel(ylabel)
plt.ylim(ymin, ymax)
plt.xticks([i+1 for i in range(len(scores))], labels, rotation=90)
if hbar:
plt.axhline(y=0.5, linestyle='--', color='r')
plt.rcParams['font.size'] = 12
plt.savefig(output_dir + '/'+output_fn+'.png', bbox_inches='tight', dpi=300)
plt.clf()
def gen_model(vals, label, output_suffix, cluster_names, color_map, output_dir, opacity=1):
plt.close('all')
# Create the output directory if required
output_fn = output_dir + '/result_'+label+'_'+output_suffix+'.mrml'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Compute the colors based on the desired colormap
if color_map == 'custom_bwr':
colors = [(0, 0, 1), (0, 0, 0), (1, 0, 0)]
cmap = LinearSegmentedColormap.from_list("custom_bwr", colors, N=100)
else:
cmap = mpl.colormaps[color_map]
colors = cmap(vals) * 256
# Collect a list of cluster filenames
input_polydatas = list()
for i in range(len(cluster_names)):
if BILATERAL:
input_dir = 'C:\Downloads\ORG-800FiberClusters'
else:
base_dir = 'C:\Downloads\ORG-separated'
if 'left' in cluster_names[i]:
input_dir = base_dir + '/tracts_left_hemisphere'
elif 'right' in cluster_names[i]:
input_dir = base_dir + '/tracts_right_hemisphere'
elif 'commissural' in cluster_names[i]:
input_dir = base_dir + '/tracts_commissural'
else:
print("ERROR: Invalid cluster")
sys.exit()
cluster_name = cluster_names[i].split('.')[-1]
name = "{0}/"+cluster_name+"*"
input_mask = name.format(input_dir)
filepath = glob(input_mask)
input_polydatas.append(filepath[0])