-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathOpenEphys.py
578 lines (450 loc) · 21.4 KB
/
OpenEphys.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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 3 15:18:38 2014
@author: Dan Denman and Josh Siegle
Loads .continuous, .events, and .spikes files saved from the Open Ephys GUI
Usage:
import OpenEphys
data = OpenEphys.load(pathToFile) # returns a dict with data, timestamps, etc.
"""
import os
import math
import numpy as np
import scipy.signal
import scipy.io
import time
import struct
from copy import deepcopy
import matplotlib.pylab as plt
# constants
NUM_HEADER_BYTES = 1024
SAMPLES_PER_RECORD = 1024
BYTES_PER_SAMPLE = 2
RECORD_SIZE = 4 + 8 + SAMPLES_PER_RECORD * BYTES_PER_SAMPLE + 10 # size of each continuous record in bytes
RECORD_MARKER = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 255])
# constants for pre-allocating matrices:
MAX_NUMBER_OF_SPIKES = int(1e6)
MAX_NUMBER_OF_RECORDS = int(1e6)
MAX_NUMBER_OF_EVENTS = int(1e6)
def load(filepath):
# redirects to code for individual file types
if 'continuous' in filepath:
data = loadContinuous(filepath)
elif 'spikes' in filepath:
data = loadSpikes(filepath)
elif 'events' in filepath:
data = loadEvents(filepath)
else:
raise Exception("Not a recognized file type. Please input a .continuous, .spikes, or .events file")
return data
def loadFolder(folderpath,**kwargs):
# load all continuous files in a folder
data = { }
# load all continuous files in a folder
if 'channels' in kwargs.keys():
filelist = ['100_CH'+x+'.continuous' for x in map(str,kwargs['channels'])]
else:
filelist = os.listdir(folderpath)
t0 = time.time()
numFiles = 0
for i, f in enumerate(filelist):
if '.continuous' in f:
data[f.replace('.continuous','')] = loadContinuous(os.path.join(folderpath, f))
numFiles += 1
print(''.join(('Avg. Load Time: ', str((time.time() - t0)/numFiles),' sec')))
print(''.join(('Total Load Time: ', str((time.time() - t0)),' sec')))
return data
def loadFolderToArray(folderpath, channels = 'all', dtype = float, source = '100'):
'''Load CH continuous files in specified folder to a single numpy array. By default all
CH continous files are loaded in numerical order, ordering can be specified with
optional channels argument which should be a list of channel numbers.'''
if channels == 'all':
channels = _get_sorted_channels(folderpath)
filelist = [source + '_CH' + x + '.continuous' for x in map(str,channels)]
t0 = time.time()
numFiles = 1
channel_1_data = loadContinuous(os.path.join(folderpath, filelist[0]), dtype)['data']
n_samples = len(channel_1_data)
n_channels = len(filelist)
data_array = np.zeros([n_samples, n_channels], dtype)
data_array[:,0] = channel_1_data
for i, f in enumerate(filelist[1:]):
data_array[:, i + 1] = loadContinuous(os.path.join(folderpath, f), dtype)['data']
numFiles += 1
print(''.join(('Avg. Load Time: ', str((time.time() - t0)/numFiles),' sec')))
print(''.join(('Total Load Time: ', str((time.time() - t0)),' sec')))
return data_array
def writeHeader(f,header):
headerstr=''
for k,v in header.items():
k = 'header.'+k.strip()
headerstr = headerstr+'{0} = {1};\n'.format(k,v)
headerstr=headerstr.ljust(1024)
f.write(headerstr.encode('ascii'))
def writeFrame(f, timestamp, recording_num, x):
byteWritten = 0
if x.size==1024:
byteWritten += f.write(np.array(timestamp).astype('<i8').tobytes())
byteWritten += f.write(np.array(1024).astype('<i2').tobytes())
byteWritten += f.write(np.array(recording_num).astype('<i2').tobytes())
byteWritten += f.write(x.astype('>i2').tobytes())
byteWritten += f.write(np.array([0,1,2,3,4,5,6,7,8,255]).astype(np.byte).tobytes())
else:
print('Data point not correct. Skipped')
return byteWritten
def writeContinuousFile(fname,header,timestamp,x,recording_num=None,dtype=np.float):
f = open(fname,'wb')
writeHeader(f,header)
noFrame = x.size//1024
if dtype == np.float:
#convert back the value to int according to the bitVolts
x = np.round(x/np.float(header['bitVolts']))
for i in range(noFrame):
if recording_num is not None:
writeFrame(f,timestamp[i],recording_num[i],x[i*1024:(i+1)*1024])
else:
writeFrame(f,timestamp[i],0,x[i*1024:(i+1)*1024])
f.close()
def loadContinuousFast(filepath, dtype = float):
#A much faster implementation for loading continous file
#load all data at once rather than by chunks
assert dtype in (float, np.int16), \
'Invalid data type specified for loadContinous, valid types are float and np.int16'
print("Loading continuous data...")
ch = { }
#read in the data
f = open(filepath,'rb')
fileLength = os.fstat(f.fileno()).st_size
# calculate number of samples
recordBytes = fileLength - NUM_HEADER_BYTES
if recordBytes % RECORD_SIZE != 0:
raise Exception("File size is not consistent with a continuous file: may be corrupt")
nrec = recordBytes // RECORD_SIZE
nsamp = nrec * SAMPLES_PER_RECORD
# pre-allocate samples
samples = np.zeros(nsamp, dtype)
timestamps = np.zeros(nrec)
recordingNumbers = np.zeros(nrec)
indices = np.arange(0, nsamp + 1, SAMPLES_PER_RECORD, np.dtype(np.int64))
header = readHeader(f)
buffer = f.read()
data_tmp=np.frombuffer(buffer,np.dtype('>i2')) #read everything into a large buffer
data_tmp = data_tmp.reshape(int(len(data_tmp)/(RECORD_SIZE/2)),int(RECORD_SIZE/2)) #reshape it into each chunk
timestamps = data_tmp[:,:4].ravel().view('<i8') #reinterpret the timestamp
N = data_tmp[:,4].ravel().view('<u2') #reinterpret number of recording
recordingNumbers = data_tmp[:,5].ravel().view('>u2') #reintepret the recording number
if np.any(N!=SAMPLES_PER_RECORD):
raise Exception('Found corrupted record at '+np.where(N!=SAMPLES_PER_RECORD))
if dtype == float: # Convert data to float array and convert bits to voltage.
samples=data_tmp[:,6:6+SAMPLES_PER_RECORD].ravel() * float(header['bitVolts']) # #extract the data
else: # Keep data in signed 16 bit integer format.
samples=data_tmp[:,6:6+SAMPLES_PER_RECORD].ravel()
ch['header'] = header
ch['timestamps'] = timestamps
ch['data'] = samples # OR use downsample(samples,1), to save space
ch['recordingNumber'] = recordingNumbers
f.close()
return ch
def loadContinuous(filepath, dtype = float):
assert dtype in (float, np.int16), \
'Invalid data type specified for loadContinous, valid types are float and np.int16'
print("Loading continuous data...")
ch = { }
#read in the data
f = open(filepath,'rb')
fileLength = os.fstat(f.fileno()).st_size
# calculate number of samples
recordBytes = fileLength - NUM_HEADER_BYTES
if recordBytes % RECORD_SIZE != 0:
raise Exception("File size is not consistent with a continuous file: may be corrupt")
nrec = recordBytes // RECORD_SIZE
nsamp = nrec * SAMPLES_PER_RECORD
# pre-allocate samples
samples = np.zeros(nsamp, dtype)
timestamps = np.zeros(nrec)
recordingNumbers = np.zeros(nrec)
indices = np.arange(0, nsamp + 1, SAMPLES_PER_RECORD, np.dtype(np.int64))
header = readHeader(f)
recIndices = np.arange(0, nrec)
for recordNumber in recIndices:
timestamps[recordNumber] = np.fromfile(f,np.dtype('<i8'),1) # little-endian 64-bit signed integer
N = np.fromfile(f,np.dtype('<u2'),1)[0] # little-endian 16-bit unsigned integer
#print index
if N != SAMPLES_PER_RECORD:
raise Exception('Found corrupted record in block ' + str(recordNumber))
recordingNumbers[recordNumber] = (np.fromfile(f,np.dtype('>u2'),1)) # big-endian 16-bit unsigned integer
if dtype == float: # Convert data to float array and convert bits to voltage.
data = np.fromfile(f,np.dtype('>i2'),N) * float(header['bitVolts']) # big-endian 16-bit signed integer, multiplied by bitVolts
else: # Keep data in signed 16 bit integer format.
data = np.fromfile(f,np.dtype('>i2'),N) # big-endian 16-bit signed integer
samples[indices[recordNumber]:indices[recordNumber+1]] = data
marker = f.read(10) # dump
#print recordNumber
#print index
ch['header'] = header
ch['timestamps'] = timestamps
ch['data'] = samples # OR use downsample(samples,1), to save space
ch['recordingNumber'] = recordingNumbers
f.close()
return ch
def loadSpikes(filepath):
'''
Loads spike waveforms and timestamps from filepath (should be .spikes file)
'''
data = { }
print('loading spikes...')
f = open(filepath, 'rb')
header = readHeader(f)
if float(header[' version']) < 0.4:
raise Exception('Loader is only compatible with .spikes files with version 0.4 or higher')
data['header'] = header
numChannels = int(header['num_channels'])
numSamples = 40 # **NOT CURRENTLY WRITTEN TO HEADER**
spikes = np.zeros((MAX_NUMBER_OF_SPIKES, numSamples, numChannels))
timestamps = np.zeros(MAX_NUMBER_OF_SPIKES)
source = np.zeros(MAX_NUMBER_OF_SPIKES)
gain = np.zeros((MAX_NUMBER_OF_SPIKES, numChannels))
thresh = np.zeros((MAX_NUMBER_OF_SPIKES, numChannels))
sortedId = np.zeros((MAX_NUMBER_OF_SPIKES, numChannels))
recNum = np.zeros(MAX_NUMBER_OF_SPIKES)
currentSpike = 0
while f.tell() < os.fstat(f.fileno()).st_size:
eventType = np.fromfile(f, np.dtype('<u1'),1) #always equal to 4, discard
timestamps[currentSpike] = np.fromfile(f, np.dtype('<i8'), 1)
software_timestamp = np.fromfile(f, np.dtype('<i8'), 1)
source[currentSpike] = np.fromfile(f, np.dtype('<u2'), 1)
numChannels = np.fromfile(f, np.dtype('<u2'), 1)
numSamples = np.fromfile(f, np.dtype('<u2'), 1)
sortedId[currentSpike] = np.fromfile(f, np.dtype('<u2'),1)
electrodeId = np.fromfile(f, np.dtype('<u2'),1)
channel = np.fromfile(f, np.dtype('<u2'),1)
color = np.fromfile(f, np.dtype('<u1'), 3)
pcProj = np.fromfile(f, np.float32, 2)
sampleFreq = np.fromfile(f, np.dtype('<u2'),1)
waveforms = np.fromfile(f, np.dtype('<u2'), numChannels*numSamples)
gain[currentSpike,:] = np.fromfile(f, np.float32, numChannels)
thresh[currentSpike,:] = np.fromfile(f, np.dtype('<u2'), numChannels)
recNum[currentSpike] = np.fromfile(f, np.dtype('<u2'), 1)
waveforms_reshaped = np.reshape(waveforms, (numChannels, numSamples))
waveforms_reshaped = waveforms_reshaped.astype(float)
waveforms_uv = waveforms_reshaped
for ch in range(numChannels):
waveforms_uv[ch, :] -= 32768
waveforms_uv[ch, :] /= gain[currentSpike, ch]*1000
spikes[currentSpike] = waveforms_uv.T
currentSpike += 1
data['spikes'] = spikes[:currentSpike,:,:]
data['timestamps'] = timestamps[:currentSpike]
data['source'] = source[:currentSpike]
data['gain'] = gain[:currentSpike,:]
data['thresh'] = thresh[:currentSpike,:]
data['recordingNumber'] = recNum[:currentSpike]
data['sortedId'] = sortedId[:currentSpike]
return data
def loadSpikesFast(filepath):
data = { }
f = open(filepath,'rb')
header = readHeader(f)
if float(header[' version']) < 0.4:
raise Exception('Loader is only compatible with .spikes files with version 0.4 or higher')
data['header'] = header
numChannels = int(header['num_channels'])
numSamples = 40 # **NOT CURRENTLY WRITTEN TO HEADER**
# Record schema
dt = [('type', '<u1'),
('timestamp', '<i8'),
('softtime', '<i8'),
('source', '<u2'),
('numchan', '<u2'),
('numsamp', '<u2'),
('sortid', '<u2'),
('electrodeid', '<u2'),
('channel', '<u2'),
('color', '<u1',(3)), # The color of a spike...
('pcproj', np.float32 ,(2)),
('sampfreq', '<u2'),
('waveform', '<u2', (numChannels, numSamples)),
('gain', np.float32, (numChannels)),
('thresh', '<u2', (numChannels)),
('recordnum', '<u2')]
record_type = np.dtype(dt);
record_bytes = record_type.itemsize
bytes_left = os.fstat(f.fileno()).st_size - f.tell()
records_to_read = math.floor(bytes_left/record_bytes)
if records_to_read > MAX_NUMBER_OF_SPIKES:
print("Warning: only loading {} spikes".format(MAX_NUMBER_OF_SPIKES))
records_to_read = MAX_NUMBER_OF_SPIKES
data['data'] = np.zeros(records_to_read, dtype=record_type)
data['data'] = np.fromfile(f, record_type, records_to_read)
return data
def loadEvents(filepath):
data = { }
print('loading events...')
f = open(filepath,'rb')
header = readHeader(f)
if float(header[' version']) < 0.4:
raise Exception('Loader is only compatible with .events files with version 0.4 or higher')
data['header'] = header
index = -1
channel = np.zeros(MAX_NUMBER_OF_EVENTS)
timestamps = np.zeros(MAX_NUMBER_OF_EVENTS)
sampleNum = np.zeros(MAX_NUMBER_OF_EVENTS)
nodeId = np.zeros(MAX_NUMBER_OF_EVENTS)
eventType = np.zeros(MAX_NUMBER_OF_EVENTS)
eventId = np.zeros(MAX_NUMBER_OF_EVENTS)
recordingNumber = np.zeros(MAX_NUMBER_OF_EVENTS)
while f.tell() < os.fstat(f.fileno()).st_size:
index += 1
timestamps[index] = np.fromfile(f, np.dtype('<i8'), 1)
sampleNum[index] = np.fromfile(f, np.dtype('<i2'), 1)
eventType[index] = np.fromfile(f, np.dtype('<u1'), 1)
nodeId[index] = np.fromfile(f, np.dtype('<u1'), 1)
eventId[index] = np.fromfile(f, np.dtype('<u1'), 1)
channel[index] = np.fromfile(f, np.dtype('<u1'), 1)
recordingNumber[index] = np.fromfile(f, np.dtype('<u2'), 1)
data['channel'] = channel[:index]
data['timestamps'] = timestamps[:index]
data['eventType'] = eventType[:index]
data['nodeId'] = nodeId[:index]
data['eventId'] = eventId[:index]
data['recordingNumber'] = recordingNumber[:index]
data['sampleNum'] = sampleNum[:index]
return data
def readHeader(f):
header = { }
h = f.read(1024).decode().replace('\n','').replace('header.','')
for i,item in enumerate(h.split(';')):
if '=' in item:
header[item.split(' = ')[0]] = item.split(' = ')[1]
return header
def downsample(trace,down):
downsampled = scipy.signal.resample(trace,np.shape(trace)[0]/down)
return downsampled
def pack(folderpath,source='100',**kwargs):
#convert single channel open ephys channels to a .dat file for compatibility with the KlustaSuite, Neuroscope and Klusters
#should not be necessary for versions of open ephys which write data into HDF5 format.
#loads .continuous files in the specified folder and saves a .DAT in that folder
#optional arguments:
# source: string name of the source that openephys uses as the prefix. is usually 100, if the headstage is the first source added, but can specify something different
#
# data: pre-loaded data to be packed into a .DAT
# dref: int specifying a channel # to use as a digital reference. is subtracted from all channels.
# order: the order in which the .continuos files are packed into the .DAT. should be a list of .continious channel numbers. length must equal total channels.
# suffix: appended to .DAT filename, which is openephys.DAT if no suffix provided.
#load the openephys data into memory
if 'data' not in kwargs.keys():
if 'channels' not in kwargs.keys():
data = loadFolder(folderpath)
else:
data = loadFolder(folderpath,channels=kwargs['channels'])
else:
data = kwargs['data']
#if specified, do the digital referencing
if 'dref' in kwargs.keys():
ref =load(os.path.join(folderpath,''.join((source,'_CH',str(kwargs['dref']),'.continuous'))))
for i,channel in enumerate(data.keys()):
data[channel]['data'] = data[channel]['data'] - ref['data']
#specify the order the channels are written in
if 'order' in kwargs.keys():
order = kwargs['order']
else:
order = data.keys()
#add a suffix, if one was specified
if 'suffix' in kwargs.keys():
suffix=kwargs['suffix']
else:
suffix=''
#make a file to write the data back out into .dat format
outpath = os.path.join(folderpath,''.join(('openephys',suffix,'.dat')))
out = open(outpath,'wb')
#go through the data and write it out in the .dat format
#.dat format specified here: http://neuroscope.sourceforge.net/UserManual/data-files.html
channelOrder = []
print(''.join(('...saving .dat to ',outpath,'...')))
bar = ProgressBar(len(data[data.keys()[0]]['data']))#progressbar.ProgressBar(maxval=1, widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
for i in range(len(data[data.keys()[0]]['data'])):
for j in range(len(order)):
if source in data.keys()[0]:
ch = data[order[j]]['data']
else:
ch = data[''.join(('CH',str(order[j]).replace('CH','')))]['data']
out.write(struct.pack('h',ch[i]))#signed 16-bit integer
#figure out which order this thing packed the channels in. only do this once.
if i == 0:
channelOrder.append(order[j])
#update how mucb we have list
if i%(len(data[data.keys()[0]]['data'])/100)==0:
bar.animate(i)
#bar.update(float(i+1)/float(len(data[data.keys()[0]])))
#bar.finish()
out.close()
print(''.join(('order: ',str(channelOrder))))
print(''.join(('.dat saved to ',outpath)))
#**********************************************************
# progress bar class used to show progress of pack()
#stolen from some post on stack overflow
import sys
try:
from IPython.display import clear_output
have_ipython = True
except ImportError:
have_ipython = False
class ProgressBar:
def __init__(self, iterations):
self.iterations = iterations
self.prog_bar = '[]'
self.fill_char = '*'
self.width = 40
self.__update_amount(0)
if have_ipython:
self.animate = self.animate_ipython
else:
self.animate = self.animate_noipython
def animate_ipython(self, iter):
print('\r', self,)
sys.stdout.flush()
self.update_iteration(iter + 1)
def update_iteration(self, elapsed_iter):
self.__update_amount((elapsed_iter / float(self.iterations)) * 100.0)
self.prog_bar += ' %d of %s complete' % (elapsed_iter, self.iterations)
def __update_amount(self, new_amount):
percent_done = int(round((new_amount / 100.0) * 100.0))
all_full = self.width - 2
num_hashes = int(round((percent_done / 100.0) * all_full))
self.prog_bar = '[' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'
pct_place = (len(self.prog_bar) // 2) - len(str(percent_done))
pct_string = '%d%%' % percent_done
self.prog_bar = self.prog_bar[0:pct_place] + \
(pct_string + self.prog_bar[pct_place + len(pct_string):])
def __str__(self):
return str(self.prog_bar)
#*************************************************************
def pack_2(folderpath, filename = 'openephys.dat', source='100', channels = 'all', dref = None):
'''Alternative version of pack which uses numpy's tofile function to write data.
pack_2 is much faster than pack and avoids quantization noise incurred in pack due
to conversion of data to float voltages during loadContinous followed by rounding
back to integers for packing.
source: string name of the source that openephys uses as the prefix. It is usually 100,
if the headstage is the first source added, but can specify something different.
channels: List of channel numbers specifying order in which channels are packed. By default
all CH continous files are packed in numerical order.
dref: Digital referencing - either supply a channel number or 'ave' to reference to the
average of packed channels.
'''
data_array = loadFolderToArray(folderpath, channels, np.int16, source)
if dref:
if dref == 'ave':
print('Digital referencing to average of all channels.')
reference = np.mean(data_array,1)
else:
print('Digital referencing to channel ' + str(dref))
if channels == 'all':
channels = _get_sorted_channels(folderpath)
reference = deepcopy(data_array[:,channels.index(dref)])
for i in range(data_array.shape[1]):
data_array[:,i] = data_array[:,i] - reference
print('Packing data to file: ' + filename)
data_array.tofile(os.path.join(folderpath,filename))
def _get_sorted_channels(folderpath):
return sorted([int(f.split('_CH')[1].split('.')[0]) for f in os.listdir(folderpath)
if '.continuous' in f and '_CH' in f])