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
def find_most_active_overlap(session_1, session_2, percentile=50): mouse = session_list[session_1]["Animal"] assert mouse == session_list[session_2]["Animal"], \ "Mouse names don't match!" s1_most_active = find_most_active(session_1, percentile) s2_most_active = find_most_active(session_2, percentile) match_map = load_cellreg_results(mouse) match_map = trim_match_map(match_map, [session_1, session_2]) _, idx = ismember(match_map[:, 1], s2_most_active) s1_cells_active_on_s2 = match_map[idx, 0] s1_overlap_with_s2 = list(set(s1_cells_active_on_s2) & set(s1_most_active)) # Number of cells that were highly active on session 1 and also registered # to session 2. _, idx = ismember(match_map[:, 0], s1_most_active) n_cells = np.sum((match_map[idx, :] > -1).all(axis=1)) n_overlap = len(s1_overlap_with_s2) percent_overlap = n_overlap / n_cells return percent_overlap
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
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_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)
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()
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
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)
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 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
[15, 16, 17, 19]] # shuffled = [] # for i in np.arange(100): # accuracy = cross_session_NB(s1,s2,shuffle=True) # shuffled.append(accuracy) # # accuracy = cross_session_NB(s1,s2) scores_events = np.zeros((len(session_1), 3)) pvals_events = np.zeros((len(session_1), 3)) scores_traces = np.zeros((len(session_1), 3)) pvals_traces = np.zeros((len(session_1), 3)) #S = ShockSequence(s1) for i, fc in enumerate(session_1): match_map = load_cellreg_results(session_list[fc]['Animal']) trimmed = trim_match_map(match_map, all_sessions[i]) neurons = trimmed[:, 0] for j, ext in enumerate(session_2[i]): score, _, p_value = \ cross_session_NB(fc, ext, bin_length=bin_length, predictor='events', neurons=neurons) scores_events[i, j] = score pvals_events[i, j] = p_value score, _, p_value = \ cross_session_NB(fc, ext, bin_length=bin_length, predictor='traces', neurons=neurons) scores_traces[i, j] = score