<jupyter_start><jupyter_text>Goal is to analyze route coding from:    - Training day 2 (habit task)    - Test day (habit task)    - Test day (probe task, 2 reps)<jupyter_code>%matplotlib inline
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp

sns.set(style='ticks', font_scale=1.5)

%load_ext rpy2.ipython
group_info = pd.read_csv('/Volumes/group/awagner/sgagnon/SST/scripts/subjects_groups.csv')
group_info.head()
filepath='/Volumes/group/awagner/sgagnon/SST/nav_data/analysis/route_coding_FINAL_detailed.csv'
d = pd.read_csv(filepath)
d.head()
# Remove subjs w/bad coding
remove_subjs = ['sst01', 'sst02', 'sst39']
d = d.loc[~d.subid.isin(remove_subjs)]

print d[d.final_code.isnull()]
codes = d.final_code.unique(); codes
d.final_code.value_counts()
d.final_code2.value_counts()
d.other_info.value_counts()
d.shortcut_rev_or_for.value_counts()<jupyter_output><empty_output><jupyter_text>Update final_code with final_code2 (convert some "other" to shortcut)<jupyter_code>d.loc[~d.final_code2.isnull(), 'final_code'] = d.loc[~d.final_code2.isnull(), 'final_code2']
d.head()
d.final_code.value_counts()<jupyter_output><empty_output><jupyter_text>Get trial counts/type for subjects/days/tasks:<jupyter_code>data = d.groupby(['subid', 'day', 'run_type', 'rep', 'final_code']).count().reset_index()
data = pd.pivot_table(data, index=['subid', 'day', 'run_type', 'rep'], 
                      columns='final_code', values='env', fill_value=0).reset_index()
print data.shape
data.head()
data_counts = d.groupby(['subid', 'day', 'run_type', 'rep']).count().reset_index()
print data_counts.shape
data_counts.head()
len(data.subid.unique())
data['total_trials'] = data_counts.env
data.head()
data.loc[(data.run_type == 'shortcut') & 
         (data.other >4)]<jupyter_output><empty_output><jupyter_text>Calculate proportions<jupyter_code>d1 = data.loc[:, codes[:4]]
d2 = data.loc[:,'total_trials']
proportions = d1.divide(d2, axis='rows')

# combine w/dataframe
proportions[['subid', 'day', 'run_type', 'rep']] = data[['subid', 'day', 'run_type', 'rep']]
proportions.head()

# convert from wide (cols are codes) to long (more rows per subj)
proportions = pd.melt(proportions, id_vars=['subid', 'day', 'run_type', 'rep'])
proportions.head()
# remove bad/missing subjs
print group_info.loc[group_info.remove == 1]

proportions = proportions.merge(group_info)
proportions = proportions.loc[(proportions.remove!=1)]
# any subjs not following habit instructions > 50% time?
proportions.loc[(proportions.run_type == 'habit') & 
                (proportions.final_code == 'habit') & 
                (proportions.value < .5)]<jupyter_output><empty_output><jupyter_text>Visualize data:  Habit task run (day 3):<jupyter_code>data = proportions.loc[(proportions.run_type =='habit') & (proportions.day == 'day3')]

sns.factorplot(x='final_code', y='value', 
               ci=68, units='subid', data=data, aspect=2)
sns.stripplot(x='final_code', y='value', jitter=True, hue='group', 
              palette=['dodgerblue', 'orange'],size=7,linewidth=1,
               data=data)

plt.ylabel('Proportion habit trials')
proportions.loc[(proportions.run_type=='habit') & 
                (proportions.final_code == 'habit'), 'value'].hist()
x_order = ['shortcut', 'backtrack', 'other', 'habit']

p = sns.factorplot(x='final_code', y='value',
                   order=x_order, aspect=1.2,
                   ci=68, units='subid', data=proportions.loc[(proportions.day == 'day3') &
                                                              (proportions.run_type == 'habit')], 
                   kind='bar')

p.set_xlabels('')
p.set_ylabels('proportion of trials')
sns.despine(trim=True)
data = proportions.loc[proportions.run_type == 'habit']
data = data.merge(group_info)

p = sns.factorplot(x='final_code', y='value', hue='group', col='day',
                   row='rep',
                   ci=68, units='subid', order=x_order, kind='bar',
                   aspect=1.5, dodge=.1, palette=['dodgerblue', 'orange'],
                   data=data)
