예제 #1
0
def make_color_panel(df):
    """Map categorical values to colors"""
    import brewer2mpl
    set1 = brewer2mpl.get_map('Set1','Qualitative',9)
    dark2 = brewer2mpl.get_map('Dark2','Qualitative',8)
    out = {}
    if 'condition' in df.columns:
        d = {'MUT' : set1.mpl_colors[0], 'WT' : set1.mpl_colors[1]}
        out['condition']  = pd.DataFrame([ d[x] for x in df.condition ],
                                         index=df.index)
    
    if 'disease_abbr' in df.columns:
        d = {'BRCA' : dark2.mpl_colors[0], 'CLL' : dark2.mpl_colors[2],
             'LUAD' : dark2.mpl_colors[1], 'LUSC' : dark2.mpl_colors[3],
             'UM' : dark2.mpl_colors[-2]}
        out['disease_abbr'] = pd.DataFrame([ d[x] for x in df.disease_abbr ],
                                           index=df.index)
    
    if 'HEAT 5-9' in df.columns:
        d = {True:dark2.mpl_colors[-3], False:(1,1,1)}
        out['HEAT 5-9'] = pd.DataFrame([ d[x] for x in df['HEAT 5-9']],
                                       index=df.index)

    if 'IGHV' in df.columns:
        d = {'mutated':set1.mpl_colors[2], 'unmutated':set1.mpl_colors[3]}
        out['IGHV'] = pd.DataFrame([ d[x] for x in df['IGHV']],
                                       index=df.index)

    if 'ZAP70' in df.columns:
        d = {'positive':set1.mpl_colors[4], 'negative':set1.mpl_colors[6]}
        out['ZAP70'] = pd.DataFrame([ d[x] for x in df['ZAP70']],
                                       index=df.index)

    return pd.Panel(out)
예제 #2
0
 def plot(self):
     # Data #
     self.frame = self.parent.taxa_table.apply(lambda x: 100*x/x.sum(), axis=1)
     # Take only the ones we use #
     self.samples = [s for s in self.parent.samples if s.short_name in self.parent.taxa_table.index]
     # Maybe drop tributaries or main river #
     if self.tributary: self.samples = [s for s in self.samples if s.info['Tributary']==self.tributary]
     # Sort #
     key = lambda s: (-int(s.info['Tributary']), s.info['Filter_fraction'], s.short_name)
     self.samples = sorted(self.samples, key=key)
     self.frame = self.frame.reindex(index=[s.short_name for s in self.samples])
     # Colors #
     colors = brewer2mpl.get_map('Set1', 'qualitative', 8).mpl_colors
     colors.reverse()
     colors += brewer2mpl.get_map('Set2', 'qualitative', 8).mpl_colors
     colors += brewer2mpl.get_map('Set3', 'qualitative', 8).mpl_colors
     # Plot #
     fig = pyplot.figure()
     axes = self.frame.plot(kind='bar', stacked=True, color=colors)
     fig = pyplot.gcf()
     # Other #
     axes.set_title('Species relative abundances per sample (blasting against "%s" database)' % self.parent.taxonomy.database)
     axes.set_ylabel('Relative abundances in percent')
     axes.xaxis.grid(False)
     axes.yaxis.grid(False)
     axes.set_ylim([0,100])
     # Put a legend below current axis
     axes.legend(loc='upper center', bbox_to_anchor=(0.5, -0.10), fancybox=True, shadow=True, ncol=5)
     # Save it #
     self.save_plot(fig, axes, width=24.0, height=14.0, bottom=0.20, top=0.97, left=0.04, right=0.98)
     pyplot.close(fig)
예제 #3
0
def make_ssps(targetSN, w1, w2):
    """ Make stellar kinematics"""
    tables = []
    for i in range(len(targetSN)):
        tables.append("ssps_sn{0}_w{1}_{2}_TMJ10ext.txt".format(targetSN[i],
                                                               w1, w2))
    columns = np.array([1,4,7,10])
    # Calculating limits
    lims = []
    pers = [15, 20, 2, 15]
    for j,i in enumerate(columns):
        files = [os.path.join(data_dir, "combined_{0}".format(field), tab)
                 for tab, field in zip(tables, fields)]
        a = np.concatenate([np.loadtxt(fname, usecols=(i,)) for fname in files])
        q1, q2 = np.percentile(a[np.isfinite(a)], [pers[j], 100 - pers[j]])
        lims.append([q1, q2])
    # lims[0] = [8, 11]
    # lims[1] = [-0.45, 0.36]
    # lims[2] = [-0.08, 0.42]
    # lims[3] = [-0.71, 0.10]
    cmaps = [brewer2mpl.get_map('Reds', 'sequential', 6).mpl_colormap,
             brewer2mpl.get_map('Blues', 'sequential', 6).mpl_colormap,
             brewer2mpl.get_map('Greens', 'sequential', 6).mpl_colormap,
             brewer2mpl.get_map('Oranges', 'sequential', 6).mpl_colormap]
    labels = ["Age (Gyr)", "[Z/H]", "[$\\alpha$/Fe]", "[Fe/H]"]
    cb_fmts = ["%.1f", "%.2f", "%.2f", "%.2f"]
    yloss = [0.6, 0.6, 0.6, 0.6]
    names = ["age", "metal", "alpha", "iron"]
    plot(targetSN, 4500, 5500, tables, names, columns, lims, cmaps, labels,
         cb_fmts, yloss, cbbox="regular")
    return
예제 #4
0
def test_get_map_reverse():
    bmap = brewer2mpl.get_map('Greens', 'Sequential', 8)
    bmap_r = brewer2mpl.get_map('Greens', 'Sequential', 8, reverse=True)

    assert bmap.type == bmap_r.type
    assert bmap.name + '_r' == bmap_r.name
    assert bmap.number == bmap_r.number
    assert bmap.colors == reverse(bmap_r.colors)
예제 #5
0
파일: base.py 프로젝트: rrane/jcvi
def prettyplot():
    import brewer2mpl

    # Get Set2 from ColorBrewer, a set of colors deemed colorblind-safe and
    # pleasant to look at by Drs. Cynthia Brewer and Mark Harrower of Pennsylvania
    # State University. These colors look lovely together, and are less
    # saturated than those colors in Set1. For more on ColorBrewer, see:
    # - Flash-based interactive map:
    #     http://colorbrewer2.org/
    # - A quick visual reference to every ColorBrewer scale:
    #     http://bl.ocks.org/mbostock/5577023
    set2 = brewer2mpl.get_map("Set2", "qualitative", 8).mpl_colors

    # Another ColorBrewer scale. This one has nice "traditional" colors like
    # reds and blues
    set1 = brewer2mpl.get_map("Set1", "qualitative", 9).mpl_colors

    # Set some commonly used colors
    almost_black = "#262626"
    light_grey = np.array([float(248) / float(255)] * 3)

    reds = mpl.cm.Reds
    reds.set_bad("white")
    reds.set_under("white")

    blues_r = mpl.cm.Blues_r
    blues_r.set_bad("white")
    blues_r.set_under("white")

    # Need to 'reverse' red to blue so that blue=cold=small numbers,
    # and red=hot=large numbers with '_r' suffix
    blue_red = brewer2mpl.get_map("RdBu", "Diverging", 11, reverse=True).mpl_colormap
    green_purple = brewer2mpl.get_map("PRGn", "diverging", 11).mpl_colormap
    red_purple = brewer2mpl.get_map("RdPu", "Sequential", 9).mpl_colormap

    # Default "patches" like scatterplots
    mpl.rcParams["patch.linewidth"] = 0.75  # edge width in points

    # Default empty circle with a colored outline
    mpl.rcParams["patch.facecolor"] = "none"
    mpl.rcParams["patch.edgecolor"] = set2[0]

    # Change the default axis colors from black to a slightly lighter black,
    # and a little thinner (0.5 instead of 1)
    mpl.rcParams["axes.edgecolor"] = almost_black
    mpl.rcParams["axes.labelcolor"] = almost_black
    mpl.rcParams["axes.linewidth"] = 0.5

    # Make the default grid be white so it "removes" lines rather than adds
    mpl.rcParams["grid.color"] = "white"

    # change the tick colors also to the almost black
    mpl.rcParams["ytick.color"] = almost_black
    mpl.rcParams["xtick.color"] = almost_black

    # change the text colors also to the almost black
    mpl.rcParams["text.color"] = almost_black
    return almost_black, blues_r, reds, blue_red, set1, set2, light_grey, green_purple, red_purple
예제 #6
0
파일: cyto_utils.py 프로젝트: gieses/CytAna
def get_color_map(name, map_type, number, reverse=False, get_map=False):
    """
    Parameters
    ----------
    name : str
        Name of color map. Use `print_maps` to see available color maps.
    map_type : {'Sequential', 'Diverging', 'Qualitative'}
        Select color map type.
    number : int
        Number of defined colors in color map.
    reverse : bool, optional
        Set to True to get the reversed color map.

    All colors:
    Sequential
    Blues     :  {3, 4, 5, 6, 7, 8, 9}
    BuGn      :  {3, 4, 5, 6, 7, 8, 9}
    BuPu      :  {3, 4, 5, 6, 7, 8, 9}
    GnBu      :  {3, 4, 5, 6, 7, 8, 9}
    Greens    :  {3, 4, 5, 6, 7, 8, 9}
    Greys     :  {3, 4, 5, 6, 7, 8, 9}
    OrRd      :  {3, 4, 5, 6, 7, 8, 9}
    Oranges   :  {3, 4, 5, 6, 7, 8, 9}
    PuBu      :  {3, 4, 5, 6, 7, 8, 9}
    PuBuGn    :  {3, 4, 5, 6, 7, 8, 9}
    PuRd      :  {3, 4, 5, 6, 7, 8, 9}
    Purples   :  {3, 4, 5, 6, 7, 8, 9}
    RdPu      :  {3, 4, 5, 6, 7, 8, 9}
    Reds      :  {3, 4, 5, 6, 7, 8, 9}
    YlGn      :  {3, 4, 5, 6, 7, 8, 9}
    YlGnBu    :  {3, 4, 5, 6, 7, 8, 9}
    YlOrBr    :  {3, 4, 5, 6, 7, 8, 9}
    YlOrRd    :  {3, 4, 5, 6, 7, 8, 9}
    Diverging
    BrBG      :  {3, 4, 5, 6, 7, 8, 9, 10, 11}
    PRGn      :  {3, 4, 5, 6, 7, 8, 9, 10, 11}
    PiYG      :  {3, 4, 5, 6, 7, 8, 9, 10, 11}
    PuOr      :  {3, 4, 5, 6, 7, 8, 9, 10, 11}
    RdBu      :  {3, 4, 5, 6, 7, 8, 9, 10, 11}
    RdGy      :  {3, 4, 5, 6, 7, 8, 9, 10, 11}
    RdYlBu    :  {3, 4, 5, 6, 7, 8, 9, 10, 11}
    RdYlGn    :  {3, 4, 5, 6, 7, 8, 9, 10, 11}
    Spectral  :  {3, 4, 5, 6, 7, 8, 9, 10, 11}
    Qualitative
    Accent    :  {3, 4, 5, 6, 7, 8}
    Dark2     :  {3, 4, 5, 6, 7, 8}
    Paired    :  {3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
    Pastel1   :  {3, 4, 5, 6, 7, 8, 9}
    Pastel2   :  {3, 4, 5, 6, 7, 8}
    Set1      :  {3, 4, 5, 6, 7, 8, 9}
    Set2      :  {3, 4, 5, 6, 7, 8}
    Set3      :  {3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

    """
    if get_map is True:
        return(brewer2mpl.get_map(name, map_type, number).mpl_colormap)
    else:
        return(brewer2mpl.get_map(name, map_type, number).mpl_colors)
예제 #7
0
파일: mplib.py 프로젝트: jowr/jopy
 def _register_color_lists(self, length=default_lst):
     cc = mpl.colors.ColorConverter()
     self._color_lists['matplotlib'] = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
     self._color_lists['simple']     = ['#FF0000', '#FFD300', '#3914AF', '#00CC00']
     self._color_lists['brewer']     = ['#e66101', '#fdb863', '#b2abd2', '#5e3c99']
     for i in self._color_lists:
         self._color_lists[i] = [cc.to_rgb(item) for item in self._color_lists[i]]
     self._color_lists['cblind']     = [(0.9,0.6,0.0), (0.35,0.70,0.90), (0.00,0.60,0.50), (0.00,0.45,0.70), (0.80,0.40,0.00), (0.80,0.60,0.70)]
     self._color_lists['brewer1']    = brewer2mpl.get_map('Set1', 'qualitative', length).mpl_colors
     self._color_lists['brewer2']    = brewer2mpl.get_map('Set2', 'qualitative', length).mpl_colors
예제 #8
0
    def __init__(self, colormap, alpha = 1):
        self.colormap = colormap
        self.set2 = colormap.mpl_colors
        self.alpha = alpha
        # Another ColorBrewer scale. This one has nice "traditional" colors like
        # reds and blues
        self.set1 = brewer2mpl.get_map('Set1', 'qualitative', 9).mpl_colors
        mpl.rcParams['axes.color_cycle'] = self.set2

        # Set some commonly used colors
        self.almost_black = '#262626'
        self.light_grey = np.array([float(248) / float(255)] * 3)

        reds = mpl.cm.Reds
        reds.set_bad('white')
        reds.set_under('white')

        blues_r = mpl.cm.Blues_r
        blues_r.set_bad('white')
        blues_r.set_under('white')

        # Need to 'reverse' red to blue so that blue=cold=small numbers,
        # and red=hot=large numbers with '_r' suffix
        self.blue_red = brewer2mpl.get_map('RdBu', 'Diverging', 11,
                                      reverse=True).mpl_colormap

        # Default "patches" like scatterplots
        mpl.rcParams['patch.linewidth'] = 0.75     # edge width in points

        # Default empty circle with a colored outline
        mpl.rcParams['patch.facecolor'] = 'none'
        mpl.rcParams['patch.edgecolor'] = self.set2[0]

        # Change the default axis colors from black to a slightly lighter black,
        # and a little thinner (0.5 instead of 1)
        mpl.rcParams['axes.edgecolor'] = self.almost_black
        mpl.rcParams['axes.labelcolor'] = self.almost_black
        mpl.rcParams['axes.linewidth'] = 0.5

        # Make the default grid be white so it "removes" lines rather than adds
        mpl.rcParams['grid.color'] = 'white'

        # change the tick colors also to the almost black
        mpl.rcParams['ytick.color'] = self.almost_black
        mpl.rcParams['xtick.color'] = self.almost_black

        # change the text colors also to the almost black
        mpl.rcParams['text.color'] = self.almost_black

        plt.rc('text', usetex=True)
        plt.rc('font', family='serif')
        plt.rc('font', size='16')
예제 #9
0
def colors(tables):
    try:
        import brewer2mpl
        allcolors1=brewer2mpl.get_map('Set1', 'Qualitative',7).mpl_colors
        allcolors2=brewer2mpl.get_map('Set2', 'Qualitative',6).mpl_colors
        allcolors=allcolors1+allcolors2
    except:
        allcolors=['b','g','r','c','m','y','k','w']

    while len(tables)> len(allcolors):
        allcolors+=allcolors

    return allcolors[:len(tables)]
