Exemplo n.º 1
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
Exemplo n.º 2
0
class Colormap(object):
    """
    Class to define a colormap.
    """
    def __init__(self, bad_color='0.7', colormap='default'):
        """
        Initialize parameters of the experiment.

        :param colormap: a colormap string. Available choices at the moment: 'default'
        :param bad_color: a string which defines the grey level for nan pixels. example: '0.7'
        """
        if colormap == 'default':
            color_dict = {
                'red': ((0.0, 1.0, 1.0), (0.11, 0.0, 0.0), (0.36, 0.0, 0.0),
                        (0.62, 1.0, 1.0), (0.87, 1.0, 1.0), (1.0, 0.0, 0.0)),
                'green': ((0.0, 1.0, 1.0), (0.11, 0.0, 0.0), (0.36, 1.0, 1.0),
                          (0.62, 1.0, 1.0), (0.87, 0.0, 0.0), (1.0, 0.0, 0.0)),
                'blue': ((0.0, 1.0, 1.0), (0.11, 1.0, 1.0), (0.36, 1.0, 1.0),
                         (0.62, 0.0, 0.0), (0.87, 0.0, 0.0), (1.0, 0.0, 0.0))
            }
        else:
            raise ValueError('Only available colormaps: "default"')
        self.cdict = color_dict
        self.bad_color = bad_color
        self.cmap = LinearSegmentedColormap('my_colormap', color_dict, 256)
        self.cmap.set_bad(color=bad_color)
Exemplo n.º 3
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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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
Exemplo n.º 6
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
Exemplo n.º 7
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)
Exemplo n.º 8
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)
Exemplo n.º 9
0
def get_color_map(color):
    '''Generate a LinearSegmentedColormap with a single color and varying
    transparency. Bad values and values below the lower limit are set to be
    completely transparent.

    Parameters
    ----------
    color: string or array-like with shape (3,)
        The color to use when overlaying the survey footprint. Either a
        string that can be input to colorConverter.to_rgb() or rgb triplet.

    Returns
    -------
    colormap1: LinearSegmentedColormap
        A color map that is a single color but varies its opacity
        from 0.5 to 1. Bad values and values below the minimum are completely
        transparent.
    '''

    from matplotlib.colors import LinearSegmentedColormap
    from matplotlib.colors import colorConverter

#   if the type is a string it is assumed to be an input to allow us
#   to get an rgb value. If it is not a string and length is 3, it is
#   assumed to be an actual rgb value
    if isinstance(color, str):
        rgb = colorConverter.to_rgb(color)
    elif len(color) == 3:
        rgb = color
    else:
        raise ValueError('Bad color input')

    cdict = {'red':   [(0, rgb[0], rgb[0]),
                       (1, rgb[0], rgb[0])],
             'green': [(0, rgb[1], rgb[1]),
                       (1, rgb[1], rgb[1])],
             'blue':  [(0, rgb[2], rgb[2]),
                       (1, rgb[2], rgb[2])],
             'alpha': [(0, 0.5, 0.5),
                       (1, 1, 1)]}

    colormap1 = LinearSegmentedColormap('FootprintCM', cdict)
    colormap1.set_bad(alpha=0.0)
    colormap1.set_under(alpha=0.0)

    return colormap1
Exemplo n.º 10
0
def red_green():
    """Creates a divergent colormap centered on black and ranging from red (low) to green (high).

    Returns:
        LinearSegmentedColormap in the red-black-green range.
    """

    color_dict = {
        'red': ((0.0, 0.0, 1.0), (0.5, 0.0, 0.0), (1.0, 0.0, 0.0)),
        'green': ((0.0, 0.0, 0.0), (0.5, 0.0, 0.0), (1.0, 1.0, 1.0)),
        'blue': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0))
    }

    cmap = LinearSegmentedColormap('RedGreen', color_dict)
    cmap.set_bad('gray', 0.5)

    return cmap
Exemplo n.º 11
0
def get_color_map(color):
    '''Generate a LinearSegmentedColormap with a single color and varying
    transparency. Bad values and values below the lower limit are set to be
    completely transparent.

    Parameters
    ----------
    color: string or array-like with shape (3,)
        The color to use when overlaying the survey footprint. Either a
        string that can be input to colorConverter.to_rgb() or rgb triplet.

    Returns
    -------
    colormap1: LinearSegmentedColormap
        A color map that is a single color but varies its opacity
        from 0.5 to 1. Bad values and values below the minimum are completely
        transparent.
    '''

    from matplotlib.colors import LinearSegmentedColormap
    from matplotlib.colors import colorConverter

    #   if the type is a string it is assumed to be an input to allow us
    #   to get an rgb value. If it is not a string and length is 3, it is
    #   assumed to be an actual rgb value
    if isinstance(color, str):
        rgb = colorConverter.to_rgb(color)
    elif len(color) == 3:
        rgb = color
    else:
        raise ValueError('Bad color input')

    cdict = {
        'red': [(0, rgb[0], rgb[0]), (1, rgb[0], rgb[0])],
        'green': [(0, rgb[1], rgb[1]), (1, rgb[1], rgb[1])],
        'blue': [(0, rgb[2], rgb[2]), (1, rgb[2], rgb[2])],
        'alpha': [(0, 0.5, 0.5), (1, 1, 1)]
    }

    colormap1 = LinearSegmentedColormap('FootprintCM', cdict)
    colormap1.set_bad(alpha=0.0)
    colormap1.set_under(alpha=0.0)

    return colormap1
Exemplo n.º 12
0
class ColormapFactory:
    """
    Class to define a colormap.

    :param colormap: a colormap string. Available choices: 'turbo',
     'custom'
    :param bad_color: a string which defines the grey level for nan pixels, e.g. '0.7'
    """

    valid_colormaps = ["turbo", "custom"]

    def __init__(self, bad_color="0.7", colormap="turbo"):
        self.bad_color = bad_color
        self.colormap = colormap
        self.cmap: Optional[Colormap] = None

    @property
    def bad_color(self):
        """Color for masked values."""
        return self._bad_color

    @bad_color.setter
    def bad_color(self, val: str):
        if not isinstance(val, str):
            raise TypeError(f"bad_color should be a str, got {type(val)})")
        if not is_float(val) or not 0.0 <= float(val) <= 1.0:
            raise ValueError(
                "float(bad_color) should be a number between 0 and 1")
        self._bad_color = val

    def generate_cmap(self):
        """Build and return the colormap."""
        if self.colormap == "turbo":
            self.cmap = ListedColormap(turbo_colormap_data)
        elif self.colormap == "custom":
            self.cmap = LinearSegmentedColormap("my_colormap",
                                                custom_colormap_data, 256)
        elif self.colormap in cc.cm:
            self.cmap = getattr(cc.cm, self.colormap)
        else:
            raise NotImplementedError(
                f"colormap {self.colormap} not implemented")
        self.cmap.set_bad(color=self.bad_color)
        return self.cmap
Exemplo n.º 13
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
Exemplo n.º 14
0
def make_cmaps():

    soft0 = [
        (0.0, 1.0, 1.0),
        (0.0, 0.0, 1.0),
        (1.0, 0.0, 0.0),
        (1.0, 1.0, 0.0),
    ]

    # (relative point, low side color component, high side color component)

    hard0 = dict(red=((0.0, 0.0, 0.0), (0.5, 0.0, 1.0), (1.0, 1.0, 1.0)),
                 green=((0.0, 0.0, 1.0), (0.5, 0.0, 0.0), (1.0, 1.0, 1.0)),
                 blue=((0.0, 0.0, 0.0), (0.5, 1.0, 0.0), (1.0, 0.0, 0.0)))

    cm = LinearSegmentedColormap.from_list('wct_soft0', soft0, N=100)
    cm.set_bad(color='white')
    plt.register_cmap(cmap=cm)

    cm = LinearSegmentedColormap('wct_hard0', hard0, N=100)
    cm.set_bad(color='white')
    plt.register_cmap(cmap=cm)
Exemplo n.º 15
0
def get_cmap(norm='linear', log_min=None, reverse=False):
    colors = np.array([
        np.array([0.0, 4., 76.]) / 255.,
        np.array([49., 130., 0.0]) / 255.,
        np.array([255, 197., 98.]) / 255.,
        np.array([245., 179., 223.]) / 255.,
        np.array([1, 1, 1]),
    ])

    if norm == 'linear':
        steps = np.linspace(0, 1, len(colors))
    elif norm == 'log':
        steps = np.logspace(log_min, 0, len(colors))
        steps[0] = 0

    if reverse:
        colors = colors[::-1]

    r = np.zeros((len(colors), 3))
    r[:, 0] = steps
    r[:, 1] = colors[:, 0]
    r[:, 2] = colors[:, 0]

    g = np.zeros((len(colors), 3))
    g[:, 0] = steps
    g[:, 1] = colors[:, 1]
    g[:, 2] = colors[:, 1]

    b = np.zeros((len(colors), 3))
    b[:, 0] = steps
    b[:, 1] = colors[:, 2]
    b[:, 2] = colors[:, 2]

    cdict = {'red': r, 'green': g, 'blue': b}

    hag_cmap = LinearSegmentedColormap('hag_cmap', cdict)
    hag_cmap.set_bad('black')
    return hag_cmap
Exemplo n.º 16
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
Exemplo n.º 17
0
def compute_presence_map(dx,
                         x,
                         y,
                         xmin,
                         xmax,
                         ymin,
                         ymax,
                         seuil_numpos=30000,
                         lat_space=5,
                         lon_space=20):

    x = np.array(x)
    x[x >= xmax] -= 360
    y = np.array(y)

    x_ravel = x.ravel()
    y_ravel = y.ravel()
    x_ravel[np.where(y_ravel == 0)] = 0
    y_ravel[np.where(x_ravel == 0)] = 0
    x_ravel_nonzero = np.delete(x_ravel, np.where(x_ravel == 0))
    y_ravel_nonzero = np.delete(y_ravel, np.where(y_ravel == 0))

    x_ravel_nonone = np.delete(x_ravel_nonzero,
                               np.where(x_ravel_nonzero == 1.0))
    y_ravel_nonone = np.delete(y_ravel_nonzero,
                               np.where(y_ravel_nonzero == 1.0))
    x_ravel_nonone = np.append(x_ravel_nonone, 0)
    x_ravel_nonone = np.append(x_ravel_nonone, 410)
    y_ravel_nonone = np.append(y_ravel_nonone, -90)
    y_ravel_nonone = np.append(y_ravel_nonone, 90)

    x, y = x_ravel_nonone, y_ravel_nonone

    heatmap, xedges, yedges = np.histogram2d(
        x,
        y,
        bins=(np.arange(xmin, xmax + dx, dx), np.arange(ymin, ymax + dx, dx)))
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

    heatmap[np.where(heatmap > seuil_numpos)] = seuil_numpos

    #CMAP IMOS
    pimos = {
        'red': (
            (0.00, 1.0, 1.0),
            #                       (0.03,  0.624, 0.624),
            (0.08, 0.000, 0.000),
            (0.25, 0.000, 0.000),
            #                       (0.33,  0.000, 0.000),
            (0.42, 0.000, 0.000),
            #                      (0.50,  0.000, 0.000),
            (0.58, 1.000, 1.000),
            (0.67, 1.000, 1.000),
            #                       (0.75,  1.000, 1.000),
            (0.83, 1.000, 1.000),
            #                       (0.92,  0.651, 0.651),
            (1.00, 0.471, 0.471)),
        'green': (
            (0.00, 1.0, 1.0),
            #                      (0.03,  0.624, 0.624),
            (0.08, 0.500, 0.500),
            (0.25, 0.000, 0.000),
            #                       (0.33,  0.000, 0.000),
            (0.42, 0.749, 0.749),
            #                       (0.50,  0.498, 0.498),
            (0.58, 1.000, 1.000),
            (0.67, 0.498, 0.498),
            #                       (0.75,  0.000, 0.000),
            (0.83, 0.000, 0.000),
            #                       (0.92,  0.325, 0.325),
            (1.00, 0.000, 0.000)),
        'blue': (
            (0.00, 1.0, 1.0),
            #                      (0.03,  0.624, 0.624),
            (0.08, 1.000, 1.000),
            (0.25, 1.000, 1.000),
            #                       (0.33,  0.498, 0.498),
            (0.42, 0.000, 0.000),
            #                       (0.50,  0.000, 0.000),
            (0.58, 0.000, 0.000),
            (0.67, 0.000, 0.000),
            #                       (0.75,  0.749, 0.749),
            (0.83, 0.000, 0.000),
            #                       (0.92,  0.235, 0.235),
            (1.00, 0.000, 0.000))
    }

    cmap_imos = LinearSegmentedColormap('cmap_imos', pimos)
    cmap_imos.set_bad(color='w', alpha=None)

    return heatmap, extent, cmap_imos
