def plotHeatmap(data, title, params={}):
    heatmap(X=data,
            inner_hier_labels=labels,
            hier_label_fontsize=8.0,
            font_scale=0.5,
            title=title,
            sort_nodes=True,
            **params)
예제 #2
0
def run_fit(
    seed,
    n_components_try_range,
    n_components_try_rdpg,
    n_block_try_range,
    directed,
    n_sims_sbm,
):
    graph = load_drosophila_left()
    if not directed:
        graph = symmetrize(graph, method="avg")
    graph = binarize(graph)

    connected = is_fully_connected(graph)

    if not connected:
        heatmap(graph)
        plt.show()
        raise ValueError("input graph not connected")

    np.random.seed(seed)

    columns = columns = [
        "n_params_gmm",
        "n_params_sbm",
        "rss",
        "mse",
        "score",
        "n_components_try",
        "n_block_try",
        "sim_ind",
    ]
    sbm_master_df = pd.DataFrame(columns=columns)
    for i in range(n_sims_sbm):
        sbm_df = select_sbm(graph,
                            n_components_try_range,
                            n_block_try_range,
                            directed=directed)
        sbm_df["sim_ind"] = i
        sbm_master_df = sbm_master_df.append(sbm_df,
                                             ignore_index=True,
                                             sort=True)

    rdpg_df = select_rdpg(graph, n_components_try_rdpg, directed)

    def metric(assignments, *args):
        return -compute_mse_from_assignments(
            assignments, graph, directed=directed)

    tsbm_master_df = select_sbm(
        graph,
        n_components_try_range,
        n_block_try_range,
        directed=directed,
        method="bc-metric",
        metric=metric,
    )
    return (sbm_master_df, rdpg_df, tsbm_master_df)
예제 #3
0
def run_fit(
    seed,
    n_components_try_range,
    n_components_try_rdpg,
    n_block_try_range,
    directed,
    n_sims_sbm,
):
    graph = load_drosophila_left()
    if not directed:
        graph = symmetrize(graph, method="avg")
    graph = binarize(graph)

    connected = is_fully_connected(graph)

    if not connected:
        heatmap(graph)
        plt.show()
        raise ValueError("input graph not connected")

    np.random.seed(seed)

    columns = columns = [
        "n_params_gmm",
        "n_params_sbm",
        "rss",
        "mse",
        "score",
        "n_components_try",
        "n_block_try",
        "sim_ind",
    ]
    sbm_master_df = pd.DataFrame(columns=columns)
    for i in range(n_sims_sbm):
        sbm_df = select_sbm(
            graph,
            n_components_try_range,
            n_block_try_range,
            directed=directed,
            rank="sweep",
        )
        sbm_df["sim_ind"] = i
        sbm_master_df = sbm_master_df.append(sbm_df,
                                             ignore_index=True,
                                             sort=True)

    save_obj(sbm_master_df, file_obs, "sbm_master_df")
    return 0
예제 #4
0
    def visualize(self, i, savedir=""):
        """
        Visualize the ith graph of self.graphs, passed-to-ranks.

        Parameters
        ----------
        i : int
        Graph to visualize.
        savedir : str, optional
        Directory to save graph into.
        If left empty, do not save.
        """

        nmax = np.max(self.graphs)

        if isinstance(i, int):
            graph = pass_to_ranks(self.graphs[i])
            sub = self.subjects[i]
            sesh = ""  # TODO
        elif isinstance(i, np.ndarray):
            graph = pass_to_ranks(i)
            sub = ""
            sesh = ""
        else:
            raise TypeError("Passed value must be integer or np.ndarray.")

        viz = heatmap(
            graph,
            title=f"sub-{sub}_session-{sesh}",
            xticklabels=True,
            yticklabels=True,
            vmin=0,
            vmax=1,
        )

        # set color of title
        viz.set_title(viz.get_title(), color="black")

        # set color of colorbar ticks
        viz.collections[0].colorbar.ax.yaxis.set_tick_params(color="black")

        # set font size and color of heatmap ticks
        for item in viz.get_xticklabels() + viz.get_yticklabels():
            item.set_color("black")
            item.set_fontsize(7)

        if savedir:
            p = Path(savedir).resolve()
            if not p.is_dir():
                p.mkdir()
            plt.savefig(
                p / f"sub-{sub}_sesh-{sesh}.png",
                facecolor="white",
                bbox_inches="tight",
                dpi=300,
            )
        else:
            plt.show()

        plt.cla()
