def make_coefficient_plot(table, positive_words, negative_words, l2_penalty_list):
    cmap_positive = plt.get_cmap('Reds')
    cmap_negative = plt.get_cmap('Blues')

    xx = l2_penalty_list
    plt.plot(xx, [0.]*len(xx), '--', lw=1, color='k')

    table_positive_words = table[positive_words]
    table_negative_words = table[negative_words]
    # table_positive_words.columns = positive_words
    # table_negative_words.columns = negative_words

    print(table_negative_words)
    print(table_positive_words)

    for i in range(len(positive_words)):
        color = cmap_positive(0.8*((i+1)/(len(positive_words)*1.2)+0.15))
        plt.plot(xx, table_positive_words[positive_words[i]].as_matrix().flatten(),
                 '-', label=positive_words[i], linewidth=4.0, color=color)

    for i in range(len(negative_words)):
        color = cmap_negative(0.8*((i+1)/(len(negative_words)*1.2)+0.15))
        plt.plot(xx, table_negative_words[negative_words[i]].as_matrix().flatten(),
                 '-', label=negative_words[i], linewidth=4.0, color=color)

    plt.legend(loc='best', ncol=3, prop={'size':16}, columnspacing=0.5)
    plt.axis([1, 1e5, -1, 2])
    plt.title('Coefficient path')
    plt.xlabel('L2 penalty ($\lambda$)')
    plt.ylabel('Coefficient value')
    plt.xscale('log')
    plt.rcParams.update({'font.size': 18})
    plt.tight_layout()
    plt.show()
Example #2
0
def plot_grid(x, y, colormap=None, plaincolor=None,
              varname=None, var=None, minvar=None, maxvar=None):

# colormap and plaincolor are optional - set defaults
  if colormap == None:
    colormap = 'jet'
  if plaincolor == None:
    plaincolor = 'black'

# Last four parameters optional: if not supplied, plain grid is plotted
  if varname == None:
    print 'Plotting plain grid ...\n'
    colorplot = False
  else:
    print 'Plotting grid colored by ' + varname + ' ...\n'
    colorplot = True

# Determine grid dimensions
  imax = x.shape[0]
  jmax = x.shape[1]

# Initialize plot
  fig = plt.figure()
  ax = fig.add_subplot(111)

# See http://wiki.scipy.org/Cookbook/Matplotlib/MulticoloredLine
# LineCollection type allows color to vary along the line according
#   to a parameter.
  for j in range(0, jmax):
    segments = line_to_segments(x[:,j], y[:,j])
    if colorplot:
      lc = LineCollection(segments, cmap=plt.get_cmap(colormap),
           norm=plt.Normalize(minvar, maxvar))
      lc.set_array(var[:,j])
    else:
      lc = LineCollection(segments, colors=plaincolor)
    ax.add_collection(lc)
  for i in range(0, imax):
    segments = line_to_segments(x[i,:], y[i,:])
    if colorplot:
      lc = LineCollection(segments, cmap=plt.get_cmap(colormap),
           norm=plt.Normalize(minvar, maxvar))
      lc.set_array(var[i,:])
    else:
      lc = LineCollection(segments, colors=plaincolor)
    ax.add_collection(lc)

# Create colorbar and title
  if colorplot:
    faux_colorbar(minvar, maxvar, varname, colormap)
    title = 'Grid geometry colored by ' + varname
  else:
    title = 'Grid geometry'

# Show plot
  plt.title(title)
  plt.xlabel('x')
  plt.ylabel('y')
  plt.axis('equal')
  plt.show(block=False)
Example #3
0
    def subplotter(df_dict,ax,title):
        for i,v in df_dict.iteritems():
            if i == "Past quarters":
                if len(v.columns) > 1:
                    colors = np.r_[np.linspace(0.2, 1, num=len(v.columns))] 
                    cmap = plt.get_cmap("Blues")
                    blueshift = cmap(colors)       
                else:
                    blueshift = (0.81411766,0.88392158,0.94980392)
                v = append_day(v)
                v.plot(ax=ax,color=blueshift,drawstyle='steps-post')
            if i == "Future quarters":
                if len(v.columns) > 1:
                    colors = np.r_[np.linspace(0.2, 1, num=len(v.columns))] 
                    cmap = plt.get_cmap("Reds")
                    redshift = cmap(colors)[::-1]  
                else:
                    redshift = (0.99137255,0.79137256,0.70823531)
                v = append_day(v)
                v.plot(ax=ax,color=redshift,drawstyle='steps-post')
            if i == "Current quarter":
                if v is not None:
                    v = append_day(v)
                    v.plot(ax=ax,color=(0.03137255,0.1882353,0.41960785),drawstyle='steps-post')

        ax.set_ylabel('$/MWh') 
        ax.set_title(title)
        handles, labels = ax.get_legend_handles_labels()
        import operator
        hl = sorted(zip(handles, labels),key=operator.itemgetter(1))
        handles2, labels2 = zip(*hl)
        ax.legend(handles2, labels2,3)
Example #4
0
def show_grid(grid, start, person, maxdiv):
    fig = plt.figure(3)

    vmin = None#np.min(grid)
    vmax = None#np.max(grid)/maxdiv
    cmap_name = 'inferno'

    print(grid.shape, vmin, vmax)

    for i in range(grid.shape[1]):
        print(i, np.sum(grid[:,i,:]))

    for i in range(9):
        a = fig.add_subplot(2,5,i+1)
        plt.imshow(grid[:,start+i,:], cmap=plt.get_cmap(cmap_name), vmin=vmin, vmax=vmax, interpolation='nearest')
        a.set_title(str(start+i))

    fig = plt.figure(30)
    a = fig.add_subplot(1,1,1)
    tmp = np.sum(grid[:,person,:], axis=1)/np.max(grid)

    vmin = np.min(tmp)
    vmax = np.max(tmp)/maxdiv
    print(vmin, vmax)
    plt.imshow(tmp, cmap=plt.get_cmap(cmap_name), vmin=vmin, vmax=vmax, interpolation='nearest')
    a.set_title('cost')
def plot_images_with_probabilities(images, predictiondistribution):
    """ Show images along with their predicted distribution
    INPUT:
        images: the images
        predictiondistribution: the output distribution of the network
    """
    assert images.shape[0] == predictiondistribution.shape[0], "Number of images does not match the amount of prediction labels"
    assert images.shape[0] <= 20, "Can't show more than 20 images"

    fig, axarr = plt.subplots(images.shape[0], 2)
    for i in range(0, images.shape[0], ):
        #Plot the image itself
        ax = axarr[i,0]
        ax.imshow(images[i,:,:],
            cmap=plt.get_cmap("gray_r"),
            interpolation="none")
        ax.axis("off")

        #Plot the probability distribution
        ax = axarr[i,1]
        ax.imshow(np.expand_dims(predictiondistribution[i,:], 0),
            cmap=plt.get_cmap("gray"),
            interpolation="none",
            vmin=0,
            vmax=1)
        ax.axes.get_xaxis().set_ticks(np.arange(10))
        ax.axes.get_yaxis().set_ticks([])

    plt.show()
Example #6
0
    def test_colormap(self):

        # without specifying values but cmap specified -> no uniform color
        # but different colors for all points

        # GeoSeries
        ax = self.points.plot(cmap='RdYlGn')
        cmap = plt.get_cmap('RdYlGn')
        exp_colors = cmap(np.arange(self.N) / (self.N - 1))
        _check_colors(self.N, ax.collections[0].get_facecolors(), exp_colors)

        ax = self.df.plot(cmap='RdYlGn')
        _check_colors(self.N, ax.collections[0].get_facecolors(), exp_colors)

        # # with specifying values -> different colors for all 10 values
        ax = self.df.plot(column='values', cmap='RdYlGn')
        cmap = plt.get_cmap('RdYlGn')
        _check_colors(self.N, ax.collections[0].get_facecolors(), exp_colors)

        # when using a cmap with specified lut -> limited number of different
        # colors
        ax = self.points.plot(cmap=plt.get_cmap('Set1', lut=5))
        cmap = plt.get_cmap('Set1', lut=5)
        exp_colors = cmap(list(range(5))*3)
        _check_colors(self.N, ax.collections[0].get_facecolors(), exp_colors)
def plot_color_gradients(cmap_category, cmap_list):
    nrows = len(cmap_list)
    fig, axes = plt.subplots(nrows=nrows, ncols=2)
    fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99, wspace=0.05)
    fig.suptitle(cmap_category + " colormaps", fontsize=14, y=1.0, x=0.6)

    for ax, name in zip(axes, cmap_list):

        # Get rgb values for colormap
        rgb = cm.get_cmap(plt.get_cmap(name))(x)[np.newaxis, :, :3]

        # Get colormap in CAM02-UCS colorspace. We want the lightness.
        lab = cspace_converter("sRGB1", "CAM02-UCS")(rgb)
        L = lab[0, :, 0]
        L = np.float32(np.vstack((L, L, L)))

        ax[0].imshow(gradient, aspect="auto", cmap=plt.get_cmap(name))
        ax[1].imshow(L, aspect="auto", cmap="binary_r", vmin=0.0, vmax=100.0)
        pos = list(ax[0].get_position().bounds)
        x_text = pos[0] - 0.01
        y_text = pos[1] + pos[3] / 2.0
        fig.text(x_text, y_text, name, va="center", ha="right", fontsize=10)

    # Turn off *all* ticks & spines, not just the ones with colormaps.
    for ax in axes:
        ax[0].set_axis_off()
        ax[1].set_axis_off()
    plt.show()
