% 09/08/2020 Makoto. Used.
% 01/29/2020 Makoto. Used again.
% 01/08/2020 Makoto. Created.

allSets = dir('/data/common/mobi/Experiments/Audiomaze/makoto/p02_fitRigidBody/*.set');

allSetNames = {allSets.name}';

allSubjNames  = cellfun(@(x) x(1:7), allSetNames, 'uniformoutput', false);
allBlockNames = cellfun(@(x) x(9:10), allSetNames, 'uniformoutput', false);

uniqueSubjs  = unique(allSubjNames);
uniqueBlocks = unique(allBlockNames);
rigidBodyApplicableRatio = nan(length(uniqueSubjs), length(uniqueBlocks), 5);
for setIdx = 1:length(allSetNames)
    
    currentBlock = allSetNames{setIdx};
    subjIdx = find(strcmp(currentBlock(1:7), uniqueSubjs));
    blockIdx = find(strcmp(currentBlock(9:10), uniqueBlocks));

    EEG = pop_loadset('filename', currentBlock, 'filepath', '/data/mobi/Audiomaze/makoto/p02_fitRigidBody/', 'loadmode', 'info');
    head_RigidBodyApplicableRatio  = 1-sum(EEG.etc.audiomaze.lessThanThreeLedMarkerIdx(1,:))/length(EEG.etc.audiomaze.lessThanThreeLedMarkerIdx);
    torso_RigidBodyApplicableRatio = 1-sum(EEG.etc.audiomaze.lessThanThreeLedMarkerIdx(2,:))/length(EEG.etc.audiomaze.lessThanThreeLedMarkerIdx);
    hand_RigidBodyApplicableRatio  = 1-sum(EEG.etc.audiomaze.lessThanThreeLedMarkerIdx(3,:))/length(EEG.etc.audiomaze.lessThanThreeLedMarkerIdx);
    footR_RigidBodyApplicableRatio = 1-sum(EEG.etc.audiomaze.lessThanThreeLedMarkerIdx(4,:))/length(EEG.etc.audiomaze.lessThanThreeLedMarkerIdx);
    footL_RigidBodyApplicableRatio = 1-sum(EEG.etc.audiomaze.lessThanThreeLedMarkerIdx(5,:))/length(EEG.etc.audiomaze.lessThanThreeLedMarkerIdx);
    
    rigidBodyApplicableRatio(subjIdx, blockIdx, :) = [head_RigidBodyApplicableRatio ...
                                                      torso_RigidBodyApplicableRatio ...
                                                      hand_RigidBodyApplicableRatio ...
                                                      footR_RigidBodyApplicableRatio ...
                                                      footL_RigidBodyApplicableRatio];
end
save rigidBodyApplicableRatio rigidBodyApplicableRatio

%%
% load('/data/mobi/Audiomaze/makoto/code/rigidBodyApplicableRatio')
load('/data/common/mobi/Experiments/Audiomaze/makoto/code/rigidBodyApplicableRatio')


figure
subplot(1,5,1)
imagesc(squeeze(rigidBodyApplicableRatio(:,:,1)))
subplot(1,5,2)
imagesc(squeeze(rigidBodyApplicableRatio(:,:,2)))
subplot(1,5,3)
imagesc(squeeze(rigidBodyApplicableRatio(:,:,3)))
subplot(1,5,4)
imagesc(squeeze(rigidBodyApplicableRatio(:,:,4)))
subplot(1,5,5)
imagesc(squeeze(rigidBodyApplicableRatio(:,:,5)))

% Move E4 to E3 for subj10.
rigidBodyApplicableRatio(10,9,:) = rigidBodyApplicableRatio(10,10,:);
rigidBodyApplicableRatio(:,10,:) = [];

% Plot parts-wise rigid body applicability.
mazeMean = cat(2, nanmean(rigidBodyApplicableRatio(:,1:3,:),2), ...
                  nanmean(rigidBodyApplicableRatio(:,4:6,:),2), ...
                  nanmean(rigidBodyApplicableRatio(:,7:9,:),2), ...
                  nanmean(rigidBodyApplicableRatio(:,10:12,:),2));

figure


subplot(1,2,1)
barHandle1 = bar(squeeze(nanmean(nanmean(mazeMean,1),2)));
hold on
permutedData = permute(mazeMean, [3 1 2]);
errorbarHandle1 = errorbar(1:5, squeeze(nanmean(nanmean(mazeMean,1),2)), nanstd(permutedData(:,:), 0, 2)/sqrt(size(permutedData,2)*size(permutedData,3)));
set(errorbarHandle1, 'linestyle', 'none', 'color', [0 0 0])
xlim([0.5 5.5])
ylim([0.7 1])
set(gca, 'xticklabel', {'Head' 'Torso' 'Hand' 'FootR' 'FootL'})
set(barHandle1, 'faceColor', [0.93 0.96 1])
ylabel('Probability of >=3 LEDs available')
title('Mean for each rigid body (+/-SE)')

interpolatedData = permutedData(:,:);
meanValues       = nanmean(interpolatedData,2);
nanIdx = find(isnan(interpolatedData(1,:)));
interpolatedData(:,nanIdx) = meanValues;
interpolatedData_originalSize = reshape(interpolatedData, size(permutedData));
[stats, df, pvals] = statcond({interpolatedData(1,:) interpolatedData(2,:) interpolatedData(3,:) interpolatedData(4,:)}, 'paired', 'on');
% F(3,201) = 74.7, p < 0.001


% Plot maze-wise rigid body applicability.
subplot(1,2,2)
barHandle2 = bar(squeeze(nanmean(nanmean(mazeMean,1),3)));
hold on
permutedData = permute(mazeMean, [2 1 3]);
errorbarHandle2 = errorbar(1:4, squeeze(nanmean(nanmean(mazeMean,1),3)), nanstd(permutedData(:,:), 0, 2)/sqrt(size(permutedData,2)*size(permutedData,3)));
set(errorbarHandle2, 'linestyle', 'none', 'color', [0 0 0])
xlim([0.5 4.5])
ylim([0.7 1])
set(gca, 'xticklabel', {'I' 'U' 'T' '+'})
set(barHandle2, 'faceColor', [0.66 0.76 1])
ylabel('Probability of >=3 LEDs available')
title('Mean for each maze (+/-SE)')

interpolatedData = permute(interpolatedData_originalSize, [3 1 2]);
interpolatedData = interpolatedData(:,:);
[stats, df, pvals] = statcond({interpolatedData(1,:) interpolatedData(2,:) interpolatedData(3,:) interpolatedData(4,:)}, 'paired', 'on');
% F(3,252) = 9.9, p < 0.001

set(findall(gcf, '-property', 'fontsize'), 'fontsize', 12)
set(gcf, 'position', [5         884        1018         430]);
print -dsvg rigidBodyApplicability

%% 
rigidBodyApplicableRatio(16,:,:) = [];

figure
subplot(1,5,1)
imagesc(squeeze(rigidBodyApplicableRatio(:,:,1)))
subplot(1,5,2)
imagesc(squeeze(rigidBodyApplicableRatio(:,:,2)))
subplot(1,5,3)
imagesc(squeeze(rigidBodyApplicableRatio(:,:,3)))
subplot(1,5,4)
imagesc(squeeze(rigidBodyApplicableRatio(:,:,4)))
subplot(1,5,5)
imagesc(squeeze(rigidBodyApplicableRatio(:,:,5)))