Source

python-axonbinaryfile / axonbinaryfile / __init__.py

import _abf
import exceptions
class AbfNotSupported(exceptions.Exception):
    pass

    
class Abf(object):
    """
    reads an axon abf file
    supports versions upto and including 2.0 (pClamp9 and pClamp10)

    all the meta data is stored in self.header dictionary
    some is copied to show the
    num_channels
    channel_names
    
    self.sample_interval_us is the number of usec between adjacent ADC collections based on ABFHeader.lADCSequenceInterval (used by axon)

    self.dt_usec is the time in usec between adjacent points in a
    trace--it's probably what you want to use
    
    data access is via self.arr which is a numpy array of the form
    arr[
    
    """
    abf_modes = {
        1: 'VARLENEVENTS',
        2: 'FIXLENEVENTS',        #(ABF_FIXLENEVENTS == ABF_LOSSFREEOSC)
        3: 'LOSSFREEOSC',
        3: 'GAPFREEFILE',
        4: 'HIGHSPEEDOSC',
        5: 'WAVEFORMFILE',
        }

    def __init__(self, file_name):
        self.file_name = file_name
        self.header = _abf.get_header_info(file_name)

        if self.header['OperationMode'] == 5:
            self.description = "axon abf file recorded in episodic mode (WAVEFORMFILE mode)"
            self.header, self.arr = _abf.read_episodic_file(file_name)
            self.num_adcs = self.header['ADCNumChannels'] # number inputs
            self.adc_names = self.header['ADCChannelName'] #input names

            self.sample_interval_us = self.header['ADCSequenceInterval']
            # dt is the time between samples in a trace (episode)
            self.dt_us = self.sample_interval_us
            self._clean_header_strings()
        else:
            
            raise AbfNotSupported("Only support episodic abf files.  This type, %s, is not yet supported" % self.abf_modes[self.header['OperationMode']])
        
    def __getitem__(self,key):
        # this is prbably a silly interface-quite inefficient
        return self.arr[key]

    def __repr__(self):
        return "Abf('%s')" % self.file_name

    def __unicode__(self):
        return "Abf('%s') %s" % (self.file_name, self.modename)

    def _clean_header_strings(self):
        """only called after reading in header, do cleanup of the
        representation"""
        strArrays = ['ADCChannelName', 'ADCUnits','DACChannelName',
                       'DACChannelUnits', 'DACFilePath','ULParamValueList',]
        h = self.header
        for sa in strArrays:
            slist = h[sa]
            for ii in range(len(slist)):
                slist[ii] = slist[ii].strip()
                
                       

    def get_cmd_waveform(self, episode, dac_channel):
        """
        easy way to get the stimulus waveform for a run
        note per python way, episode count starts at 0

        don't yet check if parameters are in range
        returns at float32 1dim numpy array
        """
        return _abf.get_cmdwaveform(self.file_name, episode+1, dac_channel)
        


def readabf(file_name):
    """reads an axon abf file
    supports versions upto and including 2.0 (pClamp9 and pClamp10)
    returns the data in the form of an Abf() class instance
    """
    return Abf(file_name)