Пример #1
0
def test_count():
    out = np.array([[5, 5], [5, 5]], dtype='i4')
    eq(c.points(df, 'x', 'y', ds.count('i32')), out)
    eq(c.points(df, 'x', 'y', ds.count('i64')), out)
    eq(c.points(df, 'x', 'y', ds.count()), out)
    out = np.array([[4, 5], [5, 5]], dtype='i4')
    eq(c.points(df, 'x', 'y', ds.count('f32')), out)
    eq(c.points(df, 'x', 'y', ds.count('f64')), out)
def merged_images(x_range, y_range, w, h, how='log'):
    cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range)
    picks = cvs.points(sep_trips, 'pickup_x',  'pickup_y',  ds.count('passenger_count'))
    drops = cvs.points(sep_trips, 'dropoff_x', 'dropoff_y', ds.count('passenger_count'))
    more_drops = tf.interpolate(drops.where(drops > picks), cmap=["lightblue", 'blue'], how=how)
    more_picks = tf.interpolate(picks.where(picks > drops), cmap=["lightpink", 'red'],  how=how)
    img = tf.stack(more_picks,more_drops)
    return tf.dynspread(img, threshold=0.1, max_px=4)
Пример #3
0
def test_count():
    out = xr.DataArray(np.array([[5, 5], [5, 5]], dtype='i4'),
                       coords=coords, dims=dims)
    assert_eq(c.points(ddf, 'x', 'y', ds.count('i32')), out)
    assert_eq(c.points(ddf, 'x', 'y', ds.count('i64')), out)
    assert_eq(c.points(ddf, 'x', 'y', ds.count()), out)
    out = xr.DataArray(np.array([[4, 5], [5, 5]], dtype='i4'),
                       coords=coords, dims=dims)
    assert_eq(c.points(ddf, 'x', 'y', ds.count('f32')), out)
    assert_eq(c.points(ddf, 'x', 'y', ds.count('f64')), out)
Пример #4
0
def test_log_axis():
    sol = np.array([[5, 5], [5, 5]], dtype='i4')
    out = xr.DataArray(sol, coords=[np.array([0., 1.]), np.array([1., 10.])],
                       dims=dims)
    assert_eq(c_logx.points(ddf, 'log_x', 'y', ds.count('i32')), out)
    out = xr.DataArray(sol, coords=[np.array([1., 10.]), np.array([0., 1.])],
                       dims=dims)
    assert_eq(c_logy.points(ddf, 'x', 'log_y', ds.count('i32')), out)
    out = xr.DataArray(sol, coords=[np.array([1., 10.]), np.array([1., 10.])],
                       dims=dims)
    assert_eq(c_logxy.points(ddf, 'log_x', 'log_y', ds.count('i32')), out)
Пример #5
0
def test_count():
    out = xr.DataArray(np.array([[5, 5], [5, 5]], dtype='i4'),
                       coords=coords,
                       dims=dims)
    assert_eq(c.points(df, 'x', 'y', ds.count('i32')), out)
    assert_eq(c.points(df, 'x', 'y', ds.count('i64')), out)
    assert_eq(c.points(df, 'x', 'y', ds.count()), out)
    out = xr.DataArray(np.array([[4, 5], [5, 5]], dtype='i4'),
                       coords=coords,
                       dims=dims)
    assert_eq(c.points(df, 'x', 'y', ds.count('f32')), out)
    assert_eq(c.points(df, 'x', 'y', ds.count('f64')), out)
Пример #6
0
def test_log_axis_line():
    # Upper bound for scale/index of x-axis
    x_max_index = 10 ** (1 / (2 / np.log10(11)))
    sol = np.array([[5, 5], [5, 5]], dtype='i4')
    out = xr.DataArray(sol, coords=[np.array([0., 1.]), np.array([1., x_max_index])],
                       dims=dims)
    assert_eq(c_logx.line(ddf, 'log_x', 'y', ds.count('i32')), out)
    out = xr.DataArray(sol, coords=[np.array([1., x_max_index]), np.array([0., 1.])],
                       dims=dims)
    assert_eq(c_logy.line(ddf, 'x', 'log_y', ds.count('i32')), out)
    out = xr.DataArray(sol, coords=[np.array([1., x_max_index]), np.array([1., x_max_index])],
                       dims=dims)
    assert_eq(c_logxy.line(ddf, 'log_x', 'log_y', ds.count('i32')), out)
Пример #7
0
def massive_scatterplot(
    ax, 
    data, 
    x, y,  
    npxlx, npxly, 
    agg=ds.count(),
    cmap=sns.cubehelix_palette(as_cmap=True),
    vmin=0,
    vmax=0,
    vmaxp=99,
    ):
    """
    """
    aggdata = agg_data(data, x, y, npxlx, npxly, agg)
    if vmin==0 and vmax == 0:
        vmin, vmax = set_vmin_vmax(aggdata.values, vmaxp)
        imshow_routine(ax, aggdata, cmap=cmap, 
                    vmin=vmin, vmax=vmax,
                    )
    else:
        imshow_routine(ax, aggdata, cmap=cmap, 
                    vmin=vmin, vmax=vmax,
                    )

    return ax 
Пример #8
0
def test_line_autorange_axis1_x_constant():
    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 9), 9)

    xs = np.array([-4, 0, 4])
    df = pd.DataFrame({
        'y0': [0,  0],
        'y1': [-4, 4],
        'y2': [0,  0]
    })

    cvs = ds.Canvas(plot_width=9, plot_height=9)

    agg = cvs.line(df,
                   xs,
                   ['y0', 'y1', 'y2'],
                   ds.count(),
                   axis=1)

    sol = np.array([[0, 0, 0, 0, 1, 0, 0, 0, 0],
                    [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0],
                    [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [2, 0, 0, 0, 0, 0, 0, 0, 2],
                    [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0],
                    [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 0, 0, 1, 0, 0, 0, 0]], dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords, lincoords],
                       dims=['y', 'x'])
    assert_eq(agg, out)
Пример #9
0
def test_line_autorange(df, x, y, ax):
    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 9), 9)

    cvs = ds.Canvas(plot_width=9, plot_height=9)

    agg = cvs.line(df,
                   x,
                   y,
                   ds.count(),
                   axis=ax)

    sol = np.array([[0, 0, 0, 0, 2, 0, 0, 0, 0],
                    [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0],
                    [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [1, 0, 0, 0, 0, 0, 0, 0, 1],
                    [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0],
                    [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 0, 0, 2, 0, 0, 0, 0]], dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords, lincoords],
                       dims=['y', 'x'])
    assert_eq(agg, out)
