def plot_filtered_performance_calendar(subj, df, num_days=7, **kwargs): ''' plots a calendar view of the performance for a subject on the past num_days. ''' return plot_performance_calendar( subj, utils.filter_normal_trials(utils.filter_recent_days(df, num_days)), **kwargs)
def plot_filtered_accperstim(title, df, num_days=7, **kwargs): ''' plots accuracy per stim for a subject on the past num_days. ''' plot_accperstim( title, utils.filter_normal_trials(utils.filter_recent_days(df, num_days)), **kwargs)
def plot_trial_feeds(behav_data, num_days=7, return_fig=False): ''' plots numer of trials and number of feeds for all birds across time Parameters: ----------- behav_data : dict of pandas dataframes from loading.load_data_pandas num_days : non-negative int number of days to include data for ''' colors = sns.hls_palette(len(behav_data)) fig = plt.figure(figsize=(16.0, 4.0)) ax1 = fig.gca() ax2 = ax1.twinx() for (subj, df), color in zip(list(behav_data.items()), colors): data_to_analyze = utils.filter_recent_days(df, num_days).copy() if not data_to_analyze.empty: data_to_analyze['date'] = data_to_analyze.index.date blocked = data_to_analyze.groupby('date') days = np.sort(list(blocked.groups.keys())) trials_per_day = blocked['response'].count().values line = ax1.plot(days, trials_per_day, label=subj + ' trials per day', c=color) if len(days) == 1: plot(0, trials_per_day[-1], 'o', c=color, ax=ax1) aggregated = blocked.agg( {'reward': lambda x: np.sum((x == True).astype(float))}) aggregated['reward'].plot(ax=ax2, label=subj + ' feeds per day', ls='--', c=color) if len(days) == 1: ax2.plot(0, aggregated['reward'][0], 'o', c=color) plt.title('trials and feeds per day') for ax, label, loc in zip((ax1, ax2), ('trials per day', 'feeds per day'), ('upper left', 'upper right')): ax.set_ylabel(label) ax.set_ylim(bottom=0) ax.legend(loc=loc) ax1.set_xticklabels(_date_labels(days)) if return_fig: return fig
def plot_accuracy_bias(subj, df, x_axis='time', smoothing='exponential', trial_lim=None, day_lim=12, plt_correct_smoothed=True, plt_correct_shade=True, plt_correct_line=True, plt_L_response_smoothed=False, plt_L_response_shade=False, plt_L_response_line=False, plt_R_response_smoothed=False, plt_R_response_shade=False, plt_R_response_line=False, plt_ci=False, block_size=100, return_fig=False): ''' plots the accuracy or bias of the subject. Parameters: ----------- subj : str the subject df : pandas DataFrame data frame of behavior data x_axis : str whether to plot 'time' or 'trial_num' along the x axis smoothing : str whether to smooth using 'exponential', 'rolling' average, 'gaussian' filter' trial_lim : None or int max number of most recent trials to include day_lim : None or non-negative int max number of days of trials to include. Zero means just today. plt_{correct, L_response, R_response}_smoothed : boolean whether to plot a smoothed line for the value plt_{correct, L_response, R_response}_shade : boolean whether to plot a red shaded region filling in the line of actual responses plt_{correct, L_response, R_response}_line : boolean whether to plot a red line of the actual responses ''' fig = plt.figure(figsize=(16, 2)) if trial_lim is not None: df = df[-trial_lim:] if day_lim is not None: df = utils.filter_recent_days(df, day_lim) df = utils.filter_normal_trials(df) if x_axis == 'time': x = df.index._mpl_repr() use_index = True elif x_axis == 'trial_num': x = np.arange(len(df)) use_index = False else: raise Exception('invalid value for x_axis') datas = (df['correct'], df['response'] == 'L', df['response'] == 'R') plot_smoothed_mask = (plt_correct_smoothed, plt_L_response_smoothed, plt_R_response_smoothed) plot_shaded_mask = (plt_correct_shade, plt_L_response_shade, plt_R_response_shade) plot_line_mask = (plt_correct_line, plt_L_response_line, plt_R_response_line) for data, smoothed, shaded, line in zip(datas, plot_smoothed_mask, plot_shaded_mask, plot_line_mask): if shaded: plt.fill_between(x, .5, data.values.astype(bool), color='r', alpha=.25) if line: #return data g = plt.plot(data.values.astype(bool), color='r', marker='o', linewidth=.5) if smoothed: if smoothing == 'exponential': data.ewm(halflife=20).mean().plot(use_index=use_index) elif smoothing == 'gaussian': plt.plot( x, sp.ndimage.filters.gaussian_filter( data.values.astype('float32'), 3, order=0)) elif smoothing == 'rolling': try: data.rolling(window=block_size, center=True).mean().plot(use_index=use_index) except TypeError as e: print(e) continue else: raise Exception('invalid value for smoothing') if plt_ci and smoothing == 'rolling': ci = utils.binomial_ci(0.5 * block_size, block_size) plt.axhspan(ci[0], ci[1], color='grey', alpha=0.5) plt.axhline(y=.5, c='black', linestyle='dotted') plt.title('Today\'s Performance: ' + subj) plt.xlabel(x_axis) if return_fig: return fig