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])
def test_GetNodes(self): """GetNodes""" nest.ResetKernel() model = 'iaf_neuron' l = nest.LayoutNetwork(model, [2, 3]) allNodes = list(range(2, 10)) allSubnets = [2, 6] allLeaves = [n for n in allNodes if n not in allSubnets] # test all self.assertEqual(nest.GetNodes(l), [allNodes]) # test all with empty dict self.assertEqual(nest.GetNodes(l, properties={}), [allNodes]) # test iteration over subnets self.assertEqual(nest.GetNodes(l + l), [allNodes, allNodes]) # children of l are nodes self.assertEqual(nest.GetNodes(l, properties={'parent': l[0]}), [allSubnets]) # local id of second intermediate subnet and middle nodes self.assertEqual(nest.GetNodes(l, properties={'local_id': 2}), [[4, 6, 8]]) # selection by model type self.assertEqual(nest.GetNodes(l, properties={'model': 'subnet'}), [allSubnets]) self.assertEqual(nest.GetNodes(l, properties={'model': model}), [allLeaves])
def test_GetDefaults(self): """GetDefaults""" model = "sample_neuron" cynest.ResetKernel() d = cynest.GetDefaults(model)
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 test_PushPop(self): """Object push and pop""" nest.ResetKernel() objects = [ 1, # int 3.14, # double 1e-4, # double -100, # negative 'string', # string { 'key': 123 }, # dict [1, 2, 3, 4, 5], # list ] for o in objects: nest.sps(o) self.assertEqual(o, nest.spp()) try: import numpy arr = numpy.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]]) nest.sps(arr[1, :]) self.assert_((nest.spp() == numpy.array([6, 7, 8, 9, 0])).all()) nest.sps(arr[:, 1]) self.assert_((nest.spp() == numpy.array([2, 7])).all()) except ImportError: pass # numpy's not required for pynest to work
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 runNeurons(ms, version=1): print "Running native, SLI and cython neurons for " + str(ms) + " ms\n\n" print "Running native neurons" # native neuron b = Brunel2000() b.run("iaf_psc_delta", ms) NativRTF = cynest.GetKernelStatus()["realtime factor"] cynest.ResetKernel() print "Running cython neurons" # cython neuron b = Brunel2000() if version == 1: cynest.RegisterNeuron("cython_iaf_psc_delta_c_members") b.run("cython_iaf_psc_delta_c_members", ms) elif version == 2: cynest.RegisterNeuron("cython_iaf_psc_delta_pydict") b.run("cython_iaf_psc_delta_pydict", ms) elif version == 3: cynest.RegisterNeuron("cython_iaf_psc_delta_pyobject") b.run("cython_iaf_psc_delta_pyobject", ms) CythonRTF = cynest.GetKernelStatus()["realtime factor"] # print "Running SLI neurons" # call(["nest", "brunel-sli_neuron.sli"]) # SliRTF = 0.0045; print "Faster factor (native / cython) : " + str(NativRTF / CythonRTF)
def test_GetChildren(self): """GetChildren""" nest.ResetKernel() model = 'iaf_neuron' l = nest.LayoutNetwork(model, [2, 3]) topKids = [2, 6] kids2 = [3, 4, 5] kids6 = [7, 8, 9] # test top level self.assertEqual(nest.GetChildren(l), [topKids]) # test underlying level self.assertEqual(nest.GetChildren([2, 6]), [kids2, kids6]) # test with empty dict self.assertEqual(nest.GetChildren(l, properties={}), [topKids]) # local id of middle nodes self.assertEqual(nest.GetChildren([2, 6], properties={'local_id': 2}), [[4], [8]]) # selection by model type self.assertEqual(nest.GetChildren(l, properties={'model': 'subnet'}), [topKids]) self.assertEqual(nest.GetChildren([2], properties={'model': 'subnet'}), [[]]) self.assertEqual(nest.GetChildren([2], properties={'model': model}), [kids2])
def test_GetLeaves(self): """GetLeaves""" nest.ResetKernel() model = 'iaf_neuron' l = nest.LayoutNetwork(model, [2, 3]) allLeaves = [3, 4, 5, 7, 8, 9] # test all self.assertEqual(nest.GetLeaves(l), [allLeaves]) # test all with empty dict self.assertEqual(nest.GetLeaves(l, properties={}), [allLeaves]) # test iteration over subnets self.assertEqual(nest.GetLeaves(l + l), [allLeaves, allLeaves]) # children of l are not leaves, should yield empty self.assertEqual(nest.GetLeaves(l, properties={'parent': l[0]}), [[]]) # local id of middle nodes self.assertEqual(nest.GetLeaves(l, properties={'local_id': 2}), [[4, 8]]) # selection by model type self.assertEqual(nest.GetLeaves(l, properties={'model': model}), [allLeaves])
def test_CurrentSubnet(self): """Current Subnet""" nest.ResetKernel() self.assertEqual(nest.CurrentSubnet(), [0]) nest.BeginSubnet() self.assertEqual(nest.CurrentSubnet(), [1])
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_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_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 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_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_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_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_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 __init__(self): """ Initialize an object of this class. """ self.name = self.__class__.__name__ self.data_path = self.name + "/" if not os.path.exists(self.data_path): os.makedirs(self.data_path) print("Writing data to: " + self.data_path) cynest.ResetKernel() cynest.SetKernelStatus({"data_path": self.data_path})
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 runNeurons(ms, opt=True): print("Running native, SLI and cython neurons for " + str(ms) + " ms") cynest.ResetKernel() cynest.SetGraphicsSimulator(True) print("\n\nRunning cython neurons") # cython neuron b = Brunel2000() b.run("iaf_psc_delta", opt, ms)
def test_BeginEndSubnet(self): """Begin/End Subnet""" nest.ResetKernel() nest.BeginSubnet() sn = nest.EndSubnet() nest.BeginSubnet(label='testlabel') sn = nest.EndSubnet() self.assertEqual(nest.GetStatus(sn, 'label')[0], 'testlabel')
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_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_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_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_UnknownModel(self): """Unknown model name""" nest.ResetKernel() try: nest.Create(-1) self.fail('an error should have risen!') # should not be reached except nest.NESTError: info = sys.exc_info()[1] if not "UnknownModelName" in info.__str__(): self.fail('wrong error message') # another error has been thrown, this is wrong except: self.fail('wrong error has been thrown')