Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

1
1

stimfitio.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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):
  94. assert not lazy, 'Do not support lazy'
  95. if self.filename is not None:
  96. self.stfio_rec = stfio.read(self.filename)
  97. bl = Block()
  98. bl.description = self.stfio_rec.file_description
  99. bl.annotate(comment=self.stfio_rec.comment)
  100. try:
  101. bl.rec_datetime = self.stfio_rec.datetime
  102. except:
  103. bl.rec_datetime = None
  104. dt = np.round(self.stfio_rec.dt * 1e-3, 9) * pq.s # ms to s
  105. sampling_rate = 1.0 / dt
  106. t_start = 0 * pq.s
  107. # iterate over sections first:
  108. for j, recseg in enumerate(self.stfio_rec[0]):
  109. seg = Segment(index=j)
  110. length = len(recseg)
  111. # iterate over channels:
  112. for i, recsig in enumerate(self.stfio_rec):
  113. name = recsig.name
  114. unit = recsig.yunits
  115. try:
  116. pq.Quantity(1, unit)
  117. except:
  118. unit = ''
  119. signal = pq.Quantity(recsig[j], unit)
  120. anaSig = AnalogSignal(signal, sampling_rate=sampling_rate,
  121. t_start=t_start, name=str(name),
  122. channel_index=i)
  123. seg.analogsignals.append(anaSig)
  124. bl.segments.append(seg)
  125. t_start = t_start + length * dt
  126. bl.create_many_to_one_relationship()
  127. return bl