(
    umap_object,
    diagnostic_type="pca",
    nhood_size=15,
    local_variance_threshold=0.8,
    ax=None,
    cmap="viridis",
    point_size=None,
    background="white",
    width=800,
    height=800,
)
return ax
Provide a diagnostic plot or plots for a UMAP embedding.
    There are a number of plots that can be helpful for diagnostic
    purposes in understanding your embedding. Currently these are
    restricted to methods of coloring a scatterplot of the
    embedding to show more about how the embedding is representing
    the data. The first class of such plots uses a linear method
    that preserves global structure well to embed the data into
    three dimensions, and then interprets such coordinates as a
    color space -- coloring the points by their location in the
    linear global structure preserving embedding. In such plots
    one should look for discontinuities of colour, and consider
    overall global gradients of color. The diagnostic types here
    are ``'pca'``, ``'ica'``, and ``'vq'`` (vector quantization).

    The second class consider the local neighbor structure. One
    can either look at how well the neighbor structure is
    preserved, or how the estimated local dimension of the data
    varies. Both of these are available, although the local
    dimension estimation is the preferred option. You can
    access these are diagnostic types ``'local_dim'`` and
    ``'neighborhood'``.

    Finally the diagnostic type ``'all'`` will provide a
    grid of diagnostic plots.

    Parameters
    ----------
    umap_object: trained UMAP object
        A trained UMAP object that has a 2D embedding.

    diagnostic_type: str (optional, default 'pca')
        The type of diagnostic plot to show. The options are
           * 'pca'
           * 'ica'
           * 'vq'
           * 'local_dim'
           * 'neighborhood'
           * 'all'

    nhood_size: int (optional, default 15)
        The size of neighborhood to compare for local
        neighborhood preservation estimates.

    local_variance_threshold: float (optional, default 0.8)
        To estimate the local dimension we consider a PCA of
        the local neighborhood and estimate the dimension
        as that which provides ``local_variance_threshold``
        or more of the ``variance_explained_ratio``.

    ax: matlotlib axis (optional, default None)
        A matplotlib axis to plot to, or, if None, a new
        axis will be created and returned.

    cmap: str (optional, default 'viridis')
        The name of a matplotlib colormap to use for coloring
        points if the ``'local_dim'`` or ``'neighborhood'``
        option are selected.

    point_size: int (optional, None)
        If provided this will fix the point size for the
        plot(s). If None then a suitable point size will
        be estimated from the data.

    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.
Provide a diagnostic plot or plots for a UMAP embedding.
    There are a number of plots that can be helpful for diagnostic
    purposes in understanding your embedding. Currently these are
    restricted to methods of coloring a scatterplot of the
    embedding to show more about how the embedding is representing
    the data. The first class of such plots uses a linear method
    that preserves global structure well to embed the data into
    three dimensions, and then interprets such coordinates as a
    color space -- coloring the points by their location in the
    linear global structure preserving embedding. In such plots
    one should look for discontinuities of colour, and consider
    overall global gradients of color. The diagnostic types here
    are ``'pca'``, ``'ica'``, and ``'vq'`` (vector quantization).
