%%%%%%% 01.20.21 extract accuracy from metrics of trained networks

%%%load files from directory
% files=dir('C:\Users\Alicia\Documents\MATLAB\mackTrainedNetwork\mackResults\*.mat')

%%% load files in correct order
P = 'C:\Users\Alicia\Documents\MATLAB\mackTrainedNetwork\mackResults';
S = dir(fullfile(P,'*.mat'));
files = natsortfiles({S.name})';
for k = 1:numel(files)
    fullfile(P,files{k});
end
clear k ans

for ii=1:length(files)
    load(fullfile(P,files{ii}),'metrics')
    
    woodAcc(ii)=metrics.ClassMetrics{1,1};
    backgroundAcc(ii)=metrics.ClassMetrics{2,1};
    woodIoU(ii)=metrics.ClassMetrics{1,2};
    backgroundIoU(ii)=metrics.ClassMetrics{2,2};
    woodBF(ii)=metrics.ClassMetrics{1,3};
    backgroundBF(ii)=metrics.ClassMetrics{2,3};
    
    globalAcc(ii)=metrics.DataSetMetrics{1,1};
    meanAcc(ii)=metrics.DataSetMetrics{1,2};
    meanIOU(ii)=metrics.DataSetMetrics{1,3};
    weightedIOU(ii)=metrics.DataSetMetrics{1,4};
    meanBF(ii)=metrics.DataSetMetrics{1,5};
    
    clear metrics
end
clear ii

WoodAcc.woodAcc=woodAcc;
WoodAcc.woodIoU=woodIoU;
WoodAcc.woodBF=woodBF;
clear woodAcc woodIoU woodBF

BackgroundAcc.backgroundAcc=backgroundAcc;
BackgroundAcc.backgroundIoU=backgroundIoU;
BackgroundAcc.backgroundBF=backgroundBF;
clear backgroundAcc backgroundIoU backgroundBF

DataAcc.global=globalAcc;
DataAcc.mean=meanAcc;
DataAcc.meanIoU=meanIOU;
DataAcc.weightedIoU=weightedIOU;
DataAcc.meanBF=meanBF;
clear globalAcc meanAcc meanIOU weightedIOU meanBF

%% plots

figure
plot(1:length(files),WoodAcc.woodAcc,'-o',1:length(files),BackgroundAcc.backgroundAcc,'-^')
legend('wood','background')
xlabel('net')
ylabel('local accuracy')

figure
plot(1:length(files),WoodAcc.woodIoU,'-o',1:length(files),BackgroundAcc.backgroundIoU,'-^')
legend('wood','background')
xlabel('net')
ylabel('local IoU')

figure
plot(1:length(files),WoodAcc.woodBF,'-o',1:length(files),BackgroundAcc.backgroundBF,'-^')
legend('wood','background')
xlabel('net')
ylabel('local BF')

figure
plot(1:length(files),DataAcc.global,'-s',1:length(files),DataAcc.mean,'-+')
legend('gobal','mean')
xlabel('net')
ylabel('global acc')

