clear;clc;close all;fclose('all');
load('data\Song-v1.mat');

%main
tic;
num_dim = length(numLabel);
lambda = 2^-3;
all_fid = 1;
numFolds = 10;
k = 10;
Metric = zeros(7,num_dim,numFolds);
Metric_t = zeros(7,num_dim,numFolds);
% d_err = zeros(numFolds,1);
for numFold=1:numFolds
    X_train = data(idx_folds{numFold}.train,:);
    X_test = data(idx_folds{numFold}.test,:);
    y_train = target(idx_folds{numFold}.train,:);
    y_test = target(idx_folds{numFold}.test,:);

    write_file.file_id = all_fid;
    write_file.head_str = '   ';
    write_file.verbose = 1;
    % [~,Metrics] = Pairwise_kNNv2(X_train,X_test,y_train,y_test,k,1,numLabel,0.5);
    % Metric(:,:,numFold) = Metrics;
    [Metric(:,:,numFold),~,y_predict_k,model] = SIDLEv1(X_train,y_train,X_test,y_test,k,numLabel,lambda,write_file);
    % [Metric_t(:,:,numFold),~,y_predict_c,~] = SIDLEv1_comp( X_train, y_train, X_test, y_test,k, numLabel, lambda, write_file );
    [Metric_t(:,:,numFold),~,y_predict_c,~] = CLIM( X_train, y_train, X_test, y_test, numLabel, lambda, write_file );
    % Metric(:,:,numFold) = SIDLEv0(X_train,y_train,X_test,y_test,k,numLabel,lambda,write_file);
    % err_c = y_predict_c~=y_test;
    % err_cc = sum(err_c(:))/numel(err_c);
    % err_k = y_predict_k~=y_test;
    % err_kk = sum(err_k(:))/numel(err_k);
    % d_err(numFold) = abs(err_cc-err_kk);
end
% d_mean = mean(d_err);
% d_var = var(d_err);
% tau = abs(sqrt(numFolds)*d_mean/sqrt(d_var));
mMetric = mean(Metric,2);
mMetric_t = mean(Metric_t,2);
mMetric = reshape(mMetric,[size(mMetric,1),size(mMetric,3)]);
mMetric_t = reshape(mMetric_t,[size(mMetric_t,1),size(mMetric_t,3)]);
d_err = mMetric-mMetric_t;
d_mean = mean(d_err,2);
d_var  = std(d_err,0,2);
tau = abs(sqrt(numFolds).*d_mean./d_var);
%disp
fprintf(all_fid,'\n');
Metric_c = zeros(numFolds,num_dim+1,7);
for iMetric=1:7
    for numFold=1:numFolds
        sum_numFold = 0;
        for dd=1:num_dim
            Metric_c(numFold,dd,iMetric) = Metric(iMetric,dd,numFold);
            sum_numFold = sum_numFold + Metric_c(numFold,dd,iMetric);
        end
        Metric_c(numFold,num_dim+1,iMetric) = sum_numFold/num_dim;
    end
end
metric_name = {'ranking loss';'coverage';'one error';'average precision';'hamming loss';'macro-F1';'micro-F1'};
space_str = '                   ';
temp_str = '    ';
for iMetric=1:7
    iLen = length(metric_name{iMetric});
    if iLen<12
        temp_str = [temp_str,' & ', metric_name{iMetric}, space_str(1:12-iLen)];
    else
        temp_str = [temp_str,' & ', metric_name{iMetric}(1:12)];
    end
end
temp_str = [temp_str,'\n'];
fprintf(all_fid,temp_str);

meanMetric = zeros(num_dim+1,7);
stdMetric = zeros(num_dim+1,7);
for iMetric=1:7
    meanMetric_i = mean(Metric_c(:,:,iMetric));
    stdMetric_i = std(Metric_c(:,:,iMetric));
    meanMetric(:,iMetric) = meanMetric_i';
    stdMetric(:,iMetric) = stdMetric_i';
end
for dd=1:num_dim+1
    if dd<=num_dim
        temp_str = ['dim',num2str(dd)];
    else
        temp_str = 'avg.';
    end
    for iMetric=1:7
        temp_str = [temp_str,' & ', num2str(meanMetric(dd,iMetric),'%4.3f'), '±', num2str(stdMetric(dd,iMetric),'%4.3f')];
    end
    temp_str = [temp_str,'\n'];
    fprintf(all_fid,temp_str);
end
toc;