예제 #5
0
def clustergram(
    adj, latent, prob_df, block_sum_df, true_labels, pred_labels, figsize=(20, 20)
):
    fig, ax = plt.subplots(2, 2, figsize=figsize)
    ax = ax.ravel()
    sns.set_context("talk", font_scale=2)
    color_dict = get_colors(true_labels, pred_labels)
    sankey(
        ax[0], true_labels, pred_labels, aspect=20, fontsize=16, colorDict=color_dict
    )
    ax[0].axis("off")
    ax[0].set_title("Known class sorting", fontsize=30, pad=45)

    ax[1] = heatmap(
        adj,
        transform="simple-all",
        inner_hier_labels=pred_labels,
        cbar=False,
        sort_nodes=True,
        ax=ax[1],
        cmap="PRGn_r",
        hier_label_fontsize=16,
    )
    ax[1].set_title("Sorted heatmap", fontsize=30, pad=70)

    probplot(100 * prob_df, ax=ax[2], title="Connection percentage")

    probplot(block_sum_df, ax=ax[3], title="Average synapses")
예제 #6
0
    def save_graph_png(self, graphname):
        """Saves adjacency graph, made using graspy's heatmap function, as a png. This will be saved in the qa/graphs_plotting/ directory

        Parameters
        ----------
        graphname : str
            name of the generated graph (do not include '.png')
        """

        conn_matrix = np.array(nx.to_numpy_matrix(self.g))
        conn_matrix = ptr.pass_to_ranks(conn_matrix)
        heatmap(conn_matrix)
        outpath = str(self.outdir /
                      f"qa/graphs_plotting/{Path(graphname).stem}.png")
        plt.savefig(outpath)
        plt.close()
예제 #7
0
파일: berlin_fig.py 프로젝트: bstadt/graspy
def evaluate_models(graph,
                    labels=None,
                    title=None,
                    plot_graphs=False,
                    min_comp=0,
                    max_comp=1,
                    n_comp=5):

    if plot_graphs:
        heatmap(graph, inner_hier_labels=cell_labels)

    ## Set up models to test
    non_rdpg_models = [
        EREstimator(fit_degrees=False),
        SBEstimator(fit_degrees=False),
        SBEstimator(fit_degrees=True),
    ]

    d = [6]
    rdpg_models = [RDPGEstimator(n_components=i) for i in d]
    models = non_rdpg_models + rdpg_models

    names_nonRDPG = ["ER", "SBM", "DCSBM"]
    names_RDPG = ["RDPGrank{}".format(i) for i in d]
    names = names_nonRDPG + names_RDPG

    bics = []
    log_likelihoods = []

    ## Test models
    for model, name in zip(models, names):
        m = model.fit(graph, y=labels)
        if plot_graphs:
            heatmap(m.p_mat_,
                    inner_hier_labels=labels,
                    title=(name + "P matrix"))
            heatmap(m.sample(),
                    inner_hier_labels=labels,
                    title=(name + "sample"))
        bic = m.bic(graph)
        log_likelihoods.append(m.score(graph))
        bics.append(bic)
        plt.show()
        ase = AdjacencySpectralEmbed(n_components=2)
        latent = ase.fit_transform(m.p_mat_)
        # if type(latent) is tuple:
        #     pairplot(np.concatenate((latent[0], latent[1]), axis=1))
        #     plt.show()
        # else:
        print("here")
        # plt.figure(figsize=(20, 20))
        ax = scatterplot(latent,
                         labels=cell_labels,
                         height=4,
                         alpha=0.6,
                         font_scale=1.25)
        # plt.suptitle(name, y=0.94, x=0.1, fontsize=30, horizontalalignment="left")
        plt.savefig(name + "latent.png", format="png", dpi=1000)
        plt.close()
예제 #8
0
def run_fit(seed, n_components_try_range, n_components_try_rdpg,
            n_block_try_range, directed):
    graph = load_drosophila_left()
    if not directed:
        graph = symmetrize(graph, method="avg")
    graph = binarize(graph)

    connected = is_fully_connected(graph)

    if not connected:
        heatmap(graph)
        plt.show()
        raise ValueError("input graph not connected")

    np.random.seed(seed)

    sbm_df = select_sbm(graph,
                        n_components_try_range,
                        n_block_try_range,
                        directed=directed)
    rdpg_df = select_rdpg(graph, n_components_try_rdpg, directed)
    return (sbm_df, rdpg_df)
