-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdbs.py
193 lines (159 loc) · 4.88 KB
/
dbs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import numpy as np
from swift import aswift
class DBS(object):
def __init__(self, dt=1e-3, stim_amp=1, width=60, tstart=0):
'''
Initialize a DBS object
Parameters
----------
| stim_amp : float
| Stimulus puse amplitude (mA)
| width : float
| Stimulus puse width (us)
'''
self._dt = dt
self._stim_amp = stim_amp
self._width = width
self._tstart = tstart
self._calc_charge()
def _calc_charge(self):
self._charge = max(0, self.stim_amp * self.width * 1e-6)
# Setters/Getters
#----------------
@property
def stim_amp(self):
return self._stim_amp
@stim_amp.setter
def stim_amp(self,x):
self._stim_amp = x
self._calc_charge()
@property
def width(self):
return self._width
@width.setter
def width(self,x):
self._width = x
self._calc_charge()
# Getters only
#-------------
@property
def dt(self):
return self._dt
@property
def tstart(self):
return self._tstart
@property
def charge(self):
return self._charge #Charge in mC
class cDBS(DBS):
def __init__(self, f, dt=1e-3, stim_amp=1, width=60, tstart=0):
'''
Continuous DBS object.
Parameters
----------
| dt : float (s)
| integration timestep
| f : float (Hz)
| stimulation frequency
| stim_amp : float (mA)
| stimulus pulse ampltiude
| width : float (us)
| stimulus pulse width
| tstart : float (s)
| stimulation start time
'''
super(cDBS,self).__init__(dt,stim_amp,width,tstart)
self._f = f
self._steps_per_pulse = int(round(1./self._f/self._dt))
self._pulse_counter = self._steps_per_pulse
self._n_start = self.tstart / self.dt
self._i = 0
def advance(self):
stim = False
if self._i < self._n_start:
self._i += 1
else:
if self._pulse_counter >= self._steps_per_pulse:
self._pulse_counter = 0
stim = True
self._pulse_counter += 1
return stim * self.charge
@property
def f(self):
return self._f
class pDBS(DBS):
def __init__(self, f, tau_s, tau_f, phase_thr=0, power_thr=-np.inf, ref_period=0.3, dt=1e-3, stim_amp=1, width=60, tstart=0):
super(pDBS,self).__init__(dt,stim_amp,width,tstart)
self._f = f
self._tau_s = tau_s
self._tau_f = tau_f
self._phase_thr = phase_thr
self._power_thr = power_thr
self._ref_period = ref_period
self._aswift = aswift(tau_s = self.tau_s,
tau_f = self.tau_f,
f = self.f,
fs = 1./self.dt)
self._X = 0
self._amp = 0
self._phase = 0
self._shift_phase = 0
self._last_shift_phase = np.inf
self._update()
def _update(self):
self._n_ref = 1/(self.f*self.dt) * self.ref_period
self._i_ref = self._n_ref
def advance(self,x):
self._X = self.aswift.slide(x)
self._amp,self._phase = np.abs(self._X), np.angle(self._X)
self._amp /= (self.tau_s - self.tau_f) / self.dt
with np.errstate(divide='ignore'):
self._amp = 10*np.log10(self._amp**2)
self._last_shift_phase = self._shift_phase
self._shift_phase = (self._phase - self._phase_thr + np.pi) % (2*np.pi) - np.pi
stim=False
if self._last_shift_phase < 0 <= self._shift_phase:
#if self._last_phase - self.phase_thr < 0 <= self._phase - self.phase_thr:
if self._i_ref >= self._n_ref:
if self._amp >= self._power_thr:
stim=True
self._i_ref = 0
self._i_ref += 1
return self.charge * stim
# Mutable Properties
#-------------------
@property
def phase_thr(self):
return self._phase_thr
@phase_thr.setter
def phase_thr(self,x):
self._phase_thr = x
@property
def power_thr(self):
return self._power_thr
@power_thr.setter
def power_thr(self,x):
self._power_thr = x
# Immutable Properties
#---------------------
@property
def f(self):
return self._f
@property
def tau_s(self):
return self._tau_s
@property
def tau_f(self):
return self._tau_f
@property
def aswift(self):
return self._aswift
@property
def amp(self):
return self._amp
@property
def ref_period(self):
return self._ref_period
@property
def phase(self):
return self._phase