Beispiel #1
0
def load_events(session_index):
    directory = session_list[session_index]["Location"]
    file_path = path.join(directory, "EventMatrix.pkl")
    try:
        with open(file_path, 'rb') as file:
            events = load(file)

        _, t = ca_traces.load_traces(session_index)

    except:
        event_times, event_values = load_event_times(session_index)

        traces, t = ca_traces.load_traces(session_index)

        events = np.zeros(traces.shape)

        for cell, timestamps in enumerate(event_times):
            for i, this_time in enumerate(timestamps):
                _, idx = find_closest(t, this_time)
                events[cell, idx] = event_values[cell][i]

        with open(file_path, 'wb') as file:
            dump(events, file, protocol=4)

    return events, t
Beispiel #2
0
    def __init__(self, session_index, window=[-1, 10]):
        self.session_index = session_index
        self.frame_rate = 20
        window.sort()
        self.window = np.asarray(window)

        assert len(self.window) is 2, "Window length must be 2."
        assert any(self.window < 0), "One input must be positive."
        assert any(self.window > 0), "One input must be negative."

        self.ref_time = np.arange(self.window[0], self.window[1],
                                  1 / self.frame_rate)

        self.footshock_times = [698, 778, 858, 938]
        traces, self.t = \
            ca_traces.load_traces(session_index)

        self.events, _ = ca_events.load_events(session_index)
        self.traces = zscore(traces, axis=1)
        self.n_neurons = len(traces)

        try:
            self.shock_modulated_cells, self.aligned_traces, \
                self.aligned_events = load_aligned_data(session_index)
            self.make_tuning_curves()
        except:
            self.align_neurons()
            self.create_shuffle_tuning_curve()
            self.find_modulated_cells()

        self.order = self.organize_sequence()
Beispiel #3
0
def overlay_events(session_index, neurons):
    """
    Plot events on top of traces.
    :param
        session_index: Number corresponding to a session.
        neurons: List of neurons.
    :return
        f: ScrollPlot class.
    """
    # Load events and traces.
    event_times, event_values = load_event_times(session_index)
    traces, t = ca_traces.load_traces(session_index)

    # Plot and scroll.
    titles = neuron_number_title(neurons)
    f = ScrollPlot(plot_funcs.overlay_events,
                   t=t,
                   traces=traces[neurons],
                   event_times=event_times[neurons],
                   event_values=event_values[neurons],
                   xlabel='Time (s)',
                   ylabel='% DF/F',
                   titles=titles)

    return f
def concatenate_sessions(sessions,
                         include_homecage=False,
                         dtype='traces',
                         global_cell_idx=None):
    # Load cell map.
    mouse = session_list[sessions[0]]['Animal']
    match_map = cell_reg.load_cellreg_results(mouse)
    if global_cell_idx is not None:
        match_map = match_map[global_cell_idx]
    match_map = cell_reg.trim_match_map(match_map, sessions)

    neural_data = []
    all_t = []
    all_days = []
    all_freezing = []
    for idx, session in enumerate(sessions):
        # Only get these neurons.
        neurons = match_map[:, idx]

        if not include_homecage:
            data, t = load_and_trim(session, dtype=dtype, neurons=neurons)
            freezing, _ = load_and_trim(session, dtype='freezing')
            t -= t.min()

        else:
            if dtype == 'traces':
                data, t = ca_traces.load_traces(session)
                data = zscore(data, axis=1)
            elif dtype == 'events':
                data, t = ca_events.load_events(session)
            else:
                raise ValueError('Invalid data type.')

            ff_session = ff.load_session(session)
            freezing = ff_session.imaging_freezing

            data = data[neurons]

        all_freezing.extend(freezing)

        # Day index.
        day_idx = np.ones(t.size) * idx
        all_days.extend(day_idx)

        # Time stamps.
        all_t.extend(t)

        # Neural data.
        neural_data.append(data)

    neural_data = np.column_stack(neural_data)
    all_days = np.asarray(all_days)
    all_t = np.asarray(all_t)
    all_freezing = np.asarray(all_freezing)

    return neural_data, all_days, all_t, all_freezing
