{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Create two populations of pyramidal cells and two populations of interneurons\n\nCreate two populations of pyramidal cells and two populations of interneurons\non a 30x30 grid. Connect with two projections, one pyr->pyr, one pyr->in, and\nvisualize.\n\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\nnest.CopyModel('iaf_psc_alpha', 'pyr')\nnest.CopyModel('iaf_psc_alpha', 'in')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "same positions for all populations\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pos = nest.spatial.grid(shape=[30, 30], extent=[3., 3.])\n\na_pyr = nest.Create('pyr', positions=pos)\na_in = nest.Create('in', positions=pos)\n\nb_pyr = nest.Create('pyr', positions=pos)\nb_in = nest.Create('in', positions=pos)\n\nnest.Connect(a_pyr, b_pyr, {'rule': 'pairwise_bernoulli',\n                            'p': 0.5,\n                            'mask': {'circular': {'radius': 0.5}}})\n\nnest.Connect(a_pyr, b_in, {'rule': 'pairwise_bernoulli',\n                           'p': 0.2,\n                           'mask': {'circular': {'radius': 1.}}})\n\nplt.clf()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "plot targets of neurons in different grid locations\nobtain node id for center: pick first node of composite\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ctr_index = 30 * 15 + 15\nctr_id = a_pyr[ctr_index:ctr_index + 1]\n\n# get all projection targets of center neuron\nconn = nest.GetConnections(ctr_id)\ntgts = conn.get('target')\n\ntpyr = nest.GetTargetPositions(ctr_id, b_pyr)[0]\ntin = nest.GetTargetPositions(ctr_id, b_in)[0]\n\ntpyr_x = np.array([x for x, y in tpyr])\ntpyr_y = np.array([y for x, y in tpyr])\ntin_x = np.array([x for x, y in tin])\ntin_y = np.array([y for x, y in tin])\n\n# scatter-plot\nplt.scatter(tpyr_x - 0.02, tpyr_y - 0.02, 20, 'b', zorder=10)\nplt.scatter(tin_x + 0.02, tin_y + 0.02, 20, 'r', zorder=10)\n\n# mark locations with background grey circle\nplt.plot(tpyr_x, tpyr_y, 'o', markerfacecolor=(0.7, 0.7, 0.7),\n         markersize=10, markeredgewidth=0, zorder=1, label='_nolegend_')\nplt.plot(tin_x, tin_y, 'o', markerfacecolor=(0.7, 0.7, 0.7),\n         markersize=10, markeredgewidth=0, zorder=1, label='_nolegend_')\n\n# mark sender position with transparent red circle\nctrpos = nest.GetPosition(ctr_id)\nplt.gca().add_patch(plt.Circle(ctrpos, radius=0.15, zorder=99,\n                               fc='r', alpha=0.4, ec='none'))\n\n# mark mask positions with open red/blue circles\nplt.gca().add_patch(plt.Circle(ctrpos, radius=0.5, zorder=2,\n                               fc='none', ec='b', lw=3))\nplt.gca().add_patch(plt.Circle(ctrpos, radius=1.0, zorder=2,\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([-1.6, 1.6, -1.6, 1.6])\nplt.axes().set_aspect('equal', 'box')\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
}