| 1 |
""" python_backend.py |
| 2 |
|
| 3 |
Collection of python functions that can be used instead of the cython |
| 4 |
implementation. |
| 5 |
|
| 6 |
Copyright 2014-2015, Mario Mulansky <[email protected]> |
| 7 |
|
| 8 |
Distributed under the BSD License |
| 9 |
|
| 10 |
""" |
| 11 |
import numpy as np |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
def isi_distance_python(s1, s2, t_start, t_end, MRTS=0.): |
| 17 |
""" Plain Python implementation of the isi distance. |
| 18 |
Out: spike_events - times from s1 and s2 merged, |
| 19 |
except beginning and end reflects t_start and t_end |
| 20 |
isi_values - ISI distance between consecutive elements of spike_events |
| 21 |
""" |
| 22 |
N1 = len(s1) |
| 23 |
N2 = len(s2) |
| 24 |
|
| 25 |
|
| 26 |
spike_events = np.empty(N1+N2+2) |
| 27 |
spike_events[0] = t_start |
| 28 |
|
| 29 |
isi_values = np.empty(len(spike_events) - 1) |
| 30 |
if s1[0] > t_start: |
| 31 |
|
| 32 |
nu1 = max(s1[0] - t_start, s1[1] - s1[0]) if N1 > 1 else s1[0]-t_start |
| 33 |
index1 = -1 |
| 34 |
else: |
| 35 |
nu1 = s1[1] - s1[0] if N1 > 1 else t_end-s1[0] |
| 36 |
index1 = 0 |
| 37 |
if s2[0] > t_start: |
| 38 |
|
| 39 |
nu2 = max(s2[0] - t_start, s2[1] - s2[0]) if N2 > 1 else s2[0]-t_start |
| 40 |
index2 = -1 |
| 41 |
else: |
| 42 |
nu2 = s2[1] - s2[0] if N2 > 1 else t_end-s2[0] |
| 43 |
index2 = 0 |
| 44 |
|
| 45 |
isi_values[0] = abs(nu1 - nu2) / max([nu1, nu2, MRTS]) |
| 46 |
index = 1 |
| 47 |
while index1+index2 < N1+N2-2: |
| 48 |
|
| 49 |
if (index1 < N1-1) and (index2 == N2-1 or s1[index1+1] < s2[index2+1]): |
| 50 |
index1 += 1 |
| 51 |
spike_events[index] = s1[index1] |
| 52 |
if index1 < N1-1: |
| 53 |
nu1 = s1[index1+1]-s1[index1] |
| 54 |
else: |
| 55 |
|
| 56 |
nu1 = max(t_end-s1[N1-1], s1[N1-1]-s1[N1-2]) if N1 > 1 \ |
| 57 |
else t_end-s1[N1-1] |
| 58 |
|
| 59 |
elif (index2 < N2-1) and (index1 == N1-1 or |
| 60 |
s1[index1+1] > s2[index2+1]): |
| 61 |
index2 += 1 |
| 62 |
spike_events[index] = s2[index2] |
| 63 |
if index2 < N2-1: |
| 64 |
nu2 = s2[index2+1]-s2[index2] |
| 65 |
else: |
| 66 |
|
| 67 |
nu2 = max(t_end-s2[N2-1], s2[N2-1]-s2[N2-2]) if N2 > 1 \ |
| 68 |
else t_end-s2[N2-1] |
| 69 |
|
| 70 |
else: |
| 71 |
index1 += 1 |
| 72 |
index2 += 1 |
| 73 |
spike_events[index] = s1[index1] |
| 74 |
if index1 < N1-1: |
| 75 |
nu1 = s1[index1+1]-s1[index1] |
| 76 |
else: |
| 77 |
|
| 78 |
nu1 = max(t_end-s1[N1-1], s1[N1-1]-s1[N1-2]) if N1 > 1 \ |
| 79 |
else t_end-s1[N1-1] |
| 80 |
if index2 < N2-1: |
| 81 |
nu2 = s2[index2+1]-s2[index2] |
| 82 |
else: |
| 83 |
|
| 84 |
nu2 = max(t_end-s2[N2-1], s2[N2-1]-s2[N2-2]) if N2 > 1 \ |
| 85 |
else t_end-s2[N2-1] |
| 86 |
|
| 87 |
isi_values[index] = abs(nu1 - nu2) / \ |
| 88 |
max([nu1, nu2, MRTS]) |
| 89 |
index += 1 |
| 90 |
|
| 91 |
if spike_events[index-1] == t_end: |
| 92 |
index -= 1 |
| 93 |
else: |
| 94 |
spike_events[index] = t_end |
| 95 |
|
| 96 |
|
| 97 |
return spike_events[:index + 1], isi_values[:index] |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
def get_min_dist(spike_time, spike_train, start_index, t_start, t_end): |
| 104 |
""" Returns the minimal distance |spike_time - spike_train[i]| |
| 105 |
with i>=start_index. |
| 106 |
""" |
| 107 |
d = abs(spike_time - t_start) |
| 108 |
if start_index < 0: |
| 109 |
start_index = 0 |
| 110 |
while start_index < len(spike_train): |
| 111 |
d_temp = abs(spike_time - spike_train[start_index]) |
| 112 |
if d_temp > d: |
| 113 |
return d |
| 114 |
else: |
| 115 |
d = d_temp |
| 116 |
start_index += 1 |
| 117 |
|
| 118 |
d_temp = abs(t_end - spike_time) |
| 119 |
if d_temp > d: |
| 120 |
return d |
| 121 |
else: |
| 122 |
return d_temp |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
def dist_at_t(isi1, isi2, s1, s2, MRTS, RI): |
| 128 |
""" Compute instantaneous Spike Distance |
| 129 |
In: isi1, isi2 - spike time differences around current times in each trains |
| 130 |
s1, s2 - weighted spike time differences between trains |
| 131 |
Out: the Spike Distance |
| 132 |
""" |
| 133 |
meanISI = .5*(isi1+isi2) |
| 134 |
limitedISI = max(MRTS, meanISI) |
| 135 |
if RI: |
| 136 |
return .5*(s1+s2)/limitedISI |
| 137 |
else: |
| 138 |
return .5*(s1*isi2 + s2*isi1)/(meanISI*limitedISI) |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
def spike_distance_python(spikes1, spikes2, t_start, t_end, MRTS=0., RI=False): |
| 144 |
""" Computes the instantaneous spike-distance S_spike (t) of the two given |
| 145 |
spike trains. The spike trains are expected to have auxiliary spikes at the |
| 146 |
beginning and end of the interval. Use the function add_auxiliary_spikes to |
| 147 |
add those spikes to the spike train. |
| 148 |
Args: |
| 149 |
- spikes1, spikes2: ordered arrays of spike times with auxiliary spikes. |
| 150 |
- t_start, t_end: edges of the spike train |
| 151 |
Returns: |
| 152 |
- spike_events - merged times from spikes1, spikes2 (with edge corrections) |
| 153 |
- y_starts, y_ends |
| 154 |
In the interval (spike_events[i], spike_events[i+1]) |
| 155 |
the SPIKE-sync value goes from y_starts[i] to y_ends[i], linearly |
| 156 |
""" |
| 157 |
|
| 158 |
|
| 159 |
t1 = spikes1 |
| 160 |
t2 = spikes2 |
| 161 |
|
| 162 |
N1 = len(t1) |
| 163 |
N2 = len(t2) |
| 164 |
|
| 165 |
spike_events = np.empty(N1+N2+2) |
| 166 |
|
| 167 |
y_starts = np.empty(len(spike_events)-1) |
| 168 |
y_ends = np.empty(len(spike_events)-1) |
| 169 |
|
| 170 |
t_aux1 = np.zeros(2) |
| 171 |
t_aux2 = np.zeros(2) |
| 172 |
t_aux1[0] = min(t_start, t1[0]-(t1[1]-t1[0])) if N1 > 1 else t_start |
| 173 |
t_aux1[1] = max(t_end, t1[N1-1]+(t1[N1-1]-t1[N1-2])) if N1 > 1 else t_end |
| 174 |
t_aux2[0] = min(t_start, t2[0]-(t2[1]-t2[0])) if N2 > 1 else t_start |
| 175 |
t_aux2[1] = max(t_end, t2[N2-1]+(t2[N2-1]-t2[N2-2])) if N2 > 1 else t_end |
| 176 |
t_p1 = t_start if (t1[0] == t_start) else t_aux1[0] |
| 177 |
t_p2 = t_start if (t2[0] == t_start) else t_aux2[0] |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
spike_events[0] = t_start |
| 182 |
if t1[0] > t_start: |
| 183 |
t_f1 = t1[0] |
| 184 |
dt_f1 = get_min_dist(t_f1, t2, 0, t_aux2[0], t_aux2[1]) |
| 185 |
dt_p1 = dt_f1 |
| 186 |
isi1 = max(t_f1-t_start, t1[1]-t1[0]) if N1 > 1 else t_f1-t_start |
| 187 |
|
| 188 |
s1 = dt_p1 |
| 189 |
index1 = -1 |
| 190 |
else: |
| 191 |
|
| 192 |
t_f1 = t1[1] if N1 > 1 else t_end |
| 193 |
dt_p1 = get_min_dist(t_p1, t2, 0, t_aux2[0], t_aux2[1]) |
| 194 |
dt_f1 = get_min_dist(t_f1, t2, 0, t_aux2[0], t_aux2[1]) |
| 195 |
isi1 = t_f1-t1[0] |
| 196 |
s1 = dt_p1 |
| 197 |
index1 = 0 |
| 198 |
if t2[0] > t_start: |
| 199 |
|
| 200 |
t_f2 = t2[0] |
| 201 |
dt_f2 = get_min_dist(t_f2, t1, 0, t_aux1[0], t_aux1[1]) |
| 202 |
dt_p2 = dt_f2 |
| 203 |
isi2 = max(t_f2-t_start, t2[1]-t2[0]) if N2 > 1 else t_f2-t_start |
| 204 |
|
| 205 |
s2 = dt_p2 |
| 206 |
index2 = -1 |
| 207 |
else: |
| 208 |
t_f2 = t2[1] if N2 > 1 else t_end |
| 209 |
dt_p2 = get_min_dist(t_p2, t1, 0, t_aux1[0], t_aux1[1]) |
| 210 |
dt_f2 = get_min_dist(t_f2, t1, 0, t_aux1[0], t_aux1[1]) |
| 211 |
isi2 = t_f2-t2[0] |
| 212 |
s2 = dt_p2 |
| 213 |
index2 = 0 |
| 214 |
y_starts[0] = dist_at_t(isi1, isi2, s1, s2, MRTS, RI) |
| 215 |
index = 1 |
| 216 |
|
| 217 |
while index1+index2 < N1+N2-2: |
| 218 |
|
| 219 |
if (index1 < N1-1) and (t_f1 < t_f2 or index2 == N2-1): |
| 220 |
index1 += 1 |
| 221 |
|
| 222 |
s1 = dt_f1*(t_f1-t_p1) / isi1 |
| 223 |
|
| 224 |
dt_p1 = dt_f1 |
| 225 |
t_p1 = t_f1 |
| 226 |
|
| 227 |
if index1 < N1-1: |
| 228 |
t_f1 = t1[index1+1] |
| 229 |
else: |
| 230 |
t_f1 = t_aux1[1] |
| 231 |
spike_events[index] = t_p1 |
| 232 |
s2 = (dt_p2*(t_f2-t_p1) + dt_f2*(t_p1-t_p2)) / isi2 |
| 233 |
y_ends[index-1] = dist_at_t(isi1, isi2, s1, s2, MRTS, RI) |
| 234 |
|
| 235 |
if index1 < N1-1: |
| 236 |
dt_f1 = get_min_dist(t_f1, t2, index2, t_aux2[0], t_aux2[1]) |
| 237 |
isi1 = t_f1-t_p1 |
| 238 |
s1 = dt_p1 |
| 239 |
else: |
| 240 |
dt_f1 = dt_p1 |
| 241 |
isi1 = max(t_end-t1[N1-1], t1[N1-1]-t1[N1-2]) if N1 > 1 \ |
| 242 |
else t_end-t1[N1-1] |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
s1 = dt_p1 |
| 247 |
|
| 248 |
y_starts[index] = dist_at_t(isi1, isi2, s1, s2, MRTS, RI) |
| 249 |
elif (index2 < N2-1) and (t_f1 > t_f2 or index1 == N1-1): |
| 250 |
index2 += 1 |
| 251 |
|
| 252 |
s2 = dt_f2*(t_f2-t_p2) / isi2 |
| 253 |
|
| 254 |
dt_p2 = dt_f2 |
| 255 |
t_p2 = t_f2 |
| 256 |
|
| 257 |
if index2 < N2-1: |
| 258 |
t_f2 = t2[index2+1] |
| 259 |
else: |
| 260 |
t_f2 = t_aux2[1] |
| 261 |
spike_events[index] = t_p2 |
| 262 |
s1 = (dt_p1*(t_f1-t_p2) + dt_f1*(t_p2-t_p1)) / isi1 |
| 263 |
y_ends[index-1] = dist_at_t(isi1, isi2, s1, s2, MRTS, RI) |
| 264 |
|
| 265 |
if index2 < N2-1: |
| 266 |
dt_f2 = get_min_dist(t_f2, t1, index1, t_aux1[0], t_aux1[1]) |
| 267 |
isi2 = t_f2-t_p2 |
| 268 |
s2 = dt_p2 |
| 269 |
else: |
| 270 |
dt_f2 = dt_p2 |
| 271 |
isi2 = max(t_end-t2[N2-1], t2[N2-1]-t2[N2-2]) if N2 > 1 \ |
| 272 |
else t_end-t2[N2-1] |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
s2 = dt_p2 |
| 277 |
|
| 278 |
y_starts[index] = dist_at_t(isi1, isi2, s1, s2, MRTS, RI) |
| 279 |
else: |
| 280 |
index1 += 1 |
| 281 |
index2 += 1 |
| 282 |
t_p1 = t_f1 |
| 283 |
t_p2 = t_f2 |
| 284 |
dt_p1 = 0.0 |
| 285 |
dt_p2 = 0.0 |
| 286 |
spike_events[index] = t_f1 |
| 287 |
y_ends[index-1] = 0.0 |
| 288 |
y_starts[index] = 0.0 |
| 289 |
if index1 < N1-1: |
| 290 |
t_f1 = t1[index1+1] |
| 291 |
dt_f1 = get_min_dist(t_f1, t2, index2, t_aux2[0], t_aux2[1]) |
| 292 |
isi1 = t_f1 - t_p1 |
| 293 |
else: |
| 294 |
t_f1 = t_aux1[1] |
| 295 |
dt_f1 = dt_p1 |
| 296 |
isi1 = max(t_end-t1[N1-1], t1[N1-1]-t1[N1-2]) if N1 > 1 \ |
| 297 |
else t_end-t1[N1-1] |
| 298 |
if index2 < N2-1: |
| 299 |
t_f2 = t2[index2+1] |
| 300 |
dt_f2 = get_min_dist(t_f2, t1, index1, t_aux1[0], t_aux1[1]) |
| 301 |
isi2 = t_f2 - t_p2 |
| 302 |
else: |
| 303 |
t_f2 = t_aux2[1] |
| 304 |
dt_f2 = dt_p2 |
| 305 |
isi2 = max(t_end-t2[N2-1], t2[N2-1]-t2[N2-2]) if N2 > 1 \ |
| 306 |
else t_end-t2[N2-1] |
| 307 |
index += 1 |
| 308 |
|
| 309 |
|
| 310 |
if spike_events[index-1] == t_end: |
| 311 |
index -= 1 |
| 312 |
else: |
| 313 |
spike_events[index] = t_end |
| 314 |
s1 = dt_f1 |
| 315 |
s2 = dt_f2 |
| 316 |
y_ends[index-1] = dist_at_t(isi1, isi2, s1, s2, MRTS, RI) |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
return spike_events[:index+1], y_starts[:index], y_ends[:index] |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
def get_tau(spikes1, spikes2, i, j, max_tau, MRTS): |
| 325 |
""" Compute coincidence window |
| 326 |
In: spikes1, spikes2 - times of two spike trains |
| 327 |
i, j - indices into spikes1, spikes2 to compare |
| 328 |
max_tau - maximum size of MRTS |
| 329 |
MRTS - adaptation parameter |
| 330 |
out: combined coincidence window (Eq 19 in reference) |
| 331 |
""" |
| 332 |
|
| 333 |
mF1 = max_tau |
| 334 |
mP1 = max_tau |
| 335 |
mF2 = max_tau |
| 336 |
mP2 = max_tau |
| 337 |
|
| 338 |
if i < len(spikes1)-1 and i > -1: |
| 339 |
mF1 = spikes1[i+1]-spikes1[i] |
| 340 |
if j < len(spikes2)-1 and j > -1: |
| 341 |
mF2 = spikes2[j+1]-spikes2[j] |
| 342 |
if i > 0: |
| 343 |
mP1 = spikes1[i]-spikes1[i-1] |
| 344 |
if j > 0: |
| 345 |
mP2 = spikes2[j]-spikes2[j-1] |
| 346 |
|
| 347 |
mF1, mF2, mP1, mP2 = mF1/2., mF2/2., mP1/2., mP2/2. |
| 348 |
MRTS /= 4 |
| 349 |
|
| 350 |
def Interpolate(a, b, t): |
| 351 |
""" thresholded interpolation |
| 352 |
If t small, return min(a,b) |
| 353 |
if t big, return b |
| 354 |
in between, return t |
| 355 |
""" |
| 356 |
mab = min(a,b) |
| 357 |
if t<mab: return mab |
| 358 |
if t > b: return b |
| 359 |
return t |
| 360 |
|
| 361 |
if i<0 or j<0 or spikes1[i] <= spikes2[j]: |
| 362 |
s1F = Interpolate(mP1, mF1, MRTS) |
| 363 |
s2P = Interpolate(mF2, mP2, MRTS) |
| 364 |
return min(s1F, s2P) |
| 365 |
else: |
| 366 |
s1P = Interpolate(mF1, mP1, MRTS) |
| 367 |
s2F = Interpolate(mP2, mF2, MRTS) |
| 368 |
return min(s1P, s2F) |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
def coincidence_python(spikes1, spikes2, t_start, t_end, max_tau, MRTS=0.): |
| 377 |
""" python version of logic for bivariate SPIKE-Sync profile |
| 378 |
UNUSED - replaced by coincidence_single_python() |
| 379 |
""" |
| 380 |
true_max = t_end - t_start |
| 381 |
if max_tau > 0: |
| 382 |
true_max = min(true_max, 2*max_tau) |
| 383 |
|
| 384 |
N1 = len(spikes1) |
| 385 |
N2 = len(spikes2) |
| 386 |
i = -1 |
| 387 |
j = -1 |
| 388 |
n = 0 |
| 389 |
st = np.zeros(N1 + N2 + 2) |
| 390 |
c = np.zeros(N1 + N2 + 2) |
| 391 |
mp = np.ones(N1 + N2 + 2) |
| 392 |
|
| 393 |
while i + j < N1 + N2 - 2: |
| 394 |
if (i < N1-1) and (j == N2-1 or spikes1[i+1] < spikes2[j+1]): |
| 395 |
i += 1 |
| 396 |
n += 1 |
| 397 |
tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS) |
| 398 |
st[n] = spikes1[i] |
| 399 |
if j > -1 and spikes1[i]-spikes2[j] < tau: |
| 400 |
|
| 401 |
|
| 402 |
c[n] = 1 |
| 403 |
c[n-1] = 1 |
| 404 |
elif (j < N2-1) and (i == N1-1 or spikes1[i+1] > spikes2[j+1]): |
| 405 |
j += 1 |
| 406 |
n += 1 |
| 407 |
tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS) |
| 408 |
st[n] = spikes2[j] |
| 409 |
if i > -1 and spikes2[j]-spikes1[i] < tau: |
| 410 |
|
| 411 |
|
| 412 |
c[n] = 1 |
| 413 |
c[n-1] = 1 |
| 414 |
else: |
| 415 |
|
| 416 |
j += 1 |
| 417 |
i += 1 |
| 418 |
n += 1 |
| 419 |
|
| 420 |
st[n] = spikes1[i] |
| 421 |
c[n] = 2 |
| 422 |
mp[n] = 2 |
| 423 |
|
| 424 |
st = st[:n+2] |
| 425 |
c = c[:n+2] |
| 426 |
mp = mp[:n+2] |
| 427 |
|
| 428 |
st[0] = t_start |
| 429 |
st[len(st)-1] = t_end |
| 430 |
if N1 + N2 > 0: |
| 431 |
c[0] = c[1] |
| 432 |
c[len(c)-1] = c[len(c)-2] |
| 433 |
mp[0] = mp[1] |
| 434 |
mp[len(mp)-1] = mp[len(mp)-2] |
| 435 |
else: |
| 436 |
c[0] = 1 |
| 437 |
c[1] = 1 |
| 438 |
|
| 439 |
return st, c, mp |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
def coincidence_single_python(spikes1, spikes2, t_start, t_end, max_tau, MRTS=0.): |
| 446 |
""" python version of logic for bivariate SPIKE-Sync profile |
| 447 |
In: spikes1, spikes2 - lists of sorted spike times |
| 448 |
t_start, t_end - range of times to consider |
| 449 |
max_tau - max window coincidence length |
| 450 |
MRTS - Minimum Relvant Time Scale (or 0 if none) |
| 451 |
Out: st - spike times |
| 452 |
c - coincidences |
| 453 |
mp - multiplicity |
| 454 |
""" |
| 455 |
true_max = t_end - t_start |
| 456 |
if max_tau > 0: |
| 457 |
true_max = min(true_max, 2*max_tau) |
| 458 |
|
| 459 |
N1 = len(spikes1) |
| 460 |
N2 = len(spikes2) |
| 461 |
j = -1 |
| 462 |
c = np.zeros(N1) |
| 463 |
for i in range(N1): |
| 464 |
while j < N2-1 and spikes2[j+1] < spikes1[i]: |
| 465 |
|
| 466 |
|
| 467 |
j += 1 |
| 468 |
tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS) |
| 469 |
if j > -1 and abs(spikes1[i]-spikes2[j]) < tau: |
| 470 |
|
| 471 |
c[i] = 1 |
| 472 |
if j < N2-1 and (j < 0 or spikes2[j] < spikes1[i]): |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
j += 1 |
| 477 |
tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS) |
| 478 |
if abs(spikes2[j]-spikes1[i]) < tau: |
| 479 |
|
| 480 |
c[i] = 1 |
| 481 |
return c |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
def add_piece_wise_const_python(x1, y1, x2, y2): |
| 488 |
""" Add piecewise constant functions |
| 489 |
In: x1,y1 - first function [y(x) = y1(i) for x(i)<=x<x(i+1)] |
| 490 |
x2,y2 - second function |
| 491 |
Out: returns x,y of the sum |
| 492 |
""" |
| 493 |
x_new = np.empty(len(x1) + len(x2)) |
| 494 |
y_new = np.empty(len(x_new)-1) |
| 495 |
x_new[0] = x1[0] |
| 496 |
y_new[0] = y1[0] + y2[0] |
| 497 |
index1 = 0 |
| 498 |
index2 = 0 |
| 499 |
index = 0 |
| 500 |
while (index1+1 < len(y1)) and (index2+1 < len(y2)): |
| 501 |
index += 1 |
| 502 |
|
| 503 |
if x1[index1+1] < x2[index2+1]: |
| 504 |
index1 += 1 |
| 505 |
x_new[index] = x1[index1] |
| 506 |
elif x1[index1+1] > x2[index2+1]: |
| 507 |
index2 += 1 |
| 508 |
x_new[index] = x2[index2] |
| 509 |
else: |
| 510 |
index1 += 1 |
| 511 |
index2 += 1 |
| 512 |
x_new[index] = x1[index1] |
| 513 |
y_new[index] = y1[index1] + y2[index2] |
| 514 |
|
| 515 |
if index1+1 < len(y1): |
| 516 |
x_new[index+1:index+1+len(x1)-index1-1] = x1[index1+1:] |
| 517 |
y_new[index+1:index+1+len(y1)-index1-1] = y1[index1+1:] + y2[-1] |
| 518 |
index += len(x1)-index1-2 |
| 519 |
elif index2+1 < len(y2): |
| 520 |
x_new[index+1:index+1+len(x2)-index2-1] = x2[index2+1:] |
| 521 |
y_new[index+1:index+1+len(y2)-index2-1] = y2[index2+1:] + y1[-1] |
| 522 |
index += len(x2)-index2-2 |
| 523 |
else: |
| 524 |
|
| 525 |
x_new[index+1] = x1[-1] |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
return x_new[:index+2], y_new[:index+1] |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
def add_piece_wise_lin_python(x1, y11, y12, x2, y21, y22): |
| 537 |
""" Add piecewise constant functions |
| 538 |
In: x1,y11,y12 - first function |
| 539 |
x2,y21,y22 - second function |
| 540 |
Out: returns x,y1,y2 - the summed function |
| 541 |
""" |
| 542 |
x_new = np.empty(len(x1) + len(x2)) |
| 543 |
y1_new = np.empty(len(x_new)-1) |
| 544 |
y2_new = np.empty_like(y1_new) |
| 545 |
x_new[0] = x1[0] |
| 546 |
y1_new[0] = y11[0] + y21[0] |
| 547 |
index1 = 0 |
| 548 |
index2 = 0 |
| 549 |
index = 0 |
| 550 |
while (index1+1 < len(y11)) and (index2+1 < len(y21)): |
| 551 |
|
| 552 |
if x1[index1+1] < x2[index2+1]: |
| 553 |
|
| 554 |
|
| 555 |
y = y21[index2] + (y22[index2]-y21[index2]) * \ |
| 556 |
(x1[index1+1]-x2[index2]) / (x2[index2+1]-x2[index2]) |
| 557 |
y2_new[index] = y12[index1] + y |
| 558 |
index1 += 1 |
| 559 |
index += 1 |
| 560 |
x_new[index] = x1[index1] |
| 561 |
|
| 562 |
y1_new[index] = y11[index1] + y |
| 563 |
elif x1[index1+1] > x2[index2+1]: |
| 564 |
|
| 565 |
|
| 566 |
y = y11[index1] + (y12[index1]-y11[index1]) * \ |
| 567 |
(x2[index2+1]-x1[index1]) / \ |
| 568 |
(x1[index1+1]-x1[index1]) |
| 569 |
y2_new[index] = y22[index2] + y |
| 570 |
index2 += 1 |
| 571 |
index += 1 |
| 572 |
x_new[index] = x2[index2] |
| 573 |
|
| 574 |
y1_new[index] = y21[index2] + y |
| 575 |
else: |
| 576 |
y2_new[index] = y12[index1] + y22[index2] |
| 577 |
index1 += 1 |
| 578 |
index2 += 1 |
| 579 |
index += 1 |
| 580 |
x_new[index] = x1[index1] |
| 581 |
y1_new[index] = y11[index1] + y21[index2] |
| 582 |
|
| 583 |
if index1+1 < len(y11): |
| 584 |
|
| 585 |
y = y21[index2] + (y22[index2]-y21[index2]) * \ |
| 586 |
(x1[index1+1:-1]-x2[index2]) / (x2[index2+1]-x2[index2]) |
| 587 |
x_new[index+1:index+1+len(x1)-index1-1] = x1[index1+1:] |
| 588 |
y1_new[index+1:index+1+len(y11)-index1-1] = y11[index1+1:]+y |
| 589 |
y2_new[index:index+len(y12)-index1-1] = y12[index1:-1] + y |
| 590 |
index += len(x1)-index1-2 |
| 591 |
elif index2+1 < len(y21): |
| 592 |
|
| 593 |
y = y11[index1] + (y12[index1]-y11[index1]) * \ |
| 594 |
(x2[index2+1:-1]-x1[index1]) / \ |
| 595 |
(x1[index1+1]-x1[index1]) |
| 596 |
x_new[index+1:index+1+len(x2)-index2-1] = x2[index2+1:] |
| 597 |
y1_new[index+1:index+1+len(y21)-index2-1] = y21[index2+1:] + y |
| 598 |
y2_new[index:index+len(y22)-index2-1] = y22[index2:-1] + y |
| 599 |
index += len(x2)-index2-2 |
| 600 |
else: |
| 601 |
|
| 602 |
x_new[index+1] = x1[-1] |
| 603 |
|
| 604 |
y2_new[index] = y12[-1]+y22[-1] |
| 605 |
|
| 606 |
return x_new[:index+2], y1_new[:index+1], y2_new[:index+1] |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
def add_discrete_function_python(x1, y1, mp1, x2, y2, mp2): |
| 613 |
""" Add two functions defined on a finite point set |
| 614 |
In: x1,y1,mp1 - discrete function, with multiplicities |
| 615 |
x2,y2,mp2 - second function |
| 616 |
Out: x, y, mp - the sum |
| 617 |
Note: Depends on floating point ==, so might not |
| 618 |
return expected answer |
| 619 |
""" |
| 620 |
x_new = np.empty(len(x1) + len(x2)) |
| 621 |
y_new = np.empty_like(x_new) |
| 622 |
mp_new = np.empty_like(x_new) |
| 623 |
x_new[0] = x1[0] |
| 624 |
index1 = 0 |
| 625 |
index2 = 0 |
| 626 |
index = 0 |
| 627 |
N1 = len(x1)-1 |
| 628 |
N2 = len(x2)-1 |
| 629 |
while (index1+1 < N1) and (index2+1 < N2): |
| 630 |
if x1[index1+1] < x2[index2+1]: |
| 631 |
index1 += 1 |
| 632 |
index += 1 |
| 633 |
x_new[index] = x1[index1] |
| 634 |
y_new[index] = y1[index1] |
| 635 |
mp_new[index] = mp1[index1] |
| 636 |
elif x1[index1+1] > x2[index2+1]: |
| 637 |
index2 += 1 |
| 638 |
index += 1 |
| 639 |
x_new[index] = x2[index2] |
| 640 |
y_new[index] = y2[index2] |
| 641 |
mp_new[index] = mp2[index2] |
| 642 |
else: |
| 643 |
index1 += 1 |
| 644 |
index2 += 1 |
| 645 |
index += 1 |
| 646 |
x_new[index] = x1[index1] |
| 647 |
y_new[index] = y1[index1] + y2[index2] |
| 648 |
mp_new[index] = mp1[index1] + mp2[index2] |
| 649 |
|
| 650 |
if index1+1 < N1: |
| 651 |
x_new[index+1:index+1+N1-index1] = x1[index1+1:] |
| 652 |
y_new[index+1:index+1+N1-index1] = y1[index1+1:] |
| 653 |
mp_new[index+1:index+1+N1-index1] = mp1[index1+1:] |
| 654 |
index += N1-index1 |
| 655 |
elif index2+1 < N2: |
| 656 |
x_new[index+1:index+1+N2-index2] = x2[index2+1:] |
| 657 |
y_new[index+1:index+1+N2-index2] = y2[index2+1:] |
| 658 |
mp_new[index+1:index+1+N2-index2] = mp2[index2+1:] |
| 659 |
index += N2-index2 |
| 660 |
else: |
| 661 |
x_new[index+1] = x1[-1] |
| 662 |
y_new[index+1] = y1[-1] + y2[-1] |
| 663 |
mp_new[index+1] = mp1[-1] + mp2[-1] |
| 664 |
index += 1 |
| 665 |
|
| 666 |
y_new[0] = y_new[1] |
| 667 |
mp_new[0] = mp_new[1] |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
return x_new[:index+1], y_new[:index+1], mp_new[:index+1] |