def exp_tracef(Injectcurr=150e-12): global flnme global exp_sampdur global exp_samprate global exp_samppoints global exp_trace_injend global exp_trace_injstart stim1391 = ['Cell 3 of 181016.abf', 'cell 4 of 61016.abf', 'cell 4 of 111016.abf', 'cell 4 of 131016.abf', 'Cell 4 of 181016.abf', 'cell 5 of 61016.abf', 'Cell 5 of 181016.abf'] # flnme = 'Cell 3 of 10717.abf' flnme = 'cell 4 of 61016.abf' exp_tracefile = f'../../Raw_data/Deepanjali_data/WT step input cells/{flnme}' reader = AxonIO(filename=exp_tracefile) currno = int(Injectcurr*1e12/25+4) seg = reader.read_block().segments[currno] # 10 means 150pA current exp_trace = seg.analogsignals[0] exp_samprate = float(exp_trace.sampling_rate) exp_sampdur = float(exp_trace.t_stop) - float(exp_trace.t_start) exp_samppoints = int(exp_samprate*exp_sampdur) if flnme in stim1391: exp_trace_injstart = 139.1e-3 exp_trace_injend = 639.1e-3 else: exp_trace_injstart = 81.4e-3 exp_trace_injend = 581.4e-3 exp_trace = np.array(exp_trace).flatten() return exp_trace
def convert_from_ABF_to_HDF5(experiment): """ Convierte los ficheros del experimento desde ABF y los graba en un fichero HDF5 :param experiment: :return: """ # Asumimos que el fichero no existe todavia f = experiment.open_experiment_data(mode='w') nsig = experiment.abfsensors datafiles = experiment.datafiles for dataf in datafiles: # Leemos los datos del fichero ABF print('Reading: ', dataf, '...') data = AxonIO(experiment.dpath + experiment.name + '/' + dataf + '.abf') bl = data.read_block(lazy=False, cascade=True) dim = bl.segments[0].analogsignals[0].shape[0] matrix = np.zeros((len(nsig), dim)) for i, j in enumerate(nsig): matrix[i][:] = bl.segments[0].analogsignals[j][:].magnitude # Los guardamos en el almacenamiento del experimento print('Saving: ', dataf, '...') experiment.save_raw_data(f, dataf, matrix.T) del matrix del bl datainfo.close_experiment_data(f)
def convert_from_ABF_to_HDF5(experiment): """ Convierte los ficheros del experimento desde ABF y los graba en un fichero HDF5 :param experiment: :return: """ # Asumimos que el fichero no existe todavia f = experiment.open_experiment_data(mode='w') nsig = experiment.abfsensors datafiles = experiment.datafiles for dataf in datafiles: # Leemos los datos del fichero ABF print('Reading: ', dataf, '...') data = AxonIO(experiment.dpath + experiment.name + '/' + dataf + '.abf') bl = data.read_block(lazy=False) dim = bl.segments[0].analogsignals[0].shape[0] matrix = np.zeros((len(nsig), dim)) for i, j in enumerate(nsig): tmpmat = bl.segments[0].analogsignals[j][:].magnitude matrix[i][:] = tmpmat.reshape(tmpmat.shape[0]) # Los guardamos en el almacenamiento del experimento print('Saving: ', dataf, '...') experiment.save_raw_data(f, dataf, matrix.T) del matrix del bl datainfo.close_experiment_data(f)
def setup_class(cls): TestBase.setup_class() ctx = cls.local_stack.getAuthenticatedDataStoreCoordinator().getContext() proj = ctx.insertProject('ABF import', 'ABF import', DateTime()) exp = proj.insertExperiment('ABF experiment', DateTime()) cls.device_info = {u'amplifier.mode': u'I-clamp', u'amplifier.channels.0.gain': 2.5, u'amplifier.channels.1.gain': 3.5} # TODO when we move to beta3 #exp.setEquipmentSetupFromMap(to_map(cls.device_info)) cls.src = ctx.insertSource("recording source", "source-id") abf_file = 'fixtures/example1.abf' logging.info("Importing file...") cls.epoch_group = import_file(abf_file, exp, "amplifier", [cls.src])[0] #single block reader = AxonIO(filename=abf_file) cls.block = reader.read()[0] # single block
def test_read_protocol(self): for f in self.files_to_test: filename = self.get_filename_path(f) reader = AxonIO(filename=filename) bl = reader.read_block(lazy=True) if bl.annotations['abf_version']>=2.: reader.read_protocol()
def test_read_protocol(self): for f in self.files_to_test: filename = self.get_filename_path(f) reader = AxonIO(filename=filename) bl = reader.read_block(lazy=True) if bl.annotations['abf_version'] >= 2.: reader.read_protocol()
def read_neo(path): # Read abf file with neo reader = AxonIO(filename=path) block = reader.read() # Extract analog signals if len(block[0].segments[0].analogsignals) == 1: channel1 = np.empty(block[0].segments[0].analogsignals[0].T.shape) channel1_units = block[0].segments[0].analogsignals[0].units channel2 = None channel2_units = None t = block[0].segments[0].analogsignals[0].times.magnitude for idx, seg in enumerate(block[0].segments): channel1[idx] = seg.analogsignals[0].T # # return {'file_name': block.file_origin, # 'recording_date': block.rec_datetime, # 'time': t, # 'channel1': channel1, elif len(block[0].segments[0].analogsignals) == 2: channel1 = np.empty((len(block[0].segments), block[0].segments[0].analogsignals[0].size)) channel1_units = block[0].segments[0].analogsignals[0].units channel1_sampling = block[0].segments[0].analogsignals[0].sampling_rate channel2 = np.empty((len(block[0].segments), block[0].segments[0].analogsignals[1].size)) channel2_units = block[0].segments[0].analogsignals[1].units channel2_sampling = block[0].segments[0].analogsignals[1].sampling_rate t = block[0].segments[0].analogsignals[0].times.magnitude for idx, seg in enumerate(block[0].segments): channel1[idx] = seg.analogsignals[0].T channel2[idx] = seg.analogsignals[1].T #channel1[idx] = seg.analogsignals[0] #channel2[idx] = seg.analogsignals[1] channel_1 = AnalogData( signal=channel1, time=t, units=channel1_units, sampling_rate=channel1_sampling ) channel_2 = AnalogData( signal=channel2, time=t, units=channel2_units, sampling_rate=channel2_sampling) else: raise Exception('File {0} either has too many channels, or no channels.'.format(path)) return AbfFile( [channel_1, channel_2], file_name=block[0].file_origin, date=block[0].rec_datetime)
def convert_from_ABF_to_HDF5(experiment): """ Convierte los ficheros del experimento desde ABF y los graba en un fichero HDF5 :param experiment: :return: """ # Asumimos que el fichero no existe todavia f = h5py.File( experiment.dpath + experiment.name + '/' + experiment.name + '.hdf5', 'r+') nsig = [s for s, _ in experiment.extrasensors] datafiles = experiment.datafiles for dataf in datafiles: # Leemos los datos del fichero ABF print('Reading: ', dataf, '...') data = AxonIO(experiment.dpath + experiment.name + '/' + dataf + '.abf') bl = data.read_block(lazy=False, cascade=True) dim = bl.segments[0].analogsignals[0].shape[0] matrix = np.zeros((len(nsig), dim)) for i, j in enumerate(nsig): matrix[i][:] = bl.segments[0].analogsignals[j][:].magnitude # Los guardamos en una carpeta del fichero HDF5 para el fichero dentro de /Raw print('Saving: ', dataf, '...') dgroup = f[dataf] if dataf + '/RawExtra' in f: del f[dataf + '/RawExtra'] dgroup.create_dataset('RawExtra', matrix.T.shape, dtype='f', data=matrix.T, compression='gzip') del matrix del bl f[dataf + '/RawExtra'].attrs['Sampling'] = experiment.sampling f[dataf + '/RawExtra'].attrs['Sensors'] = [ s for _, s in experiment.extrasensors ] f.flush() f.close()
def load_file(filename, zoom=[0,np.inf]): # loading the data file try: data = AxonIO(filename).read_block(lazy=False, cascade=True) dt = float(data.segments[0].analogsignals[0].sampling_period) if zoom[0]<data.segments[0].analogsignals[0].t_start: zoom[0]=data.segments[0].analogsignals[0].t_start if zoom[1]>data.segments[-1].analogsignals[0].t_stop: zoom[1]=data.segments[-1].analogsignals[0].t_stop ### ii = 0 while (ii<len(data.segments)) and (float(data.segments[min(ii,len(data.segments)-1)].analogsignals[0].t_start)<=zoom[0]): ii+=1 tt = np.array(data.segments[ii-1].analogsignals[0].times) cond = (tt>=zoom[0]) & (tt<=zoom[1]) DATA = {'t':tt[cond]} for j in range(1, len(data.segments[ii-1].analogsignals)+1): DATA['Ch'+str(j)] = np.array(data.segments[ii-1].analogsignals[j-1])[cond] ### while (ii<len(data.segments)) and ((float(data.segments[min(ii,len(data.segments)-1)].analogsignals[0].t_start)<=zoom[1])): tt = np.array(data.segments[ii].analogsignals[0].times) cond = (tt>=zoom[0]) & (tt<=zoom[1]) DATA['t'] = np.concatenate([DATA['t'],\ np.array(data.segments[ii].analogsignals[0].times)[cond]]) for j in range(1, len(data.segments[ii].analogsignals)+1): DATA['Ch'+str(j)] = np.concatenate([DATA['Ch'+str(j)],\ np.array(data.segments[ii].analogsignals[j-1])[cond]]) ii+=1 return DATA except FileNotFoundError: print('File not Found !') return {}
def list_keys(args): """ list the keys of the """ Block = AxonIO(args.filename).read_block(lazy=False, cascade=True) KEYS = [] for i in range(len(Block.segments[0].analogsignals)): print('key', i, '-->', Block.segments[0].analogsignals[i].name) KEYS.append(Block.segments[0].analogsignals[i].name) return KEYS
def translate(args, ikey, keys): data = {} # dictionary for hdf5 export Block = AxonIO(args.filename).read_block(lazy=False, cascade=True) RT = Block.rec_datetime # params = {} # parameters stored here params['day'] = ("%04d" % RT.year) + '_' + ("%02d" % RT.month) + '_' + ( "%02d" % RT.day) params['time'] = ("%02d" % RT.hour) + '_' + ("%02d" % RT.minute) + '_' + ( "%02d" % RT.second) Block = AxonIO(args.filename).read_block(lazy=False, cascade=True) params['dt'] = np.array( [Block.segments[0].analogsignals[0].sampling_period]) new_filename = args.filename.replace('.abf', '.dat') if len(Block.segments) == 1: # if only one episode, we swith to continuous args.force_continuous = True t = np.arange(len(Block.segments[0].analogsignals[0])) * params['dt'] cond = (t >= args.tstart) & (t <= args.tend) # ARRAY = [] # for s in range(len(Block.segments)): # ARRAY.append(Block.segments[s].analogsignals[ikey][cond]) X = Block.segments[0].analogsignals[ikey][cond] / quantities.mV # 'short int' (int16) has values from −32,768 to 32,767 # SWITCH TO int16 !!! # X -= X.mean() # scale = 30000/np.abs(X).max() # X2 = np.reshape(np.array(scale*X), (len(X), 1)).astype('int16') # X2.tofile(new_filename.replace('abf', 'dat')) # float32 by waiting scale = 1. X2 = np.reshape(np.array(X), (len(X), 1)).astype('float32') X2.tofile(new_filename.replace('abf', 'dat')) print('file exported as: ', new_filename) return {'facq': 1. / params['dt'], 'gain': scale}
def convert_from_ABF_to_HDF5(experiment): """ Convierte los ficheros del experimento desde ABF y los graba en un fichero HDF5 :param experiment: :return: """ # Asumimos que el fichero no existe todavia f = h5py.File(experiment.dpath + experiment.name + "/" + experiment.name + ".hdf5", "r+") nsig = [s for s, _ in experiment.extrasensors] datafiles = experiment.datafiles for dataf in datafiles: # Leemos los datos del fichero ABF print("Reading: ", dataf, "...") data = AxonIO(experiment.dpath + experiment.name + "/" + dataf + ".abf") bl = data.read_block(lazy=False, cascade=True) dim = bl.segments[0].analogsignals[0].shape[0] matrix = np.zeros((len(nsig), dim)) for i, j in enumerate(nsig): matrix[i][:] = bl.segments[0].analogsignals[j][:].magnitude # Los guardamos en una carpeta del fichero HDF5 para el fichero dentro de /Raw print("Saving: ", dataf, "...") dgroup = f[dataf] if dataf + "/RawExtra" in f: del f[dataf + "/RawExtra"] dgroup.create_dataset("RawExtra", matrix.T.shape, dtype="f", data=matrix.T, compression="gzip") del matrix del bl f[dataf + "/RawExtra"].attrs["Sampling"] = experiment.sampling f[dataf + "/RawExtra"].attrs["Sensors"] = [s for _, s in experiment.extrasensors] f.flush() f.close()
def show(args): keys = list_keys(args) Block = AxonIO(args.filename).read_block(lazy=False, cascade=True) fig, AX = plt.subplots(len(keys), figsize=(5, 3 * len(keys))) for i in range(len(keys)): if len(keys) > 1: ax = AX[i] else: ax = AX ax.set_title(keys[i]) ax.plot(Block.segments[0].analogsignals[i][:10000]) plt.show()
def exp_tracef(currno=10): global flnme global exp_trace global exp_sampdur global exp_samprate global exp_samppoints global exp_trace_injend global exp_trace_injstart stim1391 = ['Cell 3 of 181016.abf', 'cell 4 of 61016.abf', 'cell 4 of 111016.abf', 'cell 4 of 131016.abf', 'Cell 4 of 181016.abf', 'cell 5 of 61016.abf', 'Cell 5 of 181016.abf'] exp_tracefile = Exp_tracedir+flnme reader = AxonIO(filename=exp_tracefile) seg = reader.read_block().segments[currno] # 10 means 15pA current exp_trace = seg.analogsignals[0] exp_samprate = float(exp_trace.sampling_rate) exp_sampdur = float(exp_trace.t_stop) - float(exp_trace.t_start) exp_samppoints = int(exp_samprate*exp_sampdur) if flnme in stim1391: exp_trace_injstart = 139.1e-3 exp_trace_injend = 639.1e-3 else: exp_trace_injstart = 81.4e-3 exp_trace_injend = 581.4e-3 exp_trace = np.array(exp_trace).flatten()
def translate(args): keys = list_keys(args) data = {} # dictionary for hdf5 export Block = AxonIO(args.filename).read_block(lazy=False, cascade=True) RT = Block.rec_datetime # params = {} # parameters stored here params['day'] = ("%04d" % RT.year) + '_' + ("%02d" % RT.month) + '_' + ( "%02d" % RT.day) params['time'] = ("%02d" % RT.hour) + '_' + ("%02d" % RT.minute) + '_' + ( "%02d" % RT.second) params['dt'] = np.array( [Block.segments[0].analogsignals[0].sampling_period]) if not os.path.exists(params['day']): os.makedirs(params['day']) protocol = args.protocol args.filename = params['day'] + os.path.sep + params[ 'time'] + '_' + protocol + '.h5' if args.new_keys is not None: if len(args.new_keys) == len(keys): keys = args.new_keys else: print('need to give a new to all former keys (in the order)') if len(Block.segments) == 1: # if only one episode, we swith to continuous args.force_continuous = True t = np.arange(len(Block.segments[0].analogsignals[0])) * params['dt'] cond = (t >= args.tstart) & (t <= args.tend) for i in range(len(keys)): data[keys[i]] = [] for s in range(len(Block.segments)): data[keys[i]].append(Block.segments[s].analogsignals[i][cond]) if args.force_continuous: data[keys[i]] = np.array(data[keys[i]]).flatten() params[keys[i] + '_unit'] = Block.segments[s].analogsignals[i][ 0].dimensionality.string data['params'] = params data['t'] = t save_dict_to_hdf5(data, args.filename)
def get_metadata(filename): params = {'main_protocol':'undefined', 'protocol':'undefined'} # loading metadata data = AxonIO(filename).read_block(lazy=False, cascade=True) params['dt'] = float(data.segments[0].analogsignals[0].sampling_period) params['tstart'] = float(data.segments[0].analogsignals[0].times[0]) params['tstop'] = float(data.segments[0].analogsignals[0].times[-1]) params['Nepisodes'] = len(data.segments) params['Nchannels'] = len(data.segments[0].analogsignals) # prtocol name in case protocol = get_protocol_name(filename) if protocol!='': params['protocol'] = protocol params['main_protocol'] = 'classic_electrophy' return params
def load_file(filename, zoom=[0, np.inf]): # loading the data file try: data = AxonIO(filename).read_block(lazy=False) dt = float(data.segments[0].analogsignals[0].sampling_period) if zoom[0] < data.segments[0].analogsignals[0].t_start: zoom[0] = data.segments[0].analogsignals[0].t_start if zoom[1] > data.segments[-1].analogsignals[0].t_stop: zoom[1] = data.segments[-1].analogsignals[0].t_stop ### ii = 0 while (ii < len(data.segments)) and (float(data.segments[min( ii, len(data.segments) - 1)].analogsignals[0].t_start) <= zoom[0]): ii += 1 tt = np.array(data.segments[ii - 1].analogsignals[0].times) cond = (tt >= zoom[0]) & (tt <= zoom[1]) VEC = [tt[cond]] for j in range(1, len(data.segments[ii - 1].analogsignals) + 1): VEC.append( np.array(data.segments[ii - 1].analogsignals[j - 1])[cond].flatten()) ### while (ii < len(data.segments)) and ((float(data.segments[min( ii, len(data.segments) - 1)].analogsignals[0].t_start) <= zoom[1])): tt = np.array(data.segments[ii].analogsignals[0].times) cond = (tt >= zoom[0]) & (tt <= zoom[1]) VEC[0] = np.concatenate([VEC[0],\ np.array(data.segments[ii].analogsignals[0].times)[cond]]) for j in range(1, len(data.segments[ii].analogsignals) + 1): VEC[j] = np.concatenate([VEC[j],\ np.array(data.segments[ii].analogsignals[j-1])[cond].flatten()]) ii += 1 return VEC[0], VEC[1:] except FileNotFoundError: print('File not Found !') return [[], []]
''' max_values = [] for s_n in range(len(list_of_segments)): max_y = -100000 for t_p in range(len(list_of_segments[s_n].analogsignals[0])): if list_of_segments[s_n].analogsignals[0][t_p] > max_y: max_y = list_of_segments[s_n].analogsignals[0][t_p] t_p = t_p + 1 max_values.append(max_y) #print('The length of the seg is ' + str(len(segs[s_n].analogsignals[0])) + ' for current signal') #print('The max current is ' + str(max_y)) return max_values abf_file_name = './data/input/18227014.abf' abf = AxonIO(abf_file_name) #print file info print(abf.read_protocol) #count segments n_seg = abf.segment_count(block_index=0) print('Found ' + str(n_seg) + ' segments.') # Generate an array containing segments segs = get_segments(abf) print(segs[15]) # Visually check the values of the analog signal # in seg[15]. print(segs[15].analogsignals[0]) a_seg_1 = np.array(segs[15].analogsignals[0])
from config.paths import cinvesdata, cinvesdatanew datafiles = [(['15514028', '15514029', '15514030', '15514031', '15514032', '15514033', '15514034', '15514035', '15514036', '15514037', '15514038'], 12, 10000.0) ] # datafiles = [(['15514027'], # 12, 10000.0)] for dataf, nsig, _ in datafiles: for files in dataf: print 'Reading: ', files, '...' data = AxonIO(cinvesdatanew + 'e150514/' + files + '.abf') bl = data.read_block(lazy=False, cascade=True) dim = bl.segments[0].analogsignals[0].shape[0] print dim matrix = np.zeros((nsig, dim)) # for sig in bl.segments[0].analogsignals: # i += 1 # #print sig.duration, sig.signal, len(sig.times), i # print sig.name, sig.units, sig.sampling_rate, sig.dtype, sig.duration, sig.shape for j in range(nsig): matrix[j][:] = bl.segments[0].analogsignals[j][:].magnitude
def exp_tracef(Injectcurr=150e-12): global flnme global exp_sampdur global exp_samprate global exp_samppoints global exp_trace_injend global exp_trace_injstart global Vrest global sm_diam global sm_len global Gin stim1391 = [ 'Cell 3 of 181016.abf', 'cell 4 of 61016.abf', 'cell 4 of 111016.abf', 'cell 4 of 131016.abf', 'Cell 4 of 181016.abf', 'cell 5 of 61016.abf', 'Cell 5 of 181016.abf' ] # flnme = 'Cell 3 of 10717.abf' flnme = 'cell 4 of 61016.abf' exp_tracefile = f'../../Raw_data/Deepanjali_data/WT step input cells/{flnme}' reader = AxonIO(filename=exp_tracefile) currno = int(Injectcurr * 1e12 / 25 + 4) seg = reader.read_block().segments[currno] # 10 means 150pA current exp_trace = seg.analogsignals[0] exp_samprate = float(exp_trace.sampling_rate) exp_sampdur = float(exp_trace.t_stop) - float(exp_trace.t_start) exp_samppoints = int(exp_samprate * exp_sampdur) if flnme in stim1391: exp_trace_injstart = 139.1e-3 exp_trace_injend = 639.1e-3 else: exp_trace_injstart = 81.4e-3 exp_trace_injend = 581.4e-3 exp_trace = np.array(exp_trace).flatten() * 1e-3 Vrest = np.mean( np.array( reader.read_block().segments[4].analogsignals[0]).flatten()) * 1e-3 Rinp25 = np.abs( np.max( np.array(reader.read_block().segments[5].analogsignals[0]).flatten( )) * 1e-3 - Vrest) / 25e-12 Rinn25 = np.abs( np.min( np.array(reader.read_block().segments[3].analogsignals[0]).flatten( )) * 1e-3 - Vrest) / 25e-12 Gin = 2 / (Rinp25 + Rinn25) print(Gin) t = np.linspace(0, exp_sampdur, int(exp_sampdur * exp_samprate)) Vtracep25 = np.array( reader.read_block().segments[5].analogsignals[0]).flatten() * 1e-3 Vtracen25 = np.array( reader.read_block().segments[3].analogsignals[0]).flatten() * 1e-3 str_ind = (np.abs(t - exp_trace_injend + 100e-3)).argmin() stp_ind = (np.abs(t - exp_trace_injend)).argmin() Vtracep25_choppped = Vtracep25[:stp_ind] Vtracen25_choppped = Vtracen25[:stp_ind] vp63 = np.abs( np.max( np.array(reader.read_block().segments[5].analogsignals[0]).flatten( )) * 1e-3 - Vrest) * 0.63 + Vrest vn63 = -np.abs( np.min( np.array(reader.read_block().segments[3].analogsignals[0]).flatten( )) * 1e-3 - Vrest) * 0.63 + Vrest tau = (t[(np.abs(Vtracep25_choppped - vp63)).argmin()] - exp_trace_injstart + t[(np.abs(Vtracen25_choppped - vn63)).argmin()] - exp_trace_injstart) / 2 tauinv = (t[len(Vtracep25_choppped) - (np.abs(Vtracep25_choppped[stp_ind::-1] - vp63)).argmin()] - exp_trace_injstart + t[len(Vtracep25_choppped) - (np.abs(Vtracep25_choppped[::-1] - vp63)).argmin()] - exp_trace_injstart) / 2 Cm = (tau + tauinv) * Gin / 2 print(Cm) sm_len = np.sqrt(Cm / CM / np.pi) sm_diam = sm_len return exp_trace
import matplotlib.pyplot as plt from neo.io import AxonIO import os from allensdk.ephys.ephys_extractor import EphysSweepFeatureExtractor from allensdk.core.cell_types_cache import CellTypesCache filename = 'Cell 6 of 171117.abf' seg_no = 17 #0 is -100pA, 4 is 0pA, 20 is 400pA. Now extrapolate stim_start = 81.4e-3 #in s stim_stop = 581.4e-3 #in s Actualstim_start = seg_no * 2 + stim_start Actualstim_stop = seg_no * 2 + stim_stop Inputcurr = seg_no * 25 - 100 #in pA reader = AxonIO(filename='Deepanjali data/WT step input cells/' + filename) Vtrace = reader.read_block().segments[seg_no].analogsignals[0] i = np.zeros(int((Vtrace.t_stop - Vtrace.t_start) * Vtrace.sampling_rate)) i[int(stim_start * Vtrace.sampling_rate):int(stim_stop * Vtrace.sampling_rate)] = Inputcurr i = np.array(i) v = np.array([float(V) for V in Vtrace]) t = np.linspace(0, float(Vtrace.t_stop - Vtrace.t_start), int((Vtrace.t_stop - Vtrace.t_start) * Vtrace.sampling_rate)) t = np.array(t) sweep_ext = EphysSweepFeatureExtractor(t=t, v=v, i=i, filter=5) sweep_ext.process_spikes() sweep_ext.spike_feature_keys() #Lists all the features that can be extracted
import numpy as np import matplotlib.pyplot as plt import glob #change directory as needed flydir = 'C:\\Users\\rancher\\Desktop\\S328_.5stim\\' from neo.io import AxonIO abf_name_list = glob.glob(flydir + '*.abf') for i in abf_name_list: abf_name = i.replace('.abf','') abf_name = abf_name.replace(flydir,'') localfile = flydir + abf_name + '.abf' #change file name as needed r = AxonIO(localfile) bl = r.read_block(lazy=False, cascade=True) # Get info from channels: l_amp = np.asarray(bl.segments[0].analogsignals[3])*60/np.pi #Left wing from Kinefly r_amp = np.asarray(bl.segments[0].analogsignals[4])*60/np.pi #Right wing from Kinefly wbf = np.asarray(bl.segments[0].analogsignals[0])*100. #wing beat frequency from wing beat analyzer # l_plus_r = np.asarray(bl.segments[0].analogsignals[1])*60/np.pi #from wing beat analyzer pattern = np.asarray(bl.segments[0].analogsignals[7]) #frames = np.asarray(bl.segments[0].analogsignals[3]) #amp_sum = l_amp + r_amp #use this line if we use Channel 1 l_plus_r from wing beat analyzer again l_plus_r = l_amp + r_amp amp_diff = l_amp - r_amp # Note: amp_diff is L-R
def binary_to_csv(filename=None): ''' Writes data for given abf file into a csv format. Two .csv files will be created into csv_data folder One will be meta_data, one will be time series data. These sheets are also returned as pandas data frames by this function ''' if filename is None: sys.exit('must specify filename and path') # Set up path to save csv output to pathtocsv = str.split(filename, '/') pathtocsv = "/".join(pathtocsv[:-2]) + "/csv_data/" #Use neo function "AxonIO" to read in the axonb binary file r = AxonIO(filename=filename) r = r.read_block() # for name purposes later on, remove ".abf" extension from filename filename = splitext(basename(filename))[0] # Define number of sweeps in this protocol (.abf file) n_sweeps = len(r.segments) n_channels = len(r.segments[0].analogsignals) # Name sweeps sweep_ids = [] for i in range(0, n_sweeps): sweep_ids.append('sweep' + str(i + 1)) # List the traits to hold in meta data meta_traits = ['fs', 'celltype', 'date'] for i in range(0, n_channels): meta_traits.append('ch' + str(i + 1) + '_units') # create empty data frame for meta data meta_data = pd.DataFrame(columns=meta_traits, index=sweep_ids) # fill meta_data for i, seg in enumerate(r.segments): for ch in range(0, n_channels): meta_data['ch' + str(ch + 1) + '_units'][sweep_ids[i]] = str( seg.analogsignals[ch].units)[4:] meta_data['fs'][sweep_ids[i]] = float( seg.analogsignals[0].sampling_rate) meta_data['date'][sweep_ids[i]] = filename[-10:] meta_data['celltype'][sweep_ids[i]] = str.split( filename, '_')[0] # example of using string split to get information # create empty data frame to hold data data_cols = [] for i in range(0, n_sweeps): for ch in range(0, n_channels): data_cols.append('ch' + str(ch + 1) + '_sweep' + str(i + 1)) # Warning about how we are handling time series import warnings #warnings.warn('If fs is same for all sweeps, hard coding time series to be the same for all sweeps within an .abf file') # make sure all sampling rates are the same across segments if len(meta_data['fs'].unique()) == 1: time = np.linspace( 0, len(r.segments[0].analogsignals[0]) / float(meta_data['fs'].unique()), len(r.segments[0].analogsignals[0])) else: sys.exit( 'sampling rates are different - need to figure out what to do in this case' ) # initiate the data frame for data data = pd.DataFrame(index=time, columns=data_cols) # fill the data frame for data for s_index, seg in enumerate(r.segments): for ch_index, ch in enumerate(seg.analogsignals): data['ch' + str(ch_index + 1) + '_sweep' + str(s_index + 1)] = ch # Convert the data frames to .csv files meta_data.to_csv(pathtocsv + 'meta_data_' + str(filename) + '.csv') data.to_csv(pathtocsv + 'data_' + str(filename) + '.csv') print('saving data in csv format. Stored in csv_data folder') # return data frames for further processing return meta_data, data
from neo.io import AxonIO import os from tkinter.filedialog import askdirectory Direc = '../../Raw_data/Deepanjali_data/WT step input cells/' #Directory where all the files are stored CurrAmp = 5 #0 is for -100pA, 1 for -75pA, and so on till 20 which is 400pA. 10 for 150pA # flist = os.listdir(Direc) flist = ['cell 4 of 61016.abf'] i = 0 print(os.listdir(Direc)) for filename in flist: i = i + 1 print(i, end='\r') try: #Try is used to skip unsupported files that may be there in the folder reader = AxonIO(filename=Direc + filename) Samprate = reader.get_signal_sampling_rate() seg = reader.read_block().segments[CurrAmp] Tdur = np.array(seg.t_stop - seg.t_start) plt.plot(np.linspace(0, Tdur, Samprate * Tdur), seg.analogsignals[0], label=f'{filename}') # plt.title('WT 150pA current injection for 0.5s') # plt.xlim([0,1]) plt.ylim([-75, 50]) plt.xlabel('Time (s)') plt.ylabel('Membrane potential (mV)') plt.legend() mng = plt.get_current_fig_manager() mng.resize(*mng.window.maxsize()) plt.show()
params = {'main_protocol':'undefined', 'protocol':'undefined'} # loading metadata data = AxonIO(filename).read_block(lazy=False, cascade=True) params['dt'] = float(data.segments[0].analogsignals[0].sampling_period) params['tstart'] = float(data.segments[0].analogsignals[0].times[0]) params['tstop'] = float(data.segments[0].analogsignals[0].times[-1]) params['Nepisodes'] = len(data.segments) params['Nchannels'] = len(data.segments[0].analogsignals) # prtocol name in case protocol = get_protocol_name(filename) if protocol!='': params['protocol'] = protocol params['main_protocol'] = 'classic_electrophy' return params if __name__ == '__main__': import sys import matplotlib.pylab as plt filename = sys.argv[-1] data = AxonIO(filename).read_block(lazy=False, cascade=True) print(get_metadata(filename)) # data = load_file(filename, zoom=[-5.,np.inf]) # # for i in range(10): # # plt.plot(t, data[0][i]) # plt.plot(data['t'], data['Ch1']) # plt.show()
def _read_data_from_file(self, file_name): return AxonIO(file_name).read()[0].segments
def test_annotations(self): reader = AxonIO(filename=self.get_filename_path('File_axon_2.abf')) bl = reader.read_block() ev = bl.segments[0].events[0] assert 'comments' in ev.annotations
Sno = 0 features_pd = pd.DataFrame() for filename in os.listdir(foldername): stim1391 = ['Cell 3 of 181016.abf', 'cell 4 of 61016.abf', 'cell 4 of 111016.abf', 'cell 4 of 131016.abf', 'Cell 4 of 181016.abf', 'cell 5 of 61016.abf', 'Cell 5 of 181016.abf', 'Cell 2 of 19_10_2016', 'Cell 1 of 27_10_2016.abf', 'Cell 1 of 14_10_2016.abf', 'Cell 4 of 7_10_2016.abf', 'Cell 6 of 12_10_2016.abf', 'Cell 7 of 12_10_2016.abf'] if filename in stim1391: stim_start = 139.1e-3 stim_end = 639.1e-3 else: stim_start = 81.4e-3 #in s stim_end = 581.4e-3 #in s if filename in fskip: print(f'{filename} skipped') continue if filename[-4:] == '.abf': reader = AxonIO(filename='Deepanjali data/WT step input cells/'+filename) else: continue features = singlefeature(seg_nol,reader,filename,stim_start,stim_end) features_pd = features_pd.append(pd.DataFrame(features,index = [Sno])) Sno = Sno +1 print(Sno) print(features['Cm']) print(features['Rinput']) # features_pd.to_csv('Deepanjali data/WT step input cells/features.csv', sep='\t')
""" from neo.io import AxonIO import pandas as pd import matplotlib.pyplot as plt from scipy import signal import pylab as pl import numpy as np ''' Preliminary steps to read in the data. ''' # I commonly use the variable 'fh' as shorthand for 'file handle'. In # programming parlance, 'handle' is an abstract reference to a resource (in this # case, the AxonIO instance that allows you to read from the file). fh = AxonIO(filename='PVcell3.abf') # The block is a container for the voltage and current data stored in the file. block = fh.read_block() # Some information regarding the experiment has been saved in the file. We need # to parse this information out for the analysis. A block consists of multiple # sweeps (i.e., ,trials), known as `segments`. Each segment can contain multiple # signals, known as `analogsignals`. For this particular data file, the number # of channels, number of timepoints, sampling rate and sampling period are # identical across all sweeps. n_channels = len(block.segments[0].analogsignals) n_sweeps = len(block.segments) n_timepoints = len(block.segments[0].analogsignals[0].times) sampling_rate = int(block.segments[0].analogsignals[0].sampling_rate) sampling_period = 1.0/sampling_rate
""" Crash course on using NeoIO to display info about ABF files """ from imports import * from neo.io import AxonIO import pprint pp = pprint.PrettyPrinter(indent=4) if __name__ == "__main__": fname = PATH_DATA + "/18702001-biphasicTrain.abf" reader = AxonIO(fname) headerText = pprint.pformat(reader._axon_info) out = "# AxonIO()._axon_info\n" out += f"\n```\n{headerText}\n```\n" with open(__file__ + ".md", 'w') as f: f.write(out) print("DONE")
from bokeh.io import output_file, show from bokeh.layouts import widgetbox, row from bokeh.models import CustomJS, ColumnDataSource, Span from bokeh.models.widgets import Button, RadioButtonGroup, Select, Slider, RangeSlider from bokeh.plotting import figure from bokeh.layouts import layout import numpy as np from neo.io import AxonIO # Set output output_file("dashboard.html") # Read data reader = AxonIO('/home/matt/Downloads/2017_02_23_0110.abf') block = reader.read() ch1_data = [] ch2_data = [] for idx, seg in enumerate(block[0].segments): ch1_data.append(seg.analogsignals[0]) ch2_data.append(seg.analogsignals[1]) ch1_data = np.array(ch1_data).reshape(10,100000) ch2_data = np.array(ch2_data).reshape(10,100000) # Plot data source = ColumnDataSource(data=dict( x=np.arange(0, len(ch2_data[0])), y=ch2_data[0], main=ch2_data[0] ) )