{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Connect two populations with convergent projection and rectangular mask, visualize connection from target perspective\n\nCreate two populations of iaf_psc_alpha neurons on a 30x30 grid\nBCCN Tutorial @ CNS*09\nHans Ekkehard Plesser, UMB\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import nest\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nnest.ResetKernel()\nnest.set_verbosity('M_WARNING')\n\npos = nest.spatial.grid(shape=[30, 30], extent=[3., 3.], edge_wrap=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "create and connect two populations\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "a = nest.Create('iaf_psc_alpha', positions=pos)\nb = nest.Create('iaf_psc_alpha', positions=pos)\n\nnest.Connect(a, b,\n             conn_spec={'rule': 'pairwise_bernoulli',\n                        'p': 0.5,\n                        'use_on_source': True,\n                        'mask': {'rectangular': {'lower_left': [-0.2, -0.5],\n                                                 'upper_right': [0.2, 0.5]}}},\n             syn_spec={'weight': nest.random.uniform(0.5, 2.)})\nplt.clf()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "plot sources of neurons in different grid locations\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "for tgt_index in [30 * 15 + 15, 0]:\n    # obtain node id for center\n    tgt = a[tgt_index:tgt_index + 1]\n\n    # obtain list of outgoing connections for ctr\n    spos = nest.GetTargetPositions(tgt, b)[0]\n\n    spos_x = np.array([x for x, y in spos])\n    spos_y = np.array([y for x, y in spos])\n\n    print(spos_x)\n    print(spos_y)\n\n    # scatter-plot\n    plt.scatter(spos_x, spos_y, 20, zorder=10)\n\n    # mark sender position with transparent red circle\n    ctrpos = np.array(nest.GetPosition(tgt))\n    plt.gca().add_patch(plt.Circle(ctrpos, radius=0.1, zorder=99,\n                                   fc='r', alpha=0.4, ec='none'))\n\n    # mark mask position with open red rectangle\n    plt.gca().add_patch(\n        plt.Rectangle(ctrpos - (0.2, 0.5), 0.4, 1.0, zorder=1,\n                      fc='none', ec='r', lw=3))\n\n# mark layer edge\nplt.gca().add_patch(plt.Rectangle((-1.5, -1.5), 3.0, 3.0, zorder=1,\n                                  fc='none', ec='k', lw=3))\n\n# beautify\nplt.axes().set_xticks(np.arange(-1.5, 1.55, 0.5))\nplt.axes().set_yticks(np.arange(-1.5, 1.55, 0.5))\nplt.grid(True)\nplt.axis([-2.0, 2.0, -2.0, 2.0])\nplt.axes().set_aspect('equal', 'box')\nplt.title('Connection sources')\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}