示例#1
0
def plot_fit_consistency(population_fits, plot_true=True, fig_no=1):

    fit_means = np.array([pf["pop_params"]["means"] for pf in population_fits])
    fit_SDs = np.array([pf["pop_params"]["SDs"] for pf in population_fits])
    true_means = population_fits[0]["pop_params_true"]["means"]
    true_SDs = population_fits[0]["pop_params_true"]["SDs"]
    n_params = fit_means.shape[1]
    n_fits = fit_means.shape[0]
    x = np.arange(n_fits) / float(n_fits)
    ymin = np.min(fit_means - fit_SDs) - 0.2
    ymax = np.max(fit_means + fit_SDs) + 0.2

    if not len(true_means) == n_params:
        plot_true = False

    p.figure(fig_no)
    p.clf()

    for i in range(n_params):
        p.subplot(1, n_params, i + 1)
        if plot_true:
            p.plot([0.45, 0.45], [true_means[i] - true_SDs[i], true_means[i] + true_SDs[i]], "r", linewidth=2)
        for f in range(n_fits):
            p.plot([x[f], x[f]], [fit_means[f, i] - fit_SDs[f, i], fit_means[f, i] + fit_SDs[f, i]], "b")
            p.locator_params(axis="y", nbins=4)
            p.xticks([])
            p.xlabel(population_fits[0]["param_names"][i])
            p.ylim(ymin, ymax)
示例#2
0
def plot_error_bars_shrink(result, filename=None):
    pylab.rcParams.update({"font.size": 8})
    pylab.locator_params(nbins=3)
    fig = pylab.figure(num=None, figsize=(8, 10), facecolor="w", edgecolor="k")

    num_queries = result["num_queries"]
    confidence = result["config"]["confidence"]

    dep_means = result["dependence_probability_mean"]
    dep_error = result["dependence_probability_error"]
    inf_means = result["infer_means"]
    inf_error = result["infer_stderr"]
    x = result["iteration"]

    ax = pylab.subplot(3, 1, 1)
    for q in range(num_queries):
        pylab.errorbar(x, dep_means[q, :], yerr=dep_error[q, :], alpha=0.7)
    pylab.title("Dependence probability")
    ax.yaxis.set_major_locator(MaxNLocator(3))
    ax.xaxis.set_major_locator(MaxNLocator(5))
    pylab.xlabel("iteration")

    ax = pylab.subplot(3, 1, 2)
    for q in range(num_queries):
        pylab.errorbar(x, inf_means[q, :], yerr=inf_error[q, :], alpha=0.7)
    pylab.title("Infer. Confidence=%1.2f" % confidence)
    pylab.xlabel("iteration")
    ax.yaxis.set_major_locator(MaxNLocator(3))
    ax.xaxis.set_major_locator(MaxNLocator(5))

    num_iters = result["config"]["num_iters"]
    num_chains = result["config"]["num_chains"]
    num_runs = result["config"]["num_runs"]
    prop_missing = result["config"]["prop_missing"]
    confidence = result["config"]["confidence"]

    txt = """
        Left: Dependence probability with standard error calculated over %i runs of %i 
        chains for %i iterations.

        Right: Proportion of missing data successfully imputed with INFER with %1.2f
        confidence.

        Data used was dha.csv. %1.2f percent of data was removed.
    """ % (
        num_runs,
        num_chains,
        num_iters,
        confidence,
        prop_missing * 100,
    )

    ax = pylab.subplot(3, 1, 3)
    ax.text(0, 0, txt, fontsize=9)
    ax.axis("off")

    if filename is None:
        filename = "Error_bars_shrink-iters=%i_chains=%i_T=%i.png" % (num_iters, num_chains, int(time.time()))

    pylab.savefig(filename)
示例#3
0
def PlotAbsorbances(samples,
                    plot_baseline=True,
                    plot_original=True,
                    plot_bscorrected=False):
    if not isinstance(samples, list):
        samples = [samples]
    axis_font, legend_font = GetFontsAndSetMPLproperties()
    for s in samples:
        fig = plt.figure(figsize=(12 / 2.54, 8 / 2.54))
        ax0 = fig.add_subplot(111)
        ax0.invert_xaxis()
        plt.locator_params(nbins=6)
        if plot_original:
            ax0.plot(s.wavenumber, s.absorbance * 1E4, label='data', color='k')
        if plot_baseline:
            ax0.plot(s.wavenumber_cut,
                     s.baseline * 1E4,
                     label='baseline',
                     color='r')
        if plot_bscorrected:
            ax0.plot(s.wavenumber_cut, (s.absorbance_cut - s.baseline) * 1E4,
                     label=s.name + '_corrected')
        legend = plt.legend()
        legend.set_draggable(True)
        plt.title(s.name)
        ax0.set_xlabel('wavenumber / cm$^{-1}$')
        ax0.set_ylabel('absorbance / 10$^{-4}$nm$^{-1}$')
        plt.show()
示例#4
0
def _plot_exponential_fit(fit,
                          p_e,
                          pre_post_trials,
                          last_n,
                          col='r',
                          plot_tau=False):
    t = np.arange(0, pre_post_trials[1])
    double_exp = 'tau_f' in fit.keys()
    if double_exp:
        exp_traj = _double_exp_choice_traj(
            [fit['tau_f'], fit['tau_ratio'], fit['fs_mix']], fit['p_0'], p_e,
            t)
    else:
        exp_traj = _exp_choice_traj(fit['tau'], fit['p_0'], p_e, t)

    full_traj = np.hstack([
        ut.nans(-pre_post_trials[0] - last_n),
        np.ones(last_n) * fit['p_0'], exp_traj
    ])
    plt.plot(np.arange(pre_post_trials[0], pre_post_trials[1]),
             full_traj,
             col,
             linewidth=1.5)
    if plot_tau: plt.plot([fit['tau'], fit['tau']], [0, 1], ':' + col)
    plt.locator_params(nbins=4)
def plotReporter5bar(fig, ax, strata, pdfFile, plotDataFrame, varList,
                     yLabelText, ownership, labelYaxis):

    plotData = list(plotDataFrame[0])

    if 'Fremont-Winema NF' in ownership:
        ownership = ownership[:-35]
    elif 'JWTR' in ownership:
        ownership = 'PC1'
    elif 'Mazama' in ownership:
        ownership = 'PC2'
    elif 'Jeld' in ownership:
        ownership = 'PC3'
    elif 'Spear' in ownership:
        ownership = 'PC4'
    elif 'Collins' in ownership:
        ownership = 'PC5'
    elif 'Corporate' in ownership:
        ownership = 'Corporate'

    ind = np.arange(len(plotData))
    width = 0.35

    # reverse order of items to plot
    ##    plotData.reverse()
    ##    varListReverse = varList[::-1]

    ax.barh(ind,
            plotData,
            color=plotDataFrame.positives.map({
                True: 'b',
                False: 'r'
            }))

    pl.subplots_adjust(wspace=0.25, hspace=0.10, left=0.2)
    pl.tick_params(axis='x', which='major', labelsize=7.5)
    pl.tick_params(axis='y', which='major', labelsize=9)
    pl.locator_params(axis='x', nbins=5)

    # remove the last label on the x axis
    ##    labels = [item.get_text() for item in ax.get_xticklabels()]
    ##    labels[-1] = ''
    ##    ax.set_xticklabels(labels)

    if labelYaxis:
        ax.set(yticks=np.arange(len(plotData) + 1) + width,
               yticklabels=varList + [''],
               ylim=[2 * width - 1, len(plotData) + 1])
    else:
        ax.set(yticks=np.arange(len(plotData) + 1) + width,
               yticklabels=[],
               ylim=[2 * width - 1, len(plotData) + 1])

    ax.text(0.5,
            0.92,
            ownership,
            horizontalalignment='center',
            transform=ax.transAxes,
            fontsize=9)
示例#6
0
def plot_recovers_original_densities(result, filename=None):

    pylab.rcParams.update({"font.size": 8})
    pylab.locator_params(nbins=3)
    fig = pylab.figure(num=None, figsize=(10, 8), facecolor="w", edgecolor="k")

    plt = 0
    for shape in ["sinwave", "x", "ring", "dots"]:
        key = shape + "_original"
        X_original = result[key]
        ax = pylab.plt.subplot2grid((3, 4), (0, plt))
        pylab.scatter(X_original[:, 0], X_original[:, 1], color="blue", edgecolor="none", alpha=0.2)
        pylab.title("%s original" % shape)
        ax.yaxis.set_major_locator(MaxNLocator(3))
        ax.xaxis.set_major_locator(MaxNLocator(3))

        key = shape + "_inferred"
        X_inferred = result[key]
        ax1 = pylab.plt.subplot2grid((3, 4), (1, plt))
        pylab.scatter(X_inferred[:, 0], X_inferred[:, 1], color="red", edgecolor="none", alpha=0.2)
        pylab.xlim(ax.get_xlim())
        pylab.ylim(ax.get_ylim())
        pylab.title("%s inferred" % shape)
        ax1.yaxis.set_major_locator(MaxNLocator(3))
        ax1.xaxis.set_major_locator(MaxNLocator(3))

        plt += 1

    num_iters = result["config"]["num_iters"]
    num_rows = result["config"]["num_rows"]
    num_chains = result["config"]["num_chains"]

    txt = """
        Top panels: zero-correlation datasets consisting of %i points. 
        Bottom panels: result of SIMULATE %i datapoints after running %i 
        chains for %i iterations.
    """ % (
        num_rows,
        num_rows,
        num_chains,
        num_iters,
    )

    pylab.plt.subplot2grid((3, 3), (2, 0), colspan=3)

    ax = pylab.gca()
    ax.text(0, 0.5, txt, fontsize=10)
    ax.axis("off")

    if filename is None:
        filename = "Recovers_original_densities-num_rows=%i-num_chains=%i-num_iters=%i-T=%i.png" % (
            num_rows,
            num_chains,
            num_iters,
            int(time.time()),
        )

    pylab.savefig(filename)
示例#7
0
def plot_error_bars_shrink(result, filename=None):
    pylab.rcParams.update({'font.size': 8})
    pylab.locator_params(nbins=3)
    fig = pylab.figure(num=None, figsize=(8,10), facecolor='w', edgecolor='k')

    num_queries = result['num_queries']
    confidence = result['config']['confidence']

    dep_means = result['dependence_probability_mean'] 
    dep_error = result['dependence_probability_error']
    inf_means = result['infer_means']
    inf_error = result['infer_stderr']
    x = result['iteration']

    ax = pylab.subplot(3,1,1)
    for q in range(num_queries):
        pylab.errorbar(x, dep_means[q,:], yerr=dep_error[q,:], alpha=.7)
    pylab.title('Dependence probability')
    ax.yaxis.set_major_locator(MaxNLocator(3))
    ax.xaxis.set_major_locator(MaxNLocator(5))
    pylab.xlabel('iteration')

    ax = pylab.subplot(3,1,2)
    for q in range(num_queries):
        pylab.errorbar(x, inf_means[q,:], yerr=inf_error[q,:], alpha=.7)
    pylab.title('Infer. Confidence=%1.2f' % confidence)
    pylab.xlabel('iteration')
    ax.yaxis.set_major_locator(MaxNLocator(3))
    ax.xaxis.set_major_locator(MaxNLocator(5))

    num_iters = result['config']['num_iters']
    num_chains = result['config']['num_chains']
    num_runs = result['config']['num_runs']
    prop_missing = result['config']['prop_missing']
    confidence = result['config']['confidence']

    txt = '''
        Left: Dependence probability with standard error calculated over %i runs of %i 
        chains for %i iterations.

        Right: Proportion of missing data successfully imputed with INFER with %1.2f
        confidence.

        Data used was dha.csv. %1.2f percent of data was removed.
    ''' % (num_runs, num_chains, num_iters, confidence, prop_missing*100)

    ax = pylab.subplot(3,1,3)
    ax.text(0,0,txt, fontsize=9)
    ax.axis('off')

    if filename is None:
        filename = 'Error_bars_shrink-iters=%i_chains=%i_T=%i.png' % \
            (num_iters, num_chains, int(time.time()))

    pylab.savefig( filename )
示例#8
0
    def plotOne(b, d, name, chroms, common, shade):
        a = b.dropna()
        c = d.loc[a.index]
        if shade is not None:
            for _, row in shade.iterrows():
                plt.gca().fill_between([row.gstart, row.gend],
                                       a.min(),
                                       a.max(),
                                       color='b',
                                       alpha=0.3)
        plt.scatter(a.index,
                    a,
                    s=markerSize,
                    c=c,
                    alpha=0.8,
                    edgecolors='none')

        outliers = None
        if Outliers is not None:
            outliers = Outliers[name].dropna()
        if cutoff is not None:
            outliers = a[a >= cutoff[name]]
        elif top_k is not None:
            outliers = a.sort_values(ascending=False).iloc[:top_k]
        elif std_th is not None:
            outliers = a[a > a.mean() + std_th * a.std()]
        if outliers is not None:
            if len(outliers):
                # if name != 'Number of SNPs':
                plt.scatter(outliers.index,
                            outliers,
                            s=markerSize,
                            c='r',
                            alpha=0.8,
                            edgecolors='none')
                # plt.axhline(outliers.min(), color='k', ls='--')

        if common is not None:
            for ii in common.index:
                plt.axvline(ii, c='g', alpha=0.5)

        plt.axis('tight')
        plt.xlim(0, a.index[-1])
        plt.ylabel(name, fontsize=ticksize * 1.5)
        # plt.title('{} SNPs, {} are red.'.format(a.dropna().shape[0], outliers.shape[0]))
        plt.xticks([x for x in chroms.mid], [str(x) for x in chroms.index],
                   rotation=-90,
                   fontsize=ticksize * 1.5)
        plt.setp(plt.gca().get_xticklabels(), visible=False)
        plt.locator_params(axis='y', nbins=3)
        mpl.rc('ytick', labelsize=ticksize)
        if ylim is not None: plt.ylim(ymin=ylim)
示例#9
0
def plot_fills_in_the_blanks(result, filename=None):
    pylab.rcParams.update({"font.size": 8})
    pylab.locator_params(nbins=3)
    fig = pylab.figure(num=None, figsize=(10, 8), facecolor="w", edgecolor="k")

    prop_missing = result["config"]["prop_missing"]

    ax = pylab.subplot(2, 1, 1)
    pylab.plot(prop_missing, result["MSE_naive_bayes_indexer"], lw=3, c="red", label="Naive Bayes")
    pylab.plot(prop_missing, result["MSE_crp_mixture_indexer"], lw=3, c="blue", label="CRP mixture")
    pylab.plot(prop_missing, result["MSE_crosscat_indexer"], lw=3, c="green", label="CrossCat")

    ax.set_xticks(prop_missing)
    ax.yaxis.set_major_locator(MaxNLocator(3))
    pylab.ylabel("MSE")
    pylab.xlabel("proportion of data missing")
    pylab.legend(loc=4)

    num_iters = result["config"]["num_iters"]
    num_rows = result["config"]["num_rows"]
    num_cols = result["config"]["num_cols"]
    num_chains = result["config"]["num_chains"]
    impute_samples = result["config"]["impute_samples"]

    txt = """
        Mean squared error over all imputed missing values (y-axis) for a %i rows by %i table with
        variable proportion of missing data (x-axis).

        Green line: BayesDB normal configuration
        Blue line: BayesDB CRP mixture configuration (one view, no column transitions)
        Red line: BayesDB Naive Bayes configuration (one view, one category, only hyperparameter
        transitions)

        Each imputation was calculated with %i samples from %i chains run for %i iterations.
    """ % (
        num_rows,
        num_cols,
        impute_samples,
        num_chains,
        num_iters,
    )

    if filename is None:
        filename = "Fills_in_blanks-iters=%i_chains=%i_T=%i.png" % (num_iters, num_chains, int(time.time()))

    ax = pylab.subplot(2, 1, 2)
    ax.text(0, 0.5, txt, fontsize=10)
    ax.axis("off")

    pylab.savefig(filename)
