clear;
load('flags_combined.mat');
load('suv_percentilesSLOthenUWM.mat');
load('normal_range.mat');


%Remove patient ae8 bc he never got a PET2 scan
%Remove non melanoma pats from MSN
%Remove 5345_11 (ind=13) for bad timing
ind = [1,2,3,4,5,6,7,8,9,10,11,12,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,34,35,36,37,39,40,41,42,43,45,46,47,48,49,50,52,54,57,58,60,63,64,65,66,67,68,71];
bowel_SUVperc_COMBINED = bowel_SUVperc_COMBINED(ind, :,:);
lung_SUVperc_COMBINED = lung_SUVperc_COMBINED(ind, :,:);
thyroid_SUVperc_COMBINED = thyroid_SUVperc_COMBINED(ind, :,:);
days = days(ind, :);
flags = flags(ind,:);
patients = patients(ind);

thyroid_ind = [1:16,18:22, 24:31, 33:37, 39:49, 52:63, 65:70]; %TODO
thyroid_ind = [1:58]; %keep all
% 
% pet = load_nii('Z:\_Data\IRAE\ae9\ae9_1\Processed\ae9_1_PET_notCropped_2mmVoxel.nii.gz');
% mask = load_nii('Z:\_Data\DLorganseg\deepmedic\output\IRAE\predictions\FS_IRAE_train60_QTII_Labelmasks5\predictions\pred_ae9_1_Segm.nii.gz');
% thyroid_SUVperc_COMBINED(36,1,75) = prctile(pet.img(mask.img==4),75); %manual fix for ae9_1 thyroid
% 
% pet = load_nii('Z:\_Data\IRAE\ae15\ae15_5\Processed\ae15_5_PET_notCropped_2mmVoxel.nii.gz');
% mask = load_nii('Z:\_Data\DLorganseg\deepmedic\output\IRAE\predictions\FS_IRAE_train60_QTII_Labelmasks5\predictions\pred_ae15_5_Segm.nii.gz');
% thyroid_SUVperc_COMBINED(41,5,75) = prctile(pet.img(mask.img==4),75); %manual fix for ae15_5 thyroid



clear ind


% Number of scans weirdness 16 vs 17 - not an error. For patient with most
% scans (ae37) baseline scan is scan2.

%% FINAL PLOTS (4)
version = 'v4'; %appended to figure names
% Plot Boxplots rawMAXSUVOPT _ FINAL 

figure('pos',[100,100, 1200,400],'Color','w');
percentile_grid = [5:5:100];

for i=1:3
    switch i
        case 1
            suv_perc = bowel_SUVperc_COMBINED;
            organ = 'Bowel';
            irae_flag = flags(:,2);            
            baseline_ind = flags(:,1);
            irae_days = flags(:,3);
            suvopt = 95;
            scan_days = days;
        case 2
            suv_perc = lung_SUVperc_COMBINED;
            organ = 'Lung';
            irae_flag = flags(:,4);
            baseline_ind = flags(:,1);
            irae_days = flags(:,5);
            suvopt = 95;
            scan_days = days;
        case 3
            suv_perc = thyroid_SUVperc_COMBINED(thyroid_ind,:,:);
            organ = 'Thyroid';
            irae_flag = flags(thyroid_ind,6);
            baseline_ind = flags(thyroid_ind,1);
            irae_days = flags(thyroid_ind,7);
            suvopt = 75;
            scan_days = days(thyroid_ind,:);
    end
    clear organ_MAX
    for j=1:size(scan_days,1);
        n_scans = sum(days(j,:)~=0);
        % Take MAX over time points
        organ_MAX(j,:) = squeeze(max(suv_perc(j,[baseline_ind(j)+1:baseline_ind(j)+n_scans-1],:),[],2));
    end
    
    % Compute ROC stats for every SUV percentile 1-100
    for j=1:numel(percentile_grid)
        [~,~,~,temp_AUC,~] = perfcurve(irae_flag,organ_MAX(:,percentile_grid(j)),1);
        AUC_organ(j) = temp_AUC;
    end
    
    [maxAUC_organ, ImaxAUC_organ] = max(AUC_organ);
    p = ranksum(organ_MAX(find(~irae_flag),percentile_grid(ImaxAUC_organ)),organ_MAX(find(irae_flag),percentile_grid(ImaxAUC_organ)))
    % Plot: AUC vs SUV percentile
    subplot(1,3,i)
    h = boxplot(organ_MAX(:,percentile_grid(ImaxAUC_organ)), irae_flag, ...
        'Labels',{['NC (n=' num2str(sum(irae_flag==0)) ')'],['AE (n=' num2str(sum(irae_flag==1)) ')']},...
        'Symbol','o');
    set(h,{'linew','Color'},{1.5,[0.00,0.45,0.74]})
    h = findobj(gcf,'tag','Outliers');
    for iH = 1:length(h)
        h(iH).MarkerEdgeColor = 'k';
        h(iH).MarkerSize = 4;
    end
    set(gca,'XTickLabel',{['NC (n=' num2str(sum(irae_flag==0)) ')'],['AE (n=' num2str(sum(irae_flag==1)) ')']},'FontSize',12)
    hold on;
    set(gca,'YGrid','on');
    ylabel(['MAX (SUV_{' num2str(suvopt) '%}) [g/mL]'],'FontSize',14);
    ylim([0, 10]);
    %text(1.5, 7.4, ['SUV_{OPT%}=' num2str(percentile_grid(ImaxAUC_organ)) '%'],'FontSize',12,'HorizontalAlignment','center');
    text(1.5, 8.4, ['p = ' sprintf('%.4f',p)],'FontSize',12,'HorizontalAlignment','center');
    
    title(organ, 'FontSize',16,'FontName','Arial');
end
export_fig(['C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\boxplots_rawMAXSUVOPT_FINAL_' version '.png']);




% PLOT SUV95 vs TIME with markers for CLINICAL DX of IRAE (Frelau Fig 3) _ FINAL
figure('pos',[100,100,700,900],'Color','w');
set(0,'DefaultAxesTitleFontWeight','normal');

for i=1:3
    switch i
        case 1
            suv_perc = bowel_SUVperc_COMBINED;
            organ = 'Bowel';
            irae_flag = flags(:,2);
            irae_days = flags(:,3);
            suvopt = 95;
            scan_days = days;
            my_ylim = [0,10];
        case 2
            suv_perc = lung_SUVperc_COMBINED;
            organ = 'Lung';
            irae_flag = flags(:,4);
            irae_days = flags(:,5);
            suvopt = 95;
            scan_days = days;
            my_ylim = [0,5];
        case 3
            suv_perc = thyroid_SUVperc_COMBINED(thyroid_ind,:,:);
            organ = 'Thyroid';
            irae_flag = flags(thyroid_ind,6);
            irae_days = flags(thyroid_ind,7);
            suvopt = 75;
            scan_days = days(thyroid_ind,:);
            my_ylim = [0,5];
    end
    disp(['Starting ' organ '...']);

    cc = 0.8.*distinguishable_colors(sum(irae_flag));
    
    subplot(3,1,i);
    hold on; box on;
    cc_counter = 0;
    for j=1:size(scan_days,1);
        n_scans = sum(scan_days(j,:)~=0);
        if irae_flag(j)==1
            cc_counter = cc_counter + 1;
            l = plot(scan_days(j,1:n_scans)./30, suv_perc(j,1:n_scans,suvopt),'-','LineWidth',1.5, 'Color', cc(cc_counter,:),'HandleVisibility','off');
            if irae_days(j)>0 %if we have a date, plot it with a vertical line
                %scatter(irae_days(j)./30, max(suv_perc(j,1:n_scans,suvopt)),'v','MarkerEdgeColor',l.Color,'MarkerFaceColor',l.Color,'HandleVisibility','off');
                plot([irae_days(j)./30 irae_days(j)./30], [my_ylim(1), my_ylim(2)], 'Color', l.Color, 'LineStyle','--','HandleVisibility','off');
            end
        end
    end
    % extra vertical line for 1 pt with 2x Colitis (ae10 @ day 774)
    if i==1
        plot([774./30 774./30], [my_ylim(1), my_ylim(2)], 'Color', 'k', 'LineStyle','--','HandleVisibility','off');
    end
    %dummies for legend
    plot(-500, -100, '-','LineWidth',1.5, 'Color',[0 0 0]);
    plot(-500, -100, '--','Color',[0 0 0]);
    %scatter(-500, -500,'v','MarkerEdgeColor',[0 0 0],'MarkerFaceColor',[0 0 0]);
    patch([-100, 1400, 1400, -100],[norm_95CI(i,1),norm_95CI(i,1),norm_95CI(i,2),norm_95CI(i,2)],[0.6, 0.6, 0.6],'FaceAlpha',0.2);
    legend('AE','Clin dx','95%CI, NC');
    xlim([-6,48]);
    set(gca,'YGrid','on');
    ylabel(['SUV_{' num2str(suvopt) '%} [g/mL]'],'FontSize',14);
    ylim(my_ylim);
    xlabel('Time on ICI [months]','FontSize',12);
    xticks([-6:3:48]);
    title(organ, 'FontSize',16,'FontName','Arial');
end
export_fig(['C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\SUVOPT_vs_time_PATCH_RAW_FINAL_' version '.png'])




% Plot AUCvsSUV rawMAXSUVOPT _ FINAL

figure('pos',[100,100, 1200,400],'Color','w');
set(0,'DefaultAxesTitleFontWeight','normal');

percentile_grid = [5:5:100];

