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_image_initialize(): plt.figure(dpi=100) ax = plt.subplot(111) da = dsshow(df, ds.Point("x", "y"), ax=ax) data = da.get_ds_data() assert data[0, 0] == 5 assert data[0, -1] == 5 assert data[-1, 0] == 5 assert data[-1, -1] == 5
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 correlation_plot(x, y, ax, method): ax.legend() ax.grid(True) ax.set_ylabel(f'Amplitude Aproximada {method}') ax.set_xlabel('Amplitude de Referencia') df = pd.DataFrame(dict(x=x, y=y)) da1 = dsshow(df, ds.Point('x', 'y'), norm='log', aspect='auto', ax=ax, x_range=(-100, 400), y_range=(-100, 400), shade_hook=partial(tf.dynspread, threshold=0.8)) fig.colorbar(da1, ax=ax).set_label('Densidade') return ax
def test_image_update(): fig = plt.figure(dpi=100) ax = plt.subplot(111) da = dsshow(df, ds.Point("x", "y"), ax=ax) ax.set_xlim(0, 0.5) ax.set_ylim(0, 0.5) fig.canvas.draw() data = da.get_ds_data() assert data[0, 0] == 5 assert data[0, -1] == 0 assert data[-1, 0] == 0 assert data[-1, -1] == 0
def shade_scatter( dfs, in_ax=None, figsize: float = 6, pixels: int = 1000, spread_px: int = 1, spread_threshold: float = 0.2, min_alpha: int = 10, color_map=None, color_key: dict = None, mask_values: list = None, mask_name: str = "NA", mask_color: str = "k", ax_label_size: float = 12, frame_offset: float = 0.05, spine_width: float = 0.5, spine_color: str = "k", displayed_sides: tuple = ("bottom", "left"), legend_ondata: bool = True, legend_onside: bool = True, legend_size: float = 12, legends_per_col: int = 20, titles: Union[str, List[str]] = None, title_size: int = 12, hide_title: bool = False, cbar_shrink: float = 0.6, marker_scale: float = 70, lspacing: float = 0.1, cspacing: float = 1, savename: str = None, dpi: int = 300, force_ints_as_cats: bool = True, n_columns: int = 4, w_pad: float = None, h_pad: float = None, show_fig: bool = True, ): """ Shows shaded scatter plots. If more then one dataframe is provided it will place the scatterplots in a grid. """ import datashader as dsh from datashader.mpl_ext import dsshow import datashader.transfer_functions as tf from functools import partial titles = _handle_titles_type(titles, len(dfs)) axs = _create_axes(dfs, in_ax, figsize, figsize, w_pad, h_pad, n_columns) for n, df in _iter_dataframes(dfs, mask_values, mask_name, force_ints_as_cats): dim1, dim2, vc = df.columns[:3] v = df[vc] col_map, col_key = _scatter_make_colors(v, color_map, color_key, mask_color, mask_name) if v.dtype.name == "category": agg = dsh.count_cat(vc) else: if v.nunique() == 1: agg = dsh.count(vc) else: agg = dsh.mean(vc) ax = axs[int(n / n_columns), n % n_columns] artist = dsshow( df, dsh.Point(dim1, dim2), aggregator=agg, norm="eq_hist", color_key=col_key, cmap=col_map, alpha_range=(min_alpha, 255), shade_hook=partial(tf.dynspread, threshold=spread_threshold, max_px=spread_px), plot_height=pixels, plot_width=pixels, aspect="equal", width_scale=1, height_scale=1, ax=ax, ) _scatter_label_axis(df, ax, ax_label_size, frame_offset) _scatter_cleanup(ax, spine_width, spine_color, displayed_sides) if titles is not None: title = titles[n] else: title = None _scatter_legends( df, ax, col_map, col_key, legend_ondata, legend_onside, legend_size, title, title_size, hide_title, legends_per_col, marker_scale, lspacing, cspacing, cbar_shrink, ) if savename: plt.savefig(savename, dpi=dpi, bbox_inches="tight") if show_fig: plt.show() else: return axs
def shade_scatter(dfs, in_ax=None, figsize: float = 6, pixels: int = 1000, sampling: float = 0.1, spread_px: int = 1, spread_threshold: float = 0.2, min_alpha: int = 10, color_map=None, color_key: dict = None, mask_values: list = None, mask_name: str = 'NA', mask_color: str = 'k', ax_label_size: float = 12, frame_offset: float = 0.05, spine_width: float = 0.5, spine_color: str = 'k', displayed_sides: tuple = ('bottom', 'left'), legend_ondata: bool = True, legend_onside: bool = True, legend_size: float = 12, legends_per_col: int = 20, cbar_shrink: float = 0.6, marker_scale: float = 70, lspacing: float = 0.1, cspacing: float = 1, savename: str = None, dpi: int = 300, force_ints_as_cats: bool = True, n_columns: int = 4, w_pad: float = None, h_pad: float = None, show_fig: bool = True): """ Shows shaded scatter plots. If more then one dataframe is provided it will place the scatterplots in a grid. """ import datashader as dsh from IPython.display import display from datashader.mpl_ext import dsshow import datashader.transfer_functions as tf from functools import partial if len(dfs) > 1: if in_ax is not None: logger.warning( f"'in_ax' will not be used as multiple attributes will be plotted. Using internal grid" f"layout") fig, axs = _make_grid(figsize, figsize, w_pad, h_pad, len(dfs), n_columns) else: if in_ax is None: _, axs = plt.subplots(1, 1, figsize=(figsize, figsize), squeeze=False) else: axs = in_ax for i, df in enumerate(dfs): dim1, dim2, vc = df.columns[:3] v = _scatter_fix_mask(df[vc].copy(), mask_values, mask_name) v = _scatter_fix_type(v, force_ints_as_cats) df[vc] = v col_map, col_key = _scatter_make_colors(v, color_map, color_key, mask_color, mask_name) if v.dtype.name == 'category': agg = dsh.count_cat(vc) else: if v.nunique() == 1: agg = dsh.count(vc) else: agg = dsh.mean(vc) ax = axs[int(i / n_columns), i % n_columns] artist = dsshow(df, dsh.Point(dim1, dim2), aggregator=agg, norm='eq_hist', color_key=col_key, cmap=col_map, alpha_range=(min_alpha, 255), shade_hook=partial(tf.dynspread, threshold=spread_threshold, max_px=spread_px), plot_height=pixels, plot_width=pixels, aspect='equal', width_scale=1, height_scale=1, ax=ax) _scatter_label_axis(df, ax, ax_label_size, frame_offset) _scatter_cleanup(ax, spine_width, spine_color, displayed_sides) _scatter_legends(df, ax, col_map, col_key, legend_ondata, legend_onside, legend_size, legends_per_col, marker_scale, lspacing, cspacing, cbar_shrink) if savename: plt.savefig(savename, dpi=dpi, bbox_inches='tight') if show_fig: plt.show() else: return axs