예제 #9
0
from graspy.plot import heatmap
import matplotlib.pyplot as plt

savefig = False

left_adj, left_labels, left_full_labels = load_new_left(
    return_full_labels=True)
# right_adj, right_labels = load_new_right()

heatmap_kws = dict(cbar=False, font_scale=30)
fig, ax = plt.subplots(1, 2, figsize=(18, 10))
plt.style.use("seaborn-white")
heatmap(
    left_adj,
    inner_hier_labels=left_full_labels,
    outer_hier_labels=left_labels,
    ax=ax[1],
    hier_label_fontsize=5,
    **heatmap_kws,
)
heatmap(
    left_adj,
    inner_hier_labels=left_labels,
    ax=ax[0],
    hier_label_fontsize=10,
    **heatmap_kws,
)

plt.tight_layout()
if savefig:
    plt.savefig(
        "./maggot_models/reports/figures/raw_heatmaps/heatmaps.pdf",
예제 #10
0
mpl.rcParams["axes.spines.top"] = False

# %% [markdown]
# #

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]
예제 #11
0
    "KCs",
    "MBINs",
    "MBON",
    "MBON; CN",
    "OANs",
    "ORN mPNs",
    "ORN uPNs",
    "tPNs",
    "vPNs",
]

graph_type = "Gn"

graph = load_networkx(graph_type)

heatmap(graph, transform="simple-all")

df_adj = nx.to_pandas_adjacency(graph)
adj = df_adj.values

classes = meta_to_array(graph, "Class")
classes = classes.astype("<U64")
classes[classes == "MBON; CN"] = "MBON"
print(np.unique(classes))

nx_ids = np.array(list(graph.nodes()), dtype=int)
df_ids = df_adj.index.values.astype(int)
print("nx indexed same as pd")
print(np.array_equal(nx_ids, df_ids))
cell_ids = df_ids
예제 #12
0
                ax=ax,
                color_dict=color_dict,
                plot_proportions=False,
                norm_bar_width=True,
                category_order=sort_partition_sf.index.values,
            )
            ax.set_title(title)
            ax.set_ylabel(r"Median signal flow $\to$", fontsize=28)
            stashfig(basename + "barplot-mergeclass")

            # sorted heatmap
            heatmap(
                mg.adj,
                transform="simple-nonzero",
                figsize=(20, 20),
                inner_hier_labels=partition,
                hier_label_fontsize=10,
                title=title,
                title_pad=80,
            )
            stashfig(basename + "heatmap")

            # block probabilities
            counts = False
            weights = False
            prob_df = get_blockmodel_df(mg.adj,
                                        partition,
                                        return_counts=counts,
                                        use_weights=weights)
            prob_df = prob_df.reindex(sort_partition_sf.index, axis=0)
            prob_df = prob_df.reindex(sort_partition_sf.index, axis=1)
예제 #13
0
B = A[np.ix_(shuffle_inds,
             shuffle_inds)]  # B is a permuted version of A (corr = 1)

faq = FastApproximateQAP(
    max_iter=30,
    eps=0.0001,
    init_method="rand",
    n_init=100,
    shuffle_input=False,
    maximize=True,
)

A_found, B_found = faq.fit_predict(A, B)

reverse_shuffle = invert_permutation(shuffle_inds)
heatmap(A - B_found)  # predicted permutation
heatmap(A - B[np.ix_(reverse_shuffle, reverse_shuffle)])  # optimal permutation
plt.show()

# %% [markdown]
# # Try NMI thingy
from sklearn.manifold import MDS
from sklearn.metrics import normalized_mutual_info_score
from graspy.utils import symmetrize

n_init = 10
faq = FastApproximateQAP(max_iter=30, eps=0.0001, init_method="rand", n_init=1)

found_perms = []
scores = []
for i in range(n_init):
예제 #14
0
                   font_scale=1.5,
                   hier_label_fontsize=20,
                   cbar=False)

fig, ax = plt.subplots(4, 2, figsize=(15, 30))

# A priori SBM
ap_estimator = SBMEstimator()
ap_estimator.fit(right_graph, y=right_labels)

lik = ap_estimator.score(right_graph, clip=clip)

