Beispiel #1
0
    def create_aggregate(self, plot_width, plot_height, x_range, y_range, star_age, aperture, 
                         agg_field, x_field, y_field, agg_func, glyph):

        # this may not be the best place for it, but before we recreate the aggregation 
        # we need to edit the df according to the current slider settings, which are 
        # available here as "self.aperture", "self.age", etc. 
        self.prepare_dataframe() 

        canvas = ds.Canvas(plot_width=plot_width,plot_height=plot_height,
                           x_range=x_range,y_range=y_range)

        method = getattr(canvas, glyph)

        # handle categorical field
        if agg_field in self.categorical_fields:
            print("Categorical aggregate is about to do its thing with :", agg_field) 
            agg = method(self.df, x_field, y_field, ds.count_cat(agg_field))

        # handle ordinal field
        elif agg_field in self.ordinal_fields:
            func = self.aggregate_functions[agg_func]
            print("Ordinal aggregate is about to do its thing with :", agg_field) 
            agg = method(self.df[(self.df['LOGAGE'] == star_age) ], x_field, y_field, func(agg_field))
        else:
            print("Ordinal aggregate is about to do its thing with :", agg_field) 
            agg = method(self.df, x_field, y_field)

        return agg
Beispiel #2
0
    def view_datashade(self):
        """
        Use of datashade for performance and line for hover tool capabilities
        :return: Panel of this combination
        """
        # Select only sufficient data
        if self.x in self.y:
            self.y.remove(self.x)
        if self.y == []:
            return self.gif

        df = self.dataframe[[self.x] + self.y].copy()
        plot_opts = {
            'Scatter': {
                'color': self.color_key,
                'marker': self.marker_keys,
                'size': 10
            },
            'Curve': {
                'color': self.color_key
            }
        }
        lines_overlay = df.hvplot.scatter(
            **self.plot_options).options(plot_opts)

        def hover_curve(x_range=[df.index.min(),
                                 df.index.max()]):  # , y_range):
            # Compute
            dataframe = df.copy()
            if x_range is not None:
                dataframe = dataframe[(dataframe[self.x] > x_range[0])
                                      & (dataframe[self.x] < x_range[1])]
            data_length = len(dataframe) * len(dataframe.columns)
            step = 1 if data_length < self.max_step else data_length // self.max_step

            plot_df = dataframe[::step].hvplot.line(**self.plot_options) * \
                dataframe[::step*60].hvplot.scatter(**self.plot_options)
            plot_opts = {
                'Scatter': {
                    'color': 'k',
                    'marker': self.marker_keys,
                    'size': 10
                },
                'Curve': {
                    'color': self.color_key
                }
            }
            if len(self.y) != 1:
                plot_opts['Scatter']['color'] = self.color_key
            return plot_df.options(plot_opts)

        # Define a RangeXY stream linked to the image
        rangex = hv.streams.RangeX(source=lines_overlay)
        data_shade_plot = hv.DynamicMap(hover_curve, streams=[rangex])
        if len(self.y) == 1:
            data_shade_plot *= datashade(lines_overlay)
        else:
            data_shade_plot *= datashade(lines_overlay,
                                         aggregator=ds.count_cat('Variable'))
        return pn.panel(data_shade_plot)
Beispiel #3
0
 def test_aggregate_ndoverlay_count_cat_datetimes_microsecond_timebase(
         self):
     dates = pd.date_range(start="2016-01-01", end="2016-01-03", freq='1D')
     xstart = np.datetime64('2015-12-31T23:59:59.723518000', 'us')
     xend = np.datetime64('2016-01-03T00:00:00.276482000', 'us')
     curve = Curve((dates, [1, 2, 3]))
     curve2 = Curve((dates, [3, 2, 1]))
     ndoverlay = NdOverlay({0: curve, 1: curve2}, 'Cat')
     imgs = aggregate(ndoverlay,
                      aggregator=ds.count_cat('Cat'),
                      width=2,
                      height=2,
                      x_range=(xstart, xend),
                      dynamic=False)
     bounds = (np.datetime64('2015-12-31T23:59:59.723518'), 1.0,
               np.datetime64('2016-01-03T00:00:00.276482'), 3.0)
     dates = [
         np.datetime64('2016-01-01T11:59:59.861759000', ),
         np.datetime64('2016-01-02T12:00:00.138241000')
     ]
     expected = Image((dates, [1.5, 2.5], [[1, 0], [0, 2]]),
                      datatype=['xarray'],
                      bounds=bounds,
                      vdims='Count')
     expected2 = Image((dates, [1.5, 2.5], [[0, 1], [1, 1]]),
                       datatype=['xarray'],
                       bounds=bounds,
                       vdims='Count')
     self.assertEqual(imgs[0], expected)
     self.assertEqual(imgs[1], expected2)
