function [reliability,mean_potency]=analyze_diary(critz,minutes_per_trial,APs_per_trial,pscmat,patchtitle,trialcountmat,casecolor,current_expt,asynch_count_by_trial,bgrd_asynch_count_by_trial,...
    array_of_charges,general_figure_position,time_window_before,time_window_after,sd_ratio,processed_sd,use_psc_instead_of_charge)
%this m-file determines and displays, for three or four pulse trains, the proportion that succeed each trial ('reliability'), and the mean size of successes each trial ('potency'), & many other parameters
%called by analyze_across_sd_stdp_expts

plot_with_inset=0;
    inset_location=[.25 .05 .25 .2]; %location of the inset (subplot position in the routine that plots the inset)
    figure(current_expt+100),set(gcf,'position',general_figure_position)      %coordinate this with the inset plotter if applicable
plot_scale_bar=1;
     exaggeratedtrialcountmat=trialcountmat%[80 200];     %for scale bar;  assumes exaggeratedtrialcountmat(1) preinduction trials, and exaggeratedtrialcountmat(2) postinduction trials to make sure the fullest range is covered
widthpertrial_scalefactor=.007;
reliabilitylinewidth=0.1;
potencylinewidth=0.5;
induclinewidth=2.5;
ylimreliability=[-.25 1.25];
induclineheight=[-1 2];
markersize=4;

summed_successes=sum(critz);
reliability=(summed_successes/APs_per_trial);
if use_psc_instead_of_charge,psc_or_charge_values=pscmat(3:end,:);else psc_or_charge_values=array_of_charges;end
max_psc_or_charge_per_trial=max(psc_or_charge_values);     %note that this is done before failures are removed
psc_or_charge_o_per_trial=psc_or_charge_values(1,:);     %note that this is done before failures are removed
psc_or_charge_a_per_trial=psc_or_charge_values(2,:);     %note that this is done before failures are removed
psc_or_charge_b_per_trial=psc_or_charge_values(3,:);     %note that this is done before failures are removed
if size(psc_or_charge_values,1)==4
    psc_or_charge_c_per_trial=psc_or_charge_values(4,:);     %note that this is done before failures are removed
end
sum_psc_or_charge_per_trial=sum(psc_or_charge_values);      %note that this is done before failures are removed     
psc_or_charge_values(find(~critz))=0;
potency_numerator=sum(psc_or_charge_values);
mean_potency=potency_numerator./summed_successes;
mean_potency(isnan(mean_potency))=0;        %the presence of NaN would make change point evaluation with bootstrap based on cumsum yield a changepoint far earlier than the true changepoint

% for pot_var_finder=1:size(psc_or_charge_values,2)             %attempt to take the variability between the largest and smallest successful event each trial (from trials w/ atleast 2 successes) to track the emergence of a new site
%     if length(find(psc_or_charge_values(:,pot_var_finder)))>1,
%         liveones=psc_or_charge_values(:,pot_var_finder);
%         liveones=liveones(find(liveones));
%         potmax=max(liveones);
%         potmin=min(liveones);
%         potvarmean(pot_var_finder)=mean([potmax potmin]);potvarstd(pot_var_finder)=std([potmax potmin]);end%/potvarmean(pot_var_finder);end
% end
%potvarstd=potvarstd./potvarmean %using CV instead of std
%potvarstd(find(~potvarstd))=nan;
%potvarstd=potvarstd./potvarmean;

