Exemplo n.º 1
0
def preprocess_NB_cross_session(train_session,
                                test_session,
                                bin_length=2,
                                predictor='traces',
                                neurons=None):
    # Make sure the data comes from the same mouse.
    mouse = session_list[train_session]["Animal"]
    assert mouse == session_list[test_session]["Animal"], \
        "Mouse names don't match!"

    # Trim and bin data from both sessions.
    X_train, y_train = preprocess_NB(train_session,
                                     bin_length=bin_length,
                                     predictor=predictor)
    X_test, y_test = preprocess_NB(test_session,
                                   bin_length=bin_length,
                                   predictor=predictor)

    # Get registration map.
    match_map = load_cellreg_results(mouse)

    idx = find_match_map_index([train_session, test_session])

    if neurons is not None:
        global_cell_idx = find_cell_in_map(match_map, idx[0], neurons)
        match_map = match_map[global_cell_idx, :]

    trimmed_map = match_map[:, [idx[0], idx[1]]]
    detected_both_days = (trimmed_map > -1).all(axis=1)
    trimmed_map = trimmed_map[detected_both_days, :]

    X_train = X_train[:, trimmed_map[:, 0]]
    X_test = X_test[:, trimmed_map[:, 1]]

    return X_train, X_test, y_train, y_test
Exemplo n.º 2
0
def plot_traces_over_days(session_index, neurons):
    # Get the mouse.
    mouse = session_list[session_index]["Animal"]

    # Load the cell map.
    cell_map = load_cellreg_results(mouse)
    n_sessions = cell_map.shape[1]

    # Find all the sessions from this mouse.
    sessions_from_this_mouse, _ = find_mouse_sessions(mouse)

    # Make sure this matches the number of sessions in the cell map.
    assert len(sessions_from_this_mouse) == n_sessions, \
        "Number of sessions do not agree."

    traces = []
    t = []
    # Compile all the traces and time vectors
    for session in sessions_from_this_mouse:
        day_traces, day_t = load_traces(session)
        traces.append(zscore(day_traces, axis=1))
        t.append(day_t)

    # Get the column of cell map that corresponds to the specified session.
    map_index = find_match_map_index(session_index)

    # Get the cell numbers.
    _, cell_index = ismember(cell_map[:, map_index], neurons)

    # Compile traces, matching by cell.
    traces_to_plot = []
    for cell in cell_index:
        this_cell_traces = []

        for day in range(n_sessions):
            cell_number = cell_map[cell, day]

            # Make sure there was actually a match, otherwise insert
            # a placeholder.
            if cell_number > -1:
                this_cell_traces.append(traces[day][cell_number])
            else:
                placeholder = np.zeros(t[day].shape)
                this_cell_traces.append(placeholder)

        traces_to_plot.append(this_cell_traces)

    # Plot.
    f = ScrollPlot(plot_funcs.plot_traces_over_days,
                   n_rows=1,
                   n_cols=n_sessions,
                   share_y=True,
                   share_x=True,
                   t=t,
                   traces=traces_to_plot,
                   figsize=(12, 3))

    return f
Exemplo n.º 3
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)
Exemplo n.º 4
0
def plot_xcorr_over_days(XCorrObj1, XCorrObj2, neuron_pair):
    map = XCorrObj1.map

    map_idx = find_match_map_index(
        [XCorrObj1.session_index, XCorrObj2.session_index])
    global_cell_1 = find_cell_in_map(map, map_idx[0], neuron_pair[0])
    global_cell_2 = find_cell_in_map(map, map_idx[0], neuron_pair[1])

    # Plot day 1 cross-correlation.
    plt.figure()
    plt.plot(XCorrObj1.window,
             XCorrObj1.xcorrs[neuron_pair[0], neuron_pair[1]],
             c='b')

    # Plot day 2 cross-correlation.
    cell_1 = map[global_cell_1, map_idx[1]]
    cell_2 = map[global_cell_2, map_idx[1]]
    plt.plot(XCorrObj2.window, XCorrObj2.xcorrs[cell_1, cell_2], c='k')

    plt.title("Neuron " + str(neuron_pair[0]) + " with Neuron " +
              str(neuron_pair[1]))
Exemplo n.º 5
0
def compute_matrices_across_days(session_1,
                                 session_2,
                                 bin_length=10,
                                 neurons=None,
                                 n_clusters=5,
                                 cluster_here=0):
    # Load cell map.
    mouse = session_list[session_1]["Animal"]
    assert mouse == session_list[session_2]["Animal"], "Mice don't match."
    match_map = cell_reg.load_cellreg_results(mouse)
    map_idx = cell_reg.find_match_map_index((session_1, session_2))

    # If neurons are specified, narrow down the list.
    if neurons is not None:
        global_cell_idx = cell_reg.find_cell_in_map(match_map, map_idx[0],
                                                    neurons)
        match_map = match_map[global_cell_idx]

    # Only take cells that persisted across the sessions.
    match_map = cell_reg.trim_match_map(match_map, map_idx)
    neurons_ref = match_map[:, 0]
    neurons_test = match_map[:, 1]

    # Do correlations on time slices.
    corr_matrices_1, t = do_sliced_correlation(session_1, bin_length,
                                               neurons_ref)
    corr_matrices_2, _ = do_sliced_correlation(session_2, bin_length,
                                               neurons_test)

    # Cluster correlation matrix at specified time slice.
    if n_clusters > 0:
        clusters, order = cluster_corr_matrices(corr_matrices_1, cluster_here,
                                                n_clusters, t)
    else:
        order = range(len(neurons_ref))
        clusters = None

    return corr_matrices_1, corr_matrices_2, order, clusters
Exemplo n.º 6
0
def plot_ordered_cells_across_days(session_1, session_2, cells,
                                   order=None, dtype='event'):
    if order is None:
        order = range(len(cells))

    # 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."

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

    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]

    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)
Exemplo n.º 7
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
Exemplo n.º 8
0
def cosine_distance_between_days(session_1, session_2, neurons=None):
    # Load cell map.
    mouse = session_list[session_1]["Animal"]
    assert mouse == session_list[session_2]["Animal"], "Mice don't match."
    match_map = cell_reg.load_cellreg_results(mouse)
    map_idx = cell_reg.find_match_map_index((session_1, session_2))

    # If neurons are specified, narrow down the list.
    if neurons is not None:
        global_cell_idx = cell_reg.find_cell_in_map(match_map, map_idx[0],
                                                    neurons)
        match_map = match_map[global_cell_idx]

    # Only take cells that persisted across the sessions.
    match_map = cell_reg.trim_match_map(match_map, map_idx)
    neurons_ref = match_map[:, 0]
    neurons_test = match_map[:, 1]

    corr_matrix_1 = pairwise_correlate_traces(session_1, neurons_ref)
    corr_matrix_2 = pairwise_correlate_traces(session_2, neurons_test)

    d = distance.cosine(corr_matrix_1.flatten(), corr_matrix_2.flatten())

    return d