Parcourir la source

class PolygonalCircuit is now public

Fabian Peter Hammerle il y a 5 ans
Parent
commit
de9ccd372e
3 fichiers modifiés avec 65 ajouts et 65 suppressions
  1. 10 10
      freesurfer_surface/__init__.py
  2. 44 44
      tests/test_polygonal_circuit.py
  3. 11 11
      tests/test_surface.py

+ 10 - 10
freesurfer_surface/__init__.py

@@ -99,7 +99,7 @@ class Vertex(numpy.ndarray):
         return '{}({})'.format(type(self).__name__, self.__format_coords())
 
 
-class _PolygonalCircuit:
+class PolygonalCircuit:
 
     _VERTEX_INDICES_TYPE = typing.Tuple[int]
 
@@ -118,7 +118,7 @@ class _PolygonalCircuit:
             vertex_indices.rotate(1)
         return type(self)(vertex_indices)
 
-    def __eq__(self, other: '_PolygonalCircuit') -> bool:
+    def __eq__(self, other: 'PolygonalCircuit') -> bool:
         # pylint: disable=protected-access
         return self._normalize().vertex_indices == other._normalize().vertex_indices
 
@@ -139,11 +139,11 @@ class _PolygonalCircuit:
                      for offset in range(vertices_num)))
 
 
-class _LineSegment(_PolygonalCircuit):
+class _LineSegment(PolygonalCircuit):
 
     # pylint: disable=no-member
-    @_PolygonalCircuit.vertex_indices.setter
-    def vertex_indices(self, indices: _PolygonalCircuit._VERTEX_INDICES_TYPE):
+    @PolygonalCircuit.vertex_indices.setter
+    def vertex_indices(self, indices: PolygonalCircuit._VERTEX_INDICES_TYPE):
         assert len(indices) == 2
         # pylint: disable=attribute-defined-outside-init
         self._vertex_indices = tuple(indices)
@@ -152,9 +152,9 @@ class _LineSegment(_PolygonalCircuit):
         return '_LineSegment(vertex_indices={})'.format(self.vertex_indices)
 
 
-class Triangle(_PolygonalCircuit):
+class Triangle(PolygonalCircuit):
 
-    def __init__(self, indices: _PolygonalCircuit._VERTEX_INDICES_TYPE):
+    def __init__(self, indices: PolygonalCircuit._VERTEX_INDICES_TYPE):
         super().__init__(indices)
         assert len(self.vertex_indices) == 3
 
@@ -423,12 +423,12 @@ class Surface:
                 counts[vertex_index_pair[1]][vertex_index_pair[0]] += 1
         return counts
 
-    def find_borders(self) -> typing.Iterator[_PolygonalCircuit]:
+    def find_borders(self) -> typing.Iterator[PolygonalCircuit]:
         border_neighbours = {}
         for vertex_index, neighbour_counts \
                 in self._triangle_count_by_adjacent_vertex_indices().items():
             if not neighbour_counts:
-                yield _PolygonalCircuit((vertex_index,))
+                yield PolygonalCircuit((vertex_index,))
             else:
                 neighbours = [neighbour_index for neighbour_index, counts
                               in neighbour_counts.items()
@@ -445,7 +445,7 @@ class Surface:
                 neighbour_indices.remove(cycle_indices[-1])
                 cycle_indices.append(vertex_index)
                 vertex_index, = neighbour_indices
-            yield _PolygonalCircuit(cycle_indices)
+            yield PolygonalCircuit(cycle_indices)
 
     def _get_vertex_label_index(self, vertex_index: int) -> typing.Optional[int]:
         return self.annotation.vertex_label_index.get(vertex_index, None)

+ 44 - 44
tests/test_polygonal_circuit.py

@@ -1,20 +1,20 @@
 import pytest
 
-from freesurfer_surface import _PolygonalCircuit
+from freesurfer_surface import PolygonalCircuit
 
 
 def test_init_tuple():
-    circuit = _PolygonalCircuit((1, 2, 3, 5))
+    circuit = PolygonalCircuit((1, 2, 3, 5))
     assert circuit.vertex_indices == (1, 2, 3, 5)
 
 
 def test_init_list():
-    circuit = _PolygonalCircuit([1, 2, 3, 5])
+    circuit = PolygonalCircuit([1, 2, 3, 5])
     assert circuit.vertex_indices == (1, 2, 3, 5)
 
 
 def test_init_iterator():
-    circuit = _PolygonalCircuit(iter([1, 2, 3, 5]))
+    circuit = PolygonalCircuit(iter([1, 2, 3, 5]))
     assert circuit.vertex_indices == (1, 2, 3, 5)
 
 
@@ -36,59 +36,59 @@ def test_init_iterator():
 ])
 def test__normalize(source_vertex_indices, expected_vertex_indices):
     # pylint: disable=protected-access
-    circuit = _PolygonalCircuit(source_vertex_indices)
+    circuit = PolygonalCircuit(source_vertex_indices)
     assert expected_vertex_indices == circuit._normalize()._vertex_indices
 
 
 def test_eq():