Пример #10
0
def test_line_manual_range(df, x, y, ax):
    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(
        axis.compute_scale_and_translate((-3., 3.), 7), 7)

    cvs = ds.Canvas(plot_width=7, plot_height=7,
                    x_range=(-3, 3), y_range=(-3, 3))

    agg = cvs.line(df,
                   x,
                   y,
                   ds.count(),
                   axis=ax)

    sol = np.array([[0, 0, 1, 0, 1, 0, 0],
                    [0, 1, 0, 0, 0, 1, 0],
                    [1, 0, 0, 0, 0, 0, 1],
                    [0, 0, 0, 0, 0, 0, 0],
                    [1, 0, 0, 0, 0, 0, 1],
                    [0, 1, 0, 0, 0, 1, 0],
                    [0, 0, 1, 0, 1, 0, 0]], dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords, lincoords],
                       dims=['y', 'x'])
    assert_eq(agg, out)
Пример #11
0
    def _get_points(self):
        embeddings = self.embeddings
        classes = self.classes
        if (self.label_flag) and (classes is not None):
            data = pd.DataFrame(embeddings)
            data.columns = ['ivis 1', 'ivis 2']
            data['label'] = classes
            num_ks = len(np.unique(classes))
            color_key = list(enumerate(Sets1to3[0:num_ks]))

            embed = {
                k: hv.Points(data.values[classes == k, :],
                             ['ivis 1', 'ivis 2'],
                             'k',
                             label=str(k)).opts(color=v, size=0)
                for k, v in color_key
            }
            dse = dynspread(
                datashade(hv.NdOverlay(embed, kdims=['k']),
                          aggregator=ds.by('k', ds.count())))
            color_points = hv.NdOverlay({
                k: hv.Points([0, 0]).opts(color=v, size=0)
                for k, v in color_key
            })
            points = color_points * dse
        else:
            points = datashade(hv.Points(embeddings))

        points.opts(height=400, width=500, xaxis=None, yaxis=None)
        return points
Пример #12
0
def test_multipolygon_subpixel_horizontal(DataFrame, scale):
    df = GeoDataFrame({
        'geometry':
        MultiPolygonArray([[
            [[0, 0, 1, 0, 1, 1, 0, 1, 0, 0]],
            [[0, 2, 1, 2, 1, 3, 0, 3, 0, 2]],
        ]])
    })

    cvs = ds.Canvas(plot_height=8,
                    plot_width=8,
                    x_range=(-2 * scale, 2 * scale),
                    y_range=(0, 4))
    agg = cvs.polygons(df, 'geometry', agg=ds.count())

    sol = np.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0],
                    [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0],
                    [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]],
                   dtype=np.int32)

    axis = ds.core.LinearAxis()
    lincoords_x = axis.compute_index(
        axis.compute_scale_and_translate((-2 * scale, 2 * scale), 8), 8)
    lincoords_y = axis.compute_index(
        axis.compute_scale_and_translate((0, 4), 8), 8)
    out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x], dims=['y', 'x'])
    assert_eq_xr(agg, out)
Пример #13
0
    def test_pipeline(self):
        df = pd.DataFrame({
            'x': np.array(([0.] * 10 + [1] * 10)),
            'y': np.array(([0.] * 5 + [1] * 5 + [0] * 5 + [1] * 5)),
            'f64': np.arange(20, dtype='f8')
        })
        df.f64.iloc[2] = np.nan

        cvs = ds.Canvas(plot_width=2, plot_height=2, x_range=(0, 1), y_range=(0, 1))

        pipeline = ds.Pipeline(df, ds.Point('x', 'y'))
        img = pipeline((0, 1), (0, 1), 2, 2)
        agg = cvs.points(df, 'x', 'y', ds.count())
        self.assertTrue(img.equals(tf.shade(agg)))

        color_fn = lambda agg: tf.shade(agg, 'pink', 'red')
        pipeline.color_fn = color_fn
        img = pipeline((0, 1), (0, 1), 2, 2)
        self.assertTrue(img.equals(color_fn(agg)))

        transform_fn = lambda agg: agg + 1
        pipeline.transform_fn = transform_fn
        img = pipeline((0, 1), (0, 1), 2, 2)
        self.assertTrue(img.equals(color_fn(transform_fn(agg))))

        pipeline = ds.Pipeline(df, ds.Point('x', 'y'), ds.sum('f64'))
        img = pipeline((0, 1), (0, 1), 2, 2)
        agg = cvs.points(df, 'x', 'y', ds.sum('f64'))
        self.assertTrue(img.equals(tf.shade(agg)))
Пример #14
0
    def __call__(self, dset, **params):

        self.p = ParamOverrides(self, params)

        if self.p.vdim is None:
            vdim = dset.vdims[0].name
        else:
            vdim = self.p.vdim

        pts = hv.util.Dynamic(dset, operation=skypoints,
                              streams=[self.p.filter_stream])

        if self.p.aggregator == 'mean':
            aggregator = ds.mean(vdim)
        elif self.p.aggregator == 'std':
            aggregator = ds.std(vdim)
        elif self.p.aggregator == 'count':
            aggregator = ds.count()


        decimate_opts = dict(plot={'tools': ['hover',
                                             'box_select']},
                             style={'alpha': 0,
                                    'size': self.p.decimate_size,
                                    'nonselection_alpha': 0})

        decimated = decimate(pts).opts(**decimate_opts)
        raster_ = rasterize(pts, aggregator=aggregator)
        color_gadget = raster_.opts(cmap=Viridis[256], colorbar=True, alpha=0)
        sky_shaded = shade(raster_, cmap=viridis)

        plot = dynspread(sky_shaded) * decimated * color_gadget

        return plot.options(bgcolor="black", responsive=True, min_height=100)
Пример #15
0
def test_area_to_zero_autorange_gap(df, x, y, ax):
    axis = ds.core.LinearAxis()
    lincoords_y = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 7), 7)
    lincoords_x = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 13), 13)

    cvs = ds.Canvas(plot_width=13, plot_height=7)

    ddf = dd.from_pandas(df, npartitions=2)

    agg = cvs.area(ddf, x, y, ds.count(), axis=ax)

    sol = np.array([[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                    [1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]],
                   dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x],
                       dims=['y', 'x'])
    assert_eq(agg, out)
Пример #16
0
    def _process(self, element, key=None):
        if isinstance(element, TriMesh):
            x, y = element.nodes.kdims[:2]
        else:
            x, y = element.kdims
        info = self._get_sampling(element, x, y)
        (x_range, y_range), _, (width, height), (xtype, ytype) = info
        cvs = ds.Canvas(plot_width=width, plot_height=height,
                        x_range=x_range, y_range=y_range)

        agg = self.p.aggregator
        if not (element.vdims or element.nodes.vdims):
            if not isinstance(agg, (rd.count, rd.any)) and agg.column is not None:
                raise ValueError("Aggregation column %s not found on TriMesh element. "
                                 "The supplied TriMesh does not define any value "
                                 "dimensions.")
            self.p.aggregator = ds.count() if not isinstance(agg, ds.any) else agg
            return aggregate._process(self, element, key)
        elif element._plot_id in self._precomputed:
            precomputed = self._precomputed[element._plot_id]
        else:
            precomputed = self._precompute(element)
        simplices = precomputed['simplices']
        pts = precomputed['vertices']
        mesh = precomputed['mesh']
        if self.p.precompute:
            self._precomputed = {element._plot_id: precomputed}

        vdim = element.vdims[0] if element.vdims else element.nodes.vdims[0]
        interpolate = bool(self.p.interpolation)
        agg = cvs.trimesh(pts, simplices, agg=self._get_aggregator(element),
                          interp=interpolate, mesh=mesh)
        params = dict(get_param_values(element), kdims=[x, y],
                      datatype=['xarray'], vdims=[vdim])
        return Image(agg, **params)
