Browse Source

added method method PolygonalCircuit.normalized()

Fabian Peter Hammerle 3 years ago
parent
commit
bc3d206e3d

+ 5 - 1
CHANGELOG.md

@@ -5,8 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ## [Unreleased]
+### Added
+- method `PolygonalCircuit.normalized()`
+
 ### Fixed
-- `find_label_border_polygonal_chains`: always include vertices along border with single neighbour
+- `PolygonalCircuit.find_label_border_polygonal_chains`:
+  always include vertices along border with single neighbour
   (previously indeterministic behaviour)
 - type hints
 

File diff suppressed because it is too large
+ 0 - 0
examples/precentral_gyrus_border.ipynb


+ 8 - 0
freesurfer_surface/__init__.py

@@ -190,6 +190,14 @@ class PolygonalChain:
             vertex_indices
         )  # type: typing.Deque[int]
 
+    def normalized(self) -> "PolygonalChain":
+        vertex_indices = list(self.vertex_indices)
+        min_index = vertex_indices.index(min(vertex_indices))
+        indices_min_first = vertex_indices[min_index:] + vertex_indices[:min_index]
+        if indices_min_first[1] < indices_min_first[-1]:
+            return PolygonalChain(indices_min_first)
+        return PolygonalChain(indices_min_first[0:1] + indices_min_first[-1:0:-1])
+
     def __eq__(self, other: object) -> bool:
         return (
             isinstance(other, PolygonalChain)

+ 24 - 9
tests/test_polygonal_chain.py

@@ -18,6 +18,28 @@ def test_reassign_vertex_indices():
     assert tuple(chain.vertex_indices) == (1, 2, 3, 4)
 
 
+@pytest.mark.parametrize(
+    ("indices_init", "indices_normalized"),
+    (
+        ([0, 3], [0, 3]),
+        ([3, 0], [0, 3]),
+        ([0, 3, 2, 4], [0, 3, 2, 4]),
+        ([0, 4, 2, 3], [0, 3, 2, 4]),
+        ([2, 3, 0, 4], [0, 3, 2, 4]),
+        ([2, 4, 0, 3], [0, 3, 2, 4]),
+        ([3, 0, 4, 2], [0, 3, 2, 4]),
+        ([3, 2, 4, 0], [0, 3, 2, 4]),
+        ([4, 0, 3, 2], [0, 3, 2, 4]),
+        ([4, 2, 3, 0], [0, 3, 2, 4]),
+    ),
+)
+def test_normalized(indices_init, indices_normalized):
+    assert (
+        list(PolygonalChain(indices_init).normalized().vertex_indices)
+        == indices_normalized
+    )
+
+
 def test_eq():
     assert PolygonalChain((0, 1, 2)) == PolygonalChain((0, 1, 2))
     # pylint: disable=unneeded-not
@@ -57,10 +79,7 @@ def test_connect(vertex_indices_a, vertex_indices_b, expected_vertex_indices):
 
 
 @pytest.mark.parametrize(
-    ("vertex_indices_a", "vertex_indices_b"),
-    [
-        ((1, 2, 3), (2, 4)),
-    ],
+    ("vertex_indices_a", "vertex_indices_b"), [((1, 2, 3), (2, 4))]
 )
 def test_connect_fail(vertex_indices_a, vertex_indices_b):
     chain = PolygonalChain(vertex_indices_a)
@@ -69,11 +88,7 @@ def test_connect_fail(vertex_indices_a, vertex_indices_b):
 
 
 @pytest.mark.parametrize(
-    ("vertex_indices_a", "vertex_indices_b"),
-    [
-        ((1, 2, 3), ()),
-        ((), (3, 4)),
-    ],
+    ("vertex_indices_a", "vertex_indices_b"), [((1, 2, 3), ()), ((), (3, 4))]
 )
 def test_connect_fail_empty(vertex_indices_a, vertex_indices_b):
     chain = PolygonalChain(vertex_indices_a)

+ 1 - 1
tests/test_surface.py

@@ -485,7 +485,7 @@ def test_find_label_border_polygonal_chains():
     assert len(vertex_indices) == 418
     min_index = vertex_indices.index(min(vertex_indices))
     vertex_indices_normalized = vertex_indices[min_index:] + vertex_indices[:min_index]
-    if vertex_indices_normalized[-1] > vertex_indices_normalized[1]:
+    if vertex_indices_normalized[-1] < vertex_indices_normalized[1]:
         vertex_indices_normalized.reverse()
     assert vertex_indices_normalized[346:353] == [
         63264,

Some files were not shown because too many files changed in this diff