def test_distances(): ''' Check that distances are properly generated for SpatialGraphs ''' # simple graph # ~ num_nodes = 4 # ~ pos = [(0, 0), (1, 0), (2, 0), (3, 0)] # ~ g = nngt.SpatialGraph(num_nodes, positions=pos) # ~ edges = [(0, 1), (0, 3), (1, 2), (2, 3)] # ~ g.new_edges(edges) # ~ dist = g.edge_attributes["distance"] # ~ expected = np.abs(np.diff(g.edges_array, axis=1)).ravel() # ~ assert np.array_equal(dist, expected) # ~ g.new_node(positions=[(4, 0)]) # ~ g.new_edge(1, 4) # ~ assert g.get_edge_attributes((1, 4), "distance") == 3 # ~ # distance rule # ~ g = ng.distance_rule(2.5, rule="lin", nodes=num_nodes, avg_deg=2, # ~ positions=pos) # ~ dist = g.edge_attributes["distance"] # ~ expected = np.abs(np.diff(g.edges_array, axis=1)).ravel() # ~ assert np.array_equal(dist, expected) # ~ assert np.all(dist < 3) # using the connector functions num_nodes = 20 pop = nngt.NeuralPop.exc_and_inhib(num_nodes) pos = np.array([(i, 0) for i in range(num_nodes)]) net = nngt.SpatialNetwork(pop, positions=pos) inh = pop["inhibitory"] exc = pop["excitatory"] ng.connect_groups(net, exc, pop, "erdos_renyi", avg_deg=5) ng.connect_groups(net, inh, pop, "random_scale_free", in_exp=2.1, out_exp=2.1, avg_deg=5) dist = net.edge_attributes["distance"] expected = np.abs(np.diff(net.edges_array, axis=1)).ravel() assert np.array_equal(dist, expected)
def test_group_vs_type(): ''' Gaussian degree with groups and types ''' # first with groups nngt.seed(0) pop = nngt.NeuralPop.exc_and_inhib(1000) igroup = pop["inhibitory"] egroup = pop["excitatory"] net1 = nngt.Network(population=pop) all_groups = list(pop.keys()) # necessary to have same order as types avg_e = 50 std_e = 5 ng.connect_groups(net1, egroup, all_groups, graph_model="gaussian_degree", avg=avg_e, std=std_e, degree_type="out-degree") avg_i = 100 std_i = 5 ng.connect_groups(net1, igroup, all_groups, graph_model="gaussian_degree", avg=avg_i, std=std_i, degree_type="out-degree") # then with types nngt.seed(0) pop = nngt.NeuralPop.exc_and_inhib(1000) igroup = pop["inhibitory"] egroup = pop["excitatory"] net2 = nngt.Network(population=pop) avg_e = 50 std_e = 5 ng.connect_neural_types(net2, 1, [-1, 1], graph_model="gaussian_degree", avg=avg_e, std=std_e, degree_type="out-degree") avg_i = 100 std_i = 5 ng.connect_neural_types(net2, -1, [-1, 1], graph_model="gaussian_degree", avg=avg_i, std=std_i, degree_type="out-degree") # call only on root process (for mpi) unless using distributed backend if nngt.on_master_process() or nngt.get_config("backend") == "nngt": # check that both networks are equals assert np.all(net1.get_degrees() == net2.get_degrees())
def make_nest_net(size, w, deg): ''' Create a network in NEST ''' import nngt.simulation as ns net = nngt.Network.exc_and_inhib(size) pop = net.population ng.connect_groups(net, pop, pop, graph_model="fixed_degree", degree=deg, degree_type="out", weights=w) gids = net.to_nest() return net, gids
# two groups g1 = nngt.Group(500) # nodes 0 to 499 g2 = nngt.Group(500) # nodes 500 to 999 # make structure struct = nngt.Structure.from_groups((g1, g2), ("left", "right")) # create network from this population net = nngt.Graph(structure=struct) ''' Connect the groups ''' # inter-groups (Erdos-Renyi) prop_er1 = {"density": 0.005} ng.connect_groups(net, "left", "right", "erdos_renyi", **prop_er1) # intra-groups (Newman-Watts) prop_nw = {"coord_nb": 20, "proba_shortcut": 0.1, "reciprocity_circular": 1.} ng.connect_groups(net, "left", "left", "newman_watts", **prop_nw) ng.connect_groups(net, "right", "right", "newman_watts", **prop_nw) ''' Plot the graph ''' if nngt.get_config("with_plot"): nngt.plot.library_draw(net, show=False) pop_graph = net.get_structure_graph()
def test_structure_graph(): gclass = (nngt.Group, nngt.NeuralGroup) sclass = (nngt.Structure, nngt.NeuralPop) nclass = (nngt.Graph, nngt.Network) for gc, sc, nc in zip(gclass, sclass, nclass): room1 = gc(25, neuron_type=1) room2 = gc(50, neuron_type=1) room3 = gc(40, neuron_type=1) room4 = gc(35, neuron_type=1) names = ["R1", "R2", "R3", "R4"] kwargs = {"with_models": False} if sc == nngt.NeuralPop else {} struct = sc.from_groups((room1, room2, room3, room4), names, **kwargs) kwargs = ({ "population": struct } if nc == nngt.Network else { "structure": struct }) g = nc(**kwargs) # connect groups for room in struct: ng.connect_groups(g, room, room, "all_to_all") d1 = 5 ng.connect_groups(g, room1, room2, "erdos_renyi", avg_deg=d1) ng.connect_groups(g, room1, room3, "erdos_renyi", avg_deg=d1) ng.connect_groups(g, room1, room4, "erdos_renyi", avg_deg=d1) d2 = 5 ng.connect_groups(g, room2, room3, "erdos_renyi", avg_deg=d2) ng.connect_groups(g, room2, room4, "erdos_renyi", avg_deg=d2, weights=2) d3 = 20 ng.connect_groups(g, room3, room1, "erdos_renyi", avg_deg=d3) d4 = 10 ng.connect_groups(g, room4, room3, "erdos_renyi", avg_deg=d4) # get structure graph sg = g.get_structure_graph() assert sg.node_nb() == len(struct) eset = set([tuple(e) for e in sg.edges_array]) expected = [(0, 0), (0, 1), (0, 2), (0, 3), (1, 1), (1, 2), (1, 3), (2, 0), (2, 2), (3, 2), (3, 3)] assert eset == set(expected) # check weights w1 = room1.size * d1 w2 = room2.size * d2 w3 = room3.size * d3 w4 = room4.size * d4 expected_weights = [ room1.size * (room1.size - 1), w1, w1, w1, room2.size * (room2.size - 1), w2, w2 * 2, w3, room3.size * (room3.size - 1), w4, room4.size * (room4.size - 1) ] assert np.array_equal(sg.get_weights(edges=expected), expected_weights)
nu_th = (theta * CMem) / (J_ex * CE * np.e * tauMem * tauSyn) nu_ex = eta * nu_th p_rate = 400. * nu_ex * CE ''' Create the population and network ''' pop = nngt.NeuralPop.exc_and_inhib( N_neurons, en_model="iaf_psc_alpha", en_param=neuron_params, in_model="iaf_psc_alpha", in_param=neuron_params) net = nngt.Network(population=pop) ng.connect_groups(net, pop, "excitatory", "gaussian_degree", degree_type="in", avg=CE, std=0.1*CE, weights=J_ex, delays=delay) ng.connect_groups(net, pop, "inhibitory", "gaussian_degree", degree_type="in", avg=CI, std=0.1*CI, weights=J_in, delays=delay) ''' Tools to analyze the activity ''' def coefficient_of_variation(spikes): from scipy.stats import variation times = np.array(spikes["times"]) nrns = np.array(spikes["senders"]) return np.nanmean([variation(np.diff(times[nrns == i]))
def test_gaussian(): ''' Gaussian degree with groups ''' pop = nngt.NeuralPop.exc_and_inhib(1000) igroup = pop["inhibitory"] egroup = pop["excitatory"] net = nngt.Network(population=pop) avg_e = 50 std_e = 5 ng.connect_groups(net, egroup, [igroup, egroup], graph_model="gaussian_degree", avg=avg_e, std=std_e, degree_type="out-degree") avg_i = 100 std_i = 5 ng.connect_groups(net, igroup, [igroup, egroup], graph_model="gaussian_degree", avg=avg_i, std=std_i, degree_type="out-degree") edeg = net.get_degrees("out", nodes=egroup.ids) ideg = net.get_degrees("out", nodes=igroup.ids) # call only on root process (for mpi) and not if using distributed backend if nngt.on_master_process() and nngt.get_config("backend") != "nngt": assert avg_e - std_e < np.average(edeg) < avg_e + std_e assert avg_i - std_i < np.average(ideg) < avg_i + std_i elif nngt.get_config("mpi") and nngt.get_config("backend") == "nngt": from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() num_mpi = comm.Get_size() edeg = comm.gather(edeg[rank::num_mpi], root=0) ideg = comm.gather(ideg[rank::num_mpi], root=0) if nngt.on_master_process(): assert avg_e - std_e < np.average(edeg) < avg_e + std_e assert avg_i - std_i < np.average(ideg) < avg_i + std_i edeg = net.get_degrees("in", nodes=egroup.ids) ideg = net.get_degrees("in", nodes=igroup.ids) avg_deg = (avg_e*egroup.size + avg_i*igroup.size) / pop.size std_deg = np.sqrt(avg_deg) # call only on root process (for mpi) unless using distributed backend if nngt.on_master_process() and nngt.get_config("backend") != "nngt": assert avg_deg - std_deg < np.average(edeg) < avg_deg + std_deg assert avg_deg - std_deg < np.average(ideg) < avg_deg + std_deg elif nngt.get_config("mpi") and nngt.get_config("backend") == "nngt": from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() num_mpi = comm.Get_size() edeg = comm.gather(edeg, root=0) ideg = comm.gather(ideg, root=0) if nngt.on_master_process(): edeg = np.sum(edeg, axis=0) ideg = np.sum(ideg, axis=0) assert avg_deg - std_deg < np.average(edeg) < avg_deg + std_deg assert avg_deg - std_deg < np.average(ideg) < avg_deg + std_deg