Пример #17
0
def test_line_manual_range(df, x, y, ax):
    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(
        axis.compute_scale_and_translate((-3., 3.), 7), 7)

    cvs = ds.Canvas(plot_width=7, plot_height=7,
                    x_range=(-3, 3), y_range=(-3, 3))

    agg = cvs.line(df,
                   x,
                   y,
                   ds.count(),
                   axis=ax)

    sol = np.array([[0, 0, 1, 0, 1, 0, 0],
                    [0, 1, 0, 0, 0, 1, 0],
                    [1, 0, 0, 0, 0, 0, 1],
                    [0, 0, 0, 0, 0, 0, 0],
                    [1, 0, 0, 0, 0, 0, 1],
                    [0, 1, 0, 0, 0, 1, 0],
                    [0, 0, 1, 0, 1, 0, 0]], dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords, lincoords],
                       dims=['y', 'x'])
    assert_eq(agg, out)
Пример #18
0
def test_area_to_line_autorange_gap(df, x, y, y_stack, ax):
    axis = ds.core.LinearAxis()
    lincoords_y = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 7), 7)
    lincoords_x = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 13), 13)

    cvs = ds.Canvas(plot_width=13, plot_height=7)

    ddf = dd.from_pandas(df, npartitions=2)

    # When a line is specified to fill to, this line is not included in
    # the fill.  So we expect the y=0 line to not be filled.
    agg = cvs.area(ddf, x, y, ds.count(), y_stack=y_stack, axis=ax)

    sol = np.array([[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]],
                   dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x],
                       dims=['y', 'x'])
    assert_eq(agg, out)
Пример #19
0
def test_line_autorange(df, x, y, ax):
    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 9), 9)

    cvs = ds.Canvas(plot_width=9, plot_height=9)

    agg = cvs.line(df,
                   x,
                   y,
                   ds.count(),
                   axis=ax)

    sol = np.array([[0, 0, 0, 0, 2, 0, 0, 0, 0],
                    [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0],
                    [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [1, 0, 0, 0, 0, 0, 0, 0, 1],
                    [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0],
                    [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 0, 0, 2, 0, 0, 0, 0]], dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords, lincoords],
                       dims=['y', 'x'])
    assert_eq(agg, out)
Пример #20
0
def test_area_to_zero_fixedrange(df, x, y, ax):
    axis = ds.core.LinearAxis()
    lincoords_y = axis.compute_index(
        axis.compute_scale_and_translate((-2.25, 2.25), 5), 5)

    lincoords_x = axis.compute_index(
        axis.compute_scale_and_translate((-3.75, 3.75), 9), 9)

    cvs = ds.Canvas(plot_width=9, plot_height=5,
                    x_range=[-3.75, 3.75], y_range=[-2.25, 2.25])

    ddf = dd.from_pandas(df, npartitions=2)

    agg = cvs.area(ddf, x, y, ds.count(), axis=ax)

    sol = np.array([[0, 1, 1, 0, 0, 0, 0, 0, 0],
                    [1, 1, 1, 1, 0, 0, 0, 0, 0],
                    [1, 1, 1, 1, 1, 0, 1, 1, 1],
                    [0, 0, 0, 0, 0, 0, 1, 1, 1],
                    [0, 0, 0, 0, 0, 0, 1, 1, 0]],
                   dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x],
                       dims=['y', 'x'])
    assert_eq(agg, out)
Пример #21
0
def test_line_autorange(DataFrame, df_args, x, y, ax):
    if cudf and DataFrame is cudf_DataFrame:
        if isinstance(getattr(df_args[0].get('x', []), 'dtype', ''),
                      RaggedDtype):
            pytest.skip("cudf DataFrames do not support extension types")

    df = DataFrame(*df_args)

    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 9), 9)

    cvs = ds.Canvas(plot_width=9, plot_height=9)

    agg = cvs.line(df, x, y, ds.count(), axis=ax)

    sol = np.array([[0, 0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [1, 0, 0, 0, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 0, 0, 2, 0, 0, 0, 0]],
                   dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords, lincoords], dims=['y', 'x'])
    assert_eq_xr(agg, out)
Пример #22
0
def test_area_to_zero_fixedrange(DataFrame, df_kwargs, cvs_kwargs):
    if DataFrame is dask_cudf_DataFrame:
        if df_kwargs.get('dtype', '').startswith('Ragged'):
            pytest.skip("Ragged array not supported with cudf")

    axis = ds.core.LinearAxis()
    lincoords_y = axis.compute_index(
        axis.compute_scale_and_translate((-2.25, 2.25), 5), 5)

    lincoords_x = axis.compute_index(
        axis.compute_scale_and_translate((-3.75, 3.75), 9), 9)

    cvs = ds.Canvas(plot_width=9, plot_height=5,
                    x_range=[-3.75, 3.75], y_range=[-2.25, 2.25])

    ddf = DataFrame(**df_kwargs)

    agg = cvs.area(ddf, agg=ds.count(), **cvs_kwargs)

    sol = np.array([[0, 1, 1, 0, 0, 0, 0, 0, 0],
                    [1, 1, 1, 1, 0, 0, 0, 0, 0],
                    [1, 1, 1, 1, 1, 0, 1, 1, 1],
                    [0, 0, 0, 0, 0, 0, 1, 1, 1],
                    [0, 0, 0, 0, 0, 0, 1, 1, 0]],
                   dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x],
                       dims=['y', 'x'])
    assert_eq_xr(agg, out)
Пример #23
0
def test_area_to_zero_fixedrange(DataFrame, df_kwargs, x, y, ax):
    if cudf and DataFrame is cudf_DataFrame:
        if isinstance(getattr(df_kwargs['data'].get('x', []), 'dtype', ''),
                      RaggedDtype):
            pytest.skip("cudf DataFrames do not support extension types")

    df = DataFrame(**df_kwargs)

    axis = ds.core.LinearAxis()
    lincoords_y = axis.compute_index(
        axis.compute_scale_and_translate((-2.25, 2.25), 5), 5)

    lincoords_x = axis.compute_index(
        axis.compute_scale_and_translate((-3.75, 3.75), 9), 9)

    cvs = ds.Canvas(plot_width=9,
                    plot_height=5,
                    x_range=[-3.75, 3.75],
                    y_range=[-2.25, 2.25])

    agg = cvs.area(df, x, y, ds.count(), axis=ax)

    sol = np.array([[0, 1, 1, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0, 0],
                    [1, 1, 1, 1, 1, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1],
                    [0, 0, 0, 0, 0, 0, 1, 1, 0]],
                   dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x], dims=['y', 'x'])
    assert_eq_xr(agg, out)
