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 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
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
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
def PCA_concatenated_sessions(mouse, bin_length=5, dtype='traces', global_cell_idx=None): sessions, _ = find_mouse_sessions(mouse) sessions = sessions[[0, 1, 2, 4]] neural_data, days, t, freezing = \ d_pp.concatenate_sessions(sessions, dtype=dtype, global_cell_idx=global_cell_idx) bins = d_pp.make_bins(t, bin_length * 20) neural_data = d_pp.bin_time_series(neural_data, bins) X = np.mean(np.asarray(neural_data[0:-1]), axis=2) X = np.append(X, np.mean(neural_data[-1], axis=1)[None, :], axis=0) # Bin freezing vector. binned_freezing = d_pp.bin_time_series(freezing, bins) freezing = np.asarray([i.any() for i in binned_freezing]) binned_days = d_pp.bin_time_series(days, bins) day_id, _ = mode(np.asarray(binned_days[0:-1]), axis=1) day_id = np.append(day_id, mode(binned_days[-1])[0]) # pca = PCA(n_components=3) # pca.fit(X) # Y = pca.transform(X) # # fig = plt.figure() # ax = Axes3D(fig) # # # s1 = ax.scatter(Y[freezing, 0], # Y[freezing, 1], # Y[freezing, 2], # c=day_id[freezing], s=40, marker='.', cmap='Reds') # # s2 = ax.scatter(Y[~freezing, 0], # # Y[~freezing, 1], # # Y[~freezing, 2], # # c=day_id[~freezing], s=40, marker='+', cmap='Reds') pca = PCA(n_components=2) pca.fit(X) Y = pca.transform(X) fig, ax = plt.subplots() s1 = ax.scatter(Y[freezing, 0], Y[freezing, 1], c=day_id[freezing], s=40, marker='.', cmap='summer') # s2 = ax.scatter(Y[~freezing, 0], # Y[~freezing, 1], # c=day_id[~freezing], s=40, marker='+', # cmap='summer') fig.show() pass