Beispiel #1
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)
Beispiel #2
0
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')

# %% median removal for loadcelldata
# %% median correction
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).median()
LoadCellDf['y'] = LoadCellDf['y'] - LoadCellDf['y'].rolling(samples).median()

# %%
Spans = bhv.get_spans_from_names(LogDf, "REWARD_RIGHT_VALVE_ON", "REWARD_RIGHT_VALVE_OFF")
N = 2000
Data = sp.zeros((N,27,2))
Beispiel #3
0
 ####### ####### #     # ######  ### #     #  #####

"""

fd_path = utils.get_folder_dialog(initial_dir="/media/storage/shared-paton/georg/mesoscope_testings/behavior")

# Video data
video_path = fd_path / "bonsai_video.avi"
Vid = dlc_utils.read_video(str(video_path))

# 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')

# Moving average mean subtraction
samples = 1000 # ms
LoadCellDf['x'] = LoadCellDf['x'] - LoadCellDf['x'].rolling(samples).mean()
LoadCellDf['y'] = LoadCellDf['y'] - LoadCellDf['y'].rolling(samples).mean()

## 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
Beispiel #4
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)
Beispiel #5
0
## DeepLabCut data
#h5_path = utils.get_file_dialog()
#DlcDf = read_dlc_h5(h5_path)
#bodyparts = sp.unique([j[0] for j in DlcDf.columns[1:]]) # all body parts

# Video data
#video_path = fd_path / "bonsai_video.avi"
#Vid = dlc_utils.read_video(str(video_path))

# 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