heatmap(
    right_graph,
    inner_hier_labels=right_labels,
    title="Right MB (by cell type)",
    ax=ax[0, 0],
    **heatmap_kws,
)
heatmap(
    ap_estimator.p_mat_,
    inner_hier_labels=right_labels,
    title=f"A priori SBM, lik = {lik:.2f}",
    ax=ax[0, 1],
    **heatmap_kws,
)

# A posteriori SBM
param_grid = dict(n_blocks=[4], n_components=list(range(1, 10)))
estimator = SBMEstimator
scorers = gen_scorers(estimator, right_graph)
예제 #15
0
#%%
community_sizes = np.empty(2 * n_blocks, dtype=int)
n_feedforward = 100
n_feedback = 100
community_sizes[::2] = n_feedforward
community_sizes[1::2] = n_feedback
community_sizes = n_blocks * [n_feedforward]
labels = n_to_labels(community_sizes)

A = sbm(community_sizes, block_probs, directed=True, loops=False)
n_verts = A.shape[0]

perm_inds = np.random.permutation(n_verts)
A_perm = A[np.ix_(perm_inds, perm_inds)]
heatmap(A, cbar=False, title="Feedforward SBM")
stashfig("ffSBM")

heatmap(A_perm, cbar=False, title="Feedforward SBM, shuffled")
stashfig("ffSBM-shuffle")

true_z = signal_flow(A)
sort_inds = np.argsort(true_z)[::-1]
heatmap(
    A[np.ix_(sort_inds, sort_inds)],
    cbar=False,
    title=r"Feedforward SBM, sorted by $A$ signal flow",
)
stashfig("ffSBM-adj-sf")

A_fake = A.copy().ravel()
low_p = 0.01
diag_p = 0.1
feedforward_p = 0.2
community_sizes = np.array(5 * [20])
block_probs = get_feedforward_B(low_p, diag_p, feedforward_p)
A = sbm(community_sizes, block_probs, directed=True, loops=False)
n_verts = A.shape[0]


plt.figure(figsize=(10, 10))
plt.title("Feedforward SBM block probability matrix")
sns.heatmap(block_probs, annot=True, square=True, cmap="Reds", cbar=False)
stashfig("ffwSBM-B")
plt.show()

heatmap(A, cbar=False, title="Feedforward SBM sampled adjacency matrix")
stashfig("ffwSBM-adj")
plt.show()

labels = n_to_labels(community_sizes).astype(str)

# %% [markdown]
# # Demonstrate that FAQ works
# Shuffle the true adjacency matrix and then show that it can be recovered


shuffle_inds = np.random.permutation(n_verts)
B = A[np.ix_(shuffle_inds, shuffle_inds)]

faq = FastApproximateQAP(
    max_iter=30,
예제 #17
0
n_blocks = 4
n_per_comm = 50
n_verts = n_blocks * n_per_comm
comm_proportions = n_blocks * [n_per_comm]
low_p = 0.02
p_mat = np.array(
    [
        [0.5, low_p, low_p, low_p],
        [low_p, 0.4, low_p, low_p],
        [low_p, low_p, 0.55, low_p],
        [low_p, low_p, low_p, 0.45],
    ]
)
A, labels = sbm(comm_proportions, p_mat, return_labels=True)
heatmap(A, inner_hier_labels=labels, cbar=False)

# %% [markdown]
# # Compute some Laplacians


# The unnormalized graph Laplacian
L = unnormalized_laplacian(A)

heatmap(L, title="Unnormalized Graph Laplacian")

# L should be strictly PSD
evals, evecs = eig(L)


show_first = 20
예제 #18
0
    "mPN;FFN-multi",
    "pLN",
    "uPN",
    "sens-ORN",
]

for comm in communities:
    comm_mg = mg.copy()
    ids = partition[partition == comm].index
    inds = comm_mg.meta.index.isin(ids)
    comm_mg = comm_mg.reindex(inds)
    is_al = comm_mg.meta["Merge Class"].isin(al_classes)
    heatmap(
        comm_mg.adj,
        inner_hier_labels=comm_mg["Merge Class"],
        outer_hier_labels=is_al,
        hier_label_fontsize=7,
        figsize=(20, 20),
        cbar=False,
    )
    adj = comm_mg.adj.copy()
    adj = symmetrize(adj, method="avg")
    sym_mg = MetaGraph(adj, comm_mg.meta)
    g_sym = sym_mg.g
    skeleton_labels = np.array(list(g_sym.nodes()))
    sub_partition, modularity = run_louvain(g_sym, 1, skeleton_labels)
    sub_partition = pd.Series(data=sub_partition, index=skeleton_labels)
    sub_partition.name = "sub-partition"
    sub_partition = sub_partition.reindex(comm_mg.meta.index)
    heatmap(
        comm_mg.adj,
        inner_hier_labels=sub_partition.values,
예제 #19
0
    square=True,
    ax=axs[1],
    cbar=False,
    cmap="RdBu_r",
    center=0,
)

