Example #1
0
    def update(i):
        Frame = dlc.get_frame(Vid,i)
        im.set_data(Frame)

        # frame counter
        frame_counter.set_text("frame: %i - %i/%i" % (i, ix.index(i),len(ix)))
        t_abs = Sync.convert(i,'dlc','arduino') / 1000
        m = Sync.pairs[('dlc','arduino')][0]
        t_rel = (ix.index(i) * m) / 1000
        time_counter.set_text("time: %.2f - %.2f/%.2f" % (t_abs, t_rel,len(ix)*m/1000))

        # body parts
        for j, bp in enumerate(bodyparts):
            data = DlcDfSlice[bp].loc[i]
            if data['likelihood'] > p:
                bp_markers[j].set_data(data['x'], data['y'])
            else:
                bp_markers[j].set_data(sp.nan, sp.nan)
        
        # trace
        for j, bp in enumerate(bodyparts):
            i0 = i - n_segments*trace_len
            data = DlcDfSlice[bp].loc[i0:i]
            data.loc[data['likelihood'] < p] = sp.nan
            data = data[['x','y']].values[::-1,:]
            segments = bp_traces[j].get_segments()
            for k in range(n_segments):
                try:
                    segments[-k] = data[k*trace_len-5:(k+1)*trace_len+5,:]
                except:
                    pass
            bp_traces[j].set_segments(segments)

        for j, event in enumerate(display_events):
            t = Sync.convert(i, 'dlc', 'arduino')
            if sp.any(sp.logical_and(t > event_times[j], t < event_times[j] + event_display_dur)):
                event_texts[j].set_color(event_colors[event])
            else:
                event_texts[j].set_color(inactive_color)

        return (im, frame_counter, time_counter) + tuple(bp_markers) + tuple(bp_traces) + tuple(event_texts)
