% System for localizing bacteria in a picture of a petri dish

% Evgeniy Reznik
% January 2020

% function to compare results from different experiments
% warning('off','all'); close all; clc; clear;

%%get files
yourfolderList = {'D:\Einel\MR244\0.00005AHL\200102',...
                  'D:\Einel\MR244\0.00005AHL\200102',...
                  ''};
comparementResults = '\CompareResult';
mkdir(fullfile(comparementResults));
graphs_folder = '\Graphs';
image_time_period = 6; %period of time between images
gfp_frequency = 4; %parameter that represnts frequency of gfp pictures
bf_frequency = 1; %parameter that represnts frequency of bf pictures
mycolormap_C = rand(10000,3);
linestyle = ["-";"--";"-.";];
bin_step = 100;
histBinLocations = 200:bin_step:800;

%% fetch data from experiments
mean_int_exp = {};
std_int_exp = {};
time_length = {};
idx=0;
for exp_folder=yourfolderList
    idx = idx + 1;
    full_graph_fld = fullfile(cell2mat(exp_folder), graphs_folder);
    load(fullfile(full_graph_fld,'mean_CSH.mat'));
    load(fullfile(full_graph_fld,'std_CSH.mat'));
    im_num_index = size(mean_CSH,1);
    mean_int_exp{idx} = nanmean(mean_CSH,2);
    std_int_exp{idx} = nanmean(std_CSH,2);
    time_length{idx} = size(mean_CSH,1);
end
max_time_length = max(cell2mat(time_length));

fig = figure; hold on; title('mean cell intensity'); xlabel('time [min]'); ylabel('intensity [a.u]');
mean_intencity_array = zeros(numel(yourfolderList),max_time_length);
idx=0;
for exp_folder=yourfolderList
    idx = idx + 1;
    im_num_index = size(mean_int_exp{idx},1);
    plot((1:im_num_index).*image_time_period*gfp_frequency, mean_int_exp{idx}, 'DisplayName', cell2mat(exp_folder));
    legend('-DynamicLegend');
    legend('show');
    drawnow;
    mean_intencity_array(idx,:) = padarray(mean_int_exp{idx},[max_time_length-numel(mean_int_exp{idx}) 0],0,'post');
end
errorbar((1:max_time_length).*image_time_period*gfp_frequency, nanmean(mean_intencity_array,1),nanstd(mean_intencity_array,1),...
    'k--', 'DisplayName', 'Average');
legend('show');
drawnow;
filenameMEAN = fullfile(comparementResults, 'avg mean cell intensity.png');
saveas(fig,filenameMEAN);
filenameMEAN = fullfile(comparementResults, 'avg mean cell intensity.fig');
saveas(fig,filenameMEAN);

fig = figure; hold on; title('STD cell intensity'); xlabel('time [min]'); ylabel('intensity [a.u]');
std_intencity_array = zeros(numel(yourfolderList),max_time_length);
idx=0;
for exp_folder=yourfolderList
    idx = idx + 1;
    im_num_index = size(std_int_exp{idx},1);
    plot((1:im_num_index).*image_time_period*gfp_frequency, std_int_exp{idx}, 'DisplayName', cell2mat(exp_folder));
    legend('-DynamicLegend');
    legend('show');
    drawnow;
    std_intencity_array(idx,:) = padarray(std_int_exp{idx},[max_time_length-numel(std_int_exp{idx}) 0],0,'post');
end
errorbar((1:max_time_length).*image_time_period*gfp_frequency, nanmean(std_intencity_array,1),nanstd(std_intencity_array,1),...
    'k--', 'DisplayName', 'Average');
legend('show');
drawnow;
filenameSTD = fullfile(comparementResults, 'avg std cell intensity.png');
saveas(fig,filenameSTD);
filenameSTD = fullfile(comparementResults, 'avg std cell intensity.fig');
saveas(fig,filenameSTD);

fig = figure; hold on; title('SNR cell intensity'); xlabel('time [min]'); ylabel('intensity [a.u]');
snr_intencity_array = zeros(numel(yourfolderList),max_time_length);
idx=0;
for exp_folder=yourfolderList
    idx = idx + 1;
    im_num_index = size(std_int_exp{idx},1);
    plot((1:im_num_index).*image_time_period*gfp_frequency, mean_int_exp{idx}./std_int_exp{idx}, 'DisplayName', cell2mat(exp_folder));
    legend('-DynamicLegend');
    legend('show');
    drawnow;
    snr_intencity_array(idx,:) = padarray(mean_int_exp{idx}./std_int_exp{idx},[max_time_length-numel(mean_int_exp{idx}./std_int_exp{idx}) 0],0,'post');
end
errorbar((1:max_time_length).*image_time_period*gfp_frequency, nanmean(snr_intencity_array,1),nanstd(snr_intencity_array,1),...
    'k--', 'DisplayName', 'Average');
legend('show');
drawnow;
filenameSNR = fullfile(comparementResults, 'avg snr cell intensity.png');
saveas(fig,filenameSNR);
filenameSNR = fullfile(comparementResults, 'avg snr cell intensity.fig');
saveas(fig,filenameSNR);