Пример #24
0
def test_line_x_constant_autorange():
    # axis1 y constant
    df = pd.DataFrame({
        'y0': [0, 0, 0],
        'y1': [-4, 0, 4],
        'y2': [0, 0, 0],
    })

    x = np.array([-4, 0, 4])
    y = ['y0', 'y1', 'y2']
    ax = 1

    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 9), 9)

    ddf = dd.from_pandas(df, npartitions=2)

    cvs = ds.Canvas(plot_width=9, plot_height=9)

    agg = cvs.line(ddf, x, y, ds.count(), axis=ax)

    sol = np.array([[0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [3, 1, 1, 1, 1, 1, 1, 1, 3], [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 0, 0, 1, 0, 0, 0, 0]],
                   dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords, lincoords], dims=['y', 'x'])
    assert_eq(agg, out)
Пример #25
0
def test_uniform_diagonal_points(low, high):
    bounds = (low, high)
    x_range, y_range = bounds, bounds

    width = x_range[1] - x_range[0]
    height = y_range[1] - y_range[0]
    n = width * height
    df = pd.DataFrame({
        'time':
        np.ones(n, dtype='i4'),
        'x':
        np.array([np.arange(*x_range, dtype='f8')] * width).flatten(),
        'y':
        np.array([np.arange(*y_range, dtype='f8')] * height).flatten()
    })

    cvs = ds.Canvas(plot_width=2,
                    plot_height=2,
                    x_range=x_range,
                    y_range=y_range)
    agg = cvs.points(df, 'x', 'y', ds.count('time'))

    diagonal = agg.data.diagonal(0)
    assert sum(diagonal) == n
    assert abs(bounds[1] - bounds[0]) % 2 == abs(diagonal[1] / high -
                                                 diagonal[0] / high)
Пример #26
0
def test_area_to_line_autorange_gap(df, x, y, y_stack, ax):
    axis = ds.core.LinearAxis()
    lincoords_y = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 7), 7)
    lincoords_x = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 13), 13)

    cvs = ds.Canvas(plot_width=13, plot_height=7)

    df = pd.DataFrame({
        'x': [-4, -2, 0, np.nan, 2, 4],
        'y0': [0, -4, 0, np.nan, 4, 0],
        'y1': [0,  0, 0, np.nan, 0, 0],
    })

    # When a line is specified to fill to, this line is not included in
    # the fill.  So we expect the y=0 line to not be filled.
    agg = cvs.area(df, 'x', 'y0', ds.count(), y_stack='y1')

    sol = np.array([[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]],
                   dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x],
                       dims=['y0', 'x'])
    assert_eq(agg, out)
Пример #27
0
def update_image(dataframe):
    global dims
    dims_data = dims.data

    if not dims_data['width'] or not dims_data['height']:
        return

    plot_width = int(math.ceil(dims_data['width'][0]))
    plot_height = int(math.ceil(dims_data['height'][0]))
    x_range = (dims_data['xmin'][0], dims_data['xmax'][0])
    y_range = (dims_data['ymin'][0], dims_data['ymax'][0])

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

    agg = canvas.points(dataframe, 'dropoff_x', 'dropoff_y',
                        ds.count('trip_distance'))

    img = tf.shade(agg, cmap=BuGn9, how='log')

    new_data = {}
    new_data['image'] = [img.data]
    new_data['x'] = [x_range[0]]
    new_data['y'] = [y_range[0]]
    new_data['dh'] = [y_range[1] - y_range[0]]
    new_data['dw'] = [x_range[1] - x_range[0]]

    image_source.stream(new_data, 1)
Пример #28
0
def edges_plot(edges, name=None, canvas=None, kwargs=cvsopts):
    '''
    Plot edges using datashader Canvas functions.
    returns datashader.transfer_functions.shade().
    '''
    canvas = ds.Canvas(**kwargs) if canvas is None else canvas
    return tf.shade(canvas.line(edges, 'x', 'y', agg=ds.count()), name=name)
Пример #29
0
def ds_shade_waveforms(waveforms, canvas_width=400, canvas_height=400, color_maps=None, y_min_uv=-500, y_max_uv=400,
                       how='log'):
    n_samples = waveforms.shape[0]
    n_channels = waveforms.shape[1]

    if color_maps is None:
        color_maps = DS_CMAPS

    if n_channels > len(color_maps):
        color_maps *= len(color_maps) // n_channels + 1

    cvs = ds.Canvas(plot_height=canvas_height, plot_width=canvas_width,
                    x_range=(0, waveforms.shape[0] - 1),
                    y_range=(y_min_uv / 0.195, y_max_uv / 0.195))

    shades = []
    for ch in range(n_channels):
        df = pd.DataFrame(waveforms[:, ch, :].T)
        agg = cvs.line(df, x=np.arange(n_samples), y=list(range(n_samples)), agg=ds.count(), axis=1)
        with np.errstate(invalid='ignore'):
            img = tf.shade(agg, how=how, cmap=plt.cm.get_cmap(color_maps[ch]))
        shades.append(img)

    # If we wanted to use all channels together, we have to create a categorical column for the selection
    # df = pd.DataFrame(wv_reshaped)
    # df['channel'] = pd.Categorical(np.tile(np.arange(n_channels), wv_reshaped.shape[0]//n_channels))
    # wv_reshaped = waveforms.reshape(n_samples, -1).T
    # agg = cvs.line(df, x=np.arange(n_samples), y=list(range(32)), agg=ds.count_cat('channel'), axis=1)

    return shades
Пример #30
0
def test_area_to_line_autorange_gap(DataFrame, df_kwargs, x, y, y_stack, ax):
    if DataFrame is dask_cudf_DataFrame:
        if df_kwargs.get('dtype', '').startswith('Ragged'):
            pytest.skip("Ragged array not supported with cudf")

    axis = ds.core.LinearAxis()
    lincoords_y = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 7), 7)
    lincoords_x = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 13), 13)

    cvs = ds.Canvas(plot_width=13, plot_height=7)

    ddf = DataFrame(**df_kwargs)

    # When a line is specified to fill to, this line is not included in
    # the fill.  So we expect the y=0 line to not be filled.
    agg = cvs.area(ddf, x, y, ds.count(), y_stack=y_stack, axis=ax)

    sol = np.array([[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]],
                   dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x], dims=['y', 'x'])
    assert_eq_xr(agg, out)
Пример #31
0
def test_line_manual_range(DataFrame, df_kwargs, x, y, ax):
    if DataFrame is dask_cudf_DataFrame:
        if df_kwargs.get('dtype', '').startswith('Ragged'):
            pytest.skip("Ragged array not supported with cudf")

    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(
        axis.compute_scale_and_translate((-3., 3.), 7), 7)

    ddf = DataFrame(**df_kwargs)
    cvs = ds.Canvas(plot_width=7,
                    plot_height=7,
                    x_range=(-3, 3),
                    y_range=(-3, 3))

    agg = cvs.line(ddf, x, y, ds.count(), axis=ax)

    sol = np.array(
        [[0, 0, 1, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 1],
         [1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 1, 0],
         [0, 0, 1, 0, 1, 0, 0]],
        dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords, lincoords], dims=['y', 'x'])
    assert_eq_xr(agg, out)
