示例#1
0
def format_credible_intervals(event_name, samples, confidence_level=0.95):
    """
    Returns a list of print-able credible intervals for an NxM samples
    matrix. Handles both the two isoform and multi-isoform cases.
    """
    num_samples, num_isoforms = shape(samples)
    if num_isoforms > 2:
        cred_interval = ht.compute_multi_iso_credible_intervals(
            samples, confidence_level=confidence_level)
        cred_interval_lowbounds = ",".join(
            [str("%.2f" % (ci[0])) for ci in cred_interval])
        cred_interval_highbounds = ",".join(
            [str("%.2f" % (ci[1])) for ci in cred_interval])
        posterior_mean = ",".join("%.2f" % (val) for val in mean(samples, 0))
        output_fields = [
            event_name,
            "%s" % (posterior_mean),
            "%s" % (cred_interval_lowbounds),
            "%s" % (cred_interval_highbounds)
        ]
    else:
        cred_interval = ht.compute_credible_intervals(samples)
        posterior_mean = mean(samples, 0)[0]
        output_fields = [
            event_name,
            "%.2f" % (posterior_mean),
            "%.2f" % (cred_interval[0]),
            "%.2f" % (cred_interval[1])
        ]
    return output_fields
示例#2
0
def format_credible_intervals(event_name, samples,
                              confidence_level=0.95):
    """
    Returns a list of print-able credible intervals for an NxM samples
    matrix. Handles both the two isoform and multi-isoform cases.
    """
    num_samples, num_isoforms = shape(samples)
    if num_isoforms > 2:
        cred_interval = ht.compute_multi_iso_credible_intervals(samples,
                                                                confidence_level=confidence_level)
        cred_interval_lowbounds = ",".join([str("%.2f" %(ci[0])) for ci in cred_interval])
        cred_interval_highbounds = ",".join([str("%.2f" %(ci[1])) for ci in cred_interval])
        posterior_mean = ",".join("%.2f" %(val) for val in mean(samples, 0))
        output_fields = [event_name,
                         "%s" %(posterior_mean),
                         "%s" %(cred_interval_lowbounds),
                         "%s" %(cred_interval_highbounds)]
    else:
        cred_interval = ht.compute_credible_intervals(samples)
        posterior_mean = mean(samples, 0)[0]
        output_fields = [event_name,
                         "%.2f" %(posterior_mean),
                         "%.2f" %(cred_interval[0]),
                         "%.2f" %(cred_interval[1])]
    return output_fields
示例#3
0
    def plot_two_iso_samples(self,
                             fig=None,
                             isoform_index=0,
                             num_rows=1,
                             num_cols=1,
                             subplot_start=1,
                             plots_dir=None,
                             map_estimate=None,
                             simulation_num=1,
                             plot_intervals=False,
                             value_to_label=None,
                             label=None,
                             plot_filename=None,
                             bins=None,
                             bbox_coords=None,
                             with_legend=True,
                             title=None,
                             vanilla=False,
                             plot_mean=False,
                             normed=False,
                             fig_dims=(5, 5)):
        """
	Plot a set of samples for Psi of a two isoform gene.
	"""
        if not fig:
            sampled_psi_fig = plt.figure(figsize=fig_dims, dpi=300)
        else:
            sampled_psi_fig = fig
        ax = sampled_psi_fig.add_subplot(num_rows, num_cols, subplot_start)
        num_iters = int(self.params['iters'])
        burn_in = int(self.params['burn_in'])
        lag = int(self.params['lag'])
        percent_acceptance = float(self.params['percent_accept'])
        proposal_type = self.params['proposal_type']
        plt.rcParams['font.size'] = 10
        show_spines(ax, ['left', 'bottom'])
        bins = bins
        assert((value_to_label == None and label == None) or \
               (value_to_label != None and label != None))
        # retrieve samples
        samples_to_plot = self.samples[:, isoform_index]
        # picasso blue #0276FD

        if not vanilla:
            if bins != None:
                plt.hist(samples_to_plot,
                         align='center',
                         lw=0.5,
                         facecolor='#0276FD',
                         edgecolor='#ffffff')
            else:
                plt.hist(samples_to_plot,
                         align='center',
                         lw=0.5,
                         facecolor='#0276FD',
                         edgecolor='#ffffff')
        else:
            plt.hist(samples_to_plot,
                     align='center',
                     facecolor='#0276FD',
                     edgecolor='#0276FD')
        plt.xlabel(r'${\hat{\Psi}}_{\mathregular{MISO}}$')
        plt.ylabel('Frequency')
        plt.xlim([0, 1])

        # Normalize samples
        if normed:
            yticks = list(plt.gca().get_yticks())
            print "yticks: ", yticks
            ytick_labels = [
                "%.2f" % (float(ytick) / float(normed)) for ytick in yticks
            ]
            ax.set_yticklabels(ytick_labels)