-    assert _PolygonalCircuit((0, 1)) == _PolygonalCircuit((0, 1))
-    assert _PolygonalCircuit((0, 1)) == _PolygonalCircuit((1, 0))
-    assert _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((0, 1, 2))
-    assert _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((1, 2, 0))
-    assert _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((2, 0, 1))
+    assert PolygonalCircuit((0, 1)) == PolygonalCircuit((0, 1))
+    assert PolygonalCircuit((0, 1)) == PolygonalCircuit((1, 0))
+    assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 1, 2))
+    assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((1, 2, 0))
+    assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((2, 0, 1))
     # pylint: disable=unneeded-not
-    assert not _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((0, 1, 4))
-    assert not _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((0, 4, 2))
-    assert not _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((4, 1, 2))
+    assert not PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 1, 4))
+    assert not PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 4, 2))
+    assert not PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((4, 1, 2))
 
 
 def test_eq_reverse():
-    assert _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((2, 1, 0))
-    assert _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((0, 2, 1))
-    assert _PolygonalCircuit((0, 1, 2, 4)) == _PolygonalCircuit((4, 2, 1, 0))
-    assert _PolygonalCircuit((0, 1, 2, 4)) == _PolygonalCircuit((1, 0, 4, 2))
+    assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((2, 1, 0))
+    assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 2, 1))
+    assert PolygonalCircuit((0, 1, 2, 4)) == PolygonalCircuit((4, 2, 1, 0))
+    assert PolygonalCircuit((0, 1, 2, 4)) == PolygonalCircuit((1, 0, 4, 2))
 
 
 def test_hash():
-    assert hash(_PolygonalCircuit((0, 1, 2))) \
-        == hash(_PolygonalCircuit((0, 1, 2)))
-    assert hash(_PolygonalCircuit((0, 1, 2))) \
-        == hash(_PolygonalCircuit((1, 2, 0)))
-    assert hash(_PolygonalCircuit((0, 1, 2))) \
-        == hash(_PolygonalCircuit((2, 0, 1)))
-    assert hash(_PolygonalCircuit((0, 1, 2))) \
-        != hash(_PolygonalCircuit((0, 1, 4)))
-    assert hash(_PolygonalCircuit((0, 1, 2))) \
-        != hash(_PolygonalCircuit((0, 4, 2)))
-    assert hash(_PolygonalCircuit((0, 1, 2))) \
-        != hash(_PolygonalCircuit((4, 1, 2)))
+    assert hash(PolygonalCircuit((0, 1, 2))) \
+        == hash(PolygonalCircuit((0, 1, 2)))
+    assert hash(PolygonalCircuit((0, 1, 2))) \
+        == hash(PolygonalCircuit((1, 2, 0)))
+    assert hash(PolygonalCircuit((0, 1, 2))) \
+        == hash(PolygonalCircuit((2, 0, 1)))
+    assert hash(PolygonalCircuit((0, 1, 2))) \
+        != hash(PolygonalCircuit((0, 1, 4)))
+    assert hash(PolygonalCircuit((0, 1, 2))) \
+        != hash(PolygonalCircuit((0, 4, 2)))
+    assert hash(PolygonalCircuit((0, 1, 2))) \
+        != hash(PolygonalCircuit((4, 1, 2)))
 
 
 def test_hash_reverse():
-    assert hash(_PolygonalCircuit((0, 1, 2))) \
-        == hash(_PolygonalCircuit((2, 1, 0)))
-    assert hash(_PolygonalCircuit((0, 1, 2))) \
-        == hash(_PolygonalCircuit((0, 2, 1)))
-    assert hash(_PolygonalCircuit((0, 1, 2, 4))) \
-        == hash(_PolygonalCircuit((4, 2, 1, 0)))
-    assert hash(_PolygonalCircuit((0, 1, 2, 4))) \
-        == hash(_PolygonalCircuit((1, 0, 4, 2)))
-    assert hash(_PolygonalCircuit((0, 1, 2, 4))) \
-        != hash(_PolygonalCircuit((1, 4, 0, 2)))
+    assert hash(PolygonalCircuit((0, 1, 2))) \
+        == hash(PolygonalCircuit((2, 1, 0)))
+    assert hash(PolygonalCircuit((0, 1, 2))) \
+        == hash(PolygonalCircuit((0, 2, 1)))
+    assert hash(PolygonalCircuit((0, 1, 2, 4))) \
+        == hash(PolygonalCircuit((4, 2, 1, 0)))
+    assert hash(PolygonalCircuit((0, 1, 2, 4))) \
+        == hash(PolygonalCircuit((1, 0, 4, 2)))
+    assert hash(PolygonalCircuit((0, 1, 2, 4))) \
+        != hash(PolygonalCircuit((1, 4, 0, 2)))
 
 
 def test_adjacent_vertex_indices_1():
-    chain = _PolygonalCircuit((0, 1, 4, 8))
+    chain = PolygonalCircuit((0, 1, 4, 8))
     singles = list(chain.adjacent_vertex_indices(1))
     assert len(singles) == 4
     assert singles[0] == (0,)
