Example #1
1
def cmapFromName(cmapname, ncols=256, bad=None):
    """
        Do we need this?
    """
    if not bad:
        bad = [1.0, 1.0, 1.0, 0.0]

    cmap = mpl.cm.get_cmap('jet', ncols)

    if cmapname is not None:

        if cmapname == 'b2r':
            cmap = mpl.colors.LinearSegmentedColormap('my_colormap',
                                                      cdict, ncols)
        elif cmapname == 'viridis' and StrictVersion(mpl.__version__) < StrictVersion('1.5.0'):
            print("Mpl:", mpl.__version__, " using HB viridis")
            cmap = LinearSegmentedColormap.from_list('viridis', viridis_data[::-1])
        elif cmapname == 'viridis_r':
            print("Using HB viridis_r")
            cmap = LinearSegmentedColormap.from_list('viridis', viridis_data)
        else:
            try:
                cmap = mpl.cm.get_cmap(cmapname, ncols)
            except Exception as e:
                print("Could not retrieve colormap ", cmapname, e)

    cmap.set_bad(bad)
    return cmap
def cmap_powerlaw_adjust(cmap, a):
    '''
    returns a new colormap based on the one given
    but adjusted via power-law:

    newcmap = oldcmap**a
    '''
    if a < 0.:
        return cmap
    cdict = copy(cmap._segmentdata)
    def fn(x):
        return (x[0]**a, x[1], x[2])
    for key in ('red','green','blue'):
        try:
            cdict[key] = map(fn, cdict[key])
            cdict[key].sort()
            assert (cdict[key][0]<0 or cdict[key][-1]>1), \
                "Resulting indices extend out of the [0, 1] segment."
        except TypeError:
            def fngen(f):
                def fn(x):
                    return f(x)**a
                return fn
            cdict[key] = fngen(cdict[key])
    newcmap = LinearSegmentedColormap('colormap',cdict,1024)
    newcmap.set_bad(cmap(np.nan))
    return newcmap
Example #3
0
def get_colorMap_intensity_r():
    """ according to the colorweel intensity II"""
    color5 = [0.0,4./255,76./255] 
    color4 = [49./255., 130./255., 0.0]
    color3 = [1.,197./255.,98./255.]
    color2 = [245./255., 179./255., 223./255.]
    color1 = [ 216./255., 1.0,1.0]
    cdict = {'red':   ((0.0, color1[0], color1[0]),
                       (0.25,color2[0] ,color2[0]),
                       (0.5,color3[0] ,color3[0]),
                       (0.75,color4[0] ,color4[0]),
                       (1.00,color5[0] ,color5[0])),
    
             'green': ((0.0, color1[1], color1[1]),
                       (0.25,color2[1] , color2[1]),
                       (0.5,color3[1] ,color3[1]),
                       (0.75,color4[1] ,color4[1]),
                       (1.0,color5[1] ,color5[1])),
    
             'blue':  ((0.0, color1[2], color1[2]),
                       (0.25, color2[2], color2[2]),
                       (0.5, color3[2] ,color3[2]),
                       (0.75,color4[2] ,color4[2]),
                       (1.0,color5[2] ,color5[2]))
            }
    
    hag_cmap  = LinearSegmentedColormap('hag_cmap',cdict)
    hag_cmap.set_bad('black')
    return hag_cmap
Example #4
0
def get_colorMap_heat():
    """ according to the colorweel heat"""
    color1 = np.array([0.0,14.,161.])/255.
    color2 = np.array([0., 125., 11.])/255.
    color3 = np.array([255.,255.,255.])/255.
    color4 = np.array([255., 172., 0.])/255.
#    color5 = np.array([ 184., 0.,18.])/255.
    color5 = np.array([ 163., 0.,119.])/255.
    cdict = {'red':   ((0.0, color1[0], color1[0]),
                       (0.25,color2[0] ,color2[0]),
                       (0.5,color3[0] ,color3[0]),
                       (0.75,color4[0] ,color4[0]),
                       (1.00,color5[0] ,color5[0])),
    
             'green': ((0.0, color1[1], color1[1]),
                       (0.25,color2[1] , color2[1]),
                       (0.5,color3[1] ,color3[1]),
                       (0.75,color4[1] ,color4[1]),
                       (1.0,color5[1] ,color5[1])),
    
             'blue':  ((0.0, color1[2], color1[2]),
                       (0.25, color2[2], color2[2]),
                       (0.5, color3[2] ,color3[2]),
                       (0.75,color4[2] ,color4[2]),
                       (1.0,color5[2] ,color5[2]))
            }
    
    hag_cmap  = LinearSegmentedColormap('hag_cmap',cdict)
    hag_cmap.set_bad('black')
    return hag_cmap
Example #5
0
def demo_compositing(im_r, im_g, im_b, im_rgb):
  fig = plt.figure(3)
  grid = ImageGrid(fig, 222,
                  nrows_ncols = (2, 2),
                  axes_pad = 0.1,
                  )

  cm_red = LinearSegmentedColormap.from_list('cm_black_red',
                                             [cm.colors.cnames['black'], cm.colors.cnames['red']]) 
  cm_green = LinearSegmentedColormap.from_list('cm_black_green',
                                               [cm.colors.cnames['black'], cm.colors.cnames['green']])
  cm_blue= LinearSegmentedColormap.from_list('cm_black_blue',
                                             [cm.colors.cnames['black'], cm.colors.cnames['blue']])

  im_grid = [im_r, im_g, im_b, im_rgb]

  color_maps = [cm_red, cm_green, cm_blue, None]

  for i in range(4):
    cmap = color_maps[i]
    grid[i].imshow(im_grid[i], cmap = cmap, interpolation = 'nearest')
# The AxesGrid object work as a list of axes.


  plt.show()
Example #6
0
def get_colorMap_water():
    """elevation map according to a tundra climate """
    colors = []
#     color.append(np.array([0.,0.,0.])/255.) #white for ice
#     blue = np.array([ 0., 0., 50])/255.
    
    blue = np.array([161., 190., 255.]) / 255.
    colors.append(blue)
    colors.append(blue)
#     colors.append(np.array([39., 62., 44.])/255.)
#     colors.append(np.array([77.,102.,70.])/255.)
#     colors.append(np.array([126., 129., 110.])/255.)
#     colors.append(np.array([ 95., 93.,94.])/255.)
#     colors.append(np.array([1.,1.,1.])) #white for ice
    
    steps = np.linspace(0,1,len(colors))
#     print(len(colors))
#    print(steps)
    red = []
    green = []
    blue = []
    
    for e,c in enumerate(colors):
        red.append((steps[e],c[0],c[0])) 
        green.append((steps[e],c[1],c[1])) 
        blue.append((steps[e],c[2],c[2])) 
        
    cdict = {'red':  red,
             'green': green,
             'blue':  blue
            }
    
    hag_cmap  = LinearSegmentedColormap('svalbard',cdict)
    hag_cmap.set_bad(np.array([ 0., 0.,0.,0]))
    return hag_cmap
Example #7
0
def _make_STMView_colormap(fileName, name='my_cmap'):
    if fileName.endswith('.mat'):
        matFile = _loadmat(_path + fileName)
        for key in matFile:
            if key not in ['__version__', '__header__', '__globals__']:
                return _LSC.from_list(name, matFile[key])
    elif fileName.endswith('.txt'):
        txtFile = _np.loadtxt(_path + fileName)
        return _LSC.from_list(name, txtFile)
Example #8
0
 def __init__(self, name, segmented_data, index=None, **kwargs):
     if index is None:
         # If index not given, RGB colors are evenly-spaced in colormap.
         index = np.linspace(0, 1, len(segmented_data['red']))
     for key, value in segmented_data.items():
         # Combine color index with color values.
         segmented_data[key] = zip(index, value)
     segmented_data = dict((key, [(x, y, y) for x, y in value])
                           for key, value in segmented_data.items())
     LinearSegmentedColormap.__init__(self, name, segmented_data, **kwargs)
Example #9
0
    def __init__(self):
        aggr1=0.2
        aggr2 = 0.2
        #fitnessCmap=LinearSegmentedColormap.from_list('fitness_map',[(aggr1,1,aggr1),(1,1,aggr1),(1,aggr1,aggr1)])
        fitnessCmap = LinearSegmentedColormap.from_list('fitness_map',[(0, 1-aggr1, 0), (1-aggr1, 1-aggr1, 0), (1-aggr1, 0, 0)])
        #complexityCmap = LinearSegmentedColormap.from_list('complexity_map', [(1,1,1),(aggr2, 1, 1), (aggr2,aggr2,1),(aggr2, aggr2, aggr2)])
        complexityCmap = LinearSegmentedColormap.from_list('complexity_map',[(0.6, 0.6, 0.9),(0, 0, 0.3)])

        self.complexity = complexityCmap#plt.get_cmap("cool")
        self.globalm = fitnessCmap
        self.localm = fitnessCmap#plt.get_cmap("RdYlGn")
Example #10
0
def newgray():
    """ Modified version of Oranges."""
    oranges = cm.get_cmap("gray", 100)
    array = oranges(np.arange(100))
    array = array[40:]
    cmap = LinearSegmentedColormap.from_list("newgray", array)
    cm.register_cmap(name='newgray', cmap=cmap)
    array = array[::-1]
    cmap = LinearSegmentedColormap.from_list("newgray_r", array)
    cm.register_cmap(name='newgray_r', cmap=cmap)
    return
Example #11
0
    def __init__(self, name, color_data, index=None, **kwargs):
        if not hasattr(color_data, 'keys'):
            color_data = rgb_list_to_colordict(color_data)

        if index is None:
            # If index not given, RGB colors are evenly-spaced in colormap.
            index = np.linspace(0, 1, len(color_data['red']))

        # Adapt color_data to the form expected by LinearSegmentedColormap.
        color_data = dict((key, [(x, y, y) for x, y in zip(index, value)])
                          for key, value in color_data.items())
        LinearSegmentedColormap.__init__(self, name, color_data, **kwargs)
Example #12
0
def pl_hess_diag(
    gs, gs_y1, gs_y2, x_min_cmd, x_max_cmd, y_min_cmd, y_max_cmd, x_ax, y_ax,
        lkl_method, hess_xedges, hess_yedges, hess_x, hess_y, HD):
    """
    Hess diagram of observed minus best match synthetic cluster.
    """
    ax = plt.subplot(gs[gs_y1:gs_y2, 2:4])
    # Set plot limits
    plt.xlim(x_min_cmd, x_max_cmd)
    plt.ylim(y_min_cmd, y_max_cmd)
    # Set axis labels
    plt.xlabel('$' + x_ax + '$', fontsize=18)
    # Set minor ticks
    ax.minorticks_on()
    ax.xaxis.set_major_locator(MultipleLocator(1.0))
    if gs_y1 == 0:
        ax.set_title("Hess diagram (observed - synthetic)", fontsize=10)
    for x_ed in hess_xedges:
        # vertical lines
        ax.axvline(x_ed, linestyle=':', lw=.8, color='k', zorder=1)
    for y_ed in hess_yedges:
        # horizontal lines
        ax.axhline(y_ed, linestyle=':', lw=.8, color='k', zorder=1)
    if HD.any():
        # Add text box.
        if HD.min() < 0:
            plt.scatter(-100., -100., marker='s', lw=0., s=60, c='#0B02F8',
                        label='{}'.format(int(HD.min())))
        if HD.max() > 0:
            plt.scatter(-100., -100., marker='s', lw=0., s=60, c='#FB0605',
                        label='{}'.format(int(HD.max())))
        # Define custom colorbar.
        if HD.min() == 0:
            cmap = LinearSegmentedColormap.from_list(
                'mycmap', [(0, 'white'), (1, 'red')])
        else:
            # Zero point for empty bins which should be colored in white.
            zero_pt = (0. - HD.min()) / float(HD.max() - HD.min())
            N = 256.
            zero_pt0 = np.floor(zero_pt * (N - 1)) / (N - 1)
            zero_pt1 = np.ceil(zero_pt * (N - 1)) / (N - 1)
            cmap = LinearSegmentedColormap.from_list(
                'mycmap', [(0, 'blue'), (zero_pt0, 'white'), (zero_pt1,
                           'white'), (1, 'red')], N=N)
        ax.pcolormesh(hess_x, hess_y, HD, cmap=cmap, vmin=HD.min(),
                      vmax=HD.max(), zorder=1)
        # Legend.
        handles, labels = ax.get_legend_handles_labels()
        leg = ax.legend(
            handles, labels, loc='lower right', scatterpoints=1, ncol=2,
            columnspacing=.2, handletextpad=-.3, fontsize=10)
        leg.get_frame().set_alpha(0.7)
Example #13
0
def _create_overlay_map():
    #transparent colormap
    global _over_red
    r, g, b = plotParams['mask']['color']
    cdict = {'red': ((0.0, r, r),
                     (1.0, r, r)),
             'green': ((0.0, g, g),
                       (1.0, g, g)),
             'blue': ((0.0, b, b),
                      (1.0, b, b))
            }
    _over_red = LinearSegmentedColormap('MaskOver', cdict)
    _over_red.set_bad(alpha=0)
Example #14
0
def temp_style_file(name):
    """ A context manager for creating an empty style file in the expected path.
    """
    stylelib_path = USER_LIBRARY_PATHS[0]
    if not os.path.exists(stylelib_path):
        os.makedirs(stylelib_path)
    srcname = os.path.abspath(os.path.join(os.path.dirname(__file__),name))
    dstname = os.path.join(stylelib_path, os.path.basename(name))
    if not os.path.exists(srcname):
        raise RuntimeError('Cannot use file at "' + srcname + '". This file does not exist.')
    if os.path.exists(dstname):
        #raise RuntimeError('Cannot create a temporary file at "' + dstname + '". This file exists already.')
        warnings.warn('Overwriting the temporary file at "' + dstname + '".')
    
    #with open(filename, 'w'):
    #    pass
    
    shutil.copy2(srcname, dstname)
    
    
    rgb = [
      (  0./255. ,   0./255. ,   0./255.),     
      (  0./255. , 102./255. ,  51./255.),
      #(114./255. , 121./255. , 126./255.),
      ( 91./255. , 172./255. ,  38./255.),
      (217./255. , 220./255. , 222./255.),
      (255./255. , 255./255. , 255./255.)
      ]

    # create map and register it together with reversed colours
    maps = []
    maps.append(LinearSegmentedColormap.from_list('IPU'  , rgb))
    maps.append(LinearSegmentedColormap.from_list('IPU_r', rgb[::-1]))

    for cmap in maps:
        mplcm.register_cmap(cmap=cmap)
        #self._color_maps[cmap.name] = cmap
    
    yield
    os.remove(dstname)



#print('# styles available:', len(plt.style.available))
#
#with temp_style_file('dummy.mplstyle'):
#    print('# before reload:', len(plt.style.available))
#
#    plt.style.reload_library()
#    print('# after reload:', len(plt.style.available))
Example #15
0
def custom_div_cmap(numcolors=11, name='custom_div_cmap',
                    mincol='r', midcol='white', maxcol='g'):
    from matplotlib.colors import LinearSegmentedColormap
    cmap = LinearSegmentedColormap.from_list(name=name,
                                             colors =[mincol, midcol, maxcol],
                                             N=numcolors)
    return cmap
