Example #1
0
### Camera data
video_path = session_folder / "bonsai_video.avi"
Vid = dlc.read_video(str(video_path))

### Arduino data
log_path = session_folder / 'arduino_log.txt'
LogDf = bhv.get_LogDf_from_path(log_path)

### LoadCell Data
# LoadCellDf = bhv.parse_bonsai_LoadCellData(session_folder / 'bonsai_LoadCellData.csv')

# Syncer
from Utils import sync
cam_sync_event = sync.parse_cam_sync(session_folder / 'bonsai_frame_stamps.csv')
# lc_sync_event = sync.parse_harp_sync(session_folder / 'bonsai_harp_sync.csv')
arduino_sync_event = sync.get_arduino_sync(session_folder / 'arduino_log.txt')

Sync = Syncer()
Sync.data['arduino'] = arduino_sync_event['t'].values
# Sync.data['loadcell'] = lc_sync_event['t'].values
Sync.data['dlc'] = cam_sync_event.index.values # the frames are the DLC
Sync.data['cam'] = cam_sync_event['t'].values # used for what?
Sync.sync('arduino','dlc')
Sync.sync('arduino','cam')

DlcDf['t'] = Sync.convert(DlcDf.index.values, 'dlc', 'arduino')

# %% Dlc processing
# speed
for i,bp in enumerate(tqdm(bodyparts)):
    Vxy = sp.diff(DlcDf[bp][['x','y']].values,axis=0) / DlcDf['t'][:-1].values[:,sp.newaxis]
Example #2
0
# Arduino data
log_path = fd_path / 'arduino_log.txt'
LogDf = bhv.get_LogDf_from_path(log_path)

# LoadCell data
#LoadCellDf = bhv.parse_bonsai_LoadCellData(fd_path / 'bonsai_LoadCellData.csv')

## Synching 

from Utils import sync

# Parse sync events/triggers
cam_sync_event = sync.parse_cam_sync(fd_path / 'bonsai_frame_stamps.csv')
#lc_sync_event = sync.parse_harp_sync(fd_path / 'bonsai_harp_sync.csv')
arduino_sync_event = sync.get_arduino_sync(fd_path / 'arduino_log.txt')

# Get the values out of them
Sync = sync.Syncer()
Sync.data['arduino'] = arduino_sync_event['t'].values
#Sync.data['loadcell'] = lc_sync_event['t'].values
Sync.data['dlc'] = cam_sync_event.index.values # the frames are the DLC
Sync.data['cam'] = cam_sync_event['t'].values # used for what?

# Sync them all to master clock (arduino [FSM?] at ~1Khz) and convert
#LoadCellDf['t_loadcell'] = LoadCellDf['t'] # keeps the original

#Sync.sync('loadcell','arduino')
Sync.sync('dlc','arduino')

#LoadCellDf['t'] = Sync.convert(LoadCellDf['t'].values, 'loadcell', 'arduino')
Example #3
0
# folder = Path("/media/georg/htcondor/shared-paton/georg/reach_setup/buzz_across_freq_box1/2021-06-09_12-24-06_learn_to_choose")
# folder = Path("/media/georg/htcondor/shared-paton/georg/reach_setup/buzz_across_freq_box1_high_res/2021-06-09_12-24-06_learn_to_choose")
# folder = Path("/media/georg/htcondor/shared-paton/georg/reach_setup/buzz_across_freq_box1_high_res/2021-06-09_17-19-43_learn_to_choose")

# folder = Path("/media/georg/data/reaching_buzzer_check/2021-06-15_13-10-20_learn_to_choose") # the new internal loadcell
folder = Path("/media/georg/htcondor/shared-paton/georg/reach_setup/buzzer_check/2021-09-10_16-20-12_learn_to_choose_v3")
log_path = folder / "arduino_log.txt"

plot_dir = log_path.parent / 'plots'
os.makedirs(plot_dir, exist_ok=True)

LogDf = bhv.get_LogDf_from_path(log_path)

# %% Syncer
lc_sync_event = sync.parse_harp_sync(folder / 'bonsai_harp_sync.csv',trig_len=100, ttol=4)
arduino_sync_event = sync.get_arduino_sync(folder / 'arduino_log.txt')

Sync = Syncer()
Sync.data['arduino'] = arduino_sync_event['t'].values
Sync.data['loadcell'] = lc_sync_event['t'].values
Sync.sync('arduino','loadcell')

# DlcDf['t'] = Sync.convert(DlcDf.index.values, 'dlc', 'arduino')

# %%  Loadcell
csv_path = log_path.parent / "bonsai_LoadCellData.csv"
LoadCellDf = bhv.parse_bonsai_LoadCellData(csv_path)

