Browse Source

Annotation: rename attribute vertex_{values->color_codes}

Fabian Peter Hammerle 5 years ago
parent
commit
d9656e1dfa
4 changed files with 20 additions and 21 deletions
  1. 2 2
      examples/annotation_stats.ipynb
  2. 6 7
      freesurfer_surface/__init__.py
  3. 10 10
      tests/test_annotation.py
  4. 2 2
      tests/test_surface.py

+ 2 - 2
examples/annotation_stats.ipynb

@@ -22,7 +22,7 @@
     "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_values)"
+    "len(surface.annotation.vertex_color_codes)"
    ]
   },
   {
@@ -296,7 +296,7 @@
     "import pandas\n",
     "\n",
     "vertex_frame = pandas.DataFrame({'vertex_index': vertex_index, 'color_code': annotation_value}\n",
-    "                                for vertex_index, annotation_value in surface.annotation.vertex_values.items())\n",
+    "                                for vertex_index, annotation_value in surface.annotation.vertex_color_codes.items())\n",
     "vertex_label_frame = vertex_frame.merge(label_frame, on='color_code')\n",
     "vertex_label_frame.head()"
    ]

+ 6 - 7
freesurfer_surface/__init__.py

@@ -84,9 +84,8 @@ class Annotation:
 
     _TAG_OLD_COLORTABLE = b'\0\0\0\x01'
 
-    # TODO rename vertex_color_codes
     # TODO replace with vertex_label_index
-    vertex_values: typing.Dict[int, int] = {}
+    vertex_color_codes: typing.Dict[int, int] = {}
     colortable_path: typing.Optional[bytes] = None
     # TODO dict
     labels: typing.List[Label] = None
@@ -106,8 +105,8 @@ class Annotation:
         annotations_num, = struct.unpack('>I', stream.read(4))
         annotations = (struct.unpack('>II', stream.read(4 * 2))
                        for _ in range(annotations_num))
-        self.vertex_values = {vertex_index: color_code
-                              for vertex_index, color_code in annotations}
+        self.vertex_color_codes = {vertex_index: color_code
+                                   for vertex_index, color_code in annotations}
         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
@@ -117,7 +116,7 @@ class Annotation:
         self.labels = [self._read_label(stream) for _ in range(labels_num)]
         label_color_codes = set(l.color_code for l in self.labels)
         assert all(vertex_color_code in label_color_codes
-                   for vertex_color_code in self.vertex_values.values())
+                   for vertex_color_code in self.vertex_color_codes.values())
         assert not stream.read(1)
 
     @classmethod
@@ -232,9 +231,9 @@ class Surface:
 
     def load_annotation_file(self, annotation_file_path: str) -> None:
         annotation = Annotation.read(annotation_file_path)
-        assert len(annotation.vertex_values) <= len(self.vertices)
+        assert len(annotation.vertex_color_codes) <= len(self.vertices)
         assert all(0 <= vertex_index < len(self.vertices)
-                   for vertex_index in annotation.vertex_values.keys())
+                   for vertex_index in annotation.vertex_color_codes.keys())
         self.annotation = annotation
 
     def add_vertex(self, vertex: Vertex) -> int:

+ 10 - 10
tests/test_annotation.py

@@ -7,16 +7,16 @@ from conftest import SUBJECTS_DIR
 
 def test_load_annotation():
     annotation = Annotation.read(os.path.join(SUBJECTS_DIR, 'fabian', 'label', 'lh.aparc.annot'))
-    assert len(annotation.vertex_values) == 155622
-    assert annotation.vertex_values[64290] == 1316060
-    assert annotation.vertex_values[72160] == 1316060
-    assert annotation.vertex_values[84028] == 14423100
-    assert annotation.vertex_values[97356] == 14423100
-    assert annotation.vertex_values[123173] == 8204875
-    assert annotation.vertex_values[140727] == 8204875
-    assert annotation.vertex_values[93859] == 10542100
-    assert annotation.vertex_values[78572] == 0
-    assert annotation.vertex_values[120377] == 0
+    assert len(annotation.vertex_color_codes) == 155622
+    assert annotation.vertex_color_codes[64290] == 1316060
+    assert annotation.vertex_color_codes[72160] == 1316060
+    assert annotation.vertex_color_codes[84028] == 14423100
+    assert annotation.vertex_color_codes[97356] == 14423100
+    assert annotation.vertex_color_codes[123173] == 8204875
+    assert annotation.vertex_color_codes[140727] == 8204875
+    assert annotation.vertex_color_codes[93859] == 10542100
+    assert annotation.vertex_color_codes[78572] == 0
+    assert annotation.vertex_color_codes[120377] == 0
     assert annotation.colortable_path == b'/autofs/space/tanha_002/users/greve' \
                                          b'/fsdev.build/average/colortable_desikan_killiany.txt'
     assert len(annotation.labels) == 36

+ 2 - 2
tests/test_surface.py

@@ -122,8 +122,8 @@ def test_load_annotation():
     surface.load_annotation_file(os.path.join(SUBJECTS_DIR, 'fabian',
                                               'label', 'lh.aparc.annot'))
     assert isinstance(surface.annotation, Annotation)
-    assert len(surface.annotation.vertex_values) == 155622
-    assert surface.annotation.vertex_values[0] == (((100 << 8) + 20) << 8) + 220
+    assert len(surface.annotation.vertex_color_codes) == 155622
+    assert surface.annotation.vertex_color_codes[0] == (((100 << 8) + 20) << 8) + 220
 
 
 def test_add_vertex():