-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_db.py
285 lines (232 loc) · 10.8 KB
/
build_db.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
import h5py
import pandas as pd
import scipy.io as sio
import numpy as np
from physutils import decimate
from dbio import make_path
class DataSets:
def __init__(self, data_dir, behavior_dir, channel_file, spk_file, lfp_file, behavior_file, output_file, plexon_event_codes, flatten_events=False):
self.datadir = data_dir # directory for plexon data files
self.behdir = behavior_dir # directory for behavior files
self.chanfile = channel_file # file listing spike channels to import
self.spkfile = spk_file # file listing spike units to import
self.lfpfile = lfp_file # file listing lfp channels to import
self.behfile = behavior_file # file mapping phys to behavior files
self.outfile = output_file # name of output data file
self.plx_codes = plexon_event_codes # dict mapping event names to plexon codes
self.flatten_events = flatten_events # encode events in a single column (False => 1 column per event)
def write_to_db(self, tblname, df, **kwargs):
df.to_hdf(self.outfile, tblname, append=True)
def add_metadata(self, dbname, tblname, **kwargs):
fobj = h5py.File(dbname, 'a')
for k in kwargs:
fobj[tblname].attrs[k] = kwargs[k]
fobj.close()
def import_spikes(self, ftup):
pdir = 'patient' + str(ftup[0]).zfill(3)
fname = ('times_' + str(ftup[0]) + '.' + str(ftup[1]) + '.plx' +
str(ftup[2]) + '.mat')
fullname = self.datadir + pdir + '/' + fname
dat = sio.loadmat(fullname)['cluster_class']
unit = dat[:,0].astype('int')
times = np.around(dat[:,1]/1000., decimals=3) #times are originally in ms
sortord = np.argsort(times) #spikes aren't always time-sorted
times = times[sortord]
unit = unit[sortord]
# now restrict to valid units:
valid = unit == ftup[3]
times = times[valid]
unit = unit[valid]
ddict = {'patient': ftup[0], 'dataset': ftup[1],
'channel': ftup[2], 'unit': unit, 'time': times}
df = pd.DataFrame(ddict)
target = 'spikes/' + make_path(*ftup)
self.write_to_db(target, df)
def import_lfp(self, ftup):
pdir = 'patient' + str(ftup[0]).zfill(3)
fname = str(ftup[0]) + '.' + str(ftup[1]) + '.plx' + str(ftup[2]) + '.mat'
fullname = self.datadir + pdir + '/' + fname
dset = h5py.File(fullname, 'r')
dat = dset['data'].value.squeeze()
sr = dset['srlfp'].value.squeeze()
vv = dat
desired_rate = 200. # desired final sampling rate (Hz)
decfrac = int(sr / desired_rate)
vv = decimate(dat, decfrac) # decimate data to 200 Hz
sr = sr / decfrac;
dt = (1. / sr).round(3)
times = (np.arange(0, vv.size) * dt).round(3).squeeze()
ddict = {'patient': ftup[0], 'dataset': ftup[1],
'channel': ftup[2], 'time': times, 'voltage': vv.values.squeeze()}
df = pd.DataFrame(ddict)
target = 'lfp/' + make_path(*ftup)
self.write_to_db(target, df)
def import_censor(self, ftup):
pdir = 'patient' + str(ftup[0]).zfill(3)
fname = (str(ftup[0]) + '.' + str(ftup[1]) + '.plx' + str(ftup[2]) +
'_censoring.mat')
fullname = self.datadir + pdir + '/' + fname
excludes = sio.loadmat(fullname)['excludes'].round(3)
if excludes.size != 0:
#do this in case some exclude ranges make no sense
badrng = np.where(np.diff(excludes, axis=1) < 0)
excludes = np.delete(excludes, badrng, axis=0)
# get data ready
ddict = {'patient': ftup[0], 'dataset': ftup[1],
'channel': ftup[2], 'start': excludes[:,0], 'stop': excludes[:,1]}
df = pd.DataFrame(ddict)
target = 'censor/' + make_path(*ftup)
self.write_to_db(target, df)
def _grab_matlab_events(self, ftup):
pdir = 'patient' + str(ftup[0]).zfill(3)
behname = self.behdir + pdir + '/' + ftup[2]
# load matlab events
matevt = sio.loadmat(behname, squeeze_me=True)['data']
# make a dataframe from matlab behavior, pull out event codes
bf = pd.DataFrame(matevt)
bf.index.names = ['trial']
evf = bf[['ev', 'evt']]
bf = bf.drop(['ev', 'evt'], axis=1)
# make sure to get data types right
bf = bf.convert_objects()
bf['result'] = bf['result'].apply(str)
absstart = bf['trial_start_time'][0]
# turn each trial into a dataframe
tlist = []
for ind in evf.index:
thistrial = {'event': evf['ev'][ind], 'time': evf['evt'][ind]}
miniframe = pd.DataFrame(thistrial,
index=pd.Index(ind * np.ones_like(evf['ev'][ind]), name='trial'))
# now take care of timestamps by putting times in seconds and adjusting
# by start time of first trial
miniframe['time'] /= 1000.
miniframe['time'] += bf['trial_start_time'][ind] - absstart
miniframe['time'] = miniframe['time'].round(3) # round to ms
tlist.append(miniframe)
# concatenate trials
events = pd.concat(tlist)
events['event'] = events['event'].apply(str)
return (bf, events)
def _grab_plexon_events(self, ftup):
pdir = 'patient' + str(ftup[0]).zfill(3)
fname = str(ftup[0]) + '.' + str(ftup[1]) + '.plx_events.mat'
fullname = self.datadir + pdir + '/' + fname
# load plexon events
evt = sio.loadmat(fullname)['evt'].squeeze()
return evt
def import_events(self, ftup):
trial_variables, events = self._grab_matlab_events(ftup)
evt = self._grab_plexon_events(ftup)
# get number of events
numtrials = max(events.index) + 1
# was this an FHC recording? if so, there are no Plexon stamps
# all events dumped into first or second slot, so other slots
# should have few timestamps
isFHC = (evt[2].size < 10)
if isFHC: # line up events with phys
# for now, we kludge this by just setting the clocks to be equal
# at task start and not worrying about drift
num_events = map(len, evt)
startcode = np.argmax(num_events) # these should be trial starts
# get time of first FHC event
FHC_start = evt[startcode][0].round(3).squeeze()
# compensate for offset
all_events = events.stack()
all_events.sort()
ephys_offset = (FHC_start - all_events.values[0]).round(3)
events['time'] += ephys_offset
else: # if we have Plexon events, use them
startcode = self.plx_codes['trial_start']
stopcode = self.plx_codes['trial_over']
# trial start -- sometimes a spurious event marks recording onset
if evt[startcode].shape[0] != numtrials:
evt[startcode] = evt[startcode][1:]
# trial stop -- when last trial aborted, may not be present
if evt[stopcode].shape[0] != numtrials:
evt[stopcode] = np.append(evt[stopcode], np.nan)
for var in self.plx_codes:
# valid = pd.notnull(events[var])
# events[var][valid] = evt[self.plx_codes[var]].round(3).squeeze()
this_selection = events['event'] == var
events['time'][this_selection] = evt[self.plx_codes[var]].round(3).squeeze()
# try to make events columns: this may fail in case some events
# can happen multiple times per trial; in that case, make each
# event a row and perform a join
if self.flatten_events:
# now merge task variables and events
df = events.join(trial_variables)
else:
# make event names column names
events = events.set_index('event', append=True).unstack()
# get rid of multi-index labeling
events.columns = pd.Index([e[1] for e in events.columns])
# now merge task variables and events
df = pd.concat([trial_variables, events], axis=1)
df['patient'] = ftup[0]
df['dataset'] = ftup[1]
# do some final tidying
df = df[df['result'] != 'aborted'] # get rid of aborts
target = 'events/' + make_path(*ftup[:-1])
self.write_to_db(target, df)
def load_all_events(self):
# get all (patient, dataset) tuples from already loaded spikes
tuplist = []
print 'Loading Events....'
with open(self.behfile) as infile:
for line in infile:
thistup = tuple(line.rstrip().lower().split(','))
tuplist.append((int(thistup[0]), int(thistup[1]), thistup[2]))
plist = [t[:-1] for t in tuplist]
self.write_to_db('meta/evlist',
pd.DataFrame(plist, columns=['patient', 'dataset']))
for ftup in tuplist:
print ftup
self.import_events(ftup)
def load_all_censoring(self):
# load censoring data
tuplist = []
print 'Loading Censoring data....'
with open(self.chanfile) as infile:
for line in infile:
tuplist.append(tuple(map(int, line.split(','))))
with open(self.lfpfile) as infile:
for line in infile:
tuplist.append(tuple(map(int, line.split(','))))
self.write_to_db('meta/censlist',
pd.DataFrame(tuplist, columns=['patient', 'dataset', 'channel']))
# iterate through files, loading data
for ftup in tuplist:
print ftup
self.import_censor(ftup)
def load_all_spikes(self):
# get list of tuples with valid channels
tuplist = []
print 'Loading Spikes....'
with open(self.spkfile) as infile:
for line in infile:
tuplist.append(tuple(map(int, line.split(','))))
self.write_to_db('meta/spklist',
pd.DataFrame(tuplist,
columns=['patient', 'dataset', 'channel', 'unit']))
# iterate through files, loading data
for ftup in tuplist:
print ftup
self.import_spikes(ftup)
def load_all_lfp(self):
# load lfp data
tuplist = []
print 'Loading LFP....'
with open(self.lfpfile) as infile:
for line in infile:
tuplist.append(tuple(map(int, line.split(','))))
self.write_to_db('meta/lfplist',
pd.DataFrame(tuplist, columns=['patient', 'dataset', 'channel']))
# iterate through files, loading data
for ftup in tuplist:
print ftup
self.import_lfp(ftup)
def load_all(self):
self.load_all_events()
self.load_all_censoring()
self.load_all_spikes()
self.load_all_lfp()