for i=1:3
    switch i
        case 1
            suv_perc = bowel_SUVperc_COMBINED;
            organ = 'Bowel';
            irae_flag = flags(:,2);            
            baseline_ind = flags(:,1);
            irae_days = flags(:,3);
            suvopt = 95;
            scan_days = days;
        case 2
            suv_perc = lung_SUVperc_COMBINED;
            organ = 'Lung';
            irae_flag = flags(:,4);
            baseline_ind = flags(:,1);
            irae_days = flags(:,5);
            suvopt = 95;
            scan_days = days;
        case 3
            suv_perc = thyroid_SUVperc_COMBINED(thyroid_ind,:,:);
            organ = 'Thyroid';
            irae_flag = flags(thyroid_ind,6);
            baseline_ind = flags(thyroid_ind,1);
            irae_days = flags(thyroid_ind,7);
            suvopt = 75;
            scan_days = days(thyroid_ind,:);
    end
    clear organ_MAX

    for j=1:size(scan_days,1);
        n_scans = sum(days(j,:)~=0);
        % Take MAX over time points
        organ_MAX(j,:) = squeeze(max(suv_perc(j,[baseline_ind(j)+1:baseline_ind(j)+n_scans-1],:),[],2));
    end
    
    % Compute ROC stats for every SUV percentile 1-100
    for j=1:numel(percentile_grid)
        [~,~,~,temp_AUC,~] = perfcurve(irae_flag,organ_MAX(:,percentile_grid(j)),1);
        AUC_organ(j) = temp_AUC;
    end
    
    [maxAUC_organ, ImaxAUC_organ] = max(AUC_organ);

    % Plot: AUC vs SUV percentile
    subplot(1,3,i)
    plot(percentile_grid, AUC_organ, 'LineWidth',1.5);
    hold on; grid on;
    plot([0,100], [0.5, 0.5],'k-');
    xlabel('SUV percentile','FontSize',12);
    ylabel('AUROC','FontSize',14);
    ylim([0.4, 1]);
    text(2,0.97, ['SUV_{OPT%} = SUV_{' num2str(percentile_grid(ImaxAUC_organ)) '%}'], 'FontSize',12,'HorizontalAlignment','left')
    title(organ, 'FontSize',16,'FontName','Arial');
end
export_fig(['C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\AUCvsSUV_rawMAXSUVOPT_FINAL_' version '.png']);


% Plot ROC curve @ SUVOPT _ FINAL

figure('pos',[100,100, 1200,400],'Color','w');
set(0,'DefaultAxesTitleFontWeight','normal');

for i=1:3
    switch i
        case 1
            suv_perc = bowel_SUVperc_COMBINED;
            organ = 'Bowel';
            irae_flag = flags(:,2);            
            baseline_ind = flags(:,1);
            irae_days = flags(:,3);
            suvopt = 95;
            scan_days = days;
        case 2
            suv_perc = lung_SUVperc_COMBINED;
            organ = 'Lung';
            irae_flag = flags(:,4);
            baseline_ind = flags(:,1);
            irae_days = flags(:,5);
            suvopt = 95;
            scan_days = days;
        case 3
            suv_perc = thyroid_SUVperc_COMBINED(thyroid_ind,:,:);
            organ = 'Thyroid';
            irae_flag = flags(thyroid_ind,6);
            baseline_ind = flags(thyroid_ind,1);
            irae_days = flags(thyroid_ind,7);
            suvopt = 75;
            scan_days = days(thyroid_ind,:);
    end
    clear organ_MAX

    for j=1:size(scan_days,1);
        n_scans = sum(days(j,:)~=0);
        % Take MAX over time points
        organ_MAX(j,:) = squeeze(max(suv_perc(j,[baseline_ind(j)+1:baseline_ind(j)+n_scans-1],:),[],2));
    end
    
    % Compute ROC stats for every SUV percentile 1-100
    [X,Y,T,temp_AUC] = perfcurve(irae_flag,organ_MAX(:,suvopt),1);
    % Find indice that maximizes Youden's index (Sens + Spec - 1)
    [~,ii] = max((1-X)+Y-1);
    
    % Plot: ROC curve for SUVopt
    subplot(1,3,i)
    plot(1-X, Y, 'LineWidth',1.5);
    hold on; grid on;
    scatter(1-X(ii), Y(ii), 30,'Marker','o','MarkerEdgeColor','k','MarkerFaceColor',[0.00,0.45,0.74], 'LineWidth',1.5);
    plot([0,1], [1, 0],'k-');
    xlabel('Specificity','FontSize',14);
    set(gca, 'XDir','reverse');
    ylabel('Sensitivity','FontSize',14);
    ylim([0, 1]);
    title(organ, 'FontSize',16,'FontName','Arial');
    %text(0.1, 0.3, ['SUV_{OPT%}: SUV_{' num2str(suvopt) '%}'], 'FontSize',12,'HorizontalAlignment','right');
    text(0.1, 0.2, ['AUROC = ' num2str(round(temp_AUC,2))],'FontSize',12,'HorizontalAlignment','right');
    text(0.1, 0.1, ['T_{OPT} = ' num2str(round(T(ii),1)) ' g/mL'],'FontSize',12,'HorizontalAlignment','right');

end
export_fig(['C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\ROC_FINAL_' version '.png']);


%% PT 4982_15 - for Fig 5 - PLOT SUV95 vs TIME with markers for CLINICAL DX of IRAE - 
figure('pos',[100,100, 700,300],'Color','w');
set(0,'DefaultAxesTitleFontWeight','normal');

for i=2
    switch i
        case 1
            suv_perc = bowel_SUVperc_COMBINED;
            organ = 'Bowel';
            irae_flag = flags(:,2);
            irae_days = flags(:,3);
            suvopt = 95;
            scan_days = days;
            my_ylim = [0,10];
        case 2
            suv_perc = lung_SUVperc_COMBINED;
            organ = 'Lung';
            irae_flag = flags(:,4);
            irae_days = flags(:,5);
            suvopt = 95;
            scan_days = days;
            my_ylim = [0,5];
        case 3
            suv_perc = thyroid_SUVperc_COMBINED(thyroid_ind,:,:);
            organ = 'Thyroid';
            irae_flag = flags(thyroid_ind,6);
            irae_days = flags(thyroid_ind,7);
            suvopt = 75;
            scan_days = days(thyroid_ind,:);
            my_ylim = [0,5];
    end
    disp(['Starting ' organ '...']);

    cc = 0.8.*distinguishable_colors(sum(irae_flag));
    
    hold on; grid on; box on;
    cc_counter = 0;
    for j=11
        n_scans = sum(scan_days(j,:)~=0);
        if irae_flag(j)==1
            cc_counter = cc_counter + 1;
            l = plot(scan_days(j,1:n_scans), suv_perc(j,1:n_scans,suvopt),'-','LineWidth',1.5, 'Color', 0.1+((cc_counter-1)./sum(irae_flag)).*[0 0.9 0],'HandleVisibility','off');
            if irae_days(j)>0
                scatter(irae_days(j), max(suv_perc(j,1:n_scans,suvopt)),'v','MarkerEdgeColor',l.Color,'MarkerFaceColor',l.Color,'HandleVisibility','off');
            end
        else
            patchline(scan_days(j,1:n_scans), suv_perc(j,1:n_scans,suvopt),'LineStyle','-','LineWidth',0.5, 'EdgeColor', 'k', 'EdgeAlpha',0.1,'HandleVisibility','off');
        end
    end
    
    %dummies for legend
    plot(-500, -100, '-','LineWidth',1.5, 'Color',[0 0.6 0]);
    scatter(-500, -500,'v','MarkerEdgeColor',[0 0.7 0],'MarkerFaceColor',[0 0.6 0]);
    plot(-500, -500,'LineStyle','-','LineWidth',0.5, 'Color', [0.8 0.8 0.8]);
    patch([-100, 1400, 1400, -100],[norm_95CI(i,1),norm_95CI(i,1),norm_95CI(i,2),norm_95CI(i,2)],[0.6, 0.6, 0.6],'FaceAlpha',0.2);

    legend('AE','Clin dx', 'NC', '95%CI NC');
    xlim([-100,1400]);
    ylabel(['SUV_{' num2str(suvopt) '%} [g/mL]'],'FontSize',14);
    xlabel('Time post tx start [days]','FontSize',12);
    xticks([-100:100:1400]);
    title(organ, 'FontSize',16,'FontName','Arial');
    ylim(my_ylim);

end
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\SUVOPT_vs_time_4982_15_RAW.png')


%% PT ae6 - for Fig 5 - PLOT SUV95 vs TIME with markers for CLINICAL DX of IRAE - 
figure('pos',[100,100, 700,600],'Color','w');
set(0,'DefaultAxesTitleFontWeight','normal');

