<jupyter_start><jupyter_text>Retest<jupyter_code>retest_contrasts = []

for i in range(len(test_subj_ids)):
    subj = test_subj_ids[i]
    retest_contrast_file = os.path.join(base_data_dir, "HCP_Retest", "contrasts", "%s_joint_LR_task_contrasts.npy" % subj)
    retest_contrast_data = np.load(retest_contrast_file)
    retest_contrasts.append(retest_contrast_data)

retest_contrasts = np.asarray(retest_contrasts)
print(retest_contrasts.shape)
def compute_subj_contrast_corr(pred, ref, contrasts, contrast_ids, mask):
    all_lh_corr = []
    all_rh_corr = []
    all_avg_corr = []
    
    masked_lh_pred = pred[:, fdf8:f53e:61e4::18, mask[0, :]]
    masked_rh_pred = pred[:, fc00:db20:35b:7399::5, mask[1, :]]
    
    masked_lh_ref = ref[:, fdf8:f53e:61e4::18, mask[0, :]]
    masked_rh_ref = ref[:, fc00:db20:35b:7399::5, mask[1, :]]

    for i in range(len(contrasts)):
        lh_contrast_ref = masked_lh_ref[:, i, :]
        rh_contrast_ref = masked_rh_ref[:, i, :]
# 
        lh_contrast_pred = masked_lh_pred[:, i, :]
        rh_contrast_pred = masked_rh_pred[:, i, :]
        
#         print(lh_contrast_ref.shape)
#         print(lh_contrast_pred.shape)
#         print(lh_contrast_pred[0, :] - lh_contrast_pred[1, :])
#         print(lh_contrast_ref[0, :] - lh_contrast_ref[1, :])

        lh_corr = compute_corr_coeff(lh_contrast_ref, lh_contrast_pred)
        rh_corr = compute_corr_coeff(rh_contrast_ref, rh_contrast_pred)
        # print(lh_corr[0, :] - lh_corr[1, :])

        all_lh_corr.append(lh_corr)
        all_rh_corr.append(rh_corr)
        all_avg_corr.append((lh_corr + rh_corr) / 2)
    return all_lh_corr, all_rh_corr, all_avg_corr

lh_test_retest_corr, rh_test_retest_corr, test_retest_corr = compute_subj_contrast_corr(retest_contrasts, test_contrasts, CONTRASTS, GROUP_CONTRAST_IDS, mask)

plot_corr_matrices_across_contrasts(test_retest_corr, CONTRASTS[4:7], vmax=0.8)<jupyter_output><empty_output><jupyter_text>BrainSurfCNN (trained with MSE)<jupyter_code>base_brainsurfcnn_mse_dir = "/nfs03/users/ghn8/TaskContrastsFromRest/debug/ch50_feat64_s8_c50_lr0.01_seed28/predict/best_corr"

multisample_brainsurfcnn_mse_pred = []
for i in range(len(test_subj_ids)):
    subj = test_subj_ids[i]
    pred_file = os.path.join(base_brainsurfcnn_mse_dir, "%s_pred.npy" % subj)
    pred = np.load(pred_file)
    multisample_brainsurfcnn_mse_pred.append(pred)

multisample_brainsurfcnn_mse_pred = np.asarray(multisample_brainsurfcnn_mse_pred)
brainsurfcnn_mse_pred = np.mean(multisample_brainsurfcnn_mse_pred, 1)
print(brainsurfcnn_mse_pred.shape)

lh_test_brainsurfcnn_mse_corr, rh_test_brainsurfcnn_mse_corr, test_brainsurfcnn_mse_corr = compute_subj_contrast_corr(test_contrasts, brainsurfcnn_mse_pred, CONTRASTS, GROUP_CONTRAST_IDS, mask)

plot_corr_matrices_across_contrasts(test_brainsurfcnn_mse_corr, CONTRASTS[4:7], vmin=0.35 , vmax=0.8)<jupyter_output><empty_output><jupyter_text>BrainSurfCNN (trained with RC Loss)<jupyter_code>base_brainsurfcnn_rc_dir = "/nfs03/users/ghn8/TaskContrastsFromRest/debug/ch50_scheduled_feat64_s8_c50_lr0.01_seed28/finetuned_feat64_s8_c50_lr0.01_seed28/predict_on_test_subj/best_corr/"

