<jupyter_start><jupyter_text>Combine parcellationsHere, two parcellations are combined into one. These parcellations are:- base parcellation: whole-brain network functional parcellation from [Power et al., 2011](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3222858/)- meta-analytic parcellation: only regions involved in processing positive and negative prediction errors from [Fouragnan et al., 2018](https://pubmed.ncbi.nlm.nih.gov/29575249/)There are three main strategies for combining individual ROIs:![strategy](parcellation_combine_strategy.png)<jupyter_code>import os

import pandas as pd

from os.path import join
from pathlib import Path
from dn_utils.path import path_derivatives, path_parcellations
path_root = os.environ.get('DECIDENET_PATH')
path_derivatives = join(path_root, 'data/main_fmri_study/derivatives')
path_parcellations = join(path_derivatives, 'parcellations')

df_roi_base = pd.read_csv(
    join(path_parcellations, 'Power_roi/Power_roi_table.csv'))
df_roi_meta = pd.read_csv(
    join(path_parcellations, 'meta_roi_4and5/meta_roi_table.csv'))
df_roi_meta.head()
df_roi_new = df_roi_base.copy()
df_roi_new['origNetName'] = df_roi_new['netName']
df_roi_new['strategy'] = None
df_roi_new['origName'] = None

shared_columns = ['x', 'y', 'z', 'netName', 'radius(mm)']

for i, roi_meta in df_roi_meta.iterrows():

    roi_meta_name = roi_meta['abbrev'] + '_' + roi_meta['hemisphere']
    
    # Calculate distance between meta ROI and all ROIs from base atlas
    dist = df_roi_base.loc[:, ['x', 'y', 'z']] - roi_meta[['x', 'y', 'z']]
    dist = dist.pow(2).sum(axis=1).pow(0.5)

    # Grab closest region
    roi_base_idx = dist.argmin()
    roi_base = df_roi_base.loc[roi_base_idx]
    
    # Max radius of newly created ROI (non-overlapping)
    max_r = dist[roi_base_idx] - roi_base['radius(mm)']

    print(f'ROI: {roi_meta_name} ({roi_meta["netName"]})\n\t' + \
          f'• closest roi: {roi_base_idx} ({roi_base["netName"]}, r={roi_base["radius(mm)"]}mm)\n\t' + \
          f'• distance = {dist[roi_base_idx]:4.2f}mm, max_r={max_r:4.2f}mm')
    
    if max_r >= 5:
        # Create new ROI
        df_roi_new = df_roi_new.append(roi_meta[shared_columns], 
                                       ignore_index=True)
        df_roi_new.iloc[-1, df_roi_new.columns.get_loc('strategy')] = 'created'
        df_roi_new.iloc[-1, df_roi_new.columns.get_loc('origName')] = roi_meta_name
        
        print(f'\t✅ created new ROI: {roi_meta_name} ({roi_meta["netName"]})\n')
    
    elif max_r > 0:
        # Shift old ROI and rename network
        df_roi_new.loc[roi_base_idx, shared_columns] = roi_meta[shared_columns]
        df_roi_new.loc[roi_base_idx, 'strategy'] = 'shifted'
        df_roi_new.loc[roi_base_idx, 'origName'] = roi_meta_name
        
        before_xyz = roi_base[['x', 'y', 'z']].astype(str).str.cat(sep=', ')
        after_xyz = roi_meta[['x', 'y', 'z']].astype(str).str.cat(sep=', ')
        
        print(f'\t✅ shifted: ({before_xyz}) ⮕ ({after_xyz})')
        print(f'\t✅ relabelled: {roi_base["netName"]} ⮕ {roi_meta["netName"]}\n')
    
    else:
        # Just change network label
        df_roi_new.loc[roi_base_idx, 'netName'] = roi_meta['netName']
        df_roi_new.loc[roi_base_idx, 'strategy'] = 'relabelled'
        df_roi_new.loc[roi_base_idx, 'origName'] = roi_meta_name
        
        print(f'\t✅ relabelled: {roi_base["netName"]} ⮕ {roi_meta["netName"]}\n')
df_roi_new.loc[df_roi_new['netName'] == 'perr_inc']
df_roi_new.loc[df_roi_new['netName'] == 'perr_dec']
path_out = join(path_parcellations, 'combined_roi_4and5')
Path(path_out).mkdir(exist_ok=True)

df_roi_new = df_roi_new.sort_values(by='netName', axis=0, ascending=False)
df_roi_new.to_csv(join(path_out, 'combined_roi_4and5_table.csv'), index=False)

# For thesis
df_roi_new.loc[~df_roi_new['strategy'].isna()].to_csv(
    join(path_out, "combined_roi_4and5_new_rois_table.csv"), index=False)
df_roi_new<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14897907738845476, 'fraction_numerical': 0.011091504915553314, 'mean_word_length': 4.3549257759784075, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '287168', 'license': 'MIT', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1581, 'n_words': 253, 'path': 'connectivity_analysis/parcellations/dn_pa_01_combine_parcellations.ipynb', 'repo_name': 'kbonna/decidenet', 'size': '3967'}