Пример #32
0
def update_image(dataframe):
    global dims
    dims_data = dims.data

    if not dims_data['width'] or not dims_data['height']:
        return

    plot_width = int(math.ceil(dims_data['width'][0]))
    plot_height = int(math.ceil(dims_data['height'][0]))
    x_range = (dims_data['xmin'][0], dims_data['xmax'][0])
    y_range = (dims_data['ymin'][0], dims_data['ymax'][0])

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

    agg = canvas.points(dataframe, 'dropoff_x', 'dropoff_y',
                        ds.count('trip_distance'))

    img = tf.shade(agg, cmap=BuGn9, how='log')

    new_data = {}
    new_data['image'] = [img.data]
    new_data['x'] = [x_range[0]]
    new_data['y'] = [y_range[0]]
    new_data['dh'] = [y_range[1] - y_range[0]]
    new_data['dw'] = [x_range[1] - x_range[0]]

    image_source.stream(new_data, 1)
Пример #33
0
def test_area_to_line_autorange(DataFrame, df_kwargs, x, y, y_stack, ax):
    if cudf and DataFrame is cudf_DataFrame:
        if isinstance(getattr(df_kwargs['data'].get('x', []), 'dtype', ''),
                      RaggedDtype):
            pytest.skip("cudf DataFrames do not support extension types")

    df = DataFrame(**df_kwargs)

    axis = ds.core.LinearAxis()
    lincoords_y = axis.compute_index(
        axis.compute_scale_and_translate((-4., 0.), 7), 7)
    lincoords_x = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 13), 13)

    cvs = ds.Canvas(plot_width=13, plot_height=7)

    agg = cvs.area(df, x, y, ds.count(), axis=ax, y_stack=y_stack)

    sol = np.array([[0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0],
                    [0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0],
                    [0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0],
                    [0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0],
                    [0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
                   dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x], dims=['y', 'x'])
    assert_eq_xr(agg, out)
Пример #34
0
class AggregationOperation(ResamplingOperation):
    """
    AggregationOperation extends the ResamplingOperation defining an
    aggregator parameter used to define a datashader Reduction.
    """

    aggregator = param.ClassSelector(class_=(ds.reductions.Reduction,
                                             basestring),
                                     default=ds.count(),
                                     doc="""
        Datashader reduction function used for aggregating the data.
        The aggregator may also define a column to aggregate; if
        no column is defined the first value dimension of the element
        will be used. May also be defined as a string.""")

    _agg_methods = {
        'any': rd.any,
        'count': rd.count,
        'first': rd.first,
        'last': rd.last,
        'mode': rd.mode,
        'mean': rd.mean,
        'sum': rd.sum,
        'var': rd.var,
        'std': rd.std,
        'min': rd.min,
        'max': rd.max
    }

    def _get_aggregator(self, element, add_field=True):
        agg = self.p.aggregator
        if isinstance(agg, basestring):
            if agg not in self._agg_methods:
                agg_methods = sorted(self._agg_methods)
                raise ValueError('Aggregation method %r is not known; '
                                 'aggregator must be one of: %r' %
                                 (agg, agg_methods))
            agg = self._agg_methods[agg]()

        elements = element.traverse(lambda x: x, [Element])
        if add_field and agg.column is None and not isinstance(
                agg, (rd.count, rd.any)):
            if not elements:
                raise ValueError('Could not find any elements to apply '
                                 '%s operation to.' % type(self).__name__)
            inner_element = elements[0]
            if inner_element.vdims:
                field = inner_element.vdims[0].name
            elif isinstance(inner_element,
                            TriMesh) and inner_element.nodes.kdims:
                field = inner_element.nodes.vdims[0].name
            elif isinstance(element, NdOverlay):
                field = element.kdims[0].name
            else:
                raise ValueError('Could not determine dimension to apply '
                                 '%s operation to. Declare the dimension '
                                 'to aggregate as part of the datashader '
                                 'aggregator.' % type(self).__name__)
            agg = type(agg)(field)
        return agg
Пример #35
0
def test_line_autorange(DataFrame, df_kwargs, cvs_kwargs):
    if DataFrame is dask_cudf_DataFrame:
        dtype = df_kwargs.get('dtype', '')
        if dtype.startswith('Ragged') or dtype.startswith('Line'):
            pytest.skip("Ragged array not supported with cudf")

    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 9), 9)

    ddf = DataFrame(geo='geometry' in cvs_kwargs, **df_kwargs)

    cvs = ds.Canvas(plot_width=9, plot_height=9)

    agg = cvs.line(ddf, agg=ds.count(), **cvs_kwargs)

    sol = np.array([[0, 0, 0, 0, 3, 0, 0, 0, 0],
                    [0, 0, 0, 1, 1, 1, 0, 0, 0],
                    [0, 0, 1, 0, 1, 0, 1, 0, 0],
                    [0, 1, 0, 0, 1, 0, 0, 1, 0],
                    [1, 0, 0, 0, 1, 0, 0, 0, 1],
                    [0, 1, 0, 0, 1, 0, 0, 1, 0],
                    [0, 0, 1, 0, 1, 0, 1, 0, 0],
                    [0, 0, 0, 1, 1, 1, 0, 0, 0],
                    [0, 0, 0, 0, 3, 0, 0, 0, 0]], dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords, lincoords],
                       dims=['y', 'x'])
    assert_eq_xr(agg, out)
Пример #36
0
def test_line_autorange_axis1_x_constant():
    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 9), 9)

    xs = np.array([-4, 0, 4])
    df = pd.DataFrame({
        'y0': [0,  0],
        'y1': [-4, 4],
        'y2': [0,  0]
    })

    cvs = ds.Canvas(plot_width=9, plot_height=9)

    agg = cvs.line(df,
                   xs,
                   ['y0', 'y1', 'y2'],
                   ds.count(),
                   axis=1)

    sol = np.array([[0, 0, 0, 0, 1, 0, 0, 0, 0],
                    [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0],
                    [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [2, 0, 0, 0, 0, 0, 0, 0, 2],
                    [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0],
                    [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 0, 0, 1, 0, 0, 0, 0]], dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords, lincoords],
                       dims=['y', 'x'])
    assert_eq(agg, out)