Beispiel #4
0
def compute_range_created_radio_hist(client):
    """
    Use Datashader to compute a 3D histogram of cell_towers_ddf, binned by the created,
    log10_range, and radio dimensions
    """
    cell_towers_ddf = client.get_dataset("cell_towers_ddf")
    created_bin_edges = client.get_dataset("created_bin_edges")
    created_bin_centers = client.get_dataset("created_bin_centers")
    min_log10_range = client.get_dataset("min_log10_range")
    max_log10_range = client.get_dataset("max_log10_range")

    created_bins = created_bin_edges.astype("int")

    cvs2 = ds.Canvas(
        # Specify created bins
        plot_width=len(created_bins) - 1,
        x_range=[min(created_bins), max(created_bins)],
        # Specify log10_range bins
        plot_height=20,
        y_range=[min_log10_range, max_log10_range],
    )
    agg = cvs2.points(
        cell_towers_ddf, x="created", y="log10_range", agg=ds.count_cat("radio")
    )

    # Set created index back to datetime values
    agg = agg.assign_coords(created=created_bin_centers)

    return agg
Beispiel #5
0
def pl(lbls,key,data):
    dataset = pd.DataFrame({'Column1': data[:, 0], 'Column2': data[:, 1], 'Column3': lbls})
    dataset['Column3'] = dataset['Column3'].astype('category')
    cvs = ds.Canvas(plot_width = 900, plot_height = 500)
    aggs = cvs.points(dataset,'Column1','Column2',ds.count_cat('Column3'))
    img = ds.transfer_functions.shade(aggs, color_key=key)
    return img
Beispiel #6
0
def color_code_dm(dsname):

    ds = yt.load(dsname)
    box = ds.r[0:1, 0:1, 0:1]
    particle_df, used_particles, halo_catalog = create_particle_df(box)

    particle_df['position_x'] = particle_df['position_x'] * (
        1. + ds.current_redshift)
    particle_df['position_y'] = particle_df['position_y'] * (
        1. + ds.current_redshift)
    particle_df['position_z'] = particle_df['position_z'] * (
        1. + ds.current_redshift)
    particle_df['level'] = 'LX'

    particle_df.loc[particle_df.mass > 1e5, 'level'] = 'L3'
    particle_df.loc[particle_df.mass > 1e6, 'level'] = 'L2'
    particle_df.loc[particle_df.mass > 1e7, 'level'] = 'L1'
    particle_df.loc[particle_df.mass > 1e8, 'level'] = 'L0'
    particle_df.level = particle_df.level.astype('category')

    agg = aggregate_particles(particle_df, 'position_y', 'position_z', 800)
    color_key = collections.OrderedDict([('L0', '#666666'), ('L1', '#FF0000'),
                                         ('L2', '#00FF00'), ('L3', '#0000FF')])

    cvs = dshader.Canvas(plot_width=1000,
                         plot_height=1000,
                         x_range=[0, 25000],
                         y_range=[0, 25000])
    agg = cvs.points(particle_df, 'position_y', 'position_z',
                     dshader.count_cat('level'))
    img = tf.shade(agg, color_key=color_key, how='log')
    export = partial(export_image, background='white', export_path="./")
    export(img, dsname[7:] + '_particles_yz')

    return img