def generate_cmap(colors):
    values = range(len(colors))
    vmax = np.ceil(np.max(values))
    color_list = []
    for v, c in zip(values, colors):
        color_list.append( ( v/ vmax, c) )
    return LinearSegmentedColormap.from_list('custom_cmap', color_list)
Example #17
0
def set_binary_lut(mayavi_obj, color1, color2, use_vector_lut=False):
    '''
    sets the lookup table to of the mayavi object provided to a 2 color
    color map ranging between color1 and color2. Also sets the appropriate
    attributes of the LUT so that the scalars are immediately displayed

    Parameters
    ----------
    mayavi_obj : Instance(mlab.pipeline)
        A mayavi object with a module_manager instance
    color1 : 3-tuple
        An RGB color represented as a 3-tuple with values [0,1], and
        corresponding to the scalar value 0
    color2 : 3-tuple
        An RGB color represented as a 3-tuple with values [0,1], and
        corresponding to the scalar value 1
    use_vector_lut : Bool
        If true, uses the vector_lut_manager. Otherwise, uses the
        scalar_lut_manager. Defaults to False.
    '''
    mayavi_obj.actor.mapper.scalar_visibility = True

    if use_vector_lut:
        lut_mgr=mayavi_obj.module_manager.vector_lut_manager
    else:
        lut_mgr=mayavi_obj.module_manager.scalar_lut_manager

    lut_mgr.lut.table = map_to_table(
        LinearSegmentedColormap.from_list('ign', [color1, color2]))
        #map(mayavi2vtk_color, [color1, color2])))

    #lut_mgr.use_default_range = False
    lut_mgr.data_range = [0,1]
Example #18
0
def _init_mutable_colormap():
    """Create a matplotlib colormap that will be updated by the widgets."""
    greys = color_palette("Greys", 256)
    cmap = LinearSegmentedColormap.from_list("interactive", greys)
    cmap._init()
    cmap._set_extremes()
    return cmap
Example #19
0
def get_color_map(num_states):
    colours = plt.cm.viridis(np.linspace(0, 1, num_states))
    colormap = {i: colours[i] for i in range(num_states)}
    cmap = LinearSegmentedColormap.from_list('name',
                                             list(colormap.values()),
                                             num_states)
    return colormap, cmap
Example #20
0
def plotBackground(areaPoly=None, ax=None, globalOrigin=None):
	"""
	plots the background from the tiff-file given. In the future, maybe data is taken from
	google maps or something similar?

	#local coordinates in areaPoly, with origin in origin
	origin is given in sweref99 coordinates, not local.
	"""	
	if not areaPoly: raise Exception('need area polygon in order to plot background')
	if not ax:
		fig=plt.figure()
		ax=fig.add_subplot(111, aspect='equal')
	figCorner=(595000, 6725000) #for this specific area, not general		
	if not globalOrigin:
		globalOrigin=figCorner
	#now, let our global origin be in the origin..
	w=5000 #for this specific image..
	h=5000
	figorigin=np.array(figCorner)-np.array(globalOrigin)
	limits=[figorigin[0], figorigin[0]+w, figorigin[1], figorigin[1]+h]
	folder=os.path.dirname(os.path.abspath(__file__))
	folder=os.path.join(folder, 'GIS')
	map=LinearSegmentedColormap.from_list('lightgray', ['#666666', '#C5C5C5']) #our own cmap
	bg=mpimg.imread(os.path.join(folder,'672_59_11.tif'))
	im = plt.imshow(bg, cmap=map,origin='lower', extent=limits)
	
	#now, adjust to our polygon..
	limits=list(fun.polygonLim(areaPoly))
	w=max(10, max(limits)/10.)
	limits[0]-=w #to get some space to polygon
	limits[1]+=w
	limits[2]-=w
	limits[3]+=w
	ax.axis(limits)
	return ax
Example #21
0
    def heat_map(self, cmap='RdYlGn', vmin=None, vmax=None, font_cmap=None):
        if cmap is None:
            carr = ['#d7191c', '#fdae61', '#ffffff', '#a6d96a', '#1a9641']
            cmap = LinearSegmentedColormap.from_list('default-heatmap', carr)

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

        vals = self.actual_values.astype(float)
        if vmin is None:
            vmin = vals.min().min()
        if vmax is None:
            vmax = vals.max().max()
        norm = (vals - vmin) / (vmax - vmin)
        for ridx in range(self.nrows):
            for cidx in range(self.ncols):
                v = norm.iloc[ridx, cidx]
                if np.isnan(v):
                    continue
                color = cmap(v)
                hex = rgb2hex(color)
                styles = {'BACKGROUND': HexColor(hex)}
                if font_cmap is not None:
                    styles['TEXTCOLOR'] = HexColor(rgb2hex(font_cmap(v)))
                self.iloc[ridx, cidx].apply_styles(styles)
        return self
Example #22
0
File: table.py Project: vanife/tia
    def heat_map(self, cmap="RdYlGn", vmin=None, vmax=None, font_cmap=None):
        if cmap is None:
            carr = ["#d7191c", "#fdae61", "#ffffff", "#a6d96a", "#1a9641"]
            cmap = LinearSegmentedColormap.from_list("default-heatmap", carr)

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

        vals = self.actual_values.astype(float)
        if vmin is None:
            vmin = vals.min().min()
        if vmax is None:
            vmax = vals.max().max()
        norm = (vals - vmin) / (vmax - vmin)
        for ridx in range(self.nrows):
            for cidx in range(self.ncols):
                v = norm.iloc[ridx, cidx]
                if np.isnan(v):
                    continue
                color = cmap(v)
                hex = rgb2hex(color)
                styles = {"BACKGROUND": HexColor(hex)}
                if font_cmap is not None:
                    styles["TEXTCOLOR"] = HexColor(rgb2hex(font_cmap(v)))
                self.iloc[ridx, cidx].apply_styles(styles)
        return self
def plot_confusion_matrix(conf_matrix, cm_labels):

    startcolor = "#cccccc"
    midcolor = "#08519c"
    endcolor = "#08306b"

    b_g2 = LinearSegmentedColormap.from_list("B_G2", [startcolor, midcolor, endcolor])

    fig = plt.figure()
    ax = fig.add_subplot(111)
    cax = ax.matshow(conf_matrix, cmap=b_g2)
    fig.colorbar(cax)
    plt.title("Neural Net Confusion Matrix \n", fontsize=16)

    ax.set_xticklabels([""] + cm_labels, fontsize=13)
    ax.set_yticklabels([""] + cm_labels, fontsize=13)

    ax.xaxis.set_ticks_position("none")
    ax.yaxis.set_ticks_position("none")

    spines_to_remove = ["top", "right", "left", "bottom"]

    plt.xlabel("Predicted", fontsize=14)
    plt.ylabel("Actual", fontsize=14)
    # plt.savefig(os.path.join(graph_dir, graph_fn))

    plt.show()
Example #24
0
def like2d(datx,daty,weights=None,
           nbins=15, which=[.68,.95], range=None,
           filled=True, color=None, cmap=None, smooth=None,
           ax=None,
           **kwargs):
    
    from matplotlib.pyplot import gca, get_cmap
    from matplotlib.mlab import movavg
    from matplotlib.colors import LinearSegmentedColormap
    from scipy.ndimage import gaussian_filter
    
    if ax is None: ax = gca()
    if weights is None: weights=ones(len(datx))
    if color is None: color = kwargs.pop('c') if 'c' in kwargs else 'b' 
    
    H,xe,ye = histogram2d(datx,daty,nbins,weights=weights, range=range)
    xem, yem = movavg(xe,2), movavg(ye,2)
    
    kwargs = dict(levels=confint2d(H, sorted(which)[::-1]+[0]),**kwargs)
    if smooth: H = gaussian_filter(H,smooth)
    args = (xem,yem,transpose(H))
    
    if cmap is None: 
        cmap = {'b':'Blues',
                'g':'Greens',
                'r':'Reds',
                'orange':'Oranges',
                'grey':'Greys'}.get(color)
        if cmap is None: cmap = LinearSegmentedColormap.from_list(None,['w',color])
        else: cmap = get_cmap(cmap)
        
    if filled: ax.contourf(*args,cmap=cmap,**kwargs)
    ax.contour(*args,colors=color,**kwargs)
Example #25
0
def green_white(levels=10):
    """ Generate a colormap from green to white.
    
    """
    colors =[(0., 0.5, 0.),
             (1., 1., 1.)]
    return LinearSegmentedColormap.from_list(colors=colors, name='green_white', N=levels)
Example #26
0
def green_lightblue_blue(levels=10):
    """ Generate a colormap from green to light blue then dark blue.
    """
    colors =[(0., 1., 0.), 
             (0., 1., 1.), 
             (0., 0., 1.)]
    return LinearSegmentedColormap.from_list(colors=colors, name='green_lightblue_blue', N=levels)
def plot_confusion_matrix(model_name, conf_matrix, labels, save, cmap, graph_fn='cfm.png'):

    startcolor = '#cccccc'
    midcolor = '#08519c'
    endcolor = '#08306b'

    b_g2 = LinearSegmentedColormap.from_list('B_G2', [startcolor, midcolor, endcolor])

    fig = plt.figure()
    ax = fig.add_subplot(111)
    cax = ax.matshow(conf_matrix, cmap=b_g2)
    fig.colorbar(cax)
    plt.title('Jeeves Confusion Matrix \n', fontsize=16)
    ax.set_xticklabels([''] + labels, fontsize=13)
    ax.set_yticklabels([''] + labels, fontsize=13)
    ax.xaxis.set_ticks_position('none')
    ax.yaxis.set_ticks_position('none')
    spines_to_remove = ['top', 'right', 'left', 'bottom']
    # for spine in spines_to_remove:
    #     ax.spines[spine].set_visible(False)
    plt.xlabel('Predicted', fontsize=14)
    plt.ylabel('Actual', fontsize=14)
    if save:
        plt.savefig(os.path.join(graph_dir, graph_fn))
    plt.show()
Example #28
0
File: cea.py Project: gyfis/pycea
def heatmap(cea, synchronous=True):
    data = cea.grid.get_heat_data()
    fig, ax = plt.subplots()
    cmap = LinearSegmentedColormap.from_list('my cmap', ['black', 'white'])
    heatmap_plot = ax.imshow(data, interpolation='nearest', cmap=cmap, vmin=0, vmax=1.0)

    def init():
        heatmap_plot.set_data(cea.grid.get_heat_data())
        return heatmap_plot

    def animate(i):
        if i > 2:
            cea.iterate_population() if synchronous else cea.iterate_individual()
        heatmap_plot.set_data(cea.grid.get_heat_data())
        return heatmap

    anim = animation.FuncAnimation(fig, animate, init_func=init, frames=150)

    plt.axis('off')
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)

    anim.save('gifs/slash_final.gif', writer='imagemagick')

    plt.show()
Example #29
0
def get_with_white_added(cm_name_default, white_start=0.0, white_end=0.1,
                         ncolors_out=100):

    cmap = cm.get_cmap(cm_name_default)
    ncolors = cmap.N


    clist = []

    lower = []
    if white_start > 0:
        lower = cmap(np.linspace(0, white_start, int(white_start * ncolors)))
    clist.append(lower)

    white = np.ones((int((white_end - white_start) * ncolors), 4))
    clist.append(white)

    upper = []
    if white_end < 1:
        upper = cmap(np.linspace(white_end, 1, int((1 - white_end) * ncolors)))
    clist.append(upper)

    colors = np.vstack(tuple([p for p in clist if len(p) > 0]))

    return LinearSegmentedColormap.from_list("mycmap", colors, N=ncolors_out)
Example #30
0
def swap_colors(json_file_path):
    '''
    Switches out color ramp in meta.json files.
    Uses custom color ramp if provided and valid; otherwise falls back to nextstrain default colors.
    N.B.: Modifies json in place and writes to original file path.
    '''
    j = json.load(open(json_file_path, 'r'))
    color_options = j['color_options']

    for k,v in color_options.items():
        if 'color_map' in v:
            categories, colors = zip(*v['color_map'])

            ## Use custom colors if provided AND present for all categories in the dataset
            if custom_colors and all([category in custom_colors for category in categories]):
                colors = [ custom_colors[category] for category in categories ]

            ## Expand the color palette if we have too many categories
            elif len(categories) > len(default_colors):
                from matplotlib.colors import LinearSegmentedColormap, to_hex
                from numpy import linspace
                expanded_cmap = LinearSegmentedColormap.from_list('expanded_cmap', default_colors[-1], N=len(categories))
                discrete_colors = [expanded_cmap(i) for i in linspace(0,1,len(categories))]
                colors = [to_hex(c).upper() for c in discrete_colors]

            else: ## Falls back to default nextstrain colors
                colors = default_colors[len(categories)] # based on how many categories are present; keeps original ordering

            j['color_options'][k]['color_map'] = map(list, zip(categories, colors))

    json.dump(j, open(json_file_path, 'w'), indent=1)
Example #31
0
           [0.9418, 0.93824, 0.40155],      
           [0.94585, 0.94289, 0.40136],      
           [0.95, 0.94756, 0.40118],      
           [0.95426, 0.95224, 0.40103],      
           [0.9586, 0.95696, 0.4009],      
           [0.96301, 0.96169, 0.40079],      
           [0.9675, 0.96644, 0.40069],      
           [0.97205, 0.97121, 0.4006],      
           [0.97664, 0.97598, 0.40052],      
           [0.98129, 0.98078, 0.40045],      
           [0.98597, 0.98558, 0.40038],      
           [0.99068, 0.9904, 0.40032],      
           [0.99542, 0.99522, 0.40026],      
           [1, 1, 0.4002]]      
      
buda_map = LinearSegmentedColormap.from_list('buda', cm_data)      
# For use of "viscm view"      
test_cm = buda_map      
      
if __name__ == "__main__":      
    import matplotlib.pyplot as plt      
    import numpy as np      
      
    try:      
        from viscm import viscm      
        viscm(buda_map)      
    except ImportError:      
        print("viscm not found, falling back on simple display")      
        plt.imshow(np.linspace(0, 100, 256)[None, :], aspect='auto',      
                   cmap=buda_map)      
    plt.show()      