sns.heatmap(adj, ax=axs[2], square=True, cbar=False, cmap="RdBu_r", center=0)

for ax in axs:
    ax.set_xticks([])
    ax.set_yticks([])

from graspy.plot import heatmap

heatmap(adj, inner_hier_labels=labels, transform="simple-all")

# %% [markdown]
# #

import networkx as nx

minigraph = nx.from_numpy_array(P, nodelist=labels)

# %% [markdown]
# #

from src.traverse import to_markov_matrix

prob_mat = to_markov_matrix(adj)
예제 #20
0
print((left_pair_ids == right_pair_ids).all())

uni_pair, counts = np.unique(sym_mg["Pair ID"], return_counts=True)
print(np.min(counts))

left_pair_ids = sym_mg["Pair ID"][:n_pairs]
right_pair_ids = sym_mg["Pair ID"][n_pairs:2 * n_pairs]

# %% [markdown]
# #

heatmap(
    sym_mg.adj,
    inner_hier_labels=sym_mg["Class 1"],
    outer_hier_labels=sym_mg["Hemisphere"],
    figsize=(30, 30),
    hier_label_fontsize=5,
    transform="binarize",
    cbar=False,
)
stashfig("heatmap-after-mods")

# %% [markdown]
# #
# ad_norm_mg, n_pairs = pair_augment(ad_norm_mg)
# ad_norm_mg = max_symmetrize(ad_norm_mg, n_pairs)
# ad_norm_mg.make_lcc()
ad_norm_lse_latent = lse(sym_mg.adj, n_components=None)
# plot_latent_sweep(ad_norm_lse_latent, n_pairs)
# remove_inds = [2, 7, 10, 15]
# ad_norm_lse_latent = remove_cols(ad_norm_lse_latent, remove_inds)
예제 #21
0
#%%
from src.data import load_left, load_right
from graspy.plot import heatmap
from graspy.utils import binarize
import matplotlib.pyplot as plt

left_adj, left_labels = load_left()
right_adj, right_labels = load_right()

heatmap_kws = dict(cbar=False, font_scale=2)
fig, ax = plt.subplots(1, 2, figsize=(18, 10))
heatmap(left_adj,
        inner_hier_labels=left_labels,
        ax=ax[0],
        title="Left",
        **heatmap_kws)
heatmap(right_adj,
        inner_hier_labels=right_labels,
        ax=ax[1],
        title="Right",
        **heatmap_kws)
plt.tight_layout()
plt.savefig(
    "./maggot_models/reports/figures/raw_heatmaps/heatmaps.pdf",
    facecolor="w",
    format="pdf",
)
예제 #22
0
B = get_feedforward_B(low_p, diag_p, feedforward_p)

plt.figure(figsize=(10, 10))
plt.title("Feedforward SBM block probability matrix")
sns.heatmap(B, annot=True, square=True, cmap="Reds", cbar=False)
plt.show()

A, labels = sbm(community_sizes,
                B,
                directed=True,
                loops=False,
                return_labels=True)
labels = labels.astype(str)
heatmap(
    A,
    cbar=False,
    inner_hier_labels=labels,
    title="Feedforward SBM sampled adjacency matrix",
)
plt.show()
# %% [markdown]
# ## Compute the signal flow metrix on the perfect feedforward network
# The energy function that this metric optimizes is for any pair of vertices, make the
# signal flow metric for node $i$ ($z_i$), equal to one greater than that for node $j$
# ($z_j$) if node $i$ is above node $j$ in the hierarchy. The basic intuition is that
# every node in a level of the hierarchy, if it were a perfect hierarchy, would be at
# the same $z$ level, which would be one greater than the $z$ level for the next layer
# in the network.
#
# Here, this is exactly what we see for the perfect feedforward network. The x-axis here
# is the second laplacian eigenvector (like Figure 2A in Varshney). Note that the order
# of the nodes in the signal flow axis matches the block labels for the simulation we
inds = np.argsort(class_counts)[::-1]
uni_class = uni_class[inds]
class_counts = class_counts[inds]

