def test_curve_quadmesh_rect_autorange(): c = ds.Canvas(plot_width=8, plot_height=4) Qx = np.array([[1, 2], [1, 2]]) Qy = np.array([[1, 1], [2, 2]]) Z = np.arange(4, dtype='int32').reshape(2, 2) da = xr.DataArray( 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(np.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.] ], ), coords=[('Qy', y_coords), ('Qx', x_coords)]) res = c.quadmesh(da, x='Qx', y='Qy', agg=ds.sum('Z')) assert res.equals(out) res = c.quadmesh(da.transpose('X', 'Y'), x='Qx', y='Qy', agg=ds.sum('Z')) assert res.equals(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_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_curve_quadmesh_manual_range_subpixel(): 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( 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(np.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 res.equals(out) res = c.quadmesh(da.transpose('X', 'Y'), x='Qx', y='Qy', agg=ds.sum('Z')) assert res.equals(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_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_sum(): out = df.i32.reshape((2, 2, 5)).sum(axis=2, dtype='i8').T eq(c.points(df, 'x', 'y', ds.sum('i32')), out) eq(c.points(df, 'x', 'y', ds.sum('i64')), out) out = np.nansum(df.f64.reshape((2, 2, 5)), axis=2).T eq(c.points(df, 'x', 'y', ds.sum('f32')), out) eq(c.points(df, 'x', 'y', ds.sum('f64')), out)
def test_sum(): out = df.i32.reshape((2, 2, 5)).sum(axis=2, dtype='i8').T eq(c.points(ddf, 'x', 'y', agg=ds.sum('i32')).agg, out) eq(c.points(ddf, 'x', 'y', agg=ds.sum('i64')).agg, out) out = out.astype('f8') eq(c.points(ddf, 'x', 'y', agg=ds.sum('f32')).agg, out) eq(c.points(ddf, 'x', 'y', agg=ds.sum('f64')).agg, out)
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_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)))
def test_curve_quadmesh_manual_range(): 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( 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(np.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 res.equals(out) res = c.quadmesh(da.transpose('X', 'Y'), x='Qx', y='Qy', agg=ds.sum('Z')) assert res.equals(out)
def test_sum(): out = xr.DataArray(df.i32.values.reshape((2, 2, 5)).sum(axis=2, dtype='f8').T, coords=coords, dims=dims) assert_eq(c.points(df, 'x', 'y', ds.sum('i32')), out) assert_eq(c.points(df, 'x', 'y', ds.sum('i64')), out) out = xr.DataArray(np.nansum(df.f64.values.reshape((2, 2, 5)), axis=2).T, coords=coords, dims=dims) assert_eq(c.points(df, 'x', 'y', ds.sum('f32')), out) assert_eq(c.points(df, 'x', 'y', ds.sum('f64')), out)
def test_sum(): out = xr.DataArray(df.i32.values.reshape((2, 2, 5)).sum(axis=2, dtype='f8').T, coords=coords, dims=dims) assert_eq(c.points(ddf, 'x', 'y', ds.sum('i32')), out) assert_eq(c.points(ddf, 'x', 'y', ds.sum('i64')), out) out = xr.DataArray(np.nansum(df.f64.values.reshape((2, 2, 5)), axis=2).T, coords=coords, dims=dims) assert_eq(c.points(ddf, 'x', 'y', ds.sum('f32')), out) assert_eq(c.points(ddf, 'x', 'y', ds.sum('f64')), out)
def test_line_agg_sum_axis1_none_constant(): axis = ds.core.LinearAxis() lincoords = axis.compute_index(axis.compute_scale_and_translate((-3., 3.), 7), 7) df = pd.DataFrame({ 'x0': [4, -4], 'x1': [0, 0], 'x2': [-4, 4], 'y0': [0, 0], 'y1': [-4, 4], 'y2': [0, 0], 'v': [7, 9] }) cvs = ds.Canvas(plot_width=7, plot_height=7, x_range=(-3, 3), y_range=(-3, 3)) agg = cvs.line(df, ['x0', 'x1', 'x2'], ['y0', 'y1', 'y2'], ds.sum('v'), axis=1) nan = np.nan sol = np.array([[nan, nan, 7, nan, 7, nan, nan], [nan, 7, nan, nan, nan, 7, nan], [7, nan, nan, nan, nan, nan, 7], [nan, nan, nan, nan, nan, nan, nan], [9, nan, nan, nan, nan, nan, 9], [nan, 9, nan, nan, nan, 9, nan], [nan, nan, 9, nan, 9, nan, nan]], dtype='float32') out = xr.DataArray(sol, coords=[lincoords, lincoords], dims=['y', 'x']) assert_eq(agg, out)
def create_plot(data, out, width): """Creates a figure of the ZVV transit network without any grouping. Args: data: a csv file containing data usable for line plots out: the generated imnage is saved here Returns: None """ plot_data = pd.read_csv(data, low_memory=False) x_range = (plot_data.shape_pt_lon.min(), plot_data.shape_pt_lon.max()) y_range = (plot_data.shape_pt_lat.min(), plot_data.shape_pt_lat.max()) height = int(round(width * (y_range[1] - y_range[0]) / (x_range[1] - x_range[0]))) cvs = ds.Canvas( plot_width=width, plot_height=height, x_range=x_range, y_range=y_range ) agg = cvs.line( plot_data, 'shape_pt_lon', 'shape_pt_lat', agg=ds.sum('times_taken') ) image = tf.shade(agg, cmap=cc.fire, how='eq_hist') if out.endswith('.png'): out = out[:-4] export_image(image, filename=out, background='black')
def test(): usi = "mzspec:MSV000085852:QC_0:scan:3548" remote_link, local_filename = download._resolve_usi(usi) all_xic_values = [["278.1902", 278.1902]] xic_tolerance = 0.5 xic_ppm_tolerance = 10 xic_tolerance_unit = "Da" rt_min = 2 rt_max = 8 polarity_filter = "Positive" xic_df, ms2_data = xic._xic_file_slow(local_filename, all_xic_values, xic_tolerance, xic_ppm_tolerance, xic_tolerance_unit, rt_min, rt_max, polarity_filter) xic_df["i"] = xic_df["XIC 278.1902"] xic_df["USI"] = 1 cvs = ds.Canvas(plot_width=100, plot_height=1) agg = cvs.points(xic_df, 'rt', 'USI', agg=ds.sum("i")) import plotly.express as px fig = px.imshow(agg, origin='lower', y=[usi], labels={'color': 'Log10(abundance)'}, height=600, template="plotly_white") fig.write_image("test.png")
def plot_datashade_dnb_figure(dataset, colormap, width=900, height=600): return datashade( hv.Scatter(dataset, kdims=["x"], vdims=['y', 'values']), aggregator=ds.sum('values'), cmap=colormap, ).opts( width=width, height=height, )
def test_raster_quadmesh_upsamplex_and_downsampley(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]]), coords=[('b', [1, 2, 3, 4]), ('a', [1, 2])], name='Z') x_coords = np.linspace(0.75, 2.25, 4) y_coords = np.linspace(1.5, 3.5, 2) out = xr.DataArray(array_module.array( [[4., 4., 6., 6.], [12., 12., 14., 14.]], 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_multiple_aggregates(df): agg = c.points(df, 'x', 'y', ds.summary(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_xr(agg.f64_mean, f(np.nanmean(values(df.f64).reshape((2, 2, 5)), axis=2).T)) assert_eq_xr(agg.i32_sum, f(values(df.i32).reshape((2, 2, 5)).sum(axis=2, dtype='f8').T)) assert_eq_xr(agg.i32_count, f(np.array([[5, 5], [5, 5]], dtype='i4')))
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'))
def test_rect_quadmesh_autorange_reversed(array_module): c = ds.Canvas(plot_width=8, plot_height=4) da = xr.DataArray(array_module.array([[1, 2, 3, 4], [5, 6, 7, 8]]), coords=[('b', [-1, -2]), ('a', [-1, -2, -3, -8])], name='Z') y_coords = np.linspace(-2.25, -0.75, 4) x_coords = np.linspace(-9.875, -1.125, 8) out = xr.DataArray(array_module.array( [[8., 8., 8., 8., 7., 7., 6., 5.], [8., 8., 8., 8., 7., 7., 6., 5.], [4., 4., 4., 4., 3., 3., 2., 1.], [4., 4., 4., 4., 3., 3., 2., 1.]], 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, close=True) # 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, close=True)
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))
def update_plot(): global current_net global global_step global level_step_counter global layout while True: if level_step_counter > 300: level_step_counter = 0 if global_step > 0: current_net = current_net.give_positions_to_parent( perturbation=0.01) if current_net is None: break layout = NetworkForceLayout(current_net, spring_optimal_distance=1., attraction_normalization=0., repulsion=1., step_size=0.05, step_discount_factor=0.95, centering=0., drag=0.5, noise=0., mac=0.5, num_dim=2, force_limit=1., distance_exponent=2.) layout.simulation_step() positions = layout.x.cpu().numpy()[np.newaxis, :].copy() positions -= positions.min() positions /= positions.max() edges = np.zeros((current_net.num_connections * 3, 3), dtype=np.float32) edges[0::3, :2] = positions[0, current_net.connections[:, 0], :] edges[1::3, :2] = positions[0, current_net.connections[:, 1], :] edges[2::3, :] = float('nan') edges[0::3, 2] = 1. edges[1::3, 2] = 1. edges = pd.DataFrame(data=edges) edges.columns = ['x', 'y', 'val'] edges_lines = canvas.line(edges, 'x', 'y', agg=ds.sum('val')).values.astype(np.float32) edges_lines[edges_lines != edges_lines] = 0. edges_lines = pow(edges_lines / edges_lines.max(), 0.25) edges_lines = gaussian_filter(edges_lines, sigma=0.8) viz.set_new_node_positions( positions, new_weights=current_net.weights[None, :].numpy()) viz.edge_textures = edges_lines[np.newaxis, :, :] viz.update() level_step_counter += 1 global_step += 1
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.shade(agg)) color_fn = lambda agg: tf.shade(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.shade(agg))
def test_rect_quadmesh_autorange_reversed(): c = ds.Canvas(plot_width=8, plot_height=4) da = xr.DataArray([[1, 2, 3, 4], [5, 6, 7, 8]], coords=[('b', [-1, -2]), ('a', [-1, -2, -3, -4])], name='Z') y_coords = np.linspace(-2.25, -0.75, 4) x_coords = np.linspace(-4.25, -0.75, 8) out = xr.DataArray(np.array( [[8., 8., 7., 7., 6., 6., 5., 5.], [8., 8., 7., 7., 6., 6., 5., 5.], [4., 4., 3., 3., 2., 2., 1., 1.], [4., 4., 3., 3., 2., 2., 1., 1.]], dtype='i4'), coords=[('b', y_coords), ('a', x_coords)]) res = c.quadmesh(da, x='a', y='b', agg=ds.sum('Z')) assert res.equals(out) # Check transpose gives same answer res = c.quadmesh(da.transpose('a', 'b'), x='a', y='b', agg=ds.sum('Z')) assert res.equals(out)
def test_raster_quadmesh_autorange(array_module): c = ds.Canvas(plot_width=8, plot_height=4) 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(0.75, 2.25, 4) x_coords = np.linspace(0.75, 4.25, 8) out = xr.DataArray(array_module.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.]], 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_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 aggregate_particles(df, axis1, axis2, bincount): """ Uses datashader's points aggregator to produce a 2D histogram""" #<---- note that the "bins" are 100,000 / 1000 comoving kpc, or 100 cKpc in each dimension cvs = dshader.Canvas(plot_width=bincount, plot_height=bincount, x_range=[0, 25000], y_range=[0, 25000]) agg = cvs.points(df, axis1, axis2, dshader.sum('mass')) agg = agg.fillna( 0.) #<---- this is necessary because agg contains empty bins as NaNs return agg
def test_subpixel_quads_represented(): c = ds.Canvas(plot_width=8, plot_height=4, x_range=[0, 16], y_range=[0, 8]) da = xr.DataArray([[1, 2, 3, 4], [5, 6, 7, 8]], coords=[('b', [1, 2]), ('a', [1, 2, 3, 4])], name='Z') y_coords = np.linspace(1, 7, 4) x_coords = np.linspace(1, 15, 8) out = xr.DataArray(np.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 res.equals(out) # Check transpose gives same answer res = c.quadmesh(da.transpose('a', 'b'), x='a', y='b', agg=ds.sum('Z')) assert res.equals(out)
def test_rect_quadmesh_manual_range(array_module): c = ds.Canvas(plot_width=8, plot_height=4, x_range=[1, 3], y_range=[-1, 3]) da = xr.DataArray(array_module.array([[1, 2, 3, 4], [5, 6, 7, 8]]), coords=[('b', [1, 2]), ('a', [1, 2, 3, 8])], name='Z') y_coords = np.linspace(-0.5, 2.5, 4) x_coords = np.linspace(1.125, 2.875, 8) out = xr.DataArray(array_module.array( [[nan, nan, nan, nan, nan, nan, nan, nan], [1., 1., 2., 2., 2., 2., 3., 3.], [5., 5., 6., 6., 6., 6., 7., 7.], [nan, nan, nan, nan, nan, nan, nan, nan]], 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, close=True) # 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, close=True)
def _aggregate_lcms_map(filename, min_rt, max_rt, min_mz, max_mz, polarity_filter="None", map_plot_quantization_level="Medium"): import time start_time = time.time() ms1_results, number_spectra, msn_results = _gather_lcms_data_cached( filename, min_rt, max_rt, min_mz, max_mz, polarity_filter=polarity_filter) end_time = time.time() print("READ FILE", end_time - start_time) start_time = time.time() min_size = min(number_spectra, int(max_mz - min_mz)) width = max(min(min_size * 4, 500), 20) height = max(min(int(min_size * 1.75), 500), 20) min_size = min(number_spectra, int(max_mz - min_mz) * 4) width = max(min(min_size * 4, 750), 120) height = max(min(int(min_size * 1.75), 500), 80) if map_plot_quantization_level == "Low": width = int(width / 2) height = int(height / 2) elif map_plot_quantization_level == "High": width = int(width * 2) height = int(height * 2) print("Datashader Len", len(ms1_results)) cvs = ds.Canvas(plot_width=width, plot_height=height) agg = cvs.points(ms1_results, 'rt', 'mz', agg=ds.sum("i")) print("Datashader Agg", time.time() - start_time) start_time = time.time() zero_mask = agg.values == 0 agg.values = np.log10(agg.values, where=np.logical_not(zero_mask)) agg_dict = agg.to_dict() print("Datashader Post Processing", time.time() - start_time) return agg_dict, msn_results
def test_load_feather(): start = time.time() ms1_df = pd.read_feather("{}.feather".format(FILENAME_PREFIX)) ms1_df = ms1_df[(ms1_df["ms1_rt"] > RT_MIN) & (ms1_df["ms1_rt"] < RT_MAX) & (ms1_df["ms1_mz"] < 500) & (ms1_df["ms1_mz"] > 300)] width = 500 height = 500 cvs = ds.Canvas(plot_width=width, plot_height=height) agg = cvs.points(ms1_df, 'ms1_rt', 'ms1_mz', agg=ds.sum("ms1_i")) print("TIMING", time.time() - start) print(ms1_df)
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]) 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], [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=True), x='Qx', y='Qy', agg=ds.sum('Z')) assert_eq_xr(res, out)
def test_curve_quadmesh_autorange_chunked(): c = ds.Canvas(plot_width=4, plot_height=8) 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( np.array(Z), coords={ 'Qx': (['Y', 'X'], Qx), 'Qy': (['Y', 'X'], Qy) }, dims=['Y', 'X'], name='Z', ).chunk({ 'X': 2, 'Y': 1 }) x_coords = np.linspace(0.75, 2.25, 4) y_coords = np.linspace(-0.5, 6.5, 8) out = xr.DataArray(np.array([[nan, nan, nan, nan], [0., 0., nan, nan], [0., 0., 1., 1.], [0., 0., 3., 3.], [2., 2., 3., nan], [2., 2., nan, nan], [2., 2., nan, nan], [2., 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=True), x='Qx', y='Qy', agg=ds.sum('Z')) assert_eq_xr(res, out)