Example #32
0
    def update(self, data):
        feat_map = None
        if data["ml_frame_data"] is not None:
            frame_data = data["ml_frame_data"]
            feat_map = frame_data["current_frame"]["feature_map"]
            frame_size = frame_data["frame_info"]["frame_size"]
            frame_pad = frame_data["frame_info"]["frame_pad"]
            frame_complete = frame_data["current_frame"]["frame_complete"]
            model_dimension = data["ml_frame_data"]["model_dimension"]
        else:
            return

        sensors = []
        if self.sensor_config is not None:
            sensors = self.sensor_config.sensor

        mode = data["sensor_config"].mode

        if self.first:
            self.first = False
            self.env_plot_max_y = 0
            s_buff = frame_size + 2 * frame_pad
            self.feat_plot.resetTransform()
            if model_dimension > 1:
                self.feat_plot.translate(-frame_pad, 0)
            self.feat_plot_image.setXRange(-frame_pad, s_buff - frame_pad)

            self.border_left.setValue(0)
            self.border_right.setValue(frame_size)

            self.border_left.show()
            self.border_right.show()

            nr_sensors = len(sensors)
            self.hist_plot_max_y = np.zeros(nr_sensors)

            lut = utils.pg_mpl_cmap("viridis")
            if mode == Mode.SPARSE:
                cmap_cols = [
                    "steelblue", "lightblue", "#f0f0f0", "moccasin",
                    "darkorange"
                ]
                cmap = LinearSegmentedColormap.from_list("mycmap", cmap_cols)
                cmap._init()
                lut = (cmap._lut * 255).view(np.ndarray)

            for i in range(4):
                if (i + 1) in sensors:
                    self.hist_plot_images[i].show()
                    self.set_axis(data, self.hist_plot_images[i])
                    self.hist_plots[i].setLookupTable(lut)
                    if (i + 1) == sensors[0]:
                        self.hist_plot_images[i].setLabel(
                            "left", "Distance (mm)")
                else:
                    self.hist_plot_images[i].hide()

        for idx, sensor in enumerate(sensors):

            if mode == Mode.SPARSE:
                data_history_adj = data["hist_env"][idx, :, :].T - 2**15
                sign = np.sign(data_history_adj)
                data_history_adj = np.abs(data_history_adj)
                data_history_adj /= data_history_adj.max()
                data_history_adj = np.power(data_history_adj,
                                            1 / 2.2)  # gamma correction
                data_history_adj *= sign
                self.hist_plots[sensor - 1].updateImage(data_history_adj,
                                                        levels=(-1.05, 1.05))
            else:
                max_val = np.max(data["env_ampl"][idx])

                ymax_level = min(
                    1.5 * np.max(np.max(data["hist_env"][idx, :, :])),
                    self.hist_plot_max_y[idx])

                if max_val > self.hist_plot_max_y[idx]:
                    self.hist_plot_max_y[idx] = 1.2 * max_val

                self.hist_plots[sensor - 1].updateImage(
                    data["hist_env"][idx, :, :].T, levels=(0, ymax_level))

        if self.show_predictions and data.get("prediction") is not None:
            self.predictions_plot_window.show()
            prediction_text = "{} ({:.2f}%)".format(
                data["prediction"]["prediction"],
                data["prediction"]["confidence"] * 100,
            )
            self.predictions_plot_window.setTitle(prediction_text)
            predictions = data["prediction"]["label_predictions"]
            pred_num = data["prediction"]["number_labels"]
            pred_history = data["prediction_hist"]
            if len(self.prediction_plots) < pred_num:
                self.generate_predciction_plots(predictions, pred_num)

            if pred_history is not None:
                for idx in range(pred_num):
                    self.prediction_plots[idx].setData(pred_history[idx, :])

        detected = True
        if feat_map is None:
            detected = False

        if detected and not len(feat_map):
            detected = False

        if self.print_info_text:
            feature_nr = frame_data["current_frame"]["frame_nr"]
            self.feature_nr_text.setText("Feature: {}".format(feature_nr))
            if not detected:
                motion_score = frame_data.pop("motion_score", None)
                text = "Waiting..."
                if motion_score is not None:
                    if isinstance(motion_score, float):
                        motion_score = "{:.1f}".format(motion_score)
                    text = "Waiting.. (Motion score: {})".format(motion_score)
                self.detected_text.setText(text)
                return
            else:
                self.detected_text.setText("Collecting..")

        if not frame_complete:
            self.border_rolling.show()
            self.border_rolling.setValue(
                frame_data["current_frame"]["sweep_counter"] - frame_pad)
        else:
            self.border_rolling.hide()

        if feat_map is None:
            return

        self.feat_plot_image.setYRange(0, feat_map.shape[0])

        map_max = 1.2 * np.max(feat_map)
        ymax_level = max(map_max, self.env_plot_max_y)

        g = 1 / 2.2
        feat_map = 254 / (ymax_level + 1.0e-9)**g * feat_map**g

        feat_map[feat_map > 254] = 254

        self.feat_plot.updateImage(feat_map.T, levels=(0, 256))

        if map_max > self.env_plot_max_y:
            self.env_plot_max_y = map_max
plottype = "pseudo"  # map of pseudo for map or pseudo section
diffyn = "y"
sline = "ns"

# ==============================================================================
# a few constants
# ==============================================================================

refe = 23
# phase tensor map
ptcmapdict = {
    "red": ((0.0, 1.0, 1.0), (1.0, 1.0, 1.0)),
    "green": ((0.0, 0.0, 1.0), (1.0, 0.0, 1.0)),
    "blue": ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)),
}
ptcmap = LinearSegmentedColormap("ptcmap", ptcmapdict, 256)

# phase tensor map for difference (reverse)
ptcmapdictr = {
    "red": ((0.0, 1.0, 1.0), (1.0, 1.0, 1.0)),
    "green": ((0.0, 1.0, 0.0), (1.0, 1.0, 0.0)),
    "blue": ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)),
}
ptcmapr = LinearSegmentedColormap("ptcmapr", ptcmapdictr, 256)

# resistivity tensor map for calculating delta
ptcmapdict2 = {
    "red": ((0.0, 0.0, 1.0), (1.0, 0.0, 1.0)),
    "green": ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)),
    "blue": ((0.0, 1.0, 0.0), (1.0, 1.0, 0.0)),
}
    '#ff6555',  # red apple
    '#66c56c',  # moss green
    '#f4b247'  # mustard yellow
    '#d62728',  # brick red
    '#9467bd',  # muted purple
    '#8c564b',  # chestnut brown
    '#e377c2',  # raspberry yogurt pink
    '#bcbd22',  # curry yellow-green
    'rgb(247,104,161)',  # pink
    'rgb(104,171,184)'  # teal
]

color_array_solid = color_array[::2]
color_array_shade = color_array[1::2]

cm_confusion = LinearSegmentedColormap.from_list(
    name='confusion', colors=[color_array[0], color_array[2]], N=100)
cm_colors = LinearSegmentedColormap.from_list(name='colors',
                                              colors=color_array,
                                              N=len(color_array))
cm_confusion.set_bad((1, 0, 0, 0))

color_bw_signal = tuple(np.array([210, 242, 201]) / 255)
color_bw_noise = tuple(np.array([255, 201, 201]) / 255)

#cmap_list = [(1, 1, 1), color_array[2], color_array[6], color_array[8], color_array[0], color_array[4]]  # R -> G -> B
cmap_list = [(1, 1, 1), color_array[6], color_array[4]]  # R -> G -> B
cmap_conf = LinearSegmentedColormap.from_list("cmap_conf", cmap_list, N=100)


def show_colors():
    fig = plt.figure(figsize=(9, 4))
        Chem.Compute2DCoords(mol)

#    print index, len(annotateWeights)

