def test_directly_connect(nodes, edges): # Expect four lines starting at center (0.5, 0.5) and terminating # at a different corner and NaN data = pd.DataFrame({'x': [0.5, 0.0, np.nan, 0.5, 1.0, np.nan, 0.5, 0.0, np.nan, 0.5, 1.0, np.nan], 'y': [0.5, 1.0, np.nan, 0.5, 1.0, np.nan, 0.5, 0.0, np.nan, 0.5, 0.0, np.nan]}) expected = pd.DataFrame(data) given = directly_connect_edges(nodes, edges) assert_eq(given, expected)
def test_directly_connect_without_weights(nodes, edges): # Expect four lines starting at center (0.5, 0.5) and terminating # at a different corner and NaN data = pd.DataFrame({'x': [0.5, 0.0, np.nan, 0.5, 1.0, np.nan, 0.5, 0.0, np.nan, 0.5, 1.0, np.nan], 'y': [0.5, 1.0, np.nan, 0.5, 1.0, np.nan, 0.5, 0.0, np.nan, 0.5, 0.0, np.nan]}) expected = pd.DataFrame(data, columns=['x', 'y']) given = directly_connect_edges(nodes, edges) assert given.equals(expected)
def test_directly_connect_without_weights(nodes, edges, include_edge_id): # Expect four lines starting at center (0.5, 0.5) and terminating # at a different corner and NaN data = pd.DataFrame({'edge_id': [1.0, 1.0, np.nan, 2.0, 2.0, np.nan, 3.0, 3.0, np.nan, 4.0, 4.0, np.nan], 'x': [0.0, -100.0, np.nan, 0.0, 100.0, np.nan, 0.0, -100.0, np.nan, 0.0, 100.0, np.nan], 'y': [0.0, 100.0, np.nan, 0.0, 100.0, np.nan, 0.0, -100.0, np.nan, 0.0, -100.0, np.nan]}) columns = ['edge_id', 'x', 'y'] if include_edge_id else ['x', 'y'] expected = pd.DataFrame(data, columns=columns) given = directly_connect_edges(nodes, edges, include_edge_id=include_edge_id) assert given.equals(expected)
def test_directly_connect_with_weights(nodes, weighted_edges, include_edge_id): # Expect four lines starting at center (0.5, 0.5) and terminating # at a different corner and NaN data = pd.DataFrame({'edge_id': [1.0, 1.0, np.nan, 2.0, 2.0, np.nan, 3.0, 3.0, np.nan, 4.0, 4.0, np.nan], 'x': [0.0, -100.0, np.nan, 0.0, 100.0, np.nan, 0.0, -100.0, np.nan, 0.0, 100.0, np.nan], 'y': [0.0, 100.0, np.nan, 0.0, 100.0, np.nan, 0.0, -100.0, np.nan, 0.0, -100.0, np.nan]}) columns = ['edge_id', 'x', 'y'] if include_edge_id else ['x', 'y'] expected = pd.DataFrame(data, columns=columns) given = directly_connect_edges(nodes, weighted_edges, include_edge_id=include_edge_id) assert given.equals(expected)
def connectivity_base( x: int, y: int, edge_df: pd.DataFrame, highlights: Optional[list] = None, edge_bundling: Optional[str] = None, edge_cmap: str = "gray_r", show_points: bool = True, labels: Optional[list] = None, values: Optional[list] = None, theme: Optional[str] = None, cmap: str = "Blues", color_key: Union[dict, list, None] = None, color_key_cmap: str = "Spectral", background: str = "black", figsize: tuple = (7, 5), ax: Optional[Axes] = None, sort: str = "raw", save_show_or_return: str = "return", save_kwargs: dict = {}, ) -> Union[None, Axes]: """Plot connectivity relationships of the underlying UMAP simplicial set data structure. Internally UMAP will make use of what can be viewed as a weighted graph. This graph can be plotted using the layout provided by UMAP as a potential diagnostic view of the embedding. Currently this only works for 2D embeddings. While there are many optional parameters to further control and tailor the plotting, you need only pass in the trained/fit umap model to get results. This plot utility will attempt to do the hard work of avoiding overplotting issues and provide options for plotting the points as well as using edge bundling for graph visualization. Parameters ---------- x: `int` The first component of the embedding. y: `int` The second component of the embedding. edge_df `pd.DataFrame` The dataframe denotes the graph edge pairs. The three columns include 'source', 'target' and 'weight'. highlights: `list`, `list of list` or None (default: `None`) The list that cells will be restricted to. edge_bundling: string or None (optional, default None) The edge bundling method to use. Currently supported are None or 'hammer'. See the datashader docs on graph visualization for more details. edge_cmap: string (default 'gray_r') The name of a matplotlib colormap to use for shading/ coloring the edges of the connectivity graph. Note that the ``theme``, if specified, will override this. show_points: bool (optional False) Whether to display the points over top of the edge connectivity. Further options allow for coloring/ shading the points accordingly. labels: array, shape (n_samples,) (optional, default None) An array of labels (assumed integer or categorical), one for each data sample. This will be used for coloring the points in the plot according to their label. Note that this option is mutually exclusive to the ``values`` option. values: array, shape (n_samples,) (optional, default None) An array of values (assumed float or continuous), one for each sample. This will be used for coloring the points in the plot according to a colorscale associated to the total range of values. Note that this option is mutually exclusive to the ``labels`` option. theme: string (optional, default None) A color theme to use for plotting. A small set of predefined themes are provided which have relatively good aesthetics. Available themes are: * 'blue' * 'red' * 'green' * 'inferno' * 'fire' * 'viridis' * 'darkblue' * 'darkred' * 'darkgreen' cmap: string (optional, default 'Blues') The name of a matplotlib colormap to use for coloring or shading points. If no labels or values are passed this will be used for shading points according to density (largely only of relevance for very large datasets). If values are passed this will be used for shading according the value. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. color_key: dict or array, shape (n_categories) (optional, default None) A way to assign colors to categoricals. This can either be an explicit dict mapping labels to colors (as strings of form '#RRGGBB'), or an array like object providing one color for each distinct category being provided in ``labels``. Either way this mapping will be used to color points according to the label. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. color_key_cmap: string (optional, default 'Spectral') The name of a matplotlib colormap to use for categorical coloring. If an explicit ``color_key`` is not given a color mapping for categories can be generated from the label list and selecting a matching list of colors from the given colormap. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. background: string (optional, default 'white) The color of the background. Usually this will be either 'white' or 'black', but any color name will work. Ideally one wants to match this appropriately to the colors being used for points etc. This is one of the things that themes handle for you. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. width: int (optional, default 800) The desired width of the plot in pixels. height: int (optional, default 800) The desired height of the plot in pixels sort: `str` (optional, default `raw`) The method to reorder data so that high values points will be on top of background points. Can be one of {'raw', 'abs'}, i.e. sorted by raw data or sort by absolute values. save_show_or_return: {'show', 'save', 'return'} (default: `return`) Whether to save, show or return the figure. save_kwargs: `dict` (default: `{}`) A dictionary that will passed to the save_fig function. By default it is an empty dictionary and the save_fig function will use the {"path": None, "prefix": 'connectivity_base', "dpi": None, "ext": 'pdf', "transparent": True, "close": True, "verbose": True} as its parameters. Otherwise you can provide a dictionary that properly modify those keys according to your needs. Returns ------- result: Either return None or a matplotlib axis with the relevant plot displayed based on arguments. If you are using a notbooks and have ``%matplotlib inline`` set then this will simply display inline. """ import matplotlib.pyplot as plt import datashader as ds import datashader.transfer_functions as tf import datashader.bundling as bd dpi = plt.rcParams["figure.dpi"] if theme is not None: cmap = _themes[theme]["cmap"] color_key_cmap = _themes[theme]["color_key_cmap"] edge_cmap = _themes[theme]["edge_cmap"] background = _themes[theme]["background"] points = np.array([x, y]).T point_df = pd.DataFrame(points, columns=("x", "y")) point_size = 500.0 / np.sqrt(points.shape[0]) if point_size > 1: px_size = int(np.round(point_size)) else: px_size = 1 if show_points: edge_how = "log" else: edge_how = "eq_hist" extent = _get_extent(points) canvas = ds.Canvas( plot_width=int(figsize[0] * dpi), plot_height=int(figsize[1] * dpi), x_range=(extent[0], extent[1]), y_range=(extent[2], extent[3]), ) if edge_bundling is None: edges = bd.directly_connect_edges(point_df, edge_df, weight="weight") elif edge_bundling == "hammer": warn("Hammer edge bundling is expensive for large graphs!\n" "This may take a long time to compute!") edges = bd.hammer_bundle(point_df, edge_df, weight="weight") else: raise ValueError( "{} is not a recognised bundling method".format(edge_bundling)) edge_img = tf.shade( canvas.line(edges, "x", "y", agg=ds.sum("weight")), cmap=plt.get_cmap(edge_cmap), how=edge_how, ) edge_img = tf.set_background(edge_img, background) if show_points: point_img = _datashade_points( points, None, labels, values, highlights, cmap, color_key, color_key_cmap, None, figsize[0] * dpi, figsize[1] * dpi, True, sort=sort, ) if px_size > 1: point_img = tf.dynspread(point_img, threshold=0.5, max_px=px_size) result = tf.stack(edge_img, point_img, how="over") else: result = edge_img if ax is None: fig = plt.figure(figsize=figsize) ax = fig.add_subplot(111) _embed_datashader_in_an_axis(result, ax) ax.set(xticks=[], yticks=[]) if save_show_or_return == "save": s_kwargs = { "path": None, "prefix": "connectivity_base", "dpi": None, "ext": "pdf", "transparent": True, "close": True, "verbose": True, } s_kwargs = update_dict(s_kwargs, save_kwargs) save_fig(**s_kwargs) elif save_show_or_return == "show": plt.tight_layout() plt.show() elif save_show_or_return == "return": return ax
def connectivity( umap_object, edge_bundling=None, edge_cmap="gray_r", show_points=False, labels=None, values=None, theme=None, cmap="Blues", color_key=None, color_key_cmap="Spectral", background="white", width=800, height=800, ): """Plot connectivity relationships of the underlying UMAP simplicial set data structure. Internally UMAP will make use of what can be viewed as a weighted graph. This graph can be plotted using the layout provided by UMAP as a potential diagnostic view of the embedding. Currently this only works for 2D embeddings. While there are many optional parameters to further control and tailor the plotting, you need only pass in the trained/fit umap model to get results. This plot utility will attempt to do the hard work of avoiding overplotting issues and provide options for plotting the points as well as using edge bundling for graph visualization. Parameters ---------- umap_object: trained UMAP object A trained UMAP object that has a 2D embedding. edge_bundling: string or None (optional, default None) The edge bundling method to use. Currently supported are None or 'hammer'. See the datashader docs on graph visualization for more details. edge_cmap: string (default 'gray_r') The name of a matplotlib colormap to use for shading/ coloring the edges of the connectivity graph. Note that the ``theme``, if specified, will override this. show_points: bool (optional False) Whether to display the points over top of the edge connectivity. Further options allow for coloring/ shading the points accordingly. labels: array, shape (n_samples,) (optional, default None) An array of labels (assumed integer or categorical), one for each data sample. This will be used for coloring the points in the plot according to their label. Note that this option is mutually exclusive to the ``values`` option. values: array, shape (n_samples,) (optional, default None) An array of values (assumed float or continuous), one for each sample. This will be used for coloring the points in the plot according to a colorscale associated to the total range of values. Note that this option is mutually exclusive to the ``labels`` option. theme: string (optional, default None) A color theme to use for plotting. A small set of predefined themes are provided which have relatively good aesthetics. Available themes are: * 'blue' * 'red' * 'green' * 'inferno' * 'fire' * 'viridis' * 'darkblue' * 'darkred' * 'darkgreen' cmap: string (optional, default 'Blues') The name of a matplotlib colormap to use for coloring or shading points. If no labels or values are passed this will be used for shading points according to density (largely only of relevance for very large datasets). If values are passed this will be used for shading according the value. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. color_key: dict or array, shape (n_categories) (optional, default None) A way to assign colors to categoricals. This can either be an explicit dict mapping labels to colors (as strings of form '#RRGGBB'), or an array like object providing one color for each distinct category being provided in ``labels``. Either way this mapping will be used to color points according to the label. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. color_key_cmap: string (optional, default 'Spectral') The name of a matplotlib colormap to use for categorical coloring. If an explicit ``color_key`` is not given a color mapping for categories can be generated from the label list and selecting a matching list of colors from the given colormap. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. background: string (optional, default 'white) The color of the background. Usually this will be either 'white' or 'black', but any color name will work. Ideally one wants to match this appropriately to the colors being used for points etc. This is one of the things that themes handle for you. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. width: int (optional, default 800) The desired width of the plot in pixels. height: int (optional, default 800) The desired height of the plot in pixels Returns ------- result: matplotlib axis The result is a matplotlib axis with the relevant plot displayed. If you are using a notbooks and have ``%matplotlib inline`` set then this will simply display inline. """ if theme is not None: cmap = _themes[theme]["cmap"] color_key_cmap = _themes[theme]["color_key_cmap"] edge_cmap = _themes[theme]["edge_cmap"] background = _themes[theme]["background"] points = umap_object.embedding_ point_df = pd.DataFrame(points, columns=("x", "y")) point_size = 100.0 / np.sqrt(points.shape[0]) if point_size > 1: px_size = int(np.round(point_size)) else: px_size = 1 if show_points: edge_how = "log" else: edge_how = "eq_hist" coo_graph = umap_object.graph_.tocoo() edge_df = pd.DataFrame( np.vstack([coo_graph.row, coo_graph.col, coo_graph.data]).T, columns=("source", "target", "weight"), ) edge_df["source"] = edge_df.source.astype(np.int32) edge_df["target"] = edge_df.target.astype(np.int32) extent = _get_extent(points) canvas = ds.Canvas( plot_width=width, plot_height=height, x_range=(extent[0], extent[1]), y_range=(extent[2], extent[3]), ) if edge_bundling is None: edges = bd.directly_connect_edges(point_df, edge_df, weight="weight") elif edge_bundling == "hammer": warn("Hammer edge bundling is expensive for large graphs!\n" "This may take a long time to compute!") edges = bd.hammer_bundle(point_df, edge_df, weight="weight") else: raise ValueError( "{} is not a recognised bundling method".format(edge_bundling)) edge_img = tf.shade( canvas.line(edges, "x", "y", agg=ds.sum("weight")), cmap=plt.get_cmap(edge_cmap), how=edge_how, ) edge_img = tf.set_background(edge_img, background) if show_points: point_img = _datashade_points( points, None, labels, values, cmap, color_key, color_key_cmap, None, width, height, False, ) if px_size > 1: point_img = tf.dynspread(point_img, threshold=0.5, max_px=px_size) result = tf.stack(edge_img, point_img, how="over") else: result = edge_img font_color = _select_font_color(background) dpi = plt.rcParams["figure.dpi"] fig = plt.figure(figsize=(width / dpi, height / dpi)) ax = fig.add_subplot(111) _embed_datashader_in_an_axis(result, ax) ax.set(xticks=[], yticks=[]) ax.text( 0.99, 0.01, "UMAP: n_neighbors={}, min_dist={}".format(umap_object.n_neighbors, umap_object.min_dist), transform=ax.transAxes, horizontalalignment="right", color=font_color, ) return ax
def test_immutable_nodes(nodes, edges): # Expect nodes to remain immutable after any bundling operation original = nodes.copy() directly_connect_edges(nodes, edges) assert original.equals(nodes)
def test_immutable_nodes(nodes, edges): # Expect nodes to remain immutable after any bundling operation original = nodes.copy() directly_connect_edges(nodes, edges) assert original.equals(nodes)