def plot_prefreezing_traces(session_index,
                            neurons=None,
                            dtype='events',
                            window=1):
    # Load data and get freezing timestamps.
    session = ff.load_session(session_index)
    freeze_epochs = session.get_freezing_epochs_imaging_framerate()

    if dtype == 'traces':
        data, _ = ca_traces.load_traces(session_index)
        data = zscore(data, axis=1)
    elif dtype == 'events':
        data, _ = ca_events.load_events(session_index)
        data[data > 0] = 1
    else:
        raise ValueError("Invalid data type.")

    if neurons is not None:
        data = data[neurons]
        titles = neuron_number_title(neurons)
    else:
        titles = neuron_number_title(range(len(data)))

    n_neurons = len(data)
    n_freezes = freeze_epochs.shape[0]
    freeze_duration = np.ceil(window * 20).astype(int)

    prefreezing_traces = np.zeros((n_neurons, n_freezes, freeze_duration))

    for n, trace in enumerate(data):
        for i, epoch in enumerate(freeze_epochs):
            start = epoch[0] - freeze_duration
            stop = epoch[0]
            prefreezing_traces[n, i, :] = trace[start:stop]

    if dtype == 'events':
        events = [[(np.where(bout)[0] - freeze_duration) / 20 for bout in cell]
                  for cell in prefreezing_traces]

        f = ScrollPlot(plot_funcs.plot_raster,
                       events=events,
                       window=window,
                       xlabel='Time from start of freezing (s)',
                       ylabel='Freezing bout #',
                       titles=titles)

    elif dtype == 'traces':
        f = ScrollPlot(plot_funcs.plot_heatmap,
                       heatmap=prefreezing_traces,
                       xlabel='Time from start of freezing (s)',
                       ylabel='Freezing bout #',
                       titles=titles)

    else:
        raise ValueError("Invalid data type.")
Beispiel #6
0
def plot_sequences_across_days(session_1, session_2, dtype='event',
                               mat_file='seqNMF_results.mat'):
    # Get mouse name and ensure that you're matching correct animals.
    mouse = session_list[session_1]["Animal"]
    assert mouse == session_list[session_2]["Animal"], \
        "Animal names do not match."

    # Get sequence information from session 1 and traces from session 2.
    s1_sequence = seqNMF(session_1, mat_file=mat_file)

    if dtype == 'trace':
        data, t = ca_traces.load_traces(session_2)
        data = zscore(data, axis=1)
    elif dtype == 'event':
        data,t  = ca_events.load_events(session_2)
        data[data > 0] = 1
    else:
        raise ValueError('Wrong dtype value.')

    session = ff.load_session(session_2)

    # Trim and process trace information and freezing/velocity.
    traces = d_pp.trim_session(data, session.mouse_in_cage)

    if dtype == 'event':
        events = []
        for cell in traces:
            events.append(np.where(cell)[0]/20)

    # Get statistically significant cells in sequence and their rank.
    significant_cells = s1_sequence.get_sequential_cells()
    sequence_orders = s1_sequence.get_sequence_order()

    # Load cell matching map.
    cell_map = load_cellreg_results(mouse)
    map_idx = find_match_map_index([session_1, session_2])

    # For each sequence from seqNMF...
    for cells, order in zip(significant_cells, sequence_orders):
        # Get cell index and sort them.
        global_cell_idx = find_cell_in_map(cell_map, map_idx[0], cells)
        local_s2_cells = cell_map[global_cell_idx, map_idx[1]]
        sorted_s2_cells = local_s2_cells[order]

        # Delete cells that didn't match.
        sorted_s2_cells = sorted_s2_cells[sorted_s2_cells > -1]
        n_cells = len(sorted_s2_cells)

        plot_ordered_cells(session_2, sorted_s2_cells, range(n_cells),
                           dtype=dtype)
Beispiel #7
0
def PCA_session(session_index, bin_length=1, dtype='traces'):
    session = FF.load_session(session_index)

    # Get accepted neurons.
    if dtype == 'traces':
        traces, t = ca_traces.load_traces(session_index)
        traces = zscore(traces, axis=1)

        scaler = StandardScaler()
        traces = scaler.fit_transform(traces)
    elif dtype == 'events':
        traces, t = ca_events.load_events(session_index)
    else:
        raise ValueError('Wrong dtype input.')

    n_neurons = len(traces)

    # Trim the traces to only include instances where mouse is in the chamber.
    t = d_pp.trim_session(t, session.mouse_in_cage)
    traces = d_pp.trim_session(traces, session.mouse_in_cage)
    freezing = d_pp.trim_session(session.imaging_freezing,
                                 session.mouse_in_cage)

    samples_per_bin = bin_length * 20
    bins = d_pp.make_bins(t, samples_per_bin)
    n_samples = len(bins) + 1

    X = np.zeros([n_samples, n_neurons])
    for n, trace in enumerate(traces):
        binned_activity = d_pp.bin_time_series(trace, bins)
        avg_activity = [np.mean(chunk) for chunk in binned_activity]

        X[:, n] = avg_activity

    binned_freezing = d_pp.bin_time_series(freezing, bins)
    binned_freezing = [i.any() for i in binned_freezing]

    # lda = LinearDiscriminantAnalysis(solver='eigen',n_components=2,shrinkage='auto')
    # lda.fit(X,binned_freezing)
    # Y = lda.transform(X)

    pca = PCA(n_components=3)
    pca.fit(X)
    Y = pca.transform(X)

    fig = plt.figure()
    ax = Axes3D(fig)
    s = ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], c=binned_freezing)
    fig.show()
    def __init__(self, session_index, dtype='event'):
        self.session_index = session_index
        self.dtype = dtype

        if dtype == 'event':
            self.events, _ = load_events(session_index)
            self.events[self.events > 0] = 1
        elif dtype == 'trace':
            self.events, _ = load_traces(session_index)

        self.session = ff.load_session(session_index)
        self.freezing_epochs = \
            self.session.get_freezing_epochs_imaging_framerate()
        self.non_freezing_idx = self.get_non_freezing_epochs()
        self.n_neurons = self.events.shape[0]
