{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Intermediate usage example\n\nHere we show how to use NNMT to calculate the power spectra of the\n:cite:t:`potjans2014` microcircuit model.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nimport nnmt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First we try loading previous results, which makes running this script for\nthe second time much faster. If there are no stored results, we instantiate\na microcircuit model.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "try:\n    network = nnmt.models.Network(file='microcircuit.h5')\nexcept IOError:\n    network = nnmt.models.Microcircuit('network_params.yaml',\n                                       'analysis_params.yaml')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We compute the working point, which only requires the network parameters\ndefined in ``network_params.yaml`` (see minimal example as well).\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print('Compute working point')\nnnmt.lif.exp.working_point(network)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Then we continue by computing the transfer function, the delay distribution\nmatrix, the effective connectivity, and finally the power spectrum. This\nrequires the definition of the analysis frequencies in\n``analysis_params.yaml``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print('Compute transfer function')\nnnmt.lif.exp.transfer_function(network)\nprint('Compute delay_dist_matrix')\nnnmt.network_properties.delay_dist_matrix(network)\nprint('Compute effective connectivity')\nnnmt.lif.exp.effective_connectivity(network)\nprint('Compute power spectra')\nnnmt.lif.exp.power_spectra(network)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We retrieve the analysis frequencies and power spectra and plot the results\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "freqs = network.analysis_params['omegas'] / 2 / np.pi\nspectra = network.results['lif.exp.power_spectra'].T\n\nfor i in range(8):\n    plt.plot(freqs, spectra[i], label=network.network_params['populations'][i])\nplt.yscale('log')\nplt.xlabel('frequency (1/s)')\nplt.ylabel('power')\nplt.legend()\nplt.show()\n\n# save results to h5 file\nnetwork.save('microcircuit.h5')"
      ]
    }
  ],
  "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.9.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}