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 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 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 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 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 = 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 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 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 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
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 sweep_ext.spike_feature("width") #Extracts AP width
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 fs_axon = 1.0/2000.0 #We know there are 2000 readings per second
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() except: pass
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
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 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 sweep_duration = n_timepoints*sampling_period message = '''The data contains {n_sweeps} sweeps (i.e., trials) acquired from
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 peakdata = {'data': matrix.T}