Esempio n. 1
0
def load_everything(
    graph_type,
    version="2019-09-18-v2",
    return_keys=None,
    return_df=False,
    return_ids=False,
):
    """Function to load an adjacency matrix and optionally return some associated 
    metadata

    Parameters
    ----------
    graph_type : str
        Which version of the graph to load
    version : str, optional
        Date/version descriptor for which dataset to use, by default "2019-09-18-v2"
    return_df : bool, optional
        Whether to return a Pandas DataFrame representation of the adjacency,
        by default False
    return_ids : bool, optional
        Whether to return the cell ids (skeleton ids), by default False

    Returns
    -------
    [type]
        [description]

    Raises
    ------
    ValueError
        [description]
    """

    graph = load_networkx(graph_type, version=version)
    nx_ids = np.array(list(graph.nodes()), dtype=int)
    df_adj = nx.to_pandas_adjacency(graph)
    df_ids = df_adj.index.values.astype(int)
    if not np.array_equal(nx_ids, df_ids):
        raise ValueError(
            "Networkx indexing is inconsistent with Pandas adjacency")
    adj = nx.to_pandas_adjacency(graph).values
    outs = [adj]

    if return_keys is not None:
        if not isinstance(return_keys, list):
            return_keys = [return_keys]
        for k in return_keys:
            labels = meta_to_array(graph, k)
            outs.append(labels)
    if return_df:
        outs.append(df_adj)
    if return_ids:
        outs.append(df_ids)
    if len(outs) > 1:
        outs = tuple(outs)
        return outs
    else:
        return adj
from graspy.plot import degreeplot, edgeplot, gridplot, heatmap
from mpl_toolkits.axes_grid1 import make_axes_locatable

from src.data import load_networkx
from src.utils import meta_to_array, savefig

#%%
graph_type = "Gn"

graph = load_networkx(graph_type)

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

classes = meta_to_array(graph, "Class")
print(np.unique(classes))

nx_ids = np.array(list(graph.nodes()), dtype=int)
df_ids = df_adj.index.values.astype(int)
df_adj.index = df_ids
df_adj.columns = df_ids
np.array_equal(nx_ids, df_ids)
cell_ids = df_ids

#%% Map MW classes to the indices of cells belonging to them
unique_classes, inverse_classes = np.unique(classes, return_inverse=True)
class_ind_map = {}
class_ids_map = {}
for i, class_name in enumerate(unique_classes):
    inds = np.where(inverse_classes == i)[0]
Esempio n. 3
0
#     matched_graph = graph.subgraph(nodelist)
#     adj_df = nx.to_pandas_adjacency(matched_graph, nodelist=nodelist)
#     adj = adj_df.values
#     return adj


# def get_mean_induced(matched_adj):
#     n_per_side = matched_adj.shape[0] // 2
#     left_left_adj = matched_adj[:n_per_side, :n_per_side]
#     # left_right_adj = matched_adj[:n_per_side, n_per_side:]
#     right_right_adj = matched_adj[n_per_side:, n_per_side:]
#     # right_left_adj = matched_adj[n_per_side:, :n_per_side]
#     mean_adj = (left_left_adj + right_right_adj) / 2
#     return mean_adj
flat_g = load_networkx("G", version="mb_2019-09-23")
mb_class_labels = meta_to_array(flat_g, "Class")
side_labels = meta_to_array(flat_g, "Hemisphere")
right_inds = np.where(side_labels == "right")[0]
adj = nx.to_pandas_adjacency(flat_g).values
adj = adj[np.ix_(right_inds, right_inds)]
degrees = adj.sum(axis=0) + adj.sum(axis=1)
sort_inds = np.argsort(degrees)[::-1]
mb_class_labels = mb_class_labels[right_inds]
mb_class_labels = mb_class_labels[sort_inds]

mb_color_graphs = []
for t in graph_types:
    color_g = load_networkx(t, version="mb_2019-09-23")
    adj = nx.to_pandas_adjacency(color_g).values
    adj = adj[np.ix_(right_inds, right_inds)]
    adj = adj[np.ix_(sort_inds, sort_inds)]