Exemplo n.º 18
0
save_XZ = 1  # 1 to save the strain in XZ plane
save_XY = 1  # 1 to save the strain in XY plane
comment = '_iso' + str(support_threshold)  # should start with _
comment = comment + "_strainrange_" + str(strain_range)
######################################
# define a colormap
cdict = {
    'red': ((0.0, 1.0, 1.0), (0.11, 0.0, 0.0), (0.36, 0.0, 0.0),
            (0.62, 1.0, 1.0), (0.87, 1.0, 1.0), (1.0, 0.0, 0.0)),
    'green': ((0.0, 1.0, 1.0), (0.11, 0.0, 0.0), (0.36, 1.0, 1.0),
              (0.62, 1.0, 1.0), (0.87, 0.0, 0.0), (1.0, 0.0, 0.0)),
    'blue': ((0.0, 1.0, 1.0), (0.11, 1.0, 1.0), (0.36, 1.0, 1.0),
             (0.62, 0.0, 0.0), (0.87, 0.0, 0.0), (1.0, 0.0, 0.0))
}
my_cmap = LinearSegmentedColormap('my_colormap', cdict, 256)
my_cmap.set_bad(color='0.7')


def calc_coordination(mysupport, debugging=0):
    nbz, nby, nbx = mysupport.shape

    mykernel = np.ones((3, 3, 3))
    mycoord = np.rint(convolve(mysupport, mykernel, mode='same'))
    mycoord = mycoord.astype(int)

    if debugging == 1:
        plt.figure(figsize=(18, 15))
        plt.subplot(2, 2, 1)
        plt.imshow(mycoord[:, :, nbx // 2])
        plt.colorbar()
        plt.axis('scaled')
(0.96078431372549,0.757171875,0.757171875),
(0.964705882352941,0.76896484375,0.76896484375),
(0.968627450980392,0.78075390625,0.78075390625),
(0.972549019607843,0.79254296875,0.79254296875),
(0.976470588235294,0.80433203125,0.80433203125),
(0.980392156862745,0.81612109375,0.81612109375),
(0.984313725490196,0.82791015625,0.82791015625),
(0.988235294117647,0.839703125,0.839703125),
(0.992156862745098,0.8514921875,0.8514921875),
(0.996078431372549,0.86328125,0.86328125),
(1,0,0)),
 }


califa = LinearSegmentedColormap('CALIFA', cdict)
califa.set_bad(color='navy')
califa.set_under(color='navy')
califa.set_over(color='navy')
register_cmap(cmap=califa)




#plt.xkcd()
hdus = fits.open('NGC4047.p_e.rad_SFR_lum_Mass.fits.gz')
img = hdus[0].data
fig=plt.figure()
ax1 = fig.add_subplot(111)
#img_mask= np.power(10, img)
img_masked= img
where_are_NaNs = np.isnan(img_masked)
Exemplo n.º 20
0
class ColorPalette(object):
    def __init__(self, name, z0, z1, rgb0, rgb1, resolution=None, nan_color=0, is_log=False):
        """Construct a DataColorMap from input Z values and RGB specs.

        Args:
            name: Name of colormap.
            z0: Sequence of z0 values.
            z1: Sequence of z1 values.
            rgb0: Sequence of RGB triplets (values between 0-255).
            rgb1: Sequence of RGB triplets (values between 0-255).
            resolution: Desired Resolution of the data values in data units.
                For example, the preset population color map has a resolution
                of 1.0, meaning that we want to be able to distinguish between
                color values associated with a difference of 1 person. This
                sets the number of colors to be:
                    `max(256,int((max(z1)-min(z0))/resolution))`
            nan_color: Either 0 or RGBA quadruplet (A is for Alpha, where 0 is
                transparent, and 255 is opaque.)
        """
        # validate that lengths are all identical
        if len(z0) != len(z1) != len(rgb0) != len(rgb1):
            raise ConsistentLengthError('Lengths of input sequences to ColorPalette() '
                                      'must be identical.')
        self._is_log = is_log
        z0 = np.array(z0)
        z1 = np.array(z1)
        self._vmin = z0.min()
        self._vmax = z1.max()
        if isinstance(nan_color, int):
            nan_color = [nan_color] * 4
        self.nan_color = np.array(nan_color) / 255.0

        # Change the z values to be between 0 and 1
        adj_z0 = (z0 - self._vmin) / (self._vmax - self._vmin)
        # should this be z0 - vmin?
        adj_z1 = (z1 - self._vmin) / (self._vmax - self._vmin)

        # loop over the sequences, and construct a dictionary of red, green,
        # blue tuples
        B = -.999 * 255
        # this will mark the y0 value in the first row (isn't used)
        E = .999 * 255
        # this will mark the y1 value in the last row (isn't used)

        # if we add dummy rows to our rgb sequences, we can do one simple loop
        # through.
        rgb0_t = rgb0.copy()
        rgb1_t = rgb1.copy()
        # append a dummy row to the end of RGB0
        rgb0_t.append((E, E, E))
        # prepend a dummy row to the beginning of RGB1
        rgb1_t.insert(0, (B, B, B))
        # Make the column of x values have the same length as the rgb sequences
        x = np.append(adj_z0, adj_z1[-1])

        cdict = {'red': [],
                 'green': [],
                 'blue': []
                 }

        for i in range(0, len(x)):
            red0 = rgb1_t[i][0] / 255.0
            red1 = rgb0_t[i][0] / 255.0
            green0 = rgb1_t[i][1] / 255.0
            green1 = rgb0_t[i][1] / 255.0
            blue0 = rgb1_t[i][2] / 255.0
            blue1 = rgb0_t[i][2] / 255.0
            cdict['red'].append((x[i], red0, red1))
            cdict['green'].append((x[i], green0, green1))
            cdict['blue'].append((x[i], blue0, blue1))

        self._cdict = cdict.copy()
        # choose the number of colors to store the colormap
        # if we choose too low, then there may not be enough colors to
        # accurately capture the resolution of our data.
        # this isn't perfect
        numcolors = DEFAULT_NCOLORS
        if resolution is not None:
            ncolors_tmp = np.ceil((self._vmax - self._vmin) / resolution)
            numcolors = max(DEFAULT_NCOLORS, ncolors_tmp)

        self._cmap = LinearSegmentedColormap(name, cdict, N=numcolors)
        self._cmap.set_bad(self.nan_color)

    @classmethod
    def fromPreset(cls, preset):
        """Construct a ColorPalette from one of several preset color maps.

        Args:
            preset: String to represent one of the preset color maps (see
                getPresets()).

        Returns:
            ColorPalette object.
        """
        if preset not in PALETTES:
            raise UnsupportedArgumentError(
                f'Preset {preset} not in list of supported presets.')
        z0 = PALETTES[preset]['z0'].copy()
        z1 = PALETTES[preset]['z1'].copy()
        rgb0 = PALETTES[preset]['rgb0'].copy()
        rgb1 = PALETTES[preset]['rgb1'].copy()
        nan_color = PALETTES[preset]['nan_color']
        resolution = None
        if 'resolution' in PALETTES[preset]:
            resolution = PALETTES[preset]['resolution']
        return cls(preset, z0=z0, z1=z1, rgb0=rgb0, rgb1=rgb1,
                   nan_color=nan_color, resolution=resolution)

    @classmethod
    def getPresets(cls):
        """Get list of preset color palettes.

        Returns:
            List of strings which can be used with fromPreset() to create a
            ColorPalette.
        """
        return list(PALETTES.keys())

    @classmethod
    def fromFile(cls, filename):
        """Load a ColorPalette from a file.

        ColorPalette files should be formatted as below:
        --------------------------------------------
        #This file is a test file for ColorPalette.
        #Lines beginning with pound signs are comments.
        #Lines beginning with pound signs followed by a "$" are variable
        #definition lines.
        #For example, the following line defines a variable called nan_color.
        #$nan_color: 0,0,0,0
        #$name: test
        #$resolution: 0.01
        Z0 R0  G0  B0  Z1  R1  G1  B1
        0   0   0   0   1  85  85  85
        1  85  85  85   2 170 170 170
        2 170 170 170   3 255 255 255
        --------------------------------------------

        These files contain all the information needed to assign colors to any
        data value. The data values are in the Z0/Z1 columns, the colors
        (0-255) are in the RX/GX/BX columns. In the sample file above, a data
        value of 0.5 would be assigned the color (42.5/255,42.5/255,42.5/255).

        Args:
            filename: String file name pointing to a file formatted as above.

        Returns:
          ColorPalette object.
        """
        nan_color = (0, 0, 0, 0)
        name = 'generic'
        resolution = None
        f = open(filename, 'rt')
        for line in f.readlines():
            tline = line.strip()
            if tline.startswith('#$nan_color'):
                parts = tline[2:].split(':')
                value = parts[1].split(',')
                colortuple = tuple([int(xpi) for xpi in value])
                nan_color = colortuple
            elif tline.startswith('#$name'):
                parts = tline[2:].split(':')
                name = parts[1].strip()
            elif tline.startswith('#$resolution'):
                parts = tline[2:].split(':')
                resolution = float(parts[1].strip())
        f.close()
        df = pd.read_csv(filename, comment='#', sep=r'\s+', header=0)
        rgb0 = list(zip(df.R0, df.G0, df.B0))
        rgb1 = list(zip(df.R1, df.G1, df.B1))
        return cls(name=name, z0=df.Z0, z1=df.Z1, rgb0=rgb0, rgb1=rgb1,
                   nan_color=nan_color, resolution=resolution)

    @classmethod
    def fromColorMap(cls, name, z0, z1, cmap, resolution=None, nan_color=0, is_log=False):
        """Construct a ColorPalette from ranges of Z values and a Matplotlib Colormap.

        Args:
            name (str): Name of Colormap.
            z0 (sequence): Sequence of z0 values.
            z1 (sequence): Sequence of z1 values.
            cmap (Colormap): Matplotlib Colormap object.
            resolution (float): Desired Resolution of the data values in data units.
                For example, the preset population color map has a resolution
                of 1.0, meaning that we want to be able to distinguish between
                color values associated with a difference of 1 person. This
                sets the number of colors to be:
                    `max(256,int((max(z1)-min(z0))/resolution))`
            nan_color (0 or 4 sequence): Either 0 or RGBA quadruplet (A is for Alpha, where 0 is
                transparent, and 255 is opaque.)
        """
        # use the whole dynamic range of the colormap
        if len(z0) != len(z1):
            raise ConsistentLengthError('Lengths of input sequences to '
                                      'ColorPalette.fromColorMap() must be identical.')
        zmin = np.min(z0)
        zmax = np.max(z1)
        rgb0 = []
        rgb1 = []
        for zbottom, ztop in zip(z0, z1):
            znorm0 = (zbottom - zmin) / (zmax - zmin)
            rgb_bottom = np.round(np.array(cmap(znorm0)[0:3]) * 255)
            rgb0.append(rgb_bottom.tolist())

            znorm1 = (ztop - zmin) / (zmax - zmin)
            rgb_top = np.round(np.array(cmap(znorm1)[0:3]) * 255)
            rgb1.append(rgb_top.tolist())

        return cls(name, z0, z1, rgb0, rgb1, resolution=resolution, nan_color=nan_color, is_log=is_log)

    @property
    def vmin(self):
        """Property accessor for vmin.

        Returns:
            Minimum data value for ColorPalette.
        """
        return self._vmin

    @vmin.setter
    def vmin(self, value):
        """Property setter for vmin.

        Args:
            value: Float data value to which vmin should be set.
        """
        self._vmin = value

    @property
    def vmax(self):
        """Property accessor for vmax.

        Returns:
            Maximum data value for ColorPalette.
        """
        return self._vmax

    @vmax.setter
    def vmax(self, value):
        """Property setter for vmax.

        Args:
            value: Float data value to which vmax should be set.
        """
        self._vmax = value

    @property
    def cmap(self):
        """
        Property accessor for the Matplotlib colormap contained within the
        ColorPalette object.

        Returns:
            Matplotlib colormap object.
        """
        return self._cmap

    def getDataColor(self, value, color_format='mlab'):
        """Get the RGB color associated with a given data value.

        Args:
            value: Data value for which color should be retrieved.
            color_format: Output format for color specification.  Choices are:
                - 'mlab' Return a 4 element tuple of (R,G,B,A) with float
                  values between 0 and 1.
                - '255' Return a 4 element tuple of (R,G,B,A) with integer
                  values betwen 0 and 255.
                - 'hex' Return an HTML-style hex color specification (#RRGGBB).
                - 'array' Return an RGBA array of the same 1 or 2D dimensions as value.

        Returns:
            The color value associated with the input data value.

        Raises:
            AttributeError when color_format is not recognized.
        """
        if self._is_log:
            value = np.log(value)
        normvalue = (value - self.vmin) / (self.vmax - self.vmin)
        color = self.cmap(normvalue)
        if color_format == 'mlab':
            return color
        elif color_format == '255':
            color255 = tuple([int(c * 255) for c in color])
            return color255
        elif color_format == 'array':
            rgba = np.uint8(color * 255)
            return rgba
        elif color_format == 'hex':
            color255 = [int(c * 255) for c in color]
            hexcolor = (
                f'#{color255[0]:02x}{color255[1]:02x}{color255[2]:02x}').upper()
            return hexcolor
        else:
            raise AttributeError(
                f'Color format {color_format} is not supported.')
