Debian Codegraph @

docs/source/examples/lif.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 Copyright © 2014 German Neuroinformatics Node (G-Node)
4
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted under the terms of the BSD License. See
9 LICENSE file in the root of the Project.
10
11 Author: Jan Grewe <[email protected]>
12 """
13 import numpy as np
14
15
16 class LIF:
17
18 def __init__(self, stepsize=0.0001, offset=1.6, tau_m=0.025, tau_a=0.02, da=0.0, D=3.5):
19 self.stepsize = stepsize # simulation stepsize [s]
20 self.offset = offset # offset curent [nA]
21 self.tau_m = tau_m # membrane time_constant [s]
22 self.tau_a = tau_a # adaptation time_constant [s]
23 self.da = da # increment in adaptation current [nA]
24 self.D = D # noise intensity
25 self.v_threshold = 1.0 # spiking threshold
26 self.v_reset = 0.0 # reset voltage after spiking
27 self.i_a = 0.0 # current adaptation current
28 self.v = self.v_reset # current membrane voltage
29 self.t = 0.0 # current time [s]
30 self.membrane_voltage = []
31 self.spike_times = []
32
33 def _reset(self):
34 self.i_a = 0.0
35 self.v = self.v_reset
36 self.t = 0.0
37 self.membrane_voltage = []
38 self.spike_times = []
39
40 def _lif(self, stimulus, noise):
41 """
42 euler solution of the membrane equation with adaptation current and noise
43 """
44 self.i_a -= self.i_a - self.stepsize/self.tau_a * (self.i_a)
45 self.v += self.stepsize * ( -self.v + stimulus + noise + self.offset - self.i_a)/self.tau_m;
46 self.membrane_voltage.append(self.v)
47
48 def _next(self, stimulus):
49 """
50 working horse which delegates to the euler and gets the spike times
51 """
52 noise = self.D * (float(np.random.randn() % 10000) - 5000.0)/10000
53 self._lif(stimulus, noise)
54 self.t += self.stepsize
55 if self.v > self.v_threshold and len(self.membrane_voltage) > 1:
56 self.v = self.v_reset
57 self.membrane_voltage[len(self.membrane_voltage)-1] = 2.0
58 self.spike_times.append(self.t)
59 self.i_a += self.da
60
61 def run_const_stim(self, steps, stimulus):
62 """
63 lif simulation with constant stimulus.
64 """
65 self._reset()
66 for i in range(steps):
67 self._next(stimulus);
68 time = np.arange(len(self.membrane_voltage))*self.stepsize
69 return time, np.array(self.membrane_voltage), np.array(self.spike_times)
70
71 def run_stimulus(self, stimulus):
72 """
73 lif simulation with a predefined stimulus trace.
74 """
75 self._reset()
76 for s in stimulus:
77 self._next(s);
78 time = np.arange(len(self.membrane_voltage))*self.stepsize
79 return time, np.array(self.membrane_voltage), np.array(self.spike_times)
80
81 def __str__(self):
82 out = '\n'.join(["stepsize: \t" + str(self.stepsize),
83 "offset:\t\t" + str(self.offset),
84 "tau_m:\t\t" + str(self.tau_m),
85 "tau_a:\t\t" + str(self.tau_a),
86 "da:\t\t" + str(self.da),
87 "D:\t\t" + str(self.D),
88 "v_threshold:\t" + str(self.v_threshold),
89 "v_reset:\t" + str(self.v_reset)])
90 return out
91
92 def __repr__(self):
93 return self.__str__()