def update(self): try: sessions_df = utils.get_sessions(self.Animal.folder) lines = sessions_df['task'].tolist() lines = '\n'.join(lines) model = PandasModel(sessions_df[['date','time','task']]) self.Table.setModel(model) except ValueError: pass
def reaches_during_delay_across_sess(animal_fd_path, tasks_names, init_day_idx): SessionsDf = utils.get_sessions(animal_fd_path) animal_meta = pd.read_csv(animal_fd_path / 'animal_meta.csv') # Filter sessions to the ones of the task we want to see FilteredSessionsDf = pd.concat( [SessionsDf.groupby('task').get_group(name) for name in tasks_names]) log_paths = [ Path(path) / 'arduino_log.txt' for path in FilteredSessionsDf['path'] ] fig, axes = plt.subplots(ncols=2, figsize=[6, 4], sharey=True, sharex=True) colors = sns.color_palette(palette='turbo', n_colors=len(log_paths)) for j, log_path in enumerate(log_paths[init_day_idx:]): LogDf = bhv.get_LogDf_from_path(log_path) # ADD SINGLE GO_CUE_EVENT LogDf = bhv.add_go_cue_LogDf(LogDf) TrialSpans = bhv.get_spans_from_names(LogDf, "TRIAL_ENTRY_STATE", "ITI_STATE") TrialDfs = [] for i, row in tqdm(TrialSpans.iterrows(), position=0, leave=True): TrialDfs.append(bhv.time_slice(LogDf, row['t_on'], row['t_off'])) metrics = (met.get_start, met.get_stop, met.get_correct_side, met.get_outcome, met.get_interval_category, met.get_chosen_side, met.has_reach_left, met.has_reach_right) SessionDf = bhv.parse_trials(TrialDfs, metrics) CDF_of_reaches_during_delay(SessionDf, TrialDfs, axes=axes, color=colors[j], alpha=0.75, label='day ' + str(j + 1)) fig.suptitle('CDF of first reach split on trial type \n' + animal_meta['value'][5] + '-' + animal_meta['value'][0]) axes[0].set_ylabel('Fraction of trials') axes[0].legend(frameon=False, fontsize='x-small') fig.tight_layout() return axes
def load_last_vars(self): """ try to get arduino variables from last run for the task only loads, does not send! """ config = self.parent().config folder = Path( config['paths']['animals_folder']) / config['current']['animal'] SessionsDf = utils.get_sessions(folder) try: previous_sessions = SessionsDf.groupby('task').get_group( config['current']['task']) except KeyError: utils.printer( "trying to use last vars, but animal has not been run on this task before.", 'error') return None # to allow for this functionalty while task is running if self.parent().parent().running: ix = -2 else: ix = -1 try: prev_session_path = Path(previous_sessions.iloc[ix]['path']) prev_vars_path = prev_session_path / config['current'][ 'task'] / "Arduino" / "src" / "interface_variables.h" if prev_vars_path.exists(): prev_vars = utils.parse_arduino_vars(prev_vars_path) return prev_vars else: utils.printer( "found variables from last session, but can't set them", "error") return None except IndexError: # thrown when there is no previous session return None
def grasp_dur_across_sess(animal_fd_path, tasks_names): SessionsDf = utils.get_sessions(animal_fd_path) animal_meta = pd.read_csv(animal_fd_path / 'animal_meta.csv') # Filter sessions to the ones of the task we want to see FilteredSessionsDf = pd.concat( [SessionsDf.groupby('task').get_group(name) for name in tasks_names]) log_paths = [ Path(path) / 'arduino_log.txt' for path in FilteredSessionsDf['path'] ] fig, axes = plt.subplots(ncols=2, figsize=[8, 4], sharey=True, sharex=True) sides = ['LEFT', 'RIGHT'] Df = [] # gather data for day, log_path in enumerate(log_paths): LogDf = bhv.get_LogDf_from_path(log_path) TrialSpans = bhv.get_spans_from_names(LogDf, "TRIAL_ENTRY_STATE", "ITI_STATE") TrialDfs = [] for i, row in tqdm(TrialSpans.iterrows(), position=0, leave=True): TrialDfs.append(bhv.time_slice(LogDf, row['t_on'], row['t_off'])) metrics = (met.get_start, met.get_stop, met.get_correct_side, met.get_outcome, met.get_interval_category, met.get_chosen_side, met.has_reach_left, met.has_reach_right) SessionDf = bhv.parse_trials(TrialDfs, metrics) for side in sides: event_on, event_off = 'REACH_' + str(side) + '_ON', 'REACH_' + str( side) + '_OFF' # event names # reaches grasp_spansDf = bhv.get_spans_from_names(LogDf, event_on, event_off) reach_durs = np.array(grasp_spansDf['dt'].values, dtype=object) Df.append([reach_durs], columns=['durs'], ignore_index=True) # grasps choiceDf = bhv.groupby_dict( SessionDf, dict(has_choice=True, chosen_side=side.lower())) grasp_durs = choiceDf[~choiceDf['grasp_dur'].isna( )]['grasp_dur'].values # filter out Nans Df.append([grasp_durs, 'g', side], columns=['durs', 'type', 'side'], ignore_index=True) sns.violinplot(data=Df[Df['type'] == 'r'], x=day, y='durs', hue='side', split=True, cut=0, legend='reaches') sns.violinplot(data=Df[Df['type'] == 'g'], x=day, y='durs', hue='side', split=True, cut=0, legend='grasps') fig.suptitle('CDF of first reach split on trial type \n' + animal_meta['value'][5] + '-' + animal_meta['value'][0]) axes.legend return axes
Nicknames = ['Lifeguard', 'Lumberjack', 'Teacher', 'Plumber', 'Poolboy', 'Policeman', 'Therapist'] # Nicknames = ['Therapist'] task_name = 'learn_to_choose_v2' # get animals by Nickname Animals_folder = Path("/media/georg/htcondor/shared-paton/georg/Animals_reaching") Animals = utils.get_Animals(Animals_folder) Animals = [a for a in Animals if a.Nickname in Nicknames] overwrite = False for i, Animal in enumerate(Animals): utils.printer("processing animal %s" % Animal, 'msg') SessionsDf = utils.get_sessions(Animal.folder).groupby('task').get_group(task_name) SessionsDf = SessionsDf.reset_index() for i, row in SessionsDf.iterrows(): session_folder = Path(row['path']) Session = utils.Session(session_folder) # session overview if 0: outpath = Animal.folder / 'plots' / 'session_overviews' / ('session_overview_%s_%s_day_%s.png' % (Session.date, Session.time, Session.day)) if not outpath.exists() or overwrite: plot_session_overview(session_folder, save=outpath) # init histograms if 1: outpath = Animal.folder / 'plots' / 'init_histograms' / ('init_histogram_%s_%s_day_%s.png' % (Session.date, Session.time, Session.day))
def plot_bias_over_sessions(Animal_folder, task_name, save=None): Animal = utils.Animal(Animal_folder) # get BiasDfs SessionsDf = utils.get_sessions(Animal.folder).groupby('task').get_group(task_name) BiasDfs = [] autodeliver = [] p_lefts = [] for i, row in SessionsDf.iterrows(): session_folder = Path(row['path']) LogDf = bhv.get_LogDf_from_path(session_folder / "arduino_log.txt") LogDf['min'] = LogDf['t'] / 60000 # one sesion bias BiasDf = LogDf.groupby('var').get_group('bias') t_min = BiasDf['t'].values[0] t_max = BiasDf['t'].values[-1] BiasDf['t_rel'] = (BiasDf['t'].values - t_min)/t_max BiasDfs.append(BiasDf) # get autodeliver value for session fname = session_folder / task_name / 'Arduino' / 'src' / 'interface_variables.h' value = utils.parse_arduino_vars(fname).groupby('name').get_group('autodeliver_rewards').iloc[0]['value'] autodeliver.append(value) # get static bias corr if possible fname = session_folder / task_name / 'Arduino' / 'src' / 'interface_variables.h' try: p_left = utils.parse_arduino_vars(fname).groupby('name').get_group('p_left').iloc[0]['value'] except KeyError: p_left = 0.5 p_lefts.append(p_left) fig, axes = plt.subplots(nrows=2, sharex=True, gridspec_kw=dict(height_ratios=(0.1,1))) w = 0.5 axes[0].set_ylabel('auto\nrewards') axes[0].set_xticks([]) axes[0].set_yticks([]) for i in range(SessionsDf.shape[0]): BiasDf = BiasDfs[i] tvec = np.linspace(i-w/2,i+w/2,BiasDf.shape[0]) axes[1].plot(tvec, BiasDf['value']) axes[1].plot(i, np.average(BiasDf['value']),'o',color='k') axes[1].set_ylim(-0.1,1.1) if autodeliver[i] == 1: axes[0].plot(i,0,'o',color='black') # axes[0].text(i,0.03,str(p_lefts[i]),ha='center') axes[1].axhline(0.5,linestyle=':',lw=1,alpha=0.5,color='k') axes[1].set_xticks(range(SessionsDf.shape[0])) axes[1].set_xticklabels(SessionsDf['date'],rotation=45, ha="right") axes[1].set_xlabel('date') axes[1].set_ylabel('bias\n1=right') title = Animal.Nickname + ' - bias over sessions' axes[0].set_title(title) sns.despine(fig) fig.tight_layout() fig.subplots_adjust(hspace=0.1) if save is not None: os.makedirs(save.parent, exist_ok=True) plt.savefig(save, dpi=600) plt.close(fig) # %% # Animal_folder = Path("/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-02911_Lumberjack") # task_name = 'learn_to_choose_v2' # plot_bias_over_sessions(Animal_folder, task_name=task_name, save=None)
######## ## ## ## ## ## ## ## ## ### ## ## ## ## ## #### ## ######## ## ## ## ## ## ## ## ## ## ## #### ## ## ## ## ## ### ## ## ####### ## ## """ # Borges = Path("/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-01977") Marquez = Path("/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-01975") # TODO TO BE CHANGED BY PACO task_name = 'learn_to_fixate_discrete_v1' SessionsDf = utils.get_sessions(Marquez) SessionsDf = SessionsDf.groupby('task').get_group('learn_to_fixate_discrete_v1') log_path = Path(SessionsDf.iloc[-1]['path']) / 'arduino_log.txt' LogDf = bhv.get_LogDf_from_path(log_path) animal = utils.Animal(log_path.parent) date = log_path.parent.stem.split('_')[0] # slice into trials def get_SessionDf(LogDf, metrics, trial_entry_event="TRIAL_AVAILABLE_STATE", trial_exit_event="ITI_STATE"): TrialSpans = bhv.get_spans_from_names(LogDf, trial_entry_event, trial_exit_event) TrialDfs = [] for i, row in tqdm(TrialSpans.iterrows(),position=0, leave=True):
animals = [ 'JJP-02909_Lifeguard','JJP-02911_Lumberjack','JJP-02912_Teacher','JJP-02994_Plumber', 'JJP-02995_Poolboy','JJP-02996_Policeman','JJP-02997_Therapist'] animals_dir_plots = animals_dir / 'plots' os.makedirs(animals_dir_plots, exist_ok=True) no_sessions_to_analyze = 10 n_rews_collected = np.zeros((len(animals), no_sessions_to_analyze)) n_anticipatory = np.zeros((len(animals), no_sessions_to_analyze)) no_trials = np.zeros((len(animals), no_sessions_to_analyze)) session_length = np.zeros((len(animals), no_sessions_to_analyze)) for j, animal in enumerate(animals): SessionsDf = utils.get_sessions(animals_dir / animal) # Filter sessions to the ones of the task we want to see task_name = ['learn_to_choose_v2'] FilteredSessionsDf = pd.concat([SessionsDf.groupby('task').get_group(name) for name in task_name]) log_paths = [Path(path)/'arduino_log.txt' for path in FilteredSessionsDf['path']] for k,log_path in enumerate(tqdm(log_paths[:no_sessions_to_analyze], position=0, leave=True, desc=animal)): LogDf = bhv.get_LogDf_from_path(log_path) # Getting metrics TrialSpans = bhv.get_spans_from_names(LogDf, "TRIAL_ENTRY_STATE", "ITI_STATE") TrialDfs = [] for i, row in TrialSpans.iterrows():
folders = dict( Lifeguard="/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-02909", Lumberjack="/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-02911", Teacher="/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-02912", Plumber="/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-02994", Poolboy="/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-02995", Policeman="/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-02996", Therapist="/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-02997") # %% for animal, folder in folders.items(): print("processing %s" % animal) folder = Path(folder) Animal = utils.Animal(folder) SessionsDf = utils.get_sessions(Animal.folder).groupby('task').get_group('learn_to_choose_v2') SessionsDf = SessionsDf.reset_index() for day, row in SessionsDf.iterrows(): print(day) folder = Path(SessionsDf.loc[day,'path']) LogDf = bhv.get_LogDf_from_path(folder / "arduino_log.txt") LogDf['min'] = LogDf['t'] / 60000 # check each reach ReachesLeftDf = bhv.get_spans_from_names(LogDf, "REACH_LEFT_ON", "REACH_LEFT_OFF") # drop invalid min_th = 5 max_th = 2000
import behav_plotters_reach as bhv_plt_reach """ # ####### # ###### ### # # ##### # # # # # # # # ## # # # # # # # # # # # # # # # # # # # # # # # # # # # #### # # # ####### # # # # # # # # # # # # # # # # # ## # # ####### ####### # # ###### ### # # ##### """ animal_fd_path = sys.argv[1] fd_path = Path(utils.get_sessions(animal_fd_path).iloc[-1]['path']) # Get last session fd_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') # Parse sync events/triggers 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
import os from Utils import utils # %% folder = Path("/home/georg/data/grasping_animals/") folder = Path("/media/georg/htcondor/shared-paton/georg/Animals_reaching") Animals = utils.get_Animals(folder) # %% # lifeguard folder = "/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-02909" Animal = utils.Animal(folder) day = 0 folder = Path(utils.get_sessions(Animal.folder).path[day]) LogDf = bhv.get_LogDf_from_path(folder / "arduino_log.txt") LogDf['min'] = LogDf['t'] / 60000 # %% check each reach ReachesDf = bhv.get_spans_from_names(LogDf, "REACH_ON", "REACH_OFF") # drop invalid min_th = 5 max_th = 2000 binds = np.logical_and(ReachesDf['dt'].values > min_th, ReachesDf['dt'].values < max_th) ReachesDf = ReachesDf.loc[binds]
# %% folder = Path("/home/georg/data/grasping_animals/") folder = Path("/media/georg/htcondor/shared-paton/georg/Animals_reaching") Animals = utils.get_Animals(folder) # %% # Lifeguard folder = "/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-02909" # Lumberjack folder = "/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-02911" # Teacher folder = "/media/georg/htcondor/shared-paton/georg/Animals_reaching/JJP-02912" Animal = utils.Animal(folder) SessionsDf = utils.get_sessions(Animal.folder).groupby('task').get_group('learn_to_grasp') for day, row in SessionsDf.iterrows(): folder = Path(utils.get_sessions(Animal.folder).path[day]) LogDf = bhv.get_LogDf_from_path(folder / "arduino_log.txt") LogDf['min'] = LogDf['t'] / 60000 # check each reach ReachesDf = bhv.get_spans_from_names(LogDf, "REACH_ON", "REACH_OFF") # drop invalid min_th = 5 max_th = 2000 binds = np.logical_and(ReachesDf['dt'].values > min_th, ReachesDf['dt'].values < max_th)
# # # ###### # # ##### ##### ##### ##### ##### ##### # # # # # # ##### ####### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ## # # # # ##### # # ####### ##### ##### ##### ####### ##### ##### ### ####### # # ##### """ # %% Obtain log_paths and plot dirs animal_fd_path = utils.get_folder_dialog(initial_dir="/media/storage/shared-paton/georg/Animals_reaching/") across_session_plot_dir = animal_fd_path / 'plots' animal_meta = pd.read_csv(animal_fd_path / 'animal_meta.csv') nickname = animal_meta[animal_meta['name'] == 'Nickname']['value'].values[0] os.makedirs(across_session_plot_dir, exist_ok=True) # %% across sessions - plot weight SessionsDf = utils.get_sessions(animal_fd_path) Df = pd.read_csv(animal_fd_path / 'animal_meta.csv') ini_weight = float(Df[Df['name'] == 'Weight']['value']) for i,row in SessionsDf.iterrows(): try: path = row['path'] Df = pd.read_csv(Path(path) / 'animal_meta.csv') current_weight = float(Df[Df['name'] == 'current_weight']['value']) SessionsDf.loc[row.name,'weight'] = current_weight SessionsDf.loc[row.name,'weight_frac'] = current_weight / ini_weight except: pass # Formatting fig, axes = plt.subplots()