function change_point_data_plotter(cpdata,mpt,array_of_charges_cell_array,critz_cell_array,rawmat_cell_array,trialcountsmat)
%plots times of change in reliability and change in potency and related confidences by a variety of methods
%expects matrix with casenumber in the first row then cp occur conf then cp then cp temp conf interval lower lim then cp temp conf interval upper lim in the next four rows for rel and repeated below for pot,
%with all 15 cases, one per col
%called from command line, calls rectangles with error bars

confidence_thresh=90;
makebars=0;
makeconfidencebars=0;
    plot_each_value=1;
    cumulative_confidence=1;
linewidth=5;
makescatterplot=0;
    markersize=15;
    plot_marginals=1;
makebargraphsrel=1;
makebargraphspot=0;
    makecumuhistspot=1;
makebargraphscrit_o=0;
makebargraphs_basedonothercp=0;
    othercp_postinduconly=0;
    
successes=[1 3 5 7 8 9 10 11 12 15];
failures=[2 4 6 13 14];
succs_and_fails=[1 2 5 7 8 9 10 12 14 15 25 26 27 28 30];   %numbered here according to the original numbers from analyze_across_sd_stdp_expts
clr=colors_for_stdp_expts(length(cpdata));
labbl={'rel' 'pot'};

close all
if makebars
	for expt=1:15
        figure(expt+100),hold on,xlim([0 3])
        plot([1 1],[cpdata(4,expt)*mpt(expt) cpdata(5,expt)*mpt(expt)],'color',clr(expt,:),'linewidth',linewidth)
		plot([2 2],[cpdata(8,expt)*mpt(expt) cpdata(9,expt)*mpt(expt)],'color',clr(expt,:),'linewidth',linewidth)
		plot([1 2],[cpdata(4,expt)*mpt(expt) cpdata(8,expt)*mpt(expt)],'color',clr(expt,:),'linewidth',.1)
        plot([1 2],[cpdata(5,expt)*mpt(expt) cpdata(9,expt)*mpt(expt)],'color',clr(expt,:),'linewidth',.1)
        plot([1 2],[cpdata(3,expt)*mpt(expt) cpdata(7,expt)*mpt(expt)],'color',clr(expt,:),'linewidth',1)
        plot([0 3],[0 0],'k:')
        text(1,cpdata(4,expt)*mpt(expt),num2str(cpdata(2,expt)))
		text(2,cpdata(8,expt)*mpt(expt),num2str(cpdata(6,expt)))
        set(gca,'xtick',[1 2])
        set(gca,'xticklabel',labbl)
        ylabel('Time (min)')
        title(strcat('The',num2str(expt),'th case'))
	end
end