# %%
LogDf['t_original'] = LogDf['t']
LogDf['t'] = Sync.convert(LogDf['t'].values,'arduino','loadcell')
Example #4
0
def plot_forces_on_init(session_folder, save=None):
    LogDf = bhv.get_LogDf_from_path(session_folder / "arduino_log.txt")

    ### LoadCell Data
    LoadCellDf = bhv.parse_bonsai_LoadCellData(session_folder / 'bonsai_LoadCellData.csv')

    # Syncer
    from Utils import sync
    lc_sync_event = sync.parse_harp_sync(session_folder / 'bonsai_harp_sync.csv', trig_len=100, ttol=5)
    arduino_sync_event = sync.get_arduino_sync(session_folder / 'arduino_log.txt')

    Sync = sync.Syncer()
    Sync.data['arduino'] = arduino_sync_event['t'].values
    Sync.data['loadcell'] = lc_sync_event['t'].values
    Sync.sync('arduino','loadcell')

    LogDf['t_orig'] = LogDf['t']
    LogDf['t'] = Sync.convert(LogDf['t'].values, 'arduino', 'loadcell')

    # preprocessing
    samples = 10000 # 10s buffer: harp samples at 1khz, arduino at 100hz, LC controller has 1000 samples in buffer
    LoadCellDf['x'] = LoadCellDf['x'] - LoadCellDf['x'].rolling(samples).mean()
    LoadCellDf['y'] = LoadCellDf['y'] - LoadCellDf['y'].rolling(samples).mean()

    # plot forces  
    times = LogDf.groupby('name').get_group('TRIAL_ENTRY_EVENT')['t'].values
    pre, post = -1000, 1000
    fig, axes = plt.subplots(nrows=2,sharex=True,sharey=False)

    x_avgs = []
    y_avgs = []
    for i,t in enumerate(tqdm(times)):
        Df = bhv.time_slice(LoadCellDf, t+pre, t+post, reset_index=False)
        # these colors need to be thorougly checked
        axes[0].plot(Df['t'].values - t, Df['x'])
        axes[1].plot(Df['t'].values - t, Df['y'])

        x_avgs.append(Df['x'].values)
        y_avgs.append(Df['y'].values)

    x_avgs = np.average(np.array(x_avgs),axis=0)
    y_avgs = np.average(np.array(y_avgs),axis=0)

    tvec = np.linspace(pre,post,x_avgs.shape[0])
    axes[0].plot(tvec, x_avgs, color='k',lw=2)
    axes[1].plot(tvec, y_avgs, color='k',lw=2)

    kws = dict(linestyle=':',lw=1, alpha=0.8, color='k')
    for ax in axes:
        ax.axhline(-500, **kws)
        ax.axvline(0, **kws)

    # deco
    Session = utils.Session(session_folder)
    Animal = utils.Animal(session_folder.parent)
    title = ' - '.join([Animal.display(), Session.date, 'day: %s'% Session.day])

    for ax in axes:
        ax.set_ylim(-2500,2500)
        ax.set_ylabel('Force [au]')
    axes[1].set_xlabel('time (ms)')

    sns.despine(fig)
    fig.suptitle(title)
    fig.tight_layout()
    fig.subplots_adjust(top=0.9)

    if save is not None:
        os.makedirs(session_folder / 'plots', exist_ok=True)
        plt.savefig(save, dpi=600)
        plt.close(fig)
Example #5
0
def plot_init_hist(session_folder, save=None):

    LogDf = bhv.get_LogDf_from_path(session_folder / "arduino_log.txt")

    # Sync first
    loadcell_sync_event = sync.parse_harp_sync(session_folder /
                                               'bonsai_harp_sync.csv',
                                               trig_len=100,
                                               ttol=5)
    arduino_sync_event = sync.get_arduino_sync(session_folder /
                                               'arduino_log.txt')

    Sync = sync.Syncer()
    Sync.data['arduino'] = arduino_sync_event['t'].values
    Sync.data['loadcell'] = loadcell_sync_event['t'].values
    success = Sync.sync('arduino', 'loadcell')

    # abort if sync fails
    if not success:
        utils.printer(
            "trying to plot_init_hist, but failed to sync in file %s, - aborting"
            % session_folder)
        return None

    LogDf['t_orig'] = LogDf['t']
    LogDf['t'] = Sync.convert(LogDf['t'].values, 'arduino', 'loadcell')

    LoadCellDf = bhv.parse_bonsai_LoadCellData(session_folder /
                                               'bonsai_LoadCellData.csv')

    # preprocessing
    samples = 10000  # 10s buffer: harp samples at 1khz, arduino at 100hz, LC controller has 1000 samples in buffer
    LoadCellDf['x'] = LoadCellDf['x'] - LoadCellDf['x'].rolling(samples).mean()
    LoadCellDf['y'] = LoadCellDf['y'] - LoadCellDf['y'].rolling(samples).mean()

    # smoothing forces
    F = LoadCellDf[['x', 'y']].values
    w = np.ones(100)
    F[:, 0] = np.convolve(F[:, 0], w, mode='same')
    F[:, 1] = np.convolve(F[:, 1], w, mode='same')

    # detect pushes
    th = 500
    L = F < -th
    events = np.where(np.diff(np.logical_and(L[:, 0], L[:, 1])) == 1)[0]
    times = [LoadCellDf.iloc[int(i)]['t'] for i in events]

    # histogram of pushes pre vs pushes post trial available
    trial_times = bhv.get_events_from_name(LogDf,
                                           'TRIAL_AVAILABLE_EVENT')['t'].values
    post = []
    pre = []

    for t in trial_times:
        dt = times - t
        try:
            post.append(np.min(dt[dt > 0]))
        except ValueError:
            # thrown when no more pushes after last init
            pass
        try:
            pre.append(np.min(-1 * dt[dt < 0]))
        except ValueError:
            # thrown when no pushes before first init
            pass

    fig, axes = plt.subplots()
    bins = np.linspace(0, 5000, 25)
    axes.hist(pre, bins=bins, alpha=0.5, label='pre')
    axes.hist(post, bins=bins, alpha=0.5, label='post')
    axes.set_xlabel('time (ms)')
    axes.set_ylabel('count')
    axes.legend()

    Session = utils.Session(session_folder)
    Animal = utils.Animal(session_folder.parent)
    title = ' - '.join(
        [Animal.display(), Session.date,
         'day: %s' % Session.day])

    sns.despine(fig)
    fig.suptitle(title)
    fig.tight_layout()
    fig.subplots_adjust(top=0.85)

    if save is not None:
        os.makedirs(save.parent, exist_ok=True)
        plt.savefig(save, dpi=600)
        plt.close(fig)