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)
def test_shade_cmap_non_categorical_alpha(agg, cmap): img = tf.shade(agg.a, how='log', cmap=cmap) sol = np.array([[ 0, 671088640, 1946157056], [2701131776, 0, 3640655872], [3976200192, 4278190080, 0]]) sol = tf.Image(sol, coords=coords, dims=dims) assert_eq_xr(img, sol)
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)
def test_shade(agg, attr, span): x = getattr(agg, attr) cmap = ['pink', 'red'] img = tf.shade(x, cmap=cmap, how='log', span=span) sol = solutions['log'] assert_eq_xr(img, sol) # Check dims/coordinates order assert list(img.coords) == ['x_axis', 'y_axis'] assert list(img.dims) == ['y_axis', 'x_axis'] img = tf.shade(x, cmap=cmap, how='cbrt', span=span) sol = solutions['cbrt'] assert_eq_xr(img, sol) img = tf.shade(x, cmap=cmap, how='linear', span=span) sol = solutions['linear'] assert_eq_xr(img, sol) # span option not supported with how='eq_hist' img = tf.shade(x, cmap=cmap, how='eq_hist') sol = tf.Image(eq_hist_sol[attr], coords=coords, dims=dims) assert_eq_xr(img, sol) img = tf.shade(x, cmap=cmap, how=lambda x, mask: np.where(mask, np.nan, x**2)) sol = np.array([[0, 4291543295, 4291148543], [4290030335, 0, 4285557503], [4282268415, 4278190335, 0]], dtype='u4') sol = tf.Image(sol, coords=coords, dims=dims) assert_eq_xr(img, sol)
def test_curve_quadmesh_manual_range(array_module): c = ds.Canvas(plot_width=4, plot_height=8, x_range=[1, 2], y_range=[1, 3]) Qx = np.array([[1, 2], [1, 2]]) Qy = np.array([[1, 1], [4, 2]]) Z = np.arange(4, dtype='int32').reshape(2, 2) da = xr.DataArray( array_module.array(Z), coords={ 'Qx': (['Y', 'X'], Qx), 'Qy': (['Y', 'X'], Qy) }, dims=['Y', 'X'], name='Z', ) x_coords = np.linspace(1.125, 1.875, 4) y_coords = np.linspace(1.125, 2.875, 8) out = xr.DataArray(array_module.array([[0., 0., 1., 1.], [0., 0., 1., 1.], [0., 0., 1., 1.], [0., 0., 1., 3.], [0., 0., 3., 3.], [0., 2., 3., 3.], [2., 2., 3., 3.], [2., 2., 3., 3.]]), coords=OrderedDict([('Qx', x_coords), ('Qy', y_coords)]), dims=['Qy', 'Qx']) res = c.quadmesh(da, x='Qx', y='Qy', agg=ds.sum('Z')) assert_eq_xr(res, out) res = c.quadmesh(da.transpose('X', 'Y', transpose_coords=False), x='Qx', y='Qy', agg=ds.sum('Z')) assert_eq_xr(res, out)
def test_curve_quadmesh_rect_autorange(array_module): c = ds.Canvas(plot_width=8, plot_height=4) coord_array = dask.array if array_module is dask.array else np Qx = coord_array.array([[1, 2], [1, 2]]) Qy = coord_array.array([[1, 1], [2, 2]]) Z = np.arange(4, dtype='int32').reshape(2, 2) da = xr.DataArray( array_module.array(Z), coords={ 'Qx': (['Y', 'X'], Qx), 'Qy': (['Y', 'X'], Qy) }, dims=['Y', 'X'], name='Z', ) y_coords = np.linspace(0.75, 2.25, 4) x_coords = np.linspace(0.625, 2.375, 8) out = xr.DataArray(array_module.array( [[0., 0., 0., 0., 1., 1., 1., 1.], [0., 0., 0., 0., 1., 1., 1., 1.], [2., 2., 2., 2., 3., 3., 3., 3.], [2., 2., 2., 2., 3., 3., 3., 3.]], dtype='f8'), coords=[('Qy', y_coords), ('Qx', x_coords)]) res = c.quadmesh(da, x='Qx', y='Qy', agg=ds.sum('Z')) assert_eq_xr(res, out) res = c.quadmesh(da.transpose('X', 'Y', transpose_coords=True), x='Qx', y='Qy', agg=ds.sum('Z')) assert_eq_xr(res, out)
def test_raster_quadmesh_autorange_chunked(): c = ds.Canvas(plot_width=8, plot_height=6) da = xr.DataArray(np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]), coords=[('b', [1, 2, 3]), ('a', [1, 2, 3, 4])], name='Z').chunk({ 'a': 2, 'b': 2 }) y_coords = np.linspace(0.75, 3.25, 6) x_coords = np.linspace(0.75, 4.25, 8) out = xr.DataArray(np.array( [[1., 1., 2., 2., 3., 3., 4., 4.], [1., 1., 2., 2., 3., 3., 4., 4.], [5., 5., 6., 6., 7., 7., 8., 8.], [5., 5., 6., 6., 7., 7., 8., 8.], [9., 9., 10., 10., 11., 11., 12., 12.], [9., 9., 10., 10., 11., 11., 12., 12.]], dtype='f8'), coords=[('b', y_coords), ('a', x_coords)]) res = c.quadmesh(da, x='a', y='b', agg=ds.sum('Z')) assert_eq_xr(res, out) # Check transpose gives same answer res = c.quadmesh(da.transpose('a', 'b'), x='a', y='b', agg=ds.sum('Z')) assert_eq_xr(res, out)
def test_raster_quadmesh_autorange_downsample(array_module): c = ds.Canvas(plot_width=4, plot_height=2) da = xr.DataArray(array_module.array([[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16], [17, 18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31, 32]]), coords=[('b', [1, 2, 3, 4]), ('a', [1, 2, 3, 4, 5, 6, 7, 8])], name='Z') y_coords = np.linspace(1.5, 3.5, 2) x_coords = np.linspace(1.5, 7.5, 4) out = xr.DataArray(array_module.array( [[1 + 2 + 9 + 10, 3 + 4 + 11 + 12, 5 + 6 + 13 + 14, 7 + 8 + 15 + 16], [ 17 + 18 + 25 + 26., 19 + 20 + 27 + 28, 21 + 22 + 29 + 30, 23 + 24 + 31 + 32 ]], dtype='f8'), coords=[('b', y_coords), ('a', x_coords)]) res = c.quadmesh(da, x='a', y='b', agg=ds.sum('Z')) assert_eq_xr(res, out) # Check transpose gives same answer res = c.quadmesh(da.transpose('a', 'b'), x='a', y='b', agg=ds.sum('Z')) assert_eq_xr(res, out)
def test_curve_quadmesh_manual_range_subpixel(array_module): c = ds.Canvas(plot_width=3, plot_height=5, x_range=[-150, 150], y_range=[-250, 250]) Qx = np.array([[1, 2], [1, 2]]) Qy = np.array([[1, 1], [4, 2]]) Z = np.arange(4, dtype='int32').reshape(2, 2) da = xr.DataArray( array_module.array(Z), coords={ 'Qx': (['Y', 'X'], Qx), 'Qy': (['Y', 'X'], Qy) }, dims=['Y', 'X'], name='Z', ) x_coords = np.linspace(-100, 100, 3) y_coords = np.linspace(-200, 200, 5) out = xr.DataArray(array_module.array([[nan, nan, nan], [nan, nan, nan], [nan, 6., nan], [nan, nan, nan], [nan, nan, nan]]), coords=OrderedDict([('Qx', x_coords), ('Qy', y_coords)]), dims=['Qy', 'Qx']) res = c.quadmesh(da, x='Qx', y='Qy', agg=ds.sum('Z')) assert_eq_xr(res, out) res = c.quadmesh(da.transpose('X', 'Y', transpose_coords=False), x='Qx', y='Qy', agg=ds.sum('Z')) assert_eq_xr(res, out)
def test_line_x_constant_autorange(DataFrame): # axis1 y constant 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 = DataFrame({ 'y0': [0, 0, 0], 'y1': [-4, 0, 4], 'y2': [0, 0, 0], }) 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_xr(agg, out)
def test_shade_cmap(agg): cmap = ['red', (0, 255, 0), '#0000FF'] img = tf.shade(agg.a, how='log', cmap=cmap) sol = np.array([[0, 4278190335, 4278236489], [4280344064, 0, 4289091584], [4292225024, 4294901760, 0]]) sol = tf.Image(sol, coords=coords, dims=dims) assert_eq_xr(img, sol)
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)
def test_raster_subpixel_quads_represented(array_module): c = ds.Canvas(plot_width=8, plot_height=4, x_range=[0.5, 16.5], y_range=[0.5, 8.5]) da = xr.DataArray(array_module.array([[1, 2, 3, 4], [5, 6, 7, 8]]), coords=[('b', [1, 2]), ('a', [1, 2, 3, 4])], name='Z') y_coords = np.linspace(1.5, 7.5, 4) x_coords = np.linspace(1.5, 15.5, 8) out = xr.DataArray(array_module.array( [[14., 22., nan, nan, nan, nan, nan, nan], [nan, nan, nan, nan, nan, nan, nan, nan], [nan, nan, nan, nan, nan, nan, nan, nan], [nan, nan, nan, nan, nan, nan, nan, nan]], dtype='f4'), coords=[('b', y_coords), ('a', x_coords)]) res = c.quadmesh(da, x='a', y='b', agg=ds.sum('Z')) assert_eq_xr(res, out) # Check transpose gives same answer res = c.quadmesh(da.transpose('a', 'b'), x='a', y='b', agg=ds.sum('Z')) assert_eq_xr(res, out)
def test_shade_mpl_cmap(agg): cm = pytest.importorskip('matplotlib.cm') img = tf.shade(agg.a, how='log', cmap=cm.viridis) sol = np.array([[0, 4283695428, 4287524142], [4287143710, 0, 4282832267], [4280213706, 4280608765, 0]]) sol = tf.Image(sol, coords=coords, dims=dims) assert_eq_xr(img, sol)
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)
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)
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)
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)
def test_count(ddf): out = xr.DataArray(np.array([[5, 5], [5, 5]], dtype='i4'), coords=coords, dims=dims) assert_eq_xr(c.points(ddf, 'x', 'y', ds.count('i32')), out) assert_eq_xr(c.points(ddf, 'x', 'y', ds.count('i64')), out) assert_eq_xr(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_xr(c.points(ddf, 'x', 'y', ds.count('f32')), out) assert_eq_xr(c.points(ddf, 'x', 'y', ds.count('f64')), out)
def test_categorical_mean(ddf): sol = np.array([[[2, nan, nan, nan], [nan, nan, 12, nan]], [[nan, 7, nan, nan], [nan, nan, nan, 17]]]) out = xr.DataArray(sol, coords=(coords + [['a', 'b', 'c', 'd']]), dims=(dims + ['cat'])) agg = c.points(ddf, 'x', 'y', ds.by('cat', ds.mean('f32'))) assert_eq_xr(agg, out) agg = c.points(ddf, 'x', 'y', ds.by('cat', ds.mean('f64'))) assert_eq_xr(agg, out)
def test_shade_zeros(array): coords = [np.array([0, 1]), np.array([2, 5])] cat_agg = tf.Image(array([[(0, 0, 0), (0, 0, 0)], [(0, 0, 0), (0, 0, 0)]], dtype='u4'), coords=(coords + [['a', 'b', 'c']]), dims=(dims + ['cats'])) colors = [(255, 0, 0), '#0000FF', 'orange'] img = tf.shade(cat_agg, color_key=colors, how='linear', min_alpha=0) sol = np.array([[5584810, 5584810], [5584810, 5584810]], dtype='u4') sol = tf.Image(sol, coords=coords, dims=dims) assert_eq_xr(img, sol)
def test_categorical_sum(ddf): sol = np.array([[[10, 0, 0, 0], [0, 0, 60, 0]], [[0, 35, 0, 0], [0, 0, 0, 85]]]) out = xr.DataArray( sol, coords=(coords + [['a', 'b', 'c', 'd']]), dims=(dims + ['cat']) ) agg = c.points(ddf, 'x', 'y', ds.by('cat', ds.sum('i32'))) assert_eq_xr(agg, out) agg = c.points(ddf, 'x', 'y', ds.by('cat', ds.sum('i64'))) assert_eq_xr(agg, out) sol = np.array([[[8.0, 0, 0, 0], [0, 0, 60.0, 0]], [[0, 35.0, 0, 0], [0, 0, 0, 85.0]]]) out = xr.DataArray( sol, coords=(coords + [['a', 'b', 'c', 'd']]), dims=(dims + ['cat']) ) agg = c.points(ddf, 'x', 'y', ds.by('cat', ds.sum('f32'))) assert_eq_xr(agg, out) agg = c.points(ddf, 'x', 'y', ds.by('cat', ds.sum('f64'))) assert_eq_xr(agg, out)
def test_auto_range_line(DataFrame): axis = ds.core.LinearAxis() lincoords = axis.compute_index( axis.compute_scale_and_translate((-10., 10.), 5), 5) ddf = 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(ddf, '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_xr(agg, out)
def test_shade_category(array): coords = [np.array([0, 1]), np.array([2, 5])] cat_agg = tf.Image(array([[(0, 12, 0), (3, 0, 3)], [(12, 12, 12), (24, 0, 0)]]), coords=(coords + [['a', 'b', 'c']]), dims=(dims + ['cats'])) colors = [(255, 0, 0), '#0000FF', 'orange'] img = tf.shade(cat_agg, color_key=colors, how='log', min_alpha=20) sol = np.array([[2583625728, 335565567], [4283774890, 3707764991]], dtype='u4') sol = tf.Image(sol, coords=coords, dims=dims) assert_eq_xr(img, sol) # Check dims/coordinates order assert list(img.coords) == ['x_axis', 'y_axis'] assert list(img.dims) == ['y_axis', 'x_axis'] colors = dict(zip('abc', colors)) img = tf.shade(cat_agg, color_key=colors, how='cbrt', min_alpha=20) sol = np.array([[2650734592, 335565567], [4283774890, 3657433343]], dtype='u4') sol = tf.Image(sol, coords=coords, dims=dims) assert_eq_xr(img, sol) img = tf.shade(cat_agg, color_key=colors, how='linear', min_alpha=20) sol = np.array([[1140785152, 335565567], [4283774890, 2701132031]], dtype='u4') sol = tf.Image(sol, coords=coords, dims=dims) assert_eq_xr(img, sol) img = tf.shade(cat_agg, color_key=colors, how='linear', min_alpha=20, span=(0, 100)) sol = np.array([[587137024, 335565567], [1515534250, 1040187647]], dtype='u4') sol = tf.Image(sol, coords=coords, dims=dims) assert_eq_xr(img, sol) img = tf.shade(cat_agg, color_key=colors, how=lambda x, m: np.where(m, np.nan, x)**2, min_alpha=20) sol = np.array([[503250944, 335565567], [4283774890, 1744830719]], dtype='u4') sol = tf.Image(sol, coords=coords, dims=dims) assert_eq_xr(img, sol)
def test_categorical_var(ddf): sol = np.array([[[ 2.5, nan, nan, nan], [ nan, nan, 2., nan]], [[ nan, 2., nan, nan], [ nan, nan, nan, 2.]]]) out = xr.DataArray( sol, coords=(coords + [['a', 'b', 'c', 'd']]), dims=(dims + ['cat'])) agg = c.points(ddf, 'x', 'y', ds.by('cat', ds.var('f32'))) assert_eq_xr(agg, out, True) agg = c.points(ddf, 'x', 'y', ds.by('cat', ds.var('f64'))) assert_eq_xr(agg, out, True)
def test_max(ddf): out = xr.DataArray( values(df_pd.i64).reshape((2, 2, 5)).max(axis=2).astype('f8').T, coords=coords, dims=dims) assert_eq_xr(c.points(ddf, 'x', 'y', ds.max('i32')), out) assert_eq_xr(c.points(ddf, 'x', 'y', ds.max('i64')), out) assert_eq_xr(c.points(ddf, 'x', 'y', ds.max('f32')), out) assert_eq_xr(c.points(ddf, 'x', 'y', ds.max('f64')), out)
def test_categorical_var(ddf): if cudf and isinstance(ddf._meta, cudf.DataFrame): pytest.skip("The 'var' reduction is yet supported on the GPU") sol = np.array([[[2.5, nan, nan, nan], [nan, nan, 2., nan]], [[nan, 2., nan, nan], [nan, nan, nan, 2.]]]) out = xr.DataArray(sol, coords=(coords + [['a', 'b', 'c', 'd']]), dims=(dims + ['cat'])) agg = c.points(ddf, 'x', 'y', ds.by('cat', ds.var('f32'))) assert_eq_xr(agg, out, True) agg = c.points(ddf, 'x', 'y', ds.by('cat', ds.var('f64'))) assert_eq_xr(agg, out, True)
def test_any(ddf): out = xr.DataArray(np.array([[True, True], [True, True]]), coords=coords, dims=dims) assert_eq_xr(c.points(ddf, 'x', 'y', ds.any('i64')), out) assert_eq_xr(c.points(ddf, 'x', 'y', ds.any('f64')), out) assert_eq_xr(c.points(ddf, 'x', 'y', ds.any()), out) out = xr.DataArray(np.array([[True, True], [True, False]]), coords=coords, dims=dims) assert_eq_xr(c.points(ddf, 'x', 'y', ds.any('empty_bin')), out)
def test_multiple_polygons_auto_range(DataFrame): df = DataFrame({ 'polygons': pd.Series([[ [[0, 0, 2, 0, 2, 2, 1, 3, 0, 0], [1, 0.25, 1, 2, 1.75, .25, 0.25, 0.25]], [[2.5, 1, 4, 1, 4, 2, 2.5, 2, 2.5, 1]], ]], dtype='MultiPolygon[float64]'), 'v': [1] }) cvs = ds.Canvas(plot_width=16, plot_height=16, x_range=[-1, 3.5], y_range=[0.1, 2]) agg = cvs.polygons(df, geometry='polygons', agg=ds.count()) axis = ds.core.LinearAxis() lincoords_x = axis.compute_index( axis.compute_scale_and_translate((-1, 3.5), 16), 16) lincoords_y = axis.compute_index( axis.compute_scale_and_translate((0.1, 2), 16), 16) sol = np.array([[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1]], dtype='i4') out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x], dims=['y', 'x']) assert_eq_xr(agg, out)
def test_points_geometry(): axis = ds.core.LinearAxis() lincoords = axis.compute_index(axis.compute_scale_and_translate((0., 2.), 3), 3) ddf = dd.from_pandas(sp.GeoDataFrame({ 'geom': pd.array( [[0, 0], [0, 1, 1, 1], [0, 2, 1, 2, 2, 2]], dtype='MultiPoint[float64]'), 'v': [1, 2, 3] }), npartitions=3) cvs = ds.Canvas(plot_width=3, plot_height=3) agg = cvs.points(ddf, geometry='geom', agg=ds.sum('v')) sol = np.array([[1, nan, nan], [2, 2, nan], [3, 3, 3]], dtype='float64') out = xr.DataArray(sol, coords=[lincoords, lincoords], dims=['y', 'x']) assert_eq_xr(agg, out)