Beispiel #9
0
    def interpolate(self):
        """
        Match timestamps and position to imaging.
        """
        _, imaging_t = ca_traces.load_traces(self.session_index)
        x = np.interp(imaging_t, self.video_t, self.position[:, 0])
        y = np.interp(imaging_t, self.video_t, self.position[:, 1])
        imaging_v = np.interp(imaging_t, self.video_t, self.velocity)

        imaging_freezing = np.zeros(imaging_t.shape, dtype=bool)
        freezing_epochs = self.get_freezing_epochs()

        # Interpolate the freezing bouts.
        for this_epoch in freezing_epochs:
            _, start_idx = find_closest(imaging_t, self.video_t[this_epoch[0]])
            _, end_idx = find_closest(imaging_t,
                                      self.video_t[this_epoch[1] - 1])
            imaging_freezing[start_idx:end_idx] = True

        return x, y, imaging_t, imaging_freezing, imaging_v
def load_and_trim(session_index, dtype='traces', neurons=None):
    session = ff.load_session(session_index)

    if dtype == 'traces':
        data, t = ca_traces.load_traces(session_index)
        data = zscore(data, axis=1)
    elif dtype == 'events':
        data, t = ca_events.load_events(session_index)
    elif dtype == 'freezing':
        data = session.imaging_freezing
        t = session.imaging_t
    else:
        raise ValueError('Wrong data type.')

    if neurons is not None:
        data = data[neurons]

    data = trim_session(data, session.mouse_in_cage)
    t = trim_session(t, session.mouse_in_cage)

    return data, t
Beispiel #11
0
    def __init__(self, session_index, xcorr_window=[-15, 15]):
        self.session_index = session_index

        # Load shock-aligned cells, traces, and events.
        try:
            shock_cells, self.aligned_traces, self.aligned_events = \
                load_aligned_data(session_index)
            self.shock_cells = np.asarray(shock_cells)
        except:
            print("Shock-aligned traces not detected.")

        # Load full traces.
        traces, _ = ca_traces.load_traces(session_index)
        self.n_neurons = len(traces)

        # Get mouse in chamber indices.
        session = ff.load_session(session_index)
        self.traces = d_pp.trim_session(traces, session.mouse_in_cage)

        mouse = session_list[session_index]["Animal"]
        self.map = load_cellreg_results(mouse)

        xcorr_window.sort()
        self.xcorr_window = np.asarray(xcorr_window)

        assert len(self.xcorr_window) is 2, "Window length must be 2."
        assert any(self.xcorr_window < 0), "One input must be positive."
        assert any(self.xcorr_window > 0), "One input must be negative."

        directory = session_list[session_index]["Location"]
        xcorr_file = path.join(directory, 'CrossCorrelations.pkl')

        try:
            with open(xcorr_file, 'rb') as file:
                self.xcorrs, self.best_lags, self.window = load(file)
        except:
            self.xcorrs, self.best_lags, self.window = \
                xcorr_all_neurons(self.traces, self.xcorr_window)

            self.save_xcorrs()
