def ve_tau(specimen_id, ve_path): #print(chr(27) + "[2J") # To clear terminal screen print "START VE_TAU " + str(specimen_id) + " " + str(ve_path) expt_taus = [] data_set = NwbDataSet(ve_path) long_square_sweeps = lims_utils.get_sweeps_of_type("C1LSCOARSE", specimen_id, passed_only=True) print "ve specimen id= " + str(specimen_id) for sweep in long_square_sweeps: #print "ve_sweep_number: " + str(sweep) #print(data_set.get_sweep_metadata(sweep)) try: (data_set.get_sweep_metadata(sweep)["aibs_stimulus_amplitude_pa"]) except: continue else: if (data_set.get_sweep_metadata(sweep) ["aibs_stimulus_amplitude_pa"] < 0): v, i, t = lims_utils.get_sweep_v_i_t_from_set(data_set, sweep) sweep_feat = EphysSweepFeatureExtractor( t, v) # Get time and voltage of each hyperpolarizing sweep if np.isnan(sweep_feat): continue else: expt_taus.append(sweep_feat.estimate_time_constant( )) # Append time constant of each sweep to list mean_expt_tau = np.nanmean(expt_taus) # Mean time constant for this cell print "mean_ve_tau= " + str(mean_expt_tau) return mean_expt_tau
def from_electrophysiology( cell_id: int, ephys: NwbDataSet, duration=2.0) -> 'ProcessedAllenNeuronElectrophysiology': current_list = [] voltage_list = [] time_list = [] stim_amp_list = [] n_spikes_list = [] spike_features_list = [] for sweep_number in ephys.get_sweep_numbers(): sweep_metadata = ephys.get_sweep_metadata(sweep_number) if sweep_metadata['aibs_stimulus_name'] == 'Long Square': sweep_data = ephys.get_sweep(sweep_number) amp = sweep_metadata['aibs_stimulus_amplitude_pa'] index_range = sweep_data["index_range"] sampling_rate = sweep_data["sampling_rate"] current = sweep_data["stimulus"][ index_range[0]:index_range[1] + 1] voltage = sweep_data["response"][ index_range[0]:index_range[1] + 1] # truncate max_frames = int(duration * sampling_rate) assert max_frames < len(voltage) current = current[:max_frames] * 1e12 # in pA voltage = voltage[:max_frames] * 1e3 # in mV # extract featrures time = np.arange(0, max_frames, dtype=np.float) / sampling_rate # in seconds ext = EphysSweepFeatureExtractor(t=time, v=voltage, i=current) ext.process_spikes() spike_features = ext.spikes() n_spikes = len(spike_features) current_list.append(current) voltage_list.append(voltage) time_list.append(time) stim_amp_list.append(amp) n_spikes_list.append(n_spikes) spike_features_list.append(spike_features) return ProcessedAllenNeuronElectrophysiology( cell_id=cell_id, current_list=current_list, voltage_list=voltage_list, time_list=time_list, stim_amp_list=stim_amp_list, n_spikes_list=n_spikes_list, spike_features_list=spike_features_list)
def save_cell_data_web(self, acceptable_stimtypes, non_standard_nwb=False, ephys_dir='preprocessed', **kwargs): bpopt_stimtype_map = utility.bpopt_stimtype_map distinct_id_map = utility.aibs_stimname_map nwb_file = NwbDataSet(self.nwb_path) stim_map = defaultdict(list) stim_sweep_map = {} output_dir = os.path.join(os.getcwd(), ephys_dir) utility.create_dirpath(output_dir) sweep_numbers = kwargs.get('sweep_numbers') or nwb_file.get_sweep_numbers() for sweep_number in sweep_numbers: sweep_data = nwb_file.get_sweep_metadata(sweep_number) stim_type = sweep_data['aibs_stimulus_name'] try: stim_type = stim_type.decode('UTF-8') except: pass if stim_type in acceptable_stimtypes: sweep = nwb_file.get_sweep(sweep_number) start_idx, stop_idx = sweep['index_range'] stimulus_trace = sweep['stimulus'][start_idx:stop_idx] response_trace = sweep['response'][start_idx:stop_idx] sampling_rate = sweep['sampling_rate'] time = np.arange(0, len(stimulus_trace)) / sampling_rate trace_name = '%s_%d' % ( distinct_id_map[stim_type], sweep_number) if non_standard_nwb: calc_stimparams_func = self.calc_stimparams_nonstandard else: calc_stimparams_func = self.calc_stimparams stim_start, stim_stop, stim_amp_start, stim_amp_end, \ tot_duration, hold_curr = calc_stimparams_func( time, stimulus_trace, trace_name) response_trace_short_filename = '%s.%s' % (trace_name, 'txt') response_trace_filename = os.path.join( output_dir, response_trace_short_filename) time *= 1e3 # in ms response_trace *= 1e3 # in mV response_trace = utility.correct_junction_potential(response_trace, self.junction_potential) stimulus_trace *= 1e9 # downsampling time, stimulus_trace, response_trace = utility.downsample_ephys_data( time, stimulus_trace, response_trace) if stim_type in utility.bpopt_current_play_stimtypes: with open(response_trace_filename, 'wb') as response_trace_file: np.savetxt(response_trace_file, np.transpose([time, response_trace, stimulus_trace])) else: with open(response_trace_filename, 'wb') as response_trace_file: np.savetxt(response_trace_file, np.transpose([time, response_trace])) holding_current = hold_curr # sweep['bias_current'] stim_map[distinct_id_map[stim_type]].append([ trace_name, bpopt_stimtype_map[stim_type], holding_current/1e12, stim_amp_start / 1e12, stim_amp_end/1e12, stim_start * 1e3, stim_stop * 1e3, tot_duration * 1e3, response_trace_short_filename]) stim_sweep_map[trace_name] = sweep_number logger.debug('Writing stimmap.csv ...') stim_reps_sweep_map, stimmap_filename = self.write_stimmap_csv(stim_map, output_dir, stim_sweep_map) self.write_provenance( output_dir, self.nwb_path, stim_sweep_map, stim_reps_sweep_map) return output_dir, stimmap_filename
# stimulus is a numpy array in amps stimulus = sweep_data['stimulus'][index_range[0]:index_range[-1]] # response is a numpy array in volts response = sweep_data['response'][index_range[0]:index_range[-1]] * 1000 subset[sweep_number] = response # sampling rate is in Hz sampling_rate = sweep_data['sampling_rate'] # define some time points in seconds (i.e., convert to absolute time) time_pts = np.arange(0, len(stimulus) / sampling_rate, 1. / sampling_rate) subset['t'] = time_pts metadata = data_set.get_sweep_metadata(sweep_number) ampl = round(metadata['aibs_stimulus_amplitude_pa'], 4) # plot the stimulus and the voltage response for the random trial plt.subplot(2, 1, 1) plt.plot(time_pts, stimulus) plt.ylabel('Stimulus (A)') plt.subplot(2, 1, 2) plt.plot(time_pts, response, label='S %s, %s pA' % (sweep_number, ampl)) volts_file = open('%s.dat' % dataset_id, 'w') max = 1.5 # s for i in range(len(subset['t'])):
sweep_numbers.sort() print("All sweeps for %s: %s" % (dataset_id, sweep_numbers)) subthreshs = {} spikings = {} spike_count = {} chosen = {} stimuli = {} for sweep_number in sweep_numbers: sweep_data = data_set.get_sweep(sweep_number) if data_set.get_sweep_metadata( sweep_number)['aibs_stimulus_name'] == "Long Square": sweep_info = {} sweep_info[DH.METADATA] = data_set.get_sweep_metadata(sweep_number) amp = float(sweep_info[DH.METADATA]['aibs_stimulus_amplitude_pa']) if abs(amp - math.ceil(amp)) < 1e-5: amp = int(math.ceil(amp)) if abs(amp - math.floor(amp)) < 1e-5: amp = int(math.floor(amp)) print("Sweep: %s (%s pA): %s" % (sweep_number, amp, sweep_info[DH.METADATA])) # start/stop indices that exclude the experimental test pulse (if applicable) index_range = sweep_data['index_range'] # stimulus is a numpy array in amps
# stimulus is a numpy array in amps stimulus = sweep_data['stimulus'][index_range[0]:index_range[-1]] # response is a numpy array in volts response = sweep_data['response'][index_range[0]:index_range[-1]]*1000 subset[sweep_number] = response # sampling rate is in Hz sampling_rate = sweep_data['sampling_rate'] # define some time points in seconds (i.e., convert to absolute time) time_pts = np.arange(0,len(stimulus)/sampling_rate,1./sampling_rate) subset['t'] = time_pts metadata = data_set.get_sweep_metadata(sweep_number) ampl = round(metadata['aibs_stimulus_amplitude_pa'],4) # plot the stimulus and the voltage response for the random trial plt.subplot(2,1,1) plt.plot(time_pts,stimulus) plt.ylabel('Stimulus (A)') plt.subplot(2,1,2) plt.plot(time_pts,response, label = 'S %s, %s pA'%(sweep_number, ampl)) volts_file = open('%s.dat'%dataset_id, 'w') max = 1.5 # s for i in range(len(subset['t'])): t = subset['t'][i] if t <= max:
def extract_info_from_nwb_file(dataset_id, raw_ephys_file_name): info = {} import h5py import numpy as np h5f = h5py.File(raw_ephys_file_name, "r") metas = ['aibs_cre_line','aibs_dendrite_type','intracellular_ephys/Electrode 1/location'] for m in metas: d = h5f.get('/general/%s'%m) print("%s = \t%s"%(m,d.value)) info[m.split('/')[-1]]=str(d.value) h5f.close() from allensdk.core.nwb_data_set import NwbDataSet data_set = NwbDataSet(raw_ephys_file_name) sweep_numbers = data_set.get_experiment_sweep_numbers() if test: sweep_numbers = [33,45] sweep_numbers.sort() info[DH.DATASET] = dataset_id info[DH.COMMENT] = 'Data analysed on %s'%(time.ctime()) info[DH.PYELECTRO_VERSION] = pyel_ver info[DH.ALLENSDK_VERSION] = allensdk_ver info[DH.SWEEPS] = {} for sweep_number in sweep_numbers: sweep_data = data_set.get_sweep(sweep_number) if data_set.get_sweep_metadata(sweep_number)['aibs_stimulus_name'] == "Long Square": sweep_info = {} sweep_info[DH.METADATA] = data_set.get_sweep_metadata(sweep_number) info[DH.SWEEPS]['%i'%sweep_number] = sweep_info sweep_info[DH.SWEEP] = sweep_number # start/stop indices that exclude the experimental test pulse (if applicable) index_range = sweep_data['index_range'] # stimulus is a numpy array in amps stimulus = sweep_data['stimulus'][index_range[0]:index_range[-1]] # response is a numpy array in volts response = sweep_data['response'][index_range[0]:index_range[-1]]*1000 # sampling rate is in Hz sampling_rate = sweep_data['sampling_rate'] # define some time points in seconds (i.e., convert to absolute time) time_pts = np.arange(0,len(stimulus)/sampling_rate,1./sampling_rate)*1000 comment = 'Sweep: %i in %i; %sms -> %sms; %sA -> %sA; %smV -> %smV'%(sweep_number, dataset_id, time_pts[0], time_pts[-1], np.amin(stimulus), np.amax(stimulus), np.amin(response), np.amax(response)) print(comment) sweep_info[DH.COMMENT] = comment analysis = utils.simple_network_analysis({sweep_number:response}, time_pts, extra_targets = ['%s:value_280'%sweep_number, '%s:average_1000_1200'%sweep_number, '%s:average_100_200'%sweep_number], end_analysis=1500, plot=plot, show_plot_already=False, verbose=True) sweep_info[DH.ICLAMP_ANALYSIS] = analysis analysis_file_name = '%s_analysis.json'%(dataset_id) analysis_file = open(analysis_file_name, 'w') pretty = pp.pformat(info) pretty = pretty.replace('\'', '"') pretty = pretty.replace('u"', '"') analysis_file.write(pretty) analysis_file.close() print('Written info to %s'%analysis_file_name)
def extract_info_from_nwb_file(dataset_id, raw_ephys_file_name): info = {} import h5py import numpy as np h5f = h5py.File(raw_ephys_file_name, "r") metas = [ 'aibs_cre_line', 'aibs_dendrite_type', 'intracellular_ephys/Electrode 1/location' ] for m in metas: d = h5f.get('/general/%s' % m) print("%s = \t%s" % (m, d.value)) info[m.split('/')[-1]] = str(d.value) h5f.close() from allensdk.core.nwb_data_set import NwbDataSet data_set = NwbDataSet(raw_ephys_file_name) sweep_numbers = data_set.get_experiment_sweep_numbers() if test: sweep_numbers = [33, 45] sweep_numbers.sort() info[DH.DATASET] = dataset_id info[DH.COMMENT] = 'Data analysed on %s' % (time.ctime()) info[DH.PYELECTRO_VERSION] = pyel_ver info[DH.ALLENSDK_VERSION] = allensdk_ver info[DH.SWEEPS] = {} for sweep_number in sweep_numbers: sweep_data = data_set.get_sweep(sweep_number) if data_set.get_sweep_metadata( sweep_number)['aibs_stimulus_name'] == "Long Square": sweep_info = {} sweep_info[DH.METADATA] = data_set.get_sweep_metadata(sweep_number) info[DH.SWEEPS]['%i' % sweep_number] = sweep_info sweep_info[DH.SWEEP] = sweep_number # start/stop indices that exclude the experimental test pulse (if applicable) index_range = sweep_data['index_range'] # stimulus is a numpy array in amps stimulus = sweep_data['stimulus'][index_range[0]:index_range[-1]] # response is a numpy array in volts response = sweep_data['response'][ index_range[0]:index_range[-1]] * 1000 # sampling rate is in Hz sampling_rate = sweep_data['sampling_rate'] # define some time points in seconds (i.e., convert to absolute time) time_pts = np.arange(0, len(stimulus) / sampling_rate, 1. / sampling_rate) * 1000 comment = 'Sweep: %i in %i; %sms -> %sms; %sA -> %sA; %smV -> %smV' % ( sweep_number, dataset_id, time_pts[0], time_pts[-1], np.amin(stimulus), np.amax(stimulus), np.amin(response), np.amax(response)) print(comment) sweep_info[DH.COMMENT] = comment analysis = utils.simple_network_analysis( {sweep_number: response}, time_pts, extra_targets=[ '%s:value_280' % sweep_number, '%s:average_1000_1200' % sweep_number, '%s:average_100_200' % sweep_number ], end_analysis=1500, plot=plot, show_plot_already=False, verbose=True) sweep_info[DH.ICLAMP_ANALYSIS] = analysis analysis_file_name = '%s_analysis.json' % (dataset_id) analysis_file = open(analysis_file_name, 'w') pretty = pp.pformat(info) pretty = pretty.replace('\'', '"') pretty = pretty.replace('u"', '"') analysis_file.write(pretty) analysis_file.close() print('Written info to %s' % analysis_file_name)
sweep_numbers.sort() print("All sweeps for %s: %s"%(dataset_id, sweep_numbers)) subthreshs = {} spikings = {} spike_count = {} chosen = {} stimuli = {} for sweep_number in sweep_numbers: sweep_data = data_set.get_sweep(sweep_number) if data_set.get_sweep_metadata(sweep_number)['aibs_stimulus_name'] == "Long Square": sweep_info = {} sweep_info[DH.METADATA] = data_set.get_sweep_metadata(sweep_number) amp = float(sweep_info[DH.METADATA]['aibs_stimulus_amplitude_pa']) if abs(amp-math.ceil(amp))<1e-5: amp=int(math.ceil(amp)) if abs(amp-math.floor(amp))<1e-5: amp=int(math.floor(amp)) print("Sweep: %s (%s pA): %s"%(sweep_number,amp,sweep_info[DH.METADATA])) # start/stop indices that exclude the experimental test pulse (if applicable) index_range = sweep_data['index_range'] # stimulus is a numpy array in amps