Example #2
0
def make_annotated_video(Vid, t_on, t_off, LogDf, DlcDf, fps=20, save=None):
    LogDfSlice = bhv.time_slice(LogDf, t_on, t_off)
    DlcDfSlice = bhv.time_slice(DlcDf, t_on, t_off)

    # what events to display
    display_events = list(LogDfSlice.name.unique())
    # display_events = ['GO_CUE_SHORT_EVENT', 'GO_CUE_LONG_EVENT', 'CHOICE_CORRECT_EVENT', 'CHOICE_INCORRECT_EVENT', 'REWARD_LEFT_EVENT','REWARD_RIGHT_EVENT', 'REACH_LEFT_ON', 'REACH_LEFT_OFF', 'REACH_RIGHT_ON', 'REACH_RIGHT_OFF']
    if sp.nan in display_events:
        display_events.remove(np.nan)

    frame_on = DlcDfSlice.index[0]
    frame_off = DlcDfSlice.index[-1]
    ix = list(range(frame_on, frame_off))

    # plotting
    fig, ax = plt.subplots()
    ax.axis('off')

    if save is not None:
        import matplotlib as mpl
        # from matplotlib.animation import FFMpegWriter as AniWriter
        # Writer = AniWriter(fps=20, bitrate=7500, codec="h264", extra_args=['-pix_fmt','yuv420p'])
        # Writer = AniWriter(fps=20, bitrate=10000, codec="h264")
        from matplotlib.animation import FFMpegFileWriter as AniWriter
        Writer = AniWriter(fps=fps, codec="h264", bitrate=-1)
        mpl.rcParams['animation.ffmpeg_path'] = "/usr/bin/ffmpeg"

    # image
    ax.set_aspect('equal')
    frame = dlc.get_frame(Vid, ix[0])
    im = ax.imshow(frame, cmap='gray')

    # body parts
    bp_left = [bp for bp in bodyparts if bp.endswith('L')]
    bp_right = [bp for bp in bodyparts if bp.endswith('R')]
    c_l = sns.color_palette('viridis', n_colors=len(bp_left))
    c_r = sns.color_palette('magma', n_colors=len(bp_right))
    bp_cols = dict(zip(bp_left+bp_right,c_l+c_r))

    bp_markers = []
    for i, bp in enumerate(bodyparts):
        marker, = ax.plot([],[], 'o', color=bp_cols[bp], markersize=10)
        bp_markers.append(marker)

    # traces
    from matplotlib.collections import LineCollection
    n_segments = 10
    trace_len = 3
    lws = sp.linspace(0,5,n_segments)
    bp_traces = []
    for i, bp in enumerate(bodyparts):
        segments = []
        for j in range(n_segments):
            segment = sp.zeros((trace_len,2))
            segments.append(segment)
        
        lc = LineCollection(sp.array(segments),linewidths=lws,color=bp_cols[bp], alpha=0.75)
        bp_traces.append(lc)
        ax.add_artist(lc)
    p = 0.0

    # frame text
    inactive_color = 'white'
    frame_counter = ax.text(5, frame.shape[0]-25, '', color=inactive_color)
    time_counter = ax.text(5, frame.shape[0]-5, '', color=inactive_color)

    # event text annotations
    # color setup
    c = sns.color_palette('husl', n_colors=len(display_events))
    event_colors = dict(zip(display_events,c))
    event_display_dur = 50 # ms

    event_texts = []
    event_times = []
    for i, event in enumerate(display_events):
        # times 
        try:
            times = LogDfSlice.groupby('name').get_group(event)['t'].values
        except KeyError:
            times = [np.nan]
        event_times.append(times)

        # plot
        # bg_text = ax.text(10, i*20 + 20, event, color='black', fontweight='heavy', fontsize=6)
        text = ax.text(10, i*20 + 20, event, color=inactive_color, fontweight='heavy', fontsize=6)
        event_texts.append(text)

    fig.tight_layout()

    # the animation function
    def update(i):
        Frame = dlc.get_frame(Vid,i)
        im.set_data(Frame)

        # frame counter
        frame_counter.set_text("frame: %i - %i/%i" % (i, ix.index(i),len(ix)))
        t_abs = Sync.convert(i,'dlc','arduino') / 1000
        m = Sync.pairs[('dlc','arduino')][0]
        t_rel = (ix.index(i) * m) / 1000
        time_counter.set_text("time: %.2f - %.2f/%.2f" % (t_abs, t_rel,len(ix)*m/1000))

        # body parts
        for j, bp in enumerate(bodyparts):
            data = DlcDfSlice[bp].loc[i]
            if data['likelihood'] > p:
                bp_markers[j].set_data(data['x'], data['y'])
            else:
                bp_markers[j].set_data(sp.nan, sp.nan)
        
        # trace
        for j, bp in enumerate(bodyparts):
            i0 = i - n_segments*trace_len
            data = DlcDfSlice[bp].loc[i0:i]
            data.loc[data['likelihood'] < p] = sp.nan
            data = data[['x','y']].values[::-1,:]
            segments = bp_traces[j].get_segments()
            for k in range(n_segments):
                try:
                    segments[-k] = data[k*trace_len-5:(k+1)*trace_len+5,:]
                except:
                    pass
            bp_traces[j].set_segments(segments)

        for j, event in enumerate(display_events):
            t = Sync.convert(i, 'dlc', 'arduino')
            if sp.any(sp.logical_and(t > event_times[j], t < event_times[j] + event_display_dur)):
                event_texts[j].set_color(event_colors[event])
            else:
                event_texts[j].set_color(inactive_color)

        return (im, frame_counter, time_counter) + tuple(bp_markers) + tuple(bp_traces) + tuple(event_texts)

    ani = FuncAnimation(fig, update, frames=ix, blit=True, interval=1)
    if save is not None:
        utils.printer("saving video to %s" % save, 'msg')
        ani.save(save, writer=Writer)
        plt.close(fig)
Example #3
0
fig.tight_layout()