def agg_count_cat(
    data, x, y, z, scale_paras, 
    clip_max=0, 
    reduce=False,
    sharp_boundary=True, 
    ):
    """count categorical data
    """
    # collect aggdata and ps
    agg = ds.count_cat(z)
    aggdata, ps = agg_data_ps(data, x, y, agg, scale_paras)
    zlabels = aggdata[z].values
   
    if clip_max:
        aggdata = aggdata.clip(max=clip_max)
        
    if reduce:
        aggdata = aggdata.argmax(z)
        
    if sharp_boundary:
        # normalize by any (set no cells to nan)
        agg = ds.any()
        aggdata_any = agg_data(data, x, y, ps.npxlx, ps.npxly, agg)
        aggdata_any = aggdata_any.astype(int)
        aggdata = aggdata/aggdata_any
    
    return aggdata, ps, zlabels
Beispiel #8
0
def _compute_datashader_assets(data, x, aggregate_col, aggregate_fn,
                               color_palette):
    aggregator = None
    cmap = {"cmap": color_palette}

    if isinstance(data[x].dtype, cudf.core.dtypes.CategoricalDtype):
        if ds_version >= "0.11":
            aggregator = ds.by(
                x,
                getattr(ds, aggregate_fn)(aggregate_col),
            )
        else:
            print("only count_cat supported by datashader <=0.10")
            aggregator = ds.count_cat(x)
        cmap = {
            "color_key": {
                k: v
                for k, v in zip(
                    list(data[x].cat.categories),
                    color_palette,
                )
            }
        }
    else:
        if aggregate_fn:
            aggregator = getattr(ds, aggregate_fn)(aggregate_col)
    return aggregator, cmap
Beispiel #9
0
def create_image(x_range, y_range, w=plot_width, h=plot_height):
    cvs = dshader.Canvas(plot_width=w,
                         plot_height=h,
                         x_range=x_range,
                         y_range=y_range)
    agg = cvs.points(df, 'x', 'z', dshader.count_cat('phase'))
    img = tf.colorize(agg, phase_color_key, how='eq_hist')
    return tf.dynspread(img, threshold=0.3, max_px=4)
Beispiel #10
0
def test_count_cat():
    agg = c.points(df, 'x', 'y', ds.count_cat('cat'))
    sol = np.array([[[5, 0, 0, 0],
                     [0, 0, 5, 0]],
                    [[0, 5, 0, 0],
                     [0, 0, 0, 5]]])
    assert (nd.as_numpy(agg._data) == sol).all()
    assert agg._cats == ('a', 'b', 'c', 'd')
Beispiel #11
0
 def create_image(frame, x_range, y_range, w=plot_width, h=plot_height):
     cvs = dshader.Canvas(plot_width=w,
                          plot_height=h,
                          x_range=x_range,
                          y_range=y_range)
     agg = cvs.points(frame, 'x', 'y', dshader.count_cat('phase'))
     img = tf.shade(agg, color_key=phase_color_key, how='eq_hist')
     return img
Beispiel #12
0
def test_count_cat():
    sol = np.array([[[5, 0, 0, 0], [0, 0, 5, 0]], [[0, 5, 0, 0], [0, 0, 0,
                                                                  5]]])
    out = xr.DataArray(sol,
                       coords=(coords + [['a', 'b', 'c', 'd']]),
                       dims=(dims + ['cat']))
    agg = c.points(df, 'x', 'y', ds.count_cat('cat'))
    assert_eq(agg, out)
Beispiel #13
0
def create_image3(x_range, y_range, w=plot_width, h=plot_height):
    cvs = dshader.Canvas(plot_width=w,
                         plot_height=h,
                         x_range=x_range,
                         y_range=y_range)
    agg = cvs.points(df, 'dens', 'temp', dshader.count_cat('phase'))
    img = tf.colorize(agg, phase_color_key, how='eq_hist')
    return img
Beispiel #14
0
 def create_image2(x_range, y_range, w=plot_width, h=plot_height):
     cvs = dshader.Canvas(plot_width=w,
                          plot_height=h,
                          x_range=x_range,
                          y_range=y_range)
     agg = cvs.points(df, 'x', 'mass', dshader.count_cat('phase'))
     raw = tf.shade(agg, color_key=phase_color_key, how='eq_hist')
     img = tf.spread(raw, px=2, shape='square')
     return img
Beispiel #15
0
def nodes_plot(nodes, name=None, canvas=None, cat=None, kwargs=cvsopts):
    '''
    Plot nodes using datashader Canvas functions and
    returns datashader.transfer_functions.spread().
    '''
    canvas = ds.Canvas(**kwargs) if canvas is None else canvas
    aggregator = None if cat is None else ds.count_cat(cat)
    agg = canvas.points(nodes, 'x', 'y', aggregator)
    return tf.spread(tf.shade(agg, cmap=["#FF3333"]), px=3, name=name)