def demo_right_cbar(fig):
    """
    A grid of 2x2 images. Each row has its own colorbar.
    """

    grid = AxesGrid(F, 122, # similar to subplot(122)
                    nrows_ncols = (2, 2),
                    axes_pad = 0.10,
                    label_mode = "1",
                    share_all = True,
                    cbar_location="right",
                    cbar_mode="edge",
                    cbar_size="7%",
                    cbar_pad="2%",
                    )
    Z, extent = get_demo_image()
    cmaps = [plt.get_cmap("spring"), plt.get_cmap("winter")]
    for i in range(4):
        im = grid[i].imshow(Z, extent=extent, interpolation="nearest",
                cmap=cmaps[i//2])
        if i % 2:
            grid.cbar_axes[i//2].colorbar(im)

    for cax in grid.cbar_axes:
        cax.toggle_label(True)
        cax.axis[cax.orientation].set_label('Foo')

    # This affects all axes because we set share_all = True.
    grid.axes_llc.set_xticks([-2, 0, 2])
    grid.axes_llc.set_yticks([-2, 0, 2])
Example #9
0
    def test_linestrings_values(self):
        from geopandas.plotting import plot_linestring_collection

        fig, ax = plt.subplots()

        # default colormap
        coll = plot_linestring_collection(ax, self.lines, self.values)
        fig.canvas.draw_idle()
        cmap = plt.get_cmap()
        expected_colors = cmap(np.arange(self.N) / (self.N - 1))
        _check_colors(self.N, coll.get_color(), expected_colors)
        ax.cla()

        # specify colormap
        coll = plot_linestring_collection(ax, self.lines, self.values,
                                          cmap='RdBu')
        fig.canvas.draw_idle()
        cmap = plt.get_cmap('RdBu')
        expected_colors = cmap(np.arange(self.N) / (self.N - 1))
        _check_colors(self.N, coll.get_color(), expected_colors)
        ax.cla()

        # specify vmin/vmax
        coll = plot_linestring_collection(ax, self.lines, self.values,
                                          vmin=3, vmax=5)
        fig.canvas.draw_idle()
        cmap = plt.get_cmap()
        expected_colors = cmap([0])
        _check_colors(self.N, coll.get_color(), expected_colors)
        ax.cla()
def make_coefficient_plot(table, positive_words, negative_words, l2_penalty_list):
    cmap_positive = plt.get_cmap('Reds')
    cmap_negative = plt.get_cmap('Blues')

    xx = l2_penalty_list
    plt.plot(xx, [0.]*len(xx), '--', lw=1, color='k')

    table_positive_words = table.filter_by(column_name='word', values=positive_words)
    table_negative_words = table.filter_by(column_name='word', values=negative_words)
    del table_positive_words['word']
    del table_negative_words['word']

    for i in xrange(len(positive_words)):
        color = cmap_positive(0.8*((i+1)/(len(positive_words)*1.2)+0.15))
        plt.plot(xx, table_positive_words[i:i+1].to_numpy().flatten(),
                 '-', label=positive_words[i], linewidth=4.0, color=color)

    for i in xrange(len(negative_words)):
        color = cmap_negative(0.8*((i+1)/(len(negative_words)*1.2)+0.15))
        plt.plot(xx, table_negative_words[i:i+1].to_numpy().flatten(),
                 '-', label=negative_words[i], linewidth=4.0, color=color)

    plt.legend(loc='best', ncol=3, prop={'size':16}, columnspacing=0.5)
    plt.axis([1, 1e5, -1, 2])
    plt.title('Coefficient path')
    plt.xlabel('L2 penalty ($\lambda$)')
    plt.ylabel('Coefficient value')
    plt.xscale('log')
    plt.rcParams.update({'font.size': 18})
    plt.tight_layout()
Example #11
0
def demo_bottom_cbar(fig):
    """
    A grid of 2x2 images with a colorbar for each column.
    """
    grid = AxesGrid(fig, 121, # similar to subplot(132)
                    nrows_ncols = (2, 2),
                    axes_pad = 0.10,
                    share_all=True,
                    label_mode = "1",
                    cbar_location = "bottom",
                    cbar_mode="edge",
                    cbar_pad = 0.25,
                    cbar_size = "15%",
                    direction="column"
                    )

    Z, extent = get_demo_image()
    cmaps = [plt.get_cmap("autumn"), plt.get_cmap("summer")]
    for i in range(4):
        im = grid[i].imshow(Z, extent=extent, interpolation="nearest",
                cmap=cmaps[i//2])
        if i % 2:
            cbar = grid.cbar_axes[i//2].colorbar(im)

    for cax in grid.cbar_axes:
        cax.toggle_label(True)
        cax.axis[cax.orientation].set_label("Bar")

    # This affects all axes as share_all = True.
    grid.axes_llc.set_xticks([-2, 0, 2])
    grid.axes_llc.set_yticks([-2, 0, 2])
Example #12
0
    def heat_map(self, cmap='RdYlGn', vmin=None, vmax=None, font_cmap=None):
        if cmap is None:
            carr = ['#d7191c', '#fdae61', '#ffffff', '#a6d96a', '#1a9641']
            cmap = LinearSegmentedColormap.from_list('default-heatmap', carr)

        if isinstance(cmap, basestring):
            cmap = get_cmap(cmap)
        if isinstance(font_cmap, basestring):
            font_cmap = get_cmap(font_cmap)

        vals = self.actual_values.astype(float)
        if vmin is None:
            vmin = vals.min().min()
        if vmax is None:
            vmax = vals.max().max()
        norm = (vals - vmin) / (vmax - vmin)
        for ridx in range(self.nrows):
            for cidx in range(self.ncols):
                v = norm.iloc[ridx, cidx]
                if np.isnan(v):
                    continue
                color = cmap(v)
                hex = rgb2hex(color)
                styles = {'BACKGROUND': HexColor(hex)}
                if font_cmap is not None:
                    styles['TEXTCOLOR'] = HexColor(rgb2hex(font_cmap(v)))
                self.iloc[ridx, cidx].apply_styles(styles)
        return self
def plot_corrcoef_raftscope(raftsfits, ROIrows, ROIcols, xylabels=None, title='', norm=True):
    """
    Plot of correlation coefficients over list of CCD images.
    :param raftsfits:
    :param ROIrows: must be in the format: slice(start, stop)
    :param ROIcols: must be in the format: slice(start, stop)
    :param norm: if True, computes correlation coefficients; if not, returns covariances
    :return:
    """
    datadir, dataname = os.path.split(raftsfits[0])
    dataname = os.path.splitext(dataname)[0]

    a = corrcoef_raftscope(raftsfits, ROIrows, ROIcols, norm)
    fig, ax = plt.subplots(figsize=(10, 8))
    if norm:
        cax = ax.imshow(a, cmap=plt.get_cmap('jet'), norm=mplcol.Normalize(vmax=1, clip=True), interpolation='nearest')
    else:
        cax = ax.imshow(a, cmap=plt.get_cmap('jet'), norm=mplcol.Normalize(vmax=20000, clip=True), interpolation='nearest')
    if norm:
        titlestr = "Correlation for %s"
    else:
        titlestr = "Covariances for %s"
    if title:
        ax.set_title(titlestr % title)
    else:
        ax.set_title(titlestr % dataname)
    ax.set_xticks(np.arange(0, 16*len(raftsfits), 16))
    ax.set_yticks(np.arange(0, 16*len(raftsfits), 16))
    if xylabels:
        ax.set_xticklabels(xylabels)
        ax.set_yticklabels(xylabels)
    cbar = fig.colorbar(cax, orientation='vertical')

    plt.savefig(os.path.join(datadir, "corrscope-%s.png" % dataname))
    plt.show()
Example #14
0
def plot_color_gradients(cmap_category, cmap_list):
    fig, axes = plt.subplots(nrows=nrows, ncols=2)
    fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99, wspace=0.05)
    fig.suptitle(cmap_category + ' colormaps', fontsize=14, y=1.0, x=0.6)

    for ax, name in zip(axes, cmap_list):

        # Get rgb values for colormap
        rgb = cm.get_cmap(plt.get_cmap(name))(x)[np.newaxis,:,:3]

        # Get colormap in CIE LAB. We want the L here.
        lab = color.rgb2lab(rgb)
        L = lab[0,:,0]
        L = np.float32(np.vstack((L, L, L)))

        ax[0].imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
        ax[1].imshow(L, aspect='auto', cmap='binary_r', vmin=0., vmax=100.)
        pos = list(ax[0].get_position().bounds)
        x_text = pos[0] - 0.01
        y_text = pos[1] + pos[3]/2.
        fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)

    # Turn off *all* ticks & spines, not just the ones with colormaps.
    for ax in axes:
        ax[0].set_axis_off()
        ax[1].set_axis_off()
def main():
    x, y, z = load_data()
    print(z.shape)

    # Fit a 3rd order, 2d polynomial
    m = polyfit2d(x, y, z)

    # Evaluate it on a grid...
    nx, ny = 20, 20
    xx, yy = np.meshgrid(np.linspace(x.min(), x.max(), nx),
                         np.linspace(y.min(), y.max(), ny))
    zz = polyval2d(xx, yy, m)

    # Plot
    plt.imshow(zz,
               origin='lower',
               cmap=plt.get_cmap('hot'),
               extent=[eta_min, eta_max, xi_min, xi_max],
               aspect='auto')
    m = cm.ScalarMappable(cmap=plt.get_cmap('hot'))
    m.set_array(z)
    plt.colorbar(m, label='memory capacity', ticks=[30, 40, 50, 60, 70, 80, 90, 100])
    plt.xlabel(r'$\eta$', size=24)
    plt.ylabel(r'$\xi$', size=24)
    plt.savefig('eta_xi_mc_sampled.png')
    plt.show()
Example #16
0
def imshow(image, colormap='gray', ax=None, fig=None, draw_colorbar=True, block=False):
    """
    Image show
    :param image: image to show
    :param colormap: colormap
    :param ax: axes handle
    :param fig: figure
    :param block: block shell
    :param colorbar: inidicator
    :return: plot
    """
    if ax and fig:
        # For subplots
        img = ax.imshow(image, interpolation="none", cmap=plt.get_cmap(colormap))
        div = make_axes_locatable(ax)
        cax = div.append_axes("right", size="10%", pad=0.05)
        if draw_colorbar:
            plt.colorbar(img, cax=cax)
        plt.show(block=block)
    else:
        # For only one plot, good for interactive mode
        plt.imshow(image, interpolation="none", cmap=plt.get_cmap(colormap))
        if draw_colorbar:
            plt.colorbar()
        plt.show(block=block)
Example #17
0
def plot_data(data,lon_data, lat_data, periodname, AODcatname,maptype,cmapname,minv=0,maxv=0,folder=""):
    fig = plt.figure()
    #ax = fig.add_axes([0.1,0.1,0.8,0.8])

    m = Basemap(llcrnrlon=19,llcrnrlat=34,urcrnrlon=29,urcrnrlat=42,
                resolution='h',projection='cass',lon_0=24,lat_0=38)

    nx = int((m.xmax-m.xmin)/1000.)+1
    ny = int((m.ymax-m.ymin)/1000.)+1
    topodat = m.transform_scalar(data,lon_data,lat_data,nx,ny)
    
    if minv<>0 or maxv<>0 :
        im = m.imshow(topodat,cmap=plt.get_cmap(cmapname),vmin=minv,vmax=maxv)
    else:
        im = m.imshow(topodat,cmap=plt.get_cmap(cmapname))
 

    m.drawcoastlines()
    m.drawmapboundary()
    m.drawcountries()
    m.drawparallels(np.arange(35,42.,1.), labels=[1,0,0,1])
    m.drawmeridians(np.arange(-20.,29.,1.), labels=[1,0,0,1])
    cb = m.colorbar(im,"right", size="5%", pad='2%')
    title=maptype+" AOD "+AODcatname+" "+periodname+" 2007-2014"
    plt.title(title)
    pylab.savefig(folder+maptype+"AOD"+AODcatname+"_"+periodname + ".png")
def auxiliary_2AFC_stimuli():
	x = np.array([np.linspace(-1,1,1000)])
	y = x.copy().T
	n1 = np.random.randint(0,2,(x.size,y.size))
	n2 = np.random.randint(0,2,(x.size,y.size))
	n3 = np.random.randint(0,2,(x.size,y.size))
	freq = 3.*np.pi
	ori = -30.*np.pi/180.
	grating = np.sin(freq*(np.sin(ori)*x+y))
	mask = np.sqrt(x**2+y**2)<=1.
	alpha = 0.15

	target = alpha*grating+(1-alpha)*n1
	distractor = alpha*n2+(1-alpha)*n3
	target[np.logical_not(mask)] = np.nan
	distractor[np.logical_not(mask)] = np.nan

	plt.figure()
	plt.imshow(target,cmap=plt.get_cmap('gray'))
	plt.gca().set_axis_off()
	plt.savefig('../../figs/2AFC_target.png',bbox_inches='tight',dpi=200, transparent=True, pad_inches=0.)
	plt.figure()
	plt.imshow(distractor,cmap=plt.get_cmap('gray'))
	plt.gca().set_axis_off()
	plt.savefig('../../figs/2AFC_distractor.png',bbox_inches='tight',dpi=200, transparent=True, pad_inches=0.)
Example #19
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 #20
0
 def update_plots(framenum, data, plot, titles=kwargs.get('titles')):
     # fig.clear()
     # ax1 = fig.add_subplot(1, 2, 1, projection='3d')
     # ax2 = fig.add_subplot(1, 2, 2, projection='3d')
     ax1.clear(), ax2.clear()
     for points,dat in zip(pltpoints1, data[0][framenum]):
         plot1 = ax1.plot_trisurf(points[:,0], points[:,1], dat, linewidth=0, 
                             cmap=plt.get_cmap('jet'), vmin=zmin1, vmax=zmax1)
     for points,dat in zip(pltpoints2, data[1][framenum]):
         plot2 = ax2.plot_trisurf(points[:,0], points[:,1], dat, linewidth=0, 
                             cmap=plt.get_cmap('jet'), vmin=zmin2, vmax=zmax2)
     
     for ax,bbox,zmax,title in zip([ax1,ax2], [bbox1,bbox2], [np.maximum(zmax1,zmax2)]*2, titles):
         # Setting the axes properties
         ax.set_xlim3d(bbox.bounds[0])
         ax.set_xlabel('$s_1$', fontsize=14)
         ax.set_ylim3d(bbox.bounds[1])
         ax.set_ylabel('$s_2$', fontsize=14)
         ax.set_zlabel('$x(s)$', fontsize=14)
         ax.tick_params(labelsize=8)
         ax.set_zlim3d([-0.05, zmax])
         ax.set_zlim3d([-0.05, zmax])
         ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
         ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
         ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
         ax.set_title(title, fontsize=14)
         ax.view_init(elev=kwargs.get('elev'), azim=kwargs.get('azim'))
     plt.tight_layout()
     return [plot1, plot2]
Example #21
0
def heat_equation_plot(t0, t1, dt, n, m, u, f, nu, solver_func=heat_equation, verbose=False, save_to=None, time=False):
    """
    Solves and plots heat equation where t0 is start time, t1 is end time,
    dt is time step, n is rectangle is rectangle width, m is rectangle height,
    u are the initial values (as a n x m list), f is the heat source function
    (also a n x m list), nu is thermal diffusivity, solver_func is the
    solver function you want to use, verbose gives verbose output,
    save_to is a file handle to save the file to. Returns the solved
    heat_equation.

    Note that this function is destructive and will manipulate the given u
    argument.
    """
    if verbose:
        print "Entering plotting function"
    # Init subplots and create t0 plot.
    fig, axes = plt.subplots(nrows=1, ncols=2)
    axes[0].imshow(u, plt.get_cmap("gray"))

    # Call the given solver function, this lets us plot with other solvers.
    u = solver_func(t0, t1, dt, n, m, u, f, nu, verbose)

    # Plot t1 plot and colorbar.
    im = axes[1].imshow(u, plt.get_cmap("gray"))
    plt.colorbar(im, ax=axes.ravel().tolist())

    if save_to:
        if verbose:
            print "Saving figure to {}".format(save_to.name)
        plt.savefig(save_to)

    plt.show()

    # Return u so UI can save.
    return u
def plot_n_cumregrets(cum_regs, alg_names, players, axis=''):
	cm = plt.get_cmap('gist_rainbow')
	fig = plt.figure()
	ax = fig.add_subplot(111)

	NUM_COLORS = len(alg_names)
	
	cm = plt.get_cmap('gist_rainbow')
	cNorm  = colors.Normalize(vmin=0, vmax=NUM_COLORS-1)
	scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=cm)
	ax.set_prop_cycle(cycler('color', [scalarMap.to_rgba(i) for i in range(NUM_COLORS)]))
	
	
	for i in range(len(alg_names)):
		if(axis == 'loglog'):
			plt.loglog(players, cum_regs[:,i], label=alg_names[i])
		else:
			plt.plot(players, cum_regs[:,i], label=alg_names[i])
	plt.xlabel('Number of players')
	plt.ylabel('Average Cumulative Regret')
	plt.legend(loc='upper left')
	plt.title('Cumulative Regret')
	if axis == 'log':
		ax.set_xscale('log')
	
	# Shrink current axis by 20%
	box = ax.get_position()
	ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

	# Put a legend to the right of the current axis
	ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
	filename = 	'nplayer_figures/cumregret_'+ str(axis)+'.png'
	plt.savefig(filename,bbox_inches='tight')
	#plt.show()
	plt.close()
Example #23
0
def Plot_Gen_Mass_Density(data_file='filtered_data'):
    '''
    Create a interpolated plot where the colors of the line
    correspond to the mass transfer rate of the data

    Parameters
    ----------
    data_file : str
        file to be used to generate the  plot
    '''

    try:
        filtered_results = np.loadtxt(data_file)
    except:
        root_dir = raw_input("file not found, select directory to generate: ")
        filtered_results = mesa_calc.Gen_Filtered_Data_File(root_dir=root_dir)

    filtered_period = filtered_results[:, 1]
    filtered_mtransfer = filtered_results[:, 3]
    filtered_mass1 = filtered_results[:, 4]
    filtered_run_num = filtered_results[:, 13]

    split_ind = np.where(filtered_run_num != 0)[0]
    split_mass1 = np.split(filtered_mass1, split_ind)
    split_period = np.split(filtered_period, split_ind)
    split_mtransfer = np.split(filtered_mtransfer, split_ind)

    plt.figure(1, figsize=(24, 13.5))  # 1920x1080
    plt.clf()

    if xrange(len(split_mass1)):
        for sys_number in xrange(len(split_mass1)):
            Plot_Color_Line(split_mass1[sys_number],
                            split_period[sys_number],
                            z=split_mtransfer[sys_number],
                            cmap=plt.get_cmap('jet'),
                            norm=plt.Normalize(-12, max(filtered_mtransfer)))
    else:
        Plot_Color_Line(split_mass1[0],
                        split_period[0],
                        z=split_mtransfer[0],
                        cmap=plt.get_cmap('jet'),
                        norm=plt.Normalize(-12, max(filtered_mtransfer)))

    sm = plt.cm.ScalarMappable(cmap=plt.get_cmap('jet'),
                               norm=plt.Normalize(-12, max(filtered_mtransfer)))
    sm._A = []
    cb = plt.colorbar(sm)
    cb.set_label(r'log$(\dot{M})$', size=20)

    plt.xlabel(r'Donor Mass $(\dot{M_\odot})$', fontsize=20)
    plt.ylabel(r'Period log(days)', fontsize=20)
    plt.xlim(min(filtered_mass1) - 0.2, max(filtered_mass1) + 0.2)
    plt.ylim(min(filtered_period) - 0.2, max(filtered_period) + 0.2)
    plt.tick_params(axis='both', which='major', labelsize=20)
    plt.grid()

    figname = 'dt_mass_period'
    plt.savefig(figname)
    plt.show()
Example #24
0
def DisplayFrames(video):
    num_rows = np.shape(video)[0]
    num_cols = np.shape(video)[1]
    num_frames = np.shape(video)[2]
    
    f = 0
    plt.imshow(video[:,:,f], cmap=plt.get_cmap('gray'))
    plt.show(block=False)
    while(True):
        f = 0
        print("\033[A                                           \033[A")
        x = input("Press f: forward, b: back, q: quit  :  ")
        if (x == "f"):
            if ((f+1) < num_frames):
                f = f+1
        elif (x == "b"):
            if ((f-1) >= 0):
                f = f-1
        elif (x == "q"):
            break
        else:
            f = f

        plt.imshow(video[:,:,f], cmap=plt.get_cmap('gray'))
        plt.show(block=False)
Example #25
0
def plot_coo_matrix(m):
    if not isinstance(m, coo_matrix):
        m = coo_matrix(m)
    fig = plt.figure()
    ax = fig.add_subplot(111, axisbg='black')
    #print m.data
    #print len(m.col), len(m.row), len(m.data)

    cdata = array2cmap(m.data, -10, 10)
    cmhot = plt.get_cmap("hot")
    dgray = plt.get_cmap('gray') 
    #cnorm  = colors.Normalize(vmin=-10, vmax=10)

    #ax.scatter(m.col, m.row, c=m.data, cmap=cmhot)
    ax.plot(m.col, m.row, 's', c="white", ms=1)
    ax.set_xlim(0, m.shape[1])
    ax.set_ylim(0, m.shape[0])
    ax.set_aspect('equal')
    for spine in ax.spines.values():
        spine.set_visible(False)
    ax.invert_yaxis()
    ax.set_aspect('equal')
    ax.set_xticks([])
    ax.set_yticks([])
    return ax
Example #26
0
    def gradient(figure_object, axis_object, xs, ys, start_year, TWP_length, cmap, key_count):
        """Based on http://matplotlib.org/examples/pylab_examples/multicolored_line.html
        and http://stackoverflow.com/questions/19132402/set-a-colormap-under-a-graph
        """
        from matplotlib.collections import LineCollection

        # plot a color_map line fading to white
        points = np.array([xs, ys]).T.reshape(-1, 1, 2)
        segments = np.concatenate([points[:-1], points[1:]], axis=1)
        lc = LineCollection(segments, cmap=plt.get_cmap('gray'), norm=plt.Normalize(start_year, start_year+TWP_length),
                            linewidth=0.2, zorder=1)   # norm sets the color min:max range
        lc.set_array(np.array(xs))
        axis_object.add_collection(lc)

        # add fading color_map fill as well
        xs.append(max(xs))
        xs.append(min(xs))
        ys.append(0)
        ys.append(0)
        poly, = axis_object.fill(xs, ys, facecolor='none', edgecolor='none')
        img_data = np.arange(0, 100, 1)
        img_data = img_data.reshape(1, img_data.size)
        im = axis_object.imshow(img_data, aspect='auto', origin='lower', cmap=plt.get_cmap(cmap),
                                extent=[start_year+TWP_length, start_year, 1000, -1000], vmin=0., vmax=100., zorder=-(start_year+1)*key_count)
        im.set_clip_path(poly)
Example #27
0
    def _plot_2D(self, ax=None, plot_3D=True, **kwargs):
        fig = plt.gcf()
        if ax is None:
            if plot_3D:
                print HEEEEEEE
                ax = fig.add_subplot(111, projection='3d')
            else:
                print HEYEHYE
                ax = fig.add_subplot(111)

        if hasattr(self,'probs'):
            del self.probs
        if not hasattr(self.softmax_collection, 'probs'):
            self.softmax_collection.probability()

        X = self.softmax_collection.X
        Y = self.softmax_collection.Y
        Z = self.softmax_collection.probs[:, self.id].reshape(X.shape[0], X.shape[1])
        bounds = self.softmax_collection.bounds

        if plot_3D:
            ax.plot_surface(X, Y, Z, cstride=2, rstride=2, linewidth=0,
                            antialiased=False, cmap=plt.get_cmap(self.cmap))

            ax.set_zlabel('Probability P(D=i|X)')
        else:
            levels = np.linspace(0, np.max(Z), 50)
            ax.contourf(X, Y, Z, levels=levels, cmap=plt.get_cmap(self.cmap),
                        alpha=0.8)

        ax.set_xlim(bounds[0], bounds[2])
        ax.set_ylim(bounds[1], bounds[3])
        ax.set_xlabel('x')
        ax.set_ylabel('y')
        ax.set_title('Class Probabilities')
Example #28
0
File: table.py Project: vanife/tia
    def heat_map(self, cmap="RdYlGn", vmin=None, vmax=None, font_cmap=None):
        if cmap is None:
            carr = ["#d7191c", "#fdae61", "#ffffff", "#a6d96a", "#1a9641"]
            cmap = LinearSegmentedColormap.from_list("default-heatmap", carr)

        if isinstance(cmap, str):
            cmap = get_cmap(cmap)
        if isinstance(font_cmap, str):
            font_cmap = get_cmap(font_cmap)

        vals = self.actual_values.astype(float)
        if vmin is None:
            vmin = vals.min().min()
        if vmax is None:
            vmax = vals.max().max()
        norm = (vals - vmin) / (vmax - vmin)
        for ridx in range(self.nrows):
            for cidx in range(self.ncols):
                v = norm.iloc[ridx, cidx]
                if np.isnan(v):
                    continue
                color = cmap(v)
                hex = rgb2hex(color)
                styles = {"BACKGROUND": HexColor(hex)}
                if font_cmap is not None:
                    styles["TEXTCOLOR"] = HexColor(rgb2hex(font_cmap(v)))
                self.iloc[ridx, cidx].apply_styles(styles)
        return self
Example #29
0
def createTrainingData(file = '/Users/oli/Proj_Large_Data/Deep_Learning_MRI/BRATS-2/brats2.h5', show=True):
    lnum = 0
    with h5py.File(file, 'r') as f:
        for name in f:
            t1c = np.asarray(f[name + '/' + 'VSD.Brain.XX.O.MR_T1c'])
            pred = np.asarray(f[name + '/' + 'VSD.Brain_3more.XX.XX.OT'])
            if show:
                fig = plt.figure()
                plt.title(name)
                plt.xticks([])
                plt.yticks([])
                plt.subplots_adjust(hspace=1e-3, wspace=1e-3)
            for i, z in enumerate(range(65, 145, 5)):
                tc1s = (np.array(t1c[z, 20:180, 0:160], dtype='float32')).reshape(1,1,160,160)
                preds = (np.array(pred[z, 20:180, 0:160], dtype='uint8')).reshape(1,1,160,160)
                if (lnum == 0):
                    X = tc1s
                    Y = preds
                else:
                    X = np.vstack((X, tc1s))
                    Y = np.vstack((Y, preds))
                if show:
                    a = fig.add_subplot(6, 6, (2 * i + 1), xticks=[], yticks=[])  # NB the one based API sucks!
                    plt.imshow(X[lnum,0,:,:], cmap=plt.get_cmap('gray'))
                    a = fig.add_subplot(6, 6, (2 * i + 2), xticks=[], yticks=[])  # NB the one based API sucks!
                    plt.imshow(Y[lnum,0,:,:], cmap=plt.get_cmap('gray'))
                lnum += 1
            if show:
                plt.pause(1)
    return X,Y
Example #30
0
def plot_color_gradients(nrows, data, cmplists, names):
    fig, axes = plt.subplots(nrows+1)
    fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99)
    axes[0].set_title('weights color_maps', fontsize=14)
    i3 = -1
    for name, color in zip(names, cmplists):
        if name != 'ColorMap':
            i3 += 1
            for j in xrange(4):
                temp = np.vstack((data[i3, j, :], data[i3, j, :]))
                axes[i3*4+j].imshow(temp, aspect='auto', cmap=plt.get_cmap(color))
                pos = list(axes[i3*4+j].get_position().bounds)
                x_text = pos[0] - 0.01
                y_text = pos[1] + pos[3]/2.
                fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)
        else:
            indexes = np.linspace(0, 1, 256)
            gradient = np.vstack((indexes, indexes, indexes, indexes))
            axes[-1].imshow(gradient, aspect='auto', cmap=plt.get_cmap(color))
            pos = list(axes[-1].get_position().bounds)
            x_text = pos[0] - 0.01
            y_text = pos[1] + pos[3]/2.
            fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)
    for ax in axes:
        ax.set_axis_off()