示例#10
0
def plot_recovers_original_densities(result, filename=None):

    pylab.rcParams.update({'font.size': 8})
    pylab.locator_params(nbins=3)
    fig = pylab.figure(num=None, figsize=(10,8), facecolor='w', edgecolor='k')

    plt = 0
    for shape in ["sinwave", "x", "ring", "dots"]:
        key = shape + "_original"
        X_original = result[key]
        ax = pylab.plt.subplot2grid((3,4), (0, plt))
        pylab.scatter( X_original[:,0], X_original[:,1], color='blue', edgecolor='none', alpha=.2 )
        pylab.title("%s original" % shape)
        ax.yaxis.set_major_locator(MaxNLocator(3))
        ax.xaxis.set_major_locator(MaxNLocator(3))

        key = shape + "_inferred"
        X_inferred = result[key]
        ax1 = pylab.plt.subplot2grid((3,4), (1, plt))
        pylab.scatter( X_inferred[:,0], X_inferred[:,1], color='red', edgecolor='none', alpha=.2 )
        pylab.xlim(ax.get_xlim())
        pylab.ylim(ax.get_ylim())
        pylab.title("%s inferred" % shape)
        ax1.yaxis.set_major_locator(MaxNLocator(3))
        ax1.xaxis.set_major_locator(MaxNLocator(3))

        plt += 1

    num_iters = result['config']['num_iters']
    num_rows = result['config']['num_rows']
    num_chains = result['config']['num_chains']

    txt = '''
        Top panels: zero-correlation datasets consisting of %i points. 
        Bottom panels: result of SIMULATE %i datapoints after running %i 
        chains for %i iterations with %s datatype.
    ''' % (num_rows, num_rows, num_chains, num_iters, result['config']['datatype'])

    pylab.plt.subplot2grid((3,3), (2,0), colspan=3)

    ax = pylab.gca()
    ax.text(0,.5,txt, fontsize=10)
    ax.axis('off')

    if filename is None:
        filename = "Recovers_original_densities-num_rows=%i-num_chains=%i-num_iters=%i-T=%i.png" % \
        (num_rows, num_chains, num_iters, int(time.time()))

    pylab.savefig(filename)
示例#11
0
文件: Dynamics.py 项目: airanmehr/bio
 def plotOne(df,maxGen=None,obs=False,mean=True,color='red'):
     m=df.mean(1)
     s=df.std(1)
     if maxGen is not None:
         m=m.loc[:maxGen]
         s=s.loc[:maxGen]
     plt.locator_params(nbins=4);
     if color=='red':
         m.plot(ax=plt.gca(),legend=False,linewidth=5,color=color)
     else:
         m.plot(ax=plt.gca(),legend=False,linewidth=3,color=color)
     #;plt.gca().axvspan(ft/3, ft/3*2, alpha=0.15, color='black')
     x=m.index.values
     m=m.values;s=s.values
     plt.gca().fill_between(x, m - 2 * s, m + 2 * s, color=color, alpha=0.3)
     plt.grid()
示例#12
0
def plot_fills_in_the_blanks(result, filename=None):
    pylab.rcParams.update({'font.size': 8})
    pylab.locator_params(nbins=3)
    fig = pylab.figure(num=None, figsize=(10,8), facecolor='w', edgecolor='k')

    prop_missing = result['config']['prop_missing']

    ax = pylab.subplot(2,1,1)
    pylab.plot(prop_missing, result['MSE_naive_bayes_indexer'], lw=3, c='red', label='Naive Bayes')
    pylab.plot(prop_missing, result['MSE_crp_mixture_indexer'], lw=3, c='blue', label='CRP mixture')
    pylab.plot(prop_missing, result['MSE_crosscat_indexer'], lw=3, c='green', label='CrossCat')

    ax.set_xticks(prop_missing)
    ax.yaxis.set_major_locator(MaxNLocator(3))
    pylab.ylabel('MSE')
    pylab.xlabel('proportion of data missing')
    pylab.legend(loc=4)

    num_iters = result['config']['num_iters']
    num_rows = result['config']['num_rows']
    num_cols = result['config']['num_cols']
    num_chains = result['config']['num_chains']
    impute_samples = result['config']['impute_samples']

    txt = '''
        Mean squared error over all imputed missing values (y-axis) for a %i rows by %i table with
        variable proportion of missing data (x-axis).

        Green line: BayesDB normal configuration
        Blue line: BayesDB CRP mixture configuration (one view, no column transitions)
        Red line: BayesDB Naive Bayes configuration (one view, one category, only hyperparameter
        transitions)

        Each imputation was calculated with %i samples from %i chains run for %i iterations.
    ''' % (num_rows, num_cols, impute_samples, num_chains, num_iters)

    if filename is None:
        filename = 'Fills_in_blanks-iters=%i_chains=%i_T=%i.png' % \
            (num_iters, num_chains, int(time.time()))

    ax = pylab.subplot(2,1,2)
    ax.text(0,.5,txt, fontsize=10)
    ax.axis('off')

    pylab.savefig( filename )
示例#13
0
def lagged_log_reg_plot(population_fit, n_back = 4, n_non_lag = 1, fig_no = 1, clf = True):
    pop_params = population_fit['pop_params']
    n_params = (len(pop_params['means']) - n_non_lag)/n_back
    means = pop_params['means']
    SDs = pop_params['SDs']
    p.figure(fig_no)
    if clf:p.clf()
    x = range(1,n_back + 1)
    for i in range(n_params):
        param_means = means[n_non_lag + i::n_params]
        param_SDs   =   SDs[n_non_lag + i::n_params]
        p.subplot(n_params, 1, i + 1)
        p.errorbar(x, param_means, param_SDs)
        p.ylabel(population_fit['param_names'][i+n_non_lag][:-2])
        p.plot([0.5,n_back + 0.5],[0,0],'k')
        p.locator_params(nbins = 4)
        p.xticks(range(1,n_back + 1),[])
        p.xlim(0.5,n_back + 0.5)
    p.xticks(range(1,n_back + 1),-np.arange(1,n_back + 1))
示例#14
0
def pop_fit_comparison(population_fit_A, population_fit_B, fig_no = 1, normalize = False):
    'Plot multiple population fits on the same axis for comparison.'
    p.figure(fig_no)
    p.clf()
    pop_params_A = population_fit_A['pop_params']
    pop_params_B = population_fit_B['pop_params']
    n_params = len(pop_params_A['ranges'])
    for i in range(n_params):
        p.subplot(1, n_params, i + 1)
        _plot_dist(pop_params_A['means'][i], pop_params_A['SDs'][i], 
                   pop_params_A['ranges'][i], col = 'b', normalize = normalize)
        _plot_dist(pop_params_B['means'][i], pop_params_B['SDs'][i], 
                   pop_params_B['ranges'][i], col = 'r', normalize = normalize)
        p.locator_params('x', nbins = 3)
        p.xlabel(population_fit_A['param_names'][i])
        p.yticks([])
    for i in range(n_params):
        p.subplot(1, n_params, i + 1)
        p.ylim([0,p.ylim()[1] * 1.1])
示例#15
0
def final_figure(n= 3):
	"""Make the final figure. For n (default = 3) values of r plot along the first row relative fitness.
	That is fitness of the full model minus (or over) fitness in the mixed case.
	The second row is Xa and the 3rd row is beta. """
	rs = np.linspace(0,1,n)
	pl.figure()
	for i,r in enumerate(rs):

		pl.subplot( 3, n, i+1)
		relative_fitness(r = r, steps = 200)
		if i == n - 1 and n <= 3:
			pl.colorbar( ticks = np.linspace(-1.2,1.2,5))
		pl.locator_params( nbins=4)

		pl.subplot( 3, n, n + i + 1)
		ST_xA(r=r,steps = 200)
		if i ==n-1 and n <=3:
			pl.colorbar( ticks = np.linspace(0,1,5))
		if i ==0:
			pl.ylabel("S", fontsize = 20)
		pl.locator_params( nbins=4)

		pl.subplot( 3, n, 2*n + i + 1)
		ST_beta(r=r,steps = 200)	
		if i ==n-1 and n <= 3:
			pl.colorbar( ticks = np.linspace(-1,1,5) )	
		if i ==n/2:
			pl.xlabel("T", fontsize = 20)
		pl.locator_params( nbins=4)

	pl.savefig("figures\\final_heatmap_%d.png"%n)
示例#16
0
文件: Plots.py 项目: airanmehr/bio
    def plotOne(b, d, name, chroms,common,shade):
        a = b.dropna()
        c = d.loc[a.index]
        if shade is not None:
            for _ ,  row in shade.iterrows():
                plt.gca().fill_between([row.gstart, row.gend], a.min(), a.max(), color='b', alpha=0.3)
        plt.scatter(a.index, a, s=markerSize, c=c, alpha=0.8, edgecolors='none')

        outliers=None
        if Outliers is not None:
            outliers=Outliers[name].dropna()
        if cutoff is not None:
            outliers = a[a >= cutoff[name]]
        elif top_k is not None:
            outliers = a.sort_values(ascending=False).iloc[:top_k]
        elif std_th is not None:
            outliers = a[a > a.mean() + std_th * a.std()]
        if outliers is not None:
            if len(outliers):
                # if name != 'Number of SNPs':
                plt.scatter(outliers.index, outliers, s=markerSize, c='r', alpha=0.8, edgecolors='none')
                    # plt.axhline(outliers.min(), color='k', ls='--')


        if common is not None:
            for ii in common.index: plt.axvline(ii,c='g',alpha=0.5)

        plt.axis('tight');
        plt.xlim(max(0,a.index[0]-10000), a.index[-1]);
        setSize(plt.gca(),ticksize)
        plt.ylabel(name, fontsize=ticksize * 1.5)

        # plt.title('{} SNPs, {} are red.'.format(a.dropna().shape[0], outliers.shape[0]))
        if chroms.shape[0]>1:
            plt.xticks([x for x in chroms.mid], [str(x) for x in chroms.index], rotation=-90, fontsize=ticksize * 1.5)
        plt.setp(plt.gca().get_xticklabels(), visible=False)
        plt.locator_params(axis='y', nbins=4)
        mpl.rc('ytick', labelsize=ticksize)
        if ylim is not None:    plt.ylim(ymin=ylim)
示例#17
0
def dists_and_hists_plot(population_fit, fig_no = 1):
    ''' Plot population distributions and histogram of MAP parameter estimates in true space.
    If simulated data, plot true and fitted data seperately.
    '''
    fig = p.figure(fig_no)
    #fig.tight_layout()
    p.clf()
    pop_params = population_fit['pop_params']
    param_ranges = pop_params['ranges']
    MAP_params_T   = np.array([MAP_fit['params_T'] for MAP_fit  in population_fit['MAP_fits'] ])
    n_params = np.shape(MAP_params_T)[1]

    n_rows = 1
    if False:#population_fit.has_key('pop_params_true') & \
        #(n_params == len(population_fit['pop_params_true']['means'])):
        n_rows = 2 
        pop_params_true = population_fit['pop_params_true']
        if isinstance(pop_params_true['SDs'], float):
            pop_params_true['SDs'] = np.ones(n_params) * pop_params_true['SDs']
        true_params_T = np.array(population_fit['true_values']['params_T'])
        for i in range(n_params):
            p.subplot(2,n_params,i+1)
            _plot_dist_and_hist(pop_params_true['means'][i], pop_params_true['SDs'][i],
                               true_params_T[:,i], pop_params['ranges'][i], 'r')
            p.locator_params(nbins = 4)
            p.subplot(2, n_params, n_params + i + 1)
            _plot_dist_and_hist(pop_params_true['means'][i], pop_params_true['SDs'][i],
                               None, pop_params['ranges'][i], 'r')

    for i in range(n_params):
        p.subplot(n_rows, n_params, (n_rows-1) * n_params + i + 1)
        _plot_dist_and_hist(pop_params['means'][i], pop_params['SDs'][i],
                           MAP_params_T[:,i], pop_params['ranges'][i],'k')
        p.locator_params(nbins = 4)
        p.yticks([])
        p.xlabel(population_fit['param_names'][i])
        if param_ranges[i] == 'unit':
            p.xlim(0,1)
示例#18
0
    def _plot(y, yerr, MAP_params, rng, param_names):
        n_ses, n_params = MAP_params.shape
        if ebars:
            plt.errorbar(np.arange(n_params)+0.5+x_offset, y, yerr, linestyle='',
                     linewidth=2, color=col, marker='_', markersize=10)
        else:
            plt.plot(np.arange(n_params)+0.5+x_offset, y, linestyle='', marker='.',
                     markersize=6, color=col)
        if scatter:
            for i in range(n_params):
                plt.scatter(i+0.4+x_offset+0.2*np.random.rand(n_ses), MAP_params[:,i],
                            s=4,  facecolor=col, edgecolors='none', lw=0)

        plt.xlim(0,n_params)
        plt.xticks(np.arange(n_params)+0.5, param_names, rotation=-45, ha='left')

        if rng == 'unc':
            plt.plot(plt.xlim(),[0,0],'k', linewidth=0.5)
        elif rng == 'unit': 
            plt.ylim(0,1)
        elif rng == 'pos':
            plt.ylim(0, max(plt.ylim()[1], np.max(MAP_params) * 1.2))

        plt.locator_params('y', nbins=5)
def plot(data, w, title):
    print title
    w = pywt.Wavelet(w)
    a = data
    ca = []
    cd = []
    for i in xrange(5):
        (a, d) = pywt.dwt(a, w, mode)
        ca.append(a)
        cd.append(d)

    rec_a = []
    rec_d = []

    for i, coeff in enumerate(ca):
        coeff_list = [coeff, None] + [None] * i
        rec_a.append(pywt.waverec(coeff_list, w))

    for i, coeff in enumerate(cd):
        coeff_list = [None, coeff] + [None] * i
        rec_d.append(pywt.waverec(coeff_list, w))

    pylab.figure()
    ax_main = pylab.subplot(len(rec_a) + 1, 1, 1)
    pylab.title(title)
    ax_main.plot(data)
    pylab.xlim(0, len(data) - 1)
    pylab.locator_params(axis = 'y', nbins = 5)
    pylab.subplots_adjust(hspace=.5)

    for i, y in enumerate(rec_a):
        #print len(data), len(x), len(data) / (2**(i+1))
        ax = pylab.subplot(len(rec_a) + 1, 2, 3 + i * 2)
        ax.plot(y, 'r')
        pylab.xlim(0, 65)
        pylab.ylabel("Coef%d" % (i + 1))
        pylab.locator_params(axis = 'y', nbins = 5)


    for i, y in enumerate(rec_d):
        ax = pylab.subplot(len(rec_d) + 1, 2, 4 + i * 2)
        ax.plot(y, 'g')
        pylab.xlim(0, 65)
        #pylab.ylim(min(0,1.4*min(x)), max(0,1.4*max(x)))
        pylab.ylabel("D%d" % (i + 1))
        pylab.locator_params(axis = 'y', nbins = 5)
示例#20
0
def plot(data, w, title):
    print title
    w = pywt.Wavelet(w)
    a = data
    ca = []
    cd = []
    for i in xrange(5):
        (a, d) = pywt.dwt(a, w, mode)
        ca.append(a)
        cd.append(d)

    rec_a = []
    rec_d = []

    for i, coeff in enumerate(ca):
        coeff_list = [coeff, None] + [None] * i
        rec_a.append(pywt.waverec(coeff_list, w))

    for i, coeff in enumerate(cd):
        coeff_list = [None, coeff] + [None] * i
        rec_d.append(pywt.waverec(coeff_list, w))

    pylab.figure()
    ax_main = pylab.subplot(len(rec_a) + 1, 1, 1)
    pylab.title(title)
    ax_main.plot(data)
    pylab.xlim(0, len(data) - 1)
    pylab.locator_params(axis='y', nbins=5)
    pylab.subplots_adjust(hspace=.5)

    for i, y in enumerate(rec_a):
        #print len(data), len(x), len(data) / (2**(i+1))
        ax = pylab.subplot(len(rec_a) + 1, 2, 3 + i * 2)
        ax.plot(y, 'r')
        pylab.xlim(0, 65)
        pylab.ylabel("Coef%d" % (i + 1))
        pylab.locator_params(axis='y', nbins=5)

    for i, y in enumerate(rec_d):
        ax = pylab.subplot(len(rec_d) + 1, 2, 4 + i * 2)
        ax.plot(y, 'g')
        pylab.xlim(0, 65)
        #pylab.ylim(min(0,1.4*min(x)), max(0,1.4*max(x)))
        pylab.ylabel("D%d" % (i + 1))
        pylab.locator_params(axis='y', nbins=5)
