% Statistical and Computational Methods in Brain Image Analysis
%Chapter 17
%
% (C) Moo K. Chung 
% University of Wisconsin-Madison
% mkchung@wisc.edu

nii=load_nii('mean_diffeomorphic_initial6_fa.nii');
d=nii.img;
surf = isosurface(d); 
coord=surf.vertices;
temp=coord;
temp(:,2)=coord(:,1);
temp(:,1)=coord(:,2);
surf.vertices=temp;
figure_patch(surf,[0.74 0.71 0.61],0.2);


% plots tracts
load SL.mat
for i=1:30:10000 
    tract=SL{i}'; 
    if size(tract,2)> 10
        hold on
        plot3(tract(1,:),tract(2,:),tract(3,:),'b')
    end;
end;

% plots end points
endp=get_endp_tracts(SL,30);
hold on; plot3(endp(:,1,1),endp(:,1,2), endp(:,1,3),'.r')
hold on; plot3(endp(:,2,1),endp(:,2,2), endp(:,2,3),'.r')

% epsilon-neighbor network construction
[adj, prob, vertices]=tract2graph(SL,10);
figure; imagesc(adj); colormap('bone');


%-----------------------------
% Degree visualization
degree=sum(adj);
figure_graph_color(adj,vertices, degree);
view([0 90])
colormap('hot'); colorbar


%------------------------------
% The largest cluster size computation

[adj, prob, vertices]=tract2graph(SL,6);
adj = adj + eye(size(adj,1));
[p,q,r,s] = dmperm(adj);

figure;figure_patch(surf,[0.74 0.71 0.61],0.2);hold on;

ck=length(r);
for i=1:(ck-1)
    cp=p(r(i):r(i+1)-1);
    clr=10*i*ones(length(cp),1);
    figure_graph_color(adj(cp,cp),vertices(cp,:),clr);
    hold on;
end;
% node belonging to the same cluster is colored identically.   