Example #31
0
print('我们有' + str(test_images.shape[0]) + '个测试集数据,每个数字由' +
      str(test_images.shape[1]) + '个像素组成,标签有' + str(test_labels.shape[1]) +
      '种类型。')
print('验证集数字的形状 :', mnist.validation.images.shape)
print('验证集标签的形状 :', mnist.validation.labels.shape)
print('我们有' + str(validation_images.shape[0]) + '个验证集数据,每个数字由' +
      str(validation_images.shape[1]) + '个像素组成,标签有' +
      str(validation_labels.shape[1]) + '种类型。')

#查看前25个数据并保存
for i in range(25):
    image_array = train_images[i, :]
    image_array_labels = np.argmax(train_labels[i, :])
    image_array = image_array.reshape(28, 28)
    plt.subplot(5, 5, i + 1)
    plt.imshow(image_array, cmap=plt.get_cmap('gray'))
    plt.title(image_array_labels)
    print(image_array_labels)
    #保存图片,需要先自己创建桌面文件夹C:/Users/nhb\Desktop/MNISTset/pic
    label = str(image_array_labels)
    ind = str(i)
    filename = 'mnist_train_' + ind + '_' + label + '.jpg'
    print(filename)
    filepath = "C:/Users/nhb\Desktop/MNISTset/pic"
    imsave(filepath + "/" + filename, image_array, cmap='gray')

#003创建模型
#创建一个空的容器,Sequential,往里面添加各个层
model = Sequential()
#将数据形状变为-1,28,28,1,其中-1是指当不知道有多少个数据输入时,可以用-1代替,
#28*28像素的图片,单通道的灰度图
Example #32
0
    plt.xlabel('Phase Difference')
    plt.title(str(AM[j]))

    AM_med[:, j] = np.median(accuracy_conds, axis=1)
    AM_avgs[:, j] = accuracy_conds.mean(axis=1)
    AM_mad_sem[:, j] = spst.median_absolute_deviation(
        accuracy_conds, axis=1, scale=1.4826) / np.sqrt(
            accuracy_conds.shape[1])

    # np.sum(np.abs(accuracy_conds -
    #                             np.repeat(np.reshape(accuracy_conds.mean(axis=1),[4,1]),15,axis=1)
    #                             [0,:]),axis=1)/accuracy_conds.shape[1]
    AM_sems[:, j] = sem
    #AM_rav[:,j] = accuracy_conds[:,0]

cmap = plt.get_cmap('hot')
cmap_colors = cmap(np.linspace(0, 0.8, len(AM)))
fig, ax = plt.subplots()
for n in range(0, len(AM)):
    ax.plot(range(len(phi_conds)), AM_avgs[:, n], color=cmap_colors[n, :])
    ax.errorbar(range(len(phi_conds)),
                AM_avgs[:, n],
                yerr=AM_sems[:, n],
                color=cmap_colors[n, :],
                linewidth=2)
plt.xticks(range(len(phi_conds)), labels=phi_conds)
plt.ylim((0.2, 1))
plt.ylabel('Accuracy')
plt.xlabel('Phase Difference')
plt.title('FMphi')
plt.legend(AM)
Example #33
0
gam = np.stack((era5_gam, ukmo_gam, ncep_gam, ecmwf_gam))
tci = np.stack((era5_tci, ukmo_tci, ncep_tci, ecmwf_tci))
tet = np.stack((era5_tet, ukmo_tet, ncep_tet, ecmwf_tet))
pleg = np.stack((era5_pleg, ukmo_pleg, ncep_pleg, ecmwf_pleg))
tleg = np.stack((era5_tleg, ukmo_tleg, ncep_tleg, ecmwf_tleg))

lon = np.arange(lonlim[0], lonlim[1] + 0.5, 1.5)  #longitude array
lat = np.arange(latlim[0], latlim[1] + 0.5, 1.5)  #latitde array
lw = 1  #linewidth
gl = 20  #lat lon skips for drawing on the map
latlim = [-50, 15]  #latitude limits
lonlim = [-90, -30]  #longitude limits
data = ['ERA5', 'UKMO', 'NCEP', 'ECMWF']  #dataset titles

