def diagram_act_single_layer(act,save_path=None):
    act = act[:1000]
    # the following line complains about $DISPLAY
    fig = plt.figure(figsize=(16,6))
    ax = plt.subplot2grid((1,4), (0,0))
    #x = range(act.shape[1])
    #y = numpy.mean(act, axis=0)
    #error = numpy.std(act, axis=0)
    #ax.errorbar(x, y, error, color='b', ecolor='r')
    ax.boxplot(act)
    ax.set_xticks([-1]+range(act.shape[1]) + [act.shape[1]])

    ax = plt.subplot2grid((1,4), (0,1))
    ax.hist(act.flatten(), bins=10)
    ax.locator_params(tight=True,nbins=5)

    cov = numpy.corrcoef(act)
    ax = plt.subplot2grid((1,4), (0,2))
    ax.matshow(cov, cmap=plt.cm.gray)
    ax.locator_params(tight=True,nbins=5)
    #plt.colorbar()

    ax = plt.subplot2grid((1,4), (0,3))
    ax.matshow(act[:50], cmap=plt.cm.gray)
    #plt.colorbar()

    if save_path is not None:
        plt.savefig(save_path)
    else:
        plt.show()
Example #2
0
    def graficar_todas(self):
        plt.close('all')
        f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col')
        ax1.plot(self.t, self.data['w'], linewidth=1, label='$\omega$')
        ax1.legend(loc=0)
        ax2.plot(self.t, self.data['d'], linewidth=1, label='$\delta$')
        ax2.legend(loc=0)
        [ax3.plot(self.t, self.data['V'][:, p], linewidth=1, label='$'+nombre+'$') for p,nombre in zip(range(4),self.nombres['V'])]
        ax3.legend(loc=0)
        [ax4.plot(self.t, self.data['I'][:, p], linewidth=1, label='$'+nombre+'$') for p,nombre in zip(range(6), self.nombres['I'])]
        ax4.legend(loc=0)

        f1 = plt.figure()

        ax3 = plt.subplot2grid((2, 2), (1, 0), colspan=2)
        ax2 = plt.subplot2grid((2, 2), (0, 0))
        ax1 = plt.subplot2grid((2, 2), (0, 1))

        ax1.plot(self.data['d'], self.data['Te'], linewidth=1)
        ax1.set_xlabel('$\delta$')
        ax1.set_ylabel('$T_e$')

        ax2.plot(self.data['w'], self.data['d'], linewidth=1)
        ax2.set_xlabel('$\omega_r$')
        ax2.set_ylabel('$\delta$')

        ax3.plot(self.t, self.data['Te'], linewidth=1, label='$T_e$')
        ax3.plot(self.t, self.data['Tm'], linewidth=1, label='$T_m$')
        ax3.set_xlabel('$t$')
        ax3.set_ylabel('Par')

        ax3.legend(loc=1)

        plt.show()
Example #3
0
 def plot_probability_calibration_curves(self):
 
     """ Compute true and predicted probabilities for a calibration plot 
         fraction_of_positives - The true probability in each bin (fraction of positives).
         mean_predicted_value - The mean predicted probability in each bin.
     """
     
     fig = plt.figure()
     ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2)
     ax2 = plt.subplot2grid((3, 1), (2, 0), rowspan=2)
     
     ax1.set_ylabel("Fraction of positives")
     ax1.set_ylim([-0.05, 1.05])
     ax1.legend(loc="lower right")
     ax1.set_title('Calibration plots  (reliability curve) ' + self.description)
 
     ax2.set_xlabel("Mean predicted value")
     ax2.set_ylabel("Count")
     ax2.legend(loc="upper center", ncol=2)
     
     clf_score = brier_score_loss(self.y_true, self.y_pred, pos_label=1)
     
     
     fraction_of_positives, mean_predicted_value = calibration_curve(self.y_true, self.y_pred, n_bins=50)
     
     ax1.plot(mean_predicted_value, fraction_of_positives, "s-", color="#660066",  alpha = 0.6, label="%s (%1.3f)" % (self.description, clf_score))
     ax2.hist(self.y_pred, range=(0, 1), bins=50, color="#660066", linewidth=2.0 , alpha = 0.6, label="%s (%1.3f)" % (self.description, clf_score), histtype="step", lw=2)
     plt.yscale('log')
     return
Example #4
0
def run_div_test(fld, exact, show=False, ignore_inexact=False):
    t0 = time()
    result_numexpr = viscid.div(fld, preferred="numexpr", only=True)
    t1 = time()
    logger.info("numexpr magnitude runtime: %g", t1 - t0)

    result_diff = viscid.diff(result_numexpr, exact[1:-1, 1:-1, 1:-1])
    if not ignore_inexact and not (result_diff.data < 5e-5).all():
        logger.warn("numexpr result is far from the exact result")
    logger.info("min/max(abs(numexpr - exact)): %g / %g",
                np.min(result_diff.data), np.max(result_diff.data))

    planes = ["y=0f", "z=0f"]
    nrows = 2
    ncols = len(planes)
    ax = plt.subplot2grid((nrows, ncols), (0, 0))
    ax.axis("equal")

    for i, p in enumerate(planes):
        plt.subplot2grid((nrows, ncols), (0, i), sharex=ax, sharey=ax)
        mpl.plot(result_numexpr, p, show=False)
        plt.subplot2grid((nrows, ncols), (1, i), sharex=ax, sharey=ax)
        mpl.plot(result_diff, p, show=False)

    if show:
        mpl.mplshow()
Example #5
0
    def __init__(self):

        self.fps = 20

        self.N = 10
        self.L = 15

        self.x=np.array([])
        self.y=np.array([])

        self.fig1 = plt.figure(figsize = (6,16))
        self.ax1 = plt.subplot2grid((3,2),(1,0),colspan=2,rowspan=2)
        self.ax1.set_xlim([0,self.L])
        self.ax1.set_ylim([0,self.L])
        self.ax1.set_xlabel('Simulation')

        self.ax2 = plt.subplot2grid((3,2),(0,0),colspan=2)
        self.ax2.set_xlim([0,10])
        self.ax2.set_ylim([0,1.1])
        self.ax2.set_xlabel('Time',labelpad=-15)
        self.ax2.set_ylabel('$\phi$',fontsize=20)
        self.line, = self.ax2.plot([],[])

        #To do the animation
        self.ani1 = animation.FuncAnimation(self.fig1,self.update_scatt,interval =self.fps ,init_func = self.setup_plot,blit=True)
    def initialize(self):
        self.fig.suptitle('monitoring sample', size=12)
        self.fig.subplots_adjust(left=0.05, bottom=0.1, right=0.95, top=0.90, wspace=0.2, hspace=0.6)
        self.ax00 = plt.subplot2grid((2,2),(0,0))
        self.ax10 = plt.subplot2grid((2,2),(1,0))
        self.ax01 = plt.subplot2grid((2,2),(0,1))
        self.ax11 = plt.subplot2grid((2,2),(1,1))
        self.ax00.grid(True)
        self.ax10.grid(True)
        self.ax01.grid(True)
        self.ax11.grid(True)
        self.ax00.set_title('real time result')
        self.ax10.set_title('histogram')
        self.ax01.set_title('correlation')
        self.ax11.set_title('optimized result')
        self.ax00.set_xlabel('x')
        self.ax00.set_ylabel('y')
        self.ax01.set_xlabel('correct')
        self.ax01.set_ylabel('predict')
        self.ax11.set_xlabel('correct')
        self.ax11.set_ylabel('predict')

        # プロットの初期化
        self.lines000, = self.ax00.plot([-1,-1],[1,1],label='y1')
        self.lines001, = self.ax00.plot([-1,-1],[1,1],label='y2')
        self.lines100 = self.ax10.hist([-1,-1],label='res1')
        self.lines101 = self.ax10.hist([-1,-1],label='res2')
        self.lines01, = self.ax01.plot([-1,-1],[1,1],'.')
        self.lines11, = self.ax11.plot([-1,-1],[1,1],'.r')
Example #7
0
def fit_and_plot(cand, spd):
    data = cand.profile
    n = len(data)
    rms = np.std(data[(n/2):])
    xs = np.linspace(0.0, 1.0, n, endpoint=False)
    G = gauss._compute_data(cand)
    print "    Reduced chi-squared: %f" % (G.get_chisqr(data) / G.get_dof(n))
    print "    Baseline rms: %f" % rms
    print "    %s" % G.components[0]

    fig1 = plt.figure(figsize=(10,10))
    plt.subplots_adjust(wspace=0, hspace=0)

    # upper
    ax1 = plt.subplot2grid((3,1), (0,0), rowspan=2, colspan=1)
    ax1.plot(xs, data/rms, color="black", label="data")
    ax1.plot(xs, G.components[0].make_gaussian(n), color="red", label="best fit")

    # lower
    ax2 = plt.subplot2grid((3,1), (2,0), sharex=ax1)
    ax2.plot(xs, data/rms - G.components[0].make_gaussian(n), color="black", label="residuals")
    ax2.set_xlabel("Fraction of pulse window")

    plt.figure()
    plt.pcolormesh(xs, spd.waterfall_freq_axis(), spd.data_zerodm_dedisp, cmap=Greys)
    plt.xlabel("Fraction of pulse window")
    plt.ylabel("Frequency (MHz)")
    plt.xlim(0, 1)
    plt.ylim(spd.min_freq, spd.max_freq)

    plt.show()
Example #8
0
 def __init__(self, figsize=(10, 7), stacked=True, title='', stamp=True,
              autodiffs=True, **kwargs):
     '''
     :param (int, int) figsize:
         The size of the figure.
     :param bool stacked:
         If true then the resulting graph will be stacked
     :param string title:
         Title of the graph
     :param boolean/string stamp:
         Put a timestamp in the bottom right corner.
         If True the current time will be stamped.
         If string then the string concatenated with the current time
         will be stamped.
     :param boolean autodiffs:
         Indicates whether the diffs should be computed automatically if
         they are not specified.
     '''
     super(DiffPlotter, self).__init__(figsize=figsize, stacked=stacked,
                                       title=title, stamp=stamp, **kwargs)
     self.autodiffs = autodiffs
     self.ax1 = plt.subplot2grid((4, 1), (0, 0), rowspan=3)
     #plt.title(title)
     plt.setp(self.ax1.get_xticklabels(), visible=False)
     self.ax2 = plt.subplot2grid((4, 1), (3, 0), sharex=self.ax1)
     plt.subplots_adjust(hspace=.15)
Example #9
0
def fempipeWidget(alpha, pipedepth):
    respEW, respNS, X, Y = fempipe(alpha, pipedepth)
    fig = plt.figure(figsize = (8, 6))
    ax0 = plt.subplot2grid((2,2), (0,0))
    ax1 = plt.subplot2grid((2,2), (0,1))
    ax2 = plt.subplot2grid((2,2), (1,0), colspan=2)

    dat0 = ax0.imshow(respEW.real*100, extent=[X.min(),X.max(),Y.min(),Y.max()])
    dat1 = ax1.imshow(respNS.real*100, extent=[X.min(),X.max(),Y.min(),Y.max()])
    cb0 = plt.colorbar(dat0, ax = ax0)
    cb1 = plt.colorbar(dat1, ax = ax1)
    ax0.set_title("In-phase EW boom (%)", fontsize = 12)
    ax1.set_title("In-phase NS boom (%)", fontsize = 12)
    ax0.set_xlabel("Easting (m)", fontsize = 12)
    ax1.set_xlabel("Easting (m)", fontsize = 12)
    ax0.set_ylabel("Northing (m)", fontsize = 12)
    ax1.set_ylabel("Northing (m)", fontsize = 12)
    ax0.plot(np.r_[0., 0.], np.r_[-10., 10.], 'k--', lw=2)
    ax1.plot(np.r_[0., 0.], np.r_[-10., 10.], 'k--', lw=2)

    ax2.plot(Y[:,20],respEW[:, 20].real, 'k.-')
    ax2.plot(Y[:,20],respEW[:, 20].imag, 'k--')
    ax2.plot(Y[:,20],respNS[:, 20].real, 'r.-')
    ax2.plot(Y[:,20],respNS[:, 20].imag, 'r--')
    ax2.legend(('In-phase EW boom', 'Out-of-phase EW boom', 'In-phase NS boom', 'Out-of-phase NS boom'),loc=4)
    ax2.grid(True)
    ax2.set_ylabel('Hs/Hp (%)', fontsize = 16)
    ax2.set_xlabel('Northing (m)', fontsize = 16)
    ax2.set_title('Northing profile line at Easting 0 m', fontsize = 16)

    plt.tight_layout()
    plt.show()
Example #10
0
    def plot(self, data, notebook=False, show=True, savename=None):

        fig = pyplot.figure()
        ncenters = len(self.centers)

        colorizer = Colorize()
        colorizer.get = lambda x: self.colors[int(self.predict(x)[0])]

        # plot time series of each center
        # TODO move into a time series plotting function in viz.plots
        for i, center in enumerate(self.centers):
            ax = pyplot.subplot2grid((ncenters, 3), (i, 0))
            ax.plot(center, color=self.colors[i], linewidth=5)
            fig.add_axes(ax)

        # make a scatter plot of the data
        ax2 = pyplot.subplot2grid((ncenters, 3), (0, 1), rowspan=ncenters, colspan=2)
        ax2, h2 = scatter(data, colormap=colorizer, ax=ax2)
        fig.add_axes(ax2)

        plugins.connect(fig, HiddenAxes())

        if show and notebook is False:
            mpld3.show()

        if savename is not None:
            mpld3.save_html(fig, savename)

        elif show is False:
            return mpld3.fig_to_html(fig)
def createExamples() :
    numbersWeHave = range (0, 1)
    versionsWeHave = range(1, 5)
    path = '/Users/ahmadbarakat/Downloads/CCEP Term 7/Pattern Recognition/ass1/ass1/data/'
    
    arr = []    
    for eachNum in numbersWeHave:
        for eachVer in versionsWeHave:
            imgFilePath = path + str(eachNum) + '-' + str(eachVer) + '.jpg'
            ei = Image.open(imgFilePath)
            eiar = np.asarray(ei)
            print str(eachNum) + '-' + str(eachVer)
            eiar = threshold(eiar)
            arr.append(eiar)

        fog = plt.figure()
        ax1 = plt.subplot2grid((2,2), (0,0))
        ax2 = plt.subplot2grid((2,2), (0,1))
        ax3 = plt.subplot2grid((2,2), (1,0))
        ax4 = plt.subplot2grid((2,2), (1,1))

        ax1.imshow(graphArr(arr[0]))
        ax2.imshow(graphArr(arr[1]))
        ax3.imshow(graphArr(arr[2]))
        ax4.imshow(graphArr(arr[3]))
        plt.show()
Example #12
0
def calibration_curve_plotter(y_test, prob_pos, n_bins=10):

    brier = brier_score_loss(y_test, prob_pos, pos_label=1)

    fig = plt.figure(0, figsize=(10, 10))
    ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2)
    ax2 = plt.subplot2grid((3, 1), (2, 0))

    df = pd.DataFrame({"true": y_test})
    bins = np.linspace(0.0, 1.0, n_bins + 1)
    binids = np.digitize(prob_pos, bins) - 1
    df["Bin center"] = bins[binids] + 0.5 / n_bins
    df[""] = "Model calibration: (%1.5f)" % brier
    o = bins + 0.5 / n_bins

    df2 = pd.DataFrame({"true": o, "Bin center": o})
    df2[""] = "Perfect calibration"

    df = pd.concat([df, df2])

    sns.pointplot(x="Bin center", y="true", data=df, order=o, hue="", ax=ax1)

    ax2.hist(prob_pos, range=(0, 1), bins=10, label="Model", histtype="step", lw=2)

    ax1.set_ylabel("Fraction of positives")
    ax1.set_ylim([-0.05, 1.05])
    # ax1.legend(loc="lower right")
    ax1.set_title("Calibration plots")

    ax2.set_xlabel("Predicted Probability")
    ax2.set_ylabel("Count")

    plt.tight_layout()
def plot_scaling():
    X, y = make_blobs(n_samples=50, centers=2, random_state=4, cluster_std=1)
    X += 3

    plt.figure(figsize=(15, 8))
    main_ax = plt.subplot2grid((2, 4), (0, 0), rowspan=2, colspan=2)

    main_ax.scatter(X[:, 0], X[:, 1], c=y, cmap=cm2, s=60)
    maxx = np.abs(X[:, 0]).max()
    maxy = np.abs(X[:, 1]).max()

    main_ax.set_xlim(-maxx + 1, maxx + 1)
    main_ax.set_ylim(-maxy + 1, maxy + 1)
    main_ax.set_title("Original Data")
    other_axes = [plt.subplot2grid((2, 4), (i, j)) for j in range(2, 4) for i in range(2)]

    for ax, scaler in zip(other_axes, [StandardScaler(), RobustScaler(),
                                       MinMaxScaler(), Normalizer(norm='l2')]):
        X_ = scaler.fit_transform(X)
        ax.scatter(X_[:, 0], X_[:, 1], c=y, cmap=cm2, s=60)
        ax.set_xlim(-2, 2)
        ax.set_ylim(-2, 2)
        ax.set_title(type(scaler).__name__)

    other_axes.append(main_ax)

    for ax in other_axes:
        ax.spines['left'].set_position('center')
        ax.spines['right'].set_color('none')
        ax.spines['bottom'].set_position('center')
        ax.spines['top'].set_color('none')
        ax.xaxis.set_ticks_position('bottom')
        ax.yaxis.set_ticks_position('left')