# normalize the weights
    normweights = [w / maxval for w in weights]

    # make a custom red - white - green color map
    # slight tint so O and Cl are still visible
    cdict = {
        'red': ((0.0, 1.0, 1.0), (0.5, 1.0, 1.0), (1.0, 0.2, 0.2)),
        'green': ((0.0, 0.2, 0.2), (0.5, 1.0, 1.0), (1.0, 1.0, 1.0)),
        'blue': ((0.0, 0.2, 0.2), (0.5, 1.0, 1.0), (1.0, 0.2, 0.2))
    }
    colors = LinearSegmentedColormap('RWG', cdict)

    size = (args.size, args.size)  # get errors if not square
    # this is an awfully hacky way to standardize the color ranges across
    # all the processed molecules. A major issue is that scale and sigma
    # are in image coordinates
    sigma = 0.05
    if len(mols) and mols[0].GetNumBonds() > 0:
        mol = mols[0]
        rdkit.Chem.Draw.MolToMPL(mol, size=size)  # initialize 2D coordinates
        plt.clf()
        bond = mol.GetBondWithIdx(0)
        idx1 = bond.GetBeginAtomIdx()
        idx2 = bond.GetEndAtomIdx()
        sigma = 0.5 * math.sqrt(
            sum([(mol._atomPs[idx1][i] - mol._atomPs[idx2][i])**2
Example #36
0
def visualize_ensemble_raster(crop, osc, raster_data, raster_mask, savepath,
                              save, cmap, colorbar_name, data_type):

    # Initialize the map figure using Basemap
    m = Basemap(
        projection='cyl',
        resolution='c',
        llcrnrlat=-60,
        urcrnrlat=90,
        llcrnrlon=-180,
        urcrnrlon=180,
    )

    fig = plt.figure()
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.set_axis_off()
    fig.add_axes(ax)

    # Create the colorscale, using RGB information and the function LinearSegmentedColormap
    if cmap == 'redblue':
        cdict = {
            'blue': ((0.0, 0.0, 0.0), (0.25, 0.0, 0.0), (0.49, 0.8, 0.9),
                     (0.51, 0.9, 1.0), (0.75, 1.0, 1.0), (1.0, 0.4, 1.0)),
            'green': ((0.0, 0.0, 0.0), (0.25, 0.0, 0.0), (0.49, 0.9, 0.9),
                      (0.51, 0.9, 0.9), (0.75, 0.0, 0.0), (1.0, 0.0, 0.0)),
            'red': ((0.0, 0.0, 0.4), (0.25, 1.0, 1.0), (0.49, 1.0, 0.9),
                    (0.51, 0.9, 0.8), (0.75, 0.0, 0.0), (1.0, 0.0, 0.0))
        }
        clim = (-5, 5)
        cbar_label = 'Crop yield deviation (%) per unit index change'
        cmap = LinearSegmentedColormap('cmap', cdict)

    elif cmap == 'whiteblue':
        cdict = {
            'blue': ((0.0, 0.8, 0.9), (0.01, 0.9, 1.0), (0.5, 1.0, 1.0),
                     (1.0, 0.4, 1.0)),
            'green': ((0.0, 0.9, 0.9), (0.01, 0.9, 0.9), (0.5, 0.0, 0.0),
                      (1.0, 0.0, 0.0)),
            'red': ((0.0, 1.0, 0.9), (0.01, 0.9, 0.8), (0.5, 0.0, 0.0),
                    (1.0, 0.0, 0.0))
        }
        clim = (0, 100)
        cbar_label = 'Proportion of individual models that agree with the ensemble sensitivity sign'
        cmap = LinearSegmentedColormap('cmap', cdict)


# Modify settings for the raster visualization.
    raster_data = np.flipud(raster_data)
    raster_mask = np.flipud(raster_mask)
    mask_index = raster_mask * np.isnan(raster_data)
    raster_data[mask_index] = 0
    cs = m.imshow(raster_data[60:, :] * 100, clim=clim, cmap=cmap)

    m.drawcoastlines(linewidth=0.25)
    #    m.drawcountries(linewidth=0.25)
    m.fillcontinents(color='white', lake_color='white', zorder=0)

    m.drawmapboundary(fill_color='White')

    # Save the figure.
    if save == 1:
        os.chdir(savepath)
        plt.savefig(crop + '_' + osc + '_' + data_type + '.png',
                    dpi=300,
                    bbox_inches='tight')
    plt.show(m)
    # If colorbar for sensitivity doesn't exist in folder, create it.
    if colorbar_name not in os.listdir(savepath):
        # add colorbar
        plt.figure()
        cs = plt.imshow(raster_data[60:, :] * 100, clim=clim, cmap=cmap)
        plt.gca().set_visible(False)
        cbar = plt.colorbar(cs, extend='both', orientation='horizontal')
        cbar.set_label(cbar_label, fontsize=12)
        plt.savefig(colorbar_name, dpi=300, bbox_inches='tight')
    def visualize(self, title_override=None, show_title=True, show_container_value_and_weight=True, show_outside_value_and_weight=True, show_outside_items=True, color_items_by_profit_ratio=True, show_item_value_and_weight=True, show_value_and_weight_for_container_items=False, show_reference_positions=False, show_bounding_boxes=False, show_value_weight_ratio_bar=True, force_show_color_bar_min_max=False, show_plot=True, save_path=None):

        """Visualize the solution, with placed items in their real position and rotation, and the other ones visible outside the container"""

        can_consider_weight = self.problem.container.max_weight != np.inf

        # set up the plotting figure
        fig_size = (13, 6.75)
        dpi = 160
        fig = plt.figure(figsize=fig_size, dpi=dpi)
        if show_outside_items:
            ax1 = fig.add_subplot(1, 2, 1)
            ax1.set(aspect="equal")
            ax2 = fig.add_subplot(1, 2, 2, sharex=ax1, sharey=ax1)
            ax2.set(aspect="equal")
            ax2.tick_params(axis="both", which="major", labelsize=11)
        else:
            ax1 = plt.gca()
            ax1.set(aspect="equal")
            ax2 = None
        ax1.tick_params(axis="both", which="major", labelsize=11)
        if show_title:
            fig.suptitle(title_override if title_override else "2D Irregular Shape Packing + 0/1 Knapsack Problem")

        outside_item_bounds = dict()
        total_outside_item_width = 0.

        # represent the container
        x, y = get_shape_exterior_points(self.problem.container.shape, True)
        container_color = (.8, .8, .8)
        boundary_color = (0., 0., 0.)
        ax1.plot(x, y, color=boundary_color, linewidth=1)
        ax1.fill(x, y, color=container_color)
        empty_color = (1., 1., 1.)
        if type(self.problem.container.shape) == MultiPolygon:
            for geom in self.problem.container.shape.geoms:
                for hole in geom.interiors:
                    x, y = get_shape_exterior_points(hole, True)
                    fill_color = empty_color
                    boundary_color = (0., 0., 0.)
                    ax1.plot(x, y, color=boundary_color, linewidth=1)
                    ax1.fill(x, y, color=fill_color)

        font = {'family': 'serif', 'color':  'black', 'weight': 'normal', 'size': 12}

        # show the total value and weight in the container, and the maximum acceptable weight (capacity)
        if show_container_value_and_weight:
            value_weight_string = "V={}".format(self.value if can_consider_weight else int(self.value))
            if can_consider_weight:
                value_weight_string += ", W={}, Wmax={}".format(self.weight, self.problem.container.max_weight)
            ax1.set_title("Items inside the container\n({})".format(value_weight_string), fontsize=13)

        # determine the range of item profitability ratio, for later coloring of items
        min_profit_ratio = np.inf
        max_profit_ratio = -np.inf
        item_profit_ratios = dict()
        for item_index, item in self.problem.items.items():
            if item.weight == 0:
                profit_ratio = np.inf
            else:
                profit_ratio = item.value / item.weight
            item_profit_ratios[item_index] = profit_ratio
            min_profit_ratio = min(min_profit_ratio, profit_ratio)
            max_profit_ratio = max(max_profit_ratio, profit_ratio)
        best_profit_color = (1, 0.35, 0)
        worst_profit_color = (1, 0.8, 0.8)
        color_interp = interpolate.interp1d([min_profit_ratio, max_profit_ratio], [0, 1])

        # if possible, add a color-bar showing the value/weight ratio scale
        if show_value_weight_ratio_bar:
            fig.subplots_adjust(bottom=0.15)
            fig.subplots_adjust(wspace=0.11)
            bar_x, bar_y, bar_width, bar_height = 0.5, 0.1, 0.3, 0.02
            bar_ax = fig.add_axes([bar_x - bar_width * 0.5, bar_y - bar_height * 0.5, bar_width, bar_height])
            color_map = LinearSegmentedColormap.from_list(name="profit-colors", colors=[worst_profit_color, best_profit_color])
            norm = colors.Normalize(vmin=min_profit_ratio, vmax=max_profit_ratio)
            if force_show_color_bar_min_max:
                ticks = np.linspace(min_profit_ratio, max_profit_ratio, 7, endpoint=True)
            else:
                ticks = None
            bar = colorbar.ColorbarBase(bar_ax, cmap=color_map, norm=norm, ticks=ticks, orientation='horizontal', ticklocation="bottom")
            bar.set_label(label="value/weight ratio", size=13)
            bar.ax.tick_params(labelsize=11)

        for item_index, item in self.problem.items.items():

            # represent the placed items
            if item_index in self.placed_items:

                if color_items_by_profit_ratio:
                    fill_color = worst_profit_color + tuple(best_profit_color[i] - worst_profit_color[i] for i in range(len(best_profit_color))) * color_interp(item_profit_ratios[item_index])
                else:
                    fill_color = (1, 0.5, 0.5)

                self.show_item(item_index, ax1, boundary_color, fill_color, container_color, show_item_value_and_weight and show_value_and_weight_for_container_items, font, show_bounding_boxes, show_reference_positions)

            # determine the boundary rectangle of the outside-of-container items
            elif show_outside_items and ax2:

                outside_item_bounds[item_index] = get_bounds(self.problem.items[item_index].shape)
                total_outside_item_width += abs(outside_item_bounds[item_index][2] - outside_item_bounds[item_index][0])

        # show the outside-of-container items
        if show_outside_items and ax2:

            out_value_sum = 0
            out_weight_sum = 0
            row_num = max(1, int(np.log10(len(self.problem.items)) * (3 if len(self.problem.items) < 15 else 4)))
            row = 0
            width = 0
            max_width = 0
            row_height = 0
            height = 0
            for item_index, bounds in outside_item_bounds.items():

                out_value_sum += self.problem.items[item_index].value
                out_weight_sum += self.problem.items[item_index].weight

                if color_items_by_profit_ratio:
                    fill_color = worst_profit_color + tuple(best_profit_color[i] - worst_profit_color[i] for i in range(len(best_profit_color))) * color_interp(item_profit_ratios[item_index])
                else:
                    fill_color = (1, 0.5, 0.5)

                min_x, min_y, max_x, max_y = bounds
                shape_width = abs(max_x - min_x)
                shape_height = abs(max_y - min_y)

                shape_center = get_bounding_rectangle_center(self.problem.items[item_index].shape)
                position_offset = (width + shape_width * 0.5 - shape_center[0], row_height + shape_height * 0.5 - shape_center[1])
                self.show_item(item_index, ax2, boundary_color, fill_color, empty_color, show_item_value_and_weight, font, show_bounding_boxes, show_reference_positions, position_offset)

                height = max(height, row_height + shape_height)

                width += shape_width
                max_width += width
                if width >= total_outside_item_width / row_num:
                    row += 1
                    width = 0
                    row_height = height

            # show the value and weight outside the container
            if show_outside_value_and_weight and ax2:
                value_weight_string = "V={}".format(out_value_sum if can_consider_weight else int(out_value_sum))
                if can_consider_weight:
                    value_weight_string += ", W={}".format(out_weight_sum)
                ax2.set_title("Items outside the container\n({})".format(value_weight_string), fontsize=13)

        fig = plt.gcf()

        if show_plot:
            plt.show()

        if save_path:
            fig.savefig(save_path, bbox_inches="tight", dpi=dpi)
            plt.close(fig)
Example #38
0
import math
from matplotlib import rc

from scipy import signal

from scipy.fftpack import fft

#prec = np.float32
prec = np.float64

cdict1 = {
    'red': ((0.0, 0.0, 0.0), (0.5, 0.0, 0.1), (1.0, 1.0, 1.0)),
    'green': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)),
    'blue': ((0.0, 0.0, 1.0), (0.5, 0.1, 0.0), (1.0, 0.0, 0.0))
}
blue_red1 = LinearSegmentedColormap('BlueRed1', cdict1)


def readFromFile(name,
                 newshape=None,
                 typ=np.float32,
                 numberOfItems=-1,
                 seek_num=-1):
    ff = open(name, 'rb')
    if seek_num > 0:
        ff.seek(seek_num)
    data = np.fromfile(
        ff, dtype=typ,
        count=numberOfItems)  # numberOfItems = -1 means all items
    #print(name, data.shape, newshape)
    ff.close()
Example #39
0
def make_dismph_colormap():
    """Make a custom colormap like the one used in dismph.

    The list was created from dismphN.mat in geodmod which is a 64 segmented colormap
    using the following:
      from scipy.io import loadmat
      cmap = loadmat('dismphN.mat',struct_as_record=True)['dismphN']
      from matplotlib.colors import rgb2hex
      list=[]
      for i in cmap: list.append(rgb2hex(i))

    Source: https://imaging.unavco.org/data/geoslc/geoslc2intf.py
    """
    colors = [
        "#f579cd",
        "#f67fc6",
        "#f686bf",
        "#f68cb9",
        "#f692b3",
        "#f698ad",
        "#f69ea7",
        "#f6a5a1",
        "#f6ab9a",
        "#f6b194",
        "#f6b78e",
        "#f6bd88",
        "#f6c482",
        "#f6ca7b",
        "#f6d075",
        "#f6d66f",
        "#f6dc69",
        "#f6e363",
        "#efe765",
        "#e5eb6b",
        "#dbf071",
        "#d0f477",
        "#c8f67d",
        "#c2f684",
        "#bbf68a",
        "#b5f690",
        "#aff696",
        "#a9f69c",
        "#a3f6a3",
        "#9cf6a9",
        "#96f6af",
        "#90f6b5",
        "#8af6bb",
        "#84f6c2",
        "#7df6c8",
        "#77f6ce",
        "#71f6d4",
        "#6bf6da",
        "#65f6e0",
        "#5ef6e7",
        "#58f0ed",
        "#52e8f3",
        "#4cdbf9",
        "#7bccf6",
        "#82c4f6",
        "#88bdf6",
        "#8eb7f6",
        "#94b1f6",
        "#9aabf6",
        "#a1a5f6",
        "#a79ef6",
        "#ad98f6",
        "#b392f6",
        "#b98cf6",
        "#bf86f6",
        "#c67ff6",
        "#cc79f6",
        "#d273f6",
        "#d86df6",
        "#de67f6",
        "#e561f6",
        "#e967ec",
        "#ed6de2",
        "#f173d7",
    ]
    dismphCM = LinearSegmentedColormap.from_list("dismph", colors)
    dismphCM.set_bad("w", 0.0)
    return dismphCM
Example #40
0
def show_cut_image(shap_values, img_orig, num_features=1000):
    # segment the image so we don't have to explain every pixel
    segments_slic = slic(img_orig, n_segments=49, compactness=100, sigma=3)

    # define a function that depends on a binary mask representing if an image region is hidden
    def mask_image(zs, segmentation, image, background=None):
        if background is None:
            background = image.mean((0, 1))
        out = np.zeros(
            (zs.shape[0], image.shape[0], image.shape[1], image.shape[2]))
        for i in range(zs.shape[0]):
            out[i, :, :, :] = image
            for j in range(zs.shape[1]):
                if zs[i, j] == 0:
                    out[i][segmentation == j, :] = background
        return out

    def f(z):
        print(model.predict(mask_image(z, segments_slic, img_orig, 250)))
        return model.predict((mask_image(z, segments_slic, img_orig, 250)))

    # # use Kernel SHAP to explain the network's predictions


#     explainer = shap.KernelExplainer(f, np.zeros((1,50)))
#     shap_values = explainer.shap_values(np.ones((1,50)), nsamples=5000) # runs model 1000 times

    shap_values = extract_top_ten(shap_values, num_features)

    # get the top predictions from the model
    preds = model.predict((np.expand_dims(img_orig.copy(), axis=0)))
    top_preds = np.argsort(-preds)

    # make a color map
    from matplotlib.colors import LinearSegmentedColormap
    colors = []
    for l in np.linspace(1, 0, 100):
        colors.append((0.2, 0.2, 0.2, l))
    for l in np.linspace(0, 1, 100):
        colors.append((0.5, 0.5, 0.5, l))
    cm = LinearSegmentedColormap.from_list("shap", colors)

    def fill_segmentation(values, segmentation):
        out = np.zeros(segmentation.shape)
        for i in range(len(values)):
            out[segmentation == i] = 0 if values[i] > 0 else 1
        return out

    # plot our explanations
    fig, axes = plt.subplots(nrows=1, ncols=6, figsize=(30, 20))
    inds = top_preds[0]
    axes[0].imshow(img_orig)
    axes[0].axis('off')
    max_val = np.max([
        np.max(np.abs(shap_values[i][:, :-1])) for i in range(len(shap_values))
    ])
    for i in range(5):
        m = fill_segmentation(shap_values[inds[i]][0], segments_slic)
        axes[i + 1].set_title(
            str(list(test_image_gen.class_indices.keys())[inds[i]]))
        axes[i + 1].imshow(img_orig)
        im = axes[i + 1].imshow(m, cmap=cm, vmin=-max_val, vmax=max_val)
        plt.savefig('foo.png')
        axes[i + 1].axis('off')
    cb = fig.colorbar(im,
                      ax=axes.ravel().tolist(),
                      label="SHAP value",
                      orientation="horizontal",
                      aspect=60)
    cb.outline.set_visible(False)
Example #41
0
model.predict(np.expand_dims(img_orig.copy(), axis=0))

# In[ ]:

plt.imshow(img_orig.copy())

# In[ ]:

# make a color map
from matplotlib.colors import LinearSegmentedColormap
colors = []
for l in np.linspace(1, 0, 100):
    colors.append((245 / 255, 39 / 255, 87 / 255, l))
for l in np.linspace(0, 1, 100):
    colors.append((24 / 255, 196 / 255, 93 / 255, l))
cm = LinearSegmentedColormap.from_list("shap", colors)

# In[ ]:


def fill_segmentation(values, segmentation):
    out = np.zeros(segmentation.shape)
    for i in range(len(values)):
        out[segmentation == i] = values[i]
    return out


# plot our explanations
fig, axes = plt.subplots(nrows=1, ncols=6, figsize=(30, 20))
inds = top_preds[0]
axes[0].imshow(img)
Example #42
0
signif = (splusb - b) / (np.sqrt(b) + 1)
imax = np.unravel_index(np.argmax(signif), signif.shape)
cuts = [
    ax.edges(overflow='all')[::d][i] for ax, i, d in zip(axes, imax, intdir)
]
print("Best significance: %.4f at BvL %.2f CvL %.2f CvB %.2f" %
      ((signif[imax], ) + tuple(cuts)))
cut = (xyz[0] < cuts[0]) & (xyz[1] >= cuts[1]) & (xyz[2] >= cuts[2])

figures = []
pdfs = {}
for proc in processes:
    array = htagtensor.values()[(proc, )]
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    cmap_blue = LinearSegmentedColormap.from_list(name="transparentblue",
                                                  colors=["#0000ff00", "b"])
    cmap_red = LinearSegmentedColormap.from_list(name="transparentred",
                                                 colors=["#ff000000", "r"])

    density = array.flatten()
    density /= density.max()  # transparency according to full scale
    pdfs[proc] = density / density.sum()
    colors = cmap_blue(density)
    colors[cut] = cmap_red(density[cut])
    print("Efficiency for %s: %.3f" %
          (proc, density[cut].sum() / density.sum()))

    # rendering all 40^3 points makes it a bit slow, cut the mostly transparent ones
    rendercut = (density > density.max() / 150.)
    # print("Reduced to %.1f%%" % (100.*rendercut.sum()/rendercut.size))
    ax.scatter(xyz[0][rendercut],
Example #43
0
def main(argv):
    """     ********* Main program *********     """
    nmeltatoms = 100  #limit of natoms to consider a species being part of the melt
    #other dictionnaries and parameters for the figure for article version
    letter = ''
    statfile2 = ''
    plot_parameters = {
        "size_fonts": 12,
        "size_font_ticks": 10,
        "size_figure": (8, 4),
        "size_markers": 4,
        "size_lines": 1,
        "shift_labelpad": 20
    }
    #plot_parameters = {"size_fonts" : 12,"size_font_ticks":10,"size_figure" : (8,4),"size_markers" : 10,"size_lines" : 2,"shift_labelpad" : 20}
    #other parameters
    Na = 6.022 * 10**23
    try:
        options, arg = getopt.getopt(argv, "hf:g:v:m:d:l:", [
            "file1", "gfile2", "variable", "mineralfile", "density_max",
            "letter"
        ])
    except getopt.GetoptError:
        print(
            "plot_speciation-r1-species.py  -v <variable (rho,T)> -m <mineralfile with elements> -f <stat-concentrate_r1_abso_filename>  -g <stat-concentrate_r1_abso_filename2>  -d <maximum density to plot in g/cm3> -l <letter for article subplot, default = ''>"
        )
        sys.exit()
    for opt, arg in options:
        if opt == '-h':
            print('')
            print(
                'plot_speciation-r1-species.py program to  Plot abundance of chemical species for selected cation as a function of rho or T'
            )
            print(
                "plot_speciation-r1-species.py -v <variable (rho,T)> -m <mineralfile with elements> -f <stat-concentrate_r1_abso_filename>  -g <stat-concentrate_r1_abso_filename2>  -d <maximum density to plot in g/cm3> -l <letter for article subplot, default = ''>"
            )
            print(
                'requires the file containing elements and number (in order to compute the densities)'
            )
            print('')
            sys.exit()
        elif opt in ("-f", "--file1"):
            statfile1 = str(arg)
        elif opt in ("-g", "--gfile2"):
            statfile2 = str(arg)
        elif opt in ("-v", "--variable"):
            variable = str(arg)
        elif opt in ("-m", "--mineralfile"):
            mineralfile = str(arg)
        elif opt in ("-d", "--density_max"):
            max_den = float(arg)
        elif opt in ("-l", "--letter"):
            letter = str(arg)
    #***** Calculation of the molecular mass
    with open(mineralfile, 'r') as mf:
        entry = mf.readline()
        elements = entry.split()[1:]
        entry = mf.readline()
        number = entry.split()[1:]
    MN = 0
    for i in range(len(elements)):
        MN = MN + float(number[i]) * cr.Elements2rest(elements[i])[3]
    #***** Creation of the plot
    if statfile2 != '':
        allstatfiles = [statfile1, statfile2]
        with open(statfile1, 'r') as f:
            line = f.readline()
            file1 = line.split()[1]
        with open(statfile2, 'r') as f2:
            line = f2.readline()
            file2 = line.split()[1]
        fig, ax1, ax2 = creation_plot2(variable, file1, file2, MN,
                                       plot_parameters, max_den, letter)
        figurename = statfile1.split('/')[-1].split(
            '.dat')[0] + '+' + statfile2.split('/')[-1].split(
                '.dat')[0] + '_' + variable + '-species'
    else:
        allstatfiles = [statfile1]
        fig, ax1 = creation_plot(variable, plot_parameters, max_den, letter)
        figurename = statfile1.split('.dat')[0] + '_' + variable + '-species'

    for ii in range(len(allstatfiles)):
        statfile = allstatfiles[ii]
        #selection of the plot
        if ii == 1:
            ax = ax2
        else:
            ax = ax1

        #initialisation
        xdata = {'T': [], 'rho': []}  #dictionnary containing the x data
        data = []  #list containing y data
        melt = []  #list containing y data for species with 100 atoms or more
        selected_files = [
        ]  #list containing the files we use for the plot (depends on the density limit)
        species1 = []
        list_colored_species = []
        list_colored_red = []
        list_colored_blue = []
        list_colored_purple = []
        list_colored_green = []
        list_grey_species = []
        list_black_species = ["melt-like"]
        colors_species = {}

        #***** Count of the number of clusters for the selected atom type (needed for the automatic color change)
        #first store every cluster type into the dictionary
        if re.search("L208", statfile):
            #print("we group all the species of more than nmeltatoms atoms under the denomination 'melt-like'")
            with open(statfile, 'r') as f:
                line = f.readline()
                while True:
                    line = f.readline()
                    if not line: break
                    else:
                        entry = line.split('\n')[0].split('\t')
                        if len(entry) > 1:
                            natoms = count_natom(entry[0])
                            if natoms < nmeltatoms:
                                species1.append(entry[0])
                                colors_species[entry[0]] = []
                species1.append("melt-like")
                colors_species["melt-like"] = []
        else:
            #print("we use the standard script")
            with open(statfile, 'r') as f:
                line = f.readline()
                while True:
                    line = f.readline()
                    if not line: break
                    else:
                        entry = line.split('\n')[0].split('\t')
                        if len(entry) > 1:
                            species1.append(entry[0])
                            colors_species[entry[0]] = []
        #*****************************
        #*******************
        #*******
        #Read and compute percentages
        #in case of speciation r1 L208, we group all the species of more than 100 atoms under the denomination "melt-like"
        with open(statfile, 'r') as f:
            line = f.readline()
            entry = line.split()
            files = entry[1:]
            #creation of the x data list
            for file in files:
                temperature, acell = split_name(file)
                density = MN / (Na * float(acell)**3 * 10**(-24))
                if density <= max_den:
                    xdata['rho'].append(density)  #calculation density
                    xdata['T'].append(int(temperature))
                    selected_files.append(file)
            if re.search("L208", statfile):
                print("we group all the species of more than", nmeltatoms,
                      "atoms under the denomination 'melt-like'")
                #creation of the y data matrix
                while True:
                    line = f.readline()
                    if not line: break
                    else:
                        entry = line.split('\n')[0].split('\t')[:-1]
                        linedata = []  #we initialize the list of data per line
                        if len(entry) > 1:
                            for i in range(1, len(entry)):
                                if files[
                                        i -
                                        1] in selected_files:  #if the column which corresponding file is in the selected files, then we use the data
                                    if entry[i] == '':
                                        entry[i] = 0
                                    else:
                                        entry[i] = float(entry[i])
                                    linedata.append(
                                        entry[i]
                                    )  #we create a list of the data for this line (cluster)
                            natoms = count_natom(entry[0])
                            if natoms < nmeltatoms:
                                data.append(
                                    linedata[:]
                                )  #we add this list to the big list of data in order to have nested lists
                            else:
                                melt.append(linedata[:])
                print('for files', selected_files)
                print('all species are', species1, len(species1))
                print('and density', xdata['rho'])
                #calculation of the percentages per density
                totalsmelt = np.ndarray.tolist(np.sum(
                    melt, 0))  #we sum all the melt species together
                data.append(
                    totalsmelt[:])  #and we add this total to the data matrix
                print('initial data of len', np.size(data, 0),
                      np.size(data, 1))
                totals = np.sum(data, 0)
                #print('totals', totals)
                for i in range(np.size(data, 0)):
                    for j in range(np.size(data, 1)):
                        if totals[j] == 0:
                            data[i][j] = 0
                        else:
                            data[i][j] = data[i][j] / totals[j]
                #print('calculated percentages',data)
                newtotals = np.sum(data, 0)
                print('total should be 1', newtotals)
            else:
                print("we use the standard script")
                #creation of the y data matrix
                while True:
                    line = f.readline()
                    if not line: break
                    else:
                        entry = line.split('\n')[0].split('\t')[:-1]
                        linedata = []  #we initialize the list of data per line
                        if len(entry) > 1:
                            for i in range(1, len(entry)):
                                if files[
                                        i -
                                        1] in selected_files:  #if the column which corresponding file is in the selected files, then we use the data
                                    if entry[i] == '':
                                        entry[i] = 0
                                    else:
                                        entry[i] = float(entry[i])
                                    linedata.append(
                                        entry[i]
                                    )  #we create a list of the data for this line (cluster)
                            data.append(
                                linedata[:]
                            )  #we add this list to the big list of data in order to have nested lists
                print('for files', selected_files)
                print('all species are', species1)
                #print('initial data',data)
                #print('and density', xdata['rho'])
                #calculation of the percentages per density
                totals = np.sum(data, 0)
                #print('totals', totals)
                for i in range(np.size(data, 0)):
                    for j in range(np.size(data, 1)):
                        if totals[j] == 0:
                            data[i][j] = 0
                        else:
                            data[i][j] = data[i][j] / totals[j]
                #print('calculated percentages',data)
                newtotals = np.sum(data, 0)
                print('total should be 1', newtotals)
        #*****************************
        #*******************
        #*******
    #    #now the dictionary has every possible cluster, we attribute the colors to each cluster
    #    list_species = []
    #    color = iter(plt.cm.jet(np.linspace(0,1,len(colors_species)))) #Creation of the color list
    #    for key in natsort.natsorted(colors_species):
    #        c = next(color)
    #        colors_species[key] = c
    #        list_species.append(key)
    #    print(list_species, len(list_species))
    #*** check the abundance to put in grey the species less abundant than 1% and put a label only for species more abundante than 5% at least one time
        for i in range(np.size(data, axis=0)):  #loop on each cluster
            if max(data[i]) < 0.01:
                colors_species[species1[i]] = '0.5'
                list_grey_species.append(species1[i])
            else:
                #this is for basic automatic color change
                list_colored_species.append(species1[i])
                #this is for custom color change relative to the feldspars
                if (species1[i][0] == 'N' or species1[i][0] == 'K'
                        or species1[i][0] == 'C') and re.search(
                            'Si_1O', species1[i]):
                    list_colored_blue.append(species1[i])
                elif re.match('Si_1O', species1[i]):
                    list_colored_red.append(species1[i])
                elif (species1[i][0] == 'N' or species1[i][0] == 'K' or
                      species1[i][0] == 'C') and re.search('O', species1[i]):
                    list_colored_purple.append(species1[i])
                elif species1[i][0:5] == 'Al_1O':
                    list_colored_green.append(species1[i])
                #We add the label
                if max(data[i]) > 0.05:
                    x_max = xdata[variable][data[i].index(max(data[i]))]
                    y_max = max(data[i])
                    label = species1[i]
                    #formatage of label
                    label = format1label(label)
                    ax.text(x_max, y_max, label, color='0.35')
        print('list red', list_colored_red)
        print('list blue', list_colored_blue)
        print('list purple', list_colored_purple)
        print('list green', list_colored_green)
        #**** we attribute the colors to each cluster with abundance > 0.01
        #this is for basic automatic color change
        #color = iter(plt.cm.jet(np.linspace(0,1,len(list_colored_species)))) #Creation of the color list
        #for key in natsort.natsorted(list_colored_species):
        #    c = next(color)
        #    colors_species[key] = c
        #and this is for custom color change relative to the feldspars
        dict_r = {
            'red': (
                (
                    0.0, 1.0, 1.0
                ),  # <- at 0.0 (first value), the red component is 1 (second value) (the third value may be alpha)
                (1.0, 0.3, 1.0)),  # <- at 1.0, the red component is 0.3
            'green': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)),
            'blue': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0))
        }
        cmreds = LinearSegmentedColormap('reds', dict_r)
        color_r = iter(cmreds(np.linspace(
            0, 1,
            len(list_colored_red))))  #Creation of the color list for reds
        dict_b = {
            'red': ((0.0, 0.0, 0.0), (1.0, 0.4, 1.0)),
            'green': ((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)),
            'blue': (
                (
                    0.0, 1.0, 1.0
                ),  # <- at 0.0 (first value), the blue component is 1 (second value) (the third value may be alpha)
                (1.0, 1.0, 1.0))  # <- at 1.0, the blue component is 0.3
        }
        cmblues = LinearSegmentedColormap('blues', dict_b)
        color_b = iter(cmblues(np.linspace(
            0, 1,
            len(list_colored_blue))))  #Creation of the color list for blues
        dict_p = {
            'red': (
                (
                    0.0, 0.6, 1.0
                ),  # <- at 0.0 (first value), the red component is 0.6 (second value) (the third value may be alpha)
                (1.0, 0.3, 1.0)),  # <- at 1.0, the red component is 0.3
            'green': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)),
            'blue': (
                (
                    0.0, 0.6, 1.0
                ),  # <- at 0.0 (first value), the blue component is 0.6 (second value) (the third value may be alpha)
                (1.0, 0.3, 1.0))  # <- at 1.0, the blue component is 0.3
        }
        cmpurples = LinearSegmentedColormap('purples', dict_p)
        color_p = iter(cmpurples(np.linspace(0, 1, len(
            list_colored_purple))))  #Creation of the color list for purples
        dict_g = {
            'red': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)),
            'green': (
                (
                    0.0, 1.0, 1.0
                ),  # <- at 0.0 (first value), the green component is 0.7 (second value) (the third value may be alpha)
                (1.0, 0.3, 1.0)),  # <- at 1.0, the green component is 0.3
            'blue': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0))
        }
        cmgreens = LinearSegmentedColormap('greens', dict_g)
        color_g = iter(cmgreens(np.linspace(
            0, 1,
            len(list_colored_green))))  #Creation of the color list for greens
        for key in natsort.natsorted(list_colored_species):
            if key == 'O_1':
                colors_species[key] = 'gold'
            elif key == 'O_2':
                colors_species[key] = 'darkorange'
            elif key == 'O_3':
                colors_species[key] = 'tomato'
            elif key == 'Na_1' or key == 'K_1' or key == 'Ca_1':
                colors_species[key] = (0.5, 0, 1.0)
            elif key == "melt-like":
                colors_species[key] = '0'
            else:
                colors_species[key] = '0.5'
        colors_species = color_change(color_r, list_colored_red,
                                      colors_species)
        colors_species = color_change(color_b, list_colored_blue,
                                      colors_species)
        colors_species = color_change(color_p, list_colored_purple,
                                      colors_species)
        colors_species = color_change(color_g, list_colored_green,
                                      colors_species)
        #*****************************
        #*******************
        #*******
        #***plot for each cluster and write file with percentages
        newfilename = statfile.split(
            '.dat')[0] + '_' + variable + '-species' + '.txt'
        nf = open(newfilename, 'w')
        if variable == 'rho':
            nf.write('Density(g/cm3)\t' + '\t'.join(
                str(round(density, 2)) for density in xdata[variable]) + '\n')
        else:
            nf.write('Temperature(K)\t' +
                     '\t'.join(str(temp) for temp in xdata[variable]) + '\n')
        #plot stacked area
        #    plt.stackplot(xdata[variable],data)
        #plot lines
        if re.search("L208", statfile):
            #print("we group all the species of more than nmeltatoms atoms under the denomination 'melt-like'")
            i = -1
            with open(statfile, 'r') as f:
                line = f.readline()
                while True:
                    line = f.readline()
                    if not line: break
                    else:
                        entry = line.split('\n')[0].split('\t')[:-1]
                        if len(entry) > 1:
                            natoms = count_natom(entry[0])
                            if natoms < nmeltatoms:
                                i += 1
                                x, y = zip(
                                    *sorted(zip(xdata[variable], data[i])))
                                if entry[
                                        0] in list_colored_species:  #we plot only the colored lines. delete this line to plot also the grey species
                                    line, = ax.plot(
                                        x,
                                        y,
                                        '.--',
                                        color=colors_species[entry[0]],
                                        markersize=plot_parameters[
                                            "size_markers"],
                                        linewidth=plot_parameters["size_lines"]
                                    )
                                nf.write(
                                    format1label(entry[0]) + '\t' + '\t'.join(
                                        str(round(perc, 4))
                                        for perc in data[i]) + '\n')
                #we plot the last line, for the melt-like
                i += 1
                x, y = zip(*sorted(zip(xdata[variable], data[i])))
                line, = ax.plot(x,
                                y,
                                '.--',
                                color=colors_species['melt-like'],
                                markersize=plot_parameters["size_markers"],
                                linewidth=plot_parameters["size_lines"])
                nf.write(
                    format1label('melt-like') + '\t' +
                    '\t'.join(str(round(perc, 4)) for perc in data[i]) + '\n')
        else:
            i = -1
            with open(statfile, 'r') as f:
                line = f.readline()
                while True:
                    line = f.readline()
                    if not line: break
                    else:
                        entry = line.split('\n')[0].split('\t')[:-1]
                        if len(entry) > 1:
                            i += 1
                            x, y = zip(*sorted(zip(xdata[variable], data[i])))
                            if entry[
                                    0] in list_colored_species:  #we plot only the colored lines. delete this line to plot also the grey species
                                line, = ax.plot(
                                    x,
                                    y,
                                    '.--',
                                    color=colors_species[entry[0]],
                                    markersize=plot_parameters["size_markers"],
                                    linewidth=plot_parameters["size_lines"])
                            nf.write(
                                format1label(entry[0]) + '\t' + '\t'.join(
                                    str(round(perc, 4))
                                    for perc in data[i]) + '\n')
        #                    if variable == 'T':
        #                        label_line(ax, line, entry[0], halign='right')
        #                    elif list_species.index(entry[0]) < int(len(list_species)/2):
        #                        label_line(ax, line, entry[0], halign='left')
        #                    else:
        #                        label_line(ax, line, entry[0], halign='right')
    #    ax.hlines(y=0.05, xmin = 1, xmax=2.1, color = 'r')
    #    ax.hlines(y=0.01, xmin = 1, xmax=2.1, color = 'm')
    #*****************************
    #*******************
    #*******
    #legend
    #custom_lines_grey = [Line2D([0],[0],color = colors_species[key], ls = '--', marker = '.', markersize = plot_parameters["size_markers"], linewidth = plot_parameters["size_lines"]) for key in natsort.natsorted(list_grey_species)]
        list_grey_species = format_label(natsort.natsorted(list_grey_species))
        #legend_grey = plt.legend([line for line in custom_lines_grey],[label for label in list_grey_species],title = '$\\bf{Clusters <1\%}$', bbox_to_anchor=(1.25, 1), loc='upper left', fontsize = plot_parameters["size_fonts"],  borderaxespad=0.)
        #plt.gca().add_artist(legend_grey)
        #plt.setp(legend_grey.get_title(),fontsize= plot_parameters["size_fonts"])

        custom_lines = [
            Line2D([0], [0],
                   color=colors_species[key],
                   ls='--',
                   marker='.',
                   markersize=plot_parameters["size_markers"],
                   linewidth=plot_parameters["size_lines"])
            for key in natsort.natsorted(list_colored_species)
        ]
        list_colored_species = format_label(
            natsort.natsorted(list_colored_species))
        if statfile2 != '':
            legend = ax.legend([line for line in custom_lines],
                               [label for label in list_colored_species],
                               bbox_to_anchor=(0.5, 0.99),
                               loc='upper center',
                               fontsize=plot_parameters["size_font_ticks"],
                               borderaxespad=0.,
                               ncol=3)
        #if len(list_colored_species) > 16:
    #        legend = plt.legend([line for line in custom_lines],[label for label in list_colored_species],title = '$\\bf{Species}$', bbox_to_anchor=(1.01, 1), loc='upper left', fontsize = plot_parameters["size_fonts"],  borderaxespad=0., ncol = 2)
    #   legend = plt.legend([line for line in custom_lines],[label for label in list_colored_species],title = '$\\bf{Species}$', bbox_to_anchor=(0.5, 0.99), loc='upper center', fontsize = plot_parameters["size_fonts"],  borderaxespad=0.,ncol = 8)
        else:
            #        legend = plt.legend([line for line in custom_lines],[label for label in list_colored_species],title = '$\\bf{Species}$', bbox_to_anchor=(1.01, 1), loc='upper left', fontsize = plot_parameters["size_fonts"],  borderaxespad=0.)
            legend = ax.legend([line for line in custom_lines],
                               [label for label in list_colored_species],
                               title='$\\bf{Species}$',
                               bbox_to_anchor=(0.5, 0.99),
                               loc='upper center',
                               fontsize=plot_parameters["size_fonts"],
                               borderaxespad=0.,
                               ncol=8)
            plt.setp(legend.get_title(),
                     fontsize=plot_parameters["size_fonts"])

        nf.write('\n')
        nf.write('Species<1%\t' +
                 '\t'.join(str(species)
                           for species in list_grey_species) + '\n')
        print(newfilename, 'is created')

    figurename = figurename + '.pdf'
    plt.savefig(figurename, bbox_inches='tight', dpi=300)
    print(figurename, 'is created')