for i=1:2
    switch i
        case 1
            suv_perc = bowel_SUVperc_COMBINED;
            organ = 'Bowel';
            irae_flag = flags(:,2);
            irae_days = flags(:,3);
            suvopt = 95;
            scan_days = days;
            my_ylim = [0,10];
        case 2
            suv_perc = lung_SUVperc_COMBINED;
            organ = 'Lung';
            irae_flag = flags(:,4);
            irae_days = flags(:,5);
            suvopt = 95;
            scan_days = days;
            my_ylim = [0,5];
        case 3
            suv_perc = thyroid_SUVperc_COMBINED(thyroid_ind,:,:);
            organ = 'Thyroid';
            irae_flag = flags(thyroid_ind,6);
            irae_days = flags(thyroid_ind,7);
            suvopt = 75;
            scan_days = days(thyroid_ind,:);
            my_ylim = [0,5];
    end
    disp(['Starting ' organ '...']);

    cc = 0.8.*distinguishable_colors(sum(irae_flag));
    subplot(2,1,i)
    hold on; grid on; box on;
    cc_counter = 0;
    for j=35
        n_scans = sum(scan_days(j,:)~=0);
        if irae_flag(j)==1
            cc_counter = cc_counter + 1;
            l = plot(scan_days(j,1:n_scans), suv_perc(j,1:n_scans,suvopt),'-','LineWidth',1.5, 'Color', 0.1+((cc_counter-1)./sum(irae_flag)).*[0 0.9 0],'HandleVisibility','off');
            if irae_days(j)>0
                scatter(irae_days(j), max(suv_perc(j,1:n_scans,suvopt)),'v','MarkerEdgeColor',l.Color,'MarkerFaceColor',l.Color,'HandleVisibility','off');
            end
        else
            patchline(scan_days(j,1:n_scans), suv_perc(j,1:n_scans,suvopt),'LineStyle','-','LineWidth',0.5, 'EdgeColor', 'k', 'EdgeAlpha',0.1,'HandleVisibility','off');
        end
    end
    
    %dummies for legend
    plot(-500, -100, '-','LineWidth',1.5, 'Color',[0 0.6 0]);
    scatter(-500, -500,'v','MarkerEdgeColor',[0 0.7 0],'MarkerFaceColor',[0 0.6 0]);
    plot(-500, -500,'LineStyle','-','LineWidth',0.5, 'Color', [0.8 0.8 0.8]);
    patch([-100, 1400, 1400, -100],[norm_95CI(i,1),norm_95CI(i,1),norm_95CI(i,2),norm_95CI(i,2)],[0.6, 0.6, 0.6],'FaceAlpha',0.2);

    legend('AE','Clin dx', 'NC', '95%CI NC');
    xlim([-100,1400]);
    ylabel(['SUV_{' num2str(suvopt) '%} [g/mL]'],'FontSize',14);
    xlabel('Time post tx start [days]','FontSize',12);
    xticks([-100:100:1400]);
    title(organ, 'FontSize',16,'FontName','Arial');
    ylim(my_ylim);
end
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\SUVOPT_vs_time_ae6_RAW.png')


%% PT A022/7339_11 - for Fig 7 - PLOT SUV95 vs TIME with markers for CLINICAL DX of IRAE - 
figure('pos',[100,100, 700,600],'Color','w');
set(0,'DefaultAxesTitleFontWeight','normal');

for i=3
    switch i
        case 1
            suv_perc = bowel_SUVperc_COMBINED;
            organ = 'Bowel';
            irae_flag = flags(:,2);
            irae_days = flags(:,3);
            suvopt = 95;
            scan_days = days;
            my_ylim = [0,10];
        case 2
            suv_perc = lung_SUVperc_COMBINED;
            organ = 'Lung';
            irae_flag = flags(:,4);
            irae_days = flags(:,5);
            suvopt = 95;
            scan_days = days;
            my_ylim = [0,5];
        case 3
            suv_perc = thyroid_SUVperc_COMBINED(thyroid_ind,:,:);
            organ = 'Thyroid';
            irae_flag = flags(thyroid_ind,6);
            irae_days = flags(thyroid_ind,7);
            suvopt = 75;
            scan_days = days(thyroid_ind,:);
            my_ylim = [0,5];
    end
    disp(['Starting ' organ '...']);

    cc = 0.8.*distinguishable_colors(sum(irae_flag));
    subplot(2,1,1)
    hold on; grid on; box on;
    cc_counter = 0;
    for j=23
        n_scans = sum(scan_days(j,:)~=0);
        if irae_flag(j)==1
            cc_counter = cc_counter + 1;
            l = plot(scan_days(j,1:n_scans), suv_perc(j,1:n_scans,suvopt),'-','LineWidth',1.5, 'Color', 0.1+((cc_counter-1)./sum(irae_flag)).*[0 0.9 0],'HandleVisibility','off');
            if irae_days(j)>0
                scatter(irae_days(j), max(suv_perc(j,1:n_scans,suvopt)),'v','MarkerEdgeColor',l.Color,'MarkerFaceColor',l.Color,'HandleVisibility','off');
            end
        else
            patchline(scan_days(j,1:n_scans), suv_perc(j,1:n_scans,suvopt),'LineStyle','-','LineWidth',0.5, 'EdgeColor', 'k', 'EdgeAlpha',0.1,'HandleVisibility','off');
        end
    end
    
    %dummies for legend
    plot(-500, -100, '-','LineWidth',1.5, 'Color',[0 0.6 0]);
    scatter(-500, -500,'v','MarkerEdgeColor',[0 0.7 0],'MarkerFaceColor',[0 0.6 0]);
    plot(-500, -500,'LineStyle','-','LineWidth',0.5, 'Color', [0.8 0.8 0.8]);
    patch([-100, 1400, 1400, -100],[norm_95CI(i,1),norm_95CI(i,1),norm_95CI(i,2),norm_95CI(i,2)],[0.6, 0.6, 0.6],'FaceAlpha',0.2);

    legend('AE','Clin dx', 'NC', '95%CI NC');
    xlim([-100,1400]);
    ylabel(['SUV_{' num2str(suvopt) '%} [g/mL]'],'FontSize',14);
    xlabel('Time post tx start [days]','FontSize',12);
    xticks([-100:100:1400]);
    title(organ, 'FontSize',16,'FontName','Arial');
    ylim(my_ylim);
end
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\SUVOPT_vs_time_a022_RAW.png')



%% PLOT SUVOPT vs TIME with markers for CLINICAL DX of IRAE - all pts as line, normalized to PET1 SUVOPT (Frelau fig 3)
figure('pos',[100,100, 700,900],'Color','w');
set(0,'DefaultAxesTitleFontWeight','normal');

for i=1:3
    switch i
        case 1
            suv_perc = bowel_SUVperc_COMBINED;
            organ = 'Bowel';
            irae_flag = flags(:,2);
            irae_days = flags(:,3);
            suvopt = 95;
            scan_days = days;
        case 2
            suv_perc = lung_SUVperc_COMBINED;
            organ = 'Lung';
            irae_flag = flags(:,4);
            irae_days = flags(:,5);
            suvopt = 95;
            scan_days = days;
        case 3
            suv_perc = thyroid_SUVperc_COMBINED(thyroid_ind,:,:);
            organ = 'Thyroid';
            irae_flag = flags(thyroid_ind,6);
            irae_days = flags(thyroid_ind,7);
            suvopt = 75;
            scan_days = days(thyroid_ind,:);
    end
    disp(['Starting ' organ '...']);

    cc = 0.8.*distinguishable_colors(sum(irae_flag));
    
    subplot(3,1,i);
    hold on; grid on; box on;
    cc_counter = 0;
    for j=1:size(scan_days,1);
        n_scans = sum(scan_days(j,:)~=0);
        if irae_flag(j)==1
            cc_counter = cc_counter + 1;
            l = plot(scan_days(j,1:n_scans), 100.*(suv_perc(j,1:n_scans,suvopt)./suv_perc(j,1,suvopt)-1),'-','LineWidth',1.5, 'Color', 0.1+((cc_counter-1)./sum(irae_flag)).*[0 0.9 0],'HandleVisibility','off');
            if irae_days(j)>0
                scatter(irae_days(j), 100.*(max(suv_perc(j,1:n_scans,suvopt))./suv_perc(j,1,suvopt)-1),'v','MarkerEdgeColor',l.Color,'MarkerFaceColor',l.Color,'HandleVisibility','off');
            end
        else
            patchline(scan_days(j,1:n_scans), 100.*(suv_perc(j,1:n_scans,suvopt)./suv_perc(j,1,suvopt)-1),'LineStyle','-','LineWidth',0.5, 'EdgeColor', 'k', 'EdgeAlpha',0.1,'HandleVisibility','off');
        end
    end
    
    %dummies for legend
    plot(-500, -100, '-','LineWidth',1.5, 'Color',[0 0.6 0]);
    scatter(-500, -500,'v','MarkerEdgeColor',[0 0.7 0],'MarkerFaceColor',[0 0.6 0]);
    plot(-500, -500,'LineStyle','-','LineWidth',0.5, 'Color', [0.8 0.8 0.8]);
    %patch([-100, 1400, 1400, -100],[norm_95CI(i,1),norm_95CI(i,1),norm_95CI(i,2),norm_95CI(i,2)],[0.6, 0.6, 0.6],'FaceAlpha',0.2);

    legend('AE','Clin dx', 'NC');
    xlim([-100,1400]);
    ylabel(['\Delta SUV_{' num2str(suvopt) '%} [%]'],'FontSize',14);
    xlabel('Time post tx start [days]','FontSize',12);
    xticks([-100:100:1400]);
    ylim([-100, 250]);
    title(organ, 'FontSize',16,'FontName','Arial');
end
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\SUVOPT_vs_time_ALLPTS_NORM.png')







%% Plot Boxplots dMAXSUVOPT

figure('pos',[100,100, 1200,400],'Color','w');
percentile_grid = [5:5:100];

