function [sensitivity specificity  accuracy feature_weight ]=pr_classification_M3(inputdata,inputlabel,number_ROI)

% *************************************************************************
% This function is used to estimate the performance of M3 method[(multi-modal
% imaging (structural and functional MRI) and multi-level characteristics
% (ALFF, regional homogeneity (ReHo) and regional functional connectivity
%  strength (RFCS) for R-fMRI) with multi-classifier)] by
% leave-one-out Cross validation.We applied  different types of
% features to obtain the MLDA-based base classifier(each type of feature obtained one base classifier).
% These base classifiers were combined through weighted voting.
% we can obtain each base classifierĄ¯s weight of voting by calculating its
% classification accuracy of leave-one-out within the training set.
% Note that we can also extend our method to
% other characteristics which describe different property of the same region
% of interest(ROI).

% Accruracy=(TP+TN)/(TP+FN+TN+FP)
% Sensitivity=TP/(TP+FN);
% Specificity=TN/(TN+FP);
% where TP, FN, TN, and FP denoted the number of patients correctly
% predicted, the number of patients classified as normal controls, the
% number of normal controls correctly predicted and the number of normal
% controls classified as patients, respectively.

% Input:

%         inputdata:  n*m: n is the number of subjects and m is the number of features.
%                     m=number_ROI*the number of property of feature. We
%                     put the features of each property together.

%         inputlabel: n*1, the label corresponding the inputdate.

%         number_ROI: the number of regions of interest (ROIs). This means
%         how many ROIs we divided the brain.

% Output:

%         sensitivity:  the classification sensitivity.

%         specificity:  the classification specificity.

%            accuracy:  the classification accuracy.

%      feature_weight:  the weight of each ROI.


% Written by Zhengjia Dai, State Key Laboratory of Cognitive Neuroscience
% and Learning, Beijing Normal University, Beijing, P.R.China, 100875. 
% Contact information:
% Zhengjia Dai: daizhengjia13@gmail.com
% Yong He : yong.h.he@gmail.com; yong.he@bnu.edu.cn
% September 24th,2011
% *************************************************************************


[number_subject number_feature]=size(inputdata);

num_modality=number_feature/number_ROI;

feature_index=zeros(number_subject,num_modality,number_ROI);

feature_index1=zeros(number_subject,num_modality,number_ROI);

index=zeros(number_subject,num_modality,number_ROI);

YY_test_value=zeros(number_subject,num_modality);


for i=1:number_subject

    % dividing the inputdat into taining data and testing data.
    data=inputdata;
    label=inputlabel;
    testingdata=data(i,:);
    testinglabel=label(i,1);
    data(i,:)=[];
    label(i)=[];
    trainingdata=data;
    traininglabel=label;

    % feature normalization.
    number_train=size( trainingdata,1);
    mean_feature=mean(trainingdata);
    trainingdata=trainingdata-repmat(mean_feature,number_train,1);
    testingdata=testingdata-mean_feature;

    for k=1:num_modality

        % estimating the performance of each MLDA-based base classifier based
        % on the feature of each property.
        [ Y_label sen_train spe_train acc_train]=pr_classification_MLDA(trainingdata(:,((k-1)*number_ROI+1):k*number_ROI),traininglabel);
        acc(1,k)=acc_train;
        bcc(1,k)=sen_train;

        % obtaining the index of the selected features of each property
        % to get the final weight of feature.
        [ select_train select_test index1 ]=pr_feature_select(trainingdata(:,((k-1)*number_ROI+1):k*number_ROI),traininglabel, testingdata(:,((k-1)*number_ROI+1):k*number_ROI));

        % obtaining the weight of the selected features of each property
        % to get the final weight of feature.
        [ w_fda threshold m1_lda m2_lda Y_latent_train ]=pr_MLDA(select_train,traininglabel);


        % calculating the weight of feature in original feature space.
        
        diff=w_fda';
        index(i,k,index1 )=1;
        feature_index1(i,k, index1 )=abs(diff);
       
        feature_index(i,k, index1 )=feature_index1(i,k, index1 )/max(feature_index1(i,k, index1 ))*acc(1,k);

        [ TestResult Y_latent_test]=pr_test_MLDA(select_test,w_fda ,threshold, m1_lda ,m2_lda );

        YYY_test_label(1,k)=TestResult;  YY_test_value(i,k)=Y_latent_test;

    end

    % calculating the value of testing data in one-dimensional feature
    % space.
    test_values(i,1)=sum(acc.*YYY_test_label);

    % predicting the label of testing data.
    test_label11(i,1)=sign(sum(acc.*YYY_test_label));

    if test_label11(i,1)==0
        acc_index=find(acc==max(acc));
        if size(acc_index,2)~=1
            bcc_index=find(bcc==max(bcc));
            test_label11(i,1)=YYY_test_label(1,bcc_index(1,1));
        else
            test_label11(i,1)=YYY_test_label(1,acc_index);
        end
    end


end

% estimating the performance of M3 method.
sensitivity=size(find((test_label11==-1)&(inputlabel==-1)),1)/size(find(inputlabel==-1),1);
specificity=size(find((test_label11==1)&(inputlabel==1)),1)/size(find(inputlabel==1),1);
accuracy=size(find(test_label11==inputlabel),1)/number_subject;

% intergrating the weights of features based each property.
for i=1:num_modality
    bb(:,:)=index(:,i,:);
    cc(:,:)=feature_index(:,i,:);
    cc(:,find(sum(bb)~=number_subject))=0;
    feature_weight_all(i,:)=mean(cc);
end
feature_weight=sum(feature_weight_all,1);