Example #44
0
cluster_img[(ent_dat >= 0) & (ent_dat <= 0.3) & (theta_dat >= -10) &
            (theta_dat < 0)] = 6
cluster_img[(ent_dat >= 0) & (ent_dat <= 0.3) & (theta_dat >= 0) &
            (theta_dat < 20)] = 9
cluster_img[(ent_dat >= 0) & (ent_dat <= 0.3) & (theta_dat >= 20) &
            (theta_dat <= 90)] = 12

cluster_img = cluster_img.astype(np.float32)
cluster_img[np.isnan(theta_dat) == True] = np.nan

mymap = [(1, 0.69, 0.69), (0.97, 0.24, 0.24), (0.77, 0.19, 0.19),
         (0.51, 1, 0.84), (0.01, 1, 0.67), (0.50, 0.99, 0.51),
         (0.45, 0.89, 0.46), (0.35, 0.69, 0.58), (0.11, 0.44, 0.23),
         (0.51, 0.58, 1), (0.2, 0.32, 0.97), (0, 0.15, 0.96)]

mymap = LinearSegmentedColormap.from_list("mymap", mymap, N=12)

num_list = []

low_val = 1
high_val = 12

step = (high_val - low_val) / 24  # total 12 zones
cur_val = low_val + step
num_list.append(cur_val)

