stimfitio.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. """
  2. README
  3. ===============================================================================
  4. This is an adapter to represent stfio objects as neo objects.
  5. stfio is a standalone file i/o Python module that ships with the Stimfit
  6. program (http://www.stimfit.org). It's a Python wrapper around Stimfit's file
  7. i/o library (libstfio) that natively provides support for the following file
  8. types:
  9. - ABF (Axon binary file format; pClamp 6--9)
  10. - ABF2 (Axon binary file format 2; pClamp 10+)
  11. - ATF (Axon text file format)
  12. - AXGX/AXGD (Axograph X file format)
  13. - CFS (Cambridge electronic devices filing system)
  14. - HEKA (HEKA binary file format)
  15. - HDF5 (Hierarchical data format 5; only hdf5 files written by Stimfit or
  16. stfio are supported)
  17. In addition, libstfio can use the biosig file i/o library as an additional file
  18. handling backend (http://biosig.sourceforge.net/), extending support to more
  19. than 30 additional file formats (http://pub.ist.ac.at/~schloegl/biosig/TESTED).
  20. Based on exampleio.py and axonio.py from neo.io
  21. 08 Feb 2014, C. Schmidt-Hieber, University College London
  22. """
  23. # needed for python 3 compatibility
  24. from __future__ import absolute_import
  25. import sys
  26. import numpy as np
  27. import quantities as pq
  28. from neo.io.baseio import BaseIO
  29. from neo.core import Block, Segment, AnalogSignal
  30. try:
  31. import stfio
  32. except ImportError as err:
  33. HAS_STFIO = False
  34. STFIO_ERR = err
  35. else:
  36. HAS_STFIO = True
  37. STFIO_ERR = None
  38. class StimfitIO(BaseIO):
  39. """
  40. Class for converting a stfio Recording to a Neo object.
  41. Provides a standardized representation of the data as defined by the neo
  42. project; this is useful to explore the data with an increasing number of
  43. electrophysiology software tools that rely on the Neo standard.
  44. stfio is a standalone file i/o Python module that ships with the Stimfit
  45. program (http://www.stimfit.org). It is a Python wrapper around Stimfit's file
  46. i/o library (libstfio) that natively provides support for the following file
  47. types:
  48. - ABF (Axon binary file format; pClamp 6--9)
  49. - ABF2 (Axon binary file format 2; pClamp 10+)
  50. - ATF (Axon text file format)
  51. - AXGX/AXGD (Axograph X file format)
  52. - CFS (Cambridge electronic devices filing system)
  53. - HEKA (HEKA binary file format)
  54. - HDF5 (Hierarchical data format 5; only hdf5 files written by Stimfit or
  55. stfio are supported)
  56. In addition, libstfio can use the biosig file i/o library as an additional file
  57. handling backend (http://biosig.sourceforge.net/), extending support to more
  58. than 30 additional file formats (http://pub.ist.ac.at/~schloegl/biosig/TESTED).
  59. Example usage:
  60. >>> import neo
  61. >>> neo_obj = neo.io.StimfitIO("file.abf")
  62. or
  63. >>> import stfio
  64. >>> stfio_obj = stfio.read("file.abf")
  65. >>> neo_obj = neo.io.StimfitIO(stfio_obj)
  66. """
  67. is_readable = True
  68. is_writable = False
  69. supported_objects = [Block, Segment, AnalogSignal]
  70. readable_objects = [Block]
  71. writeable_objects = []
  72. has_header = False
  73. is_streameable = False
  74. read_params = {Block: []}
  75. write_params = None
  76. name = 'Stimfit'
  77. extensions = ['abf', 'dat', 'axgx', 'axgd', 'cfs']
  78. mode = 'file'
  79. def __init__(self, filename=None):
  80. """
  81. Arguments:
  82. filename : Either a filename or a stfio Recording object
  83. """
  84. if not HAS_STFIO:
  85. raise STFIO_ERR
  86. BaseIO.__init__(self)
  87. if hasattr(filename, 'lower'):
  88. self.filename = filename
  89. self.stfio_rec = None
  90. else:
  91. self.stfio_rec = filename
  92. self.filename = None
  93. def read_block(self, lazy=False, cascade=True):
  94. if self.filename is not None:
  95. self.stfio_rec = stfio.read(self.filename)
  96. bl = Block()
  97. bl.description = self.stfio_rec.file_description
  98. bl.annotate(comment=self.stfio_rec.comment)
  99. try:
  100. bl.rec_datetime = self.stfio_rec.datetime
  101. except:
  102. bl.rec_datetime = None
  103. if not cascade:
  104. return bl
  105. dt = np.round(self.stfio_rec.dt * 1e-3, 9) * pq.s # ms to s
  106. sampling_rate = 1.0/dt
  107. t_start = 0 * pq.s
  108. # iterate over sections first:
  109. for j, recseg in enumerate(self.stfio_rec[0]):
  110. seg = Segment(index=j)
  111. length = len(recseg)
  112. # iterate over channels:
  113. for i, recsig in enumerate(self.stfio_rec):
  114. name = recsig.name
  115. unit = recsig.yunits
  116. try:
  117. pq.Quantity(1, unit)
  118. except:
  119. unit = ''
  120. if lazy:
  121. signal = pq.Quantity([], unit)
  122. else:
  123. signal = pq.Quantity(recsig[j], unit)
  124. anaSig = AnalogSignal(signal, sampling_rate=sampling_rate,
  125. t_start=t_start, name=str(name),
  126. channel_index=i)
  127. if lazy:
  128. anaSig.lazy_shape = length
  129. seg.analogsignals.append(anaSig)
  130. bl.segments.append(seg)
  131. t_start = t_start + length * dt
  132. bl.create_many_to_one_relationship()
  133. return bl