Esempio n. 1
0
def compute_percent_freezing(session_index, bin_length=100, plot_flag=False):
    session = load_ff(session_index)
    _, t = ca_events.load_events(session_index)

    t = d_pp.trim_session(t, 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)

    # Elapsed time in minutes.
    t -= t.min()
    t /= 60
    t_binned = (bins - bins.min()) / 1200
    t_binned = np.append(t_binned, t.max())

    binned_freezing = d_pp.bin_time_series(freezing, bins)

    percent_freezing = np.zeros((len(binned_freezing)))
    for epoch_number, epoch in enumerate(binned_freezing):
        percent_freezing[epoch_number] = np.sum(epoch) / len(epoch)

    if plot_flag:
        plt.figure()
        plt.plot(t_binned, percent_freezing)

    return percent_freezing, t_binned
Esempio n. 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()
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.")
Esempio n. 5
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)
Esempio n. 6
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]
Esempio n. 8
0
def find_most_active(session_index, percentile):
    session = ff.load_session(session_index)
    events, _ = ca_events.load_events(session_index)
    events = d_pp.trim_session(events, session.mouse_in_cage)
    events = events > 0

    n_events = []
    for event in events:
        n_events.append(np.sum(event))

    threshold = np.percentile(n_events, percentile)

    neurons_above_threshold = [neuron for neuron, n
                               in enumerate(n_events)
                               if n >= threshold]

    return neurons_above_threshold
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
Esempio n. 10
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
Esempio n. 11
0
def detect_ramping_cells(session_index, bin_length=300):
    events = ca_events.load_events(session_index)
    session = ff.load_session(session_index)

    n_neurons = len(events)

    t = d_pp.trim_session(session.imaging_t,session.mouse_in_cage)
    events = d_pp.trim_session(events, session.mouse_in_cage)

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

    binned_events = np.zeros([n_neurons, n_samples])
    for n, event in enumerate(events):
        binned = d_pp.bin_time_series(event, bins)
        binned_events[n, :] = [np.sum(chunk > 0) for chunk in binned]

    ScrollPlot(plot_funcs.plot_traces,
               t=np.arange(0, len(bins) + 1),
               traces=binned_events)

    pass
Esempio n. 12
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 #')