for i in np.arange(low_val + 1, high_val + 1):
    step1 = 2 * step
    cur_val = cur_val + step1
    num_list.append(cur_val)
Example #45
0
def render_hunting_policy(title, policy):
    """render_hunting_policy

    This was pure hell to make. Renders a visual representation of a hunting policy.

    Parameters
    ----------

    title : Title of figure
    policy :

    Returns
    -------
    """
    env = create_small_hunting_environment()
    p_stats, v_stats = get_solver_stats_by_animal(env, policy)

    fig, axs = plt.subplots(1, 5, sharey=True, figsize=(8, 10))

    cmap = LinearSegmentedColormap.from_list("Custom", ("goldenrod", "purple"),
                                             2)

    buffalo = p_stats["buffalo"]
    ostrich = p_stats["ostrich"]
    lemur = p_stats["lemur"]
    rabbit = p_stats["rabbit"]
    bird = p_stats["bird"]

    cmap = colors.ListedColormap(["goldenrod", "red", "purple"])
    bounds = [0, 1, 2]
    norm = colors.BoundaryNorm(bounds, cmap.N)

    for ax, intmap, name in zip(
            axs,
        [buffalo, ostrich, lemur, rabbit, bird],
        ["buffalo", "ostrich", "lemur", "rabbit", "bird"],
    ):
        values = None
        if values == None:
            ax.imshow(intmap, cmap=cmap, norm=norm)
        else:
            arr_values = np.array(values).reshape((size, size))
            ax.imshow(arr_values, cmap="Blues")

        # draw gridlines
        rows = 50
        cols = 5
        ax.grid(which="major",
                axis="both",
                linestyle="-",
                color="k",
                linewidth=2)
        ax.set_xticks(np.arange(-0.5, cols, 1))
        ax.set_yticks(np.arange(-0.5, rows, 1))

        ax.set_xticklabels(np.arange(0, cols + 1, 1))
        ax.set_yticklabels(np.arange(0, rows + 1, 1))

        ax.set_xlabel("Injury")

        ax.xaxis.set_tick_params(size=0)
        ax.yaxis.set_tick_params(size=0)

        for label in ax.xaxis.get_majorticklabels():
            label.set_horizontalalignment("left")

        for label in ax.yaxis.get_majorticklabels():
            label.set_verticalalignment("top")

        ax.set_title(name)

    axs[0].set_ylabel('Energy')
    legend_elements = [
        Patch(facecolor="goldenrod", edgecolor="goldenrod", label="HUNT"),
        Patch(facecolor="purple", edgecolor="purple", label="WAIT"),
    ]
    axs[2].legend(handles=legend_elements,
                  loc="center left",
                  bbox_to_anchor=(-0.06, 1.08))
    plt.suptitle(title)

    outpath = os.path.join(output_dir, title + ".png")
    plt.savefig(outpath)
    plt.close()
Example #46
0
def create_range_aprs(time):
    client = MongoClient(os.environ["MONGODB_CLIENT"])
    lat = 29.780880
    lon = -95.420410

    start, now = helpers.get_time_range(time)
    db = client.aprs
    df = pd.DataFrame(
        list(
            db.raw.find({
                "script": "entry",
                "latitude": {
                    "$exists": True,
                    "$ne": None
                },
                "timestamp_": {
                    "$gt": start,
                    "$lte": now
                },
            }).sort([("timestamp_", -1)])))
    df["dist"] = helpers.haversine_np(lon, lat, df["longitude"],
                                      df["latitude"])
    df["month"] = df["timestamp_"].apply(
        lambda row: str(row.year) + "-" + str(row.month).zfill(2))
    df["dist_"] = np.round(df["dist"] * 1) / 1
    df = df[df["dist"] <= 250]

    c5 = np.array([245 / 256, 200 / 256, 66 / 256, 1])
    c4 = np.array([245 / 256, 218 / 256, 66 / 256, 1])
    c3 = np.array([188 / 256, 245 / 256, 66 / 256, 1])
    c2 = np.array([108 / 256, 201 / 256, 46 / 256, 1])
    c1 = np.array([82 / 256, 138 / 256, 45 / 256, 1])
    c0 = np.array([24 / 256, 110 / 256, 45 / 256, 1])
    total = len(df["month"].unique())
    cm = LinearSegmentedColormap.from_list("custom", [c0, c1, c2, c3, c4, c5],
                                           N=total)

    data = []
    for idx, month in enumerate(df["month"].unique()):
        color = rgb2hex(cm(idx / total))
        df2 = df[df["month"] == month]
        df2 = df2.groupby(by="dist_").count()
        data.append(
            go.Scatter(
                x=df2.index,
                y=df2["_id"],
                name=month,
                line=dict(color=color, width=3, shape="spline", smoothing=0.3),
                mode="lines",
            ), )

    layout = go.Layout(
        autosize=True,
        hoverlabel=dict(font=dict(family="Roboto Mono")),
        yaxis=dict(
            domain=[0.02, 0.98],
            type="log",
            title="Frequency",
            fixedrange=False,
        ),
        xaxis=dict(
            type="log",
            title="Distance (mi)",
            fixedrange=False,
        ),
        margin=dict(r=50, t=30, b=30, l=60, pad=0),
        #     showlegend=False,
    )

    graphJSON = json.dumps(dict(data=data, layout=layout),
                           cls=plotly.utils.PlotlyJSONEncoder)
    client.close()
    return graphJSON
Example #47
0
def hist2d(x, y, bins=20, range=None, weights=None, levels=None, smooth=None,
           ax=None, color=None, plot_datapoints=True, plot_density=True,
           plot_contours=True, no_fill_contours=False, fill_contours=False,
           contour_kwargs=None, contourf_kwargs=None, data_kwargs=None,
           **kwargs):
    """
    Plot a 2-D histogram of samples.

    Parameters
    ----------
    x : array_like[nsamples,]
       The samples.

    y : array_like[nsamples,]
       The samples.

    levels : array_like
        The contour levels to draw.

    ax : matplotlib.Axes
        A axes instance on which to add the 2-D histogram.

    plot_datapoints : bool
        Draw the individual data points.

    plot_density : bool
        Draw the density colormap.

    plot_contours : bool
        Draw the contours.

    no_fill_contours : bool
        Add no filling at all to the contours (unlike setting
        ``fill_contours=False``, which still adds a white fill at the densest
        points).

    fill_contours : bool
        Fill the contours.

    contour_kwargs : dict
        Any additional keyword arguments to pass to the `contour` method.

    contourf_kwargs : dict
        Any additional keyword arguments to pass to the `contourf` method.

    data_kwargs : dict
        Any additional keyword arguments to pass to the `plot` method when
        adding the individual data points.

    """
    if ax is None:
        ax = pl.gca()

    # Set the default range based on the data range if not provided.
    if range is None:
        if "extent" in kwargs:
            logging.warn("Deprecated keyword argument 'extent'. "
                         "Use 'range' instead.")
            range = kwargs["extent"]
        else:
            range = [[x.min(), x.max()], [y.min(), y.max()]]

    # Set up the default plotting arguments.
    if color is None:
        color = "k"

    # Choose the default "sigma" contour levels.
    if levels is None:
        levels = 1.0 - np.exp(-0.5 * np.arange(0.5, 2.1, 0.5) ** 2)

    # This is the color map for the density plot, over-plotted to indicate the
    # density of the points near the center.
    density_cmap = LinearSegmentedColormap.from_list(
        "density_cmap", [color, (1, 1, 1, 0)])

    # This color map is used to hide the points at the high density areas.
    white_cmap = LinearSegmentedColormap.from_list(
        "white_cmap", [(1, 1, 1), (1, 1, 1)], N=2)

    # This "color map" is the list of colors for the contour levels if the
    # contours are filled.
    rgba_color = colorConverter.to_rgba(color)
    contour_cmap = [list(rgba_color) for l in levels] + [rgba_color]
    for i, l in enumerate(levels):
        contour_cmap[i][-1] *= float(i) / (len(levels)+1)

    # We'll make the 2D histogram to directly estimate the density.
    try:
        H, X, Y = np.histogram2d(x.flatten(), y.flatten(), bins=bins,
                                 range=list(map(np.sort, range)),
                                 weights=weights)
    except ValueError:
        raise ValueError("It looks like at least one of your sample columns "
                         "have no dynamic range. You could try using the "
                         "'range' argument.")

    if smooth is not None:
        if gaussian_filter is None:
            raise ImportError("Please install scipy for smoothing")
        H = gaussian_filter(H, smooth)

    # Compute the density levels.
    Hflat = H.flatten()
    inds = np.argsort(Hflat)[::-1]
    Hflat = Hflat[inds]
    sm = np.cumsum(Hflat)
    sm /= sm[-1]
    V = np.empty(len(levels))
    for i, v0 in enumerate(levels):
        try:
            V[i] = Hflat[sm <= v0][-1]
        except:
            V[i] = Hflat[0]
    V.sort()
    m = np.diff(V) == 0
    if np.any(m):
        logging.warning("Too few points to create valid contours")
    while np.any(m):
        V[np.where(m)[0][0]] *= 1.0 - 1e-4
        m = np.diff(V) == 0
    V.sort()

    # Compute the bin centers.
    X1, Y1 = 0.5 * (X[1:] + X[:-1]), 0.5 * (Y[1:] + Y[:-1])

    # Extend the array for the sake of the contours at the plot edges.
    H2 = H.min() + np.zeros((H.shape[0] + 4, H.shape[1] + 4))
    H2[2:-2, 2:-2] = H
    H2[2:-2, 1] = H[:, 0]
    H2[2:-2, -2] = H[:, -1]
    H2[1, 2:-2] = H[0]
    H2[-2, 2:-2] = H[-1]
    H2[1, 1] = H[0, 0]
    H2[1, -2] = H[0, -1]
    H2[-2, 1] = H[-1, 0]
    H2[-2, -2] = H[-1, -1]
    X2 = np.concatenate([
        X1[0] + np.array([-2, -1]) * np.diff(X1[:2]),
        X1,
        X1[-1] + np.array([1, 2]) * np.diff(X1[-2:]),
    ])
    Y2 = np.concatenate([
        Y1[0] + np.array([-2, -1]) * np.diff(Y1[:2]),
        Y1,
        Y1[-1] + np.array([1, 2]) * np.diff(Y1[-2:]),
    ])

    if plot_datapoints:
        if data_kwargs is None:
            data_kwargs = dict()
        data_kwargs["color"] = data_kwargs.get("color", color)
        data_kwargs["ms"] = data_kwargs.get("ms", 2.0)
        data_kwargs["mec"] = data_kwargs.get("mec", "none")
        data_kwargs["alpha"] = data_kwargs.get("alpha", 0.1)
        ax.plot(x, y, "o", zorder=-1, rasterized=True, **data_kwargs)

    # Plot the base fill to hide the densest data points.
    if (plot_contours or plot_density) and not no_fill_contours:
        ax.contourf(X2, Y2, H2.T, [V.min(), H.max()],
                    cmap=white_cmap, antialiased=False)

    if plot_contours and fill_contours:
        if contourf_kwargs is None:
            contourf_kwargs = dict()
        contourf_kwargs["colors"] = contourf_kwargs.get("colors", contour_cmap)
        contourf_kwargs["antialiased"] = contourf_kwargs.get("antialiased",
                                                             False)
        ax.contourf(X2, Y2, H2.T, np.concatenate([[0], V, [H.max()*(1+1e-4)]]),
                    **contourf_kwargs)

    # Plot the density map. This can't be plotted at the same time as the
    # contour fills.
    elif plot_density:
        ax.pcolor(X, Y, H.max() - H.T, cmap=density_cmap)

    # Plot the contour edge colors.
    if plot_contours:
        if contour_kwargs is None:
            contour_kwargs = dict()
        contour_kwargs["colors"] = contour_kwargs.get("colors", color)
        ax.contour(X2, Y2, H2.T, V, **contour_kwargs)

    ax.set_xlim(range[0])
    ax.set_ylim(range[1])
