-
Notifications
You must be signed in to change notification settings - Fork 1
/
statisticalAnalysis.m
1606 lines (1541 loc) · 53.7 KB
/
statisticalAnalysis.m
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
%% ---- Statstical Analysis ---- %%
% This function is to calculate the performance and evaluation metrics
% those extacted from Scan 1 and Scan 2 for the
% study "The Reproducibility of deep learning-based segmentation of the
% prostate on T2-weighted MR images".
%
% Sunoqrot, M.R.S.; Selnæs, K.M.; Sandsmark, E.; Langørgen, S.; Bertilsson,
% H.; Bathen, T.F.; Elschot, M. The Reproducibility of Deep Learning-Based
% Segmentation of the Prostate Gland and Zones on T2-Weighted MR Images.
% Diagnostics 2021, 11, 1690. https://doi.org/10.3390/diagnostics11091690
% https://www.mdpi.com/2075-4418/11/9/1690
%
% By Mohammed R. S. Sunoqrot, MR cancer group, NTNU, Trondheim, Norway
% 02.Nov.2020
%
% Input:
% 1. masterPath: The path to the master analysis file. (string)
% 2. esPath: The path to the automatically segmented cases post processing. (string)
% 3. features: The extracted shape features. (structure)
% 4. factors: The factors we need to transfer the mertics to percentage. (structure)
%
% output:
% 1. stats: The statistical analysis results. (structure)
%
function stats = statisticalAnalysis(masterPath,esPath,features,factors)
%% Statistical test type
test = {'signrank','ranksum'};
%---- Main investigation
%% Get Scanning protocol parameters for investigation set
stats.protocol = getProtocol(masterPath);
%% Get Scanning protocol parameters for training set
stats.protocolTr = getProtocolTr(masterPath);
%% Calculate ICC
stats.ICC = calculateICC(features);
%% Calculate the significancy for ICC
stats.sICC = calculateSicc(stats.ICC);
%% Calculate CV
stats.CV = calculateCV(features);
%% Calculate the significancy for CV
stats.sCV = calculateScv(stats.CV,test{1});
%% Calculate the perfromance scores
stats.pScores = qualityControl(masterPath,esPath,factors,0);
%% Calculate the significancy for performance metrics
stats.sScores = calculateSscores(stats.pScores.pScores,test{1});
%% Calculate number of included slices
stats.nSlices = calculateNslices(masterPath,esPath,[]);
%% Calculate the significancy for number of included slices
stats.sSlices = calculateSslices(stats.nSlices,test{1});
%% Calculate the volume
stats.Volume = calculateVolume(features);
%% Calculate the significancy for volume
stats.sVolume = calculateSvolume(stats.Volume,test{1});
%% Calculate the change in volume
stats.cVolume = calculateCvolume(features);
%% Calculate significancy forthe change in volume
stats.sCvolume = calculateSCvolume(stats.cVolume,test{1});
%% Calculate Median +/- SD for Scores
stats.MSscores = clalcMSscores(stats.pScores.pScores);
%% Calculate Median +/- SD for number of slices
stats.MSslices = clalcMSslices(stats.nSlices);
%% Calculate Median +/- SD for Change in Volume
stats.MScvolume = clalcMScvolume(stats.cVolume);
%% Calculate DSC Difference Median +/- SD
stats.DMSscores = clalcDMSscores(stats.pScores.pScores);
%---- After Implementing QC system
%% Get Idx of the excluded cases based on the quality control score
stats.ExcludedIdx = excludedIdx(stats.pScores.pScores);
%% Get features structure after excluding cases
stats.FeaturesAE = featuresAE(features,stats.ExcludedIdx);
%% Calculate ICC
stats.ICCAE = calculateICC(stats.FeaturesAE);
%% Generate ICC after 1000 random exclusion
stats.ICCR = iccRN(features,stats.ExcludedIdx);
%% Calculate the significancy for ICC
stats.sICCAE = calculateSicc(stats.ICCAE);
%% Calculate CV
stats.CVAE = calculateCV(stats.FeaturesAE);
%% Calculate the significancy for CV
stats.sCVAE = calculateScv(stats.CVAE,test{2});
%% Calculate the perfromance scores
stats.pScoresAE = getScoresAE(stats.pScores.pScores,stats.ExcludedIdx);
%% Calculate the significancy for performance metrics
stats.sScoresAE = calculateSscores(stats.pScoresAE,test{2});
%% Calculate number of included slices
stats.nSlicesAE = calculateNslices(masterPath,esPath,stats.ExcludedIdx);
%% Calculate the significancy for number of included slices
stats.sSlicesAE = calculateSslices(stats.nSlicesAE,test{2});
%% Calculate the volume
stats.VolumeAE = calculateVolume(stats.FeaturesAE);
%% Calculate the significancy for volume
stats.sVolumeAE = calculateSvolume(stats.VolumeAE,test{2});
%% Calculate the change in volume
stats.cVolumeAE = calculateCvolume(stats.FeaturesAE);
%% Calculate significancy forthe change in volume
stats.sCvolumeAE = calculateSCvolume(stats.cVolumeAE,test{2});
%---- Compare between before and after implementing QCS
%% Calculate the significancy for ICC between before and after
stats.sICCBA = calculateSiccBA(stats.ICC,stats.ICCAE);
%% Calculate the significancy for ICC between before and after (qith random 1000)
stats.sICCBAR = calculateSiccBAR(stats.ICCR,stats.ICCAE);
%---- Before including post-processing step
BPP = load('features-beforePP.mat');
esbppPath = fullfile(masterPath,'Data','Segmentation','Automated');
%% Calculate ICC
stats.ICCBPP = calculateICC(BPP.features);
%% Calculate the significancy for ICC
stats.sICCBPP = calculateSicc(stats.ICCBPP);
%% Calculate CV
stats.CVBPP = calculateCV(BPP.features);
%% Calculate the significancy for CV
stats.sCVBPP = calculateScv(stats.CVBPP,test{1});
%% Calculate the perfromance scores
stats.pScoresBPP = qualityControl(masterPath,esbppPath,factors,0);
%% Calculate the significancy for performance metrics
stats.sScoresBPP = calculateSscores(stats.pScoresBPP.pScores,test{1});
%% Calculate number of included slices
stats.nSlicesBPP = calculateNslices(masterPath,esbppPath,[]);
%% Calculate the significancy for number of included slices
stats.sSlicesBPP = calculateSslices(stats.nSlicesBPP,test{1});
%% Calculate the volume
stats.VolumeBPP = calculateVolume(BPP.features);
%% Calculate the significancy for volume
stats.sVolumeBPP = calculateSvolume(stats.VolumeBPP,test{1});
%% Calculate the change in volume
stats.cVolumeBPP = calculateCvolume(BPP.features);
%% Calculate significancy forthe change in volume
stats.sCvolumeBPP = calculateSCvolume(stats.cVolumeBPP,test{1});
%---- Compare between before and after including post-processing
%% Calculate the significancy for ICC between before and after post-process step
stats.sICCBABPP = calculateSiccBA(stats.ICC,stats.ICCBPP);
%% Save
save('stats.mat','stats')
end
%% getProtocol
% This function is to get the scanning protocol parameters for Scan 1 and
% Scan 2 cases
%
% Input:
% 1. masterPath: The path to the master analysis file. (string)
%
% output:
% 1. protocol: The scanning protocol parameters. (structure)
%
function protocol = getProtocol(masterPath)
% DICOM folders path
dicomP = '.../dataset';
% Original Cases path
oPath = fullfile(masterPath,'Data','Original');
% Loop over Scans
oD = dir(oPath);
oD = oD(~ismember({oD.name},{'.','..'}));
for kk = 1:numel(oD)
% Make table to fill later
protocol.(oD(kk).name) = table;
% Loop over cases
cD = dir(fullfile(oD(kk).folder,oD(kk).name,'*.mhd'));
for cc = 1:numel(cD)
% Name
protocol.(oD(kk).name).Name(cc,:) = {cD(cc).name(1:18)};
% Get into the case DICOM folder
dicomD = dir(fullfile(dicomP,cD(cc).name(1:8),...
cD(cc).name(1:18)));
% Find the correct T2W secquance
% Patterns we want
pattern = ["T2W_tra","T2W tra","T2W TRA","T2W_TRA",...
"t2_tse_tra","t2 tse tra","t2 tse _ra","t2_tse tra"];
% Secquances with the pattern
sIdx = contains({dicomD.name},pattern,'IgnoreCase',true);
imagefile = dicomD(sIdx);
% In case more than one match, order them
infoTemp = zeros(1,numel(imagefile));
for mm = 1:numel(imagefile)
sD = dir(fullfile(imagefile(mm).folder,imagefile(mm).name));
sD = sD(~ismember({sD.name},{'.','..'}));
inf = dicominfo(fullfile(sD(1).folder,sD(1).name));
infoTemp(mm) = inf.SeriesNumber;
end
[~,maxSIdx] = max(infoTemp);
[~,minSIdx] = min(infoTemp);
% Read the newest sequance except if it was an exception
exceptions = {'.....'};
if ismember(cD(cc).name(1:18),exceptions)
seq = fullfile(imagefile(minSIdx).folder,imagefile(minSIdx).name);% espcial cases
else
seq = fullfile(imagefile(maxSIdx).folder,imagefile(maxSIdx).name);
end
% Get the dicom tags
iD = dir(seq);
iD = iD(~ismember({iD.name},{'.','..'}));
info = dicominfo(fullfile(iD(1).folder,iD(1).name));
protocol.(oD(kk).name).PatientAge(cc,:) = str2double(info.PatientAge(1:end-1));
protocol.(oD(kk).name).ScanDate(cc,:) =...
datestr(datenum(num2str(str2double(info.SeriesDate),'%d'),'yyyymmdd'),'yyyy/mm/dd');
protocol.(oD(kk).name).MagneticFieldStrength(cc,:) = info.MagneticFieldStrength;
protocol.(oD(kk).name).Manufacturer(cc,:) = {info.Manufacturer};
protocol.(oD(kk).name).ManufacturerModelName(cc,:) = {info.ManufacturerModelName};
protocol.(oD(kk).name).ScanningSequence(cc,:) = {info.ScanningSequence};
protocol.(oD(kk).name).RepetitionTime(cc,:) = info.RepetitionTime;
protocol.(oD(kk).name).EchoTime(cc,:) = info.EchoTime;
protocol.(oD(kk).name).FlipAngle(cc,:) = info.FlipAngle;
protocol.(oD(kk).name).NumberOfAverages(cc,:) = info.NumberOfAverages;
protocol.(oD(kk).name).SlicesNumber(cc,:) = numel(iD)-2;
protocol.(oD(kk).name).Width(cc,:) = info.Width;
protocol.(oD(kk).name).Height(cc,:) = info.Height;
protocol.(oD(kk).name).SliceThickness(cc,:) = info.SliceThickness;
protocol.(oD(kk).name).PixelSpacing(cc,:) = info.PixelSpacing(1);
end
end
end
%% getProtocolTr
% This function is to get the scanning protocol parameters for segmentation
% training set.
%
% Input:
% 1. masterPath: The path to the master analysis file. (string)
%
% output:
% 1. protocol: The scanning protocol parameters. (table)
%
function protocol = getProtocolTr(masterPath)
% DICOM folders path
dicomP = '.../dataset';
% Original Cases path
oPath = fullfile(masterPath,'Data','Train','Original');
% Make table to fill later
protocol = table;
% Loop over cases
cD = dir(fullfile(oPath,'*.mhd'));
for cc = 1:numel(cD)
% Name
protocol.Name(cc,:) = {cD(cc).name(1:18)};
% Get into the case DICOM folder
dicomD = dir(fullfile(dicomP,cD(cc).name(1:8),...
cD(cc).name(1:18)));
% Find the correct T2W secquance
% Patterns we want
pattern = ["T2W_tra","T2W tra","T2W TRA","T2W_TRA",...
"t2_tse_tra","t2 tse tra","t2 tse _ra","t2_tse tra"];
% Secquances with the pattern
sIdx = contains({dicomD.name},pattern,'IgnoreCase',true);
imagefile = dicomD(sIdx);
% In case more than one match, order them
infoTemp = zeros(1,numel(imagefile));
for mm = 1:numel(imagefile)
sD = dir(fullfile(imagefile(mm).folder,imagefile(mm).name));
sD = sD(~ismember({sD.name},{'.','..'}));
inf = dicominfo(fullfile(sD(1).folder,sD(1).name));
infoTemp(mm) = inf.SeriesNumber;
end
[~,maxSIdx] = max(infoTemp);
[~,minSIdx] = min(infoTemp);
% Read the newest sequance except if it was an exception
exceptions = {'.....'};
if ismember(cD(cc).name(1:18),exceptions)
seq = fullfile(imagefile(minSIdx).folder,imagefile(minSIdx).name);% espcial cases
else
seq = fullfile(imagefile(maxSIdx).folder,imagefile(maxSIdx).name);
end
% Get the dicom tags
iD = dir(seq);
iD = iD(~ismember({iD.name},{'.','..'}));
info = dicominfo(fullfile(iD(1).folder,iD(1).name));
protocol.PatientAge(cc,:) = str2double(info.PatientAge(1:end-1));
protocol.ScanDate(cc,:) =...
datestr(datenum(num2str(str2double(info.SeriesDate),'%d'),'yyyymmdd'),'yyyy/mm/dd');
protocol.MagneticFieldStrength(cc,:) = info.MagneticFieldStrength;
protocol.Manufacturer(cc,:) = {info.Manufacturer};
protocol.ManufacturerModelName(cc,:) = {info.ManufacturerModelName};
protocol.ScanningSequence(cc,:) = {info.ScanningSequence};
protocol.RepetitionTime(cc,:) = info.RepetitionTime;
protocol.EchoTime(cc,:) = info.EchoTime;
protocol.FlipAngle(cc,:) = info.FlipAngle;
protocol.NumberOfAverages(cc,:) = info.NumberOfAverages;
protocol.SlicesNumber(cc,:) = numel(iD)-2;
protocol.Width(cc,:) = info.Width;
protocol.Height(cc,:) = info.Height;
protocol.SliceThickness(cc,:) = info.SliceThickness;
protocol.PixelSpacing(cc,:) = info.PixelSpacing(1);
end
end
%% calculateICC
% This function is to calculate the intraclass correlation (ICC)
% between the features those extacted from Scan 1 and Scan 2
%
% Input:
% 1. features: The extracted shape features. (structure)
%
% output:
% 1. ICC: The calculated ICC results. (structure)
%
function ICC = calculateICC(features)
% Get Netwroks names
nets = fieldnames(features);
% Loop over Netwroks
for ii = 1:numel(nets)
% Get regions names
regs = fieldnames(features.(nets{ii}));
% Make tables to fill
ICC.(nets{ii}).ICC = table;
ICC.(nets{ii}).ICCpVal = table;
ICC.(nets{ii}).ICCCI = table;
% Loop over regions
for jj = 1:numel(regs)
% Get scans names
scans = fieldnames(features.(nets{ii}).(regs{jj}));
% Get features names
fNames = features.V_Net_3D.PZ.Scan1.Properties.VariableNames;
% Loop over features to calculate ICC
for ff = 1:numel(fNames)
% Dummy matrix
temp = [features.(nets{ii}).(regs{jj}).(scans{1}).(fNames{ff}),...
features.(nets{ii}).(regs{jj}).(scans{2}).(fNames{ff})];
% Set alpha
alpha = 0.05;
% Temp ICC
tICC = f_ICC(temp,alpha);
% Assign to table
ICC.(nets{ii}).ICC.(regs{jj})(ff,1) = tICC{1, 2}.est;
ICC.(nets{ii}).ICCpVal.(regs{jj})(ff,1) = tICC{1, 2}.pVal;
ICC.(nets{ii}).ICCCI.(regs{jj}){ff,1} = tICC{1, 2}.confInterval;
% clear
clear temp tICC
end
end
% Set features names as raws
rnames = fNames';
ICC.(nets{ii}).ICC.Properties.RowNames = regexprep(rnames, 'shape_', '');
ICC.(nets{ii}).ICCpVal.Properties.RowNames = regexprep(rnames, 'shape_', '');
ICC.(nets{ii}).ICCCI.Properties.RowNames = regexprep(rnames, 'shape_', '');
end
end
%% calculateSicc
% This function is to calculate the significance of changes of the
% calculated ICC
%
% Input:
% 1. ICC: The calculated ICC results. (structure)
%
% output:
% 1. sICC: The significance of ICC changes. (structure)
%
function sICC = calculateSicc(ICC)
% --Test between Networks for All features
% Get Netwroks names
nets = fieldnames(ICC);
% Loop over Netwroks
netsN = cell(numel(nets),1);
for ii = 1:numel(nets)
% Get nets names
netsN{ii} = nets{ii};
% Make table to fill later
sICC.NetsAll = table;
% Get regions names
regs = ICC.(nets{ii}).ICC.Properties.VariableNames;
% Loop over regions
for jj = 1:numel(regs)
% Do the statistical test
for ll = 1:numel(nets)
[pvNA.(regs{jj})(ll,ii),~] =...
signrank(ICC.(nets{ll}).ICC.(regs{jj})(:),...
ICC.(nets{ii}).ICC.(regs{jj})(:));
end
end
end
% Get raw names
netsCN = cell(numel(netsN),numel(netsN));
for ii = 1:numel(netsN)
for jj = ii:numel(netsN)-1
netsCN{jj,ii} = [netsN{ii} ' Vs ' netsN{jj+1}];
end
end
netsCN = netsCN(~cellfun('isempty',netsCN));
netsCN = netsCN(:);
% Loop over regions to correct
for jj = 1:numel(regs)
% Correct
inP = nonzeros(tril(pvNA.(regs{jj}),-1));
[~, ~, ~, sICC.NetsAll.(regs{jj})] =...
fdr_bh(inP,0.05,'pdep','no');
end
% Add raw names
sICC.NetsAll.Properties.RowNames = netsCN;
% --Test between Regions
% Loop over Netwroks
for ii = 1:numel(nets)
% Make table to fill later
sICC.Regions = table;
% Loop over regions
for jj = 1:numel(regs)
% Do the statistical test
for ll = 1:numel(regs)
[pvR.(nets{ii})(ll,jj),~] =...
signrank(ICC.(nets{ii}).ICC.(regs{jj})(:),...
ICC.(nets{ii}).ICC.(regs{ll})(:));
end
end
end
% Get raw names
reCN = cell(numel(regs),numel(regs));
for ii = 1:numel(regs)
for jj = ii:numel(regs)-1
reCN{jj,ii} = [regs{ii} ' Vs ' regs{jj+1}];
end
end
reCN = reCN(~cellfun('isempty',reCN));
reCN = reCN(:);
% Loop over Netwroks
for ii = 1:numel(nets)
% Correct
inP = nonzeros(tril(pvR.(nets{ii}),-1));
[~, ~, ~, sICC.Regions.(nets{ii})] =...
fdr_bh(inP,0.05,'pdep','no');
end
% Add raw names
sICC.Regions.Properties.RowNames = reCN;
% --Test between Networks per feature
% Loop over Netwroks
for ii = 1:numel(nets)
% Make table to fill later
sICC.Nets.(nets{ii}) = table;
% Loop over regions
for jj = 1:numel(regs)
% Loop over features
fNames = ICC.(nets{ii}).ICCCI.Properties.RowNames;
for ff = 1:numel(fNames)
% Do the statistical test
sICC.Nets.(nets{ii}).(regs{jj})(ff) =...
isempty(range_intersection(ICC.(nets{1}).ICCCI.(regs{jj}){ff},...
ICC.(nets{ii}).ICCCI.(regs{jj}){ff}));
end
end
end
end
%% calculateCV
% This function is to calculate the coefficent of variation (CV)
% between the features those extacted from Scan 1 and Scan 2
%
% Input:
% 1. features: The extracted shape features. (structure)
%
% output:
% 1. CV: The calculated CV results. (structure)
%
function CV = calculateCV(features)
% Get Netwroks names
nets = fieldnames(features);
% Loop over Netwroks
for ii = 1:numel(nets)
% Get regions names
regs = fieldnames(features.(nets{ii}));
% Loop over regions
for jj = 1:numel(regs)
% Get scans names
scans = fieldnames(features.(nets{ii}).(regs{jj}));
% Get features names
fNames = features.V_Net_3D.PZ.Scan1.Properties.VariableNames;
% Loop over features to calculate ICC
for ff = 1:numel(fNames)
% Loop over cases
for ll = 1:size(features.(nets{ii}).(regs{jj}).(scans{1}).(fNames{ff}),1)
% values
v1 = features.(nets{ii}).(regs{jj}).(scans{1}).(fNames{ff})(ll);
v2 = features.(nets{ii}).(regs{jj}).(scans{2}).(fNames{ff})(ll);
CV.(nets{ii}).(regs{jj})(ll,ff) = (std([v1,v2]))/(mean([v1,v2]));
end
end
% Convert to table
CV.(nets{ii}).(regs{jj}) = array2table(CV.(nets{ii}).(regs{jj}));
% Set features names as columns
vnames = fNames';
CV.(nets{ii}).(regs{jj}).Properties.VariableNames = regexprep(vnames, 'shape_', '');
% Set cases as raws
rname = features.(nets{ii}).(regs{jj}).(scans{1}).Properties.RowNames;
rname = cellfun(@(x) x(1:8), rname, 'un', 0);
CV.(nets{ii}).(regs{jj}).Properties.RowNames = rname;
end
end
end
%% calculateScv
% This function is to calculate the significance of changes of the
% calculated CV
%
% Input:
% 1. CV: The calculated CV results. (structure)
% 2. test: the statistical test. (string)
%
% output:
% 1. sCV: The significance of CV changes. (structure)
%
function sCV = calculateScv(CV,test)
% --Test between Networks
% Get Netwroks names
nets = fieldnames(CV);
% Loop over Netwroks
netsN = cell(numel(nets),1);
for ii = 1:numel(nets)
% Get nets names
netsN{ii} = nets{ii};
% Get regions names
regs = fieldnames(CV.(nets{ii}));
% Loop over regions
for jj = 1:numel(regs)
% Make table to fill later
sCV.Nets.(regs{jj}) = table;
% Get features names
fnames = CV.(nets{ii}).(regs{jj}).Properties.VariableNames;
% Do the statistical test
for ff = 1:numel(fnames)
for ll = 1:numel(nets)
if contains(test,'signrank')
[pvN.(regs{jj}).(fnames{ff})(ll,ii),~] =...
signrank(table2array(CV.(nets{ll}).(regs{jj})(:,ff)),...
table2array(CV.(nets{ii}).(regs{jj})(:,ff)));
elseif contains(test,'ranksum')
[pvN.(regs{jj}).(fnames{ff})(ll,ii),~] =...
ranksum(table2array(CV.(nets{ll}).(regs{jj})(:,ff)),...
table2array(CV.(nets{ii}).(regs{jj})(:,ff)));
end
end
end
end
end
% Get raw names
netsCN = cell(numel(netsN),numel(netsN));
for ii = 1:numel(netsN)
for jj = ii:numel(netsN)-1
netsCN{jj,ii} = [netsN{ii} ' Vs ' netsN{jj+1}];
end
end
netsCN = netsCN(~cellfun('isempty',netsCN));
netsCN = netsCN(:);
% Loop over regions to correct
for jj = 1:numel(regs)
% Loop over features
for ff = 1:numel(fnames)
% Correct
inP = nonzeros(tril(pvN.(regs{jj}).(fnames{ff}),-1));
[~, ~, ~, sCV.Nets.(regs{jj}).(fnames{ff})] =...
fdr_bh(inP,0.05,'pdep','no');
end
% Add raw names
sCV.Nets.(regs{jj}).Properties.RowNames = netsCN;
end
% --Test between features
% Loop over Netwroks
for ii = 1:numel(nets)
% Make table to fill later
sCV.Features.(nets{ii}) = table;
% Loop over regions
for jj = 1:numel(regs)
% Do the statistical test
for ff = 1:numel(fnames)
for ll = 1:numel(fnames)
if contains(test,'signrank')
[pvF.(nets{ii}).(regs{jj})(ll,ff),~] =...
signrank(table2array(CV.(nets{ii}).(regs{jj})(:,ff)),...
table2array(CV.(nets{ii}).(regs{jj})(:,ll)));
elseif contains(test,'ranksum')
[pvF.(nets{ii}).(regs{jj})(ll,ff),~] =...
ranksum(table2array(CV.(nets{ii}).(regs{jj})(:,ff)),...
table2array(CV.(nets{ii}).(regs{jj})(:,ll)));
end
end
end
end
end
% Get raw names
feCN = cell(numel(fnames),numel(fnames));
for ii = 1:numel(fnames)
for jj = ii:numel(fnames)-1
feCN{jj,ii} = [fnames{ii} ' Vs ' fnames{jj+1}];
end
end
feCN = feCN(~cellfun('isempty',feCN));
feCN = feCN(:);
% Loop over Netwroks
for ii = 1:numel(nets)
% Loop over regions to correct
for jj = 1:numel(regs)
% Correct
for ff = 1:numel(fnames)
inP = nonzeros(tril(pvF.(nets{ii}).(regs{jj}),-1));
[~, ~, ~, sCV.Features.(nets{ii}).(regs{jj})] =...
fdr_bh(inP,0.05,'pdep','no');
end
% Add raw names
sCV.Features.(nets{ii}).Properties.RowNames = feCN;
end
end
% --Test between regions
% Loop over Netwroks
for ii = 1:numel(nets)
% Make table to fill later
sCV.Regions.(nets{ii}) = table;
% Loop over regions
for jj = 1:numel(regs)
% Do the statistical test
for ff = 1:numel(fnames)
for ll = 1:numel(regs)
if contains(test,'signrank')
[pvR.(nets{ii}).(fnames{ff})(ll,jj),~] =...
signrank(table2array(CV.(nets{ii}).(regs{jj})(:,ff)),...
table2array(CV.(nets{ii}).(regs{ll})(:,ff)));
elseif contains(test,'ranksum')
[pvR.(nets{ii}).(fnames{ff})(ll,jj),~] =...
ranksum(table2array(CV.(nets{ii}).(regs{jj})(:,ff)),...
table2array(CV.(nets{ii}).(regs{ll})(:,ff)));
end
end
end
end
end
% Get raw names
reCN = cell(numel(regs),numel(regs));
for ii = 1:numel(regs)
for jj = ii:numel(regs)-1
reCN{jj,ii} = [regs{ii} ' Vs ' regs{jj+1}];
end
end
reCN = reCN(~cellfun('isempty',reCN));
reCN = reCN(:);
% Loop over Netwroks
for ii = 1:numel(nets)
% Loop over regions to correct
for jj = 1:numel(regs)
% Correct
for ff = 1:numel(fnames)
inP = nonzeros(tril(pvR.(nets{ii}).(fnames{ff}),-1));
[~, ~, ~, sCV.Regions.(nets{ii}).(fnames{ff})] =...
fdr_bh(inP,0.05,'pdep','no');
end
% Add raw names
sCV.Regions.(nets{ii}).Properties.RowNames = reCN;
end
end
end
%% calculateSscores
% This function is to calculate the significance of changes of the
% calculated performance scores
%
% Input:
% 1. scores: The calculated performance scores for the cases. (structure)
% 2. test: the statistical test. (string)
%
% output:
% 1. sCV: The significance of the changes in the perfrormance scores. (structure)
%
function sScores = calculateSscores(scores,test)
% --Test between Scan 1 and Scan 2
% Get Netwroks names
nets = fieldnames(scores);
% Loop over Netwroks
netsN = cell(numel(nets),1);
for ii = 1:numel(nets)
% Get nets names
netsN{ii} = nets{ii};
% Get regions names
regs = fieldnames(scores.(nets{ii}));
% Loop over regions
for jj = 1:numel(regs)
% Get scan names
scans = fieldnames(scores.(nets{ii}).(regs{jj}));
% Get the metrics fields
mets = fieldnames(scores.(nets{ii}).(regs{jj}).(scans{1}));
mets = mets(~contains(mets,'names'));
% Loop over the metrics
for mm = 1:numel(mets)
% Check if it is structure
if isstruct(scores.(nets{ii}).(regs{jj}).(scans{1}).(mets{mm}))
% Get metrics from that field
mf = fieldnames(scores.(nets{ii}).(regs{jj}).(scans{1}).(mets{mm}));
for ff = 1:numel(mf)
% Make table to fill
sScores.Scans.(mets{mm}).(mf{ff}) = table;
% Do the statistical test
if contains(test,'signrank')
[pv.(mets{mm}).(mf{ff}).(regs{jj})(ii,:),~] = ...
signrank(scores.(nets{ii}).(regs{jj}).(scans{1}).(mets{mm}).(mf{ff})(:),...
scores.(nets{ii}).(regs{jj}).(scans{2}).(mets{mm}).(mf{ff})(:));
elseif contains(test,'ranksum')
[pv.(mets{mm}).(mf{ff}).(regs{jj})(ii,:),~] = ...
ranksum(scores.(nets{ii}).(regs{jj}).(scans{1}).(mets{mm}).(mf{ff})(:),...
scores.(nets{ii}).(regs{jj}).(scans{2}).(mets{mm}).(mf{ff})(:));
end
end
else
% Make table to fill
sScores.Scans.(mets{mm}) = table;
% Do the statistical test
[pv.(mets{mm}).(regs{jj})(ii,:),~] =...
signrank(scores.(nets{ii}).(regs{jj}).(scans{1}).(mets{mm})(:),...
scores.(nets{ii}).(regs{jj}).(scans{2}).(mets{mm})(:));
end
end
end
end
% Loop over regions
for jj = 1:numel(regs)
% Loop over the metrics
for mm = 1:numel(mets)
% Check if it is structure
if isstruct(scores.(nets{ii}).(regs{jj}).(scans{1}).(mets{mm}))
% Loop over metrics from that field
for ff = 1:numel(mf)
% Correct
[~, ~, ~, sScores.Scans.(mets{mm}).(mf{ff}).(regs{jj})] =...
fdr_bh(pv.(mets{mm}).(mf{ff}).(regs{jj}),0.05,'pdep','no');
% Add raw names
sScores.Scans.(mets{mm}).(mf{ff}).Properties.RowNames = netsN;
end
else
% Correct
[~, ~, ~, sScores.Scans.(mets{mm}).(regs{jj})] =...
fdr_bh(pv.(mets{mm}).(regs{jj}),0.05,'pdep','no');
% Add raw names
sScores.Scans.(mets{mm}).Properties.RowNames = netsN;
end
end
end
% --Test between netwroks
% Loop over Netwroks
for ii = 1:numel(nets)
% Loop over regions
for jj = 1:numel(regs)
% Loop over scans
for kk = 1:numel(scans)
% Loop over the metrics
for mm = 1:numel(mets)
% Check if it is structure
if isstruct(scores.(nets{ii}).(regs{jj}).(scans{1}).(mets{mm}))
% Loop over the metrics from that field
for ff = 1:numel(mf)
% Make table to fill
sScores.(['Nets' (scans{kk})]).(mets{mm}).(mf{ff}) = table;
% Do the statistical test
for ll = 1:numel(nets)
if contains(test,'signrank')
[pvN.(mets{mm}).(mf{ff}).(regs{jj}).(scans{kk})(ll,ii),~] =....
signrank(scores.(nets{ll}).(regs{jj}).(scans{kk}).(mets{mm}).(mf{ff})(:),...
scores.(nets{ii}).(regs{jj}).(scans{kk}).(mets{mm}).(mf{ff})(:));
elseif contains(test,'ranksum')
[pvN.(mets{mm}).(mf{ff}).(regs{jj}).(scans{kk})(ll,ii),~] =....
ranksum(scores.(nets{ll}).(regs{jj}).(scans{kk}).(mets{mm}).(mf{ff})(:),...
scores.(nets{ii}).(regs{jj}).(scans{kk}).(mets{mm}).(mf{ff})(:));
end
end
end
else
% Make table to fill
sScores.(['Nets' (scans{kk})]).(mets{mm}) = table;
% Do the statistical test
for ll = 1:numel(nets)
if contains(test,'signrank')
[pvN.(mets{mm}).(regs{jj}).(scans{kk})(ll,ii),~] =....
signrank(scores.(nets{ll}).(regs{jj}).(scans{kk}).(mets{mm})(:),...
scores.(nets{ii}).(regs{jj}).(scans{kk}).(mets{mm})(:));
elseif contains(test,'ranksum')
[pvN.(mets{mm}).(regs{jj}).(scans{kk})(ll,ii),~] =....
ranksum(scores.(nets{ll}).(regs{jj}).(scans{kk}).(mets{mm})(:),...
scores.(nets{ii}).(regs{jj}).(scans{kk}).(mets{mm})(:));
end
end
end
end
end
end
end
% Get raw names
netsCN = cell(numel(netsN),numel(netsN));
for ii = 1:numel(netsN)
for jj = ii:numel(netsN)-1
netsCN{jj,ii} = [netsN{ii} ' Vs ' netsN{jj+1}];
end
end
netsCN = netsCN(~cellfun('isempty',netsCN));
netsCN = netsCN(:);
% Loop over regions
for jj = 1:numel(regs)
% Loop over the metrics
for mm = 1:numel(mets)
% Check if it is structure
if isstruct(scores.(nets{ii}).(regs{jj}).(scans{1}).(mets{mm}))
% Loop over metrics from that field
for ff = 1:numel(mf)
% Loop over scans
for kk = 1:numel(scans)
% Correct
inP = nonzeros(tril(pvN.(mets{mm}).(mf{ff}).(regs{jj}).(scans{kk}),-1));
[~, ~, ~, sScores.(['Nets' (scans{kk})]).(mets{mm}).(mf{ff}).(regs{jj})] =...
fdr_bh(inP,0.05,'pdep','no');
% Add raw names
sScores.(['Nets' (scans{kk})]).(mets{mm}).(mf{ff}).Properties.RowNames = netsCN;
end
end
else
% Loop over scans
for kk = 1:numel(scans)
% Correct
inP = nonzeros(tril(pvN.(mets{mm}).(regs{jj}).(scans{kk}),-1));
[~, ~, ~,sScores.(['Nets' (scans{kk})]).(mets{mm}).(regs{jj})] =...
fdr_bh(inP,0.05,'pdep','no');
% Add raw names
sScores.(['Nets' (scans{kk})]).(mets{mm}).Properties.RowNames = netsCN;
end
end
end
end
end
%% calculateNslices
% This function is to calculate the number of the included slices in
% calculations for the cases in Scan1 and Scan2
%
% Input:
% 1. masterPath: The path to the master analysis file. (string)
% 2. esPath: The path to the automatically segmented cases post processing. (string)
% 3. eIdx: The excluded cases Idxs. (structure)
%
% output:
% 1. nSlices: The numbers of the included slices. (structure)
%
function nSlices = calculateNslices(masterPath,esPath,eIdx)
% Paths
msPath = fullfile(masterPath,'Data','Segmentation','Manual','Final');
% Loop over networks
taD = dir(esPath);
taD = taD(~ismember({taD.name},{'.','..'}));
aD(1).name = 'Manual'; % add manual
aD(1).folder = msPath; % add manual path
aD(1).date = taD(1).date;
aD(1).bytes = taD(1).bytes;
aD(1).isdir = taD(1).isdir;
aD(1).datenum = taD(1).datenum;
aD(2:numel(taD)+1) = taD(1:end);
for ii = 1:numel(aD)
% Get regions names
if strcmp(aD(ii).name,'Manual')
nD = dir(fullfile(esPath,aD(ii+1).name));
nD = nD(~ismember({nD.name},{'.','..'}));
else
nD = dir(fullfile(esPath,aD(ii).name));
nD = nD(~ismember({nD.name},{'.','..'}));
end
for jj = 1:numel(nD)
% Get scans names
rD = dir(fullfile(nD(jj).folder,nD(jj).name));
rD = rD(~ismember({rD.name},{'.','..'}));
% Loop over scans
for kk = 1:numel(rD)
% Make table to fill
% replace the Network name, replace "-" by "_"
netName = strrep(aD(ii).name,'-','_');
% Loop over cases
if strcmp(aD(ii).name,'Manual')
sD = dir(fullfile(aD(ii).folder,rD(kk).name,nD(jj).name,'*.mhd'));
else
sD = dir(fullfile(rD(kk).folder,rD(kk).name,'*.mhd'));
if ~isempty(eIdx)
sD(eIdx.(netName)) = [];
end
end
for ll = 1:numel(sD)
FilePath = fullfile(sD(ll).folder,sD(ll).name);
nSlices.(netName).(nD(jj).name).(rD(kk).name)(ll,:) =...
getNslices(FilePath);
end
end
end
end
end
%% getNslices
% This function is to calculate the number of the included slices in a mask
%
% Input:
% 1. FilePath: The path to the case. (string)
%
% output:
% 1. nSlices: The numbers of the included slices. (structure)
%
function nSlices = getNslices(FilePath)
% Read the image in matlab
[StrDatax, ~, ~] = elxMetaIOFileToStrDatax(FilePath, 0);
% Loop over all slices and find if it contains mask
sIdx = zeros(size(StrDatax.Data,3),1);
for ii = 1:size(StrDatax.Data,3)
sIdx(ii,:) = sum(sum(StrDatax.Data(:,:,ii)))>0;
end
nSlices = sum(sIdx);
end
%% calculateSslices
% This function is to calculate the significance of change between
% Scan 1 and Scan 2 volumes
%
% Input:
% 1. nSlices: The numbers of the included slices. (structure)
% 2. test: the statistical test. (string)
%
% output:
% 1. sSlices: The significance of slices numbers change. (structure)
%
function sSlices = calculateSslices(nSlices,test)
% --Test between Scan 1 and Scan 2
sSlices.Scans = table;
% Get Netwroks names
nets = fieldnames(nSlices);
% Loop over Netwroks
netsN = cell(numel(nets),1);
for ii = 1:numel(nets)
% Get nets names
netsN{ii} = nets{ii};
% Get regions names
regs = fieldnames(nSlices.(nets{ii}));
% Loop over regions
for jj = 1:numel(regs)
% Get scan names
scans = fieldnames(nSlices.(nets{ii}).(regs{jj}));
% Do the statistical test
if contains(test,'signrank')
[pv.(regs{jj})(ii,:),~] =...
signrank(nSlices.(nets{ii}).(regs{jj}).(scans{1})(:),...
nSlices.(nets{ii}).(regs{jj}).(scans{2})(:));
elseif contains(test,'ranksum')
[pv.(regs{jj})(ii,:),~] =...
ranksum(nSlices.(nets{ii}).(regs{jj}).(scans{1})(:),...
nSlices.(nets{ii}).(regs{jj}).(scans{2})(:));
end
end
end
% Loop over regions
for jj = 1:numel(regs)
% Correct
[~, ~, ~, sSlices.Scans.(regs{jj})] = fdr_bh(pv.(regs{jj}),0.05,'pdep','no');
end
% Add raw names
sSlices.Scans.Properties.RowNames = netsN;
% --Test between netwroks
% Loop over Netwroks
for ii = 1:numel(nets)
% Loop over regions
for jj = 1:numel(regs)
% Loop over scans
for kk = 1:numel(scans)
sSlices.(['Nets' (scans{kk})]) = table;
% Do the statistical test
for ll = 1:numel(nets)
if contains(test,'signrank')
[pvN.(regs{jj}).(scans{kk})(ll,ii),~] =...
signrank(nSlices.(nets{ll}).(regs{jj}).(scans{kk})(:),...
nSlices.(nets{ii}).(regs{jj}).(scans{kk})(:));
elseif contains(test,'ranksum')
[pvN.(regs{jj}).(scans{kk})(ll,ii),~] =...
ranksum(nSlices.(nets{ll}).(regs{jj}).(scans{kk})(:),...
nSlices.(nets{ii}).(regs{jj}).(scans{kk})(:));
end
end
end
end
end
% Loop over regions
for jj = 1:numel(regs)
% Loop over scans