Esempio n. 1
0
#     hatch_dict=None,
# )
from src.visualization import stacked_barplot

stacked_barplot(
    partition,
    mg["Merge Class"],
    color_dict=color_dict,
    plot_proportions=False,
    norm_bar_width=True,
    hatch_dict=None,
    ax=axs[0],
)
draw_networkx_nice(
    minigraph,
    "Spring-x",
    "Spring-y",
    sizes="Size",
    colors="Color",
    ax=axs[1],
    weight_scale=20,
    vmin=0.0001,
)
axs[1].set_xlabel("")
axs[1].set_ylabel("")
for ax in axs:
    ax.spines["left"].set_visible(False)
    ax.spines["bottom"].set_visible(False)

stashfig("try-clusterplot")
Esempio n. 2
0
    # add size attribute base on number of edges
    size_map = dict(path_graph.degree(weight="weight"))
    nx.set_node_attributes(g, size_map, name="Size")

    return g


path_graph = add_attributes(path_graph)

fig, ax = plt.subplots(1, 1, figsize=(20, 20))
draw_networkx_nice(
    path_graph,
    "Spring-x",
    "median_visit",
    sizes="Size",
    colors="color",
    weight_scale=0.01,
    draw_labels=False,
    ax=ax,
    size_scale=0.25,
)
ax.invert_yaxis()
stashfig(f"cluster-graph-map-c{chosen_cluster}")
plt.close()

# %% [markdown]
# #

flat_labels = fcluster(z, 100, "maxclust")
print(np.unique(flat_labels))
Esempio n. 3
0
    ax.set_ylabel("Hops")
    top_ax.set_title(title)
    stashfig(f"hop_hist-cluster={up}" + basename)

    sub_meta["colors"] = sub_meta["merge_class"].map(CLASS_COLOR_DICT)
    sub_meta_dict = sub_meta.set_index("inds").to_dict(orient="index")

    cluster_paths = []
    for i, p in enumerate(paths):
        if mask[i]:
            cluster_paths.append(p)
    path_graph = to_path_graph(cluster_paths)
    nx.set_node_attributes(path_graph, sub_meta_dict)

    for n, d in path_graph.degree():
        path_graph.nodes[n]["degree"] = d

    ax = draw_networkx_nice(
        path_graph,
        "dend_order",
        "mean_visit",
        colors="colors",
        sizes="degree",
        draw_labels=False,
        size_scale=2,
        weight_scale=0.25,
    )
    ax.invert_yaxis()
    ax.set_title(title)
    stashfig(f"path-graph-cluster={up}" + basename)
Esempio n. 4
0
            prob_df = prob_df.reindex(sort_partition_sf.index, axis=0)
            prob_df = prob_df.reindex(sort_partition_sf.index, axis=1)

            ax = probplot(
                100 * prob_df,
                fmt="2.0f",
                figsize=(20, 20),
                title=
                f"Louvain, res = {r}, counts = {counts}, weights = {weights}",
            )
            ax.set_ylabel(r"Median signal flow $\to$", fontsize=28)

            stashfig(basename + f"probplot-counts{counts}-weights{weights}")

            adjusted_partition = adjust_partition(partition)
            minigraph = to_minigraph(mg.adj,
                                     adjusted_partition,
                                     use_counts=True,
                                     size_scaler=10)
            draw_networkx_nice(
                minigraph,
                "Spring-x",
                "Signal Flow",
                sizes="Size",
                colors="Color",
                cmap="Greys",
                vmin=100,
                weight_scale=0.001,
            )
            stashfig(basename + "sbm-drawn-network")
