def hexbin(x, y, ids, size, aspect_scale, orientation):
    '''
    This function recieves x, y coordinates arrays and turns these into q, r hexagon coordinates returned in a pandas dataframe.
    Additionally, the center x, y coordinates for every hexagon are added.
    '''
    q, r = cartesian_to_axial(x,
                              y,
                              size,
                              orientation=orientation,
                              aspect_scale=aspect_scale)
    x, y = axial_to_cartesian(q,
                              r,
                              size,
                              orientation=orientation,
                              aspect_scale=aspect_scale)
    df = pd.DataFrame(dict(r=r, q=q, x=x, y=y))
    df['ids'] = ids
    df = df.groupby(['q', 'r', 'x',
                     'y'])['ids'].apply(list).reset_index(name='ids')

    counts = []
    for row in df['ids']:
        counts.append(len(row))

    df['counts'] = counts

    return df
示例#2
0
    def test_default_aspect_flattop(self):
        x = np.array([0, 0, 0, 1.5, -1.5, 1.5, -1.5])
        y = np.array([0, -2, 2, -1.5, -1.5, 1.5, 1.5])

        q, r = buh.cartesian_to_axial(x, y, 1, "flattop")

        assert list(zip(q, r)) == [(0, 0), (0, 1), (0, -1), (1, 0), (-1, 1),
                                   (1, -1), (-1, 0)]
示例#3
0
文件: test_hex.py 项目: gully/bokeh
    def test_default_aspect_flattop(self):
        x = np.array([0, 0, 0, 1.5, -1.5, 1.5, -1.5])
        y = np.array([0, -2, 2, -1.5, -1.5, 1.5, 1.5])

        q, r = buh.cartesian_to_axial(x, y, 1, "flattop")

        assert list(zip(q, r)) == [
            (0,0), (0,1), (0,-1), (1, 0), (-1, 1), (1, -1), (-1,0)
        ]
def hex_to_bin(x, y, size, aspect_scale, orientation="pointytop"):
    # pd = import_required('pandas','hexbin requires pandas to be installed')

    q, r = cartesian_to_axial(x,
                              y,
                              size,
                              orientation,
                              aspect_scale=aspect_scale)
    df = pd.DataFrame(dict(r=r, q=q))
    return df
示例#5
0
def hex_to_bin(x, y, size, aspect_scale, orientation):
    '''
    This function recieves x, y coordinates arrays and turns these into q, r hexagon coordinates returned in a pandas dataframe.
    '''
    q, r = cartesian_to_axial(x,
                              y,
                              size,
                              orientation,
                              aspect_scale=aspect_scale)
    df = pd.DataFrame(dict(r=r, q=q))
    return df
示例#6
0
    def _process(self, element, key=None):
        gridsize, aggregator, orientation = self.p.gridsize, self.p.aggregator, self.p.orientation

        # Determine sampling
        indexes = [1, 0] if self.p.invert_axes else [0, 1]
        (x0, x1), (y0, y1) = (element.range(i) for i in indexes)
        if isinstance(gridsize, tuple):
            sx, sy = gridsize
        else:
            sx, sy = gridsize, gridsize
        xsize = ((x1 - x0) / sx) * (2.0 / 3.0)
        ysize = ((y1 - y0) / sy) * (2.0 / 3.0)
        size = xsize if self.orientation == 'flat' else ysize
        if isfinite(ysize) and isfinite(xsize) and not xsize == 0:
            scale = ysize / xsize
        else:
            scale = 1

        # Compute hexagonal coordinates
        x, y = (element.dimension_values(i) for i in indexes)
        if not len(x):
            return element.clone([])
        finite = isfinite(x) & isfinite(y)
        x, y = x[finite], y[finite]
        q, r = cartesian_to_axial(x, y, size, orientation + 'top', scale)
        coords = q, r

        # Get aggregation values
        if aggregator is np.size:
            aggregator = np.sum
            values = (np.full_like(q, 1), )
            vdims = ['Count']
        elif not element.vdims:
            raise ValueError('HexTiles aggregated by value must '
                             'define a value dimensions.')
        else:
            vdims = element.vdims
            values = tuple(element.dimension_values(vdim) for vdim in vdims)

        # Construct aggregate
        data = coords + values
        xd, yd = (element.get_dimension(i) for i in indexes)
        xdn, ydn = xd.clone(range=(x0, x1)), yd.clone(range=(y0, y1))
        kdims = [ydn, xdn] if self.p.invert_axes else [xdn, ydn]
        agg = (element.clone(data, kdims=kdims,
                             vdims=vdims).aggregate(function=aggregator))
        if self.p.min_count is not None and self.p.min_count > 1:
            agg = agg[:, :, self.p.min_count:]
        agg.cdims = {xd.name: xdn, yd.name: ydn}
        return agg