for i=1:3
    switch i
        case 1
            suv_perc = bowel_SUVperc_COMBINED;
            organ = 'Bowel';
            irae_flag = flags(:,2);
            irae_days = flags(:,3);
        case 2
            suv_perc = lung_SUVperc_COMBINED;
            organ = 'Lung';
            irae_flag = flags(:,4);
            irae_days = flags(:,5);
        case 3
            suv_perc = thyroid_SUVperc_COMBINED;
            organ = 'Thyroid';
            irae_flag = flags(:,6);
            irae_days = flags(:,7);
    end
    
    for j=1:58
        n_scans = sum(days(j,:)~=0);
        d_organ(j,1:n_scans,:) = 100.*(suv_perc(j, flags(j,1)+[0:n_scans-1], :)./suv_perc(j, flags(j,1), :) - 1);

        % Take MAX over time points
        d_organ_MAX(j,:) = squeeze(max(d_organ(j,2:n_scans,:),[],2));
    end
    
    % Compute ROC stats for every SUV percentile 1-100
    for j=1:numel(percentile_grid)
        [~,~,~,temp_AUC,~] = perfcurve(irae_flag,d_organ_MAX(:,percentile_grid(j)),1);
        AUC_organ(j) = temp_AUC;
    end
    
    [maxAUC_organ, ImaxAUC_organ] = max(AUC_organ);

    % Plot: AUC vs SUV percentile
    subplot(1,3,i)
    boxplot(d_organ_MAX(:,percentile_grid(ImaxAUC_organ)), irae_flag, 'Labels',{'NC','AE'});
    hold on; grid on;
    ylabel('MAX (\Delta SUV_{OPT%}) [%]','FontSize',14);
    ylim([-50, 300]);
    text(1.5, 250, ['AUROC=' num2str(round(maxAUC_organ,2))],'HorizontalAlignment','center');
    text(1.5, 220, ['SUV_{OPT%}=' num2str(percentile_grid(ImaxAUC_organ)) '%'] ,'HorizontalAlignment','center');
    title(organ, 'FontSize',16,'FontName','Arial');
end
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\boxplots_dMAX_SUVOPT.png');



%% Plot AUCvsSUV dMAXSUVOPT

figure('pos',[100,100, 1200,400],'Color','w');
percentile_grid = [5:5:100];

for i=1:3
    switch i
        case 1
            suv_perc = bowel_SUVperc_COMBINED;
            organ = 'Bowel';
            irae_flag = flags(:,2);
            irae_days = flags(:,3);
        case 2
            suv_perc = lung_SUVperc_COMBINED;
            organ = 'Lung';
            irae_flag = flags(:,4);
            irae_days = flags(:,5);
        case 3
            suv_perc = thyroid_SUVperc_COMBINED;
            organ = 'Thyroid';
            irae_flag = flags(:,6);
            irae_days = flags(:,7);
    end
    
    for j=1:58
        n_scans = sum(days(j,:)~=0);
        d_organ(j,1:n_scans,:) = 100.*(suv_perc(j, flags(j,1)+[0:n_scans-1], :)./suv_perc(j, flags(j,1), :) - 1);

        % Take MAX over time points
        d_organ_MAX(j,:) = squeeze(max(d_organ(j,2:n_scans,:),[],2));
    end
    
    % Compute ROC stats for every SUV percentile 1-100
    for j=1:numel(percentile_grid)
        [~,~,~,temp_AUC,~] = perfcurve(irae_flag,d_organ_MAX(:,percentile_grid(j)),1);
        AUC_organ(j) = temp_AUC;
    end
    
    [maxAUC_organ, ImaxAUC_organ] = max(AUC_organ);

    % Plot: AUC vs SUV percentile
    subplot(1,3,i)
    plot(percentile_grid, AUC_organ, 'LineWidth',1);
    hold on; grid on;
    plot([0,100], [0.5, 0.5],'k-');
    xlabel('SUV percentile','FontSize',14);
    ylabel('AUC','FontSize',14);
    ylim([0.4, 1]);
    text(2,0.98, ['Optimal %ile: ' num2str(percentile_grid(ImaxAUC_organ))], 'FontSize',12,'HorizontalAlignment','left')
    title(organ, 'FontSize',16,'FontName','Arial');
end
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\AUCvsSUV_dMAXSUVOPT.png');


%% LUNG - USING MAX (N=65 NC vs N=5 AE)
xlims = [-100, 1200]; %for SUV longitudinal plots - in days

% Compute relative lung change for all patients, all percentiles
for i=1:58
    n_scans = sum(days(i,:)~=0);
    d_lung(i,1:n_scans,:) = 100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], :)./lung_SUVperc_COMBINED(i, flags(i,1), :) - 1);
    
    % Take MAX over time points
    d_lung_MAX(i,:) = squeeze(max(d_lung(i,2:n_scans,:),[],2));
end

% Compute ROC stats for every SUV percentile 1-100
for i=1:100
    [~,~,~,temp_AUC,~] = perfcurve(flags(:,4),d_lung_MAX(:,i),1);
    AUC_lung(i) = temp_AUC;
end

% Determine optimal percentile SUVxx that maximizes AUC
[maxAUC_lung, ImaxAUC_lung] = max(AUC_lung);

%redo AUC to get X, Y for optimal SUVxx
[OPT_X_lung,OPT_Y_lung,OPT_T_lung,OPT_AUC_lung] = perfcurve(flags(:,4),d_lung_MAX(:,ImaxAUC_lung),1);

% determine threshold that maximizes sum of sens and spec
t = 1-OPT_X_lung+OPT_Y_lung;
[~,ii] = max(t);

OPT_OPT_T_lung = OPT_T_lung(ii);
OPT_ROCPT_lung = [1-OPT_X_lung(ii), OPT_Y_lung(ii)];

% Compute mean and sd for NC patients for plotting normal range
d_lung_NC = d_lung(flags(:,4)==0,:,:);

mu_lung_NC = squeeze(mean(d_lung_NC(:,2,:), 1));
sigma_lung_NC = squeeze(std(d_lung_NC(:,2,:), 1));

% Plot: AUC vs SUV percentile
figure('pos',[200, 800, 400, 400], 'Color','w');
plot([1:100], AUC_lung, 'LineWidth',2);
hold on; grid on;
plot([0,100], [0.5, 0.5],'k-');
xlabel('SUV percentile','FontSize',14);
ylabel('AUC','FontSize',14);
ylim([0.4, 1]);
text(10,0.9, ['Optimal %ile: ' num2str(ImaxAUC_lung)], 'FontSize',12)
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\lung_AUCvsSUVperc.png');

% Plot: Paired boxplots for SUVxx that produce highest AUC
figure('pos',[700, 800, 300, 400], 'Color','w');
boxplot(d_lung_MAX(:,ImaxAUC_lung), flags(:,4), 'Labels',{'NC (n=65)','AE (n=5)'});
ylabel('MAX(\Delta SUV_{OPT%})','FontSize',14);
grid on;
lines = findobj(gcf, 'type', 'line');
set(lines,'LineWidth',1.5);
hold on;
plot([0,3], [OPT_OPT_T_lung, OPT_OPT_T_lung], 'k-');
p_lung = ranksum(d_lung_MAX(find(~flags(:,4)), ImaxAUC_lung) ,d_lung_MAX(find(flags(:,4)), ImaxAUC_lung));
text(1.5, 180, ['p = 3e-4'],'HorizontalAlignment','center');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\lung_boxplots.png');

% Plot: ROC curve for SUVxx that produced highest AUC
figure('pos',[1100, 800, 400, 400], 'Color','w');
plot(1-OPT_X_lung, OPT_Y_lung,'LineWidth',2);
hold on; grid on;
scatter(OPT_ROCPT_lung(1), OPT_ROCPT_lung(2), 'ro');
set(gca, 'XDir','reverse');
xlabel('Specificity','FontSize',14);
ylabel('Sensitivity','FontSize',14);
text(0.05, 0.25, ['AUC = ' num2str(round(OPT_AUC_lung,3))],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment', 'right');
text(0.05, 0.2, ['Sens = ' num2str(round(OPT_ROCPT_lung(2),2))],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment', 'right');
text(0.05, 0.15, ['Spec = ' num2str(round(OPT_ROCPT_lung(1),2))],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment', 'right');
text(0.05, 0.08, ['T_{opt} = +' num2str(round(OPT_OPT_T_lung,1)) '%'],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment', 'right');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\lung_ROC.png');

% Plot: Longitudinal SUVxx with normal range 
figure('pos',[200, 200, 800, 400], 'Color','w');
xlims = [-100, 1000];
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung)],...
    [0.95 0.95 0.95]);
hold on; grid on;
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_lung_NC(ImaxAUC_lung), mu_lung_NC(ImaxAUC_lung)], 'k-', 'LineWidth',2);
plot([xlims(1), xlims(2)], [mu_lung_NC(ImaxAUC_lung) + 2.*sigma_lung_NC(ImaxAUC_lung), mu_lung_NC(ImaxAUC_lung)+ 2.*sigma_lung_NC(ImaxAUC_lung)], 'k-', 'LineWidth',0.5);
plot([xlims(1), xlims(2)], [mu_lung_NC(ImaxAUC_lung) - 2.*sigma_lung_NC(ImaxAUC_lung), mu_lung_NC(ImaxAUC_lung)- 2.*sigma_lung_NC(ImaxAUC_lung)], 'k-', 'LineWidth',0.5);

text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)+2.*sigma_lung_NC(ImaxAUC_lung), '+2\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)-2.*sigma_lung_NC(ImaxAUC_lung), '-2\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

for i=1:58
    if flags(i,4) == 1;
        n_scans = sum(days(i,:)~=0);
        if i<=30
            plot(days(i, 1:n_scans), ...
                100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1), ...
                ':o',...
                'LineWidth',2,...
                'Color','k');
        else
            plot(days(i, 1:n_scans), ...
                100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1), ...
                '-.o',...
                'LineWidth',2,...
                'Color','r');
        end
        hold on; grid on;
    end
