Beispiel #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())
Beispiel #2
0
def draw_noise(data_files: Dict[int, File], neuron_id: int, params: MotionParams):
    last_day = max(data_files.keys())
    lever = load_mat(data_files[last_day]['response'])
    neuron_rate = data_files[last_day].attrs['frame_rate']
    neurons = common_axis([DataFrame.load(x['spike']) for x in data_files.values()])
    good, bad, anti = classify_cells(motion_corr(
        lever, neurons[-1], neuron_rate, 16000, params), 0.001)
    amp = list()
    corrs: Dict[str, List[List[float]]] = {'good': [], 'unrelated': [], 'between': []}
    for (day_id, data_file), neuron in zip(data_files.items(), neurons):
        if day_id == last_day:
            continue
        lever = load_mat(data_file['response'])
        corrs['good'].append(_take_triu(noise_autocorrelation(lever, neuron[good], neuron_rate)))
        corrs['unrelated'].append(_take_triu(noise_autocorrelation(lever, neuron[bad | anti], neuron_rate)))
        corrs['between'].append(_take_triu(noise_correlation(lever, neuron[good], neuron[bad | anti], neuron_rate)))
        lever.center_on("motion", **params)
        neuron_trials = fold_by(neuron, lever, neuron_rate, True)
        amp.append(neuron_trials.values[np.argwhere(neuron.axes[0] == neuron_id)[0, 0], :, :].max(axis=1))
    with Figure(join(project_folder, 'report', 'img', f'noise_corr_{neuron_id}.svg')) as (ax,):
        day_ids = [x for x in data_files.keys() if x != last_day]
        for idx, (group_str, group) in enumerate(corrs.items()):
            ax.errorbar(day_ids, [np.mean(x) for x in group],
                        yerr=[_sem(x) for x in group], color=COLORS[idx], label=group_str)
        ax2 = ax.twinx()
        ax2.errorbar(day_ids, [np.mean(x) for x in amp], [_sem(x) for x in amp], color=COLORS[-1])
        ax.set_title(str(neuron_id))
        ax.legend()
Beispiel #3
0
def make_sample_neurons(spike_framerate: Tuple[Dict[str, np.ndarray], float],
                        log: SparseRec, params: Dict[str, float]) -> SparseRec:
    lever = log.center_on("motion", **params).fold_trials()
    lever.values = np.squeeze(lever.values, 0)
    lever.axes = lever.axes[1:]
    filtered = devibrate_rec(lever, params)
    spikes, frame_rate = spike_framerate
    return fold_by(DataFrame.load(spikes), filtered, frame_rate, True)
Beispiel #4
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, :]
Beispiel #5
0
def draw_rasterplot(day: int, data_file: File, neuron_id: int, params: MotionParams):
    lever = load_mat(data_file['response'])
    lever.center_on('motion', **params)
    neurons = DataFrame.load(data_file['spike'])
    neuron_rate = data_file.attrs['frame_rate']
    traces = fold_by(neurons, lever, neuron_rate, True)[np.flatnonzero(neurons.axes[0] == neuron_id)[0], :, :]
    mask = np.all(traces.values > 0, axis=1)
    onset = int(round(params['pre_time'] * neuron_rate))
    with Figure(join(img_folder, 'neuron-trace', f"raster-day-{day}.svg"), (2, 4)) as (ax,):
        labeled_heatmap(ax, traces[mask, :] - traces[mask, 0: onset].mean(axis=1, keepdims=True), cmap="coolwarm")
        ax.set_title(f"day-{day}")
Beispiel #6
0
def make_related_neurons(
        trial_log: SparseRec, spike_framerate: Tuple[Dict[str, np.ndarray],
                                                     float]) -> np.ndarray:
    """Calcualte the pearson r between neuron activity and trajectory. Returns an array of p values for each neuron."""
    spike, frame_rate = spike_framerate
    trial_spikes = fold_by(DataFrame.load(spike), trial_log, frame_rate, False)
    trajectory = scale(trial_log.values).ravel()
    p_values = [
        pearsonr(neuron, trajectory)[1]
        for neuron in scale(trial_spikes.values).reshape(
            trial_spikes.shape[0], -1)
    ]
    return np.array(p_values)
Beispiel #7
0
def draw_neuron(day: int, data_file: File, neuron_id: int, params: MotionParams):
    """Draw one neuron in trial for one session, with bootstrapped spread as shadow."""
    lever = load_mat(data_file['response'])
    lever.center_on("motion", **params)
    neuron = DataFrame.load(data_file['spike'])
    traces = fold_by(neuron, lever, data_file.attrs['frame_rate'])
    traces = traces[np.flatnonzero(traces.axes[0] == neuron_id)[0], :, :]
    mask = np.all(traces.values > 0, axis=1)
    pre_value = traces.values[mask, 0: int(round(params['pre_time'] * lever.sample_rate))].mean(axis=1, keepdims=True)
    trace_values = traces.values[mask, :] - pre_value
    with Figure(join(img_folder, "neuron-trace", f"day-{day:02d}.svg"), (1, 4)) as (ax,):
        tsplot(ax, trace_values, time=traces.axes[2], color=COLORS[4])
        ax.set_title(f"day_{day:02d}")
Beispiel #8
0
def draw_neuron_corr(data_files: Dict[int, File], params: MotionParams, fov_id: str = None):
    neurons = common_axis([DataFrame.load(x['spike']) for x in data_files.values()])
    last_day = max(data_files.keys())
    lever = load_mat(data_files[last_day]['response'])
    neuron_rate = data_files[last_day].attrs['frame_rate']
    good, bad, anti = classify_cells(motion_corr(
        lever, neurons[-1], neuron_rate, 16000, params), 0.001)
    result_list = list()
    for (day, data_file), neuron in zip(data_files.items(), neurons):
        lever.center_on('motion')  # type: ignore
        motion_neurons = fold_by(neuron, lever, neuron_rate, True)
        result_list.append([reliability(motion_neuron) for motion_neuron in motion_neurons.values])
    result = np.array(result_list)

    with Figure(join(img_folder, ("neuron_corr.svg" if fov_id is None else f"{fov_id}.svg"))) as ax:
        ax[0].plot(list(data_files.keys()), result[:, good])
Beispiel #9
0
def show_correspondance(ax: Axes, record_file: File,
                        motion_params: Dict[str, float]):
    def scale(x):
        x -= x.mean()
        x /= x.std()
        return x

    lever = load_mat(record_file['response'])
    lever.center_on("motion", **motion_params)
    activity = DataFrame.load(record_file["measurement"])
    neuron_rate = record_file.attrs['frame_rate']
    trials = filter_empty_trials(ts.fold_by(activity, lever, neuron_rate,
                                            True))
    slow_lever = ts.resample(lever.fold_trials().values, lever.sample_rate,
                             neuron_rate, -1)[:, 0:trials.shape[1], :]
    ax.plot(scale(trials.values[0].reshape(-1)), 'green')
    ax.plot(scale(slow_lever.reshape(-1)), 'red')
Beispiel #10
0
def make_trial_neuron(
        trial_log: SparseRec, spike_framerate: Tuple[Dict[str, np.ndarray],
                                                     float]) -> DataFrame:
    spikes, frame_rate = spike_framerate
    # trial_neurons should be [neuron, trial, time_points]
    return fold_by(DataFrame.load(spikes), trial_log, frame_rate, True)