% lecture 09
% Moo K. Chung mkchung@wisc.edu

% Spherical harmonic modeling for cortical surfaces
%http://www.stat.wisc.edu/~mchung/softwares/weighted-SPHARM/weighted-SPHARM.html

%FreeSurfer format. For MNI format (autism cortical data) see webpage.
dir='lh.pial'
[vertices, faces] = read_surf(dir);

size(faces)

faces(1:10,:)

surf.vertices=vertices;
surf.faces=faces+1;
figure_wire(surf,'white');

% corresponding sphere
dir='lh.sphere'
[vertices2, faces2] = read_surf(dir);
sphere.vertices=vertices2;
sphere.faces=faces2+1;
figure_wire(sphere,'white');

% spherical coordinates on cortex
[theta varphi]=EULERangles(sphere);
figure_trimesh(surf,theta) 
figure_trimesh(surf,varphi) 

% Visualization of spherical harmonic basis
Y900=Y_lm(90,0, theta',varphi');
figure_trimesh(sphere,Y900); 

% spherical harmonic representation

[surf_smooth, fourier]=SPHARMsmooth2(surf,sphere,2,0);
figure_wire(surf_smooth,'white');

% this takes a while
[surf_smooth, fourier]=SPHARMsmooth2(surf,sphere,30,0);
figure_wire(surf_smooth,'white');

fourier
imagesc(fourier.x); colorbar;

%extract only the lower trinagle elements in fourier


% Let's see if you have a supercomputer ;)

%HW19. Discriminate/classify cortical surfaces (autism vs.control) 
%      by using the spherical harmonic coefficients up to degree 40. 
%

41^2 --> 1681 unknown coefficients x 3 cooridinates 
--> 5043 parameters for a cortical surface x 27 subjects 
--> 136161

%      Hint: It is not the best approach but you can use the discrimination 
%            technque used in Chung et al., 2010 Statistics and Its Inferface. 
%            Support vector machine (SVM) technique given in Li Shen et al, Intelligent 
%            Data Analysis might be better. See
% http://www.stat.wisc.edu/~mchung/teaching/MIA/reading/SPHARM.shen.2003.pdf
%          
%      Using only spherical harmonic coefficients, do you think you can
%      localize the region of cortical shape difference? Discuss. 
%
%      Note: There is a new technique utilizing spherical wavelets and 
%            there is many claims wavelets perform better than spherical 
%            harmonic representation. This is not necessarly true though. 


%HW20, Only using 5 spherical harmonic basis 
%      Y_lm(0,0,..), Y_lm(1,1,...), Y_lm(2,2,...), Y_lm(3,3,...),
%      Y_lm(4,4,...), write codes for performing the least squares
%      approximation of estimating cortical thickness on a unit sphere.
%
%      cortical thickness = linear combination of 5 basis. 
%
%      Find the best linear combination of 5 basis that give the smallest
%      SSE among all possible combinations up to degree 60.
%      Note: there are total 41^2 basis. So there are 41^2 choose 5
%      possibilities. 

(61^2)*(61^2-1)*(61^2-2)*(61^2-3)*(61^2-4)/factorial(5) 
%5.9286e+15

% Submit the last set of homeworks HW17-22 by December 4 1:00pm. 
% 3 hour open book in-class exam on November 27 1:00-4:00pm. 
% 6 problems similar to homework problems. will basically test the
% understanding of the course materials. Expected problems are on
%
% image segmentation, image smoothing, GLM, DTI, 
% cortical surface modeling, connectivity.