end
xlim([xlims(1) xlims(2)]);
ylim([-50, 220]);
box on;
ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\lung_SUVLong_CI.png');

% Plot: Longitudinal SUVxx with optimal threshold
figure('pos',[1200, 200, 800, 400], 'Color','w');
xlims = [-100, 1000];
hold on; grid on;
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [OPT_OPT_T_lung, OPT_OPT_T_lung], '-','Color','k', 'LineWidth',2);

for i=1:58
    if flags(i,4) == 1;
        n_scans = sum(days(i,:)~=0);
        if i<=30
            plot(days(i, 1:n_scans), ...
                100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1), ...
                ':o',...
                'LineWidth',2,...
                'Color','k');
        else
            plot(days(i, 1:n_scans), ...
                100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1), ...
                '-.o',...
                'LineWidth',2,...
                'Color','r');
        end
        hold on; grid on;
    end
end
xlim([xlims(1) xlims(2)]);
ylim([-50, 220]);
box on;
ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\lung_SUVLong_TOPT.png');

% PLOT - logistic regression for % prob AE
[log_reg_lung, gof_lung, output_lung] = fit(d_lung_MAX(:,ImaxAUC_lung), flags(:,4),'1./(1+exp(-k.*(x-x0)))','start',[1 50]);

log_reg_lung
figure('pos',[100,100, 500, 200], 'Color','w');
scatter(d_lung_MAX(:,ImaxAUC_lung), flags(:,4));
hold on; box on; grid on;
plot(log_reg_lung, d_lung_MAX(:,ImaxAUC_lung), flags(:,4));
xticks([-50:25:225]);
xlim([-50, 225]);
yticks([0:0.2:1]);
xlabel('MAX (\Delta SUV_{OPT%}) [%]','FontSize',14);
ylabel('AE (y/n)','FontSize',14);
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\lung_Logreg.png');

%% PLOT - individual SUVopt vs time with tx and dx start/stops
% pat 608/13
i=18;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-50, 240];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_lung_NC(ImaxAUC_lung), mu_lung_NC(ImaxAUC_lung)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 122 122 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

%irAE diagnosis
plot([115 115], [ylims(1), ylims(2)], 'r:','LineWidth',2);


