.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/synapsecollection.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_synapsecollection.py: Synapse Collection usage example -------------------------------- Example script to show some of the possibilities of the SynapseCollection class. We connect neurons, and get the SynapseCollection with a GetConnections call. To get a better understanding of the connections, we plot the weights between the source and targets. .. GENERATED FROM PYTHON SOURCE LINES 31-63 .. code-block:: default import nest import matplotlib.pyplot as plt import numpy as np def makeMatrix(sources, targets, weights): """ Returns a matrix with the weights between the source and target node_ids. """ aa = np.zeros((max(sources) + 1, max(targets) + 1)) for src, trg, wght in zip(sources, targets, weights): aa[src, trg] += wght return aa def plotMatrix(srcs, tgts, weights, title, pos): """ Plots weight matrix. """ plt.subplot(pos) plt.matshow(makeMatrix(srcs, tgts, weights), fignum=False) plt.xlim([min(tgts) - 0.5, max(tgts) + 0.5]) plt.xlabel('target') plt.ylim([max(srcs) + 0.5, min(srcs) - 0.5]) plt.ylabel('source') plt.title(title) plt.colorbar(fraction=0.046, pad=0.04) .. GENERATED FROM PYTHON SOURCE LINES 64-68 Start with a simple, one_to_one example. We create the neurons, connect them, and get the connections. From this we can get the connected sources, targets, and weights. The corresponding matrix will be the identity matrix, as we have a one_to_one connection. .. GENERATED FROM PYTHON SOURCE LINES 68-76 .. code-block:: default nest.ResetKernel() nrns = nest.Create('iaf_psc_alpha', 10) nest.Connect(nrns, nrns, 'one_to_one') conns = nest.GetConnections(nrns, nrns) # This returns a SynapseCollection .. GENERATED FROM PYTHON SOURCE LINES 77-78 We can get desired information of the SynapseCollection with simple get() call. .. GENERATED FROM PYTHON SOURCE LINES 78-84 .. code-block:: default g = conns.get(['source', 'target', 'weight']) srcs = g['source'] tgts = g['target'] weights = g['weight'] .. GENERATED FROM PYTHON SOURCE LINES 85-86 Plot the matrix consisting of the weights between the sources and targets .. GENERATED FROM PYTHON SOURCE LINES 86-90 .. code-block:: default plt.figure(figsize=(12, 10)) plotMatrix(srcs, tgts, weights, 'Uniform weight', 121) .. GENERATED FROM PYTHON SOURCE LINES 91-92 Add some weights to the connections, and plot the updated weight matrix. .. GENERATED FROM PYTHON SOURCE LINES 94-95 We can set data of the connections with a simple set() call. .. GENERATED FROM PYTHON SOURCE LINES 95-102 .. code-block:: default w = [{'weight': x * 1.0} for x in range(1, 11)] conns.set(w) weights = conns.weight plotMatrix(srcs, tgts, weights, 'Set weight', 122) .. GENERATED FROM PYTHON SOURCE LINES 103-105 We can also plot an all_to_all connection, with uniformly distributed weights, and different number of sources and targets. .. GENERATED FROM PYTHON SOURCE LINES 105-112 .. code-block:: default nest.ResetKernel() pre = nest.Create('iaf_psc_alpha', 10) post = nest.Create('iaf_psc_delta', 5) nest.Connect(pre, post, syn_spec={'weight': nest.random.uniform(min=0.5, max=4.5)}) .. GENERATED FROM PYTHON SOURCE LINES 113-114 Get a SynapseCollection with all connections .. GENERATED FROM PYTHON SOURCE LINES 114-123 .. code-block:: default conns = nest.GetConnections() srcs = conns.source tgts = conns.target weights = conns.weight plt.figure(figsize=(12, 10)) plotMatrix(srcs, tgts, weights, 'All to all connection', 111) .. GENERATED FROM PYTHON SOURCE LINES 124-127 Lastly, we'll do an exmple that is a bit more complex. We connect different neurons with different rules, synapse models and weight distributions, and get different SynapseCollections by calling GetConnections with different inputs. .. GENERATED FROM PYTHON SOURCE LINES 127-146 .. code-block:: default nest.ResetKernel() nrns = nest.Create('iaf_psc_alpha', 15) nest.Connect(nrns[:5], nrns[:5], 'one_to_one', {'synapse_model': 'stdp_synapse', 'weight': nest.random.normal(mean=5.0, std=2.0)}) nest.Connect(nrns[:10], nrns[5:12], {'rule': 'pairwise_bernoulli', 'p': 0.4}, {'weight': 4.0}) nest.Connect(nrns[5:10], nrns[:5], {'rule': 'fixed_total_number', 'N': 5}, {'weight': 3.0}) nest.Connect(nrns[10:], nrns[:12], 'all_to_all', {'synapse_model': 'stdp_synapse', 'weight': nest.random.uniform(min=1., max=5.)}) nest.Connect(nrns, nrns[12:], {'rule': 'fixed_indegree', 'indegree': 3}) .. GENERATED FROM PYTHON SOURCE LINES 147-148 First get a SynapseCollection consisting of all the connections .. GENERATED FROM PYTHON SOURCE LINES 148-157 .. code-block:: default conns = nest.GetConnections() srcs = conns.source tgts = conns.target weights = conns.weight plt.figure(figsize=(14, 12)) plotMatrix(list(srcs), list(tgts), weights, 'All connections', 221) .. GENERATED FROM PYTHON SOURCE LINES 158-159 Get SynapseCollection consisting of a subset of connections .. GENERATED FROM PYTHON SOURCE LINES 159-168 .. code-block:: default conns = nest.GetConnections(nrns[:10], nrns[:10]) g = conns.get(['source', 'target', 'weight']) srcs = g['source'] tgts = g['target'] weights = g['weight'] plotMatrix(srcs, tgts, weights, 'Connections of the first ten neurons', 222) .. GENERATED FROM PYTHON SOURCE LINES 169-170 Get SynapseCollection consisting of just the stdp_synapses .. GENERATED FROM PYTHON SOURCE LINES 170-179 .. code-block:: default conns = nest.GetConnections(synapse_model='stdp_synapse') g = conns.get(['source', 'target', 'weight']) srcs = g['source'] tgts = g['target'] weights = g['weight'] plotMatrix(srcs, tgts, weights, 'Connections with stdp_synapse', 223) .. GENERATED FROM PYTHON SOURCE LINES 180-182 Get SynapseCollection consisting of the fixed_total_number connections, but set weight before plotting .. GENERATED FROM PYTHON SOURCE LINES 182-195 .. code-block:: default conns = nest.GetConnections(nrns[5:10], nrns[:5]) w = [{'weight': x * 1.0} for x in range(1, 6)] conns.set(w) g = conns.get(['source', 'target', 'weight']) srcs = g['source'] tgts = g['target'] weights = g['weight'] plotMatrix(srcs, tgts, weights, 'fixed_total_number, set weight', 224) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_examples_synapsecollection.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: synapsecollection.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: synapsecollection.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_