{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0b85a598",
   "metadata": {},
   "outputs": [],
   "source": [
    "from brian2 import *\n",
    "\n",
    "from adex_sine import *\n",
    "\n",
    "defaultclock.dt = 10 * us\n",
    "\n",
    "\n",
    "class Model:\n",
    "    # Model type flags\n",
    "    SINE = 0\n",
    "    EXP2SYN = 1\n",
    "\n",
    "    # Noise flags\n",
    "    HIGH = 0\n",
    "    LOW = 1\n",
    "    OFF = -1\n",
    "\n",
    "    # Noise parameters\n",
    "    EXCITATORY_NOISE_VARIANCE = {HIGH: 0.5 * nS, LOW: 0.25 * nS, OFF: 0 * nS}\n",
    "    INHIBITORY_NOISE_VARIANCE = {HIGH: 1.25 * nS, LOW: 0.625 * nS, OFF: 0 * nS}\n",
    "\n",
    "    # Noise mean conductance\n",
    "    EXCITATORY_CONDUCTANCE = 1 * nS\n",
    "    INHIBITORY_CONDUCTANCE = 4 * nS\n",
    "\n",
    "    DEFAULT_PARAMETERS = {\n",
    "        \"sigma_flux\" : 6.75*pA,     \n",
    "        \"c\": 85 * pF,\n",
    "        \"tau_w\": 19.240498 * ms,\n",
    "        \"b\": 0.25 * nA,\n",
    "        \"a\": 1.3 * nS,\n",
    "        \"v_T\": -45 * mV,\n",
    "        \"v_thresh\": 0 * mV,\n",
    "        \"DeltaT\": 0.2 * mV,\n",
    "        # EQUILIBRIUM POTENTIAL\n",
    "        \"e_l\": -65 * mV,\n",
    "        \"e_ex\": 0 * mV,\n",
    "        \"e_in\": -70 * mV,\n",
    "        # CONDUCTANCES\n",
    "        \"g_l\": 3 * nS,\n",
    "        \"mu_ex\": 0 * nS,\n",
    "        \"mu_in\": 0 * nS,\n",
    "        # EXCITATORY NOISE\n",
    "        \"sigma_ex\": 0 * nS,\n",
    "        \"tau_noise_ex\": 3 * ms,\n",
    "        # INHIBITORY NOISE\n",
    "        \"sigma_in\": 0 * nS,\n",
    "        \"tau_noise_in\": 10 * ms,\n",
    "        # SINE INPUT\n",
    "        \"f\": 100 * Hz,\n",
    "        \"A\": 0 * pA,\n",
    "        \"i_injected\": 0 * pA,\n",
    "        \"v_reset\": -70 * mV,\n",
    "        # m current\n",
    "        \"g_adapt\": 10 * nS,\n",
    "        \"e_k\": -90*mV,\n",
    "        \"beta_z\": -35*mV,\n",
    "        \"gamma_z\": 4*mV,     #5\n",
    "        \"tau_z\": 100*ms,\n",
    "    }\n",
    "\n",
    "    def __init__(\n",
    "        self, n, *, stim=None, noise=None, resistance=None, additional_vars=()\n",
    "    ):\n",
    "        if resistance is None:\n",
    "            raise ValueError(\"Resistance must be specified\")\n",
    "\n",
    "        if noise is None:\n",
    "            raise ValueError(\"Noise must be specified\")\n",
    "\n",
    "        self.stim_type = stim\n",
    "        self._input_resistance = None\n",
    "        self._noise_level = None\n",
    "        self._duration = 0\n",
    "        self.recorded_vars = (\"v\",) + additional_vars\n",
    "\n",
    "        self.neurons = self.set_default(n_neuron=n)\n",
    "        self.set_resistance(resistance)\n",
    "        self.set_noise(noise)\n",
    "\n",
    "        self.spikes = None\n",
    "        self.spiker = None\n",
    "        self.synapses = None\n",
    "        self.inhib_synapses = None\n",
    "        self.smon = None\n",
    "        self.network = None\n",
    "        self.build_network()\n",
    "\n",
    "    def create_model(self):\n",
    "        return ADEX_MODEL, self.DEFAULT_PARAMETERS\n",
    "\n",
    "    def set_default(self, n_neuron):\n",
    "        model, parameters = self.create_model()\n",
    "\n",
    "        neurons = NeuronGroup(\n",
    "            n_neuron,\n",
    "            model=model,\n",
    "            method=\"Euler\",\n",
    "            name=\"neurons\",\n",
    "            threshold=\"v > v_thresh\",\n",
    "            reset=\"v = v_reset; w += b\",\n",
    "        )\n",
    "\n",
    "        for parameter, value in parameters.items():\n",
    "            neurons.__setattr__(parameter, value)\n",
    "\n",
    "        neurons.v = neurons.e_l  # remove most of transient\n",
    "\n",
    "        return neurons\n",
    "\n",
    "    def set_resistance(self, level):\n",
    "        if level == self.LOW:\n",
    "            exc_conductance = self.EXCITATORY_CONDUCTANCE\n",
    "            inhib_conductance = self.INHIBITORY_CONDUCTANCE\n",
    "\n",
    "        else:\n",
    "            exc_conductance = inhib_conductance = 0\n",
    "\n",
    "        self._input_resistance = level\n",
    "        self._set_variable(\"mu_ex\", exc_conductance)\n",
    "        self._set_variable(\"mu_in\", inhib_conductance)\n",
    "\n",
    "    def set_noise(self, level):\n",
    "        if level == self.HIGH or level == self.LOW:\n",
    "            exc_noise = self.EXCITATORY_NOISE_VARIANCE[level]\n",
    "            inhib_noise = self.INHIBITORY_NOISE_VARIANCE[level]\n",
    "\n",
    "        else:\n",
    "            exc_noise = inhib_noise = 0\n",
    "\n",
    "        self._noise_level = level\n",
    "        self._set_variable(\"sigma_ex\", exc_noise)\n",
    "        self._set_variable(\"sigma_in\", inhib_noise)\n",
    "\n",
    "    def set_injected_current(self, amplitude):\n",
    "        self._set_variable(\"i_injected\", amplitude)\n",
    "        self._set_variable(\"A\", 0 * pA)\n",
    "\n",
    "    def set_stimulus_current(self, amplitude):\n",
    "        self._set_variable(\"A\", amplitude)\n",
    "        self._set_variable(\"i_injected\", 0 * pA)\n",
    "\n",
    "    @property\n",
    "    def f(self):\n",
    "        return self.neurons.f\n",
    "\n",
    "    @f.setter\n",
    "    def f(self, new_f):\n",
    "        self._set_variable(\"f\", new_f)  # this will reset smon\n",
    "        if self.stim_type == self.EXP2SYN:\n",
    "            self.spiker.T = 1 / new_f\n",
    "\n",
    "    def run(self, duration, report=\"stdout\"):\n",
    "        self._duration = duration\n",
    "        self.network.run(duration, report=report)\n",
    "\n",
    "    def build_network(self):\n",
    "        self.smon = StateMonitor(\n",
    "            self.neurons, self.recorded_vars, record=True, name=\"smon\"\n",
    "        )\n",
    "        self.spikes = SpikeMonitor(self.neurons, name=\"spikes\")\n",
    "\n",
    "        self.network = Network(self.neurons, self.smon, self.spikes)\n",
    "\n",
    "    def _set_variable(self, name, value):\n",
    "        self.neurons.__setattr__(name, value)\n",
    "        self.reset_recording()\n",
    "\n",
    "    def reset_recording(self):\n",
    "        try:\n",
    "            self.network\n",
    "        except AttributeError:\n",
    "            return  # network not yet initialized\n",
    "\n",
    "        self.network.remove(self.smon, self.spikes)\n",
    "\n",
    "        self.smon = StateMonitor(\n",
    "            self.neurons, self.recorded_vars, record=True, name=\"smon\"\n",
    "        )\n",
    "        self.spikes = SpikeMonitor(self.neurons, name=\"spikes\")\n",
    "\n",
    "        self.network.add(self.smon, self.spikes)\n",
    "\n",
    "    @property\n",
    "    def spike_train(self):\n",
    "        return self.spikes.spike_trains()\n",
    "\n",
    "    @property\n",
    "    def firing_rate(self):\n",
    "        return self.spikes.count / self.duration\n",
    "\n",
    "    @property\n",
    "    def duration(self):\n",
    "        return self._duration\n",
    "\n",
    "    @property\n",
    "    def input_resistance(self):\n",
    "        if self._input_resistance == self.HIGH:\n",
    "            return \"HIGH\"\n",
    "        else:\n",
    "            return \"LOW\"\n",
    "\n",
    "    @property\n",
    "    def noise_level(self):\n",
    "        if self._noise_level == self.HIGH:\n",
    "            return \"HIGH\"\n",
    "        elif self._noise_level == self.LOW:\n",
    "            return \"LOW\"\n",
    "        else:\n",
    "            return \"NO\"\n",
    "\n",
    "    def __repr__(self):\n",
    "        return f\"{self.neurons.N} Neurons with {self.input_resistance} input resistance and {self.noise_level} noise\"\n",
    "\n",
    "    def __str__(self):\n",
    "        return self.__repr__()\n",
    "\n",
    "    def store(self, name):\n",
    "        self.network.store(name)\n",
    "\n",
    "    def restore(self, name):\n",
    "        self.network.restore(name)\n",
    "\n",
    "    @property\n",
    "    def v(self):\n",
    "        return self.smon.v\n",
    "\n",
    "    @property\n",
    "    def t(self):\n",
    "        return self.smon.t\n",
    "\n",
    "    @property\n",
    "    def injected_current(self):\n",
    "        return self.neurons.i_injected\n",
    "\n",
    "    @property\n",
    "    def stimulus_amplitude(self):\n",
    "        return self.neurons.A\n",
    "\n",
    "\n",
    "class CurrentModel(Model):\n",
    "    def __init__(self, **kwargs):\n",
    "        super().__init__(stim=self.SINE, **kwargs)\n",
    "\n",
    "    def create_model(self):\n",
    "        model, parameters = super().create_model()\n",
    "        model += CURRENT_INPUT\n",
    "\n",
    "        return model, parameters\n",
    "\n",
    "\n",
    "class SineModel(CurrentModel):\n",
    "    def create_model(self):\n",
    "        model, parameters = super().create_model()\n",
    "        model += SINE_INPUT\n",
    "\n",
    "        return model, parameters\n",
    "\n",
    "\n",
    "class SawModel(CurrentModel):\n",
    "    def create_model(self):\n",
    "        model, parameters = super().create_model()\n",
    "        model += SAW_INPUT\n",
    "\n",
    "        return model, parameters\n",
    "\n",
    "\n",
    "class SynapticModel(Model):\n",
    "    def __init__(self, **kwargs):\n",
    "        super().__init__(stim=self.EXP2SYN, **kwargs)\n",
    "\n",
    "    SYNAPTIC_PARAMETERS = {\n",
    "        \"tau_input_1\": 0.4 * ms,\n",
    "        \"tau_input_2\": 4 * ms,\n",
    "        \"offset_A\": 1.48793507e-11,\n",
    "        \"offset_B\": -2.66359562e-08,\n",
    "        \"offset_C\": 1.77538800e-05,\n",
    "        \"offset_D\": -8.05925810e-04,\n",
    "        \"offset_E\": -3.51463644e-02,\n",
    "        \"offset_switch\": 0,\n",
    "    }\n",
    "\n",
    "    def create_model(self):\n",
    "        model, parameters = super().create_model()\n",
    "        model += EXP2SYN_WAVEFORM + SUMMATION_OFFSET\n",
    "        parameters = {**parameters, **self.SYNAPTIC_PARAMETERS}\n",
    "\n",
    "        return model, parameters\n",
    "\n",
    "    def build_network(self):\n",
    "        super().build_network()\n",
    "        self.spiker = NeuronGroup(\n",
    "            self.neurons.N,\n",
    "            \"\"\"T : second (constant)\n",
    "                                     lastspike : second\"\"\",\n",
    "            threshold=\"timestep(t-lastspike, dt)>=timestep(T, dt)\",\n",
    "            reset=\"lastspike=t\",\n",
    "        )\n",
    "        self.spiker.T = 1 / self.neurons.f\n",
    "        self.synapses = Synapses(\n",
    "            self.spiker, self.neurons, on_pre=\"input_aux += 1\"\n",
    "        )  # connect input to neurons\n",
    "        self.synapses.connect(\"i==j\")  # one synapse goes to one neuron\n",
    "\n",
    "        self.network.add(self.spiker, self.synapses)\n",
    "\n",
    "\n",
    "class SynapticCurrentModel(SynapticModel):\n",
    "    def __init__(self, offset=True, **kwargs):\n",
    "        self.offset = 1 if offset else 0\n",
    "        super().__init__(**kwargs)\n",
    "\n",
    "    def create_model(self):\n",
    "        model, parameters = super().create_model()\n",
    "        model += CURRENT_INPUT + SYNAPTIC_INPUT_CURRENT\n",
    "        parameters = {**parameters, **{\"offset_switch\": self.offset}}\n",
    "\n",
    "        return model, parameters\n",
    "\n",
    "\n",
    "class SynapticConductanceModel(SynapticModel):\n",
    "    FLAT = 0\n",
    "    ACTIVE = 1\n",
    "\n",
    "    CONDUCTANCE_PARAMETERS = {\n",
    "        \"A\": 0 * nS,  # overwrite A to be conductance\n",
    "        \"g_i\": 1 * nS,\n",
    "    }\n",
    "\n",
    "    INHIBITION_PARAMETERS = {\n",
    "        \"tau_inhibition_1\": 1 * ms,\n",
    "        \"tau_inhibition_2\": 10 * ms,\n",
    "    }\n",
    "\n",
    "    def __init__(self, offset=ACTIVE, **kwargs):\n",
    "        self.offset = offset\n",
    "        super().__init__(**kwargs)\n",
    "\n",
    "    def create_model(self):\n",
    "        model, parameters = super().create_model()\n",
    "        if self.offset == self.FLAT:\n",
    "            model += CONDUCTANCE_INPUT + SYNAPTIC_CONDUCTANCE_FLAT\n",
    "            parameters = {\n",
    "                **parameters,\n",
    "                **self.SYNAPTIC_PARAMETERS,\n",
    "                **self.CONDUCTANCE_PARAMETERS,\n",
    "                **{\"offset_switch\": 1},\n",
    "            }\n",
    "\n",
    "        elif self.offset == self.ACTIVE:\n",
    "            model += CONDUCTANCE_INPUT + SYNAPTIC_CONDUCTANCE_STIM\n",
    "            parameters = {\n",
    "                **parameters,\n",
    "                **self.SYNAPTIC_PARAMETERS,\n",
    "                **self.CONDUCTANCE_PARAMETERS,\n",
    "                **self.INHIBITION_PARAMETERS,\n",
    "            }\n",
    "\n",
    "        return model, parameters\n",
    "\n",
    "    def build_network(self):\n",
    "        super().build_network()\n",
    "        if self.offset != self.ACTIVE:\n",
    "            return\n",
    "\n",
    "        self.inhib_synapses = Synapses(\n",
    "            self.spiker, self.neurons, on_pre=\"input_inhib_aux += 1\", delay=2 * ms\n",
    "        )  # connect input to neurons\n",
    "        self.inhib_synapses.connect(\"i==j\")  # one synapse goes to one neuron\n",
    "\n",
    "        self.network.add(self.inhib_synapses)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c82769f2",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING    Cannot use Cython, a test compilation failed: Microsoft Visual C++ 14.0 or greater is required. Get it with \"Microsoft C++ Build Tools\": https://visualstudio.microsoft.com/visual-cpp-build-tools/ (DistutilsPlatformError) [brian2.codegen.runtime.cython_rt.cython_rt.failed_compile_test]\n",
      "INFO       Cannot use compiled code, falling back to the numpy code generation target. Note that this will likely be slower than using compiled code. Set the code generation to numpy manually to avoid this message:\n",
      "prefs.codegen.target = \"numpy\" [brian2.devices.device.codegen_fallback]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting simulation at t=0. s for a duration of 12. s\n",
      "0.32415 s (2%) simulated in 10s, estimated 6m 0s remaining.\n",
      "0.6406 s (5%) simulated in 20s, estimated 5m 55s remaining.\n",
      "0.93958 s (7%) simulated in 30s, estimated 5m 53s remaining.\n",
      "1.26964 s (10%) simulated in 40s, estimated 5m 38s remaining.\n",
      "1.58262 s (13%) simulated in 50s, estimated 5m 29s remaining.\n",
      "1.91655 s (15%) simulated in 1m 0s, estimated 5m 16s remaining.\n",
      "2.25676 s (18%) simulated in 1m 10s, estimated 5m 2s remaining.\n",
      "2.58152 s (21%) simulated in 1m 20s, estimated 4m 52s remaining.\n",
      "2.9079 s (24%) simulated in 1m 30s, estimated 4m 41s remaining.\n",
      "3.23096 s (26%) simulated in 1m 40s, estimated 4m 31s remaining.\n",
      "3.54291 s (29%) simulated in 1m 50s, estimated 4m 23s remaining.\n",
      "3.86226 s (32%) simulated in 2m 0s, estimated 4m 13s remaining.\n",
      "4.17321 s (34%) simulated in 2m 10s, estimated 4m 4s remaining.\n",
      "4.48419 s (37%) simulated in 2m 20s, estimated 3m 55s remaining.\n",
      "4.78725 s (39%) simulated in 2m 30s, estimated 3m 46s remaining.\n",
      "5.10772 s (42%) simulated in 2m 40s, estimated 3m 36s remaining.\n",
      "5.4584 s (45%) simulated in 2m 50s, estimated 3m 24s remaining.\n",
      "5.76958 s (48%) simulated in 3m 0s, estimated 3m 14s remaining.\n",
      "6.06172 s (50%) simulated in 3m 10s, estimated 3m 6s remaining.\n",
      "6.38039 s (53%) simulated in 3m 20s, estimated 2m 56s remaining.\n",
      "6.72216 s (56%) simulated in 3m 30s, estimated 2m 45s remaining.\n",
      "7.08262 s (59%) simulated in 3m 40s, estimated 2m 33s remaining.\n",
      "7.42151 s (61%) simulated in 3m 50s, estimated 2m 22s remaining.\n",
      "7.74866 s (64%) simulated in 4m 0s, estimated 2m 12s remaining.\n",
      "8.06406 s (67%) simulated in 4m 10s, estimated 2m 2s remaining.\n",
      "8.43561 s (70%) simulated in 4m 20s, estimated 1m 50s remaining.\n",
      "8.8323 s (73%) simulated in 4m 30s, estimated 1m 37s remaining.\n",
      "9.2244 s (76%) simulated in 4m 40s, estimated 1m 24s remaining.\n",
      "9.61809 s (80%) simulated in 4m 50s, estimated 1m 12s remaining.\n",
      "10.02168 s (83%) simulated in 5m 0s, estimated 59s remaining.\n",
      "10.41833 s (86%) simulated in 5m 10s, estimated 47s remaining.\n",
      "10.81462 s (90%) simulated in 5m 20s, estimated 35s remaining.\n",
      "11.21245 s (93%) simulated in 5m 30s, estimated 23s remaining.\n",
      "11.61342 s (96%) simulated in 5m 40s, estimated 11s remaining.\n",
      "12. s (100%) simulated in 5m 49s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[<matplotlib.collections.EventCollection at 0x20686cc86a0>]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD4CAYAAADvsV2wAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAATiklEQVR4nO3df6zdd33f8edrTtyWNCUFX9IsTnCQrKnWREJ6ZZIFQdKOzEZQCwlptigwBnLpEqnt1m7JkEDb/uqYqomS4lrUS+lIwtYkYCFDgrpq6cpCfU3T/Da9M8ly52y+kCqhUCl1994f53vTs8u593yvfXx/+PN8SEf3+/38+J7P596bl7/3c87JJ1WFJOn897fWegCSpNVh4EtSIwx8SWqEgS9JjTDwJakRF6z1AEbZsmVLbdu2ba2HIUkbxrFjx75dVVPLtVmXgb9t2zZmZmbWehiStGEkeXZcG5d0JKkRBr4kNcLAl6RGGPiS1AgDX5IaMTbwk1yR5A+SPJXkiSS/OKJNknwyyWySR5NcO1S3K8nxru62SU9AktRPnzv808A/q6qfBK4DbkmyY1Gb3cD27rEf+DRAkk3AHV39DmDfiL6SpFUwNvCr6vmq+kZ3/F3gKeDyRc32AJ+tgYeBS5JcBuwEZqvqRFW9DNzTtZUkrbIVreEn2Qa8Cfj6oqrLgeeGzue6sqXKR117f5KZJDPz8/MrGZYkqYfegZ/kR4F7gV+qqpcWV4/oUsuU/2Bh1cGqmq6q6ampZT8dLEk6A73+1wpJLmQQ9p+rqvtGNJkDrhg63wqcBDYvUS5JWmV93qUT4LeBp6rq15dodhh4f/duneuAF6vqeeAosD3JVUk2A3u7tpKkVdbnDv8G4H3AY0ke6cr+JXAlQFUdAI4A7wBmge8DH+zqTie5FXgA2AQcqqonJjkBSVI/YwO/qv4bo9fih9sUcMsSdUcY/IMgSVpDftJWkhph4EtSIwx8SWqEgS9JjTDwJakRBr4kNcLAl6RGGPiS1AgDX5IaYeBLUiMMfElq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Xwwjkb+RKSfAB4J/De6tZjWKX5nA+BvyE2Su9eWf9t4Kmq+vWhqsPAB7rjDzBY218o39u9En8VsB344+7P2u8mua675vuH+qyKqrq9qrZW1TYG3+//UlU/t0Hn8r+B55L8na7oZ4An2YBzYbCUc12SV3Vj+BngKTbmXBZMcuzD13oPg9/b1f4rbBfwL4CfrarvD1WtznxW44WYc/1gsIH6Nxm8sv3RtR7PEmN8C4M/tx4FHuke72Cw5vb7wJ91X18z1Oej3ZyOM/QuCWAaeLyr+xTn+IWnMfO6kb950XZDzgW4BpjpfjZfAH58A8/lXwFPd+P4XQbv+tgQcwHuZvDaw18xuHv90CTHDvww8J+BWQbvfHnDGsxnlsG6+0IGHFjN+fi/VpCkRpwPSzqSpB4MfElqhIEvSY0w8CWpEQa+JDXCwJekRhj4ktSI/weHSbclcC4b0gAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#from models import Model, SynapticConductanceModel\n",
    "from brian2 import *\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "\n",
    "model = SynapticConductanceModel(resistance=Model.LOW, # Model.LOW, Model.HIGH\n",
    "                                 noise=Model.HIGH, n=1, # Model.OFF, Model.LOW, Model,HIGH\n",
    "                                 offset=SynapticConductanceModel.ACTIVE)\n",
    "\n",
    "\n",
    "# model = SineModel(resistance=Model.LOW, # Model.LOW, Model.HIGH\n",
    "#                                   noise=Model.OFF, n=1, # Model.OFF, Model.LOW, Model,HIGH\n",
    "#                                 )\n",
    "\n",
    "\n",
    "model.f = 100 * Hz\n",
    "model.set_stimulus_current(400 * nS) # current should be scaled by 100x for Active Offset so (500nS is actually 5nS)\n",
    "model._set_variable(\"i_injected\", 65 * pA)\n",
    "\n",
    "\n",
    "model.run(12*second)\n",
    "\n",
    "spike_times = [s/ms for s in model.spike_train.values()]\n",
    "\n",
    "plt.eventplot(spike_times)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7f22e21e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#print(spike_times)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "20e3191c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import scipy.io\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "file_path = 'data47.mat'\n",
    "scipy.io.savemat(file_path, {'data47': spike_times[0]})\n",
    "\n",
    "# file_path = 'data1.mat'\n",
    "# scipy.io.savemat(file_path, {'data1': spike_times[1]})\n",
    "\n",
    "# file_path = 'data2.mat'\n",
    "# scipy.io.savemat(file_path, {'data2': spike_times[2]})\n",
    "\n",
    "# file_path = 'data3.mat'\n",
    "# scipy.io.savemat(file_path, {'data3': spike_times[3]})\n",
    "\n",
    "# file_path = 'data4.mat'\n",
    "# scipy.io.savemat(file_path, {'data4': spike_times[4]})\n",
    "\n",
    "# file_path = 'data5.mat'\n",
    "# scipy.io.savemat(file_path, {'data5': spike_times[5]})\n",
    "\n",
    "# file_path = 'data6.mat'\n",
    "# scipy.io.savemat(file_path, {'data6': spike_times[6]})\n",
    "\n",
    "# file_path = 'data7.mat'\n",
    "# scipy.io.savemat(file_path, {'data7': spike_times[7]})\n",
    "\n",
    "# file_path = 'data8.mat'\n",
    "# scipy.io.savemat(file_path, {'data8': spike_times[8]})\n",
    "\n",
    "# file_path = 'data9.mat'\n",
    "# scipy.io.savemat(file_path, {'data9': spike_times[9]})\n",
    "\n",
    "# file_path = 'data10.mat'\n",
    "# scipy.io.savemat(file_path, {'data10': spike_times[10]})\n",
    "\n",
    "# file_path = 'data11.mat'\n",
    "# scipy.io.savemat(file_path, {'data11': spike_times[11]})\n",
    "\n",
    "# file_path = 'data12.mat'\n",
    "# scipy.io.savemat(file_path, {'data12': spike_times[12]})\n",
    "\n",
    "# file_path = 'data13.mat'\n",
    "# scipy.io.savemat(file_path, {'data13': spike_times[13]})\n",
    "\n",
    "# file_path = 'data14.mat'\n",
    "# scipy.io.savemat(file_path, {'data14': spike_times[14]})\n",
    "\n",
    "# file_path = 'data15.mat'\n",
    "# scipy.io.savemat(file_path, {'data15': spike_times[15]})\n",
    "\n",
    "# file_path = 'data16.mat'\n",
    "# scipy.io.savemat(file_path, {'data16': spike_times[16]})\n",
    "\n",
    "# file_path = 'data17.mat'\n",
    "# scipy.io.savemat(file_path, {'data17': spike_times[17]})\n",
    "\n",
    "# file_path = 'data18.mat'\n",
    "# scipy.io.savemat(file_path, {'data18': spike_times[18]})\n",
    "\n",
    "# file_path = 'data19.mat'\n",
    "# scipy.io.savemat(file_path, {'data19': spike_times[19]})\n",
    "\n",
    "# file_path = 'data20.mat'\n",
    "# scipy.io.savemat(file_path, {'data20': spike_times[20]})\n",
    "\n",
    "# file_path = 'data21.mat'\n",
    "# scipy.io.savemat(file_path, {'data21': spike_times[21]})\n",
    "\n",
    "# file_path = 'data22.mat'\n",
    "# scipy.io.savemat(file_path, {'data22': spike_times[22]})\n",
    "\n",
    "# file_path = 'data23.mat'\n",
    "# scipy.io.savemat(file_path, {'data23': spike_times[23]})\n",
    "\n",
    "# file_path = 'data24.mat'\n",
    "# scipy.io.savemat(file_path, {'data24': spike_times[24]})\n",
    "\n",
    "# file_path = 'data25.mat'\n",
    "# scipy.io.savemat(file_path, {'data25': spike_times[25]})\n",
    "\n",
    "# file_path = 'data26.mat'\n",
    "# scipy.io.savemat(file_path, {'data26': spike_times[26]})\n",
    "\n",
    "# file_path = 'data27.mat'\n",
    "# scipy.io.savemat(file_path, {'data27': spike_times[27]})\n",
    "\n",
    "# file_path = 'data28.mat'\n",
    "# scipy.io.savemat(file_path, {'data28': spike_times[28]})\n",
    "\n",
    "# file_path = 'data29.mat'\n",
    "# scipy.io.savemat(file_path, {'data29': spike_times[29]})\n",
    "\n",
    "# file_path = 'data30.mat'\n",
    "# scipy.io.savemat(file_path, {'data30': spike_times[30]})\n",
    "\n",
    "# file_path = 'data31.mat'\n",
    "# scipy.io.savemat(file_path, {'data31': spike_times[31]})\n",
    "\n",
    "# file_path = 'data32.mat'\n",
    "# scipy.io.savemat(file_path, {'data32': spike_times[32]})\n",
    "\n",
    "# file_path = 'data33.mat'\n",
    "# scipy.io.savemat(file_path, {'data33': spike_times[33]})\n",
    "\n",
    "# file_path = 'data34.mat'\n",
    "# scipy.io.savemat(file_path, {'data34': spike_times[34]})\n",
    "\n",
    "# file_path = 'data35.mat'\n",
    "# scipy.io.savemat(file_path, {'data35': spike_times[35]})\n",
    "\n",
    "# file_path = 'data36.mat'\n",
    "# scipy.io.savemat(file_path, {'data36': spike_times[36]})\n",
    "\n",
    "# file_path = 'data37.mat'\n",
    "# scipy.io.savemat(file_path, {'data37': spike_times[37]})\n",
    "\n",
    "# file_path = 'data38.mat'\n",
    "# scipy.io.savemat(file_path, {'data38': spike_times[38]})\n",
    "\n",
    "# file_path = 'data39.mat'\n",
    "# scipy.io.savemat(file_path, {'data39': spike_times[39]})\n",
    "\n",
    "# file_path = 'data40.mat'\n",
    "# scipy.io.savemat(file_path, {'data40': spike_times[40]})\n",
    "\n",
    "# file_path = 'data41.mat'\n",
    "# scipy.io.savemat(file_path, {'data41': spike_times[41]})\n",
    "\n",
    "# file_path = 'data42.mat'\n",
    "# scipy.io.savemat(file_path, {'data42': spike_times[42]})\n",
    "\n",
    "# file_path = 'data43.mat'\n",
    "# scipy.io.savemat(file_path, {'data43': spike_times[43]})\n",
    "\n",
    "# file_path = 'data44.mat'\n",
    "# scipy.io.savemat(file_path, {'data44': spike_times[44]})\n",
    "\n",
    "# file_path = 'data45.mat'\n",
    "# scipy.io.savemat(file_path, {'data45': spike_times[45]})\n",
    "\n",
    "# file_path = 'data46.mat'\n",
    "# scipy.io.savemat(file_path, {'data46': spike_times[46]})\n",
    "\n",
    "# file_path = 'data47.mat'\n",
    "# scipy.io.savemat(file_path, {'data47': spike_times[47]})\n",
    "\n",
    "# file_path = 'data48.mat'\n",
    "# scipy.io.savemat(file_path, {'data48': spike_times[48]})\n",
    "\n",
    "# file_path = 'data49.mat'\n",
    "# scipy.io.savemat(file_path, {'data49': spike_times[49]})\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d353eff4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}