示例#21
0
def contourTri(chain, **kwargs):
    """
    #Given a chain, labels and a list of which parameters to plot, plots the contours
    # Arguments:
    # chain=an array of the chain (not using weights, i.e. each row counts only once)
    # p= a list of integers: the two parameters you want to plot (refers to two columns in the chain)
    #kwargs:        labels= the labels of the parameters (list of strings)
    #               col=a tuple of the two colours for the contour plot
    #               line=boolean whether or not to just do a line contour plot
    #               outfile='triangle.png'
    #               binsize=50
    #               reconstruct=boolean whether or not to plot reconstruction
    #               autoscale=boolean whether or not to autoscale axes
    #               ranges=dictionary of plot range lists, labelled by
    #                      parameter name, e.g. {'A':[0.0,1.0],etc.}
    #               title=outdir
    p is now ignored
    """

    # Collate the contour-region info
    bundle = chain

    TRUNCATE_C = False
    FONTSIZE = 15
    ROTATION = 60.0
    FIGSIZE = (8.27, 11.69)
    DPI = 600
    #FIGSIZE=(8.27,11.69); DPI=400
    AXIS_LABEL_OFFSET = -0.5  #-0.7

    X_LABEL_OFFSET = -0.35  #-0.35    #-0.8
    Y_LABEL_OFFSET = -0.4  #-0.4    #-0.6

    # !!!! BEWARE THE BINSIZE --- PLOT IS A STRONG FUNCTION OF THIS
    if 'binsize' in kwargs:
        binsize = kwargs['binsize']
    else:
        binsize = 50
    print 'Using binsize = %i' % binsize

    if 'labels' in kwargs:
        labels = kwargs['labels']
        parameters = labels  # How did this ever work without??
    else:
        labels = ['x', 'y']

    if 'ranges' in kwargs:
        ranges = kwargs['ranges']
    else:
        ranges = None

    if 'title' in kwargs:
        title = kwargs['title']
    else:
        title = ''

    if 'autoscale' in kwargs:
        autoscale = kwargs['autoscale']
    else:
        autoscale = True

    p = range(len(labels))
    pairs = trianglePairs(p)
    nparams = len(p)

    # Start setting up the plot
    ipanel = 0
    ax = {}
    pylab.clf()
    for panel in pairs:
        ipanel += 1
        H, xedges, yedges = numpy.histogram2d(chain[:, panel[0]],
                                              chain[:, panel[1]],
                                              bins=(binsize, binsize))

        x = []
        y = []
        z = []
        for i in range(len(xedges[:-1])):
            for j in range(len(yedges[:-1])):
                x.append(xedges[:-1][i])
                y.append(yedges[:-1][j])
                z.append(H[i, j])

        SMOOTH = False
        if SMOOTH:
            sz = 50
            smth = 80e6
            spl = interpolate.bisplrep(x, y, z, s=smth)
            X = numpy.linspace(min(xedges[:-1]), max(xedges[:-1]), sz)
            Y = numpy.linspace(min(yedges[:-1]), max(yedges[:-1]), sz)
            Z = interpolate.bisplev(X, Y, spl)
        else:
            X = xedges[:-1]
            Y = yedges[:-1]
            Z = H

        #I think this is the weird thing I have to do to make the contours work properly
        X1 = numpy.zeros([len(X), len(X)])
        Y1 = numpy.zeros([len(X), len(X)])
        for i in range(len(X)):
            X1[:, i] = X
            Y1[i, :] = Y
        X = X1
        Y = Y1

        N100, N95, N68 = findconfidence(Z)

        if 'col' in kwargs:
            col = kwargs['col']
        else:
            col = ('#a3c0f6', '#0057f6')  #A pretty blue

        # Now construct the subplot
        ax[ipanel] = pylab.subplot2grid((nparams, nparams),
                                        panel[::-1])  # Reverse quadrant

        if 'line' in kwargs and kwargs['line'] == True:
            CS = pylab.contour(X,
                               Y,
                               Z,
                               levels=[N95, N68, N100],
                               colors=col,
                               linewidth=100)
        else:
            CS = pylab.contourf(X, Y, Z, levels=[N95, N68, N100], colors=col)

        # Identify points lying within 68 percent contour region
        #print ipanel,bundle.shape,chain.shape
        #v=CS.collections[0].get_paths()[0].vertices
        #w=CS.collections[1].get_paths()[0].vertices
        #print v[:,0]
        #print v[:,1]
        #b=bundle[:,[panel[0],panel[1]]]
        #mask=Path(v).contains_points(b)
        #mask2=Path(w).contains_points(b)
        #print panel[0],panel[1],b[:,0].size,b[:,0][mask].size,b[:,0][mask2].size,labels[panel[0]],labels[panel[1]]

        if 'truth' in kwargs and kwargs['truth'] is not None:
            truth = kwargs['truth']
            #pylab.plot(truth[labels[panel[0]]],truth[labels[panel[1]]],'r+',\
            #markersize=20)

        if 'labelDict' in kwargs and kwargs['labelDict'] is not None:
            labelDict = kwargs['labelDict']
        else:
            labelDict = dict((name, name) for name in parameters)

        # Set the axis labels only for left and bottom:
        #print ax[ipanel].get_xlabel(),ax[ipanel].get_ylabel()
        if panel[1] == (nparams - 1):
            ax[ipanel].set_xlabel(labelDict[labels[panel[0]]], fontsize=18)
            ax[ipanel].xaxis.set_label_coords(
                0.5, X_LABEL_OFFSET)  # align axis labels
            ax[ipanel].xaxis.set_major_formatter(FormatStrFormatter('%.2f'))
            ax[ipanel].yaxis.set_major_formatter(FormatStrFormatter('%.2f'))

        else:
            ax[ipanel].set_xlabel('')
            ax[ipanel].get_xaxis().set_ticklabels([])
        if panel[0] == 0:
            ax[ipanel].set_ylabel(labelDict[labels[panel[1]]], fontsize=18)
            ax[ipanel].yaxis.set_label_coords(Y_LABEL_OFFSET,
                                              0.5)  # align axis labels
            #ax[ipanel].xaxis.set_major_formatter(FormatStrFormatter('%.2f'))
            ax[ipanel].yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
        else:
            ax[ipanel].set_ylabel('')
            ax[ipanel].get_yaxis().set_ticklabels([])

        # Set plot limits
        if autoscale:
            # HACK FOR C ONLY:
            if TRUNCATE_C and panel[0] == 0:
                xxlo, xxhi = ax[ipanel].xaxis.get_data_interval()
                if xxhi > 1.0e4:
                    pylab.xlim(xxlo, 1.0e4)
                #ax[ipanel].set_xscale('log')
                autoscale = True
                #locs,labels = plt.xticks()
                #plt.xticks(locs, map(lambda x: "%g" % x, locs*1.0e5))
        else:
            xlo = ranges[labels[panel[0]]][0]
            xhi = ranges[labels[panel[0]]][1]
            ylo = ranges[labels[panel[1]]][0]
            yhi = ranges[labels[panel[1]]][1]
            pylab.xlim(xlo, xhi)
            pylab.ylim(ylo, yhi)

        # Some housekeeping
        pylab.xticks(fontsize=FONTSIZE, rotation=ROTATION)
        pylab.yticks(fontsize=FONTSIZE, rotation=0)
        pylab.locator_params(nbins=4)

    # Set up the 1-D plots on the diagonal
    for iparam in range(nparams):
        #        b=numpy.histogram(R,bins=bins)
        J, edges = numpy.histogram(chain[:, iparam],
                                   density=True,
                                   bins=binsize)
        ax1d = pylab.subplot2grid((nparams, nparams), (iparam, iparam))
        pylab.plot(edges[:-1], J, color='k')
        #print iparam,nparams,labels[iparam]

        if 'truth' in kwargs and kwargs['truth'] is not None:
            truth = kwargs['truth']
            #pylab.axvline(truth[parameters[iparam]],color='g')

        if iparam == 0:
            ax1d.set_ylabel(labelDict[labels[iparam]], fontsize=18)
            ax1d.yaxis.set_label_coords(Y_LABEL_OFFSET,
                                        0.5)  # align axis labels
        if iparam == (nparams - 1):
            ax1d.set_xlabel(labelDict[labels[iparam]], fontsize=18)
            ax1d.xaxis.set_label_coords(0.5,
                                        X_LABEL_OFFSET)  # align axis labels

        # Set plot limits
        #parameters=['x', 'y', 'S', 'sig', 'Q', 'el', 'em', 'R']
        if autoscale:
            # HACK FOR C ONLY:
            if TRUNCATE_C and iparam == 0:
                xxlo, xxhi = ax1d.xaxis.get_data_interval()
                if xxhi > 1.0e4:
                    pylab.xlim(xxlo, 1.0e4)
                #ax1d.set_xscale('log')
                autoscale = True
        if not autoscale:
            xlo, xhi = ranges[parameters[iparam]]
            pylab.xlim(xlo, xhi)
            if TRUNCATE_C and iparam == 0:
                xxlo, xxhi = ax1d.xaxis.get_data_interval()
                if xxhi > 1.0e4:
                    pylab.xlim(xxlo, 1.0e4)

        if iparam < (nparams - 1):
            ax1d.get_xaxis().set_ticklabels([])
        ax1d.get_yaxis().set_ticklabels([])
        pylab.xticks(fontsize=FONTSIZE, rotation=ROTATION)
        pylab.yticks(fontsize=FONTSIZE)
        pylab.locator_params(nbins=4)
        #if iparam == 0: ax1d.set_xscale('log')
    #ax1d.set_xscale('linear')

    #axinfo=pylab.subplot2grid((nparams,nparams),(0,nparams-3))
    axinfo = pylab.subplot2grid((nparams, nparams),
                                (0, nparams - nparams % 2 - 1))
    axinfo.get_xaxis().set_visible(False)
    axinfo.get_yaxis().set_visible(False)
    pylab.axis('off')
    pylab.title(title)

    # Plot the truth - this needs to be generalized for non-lumfunc
    if 'truth' in kwargs and kwargs['truth'] is not None:
        truth = kwargs['truth']
        note = ['nparams %i\n truth:' % nparams]
        for k, v in truth.items():
            notelet = '%s = %4.2e' % (k, v)
            note.append(notelet)
        #pylab.text(-1,-1,'\n'.join(note))

        if 'reconstruct' in kwargs:
            reconstruct = kwargs['reconstruct']
            axrecon=pylab.subplot2grid((nparams,nparams),(0,nparams-2),\
                                       rowspan=2,colspan=2)
            axrecon.set_xscale('log')
            axrecon.set_yscale('log')
            pylab.xticks(fontsize=FONTSIZE, rotation=60)
            pylab.yticks(fontsize=FONTSIZE)
            pylab.locator_params(nbins=5)
            median_bins = medianArray(reconstruct[0])
            dnds = calculateDnByDs(median_bins, reconstruct[1])
            dndsN = calculateDnByDs(median_bins, ksNoisy)
            print median_bins
            print dnds
            print 'truth items \n'
            print truth.items()
            recon = numpy.zeros(numpy.shape(median_bins))
            post = numpy.zeros(numpy.shape(median_bins))
            print '# i Smedian ks dnds dndsS2.5 NRecon dndsRecon dndsS2.5Recon log10dnds log10dndsR diffR dndsN'
            if nparams == 4:
                (C,alpha,Smin,Smax)\
                  =(truth['C'],truth['alpha'],truth['Smin'],truth['Smax'])
                area = 10.0  # Hack
                # Reconstruct powerLaw points given truth
                for i in range(len(median_bins)):
                    recon[i]=powerLawFuncS(median_bins[i],\
                                                   C,alpha,Smin,Smax,area)
                    post[i]=powerLawFuncS(median_bins[i],\
                                                  9.8,-0.63,0.04,14.1,area)
                #recon *= lumfunc.ksRaw
                #dndsR=lumfunc.calculateDnByDs(median_bins,recon)
                # **** XXXX Where does the 1000 come from!? :(( XXXX
                dndsR = recon * 1000.0
                dndsP = post * 1000.0
                # cols: i Smedian ks dnds dndsS2.5 NRecon dndsRecon
                # dndsS2.5Recon log10dnds log10dndsR diffR dndsN
                for i in range(len(median_bins)):
                    print '%i %f %i %i %i %i %i %i %f %f %i %i' % (i,median_bins[i],\
                                                  reconstruct[-1][i],dnds[i],\
                      dnds[i]*median_bins[i]**2.5,recon[i],dndsR[i],\
                      dndsR[i]*median_bins[i]**2.5,numpy.log10(dnds[i]),\
                        numpy.log10(dndsR[i]),int(dndsR[i]-dnds[i]),dndsN[i])

                    #print recon
            pylab.xlim(reconstruct[0][0], reconstruct[0][-1])
            #pylab.ylim(1.0e2,1.0e8)

            #pylab.plot(median_bins,dnds*numpy.power(median_bins,2.5)*lumfunc.sqDeg2sr,'+')
            power = 2.5
            pylab.plot(median_bins,
                       dnds * sqDeg2sr * numpy.power(median_bins, power), '+')
            pylab.plot(median_bins,
                       dndsR * sqDeg2sr * numpy.power(median_bins, power), '-')
            pylab.plot(median_bins,
                       dndsN * sqDeg2sr * numpy.power(median_bins, power), '+')
            pylab.plot(median_bins,
                       dndsP * sqDeg2sr * numpy.power(median_bins, power), '-')
            #pylab.plot(dnds,dndsR*numpy.power(median_bins,1.0))
            #b=lumfunc.simtable(lumfunc.bins,a=-1.5,seed=1234,noise=10.0,dump=False)

    if 'outfile' in kwargs:
        outfile = kwargs['outfile']
        pylab.subplots_adjust(wspace=0, hspace=0)
        #pylab.show()
        pylab.savefig(outfile, figsize=FIGSIZE, dpi=DPI)
        print 'Run: open %s' % outfile
        pylab.close()
    else:
        pylab.show()

    return bundle
示例#22
0
文件: Plot.py 项目: airanmehr/bio
def plotBias():
    def computeBias():  # s-shat
        print 'computing bias...'
        a = pd.read_pickle('{}ROC/{}'.format(utl.outpath, 'HMM')).s
        gp = pd.read_pickle(utl.outpath + 'ROC/GP.causal').s
        b = pd.concat([a, gp]).sort_index().xs(True, level='causal')

        bias = b.groupby(level=range(4)).apply(lambda x: x.name[-1] - x)
        bias.to_pickle('{}ROC/bias.df'.format(utl.outpath))

    def biash():
        a = pd.read_pickle('{}ROC/bias.df'.format(utl.outpath))
        a = a[a.index.get_level_values('method') == 'HMM']
        a = a + np.random.randn(a.size)
        a /= 10
        a[a.index.get_level_values('nu0') == 0.1] /= 2
        for name, g in a.groupby(level=range(4)):
            g -= g.mean();
        a.to_pickle('{}ROC/bias.h.df'.format(utl.outpath))

    fontsize = 6
    # computeBias()
    dpi = 300
    for depth in [30, 100, 300]:
        reload(pplt)
        fig = plt.figure(figsize=(5, 5), dpi=dpi)
        j = 0
        df = pd.read_pickle(utl.outpath + 'ROC/bias.df').loc[depth].reset_index()
        df.method = df.method.replace({'HMM': comaleName})
        df = df.set_index(pd.read_pickle(utl.outpath + 'ROC/bias.df').loc[depth].index.names).sort_index().s
        dfh = pd.read_pickle(utl.outpath + 'ROC/bias.h.df').loc[depth].reset_index()
        dfh.method = dfh.method.replace({'HMM': comaleName})
        dfh = dfh.set_index(pd.read_pickle(utl.outpath + 'ROC/bias.h.df').loc[depth].index.names).sort_index().s
        df[(0.1, comaleName)] += np.random.rand(df[(0.1, comaleName)].shape[0]) / 100 - 0.005
        # df[(0.005, 'HMM')] += np.random.rand(df[(0.005, 'HMM')].shape[0]) / 100 - 0.005
        ax = []
        for nu0 in [0.005, 0.1]:
            if j == 0:
                ax += [plt.subplot(2, 2, j + 1)]
            else:
                ax += [plt.subplot(2, 2, j + 1, sharex=ax[-1], sharey=ax[-1])]
            a = pd.DataFrame(df.loc[nu0])
            a.columns = ['bias']
            a['method'] = a.index.get_level_values('method')
            a['s'] = a.index.get_level_values('S')
            sns.violinplot(x="s", y="bias", hue="method", data=a, linewidth=1,
                           palette={comaleName: "r", "GP": "darkblue"}, split=True, ax=ax[j]);
            if j < 2: ax[j].set_title(r'$\nu_0=${} ({} Sweep)'.format(nu0, ('Soft', 'Hard')[nu0 == 0.005]),
                                      fontsize=fontsize + 2)
            # pplt.annotate(r'$\nu_0=${} ({} Sweep)'.format(nu0, ('Soft', 'Hard')[nu0 == 0.005]),xpad=0.05,ypad=1,fontsize=fontsize)
            plt.locator_params(axis='y', nbins=5)
            pplt.setSize(plt.gca(), fontsize)
            ax[j].set_xlabel('$s$', fontsize=fontsize + 2)
            # plt.xlabel('$s$', fontsize=fontsize + 2)
            ax[j].legend(title='', loc='upper right', fontsize=fontsize + 2)
            ax[j].set_ylabel(('Bias ($s-\hat{s}$)', '')[j % 2], fontsize=fontsize + 2)

            if j != 1: ax[j].legend_.remove()
            pplt.annotate(ABCD[j], fontsize=fontsize + 2, ax=ax[j])

            j += 1
        for nu0 in [0.005, 0.1]:
            if j == 2:
                ax += [plt.subplot(2, 2, j + 1)]
            else:
                ax += [plt.subplot(2, 2, j + 1, sharex=ax[-1], sharey=ax[-1])]
            a = pd.DataFrame(dfh.loc[nu0])
            a.columns = ['bias']
            a['method'] = a.index.get_level_values('method')
            a['s'] = a.index.get_level_values('S')
            sns.violinplot(x="s", y="bias", hue="method", data=a[a['method'] == comaleName], linewidth=1,
                           palette={comaleName: "r", "GP": "g"}, ax=ax[j]);
            plt.locator_params(axis='y', nbins=5)
            pplt.setSize(plt.gca(), fontsize)
            ax[j].set_xlabel('$h$', fontsize=fontsize + 2)
            ax[j].set_ylabel(('Bias ($h-\hat{h}$)', '')[j % 2], fontsize=fontsize + 2)
            pplt.annotate(ABCD[j], fontsize=fontsize + 2, ax=ax[j])
            ax[j].legend_.remove()
            j += 1
        df = df.groupby(level=['method', 'nu0']).describe().round(3).unstack(['method', 'nu0']).loc[
            ['mean', 'std']].T.reset_index().sort_values('nu0')
        df.columns = ['Method', r'$\nu_0$', 'Mean', 'STD']
        utl.DataframetolaTexTable(df, fname=utl.paperPath + 'tables/bias.{}.tex'.format(depth))
        pplt.savefig('bias.{}'.format(depth), dpi)
