def neuron_lever(lever: SparseRec, neuron: DataFrame, neuron_rate: float, motion_params: MotionParams) -> Tuple[DataFrame, np.ndarray]: """Read lever and corresponding neuron data by trial. I reimplement [fold_by] here to acheive same length of the two segments. Args: data_file: file including spike and response(lever) motion_params: MotionParams, a dict but all params are optional may have [pre_time] and [post_time] in seconds, [quiet_var] and [event_thres] in float, [window_size] in sample_no Retursn: neurons: a DataFrame with folded neurons lever: a DataFrame with folded lever, resampled to neuron sample rate """ params = { x: motion_params[x] for x in ("quiet_var", "window_size", "event_thres") if x in motion_params } event_onsets = event.find_response_onset(lever, **params)[0] lever_resample = resample(lever.values[0], lever.sample_rate, neuron_rate) anchor = np.rint( (event_onsets / lever.sample_rate - motion_params['pre_time']) * neuron_rate).astype(np.int) duration = int( round((motion_params.get('pre_time', 0.1) + motion_params.get('post_time', 0.9)) * neuron_rate)) lever_folded = utils.take_segment(lever_resample, anchor, duration) neuron_trials = np.stack([ utils.take_segment(trace, anchor, duration) for trace in neuron.values ]) mask, filtered = devibrate_trials(lever_folded, motion_params['pre_time']) return neuron_trials[:, mask, :], lever_folded[mask, :]
def get_hitrate(data_file: File) -> float: lever = load_mat(data_file['response']) params = { x: y for x, y in motion_params.items() if x in ('quiet_var', 'window_size', 'event_thres') } _, _, _, correct_trials, _ = find_response_onset(lever, **params) return correct_trials.mean()
def get_hitrate(data_file: File, params: MotionParams) -> float: """Get hitrate (hit / trial_no) for one session. Hit is based reanalysis of trajectory and not whether reward was given.""" lever = load_mat(data_file['response']) params = { x: y for x, y in params.items() if x in ('quiet_var', 'window_size', 'event_thres') } _, _, _, correct_trials, _ = find_response_onset(lever, **params) return correct_trials.mean()
def get_delay(data_file: File) -> float: lever = load_mat(data_file['response']) params = { x: y for x, y in motion_params.items() if x in ('quiet_var', 'window_size', 'event_thres') } motion_onsets, stim_onsets, _, correct_trials, _ = find_response_onset( lever, **params) return np.mean( (motion_onsets - stim_onsets[correct_trials]) / lever.sample_rate)
def get_delay(data_file: File, params: MotionParams) -> float: """Get delay in seconds. Delay as the time between EarlyCueTime and actual motion onset (passing threshold).""" lever = load_mat(data_file['response']) params = { x: y for x, y in params.items() if x in ('quiet_var', 'window_size', 'event_thres') } motion_onsets, stim_onsets, _, correct_trials, _ = find_response_onset( lever, **params) return np.mean( (motion_onsets - stim_onsets[correct_trials]) / lever.sample_rate)