Example #14
0
def test(ym):
    
    runtime = 1000.*100

    V, t = ym.simulate(runtime)
    df = ym.sim_data
    print "Sim done"

    Q = df.loc[0.0:,'Q10':'Q40']
    C = df.loc[0.0:,'C10':'C40']
    
    Qt = Q.T
    Ct = C.T
    
    fig = plt.figure()
    '''
    ax1 = plt.subplot(211)
    ax2 = plt.subplot(222)
    ax3 = plt.subplot(223)
    '''
    ax1 = plt.subplot2grid((2,2), (0,0), colspan=2)
    ax2 = plt.subplot2grid((2,2), (1,0))
    ax3 = plt.subplot2grid((2,2), (1, 1))


    ax1.plot(t,V)
    
    plot_states(ax2,Qt,t)
    plot_states(ax3,Ct,t)

    plt.tight_layout()
    return plt.gca()
    
    '''
Example #15
0
def _onclick_help(event, params):
    """Function for drawing help window"""
    import matplotlib.pyplot as plt
    text, text2 = _get_help_text(params)

    width = 6
    height = 5

    fig_help = figure_nobar(figsize=(width, height), dpi=80)
    fig_help.canvas.set_window_title('Help')
    ax = plt.subplot2grid((8, 5), (0, 0), colspan=5)
    ax.set_title('Keyboard shortcuts')
    plt.axis('off')
    ax1 = plt.subplot2grid((8, 5), (1, 0), rowspan=7, colspan=2)
    ax1.set_yticklabels(list())
    plt.text(0.99, 1, text, fontname='STIXGeneral', va='top', weight='bold',
             ha='right')
    plt.axis('off')

    ax2 = plt.subplot2grid((8, 5), (1, 2), rowspan=7, colspan=3)
    ax2.set_yticklabels(list())
    plt.text(0, 1, text2, fontname='STIXGeneral', va='top')
    plt.axis('off')

    tight_layout(fig=fig_help)
    # this should work for non-test cases
    try:
        fig_help.canvas.draw()
        fig_help.show(warn=False)
    except Exception:
        pass
Example #16
0
def renderGraphs_pair(tracks, params, filename):

  arena_frame = plt.subplot2grid((1,2), (0,0))
  arena_frame.set_title("Rat track [Arena frame]")
  arena_frame.set_xlim([params["arena_x"]-params["diameter"]/2-5,params["arena_x"]+params["diameter"]/2+5])
  arena_frame.set_ylim([params["arena_y"]-params["diameter"]/2-5,params["arena_y"]+params["diameter"]/2+5])
  arena_frame.set_aspect('equal', adjustable='box')
  arena_frame.axis('off')
  arena_frame.add_artist(plt.Circle((params["arena_x"],params["arena_y"]),params["diameter"]/2,color='r',fill=False))
  arena_frame.plot([float(f[2]) for f in tracks[0] if f[2] is not '0'], [float(f[3]) for f in tracks[0] if f[3] is not '0'])

  xvals = []
  yvals = []
  for i in range(min(len(tracks[0]), len(tracks[1]))):
    # god forgive me
    if not ((tracks[0][i][2] is '0' and tracks[0][i][3] is '0') or (tracks[1][i][2] is '0' and tracks[1][i][3] is '0')):
      xvals.append(float(tracks[0][i][2]) - float(tracks[1][i][2]))
      yvals.append(float(tracks[0][i][3]) - float(tracks[1][i][3]))

  robot_frame = plt.subplot2grid((1,2), (0,1))
  robot_frame.set_title("Rat track [Robot frame]")
  robot_frame.set_xlim(-params["diameter"], params["diameter"])
  robot_frame.set_ylim(-params["diameter"], params["diameter"])
  robot_frame.set_aspect('equal', adjustable='box')
  robot_frame.axis('off')
  robot_frame.add_artist(plt.Circle((0, 0), params["shock_radius"],color='y',fill=False))
  robot_frame.plot(xvals, yvals) 

  # histogram = plt.subplot2grid((2,2), (1,0), colspan=2)

  plt.savefig(filename)
Example #17
0
def overlap_plot(profile_A, profile_B, w, y_slice):
    """Make nice overlap plots.
    """
    import matplotlib.pyplot as plt
    plt.figure()
    plt.subplots_adjust(hspace=0.001)
    
    norm_product_AB = profile_A * profile_B 
    ax1 = plt.subplot2grid((4, 1), (0, 0), rowspan=3)
    ax1.plot(profile_A, label='Source A', linewidth=4, color='b')
    ax1.plot(profile_B, label='Source B', linewidth=4, color='g')
    ax1.tick_params(axis='both', which='major', labelsize=16)
    plt.yticks(np.arange(0, 1.2, 0.2))
    plt.legend()
    
    ax2 = plt.subplot2grid((4, 1), (3, 0), rowspan=1)
    ax2.plot(norm_product_AB, label='Product', linewidth=4, color='r')
    plt.yticks(np.arange(0, 0.5, 0.25))
    plt.ylim(0, 0.5)
    ax2.tick_params(axis='both', which='major', labelsize=16)
    
    pos = np.linspace(0, profile_A.size, 11)
    y_labels, x_labels = w.wcs_pix2world(pos, np.ones_like(pos) * y_slice, 0)
    plt.xticks(pos, x_labels)
    plt.setp(ax1.get_xticklabels(), visible=False)
    plt.legend()    
def plot_fg(fg, data1, data2, bound):
    # obtain estimates
    e_mu1 = fg["mu1"].moments(0)
    e_mu2 = fg["mu2"].moments(0)
    e_d1  = normal_distribution_t(e_mu1, 100)
    e_d2  = normal_distribution_t(e_mu2, 100)
    if e_mu1 < e_mu2:
        d1 = fg["mu1"]
        d2 = fg["mu2"]
    else:
        d1 = fg["mu2"]
        d2 = fg["mu1"]
    # plot result
    plt.clf()
    ax1 = plt.subplot2grid((3,2), (0,0), colspan=2)
    ax2 = plt.subplot2grid((3,2), (1,0))
    ax3 = plt.subplot2grid((3,2), (1,1))
    ax4 = plt.subplot2grid((3,2), (2,0), colspan=2)
    plot_density(ax1, e_d1, [-0.5, 0.5], xlab="x", ylab="density")
    plot_density(ax1, e_d2, [-0.5, 0.5], xlab="x", ylab="density")
    plot_density(ax2, d1, [-0.2, 0.0], xlab=r'$\mu_1$', ylab="density")
    plot_density(ax3, d2, [ 0.0, 0.2], xlab=r'$\mu_2$', ylab="density")
    ax1.hist(data1, 50, normed=1)
    ax1.hist(data2, 50, normed=1)
    ax4.plot(bound)
    ax4.set_xlabel("iteration")
    ax4.set_ylabel("bound")
    plt.tight_layout()
    #plt.savefig("factor-graph-test-5.png")
    plt.show()
Example #19
0
def visualize_solution(input_matrix, solved_matrix, graph, zero_indices, ts, out_path, time_to_solution):
    """
    Plotting function that takes sudoku solution and intermediate data
    and plots them. It create three axes, one for plotting the progress,
    one for printing the summary metrics, and one for solved sudoku.
    """
    # prepare figure window for plotting
    plt.figure().patch.set_facecolor('white')
    plt.suptitle('Sudoku Solver\n', fontsize=20, color=plt.cm.Blues(0.9))

    # plot the performance: number of iterations vs. numbers filled
    ax1 = plt.subplot2grid((20, 3), (1, 0), rowspan=17, colspan=2)
    [x, y, ylabels, eval_histogram, n] = generate_progress_data(graph, zero_indices)
    max_eval_per_cell = max(eval_histogram)
    total_iterations = n
    plot_decorate_performance_data(ax1, x, y, ylabels)

    # work on sudoku box area
    ax2 = plt.subplot2grid((20, 3), (10, 2), rowspan=10, colspan=1)
    create_colorbar(ax2, max_eval_per_cell)
    decorate_sudoku_box(ax2)
    fill_numbers(ax2, input_matrix, solved_matrix)
    shade_cell_by_difficulty(ax2, zero_indices, eval_histogram, max_eval_per_cell)

    # work on statistics area
    ax3 = plt.subplot2grid((20, 3), (1, 2), rowspan=7, colspan=1)
    time_to_plot = time.time() - ts
    write_statistics(ax3, time_to_solution, time_to_plot, total_iterations)

    # save figure and show
    plt.savefig(out_path)
    plt.show()
Example #20
0
def plot_all_3(results, colors):
  x = np.arange(len(results))

  m_results = tuple(r[1] for r in results)
  m_results = tuple(zip(*m_results)) # transpose
  m_results = tuple((m,r) for m, r in zip(methods, m_results))
  print(m_results)

  ax1 = plt.subplot2grid((3, 5), (0, 1), colspan=4)
  ax2 = plt.subplot2grid((3, 5), (1, 1), colspan=4)
  ax3 = plt.subplot2grid((3, 5), (2, 1), colspan=4)
  ax1.set_title('all')
  ax2.set_ylabel('time / ms')
  ax2.set_ylim(0,m_results[-3][1][-1])
  ax3.set_ylim(0,0.05)

  ax1.set_xticks(x)
  ax1.set_xticklabels(['' for _ in range(5)])
  ax2.set_xticks(x)
  ax2.set_xticklabels(['' for _ in range(5)])
  ax3.set_xticks(x)
  ax3.set_xticklabels([r[0] for r in results])

  for idx, m_result in enumerate(m_results):
    title, times = m_result
    ax1.plot(x, times, color=colors[idx])
    ax2.plot(x, times, color=colors[idx])
    ax3.plot(x, times, color=colors[idx])
    
    # ax2.barh(x[:-2], times[:-2], color=colors[:-2])
    # plt.savefig(title+'.png')
  plt.show()
Example #21
0
    def plot(self, show: bool=True):
        """Plot the analyzed image. Shows both flatness & symmetry.

        Parameters
        ----------
        show : bool
            Whether to show the plot when called.
        """
        if not self._is_analyzed:
            raise NotAnalyzed("Image is not analyzed yet. Use analyze() first.")
        # set up axes
        plt.ioff()
        vert_flat_ax = plt.subplot2grid((3, 3), (0, 0))
        vert_sym_ax = plt.subplot2grid((3, 3), (1, 0))
        image_ax = plt.subplot2grid((3, 3), (0, 1), colspan=2, rowspan=2)
        horiz_flat_ax = plt.subplot2grid((3, 3), (2, 1))
        horiz_sym_ax = plt.subplot2grid((3, 3), (2, 2))

        # plot flat/sym axis
        self._plot_flatness('vertical', vert_flat_ax)
        self._plot_flatness('horizontal', horiz_flat_ax)
        self._plot_symmetry('vertical', vert_sym_ax)
        self._plot_symmetry('horizontal', horiz_sym_ax)

        # plot image and profile lines
        self._plot_image(image_ax, title=osp.basename(self.path))

        plt.suptitle("Flatness & Symmetry")
        if show:
            plt.show()
Example #22
0
def viewQueryGoalProportions(distribution_1, distribution_2):
    
    proportion_1, goals_1, max_goal_1 = infoDistribution(distribution_1)
    proportion_2, goals_2, max_goal_2 = infoDistribution(distribution_2)

    width = 0.5 # gives histogram aspect to the bar diagram

    plt.figure(figsize=(16, 5))
    ax1 = plt.subplot2grid((1, 2), (0, 0))
    #plt.subplot(1,2,1)
    #ax = plt.axes()
    #ax.set_xticks(pos_1 + (width / 2))
    #ax.set_xticklabels(goals_1)
    ax1.bar(goals_1, proportion_1, width, color='r')
    plt.ylabel('Goals distribution for query')
    plt.xlabel('Goals Distribution 1')   
    plt.grid(True)  
    
    ax2= plt.subplot2grid((1, 2), (0, 1))
    ax2.bar(goals_2, proportion_2, width, color='blue')
    plt.ylabel('Goals distribution for query')
    plt.xlabel('Goals Distribution 2')        
    
    plt.grid(True)    
    plt.show()
    
    max_goals = []
    max_goals.append(max_goal_1)
    max_goals.append(max_goal_2)

    return max_goals
Example #23
0
def visualize_tss(flare_class, prediction_threshold, flare_threshold,tss):
    hits_x = []
    hits_y = []
    miss_x = []
    miss_y = []

    for x,y in plot_xy:
        if (x>=prediction_threshold) == (y>=flare_threshold):
            hits_x.append(x)
            hits_y.append(y)
        else:
            miss_x.append(x)
            miss_y.append(y)

    plt.rcParams['figure.figsize'] = (12.8,9.6)
    plt.subplot2grid((1,1),(0,0), colspan=1, rowspan=1)
    plt.plot(miss_x, miss_y, 'mo',markersize=1.0, markeredgecolor='r')
    plt.plot(hits_x, hits_y, 'mo',markersize=1.0, markeredgecolor='b')
    plt.gca().set_xscale('log')
    plt.gca().set_yscale('log')
    plt.gca().set_xlabel("AIA 193nm thresholded sum ({})".format(threshold_value))
    plt.gca().set_ylabel("GOES 1-8A 24hour future max")
    filename = "Flarepredict-{}-{}.png".format(flare_class, threshold_value)
    plt.title("{}-class flare prediction with TI({}) : TSS = {}".format(flare_class, threshold_value, tss))

    plt.savefig(filename, dpi=100)
    plt.close("all")
Example #24
0
    def plot(self, weights=True, assets=True, portfolio_label='PORTFOLIO', **kwargs):
        """ Plot equity of all assets plus our strategy.
        :param weights: Plot weights as a subplot.
        :param assets: Plot asset prices.
        :return: List of axes.
        """
        res = ListResult([self], [portfolio_label])
        if not weights:
            ax1 = res.plot(assets=assets, **kwargs)
            return [ax1]
        else:
            plt.figure(1)
            ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2)
            res.plot(assets=assets, ax=ax1, **kwargs)
            ax2 = plt.subplot2grid((3, 1), (2, 0), sharex=ax1)

            # plot weights as lines
            if self.B.values.min() < -0.01:
                self.B.plot(ax=ax2, ylim=(min(0., self.B.values.min()), max(1., self.B.sum(1).max())),
                            legend=False, colormap=plt.get_cmap('jet'))
            else:
                # fix rounding errors near zero
                if self.B.values.min() < 0:
                    B = self.B - self.B.values.min()
                else:
                    B = self.B
                B.plot(ax=ax2, ylim=(0., max(1., B.sum(1).max())),
                       legend=False, colormap=plt.get_cmap('jet'), kind='area', stacked=True)
            plt.ylabel('weights')
            return [ax1, ax2]
Example #25
0
def calibration_plot(clf, xtest, ytest):
	prob = clf.predict_proba(xtest)[:, 1]
	outcome = ytest
	data = pd.DataFrame(dict(prob=prob, outcome=outcome))

	# group outcomes into bins of similar probability
	bins = np.linspace(0, 1, 20)
	cuts = pd.cut(prob, bins)
	binwidth = bins[1] - bins[0]

	# freshness ratio and number of examples in each bin
	cal = data.groupby(cuts).outcome.agg(['mean', 'count'])
	cal['pmid'] = (bins[:-1] + bins[1:]) / 2
	cal['sig'] = np.sqrt(cal.pmid * ( 1- cal.pmid) / cal['count'])

	# the calibration plot
	ax = plt.subplot2grid((3, 1), (0, 0), rowspan=2)
	p = plt.errorbar(cal.pmid, cal['mean'], cal['sig'])
	plt.plot(cal.pmid, cal.pmid, linestyle='--', lw=l, color='k')
	plt.ylabel("Empirical P(Fresh)")
	remove_border(ax)

	# the distirubtion of P(fresh)
	ax = plt.subplot2grid((3, 1), (2, 0), sharex=ax)

	plt.bar(left=cal.pmid - binwidth / 2, height=cal['count'],
			width=.95 * (bins[1] - bins[0]),
			fc=p[0].get_color())

	plt.xlabel("Predicted P(Fresh)")
	remove_border()
	plt.ylabel("Number")
Example #26
0
def paint_clustering(results, clusters, num, chrom, tad_names):
    dendros = []
    axes = []
    prev = 0
    xlim = [-100, 100]
    tmp = []
    for i, result in enumerate(results):
        if axes:
            axes[-1].set_xticklabels([], visible=False)
        clust = linkage(result, method='ward')
        tmp = dendrogram(clust, orientation='right', no_plot=True)['leaves']
        dendros += reversed(list([clusters[i][n] for n in tmp]))
        axes.append(plt.subplot2grid((num, 9),(prev, 0), rowspan=len(result),
                                     colspan=4))
        dendrogram(clust, orientation='right',
                   labels=[tad_names[c] for c in clusters[i]])
        if xlim[0] < axes[-1].get_xlim()[0]:
            xlim[0] = axes[-1].get_xlim()[0]
        if xlim[1] > axes[-1].get_xlim()[1]:
            xlim[1] = axes[-1].get_xlim()[1]
        prev += len(result)
    for ax in axes:
        ax.set_xlim(left=xlim[0], right=xlim[1])
    axes = []
    for i, j in enumerate(dendros):
        axes.append(plt.subplot2grid((num, 9),(i, 4)))#gs1[i]))
        chrom.visualize('exp1',
                        tad=chrom.get_experiment('exp1').tads[tad_names[j]],
                        axe=axes[-1], show=False)
        axes[-1].set_axis_off()
    ax4 = plt.subplot2grid((num, 9),(0, 5), rowspan=num, colspan=4)
    chrom.visualize('exp1', paint_tads=True, axe=ax4)
    plt.draw()
Example #27
0
def main():

  print "loading data.."
  # Open hdf5 file
  df = pd.read_csv('train.shuffled', nrows = 100);
  df = df.dropna();
  # specifies the parameters of our graphs
  fig = plt.figure(figsize=(18,6)); 
  alpha=alpha_scatterplot = 0.2 
  alpha_bar_chart = 0.55
   # lets us plot many diffrent shaped graphs together 
  ax1 = plt.subplot2grid((2,3),(0,0))
# plots a bar graph of those who surived vs those who did not.               
  df.click.value_counts().plot(kind='bar', alpha=alpha_bar_chart)
  ax1.set_xlim(-1, 2)
# puts a title on our graph
  plt.title("Distribution of click, (1 = clicked)")    

  plt.subplot2grid((2,3),(0,1))
  plt.scatter(df.click, df.banner_pos, alpha=alpha_scatterplot)
# sets the y axis lable
  plt.ylabel("banner pos")
# formats the grid line style of our graphs                          
  plt.grid(b=True, which='major', axis='y')  
  plt.title("Survial by banner pos,  (1 = clicked)")

  df["date_time"] = df['hour'].apply(string_to_date);
  dfts = df.set_index("date_time");
  time_mean =dfts.groupby(lambda x: x.time()).mean();
  print "saving data to hdf5"
  print time_mean
  test = pd.read_csv('test');
def plot_calibration_curve(est, name, fig_index):
    """Plot calibration curve for est w/o and with calibration. """
    # Calibrated with isotonic calibration
    isotonic = CalibratedClassifierCV(est, cv=2, method='isotonic')

    # Calibrated with sigmoid calibration
    sigmoid = CalibratedClassifierCV(est, cv=2, method='sigmoid')

    # Calibrated with ROC convex hull calibration
    rocch = CalibratedClassifierCV(est, cv=2, method='rocch')

    # Logistic regression with no calibration as baseline
    lr = LogisticRegression(C=1., solver='lbfgs')

    fig = plt.figure(fig_index, figsize=(10, 10))
    ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2)
    ax2 = plt.subplot2grid((3, 1), (2, 0))

    ax1.plot([0, 1], [0, 1], "k:", label="Perfectly calibrated")
    for clf, name in [(lr, 'Logistic'),
                      (est, name),
                      (isotonic, name + ' + Isotonic'),
                      (sigmoid, name + ' + Sigmoid'),
                      (rocch, name + ' + ROCConvexHull')]:
        clf.fit(X_train, y_train)
        y_pred = clf.predict(X_test)
        if hasattr(clf, "predict_proba"):
            prob_pos = clf.predict_proba(X_test)[:, 1]
        else:  # use decision function
            prob_pos = clf.decision_function(X_test)
            prob_pos = \
                (prob_pos - prob_pos.min()) / (prob_pos.max() - prob_pos.min())

        clf_score = brier_score_loss(y_test, prob_pos, pos_label=y.max())
        print("%s:" % name)
        print("\tBrier: %1.4f" % (clf_score))
        print("\tPrecision: %1.3f" % precision_score(y_test, y_pred))
        print("\tRecall: %1.3f" % recall_score(y_test, y_pred))
        print("\tF1: %1.3f" % f1_score(y_test, y_pred))
        print("\tAuc: %1.4f\n" % roc_auc_score(y_test, prob_pos))

        fraction_of_positives, mean_predicted_value = \
            calibration_curve(y_test, prob_pos, n_bins=10)

        ax1.plot(mean_predicted_value, fraction_of_positives, "s-",
                 label="%s (%1.4f)" % (name, clf_score))

        ax2.hist(prob_pos, range=(0, 1), bins=10, label=name,
                 histtype="step", lw=2)

    ax1.set_ylabel("Fraction of positives")
    ax1.set_ylim([-0.05, 1.05])
    ax1.legend(loc="lower right")
    ax1.set_title('Calibration plots  (reliability curve)')

    ax2.set_xlabel("Mean predicted value")
    ax2.set_ylabel("Count")
    ax2.legend(loc="upper center", ncol=2)

    plt.tight_layout()
Example #29
0
def plot_reg_hist(datafile):
    AVE_A = 0.42128425
    STD_A = 0.28714299
    AVE_Z = 0.09973424
    STD_Z = 0.05802179

    ID, real, res = ReadData(datafile)

    uAn = res[:, 0]
    rAn = real[:, 0]
    uZn = res[:, 1]
    rZn = real[:, 1]

    dAn = uAn - rAn
    dZn = uZn - rZn

    uA = 3.0 * STD_A * uAn + AVE_A
    uZ = 3.0 * STD_Z * uZn + AVE_Z

    rA = 3.0 * STD_A * rAn + AVE_A
    rZ = 3.0 * STD_Z * rZn + AVE_Z

    dA = uA - rA
    dZ = uZ - rZ

    fig = plt.figure(figsize=(17,8))
    ax1 = plt.subplot2grid((1,2), (0,0))
    ax2 = plt.subplot2grid((1,2), (0,1))
    ax1.set_title('Extinction SD Hist')
    ax1.hist(dA)
    ax2.set_title('Redshift SD Hist')
    ax2.hist(dZ)
    def plotDistanceInfo(self, title=None, printInfo=False):
        """
        Utility function for generating informative plots of the ensemble relative to the observation
        """
        if self.observation_type == dautils.ObservationType.UnderlyingFlow or \
           self.observation_type == dautils.ObservationType.DirectUnderlyingFlow:
            return self.plotVelocityInfo(title=title, printInfo=printInfo)

        fig = plt.figure(figsize=(10,6))
        gridspec.GridSpec(2, 3)

        # PLOT POSITIONS OF PARTICLES AND OBSERVATIONS
        ax0 = plt.subplot2grid((2,3), (0,0))
        plt.plot(self.observeParticles()[:,:,0].flatten(), \
                 self.observeParticles()[:,:,1].flatten(), 'b.')
        plt.plot(self.observeTrueState()[:,0], \
                 self.observeTrueState()[:,1], 'r.')
        ensembleMean = self.getEnsembleMean()
        if ensembleMean is not None:
            plt.plot(ensembleMean[0], ensembleMean[1], 'r+')
        plt.xlim(0, self.getDomainSizeX())
        plt.xlabel('x')
        plt.ylabel('y')
        plt.ylim(0, self.getDomainSizeY())
        plt.title("Particle positions")

        # PLOT DISCTRIBUTION OF PARTICLE DISTANCES AND THEORETIC OBSERVATION PDF
        ax0 = plt.subplot2grid((2,3), (0,1), colspan=2)
        innovations = self.getInnovationNorms()
        obs_var = self.getObservationVariance()
        plt.hist(innovations, bins=30, \
                 range=(0, max(min(self.getDomainSizeX(), self.getDomainSizeY()), np.max(innovations))),\
                 normed=True, label="particle innovations")

        # With observation 
        x = np.linspace(0, max(self.getDomainSizeX(), self.getDomainSizeY()), num=100)
        gauss_pdf = self.getGaussianWeight(x, normalize=False)
        plt.plot(x, gauss_pdf, 'g', label="pdf directly from innovations")
        plt.legend()
        plt.title("Distribution of particle innovations")

        # PLOT SORTED DISTANCES FROM OBSERVATION
        ax0 = plt.subplot2grid((2,3), (1,0), colspan=3)
        gaussWeights = self.getGaussianWeight()
        indices_sorted_by_observation = innovations.argsort()
        ax0.plot(gaussWeights[indices_sorted_by_observation]/np.max(gaussWeights), 'g', label="Gauss weight")
        ax0.set_ylabel('Relative weight')
        ax0.grid()
        ax0.set_ylim(0,1.4)
        plt.legend(loc=7)

        ax1 = ax0.twinx()
        ax1.plot(innovations[indices_sorted_by_observation], label="innovations")
        ax1.set_ylabel('Innovations from observation', color='b')

        plt.title("Sorted innovations from observation")

        if title is not None:
            plt.suptitle(title, fontsize=16)
        return fig
import numpy as np
import os
import glob
import sys
import scipy.integrate

#Load the data
path = os.environ["GWDir"]
name = path + 'ForPaper/MW_inclined/*.txt'
datafiles = glob.glob(name)

#Set up plotting environment
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
fig = plt.figure(figsize=(10, 10))
ax1 = plt.subplot2grid((1, 1), (0, 0))

#Load the data once
#ignore the first burst - useful if we start at periapsis


def process(datafile, Tintegration, color):

    length = np.loadtxt(datafile)
    length = len(length[:, 0])
    data = np.loadtxt(datafile, skiprows=int(length / 4))

    t = data[:, 0]
    hplus_norm = data[:, 4]
    hcross_norm = data[:, 5]
    hplus = data[:, 6]
plt.loglog(ell, c_ells[0][3], 'g', linewidth=2, label='g1 x src2')
plt.loglog(ell, c_ells[4][4], 'm', linewidth=2, label='CMB lensing auto')
plt.xlabel('$\ell$', fontsize=20)
plt.ylabel('$C_\ell$', fontsize=20)
plt.xlim(1, 1000)
plt.tick_params(labelsize=13)
plt.legend(loc='lower left')
plt.show()


fig = plt.figure()

for i in range(2,4):
  for j in range(2,4):
    if i >= j: 
      ax = plt.subplot2grid((2,2), ((i-2),(j-2)))

      plt.loglog(ell, c_ells[i][j], label=f'src{i-1} x src{j-1}')
      plt.legend(frameon=False)

      if (i, j) == (2,2):
        plt.ylabel(r'$C_\ell$')
      elif (i,j) == (3,3):
        plt.xlabel(r'$\ell$')

plt.subplots_adjust(wspace = 0.4, hspace = 0.4)

plt.show()


Example #33
0
SA_MSS_05 = gsw.SA_from_SP_Baltic(SSbin, lon, lat)
CT_MSS_05 = gsw.CT_from_t(SA_MSS_05, TTbin, Pbin)
SIG0_MSS_05 = gsw.sigma0(SA_MSS_05, CT_MSS_05)
N2_MSS_05 = gsw.Nsquared(SA_MSS_05, CT_MSS_05, Pbin, lat)
N2_05 = N2_MSS_05[0]
zN2_05 = N2_MSS_05[1]
N2_period_05 = 60.0 / swe.cph(N2_05)
## # ------------------------------------#

#### --------- plots ---------- ####
days = dates.DayLocator()
hours = dates.HourLocator(interval=6)
dfmt = dates.DateFormatter('%d')

fig = plt.figure(2)
ax0 = plt.subplot2grid((4, 9), (0, 2), rowspan=1, colspan=6)
ax0.plot(alkor)  # WIND
storm = pd.Timestamp('2010-03-01 12:00:00')
plt.plot([storm, storm], [0, 20], '--k')
ax0.set_xlim(XLIM[0], XLIM[1])
ax0.tick_params(labelbottom='off')
ax0.xaxis.label.set_visible(False)
ax0.xaxis.set_major_locator(days)
ax0.xaxis.set_major_formatter(dfmt)
ax0.xaxis.set_minor_locator(hours)
ax0.set_ylabel(r'$\overline{U} (m s^{-1})$')
ax0.set_ylim(0, 20)
plt.grid()

## df = pd.DataFrame(wind_mag)
## df = df.set_index('date_time')
    avgfluxperob = np.nanmean(flux, axis=1)
    
    ### find chi squared ###
    vary = vari_funcs.vary_stats.my_chisquare_err(flux, fluxerr)
    
    return tbdata, avgfluxperob, vary

tbdata, avgfluxperob, vary = prep_data(tbdata)

### reset X-ray column as messed up by stacking ###
#tbdata['X-ray'][tbdata['X-ray']==70] = False 
#tbdata['X-ray'][tbdata['X-ray']==84] = True

### Create plot with two axes ##
fig = plt.figure(figsize=[9,8])
ax1 = plt.subplot2grid((5,5), (0,0), colspan=4, rowspan=5)# Subplot covering majority of plot
ax4 = plt.subplot2grid((5,5), (0,4), rowspan=5, sharey=ax1)

ax2 = ax1.twiny() # Twin of the first subplot

### set tick values ###
low_ticks = np.array([1e2, 1e3, 1e4, 1e5, 1e6])#, 1e7]) #set flux ticks
#new_ticks = 30 - 2.5*np.log10(ticks) #find equivilant mag ticks
#new_ticks = new_ticks + 1.9

new_ticks =np.array([27, 25, 23, 21, 19, 17, 15])
upp_ticks = new_ticks - 1.9
upp_ticks = 10**((upp_ticks-30)/(-2.5))

### Plot the chi sq v mean ###
#ax1.plot(savgfluxperob, varystar, 'm*', mfc = 'none', markersize = 10,
            axis=1).reshape(X.shape)  #[N]

        # levels = [-1] + list(range(0,n_all_classes))
        # ax = plt.subplot2grid((rows,columns), (0,0), frameon=False)#, colspan=3)
        # cs = ax.contourf(X, Y, predictions_argmax, levels=levels, cmap='jet')
        # ax.annotate('Arg Max', xytext=(.4, 1.05), xy=(.5, .5), textcoords='axes fraction', family='serif', color='Black')
        # plt.gca().set_aspect('equal', adjustable='box')
        # ax.tick_params(labelsize=6)

        # ax = plt.subplot2grid((rows,columns), (0,0), frameon=False)#, colspan=3)
        # cs = ax.contourf(X, Y, predictions_max, cmap='jet')
        # ax.annotate('Max', xytext=(.4, 1.05), xy=(.5, .5), textcoords='axes fraction', family='serif', color='Black')
        # plt.gca().set_aspect('equal', adjustable='box')
        # ax.tick_params(labelsize=6)

        ax = plt.subplot2grid((rows, columns), (0, 0),
                              frameon=False)  #, colspan=3)
        cs = ax.contourf(X, Y, predictions_entropy, cmap='jet')
        ax.annotate('Entropy',
                    xytext=(.4, 1.05),
                    xy=(.5, .5),
                    textcoords='axes fraction',
                    family='serif',
                    color='Black')
        plt.gca().set_aspect('equal', adjustable='box')
        ax.tick_params(labelsize=6)

        for j in range(n_s):
            #convert to encodings
            samples_from_each_encoded = []
            for i in range(5):
                pytorch_input = Variable(
Example #36
0
import matplotlib.dates as mdates
import pandas_datareader.data as web
from matplotlib import style

style.use('ggplot')

start = dt.datetime(2000, 1, 1)
end = dt.datetime(2019, 12, 31)

df = web.DataReader('TSLA', 'yahoo', start, end)
df.to_csv('tsla.csv')
df.head()

df = pd.read_csv('tsla.csv', parse_dates=True, index_col=0)
#df['100ma'] = df['Adj Close'].rolling(window = 100, min_periods = 0).mean()
#df.head()
#df.tail()
df_ohlc = df['Adj Close'].resample('10D').ohlc()
df_volume = df['Volume'].resample('10D').sum()

df_ohlc.reset_index(inplace=True)
df_ohlc['Date'] = df_ohlc['Date'].map(mdates.date2num)

ax1 = plt.subplot2grid((6, 1), (0, 0), rowspan=5, colspan=1)
ax2 = plt.subplot2grid((6, 1), (5, 0), rowspan=1, colspan=1, sharex=ax1)
ax1.xaxis_date()

candlestick_ohlc(ax1, df_ohlc.values, width=2, colorup='g')
ax2.fill_between(df_volume.index.map(mdates.date2num), df_volume.values, 0)

plt.show()
Example #37
0
def plot_wavetransf_time_zoom_tbound(x, wa, T, S, sig95, gs, signif_g, time_base, t_min, t_max, scalemin=0, \
    scalemax=6, ylabel='Pressure (mb)', plot_percentile=False):
    """plotting contours w/global and timeseries with specified time limits"""

    fig = plt.figure(22)
    ax = plt.subplot2grid((3, 4), (1, 0), colspan=3, rowspan=2)
    # use following with unnormalized data to match web output
    if plot_percentile:
        #use following to contour at "percentiles variances" when using non-normalized data to match web output
        csf = plt.contourf(T,
                           S,
                           wa.wavelet_power,
                           levels=[
                               0,
                               stats.scoreatpercentile(wa.wavelet_power, 25),
                               stats.scoreatpercentile(wa.wavelet_power, 50),
                               stats.scoreatpercentile(wa.wavelet_power, 75),
                               stats.scoreatpercentile(wa.wavelet_power, 95),
                               stats.scoreatpercentile(wa.wavelet_power, 100)
                           ],
                           colors=bmap)
    else:
        levels = [0, 1, 5, 100, 250, 500, 750]
        csf = plt.contourf(T, S, wa.wavelet_power, levels=levels, colors=bmap)
    levels = [-99, 1]  # values greater than 1 are significant
    plt.contour(T, S, sig95, levels, colors='black', linewidth=5)
    ax.set_yscale('log')
    ax.set_ylabel('Contoured Wavelets')
    ax.grid(True)

    # put the ticks at powers of 2 in the scale
    ticks = np.unique(2**np.floor(np.log2(wa.scales)))[1:]
    ax.yaxis.set_ticks(ticks)
    ax.yaxis.set_ticklabels(ticks.astype(str))
    ax.set_ylim(scalemax, scalemin)

    ax.xaxis.set_minor_locator(MonthLocator(bymonthday=(1, 15)))
    ax.xaxis.set_major_locator(MonthLocator(bymonthday=1, interval=1))
    ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
    #fig.autofmt_xdate()

    # shade the region between the edge and coi
    C, S = wa.coi
    C = C / 24.
    ax.fill_between(x=C, y1=S, y2=wa.scales.max(), color='gray', alpha=0.5)
    ax.set_xlim(t_min, t_max)

    ax = plt.subplot2grid((3, 4), (1, 3), colspan=1, rowspan=2)
    p1 = ax.plot(gs, wa.scales, signif_g, wa.scales, 'k--')
    ax.set_yscale('log')
    ax.set_xscale('log')
    ax.grid(True)

    # put the ticks at powers of 2 in the scale
    ticks = np.unique(2**np.floor(np.log2(wa.scales)))[1:]
    ax.yaxis.set_ticks(ticks)
    ax.set_yticklabels([])
    #ax.yaxis.set_ticklabels(ticks.astype(str))
    ax.set_ylim(scalemax, scalemin)

    # second y scale with equivalent fourier periods to scales
    # except with the ticks at the powers of 2
    ax_fourier = ax.twinx()
    ax_fourier.set_yscale('log')
    # match the fourier ticks to the scale ticks
    ax_fourier.set_yticks(ticks)
    ax_fourier.set_yticklabels(ticks.astype(str))
    ax_fourier.set_ylabel('fourier period (%s)' % time_base)
    fourier_lim = [wa.fourier_period(i) for i in ax.get_ylim()]
    ax_fourier.set_ylim(fourier_lim)

    #plt.show()
    DefaultSize = fig.get_size_inches()
    fig.set_size_inches((DefaultSize[0] * 2, DefaultSize[1]))

    return (plt, fig)
Example #38
0
for x in range(norm_2iso_lin.shape[0]):
    for y in range(norm_2iso_lin.shape[1]):
        norm_2iso_lin[(x, y)] = int((norm_2iso_lin[(x, y)]))

for x in range(norm_2niso_lin.shape[0]):
    for y in range(norm_2niso_lin.shape[1]):
        norm_2niso_lin[(x, y)] = int(norm_2niso_lin[(x, y)])

f_s = 16
#################### SIZES ####################
fig = plt.figure(figsize=(4.75, 8))
###############################################

#################### PANEL (a) ####################
ax1 = plt.subplot2grid((2, 1), (0, 0), colspan=1, rowspan=1)

new_xrange = np.linspace(LX // 2, int(xrange[1]), 5)
new_yrange = np.linspace(LX // 2, int(xrange[1]), 5)

ax1.set_xticks(new_xrange)
ax1.set_xticklabels([int(elem) for elem in new_xrange - LX // 2])
ax1.set_yticks(new_yrange)
ax1.set_yticklabels([int(elem) for elem in new_yrange - LX // 2])

ax1.set_xlim(xrange)
ax1.set_ylim(xrange)
ax1.add_artist(circle_2iso)
im1 = ax1.imshow(norm_2iso_lin,
                 cmap='rainbow',
                 vmin=0.,
    def plot_compare_scalCls(self):
        """
        Plots and saves the comparison of all the scalar Cls in a unique image
        """

        # number of Cls in the two files:
        num1     = self.scalCls_1.shape[1]-1
        num2     = self.scalCls_2.shape[1]-1
        # protection against different runs
        if num1!=num2:
            print 'wrong number of Cls'
            return

        # add the matter power spectrum if required:
        if self.transfer: num1 += 1
        # values of l:
        xvalues_1 = self.scalCls_1[:,0]
        xvalues_2 = self.scalCls_2[:,0]
        # get minimuma and maximum l values:
        l_min = np.amax( [np.amin(xvalues_1), np.amin(xvalues_2)] )
        l_max = np.amin( [np.amax(xvalues_1), np.amax(xvalues_2)] )
        # get the l values:
        l_values = np.linspace(l_min, l_max, l_max-l_min)

        # set up the plots:
        plots_1       = CMB_plots()
        plots_2       = CMB_plots()
        plots_compa   = CMB_plots()

        plots_1.color          = self.color1
        plots_2.color          = self.color2
        plots_compa.color      = self.color_compa
        plots_compa.comparison = True
        plots_compa.axes_label_position = 'right'
        fig = plt.gcf()

        # do the plots:
        for ind in xrange(1,num1+1):

            # distribute the plots in the figure:
            temp      = plt.subplot2grid((num1,2), (ind-1, 0))
            temp_comp = plt.subplot2grid((num1,2), (ind-1, 1))

            # do the l sampling and comparison:
            if not ( ind == num1 and self.transfer ):
                # get the y values:
                yvalues_1    = self.scalCls_1[:,ind]
                yvalues_2    = self.scalCls_2[:,ind]
                # interpolate the two spectra:
                spline_1 = UnivariateSpline( xvalues_1, yvalues_1, s=0)
                spline_2 = UnivariateSpline( xvalues_2, yvalues_2, s=0)
                # evaluate on the l grid:
                yvalues_1 = spline_1(l_values)
                yvalues_2 = spline_2(l_values)
                # protect against zeroes:
                yvalues_1_temp = np.abs( yvalues_1 )
                yvalues_2_temp = np.abs( yvalues_2 )
                try:
                    min2val_1      = np.amin(yvalues_1_temp[np.nonzero(yvalues_1_temp)])
                    min2val_2      = np.amin(yvalues_2_temp[np.nonzero(yvalues_2_temp)])
                except:
                    min2val_1      = cutoff
                    min2val_2      = cutoff
                np.place(yvalues_1, yvalues_1 == 0.0, min2val_1)
                np.place(yvalues_2, yvalues_2 == 0.0, min2val_2)
                # computation of the percentual comparison:
                yvalues_comp = (yvalues_1 - yvalues_2)/abs(yvalues_1)*100
                # protection against values too small:
                np.place(yvalues_comp, abs(yvalues_comp)<cutoff, [cutoff])
            else: # matter power spectrum case
                # get the k values:
                xvalues_1      = self.matterpower_1[:,0]
                xvalues_2      = self.matterpower_2[:,0]
                # get the y values:
                yvalues_1    = self.matterpower_1[:,1]
                yvalues_2    = self.matterpower_2[:,1]
                # get the k grid:
                k_min = np.amax( [np.amin(xvalues_1), np.amin(xvalues_2)] )
                k_max = np.amin( [np.amax(xvalues_1), np.amax(xvalues_2)] )
                k_values = np.logspace(np.log10(k_min), np.log10(k_max), 1000)
                # interpolate the two spectra:
                spline_1 = UnivariateSpline( xvalues_1, yvalues_1, s=0)
                spline_2 = UnivariateSpline( xvalues_2, yvalues_2, s=0)
                # evaluate on the l grid:
                yvalues_1 = spline_1(k_values)
                yvalues_2 = spline_2(k_values)
                # protect against zeroes:
                yvalues_1_temp = np.abs( yvalues_1 )
                yvalues_2_temp = np.abs( yvalues_2 )
                try:
                    min2val_1      = np.amin(yvalues_1_temp[np.nonzero(yvalues_1_temp)])
                    min2val_2      = np.amin(yvalues_2_temp[np.nonzero(yvalues_2_temp)])
                except:
                    min2val_1      = cutoff
                    min2val_2      = cutoff
                np.place(yvalues_1, yvalues_1 == 0.0, min2val_1)
                np.place(yvalues_2, yvalues_2 == 0.0, min2val_2)
                # computation of the percentual comparison:
                yvalues_comp = (yvalues_1 - yvalues_2)/abs(yvalues_1)*100
                # protection against values too small:
                np.place(yvalues_comp, abs(yvalues_comp)<cutoff, [cutoff])

            # make the plots:
            if ind == 1: # TT power spectrum:

                plots_1.TT_plot(temp, l_values, yvalues_1)
                plots_2.TT_plot(temp, l_values, yvalues_2)
                plots_compa.TT_plot(temp_comp, l_values, yvalues_comp)
                temp_comp.set_yscale('Log')
                temp.set_title('TT power spectrum')

            elif ind == 2: # EE power spectrum:

                plots_1.EE_plot(temp, l_values, yvalues_1)
                plots_2.EE_plot(temp, l_values, yvalues_2)
                plots_compa.EE_plot(temp_comp, l_values, yvalues_comp)
                temp.set_title('EE power spectrum')

            elif ind == 3: # TE power spectrum:

                plots_1.TE_plot(temp, l_values, yvalues_1)
                plots_2.TE_plot(temp, l_values, yvalues_2)
                plots_compa.TE_plot(temp_comp, l_values, yvalues_comp)
                temp.set_title('TE power spectrum')

            elif ind == 4 and self.lensing: # CMB lensing power spectrum:

                plots_1.Phi_plot(temp, l_values, yvalues_1)
                plots_2.Phi_plot(temp, l_values, yvalues_2)
                plots_compa.Phi_plot(temp_comp, l_values, yvalues_comp)
                temp.set_title('$\phi$ power spectrum')

            elif ind == 5 and self.lensing: # CMB lensing - Temperature power spectrum:

                plots_1.PhiT_plot(temp, l_values, yvalues_1)
                plots_2.PhiT_plot(temp, l_values, yvalues_2)
                plots_compa.PhiT_plot(temp_comp, l_values, yvalues_comp)
                temp.set_title('$\phi$T power spectrum')

            elif ind == num1 and self.transfer: # matter power spectrum:

                plots_1.Matter_plot(temp, k_values, yvalues_1)
                plots_2.Matter_plot(temp, k_values, yvalues_2)
                plots_compa.Matter_plot(temp_comp, k_values, yvalues_comp)
                temp.set_title('Matter power spectrum')

            else: # generic Cl comparison:

                plots_1.Generic_Cl(temp, l_values, yvalues_1)
                plots_2.Generic_Cl(temp, l_values, yvalues_2)
                plots_compa.Generic_Cl(temp_comp, l_values, yvalues_comp)

        # set the size of the image
        fig.set_size_inches( self.x_size_inc, self.y_size_inc)
        # set a tight layout
        fig.tight_layout(pad=0.3, h_pad=0.3, w_pad=0.3)
        # set the global title
        plt.suptitle(self.name_h1+' VS '+self.name_h2+
                     ' comparison of scalar Cls', fontsize=16)
        # set the global legend
        fig.legend( handles = [plots_1.TT_p, plots_2.TT_p, plots_compa.CV_p],
                    labels  = [self.name_h1, self.name_h2, 'Cosmic variance'],
                    loc='lower center', ncol=3 ,fancybox=True)
        # adjust the size of the plot
        fig.subplots_adjust(top=0.92, bottom=0.08)
        # save the result and close
        plt.savefig(self.outpath+self.name1+'_'+self.name2+'_scalCls_comp.pdf')
        plt.clf()
        plt.close("all")
Example #40
0
def main():
    Nbins = 72
    config = ConfigLoader("config.yml")
    config.set_params("final_params.json")
    error_matrix = np.load("error_matrix.npy")

    idx = config.get_data_index("mass", "R_BC")

    datas = config.get_data("data")
    phsps = config.get_data("phsp")
    bgs = config.get_data("bg")
    amp = config.get_amplitude()
    var = amp.trainable_variables

    get_data = lambda x: data_index(x, idx).numpy()

    m_all = np.concatenate([get_data(i) for i in datas])

    m_min = np.min(m_all) - 0.1
    m_max = np.max(m_all) + 0.1
    binning = np.linspace(m_min, m_max, Nbins + 1)

    get_hist = lambda x, w: Hist1D.histogram(
        get_data(x), bins=Nbins, range=(m_min, m_max), weights=w
    )

    data_hist = [get_hist(i, i.get("weight")) for i in datas]
    bg_hist = [get_hist(i, np.abs(i.get("weight"))) for i in bgs]

    phsp_hist = []
    for dh, bh, phsp in zip(data_hist, bg_hist, phsps):
        m_phsp = data_index(phsp, idx).numpy()

        y_frac, grads, w_error2 = binning_gradient(
            binning, amp, phsp, m_phsp, var
        )
        error2 = np.einsum("ij,jk,ik->i", grads, error_matrix, grads)
        # error parameters and error from integration sample weights
        yerr = np.sqrt(error2 + w_error2)

        n_fit = dh.get_count() - bh.get_count()
        phsp_hist.append(n_fit * Hist1D(binning, y_frac, yerr))

    total_data = reduce(operator.add, data_hist)
    ax = plt.subplot2grid((4, 1), (0, 0), rowspan=3)
    total_data.draw(ax, label="data")
    total_data.draw_error(ax)
    total_bg = reduce(operator.add, bg_hist)
    total_bg.draw_bar(ax, label="back ground", color="grey", alpha=0.5)
    total_fit = reduce(operator.add, phsp_hist + bg_hist)
    total_fit.draw(ax, label="fit")
    total_fit.draw_error(ax)
    ax.set_ylim((0, None))
    ax.set_xlim((m_min, m_max))
    ax.legend()
    ax.set_ylabel(f"Events/ {(m_max-m_min)/Nbins:.3f} GeV")
    ax2 = plt.subplot2grid((4, 1), (3, 0), rowspan=1)
    (total_data - total_fit).draw_pull(ax2)
    plt.setp(ax.get_xticklabels(), visible=False)
    ax2.set_ylim((-5, 5))
    ax2.set_xlim((m_min, m_max))
    ax2.axhline(0, c="black", ls="-")
    ax2.axhline(-3, c="r", ls="--")
    ax2.axhline(3, c="r", ls="--")
    ax2.set_xlabel("$M(BC)$/GeV", loc="right")
    plt.savefig("fit_full_error.png")
    def plot_compare_totalCls(self):
        """
        Plots and saves the comparison of all the total (scalar + tensor) Cls in a unique image
        If lensing is included lensed Cls are used.
        """

        # protection from direct calls if tensors are not included
        if not self.tensor: return

        # decide what data to use:
        if self.lensing:
            data1 = self.lensedtotCls_1
            data2 = self.lensedtotCls_2
        else:
            data1 = self.totCls_1
            data2 = self.totCls_2

        # number of Cls:
        num1     = data1.shape[1]-1
        num2     = data2.shape[1]-1
        # protection against different runs
        if num1!=num2:
            print 'wrong number of Cls'
            return

        # get the x values:
        xvalues_1 = data1[:,0]
        xvalues_2 = data2[:,0]
        # get the l values:
        l_min = np.amax( [np.amin(xvalues_1), np.amin(xvalues_2)] )
        l_max = np.amin( [np.amax(xvalues_1), np.amax(xvalues_2)] )
        # get the l values:
        l_values = np.linspace(l_min, l_max, l_max-l_min)

        # set up the plots:
        plots_1       = CMB_plots()
        plots_2       = CMB_plots()
        plots_compa   = CMB_plots()

        plots_1.color          = self.color1
        plots_2.color          = self.color2
        plots_compa.color      = self.color_compa
        plots_compa.comparison = True
        plots_compa.axes_label_position = 'right'
        fig = plt.gcf()

        # do the plots:
        for ind in xrange(1,num1+1):

            temp      = plt.subplot2grid((num1,2), (ind-1, 0))
            temp_comp = plt.subplot2grid((num1,2), (ind-1, 1))

            # get the y values:
            yvalues_1    = data1[:,ind]
            yvalues_2    = data2[:,ind]
            # interpolate the two spectra:
            spline_1 = UnivariateSpline( xvalues_1, yvalues_1, s=0)
            spline_2 = UnivariateSpline( xvalues_2, yvalues_2, s=0)
            # evaluate on the l grid:
            yvalues_1 = spline_1(l_values)
            yvalues_2 = spline_2(l_values)
            # protect against zeroes:
            yvalues_1_temp = np.abs( yvalues_1 )
            yvalues_2_temp = np.abs( yvalues_2 )
            try:
                min2val_1      = np.amin(yvalues_1_temp[np.nonzero(yvalues_1_temp)])
                min2val_2      = np.amin(yvalues_2_temp[np.nonzero(yvalues_2_temp)])
            except:
                min2val_1      = cutoff
                min2val_2      = cutoff
            np.place(yvalues_1, yvalues_1 == 0.0, min2val_1)
            np.place(yvalues_2, yvalues_2 == 0.0, min2val_2)
            # computation of the percentual comparison:
            yvalues_comp = (yvalues_1 - yvalues_2)/abs(yvalues_1)*100
            # protection against values too small:
            np.place(yvalues_comp, abs(yvalues_comp)<cutoff, [cutoff])

            if ind == 1:

                plots_1.TT_plot(temp, l_values, yvalues_1)
                plots_2.TT_plot(temp, l_values, yvalues_2)
                plots_compa.TT_plot(temp_comp, l_values, yvalues_comp)
                temp.set_yscale('Log')
                temp.set_title('TT power spectrum')

            elif ind == 2:

                plots_1.EE_plot(temp, l_values, yvalues_1)
                plots_2.EE_plot(temp, l_values, yvalues_2)
                plots_compa.EE_plot(temp_comp, l_values, yvalues_comp)
                temp.set_title('EE power spectrum')

            elif ind == 3:

                plots_1.BB_plot(temp, l_values, yvalues_1)
                plots_2.BB_plot(temp, l_values, yvalues_2)
                plots_compa.BB_plot(temp_comp, l_values, yvalues_comp)
                temp.set_title('BB power spectrum')

            elif ind == 4:

                plots_1.TE_plot(temp, l_values, yvalues_1)
                plots_2.TE_plot(temp, l_values, yvalues_2)
                plots_compa.TE_plot(temp_comp, l_values, yvalues_comp)
                temp.set_title('TE power spectrum')

            else:

                plots_1.TT_plot(temp, l_values, yvalues_1)
                plots_2.TT_plot(temp, l_values, yvalues_2)
                plots_compa.TT_plot(temp_comp, l_values, yvalues_comp)


        # set the size of the image
        fig.set_size_inches( self.x_size_inc, self.y_size_inc)
        # set a tight layout
        fig.tight_layout(pad=0.3, h_pad=0.3, w_pad=0.3)
        # set the global title
        if self.lensing:
            plt.suptitle(self.name_h1+' VS '+self.name_h2+' comparison of total lensed Cls', fontsize=16)
        else:
            plt.suptitle(self.name_h1+' VS '+self.name_h2+' comparison of total Cls', fontsize=16)
        # set the global legend
        fig.legend( handles = [plots_1.TT_p, plots_2.TT_p, plots_compa.CV_p],
                    labels  = [self.name_h1, self.name_h2, 'Cosmic variance'],
                    loc='lower center', ncol=3 ,fancybox=True)
        # adjust the size of the plot
        fig.subplots_adjust(top=0.92, bottom=0.08)
        # save the result and close
        plt.savefig(self.outpath+self.name1+'_'+self.name2+'_totCls_comp.pdf')
        plt.clf()
        plt.close("all")
Example #42
0
def plot_comparison(data,
                    ticksize=16,
                    fontsize=20,
                    tickfont="Crimson Text",
                    fontname="Crimson Text"):
    # adding legends
    d_acc, d_gyr, d_grav = group_perturbation_data(data)

    fig = plt.figure(figsize=(16, 9), dpi=90)
    funcs = ["mse_losses_x", "mse_losses_y", "mse_losses_z", "avg_mse_losses"]
    for i, func in enumerate(funcs):
        plt.subplot2grid([2, len(funcs)], [0, i], fig=fig)
        plot_var_boxplot(
            data,
            func,
            x="legend",
            hue="model",
            order=None,
            hue_order=["noptrb", "bias", "bias-grav"],
        )
        plt.legend([])
    funcs = [
        "likelihood_losses_x",
        "likelihood_losses_y",
        "likelihood_losses_z",
        "avg_likelihood_losses",
    ]
    for i, func in enumerate(funcs):
        plt.subplot2grid([2, len(funcs)], [1, i], fig=fig)
        plot_var_boxplot(
            data,
            func,
            x="legend",
            hue="model",
            order=None,
            hue_order=["noptrb", "bias", "bias-grav"],
        )

    fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(9, 3), dpi=90)
    for i in range(3):
        plt.sca(axs[i])
        if i == 2:
            plot_var_boxplot(
                d_grav,
                "avg_mse_losses",
                x="legend",
                hue="model",
                order=["0.0", "2.0", "4.0", "6.0", "8.0", "10.0"],
                hue_order=["noptrb", "bias", "bias-grav"],
            )
        elif i == 0:
            plot_var_boxplot(
                d_acc,
                "avg_mse_losses",
                x="legend",
                hue="model",
                order=None,
                hue_order=["noptrb", "bias", "bias-grav"],
            )
        elif i == 1:
            plot_var_boxplot(
                d_gyr,
                "avg_mse_losses",
                x="legend",
                hue="model",
                order=None,
                hue_order=["noptrb", "bias", "bias-grav"],
            )
        plt.setp(axs[i].get_xticklabels(),
                 fontsize=ticksize,
                 fontname=tickfont)
        plt.setp(axs[i].get_yticklabels(),
                 fontsize=ticksize,
                 fontname=tickfont)
    axs[0].set_xlabel("Accel Bias (m/s^2)",
                      fontsize=fontsize,
                      fontname=fontname)
    axs[0].set_ylabel("Avg MSE Loss", fontsize=fontsize, fontname=fontname)
    axs[1].set_xlabel("Gyro Bias (rad/s)",
                      fontsize=fontsize,
                      fontname=fontname)
    axs[1].set_ylabel(None)
    axs[2].set_xlabel("Gravity Direction (deg)",
                      fontsize=fontsize,
                      fontname=fontname)
    axs[2].set_ylabel(None)
    axs[0].legend([])
    axs[1].legend([])
    leg = axs[2].legend(loc="upper left")
    plt.setp(leg.texts, family=fontname, fontsize=fontsize)
    plt.tight_layout(pad=0.2)
    plt.show()
    c3a = 4*Oa**2/G**2 
    c3b = 4*Ob**2/G**2 
    c3c = 4*Oc**2/G**2 
    c3d = 4*Od**2/G**2 
    c3e = 4*Oe**2/G**2 
    c3f = 4*Of**2/G**2 
    c3g = 4*Og**2/G**2 
    rhoaa =  -(IoIs/2)*(1/(c1a-c2a*v+c3a*v**2)+1/(c1b-c2b*v+c3b*v**2)+1/(c1c-c2c*v+c3c*v**2)+1/(c1d-c2d*v+c3d*v**2)+1/(c1e-c2e*v+c3e*v**2)+1/(c1f-c2f*v+c3f*v**2)+1/(c1g-c2g*v+c3g*v**2))   
    return rhoaa*hbar*k*G/M
print(dv(3,5,200))
def dz(t,z,v):
    return v  
    
plt.close('all')
fig = plt.figure()
ax1 = plt.subplot2grid((2,1), (0,0))#, rowspan=2)
ax2 = plt.subplot2grid((2,1), (1,0), sharex=ax1)


"""creation of our array of velocities"""
#vlin=Jgen(T,Natoms,M)
vlin=vgen(Natoms)
zlin=zgen(Natoms)
#y_vlin=y_vgen(Natoms)
y_vlin=y_vGauss(Natoms)
ylin=ygen(Natoms)
zs,vs,ts=[],[],[]
ys,yvs=[],[]
"""this loop goes through all the atoms we've got and applies the force dv to them for a number of steps, Nsteps"""
for j in range(Natoms):
    vi = vlin[j] 
    def plot_compare_Transfer(self):
        """
        Plots and saves the comparison of all the transfer functions in a unique image
        """

        # protection from direct calls if transfer functions are not included
        if not self.transfer: return

        data1 = self.transfer_func_1
        data2 = self.transfer_func_2

        # number of transfer functions:
        num1     = data1.shape[1]-1
        num2     = data2.shape[1]-1
        # protection against different runs
        if num1!=num2:
            print 'wrong number of transfer functions'
            return

        # get the x values:
        xvalues_1 = data1[:,0]
        xvalues_2 = data2[:,0]
        # get the k grid:
        k_min = np.amax( [np.amin(xvalues_1), np.amin(xvalues_2)] )
        k_max = np.amin( [np.amax(xvalues_1), np.amax(xvalues_2)] )
        k_values = np.logspace(np.log10(k_min), np.log10(k_max), 1000)

        # set up the plots:
        plots_1       = CMB_plots()
        plots_2       = CMB_plots()
        plots_compa   = CMB_plots()

        plots_1.color          = self.color1
        plots_2.color          = self.color2
        plots_compa.color      = self.color_compa
        plots_compa.comparison = True
        plots_compa.axes_label_position = 'right'
        fig = plt.gcf()

        labels = [ 'CDM', 'baryons', 'photons', 'massless neutrinos', 'massive neutrinos',
                   'CDM+baryons+massive neutrinos', 'CDM+baryons', 'CDM+baryons+massive neutrinos+ de',
                   'The Weyl potential', 'vel_Newt_cdm', 'vel_Newt_b', 'relative baryon-CDM velocity',
                  ]

        if not len(labels) == num1:
            print 'Not enough transfer functions for labels'
            print 'Labels are:'
            print labels
            return

        # do the plots:
        for ind in xrange(1,num1+1):

            temp      = plt.subplot2grid((num1,2), (ind-1, 0))
            temp_comp = plt.subplot2grid((num1,2), (ind-1, 1))

            # get the y values:
            yvalues_1    = data1[:,ind]
            yvalues_2    = data2[:,ind]
            # interpolate the two spectra:
            spline_1 = UnivariateSpline( xvalues_1, yvalues_1, s=0)
            spline_2 = UnivariateSpline( xvalues_2, yvalues_2, s=0)
            # evaluate on the l grid:
            yvalues_1 = spline_1(k_values)
            yvalues_2 = spline_2(k_values)
            # protect against zeroes:
            yvalues_1_temp = np.abs( yvalues_1 )
            yvalues_2_temp = np.abs( yvalues_2 )
            try:
                min2val_1      = np.amin(yvalues_1_temp[np.nonzero(yvalues_1_temp)])
                min2val_2      = np.amin(yvalues_2_temp[np.nonzero(yvalues_2_temp)])
            except:
                min2val_1      = cutoff
                min2val_2      = cutoff
            np.place(yvalues_1, yvalues_1 == 0.0, min2val_1)
            np.place(yvalues_2, yvalues_2 == 0.0, min2val_2)
            # computation of the percentual comparison:
            yvalues_comp = (yvalues_1 - yvalues_2)/abs(yvalues_1)*100
            # protection against values too small:
            np.place(yvalues_comp, abs(yvalues_comp)<cutoff, [cutoff])

            if not ( np.all( np.abs(yvalues_1) <= cutoff) ):
                plots_1.Transfer_plot(temp, k_values, yvalues_1)
            if not ( np.all( np.abs(yvalues_2) <= cutoff) ):
                plots_2.Transfer_plot(temp, k_values, yvalues_2)
            if not ( np.all( np.abs(yvalues_comp) <= cutoff) ):
                plots_compa.Transfer_plot(temp_comp, k_values, yvalues_comp)

            temp.set_title(labels[ind-1])

        # set the size of the image
        fig.set_size_inches( self.x_size_inc, 1.61803398875*self.x_size_inc/6.*num1 )
        # set a tight layout
        fig.tight_layout(pad=0.3, h_pad=0.3, w_pad=0.3)
        # set the global title
        plt.suptitle(self.name_h1+' VS '+self.name_h2+' comparison of transfer functions', fontsize=16)
        # set the global legend
        fig.legend( handles = [plots_1.Transfer_p, plots_2.Transfer_p],
                    labels  = [self.name_h1, self.name_h2],
                    loc='lower center', ncol=3 ,fancybox=True)
        # adjust the size of the plot
        fig.subplots_adjust(top=0.95, bottom=0.05)
        # save the result and close
        plt.savefig(self.outpath+self.name1+'_'+self.name2+'_transfer_comp.pdf')
        plt.clf()
        plt.close("all")
Example #45
0
def graph_data(stock_file):
    stock_reader = StockFileReader(stock_file)
    stock_reader.open()

    date_list = []
    dates = []
    openp = []
    highp = []
    lowp = []
    closep = []
    volume = []
    sma1 = []
    sma2 = []

    sma1_indicator = SmaIndicator(20)
    sma2_indicator = SmaIndicator(200, 200)
    macd_indicator = MacdIndicator(26, 12, 9)
    rsi_indicator = RsiIndicator(14)

    rsi = []
    macd_line = []
    macd_histogram = []
    macd_signal_line = []

    dateconverter = mdates.strpdate2num("%Y%m%d")

    with stock_reader.open():
        for row in stock_reader.read_all():
            dates.append(dateconverter(row.timestamp.to_string("%Y%m%d")))
            date_list.append(row.timestamp.to_datetime())
            openp.append(row.open)
            highp.append(row.high)
            lowp.append(row.low)
            closep.append(row.adjusted_close)
            volume.append(row.volume)
            sma1_value = sma1_indicator.calculate(row.timestamp, row.close)
            if sma1_value is not None:
                sma1.append(sma1_value)

            sma2_value = sma2_indicator.calculate(row.timestamp, row.close)
            if sma2_value is not None:
                sma2.append(sma2_value)

            rsi_value = rsi_indicator.calculate(row.timestamp, row.close)
            if rsi_value is not None:
                rsi.append(rsi_value)

            macd_value = macd_indicator.calculate(row.timestamp, row.close)
            if macd_value is not None:
                macd_line.append(macd_value)
                macd_signal_line.append(macd_indicator.current.signal_line)
                macd_histogram.append(macd_indicator.current.histogram)

    stock_reader.close()

    x = 0
    y = len(dates)
    candles = []
    while x < y:
        append_line = dates[x], openp[x], highp[x], lowp[x], closep[x], volume[
            x]
        candles.append((append_line))
        x += 1

    fig = plt.figure(facecolor='#07000d')

    ax0 = plt.subplot2grid((6, 4), (0, 0),
                           rowspan=1,
                           colspan=4,
                           facecolor='#07000d')

    ax0.plot(dates[rsi_indicator.period - 1:], rsi)
    ax0.spines['top'].set_color('#5998ff')
    ax0.spines['bottom'].set_color('#5998ff')
    ax0.spines['left'].set_color('#5998ff')
    ax0.spines['right'].set_color('#5998ff')
    ax0.spines['top'].set_color('#5998ff')
    ax0.tick_params(axis='x', colors='w')
    ax0.tick_params(axis='y', colors='w')
    plt.ylabel('RSI', color='w')

    xLength = range(
        len(dates)
    )  # length of the x-axis used for plotting coordinates (xLength, y)
    candleAr = list(zip(xLength, openp, highp, lowp, closep,
                        volume))  # The data set

    # Formatter Class to eliminate weekend data gaps on chart
    class Jackarow(mdates.DateFormatter):
        def __init__(self, fmt):
            mdates.DateFormatter.__init__(self, fmt)

        def __call__(self, x, pos=0):
            # This gets called even when out of bounds, so IndexError must be prevented.
            if x < 0:
                x = 0
            elif x >= len(dates):
                x = -1
            return mdates.DateFormatter.__call__(self, dates[int(x)], pos)

    ax1 = plt.subplot2grid((6, 4), (1, 0),
                           rowspan=4,
                           colspan=4,
                           facecolor='#07000d')

    print(candles)
    print(candleAr)
    candlestick_ohlc(ax1,
                     candleAr,
                     width=0.75,
                     colorup='#9eff15',
                     colordown='#ff1717',
                     alpha=0.75)

    ax1.plot(xLength[sma1_indicator.period - 1:],
             sma1,
             label=sma1_indicator.label)
    ax1.plot(xLength[sma2_indicator.period - 1:],
             sma2,
             label=sma2_indicator.label)

    #ax1.grid(True, color='w')
    ax1.xaxis.set_major_locator(mticker.MaxNLocator(30))
    ax1.xaxis.set_major_formatter(Jackarow('%Y%m%d'))
    #ax1.yaxis.label.set_color('w')
    ax1.spines['top'].set_color('#5998ff')
    ax1.spines['bottom'].set_color('#5998ff')
    ax1.spines['left'].set_color('#5998ff')
    ax1.spines['right'].set_color('#5998ff')
    ax1.spines['top'].set_color('#5998ff')
    ax1.tick_params(axis='y', colors='w')
    ax1.tick_params(axis='x', colors='w')

    plt.ylabel('Stock price', color='w')
    plt.legend(loc=3, prop={'size': 7}, fancybox=True, borderaxespad=0)
    '''
    ax2 = plt.subplot2grid((5, 4), (4, 0), sharex=ax1, rowspan=1, colspan=4, facecolor='#07000d')
    ax2.plot(dates, volume, '#00ffe8', linewidth=.8)
    ax2.fill_between(dates, 0, volume, facecolor='#00ffe8', alpha=.5)
    ax2.axes.yaxis.set_ticklabels([])
    ax2.spines['top'].set_color('#5998ff')
    ax2.spines['bottom'].set_color('#5998ff')
    ax2.spines['left'].set_color('#5998ff')
    ax2.spines['right'].set_color('#5998ff')
    ax2.spines['top'].set_color('#5998ff')
    ax2.tick_params(axis='x', colors='w')
    ax2.tick_params(axis='y', colors='w')
    for label in ax2.xaxis.get_ticklabels():
        label.set_rotation(45)
    plt.ylabel('Volume', color='w')
    '''

    ax1v = ax1.twinx()
    ax1v.fill_between(xLength, 0, volume, facecolor='#00ffe8', alpha=.5)
    ax1v.axes.yaxis.set_ticklabels([])
    ax1v.spines['top'].set_color('#5998ff')
    ax1v.spines['bottom'].set_color('#5998ff')
    ax1v.spines['left'].set_color('#5998ff')
    ax1v.spines['right'].set_color('#5998ff')
    ax1v.spines['top'].set_color('#5998ff')
    ax1v.set_ylim(0, 2 * max(volume))
    ax1v.tick_params(axis='x', colors='w')
    ax1v.tick_params(axis='y', colors='w')

    ax2 = plt.subplot2grid((6, 4), (5, 0),
                           sharex=ax1,
                           rowspan=1,
                           colspan=4,
                           facecolor='#07000d')

    ax2.plot(
        xLength[macd_indicator.slow_moving_period +
                macd_indicator.signal_line_period - 2:], macd_line)
    ax2.plot(
        xLength[macd_indicator.slow_moving_period +
                macd_indicator.signal_line_period - 2:], macd_signal_line)

    fillcolor = '#00ffe8'

    ax2.fill_between(xLength[macd_indicator.slow_moving_period +
                             macd_indicator.signal_line_period - 2:],
                     macd_histogram,
                     facecolor=fillcolor,
                     edgecolor=fillcolor,
                     alpha=.5)

    ax2.spines['top'].set_color('#5998ff')
    ax2.spines['bottom'].set_color('#5998ff')
    ax2.spines['left'].set_color('#5998ff')
    ax2.spines['right'].set_color('#5998ff')
    ax2.spines['top'].set_color('#5998ff')
    ax2.tick_params(axis='x', colors='w')
    ax2.tick_params(axis='y', colors='w')
    plt.ylabel('MACD', color='w')
    plt.gca().yaxis.set_major_locator(mticker.MaxNLocator(prune='upper'))

    for label in ax2.xaxis.get_ticklabels():
        label.set_rotation(45)

    plt.subplots_adjust(left=.10, bottom=.20, top=.90, wspace=.20, hspace=0)

    plt.xlabel('Date', color='w')

    plt.suptitle(stock_file + 'Stock Price', color='w')

    plt.setp(ax0.get_xticklabels(), visible=False)
    plt.setp(ax1.get_xticklabels(), visible=False)

    plt.show()
    def plot_compare_scalCovCls(self):
        """
        Plots and saves the comparison of all the scalar Cov Cls in a unique image
        """

        # number of Cls:
        num1     = self.scalCovCls_1.shape[1]-1
        num2     = self.scalCovCls_2.shape[1]-1
        # protection against different runs
        if num1!=num2:
            print 'wrong number of Cls'
            return

        # size of the Cl Cov matrix:
        num1     = int(math.sqrt(num1))
        num_tot  = sum(xrange(1,num1+1))

        # set up the plots:
        plots_1       = CMB_plots()
        plots_2       = CMB_plots()
        plots_compa   = CMB_plots()

        plots_1.color          = self.color1
        plots_2.color          = self.color2
        plots_compa.color      = self.color_compa
        plots_compa.comparison = True
        plots_compa.axes_label_position = 'right'
        fig = plt.gcf()

        # setup a dictionary with the names of the Cls
        dict = { 1: 'T', 2: 'E', 3: '$\phi$'}
        for i in xrange(4, num1+1):
            dict[i] = 'W'+str(i)

        # values of the multipoles:
        xvalues_1 = self.scalCovCls_1[:,0]
        xvalues_2 = self.scalCovCls_2[:,0]
        # get the l values:
        l_min = np.amax( [np.amin(xvalues_1), np.amin(xvalues_2)] )
        l_max = np.amin( [np.amax(xvalues_1), np.amax(xvalues_2)] )
        # get the l values:
        l_values = np.linspace(l_min, l_max, l_max-l_min)

        # other stuff:
        ind_tot = 0
        # do the plots:
        for ind in xrange(1,num1+1):
            for ind2 in xrange(1, ind+1):

                ind_tot += 1
                # place the plots:
                temp      = plt.subplot2grid((num_tot,2), (ind_tot-1, 0))
                temp_comp = plt.subplot2grid((num_tot,2), (ind_tot-1, 1))
                # values of the Cls:
                col          = ind + num1*(ind2-1)
                # get the y values:
                yvalues_1    = self.scalCovCls_1[:,col]
                yvalues_2    = self.scalCovCls_2[:,col]
                # interpolate the two spectra:
                spline_1 = UnivariateSpline( xvalues_1, yvalues_1, s=0)
                spline_2 = UnivariateSpline( xvalues_2, yvalues_2, s=0)
                # evaluate on the l grid:
                yvalues_1 = spline_1(l_values)
                yvalues_2 = spline_2(l_values)
                # protect against zeroes:
                yvalues_1_temp = np.abs( yvalues_1 )
                yvalues_2_temp = np.abs( yvalues_2 )
                try:
                    min2val_1      = np.amin(yvalues_1_temp[np.nonzero(yvalues_1_temp)])
                    min2val_2      = np.amin(yvalues_2_temp[np.nonzero(yvalues_2_temp)])
                except:
                    min2val_1      = cutoff
                    min2val_2      = cutoff
                np.place(yvalues_1, yvalues_1 == 0.0, min2val_1)
                np.place(yvalues_2, yvalues_2 == 0.0, min2val_2)
                # computation of the percentual comparison:
                yvalues_comp = (yvalues_1 - yvalues_2)/abs(yvalues_1)*100
                # protection against values too small:
                np.place(yvalues_comp, abs(yvalues_comp)<cutoff, [cutoff])

                # make the plots:
                plots_1.Generic_Cl(temp, l_values, yvalues_1)
                plots_2.Generic_Cl(temp, l_values, yvalues_2)
                plots_compa.Generic_Cl(temp_comp, l_values, yvalues_comp)

                temp.set_title(dict[ind2]+dict[ind]+' power spectrum')

        # set the size of the image
        fig.set_size_inches( self.x_size_inc, self.y_size_inc/5.0*num_tot)
        # set a tight layout
        fig.tight_layout(pad=0.3, h_pad=0.3, w_pad=0.3)

        # set the global legend
        fig.legend( handles = [plots_1.Generic_Cl_plot, plots_2.Generic_Cl_plot, plots_compa.CV_p],
                    labels  = [self.name_h1, self.name_h2, 'Cosmic variance'],
                    loc='lower center', ncol=3 ,fancybox=True)

        # set the global title
        plt.suptitle(self.name_h1+' VS '+self.name_h2+
                     ' comparison of scalar Cov Cls', fontsize=16)

        # adjust the size of the plot
        fig.subplots_adjust(top=0.92, bottom=0.08)
        #        fig.subplots_adjust(top=0.96, bottom=0.01)
        # save the result and close
        plt.savefig(self.outpath+self.name1+'_'+self.name2+'_scalCovCls_comp.pdf')
        plt.clf()
        plt.close("all")
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

# method1: subplot2grid
plt.figure()
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3, rowspan=1)
ax1.plot([1, 2], [1, 2])
ax1.set_title('ax1_title')
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1))

# method2: gridspec
plt.figure()
gs = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(gs[0, :])
ax2 = plt.subplot(gs[1, :2])
ax3 = plt.subplot(gs[1:, 2])
ax4 = plt.subplot(gs[-1, 0])
ax5 = plt.subplot(gs[-1, -2])

plt.tight_layout()
plt.show()
Example #48
0
    (0.904005064928743, 0.3038725882767085, 0.9399279068775889),
    (0.39140782566655674, 0.761012099948101, 0.7475874114794775),
    (0.0965359591761811, 0.43566457484639054, 0.9375581594394308),
    (0.859944654091911, 0.208070821188862, 0.8893517695418856),
    (0.022700048163251885, 0.658455757390323, 0.45194508876647577),
    (0.5934259725250017, 0.6259544064286037, 0.8943937276483482),
    (0.1248759682295419, 0.1286185769691658, 0.6973677590395778),
    (0.1834548561930609, 0.8625908063396674, 0.2808367027257399),
    (0.7072265637451247, 0.795648339142106, 0.4662593453344923),
    (0.9522043509564118, 0.8383482335114356, 0.04624824811210648),
    (0.2509444122476855, 0.723665792376911, 0.1685356796751546)
]

d_widths = [.5] * len(['2', '3', '4'])

ax1 = plt.subplot2grid((7, 7), (0, 0), rowspan=6, colspan=7)
SBG = StackedBarGrapher()
SBG.stackedBarPlot(ax1,
                   A,
                   my_color_list,
                   xLabels=['2', '3', '4'],
                   yTicks=3,
                   widths=d_widths,
                   gap=0.005,
                   scale=False)

for i in range(len(A)):
    Ai = [x for x in A[i] if x > 0]
    y = [x / 2.0 for x in Ai]
    for j in range(len(Ai)):
        if j > 0:
Example #49
0
def loadRunSTGNeuroML_L123(filename):
    'Loads and runs the pyloric rhythm generator from NeuroML files.'
    # for graded synapses, else NeuroML event-based are used
    from load_synapses import load_synapses
    moose.Neutral('/library')
    # set graded to False to use event based synapses
    #  if False, neuroml event-based synapses get searched for and loaded
    # True to load graded synapses
    graded_syn = True
    #graded_syn = False
    if graded_syn:
        load_synapses()

    neuromlR = NeuroML()
    ## readNeuroMLFromFile below returns:
    # This returns
    # populationDict = {
    #     'populationname1':('cellName',{('instanceid1'):moosecell, ... })
    #     , ...
    #     }
    # (cellName and instanceid are strings, mooosecell is a moose.Neuron object instance)
    # and
    # projectionDict = {
    #     'projName1':('source','target',[('syn_name1','pre_seg_path','post_seg_path')
    #     ,...])
    #     , ...
    #     }
    populationDict, projectionDict = \
        neuromlR.readNeuroMLFromFile(filename)
    soma1_path = populationDict['AB_PD'][1][0].path + '/Soma_0'
    soma1Vm = setupTable('somaVm', moose.Compartment(soma1_path), 'Vm')
    soma2_path = populationDict['LP'][1][0].path + '/Soma_0'
    soma2Vm = setupTable('somaVm', moose.Compartment(soma2_path), 'Vm')
    soma3_path = populationDict['PY'][1][0].path + '/Soma_0'
    soma3Vm = setupTable('somaVm', moose.Compartment(soma3_path), 'Vm')

    # monitor channel current
    channel_path = soma1_path + '/KCa_STG'
    channel_Ik = setupTable('KCa_Ik', moose.element(channel_path), 'Ik')
    # monitor Ca
    capool_path = soma1_path + '/CaPool_STG'
    capool_Ca = setupTable('CaPool_Ca', moose.element(capool_path), 'Ca')

    # monitor synaptic current
    soma2 = moose.element(soma2_path)
    print(("Children of", soma2_path, "are:"))
    for child in soma2.children:
        print((child.className, child.path))
    if graded_syn:
        syn_path = soma2_path + '/DoubExpSyn_Ach__cells-0-_AB_PD_0-0-_Soma_0'
        syn = moose.element(syn_path)
    else:
        syn_path = soma2_path + '/DoubExpSyn_Ach'
        syn = moose.element(syn_path)
    syn_Ik = setupTable('DoubExpSyn_Ach_Ik', syn, 'Ik')

    print("Reinit MOOSE ... ")
    resetSim(['/elec', cells_path], simdt, plotdt, simmethod='hsolve')

    print(("Using graded synapses? = ", graded_syn))
    print(("Running model filename = ", filename, " ... "))
    moose.start(runtime)
    tvec = np.arange(0.0, runtime + 2 * plotdt, plotdt)
    tvec = tvec[:soma1Vm.vector.size]

    fig = plt.figure(facecolor='w', figsize=(10, 6))
    axA = plt.subplot2grid((3, 2), (0, 0), rowspan=3, colspan=1, frameon=False)
    img = plt.imread('STG.png')
    imgplot = axA.imshow(img)
    for tick in axA.get_xticklines():
        tick.set_visible(False)
    for tick in axA.get_yticklines():
        tick.set_visible(False)
    axA.set_xticklabels([])
    axA.set_yticklabels([])
    ax = plt.subplot2grid((3, 2), (0, 1), rowspan=1, colspan=1)
    ax.plot(tvec,
            soma1Vm.vector * 1000,
            label='AB_PD',
            color='g',
            linestyle='solid')
    ax.set_xticklabels([])
    ax.set_ylabel('AB_PD (mV)')
    ax = plt.subplot2grid((3, 2), (1, 1), rowspan=1, colspan=1)
    ax.plot(tvec,
            soma2Vm.vector * 1000,
            label='LP',
            color='r',
            linestyle='solid')
    ax.set_xticklabels([])
    ax.set_ylabel('LP (mV)')
    ax = plt.subplot2grid((3, 2), (2, 1), rowspan=1, colspan=1)
    ax.plot(tvec,
            soma3Vm.vector * 1000,
            label='PY',
            color='b',
            linestyle='solid')
    ax.set_ylabel('PY (mV)')
    ax.set_xlabel('time (s)')
    fig.tight_layout()

    fig = plt.figure(facecolor='w')
    plt.plot(tvec,
             soma2Vm.vector * 1000,
             label='LP',
             color='r',
             linestyle='solid')
    plt.plot(tvec,
             soma3Vm.vector * 1000,
             label='PY',
             color='b',
             linestyle='solid')
    plt.legend()
    plt.xlabel('time (s)')
    plt.ylabel('Soma Vm (mV)')

    plt.figure(facecolor='w')
    plt.plot(tvec, channel_Ik.vector, color='b', linestyle='solid')
    plt.title('KCa current; Ca conc')
    plt.xlabel('time (s)')
    plt.ylabel('Ik (Amp)')
    plt.twinx()
    plt.plot(tvec, capool_Ca.vector, color='r', linestyle='solid')
    plt.ylabel('Ca (mol/m^3)')

    plt.figure(facecolor='w')
    plt.plot(tvec, syn_Ik.vector, color='b', linestyle='solid')
    plt.title('Ach syn current in ' + soma2_path)
    plt.xlabel('time (s)')
    plt.ylabel('Isyn (S)')
    print("Showing plots ...")

    plt.show()
Example #50
0
    plotenddate = mdates.num2date(frame_time_num + k + 4)

    #slicing

    #take only those indices where the difference to frame_time_num+k is less than 3
    mes_ind_plot = np.where(abs(mes_time - (frame_time_num + k)) < 4)
    vex_ind_plot = np.where(abs(vex_time - (frame_time_num + k)) < 4)
    stb_ind_plot = np.where(abs(stb_time - (frame_time_num + k)) < 4)
    sta_ind_plot = np.where(abs(sta_time - (frame_time_num + k)) < 4)
    wind_ind_plot = np.where(abs(wind_time - (frame_time_num + k)) < 4)

    lineweit = 0.2

    #rows - columns
    #MESSENGER
    ax2 = plt.subplot2grid((3, 1), (0, 0))
    ax2.plot_date(mes_time[mes_ind_plot],
                  mes.btot[mes_ind_plot],
                  '-k',
                  lw=lineweit)
    ax2.plot_date(mes_time[mes_ind_plot],
                  mes.bx[mes_ind_plot],
                  '-r',
                  lw=lineweit)
    ax2.plot_date(mes_time[mes_ind_plot],
                  mes.by[mes_ind_plot],
                  '-g',
                  lw=lineweit)
    ax2.plot_date(mes_time[mes_ind_plot],
                  mes.bz[mes_ind_plot],
                  '-b',
def main():

    args = get_args()

    # treat "-" as none in ylims
    a, b = args.ylims
    args.ylims[0] = None if a in ["-", None] else float(a)
    args.ylims[1] = None if b in ["-", None] else float(b)

    # load table manipulation
    table = FeatureTable(
        args.input,
        focus=args.focal_feature,
        last=args.last_metadatum,
        metaheader=args.focal_metadatum,
        exclude_unclassified=args.exclude_unclassified,
    )

    # collapse to genera?
    if args.as_genera:
        table.as_genera()

    # remove zeroes?
    if args.remove_zeroes:
        table.remove_zeroes()

    # apply one or more sorting methods
    for method in args.sort:
        table.sort(method)

    # filter/collapse features (moved to take place AFTER sorting)
    table.filter_top_strata(args.top_strata)

    # set up axis system
    wunits = args.width + 1
    fig = plt.figure()
    fig.set_size_inches(*args.dimensions)
    if table.metarow is not None:
        main_ax = plt.subplot2grid((c_hunits, wunits), (0, 0),
                                   rowspan=c_hunits - 1,
                                   colspan=wunits - 1)
        anno_ax = plt.subplot2grid((c_hunits, wunits), (0, wunits - 1),
                                   rowspan=c_hunits,
                                   colspan=1)
        meta_ax = plt.subplot2grid((c_hunits, wunits), (c_hunits - 1, 0),
                                   rowspan=1,
                                   colspan=wunits - 1)
        dummy(meta_ax, border=True)
    else:
        main_ax = plt.subplot2grid((1, wunits), (0, 0),
                                   rowspan=1,
                                   colspan=wunits - 1)
        anno_ax = plt.subplot2grid((1, wunits), (0, wunits - 1),
                                   rowspan=1,
                                   colspan=1)
    dummy(anno_ax)
    anno_ax.set_xlim(0, 1)
    anno_ax.set_ylim(0, 1)

    # setup: strata colors
    cdict = {"Other": "0.5", "Unclassified": "0.8"}
    if os.path.exists(args.colormap):
        sys.stderr.write("Reading strata colors from file: {}\n".format(
            args.colormap))
        for item, color in tsv_reader(args.colormap):
            if item not in cdict:
                cdict[item] = color
        for f in table.rowheads:
            cdict[f] = cdict.get(f, "black")
    else:
        colors = get_colors(args.colormap, args.top_strata)
        for f in table.rowheads:
            if f not in cdict:
                cdict[f] = colors.pop()

    # scaling options
    if args.scaling == "none":
        ylabel = "Relative abundance"
        bottoms = np.zeros(table.ncols)
        ymin = 0 if args.ylims[0] is None else args.ylims[0]
        ymax = max(sum(table.data)) if args.ylims[1] is None else args.ylims[1]
        main_ax.set_ylim(ymin, ymax)
    elif args.scaling == "normalize":
        ylabel = "Relative contributions"
        table.data = table.data / table.colsums
        bottoms = np.zeros(table.ncols)
        main_ax.set_ylim(0, 1)
    elif args.scaling == "pseudolog":
        ylabel = "log10(Relative abundance)"
        ymin = min([k for k in table.colsums
                    if k > 0]) if args.ylims[0] is None else args.ylims[0]
        floor = math.floor(np.log10(ymin))
        floor = floor if abs(np.log10(ymin) -
                             floor) >= c_epsilon else floor - 1
        floors = floor * np.ones(table.ncols)
        crests = np.array(
            [np.log10(k) if k > 10**floor else floor for k in table.colsums])
        heights = crests - floors
        table.data = table.data / table.colsums * heights
        ymax = max(table.colsums) if args.ylims[1] is None else args.ylims[1]
        ceil = math.ceil(np.log10(ymax))
        bottoms = floors
        main_ax.set_ylim(floor, ceil)

    # add bars
    series = []
    for i, f in enumerate(table.rowheads):
        frow = table.data[table.rowmap[f]]
        series.append(
            main_ax.bar(
                range(table.ncols),
                frow,
                width=1,
                bottom=bottoms,
                color=cdict[f],
                edgecolor="none",
            ))
        bottoms += frow

    # setup: meta colors
    if table.metarow is not None:
        if os.path.exists(args.meta_colormap):
            sys.stderr.write("Reading meta colors from file: {}\n".format(
                args.meta_colormap))
            mcdict = {}
            for item, color in tsv_reader(args.meta_colormap):
                mcdict[item] = color
            for m in table.metarow:
                mcdict[m] = mcdict.get(m, "black")
        else:
            unique = sorted(set(table.metarow))
            mcdict = {
                v: c
                for v, c in zip(unique,
                                get_colors(args.meta_colormap, len(unique)))
            }

    # plot metadata?
    if table.metarow is not None:
        meta_ax.set_xlim(0, len(table.metarow))
        for i, v in enumerate(table.metarow):
            meta_ax.bar(
                i,
                1,
                width=1,
                color=mcdict[v],
                edgecolor="none",
            )

    # attach metadata separators
    if table.metarow is not None and args.sort[-1] == "metadata":
        xcoords = []
        for i, value in enumerate(table.metarow):
            if i > 0 and value != table.metarow[i - 1]:
                main_ax.axvline(x=i, color="black", zorder=2)
                meta_ax.axvline(x=i, color="black", zorder=2)

    # axis limits
    main_ax.set_xlim(0, table.ncols)

    # labels
    samp_ax = main_ax if table.metarow is None else meta_ax
    samp_ax.set_xlabel("Samples (N=%d)" % (table.ncols))
    #main_ax.set_title( "%s\nFrom: %s" % ( table.fname, os.path.split( args.input )[1] ) )
    main_ax.set_title(table.fname, weight="bold")
    main_ax.set_ylabel(ylabel, size=12)
    # tick params
    main_ax.tick_params(axis="x",
                        which="major",
                        direction="out",
                        bottom="on",
                        top="off")
    main_ax.tick_params(axis="y",
                        which="major",
                        direction="out",
                        left="on",
                        right="off")
    main_ax.set_xticks([])

    # pseudolog note
    if args.scaling == "pseudolog":
        xmin, xmax = main_ax.get_xlim()
        x = xmin + 0.01 * abs(xmax - xmin)
        ymin, ymax = main_ax.get_ylim()
        y = ymax - 0.04 * abs(ymax - ymin)
        main_ax.text(
            x,
            y,
            "*Stratifications are proportional",
            va="top",
            size=11,
            backgroundcolor="white",
        )

    # optional yaxis grid
    if args.grid:
        for ycoord in main_ax.yaxis.get_majorticklocs():
            main_ax.axhline(y=ycoord, color="0.75", ls=":", zorder=0)

    # legend
    scale = args.legend_stretch
    xmar = 0.01
    xsep = 0.03
    ybuf = 0.06 * scale
    ysep = 0.02 * scale
    yinc = 0.03 * scale
    rech = 0.03 * scale
    recw = 0.1
    big_font = 10 * scale
    sml_font = 8 * scale
    ydex = 1.0

    def add_items(title, labels, colors, ydex, bugmode=False):
        ydex -= ybuf
        anno_ax.text(xmar,
                     ydex,
                     title,
                     weight="bold",
                     va="center",
                     size=big_font)
        ydex -= ybuf
        for l, c in zip(labels, colors):
            anno_ax.add_patch(
                patches.Rectangle(
                    (xmar, ydex - rech),
                    recw,
                    rech,
                    facecolor=c,
                    edgecolor="k",
                ))
            anno_ax.text(
                xsep + recw,
                ydex - 0.5 * rech,
                l,
                size=sml_font,
                va="center",
                style="italic" if
                (bugmode and l not in ["Unclassified", "Other"]) else "normal",
            )
            ydex -= rech + ysep
        ydex += ysep
        return ydex

    # add legend for stratifications
    ydex = add_items(
        "Stratifications:",
        map(bugname, table.rowheads[::-1]),
        [cdict[r] for r in table.rowheads[::-1]],
        ydex,
        bugmode=True,
    )

    # add legend for metadata
    if table.metarow is not None:
        levels = sorted(set(table.metarow))
        add_items(
            "Sample label:",
            levels,
            [mcdict[k] for k in levels],
            ydex,
        )

    # wrapup
    plt.tight_layout()
    fig.subplots_adjust(hspace=0.2, wspace=0.03)
    if args.output is not None:
        plt.savefig(args.output)
    else:
        plt.savefig(os.path.split(args.input)[1] + ".pdf")
Example #52
0
def graph_data(stock):
    # Unfortunately, Yahoo's API is no longer available
    # feel free to adapt the code to another source, or use this drop-in replacement.
    stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement'
    source_code = urlrequest.urlopen(stock_price_url).read().decode()
    stock_data = []
    split_source = source_code.split('\n')
    for line in split_source[1:]:
        split_line = line.split(',')
        if len(split_line) == 7:
            if 'values' not in line and 'labels' not in line:
                stock_data.append(line)

    date, closep, highp, lowp, openp, adj_closep, volume = np.loadtxt(
        stock_data,
        delimiter=',',
        unpack=True,
        converters={0: bytespdate2num('%Y-%m-%d')})

    # plt.plot_date(date, closep, '-', label='Price')
    # dateconv = np.vectorize(dt.datetime.fromtimestamp)
    # date = dateconv(date)
    #
    # fig = plt.figure()
    # ax1 = plt.subplot2grid((1,1),(0,0))
    # ax1.plot_date(date, closep, '-', label='Price')
    # for label in ax1.xaxis.get_ticklabels():
    #     label.set_rotation(45)
    # ax1.grid(True)  # , color='g', linestyle='-', linewidth=5)
    #
    #
    # plt.xlabel('Date')
    # plt.ylabel('Price')
    # plt.title(stock)
    # plt.legend()
    # plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0)
    #
    # plt.show()
    #fig = plt.figure()
    ax1 = plt.subplot2grid((1, 1), (0, 0))

    ax1.plot([], [], linewidth=5, label='loss', color='r', alpha=0.5)
    ax1.plot([], [], linewidth=5, label='gain', color='g', alpha=0.5)

    ax1.fill_between(date,
                     closep,
                     closep[0],
                     where=(closep > closep[0]),
                     facecolor='g')
    ax1.fill_between(date,
                     closep,
                     closep[0],
                     where=(closep < closep[0]),
                     facecolor='r')

    for label in ax1.xaxis.get_ticklabels():
        label.set_rotation(45)
    ax1.grid(True)
    # ax1.xaxis.label.set_color('c')
    # ax1.yaxis.label.set_color('r')
    ax1.set_yticks([0, 25, 50, 75])

    ax1.spines['left'].set_color('c')
    ax1.spines['right'].set_visible(False)
    ax1.spines['top'].set_visible(False)
    ax1.spines['left'].set_linewidth(5)

    ax1.tick_params(axis='x', colors='#f06125')

    plt.xlabel('Date')
    plt.ylabel('Price')
    plt.title(stock)
    plt.legend()
    plt.subplots_adjust(left=0.09,
                        bottom=0.20,
                        right=0.94,
                        top=0.90,
                        wspace=0.2,
                        hspace=0)
    plt.show()
Example #53
0
################################################################################################################################
data = scipy.io.loadmat('PPO_d5_v20_0_reward-82.93')
cx = data['cx'][0]
cy = data['cy'][0]
cv = data['cv'][0]
sim_t_ppo = data['sim_t'][0]
sim_x_ppo = data['sim_x'][0]
sim_y_ppo = data['sim_y'][0]

data = scipy.io.loadmat('PP_d5_v20_0_reward-43.7')
sim_t_pp = data['sim_t'][0]
sim_x_pp = data['sim_x'][0]
sim_y_pp = data['sim_y'][0]

ax = plt.subplot2grid((2, 2), (0, 1), rowspan=1, colspan=1)
plt.plot(cx, cy, "-r", label="Ref")
plt.plot(sim_x_ppo, sim_y_ppo, "-g", label="PP+PPO")
plt.plot(sim_x_pp, sim_y_pp, "--b", label="PP")
plt.grid(True)
plt.axis("equal")
plt.xlabel("X (m)")
plt.ylabel("Y (m)")
# plt.legend(loc='upper right')
plt.title('(d) v=20, rc=5', loc='left')
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width, box.height * 0.8])
############################

################################################################################################################################
data = scipy.io.loadmat('PPO_d5_v50_0_reward-77.99')
Example #54
0
def _setup_browser_selection(raw, kind, selector=True):
    """Organize browser selections."""
    import matplotlib.pyplot as plt
    from matplotlib.widgets import RadioButtons
    from ..selection import (read_selection, _SELECTIONS, _EEG_SELECTIONS,
                             _divide_to_regions)
    from ..utils import _get_stim_channel
    _check_option('group_by', kind, ('position, selection'))
    if kind == 'position':
        order = _divide_to_regions(raw.info)
        keys = _SELECTIONS[1:]  # no 'Vertex'
        kind = 'position'
    else:  # kind == 'selection'
        from ..io import RawFIF, RawArray
        if not isinstance(raw, (RawFIF, RawArray)):
            raise ValueError("order='selection' only works for Neuromag data. "
                             "Use order='position' instead.")
        order = dict()
        try:
            stim_ch = _get_stim_channel(None, raw.info)
        except ValueError:
            stim_ch = ['']
        keys = np.concatenate([_SELECTIONS, _EEG_SELECTIONS])
        stim_ch = pick_channels(raw.ch_names, stim_ch)
        for key in keys:
            channels = read_selection(key, info=raw.info)
            picks = pick_channels(raw.ch_names, channels)
            if len(picks) == 0:
                continue  # omit empty selections
            order[key] = np.concatenate([picks, stim_ch])

    misc = pick_types(raw.info,
                      meg=False,
                      eeg=False,
                      stim=True,
                      eog=True,
                      ecg=True,
                      emg=True,
                      ref_meg=False,
                      misc=True,
                      resp=True,
                      chpi=True,
                      exci=True,
                      ias=True,
                      syst=True,
                      seeg=False,
                      bio=True,
                      ecog=False,
                      fnirs=False,
                      exclude=())
    if len(misc) > 0:
        order['Misc'] = misc
    keys = np.concatenate([keys, ['Misc']])
    if not selector:
        return order
    fig_selection = figure_nobar(figsize=(2, 6), dpi=80)
    fig_selection.canvas.set_window_title('Selection')
    rax = plt.subplot2grid((6, 1), (2, 0), rowspan=4, colspan=1)
    topo_ax = plt.subplot2grid((6, 1), (0, 0), rowspan=2, colspan=1)
    keys = np.concatenate([keys, ['Custom']])
    order.update({'Custom': list()})  # custom selection with lasso
    plot_sensors(raw.info,
                 kind='select',
                 ch_type='all',
                 axes=topo_ax,
                 ch_groups=kind,
                 title='',
                 show=False)
    fig_selection.radio = RadioButtons(
        rax, [key for key in keys if key in order.keys()])

    for circle in fig_selection.radio.circles:
        circle.set_radius(0.02)  # make them smaller to prevent overlap
        circle.set_edgecolor('gray')  # make sure the buttons are visible

    return order, fig_selection
def seasonal_section_plot(VAR,
                          SECTION,
                          SEASON,
                          YEAR,
                          ZMAX=400,
                          STATION_BASED=False):
    """
    Contour plot on standard AZMP-NL sections for a certain year (specified with nc_file), season (specified as survey), section and variable.
    This is a function version of script "azmp_section_report_plot.py" used for ResDocs figures.

    Pickled climatologies are generated by azmp_section_clim.py

    See usage example in azmp_genReport.py

    [email protected]
    August 2019

    """
    if VAR == 'temperature':
        v = np.arange(-2, 11, 1)
        v_anom = np.linspace(-3.5, 3.5, 15)
        v_anom = np.delete(v_anom, np.where(v_anom == 0))
        CMAP = cmocean.cm.thermal
        if SECTION == 'HL':
            v = np.arange(2, 15, 1)
            v_anom = np.linspace(-5.5, 5.5, 23)
            v_anom = np.delete(v_anom, np.where(v_anom == 0))
    elif VAR == 'salinity':
        v = np.arange(29, 36, .5)
        v_anom = np.linspace(-1.5, 1.5, 16)
        CMAP = cmocean.cm.haline
        if SECTION == 'HL':
            v = np.arange(30, 37, .5)
            v_anom = np.linspace(-1.5, 1.5, 16)
    elif VAR == 'sigma-t':
        v = np.arange(24, 28.4, .2)
        v_anom = np.linspace(-1.5, 1.5, 16)
        CMAP = cmocean.cm.haline
    else:
        v = 10
        v_anom = 10

    SECTION_BATHY = SECTION

    # CIL surface (Note that there is a bias because )
    def area(vs):
        a = 0
        x0, y0 = vs[0]
        for [x1, y1] in vs[1:]:
            dx = x1 - x0
            dy = y1 - y0
            a += 0.5 * (y0 * dx - x0 * dy)
            x0 = x1
            y0 = y1
        return a

    ## ---- Get this year's section ---- ##
    df_section_stn, df_section_itp = get_section(SECTION, YEAR, SEASON, VAR)

    #if df_section_itp.dropna(how='all', axis=0).size == 0: # Not sure why this is needed...
    #    return
    # Use itp or station-based definition
    if STATION_BASED:
        df_section = df_section_stn.copy()
    else:
        df_section = df_section_itp.copy()

    # In case df_section only contains NaNs..
    df_section.dropna(axis=0, how='all', inplace=True)
    if df_section.size == 0:
        print(' !!! Empty section [return None] !!!')
        return None

    ## ---- Get climatology ---- ##
    clim_name = 'df_' + VAR + '_' + SECTION + '_' + SEASON + '_clim.pkl'
    df_clim = pd.read_pickle(clim_name)
    # Update index to add distance (in addition to existing station name)
    df_clim.index = df_section_itp.loc[df_clim.index].index

    ## ---- Retrieve bathymetry using function ---- ##
    bathymetry = section_bathymetry(SECTION_BATHY)

    ## ---  ---- ##
    df_anom = df_section - df_clim
    df_anom = df_anom.reset_index(level=0, drop=True)
    # drop empty columns (NAKE SURE I AM NOT INTRODUCING ERRROS)
    df_anom.dropna(how='all', inplace=True)
    ## ---- plot Figure ---- ##
    XLIM = df_section_itp.index[-1][1]
    fig = plt.figure()
    # ax1
    ax = plt.subplot2grid((3, 1), (0, 0))
    if len(df_section.index) > 1 & len(df_section.columns > 1):
        c = plt.contourf(df_section.index.droplevel(0),
                         df_section.columns,
                         df_section.T,
                         v,
                         cmap=CMAP,
                         extend='max')
        plt.colorbar(c)
        if VAR == 'temperature':
            c_cil_itp = plt.contour(df_section.index.droplevel(0),
                                    df_section.columns,
                                    df_section.T, [
                                        0,
                                    ],
                                    colors='k',
                                    linewidths=2)
    ax.set_ylim([0, ZMAX])
    ax.set_xlim([0, XLIM])
    ax.set_ylabel('Depth (m)', fontWeight='bold')
    ax.invert_yaxis()
    Bgon = plt.Polygon(bathymetry,
                       color=np.multiply([1, .9333, .6667], .4),
                       alpha=0.8)
    ax.add_patch(Bgon)
    ax.xaxis.label.set_visible(False)
    ax.tick_params(labelbottom='off')
    ax.set_title(VAR + ' for section ' + SECTION + ' - ' + SEASON + ' ' +
                 str(YEAR))

    # ax2
    ax2 = plt.subplot2grid((3, 1), (1, 0))
    c = plt.contourf(df_clim.index.droplevel(0),
                     df_clim.columns,
                     df_clim.T,
                     v,
                     cmap=CMAP,
                     extend='max')
    plt.colorbar(c)
    if VAR == 'temperature':
        c_cil_itp = plt.contour(df_clim.index.droplevel(0),
                                df_clim.columns,
                                df_clim.T, [
                                    0,
                                ],
                                colors='k',
                                linewidths=2)
    ax2.set_ylim([0, ZMAX])
    ax2.set_xlim([0, XLIM])
    ax2.set_ylabel('Depth (m)', fontWeight='bold')
    ax2.invert_yaxis()
    Bgon = plt.Polygon(bathymetry,
                       color=np.multiply([1, .9333, .6667], .4),
                       alpha=0.8)
    ax2.add_patch(Bgon)
    ax2.xaxis.label.set_visible(False)
    ax2.tick_params(labelbottom='off')
    ax2.set_title('1991-2020 climatology')

    # ax3
    ax3 = plt.subplot2grid((3, 1), (2, 0))
    df_anom.shape
    if len(df_section.index) > 1 & len(df_section.columns > 1):
        c = plt.contourf(df_anom.index,
                         df_anom.columns,
                         df_anom.T,
                         v_anom,
                         cmap=cmocean.cm.balance,
                         extend='both')
        plt.colorbar(c)
    ax3.set_ylim([0, ZMAX])
    ax3.set_xlim([0, XLIM])
    ax3.set_ylabel('Depth (m)', fontWeight='bold')
    ax3.set_xlabel('Distance (km)', fontWeight='bold')
    ax3.invert_yaxis()
    Bgon = plt.Polygon(bathymetry,
                       color=np.multiply([1, .9333, .6667], .4),
                       alpha=0.8)
    ax3.add_patch(Bgon)
    ax3.set_title(r'Anomaly')

    fig.set_size_inches(w=8, h=12)
    fig_name = VAR + '_' + SECTION + '_' + SEASON + '_' + str(YEAR) + '.png'
    fig.savefig(fig_name, dpi=200)
    os.system('convert -trim ' + fig_name + ' ' + fig_name)

    # Save in French
    if (VAR == 'temperature') & (SEASON == 'summer'):
        ax.set_title('Température à la section ' + SECTION + ' - été ' +
                     str(YEAR))
    elif (VAR == 'salinity') & (SEASON == 'summer'):
        ax.set_title('Salinité à la section ' + SECTION + ' - été ' +
                     str(YEAR))

    ax.set_ylabel('Profondeur (m)', fontWeight='bold')
    ax2.set_ylabel('Profondeur (m)', fontWeight='bold')
    ax3.set_ylabel('Profondeur (m)', fontWeight='bold')

    ax2.set_title(r'Climatologie 1991-2020')
    ax3.set_title(r'Anomalie')
    fig_name = VAR + '_' + SECTION + '_' + SEASON + '_' + str(YEAR) + '_FR.png'
    fig.savefig(fig_name, dpi=200)
    os.system('convert -trim ' + fig_name + ' ' + fig_name)

    # Export data in csv.
    stn_file = VAR + '_' + SECTION + '_' + SEASON + '_' + str(
        YEAR) + '_stn.csv'
    itp_file = VAR + '_' + SECTION + '_' + SEASON + '_' + str(
        YEAR) + '_itp.csv'
    df_section_stn.T.to_csv(stn_file, float_format='%.3f')
    df_section_itp.T.to_csv(itp_file, float_format='%.3f')

    # Pickle data
    stn_pickle = VAR + '_' + SECTION + '_' + SEASON + '_' + str(
        YEAR) + '_stn.pkl'
    itp_pickle = VAR + '_' + SECTION + '_' + SEASON + '_' + str(
        YEAR) + '_itp.pkl'
    df_section_stn.to_pickle(stn_pickle)
    df_section_itp.to_pickle(itp_pickle)
    del df_section_stn, df_section_itp, df_clim, df_anom
Example #56
0
    except ValueError:  # aka if x_min_list is empty
        x_min = datetime.date(2009, 1, 1)

    # Creation of a letter paper-sized figure
    fig = plt.figure(figsize=(16.5, 11.7), dpi=100)  # figsize=(11.69,8.27)

    plt.xticks(rotation=25)

    # Adding a title
    fig.suptitle(well, fontsize=16)

    # Setting the space between subplots
    plt.subplots_adjust(hspace=0, wspace=0.3)

    # Creation of the first subplot (main graph)
    ax0 = plt.subplot2grid(gridSpace, (0, 0), colspan=3, rowspan=4)

    # Plotting the Water Cut percentage.
    line5 = sns.lineplot(x='Date',
                         y='WCut [%]',
                         data=well_df,
                         ax=ax0,
                         color=Water,
                         label='Water Cut',
                         alpha=1)
    # Plotting the Well Tests
    line4 = sns.lineplot(x='Date',
                         y='Test',
                         data=well_df,
                         ax=ax0,
                         color=Steel,
Example #57
0
        S, S_lab, B, B_lab = list(map(tf.convert_to_tensor, ds))
        model = Conv(K, N, episode_len, S, S_lab, B, B_lab, mem)
        sess.run(tf.global_variables_initializer())
        tf.train.Saver().restore(sess, model_path)
        blab, plab = sess.run([B_lab, model.pred])
    sess.close()

    num_correct = (plab == blab.argmax(axis=1)).sum()

    # Display support set
    for i in range(N):
        ax = plt.subplot(100 + N * 10 + i + 1)
        plt.imshow(ds[0][i][:, :, 0], cmap='gray')
        ax.set_title(str(ds[1][i].argmax()))
        ax.axis('off')
    plt.suptitle('Support Set', fontsize=30)
    plt.savefig(os.path.join(output_dir, 'demo_S.png'))

    # Display test samples and predictions
    for i in range(episode_len):
        for j in range(N):
            ax = plt.subplot2grid((episode_len, N), (i, j))
            ind = N * i + j
            plt.imshow(ds[2][ind][:, :, 0], cmap='gray')
            ax.set_title(str(plab[ind]))
            ax.axis('off')
    plt.suptitle('Predictions. Correct: ' + str(num_correct) + '/' +
                 str(plab.size),
                 fontsize=20)
    plt.savefig(os.path.join(output_dir, 'demo_B.png'))
def btl_section_plot(VAR, SECTION, SEASON, YEAR, ZMAX=400):
    """
    Contour plot of water sample (bottle) data on standard AZMP-NL sections for a certain year, season, section and variable.
    This is a function version of script "azmp_blt_section.py" used for ResDocs figures.

    This function uses pickled objects generated by azmp_utils.masterfile_section_to_multiindex.py

    See usage example in azmp_process_btl_plots.py

    [email protected]
    Sept. 2019

    """

    # derived parameters
    if VAR == 'temperature':
        v = np.arange(-2, 11, 1)
        v_anom = np.linspace(-3.5, 3.5, 15)
        v_anom = np.delete(v_anom, np.where(v_anom == 0))
        CMAP = cmocean.cm.thermal
    elif VAR == 'salinity':
        v = np.arange(29, 36, .5)
        v_anom = np.linspace(-1.5, 1.5, 16)
        CMAP = cmocean.cm.haline
    elif VAR == 'oxygen':
        v = np.arange(5.5, 8.5, .2)
        v_anom = np.linspace(-1, 1, 11)
        CMAP = cmocean.cm.thermal
    elif VAR == 'satO2_perc':
        v = np.arange(70, 100, 2)
        v_anom = np.linspace(-10, 10, 11)
        CMAP = cmocean.cm.thermal
    elif VAR == 'PO4':
        v = np.arange(0, 2, .1)
        v_anom = np.linspace(-1, 1, 11)
        CMAP = cmocean.cm.thermal
    elif VAR == 'NO3':
        v = np.arange(0, 16, 1)
        v_anom = np.linspace(-5, 5, 11)
        CMAP = cmocean.cm.thermal
    elif VAR == 'SIO':
        v = np.arange(0, 20, 1)
        v_anom = np.linspace(-5, 5, 11)
        CMAP = cmocean.cm.thermal
    elif VAR == 'sigmat':
        v = np.arange(25, 27.6, .1)
        v_anom = np.linspace(-5, 5, 11)
        CMAP = cmocean.cm.thermal
    else:
        v = 10
        v_anom = 10
        CMAP = cmocean.cm.thermal

    SECTION_BATHY = SECTION
    v_sig = [25, 26, 27, 27.5]

    ## ---- Retrieve bottle data ---- ##
    SECTION_FILE = 'bottle_data_multiIndex_' + SECTION + '.pkl'
    df = pd.read_pickle(SECTION_FILE)

    if YEAR in df.index.get_level_values(1).unique():

        ## ---- Compute climatology ---- ##
        df_clim = df.xs((VAR, SEASON), level=('variable', 'season'))
        df_clim = df_clim.groupby(level=0).apply(lambda x: x.mean())
        # Compute also sigma-t
        df_sigt_clim = df.xs(('sigmat', SEASON), level=('variable', 'season'))
        df_sigt_clim = df_sigt_clim.groupby(level=0).apply(lambda x: x.mean())

        ## ---- Compute current year & anomaly ---- ##
        df_year = df.xs((YEAR, VAR, SEASON),
                        level=('year', 'variable', 'season'))
        df_year = df_year.groupby(level=0).apply(lambda x: x.mean())
        df_sigt_year = df.xs((YEAR, 'sigmat', SEASON),
                             level=('year', 'variable', 'season'))
        df_sigt_year = df_sigt_year.groupby(level=0).apply(lambda x: x.mean())

        df_anom = df_year - df_clim

        # Drop NaNs
        df_year = df_year.dropna(axis=0, how='all')
        df_sigt_year = df_sigt_year.dropna(axis=0, how='all')
        df_clim = df_clim.dropna(axis=0, how='all')
        df_sigt_clim = df_sigt_clim.dropna(axis=0, how='all')
        df_anom = df_anom.dropna(axis=0, how='all')

        if df_year.size == 0:
            print(' !!! Empty section [return None] !!!')
            return None

        ## ---- Load station lat/lon ---- ##
        df_stn = pd.read_excel(
            '/home/cyrf0006/github/AZMP-NL/data/STANDARD_SECTIONS.xlsx')
        df_stn = df_stn.drop(['SECTION', 'LONG'], axis=1)
        df_stn = df_stn.rename(columns={'LONG.1': 'LON'})
        df_stn = df_stn.dropna()
        df_stn = df_stn[df_stn.STATION.str.contains(SECTION)]
        df_stn = df_stn.reset_index(drop=True)
        # remove hyphen in stn names (normally should keep it everywhere, but simpler here)
        df_stn['STATION'] = df_stn.STATION.str.replace('-', '')

        ## ---- Compute distance vector ---- ##
        distance = np.full((df_clim.index.shape), np.nan)
        lat0 = df_stn[df_stn.index == 0]['LAT']
        lon0 = df_stn[df_stn.index == 0]['LON']
        for i, stn in enumerate(df_clim.index):
            lat_stn = df_stn[df_stn.STATION == stn]['LAT']
            lon_stn = df_stn[df_stn.STATION == stn]['LON']
            distance[i] = haversine(lon0, lat0, lon_stn, lat_stn)
        XLIM = distance.max()
        # Distance for current year
        distance_year = np.full((df_year.index.shape), np.nan)
        for i, stn in enumerate(df_year.index):
            lat_stn = df_stn[df_stn.STATION == stn]['LAT']
            lon_stn = df_stn[df_stn.STATION == stn]['LON']
            distance_year[i] = haversine(lon0, lat0, lon_stn, lat_stn)
        # Distance for sigt (rare but sometimes not same as other)
        distance_sigt = np.full((df_sigt_year.index.shape), np.nan)
        for i, stn in enumerate(df_sigt_year.index):
            lat_stn = df_stn[df_stn.STATION == stn]['LAT']
            lon_stn = df_stn[df_stn.STATION == stn]['LON']
            distance_sigt[i] = haversine(lon0, lat0, lon_stn, lat_stn)

        ## ---- Retrieve bathymetry using function ---- ##
        bathymetry = section_bathymetry(SECTION_BATHY)

        ## ---- plot Figure ---- ##
        #XLIM = df_section_itp.index[-1][1]
        fig = plt.figure()
        # ax1
        ax = plt.subplot2grid((3, 1), (0, 0))
        c = plt.contourf(distance_year,
                         df_year.columns,
                         df_year.T,
                         v,
                         cmap=CMAP,
                         extend='both')
        c_sig1 = plt.contour(distance_sigt,
                             df_sigt_year.columns,
                             df_sigt_year.T,
                             v_sig,
                             colors='gray',
                             linewidths=1)
        for i in distance_year:
            plt.plot([i, i], [0, ZMAX], '--k', alpha=.5)
        ax.set_ylim([0, ZMAX])
        ax.set_xlim([0, XLIM])
        plt.clabel(c_sig1, inline=1, fontsize=10, colors='gray', fmt='%1.1f')
        ax.set_ylabel('Depth (m)', fontWeight='bold')
        ax.invert_yaxis()
        Bgon = plt.Polygon(bathymetry,
                           color=np.multiply([1, .9333, .6667], .4),
                           alpha=1,
                           zorder=10)
        ax.add_patch(Bgon)
        plt.colorbar(c)
        ax.xaxis.label.set_visible(False)
        ax.tick_params(labelbottom='off')
        ax.set_title(VAR + ' for section ' + SECTION + ' - ' + SEASON + ' ' +
                     str(YEAR))

        # ax2
        ax2 = plt.subplot2grid((3, 1), (1, 0))
        c = plt.contourf(distance,
                         df_clim.columns,
                         df_clim.T,
                         v,
                         cmap=CMAP,
                         extend='both')
        c_sig2 = plt.contour(distance,
                             df_sigt_clim.columns,
                             df_sigt_clim.T,
                             v_sig,
                             colors='gray',
                             linewidths=1)
        for i in distance:
            plt.plot([i, i], [0, ZMAX], '--k', alpha=.5)
        ax2.set_ylim([0, ZMAX])
        ax2.set_xlim([0, XLIM])
        plt.clabel(c_sig2, inline=1, fontsize=10, colors='gray', fmt='%1.1f')
        ax2.set_ylabel('Depth (m)', fontWeight='bold')
        ax2.invert_yaxis()
        Bgon = plt.Polygon(bathymetry,
                           color=np.multiply([1, .9333, .6667], .4),
                           alpha=1,
                           zorder=10)
        ax2.add_patch(Bgon)
        plt.colorbar(c)
        ax2.xaxis.label.set_visible(False)
        ax2.tick_params(labelbottom='off')
        ax2.set_title('1999-' + str(df.index.get_level_values(1).max()) +
                      ' climatology')

        # ax3
        ax3 = plt.subplot2grid((3, 1), (2, 0))
        c = plt.contourf(distance_year,
                         df_anom.columns,
                         df_anom.T,
                         v_anom,
                         cmap=cmocean.cm.balance,
                         extend='both')
        ax3.set_ylim([0, ZMAX])
        ax3.set_xlim([0, XLIM])
        ax3.set_ylabel('Depth (m)', fontWeight='bold')
        ax3.set_xlabel('Distance (km)', fontWeight='bold')
        ax3.invert_yaxis()
        Bgon = plt.Polygon(bathymetry,
                           color=np.multiply([1, .9333, .6667], .4),
                           alpha=1,
                           zorder=10)
        ax3.add_patch(Bgon)
        plt.colorbar(c)
        ax3.set_title(r'Anomaly')

        fig.set_size_inches(w=8, h=12)
        fig_name = 'btl_' + VAR + '_' + SECTION + '_' + SEASON + '_' + str(
            YEAR) + '.png'
        fig.savefig(fig_name, dpi=200)
        os.system('convert -trim ' + fig_name + ' ' + fig_name)

        ## ---- Export data in csv ---- ##
        # add new index
        df_year['distance'] = distance_year
        df_year.set_index('distance', append=True, inplace=True)
        df_clim['distance'] = distance
        df_clim.set_index('distance', append=True, inplace=True)
        # Save in csv
        csv_file = VAR + '_' + SECTION + '_' + SEASON + '_' + str(
            YEAR) + '_btl.csv'
        df_year.T.to_csv(csv_file, float_format='%.3f')
        csv_clim = VAR + '_' + SECTION + '_' + SEASON + '_btl_clim.csv'
        df_clim.T.to_csv(csv_clim, float_format='%.3f')
Example #59
0
def plot_recordsection_helper(records, color_code, opt_params, list_inc,
                              list_process, list_filters):
    """ Plots seismic record section. It is an internal function for
     plot_record_section methods in the Project class.
     should not be directly used by the end users.
    """

    # Check number of input incidents
    if len(records[0]) > len(color_code):
        LOGGER.error(f"Number of timeseries are more than dedicated" "colors.")
        return

    with_details = False
    if query_opt_params(opt_params, 'save_figure'):
        nrs = 7
        fig, axarr = plt.subplots(nrows=nrs, ncols=1, figsize=(14, 9))
        rspan = 6
        with_details = True
        axarr[0] = plt.subplot2grid((nrs, 1), (0, 0), rowspan=rspan, colspan=1)

    else:
        nrs = 1
        fig, axarr = plt.subplots(nrows=nrs, ncols=1, figsize=(14, 9))
        rspan = 1
        axarr = plt.subplot2grid((nrs, 1), (0, 0), rowspan=rspan, colspan=1)

    if with_details:
        axarr[1] = plt.subplot2grid((nrs, 1), (6, 0), rowspan=1, colspan=1)

    x_lim_t = check_opt_param_minmax(opt_params, 'zoom_in_time')

    comp = opt_params.get('comp', None)
    if not comp or (comp not in ["h1", "h2", "ver"]):
        if comp:
            LOGGER.warning(f"The component: {comp} is not supported. "
                           "h1 is used.")
        comp = "h1"

    for k, record in enumerate(records):
        for i, item in enumerate(record):
            if not item:
                continue

            if comp == "h1":
                tmp_data = (0.8*item.vel_h1.value/item.vel_h1.peak_vv)+\
                    item.epicentral_distance
            elif comp == "h2":
                tmp_data = (0.8*item.vel_h2.value/item.vel_h2.peak_vv)+\
                    item.epicentral_distance
            elif comp == "ver":
                tmp_data = (0.8*item.vel_ver.value/item.vel_ver.peak_vv)+\
                    item.epicentral_distance
            else:
                LOGGER.error("Should never get here. Double check.")

            if k == 0:
                legend_label = list_inc[i]
            else:
                legend_label = None

            if with_details:
                axarr[0].plot(item.time_vec,
                              tmp_data,
                              color_code[i],
                              label=legend_label,
                              linewidth=0.2)
            else:
                axarr.plot(item.time_vec,
                           tmp_data,
                           color_code[i],
                           label=legend_label,
                           linewidth=0.2)

    if with_details:
        axarr[0].set_xlabel('Time (s)')
        axarr[0].set_ylabel('Epicentral Distance (km)')
        axarr[0].set_xlim(x_lim_t)
        axarr[0].legend()
        axarr[0].set_title(f"Normalized Seismic Record Section -"
                           f"Number of stations/incident: {k+1}"
                           f"- Component: {comp}")
    else:
        axarr.set_xlabel('Time (s)')
        axarr.set_ylabel('Epicentral Distance (km)')
        axarr.set_xlim(x_lim_t)
        axarr.legend()
        axarr.set_title(f"Normalized Seismic Record Section -"
                        f"Number of stations/incident: {k+1}"
                        f"- Component: {comp}")


    f_name_save = "f_recordsection_plot_" +\
    datetime.now().strftime("%Y%m%d_%H%M%S_%f" + ".pdf")
    details = [f_name_save, list_inc, list_process, list_filters, {}]
    message = list2message(details)

    if with_details:
        footnote_font = FontProperties()
        footnote_font.set_size(6)
        max_height = 100
        axarr[1].text(1,
                      0.8 * max_height,
                      message,
                      va='top',
                      fontproperties=footnote_font,
                      wrap=True)

        axarr[1].set_xlim([0, 50])
        axarr[1].set_ylim([0, max_height])
        axarr[1].get_xaxis().set_ticks([])
        axarr[1].get_yaxis().set_ticks([])

    fig.tight_layout()

    return fig, message, f_name_save
    print "----------------------------"
    data_list = cs_list.get( z )
    file_name = datadir + data_list.get( 'electroatomic_file_path' )
    native_data = Native.ElectronPhotonRelaxationDataContainer( file_name )

    # Select distribution parameters
    for interp in interps:
      print "Interp = ", interp
      print "----------------------------"
      # Plot the given energy
      for energy in energies:

        title = 'Bremsstrahlung Energy Loss CDF at ' + str(energy)
        fig = plt.figure(num=plot_number, figsize=(10,5))
        if show_difference and len( schemes ) == 2:
          ax1 = plt.subplot2grid((2,1),(0, 0), colspan=5)
        else:
          plt.subplot2grid((1,1),(0, 0), colspan=5)
        plt.xlabel('Energy Loss')
        plt.ylabel('CDF')
        plt.title( title )
        # plt.xlim(0.85,1.0)
        # plt.ylim(0.02,0.05)

        # Plot all schemes on one graph
        samples = numpy.zeros(shape=(len( schemes ),len( cdf_values )))
        differences = numpy.zeros(shape=(2,len( cdf_values )))
        scheme_number = 0
        for scheme in schemes:

          # Create the distribution