Пример #37
0
def test_line_autorange_axis1_ragged():
    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 9), 9)

    df = pd.DataFrame({
        'x': pd.array([[4, 0], [0, -4, 0, 4]], dtype='Ragged[float32]'),
        'y': pd.array([[0, -4], [-4, 0, 4, 0]], dtype='Ragged[float32]')
    })

    cvs = ds.Canvas(plot_width=9, plot_height=9)

    agg = cvs.line(df,
                   'x',
                   'y',
                   ds.count(),
                   axis=1)

    sol = np.array([[0, 0, 0, 0, 2, 0, 0, 0, 0],
                    [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0],
                    [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [1, 0, 0, 0, 0, 0, 0, 0, 2],
                    [0, 1, 0, 0, 0, 0, 0, 1, 0],
                    [0, 0, 1, 0, 0, 0, 1, 0, 0],
                    [0, 0, 0, 1, 0, 1, 0, 0, 0],
                    [0, 0, 0, 0, 1, 0, 0, 0, 0]], dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords, lincoords],
                       dims=['y', 'x'])
    assert_eq(agg, out)
Пример #38
0
def test_area_to_line_autorange(DataFrame, df_kwargs, cvs_kwargs):
    if DataFrame is dask_cudf_DataFrame:
        if df_kwargs.get('dtype', '').startswith('Ragged'):
            pytest.skip("Ragged array not supported with cudf")

    axis = ds.core.LinearAxis()
    lincoords_y = axis.compute_index(
        axis.compute_scale_and_translate((-4., 0.), 7), 7)
    lincoords_x = axis.compute_index(
        axis.compute_scale_and_translate((-4., 4.), 13), 13)

    cvs = ds.Canvas(plot_width=13, plot_height=7)

    ddf = DataFrame(**df_kwargs)
    agg = cvs.area(ddf, agg=ds.count(), **cvs_kwargs)

    sol = np.array([[0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0],
                    [0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0],
                    [0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0],
                    [0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0],
                    [0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
                   dtype='i4')

    out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x],
                       dims=['y', 'x'])
    assert_eq_xr(agg, out)
Пример #39
0
def test_log_axis_line():
    axis = ds.core.LogAxis()
    logcoords = axis.compute_index(axis.compute_scale_and_translate((1, 10), 2), 2)

    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(axis.compute_scale_and_translate((0, 1), 2), 2)

    sol = np.array([[5, 5], [5, 5]], dtype='i4')
    out = xr.DataArray(sol, coords=[lincoords, logcoords],
                       dims=['y', 'log_x'])
    assert_eq(c_logx.line(ddf, 'log_x', 'y', ds.count('i32')), out)
    out = xr.DataArray(sol, coords=[logcoords, lincoords],
                       dims=['log_y', 'x'])
    assert_eq(c_logy.line(ddf, 'x', 'log_y', ds.count('i32')), out)
    out = xr.DataArray(sol, coords=[logcoords, logcoords],
                       dims=['log_y', 'log_x'])
    assert_eq(c_logxy.line(ddf, 'log_x', 'log_y', ds.count('i32')), out)
Пример #40
0
def test_auto_range_points():
    n = 10
    data = np.arange(n, dtype='i4')
    df = pd.DataFrame({'time': np.arange(n),
                       'x': data,
                       'y': data})
    ddf = dd.from_pandas(df, npartitions=2)

    cvs = ds.Canvas(plot_width=n, plot_height=n)
    agg = cvs.points(ddf, 'x', 'y', ds.count('time'))
    sol = np.zeros((n, n), int)
    np.fill_diagonal(sol, 1)
    np.testing.assert_equal(agg.data, sol)

    cvs = ds.Canvas(plot_width=n+1, plot_height=n+1)
    agg = cvs.points(ddf, 'x', 'y', ds.count('time'))
    sol = np.zeros((n+1, n+1), int)
    np.fill_diagonal(sol, 1)
    sol[5, 5] = 0
    np.testing.assert_equal(agg.data, sol)

    n = 4
    data = np.arange(n, dtype='i4')
    df = pd.DataFrame({'time': np.arange(n),
                       'x': data,
                       'y': data})
    ddf = dd.from_pandas(df, npartitions=2)

    cvs = ds.Canvas(plot_width=2*n, plot_height=2*n)
    agg = cvs.points(ddf, 'x', 'y', ds.count('time'))
    sol = np.zeros((2*n, 2*n), int)
    np.fill_diagonal(sol, 1)
    sol[[range(1, 4, 2)]] = 0
    sol[[range(4, 8, 2)]] = 0
    np.testing.assert_equal(agg.data, sol)

    cvs = ds.Canvas(plot_width=2*n+1, plot_height=2*n+1)
    agg = cvs.points(ddf, 'x', 'y', ds.count('time'))
    sol = np.zeros((2*n+1, 2*n+1), int)
    sol[0, 0] = 1
    sol[3, 3] = 1
    sol[6, 6] = 1
    sol[8, 8] = 1
    np.testing.assert_equal(agg.data, sol)
Пример #41
0
def test_multiple_aggregates():
    agg = c.points(ddf, 'x', 'y',
                   f64=dict(std=ds.std('f64'), mean=ds.mean('f64')),
                   i32_sum=ds.sum('i32'),
                   i32_count=ds.count('i32'))

    eq(agg.f64.std, df.f64.reshape((2, 2, 5)).std(axis=2).T)
    eq(agg.f64.mean, df.f64.reshape((2, 2, 5)).mean(axis=2).T)
    eq(agg.i32_sum, df.i32.reshape((2, 2, 5)).sum(axis=2).T)
    eq(agg.i32_count, np.array([[5, 5], [5, 5]], dtype='i4'))
Пример #42
0
def test_uniform_points():
    n = 101
    df = pd.DataFrame({'time': np.ones(2*n, dtype='i4'),
                       'x': np.concatenate((np.arange(n, dtype='f8'),
                                            np.arange(n, dtype='f8'))),
                       'y': np.concatenate(([0.] * n, [1.] * n))})

    cvs = ds.Canvas(plot_width=10, plot_height=2, y_range=(0, 1))
    agg = cvs.points(df, 'x', 'y', ds.count('time'))
    sol = np.array([[10] * 9 + [11], [10] * 9 + [11]], dtype='i4')
    np.testing.assert_equal(agg.data, sol)
Пример #43
0
def tests_datashader():
    import datashader as ds
    import datashader.transfer_functions as tf
    import pandas as pd

    df = pd.read_csv('/Users/iregon/C3S/dessaps/test_data/imma_converter/observations-sst-2014-6.psv',usecols=[6,7,14],sep="|",skiprows=0) 

    agg_mean = cvs.points(df, 'longitude', 'latitude', ds.mean('observation_value'))
    agg_max = cvs.points(df, 'longitude', 'latitude', ds.max('observation_value'))
    agg_min = cvs.points(df, 'longitude', 'latitude', ds.min('observation_value'))
    agg_count = cvs.points(df, 'longitude', 'latitude', ds.count('observation_value'))
Пример #44
0
def create_image(x_range=x_range, y_range=y_range, w=plot_width, h=plot_height, 
                 aggregator=ds.count(), categorical=None, black=False, cmap=None):
    opts={}
    if categorical and cmap:
        opts['color_key'] = categorical_color_key(len(df[aggregator.column].unique()),cmap)       

    cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range)
    agg = cvs.line(df, 'longitude', 'latitude',  aggregator)
    img = tf.shade(agg, cmap=inferno, **opts)
        
    if black: img = tf.set_background(img, 'black')
    return img