Exemplo n.º 21
0
def draw_map(
    data,
    genome_seq,
    cumcs,
    savefig,
    show,
    one=False,
    clim=None,
    cmap="jet",
    decay=False,
    perc=10,
    name=None,
    cistrans=None,
    decay_resolution=10000,
    normalized=False,
    max_diff=None,
):
    _ = plt.figure(figsize=(15.0, 12.5))
    if not max_diff:
        max_diff = len(data)
    ax1 = plt.axes([0.34, 0.08, 0.6, 0.7205])
    ax2 = plt.axes([0.07, 0.65, 0.21, 0.15])
    if decay:
        ax3 = plt.axes([0.07, 0.42, 0.21, 0.15])
        plot_distance_vs_interactions(
            data, genome_seq=genome_seq, axe=ax3, resolution=decay_resolution, max_diff=max_diff, normalized=normalized
        )
    ax4 = plt.axes([0.34, 0.805, 0.6, 0.04], sharex=ax1)
    ax5 = plt.axes([0.34, 0.845, 0.6, 0.04], sharex=ax1)
    ax6 = plt.axes([0.34, 0.885, 0.6, 0.04], sharex=ax1)
    try:
        minoridata = np.nanmin(data)
        maxoridata = np.nanmax(data)
    except AttributeError:
        vals = [i for d in data for i in d if not np.isnan(i)]
        minoridata = np.min(vals)
        maxoridata = np.max(vals)
    totaloridata = np.nansum([data[i][j] for i in xrange(len(data)) for j in xrange(i, len(data))])
    data = nozero_log(data, np.log2)
    vals = np.array([i for d in data for i in d])
    vals = vals[np.isfinite(vals)]

    mindata = np.nanmin(vals)
    maxdata = np.nanmax(vals)
    diff = maxdata - mindata
    posI = 0.01 if not clim else (float(clim[0]) / diff) if clim[0] != None else 0.01
    posF = 1.0 if not clim else (float(clim[1]) / diff) if clim[1] != None else 1.0
    if cmap == "tadbit":
        cuts = perc
        cdict = {"red": [(0.0, 0.0, 0.0)], "green": [(0.0, 0.0, 0.0)], "blue": [(0.0, 0.5, 0.5)]}
        prev_pos = 0
        median = (np.median(vals) - mindata) / diff
        for prc in np.linspace(posI, median, cuts / 2, endpoint=False):
            try:
                pos = (np.percentile(vals, prc * 100.0) - mindata) / diff
                prc = ((prc - posI) / (median - posI)) + 1.0 / cuts
            except ValueError:
                pos = prc = 0
            if prev_pos >= pos:
                continue
            cdict["red"].append([pos, prc, prc])
            cdict["green"].append([pos, prc, prc])
            cdict["blue"].append([pos, 1, 1])
            prev_pos = pos
        for prc in np.linspace(median + 1.0 / cuts, posF, cuts / 2, endpoint=False):
            try:
                pos = (np.percentile(vals, prc * 100.0) - mindata) / diff
                prc = (prc - median) / (posF - median)
            except ValueError:
                pos = prc = 0
            if prev_pos >= pos:
                continue
            cdict["red"].append([pos, 1.0, 1.0])
            cdict["green"].append([pos, 1 - prc, 1 - prc])
            cdict["blue"].append([pos, 1 - prc, 1 - prc])
            prev_pos = pos
        pos = (np.percentile(vals, 97.0) - mindata) / diff
        cdict["red"].append([pos, 0.1, 0.1])
        cdict["green"].append([pos, 0, 0])
        cdict["blue"].append([pos, 0, 0])

        cdict["red"].append([1.0, 1, 1])
        cdict["green"].append([1.0, 1, 1])
        cdict["blue"].append([1.0, 0, 0])
        cmap = LinearSegmentedColormap(cmap, cdict)
        clim = None
    else:
        cmap = plt.get_cmap(cmap)
    cmap.set_bad("darkgrey", 1)

    ax1.imshow(data, interpolation="none", cmap=cmap, vmin=clim[0] if clim else None, vmax=clim[1] if clim else None)
    size = len(data)
    for i in xrange(size):
        for j in xrange(i, size):
            if np.isnan(data[i][j]):
                data[i][j] = 0
                data[j][i] = 0
            # data[j][i] = data[i][j]
    evals, evect = eigh(data)
    sort_perm = evals.argsort()
    evect = evect[sort_perm]
    data = [i for d in data for i in d if not np.isnan(i)]
    gradient = np.linspace(np.nanmin(data), np.nanmax(data), size)
    gradient = np.vstack((gradient, gradient))
    h = ax2.hist(data, color="darkgrey", linewidth=2, bins=20, histtype="step", normed=True)
    _ = ax2.imshow(gradient, aspect="auto", cmap=cmap, extent=(np.nanmin(data), np.nanmax(data), 0, max(h[0])))
    if genome_seq:
        for crm in genome_seq:
            ax1.vlines(
                [cumcs[crm][0] - 0.5, cumcs[crm][1] - 0.5],
                cumcs[crm][0] - 0.5,
                cumcs[crm][1] - 0.5,
                color="w",
                linestyle="-",
                linewidth=1,
                alpha=1,
            )
            ax1.hlines(
                [cumcs[crm][1] - 0.5, cumcs[crm][0] - 0.5],
                cumcs[crm][0] - 0.5,
                cumcs[crm][1] - 0.5,
                color="w",
                linestyle="-",
                linewidth=1,
                alpha=1,
            )
            ax1.vlines(
                [cumcs[crm][0] - 0.5, cumcs[crm][1] - 0.5],
                cumcs[crm][0] - 0.5,
                cumcs[crm][1] - 0.5,
                color="k",
                linestyle="--",
            )
            ax1.hlines(
                [cumcs[crm][1] - 0.5, cumcs[crm][0] - 0.5],
                cumcs[crm][0] - 0.5,
                cumcs[crm][1] - 0.5,
                color="k",
                linestyle="--",
            )
        if not one:
            vals = [0]
            keys = [""]
            for crm in genome_seq:
                vals.append(cumcs[crm][0])
                keys.append(crm)
            vals.append(cumcs[crm][1])
            ax1.set_yticks(vals)
            ax1.set_yticklabels("")
            ax1.set_yticks([float(vals[i] + vals[i + 1]) / 2 for i in xrange(len(vals) - 1)], minor=True)
            ax1.set_yticklabels(keys, minor=True)
            for t in ax1.yaxis.get_minor_ticks():
                t.tick1On = False
                t.tick2On = False
    # totaloridata = ''.join([j + ('' if (i+1)%3 else ',') for i, j in enumerate(str(totaloridata)[::-1])])[::-1].strip(',')
    # minoridata = ''.join([j + ('' if (i+1)%3 else ',') for i, j   in enumerate(str(minoridata)[::-1])])[::-1].strip(',')
    # maxoridata = ''.join([j + ('' if (i+1)%3 else ',') for i, j   in enumerate(str(maxoridata)[::-1])])[::-1].strip(',')
    plt.figtext(
        0.05,
        0.25,
        "".join(
            [
                (name + "\n") if name else "",
                "Number of interactions: %s\n" % str(totaloridata),
                ("" if np.isnan(cistrans) else ("Percentage of cis interactions: %.0f%%\n" % (cistrans * 100))),
                "Min interactions: %s\n" % (minoridata),
                "Max interactions: %s\n" % (maxoridata),
            ]
        ),
    )
    ax2.set_xlim((np.nanmin(data), np.nanmax(data)))
    ax2.set_ylim((0, max(h[0])))
    ax1.set_xlim((-0.5, size - 0.5))
    ax1.set_ylim((-0.5, size - 0.5))
    ax2.set_xlabel("log interaction count")
    # we reduce the number of dots displayed.... we just want to see the shape
    subdata = np.array(list(set([float(int(d * 100)) / 100 for d in data])))
    try:
        normfit = sc_norm.pdf(subdata, np.nanmean(data), np.nanstd(data))
    except AttributeError:
        normfit = sc_norm.pdf(subdata, np.mean(data), np.std(data))
    ax2.plot(subdata, normfit, "w.", markersize=2.5, alpha=0.4)
    ax2.plot(subdata, normfit, "k.", markersize=1.5, alpha=1)
    ax2.set_title("skew: %.3f, kurtosis: %.3f" % (skew(data), kurtosis(data)))
    ax4.vlines(range(size), 0, evect[:, -1], color="k")
    ax4.hlines(0, 0, size, color="red")
    ax4.set_ylabel("E1")
    ax4.set_yticklabels([])
    try:
        ax5.vlines(range(size), 0, evect[:, -2], color="k")
    except IndexError:
        pass
    ax5.hlines(0, 0, size, color="red")
    ax5.set_ylabel("E2")
    ax5.set_yticklabels([])
    try:
        ax6.vlines(range(size), 0, evect[:, -3], color="k")
    except IndexError:
        pass
    ax6.hlines(0, 0, size, color="red")
    ax6.set_ylabel("E3")
    ax6.set_yticklabels([])
    xticklabels = ax4.get_xticklabels() + ax5.get_xticklabels() + ax6.get_xticklabels()
    plt.setp(xticklabels, visible=False)
    if savefig:
        tadbit_savefig(savefig)
    elif show:
        plt.show()
    plt.close("all")