Beispiel #16
0
def create_image(longitude_range, latitude_range, w=plot_width, h=plot_height):
    x_range, y_range = webm(longitude_range, latitude_range)
    cvs = ds.Canvas(plot_width=w,
                    plot_height=h,
                    x_range=x_range,
                    y_range=y_range)
    agg = cvs.points(df, 'easting', 'northing', ds.count_cat('race'))
    img = tf.shade(agg, color_key=color_key, how='eq_hist')
    return img
Beispiel #17
0
def test_count_cat():
    sol = np.array([[[5, 0, 0, 0],
                     [0, 0, 5, 0]],
                    [[0, 5, 0, 0],
                     [0, 0, 0, 5]]])
    out = xr.DataArray(sol, coords=(coords + [['a', 'b', 'c', 'd']]),
                       dims=(dims + ['cat']))
    agg = c.points(ddf, 'x', 'y', ds.count_cat('cat'))
    assert_eq(agg, out)
Beispiel #18
0
 def my_nodesplot(nodes, name=None, canvas=None, cat=None):
     canvas = ds.Canvas(**cvsopts) if canvas is None else canvas
     aggregator = None if cat is None else ds.count_cat(cat)
     agg = canvas.points(nodes, 'x', 'y', aggregator)
     return tf.spread(tf.shade(agg,
                               cmap=["#333333"],
                               color_key=colors,
                               min_alpha=255),
                      px=3,
                      name=name)
Beispiel #19
0
 def test_aggregate_points_categorical(self):
     points = Points([(0.2, 0.3, 'A'), (0.4, 0.7, 'B'), (0, 0.99, 'C')], vdims='z')
     img = aggregate(points, dynamic=False,  x_range=(0, 1), y_range=(0, 1),
                     width=2, height=2, aggregator=ds.count_cat('z'))
     xs, ys = [0.25, 0.75], [0.25, 0.75]
     expected = NdOverlay({'A': Image((xs, ys, [[1, 0], [0, 0]]), vdims='z Count'),
                           'B': Image((xs, ys, [[0, 0], [1, 0]]), vdims='z Count'),
                           'C': Image((xs, ys, [[0, 0], [1, 0]]), vdims='z Count')},
                          kdims=['z'])
     self.assertEqual(img, expected)
Beispiel #20
0
 def test_aggregate_points_categorical(self):
     points = Points([(0.2, 0.3, 'A'), (0.4, 0.7, 'B'), (0, 0.99, 'C')], vdims='z')
     img = aggregate(points, dynamic=False,  x_range=(0, 1), y_range=(0, 1),
                     width=2, height=2, aggregator=ds.count_cat('z'))
     xs, ys = [0.25, 0.75], [0.25, 0.75]
     expected = NdOverlay({'A': Image((xs, ys, [[1, 0], [0, 0]]), vdims='z Count'),
                           'B': Image((xs, ys, [[0, 0], [1, 0]]), vdims='z Count'),
                           'C': Image((xs, ys, [[0, 0], [1, 0]]), vdims='z Count')},
                          kdims=['z'])
     self.assertEqual(img, expected)
Beispiel #21
0
 def test_aggregate_points_categorical_zero_range(self):
     points = Points([(0.2, 0.3, 'A'), (0.4, 0.7, 'B'), (0, 0.99, 'C')], vdims='z')
     img = aggregate(points, dynamic=False,  x_range=(0, 0), y_range=(0, 1),
                     aggregator=ds.count_cat('z'))
     xs, ys = [], [0.25, 0.75]
     params = dict(bounds=(0, 0, 0, 1), xdensity=1)
     expected = NdOverlay({'A': Image((xs, ys, np.zeros((2, 0))), vdims='z Count', **params),
                           'B': Image((xs, ys, np.zeros((2, 0))), vdims='z Count', **params),
                           'C': Image((xs, ys, np.zeros((2, 0))), vdims='z Count', **params)},
                          kdims=['z'])
     self.assertEqual(img, expected)
