
%% ******** Semi: Indian  *****************************************
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];

load Indian_index_nSam30.mat;
for rr = 1 : 10
    postrn = Recpostrn{rr};  postst = Recpostst{rr}; % sample position, single index
    XTrn = data2(postrn(1,:),:);  YTrn = postrn(2, :)';
    XTst = data2(postst(1,:),:);  YTst = postst(2, :)';
    ntrn = length(YTrn);  ntst = length(YTst); nSam = ntrn + ntst;
    % select 300 samples in each class from XTst to form the unlabeled set
    XU = []; YU = [];
    for i = 1 : nClass
        indexi = find(YTst==classLabel(i));
        nnn = min(length(indexi),300);
        indexi = indexi(randperm(length(indexi))); indexi = indexi(1:nnn);
        XU = [XU; XTst(indexi,:)]; YU = [YU; YTst(indexi)];
    end
  
    % SDA
    options = [];  options.Metric = 'Euclidean';
    options.NeighborMode = 'KNN';       options.k = 5;
    options.WeightMode = 'HeatKernel';  options.t = 0.5;
    X1 = [XTrn; XU];   Y1 = [YTrn; YU];   ntrn1 = size(X1, 1);
    semisplit = false(ntrn1,1);  semisplit(1:ntrn) = true;
    W = constructW(X1, options);
    options = [];  options.W = W;  options.ReducedDim = dim;
    options.ReguBeta = 0.5;
    eigvector_sda = SDA(Y1, X1, semisplit, options);
    for dd = 1 : dim
        XTrnd_sda = XTrn*eigvector_sda(:,1:dd);
        XTstd_sda = XTst*eigvector_sda(:,1:dd);
        Ypre_sda = knnclassify(XTstd_sda, XTrnd_sda, YTrn);
        OA_SDA(rr,dd) = calcError(YTst, Ypre_sda, classLabel);
        Ypre_sdasvm = SVMclassify(XTstd_sda, XTrnd_sda, YTrn, gammaval, Cval);
        OA_SDAs(rr,dd) = calcError(YTst, Ypre_sdasvm, classLabel);
    end
    
    % SELDLPP
    options = []; options.k = 5; options.t = 0.5;  options.ReducedDim = dim;
    eigvector_slpp = SELDLPP(YTrn, XTrn, XU, options);
    for dd = 1 : dim
        XTrnd_slpp  = XTrn*eigvector_slpp(:,1:dd);
        XTstd_slpp  = XTst*eigvector_slpp(:,1:dd);
        Ypre_slpp = knnclassify(XTstd_slpp, XTrnd_slpp, YTrn);
        OA_sLPP(rr,dd) = calcError(YTst, Ypre_slpp, classLabel);
        Ypre_slppsvm = SVMclassify(XTstd_slpp, XTrnd_slpp, YTrn, gammaval, Cval);
        OA_sLPPs(rr,dd) = calcError(YTst, Ypre_slppsvm, classLabel);
    end
    
    % SELDNPE
    options = []; options.k = 5;  options.ReducedDim = dim;
    eigvector_snpe = SELDNPE(YTrn, XTrn, XU, options);
    for dd = 1 : dim
        XTrnd_snpe = XTrn*eigvector_snpe(:,1:dd);
        XTstd_snpe = XTst*eigvector_snpe(:,1:dd);
        Ypre_snpe = knnclassify(XTstd_snpe, XTrnd_snpe, YTrn);
        OA_sNPE(rr,dd) = calcError(YTst, Ypre_snpe, classLabel);
        Ypre_snpesvm = SVMclassify(XTstd_snpe, XTrnd_snpe, YTrn, gammaval, Cval);
        OA_sNPEs(rr,dd) = calcError(YTst, Ypre_snpesvm, classLabel);
    end
end
save Indian2_SF_semi_nSam_30  OA*
[max(mean(OA_SDA))  max(mean(OA_sLPP))  max(mean(OA_sNPE)); max(mean(OA_SDAs))  max(mean(OA_sLPPs))  max(mean(OA_sNPEs))]