Exemplo n.º 22
0
    h_vals = np.linspace(blue_lch[2], red_lch[2], nsteps)
    for pos, l, c, h in zip(np.linspace(0, 1, nsteps), l_vals, c_vals, h_vals):
        lch = [l, c, h]
        rgb = lch2rgb(lch)
        reds.append((pos, rgb[0], rgb[0]))
        greens.append((pos, rgb[1], rgb[1]))
        blues.append((pos, rgb[2], rgb[2]))
        alphas.append((pos, 1.0, 1.0))

    red_blue = LinearSegmentedColormap('red_blue', {
        "red": reds,
        "green": greens,
        "blue": blues,
        "alpha": alphas
    })
    red_blue.set_bad(gray_rgb, 1.0)
    red_blue.set_over(gray_rgb, 1.0)
    red_blue.set_under(
        gray_rgb, 1.0
    )  # "under" is incorrectly used instead of "bad" in the scatter plot

    red_blue_no_bounds = LinearSegmentedColormap('red_blue_no_bounds', {
        "red": reds,
        "green": greens,
        "blue": blues,
        "alpha": alphas
    })

    red_blue_transparent = LinearSegmentedColormap(
        'red_blue_no_bounds', {
            "red": reds,
Exemplo n.º 23
0
def draw_map(data, genome_seq, cumcs, savefig, show, one=False, clim=None,
             cmap='jet', decay=False, perc=10, name=None, cistrans=None,
             decay_resolution=10000, normalized=False, max_diff=None):
    _ = plt.figure(figsize=(15.,12.5))
    if not max_diff:
        max_diff = len(data)
    ax1 = plt.axes([0.34, 0.08, 0.6, 0.7205])
    ax2 = plt.axes([0.07, 0.65, 0.21, 0.15])
    if decay:
        ax3 = plt.axes([0.07, 0.42, 0.21, 0.15])
        plot_distance_vs_interactions(data, genome_seq=genome_seq, axe=ax3,
                                      resolution=decay_resolution,
                                      max_diff=max_diff, normalized=normalized)
    ax4 = plt.axes([0.34, 0.805, 0.6, 0.04], sharex=ax1)
    ax5 = plt.axes([0.34, 0.845, 0.6, 0.04], sharex=ax1)
    ax6 = plt.axes([0.34, 0.885, 0.6, 0.04], sharex=ax1)
    try:
        minoridata   = np.nanmin(data)
        maxoridata   = np.nanmax(data)
    except AttributeError:
        vals = [i for d in data for i in d if not np.isnan(i)]
        minoridata   = np.min(vals)
        maxoridata   = np.max(vals)
    totaloridata = np.nansum([data[i][j] for i in xrange(len(data))
                              for j in xrange(i, len(data))])
    data = nozero_log(data, np.log2)
    vals = np.array([i for d in data for i in d])
    vals = vals[np.isfinite(vals)]

    mindata = np.nanmin(vals)
    maxdata = np.nanmax(vals)
    diff = maxdata - mindata
    posI = 0.01 if not clim else (float(clim[0]) / diff) if clim[0] != None else 0.01
    posF = 1.0  if not clim else (float(clim[1]) / diff) if clim[1] != None else 1.0
    if cmap == 'tadbit':
        cuts = perc
        cdict = {'red'  : [(0.0,  0.0, 0.0)],
                 'green': [(0.0,  0.0, 0.0)],
                 'blue' : [(0.0,  0.5, 0.5)]}
        prev_pos  = 0
        median = (np.median(vals) - mindata) / diff
        for prc in np.linspace(posI, median, cuts / 2, endpoint=False):
            try:
                pos = (np.percentile(vals, prc * 100.) - mindata) / diff
                prc = ((prc - posI) / (median - posI)) + 1. / cuts
            except ValueError:
                pos = prc = 0
            if prev_pos >= pos:
                continue
            cdict['red'  ].append([pos, prc, prc])
            cdict['green'].append([pos, prc, prc])
            cdict['blue' ].append([pos, 1, 1])
            prev_pos  = pos
        for prc in np.linspace(median + 1. / cuts, posF, cuts / 2, endpoint=False):
            try:
                pos = (np.percentile(vals, prc * 100.) - mindata) / diff
                prc = ((prc - median) / (posF - median))
            except ValueError:
                pos = prc = 0
            if prev_pos >= pos:
                continue
            cdict['red'  ].append([pos, 1.0, 1.0])
            cdict['green'].append([pos, 1 - prc, 1 - prc])
            cdict['blue' ].append([pos, 1 - prc, 1 - prc])
            prev_pos  = pos
        pos = (np.percentile(vals ,97.) - mindata) / diff
        cdict['red'  ].append([pos, 0.1, 0.1])
        cdict['green'].append([pos, 0, 0])
        cdict['blue' ].append([pos, 0, 0])

        cdict['red'  ].append([1.0, 1, 1])
        cdict['green'].append([1.0, 1, 1])
        cdict['blue' ].append([1.0, 0, 0])
        cmap  = LinearSegmentedColormap(cmap, cdict)
        clim = None
    else:
        cmap = plt.get_cmap(cmap)
    cmap.set_bad('darkgrey', 1)

    ax1.imshow(data, interpolation='none',
               cmap=cmap, vmin=clim[0] if clim else None, vmax=clim[1] if clim else None)
    size = len(data)
    for i in xrange(size):
        for j in xrange(i, size):
            if np.isnan(data[i][j]):
                data[i][j] = 0
                data[j][i] = 0
            #data[j][i] = data[i][j]
    evals, evect = eigh(data)
    sort_perm = evals.argsort()
    evect = evect[sort_perm]
    data = [i for d in data for i in d if not np.isnan(i)]
    gradient = np.linspace(np.nanmin(data),
                           np.nanmax(data), size)
    gradient = np.vstack((gradient, gradient))
    h  = ax2.hist(data, color='darkgrey', linewidth=2,
                  bins=20, histtype='step', normed=True)
    _  = ax2.imshow(gradient, aspect='auto', cmap=cmap,
                    extent=(np.nanmin(data), np.nanmax(data) , 0, max(h[0])))
    if genome_seq:
        for crm in genome_seq:
            ax1.vlines([cumcs[crm][0]-.5, cumcs[crm][1]-.5], cumcs[crm][0]-.5, cumcs[crm][1]-.5,
                       color='w', linestyle='-', linewidth=1, alpha=1)
            ax1.hlines([cumcs[crm][1]-.5, cumcs[crm][0]-.5], cumcs[crm][0]-.5, cumcs[crm][1]-.5,
                       color='w', linestyle='-', linewidth=1, alpha=1)
            ax1.vlines([cumcs[crm][0]-.5, cumcs[crm][1]-.5], cumcs[crm][0]-.5, cumcs[crm][1]-.5,
                       color='k', linestyle='--')
            ax1.hlines([cumcs[crm][1]-.5, cumcs[crm][0]-.5], cumcs[crm][0]-.5, cumcs[crm][1]-.5,
                       color='k', linestyle='--')
        if not one:
            vals = [0]
            keys = ['']
            for crm in genome_seq:
                vals.append(cumcs[crm][0])
                keys.append(crm)
            vals.append(cumcs[crm][1])
            ax1.set_yticks(vals)
            ax1.set_yticklabels('')
            ax1.set_yticks([float(vals[i]+vals[i+1])/2
                            for i in xrange(len(vals) - 1)], minor=True)
            ax1.set_yticklabels(keys, minor=True)
            for t in ax1.yaxis.get_minor_ticks():
                t.tick1On = False
                t.tick2On = False
    # totaloridata = ''.join([j + ('' if (i+1)%3 else ',') for i, j in enumerate(str(totaloridata)[::-1])])[::-1].strip(',')
    # minoridata = ''.join([j + ('' if (i+1)%3 else ',') for i, j   in enumerate(str(minoridata)[::-1])])[::-1].strip(',')
    # maxoridata = ''.join([j + ('' if (i+1)%3 else ',') for i, j   in enumerate(str(maxoridata)[::-1])])[::-1].strip(',')
    plt.figtext(0.05,0.25, ''.join([
        (name + '\n') if name else '',
        'Number of interactions: %s\n' % str(totaloridata),
        ('' if np.isnan(cistrans) else
         ('Percentage of cis interactions: %.0f%%\n' % (cistrans*100))),
        'Min interactions: %s\n' % (minoridata),
        'Max interactions: %s\n' % (maxoridata)]))
    ax2.set_xlim((np.nanmin(data), np.nanmax(data)))
    ax2.set_ylim((0, max(h[0])))
    ax1.set_xlim ((-0.5, size - .5))
    ax1.set_ylim ((-0.5, size - .5))
    ax2.set_xlabel('log interaction count')
    # we reduce the number of dots displayed.... we just want to see the shape
    subdata = np.array(list(set([float(int(d*100))/100 for d in data])))
    try:
        normfit = sc_norm.pdf(subdata, np.nanmean(data), np.nanstd(data))
    except AttributeError:
        normfit = sc_norm.pdf(subdata, np.mean(data), np.std(data))
    ax2.plot(subdata, normfit, 'w.', markersize=2.5, alpha=.4)
    ax2.plot(subdata, normfit, 'k.', markersize=1.5, alpha=1)
    ax2.set_title('skew: %.3f, kurtosis: %.3f' % (skew(data),
                                                   kurtosis(data)))
    ax4.vlines(range(size), 0, evect[:,-1], color='k')
    ax4.hlines(0, 0, size, color='red')
    ax4.set_ylabel('E1')
    ax4.set_yticklabels([])
    try:
        ax5.vlines(range(size), 0, evect[:,-2], color='k')
    except IndexError:
        pass
    ax5.hlines(0, 0, size, color='red')
    ax5.set_ylabel('E2')
    ax5.set_yticklabels([])
    try:
        ax6.vlines(range(size), 0, evect[:,-3], color='k')
    except IndexError:
        pass
    ax6.hlines(0, 0, size, color='red')
    ax6.set_ylabel('E3')
    ax6.set_yticklabels([])
    xticklabels = ax4.get_xticklabels() + ax5.get_xticklabels() + ax6.get_xticklabels()
    plt.setp(xticklabels, visible=False)
    if savefig:
        tadbit_savefig(savefig)
    elif show:
        plt.show()
    plt.close('all')
Exemplo n.º 24
0
            ((0.0, 229. / 255, 229. / 255), (1.0, 87. / 255, 87. / 255)),
            'alpha': ((0.0, 1, 1), (0.5, 0.3, 0.3), (1.0, 1, 1))
        })
    #red_blue.set_bad("#777777")

    red_blue_solid = LinearSegmentedColormap(
        'red_blue_solid', {
            'red':
            ((0.0, 30. / 255, 30. / 255), (1.0, 255. / 255, 255. / 255)),
            'green':
            ((0.0, 136. / 255, 136. / 255), (1.0, 13. / 255, 13. / 255)),
            'blue':
            ((0.0, 229. / 255, 229. / 255), (1.0, 87. / 255, 87. / 255)),
            'alpha': ((0.0, 1, 1), (0.5, 1, 1), (1.0, 1, 1))
        })
    red_blue_solid.set_bad("#777777", 1.0)
    red_blue_solid.set_over("#777777", 1.0)
    red_blue_solid.set_under(
        "#777777", 1.0
    )  # "under" is incorrectly used instead of "bad" in the scatter plot

    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))
    red_transparent_blue = LinearSegmentedColormap.from_list(
        "red_transparent_blue", colors)

    colors = []
    for l in np.linspace(0, 1, 100):
    def test_problem_16(self):
        """
        Schittkowski problem #16
        """
        cost = Problem16_Cost ()
        problem = roboptim.core.PyProblem (cost)
        problem.startingPoint = numpy.array([-2, 1., ])
        problem.argumentBounds = numpy.array([[-2., 0.5],
                                              [-float("inf"), 1.]])

        g1 = Problem16_G1 ()
        problem.addConstraint (g1, [0., float("inf"),])
        g2 = Problem16_G2 ()
        problem.addConstraint (g2, [0., float("inf"),])

        # Check starting value
        numpy.testing.assert_almost_equal (cost (problem.startingPoint)[0], 909.)

        # Initialize callback
        callback = IterCallback (problem)

        # Let the test fail if the solver does not exist.
        try:
            # Create solver
            log_dir = "/tmp/roboptim-core-python/problem_16"
            solver = roboptim.core.PySolver ("ipopt", problem, log_dir = log_dir)

            # Add callback
            solver.addIterationCallback(callback)

            print (solver)
            solver.solve ()
            r = solver.minimum ()
            print (r)

            # Plot results
            plotter = Plotter2D([-2.1,0.6],[0,1.1])
            plotter.x_res = 100
            plotter.y_res = 100
            plotter.plot(cost, plot_style = PlotStyle2D.PColorMesh, vmax=10,
                         norm=LogNorm())

            # Set up a colormap:
            cdict = {'red':   ((0.0, 0.0, 0.0),
                               (1.0, 0.0, 0.0)),

                     '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)),

                     'alpha': ((0.0, 0.0, 0.0),
                               (1.0, 1.0, 1.0))
                    }
            cstr_cmap = LinearSegmentedColormap('Mask', cdict)
            cstr_cmap.set_under('r', alpha=0)
            cstr_cmap.set_over('w', alpha=0)
            cstr_cmap.set_bad('g', alpha=0)

            plotter.plot(g1, plot_style=PlotStyle2D.Contourf,
                         linewidth=10, alpha=None,
                         cmap=cstr_cmap, vmax=0, fontsize=20)
            plotter.plot(g1, plot_style=PlotStyle2D.Contour,
                         linewidth=10, alpha=None, levels=[0],
                         vmax=0, fontsize=20, colors="k")
            plotter.plot(g2, plot_style=PlotStyle2D.Contourf,
                         linewidth=10, alpha=None,
                         cmap=cstr_cmap, vmax=0)

            # Print iterations
            X = zip(*callback.x)[0]
            Y = zip(*callback.x)[1]
            # Show evolution
            plotter.add_marker(X, Y,
                               color="white", marker=".", markersize=5)
            # First point
            plotter.add_marker(X[0], Y[0],
                               color="white", marker="o", markersize=10, markeredgewidth=2)
            # Final result
            plotter.add_marker(X[-1], Y[-1],
                               color="white", marker="s", markersize=10, markeredgewidth=2)

            # Print actual global minimum
            plotter.add_marker(0.5, 0.25,
                               color="black", marker="x", markersize=14, markeredgewidth=6)
            plotter.add_marker(0.5, 0.25,
                               color="white", marker="x", markersize=10, markeredgewidth=3)

            plotter.show()

        except Exception as e:
            print ("Error: %s" % e)