% Imm Supp tx patch GREEN
tx = patch([136 198 198 136], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% Death
plot([198 198], [ylims(1), ylims(2)], 'r-','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('pneumonitisG2 608\_13');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\pneumonitis_608_13.png');


% pat 4982/15
i=11;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-50, 1200];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_lung_NC(ImaxAUC_lung), mu_lung_NC(ImaxAUC_lung)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 299 299 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([376 xlims(2) xlims(2) 376], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

%irAE diagnosis
plot([245 245], [ylims(1), ylims(2)], 'r:','LineWidth',2);


% Imm Supp tx patch GREEN
tx = patch([309 xlims(2) xlims(2) 309], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('pneumonitisG2 4982\_15');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\pneumonitis_4982_15.png');



% pat 112_14
i=1;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-50, 280];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_lung_NC(ImaxAUC_lung), mu_lung_NC(ImaxAUC_lung)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 201 201 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([232 232], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([201 xlims(2) xlims(2) 201], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('pneumonitisG4 112\_14');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\pneumonitis_112_14.png');


% pat 2278_07
i=5;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-50, 1500];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_lung_NC(ImaxAUC_lung), mu_lung_NC(ImaxAUC_lung)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 218 218 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([191 191], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([225 830 830 225], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% ICI tx patch BLUE
tx = patch([560 590 590 560], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([590 590], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% ICI tx patch BLUE
tx = patch([1447 xlims(2) xlims(2) 1447], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('pneumonitisG2 2279\_07');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\pneumonitis_2278_07.png');


% pat AE6
i=36;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-20, 450];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_lung_NC(ImaxAUC_lung), mu_lung_NC(ImaxAUC_lung)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 14 14 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([98 140 140 98], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([273 273], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([14 80 80 14], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([154 xlims(2) xlims(2) 154], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('pneumonitis ae6');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\pneumonitis_ae6.png');



%% PLOT - individual SUVopt vs time with tx and dx start/stops
% pat 608/13
i=18;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-80, 1600];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_lung_NC(ImaxAUC_lung), mu_lung_NC(ImaxAUC_lung)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 122 122 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

%irAE diagnosis
plot([115 115], [ylims(1), ylims(2)], 'r:','LineWidth',2);


% Imm Supp tx patch GREEN
tx = patch([136 198 198 136], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% Death
plot([198 198], [ylims(1), ylims(2)], 'r-','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('pneumonitisG2 608\_13');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\pneumonitis_608_13_fixedtime.png');


% pat 4982/15
i=11;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-80, 1600];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_lung_NC(ImaxAUC_lung), mu_lung_NC(ImaxAUC_lung)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 299 299 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([376 xlims(2) xlims(2) 376], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

%irAE diagnosis
plot([245 245], [ylims(1), ylims(2)], 'r:','LineWidth',2);


% Imm Supp tx patch GREEN
tx = patch([309 xlims(2) xlims(2) 309], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('pneumonitisG2 4982\_15');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\pneumonitis_4982_15_fixedtime.png');



% pat 112_14
i=1;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-80, 1600];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_lung_NC(ImaxAUC_lung), mu_lung_NC(ImaxAUC_lung)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 201 201 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([232 232], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([201 xlims(2) xlims(2) 201], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('pneumonitisG4 112\_14');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\pneumonitis_112_14_fixedtime.png');


% pat 2278_07
i=5;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-80, 1600];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_lung_NC(ImaxAUC_lung), mu_lung_NC(ImaxAUC_lung)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 218 218 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([191 191], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([225 830 830 225], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% ICI tx patch BLUE
tx = patch([560 590 590 560], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([590 590], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% ICI tx patch BLUE
tx = patch([1447 xlims(2) xlims(2) 1447], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('pneumonitisG2 2279\_07');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\pneumonitis_2278_07_fixedtime.png');


% pat AE6
i=36;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-80, 1600];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung) mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)+sigma_lung_NC(ImaxAUC_lung), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_lung_NC(ImaxAUC_lung)-sigma_lung_NC(ImaxAUC_lung), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_lung_NC(ImaxAUC_lung), mu_lung_NC(ImaxAUC_lung)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(lung_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_lung)./lung_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_lung) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 14 14 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([98 140 140 98], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([273 273], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([14 80 80 14], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([154 xlims(2) xlims(2) 154], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('pneumonitis ae6');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\pneumonitis_ae6_fixedtime.png');




%% BOWEL - USING MAX (N=64 NC vs N=6 AE)
xlims = [-100, 1200]; %for SUV longitudinal plots - in days

% Compute relative bowel change for all patients, all percentiles
for i=1:58
    n_scans = sum(days(i,:)~=0);
    d_bowel(i,1:n_scans,:) = 100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], :)./bowel_SUVperc_COMBINED(i, flags(i,1), :) - 1);
    
    % Take MAX over time points
    d_bowel_MAX(i,:) = squeeze(max(d_bowel(i,2:n_scans,:),[],2));
end

% Compute ROC stats for every SUV percentile 1-100
for i=1:100
    [~,~,~,temp_AUC,~] = perfcurve(flags(:,2),d_bowel_MAX(:,i),1);
    AUC_bowel(i) = temp_AUC;
end

% Determine optimal percentile SUVxx that maximizes AUC
[maxAUC_bowel, ImaxAUC_bowel] = max(AUC_bowel);

%redo AUC to get X, Y for optimal SUVxx
[OPT_X_bowel,OPT_Y_bowel,OPT_T_bowel,OPT_AUC_bowel] = perfcurve(flags(:,2),d_bowel_MAX(:,ImaxAUC_bowel),1);

%pick optimal operating pt by sum of sensitivity and specificity
t = 1-OPT_X_bowel+OPT_Y_bowel;
[~,ii] = max(t);

OPT_OPT_T_bowel = OPT_T_bowel(ii);
OPT_ROCPT_bowel = [1-OPT_X_bowel(ii), OPT_Y_bowel(ii)];

% Compute mean and sd for NC patients for plotting normal range
d_bowel_NC = d_bowel(flags(:,2)==0,:,:);

mu_bowel_NC = squeeze(mean(d_bowel_NC(:,2,:), 1));
sigma_bowel_NC = squeeze(std(d_bowel_NC(:,2,:), 1));

% Plot: AUC vs SUV percentile
figure('pos',[200, 800, 400, 400], 'Color','w');
plot([1:100], AUC_bowel, 'LineWidth',2);
hold on; grid on;
plot([0,100], [0.5, 0.5],'k-');
xlabel('SUV percentile','FontSize',14);
ylabel('AUC','FontSize',14);
ylim([0.4, 1]);
text(10,0.9, ['Optimal %ile: ' num2str(ImaxAUC_bowel)], 'FontSize',12)
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\bowel_AUCvsSUVperc.png');

% Plot: Paired boxplots for SUVxx that produce highest AUC
figure('pos',[700, 800, 300, 400], 'Color','w');
boxplot(d_bowel_MAX(:,ImaxAUC_bowel), flags(:,2), 'Labels',{'NC (n=64)','AE (n=6)'});
ylabel('MAX(\Delta SUV_{OPT%})','FontSize',14);
grid on;
lines = findobj(gcf, 'type', 'line');
set(lines,'LineWidth',1.5);
hold on;
plot([0,3], [OPT_OPT_T_bowel, OPT_OPT_T_bowel], 'k-');
p_bowel = ranksum(d_bowel_MAX(find(~flags(:,2)), ImaxAUC_bowel) ,d_bowel_MAX(find(flags(:,2)), ImaxAUC_bowel));
text(1.5, 180, ['p = 1e-3'],'HorizontalAlignment','center');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\bowel_boxplots.png');

% Plot: ROC curve for SUVxx that produced highest AUC
figure('pos',[1100, 800, 400, 400], 'Color','w');
plot(1-OPT_X_bowel, OPT_Y_bowel,'LineWidth',2);
hold on; grid on;
scatter(OPT_ROCPT_bowel(1), OPT_ROCPT_bowel(2), 'ro');
set(gca, 'XDir','reverse');
xlabel('Specificity','FontSize',14);
ylabel('Sensitivity','FontSize',14);
text(0.05, 0.25, ['AUC = ' num2str(round(OPT_AUC_bowel,3))],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment', 'right');
text(0.05, 0.2, ['Sens = ' num2str(round(OPT_ROCPT_bowel(2),2))],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment', 'right');
text(0.05, 0.15, ['Spec = ' num2str(round(OPT_ROCPT_bowel(1),2))],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment', 'right');
text(0.05, 0.08, ['T_{opt} = +' num2str(round(OPT_OPT_T_bowel,1)) '%'],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment', 'right');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\bowel_ROC.png');

% Plot: Longitudinal SUVxx with normal range 
figure('pos',[200, 200, 800, 400], 'Color','w');
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel)],...
    [0.95 0.95 0.95]);
hold on; grid on;
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',2);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel) + 2.*sigma_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)+ 2.*sigma_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',0.5);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel) - 2.*sigma_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)- 2.*sigma_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',0.5);

text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+2.*sigma_bowel_NC(ImaxAUC_bowel), '+2\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-2.*sigma_bowel_NC(ImaxAUC_bowel), '-2\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

for i=1:58
    if flags(i,2) == 1;
        n_scans = sum(days(i,:)~=0);
        if i<=30
            plot(days(i, 1:n_scans), ...
                100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1), ...
                ':o',...
                'LineWidth',2,...
                'Color','k');
        else
            plot(days(i, 1:n_scans), ...
                100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1), ...
                '-.o',...
                'LineWidth',2,...
                'Color','r');
        end
        hold on; grid on;
    end
end
xlim([xlims(1) xlims(2)]);
ylim([-60, 140]);
box on;
ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\bowel_SUVLong_CI.png');

% Plot: Longitudinal SUVxx with optimal threshold
figure('pos',[1200, 200, 800, 400], 'Color','w');
xlims = [-100, 1000];
hold on; grid on;
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [OPT_OPT_T_bowel, OPT_OPT_T_bowel], '-','Color','k', 'LineWidth',2);

for i=1:58
    if flags(i,2) == 1;
        n_scans = sum(days(i,:)~=0);
        if i<=30
            plot(days(i, 1:n_scans), ...
                100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1), ...
                ':o',...
                'LineWidth',2,...
                'Color','k');
        else
            plot(days(i, 1:n_scans), ...
                100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1), ...
                '-.o',...
                'LineWidth',2,...
                'Color','r');
        end
        hold on; grid on;
    end
end
xlim([xlims(1) xlims(2)]);
ylim([-60, 140]);
box on;
ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\bowel_SUVLong_TOPT.png');

% PLOT - logistic regression for % prob AE
[log_reg_bowel, gof_bowel, output_bowel] = fit(d_bowel_MAX(:,ImaxAUC_bowel), flags(:,2),'1./(1+exp(-k.*(x-x0)))','start',[1 50]);
log_reg_bowel
figure('pos',[100,100, 500, 200], 'Color','w');
scatter(d_bowel_MAX(:,ImaxAUC_bowel), flags(:,2));
hold on; box on; grid on;
plot(log_reg_bowel, d_bowel_MAX(:,ImaxAUC_bowel), flags(:,2));
xticks([-50:50:400]);
yticks([0:0.2:1]);
xlabel('MAX (\Delta SUV_{OPT%}) [%]','FontSize',14);
ylabel('AE (y/n)','FontSize',14);
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\bowel_Logreg.png');


% PLOT - logistic regression for % prob AE
ind = [1,3:10,12:58];
[log_reg_bowel2, gof_bowel2, output_bowel2] = fit(d_bowel_MAX(ind,ImaxAUC_bowel), flags(ind,2),'1./(1+exp(-k.*(x-x0)))','start',[1 50]);

figure('pos',[100,100, 500, 200], 'Color','w');
scatter(d_bowel_MAX(ind,ImaxAUC_bowel), flags(ind,2));
hold on; box on; grid on;
plot(log_reg_bowel2, d_bowel_MAX(ind,ImaxAUC_bowel), flags(ind,2));
xticks([-50:50:400]);
yticks([0:0.2:1]);
xlabel('MAX (\Delta SUV_{OPT%}) [%]','FontSize',14);
ylabel('AE (y/n)','FontSize',14);
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\bowel_Logreg_no_outliers.png');


%% Individual patients with colitis AE

% pat 5490_15
i=15;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-80, 150];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 65 65 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([120 120], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([65 110 100 65], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('colitisG2 5490\_15');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\colitis_5490_15.png');

%% pat 5003_16
i=12;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-80, 320];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 196 196 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([173 173], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([196 238 238 196], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% ICI tx patch BLUE
tx = patch([238 308 308 238], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('colitisG2 5003\_16');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\colitis_5003_16.png');


%% pat ae6
i=36;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-80, 450];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 14 14 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([98 140 140 98], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([195 195], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([14 80 80 14], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([154 xlims(2) xlims(2) 154], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('colitisG3 ae6');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\colitis_ae6.png');



%% pat ae10
i=39;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-80, 1000];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 760 760 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([32 32], [ylims(1), ylims(2)], 'r:','LineWidth',2);
plot([774 774], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('colitisG2 ae10');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\colitis_ae10.png');


%% AE 11
i=40;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-80, 350];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 296 296 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([302 302], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('colitisG3 ae11');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\colitis_ae11.png');




%% AE 17
i=46;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-100, 550];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 155 155 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([231 231], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('colitisG2 ae17');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\colitis_ae17.png');






%% Individual patients, same xlims
% pat 5490_15
i=15;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-100, 1000];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 65 65 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([120 120], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([65 110 100 65], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('colitisG2 5490\_15');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\colitis_5490_15_fixedtime.png');

% pat 5003_16
i=12;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-100, 1000];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 196 196 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([173 173], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([196 238 238 196], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% ICI tx patch BLUE
tx = patch([238 308 308 238], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('colitisG2 5003\_16');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\colitis_5003_16_fixedtime.png');


% pat ae6
i=36;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-100, 1000];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 14 14 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([98 140 140 98], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([195 195], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([14 80 80 14], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([154 xlims(2) xlims(2) 154], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('colitisG3 ae6');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\colitis_ae6_fixedtime.png');



% pat ae10
i=39;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-100, 1000];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 760 760 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([32 32], [ylims(1), ylims(2)], 'r:','LineWidth',2);
plot([774 774], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('colitisG2 ae10');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\colitis_ae10_fixedtime.png');


% AE 11
i=40;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-100, 1000];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 296 296 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([302 302], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('colitisG3 ae11');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\colitis_ae11_fixedtime.png');




% AE 17
i=46;
n_scans = sum(days(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-100, 1000];
ylims = [-40, 220];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel) mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)+sigma_bowel_NC(ImaxAUC_bowel), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_bowel_NC(ImaxAUC_bowel)-sigma_bowel_NC(ImaxAUC_bowel), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_bowel_NC(ImaxAUC_bowel), mu_bowel_NC(ImaxAUC_bowel)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(bowel_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_bowel)./bowel_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_bowel) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 155 155 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([231 231], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('colitisG2 ae17');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\colitis_ae17_fixedtime.png');

















%% thyroid - USING MAX (N=59 NC vs N=11 AE)
xlims = [-350, 1200]; %for SUV longitudinal plots - in days

thyroid_ind = [1:16,18:22, 24:31, 33:37, 39:49, 52:63, 65:70];
thyroid_ind = [1:58];
thyroid_SUVperc_COMBINED = thyroid_SUVperc_COMBINED(thyroid_ind, :, :);
flags_thyroid = flags(thyroid_ind,:);
patients_thyroid = patients(thyroid_ind);
days_thyroid = days(thyroid_ind,:);
% Compute relative thyroid change for all patients, all percentiles
for i=1:numel(thyroid_ind)
    n_scans = sum(days_thyroid(i,:)~=0);
    d_thyroid(i,1:n_scans,:) = 100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], :)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), :) - 1);
    
    % Take MAX over time points
    d_thyroid_MAX(i,:) = squeeze(max(d_thyroid(i,2:n_scans,:),[],2));
end

% Compute ROC stats for every SUV percentile 1-100
for i=1:100
    [~,~,~,temp_AUC,~] = perfcurve(flags_thyroid(:,6),d_thyroid_MAX(:,i),1);
    AUC_thyroid(i) = temp_AUC;
end

% Determine optimal percentile SUVxx that maximizes AUC
[maxAUC_thyroid, ImaxAUC_thyroid] = max(AUC_thyroid);

%redo AUC to get X, Y for optimal SUVxx
[OPT_X_thyroid,OPT_Y_thyroid,OPT_T_thyroid,OPT_AUC_thyroid] = perfcurve(flags_thyroid(:,6),d_thyroid_MAX(:,ImaxAUC_thyroid),1);

t = 1-OPT_X_thyroid+OPT_Y_thyroid;
[~,ii] = max(t);

OPT_OPT_T_thyroid = OPT_T_thyroid(ii);
OPT_ROCPT_thyroid = [1-OPT_X_thyroid(ii), OPT_Y_thyroid(ii)];

% Compute mean and sd for NC patients for plotting normal range
d_thyroid_NC = d_thyroid(flags_thyroid(:,6)==0,:,:);

mu_thyroid_NC = squeeze(mean(d_thyroid_NC(:,2,:), 1));
sigma_thyroid_NC = squeeze(std(d_thyroid_NC(:,2,:), 1));

% Plot: AUC vs SUV percentile
figure('pos',[200, 800, 400, 400], 'Color','w');
plot([1:100], AUC_thyroid, 'LineWidth',2);
hold on; grid on;
plot([0,100], [0.5, 0.5],'k-');
xlabel('SUV percentile','FontSize',14);
ylabel('AUC','FontSize',14);
ylim([0.4, 1]);
text(10,0.9, ['Optimal %ile: ' num2str(ImaxAUC_thyroid)], 'FontSize',12)
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroid_AUCvsSUVperc.png');

% Plot: Paired boxplots for SUVxx that produce highest AUC
figure('pos',[700, 800, 300, 400], 'Color','w');
boxplot(d_thyroid_MAX(:,ImaxAUC_thyroid), flags_thyroid(:,6), 'Labels',{['NC (n=' num2str(sum(flags_thyroid(:,6)==0)) ')'],['AE (n=' num2str(sum(flags_thyroid(:,6)==1)) ')']});
ylabel('MAX(\Delta SUV_{OPT%})','FontSize',14);
grid on;
lines = findobj(gcf, 'type', 'line');
set(lines,'LineWidth',1.5);
hold on;
plot([0,3], [OPT_OPT_T_thyroid, OPT_OPT_T_thyroid], 'k-');
ylim([-60, 250]);
p_thyroid = ranksum(d_thyroid_MAX(find(~flags_thyroid(:,6)), ImaxAUC_thyroid) ,d_thyroid_MAX(find(flags_thyroid(:,6)), ImaxAUC_thyroid));
text(1.5, 180, ['p = 6e-6'],'HorizontalAlignment','center');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroid_boxplots.png');

% Plot: ROC curve for SUVxx that produced highest AUC
figure('pos',[1100, 800, 400, 400], 'Color','w');
plot(1-OPT_X_thyroid, OPT_Y_thyroid,'LineWidth',2);
hold on; grid on;
scatter(OPT_ROCPT_thyroid(1), OPT_ROCPT_thyroid(2), 'ro');
set(gca, 'XDir','reverse');
xlabel('Specificity','FontSize',14);
ylabel('Sensitivity','FontSize',14);
text(0.05, 0.25, ['AUC = ' num2str(round(OPT_AUC_thyroid,3))],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment', 'right');
text(0.05, 0.2, ['Sens = ' num2str(round(OPT_ROCPT_thyroid(2),2))],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment', 'right');
text(0.05, 0.15, ['Spec = ' num2str(round(OPT_ROCPT_thyroid(1),2))],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment', 'right');
text(0.05, 0.08, ['T_{opt} = +' num2str(round(OPT_OPT_T_thyroid,1)) '%'],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment', 'right');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroid_ROC.png');

% Plot: Longitudinal SUVxx with normal range 
figure('pos',[200, 200, 800, 400], 'Color','w');
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
hold on; grid on;
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid) + 2.*sigma_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)+ 2.*sigma_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',0.5);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid) - 2.*sigma_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)- 2.*sigma_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',0.5);

text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+2.*sigma_thyroid_NC(ImaxAUC_thyroid), '+2\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-2.*sigma_thyroid_NC(ImaxAUC_thyroid), '-2\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

for i=1:numel(thyroid_ind)
    if flags_thyroid(i,6) == 1;
        n_scans = sum(days_thyroid(i,:)~=0);
        if i<=28
            plot(days_thyroid(i, 1:n_scans), ...
                100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1), ...
                ':o',...
                'LineWidth',2,...
                'Color','k');
        else
            plot(days_thyroid(i, 1:n_scans), ...
                100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1), ...
                '-.o',...
                'LineWidth',2,...
                'Color','r');
        end
        hold on; grid on;
    end
end
xlim([xlims(1) xlims(2)]);
ylim([-60, 250]);
box on;
ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroid_SUVLong_CI.png');

% Plot: Longitudinal SUVxx with optimal threshold
figure('pos',[1200, 200, 800, 400], 'Color','w');
hold on; grid on;
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [OPT_OPT_T_thyroid, OPT_OPT_T_thyroid], '-','Color','k', 'LineWidth',2);

for i=1:numel(thyroid_ind)
    if flags_thyroid(i,6) == 1;
        n_scans = sum(days_thyroid(i,:)~=0);
        if i<=28
            plot(days_thyroid(i, 1:n_scans), ...
                100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1), ...
                ':o',...
                'LineWidth',2,...
                'Color','k');
        else
            plot(days_thyroid(i, 1:n_scans), ...
                100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1), ...
                '-.o',...
                'LineWidth',2,...
                'Color','r');
        end
        %scatter(flags_thyroid(i,7), d_thyroid_MAX(i, ImaxAUC_thyroid), 'bo','filled');
        hold on; grid on;
    end
end
xlim([xlims(1) xlims(2)]);
ylim([-60, 250]);
box on;
ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroid_SUVLong_TOPT.png');

% PLOT - logistic regression for % prob AE
[log_reg_thyroid, gof_thyroid, output_thyroid] = fit(d_thyroid_MAX(:,ImaxAUC_thyroid), flags_thyroid(:,6),'1./(1+exp(-k.*(x-x0)))','start',[1 50]);
log_reg_thyroid
figure('pos',[100,100, 500, 200],'Color','w');
scatter(d_thyroid_MAX(:,ImaxAUC_thyroid), flags_thyroid(:,6));
hold on; box on; grid on;
plot(log_reg_thyroid, d_thyroid_MAX(:,ImaxAUC_thyroid), flags_thyroid(:,6));
xticks([-50:25:225]);
yticks([0:0.2:1]);
xlabel('MAX (\Delta SUV_{OPT%}) [%]','FontSize',14);
ylabel('AE (y/n)','FontSize',14);
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroid_Logreg.png');


%% Individual patients with thyroid AE
% use days_thyroid, patients_thyroid
%% pat 5345_11
i=13;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-320, 1200];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 xlims(2) xlims(2) 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([225 225], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([108 xlims(2) xlims(2) 108], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;


ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 5345\_11');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_5345_11.png');

%% pat 7339_11
i=22;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-60, 300];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 xlims(2) xlims(2) 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([120 120], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([154 xlims(2) xlims(2) 154], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;


ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 7339\_11');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_7339_11.png');


%% pat 6756_12
i=19;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-60, 1500];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 xlims(2) xlims(2) 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([630 630], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([675 xlims(2) xlims(2) 675], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;


ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 6756\_12');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_6756_12.png');


%% pat 608_13
i=17;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-60, 240];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 122 122 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([115 115], [ylims(1), ylims(2)], 'r:','LineWidth',2);
% Death
plot([198 198], [ylims(1), ylims(2)], 'r-','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([140 198 198 140], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;


ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 608\_13');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_608_13.png');




%% pat 7359_09
i=23;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-80, 1050];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 xlims(2) xlims(2) 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([108 108], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([15 xlims(2) xlims(2) 15], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;


ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 7359\_09');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_7359_09.png');



%% pat 1127_06
i=2;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-140, 720];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 xlims(2) xlims(2) 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([83 83], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([17 xlims(2) xlims(2) 17], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;


ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 1127\_06');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_1127_06.png');


%% pat ae6
i=33;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-50, 550];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 14 14 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([98 140 140 98], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([76 76], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([14 80 80 14], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([154 xlims(2) xlims(2) 154], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 ae6');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_ae6.png');




%% pat ae15
i=40;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-30, 400];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 xlims(2) xlims(2) 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([27 27], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 ae15');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_ae15.png');



%% pat ae20
i=45;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-100, 700];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 357 357 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([28 28], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 ae20');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_ae20.png');


%% pat ae40
i=58;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-50, 300];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 111 111 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
%plot([28 28], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 ae40');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_ae40.png');


%% pat ae41
i=58;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-50, 550];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 217 217 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
%plot([28 28], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 ae41');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_ae41.png');







%% Individual patients with thyroid AE - _fixedtime
% use days_thyroid, patients_thyroid
%% pat 5345_11
i=13;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-300, 1500];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 xlims(2) xlims(2) 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([225 225], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([108 xlims(2) xlims(2) 108], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;


ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 5345\_11');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_5345_11_fixedtime.png');

%% pat 7339_11
i=22;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-300, 1500];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 xlims(2) xlims(2) 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([120 120], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([154 xlims(2) xlims(2) 154], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;


ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 7339\_11');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_7339_11_fixedtime.png');


%% pat 6756_12
i=19;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-300, 1500];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 xlims(2) xlims(2) 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([630 630], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([675 xlims(2) xlims(2) 675], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;


ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 6756\_12');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_6756_12_fixedtime.png');


%% pat 608_13
i=17;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-300, 1500];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 122 122 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([115 115], [ylims(1), ylims(2)], 'r:','LineWidth',2);
% Death
plot([198 198], [ylims(1), ylims(2)], 'r-','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([140 198 198 140], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;


ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 608\_13');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_608_13_fixedtime.png');




%% pat 7359_09
i=23;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-300, 1500];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 xlims(2) xlims(2) 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([108 108], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([15 xlims(2) xlims(2) 15], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;


ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 7359\_09');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_7359_09_fixedtime.png');



%% pat 1127_06
i=2;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-300, 1500];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 xlims(2) xlims(2) 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([83 83], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([17 xlims(2) xlims(2) 17], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;


ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 1127\_06');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_1127_06_fixedtime.png');


%% pat ae6
i=33;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-300, 1500];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 14 14 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([98 140 140 98], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([76 76], [ylims(1), ylims(2)], 'r:','LineWidth',2);

% Imm Supp tx patch GREEN
tx = patch([14 80 80 14], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

tx = patch([154 xlims(2) xlims(2) 154], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0.9, 0]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 ae6');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_ae6_fixedtime.png');




%% pat ae15
i=40;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-300, 1500];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 xlims(2) xlims(2) 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([27 27], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 ae15');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_ae15_fixedtime.png');



%% pat ae20
i=45;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-300, 1500];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 357 357 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
plot([28 28], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 ae20');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_ae20_fixedtime.png');


