summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMario Mulansky <mario.mulansky@gmx.net>2014-10-12 18:49:37 +0200
committerMario Mulansky <mario.mulansky@gmx.net>2014-10-12 18:49:37 +0200
commitadc1e91c89aeecf3ee1743d3595b282061a22573 (patch)
tree0283d15e9e06ffbba19ceb63b0fa0650bcd57194
parent94408401e31830e7db7902afdeef714cb981bce2 (diff)
added sort to load function, renamed test data file
-rwxr-xr-xexamples/PySpike_testdata.txt6
-rw-r--r--pyspike/spikes.py15
-rwxr-xr-xtest/PySpike_testdata.txt (renamed from test/SPIKY_testdata.txt)0
-rw-r--r--test/test_spikes.py8
4 files changed, 17 insertions, 12 deletions
diff --git a/examples/PySpike_testdata.txt b/examples/PySpike_testdata.txt
index c8bea67..41b2362 100755
--- a/examples/PySpike_testdata.txt
+++ b/examples/PySpike_testdata.txt
@@ -1,10 +1,10 @@
+# PySpike exemplary spike trains
+# lines starting with # are ignored, just like empty lines
+
64.886 305.81 696 937.77 1059.7 1322.2 1576.1 1808.1 2121.5 2381.1 2728.6 2966.9 3223.7 3473.7 3644.3 3936.3
65.553 307.49 696.63 948.66 1070.4 1312.2 1712.7 1934.3 2117.6 2356.9 2727.3 2980.6 3226.9 3475.7 3726.4 3944
-# test comment
69.064 319.1 688.32 947.85 1071.8 1300.8 1697.2 1930.6 2139.4 2354.2 2723.7 2963.6 3221.3 3470.1
59.955 313.83 692.23 955.95 1070.4 1319.6 1681.9 1963.5 2151.4 2373.8 2729.4 2971.2 3220.2 3475.5 3632.3 3788.9
-# empty line
-
59.977 306.84 686.09 935.08 1059.9 1325.9 1543.4 1821.9 2150.2 2390.4 2724.5 2969.6 3222.5 3471.5 3576 3913.9
66.415 313.41 688.83 931.43 1051.8 1304.6 1555.6 1820.2 2150.5 2383.1 2723.4 2947.7 3196.6 3443.5 3575 3804.9
66.449 311.02 689.26 947.12 1058.9 1286.6 1708.2 1957.3 2124.8 2375.7 2709.4 2977.6 3191.1 3449.6 3590.4 3831.2
diff --git a/pyspike/spikes.py b/pyspike/spikes.py
index 9375e30..6ea94de 100644
--- a/pyspike/spikes.py
+++ b/pyspike/spikes.py
@@ -46,22 +46,26 @@ def add_auxiliary_spikes(spike_train, time_interval):
############################################################
# spike_train_from_string
############################################################
-def spike_train_from_string(s, sep=' '):
+def spike_train_from_string(s, sep=' ', sort=True):
""" Converts a string of times into an array of spike times.
Args:
- s: the string with (ordered) spike times
- - sep: The separator between the time numbers.
+ - sep: The separator between the time numbers, default=' '.
+ - sort: If True, the spike times are order via `np.sort`, default=True.
Returns:
- array of spike times
"""
- return np.fromstring(s, sep=sep)
+ if sort:
+ return np.sort(np.fromstring(s, sep=sep))
+ else:
+ return np.fromstring(s, sep=sep)
############################################################
# load_spike_trains_txt
############################################################
def load_spike_trains_from_txt(file_name, time_interval=None,
- separator=' ', comment='#'):
+ separator=' ', comment='#', sort=True):
""" Loads a number of spike trains from a text file. Each line of the text
file should contain one spike train as a sequence of spike times separated
by `separator`. Empty lines as well as lines starting with `comment` are
@@ -78,13 +82,14 @@ def load_spike_trains_from_txt(file_name, time_interval=None,
added to the spike train at the beginning and end of this interval.
- separator: The character used to seprate the values in the text file.
- comment: Lines starting with this character are ignored.
+ - sort: If true, the spike times are order via `np.sort`, default=True.
"""
spike_trains = []
spike_file = open(file_name, 'r')
for line in spike_file:
if len(line) > 1 and not line.startswith(comment):
# use only the lines with actual data and not commented
- spike_train = spike_train_from_string(line)
+ spike_train = spike_train_from_string(line, separator, sort)
if not time_interval == None: # add auxiliary spikes if times given
spike_train = add_auxiliary_spikes(spike_train, time_interval)
spike_trains.append(spike_train)
diff --git a/test/SPIKY_testdata.txt b/test/PySpike_testdata.txt
index c8bea67..c8bea67 100755
--- a/test/SPIKY_testdata.txt
+++ b/test/PySpike_testdata.txt
diff --git a/test/test_spikes.py b/test/test_spikes.py
index 456ed62..e008207 100644
--- a/test/test_spikes.py
+++ b/test/test_spikes.py
@@ -23,7 +23,7 @@ def test_auxiliary_spikes():
def test_load_from_txt():
- spike_trains = spk.load_spike_trains_from_txt("SPIKY_testdata.txt",
+ spike_trains = spk.load_spike_trains_from_txt("PySpike_testdata.txt",
time_interval=(0,4000))
assert len(spike_trains) == 40
@@ -39,7 +39,7 @@ def test_load_from_txt():
assert spike_train[-1] == 4000
# load without adding auxiliary spikes
- spike_trains2 = spk.load_spike_trains_from_txt("SPIKY_testdata.txt",
+ spike_trains2 = spk.load_spike_trains_from_txt("PySpike_testdata.txt",
time_interval=None)
assert len(spike_trains2) == 40
# check auxiliary spikes
@@ -59,12 +59,12 @@ def check_merged_spikes( merged_spikes, spike_trains ):
# change to something impossible so we dont find this event again
all_spikes[i] = -1.0
indices[i] = True
- assert( indices.all() )
+ assert indices.all()
def test_merge_spike_trains():
# first load the data
- spike_trains = spk.load_spike_trains_from_txt("SPIKY_testdata.txt",
+ spike_trains = spk.load_spike_trains_from_txt("PySpike_testdata.txt",
time_interval=(0,4000))
spikes = spk.merge_spike_trains([spike_trains[0], spike_trains[1]])