load('fig7.mat')

% note on tumours.. baseline ADDITIVE intensity is 0.3, and -ve change is
% 0.15, with +ve change 0.45
% Radii are: baseline 6mm, -ve change is 4.5mm, +ve is 7.5mm

fontSize = 20;
mrkers = {'s','v','d'};
markSize = 11;
linColors = {[0,0.5,0];
    [0.7,0,0];
    [0,0,0];
    [0.7,0,0.7];
    [0,0,0.7]
    [0.45,0.45,0.45]};

legText = {'PET1 ML-EM';
    'PET1 TV-OSL-SLR';
    'PET1 ML-EM double counts';
    'PET2 ML-EM';
    'PET2 TV-OSL-SLR'
    'PET2 ML-EM double counts'};

faceTint = 0.6;

%% analyse the regions to get the mean and cv values for all noise
% realisations

% tumour means
for ss = 1:3
    for ii = 1:3
        % get all means in the region for all it and noise
        tumrMeansMlem{ss,ii}{1} = mean(tumourPixelVals_MLEM{ss,ii}{1}(:,:,:),1);
        tumrMeansMlem{ss,ii}{2} = mean(tumourPixelVals_MLEM{ss,ii}{2}(:,:,:),1);
        
        % take only last iteration
        tumrMeansEndItMlem{ss,ii}{1} = squeeze(tumrMeansMlem{ss,ii}{1}(:,end,:));
        tumrMeansEndItMlem{ss,ii}{2} = squeeze(tumrMeansMlem{ss,ii}{2}(:,end,:));
        
        % repeat for SLR
        tumrMeansSlr{ss,ii}{1} = mean(tumourPixelVals_TVMAPSLR{ss,ii}{1}(:,:,:),1);
        tumrMeansSlr{ss,ii}{2} = mean(tumourPixelVals_TVMAPSLR{ss,ii}{2}(:,:,:),1);
        
        tumrMeansEndItSlr{ss,ii}{1} = squeeze(tumrMeansSlr{ss,ii}{1}(:,end,:));
        tumrMeansEndItSlr{ss,ii}{2} = squeeze(tumrMeansSlr{ss,ii}{2}(:,end,:));
        
        % and MLEM double
        tumrMeansMlemDouble{ss,ii}{1} = mean(tumourPixelVals_MLEM_double{ss,ii}{1}(:,:,:),1);
        tumrMeansMlemDouble{ss,ii}{2} = mean(tumourPixelVals_MLEM_double{ss,ii}{2}(:,:,:),1);
        
        tumrMeansEndItMlemDouble{ss,ii}{1} = squeeze(tumrMeansMlemDouble{ss,ii}{1}(:,end,:));
        tumrMeansEndItMlemDouble{ss,ii}{2} = squeeze(tumrMeansMlemDouble{ss,ii}{2}(:,end,:));
    end
end

% white matter CVs
for ss = 1:3
    for ii = 1:3
        % get all CVs in the region for all iterations and noise
        wmCvsMlem{ss,ii}{1} = std(whiteMatterPixelVals_MLEM{ss,ii}{1}(:,:,:),1)./mean(whiteMatterPixelVals_MLEM{ss,ii}{1}(:,:,:),1);
        wmCvsMlem{ss,ii}{2} = std(whiteMatterPixelVals_MLEM{ss,ii}{2}(:,:,:),1)./mean(whiteMatterPixelVals_MLEM{ss,ii}{2}(:,:,:),1);
        
        % take just last iteration
        wmCvsEndItMlem{ss,ii}{1} = squeeze(wmCvsMlem{ss,ii}{1}(:,end,:));
        wmCvsEndItMlem{ss,ii}{2} = squeeze(wmCvsMlem{ss,ii}{2}(:,end,:));

        % repeat for TV-MAP-SLR
        wmCvsSlr{ss,ii}{1} = std(whiteMatterPixelVals_TVMAPSLR{ss,ii}{1}(:,:,:),1)./mean(whiteMatterPixelVals_TVMAPSLR{ss,ii}{1}(:,:,:),1);
        wmCvsSlr{ss,ii}{2} = std(whiteMatterPixelVals_TVMAPSLR{ss,ii}{2}(:,:,:),1)./mean(whiteMatterPixelVals_TVMAPSLR{ss,ii}{2}(:,:,:),1);
        
        wmCvsEndItSlr{ss,ii}{1} = squeeze(wmCvsSlr{ss,ii}{1}(:,end,:));
        wmCvsEndItSlr{ss,ii}{2} = squeeze(wmCvsSlr{ss,ii}{2}(:,end,:));
        
        % and MLEM double counts
        wmCvsMlemDouble{ss,ii}{1} = std(whiteMatterPixelVals_MLEM_double{ss,ii}{1}(:,:,:),1)./mean(whiteMatterPixelVals_MLEM_double{ss,ii}{1}(:,:,:),1);
        wmCvsMlemDouble{ss,ii}{2} = std(whiteMatterPixelVals_MLEM_double{ss,ii}{2}(:,:,:),1)./mean(whiteMatterPixelVals_MLEM_double{ss,ii}{2}(:,:,:),1);
        
        wmCvsEndItMlemDouble{ss,ii}{1} = squeeze(wmCvsMlemDouble{ss,ii}{1}(:,end,:));
        wmCvsEndItMlemDouble{ss,ii}{2} = squeeze(wmCvsMlemDouble{ss,ii}{2}(:,end,:));
    end