#Zeng's gamma plot
cmap = plt.get_cmap('RdBu')
cmap.set_bad(color='0.75', alpha=1.)
cmin = -0.2
cmax = 0.2
cspc = 0.01
cstp = 0.1
clevs = np.arange(cmin, cmax + cspc, cspc)
label = 'Gamma'
title = 'PR-ET'
units = ''
clabel = label + ' ' + title + ' ' + units
norm = BoundaryNorm(boundaries=clevs, ncolors=256)
fig = plt.figure(figsize=(12, 9))
ax = plt.subplot(4, 5, 3)
mymap = Basemap(projection='cyl',
                resolution='l',
Example #34
0
            core=core,
            filters=filters[core],
            kernel_size=argv.ksize,
            dropout=argv.dropout,
        )
        model.compile(
            run_eagerly=False,
            optimizer=keras.optimizers.Adam(lr=argv.lr, clipnorm=argv.clip),
            loss='mse',
        )
        model.summary()
        histories[core] = model.fit(
            train_dataset,
            epochs=argv.epochs,
            validation_data=test_dataset,
        ).history

    interval = np.linspace(0, argv.epochs, argv.epochs)
    cmap = pyplot.get_cmap('viridis')
    colors = cmap(np.linspace(0, 1, len(histories.items())))

    for (core, m), color in zip(histories.items(), colors):
        pyplot.plot(interval, m['loss'], color=color, label=core)

    pyplot.title('Adding problem')
    pyplot.xlabel('Epoch')
    pyplot.ylabel('Loss')
    pyplot.legend()
    pyplot.savefig('loss.png')
    pyplot.close()
Example #35
0
    def draw_figure(self,
                    data,
                    image_extent,
                    scan_axis=None,
                    cbar_range=None,
                    percentile_range=None,
                    crosshair_pos=None):
        """ Create a 2-D color map figure of the scan image.

        @param: array data: The NxM array of count values from a scan with NxM pixels.

        @param: list image_extent: The scan range in the form [hor_min, hor_max, ver_min, ver_max]

        @param: list axes: Names of the horizontal and vertical axes in the image

        @param: list cbar_range: (optional) [color_scale_min, color_scale_max].  If not supplied then a default of
                                 data_min to data_max will be used.

        @param: list percentile_range: (optional) Percentile range of the chosen cbar_range.

        @param: list crosshair_pos: (optional) crosshair position as [hor, vert] in the chosen image axes.

        @return: fig fig: a matplotlib figure object to be saved to file.
        """
        if scan_axis is None:
            scan_axis = ['X', 'Y']

        # If no colorbar range was given, take full range of data
        if cbar_range is None:
            cbar_range = [np.min(data), np.max(data)]

        # Scale color values using SI prefix
        prefix = ['', 'k', 'M', 'G']
        prefix_count = 0
        image_data = data
        draw_cb_range = np.array(cbar_range)
        image_dimension = image_extent.copy()

        while draw_cb_range[1] > 1000:
            image_data = image_data / 1000
            draw_cb_range = draw_cb_range / 1000
            prefix_count = prefix_count + 1

        c_prefix = prefix[prefix_count]

        # Scale axes values using SI prefix
        axes_prefix = ['', 'm', r'$\mathrm{\mu}$', 'n']
        x_prefix_count = 0
        y_prefix_count = 0

        while np.abs(image_dimension[1] - image_dimension[0]) < 1:
            image_dimension[0] = image_dimension[0] * 1000.
            image_dimension[1] = image_dimension[1] * 1000.
            x_prefix_count = x_prefix_count + 1

        while np.abs(image_dimension[3] - image_dimension[2]) < 1:
            image_dimension[2] = image_dimension[2] * 1000.
            image_dimension[3] = image_dimension[3] * 1000.
            y_prefix_count = y_prefix_count + 1

        x_prefix = axes_prefix[x_prefix_count]
        y_prefix = axes_prefix[y_prefix_count]

        # Use qudi style
        plt.style.use(self._save_logic.mpl_qd_style)

        # Create figure
        fig, ax = plt.subplots()

        # Create image plot
        cfimage = ax.imshow(
            image_data,
            # reference the right place in qd
            cmap=plt.get_cmap('inferno'),
            origin="lower",
            vmin=draw_cb_range[0],
            vmax=draw_cb_range[1],
            interpolation='none',
            extent=image_dimension)

        ax.set_aspect(1)
        ax.set_xlabel(scan_axis[0] + ' position (' + x_prefix + 'm)')
        ax.set_ylabel(scan_axis[1] + ' position (' + y_prefix + 'm)')
        ax.spines['bottom'].set_position(('outward', 10))
        ax.spines['left'].set_position(('outward', 10))
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.get_xaxis().tick_bottom()
        ax.get_yaxis().tick_left()

        # draw the crosshair position if defined
        if crosshair_pos is not None:
            trans_xmark = mpl.transforms.blended_transform_factory(
                ax.transData, ax.transAxes)

            trans_ymark = mpl.transforms.blended_transform_factory(
                ax.transAxes, ax.transData)

            ax.annotate(
                '',
                xy=(crosshair_pos[0] * np.power(1000, x_prefix_count), 0),
                xytext=(crosshair_pos[0] * np.power(1000, x_prefix_count),
                        -0.01),
                xycoords=trans_xmark,
                arrowprops=dict(facecolor='#17becf', shrink=0.05),
            )

            ax.annotate(
                '',
                xy=(0, crosshair_pos[1] * np.power(1000, y_prefix_count)),
                xytext=(-0.01,
                        crosshair_pos[1] * np.power(1000, y_prefix_count)),
                xycoords=trans_ymark,
                arrowprops=dict(facecolor='#17becf', shrink=0.05),
            )

        # Draw the colorbar
        # , fraction=0.046, pad=0.08, shrink=0.75)
        cbar = plt.colorbar(cfimage, shrink=0.8)
        cbar.set_label('Fluorescence (' + c_prefix + 'c/s)')

        # remove ticks from colorbar for cleaner image
        cbar.ax.tick_params(which=u'both', length=0)

        # If we have percentile information, draw that to the figure
        if percentile_range is not None:
            cbar.ax.annotate(str(percentile_range[0]),
                             xy=(-0.3, 0.0),
                             xycoords='axes fraction',
                             horizontalalignment='right',
                             verticalalignment='center',
                             rotation=90)
            cbar.ax.annotate(str(percentile_range[1]),
                             xy=(-0.3, 1.0),
                             xycoords='axes fraction',
                             horizontalalignment='right',
                             verticalalignment='center',
                             rotation=90)
            cbar.ax.annotate('(percentile)',
                             xy=(-0.3, 0.5),
                             xycoords='axes fraction',
                             horizontalalignment='right',
                             verticalalignment='center',
                             rotation=90)
        return fig
Example #36
0
def display_img(im,
                beamparams=None,
                scale='linear',
                gamma=0.5,
                cbar_lims=False,
                has_cbar=True,
                has_title=True,
                cfun='afmhot',
                axis=False,
                show=False,
                fontsize=FONTSIZE):
    """display the figure on a given axis
       cannot use im.display because  it makes a new figure
    """

    interp = 'gaussian'

    if axis:
        ax = axis
    else:
        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1)

    imvec = np.array(im.imvec).reshape(-1)
    #flux unit is mJy/uas^2
    imvec = imvec * 1.e3
    fovfactor = im.xdim * im.psize * (1 / RADPERUAS)
    factor = (1. / fovfactor)**2 / (1. / im.xdim)**2
    imvec = imvec * factor

    imarr = (imvec).reshape(im.ydim, im.xdim)
    unit = 'mJy/$\mu$ as$^2$'
    if scale == 'log':
        if (imarr < 0.0).any():
            print('clipping values less than 0')
            imarr[imarr < 0.0] = 0.0
        imarr = np.log(imarr + np.max(imarr) / dynamic_range)
        unit = 'log(' + unit + ')'

    if scale == 'gamma':
        if (imarr < 0.0).any():
            print('clipping values less than 0')
            imarr[imarr < 0.0] = 0.0
        imarr = (imarr + np.max(imarr) / dynamic_range)**(gamma)
        unit = '(' + unit + ')^gamma'

    if cbar_lims:
        imarr[imarr > cbar_lims[1]] = cbar_lims[1]
        imarr[imarr < cbar_lims[0]] = cbar_lims[0]

    if cbar_lims:
        ax = ax.imshow(imarr,
                       cmap=plt.get_cmap(cfun),
                       interpolation=interp,
                       vmin=cbar_lims[0],
                       vmax=cbar_lims[1])
    else:
        ax = ax.imshow(imarr, cmap=plt.get_cmap(cfun), interpolation=interp)

    if has_cbar:
        cbar = plt.colorbar(ax, fraction=0.046, pad=0.04, format='%1.2g')
        cbar.set_label(unit, fontsize=fontsize)
        cbar.ax.xaxis.set_label_position('top')
        cbar.ax.tick_params(labelsize=16)
        if cbar_lims:
            plt.clim(cbar_lims[0], cbar_lims[1])

    if not (beamparams is None):
        beamparams = [
            beamparams[0], beamparams[1], beamparams[2], -.35 * im.fovx(),
            -.35 * im.fovy()
        ]
        beamimage = im.copy()
        beamimage.imvec *= 0
        beamimage = beamimage.add_gauss(1, beamparams)
        halflevel = 0.5 * np.max(beamimage.imvec)
        beamimarr = (beamimage.imvec).reshape(beamimage.ydim, beamimage.xdim)
        plt.contour(beamimarr, levels=[halflevel], colors='w', linewidths=3)
        ax = plt.gca()

    plt.axis('off')
    fov_uas = im.xdim * im.psize / RADPERUAS  # get the fov in uas
    roughfactor = 1. / 3.  # make the bar about 1/3 the fov
    fov_scale = 40
    #fov_scale = int( math.ceil(fov_uas * roughfactor / 10.0 ) ) * 10 # round around 1/3 the fov to nearest 10
    start = im.xdim * roughfactor / 3.0  # select the start location
    end = start + fov_scale / fov_uas * im.xdim  # determine the end location based on the size of the bar
    plt.plot([start, end], [im.ydim - start, im.ydim - start],
             color="white",
             lw=1)  # plot line
    plt.text(x=(start + end) / 2.0,
             y=im.ydim - start - im.ydim / 20,
             s=str(fov_scale) + " $\mu$as",
             color="white",
             ha="center",
             va="center",
             fontsize=int(1.2 * fontsize),
             fontweight='bold')

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

    if show:
        plt.show(block=False)

    return ax
Example #37
0
from magine.data.tools import log2_normalize_df

fold_change = 'fold_change'
flag = 'significant'
exp_method = 'source'
p_val = 'p_value'
rna = 'rna_seq'
gene = 'gene'
protein = 'protein'
metabolites = 'metabolites'
species_type = 'species_type'
sample_id = 'sample_id'
identifier = 'identifier'
label_col = 'label'

cm = plt.get_cmap('jet')