p.set_xlabels('')
plt.ylim(0,1)
p.set_ylabels('proportion of trials')
sns.despine(trim=True)

plt.savefig('/Volumes/group/awagner/sgagnon/SST/results/figures/route_coding/habit_choices_bygroup.png', dpi=300)<jupyter_output><empty_output><jupyter_text>First probe trial:<jupyter_code>data = proportions.loc[(proportions.run_type =='shortcut') & (proportions.rep == 1)]

sns.boxplot(x='final_code', y='value', hue='group',
            data=data, palette=['white'])
sns.swarmplot(x='final_code', y='value', hue='group', split=True,
              palette=['dodgerblue', 'orange'],size=7,linewidth=1,
               data=data)

plt.ylabel('Proportion habit trials')
x_order = ['shortcut', 'backtrack', 'other', 'habit']

p = sns.factorplot(x='final_code', y='value', order=x_order, hue='rep',
                   ci=68, units='subid', aspect=1.5, kind='bar',
                   data=proportions.loc[proportions.run_type == 'shortcut'])
p.set_xlabels('')
p.set_ylabels('proportion of trials')
sns.despine(trim=True)

plt.savefig('/Volumes/group/awagner/sgagnon/SST/results/figures/route_coding/shortcut_choices.png', dpi=300)
data = proportions.loc[proportions.run_type == 'shortcut']
data = data.merge(group_info)

p = sns.factorplot(x='final_code', y='value', hue='group',col='rep',
                   ci=68, units='subid', order=x_order, kind='bar',
                   aspect=1.5, dodge=.1, palette=['dodgerblue', 'orange'],
                   data=data)
p.set_xlabels('')
p.set_ylabels('proportion of trials')
sns.despine(trim=True)
plt.savefig('/Volumes/group/awagner/sgagnon/SST/results/figures/route_coding/shortcut_choices_bygroup.png', dpi=300)
data = proportions.loc[(proportions.run_type == 'shortcut') & 
                       (proportions.rep == 1)]
data = data.merge(group_info)

p = sns.violinplot(x='final_code', y='value', hue='group', inner='quartile',
                   order=x_order, split=True,
                   aspect=1.5, palette=['white'],
                   data=data)
sns.stripplot(x='final_code', y='value', hue='group', data=data, split=True, 
              order=x_order, jitter=.2, alpha=.5, size=8,
              palette=['dodgerblue', 'orange'])

handles, labels = p.get_legend_handles_labels()
p.legend(handles[2:], labels[2:], loc=9)

p.set_xlabel('')
p.set_ylabel('proportion of trials')
sns.despine(trim=True)

plt.savefig('/Volumes/group/awagner/sgagnon/SST/results/figures/route_coding/shortcut_choices_rep1_bygroup_strip.png', dpi=300)
data = proportions.loc[(proportions.run_type == 'habit') & (proportions.day == 'day3')]
data = data.merge(group_info)

p = sns.violinplot(x='final_code', y='value', hue='group', inner='quartile',
                   order=x_order, split=True,
                   aspect=1.5, palette=['white'],
                   data=data)
sns.stripplot(x='final_code', y='value', hue='group', data=data, split=True, 
              order=x_order, jitter=.2, alpha=.5, size=8,
              palette=['dodgerblue', 'orange'])

handles, labels = p.get_legend_handles_labels()
p.legend(handles[2:], labels[2:], loc=9)

p.set_xlabel('')
p.set_ylabel('proportion of trials')
sns.despine(trim=True)

plt.savefig('/Volumes/group/awagner/sgagnon/SST/results/figures/route_coding/habit_choices_rep1_bygroup_strip.png', dpi=300)
data = proportions.loc[proportions.run_type == 'shortcut']
g = sns.FacetGrid(data.loc[data.rep == 1], 
                  col="final_code",  row="group", 
                  size=4, margin_titles=True)
g = g.map(plt.hist, "value")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1712373704238289, 'fraction_numerical': 0.01259677207715523, 'mean_word_length': 4.1815091774303195, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '650331', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 2896, 'n_tokens_neox': 2751, 'n_words': 429, 'path': 'analysis/Analyze_routecoding_FINAL.ipynb', 'repo_name': 'thackerybrown/SST-anal', 'size': '7621'}