Browse Source

Annotation.read: set colortable_path

Fabian Peter Hammerle 5 years ago
parent
commit
5978f4f67d
3 changed files with 33 additions and 6 deletions
  1. 26 6
      examples/annotation_stats.ipynb
  2. 5 0
      freesurfer_surface/__init__.py
  3. 2 0
      tests/test_annotation.py

+ 26 - 6
examples/annotation_stats.ipynb

@@ -21,14 +21,34 @@
     "\n",
     "SUBJECTS_DIR = '../tests/subjects'\n",
     "surface = Surface.read_triangular(SUBJECTS_DIR + '/fabian/surf/lh.pial')\n",
-    "surface.load_annotation(SUBJECTS_DIR + '/fabian/label/lh.aparc.annot')\n",
-    "len(surface.vertex_annotation_values)"
+    "surface.load_annotation_file(SUBJECTS_DIR + '/fabian/label/lh.aparc.annot')\n",
+    "len(surface.annotation.vertex_values)"
    ]
   },
   {
    "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": {
@@ -94,7 +114,7 @@
        "4           6558940             4"
       ]
      },
-     "execution_count": 2,
+     "execution_count": 3,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -103,13 +123,13 @@
     "import pandas\n",
     "\n",
     "vertex_frame = pandas.DataFrame({'vertex_index': vertex_index, 'annotation_value': annotation_value}\n",
-    "                                for vertex_index, annotation_value in surface.vertex_annotation_values.items())\n",
+    "                                for vertex_index, annotation_value in surface.annotation.vertex_values.items())\n",
     "vertex_frame.head()"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 3,
+   "execution_count": 4,
    "metadata": {},
    "outputs": [
     {
@@ -153,7 +173,7 @@
        "Name: annotation_value, dtype: int64"
       ]
      },
-     "execution_count": 3,
+     "execution_count": 4,
      "metadata": {},
      "output_type": "execute_result"
     }

+ 5 - 0
freesurfer_surface/__init__.py

@@ -59,6 +59,7 @@ class Annotation:
     _TAG_OLD_COLORTABLE = b'\0\0\0\x01'
 
     vertex_values: typing.Dict[int, int] = {}
+    colortable_path: typing.Optional[bytes] = None
 
     def _read(self, stream: typing.BinaryIO) -> None:
         # https://surfer.nmr.mgh.harvard.edu/fswiki/LabelsClutsAnnotationFiles
@@ -70,6 +71,10 @@ class Annotation:
         assert all((annotation_value >> (8 * 3)) == 0
                    for annotation_value in self.vertex_values.values())
         assert stream.read(4) == self._TAG_OLD_COLORTABLE
+        colortable_version, _, filename_length = struct.unpack('>III', stream.read(4 * 3))
+        assert colortable_version > 0  # new version
+        self.colortable_path = stream.read(filename_length - 1)
+        assert stream.read(1) == b'\0'
 
     @classmethod
     def read(cls, annotation_file_path: str) -> 'Annotation':

+ 2 - 0
tests/test_annotation.py

@@ -11,3 +11,5 @@ def test_load_annotation():
     assert annotation.vertex_values[0] == (((100 << 8) + 20) << 8) + 220
     assert annotation.vertex_values[1] == (((100 << 8) + 20) << 8) + 220
     assert annotation.vertex_values[42] == (((140 << 8) + 30) << 8) + 20
+    assert annotation.colortable_path == b'/autofs/space/tanha_002/users/greve' \
+                                         b'/fsdev.build/average/colortable_desikan_killiany.txt'