def write_table_to_html(data, save_name='index', out_dir=None,
                        run_parallel=False, exp_data=None,
                        plot_type='matplotlib'):
    """
    Creates a html table  of plots of genes for each ontology term.

    Parameters
    ----------
    data : magine.enrichment.enrichment_result.EnrichmentResult
    save_name : str
        name of html output file
    out_dir : str, optional
        output path for all plots
Example #38
0
    'lib.common',
    'lib.yeyuc_logging',
    'lib.yeyuc_keras',
    'lib.yeyuc_matplotlib',
    'lib.yeyuc_mongo',
    'lib.yeyuc_multicore',
    'lib.yeyuc_mysql',
    'lib.yeyuc_networkx',
    'lib.yeyuc_read',
    'lib.yeyuc_sklearn',
    'lib.yeyuc_spider',
    'lib.yeyuc_write',
]

import matplotlib.pyplot as plt

COLOR_DICT = {
    'tab20c': plt.get_cmap('tab20c'),
}

FONT_DICT = {
    'font': {'family': 'Times New Roman', 'weight': 'normal', 'size': 16},
    'title': {'family': 'Times New Roman', 'weight': 'normal', 'size': 16},
    'axis': {'family': 'Times New Roman', 'weight': 'normal', 'size': 13},
    'legend': {'family': 'Times New Roman', 'weight': 'normal', 'size': 10},
    'sub_font': {'family': 'Times New Roman', 'weight': 'normal', 'size': 14},
    'sub_title': {'family': 'Times New Roman', 'weight': 'normal', 'size': 14},
    'sub_axis': {'family': 'Times New Roman', 'weight': 'normal', 'size': 11},
    'sub_legend': {'family': 'Times New Roman', 'weight': 'normal', 'size': 8},
}
print(ds)

X = ds.iloc[:,0]
y = ds.iloc[:,1]
n = len(X)

#X, y = datasets.make_regression(n_samples = 100, n_features=1, noise=20, random_state=4)
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size =0.2, random_state = 1234)

fig = plt.figure(figsize=(8,6))
plt.scatter(X, y, color="b", marker="o", s = 30)
plt.show()

reg = LinearRegression(lr=0.01)
reg.fit(X_train, y_train, n)
predicted = reg.predict(X_test)

def mse(y_true, y_predicted):
    return np.mean(y_true - y_predicted**2)

mse_value = mse(y_test, predicted)
print(mse_value)

y_predic_line = reg.predict(X)
cmap = plt.get_cmap('viridis')
fig = plt.figure(figsize=(8,6))
m1= plt.scatter(X_train, y_train, color=cmap(0.9), s=10)
m1= plt.scatter(X_test, y_test, color=cmap(0.5), s=10)
plt.plot(X,y_predic_line, color='black', linewidth=2, label='prediction')
plt.show()
            'FinTrimestre', 'Nb_Inst', 'P_MW', 'GeoShape', 'GeoPoint'
        ]
    dep_parc_prod.columns = cols
    code = {
        'a-]0;36]': 'home_rooftop',
        'b-]36;100]': 'commercial_rooftop',
        'c-]100;250]': 'commercial_rooftop',
        'd-]250;...[': 'solar_farm'
    }
    dep_parc_prod['TYPE_PV'] = dep_parc_prod.TRANCHE.apply(lambda x: code[x])

dep_parc_prod = dep_parc_prod[dep_parc_prod.TYPE_PROD == 'Photovoltaïque']

#%% plot PV installed per departement
pv_inst = dep_parc_prod.groupby('Code Département')['Puissance MW'].sum()
cmap = plt.get_cmap('plasma')
polys = util.list_polygons(util.do_polygons(dep_polys, plot=False),
                           dep_polys.index)
color = cmap([
    pv_inst[d] / pv_inst.max() for d in pv_inst.index
    for p in dep_polys.Polygon[d]
])

ax = util.plot_polygons(polys, color=color)
tranches = np.arange(0, 7, 1) * 100
labels = ['{:3} MW'.format(t) for t in tranches]
palette = list(cmap([i / 600 for i in tranches]))
util.aspect_carte_france(ax,
                         title='PV Installed capacity',
                         palette=palette,
                         labels=labels)
Example #41
0
def Main():
    if len(sys.argv) == 1: show_help()

    opts,restlist = getopt(sys.argv[1:],"m:oht:d",\
                        ["matrix=","threshold=","help","direct"])
    threshold = 0.5
    direct = False
    for o, a in opts:
        if o in ("-m", "--matrix"): M = a
        if o in ("-h", "--help"): show_help()
        if o in ("-t", "--threshold"): threshold = float(a)
        if o in ("-d", "--direct"): direct = True
    if not 'M' in dir():
        show_help()
    try:
        f = open(M)
    except:
        print >> sys.stderr, "Can't open file", M
        show_help()

    max = 0
    nodes = []
    edges = []
    pos = {}
    edges_col = []
    col = {}

    rank = {}
    lines = f.readlines()
    i = 0
    maxcols = 0
    for line in lines:
        line = line.strip()
        if line[0] == "#": continue
        a = line.split("\t")
        nodes.append(a[0])
        rank[a[0]] = 0
        for k, x in enumerate(a[1:]):
            if k == i: continue
            x = float(x)
            if x > threshold or x < -threshold:
                if k < len(nodes) and rank[a[0]] < rank[nodes[k]] + 1:
                    rank[a[0]] = rank[nodes[k]] + 1
        if col.has_key(rank[a[0]]):
            col[rank[a[0]]] += 1
        else:
            col[rank[a[0]]] = 1
        #pos[a[0]]=(rank[a[0]],col[rank[a[0]]]+rank[a[0]]%2*0.5+rank[a[0]]*0.111)
        pos[a[0]] = (rank[a[0]], col[rank[a[0]]])
        i += 1
    for e in col.values():
        if e > maxcols: maxcols = e
    for e in pos.keys():
        a = pos[e]
        pos[e] = (a[0], float(a[1] + 0.05 * (rank[e] % 3)) /
                  float(col[rank[e]] + 1) * maxcols)

    j = 0

    G = nx.DiGraph()
    G.add_nodes_from(nodes)
    edges = []
    for line in lines:
        line = line.strip()
        if line[0] == "#": continue
        a = line.split("\t")
        for i, x in enumerate(a[1:]):
            if i == j: continue
            x = float(x)
            if max < abs(x): max = abs(x)
            if x > threshold:
                edges.append((nodes[i], nodes[j], {
                    'color': 'green',
                    'weight': x
                }))
        #        edges_col.append(x)
            elif x < -threshold:
                edges.append((nodes[i], nodes[j], {
                    'color': 'red',
                    'weight': x
                }))
        #        edges_col.append(x)
        j += 1
    G.add_edges_from(edges)
    e = G.edges()
    for i in e:
        edges_col.append(G[i[0]][i[1]]['weight'])

    nx.draw(G,
            edge_cmap=plt.get_cmap("RdYlGn"),
            edgelist=e,
            edge_color=edges_col,
            pos=pos,
            node_color="y",
            edge_vmin=-max,
            edge_vmax=max,
            linewidths=0,
            width=2,
            arrows=direct,
            node_size=100,
            font_size=10)
    #nx.draw(G,edge_cmap=plt.get_cmap("RdYlGn"),edge_list=edges,edge_color=edges_col,pos=pos,node_color="y",edge_vmin=-max,edge_vmax=max,linewidths=0,width=2)
    #nx.draw(G,pos=pos,node_color="y",linewidths=0,width=2)
    plt.colorbar()
    plt.show()
Example #42
0
#print(pos)
pos.update((node, (2, index)) for index, node in enumerate(r))
#print(pos)
color_map = []
for nodeCount in range(len(item1)):
    #print(nodeCount)
    color_map.append('pink') #Item 1 objects colored one color
for nodeCount in range(len(item2)):
    #print(nodeCount)
    color_map.append('green') #Item 2 objects colored second color
print("Colored the Nodes")
#print(color_map)
#This is for two parallel line bipartite graph. Put pos=pos as an argument and see . To plot based on some edge weights
#nx.draw(B, pos=pos, with_labels=True, edge_color=Edge_Weight, node_color=color_map, node_size=1500, font_size=25, font_color="yellow", font_weight="bold",edge_cmap=plt.get_cmap('BuGn'), label ="SNP To Gene eQTL Associations Cis & Trans")
#To plot with degrees of association in linear bipartite
nx.draw(B, pos=pos, with_labels=True, edge_color=['blue' if B.degree[e[0]] >= int(sys.argv[1]) else 'red' for e in B.edges],font_size=4,font_weight="bold", node_color=color_map, font_color="black", edge_cmap=plt.get_cmap('BuGn'), label ="SNP To Gene eQTL Associations Cis & Trans")
#We make circular network plot with argument in command line for the degree of connectedness and above that needs to be colored differently
#nx.draw_circular(B,with_labels=True, edge_color=['blue' if B.degree[e[0]] >= int(sys.argv[1]) else 'red' for e in B.edges], node_color=color_map, font_color="black",edge_cmap=plt.get_cmap('Blues'), label ="SNP To Gene eQTL Associations Cis & Trans" )
#plt.title("SNP to Gene eQTL Association")
plt.title('ReGen Bipartite Plot', color='magenta')
#plt.show()
print("Drawing for Circular Plot prepared")
plt.savefig('abiPlot.png', bbox_inches='tight')
print("Graph Plotted by name abiPlot.png")

#######################################################Here we Generate the Communities########################################
communities_generator = community.girvan_newman(B) #Finds communities in a graph using the Girvan–Newman Division method for centrality in packing
#communities_generator = community.greedy_modularity_communities(B)#Find communities in graph using Clauset-Newman-Moore greedy modularity maximization.
#communities_generator = community.asyn_fluidc(B) #Returns communities in G as detected by Fluid Communities algorithm.
#communities_generator = community.label_propagation_communities(B) #Generates community sets determined by label propagation
#communities_generator = community.kernighan_lin_bisection(B) #Partition a graph into two blocks using the Kernighan–Lin algorithm.
#
# To Sympy Team: A `.to_python` and `.to_c` and `.to_matlab` whould be nice to generate code, like it already works with `print latex()`.

# ## Initial Uncertainty $P_0$
#
# Initialized with $0$ means you are pretty sure where the vehicle starts

# In[42]:

P = np.diag([1000.0, 1000.0, 1000.0, 1000.0, 1000.0])
print(P, P.shape)

# In[43]:

fig = plt.figure(figsize=(5, 5))
im = plt.imshow(P, interpolation="none", cmap=plt.get_cmap('binary'))
plt.title('Initial Covariance Matrix $P$')
ylocs, ylabels = plt.yticks()
# set the locations of the yticks
plt.yticks(np.arange(6))
# set the locations and labels of the yticks
plt.yticks(np.arange(5), ('$x$', '$y$', '$\psi$', '$v$', '$\dot \psi$'),
           fontsize=22)

xlocs, xlabels = plt.xticks()
# set the locations of the yticks
plt.xticks(np.arange(6))
# set the locations and labels of the yticks
plt.xticks(np.arange(5), ('$x$', '$y$', '$\psi$', '$v$', '$\dot \psi$'),
           fontsize=22)
Example #44
0
p = []
mp = [] 
for pol in fiona.open('./politic_brazil_states/politic_brazil_states.shp'):
    print pol['geometry']['type']
    if pol['geometry']['type'] == 'Polygon':
        p.append(shape(pol['geometry']))
    elif pol['geometry']['type'] == 'MultiPolygon':
        mp.append(shape(pol['geometry']))



# We can now do GIS-ish operations on each borough polygon!
# we could randomize this by dumping the polygons into a list and shuffling it
# or we could define a random colour using fc=np.random.rand(3,)
# available colour maps are here: http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps
cm = plt.get_cmap('RdBu')
num_colours = len(mp)

fig = plt.figure()
ax = fig.add_subplot(111)

minx, miny, maxx, maxy = mp.bounds
w, h = maxx - minx, maxy - miny

ax.set_xlim(minx - 0.2 * w, maxx + 0.2 * w)
ax.set_ylim(miny - 0.2 * h, maxy + 0.2 * h)
ax.set_aspect(1)
 
patches = []
for idx, p in enumerate(mp):
    colour = cm(1. * idx / num_colours)
Example #45
0
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

num_file = 44
folder_name = 'example_2_2_vem_coarse/'
file_name = '/plot_over_line.txt'
figure_name = 'plot_over_line_mpfa.pdf'  # pdf

plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.rc('font', size=15)

cm = plt.get_cmap('gist_rainbow')

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlabel('arc length')
ax.set_ylabel('$p$')
ax.set_prop_cycle('color',
                  plt.cm.copper(np.linspace(0, 1, num_file, endpoint=False)))

for f in np.arange(1, num_file + 1):
    f_name = folder_name + str(f) + file_name
    data = np.loadtxt(f_name, delimiter=' ', unpack=True)
    if np.isnan(data).any():
        print(f_name, data)

    ax.plot(data[:, 0], data[:, 1])

#ax.legend()
Example #46
0
def show_image(dataset, index):
    import matplotlib.pyplot as plt
    plt.imshow(dataset[index][0][0], cmap=plt.get_cmap('gray'))
Example #47
0
def plot_conn_mat_func(
    conn_matrix,
    conn_model,
    atlas,
    dir_path,
    ID,
    subnet,
    labels,
    roi,
    thr,
    node_radius,
    smooth,
    hpass,
    signal,
):
    """
    API for selecting among various functional connectivity matrix plotting
    approaches.

    Parameters
    ----------
    conn_matrix : array
        NxN matrix.
    conn_model : str
       Connectivity estimation model (e.g. corr for correlation, cov for
       covariance, sps for precision covariance, partcorr for partial
       correlation). sps type is used by default.
    atlas : str
        Name of atlas parcellation used.
    dir_path : str
        Path to directory containing subject derivative data for given run.
    ID : str
        A subject id or other unique identifier.
    subnet : str
        Resting-state subnet based on Yeo-7 and Yeo-17 naming (e.g.
        'Default') used to filter nodes in the study of brain subgraphs.
    labels : list
        List of string labels corresponding to ROI nodes.
    roi : str
        File path to binarized/boolean region-of-interest Nifti1Image file.
    thr : float
        A value, between 0 and 1, to threshold the graph using any variety of
        methods triggered through other options.
    node_radius : int
        Spherical centroid node size in the case that coordinate-based
        centroids are used as ROI's.
    smooth : int
        Smoothing width (mm fwhm) to apply to time-series when extracting
        signal from ROI's.
    hpass : bool
        High-pass filter values (Hz) to apply to node-extracted time-series.
    signal : str
        The name of a valid function used to reduce the time-series region
        extraction.
    """
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    from pynets.core.utils import load_runconfig
    import networkx as nx
    import os.path as op

    out_path_fig = \
        "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s" % \
        (dir_path,
         "/adjacency_",
         ID,
         "_modality-func_",
         "%s" % ("%s%s%s" % ("subnet-",
                             subnet,
                             "_") if subnet is not None else ""),
         "%s" % ("%s%s%s" % ("roi-",
                             op.basename(roi).split(".")[0],
                             "_") if roi is not None else ""),
         "model-",
         conn_model,
         "_",
         "%s" % ("%s%s%s" % ("nodetype-spheres-",
                             node_radius,
                             "mm_") if (
             (node_radius != "parc") and (
                 node_radius is not None)) else "nodetype-parc_"),
         "%s" % ("%s%s%s" % ("tol-",
                             smooth,
                             "fwhm_") if float(smooth) > 0 else ""),
         "%s" % ("%s%s%s" % ("hpass-",
                             hpass,
                             "Hz_") if hpass is not None else ""),
         "%s" % ("%s%s%s" % ("extract-",
                             signal,
                             "") if signal is not None else ""),
         "_thr-",
         thr,
         ".png",
         )

    hardcoded_params = load_runconfig()
    try:
        cmap_name = hardcoded_params["plotting"]["functional"]["adjacency"][
            "color_theme"][0]
    except KeyError as e:
        print(
            e, "Plotting configuration not successfully extracted from"
            " advanced.yaml")

    plot_conn_mat(conn_matrix,
                  labels,
                  out_path_fig,
                  cmap=plt.get_cmap(cmap_name))

    # Plot community adj. matrix
    try:
        from pynets.statistics.individual.algorithms import \
            community_resolution_selection

        G = nx.from_numpy_matrix(np.abs(conn_matrix))
        _, node_comm_aff_mat, resolution, num_comms = \
            community_resolution_selection(G)
        out_path_fig_comm = \
            "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s" % \
            (dir_path,
             "/adjacency-communities_",
             ID,
             "_modality-func_",
             "%s" % ("%s%s%s" % ("subnet-",
                                 subnet,
                                 "_") if subnet is not None else ""),
             "%s" % ("%s%s%s" % ("roi-",
                                 op.basename(roi).split(".")[0],
                                 "_") if roi is not None else ""),
             "model-",
             conn_model,
             "_",
             "%s" % ("%s%s%s" % ("nodetype-spheres-",
                                 node_radius,
                                 "mm_") if (
                 (node_radius != "parc") and (
                     node_radius is not None)) else "nodetype-parc_"),
             "%s" % ("%s%s%s" % ("tol-",
                                 smooth,
                                 "fwhm_") if float(smooth) > 0 else ""),
             "%s" % ("%s%s%s" % ("hpass-",
                                 hpass,
                                 "Hz_") if hpass is not None else ""),
             "%s" % ("%s%s%s" % ("extract-",
                                 signal,
                                 "") if signal is not None else ""),
             "_thr-",
             thr,
             ".png",
             )
        plot_community_conn_mat(
            conn_matrix,
            labels,
            out_path_fig_comm,
            node_comm_aff_mat,
            cmap=plt.get_cmap(cmap_name),
        )
    except BaseException:
        print("\nWARNING: Louvain community detection failed. Cannot plot "
              "community matrix...")

    return
Example #48
0
def create_plots(cwd=''):
    """
    Function to plot the results of the fault-tolerant Boussinesq system

    Args:
        cwd: current workign directory
    """

    ref = 'PFASST_BOUSSINESQ_stats_hf_NOFAULT_P16.npz'

    # noinspection PyShadowingBuiltins
    list = [('PFASST_BOUSSINESQ_stats_hf_SPREAD_P16.npz', 'SPREAD', '1-sided',
             'red', 's'),
            ('PFASST_BOUSSINESQ_stats_hf_INTERP_P16.npz', 'INTERP', '2-sided',
             'orange', 'o'),
            ('PFASST_BOUSSINESQ_stats_hf_SPREAD_PREDICT_P16.npz',
             'SPREAD_PREDICT', '1-sided+corr', 'blue', '^'),
            ('PFASST_BOUSSINESQ_stats_hf_INTERP_PREDICT_P16.npz',
             'INTERP_PREDICT', '2-sided+corr', 'green', 'd'),
            ('PFASST_BOUSSINESQ_stats_hf_NOFAULT_P16.npz', 'NOFAULT',
             'no fault', 'black', 'v')]

    nprocs = 16

    xtick_dist = 8

    minstep = 128
    maxstep = 176
    # minstep = 0
    # maxstep = 320

    nblocks = int(320 / nprocs)

    # maxiter = 14
    nsteps = 0
    maxiter = 0
    vmax = -99
    vmin = -8
    for file, strategy, label, color, marker in list:
        data = np.load(cwd + 'data/' + file)

        iter_count = data['iter_count'][minstep:maxstep]
        residual = data['residual'][:, minstep:maxstep]

        residual[residual <= 0] = 1E-99
        residual = np.log10(residual)
        vmax = max(vmax, int(np.amax(residual)))

        maxiter = max(maxiter, int(max(iter_count)))
        nsteps = max(nsteps, len(iter_count))

    print(vmin, vmax)
    data = np.load(cwd + 'data/' + ref)
    ref_iter_count = data['iter_count'][nprocs - 1::nprocs]

    rcParams['figure.figsize'] = 6.0, 2.5
    fig, ax = plt.subplots()

    plt.plot(range(nblocks), [0] * nblocks, 'k-', linewidth=2)

    ymin = 99
    ymax = 0
    for file, strategy, label, color, marker in list:

        if file is not ref:
            data = np.load(cwd + 'data/' + file)
            iter_count = data['iter_count'][nprocs - 1::nprocs]

            ymin = min(ymin, min(iter_count - ref_iter_count))
            ymax = max(ymax, max(iter_count - ref_iter_count))

            plt.plot(range(nblocks),
                     iter_count - ref_iter_count,
                     color=color,
                     label=label,
                     marker=marker,
                     linestyle='',
                     linewidth=lw,
                     markersize=ms)

    plt.xlabel('block', **axis_font)
    plt.ylabel('$K_\\mathrm{add}$', **axis_font)
    plt.title('ALL', **axis_font)
    plt.xlim(-1, nblocks)
    plt.ylim(-1 + ymin, ymax + 1)
    plt.legend(loc=2, numpoints=1, fontsize=fs)
    plt.tick_params(axis='both', which='major', labelsize=fs)
    ax.xaxis.labelpad = -0.5
    ax.yaxis.labelpad = -1
    # plt.tight_layout()

    fname = 'data/BOUSSINESQ_Kadd_vs_NOFAULT_hf.png'
    plt.savefig(fname, rasterized=True, bbox_inches='tight')
    # os.system('pdfcrop ' + fname + ' ' + fname)

    for file, strategy, label, color, marker in list:

        data = np.load(cwd + 'data/' + file)

        residual = data['residual'][:, minstep:maxstep]
        stats = data['hard_stats']

        residual[residual <= 0] = 1E-99
        residual = np.log10(residual)

        rcParams['figure.figsize'] = 6.0, 2.5
        fig, ax = plt.subplots()

        cmap = plt.get_cmap('Reds', vmax - vmin + 1)
        pcol = plt.pcolor(residual, cmap=cmap, vmin=vmin, vmax=vmax)
        pcol.set_edgecolor('face')

        if file is not ref:
            for item in stats:
                if item[0] in range(minstep, maxstep):
                    plt.text(item[0] + 0.5 - (maxstep - nsteps),
                             item[1] - 1 + 0.5,
                             'x',
                             horizontalalignment='center',
                             verticalalignment='center')

        plt.axis([0, nsteps, 0, maxiter])

        ticks = np.arange(vmin, vmax + 1)
        tickpos = np.linspace(ticks[0] + 0.5, ticks[-1] - 0.5, len(ticks))
        cax = plt.colorbar(pcol, ticks=tickpos, pad=0.02)
        cax.set_ticklabels(ticks)
        cax.ax.tick_params(labelsize=fs)

        cax.set_label('log10(residual)', **axis_font)
        plt.tick_params(axis='both', which='major', labelsize=fs)
        ax.xaxis.labelpad = -0.5
        ax.yaxis.labelpad = -0.5

        ax.set_xlabel('step', **axis_font)
        ax.set_ylabel('iteration', **axis_font)

        ax.set_yticks(np.arange(1, maxiter, 2) + 0.5, minor=False)
        ax.set_xticks(np.arange(0, nsteps, xtick_dist) + 0.5, minor=False)
        ax.set_yticklabels(np.arange(1, maxiter, 2) + 1, minor=False)
        ax.set_xticklabels(np.arange(minstep, maxstep, xtick_dist),
                           minor=False)

        plt.title(strategy)

        # plt.tight_layout()

        fname = 'data/BOUSSINESQ_steps_vs_iteration_hf_' + strategy + '.png'
        plt.savefig(fname, rasterized=True, bbox_inches='tight')
        # os.system('pdfcrop ' + fname + ' ' + fname)

        plt.close('all')
Example #49
0
def tpi_profiles(base_tpi, base_params, reform_tpi=None,
                 reform_params=None, by_j=True, var='n_mat',
                 num_years=5, start_year=DEFAULT_START_YEAR, plot_title=None,
                 path=None):
    '''
    Plot lifecycle profiles of given variable in the SS.

    Args:
        base_ss (dictionary): TPI output from baseline run
        base_params (OG-USA Specifications class): baseline parameters
            object
        reform_ss (dictionary): TPI output from reform run
        reform_params (OG-USA Specifications class): reform parameters
            object
        var (string): name of variable to plot
        num_year (integer): number of years to compute changes over
        start_year (integer): year to start plot
        plot_title (string): title for plot
        path (string): path to save figure to

    Returns:
        fig (Matplotlib plot object): plot of lifecycle profiles

    '''
    assert isinstance(start_year, (int, np.integer))
    assert isinstance(num_years, (int, np.integer))
    if reform_tpi:
        assert (base_params.start_year == reform_params.start_year)
        assert (base_params.S == reform_params.S)
        assert (base_params.starting_age == reform_params.starting_age)
        assert (base_params.ending_age == reform_params.ending_age)
    age_vec = np.arange(base_params.starting_age,
                        base_params.starting_age + base_params.S)
    fig1, ax1 = plt.subplots()
    start_idx = start_year - base_params.start_year
    end_idx = start_idx + num_years
    if by_j:
        cm = plt.get_cmap('coolwarm')
        ax1.set_prop_cycle(color=[cm(1. * i / 7) for i in range(7)])
        for j in range(base_params.J):
            plt.plot(age_vec,
                     base_tpi[var][start_idx: end_idx, :,
                                   j].sum(axis=0) / num_years,
                     label='Baseline, j = ' + str(j))
            if reform_tpi:
                plt.plot(age_vec,
                         reform_tpi[var][start_idx: end_idx, :,
                                         j].sum(axis=0) / num_years,
                         label='Reform, j = ' + str(j), linestyle='--')
    else:
        base_var = ((
            base_tpi[var][start_idx: end_idx, :, :] *
            base_params.lambdas.reshape(1, 1, base_params.J)
            ).sum(axis=2).sum(axis=0) / num_years)
        plt.plot(age_vec, base_var, label='Baseline')
        if reform_tpi:
            reform_var = ((
                reform_tpi[var][start_idx: end_idx, :, :] *
                reform_params.lambdas.reshape(1, 1, base_params.J)
                ).sum(axis=2).sum(axis=0) / num_years)
            plt.plot(age_vec, reform_var, label='Reform',
                     linestyle='--')
    plt.xlabel(r'Age')
    plt.ylabel(VAR_LABELS[var])
    plt.legend(loc=9, bbox_to_anchor=(0.5, -0.15), ncol=2)
    if plot_title:
        plt.title(plot_title, fontsize=15)
    if path:
        fig_path1 = os.path.join(path)
        plt.savefig(fig_path1, bbox_inches="tight")
    else:
        return fig1
    plt.close()
Example #50
0
def plot_conn_mat_struct(conn_matrix, conn_model, atlas, dir_path, ID, subnet,
                         labels, roi, thr, node_radius, track_type, traversal,
                         min_length, error_margin):
    """
    API for selecting among various structural connectivity matrix plotting
    approaches.

    Parameters
    ----------
    conn_matrix : array
        NxN matrix.
    conn_model : str
       Connectivity estimation model (e.g. corr for correlation, cov for
       covariance, sps for precision covariance, partcorr for partial
       correlation). sps type is used by default.
    atlas : str
        Name of atlas parcellation used.
    dir_path : str
        Path to directory containing subject derivative data for given run.
    ID : str
        A subject id or other unique identifier.
    subnet : str
        Resting-state subnet based on Yeo-7 and Yeo-17 naming
        (e.g. 'Default') used to filter nodes in the study of brain subgraphs.
    labels : list
        List of string labels corresponding to ROI nodes.
    roi : str
        File path to binarized/boolean region-of-interest Nifti1Image file.
    thr : float
        A value, between 0 and 1, to threshold the graph using any variety of
        methods triggered through other options.
    node_radius : int
        Spherical centroid node size in the case that coordinate-based
        centroids are used as ROI's.
    track_type : str
        Tracking algorithm used (e.g. 'local' or 'particle').
    traversal : str
        The statistical approach to tracking. Options are:
        det (deterministic), closest (clos), boot (bootstrapped), and prob
        (probabilistic).
    min_length : int
        Minimum fiber length threshold in mm to restrict tracking.
    """
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    from pynets.core.utils import load_runconfig
    from pynets.plotting import adjacency
    import networkx as nx
    import os.path as op

    out_path_fig = \
        "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s" % \
        (dir_path,
         "/adjacency_",
         ID,
         "_modality-dwi_",
         "%s" % ("%s%s%s" % ("subnet-",
                             subnet,
                             "_") if subnet is not None else ""),
         "%s" % ("%s%s%s" % ("roi-",
                             op.basename(roi).split(".")[0],
                             "_") if roi is not None else ""),
         "model-",
         conn_model,
         "_",
         "%s" % ("%s%s%s" % ("nodetype-spheres-",
                             node_radius,
                             "mm_") if (
             (node_radius != "parc") and (
                 node_radius is not None)) else "nodetype-parc_"),
         "_tracktype-",
         track_type,
         "_traversal-",
         traversal,
         "_minlength-",
         min_length,
         "_tol-",
         error_margin,
         "_thr-",
         thr,
         ".png",
         )

    hardcoded_params = load_runconfig()
    try:
        cmap_name = hardcoded_params["plotting"]["structural"]["adjacency"][
            "color_theme"][0]
    except KeyError as e:
        print(
            e, "Plotting configuration not successfully extracted from"
            " advanced.yaml")

    adjacency.plot_conn_mat(conn_matrix,
                            labels,
                            out_path_fig,
                            cmap=plt.get_cmap(cmap_name))

    # Plot community adj. matrix
    try:
        from pynets.statistics.individual.algorithms import \
            community_resolution_selection

        G = nx.from_numpy_matrix(np.abs(conn_matrix))
        _, node_comm_aff_mat, resolution, num_comms = \
            community_resolution_selection(G)
        out_path_fig_comm = \
            "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s" \
            % (dir_path,
               "/adjacency-communities_",
               ID,
               "_modality-dwi_",
               "%s" % ("%s%s%s" % ("subnet-",
                                   subnet,
                                   "_") if subnet is not None else ""),
               "%s" % ("%s%s%s" % ("roi-",
                                   op.basename(roi).split(".")[0],
                                   "_") if roi is not None else ""),
               "model-",
               conn_model,
               "_",
               "%s" % ("%s%s%s" % ("nodetype-spheres-",
                                   node_radius,
                                   "mm_") if (
                   (node_radius != "parc") and (
                       node_radius is not None)) else "nodetype-parc_"),
               "_tracktype-",
               track_type,
               "_traversal-",
               traversal,
               "_minlength-",
               min_length,
               "_tol-",
               error_margin,
               "_thr-",
               thr,
               ".png",
               )
        adjacency.plot_community_conn_mat(
            conn_matrix,
            labels,
            out_path_fig_comm,
            node_comm_aff_mat,
            cmap=plt.get_cmap(cmap_name),
        )
    except BaseException:
        print("\nWARNING: Louvain community detection failed. Cannot plot"
              " community matrix...")

    return
Example #51
0
def create_waveform_image(fpath_in,
                          fpath_out,
                          max_num_of_points=None,
                          colormap_options=None):
    """
    Create a waveform image from audio file at fpath_in and write to fpath_out.
    Colormap info: http://matplotlib.org/examples/color/colormaps_reference.html
    """
    colormap_options = colormap_options or {}
    cmap_name = colormap_options.get('name') or 'cool'
    vmin = colormap_options.get('vmin') or 0
    vmax = colormap_options.get('vmax') or 1
    color = colormap_options.get('color') or 'w'

    tempwav_fh, tempwav_name = tempfile.mkstemp(suffix=".wav")
    os.close(
        tempwav_fh)  # close the file handle so ffmpeg can write to the file
    try:
        ffmpeg_cmd = ['ffmpeg', '-y', '-loglevel', 'panic', '-i', fpath_in]
        # The below settings apply to the WebM encoder, which doesn't seem to be
        # built by Homebrew on Mac, so we apply them conditionally
        if not sys.platform.startswith('darwin'):
            ffmpeg_cmd.extend(['-cpu-used', '-16'])
        ffmpeg_cmd += [tempwav_name]
        result = subprocess.check_output(ffmpeg_cmd)

        spf = wave.open(tempwav_name, 'r')

        # Extract raw audio from wav file
        signal = spf.readframes(-1)
        spf.close()
        signal = np.frombuffer(signal, np.int16)

        # Get subarray from middle
        length = len(signal)
        count = max_num_of_points or length
        subsignals = signal[int((length - count) / 2):int((length + count) /
                                                          2)]

        # Set up max and min values for axes
        X = [[.6, .6], [.7, .7]]
        xmin, xmax = xlim = 0, count
        max_y_axis = max(-min(subsignals), max(subsignals))
        ymin, ymax = ylim = -max_y_axis, max_y_axis

        # Set up canvas according to user settings
        (xsize, ysize) = (THUMBNAIL_SIZE[0] / 100.0, THUMBNAIL_SIZE[1] / 100.0)
        figure = Figure(figsize=(xsize, ysize), dpi=100)
        canvas = FigureCanvasAgg(figure)
        ax = figure.add_subplot(111,
                                xlim=xlim,
                                ylim=ylim,
                                autoscale_on=False,
                                frameon=False)
        ax.set_yticklabels([])
        ax.set_xticklabels([])
        ax.set_xticks([])
        ax.set_yticks([])
        cmap = plt.get_cmap(cmap_name)
        cmap = LinearSegmentedColormap.from_list(
            'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=vmin, b=vmax),
            cmap(np.linspace(vmin, vmax, 100)))
        ax.imshow(X,
                  interpolation='bicubic',
                  cmap=cmap,
                  extent=(xmin, xmax, ymin, ymax),
                  alpha=1)

        # Plot points
        ax.plot(np.arange(count), subsignals, color)
        ax.set_aspect("auto")
        canvas.print_figure(fpath_out)
    except (subprocess.CalledProcessError, Exception) as e:
        raise ThumbnailGenerationError("Failed file {} {}".format(fpath_in, e))
    finally:
        os.remove(tempwav_name)
Example #52
0
def ss_profiles(base_ss, base_params, reform_ss=None,
                reform_params=None, by_j=True, var='nssmat',
                plot_data=False,
                plot_title=None, path=None):
    '''
    Plot lifecycle profiles of given variable in the SS.

    Args:
        base_ss (dictionary): SS output from baseline run
        base_params (OG-USA Specifications class): baseline parameters
            object
        reform_ss (dictionary): SS output from reform run
        reform_params (OG-USA Specifications class): reform parameters
            object
        var (string): name of variable to plot
        plot_data (bool): whether to plot data values for given variable
        plot_title (string): title for plot
        path (string): path to save figure to

    Returns:
        fig (Matplotlib plot object): plot of lifecycle profiles

    '''
    if reform_ss:
        assert (base_params.S == reform_params.S)
        assert (base_params.starting_age == reform_params.starting_age)
        assert (base_params.ending_age == reform_params.ending_age)
    age_vec = np.arange(base_params.starting_age,
                        base_params.starting_age + base_params.S)
    fig1, ax1 = plt.subplots()
    if by_j:
        cm = plt.get_cmap('coolwarm')
        ax1.set_prop_cycle(color=[cm(1. * i / 7) for i in range(7)])
        for j in range(base_params.J):
            plt.plot(age_vec, base_ss[var][:, j],
                     label='Baseline, j = ' + str(j))
            if reform_ss:
                plt.plot(age_vec, reform_ss[var][:, j],
                         label='Reform, j = ' + str(j), linestyle='--')
    else:
        base_var = (
            base_ss[var][:, :] *
            base_params.lambdas.reshape(1, base_params.J)).sum(axis=1)
        plt.plot(age_vec, base_var, label='Baseline')
        if reform_ss:
            reform_var = (
                reform_ss[var][:, :] *
                reform_params.lambdas.reshape(1, reform_params.J)).sum(axis=1)
            plt.plot(age_vec, reform_var, label='Reform', linestyle='--')
        if plot_data:
            assert var == 'nssmat'
            labor_file = utils.read_file(
                cur_path, "data/labor/cps_hours_by_age_hourspct.txt")
            data = pd.read_csv(labor_file, header=0, delimiter='\t')
            piv = data.pivot(index='age', columns='hours_pct',
                             values='mean_hrs')
            lab_mat_basic = np.array(piv)
            lab_mat_basic /= np.nanmax(lab_mat_basic)
            piv2 = data.pivot(index='age', columns='hours_pct',
                              values='num_obs')
            weights = np.array(piv2)
            weights /= np.nansum(weights, axis=1).reshape(
                60, 1)
            weighted = np.nansum((lab_mat_basic * weights), axis=1)
            weighted = np.append(weighted, np.zeros(20))
            weighted[60:] = np.nan
            plt.plot(age_vec, weighted, linewidth=2.0, label='Data',
                     linestyle=':')
    plt.xlabel(r'Age')
    plt.ylabel(VAR_LABELS[var])
    plt.legend(loc=9, bbox_to_anchor=(0.5, -0.15), ncol=2)
    if plot_title:
        plt.title(plot_title, fontsize=15)
    if path:
        fig_path1 = os.path.join(path)
        plt.savefig(fig_path1, bbox_inches="tight")
    else:
        return fig1
    plt.close()
def plot_temporal_trajectory(agent_timeseries,
                             config,
                             out_dir='out',
                             filename='temporal'):

    bounds = config.get('bounds', DEFAULT_BOUNDS)
    field = config.get('field')
    rotate_90 = config.get('rotate_90', False)

    # get agents
    times = np.array(agent_timeseries['time'])
    agents = agent_timeseries['agents']

    if rotate_90:
        field = rotate_field_90(field)
        for agent_id, series in agents.items():
            agents[agent_id] = rotate_agent_series_90(series, bounds)
        bounds = rotate_bounds_90(bounds)

    # get each agent's trajectory
    trajectories = get_agent_trajectories(agents, times)

    # initialize a spatial figure
    fig, ax = initialize_spatial_figure(bounds)

    if field is not None:
        field = np.transpose(field)
        shape = field.shape
        im = plt.imshow(field,
                        origin='lower',
                        extent=[0, shape[1], 0, shape[0]],
                        cmap='Greys')

    for agent_id, trajectory_data in trajectories.items():
        agent_trajectory = trajectory_data['value']

        # convert trajectory to 2D array
        locations_array = np.array(agent_trajectory)
        x_coord = locations_array[:, 0]
        y_coord = locations_array[:, 1]

        # make multi-colored trajectory
        points = np.array([x_coord, y_coord]).T.reshape(-1, 1, 2)
        segments = np.concatenate([points[:-1], points[1:]], axis=1)
        lc = LineCollection(segments, cmap=plt.get_cmap('cool'))
        lc.set_array(times)
        lc.set_linewidth(6)

        # plot line
        line = plt.gca().add_collection(lc)

    # color bar
    cbar = plt.colorbar(line,
                        ticks=[times[0], times[-1]],
                        aspect=90,
                        shrink=0.4)
    cbar.set_label('time (s)', rotation=270)

    fig_path = os.path.join(out_dir, filename)
    plt.subplots_adjust(wspace=0.7, hspace=0.1)
    plt.savefig(fig_path, bbox_inches='tight')
    plt.close(fig)
Example #54
0
def createArray(df_train, df_labels, df_test):

    # Let's replace the construction year, longitude and latitude with the mean of the rest of the values.
    numEnhancer = NumericalEnhancer()
    df_train = numEnhancer.fit_transform(df_train)
    df_test = numEnhancer.transform(df_test)

    # Predict missing heights
    HeightTrain = df_train[['longitude', 'latitude',
                            'gps_height']][df_train.gps_height > 0]
    HeightTest = df_train[['longitude', 'latitude',
                           'gps_height']][df_train.gps_height == 0]

    Xtrain, Xtest, ytrain, ytest = train_test_split(HeightTrain[['longitude','latitude']], \
                                                    HeightTrain['gps_height'], random_state=0)

    models = [
        GaussianNB, LinearRegression, DecisionTreeRegressor,
        RandomForestRegressor
    ]

    height_results = try_model(models, Xtrain, ytrain, Xtest, ytest).sort_values(by=['Cross Val Mean Score'], \
                               ascending=[True]).reset_index(drop=True)
    print(height_results)

    df_train.longitude = df_train.longitude.replace(np.nan,
                                                    df_train.longitude.mean())
    df_test.longitude = df_test.longitude.replace(np.nan,
                                                  df_test.longitude.mean())

    df_train['gps_height'][df_train.gps_height == 0] = height_results.loc[0,"Model"].\
    predict(df_train[['longitude','latitude']][df_train.gps_height == 0])

    df_test['gps_height'][df_test.gps_height == 0] = height_results.loc[0,"Model"].\
    predict(df_test[['longitude','latitude']][df_test.gps_height == 0])

    # Let's see the result of the predicted heights in the train dataframe
    df_train[df_train.longitude > 0].plot(kind="scatter",
                                          x="longitude",
                                          y="latitude",
                                          alpha=0.4,
                                          figsize=(14, 10),
                                          title='All data',
                                          c="gps_height",
                                          cmap=plt.get_cmap("jet"),
                                          colorbar=True,
                                          sharex=False)
    plt.show()

    # Drop features:
    features_dropper = DropFeatures(date_recorded=True)
    df_train = features_dropper.transform(df_train)
    df_test = features_dropper.transform(df_test)

    # Reduce cardinality:
    cardReducer = ReduceCardinality()
    df_train = cardReducer.fit_transform(df_train)
    df_test = cardReducer.transform(df_test)

    # Vectorization: Let's create now the arrays that will be used in following tasks
    vectorizator = Vectorization()
    X_train = vectorizator.fit_transform(df_train)
    X_test = vectorizator.transform(df_test)
    y_train = df_labels.status_group.values
    print(X_train.shape)
    print(X_test.shape)
    print(y_train.shape)

    # Standarization: Normalization of the numerical features
    mms = MinMaxScaler()
    X_train = mms.fit_transform(X_train)
    X_test = mms.transform(X_test)

    # PCA: Although the dataset has many features, let's try to visualize in 2D the different groups using PCA.
    pca = PCA(n_components=2)
    X_train_viz = pca.fit_transform(X_train)
    df_PCA = pd.DataFrame(X_train_viz, columns=[1, 2])
    df_PCA['label'] = y_train

    ax = df_PCA[df_PCA.label == 'functional'].plot(kind='scatter',
                                                   x=1,
                                                   y=2,
                                                   color='r',
                                                   label='Functional',
                                                   figsize=(14, 10))
    df_PCA[df_PCA.label == 'non functional'].plot(kind='scatter',
                                                  x=1,
                                                  y=2,
                                                  color='g',
                                                  label='Non functional',
                                                  ax=ax)
    df_PCA[df_PCA.label == 'functional needs repair'].plot(
        kind='scatter',
        x=1,
        y=2,
        color='b',
        label='Functional needs repair',
        ax=ax)
    plt.show()

    # There is no obvious separation between groups with only 2 dimensions. Let's see how many do we need in order to loose the minimum possible variance.
    pca = PCA(n_components=100)
    X_train_reduced = pca.fit_transform(X_train)
    X_test_reduced = pca.transform(X_test)

    cumsum = np.cumsum(pca.explained_variance_ratio_)
    var_exp = pca.explained_variance_ratio_

    plt.figure(figsize=(10, 8))
    plt.bar(range(100),
            var_exp,
            alpha=0.5,
            align='center',
            label='individual explained variance')
    plt.step(range(100),
             cumsum,
             where='mid',
             label='cumulative explained variance')
    plt.ylabel('Explained variance ratio')
    plt.xlabel('Principal components')
    plt.legend(loc='best')
    plt.tight_layout()
    plt.show()

    # Working with the PCA reduced data:
    cv_scores = cross_val_score(RandomForestClassifier(),
                                X_train_reduced,
                                y_train,
                                cv=10)
    sns.distplot(cv_scores)
    plt.title('Average score: {}'.format(np.mean(cv_scores)))
    print('CV accuracy: %.3f +/- %.3f' %
          (np.mean(cv_scores), np.std(cv_scores)))
    plt.show()

    # Working with all the features after reducing cardinality:
    cv_scores = cross_val_score(RandomForestClassifier(),
                                X_train,
                                y_train,
                                cv=10)
    sns.distplot(cv_scores)
    plt.title('Average score: {}'.format(np.mean(cv_scores)))
    print('CV accuracy: %.3f +/- %.3f' %
          (np.mean(cv_scores), np.std(cv_scores)))
    plt.show()
    # We can see that with PCA, the accuracy drops around 2%, but the model deals with less than 5% features.

    return X_train_reduced, X_test_reduced
Example #55
0
def graph_align(g1, g2, ops, title="", save=None, rna=False):
    """
    Draw aligned graphs.
    """

    f = nx.compose(g1, g2)
    path = reversed(list(ops.path_iter()))
    cost = ops.cost
    # for o in path:
    edit_edges = []
    edge_list = []
    color_list = []
    for i, o in enumerate(path):
        e1, e2 = o.op
        if e1 == 'NILL' and e2 == 'NILL':
            continue
        f.add_edge(e1, e2, label='')
        edit_edges.append((e1, e2))

    c = plt.get_cmap('Paired')
    pos = nx.spring_layout(f)
    # pos = rna_layout.circular_layout(f)
    # nx.draw_networkx(f, pos, color='red')
    config = {'node_size': 500, 'alpha': 1, 'font_size': 25}
    nx.draw(f, pos, nodelist=['NILL'], color='red', **config)
    nx.draw_networkx_edges(f, pos,
                           edgelist=edit_edges, edge_color='grey', width=3)
    nx.draw_networkx_nodes(f, pos, nodelist=g1.nodes, node_color='blue', **config)
    nx.draw_networkx_edges(f, pos, edgelist=g1.edges)
    nx.draw_networkx_nodes(f, pos, nodelist=g2.nodes, node_color='green', **config)
    nx.draw_networkx_edges(f, pos, edgelist=g2.edges)

    make_label = lambda s: labels[s[:2]] + labels[s[0::2]] if len(set(s[1:])) == 2 \
        else labels[s[:2]]
    edge_labels = {}
    for e in f.edges.data():
        label = e[2]['label']
        if label == 'B53':
            label = ''
        edge_labels[(e[0], e[1])] = label

    if rna:
        import matplotlib
        # matplotlib.rcParams['text.usetex'] = False
        matplotlib.rcParams['text.usetex'] = True
        params = {'text.latex.preamble': [r'\usepackage{fdsymbol}\usepackage{xspace}']}
        plt.rcParams.update(params)

        labels = {
            'CW': r"$\medblackcircle$\xspace",
            'CS': r"$\medblacktriangleright$\xspace",
            'CH': r"$\medblacksquare$\xspace",
            'TW': r"$\medcircle$\xspace",
            'TS': r"$\medtriangleright$\xspace",
            'TH': r"$\medsquare$\xspace"
        }
        make_label = lambda s: labels[s[:2]] + labels[s[0::2]] if len(set(s[1:])) == 2 \
            else labels[s[:2]]
        edge_labels = {(e1, e2):
                           make_label(d['label']) if d['label'] not in ['B53', '']
                           else '' for e1, e2, d in f.edges.data()}
        nx.draw_networkx_edge_labels(f, pos, edge_labels=edge_labels)
    else:
        nx.draw_networkx_edge_labels(f, pos, edge_labels=edge_labels, font_size=config['font_size'])

    plt.title(f"GED: {cost} " + title)
    if save:
        plt.savefig(save, format="pdf")
    plt.show()

    pass
Example #56
0
#Mostrar a quantidade de cada tipo de imóvel disponível
df_clean.room_type.value_counts()

#Mostrar a porcentagem de cada tipo de imóvel disponível
(df_clean.room_type.value_counts() / df_clean.shape[0]) * 100
"""### **Q7. Quais as localidades mais caras em Hong Kong?**"""