if makeconfidencebars
    %mean_confs=[mean(cpdata(2,successes)) mean(cpdata(2,failures));mean(cpdata(6,successes)) mean(cpdata(6,failures))];
    %semz=[std(cpdata(2,successes))/sqrt(length(successes)) std(cpdata(2,failures))/sqrt(length(failures));std(cpdata(6,successes))/sqrt(length(successes)) std(cpdata(6,failures))/sqrt(length(failures))];
    %upper_error_bars=mean_confs+semz;
    %lower_error_bars=mean_confs-semz;
    %if plot_each_value, each_value_cell={cpdata(2,successes) cpdata(2,failures);cpdata(6,successes) cpdata(6,failures)};else each_value_cell={};end
    succ_reloccur=cpdata(2,successes);succ_reloccur_above_thresh=succ_reloccur(find(succ_reloccur>=confidence_thresh));succ_reloccur_below_thresh=succ_reloccur(find(succ_reloccur<confidence_thresh));fail_reloccur=cpdata(2,failures);
    mnz_reloccur=[mean(succ_reloccur_above_thresh) mean(succ_reloccur_below_thresh) mean(fail_reloccur)];errz_reloccur=[standard_err(succ_reloccur_above_thresh) standard_err(succ_reloccur_below_thresh) standard_err(fail_reloccur)];
    succ_potoccur=cpdata(6,successes);succ_potoccur_above_thresh=succ_potoccur(find(succ_potoccur>=confidence_thresh));succ_potoccur_below_thresh=succ_potoccur(find(succ_potoccur<confidence_thresh));fail_potoccur=cpdata(6,failures);
    mnz_potoccur=[mean(succ_potoccur_above_thresh) mean(succ_potoccur_below_thresh) mean(fail_potoccur)];errz_potoccur=[standard_err(succ_potoccur_above_thresh) standard_err(succ_potoccur_below_thresh) standard_err(fail_potoccur)];
    indiv_valsoccur{1,1}=succ_reloccur_above_thresh;indiv_valsoccur{1,2}=succ_reloccur_below_thresh;indiv_valsoccur{1,3}=fail_reloccur;
    indiv_valsoccur{2,1}=succ_potoccur_above_thresh;indiv_valsoccur{2,2}=succ_potoccur_below_thresh;indiv_valsoccur{2,3}=fail_potoccur;
    rectangles_with_error_bars([mnz_reloccur;mnz_potoccur],[mnz_reloccur+errz_reloccur;mnz_potoccur+errz_potoccur],[mnz_reloccur-errz_reloccur;mnz_potoccur-errz_potoccur],zeros(3,3),plot_each_value,indiv_valsoccur)
    
    succ_reltime=cpdata(3,successes).*mpt(successes);succ_pottime=cpdata(7,successes).*mpt(successes);
    fail_reltime=cpdata(3,failures).*mpt(failures);fail_pottime=cpdata(7,failures).*mpt(failures);
    succ_reltime_abovethresh=succ_reltime(find(succ_reloccur>=confidence_thresh));succ_reltime_belowthresh=succ_reltime(find(succ_reloccur<confidence_thresh));
    succ_pottime_abovethresh=succ_pottime(find(succ_potoccur>=confidence_thresh));succ_pottime_belowthresh=succ_pottime(find(succ_potoccur<confidence_thresh));
    mnz_reltime=[mean(succ_reltime_abovethresh) mean(succ_reltime_belowthresh) mean(fail_reltime)];errz_reltime=[standard_err(succ_reltime_abovethresh) standard_err(succ_reltime_belowthresh) standard_err(fail_reltime)];
    mnz_pottime=[mean(succ_pottime_abovethresh) mean(succ_pottime_belowthresh) mean(fail_pottime)]; errz_pottime=[standard_err(succ_pottime_abovethresh) standard_err(succ_pottime_belowthresh) standard_err(fail_pottime)];
    indiv_valstime{1,1}=succ_reltime_abovethresh;indiv_valstime{1,2}=succ_reltime_belowthresh;indiv_valstime{1,3}=fail_reltime;
    indiv_valstime{2,1}=succ_pottime_abovethresh;indiv_valstime{2,2}=succ_pottime_belowthresh;indiv_valstime{2,3}=fail_pottime;
    figure
    rectangles_with_error_bars([mnz_reltime;mnz_pottime],[mnz_reltime+errz_reltime;mnz_pottime+errz_pottime],[mnz_reltime-errz_reltime;mnz_pottime-errz_pottime],zeros(3,3),plot_each_value,indiv_valstime)
    if cumulative_confidence
        figure(400),hold on
        cumulative_histogram(succ_reltime_abovethresh,'r')
        cumulative_histogram(succ_reltime_belowthresh,'g')
        cumulative_histogram(fail_reltime,'b')
        figure(500),hold on
        cumulative_histogram(succ_pottime_abovethresh,'r')
        cumulative_histogram(succ_pottime_belowthresh,'g')
        cumulative_histogram(fail_pottime,'b')
    end
end
    
