def preprocess_ramp_sweeps(data_set, sweep_numbers): if len(sweep_numbers) == 0: raise er.FeatureError("No ramp sweeps available for feature extraction") ramp_sweeps = data_set.sweep_set(sweep_numbers) ramp_sweeps.select_epoch("recording") ramp_start, ramp_dur, _, _, _ = stf.get_stim_characteristics(ramp_sweeps.sweeps[0].i, ramp_sweeps.sweeps[0].t) ramp_spx, ramp_spfx = dsf.extractors_for_sweeps(ramp_sweeps, start = ramp_start, **dsf.detection_parameters(data_set.RAMP)) ramp_an = spa.RampAnalysis(ramp_spx, ramp_spfx) ramp_features = ramp_an.analyze(ramp_sweeps) return ramp_sweeps, ramp_features, ramp_an
def extract_features(data_set, ramp_sweep_numbers, ssq_sweep_numbers, lsq_sweep_numbers, amp_interval=20, max_above_rheo=100): features = {} # RAMP FEATURES ----------------- if len(ramp_sweep_numbers) > 0: ramp_sweeps = data_set.sweep_set(ramp_sweep_numbers) ramp_start, ramp_dur, _, _, _ = stf.get_stim_characteristics(ramp_sweeps.sweeps[0].i, ramp_sweeps.sweeps[0].t) ramp_spx, ramp_spfx = dsf.extractors_for_sweeps(ramp_sweeps, start = ramp_start, **dsf.detection_parameters(data_set.RAMP)) ramp_an = spa.RampAnalysis(ramp_spx, ramp_spfx) basic_ramp_features = ramp_an.analyze(ramp_sweeps) first_spike_ramp_features = first_spike_ramp(ramp_an) features.update(first_spike_ramp_features) # SHORT SQUARE FEATURES ----------------- if len(ssq_sweep_numbers) > 0: ssq_sweeps = data_set.sweep_set(ssq_sweep_numbers) ssq_start, ssq_dur, _, _, _ = stf.get_stim_characteristics(ssq_sweeps.sweeps[0].i, ssq_sweeps.sweeps[0].t) ssq_spx, ssq_spfx = dsf.extractors_for_sweeps(ssq_sweeps, est_window = [ssq_start, ssq_start+0.001], **dsf.detection_parameters(data_set.SHORT_SQUARE)) ssq_an = spa.ShortSquareAnalysis(ssq_spx, ssq_spfx) basic_ssq_features = ssq_an.analyze(ssq_sweeps) first_spike_ssq_features = first_spike_ssq(ssq_an) first_spike_ssq_features["short_square_current"] = basic_ssq_features["stimulus_amplitude"] features.update(first_spike_ssq_features) # LONG SQUARE SUBTHRESHOLD FEATURES ----------------- if len(lsq_sweep_numbers) > 0: check_lsq_sweeps = data_set.sweep_set(lsq_sweep_numbers) lsq_start, lsq_dur, _, _, _ = stf.get_stim_characteristics(check_lsq_sweeps.sweeps[0].i, check_lsq_sweeps.sweeps[0].t) # Check that all sweeps are long enough and not ended early extra_dur = 0.2 good_lsq_sweep_numbers = [n for n, s in zip(lsq_sweep_numbers, check_lsq_sweeps.sweeps) if s.t[-1] >= lsq_start + lsq_dur + extra_dur and not np.all(s.v[tsu.find_time_index(s.t, lsq_start + lsq_dur)-100:tsu.find_time_index(s.t, lsq_start + lsq_dur)] == 0)] lsq_sweeps = data_set.sweep_set(good_lsq_sweep_numbers) lsq_spx, lsq_spfx = dsf.extractors_for_sweeps(lsq_sweeps, start = lsq_start, end = lsq_start + lsq_dur, **dsf.detection_parameters(data_set.LONG_SQUARE)) lsq_an = spa.LongSquareAnalysis(lsq_spx, lsq_spfx, subthresh_min_amp=-100.) basic_lsq_features = lsq_an.analyze(lsq_sweeps) features.update({ "input_resistance": basic_lsq_features["input_resistance"], "tau": basic_lsq_features["tau"], "v_baseline": basic_lsq_features["v_baseline"], "sag_nearest_minus_100": basic_lsq_features["sag"], "sag_measured_at": basic_lsq_features["vm_for_sag"], "rheobase_i": int(basic_lsq_features["rheobase_i"]), "fi_linear_fit_slope": basic_lsq_features["fi_fit_slope"], }) # TODO (maybe): port sag_from_ri code over # Identify suprathreshold set for analysis sweep_table = basic_lsq_features["spiking_sweeps"] mask_supra = sweep_table["stim_amp"] >= basic_lsq_features["rheobase_i"] sweep_indexes = fv._consolidated_long_square_indexes(sweep_table.loc[mask_supra, :]) amps = np.rint(sweep_table.loc[sweep_indexes, "stim_amp"].values - basic_lsq_features["rheobase_i"]) spike_data = np.array(basic_lsq_features["spikes_set"]) for amp, swp_ind in zip(amps, sweep_indexes): if (amp % amp_interval != 0) or (amp > max_above_rheo) or (amp < 0): continue amp_label = int(amp / amp_interval) first_spike_lsq_sweep_features = first_spike_lsq(spike_data[swp_ind]) features.update({"ap_1_{:s}_{:d}_long_square".format(f, amp_label): v for f, v in first_spike_lsq_sweep_features.items()}) mean_spike_lsq_sweep_features = mean_spike_lsq(spike_data[swp_ind]) features.update({"ap_mean_{:s}_{:d}_long_square".format(f, amp_label): v for f, v in mean_spike_lsq_sweep_features.items()}) sweep_feature_list = [ "first_isi", "avg_rate", "isi_cv", "latency", "median_isi", "adapt", ] features.update({"{:s}_{:d}_long_square".format(f, amp_label): sweep_table.at[swp_ind, f] for f in sweep_feature_list}) features["stimulus_amplitude_{:d}_long_square".format(amp_label)] = int(amp + basic_lsq_features["rheobase_i"]) rates = sweep_table.loc[sweep_indexes, "avg_rate"].values features.update(fi_curve_fit(amps, rates)) return features