clear all;
clc;
load('average_networks.mat');

% These parameters are based on the reference
% Diez, I. et al. A novel brain partition highlights the modular skeleton shared by structure and function.
% Sci. Rep. 5, 10532; doi: 10.1038/srep10532 (2015)

num_clusters=20;
alpha=0.45; 
beta=0.0;
struct_network=struct_network./max(struct_network(:));


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Functional dendogram -> structure follows function %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


Y = pdist(func_network,'cosine');
Z = linkage(Y,'weighted'); % here you can use weighted, average, and also ward
figure
[H,T,permAll] = dendrogram(Z,num_clusters,'colorthreshold',1000);
set(gca,'FontSize',20);legend('Functional Dendrogram');

[Xsf Qff Qsf Lsf] =crossmodularity(func_network,struct_network,alpha,beta,T)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Structural dendogram  ->  function follows structure %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

X=1-struct_network;
Y = zeros(1,size(X,1)*(size(X,1)-1)/2);
idxEnd=0;
for i=1:size(X,1)-1
    Y(idxEnd+1:idxEnd+length(X(i,i+1:end)))=X(i,i+1:end);
    idxEnd=idxEnd+length(X(i,i+1:end));
end
Z = linkage(Y,'average');
figure
[H,T,permAll] = dendrogram(Z,num_clusters,'colorthreshold',1000);
set(gca,'FontSize',20);legend('Structural Dendrogram');
[Xfs Qfs Qss Lfs] =crossmodularity(func_network,struct_network,alpha,beta,T)





