<jupyter_start><jupyter_text>Define RDMs based on student data<NAME>, April 2022<jupyter_code>%matplotlib inline

import sys
import pandas as pd
import numpy as np
from ast import literal_eval
import matplotlib.pyplot as plt
import seaborn as sns
import rsatoolbox as rsa
from scipy.spatial import distance

sys.path.append('..')
from utils import upper_tri, gsearch

sys.path.append('../2_behavioral')
from teaching_models import human_df as teach_raw

sns.set_context('talk')
sns.set_style('white')<jupyter_output><empty_output><jupyter_text>Data wrangling Load student data:<jupyter_code>student_df = pd.read_csv('../3_student/outputs/student_beliefs.csv')
student_df['bets_sorted'] = student_df['bets_sorted'].apply(literal_eval)
student_df['bets_rescaled'] = student_df['bets_sorted'].apply(lambda v: (v/np.sum(v)).tolist())

print(student_df.shape)
student_df.head()<jupyter_output>(15726, 15)<jupyter_text>Get problem orders from teacher data<jupyter_code>teach_timing = (
    teach_raw
    .copy()
    [['subject', 'run', 'block_idx', 'ex_idx', 'problem']]
)
teach_timing['hint_presented'] = 1
teach_timing['num_hint'] = teach_timing.groupby(['subject', 'problem'])['hint_presented'].transform('cumsum')
teach_timing = teach_timing.drop(columns=['hint_presented'])
teach_timing['trial'] = teach_timing.block_idx*3+teach_timing.ex_idx+1

print(teach_timing.shape)
teach_timing.head()<jupyter_output>(3213, 7)<jupyter_text>Get average beliefs<jupyter_code>avg_list = []

for name,group in student_df.groupby(['teacher', 'problem', 'num_hint']):
    bets = group.bets_rescaled.values.tolist()
    bets_mtx = np.array(bets)
    
    avg_bet = bets_mtx.mean(axis=0)
    avg_list.append(name+tuple([avg_bet]))
    
avg_student = pd.DataFrame(avg_list, columns=['subject', 'problem', 'num_hint', 'bet'])
avg_student = teach_timing.merge(avg_student)
print(avg_student.shape)
avg_student.head()<jupyter_output>(3122, 8)<jupyter_text>Build teacher-specific RDMs<jupyter_code>sub = 1
sub_id = f'sub-{sub:02}'

# merge with teacher behavioral data to get trial orders
teacher_bets = avg_student[avg_student.subject == sub].reset_index(drop=True)

# make distance matrix
n_obs = teacher_bets.shape[0]
student_dist = np.zeros((n_obs, n_obs))

for i, row_i in teacher_bets.iterrows():
    for j, row_j in teacher_bets.iterrows():
        student_dist[i,j] = distance.cosine(row_i.bet, row_j.bet)

# define behavioral rdm
student_model = rsa.model.ModelFixed('Student Beliefs', upper_tri(student_dist))

# line up with betas
beta_files = gsearch('outputs', 'roi_rsa', sub_id, '*RDM.h5')
f = beta_files[0]
beta_rdm = rsa.rdm.load_rdm(f)
beta_rdm<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1372328458942632, 'fraction_numerical': 0.012373453318335208, 'mean_word_length': 7.012012012012012, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '266663', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1049, 'n_tokens_neox': 1015, 'n_words': 177, 'path': '7_multivariate/2_student_rdm.ipynb', 'repo_name': 'nataliavelez/teaching_fmri', 'size': '2674'}