def plot_ephys_ophys_trace(key_cell,time_to_plot=None,trace_window = 1,show_stimulus = False,show_e_ap_peaks = False, show_o_ap_peaks = False): #%% # ============================================================================= # key_cell = {'session': 1, # 'subject_id': 456462, # 'cell_number': 3, # 'motion_correction_method': 'VolPy', # 'roi_type': 'VolPy'} # time_to_plot=None # trace_window = 100 # show_stimulus = False # show_e_ap_peaks = True # show_o_ap_peaks = True # ============================================================================= fig=plt.figure() ax_ophys = fig.add_axes([0,0,2,.8]) ax_ephys = fig.add_axes([0,-1,2,.8]) if show_stimulus: ax_ephys_stim = fig.add_axes([0,-1.5,2,.4]) #% session_time, cell_recording_start = (experiment.Session()*ephys_patch.Cell()&key_cell).fetch1('session_time','cell_recording_start') first_movie_start_time = np.min(np.asarray(((imaging.Movie()*imaging_gt.GroundTruthROI())&key_cell).fetch('movie_start_time'),float)) first_movie_start_time_real = first_movie_start_time + session_time.total_seconds() if not time_to_plot: time_to_plot = trace_window/2#ephys_matched_ap_times[0] session_time_to_plot = time_to_plot+first_movie_start_time # time relative to session start cell_time_to_plot= session_time_to_plot + session_time.total_seconds() -cell_recording_start.total_seconds() # time relative to recording start #% sweep_start_times,sweep_end_times,sweep_nums = (ephys_patch.Sweep()&key_cell).fetch('sweep_start_time','sweep_end_time','sweep_number') needed_start_time = cell_time_to_plot - trace_window/2 needed_end_time = cell_time_to_plot + trace_window/2 #% sweep_nums = sweep_nums[((sweep_start_times > needed_start_time) & (sweep_start_times < needed_end_time)) | ((sweep_end_times > needed_start_time) & (sweep_end_times < needed_end_time)) | ((sweep_end_times > needed_end_time) & (sweep_start_times < needed_start_time)) ] ephys_traces = list() ephys_trace_times = list() ephys_sweep_start_times = list() ephys_traces_stim = list() for sweep_num in sweep_nums: sweep = ephys_patch.Sweep()&key_cell&'sweep_number = %d' % sweep_num trace,sr= (ephys_patch.SweepMetadata()*ephys_patch.SweepResponse()&sweep).fetch1('response_trace','sample_rate') trace = trace*1000 sweep_start_time = float(sweep.fetch('sweep_start_time')) trace_time = np.arange(len(trace))/sr + sweep_start_time + cell_recording_start.total_seconds() - first_movie_start_time_real trace_idx = (time_to_plot-trace_window/2 < trace_time) & (time_to_plot+trace_window/2 > trace_time) ax_ephys.plot(trace_time[trace_idx],trace[trace_idx],'k-') ephys_traces.append(trace) ephys_trace_times.append(trace_time) ephys_sweep_start_times.append(sweep_start_time) if show_e_ap_peaks: ap_max_index = (ephysanal.ActionPotential()&sweep).fetch('ap_max_index') aptimes = trace_time[np.asarray(ap_max_index,int)] apVs = trace[np.asarray(ap_max_index,int)] ap_needed = (time_to_plot-trace_window/2 < aptimes) & (time_to_plot+trace_window/2 > aptimes) aptimes = aptimes[ap_needed] apVs = apVs[ap_needed] ax_ephys.plot(aptimes,apVs,'ro') if show_stimulus: trace_stim= (ephys_patch.SweepMetadata()*ephys_patch.SweepStimulus()&sweep).fetch1('stimulus_trace') trace_stim = trace_stim*10**12 ax_ephys_stim.plot(trace_time[trace_idx],trace_stim[trace_idx],'k-') ephys_traces_stim.append(trace_stim) ephysdata = {'ephys_traces':ephys_traces,'ephys_trace_times':ephys_trace_times} if show_stimulus: ephysdata['ephys_traces_stimulus'] = ephys_traces_stim movie_nums, movie_start_times,movie_frame_rates,movie_frame_nums = ((imaging.Movie()*imaging_gt.GroundTruthROI())&key_cell).fetch('movie_number','movie_start_time','movie_frame_rate','movie_frame_num') movie_start_times = np.asarray(movie_start_times, float) movie_end_times = np.asarray(movie_start_times, float)+np.asarray(movie_frame_nums, float)/np.asarray(movie_frame_rates, float) needed_start_time = session_time_to_plot - trace_window/2 needed_end_time = session_time_to_plot + trace_window/2 movie_nums = movie_nums[((movie_start_times >= needed_start_time) & (movie_start_times <= needed_end_time)) | ((movie_end_times >= needed_start_time) & (movie_end_times <= needed_end_time)) | ((movie_end_times >= needed_end_time) & (movie_start_times <= needed_start_time)) ] dffs=list() frametimes = list() for movie_num in movie_nums: #movie_num = movie_nums[(session_time_to_plot>movie_start_times)&(session_time_to_plot<movie_end_times)][0] key_movie = key_cell.copy() key_movie['movie_number'] = movie_num dff,gt_roi_number = ((imaging.ROI()*imaging_gt.GroundTruthROI())&key_movie).fetch1('roi_dff','roi_number') dff_all,roi_number_all = (imaging.ROI()&key_movie).fetch('roi_dff','roi_number') dff_all =dff_all[roi_number_all!=gt_roi_number] frame_times = ((imaging.MovieFrameTimes()*imaging_gt.GroundTruthROI())&key_movie).fetch1('frame_times') + (session_time).total_seconds() - first_movie_start_time_real #modified to cell time frame_idx = (time_to_plot-trace_window/2 < frame_times) & (time_to_plot+trace_window/2 > frame_times) dff_list = [dff] dff_list.extend(dff_all) prevminval = 0 for dff_now,alpha_now in zip(dff_list,np.arange(1,1/(len(dff_list)+1),-1/(len(dff_list)+1))): dfftoplotnow = dff_now[frame_idx] + prevminval ax_ophys.plot(frame_times[frame_idx],dfftoplotnow,'g-',alpha=alpha_now) prevminval = np.min(dfftoplotnow) -.005 #ax_ophys.plot(frame_times[frame_idx],dff[frame_idx],'g-') dffs.append(dff) frametimes.append(frame_times) if show_o_ap_peaks: if 'raw' in key_cell['roi_type']: apidxes = ((imaging.ROI()*imaging_gt.GroundTruthROI())&key_movie).fetch1('roi_spike_indices') else: apidxes = ((imaging.ROI()*imaging_gt.GroundTruthROI())&key_movie).fetch1('roi_spike_indices')-1 oap_times = frame_times[apidxes] oap_vals = dff[apidxes] oap_needed = (time_to_plot-trace_window/2 < oap_times) & (time_to_plot+trace_window/2 > oap_times) oap_times = oap_times[oap_needed] oap_vals = oap_vals[oap_needed] ax_ophys.plot(oap_times,oap_vals,'ro') ophysdata = {'ophys_traces':dffs,'ophys_trace_times':frametimes} ax_ophys.autoscale(tight = True) # ============================================================================= # dfff = np.concatenate(dffs) # dfff[np.argmin(dfff)]=0 # ax_ophys.set_ylim([min(dfff),max(dfff)]) # ============================================================================= ax_ophys.set_xlim([time_to_plot-trace_window/2,time_to_plot+trace_window/2]) ax_ophys.set_ylabel('dF/F') ax_ophys.spines["top"].set_visible(False) ax_ophys.spines["right"].set_visible(False) ax_ophys.invert_yaxis() ax_ephys.autoscale(tight = True) ax_ephys.set_xlim([time_to_plot-trace_window/2,time_to_plot+trace_window/2]) ax_ephys.set_ylabel('Vm (mV))') ax_ephys.spines["top"].set_visible(False) ax_ephys.spines["right"].set_visible(False) if show_stimulus: # ax_ephys_stim.autoscale(tight = True) ax_ephys_stim.set_xlim([time_to_plot-trace_window/2,time_to_plot+trace_window/2]) ax_ephys_stim.set_ylabel('Injected current (pA))') ax_ephys_stim.set_xlabel('time from first movie start (s)') ax_ephys_stim.spines["top"].set_visible(False) ax_ephys_stim.spines["right"].set_visible(False) else: ax_ephys.set_xlabel('time from first movie start (s)') outdict = {'ephys':ephysdata,'ophys':ophysdata,'figure_handle':fig} #%% return outdict
def make(self, key): #% #key = {'subject_id': 454597, 'session': 1, 'cell_number': 0, 'motion_correction_method': 'Matlab', 'roi_type': 'SpikePursuit', 'roi_number': 1} #key = {'subject_id': 456462, 'session': 1, 'cell_number': 5, 'movie_number': 3, 'motion_correction_method': 'VolPy', 'roi_type': 'VolPy', 'roi_number': 1} if len( ROIEphysCorrelation & key ) > 0: # and key['roi_type'] == 'SpikePursuit' #only spikepursuit for now.. key_to_compare = key.copy() del key_to_compare['roi_number'] #print(key) #% roinums = np.unique( (ROIEphysCorrelation() & key_to_compare).fetch('roi_number')) snratios_mean = list() snratios_median = list() snratios_first50 = list() for roinum_now in roinums: snratios = (ROIAPWave() & key_to_compare & 'roi_number = {}'.format(roinum_now) ).fetch('apwave_snratio') snratios_mean.append(np.mean(snratios)) snratios_median.append(np.median(snratios)) snratios_first50.append(np.mean(snratios[:50])) #%% if np.max( (ROIEphysCorrelation() & key).fetch('roi_number') ) == roinums[np.argmax( snratios_first50 )]: #np.max((imaging_gt.ROIEphysCorrelation()&key).fetch('roi_number')) == np.min((imaging_gt.ROIEphysCorrelation&key_to_compare).fetch('roi_number')):#np.max(np.abs((ROIEphysCorrelation&key).fetch('corr_coeff'))) == np.max(np.abs((ROIEphysCorrelation&key_to_compare).fetch('corr_coeff'))): print('this is it') print(key['roi_type']) cellstarttime = (ephys_patch.Cell() & key).fetch1('cell_recording_start') sessionstarttime = (experiment.Session() & key).fetch1('session_time') aptimes = np.asarray( (ephysanal.ActionPotential() & key).fetch('ap_max_time'), float) + (cellstarttime - sessionstarttime).total_seconds() sweep_start_times, sweep_end_times = (ephys_patch.Sweep() & key).fetch( 'sweep_start_time', 'sweep_end_time') sweep_start_times = np.asarray(sweep_start_times, float) + ( cellstarttime - sessionstarttime).total_seconds() sweep_end_times = np.asarray(sweep_end_times, float) + ( cellstarttime - sessionstarttime).total_seconds() frame_timess, roi_spike_indicess = ( imaging.MovieFrameTimes() * imaging.Movie() * imaging.ROI() & key).fetch('frame_times', 'roi_spike_indices') movie_start_times = list() movie_end_times = list() roi_ap_times = list() for frame_times, roi_spike_indices in zip( frame_timess, roi_spike_indicess): movie_start_times.append(frame_times[0]) movie_end_times.append(frame_times[-1]) roi_ap_times.append(frame_times[roi_spike_indices]) movie_start_times = np.sort(movie_start_times) movie_end_times = np.sort(movie_end_times) roi_ap_times = np.sort(np.concatenate(roi_ap_times)) #% ##delete spikes in optical traces where there was no ephys recording for start_t, end_t in zip( np.concatenate([sweep_start_times, [np.inf]]), np.concatenate([[0], sweep_end_times])): idxtodel = np.where((roi_ap_times > end_t) & (roi_ap_times < start_t))[0] if len(idxtodel) > 0: roi_ap_times = np.delete(roi_ap_times, idxtodel) ##delete spikes in ephys traces where there was no imaging for start_t, end_t in zip( np.concatenate([movie_start_times, [np.inf]]), np.concatenate([[0], movie_end_times])): idxtodel = np.where((aptimes > end_t) & (aptimes < start_t))[0] if len(idxtodel) > 0: #print(idxtodel) aptimes = np.delete(aptimes, idxtodel) #% D = np.zeros([len(aptimes), len(roi_ap_times)]) for idx, apt in enumerate(aptimes): D[idx, :] = (roi_ap_times - apt) * 1000 D_test = np.abs(D) D_test[D_test > 15] = 1000 D_test[D < -1] = 1000 X = scipy.optimize.linear_sum_assignment(D_test) #% cost = D_test[X[0], X[1]] unmatched = np.where(cost == 1000)[0] X0_final = np.delete(X[0], unmatched) X1_final = np.delete(X[1], unmatched) ephys_ap_times = aptimes[X0_final] ophys_ap_times = roi_ap_times[X1_final] false_positive_time_imaging = list() for roi_ap_time in roi_ap_times: if roi_ap_time not in ophys_ap_times: false_positive_time_imaging.append(roi_ap_time) false_negative_time_ephys = list() for aptime in aptimes: if aptime not in ephys_ap_times: false_negative_time_ephys.append(aptime) key['ephys_matched_ap_times'] = ephys_ap_times key['ophys_matched_ap_times'] = ophys_ap_times key['ephys_unmatched_ap_times'] = false_negative_time_ephys key['ophys_unmatched_ap_times'] = false_positive_time_imaging #print(imaging.ROI()&key) #print([len(aptimes),'vs',len(roi_ap_times)]) #%% self.insert1(key, skip_duplicates=True)
# try: # ============================================================================= #% movie_dir = os.path.join(ground_truth_basedir , subject_id , cell_id , movie) with open(os.path.join(movie_dir,'movie_metadata.json')) as json_file: movie_metadata = json.load(json_file) frame_times = np.load(os.path.join(movie_dir,'frame_times.npy')) pixel_shifts = np.load(os.path.join(movie_dir,'motion_corr_shifts_rigid.npy')) key_roi = {'subject_id': movie_metadata['subject_id'], 'session': movie_metadata['session'], 'movie_number': movie_metadata['movie_number'], 'motion_correction_method': 'VolPy', 'roi_type': 'VolPy'} #% dff,gt_roi_num = (imaging.ROI()*imaging_gt.GroundTruthROI()&key_roi).fetch1('roi_dff','roi_number') dff_all,roi_number_all = (imaging.ROI()&key_roi).fetch('roi_dff','roi_number') dff_all =dff_all[roi_number_all!=gt_roi_num] dff_list = [dff] dff_list.extend(dff_all) ephys_files_dir = os.path.join(movie_dir,'ephys') ephys_files = sorted(os.listdir(ephys_files_dir)) sweep_time = list() sweep_response = list() sweep_stimulus = list() sweep_metadata = list() for ephys_file in ephys_files: if ephys_file[-3:]=='npz': data_dict = np.load(os.path.join(ephys_files_dir,ephys_file)) sweep_time.append(data_dict['time']) sweep_response.append(data_dict['voltage'])
for movie_number in movie_numbers: frame_rate = ((imaging.Movie())&key_cell & 'movie_number = '+str(movie_number)).fetch('movie_frame_rate')[0] #frame_num = ((imaging.Movie())&key_cell & 'movie_number = '+str(movie_number)).fetch('movie_frame_num')[0] movie_start_time = float(((imaging.Movie())&key_cell & 'movie_number = '+str(movie_number)).fetch('movie_start_time')[0]) movie_start_time = session_time.total_seconds() + movie_start_time - cell_time.total_seconds() movie_time = (imaging.MovieFrameTimes()&key_cell & 'movie_number = '+str(movie_number)).fetch('frame_times')[0] -cell_time.total_seconds() +session_time.total_seconds() movie_end_time = movie_time[-1] sweeps_needed = ephys_patch.Sweep()&key_cell&'sweep_start_time < '+str(movie_end_time) & 'sweep_end_time > '+str(movie_start_time) sweep_start_ts, sweep_end_ts, traces,sweep_nums, sample_rates= (sweeps_needed*ephys_patch.SweepResponse()*ephys_patch.SweepMetadata()).fetch('sweep_start_time','sweep_end_time','response_trace','sweep_number','sample_rate') trace_times = list() for sweep_start_t, sweep_end_t, trace,sample_rate in zip(sweep_start_ts, sweep_end_ts, traces,sample_rates): trace_times.append(np.arange(float(sweep_start_t), float(sweep_end_t)+1/sample_rate,1/sample_rate))#np.arange(len(trace))/sample_rate+float(sweep_start_t) #% dff = (imaging.ROI()*imaging_gt.GroundTruthROI()&key_cell&'movie_number = {}'.format(movie_number)).fetch1('roi_dff') for trace,tracetime,sweep_number,sample_rate in zip(traces,trace_times,sweep_nums,sample_rates): #% start_t = tracetime[0] start_t = movie_time[np.argmax(movie_time>=start_t)] end_t = np.min([tracetime[-1],movie_time[-1]]) t = np.arange(0,.01,1/sample_rate) if convolve_voltron_kinetics: f_on = tau_1_ratio_on*np.exp(t/tau_1_on) + (1-tau_1_ratio_on)*np.exp(-t/tau_2_on) f_off = tau_1_ratio_off*np.exp(t[::-1]/tau_1_off) + (1-tau_1_ratio_off)*np.exp(-t[::-1]/tau_2_off) f_on = f_on/np.max(f_on) f_off = f_off/np.max(f_off) kernel = np.concatenate([f_on,np.zeros(len(f_off))])[::-1] kernel = kernel /sum(kernel )
) & key_cell & 'sweep_start_time < ' + str( movie_end_time) & 'sweep_end_time > ' + str(movie_start_time) sweep_start_ts, sweep_end_ts, traces, sweep_nums, sample_rates = ( sweeps_needed * ephys_patch.SweepResponse() * ephys_patch.SweepMetadata()).fetch('sweep_start_time', 'sweep_end_time', 'response_trace', 'sweep_number', 'sample_rate') trace_times = list() for sweep_start_t, sweep_end_t, trace, sample_rate in zip( sweep_start_ts, sweep_end_ts, traces, sample_rates): trace_times.append( np.arange(float(sweep_start_t), float(sweep_end_t) + 1 / sample_rate, 1 / sample_rate) ) #np.arange(len(trace))/sample_rate+float(sweep_start_t) #% dff = (imaging.ROI() * imaging_gt.GroundTruthROI() & key_cell & 'movie_number = {}'.format(movie_number)).fetch1('roi_dff') for trace, tracetime, sweep_number, sample_rate in zip( traces, trace_times, sweep_nums, sample_rates): #% start_t = tracetime[0] start_t = movie_time[np.argmax(movie_time >= start_t)] end_t = np.min([tracetime[-1], movie_time[-1]]) t = np.arange(0, .01, 1 / sample_rate) if convolve_voltron_kinetics: f_on = tau_1_ratio_on * np.exp( t / tau_1_on) + (1 - tau_1_ratio_on) * np.exp(-t / tau_2_on) f_off = tau_1_ratio_off * np.exp(t[::-1] / tau_1_off) + ( 1 - tau_1_ratio_off) * np.exp(-t[::-1] / tau_2_off)