示例#7
0
    def _process(self, element, key=None):
        gridsize, aggregator, orientation = self.p.gridsize, self.p.aggregator, self.p.orientation

        # Determine sampling
        indexes = [1, 0] if self.p.invert_axes else [0, 1]
        (x0, x1), (y0, y1) = (element.range(i) for i in indexes)
        if isinstance(gridsize, tuple):
            sx, sy = gridsize
        else:
            sx, sy = gridsize, gridsize
        xsize = ((x1-x0)/sx)*(2.0/3.0)
        ysize = ((y1-y0)/sy)*(2.0/3.0)
        size = xsize if self.orientation == 'flat' else ysize
        if isfinite(ysize) and isfinite(xsize) and not xsize == 0:
            scale = ysize/xsize
        else:
            scale = 1

        # Compute hexagonal coordinates
        x, y = (element.dimension_values(i) for i in indexes)
        if not len(x):
            return element.clone([])
        finite = isfinite(x) & isfinite(y)
        x, y = x[finite], y[finite]
        q, r = cartesian_to_axial(x, y, size, orientation+'top', scale)
        coords = q, r

        # Get aggregation values
        if aggregator is np.size:
            aggregator = np.sum
            values = (np.full_like(q, 1),)
            vdims = ['Count']
        elif not element.vdims:
            raise ValueError('HexTiles aggregated by value must '
                             'define a value dimensions.')
        else:
            vdims = element.vdims
            values = tuple(element.dimension_values(vdim) for vdim in vdims)

        # Construct aggregate
        data = coords + values
        xd, yd = (element.get_dimension(i) for i in indexes)
        xd, yd = xd(range=(x0, x1)), yd(range=(y0, y1))
        kdims = [yd, xd] if self.p.invert_axes else [xd, yd]
        agg = element.clone(data, kdims=kdims, vdims=vdims).aggregate(function=aggregator)
        if self.p.min_count is not None and self.p.min_count > 1:
            agg = agg[:, :, self.p.min_count:]
        return agg
示例#8
0
def create_class_source(table, term, size, ratio, orientation, superterm):
    id_to_name = read_file('files/ChEBI2Names.tsv')
    x = []
    y = []
    counter = 0

    for id in table.keys():
        superterms = table[id]["Superterms"]
        superterms = superterms.strip()
        superterms = superterms.strip("[]")
        superterms = superterms.split(',')
        for class_id in superterms:
            class_id = class_id.replace("\"", '')
            class_id = class_id.replace("\'", '')
            class_id = class_id.strip()
            try:
                name = id_to_name[class_id]
            except:
                print([class_id])

            if name == superterm:
                mass = table[id]["Mass"]
                logP = table[id]["logP"]
                x.extend([logP])
                y.extend([mass])
    x = np.asarray(x)
    y = np.asarray(y)
    q, r = cartesian_to_axial(x,
                              y,
                              size,
                              orientation=orientation,
                              aspect_scale=ratio)
    df = pd.DataFrame(dict(r=r, q=q))
    source = ColumnDataSource(df)

    return source
示例#9
0
def c_t_a_r(x):
    a, b = cartesian_to_axial(x['q'], x['r'],
                              df_meta.edge_length_m.iloc[RESOLUTION],
                              "pointytop")

    return b