Beispiel #22
0
 def test_aggregate_points_categorical_zero_range(self):
     points = Points([(0.2, 0.3, 'A'), (0.4, 0.7, 'B'), (0, 0.99, 'C')], vdims='z')
     img = aggregate(points, dynamic=False,  x_range=(0, 0), y_range=(0, 1),
                     aggregator=ds.count_cat('z'))
     xs, ys = [], [0.25, 0.75]
     params = dict(bounds=(0, 0, 0, 1), xdensity=1)
     expected = NdOverlay({'A': Image((xs, ys, np.zeros((2, 0))), vdims='z Count', **params),
                           'B': Image((xs, ys, np.zeros((2, 0))), vdims='z Count', **params),
                           'C': Image((xs, ys, np.zeros((2, 0))), vdims='z Count', **params)},
                          kdims=['z'])
     self.assertEqual(img, expected)
Beispiel #23
0
def feat_reduction_umap(type=None, filename='data.csv', random_state=42):

    print("UMAP with random_state {}".format(random_state))

    if type is not None:
        ext = '_{}'.format(type)
    else:
        ext = ''

    filename = RESULTS_DIR + filename
    source_df = pd.read_csv(filename)

    data = source_df.iloc[:, :224].values.astype(np.float32)
    target = source_df['emotion'].values

    pal = [
        '#0000FF',
        '#FF0000'  # red
        # '#FFFF00'  # yellow
    ]
    color_key = {str(d): c for d, c in enumerate(pal)}

    reducer = umap.UMAP(random_state=random_state)
    embedding = reducer.fit_transform(data)

    df = pd.DataFrame(embedding, columns=('x', 'y'))
    df['class'] = pd.Series([str(int(x)) for x in target], dtype="category")
    # for k, v in df.iteritems():
    #     print("k: {}, v: {}".format(k, v))

    # cvs = ds.Canvas(plot_width=400, plot_height=400)
    cvs = ds.Canvas()
    agg = cvs.points(df, 'x', 'y', ds.count_cat('class'))
    img = tf.shade(agg, color_key=color_key, how='eq_hist')

    ds_utils.export_image(img,
                          filename='cognimuse_{}'.format(random_state),
                          export_path=RESULTS_DIR,
                          background='white')

    image = plt.imread(RESULTS_DIR + 'cognimuse_{}.png'.format(random_state))
    fig, ax = plt.subplots(figsize=(6, 6))
    plt.imshow(image)
    plt.setp(ax, xticks=[], yticks=[])
    plt.xlabel('Dimension 1')
    plt.ylabel('Dimension 2')
    plt.title(
        "COGNIMUS data data embedded\n"
        "into two dimensions by UMAP\n"
        "visualised with Datashader",
        fontsize=12)

    plt.savefig(RESULTS_DIR + 'cognimuse_{}_labels.png'.format(random_state))
def ds_to_img(heatmap, split_val, color_key=None):

    if color_key is None:
        color_key = {'1': "#FF6A00", '0': "#009951"}
    # for value in heatmap:
    # ['mmsi', 'longitude', 'latitude', 'distshore_f', 'ourlabel']
    df = pd.DataFrame(heatmap,
                      columns=["mmsi", "X", "Y", "distshore_f", "ourlabel"])
    df.ourlabel = df.ourlabel.astype(int)
    df.ourlabel = df.ourlabel.astype(str)
    df.X = df.X.astype(float)
    df.Y = df.Y.astype(float)
    df.distshore_f = df.distshore_f.astype(float)

    cvs = ds.Canvas(plot_width=int(360 * split_val),
                    plot_height=int(180 * split_val),
                    x_range=(-180, 180),
                    y_range=(-90, 90))
    df.ourlabel = df.ourlabel.astype('category')

    # now make the image
    agg = cvs.points(df, 'X', 'Y', ds.count_cat('ourlabel'))

    images = tf.shade(agg, color_key=color_key, how='log', min_alpha=0)
    img = images.data.tolist()
    image = []

    # change alpha=0 to 0, no need to send these values
    for x in img:
        i = []
        for y in x:
            if y != 0:
                alpha = (y >> 24) & 255
                # blue = (y >> 16) & 255
                # green = (y >> 8) & 255
                # red = y & 255
                if alpha > 0:
                    i.append(y)
                else:
                    i.append(0)
        image.append(i)

    try:
        vals_arr, min_val, max_val = summarize_aggregate_values(agg, how='log')
        # print '%s %s %s' % (vals_arr, min_val, max_val)
    except ValueError:
        min_val = 0
        max_val = 0

    # return json.dumps(img)
    return image, min_val, max_val