end

%% then plot
figure
for ss = 1:3
    for jj = 1:3
        
        ii = 4-jj; % reverse plots so intensity increases upwards..
        
        subplot(3,3,sub2ind([3,3],ss,jj))
        
        % PET1
        % plot MLEM result
        h1 = plot(mean(tumrMeansEndItMlem{ss,ii}{1}),mean(wmCvsEndItMlem{ss,ii}{1}),...
            mrkers{1});
        hold on
        % and adjust
        h1.MarkerSize=markSize;
        h1.Color = linColors{1};
        h1.MarkerFaceColor = linColors{1}+(1-linColors{1}).*faceTint;
        
        % plot SLR result
        h2 = plot(mean(tumrMeansEndItSlr{ss,ii}{1}),mean(wmCvsEndItSlr{ss,ii}{1}),...
            mrkers{2});
        hold on
        % and adjust
        h2.MarkerSize=markSize;
        h2.Color = linColors{2};
        h2.MarkerFaceColor = linColors{2}+(1-linColors{2}).*faceTint;
        
        % and MLEM double
        h3 = plot(0.5*mean(tumrMeansEndItMlemDouble{ss,ii}{1}),mean(wmCvsEndItMlemDouble{ss,ii}{1}),mrkers{3},'color',linColors{3});
        % and adjust
        h3.MarkerSize=markSize;
        hold on
        h3.MarkerFaceColor = linColors{3}+(1-linColors{3}).*faceTint;
        
        % PET2
        % plot MLEM result
        h4 = plot(mean(tumrMeansEndItMlem{ss,ii}{2}),mean(wmCvsEndItMlem{ss,ii}{2}),...
            mrkers{1});
        hold on
        % and adjust
        h4.MarkerSize=markSize;
        h4.Color = linColors{4};
        h4.MarkerFaceColor = linColors{4}+(1-linColors{4}).*faceTint;
        
        % plot SLR result
        h5 = plot(mean(tumrMeansEndItSlr{ss,ii}{2}),mean(wmCvsEndItSlr{ss,ii}{2}),...
            mrkers{2});
        hold on
        % and adjust
        h5.MarkerSize=markSize;
        h5.Color = linColors{5};
        h5.MarkerFaceColor = linColors{5}+(1-linColors{5}).*faceTint;
        
        % and MLEM double
        h6 = plot(0.5*mean(tumrMeansEndItMlemDouble{ss,ii}{2}),mean(wmCvsEndItMlemDouble{ss,ii}{2}),mrkers{3},'color',linColors{6});
        h6.MarkerSize=markSize;
        h6.MarkerFaceColor = linColors{6}+(1-linColors{6}).*faceTint;
        
        % plot the 'crosshairs' too, 
        xl = xlim;
        yl = ylim;
        plot([-100,100],[mean(wmCvsEndItMlemDouble{ss,ii}{1}),mean(wmCvsEndItMlemDouble{ss,ii}{1})],'--','color',linColors{3});
        plot([0.5*mean(tumrMeansEndItMlemDouble{ss,ii}{1}),0.5*mean(tumrMeansEndItMlemDouble{ss,ii}{1})],[-100,100],'--','color',linColors{3});
        xlim(xl),ylim(yl)
        xl = xlim;
        yl = ylim;
        plot([-100,100],[mean(wmCvsEndItMlemDouble{ss,ii}{2}),mean(wmCvsEndItMlemDouble{ss,ii}{2})],'--','color',linColors{6});
        plot([0.5*mean(tumrMeansEndItMlemDouble{ss,ii}{2}),0.5*mean(tumrMeansEndItMlemDouble{ss,ii}{2})],[-100,100],'--','color',linColors{6});
        % and adjust
        
        xlabel('Tumour Mean')
        ylabel('White matter CV')
        
    end
