The stfio module


Christoph Schmidt-Hieber


19 August, 2021

The stfio Python module allows to read and write data in common electrophysiology formats without running Stimfit. Build instructions for GNU/Linux can be found in Building the Python module only.

The central object in the stfio module is called a Recording. There are two ways to construct a Recording: You can either read it in from a file, or you can build it up from scratch using NumPy arrays.

Reading files

Files can be opened using the read function that returns a Recording object:

>>> import stfio
>>> rec ="/home/cs/data/test.abf")

read takes a filename and optionally a file type (as a string) as an argument. At present, the following types are supported:




CED filing system




Axon binary file


Axon text file


Axograph X binary file


HEKA binary file

If the file type is None (default), it will be guessed from the file name extension.

A Recording has a number of attributes that describe the recording:

>>> print(rec.comment)
Created with Clampex
>>> print(
>>> print(rec.dt) # sampling interval
>>> print(rec.file_description) # no file description in this case

>>> print(rec.time)
>>> print(rec.xunits)

A Recording consists of one or more Channels, which in turn are composed of one or more Sections. They can be accessed using indexing operators ([]).

>>> len(rec) # Recording consists of 2 Channels
>>> len(rec[0]) # First Channel consists of 13 Sections
>>> len(rec[0][0]) # First Section in first channel contains 146450 data points
>>> print(rec[0].name) # channel name
>>> print(rec[1].name)
IN 3
>>> print(rec[0].yunits) # channel units
>>> print(rec[1].yunits)

The time series in a Section can be accessed as a NumPy array:

>>> arr = rec[0][0].asarray()
>>> type(arr)
<type 'numpy.ndarray'>
>>> arr.shape

Note that the Section itself is not a NumPy array and therefore needs to be converted as described above before you can do fancy arithmetics:

>>> type(rec[0][0])
<class 'stfio.Section'>
>>> res = rec[0][0] + 2.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Section' and 'float'
>>> res = rec[0][0].asarray() + 2.0

Constructing Recordings from scratch

Recordings can be assembled from NumPy arrays. Here’s a particularly stupid example:

import stfio
import numpy as np

arr = np.arange(0,500,0.1)

# construct Sections from arrays:
seclist = [stfio.Section(arr), stfio.Section(arr)]

# construct Channels from lists of Sections
chlist = [stfio.Channel(seclist), stfio.Channel(seclist)]
# Set channel units
chlist[0].yunits = "pA"
chlist[1].yunits = "mV"

# construct a Recording from a list of channels
rec = stfio.Recording(chlist)
rec.dt = 0.05 # set sampling interval
rec.xunits = "ms" # set time units

Writing files

Recordings can be stored to files using the write method:

>>> import stfio
>>> rec ="/home/cs/data/test.abf")
>>> rec.write("/home/cs/data/out.h5")

At present, write only supports hdf5 files.