Esempio n. 1
0
def get_predictor(
        behavior: Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, float],
        aligned_spike: Tuple[DataFrame, SparseRec], sample_rate: int,
        spline: np.ndarray) -> Tuple[Predictors, np.ndarray, Grouping]:
    """Build Predictors from lever trajectory, trial parameters and spikes.
    Args:
        behavior: from behavior::get_behavior, amplitude, speed, delay, hit and reliability
            reliability is one float, hit is a bool array with k trials. while amplitude, speed and delay are all floats
            same length as number of hit trials.
        aligned_spike: (spike, lever_trajectory) from decoder::align_xy, spike has n * k * t with n neurons, k trials
            and t timepoints in a trial. lever trajectory has a value of k * t
    Returns:
        predictor_mat: a Predictor, with each of (event, period, trial, temporal) being 3D event and trial predictors,
            n * k * t with n features, k trials and t timepoints
        y: 3D neuronal activity, n * k * t with n neurons, k trials and t timepoints
        grouping: a Predictor of 1d grouping arrays (event, period, trial, temporal)
    """
    (amplitude, max_speed, delay, hit, _), (spike,
                                            trace) = behavior, aligned_spike
    trial_samples = int(round(sample_rate * 5.0))
    delay_sample = np.minimum(trial_samples - 1,
                              np.rint(delay * sample_rate).astype(int))
    delay_period = np.vstack(list(np.broadcast(0, delay_sample))).T
    onset = np.rint(trace.timestamps * 5 / 256).astype(int)
    trajectory: np.ndarray = take_segment(trace.values, onset, trial_samples)
    speed: np.ndarray = take_segment(np.diff(trace.values), onset - 1,
                                     trial_samples)
    y = np.array([
        take_segment(neuron, onset, trial_samples) for neuron in spike.values
    ])
    preds = Predictors((0, delay_sample), [delay_period], [hit, delay],
                       [trajectory, speed])
    grouping = Grouping([1, 2], [3], [4, 5], [6, 7])
    preds, grouping = build_predictor(preds, grouping, hit, spline)
    return preds, y, grouping
Esempio n. 2
0
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, :]
Esempio n. 3
0
def splice_trial_xy(xy: Tuple[DataFrame, SparseRec], sample_rate: float):
    spike, trace = xy
    onset = np.rint(trace.timestamps * 5 / 256).astype(int)
    trial_samples = int(round(sample_rate * 5.0))
    neurons = np.array([
        take_segment(neuron, onset, trial_samples).ravel()
        for neuron in spike.values
    ])
    trajectory = take_segment(trace.values, onset, trial_samples).ravel()
    timestamps = take_segment(trace.axes[0], onset, trial_samples).ravel()
    return spike.create_like(neurons,
                             [spike.axes[0], timestamps]), trace.create_like(
                                 trajectory, [timestamps])
Esempio n. 4
0
def test_take_segment():
    trace = np.arange(5, 1005)
    points = np.array([3, 18, 518, 998])
    result = take_segment(trace, points - 3, 5)
    assert (np.array_equal(
        result,
        np.array([[5, 6, 7, 8, 9], [20, 21, 22, 23, 24],
                  [520, 521, 522, 523, 524], [1000, 1001, 1002, 1003, 1004]])))
Esempio n. 5
0
def test_take_segments(lever_file, correct_last_trial):
    raw = lever_file['response']['mvmtdata'].ravel()
    onset = np.array(lever_file['response']['samples_start'] + [156000])
    segments = take_segment(raw, onset, 1280)
    print(segments)
    assert (np.allclose(segments[-1, :], correct_last_trial))