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 test_fixed(): ''' Fixed degree with type ''' pop = nngt.NeuralPop.exc_and_inhib(1000) igroup = pop["inhibitory"] egroup = pop["excitatory"] net = nngt.Network(population=pop) deg_e = 50 ng.connect_neural_types(net, 1, [-1, 1], graph_model="fixed_degree", degree=deg_e, degree_type="out-degree") deg_i = 100 ng.connect_neural_types(net, -1, [-1, 1], graph_model="fixed_degree", degree=deg_i, degree_type="out-degree") edeg = net.get_degrees("out", nodes=egroup.ids) ideg = net.get_degrees("out", nodes=igroup.ids) assert np.all(edeg == deg_e) assert np.all(ideg == deg_i) edeg = net.get_degrees("in", nodes=egroup.ids) ideg = net.get_degrees("in", nodes=igroup.ids) avg_deg = (deg_e*egroup.size + deg_i*igroup.size) / pop.size std_deg = np.sqrt(avg_deg) assert avg_deg - std_deg < np.average(edeg) < avg_deg + std_deg assert avg_deg - std_deg < np.average(ideg) < avg_deg + std_deg
''' Make the population ''' # two groups of neurons g1 = nngt.NeuralGroup(500) # neurons 0 to 499 g2 = nngt.NeuralGroup(500) # neurons 500 to 999 # make population (without NEST models) pop = nngt.NeuralPop.from_groups( (g1, g2), ("left", "right"), with_models=False) # create network from this population net = nngt.Network(population=pop) ''' Connect the groups ''' # inter-groups (Erdos-Renyi) prop_er1 = {"density": 0.005} ng.connect_neural_groups(net, "left", "right", "erdos_renyi", **prop_er1) # intra-groups (Newman-Watts) prop_nw = { "coord_nb": 20, "proba_shortcut": 0.1 }
def test_directed_adjacency(): ''' Check directed adjacency matrix ''' num_nodes = 5 edge_list = [(0, 1), (0, 3), (1, 3), (2, 0), (3, 2), (3, 4), (4, 2)] weights = [0.54881, 0.71518, 0.60276, 0.54488, 0.42365, 0.64589, 0.43758] etypes = [-1, 1, 1, -1, -1, 1, 1] g = nngt.Graph(num_nodes) g.new_edges(edge_list, attributes={"weight": weights}) g.new_edge_attribute("type", "int", values=etypes) adj_mat = np.array([[0, 1, 0, 1, 0], [0, 0, 0, 1, 0], [1, 0, 0, 0, 0], [0, 0, 1, 0, 1], [0, 0, 1, 0, 0]]) assert np.all( np.isclose(g.adjacency_matrix(weights=False).todense(), adj_mat)) w_mat = np.array([[0, 0.54881, 0, 0.71518, 0], [0, 0, 0, 0.60276, 0], [0.54488, 0, 0, 0, 0], [0, 0, 0.42365, 0, 0.64589], [0, 0, 0.43758, 0, 0]]) assert np.all(np.isclose( g.adjacency_matrix(weights=True).todense(), w_mat)) # for typed edges tpd_mat = np.array([[0, -1, 0, 1, 0], [0, 0, 0, 1, 0], [-1, 0, 0, 0, 0], [0, 0, -1, 0, 1], [0, 0, 1, 0, 0]]) assert np.all(np.isclose( g.adjacency_matrix(types=True).todense(), tpd_mat)) wt_mat = np.array([[0, -0.54881, 0, 0.71518, 0], [0, 0, 0, 0.60276, 0], [-0.54488, 0, 0, 0, 0], [0, 0, -0.42365, 0, 0.64589], [0, 0, 0.43758, 0, 0]]) assert np.all( np.isclose( g.adjacency_matrix(types=True, weights=True).todense(), wt_mat)) # for Network and node attribute type num_nodes = 5 edge_list = [(0, 1), (0, 3), (1, 3), (2, 0), (3, 2), (3, 4), (4, 2)] weights = [0.54881, 0.71518, 0.60276, 0.54488, 0.42365, 0.64589, 0.43758] inh = nngt.NeuralGroup([0, 2, 4], neuron_type=-1, name="inh") exc = nngt.NeuralGroup([1, 3], neuron_type=1, name="exc") pop = nngt.NeuralPop.from_groups((inh, exc), with_models=False) net = nngt.Network(population=pop) net.new_edges(edge_list, attributes={"weight": weights}) g = nngt.Graph(num_nodes) g.new_node_attribute("type", "int", values=[-1, 1, -1, 1, -1]) g.new_edges(edge_list, attributes={"weight": weights}) tpd_mat = np.array([[0, -1, 0, -1, 0], [0, 0, 0, 1, 0], [-1, 0, 0, 0, 0], [0, 0, 1, 0, 1], [0, 0, -1, 0, 0]]) assert np.all( np.isclose(net.adjacency_matrix(types=True).todense(), tpd_mat)) assert np.all(np.isclose( g.adjacency_matrix(types=True).todense(), tpd_mat)) wt_mat = np.array([[0, -0.54881, 0, -0.71518, 0], [0, 0, 0, 0.60276, 0], [-0.54488, 0, 0, 0, 0], [0, 0, 0.42365, 0, 0.64589], [0, 0, -0.43758, 0, 0]]) assert np.all( np.isclose( net.adjacency_matrix(types=True, weights=True).todense(), wt_mat)) assert np.all( np.isclose( g.adjacency_matrix(types=True, weights=True).todense(), wt_mat))
right_shape = nngt.geometry.Shape.rectangle(800, 200, (300, 0)) left_pos = left_shape.seed_neurons(left.size) right_pos = right_shape.seed_neurons(right.size) # we order the positions according to the neuron ids positions = np.empty((num_neurons, 2)) for i, p in zip(left_nodes, left_pos): positions[i] = p for i, p in zip(right_nodes, right_pos): positions[i] = p # create network from this population net = nngt.Network(population=pop, positions=positions) ''' Access metagroups ''' print(pop.meta_groups) print(pop["left"]) ''' Plot the graph ''' plt = None
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