Beispiel #25
0
def plot_catalog_overlap(catalog_a, catalog_b, legend):

    a_coords = catalog_a[['ra', 'dec']]
    a_coords['catalog'] = legend[0]
    b_coords = catalog_b[['ra', 'dec']]
    b_coords['catalog'] = legend[1]

    df_to_plot = pd.concat([a_coords, b_coords])
    df_to_plot['catalog'] = df_to_plot['catalog'].astype('category')

    canvas = ds.Canvas(plot_width=400, plot_height=400)
    aggc = canvas.points(df_to_plot, 'ra', 'dec', ds.count_cat('catalog'))
    img = tf.shade(aggc)
    export_image(img, 'catalog_overlap')
Beispiel #26
0
def plot_df(df_list):
    renderer = hv.renderer('bokeh').instance(mode='server')
    print(df_list)
    lines = {
        i: hv.Curve(df_list[i], kdims=['x'], vdims=['y'])
        for i in range(len(df_list))
    }
    linespread = dynspread(datashade(hv.NdOverlay(lines, kdims='k')),
                           aggregator=ds.count_cat('k')).opts(
                               **{'plot': {
                                   'height': 400,
                                   'width': 1400
                               }})
    return renderer.get_plot(linespread).state
Beispiel #27
0
def plot_datashader_as_base64(df, param):
    cat = param['target_variables'][0]
    dfr = df.astype({cat: 'category'})
    cvs = ds.Canvas(plot_width=300, plot_height=300)
    agg = cvs.points(dfr, 'UMAP1', 'UMAP2', ds.count_cat(cat))
    color_key_dga = {'dga': 'red', 'legit': 'blue'}
    #img = tf.shade(agg, cmap='darkblue', how='log') #, cmap=color_key_dga, how="eq_hist")

    img = tf.shade(agg, cmap=color_key_dga, how="eq_hist")

    img.plot()
    pic_IObytes = img.to_bytesio()
    pic_IObytes.seek(0)
    pic_hash = base64.b64encode(pic_IObytes.read())
    return str(pic_hash)
Beispiel #28
0
def querygraph(gene, set):
    if (set == "microglia"):
        categorical_points = hv.Points(
            (df3["UMAP_1"], df3["UMAP_2"], getVals_microglia(gene)),
            ['UMAP_1', 'UMAP_2'],
            vdims='Category')
    else:
        categorical_points = hv.Points(
            (df["UMAP_1"], df["UMAP_2"], getVals(gene)), ['UMAP_1', 'UMAP_2'],
            vdims='Category')

    return dynspread(
        datashade(categorical_points,
                  aggregator=ds.count_cat('Category'),
                  color_key=['#c9c9c9', '#00008a'],
                  dynamic=False).opts(plot=dict(width=600, height=450)))
Beispiel #29
0
    def create_aggregate(self, plot_width, plot_height, x_range, y_range, agg_field, x_field, y_field, agg_func):

        canvas = ds.Canvas(plot_width=plot_width, plot_height=plot_height, x_range=x_range, y_range=y_range)

        # handle categorical field
        if agg_field in self.categorical_fields:
            agg = canvas.points(self.df, x_field, y_field, ds.count_cat(agg_field))

        # handle ordinal field
        elif agg_field in self.ordinal_fields:
            func = self.aggregate_functions[agg_func]
            agg = canvas.points(self.df, x_field, y_field, func(agg_field))
        else:
            agg = canvas.points(self.df, x_field, y_field)

        return agg
Beispiel #30
0
def clustersgraph(set):
    if (set == "microglia"):
        categorical_points = hv.Points(
            (df3["UMAP_1"], df3["UMAP_2"], df4["SCT_snn_res.0.2"]),
            ['UMAP_1', 'UMAP_2'],
            vdims='Category')
    else:
        categorical_points = hv.Points(
            (df["UMAP_1"], df["UMAP_2"], df2["SCT_snn_res.0.2"]),
            ['UMAP_1', 'UMAP_2'],
            vdims='Category')

    return dynspread(
        datashade(categorical_points,
                  aggregator=ds.count_cat('Category'),
                  color_key=Sets1to3,
                  dynamic=False).opts(plot=dict(width=600, height=450)))