Provide a diagnostic plot or plots for a UMAP embedding . There are a number of plots that can be helpful for diagnostic purposes in understanding your embedding . Currently these are restricted to methods of coloring a scatterplot of the embedding to show more about how the embedding is representing the data . The first class of such plots uses a linear method that preserves global structure well to embed the data into three dimensions and then interprets such coordinates as a color space -- coloring the points by their location in the linear global structure preserving embedding . In such plots one should look for discontinuities of colour and consider overall global gradients of color . The diagnostic types here are pca ica and vq ( vector quantization ) .
def diagnostic(
    umap_object,
    diagnostic_type="pca",
    nhood_size=15,
    local_variance_threshold=0.8,
    ax=None,
    cmap="viridis",
    point_size=None,
    background="white",
    width=800,
    height=800,
):
    """Provide a diagnostic plot or plots for a UMAP embedding.
    There are a number of plots that can be helpful for diagnostic
    purposes in understanding your embedding. Currently these are
    restricted to methods of coloring a scatterplot of the
    embedding to show more about how the embedding is representing
    the data. The first class of such plots uses a linear method
    that preserves global structure well to embed the data into
    three dimensions, and then interprets such coordinates as a
    color space -- coloring the points by their location in the
    linear global structure preserving embedding. In such plots
    one should look for discontinuities of colour, and consider
    overall global gradients of color. The diagnostic types here
    are ``'pca'``, ``'ica'``, and ``'vq'`` (vector quantization).

    The second class consider the local neighbor structure. One
    can either look at how well the neighbor structure is
    preserved, or how the estimated local dimension of the data
    varies. Both of these are available, although the local
    dimension estimation is the preferred option. You can
    access these are diagnostic types ``'local_dim'`` and
    ``'neighborhood'``.

    Finally the diagnostic type ``'all'`` will provide a
    grid of diagnostic plots.

    Parameters
    ----------
    umap_object: trained UMAP object
        A trained UMAP object that has a 2D embedding.

    diagnostic_type: str (optional, default 'pca')
        The type of diagnostic plot to show. The options are
           * 'pca'
           * 'ica'
           * 'vq'
           * 'local_dim'
           * 'neighborhood'
           * 'all'

    nhood_size: int (optional, default 15)
        The size of neighborhood to compare for local
        neighborhood preservation estimates.

    local_variance_threshold: float (optional, default 0.8)
        To estimate the local dimension we consider a PCA of
        the local neighborhood and estimate the dimension
        as that which provides ``local_variance_threshold``
        or more of the ``variance_explained_ratio``.

    ax: matlotlib axis (optional, default None)
        A matplotlib axis to plot to, or, if None, a new
        axis will be created and returned.

    cmap: str (optional, default 'viridis')
        The name of a matplotlib colormap to use for coloring
        points if the ``'local_dim'`` or ``'neighborhood'``
        option are selected.

    point_size: int (optional, None)
        If provided this will fix the point size for the
        plot(s). If None then a suitable point size will
        be estimated from the data.

    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.
    """

    points = _get_embedding(umap_object)

    if points.shape[1] != 2:
        raise ValueError("Plotting is currently only implemented for 2D embeddings")

    if point_size is None:
        point_size = 100.0 / np.sqrt(points.shape[0])

    if ax is None:
        dpi = plt.rcParams["figure.dpi"]
        if diagnostic_type in ("local_dim", "neighborhood"):
            width *= 1.1

    font_color = _select_font_color(background)

    if ax is None and diagnostic_type != "all":
        fig = plt.figure()
        ax = fig.add_subplot(111)

    if diagnostic_type == "pca":
        color_proj = sklearn.decomposition.PCA(n_components=3).fit_transform(
            umap_object._raw_data
        )
        color_proj -= np.min(color_proj)
        color_proj /= np.max(color_proj, axis=0)

        ax.scatter(points[:, 0], points[:, 1], s=point_size, c=color_proj, alpha=0.66)
        ax.set_title("Colored by RGB coords of PCA embedding")
        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,
        )
        ax.set(xticks=[], yticks=[])

    elif diagnostic_type == "ica":
        color_proj = sklearn.decomposition.FastICA(n_components=3).fit_transform(
            umap_object._raw_data
        )
        color_proj -= np.min(color_proj)
        color_proj /= np.max(color_proj, axis=0)

        ax.scatter(points[:, 0], points[:, 1], s=point_size, c=color_proj, alpha=0.66)
        ax.set_title("Colored by RGB coords of FastICA embedding")
        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,
        )
        ax.set(xticks=[], yticks=[])

    elif diagnostic_type == "vq":
        color_projector = sklearn.cluster.KMeans(n_clusters=3).fit(
            umap_object._raw_data
        )
        color_proj = sklearn.metrics.pairwise_distances(
            umap_object._raw_data, color_projector.cluster_centers_
        )
        color_proj -= np.min(color_proj)
        color_proj /= np.max(color_proj, axis=0)

        ax.scatter(points[:, 0], points[:, 1], s=point_size, c=color_proj, alpha=0.66)
        ax.set_title("Colored by RGB coords of Vector Quantization")
        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,
        )
        ax.set(xticks=[], yticks=[])

    elif diagnostic_type == "neighborhood":
        highd_indices, highd_dists = _nhood_search(umap_object, nhood_size)
        tree = sklearn.neighbors.KDTree(points)
        lowd_dists, lowd_indices = tree.query(points, k=nhood_size)
        accuracy = _nhood_compare(
            highd_indices.astype(np.int32), lowd_indices.astype(np.int32)
        )

        vmin = np.percentile(accuracy, 5)
        vmax = np.percentile(accuracy, 95)
        ax.scatter(
            points[:, 0],
            points[:, 1],
            s=point_size,
            c=accuracy,
            cmap=cmap,
            vmin=vmin,
            vmax=vmax,
        )
        ax.set_title("Colored by neighborhood Jaccard index")
        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,
        )
        ax.set(xticks=[], yticks=[])
        norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)
        mappable = matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)
        mappable.set_array(accuracy)
        plt.colorbar(mappable, ax=ax)

    elif diagnostic_type == "local_dim":
        highd_indices, highd_dists = _nhood_search(umap_object, umap_object.n_neighbors)
        data = umap_object._raw_data
        local_dim = np.empty(data.shape[0], dtype=np.int64)
        for i in range(data.shape[0]):
            pca = sklearn.decomposition.PCA().fit(data[highd_indices[i]])
            local_dim[i] = np.where(
                np.cumsum(pca.explained_variance_ratio_) > local_variance_threshold
            )[0][0]
        vmin = np.percentile(local_dim, 5)
        vmax = np.percentile(local_dim, 95)
        ax.scatter(
            points[:, 0],
            points[:, 1],
            s=point_size,
            c=local_dim,
            cmap=cmap,
            vmin=vmin,
            vmax=vmax,
        )
        ax.set_title("Colored by approx local dimension")
        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,
        )
        ax.set(xticks=[], yticks=[])
        norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)
        mappable = matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)
        mappable.set_array(local_dim)
        plt.colorbar(mappable, ax=ax)

    elif diagnostic_type == "all":
        cols = int(len(_diagnostic_types) ** 0.5 // 1)
        rows = len(_diagnostic_types) // cols + 1

        fig, axs = plt.subplots(rows, cols, figsize=(10, 10), constrained_layout=True)
        axs = axs.flat
        for ax in axs[len(_diagnostic_types) :]:
            ax.remove()
        for ax, plt_type in zip(axs, _diagnostic_types):
            diagnostic(
                umap_object,
                diagnostic_type=plt_type,
                ax=ax,
                point_size=point_size / 4.0,
            )

    else:
        raise ValueError(
            "Unknown diagnostic; should be one of "
            + ", ".join(list(_diagnostic_types))
            + ' or "all"'
        )

    return ax
def diagnostic ( umap_object , diagnostic_type = "pca" , nhood_size = 15 , local_variance_threshold = 0.8 , ax = None , cmap = "viridis" , point_size = None , background = "white" , width = 800 , height = 800 , ) : points = _get_embedding ( umap_object ) if points . shape [ 1 ] != 2 : raise ValueError ( "Plotting is currently only implemented for 2D embeddings" ) if point_size is None : point_size = 100.0 / np . sqrt ( points . shape [ 0 ] ) if ax is None : dpi = plt . rcParams [ "figure.dpi" ] if diagnostic_type in ( "local_dim" , "neighborhood" ) : width *= 1.1 font_color = _select_font_color ( background ) if ax is None and diagnostic_type != "all" : fig = plt . figure ( ) ax = fig . add_subplot ( 111 ) if diagnostic_type == "pca" : color_proj = sklearn . decomposition . PCA ( n_components = 3 ) . fit_transform ( umap_object . _raw_data ) color_proj -= np . min ( color_proj ) color_proj /= np . max ( color_proj , axis = 0 ) ax . scatter ( points [ : , 0 ] , points [ : , 1 ] , s = point_size , c = color_proj , alpha = 0.66 ) ax . set_title ( "Colored by RGB coords of PCA embedding" ) 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 , ) ax . set ( xticks = [ ] , yticks = [ ] ) elif diagnostic_type == "ica" : color_proj = sklearn . decomposition . FastICA ( n_components = 3 ) . fit_transform ( umap_object . _raw_data ) color_proj -= np . min ( color_proj ) color_proj /= np . max ( color_proj , axis = 0 ) ax . scatter ( points [ : , 0 ] , points [ : , 1 ] , s = point_size , c = color_proj , alpha = 0.66 ) ax . set_title ( "Colored by RGB coords of FastICA embedding" ) 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 , ) ax . set ( xticks = [ ] , yticks = [ ] ) elif diagnostic_type == "vq" : color_projector = sklearn . cluster . KMeans ( n_clusters = 3 ) . fit ( umap_object . _raw_data ) color_proj = sklearn . metrics . pairwise_distances ( umap_object . _raw_data , color_projector . cluster_centers_ ) color_proj -= np . min ( color_proj ) color_proj /= np . max ( color_proj , axis = 0 ) ax . scatter ( points [ : , 0 ] , points [ : , 1 ] , s = point_size , c = color_proj , alpha = 0.66 ) ax . set_title ( "Colored by RGB coords of Vector Quantization" ) 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 , ) ax . set ( xticks = [ ] , yticks = [ ] ) elif diagnostic_type == "neighborhood" : highd_indices , highd_dists = _nhood_search ( umap_object , nhood_size ) tree = sklearn . neighbors . KDTree ( points ) lowd_dists , lowd_indices = tree . query ( points , k = nhood_size ) accuracy = _nhood_compare ( highd_indices . astype ( np . int32 ) , lowd_indices . astype ( np . int32 ) ) vmin = np . percentile ( accuracy , 5 ) vmax = np . percentile ( accuracy , 95 ) ax . scatter ( points [ : , 0 ] , points [ : , 1 ] , s = point_size , c = accuracy , cmap = cmap , vmin = vmin , vmax = vmax , ) ax . set_title ( "Colored by neighborhood Jaccard index" ) 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 , ) ax . set ( xticks = [ ] , yticks = [ ] ) norm = matplotlib . colors . Normalize ( vmin = vmin , vmax = vmax ) mappable = matplotlib . cm . ScalarMappable ( norm = norm , cmap = cmap ) mappable . set_array ( accuracy ) plt . colorbar ( mappable , ax = ax ) elif diagnostic_type == "local_dim" : highd_indices , highd_dists = _nhood_search ( umap_object , umap_object . n_neighbors ) data = umap_object . _raw_data local_dim = np . empty ( data . shape [ 0 ] , dtype = np . int64 ) for i in range ( data . shape [ 0 ] ) : pca = sklearn . decomposition . PCA ( ) . fit ( data [ highd_indices [ i ] ] ) local_dim [ i ] = np . where ( np . cumsum ( pca . explained_variance_ratio_ ) > local_variance_threshold ) [ 0 ] [ 0 ] vmin = np . percentile ( local_dim , 5 ) vmax = np . percentile ( local_dim , 95 ) ax . scatter ( points [ : , 0 ] , points [ : , 1 ] , s = point_size , c = local_dim , cmap = cmap , vmin = vmin , vmax = vmax , ) ax . set_title ( "Colored by approx local dimension" ) 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 , ) ax . set ( xticks = [ ] , yticks = [ ] ) norm = matplotlib . colors . Normalize ( vmin = vmin , vmax = vmax ) mappable = matplotlib . cm . ScalarMappable ( norm = norm , cmap = cmap ) mappable . set_array ( local_dim ) plt . colorbar ( mappable , ax = ax ) elif diagnostic_type == "all" : cols = int ( len ( _diagnostic_types ) ** 0.5 // 1 ) rows = len ( _diagnostic_types ) // cols + 1 fig , axs = plt . subplots ( rows , cols , figsize = ( 10 , 10 ) , constrained_layout = True ) axs = axs . flat for ax in axs [ len ( _diagnostic_types ) : ] : ax . remove ( ) for ax , plt_type in zip ( axs , _diagnostic_types ) : diagnostic ( umap_object , diagnostic_type = plt_type , ax = ax , point_size = point_size / 4.0 , ) else : raise ValueError ( "Unknown diagnostic; should be one of " + ", " . join ( list ( _diagnostic_types ) ) + ' or "all"' ) return ax