#            samples_to_plot = samples_to_plot / float(len(samples_to_plot))

# curr_tick_labels = [label.get_label() for label in ax.get_yticklabels()]
# print "Current tick labels: ", curr_tick_labels
# new_tick_labels = []
# for label in curr_tick_labels:
#     if len(label) > 0:
#         new_label = "%.1f" %(float(label) / normed)
#     else:
#         new_label = ""
#     new_tick_labels.append(new_label)
# #ax.set_yticklabels(new_tick_labels)

        curr_axes = plt.gca()
        # Plot MAP estimate for same data
        if map_estimate:
            l = plt.axvline(x=map_estimate,
                            color='b',
                            linewidth=1.2,
                            ls='-',
                            label=r'${\hat{\Psi}}_{MAP}\ =\ %.2f$' %
                            (map_estimate))
        # Plot true Psi
        if self.true_psi:
            plot_id = "%dsimul_%diters_%dburnin_%dlag_%s_truepsi_%.2f.pdf" % (
                simulation_num, num_iters, burn_in, lag, proposal_type,
                self.true_psi)
            l = plt.axvline(x=self.true_psi,
                            color='r',
                            linewidth=1.2,
                            ls='-',
                            label=r'True $\Psi$')
        else:
            # Unknown true Psi
            plot_id = "%dsimul_%diters_%dburnin_%dlag_%s_%s_truepsi.pdf" % (
                simulation_num, num_iters, burn_in, lag, proposal_type,
                'unknown')
        if value_to_label:
            l = plt.axvline(x=value_to_label,
                            color='r',
                            linewidth=1.2,
                            ls='-',
                            label=label)
        # plot credible intervals if given
        if plot_intervals:
            #	    print "Plotting %.2f confidence intervals" %(plot_intervals * 100)
            interval_c1, interval_c2 = ht.compute_credible_intervals(
                samples_to_plot, plot_intervals)
            plt.axvline(x=interval_c1,
                        color='#999999',
                        linewidth=0.7,
                        ls='--',
                        label=r'%d' % (plot_intervals * 100) + '% CI')
            plt.axvline(x=interval_c2, color='#999999', linewidth=0.7, ls='--')
        if plot_mean:
            sample_mean = mean(samples_to_plot)
            plt.axvline(x=sample_mean, color='r', linewidth=0.8, label='Mean')
        if with_legend and (plot_intervals or self.true_psi):
            if not bbox_coords:
                lg = plt.legend(handletextpad=0.172,
                                borderpad=0.01,
                                labelspacing=.008,
                                handlelength=1.4,
                                loc='best',
                                numpoints=1)
            else:
                lg = plt.legend(handletextpad=0.172,
                                borderpad=0.01,
                                labelspacing=.008,
                                handlelength=1.4,
                                loc='best',
                                numpoints=1,
                                bbox_to_anchor=bbox_coords)
            lg.get_frame().set_linewidth(0)
            for t in lg.get_texts():
                t.set_fontsize(8)
        if title:
            plt.title(title)
        if plots_dir:
            if not plot_filename:
                plt.savefig(plots_dir + "sampled_psi_hist_%s" % (plot_id))
            else:
                plt.savefig(plots_dir + plot_filename + '.pdf')
        return curr_axes
示例#4
0
    def plot_two_iso_samples(self, fig=None, isoform_index=0, num_rows=1, num_cols=1, subplot_start=1,
			     plots_dir=None, map_estimate=None, simulation_num=1,
			     plot_intervals=False, value_to_label=None, label=None, plot_filename=None,
                             bins=None, bbox_coords=None, with_legend=True, title=None, vanilla=False,
                             plot_mean=False, normed=False):
	"""
	Plot a set of samples for Psi of a two isoform gene.
	"""
	if not fig:
	    sampled_psi_fig = plt.figure(figsize=(2.5, 2.2), dpi=300)
	else:
	    sampled_psi_fig = fig
	ax = sampled_psi_fig.add_subplot(num_rows, num_cols, subplot_start)
	num_iters = int(self.params['iters'])
	burn_in = int(self.params['burn_in'])
	lag = int(self.params['lag'])
	percent_acceptance = float(self.params['percent_accept'])
	proposal_type = self.params['proposal_type']
	plt.rcParams['font.size'] = 10
