asciispiketrainio.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """
  2. Classe for reading/writing SpikeTrains in a text file.
  3. It is the simple case where different spiketrains are written line by line.
  4. Supported : Read/Write
  5. Author: sgarcia
  6. """
  7. import os
  8. import numpy as np
  9. import quantities as pq
  10. from neo.io.baseio import BaseIO
  11. from neo.core import Segment, SpikeTrain
  12. class AsciiSpikeTrainIO(BaseIO):
  13. """
  14. Class for reading/writing SpikeTrains in a text file.
  15. Each Spiketrain is a line.
  16. Usage:
  17. >>> from neo import io
  18. >>> r = io.AsciiSpikeTrainIO( filename = 'File_ascii_spiketrain_1.txt')
  19. >>> seg = r.read_segment()
  20. >>> print seg.spiketrains # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
  21. [<SpikeTrain(array([ 3.89981604, 4.73258781, 0.608428 , 4.60246277, 1.23805797,
  22. ...
  23. """
  24. is_readable = True
  25. is_writable = True
  26. supported_objects = [Segment, SpikeTrain]
  27. readable_objects = [Segment]
  28. writeable_objects = [Segment]
  29. has_header = False
  30. is_streameable = False
  31. read_params = {
  32. Segment: [
  33. ('delimiter', {'value': '\t', 'possible': ['\t', ' ', ',', ';']}),
  34. ('t_start', {'value': 0., }),
  35. ]
  36. }
  37. write_params = {
  38. Segment: [
  39. ('delimiter', {'value': '\t', 'possible': ['\t', ' ', ',', ';']}),
  40. ]
  41. }
  42. name = None
  43. extensions = ['txt']
  44. mode = 'file'
  45. def __init__(self, filename=None):
  46. """
  47. This class read/write SpikeTrains in a text file.
  48. Each row is a spiketrain.
  49. **Arguments**
  50. filename : the filename to read/write
  51. """
  52. BaseIO.__init__(self)
  53. self.filename = filename
  54. def read_segment(self,
  55. lazy=False,
  56. delimiter='\t',
  57. t_start=0. * pq.s,
  58. unit=pq.s,
  59. ):
  60. """
  61. Arguments:
  62. delimiter : columns delimiter in file '\t' or one space or two space or ',' or ';'
  63. t_start : time start of all spiketrain 0 by default
  64. unit : unit of spike times, can be a str or directly a Quantities
  65. """
  66. assert not lazy, 'Do not support lazy'
  67. unit = pq.Quantity(1, unit)
  68. seg = Segment(file_origin=os.path.basename(self.filename))
  69. f = open(self.filename, 'Ur')
  70. for i, line in enumerate(f):
  71. alldata = line[:-1].split(delimiter)
  72. if alldata[-1] == '':
  73. alldata = alldata[:-1]
  74. if alldata[0] == '':
  75. alldata = alldata[1:]
  76. spike_times = np.array(alldata).astype('f')
  77. t_stop = spike_times.max() * unit
  78. sptr = SpikeTrain(spike_times * unit, t_start=t_start, t_stop=t_stop)
  79. sptr.annotate(channel_index=i)
  80. seg.spiketrains.append(sptr)
  81. f.close()
  82. seg.create_many_to_one_relationship()
  83. return seg
  84. def write_segment(self, segment,
  85. delimiter='\t',
  86. ):
  87. """
  88. Write SpikeTrain of a Segment in a txt file.
  89. Each row is a spiketrain.
  90. Arguments:
  91. segment : the segment to write. Only analog signals will be written.
  92. delimiter : columns delimiter in file '\t' or one space or two space or ',' or ';'
  93. information of t_start is lost
  94. """
  95. f = open(self.filename, 'w')
  96. for s, sptr in enumerate(segment.spiketrains):
  97. for ts in sptr:
  98. f.write('{:f}{}'.format(ts, delimiter))
  99. f.write('\n')
  100. f.close()