-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.py
286 lines (209 loc) · 10.9 KB
/
cat.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
#Animal class and subclasses
#Code: Cat
from analysis import *
import os
import glob
import numpy as np
import struct
import string, re
import scipy
import tifffile as tiff
import cPickle as pickle
import gc
#from skimage.measure import block_reduce
import shutil
from load_intan_rhd_format import *
class Cat(object):
def __init__(self, animal_name, home_dir):
self.name = animal_name
self.home_dir = home_dir
self.load_filenames()
#self.probe = Probe() #Load intan probe map;
def load_filenames(self):
print "...loading filenames..."
self.filenames = glob.glob(self.home_dir+self.name+'*.tsf') #use .tif extension otherwise will load .npy files
for f in range(len(self.filenames)):
self.filenames[f] = self.filenames[f][:-4]
#self.tsf_filenames = glob.glob( self.home_dir+self.name+'/tsf_files/*.tsf') #use .tif extension otherwise will load .npy files
def load_tsf_header(self, file_name):
self.tsf = Tsf_file(file_name)
def load_tsf(self, file_name):
self.tsf = Tsf_file(file_name)
self.tsf.read_ec_traces()
def load_channel(self, file_name, channel):
self.tsf = Tsf_file(file_name)
self.tsf.file_name = file_name
self.tsf.read_trace(channel)
def load_lfp_channel(self, file_name, channel): #Nick/Martin data has different LFP structure to their data.
class Object_empty(object):
def __init__(self):
pass
self.tsf = Object_empty()
self.tsf.file_name = file_name
data_in = np.load(file_name)
self.tsf.SampleFrequency = data_in['tres']
print "freq: ", self.tsf.SampleFrequency
self.tsf.ec_traces = data_in['data']
self.tsf.ec_traces = self.tsf.ec_traces * data_in['uVperAD']
print self.tsf.ec_traces.shape
print self.tsf.ec_traces[0]
self.tsf.ec_traces = self.tsf.ec_traces[channel]
#Home made notch filter; filter.notch doesn't seem to work...
notched = np.int16(butter_bandpass_filter(self.tsf.ec_traces, 59.75, 60.25, self.tsf.SampleFrequency, order = 2))
self.tsf.ec_traces = self.tsf.ec_traces-notched
#compute LFP offset relative high-pass and pad LFP record.
print "offseting lfp data to highpass"
self.tsf.ec_traces = np.append(np.zeros(int(data_in['t0']*1E-3), dtype=np.int16), self.tsf.ec_traces)
#self.tsf.ec_traces = np.concatenate((start_offset, self.tsf.ec_traces), axis=0)
def load_lfp_all(self, file_name): #Nick/Martin data has different LFP structure to their data.
class Object_empty(object):
def __init__(self):
pass
self.tsf = Object_empty()
self.tsf.file_name = file_name
data_in = np.load(file_name)
self.tsf.SampleFrequency = data_in['tres']
self.tsf.chans = data_in['chans']
self.tsf.n_electrodes = len(self.tsf.chans)
self.tsf.Siteloc = data_in['chanpos']
self.tsf.vscale_HP = data_in['uVperAD']
self.tsf.ec_traces = data_in['data']
self.tsf.ec_traces = self.tsf.ec_traces
#Home made notch filter; filter.notch doesn't seem to work...
offset = np.zeros(int(data_in['t0']*1E-3), dtype=np.int16) #Convert microsecond offset to miliseconds;
temp_traces = []
for k in range(self.tsf.n_electrodes):
self.tsf.ec_traces[k] = Notch_Filter(self.tsf.ec_traces[k])
temp_traces.append(np.append(offset, self.tsf.ec_traces[k]))
self.tsf.ec_traces = np.int16(temp_traces)
self.tsf.n_vd_samples = len(self.tsf.ec_traces[0])
def tsf_to_lfp(self):
'''Read .tsf files - subsample to 1Khz, save as *_lp.tsf
'''
print "...making low-pass tsf files (1Khz sample rates)..."
for file_name in self.filenames:
file_out = file_name[:file_name.find('rhd_files')]+'tsf_files/'+ file_name[file_name.find('rhd_files')+10:]+'_lp.tsf'
if os.path.exists(file_out)==True: continue
file_in = file_name[:file_name.find('rhd_files')]+'tsf_files/'+ file_name[file_name.find('rhd_files')+10:]+'_raw.tsf'
print "Processing: \n", file_in
self.load_tsf(file_in)
#self.load_channel(file_in, 0)
print self.tsf.Siteloc.shape
n_vd_samples = len(self.tsf.ec_traces[0]); print "Number of samples: ", n_vd_samples
print "...converting raw to .lfp (1Khz) sample rate tsf files ..."
temp_array=[]
lowcut = 0.1; highcut=110; fs=1000
#import matplotlib.pyplot as plt
#lowcut = 5; highcut=240; fs=1000 #Use 5Hz low cutoff to reduce slower oscillations for spike sorting;
for k in range(len(self.tsf.ec_traces)): #Subsample to 1Khz and notch filter
print "ch: ", k
temp = np.array(butter_bandpass_filter(self.tsf.ec_traces[k][::int(self.tsf.SampleFrequency/1000)], lowcut, highcut, fs, order = 2), dtype=np.int16)
#Home made notch filter; filter.notch doesn't seem to work...
notch = np.array(butter_bandpass_filter(temp, 59.9, 60.1, fs, order = 2), dtype=np.int16)
temp = temp-notch
temp_array.append(temp)
ec_traces = np.int16(temp_array)
print ec_traces.shape
#SAVE RAW DATA ******NB: SHOULD CLEAN THIS UP: the write function should be shared by all, just data is changing so no need to repeat;
print "Writing raw data ..."
fout = open(file_out, 'wb')
fout.write(self.tsf.header)
fout.write(struct.pack('i', 1002))
fout.write(struct.pack('i', 1000))
fout.write(struct.pack('i', self.tsf.n_electrodes))
fout.write(struct.pack('i', len(ec_traces[0])))
fout.write(struct.pack('f', self.tsf.vscale_HP))
for i in range (self.tsf.n_electrodes):
fout.write(struct.pack('h', self.tsf.Siteloc[i*2]))
fout.write(struct.pack('h', self.tsf.Siteloc[i*2+1]))
fout.write(struct.pack('i', i+1))
for i in range(self.tsf.n_electrodes):
print i,
ec_traces[i].tofile(fout) #Frontside
fout.write(struct.pack('i', self.tsf.n_cell_spikes))
fout.close()
#quit()
def lfp_compress(self):
print "...making compressed lfp files ..."
for file_name in self.filenames:
file_out = file_name[:file_name.find('rhd_files')]+'tsf_files/'+ file_name[file_name.find('rhd_files')+10:]+'_lp_compressed.tsf'
if os.path.exists(file_out)==True: continue
#Load low-pass .tsf file
file_in = file_name[:file_name.find('rhd_files')]+'tsf_files/'+ file_name[file_name.find('rhd_files')+10:]+'_lp.tsf'
print "Processing: \n", file_in
self.load_tsf(file_in)
print self.tsf.Siteloc.shape
#Save .lfp.zip file; Martin format; still used by some routines (may wish to remove eventually)
out_file = file_name[:file_name.find('rhd_files')]+'tsf_files/'+ file_name[file_name.find('rhd_files')+10:]+'.lfp.zip'
print "Saving LFP : ", out_file
t0 = 0
t1 = len(self.tsf.ec_traces[0])*1E6/self.tsf.SampleFrequency #time of end of file in usec
tres = 1000 #1Khz sample rate
uVperAD = 1.0
chans = np.arange(0, self.tsf.n_electrodes, 1)
chanpos = self.tsf.Siteloc
np.savez(out_file, chans=chans, chanpos=chanpos, data=self.tsf.ec_traces, t0=t0, t1=t1, tres=tres, uVperAD=uVperAD)
os.rename(out_file+'.npz', out_file)
#COMPRESS LFP
#PAD DATA - Required for Nick/Martin recs as LFP starts bit after highpass data;
#print "Loading LFP (.lfp.zip) file: ", file_lfp
#data_in = np.load(file_lfp+'.lfp.zip')
#t_start = data_in['t0']*1E-6 #Convert to seconds
#t_end = data_in['t1']*1E-6 #Convert to seconds
#start_offset = np.zeros((len(data_in['data']), int(t_start*1E3)), dtype=np.int16) #make padding at front of data
#data_out = np.concatenate((start_offset, data_out), axis=1)
#*********** SAVE COMPRESSED LOW PASS .TSF FILE *********
header = 'Test spike file '
iformat = 1002
n_electrodes = self.tsf.n_electrodes
SampleFrequency = 50000
vscale_HP = self.tsf.vscale_HP #use the same as above
#n_vd_samples = len(self.tsf.ec_traces[0][::Compress_factor])
n_vd_samples = len(self.tsf.ec_traces[0])
f1 = open(file_out, 'wb')
f1.write(header)
f1.write(struct.pack('i', iformat))
f1.write(struct.pack('i', SampleFrequency))
f1.write(struct.pack('i', n_electrodes))
f1.write(struct.pack('i', n_vd_samples))
f1.write(struct.pack('f', vscale_HP))
for i in range (n_electrodes):
f1.write(struct.pack('h', self.tsf.Siteloc[i*2]))
f1.write(struct.pack('h', self.tsf.Siteloc[i*2+1]))
f1.write(struct.pack('i', i+1)) #Need to add extra value for Fortran arrays
print "Writing data"
for i in range(n_electrodes):
self.tsf.ec_traces[i].tofile(f1)
f1.write(struct.pack('i', 0)) #Write # of fake spikes
#text = "Compression:" + str(overall_compression)
#n_bytes = len(text)
#f1.write(struct.pack('i', n_bytes))
#f1.write(text)
f1.close()
def write_tsf(self):
pass
class Probe(object):
def __init__(self):
print "...loading old cat probes..."
print "...TODO..."
#NB********** ALREADY HAVE PROBE CLASS IN MOUSE... NEED TO HAVE DIFFERENT NAMES HERE OR PUT CLASS INSIDE ANALYSIS.
#self.name = "NeuroNexus 64Ch probe" #Hardwired, but should add options here...
#self.load_layout()
def load_layout(self):
''' Load intan probe map layout
'''
pass
def find_previous(self, array, value):
temp = (np.abs(array-value)).argmin()
if array[temp]>value: return temp-1
else: return temp
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = filtfilt(b, a, data)
return y