#Verificar preços por bairros, na média (top 10)
df_clean.groupby(['neighbourhood'
                  ]).price.mean().sort_values(ascending=False)[:10]

#Plotar os imóveis pela latitude-longitude
df_clean.plot(kind="scatter",
              x='longitude',
              y='latitude',
              alpha=0.4,
              c=df_clean['price'],
              s=8,
              cmap=plt.get_cmap('jet'),
              figsize=(12, 8))
"""## Conclusões

Foi feita apenas uma análise superficial na base de dados do Airbnb relativos à cidade de Hong Kong.
Nesta análise percebe-se que existem *outliers* em algumas das variáveis. 

Também pode-se notar que em algumas localidades há poucos imóveis disponíveis, o que pode distorcer as informações estatísticas de alguns atributos.

Por fim, lembra-se que este *dataset* é uma versão resumida, ideal apenas para uma abordagem inicial. Recomenda-se que seja usado, em uma próxima análise exploratória, o conjunto de dados completos, com 106 atributos disponíveis.
"""
Example #57
0
from stem_pytools import STEM_mapper


def read_aqout(fname, tstep=-1):
    """read and return surface "slab" at specified timestep from STEM
    AQOUT file (default timestep is the last step in the file)
    """

    nc = netCDF4.Dataset(fname)
    z_sfc = 0  # z = 0 is the surface
    cos = nc.variables['CO2_TRACER1'][tstep, z_sfc, ...].squeeze()
    return (cos)


