예제 #1
0
def visualize_prediction(data_file: File, params: MotionParams):
    """Show 3 PCs of neuron activity and the plane in linear SVC classifier."""
    lever = get_trials(data_file, params)
    trials = fold_by(DataFrame.load(data_file['spike']), lever,
                     data_file.attrs['frame_rate'], True)
    k_cluster = k_means(lever.values, 2)[1].astype(np.bool)
    main_cluster, null_cluster = np.flatnonzero(
        k_cluster)[:20], np.flatnonzero(~k_cluster)[:10]
    all_neurons = np.hstack([
        trials.values.take(main_cluster, 1),
        trials.values.take(null_cluster, 1)
    ])
    all_results = np.array([0] * 20 + [1] * 10)
    training_X = all_neurons.swapaxes(0, 1).reshape(all_neurons.shape[1], -1)
    pca_weigths = PCA(20).fit_transform(training_X)
    classifier = SVC(kernel='linear')
    classifier.fit(pca_weigths, all_results)
    classifier.score(pca_weigths, all_results)
    coef = classifier.coef_[0]
    intercept = classifier.intercept_
    xx, yy = np.meshgrid(
        np.linspace(pca_weigths[:, 0].min(), pca_weigths[:, 0].max(), 20),
        np.linspace(pca_weigths[:, 1].min(), pca_weigths[:, 1].max(), 20))
    z = (-intercept - xx * coef[0] - yy * coef[1]) / coef[2]
    with Figure(projection='3d') as ax:
        ax[0].scatter(*pca_weigths[:, 6:9].T,
                      color=np.asarray(COLORS)[all_results],
                      s=50)
        ax[0].plot_surface(xx, yy, z, alpha=0.4, color=COLORS[-1])
        ax[0].set_zlim(pca_weigths[:, 2].min(), pca_weigths[:, 2].max())
