stimfitio.py 4.9 KB

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