diff options
author | Mario Mulansky <mario.mulansky@gmx.net> | 2016-03-24 15:54:48 +0100 |
---|---|---|
committer | Mario Mulansky <mario.mulansky@gmx.net> | 2016-03-24 15:54:48 +0100 |
commit | 0d8af2c97d766a4fa514f0232189bc17c31c67a0 (patch) | |
tree | 73ca3098f3e8e5cfe958309994db0507dd2f1335 /pyspike/spikes.py | |
parent | b09561705ab9c67c93a384248f7c3bc9ad5bdd32 (diff) |
+function for saving spike trains to txt files
save_spike_trains_to_txt allows to save spike train data into txt files which
can then be loaded via load_spike_trains_from_txt again.
Diffstat (limited to 'pyspike/spikes.py')
-rw-r--r-- | pyspike/spikes.py | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/pyspike/spikes.py b/pyspike/spikes.py index b18d7eb..966ad69 100644 --- a/pyspike/spikes.py +++ b/pyspike/spikes.py @@ -2,6 +2,7 @@ # Copyright 2014, Mario Mulansky <mario.mulansky@gmx.net> # Distributed under the BSD License + import numpy as np from pyspike import SpikeTrain @@ -25,7 +26,7 @@ def spike_train_from_string(s, edges, sep=' ', is_sorted=False): ############################################################ -# load_spike_trains_txt +# load_spike_trains_from_txt ############################################################ def load_spike_trains_from_txt(file_name, edges, separator=' ', comment='#', is_sorted=False, @@ -47,17 +48,37 @@ def load_spike_trains_from_txt(file_name, edges, :returns: list of :class:`.SpikeTrain` """ 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, edges, - separator, is_sorted) - spike_trains.append(spike_train) + with open(file_name, 'r') as spike_file: + 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, edges, + separator, is_sorted) + spike_trains.append(spike_train) return spike_trains ############################################################ +# save_spike_trains_to_txt +############################################################ +def save_spike_trains_to_txt(spike_trains, file_name, + separator=' ', precision=8): + """ Saves the given spike trains into a file with the given file name. + Each spike train will be stored in one line in the text file with the times + separated by `separator`. + + :param spike_trains: List of :class:`.SpikeTrain` objects + :param file_name: The name of the text file. + """ + # format string to print the spike times with given precision + format_str = "{:0.%de}" % precision + with open(file_name, 'w') as spike_file: + for st in spike_trains: + s = separator.join(map(format_str.format, st.spikes)) + spike_file.write(s+'\n') + + +############################################################ # merge_spike_trains ############################################################ def merge_spike_trains(spike_trains): |