-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenEphys.py
225 lines (168 loc) · 7.7 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
# -*- coding: utf-8 -*-
import os
import numpy as np
import scipy.signal
import scipy.io
import time
import struct
from copy import deepcopy
# 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, dtype = float):
# redirects to code for individual file types
if 'continuous' in filepath:
data = loadContinuous(filepath, dtype)
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 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)[0]
numSamples = np.fromfile(f, np.dtype('<u2'), 1)[0]
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 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