@@ -98,7 +98,7 @@ def test_adjacent_vertex_indices_1():
 
 
 def test_adjacent_vertex_indices_2():
-    chain = _PolygonalCircuit((0, 1, 4, 8))
+    chain = PolygonalCircuit((0, 1, 4, 8))
     pairs = list(chain.adjacent_vertex_indices(2))
     assert len(pairs) == 4
     assert pairs[0] == (0, 1)
@@ -108,7 +108,7 @@ def test_adjacent_vertex_indices_2():
 
 
 def test_adjacent_vertex_indices_3():
-    chain = _PolygonalCircuit((0, 1, 4, 8))
+    chain = PolygonalCircuit((0, 1, 4, 8))
     triplets = list(chain.adjacent_vertex_indices(3))
     assert len(triplets) == 4
     assert triplets[0] == (0, 1, 4)
@@ -118,7 +118,7 @@ def test_adjacent_vertex_indices_3():
 
 
 def test_adjacent_vertex_indices_4():
-    chain = _PolygonalCircuit((0, 1, 4, 8))
+    chain = PolygonalCircuit((0, 1, 4, 8))
     quadruples = list(chain.adjacent_vertex_indices(4))
     assert len(quadruples) == 4
     assert quadruples[0] == (0, 1, 4, 8)
@@ -128,7 +128,7 @@ def test_adjacent_vertex_indices_4():
 
 
 def test_adjacent_vertex_indices_5():
-    chain = _PolygonalCircuit((0, 1, 4, 8))
+    chain = PolygonalCircuit((0, 1, 4, 8))
     quintuples = list(chain.adjacent_vertex_indices(5))
     assert len(quintuples) == 4
     assert quintuples[0] == (0, 1, 4, 8, 0)

+ 11 - 11
tests/test_surface.py

@@ -4,7 +4,7 @@ import numpy
 import pytest
 
 from freesurfer_surface import setlocale, Vertex, Triangle, _LineSegment, \
-                               Annotation, Surface, _PolygonalCircuit
+                               Annotation, Surface, PolygonalCircuit
 
 from conftest import ANNOTATION_FILE_PATH, SURFACE_FILE_PATH
 
@@ -266,7 +266,7 @@ def test_find_borders_single():
     single_index = surface.add_vertex(Vertex(0, 21, 42))
     borders = list(surface.find_borders())
     assert len(borders) == 1
-    assert borders[0] == _PolygonalCircuit((single_index,))
+    assert borders[0] == PolygonalCircuit((single_index,))
 
 
 def test_find_borders_singles():
@@ -275,9 +275,9 @@ def test_find_borders_singles():
                       for i in range(3)]
     borders = set(surface.find_borders())
     assert len(borders) == 3
-    assert _PolygonalCircuit((single_indices[0],)) in borders
-    assert _PolygonalCircuit((single_indices[1],)) in borders
-    assert _PolygonalCircuit((single_indices[2],)) in borders
+    assert PolygonalCircuit((single_indices[0],)) in borders
+    assert PolygonalCircuit((single_indices[1],)) in borders
+    assert PolygonalCircuit((single_indices[2],)) in borders
 
 
 def test_find_borders_single_triangle_simple():
@@ -287,7 +287,7 @@ def test_find_borders_single_triangle_simple():
     surface.triangles.append(Triangle(vertex_indices))
     borders = set(surface.find_borders())
     assert len(borders) == 1
-    assert _PolygonalCircuit(vertex_indices) in borders
+    assert PolygonalCircuit(vertex_indices) in borders
 
 
 def test_find_borders_single_triangle_real():
@@ -297,7 +297,7 @@ def test_find_borders_single_triangle_real():
     surface.triangles.append(Triangle(vertex_indices))
     borders = set(surface.find_borders())
     assert len(borders) == 1
-    assert _PolygonalCircuit(vertex_indices) in borders
+    assert PolygonalCircuit(vertex_indices) in borders
 
 
 def test_find_borders_remove_triangle():
@@ -328,17 +328,17 @@ def test_find_borders_remove_adjacent_triangles():
     borders = set(surface.find_borders())
     assert len(borders) == 2
     assert triangles[0] in borders
-    assert _PolygonalCircuit((137076, 136141, 135263, 136142)) in borders
+    assert PolygonalCircuit((137076, 136141, 135263, 136142)) in borders
     surface.triangles.pop(270682)
     borders = set(surface.find_borders())
     assert len(borders) == 2
     assert triangles[0] in borders
-    assert _PolygonalCircuit((137076, 136141, 135263, 135264, 136142)) in borders
+    assert PolygonalCircuit((137076, 136141, 135263, 135264, 136142)) in borders
     surface.triangles.pop(274320)
     borders = set(surface.find_borders())
     assert len(borders) == 2
-    assert _PolygonalCircuit((136143, 138007, 138008, 137078)) in borders
-    assert _PolygonalCircuit((137076, 136141, 135263, 135264, 136142)) in borders
+    assert PolygonalCircuit((136143, 138007, 138008, 137078)) in borders
+    assert PolygonalCircuit((137076, 136141, 135263, 135264, 136142)) in borders
 
 
 def test__get_vertex_label_index():