Exemplo n.º 26
0
# Continuous 2
reds = [0.26, 0.26, 0.26, 0.39, 0.69, 1, 1, 1, 1, 1, 1]
greens_half = [0.26, 0.16, 0.09, 0.26, 0.69]
colordict = {
    'red':
    tuple([(0.1 * i, r, r) for i, r in enumerate(reds)]),
    'green':
    tuple([
        (0.1 * i, r, r)
        for i, r in enumerate(greens_half + [1] + list(reversed(greens_half)))
    ]),
    'blue':
    tuple([(0.1 * i, r, r) for i, r in enumerate(reversed(reds))])
}
CMAP_ASSOCIATION = LinearSegmentedColormap('association', colordict)
CMAP_ASSOCIATION.set_bad(C_BAD)

# Categorical
CMAP_CATEGORICAL = Paired
CMAP_CATEGORICAL_2 = Dark2
CMAP_CATEGORICAL.set_bad(C_BAD)

# Binary
CMAP_BINARY = ListedColormap(['#cdcdcd', '#404040'])
CMAP_BINARY.set_bad(C_BAD)

DPI = 100


# ======================================================================================================================
# Functions
Exemplo n.º 27
0
def draw_map(data, genome_seq, cumcs, savefig, show, one=False, clim=None,
             cmap='jet', decay=False, perc=10, name=None, cistrans=None,
             decay_resolution=10000, normalized=False, max_diff=None):
    _ = plt.figure(figsize=(15.,12.5))
    if not max_diff:
        max_diff = len(data)
    ax1 = plt.axes([0.34, 0.08, 0.6, 0.7205])
    ax2 = plt.axes([0.07, 0.65, 0.21, 0.15])
    if decay:
        ax3 = plt.axes([0.07, 0.42, 0.21, 0.15])
        plot_distance_vs_interactions(data, genome_seq=genome_seq, axe=ax3,
                                      resolution=decay_resolution,
                                      max_diff=max_diff, normalized=normalized)
    ax4 = plt.axes([0.34, 0.805, 0.6, 0.04], sharex=ax1)
    ax5 = plt.axes([0.34, 0.845, 0.6, 0.04], sharex=ax1)
    ax6 = plt.axes([0.34, 0.885, 0.6, 0.04], sharex=ax1)
    try:
        minoridata   = np.nanmin(data)
        maxoridata   = np.nanmax(data)
    except AttributeError:
        vals = [i for d in data for i in d if not np.isnan(i)]
        minoridata   = np.min(vals)
        maxoridata   = np.max(vals)
    totaloridata = np.nansum([data[i][j] for i in xrange(len(data))
                              for j in xrange(i, len(data[i]))]) # may not be square
    data = nozero_log(data, np.log2)
    vals = np.array([i for d in data for i in d])
    vals = vals[np.isfinite(vals)]

    mindata = np.nanmin(vals)
    maxdata = np.nanmax(vals)
    diff = maxdata - mindata
    posI = 0.01 if not clim else (float(clim[0]) / diff) if clim[0] != None else 0.01
    posF = 1.0  if not clim else (float(clim[1]) / diff) if clim[1] != None else 1.0
    if cmap == 'tadbit':
        cuts = perc
        cdict = {'red'  : [(0.0,  0.0, 0.0)],
                 'green': [(0.0,  0.0, 0.0)],
                 'blue' : [(0.0,  0.5, 0.5)]}
        prev_pos  = 0
        median = (np.median(vals) - mindata) / diff
        for prc in np.linspace(posI, median, cuts / 2, endpoint=False):
            try:
                pos = (np.percentile(vals, prc * 100.) - mindata) / diff
                prc = ((prc - posI) / (median - posI)) + 1. / cuts
            except ValueError:
                pos = prc = 0
            if prev_pos >= pos:
                continue
            cdict['red'  ].append([pos, prc, prc])
            cdict['green'].append([pos, prc, prc])
            cdict['blue' ].append([pos, 1, 1])
            prev_pos  = pos
        for prc in np.linspace(median + 1. / cuts, posF, cuts / 2, endpoint=False):
            try:
                pos = (np.percentile(vals, prc * 100.) - mindata) / diff
                prc = ((prc - median) / (posF - median))
            except ValueError:
                pos = prc = 0
            if prev_pos >= pos:
                continue
            cdict['red'  ].append([pos, 1.0, 1.0])
            cdict['green'].append([pos, 1 - prc, 1 - prc])
            cdict['blue' ].append([pos, 1 - prc, 1 - prc])
            prev_pos  = pos
        pos = (np.percentile(vals ,97.) - mindata) / diff
        cdict['red'  ].append([pos, 0.1, 0.1])
        cdict['green'].append([pos, 0, 0])
        cdict['blue' ].append([pos, 0, 0])

        cdict['red'  ].append([1.0, 1, 1])
        cdict['green'].append([1.0, 1, 1])
        cdict['blue' ].append([1.0, 0, 0])
        cmap  = LinearSegmentedColormap(cmap, cdict)
        clim = None
    else:
        cmap = plt.get_cmap(cmap)
    cmap.set_bad('darkgrey', 1)

    ax1.imshow(data, interpolation='none',
               cmap=cmap, vmin=clim[0] if clim else None, vmax=clim[1] if clim else None)
    size1 = len(data)
    size2 = len(data[0])
    if size1 == size2:
        for i in xrange(size1):
            for j in xrange(i, size2):
                if np.isnan(data[i][j]):
                    data[i][j] = 0
                    data[j][i] = 0
    else:
        for i in xrange(size1):
            for j in xrange(size2):
                if np.isnan(data[i][j]):
                    data[i][j] = 0
            #data[j][i] = data[i][j]
    try:
        evals, evect = eigh(data)
        sort_perm = evals.argsort()
        evect = evect[sort_perm]
    except:
        evals, evect = None, None
    data = [i for d in data for i in d if not np.isnan(i)]
    gradient = np.linspace(np.nanmin(data),
                           np.nanmax(data), max(size1, size2))
    gradient = np.vstack((gradient, gradient))
    h  = ax2.hist(data, color='darkgrey', linewidth=2,
                  bins=20, histtype='step', normed=True)
    _  = ax2.imshow(gradient, aspect='auto', cmap=cmap,
                    extent=(np.nanmin(data), np.nanmax(data) , 0, max(h[0])))
    if genome_seq:
        for crm in genome_seq:
            ax1.vlines([cumcs[crm][0]-.5, cumcs[crm][1]-.5], cumcs[crm][0]-.5, cumcs[crm][1]-.5,
                       color='w', linestyle='-', linewidth=1, alpha=1)
            ax1.hlines([cumcs[crm][1]-.5, cumcs[crm][0]-.5], cumcs[crm][0]-.5, cumcs[crm][1]-.5,
                       color='w', linestyle='-', linewidth=1, alpha=1)
            ax1.vlines([cumcs[crm][0]-.5, cumcs[crm][1]-.5], cumcs[crm][0]-.5, cumcs[crm][1]-.5,
                       color='k', linestyle='--')
            ax1.hlines([cumcs[crm][1]-.5, cumcs[crm][0]-.5], cumcs[crm][0]-.5, cumcs[crm][1]-.5,
                       color='k', linestyle='--')
        if not one:
            vals = [0]
            keys = ['']
            for crm in genome_seq:
                vals.append(cumcs[crm][0])
                keys.append(crm)
            vals.append(cumcs[crm][1])
            ax1.set_yticks(vals)
            ax1.set_yticklabels('')
            ax1.set_yticks([float(vals[i]+vals[i+1])/2
                            for i in xrange(len(vals) - 1)], minor=True)
            ax1.set_yticklabels(keys, minor=True)
            for t in ax1.yaxis.get_minor_ticks():
                t.tick1On = False
                t.tick2On = False
    # totaloridata = ''.join([j + ('' if (i+1)%3 else ',') for i, j in enumerate(str(totaloridata)[::-1])])[::-1].strip(',')
    # minoridata = ''.join([j + ('' if (i+1)%3 else ',') for i, j   in enumerate(str(minoridata)[::-1])])[::-1].strip(',')
    # maxoridata = ''.join([j + ('' if (i+1)%3 else ',') for i, j   in enumerate(str(maxoridata)[::-1])])[::-1].strip(',')
    plt.figtext(0.05,0.25, ''.join([
        (name + '\n') if name else '',
        'Number of interactions: %s\n' % str(totaloridata),
        ('' if np.isnan(cistrans) else
         ('Percentage of cis interactions: %.0f%%\n' % (cistrans*100))),
        'Min interactions: %s\n' % (minoridata),
        'Max interactions: %s\n' % (maxoridata)]))
    ax2.set_xlim((np.nanmin(data), np.nanmax(data)))
    ax2.set_ylim((0, max(h[0])))
    ax1.set_xlim ((-0.5, size1 - .5))
    ax1.set_ylim ((-0.5, size2 - .5))
    ax2.set_xlabel('log interaction count')
    # we reduce the number of dots displayed.... we just want to see the shape
    subdata = np.array(list(set([float(int(d*100))/100 for d in data])))
    try:
        normfit = sc_norm.pdf(subdata, np.nanmean(data), np.nanstd(data))
    except AttributeError:
        normfit = sc_norm.pdf(subdata, np.mean(data), np.std(data))
    ax2.plot(subdata, normfit, 'w.', markersize=2.5, alpha=.4)
    ax2.plot(subdata, normfit, 'k.', markersize=1.5, alpha=1)
    ax2.set_title('skew: %.3f, kurtosis: %.3f' % (skew(data),
                                                   kurtosis(data)))
    try: 
        ax4.vlines(range(size1), 0, evect[:,-1], color='k')
    except (TypeError, IndexError):
        pass
    ax4.hlines(0, 0, size2, color='red')
    ax4.set_ylabel('E1')
    ax4.set_yticklabels([])
    try:
        ax5.vlines(range(size1), 0, evect[:,-2], color='k')
    except (TypeError, IndexError):
        pass
    ax5.hlines(0, 0, size2, color='red')
    ax5.set_ylabel('E2')
    ax5.set_yticklabels([])
    try:
        ax6.vlines(range(size1), 0, evect[:,-3], color='k')
    except (TypeError, IndexError):
        pass
    ax6.hlines(0, 0, size2, color='red')
    ax6.set_ylabel('E3')
    ax6.set_yticklabels([])
    xticklabels = ax4.get_xticklabels() + ax5.get_xticklabels() + ax6.get_xticklabels()
    plt.setp(xticklabels, visible=False)
    if savefig:
        tadbit_savefig(savefig)
    elif show:
        plt.show()
    plt.close('all')
Exemplo n.º 28
0
    def run (self) :
        cuda.init()
        self.cuda_dev = cuda.Device(0)
        self.cuda_context = self.cuda_dev.make_context()

