def run_experiment(seed=None, graph_type=None, threshold=None, param_key=None): np.random.seed(seed) if BLIND: temp_param_key = param_key.replace( " ", "") # don't want spaces in filenames savename = f"{temp_param_key}-cell-types-" title = param_key else: savename = f"{graph_type}-t{threshold}-cell-types" title = f"{graph_type}, threshold = {threshold}" mg = load_metagraph(graph_type, version=VERSION) # simple threshold # TODO they will want symmetric threshold... # TODO maybe make that a parameter adj = mg.adj.copy() adj[adj <= threshold] = 0 meta = mg.meta.copy() meta = pd.DataFrame(mg.meta["neuron_name"]) mg = MetaGraph(adj, meta) # run the graphtool code temp_loc = f"maggot_models/data/interim/temp-{param_key}.graphml" block_series = run_minimize_blockmodel(mg, temp_loc) # manage the output mg = load_metagraph(graph_type, version=VERSION) mg.meta = pd.concat((mg.meta, block_series), axis=1) mg.meta["Original index"] = range(len(mg.meta)) keep_inds = mg.meta[~mg.meta["block_label"].isna( )]["Original index"].values mg.reindex(keep_inds) if graph_type != "G": mg.verify(10000, graph_type=graph_type, version=VERSION) # deal with class labels lineage_labels = mg.meta["lineage"].values lineage_labels = np.vectorize(lambda x: "~" + x)(lineage_labels) class_labels = mg["Merge Class"] skeleton_labels = mg.meta.index.values classlin_labels, color_dict, hatch_dict = augment_classes( skeleton_labels, class_labels, lineage_labels) block_label = mg["block_label"].astype(int) # barplot with unknown class labels merged in, proportions _, _, order = barplot_text( block_label, classlin_labels, norm_bar_width=True, color_dict=color_dict, hatch_dict=hatch_dict, title=title, figsize=(24, 18), return_order=True, ) stashfig(savename + "barplot-mergeclasslin-props") category_order = np.unique(block_label)[order] # barplot with regular class labels barplot_text( block_label, class_labels, norm_bar_width=True, color_dict=color_dict, hatch_dict=hatch_dict, title=title, figsize=(24, 18), category_order=category_order, ) stashfig(savename + "barplot-mergeclass-props") # barplot with unknown class labels merged in, counts barplot_text( block_label, classlin_labels, norm_bar_width=False, color_dict=color_dict, hatch_dict=hatch_dict, title=title, figsize=(24, 18), return_order=True, category_order=category_order, ) stashfig(savename + "barplot-mergeclasslin-counts") # barplot of hemisphere membership fig, ax = plt.subplots(1, 1, figsize=(10, 20)) stacked_barplot( block_label, mg["Hemisphere"], norm_bar_width=True, category_order=category_order, ax=ax, ) remove_spines(ax) stashfig(savename + "barplot-hemisphere") # plot block probability matrix counts = False weights = False prob_df = get_blockmodel_df(mg.adj, block_label, return_counts=counts, use_weights=weights) prob_df = prob_df.reindex(order, axis=0) prob_df = prob_df.reindex(order, axis=1) ax = probplot(100 * prob_df, fmt="2.0f", figsize=(20, 20), title=title, font_scale=0.4) stashfig(savename + "probplot") block_series.name = param_key return block_series
for thresh in threshs: for r in [0.3, 0.5, 0.7, 1]: mg = load_metagraph(graph_type, version=BRAIN_VERSION) edgelist = mg.to_edgelist() edgelist = edgelist[edgelist["weight"] > thresh] nodelist = list(mg.g.nodes()) thresh_g = nx.from_pandas_edgelist(edgelist, edge_attr=True, create_using=nx.DiGraph) nx.set_node_attributes(thresh_g, mg.meta.to_dict(orient="index")) mg = MetaGraph(thresh_g) print(len(mg.meta)) mg = mg.make_lcc() print(len(mg.meta)) not_pdiff = np.where(~mg["is_pdiff"])[0] mg = mg.reindex(not_pdiff) print(len(mg.meta)) g_sym = nx.to_undirected(mg.g) skeleton_labels = np.array(list(g_sym.nodes())) out_dict = cm.best_partition(g_sym, resolution=r) partition = np.array(itemgetter(*skeleton_labels)(out_dict)) adj = nx.to_numpy_array(g_sym, nodelist=skeleton_labels) part_unique, part_count = np.unique(partition, return_counts=True) for uni, count in zip(part_unique, part_count): if count < 3: inds = np.where(partition == uni)[0] partition[inds] = -1 mg.meta["signal_flow"] = signal_flow(mg.adj) mg.meta["partition"] = partition
mg.verify() # %% [markdown] # # Extract subgraphs extract_fb = True if extract_fb: from_classes = ["MBON", "FAN", "FBN", "FB2N"] to_classes = ["MBIN"] sub_mg = MetaGraph(mg.adj, mg.meta) from_class_inds = sub_mg.meta["Class 1"].isin(from_classes) to_class_inds = sub_mg.meta["Class 1"].isin(to_classes) any_inds = np.logical_or(from_class_inds, to_class_inds) sub_mg.reindex(any_inds) sub_mg.sort_values(["Hemisphere", "Class 1", "Pair ID"]) # meta = sub_mg.meta # meta["Original index"] = range(meta.shape[0]) # meta.sort_values( # ["Hemisphere", "Class 1", "Pair ID"], # inplace=True, # kind="mergesort", # ascending=False, # ) # temp_inds = meta["Original index"] # sub_mg = sub_mg.reindex(temp_inds) # sub_mg = MetaGraph(sub_mg.adj, meta) adj = sub_mg.adj.copy() from_class_inds = np.where(sub_mg.meta["Class 1"].isin(from_classes).values)[0]
# %% [markdown] # # from src.graph import MetaGraph import numpy as np import pandas as pd import networkx as nx n_verts = 5 adj = np.arange(0, n_verts**2).reshape(n_verts, n_verts) adj[0, :] = 0 adj[:, 0] = 0 print(adj) meta = pd.DataFrame(index=range(n_verts)) meta["title"] = ["zero", "one", "two", "three", "four"] meta["remove"] = [False, True, False, False, True] meta.index = ["0", "1", "2", "3", "4"] print(meta) mg = MetaGraph(adj, meta) # mg.make_lcc() mg.reindex(np.array([2, 3])) print(mg.meta) print(mg.adj) mg2 = MetaGraph(mg.g) print(mg2.adj) print(mg2.meta)
n_pairs = mg.adj.shape[0] // 2 mg.verify() # %% [markdown] # # Extract subgraphs adj = mg.adj.copy() from_classes = ["MBON", "FAN", "FBN", "FB2N"] to_classes = ["MBIN"] from_class_inds = mg.meta["Class 1"].isin(from_classes) to_class_inds = mg.meta["Class 1"].isin(to_classes) any_inds = np.logical_or(from_class_inds, to_class_inds) mg.reindex(any_inds) meta = mg.meta meta["Original index"] = range(meta.shape[0]) meta.sort_values( ["Hemisphere", "Class 1", "Pair ID"], inplace=True, kind="mergesort", ascending=False, ) temp_inds = meta["Original index"] mg = mg.reindex(temp_inds) mg = MetaGraph(mg.adj, meta) adj = mg.adj from_class_inds = np.where(mg.meta["Class 1"].isin(from_classes).values)[0]
) plt.legend(bbox_to_anchor=(1.08, 1), loc=2, borderaxespad=0.0) ax.set_title(title) stashfig(f"threshold-vs-knn" + base_save) # %% [markdown] # # graph_type = "Gad" use_spl = False embed = "lse" remove_pdiff = True plus_c = True mg = load_metagraph(graph_type, BRAIN_VERSION) keep_inds = np.where(~mg["is_pdiff"])[0] mg = mg.reindex(keep_inds) n_original_verts = mg.n_verts mg = mg.make_lcc() edgelist_df = mg.to_edgelist() edgelist_df.rename(columns={"weight": "syn_weight"}, inplace=True) edgelist_df["norm_weight"] = (edgelist_df["syn_weight"] / edgelist_df["target dendrite_input"]) max_pair_edges = edgelist_df.groupby("edge pair ID", sort=False)["syn_weight"].max() edge_max_weight_map = dict( zip(max_pair_edges.index.values, max_pair_edges.values)) edgelist_df["max_syn_weight"] = itemgetter( *edgelist_df["edge pair ID"])(edge_max_weight_map) temp_df = edgelist_df[edgelist_df["edge pair ID"] == 0] edgelist_df.loc[temp_df.index, "max_syn_weight"] = temp_df["syn_weight"]