Esempio n. 5
0
def run_experiment(graph_type=None, thresh=None, res=None):
    # load and preprocess the data
    mg = load_metagraph(graph_type, version=BRAIN_VERSION)
    edgelist = mg.to_edgelist()
    edgelist = add_max_weight(edgelist)
    edgelist = edgelist[edgelist["max_weight"] > thresh]
    mg = edgelist_to_mg(edgelist, mg.meta)
    mg = mg.make_lcc()
    mg = mg.remove_pdiff()
    g_sym = nx.to_undirected(mg.g)
    skeleton_labels = np.array(list(g_sym.nodes()))
    partition = run_louvain(g_sym, res, skeleton_labels)

    # compute signal flow for sorting purposes
    mg.meta["signal_flow"] = signal_flow(mg.adj)
    mg.meta["partition"] = partition
    partition_sf = mg.meta.groupby("partition")["signal_flow"].median()
    sort_partition_sf = partition_sf.sort_values(ascending=False)

    # common names
    basename = f"louvain-res{res}-t{thresh}-{graph_type}-"
    title = f"Louvain, {graph_type}, res = {res}, thresh = {thresh}"

    # get out some metadata
    class_label_dict = nx.get_node_attributes(g_sym, "Merge Class")
    class_labels = np.array(itemgetter(*skeleton_labels)(class_label_dict))
    lineage_label_dict = nx.get_node_attributes(g_sym, "lineage")
    lineage_labels = np.array(itemgetter(*skeleton_labels)(lineage_label_dict))
    lineage_labels = np.vectorize(lambda x: "~" + x)(lineage_labels)
    classlin_labels, color_dict, hatch_dict = augment_classes(
        skeleton_labels, class_labels, lineage_labels)

    # barplot by merge class and lineage
    fig, axs = barplot_text(
        partition,
        classlin_labels,
        color_dict=color_dict,
        plot_proportions=False,
        norm_bar_width=True,
        figsize=(24, 18),
        title=title,
        hatch_dict=hatch_dict,
    )
    stashfig(basename + "barplot-mergeclasslin-props")
    fig, axs = barplot_text(
        partition,
        class_labels,
        color_dict=color_dict,
        plot_proportions=False,
        norm_bar_width=True,
        figsize=(24, 18),
        title=title,
        hatch_dict=None,
    )
    stashfig(basename + "barplot-mergeclass-props")
    fig, axs = barplot_text(
        partition,
        class_labels,
        color_dict=color_dict,
        plot_proportions=False,
        norm_bar_width=False,
        figsize=(24, 18),
        title=title,
        hatch_dict=None,
    )
    stashfig(basename + "barplot-mergeclass-counts")
    fig, axs = barplot_text(
        partition,
        lineage_labels,
        color_dict=None,
        plot_proportions=False,
        norm_bar_width=True,
        figsize=(24, 18),
        title=title,
    )
    stashfig(basename + "barplot-lineage-props")

    # 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 probability matrices
    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)
    ax = probplot(
        100 * prob_df,
        fmt="2.0f",
        figsize=(20, 20),
        title=f"Louvain, res = {res}, counts = {counts}, weights = {weights}",
    )
    ax.set_ylabel(r"Median signal flow $\to$", fontsize=28)
    stashfig(basename + f"probplot-counts{counts}-weights{weights}")

    # plot minigraph with layout
    adjusted_partition = adjust_partition(partition, class_labels)
    minigraph = to_minigraph(mg.adj,
                             adjusted_partition,
                             use_counts=True,
                             size_scaler=10)
    draw_networkx_nice(
        minigraph,
        "Spring-x",
        "Signal Flow",
        sizes="Size",
        colors="Color",
        cmap="Greys",
        vmin=100,
        weight_scale=0.001,
    )
    stashfig(basename + "sbm-drawn-network")
Esempio n. 6
0
    nx.set_node_attributes(g, color_map, name="Color")
    return g


from src.visualization import draw_networkx_nice

from src.utils import get_blockmodel_df

for l in range(n_levels):
    labels = new_meta[f"lvl{l}_labels_side"].values
    # block_df = get_blockmodel_df(new_adj, labels, return_counts=False, use_weights=True)
    mini_g = to_minigraph(new_adj, labels, use_counts=True, use_weights=True)
    draw_networkx_nice(
        mini_g,
        "Spring-x",
        "Signal Flow",
        colors="Color",
        sizes="Size",
        weight_scale=1 / 1000,
    )

# %%
from src.visualization import plot_neurons
from src.pymaid import start_instance

lvl = 4

uni_labels = np.unique(new_meta[f"lvl{lvl}_labels"])
start_instance()

for label in uni_labels:
    plot_neurons(new_meta, f"lvl{lvl}_labels", label=label, barplot=True)
Esempio n. 7
0
    # add size attribute base on number of edges
    size_map = dict(path_graph.degree(weight="weight"))
    nx.set_node_attributes(g, size_map, name="Size")

    return g


path_graph = add_attributes(path_graph)

fig, ax = plt.subplots(1, 1, figsize=(20, 20))
draw_networkx_nice(
    path_graph,
    "AdjEvec-1",
    "median_visit",
    sizes="Size",
    colors="color",
    weight_scale=0.0001,
    draw_labels=False,
    ax=ax,
    size_scale=0.005,
)
ax.invert_yaxis()
stashfig(f"propogate-graph-map")
plt.close()

# %% [markdown]
# #

latent = ase(probs, 5, ptr=True)
pairplot(latent, labels=meta["Merge Class"].values, palette=CLASS_COLOR_DICT)