#        print 'Loading matrix...'
#        npz = np.load(self.MATRIX_FILE)
#        station_coords = npz['station_coords']
#        grid_dim       = npz['grid_dim']
#        matrix         = npz['matrix']
        station_coords  = self.station_coords
        grid_dim        = self.grid_dim
        matrix          = self.matrix
        nearby_stations = self.nearby_stations    

        # EVERYTHING SHOULD BE IN FLOAT32 for ease of debugging. even times.
        # Matrix and others should be textures, arrays, or in constant memory, to do cacheing.
        
        # make matrix symmetric before converting to int32. this avoids halving the pseudo-infinity value.
        matrix = (matrix + matrix.T) / 2
       #print np.where(matrix == np.inf)
        matrix[matrix == np.inf] = 99999999
        # nan == nan is False because any operation involving nan is False !
        # must use specific isnan function. however, inf works like a normal number.
        matrix[np.isnan(matrix)] = 99999999
        #matrix[matrix >= 60 * 60 * 3] = 0
        matrix = matrix.astype(np.int32)
        #matrix += 60 * 5
        #matrix = np.nan_to_num(matrix)
        print matrix
        
        # force OD matrix symmetry for test
        # THIS was responsible for the coordinate drift!!!
        # need to symmetrize it before copy to device
#        matrix = (matrix + matrix.T) / 2
#        print matrix

        #print np.any(matrix == np.nan)
        #print np.any(matrix == np.inf)

        station_coords_int = station_coords.round().astype(np.int32)

        # to be removed when textures are working
        station_coords_gpu = gpuarray.to_gpu(station_coords_int)
        matrix_gpu = gpuarray.to_gpu(matrix)

        max_x, max_y = grid_dim
        n_gridpoints = int(max_x * max_y)
        n_stations   = len(station_coords)

        cuda_grid_shape = ( int( math.ceil( float(max_x)/CUDA_BLOCK_SHAPE[0] ) ), int( math.ceil( float(max_y)/CUDA_BLOCK_SHAPE[1] ) ) )

        print "\n----PARAMETERS----"
        print "Input file:            ", self.MATRIX_FILE
        print "Number of stations:    ", n_stations
        print "OD matrix shape:       ", matrix.shape    
        print "Station coords shape:  ", station_coords_int.shape 
        print "Station cache size:    ", self.N_NEARBY_STATIONS
        print "Map dimensions:        ", grid_dim
        print "Number of map points:  ", n_gridpoints
        print "Target space dimensionality: ", self.DIMENSIONS
        print "CUDA block dimensions: ", CUDA_BLOCK_SHAPE 
        print "CUDA grid dimensions:  ", cuda_grid_shape

        assert station_coords.shape == (n_stations, 2)
        assert self.N_NEARBY_STATIONS <= n_stations
        
        #sys.exit()

        # Make and register custom color map for pylab graphs

        cdict = {'red':   ((0.0,  0.0, 0.0),
                           (0.2,  0.0, 0.0),
                           (0.4,  0.9, 0.9),
                           (1.0,  0.0, 0.0)),

                 'green': ((0.0,  0.0, 0.1),
                           (0.05, 0.9, 0.9),
                           (0.1,  0.0, 0.0),
                           (0.4,  0.9, 0.9),
                           (0.6,  0.0, 0.0),
                           (1.0,  0.0, 0.0)),

                 'blue':  ((0.0,  0.0, 0.0),
                           (0.05, 0.0, 0.0),
                           (0.2,  0.9, 0.9),
                           (0.3,  0.0, 0.0),
                           (1.0,  0.0, 0.0))}

        mymap = LinearSegmentedColormap('mymap', cdict)
        mymap.set_over( (1.0, 0.0, 1.0) )
        mymap.set_bad ( (0.0, 0.0, 0.0) )
        #pl.plt.register_cmap(cmap=mymap)

        # set up arrays for calculations

        coords_gpu        = gpuarray.to_gpu(np.random.random( (max_x, max_y, self.DIMENSIONS) ).astype(np.float32))   # initialize coordinates to random values in range 0...1
        forces_gpu        = gpuarray.zeros( (int(max_x), int(max_y), self.DIMENSIONS), dtype=np.float32 )             # 3D float32 accumulate forces over one timestep
        weights_gpu       = gpuarray.zeros( (int(max_x), int(max_y)),                  dtype=np.float32 )             # 2D float32 cell error accumulation
        errors_gpu        = gpuarray.zeros( (int(max_x), int(max_y)),                  dtype=np.float32 )             # 2D float32 cell error accumulation
        #near_stations_gpu = gpuarray.zeros( (int(max_x), int(max_y), self.N_NEARBY_STATIONS, 2), dtype=np.int32)
        # instead of using synthetic distances, use the network distance near stations lists.         
        # rather than copying the array over to the GPU then binding to external texref, 
        # could just use matrix_to_texref, but this function only seems to understand 2d arrays
        near_stations_gpu = gpuarray.to_gpu( nearby_stations )

        debug_gpu     = gpuarray.zeros( n_gridpoints, dtype = np.int32 )
        debug_img_gpu = gpuarray.zeros_like( errors_gpu )

        print "\n----COMPILATION----"
        # times could be merged into forces kernel, if done by pixel not station.
        # integrate kernel could be GPUArray operation; also helps clean up code by using GPUArrays.
        # DIM should be replaced by python script, so as not to define twice. 

        replacements = [( 'N_NEAR_STATIONS', self.N_NEARBY_STATIONS ),
                        ( 'N_STATIONS',      n_stations ),
                        ( 'DIM',             self.DIMENSIONS)]
        src = preprocess_cu('unified_mds_stochastic.cu', replacements)
        #print src
        mod = SourceModule(src, options=["--ptxas-options=-v"])
        stations_kernel  = mod.get_function("stations"  )
        forces_kernel    = mod.get_function("forces"  )
        integrate_kernel = mod.get_function("integrate")

        matrix_texref         = mod.get_texref('tex_matrix')
        station_coords_texref = mod.get_texref('tex_station_coords')
        near_stations_texref  = mod.get_texref('tex_near_stations')
        #ts_coords_texref  = mod.get_texref('tex_ts_coords') could be a 4-channel 2 dim texture, or 3 dim texture. or just 1D.

        cuda.matrix_to_texref(matrix, matrix_texref, order="F") # copy directly to device with texref - made for 2D x 1channel textures
        cuda.matrix_to_texref(station_coords_int, station_coords_texref, order="F") # fortran ordering, because we will be accessing with texND() instead of C-style indices
        # again, matrix_to_texref is not used here because that function only understands 2D arrays
        near_stations_gpu.bind_to_texref_ext(near_stations_texref)

        # note, cuda.In and cuda.Out are from the perspective of the KERNEL not the host app!
        # stations_kernel disabled since true network distances are now being used        
        #stations_kernel(near_stations_gpu, max_x, max_y, block=CUDA_BLOCK_SHAPE, grid=cuda_grid_shape)    
        # autoinit.context.synchronize()
        #self.cuda_context.synchronize() 
        
        #print "Near stations list:"
        #print near_stations_gpu
        
        #sys.exit()
        
        print "\n----CALCULATION----"
        t_start = time.time()
        n_pass = 0
        active_cells = list(self.active_cells) # make a working list of map cells that are accessible
        print "N active cells:", len(active_cells)
        while (n_pass < self.N_ITERATIONS) :
            n_pass += 1    
            random.shuffle(active_cells)
            active_cells_gpu = gpuarray.to_gpu(np.array(active_cells).astype(np.int32))
            # Pay attention to grid sizes when testing: if you don't run the integrator on the coordinates connected to stations, 
            # they don't move... so the whole thing stabilizes in a couple of cycles.    
            # Stations are worked on in blocks to avoid locking up the GPU with one giant kernel.
#            for subset_low in range(0, n_stations, self.STATION_BLOCK_SIZE) :
            for subset_low in range(1) : # changed to try integrating more often
                subset_high = subset_low + self.STATION_BLOCK_SIZE
                if subset_high > n_stations : subset_high = n_stations
                sys.stdout.write( "\rpass %03i / station %04i of %04i / total runtime %03.1f min " % (n_pass, subset_high, n_stations, (time.time() - t_start) / 60.0) )
                sys.stdout.flush()
                self.emit(QtCore.SIGNAL( 'outputProgress(int, int, int, float, float)' ),
                      n_pass, subset_high, n_stations, 
                      (time.time() - t_start) / 60.0, (time.time() - t_start) / n_pass + (subset_low/n_stations) )
                
                # adding texrefs in kernel call seems to change nothing, leaving them out.
                # max_x and max_y could be #defined in kernel source, along with STATION_BLOCK_SIZE 

                forces_kernel(np.int32(n_stations), np.int32(subset_low), np.int32(subset_high), max_x, max_y, active_cells_gpu, coords_gpu, forces_gpu, weights_gpu, errors_gpu, debug_gpu, debug_img_gpu, block=CUDA_BLOCK_SHAPE, grid=cuda_grid_shape)
                #autoinit.context.synchronize()
                self.cuda_context.synchronize()
                
                # show a sample of the results
                #print coords_gpu.get() [00:10,00:10]
                #print forces_gpu.get() [00:10,00:10]
                #print weights_gpu.get()[00:10,00:10]
                time.sleep(0.05)  # let the OS GUI use the GPU for a bit.
                
                #pl.imshow( (debug_img_gpu.get() / 60.0).T, cmap=mymap, origin='bottom')#, vmin=0, vmax=100 )
                #pl.title( 'Debugging Output - step %03d' %n_pass )
                #pl.colorbar()
                #pl.savefig( 'img/debug%03d.png' % n_pass )
                #pl.close()

# why was this indented? shouldn't it be integrated only after all stations are taken into account?
            integrate_kernel(max_x, max_y, coords_gpu, forces_gpu, weights_gpu, block=CUDA_BLOCK_SHAPE, grid=cuda_grid_shape)    
            self.cuda_context.synchronize()

            if (self.IMAGES_EVERY > 0) and (n_pass % self.IMAGES_EVERY == 0) :
                #print 'Kernel debug output:'
                #print debug_gpu
                
#                velocities = np.sqrt(np.sum(forces_gpu.get() ** 2, axis = 2)) 
#                png_f = open('img/vel%03d.png' % n_pass, 'wb')
#                png_w = png.Writer(max_x, max_y, greyscale = True, bitdepth=8)
#                png_w.write(png_f, velocities / 1200)
#                png_f.close()

#                np.set_printoptions(threshold=np.nan)
#                print velocities.astype(np.int32)

#                pl.imshow( velocities.T, origin='bottom') #, vmin=0, vmax=100 )
#                pl.title( 'Velocity ( sec / timestep) - step %03d' % n_pass )
#                pl.colorbar()
#                pl.savefig( 'img/vel%03d.png' % n_pass )
#                plt.close()
#                
#                pl.imshow( (errors_gpu.get() / weights_gpu.get() / 60.0 ).T, cmap=mymap, origin='bottom') #, vmin=0, vmax=100 )
#                pl.title( 'Average absolute error (min) - step %03d' %n_pass )
#                pl.colorbar()
#                pl.savefig( 'img/err%03d.png' % n_pass )
#                pl.close()

#                pl.imshow( (debug_img_gpu.get() / 60.0).T, cmap=mymap, origin='bottom') #, vmin=0, vmax=100 )
#                pl.title( 'Debugging Output - step %03d' %n_pass )
#                pl.colorbar()
#                pl.savefig( 'img/debug%03d.png' % n_pass )
#                pl.close()
                
                #self.emit( QtCore.SIGNAL( 'outputImage(QString)' ), QtCore.QString('img/err%03d.png' % n_pass) )
                #self.emit( QtCore.SIGNAL( 'outputImage(QImage)' ), numpy2qimage( (errors_gpu.get() / weights_gpu.get() / 60.0 / 30 * 255 ).astype(np.uint8) ) )
                velocities = np.sqrt(np.sum(forces_gpu.get() ** 2, axis = 2))
                velocities /= 15. # out of 15 sec range
                velocities *= 255
                np.clip(velocities, 0, 255, velocities)  
                velImage = numpy2qimage(velocities.astype(np.uint8)).transformed(QtGui.QMatrix().rotate(-90))
                
