How to use datashader - 10 common examples

To help you get started, we’ve selected a few datashader examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github holoviz / datashader / datashader / geom / base.py View on Github external
def ys(self):
        start_indices = self.start_indices // 2
        flat_array = self.flat_array[1::2]
        return RaggedArray({"start_indices": start_indices, "flat_array": flat_array})
github lmcinnes / umap / umap / plot.py View on Github external
height=height)
        elif values is not None:
            min_val = data.values.min()
            val_range = data.values.max() - min_val
            data['val_cat'] = pd.Categorical((data.values - min_val) //
                                             (val_range // 256))
            point_plot = hv.Points(data, kdims=['x', 'y'], vdims=['val_cat'])
            plot = hd.datashade(point_plot,
                                aggregator=ds.count_cat('val_cat'),
                                cmap=plt.get_cmap(cmap),
                                width=width,
                                height=height)
        else:
            point_plot = hv.Points(data, kdims=['x', 'y'])
            plot = hd.datashade(point_plot,
                                aggregator=ds.count(),
                                cmap=plt.get_cmap(cmap),
                                width=width,
                                height=height)

    return plot
github holoviz / datashader / datashader / transfer_functions / __init__.py View on Github external
if px == 0:
            return img
        mask = _mask_lookup[shape](px)
    elif not (isinstance(mask, np.ndarray) and mask.ndim == 2 and
              mask.shape[0] == mask.shape[1] and mask.shape[0] % 2 == 1):
        raise ValueError("mask must be a square 2 dimensional ndarray with "
                         "odd dimensions.")
        mask = mask if mask.dtype == 'bool' else mask.astype('bool')
    kernel = _build_spread_kernel(how)
    w = mask.shape[0]
    extra = w // 2
    M, N = img.shape
    buf = np.zeros((M + 2*extra, N + 2*extra), dtype='uint32')
    kernel(img.data, mask, buf)
    out = buf[extra:-extra, extra:-extra].copy()
    return Image(out, dims=img.dims, coords=img.coords, name=name)
github holoviz / holoviews / tests / operation / testdatashader.py View on Github external
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)
github holoviz / holoviews / tests / operation / testdatashader.py View on Github external
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)
github holoviz / holoviews / tests / operation / testdatashader.py View on Github external
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)
github holoviz / holoviews / tests / operation / testdatashader.py View on Github external
def test_rasterize_quadmesh(self):
        qmesh = QuadMesh(([0, 1], [0, 1], np.array([[0, 1], [2, 3]])))
        img = rasterize(qmesh, width=3, height=3, dynamic=False, aggregator=ds.mean('z'))
        image = Image(np.array([[2., 3., np.NaN], [0, 1, np.NaN], [np.NaN, np.NaN, np.NaN]]),
                      bounds=(-.5, -.5, 1.5, 1.5))
        self.assertEqual(img, image)
github holoviz / holoviews / tests / operation / testdatashader.py View on Github external
def test_rasterize_trimesh_ds_aggregator(self):
        simplices = [(0, 1, 2, 0.5), (3, 2, 1, 1.5)]
        vertices = [(0., 0.), (0., 1.), (1., 0), (1, 1)]
        trimesh = TriMesh((simplices, vertices), vdims=['z'])
        img = rasterize(trimesh, width=3, height=3, dynamic=False, aggregator=ds.mean('z'))
        image = Image(np.array([[1.5, 1.5, np.NaN], [0.5, 1.5, np.NaN], [np.NaN, np.NaN, np.NaN]]),
                      bounds=(0, 0, 1, 1))
        self.assertEqual(img, image)
github holoviz / datashader / datashader / resampling.py View on Github external
from itertools import groupby
from math import floor, ceil

import dask.array as da
import numpy as np
import numba as nb

from dask.delayed import delayed
from numba import prange
from .utils import ngjit

try:
    # Try to create numba JIT with 'parallel' target
    ngjit_parallel = nb.jit(nopython=True, nogil=True, parallel=True)
except:
    ngjit_parallel, prange = ngjit, range # NOQA


#: Interpolation method for upsampling: Take nearest source grid cell, even if it is invalid.
US_NEAREST = 10
#: Interpolation method for upsampling: Bi-linear interpolation between the 4 nearest source grid cells.
US_LINEAR = 11

#: Aggregation method for downsampling: Take first valid source grid cell, ignore contribution areas.
DS_FIRST = 50
#: Aggregation method for downsampling: Take last valid source grid cell, ignore contribution areas.
DS_LAST = 51
#: Aggregation method for downsampling: Take the minimum source grid cell value, ignore contribution areas.
DS_MIN = 52
#: Aggregation method for downsampling: Take the maximum source grid cell value, ignore contribution areas.
DS_MAX = 53
#: Aggregation method for downsampling: Compute average of all valid source grid cells,
github holoviz / datashader / datashader / glyphs / area.py View on Github external
def perform_extend(i, sx, tx, sy, ty, xmin, xmax, ymin, ymax, plot_start,
                       xs, ys0, ys1, *aggs_and_cols):
        x0 = xs[i]
        x1 = xs[i + 1]
        y0 = ys0[i]
        y1 = ys1[i]
        y2 = ys1[i + 1]
        y3 = ys0[i + 1]
        trapezoid_start = (plot_start if i == 0 else
                           (isnull(xs[i - 1]) or
                            isnull(ys0[i - 1]) or
                            isnull(ys1[i - 1])))
        stacked = True
        draw_trapezoid_y(
            i, sx, tx, sy, ty, xmin, xmax, ymin, ymax,
            x0, x1, y0, y1, y2, y3, trapezoid_start, stacked,
            *aggs_and_cols
        )