From aeec6cfafed8df110e60743073cff6d778f65af0 Mon Sep 17 00:00:00 2001 From: Mario Mulansky Date: Mon, 29 Sep 2014 18:45:10 +0200 Subject: +cython version for isi and performance tests --- examples/perf_isi.py | 46 +++++++++++++++++++++++++++ examples/perf_spike.py | 39 +++++++++++++++++++++++ pyspike/cython_distance.pyx | 75 +++++++++++++++++++++++++++++++++++++++++++++ pyspike/distances.py | 17 ++++++++-- 4 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 examples/perf_isi.py create mode 100644 examples/perf_spike.py create mode 100644 pyspike/cython_distance.pyx diff --git a/examples/perf_isi.py b/examples/perf_isi.py new file mode 100644 index 0000000..8b44946 --- /dev/null +++ b/examples/perf_isi.py @@ -0,0 +1,46 @@ +# performance measure of the isi calculation + +from __future__ import print_function + +import numpy as np +import matplotlib.pyplot as plt +import time +from functools import partial + +import pyspike as spk +#import pyspike.distances # for the python functions + +def measure_perf(func, loops=10): + times = np.empty(loops) + for i in xrange(loops): + start = time.clock() + func() + times[i] = time.clock() - start + return np.min(times) + +print("# approximate number of spikes\tcython time [ms]\tpython time [ms]") + +# fix seed to get reproducible results +np.random.seed(1) + +# max times +Ns = np.arange(10000, 50001, 10000) +for N in Ns: + + # first generate some data + times = 2.0*np.random.random(1.1*N) + t1 = np.cumsum(times) + # only up to T + t1 = spk.add_auxiliary_spikes(t1[t1= N1: + break + spike_events[index] = s1[index1] + nu1 = s1[index1+1]-s1[index1] + elif s1[index1+1] > s2[index2+1]: + index2 += 1 + if index2 >= N2: + break + spike_events[index] = s2[index2] + nu2 = s2[index2+1]-s2[index2] + else: # s1[index1+1] == s2[index2+1] + index1 += 1 + index2 += 1 + if (index1 >= N1) or (index2 >= N2): + break + spike_events[index] = s1[index1] + nu1 = s1[index1+1]-s1[index1] + nu2 = s2[index2+1]-s2[index2] + # compute the corresponding isi-distance + isi_values[index] = (nu1 - nu2) / max(nu1, nu2) + index += 1 + # the last event is the interval end + spike_events[index] = s1[N1] + + return spike_events[:index+1], isi_values[:index] diff --git a/pyspike/distances.py b/pyspike/distances.py index 52c6640..76dcd83 100644 --- a/pyspike/distances.py +++ b/pyspike/distances.py @@ -53,10 +53,21 @@ def isi_distance(spikes1, spikes2): assert spikes1[-1]==spikes2[-1], \ "Given spike trains seems not to have auxiliary spikes!" - # shorter names - s1 = spikes1 - s2 = spikes2 + # compile and load cython implementation + import pyximport + pyximport.install(setup_args={'include_dirs': [np.get_include()]}) + from cython_distance import isi_distance_cython + times, values = isi_distance_cython(spikes1, spikes2) + return PieceWiseConstFunc(times, values) + + +############################################################ +# isi_distance_python +############################################################ +def isi_distance_python(s1, s2): + """ Plain Python implementation of the isi distance. + """ # compute the interspike interval nu1 = s1[1:]-s1[:-1] nu2 = s2[1:]-s2[:-1] -- cgit v1.2.3