Example #48
0
        #         (178, 24, 43, 256),
        #     ]) / 256)


SEISMIC_WIDE2 = LinearSegmentedColormap.from_list(
    "seismic_wider",
    [
        (0, 0, 0.3, 1),
        (0, 0, 0.7, 1),
        (0.1, 0.1, 0.9, 1),
        (0.3, 0.3, 0.95, 1),
        (0.6, 0.6, 1, 1),
        (0.85, 0.85, 1, 1),
        (0.92, 0.92, 1, 0.99),
        (0.98, 0.98, 1, 0.98),
        (1, 1, 1, 0.95),
        (1, 0.98, 0.98, 0.98),
        (1, 0.92, 0.92, 0.99),
        (1, 0.85, 0.85, 1),
        (1, 0.6, 0.6, 1),
        (1, 0.3, 0.3, 1),
        (0.9, 0.1, 0.1, 1),
        (0.7, 0, 0, 1),
        (0.3, 0, 0, 1),
    ],
    N=250,
)
plt.register_cmap("seismic_wider", SEISMIC_WIDE2)
SEISMIC_WIDE_Y = LinearSegmentedColormap.from_list(
    "seismic_wide_y",
    [
Example #49
0
def plot_onestep_1d(
    res,
    function,
    test_box,
    x_next,
    eta,
    rhos,
    theta_S,
    x0,
    y0,
    smoothGaussians=True,
    plotGradient=False,
):

    x_max = test_box[0, 0]
    x_min = test_box[1, 0]
    X = np.linspace(x_min, x_max, res)

    y_func = function(X)
    plt.plot(X, y_func, "--k", label="objective function")
    y_max = y_func.max() * 1.2
    y_min = y_func.min() * 1.2

    y_loss = np.zeros(X.shape[0])
    y_loss_grad = np.zeros(X.shape[0])

    for i in range(X.shape[0]):
        y_loss[i] = expectedLoss(X[i].reshape((1, -1)),
                                 eta,
                                 rhos,
                                 theta_S,
                                 x0=x0,
                                 y0=y0).flatten()
        y_loss_grad[i] = expectedLossGrad(X[i].reshape((1, -1)),
                                          eta,
                                          rhos,
                                          theta_S,
                                          x0=x0,
                                          y0=y0)

    mean, std = get_plotting_mean_std(X.reshape((-1, 1)), rhos, theta_S, x0,
                                      y0)

    plt.plot(X, y_loss, color="blue", label="expected loss")
    if plotGradient:
        plt.plot(X, y_loss_grad, color="green", label="expected loss grad")

    plt.plot(X, mean, "r", label="mean")
    if smoothGaussians:
        y_space = np.linspace(
            y_min, y_max, int(X.shape[0] * (y_max - y_min) / (x_max - x_min)))
        M = np.flip(-np.abs(y_space[:, None] - mean[None, :]) / std[None, :] +
                    3,
                    axis=0)
        cm = LinearSegmentedColormap.from_list("myMap", [(1, 1, 1),
                                                         (1, 0.5, 0.5)])
        plt.imshow(
            M,
            extent=[x_min, x_max, y_min, y_max],
            interpolation="bicubic",
            vmin=0,
            vmax=3,
            cmap=cm,
        )
    else:
        plt.fill_between(X,
                         mean - std,
                         mean + std,
                         alpha=0.1,
                         color="red",
                         label="+- 1SD")
    plt.scatter(x0, y0, marker="+", color="black", s=300, label="observation")

    x_next_loss = expectedLoss(x_next, eta, rhos, theta_S, x0=x0, y0=y0)
    plt.scatter(x_next,
                x_next_loss,
                color="blue",
                marker="D",
                label="next evaluation")

    plt.hlines(
        eta,
        x_min,
        x_max,
        linestyles="--",
        linewidths=0.8,
        color="green",
        label=r"$\eta$",
    )

    plt.ylim(y_min, y_max)
    plt.xlim(x_min, x_max)
    plt.title("One Step Lookahead: function evaluation #" + str(x0.shape[0]))
    plt.grid(True)
    plt.legend(loc="best")
    plt.show()
Example #50
0
def plot_field_panel(
    grid,
    field,
    level,
    fmin,
    fmax,
    lat_index=None,
    lon_index=None,
    date="",
    name_multi="",
    shp_name="",
    hailpad_pos=None,
    zero_height=3.0,
    minusforty_height=10.0,
    grid_spc=0.25,
    cmap=None,
    reverse_cmap=False,
    norm=None,
    xlim=(-48, -46),
    ylim=(-24, -22),
    save_path="./",
):
    """
    Using gridded multidoppler processed data, plot horizontal and vertical
    views:
    - In a specific height (defined by index)
    - In a specific cross-section (defined by lat_index and lon_index)

    Parameters
    ----------
    grid: gridded multidoppler processed data
    field: field to be plotted
    level: level of horizontal plot
    fmin, fmax: field min and max values
    lat_index: tuple of latitude indexes for cross section
        (end, start) in degrees
    lon_index: tuple of longitude indexes for cross section
        (end, start) in degrees
    date: date to be shown on main title
    name_multi: acronym with all radar names
    shp_name: path of shapefiles
    hailpad_pos: tuple of hailpad position
        (lon, lat)
    zero_height: 0 degrees height
    grid_spc: grid spacing for horizontal plot
    cmap: define colorbar. None will use Py-ART defauts
    reverse_cmap: If cmap is defined and this is True, the colormap will be
        reversed
    norm: normalization of the colormap
    xlim, ylim: plot limits in lon, lat for horizontal view
        (min, max) in degrees
    save_path: path to save the figures
    """

    # Getting lat-lon-z points
    lons, lats = grid.get_point_longitude_latitude(level)
    xz, z = np.meshgrid(grid.get_point_longitude_latitude()[0], grid.z["data"])

    # Opening colortables
    if field != "FH":
        if cmap:
            cpt = loadCPT(cmap)
            if reverse_cmap:
                cmap = LinearSegmentedColormap("cpt_r", revcmap(cpt))
            else:
                cmap = LinearSegmentedColormap("cpt", cpt)

    # Main figure
    display = pyart.graph.GridMapDisplay(grid)
    fig = plt.figure(figsize=(10, 3.25), constrained_layout=True)
    if field == "FH":
        gs = GridSpec(nrows=1, ncols=8, figure=fig)
    else:
        gs = GridSpec(nrows=1, ncols=7, figure=fig)

    # - Horizontal view
    print("-- Plotting horizontal view --")
    ax1 = fig.add_subplot(gs[0, :3])
    display.plot_basemap(
        min_lon=xlim[0],
        max_lon=xlim[1],
        min_lat=ylim[0],
        max_lat=ylim[1],
        lon_lines=np.arange(xlim[0], xlim[1], grid_spc),
        lat_lines=np.arange(ylim[0], ylim[1], grid_spc),
        auto_range=False,
    )
    display.basemap.readshapefile(shp_name, "sao_paulo", color="gray")
    # -- Reflectivity (shaded)
    display.plot_grid(
        field,
        level,
        vmin=fmin,
        vmax=fmax,
        cmap=cmap,
        colorbar_flag=False,
        norm=norm,
    )

    # -- Hailpad position
    display.basemap.plot(
        hailpad_pos[0],
        hailpad_pos[1],
        "kX",
        markersize=15,
        markerfacecolor="None",
        latlon=True,
    )
    # -- Cross section position
    display.basemap.plot(lon_index, lat_index, "k--", latlon=True)

    # - Vertical view
    print("-- Plotting vertical view --")
    ax2 = fig.add_subplot(gs[0, 3:])
    # -- Reflectivity (shaded)
    display.plot_latlon_slice(
        field,
        vmin=fmin,
        vmax=fmax,
        coord1=(lon_index[0], lat_index[0]),
        coord2=(lon_index[1], lat_index[1]),
        zerodeg_height=zero_height,
        minusfortydeg_height=minusforty_height,
        zdh_col="k",
        cmap=cmap,
        dot_pos=hailpad_pos,
        colorbar_flag=False,
        norm=norm,
    )
    cb = display.plot_colorbar(
        orientation="vertical", label=grid.fields[field]["units"]
    )
    if field == "FH":
        cb = adjust_fhc_colorbar_for_pyart(cb)

    # - General aspects
    plt.suptitle(
        name_multi + " " + date,
        weight="bold",
        stretch="condensed",
        size="x-large",
    )
    ax1.set_title(
        str(level + 1) + " km " + grid.fields[field]["standard_name"].title()
    )
    ax2.set_title(
        "Cross Section " + grid.fields[field]["standard_name"].title()
    )
    ax2.set_xlabel("")
    ax2.set_ylabel("Distance above Ground (km)")
    ax2.grid(linestyle="-", linewidth=0.25)
    plt.savefig(
        save_path
        + name_multi
        + " "
        + grid.fields[field]["standard_name"].title()
        + " "
        + date
        + ".png",
        dpi=300,
        bbox_inches="tight",
        transparent=True,
    )
Example #51
0
    mpl.rc('axes', unicode_minus=False, linewidth=0.8)
    mpl.rc('figure.subplot', right=0.97, top=0.97, bottom=0.15, left=0.13)
    mpl.rc('axes', titlepad=-10)
    mpl.rc('axes', prop_cycle=mpl.cycler(
        'color', [
            '#0083b8', '#e66400', '#93a661', '#ebc944', '#da1884', '#7e48bd']))
    # girl scouts green is 0x00ae58
    # #0083b8 = (0.0, 0.51, 0.72)
    mpl.rc('figure', max_open_warning=False)

def gen_cdict(red, green, blue, midpoint, frac):
    return {'red':   [[0.0,  1.0, 1.0],
                      [midpoint,  red, red],
                      [1.0,  red*frac, red*frac]],
            'green': [[0.0,  1.0, 1.0],
                      [midpoint, green, green],
                      [1.0,  green*frac, green*frac]],
            'blue':  [[0.0,  1.0, 1.0],
                      [midpoint,  blue, blue],
                      [1.0,  blue*frac, blue*frac]]}

blue_cmap = LinearSegmentedColormap(
    'pbpl_blue_cmap', segmentdata=gen_cdict(0, 0.51, 0.72, 0.75, 0.3), N=256)
    # 'pbpl_blue_cmap', segmentdata=gen_cdict(0, 0.51, 0.72, 0.6, 0.3), N=256)

orange_cmap = LinearSegmentedColormap(
    'pbpl_orange_cmap', segmentdata=gen_cdict(0.90, 0.39, 0, 0.75, 0.3), N=256)

BlueOrange_cmap = LinearSegmentedColormap.from_list(
    'BlueOrange_colormap', ['#0083b8', '#ffffff', '#e66400'], 256)
def plotter(image,
            odor_on,
            water_on,
            odor_names,
            condition_config,
            save_path,
            name_str=''):
    if black:
        plt.style.use('dark_background')

    frames_per_trial = 75

    titles = odor_names
    n_plots = int(image.shape[1] / frames_per_trial)

    fig = plt.figure(figsize=(3.5, 3))
    fig_width = .14 * n_plots
    rect = [.1, .1, fig_width, .7]
    rect_cb = [fig_width + .1 + .02, 0.1, 0.02, .7]
    ax = fig.add_axes(rect)

    if black:
        from matplotlib.colors import LinearSegmentedColormap
        cdict1 = {
            'red': ((0.0, 0.0, 0.0), (0.5, 0.0, 0.1), (1.0, 1.0, 1.0)),
            'green': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)),
            'blue': ((0.0, 0.0, 0.55), (0.5, 0.1, 0.0), (1.0, 0.0, 0.0))
        }
        cmap = LinearSegmentedColormap('BlueRed1', cdict1)
        cmap = LinearSegmentedColormap.from_list("",
                                                 ["turquoise", "black", "red"])

        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.spines['bottom'].set_visible(False)
        ax.spines['left'].set_visible(False)
        ax.tick_params(axis=u'both', which=u'both', length=0)
    else:
        cmap = 'bwr'
        plt.tick_params(direction='out', length=2, width=.5, grid_alpha=0.5)

    plt.imshow(image,
               vmin=-condition_config.vlim,
               vmax=condition_config.vlim,
               cmap=cmap)
    plt.axis('tight')

    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')

    condition_lines = np.cumsum([frames_per_trial] * n_plots)[:-1]
    odor_on_lines_raw = np.arange(odor_on, frames_per_trial * n_plots,
                                  frames_per_trial)
    water_on_lines = np.arange(water_on, frames_per_trial * n_plots,
                               frames_per_trial)
    if 'water' in titles:
        water_on_lines = water_on_lines[[0, 1, 4]]
        # water_on_lines = water_on_lines[[2, 3, 4]]
        odor_on_lines = odor_on_lines_raw[:-1]
    else:
        if condition_config.period == 'pt':
            odor_on_lines = odor_on_lines_raw
        else:
            if condition_config.filter_ix is None:
                water_on_lines = water_on_lines[:2]
                # water_on_lines = water_on_lines[2:]
            else:
                water_on_lines = water_on_lines[[0]]
            odor_on_lines = odor_on_lines_raw

    if not naive:
        xticks = np.concatenate(
            (odor_on_lines, odor_on_lines + 8, water_on_lines))
        xticklabels = ['ON'] * len(odor_on_lines) + ['OFF'] * len(
            odor_on_lines) + ['US'] * len(water_on_lines)
    else:
        xticks = np.concatenate((odor_on_lines, odor_on_lines + 8))
        xticklabels = ['ON'] * len(odor_on_lines) + ['OFF'
                                                     ] * len(odor_on_lines)

    plt.xticks(xticks, xticklabels, fontsize=5)
    range = image.shape[0]
    if range > 100:
        interval = 50
    else:
        interval = 25
    plt.yticks(np.arange(0, range, interval))

    for line in xticks:
        plt.plot([line, line],
                 plt.ylim(),
                 '--',
                 color='grey',
                 linewidth=.5,
                 alpha=0.5)

    for line in condition_lines:
        plt.plot([line, line],
                 plt.ylim(),
                 '--',
                 color='grey',
                 linewidth=.75,
                 alpha=1)

    for j, x in enumerate(odor_on_lines_raw):
        plt.text(x, -1, titles[j].upper())

    axcb = fig.add_axes(rect_cb)
    cb = plt.colorbar(cax=axcb,
                      ticks=[-condition_config.vlim, condition_config.vlim])

    if black:
        cb.outline.set_visible(False)
        cb.set_ticks([])
    else:
        cb.outline.set_linewidth(0.5)
        cb.set_label(r'$\Delta$ F/F', fontsize=7, labelpad=-10)

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

    name_black = '_black' if black else ''
    if condition_config.plot_big:
        name = 'big_mouse_' + ','.join(
            [str(x) for x in condition_config.plot_big_days])
        name += '_sorted_to_' + ','.join(
            [str(x) for x in condition_config.sort_days])
        name += name_black
    else:
        name = 'mouse_' + str(mouse) + name_str
        if not condition_config.independent_sort:
            name += '_sorted_to_' + str(condition_config.sort_days)

    if condition_config.filter_ix is not None:
        name += '_odor_' + str(condition_config.filter_ix)

    plt.sca(ax)
    # plt.title(name)
    plot._easy_save(save_path, name)
    mask = data > 999.
    data[data > datamax] = datamax
    data[data < datamin] = datamin
    #Finally, nan out the missing data based on the above "mask test"
    data[mask] = np.nan

    fig = plt.figure(figsize=(figxsize, figysize))
    ax1 = fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False, axisbg='#F5F5F5')

    # setup basemap.
    m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90,area_thresh=10000,\
     llcrnrlon=-180,urcrnrlon=180,resolution='l')
    #m.drawlsmask(land_color='#E2E2E2', ocean_color='#E2E2E2', lakes=False)

    cdict1 = gmtColormap('./OceanHeatContent.cpt')
    cmap_temp = LinearSegmentedColormap('cmap_temp', cdict1)

    # Set up grid
    x, y = np.meshgrid(lons, lats)

    levs = np.arange(101) - 50
    m.contourf(x, y, data, levs, cmap=cmap_temp)
    #m.fillcontinents(color='#E2E2E2', lake_color='#E2E2E2')
    #m.drawcoastlines(color='#787878',linewidth=0.15)
    #m.drawcoastlines(color='#E2E2E2',linewidth=0.15)
    #m.drawrivers(color='#E2E2E2',linewidth=1)

    plt.savefig(outpng,
                dpi=figdpi,
                orientation='landscape',
                bbox_inches='tight',
Example #54
0
import numpy as np
import h5py
import imagesize

from PIL import Image as ImagePil

ImagePil.MAX_IMAGE_PIXELS = None

import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

from irreg.normalizations import min_max_scaler

cmap_eosin = LinearSegmentedColormap.from_list("mycmap",
                                               ["white", "darkviolet"])
cmap_hema = LinearSegmentedColormap.from_list("mycmap", ["white", "navy"])


class Image:
    data = None

    def __init__(self, data):
        self.data = data

    @staticmethod
    def read(filename):
        img = ImagePil.open(filename)
        img = np.asarray(img)
        return Image(img)

    @staticmethod
Example #55
0
def main():
    """ Main function """

    # Initialize and configure tmap
    dims = 2048
    enc = tm.Minhash(dims)
    lf = tm.LSHForest(dims, 128, store=True)

    fps = []
    # fps_umap = []
    for row in DATA:
        fps.append(tm.VectorUint(list(row)))

    lf.batch_add(enc.batch_from_sparse_binary_array(fps))
    lf.index()

    x_tmap, y_tmap, s, t, _ = tm.layout_from_lsh_forest(lf, CFG_TMAP)
    lf.clear()

    # Prepare custom color map
    tab10 = plt.get_cmap("tab10").colors
    colors_gray = [(0.2, 0.2, 0.2), tab10[0], tab10[1], tab10[2], tab10[3],
                   tab10[4]]
    custom_cm_gray = LinearSegmentedColormap.from_list("custom_cm_gray",
                                                       colors_gray,
                                                       N=len(colors_gray))

    legend_labels = [
        (1, "Rudyard Kipling"),
        (2, "Herbert George Wells"),
        (3, "Charles Darwin"),
        (4, "George Bernard Shaw"),
        (5, "William Wymark Jacobs"),
        (0, "Other"),
    ]

    faerun = Faerun(
        clear_color="#111111",
        view="front",
        coords=False,
        alpha_blending=True,
        legend_title="",
    )
    faerun.add_scatter(
        "gutenberg",
        {
            "x": x_tmap,
            "y": y_tmap,
            "c": LABELS,
            "labels": FAERUN_LABELS
        },
        colormap=custom_cm_gray,
        point_scale=4.2,
        max_point_size=10,
        has_legend=True,
        categorical=True,
        legend_title="Authors",
        legend_labels=legend_labels,
        shader="smoothCircle",
        selected_labels=["Author", "Title"],
    )
    faerun.add_tree(
        "gutenberg_tree",
        {
            "from": s,
            "to": t
        },
        point_helper="gutenberg",
        color="#222222",
    )
    faerun.plot("gutenberg", template="default")
				 (0.25, 72./255., 72./255.),
				 (0.5, 60./255., 60./255.),
				 (0.75, 111./255., 111./255.),
				 (1.0, 1.0, 1.0)),
		 'green': ((0.0, 0.0, 0.0),
				 (0.25, 0./255., 0./255.),
				 (0.5, 112./255., 112./255.),
				 (0.75, 200./255., 200./255.),
				 (1.0, 1.0, 1.0)),
		 'blue': ((0.0, 0.0, 0.0),
				 (0.25, 173./255., 173./255.),
				 (0.5, 229./255., 229./255.),
				 (0.75, 204./255., 204./255.),
				 (1.0, 1.0, 1.0))}
