(
    data,
    graph,
    n_components,
    component_labels,
    dim,
    random_state,
    metric="euclidean",
    metric_kwds={},
)
return result
Specialised layout algorithm for dealing with graphs with many connected components.
    This will first fid relative positions for the components by spectrally embedding
    their centroids, then spectrally embed each individual connected component positioning
    them according to the centroid embeddings. This provides a decent embedding of each
    component while placing the components in good relative positions to one another.

    Parameters
    ----------
    data: array of shape (n_samples, n_features)
        The source data -- required so we can generate centroids for each
        connected component of the graph.

    graph: sparse matrix
        The adjacency matrix of the graph to be emebdded.

    n_components: int
        The number of distinct components to be layed out.

    component_labels: array of shape (n_samples)
        For each vertex in the graph the label of the component to
        which the vertex belongs.

    dim: int
        The chosen embedding dimension.

    metric: string or callable (optional, default 'euclidean')
        The metric used to measure distances among the source data points.

    metric_kwds: dict (optional, default {})
        Keyword arguments to be passed to the metric function.


    Returns
    -------
    embedding: array of shape (n_samples, dim)
        The initial embedding of ``graph``.
Specialised layout algorithm for dealing with graphs with many connected components.
    This will first fid relative positions for the components by spectrally embedding
    their centroids, then spectrally embed each individual connected component positioning
    them according to the centroid embeddings. This provides a decent embedding of each
    component while placing the components in good relative positions to one another.
Specialised layout algorithm for dealing with graphs with many connected components . This will first fid relative positions for the components by spectrally embedding their centroids then spectrally embed each individual connected component positioning them according to the centroid embeddings . This provides a decent embedding of each component while placing the components in good relative positions to one another .
def multi_component_layout(
    data,
    graph,
    n_components,
    component_labels,
    dim,
    random_state,
    metric="euclidean",
    metric_kwds={},
):
    """Specialised layout algorithm for dealing with graphs with many connected components.
    This will first fid relative positions for the components by spectrally embedding
    their centroids, then spectrally embed each individual connected component positioning
    them according to the centroid embeddings. This provides a decent embedding of each
    component while placing the components in good relative positions to one another.

    Parameters
    ----------
    data: array of shape (n_samples, n_features)
        The source data -- required so we can generate centroids for each
        connected component of the graph.

    graph: sparse matrix
        The adjacency matrix of the graph to be emebdded.

    n_components: int
        The number of distinct components to be layed out.

    component_labels: array of shape (n_samples)
        For each vertex in the graph the label of the component to
        which the vertex belongs.

    dim: int
        The chosen embedding dimension.

    metric: string or callable (optional, default 'euclidean')
        The metric used to measure distances among the source data points.

    metric_kwds: dict (optional, default {})
        Keyword arguments to be passed to the metric function.


    Returns
    -------
    embedding: array of shape (n_samples, dim)
        The initial embedding of ``graph``.
    """

    result = np.empty((graph.shape[0], dim), dtype=np.float32)

    if n_components > 2 * dim:
        meta_embedding = component_layout(
            data,
            n_components,
            component_labels,
            dim,
            random_state,
            metric=metric,
            metric_kwds=metric_kwds,
        )
    else:
        k = int(np.ceil(n_components / 2.0))
        base = np.hstack([np.eye(k), np.zeros((k, dim - k))])
        meta_embedding = np.vstack([base, -base])[:n_components]

    for label in range(n_components):
        component_graph = graph.tocsr()[component_labels == label, :].tocsc()
        component_graph = component_graph[:, component_labels == label].tocoo()

        distances = pairwise_distances([meta_embedding[label]], meta_embedding)
        data_range = distances[distances > 0.0].min() / 2.0

        if component_graph.shape[0] < 2 * dim or component_graph.shape[0] <= dim + 1:
            result[component_labels == label] = (
                random_state.uniform(
                    low=-data_range,
                    high=data_range,
                    size=(component_graph.shape[0], dim),
                )
                + meta_embedding[label]
            )
            continue

        diag_data = np.asarray(component_graph.sum(axis=0))
        # standard Laplacian
        # D = scipy.sparse.spdiags(diag_data, 0, graph.shape[0], graph.shape[0])
        # L = D - graph
        # Normalized Laplacian
        I = scipy.sparse.identity(component_graph.shape[0], dtype=np.float64)
        D = scipy.sparse.spdiags(
            1.0 / np.sqrt(diag_data),
            0,
            component_graph.shape[0],
            component_graph.shape[0],
        )
        L = I - D * component_graph * D

        k = dim + 1
        num_lanczos_vectors = max(2 * k + 1, int(np.sqrt(component_graph.shape[0])))
        try:
            eigenvalues, eigenvectors = scipy.sparse.linalg.eigsh(
                L,
                k,
                which="SM",
                ncv=num_lanczos_vectors,
                tol=1e-4,
                v0=np.ones(L.shape[0]),
                maxiter=graph.shape[0] * 5,
            )
            order = np.argsort(eigenvalues)[1:k]
            component_embedding = eigenvectors[:, order]
            expansion = data_range / np.max(np.abs(component_embedding))
            component_embedding *= expansion
            result[component_labels == label] = (
                component_embedding + meta_embedding[label]
            )
        except scipy.sparse.linalg.ArpackError:
            warn(
                "WARNING: spectral initialisation failed! The eigenvector solver\n"
                "failed. This is likely due to too small an eigengap. Consider\n"
                "adding some noise or jitter to your data.\n\n"
                "Falling back to random initialisation!"
            )
            result[component_labels == label] = (
                random_state.uniform(
                    low=-data_range,
                    high=data_range,
                    size=(component_graph.shape[0], dim),
                )
                + meta_embedding[label]
            )

    return result