import pandas as pd
import seaborn as sns

from graspy.plot import heatmap
from graspy.utils import binarize
from src.data import load_networkx
from src.utils import meta_to_array

version = "mb_2019-09-23"

plt.style.use("seaborn-white")
sns.set_palette("deep")
#%% Load and plot the full graph
graph_type = "G"
graph = load_networkx(graph_type, version=version)
classes = meta_to_array(graph, "Class")
side_labels = meta_to_array(graph, "Hemisphere")
adj_df = nx.to_pandas_adjacency(graph)
name_map = {
    "APL": "APL",
    "Gustatory PN": "PN",
    "KC 1 claw": "KC",
    "KC 2 claw": "KC",
    "KC 3 claw": "KC",
    "KC 4 claw": "KC",
    "KC 5 claw": "KC",
    "KC 6 claw": "KC",
    "KC young": "KC",
    "MBIN": "MBIN",
    "MBON": "MBON",
    "ORN mPN": "PN",
Esempio n. 5
0
                                      return_counts=True)
    inds_sort = np.argsort(inds)
    uniques = uniques[inds_sort]
    counts = counts[inds_sort]
    return uniques, counts


#%%
graph_type = "Gn"

graph = load_networkx(graph_type, version="mb_2019-09-23")

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

classes = meta_to_array(graph, "Class")
print(np.unique(classes))

nx_ids = np.array(list(graph.nodes()), dtype=int)
df_ids = df_adj.index.values.astype(int)
df_adj.index = df_ids
df_adj.columns = df_ids
np.array_equal(nx_ids, df_ids)
cell_ids = df_ids

sort_inds = _sort_inds(adj, classes, np.ones_like(classes), True)
adj = _sort_graph(adj, classes, np.ones_like(classes), True)
classes = classes[sort_inds]
cell_ids = cell_ids[sort_inds]

xs, ys = np.meshgrid(range(adj.shape[1]), range(adj.shape[0]))
Esempio n. 6
0
#%%
MB_VERSION = "mb_2019-09-23"
BRAIN_VERSION = "2019-09-18-v2"
GRAPH_TYPES = ["Gad", "Gaa", "Gdd", "Gda"]
GRAPH_TYPE_LABELS = [r"A $\to$ D", r"A $\to$ A", r"D $\to$ D", r"D $\to$ A"]
N_GRAPH_TYPES = len(GRAPH_TYPES)

FNAME = os.path.basename(__file__)[:-3]
print(FNAME)

adj, class_labels, side_labels = load_everything("G",
                                                 version=BRAIN_VERSION,
                                                 return_class=True,
                                                 return_side=True)

graph = load_networkx("G", BRAIN_VERSION)
node2vec = Node2Vec(graph,
                    dimensions=6,
                    workers=12,
                    p=0.5,
                    q=0.5,
                    walk_length=100,
                    num_walks=20)

model = node2vec.fit(window=20, min_count=1, batch_words=4)
vecs = [model.wv.get_vector(n) for n in graph.node()]

embedding = np.array(vecs)

pairplot(embedding, labels=meta_to_array(graph, "Class"), palette="tab20")
    latent = ase.fit_transform(graph)
    if isinstance(latent, tuple):
        latent = np.concatenate(latent, axis=-1)
    norm_vec = np.linalg.norm(latent, axis=1)
    norm_vec[norm_vec == 0] = 1
    norm_latent = latent / norm_vec[:, np.newaxis]
    return norm_latent


#%%
print("Loading graph")


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,
)
#%%
#%%
from src.data import load_june
from src.utils import meta_to_array
from graspy.utils import import_graph
import networkx as nx

graph = load_june("Gadn")
names = meta_to_array(graph, "Name")
names

classes = meta_to_array(graph, "Class")
classes

nodes = list(graph.nodes())

labels = dict(zip(nodes, names))

for i, c in enumerate(classes):
    if c == "OANs":
        print(names[i])
        print(i)
        print

adj = import_graph(graph)
#%%
np.unique(classes)

#%%
for i, n in enumerate(graph.nodes(data=True)):
    data = n[1]
    node = n[0]