%load the data
patient_2 = niftiread('location of the niftii file');
info_2 = niftiinfo('location of the niftii file');
%
%Load the registered MRA atlas to the patient PET scan
vessel = double(niftiread('location of the registeed atlas'));

%
%Select the time frames 

%
for k = 1:645
    data_62(:,:,k)=double(patient_2(:,:,k,62));
end

for k = 1:645
    data_61(:,:,k)=double(patient_2(:,:,k,61));
end

for k = 1:645
    data_60(:,:,k)=double(patient_2(:,:,k,60));
end

for k = 1:645
    data_59(:,:,k)=double(patient_2(:,:,k,59));
end

for k = 1:645
    data_58(:,:,k)=double(patient_2(:,:,k,58));
end

for k = 1:645
    data_57(:,:,k)=double(patient_2(:,:,k,57));
end

for k = 1:645
    data_54(:,:,k)=double(patient_2(:,:,k,54));
end

for k = 1:645
    data_47(:,:,k)=double(patient_2(:,:,k,47));
end

for k = 1:645
    data_40(:,:,k)=double(patient_2(:,:,k,40));
end

for k = 1:645
    data_35(:,:,k)=double(patient_2(:,:,k,35));
end

for k = 1:645
    data_30(:,:,k)=double(patient_2(:,:,k,30));
end

for k = 1:645
    data_27(:,:,k)=double(patient_2(:,:,k,27));
end

for k = 1:645
    data_25(:,:,k)=double(patient_2(:,:,k,25));
end

for k = 1:645
    data_17(:,:,k)=double(patient_2(:,:,k,17));
end

for k = 1:645
    data_15(:,:,k)=double(patient_2(:,:,k,15));
end

for k = 1:645
    data_7(:,:,k)=double(patient_2(:,:,k,7));
end

for k = 1:645
    data_8(:,:,k)=double(patient_2(:,:,k,8));
end

for k = 1:645
    data_9(:,:,k)=double(patient_2(:,:,k,9));
end

for k = 1:645
    data_10(:,:,k)=double(patient_2(:,:,k,10));
end

for k = 1:645
    data_11(:,:,k)=double(patient_2(:,:,k,11));
end

for k = 1:645
    data_12(:,:,k)=double(patient_2(:,:,k,12));
end

for k = 1:645
    data_13(:,:,k)=double(patient_2(:,:,k,13));
end

%
%Extract the brain images from total body PET images

A=103;
for b = 531:633
    brain_7(:,:,A)=data_7(:,:,b);
    A=A-1;
end 

A=103;
for b = 531:633
    brain_8(:,:,A)=data_8(:,:,b);
    A=A-1;
end 

A=103;
for b = 531:633
    brain_9(:,:,A)=data_9(:,:,b);
    A=A-1;
end 

A=103;
for b = 531:633
    brain_10(:,:,A)=data_10(:,:,b);
    A=A-1;
end 