n_clusters = 12
for k in range(2, n_clusters):
    skmeans = SphericalKMeans(n_clusters=k, **skmeans_kws)
    pred_labels = skmeans.fit_predict(latent)
    pred_labels = relabel(pred_labels)
    models.append(skmeans)

    # gridplot(
    #     [adj], inner_hier_labels=pred_labels, hier_label_fontsize=18, sizes=(2, 10)
    # )
    fig, ax = plt.subplots(1, 2, figsize=(30, 18))
    heatmap(
        binarize(adj),
        inner_hier_labels=pred_labels,
        # outer_hier_labels=side_labels,
        hier_label_fontsize=18,
        ax=ax[0],
        cbar=False,
        sort_nodes=True,
    )
    uni_labels = np.unique(pred_labels)
    # survey(pred_labels, uni_class, ax=ax[1])
    survey(class_labels[:n_per_side], pred_labels, ax=ax[1])

#%%
# heatmap(adj, inner_hier_labels=pred_labels)
예제 #24
0
sns.heatmap(block_probs,
            cbar=False,
            annot=True,
            square=True,
            cmap="Reds",
            ax=ax)
ax.set_title("SBM block probabilities")

# generate graphs
A1, A2 = sbm_corr(block_members,
                  block_probs,
                  rho,
                  directed=directed,
                  loops=loops)
fig, axs = plt.subplots(1, 3, figsize=(10, 5))
heatmap(A1, ax=axs[0], cbar=False, title="Graph 1")
heatmap(A2, ax=axs[1], cbar=False, title="Graph 2")
heatmap(A1 - A2, ax=axs[2], cbar=False, title="Diff (G1 - G2)")

# shuffle for testing
node_shuffle_input = np.random.permutation(n_verts)
A2_shuffle = A2[np.ix_(node_shuffle_input, node_shuffle_input)]
node_unshuffle_input = np.array(range(n_verts))
node_unshuffle_input[node_shuffle_input] = np.array(range(n_verts))