Beispiel #31
0
 def datashading_plot(*args, **kwargs):
     self = args[0]
     plot = method(*args, **kwargs)
     if not self.datashade:
         return plot
     try:
         from holoviews.operation.datashader import datashade
         from datashader import count_cat
     except:
         raise ImportError('Datashading is not available')
     opts = dict(width=self._plot_opts['width'],
                 height=self._plot_opts['height'])
     if 'cmap' in self._style_opts:
         opts['cmap'] = self._style_opts['cmap']
     if self.by:
         opts['aggregator'] = count_cat(self.by)
     return datashade(plot, **opts).opts(plot=self._plot_opts)
Beispiel #32
0
def test_count_cat(ddf):
    sol = np.array([[[5, 0, 0, 0], [0, 0, 5, 0]], [[0, 5, 0, 0], [0, 0, 0,
                                                                  5]]])
    out = xr.DataArray(sol,
                       coords=(coords + [['a', 'b', 'c', 'd']]),
                       dims=(dims + ['cat']))
    agg = c.points(ddf, 'x', 'y', ds.count_cat('cat'))
    assert_eq_xr(agg, out)

    # categorizing by (cat_int-10)%4 ought to give the same result
    out = xr.DataArray(sol,
                       coords=(coords + [range(4)]),
                       dims=(dims + ['cat_int']))
    agg = c.points(
        ddf, 'x', 'y',
        ds.by(ds.category_modulo('cat_int', modulo=4, offset=10), ds.count()))
    assert_eq_xr(agg, out)

    # easier to write these tests in here, since we expect the same result with only slight tweaks

    # add an extra category (this will count nans and out of bounds)
    sol = np.append(sol, [[[0], [0]], [[0], [0]]], axis=2)

    # categorizing by binning the integer arange columns using [0,20] into 4 bins. Same result as for count_cat
    for col in 'i32', 'i64':
        out = xr.DataArray(sol,
                           coords=(coords + [range(5)]),
                           dims=(dims + [col]))
        agg = c.points(ddf, 'x', 'y',
                       ds.by(ds.category_binning(col, 0, 20, 4), ds.count()))
        assert_eq_xr(agg, out)

    # as above, but for the float arange columns. Element 2 has a nan, so the first bin is one short, and the nan bin is +1
    sol[0, 0, 0] = 4
    sol[0, 0, 4] = 1

    for col in 'f32', 'f64':
        out = xr.DataArray(sol,
                           coords=(coords + [range(5)]),
                           dims=(dims + [col]))
        agg = c.points(ddf, 'x', 'y',
                       ds.by(ds.category_binning(col, 0, 20, 4), ds.count()))
        assert_eq_xr(agg, out)
Beispiel #33
0
 def test_aggregate_ndoverlay_count_cat_datetimes_microsecond_timebase(self):
     dates = pd.date_range(start="2016-01-01", end="2016-01-03", freq='1D')
     xstart = np.datetime64('2015-12-31T23:59:59.723518000', 'us')
     xend = np.datetime64('2016-01-03T00:00:00.276482000', 'us')
     curve = Curve((dates, [1, 2, 3]))
     curve2 = Curve((dates, [3, 2, 1]))
     ndoverlay = NdOverlay({0: curve, 1: curve2}, 'Cat')
     imgs = aggregate(ndoverlay, aggregator=ds.count_cat('Cat'), width=2, height=2,
                      x_range=(xstart, xend), dynamic=False)
     bounds = (np.datetime64('2015-12-31T23:59:59.723518'), 1.0,
               np.datetime64('2016-01-03T00:00:00.276482'), 3.0)
     dates = [np.datetime64('2016-01-01T11:59:59.861759000',),
              np.datetime64('2016-01-02T12:00:00.138241000')]
     expected = Image((dates, [1.5, 2.5], [[1, 0], [0, 2]]),
                      datatype=['xarray'], bounds=bounds, vdims='Count')
     expected2 = Image((dates, [1.5, 2.5], [[0, 1], [1, 1]]),
                      datatype=['xarray'], bounds=bounds, vdims='Count')
     self.assertEqual(imgs[0], expected)
     self.assertEqual(imgs[1], expected2)
