{ "cells": [ { "cell_type": "code", "execution_count": null, "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\": 18 * 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": null, "id": "c82769f2", "metadata": { "scrolled": false }, "outputs": [], "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=50, # 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 = 200 * 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": null, "id": "7f22e21e", "metadata": {}, "outputs": [], "source": [ "#print(spike_times)" ] }, { "cell_type": "code", "execution_count": null, "id": "20e3191c", "metadata": {}, "outputs": [], "source": [ "import scipy.io\n", "import numpy as np\n", "\n", "\n", "file_path = 'data0.mat'\n", "scipy.io.savemat(file_path, {'data0': 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 }