def freezing_trace_heatmap(session_index, neurons='all'): # Load the position data. session = ff.load_session(session_index) # Get freezing epochs. freeze_epochs = session.get_freezing_epochs_imaging_framerate() # Load trace data. traces, t = load_traces(session_index) if neurons == 'all': n_neurons = len(traces) neurons = arange(n_neurons) else: n_neurons = len(neurons) # Get dimensions for heatmap. n_freezes = freeze_epochs.shape[0] freeze_durations = np.diff(freeze_epochs) longest_freeze = freeze_durations.max() # Preallocate heatmap. freezing_traces = np.full([n_neurons, n_freezes, longest_freeze], np.nan) for n in neurons: for i, epoch in enumerate(freeze_epochs): freezing_traces[n,i,np.arange(freeze_durations[i])] = \ traces[n,epoch[0]:epoch[1]] titles = neuron_number_title(neurons) f = ScrollPlot(plot_funcs.heatmap, heatmap=freezing_traces, xlabel='Time from start of freezing (s)', ylabel='Freezing bout #', titles=titles)
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.")
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})
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})
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 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]
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 plot_freezing_traces(session_index): # Load the position data. session = ff.load_session(session_index) # Get freezing epochs. freeze_epochs = session.get_freezing_epochs_imaging_framerate() # Load trace data. traces, t = load_traces(session_index) f = ScrollPlot(plot_funcs.plot_freezing_traces, t=t, traces=traces, epochs=freeze_epochs, n_rows=freeze_epochs.shape[0], share_y=True, xlabel='Time (s)', ylabel='%DF/F')
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
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 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 trim_and_bin(session_index, dtype='trace', neurons=None, samples_per_bin=200): session = ff.load_session(session_index) if dtype == 'trace': data, t = ca_traces.load_traces(session_index) data = zscore(data, axis=1) if neurons is not None: data = data[neurons] # Trim away home cage epochs. t = trim_session(t, session.mouse_in_cage) data = trim_session(data, session.mouse_in_cage) # Bin time series. bins = make_bins(t, samples_per_bin) t = bin_time_series(t, bins) data = bin_time_series(data, bins) return data, t, bins
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 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 #')