#	show_spines(ax, ['left', 'bottom'])
	bins = bins
	assert((value_to_label == None and label == None) or \
	       (value_to_label != None and label != None))
	# retrieve samples
	samples_to_plot = self.samples[:, isoform_index]
	# picasso blue #0276FD
        print "normed: ", normed, " bins: ", bins
        
	if not vanilla:
	    if bins != None:
		plt.hist(samples_to_plot, align='center', lw=0.5, facecolor='#0276FD',
                         edgecolor='#ffffff')
	    else:
		plt.hist(samples_to_plot, align='center', lw=0.5, facecolor='#0276FD',
                         edgecolor='#ffffff')
	else:
	    plt.hist(samples_to_plot, align='center', facecolor='#0276FD', edgecolor='#0276FD')
	plt.xlabel(r'${\hat{\Psi}}_{\mathregular{MISO}}$')
	plt.ylabel('Frequency')
	plt.xlim([0, 1])

        # Normalize samples
        if normed:
            yticks = list(plt.gca().get_yticks())
            print "yticks: ", yticks
            ytick_labels = ["%.2f" %(float(ytick) / float(normed)) for ytick in yticks]
            ax.set_yticklabels(ytick_labels)
#            samples_to_plot = samples_to_plot / float(len(samples_to_plot))
        

            # curr_tick_labels = [label.get_label() for label in ax.get_yticklabels()]
            # print "Current tick labels: ", curr_tick_labels
            # new_tick_labels = []
            # for label in curr_tick_labels:
            #     if len(label) > 0:
            #         new_label = "%.1f" %(float(label) / normed)
            #     else:
            #         new_label = ""
            #     new_tick_labels.append(new_label)
            # #ax.set_yticklabels(new_tick_labels)
        
	curr_axes = plt.gca()
	# Plot MAP estimate for same data
	if map_estimate:
	    l = plt.axvline(x=map_estimate, color='b', linewidth=1.2, ls='-', label=r'${\hat{\Psi}}_{MAP}\ =\ %.2f$' %(map_estimate))
	# Plot true Psi
	if self.true_psi:
	    plot_id = "%dsimul_%diters_%dburnin_%dlag_%s_truepsi_%.2f.pdf" %(simulation_num, num_iters, burn_in, lag, proposal_type, self.true_psi)
	    l = plt.axvline(x=self.true_psi, color='r', linewidth=1.2, ls='-', label=r'True $\Psi$')
	else:
	    # Unknown true Psi
	    plot_id = "%dsimul_%diters_%dburnin_%dlag_%s_%s_truepsi.pdf" %(simulation_num, num_iters, burn_in, lag, proposal_type, 'unknown')
	if value_to_label:
	    l = plt.axvline(x=value_to_label, color='r', linewidth=1.2, ls='-', label=label)
	# plot credible intervals if given
	if plot_intervals:
#	    print "Plotting %.2f confidence intervals" %(plot_intervals * 100)
	    interval_c1, interval_c2 = ht.compute_credible_intervals(samples_to_plot, plot_intervals)
	    plt.axvline(x=interval_c1, color='#999999', linewidth=0.7, ls='--',
			label=r'%d' %(plot_intervals*100) + '% CI')
	    plt.axvline(x=interval_c2, color='#999999', linewidth=0.7, ls='--')
	if plot_mean:
	    sample_mean = mean(samples_to_plot)
	    plt.axvline(x=sample_mean, color='r', linewidth=0.8, label='Mean')
	if with_legend and (plot_intervals or self.true_psi):
	    if not bbox_coords:
		lg = plt.legend(handletextpad=0.172, borderpad=0.01, labelspacing=.008, handlelength=1.4, loc='best', numpoints=1)
	    else:
		lg = plt.legend(handletextpad=0.172, borderpad=0.01, labelspacing=.008, handlelength=1.4, loc='best', numpoints=1,
				bbox_to_anchor=bbox_coords)
	    lg.get_frame().set_linewidth(0)
	    for t in lg.get_texts():
		t.set_fontsize(8)
	if title:
	    plt.title(title)
	if plots_dir:
	    if not plot_filename:
		plt.savefig(plots_dir + "sampled_psi_hist_%s" %(plot_id))
	    else:
		plt.savefig(plots_dir + plot_filename + '.pdf')
	return curr_axes