This notebook will explore methods to intelligently order the advanced Destrieux labels produced by freesurfer.

In [94]:
import os
import pandas as pd
import numpy as np
from glob import glob
%matplotlib inline
import seaborn as sns
In [91]:
def df_from_label(label):
    df = pd.read_table(label,
                       sep=r'[\s]*',
                       names=['Vertex', 'X', 'Y', 'Z', '?'],
                       skiprows=2,
                       header=None,
                       index_col='Vertex').drop(['?'], axis=1)
    return df

def distance(record, anchor):
    "Compute the distance from some label"
    x2 = pow(record['X'] - anchor['X'], 2)
    y2 = pow(record['Y'] - anchor['Y'], 2)
    z2 = pow(record['Z'] - anchor['Z'], 2)
    return np.sqrt(x2 + y2 + z2)

def cog_from_df(df):
    df.sort_index(by=['X', 'Y', 'Z'], inplace=True)
    df['dist'] = df.apply(distance, axis=1, anchor=df.median())
    df.sort_index(by='dist', inplace=True)
    return df.ix[df.index[0]]
In [102]:
template = '/scratch/burnsss1/freesurfer-subjects/fsaverage/label/lh.*.label'
data = []
for label in glob(template):
    roi_from_label = os.path.basename(label).replace('lh.', '').replace('.label', '')
    record = dict(label=roi_from_label)
    cog = cog_from_df(df_from_label(label))
    record['X'], record['Y'], record['Z'] = int(cog['X']), int(cog['Y']), int(cog['Z'])
    data.append(record)
In [103]:
df = pd.DataFrame(data)
df.index = df.label
del df['label']
df.head()
Out[103]:
X Y Z
label
BA1 -42 -6 38
BA2 -38 -10 30
BA3a -32 -2 22
BA3b -41 5 24
BA44 -48 30 4
In [104]:
df.sort_index(by=['Y', 'X', 'Z'], ascending=False, inplace=True)
df.head()
Out[104]:
X Y Z
label
G_and_S_transv_frontopol -14 62 0
G_and_S_frontomargin -23 53 -6
BA45 -44 47 0
S_front_middle -25 43 16
G_and_S_cingul-Ant -10 39 8
In [105]:
print '\n'.join(df.index)
G_and_S_transv_frontopol
G_and_S_frontomargin
BA45
S_front_middle
G_and_S_cingul-Ant
S_orbital_lateral
S_suborbital
G_rectus
S_orbital-H_Shaped
Lat_Fis-ant-Horizont
G_front_inf-Orbital
BA44
G_front_middle
G_orbital
G_front_inf-Triangul
S_orbital_med-olfact
S_circular_insula_ant
S_front_inf
G_front_sup
Lat_Fis-ant-Vertical
S_front_sup
G_subcallosal
G_and_S_cingul-Mid-Ant
G_front_inf-Opercular
G_insular_short
BA6
S_circular_insula_sup
BA3b
Pole_temporal
G_temp_sup-Plan_polar
S_precentral-inf-part
BA4p
BA3a
entorhinal
BA4a
BA1
G_Ins_lg_and_S_cent_ins
S_pericallosal
BA2
G_precentral
G_and_S_cingul-Mid-Post
S_precentral-sup-part
G_and_S_subcentral
S_circular_insula_inf
Medial_wall
Unknown
G_temp_sup-Lateral
G_oc-temp_med-Parahip
S_collat_transv_ant
G_temp_sup-G_T_transv
S_central
aparc
cortex
S_temporal_transverse
G_postcentral
S_postcentral
Lat_Fis-post
G_and_S_paracentral
G_temporal_middle
G_cingul-Post-dorsal
S_cingul-Marginalis
G_pariet_inf-Supramar
G_temporal_inf
S_temporal_inf
G_temp_sup-Plan_tempo
G_cingul-Post-ventral
MT
S_temporal_sup
S_oc-temp_med_and_Lingual
S_oc-temp_lat
S_subparietal
S_interm_prim-Jensen
V1
G_oc-temp_lat-fusifor
V2
G_parietal_sup
S_intrapariet_and_P_trans
G_precuneus
G_pariet_inf-Angular
S_parieto_occipital
S_occipital_ant
S_calcarine
G_oc-temp_med-Lingual
S_oc_sup_and_transversal
S_collat_transv_post
G_and_S_occipital_inf
G_cuneus
G_occipital_middle
S_oc_middle_and_Lunatus
G_occipital_sup
Pole_occipital