def train_response_plot(expt_list, name=None, summary_plots=[None, None], color=None): ind_base_subtract = [] rec_base_subtract = [] train_plots = pg.plot() train_plots.setLabels(left=('Vm', 'V')) tau =15e-3 lp = 1000 for expt in expt_list: for pre, post in expt.connections: if expt.cells[pre].cre_type == cre_type[0] and expt.cells[post].cre_type == cre_type[1]: print ('Processing experiment: %s' % (expt.nwb_file)) ind = [] rec = [] analyzer = DynamicsAnalyzer(expt, pre, post) train_responses = analyzer.train_responses artifact = analyzer.cross_talk() if artifact > 0.03e-3: continue for i, stim_params in enumerate(train_responses.keys()): rec_t = int(np.round(stim_params[1] * 1e3, -1)) if stim_params[0] == 50 and rec_t == 250: pulse_offsets = analyzer.pulse_offsets if len(train_responses[stim_params][0]) != 0: ind_group = train_responses[stim_params][0] rec_group = train_responses[stim_params][1] for j in range(len(ind_group)): ind.append(ind_group.responses[j]) rec.append(rec_group.responses[j]) if len(ind) > 5: ind_avg = TraceList(ind).mean() rec_avg = TraceList(rec).mean() rec_avg.t0 = 0.3 base = float_mode(ind_avg.data[:int(10e-3 / ind_avg.dt)]) ind_base_subtract.append(ind_avg.copy(data=ind_avg.data - base)) rec_base_subtract.append(rec_avg.copy(data=rec_avg.data - base)) train_plots.plot(ind_avg.time_values, ind_avg.data - base) train_plots.plot(rec_avg.time_values, rec_avg.data - base) app.processEvents() if len(ind_base_subtract) != 0: print (name + ' n = %d' % len(ind_base_subtract)) ind_grand_mean = TraceList(ind_base_subtract).mean() rec_grand_mean = TraceList(rec_base_subtract).mean() ind_grand_mean_dec = bessel_filter(exp_deconvolve(ind_grand_mean, tau), lp) train_plots.addLegend() train_plots.plot(ind_grand_mean.time_values, ind_grand_mean.data, pen={'color': 'g', 'width': 3}, name=name) train_plots.plot(rec_grand_mean.time_values, rec_grand_mean.data, pen={'color': 'g', 'width': 3}, name=name) #train_plots.plot(ind_grand_mean_dec.time_values, ind_grand_mean_dec.data, pen={'color': 'g', 'dash': [1,5,3,2]}) train_amps = train_amp([ind_base_subtract, rec_base_subtract], pulse_offsets, '+') if ind_grand_mean is not None: train_plots = summary_plot_train(ind_grand_mean, plot=summary_plots[0], color=color, name=(legend + ' 50 Hz induction')) train_plots = summary_plot_train(rec_grand_mean, plot=summary_plots[0], color=color) train_plots2 = summary_plot_train(ind_grand_mean_dec, plot=summary_plots[1], color=color, name=(legend + ' 50 Hz induction')) return train_plots, train_plots2, train_amps else: print ("No Traces") return None
def bsub_mean(self): """Return a baseline-subtracted, average evoked response trace between two cells. All traces are downsampled to the minimum sample rate in the set. """ if len(self) == 0: return None if self._bsub_mean is None: responses = self.responses baselines = self.baselines # downsample all traces to the same rate # yarg: how does this change SNR? avg = TraceList([r.copy(t0=0) for r in responses]).mean() avg_baseline = TraceList([b.copy(t0=0) for b in baselines]).mean().data # subtract baseline baseline = np.median(avg_baseline) bsub = avg.data - baseline result = avg.copy(data=bsub) assert len(result.time_values) == len(result) # Attach some extra metadata to the result: result.meta['baseline'] = avg_baseline result.meta['baseline_med'] = baseline if len(avg_baseline) == 0: result.meta['baseline_std'] = None else: result.meta['baseline_std'] = scipy.signal.detrend(avg_baseline).std() self._bsub_mean = result return self._bsub_mean
def bsub_mean(self): """Return a baseline-subtracted, average evoked response trace between two cells. All traces are downsampled to the minimum sample rate in the set. """ if len(self) == 0: return None if self._bsub_mean is None: responses = self.responses baselines = self.baselines # downsample all traces to the same rate # yarg: how does this change SNR? avg = TraceList([r.copy(t0=0) for r in responses]).mean() avg_baseline = TraceList([b.copy(t0=0) for b in baselines]).mean().data # subtract baseline baseline = np.median(avg_baseline) bsub = avg.data - baseline result = avg.copy(data=bsub) assert len(result.time_values) == len(result) # Attach some extra metadata to the result: result.meta['baseline'] = avg_baseline result.meta['baseline_med'] = baseline if len(avg_baseline) == 0: result.meta['baseline_std'] = None else: result.meta['baseline_std'] = scipy.signal.detrend(avg_baseline).std() self._bsub_mean = result return self._bsub_mean
def get_amplitude(response_list): avg_trace = TraceList(response_list).mean() data = avg_trace.data dt = avg_trace.dt base = float_mode(data[:int(10e-3 / dt)]) bsub_mean = avg_trace.copy(data=data - base) neg = bsub_mean.data[int(13e-3 / dt):].min() pos = bsub_mean.data[int(13e-3 / dt):].max() avg_amp = neg if abs(neg) > abs(pos) else pos amp_sign = '-' if avg_amp < 0 else '+' peak_ind = list(bsub_mean.data).index(avg_amp) peak_t = bsub_mean.time_values[peak_ind] return bsub_mean, avg_amp, amp_sign, peak_t
def first_pulse_features(pair, pulse_responses, pulse_response_amps): avg_psp = TraceList(pulse_responses).mean() dt = avg_psp.dt avg_psp_baseline = float_mode(avg_psp.data[:int(10e-3 / dt)]) avg_psp_bsub = avg_psp.copy(data=avg_psp.data - avg_psp_baseline) lower_bound = -float('inf') upper_bound = float('inf') xoffset = pair.connection_strength.ic_fit_xoffset if xoffset is None: xoffset = 14 * 10e-3 synapse_type = pair.connection_strength.synapse_type if synapse_type == 'ex': amp_sign = '+' elif synapse_type == 'in': amp_sign = '-' else: raise Exception( 'Synapse type is not defined, reconsider fitting this pair %s %d->%d' % (pair.expt_id, pair.pre_cell_id, pair.post_cell_id)) weight = np.ones(len( avg_psp.data)) * 10. # set everything to ten initially weight[int(10e-3 / dt):int(12e-3 / dt)] = 0. # area around stim artifact weight[int(12e-3 / dt):int(19e-3 / dt)] = 30. # area around steep PSP rise psp_fits = fit_psp(avg_psp, xoffset=(xoffset, lower_bound, upper_bound), yoffset=(avg_psp_baseline, lower_bound, upper_bound), sign=amp_sign, weight=weight) amp_cv = np.std(pulse_response_amps) / np.mean(pulse_response_amps) features = { 'ic_fit_amp': psp_fits.best_values['amp'], 'ic_fit_latency': psp_fits.best_values['xoffset'] - 10e-3, 'ic_fit_rise_time': psp_fits.best_values['rise_time'], 'ic_fit_decay_tau': psp_fits.best_values['decay_tau'], 'ic_amp_cv': amp_cv, 'avg_psp': avg_psp_bsub.data } #'ic_fit_NRMSE': psp_fits.nrmse()} TODO: nrmse not returned from psp_fits? return features
def first_pulse_features(pair, pulse_responses, pulse_response_amps): avg_psp = TraceList(pulse_responses).mean() dt = avg_psp.dt avg_psp_baseline = float_mode(avg_psp.data[:int(10e-3/dt)]) avg_psp_bsub = avg_psp.copy(data=avg_psp.data - avg_psp_baseline) lower_bound = -float('inf') upper_bound = float('inf') xoffset = pair.connection_strength.ic_fit_xoffset if xoffset is None: xoffset = 14*10e-3 synapse_type = pair.connection_strength.synapse_type if synapse_type == 'ex': amp_sign = '+' elif synapse_type == 'in': amp_sign = '-' else: raise Exception('Synapse type is not defined, reconsider fitting this pair %s %d->%d' % (pair.expt_id, pair.pre_cell_id, pair.post_cell_id)) weight = np.ones(len(avg_psp.data)) * 10. # set everything to ten initially weight[int(10e-3 / dt):int(12e-3 / dt)] = 0. # area around stim artifact weight[int(12e-3 / dt):int(19e-3 / dt)] = 30. # area around steep PSP rise psp_fits = fit_psp(avg_psp, xoffset=(xoffset, lower_bound, upper_bound), yoffset=(avg_psp_baseline, lower_bound, upper_bound), sign=amp_sign, weight=weight) amp_cv = np.std(pulse_response_amps)/np.mean(pulse_response_amps) features = {'ic_fit_amp': psp_fits.best_values['amp'], 'ic_fit_latency': psp_fits.best_values['xoffset'] - 10e-3, 'ic_fit_rise_time': psp_fits.best_values['rise_time'], 'ic_fit_decay_tau': psp_fits.best_values['decay_tau'], 'ic_amp_cv': amp_cv, 'avg_psp': avg_psp_bsub.data} #'ic_fit_NRMSE': psp_fits.nrmse()} TODO: nrmse not returned from psp_fits? return features
name=("n = %d" % n_synapses)) rec_plot[t, c].plot(recovery_grand_trace.time_values, recovery_grand_trace.data, pen={ 'color': color, 'width': 2 }) rec_plot[t, c].setLabels(left=('Vm', 'V')) rec_plot[t, c].setLabels(bottom=('t', 's')) if deconv is True: rec_deconv = deconv_train(rec_pass_qc[:2]) rec_deconv_grand = TraceList(rec_deconv[0]).mean() rec_ind_deconv_grand = TraceList(rec_deconv[1]).mean() #log_rec_plt.plot(rec_deconv_grand.time_values, rec_deconv_grand.data, # pen={'color': color, 'width': 2}) rec_deconv_ind_grand2 = rec_ind_deconv_grand.copy(t0=delta + 0.2) #log_rec_plt.plot(rec_deconv_ind_grand2.time_values, rec_deconv_ind_grand2.data, # pen={'color': color, 'width': 2}) [ deconv_rec_plot[t, c].plot(ind.time_values, ind.data, pen=trace_color) for ind in rec_deconv[0] ] [ deconv_rec_plot[t, c].plot(rec.time_values, rec.data, pen=trace_color) for rec in rec_deconv[1] ] deconv_rec_plot[t, c].plot(rec_ind_deconv_grand.time_values,
summary_plot[c, 1].setTitle('Recovery') rec_plot[t, c].addLegend() [rec_plot[t, c].plot(ind.time_values, ind.data, pen=trace_color) for ind in rec_pass_qc[0]] [rec_plot[t, c].plot(rec.time_values, rec.data, pen=trace_color) for rec in rec_pass_qc[1]] rec_plot[t, c].plot(rec_ind_grand_trace.time_values, rec_ind_grand_trace.data, pen={'color': color, 'width': 2}, name=("n = %d" % n_synapses)) rec_plot[t, c].plot(recovery_grand_trace.time_values, recovery_grand_trace.data, pen={'color': color, 'width': 2}) rec_plot[t, c].setLabels(left=('Vm', 'V')) rec_plot[t, c].setLabels(bottom=('t', 's')) if deconv is True: rec_deconv = deconv_train(rec_pass_qc[:2]) rec_deconv_grand = TraceList(rec_deconv[0]).mean() rec_ind_deconv_grand = TraceList(rec_deconv[1]).mean() #log_rec_plt.plot(rec_deconv_grand.time_values, rec_deconv_grand.data, # pen={'color': color, 'width': 2}) rec_deconv_ind_grand2 = rec_ind_deconv_grand.copy(t0=delta + 0.2) #log_rec_plt.plot(rec_deconv_ind_grand2.time_values, rec_deconv_ind_grand2.data, # pen={'color': color, 'width': 2}) [deconv_rec_plot[t, c].plot(ind.time_values, ind.data, pen=trace_color) for ind in rec_deconv[0]] [deconv_rec_plot[t, c].plot(rec.time_values, rec.data, pen=trace_color) for rec in rec_deconv[1]] deconv_rec_plot[t, c].plot(rec_ind_deconv_grand.time_values, rec_ind_deconv_grand.data, pen={'color': color, 'width': 2}, name=("n = %d" % n_synapses)) deconv_rec_plot[t, c].plot(rec_deconv_grand.time_values, rec_deconv_grand.data, pen={'color': color, 'width': 2}) summary_plot[c, 1].setLabels(left=('Norm Amp', '')) summary_plot[c, 1].setLabels(bottom=('Pulse Number', '')) f_color = pg.hsvColor(hue=hue, sat=float(t+0.5) / len(rec_t), val=1) summary_plot[c, 1].plot(rec_amp_grand/rec_amp_grand[0], name=(' %d ms' % delta), pen=f_color, symbol=symbols[t], symbolBrush=f_color, symbolPen=None) if connection_types[c] not in rec_amp_summary.keys(): rec_amp_summary[connection_types[c]] = []