def multi_component_layout ( data , graph , n_components , component_labels , dim , random_state , metric = "euclidean" , metric_kwds = { } , ) : result = np . empty ( ( graph . shape [ 0 ] , dim ) , dtype = np . float32 ) if n_components > 2 * dim : meta_embedding = component_layout ( data , n_components , component_labels , dim , random_state , metric = metric , metric_kwds = metric_kwds , ) else : k = int ( np . ceil ( n_components / 2.0 ) ) base = np . hstack ( [ np . eye ( k ) , np . zeros ( ( k , dim - k ) ) ] ) meta_embedding = np . vstack ( [ base , - base ] ) [ : n_components ] for label in range ( n_components ) : component_graph = graph . tocsr ( ) [ component_labels == label , : ] . tocsc ( ) component_graph = component_graph [ : , component_labels == label ] . tocoo ( ) distances = pairwise_distances ( [ meta_embedding [ label ] ] , meta_embedding ) data_range = distances [ distances > 0.0 ] . min ( ) / 2.0 if component_graph . shape [ 0 ] < 2 * dim or component_graph . shape [ 0 ] <= dim + 1 : result [ component_labels == label ] = ( random_state . uniform ( low = - data_range , high = data_range , size = ( component_graph . shape [ 0 ] , dim ) , ) + meta_embedding [ label ] ) continue diag_data = np . asarray ( component_graph . sum ( axis = 0 ) ) # standard Laplacian # D = scipy.sparse.spdiags(diag_data, 0, graph.shape[0], graph.shape[0]) # L = D - graph # Normalized Laplacian I = scipy . sparse . identity ( component_graph . shape [ 0 ] , dtype = np . float64 ) D = scipy . sparse . spdiags ( 1.0 / np . sqrt ( diag_data ) , 0 , component_graph . shape [ 0 ] , component_graph . shape [ 0 ] , ) L = I - D * component_graph * D k = dim + 1 num_lanczos_vectors = max ( 2 * k + 1 , int ( np . sqrt ( component_graph . shape [ 0 ] ) ) ) try : eigenvalues , eigenvectors = scipy . sparse . linalg . eigsh ( L , k , which = "SM" , ncv = num_lanczos_vectors , tol = 1e-4 , v0 = np . ones ( L . shape [ 0 ] ) , maxiter = graph . shape [ 0 ] * 5 , ) order = np . argsort ( eigenvalues ) [ 1 : k ] component_embedding = eigenvectors [ : , order ] expansion = data_range / np . max ( np . abs ( component_embedding ) ) component_embedding *= expansion result [ component_labels == label ] = ( component_embedding + meta_embedding [ label ] ) except scipy . sparse . linalg . ArpackError : warn ( "WARNING: spectral initialisation failed! The eigenvector solver\n" "failed. This is likely due to too small an eigengap. Consider\n" "adding some noise or jitter to your data.\n\n" "Falling back to random initialisation!" ) result [ component_labels == label ] = ( random_state . uniform ( low = - data_range , high = data_range , size = ( component_graph . shape [ 0 ] , dim ) , ) + meta_embedding [ label ] ) return result