Пример #45
0
def test_multiple_aggregates():
    agg = c.points(ddf, 'x', 'y',
                   ds.summary(f64_std=ds.std('f64'),
                              f64_mean=ds.mean('f64'),
                              i32_sum=ds.sum('i32'),
                              i32_count=ds.count('i32')))

    f = lambda x: xr.DataArray(x, coords=coords, dims=dims)
    assert_eq(agg.f64_std, f(np.nanstd(df.f64.values.reshape((2, 2, 5)), axis=2).T))
    assert_eq(agg.f64_mean, f(np.nanmean(df.f64.values.reshape((2, 2, 5)), axis=2).T))
    assert_eq(agg.i32_sum, f(df.i32.values.reshape((2, 2, 5)).sum(axis=2, dtype='f8').T))
    assert_eq(agg.i32_count, f(np.array([[5, 5], [5, 5]], dtype='i4')))
def waveforms_datashader(waveforms, x_values, dir_name = "datashader_temp"):

	# Make a pandas dataframe with two columns, x and y, holding all the data. The individual waveforms are separated by a row of NaNs

	# First downsample the waveforms 10 times (to remove the effects of 10 times upsampling during de-jittering)
	waveforms = waveforms[:, ::10]
	# Then make a new array of waveforms - the last element of each waveform is a NaN
	new_waveforms = np.zeros((waveforms.shape[0], waveforms.shape[1] + 1))
	new_waveforms[:, -1] = np.nan
	new_waveforms[:, :-1] = waveforms

	# Now make an array of x's - the last element is a NaN
	x = np.zeros(x_values.shape[0] + 1)
	x[-1] = np.nan
	x[:-1] = x_values

	# Now make the dataframe
	df = pd.DataFrame({'x': np.tile(x, new_waveforms.shape[0]), 'y': new_waveforms.flatten()})	

	# Datashader function for exporting the temporary image with the waveforms
	export = partial(export_image, background = "white", export_path=dir_name)

	# Produce a datashader canvas
	canvas = ds.Canvas(x_range = (np.min(x_values), np.max(x_values)), 
			   y_range = (df['y'].min() - 10, df['y'].max() + 10),
			   plot_height=1200, plot_width=1600)
	# Aggregate the data
	agg = canvas.line(df, 'x', 'y', ds.count())   
	# Transfer the aggregated data to image using log transform and export the temporary image file
	export(tf.shade(agg, how='eq_hist'),'tempfile')

	# Read in the temporary image file
	img = imread(dir_name + "/tempfile.png")
	
	# Figure sizes chosen so that the resolution is 100 dpi
	fig,ax = plt.subplots(1, 1, figsize = (8,6), dpi = 200)
	# Start plotting
	ax.imshow(img)
	# Set ticks/labels - 10 on each axis
	ax.set_xticks(np.linspace(0, 1600, 10))
	ax.set_xticklabels(np.floor(np.linspace(np.min(x_values), np.max(x_values), 10)))
	ax.set_yticks(np.linspace(0, 1200, 10))
	ax.set_yticklabels(np.floor(np.linspace(df['y'].max() + 10, df['y'].min() - 10, 10)))

	# Delete the dataframe
	del df, waveforms, new_waveforms

	# Also remove the directory with the temporary image files
	shutil.rmtree(dir_name, ignore_errors = True)

	# Return and figure and axis for adding axis labels, title and saving the file
	return fig, ax
Пример #47
0
def test_auto_range_line():
    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(axis.compute_scale_and_translate((-10., 10.), 5), 5)

    df = pd.DataFrame({'x': [-10,  0, 10,   0, -10],
                       'y': [  0, 10,  0, -10,   0]})
    cvs = ds.Canvas(plot_width=5, plot_height=5)
    agg = cvs.line(df, 'x', 'y', ds.count())
    sol = np.array([[0, 0, 1, 0, 0],
                    [0, 1, 0, 1, 0],
                    [2, 0, 0, 0, 1],
                    [0, 1, 0, 1, 0],
                    [0, 0, 1, 0, 0]], dtype='i4')
    out = xr.DataArray(sol, coords=[lincoords, lincoords],
                       dims=['y', 'x'])
    assert_eq(agg, out)
Пример #48
0
def test_line():
    df = pd.DataFrame({'x': [4, 0, -4, -3, -2, -1.9, 0, 10, 10, 0, 4],
                       'y': [0, -4, 0, 1, 2, 2.1, 4, 20, 30, 4, 0]})
    cvs = ds.Canvas(plot_width=7, plot_height=7,
                    x_range=(-3, 3), y_range=(-3, 3))
    agg = cvs.line(df, 'x', 'y', ds.count())
    sol = np.array([[0, 0, 1, 0, 1, 0, 0],
                    [0, 1, 0, 0, 0, 1, 0],
                    [1, 0, 0, 0, 0, 0, 1],
                    [0, 0, 0, 0, 0, 0, 0],
                    [1, 0, 0, 0, 0, 0, 1],
                    [0, 2, 0, 0, 0, 1, 0],
                    [0, 0, 1, 0, 1, 0, 0]], dtype='i4')
    out = xr.DataArray(sol, coords=[np.arange(-3, 4), np.arange(-3, 4)],
                       dims=['y_axis', 'x_axis'])
    assert_eq(agg, out)
Пример #49
0
def test_uniform_diagonal_points(low, high):
    bounds = (low, high)
    x_range, y_range = bounds, bounds

    width = x_range[1] - x_range[0]
    height = y_range[1] - y_range[0]
    n = width * height
    df = pd.DataFrame({'time': np.ones(n, dtype='i4'),
                       'x': np.array([np.arange(*x_range, dtype='f8')] * width).flatten(),
                       'y': np.array([np.arange(*y_range, dtype='f8')] * height).flatten()})

    cvs = ds.Canvas(plot_width=2, plot_height=2, x_range=x_range, y_range=y_range)
    agg = cvs.points(df, 'x', 'y', ds.count('time'))

    diagonal = agg.data.diagonal(0)
    assert sum(diagonal) == n
    assert abs(bounds[1] - bounds[0]) % 2 == abs(diagonal[1] / high - diagonal[0] / high)
Пример #50
0
def test_bug_570():
    # See https://github.com/pyviz/datashader/issues/570
    df = pd.DataFrame({
        'Time': [1456353642.2053893, 1456353642.2917893],
        'data': [-59.4948743433377, 506.4847376716022],
    }, columns=['Time', 'data'])

    x_range = (1456323293.9859753, 1456374687.0009754)
    y_range = (-228.56721300380943, 460.4042291124646)

    cvs = ds.Canvas(x_range=x_range, y_range=y_range,
                    plot_height=300, plot_width=1000)
    agg = cvs.line(df, 'Time', 'data', agg=ds.count())

    # Check location of line
    yi, xi = np.where(agg.values == 1)
    assert np.array_equal(yi, np.arange(73, 300))
    assert np.array_equal(xi, np.array([590] * len(yi)))