'''
my_cmap = LinearSegmentedColormap('my_colormap', cdict, 256)

######
## Image picture of matS

theFig = plt.figure(figsize=(14, 9))
theImage = plt.imshow(scaledMatS,
                      aspect='auto',
                      interpolation='none',
                      cmap=my_cmap)
theImage.set_clim(0.0, 1.0)
plt.xlabel('Time (tenths of seconds)', fontsize=10)
plt.xticks(fontsize=10)
plt.yticks(range(0, num_normalForms), forte_names, fontsize=10)

### Everything we need for the colorbar
Example #57
0
def image_plot_v2(shap_values,
                  x,
                  labels=None,
                  show=True,
                  width=20,
                  aspect=0.2,
                  hspace=0.2,
                  labelpad=None):

    input_image = list()
    curr_gray_image = list()
    multi_output = True

    if type(shap_values) != list:
        multi_output = False
        shap_values = [shap_values]

    # make sure labels
    if labels is not None:
        assert labels.shape[0] == shap_values[0].shape[
            0], "Labels must have same row count as shap_values arrays!"
        if multi_output:
            assert labels.shape[1] == len(
                shap_values
            ), "Labels must have a column for each output in shap_values!"
        else:
            assert len(
                labels.shape
            ) == 1, "Labels must be a vector for single output shap_values."

    label_kwargs = {} if labelpad is None else {'pad': labelpad}

    # plot our explanations
    fig_size = np.array([3 * (len(shap_values) + 1), 2.5 * (x.shape[0] + 1)])

    if fig_size[0] > width:
        fig_size *= width / fig_size[0]
    fig, axes = pl.subplots(nrows=x.shape[0],
                            ncols=len(shap_values) + 1,
                            figsize=fig_size)

    if len(axes.shape) == 1:
        axes = axes.reshape(1, axes.size)

    for row in range(x.shape[0]):
        x_curr = x[row].copy()

        # make sure
        if len(x_curr.shape) == 3 and x_curr.shape[2] == 1:
            x_curr = x_curr.reshape(x_curr.shape[:2])

        if x_curr.max() > 1:
            x_curr /= 255.

        # get a grayscale version of the image
        if len(x_curr.shape) == 3 and x_curr.shape[2] == 3:
            x_curr_gray = (0.2989 * x_curr[:, :, 0] +
                           0.5870 * x_curr[:, :, 1] + 0.1140 * x_curr[:, :, 2]
                           )  # rgb to gray
        else:
            x_curr_gray = x_curr

        axes[row, 0].imshow(x_curr)
        axes[row, 0].axis('off')

        input_image.append(x_curr)
        curr_gray_image.append(x_curr_gray)

        if len(shap_values[0][row].shape) == 2:
            abs_vals = np.stack(
                [np.abs(shap_values[i]) for i in range(len(shap_values))],
                0).flatten()
        else:
            abs_vals = np.stack([
                np.abs(shap_values[i].sum(-1)) for i in range(len(shap_values))
            ], 0).flatten()
        max_val = np.nanpercentile(abs_vals, 99.9)

        for i in range(len(shap_values)):
            if labels is not None:
                axes[row, i + 1].set_title(labels[row, i], **label_kwargs)
            sv = shap_values[i][row] if len(
                shap_values[i][row].shape) == 2 else shap_values[i][row].sum(
                    -1)
            axes[row, i + 1].imshow(x_curr_gray,
                                    cmap=pl.get_cmap('gray'),
                                    alpha=0.15,
                                    extent=(-1, sv.shape[0], sv.shape[1], -1))

            colors = []
            for l in np.linspace(1, 0, 100):
                colors.append((30. / 255, 136. / 255, 229. / 255, l))
            for l in np.linspace(0, 1, 100):
                colors.append((255. / 255, 13. / 255, 87. / 255, l))

            cm = LinearSegmentedColormap.from_list("red_transparent_blue",
                                                   colors)
            im = axes[row, i + 1].imshow(sv,
                                         cmap=cm,
                                         vmin=-max_val,
                                         vmax=max_val)
            axes[row, i + 1].axis('off')

    hspace = 0.5
    if hspace == 'auto':
        fig.tight_layout()
    else:
        fig.subplots_adjust(hspace=hspace)

    cb = fig.colorbar(im,
                      ax=np.ravel(axes).tolist(),
                      label="SHAP value",
                      orientation="horizontal",
                      aspect=fig_size[0] / aspect)
    cb.outline.set_visible(False)

    fig.savefig('output.png')
Example #58
0
 [0.6473, 0.7456, 0.4188], [0.6834190476, 0.7434761905, 0.4044333333], 
 [0.7184095238, 0.7411333333, 0.3904761905], 
 [0.7524857143, 0.7384, 0.3768142857], [0.7858428571, 0.7355666667, 
  0.3632714286], [0.8185047619, 0.7327333333, 0.3497904762], 
 [0.8506571429, 0.7299, 0.3360285714], [0.8824333333, 0.7274333333, 0.3217], 
 [0.9139333333, 0.7257857143, 0.3062761905], [0.9449571429, 0.7261142857, 
  0.2886428571], [0.9738952381, 0.7313952381, 0.266647619], 
 [0.9937714286, 0.7454571429, 0.240347619], [0.9990428571, 0.7653142857, 
  0.2164142857], [0.9955333333, 0.7860571429, 0.196652381], 
 [0.988, 0.8066, 0.1793666667], [0.9788571429, 0.8271428571, 0.1633142857], 
 [0.9697, 0.8481380952, 0.147452381], [0.9625857143, 0.8705142857, 0.1309], 
 [0.9588714286, 0.8949, 0.1132428571], [0.9598238095, 0.9218333333, 
  0.0948380952], [0.9661, 0.9514428571, 0.0755333333], 
 [0.9763, 0.9831, 0.0538]]

parula_map = LinearSegmentedColormap.from_list('parula', cm_data)
# For use of "viscm view"
test_cm = parula_map

if __name__ == "__main__":
    import matplotlib.pyplot as plt
    import numpy as np

    try:
        from viscm import viscm
        viscm(parula_map)
    except ImportError:
        print("viscm not found, falling back on simple display")
        plt.imshow(np.linspace(0, 100, 256)[None, :], aspect='auto',
                   cmap=parula_map)
    plt.show()
Example #59
0
import matplotlib.cm as cm
from pylab import setp
from osgeo import gdal
from mpl_toolkits.axes_grid1 import make_axes_locatable
import os

# read arguments
arguments = docopt.docopt(__doc__)
infile = arguments["--infile"]

if arguments["--cpt"] is None:
    # cmap=cm.jet
    try:
        from matplotlib.colors import LinearSegmentedColormap
        cm_locs = os.environ["PYGDALSAR"] + '/contrib/python/colormaps/'
        cmap = LinearSegmentedColormap.from_list(
            'roma', np.loadtxt(cm_locs + "roma.txt"))
        cmap = cmap.reversed()
    except:
        cmap = cm.rainbow
else:
    cmap = arguments["--cpt"]

if arguments["--format"] == None:
    sformat = 'ROI_PAC'
else:
    sformat = arguments["--format"]

if arguments["--lectfile"] == None:
    lecfile = "lect.in"
else:
    lecfile = arguments["--lectfile"]
Example #60
0
            mainString = mainString.replace(elem, newString)

    return mainString


# This for-loop will get tweet from mongo to value(text)
for x in collection.find({}, {"text": 1, "_id": 0}):
    tweet = x['text']
    text = text + " " + tweet
    text = replaceMultiple(text, ['https', 'RT', 'co'], '')

# Generate a word cloud image
# ตัดคำพวก stopword เช่น "the", "a", "an", "or", "not", "in"
stopword = set(STOPWORDS)
# เปิดภาพแล้วเก็บภาพเป็น array
shape = np.array(Image.open('google_mask.png'))
colors = ["#FF0000", "#111111", "#101010", "#121212", "#212121", "#222222"]
cmap = LinearSegmentedColormap.from_list("mycmap", colors)

# Word cloud object สำหรับการสร้างภาพและวาดภาพ โดยมีการกำหนด background_color คือสีพื้นหลังสำหรับรูป WordCloud,
# stopwords คือ คำที่เราต้องการกำจัดออกไป, mask คือลักษณะที่จะวาด WordCloud ลงไป, width กว้าง, height ยาว, เซตของสีที่จะใช้
# generate สร้าง WordCloud จาก text
wordcloud = WordCloud(background_color="white",
                      stopwords=stopword, mask=shape, width=1987, height=736, colormap=cmap).generate(text)
# แสดงรูปภาพ wordcloud
plt.imshow(wordcloud, interpolation='bilinear')
# ปิดไม่แสดงเส้นแกน x,y
plt.axis("off")
# แสดงหน้าต่างและรูป
plt.show()