예제 #10
0
	def do_for_dataset(data, data_name):
		transformed_data, pca, components  = calculate_pca(data, n_components=3, normalize = True)
		colormap = brewer2mpl.get_map('RdBu', 'diverging', 4, reverse=True)
		filename = figure_save_path + dataset + '_issue_26_1_%s_PCA_3components.png'%data_name
		print "Making scatter plot of PCA decompositions of %s data for dataset %s"%(data_name, dataset)
		make_color_grouped_scatter_plot(data_frame=transformed_data, x_name='d1', y_name='d2', color_by='d3', filename=filename, colormap=colormap)
		
		kmeans = KMeans(n_clusters = n_clusters)
		kmeans.fit(transformed_data.values)
		colormap = brewer2mpl.get_map('Set2', 'Qualitative', n_clusters, reverse=True)
		filename = figure_save_path + dataset + '_issue_26_2_%s_clusters_in_PCA_space.png'%data_name
		print "Making scatter plot of K-means clusters of %s data for dataset %s"%(data_name, dataset)
		make_scatter_plot_for_labelled_data(data_frame=transformed_data, x_name='d1', y_name='d2', labels=kmeans.labels_, filename=filename, colormap = colormap, legend=True)
	def _colourise_heatmap(self,colourscheme,maxColours):
		""" by this stage we already have a regions_merged structure with the depth. This method turns those depths into RGB calls using colorbrewer
		"""
		## what's the maximum depth?
		maxRec = max( [ max( [int(y['depth']) for y in self.regions_merged[taxa] ] ) for taxa in self.regions_merged ] )
		## what scheme are we employing?
		if colourscheme in ['BrBG','PRGn','PiYG','PuOr','RdBu','RdGy','RdYlBu','RdYlGn','Spectral']:
			category="Diverging"
			maxCol = 11
			reverse = True
		elif colourscheme in ['Blues','BuGn','BuPu','GnBu','Greens','Greys','OrRd','Oranges','PuBu','PuBuGn','PuRd','Purples','RdPu','Reds','YlGn','YlGnBu','YlOrBr','YlOrRd']:
			category="Sequential"
			maxCol = 9
			reverse = False
		else:
			print "Invalid ColorBrewer scheme. Tabfile will not have any colours."
			return 1

		if maxColours < maxCol: maxCol = maxColours
		if maxRec < 3: maxRec = 3 ## brewer can't handle only 2

		# create depth -> RGB maps
		depth2RGB = {}
		if maxRec <= maxCol: ## easy! 1-1 map
			print "Colorising heatmap using the Colour Brewer scheme {} with {} colours for {} levels".format(colourscheme,maxRec,maxRec)
			brewerMap = brewer2mpl.get_map(colourscheme,category,maxRec,reverse=reverse).colors
			for x in xrange(1,maxRec+1):
				depth2RGB[x] = brewerMap[x-1]
		else:
			print "Colorising heatmap using the Colour Brewer scheme {} with {} colours for {} levels by binning all recombinations >= {} into the top colour".format(colourscheme,maxCol,maxRec,maxCol)
			## we will just use the max num of categories here!
			levels_per_colour = int(ceil(float(maxRec)/maxCol))
			brewerMap = brewer2mpl.get_map(colourscheme,category,maxCol,reverse=reverse).colors
			# depth,brewerLevel = 0,0
			# while depth <= maxRec:
			# 	for x in xrange(0,levels_per_colour):
			# 		depth+=1 ## starts at 1
			# 		depth2RGB[depth] =  brewerMap[brewerLevel]
			# 	brewerLevel+=1 ## starts at 0

			for depth in xrange(1,maxRec+1):
				if depth<maxCol:
					depth2RGB[depth] =  brewerMap[depth-1]
				else:
					depth2RGB[depth] =  brewerMap[maxCol-1]


		## apply it to the regions_merged
		for taxa in self.regions_merged:
			for idx,x in enumerate(self.regions_merged[taxa]):
				self.regions_merged[taxa][idx]['RGB'] = depth2RGB[ x['depth'] ]
예제 #12
0
def testFilterLinearCoh(n_iterations=5):
    cohs = np.array([-0.512, -0.256, -0.128, -0.064, -0.032,
                     0, 0.032, 0.064, 0.128, 0.256, 0.512])

    inputs = []
    for coh in cohs:
        for j in xrange(n_iterations):
            seed = [np.random.randint(999), np.random.randint(999)]
            if np.sign(coh) == 1:
                direction = 0
            else:
                direction = 180
            inputs.append(
                gd.generateDots(seed, 0, 0, 5, 60, direction, np.abs(coh), 5))
    pool = multiprocessing.Pool()
    out = pool.map(filterDots, inputs)
    pool.close()

    data = np.array(out)
    summed_me = np.sum(data, axis=1)
    mean_me = np.mean(summed_me.reshape(-1, n_iterations), axis=1)

    fig = plt.figure()
    ax = fig.add_subplot(111)
    colors = b2mpl.get_map('Set1', 'Qualitative', 3).mpl_colors
    ax.add_line(plt.Line2D(cohs, mean_me, color=colors[0]))
    ax.set_xlabel('Motion strength (proportion coh)')
    ax.set_ylabel('Total motion energy')
    ax.set_xlim(-.61, .61)
    ax.set_ylim(np.min(mean_me) * 1.2, np.max(mean_me) * 1.2)
    sns.despine(offset=5, trim=True)
    plt.tight_layout()

    return cohs, mean_me
예제 #13
0
def get_colors(nodes):
    n_colors = 8
    colors = {}
    set2 = brewer2mpl.get_map('Dark2', 'qualitative', n_colors).hex_colors
    for i, node in enumerate(nodes):
        colors[node] = set2[i % n_colors]
    return colors
예제 #14
0
def plotTimeseries(ts,myvar):

    ts_annual = ts.resample("A")
    ts_quarterly = ts.resample("Q")
    ts_monthly = ts.resample("M")

    # Write data to file
    mypath="%s_annualaverages.csv"%(myvar)
    if os.path.exists(mypath):os.remove(mypath)
    ts.to_csv(mypath)
    print "Wrote timeseries to file: %s"%(mypath)

    red_purple = brewer2mpl.get_map('RdPu', 'Sequential', 9).mpl_colormap
    colors = red_purple(np.linspace(0, 1, 12))
    fig = plt.figure(figsize=(6,6))
    ax = fig.add_subplot(111)
   # for mymonth in xrange(12):
        #ts[(ts.index.month==mymonth+1)].plot(marker='o', color=colors[mymonth],markersize=5, linewidth=0,alpha=0.8)
    #hold(True)
    ts_annual.plot(marker='o', color="#FA9D04", linewidth=0,alpha=1.0, markersize=7, label="Annual")
    remove_border(top=False, right=False, left=True, bottom=True)
    #ts_monthly.plot(style="r", marker='o', linewidth=1,label="Monthly")

    # legend(loc='best')
    ylabel(r'Light (W m$^{-2})$')

    plotfile='figures/timeseries_'+str(myvar)+'.pdf'
    plt.savefig(plotfile,dpi=300,bbox_inches="tight",pad_inches=0)
    print 'Saved figure file %s\n'%(plotfile)
    plt.show()
예제 #15
0
def struct_to_fig(data):
    for a in data:
        K = int(data[a]['assn'].shape[0])
        bmap = brewer2mpl.get_map('Set3', 'qualitative', K)
        colors = bmap.mpl_colors
        width=1
        xcoord = np.arange(len(data[a]['ids'])*width, step=width)
        bars=[]

        xcoord = np.arange(len(data[a]['ids'])*width, step=width)
        for i, (c, ki) in enumerate(zip(colors,data[a]['assn'])):
            if i ==0:
                bars.append( plt.bar(left=xcoord, height=ki, width=width, color=c, edgecolor='none') )
            else:
                bottom = np.sum(data[a]['assn'][:i,:], axis=0)
                bars.append( plt.bar(left=xcoord, height=ki, width=width, color=c, bottom=bottom, edgecolor='none') )

        plt.title('Structure Plot, K='+str(K))
        plt.grid(False)
        plt.xlim([0,xcoord[-1]+width])
        plt.ylim([0,1])
        plt.xticks(xcoord+width/2., data[a]['ids'], rotation=90, fontsize=6)
        plt.tick_params(axis='x', which='both', bottom='on', top='off', direction='out')
        plt.tick_params(axis='y', which='both', left='off', right='off', labelleft='off')

        plt.show()
예제 #16
0
def esat_comparison_plot(t=_np.linspace(173.15, 373.15, 20), std="Hyland_Wexler", percent=True, log=False):
    import pylab
    import brewer2mpl

    methods = list(esat(0, method="return"))
    methods.remove(std)
    print len(methods)
    pylab.rcParams.update({"axes.color_cycle": brewer2mpl.get_map("Paired", "qualitative", 12).mpl_colors})
    y = esat(t, method=std, info=False)
    i = 0
    style = "-"
    for im in methods:
        if i > 11:
            style = "--"
        print im
        if percent:
            pylab.plot(t, 100.0 * (esat(t, method=im, info=False) - y) / y, lw=2, ls=style)
        else:
            pylab.plot(t, esat(t, method=im, info=False) - y, lw=2, ls=style)
        i += 1
    pylab.legend(methods, loc="upper right", fontsize=8)
    pylab.xlabel("Temperature [K]")
    if percent:
        # pylab.semilogy()
        pylab.ylabel("Water Vapor Pressure Difference [%]")
    else:
        pylab.ylabel("Water Vapor Pressure [Pa]")
    pylab.title("Comparison of Water Vapor Calculations Ref:" + std)
    pylab.xlim(_np.round(t[0]), _np.round(t[-1]))
    pylab.grid()
    pylab.axvline(x=273.15, color="k")
    if log:
        pylab.yscale("log")
예제 #17
0
def data(levels=5,depth=20):
    '''return list contour paths calculated from the sum file'''
    global file_count,sum_files
    sum_files()
    paths,matplotlib_colors,legend = get_contour_paths('data/_sumfile.csv',depth)
    n=levels
    colors = brewer2mpl.get_map('YlOrBr', 'Sequential', n)
    result_list = [] 
    hexcolors = colors.hex_colors
    m = len(hexcolors)
    result_list = []

    for i in range(n):
        

	keyval = (float(legend[i])/60.)/file_count
        working_dict = {'strokeColor':almostblack,
	        'fillColor':hexcolors[i],
	        'fillOpacity':0.4,
	        'keyValue':'%0.0f min' % keyval,
	        'polygon':list()}
	tmp = list() #all polygons of this level are in the same set
	for n,path in enumerate(paths[i]):
	        p =  map(list,path)
	        tmp.append(p)
	if i != 0:
	    #cut out the hole for the layer before it also
	    for n,path in enumerate(paths[i-1]):
	        p =  map(list,path)
	        p.reverse()
	        tmp.append(p)
	
	working_dict['polygon'].append(tmp)
	result_list.append(working_dict)
    return result_list
예제 #18
0
def get_series(values, intervals, color, color_type, reverse=False):
    classification_values = []
    for v in values:
        classification_values.append(float(v['freq']))

    classes = classify_values(classification_values, intervals)
    #bmap = brewer2mpl.get_map('RdYlGn', 'Diverging', 9, reverse=True)
    bmap = brewer2mpl.get_map(color, color_type, intervals+1, reverse=reverse)
    colors = bmap.hex_colors

    # creating series
    series = []
    for color in colors:
        #print color
        series.append({
            "color": color,
            "data" : []
        })

    #print classes
    for v in values:
        freq = v['freq']
        for i in range(len(classes)):
            if freq <= classes[i]:
                series[i]['data'].append([float(v['data'][0]), float(v['data'][1])])
                break
    return series
예제 #19
0
def fig_boxplotcomparison(regular, phospho, sheet, column, bounds, figpath):
    """
    Figure 2c from Aurora paper
    """
    #%%
    xvals = []
    yvals = []
    for i, j in zip([regular[sheet], regular[column], phospho[sheet].values, phospho[column].values], ["peptides", "norm_peptides", "phospho", "norm_phospho"]):
        xvals.extend(i)
        yvals.extend([j]*len(i))

    df = pd.DataFrame([xvals, yvals]).transpose()
    df.columns = [column, "peptide type"]
    #%%
    lower_bound = bounds[0]
    upper_bound = bounds[1]
    bmap = brewer2mpl.get_map('Paired', 'Qualitative', 6).mpl_colors
    f, ax = plt.subplots(1, figsize=(11.69, 8.27))
    sns.boxplot(x="peptide type", y=column, data=df, palette=bmap)
    ax.set(xticks=[0, 1, 2, 3],
           xticklabels=["peptides", "peptides \n(normalized)", "phospho-\npeptides", "phosphopeptides \n(normalized)"])
    ax.set(ylim=(-2, 2), ylabel="log2 (fold change)", title=column)
    ax.axhline(lower_bound, ls="--", lw=2, color="red", alpha=0.7)
    ax.axhline(upper_bound, ls="--", lw=2, color="red", alpha=0.7)
    ax.yaxis.set_major_locator(MaxNLocator(4))
    sns.despine()
    cutils.save_fig(f, figpath+"162_BoxplotNorm_{}".format(column))
예제 #20
0
파일: Runner.py 프로젝트: gaow/SEQLinkage
def heatmap(file, theta_inc, theta_max):
    #env.log("Start ploting heatmap for {} ...".format(file), flush=True)
    if os.path.getsize(file) == 0:
        hinton('{}.png'.format(file))
        return
    lods = []
    with open(file, 'r') as f:
        for line in f.readlines():
            theta,lod = line.split()[-2:]
            if float(theta) >= theta_max:
                continue
            lods.append(lod)
        if max(lods) == min(lods):
            #env.log('Max equals Min for [{}], No real heatmap will be generated.'.format(file))
            hinton('{}.png'.format(file))
            return
        Num=int(round(theta_max/theta_inc))
        lods = np.array(map(float,lods)).reshape((-1,Num))
        chrID = re.search(r'\.chr([0-9XY]+)\.', file).group(1)
        fig, ax = plt.subplots(1)
        ax.set_title('Chromosome {}'.format(chrID))
        ppl.pcolormesh(fig,ax,lods.transpose(),
                       xticklabels=[''] * len(lods),
                       yticklabels=np.round(np.array(range(Num)) * theta_inc,2).tolist(),
                       cmap=brewer2mpl.get_map('Blues', 'Sequential', 9).mpl_colormap)
        fig.savefig('{}.png'.format(file))
예제 #21
0
def main():
    data = generate_series()

    print(mpl.rcParams["axes.color_cycle"])

    # brewer version

    # brewer2mpl.get_map args: set name  set type  number of colors
    bmap = brewer2mpl.get_map("Set1", "qualitative", 9)
    # Change the default colors
    mpl.rcParams["axes.color_cycle"] = bmap.mpl_colors
    plot_data(data)
    print(bmap.mpl_colors)


    # mpl version

    cmap = cm.get_cmap("Set1")
    assert isinstance(cmap, LinearSegmentedColormap)
    print(type(cmap))

    #mpl.rcParams["axes.color_cycle"] =
    plot_data(data)

    plt.show()

    pass
