diff options
author | Mario Mulansky <mario.mulansky@gmx.net> | 2014-09-15 17:31:55 +0200 |
---|---|---|
committer | Mario Mulansky <mario.mulansky@gmx.net> | 2014-09-15 17:31:55 +0200 |
commit | 5ea0fc218bb3bb30b1c40dd20e2e35a8bd11151c (patch) | |
tree | e4e01ee2d7f8698b4dcc2b9d6041d37963474e41 | |
parent | f9529c78538882879a07cb67e342eade8d2153ab (diff) |
+merge_spike_trains
-rw-r--r-- | examples/test_merge.py | 23 | ||||
-rw-r--r-- | pyspike/__init__.py | 2 | ||||
-rw-r--r-- | pyspike/distances.py | 10 | ||||
-rw-r--r-- | pyspike/spikes.py | 26 |
4 files changed, 49 insertions, 12 deletions
diff --git a/examples/test_merge.py b/examples/test_merge.py new file mode 100644 index 0000000..1186062 --- /dev/null +++ b/examples/test_merge.py @@ -0,0 +1,23 @@ +# compute the isi distance of some test data +from __future__ import print_function + +import numpy as np +import matplotlib.pyplot as plt + +import pyspike as spk + +# first load the data +spike_trains = [] +spike_file = open("SPIKY_testdata.txt", 'r') +for line in spike_file: + spike_trains.append(spk.spike_train_from_string(line)) + +spikes = spk.merge_spike_trains([spike_trains[0], spike_trains[1]]) + +print(spikes) + +plt.plot(spike_trains[0], np.ones_like(spike_trains[0]), 'o') +plt.plot(spike_trains[1], np.ones_like(spike_trains[1]), 'x') +plt.plot(spikes, 2*np.ones_like(spikes), 'o') + +plt.show() diff --git a/pyspike/__init__.py b/pyspike/__init__.py index 6651eb5..6895bd8 100644 --- a/pyspike/__init__.py +++ b/pyspike/__init__.py @@ -2,4 +2,4 @@ __all__ = ["function", "distances", "spikes"] from function import PieceWiseConstFunc from distances import isi_distance -from spikes import spike_train_from_string +from spikes import spike_train_from_string, merge_spike_trains diff --git a/pyspike/distances.py b/pyspike/distances.py index d9790dc..7044a52 100644 --- a/pyspike/distances.py +++ b/pyspike/distances.py @@ -9,16 +9,6 @@ import numpy as np from pyspike import PieceWiseConstFunc -def spike_train_from_string(s, sep=' '): - """ Converts a string of times into a SpikeTrain object. - Params: - - s: the string with (ordered) spike times - - sep: The separator between the time numbers. - Returns: - - array of spike times - """ - return np.fromstring(s, sep=sep) - def isi_distance(spikes1, spikes2, T_end, T_start=0.0): """ Computes the instantaneous isi-distance S_isi (t) of the two given spike trains. diff --git a/pyspike/spikes.py b/pyspike/spikes.py index 42b6501..66ef554 100644 --- a/pyspike/spikes.py +++ b/pyspike/spikes.py @@ -8,7 +8,7 @@ Copyright 2014, Mario Mulansky <mario.mulansky@gmx.net> import numpy as np def spike_train_from_string(s, sep=' '): - """ Converts a string of times into a SpikeTrain object. + """ Converts a string of times into an array of spike times. Params: - s: the string with (ordered) spike times - sep: The separator between the time numbers. @@ -16,3 +16,27 @@ def spike_train_from_string(s, sep=' '): - array of spike times """ return np.fromstring(s, sep=sep) + + +def merge_spike_trains( spike_trains ): + """ Merges a number of spike trains into a single spike train. + Params: + - spike_trains: list of arrays of spike times + Returns: + - array with the merged spike times + """ + # get the lengths of the spike trains + lens = np.array([len(st) for st in spike_trains]) + merged_spikes = np.empty(np.sum(lens)) + index = 0 + indices = np.zeros_like(lens) + vals = [spike_trains[i][indices[i]] for i in xrange(len(indices))] + while len(indices) > 0: + i = np.argmin(vals) + merged_spikes[index] = vals[i] + index += 1 + indices[i] += 1 + if indices[i] >= lens[i]: + indices = np.delete(indices, i) + vals = [spike_trains[i][indices[i]] for i in xrange(len(indices))] + return merged_spikes |