Пример #51
0
def test_lines_on_edge():
    df = pd.DataFrame(dict(x=[0, 3, 3, 0],
                           y=[0, 0, 3, 3]))

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

    agg = canvas.line(df, 'x', 'y', agg=ds.count())

    sol = np.array([[1, 1, 1],
                    [0, 0, 1],
                    [1, 1, 1]], dtype='int32')
    out = xr.DataArray(sol,
                       coords=[('x', [0.5, 1.5, 2.5]),
                               ('y', [0.5, 1.5, 2.5])],
                       dims=['y', 'x'])

    assert_eq(agg, out)
Пример #52
0
def test_line():
    axis = ds.core.LinearAxis()
    lincoords = axis.compute_index(axis.compute_scale_and_translate((-3., 3.), 7), 7)

    df = pd.DataFrame({'x': [4, 0, -4, -3, -2, -1.9, 0, 10, 10, 0, 4],
                       'y': [0, -4, 0, 1, 2, 2.1, 4, 20, 30, 4, 0]})
    cvs = ds.Canvas(plot_width=7, plot_height=7,
                    x_range=(-3, 3), y_range=(-3, 3))
    agg = cvs.line(df, 'x', 'y', ds.count())
    sol = np.array([[0, 0, 1, 0, 1, 0, 0],
                    [0, 1, 0, 0, 0, 1, 0],
                    [1, 0, 0, 0, 0, 0, 1],
                    [0, 0, 0, 0, 0, 0, 0],
                    [1, 0, 0, 0, 0, 0, 1],
                    [0, 2, 0, 0, 0, 1, 0],
                    [0, 0, 1, 0, 1, 0, 0]], dtype='i4')
    out = xr.DataArray(sol, coords=[lincoords, lincoords],
                       dims=['y', 'x'])
    assert_eq(agg, out)
Пример #53
0
def test_pipeline():
    pipeline = ds.Pipeline(df, ds.Point('x', 'y'))
    img = pipeline((0, 1), (0, 1), 2, 2)
    agg = cvs.points(df, 'x', 'y', ds.count())
    assert img.equals(tf.interpolate(agg))

    color_fn = lambda agg: tf.interpolate(agg, 'pink', 'red')
    pipeline.color_fn = color_fn
    img = pipeline((0, 1), (0, 1), 2, 2)
    assert img.equals(color_fn(agg))

    transform_fn = lambda agg: agg + 1
    pipeline.transform_fn = transform_fn
    img = pipeline((0, 1), (0, 1), 2, 2)
    assert img.equals(color_fn(transform_fn(agg)))

    pipeline = ds.Pipeline(df, ds.Point('x', 'y'), ds.sum('f64'))
    img = pipeline((0, 1), (0, 1), 2, 2)
    agg = cvs.points(df, 'x', 'y', ds.sum('f64'))
    assert img.equals(tf.interpolate(agg))
Пример #54
0
def create_image(x_range, y_range, w, h):
    cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range)
    agg = cvs.points(picks, 'x', 'y',  ds.count('visibility'))
    img = tf.interpolate(agg, cmap=Hot, how='eq_hist')
    return tf.dynspread(img, threshold=0.5, max_px=4)
def create_image(x_range, y_range, w, h):
    cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range)
    agg = cvs.points(df, 'web_long', 'web_lat',  ds.count('trip_distance'))
    img = tf.interpolate(agg, cmap=Hot, how='eq_hist')
    return tf.dynspread(img, threshold=0.5, max_px=4)
Пример #56
0
def test_count():
    out = np.array([[5, 5], [5, 5]], dtype='i4')
    eq(c.points(ddf, 'x', 'y', agg=ds.count('i32')).agg, out)
    eq(c.points(ddf, 'x', 'y', agg=ds.count('i64')).agg, out)
    eq(c.points(ddf, 'x', 'y', agg=ds.count('f32')).agg, out)
    eq(c.points(ddf, 'x', 'y', agg=ds.count('i64')).agg, out)
Пример #57
0
def edgesplot(edges, name=None, canvas=None):
    canvas = ds.Canvas(**cvsopts) if canvas is None else canvas
    return tf.shade(canvas.line(edges, 'x','y', agg=ds.count()), name=name)
Пример #58
0
def test_log_axis():
    out = np.array([[5, 5], [5, 5]], dtype='i4')
    eq(c_logx.points(df, 'log_x', 'y', ds.count('i32')), out)
    eq(c_logy.points(df, 'x', 'log_y', ds.count('i32')), out)
    eq(c_logxy.points(df, 'log_x', 'log_y', ds.count('i32')), out)
Пример #59
0
for descriptor in descriptor_list:
        descriptors[descriptor] = dict() 
        for vari in vars_in:
            descriptors[descriptor][vari] = dict()

i = 1
for yr in range(y_ini,y_end + 1):
    for mo in range(1,13):
        logging.info('{0}-{1}'.format(yr,mo)) 
        mm = '{:02d}'.format(mo)
        # Read monthly data (header and observed vars) to df indexed by report_id
        df_mo = read_monthly_data()
        # For each observed variable in df, compute stats and aggregate to its monhtly composite.
        for vari in vars_in:
            if mm in descriptors['counts'][vari].keys():
                descriptors['counts'][vari][mm] = descriptors['counts'][vari][mm] + cvs.points(df_mo[['latitude','longitude',vari]], 'longitude', 'latitude', ds.count(vari))
                descriptors['max'][vari][mm] = np.fmax(descriptors['max'][vari][mm],cvs.points(df_mo[['latitude','longitude',vari]], 'longitude', 'latitude', ds.max(vari)))
                descriptors['min'][vari][mm] = np.fmin(descriptors['min'][vari][mm],cvs.points(df_mo[['latitude','longitude',vari]], 'longitude', 'latitude', ds.min(vari)))
                # All this mess, because addition propagates nan's in xarrays!
                mean_mm = cvs.points(df_mo[['latitude','longitude',vari]], 'longitude', 'latitude', ds.mean(vari))
                max_frame = np.fmax(descriptors['ave'][vari][mm],mean_mm)
                min_frame = np.fmin(descriptors['ave'][vari][mm],mean_mm)
                descriptors['ave'][vari][mm] = 0.5*max_frame + 0.5*min_frame
            else:
                descriptors['counts'][vari][mm] = cvs.points(df_mo[['latitude','longitude',vari]], 'longitude', 'latitude', ds.count(vari)) # <class 'xarray.core.dataarray.DataArray'>
                descriptors['max'][vari][mm] = cvs.points(df_mo[['latitude','longitude',vari]], 'longitude', 'latitude', ds.max(vari))
                descriptors['min'][vari][mm] = cvs.points(df_mo[['latitude','longitude',vari]], 'longitude', 'latitude', ds.min(vari))
                mean_mm = cvs.points(df_mo[['latitude','longitude',vari]], 'longitude', 'latitude', ds.mean(vari))
                descriptors['ave'][vari][mm] = mean_mm
                    
            # Also add monthly stats to the global aggregate