def quick_gridmap(adj, labels): gridplot( [adj], height=20, sizes=(10, 20), inner_hier_labels=labels, sort_nodes=False, palette="deep", legend=False, )
cols = [str(c).strip(" ") for c in cols] input_counts_df.columns = cols print(input_counts_df.head()) #%% Import the raw graphs nx_graphs_raw = {} df_graphs_raw = {} for graph_type in graph_types: print(graph_type) edgelist_path = data_path / data_date_graphs / (graph_type + ".csv") adj = pd.read_csv(edgelist_path, index_col=0) graph = df_to_nx(adj, meta_data_dict) nx_graphs_raw[graph_type] = graph df_graphs_raw[graph_type] = adj gridplot([adj.values], title=graph_type) print() #%% Normalize weights for the raw graphs df_graphs_norm = {} nx_graphs_norm = {} input_counts = input_counts_df["axon_inputs"].values input_counts[input_counts == 0] = 1 for graph_type in ["axon-axon", "dendrite-axon"]: print(graph_type) df_adj_raw = df_graphs_raw[graph_type] if (input_counts_df.index.values == adj.index.values).all(): print("Same indexing!") adj_raw = df_adj_raw.values adj_norm = adj_raw / input_counts[np.newaxis, :]
n_init=n_init, shuffle_input=True, gmp=True, ) start = timer() faq.fit(template, adj) end = timer() print(f"FAQ took {(end - start)/60.0} minutes") perm_inds = faq.perm_inds_ # perm_inds = unshuffle(shuffle_inds, shuffle_perm_inds) # %% [markdown] # # from graspy.plot import gridplot gridplot([adj[np.ix_(perm_inds, perm_inds)]]) stashfig("unshuffled-real-heatmap-faq") # %% [markdown] # # from src.hierarchy import signal_flow z = signal_flow(adj) sort_inds = np.argsort(z)[::-1] gridplot([adj[np.ix_(sort_inds, sort_inds)]]) stashfig("unshuffled-real-heatmap-sf") # %% [markdown] # #
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), ) #%% Gridplot for the colors graph_types = ["Gad", "Gaa", "Gdd", "Gda"] color_graphs = [] for t in graph_types: g = load_networkx(t) matched_g = g.subgraph(nodelist) g_df = nx.to_pandas_adjacency(matched_g, nodelist=nodelist) color_graphs.append(g_df.values) gp = gridplot(
sym_ipsi_adj = (left_left_adj + right_right_adj) / 2 sym_contra_adj = (left_right_adj + right_left_adj) / 2 sym_adj = adj.copy() sym_adj[:n_pairs, :n_pairs] = sym_ipsi_adj sym_adj[n_pairs:2 * n_pairs, n_pairs:2 * n_pairs] = sym_ipsi_adj sym_adj[:n_pairs, n_pairs:2 * n_pairs] = sym_contra_adj sym_adj[n_pairs:2 * n_pairs, :n_pairs] = sym_contra_adj side_labels = mg["Hemisphere"] f = gridplot( [sym_adj], transform="binarize", height=20, sizes=(10, 20), inner_hier_labels=side_labels, sort_nodes=False, palette="deep", legend=False, ) stashfig("sym-adj") mg = MetaGraph(sym_adj, mg.meta) # %% [markdown] # # Preprocess graph n_verts = mg.n_verts mg.make_lcc() print(f"Removed {n_verts - mg.n_verts} when finding the LCC") # old_n_verts = sym_adj.shape[0] # sym_adj, class_labels, side_labels = preprocess_graph( # sym_adj, class_labels, side_labels
) from_paths += paths path_graph = to_path_graph(from_paths) path_graphs.append(path_graph) # %% [markdown] # # path_adjs = [] for pg in path_graphs: path_adj = nx.to_numpy_array(pg, nodelist=meta["idx"].values) path_adjs.append(path_adj) from graspy.plot import gridplot gridplot(path_adjs, labels=from_group_names, transform="simple-all", sizes=(5, 10)) from graspy.utils import get_multigraph_union_lcc graphs, inds = get_multigraph_union_lcc(path_adjs, return_inds=True) from graspy.embed import MultipleASE from graspy.utils import pass_to_ranks graphs = [pass_to_ranks(g) for g in graphs] mase = MultipleASE() embed = mase.fit_transform(graphs) # %% [markdown] # #
left_left_adj = adj[:n_pairs, :n_pairs] left_right_adj = adj[:n_pairs, n_pairs:2 * n_pairs] right_right_adj = adj[n_pairs:2 * n_pairs, n_pairs:2 * n_pairs] right_left_adj = adj[n_pairs:2 * n_pairs, :n_pairs] sym_ipsi_adj = (left_left_adj + right_right_adj) / 2 sym_contra_adj = (left_right_adj + right_left_adj) / 2 sym_adj = adj.copy() sym_adj[:n_pairs, :n_pairs] = sym_ipsi_adj sym_adj[n_pairs:2 * n_pairs, n_pairs:2 * n_pairs] = sym_ipsi_adj sym_adj[:n_pairs, n_pairs:2 * n_pairs] = sym_contra_adj sym_adj[n_pairs:2 * n_pairs, :n_pairs] = sym_contra_adj gridplot([sym_adj], transform="binarize", height=20, sizes=(10, 20)) # %% [markdown] # # Preprocess graph old_n_verts = sym_adj.shape[0] sym_adj, class_labels, side_labels = preprocess_graph(sym_adj, class_labels, side_labels) n_verts = sym_adj.shape[0] print(f"Removed {old_n_verts - n_verts} nodes") # %% [markdown] # # Embedding n_verts = sym_adj.shape[0] latent, laplacian = lse(sym_adj, N_COMPONENTS, regularizer=None, ptr=PTR) latent_dim = latent.shape[1] // 2 screeplot( laplacian,
full_graph = load_june(graph_type) simple_classes = get_simple(full_graph) hemisphere = meta_to_array(full_graph, "Hemisphere") n_nodes = len(full_graph) n_edges = len(full_graph.edges()) print(f"Number of nodes: {n_nodes}") print(f"Number of edges: {n_edges}") gridplot( [full_graph], inner_hier_labels=simple_classes, outer_hier_labels=hemisphere, title="Full graph, Gadn, PTR-simple-all", **gridplot_kws, ) #%% print("Performing thresholding") full_graph_t = full_graph.copy() full_graph_t = import_graph(full_graph_t) full_graph_t[full_graph_t < 0.01] = 0 n_edges_t = np.count_nonzero(full_graph_t) print(f"Number of edges remaining: {n_edges_t}") print(f"Removed {(n_edges - n_edges_t) / n_edges} of edges") gridplot( [full_graph_t],
print(f"Number of nodes: {n_nodes}") print(f"Number of edges: {n_edges}") heatmap( full_graph, transform="simple-all", figsize=(20, 20), inner_hier_labels=simple_classes, outer_hier_labels=hemisphere, hier_label_fontsize=10, ) gridplot( [full_graph], transform="simple-all", height=15, inner_hier_labels=simple_classes, outer_hier_labels=hemisphere, hier_label_fontsize=10, sizes=(1, 10), ) #%% print("Performing thresholding") full_graph_t = full_graph.copy() full_graph_t = import_graph(full_graph_t) full_graph_t[full_graph_t < 0.01] = 0 n_edges_t = np.count_nonzero(full_graph_t) print(f"Number of edges remaining: {n_edges_t}") print(f"Removed {(n_edges - n_edges_t) / n_edges} of edges") heatmap(
# %% [markdown] # ## fig, ax = plt.subplots(1, 1, figsize=(15, 15)) meta["Total degree"] = -meta["Total degree"] adjplot( binarize(adj), # binarize the data, even this may be hard to see meta=meta, sort_class="merge_class", plot_type="heatmap", item_order="Total degree", # order nodes w/in group by degree ax=ax, cbar=False, ticks=False, colors="merge_class", palette=CLASS_COLOR_DICT, gridline_kws=dict(linewidth=0.2, color="grey", linestyle="--"), ) # %% [markdown] # ## Can also try Graspy funcs # I have been meaning to update them to be more like the above ones # simple version of the above, has some more handy features (see documentation) heatmap(adj, transform="binarize", cbar=False) # this one is like "scattermap" but expects a list of graphs # can plot multiple adjacencies on top of one another in different colors, for example gridplot([adj], transform="simple-nonzero", sizes=(2, 4))
stashfig("4-color-split-gridmap-hemisphere") # %% [markdown] # # Plot the adjacency matrices with class labels class_labels = mgs[0].meta["Class 1"].copy() uni_labels, counts = np.unique(class_labels, return_counts=True) count_thresh = 20 counts[counts < count_thresh] = -1 is_low_labels = np.zeros(len(uni_labels), dtype=bool) is_low_labels[counts == -1] = True label_map = dict(zip(uni_labels, is_low_labels)) is_low_class = np.array(itemgetter(*class_labels)(label_map)) class_labels[is_low_class] = "Other" mg.meta["Simple class"] = class_labels gridplot( [mg.adj for mg in mgs], labels=graph_names, sizes=(10, 15), height=20, hier_label_fontsize=15, inner_hier_labels=mg["Simple class"], palette="deep", # legend=False, ) stashfig("4-color-gridplot-class")
ax=ax[i], title=graph_type, cbar=False, title_pad=70, font_scale=2, ) # plt.tight_layout() plt.savefig("4-color.png", facecolor="w", format="png") labels = ["A -> A", "A -> D", "D -> D", "D -> A"] gridplot( plot_graphs, labels=labels, inner_hier_labels=simple_classes, height=40, sizes=(5, 5), transform="binarize", title="Right Drosophila larva, 4-color graph", title_pad=190, font_scale=3.5, ) plt.savefig("gridplot-4color.pdf", format="pdf", facecolor="w", bbox_inches="tight") #%% g = graph_list[0] right_nodes = [x for x, y in g.nodes(data=True) if y["Hemisphere"] == "right"] left_subgraph = g.subgraph(right_nodes) #%% # %config InlineBackend.figure_format = 'png'