%% pat ae40
i=58;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-300, 1500];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 111 111 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
%plot([28 28], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 ae40');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_ae40_fixedtime.png');


%% pat ae41
i=58;
n_scans = sum(days_thyroid(i,:)~=0);
figure('pos',[100,100, 500, 400],'Color','w');
xlims = [-300, 1500];
ylims = [-50, 250];
xlim(xlims);
ylim(ylims);
% patch for NC +-sigma
patch([xlims(1) xlims(2) xlims(2) xlims(1)], ...
    [mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid) mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid)],...
    [0.95 0.95 0.95]);
% text for mu, sigma
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid), '\mu','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)+sigma_thyroid_NC(ImaxAUC_thyroid), '+1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');
text(0.98.*xlims(2), mu_thyroid_NC(ImaxAUC_thyroid)-sigma_thyroid_NC(ImaxAUC_thyroid), '-1\sigma','FontSize',12, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'right');

hold on; box on; grid on;
% y axis and mu
plot([xlims(1), xlims(2)], [0, 0], 'k--', 'LineWidth',1);
plot([xlims(1), xlims(2)], [mu_thyroid_NC(ImaxAUC_thyroid), mu_thyroid_NC(ImaxAUC_thyroid)], 'k-', 'LineWidth',2);

% SUVopt
plot(days_thyroid(i, 1:n_scans), 100.*(thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1)+[0:n_scans-1], ImaxAUC_thyroid)./thyroid_SUVperc_COMBINED(i, flags_thyroid(i,1), ImaxAUC_thyroid) - 1),...
    'LineWidth',2,...
    'Marker','o');