A=103;
for b = 531:633
    brain_11(:,:,A)=data_11(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_12(:,:,A)=data_12(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_13(:,:,A)=data_13(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_15(:,:,A)=data_15(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_17(:,:,A)=data_17(:,:,b);
    A=A-1;
end


A=103;
for b = 531:633
    brain_25(:,:,A)=data_25(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_27(:,:,A)=data_27(:,:,b);
    A=A-1;
end


A=103;
for b = 531:633
    brain_30(:,:,A)=data_30(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_35(:,:,A)=data_35(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_40(:,:,A)=data_40(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_47(:,:,A)=data_47(:,:,b);
    A=A-1;
end


A=103;
for b = 531:633
    brain_54(:,:,A)=data_54(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_57(:,:,A)=data_57(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_58(:,:,A)=data_58(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_59(:,:,A)=data_59(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_60(:,:,A)=data_60(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_61(:,:,A)=data_61(:,:,b);
    A=A-1;
end

A=103;
for b = 531:633
    brain_62(:,:,A)=data_62(:,:,b);
    A=A-1;
end 

%%
%Create a 4d matrix to store the dynamic PET scans of brain
dynamic_ts_kp=double(cat(4,brain_8,brain_9,brain_10,brain_11,brain_12,brain_13,brain_15,brain_17,brain_25,brain_27,brain_30,brain_35,brain_40,brain_47,brain_54,brain_57, brain_58,brain_59,brain_60,brain_61,brain_62));

%Define the timing frames (mins)
T_kp=[0.18333334,0.21666667,0.25,0.28333333,0.31666666,0.35,0.41666666,0.48333332,0.7499833,...
0.81665,0.91665,1.41665,3.4166667,8.16665,22.16665,37.16665,42.16665,47.16665,52.16665,57.16665,62.16665];

%Create a 4d matrix to store the dynamic PET scans of total body PET
dynamic_ts_frames=double(cat(4,data_8,data_9,data_10,data_11,data_12,data_13,data_15,data_17,data_25,data_27,data_30,data_35,data_40,data_47,data_54,data_57, data_58,data_59,data_60,data_61,data_62));

%%
%Thresholding brain PET images for higher probaility voxels
vessel_m=double(vessel);

%
B=1;
for iii=1:1:103
    for ii=1:1:440
        for i=1:1:440
            if vessel_m(i,ii,iii)<=200%thresholding for higher prob 
                vessel_m(i,ii,B)=0;
%             else
%                  vessel_prob(i,ii,B)= 1;%vessel_m(i,ii,B)/max(vessel_m,[],'all');
            
            end
        end
    end
    B=B+1;
    
end
%
vessel_m_max= max(vessel_m,[],'all');
vessel_prob= vessel_m./vessel_m_max;
%
pr_2=dynamic_ts_kp.*vessel_prob;

%
[x, y, z] = size(vessel_prob);
Aif_main = [];
C = {};
C_main={};
mixing_main=[];
X_aif_main = [];
Y_aif_main = [];
for ii=6:21
    

    mixing_prop =[];
    Aif = [];
    Tac = [];
    X_aif = [];
    Y_aif = [];
    A=1;


    for i=62:71 %slice = 10 
        volumeData = pr_2;
        volumeData = squeeze(volumeData(:,:,i,ii));
        
        vol_re = reshape(volumeData,193600,1)

        vol_re( all(~vol_re,2), : ) = [];
        vol_re( :, all(~vol_re,1) ) = [];

        aif = max(vol_re,[],'all');

        Aif =[Aif,aif];
        
        for m=1:x
            for n=1:y
               if(volumeData(m,n)== aif)
                    X_aif=[X_aif,m];
                    Y_aif=[Y_aif,n];
               end
            end
        end
       

    end

    Aif_main(:,ii) = [Aif];
    
    X_aif_main(:,ii)=[X_aif];
    Y_aif_main(:,ii)=[Y_aif];
end
%%
%Plot the AIF estimated from different brain slices
%semi-log axis
for i=1:10
    figure(90);semilogx(T_kp,Aif_main(i,:));legend('slice 1', 'slice 2',  'slice 3',  'slice 4',  'slice 5',  'slice 6',  'slice 7',  'slice 8',  'slice 9',  'slice 10');
    hold on
end
%log-log axis
for i=1:10
    figure(91);loglog(T_kp,Aif_main(i,:));legend('slice 1', 'slice 2',  'slice 3',  'slice 4',  'slice 5',  'slice 6',  'slice 7',  'slice 8',  'slice 9',  'slice 10');
    hold on
end

%%
%Estimate AIF from heart for vaidation
%aorta
Aif_heart_3=[];

for i=1:21
    px_i=dynamic_ts_frames(:,:,380,i);
    px=imcrop(px_i,[250,238,9,9]);
    px_v=mean(px(:));
    Aif_heart_3=[Aif_heart_3,px_v];
end

figure(121);plot(T_kp,Aif_heart_3);
hold on

%%

Aif_mean=[];

for i=1:21 
    A1_m = mean(Aif_main(:,i));
    Aif_mean=[Aif_mean,(A1_m)];
end


figure(121);plot(T_kp,Aif_mean);
hold on
%
figure(121);plot(T_kp, Aif_heart_3);
%%
%Calculate the mean and std dev from the tissue time activity curve of
%brain
TAC_gm = [];
TAC_st_g=[];
for ii=1:21
    volumeData_g = dynamic_ts_kp;
    volumeData_g = squeeze(volumeData_g(:,:,67,ii));
    
    px_g=imcrop(volumeData_g,[237,224,25,25]);
    px_m=mean(px_g(:));
    std_g=std(px_g(:));
    TAC_gm =[TAC_gm,px_m];
    TAC_st_g=[TAC_st_g,std_g];
    
end

figure(121);plot(T_kp, TAC_gm);
hold on

%Normalization
Aif_norm_mean = round(Aif_mean./max(Aif_mean),2);
TAC_norm_mean= round(TAC_gm./max(Aif_mean),2);
TAC_norm_std=round(TAC_st_g./max(Aif_mean),2);

%Tabulate and save as CSV
Norm_table=table(Aif_norm_mean',TAC_norm_mean', TAC_norm_std');

%% mu_surr is obtain from the TF package 
mu_surr=[];
%
mu_surr=round(mu_surr,2);
%
mu_surr=mu_surr.*max(Aif_mean);
mu_surr=mu_surr';
%%
%Plot and comapre the IDIF's

figure(145);semilogx(T_kp,Aif_heart_3);
hold on
figure(145);semilogx(T_kp,Aif_mean);
hold on

figure(145);semilogx(T_kp,mu_surr);legend('IDIF-DA', 'IDIF-Mean ','IDIF-Brain');

%%
%Compute AUC
AUC_Aorta = round(trapz(Aif_heart_3));
AUC_Mean = round(trapz(Aif_mean));
AUC_Estimated = round(trapz(mu_surr));

AUC_table=table(AUC_HM,AUC_Aorta, AUC_Mean, AUC_Estimated);

%
Aif_heart_hm=Aif_heart_2(1:21);
Aif_heart_ar=Aif_heart_3(1:21);

Table_curves=table(T_kp',Aif_heart_hm',Aif_heart_ar',mu_surr',TAC_gm');


           