if __name__ == "__main__":

    suffixes = ['0_124', '125_249', '250_374', '375_499']
    cos = [
        read_aqout("./STEM_TestRuns/output/AQOUT_{}.nc".format(sfx))
        for sfx in suffixes
    ]

    molecules_m3_to_pptv = 1e12
    for i, this_cos in enumerate(cos):
        this_cos = this_cos * molecules_m3_to_pptv
        m = STEM_mapper.Mapper124x124(this_cos).draw_map(
            t_str="I/O API bounds test, COS in bounds[{}]".format(suffixes[i]),
            fast_or_pretty='pretty',
            cmap=plt.get_cmap('Blues'))
        m.map.fig.savefig('bounds{}.png'.format(suffixes[i]))
Example #58
0
# List of stations to plot
# list_station=["ANDRA","PdB","Marseille","Nice","Frenes","Chamonix","Marnaz","Passy"]
list_station = [
    "Pipiripi", "El Alto"
]  #ANDRA-PM10","PdB","MRS-5av","Nice","GRE-fr","Chamonix","Marnaz","Passy"]

# ========================================================================
# Plot part
# ========================================================================

conn = sqlite3.connect("/home/webersa/Documents/BdD/BdD_PM/db.sqlite")
for season in ["La Paz Experiment"]:  #"DJF", "MAM", "JJA", "SON"]:
    # initialize the figure
    f = plt.figure(figsize=(9.41, 5.59))
    # set the color map + missing value in lightgrey
    cmap = plt.get_cmap(name="RdBu_r")
    cmap.set_bad(color='0.85')

    # load the chemistry and OP, then plot the correlation matrix
    # for each station
    for i, name in enumerate(list_station):
        # station_file = INPUT_DIR+"/"+name+"/"+name+"_PM.csv"
        # df = pd.read_csv(station_file, index_col=["date"], parse_dates=True)
        df = pd.read_sql(
            "SELECT * FROM values_all WHERE station in ('{station}');".format(
                station=name),
            con=conn,
            index_col=["date"],
            parse_dates=["date"])
        colKO = set(df.columns) - set(colOK)
        df.drop(colKO, axis=1, inplace=True)
