def disconnect(self): change = self.__disconnect_rate cc = self.__get_connections(self.__layer['inputLayer'], self.__layer['outputLayer']) cc_input_dis = min(self.__predictLayer['inputLayer']) - min(self.__layer['inputLayer']) cc_output_dis = min(self.__predictLayer['outputLayer']) - min(self.__layer['outputLayer']) for c in cc: if abs(c[2] - 1.0) <= change: nest.Disconnect(pre=[c[0]], post=[c[1]], conn_spec={'rule': "one_to_one"}, syn_spec={'model': 'stdp_synapse'}) nest.Disconnect(pre=[c[0] + cc_input_dis], post=[c[1] + cc_output_dis], conn_spec={'rule': "one_to_one"}, syn_spec={'model': 'static_synapse'})
def test_multiple_synapse_deletion_one_to_one_no_sp(self): for syn_model in nest.Models('synapses'): if syn_model not in self.exclude_synapse_model: nest.ResetKernel() nest.CopyModel('static_synapse', 'my_static_synapse') neurons = nest.Create('iaf_psc_alpha', 10) syn_dict = {'synapse_model': syn_model} nest.Connect(neurons, neurons, "all_to_all", syn_dict) src_neurons = neurons[:5] tgt_neurons = neurons[5:] conns = nest.GetConnections(src_neurons, tgt_neurons, syn_model) assert len(conns) == 25 conndictionary = {'rule': 'one_to_one'} syndictionary = {'synapse_model': syn_model} nest.Disconnect( src_neurons, tgt_neurons, conndictionary, syndictionary ) conns = nest.GetConnections(src_neurons, tgt_neurons, syn_model) assert len(conns) == 20
def test_synapse_deletion_one_to_one_no_sp(self): for syn_model in nest.Models('synapses'): if syn_model not in self.exclude_synapse_model: nest.ResetKernel() nest.SetKernelStatus( { 'resolution': 0.1, 'total_num_virtual_procs': self.num_procs } ) neurons = nest.Create('iaf_psc_alpha', 4) syn_dict = {'synapse_model': syn_model} nest.Connect(neurons[0], neurons[2], "one_to_one", syn_dict) nest.Connect(neurons[1], neurons[3], "one_to_one", syn_dict) # Delete existent connection conns = nest.GetConnections( neurons[0], neurons[2], syn_model) if mpi_test: print("rim with mpi") conns = self.comm.allgather(conns.get('source')) conns = list(filter(None, conns)) assert len(conns) == 1 nest.Disconnect(neurons[0], neurons[2], syn_spec=syn_dict) conns = nest.GetConnections( neurons[0], neurons[2], syn_model) if mpi_test: conns = self.comm.allgather(conns.get('source')) conns = list(filter(None, conns)) assert len(conns) == 0 # Assert that one can not delete a non existent connection conns1 = nest.GetConnections( neurons[:1], neurons[1:2], syn_model) if mpi_test: conns1 = self.comm.allgather(conns1.get('source')) conns1 = list(filter(None, conns1)) assert len(conns1) == 0 try: nest.Disconnect(neurons[0], neurons[1], syn_spec=syn_dict) assert False except nest.kernel.NESTError: print("Synapse deletion ok: " + syn_model)
def test_multiple_synapse_deletion_all_to_all(self): for syn_model in nest.Models('synapses'): if syn_model not in self.exclude_synapse_model: nest.ResetKernel() nest.CopyModel('static_synapse', 'my_static_synapse') nest.SetDefaults(syn_model, {'delay': 0.5}) syn_dict = { 'model': syn_model, 'pre_synaptic_element': 'SE1', 'post_synaptic_element': 'SE2' } nest.SetKernelStatus({ 'min_delay': 0.1, 'max_delay': 1.0, 'structural_plasticity_synapses': { 'syn1': syn_dict } }) neurons = nest.Create( 'iaf_psc_alpha', 10, { 'synaptic_elements': { 'SE1': { 'z': 0.0, 'growth_rate': 0.0 }, 'SE2': { 'z': 0.0, 'growth_rate': 0.0 } } }) nest.Connect(neurons, neurons, "all_to_all", syn_dict) # Test if the connected synaptic elements before the simulation # are correct status = nest.GetStatus(neurons, 'synaptic_elements') for st_neuron in status: self.assertEqual(10, st_neuron['SE1']['z_connected']) self.assertEqual(10, st_neuron['SE2']['z_connected']) srcId = range(0, 5) targId = range(5, 10) conns = nest.GetConnections(srcId, targId, syn_model) assert conns conndictionary = {'rule': 'all_to_all'} syndictionary = {'model': syn_model} nest.Disconnect([neurons[i] for i in srcId], [neurons[i] for i in targId], conndictionary, syndictionary) status = nest.GetStatus(neurons, 'synaptic_elements') for st_neuron in status[0:5]: self.assertEqual(5, st_neuron['SE1']['z_connected']) self.assertEqual(10, st_neuron['SE2']['z_connected']) for st_neuron in status[5:10]: self.assertEqual(10, st_neuron['SE1']['z_connected']) self.assertEqual(5, st_neuron['SE2']['z_connected'])
def test_disconnect_defaults(self): nodes = nest.Create('iaf_psc_alpha', 5) nest.Connect(nodes, nodes) self.assertEqual(nest.GetKernelStatus('num_connections'), 25) nest.Disconnect(nodes, nodes) self.assertEqual(nest.GetKernelStatus('num_connections'), 20)
def test_disconnect_static_synapse(self): nodes = nest.Create('iaf_psc_alpha', 5) nest.Connect(nodes, nodes) self.assertEqual(nest.GetKernelStatus('num_connections'), 25) nest.Disconnect(nodes, nodes, syn_spec='static_synapse') self.assertEqual(nest.GetKernelStatus('num_connections'), 20)
def test_disconnect_all_to_all(self): nodes = nest.Create('iaf_psc_alpha', 5) nest.Connect(nodes, nodes) self.assertEqual(nest.num_connections, 25) nest.Disconnect(nodes, nodes, 'all_to_all') self.assertEqual(nest.num_connections, 0)
def test_single_synapse_deletion_sp(self): for syn_model in nest.Models('synapses'): if syn_model not in self.exclude_synapse_model: nest.ResetKernel() nest.CopyModel('static_synapse', 'my_static_synapse') syn_dict = { 'synapse_model': syn_model, 'pre_synaptic_element': 'SE1', 'post_synaptic_element': 'SE2' } # nest.SetKernelStatus( # {'structural_plasticity_synapses': {'syn1': syn_dict}} # ) neurons = nest.Create( 'iaf_psc_alpha', 2, { 'synaptic_elements': { 'SE1': { 'z': 0.0, 'growth_rate': 0.0 }, 'SE2': { 'z': 0.0, 'growth_rate': 0.0 } } }) nest.Connect(neurons, neurons, "all_to_all", syn_dict) nest.Connect(neurons, neurons, "all_to_all", {'synapse_model': 'my_static_synapse'}) # Test if the connected synaptic elements before the simulation # are correct status = nest.GetStatus(neurons, 'synaptic_elements') for st_neuron in status: self.assertEqual(2, st_neuron['SE1']['z_connected']) self.assertEqual(2, st_neuron['SE2']['z_connected']) srcId = 0 targId = 1 conns = nest.GetConnections(neurons[srcId], neurons[targId], syn_model) assert conns nest.Disconnect(neurons[srcId], neurons[targId], syn_spec=syn_dict) status = nest.GetStatus(neurons, 'synaptic_elements') self.assertEqual(1, status[srcId]['SE1']['z_connected']) self.assertEqual(2, status[srcId]['SE2']['z_connected']) self.assertEqual(2, status[targId]['SE1']['z_connected']) self.assertEqual(1, status[targId]['SE2']['z_connected']) conns = nest.GetConnections(neurons[srcId], neurons[targId], syn_model) assert not conns
def test_multiple_synapse_deletion_one_to_one(self): for syn_model in nest.Models('synapses'): if syn_model not in self.exclude_synapse_model: nest.ResetKernel() nest.CopyModel('static_synapse', 'my_static_synapse') nest.SetDefaults(syn_model, {'delay': 0.5}) syn_dict = { 'synapse_model': syn_model, 'pre_synaptic_element': 'SE1', 'post_synaptic_element': 'SE2' } # For co-dependent properties, we use `set()` instead of kernel attributes nest.set(min_delay=0.1, max_delay=1.0) nest.structural_plasticity_synapses = {'syn1': syn_dict} neurons = nest.Create( 'iaf_psc_alpha', 10, { 'synaptic_elements': { 'SE1': { 'z': 0.0, 'growth_rate': 0.0 }, 'SE2': { 'z': 0.0, 'growth_rate': 0.0 } } }) nest.Connect(neurons, neurons, "all_to_all", syn_dict) # Test if the connected synaptic elements before the simulation # are correct status = nest.GetStatus(neurons, 'synaptic_elements') for st_neuron in status: self.assertEqual(10, st_neuron['SE1']['z_connected']) self.assertEqual(10, st_neuron['SE2']['z_connected']) src_neurons = neurons[:5] tgt_neurons = neurons[5:] conns = nest.GetConnections(src_neurons, tgt_neurons, syn_model) assert conns conndictionary = {'rule': 'one_to_one'} syndictionary = {'synapse_model': syn_model} nest.Disconnect(src_neurons, tgt_neurons, conndictionary, syndictionary) status = nest.GetStatus(neurons, 'synaptic_elements') for st_neuron in status[0:5]: self.assertEqual(9, st_neuron['SE1']['z_connected']) self.assertEqual(10, st_neuron['SE2']['z_connected']) for st_neuron in status[5:10]: self.assertEqual(10, st_neuron['SE1']['z_connected']) self.assertEqual(9, st_neuron['SE2']['z_connected'])
def remove_weak_conns(self, connlist, model): """ Checks every synapse and removes weak ones in connlis. :param connlist: :param model: :return: """ def checkIfMatch(a, b): return a[0] == b[0] and a[1] == b[1] if len(connlist) == 0: return False removed = False tobe_removed = [] for i, conn in enumerate(connlist): # get directly form backend because we are editing the back-end, which will outdate teh connection nestconn = nest.GetConnections( source=nest.NodeCollection([conn[0]]), target=nest.NodeCollection([conn[1]])) w = nestconn.get({"weight"})["weight"] if abs(w) < gv.strp_min: print(f"Disconnecting {conn}") nest.Disconnect(nest.NodeCollection([conn[0]]), nest.NodeCollection([conn[1]]), syn_spec={'synapse_model': model}) tobe_removed.append(conn) removed = True # delete from conns connpair = (conn[0], conn[1]) if model == "stdp_dopamine_synapse_ex": for i, n in enumerate(self.conns_ex): if checkIfMatch(n, connpair): del self.conns_ex[i] break else: for i, n in enumerate(self.conns_in): if checkIfMatch(n, connpair): del self.conns_in[i] break for i, n in enumerate(self.conns): if checkIfMatch(n, connpair): del self.conns[i] break if removed: self.synapsecontingent += len(tobe_removed) for delete in tobe_removed: if delete in connlist: # might be already deleted when using conns_ex or conns_in connlist.remove(delete) self.update_connections_nest() return removed
def test_synapse_deletion_one_to_one_no_sp(self): for syn_model in nest.synapse_models: if syn_model not in self.exclude_synapse_model: nest.ResetKernel() nest.resolution = 0.1 nest.total_num_virtual_procs = nest.num_processes neurons = nest.Create('iaf_psc_alpha', 4) syn_dict = {'synapse_model': syn_model} nest.Connect(neurons[0], neurons[2], "one_to_one", syn_dict) nest.Connect(neurons[1], neurons[3], "one_to_one", syn_dict) # Delete existent connection conns = nest.GetConnections(neurons[0], neurons[2], syn_model) if test_with_mpi: conns = self.comm.allgather(conns.get('source')) conns = list(filter(None, conns)) assert len(conns) == 1 nest.Disconnect(neurons[0], neurons[2], syn_spec=syn_dict) conns = nest.GetConnections(neurons[0], neurons[2], syn_model) if test_with_mpi: conns = self.comm.allgather(conns.get('source')) conns = list(filter(None, conns)) assert len(conns) == 0 # Assert that one cannot delete a non-existing connection conns1 = nest.GetConnections(neurons[:1], neurons[1:2], syn_model) if test_with_mpi: conns1 = self.comm.allgather(conns1.get('source')) conns1 = list(filter(None, conns1)) assert len(conns1) == 0 with self.assertRaises(nest.NESTErrors.NESTError): nest.Disconnect(neurons[0], neurons[1], syn_spec=syn_dict)
def test_threaded_disconnect(self): """Test that threaded disconnect does not produce segmentation fault""" nest.ResetKernel() nest.set_verbosity('M_ERROR') nest.SetKernelStatus({'local_num_threads': 2}) neurons = nest.Create('iaf_psc_alpha', 3) nest.Connect(neurons[0], neurons[2]) conns = nest.GetConnections() self.assertEqual(len(conns), 1) # Make sure we are able to call Disconnect when we have number of threads more than one nest.Disconnect(neurons[0], neurons[2]) conns = nest.GetConnections() self.assertEqual(len(conns), 0)
def test_multiple_synapse_deletion_one_to_one_no_sp(self): for syn_model in nest.Models('synapses'): if syn_model not in self.exclude_synapse_model: nest.ResetKernel() nest.CopyModel('static_synapse', 'my_static_synapse') neurons = nest.Create('iaf_psc_alpha', 10) syn_dict = {'model': syn_model} nest.Connect(neurons, neurons, "all_to_all", syn_dict) srcId = range(0, 5) targId = range(5, 10) conns = nest.GetConnections(srcId, targId, syn_model) assert len(conns) == 20 conndictionary = {'rule': 'one_to_one'} syndictionary = {'model': syn_model} nest.Disconnect([neurons[i] for i in srcId], [neurons[i] for i in targId], conndictionary, syndictionary) conns = nest.GetConnections(srcId, targId, syn_model) assert len(conns) == 16