{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "155622"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from freesurfer_surface import Surface\n",
    "\n",
    "SUBJECTS_DIR = '../tests/subjects'\n",
    "surface = Surface.read_triangular(SUBJECTS_DIR + '/fabian/surf/lh.pial')\n",
    "surface.load_annotation_file(SUBJECTS_DIR + '/fabian/label/lh.aparc.annot')\n",
    "len(surface.annotation.vertex_label_index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/autofs/space/tanha_002/users/greve/fsdev.build/average/colortable_desikan_killiany.txt'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "surface.annotation.colortable_path.decode()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>red</th>\n",
       "      <th>green</th>\n",
       "      <th>blue</th>\n",
       "      <th>transparency</th>\n",
       "      <th>hex_color_code</th>\n",
       "      <th>color_code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>unknown</td>\n",
       "      <td>25</td>\n",
       "      <td>5</td>\n",
       "      <td>25</td>\n",
       "      <td>0</td>\n",
       "      <td>#190519</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bankssts</td>\n",
       "      <td>25</td>\n",
       "      <td>100</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "      <td>#196428</td>\n",
       "      <td>2647065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>caudalanteriorcingulate</td>\n",
       "      <td>125</td>\n",
       "      <td>100</td>\n",
       "      <td>160</td>\n",
       "      <td>0</td>\n",
       "      <td>#7d64a0</td>\n",
       "      <td>10511485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>caudalmiddlefrontal</td>\n",
       "      <td>100</td>\n",
       "      <td>25</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>#641900</td>\n",
       "      <td>6500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>corpuscallosum</td>\n",
       "      <td>120</td>\n",
       "      <td>70</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>#784632</td>\n",
       "      <td>3294840</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      name  red  green  blue  transparency hex_color_code  \\\n",
       "0                  unknown   25      5    25             0        #190519   \n",
       "1                 bankssts   25    100    40             0        #196428   \n",
       "2  caudalanteriorcingulate  125    100   160             0        #7d64a0   \n",
       "3      caudalmiddlefrontal  100     25     0             0        #641900   \n",
       "4           corpuscallosum  120     70    50             0        #784632   \n",
       "\n",
       "   color_code  \n",
       "0           0  \n",
       "1     2647065  \n",
       "2    10511485  \n",
       "3        6500  \n",
       "4     3294840  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas\n",
    "\n",
    "label_frame = pandas.DataFrame((dict(color_code=label.color_code,\n",
    "                                     hex_color_code=label.hex_color_code,\n",
    "                                     **vars(label))\n",
    "                                for label in surface.annotation.labels.values()),\n",
    "                               columns=['index', 'name', \n",
    "                                        'red', 'green', 'blue', 'transparency', \n",
    "                                        'hex_color_code', 'color_code'])\n",
    "label_frame.drop(columns=['index']).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label_index</th>\n",
       "      <th>vertex_index</th>\n",
       "      <th>index</th>\n",
       "      <th>name</th>\n",
       "      <th>red</th>\n",
       "      <th>green</th>\n",
       "      <th>blue</th>\n",
       "      <th>transparency</th>\n",
       "      <th>hex_color_code</th>\n",
       "      <th>color_code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>cuneus</td>\n",
       "      <td>220</td>\n",
       "      <td>20</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>#dc1464</td>\n",
       "      <td>6558940</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>cuneus</td>\n",
       "      <td>220</td>\n",
       "      <td>20</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>#dc1464</td>\n",
       "      <td>6558940</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>cuneus</td>\n",
       "      <td>220</td>\n",
       "      <td>20</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>#dc1464</td>\n",
       "      <td>6558940</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>cuneus</td>\n",
       "      <td>220</td>\n",
       "      <td>20</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>#dc1464</td>\n",
       "      <td>6558940</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>cuneus</td>\n",
       "      <td>220</td>\n",
       "      <td>20</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "      <td>#dc1464</td>\n",
       "      <td>6558940</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   label_index  vertex_index  index    name  red  green  blue  transparency  \\\n",
       "0            5             0      5  cuneus  220     20   100             0   \n",
       "1            5             1      5  cuneus  220     20   100             0   \n",
       "2            5             2      5  cuneus  220     20   100             0   \n",
       "3            5             3      5  cuneus  220     20   100             0   \n",
       "4            5             4      5  cuneus  220     20   100             0   \n",
       "\n",
       "  hex_color_code  color_code  \n",
       "0        #dc1464     6558940  \n",
       "1        #dc1464     6558940  \n",
       "2        #dc1464     6558940  \n",
       "3        #dc1464     6558940  \n",
       "4        #dc1464     6558940  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas\n",
    "\n",
    "vertex_frame = pandas.DataFrame({'vertex_index': vertex_index, 'label_index': annotation_value}\n",
    "                                for vertex_index, annotation_value in surface.annotation.vertex_label_index.items())\n",
    "vertex_label_frame = vertex_frame.merge(label_frame, left_on='label_index', right_on='index')\n",
    "vertex_label_frame.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "superiorfrontal             12532\n",
       "superiorparietal             9883\n",
       "lateraloccipital             9792\n",
       "rostralmiddlefrontal         9484\n",
       "supramarginal                8156\n",
       "unknown                      8062\n",
       "precentral                   7944\n",
       "postcentral                  7095\n",
       "superiortemporal             7006\n",
       "inferiorparietal             6737\n",
       "lingual                      5636\n",
       "precuneus                    5560\n",
       "middletemporal               5207\n",
       "fusiform                     5003\n",
       "inferiortemporal             4756\n",
       "lateralorbitofrontal         4383\n",
       "caudalmiddlefrontal          4128\n",
       "insula                       4117\n",
       "cuneus                       3033\n",
       "medialorbitofrontal          3017\n",
       "parsopercularis              2839\n",
       "pericalcarine                2734\n",
       "paracentral                  2507\n",
       "parstriangularis             2460\n",
       "posteriorcingulate           2352\n",
       "isthmuscingulate             1861\n",
       "rostralanteriorcingulate     1711\n",
       "caudalanteriorcingulate      1362\n",
       "bankssts                     1322\n",
       "parsorbitalis                1078\n",
       "parahippocampal               991\n",
       "transversetemporal            990\n",
       "entorhinal                    789\n",
       "temporalpole                  727\n",
       "frontalpole                   368\n",
       "Name: name, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vertex_label_frame['name'].value_counts()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
