.. 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 :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_synapsecollection.py: Synapse Collection usage example -------------------------------- .. only:: html ---- Run this example as a Jupyter notebook: .. card:: :width: 25% :margin: 2 :text-align: center :link: https://lab.ebrains.eu/hub/user-redirect/git-pull?repo=https%3A%2F%2Fgithub.com%2Fnest%2Fnest-simulator-examples&urlpath=lab%2Ftree%2Fnest-simulator-examples%2Fnotebooks%2Fnotebooks%2Fsynapsecollection.ipynb&branch=main :link-alt: JupyterHub service .. image:: https://nest-simulator.org/TryItOnEBRAINS.png .. grid:: 1 1 1 1 :padding: 0 0 2 0 .. grid-item:: :class: sd-text-muted :margin: 0 0 3 0 :padding: 0 0 3 0 :columns: 4 See :ref:`our guide ` for more information and troubleshooting. ---- 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:: Python import matplotlib.pyplot as plt import nest 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:: Python 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:: Python 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:: Python 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:: Python 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:: Python 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:: Python 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-144 .. code-block:: Python 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.0, max=5.0)}, ) nest.Connect(nrns, nrns[12:], {"rule": "fixed_indegree", "indegree": 3}) .. GENERATED FROM PYTHON SOURCE LINES 145-146 First get a SynapseCollection consisting of all the connections .. GENERATED FROM PYTHON SOURCE LINES 146-155 .. code-block:: Python 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 156-157 Get SynapseCollection consisting of a subset of connections .. GENERATED FROM PYTHON SOURCE LINES 157-166 .. code-block:: Python 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 167-168 Get SynapseCollection consisting of just the stdp_synapses .. GENERATED FROM PYTHON SOURCE LINES 168-177 .. code-block:: Python 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 178-180 Get SynapseCollection consisting of the fixed_total_number connections, but set weight before plotting .. GENERATED FROM PYTHON SOURCE LINES 180-193 .. code-block:: Python 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() .. _sphx_glr_download_auto_examples_synapsecollection.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: synapsecollection.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: synapsecollection.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_