def reaction_times_first_step(sessions): median_reaction_times = np.zeros([len(sessions),4]) all_reaction_times = [] for i,session in enumerate(sessions): event_times = ut.get_event_times(session.time_stamps, session.event_codes, session.IDs) ITI_start_times = event_times['ITI_start'] center_poke_times = sorted(np.hstack((event_times['high_poke'], event_times['low_poke']))) reaction_times = 1000 * _latencies(ITI_start_times, center_poke_times)[1:-1] all_reaction_times.append(reaction_times) transitions = (session.blocks['trial_trans_state'] == session.CTSO['transitions'])[:len(reaction_times)] # Transitions common/rare. outcomes = session.CTSO['outcomes'][:len(reaction_times)].astype(bool) median_reaction_times[i, 0] = np.median(reaction_times[ transitions & outcomes]) # Common transition, rewarded. median_reaction_times[i, 1] = np.median(reaction_times[~transitions & outcomes]) # Rare transition, rewarded. median_reaction_times[i, 2] = np.median(reaction_times[ transitions & ~outcomes]) # Common transition, non-rewarded. median_reaction_times[i, 3] = np.median(reaction_times[~transitions & ~outcomes]) # Rare transition, non-rewarded. mean_RTs = np.mean(median_reaction_times,0) SEM_RTs = np.sqrt(np.var(median_reaction_times,0)/len(sessions)) p.figure(1) p.clf() p.title('First step reaction times') p.bar([1,2,3,4], mean_RTs, yerr = SEM_RTs) p.ylim(min(mean_RTs) * 0.8, max(mean_RTs) * 1.1) p.xticks([1.4, 2.4, 3.4, 4.4], ['Com. Rew.', 'Rare Rew.', 'Com. Non.', 'Rare. Non.']) p.xlim(0.8,5) p.ylabel('Reaction time (ms)') all_reaction_times = np.hstack(all_reaction_times) bin_edges = np.arange(0,3001) rt_hist = np.histogram(all_reaction_times, bin_edges)[0] cum_rt_hist = np.cumsum(rt_hist) / float(len(all_reaction_times)) p.figure(2) p.clf() p.plot(bin_edges[:-1],cum_rt_hist) p.ylim(0,1) p.xlabel('Time from ITI start (ms)') p.ylabel('Cumumative fraction of first central pokes.')
def reaction_times_second_step(sessions, fig_no = 1): 'Reaction times for second step pokes as function of common / rare transition.' sec_step_IDs = ut.get_IDs(sessions[0].IDs, ['right_active', 'left_active']) median_RTs_common = np.zeros(len(sessions)) median_RTs_rare = np.zeros(len(sessions)) for i,session in enumerate(sessions): event_times = ut.get_event_times(session.time_stamps, session.event_codes, session.IDs) left_active_times = event_times['left_active'] right_active_times = event_times['right_active'] left_reaction_times = _latencies(left_active_times, event_times['left_poke']) right_reaction_times = _latencies(right_active_times, event_times['right_poke']) ordered_reaction_times = np.hstack((left_reaction_times,right_reaction_times))\ [np.argsort(np.hstack((left_active_times,right_active_times)))] transitions = session.blocks['trial_trans_state'] == session.CTSO['transitions'] # common vs rare. median_RTs_common[i] = np.median(ordered_reaction_times[ transitions]) median_RTs_rare[i] = np.median(ordered_reaction_times[~transitions]) mean_RT_common = 1000 * np.mean(median_RTs_common) mean_RT_rare = 1000 * np.mean(median_RTs_rare) SEM_RT_common = 1000 * np.sqrt(np.var(median_RTs_common/len(sessions))) SEM_RT_rare = 1000 * np.sqrt(np.var(median_RTs_rare /len(sessions))) p.figure(fig_no) p.bar([1,2],[mean_RT_common, mean_RT_rare], yerr = [SEM_RT_common,SEM_RT_rare]) p.xlim(0.8,3) p.ylim(mean_RT_common * 0.8, mean_RT_rare * 1.1) p.xticks([1.4, 2.4], ['Common', 'Rare']) p.title('Second step reaction times') p.ylabel('Reaction time (ms)') print('Paired t-test P value: {}'.format(ttest_rel(median_RTs_common, median_RTs_rare)[1]))