
%% Exp6: Indian data, different training numbers,5,10,15,20,25,30
clear; clc;
load Indian_pines_corrected.mat;
load Indian_pines_gt.mat;
Data = indian_pines_corrected;  DataClass = indian_pines_gt;
clear indian*
[nr nc nDim] = size(Data);  nAll = nr*nc;
gt2   = reshape(DataClass, nAll, 1);
tmp = unique(gt2);  classLabel = tmp(tmp~=0); nClass = length(classLabel);
clear DataClass; 
data2 = reshape(Data, nAll, nDim);
X = data2(find(gt2~=0),:);   % all samples
minX = min(X,[],1);  maxX = max(X,[],1);
clear X Data DataClass Class Am;
data2 = (data2 - repmat(minX,nAll,1)) ./ repmat( maxX-minX, nAll, 1);

pos = find(gt2~=0);  nSam = length(pos);
MC = zeros(nSam,nDim);  gamma0 = 0.2;
w = 3;  wc = (w-1)/2;  a = -wc : wc;
for i = 1 : nSam
    id = pos(i);
    idw = repmat(a*nr,w,1) + repmat(a',1,w);
    idw = idw + id; idw = reshape(idw,1,w*w);
    tmp = idw; tmp(tmp>nAll | tmp<1) = [];
    dtmp = repmat(data2(id,:),length(tmp),1) - data2(tmp,:);
    A = sum(dtmp.*dtmp, 2);  wei = exp(-gamma0*A);
    MC(i,:) = 1/sum(wei) * sum(diag(wei)*data2(tmp,:),1);
end
data2(pos,:) = MC;

dim = 30;   alpha = 0.1;   K = 5;   t = 1;
sigmaval = 2.^[-4 : 4];  gammaval = 1./(2*sigmaval.^2);
Cval = [1 10 100 1000 10000];  
lamRLDA = 1e-3;  pcaratioLDE = 0.996;

load Indian_index_nSam5.mat;  nsec = 5;
for rr = 1 : 10
    postrn = Recpostrn{rr};  postst = Recpostst{rr}; 
    XTrn = data2(postrn(1,:),:);  YTrn = postrn(2, :)';
    XTst = data2(postst(1,:),:);  YTst = postst(2, :)';  
    ntrn = length(YTrn);  ntst = length(YTst); nSam = ntrn + ntst; 
    
    % Original data
    Ypre_ori  = knnclassify(XTst, XTrn, YTrn);
    OA_ORI(rr) = calcError(YTst, Ypre_ori, classLabel);
    Ypre_orisvm = SVMclassify(XTst, XTrn, YTrn, gammaval, Cval);
    OA_ORIs(rr) = calcError(YTst, Ypre_orisvm, classLabel);

    % PCA
    eigvector_pca = PCA(XTrn, dim);
    XTrnd_pca = XTrn*eigvector_pca;    XTstd_pca = XTst*eigvector_pca;
    for dd  = 1 : dim
        Ypre_pca  = knnclassify(XTstd_pca(:,1:dd), XTrnd_pca(:,1:dd), YTrn);
        OA_PCA(rr,dd) = calcError(YTst, Ypre_pca, classLabel);
        Ypre_pcasvm = SVMclassify(XTstd_pca(:,1:dd), XTrnd_pca(:,1:dd), YTrn, gammaval, Cval);
        OA_PCAs(rr,dd)  = calcError(YTst, Ypre_pcasvm, classLabel);
    end
    
    % LDA
    eigvector_lda = RLDA(YTrn, XTrn, lamRLDA);  
    XTrnd_lda = XTrn*eigvector_lda;    XTstd_lda = XTst*eigvector_lda;
    for dd = 1 : size(XTrnd_lda,2)
        Ypre_lda  = knnclassify(XTstd_lda(:,1:dd), XTrnd_lda(:,1:dd), YTrn);
        OA_LDA(rr,dd) = calcError(YTst, Ypre_lda, classLabel);
        Ypre_ldasvm = SVMclassify(XTstd_lda(:,1:dd), XTrnd_lda(:,1:dd), YTrn, gammaval, Cval);
        OA_LDAs(rr,dd)  = calcError(YTst, Ypre_ldasvm, classLabel);
    end
    
    % LFDA
    kk = min(7,nsec-1);
    eigvector_lfda = LFDA(XTrn', YTrn, dim, 'orthonormalized', kk);
    XTrnd_lfda  = XTrn*eigvector_lfda;    XTstd_lfda = XTst*eigvector_lfda;
    for dd = 1 : dim
        Ypre_lfda = knnclassify(XTstd_lfda(:,1:dd), XTrnd_lfda(:,1:dd), YTrn);
        OA_LFDA(rr,dd) = calcError(YTst, Ypre_lfda, classLabel);
        Ypre_lfdasvm = SVMclassify(XTstd_lfda(:,1:dd), XTrnd_lfda(:,1:dd), YTrn, gammaval, Cval);
        OA_LFDAs(rr,dd) = calcError(YTst, Ypre_lfdasvm, classLabel);
    end

    % NWFE
    eigvector_nwfe = nwfe(XTrn, YTrn, dim, 0.5); eigvector_nwfe = eigvector_nwfe';
    XTrnd_nwfe = XTrn*eigvector_nwfe;    XTstd_nwfe = XTst*eigvector_nwfe;
    for dd = 1 : dim
        Ypre_nwfe  = knnclassify(XTstd_nwfe(:,1:dd), XTrnd_nwfe(:,1:dd), YTrn);
        OA_NWFE(rr,dd) = calcError(YTst, Ypre_nwfe, classLabel);
        Ypre_nwfesvm = SVMclassify(XTstd_nwfe(:,1:dd), XTrnd_nwfe(:,1:dd), YTrn, gammaval, Cval);
        OA_NWFEs(rr,dd)  = calcError(YTst, Ypre_nwfesvm, classLabel);
    end

    % LDE
    options = [];   options.ReducedDim = dim;  
    options.PCARatio = pcaratioLDE;  options.t = t;
    options.intraK = K;  options.intelK = K; 
    eigvector_lde = lde(XTrn, YTrn, options);
    XTrnd_lde = XTrn*eigvector_lde;    XTstd_lde = XTst*eigvector_lde;
    for dd = 1 : size(XTrnd_lde,2)
        Ypre_lde  = knnclassify(XTstd_lde(:,1:dd), XTrnd_lde(:,1:dd), YTrn);
        OA_LDE(rr,dd) = calcError(YTst, Ypre_lde, classLabel);
        Ypre_ldesvm = SVMclassify(XTstd_lde(:,1:dd), XTrnd_lde(:,1:dd), YTrn, gammaval, Cval);
        OA_LDEs(rr,dd) = calcError(YTst, Ypre_ldesvm, classLabel);
    end
    
    % RLDE
    options = [];   options.ReducedDim = dim;  options.t = t;
    options.alpha = alpha; options.intraK = K;  options.intelK = K;
    eigvector_rlde = RLDE(XTrn, YTrn, options);
    XTrnd_rlde = XTrn*eigvector_rlde;  XTstd_rlde = XTst*eigvector_rlde;
    for dd = 1 : dim
        Ypre_rlde  = knnclassify(XTstd_rlde(:,1:dd), XTrnd_rlde(:,1:dd), YTrn);
        OA_RLDE(rr,dd) = calcError(YTst, Ypre_rlde, classLabel);
        Ypre_rldesvm = SVMclassify(XTstd_rlde(:,1:dd), XTrnd_rlde(:,1:dd), YTrn, gammaval, Cval);
        OA_RLDEs(rr,dd) = calcError(YTst, Ypre_rldesvm, classLabel);
    end
end
save Indian2_SF_nSam_5 gamma0 w alpha K t lamRLDA pcaratioLDE nsec OA* Recpos*
[max(mean(OA_ORI)) max(mean(OA_PCA)) max(mean(OA_LDA))  max(mean(OA_LFDA)) max(mean(OA_NWFE))  max(mean(OA_LDE)) max(mean(OA_RLDE)) ]
[max(mean(OA_ORIs)) max(mean(OA_PCAs)) max(mean(OA_LDAs))  max(mean(OA_LFDAs)) max(mean(OA_NWFEs))  max(mean(OA_LDEs)) max(mean(OA_RLDEs)) ]

%% Multi-window Results
clear; clc;
load Indian_pines_corrected.mat;
load Indian_pines_gt.mat;
Data = indian_pines_corrected;  DataClass = indian_pines_gt;
clear indian*
[nr nc nDim] = size(Data);  nAll = nr*nc;
gt2   = reshape(DataClass, nAll, 1);
tmp = unique(gt2);  classLabel = tmp(tmp~=0); nClass = length(classLabel);
clear DataClass; 
data2 = reshape(Data, nAll, nDim);
X = data2(find(gt2~=0),:);   % all samples
minX = min(X,[],1);  maxX = max(X,[],1);
clear X Data DataClass Class Am;
data2 = (data2 - repmat(minX,nAll,1)) ./ repmat( maxX-minX, nAll, 1);

pos = find(gt2~=0);  nSam = length(pos);  
MC = zeros(nSam,nDim);  gamma0 = 0.2;
w = [3 5 7 9 11 13 15];  
for nn = 1 : length(w)
    wc = (w(nn)-1)/2;  a1 = -wc : wc;
    for i = 1 : nSam
        id = pos(i);
        idw = repmat(a1*nr,w(nn),1) + repmat(a1',1,w(nn));
        idw = idw + id; idw = reshape(idw,1,w(nn)*w(nn));
        tmp = idw; tmp(tmp>nAll | tmp<1) = [];
        dtmp = repmat(data2(id,:),length(tmp),1) - data2(tmp,:);
        A = sum(dtmp.*dtmp, 2);  wei = exp(-gamma0*A);
        MC(i,:) = 1/sum(wei) * sum(diag(wei)*data2(tmp,:),1);
    end
    data3 = data2;  data3(pos,:) = MC;  data{nn} = data3; clear data3;
end
clear Data;

dim = 30;   alpha = 0.1;   K = 5;   t = 0.5;
sigmaval = 2.^[-4 : 4];  gammaval = 1./(2*sigmaval.^2);
Cval = [1 10 100 1000 10000];  
options = [];   options.ReducedDim = dim;  options.t = t;
options.alpha = alpha; options.intraK = K;  options.intelK = K;

load Indian_index_nSam30; 
for rr = 1 : 10
   postrn = Recpostrn{rr};  postst = Recpostst{rr};
    for nn = 1 : length(w)
        dataU = data{nn};
        XTrn = dataU(postrn(1,:),:);  YTrn = postrn(2, :)';
        XTst = dataU(postst(1,:),:);  YTst = postst(2, :)';
        ntrn = length(YTrn);  ntst = length(YTst); nSam = ntrn + ntst;

        [eigvector_rlde L1 L2] = RLDE(XTrn, YTrn, options);
        XLX1 = XTrn'*L1*XTrn; XLX1 = max(XLX1, XLX1');
        XLX2 = XTrn'*L2*XTrn; XLX2 = max(XLX2, XLX2');

        % local pixel neighbor scatter
        w2 = w(nn); wc2 = (w2-1)/2;  a2 = -wc2 : wc2;
        Hw = zeros(nDim, nDim);
        for i = 1 : ntrn
            id = postrn(1,i);
            idw = repmat(a2*nr,w2,1) + repmat(a2',1,w2);
            idw = idw + id; idw = reshape(idw,1,w2*w2);
            tmp = idw; tmp(tmp>nAll | tmp<1) = [];
            dtmp = repmat(dataU(id,:),length(tmp),1) - dataU(tmp,:);
            hi = dtmp'*dtmp;
            Hw = Hw + hi;
        end
        
        XTrnm = XTrn - ones(ntrn,1)*mean(XTrn); Ht = XTrnm'*XTrnm; 
        Hw = max(Hw, Hw'); Ht = max(Ht, Ht');

        option = struct('disp', 0);
        [eigvector, Ei] = eigs(Ht, Hw, dim, 'la', option);
        XTrnd_spda = XTrn*eigvector;  XTstd_spda = XTst*eigvector;
        for dd = 1 : dim
            Ypre_spdaknn(dd,:,nn)  = knnclassify(XTstd_spda(:,1:dd), XTrnd_spda(:,1:dd), YTrn);
            OA_SpDA(dd,rr,nn) = calcError(YTst, Ypre_spdaknn(dd,:,nn)', classLabel);
            Ypre_spdasvm(dd,:,nn) = SVMclassify(XTstd_spda(:,1:dd), XTrnd_spda(:,1:dd), YTrn, gammaval, Cval);
            OA_SpDAs(dd,rr,nn) = calcError(YTst, Ypre_spdasvm(dd,:,nn)', classLabel);
        end

        a = 0.1; b = 0.1;
        Hw1 = (1-b)*Hw + b*(1-a)*XLX1 + a*b*diag(diag(XLX1));
        Ht1 = b*(1-a)*XLX2 + (1-b*(1-a))*Ht;
        option = struct('disp', 0);
        [eigvector, Ei] = eigs(Ht1, Hw1, dim, 'la', option);
        XTrnd_ssrlde = XTrn*eigvector;  XTstd_ssrlde = XTst*eigvector;
        for dd = 1 : dim
            Ypre_ssrldeknn(dd,:,nn)  = knnclassify(XTstd_ssrlde(:,1:dd), XTrnd_ssrlde(:,1:dd), YTrn);
            OA_SSRLDE(dd,rr,nn) = calcError(YTst, Ypre_ssrldeknn(dd,:,nn)', classLabel);
            Ypre_ssrldesvm(dd,:,nn) = SVMclassify(XTstd_ssrlde(:,1:dd), XTrnd_ssrlde(:,1:dd), YTrn, gammaval, Cval);
            OA_SSRLDEs(dd,rr,nn) = calcError(YTst, Ypre_ssrldesvm(dd,:,nn)', classLabel);
        end
    end

    for dd = 1 : dim
        YF_tst = zeros(ntst, 1);
        for k = 1 : ntst
            tmp  = Ypre_spdaknn(dd,k,:);
            itmp = unique(tmp);
            count = histc(tmp, itmp);
            [fc,ic] = max(count);
            YF_tst(k) = itmp(ic);
        end
        OA_SpDA_mw(rr,dd) = calcError(YTst, YF_tst, classLabel);
    end
    for dd = 1 : dim
        YF_tst = zeros(ntst, 1);
        for k = 1 : ntst
            tmp  = Ypre_spdasvm(dd,k,:);
            itmp = unique(tmp);
            count = histc(tmp, itmp);
            [fc,ic] = max(count);
            YF_tst(k) = itmp(ic);
        end
        OA_SpDAs_mw(rr,dd) = calcError(YTst, YF_tst, classLabel);
    end
    for dd = 1 : dim
        YF_tst = zeros(ntst, 1);
        for k = 1 : ntst
            tmp  = Ypre_ssrldeknn(dd,k,:);
            itmp = unique(tmp);
            count = histc(tmp, itmp);
            [fc,ic] = max(count);
            YF_tst(k) = itmp(ic);
        end
        OA_SSRLDE_mw(rr,dd) = calcError(YTst, YF_tst, classLabel);
    end
    for dd = 1 : dim
        YF_tst = zeros(ntst, 1);
        for k = 1 : ntst
            tmp  = Ypre_ssrldesvm(dd,k,:);
            itmp = unique(tmp);
            count = histc(tmp, itmp);
            [fc,ic] = max(count);
            YF_tst(k) = itmp(ic);
        end
        OA_SSRLDEs_mw(rr,dd) = calcError(YTst, YF_tst, classLabel);
    end
end
save Indian2_mw_nSam5_w7now a b alpha K  t OA*  Recpostrn Recpostst;

A1 = [reshape(max(mean(OA_SpDA,2)),1,7); reshape(max(mean(OA_SSRLDE,2)),1,7)];
A2 = [max(mean(OA_SpDA_mw,1));  max(mean(OA_SSRLDE_mw,1))];
A = [A1 A2]
B1 = [reshape(max(mean(OA_SpDAs,2)),1,7); reshape(max(mean(OA_SSRLDEs,2)),1,7)];
B2 = [max(mean(OA_SpDAs_mw,1));  max(mean(OA_SSRLDEs_mw,1))];
B = [B1 B2]


if 0
    clear; clc;
    load Indian2_SF_nSam_15.mat; 
    load Indian2_mw_nSam15_w7now.mat
    plot(100*mean(OA_PCAs),'k-o','linewidth',2); hold on; 
    plot(100*mean(OA_LDAs),'b->','linewidth',2);  
    plot(100*mean(OA_LFDAs),'m-<','linewidth',2);
    plot(100*mean(OA_NWFEs),'c-*','linewidth',2);  
    plot(100*mean(OA_LDEs(:,1:20)),'g-d','linewidth',2); 
    plot(100*mean(OA_RLDEs),'-*','linewidth',2,'Color',[0.8 0.5 0.1])
    plot(100*mean(OA_SSRLDEs_mw),'r-o','linewidth',2);   
    set(gca,'fontsize',14); ylim([15 95]);
    h = legend('PCA','LDA','LFDA','NWFE','LDE','RLDE','SSRLDE');  
    set(h,'Fontsize',13);  
    xlabel('Reduced Dimension','fontsize',15); 
    ylabel('Overall Accuracy (%)','fontsize',15); xlim([1 30]);   
end