示例#23
0
def run_plots(args, scores, all_tasks, keys):
    global gs
    if args.colorcycle:
        if ',' in args.colorcycle:
            lst = args.colorcycle.split(',')
        else:
            lst = list(args.colorcycle)
        rc('axes', prop_cycle=matplotlib.cycler('color', lst))

    rc('lines', linewidth=args.lw)
    title = args.title
    if not title:
        title = os.path.split(args.files[0])[-2]
    pylab.suptitle(title, size=18)
    goal_xlim = None
    axes = [[None for _ in range(len(all_tasks))] for _ in range(len(keys))]

    figkws = {}
    if args.figsize:
        figkws['figsize'] = map(int, args.figsize.split(','))
    fig = pylab.figure(1, **figkws)
    task_overlays = args.overlay
    if gs is None:
        gs = gridspec.GridSpec(len(keys), len(all_tasks) / task_overlays)
    for ki, key in enumerate(keys):
        for i, task in enumerate(all_tasks):
            full_plot_index = ki * len(all_tasks) + i
            plot_index = full_plot_index // task_overlays
            if args.only_plot and plot_index + 1 != int(
                    args.only_plot.split(',')[0]):
                continue
            print("Subplot %s/%s" %
                  (full_plot_index + 1, len(all_tasks) * len(keys)))
            sharex = axes[0][i]
            if args.only_plot:
                newloc = int(args.only_plot.split(',')[1])
                ax = fig.add_subplot(gs[newloc - 1])
                axes[ki][i] = ax
            else:
                axes[ki][i] = fig.add_subplot(gs[plot_index], sharex=sharex)
            if ki == len(keys) - 1 and args.startx:
                plot_startx(key)
            if i == 0 and args.starty:
                plot_starty(key)
            order = get_value(args.order, i)
            if order:
                order = map(int, order.split(','))
            plot_all(plot_results,
                     scores,
                     column=key,
                     taskset=[task],
                     order=order)
            if not args.global_legend and (not args.one_legend or
                                           (ki == len(keys) - 1 and
                                            (i == len(all_tasks) - 1 or 1))):
                pylab.legend(loc=legend_locs.get(key, 0))
            if not args.titles:
                pylab.title('Task %s' % task)
            else:
                pylab.title(args.titles.split('|')[plot_index])
            maxy = None
            if key in ('score', 'errors', 'seq-errors'):
                maxy = 1
                axes[ki][i].yaxis.set_major_formatter(
                    mtick.FuncFormatter(lambda x, pos: '% 2d\\%%' % (x * 100)))
            ylims = map(float,
                        get_value(args.ylims,
                                  ki).split(',')) if args.ylims else (0, 1)
            pylab.ylim(ylims)
            xlims = map(float,
                        get_value(args.xlims,
                                  i).split(',')) if args.xlims else (0, None)
            pylab.xlim(xlims)

            if args.nbinsx:
                pylab.locator_params(axis='x',
                                     nbins=int(get_value(args.nbinsx, i)))
            if args.nbinsy:
                pylab.locator_params(axis='y',
                                     nbins=int(get_value(args.nbinsy, ki)))
            if args.yticks:
                pylab.yticks(map(float, get_value(args.yticks, ki).split(',')))

            axes[ki][i].xaxis.set_major_formatter(
                mtick.FuncFormatter(lambda x, pos: '%dk' % (x // 1000)
                                    if x else '0'))
    rect = [0, 0, 1, .92]
    if args.global_legend:
        if not args.only_plot:
            ax = [row for row in axes if row[0]][0][0]
        lines, labels = ax.get_legend_handles_labels()
        my_labels = args.global_legend.split('|')
        if my_labels == ['1']:
            my_labels = labels
        if my_labels != ['0']:
            if my_labels != ['2']:
                fig.legend(lines,
                           my_labels,
                           loc='lower center',
                           ncol=2,
                           labelspacing=0.)
            rect = [0, 0.1, 1, 0.92]
    gs.tight_layout(fig, rect=rect)
    if args.save_to:
        pylab.savefig(args.save_to)
    else:
        pylab.show()
示例#24
0
def plot(best, best_rs, best_v, best_d, best_r):

    init_plotting_isi(16, 8)

    #___________________Plot final results
    #

    if eval(inp_dic['SYNTHETIC_F']):

        vel_list = [velocity_min, velocity_max, best_v, velocity_real]
        dep_list = [depth_min, depth_max, best_d, depth_real]
        vpvs_list = [vpvs_min, vpvs_max, best_r, vpvs_real]
        colors = ['k', 'k', 'b', 'r']
        labels = ['Min', 'Max', 'Best', 'Real']

    else:

        vel_list = [
            velocity_min,
            velocity_max,
            best_v,
        ]
        dep_list = [depth_min, depth_max, best_d]
        vpvs_list = [vpvs_min, vpvs_max, best_r]
        colors = ['r', 'k', 'g']
        labels = ['Min', 'Max', 'Best']

    ax = plt.subplot(121)
    [i.set_linewidth(0.6) for i in ax.spines.itervalues()]

    for v, d, c, l in zip(vel_list, dep_list, colors, labels):

        xs = []
        ys = []

        x = array(v)
        y = array(d)

        for i, j in zip(x, y):

            xs.append(i)
            xs.append(i)
            ys.append(j)
            ys.append(j)

        xs.pop(-1)
        ys.pop(0)
        xs.append(xs[-1])
        ys.append(max(inp_dic['PLT_DP_RNG']))

        if l == 'Min':
            ax.plot(array(xs),
                    -array(ys),
                    linewidth=1.5,
                    color='k',
                    linestyle='--',
                    label=l)
        elif l == 'Max':
            ax.plot(array(xs),
                    -array(ys),
                    linewidth=1.5,
                    color='k',
                    linestyle='-.',
                    label=l)
        else:
            ax.plot(array(xs),
                    -array(ys),
                    linewidth=1.5,
                    color=c,
                    linestyle='-',
                    label=l)

    ax.set_xlabel('Velocity [km/s]')
    ax.set_ylabel('Depth [km]')
    ax.set_xlim(inp_dic['PLT_VP_RNG'])
    ax.set_ylim(-array(inp_dic['PLT_DP_RNG'])[::-1])
    ax.locator_params(axis='x', nbins=6)
    ax.locator_params(axis='y', nbins=6)
    ax.grid()
    ax.legend(loc=3)

    ax = plt.subplot(122)
    [i.set_linewidth(0.6) for i in ax.spines.itervalues()]

    for r, d, c, l in zip(vpvs_list, dep_list, colors, labels):

        xs = []
        ys = []

        x = array(r)
        y = array(d)

        for i, j in zip(x, y):

            xs.append(i)
            xs.append(i)
            ys.append(j)
            ys.append(j)

        xs.pop(-1)
        ys.pop(0)
        xs.append(xs[-1])
        ys.append(max(inp_dic['PLT_DP_RNG']))

        if l == 'Min':
            ax.plot(array(xs),
                    -array(ys),
                    linewidth=1.5,
                    color='k',
                    linestyle='--',
                    label=l)
        elif l == 'Max':
            ax.plot(array(xs),
                    -array(ys),
                    linewidth=1.5,
                    color='k',
                    linestyle='-.',
                    label=l)
        else:
            ax.plot(array(xs),
                    -array(ys),
                    linewidth=1.5,
                    color=c,
                    linestyle='-',
                    label=l)

    ax.set_xlabel('VpVs [km/s]')
    ax.set_ylabel('Depth [km]')
    ax.set_xlim(inp_dic['PLT_R_RNG'])
    ax.set_ylim(-array(inp_dic['PLT_DP_RNG'])[::-1])
    ax.locator_params(axis='x', nbins=6)
    ax.locator_params(axis='y', nbins=6)
    ax.grid()
    ax.legend(loc=2)

    plt.tight_layout()
    plt.savefig(dbase_name + '.tiff', dpi=300)
    plt.close()

    #__________StdDev (V,D,R)

    models = loadtxt('models.dat')
    model_std = std(models, axis=0)[0]
    model_men = mean(models, axis=0)[0]
    best = array([best_v, best_d, best_r]).flatten()
    tot_ax = ceil(models.shape[1] / 4.)

    init_plotting_isi(16, 16)
    plt.rcParams['xtick.labelsize'] = 6
    plt.rcParams['ytick.labelsize'] = 6
    plt.rcParams['axes.labelsize'] = 6

    for par in range(models.shape[1]):

        ax = plt.subplot(tot_ax, 4, par + 1)
        [i.set_linewidth(0.6) for i in ax.spines.itervalues()]

        ax.text(0.50,
                1.18,
                'Parameter-%d' % (par + 1),
                fontsize=6,
                transform=ax.transAxes,
                ha='center',
                va='top')

        mu = mean(models[:, par])
        sig = std(models[:, par])
        x = linspace(mu - 3 * sig, mu + 3 * sig, 100)
        data = models[:, par]
        a, b, c = plt.hist(data, 50, color='r', linewidth=0, alpha=.6)
        plt.vlines(best[par], 0, max(a), color='g', zorder=20, linewidth=2)
        plt.vlines(mu, 0, max(a), color='b', zorder=20, linewidth=2)
        plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
        plt.locator_params(axis='x', nbins=6)
        plt.locator_params(axis='y', nbins=4)

    plt.tight_layout(True)
    plt.savefig('models_stat.tiff', dpi=300)
    plt.close()
示例#25
0
def firasPlot(individualDataDict,
			  xaxislabel,
			  yaxislabel,
			  graphType=None,
			  colormap = 'YlOrRd'):
	'''This produces the bubble plots '''

	xL = []
	yL = []
	area = [] 
	colour = []

	areaScalingFactor = 1.7E4

	# Plot the y=x line

	plt = pylab.subplot(111)

	pylab.plot(numpy.arange(0,100),numpy.arange(0,100),linewidth=4,color='g')
	pylab.xlabel(xaxislabel,fontsize=30)
	pylab.ylabel(yaxislabel,fontsize=30)
	pylab.text(1.5,1.2,'No-Change line',fontsize=12)
	# Set the axis limits
	pylab.tick_params(axis='both', which='major', labelsize=16)
	pylab.xlim(0,100)
	pylab.ylim(0,100)	
	for it,val in individualDataDict.iteritems():

		try:
			coord = [float(g) for g in it.strip('[]').split(',')]
		except ValueError: # excludes all blank keys
			continue
		xL.append(coord[0])
		yL.append(coord[1])
		area.append(areaScalingFactor*val/sum(individualDataDict.values()))
		colour.append('k')

	if graphType is None:

		for it,val in individualDataDict.iteritems():

			try:
				coord = [float(g) for g in it.strip('[]').split(',')]
			except ValueError: # excludes all blank keys
				continue		

			if val > 0:

				pylab.text(coord[0],
						   coord[1],
						   val,
						   size=16,
						   horizontalalignment='center',
						   verticalalignment='center',
						   color='w')  		

		sct = pylab.scatter(xL, 
							yL, 
							s=area, 
							c = colour, 
							linewidths=2, 
							edgecolor='k')
		#sct.set_alpha(0.65)

		pylab.locator_params(axis = 'x', nbins = 6)
		pylab.locator_params(axis = 'y', nbins = 6)

	elif graphType == 'heatMap':
		heatmap = numpy.empty(shape=[101,101])*numpy.nan

		for xcoord,ycoord,areaVal in zip(xL,yL,area):
			# Get back the number of students
			n = areaVal*sum(individualDataDict.values()) / areaScalingFactor
			heatmap[ycoord,xcoord] = n

			# pylab.text(xcoord,
			# 			ycoord,
			# 			math.trunc(n),
			# 			size=16,
			# 			horizontalalignment='center',
			# 			verticalalignment='center',
			# 			color='k') 
		# http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.block_reduce
		heatmap_reduced = block_reduce(heatmap, block_size=(10, 10), func=scipy.nansum)
		pylab.imshow(heatmap_reduced,
					 interpolation='None',
					 origin='lower',
					 cmap=colormap)			

		pylab.colorbar()
		pylab.xlabel(xaxislabel,fontsize=30)
		pylab.ylabel(yaxislabel,fontsize=30)
		# Set reduced the axis limits
		pylab.tick_params(axis='both', which='major', labelsize=16)
		pylab.xlim(0,heatmap_reduced.shape[0])
		pylab.ylim(0,heatmap_reduced.shape[1])	

		return heatmap
示例#26
0
def cell_morphs3d(cells_select_df, morphologies, fig, skip=100, color='k'):

    ax = fig.gca(projection='3d')

    col = 'k'

    RotX = np.array([
        [1, 0, 0],  # rotate around x axis
        [0, 0, 1],
        [0, 1, 0]
    ])

    ncells_select = len(cells_select_df.index)
    rows_display = range(0, ncells_select, skip)
    cells_display_df = cells_select_df.iloc[rows_display]

    for gid, cell_prop in cells_display_df.iterrows():

        x_soma = cell_prop[
            'x_soma']  # needed to do those in sequence because Series did not inherite the dtype from DataFrame
        y_soma = cell_prop[
            'y_soma']  # need to look for a more elegant solution to this
        z_soma = cell_prop['z_soma']
        pos_soma = np.array([x_soma, y_soma, z_soma])

        phi_y = cell_prop['rotation_angle_yaxis']
        phi_z = cell_prop['rotation_angle_zaxis']

        RotY = rotation_matrix([0, 1, 0], phi_y)
        RotZ = rotation_matrix([0, 0, 1], -phi_z)
        RotYZ = RotY.dot(RotZ)  # apply two rotations

        model_id = cell_prop['model_id']
        morph_segs_start = morphologies[model_id]["segs_start"]
        morph_segs_end = morphologies[model_id]["segs_end"]

        segs_start = pos_soma + np.dot(
            morph_segs_start,
            RotYZ.T)  # use the tranposed matrix since we multiply on the right
        segs_end = pos_soma + np.dot(morph_segs_end, RotYZ.T)
        pos_soma = np.dot(pos_soma, RotX.T) * 1E-3

        segs_start = np.dot(segs_start, RotX.T) * 1E-3
        segs_end = np.dot(
            segs_end, RotX.T
        ) * 1E-3  # for display purposes switch z and y, convert to mm:

        col_segs = build_collection3d(segs_start, segs_end)

        ax.add_collection(col_segs)  # plot segments

        ax.scatter(pos_soma[0],
                   pos_soma[1],
                   pos_soma[2],
                   color='red',
                   alpha=0.4)  # plot somata

    set_axes_equal(ax)
    ax.set_aspect('equal')

    ax.set_xlim([-0.8, 0.8])
    ax.set_zlim([-1.2, 0.2])
    ax.set_ylim([-0.8, 0.8])
    #	ax.autoscale_view(True,True,True)

    plt.locator_params(nbins=4)

    ax.set_xlabel('lat')
    ax.set_ylabel('lat')
    ax.set_zlabel('depth')
示例#27
0
文件: test11.py 项目: suracefm/dynXRD
crystal.calc_g0_gH(Energy)
thBragg= float(layer1.calc_Bragg_angle(Energy).subs(layer1.structure.subs).evalf())
angle=pl.linspace(0.992, 1.008,501)*thBragg


crystal.calc_reflectivity(angle, Energy)
layer1.calc_amplitudes(angle, Energy)
Sub.calc_amplitudes(angle, Energy)

XRl = layer1.XR
XRs = Sub.XR
XT = layer1.XT

crystal.print_values(angle, Energy)


pl.plot(data[:,0], data[:,1], label='GID_sl', color='red')
# pl.plot(angle-thBragg,abs(XT)**2-1)
pl.plot(pl.degrees(angle-thBragg),abs(XRl)**2, label='dynXRD', color='black')
# pl.plot(angle-thBragg,1 - abs(XT)**2 - abs(XRl)**2)

pl.xlabel('Angle (degrees)')
pl.ylabel('Reflectivity')
pl.xlim(-0.2,0.2)
pl.yscale('log')
pl.rc('font', size=18)
pl.legend(loc="upper left", prop={'size':19})
pl.locator_params(axis = 'x', nbins=4)

pl.savefig('pics/test11.eps')
pl.show()
示例#28
0
            # if (int(data_read[i][2].replace('.', '')) < 100):
            #     name_of_maps.append(data_read[i][0])
            #     teamrank.append(int(data_read[i][2].replace('.', '')))
            name_of_maps.append(data_read[i][0])
            teamrank.append(int(data_read[i][2].replace('.', '')))

    ind = np.argsort(teamrank)
    sorted_maps = [name_of_maps[i] for i in ind]
    sorted_ranks = [teamrank[i] for i in ind]

    return sorted_ranks, sorted_maps, nick_name


sorted_ranks_list, sorted_maps_list, name = get_player_data(nickname)
x_val1 = list(range(0, len(sorted_ranks_list)))

# plot
ax1 = plt.subplot()
t = x_val1
plt.scatter(x_val1, sorted_ranks_list, s=5, c=t, cmap='viridis')
plt.ylabel('Team ranks')
plt.title(name + " team ranks")
plt.grid()
plt.locator_params(axis='y', nbins=15)
plt.locator_params(axis='x', nbins=len(sorted_maps_list))
ax1.set_yticks([0, 5, 10], minor=True)
ax1.grid(which='minor', linestyle='--', linewidth='0.5', color='k')
ax1.set_xlim(0, len(sorted_maps_list))
ax1.set_xticklabels(sorted_maps_list, rotation=90, fontsize=8)
plt.show()
示例#29
0
          interpolation='none')