예제 #2
0
def get_fall(data_file: File, params: MotionParams):
    lever = devibrate_rec(get_trials(data_file, params))
    pre_value = lever.values[:, :lever._pre // 2].mean(axis=1, keepdims=True)
    lever_off = np.argmax(lever.values[:, lever._pre:] <= pre_value, axis=1) + lever._pre
    lever_top = int(np.median([np.argmax(x[lever._pre: y]) for x, y in zip(lever.values, lever_off) if y > lever._pre])) + lever._pre
    lever_off = int(np.median(lever_off))
    return reliability(lever.values[:, lever_top: lever_off])
예제 #3
0
def neuron_lever_unsampled(data_file: File, params: Dict[str, float]) -> Tuple[np.ndarray, np.ndarray]:
    """
    Returns:
        neural_activity, lever
    """
    lever = get_trials(data_file)
    neurons = fold_by(DataFrame.load(data_file["spike"]), lever, data_file.attrs['frame_rate'], True)
    neurons, lever = filter_empty_trial_sets(neurons.values, lever.values[0])
    mask, filtered = devibrate_trials(lever, params["pre_time"])
    return neurons[:, mask, :], lever[mask, :]
예제 #4
0
def draw_template(data_file: File, params: MotionParams, labels: np.ndarray,
                  info: Dict[str, Union[str, int]]):
    trials = get_trials(data_file, params).values
    labels = (k_means(trials, 2)[1] == 1, quantize(labels).astype(np.bool))
    session_name = f"template_cmp-{info['group']}-{info['id']}-{info['fov']:02d}{info['session']:02d}.svg"
    with Figure(join(img_folder, 'template', session_name), (6, 4),
                grid=(1, 2)) as axes:
        for ax, label, colors, name in zip(axes, labels,
                                           (COLORS[0:2], COLORS[2:]),
                                           ('kmeans', 'hierarchy')):
            ax.set_title(name)
            tsplot(ax, trials[label, :], color=colors[0])
            tsplot(ax, trials[~label, :], color=colors[1])
예제 #5
0
def draw_cluster_3d(data_file: File, params: MotionParams, labels: np.ndarray,
                    info: Dict[str, Union[str, int]]):
    trials = get_trials(data_file, params).values
    points = PCA(3).fit_transform(trials)
    session_name = f"{info['group']}-{info['id']}-{info['fov']:02d}{info['session']:02d}"
    with Figure(join(img_folder, 'pca', session_name + '.svg'),
                grid=(1, 2),
                projection='3d') as axes:
        for (clusters, title), ax in zip(((k_means(trials, 2)[1], "k-means"),
                                          (quantize(labels), "hierarchy")),
                                         axes):
            ax.set_title(title)
            for unique, color in zip(np.unique(clusters), COLORS):
                cluster = clusters == unique
                ax.scatter(points[cluster, 0],
                           points[cluster, 1],
                           points[cluster, 2],
                           c=color)
예제 #6
0
def run_amp_power(data_file: File) -> Tuple[float, float, float, float]:
    """Try to decode the max lever trajectory amplitude of each trial.
    Returns:
        pre_amp_power: mutual info between predicted (from neuron activity before motor onset)
            and real amplitude of trials in one session
        post_amp_power: mutual info between predicted (from neuron activity before motor onset)
            and real amplitude of trials in one session
    """
    VALIDATE_FOLD = 10
    lever = get_trials(data_file, motion_params)
    neuron = DataFrame.load(data_file['spike'])
    resampled_onsets = np.rint(lever.trial_anchors *
                               (5 / 256)).astype(np.int_) - 3
    folded = np.stack(
        [take_segment(trace, resampled_onsets, 6) for trace in neuron.values])
    mask, filtered = devibrate_trials(lever.values, motion_params['pre_time'])
    mask &= np.any(folded > 0, axis=(0, 2))
    amp = filtered[mask, 25:64].max(axis=1) - filtered[mask, 0:15].mean(axis=1)
    speed = np.diff(filtered[mask, 5:50], axis=1).max(axis=1)
    svr_rbf = SVR('rbf', 3, 1E-7, cache_size=1000)
    X = folded[:, mask, 0:3].swapaxes(0, 1).reshape(mask.sum(), -1)
    pre_amp_hat = cross_predict(
        X.T, amp, lambda x, y, y_t: svr_rbf.fit(x.T, y).predict(y_t.T),
        VALIDATE_FOLD, False)
    pre_v_hat = cross_predict(
        X.T, speed, lambda x, y, y_t: svr_rbf.fit(x.T, y).predict(y_t.T),
        VALIDATE_FOLD, False)
    X = folded[:, mask, 3:].swapaxes(0, 1).reshape(mask.sum(), -1)
    post_amp_hat = cross_predict(
        X.T, amp, lambda x, y, y_t: svr_rbf.fit(x.T, y).predict(y_t.T),
        VALIDATE_FOLD, False)
    post_v_hat = cross_predict(
        X.T, speed, lambda x, y, y_t: svr_rbf.fit(x.T, y).predict(y_t.T),
        VALIDATE_FOLD, False)
    return (mutual_info(pre_amp_hat, amp), mutual_info(post_amp_hat, amp),
            mutual_info(pre_v_hat, speed), mutual_info(post_v_hat, speed))
예제 #7
0
 def _get_bisect(data):
     mask, filtered = devibrate_trials(
         get_trials(data[0], motion_params)[0], motion_params["pre_time"])
     return pca_bisect(filtered), mask
예제 #8
0
 def _get_k_means(data_file: File):
     return k_means(get_trials(data_file, motion_params).values, 2)[1]
예제 #9
0
def visualize_kmeans(data_file: File, params: MotionParams):
    lever = get_trials(data_file)
    lever.scale()
    dist = kNNdist(lever.values, 4)[:, -1]
    plt.plot(sorted(dist))
    print("min points: ", min_points(lever.values, 16.7))
예제 #10
0
def lever_corr(record_file, record_info, params: MotionParams):
    lever = get_trials(record_file, params)
    mask, filtered = devibrate_trials(lever.values, params['pre_time'])
    lever_value = lever.values[0, mask, :]
    indices = np.triu_indices(lever_value.shape[0], 1)
    return np.corrcoef(lever_value)[indices]
예제 #11
0
def get_later(data_file: File, params: MotionParams):
    lever = get_trials(data_file, params)
    pre_value = lever.values[:, :lever._pre // 2].mean(axis=1, keepdims=True)
    lever_off = int(np.median(np.argmax(lever.values[:, lever._pre:] <= pre_value, axis=1))) + lever._pre
    return reliability(lever.values[:, lever_off:])
예제 #12
0
def fall_spread(data_file: File, params: MotionParams):
    lever = devibrate_rec(get_trials(data_file, params))
    pre_value = lever.values[:, :lever._pre // 2].mean(axis=1, keepdims=True)
    lever_off = np.argmax(lever.values[:, lever._pre:] <= pre_value, axis=1)
    return np.std(lever_off)
예제 #13
0
def get_rise(data_file: File, params: MotionParams):
    lever = devibrate_rec(get_trials(data_file, params))
    lever_top = int(np.median(np.argmax(lever.values[:, lever._pre:], axis=1))) + lever._pre
    return reliability(lever.values[:, lever._pre // 2: lever_top])
예제 #14
0
def get_reliability(data_file: File) -> float:
    mask, filtered = devibrate_trials(
        get_trials(data_file, motion_params).values, motion_params['pre_time'])
    return np.corrcoef(filtered[mask, 15:64])[np.triu_indices(mask.sum(),
                                                              1)].mean()
예제 #15
0
def get_speed(data_file: File) -> float:
    mask, filtered = devibrate_trials(
        get_trials(data_file, motion_params).values, motion_params['pre_time'])
    speed = np.diff(filtered[mask, 5:50], axis=1).max(axis=1)
    return np.mean(speed)
예제 #16
0
def get_amp(data_file: File) -> float:
    mask, filtered = devibrate_trials(
        get_trials(data_file, motion_params).values, motion_params['pre_time'])
    return np.quantile(
        filtered[mask, 25:64].max(axis=1) - filtered[mask, 0:15].mean(axis=1),
        0.75)