end

set(findall(gcf,'-property','FontSize'),'FontSize',0.7*fontSize)

%% separate figure for the legend
figure

% PET1
% plot MLEM result
h1 = plot(mean(tumrMeansEndItMlem{ss,ii}{1}),mean(wmCvsEndItMlem{ss,ii}{1}),...
    mrkers{1});
hold on
% and adjust
h1.MarkerSize=markSize;
h1.Color = linColors{1};
h1.MarkerFaceColor = linColors{1}+(1-linColors{1}).*faceTint;

% plot SLR result
h2 = plot(mean(tumrMeansEndItSlr{ss,ii}{1}),mean(wmCvsEndItSlr{ss,ii}{1}),...
    mrkers{2});
hold on
% and adjust
h2.MarkerSize=markSize;
h2.Color = linColors{2};
h2.MarkerFaceColor = linColors{2}+(1-linColors{2}).*faceTint;

% and MLEM double
h3 = plot(0.5*mean(tumrMeansEndItMlemDouble{ss,ii}{1}),mean(wmCvsEndItMlemDouble{ss,ii}{1}),mrkers{3},'color',linColors{3});
% and adjust
h3.MarkerSize=markSize;
hold on
h3.MarkerFaceColor = linColors{3}+(1-linColors{3}).*faceTint;

% PET2
% plot MLEM result
h4 = plot(mean(tumrMeansEndItMlem{ss,ii}{2}),mean(wmCvsEndItMlem{ss,ii}{2}),...
    mrkers{1});
hold on
% and adjust
h4.MarkerSize=markSize;
h4.Color = linColors{4};
h4.MarkerFaceColor = linColors{4}+(1-linColors{4}).*faceTint;

% plot SLR result
h5 = plot(mean(tumrMeansEndItSlr{ss,ii}{2}),mean(wmCvsEndItSlr{ss,ii}{2}),...
    mrkers{2});
hold on
% and adjust
h5.MarkerSize=markSize;
h5.Color = linColors{5};
h5.MarkerFaceColor = linColors{5}+(1-linColors{5}).*faceTint;

% and MLEM double
h6 = plot(0.5*mean(tumrMeansEndItMlemDouble{ss,ii}{2}),mean(wmCvsEndItMlemDouble{ss,ii}{2}),mrkers{3},'color',linColors{6});
h6.MarkerSize=markSize;
h6.MarkerFaceColor = linColors{6}+(1-linColors{6}).*faceTint;

% plot the 'crosshairs' too
xl = xlim;
yl = ylim;
plot([-100,100],[mean(wmCvsEndItMlemDouble{ss,ii}{1}),mean(wmCvsEndItMlemDouble{ss,ii}{1})],'--','color',linColors{3});
plot([0.5*mean(tumrMeansEndItMlemDouble{ss,ii}{1}),0.5*mean(tumrMeansEndItMlemDouble{ss,ii}{1})],[-100,100],'--','color',linColors{3});
xl = xlim;
yl = ylim;
plot([-100,100],[mean(wmCvsEndItMlemDouble{ss,ii}{2}),mean(wmCvsEndItMlemDouble{ss,ii}{2})],'--','color',linColors{6});
plot([0.5*mean(tumrMeansEndItMlemDouble{ss,ii}{2}),0.5*mean(tumrMeansEndItMlemDouble{ss,ii}{2})],[-100,100],'--','color',linColors{6});
% and adjust

xlabel('Tumour Mean')
ylabel('White matter CV')


legend([h1,h2,h3,h4,h5,h6],...
    legText,...
    'location','westoutside');

set(gca,'visible','off')
set(get(gca,'children'),'visible','off')
set(findall(gca,'-property','FontSize'),'FontSize',fontSize)
