% Quantifying QAR and SAR values

clear

% Load QAR files

folder = '/Users/Colin/Documents/MATLAB/DFlow/influences_chap2/QAR_cells/';
files = dir([folder '*.mat']);

for i=1:length(files)
   load([folder files(i).name]);
end

% Load SAR files

folder = '/Users/Colin/Documents/MATLAB/DFlow/influences_chap2/SAR_cells/';
files = dir([folder '*.mat']);

for i=1:length(files)
   load([folder files(i).name]);
end

% Outputs reported in following columns
% Column 1 - Baseline
% Column 2 - % diff ch down
% Column 3 - % diff ch up
% Column 4 - % diff bank down
% Column 5 - % diff bank up
% Column 6 - % diff fp040
% Column 7 - % diff fp080
% Column 8 - % diff fp100
% Column 9 - % diff bank040
% Column 10 - % diff bank080
% Column 11 - % diff bank100

%% QAR

% Moderate event calculation
for i = 1:6
    qar_flow(:,1) = xs_fp060_cell{i,9};
    qar_flow(:,2) = xs_chdown_cell{i,9};
    qar_flow(:,3) = xs_chup_cell{i,9};
    qar_flow(:,4) = xs_bankdown_cell{i,9};
    qar_flow(:,5) = xs_bankup_cell{i,9};
    qar_flow(:,6) = xs_fp040_cell{i,9};
    qar_flow(:,7) = xs_fp080_cell{i,9};
    qar_flow(:,8) = xs_fp100_cell{i,9};
    qar_flow(:,9) = xs_bank040_cell{i,9};
    qar_flow(:,10) = xs_bank080_cell{i,9};
    qar_flow(:,11) = xs_bank100_cell{i,9};

    qar_diff(i,1) = qar_flow(108,1);
    for j = 2:11
        qar_diff(i,j) = ((qar_flow(108,j) - qar_flow(108,1)) ./...
            (1 - qar_flow(108,1))) * 100;
    end
end
% Major event calculation
% i = 2;
% qar_s300(:,1) = xs_fp060_cell{i,9};
% qar_s300(:,2) = xs_chdown_cell{i,9};
% qar_s300(:,3) = xs_chup_cell{i,9};
% qar_s300(:,4) = xs_bankdown_cell{i,9};
% qar_s300(:,5) = xs_bankup_cell{i,9};
% qar_s300(:,6) = xs_fp040_cell{i,9};
% qar_s300(:,7) = xs_fp080_cell{i,9};
% qar_s300(:,8) = xs_fp100_cell{i,9};
% qar_s300(:,9) = xs_bank040_cell{i,9};
% qar_s300(:,10) = xs_bank080_cell{i,9};
% qar_s300(:,11) = xs_bank100_cell{i,9};
% 
% qar_diff_s300(1,1) = qar_s300(108,1);
% for j = 2:11
%     qar_diff_s300(1,j) = ((qar_s300(108,j) - qar_s300(108,1)) ./...
%         (1 - qar_s300(108,1))) * 100;
% end

%% SAR

% Moderate event calculation
for i = 1:6
    sar_flow(:,1) = obs_fp060_cell{i,6};
    sar_flow(:,2) = obs_chdown_cell{i,6};
    sar_flow(:,3) = obs_chup_cell{i,6};
    sar_flow(:,4) = obs_bankdown_cell{i,6};
    sar_flow(:,5) = obs_bankup_cell{i,6};
    sar_flow(:,6) = obs_fp040_cell{i,6};
    sar_flow(:,7) = obs_fp080_cell{i,6};
    sar_flow(:,8) = obs_fp100_cell{i,6};
    sar_flow(:,9) = obs_bank040_cell{i,6};
    sar_flow(:,10) = obs_bank080_cell{i,6};
    sar_flow(:,11) = obs_bank100_cell{i,6};

    sar_diff(i,1) = sar_flow(108,1);
    for j = 2:11
        sar_diff(i,j) = ((sar_flow(108,j) - sar_flow(108,1)) ./...
            (1- sar_flow(108,1))) * 100;
    end
end

% % Major event calculation
% i = 2;
% sar_s300(:,1) = obs_fp060_cell{i,6};
% sar_s300(:,2) = obs_chdown_cell{i,6};
% sar_s300(:,3) = obs_chup_cell{i,6};
% sar_s300(:,4) = obs_bankdown_cell{i,6};
% sar_s300(:,5) = obs_bankup_cell{i,6};
% sar_s300(:,6) = obs_fp040_cell{i,6};
% sar_s300(:,7) = obs_fp080_cell{i,6};
% sar_s300(:,8) = obs_fp100_cell{i,6};
% sar_s300(:,9) = obs_bank040_cell{i,6};
% sar_s300(:,10) = obs_bank080_cell{i,6};
% sar_s300(:,11) = obs_bank100_cell{i,6};
% 
% sar_diff_s300(1,1) = sar_s300(108,1);
% for j = 2:11
%     sar_diff_s300(1,j) = ((sar_s300(108,j) - sar_s300(108,1)) ./...
%         (1 - sar_s300(108,1))) * 100;
% end