pl.title('pw-rigid mean')
pl.axis('off')
pl.xlim([0, 100])
pl.ylim([220, 320])
pl.axis('off')

pl.subplot(4, 3, 5)
pl.scatter(r_raw, r_rig, s=50, c='red')
pl.axis('tight')
pl.plot([0, 1], [0, 1], 'k--')
pl.xlabel('raw')
pl.ylabel('rigid')
pl.xlim([0.2, .45])
pl.ylim([.2, .45])
pl.locator_params(nbins=4)

pl.subplot(4, 3, 6)
pl.scatter(r_rig, r_el, s=50, c='red')
pl.plot([0, 1], [0, 1], 'k--')
pl.ylabel('pw-rigid')
pl.xlabel('rigid')
pl.xlim([0.3, .45])
pl.ylim([.3, .45])
pl.locator_params(nbins=4)

pl.subplot(4, 3, 4)
pl.plot(np.arange(0, 3000 * .2, 0.2), r_el)
pl.plot(np.arange(0, 3000 * .2, 0.2), r_rig)
pl.plot(np.arange(0, 3000 * .2, 0.2), r_raw)
pl.xlim([220, 320])
示例#30
0
        t1 = map(int, parsedate(ts[ind]))
    
        date_obj1 = datetime.datetime(year = t1[0], month = t1[1], day = t1[2], hour = t1[3], minute = t1[4], second = t1[5])
    
        tweet_time.append((date_obj1 - reference_date).total_seconds())

    seconds = tweet_time[-1] + 1 # The total number of seconds from the start_time

    binarized = numpy.zeros(seconds)

    binarized[tweet_time] = 1

    axarr[axind].vlines(numpy.arange(seconds)[binarized==1], -0.5, 0.5)
    axarr[axind].yaxis.set_visible(False)

pylab.locator_params(axis = 'x', nbins = 5)
pylab.xlabel('Time (s)')
pylab.savefig('_num_tweets.pdf')
pylab.show()

# pylab.figure()
# pylab.hist(interarrivals, bins = numpy.unique(interarrivals).shape[0], normed = True)
# pylab.show()

# Q: How to deal with *night-time*?

# pylab.figure()
# pylab.hist(num_tweets[0,:], bins = 200, normed = True)
# pylab.xlabel('Number of Tweets Made')
# pylab.ylabel('Count of Users Making that Number of Tweets')
# pylab.show()
示例#31
0
def plot_reasonably_calibrated_errors(result, filename=None):
    pylab.rcParams.update({'font.size': 8})
    pylab.locator_params(nbins=3)
    fig = pylab.figure(num=None, figsize=(10,8), facecolor='w', edgecolor='k')

    num_cols = result['config']['num_cols']

    ax = pylab.subplot(2,3,1)
    pylab.plot([0,1],[0,1], color='black', lw=3)
    for col in range(num_cols):
        X = numpy.cumsum(result['actual_frequencies'][col])
        Y = numpy.cumsum(result['inferred_P_cc'][col])
        pylab.plot(X, Y, color='green', alpha = .5, lw=2)

    pylab.ylabel('Inferred frequencies')
    pylab.xlabel('Actual frequencies')
    ax.set_yticks([0,.5,1])
    ax.set_xticks([0,.5,1])
    pylab.title('CrossCat')
    pylab.xlim([0,1])
    pylab.ylim([0,1])


    ax = pylab.subplot(2,3,2)
    pylab.plot([0,1],[0,1], color='black', lw=3)
    for col in range(num_cols):
        X = numpy.cumsum(result['actual_frequencies'][col])
        Y = numpy.cumsum(result['inferred_P_crp'][col])
        pylab.plot(X, Y, color='blue', alpha = .5, lw=2)

    pylab.ylabel('Inferred frequencies')
    pylab.xlabel('Actual frequencies')
    ax.set_yticks([0,.5,1])
    ax.set_xticks([0,.5,1])
    pylab.title('CRP mixture')
    pylab.xlim([0,1])
    pylab.ylim([0,1])

    ax = pylab.subplot(2,3,3)
    pylab.plot([0,1],[0,1], color='black', lw=3)
    for col in range(num_cols):
        X = numpy.cumsum(result['actual_frequencies'][col])
        Y = numpy.cumsum(result['inferred_P_nb'][col])
        pylab.plot(X, Y, color='red', alpha = .5, lw=2)

    pylab.ylabel('Inferred frequencies')
    pylab.xlabel('Actual frequencies')
    ax.set_yticks([0,.5,1])
    ax.set_xticks([0,.5,1])
    pylab.title('Naive Bayes')
    pylab.xlim([0,1])
    pylab.ylim([0,1])

    ax = pylab.subplot(2,3,4)

    num_iters = result['config']['num_iters']
    num_rows = result['config']['num_rows']
    num_chains = result['config']['num_chains']
    num_views = result['config']['num_views']
    num_clusters = result['config']['num_clusters']
    prop_missing = result['config']['prop_missing']
    multinomial_categories = result['config']['multinomial_categories']

    txt = '''
        Cumulated frequency plots over missing values for multinomial data. Each line 
        represents the distribution of a column. Y-axis is the inferred distribution
        and the x-axis is the actual distribution. Each line can be thought of as a 
        multinomial P-P plot using normalized frequencies over missing values.

        BayesDB: normal configuration
        BayesDB CRP mixture configuration: one view, no column transitions
        BayesDB Naive Bayes configuration: one view, one category, only hyperparameter
        transitions

        The table was %i rows by %i cols with %i views and %i categories per view. Each
        column's data was generates from a multinomial distribution with %i values. %1.2f
        percent of the data was removed.

        %i chains of where run for %i iterations for each configuration.
    ''' % (num_rows, num_cols, num_views, num_clusters, multinomial_categories, prop_missing*100, \
        num_chains, num_iters)

    ax.text(0,0,txt, fontsize=9)
    ax.axis('off')

    if filename is None:
        filename = 'Reasonably_calibrated=%i_chains=%i_T=%i.png' % \
            (num_iters, num_chains, int(time.time()))

    pylab.savefig( filename )
示例#32
0
def true_vs_fitted_session_params(population_fit):
    ''' For a set of simulated sessions plot the fitted parameter values
    against the true paramter values to assess goodness of fit.  If the 
    population fit includes ML fits, these are plotted as well as the MAP_fit
    fits and some metrics of the difference in fit quality are provided.'''

    #Unpack true and fitted parameter values into arrays.
    true_params_U = np.array(population_fit['true_values']['params_U']).T   
    true_params_T = np.array(population_fit['true_values']['params_T']).T 


    MAP_params_U   = np.array([MAP_fit['params_U'] for MAP_fit  in population_fit['MAP_fits'] ]).T
    MAP_params_T   = np.array([MAP_fit['params_T'] for MAP_fit  in population_fit['MAP_fits'] ]).T

    n_params = np.shape(true_params_U)[0]
    n_sessions = np.shape(true_params_U)[1]
    cols = (np.arange(n_sessions) + 0.01)/n_sessions
    n_rows = 2
    p.figure(2)
    p.clf()

    if not population_fit['ML_fits'] is None:

        ML_params_U   = np.array([ML_fit['params_U'] for ML_fit  in population_fit['ML_fits'] ]).T
        ML_params_T   = np.array([ML_fit['params_T'] for ML_fit  in population_fit['ML_fits'] ]).T

        #Evaluate and print fit errors.
        ML_mean_abs_error_T  = np.mean(np.abs(ML_params_T   - true_params_T), 1)
        MAP_mean_abs_error_T = np.mean(np.abs(MAP_params_T  - true_params_T), 1)
        mean_abs_error_MAP_bonus = ML_mean_abs_error_T - MAP_mean_abs_error_T
        ML_error_of_mean_T  = np.abs(np.mean(ML_params_T, 1)  - np.mean(true_params_T, 1))
        MAP_error_of_mean_T = np.abs(np.mean(MAP_params_T, 1) - np.mean(true_params_T, 1))
        error_of_mean_MAP_bonus = ML_error_of_mean_T - MAP_error_of_mean_T
        print('MAP bonus - mean absolute error: {}'.format(mean_abs_error_MAP_bonus))
        print('MAP bonus - error of mean      : {}'.format(error_of_mean_MAP_bonus))

        n_rows = 4

        for true_params_U_i, true_params_T_i, ML_params_U_i, ML_params_T_i, i in \
           zip(true_params_U, true_params_T, ML_params_U, ML_params_T, range(n_params)):

            p.subplot(4, n_params, 2 * n_params + i + 1)
            p.scatter(true_params_U_i, ML_params_U_i, c = cols, cmap = 'hsv', vmin = 0., vmax = 1.)
            p.plot([min(true_params_U_i), max(true_params_U_i)], [min(true_params_U_i), max(true_params_U_i)] , 'k')
            p.locator_params(axis = 'x', nbins = 4, tight = True)

            p.subplot(4, n_params, 3 * n_params + i + 1)
            p.scatter(true_params_T_i, ML_params_T_i, c = cols, cmap = 'hsv', vmin = 0., vmax = 1.)
            p.plot([min(true_params_T_i), max(true_params_T_i)], [min(true_params_T_i), max(true_params_T_i)] , 'k')
            p.locator_params(axis = 'x', nbins = 4, tight = True)

        p.subplot(4, n_params, 1 + 2 * n_params)
        p.title('ML Fits: Unconstrained space')
        p.subplot(4, n_params, 1 + 3 * n_params)
        p.title('ML Fits: True space')

  
    for true_params_U_i, true_params_T_i,  MAP_params_U_i, MAP_params_T_i, i in \
        zip(true_params_U, true_params_T,  MAP_params_U, MAP_params_T, range(n_params)):
 
        p.subplot(n_rows, n_params, i + 1)
        p.scatter(true_params_U_i, MAP_params_U_i, c = cols, cmap = 'hsv', vmin = 0., vmax = 1.)
        p.plot([min(true_params_U_i), max(true_params_U_i)], [min(true_params_U_i), max(true_params_U_i)] , 'k')
        p.locator_params(axis = 'x', nbins = 4, tight = True)

        p.subplot(n_rows, n_params, 1 * n_params + i + 1)
        p.scatter(true_params_T_i, MAP_params_T_i, c = cols, cmap = 'hsv', vmin = 0., vmax = 1.)
        p.plot([min(true_params_T_i), max(true_params_T_i)], [min(true_params_T_i), max(true_params_T_i)] , 'k')
        p.locator_params(axis = 'x', nbins = 4, tight = True)

    p.subplot(n_rows, n_params, 1)
    p.title('MAP Fits: Unconstrained space')
    p.subplot(n_rows, n_params, 1 + n_params)
    p.title('MAP Fits: True space')

    for i in range(n_params):
        p.subplot(n_rows, n_params, i + 1 + (n_rows - 1) * n_params)
        p.xlabel(population_fit['param_names'][i])
                            max_shft], cmap='gray', vmax=vmax, interpolation='none')
pl.title('pw-rigid mean')
pl.axis('off')
pl.xlim([0, 100])
pl.ylim([220, 320])
pl.axis('off')

pl.subplot(4, 3, 5)
pl.scatter(r_raw, r_rig, s=50, c='red')
pl.axis('tight')
pl.plot([0, 1], [0, 1], 'k--')
pl.xlabel('raw')
pl.ylabel('rigid')
pl.xlim([0.2, .45])
pl.ylim([.2, .45])
pl.locator_params(nbins=4)

pl.subplot(4, 3, 6)
pl.scatter(r_rig, r_el, s=50, c='red')
pl.plot([0, 1], [0, 1], 'k--')
pl.ylabel('pw-rigid')
pl.xlabel('rigid')
pl.xlim([0.3, .45])
pl.ylim([.3, .45])
pl.locator_params(nbins=4)


pl.subplot(4, 3, 4)
pl.plot(np.arange(0, 3000 * .2, 0.2), r_el)
pl.plot(np.arange(0, 3000 * .2, 0.2), r_rig)
pl.plot(np.arange(0, 3000 * .2, 0.2), r_raw)
示例#34
0
    n, bins, patches = P.hist(out[:, col_j],
                              50,
                              normed=1,
                              histtype='stepfilled',
                              color='white')
    y = P.normpdf(bins, m, s)
    P.plot(bins, y, 'b--', linewidth=1.5)

    ax.annotate(r"$\epsilon = %.3g\%%$" % (s / m * 100),
                xy=(0.5, 0.125),
                xycoords='axes fraction',
                horizontalalignment='center',
                verticalalignment='center')

    P.xlabel(name)
    ax.set_yticklabels('', visible=False)

    P.locator_params(axis='x', nbins=3)
    xticks = [m - 3 * s, m, m + 3 * s]
    ax.set_xlim([m - 4 * s, m + 4 * s])
    ax.set_xticks(xticks)
    ax.set_xticklabels(['%.3g' % xtick for xtick in xticks])
    #ax.minorticks_on()
    #"""
    plt.tight_layout()

print(outstr)
plt.subplots_adjust(top=0.9)
plt.savefig(os.path.join('learn_plots', '16Cyg-c.png'))
plt.close()
示例#35
0
def _plot_exponential_fit(fit, p_1, pre_post_trials, last_n, col = 'r'):
    t = np.arange(0,pre_post_trials[1])
    p_traj = np.hstack([ut.nans(-pre_post_trials[0]-last_n), np.ones(last_n) * fit['p_0'], \
                   _exp_choice_traj(fit['tau'], fit['p_0'], p_1, t)])
    p.plot(range(pre_post_trials[0], pre_post_trials[1]),p_traj, col, linewidth = 2)
    p.locator_params(nbins = 4)
