def test_groups(): ids = [i for i in range(10)] g1 = nngt.NeuralGroup(ids, neuron_type=None) assert ids == g1.ids assert len(ids) == len(g1) assert g1.neuron_model is None assert g1.neuron_type is None assert not g1.has_model assert g1.is_metagroup g2 = nngt.NeuralGroup(ids, neuron_type=None) assert g1 == g2 assert g1.name.startswith("MetaGroup ") assert int(g1.name[9:]) + 1 == int(g2.name[9:]) g3 = nngt.NeuralGroup(ids, neuron_type=1, name="test") assert g1 != g3 assert g3.name == "test" assert g3.neuron_type == 1 g4 = nngt.NeuralGroup() assert g4.neuron_type == 1
def test_population(): # basic population ids1 = [i for i in range(10)] g1 = nngt.NeuralGroup(ids1, neuron_type=1) ids2 = [i for i in range(10, 20)] g2 = nngt.NeuralGroup(ids2, neuron_type=-1) pop = nngt.NeuralPop.from_groups((g1, g2), ("g1", "g2"), with_models=False) assert "g1" in pop and "g2" in pop assert set(ids1 + ids2) == set(pop.ids) assert pop.inhibitory == ids2 assert pop.excitatory == ids1 assert len(pop) == 2 assert pop.size == len(ids1) + len(ids2) assert pop["g1"].ids == ids1 #add meta groups g3 = nngt.NeuralGroup(ids1[::2] + ids2[::2], neuron_type=None) pop.add_meta_group(g3) g4 = pop.create_meta_group([i for i in range(pop.size) if i % 3], "mg2") assert g3 in pop.meta_groups.values() assert g4 in pop.meta_groups.values() assert set(g2.ids).issuperset(g3.inhibitory) assert set(g1.ids).issuperset(g3.excitatory) assert set(g2.ids).issuperset(g4.inhibitory) assert set(g1.ids).issuperset(g4.excitatory) # build population from empty one pop = nngt.NeuralPop() g5 = pop.create_group([i for i in range(10)], "group5", neuron_model="none") g2.neuron_model = "none" pop["group2"] = g2 pop.set_model({"neuron": "random_model"}) assert pop.is_valid # basic populations pop = nngt.NeuralPop.uniform(100) assert len(pop) == 1 assert pop.size == 100 pop = nngt.NeuralPop.exc_and_inhib(100) assert len(pop) == 2 assert len(pop["excitatory"].ids) == 80 assert pop["excitatory"].ids == pop.excitatory assert len(pop["inhibitory"].ids) == 20 assert pop["inhibitory"].ids == pop.inhibitory
def test_failed_pop(): ids1 = [i for i in range(10)] g1 = nngt.NeuralGroup(ids1, neuron_type=None) ids2 = [i for i in range(10, 20)] g2 = nngt.NeuralGroup(ids2, neuron_type=None) failed = True try: pop = nngt.NeuralPop.from_groups((g1, g2), ("g1", "g2")) failed = False except: pass try: pop = nngt.NeuralPop.from_groups((g1, g2), ("g1", "g2"), with_models=False) failed = False except: pass assert failed
'C_m': 250. } # oscillators params1, params2 = base_params.copy(), base_params.copy() params1.update({ 'E_L': -65., 'b': 40., 'I_e': 200., 'tau_w': 400., "V_th": -57. }) # bursters params2.update({'b': 25., 'V_reset': -55., 'tau_w': 300.}) oscill = nngt.NeuralGroup(nodes=400, neuron_model='aeif_psc_alpha', neuron_type=1, neuron_param=params1) burst = nngt.NeuralGroup(nodes=200, neuron_model='aeif_psc_alpha', neuron_type=1, neuron_param=params2) adapt = nngt.NeuralGroup(nodes=200, neuron_model='aeif_psc_alpha', neuron_type=1, neuron_param=base_params) model = 'model' try:
def from_gids(cls, gids, get_connections=True, get_params=False, neuron_model=default_neuron, neuron_param=None, syn_model=default_synapse, syn_param=None, **kwargs): ''' Generate a network from gids. Warning ------- Unless `get_connections` and `get_params` is True, or if your population is homogeneous and you provide the required information, the information contained by the network and its `population` attribute will be erroneous! To prevent conflicts the :func:`~nngt.Network.to_nest` function is not available. If you know what you are doing, you should be able to find a workaround... Parameters ---------- gids : array-like Ids of the neurons in NEST or simply user specified ids. get_params : bool, optional (default: True) Whether the parameters should be obtained from NEST (can be very slow). neuron_model : string, optional (default: None) Name of the NEST neural model to use when simulating the activity. neuron_param : dict, optional (default: {}) Dictionary containing the neural parameters; the default value will make NEST use the default parameters of the model. syn_model : string, optional (default: 'static_synapse') NEST synaptic model to use when simulating the activity. syn_param : dict, optional (default: {}) Dictionary containing the synaptic parameters; the default value will make NEST use the default parameters of the model. Returns ------- net : :class:`~nngt.Network` or subclass Uniform network of disconnected neurons. ''' from nngt.lib.errors import not_implemented if neuron_param is None: neuron_param = {} if syn_param is None: syn_param = {} # create the population size = len(gids) nodes = [i for i in range(size)] group = nngt.NeuralGroup(nodes, neuron_type=1, neuron_model=neuron_model, neuron_param=neuron_param) pop = nngt.NeuralPop.from_groups([group]) # create the network net = cls(population=pop, **kwargs) net.nest_gids = np.array(gids) net._id_from_nest_gid = {gid: i for i, gid in enumerate(gids)} net.to_nest = not_implemented if get_connections: from nngt.simulation import get_nest_adjacency converter = {gid: i for i, gid in enumerate(gids)} mat = get_nest_adjacency(converter) edges = np.array(mat.nonzero()).T w = mat.data net.new_edges(edges, {'weight': w}, check_duplicates=False, check_self_loops=False, check_existing=False) if get_params: raise NotImplementedError('`get_params` not implemented yet.') return net
import nngt import nngt.generation as ng import numpy as np num_nodes = 1000 # np.random.seed(0) ''' 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)
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))
''' Make a mixed excitatory and inhibitory population, then subdived it in subgroups ''' num_neurons = 1000 pop = nngt.NeuralPop.exc_and_inhib(num_neurons) # create two separated subgroups associated to two shapes where the neurons # will be seeded # we select 500 random nodes for the left group left_nodes = np.random.choice([i for i in range(num_neurons)], 500, replace=False) left = nngt.NeuralGroup(left_nodes, neuron_type=None) # here we first create... pop.add_meta_group(left, "left") # ... then add # right group is the complement right_nodes = list(set(pop.ids).difference(left_nodes)) right = pop.create_meta_group(right_nodes, "right") # here both in one call # create another pair of random metagroups # we select 500 random nodes for the left group group1 = pop.create_meta_group([i for i in range(500)], "g1") group2 = pop.create_meta_group([i for i in range(500, num_neurons)], "g2") ''' We then create the shapes associated to the left and right groups and seed