import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from allensdk.core.lazy_property import LazyProperty, LazyPropertyMixin
from allensdk.brain_observatory.behavior.behavior_ophys_session import BehaviorOphysSession
[docs]def plot_trace(timestamps, trace, ax=None, xlabel='time (seconds)', ylabel='fluorescence', title='roi'):
if ax is None:
fig, ax = plt.subplots(figsize=(15, 5))
colors = sns.color_palette()
ax.plot(timestamps, trace, color=colors[0], linewidth=3)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.set_title(title)
ax.set_xlim([timestamps[0], timestamps[-1]])
return ax
[docs]def plot_example_traces_and_behavior(dataset, cell_roi_ids, xmin_seconds, length_mins, save_dir=None,
include_running=False, cell_label=False):
suffix = ''
if include_running:
n = 2
else:
n = 1
interval_seconds = 20
xmax_seconds = xmin_seconds + (length_mins * 60) + 1
xlim = [xmin_seconds, xmax_seconds]
figsize = (15, 10)
fig, ax = plt.subplots(len(cell_roi_ids) + n, 1, figsize=figsize, sharex=True)
ax = ax.ravel()
ymins = []
ymaxs = []
for i, cell_roi_id in enumerate(cell_roi_ids):
trace = dataset.dff_traces[dataset.dff_traces['cell_roi_id']==cell_roi_id]['dff'].values[0]
ax[i] = plot_trace(dataset.ophys_timestamps, trace, ax=ax[i],
title='', ylabel=str(cell_roi_id))
ax[i] = add_stim_color_span(dataset, ax=ax[i], xlim=xlim)
ax[i] = restrict_axes(xmin_seconds, xmax_seconds, interval_seconds, ax=ax[i])
ax[i].set_xlabel('')
ymin, ymax = ax[i].get_ylim()
ymins.append(ymin)
ymaxs.append(ymax)
if cell_label:
ax[i].set_ylabel(str(cell_index))
else:
ax[i].set_ylabel('dF/F')
sns.despine(ax=ax[i])
for i, cell_roi_id in enumerate(cell_roi_ids):
ax[i].set_ylim([np.amin(ymins), np.amax(ymaxs)])
i += 1
ax[i].set_ylim([np.amin(ymins), 1])
ax[i] = plot_behavior_events(dataset, ax=ax[i], behavior_only=True)
ax[i] = add_stim_color_span(dataset, ax=ax[i], xlim=xlim)
ax[i].set_xlim(xlim)
ax[i].set_ylabel('')
ax[i].axes.get_yaxis().set_visible(False)
ax[i].legend(loc='upper left', fontsize=14)
sns.despine(ax=ax[i])
if include_running:
i += 1
ax[i].plot(dataset.stimulus_timestamps, dataset.running_speed)
ax[i] = add_stim_color_span(dataset, ax=ax[i], xlim=xlim)
ax[i] = restrict_axes(xmin_seconds, xmax_seconds, interval_seconds, ax=ax[i])
ax[i].set_ylabel('run speed\n(cm/s)')
# ax[i].axes.get_yaxis().set_visible(False)
sns.despine(ax=ax[i])
ax[i].set_xlabel('time (seconds)')
ax[0].set_title(dataset.ophys_experiment_id)
fig.tight_layout()
plt.subplots_adjust(wspace=0, hspace=0)
if save_dir is not None:
save_figure(fig, figsize, save_dir, 'example_traces', 'example_traces_' + str(xlim[0]) + suffix)
save_figure(fig, figsize, save_dir, 'example_traces',
str(dataset.ophys_experiment_id) + '_' + str(xlim[0]) + suffix)
plt.close()
[docs]class BehaviorOphysAnalysis(LazyPropertyMixin):
def __init__(self, session, api=None):
self.session = session
self.api = self if api is None else api
# self.active_cell_roi_ids = LazyProperty(self.api.get_active_cell_roi_ids, ophys_experiment_id=self.ophys_experiment_id)
[docs] def plot_example_traces_and_behavior(self, N=10):
dff_traces_df = self.session.dff_traces
dff_traces_df['mean'] = dff_traces_df['dff'].apply(np.mean)
dff_traces_df['std'] = dff_traces_df['dff'].apply(np.std)
dff_traces_df['snr'] = dff_traces_df['mean']/dff_traces_df['std']
active_cell_roi_ids = dff_traces_df.sort_values('snr', ascending=False)['cell_roi_id'].values[:N]
length_mins = 1
for xmin_seconds in np.arange(0, 5000, length_mins * 60):
plot_example_traces_and_behavior(self.session, active_cell_roi_ids, xmin_seconds, length_mins, cell_label=False, include_running=True)
if __name__ == "__main__":
session = BehaviorOphysSession(789359614)
analysis = BehaviorOphysAnalysis(session)
analysis.plot_example_traces_and_behavior()