if makescatterplot
    for expt=1:size(cpdata,2)
        if cpdata(2,expt)>confidence_thresh && cpdata(6,expt)>confidence_thresh
            figure(10),subplot('position',[.25 .25 .5 .5]),hold on
            plot(cpdata(3,expt)*mpt(expt),cpdata(7,expt)*mpt(expt),'o','markerfacecolor',clr(expt,:),'markeredgecolor',clr(expt,:),'markersize',markersize)
            xlabel('Reliability change time min'),ylabel('Potency change time min'),axis([-1 25 -1 25]),set(gca,'xtick',[0 5 10 15 20 25]),set(gca,'ytick',[0 5 10 15 20 25])
        end
        if plot_marginals && cpdata(2,expt)>confidence_thresh
            figure(10),subplot('position',[.25 .05 .5 .1]),hold on
            plot(cpdata(3,expt)*mpt(expt),1+rand*.05,'o','markerfacecolor',clr(expt,:),'markeredgecolor',clr(expt,:),'markersize',markersize)
            xlim([-1 25]),set(gca,'xtick',[0 5 10 15 20 25]),set(gca,'ytick',[]),ylim([.95 1.1])
        end
        if plot_marginals && cpdata(6,expt)>confidence_thresh
            figure(10),subplot('position',[.05 .25 .1 .5]),hold on
            plot(1+rand*.05,cpdata(7,expt)*mpt(expt),'o','markerfacecolor',clr(expt,:),'markeredgecolor',clr(expt,:),'markersize',markersize)
            ylim([-1 25]),set(gca,'ytick',[0 5 10 15 20 25]),set(gca,'xtick',[]),xlim([.95 1.1])
        end 
        figure(20)
        if cpdata(2,expt)>confidence_thresh
            subplot(5,5,1:5),hold on,plot(cpdata(3,expt)*mpt(expt),1+rand*.05,'o','markerfacecolor',clr(expt,:),'markeredgecolor',clr(expt,:),'markersize',markersize)
            set(gca,'ytick',[],'xlim',[-1 25])
        end
        if cpdata(6,expt)>confidence_thresh
            subplot(5,5,21:25),hold on,plot(cpdata(7,expt)*mpt(expt),1+rand*.05,'o','markerfacecolor',clr(expt,:),'markeredgecolor',clr(expt,:),'markersize',markersize)
            set(gca,'ytick',[],'xlim',[-1 25])
        end
    end
end

for expt=1:size(cpdata,2)
    lastpreinductrial=trialcountsmat(1,expt);
    cp_rel=cpdata(3,expt)+lastpreinductrial;  %translating the cp to the overall number of trials
    cp_pot=cpdata(7,expt)+lastpreinductrial; %translating the cp to the overall number of trials
    crit=critz_cell_array{succs_and_fails(expt)};num_APs=size(crit,1);      %the cell arrays are numbered by 'presentcase' in analyze_across... (i.e., up to 30), so a reorientation is needed
    sumcrit=sum(crit);
    reliability=sumcrit/num_APs;
    array_of_charges=array_of_charges_cell_array{succs_and_fails(expt)};
    array_of_charges(find(~crit))=0;
	potency_numerator=sum(array_of_charges);
	warning off matlab:dividebyzero,potency=potency_numerator./sumcrit;warning on matlab:dividebyzero
	potency(isnan(potency))=0;
    if lastpreinductrial+trialcountsmat(2,expt) ~= size(crit,2), error('Problem with trial counts'),end
    if makebargraphsrel && cpdata(2,expt)>confidence_thresh
        figure(expt+100), hold on
        beforeinduc_rel=reliability(1:lastpreinductrial);
        if lastpreinductrial<cp_rel,postinducprechange_rel=reliability((lastpreinductrial+1):cp_rel);else postinducprechange_rel=2;end
        postchange_rel=reliability((cp_rel+1):end);
        meanz=[mean(beforeinduc_rel) mean(postinducprechange_rel) mean(postchange_rel)];
        semz=[standard_err(beforeinduc_rel) standard_err(postinducprechange_rel) standard_err(postchange_rel)];