예제 #22
0
def testFilterSpeedBandwidth(n_iterations=5):
    speeds = np.arange(1, 9.5, 0.5)
    inputs = []
    for speed in speeds:
        for j in xrange(n_iterations):
            seed = [np.random.randint(999), np.random.randint(999)]
            inputs.append(
                gd.generateDots(seed, 0, 0, 5, 60, 0, 1, speed))
    pool = multiprocessing.Pool()
    out = pool.map(filterDots, inputs)
    pool.close()

    data = np.array(out)
    summed_me = np.sum(data, axis=1)
    mean_me = np.mean(summed_me.reshape(-1, n_iterations), axis=1)

    fig = plt.figure()
    ax = fig.add_subplot(111)
    colors = b2mpl.get_map('Set1', 'Qualitative', 3).mpl_colors
    ax.add_line(plt.Line2D(speeds, mean_me, color=colors[0]))
    ax.set_xlabel('Motion speed (degrees per s))')
    ax.set_ylabel('Total motion energy')

    majorLocator = MultipleLocator(1)
    ax.xaxis.set_major_locator(majorLocator)

    majorLocator = MultipleLocator(50)
    ax.yaxis.set_major_locator(majorLocator)

    ax.set_xlim(1, 9)
    ax.set_ylim(np.min(mean_me) - 10, np.max(mean_me) + 10)
    sns.despine(offset=5, trim=True)
    plt.tight_layout()

    return speeds, mean_me
예제 #23
0
    def __radd__(self, gg):
        gg = deepcopy(gg)

        if self.type:
            ctype = self.type
        else:
            ctype = "Sequential"
        ctype = _handle_shorthand(ctype)

        if self.palette:
            palette = self.palette
        else:
            palette = _number_to_palette(ctype, 1)
        if isinstance(palette, int):
            palette = _number_to_palette(ctype, palette)

        # Try to get colors
        try:
            color_col = gg.aesthetics.get('color', gg.aesthetics['fill'])
            n_colors = max(gg.data[color_col].nunique(),3)
        except KeyError :
            # If we are neither using 'color' nor 'fill' then assume there is
            # only one color used
            n_colors = 3

        bmap = brewer2mpl.get_map(palette, ctype, n_colors)
        gg.manual_color_list = bmap.hex_colors

        return gg
예제 #24
0
def make_pretty_generation_plot(filename, generations, lines_to_plot, x_axis_name, legend_labels, y_errorbar=None, y_logscale = False, vline_x = [], save_figure = True):
    if y_errorbar:
        assert isinstance(y_errorbar, list), "Please provide a list for error bars"
        assert len(y_errorbar) == len(lines_to_plot), "When plotting error bars you must specify an array of error bars for each line that is plotted"
    

    cmap = brewer2mpl.get_map('Set1', 'qualitative', 9)
    p = Ppl(cmap, alpha=1)
    fig, ax = plt.subplots(1)
    ax.set_xlabel('Generation')
    ax.set_ylabel(x_axis_name)
    if y_logscale: 
        ax.set_yscale('log')
        plt.grid(b=True, which='both', axis='y', color='0.65',linestyle='dashed')
        legend_labels = map(lambda s: s + ' (log)', legend_labels)
    else: ax.yaxis.get_major_formatter().set_powerlimits((0, 1))

    for i, (series, label) in enumerate(zip(lines_to_plot, legend_labels)):
        p.plot(ax, generations, series, linewidth=2, label=label, zorder=2)
    
    if y_errorbar: 
        for i, (series, label) in enumerate(zip(lines_to_plot, legend_labels)):
            ax.errorbar(generations, series, yerr=y_errorbar[i], zorder=1, capsize=2, barsabove=True, ecolor = errorbar_color)
    
    if vline_x:
        for x in vline_x:
            ax.vlines(x = x, ymin = ax.get_ylim()[0], ymax = ax.get_ylim()[1], linewidth = 1, linestyles = 'dashed', alpha = 0.5)

    p.legend(ax, loc=0)
    if save_figure: fig.savefig(filename)
    return fig, ax, filename
예제 #25
0
def issue_21_basic_scatter_plots(dataset):
	"""
	Makes scatter plots of fitness
	"""
	from plotting import make_color_grouped_scatter_plot
	folder = make_issue_specific_figure_folder('21_scatter_plots', dataset)
	fit_data, par_data, gen, ids = IO.load_pickled_generation_dataframe(dataset_name=dataset)
	
	#colormap = brewer2mpl.get_map('YlOrRd', 'Sequential', 9, reverse=True)
	colormap = brewer2mpl.get_map('Spectral', 'Diverging', 9, reverse=True)
	print "Making scatter plots of fitness data for dataset %s"%dataset
	filename = folder + 'a.png'
	make_color_grouped_scatter_plot(data_frame=fit_data, x_name='overshoot', y_name='time_to_reach_new_fundamental', color_by='stdev', filename=filename, colormap = colormap, y_function='log')

	filename = folder + 'b.png'
	make_color_grouped_scatter_plot(data_frame=fit_data, x_name='overshoot', y_name='stdev', color_by='time_to_reach_new_fundamental', filename=filename, colormap = colormap)

	filename = folder + 'c.png'
	make_color_grouped_scatter_plot(data_frame=fit_data, x_name='time_to_reach_new_fundamental', y_name='round_stable', color_by='stdev', filename=filename, colormap = colormap)
	
	filename = folder + 'd.png'
	make_color_grouped_scatter_plot(data_frame=fit_data, x_name='stdev', y_name='round_stable', color_by='time_to_reach_new_fundamental', filename=filename, colormap = colormap, x_function='log', y_function='log')

	filename = folder + 'e.png'
	make_color_grouped_scatter_plot(data_frame=fit_data, x_name='stdev', y_name='time_to_reach_new_fundamental', color_by='round_stable', filename=filename, colormap = colormap, x_function='log', y_function='log')
	
	filename = folder + 'f.png'
	make_color_grouped_scatter_plot(data_frame=fit_data, x_name='time_to_reach_new_fundamental', y_name='stdev', color_by='round_stable', filename=filename, colormap = colormap)

	filename = folder + 'g.png'
	make_color_grouped_scatter_plot(data_frame=fit_data, x_name='time_to_reach_new_fundamental', y_name='stdev', color_by='round_stable', filename=filename, colormap = colormap, x_function='log', y_function='log', color_function='log')
예제 #26
0
def anom_cmap():
    """return a discrete blue-red cmap from colorbrewer"""
    ncols = 11
    cmap_anom = brewer2mpl.get_map('RdBu', 'diverging', ncols,
                                   reverse=True).mpl_colormap
    cmap_anom = discrete_cmap(ncols, cmap_anom)
    return cmap_anom
예제 #27
0
def plot_figures(fignum='2a', direction='-', show=False,
                 T=45., R=0.1, gamma=1.):

    import brewer2mpl
    cmap = brewer2mpl.get_map('Set1', 'qualitative', 9)
    colors = cmap.mpl_colors

    params = {"T": T,
              "R": R,
              "gamma": gamma,
              "loop_type": "Outside_circle"}

    if fignum == '2a':
        settings = {"init_state": 'a',
                    "init_phase": 0,
                    "loop_direction": '+'}
    elif fignum == '2b':
        settings = {"init_state": 'b',
                    "init_phase": 0,
                    "loop_direction": '-'}
    elif fignum == '2c':
        settings = {"init_state": 'b',
                    "init_phase": np.pi,
                    "loop_direction": '-'}

    params.update(settings)
    OM = OptoMech(**params)

    t, cp, cm = OM.solve_ODE()
    Psi = OM.Psi

    # f, (ax1, ax2) = plt.subplots(ncols=2)
    f, ax1 = plt.subplots()

    ax1.semilogy(t, np.abs(Psi[:, 0])**2, ls="-", ms="o",
                 color=colors[0], label=r"$|\alpha_+(t)|^2$")
    ax1.semilogy(t, np.abs(Psi[:, 1])**2, ls="-", ms="o",
                 color=colors[1], label=r"$|\alpha_-(t)|^2$")
    ax1.semilogy(t, np.abs(cp)**2, ls="--",
                 color=colors[2], label=r"$|c_+(t)|^2$")
    ax1.semilogy(t, np.abs(cm)**2, ls="--",
                 color=colors[3], label=r"$|c_-(t)|^2$")
    ax1.legend(loc="lower right")
    ax1.set_xlabel(r"$t$")
    m = [(abs(x)**2).max() for x in Psi, cp, cm]
    ax1.set_ylim(1e-3, max(m))

    omega, g = OM.get_cycle_parameters(t)
    # np.savetxt("parameters_{}.dat".format(fignum), zip(g, omega))

    ax2 = plt.axes([0.2, 0.65, .2, .2])
    ax2.plot(gamma/2, 0, "ko")
    ax2.plot(g, omega, ls="-", color=colors[0])
    ax2.set_xlim(gamma/4, 3/4.*gamma)
    ax2.set_ylim(-gamma/4., gamma/4.)

    if show:
        plt.show()
    else:
        plt.savefig("{}.png".format(fignum))
def plot_integrated_colors(filenames, labels='Z'):
    if type(filenames) is str:
        filenames = [filenames]
        ax = None
        cols = ['k']
    else:
        fig, ax = plt.subplots()
        cols = brewer2mpl.get_map('Spectral', 'Diverging',
                                  len(filenames)).mpl_colors

    if labels == 'Z':
        fmt = '$Z=%.4f$'
        labels = [fmt % float(l.replace('.dat', '').split('Z')[1])
                  for l in filenames]
    else:
        print 'need to fix labels'
        labels = [''] * len(filenames)
    for i, filename in enumerate(filenames):
        data = rsp.fileIO.readfile(filename)
        ycol = 'V-K'
        xcol = 'Age'
        ax = rg.color_color(data, xcol, ycol, xscale='log', ax=ax,
                            plt_kw={'lw': 2, 'color': cols[i],
                                    'label': labels[i]})

    plot_cluster_data(ax)
    ax.legend(frameon=False, loc=0, numpoints=1)
    ax.set_xlabel(r'${\rm %s}$' % xcol, fontsize=20)
    ax.set_ylabel(r'${\rm %s}$' % ycol, fontsize=20)
    plt.tick_params(labelsize=16)
    return ax
예제 #29
0
def get_colors(non_null):
    if 'brewer2mpl' in globals():
        num_color = max(min(len(non_null), 11), 3)
        all_colors = brewer2mpl.get_map('Spectral', 'Diverging', num_color).hex_colors
    else:
        all_colors = None
    return all_colors
def paint_clusters(G1, G2, beacons_G1, beacons_G2, kmeans_labels1, kmeans_labels2):
	accent_colors = brewer2mpl.get_map('Accent', 'qualitative',8).mpl_colors
	plt.subplot(121)
	plt.axis('off')
	plt.title('$G_1$')
	(labels, node_colors, node_sizes) = visualize_beacons(G1, beacons_G1)
	clustered_colors = cluster_colors(G1, beacons_G1, kmeans_labels1, accent_colors)

	pos = nx.spring_layout(G1, weight=None)
	nx.draw_networkx_nodes(G1, pos, node_color=clustered_colors, node_size=node_sizes, font_size=18)
	nx.draw_networkx_labels(G1, pos, font_size=17, labels=labels, font_color = '#262626')
	nx.draw_networkx_edges(G1, pos, width=2, alpha=0.3)


	plt.subplot(122)
	plt.axis('off')
	plt.title('$G_2$')
	(labels, node_colors, node_sizes) = visualize_beacons(G2, beacons_G2)
	clustered_colors = cluster_colors(G2, beacons_G2, kmeans_labels2, accent_colors)

	pos2_init= {key:value for (key, value) in zip(beacons_G2,[pos[beacon] for beacon in beacons_G1])}
	pos2 = nx.spring_layout(G2, weight=None, pos = pos2_init)

	nx.draw_networkx_nodes(G2, pos=pos2, node_color=clustered_colors, node_size=node_sizes, font_size=18)
	nx.draw_networkx_labels(G2, pos=pos2, font_size=17, labels=labels, font_color = '#262626')
	nx.draw_networkx_edges(G2, pos=pos2, width=2, alpha=0.3)
예제 #31
0
import cPickle
import matplotlib.pyplot as plt
import numpy as np
import sys
import brewer2mpl

bmap = brewer2mpl.get_map('Dark2', 'qualitative', 6)
colors = bmap.mpl_colors

colors_config = {
    "hand": colors[1],
    "stick": colors[2],
    "object": colors[4],
}

bmap2 = brewer2mpl.get_map('Dark2', 'qualitative', 4)
colors2 = bmap2.mpl_colors

plt.switch_backend('Agg')

sw = 1


def runningMeanFast(x, sw):
    return np.convolve(x, np.ones((sw, )) / sw, mode="valid")


def n_behavior_change(x, smoothing=10, th=10):
    x = runningMeanFast(x, smoothing)
    l = np.linspace(0, len(x) - 1, 1 + int((len(x) - 1) / smoothing))
    l = np.array(l, dtype=int)
예제 #32
0
    plt.yticks(range(1, 24))
    yticks = range(1, 23)
    yticks.append('X')
    ax.set_yticklabels(yticks)
else:
    plt.yticks(range(1, 23))


#define colors
def hex_to_rgb(value):
    value = value.lstrip('#')
    lv = len(value)
    return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))


bmap = brewer2mpl.get_map('Set1', 'qualitative', 4)
if args.colors is not None:
    colors = []
    color_list = args.colors.split(',')
    [colors.append(x) for x in color_list]
else:
    colors = bmap.mpl_colors
    colors.append((0, 0, 0))

#define centromeres
centro = open(args.centromeres)
centromeres = {}
for line in centro:
    line = line.strip().split()
    if chrX and line[0] == 'X':
        line[0] = '23'
예제 #33
0
import os
import sys
import cPickle
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

import brewer2mpl

bmap = brewer2mpl.get_map('Dark2', 'qualitative', 8)
#colors = bmap.mpl_colors
colors = {
    "motor_babbling": bmap.mpl_colors[0],
    "Hand": bmap.mpl_colors[1],
    "Joystick_L": bmap.mpl_colors[2],
    "Joystick_R": bmap.mpl_colors[3],
    "Ergo": bmap.mpl_colors[4],
    "Ball": bmap.mpl_colors[5],
    "Light": bmap.mpl_colors[6],
    "Sound": bmap.mpl_colors[7]
}

simu = False

if simu:

    # From SIMU
    path = "/home/sforesti/catkin_ws/src/nips2017/logs/"
    experiment_name = "experiment"
    configs = dict(AMB=9)  #, RMB=3, RmB=3, FC=1, OS=3)
    n = 10000