%% Clear variables

clear xs_* obs_*

%% Maximum and mininum percentages

for i = 1:6
    
    maxQ_topo_ch(i,1) = max([qar_diff(i,2:3)]);
    maxQ_topo_bank(i,1) = max([qar_diff(i,4:5)]);
    maxQ_n_fp(i,1) = max([qar_diff(i,6:8)]);
    maxQ_n_bank(i,1) = max([qar_diff(i,9:11)]);

    minQ_topo_ch(i,1) = min([qar_diff(i,2:3)]);
    minQ_topo_bank(i,1) = min([qar_diff(i,4:5)]);
    minQ_n_fp(i,1) = min([qar_diff(i,6:8)]);
    minQ_n_bank(i,1) = min([qar_diff(i,9:11)]);

    maxS_topo_ch(i,1) = max([sar_diff(i,2:3)]);
    maxS_topo_bank(i,1) = max([sar_diff(i,4:5)]);
    maxS_n_fp(i,1) = max([sar_diff(i,6:8)]);
    maxS_n_bank(i,1) = max([sar_diff(i,9:11)]);

    minS_topo_ch(i,1) = min([sar_diff(i,2:3)]);
    minS_topo_bank(i,1) = min([sar_diff(i,4:5)]);
    minS_n_fp(i,1) = min([sar_diff(i,6:8)]);
    minS_n_bank(i,1) = min([sar_diff(i,9:11)]);
    
%     maxQ_topo_ch(i,1) = max([qar_diff(i,2:3) qar_diff_s300(i,2:3)]);
%     maxQ_topo_bank(i,1) = max([qar_diff(i,4:5) qar_diff_s300(i,4:5)]);
%     maxQ_n_fp(i,1) = max([qar_diff(i,6:8) qar_diff_s300(i,6:8)]);
%     maxQ_n_bank(i,1) = max([qar_diff(i,9:11) qar_diff_s300(i,9:11)]);
% 
%     minQ_topo_ch(i,1) = min([qar_diff(i,2:3) qar_diff_s300(i,2:3)]);
%     minQ_topo_bank(i,1) = min([qar_diff(i,4:5) qar_diff_s300(i,4:5)]);
%     minQ_n_fp(i,1) = min([qar_diff(i,6:8) qar_diff_s300(i,6:8)]);
%     minQ_n_bank(i,1) = min([qar_diff(i,9:11) qar_diff_s300(i,9:11)]);
% 
%     maxS_topo_ch(i,1) = max([sar_diff(i,2:3) sar_diff_s300(i,2:3)]);
%     maxS_topo_bank(i,1) = max([sar_diff(i,4:5) sar_diff_s300(i,4:5)]);
%     maxS_n_fp(i,1) = max([sar_diff(i,6:8) sar_diff_s300(i,6:8)]);
%     maxS_n_bank(i,1) = max([sar_diff(i,9:11) sar_diff_s300(i,9:11)]);
% 
%     minS_topo_ch(i,1) = min([sar_diff(i,2:3) sar_diff_s300(i,2:3)]);
%     minS_topo_bank(i,1) = min([sar_diff(i,4:5) sar_diff_s300(i,4:5)]);
%     minS_n_fp(i,1) = min([sar_diff(i,6:8) sar_diff_s300(i,6:8)]);
%     minS_n_bank(i,1) = min([sar_diff(i,9:11) sar_diff_s300(i,9:11)]);
end
%% Plot trials
% Outputs reported in following columns
% Column 1 - Baseline
% Column 2 - % diff ch down
% Column 3 - % diff ch up
% Column 4 - % diff bank down
% Column 5 - % diff bank up
% Column 6 - % diff fp040
% Column 7 - % diff fp080
% Column 8 - % diff fp100
% Column 9 - % diff bank040
% Column 10 - % diff bank080
% Column 11 - % diff bank100
%col_id = [1 6 7 8];

% figure(1)
% set(gcf,'color','white');
% for k = col_id
%     if k == 1
%         scatter(qar_s180(1:108,k),sar_s180(:,k),'k');
%         ax = gca;
%         ax.ColorOrderIndex = 1;
%     else
%         scatter(qar_s180(1:108,k),sar_s180(:,k));
%     end
%     hold on
% end
% legend('Baseline','FP 0.040','FP 0.80','FP 0.100',...
%     'Location','southeast');
% legend('boxoff');
% 
% figure(2)
% set(gcf,'color','white');
% for k = col_id
%     if k == 1
%         scatter(qar_s300(1:108,k),sar_s300(:,k),'k');
%         ax = gca;
%         ax.ColorOrderIndex = 1;
%     else
%         scatter(qar_s300(1:108,k),sar_s300(:,k));
%     end
%     hold on
% end
% legend('Baseline','FP 0.040','FP 0.80','FP 0.100',...
%     'Location','southeast');
% legend('boxoff');