% uno=psc_or_charge_values(end,:);liveuno=find(uno);  %attempt to determine how much a successful PSCo differed from its successful predecessor as a way of catching the emergence of a new release site
% for lu=2:length(liveuno)
%     potvarstd(liveuno(lu))=std([uno(liveuno(lu)) uno(liveuno(lu-1))])/mean([uno(liveuno(lu)) uno(liveuno(lu-1))]);
% end
% potvarstd(liveuno(1))=1;%uno(liveuno(1));
%potvarstd=mean_with_preference_for_precedents(potvarstd,find(potvarstd),2)
% nonnantrials=find(~isnan(potvarstd))
% for mmm=1:length(nonnantrials)-1       %this will fail if there is only one such trial
% 	gulf=1:(nonnantrials(mmm+1)-nonnantrials(mmm)-1);
% 	if ~isempty(gulf)
%         increment=(potvarstd(nonnantrials(mmm+1))-potvarstd(nonnantrials(mmm)))/(nonnantrials(mmm+1)-nonnantrials(mmm));
%         adder=[gulf*increment+potvarstd(nonnantrials(mmm)) potvarstd(nonnantrials(mmm+1))];
%         if 1 & length(adder)>3,adder=interpolate_with_smoothing(adder);end
% 		potvarstd((nonnantrials(mmm)+1):(nonnantrials(mmm+1)-1))=adder(1:end-1);
% 	end
% end

widthh_pre=widthpertrial_scalefactor*minutes_per_trial*trialcountmat(1);
widthh_post=widthpertrial_scalefactor*minutes_per_trial*trialcountmat(2);

if ~plot_with_inset
    heightt=.2;    bottompoint=.05;
    inductiontime_alignment_point=.35;
    induclineposition=inductiontime_alignment_point;
    postindstartspot=inductiontime_alignment_point;
else
    heightt=inset_location(4);   bottompoint=inset_location(2);
    inductiontime_alignment_point=inset_location(1);
    induclineposition=[nan nan];
    postindstartspot=inset_location(1)+inset_location(3);
end

h1=subplot('position',[(inductiontime_alignment_point-widthh_pre) bottompoint widthh_pre heightt]);
plot(reliability(1:trialcountmat(1)), 'o','markerfacecolor','k','markeredgecolor','k','markersize',markersize,'linewidth',reliabilitylinewidth)
set(h1,'ylim',ylimreliability,'ytick',[],'xlim',[1 trialcountmat(1)],'xtick',[])
h2=subplot('position',[postindstartspot bottompoint widthh_post heightt]);
plot(reliability((trialcountmat(1)+1):end), 'o','markerfacecolor','k','markeredgecolor','k','markersize',markersize,'linewidth',reliabilitylinewidth)
hold on, plot(induclineposition,induclineheight,'k-','linewidth',induclinewidth)
set(h2,'ylim',ylimreliability,'ytick',[],'xlim',[1 trialcountmat(2)],'xtick',[])
h3=axes('position',get(h1,'position'));               %subplot('position',[(inductiontime_alignment_point-widthh_pre) bottompoint widthh_pre heightt]);
plot(mean_potency(1:trialcountmat(1)),'color',casecolor,'linewidth',potencylinewidth)
set(h3,'yaxislocation','left','color','none','ylim',[0 max(mean_potency)],'ytick',[],'xlim',[1 trialcountmat(1)],'xtick',[])
h4=axes('position',get(h2,'position'));               %subplot('position',[postindstartspot bottompoint widthh_post heightt]);
plot(mean_potency((trialcountmat(1)+1):end),'color',casecolor,'linewidth',potencylinewidth)
set(h4,'yaxislocation','right','color','none','ylim',[0 max(mean_potency)],'xlim',[1 trialcountmat(2)],'xtick',[])

% h23=axes('position',get(h1,'position'));
% plot(potvarmean(1:trialcountmat(1)),'color','m','linewidth',potencylinewidth)
% set(h23,'yaxislocation','left','color','none','ylim',[0 max(potvarmean)],'ytick',[],'xlim',[1 trialcountmat(1)],'xtick',[])
% h24=axes('position',get(h2,'position'));
% plot(potvarmean((trialcountmat(1)+1):end),'color','m','linewidth',potencylinewidth)
% set(h24,'yaxislocation','right','color','none','ylim',[0 max(potvarmean)],'xlim',[1 trialcountmat(2)],'xtick',[])

% minn=[-2.5 min(potvarstd)];maxx=[2.5 max(potvarstd)];
% h25=axes('position',get(h3,'position'));
% plot(potvarstd(1:trialcountmat(1)),'k','linewidth',.1)
% set(h25,'yaxislocation','left','color','none','ylim',[minn((minn(1)<minn(2))+1) maxx((maxx(1)>maxx(2))+1)],'xlim',[1 trialcountmat(1)],'xtick',[])
% h26=axes('position',get(h4,'position'));
% plot(potvarstd((trialcountmat(1)+1):end),'k','linewidth',.1)
% set(h26,'yaxislocation','right','color','none','ylim',[minn((minn(1)<minn(2))+1) maxx((maxx(1)>maxx(2))+1)],'xlim',[1 trialcountmat(2)],'xtick',[], 'ytick',[])

% figure(current_expt+200),set(gcf,'position',general_figure_position) 
% h5=axes('position',get(h1,'position'));
% plot(max_psc_or_charge_per_trial(1:trialcountmat(1)),'d','color',[0 0 0],'markersize',markersize)
% set(h5,'yaxislocation','left','ylim',[0 max(max_psc_or_charge_per_trial)],'ytick',[],'xlim',[1 trialcountmat(1)],'xtick',[])
% h6=axes('position',get(h2,'position'));
% plot(max_psc_or_charge_per_trial((trialcountmat(1)+1):end),'d','color',[0 0 0],'markersize',markersize)
% set(h6,'yaxislocation','right','ylim',[0 max(max_psc_or_charge_per_trial)],'ytick',[],'xlim',[1 trialcountmat(2)],'xtick',[])
% h7=axes('position',get(h1,'position'));
% plot(processed_sd(1,1:trialcountmat(1)),'m:')
% set(h7,'yaxislocation','left','color','none','ylim',([min(processed_sd(1,:)) max(processed_sd(1,:))]),'ytick',[],'xlim',[1 trialcountmat(1)],'xtick',[])
% h8=axes('position',get(h2,'position'));
% plot(processed_sd(1,(trialcountmat(1)+1):end),'m:')
% set(h8,'yaxislocation','right','color','none','ylim',([min(processed_sd(1,:)) max(processed_sd(1,:))]),'ytick',[],'xlim',[1 trialcountmat(2)],'xtick',[])
% h23=axes('position',get(h1,'position'));
% plot(processed_sd(2,1:trialcountmat(1)),'c')
% set(h23,'yaxislocation','left','color','none','ylim',([min(processed_sd(2,:)) max(processed_sd(2,:))]),'ytick',[],'xlim',[1 trialcountmat(1)],'xtick',[])
% h24=axes('position',get(h2,'position'));
% plot(processed_sd(2,(trialcountmat(1)+1):end),'c')
% set(h24,'yaxislocation','right','color','none','ylim',([min(processed_sd(2,:)) max(processed_sd(2,:))]),'ytick',[],'xlim',[1 trialcountmat(2)],'xtick',[])
% figure(current_expt+100)

% h9=axes('position',get(h3,'position'));
% plot(psc_or_charge_o_per_trial(1:trialcountmat(1)),'k','linewidth',.1)%'r--','markerfacecolor',[.2 .2 .2],'markeredgecolor',[.2 .2 .2],'markersize',markersize-1)
% set(h9,'yaxislocation','left','color','none','ylim',[0 max(max_psc_or_charge_per_trial)],'ytick',[],'xlim',[1 trialcountmat(1)],'xtick',[])
% h10=axes('position',get(h4,'position'));
% plot(psc_or_charge_o_per_trial((trialcountmat(1)+1):end),'k','linewidth',.1)%'r--','markerfacecolor',[.2 .2 .2],'markeredgecolor',[.2 .2 .2],'markersize',markersize-1)
% set(h10,'yaxislocation','right','color','none','ylim',[0 max(max_psc_or_charge_per_trial)],'ytick',[],'xlim',[1 trialcountmat(2)],'xtick',[])
% 
% h11=axes('position',get(h1,'position'));
% plot(psc_or_charge_a_per_trial(1:trialcountmat(1)),'s','markerfacecolor','r','markeredgecolor','r','markersize',markersize-1)
% set(h11,'yaxislocation','left','color','none','ylim',[0 max(max_psc_or_charge_per_trial)],'ytick',[],'xlim',[1 trialcountmat(1)],'xtick',[])
% h12=axes('position',get(h2,'position'));
% plot(psc_or_charge_a_per_trial((trialcountmat(1)+1):end),'s','markerfacecolor','r','markeredgecolor','r','markersize',markersize-1)
% set(h12,'yaxislocation','right','color','none','ylim',[0 max(max_psc_or_charge_per_trial)],'ytick',[],'xlim',[1 trialcountmat(2)],'xtick',[])
% 
% h13=axes('position',get(h1,'position'));
% plot(psc_or_charge_b_per_trial(1:trialcountmat(1)),'s','markerfacecolor','g','markeredgecolor','g','markersize',markersize-1)
% set(h13,'yaxislocation','left','color','none','ylim',[0 max(max_psc_or_charge_per_trial)],'ytick',[],'xlim',[1 trialcountmat(1)],'xtick',[])
% h14=axes('position',get(h2,'position'));
% plot(psc_or_charge_b_per_trial((trialcountmat(1)+1):end),'s','markerfacecolor','g','markeredgecolor','g','markersize',markersize-1)
% set(h14,'yaxislocation','right','color','none','ylim',[0 max(max_psc_or_charge_per_trial)],'ytick',[],'xlim',[1 trialcountmat(2)],'xtick',[])

% if size(psc_or_charge_values,1)==4
% 	h15=axes('position',get(h1,'position'));
% 	plot(psc_or_charge_c_per_trial(1:trialcountmat(1)),'s','markerfacecolor','b','markeredgecolor','b','markersize',markersize-1)
% 	set(h15,'yaxislocation','left','color','none','ylim',[0 max(max_psc_or_charge_per_trial)],'ytick',[],'xlim',[1 trialcountmat(1)],'xtick',[])
% 	h16=axes('position',get(h2,'position'));
% 	plot(psc_or_charge_c_per_trial((trialcountmat(1)+1):end),'s','markerfacecolor','b','markeredgecolor','b','markersize',markersize-1)
% 	set(h16,'yaxislocation','right','color','none','ylim',[0 max(max_psc_or_charge_per_trial)],'ytick',[],'xlim',[1 trialcountmat(2)],'xtick',[])
% end

% h17=axes('position',get(h1,'position'));
% plot(asynch_count_by_trial(1:trialcountmat(1)),'d','markerfacecolor','m','markeredgecolor','m','markersize',markersize+1)
% set(h17,'yaxislocation','left','color','none','ylim',[0 max(asynch_count_by_trial)],'ytick',[],'xlim',[1 trialcountmat(1)],'xtick',[])
% h18=axes('position',get(h2,'position'));
% plot(asynch_count_by_trial((trialcountmat(1)+1):end),'d','markerfacecolor','m','markeredgecolor','m','markersize',markersize+1)
% set(h18,'yaxislocation','right','color','none','ylim',[0 max(asynch_count_by_trial)],'ytick',[],'xlim',[1 trialcountmat(2)],'xtick',[])
% h19=axes('position',get(h1,'position'));
% plot(bgrd_asynch_count_by_trial(1:trialcountmat(1)),'*','markerfacecolor','g','markeredgecolor','g','markersize',markersize+1)
% set(h19,'yaxislocation','left','color','none','ylim',[0 max(bgrd_asynch_count_by_trial)],'ytick',[],'xlim',[1 trialcountmat(1)],'xtick',[])
% h20=axes('position',get(h2,'position'));
% plot(bgrd_asynch_count_by_trial((trialcountmat(1)+1):end),'*','markerfacecolor','g','markeredgecolor','g','markersize',markersize+1)
% set(h20,'yaxislocation','right','color','none','ylim',[0 max(bgrd_asynch_count_by_trial)],'ytick',[],'xlim',[1 trialcountmat(2)],'xtick',[])

% h21=axes('position',get(h1,'position'));
% plot(sum_psc_or_charge_per_trial(1:trialcountmat(1)),'g','linewidth',2)%,'markerfacecolor','m','markeredgecolor','m','markersize',markersize-1)
% set(h21,'yaxislocation','left','color','none','ylim',[0 max(sum_psc_or_charge_per_trial)],'ytick',[],'xlim',[1 trialcountmat(1)],'xtick',[])
% h22=axes('position',get(h2,'position'));
% plot(sum_psc_or_charge_per_trial,'g','linewidth',2)%,'markerfacecolor','m','markeredgecolor','m','markersize',markersize-1)
% set(h22,'yaxislocation','right','color','none','ylim',[0 max(sum_psc_or_charge_per_trial)],'ytick',[],'xlim',[1 trialcountmat(2)],'xtick',[])

title(patchtitle)

if plot_scale_bar       %the x-axis scale bar
   subplot('position',[inductiontime_alignment_point-(widthpertrial_scalefactor*minutes_per_trial*exaggeratedtrialcountmat(1)) .5*bottompoint ...
        (minutes_per_trial*widthpertrial_scalefactor*exaggeratedtrialcountmat(1)) .25*bottompoint])
   plot([(-1*exaggeratedtrialcountmat(1)):-1]*minutes_per_trial,repmat(nan,1,exaggeratedtrialcountmat(1)), 'ko','linewidth',2),...
        set(gca,'ylim',ylimreliability,'ytick',[],'xlim',[(-1*exaggeratedtrialcountmat(1))*minutes_per_trial -1]), box off
  hold on, rectangle('position',[time_window_before(1) .5 time_window_before(end)-time_window_before(1) .25],'facecolor','k')
   subplot('position',[postindstartspot .5*bottompoint (minutes_per_trial*widthpertrial_scalefactor*exaggeratedtrialcountmat(2)) .25*bottompoint])
   plot(minutes_per_trial*[1:exaggeratedtrialcountmat(2)],repmat(nan,1,exaggeratedtrialcountmat(2)), 'ko','linewidth',2),...
        set(gca,'ylim',ylimreliability,'ytick',[],'xlim',[1 exaggeratedtrialcountmat(2)*minutes_per_trial]), box off
    hold on, rectangle('position',[time_window_after(1) .5 time_window_after(end)-time_window_after(1) .25],'facecolor','k')
    %hold on, plot([exaggeratedtrialcountmat(1)*minutes_per_trial+.5 exaggeratedtrialcountmat(1)*minutes_per_trial+.5],[-1 2],'k-','linewidth',2.5)
end

%----------
%other variables, including log PPR, etc., were also considered
% ppr_b_a_over_time=[preinduction_psc(5,find(crit_a_pre&crit_b_pre&preinduction_psc(4,:)>0&preinduction_psc(5,:)>0))./preinduction_psc(4,find(crit_a_pre&crit_b_pre&preinduction_psc(4,:)>0&preinduction_psc(5,:)>0)),...
%         postinduction_psc(5,find(crit_a_post&crit_b_post&postinduction_psc(4,:)>0&postinduction_psc(5,:)>0))./postinduction_psc(4,find(crit_a_post&crit_b_post&postinduction_psc(4,:)>0&postinduction_psc(5,:)>0))];
% ppr_a_o_over_time=[preinduction_psc(4,find(crit_o_pre&crit_a_pre&preinduction_psc(3,:)>0&preinduction_psc(4,:)>0))./preinduction_psc(3,find(crit_o_pre&crit_a_pre&preinduction_psc(3,:)>0&preinduction_psc(4,:)>0)),...
%         postinduction_psc(4,find(crit_o_post&crit_a_post&postinduction_psc(3,:)>0&postinduction_psc(4,:)>0))./postinduction_psc(3,find(crit_o_post&crit_a_post&postinduction_psc(3,:)>0&postinduction_psc(4,:)>0))];