예제 #34
0
def plot():

    cs = palettable.colorbrewer.qualitative.Set1_3.mpl_colors

    # Load parameter file
    with open('/home/lc585/qsosed/input.yml', 'r') as f:
        parfile = yaml.load(f)

    ebvmin = -0.075
    ebvmax = 0.075

    zmin = 1
    zmax = 3.0

    # Load stuff
    fittingobj = load(parfile)
    wavlen = fittingobj.get_wavlen()

    # Load data
    df = get_data()

    df = df[(df.z_HW >= zmin) & (df.z_HW <= zmax)]

    colmin, colmax = [], []

    zs = np.arange(zmin, zmax + 0.025, 0.025)

    mycm = cm.get_cmap('Blues_r')
    mycm.set_under('w')
    cset = brewer2mpl.get_map('Blues', 'sequential', 9).mpl_colors
    mycm = truncate_colormap(mycm, 0.0, 0.8)

    fig = plt.figure(figsize=(figsize(1, vscale=0.8)))
    ax = fig.add_subplot(1, 1, 1)
    plt.tight_layout()

    #histogram definition
    xyrange = [[0., 3.0], [0, 3]]  # data range
    bins = [100, 60]  # number of bins
    thresh = 7  #density threshold

    #data definition

    xdat, ydat = df.z_HW, df.iVEGA - df.KVEGA

    # histogram the data
    hh, locx, locy = histogram2d(xdat, ydat, range=xyrange, bins=bins)
    posx = np.digitize(xdat, locx)
    posy = np.digitize(ydat, locy)

    #select points within the histogram
    ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
    hhsub = hh[posx[ind] - 1,
               posy[ind] - 1]  # values of the histogram where the points are
    xdat1 = xdat[ind][hhsub < thresh]  # low density points
    ydat1 = ydat[ind][hhsub < thresh]
    hh[hh < thresh] = np.nan  # fill the areas with low density by NaNs

    im = ax.imshow(np.flipud(hh.T),
                   cmap=mycm,
                   extent=np.array(xyrange).flatten(),
                   interpolation='none',
                   aspect='auto',
                   vmin=thresh,
                   vmax=45)

    clb = fig.colorbar(im)
    clb.set_label('Number of Objects')
    ax.scatter(xdat1, ydat1, color=cset[-1], s=2)

    plt.ylim(0, 3.5)

    col = []
    for z in zs:

        parfile['ext']['EBV'] = ebvmax

        magtmp, wavlentmp, fluxtmp = model(redshift=z, parfile=parfile)

        col.append(magtmp[3] - magtmp[8])

    plt.plot(zs, col, label='E(B-V) = 0.075', color=cs[0], linewidth=1.0)
    upper_bound_1 = col[np.argmin(np.abs(zs - 1.0)):np.argmin(np.abs(zs -
                                                                     1.5))]
    upper_bound_2 = col[np.argmin(np.abs(zs - 2.0)):np.argmin(np.abs(zs -
                                                                     2.7))]

    col = []
    for z in zs:

        parfile['ext']['EBV'] = ebvmin

        magtmp, wavlentmp, fluxtmp = model(redshift=z, parfile=parfile)

        col.append(magtmp[3] - magtmp[8])

    plt.plot(zs, col, label='E(B-V) = -0.075', color=cs[0], linewidth=1.0)
    lower_bound_1 = col[np.argmin(np.abs(zs - 1.0)):np.argmin(np.abs(zs -
                                                                     1.5))]
    lower_bound_2 = col[np.argmin(np.abs(zs - 2.0)):np.argmin(np.abs(zs -
                                                                     2.7))]

    plt.fill_between(zs[np.argmin(np.abs(zs - 2.0)):np.argmin(np.abs(zs -
                                                                     2.7))],
                     lower_bound_2,
                     upper_bound_2,
                     facecolor='None',
                     edgecolor=cs[0],
                     linewidth=3.0)

    col = []
    for z in zs:

        parfile['ext']['EBV'] = 0.0

        magtmp, wavlentmp, fluxtmp = model(redshift=z, parfile=parfile)

        col.append(magtmp[3] - magtmp[8])

    plt.plot(zs, col, label='E(B-V) = 0.0', color=cs[0], linewidth=1.0)

    plt.xlim(0.75, 3.5)

    plt.text(3.33821,
             2.5,
             'E(B-V)=',
             horizontalalignment='right',
             verticalalignment='center',
             color='black')

    plt.text(3.09608,
             2.2,
             '0.075',
             horizontalalignment='left',
             verticalalignment='center',
             color='black')

    plt.text(3.09608,
             1.2,
             '-0.075',
             horizontalalignment='left',
             verticalalignment='center',
             color='black')

    plt.text(3.09608,
             1.7,
             '0.0',
             horizontalalignment='left',
             verticalalignment='center',
             color='black')

    plt.xlabel(r'Redshift $z$')
    plt.ylabel(r'$i$-$K$')
    plt.tight_layout()

    fig.savefig('/home/lc585/thesis/figures/chapter05/ik_versus_z_low_ext.pdf')
    plt.show()

    return None
예제 #35
0
import glob
from pylab import *
import scipy.stats

import brewer2mpl
bmap = brewer2mpl.get_map('Set2', 'qualitative', 7)
colors = bmap.mpl_colors

params = {
    'axes.labelsize': 8,
    'font.size': 8,
    'legend.fontsize': 10,
    'xtick.labelsize': 10,
    'ytick.labelsize': 10,
    'text.usetex': False,
    'figure.figsize': [2.5, 4.5]
}
rcParams.update(params)


def load(dir):
    f_list = glob.glob(dir + '/*/*/bestfit.dat')
    num_lines = sum(1 for line in open(f_list[0]))
    i = 0
    data = np.zeros((len(f_list), num_lines))
    for f in f_list:
        data[i, :] = np.loadtxt(f)[:, 1]
        i += 1
    return data

예제 #36
0
mpl.rcParams["savefig.transparent"] = True
mpl.rcParams["savefig.bbox"] = "tight"
mpl.rcParams["font.size"] = 10.0
mpl.rcParams["font.sans-serif"] = "Arial"
mpl.rcParams["savefig.format"] = "pdf"
import numpy as np
import pandas as pd
from joblib import Parallel, delayed
from sklearn.cluster import DBSCAN
from sklearn import metrics
from sklearn.datasets.samples_generator import make_blobs
from sklearn.preprocessing import StandardScaler
import pylab
import brewer2mpl
#import seaborn as sns
colors = brewer2mpl.get_map('Set3', 'qualitative', 8).mpl_colors

#own library
from cDBSCAN import cDBSCAN
from kDBSCAN import kDBSCAN