# plot shuffled
fig, axs = plt.subplots(1, 3, figsize=(10, 5))
heatmap(A1, ax=axs[0], cbar=False, title="Graph 1")
heatmap(A2_shuffle, ax=axs[1], cbar=False, title="Graph 2 shuffled")
heatmap(A1 - A2_shuffle,
        ax=axs[2],
예제 #25
0
n_init = 50
eps = 1.0
n_samples = 20

rows = []
for i in range(n_samples):
    A1, A2 = sbm_corr(comm, B, rho)
    max_score = np.trace(A1 @ A2.T)

    shuffle_inds = np.random.choice(len(A1), replace=False, size=len(A1))
    A2_shuffle = A2[np.ix_(shuffle_inds, shuffle_inds)]

    if show_adjs:
        fig, axs = plt.subplots(1, 4, figsize=(10, 5))
        heatmap(A1, ax=axs[0], cbar=False, title="Graph 1")
        heatmap(A2, ax=axs[1], cbar=False, title="Graph 2")
        heatmap(A1 - A2, ax=axs[2], cbar=False, title="Diff (G1 - G2)")
        heatmap(A2_shuffle, ax=axs[3], cbar=False, title="Graph 2 shuffled")

        P = np.zeros_like(A1)
        P[np.arange(len(P)), shuffle_inds] = 1
        fig, axs = plt.subplots(1, 4, figsize=(20, 5))
        heatmap(A2, ax=axs[0], cbar=False, title="Graph 2")
        heatmap(P @ A2 @ P.T, ax=axs[1], cbar=False, title="P shuffled")
        heatmap(A2_shuffle, ax=axs[2], cbar=False, title="Index shuffled")
        heatmap(P.T @ A2_shuffle @ P,
                ax=axs[3],
                cbar=False,
                title="P unshuffled")
예제 #26
0
    for i in range(10):
        remove_inds = np.random.choice(len(edge_inds[0]), size=n_removed, replace=False)
        x_inds = edge_inds[0][remove_inds]
        y_inds = edge_inds[1][remove_inds]
        rand_thresh_adj = raw_adj.copy()
        rand_thresh_adj[x_inds, y_inds] = 0
        sr = stable_rank(rand_thresh_adj)
        rows.append({"stable rank": sr, "type": "random", "threshold": t})
# %% [markdown]
# #
plt.figure(figsize=(10, 5))
df = pd.DataFrame(rows)
sns.stripplot(data=df, x="threshold", y="stable rank", hue="type")
# %% [markdown]
# #
heatmap(raw_adj, transform="binarize", figsize=(20, 20))
heatmap(thresh_adj, transform="binarize", figsize=(20, 20))
print(stable_rank(raw_adj))
print(stable_rank(thresh_adj))


# %% [markdown]
# #
A = np.ones((10, 10))
stable_rank(A)

# %% [markdown]
# # Try thresholding in this new format
props = []
prop_edges = []
prop_syns = []
예제 #27
0
side_labels = np.array(
    len(left_nodes) * ["Left"] + len(right_nodes) * ["Right"])
nodelist = np.concatenate((left_nodes, right_nodes)).astype(str)

n_per_side = len(left_nodes)

class_labels = meta_df.loc[nodelist.astype(int), "Class"].values

#%% plot normalized heatmap, sorted
adj = get_paired_adj("Gn", nodelist)

heatmap(
    adj,
    inner_hier_labels=class_labels,
    outer_hier_labels=side_labels,
    transform="simple-all",
    figsize=(30, 30),
    hier_label_fontsize=10,
    sort_nodes=False,
)

#%%  plot edge weight agreement for the normalized graph

left_edges_norm, right_edges_norm = get_paired_edges(adj)
plot_df = pd.DataFrame(columns=("Left edge weight", "Right edge weight"))
plot_df["Left edge weight"] = left_edges_norm
plot_df["Right edge weight"] = right_edges_norm

plt.figure(figsize=(15, 15))
sns.set_context("talk", font_scale=1.5)
sns.scatterplot(x="Left edge weight",
예제 #28
0
# %% [markdown]
# #

from cdlib import algorithms, viz
import networkx as nx
import matplotlib.pyplot as plt
from graspy.plot import heatmap

g = nx.LFR_benchmark_graph(1000, 3, 1.5, 0.7, min_community=20, average_degree=5)
coms = algorithms.leiden(g)


def nc_to_label(coms):
    nodelist = []
    comlist = []
    com_map = coms.to_node_community_map()
    for node, assignment in com_map.items():
        assignment = assignment[0]
        nodelist.append(node)
        comlist.append(assignment)
    return nodelist, comlist


nodelist, labels = nc_to_label(coms)
adj = nx.to_numpy_array(g, nodelist=nodelist)

heatmap(adj, inner_hier_labels=labels)
예제 #29
0
for ul in np.unique(labels):
    dc_sum = degree_corrections[labels == ul].sum()
    degree_corrections[labels == ul] /= dc_sum

adj, labels = sbm(
    community_sizes,
    scaling_factor * block_probs,
    directed=False,
    loops=False,
    dc=degree_corrections,
    return_labels=True,
)
heatmap(
    adj,
    cbar=False,
    title="Adjacency matrix",
    inner_hier_labels=labels,
    sort_nodes=True,
    hier_label_fontsize=16,
)
mean_degree = np.mean(np.sum(adj, axis=0))
print(f"Mean degree: {mean_degree:.3f}")

# %% [markdown]
# ## Double checking the model parameters
# Below is a quick sanity check that the graph we sampled has block probabilities that are
# close to what we set originally if we undo the rescaling step.
# %% double checking on model params
sbme = SBMEstimator(directed=False, loops=False)
sbme.fit(adj, y=labels)
block_p_hat = sbme.block_p_
block_heatmap(block_p_hat, title=r"Observed $\hat{B}$")
예제 #30
0
    "vPNs": "PN",
    "Unidentified": "Unknown",
    "Other": "Unknown",
}
simple_class_labels = np.array(itemgetter(*class_labels)(name_map))
side_map = {"left": "Left", "right": "Right"}
side_labels = np.array(itemgetter(*side_labels)(side_map))

adj = adj_df.values

sns.set_palette("deep")
heatmap(
    adj,
    inner_hier_labels=simple_class_labels,
    outer_hier_labels=side_labels,
    transform="binarize",
    figsize=(30, 30),
    hier_label_fontsize=10,
    sort_nodes=False,
    cbar=False,
)

gridplot(
    [adj],
    inner_hier_labels=simple_class_labels,
    outer_hier_labels=side_labels,
    transform="simple-all",
    height=20,
    hier_label_fontsize=10,
    sort_nodes=False,
    sizes=(2, 10),
)