def load_raw_binary_gain_chmap_range(rawfile, number_of_channels, hstype, nprobes=1, lraw=1, ts=0, te=25000): ''' load ecube data preprocessed and return a channels data load_a_ch(name, number_of_channels, channel_number, lraw, block_size) name - name of file number_of_channels - number of channels channel_number - channel data to return hstype : Headstage type, 'hs64' nprobes : Number of probes (default 1) lraw - whether raw file or not (default : raw lraw=1) ts : sample start (not seconds but samples), default : 0 ( begining of file) ts : sample end (not seconds but samples), default : -1 ( full data) returns time, data ''' from neuraltoolkit import ntk_channelmap as ntkc gain = np.float64(0.19073486328125) d_bgc = np.array([]) f = open(rawfile, 'rb') if lraw: tr = np.fromfile(f, dtype=np.uint64, count=1) f.seek(8, 0) else: f.seek(0, 0) if (ts == 0) and (te == -1): d = np.frombuffer(f.read(-1), dtype=np.int16) else: # check time is not negative if te-ts < 1: raise ValueError('Please recheck ts and te') f.seek(ts*2*number_of_channels, 1) block_size = (te - ts)*2*number_of_channels d = np.frombuffer(f.read(block_size), dtype=np.int16) f.close() d_bgc = np.reshape(d, [number_of_channels, np.int64(d.shape[0]/number_of_channels)], order='F') if lraw: d_bgc = ntkc.channel_map_data(d_bgc, number_of_channels, hstype, nprobes) d_bgc = np.int16(d_bgc * gain) if lraw: return tr, d_bgc else: return d_bgc
def load_raw_binary_gain_chmap_nsec(name, number_of_channels, hstype, fs, nsec, nprobes=1): ''' load ecube nsec of data and multiply gain and apply channel mapping load_raw_binary_gain_chmap_nsec(name, number_of_channels, hstype, fs, nsec): hstype : 'hs64', 'eibless-hs64_port32', 'eibless-hs64_port64', 'intan', 'Si_64_KS_chmap', 'Si_64_KT_T1_K2_chmap' and linear fs : sampling rate nsec : number of seconds nprobes : Number of probes (default 1) returns first timestamp and data tt, ddgc = ntk.load_raw_binary_gain_chmap_nsec(rawfile, number_of_channels, 'hs64', 25000, 2) ''' from neuraltoolkit import ntk_channelmap as ntkc if isinstance(hstype, str): hstype = [hstype] assert len(hstype) == nprobes, \ 'length of hstype not same as nprobes' # constants gain = np.float64(0.19073486328125) f = open(name, 'rb') tr = np.fromfile(f, dtype=np.uint64, count=1) dr = np.fromfile(f, dtype=np.int16, count=nsec * number_of_channels * fs) f.close() length = np.int64(np.size(dr) / number_of_channels) drr = np.reshape(dr, [number_of_channels, length], order='F') dg = drr * gain dgc = ntkc.channel_map_data(dg, number_of_channels, hstype, nprobes) return tr, dgc
def load_intan_raw_gain_chanmap(rawfile, number_of_channels, hstype, nprobes=1, ldin=0): ''' load intan data and multiply gain and apply channel mapping load_intan_raw_gain_chanmap(name, number_of_channels, hstype) hstype : 'hs64', 'intan32', 'Si_64_KS_chmap', 'Si_64_KT_T1_K2_chmap' and 'linear' nprobes : Number of probes (default 1) ldin : Flag for getting digital input (default 0) returns first timestamp and data ''' from neuraltoolkit import ntk_channelmap as ntkc from .load_intan_rhd_format_hlab import read_data if isinstance(hstype, str): hstype = [hstype] assert len(hstype) == nprobes, \ 'length of hstype not same as nprobes' # Read intan file a = read_data(rawfile) # Time and data tr = np.array(a['t_amplifier'][0]) dg = np.array(a['amplifier_data']) # Apply channel mapping dgc = ntkc.channel_map_data(dg, number_of_channels, hstype, nprobes) if ldin == 1: din = np.array(a['board_dig_in_data']) return np.int64(tr), np.int16(dgc), din else: return np.int64(tr), np.int16(dgc)
def load_raw_binary_gain_chmap(name, number_of_channels, hstype, nprobes=1, t_only=0): ''' load ecube data and multiply gain and apply channel mapping load_raw_binary_gain_chmap(name, number_of_channels, hstype) hstype : 'hs64', 'Si_64_KS_chmap', 'Si_64_KT_T1_K2_chmap' and linear nprobes : Number of probes (default 1) t_only : if t_only=1, just return tr, timestamp (Default 0, returns timestamp and data) returns first timestamp and data ''' from neuraltoolkit import ntk_channelmap as ntkc if isinstance(hstype, str): hstype = [hstype] assert len(hstype) == nprobes, \ 'length of hstype not same as nprobes' # constants gain = np.float64(0.19073486328125) f = open(name, 'rb') tr = np.fromfile(f, dtype=np.uint64, count=1) if t_only: f.close() return tr dr = np.fromfile(f, dtype=np.int16, count=-1) f.close() length = np.int64(np.size(dr) / number_of_channels) drr = np.reshape(dr, [number_of_channels, length], order='F') dg = drr * gain dgc = ntkc.channel_map_data(dg, number_of_channels, hstype, nprobes) return tr, dgc
def ecube_raw_to_preprocessed(rawfile, outdir, number_of_channels, hstype, nprobes=1, ts=0, te=25000, tetrode_channels=None): ''' Convert ecube_raw_to_preprocessed all channels or just a tetrode ecube_raw_to_preprocessed(rawfile, outdir, number_of_channels, hstype, nprobes=1, ts=0, te=25000, tetrode_channels=None) rawfile - name of rawfile with path, '/home/ckbn/Headstage.bin' outdir - directory to save preprocessed file, '/home/ckbn/output/' number_of_channels - number of channels in rawfile hstype : Headstage type, 'hs64' (see manual for full list) nprobes : Number of probes (default 1) ts : sample start (not seconds but samples), default : 0 ( begining of file) ts : sample end (not seconds but samples), default : -1 ( full data) tetrode_channels : default (None, all channels) to select tetrodes give channel numbers as a list Example: tetrode_channels=[4, 5, 6, 7] for second tetrode returns ''' from neuraltoolkit import ntk_channelmap as ntkc import struct gain = np.float64(0.19073486328125) d_bgc = np.array([]) # Check file exists if not (os.path.isfile(rawfile)): raise FileExistsError('Raw file not found') # Check outdir exists print("os.path.isdir(outdir) ", os.path.isdir(outdir)) if not (os.path.isdir(outdir)): raise FileExistsError('Output directory does not exists') # Get pfilename pbasename = str('P_') + os.path.splitext(os.path.basename(rawfile))[0] \ + str('.bin') print("pbasename ", pbasename) pfilename = os.path.join(outdir, pbasename) print("pfilename ", pfilename) # Read binary file and order based on channelmap f = open(rawfile, 'rb') tr = np.fromfile(f, dtype=np.uint64, count=1) print("Time ", tr) f.seek(8, 0) if (ts == 0) and (te == -1): d = np.frombuffer(f.read(-1), dtype=np.int16) else: # check time is not negative if te-ts < 1: raise ValueError('Please recheck ts and te') f.seek(ts*2*number_of_channels, 1) block_size = (te - ts)*2*number_of_channels d = np.frombuffer(f.read(block_size), dtype=np.int16) f.close() d_bgc = np.reshape(d, [number_of_channels, np.int64(d.shape[0]/number_of_channels)], order='F') d_bgc = ntkc.channel_map_data(d_bgc, number_of_channels, hstype, nprobes) d_bgc = np.int16(d_bgc * gain) if tetrode_channels: print("tetrode_channels ", tetrode_channels) d_bgc = d_bgc[tetrode_channels, :] else: print("tetrode_channels is empty saving all tetrodes") # write preprocessed file with open(pfilename, "wb") as binary_file: # Write data rawdata/digital binary_file.write(struct.pack('h'*d_bgc.size, *d_bgc.transpose().flatten()))
def load_raw_gain_chmap_1probe(rawfile, number_of_channels, hstype, nprobes=1, lraw=1, ts=0, te=-1, probenum=0, probechans=64): ''' load ecube data and return channel mapped data for single probe name - name of file number_of_channels - number of channels channel_number - channel data to return hstype : Headstage type list for example ['APT_PCB', 'APT_PCB'] nprobes=2 Headstage types are 'hs64' 'eibless-hs64_port32', 'eibless-hs64_port64', 'intan32', 'intan16test2', 'Si_64_KS_chmap', 'Si_64_KT_T1_K2_chmap' Si_64_KS_chmap includes 8-K2., 5-KS., 1A-K2. probe Si_64_KT_T1_K2_chmap includes 5-KT. and 5-K2. probe 'UCLA_Si1' 'PCB_tetrode', 'EAB50chmap_00', 'APT_PCB' and 'linear' nprobes : Number of probes (default 1) lraw - whether raw file or not (default : raw lraw=1) ts : sample start (not seconds but samples), default : 0 ( begining of file) ts : sample end (not seconds but samples), default : -1 ( full data) probenum : which probe to return (starts from zero) probechans : number of channels per probe (symmetric) returns time, data ''' from neuraltoolkit import ntk_channelmap as ntkc # import time # tic = time.time() gain = np.float64(0.19073486328125) probename = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eight", "ninth", "tenth"] # d_bgc = np.array([]) if ((number_of_channels/probechans) != nprobes): raise ValueError("number of channels/probechans != nprobes") # check probe number is in range 0 9 # max probe in a recording is limitted to 10 if ((probenum < 0) or (probenum > 9)): raise ValueError("Please check probenum {}".format(probenum)) # Print probe number of probenum is 0 to avoid confusion print("Loading data from {} probe\n".format(probename[probenum])) f = open(rawfile, 'rb') # toc = time.time() # print('ntk opening file took {} seconds'.format(toc - tic)) # tic = time.time() if lraw: tr = np.fromfile(f, dtype=np.uint64, count=1) f.seek(8, 0) else: f.seek(0, 0) if (ts == 0) and (te == -1): d_bgc = np.frombuffer(f.read(-1), dtype=np.int16) else: # check time is not negative if te-ts < 1: raise ValueError('Please recheck ts and te') f.seek(ts*2*number_of_channels, 1) block_size = (te - ts)*2*number_of_channels d_bgc = np.frombuffer(f.read(block_size), dtype=np.int16) f.close() # toc = time.time() # print('ntk reading file took {} seconds'.format(toc - tic)) # tic = time.time() d_bgc = np.reshape(d_bgc, [number_of_channels, np.int64(d_bgc.shape[0]/number_of_channels)], order='F') # toc = time.time() # print('ntk reshaping file took {} seconds'.format(toc - tic)) if lraw: # tic = time.time() d_bgc = d_bgc[((probenum)*probechans): ((probenum+1)*probechans), :] # print("d_bgc shape ", d_bgc.shape) # toc = time.time() # print('ntk probe selecting file took {} seconds'.format(toc - tic)) # tic = time.time() # d_bgc = ntkc.channel_map_data(d_bgc, number_of_channels, # hstype, nprobes) d_bgc = ntkc.channel_map_data(d_bgc, probechans, list([hstype[probenum]]), 1) # toc = time.time() # print('ntk channel mapping file took {} seconds'.format(toc - tic)) # tic = time.time() d_bgc = np.int16(d_bgc * gain) # toc = time.time() # print('ntk adding gain to file took {} seconds'.format(toc - tic)) if lraw: return tr, d_bgc else: return d_bgc