示例#36
0
文件: Markov.py 项目: airanmehr/bio
def plotNull(subp, nu0=0.005, fontsize=5):
    obs = pd.read_pickle(utl.outpath + 'markov/neutral.obs.{}.pkl'.format(nu0))
    T = Markov.computeTransition(0, N=1000)

    dfplt = pd.concat([pd.Series({'scale': 10, 'xlim': [0.0, 0.01], 'ylim': [0, 1]}, name=(0.005, 1)),
                       pd.Series({'scale': 30, 'xlim': [0.06, 0.14], 'ylim': [0, 0.15]}, name=(0.1, 1)),
                       pd.Series({'scale': 30, 'xlim': [0.0, 0.015], 'ylim': [0, 0.3]}, name=(0.005, 10)),
                       pd.Series({'scale': 45, 'xlim': [0.0, 0.2], 'ylim': [0, 0.025]}, name=(0.1, 10)),
                      pd.Series({'scale':30, 'xlim':[0.0,0.03],'ylim': [0,0.2]},name=(0.005,100)),pd.Series({'scale':50, 'xlim':[0.00,0.4],'ylim': [0,0.004]},name=(0.1,100))
                         ],axis=1).T

    markov=T.loc[nu0].copy(True);markov.name='Markov Chain'
    xx=np.arange(0,1,0.00001)
    N=200; tau=1;h=2*nu0*(1-nu0);sig2=h*tau/N;brownian=stats.norm(nu0, sig2).pdf(xx);
    brownian=pd.Series(brownian,index=xx);brownian/=brownian.sum();brownian.name='Brownian Motion';brownian*=dfplt.loc[(nu0,tau)].scale
    pplt.setSize(plt.gca(), fontsize=fontsize)
    plt.subplot(3, 3, subp[0]);
    brownian.plot(color='r');
    markov.plot(color='b');
    o=pd.Series(obs.X[1].flatten()).value_counts().sort_index();o=o/o.sum();
    if nu0==0.1:
        counts,limits=np.histogram(obs.X[1].flatten(),bins=500,range=[0,1]);centers = 0.5*(limits[1:]+limits[:-1]);o=pd.Series(counts,index=centers);o=o/(obs.X.shape[1]*obs.X.shape[2]*4)
    o.plot(color='g')
    plt.xlim(dfplt.loc[(nu0, tau)].xlim);
    plt.ylim(dfplt.loc[(nu0, tau)].ylim);
    plt.locator_params(nbins=3)
    pplt.annotate(r'$s=0$, $\nu_0=${}, $\tau$={}'.format(nu0, tau), fontsize=fontsize)
    plt.ylabel(r'$P(\nu_\tau|\nu_0)$')
    tau=10
    for _ in range(9):
        markov=markov.dot(T)
    N=200;h=2*nu0*(1-nu0);sig2=h*tau/N;brownian=stats.norm(nu0, sig2).pdf(xx)
    brownian=pd.Series(brownian,index=xx);brownian/=brownian.sum();brownian.name='Brownian Motion';
    brownian*=dfplt.loc[(nu0,tau)].scale
    pplt.setSize(plt.gca(), fontsize=fontsize)
    plt.title('({})'.format(subptitle[subp[0] - 1]), fontsize=fontsize)
    plt.subplot(3, 3, subp[1]);
    brownian.plot(color='r');
    markov.plot(color='b');
    o=pd.Series(obs.X[10].flatten()).value_counts().sort_index();o=o/o.sum();
    if nu0==0.1:
        counts,limits=np.histogram(obs.X[10].flatten(),bins=100,range=[0,1]);centers = 0.5*(limits[1:]+limits[:-1]);o=pd.Series(counts,index=centers);o=o/(obs.X.shape[1]*obs.X.shape[2]*20)
    o.plot(color='g')
    plt.xlim(dfplt.loc[(nu0, tau)].xlim);
    plt.ylim(dfplt.loc[(nu0, tau)].ylim);
    plt.locator_params(nbins=3)
    pplt.annotate(r'$s=0$, $\nu_0=${}, $\tau$={}'.format(nu0, tau), loc=1, fontsize=fontsize)
    pplt.setSize(plt.gca(), fontsize=fontsize)



    tau=100
    for _ in range(90):
        markov=markov.dot(T)
    N=200;h=2*nu0*(1-nu0);sig2=h*tau/N;brownian=stats.norm(nu0, sig2).pdf(xx)
    brownian=pd.Series(brownian,index=xx);brownian/=brownian.sum();brownian.name='Brownian Motion';
    brownian*=dfplt.loc[(nu0,tau)].scale
    plt.title('({})'.format(subptitle[subp[1] - 1]), fontsize=fontsize)
    plt.subplot(3, 3, subp[2]);
    brownian.plot(color='r');
    markov.plot(color='b')
    o=pd.Series(obs.X[100].flatten()).value_counts().sort_index();o=o/o.sum();
    if nu0==0.1:
        counts,limits=np.histogram(obs.X[100].flatten(),bins=30,range=[0,1]);centers = 0.5*(limits[1:]+limits[:-1]);o=pd.Series(counts,index=centers);o=o/(obs.X.shape[1]*obs.X.shape[2]*60)
    o.name = 'Observation';
    o.plot(color='g')
    plt.xlim(dfplt.loc[(nu0, tau)].xlim);
    plt.ylim(dfplt.loc[(nu0, tau)].ylim);
    plt.locator_params(nbins=3)
    pplt.annotate(r'$s=0$, $\nu_0=${}, $\tau$={}'.format(nu0, tau), loc=1, fontsize=fontsize)
    if subp[2] == 3:
        plt.legend(loc='center right', fontsize=fontsize)
    pplt.setSize(plt.gca(), fontsize=fontsize)
    plt.title('({})'.format(subptitle[subp[2] - 1]), fontsize=fontsize)
#	pylab.plot(np.arange(DELAY_MAX + 1) * average_sampling_time, acf[i, :], "k-")

#  Plot min and mac auto-correlation for each lag from ensemble
pylab.fill_between(np.arange(DELAY_MAX + 1) * average_sampling_time,
                   acf.min(axis=0),
                   acf.max(axis=0),
                   color="0.75")

#  Load Kernel ACF computed using Kira's method
kernel_acf = pylab.loadtxt("kernel_acf_dongge.txt")

pylab.plot(kernel_acf[:, 0], kernel_acf[:, 1], "k-")

pylab.xlabel("Time lag (a)")
pylab.ylabel("Autocorrelation")

pylab.xlim(0, DELAY_TIME_MAX)
pylab.ylim(-0.4, 1)

#  Enable minor ticks
pylab.minorticks_on()

#  Set number of ticks and tick labels
pylab.locator_params(axis="y", tight=True, nbins=5)

pylab.axhline(y=0, color="k", linestyle="dashed")

pylab.savefig("dongge_acf.pdf")

pylab.show()
示例#38
0
文件: test14.py 项目: suracefm/dynXRD
layer1.calc_orientation_from_angle(psi, v_perp)
crystal=reflectivity.Sample(Sub, layer1)
#crystal.calc_layer_Miller()
crystal.set_Miller(R)
crystal.calc_g0_gH(Energy)
thBragg= float(Sub.calc_Bragg_angle(Energy).subs(Sub.structure.subs).evalf())
angle=pl.linspace(0.987, 1.013,501)*thBragg

#XRl = layer1.calc_reflection_amplitude(angle, Energy)
#XRs = Sub.calc_reflection_amplitude(angle, Energy)
# XT = layer1.calc_transmission_amplitude(angle, Energy)
XR=crystal.calc_reflectivity(angle, Energy)
crystal.print_values(angle, Energy)

pl.plot(data[:,0], data[:,1] , label='GID_sl', color='red')
# pl.plot(angle-thBragg,abs(XT)**2)
# pl.plot(angle-thBragg,abs(XRl)**2)
# pl.plot(angle-thBragg,1 - abs(XT)**2 - abs(XRl)**2)
pl.plot(pl.degrees(angle-thBragg),abs(XR)**2, label='dynXRD', color='black')
#pl.plot(angle-thBragg,abs(XRs)**2)
#pl.plot(angle-thBragg,abs(XRl)**2)
pl.yscale('log')
pl.xlabel('Angle (degrees)')
pl.ylabel('Reflectivity')
pl.xlim(-0.2,0.2)
pl.rc('font', size=18)
pl.legend(loc="upper left", prop={'size':19})
pl.locator_params(axis = 'x', nbins=4)

