Skip to content

Instantly share code, notes, and snippets.

@sburns
Created April 2, 2014 13:47
Show Gist options
  • Save sburns/9934490 to your computer and use it in GitHub Desktop.
Save sburns/9934490 to your computer and use it in GitHub Desktop.
ipython notebook exploring a data-driven approach to ordering the Destrieux labels from freesurfer
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:7ca65819a129445282796c3868d0f651a25b6885081aac6f1099f317f93e1638"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook will explore methods to intelligently order the advanced Destrieux labels produced by freesurfer."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import os\n",
"import pandas as pd\n",
"import numpy as np\n",
"from glob import glob\n",
"%matplotlib inline\n",
"import seaborn as sns"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 94
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def df_from_label(label):\n",
" df = pd.read_table(label,\n",
" sep=r'[\\s]*',\n",
" names=['Vertex', 'X', 'Y', 'Z', '?'],\n",
" skiprows=2,\n",
" header=None,\n",
" index_col='Vertex').drop(['?'], axis=1)\n",
" return df\n",
"\n",
"def distance(record, anchor):\n",
" \"Compute the distance from some label\"\n",
" x2 = pow(record['X'] - anchor['X'], 2)\n",
" y2 = pow(record['Y'] - anchor['Y'], 2)\n",
" z2 = pow(record['Z'] - anchor['Z'], 2)\n",
" return np.sqrt(x2 + y2 + z2)\n",
"\n",
"def cog_from_df(df):\n",
" df.sort_index(by=['X', 'Y', 'Z'], inplace=True)\n",
" df['dist'] = df.apply(distance, axis=1, anchor=df.median())\n",
" df.sort_index(by='dist', inplace=True)\n",
" return df.ix[df.index[0]]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 91
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"template = '/scratch/burnsss1/freesurfer-subjects/fsaverage/label/lh.*.label'\n",
"data = []\n",
"for label in glob(template):\n",
" roi_from_label = os.path.basename(label).replace('lh.', '').replace('.label', '')\n",
" record = dict(label=roi_from_label)\n",
" cog = cog_from_df(df_from_label(label))\n",
" record['X'], record['Y'], record['Z'] = int(cog['X']), int(cog['Y']), int(cog['Z'])\n",
" data.append(record)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 102
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"df = pd.DataFrame(data)\n",
"df.index = df.label\n",
"del df['label']\n",
"df.head()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>X</th>\n",
" <th>Y</th>\n",
" <th>Z</th>\n",
" </tr>\n",
" <tr>\n",
" <th>label</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>BA1</th>\n",
" <td>-42</td>\n",
" <td> -6</td>\n",
" <td> 38</td>\n",
" </tr>\n",
" <tr>\n",
" <th>BA2</th>\n",
" <td>-38</td>\n",
" <td>-10</td>\n",
" <td> 30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>BA3a</th>\n",
" <td>-32</td>\n",
" <td> -2</td>\n",
" <td> 22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>BA3b</th>\n",
" <td>-41</td>\n",
" <td> 5</td>\n",
" <td> 24</td>\n",
" </tr>\n",
" <tr>\n",
" <th>BA44</th>\n",
" <td>-48</td>\n",
" <td> 30</td>\n",
" <td> 4</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 103,
"text": [
" X Y Z\n",
"label \n",
"BA1 -42 -6 38\n",
"BA2 -38 -10 30\n",
"BA3a -32 -2 22\n",
"BA3b -41 5 24\n",
"BA44 -48 30 4"
]
}
],
"prompt_number": 103
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"df.sort_index(by=['Y', 'X', 'Z'], ascending=False, inplace=True)\n",
"df.head()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>X</th>\n",
" <th>Y</th>\n",
" <th>Z</th>\n",
" </tr>\n",
" <tr>\n",
" <th>label</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>G_and_S_transv_frontopol</th>\n",
" <td>-14</td>\n",
" <td> 62</td>\n",
" <td> 0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>G_and_S_frontomargin</th>\n",
" <td>-23</td>\n",
" <td> 53</td>\n",
" <td> -6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>BA45</th>\n",
" <td>-44</td>\n",
" <td> 47</td>\n",
" <td> 0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>S_front_middle</th>\n",
" <td>-25</td>\n",
" <td> 43</td>\n",
" <td> 16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>G_and_S_cingul-Ant</th>\n",
" <td>-10</td>\n",
" <td> 39</td>\n",
" <td> 8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 104,
"text": [
" X Y Z\n",
"label \n",
"G_and_S_transv_frontopol -14 62 0\n",
"G_and_S_frontomargin -23 53 -6\n",
"BA45 -44 47 0\n",
"S_front_middle -25 43 16\n",
"G_and_S_cingul-Ant -10 39 8"
]
}
],
"prompt_number": 104
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print '\\n'.join(df.index)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"G_and_S_transv_frontopol\n",
"G_and_S_frontomargin\n",
"BA45\n",
"S_front_middle\n",
"G_and_S_cingul-Ant\n",
"S_orbital_lateral\n",
"S_suborbital\n",
"G_rectus\n",
"S_orbital-H_Shaped\n",
"Lat_Fis-ant-Horizont\n",
"G_front_inf-Orbital\n",
"BA44\n",
"G_front_middle\n",
"G_orbital\n",
"G_front_inf-Triangul\n",
"S_orbital_med-olfact\n",
"S_circular_insula_ant\n",
"S_front_inf\n",
"G_front_sup\n",
"Lat_Fis-ant-Vertical\n",
"S_front_sup\n",
"G_subcallosal\n",
"G_and_S_cingul-Mid-Ant\n",
"G_front_inf-Opercular\n",
"G_insular_short\n",
"BA6\n",
"S_circular_insula_sup\n",
"BA3b\n",
"Pole_temporal\n",
"G_temp_sup-Plan_polar\n",
"S_precentral-inf-part\n",
"BA4p\n",
"BA3a\n",
"entorhinal\n",
"BA4a\n",
"BA1\n",
"G_Ins_lg_and_S_cent_ins\n",
"S_pericallosal\n",
"BA2\n",
"G_precentral\n",
"G_and_S_cingul-Mid-Post\n",
"S_precentral-sup-part\n",
"G_and_S_subcentral\n",
"S_circular_insula_inf\n",
"Medial_wall\n",
"Unknown\n",
"G_temp_sup-Lateral\n",
"G_oc-temp_med-Parahip\n",
"S_collat_transv_ant\n",
"G_temp_sup-G_T_transv\n",
"S_central\n",
"aparc\n",
"cortex\n",
"S_temporal_transverse\n",
"G_postcentral\n",
"S_postcentral\n",
"Lat_Fis-post\n",
"G_and_S_paracentral\n",
"G_temporal_middle\n",
"G_cingul-Post-dorsal\n",
"S_cingul-Marginalis\n",
"G_pariet_inf-Supramar\n",
"G_temporal_inf\n",
"S_temporal_inf\n",
"G_temp_sup-Plan_tempo\n",
"G_cingul-Post-ventral\n",
"MT\n",
"S_temporal_sup\n",
"S_oc-temp_med_and_Lingual\n",
"S_oc-temp_lat\n",
"S_subparietal\n",
"S_interm_prim-Jensen\n",
"V1\n",
"G_oc-temp_lat-fusifor\n",
"V2\n",
"G_parietal_sup\n",
"S_intrapariet_and_P_trans\n",
"G_precuneus\n",
"G_pariet_inf-Angular\n",
"S_parieto_occipital\n",
"S_occipital_ant\n",
"S_calcarine\n",
"G_oc-temp_med-Lingual\n",
"S_oc_sup_and_transversal\n",
"S_collat_transv_post\n",
"G_and_S_occipital_inf\n",
"G_cuneus\n",
"G_occipital_middle\n",
"S_oc_middle_and_Lunatus\n",
"G_occipital_sup\n",
"Pole_occipital\n"
]
}
],
"prompt_number": 105
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment