function RD_fisher_matrix=analyze_release_dependence(critmat_pre,critmat_post,pscmat_pre,pscmat_post,chargemat_pre,chargemat_post,specialwaveforms_pre,...
    specialwaveforms_post,presentcase,casecolor,patchtitle,RD_fisher_matrix)
%designed for looking at the nth psc or q and seeing whether its size or likelihood of success is different whether the mth event succeeded or failed on trials with a given ipi
%we can consider both the preinduc period and the postinduc period, but the possibility of ongoing change postinduction complicates assessment
%called by: analyze_across_expts
%calls: cumulative_histogram, agresti_coull_binomial_confidence_interval, rung_plotter_asymm_intervals, rectangles_with_error_bars
%code confirmed date:

RD_epoch=1;RD_diary=0;RD_cumuhist=0;RD_cumuhist_showraw=0;RD_p_versus_p=1;RD_use_charge_not_psc=1;RD_which_value_is_trigger=2;RD_which_value_is_measured=1;
RD_check_IPI=0;RD_which_IPI_is_checked=1;RD_IPI_check_value=400; %in ms

casecolor=[.2 .2 .2];   %needed due to the inclusion of failure expts
markersize=5;
if RD_epoch==1,
    label_addendum='preinduction';
    critmat=critmat_pre;IPImat=pscmat_pre(1:2,:),pscmat=pscmat_pre(3:end,:);chargemat=chargemat_pre;specialwaveforms=specialwaveforms_pre;
elseif RD_epoch==2,label_addendum='postinduction';
    critmat=critmat_post;IPImat=pscmat_post(1:2,:);pscmat=pscmat_post(3:end,:);chargemat=chargemat_post;specalwaveforms=specialwaveforms_post;
elseif RD_epoch==3,label_addendum='pre and postinduction';
    critmat=[critmat_pre critmat_post];IPImat=[pscmat_pre(1:2,:) pscmat_post(1:2,:)];pscmat=[pscmat_pre(3:end,:) pscmat_post(3:end,:)];
    chargemat=[chargemat_pre chargemat_post];specialwaveforms=[specialwaveforms_pre specialwaveforms_post];
end

if RD_diary||RD_cumuhist
	if RD_use_charge_not_psc
        parameter_vector=chargemat(RD_which_value_is_measured,:);
        histlabel=strcat('Q',num2str(RD_which_value_is_measured),'trigger',num2str(RD_which_value_is_trigger),'IPI',num2str(RD_IPI_check_value),label_addendum);
    else
        parameter_vector=pscmat(RD_which_value_is_measured,:);
        histlabel=strcat('PSC',num2str(RD_which_value_is_measured),'trigger',num2str(RD_which_value_is_trigger),'IPI',num2str(RD_IPI_check_value),label_addendum);
    end 
    parameter_following_success=NaN(1,length(parameter_vector));
    parameter_following_success(find(critmat(RD_which_value_is_trigger,:)))=parameter_vector(find(critmat(RD_which_value_is_trigger,:)));
    if RD_check_IPI
        parameter_following_success(find(IPImat(RD_which_IPI_is_checked,:)~=RD_IPI_check_value))=nan;%parameter_following_success(find(~critmat(RD_which_value_is_measured,:)))=nan;
    end
    parameter_following_failure=NaN(1,length(parameter_vector));
    parameter_following_failure(find(~critmat(RD_which_value_is_trigger,:)))=parameter_vector(find(~critmat(RD_which_value_is_trigger,:)));
    if RD_check_IPI
        parameter_following_failure(find(IPImat(which_IPI_is_checked,:)~=RD_IPI_check_value))=nan;%parameter_following_failure(find(~critmat(RD_which_value_is_measured,:)))=nan;
    end
end

if RD_diary
	figure(presentcase+200),hold on
    plot(pscmat(1,:),'^','markerfacecolor','k','markeredgecolor','k','markersize',markersize)   %psc always used here
	plot(parameter_following_success,'d','markerfacecolor','g','markeredgecolor','g','markersize',markersize)
	plot(parameter_following_failure,'d','markeredgecolor','r','markerfacecolor','r','markersize',markersize)
    if RD_epoch==3
        plot([size(critmat_pre,2)+.5 size(critmat_pre,2)+.5],[0 max([parameter_following_success(find(~isnan(parameter_following_success))) parameter_following_failure(find(~isnan(parameter_following_failure)))])],'r','linewidth',2)
    end
    title(num2str(presentcase))
end

if RD_cumuhist
    if any(~isnan(parameter_following_success))&&any(~isnan(parameter_following_failure))
		figure(presentcase+300),hold on
        [x_for_ecdf_triggersucceeded,y_for_ecdf_triggersucceeded]=cumulative_histogram(parameter_following_success,'g');
		[x_for_ecdf_triggerfailed,y_for_ecdf_triggerfailed]=cumulative_histogram(parameter_following_failure,'r:');
		[h,p]=kstest2(parameter_following_success,parameter_following_failure)
		ylabel(strcat('KS p:',num2str(p)))
		set(findobj('type','line'),'color','k','linewidth',.5)
		set(gca,'ytick',[0 1]),xlabel(histlabel)
		title(patchtitle)
        if RD_cumuhist_showraw
            forposselec=critmat(RD_which_value_is_trigger,:);
            if RD_check_IPI,forposselec(find(IPImat(RD_which_IPI_is_checked,:)~=RD_IPI_check_value))=0;end
            fornegselec=critmat(RD_which_value_is_trigger,:);
            if RD_check_IPI,fornegselec(find(IPImat(RD_which_IPI_is_checked,:)~=RD_IPI_check_value))=1;end
            positive_selection_waveforms=specialwaveforms(:,find(forposselec))';negative_selection_waveforms=specialwaveforms(:,find(~fornegselec))';
			figure(400+presentcase),hold on,for iii=1:size(positive_selection_waveforms,1),plot(positive_selection_waveforms(iii,:),'color',casecolor,'linewidth',.2),end
			figure(500+presentcase),hold on,for jjj=1:size(negative_selection_waveforms,1),plot(negative_selection_waveforms(jjj,:),'color',casecolor,'linewidth',.2),end
		end
    end