pl.savefig('pics/test14.eps')
pl.show()
示例#39
0
def plot_star(star, predict, y_names, out_dir=plot_dir):
    ## Regular histograms
    middles = np.mean(predict, 0)
    stds = np.std(predict, 0)
    
    #outstr = star
    num_ys = predict.shape[1]
    
    rows, cols, sqrt = get_rc(num_ys)
    
    plt.figure(figsize=(6.97522*2, 4.17309*2), dpi=400, 
        facecolor='w', edgecolor='k')
    for (pred_j, name) in enumerate(y_names[0:num_ys]):
        (m, s) = (middles[pred_j], stds[pred_j])
        
        if num_ys%2==0 or num_ys%3==0 or int(sqrt)==sqrt:
            ax = plt.subplot(rows, cols, pred_j+1)
        elif pred_j%2==0 and pred_j == num_ys-1:
            ax = plt.subplot2grid((rows, cols), (pred_j//2, 1), colspan=2)
        else:
            ax = plt.subplot2grid((rows, cols), (pred_j//2, (pred_j%2)*2),
                colspan=2)
        
        n, bins, patches = ax.hist(predict[:,pred_j], 50, normed=1, 
            histtype='stepfilled', color='white')
        
        #if y_central is not None:
        #    mean = np.mean(y_central[:,pred_j])
        #    std = np.std(y_central[:,pred_j])
        #    
        #    plot_line(mean, n, bins, plt, 'r--')
        #    plot_line(mean+std, n, bins, plt, 'r-.')
        #    plot_line(mean-std, n, bins, plt, 'r-.')
        
        q_16, q_50, q_84 = corner.quantile(predict[:,pred_j], [0.16, 0.5, 0.84])
        q_m, q_p = q_50-q_16, q_84-q_50
        plot_line(q_50, n, bins, plt, 'k--')
        plot_line(q_16, n, bins, plt, 'k-.')
        plot_line(q_84, n, bins, plt, 'k-.')
        
        # Format the quantile display.
        fmt = "{{0:{0}}}".format(".3g").format
        title = r"${{{0}}}_{{-{1}}}^{{+{2}}}$"
        title = title.format(fmt(q_50), fmt(q_m), fmt(q_p))
        
        ax.annotate(r"$\epsilon = %.3g\%%$" % (s/m*100),
            xy=(0.99, 0.12), xycoords='axes fraction',
            horizontalalignment='right', verticalalignment='right')
        
        P.xlabel(y_latex[y_names[pred_j]] + " = " + title)
        P.locator_params(axis='x', nbins=3)
        
        xs = [max(0, m-4*s), m+4*s]
        
        xticks = [max(0, m-3*s), m, m+3*s]
        ax.set_xticks(xticks)
        ax.set_xlim(xs)
        ax.set_xticklabels(['%.3g'%xtick for xtick in xticks])
        ax.set_yticklabels('',visible=False)
        
        ax.set_frame_on(False)
        ax.get_xaxis().tick_bottom()
        ax.axes.get_yaxis().set_visible(False)
        
        #xmin, xmax = ax1.get_xaxis().get_view_interval()
        ymin, ymax = ax.get_yaxis().get_view_interval()
        ax.add_artist(mpl.lines.Line2D(xs, (ymin, ymin), 
            color='black', linewidth=2))
        
        #ax.minorticks_on()
        plt.tight_layout()
    
    plt.savefig(os.path.join(out_dir, star + '.pdf'), dpi=400)
    plt.close()
示例#40
0
    'csv/carry_errors_big.csv',
    dict(label='Train on random examples'),
)
make_plot(
    'csv/carry_errors_big.baddt.csv',
    dict(label='Train with some hard examples'),
)
#pylab.xlabel("Number of carries $k$")
#pylab.legend(loc=0, prop={'size': legsize})
pylab.gca().yaxis.set_major_formatter(
    mtick.FuncFormatter(lambda x, pos: '% 2d%%' % (x * 100)))
if not vertical:
    pylab.xlabel("Number of carries $k$")
pylab.ylabel("Error rate")
pylab.title("Binary", size=titlesize)
pylab.locator_params(axis='y', nbins=4)  #

pylab.subplot(*(orientation + (2, )))
make_plot(
    'csv/carry_errors_add_large.csv',
    dict(label='Train on random examples'),
)
make_plot(
    'csv/carry_errors_addt_large.csv',
    dict(label='Train with some hard examples'),
)
pylab.xlabel("Number of carries $k$")
#pylab.legend(loc=0, prop={'size': legsize})
if vertical:
    pylab.ylabel("Error rate")
pylab.gca().yaxis.set_major_formatter(
                joystick_handle = jh
        if jj == 0:
            handles.append(th)
ax.set_xlabel('x [m]', fontsize=fontsize)
ax.set_ylabel('y [m]', fontsize=fontsize)
pl.axis('equal')
ax.set_xlim([-15, 6])
ax.set_ylim([-8, 2])
ax.tick_params(labelsize=fontsize)
pl.legend((handles[0][0], handles[1][0], joystick_handle[0][0]),
          (names[0], names[1], 'Joystick'),
          loc='best',
          fancybox=True,
          framealpha=0.5,
          numpoints=1)
pl.locator_params(nbins=8)
pl.tight_layout()
pl.subplots_adjust(left=0.17, right=0.95, top=0.95, bottom=0.15)

abs_dist = np.zeros([len(planner_missions[0])])
joy_dist = np.zeros([len(planner_missions[0])])
fused_missions = planner_missions_background
fused_missions.append(planner_missions[0])
for miss in fused_missions:
    for ii, m in enumerate(miss):
        abs_dist[ii] += m.distance()
        joy_dist[ii] += pc_util.compute_joystick_distance(m)

autonomous_ratio = 100 * (1 - joy_dist / abs_dist)

if save_figures:
示例#42
0
i = 0

textfs = 32
markersize = 200

fig = pl.figure()

pl.subplots_adjust(left=0.1,
                   bottom=0.1,
                   right=0.9,
                   top=0.88,
                   wspace=0.35,
                   hspace=0.1)

pl.subplot(1, 2, 1)
pl.locator_params(nbins=7)
# pl.scatter(norms, wrongs_d[i], alpha=0.75, s=markersize, label='desired B-tensors')
pl.scatter(norms,
           wrongs_d[i],
           alpha=0.75,
           s=markersize,
           label=r'Fit using $\mathbf{{B}}$',
           edgecolors=None)
# pl.scatter(norms, propers_d[i], alpha=0.75, s=markersize, label='actual B-tensors')
pl.scatter(norms,
           propers_d[i],
           alpha=0.75,
           s=markersize,
           label=r'Fit using $\mathbf{{B}}_a$',
           edgecolors=None)
k = max(max(propers_d[i]), max(wrongs_d[i]))
示例#43
0
def plot_reasonably_calibrated_errors(result, filename=None):
    pylab.rcParams.update({"font.size": 8})
    pylab.locator_params(nbins=3)
    fig = pylab.figure(num=None, figsize=(10, 8), facecolor="w", edgecolor="k")

    num_cols = result["config"]["num_cols"]

    ax = pylab.subplot(2, 3, 1)
    pylab.plot([0, 1], [0, 1], color="black", lw=3)
    for col in range(num_cols):
        X = numpy.cumsum(result["actual_frequencies"][col])
        Y = numpy.cumsum(result["inferred_P_cc"][col])
        pylab.plot(X, Y, color="green", alpha=0.5, lw=2)

    pylab.ylabel("Inferred frequencies")
    pylab.xlabel("Actual frequencies")
    ax.set_yticks([0, 0.5, 1])
    ax.set_xticks([0, 0.5, 1])
    pylab.title("CrossCat")
    pylab.xlim([0, 1])
    pylab.ylim([0, 1])

    ax = pylab.subplot(2, 3, 2)
    pylab.plot([0, 1], [0, 1], color="black", lw=3)
    for col in range(num_cols):
        X = numpy.cumsum(result["actual_frequencies"][col])
        Y = numpy.cumsum(result["inferred_P_crp"][col])
        pylab.plot(X, Y, color="blue", alpha=0.5, lw=2)

    pylab.ylabel("Inferred frequencies")
    pylab.xlabel("Actual frequencies")
    ax.set_yticks([0, 0.5, 1])
    ax.set_xticks([0, 0.5, 1])
    pylab.title("CRP mixture")
    pylab.xlim([0, 1])
    pylab.ylim([0, 1])

    ax = pylab.subplot(2, 3, 3)
    pylab.plot([0, 1], [0, 1], color="black", lw=3)
    for col in range(num_cols):
        X = numpy.cumsum(result["actual_frequencies"][col])
        Y = numpy.cumsum(result["inferred_P_nb"][col])
        pylab.plot(X, Y, color="red", alpha=0.5, lw=2)

    pylab.ylabel("Inferred frequencies")
    pylab.xlabel("Actual frequencies")
    ax.set_yticks([0, 0.5, 1])
    ax.set_xticks([0, 0.5, 1])
    pylab.title("Naive Bayes")
    pylab.xlim([0, 1])
    pylab.ylim([0, 1])

    ax = pylab.subplot(2, 3, 4)

    num_iters = result["config"]["num_iters"]
    num_rows = result["config"]["num_rows"]
    num_chains = result["config"]["num_chains"]
    num_views = result["config"]["num_views"]
    num_clusters = result["config"]["num_clusters"]
    prop_missing = result["config"]["prop_missing"]
    multinomial_categories = result["config"]["multinomial_categories"]

    txt = """
        Cumulated frequency plots over missing values for multinomial data. Each line 
        represents the distribution of a column. Y-axis is the inferred distribution
        and the x-axis is the actual distribution. Each line can be thought of as a 
        multinomial P-P plot using normalized frequencies over missing values.

        BayesDB: normal configuration
        BayesDB CRP mixture configuration: one view, no column transitions
        BayesDB Naive Bayes configuration: one view, one category, only hyperparameter
        transitions

        The table was %i rows by %i cols with %i views and %i categories per view. Each
        column's data was generates from a multinomial distribution with %i values. %1.2f
        percent of the data was removed.

        %i chains of where run for %i iterations for each configuration.
    """ % (
        num_rows,
        num_cols,
        num_views,
        num_clusters,
        multinomial_categories,
        prop_missing * 100,
        num_chains,
        num_iters,
    )

    ax.text(0, 0, txt, fontsize=9)
    ax.axis("off")

    if filename is None:
        filename = "Reasonably_calibrated=%i_chains=%i_T=%i.png" % (num_iters, num_chains, int(time.time()))

    pylab.savefig(filename)
示例#44
0
    #make g(t)
    g[n] = []

    #doing the sum for each t and populate g(t)
    for i in t:
        g_i = ([(c_k * numpy.sin(w_k * i))
                for c_k, w_k in zip(c_karray, w_karray)])
        g[n].append(sum(g_i))

#plotting in a cutie way
n_plots = len(n_array)
cols = 2
rows = n_plots / cols

pylab.figure(1)
pylab.suptitle("Square waveform simulation", fontsize=14)

for h, n in zip(range(1, n_plots + 1, 1), n_array):
    pylab.subplot(rows, cols, h)
    pylab.plot(t / T, g[n], label='n = %d' % n)  #setting period as x-unit
    pylab.legend(prop={'size': 10})
    pylab.locator_params(nbins=4)
    pylab.grid()

    #not really elegant, but less boring...
    if (h == 3):
        pylab.ylabel('Simulated Signal [arb.un.]', size='large')
    if (h == 5) or (h == 6):
        pylab.xlabel('Time [T]', size='large')

pylab.show()
示例#45
0
def draw_ST_space(alpha = 0, debug = False, points = 11, reps = 2, force = False, file_format = 'png', output = True, **kwargs):
	"""
	Takes model III and plots the results over all of ST space. Two heat maps
	are created, one for x_bar and one for beta_bar.

	Params
	======
	alpha : float [0,1] {0} :
		alpha, i.e. r genetic assortment.
	debug : bool {False}
		Set to true to very quickly run the model to see if it is working.
	points : int {11}
		Number of descrete points to break up the space into.
	reps : int {2}
		Number of times to repeat the model.
	force : bool {False}
		The function will pickle the outputs, set to true to force the
		function to regenerate the data.
	file_format : str {'png'}
		Type of figure file to save. Anything that matplotlib will accept
	output : bool {True}
		Set to False to not save data and figure. You'll only be able to
		see the outputs if you're running interactively.
	kwargs :
		Additional keyword arguments passed to GP_evo model.

	"""

	label_size = 18
	tick_label_size = 15
	legend_label_size = 13

	##We don't want this to be the integer 1 or 0, it messes with the formatting!
	alpha = float(alpha)
	
	Ss = np.linspace(-1,4,points)
	Ts = np.linspace(0,4,points)

	beta_data_file = os.path.join( data_dir, "ST_sweep_beta_%.2f"%alpha )
	x_data_file = os.path.join( data_dir, "ST_sweep_x_%.2f"%alpha )

	#See if the data has been generated already
	try:

		##Bit of a hack
		if force:
			raise IOError

		f1 = open( beta_data_file,'rb')
		data_beta_bar = pickle.load( f1 )
		f1.close()


		f2 = open( x_data_file, 'rb' )
		data_x_bar = pickle.load( f2)
		f1.close()

		print "Data Found"

	except IOError:

		print "No Data Found, generating ..."

		data_x_bar = np.zeros( ( points, points, reps ) )
		data_beta_bar = np.zeros( ( points, points, reps ) )
		for k in xrange(reps):
			print "Rep:",k + 1
			for i,S in enumerate( Ss ):
				for j,T in enumerate( Ts ):
					#print S,T
					if debug:
						mod = GP_evo(S,T, alpha = alpha, x_points = 3, beta_points = 2, makeGraphs = False, printInfo = False, **kwargs)
					else:
						mod = GP_evo(S,T, alpha = alpha, makeGraphs = False, printInfo = False, **kwargs)
					mod.go()
					data_x_bar[i,j,k] = mod.mean_x
					data_beta_bar[i,j,k] = mod.mean_b

		##Save the data
		if output:
			print "Saving data at " + beta_data_file
			f1 = open( beta_data_file,'wb')
			pickle.dump( data_beta_bar, f1 )
			f1.close()

			print "Saving data at " + x_data_file
			f2 = open( x_data_file,'wb')
			pickle.dump( data_x_bar, f2)
			f2.close()

	##Create a red green blue array for x data 
	# x_mean = np.mean( data_x_bar, 2 )
	# RGB_data = np.array( [[ [ 1 - x_mean[i,j], 0 , x_mean[i,j], 0.1 ]  for i in xrange(points) ] for j in xrange(points) ] )

	#cmap = pl.get_cmap('Reds')

	##Create a colour map using seaborn
	cmap_linear = mpl.colors.ListedColormap( sns.light_palette((210, 90, 60), input="husl", n_colors = 101) )

	##For diverging colour
	cmap_diverging = mpl.colors.ListedColormap( sns.diverging_palette(10, 220, sep=80, n=101)  )

	f = pl.figure()
	pl.subplot(121)
	pl.imshow( np.mean( data_x_bar, 2), origin = [Ts[0], Ss[0]], interpolation = 'nearest',\
		extent = [ Ts[0],Ts[-1],Ss[0],Ss[-1] ], vmin = 0, vmax = 1, cmap = cmap_linear )
	# pl.imshow( RGB_data, origin = [Ts[0], Ss[0]], interpolation = 'nearest',\
	#  	extent = [ Ts[0],Ts[-1],Ss[0],Ss[-1] ], vmin = 0, vmax = 1, cmap = cmap)
	pl.title('$\\bar{x}$', fontsize = label_size)
	pl.xlabel('T', fontsize = label_size)
	pl.ylabel('S', fontsize = label_size)
	pl.plot( [ Ts[0], Ts[-1] ],[ 0,0 ],color='black', linewidth = 2.5 )
	pl.plot( [ 1, 1 ],[ Ss[0],Ss[-1] ],'--',color='black',  linewidth = 2.5 )
	pl.plot( [ 0, 3 ],[ 2, -1 ],'--',color='black',  linewidth = 2.5 )
	cb = pl.colorbar(shrink = .62, ticks = np.linspace( 0, 1, 6 ) )
	cb.ax.tick_params(labelsize=legend_label_size)
	##Fidle with the axis lables
	pl.tick_params(axis='both', which='major', labelsize=tick_label_size)
	pl.locator_params(axis = 'x', nbins=4)
	##Turn grid lines off
	pl.grid('off')

	pl.subplot(122)
	pl.imshow( np.mean( data_beta_bar, 2), origin = [Ts[0], Ss[0]], interpolation = 'nearest',\
	 extent = [ Ts[0],Ts[-1],Ss[0],Ss[-1] ], vmin = -1, vmax = 1, cmap = cmap_diverging )
	pl.title('$\\bar{\\beta}$', fontsize = label_size)
	pl.xlabel('T', fontsize = label_size)
	#pl.ylabel('S', fontsize = label_size)
	pl.plot( [ Ts[0], Ts[-1] ],[ 0,0 ],color='black', linewidth = 2.5 )
	pl.plot( [ 1, 1 ],[ Ss[0],Ss[-1] ],'--',color='black',  linewidth = 2.5 )
	pl.plot( [ 0, 3 ],[ 2, -1 ],'--',color='black',  linewidth = 2.5 )
	cb = pl.colorbar(shrink  = .62, ticks = np.linspace( -1, 1, 6 ))
	cb.ax.tick_params(labelsize=legend_label_size)
	##Fidle with the axis lables
	pl.tick_params(axis='both', which='major', labelsize=tick_label_size)
	pl.locator_params(axis = 'x', nbins=4)
	##Turn grid lines off
	pl.grid('off')

	fig_file = os.path.join( graph_dir, "alpha_{:.2}.{}".format( alpha, file_format) )
	if output:
		print "saving figure at " + fig_file
		pl.savefig( fig_file, bbox_inches='tight')
	else:
		return f
示例#46
0
def p(prior):
    ng = NG(**prior)
    print("Mode of prior: %2.2f, %2.2fP / %2.2fS" %
          (ng.mu, (ng.alpha - .5) / ng.beta, p2s((ng.alpha - .5) / ng.beta)))
    M, P = meshgrid(linspace(-2, 2, 151), linspace(s2p(6.5), s2p(0.15), 1051))
    clf()
    subplot(2, 3, 1)
    contourf(M, p2s(P), NG(**prior)(M, P), 51)
    axhline(p2s((ng.alpha - .5) / ng.beta), color='k')
    pyplot.locator_params(nbins=4)

    ylabel('Sample STD')
    xlabel('Sample Mean')
    title('Prior believe')
    subplot(2, 3, 2)

    title(r'$log(P(\mu>0|Stimulus))$')
    pH1 = PestH1Vec(ng)(M, P, 10)
    contourf(M, p2s(P), pH1, 11)
    contour(M, p2s(P), pH1, [.5], colors='k')
    pyplot.locator_params(nbins=4)

    xlabel('Sample Mean')
    yticks([])

    subplot(2, 3, 3)
    title(r'$log \frac{P(\mu>0|Stimulus)}{P(\mu<0|Stimulus)}$')
    dP = log(PestH1Vec(ng)(M, P, 10)) - log((1 - PestH1Vec(ng)(M, P, 10)))
    CS = contourf(M, p2s(P), dP, 11)
    contour(M, p2s(P), dP, [0.0], colors='k')
    pyplot.locator_params(nbins=4)

    xlabel('Sample Mean')
    yticks([])

    #axvline(0.075, color='k')
    '''
    subplot(2,3,4)
    em, es = linspace(-5.5, 5.5, 51), linspace(0, 5.5, 151)
    sm, ss, phit = foo([.1, .1, .1], prior, side=-1)
    out = bar(sm, ss, phit, em, es)
    print out.shape
    print phit
    contourf(centers(em), centers(es), out, linspace(0, 1, 11), cmap=winter())
    contour(M, p2s(P), PestH1Vec(ng)(M,P, 10), [0.5], colors='k')
    fa = []
    for i, (yl, yh) in enumerate(zip(es[:-1], es[1:])):
        idy = (yl<ss) & (ss<=yh)
        fa.append(sum(phit[idy]>0.5)/float(sum(idy)))

    subplot(2,3,5)
    sm, ss, phit = foo([.1, .1, .1], prior, side=1)
    out = bar(sm, ss, phit, em, es)
    print phit
    contourf(centers(em), centers(es), out, linspace(0, 1, 11), cmap=winter())
    contour(M, p2s(P), PestH1Vec(ng)(M,P, 10), [0.5], colors='k')
    subplot(2,3,6)
    hit = []
    for i, (yl, yh) in enumerate(zip(es[:-1], es[1:])):
        idy = (yl<ss) & (ss<=yh)
        hit.append(sum(phit[idy]>0.5)/float(sum(idy)))
    plot(centers(es), hit, label='hits')
    plot(centers(es), fa, label='fa')
    legend()
    #xlim([.75, 1.75])
    xlabel('STD')
    '''
    return prior
示例#47
0
文件: vismorph.py 项目: catubc/biovis
def cell_morphs3d(cells_select_df, morphologies,fig,skip=100,color='k'):

	ax = fig.gca(projection='3d')

	col='k'

	RotX = np.array([[1, 0, 0],    # rotate around x axis
				     [0, 0, 1],
				     [0, 1, 0]])

	ncells_select = len(cells_select_df.index)
	rows_display = range(0,ncells_select,skip)
	cells_display_df = cells_select_df.iloc[rows_display]


	for gid,cell_prop in cells_display_df.iterrows():  

		x_soma = cell_prop['x_soma'] # needed to do those in sequence because Series did not inherite the dtype from DataFrame 
		y_soma = cell_prop['y_soma'] # need to look for a more elegant solution to this 
		z_soma = cell_prop['z_soma']
		pos_soma = np.array([x_soma,y_soma,z_soma])


		phi_y = cell_prop['rotation_angle_yaxis']
		phi_z = cell_prop['rotation_angle_zaxis']

		RotY = rotation_matrix([0,1,0],phi_y)
		RotZ = rotation_matrix([0,0,1],-phi_z)
		RotYZ = RotY.dot(RotZ)	# apply two rotations


		model_id =  cell_prop['model_id']	
		morph_segs_start = morphologies[model_id]["segs_start"]
		morph_segs_end =   morphologies[model_id]["segs_end"] 


		segs_start = pos_soma + np.dot(morph_segs_start,RotYZ.T)  # use the tranposed matrix since we multiply on the right
		segs_end = pos_soma + np.dot(morph_segs_end,RotYZ.T)
		pos_soma = np.dot(pos_soma,RotX.T)*1E-3


		segs_start = np.dot(segs_start,RotX.T)*1E-3	
		segs_end = np.dot(segs_end,RotX.T)*1E-3		# for display purposes switch z and y, convert to mm:

		col_segs = build_collection3d(segs_start,segs_end)

		ax.add_collection(col_segs)	# plot segments

		ax.scatter(pos_soma[0], pos_soma[1], pos_soma[2],color='red',alpha=0.4) # plot somata


	set_axes_equal(ax)
	ax.set_aspect('equal')

	ax.set_xlim([-0.8,0.8])
	ax.set_zlim([-1.2,0.2])
	ax.set_ylim([-0.8,0.8])
#	ax.autoscale_view(True,True,True)


	plt.locator_params(nbins=4)

	ax.set_xlabel('lat')
	ax.set_ylabel('lat')
	ax.set_zlabel('depth')
示例#48
0
def main(experiment):
    swapped = False
    use_geodesic = True

    experiments = dict(
        cat=dict(
            file1='meshes/tosca/cat0.mat',
            file2='meshes/tosca/cat2.mat',
            mu=[0.7],
            Ks=np.arange(10, 50, 5),
        ),
        david=dict(
            file1='meshes/tosca_holes/david0_halfed.obj',
            file2='meshes/tosca_holes/david10_nolegs_nohead.obj',
            mu=[0.7],
            Ks=np.arange(10, 50, 5),
        ),
        hand=dict(
            file1='meshes/hand_868.obj',
            file2='meshes/hand_868_holes2.obj',
            mu=[5.0],
            Ks=np.arange(10, 50, 2),
        ),
    )
    if experiment not in experiments:
        print "Usage: python %s <experiment>" % sys.argv[0]
        print "where experiment is one of: %s" % (', '.join(
            experiments.keys()))
        sys.exit(1)

    exp = experiments[experiment]
    file1, file2 = exp['file1'], exp['file2']
    mu, Ks = exp['mu'], exp['Ks']

    print "this will take a while..."

    # load meshes
    verts1, tris1, verts2, tris2, ij = load_shape_pair(file1,
                                                       file2,
                                                       check=False,
                                                       normalize=False)
    if swapped:  # swapped?
        verts2, tris2, verts1, tris1 = verts1, tris1, verts2, tris2
        ij = ij[:, ::-1]

    if use_geodesic:
        D = compute_geodesic_distance_matrix(verts1, tris1)

    eigenvector_algorithms = [
        ('MHB', partial(cmm.manifold_harmonics, scaled=False)),
    ]

    for cmu in mu:
        name = 'CMHB' if len(mu) == 1 else 'CMHB %f' % (cmu)
        eigenvector_algorithms.append((name,
                                       partial(cmm.compressed_manifold_modes,
                                               mu=cmu,
                                               maxiter=10000,
                                               verbose=0,
                                               scaled=False)))

    mapping_algorithms = [
        ('rotation', partial(align.optimal_rotation, allow_reflection=True)),
        #('permutation', partial(align.optimal_permutation, allow_reflection=True)),
    ]

    print "computing basis functions (first mesh)"
    runs = [(K, name, algo) for K in Ks
            for name, algo in eigenvector_algorithms]
    Phis1 = Parallel(-1)(delayed(algo)(verts1, tris1, K)
                         for K, name, algo in runs)
    print "computing basis functions (second mesh)"
    Phis2 = Parallel(-1)(delayed(algo)(verts2, tris2, K)
                         for K, name, algo in runs)

    # run for different number of eigenfunctions K
    errors_per_mapping_algorithm = defaultdict(lambda: defaultdict(list))
    C_by_mapping_algorithm = defaultdict(lambda: defaultdict(list))
    for (K, eigenvector_algorithm_name,
         algo), Phi1, Phi2 in zip(runs, Phis1, Phis2):
        for mapping_algorithm_name, mapping_algorithm in mapping_algorithms:
            # compute the mapping between the eigenfunctions
            C = mapping_algorithm(Phi1[ij[:, 0]].T, Phi2[ij[:, 1]].T)
            Phi1_rot = np.dot(C, Phi1.T).T

            # for all points in shape 2 where there is a correspondence to shape 1 (all j),
            # find corresponding point i in shape 1
            tree = cKDTree(Phi1_rot)
            distance_eigenspace, nearest = tree.query(Phi2[ij[:, 1]])
            ij_corr = np.column_stack((ij[:, 0], nearest))

            # compute error
            if use_geodesic:
                errors = D[ij_corr[:, 0], ij_corr[:, 1]]
            else:
                errors = veclen(verts1[ij_corr[:, 0]] - verts1[ij_corr[:, 1]])
            mean_error = np.mean(errors)
            errors_per_mapping_algorithm[mapping_algorithm_name][
                eigenvector_algorithm_name].append(mean_error)
            C_by_mapping_algorithm[mapping_algorithm_name][
                eigenvector_algorithm_name].append(C)
            print "%s/%s (K=%d) %.2f" % (mapping_algorithm_name,
                                         eigenvector_algorithm_name, K,
                                         mean_error)

    for mapping_algorithm_name, C_by_trial in C_by_mapping_algorithm.iteritems(
    ):
        for trial_name, Cs in C_by_trial.iteritems():
            # just use the last rotation matrix computed
            C = Cs[-2]
            pl.figure(mapping_algorithm_name + '_' + trial_name,
                      figsize=(4, 3.5))
            plot_functional_map(reorder_functional_map(C), newfig=False)
            pl.subplots_adjust(left=0.01, right=1, top=0.95, bottom=0.05)

    show_correspondence(
        verts1,
        tris1,
        verts2,
        tris2,
        ij,
        points=5,
        color_only_correspondences=True,
        colormap='Paired',  #offset_factor=(-1.8, +0.0, 0.),
        show_spheres=False,
        blend_factor=1.0,
        block=False)

    for mapping_algorithm_name, errors_per_trial in errors_per_mapping_algorithm.iteritems(
    ):
        #pl.rcParams.update({'font.size': 6})
        pl.figure(mapping_algorithm_name + '_' + file1 + '_' + file2,
                  figsize=(2.5, 2.5))
        pl.rc('text', usetex=False)
        style_by_name = dict(CMHB=dict(c=(6 / 255., 50 / 255., 100 / 255.),
                                       lw=2),
                             MHB=dict(c=(121 / 255., 6 / 255., 34 / 255.),
                                      lw=1))
        for trial_name, errors in sorted(errors_per_trial.items(),
                                         key=lambda (k, v): k):
            print trial_name, errors
            style = style_by_name.get(trial_name, {})
            pl.plot(Ks, errors, label=trial_name, **style)
        pl.locator_params(nbins=4)
        pl.legend().draggable()
        pl.autoscale(tight=True)
        pl.ylim(bottom=0)
        pl.grid()
        pl.xlabel(r'$K$')
        pl.ylabel(r'geodesic error')
        pl.tight_layout()
    pl.show()
示例#49
0
def plot_star(star, predict, y_names, out_dir=plot_dir, nbins=10):
    ## Regular histograms
    middles = np.mean(predict, 0)
    stds = np.std(predict, 0)

    #outstr = star
    num_ys = predict.shape[1]

    rows, cols, sqrt = get_rc(num_ys)

    plt.figure(figsize=(6.97522 * 2, 4.17309 * 2),
               dpi=400,
               facecolor='w',
               edgecolor='k')
    for (pred_j, name) in enumerate(y_names[0:num_ys]):
        (m, s) = (middles[pred_j], stds[pred_j])

        if num_ys % 2 == 0 or num_ys % 3 == 0 or int(sqrt) == sqrt:
            ax = plt.subplot(rows, cols, pred_j + 1)
        elif pred_j % 2 == 0 and pred_j == num_ys - 1:
            ax = plt.subplot2grid((rows, cols), (pred_j // 2, 1), colspan=2)
        else:
            ax = plt.subplot2grid((rows, cols),
                                  (pred_j // 2, (pred_j % 2) * 2),
                                  colspan=2)

        n, bins, patches = ax.hist(predict[:, pred_j], nbins, density=1)  #,
        #histtype='stepfilled', color='white')

        if star == 'Sun' or star == 'Tagesstern' or star == '5774694':
            if name is 'age':
                plot_line(4.572, n, bins, plt, 'r--')
            if name is 'M':
                plot_line(1, n, bins, plt, 'r--')

        #if y_central is not None:
        #    mean = np.mean(y_central[:,pred_j])
        #    std = np.std(y_central[:,pred_j])
        #
        #    plot_line(mean, n, bins, plt, 'r--')
        #    plot_line(mean+std, n, bins, plt, 'r-.')
        #    plot_line(mean-std, n, bins, plt, 'r-.')

        q_16, q_50, q_84 = corner.quantile(predict[:, pred_j],
                                           [0.16, 0.5, 0.84])
        q_m, q_p = q_50 - q_16, q_84 - q_50
        plot_line(q_50, n, bins, plt, 'k--')
        plot_line(q_16, n, bins, plt, 'k-.')
        plot_line(q_84, n, bins, plt, 'k-.')

        # Format the quantile display.
        fmt = "{{0:{0}}}".format(".3g").format
        title = r"${{{0}}}_{{-{1}}}^{{+{2}}}$"
        title = title.format(fmt(q_50), fmt(q_m), fmt(q_p))

        ax.annotate(r"$\epsilon = %.3g\%%$" % (s / m * 100),
                    xy=(0.99, 0.12),
                    xycoords='axes fraction',
                    horizontalalignment='right',
                    verticalalignment='right')

        P.xlabel(y_latex[y_names[pred_j]] + " = " + title)
        P.locator_params(axis='x', nbins=3)

        xs = [m - 4 * s, m + 4 * s]  #[max(0, m-4*s), m+4*s]

        xticks = [m - 3 * s, m, m + 3 * s]  #[max(0, m-3*s), m, m+3*s]
        ax.set_xticks(xticks)
        ax.set_xlim(xs)
        ax.set_xticklabels(['%.3g' % xtick for xtick in xticks])
        ax.set_yticklabels('', visible=False)

        ax.set_frame_on(False)
        ax.get_xaxis().tick_bottom()
        ax.axes.get_yaxis().set_visible(False)

        #xmin, xmax = ax1.get_xaxis().get_view_interval()
        ymin, ymax = ax.get_yaxis().get_view_interval()
        ax.add_artist(
            mpl.lines.Line2D(xs, (ymin, ymin), color='black', linewidth=2))

        #ax.minorticks_on()
        plt.tight_layout()

    plt.savefig(os.path.join(out_dir, star + '.pdf'), dpi=400)
    plt.close()
示例#50
0
  data_lines = f.readlines()[3:]

data = np.loadtxt(data_lines)

# plot numerical and exact solution in first subplot
# plot error in second subplot
plt.figure(1, figsize=(7.0,3.0))

plt.subplot(1,2,1)

plot1 = plt.plot(data[:,3], data[:,1], 'ro')
plot2 = plt.plot(data[:,4], data[:,1], '-b+', linewidth=2)
plt.xlabel('x-velocity (m/s)',labelpad=20)
plt.ylabel('y (m)')
plt.xlim(0.0, 20.0)
plt.ylim(0.0, 0.01)
plt.legend([plot1, plot2], ('MFIX','Exact'), 'best')
plt.title('(a)',x=0.5,y=-0.4)

plt.subplot(1,2,2)
plot3 = plt.plot(data[:,5], data[:,1], '-b+')
plt.xlabel('Error (m/s)',labelpad=20)
plt.ylabel('y (m)')
plt.locator_params(axis='x',nbins=5)
plt.title('(b)',x=0.5,y=-0.4)

plt.subplots_adjust(wspace=0.5)

plt.savefig('tfm01_01.png', bbox_inches='tight')
plt.close(1)
示例#51
0
    # Say we want detect extrema using parabolic method
    emd = eemd.EMD
    emd.extrema_detection = "parabol"
    # Execute EEMD on S
    eIMFs = eemd.eemd(S)
    return eIMFs


if __name__ == "__main__":
    # Define signal
    data = cwru_input.read_matdata('../data/12k_Drive_End_B007_0_118.mat')
    print(data.shape)
    S = data[0:400]
    t = np.linspace(0, 1, 400)
    eIMFs = eemd(S)
    nIMFs = eIMFs.shape[0]
    # Plot results
    plt.figure(figsize=(12, 9))
    plt.subplot(nIMFs + 1, 1, 1)
    plt.plot(t, S, 'r')

    for n in range(nIMFs):
        plt.subplot(nIMFs + 1, 1, n + 2)
        plt.plot(t, eIMFs[n], 'g')
        plt.ylabel("eIMF %i" % (n + 1))
        plt.locator_params(axis='y', nbins=5)

    plt.xlabel("Time [s]")
    plt.tight_layout()
    plt.savefig('105', dpi=120)
    plt.show()
def export_ts(ts, user_id, num_bins, toplot = False, saveplot = True, iresolution = None):
    if iresolution != None:
        fname = 'timeseries/byday-{0}s-{1}.dat'.format(iresolution, user_id)
    else:
        fname = 'timeseries/byday-1s-{0}.dat'.format(user_id)
    
    ofile = open(fname, 'w')

    if toplot == True or saveplot == True:
        f, axarr = pylab.subplots(len(ts), sharex = True)

    if iresolution != None:
        num_bins_coarse = num_bins / iresolution # Account for the fact that we plan to coarsen the timeseries

    for axind, day in enumerate(ts):
        if len(axarr) == 1:
            ax = axarr
        else:
            ax = axarr[axind]

        if day[0] == None: # We've hit on a day that didn't have any Twitter activity

            binarized = numpy.zeros(num_bins)
        else: # The day has legitimate tweets
            binarized = binarize_timeseries(day, num_bins)

        if iresolution != None:
            binarized = coarse_resolution(binarized, iresolution = iresolution)

            if day[0] != None: # We only need to plot the raster if there are points to plot
                if toplot == True or saveplot == True:
                    plot_raster(binarized, num_bins_coarse, axarr, axind)
            else: # We still need to fix how the axes look, even when we don't plot
                if toplot == True or saveplot == True:
                    ax.yaxis.set_visible(False)
        else:
            if day[0] != None:
                if toplot == True or saveplot == True:
                    plot_raster(binarized, num_bins, axarr, axind)
            else: # We still need to fix how the axes look, even when we don't plot
                if toplot == True or saveplot == True:
                    ax.yaxis.set_visible(False)

        for symbol in binarized:
            ofile.write("{0}".format(int(symbol)))

        ofile.write("\n")

    if toplot == True or saveplot == True:
        if iresolution == None:
            pylab.xlabel('Time (each time tick corresponds to 1 s)')
        else:
            pylab.xlabel('Time (each time tick corresponds to {} s)'.format(iresolution))

        pylab.locator_params(axis = 'x', nbins = 5)

    if saveplot == True:
        if iresolution == None:
            pylab.savefig('raster-1s-{0}.pdf'.format(user_id))
        else:
            pylab.savefig('raster-{0}s-{1}.pdf'.format(iresolution, user_id))

        pylab.close(f)
    
    if toplot == True:
        pylab.show()

    ofile.close()
示例#53
0
def PlotSuperposition(fit_results,
                      composite_sample,
                      pure_samples,
                      k_min=None,
                      k_max=None,
                      interactive_plot=False,
                      output_folder=None):
    d_vec = fit_results["d_vec"]
    if not interactive_plot:
        plt.ioff()
        plt.close()
    axis_font, legend_font = GetFontsAndSetMPLproperties()
    fig = plt.figure(figsize=(12 / 2.54, 8 / 2.54))
    ax0 = fig.add_subplot(111)
    plt.locator_params(nbins=6)
    ax0.plot(composite_sample.wavenumber,
             composite_sample.absorbance,
             linewidth=2,
             label='data',
             color='r')
    fill_colors = ['b', 'g', 'm', 'r', 'c', 'm', 'k']
    hatches = ['/' * 3, '\\' * 3, '-' * 3, '*' * 3, 'o' * 3, 'O' * 3, '.' * 3]
    composite_sample_baseline_cut_fit = composite_sample.baseline[
        np.logical_and(composite_sample.wavenumber_cut > k_min,
                       composite_sample.wavenumber_cut < k_max)]
    for k, s in enumerate(pure_samples):
        ax0.fill_between(s.wavenumber_cut_fit,
                         composite_sample_baseline_cut_fit,
                         d_vec[k] * s.absorbance_corr_cut_fit +
                         composite_sample_baseline_cut_fit,
                         label='fit {}'.format(s.name),
                         alpha=0.5,
                         linewidth=0,
                         color=fill_colors[k],
                         hatch=hatches[k])
    absorbance_fit = np.dot(
        d_vec, np.vstack([s.absorbance_corr_cut_fit for s in pure_samples
                          ])) + composite_sample_baseline_cut_fit
    ax0.plot(composite_sample.wavenumber_cut_fit,
             absorbance_fit,
             'k--',
             dashes=(3, 3),
             label='fit',
             linewidth=2)
    ax0.plot(composite_sample.wavenumber_cut,
             composite_sample.baseline,
             linewidth=1.5,
             color='k',
             label='baseline')
    ax0.set_xlabel('wavenumber / cm$^{-1}$')
    ax0.set_ylabel('absorbance')
    min_, max_ = np.min(absorbance_fit), np.max(absorbance_fit)
    ax0.set_ylim([min_ - (max_ - min_) / 10, max_ + (max_ - min_) / 10])
    if k_min != None:
        ax0.set_xlim([k_min, ax0.get_xlim()[1]])
    if k_max != None:
        ax0.set_xlim([ax0.get_xlim()[0], k_max])
    legend = ax0.legend(loc='best', prop=legend_font, handlelength=3)
    legend.set_draggable(True)
    ax0.invert_xaxis()
    if output_folder != None:
        if not os.path.exists(output_folder):
            os.makedirs(output_folder)
        plt.savefig(
            os.path.join(output_folder,
                         'FTIR_' + composite_sample.name + '.png'))
    if interactive_plot:
        plt.show()
def main():
    # prepare data
    verts, tris = load_mesh('meshes/bumpy_cube6.obj')
    K = 8 + 6 + 12
    mu = 20.0
    maxiter = 500
    num_experiments = 1

    # prepare data structures
    ResultData = namedtuple('ResultData', 'Phi info logger')
    data_per_algo = defaultdict(list)
    # parameters passed to algorithms - to be fair,
    # turn off automatic penalty adjustment in our method
    # set check_interval = 1 to force callback to be called in every iteration
    algorithms = [
        ('osher', cmm.solve_compressed_osher,
            dict()),
        ('ours', cmm.solve_compressed_splitorth,
            dict(auto_adjust_penalty=False, check_interval=1)),
    ]

    # run the algorithm with same input mesh and
    # different random initializations
    print "will now run the method with %d different initializations" % num_experiments
    print "the primal residual with our method should consistently be lower compared to Oshers et al."
    for random_seed in range(num_experiments):
        np.random.seed(random_seed)
        Phi_init = np.random.random((len(verts), K))
        print "--"
        print "running experiment %d" % random_seed
        for name, method, options in algorithms:
            Phi, info, logger = test_convergence(
                verts, tris, K, mu,
                Phi_init, method, maxiter, **options)
            print name, info['r_primal']
            data_per_algo[name].append(ResultData(Phi, info, logger))

    # visualize results
    import matplotlib
    if len(sys.argv) > 1:
        matplotlib.use('pgf') # TODO PGF
    else:
        matplotlib.use('WxAgg')

    import pylab as pl
    pl.rc('text', usetex=True)
    pl.rc('font', size=6)
    pl.figure(figsize=(0.9, 1.1))
    plot_fun = pl.semilogy
    plot_fun(data_per_algo['osher'][0].logger.r_primals, c='r',
             label=r'osher')
    plot_fun(data_per_algo['ours'][0].logger.r_primals, c='g',
             label=r'ours')
    pl.legend(loc='center right').draggable()
    pl.locator_params(nbins=4, axis='x')
    pl.xlabel('iteration')
    pl.ylabel(r'$  \left\Vert r \right\Vert_2$')
    pl.gca().yaxis.set_major_locator(matplotlib.ticker.LogLocator(numticks=4))

    if len(sys.argv) > 2:
        pl.savefig(sys.argv[1])  #, bbox_inches='tight')
    else:
        pl.ion()
        pl.show()

    # let us first visualize the modes where things go wrong
    # which are most probably those where the primal residual is very high
    Q = data_per_algo['osher'][0].info['Q']
    P = data_per_algo['osher'][0].info['P']
    worst_first = ((Q - P)**2).sum(axis=0).argsort()[::-1]

    show_weights(
        verts, tris,
        (data_per_algo['osher'][0].Phi[:, worst_first], 
         data_per_algo['ours'][0].Phi[:, worst_first]), 
        ('osher', 'ours')
    )