def test_SetStatusVth_E_L(self): """SetStatus of reversal and threshold potential """ m = "sample_neuron" cynest.ResetKernel() neuron1 = cynest.Create(m) neuron2 = cynest.Create(m) # must not depend on the order. new_EL = -90. new_Vth = -10. cynest.SetStatus(neuron1, {'E_L': new_EL}) cynest.SetStatus(neuron2, {'V_th': new_Vth}) cynest.SetStatus(neuron1, {'V_th': new_Vth}) cynest.SetStatus(neuron2, {'E_L': new_EL}) # next three lines for debugging vth1, vth2 = cynest.GetStatus(neuron1, 'V_th'), cynest.GetStatus( neuron2, 'V_th') if vth1 != vth2: print(m, vth1, vth2, cynest.GetStatus(neuron1, 'E_L'), cynest.GetStatus(neuron2, 'E_L')) assert (cynest.GetStatus(neuron1, 'V_th') == cynest.GetStatus(neuron2, 'V_th'))
def build(self, neuronName): """ Create all nodes, used in the model. """ if self.built == True: return self.calibrate() #self.nodes = cynest.Create("iaf_psc_delta",self.N_neurons) #self.nodes = cynest.Create("cython_neuron",self.N_neurons) #self.nodes = cynest.Create("brunell-sli_neuron",self.N_neurons) print neuronName self.nodes = cynest.Create(neuronName, self.N_neurons) self.noise = cynest.Create("poisson_generator", 1, {"rate": self.p_rate}) self.spikes = cynest.Create("spike_detector", 2, [{ "label": "brunel-py-ex" }, { "label": "brunel-py-in" }]) self.nodes_E = self.nodes[:self.N_E] self.nodes_I = self.nodes[self.N_E:] self.spikes_E = self.spikes[:1] self.spikes_I = self.spikes[1:] self.built = True
def build(self, neuronName, custom): """ Create all nodes, used in the model. """ if self.built == True: return self.calibrate() self.nodes = cynest.Create(neuronName, self.N_neurons) self.noise = cynest.Create("poisson_generator", 1, {"rate": self.p_rate}) self.spikes = cynest.Create("spike_detector", 2, [{ "label": "brunel-py-ex" }, { "label": "brunel-py-in" }]) if custom == True: cynest.SetStatus(self.nodes, [{"optimized": True}]) self.nodes_E = self.nodes[:self.N_E] self.nodes_I = self.nodes[self.N_E:] self.spikes_E = self.spikes[:1] self.spikes_I = self.spikes[1:] self.built = True
def build(self): nest.ResetKernel() nest.SetKernelStatus({'local_num_threads': self.n_threads}) if self.params: nest.SetDefaults(self.model,self.params) self.neuron=nest.Create(self.model,self.n_neurons) self.noise=nest.Create('noise_generator') self.spike=nest.Create('spike_detector') nest.SetStatus(self.spike,[{'to_memory':True, 'to_file':False}])
def test_DivergentConnect(self): """DivergentConnect pre to post""" nest.ResetKernel() pre = nest.Create("iaf_neuron", 1) post = nest.Create("iaf_neuron", 3) nest.DivergentConnect(pre, post) connections = nest.FindConnections(pre) targets = nest.GetStatus(connections, "target") self.assertEqual(targets, post)
def test_ConvergentConnect(self): """ConvergentConnect pre to post""" nest.ResetKernel() pre = nest.Create("iaf_neuron", 3) post = nest.Create("iaf_neuron", 1) nest.ConvergentConnect(pre, post) expected_targets = [post[0] for x in range(len(pre))] connections = nest.FindConnections(pre) targets = nest.GetStatus(connections, "target") self.assertEqual(expected_targets, targets)
def test_ConnectPrePost(self): """Connect pre to post""" # Connect([pre], [post]) nest.ResetKernel() pre = nest.Create("iaf_neuron", 2) post = nest.Create("iaf_neuron", 2) nest.Connect(pre, post) connections = nest.FindConnections(pre) targets = nest.GetStatus(connections, "target") self.assertEqual(targets, post)
def test_GetStatus(self): """GetStatus""" m = "sample_neuron" cynest.ResetKernel() n = cynest.Create(m) d = cynest.GetStatus(n) v1 = cynest.GetStatus(n)[0]['param'] v2 = cynest.GetStatus(n, 'param')[0] self.assertEqual(v1, v2) n = cynest.Create(m, 10) self.assertEqual(len(cynest.GetStatus(n, 'param')), 10)
def test_DivergentConnectWD(self): """DivergentConnect pre to post with weight and delay""" nest.ResetKernel() pre = nest.Create("iaf_neuron", 1) post = nest.Create("iaf_neuron", 3) nest.DivergentConnect(pre, post, weight=[2.0, 2.0, 2.0], delay=[1.0, 2.0, 3.0]) connections = nest.FindConnections(pre) weights = nest.GetStatus(connections, "weight") delays = nest.GetStatus(connections, "delay") self.assertEqual(weights, [2.0, 2.0, 2.0]) self.assertEqual(delays, [1.0, 2.0, 3.0])
def test_Events_1(self): """Recorder Events""" nest.ResetKernel() sd = nest.Create('spike_detector', 1, {'withtime': True}) d = nest.GetStatus(sd, 'events')[0] senders = d['senders'] times = d['times'] vm = nest.Create('voltmeter', 1, {'withtime': True}) d = nest.GetStatus(vm, 'events')[0] senders = d['V_m'] times = d['times']
def test_EventsVoltage(self): """Voltage Events""" nest.ResetKernel() nest.sr('20 setverbosity') n = nest.Create('iaf_neuron') vm = nest.Create('voltmeter', 1, {'withtime': True, 'interval': 1.}) nest.Connect(vm, n) nest.SetKernelStatus({'print_time': False}) nest.Simulate(10) d = nest.GetStatus(vm, 'events')[0] self.assertEqual(len(d['V_m']), 9)
def test_CopyModel(self): """CopyModel""" cynest.ResetKernel() cynest.CopyModel("sample_neuron", 'new_neuron', {'V_m': 10.0}) vm = cynest.GetDefaults('new_neuron')['V_m'] self.assertEqual(vm, 10.0) n = cynest.Create('new_neuron', 10) vm = cynest.GetStatus([n[0]])[0]['V_m'] self.assertEqual(vm, 10.0) cynest.CopyModel('static_synapse', 'new_synapse', {'weight': 10.}) cynest.Connect([n[0]], [n[1]], model='new_synapse') w = cynest.GetDefaults('new_synapse')['weight'] self.assertEqual(w, 10.0) try: cynest.CopyModel( "sample_neuron", 'new_neuron') # shouldn't be possible a second time self.fail('an error should have risen!') # should not be reached except: info = sys.exc_info()[1] if not "NewModelNameExists" in info.__str__(): self.fail('could not pass error message to cynest!')
def test_ParamTypes(self): """Test of all parameter types""" cynest.ResetKernel() node = cynest.Create("sample_neuron") cynest.SetStatus(node, {"param1":2, "param2":6.5, "param3": True, "param4":"sd", "param5":'r', \ "param6":[1,2,3,4], "param7":{"c1":1, "c2":2}, "param8":{"e1":{"s1":1}}}) cynest.Simulate(1) s = cynest.GetStatus(node)[0] self.assertEqual(s["param1"], 2) self.assertEqual(s["param2"], 6.5) self.assertEqual(s["param3"], True) self.assertEqual(s["param4"], "sd") self.assertEqual(s["param5"], 'r') self.assertEqual(s["param6"][0], 1) self.assertEqual(s["param6"][1], 2) self.assertEqual(s["param6"][2], 3) self.assertEqual(s["param6"][3], 4) self.assertEqual(s["param7"]["c1"], 1) self.assertEqual(s["param7"]["c2"], 2) self.assertEqual(s["param8"]["e1"]["s1"], 1)
def test_EventsSpikes(self): """Spike Events""" nest.ResetKernel() nest.sr('20 setverbosity') n = nest.Create('iaf_neuron', 1, {'I_e': 1000.}) sd = nest.Create('spike_detector', 1, {'withtime': True}) nest.Connect(n, sd) nest.SetKernelStatus({'print_time': False}) nest.Simulate(1000) d = nest.GetStatus(sd, 'events')[0] self.assert_(len(d['times']) > 0)
def build_network(dt): nest.ResetKernel() nest.SetKernelStatus({"local_num_threads": 1, "resolution": dt}) neuron = nest.Create('iaf_neuron') nest.SetStatus(neuron, "I_e", 376.0) vm = nest.Create('voltmeter') nest.SetStatus(vm, "withtime", True) sd = nest.Create('spike_detector') nest.Connect(vm, neuron) nest.Connect(neuron, sd) return vm, sd
def test_UnexcpectedEvent(self): """Unexpected Event""" nest.ResetKernel() n = nest.Create('iaf_neuron') vm = nest.Create('voltmeter') sd = nest.Create('spike_detector') try: nest.Connect(sd, n) self.fail() # should not be reached except nest.NESTError: info = sys.exc_info()[1] if not "UnexpectedEvent" in info.__str__(): self.fail() # another error has been thrown, this is wrong except: self.fail()
def test_ModelDicts(self): """sample_neuron Creation with N and dicts""" cynest.ResetKernel() V_m = [0.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.] n = cynest.Create("sample_neuron", 10, [{'V_m': v} for v in V_m]) self.assertEqual(cynest.GetStatus(n, 'V_m'), V_m) self.assertEqual([key['V_m'] for key in cynest.GetStatus(n)], V_m)
def test_ThreadsFindConnections(self): """FindConnections with threads""" # Test if we have a thread-enabled NEST nest.sr("statusdict /have_pthreads get") if not nest.spp(): return nest.ResetKernel() nest.SetKernelStatus({'local_num_threads': 8}) pre = nest.Create("iaf_neuron") post = nest.Create("iaf_neuron", 6) nest.DivergentConnect(pre, post) conn = nest.FindConnections(pre) targets = nest.GetStatus(conn, "target") self.assertEqual(targets, post)
def test_SetStatusParam(self): """SetStatus with parameter""" m = "sample_neuron" cynest.ResetKernel() n = cynest.Create(m) cynest.SetStatus(n, 'V_m', 3.) self.assertEqual(cynest.GetStatus(n, 'V_m')[0], 3.)
def test_SetStatusList(self): """SetStatus with list""" m = "sample_neuron" cynest.ResetKernel() n = cynest.Create(m) cynest.SetStatus(n, [{'V_m': 2.}]) self.assertEqual(cynest.GetStatus(n, 'V_m')[0], 2.)
def test_SetStatus(self): """SetStatus with dict""" m = "sample_neuron" cynest.ResetKernel() n = cynest.Create(m) cynest.SetStatus(n, {'V_m': 1.}) self.assertEqual(cynest.GetStatus(n, 'V_m')[0], 1.)
def test_WrongConnection(self): """Wrong Connections""" nest.ResetKernel() n = nest.Create('iaf_neuron') vm = nest.Create('voltmeter') sd = nest.Create('spike_detector') try: nest.Connect(n, vm) self.fail() # should not be reached except nest.NESTError: info = sys.exc_info()[1] if not "IllegalConnection" in info.__str__(): self.fail() # another error has been thrown, this is wrong except: self.fail()
def test_ModelDict(self): """sample_neuron Creation with N and dict""" cynest.ResetKernel() n = cynest.Create("sample_neuron", 10, [{'V_m': 12.0}]) V_m = [12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0] self.assertEqual(cynest.GetStatus(n, 'V_m'), V_m) self.assertEqual([key['V_m'] for key in cynest.GetStatus(n)], V_m)
def test_ModelCreateSimulate(self): """Model Creation and Simulation""" cynest.ResetKernel() cynest.SetDefaults("sample_neuron", {"param": 20}) node = cynest.Create("sample_neuron") cynest.Simulate(1) self.assertEqual(cynest.GetStatus(node)[0]["param"], 30)
def test_Function(self): """Test of function object""" cynest.ResetKernel() node = cynest.Create("sample_neuron") try: cynest.SetStatus(node, {"param2": testFct}) except: info = sys.exc_info()[1]
def test_RandomDivergentConnect(self): """RandomDivergentConnect""" cynest.ResetKernel() a=cynest.Create("iaf_neuron", 1000) source=[1] targets=range(2,1000) cynest.RandomDivergentConnect(source,targets, 500, [1.0], [1.0]) conn1=cynest.GetConnections(source) self.assertEqual(len(conn1), 500)
def test_RandomConvergentConnect(self): """RandomConvergentConnect""" cynest.ResetKernel() a=cynest.Create("iaf_neuron", 1000) sources=list(range(2,1000)) target=[1] cynest.RandomConvergentConnect(sources,target, 500, [1.0], [1.0]) conn1=cynest.GetConnections(sources) self.assertEqual(len(conn1), 500)
def test_ThreadsGetEvents(self): """ Gathering events across threads """ # Test if we have a thread-enabled NEST nest.sr("statusdict /have_pthreads get") if not nest.spp(): return threads = [1, 2, 4, 8] n_events_sd = [] n_events_vm = [] N = 128 Simtime = 1000. for t in threads: nest.ResetKernel() nest.SetKernelStatus({'local_num_threads': t}) n = nest.Create('iaf_psc_alpha', N, {'I_e': 2000.}) # force a lot of spike events sd = nest.Create('spike_detector') vm = nest.Create('voltmeter') nest.ConvergentConnect(n, sd) nest.DivergentConnect(vm, n) nest.Simulate(Simtime) n_events_sd.append(nest.GetStatus(sd, 'n_events')[0]) n_events_vm.append(nest.GetStatus(vm, 'n_events')[0]) ref_vm = N * (Simtime - 1) ref_sd = n_events_sd[0] # could be done more elegantly with any(), ravel(), # but we dont want to be dependent on numpy et al [self.assertEqual(x, ref_vm) for x in n_events_vm] [self.assertEqual(x, ref_sd) for x in n_events_sd]
def test_ConnectPrePostParams(self): """Connect pre to post with a params dict""" # Connect([pre], [post], params) nest.ResetKernel() pre = nest.Create("iaf_neuron", 2) post = nest.Create("iaf_neuron", 2) nest.Connect(pre, post, {"weight": 2.0}) connections = nest.FindConnections(pre) weights = nest.GetStatus(connections, "weight") self.assertEqual(weights, [2.0, 2.0]) # Connect([pre], [post], [params]) nest.ResetKernel() pre = nest.Create("iaf_neuron", 2) post = nest.Create("iaf_neuron", 2) nest.Connect(pre, post, [{"weight": 2.0}]) connections = nest.FindConnections(pre) weights = nest.GetStatus(connections, "weight") self.assertEqual(weights, [2.0, 2.0]) # Connect([pre], [post], [params, params]) nest.ResetKernel() pre = nest.Create("iaf_neuron", 2) post = nest.Create("iaf_neuron", 2) nest.Connect(pre, post, [{"weight": 2.0}, {"weight": 3.0}]) connections = nest.FindConnections(pre) weights = nest.GetStatus(connections, "weight") self.assertEqual(weights, [2.0, 3.0])
def test_ConnectPrePostWD(self): """Connect pre to post with a weight and delay""" # Connect([pre], [post], w, d) nest.ResetKernel() pre = nest.Create("iaf_neuron", 2) post = nest.Create("iaf_neuron", 2) nest.Connect(pre, post, 2.0, 2.0) connections = nest.FindConnections(pre) weights = nest.GetStatus(connections, "weight") self.assertEqual(weights, [2.0, 2.0]) # Connect([pre], [post], [w], [d]) nest.ResetKernel() pre = nest.Create("iaf_neuron", 2) post = nest.Create("iaf_neuron", 2) nest.Connect(pre, post, [2.0], [2.0]) connections = nest.FindConnections(pre) weights = nest.GetStatus(connections, "weight") delays = nest.GetStatus(connections, "delay") self.assertEqual(weights, [2.0, 2.0]) self.assertEqual(delays, [2.0, 2.0]) # Connect([pre], [post], [w, w], [d, d]) nest.ResetKernel() pre = nest.Create("iaf_neuron", 2) post = nest.Create("iaf_neuron", 2) nest.Connect(pre, post, [2.0, 3.0], [2.0, 3.0]) connections = nest.FindConnections(pre) weights = nest.GetStatus(connections, "weight") delays = nest.GetStatus(connections, "delay") self.assertEqual(weights, [2.0, 3.0]) self.assertEqual(delays, [2.0, 3.0])