%% Load the data

% load the module data for 120 reps on R = 5, earlyStop = 60
% if it is of the full length, we need to set earlyStop = 100 with
% moduleResultfull.mat
clear; close all;
load('accuTypeMannual.mat');
load('allegiance.mat');
epochEarlystop = 100;
numInstance = 120;

parfor i = 1:numInstance
    i
    folderName_R_5_rep_120_diff_data{i} = sprintf('r_5_rep_120_diff_data/%d',i);
    dataFile_R_5_rep_120_diff_data{i} = load(sprintf('data/%s/moduleResultFull.mat',folderName_R_5_rep_120_diff_data{i}));
end



%
%% Random test on the activation and correlation modularity

actVec = {};
corrVec = {};
selectedEpochIdx = 1:19:101;
kurtVals = zeros(epochEarlystop,numInstance);
skewVals = zeros(epochEarlystop,numInstance);
actMeanVals = zeros(epochEarlystop, numInstance);
actStdVals = zeros(epochEarlystop, numInstance);

%% plot the curve of representation kurtosis and skewness    
for j = 1:epochEarlystop
    j
    actVecSlice = [];
    corrVecSlice = [];
    for i = 1:numInstance
        tmpAct = dataFile_R_5_rep_120_diff_data{i}.a(:,:,j);
        tmpCorr = corr(tmpAct');
        actVecSlice = [actVecSlice, tmpAct(:)];
        upperIdx = tril(true(size(tmpCorr)),1);
        corrVecSlice = [corrVecSlice, tmpCorr(upperIdx)];
    end
    actMeanVals(j,:) = mean(actVecSlice);
    actStdVals(j,:) = std(actVecSlice);
    kurtVals(j,:) = kurtosis(actVecSlice);
    skewVals(j,:) = skewness(actVecSlice);
    corrMeanVals(j,:) = mean(corrVecSlice);
    corrStdVals(j,:) = std(corrVecSlice);
end

%

% plot the curve of mean activation and mean correlation
figure;
set(gcf,'position',[0,0,1000,500]); 
subplot(1,2,1);
for j = 1:numInstance
    ll = plot((1:epochEarlystop)*10, actMeanVals(:,j),'cyan'); 
    ll.Color = [0.3010, 0.7450, 0.9330, 0.3];hold on;
end
plot((1:epochEarlystop)*10, mean(actMeanVals,2),'blue','LineWidth',2);
ylabel('Activation');
xlabel('Epoch'); xlim([1,1000]);
set(gca,'FontSize',15);
axis square;

subplot(1,2,2);
for j = 1:numInstance
    ll = plot((1:epochEarlystop)*10, corrMeanVals(:,j),'cyan'); 
    ll.Color = [0.3010, 0.7450, 0.9330, 0.3];hold on;
end
plot((1:epochEarlystop)*10, mean(corrMeanVals,2),'blue','LineWidth',2);
ylabel('Correlation');
xlabel('Epoch');xlim([1,1000]);
set(gca,'FontSize',15);
axis square;

%

% plot the curve of kurtosis and skewness
figure;
set(gcf,'position',[0,0,1000,500]); 
subplot(1,2,1);
for j = 1:numInstance
    ll = plot((1:epochEarlystop)*10, kurtVals(:,j),'cyan'); 
    ll.Color = [0.3010, 0.7450, 0.9330, 0.3];hold on;
end
plot((1:epochEarlystop)*10, mean(kurtVals,2),'blue','LineWidth',2);
ylabel('Kurtosis');
xlabel('Epoch'); xlim([1,1000]);
set(gca,'FontSize',15);
axis square;

subplot(1,2,2);
for j = 1:numInstance
    ll = plot((1:epochEarlystop)*10, skewVals(:,j),'cyan'); 
    ll.Color = [0.3010, 0.7450, 0.9330, 0.3];hold on;
end
plot((1:epochEarlystop)*10, mean(skewVals,2),'blue','LineWidth',2);
ylabel('Skewness');
xlabel('Epoch');xlim([1,1000]);
set(gca,'FontSize',15);
axis square;

%


for j = 1:numel(selectedEpochIdx)
    actVec{j} = [];
    corrVec{j} = [];
    for i = 1:numInstance
        tmp = dataFile_R_5_rep_120_diff_data{i}.a(:,:,selectedEpochIdx(j));
        actVec{j} = [actVec{j}; tmp(:)];
        corrVal = corr(tmp');
        upperIdx = tril(true(size(corrVal)),1);
        corrVec{j} = [corrVec{j}; corrVal(upperIdx)];
    end
end


for i = 1:120
    for j = 1:numel(selectedEpochIdx)
        allegianceMatrixSelected{i}(:,:,j) = squeeze(allegianceMatrix{i}(:,:,selectedEpochIdx(j)));
    end
end
rng(0);
iddx = randi(120)

figure;
for i = 1:numel(selectedEpochIdx)
    subplot(3,numel(selectedEpochIdx),i);
    histogram(actVec{i},'NumBins', 20,'Normalization','probability');
    xlim([-40,40]); ylim([0,0.5]);
    xlabel('Activation');
    axis square;
    title(sprintf('Epoch: %d',selectedEpochIdx(i)));
    set(gca,'FontSize',15);

    
    subplot(3,numel(selectedEpochIdx),i+numel(selectedEpochIdx));
    histogram(corrVec{i},'NumBins',20,'Normalization','probability');
    xlim([-1,1]);
    xlabel('Correlation')
    axis square;
    set(gca,'FontSize',15);

    
    subplot(3,numel(selectedEpochIdx),i+2*numel(selectedEpochIdx));
    gplus = 1.00;
    Acell{1} = squeeze(allegianceMatrixSelected{iddx}(:,:,i));
    [cPartition{i}, allegiance{i}, modularityValue(i)] = genRobustCommunityFromASeries(Acell,gplus,1.0);
    colormap('parula');
    plotCommunities(Acell{1},cPartition{i},2); axis square; colorbar;
    set(gca,'FontSize',15);
end


%

% add mediaton analysis for the increase of accuracy?
