Example #59
0
def get_bboxes(img):
    # YOLO START
    img_in = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img_in = tf.expand_dims(img_in, 0)
    img_in = transform_images(img_in, 416)

    # print("-------------------------------")
    # print(type(img_in)) # <class 'tensorflow.python.framework.ops.EagerTensor'>
    # print(img_in.shape) # (1, 416, 416, 3)
    # print(img_in.dtype) # <dtype: 'float32'>
    # print("-------------------------------")

    boxes, scores, classes, nums = yolo.predict(img_in)

    classes = classes[0]
    names = []
    for i in range(len(classes)):
        names.append(class_names[int(classes[i])])

    names = np.array(names)

    converted_boxes = convert_boxes(img, boxes[0])

    features = encoder(img, converted_boxes)

    detections = [
        Detection(bbox, score, class_name,
                  feature) for bbox, score, class_name, feature in zip(
                      converted_boxes, scores[0], names, features)
    ]

    boxs = np.array([d.tlwh for d in detections])
    scores = np.array([d.confidence for d in detections])
    classes = np.array([d.class_name for d in detections])
    indices = preprocessing.non_max_suppression(boxs, classes, nms_max_overlap,
                                                scores)
    detections = [detections[i] for i in indices]

    #p.print(detections)  // [<deep_sort.detection.Detection object at 0x00000218F72D8D68>, <deep_sort.detection.Detection object at 0x00000218F7325940>, <deep_sort.detection.Detection object at 0x00000218F7325C18>, <deep_sort.detection.Detection object at 0x00000218F7325F28>, <deep_sort.detection.Detection object at 0x00000218F7325EF0>, <deep_sort.detection.Detection object at 0x00000218F7325F60>, <deep_sort.detection.Detection object at 0x00000218F7325D68>, <deep_sort.detection.Detection object at 0x00000218F7325DA0>, <deep_sort.detection.Detection object at 0x00000218F7329400>]
    #p.type_(detections) // TYPE => <class 'list'>
    # p.print(detections[1]) // <deep_sort.detection.Detection object at 0x00000266814E3828>

    tracker.predict()
    tracker.update(detections)

    # Matplotlib has a number of built-in colormaps accessible via matplotlib.cm.get_cmap.
    cmap = plt.get_cmap('tab20b')
    colors = [cmap(i)[:3] for i in np.linspace(0, 1, 20)]

    # Current vehicle count
    current_count = int(0)

    # (tek frame içindeki tüm araçlar için döner) tracker'ın tüm sonuçları için for döngüsü
    bboxes = []
    for track in tracker.tracks:
        if not track.is_confirmed() or track.time_since_update > 1:
            continue
        bbox = track.to_tlbr(
        )  # [848.78925062 113.98058018 901.1299524  144.32627563]
        # class_name = track.get_class() # car (nesne ismi)
        # color = colors[int(track.track_id) % len(colors)] # (0.807843137254902, 0.8588235294117647, 0.611764705882353)
        # color = [i * 255 for i in color] # [231.0, 203.0, 148.0]
        bboxes.append(bbox)

        # img => videodan alınan frame (np ndarray)

        #Bounding box çiz
        # cv2.rectangle(img, (int(bbox[0]),int(bbox[1])), (int(bbox[2]),int(bbox[3])), color, 2)
        # #cv2.rectangle(img, (int(bbox[0]), int(bbox[1]-30)), (int(bbox[0])+(len(class_name)
        #             #+len(str(track.track_id)))*17, int(bbox[1])), color, -1)
        # cv2.putText(img, class_name+"-"+str(track.track_id), (int(bbox[0]), int(bbox[1]-10)), 0, 0.75,
        #             (255, 255, 255), 2)
    return bboxes
Example #60
0
def variable_sweep(problem):
    from matplotlib import rcParams
    rcParams['font.family'] = 'times new roman'
    # rcParams['font.times-new-roman'] = ['times new roman']

    number_of_points = 5
    outputs = carpet_plot(problem, number_of_points, 0,
                          0)  #run carpet plot, suppressing default plots
    inputs = outputs.inputs
    objective = outputs.objective
    constraints = outputs.constraint_val
    plt.figure(0)
    CS = plt.contourf(inputs[0, :], inputs[1, :], objective, 20, linewidths=2)
    cbar = plt.colorbar(CS)

    cbar.ax.set_ylabel('Fuel Burn (kg)')
    CS_const = plt.contour(inputs[0, :],
                           inputs[1, :],
                           constraints[-1, :, :],
                           cmap=plt.get_cmap('hot'))
    plt.clabel(CS_const, inline=1, fontsize=12, family='times new roman')
    cbar = plt.colorbar(CS_const)
    # plt.FontProperties(family='times new roman', style='italic', size=12)
    cbar.ax.set_ylabel('BOW (kg)')
    # font = matplotlib.font_manager.FontProperties(family='times new roman', style='italic', size=12)

    # CS_const.font_manager.FontProperties.set_family(family='times new roman')

    plt.xlabel('Wing Area (m^2)')
    plt.ylabel('Aspect Ratio (-)')

    plt.legend(loc='upper left')
    # plt.show(block=True)
    plt.show()

    number_of_points = 5
    outputs = carpet_plot(
        problem, number_of_points, 0, 0, sweep_index_0=1,
        sweep_index_1=3)  # run carpet plot, suppressing default plots
    inputs = outputs.inputs
    objective = outputs.objective
    constraints = outputs.constraint_val
    plt.figure(0)
    CS = plt.contourf(inputs[0, :], inputs[1, :], objective, 20, linewidths=2)
    cbar = plt.colorbar(CS)

    cbar.ax.set_ylabel('Fuel Burn (kg)')
    CS_const = plt.contour(inputs[0, :],
                           inputs[1, :],
                           constraints[-1, :, :],
                           cmap=plt.get_cmap('hot'))
    plt.clabel(CS_const, inline=1, fontsize=10)
    cbar = plt.colorbar(CS_const)
    cbar.ax.set_ylabel('BOW (kg)')

    plt.xlabel('AR (-)')
    plt.ylabel('Sweep Angle (Deg)')

    plt.legend(loc='upper left')
    plt.show()

    number_of_points = 5
    outputs = carpet_plot(
        problem, number_of_points, 0, 0, sweep_index_0=2,
        sweep_index_1=3)  # run carpet plot, suppressing default plots
    inputs = outputs.inputs
    objective = outputs.objective
    constraints = outputs.constraint_val
    plt.figure(0)
    CS = plt.contourf(inputs[0, :], inputs[1, :], objective, 20, linewidths=2)
    cbar = plt.colorbar(CS)

    cbar.ax.set_ylabel('Fuel Burn (kg)')
    CS_const = plt.contour(inputs[0, :],
                           inputs[1, :],
                           constraints[-1, :, :],
                           cmap=plt.get_cmap('hot'))
    plt.clabel(CS_const, inline=1, fontsize=10)
    cbar = plt.colorbar(CS_const)
    cbar.ax.set_ylabel('BOW (kg)')

    plt.xlabel('t/c (-)')
    plt.ylabel('Sweep Angle (Deg)')

    plt.legend(loc='upper left')
    plt.show(block=True)

    return