<jupyter_start><jupyter_text>Load in fitness dataGet summaries of recent measurements and recent vs. baseline measurements:  - Activity (steps, zone minutes, floors/elevation)  - Resting heart rate  - Sleep<jupyter_code>fitness_tags = ['baseline' if 'baseline' in c else 'recent' if 'recent' in c else 'static' for c in fitbit.columns]
fitbit
behavioral_tags = ['delayed' if 'delayed' in c else 'immediate' for c in behavioral.columns]
behavioral<jupyter_output><empty_output><jupyter_text>Plot distributions of fitness statsFor each column of the behavioral dataframe, divide participants based on behavioral performance (using `bounds`).  Then plot the distribution of each fitness variable.  Create one panel for each fitness variable, where each column has one distribution per group of particiapants (divided by behavior for that task).Full figure:  - Rows: behavioral columns  - Columns: fitness vars  - Each panel: distributions of fitness var-- one for each group (as defined by bounds applied to behavioral var)  Create 6 figures-- each combination of:  - Immediate vs. delayed (behavioral)  - Static vs. recent vs. baseline (fitness)<jupyter_code>def split_distplots(vals, group_vals, bounds, cmap, bins=10, xlabel='Value', ylabel='Number of participants', fontsize=14, **kwargs):
    x = pd.DataFrame(vals)            
    x['group'] = np.digitize(group_vals, get_bounds(group_vals, bounds=bounds))
    x = x.query(f'group < {len(bounds)}').dropna(axis=0, how='any')        
    
    sns.histplot(x, x=x.columns[0], hue='group', palette=cmap[:len(np.unique(x['group']))],
                 fill=True, legend=False, bins=bins, **kwargs)
    
    ax = kwargs.pop('ax', plt.gca())
    if len(xlabel) > 0:
        ax.set_xlabel(xlabel, fontsize=fontsize)
    if len(ylabel) > 0:
        ax.set_ylabel(ylabel, fontsize=fontsize)
def split_scatterplots(vals, group_vals, bounds, cmap, xlabel='', ylabel='', fontsize=14, **kwargs):    
    sns.scatterplot(x=vals.values.ravel(), y=group_vals.values.ravel(), hue=group_vals.values.ravel(), palette=cmap,
                    legend=False, **kwargs)
    
    ax = kwargs.pop('ax', plt.gca())
    if len(xlabel) > 0:
        ax.set_xlabel(xlabel, fontsize=fontsize)
    if len(ylabel) > 0:
        ax.set_ylabel(ylabel, fontsize=fontsize)
def grid_helper(behavioral, fitbit, bounds, task_cmaps, plotfun, **kwargs):
    fig, axes = plt.subplots(nrows=len(behavioral.columns), ncols=len(fitbit.columns),
                             sharex='col', sharey='row', figsize=(2*len(fitbit.columns), 2*len(behavioral.columns)))

    for i, b in enumerate(behavioral.columns):                    
        cmap = [v for k, v in task_cmaps.items() if k in b][0]  # get this task's colormap        
        for j, f in enumerate(fitbit.columns):
            if i == len(behavioral.columns) - 1:
                xlabel = format_text(f)
            else:
                xlabel = ''    
            
            if j == 0:
                ylabel = format_text(b)
            else:
                ylabel = ''
                        
            plotfun(pd.DataFrame(index=fitbit.index, columns=[format_text(f)], data=fitbit[f].values),
                    behavioral[b], bounds, cmap=cmap, ax=axes[i, j], 
                    xlabel=xlabel, ylabel=ylabel, fontsize=8,
                    **kwargs)

    return fig
def distplot_grid(behavioral, fitbit, bounds, task_cmaps, bins=10, **kwargs):
    return grid_helper(behavioral, fitbit, bounds, task_cmaps, split_distplots, bins=bins, **kwargs)
def scatterplot_grid(behavioral, fitbit, bounds, task_cmaps, **kwargs):
    return grid_helper(behavioral, fitbit, bounds, task_cmaps, split_scatterplots, **kwargs)
def grid_plotter(behavioral, fitbit, behavioral_tags, fitness_tags, gridplotter, task_cmaps, **kwargs):
    for task_cond in np.unique(behavioral_tags):
        b = behavioral.loc[:, np.array(behavioral_tags) == task_cond]
        for fit_cond in np.unique(fitness_tags):
            f = fitbit.loc[:, np.array(fitness_tags) == fit_cond]

            plt.clf()
            fig = gridplotter(b, f, bounds, task_cmaps, **kwargs)

            fname = f'{task_cond}_{fit_cond}_{gridplotter.__name__}.pdf'
            fig.savefig(fname)
grid_plotter(behavioral, fitbit, behavioral_tags, fitness_tags, distplot_grid, task_cmaps, bins=15)
grid_plotter(behavioral, fitbit, behavioral_tags, fitness_tags, scatterplot_grid, task_cmaps_extended)<jupyter_output><empty_output><jupyter_text>Plot distributions of fitness variables<jupyter_code>color_dict = {f: c for f, c in zip(np.unique(fitness_tags), ['#A81A8A', '#1D2A7C', '#B29802'])}


for i, c in enumerate(fitbit.columns):
    plt.clf()
    fig = plt.figure(figsize=(3, 3))
    
    sns.displot(fitbit[c], kde=True, color=color_dict[fitness_tags[i]])
    plt.ylabel('Number of participants', fontsize=14)
    plt.xlabel(format_text(c, width=50), fontsize=14)
    
    plt.savefig(f"{c.replace(':', '-').replace('/', '-').replace(' ', '')}_dist.pdf")
    plt.close()
fitbit
with open(os.path.join(DATA_DIR, 'fitness_summary.pkl'), 'wb') as f:
    pickle.dump(fitbit, f)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12947819314641745, 'fraction_numerical': 0.008177570093457943, 'mean_word_length': 3.4903846153846154, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '406134', 'license': 'MIT', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1653, 'n_words': 404, 'path': 'code/fitness_data.ipynb', 'repo_name': 'ContextLab/brainfit-paper', 'size': '5136'}