예제 #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
예제 #2
0
def plot_freezing(session_index):
    session = load_ff(session_index)

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

    fig, velocity_plot = plt.subplots()
    plt.plot(t, v, 'k')
    velocity_plot.set_ylabel('Velocity', color='k')
    velocity_plot.fill_between(t, 0, v.max(), freezing, facecolor='r')
예제 #3
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()
예제 #4
0
def save_events(session_index):
    from ff_video_fixer import load_session

    entire_session_events, _ = load_events(session_index)
    session = load_session(session_index)
    events = d_pp.trim_session(entire_session_events, session.mouse_in_cage)

    directory = session_list[session_index]["Location"]
    file = path.join(directory, 'Events.mat')

    sio.savemat(file, {'events': events, 'events_all': entire_session_events})
예제 #5
0
def save_traces(session_index):
    from ff_video_fixer import load_session

    entire_session_traces, _ = load_traces(session_index)
    session = load_session(session_index)
    traces = d_pp.trim_session(entire_session_traces, session.mouse_in_cage)

    directory = session_list[session_index]["Location"]
    file = path.join(directory, 'Traces.mat')

    sio.savemat(file, {'traces': traces, 'traces_all': entire_session_traces})
예제 #6
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
예제 #7
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)
예제 #8
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
예제 #9
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
예제 #10
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()
예제 #11
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 #')