multisample_brainsurfcnn_rc_pred = []
for i in range(len(test_subj_ids)):
    subj = test_subj_ids[i]
    pred_file = os.path.join(base_brainsurfcnn_rc_dir, "%s_pred.npy" % subj)
    pred = np.load(pred_file)
    multisample_brainsurfcnn_rc_pred.append(pred)
multisample_brainsurfcnn_rc_pred = np.asarray(multisample_brainsurfcnn_rc_pred)
brainsurfcnn_rc_pred = np.mean(multisample_brainsurfcnn_rc_pred, 1)
print(brainsurfcnn_rc_pred.shape)

lh_test_brainsurfcnn_rc_corr, rh_test_brainsurfcnn_rc_corr, test_brainsurfcnn_rc_corr = compute_subj_contrast_corr(test_contrasts, brainsurfcnn_rc_pred, CONTRASTS, GROUP_CONTRAST_IDS, mask)

plot_corr_matrices_across_contrasts(test_brainsurfcnn_rc_corr, CONTRASTS[4:7], vmin=0.35, vmax=0.8)

np.save(os.path.join(base_brainsurfcnn_rc_dir, "pred_corr.npy"), test_brainsurfcnn_rc_corr)<jupyter_output><empty_output><jupyter_text>Model Comparison<jupyter_code>pred_by_model = {
    "Retest": test_retest_corr,
    "BrainSurfCNN - only MSE": test_brainsurfcnn_mse_corr,
    "BrainSurfCNN - RC": test_brainsurfcnn_rc_corr
}
colors = [ '#f99154', '#63bfa6', '#358cbb']
sns.set_palette(sns.color_palette(colors))
df = pd.DataFrame(columns=["Model", "Task Contrast", "Subject", "Correlation with Groundtruth", "Correlation Increase of Self vs Mean Others", "Rank"])
for model in pred_by_model:
    all_subj_contrast_corr = pred_by_model[model]
    for i in range(len(CONTRASTS)):
        item = CONTRASTS[i]
        task, cope_id, contrast_label = item
        key = "%s %s" % (task, contrast_label)

        contrast_corr = all_subj_contrast_corr[i]

        count = 0
        for j in range(len(test_subj_ids)):
            count = count  + 1
            corr_row = contrast_corr[j, :]
            self_corr = corr_row[j]
            other_corrs = np.concatenate((corr_row[:j], corr_row[j+1:]))
            mean_other_corr = np.mean(other_corrs)

            num_other_gt_self = np.sum(other_corrs > self_corr)
            sorted_indices = np.flip(np.argsort(corr_row))
            for k in range(len(sorted_indices)):
                if sorted_indices[k] == j:
                    rank = k
            rank = rank + 1
            df = df.append({
                "Model": model,
                "Task Contrast": key,
                "Subject": test_subj_ids[j],
                "Correlation with Groundtruth": self_corr,
                "Correlation Increase of Self vs Mean Others": (self_corr - mean_other_corr),
                "Rank": rank
            }, ignore_index=True)
fig, ax = plt.subplots(1, 1, figsize=(36, 10))
sns.boxplot(x="Task Contrast",
            y="Correlation with Groundtruth", hue="Model",
            data=df, ax=ax, # palette="Set3",
            hue_order=["BrainSurfCNN - RC", "BrainSurfCNN - only MSE", "Retest"])
plt.setp(ax.xaxis.get_majorticklabels(), rotation=45, ha="right", rotation_mode="anchor") 
L = ax.legend(frameon=False, ncol=4, loc='upper center', bbox_to_anchor=(0.5, 1.1), fontsize=32)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.set_ylim(-0.1, 0.901)
ax.set_yticks(np.arange(-0.1, 0.901, 0.2))
ax.set_xlim(-1, len(CONTRASTS))
ax.tick_params(direction="in", labelsize=32)
ax.xaxis.get_label().set_fontsize(40)
ax.yaxis.get_label().set_fontsize(40)
ax.tick_params(length = 10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11918379965991653, 'fraction_numerical': 0.02519709383212243, 'mean_word_length': 4.616319444444445, 'pattern_counts': {'":': 9, '<': 17, '<?xml version=': 0, '>': 18, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '469891', 'license': 'MIT', 'n_tokens_mistral': 2793, 'n_tokens_neox': 2565, 'n_words': 332, 'path': 'experiments/MICCAI2020/posthoc_analysis.ipynb', 'repo_name': 'sabunculab/brainsurfcnn', 'size': '6405'}