%         rectangles_with_error_bars(meanz, meanz+semz, meanz-semz,repmat(clr(expt,:)',1,3),0,{})
%         title(strcat('Reliability, the',num2str(succs_and_fails(expt)),'th case, thresh',num2str(confidence_thresh)))
%         set(gca,'ytick',[0 1],'xtick',[]),xlim([0 0.3])
        %
        crit_o=crit(1,:);
        beforeinduc_crit_o=crit_o(1:lastpreinductrial);
        if lastpreinductrial<cp_rel,postinducprechange_crit_o=crit_o((lastpreinductrial+1):cp_rel);else postinducprechange_crit_o=2;end
        postchange_crit_o=crit_o((cp_rel+1):end);
        if postinducprechange_crit_o==0,postinducprechange_crit_o=2;end
%        crit_o_cell={beforeinduc_crit_o postinducprechange_crit_o postchange_crit_o};
%         meanz=[];upper_limz=[];lower_limz=[];
%         for iii=1:3,meanz(iii)=mean(crit_o_cell{iii});[upper_limz(iii),lower_limz(iii)]=agresti_coull_binomial_confidence_interval(length(find(crit_o_cell{iii})),length(crit_o_cell{iii}));end
%         meanz(find(~meanz))=2;          
%         rectangles_with_error_bars(meanz, upper_limz, lower_limz,repmat(clr(expt,:)',1,3),0,{})
%         title(strcat('Pr o, the',num2str(succs_and_fails(expt)),'th case, thresh',num2str(confidence_thresh)))
%         set(gca,'ytick',[0 1],'xtick',[]),ylim([0 1]),xlim([0 0.3])
        meanz=[mean(beforeinduc_rel) mean(beforeinduc_crit_o);mean(postinducprechange_rel) mean(postinducprechange_crit_o); mean(postchange_rel) mean(postchange_crit_o)]
        errz=[standard_err(beforeinduc_rel) 0;standard_err(postinducprechange_rel) 0; standard_err(postchange_rel) 0]
        rectangles_with_error_bars(meanz,meanz+errz,meanz-errz,repmat(clr(expt,:)',1,3),0,{})
        %
        
	end
    if makebargraphspot && cpdata(6,expt)>confidence_thresh
        figure(200+expt),hold on
        %----redefining potency temporarily so as not to have to rewrite the following to get plots -- oct 7 2007
%         potency=array_of_charges_cell_array{succs_and_fails(expt)}(1,:);
%         potency(find(~crit(1,:)))=nan;
        %potency=sum(array_of_charges_cell_array{succs_and_fails(expt)});
        %potency=max(array_of_charges_cell_array{succs_and_fails(expt)});
        %-----
        beforeinduc_pot=potency(1:lastpreinductrial);
        if lastpreinductrial<cp_pot,postinducprechange_pot=potency((lastpreinductrial+1):cp_pot);else postinducprechange_pot=1;end
        postchange_pot=potency((cp_pot+1):end);
        
        beforeinduc_pot(isnan(beforeinduc_pot))=[];postinducprechange_pot(isnan(postinducprechange_pot))=[];postchange_pot(isnan(postchange_pot))=[];
        
        meanz=[mean(beforeinduc_pot) mean(postinducprechange_pot) mean(postchange_pot)];
        semz=[standard_err(beforeinduc_pot) standard_err(postinducprechange_pot) standard_err(postchange_pot)];
        rectangles_with_error_bars(meanz, meanz+semz, meanz-semz,repmat(clr(expt,:)',1,3),0,{})
        title(strcat('Total charge each trial, the',num2str(succs_and_fails(expt)),'th case, thresh',num2str(confidence_thresh)))
        set(gca,'xtick',[]),xlim([0 0.3])
        if makecumuhistspot
            figure(300+expt),hold on
            cumulative_histogram(beforeinduc_pot,'r');
            cumulative_histogram(postinducprechange_pot,'g');
            cumulative_histogram(postchange_pot,'b');
            title(strcat('Total charge each trial, the',num2str(succs_and_fails(expt)),'th case, thresh',num2str(confidence_thresh)))
            xlabel('Mean potency (fC)'),ylabel('Cumulative proportion')
            set(gca,'ytick',[0 0.5 1])
        end
    end
    if makebargraphscrit_o && cpdata(2,expt)>confidence_thresh
        figure(expt+400),hold on
        crit_o=crit(1,:);
        beforeinduc_crit_o=crit_o(1:lastpreinductrial);
        if lastpreinductrial<cp_rel,postinducprechange_crit_o=crit_o((lastpreinductrial+1):cp_rel);else postinducprechange_crit_o=2;end
        postchange_crit_o=crit_o((cp_rel+1):end);
        crit_o_cell={beforeinduc_crit_o postinducprechange_crit_o postchange_crit_o};
        meanz=[];upper_limz=[];lower_limz=[];
        for iii=1:3,meanz(iii)=mean(crit_o_cell{iii});[upper_limz(iii),lower_limz(iii)]=agresti_coull_binomial_confidence_interval(length(find(crit_o_cell{iii})),length(crit_o_cell{iii}));end
        meanz(find(~meanz))=2;
        rectangles_with_error_bars(meanz, upper_limz, lower_limz,repmat(clr(expt,:)',1,3),0,{})
        title(strcat('Pr o, the',num2str(succs_and_fails(expt)),'th case, thresh',num2str(confidence_thresh)))
        set(gca,'ytick',[0 1],'xtick',[]),ylim([0 1]),xlim([0 0.3])
    end
    if makebargraphs_basedonothercp && cpdata(2,expt)>confidence_thresh && cpdata(6,expt)>confidence_thresh
        if cp_rel<cp_pot
            figure(expt+500),hold on
            if othercp_postinduconly
                postinduc_prechangerel_prechangepot_pot=potency((lastpreinductrial+1):cp_rel);if isempty(postinduc_prechangerel_prechangepot_pot),postinduc_prechangerel_prechangepot_pot=1;end
                postinduc_postchangerel_prechangepot_pot=potency((cp_rel+1):cp_pot);if isempty(postinduc_postchangerel_prechangepot_pot),postinduc_postchangerel_prechangepot_pot=1;end
                meanz=[mean(postinduc_prechangerel_prechangepot_pot) mean(postinduc_postchangerel_prechangepot_pot)];
                errz=[standard_err(postinduc_prechangerel_prechangepot_pot) standard_err(postinduc_postchangerel_prechangepot_pot)];
            else
                prechangerel_prechangepot_pot=potency(1:cp_rel); if isempty(prechangerel_prechangepot_pot),prechangerel_prechangepot_pot=1;end
                postchangerel_prechangepot_pot=potency((cp_rel+1):cp_pot); if isempty(postchangerel_prechangepot_pot),postchangerel_prechangepot_pot=1;end
                meanz=[mean(prechangerel_prechangepot_pot) mean(postchangerel_prechangepot_pot)];
                errz=[standard_err(prechangerel_prechangepot_pot) standard_err(postchangerel_prechangepot_pot)];
            end
            rectangles_with_error_bars(meanz,meanz+errz,meanz-errz,repmat(clr(expt,:)',1,2),0,{})
        elseif cp_pot<cp_rel
            figure(expt+600),hold on
            if othercp_postinduconly
                postinduc_prechangepot_prechangerel_rel=reliability((lastpreinductrial+1):cp_pot);if isempty(postinduc_prechangepot_prechangerel_rel),postinduc_prechangepot_prechangerel_rel=1;end
                postinduc_postchangepot_prechangerel_rel=reliability((cp_pot+1):cp_rel); if isempty(postinduc_postchangepot_prechangerel_rel),postinduc_postchangepot_prechangerel_rel=1;end
                meanz=[mean(postinduc_prechangepot_prechangerel_rel) mean(postinduc_postchangepot_prechangerel_rel)];
                errz=[standard_err(postinduc_prechangepot_prechangerel_rel) standard_err(postinduc_postchangepot_prechangerel_rel)];
            else
                prechangepot_prechangerel_rel=reliability(1:cp_pot); if isempty(prechangepot_prechangerel_rel),prechangepot_prechangerel_rel=1;end
                postchangepot_prechangerel_rel=reliability(cp_pot+1:cp_rel); if isempty(postchangepot_prechangerel_rel),postchangepot_prechangerel_rel=1;end
                meanz=[mean(prechangepot_prechangerel_rel) mean(postchangepot_prechangerel_rel)];
                errz=[standard_err(prechangepot_prechangerel_rel) standard_err(postchangepot_prechangerel_rel)];
            end
            rectangles_with_error_bars(meanz,meanz+errz,meanz-errz,repmat(clr(expt,:)',1:2),0,{})
        end
    end
%     if show_raw_waveform  && cpdata(6,expt)>confidence_thresh
%         
%     end
end    