In [1]:
import numpy as np
import ndio.remote.neurodata as neurodata
import ndio.ramon as ramon
import time
import ndio
import networkx as nx
start = time.time()
token_synapse = 'kasthuri2015_ramon_v4'
channel_synapse = 'synapses'
token_neurons = 'kasthuri2015_ramon_v4'
channel_neurons = 'neurons'
res = 3
In [2]:
# Find all synapses
nd = neurodata()
id_synapse = nd.get_ramon_ids(token_synapse, channel_synapse, ramon_type=ramon.RAMONSynapse)
print len(id_synapse)
In [3]:
# An example synapse
s = nd.get_ramon(token_synapse, channel_synapse, id_synapse[3])
print s.segments[0]
vars(s)
Out[3]:
In [4]:
def make_graph(local=True, directed=True):
# Instantiate graph
if is_directed == False: # undirected case
G = nx.Graph()
else:
G = nx.DiGraph()
# if local, then cache all synapse and segment metadata - faster by 10x(!), but limited in scalability
In [5]:
# for each synapse
c = 0
for x in id_synapse:
c += 1
print str(c).zfill(4),
s = nd.get_ramon(token_synapse,channel_synapse, x)
# for each segment
segments = [seg[0] for seg in s.segments]
direction = [seg[1] for seg in s.segments]
# 1: axon/pre, 2: dendrite/post (see enumerated types)
#print direction
if len(segments) != 2:
raise('multiway synapses not implemented!')
s1 = nd.get_ramon(token_neurons,channel_neurons,segments[0])
n1 = s1.neuron
s2 = nd.get_ramon(token_neurons,channel_neurons,segments[1])
n2 = s2.neuron
if is_directed is False or (direction[0] == 1 and direction[1] == 2):
if G.has_edge(n1, n2): # edge already exists, increase weight
G[n1][n2]['weight'] += 1
else:
# new edge. add with weight=1
G.add_edge(n1, n2, weight=1)
elif direction[0] == 2 and direction[1] == 1:
if G.has_edge(n2, n1): # edge already exists, increase weight
G[n2][n1]['weight'] += 1
else:
# new edge. add with weight=1
G.add_edge(n2, n1, weight=1)
else:
print('1 pre and 1 post synaptic partner are required for directed graph estimation.')
raise
In [6]:
import matplotlib.pyplot as plt
%matplotlib inline
nx.draw_networkx(G, width=1, node_size = 100, with_labels=False, pos=nx.fruchterman_reingold_layout(G))#random_layout(G))# fruchterman_reingold_layout(G))
plt.show()
nx.draw_networkx(G, width=1, node_size = 100, with_labels=False, pos=nx.random_layout(G))#random_layout(G))
plt.show()
In [7]:
# Save graphml graph
nx.write_graphml(G, 'kasthuri2015_ramon_v4.graphml')
In [8]:
print '{} seconds elapsed.'.format(time.time()-start)