Beispiel #34
0
def plot_ds_bespoke(ax, mdf, cats, explicit_colors):
    import datashader as ds
    from datashader import transfer_functions as tf
    from datashader.colors import Sets1to3
    cvs = ds.Canvas(plot_width=960, plot_height=640)
    agg = cvs.points(mdf, 'x', 'y', ds.count_cat('cat'))

    color_key = dict(zip(cats, Sets1to3))
    color_key.update(explicit_colors)
    img = tf.shade(agg, min_alpha=128, color_key=color_key)
    img = tf.dynspread(img, threshold=0.5, max_px=1000)
    img = img.data.view(np.uint8).reshape(*img.shape, 4)[::-1, :, :]

    ax.imshow(img)
    from matplotlib.lines import Line2D
    ax.legend([Line2D([0], [0], color=color_key[c], lw=4) for c in cats],
              cats,
              loc='upper left',
              markerscale=5)
Beispiel #35
0
def plot_ds(ax, mdf, col, title):
    import datashader as ds
    from datashader import transfer_functions as tf
    from datashader.colors import Sets1to3
    mdf[col] = mdf[col].astype('category')
    cvs = ds.Canvas(plot_width=960, plot_height=540)
    agg = cvs.points(mdf, 'x', 'y', ds.count_cat(col))

    color_key = dict(zip(mdf[col].cat.categories, Sets1to3))
    img = tf.shade(agg, name=title, min_alpha=64, color_key=color_key)
    img = tf.dynspread(img)
    img = img.data.view(np.uint8).reshape(*img.shape, 4)[::-1, :, :]

    ax.imshow(img)
    from matplotlib.lines import Line2D
    ax.legend([Line2D([0], [0], color=c, lw=4) for c in color_key.values()],
              color_key.keys(),
              loc='upper left',
              markerscale=5)
Beispiel #36
0
 def callback_InteractiveImage(self,
                               x_range,
                               y_range,
                               plot_width,
                               plot_height,
                               name=None):
     cvs = ds.Canvas(
         plot_width=plot_width,
         plot_height=plot_height,
         x_range=x_range,
         y_range=y_range,
     )
     agg = cvs.points(
         self.result,
         'x',
         'y',
         agg=ds.count_cat('c'),
     )
     img = tf.shade(agg, min_alpha=255, color_key=self.color_key)
     return img
Beispiel #37
0
def nodesplot(nodes, name=None, canvas=None, cat=None):
    canvas = ds.Canvas(**cvsopts) if canvas is None else canvas
    aggregator=None if cat is None else ds.count_cat(cat)
    agg=canvas.points(nodes,'x','y',aggregator)
    return tf.spread(tf.shade(agg, cmap=["#FF3333"]), px=3, name=name)
def get_events(tdb):
    query = [('title', 'Prince (musician)')]
    for i in range(len(tdb)):
        events = list(tdb.trail(i, event_filter=query))
        if events:
            yield events[0].time, events

def get_dataframe():
    tdb = TrailDB('pydata-tutorial.tdb')
    base = tdb.min_timestamp()
    types = []
    xs = []
    ys = []
    #try this:
    #for y, (first_ts, events) in enumerate(sorted(get_events(tdb), reverse=True)):
    for y, (first_ts, events) in enumerate(get_events(tdb)):
        for event in events:
            xs.append(int(event.time - base) / (24 * 3600))
            ys.append(y)
            types.append('user' if event.user else 'anon')
    data = pd.DataFrame({'x': xs, 'y': ys})
    data['type'] = pd.Series(types, dtype='category')
    return data

cnv = ds.Canvas(400, 300)
agg = cnv.points(get_dataframe(), 'x', 'y', ds.count_cat('type'))
colors = {'anon': 'red', 'user': '******'}
img=tf.set_background(tf.colorize(agg, colors, how='eq_hist'), 'white')
with open('prince.png', 'w') as f:
    f.write(img.to_bytesio().getvalue())