-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSNCs.m
More file actions
1616 lines (1244 loc) · 57.6 KB
/
SNCs.m
File metadata and controls
1616 lines (1244 loc) · 57.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
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
function []=SNCs(K,Louv,V,Q,Tmax,S,N,SNC_size,thr,evenodd)
% Marika Strindberg, Karolinska Institutet 2018-2020
% marika.strindberg@ki.se, marikastrindberg@gmail.com
% Subrutine to TimeResolvedNets that calculates SNCs of size n = 4, n = 6, n = 8, n = 10,
% n = 12 areas as specified by SNC_size (alternatively size n = 5, n = 7, n = 9, n = 11 if single seeds are specified)
% At each iteration the mean accross participants are calculated and the
% top K constellations for each seed is used as seed in the next
% iteration.
% The runtime is sensitive to number of subjects, number of areas in the parcellation, number of seed pairs/areas but much less to total number of time-points.
% Q = contains the initial seed pairs (or seed if only on area is chosen as seed)
% K = the expansion factor ie how many top pairs in terms of qint is kept
% for each seed(SNC) in each iteration. This number is allowed to be as
% great as K+add to accommodate multiple pairs with same qint
% V = the qint-matrix for all pairs
% Louv = contains the community assignment of all areas at all timepoints. The
% numbers range from 1-3 and does not have any meaning in them selves beyond
%markers of different groups.
% Tmax = the number of timepoints
% thr = by default 0, if set higher it only searches trough SNCs with qint
% above this thr
% S = number of subjects
% N = number of areas in parcellation
% SNC_size = the size of the final SNC ie number of areas they contain,
% default = 8. If one area is used instead of a pair it will be an odd
% number.
% Max number of components that have the same qint as the K:th highest
add=40;
%Divide computations into smaller units
Div=100;
%a=0;
if length(Q)>Div
X=floor(length(Q)/Div)
B=[Div:Div:X*Div];
B=[0 B];
A=ones(1,X)*Div; % If A is even multiplication of div
if (length(Q)-X*Div)>0 % If A is not an even multiplication of div
A=[A length(Q)-X*Div];
else
end
Y=numel(A)
else
X=floor(length(Q)/Div)
B=[Div:Div:X*Div];
B=[0 B];
Div=length(Q)
A=length(Q)
Y=numel(A)
end
%Identify pairs with a qint above threshold a
[row col]=find(V>thr);
R=[row col];
R=sort(R,2);
F=unique(R,'rows');
if evenodd==0
for p=1:Y
meanNet_2_4=zeros(A(p),S,N,N);
parfor h=1:A(p)
mN_2_4=zeros(S,N,N);
for s=1:S
temp2=squeeze(Louv(s,:,:));% Time and community
for m=1:length(F) % only pairs above thr a is seached through
if ~ismember(Q(B(p)+h,:),F(m,:))
InCom=0;
assembly=[Q(B(p)+h,:) F(m,:)];
y=temp2(:,assembly);
z=prod(y,2); %Row with same community nr have product 1^4=1,2^4=16,3^4=81
zz=(z==[1 16 81]);
zzz=(zz(:,1)+zz(:,2)+zz(:,3));
InCom=numel(nonzeros(zzz));
mN_2_4(s,F(m,1),F(m,2))=InCom/Tmax;
end
end
meanNet_2_4(h,s,:,:)=mN_2_4(s,:,:);
end
end
MeanALLNetw2_4=zeros(A(p),N,N);
if S>1
for h=1:A(p) %calculate mean for each matrix in each row
G=squeeze(meanNet_2_4(h,:,:,:));
MeanALLNetw2_4(h,:,:)=mean(G);
end
else
G=squeeze(meanNet_2_4(:,1,:,:));
MeanALLNetw2_4(:,:,:)=G;
end
save(sprintf('P%d_MeanAllSNC4_%d',K,p),'MeanALLNetw2_4','-v7.3')
end
else
for p=1:Y
meanNet_2_4=zeros(A(p),S,N,N);
parfor h=1:A(p)
mN_2_4=zeros(S,N,N);
for s=1:S
temp2=squeeze(Louv(s,:,:));% Time and community
for m=1:length(F) % only pairs above threshold a is seached through
if ~ismember(Q(B(p)+h,:),F(m,:))
InCom=0;
assembly=[Q(B(p)+h,:) F(m,:)];
y=temp2(:,assembly);
z=prod(y,2); %Row with same community nr have product 1^3=1,2^^=8,3^3=27
zz=(z==[1 8 27]);
zzz=(zz(:,1)+zz(:,2)+zz(:,3));
InCom=numel(nonzeros(zzz));
mN_2_4(s,F(m,1),F(m,2))=InCom/Tmax;
end
end
meanNet_2_4(h,s,:,:)=mN_2_4(s,:,:);
end
end
MeanALLNetw2_4=zeros(A(p),N,N);
if S>1
for h=1:A(p) %calculate mean for each matrix in each row
G=squeeze(meanNet_2_4(h,:,:,:));
MeanALLNetw2_4(h,:,:)=mean(G);
end
else
G=squeeze(meanNet_2_4(:,1,:,:));
MeanALLNetw2_4(:,:,:)=G;
end
save(sprintf('P%d_MeanAllSNC4_%d',K,p),'MeanALLNetw2_4','-v7.3')
end
end
% For each pair calculate the the top 10 most likely 4 node components
DnumelAll=[];
for p=1:Y
clear MeanAllNetw2_4
load(sprintf('P%d_MeanAllSNC4_%d',K,p)) % Load search space matrix
R4_IDXt=zeros(A(p),K+add,4);
R4_length_t=zeros(1,A(p));
Freq_Y=zeros(A(p),K+add); %Save all qints for division Y
for h=1:A(p) %For each 2 area seed
R4_t=zeros(K+add,4); % Stores the 4 area seeds
D=squeeze(MeanALLNetw2_4(h,:,:));
Dnumel(h)=numel(nonzeros(D));
if Dnumel(h)>0 && Dnumel(h)>=K %if more than K pairs
a=maxk(nonzeros(D),K);
[row col]=find(D>=a(K));%pairs above or equal to 10th highest value
R4_t(1:numel(row),:)=[Q(B(p)+h,:).*ones(numel(row),2) row col]; %Save the top 4 area seeds
Freq_Y(h,1:numel(row))=D(D>=a(K)); % The qint of the pairs
elseif Dnumel(h)>0
[row col]=find(D>0);
R4_t(1:numel(row),:)=[Q(B(p)+h,:).*ones(numel(row),2) row col];%Save the top 4 area seeds
Freq_Y(h,1:numel(row))=D(D>0);
else
R4_t(1,:)=[Q(B(p)+h,:) 0 0];
Freq_Y(h,1)=0;
end
R4_IDXt(h,:,:)=R4_t(:,:);
end
save(sprintf('P%d_R4prob%d',K,p),'Dnumel','Freq_Y', 'R4_IDXt','-v7.3')
DnumelAll=[DnumelAll Dnumel];
end
save SizeS2 DnumelAll
%dubblecheck that the SNCs have unique areas
for p=1:Y
load(sprintf('P%d_R4prob%d',K,p))
h=1;
Unika=[];
for i=1:length(nonzeros(R4_IDXt(:,1,1)))
temp= squeeze(R4_IDXt(i,:,:));
for j=1:length(nonzeros(temp(:,1)))
Unika(h)=numel(unique(temp(j,:)));
h=h+1;
end
end
MinR4IDX(p)=min(Unika)
end
save(sprintf('UniqueR4IDXt_%d',p), 'MinR4IDX')
%Add to one matrix
R4_IDX=[];R4_length=[];Freq=[];
for p=1:Y
load(sprintf('P%d_R4prob%d',K,p))
R4_IDX=[R4_IDX; R4_IDXt];
Freq=[Freq; Freq_Y]; %Probability of the SNC components
save(sprintf('P%d_R4idx',K), 'R4_IDX', 'Freq')
clear R R4_IDXt Freq_Y R4_length_t
delete(sprintf('P%d_R4prob%d.mat',K,p))
end
%Sort from lowest to highest area nr in each row in order to later remove
%dubplicates
for h=1:length(R4_IDX(:,1,1))
for k=1:length(R4_IDX(h,:,1))
R4_IDXs(h,k,:)=sort(R4_IDX(h,k,:));
end
end
R4_IDXsALL=[];
R4_IDXsALL_ORD=[];
R4_Freq_ALL=[];
for h=1:length(nonzeros(R4_IDXs(:,1,1)))
T=squeeze(R4_IDXs(h,:,:));
[H_i I]=unique(T,'rows','stable'); %zero row at the end in most cases
Nu=ones(numel(I),1).*h; % saves the origin, ie which row the seed is derived from in R4_IDXsU
R4_Freq_ALL=[R4_Freq_ALL; Freq(h,I)']; % get the accompanying qint for each of the SNCs
R4_IDXsALL=[R4_IDXsALL;H_i];
R4_IDXsALL_ORD=[R4_IDXsALL_ORD; Nu ];
end
%delete rows with zeros
[ro col]=find(R4_IDXsALL==0);
zeRO=unique(ro); numel(zeRO)
R4_IDXsALL(zeRO,:)=[];
R4_IDXsALL_ORD(zeRO,:)=[];
R4_Freq_ALL(zeRO)=[];
%Get unique rows
[R4_IDXsU I4]=unique(R4_IDXsALL,'rows','stable'); % keep the order, dont sort
FreqIDXsU4=R4_Freq_ALL(I4);%%%%
R4_IDXsUfreq=[FreqIDXsU4(:,1) R4_IDXsU];
R4_IDXsUfreqSort=sortrows(R4_IDXsUfreq,1,'descend');
save(sprintf('P%d_I4',K),'R4_IDXsUfreq','R4_IDXsUfreqSort', 'R4_Freq_ALL','FreqIDXsU4', 'R4_IDXsU', 'I4', 'R4_IDXsALL', 'R4_IDXsALL_ORD', 'R4_IDX', 'R4_IDXs', '-v7.3') % 'ProbDR4_ALL',
% Dubbelcheck no rows with duplicate area
duplicate=0;
U=0;
for hh=1:length(R4_IDXsU)
U(hh)=numel(unique(R4_IDXsU(hh,:)));
if evenodd==0
if U(hh)<4
duplicate=1
U(hh)
save(sprintf('P%d_4D',K), 'U')
break
end
else
if U(hh)<3
duplicate=1
save(sprintf('P%d_4D',K), 'U')
break
end
end
end
fileID = fopen('Step1.txt','w');
fprintf(fileID,'Number of rows in step 2 is %d\n',size(R4_IDXsU,1));
fprintf(fileID,'Duplicate %d \n', duplicate);
fprintf(fileID,'Number of divisions %d', Y);
fclose(fileID);
Dnumel=[];
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %Step2 ie from SNC=4 to SNC=6
if SNC_size>4
Div=100
X=floor(numel(I4)/Div); % Division not to make matrix too big
B=[Div:Div:X*Div];
B=[0 B];
A=ones(1,X)*Div;
if (length(I4)-X*Div)>0 % If A is not an even multiplication of div
A=[A numel(I4)-X*Div]; % Nr of rows in each division
else
end
Y=numel(A) %Nr of divisions
if evenodd==0
for p=1:Y
clear TempMeanALL E MeanALLNetw2_4_6
meanNet_2_4_6=zeros(A(p),S,N,N);
if p< Y
R4ORDtemp=R4_IDXsALL_ORD(I4(B(p)+1:B(p+1))); % length decided by A(p)
else
R4ORDtemp=R4_IDXsALL_ORD(I4(B(p)+1:B(p)+A(p)));
end
TempMeanALL=zeros(length(R4ORDtemp),N,N);
for i=1:length(R4ORDtemp) % length decided by A(p) ie max=div
MeanALLNetw2_4=[];
F=R4ORDtemp(i);
b1=floor(F/Div);
b2=mod(F,Div);
if b2==0
load(sprintf('P%d_MeanAllSNC4_%d',K,b1));
TempMeanALL(i,:,:)=MeanALLNetw2_4(Div,:,:);
else
load(sprintf('P%d_MeanAllSNC4_%d',K,b1+1));
TempMeanALL(i,:,:)=MeanALLNetw2_4(b2,:,:);
end
%Identify pairs with a qint above threshold a to search through
temp=squeeze(TempMeanALL(i,:,:));
[row col]=find(temp>thr);
Rx=[row col];
Rx=sort(Rx,2);
Rx=unique(Rx,'rows');
E{i}=Rx; %E has max length=div
end
parfor h=1:A(p)
mN_2_4_6=zeros(S,N,N);
F=cell2mat(E(h));
for s=1:S
temp2=squeeze(Louv(s,:,:));% Time and community
for m=1:length(F(:,1)) %number of pairs to search through
if ~ismember(R4_IDXsU(h+B(p),:),F(m,:))
InCom=0;
assembly=[R4_IDXsU(h+B(p),:) F(m,:)];
y=temp2(:,assembly);
z=prod(y,2); %Row with same community nr have product 1^6=1,2^6=64,3^729
zz=(z==[1 64 729]);
zzz=(zz(:,1)+zz(:,2)+zz(:,3));
InCom=numel(nonzeros(zzz));
mN_2_4_6(s,F(m,1),F(m,2))=InCom/Tmax;
end
end
meanNet_2_4_6(h,s,:,:)=mN_2_4_6(s,:,:);
end
end
MeanALLNetw2_4_6=zeros(A(p),N,N);
if S>1
for h=1:A(p) %calculate mean for each matrix in each row
G=squeeze(meanNet_2_4_6(h,:,:,:));
MeanALLNetw2_4_6(h,:,:)=mean(G);
end
else
G=squeeze(meanNet_2_4_6(:,1,:,:));
MeanALLNetw2_4_6(:,:,:)=G;
end
save(sprintf('P%d_MeanAllSNC6_%d',K,p),'MeanALLNetw2_4_6','-v7.3')
end
else
for p=1:Y
clear TempMeanALL E MeanALLNetw2_4_6
meanNet_2_4_6=zeros(A(p),S,N,N);
if p< Y
R4ORDtemp=R4_IDXsALL_ORD(I4(B(p)+1:B(p+1))); % length decided by A(p)
else
R4ORDtemp=R4_IDXsALL_ORD(I4(B(p)+1:B(p)+A(p)));
end
TempMeanALL=zeros(length(R4ORDtemp),N,N);
for i=1:length(R4ORDtemp) % length decided by A(p) ie max=div
MeanALLNetw2_4=[];
F=R4ORDtemp(i);
b1=floor(F/Div);
b2=mod(F,Div);
if b2==0
load(sprintf('P%d_MeanAllSNC4_%d',K,b1));
TempMeanALL(i,:,:)=MeanALLNetw2_4(Div,:,:);
else
load(sprintf('P%d_MeanAllSNC4_%d',K,b1+1));
TempMeanALL(i,:,:)=MeanALLNetw2_4(b2,:,:);
end
%Identify pairs with a qint above threshold a to search through
temp=squeeze(TempMeanALL(i,:,:));
[row col]=find(temp>thr);
Rx=[row col];
Rx=sort(Rx,2);
Rx=unique(Rx,'rows');
E{i}=Rx; %E has max length=div
end
parfor h=1:A(p)
mN_2_4_6=zeros(S,N,N);
F=cell2mat(E(h));
for s=1:S
temp2=squeeze(Louv(s,:,:));% Time and community
for m=1:length(F(:,1)) %number of pairs to search through
if ~ismember(R4_IDXsU(h+B(p),:),F(m,:))
InCom=0;
assembly=[R4_IDXsU(h+B(p),:) F(m,:)];
y=temp2(:,assembly);
z=prod(y,2); %Row with same community nr have product 1^5=1,2^5=32,3^5=243
zz=(z==[1 32 243]);
zzz=(zz(:,1)+zz(:,2)+zz(:,3));
InCom=numel(nonzeros(zzz));
mN_2_4_6(s,F(m,1),F(m,2))=InCom/Tmax;
end
end
meanNet_2_4_6(h,s,:,:)=mN_2_4_6(s,:,:);
end
end
MeanALLNetw2_4_6=zeros(A(p),N,N);
if S>1
for h=1:A(p) %calculate mean for each matrix in each row
G=squeeze(meanNet_2_4_6(h,:,:,:));
MeanALLNetw2_4_6(h,:,:)=mean(G);
end
else
G=squeeze(meanNet_2_4_6(:,1,:,:));
MeanALLNetw2_4_6(:,:,:)=G;
end
save(sprintf('P%d_MeanAllSNC6_%d',K,p),'MeanALLNetw2_4_6','-v7.3')
end
end
DnumelAll=[];
for p=1:Y
load(sprintf('P%d_MeanAllSNC6_%d',K,p)) % Load search space matrix
R6_IDXt=zeros(A(p),K+add,6);
R6_freqt=zeros(A(p),K+add);
R6_length_t=zeros(1,A(p));
Freq_Y=zeros(A(p),K+add);
for h=1:A(p) %For each 4 area seed
R6_t=zeros(K+add,6); %Stores the 6 area seed
D=squeeze(MeanALLNetw2_4_6(h,:,:));
Dnumel(h)=numel(nonzeros(D));
if Dnumel(h)>0 && Dnumel(h)>=K %if more than 10 pairs
a=maxk(nonzeros(D),K); %The pairs above or equal to 10th highest value
[row col]=find(D>=a(K));
R6_t(1:numel(row),:)=[R4_IDXsU(B(p)+h,:).*ones(numel(row),4) row col];
Freq_Y(h,1:numel(row))=D(D>=a(K)); % The qint of the pairs
elseif Dnumel(h)>0
[row col]=find(D>0);
R6_t(1:numel(row),:)=[R4_IDXsU(B(p)+h,:).*ones(numel(row),4) row col];
Freq_Y(h,1:numel(row))=D(D>0);
else
R6_t(1,:)=[R4_IDXsU(B(p)+h,:) 0 0];
Freq_Y(h,1)=0;
end
R6_IDXt(h,:,:)=R6_t(:,:);
end
save(sprintf('P%d_R6prob%d',K,p),'Dnumel','Freq_Y', 'R6_IDXt','-v7.3')
DnumelAll=[DnumelAll Dnumel];
end
save SizeS2 DnumelAll
%Add to one matrix
R6_IDX=[];R6_Freq=[];
for p=1:Y
load(sprintf('P%d_R6prob%d',K,p))
R6_IDX=[R6_IDX; R6_IDXt];
R6_Freq=[R6_Freq; Freq_Y]; %Probability of the SNC component
save(sprintf('P%d_R6idx',K), 'R6_IDX', 'R6_Freq')
clear R R6_IDXt ProbDR6t ProbIn6t
delete(sprintf('P%d_R6prob%d.mat',K,p))
end
%Sort each SNC from lowest to highest area nr in order to later
%delete dublicate rows that have converged in this step
R6_IDXs=[];
R6_IDXsALL=[];
R6_IDXsALL_ORD=[];
R6_Freq_ALL=[];
q=mod(length(R6_IDX),Div)
qa=floor(length(R6_IDX)/Div) %Rows in each division
if qa>1000
Av=0;
for i=1:Div
Av(i+1,1)=qa*i;
end
Le=ones(Div,1)*qa;
if q>0
Av(Div+1)=q;
Le=[Le; q];
Di=Div+1; %Number of divisions
else
Di=Div;
end
for g=1:Di
temp=[];
R6_IDXsALLtemp=[];
R6_IDXsALL_ORDtemp=[];
R6_Freq_ALLtemp=[];
for h=1:Le(g)
for k=1:length(R6_IDX(h+Av(g),:,1))
temp(h,k,:)=sort(R6_IDX(h+Av(g),k,:));
end
H_i=[]; I=[];Nu=[];
T=squeeze(temp(h,:,:));
[H_i I]=unique(T,'rows','stable');
Nu=ones(numel(I),1).*h;
R6_Freq_ALLtemp=[R6_Freq_ALLtemp; R6_Freq(h+Av(g),I)'];
R6_IDXsALLtemp=[R6_IDXsALLtemp;H_i];
R6_IDXsALL_ORDtemp=[R6_IDXsALL_ORDtemp;Nu];
end
R6_IDXs=[R6_IDXs; temp];
R6_IDXsALL=[R6_IDXsALL;R6_IDXsALLtemp];
R6_IDXsALL_ORD=[R6_IDXsALL_ORD;R6_IDXsALL_ORDtemp]
R6_Freq_ALL=[R6_Freq_ALL;R6_Freq_ALLtemp];
end
else
for h=1:length(R6_IDX)
for k=1:length((R6_IDX(h,:,1)))
R6_IDXs(h,k,:)=sort(R6_IDX(h,k,:));
end
H_i=[]; I=[];Nu=[];
T=squeeze(R6_IDXs(h,:,:));
[H_i I]=unique(T,'rows','stable');
Nu=ones(numel(I),1).*h;
R6_Freq_ALL=[R6_Freq_ALL; R6_Freq(h,I)'];
R6_IDXsALL=[R6_IDXsALL;H_i];
R6_IDXsALL_ORD=[R6_IDXsALL_ORD;Nu];
end
end
% %delete rows with zeros
[ro col]=find(R6_IDXsALL==0);
zeRO=unique(ro); numel(zeRO)
R6_IDXsALL(zeRO,:)=[];
R6_IDXsALL_ORD(zeRO)=[];
R6_Freq_ALL(zeRO)=[];
[R6_IDXsU I6]=unique(R6_IDXsALL,'rows');
FreqIDXsU6=R6_Freq_ALL(I6);
R6_IDXsUfreq=[FreqIDXsU6 R6_IDXsU];
R6_IDXsUfreqSort=sortrows(R6_IDXsUfreq,1,'descend');
save(sprintf('P%d_I6',K), 'R6_IDXsUfreqSort', 'R6_IDXsU', 'FreqIDXsU6', 'R6_IDXs', 'I6', 'R6_IDXsALL','R6_IDXsALL_ORD', 'R6_IDXsUfreq','R6_Freq_ALL','-v7.3')
%Dubbelcheck no dublicates
duplicate=0;
for hh=1:length(R6_IDXsU)
U(hh)=numel(unique(R6_IDXsU(hh,:)));
if evenodd==0
if U(hh)<6
duplicate=1
save(sprintf('P%d_6',K), 'U')
break
end
else
if U(hh)<5
duplicate=1
save(sprintf('P%d_6',K), 'U')
break
end
end
end
fileID = fopen('Step2.txt','w');
fprintf(fileID,'Number of rows in step 3 is %d\n',size(R6_IDXsU,1));
fprintf(fileID,'area dubplicate in component %d \n', duplicate);
fprintf(fileID,'min search space %d ',min(DnumelAll));
fprintf(fileID,'Number of divisions %d', Y);
fclose(fileID);
if SNC_size>6
% From SNC=6 to SNC=8 (or from 5 to 7)
R6_IDXsU=[];
load(sprintf('P%d_I6',K))
X=floor(numel(I6)/Div)
B=[Div:Div:X*Div];
B=[0 B];
A=ones(1,X)*Div;
if (length(I6)-X*Div)>0 % If A is not an even multiplication of div
A=[A numel(I6)-X*Div];
else
end
Y=numel(A)
if evenodd==0
for p=1:Y
clear TempMeanALL E MeanALLNetw2_4_6_8
meanNet2_4_6_8=zeros(A(p),S,N,N);
if p< Y
R6ORDtemp=R6_IDXsALL_ORD(I6(B(p)+1:B(p+1)));
else
R6ORDtemp=R6_IDXsALL_ORD(I6(B(p)+1:B(p)+A(p)));
end
TempMeanALL=zeros(length(R6ORDtemp),N,N);
for i=1:length(R6ORDtemp)
MeanALLNetw2_4_6=[];
F=R6ORDtemp(i);
b1=floor(F/Div);
b2=mod(F,Div);
if b2==0
load(sprintf('P%d_MeanAllSNC6_%d',K,b1))
TempMeanALL(i,:,:)=MeanALLNetw2_4_6(Div,:,:);
else
load(sprintf('P%d_MeanAllSNC6_%d',K,b1+1))
TempMeanALL(i,:,:)=MeanALLNetw2_4_6(b2,:,:);
end
%Identify pairs with a qint above threshold a to search through
temp=squeeze(TempMeanALL(i,:,:));
[row col]=find(temp>thr);
Rx=[row col];
Rx=sort(Rx,2);
Rx=unique(Rx,'rows');
E{i}=Rx; %E has max length=div
end
parfor h=1:A(p)
mN_2_4_6_8=zeros(S,N,N);
F=cell2mat(E(h));
for s=1:S
temp2=squeeze(Louv(s,:,:));% Time and community
for m=1:length(F(:,1))
if ~ismember(R6_IDXsU(h+B(p),:),F(m,:)) % if area is not already part of the seed
InCom=0;
assembly=[R6_IDXsU(h+B(p),:) F(m,:)];
y=temp2(:,assembly);
z=prod(y,2); %Row with same community nr have product 1^8=1,2^8=256,3^8=6561
zz=(z==[1 256 6561]);
zzz=(zz(:,1)+zz(:,2)+zz(:,3));
InCom=numel(nonzeros(zzz));
mN_2_4_6_8(s,F(m,1),F(m,2))=InCom/Tmax;
end
end
meanNet2_4_6_8(h,s,:,:)=mN_2_4_6_8(s,:,:);
end
end
MeanALLNetw2_4_6_8=zeros(A(p),N,N);
if S>1
for h=1:A(p)
G=squeeze(meanNet2_4_6_8(h,:,:,:));
MeanALLNetw2_4_6_8(h,:,:)=mean(G);
end
else
G=squeeze(meanNet2_4_6_8(:,1,:,:));
MeanALLNetw2_4_6_8(:,:,:)=G;
end
save(sprintf('P%d_MeanAllSNC8_%d',K,p),'MeanALLNetw2_4_6_8','-v7.3')
end
else
for p=1:Y
clear TempMeanALL E MeanALLNetw2_4_6_8
meanNet2_4_6_8=zeros(A(p),S,N,N);
if p< Y
R6ORDtemp=R6_IDXsALL_ORD(I6(B(p)+1:B(p+1)));
else
R6ORDtemp=R6_IDXsALL_ORD(I6(B(p)+1:B(p)+A(p)));
end
TempMeanALL=zeros(length(R6ORDtemp),N,N);
for i=1:length(R6ORDtemp)
MeanALLNetw2_4_6=[];
F=R6ORDtemp(i);
b1=floor(F/Div);
b2=mod(F,Div);
if b2==0
load(sprintf('P%d_MeanAllSNC6_%d',K,b1))
TempMeanALL(i,:,:)=MeanALLNetw2_4_6(Div,:,:);
else
load(sprintf('P%d_MeanAllSNC6_%d',K,b1+1))
TempMeanALL(i,:,:)=MeanALLNetw2_4_6(b2,:,:);
end
%Identify pairs with a qint above threshold a to search through
temp=squeeze(TempMeanALL(i,:,:));
[row col]=find(temp>thr);
Rx=[row col];
Rx=sort(Rx,2);
Rx=unique(Rx,'rows');
E{i}=Rx; %E has max length=div
end
parfor h=1:A(p)
mN_2_4_6_8=zeros(S,N,N);
F=cell2mat(E(h));
for s=1:S
temp2=squeeze(Louv(s,:,:));% Time and community
for m=1:length(F(:,1))
if ~ismember(R6_IDXsU(h+B(p),:),F(m,:)) % if area is not already part of the seed
InCom=0;
assembly=[R6_IDXsU(h+B(p),:) F(m,:)];
y=temp2(:,assembly);
z=prod(y,2); %Row with same community nr have product 1^7=1,2^7=128,3^8=2187
zz=(z==[1 128 2187]);
zzz=(zz(:,1)+zz(:,2)+zz(:,3));
InCom=numel(nonzeros(zzz));
mN_2_4_6_8(s,F(m,1),F(m,2))=InCom/Tmax;
end
end
meanNet2_4_6_8(h,s,:,:)=mN_2_4_6_8(s,:,:);
end
end
MeanALLNetw2_4_6_8=zeros(A(p),N,N);
if S>1
for h=1:A(p)
G=squeeze(meanNet2_4_6_8(h,:,:,:));
MeanALLNetw2_4_6_8(h,:,:)=mean(G);
end
else
G=squeeze(meanNet2_4_6_8(:,1,:,:));
MeanALLNetw2_4_6_8(:,:,:)=G;
end
save(sprintf('P%d_MeanAllSNC8_%d',K,p),'MeanALLNetw2_4_6_8','-v7.3')
end
end
DnumelAll=[];
for p=1:Y
load(sprintf('P%d_MeanAllSNC8_%d',K,p))
R8_IDXt=zeros(A(p),K+add,8);
Freq_Y=zeros(A(p),K+add);
for h=1:A(p)
R8_t=zeros(K+add,8);
D=squeeze(MeanALLNetw2_4_6_8(h,:,:));
Dnumel(h)=numel(nonzeros(D));
if Dnumel(h)>0 && Dnumel(h)>=K
a=maxk(nonzeros(D),K);
[row col]=find(D>=a(K));
R8_t(1:numel(row),:)=[R6_IDXsU(B(p)+h,:).*ones(numel(row),6) row col];
Freq_Y(h,1:numel(row))=D(D>=a(K));
elseif Dnumel(h)>0
[row col]=find(D>0);
R8_t(1:numel(row),:)=[R6_IDXsU(B(p)+h,:).*ones(numel(row),6) row col]; % The SNCs
Freq_Y(h,1:numel(row))=D(D>0); % Stores qint of the SNCs
else
R8_t(1,:)=[R6_IDXsU(B(p)+h,:) 0 0];
Freq_Y(h,1)=0;
end
R8_IDXt(h,:,:)=R8_t(:,:);
end
save(sprintf('P%d_R8prob%d',K,p),'Freq_Y', 'R8_IDXt','-v7.3')
DnumelAll=[DnumelAll Dnumel];
end
save SizeS3 DnumelAll
%Add to one matrix
R8_IDX=[];Freq=[];
for p=1:Y
load(sprintf('P%d_R8prob%d',K,p))
R8_IDX=[R8_IDX; R8_IDXt];
Freq=[Freq; Freq_Y];
save(sprintf('P%d_R8idx',K), 'R8_IDX', 'Freq')
clear R R8_IDXt Freq_Y
delete(sprintf('P%d_R8prob%d.mat',K,p))
end
%Sort each SNC from lowest to highest area nr in order to later
%delete dublicate rows that have converged in this step
R8_IDXs=[];
R8_IDXsALL=[];
R8_IDXsALL_ORD=[];
R8_Freq_ALL=[];
q=mod(length(R8_IDX),Div)
qa=floor(length(R8_IDX)/Div) %Rows in each division
if qa>1000
Av=0;
for i=1:Div
Av(i+1,1)=qa*i;
end
Le=ones(Div,1)*qa;
if q>0
Av(Div+1)=q;
Le=[Le; q];
Di=Div+1; %Number of divisions
else
Di=Div;
end
for g=1:Di
g
temp=[];
R8_IDXsALLtemp=[];
R8_IDXsALL_ORDtemp=[];
Freq_ALLtemp=[];
for h=1:Le(g)
for k=1:length(R8_IDX(h+Av(g),:,1))
temp(h,k,:)=sort(R8_IDX(h+Av(g),k,:));
end
H_i=[]; I=[];Nu=[];
T=squeeze(temp(h,:,:));
[H_i I]=unique(T,'rows','stable');
Nu=ones(numel(I),1).*h;
Freq_ALLtemp=[Freq_ALLtemp; Freq(h+Av(g),I)'];
R8_IDXsALLtemp=[R8_IDXsALLtemp;H_i];
R8_IDXsALL_ORDtemp=[R8_IDXsALL_ORDtemp;Nu];
end
R8_IDXs=[R8_IDXs; temp];
R8_IDXsALL=[R8_IDXsALL;R8_IDXsALLtemp];
R8_IDXsALL_ORD=[R8_IDXsALL_ORD;R8_IDXsALL_ORDtemp];
R8_Freq_ALL=[R8_Freq_ALL;Freq_ALLtemp];
end
else
for h=1:length(R8_IDX)
for k=1:length((R8_IDX(h,:,1)))
R8_IDXs(h,k,:)=sort(R8_IDX(h,k,:));
end
H_i=[]; I=[];Nu=[];
T=squeeze(R8_IDXs(h,:,:));
[H_i I]=unique(T,'rows','stable');
Nu=ones(numel(I),1).*h;
R8_Freq_ALL=[R8_Freq_ALL; Freq(h,I)'];
R8_IDXsALL=[R8_IDXsALL;H_i];
R8_IDXsALL_ORD=[R8_IDXsALL_ORD;Nu];
end
end
%delete rows with zeros
[ro col]=find(R8_IDXsALL==0);
zeRO=unique(ro); numel(zeRO)
R8_IDXsALL(zeRO,:)=[];
R8_IDXsALL_ORD(zeRO)=[];
R8_Freq_ALL(zeRO)=[];
[R8_IDXsU I8]=unique(R8_IDXsALL,'rows');
FreqIDXsU8=R8_Freq_ALL(I8);
R8_IDXsUfreq=[FreqIDXsU8 R8_IDXsU];
R8_IDXsUfreqSort=sortrows(R8_IDXsUfreq,1,'descend');
save(sprintf('P%d_I8',K),'R8_IDXsALL_ORD', 'R8_IDXsUfreqSort', 'R8_IDXsU', 'FreqIDXsU8', 'R8_IDXs', 'I8', 'R8_IDXsALL', 'R8_IDXsUfreq','-v7.3')
%Dubbelcheck there are no area duplicates
duplicate=0;
for hh=1:length(R8_IDXsU)
U(hh)=numel(unique(R8_IDXsU(hh,:)));
if evenodd==0
if U(hh)<8
hh
duplicate=1
save(sprintf('P%d_8U',K),'U')
break
end
else
if U(hh)<7
hh
duplicate=1
save(sprintf('P%d_8U',K),'U')
break
end
end