Răsfoiți Sursa

added Surface._triangle_count_by_adjacent_vertex_indices()

Fabian Peter Hammerle 5 ani în urmă
părinte
comite
3787a2c16b
2 a modificat fișierele cu 78 adăugiri și 1 ștergeri
  1. 10 0
      freesurfer_surface/__init__.py
  2. 68 1
      tests/test_surface.py

+ 10 - 0
freesurfer_surface/__init__.py

@@ -421,6 +421,16 @@ class Surface:
         self.triangles.append(Triangle(vertex_indices[2:]
                                        + vertex_indices[:1]))
 
+    def _triangle_count_by_adjacent_vertex_indices(self) \
+            -> typing.Dict[int, typing.Dict[int, int]]:
+        counts = {vertex_index: collections.defaultdict(lambda: 0)
+                  for vertex_index in range(len(self.vertices))}
+        for triangle in self.triangles:
+            for vertex_index_pair in triangle.adjacent_vertex_indices(2):
+                counts[vertex_index_pair[0]][vertex_index_pair[1]] += 1
+                counts[vertex_index_pair[1]][vertex_index_pair[0]] += 1
+        return counts
+
     def _get_vertex_label_index(self, vertex_index: int) -> typing.Optional[int]:
         return self.annotation.vertex_label_index.get(vertex_index, None)
 

+ 68 - 1
tests/test_surface.py

@@ -4,10 +4,12 @@ import numpy
 import pytest
 
 from freesurfer_surface import setlocale, Vertex, Triangle, _LineSegment, \
-                               Annotation, Surface
+                               Annotation, Surface, _PolygonalCircuit
 
 from conftest import ANNOTATION_FILE_PATH, SURFACE_FILE_PATH
 
+# pylint: disable=protected-access
+
 
 def test_read_triangular():
     surface = Surface.read_triangular(SURFACE_FILE_PATH)
@@ -189,6 +191,71 @@ def test_add_rectangle_3(vertices_coords, expected_extra_vertex_coords):
         == pytest.approx(expected_extra_vertex_coords)
 
 
+def test__triangle_count_by_adjacent_vertex_indices_empty():
+    surface = Surface()
+    assert surface._triangle_count_by_adjacent_vertex_indices() == {}
+
+
+def test__triangle_count_by_adjacent_vertex_indices_none():
+    surface = Surface()
+    surface.vertices.append(Vertex(1, 0, 0))
+    surface.vertices.append(Vertex(2, 0, 0))
+    surface.vertices.append(Vertex(3, 0, 0))
+    assert surface._triangle_count_by_adjacent_vertex_indices() \
+        == {0: {}, 1: {}, 2: {}}
+
+
+def test__triangle_count_by_adjacent_vertex_indices_single():
+    surface = Surface()
+    surface.triangles.append(Triangle([surface.add_vertex(Vertex(i, 0, 0))
+                                       for i in range(3)]))
+    assert surface._triangle_count_by_adjacent_vertex_indices() \
+        == {0: {1: 1, 2: 1},
+            1: {0: 1, 2: 1},
+            2: {0: 1, 1: 1}}
+
+
+def test__triangle_count_by_adjacent_vertex_indices_multiple():
+    surface = Surface()
+    for i in range(5):
+        surface.add_vertex(Vertex(i, 0, 0))
+    surface.triangles.append(Triangle((0, 1, 2)))
+    surface.triangles.append(Triangle((3, 1, 2)))
+    assert surface._triangle_count_by_adjacent_vertex_indices() \
+        == {0: {1: 1, 2: 1},
+            1: {0: 1, 2: 2, 3: 1},
+            2: {0: 1, 1: 2, 3: 1},
+            3: {1: 1, 2: 1},
+            4: {}}
+    surface.triangles.append(Triangle((3, 4, 2)))
+    assert surface._triangle_count_by_adjacent_vertex_indices() \
+        == {0: {1: 1, 2: 1},
+            1: {0: 1, 2: 2, 3: 1},
+            2: {0: 1, 1: 2, 3: 2, 4: 1},
+            3: {1: 1, 2: 2, 4: 1},
+            4: {2: 1, 3: 1}}
+    surface.triangles.append(Triangle((3, 0, 2)))
+    assert surface._triangle_count_by_adjacent_vertex_indices() \
+        == {0: {1: 1, 2: 2, 3: 1},
+            1: {0: 1, 2: 2, 3: 1},
+            2: {0: 2, 1: 2, 3: 3, 4: 1},
+            3: {0: 1, 1: 1, 2: 3, 4: 1},
+            4: {2: 1, 3: 1}}
+
+
+def test__triangle_count_by_adjacent_vertex_indices_real():
+    surface = Surface.read_triangular(SURFACE_FILE_PATH)
+    counts = surface._triangle_count_by_adjacent_vertex_indices()
+    assert len(counts) == len(surface.vertices)
+    assert all(counts.values())
+    assert all(count == 2
+               for vertex_counts in counts.values()
+               for count in vertex_counts.values())
+    assert sum(count for vertex_counts in counts.values()
+               for count in vertex_counts.values()) \
+        == len(surface.triangles) * 6
+
+
 def test__get_vertex_label_index():
     surface = Surface.read_triangular(SURFACE_FILE_PATH)
     surface.load_annotation_file(ANNOTATION_FILE_PATH)