#                errors = np.sqrt(errors_gpu.get() / weights_gpu.get())
                e = np.sum(np.nan_to_num(errors_gpu.get())) / np.sum(np.nan_to_num(weights_gpu.get()))
                print "average error (sec) over all active cells:", e
                errors = errors_gpu.get() / weights_gpu.get() # average instead of RMS error 
                errors /= 60.
                errors /= 15. # out of 15 min range
                errors *= 255
                np.clip(errors, 0, 255, errors)  
                errImage = numpy2qimage(errors.astype(np.uint8)).transformed(QtGui.QMatrix().rotate(-90))
                
                self.emit( QtCore.SIGNAL( 'outputImage(QImage, QImage)' ), errImage, velImage )
                velImage.save('img/vel%03d.png' % n_pass, 'png' )
                errImage.save('img/err%03d.png' % n_pass, 'png' )            
  
            sys.stdout.write( "/ avg pass time %02.1f sec" % ( (time.time() - t_start) / n_pass, ) )
            sys.stdout.flush()

        #end of main loop
        np.save('result.npy', coords_gpu.get())
Exemplo n.º 29
0
    def test_problem_16(self):
        """
        Schittkowski problem #16
        """
        cost = Problem16_Cost()
        problem = roboptim.core.PyProblem(cost)
        problem.startingPoint = numpy.array([
            -2,
            1.,
        ])
        problem.argumentBounds = numpy.array([[-2., 0.5], [-float("inf"), 1.]])

        g1 = Problem16_G1()
        problem.addConstraint(g1, [
            0.,
            float("inf"),
        ])
        g2 = Problem16_G2()
        problem.addConstraint(g2, [
            0.,
            float("inf"),
        ])

        # Check starting value
        numpy.testing.assert_almost_equal(cost(problem.startingPoint)[0], 909.)

        # Initialize callback
        callback = IterCallback(problem)

        # Let the test fail if the solver does not exist.
        try:
            # Create solver
            log_dir = "/tmp/roboptim-core-python/problem_16"
            solver = roboptim.core.PySolver("ipopt", problem, log_dir=log_dir)

            # Add callback
            solver.addIterationCallback(callback)

            print(solver)
            solver.solve()
            r = solver.minimum()
            print(r)

            # Plot results
            plotter = Plotter2D([-2.1, 0.6], [0, 1.1])
            plotter.x_res = 100
            plotter.y_res = 100
            plotter.plot(cost,
                         plot_style=PlotStyle2D.PColorMesh,
                         vmax=10,
                         norm=LogNorm())

            # Set up a colormap:
            cdict = {
                'red': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)),
                '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)),
                'alpha': ((0.0, 0.0, 0.0), (1.0, 1.0, 1.0))
            }
            cstr_cmap = LinearSegmentedColormap('Mask', cdict)
            cstr_cmap.set_under('r', alpha=0)
            cstr_cmap.set_over('w', alpha=0)
            cstr_cmap.set_bad('g', alpha=0)

            plotter.plot(g1,
                         plot_style=PlotStyle2D.Contourf,
                         linewidth=10,
                         alpha=None,
                         cmap=cstr_cmap,
                         vmax=0,
                         fontsize=20)
            plotter.plot(g1,
                         plot_style=PlotStyle2D.Contour,
                         linewidth=10,
                         alpha=None,
                         levels=[0],
                         vmax=0,
                         fontsize=20,
                         colors="k")
            plotter.plot(g2,
                         plot_style=PlotStyle2D.Contourf,
                         linewidth=10,
                         alpha=None,
                         cmap=cstr_cmap,
                         vmax=0)

            # Print iterations
            X = zip(*callback.x)[0]
            Y = zip(*callback.x)[1]
            # Show evolution
            plotter.add_marker(X, Y, color="white", marker=".", markersize=5)
            # First point
            plotter.add_marker(X[0],
                               Y[0],
                               color="white",
                               marker="o",
                               markersize=10,
                               markeredgewidth=2)
            # Final result
            plotter.add_marker(X[-1],
                               Y[-1],
                               color="white",
                               marker="s",
                               markersize=10,
                               markeredgewidth=2)

            # Print actual global minimum
            plotter.add_marker(0.5,
                               0.25,
                               color="black",
                               marker="x",
                               markersize=14,
                               markeredgewidth=6)
            plotter.add_marker(0.5,
                               0.25,
                               color="white",
                               marker="x",
                               markersize=10,
                               markeredgewidth=3)

            plotter.show()

        except Exception as e:
            print("Error: %s" % e)
Exemplo n.º 30
0
def mds(MATRIX_FILE, DIMENSIONS, N_ITERATIONS, IMAGES_EVERY, STATION_BLOCK_SIZE, N_NEARBY_STATIONS, DEBUG_OUTPUT, STATUS_FUNCTION, GRAPH_FUNCTION) :

        print 'Loading matrix...'
        npz = np.load(MATRIX_FILE)
        station_coords = npz['station_coords']
        grid_dim       = npz['grid_dim']
        matrix         = npz['matrix'].astype(np.int32)

        # EVERYTHING SHOULD BE IN FLOAT32 for ease of debugging. even times.
        # Matrix and others should be textures, arrays, or in constant memory, to do cacheing.
        # As it is, I'm doing explicit cacheing.

        # force OD matrix symmetry for test
        # THIS was responsible for the coordinate drift!!!
        # need to symmetrize it before copy to device
        matrix = (matrix + matrix.T) / 2

        station_coords_int = station_coords.round().astype(np.int32)

        # to be removed when textures are working
        station_coords_gpu = gpuarray.to_gpu(station_coords_int)
        matrix_gpu = gpuarray.to_gpu(matrix)

        max_x, max_y = grid_dim
        n_gridpoints = int(max_x * max_y)
        n_stations   = len(station_coords)

        cuda_grid_shape = ( int( math.ceil( float(max_x)/CUDA_BLOCK_SHAPE[0] ) ), int( math.ceil( float(max_y)/CUDA_BLOCK_SHAPE[1] ) ) )

        print "\n----PARAMETERS----"
        print "Input file:            ", MATRIX_FILE
        print "Number of stations:    ", n_stations
        print "OD matrix shape:       ", matrix.shape    
        print "Station coords shape:  ", station_coords_int.shape 
        print "Station cache size:    ", N_NEARBY_STATIONS
        print "Map dimensions:        ", grid_dim
        print "Number of map points:  ", n_gridpoints
        print "Target space dimensionality: ", DIMENSIONS
        print "CUDA block dimensions: ", CUDA_BLOCK_SHAPE 
        print "CUDA grid dimensions:  ", cuda_grid_shape

        assert station_coords.shape == (n_stations, 2)
        assert N_NEARBY_STATIONS <= n_stations
        
        #sys.exit()

        # Make and register custom color map for pylab graphs

        cdict = {'red':   ((0.0,  0.0, 0.0),
                           (0.2,  0.0, 0.0),
                           (0.4,  0.9, 0.9),
                           (1.0,  0.0, 0.0)),

                 'green': ((0.0,  0.0, 0.1),
                           (0.05, 0.9, 0.9),
                           (0.1,  0.0, 0.0),
                           (0.4,  0.9, 0.9),
                           (0.6,  0.0, 0.0),
                           (1.0,  0.0, 0.0)),

                 'blue':  ((0.0,  0.0, 0.0),
                           (0.05, 0.0, 0.0),
                           (0.2,  0.9, 0.9),
                           (0.3,  0.0, 0.0),
                           (1.0,  0.0, 0.0))}

        mymap = LinearSegmentedColormap('mymap', cdict)
        mymap.set_over( (1.0, 0.0, 1.0) )
        mymap.set_bad ( (0.0, 0.0, 0.0) )
        pl.plt.register_cmap(cmap=mymap)

        # set up arrays for calculations

        coords_gpu = gpuarray.to_gpu(np.random.random( (max_x, max_y, DIMENSIONS) ).astype(np.float32))   # initialize coordinates to random values in range 0...1
        forces_gpu = gpuarray.zeros( (int(max_x), int(max_y), DIMENSIONS), dtype=np.float32 )             # 3D float32 accumulate forces over one timestep
        weights_gpu = gpuarray.zeros( (int(max_x), int(max_y)),             dtype=np.float32 )             # 2D float32 cell error accumulation
        errors_gpu = gpuarray.zeros( (int(max_x), int(max_y)),             dtype=np.float32 )             # 2D float32 cell error accumulation
        near_stations_gpu = gpuarray.zeros( (cuda_grid_shape[0], cuda_grid_shape[1], N_NEARBY_STATIONS), dtype=np.int32)

        debug_gpu     = gpuarray.zeros( n_gridpoints, dtype = np.int32 )
        debug_img_gpu = gpuarray.zeros_like( errors_gpu )

        print "\n----COMPILATION----"
        # times could be merged into forces kernel, if done by pixel not station.
        # integrate kernel could be GPUArray operation; also helps clean up code by using GPUArrays.
        # DIM should be replaced by python script, so as not to define twice. 
        src = open("unified_mds.cu").read()
        src = src.replace( 'N_NEARBY_STATIONS_PYTHON', str(N_NEARBY_STATIONS) )
        src = src.replace( 'N_STATIONS_PYTHON', str(n_stations) )
        src = src.replace( 'DIMENSIONS_PYTHON', str(DIMENSIONS) )
        mod = SourceModule(src, options=["--ptxas-options=-v"])
        stations_kernel  = mod.get_function("stations"  )
        forces_kernel    = mod.get_function("forces"  )
        integrate_kernel = mod.get_function("integrate")

        matrix_texref         = mod.get_texref('tex_matrix')
        station_coords_texref = mod.get_texref('tex_station_coords')
        near_stations_texref  = mod.get_texref('tex_near_stations')
        #ts_coords_texref  = mod.get_texref('tex_ts_coords') could be a 4-channel 2 dim texture, or 3 dim texture. or just 1D.

        cuda.matrix_to_texref(matrix, matrix_texref, order="F") # copy directly to device with texref - made for 2D x 1channel textures
        cuda.matrix_to_texref(station_coords_int, station_coords_texref, order="F") # fortran ordering, because we will be accessing with texND() instead of C-style indices
        near_stations_gpu.bind_to_texref_ext(near_stations_texref)

        # note, cuda.In and cuda.Out are from the perspective of the KERNEL not the host app!
        stations_kernel(near_stations_gpu, block=CUDA_BLOCK_SHAPE, grid=cuda_grid_shape)    
        autoinit.context.synchronize()

        #print "Near stations list:"
        #print near_stations_gpu
        print "\n----CALCULATION----"
        t_start = time.time()
        n_pass = 0
        while (n_pass < N_ITERATIONS) :
            n_pass += 1    
            # Pay attention to grid sizes when testing: if you don't run the integrator on the coordinates connected to stations, 
            # they don't move... so the whole thing stabilizes in a couple of cycles.    
            # Stations are worked on in blocks to avoid locking up the GPU with one giant kernel.
            for subset_low in range(0, n_stations, STATION_BLOCK_SIZE) :
                subset_high = subset_low + STATION_BLOCK_SIZE
                if subset_high > n_stations : subset_high = n_stations
                sys.stdout.write( "\rpass %03i / station %04i of %04i / total runtime %03.1f min " % (n_pass, subset_high, n_stations, (time.time() - t_start) / 60.0) )
                sys.stdout.flush()
                STATUS_FUNCTION(n_pass, subset_high, n_stations, (time.time() - t_start) / 60.0, (time.time() - t_start) / n_pass + (subset_low/n_stations))
                # adding texrefs in kernel call seems to change nothing, leaving them out.
                # max_x and max_y could be #defined in kernel source, along with STATION_BLOCK_SIZE 
                forces_kernel(np.int32(n_stations), np.int32(subset_low), np.int32(subset_high), max_x, max_y, coords_gpu, forces_gpu, weights_gpu, errors_gpu, debug_gpu, debug_img_gpu, block=CUDA_BLOCK_SHAPE, grid=cuda_grid_shape)
                autoinit.context.synchronize()
                # print coords_gpu, forces_gpu
                time.sleep(0.5)  # let the user interface catch up.
                
            integrate_kernel(max_x, max_y, coords_gpu, forces_gpu, weights_gpu, block=CUDA_BLOCK_SHAPE, grid=cuda_grid_shape)    
            autoinit.context.synchronize()

            print IMAGES_EVERY
            if (IMAGES_EVERY > 0) and (n_pass % IMAGES_EVERY == 0) :
            
                #print 'Kernel debug output:'
                #print debug_gpu
                
                velocities = np.sqrt(np.sum(forces_gpu.get() ** 2, axis = 2)) 
                pl.imshow( velocities.T, cmap=mymap, origin='bottom', vmin=0, vmax=100 )
                pl.title( 'Velocity ( sec / timestep) - step %03d' % n_pass )
                pl.colorbar()
                pl.savefig( 'img/vel%03d.png' % n_pass )
                pl.close()
                
                pl.imshow( (errors_gpu.get() / weights_gpu.get() / 60.0 ).T, cmap=mymap, origin='bottom', vmin=0, vmax=100 )
                pl.title( 'Average absolute error (min) - step %03d' %n_pass )
                pl.colorbar()
                pl.savefig( 'img/err%03d.png' % n_pass )
                pl.close()

                pl.imshow( (debug_img_gpu.get() / 60.0).T, cmap=mymap, origin='bottom', vmin=0, vmax=100 )
                pl.title( 'Debugging Output - step %03d' %n_pass )
                pl.colorbar()
                pl.savefig( 'img/debug%03d.png' % n_pass )
                pl.close()
                
                GRAPH_FUNCTION('img/err%03d.png' % n_pass)
                #INTERFACE.update
              
            sys.stdout.write( "/ avg pass time %02.1f sec" % ( (time.time() - t_start) / n_pass, ) )
            sys.stdout.flush()