% ICI tx patch BLUE
tx = patch([0 217 217 0], [ylims(1) ylims(1) ylims(2) ylims(2)], [0, 0, 0.9]);
tx.FaceAlpha = 0.1;
tx.EdgeAlpha = 0;

% irAE diagnosis
%plot([28 28], [ylims(1), ylims(2)], 'r:','LineWidth',2);

ylabel('change SUV_{OPT%} [%]','FontSize',14);
xlabel('Time [days]','FontSize',14);
title('thyroidG2 ae41');
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroiditis_ae41_fixedtime.png');









%% Normal N=15 subcohort
patient_ids_norm = {'376_17', '1414_15', '1705_16', '4082_15', '4091_14', '4620_17', '5546_16','5706_16', '6368_17', '6799_98', '7037_17',   '7093_08', '7481_15',  '8022_16', '8285_09'};

% PATIENT 5706_16 has to be excluded from normals for Lung and Thyroid
% PATIENT 7093_08 has to be excluded from normals for Thyroid
nc0_ind_thyroid = [];

for i=1:numel(patient_ids_norm);
    for j=1:numel(patients_thyroid);
        if strcmp(patient_ids_norm{i}, patients_thyroid{j});
            nc0_ind_thyroid(i) = j;
        end
    end
end
nc0_ind_thyroid = nc0_ind_thyroid(nc0_ind_thyroid>0);
% Compute mean and sd for NC patients for plotting normal range
d_thyroid_NC0 = d_thyroid(nc0_ind_thyroid,:,:);

mu_thyroid_NC0 = squeeze(mean(d_thyroid_NC0(:,2,:), 1));
sigma_thyroid_NC0 = squeeze(std(d_thyroid_NC0(:,2,:), 1));


figure('pos',[700, 800, 300, 400], 'Color','w');
boxplot([d_thyroid_MAX(nc0_ind_thyroid,ImaxAUC_thyroid); ...
         d_thyroid_MAX(flags_thyroid(:,6)==0,ImaxAUC_thyroid); ...
         d_thyroid_MAX(flags_thyroid(:,6)==1,ImaxAUC_thyroid)], ...
        [1.*ones(1, numel(d_thyroid_MAX(nc0_ind_thyroid,ImaxAUC_thyroid))), ...
         2.*ones(1, numel(d_thyroid_MAX(flags_thyroid(:,6)==0,ImaxAUC_thyroid))), ...
         3.*ones(1, numel(d_thyroid_MAX(flags_thyroid(:,6)==1,ImaxAUC_thyroid)))], ...
         'Labels',{['NC0 (n=' num2str(numel(nc0_ind_thyroid)) ')'],...
                   ['NC (n=' num2str(sum(flags_thyroid(:,6)==0)) ')'],...
                   ['AE (n=' num2str(sum(flags_thyroid(:,6)==1)) ')']});
ylabel('MAX(\Delta SUV_{OPT%})','FontSize',14);
grid on;
lines = findobj(gcf, 'type', 'line');
set(lines,'LineWidth',1.5);
hold on;
plot([0,4], [OPT_OPT_T_thyroid, OPT_OPT_T_thyroid], 'k-');
ylim([-60, 250]);
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\thyroid_boxplots3.png');


% Lung
nc0_ind = [];

for i=1:numel(patient_ids_norm);
    for j=1:numel(patients);
        if strcmp(patient_ids_norm{i}, patients{j});
            nc0_ind(i) = j;
        end
    end
end
nc0_ind = nc0_ind(nc0_ind>0);
% Compute mean and sd for NC patients for plotting normal range
d_lung_NC0 = d_lung(nc0_ind,:,:);

mu_lung_NC0 = squeeze(mean(d_lung_NC0(:,2,:), 1));
sigma_lung_NC0 = squeeze(std(d_lung_NC0(:,2,:), 1));


figure('pos',[700, 800, 300, 400], 'Color','w');
boxplot([d_lung_MAX(nc0_ind,ImaxAUC_lung); ...
         d_lung_MAX(flags(:,4)==0,ImaxAUC_lung); ...
         d_lung_MAX(flags(:,4)==1,ImaxAUC_lung)], ...
        [1.*ones(1, numel(d_lung_MAX(nc0_ind,ImaxAUC_lung))), ...
         2.*ones(1, numel(d_lung_MAX(flags(:,4)==0,ImaxAUC_lung))), ...
         3.*ones(1, numel(d_lung_MAX(flags(:,4)==1,ImaxAUC_lung)))], ...
         'Labels',{['NC0 (n=' num2str(numel(nc0_ind)) ')'],...
                   ['NC (n=' num2str(sum(flags(:,4)==0)) ')'],...
                   ['AE (n=' num2str(sum(flags(:,4)==1)) ')']});
ylabel('MAX(\Delta SUV_{OPT%})','FontSize',14);
grid on;
lines = findobj(gcf, 'type', 'line');
set(lines,'LineWidth',1.5);
hold on;
plot([0,4], [OPT_OPT_T_lung, OPT_OPT_T_lung], 'k-');
ylim([-60, 250]);
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\lung_boxplots3.png');

% Bowel
% Compute mean and sd for NC patients for plotting normal range
d_bowel_NC0 = d_bowel(nc0_ind,:,:);

mu_bowel_NC0 = squeeze(mean(d_bowel_NC0(:,2,:), 1));
sigma_bowel_NC0 = squeeze(std(d_bowel_NC0(:,2,:), 1));


figure('pos',[700, 800, 300, 400], 'Color','w');
boxplot([d_bowel_MAX(nc0_ind,ImaxAUC_bowel); ...
         d_bowel_MAX(flags(:,2)==0,ImaxAUC_bowel); ...
         d_bowel_MAX(flags(:,2)==1,ImaxAUC_bowel)], ...
        [1.*ones(1, numel(d_bowel_MAX(nc0_ind,ImaxAUC_bowel))), ...
         2.*ones(1, numel(d_bowel_MAX(flags(:,2)==0,ImaxAUC_bowel))), ...
         3.*ones(1, numel(d_bowel_MAX(flags(:,2)==1,ImaxAUC_bowel)))], ...
         'Labels',{['NC0 (n=' num2str(numel(nc0_ind)) ')'],...
                   ['NC (n=' num2str(sum(flags(:,2)==0)) ')'],...
                   ['AE (n=' num2str(sum(flags(:,2)==1)) ')']});
ylabel('MAX(\Delta SUV_{OPT%})','FontSize',14);
grid on;
lines = findobj(gcf, 'type', 'line');
set(lines,'LineWidth',1.5);
hold on;
plot([0,4], [OPT_OPT_T_bowel, OPT_OPT_T_bowel], 'k-');
ylim([-60, 400]);
export_fig('C:\Users\strah\OneDrive\Namizje\Melanoma\Daniel\bowel_boxplots3.png');