end

if RD_p_versus_p
    trig=critmat(RD_which_value_is_trigger,:);
    meas=critmat(RD_which_value_is_measured,:);
    
   if RD_check_IPI,
       trig(find(IPImat(RD_which_IPI_is_checked,:)~=RD_IPI_check_value))=[];
       meas(find(IPImat(RD_which_IPI_is_checked,:)~=RD_IPI_check_value))=[];
   end

   probability_label=strcat('meas:',num2str(RD_which_value_is_measured),'trig:',num2str(RD_which_value_is_trigger),'IPI:',num2str(RD_IPI_check_value),label_addendum);

    if ~isempty(trig)
        meas_succ_count_when_trig_succ=length(find(meas(find(trig))));      trig_succ_count=length(find(trig));
        meas_succ_count_when_trig_fail=length(find(meas(find(~trig))));     trig_fail_count=length(find(~trig));
        prop_meas_succ_when_trig_succ=meas_succ_count_when_trig_succ/trig_succ_count;prop_meas_succ_when_trig_fail=meas_succ_count_when_trig_fail/trig_fail_count;
        [upperlimsuccess_trigontrig,lowerlimsuccess_trigontrig]=agresti_coull_binomial_confidence_interval(meas_succ_count_when_trig_succ,trig_succ_count);
        [upperlimfailure_trigontrig,lowerlimfailure_trigontrig]=agresti_coull_binomial_confidence_interval(meas_succ_count_when_trig_fail,trig_fail_count);
        
        trig_succ_count_when_meas_succ=length(find(trig(find(meas))));      meas_succ_count=length(find(meas));
        trig_succ_count_when_meas_fail=length(find(trig(find(~meas))));     meas_fail_count=length(find(~meas));
        prop_trig_succ_when_meas_succ=trig_succ_count_when_meas_succ/meas_succ_count;prop_trig_succ_when_meas_fail=trig_succ_count_when_meas_fail/meas_fail_count;
        [upperlimsuccess_trigonmeas,lowerlimsuccess_trigonmeas]=agresti_coull_binomial_confidence_interval(trig_succ_count_when_meas_succ,meas_succ_count);
        [upperlimfailure_trigonmeas,lowerlimfailure_trigonmeas]=agresti_coull_binomial_confidence_interval(trig_succ_count_when_meas_fail,meas_fail_count);
        
        
        RD_fisher_matrix{presentcase}=[meas_succ_count_when_trig_succ meas_succ_count_when_trig_fail; trig_succ_count-meas_succ_count_when_trig_succ trig_fail_count-meas_succ_count_when_trig_fail]
                
        %bp=[prop_meas_succ_when_trig_succ,prop_meas_succ_when_trig_fail,prop_trig_succ_when_meas_succ,prop_trig_succ_when_meas_fail];
        %release_dependence_binomial_proportions=[release_dependence_binomial_proportions;bp]
        

        figure(600+presentcase),rung_plotter_asymm_intervals(prop_meas_succ_when_trig_succ,upperlimsuccess_trigontrig,lowerlimsuccess_trigontrig,prop_meas_succ_when_trig_fail,upperlimfailure_trigontrig,lowerlimfailure_trigontrig,[.2 1 .2],.1)
        hold on, rung_plotter_asymm_intervals(prop_trig_succ_when_meas_succ,upperlimsuccess_trigonmeas,lowerlimsuccess_trigonmeas,prop_trig_succ_when_meas_fail,upperlimfailure_trigonmeas,lowerlimfailure_trigonmeas,[.2 .2 1],.2)
        xlabel(probability_label)

        if ~isnan(prop_meas_succ_when_trig_succ) & ~isnan(prop_trig_succ_when_meas_succ) & ~isnan(prop_meas_succ_when_trig_fail) & ~isnan(prop_trig_succ_when_meas_fail)...
            & prop_meas_succ_when_trig_succ & prop_trig_succ_when_meas_succ & prop_meas_succ_when_trig_fail & prop_trig_succ_when_meas_fail

            %figure(700+presentcase),rectangles_with_error_bars([prop_meas_succ_when_trig_succ prop_trig_succ_when_meas_succ;prop_meas_succ_when_trig_fail prop_trig_succ_when_meas_fail],...
            %    [upperlimsuccess_trigontrig upperlimsuccess_trigonmeas;upperlimfailure_trigontrig upperlimfailure_trigonmeas],[lowerlimsuccess_trigontrig lowerlimsuccess_trigonmeas;lowerlimfailure_trigontrig...
            %    lowerlimfailure_trigonmeas], [.2 1 .2;.2 .2 1]',1)
            %xlabel(probability_label)
        end
    end
end