function [rankingBy]=nic_rsdd_showAggCompRanking(appConfig)
% rank and show the components using scores
% Plot the curve of component rankings
% Input: appConfig - structure containing all parameters necessary for
%		     group
%-----------------------------------------------------------
%   Copyright (C) 2009 Neuroimage Computing Group, State Key Laboratory of
%   Cognitive Neuroscience and Learning, Beijing normal university
%	Written by Gwen Chen
%	Version=1.0;
%	Release=20090726;
%   Last Revised by Zhang Han 2011/10/30 by fixing the error of rank showing pictures
%-----------------------------------------------------------
%

%close all;
numSub =  length(appConfig.AppSubjectLabels);
numOfIC = appConfig.compNum;% the number of IC
outPrefix=appConfig.outPrefix;

figNumPerPage=[2 2]; % the number of sub plot: column*rows
pageItemCount=prod(figNumPerPage);
indRankings=[];
indRankingsIndex=[];
% indRankings1=[];
% indRankingsIndex1=[];
% indRankings2=[];
% indRankingsIndex2=[];

outDir=appConfig.outDir;

% plot curve of agg and individual component
for i=1:pageItemCount:numSub+1
%     figure;
%     set(gcf,'Name',[outPrefix,'_','Ranking component by mean correlation coefficient']);
%     hold on;
    rankings=[];
    titleStr='';
    for j=1:pageItemCount
        if(i+j>numSub+2)
            break;
        end        
        if(i==1 && j==1)
            % agg
            icaOut=fullfile(outDir,'mat',sprintf('%s_ica.mat',outPrefix));% load ICA of agg component
            dd=load(icaOut,'coefRanking');
            aggCoefRanking=1-dd.coefRanking;
            rankings=aggCoefRanking;
            titleStr='Aggregate';
        else
            icaOut=fullfile(outDir,'mat',sprintf('%s_ica_br%d.mat',outPrefix,i-2+j));% load ICA of individual component
            dd=load(icaOut,'coefRanking');
            aggCoefRanking=1-dd.coefRanking;
            rankings=aggCoefRanking;%indCoefRanking{i-2+j};
            titleStr=sprintf('Subject %d',i-2+j);
            
            % record coef of every individual component
            indRankings=[indRankings;rankings];
            
            [ar,ind]=sort(rankings,'descend');
            [ar,ind]=sort(ind);
            % the index of component 
            indRankingsIndex=[indRankingsIndex;ind];
                   
        end
        
        
%         subplot(figNumPerPage(1),figNumPerPage(2),j);
        
%         plotMap(rankings,titleStr);% plot the curve
        
%         hold off;
    end
    
%     hold off;
end

plotRankingCurve(appConfig,indRankings,indRankingsIndex,'all');

return;

function plotMap(rankings,titleStr)
% plot a curve of the average coef of every coponent in a subject
% input: rankigs, the ranking coef, titleStr, the title of map

[ar,ind]=sort(rankings,'descend');
hold on;
plot(ar);

title(titleStr);
ttt={};
for i=1:length(ind)
    ttt{i}=ind(i);
end

set(gca,'XTick',1:1:length(ind));

set(gca,'XTickLabel',ttt);
xlabel('Comp index');
ylabel('r');
hold off;
return;



function plotRankingCurve(appConfig,indRankings,indRankingsIndex,tt)
% Plot the curve  of ranking the components
%
MAX_ICS_PERPAGE=20;
numSub =  length(appConfig.AppSubjectLabels);
numOfIC = appConfig.compNum;% the number of IC
outPrefix=appConfig.outPrefix;

means=mean(indRankings,1);


stds=std(indRankings);

if(size(indRankings,1)>1)
    rankingBy=means./stds; % mean coef of individual component / standard deviation of componnet
else
    rankingBy=means;
end

