Commits

ch...@lee-messer.net  committed 8b4a9c7

fixed string parsing of 2d char arrays

  • Participants
  • Parent commits 7b9b0c5

Comments (0)

Files changed (7)

File axonbinaryfile/__init__.py

             self.num_channels = self.header['ADCNumChannels']
             self.channel_names = self.header['ADCChannelName']
             self.sample_interval_us = self.header['ADCSequenceInterval']
+            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 __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_waveform(self):
         
 

File axonbinaryfile/abf_hdr_snippet.c

-PyObject *tmpval=NULL, *tmplist=NULL, *tmplist1;
+PyObject *tmpval=NULL, *tmplist=NULL, *tmplist1; char tmpstr[256];
 tmpval = PyFloat_FromDouble(FH.fFileVersionNumber); PyDict_SetItem(dict, PyString_FromString("FileVersionNumber"), tmpval);  Py_DECREF(tmpval); /*simplevar fFileVersionNumber*/
 tmpval = PyInt_FromLong(FH.nOperationMode); PyDict_SetItem(dict, PyString_FromString("OperationMode"), tmpval);  Py_DECREF(tmpval); /*simplevar nOperationMode*/
 tmpval = PyLong_FromLong(FH.lActualAcqLength); PyDict_SetItem(dict, PyString_FromString("ActualAcqLength"), tmpval);  Py_DECREF(tmpval); /*simplevar lActualAcqLength*/
         tmplist = PyList_New(0);  /*2dstring sADCChannelName[ABF_ADCCOUNT][ABF_ADCNAMELEN]*/
 
           for(int ii=0; ii < ABF_ADCCOUNT; ii++) {
-          tmpval = PyString_FromString(FH.sADCChannelName[ii]);
+             for(int jj=0; jj< ABF_ADCNAMELEN; jj++) {
+                tmpstr[jj] = FH.sADCChannelName[ii][jj];
+             }
+             tmpstr[ABF_ADCNAMELEN]=0; /* null terminate*/
+          tmpval = PyString_FromString(tmpstr);
           PyList_Append(tmplist, tmpval);
           Py_DECREF(tmpval);
         }
         tmplist = PyList_New(0);  /*2dstring sADCUnits[ABF_ADCCOUNT][ABF_ADCUNITLEN]*/
 
           for(int ii=0; ii < ABF_ADCCOUNT; ii++) {
-          tmpval = PyString_FromString(FH.sADCUnits[ii]);
+             for(int jj=0; jj< ABF_ADCUNITLEN; jj++) {
+                tmpstr[jj] = FH.sADCUnits[ii][jj];
+             }
+             tmpstr[ABF_ADCUNITLEN]=0; /* null terminate*/
+          tmpval = PyString_FromString(tmpstr);
           PyList_Append(tmplist, tmpval);
           Py_DECREF(tmpval);
         }
         tmplist = PyList_New(0);  /*2dstring sDACChannelName[ABF_DACCOUNT][ABF_DACNAMELEN]*/
 
           for(int ii=0; ii < ABF_DACCOUNT; ii++) {
-          tmpval = PyString_FromString(FH.sDACChannelName[ii]);
+             for(int jj=0; jj< ABF_DACNAMELEN; jj++) {
+                tmpstr[jj] = FH.sDACChannelName[ii][jj];
+             }
+             tmpstr[ABF_DACNAMELEN]=0; /* null terminate*/
+          tmpval = PyString_FromString(tmpstr);
           PyList_Append(tmplist, tmpval);
           Py_DECREF(tmpval);
         }
         tmplist = PyList_New(0);  /*2dstring sDACChannelUnits[ABF_DACCOUNT][ABF_DACUNITLEN]*/
 
           for(int ii=0; ii < ABF_DACCOUNT; ii++) {
-          tmpval = PyString_FromString(FH.sDACChannelUnits[ii]);
+             for(int jj=0; jj< ABF_DACUNITLEN; jj++) {
+                tmpstr[jj] = FH.sDACChannelUnits[ii][jj];
+             }
+             tmpstr[ABF_DACUNITLEN]=0; /* null terminate*/
+          tmpval = PyString_FromString(tmpstr);
           PyList_Append(tmplist, tmpval);
           Py_DECREF(tmpval);
         }
         tmplist = PyList_New(0);  /*2dstring sDACFilePath[ABF_DACCOUNT][ABF_PATHLEN]*/
 
           for(int ii=0; ii < ABF_DACCOUNT; ii++) {
-          tmpval = PyString_FromString(FH.sDACFilePath[ii]);
+             for(int jj=0; jj< ABF_PATHLEN; jj++) {
+                tmpstr[jj] = FH.sDACFilePath[ii][jj];
+             }
+             tmpstr[ABF_PATHLEN]=0; /* null terminate*/
+          tmpval = PyString_FromString(tmpstr);
           PyList_Append(tmplist, tmpval);
           Py_DECREF(tmpval);
         }
         tmplist = PyList_New(0);  /*2dstring sULParamValueList[ABF_USERLISTCOUNT][ABF_USERLISTLEN]*/
 
           for(int ii=0; ii < ABF_USERLISTCOUNT; ii++) {
-          tmpval = PyString_FromString(FH.sULParamValueList[ii]);
+             for(int jj=0; jj< ABF_USERLISTLEN; jj++) {
+                tmpstr[jj] = FH.sULParamValueList[ii][jj];
+             }
+             tmpstr[ABF_USERLISTLEN]=0; /* null terminate*/
+          tmpval = PyString_FromString(tmpstr);
           PyList_Append(tmplist, tmpval);
           Py_DECREF(tmpval);
         }

File axonbinaryfile/hdr_parse.py

     if not cline[2]: # regular variable
         pass
         
-production_list = ["PyObject *tmpval=NULL, *tmplist=NULL, *tmplist1;"]
+production_list = ["PyObject *tmpval=NULL, *tmplist=NULL, *tmplist1; char tmpstr[256];"]
 # use tmplist, tmpval
 for var in catlines:
     if var['category']=='simplevar':
         # try as a 1d array of strings
         # pprint.pprint(var)
         tmps=[]
-        var['converter'] = '''%s(FH.%s[ii])''' % (var_prod_dict[var['ctype']], var['id'])
-
+        var['converter'] = '''%s(tmpstr)''' % (var_prod_dict[var['ctype']])
+        # assert %(len1) < 256 # len(tmpstr)
         tmps.append('''
         tmplist = PyList_New(0);  /*%(category)s %(orig)s*/''' % var)
         tmps.append('''
           for(int ii=0; ii < %(len0)s; ii++) {
+             for(int jj=0; jj< %(len1)s; jj++) {
+                tmpstr[jj] = FH.%(id)s[ii][jj];
+             }
+             tmpstr[%(len1)s]=0; /* null terminate*/
           tmpval = %(converter)s;
           PyList_Append(tmplist, tmpval);
           Py_DECREF(tmpval);

File axonbinaryfile/test_abf.py

 import _abf, pprint
 a,data =  _abf.read_episodic_file('../tests/pclamp10v2abf-1.abf')
+pprint.pprint(a)
 open('test-md.txt.compare', 'wb+').write(pprint.pformat(a))
 # ADCChannelName
 s = _abf.get_cmdwaveform('../tests/pclamp10v2abf-1.abf', 0,0)

File axonbinaryfile/utils/__init__.py

+#
+from plottraces import *

File tests/simple-vc-1.abf

Binary file added.

File tests/simple-vc-MT-2.abf

Binary file added.