Exemplo n.º 1
0
def preprocess_adjs(adjs, method="ase"):
    adjs = [pass_to_ranks(a) for a in adjs]
    adjs = [a + 1 / a.size for a in adjs]
    if method == "ase":
        adjs = [augment_diagonal(a) for a in adjs]
    elif method == "lse":
        adjs = [to_laplace(a) for a in adjs]
    return adjs
Exemplo n.º 2
0
def preprocess_adjs(adjs, method="ase"):
    """Preprocessing necessary prior to embedding a graph, opetates on a list

    Parameters
    ----------
    adjs : list of adjacency matrices
        [description]
    method : str, optional
        [description], by default "ase"

    Returns
    -------
    [type]
        [description]
    """
    adjs = [pass_to_ranks(a) for a in adjs]
    adjs = [a + 1 / a.size for a in adjs]
    if method == "ase":
        adjs = [augment_diagonal(a) for a in adjs]
    elif method == "lse":  # haven't really used much. a few params to look at here
        adjs = [to_laplace(a) for a in adjs]
    return adjs
Exemplo n.º 3
0
            ax=ax,
            legend=False,
            palette=palette,
            s=10,
            alpha=0.8,
            linewidth=0,
        )
        ax.set(xticks=[], yticks=[], xlabel="")
    fig.suptitle(method, y=0.93)

#%% [markdown]
# ## Appendix: double checking some assumptions

# %% try with the actual eigendecomp

L = to_laplace(adj, form="I-DAD")

evals, evecs = eigh(L)

method = "lse"
embedding = evecs[:, :k]


plot_df = make_plot_df(embedding)
plot_dfs[method] = plot_df

fig, axs = plt.subplots(2, 2, figsize=(16, 8))
for i, ax in enumerate(axs.ravel()):
    sns.scatterplot(
        data=plot_df,
        x="ind",
Exemplo n.º 4
0
n_per_comm = [100, 100, 100]
n_verts = np.sum(n_per_comm)
block_probs = np.array([[0.4, 0.1, 0.1], [0.1, 0.4, 0.1], [0.1, 0.1, 0.4]])

adj, labels = sbm(n_per_comm, block_probs, return_labels=True)

fig, axs = plt.subplots(1, 2, figsize=(10, 5))
sns.heatmap(
    block_probs, annot=True, cmap="RdBu_r", center=0, square=True, ax=axs[0], cbar=False
)
heatmap(adj, inner_hier_labels=labels, ax=axs[1], cbar=False)

#%%

I_DAD = to_laplace(adj, form="I-DAD")
DAD = to_laplace(adj, form="DAD")

fig, axs = plt.subplots(1, 2, figsize=(10, 5))
heatmap_kws = dict(inner_hier_labels=labels, cbar=False)
heatmap(I_DAD, ax=axs[0], **heatmap_kws)
heatmap(DAD, ax=axs[1], **heatmap_kws)


def eig(A):
    evals, evecs = np.linalg.eig(A)
    sort_inds = np.argsort(evals)[::-1]
    evals = evals[sort_inds]
    evecs = evecs[:, sort_inds]
    return evals, evecs
Exemplo n.º 5
0
pairplot(latent, labels=labels, title="LSE" + base_title)
save("LSE")
#%% MASE
embed_graphs = []
for graph in split_graph_list:
    graph = preprocess(graph)
    embed_graphs.append(graph)

mase = MultipleASE(n_components=n_components, scaled=True)
shared_latent = mase.fit_transform(embed_graphs)
shared_latent = np.concatenate(shared_latent, axis=-1)
labels = get_simple(split_graph_list[0])
pairplot(shared_latent, labels=labels, title="MASE" + base_title)
save("MASE")
#%% MLSE
regularizer = 1
embed_graphs = []
for graph in split_graph_list:
    graph = preprocess(graph)
    laplacian = to_laplace(graph, "R-DAD", regularizer=regularizer)
    embed_graphs.append(laplacian)

mase = MultipleASE(n_components=n_components, scaled=False)
shared_latent = mase.fit_transform(embed_graphs)
shared_latent = np.concatenate(shared_latent, axis=-1)
labels = get_simple(split_graph_list[0])
pairplot(shared_latent, labels=labels, title="MLSE" + base_title)
save("MLSE")

#%%
Exemplo n.º 6
0
print("Embedding graph")

n_components = 4
regularizer = 2
ptr = True
binary = False
embed_graph = lcc_graph_t
if ptr:
    embed_graph = pass_to_ranks(embed_graph)
if binary:
    embed_graph = binarize(embed_graph)

# lse = LaplacianSpectralEmbed(
#     n_components=n_components, form="R-DAD", regularizer=regularizer
# )
embed_graph = to_laplace(embed_graph, form="R-DAD", regularizer=regularizer)

heatmap(
    embed_graph,
    figsize=(20, 20),
    inner_hier_labels=lcc_simple_classes,
    outer_hier_labels=lcc_hemisphere,
    hier_label_fontsize=10,
    sort_nodes=True,
)
gridplot(
    [embed_graph],
    height=15,
    inner_hier_labels=lcc_simple_classes,
    outer_hier_labels=lcc_hemisphere,
    hier_label_fontsize=10,
Gn_adj = import_graph(Gn)
Gn_adj = remove_loops(Gn_adj)
# Gn = pass_to_ranks(Gn)
Gn_adj = binarize(Gn_adj)

# plot graph
heatmap(
    Gn_adj,
    inner_hier_labels=simple_classes,
    sort_nodes=True,
    figsize=(15, 15),
    title=use_graph,
)

# compute and plot directed normalized laplacian
L = to_laplace(Gn_adj, form="R-DAD", regularizer=1)
heatmap(
    L,
    inner_hier_labels=simple_classes,
    sort_nodes=True,
    figsize=(15, 15),
    title="Normalized Laplacian",
)

# compute lse, plot with simple class labels
lse = AdjacencySpectralEmbed(n_components=n_components)
latent = lse.fit_transform(L)
latent = np.concatenate(latent, axis=-1)
pairplot(latent, labels=simple_classes)