if(numOfIC<=MAX_ICS_PERPAGE*1.5)

	figure;

	[a b]=sort(rankingBy,'descend');

	disp(sprintf('The ranking for mean/std is:'));   % corrected by Zhang Han
	b
	xx={};
	for i=1:numOfIC
	xx{i}=b(i);
	end


	% plot curve using index of mean/std
	subplot(3,1,1);
	plot(a,'b-o')
	set(gca,'XTickLabel',xx);
	set(gca,'XTick',1:1:numOfIC);
	title('Ranking component by mean/std');


	% plot curve using index of mean (every individual )
	[a b]=sort(means,'descend');

	disp(sprintf('The ranking of mean distance is:'));
	b


	xx={};
	for i=1:numOfIC
	xx{i}=b(i);
	end
	subplot(3,1,2);
	plot(a,'r-o');
	set(gca,'XTickLabel',xx);
	set(gca,'XTick',1:1:numOfIC);
	title('Ranking component by mean');

	% plot curve using index of mean (every individual )
	[x y]=sort(indRankings,2,'descend');
	meanIndex=mean(y,1);
	[a b]=sort(meanIndex,'ascend');


	disp(sprintf('The ranking of mean index is:'));
	b

	xx={};
	for i=1:numOfIC
	xx{i}=b(i);
	end
	subplot(3,1,3);
	plot(a,'r-o');
	set(gca,'XTickLabel',xx);
	set(gca,'XTick',1:1:numOfIC);
	title('Ranking component by mean index');

	set(gcf,'Name',[outPrefix,'_','Ranking component','(',tt,')']);
else
	figure; % the first
	[a b]=sort(rankingBy,'descend');

	disp(sprintf('The ranking for ttest of distance is:'));
	b
	xx={};
	for i=1:numOfIC
	xx{i}=b(i);
	end


	% plot curve using index of mean/std
	subplot(2,1,1);
	plot(a(1:round(numOfIC/2)),'b-o')
	set(gca,'XTickLabel',xx(1:round(numOfIC/2)));
	set(gca,'XTick',1:round(numOfIC/2));
	title(sprintf('Ranking component by mean/std(%d~%d)',1,round(numOfIC/2)));

	subplot(2,1,2);
	plot(a((round(numOfIC/2)+1):numOfIC),'b-o')
	set(gca,'XTickLabel',xx(round(numOfIC/2)+1:numOfIC));
	set(gca,'XTick',1:1:(numOfIC-round(numOfIC/2))); % fixed the error by Zhang Han 2011/10/30

	title(sprintf('Ranking component by mean/std(%d~%d)',round(numOfIC/2)+1,numOfIC));
	set(gcf,'Name',[outPrefix,'_','Ranking component','(',tt,')']);

       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	figure; % the second
	% plot curve using index of mean (every individual )
	[a b]=sort(means,'descend');

	disp(sprintf('The ranking of mean distance is:'));
	b


	xx={};
	for i=1:numOfIC
	xx{i}=b(i);
	end

	subplot(2,1,1);
	plot(a(1:round(numOfIC/2)),'r-o');
	set(gca,'XTickLabel',xx(1:1:round(numOfIC/2)));
	set(gca,'XTick',1:round(numOfIC/2));
	title(sprintf('Ranking component by mean(%d~%d)',1,round(numOfIC/2)));
	subplot(2,1,2);
	plot(a((round(numOfIC/2)+1):numOfIC),'r-o');
	set(gca,'XTickLabel',xx(round(numOfIC/2)+1:numOfIC));
	set(gca,'XTick',1:1:(numOfIC-round(numOfIC/2)));
	title(sprintf('Ranking component by mean(%d~%d)',round(numOfIC/2)+1,numOfIC));
	set(gcf,'Name',[outPrefix,'_','Ranking component','(',tt,')']); % fixed the error by Zhang Han 2011/10/30
	
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	figure; % the third
	% plot curve using index of mean (every individual )
	[x y]=sort(indRankings,2,'descend');
	meanIndex=mean(y,1);
	[a b]=sort(meanIndex,'ascend');


	disp(sprintf('The ranking of mean index is:'));
	b

	xx={};
	for i=1:numOfIC
	xx{i}=b(i);
	end
	subplot(2,1,1);
	plot(a(1:round(numOfIC/2)),'k-o');
	set(gca,'XTickLabel',xx(1:round(numOfIC/2)));
	set(gca,'XTick',1:1:round(numOfIC/2));
	title(sprintf('Ranking component by mean index(%d~%d)',1,round(numOfIC/2)));
	subplot(2,1,2);
	plot(a((round(numOfIC/2)+1):numOfIC),'k-o');
	set(gca,'XTickLabel',xx(round(numOfIC/2)+1:numOfIC));
	set(gca,'XTick',1:1:(numOfIC-round(numOfIC/2)));
	title(sprintf('Ranking component by mean index(%d~%d)',round(numOfIC/2)+1,numOfIC));
    % fixed the error by Zhang Han 2011/10/30
	set(gcf,'Name',[outPrefix,'_','Ranking component','(',tt,')']);

end