Exemplo n.º 31
0

def generate_cmap_flame():
    clrs = [(1, 1, 1), (0, 0.3, 1), (0, 1, 1), (0, 1, 0.3), (1, 1, 0),
            (1, 0.5, 0), (1, 0, 0), (0.5, 0, 0)]
    flame = LinearSegmentedColormap.from_list('flame', clrs)
    #flame.set_bad(flame(0)) # set nan's and inf's to white
    flame.set_bad('0.75')  # set nan's and inf's to light gray
    return flame


flame = generate_cmap_flame()

cubehelix = LinearSegmentedColormap('cubehelix',
                                    cm.revcmap(_cm.cubehelix(1, 0, 1, 2.5)))
cubehelix.set_bad(cubehelix(0))


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])
Exemplo n.º 32
0
from .horizon import Horizon
from .utility_classes import Accumulator
from .functional import to_device, from_device
from .functional import correlation, crosscorrelation, btch, kl, js, hellinger, tv, hilbert
from .functional import smooth_out, digitize, gridify, perturb, histo_reduce
from .plotters import plot_image



CDICT = {
    'red': [[0.0, None, 1.0], [0.33, 1.0, 1.0], [0.66, 1.0, 1.0], [1.0, 0.0, None]],
    'green': [[0.0, None, 0.0], [0.33, 0.0, 0.0], [0.66, 1.0, 1.0], [1.0, 0.5, None]],
    'blue': [[0.0, None, 0.0], [0.33, 0.0, 0.0], [0.66, 0.0, 0.0], [1.0, 0.0, None]]
}
METRIC_CMAP = LinearSegmentedColormap('MetricMap', CDICT)
METRIC_CMAP.set_bad(color='black')
register_cmap(name='Metric', cmap=METRIC_CMAP)



class BaseMetrics:
    """ Base class for seismic metrics.
    Child classes have to implement access to `data`, `probs`, `bad_traces` attributes.
    """
    # pylint: disable=attribute-defined-outside-init, blacklisted-name
    PLOT_DEFAULTS = {
        'cmap': 'Metric',
        'fill_color': 'black'
    }

    LOCAL_DEFAULTS = {
Exemplo n.º 33
0
        (0,1,1),
        (0,1,0.3),
        (1,1,0),
        (1,0.5,0),
        (1,0,0),
        (0.5,0,0)
        ]
    flame = LinearSegmentedColormap.from_list('flame', clrs)
    #flame.set_bad(flame(0)) # set nan's and inf's to white
    flame.set_bad('0.75') # set nan's and inf's to light gray
    return flame

flame = generate_cmap_flame()

cubehelix = LinearSegmentedColormap('cubehelix',cm.revcmap(_cm.cubehelix(1,0,1,2.5)))
cubehelix.set_bad(cubehelix(0))

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:
Exemplo n.º 34
0
import matplotlib.patches as patches
from numpy import *
from numpy.linalg import norm
import os
plt.viridis()

cdict = {
    'red': ((0.0, 1.0, 1.0), (0.25, 1.0, 1.0), (0.5, 1.0, 1.0),
            (0.75, 0.902, 0.902), (1.0, 0.0, 0.0)),
    'green': ((0.0, 0.708, 0.708), (0.25, 0.302, 0.302), (0.5, 0.2392, 0.2392),
              (0.75, 0.1412, 0.1412), (1.0, 0.0, 0.0)),
    'blue': ((0.0, 0.4, 0.4), (0.25, 0.3569, 0.3569), (0.5, 0.6078, 0.6078),
             (0.75, 1., 1.), (1.0, 1., 1.))
}
orange_blue = LinearSegmentedColormap('orange_blue', cdict, 256)
orange_blue.set_bad('w', 1.0)
# print(orange_blue)
# viridis.set_under('w')
# viridis.set_over('w')
# bwr.set_under('w')
# bwr.set_over('w')
bwr.set_bad('k')

figs = {
    'backend': 'agg',
    'axes.labelsize': 10,
    'xtick.labelsize': 10,
    'ytick.labelsize': 10,
    #           'text.usetex': True,
    #           'font.serif' : 'serif',
    #           'font.sans-serif' : 'cm',
Exemplo n.º 35
0
    def _shiftedColorMap(self,
                         cmap,
                         start=0,
                         midpoint=0.5,
                         stop=1.0,
                         name='shiftedcmap'):
        '''
        Taken from

        https://github.com/olgabot/prettyplotlib/blob/master/prettyplotlib/colors.py

        which makes beautiful plots by the way


        Function to offset the "center" of a colormap. Useful for
        data with a negative min and positive max and you want the
        middle of the colormap's dynamic range to be at zero

        Input
        -----
          cmap : The matplotlib colormap to be altered
          start : Offset from lowest point in the colormap's range.
              Defaults to 0.0 (no lower ofset). Should be between
              0.0 and `midpoint`.
          midpoint : The new center of the colormap. Defaults to
              0.5 (no shift). Should be between 0.0 and 1.0. In
              general, this should be  1 - vmax/(vmax + abs(vmin))
              For example if your data range from -15.0 to +5.0 and
              you want the center of the colormap at 0.0, `midpoint`
              should be set to  1 - 5/(5 + 15)) or 0.75
          stop : Offset from highets point in the colormap's range.
              Defaults to 1.0 (no upper ofset). Should be between
              `midpoint` and 1.0.
        '''
        from matplotlib.colors import LinearSegmentedColormap
        from matplotlib import cm
        cdict = {'red': [], 'green': [], 'blue': [], 'alpha': []}

        # regular index to compute the colors
        reg_index = np.linspace(start, stop, 257)

        # shifted index to match the data
        shift_index = np.hstack([
            np.linspace(0.0, midpoint, 128, endpoint=False),
            np.linspace(midpoint, 1.0, 129, endpoint=True)
        ])

        for ri, si in zip(reg_index, shift_index):
            r, g, b, a = cmap(ri)

            cdict['red'].append((si, r, r))
            cdict['green'].append((si, g, g))
            cdict['blue'].append((si, b, b))
            cdict['alpha'].append((si, a, a))

        newcmap = LinearSegmentedColormap(name, cdict)

        # add some overunders
        newcmap.set_bad(color='g', alpha=0.75)
        newcmap.set_over(color='m', alpha=0.75)
        newcmap.set_under(color='c', alpha=0.75)

        cm.register_cmap(cmap=newcmap)

        return newcmap
Exemplo n.º 36
0
reds = [0.26, 0.26, 0.26, 0.39, 0.69, 1, 1, 1, 1, 1, 1]
greens_half = [0.26, 0.16, 0.09, 0.26, 0.69]
colordict = {
    'red':
    tuple([(0.1 * i, r, r) for i, r in enumerate(reds)]),
    'green':
    tuple([
        (0.1 * i, r, r)
        for i, r in enumerate(greens_half + [1] + list(reversed(greens_half)))
    ]),
    'blue':
    tuple([(0.1 * i, r, r) for i, r in enumerate(reversed(reds))])
}
CMAP_CONTINUOUS_ASSOCIATION = LinearSegmentedColormap('association', colordict)
CMAP_CONTINUOUS_ASSOCIATION.set_bad(C_BAD)

# Categorical colormap
CMAP_CATEGORICAL_PAIRED = Paired
CMAP_CATEGORICAL_PAIRED.set_bad(C_BAD)

CMAP_CATEGORICAL_SET3 = Set3
CMAP_CATEGORICAL_SET3.set_bad(C_BAD)

CMAP_CATEGORICAL_TAB20 = tab20
CMAP_CATEGORICAL_TAB20.set_bad(C_BAD)

CMAP_CATEGORICAL_TAB20B = tab20b
CMAP_CATEGORICAL_TAB20B.set_bad(C_BAD)

CMAP_CATEGORICAL_TAB20C = tab20c
Exemplo n.º 37
0
    def _shiftedColorMap(self, cmap, start=0, midpoint=0.5, stop=1.0,
                         name='shiftedcmap'):
        '''
        Taken from

        https://github.com/olgabot/prettyplotlib/blob/master/prettyplotlib/colors.py

        which makes beautiful plots by the way


        Function to offset the "center" of a colormap. Useful for
        data with a negative min and positive max and you want the
        middle of the colormap's dynamic range to be at zero

        Input
        -----
          cmap : The matplotlib colormap to be altered
          start : Offset from lowest point in the colormap's range.
              Defaults to 0.0 (no lower ofset). Should be between
              0.0 and `midpoint`.
          midpoint : The new center of the colormap. Defaults to
              0.5 (no shift). Should be between 0.0 and 1.0. In
              general, this should be  1 - vmax/(vmax + abs(vmin))
              For example if your data range from -15.0 to +5.0 and
              you want the center of the colormap at 0.0, `midpoint`
              should be set to  1 - 5/(5 + 15)) or 0.75
          stop : Offset from highets point in the colormap's range.
              Defaults to 1.0 (no upper ofset). Should be between
              `midpoint` and 1.0.
        '''
        import matplotlib.pyplot as plt
        from matplotlib.colors import LinearSegmentedColormap
        cdict = {
            'red': [],
            'green': [],
            'blue': [],
            'alpha': []
        }

        # regular index to compute the colors
        reg_index = np.linspace(start, stop, 257)

        # shifted index to match the data
        shift_index = np.hstack([
            np.linspace(0.0, midpoint, 128, endpoint=False),
            np.linspace(midpoint, 1.0, 129, endpoint=True)
        ])

        for ri, si in zip(reg_index, shift_index):
            r, g, b, a = cmap(ri)

            cdict['red'].append((si, r, r))
            cdict['green'].append((si, g, g))
            cdict['blue'].append((si, b, b))
            cdict['alpha'].append((si, a, a))

        newcmap = LinearSegmentedColormap(name, cdict)

        # add some overunders
        newcmap.set_bad(color='g', alpha=0.75)
        newcmap.set_over(color='m', alpha=0.75)
        newcmap.set_under(color='c', alpha=0.75)

        plt.register_cmap(cmap=newcmap)

        return newcmap
Exemplo n.º 38
0
        (0.36, 1.0, 1.0),
        (0.62, 1.0, 1.0),
        (0.87, 0.0, 0.0),
        (1.0, 0.0, 0.0),
    ),
    "blue": (
        (0.0, 1.0, 1.0),
        (0.11, 1.0, 1.0),
        (0.36, 1.0, 1.0),
        (0.62, 0.0, 0.0),
        (0.87, 0.0, 0.0),
        (1.0, 0.0, 0.0),
    ),
}
my_cmap = LinearSegmentedColormap("my_colormap", cdict, 256)
my_cmap.set_bad(color="0.7")


def calc_coordination(mysupport, debugging=0):
    """Calculate the coordination number of the support using a 3x3x3 kernel."""
    nbz, nby, nbx = mysupport.shape

    mykernel = np.ones((3, 3, 3))
    mycoord = np.rint(convolve(mysupport, mykernel, mode="same"))
    mycoord = mycoord.astype(int)

    if debugging == 1:
        plt.figure(figsize=(18, 15))
        plt.subplot(2, 2, 1)
        plt.imshow(mycoord[:, :, nbx // 2])
        plt.colorbar()