function evaluate_labels2(labelfile,labels,phasefile,tmapfile,Nbins,shift_factor);
% function evaluate_labels2(labelfile,labels,phasefile,tmapfile,Nbins,shift_factor);


% ---------------------------------
% Copyright (c) 2003 Serge Dumoulin
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License version 2
% as published by the Free Software Foundation.
% ---------------------------------


% files=strvcat('/data/mri/Areas/CB/cb_Vall_mask.mnc','/data/mri/Areas/GB/gb_Vall_mask.mnc','/data/mri/Areas/NP/np_Vall_mask.mnc','/data/mri/Areas/OE/oe_Vall_mask.mnc','/data/mri/Areas/RA/ra_Vall_mask.mnc','/data/mri/Areas/SD/sd_Vall_mask.mnc','/data/mri/Areas/TL/tl_Vall_mask.mnc');
% files2=strvcat('/data/mri/3Dret/CB/VFS/cb_20010207_vVFSi_pol_fft_phase.mnc','/data/mri/3Dret/GB/VFS/gb_20000511_vVFSi_pol_fft_phase.mnc','/data/mri/3Dret/NP/VFS/np_20010126_vVFSi_pol_fft_phase.mnc','/data/mri/3Dret/OE/VFS/oe_vVFSi_pol_fft_phase.mnc','/data/mri/3Dret/RA/VFS/ra_19990712and20010124_vVFSi_pol_fft_phase.mnc','/data/mri/3Dret/SD/VFS/sd_20000209_vVFSi_pol_fft_phase.mnc','/data/mri/3Dret/TL/VFS/tim_20001120_avg3_2_pol_fft_phase.mnc');
% files3=strvcat('/data/mri/3Dret/CB/VFS/cb_20010207_vVFSi_avg_dspear_tmap.mnc','/data/mri/3Dret/GB/VFS/gb_20000511_vVFSi_avg_dspear_tmap.mnc','/data/mri/3Dret/NP/VFS/np_20010126_vVFSi_avg_dspear_tmap.mnc','/data/mri/3Dret/OE/VFS/oe_vVFSi_avg_dspear_tmap.mnc','/data/mri/3Dret/RA/VFS/ra_19990712and20010124_vVFSi_avg_dspear_tmap.mnc','/data/mri/3Dret/SD/VFS/sd_20000209_vVFSi_avg_dspear_tmap.mnc','/data/mri/3Dret/TL/VFS/tim_20001120_avg3_2_avg_dspear_tmap.mnc');

if nargin < 6,
   shift_factor=pi/2-0.361;
end;
if nargin < 5,
   Nbins=100;
end;
if nargin < 4,
   tmapfile=[];
end;
if nargin < 3,
   help evaluate_labels2;
   return;
end;
if isempty(labels),
   labels=[1:16 19:22];
end;
labelname=strvcat(...
            'lV1',...%1
            'rV1',...%2
            'lV2v',...%3
            'lV2d',...
            'rV2v',...
            'rV2d',...
            'lV3',...
            'rV3',...
            'lVP',...
            'rVP',...
            'lV3A',...
            'rV3A',...
            'lV4v',...
            'rV4v',...
            'lV3B',...
            'rV3B',...
            'lV5',...
            'rV5',...
            'lV7',...
            'rV7',...
            'lV8',...
            'rV8');

%make output matrix
MySize=128;
% Make matrix coordinate system with 0 in center:
coords=-MySize/2+.5:MySize/2-.5;
[xx,yy]=meshgrid(coords);
dist_from_center = sqrt((xx.*xx)+(yy.*yy));
inside  = find(dist_from_center <= MySize/10);
outside = find(dist_from_center > MySize/2);
dist = rot90(angle(xx+i.*yy));

%histogram bins
binwidth=(2*pi)/Nbins;
bins=[-pi:binwidth:pi]';
binsh=bins;
binsh(length(bins))=pi+0.1;% hack to include pi in the last bin

for label=labels,
   disp(sprintf('Processing label(%d), %d of %d', ...
                label,find(label==labels),length(labels)));
   
   phases=[];tval=[];
   for n=1:size(phasefile,1),
      %--- creating mask
      unix(['mincmath -seg -clobber -const2 ' ...
            sprintf(' %.1f %.1f ',label-.5,label+.5) ...
            labelfile(n,:) ' tmpmask.mnc ']);
      if ~isempty(tmapfile),
         [tvals]=find_values_in_mask(deblank(tmapfile(n,:)),'tmpmask.mnc');
         
         if ~isempty(tvals);
            %phase shifting
            tval1=mean(tvals);
            tval=[tval; tval1];
         end;
      else,
         tval='None';
      end;
      %--- getting phases
      [phasesfile]=find_values_in_mask(deblank(phasefile(n,:)),'tmpmask.mnc');
      if ~isempty(phasesfile);
         %phase shifting
         phasesfile=phasesfile+shift_factor;
         values=phasesfile;
         underlimit=find(phasesfile < -pi);
         phasesfile(underlimit)=values(underlimit)+(2*pi);
         overlimit=find(phasesfile > pi);
         phasesfile(overlimit)=values(overlimit)-(2*pi);
         phasesfile=phasesfile*-1;
         
         nvalues2=histc(phasesfile,binsh);
         nvalues=nvalues2(1:length(nvalues2)-1);
         phasesfile2=nvalues./sum(nvalues);
         phases=[phases phasesfile2];
      end;
   end;
   phases=phases*100;%in percentages
   if ~isempty(tmapfile),
      tval=mean(tval);
      titlename=sprintf('%s-label%d-tval%.2f-%d',...
                        labelname(round(label),:),round(label),mean(tval),size(phases,2));
   else,
      titlename=sprintf('%s-label%d-tval%s-%d',...
                        labelname(round(label),:),round(label),mean(tval),size(phases,2));
   end;
   
   MyImage = zeros(size(dist));
   m_phases=mean(phases,2);
   for b=1:Nbins,
      MyImage(find(dist >= bins(b) & dist <= bins(b+1)))=m_phases(b);
   end;
   MyImage(outside)=0;MyImage(inside)=0;
   figure;
   subplot(1,2,1);imagesc(MyImage,[0 max(m_phases)/4]);
   axis('square');axis('off');colormap('gray');%colorbar;
   if ~isempty(titlename),
      set(gcf,'Name',titlename);
      title(titlename);
   end;
   subplot(1,2,2);bar(bins(1:Nbins)+binwidth/2,m_phases,'hist');
   set(gca,'XTick',[-pi/2 0 pi/2],'XTickLabel',[],'XGrid','on');
   hold on;
   subplot(1,2,2);plot([-pi:(2*pi)/Nbins:pi-((2*pi)/Nbins)]+binwidth/2,m_phases+std(phases,0,2),'k.');
   subplot(1,2,2);plot([-pi pi], [max(m_phases)/4 max(m_phases)/4],'--k');
   curax=axis;axis([-pi pi curax(3:4)]);axis('square');
   drawnow;

end;







