%% Příprava workspace
clear all;
clc;
close all;

%% Načtení datasetů
%load('/home/karela/data/StrokeRatBrain/batches/20190610_092401_190610_STROKE_RAT_BRAIN_AMCAO9_1_1__01/experiment1/conv/data.mat');
%data_reg = load('/home/karela/data/StrokeRatBrain/batches/20190610_092401_190610_STROKE_RAT_BRAIN_AMCAO9_1_1__01/experiment1/reg/data.mat');

%load('/home/karela/data/StrokeRatBrain/batches/20190611_093556_190611_STROKE_RAT_BRAIN_AMCAO10_1_1_01/experiment1/conv/data.mat');
%data_reg = load('/home/karela/data/StrokeRatBrain/batches/20190611_093556_190611_STROKE_RAT_BRAIN_AMCAO10_1_1_01/experiment1/reg/data.mat')

%load('/home/karela/data/StrokeRatBrain/batches/20190614_094953_20190614_Stroke_RAT_BRAIN_AMCAO12_1_1_01/experiment1/conv/data.mat');
%data_reg = load('/home/karela/data/StrokeRatBrain/batches/20190614_094953_20190614_Stroke_RAT_BRAIN_AMCAO12_1_1_01/experiment1/reg/data.mat');

%load('/home/karela/data/StrokeRatBrain/batches/20190821_092549_190821_STROKE_RAT_BRAIN_AMCAO14_1_1_01/experiment1/conv/data.mat')
%data_reg = load('/home/karela/data/StrokeRatBrain/batches/20190821_092549_190821_STROKE_RAT_BRAIN_AMCAO14_1_1_01/experiment1/reg/data.mat')

load('/home/karela/data/StrokeRatBrain/batches/20200610_101545_AMCAO_AC05_1_1_20200610_101545_AMCAO_AC05_1_1_01/experiment1/conv/data.mat');
% data_reg = load('/home/karela/data/StrokeRatBrain/batches/20200610_101545_AMCAO_AC05_1_1_20200610_101545_AMCAO_AC05_1_1_01/experiment1/reg/data.mat');

%load('/home/karela/data/StrokeRatBrain/batches/20200611_114511_AMCAO_AC08_1_1_20200611_114511_AMCAO_AC08_1_1_01/experiment1/conv/data.mat');
%data_reg = load('/home/karela/data/StrokeRatBrain/batches/20200611_114511_AMCAO_AC08_1_1_20200611_114511_AMCAO_AC08_1_1_01/experiment1/reg/data.mat');

%load('/home/karela/data/StrokeRatBrain/batches/20200713_105447_AMCAO_AC10_1_1_20200713_105447_AMCAO_AC10_1_1_01/experiment1/conv/data.mat');
%data_reg = load('/home/karela/data/StrokeRatBrain/batches/20200713_105447_AMCAO_AC10_1_1_20200713_105447_AMCAO_AC10_1_1_01/experiment1/reg/data.mat');

%% Převedení buňky do 4D matice (x,y,z,čas)
nSlices=size(data2,1);
nSamples=size(data2,2);
[nRows,nCols]=size(data2{1,1});
data4D=zeros(nRows,nCols,nSlices,nSamples);
for sl=1:nSlices
    for t=1:nSamples
        data4D(:,:,sl,t)=imrotate(data2{sl,t},90); % Rotace o 90 stupňů
    end
end

outputFolder=[];
nSamples=size(data4D,4);
timeAxis=(0:1:nSamples-1)*info.acq.Ts/60; % Časová osa
Ts = info.acq.Ts; % Vzorkovací perioda

data = data4D;

%% Normalizace dat

AUC_data = sum(abs(data),4)/size(data,4); % Plocha pod křivkou
AUC_data = AUC_data(:);
AUC_data(AUC_data==0)=[];
med_data = median(AUC_data,'all'); % Výběr mediánu
data = data./med_data; % Data dělená nalezeným mediánem

% data(isinf(data))=0;
% data(isnan(data))=0;

%% Automatická detekce

[output_common] = automatic_detection(data,Ts);

figure;
imshow(output_common(:,:,8))

aifs_mean = zeros(size(data,3),size(data,4));

for j = 8
[rows, columns]=find(output_common(:,:,j)==1); % Nalezení indexů společných pozic

%% Získání AIF
aif = zeros(size(data,4)*size(data,3),length(rows));
aif = [];
aifs = [];
aif_s = [];

for i = 1:length(rows)
        aifs= squeeze(data(rows(i,1),columns(i,1),j,:)); % Vykreslení AIF ve voxelech podle nalezených společných pozic
        aif_s(i,:) = aifs;
end

figure;
plot(timeAxis,aif_s'); % Vykreslení všech AIF v řezu
xlabel('Časová osa [min.]')
ylabel('AIF [a.u.]')

aif_mean = mean(aif_s,1); % Průměrná AIF v řezu
figure;
plot(timeAxis,aif_mean); % Vykreslení průměrné AIF v řezu
xlabel('Časová osa [min.]')
ylabel('AIF [a.u.]')

end