def getDataD(r=1, samples=1000, std1=0.2, centercounts=10, rmax=15):
    centers = []
    for i in xrange(centercounts):
        x, y = np.random.randint(0 - rmax,
                                 rmax), np.random.randint(0 - rmax, rmax)
        centers.append([x, y])
    #data as true clusters
    X, xlabels = make_blobs(n_samples=samples,
                            centers=centers,
예제 #37
0
        h = plt.gca()
        h.set_xlabel('epochs')
        plt.legend(loc='best')

        fig.savefig('{}/{}_{}.png'.format(folder_today, plotx, meas),
                    bbox_inches='tight')

# %% --- Prepapare visual output as in [1] ---
# set line width and style
lwidth = 2
lwidth_help = 2
lstyle = '-'
lstyle_help = '--'

# set colors using colorbrewer
bmap = brewer2mpl.get_map('Paired', 'Qualitative', 5)
colors = bmap.mpl_colors

# set latex options
matplotlib.rc('text', usetex=True)
matplotlib.rcParams['text.latex.preamble'] = [r"\usepackage{amsmath}"]

# set font
fsize = 15
font = {'family': 'serif', 'size': fsize}
matplotlib.rc('font', **font)
matplotlib.rc('axes', labelsize=fsize)  # fontsize of x and y labels
matplotlib.rc('xtick', labelsize=fsize)  # fontsize of xtick labels
matplotlib.rc('ytick', labelsize=fsize)  # fontsize of ytick labels
matplotlib.rc('legend', fontsize=fsize)  # legend fontsize
import time
import numpy as np

np.random.seed(1111)
np.seterr(over="raise")
import cPickle, os

from hips.plotting.layout import create_figure
import matplotlib.pyplot as plt
import brewer2mpl

colors = brewer2mpl.get_map("Set1", "Qualitative", 9).mpl_colors
# goodcolors = np.array([0,1,2,4,6,7,8])
# colors = np.array(colors)[goodcolors]

from pybasicbayes.util.general import ibincount
from pybasicbayes.util.text import progprint_xrange

import pyhawkes.models

reload(pyhawkes.models)

# Set globals
K = 10
B = 3
dt = 1
dt_max = 10.
T = 100.
network_hypers = {
    'C': 1,
    'kappa': 1.,
예제 #39
0
from naive_bayes import BernoulliNB, GaussianNB, GaussianNB2, MultinomialNB, PoissonNB, MixNB, MixNB2
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from matplotlib.mlab import rec_drop_fields
from matplotlib import cm
# import itertools
from inspect import getargspec
from sklearn.grid_search import RandomizedSearchCV
import os
import re
from sklearn import preprocessing
import brewer2mpl
from scipy.stats import itemfreq
paired = brewer2mpl.get_map('Paired', 'qualitative', 10).mpl_colors


# Testing Pipeline:
def testAlgo(clf,
             X,
             y,
             clfName,
             opt=False,
             param_dict=None,
             opt_metric='roc_auc',
             n_iter=50,
             folds=10,
             times=10):
    '''An algorithm that output the perdicted y and real y'''
    y_true = []
예제 #40
0
t_opts = []
t_stars = []
phi_stars = []
#recover the parts
for result in results:
    delta_phi_fs.append(result[0])
    t_opts.append(result[1])
    t_stars.append(result[2])
    phi_stars.append(result[3])

delta_phi_fs = np.vstack(delta_phi_fs).T
t_opts = np.vstack(t_opts).T
phi0s_plt = np.vstack([phi0s] * 200)
np.save('Data/t_opts.npy', t_opts)

palatte = colorbrewer.get_map('Dark2', 'Qualitative', 3)
colors = palatte.mpl_colors

plo.PlotOptions(uselatex=True, ticks='in')
plt.figure(figsize=(3.5, 2.85))
gs = gridspec.GridSpec(3,
                       2,
                       height_ratios=(1, 0.5, 0.5),
                       width_ratios=(1.5, 1))

ax = plt.subplot(gs[0, 0])
ts = np.arange(0, 23.7, 0.01)
ax.plot(times * np.pi / 11.85, prc, color='k')
ax.set_ylabel('ipPRC$(\phi)$')
ax.set_xlabel('Phase $\phi$')
plo.format_2pi_axis(ax)
예제 #41
0
p.add_argument('--output',
               '-o',
               help='Output to file. Otherwise show.',
               nargs='?')
args = p.parse_args()

if args.style:
    try:
        from mpltools import style
        style.use(args.style)
    except ImportError:
        print("Need mpltools for setting styles (pip install mpltools)")

try:
    import brewer2mpl
    all_colors = brewer2mpl.get_map('Paired', 'Qualitative', 12).hex_colors
except ImportError:
    print("Install brewer2mpl for better colors (pip install brewer2mpl)")
    all_colors = (
        'green',
        'orange',
        'red',
        'blue',
        'black',
        'olive',
        'purple',
        '#6960EC',
        '#F0FFFF',
        '#728C00',
        '#827B60',
        '#F87217',
예제 #42
0
파일: plot.py 프로젝트: lyltc1/D2C-2.0
def perfcheck(nstart=0,nend=100,noisemax=100):
    
    bmap = brewer2mpl.get_map('Set2','qualitative', 7)
    colors = bmap.mpl_colors

    params = {
    'axes.labelsize': 15,
    'font.size': 12,
    'legend.fontsize': 15,
    'xtick.labelsize': 20,
    'ytick.labelsize': 20,
    'text.usetex': True ,
    'figure.figsize': [8, 6], # instead of 4.5, 4.5
    'font.weight': 'bold',
    'axes.labelweight': 'bold',
    'ps.useafm' : True,
    'pdf.use14corefonts':True,
    'pdf.fonttype': 42,
    'ps.fonttype': 42
     }
    mpl.rcParams.update(params)

    # y=np.array(np.loadtxt('perfcheck.txt'))
    # perf=np.mean(y,axis=1)
    # cstd=np.std(y,axis=1)
    # step=noisemax/int(perf.shape[0]-1)
    # sind=int(nstart/step)
    # eind=int(nend/step)+1
    # plt.figure(figsize=(12,9))
    # f5,=plt.plot(np.arange(sind,(eind-1)*step+1,step),perf[sind:eind],color=colors[0], linewidth=4)
    # plt.fill_between(np.arange(sind,(eind-1)*step+1,step),(perf[sind:eind]-cstd[sind:eind]),(perf[sind:eind]+cstd[sind:eind]), alpha=0.3, color=colors[0])



    #D2C
    i, x, y = np.loadtxt('swimmer6_testing_data.txt', dtype=np.float64, delimiter=',\t',  unpack=True, usecols=(0,1,2))
    i = 100*i
    plt.figure(1)
    plt.plot(i,x,color=colors[1], linewidth=4)
    #plt.plot(i, (x+y), alpha=0.3, color='orange')
    #plt.plot(i, (x-y), alpha=0.3, color='orange')
    plt.fill_between(i, (x+y), (x-y), alpha=0.2, color=colors[1])

    #plt.xlabel(" Percent of max. control (Std dev of perturbed noise)", fontsize=16)
    #plt.ylabel("Terminal state MSE (Avergaed over {} samples)".format(n_samples), fontsize=16)
    plt.grid(axis='y', color='.910', linestyle='-', linewidth=1.5)
    plt.grid(axis='x', color='.910', linestyle='-', linewidth=1.5)

    #DDPG
    DDPG_testing_data_location = "/media/karthikeya/Elements/DDPG_D2C/results/Swimmer6/exp_4/"
    i, x, y, z, a = np.loadtxt(DDPG_testing_data_location + "data.txt", dtype=np.float64, delimiter=',\t',  unpack=True, usecols=(0,1,2,3,4))
    i = 100*i
    
    plt.plot(i,x,color=colors[2], linewidth=4)
    #plt.plot(i, (x+y), alpha=0.3, color='orange')
    #plt.plot(i, (x-y), alpha=0.3, color='orange')
    plt.fill_between(i, (x+y), (x-y), alpha=0.2, color=colors[2])

    #plt.xlabel(" Percent of max. control (Std dev of perturbed noise)", fontsize=16)
    #plt.ylabel("Terminal state MSE (Avergaed over {} samples)".format(n_samples), fontsize=16)
    plt.grid(axis='y', color='.910', linestyle='-', linewidth=1.5)
    plt.grid(axis='x', color='.910', linestyle='-', linewidth=1.5)
    #plt.tight_layout()
    plt.legend(['D2C','DDPG'], loc='upper left', fontsize='xx-large')
    
    plt.xlabel('Std dev of perturbed noise (Percent of max. control)', fontweight='bold',fontsize=22)
    plt.ylabel('L2-norm of terminal state error', fontweight='bold', fontsize=25)
    plt.grid(True)
    plt.show()  
    print('averaged by {value1} rollouts'.format(value1=y.shape[1]))
예제 #43
0
 lick3311 = lick[:, idx3311]
 lickhalo = lick[:, idxhalo]
 errs1_3311 = lickerr[:, idx3311]
 errs_halo = lickerr[:, idxhalo]
 #########################################################################
 # First figure, simple indices
 app = "_pa" if restrict_pa else ""
 mkfig1 = True
 gray = "0.75"
 ##########################################################################
 lims, ranges = get_model_lims(
     os.path.join(tables_dir, "models_thomas_2010_metal_extrapolated.dat"))
 idx = np.array([12, 13, 16, 17, 18, 19, 20])
 lims = lims[idx]
 # Setting the colormap properties for the scatter plots
 cmap = brewer2mpl.get_map('Blues', 'sequential', 9).mpl_colormap
 cmap = nc.cmap_discretize(cmap, 3)
 color = cm.get_cmap(cmap)
 norm = Normalize(vmin=0, vmax=45)
 if mkfig1:
     plt.figure(1, figsize=(6, 14))
     gs = gridspec.GridSpec(7, 1)
     gs.update(left=0.15,
               right=0.95,
               bottom=0.1,
               top=0.94,
               wspace=0.1,
               hspace=0.09)
     tex = []
     for j, ll in enumerate(lick):
         # print indices[j], ranges[j], ssp.fn(9.,0.12,.4)[ii[j]]
예제 #44
0
def get_colors():
    """
    Get colorbrewer colors, which are nicer
    """
    bmap = brewer2mpl.get_map('Accent', 'qualitative', 6)
    return bmap.mpl_colors
예제 #45
0
if 'data_contacts' not in globals(): 
	sns_contacts,(data_contacts,calc_contacts) = work.sns(),plotload('contacts',work)
	sns_hbonds,(data_hbonds,calc_hbonds) = work.sns(),plotload('hydrogen_bonding',work)
	if sns_hbonds!=sns_contacts: 
		raise Exception('collections for hydrogen_bonding and contacts are not equal')
	else: sns = sns_hbonds
	#---set the cutoff in the yaml file to make this plot because we pull from multiple upstream sources
	cutoff = calc_contacts['calcs']['specs']['cutoff']
	#---specify the upstream data and naming scheme
	rowspec = ['subject_resname','subject_resid','subject_atom',
		'target_resname','target_resid','target_atom']
	#---common colors
	import brewer2mpl
	colors = {'DOPC':'blue','DOPS':'red','POP2':'magenta','PI2P':'magenta',
		'all lipids':'black','DOPE':'blue'}
	colors.update(**dict([(sn,brewer2mpl.get_map('Set1','qualitative',9).mpl_colors[sns.index(sn)]) 
		for sn in sns]))
	lipid_label = lambda x: dict([(i,'$$\mathrm{{PIP}_{2}}$$') 
		for i in work.vars.get('selectors',{}).get('resnames_PIP2',{})]).get(x,x)
	sn_title = lambda sn: '%s%s'%(work.meta[sn].get('label',re.sub('_','-',sn)),
		'' if not work.meta[sn].get('cholesterol',False) else '\n(cholesterol)')
	#---post-post processing
	if 'postdat' not in globals(): 
		salt_bridge_filter()
		hydrogen_bond_compactor()
		postdat = make_postdat()

#---block: main plotting loop (see settings in the header)
for bond in bond_mappings:
	for specname,spec in specs.items():
		this_plotspec = dict(plotspec)
예제 #46
0
# Built-in modules #
import os, time, getpass

# Internal modules #
from common import split_thousands
from autopaths import FilePath

# Third party modules #
import matplotlib, brewer2mpl
from matplotlib import pyplot

# Constants #
cool_colors = brewer2mpl.get_map('Set1', 'qualitative', 8).mpl_colors
cool_colors.reverse()
cool_colors += brewer2mpl.get_map('Set2', 'qualitative', 8).mpl_colors
cool_colors += brewer2mpl.get_map('Set3', 'qualitative', 8).mpl_colors
cool_colors += brewer2mpl.get_map('Pastel1', 'qualitative', 8).mpl_colors
cool_colors += brewer2mpl.get_map('Pastel2', 'qualitative', 8).mpl_colors
cool_colors += brewer2mpl.get_map('Greys', 'sequential', 8).mpl_colors


################################################################################
class Graph(FilePath):
    width = 12.0
    height = 7.0
    bottom = 0.14
    top = 0.93
    left = 0.06
    right = 0.98
    formats = ('pdf', )
예제 #47
0
def plot():

    tab = Table.read('/data/lc585/QSOSED/Results/141203/sample1/out_add.fits')
    tab = tab[tab['BBT_STDERR'] < 200.0]
    tab = tab[tab['BBFLXNRM_STDERR'] < 0.05]
    tab = tab[tab['CHI2_RED'] < 3.0]
    tab = tab[tab['LUM_IR'] > 40.0]  # just gets rid of one annoying point

    mycm = cm.get_cmap('YlOrRd_r')
    mycm.set_under('w')
    cset = brewer2mpl.get_map('YlOrRd', 'sequential', 9).mpl_colors
    mycm = truncate_colormap(mycm, 0.0, 0.8)

    set_plot_properties()  # change style

    fig = plt.figure(figsize=figsize(1.5, 0.7))

    gs = gridspec.GridSpec(9, 13)

    ax1 = fig.add_subplot(gs[1:5, 0:4])
    ax2 = fig.add_subplot(gs[5:, 0:4])
    ax3 = fig.add_subplot(gs[0, 0:4])

    ax4 = fig.add_subplot(gs[1:5, 4:8])
    ax5 = fig.add_subplot(gs[5:, 4:8])
    ax6 = fig.add_subplot(gs[0, 4:8])

    ax7 = fig.add_subplot(gs[1:5, 8:12])
    ax8 = fig.add_subplot(gs[5:, 8:12])
    ax9 = fig.add_subplot(gs[0, 8:12])

    ax10 = fig.add_subplot(gs[1:5, 12])
    ax11 = fig.add_subplot(gs[5:, 12])

    fig.subplots_adjust(wspace=0.0)
    fig.subplots_adjust(hspace=0.0)

    #histogram definition
    xyrange = [[44, 48], [0, 2]]  # data range
    bins = [80, 40]  # number of bins
    thresh = 4  #density threshold

    #data definition
    xdat, ydat = tab['LUM_UV'], tab['RATIO_IR_UV']

    # histogram the data
    hh, locx, locy = histogram2d(xdat, ydat, range=xyrange, bins=bins)
    posx = np.digitize(xdat, locx)
    posy = np.digitize(ydat, locy)

    #select points within the histogram
    ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
    hhsub = hh[posx[ind] - 1,
               posy[ind] - 1]  # values of the histogram where the points are
    xdat1 = xdat[ind][hhsub < thresh]  # low density points
    ydat1 = ydat[ind][hhsub < thresh]
    hh[hh < thresh] = np.nan  # fill the areas with low density by NaNs

    im = ax1.imshow(np.flipud(hh.T),
                    cmap=mycm,
                    extent=np.array(xyrange).flatten(),
                    interpolation='none',
                    aspect='auto',
                    vmin=thresh,
                    vmax=45)

    ax1.scatter(xdat1, ydat1, color=cset[-1])

    ax1.set_ylabel(r'$R_{{\rm NIR}/{\rm UV}}$', fontsize=14)

    ax1.set_ylim(0., 2.)
    ax1.set_xlim(45.25, 47)

    ax3.hist(tab['LUM_UV'], color=cset[-1], bins=20)
    ax3.set_xlim(ax1.get_xlim())
    ax3.set_axis_off()

    #histogram definition
    xyrange = [[44, 48], [200, 2000]]  # data range
    bins = [90, 35]  # number of bins
    thresh = 4  #density threshold

    #data definition
    xdat, ydat = tab['LUM_UV'], tab['BBT']

    # histogram the data
    hh, locx, locy = histogram2d(xdat, ydat, range=xyrange, bins=bins)
    posx = np.digitize(xdat, locx)
    posy = np.digitize(ydat, locy)

    #select points within the histogram
    ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
    hhsub = hh[posx[ind] - 1,
               posy[ind] - 1]  # values of the histogram where the points are
    xdat1 = xdat[ind][hhsub < thresh]  # low density points
    ydat1 = ydat[ind][hhsub < thresh]
    hh[hh < thresh] = np.nan  # fill the areas with low density by NaNs

    im = ax2.imshow(np.flipud(hh.T),
                    cmap=mycm,
                    extent=np.array(xyrange).flatten(),
                    interpolation='none',
                    aspect='auto',
                    vmin=thresh,
                    vmax=45)

    ax2.scatter(xdat1, ydat1, color=cset[-1])

    ax2.set_xlabel(r'Log$_{10} (L_{\rm UV} ({\rm erg/s}))$', fontsize=14)
    ax2.set_ylabel(r'$T_{\mathrm{BB}}$', fontsize=14)

    ax2.set_xlim(ax1.get_xlim())
    ax2.set_ylim(500, 2100)

    plt.tick_params(axis='both', which='major', labelsize=10)

    ax1.get_xaxis().set_ticks([])
    ax2.get_yaxis().set_ticks([600, 800, 1000, 1200, 1400, 1600, 1800, 2000])
    ax2.get_xaxis().set_ticks([45.4, 45.6, 45.8, 46.0, 46.2, 46.4, 46.6, 46.8])

    #############################################################################

    #histogram definition
    xyrange = [[7.5, 10.5], [0, 2]]  # data range
    bins = [30, 30]  # number of bins
    thresh = 4  #density threshold

    #data definition
    xdat, ydat = tab['LOGBH'], tab['RATIO_IR_UV']

    # histogram the data
    hh, locx, locy = histogram2d(xdat, ydat, range=xyrange, bins=bins)
    posx = np.digitize(xdat, locx)
    posy = np.digitize(ydat, locy)

    #select points within the histogram
    ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
    hhsub = hh[posx[ind] - 1,
               posy[ind] - 1]  # values of the histogram where the points are
    xdat1 = xdat[ind][hhsub < thresh]  # low density points
    ydat1 = ydat[ind][hhsub < thresh]
    hh[hh < thresh] = np.nan  # fill the areas with low density by NaNs

    im = ax4.imshow(np.flipud(hh.T),
                    cmap=mycm,
                    extent=np.array(xyrange).flatten(),
                    interpolation='none',
                    aspect='auto',
                    vmin=thresh,
                    vmax=45)

    ax4.scatter(xdat1, ydat1, color=cset[-1])

    ax4.set_ylim(ax1.get_ylim())
    ax4.set_xlim(7.5, 10.5)
    ax4.set_xticklabels([])
    ax4.set_yticklabels([])

    ax6.hist(tab[tab['LOGBH'] > 6]['LOGBH'], color=cset[-1], bins=20)
    ax6.set_xlim(ax4.get_xlim())
    ax6.set_axis_off()

    #histogram definition
    xyrange = [[7.5, 10.5], [500, 2000]]  # data range
    bins = [50, 30]  # number of bins
    thresh = 4  #density threshold

    #data definition
    xdat, ydat = tab['LOGBH'], tab['BBT']

    # histogram the data
    hh, locx, locy = histogram2d(xdat, ydat, range=xyrange, bins=bins)
    posx = np.digitize(xdat, locx)
    posy = np.digitize(ydat, locy)

    #select points within the histogram
    ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
    hhsub = hh[posx[ind] - 1,
               posy[ind] - 1]  # values of the histogram where the points are
    xdat1 = xdat[ind][hhsub < thresh]  # low density points
    ydat1 = ydat[ind][hhsub < thresh]
    hh[hh < thresh] = np.nan  # fill the areas with low density by NaNs

    im = ax5.imshow(np.flipud(hh.T),
                    cmap=mycm,
                    extent=np.array(xyrange).flatten(),
                    interpolation='none',
                    aspect='auto',
                    vmin=thresh,
                    vmax=45)

    ax5.scatter(xdat1, ydat1, color=cset[-1])

    ax5.set_xlabel(r'Log$_{10}$ (Black Hole Mass $M_{\rm BH}$)')

    ax5.set_yticklabels([])

    # ax5.hist(tab['BBT'], orientation='horizontal',color=cset[-1],bins=20)
    # ax5.set_ylim(ax2.get_ylim())
    # ax5.set_axis_off()

    plt.tick_params(axis='both', which='major')

    ax4.set_ylim(ax1.get_ylim())
    ax5.set_ylim(ax2.get_ylim())
    ax5.set_xlim(ax4.get_xlim())

    ax6.hist(tab['LOGBH'][tab['LOGBH'] > 6], color=cset[-1], bins=20)
    ax6.set_xlim(ax5.get_xlim())
    ax6.set_axis_off()

    ######################################################################################

    #histogram definition
    xyrange = [[-2, 0.5], [0, 2]]  # data range
    bins = [30, 30]  # number of bins
    thresh = 4  #density threshold

    #data definition
    xdat, ydat = tab['LOGEDD_RATIO'], tab['RATIO_IR_UV']

    # histogram the data
    hh, locx, locy = histogram2d(xdat, ydat, range=xyrange, bins=bins)
    posx = np.digitize(xdat, locx)
    posy = np.digitize(ydat, locy)

    #select points within the histogram
    ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
    hhsub = hh[posx[ind] - 1,
               posy[ind] - 1]  # values of the histogram where the points are
    xdat1 = xdat[ind][hhsub < thresh]  # low density points
    ydat1 = ydat[ind][hhsub < thresh]
    hh[hh < thresh] = np.nan  # fill the areas with low density by NaNs

    im = ax7.imshow(np.flipud(hh.T),
                    cmap=mycm,
                    extent=np.array(xyrange).flatten(),
                    interpolation='none',
                    aspect='auto',
                    vmin=thresh,
                    vmax=45)
    ax7.scatter(xdat1, ydat1, color=cset[-1])

    ax7.set_ylim(ax1.get_ylim())
    ax7.set_xlim(-2, 0.5)

    ax9.hist(tab[tab['LOGEDD_RATIO'] > -2.5]['LOGEDD_RATIO'],
             color=cset[-1],
             bins=20)
    ax9.set_xlim(ax7.get_xlim())
    ax9.set_axis_off()

    #histogram definition
    xyrange = [[-2, 0.5], [500, 2000]]  # data range
    bins = [50, 30]  # number of bins
    thresh = 4  #density threshold

    #data definition
    xdat, ydat = tab['LOGEDD_RATIO'], tab['BBT']

    # histogram the data
    hh, locx, locy = histogram2d(xdat, ydat, range=xyrange, bins=bins)
    posx = np.digitize(xdat, locx)
    posy = np.digitize(ydat, locy)

    #select points within the histogram
    ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
    hhsub = hh[posx[ind] - 1,
               posy[ind] - 1]  # values of the histogram where the points are
    xdat1 = xdat[ind][hhsub < thresh]  # low density points
    ydat1 = ydat[ind][hhsub < thresh]
    hh[hh < thresh] = np.nan  # fill the areas with low density by NaNs

    im = ax8.imshow(np.flipud(hh.T),
                    cmap=mycm,
                    extent=np.array(xyrange).flatten(),
                    interpolation='none',
                    aspect='auto',
                    vmin=thresh,
                    vmax=45)
    ax8.scatter(xdat1, ydat1, color=cset[-1])

    axcb = fig.add_axes([0.33, 0.05, 0.33, 0.02])
    clb = fig.colorbar(im, cax=axcb, orientation='horizontal')
    clb.set_label('Number of Objects')

    ax8.set_xlabel(r'Log$_{10}$ (Eddington Ratio $\lambda$)')

    ax8.set_ylim(ax2.get_ylim())
    ax8.set_xlim(ax7.get_xlim())

    plt.tick_params(axis='both', which='major')

    ax8.set_yticklabels([])
    ax8.set_xticklabels(['', '-1.5', '-1.0', '-0.5', '0.0', '0.5'])
    ax7.set_yticklabels([])
    ax7.set_xticklabels([])

    ax9.hist(tab['LOGBH'][tab['LOGBH'] > 6], color=cset[-1], bins=20)
    ax9.set_xlim(ax8.get_xlim())
    ax9.set_axis_off()

    ax10.hist(tab['RATIO_IR_UV'],
              orientation='horizontal',
              color=cset[-1],
              bins=np.arange(0, 2, 0.1))
    ax10.set_ylim(ax1.get_ylim())
    ax10.set_axis_off()

    ax11.hist(tab['BBT'], orientation='horizontal', color=cset[-1], bins=20)
    ax11.set_ylim(ax2.get_ylim())
    ax11.set_axis_off()

    s1 = spearmanr(tab[tab['LOGEDD_RATIO'] > -2.5]['LOGEDD_RATIO'],
                   tab[tab['LOGEDD_RATIO'] > -2.5]['RATIO_IR_UV'])[0]
    s2 = spearmanr(tab[tab['LOGBH'] > 6]['LOGBH'],
                   tab[tab['LOGBH'] > 6]['RATIO_IR_UV'])[0]
    s3 = spearmanr(tab['LUM_UV'], tab['RATIO_IR_UV'])[0]
    s4 = spearmanr(tab[tab['LOGEDD_RATIO'] > -2.5]['LOGEDD_RATIO'],
                   tab[tab['LOGEDD_RATIO'] > -2.5]['BBT'])[0]
    s5 = spearmanr(tab[tab['LOGBH'] > 6]['LOGBH'],
                   tab[tab['LOGBH'] > 6]['BBT'])[0]
    s6 = spearmanr(tab['LUM_UV'], tab['BBT'])[0]

    ax1.text(46.5, 0.2, r'$\rho =$ {0:.2f}'.format(s3))
    ax4.text(9.7, 0.2, r'$\rho =$ {0:.2f}'.format(s2))
    ax7.text(-0.1, 0.2, r'$\rho =$ {0:.2f}'.format(s1))
    ax2.text(46.5, 1900, r'$\rho =$ {0:.2f}'.format(s6))
    ax5.text(9.7, 1900, r'$\rho =$ {0:.2f}'.format(s5))
    ax8.text(-0.1, 1900, r'$\rho =$ {0:.2f}'.format(s4))

    fig.savefig('/home/lc585/thesis/figures/chapter06/correlations.pdf')

    plt.show()

    return None
예제 #48
0
def main():
    #usage: python <thisFile> level <outfile> <file.taxonomy> ..
    if len(sys.argv) < 3:
        mes = 'usage: python {} level <outfile> <file.taxonomy>..'
        print >> sys.stderr, mes.format(os.path.basename(sys.argv[0]))
        print >> sys.stderr, "*** filename.split('.')[0] will "\
                                 'be the sample label'
        sys.exit(1)

    level = int(sys.argv[1])
    level = level - 1
    outfile = sys.argv[2]
    d = {}
    dCombined = {}
    lisSampOrder = []
    for f in sys.argv[3:]:
        samp = os.path.basename(f).split('.')[0]  # sample name
        container, num_lis = readData(f)
        tranLis = itertools.izip_longest(*container, fillvalue='Unclassified')
        levelLis = list(tranLis)[level]
        countD = {}
        for tax, num in zip(levelLis, num_lis):
            countD[tax] = countD.get(tax, 0) + num

        total = sum(countD.values())
        d[samp] = dict((taxa, countD[taxa] * 1.0 / total) for taxa in countD)
        for key in d[samp]:
            dCombined[key] = dCombined.get(key, 0) + d[samp][key]

        lisSampOrder.append(samp)

    items = sorted(dCombined.items(), key=itemgetter(1), reverse=True)
    items = items[:TOP]
    if ORDER:
        items.reverse()
    taxas, nums = zip(*items)

    d2 = {}
    with open('{}.taxa.plot.summary'.format(outfile), 'wb') as fw2:
        print >> fw2, 'Sample\t{}'.format('\t'.join(taxas))
        for key in lisSampOrder:
            lis = []
            for word, count in items[:TOP]:
                pct = d[key].get(word, 0)
                lis.append(pct * 100)
            print >> fw2, '{}\t{}'.format(
                key, '\t'.join(('{:.1f}'.format(x) for x in lis)))
            d2[key] = lis

    sampNum = len(d2)
    #color = brewer2mpl.get_map('Set3', 'qualitative', TOP).mpl_colors
    color = brewer2mpl.get_map('Paired', 'qualitative', TOP).mpl_colors
    almost_black = '#262626'

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

    margin = 0.1
    #width = (1.-2.*margin)/sampNum
    width = 0.25
    xs = numpy.arange(sampNum)
    xs = xs + margin
    rects = []
    labels = []
    liss = []
    for samp in lisSampOrder:
        labels.append(samp)
        liss.append(d2[samp])
    #labels, liss = zip(*d2.items())

    taxaLiss = zip(*liss)
    taxaNum = len(taxaLiss)
    assert taxaNum <= TOP

    rect = ax.bar(xs, taxaLiss[0], width, color=color[0], linewidth=0)
    #rect = ax.bar(xs, taxaLiss[0], width, color=color[0], edgecolor='none')
    rects.append(rect)

    bottom = numpy.cumsum(taxaLiss, axis=0)
    for i in range(1, taxaNum):
        rect = ax.bar(xs,
                      taxaLiss[i],
                      width,
                      bottom=bottom[i - 1],
                      color=color[i],
                      linewidth=0)
        #rect = ax.bar(xs, taxaLiss[i], width, bottom=bottom[i-1], color=color[i], edgecolor='none')
        rects.append(rect)

    ax.set_ylabel('Percent of total community')
    ax.set_xticks(xs + width / 2)
    ax.set_xticklabels(labels)

    # Remove top and right axes lines ("spines")
    spines_to_remove = ['top', 'right']
    for spine in spines_to_remove:
        ax.spines[spine].set_visible(False)

    # Get rid of ticks. The position of the numbers is informative enough of
    # the position of the value.
    ax.get_xaxis().tick_bottom()
    ax.get_yaxis().tick_left()
    ax.xaxis.set_ticks_position('none')
    #ax.yaxis.set_ticks_position('none')

    # For remaining spines, thin out their line and change the black to a slightly off-black dark grey
    spines_to_keep = ['bottom', 'left']
    for spine in spines_to_keep:
        ax.spines[spine].set_linewidth(0.5)
        ax.spines[spine].set_color(almost_black)

    # shrink current axis by 20%
    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 0.6, box.height])

    # Change the labels to the off-black
    ax.xaxis.label.set_color(almost_black)
    ax.yaxis.label.set_color(almost_black)

    # Change the axis title to off-black
    ax.title.set_color(almost_black)

    # Remove the line around the legend box, and instead fill it with a light grey
    # Also only use one point for the scatterplot legend because the user will
    # get the idea after just one, they don't need three.
    #light_grey = np.array([float(248)/float(255)]*3)
    legend = ax.legend(rects[::-1],
                       taxas[::-1],
                       frameon=True,
                       loc='center left',
                       bbox_to_anchor=(1, 0.5))
    #legend = ax.legend(lines, keys, loc = 'center left', bbox_to_anchor=(1,0.5))
    #legend = ax.legend(frameon=True)
    rect = legend.get_frame()
    #rect.set_facecolor(light_grey)
    rect.set_linewidth(0.0)

    # Change the legend label colors to almost black, too
    texts = legend.texts
    for t in texts:
        t.set_color(almost_black)
        t.set_fontsize('medium')
        t.set_weight('bold')

    ax.set_ylim((0, 100.8))
    ### stuff for log scale
    #ax.set_yscale('symlog', linthreshy=0.1)
    #yticks = (0.1, 1, 10, 100)
    #yticks_str = [str(i) for i in yticks]
    #ax.set_yticks(yticks)
    #ax.set_yticklabels(yticks_str)

    # handling the ticks
    #majorLocator = AutoLocator()
    #minorLocator = MultipleLocator(10)
    #minorLocator = LogLocator(subs=[2,4,6,8])
    #minorLocator = AutoMinorLocator()
    #minorLocator = LinearLocator()
    #minorLocator = MaxNLocator(nbins=9)
    #ax.yaxis.set_major_locator(majorLocator)
    #ax.yaxis.set_minor_locator(minorLocator)

    plt.savefig('{}.png'.format(outfile))
예제 #49
0
class AreaAggregate(Graph):
    """
    This graph will show the combined forested area by summing all countries
    together into one graph.
    """

    # Size #
    height = 7
    width  = 10

    # Basic params #
    short_name = 'area_aggregate'
    y_grid  = True
    x_label = "Years"
    y_label = "Area in million hectares"

    # Colors #
    colors = brewer2mpl.get_map('Set1', 'qualitative', 7).mpl_colors

    # Name mapping #
    label_to_color = {'IPCC':    colors[0],
                      'SOEF':    colors[1],
                      'FAOSTAT': colors[3],
                      'FRA':     colors[4]}

    def line_plot(self, axes, x, y, source, **kw):
        # Filter by source #
        data = area_agg_data.df.query("source == '%s'" % source)
        # Plot #
        axes.plot(data[x], data[y],
                  marker     = ".",
                  markersize = 10.0,
                  color      = self.label_to_color[source.upper()],
                  **kw)

    def add_main_legend(self, axes):
        items   = self.label_to_color.items()
        patches = [matplotlib.patches.Patch(color=v, label=k) for k,v in items]
        axes.legend(handles   = patches,
                    borderpad      = 1,
                    prop           = {'size': 12},
                    frameon        = True,
                    shadow         = True,
                    loc            = 'center left',
                    bbox_to_anchor = (1.03, 0.5))

    def plot(self, **kwargs):
        # Plot #
        fig  = pyplot.figure()
        axes = fig.add_subplot(111)

        # Plot every data source #
        self.line_plot(axes, 'year', 'area', 'ipcc')
        self.line_plot(axes, 'year', 'area', 'soef')
        self.line_plot(axes, 'year', 'area', 'faostat')
        self.line_plot(axes, 'year', 'area', 'fra')

        # Leave space for the legend #
        fig.subplots_adjust(left=0.1, right=0.8, top=0.95)

        # Add legend #
        self.add_main_legend(axes)

        # Save #
        self.save_plot(**kwargs)

        # Return for display in notebooks for instance #
        return fig
예제 #50
0
def main():

    fdir = "data/processed"
    df_flux = pd.read_csv(os.path.join(fdir, "g1_fluxnet_screened_PM.csv"))
    df_flux_coupled = pd.read_csv(
        os.path.join(fdir, "g1_fluxnet_screened_matching_sites.csv"))
    df_leaf = pd.read_csv(os.path.join(fdir, "g1_leaf_gas_exchange.csv"))
    df_isotope = pd.read_csv(os.path.join(fdir, "g1_isotope_screened.csv"))
    df_sig = pd.read_csv(os.path.join(fdir, "g1_signifletters_withinPFT.csv"))
    df_flux['Scale'] = 'FLUXNET - PM'
    df_flux_coupled['Scale'] = 'FLUXNET'
    df_isotope['Scale'] = 'Leaf isotope'

    df = pd.concat([df_flux_coupled, df_leaf, df_flux])

    # arbitary cut off of values < 18
    aa = len(df)
    df = df[df.g1 <= 18]
    bb = len(df)
    print(aa - bb) / float(aa) * 100.

    pfts = [
        'ENF', 'EBF', 'DBF', 'TRF', 'SAV', 'SHB', 'C3G', 'C4G', 'C3C', 'C4C'
    ]
    sns.set_style("ticks")
    sns.set_style({"xtick.direction": "in", "ytick.direction": "in"})
    set2 = brewer2mpl.get_map('Set2', 'qualitative', 8).mpl_colors

    fig = plt.figure(figsize=(12, 6))
    fig.subplots_adjust(hspace=0.1)
    fig.subplots_adjust(wspace=0.05)
    plt.rcParams['text.usetex'] = False
    plt.rcParams['font.family'] = "sans-serif"
    plt.rcParams['font.sans-serif'] = "Helvetica"
    plt.rcParams['axes.labelsize'] = 14
    plt.rcParams['font.size'] = 12
    plt.rcParams['legend.fontsize'] = 12
    plt.rcParams['xtick.labelsize'] = 12
    plt.rcParams['ytick.labelsize'] = 12

    almost_black = '#262626'
    # change the tick colors also to the almost black
    plt.rcParams['ytick.color'] = almost_black
    plt.rcParams['xtick.color'] = almost_black

    # change the text colors also to the almost black
    plt.rcParams['text.color'] = almost_black

    # Change the default axis colors from black to a slightly lighter black,
    # and a little thinner (0.5 instead of 1)
    plt.rcParams['axes.edgecolor'] = almost_black
    plt.rcParams['axes.labelcolor'] = almost_black

    ax = fig.add_subplot(111)

    # define outlier properties
    flierprops = dict(marker='o', markersize=3, markerfacecolor="black")
    ax = sns.boxplot(
        x="PFT",
        y="g1",
        hue="Scale",
        data=df,
        palette=["#66c2a5", "#e78ac3", "#8da0cb"],
        order=pfts,
        flierprops=flierprops,
        hue_order=['Leaf gas exchange', 'FLUXNET - PM', 'FLUXNET'])

    plt.legend(loc="upper right")

    ax.set_ylabel("$g_1$ (kPa$^{0.5}$)")
    ax.set_xlabel("Plant functional type")
    #p.set_xlabels(" ")
    #p.despine(right=False,top=False,left=False,bottom=False)
    ax.set_ylim(-1.5, 14)
    ax.set_yticks(np.arange(0, 16, 2))
    ax.set_xticklabels(pfts)

    for i, p in enumerate(pfts):

        if i == 0:
            offset = 0

        if p != "C4C":
            plt.text(-0.13 + offset,
                     15,
                     "n=%d\n" % (len(df_leaf[df_leaf.PFT == p].g1)),
                     horizontalalignment='left',
                     size='small',
                     color="#66c2a5",
                     weight="bold")

        if p != "C4G" and p != "C3C" and p != "C4C":

            plt.text(-0.13+offset, 14.5,
                     "n=%d\n" % \
                        (len(df_flux_coupled[df_flux_coupled.PFT == p].g1)),
                     horizontalalignment='left', size='small', color="#e78ac3",
                     weight="bold")

        if p != "C4G":
            plt.text(-0.13 + offset,
                     14,
                     "n=%d\n" % (len(df_flux[df_flux.PFT == p].g1)),
                     horizontalalignment='left',
                     size='small',
                     color="#8da0cb",
                     weight="bold")

        offset += 1.0


    odir = "/Users/%s/Dropbox/g1_leaf_ecosystem_paper/figures/figs/" %\
            (os.getlogin())
    #odir = "/Users/%s/Desktop/" % (os.getlogin())
    plt.savefig(os.path.join(
        odir, "g1_gas_exchange_isotope_fluxnet_boxplot_inverted_PM.pdf"),
                bbox_inches='tight',
                pad_inches=0.1)
for line in data:
    toks = line.strip().split(',')
    power_budget.append(toks[0])
    hocmp_value.append(toks[1])
    hecmp_value.append(toks[2])
    improvement.append(((float(toks[2]) / float(toks[1])) - 1) * 100)

fig, ax1 = plt.subplots()
num_plots = 3

###Create markers
#markers = itertools.cycle(('+', '.', 'o', '*', '^'))
markers = itertools.cycle(('o', '*', '^'))

##Color Map
set2 = brewer2mpl.get_map('Set1', 'qualitative', 9).mpl_colors

t = 1
ax1.plot(power_budget,
         improvement,
         color=set2[t],
         label=sys.argv[2],
         marker=markers.next(),
         markersize=4,
         linewidth=2)
ax1.set_xlabel('Power Budget (W)')
# Make the y-axis label and tick labels match the line color.
ax1.set_ylabel('Improvement(%)', color=set2[t])
for tl in ax1.get_yticklabels():
    tl.set_color(set2[t])
예제 #52
0
import os

#Science packages
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
import matplotlib.ticker as plticker
import numpy as np
from scipy import stats

#Other Packages
import brewer2mpl as b2m  #colorbrewer maps
"""----------------------------- plot setup ------------------------------------------"""

#set up color map with brewer2mpl, not mandatory as matplotlib as the colorbrewer maps but
# provides an easy way to limit the number of colors in a map
bmap = b2m.get_map('Blues', 'Sequential', 5, reverse=False)
bmap = bmap.mpl_colors
"""----------------------------- plots  ----------------------------------------------"""


def plot_wavetransf(wa, T, S, sig95, time_base, plot_percentile=False):
    """plotting WaveTransform Power with confidence interval contour"""

    fig = plt.figure(1)
    ax = plt.subplot(1, 1, 1)
    if plot_percentile:
        #use following to contour at "percentiles variances" when using non-normalized data to match web output
        csf = plt.contourf(T,
                           S,
                           wa.wavelet_power,
                           levels=[
예제 #53
0
def plot_aa(analysis_dir, params, runs, refnames, plot_type, sel='backbone',   \
        pltparams=(None, None, (5.75, 12)), plotname=None, showplot=True):
    #-----------------------
    # Timeseries angles for 1ake and 4ake trajectories
    #-----------------------
    methods, protein, directions = params
    drmsd, angles, dmcd, idf = False, True, False, False
    st = en = sk = None

    N = len(runs)
    ofname_ref_nmp, ofname_ref_lid = refnames
    timeseries, twod = plot_type

    xlims, ylims, (fsize1, fsize2), lsize = pltparams
    #-- plot settings ------
    xmin, xmax = xlims
    ymin, ymax = ylims
    xdatarange, ydatarange = (xmax-xmin), (ymax-ymin)

    #-----------------------
    # 2D angles for 1ake and 4ake trajectories
    #-----------------------
    if twod:
        print "Plotting angles for 1ake and 4ake trajectories..."

        # Plot colors:
        import brewer2mpl
        import seaborn as sns
        sns.set_style('whitegrid')
        # Save a nice dark grey as a variable
        almost_black = '#262626'
        # Get "Set2" colors from ColorBrewer (all colorbrewer scales: http://bl.ocks.org/mbostock/5577023)
        dark2 = brewer2mpl.get_map('Dark2', 'qualitative', 8).mpl_colors
        accent = brewer2mpl.get_map('Accent', 'qualitative', 8).mpl_colors
        set1 = brewer2mpl.get_map('Set1', 'qualitative', 9).mpl_colors
        s2 = brewer2mpl.get_map('Set2', 'qualitative', 8).mpl_colors
        s3 = brewer2mpl.get_map('Set3', 'qualitative', 12).mpl_colors
        # colors = [s3[9],s3[5],s2[4],s3[3],s3[7],s2[5],s2[7],s2[6],s2[0],s3[10],s2[1]]
        # colors = [s3[0],s3[11]] + s3[2:8] + s3[9:11] + [s2[6]]
        # colors = [s3[1],s2[6]]+s3[2:8] + [s2[7]] + s3[9:11] +[s3[8]]
        # colors = [s3[11],s2[6],s3[2],s3[3],s2[0],s3[5],s3[7],s3[9],s3[4],s2[4],'gray']
        # colors = [s2[0],s3[11],s3[2],s3[3],s3[4],s3[5],s2[4],s3[7],s2[6],s3[9],'gray']
        # colors = [s2[0],s3[11],s2[3],s3[3],s2[2],s3[7],s2[4],s3[5],s3[9],s2[6],'gray']
        # xkcd_colors = ["pinkish", "brick red",
        #                "goldenrod", "orange",
        #                "buff", "banana yellow",
        #                "pale lime green", "tree green",
        #                "robin egg blue", "windows blue",
        #                "lilac", "purplish",
        #                "tan", "raw umber",
        #                "grey"]
        xkcd_colors = ["pinkish", "brick red", "goldenrod", "orange",
                       "banana yellow", "pale lime green", "tree green",
                       "robin egg blue", "windows blue", "lilac", "purplish",
                       "raw umber", "grey"]
        xkcd_colors = ["cherry", "tree green", "orange", "windows blue",
                   "banana yellow", "bubblegum pink", "goldenrod",
                   "robin egg blue", "pale lime green", "lilac", "purply",
                   "raw umber", "grey"]
        colors = sns.xkcd_palette(xkcd_colors)

        end_colors = sns.xkcd_palette(['vibrant green', 'bright red'])

        # Turn off lines and multiple markers in legend
        mpl.rcParams['legend.handlelength'] = 0
        mpl.rcParams['legend.numpoints'] = 1
        mpl.rcParams['legend.scatterpoints'] = 1
        plt.rcParams['xtick.major.pad']='3'
        plt.rcParams['ytick.major.pad']='3'

        #-- create figure ------
        plt.close("all")
        aspect_ratio = float(xdatarange)/ydatarange
        fig = plt.figure(1, figsize=(fsize1, fsize2))
        ax = fig.add_subplot(111)

        if sel == 'name CA':
            postf = '_ca'
        elif sel == 'backbone':
            postf = '_bb'
        ref_theta_nmp = np.loadtxt(ofname_ref_nmp+postf+'.dat')
        ref_theta_lid = np.loadtxt(ofname_ref_lid+postf+'.dat')
        #-- plot reference structures ----------
        s1 = ax.scatter(ref_theta_nmp[0], ref_theta_lid[0], marker="o", s=100, \
                label=" 1AKE", c=end_colors[0], zorder=15, linewidths=1)
        # s1 = ax.scatter(ref_theta_nmp[0], ref_theta_lid[0], marker="o", s=150, \
        #         c='gray', zorder=14, linewidths=0)
        s2 = ax.scatter(ref_theta_nmp[1], ref_theta_lid[1], marker="D", s=90,  \
                label=" 4AKE", c=end_colors[1], zorder=15, linewidths=1)
        # s2 = ax.scatter(ref_theta_nmp[1], ref_theta_lid[1], marker="D", s=140,  \
        #         c='gray', zorder=14, linewidths=0)

        for i, m in zip(xrange(len(methods)), methods):
            method, methodname, postfix = m[0], m[1], m[2]
            d = 'co'
            theta_nmp_accum = []
            theta_lid_accum = []
            for run in xrange(N):
                mn = ' %s' % methodname
                mtype = 'o' if d=='co' else 'D'
                if run > 0:
                    mn = None
                    if (method == 'intp'): break # only one interp line traj
                if  (method == 'intp' and d == 'oc'): break
                if postfix != None:
                    basename = '%s/%s_%s-%s-%s-%s'                         \
                            % (analysis_dir, 'aa', method, postfix, d, protein)
                else:
                    basename = '%s/%s_%s-%s-%s'                            \
                            % (analysis_dir, 'aa', method, d, protein)
                ### Hack to use all-atom intp trajectories ('_full' vs '_ca')
                if method == 'intp':
                    ofname1 = '%s-%s_%s_full.dat' % (basename, 'nmp', runs[run])
                    ofname2 = '%s-%s_%s_full.dat' % (basename, 'lid', runs[run])
                else:
                    ofname1 = '%s-%s_%s.dat' % (basename, 'nmp', runs[run])
                    ofname2 = '%s-%s_%s.dat' % (basename, 'lid', runs[run])
                sk = 2 if method == 'map' else 1 # reduce # map frames
                theta_trj_nmp = np.loadtxt(ofname1)[st:en:sk]
                theta_trj_lid = np.loadtxt(ofname2)[st:en:sk]
                theta_nmp_accum = np.append(theta_nmp_accum, theta_trj_nmp)
                theta_lid_accum = np.append(theta_lid_accum, theta_trj_lid)
                color = colors[i]
                zorder = 12-i
                (mtype,size) = ('D',15) if i < 6 else ('o',21)
                ax.plot(theta_trj_nmp, theta_trj_lid, lw=1,              \
                    c=color, alpha=1, zorder=zorder-1)
                # zorder = -i #if 'menm' not in method else 9
                ax.scatter(theta_trj_nmp, theta_trj_lid, lw=0.2, s=size,     \
                        c=color, marker=mtype, label=mn, alpha=1, zorder=zorder)


        #----------------------------------------
        # Legends
        #----------------------------------------
        # Thin line around the legend box, and instead fill it with a light grey
        # Also only use one point for the scatterplot legend because the user will
        # get the idea after just one, they don't need three.
        # axl = fig.add_subplot(122)
        # light_grey = np.array([float(248)/float(255)]*3)
        # legend = axl.legend(loc='lower left', bbox_to_anchor=(0.85, 0.025),     \
        #         frameon=True, fancybox=True, scatterpoints=1, borderpad=1,     \
        #         labelspacing=0.5, markerscale=1.0)
        # rect = legend.get_frame()
        # rect.set_facecolor(light_grey)
        # rect.set_linewidth(1.0)
        # Change the legend label colors to almost black, too
        # for t in legend.texts: t.set_color(almost_black)
        # plt.setp(legend.get_texts(), fontsize=(lsize-2))
        #----------------------------------------
        legend = None

        ax.set_xlim([xmin,xmax])
        ax.set_ylim([ymin,ymax])

        # Remove top and right axes lines ("spines")
        # spines_to_remove = ['top', 'right']
        # for spine in spines_to_remove:
        #     ax.spines[spine].set_visible(False)

        # Locate major ticks at 5 deg intervals on both axes
        intvl = 10
        x_major_ticklocs = np.arange(40, 75+intvl, intvl)
        y_major_ticklocs = np.arange(100, 155+intvl, intvl)
        locx = plticker.FixedLocator(x_major_ticklocs)
        locy = plticker.FixedLocator(y_major_ticklocs)
        ax.xaxis.set_major_locator(locx)
        ax.yaxis.set_major_locator(locy)

        ax.tick_params(axis='both', which='major', labelsize=lsize, pad=-7.5)

        # major_formatter = plticker.FormatStrFormatter(r'%3i$^\circ$')
        # ax.xaxis.set_major_formatter(major_formatter)
        # ax.yaxis.set_major_formatter(major_formatter)

        xticklabels = np.array(ax.get_xticks())
        yticklabels = np.array(ax.get_yticks())
        # x_major_ticklocs = np.arange(xmin, xmax+intvl, intvl)
        xticklabels = ['' if (t%2).round() != 0 else r'%3i$^\circ$' % t for t in xticklabels] # change fraction to %
        yticklabels = ['' if (t%2).round() != 0 else r'%3i$^\circ$' % t for t in yticklabels] # change fraction to %
        ax.set_xticklabels(xticklabels, minor=False)
        ax.set_yticklabels(yticklabels, minor=False)

        # ax.grid(False)

        # ax.tick_params(axis='both', which='major', labelsize=lsize)

        # ax.grid(True, which='major', ls='-', c=almost_black, alpha=1)

        # rec = mpatches.Rectangle((xmin,ymin), xdatarange, ydatarange,          \
        #         fill=False,lw=1, color='grey', alpha=0.4)
        # rec = ax.add_patch(rec)
        # rec.set_clip_on(False)

        #-- labels ------
        ax.set_xlabel(r'NMP$-$CORE angle', fontsize=lsize)
        ax.set_ylabel(r'LID$-$CORE angle', fontsize=lsize)

        # ax.xaxis.set_ticks_position('none')
        # ax.yaxis.set_ticks_position('none')

        sns.despine(offset=10, bottom=True, left=True)

        plt.tight_layout()

        def multisavefig(name, lgd=None, transparent=True):
            for ext in ['tif', 'pdf', 'png']:
                if lgd is None:
                    plt.savefig(name+'.'+ext, format=ext,                      \
                        transparent=transparent, bbox_inches='tight')
                else:
                    plt.savefig(name+'.'+ext, format=ext,                      \
                        transparent=transparent,                               \
                        bbox_extra_artists=(lgd,), bbox_inches='tight')

        if (plotname is not None):
            if type(plotname) is str:
                pltfp1, pltfp2 = plotname, None
            else: pltfp1, pltfp2 = plotname
            # multisavefig(pltfp1, lgd=legend, transparent=True)
            multisavefig(pltfp1, transparent=True)
            # multisavefig(pltfp1+'-notrans', lgd=legend, transparent=False)
            if pltfp2 is not None:
                import glob, os, shutil, re
                source_dir_split = [x for x in pltfp1.split('/')]
                source_dir = '/'.join(source_dir_split[:-1])
                name = '%s*' % pltfp1
                files = glob.iglob(os.path.join(source_dir, name))
                for f in files:
                    if os.path.isfile(f):
                        ext = f.split('.')[-1]
                        newfile = '%s.%s' % (pltfp2, ext)
                        newfile_t = '%s-notrans.%s' % (pltfp2, ext)
                        shutil.copyfile(f, newfile)
                        shutil.copyfile(f, newfile_t)
        if showplot:
            plt.show()
            plt.close()
예제 #54
0
reference_genomes = OrderedDict([
    ('ont_lambda', '/data/minion/reference_genomes/lambda/lambda_ref.fasta'),
    ('cs', '/data/minion/reference_genomes/cs/dna_cs.fasta'),
    ('3D7_V3', '/data/minion/reference_genomes/3D7_V3/3D7_V3.fasta'),
    ('AgamP3', '/data/minion/reference_genomes/AgamP3/Anopheles-gambiae-PEST_CHROMOSOMES_AgamP3.fasta')
])

# <codecell>

read_types = ['template', 'complement', '2D']

# <codecell>

import brewer2mpl
set1 = brewer2mpl.get_map('Set1', 'qualitative', 8).mpl_colors
set3 = brewer2mpl.get_map('Set3', 'qualitative', 12).mpl_colors

# <headingcell level=1>

# Functions

# <codecell>

ids = OrderedDict([
        ('2D basecalling successful'                  , 'Basecalling 2D hairpin data'),
        ('Missing Complement data'                    , 'Splitter indicates no complement data. Workflow cannot continue'),
        ('Missing Template data'                      , 'Splitter indicates no template data. Workflow cannot continue'),
        ('Too few events'                             , 'Read has too few events. Workflow cannot continue'),
        ('Poor ratio of template to complement data'  , 'Ratio of template to complement data is no good. Workflow cannot continue'),
        ('Inconsistent template vs complement events' , 'Number of template events out of range aligning template'),
topo_fname = os.path.join(os.getenv('SARIKA_INPUT'), 'TOPO-124x124.nc')
wrf_fname = os.path.join(os.getenv('SARIKA_INPUT'),
                         'wrfheight-124x124-22levs.nc')

bd = sp.parse_STEM_var(bd_fname,
                       varname='CO2_TRACER1',
                       t0=datetime(2008, 7, 1),
                       t1=datetime(2008, 9, 1))

w_bnd_tavg = np.mean(bd['data'][:, :, -125:-1], axis=0) * 1000

stemlon, stemlat, topo = sp.parse_STEM_coordinates(topo_fname)
agl, asl = get_STEMZ_height(topo_fname, wrf_fname)

cos_norm = midpt_norm.MidpointNormalize(midpoint=450)
cos_cmap = brewer2mpl.get_map('PuOr', 'diverging', 8).mpl_colormap

fig, ax = plt.subplots()
lat = stemlat[:, -1]
ax.set_xlim([lat.min(), lat.max()])
cm = ax.pcolormesh(lat,
                   agl[:, :, -1],
                   w_bnd_tavg,
                   cmap=plt.get_cmap('PuOr'),
                   norm=cos_norm)
ax.set_xlabel('latitude ($^\circ$N)')
ax.set_ylabel('height AGL (m)')
ax.set_title('PCTM Western boundary July/August mean')
ticks = np.round(
    np.concatenate([
        np.linspace(w_bnd_tavg.min(), 450, 4),
예제 #56
0
def generate_plots(df, m, bin_size, min_bins):
    # Map-related info
    longitudes = np.arange(minLon, maxLon, 0.25)
    latitudes = np.arange(minLat, maxLat, 0.1)

    #Relevant quatities

    categs = pd.Series(sorted(df['PropertyType'].unique()))
    n_categs = len(categs)
    n_samples = 1. * len(fire)

    # Posterior mean estimate of category probabilities using Dirichlet prior.
    categ_priors = (df.groupby(['PropertyType']).size() + 1) / (n_samples +
                                                                n_categs)

    # Create the x and y bin edges over which we'll smooth positions.
    # Better approach might be to find optimal bin size through
    # cross validation based approach (need more time though).
    bin_edges_x = np.arange(
        np.min(df['X']) - bin_size / 2,
        np.max(df['X']) + bin_size / 2, bin_size)
    bin_edges_y = np.arange(
        np.min(df['Y']) - bin_size / 2,
        np.max(df['Y']) + bin_size / 2, bin_size)
    overall_hist, yedges, xedges, Img = plt.hist2d(df.Y.values,
                                                   df.X.values,
                                                   bins=(bin_edges_y,
                                                         bin_edges_x))

    # We'll assume that fires really only occurs at a location
    # at which we have seen at least one fire incidence over
    #the period comprising 2012-2015.

    # This will help with the plotting
    mask = overall_hist == 0
    n_bins = np.sum(overall_hist > 0)

    # Obtain the class condition probabilities p(x|y).
    # Essentially we are computing the quantity: given the type of affected property,
    # what is the probability that the fire occurred in a given xy bin.
    # Because a single fire can happen in only one location, we are
    # treating the class conditional densities as multinomial.
    groups = df.groupby(['PropertyType'])
    px_y = np.zeros([len(bin_edges_y) - 1, len(bin_edges_x) - 1, n_categs])
    px_y_ma = np.ma.masked_where(
        np.tile(np.atleast_3d(mask), [1, 1, n_categs]), px_y)
    for i, (name, group) in enumerate(groups):
        group_hist, yedges, xedges, Img = plt.hist2d(group.Y.values,
                                                     group.X.values,
                                                     bins=(bin_edges_y,
                                                           bin_edges_x))
        group_hist_ma = np.ma.masked_where(mask, group_hist)

        # Posterior mean estimates of class conditonal probabilities
        # using Dirichlet prior.
        px_y_ma[:, :,
                i] = (group_hist_ma + 1.0) / (np.sum(group_hist_ma) + n_bins)

    # Present the category prior into the right shape
    p_y = np.atleast_3d(categ_priors.as_matrix()).reshape(1, 1, n_categs)
    p_y = np.tile(p_y, [len(bin_edges_y) - 1, len(bin_edges_x) - 1, 1])
    p_y_ma = np.ma.masked_where(np.tile(np.atleast_3d(mask), [1, 1, n_categs]),
                                p_y)

    # Obtain the posterior probabilites of each property type,
    # given that the fire occurred in a known xy location
    py_x_ma = (p_y_ma * px_y_ma) / np.atleast_3d(
        np.sum(p_y_ma * px_y_ma, axis=2))

    # Compute entropy of posterior distribution
    hy = -np.sum(categ_priors * np.log2(categ_priors))
    hy_x = -np.sum(py_x_ma * np.log2(py_x_ma), axis=2)
    entropy_diff = hy - hy_x

    X, Y = np.meshgrid(bin_edges_x, bin_edges_y)

    # Get the property type that maximized the posterior probability.
    winner = np.argmax(py_x_ma, axis=2)
    winner_ma = np.ma.masked_where(mask, winner)

    # How many time does each fire(property type) prove to be the most likely?
    counts = itemfreq(winner_ma[winner_ma.mask == False])

    # Sort the counts and take only those types which have
    # at least min_pop_bins bins in which they were the winner
    sorted_counts = counts[np.argsort(counts[:, 1])[::-1], :]
    top_counts = sorted_counts[sorted_counts[:, 1] >= min_bins, :]
    #top_counts_less = top_counts[1:, :]
    top_counts_less = top_counts

    ####### Plotting preferences
    plt.figure(figsize=(16, 18))
    m.arcgisimage(service='Canvas/World_Light_Gray_Base',
                  xpixels=1200,
                  ypixels=1000)
    #plt.figure(figsize=(16,18))
    #m.drawmapboundary(fill_color='paleturquoise')
    #m.fillcontinents(color=(0.25, 0.25, 0.25), zorder=0)
    #m.drawparallels(latitudes, color='white', labels=[1, 0, 0, 0],
    #               dashes=[5, 5], linewidth=0.75)
    #m.drawmeridians(longitudes, color='white', labels=[0, 0, 0, 1],
    #                dashes=[5, 5], linewidth=0.75)
    n_colors = top_counts_less.shape[0]
    colors = b2mpl.get_map('Set3', 'Qualitative', n_colors).mpl_colors
    recoded = np.zeros(winner.shape)
    for i, categ_num in enumerate(top_counts[:, 0]):
        recoded[winner == categ_num] = i

    winner_ma = np.ma.masked_where(recoded == 0, recoded)
    m.pcolormesh(X,
                 Y,
                 winner_ma,
                 cmap=mpl_colors.ListedColormap(colors),
                 latlon=True)

    winner_ma = np.ma.masked_where(np.logical_or(mask, winner != 16), winner)
    m.pcolormesh(X, Y, winner_ma, cmap='Greys', alpha=0.5, edgecolor='None')

    legend_labels = [categs[i] for i in top_counts[:, 0]]

    # separate way of doing legend for the most common crime, which is not
    # part of the colormap
    legend_markers = []
    legend_markers.append(Rectangle((0, 0), 1, 1, fc='thistle', ec='thistle'))

    # now the other types
    legend_edgecolors = colors
    legend_facecolors = colors
    for i in range(len(top_counts_less)):
        legend_markers.append(
            Rectangle((0, 0),
                      1,
                      1,
                      fc=legend_facecolors[i],
                      ec=legend_edgecolors[i]))
    legend = plt.legend(legend_markers,
                        legend_labels,
                        labelspacing=.075,
                        handlelength=.5,
                        handletextpad=.1,
                        fancybox=True,
                        frameon=1,
                        loc='lower left')
    frame = legend.get_frame()
    frame.set_facecolor('black')

    texts = legend.get_texts()
    texts[0].set_fontsize(10)
    texts[0].set_color('white')
    for t, c in zip(texts[1:], legend_edgecolors):
        t.set_fontsize(10.5)
        t.set_color(c)

    plt.title(
        "Most likely fire (specified by affected property type) \n"
        "given knowledge of location only,\n"
        "taking into account the prior fire probability\n\n",
        fontsize=14)
    plt.savefig('Type_output.png')

    #### Entropy difference plot
    plt.figure(figsize=(16, 18))

    entropy_diff_ma = np.ma.masked_where(mask, entropy_diff)
    max_diff = np.max(entropy_diff_ma)
    min_diff = np.min(entropy_diff_ma)
    max_abs = np.max([max_diff, np.abs(min_diff)])
    m.pcolormesh(X,
                 Y,
                 entropy_diff_ma,
                 cmap='BuPu',
                 alpha=0.75,
                 edgecolor='None',
                 vmin=-max_abs,
                 vmax=max_abs)

    m.arcgisimage(service='Canvas/World_Light_Gray_Base', xpixels=1500)
    #m.drawmapboundary(fill_color='paleturquoise')
    #m.fillcontinents(color=(0.25, 0.25, 0.25), zorder=0)
    #m.drawparallels(latitudes, color='white', labels=[1, 0, 0, 0],
    #                dashes=[5, 5], linewidth=.75)
    #m.drawmeridians(longitudes, color='white', labels=[0, 0, 0, 1],
    #                dashes=[5, 5], linewidth=.75)
    m.scatter(fire.X, fire.Y)
    m.pcolormesh(X,
                 Y,
                 entropy_diff_ma,
                 cmap='BuPu',
                 alpha=0.75,
                 edgecolor='None',
                 vmin=-max_abs,
                 vmax=max_abs)
    cbar = plt.colorbar(shrink=0.25)
    cbar.solids.set_edgecolor("face")

    plt.title(
        'Entropy of prior distribution minus entropy of posterior distribution:\n'
        'positive values indicate less uncertainty about fire type '
        'after observing location\n\n',
        fontsize=14)
    plt.savefig('Entropy.png')
예제 #57
0
class SimulationRunner(object):
    Simulation = namedtuple('Simulation', [
        'predictor_config', 'feature_cardinality', 'num_examples',
        'biased_feature_proportion', 'directory', 'extension',
        'visualization_interval', 'biased_feature_effect_length'
    ])

    COLORS = brewer2mpl.get_map('Set2', 'qualitative', 8).mpl_colors

    def __init__(self, simulation):
        self._simulation = simulation
        self._predictor = AdPredictor(simulation.predictor_config)
        self._sampler = Sampler(simulation)

    def _current_weights_by_feature(self):
        by_feature = lambda kv: kv[0].feature
        by_feature_value = lambda kv: (kv[0].feature, kv[0].value)
        weights = sorted(self._predictor.weights, key=by_feature_value)
        for feature, group in itertools.groupby(weights, key=by_feature):
            yield feature, [(f, w.mean, w.variance) for (f, w) in group]

    def _plot_weights(self):
        for color, (feature, weights) in itertools.izip(
                itertools.cycle(self.COLORS),
                self._current_weights_by_feature()):
            _, means, variances = zip(*weights)
            logging.debug("Feature %s, Weights: %s", feature, weights)

            label = "F{}".format(feature) if feature != 0 else "Bias"
            plt.scatter(means,
                        variances,
                        label=label,
                        color=color,
                        alpha=0.8,
                        s=40)

    def _annotate_biased_weights(self):
        for _, weights in self._current_weights_by_feature():
            for (feature, mean, variance) in weights:
                bias_weight = self._sampler.get_bias_for_feature(feature)
                if bias_weight is not None:
                    plt.annotate('+' if bias_weight else '-', (mean, variance),
                                 size=40)

    def _visualize(self, num_examples):
        plt.clf()

        self._plot_weights()
        self._annotate_biased_weights()

        plt.title(u"(μ, σ²) after {} examples".format(num_examples))
        plt.xlabel(u"μ")
        plt.ylabel(u"σ²")
        plt.legend(loc='best')
        plt.xlim(-4, 4)
        plt.ylim(-0.1, 1.1)

        filename = "{:03d}.{}".format(num_examples, self._simulation.extension)
        logger.info("Saving graph to %s", filename)
        plt.savefig(os.path.join(self._simulation.directory, filename),
                    dpi=300)

    def run(self):
        samples = itertools.islice(self._sampler,
                                   self._simulation.num_examples)
        for iteration, (features, label) in enumerate(samples):
            self._predictor.train(features, label)
            if iteration % self._simulation.visualization_interval == 0:
                self._visualize(iteration)
예제 #58
0
import numpy as np

import brewer2mpl
from matplotlib import cm

# ColorBrewer by Drs. Cynthia Brewer and Mark Harrower of Pennsylvania
# State University. For more information on ColorBrewer, see:
# - Flash-based interactive map:
# http://colorbrewer2.org/
# - A quick visual reference to every ColorBrewer scale:
# http://bl.ocks.org/mbostock/5577023

# ColorBrewer scale 'Qualitative.Set1'.
# This one has nice "traditional" colors like reds and blues
brewer_set1 = brewer2mpl.get_map('Set1', 'qualitative', 9).mpl_colors
# Remove the sixth color (yellow) which is too bright
brewer_set1.pop(5)
# Swap the red and blue to let blue come first
brewer_set1[0], brewer_set1[1] = brewer_set1[1], brewer_set1[0]

# ColorBrewer scale 'Qualitative.Set2'.
# This one are less saturated than those colors in Set1.
brewer_set2 = brewer2mpl.get_map('Set2', 'qualitative', 8).mpl_colors

# ColorBrewer scale 'Qualitative.Set3'.
# This one are even less saturated than those colors in Set1.
brewer_set3 = brewer2mpl.get_map('Set3', 'qualitative', 12).mpl_colors
# Remove the second color (yellow) which is too bright
brewer_set3.pop(1)
예제 #59
0
파일: maz.py 프로젝트: sunash2/DMDControl
    plt.show()


if __name__ == '__main__':
    #manual()

    #radii = np.array([5,10,20,30,40,60,80,100,150,200,250])
    radii = np.array([5, 20, 40, 60, 80, 100, 150, 200, 250])
    #radii = np.array([5,60,200])

    #on_pix_um_radius = 50

    center_amp_vec = np.zeros_like(radii)

    #plot_settings
    colors = cb.get_map('YlOrRd', 'Sequential', 9).mpl_colors

    plt.figure(figsize=(10, 10))
    plt.subplot(2, 1, 1)
    for idx, r in enumerate(radii):
        dmd = DMDchip(r, chip_side_length_pixels=100)

        sim = FourierSim(xrng=[-500.0, 500.0],
                         yrng=[-500.0, 500.0],
                         N=[500, 500],
                         dmd=dmd)
        sim.dmd.compute_on_pixels()
        sim.compute_initial_amplitude_distribution()
        sim.fourier_filter()
        rads, avg, center_amp = sim.compute_radial_average()
@author: isaac
"""

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

import plots
reload(plots)
from plots import bmap, rcj, tl, tickout, four_plot

# %% Get some colors

import brewer2mpl
new_bmap = brewer2mpl.get_map('Set1', 'Qualitative', 9).mpl_colors
new_bmap.pop(5)

more_colors = brewer2mpl.get_map('Set2', 'Qualitative', 8).mpl_colors

new_bmap += more_colors

# %%

d = pd.read_csv('./Data/epsilon.csv')
mass = d[u'Mass (g)'].values
eps = d[u'epsilon'].values
eps_snake = d[u'eps from c from sqrt(Ws)'].values
Ws = d[u'Wing loading (N/m^2)']

all_labels = d[u'Label']