-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabfheader.py
552 lines (505 loc) · 18.2 KB
/
abfheader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# -*- coding: utf-8 -*-
"""
Classe for reading data from pCLAMP and AxoScope
files (.abf version 1 and 2), developed by Molecular device/Axon technologies.
- abf = Axon binary file
- atf is a text file based format from axon that could be
read by AsciiIO (but this file is less efficient.)
This code is a port of abfload and abf2load
written in Matlab (BSD-2-Clause licence) by :
- Copyright (c) 2009, Forrest Collman, fcollman@princeton.edu
- Copyright (c) 2004, Harald Hentschke
and available here :
http://www.mathworks.com/matlabcentral/fileexchange/22114-abf2load
Information on abf 1 and 2 formats is available here :
http://www.moleculardevices.com/pages/software/developer_info.html
This file supports the old (ABF1) and new (ABF2) format.
ABF1 (clampfit <=9) and ABF2 (clampfit >10)
All possible mode are possible :
- event-driven variable-length mode 1 -> return several Segments per Block
- event-driven fixed-length mode 2 or 5 -> return several Segments
- gap free mode -> return one (or sevral) Segment in the Block
Supported : Read
Author: sgarcia, jnowacki
Note: j.s.nowacki@gmail.com has a C++ library with SWIG bindings which also
reads abf files - would be good to cross-check
"""
import struct
import datetime
import os
import io
import numpy as np
class struct_file(io.BufferedReader):
def read_f(self, fmt, offset=None):
if offset is not None:
self.seek(offset)
return struct.unpack(fmt, self.read(struct.calcsize(fmt)))
def write_f(self, fmt, offset=None, *args):
if offset is not None:
self.seek(offset)
self.write(struct.pack(fmt, *args))
def reformat_integer_V1(data, nbchannel, header):
"""
reformat when dtype is int16 for ABF version 1
"""
chans = [chan_num for chan_num in
header['nADCSamplingSeq'] if chan_num >= 0]
for n, i in enumerate(chans[:nbchannel]): # respect SamplingSeq
data[:, n] /= header['fInstrumentScaleFactor'][i]
data[:, n] /= header['fSignalGain'][i]
data[:, n] /= header['fADCProgrammableGain'][i]
if header['nTelegraphEnable'][i]:
data[:, n] /= header['fTelegraphAdditGain'][i]
data[:, n] *= header['fADCRange']
data[:, n] /= header['lADCResolution']
data[:, n] += header['fInstrumentOffset'][i]
data[:, n] -= header['fSignalOffset'][i]
def reformat_integer_V2(data, nbchannel, header):
"""
reformat when dtype is int16 for ABF version 2
"""
for i in range(nbchannel):
data[:, i] /= header['listADCInfo'][i]['fInstrumentScaleFactor']
data[:, i] /= header['listADCInfo'][i]['fSignalGain']
data[:, i] /= header['listADCInfo'][i]['fADCProgrammableGain']
if header['listADCInfo'][i]['nTelegraphEnable']:
data[:, i] /= header['listADCInfo'][i]['fTelegraphAdditGain']
data[:, i] *= header['protocol']['fADCRange']
data[:, i] /= header['protocol']['lADCResolution']
data[:, i] += header['listADCInfo'][i]['fInstrumentOffset']
data[:, i] -= header['listADCInfo'][i]['fSignalOffset']
def read_header(filename):
"""
read the header of the file
The strategy differ here from the original script under Matlab.
In the original script for ABF2, it complete the header with
informations that are located in other structures.
In ABF2 this function return header with sub dict :
sections (ABF2)
protocol (ABF2)
listTags (ABF1&2)
listADCInfo (ABF2)
listDACInfo (ABF2)
dictEpochInfoPerDAC (ABF2)
that contain more information.
"""
fid = struct_file(io.open(filename, 'rb')) # fix for py3
# version
fFileSignature = fid.read(4)
if fFileSignature == b'ABF ': # fix for p3 where read returns bytes
headerDescription = headerDescriptionV1
elif fFileSignature == b'ABF2':
headerDescription = headerDescriptionV2
else:
return None
# construct dict
header = {}
for key, offset, fmt in headerDescription:
val = fid.read_f(fmt, offset=offset)
if len(val) == 1:
header[key] = val[0]
else:
header[key] = np.array(val)
# correction of version number and starttime
if fFileSignature == b'ABF ':
header['lFileStartTime'] = header['lFileStartTime'] +\
header['nFileStartMillisecs'] * .001
elif fFileSignature == b'ABF2':
n = header['fFileVersionNumber']
header['fFileVersionNumber'] = n[3] + 0.1 * n[2] +\
0.01 * n[1] + 0.001 * n[0]
header['lFileStartTime'] = header['uFileStartTimeMS'] * .001
if header['fFileVersionNumber'] < 2.:
# tags
listTag = []
for i in range(header['lNumTagEntries']):
fid.seek(header['lTagSectionPtr'] + i * 64)
tag = {}
for key, fmt in TagInfoDescription:
val = fid.read_f(fmt)
if len(val) == 1:
tag[key] = val[0]
else:
tag[key] = np.array(val)
listTag.append(tag)
header['listTag'] = listTag
#protocol name formatting #TODO move to read_protocol?
header['sProtocolPath'] = clean_string(header['sProtocolPath'])
header['sProtocolPath'] = header['sProtocolPath'].\
replace(b'\\', b'/')
elif header['fFileVersionNumber'] >= 2.:
# in abf2 some info are in other place
# sections
sections = {}
for s, sectionName in enumerate(sectionNames):
uBlockIndex, uBytes, llNumEntries =\
fid.read_f('IIl', offset=76 + s * 16)
sections[sectionName] = {}
sections[sectionName]['uBlockIndex'] = uBlockIndex
sections[sectionName]['uBytes'] = uBytes
sections[sectionName]['llNumEntries'] = llNumEntries
header['sections'] = sections
# strings sections
# hack for reading channels names and units
fid.seek(sections['StringsSection']['uBlockIndex'] * BLOCKSIZE)
bigString = fid.read(sections['StringsSection']['uBytes'])
goodstart = bigString.lower().find(b'clampex')
if goodstart == -1:
goodstart = bigString.lower().find(b'axoscope')
bigString = bigString[goodstart:]
strings = bigString.split(b'\x00')
# ADC sections
header['listADCInfo'] = []
for i in range(sections['ADCSection']['llNumEntries']):
# read ADCInfo
fid.seek(sections['ADCSection']['uBlockIndex'] *
BLOCKSIZE + sections['ADCSection']['uBytes'] * i)
ADCInfo = {}
for key, fmt in ADCInfoDescription:
val = fid.read_f(fmt)
if len(val) == 1:
ADCInfo[key] = val[0]
else:
ADCInfo[key] = np.array(val)
ADCInfo['ADCChNames'] = strings[ADCInfo['lADCChannelNameIndex']
- 1]
ADCInfo['ADCChUnits'] = strings[ADCInfo['lADCUnitsIndex'] - 1]
header['listADCInfo'].append(ADCInfo)
# protocol sections
protocol = {}
fid.seek(sections['ProtocolSection']['uBlockIndex'] * BLOCKSIZE)
for key, fmt in protocolInfoDescription:
val = fid.read_f(fmt)
if len(val) == 1:
protocol[key] = val[0]
else:
protocol[key] = np.array(val)
header['protocol'] = protocol
# tags
listTag = []
for i in range(sections['TagSection']['llNumEntries']):
fid.seek(sections['TagSection']['uBlockIndex'] *
BLOCKSIZE + sections['TagSection']['uBytes'] * i)
tag = {}
for key, fmt in TagInfoDescription:
val = fid.read_f(fmt)
if len(val) == 1:
tag[key] = val[0]
else:
tag[key] = np.array(val)
listTag.append(tag)
header['listTag'] = listTag
# DAC sections
header['listDACInfo'] = []
for i in range(sections['DACSection']['llNumEntries']):
# read DACInfo
fid.seek(sections['DACSection']['uBlockIndex'] *
BLOCKSIZE + sections['DACSection']['uBytes'] * i)
DACInfo = {}
for key, fmt in DACInfoDescription:
val = fid.read_f(fmt)
if len(val) == 1:
DACInfo[key] = val[0]
else:
DACInfo[key] = np.array(val)
DACInfo['DACChNames'] = strings[DACInfo['lDACChannelNameIndex']
- 1]
DACInfo['DACChUnits'] = strings[
DACInfo['lDACChannelUnitsIndex'] - 1]
header['listDACInfo'].append(DACInfo)
# EpochPerDAC sections
# header['dictEpochInfoPerDAC'] is dict of dicts:
# - the first index is the DAC number
# - the second index is the epoch number
# It has to be done like that because data may not exist
# and may not be in sorted order
header['dictEpochInfoPerDAC'] = {}
for i in range(sections['EpochPerDACSection']['llNumEntries']):
# read DACInfo
fid.seek(sections['EpochPerDACSection']['uBlockIndex'] *
BLOCKSIZE +
sections['EpochPerDACSection']['uBytes'] * i)
EpochInfoPerDAC = {}
for key, fmt in EpochInfoPerDACDescription:
val = fid.read_f(fmt)
if len(val) == 1:
EpochInfoPerDAC[key] = val[0]
else:
EpochInfoPerDAC[key] = np.array(val)
DACNum = EpochInfoPerDAC['nDACNum']
EpochNum = EpochInfoPerDAC['nEpochNum']
# Checking if the key exists, if not, the value is empty
# so we have to create empty dict to populate
if DACNum not in header['dictEpochInfoPerDAC']:
header['dictEpochInfoPerDAC'][DACNum] = {}
header['dictEpochInfoPerDAC'][DACNum][EpochNum] =\
EpochInfoPerDAC
fid.close()
return header
BLOCKSIZE = 512
headerDescriptionV1 = [
('fFileSignature', 0, '4s'),
('fFileVersionNumber', 4, 'f'),
('nOperationMode', 8, 'h'),
('lActualAcqLength', 10, 'i'),
('nNumPointsIgnored', 14, 'h'),
('lActualEpisodes', 16, 'i'),
('lFileStartTime', 24, 'i'),
('lDataSectionPtr', 40, 'i'),
('lTagSectionPtr', 44, 'i'),
('lNumTagEntries', 48, 'i'),
('lSynchArrayPtr', 92, 'i'),
('lSynchArraySize', 96, 'i'),
('nDataFormat', 100, 'h'),
('nADCNumChannels', 120, 'h'),
('fADCSampleInterval', 122, 'f'),
('fSynchTimeUnit', 130, 'f'),
('lNumSamplesPerEpisode', 138, 'i'),
('lPreTriggerSamples', 142, 'i'),
('lEpisodesPerRun', 146, 'i'),
('fADCRange', 244, 'f'),
('lADCResolution', 252, 'i'),
('nFileStartMillisecs', 366, 'h'),
('nADCPtoLChannelMap', 378, '16h'),
('nADCSamplingSeq', 410, '16h'),
('sADCChannelName', 442, '10s'*16),
('sADCUnits', 602, '8s'*16),
('fADCProgrammableGain', 730, '16f'),
('fInstrumentScaleFactor', 922, '16f'),
('fInstrumentOffset', 986, '16f'),
('fSignalGain', 1050, '16f'),
('fSignalOffset', 1114, '16f'),
('nDigitalEnable', 1436, 'h'),
('nActiveDACChannel', 1440, 'h'),
('nDigitalHolding', 1584, 'h'),
('nDigitalInterEpisode', 1586, 'h'),
('nDigitalValue', 2588, '10h'),
('lDACFilePtr', 2048, '2i'),
('lDACFileNumEpisodes', 2056, '2i'),
('fDACCalibrationFactor', 2074, '4f'),
('fDACCalibrationOffset', 2090, '4f'),
('nWaveformEnable', 2296, '2h'),
('nWaveformSource', 2300, '2h'),
('nInterEpisodeLevel', 2304, '2h'),
('nEpochType', 2308, '20h'),
('fEpochInitLevel', 2348, '20f'),
('fEpochLevelInc', 2428, '20f'),
('lEpochInitDuration', 2508, '20i'),
('lEpochDurationInc', 2588, '20i'),
('nTelegraphEnable', 4512, '16h'),
('fTelegraphAdditGain', 4576, '16f'),
('sProtocolPath', 4898, '384s'),
]
headerDescriptionV2 = [
('fFileSignature', 0, '4s'),
('fFileVersionNumber', 4, '4b'),
('uFileInfoSize', 8, 'I'),
('lActualEpisodes', 12, 'I'),
('uFileStartDate', 16, 'I'),
('uFileStartTimeMS', 20, 'I'),
('uStopwatchTime', 24, 'I'),
('nFileType', 28, 'H'),
('nDataFormat', 30, 'H'),
('nSimultaneousScan', 32, 'H'),
('nCRCEnable', 34, 'H'),
('uFileCRC', 36, 'I'),
('FileGUID', 40, 'I'),
('uCreatorVersion', 56, 'I'),
('uCreatorNameIndex', 60, 'I'),
('uModifierVersion', 64, 'I'),
('uModifierNameIndex', 68, 'I'),
('uProtocolPathIndex', 72, 'I'),
]
sectionNames = [
'ProtocolSection',
'ADCSection',
'DACSection',
'EpochSection',
'ADCPerDACSection',
'EpochPerDACSection',
'UserListSection',
'StatsRegionSection',
'MathSection',
'StringsSection',
'DataSection',
'TagSection',
'ScopeSection',
'DeltaSection',
'VoiceTagSection',
'SynchArraySection',
'AnnotationSection',
'StatsSection',
]
protocolInfoDescription = [
('nOperationMode', 'h'),
('fADCSequenceInterval', 'f'),
('bEnableFileCompression', 'b'),
('sUnused1', '3s'),
('uFileCompressionRatio', 'I'),
('fSynchTimeUnit', 'f'),
('fSecondsPerRun', 'f'),
('lNumSamplesPerEpisode', 'i'),
('lPreTriggerSamples', 'i'),
('lEpisodesPerRun', 'i'),
('lRunsPerTrial', 'i'),
('lNumberOfTrials', 'i'),
('nAveragingMode', 'h'),
('nUndoRunCount', 'h'),
('nFirstEpisodeInRun', 'h'),
('fTriggerThreshold', 'f'),
('nTriggerSource', 'h'),
('nTriggerAction', 'h'),
('nTriggerPolarity', 'h'),
('fScopeOutputInterval', 'f'),
('fEpisodeStartToStart', 'f'),
('fRunStartToStart', 'f'),
('lAverageCount', 'i'),
('fTrialStartToStart', 'f'),
('nAutoTriggerStrategy', 'h'),
('fFirstRunDelayS', 'f'),
('nChannelStatsStrategy', 'h'),
('lSamplesPerTrace', 'i'),
('lStartDisplayNum', 'i'),
('lFinishDisplayNum', 'i'),
('nShowPNRawData', 'h'),
('fStatisticsPeriod', 'f'),
('lStatisticsMeasurements', 'i'),
('nStatisticsSaveStrategy', 'h'),
('fADCRange', 'f'),
('fDACRange', 'f'),
('lADCResolution', 'i'),
('lDACResolution', 'i'),
('nExperimentType', 'h'),
('nManualInfoStrategy', 'h'),
('nCommentsEnable', 'h'),
('lFileCommentIndex', 'i'),
('nAutoAnalyseEnable', 'h'),
('nSignalType', 'h'),
('nDigitalEnable', 'h'),
('nActiveDACChannel', 'h'),
('nDigitalHolding', 'h'),
('nDigitalInterEpisode', 'h'),
('nDigitalDACChannel', 'h'),
('nDigitalTrainActiveLogic', 'h'),
('nStatsEnable', 'h'),
('nStatisticsClearStrategy', 'h'),
('nLevelHysteresis', 'h'),
('lTimeHysteresis', 'i'),
('nAllowExternalTags', 'h'),
('nAverageAlgorithm', 'h'),
('fAverageWeighting', 'f'),
('nUndoPromptStrategy', 'h'),
('nTrialTriggerSource', 'h'),
('nStatisticsDisplayStrategy', 'h'),
('nExternalTagType', 'h'),
('nScopeTriggerOut', 'h'),
('nLTPType', 'h'),
('nAlternateDACOutputState', 'h'),
('nAlternateDigitalOutputState', 'h'),
('fCellID', '3f'),
('nDigitizerADCs', 'h'),
('nDigitizerDACs', 'h'),
('nDigitizerTotalDigitalOuts', 'h'),
('nDigitizerSynchDigitalOuts', 'h'),
('nDigitizerType', 'h'),
]
ADCInfoDescription = [
('nADCNum', 'h'),
('nTelegraphEnable', 'h'),
('nTelegraphInstrument', 'h'),
('fTelegraphAdditGain', 'f'),
('fTelegraphFilter', 'f'),
('fTelegraphMembraneCap', 'f'),
('nTelegraphMode', 'h'),
('fTelegraphAccessResistance', 'f'),
('nADCPtoLChannelMap', 'h'),
('nADCSamplingSeq', 'h'),
('fADCProgrammableGain', 'f'),
('fADCDisplayAmplification', 'f'),
('fADCDisplayOffset', 'f'),
('fInstrumentScaleFactor', 'f'),
('fInstrumentOffset', 'f'),
('fSignalGain', 'f'),
('fSignalOffset', 'f'),
('fSignalLowpassFilter', 'f'),
('fSignalHighpassFilter', 'f'),
('nLowpassFilterType', 'b'),
('nHighpassFilterType', 'b'),
('fPostProcessLowpassFilter', 'f'),
('nPostProcessLowpassFilterType', 'c'),
('bEnabledDuringPN', 'b'),
('nStatsChannelPolarity', 'h'),
('lADCChannelNameIndex', 'i'),
('lADCUnitsIndex', 'i'),
]
TagInfoDescription = [
('lTagTime', 'i'),
('sComment', '56s'),
('nTagType', 'h'),
('nVoiceTagNumber_or_AnnotationIndex', 'h'),
]
DACInfoDescription = [
('nDACNum', 'h'),
('nTelegraphDACScaleFactorEnable', 'h'),
('fInstrumentHoldingLevel', 'f'),
('fDACScaleFactor', 'f'),
('fDACHoldingLevel', 'f'),
('fDACCalibrationFactor', 'f'),
('fDACCalibrationOffset', 'f'),
('lDACChannelNameIndex', 'i'),
('lDACChannelUnitsIndex', 'i'),
('lDACFilePtr', 'i'),
('lDACFileNumEpisodes', 'i'),
('nWaveformEnable', 'h'),
('nWaveformSource', 'h'),
('nInterEpisodeLevel', 'h'),
('fDACFileScale', 'f'),
('fDACFileOffset', 'f'),
('lDACFileEpisodeNum', 'i'),
('nDACFileADCNum', 'h'),
('nConditEnable', 'h'),
('lConditNumPulses', 'i'),
('fBaselineDuration', 'f'),
('fBaselineLevel', 'f'),
('fStepDuration', 'f'),
('fStepLevel', 'f'),
('fPostTrainPeriod', 'f'),
('fPostTrainLevel', 'f'),
('nMembTestEnable', 'h'),
('nLeakSubtractType', 'h'),
('nPNPolarity', 'h'),
('fPNHoldingLevel', 'f'),
('nPNNumADCChannels', 'h'),
('nPNPosition', 'h'),
('nPNNumPulses', 'h'),
('fPNSettlingTime', 'f'),
('fPNInterpulse', 'f'),
('nLTPUsageOfDAC', 'h'),
('nLTPPresynapticPulses', 'h'),
('lDACFilePathIndex', 'i'),
('fMembTestPreSettlingTimeMS', 'f'),
('fMembTestPostSettlingTimeMS', 'f'),
('nLeakSubtractADCIndex', 'h'),
('sUnused', '124s'),
]
EpochInfoPerDACDescription = [
('nEpochNum', 'h'),
('nDACNum', 'h'),
('nEpochType', 'h'),
('fEpochInitLevel', 'f'),
('fEpochLevelInc', 'f'),
('lEpochInitDuration', 'i'),
('lEpochDurationInc', 'i'),
('lEpochPulsePeriod', 'i'),
('lEpochPulseWidth', 'i'),
('sUnused', '18s'),
]
EpochInfoDescription = [
('nEpochNum', 'h'),
('nDigitalValue', 'h'),
('nDigitalTrainValue', 'h'),
('nAlternateDigitalValue', 'h'),
('nAlternateDigitalTrainValue', 'h'),
('bEpochCompression', 'b'),
('sUnused', '21s'),
]