Beispiel #12
0
def preprocess_NB(session_index, bin_length=2, predictor='traces'):
    session = ff.load_session(session_index)

    # Get accepted neurons.
    if predictor == 'traces':
        predictor_var, t = ca_traces.load_traces(session_index)
        predictor_var = zscore(predictor_var, axis=1)
    elif predictor == 'events':
        predictor_var, t = ca_events.load_events(session_index)
        predictor_var[predictor_var > 0] = 1
    else:
        raise ValueError('Predictor incorrectly defined.')

    # Trim the traces to only include instances where mouse is in the chamber.
    t = d_pp.trim_session(t, session.mouse_in_cage)
    predictor_var = d_pp.trim_session(predictor_var, session.mouse_in_cage)
    freezing = d_pp.trim_session(session.imaging_freezing,
                                 session.mouse_in_cage)

    # Define bin limits.
    samples_per_bin = bin_length * 20
    bins = d_pp.make_bins(t, samples_per_bin)
    binned_activity = d_pp.bin_time_series(predictor_var, bins)

    if predictor == 'traces':
        X = np.mean(np.asarray(binned_activity[0:-1]), axis=2)
        X = np.append(X, np.mean(binned_activity[-1], axis=1)[None, :], axis=0)

    elif predictor == 'events':
        X = np.sum(np.asarray(binned_activity[0:-1]), axis=2)
        X = np.append(X, np.sum(binned_activity[-1], axis=1)[None, :], axis=0)

    else:
        raise ValueError('Invalid data type.')

    # Bin freezing vector.
    binned_freezing = d_pp.bin_time_series(freezing, bins)
    Y = [i.any() for i in binned_freezing]

    return X, Y
Beispiel #13
0
def plot_sequence_over_days(FC_session, test_session):
    FC = ShockSequence(FC_session)
    mouse = session_list[FC_session]["Animal"]

    assert mouse == session_list[test_session]["Animal"], \
        "Mice in sessions you're comparing are different!"
    match_map = load_cellreg_results(mouse)

    map_idx = find_match_map_index([FC.session_index, test_session])
    shock_cells_global = find_cell_in_map(match_map, map_idx[0],
                                          FC.shock_modulated_cells)

    shock_cells_local = match_map[shock_cells_global, map_idx[1]]
    shock_cells_local_sorted = shock_cells_local[FC.order]
    shock_cells_local_sorted = \
        shock_cells_local_sorted[shock_cells_local_sorted > 0]

    traces, _ = ca_traces.load_traces(test_session)
    traces = zscore(traces, axis=1)

    plt.imshow(traces[shock_cells_local_sorted])

    pass
def trim_and_bin(session_index,
                 dtype='trace',
                 neurons=None,
                 samples_per_bin=200):
    session = ff.load_session(session_index)

    if dtype == 'trace':
        data, t = ca_traces.load_traces(session_index)
        data = zscore(data, axis=1)

    if neurons is not None:
        data = data[neurons]

    # Trim away home cage epochs.
    t = trim_session(t, session.mouse_in_cage)
    data = trim_session(data, session.mouse_in_cage)

    # Bin time series.
    bins = make_bins(t, samples_per_bin)
    t = bin_time_series(t, bins)
    data = bin_time_series(data, bins)

    return data, t, bins
Beispiel #15
0
def plot_ordered_cells(session_index, cells, order=None, dtype='event'):
    if order is None:
        order = range(len(cells))

    session = ff.load_session(session_index)
    n_cells = len(cells)

    if type(cells) is list:
        cells = np.asarray(cells)

    if dtype == 'trace':
        data, t = ca_traces.load_traces(session_index)
        data = zscore(data, axis=1)
    elif dtype == 'event':
        data, t  = ca_events.load_events(session_index)
        data[data > 0] = 1
    else:
        raise ValueError('Wrong dtype value.')

    # Trim all the time series.
    traces = d_pp.trim_session(data, session.mouse_in_cage)
    t = d_pp.trim_session(t, session.mouse_in_cage)
    t -= min(t)
    v = d_pp.trim_session(session.imaging_v, session.mouse_in_cage)
    freezing = d_pp.trim_session(session.imaging_freezing,
                                 session.mouse_in_cage)

    if dtype == 'event':
        events = []
        for cell in traces:
            events.append(np.where(cell)[0]/20)

    # Plot.
    gs = gridspec.GridSpec(4, 1)
    fig = plt.figure()

    # Velocity plot.
    velocity_plot = fig.add_subplot(gs[0,:])
    plt.plot(t, v, 'k')
    velocity_plot.set_ylabel('Velocity',color='k')
    velocity_plot.fill_between(t, 0, v.max(), freezing,
                               facecolor='r')
    plt.setp(velocity_plot.get_xticklabels(), visible=False)

    # Raster plot.
    event_plot = fig.add_subplot(gs[1:,0], sharex=velocity_plot)

    if dtype == 'trace':
        plt.imshow(traces[cells[order]],
                   extent=[t[0], t[-1], n_cells, 0])
    elif dtype == 'event':
        plt.eventplot([events[x] for
                       x in cells[order]])

        event_plot.fill_between(t, -0.5, n_cells+0.5, freezing,
                                facecolor='r', alpha=0.4)
        event_plot.invert_yaxis()

    plt.axis('tight')
    plt.xlabel('Time (s)')
    plt.ylabel('Cell #')