# %%
"""
.########..##........#######..########.########.####.##....##..######..
.##.....##.##.......##.....##....##.......##.....##..###...##.##....##.
.##.....##.##.......##.....##....##.......##.....##..####..##.##.......
.########..##.......##.....##....##.......##.....##..##.##.##.##...####
.##........##.......##.....##....##.......##.....##..##..####.##....##.
.##........##.......##.....##....##.......##.....##..##...###.##....##.
.##........########..#######.....##.......##....####.##....##..######..
"""
# %% plot a single frame with DLC markers and Skeleton
fig, axes = plt.subplots()
i = 8000 # frame index
Frame = dlc_utils.get_frame(Vid, i)
axes = dlc_utils.plot_frame(Frame, axes=axes)
axes = dlc_utils.plot_bodyparts(bodyparts, DlcDf, i, axes=axes)

# %% plot a heatmap of movement for both paws on a 2D background
fig, axes = plt.subplots()

i = 4000 # frame index
Frame = dlc_utils.get_frame(Vid, i)
axes = dlc_utils.plot_frame(Frame, axes=axes)
axes = dlc_utils.plot_trajectories(DlcDf, paws, axes=axes,lw=0.025)
axes.axis('off')
axes.set_title('Whole session heatmap of paw placement')

plt.savefig(plot_dir / ('heatmap_both_paws.png'), dpi=600)
Example #4
0
# trial selection
# SDf = bhv.groupby_dict(SessionDf, dict(outcome='correct', correct_side='left'))
# TrialDf = TrialDfs[SDf.index[0]]
TrialDf = TrialDfs[0]

Df = bhv.event_slice(TrialDf, 'TRIAL_ENTRY_EVENT', 'ITI_STATE')
t_on = Df.iloc[0]['t']
t_off = Df.iloc[-1]['t']

# %% static image with trajectory between t_on and t_off

bp_cols = make_bodypart_colors(bodyparts)

fig, axes = plt.subplots()
frame_ix = Sync.convert(t_on, 'arduino', 'dlc')
frame = dlc.get_frame(Vid, frame_ix)
dlc.plot_frame(frame, axes=axes)
dlc.plot_bodyparts(bodyparts, DlcDf, frame_ix, colors=bp_cols, axes=axes)

# trajectory
DlcDfSlice = bhv.time_slice(DlcDf, t_on, t_off)
dlc.plot_trajectories(DlcDfSlice, bodyparts, axes=axes, colors=bp_cols, lw=1, p=0.99)

# %% plot all of the selected trial type

# SDf = bhv.groupby_dict(SessionDf, dict(outcome='correct', correct_side='right', paw_resting=False))

# trial selection
SDf = bhv.groupby_dict(SessionDf, dict(has_choice=True, correct_side='left', outcome='correct'))

# plot some random frame
Example #5
0
fig, axes = plt.subplots(figsize=(4,3))
for i in np.arange(0,n_clusters+1):
    axes.scatter(i, np.sum(code == i))
axes.set_ylabel('No. neurons')
axes.set_xlabel('No. of clusters')
axes.set_title('How many neurons belong to each cluster')
fig.tight_layout()

# plots image of brain
neural_vid_path = neural_data_path / "reconstructed.avi"
neural_vid = dlc_utils.read_video(str(neural_vid_path))

fig, axes = plt.subplots(figsize=(5,5))
frame_ix = 10
frame = dlc_utils.get_frame(neural_vid, frame_ix)
dlc_utils.plot_frame(frame, axes=axes)

# for each cluster plot pixel color coded by cluster
for i in np.arange(0,n_clusters+1):
    cluster_idxs = code == i
    axes.scatter(   neuron_coords[cluster_idxs,0],neuron_coords[cluster_idxs,1], s = 1,
                    alpha = 0.75, label = i)

axes.legend(frameon=False, bbox_to_anchor=(1,1), title = 'Cluster No.')
fig.tight_layout()

# %% Defining groups based on brain areas

# %% Neurons significantly modulated by events