Browse Source

added Surface.add_vertex()

Fabian Peter Hammerle 5 years ago
parent
commit
7ce9a1ac11
3 changed files with 31 additions and 1 deletions
  1. 10 1
      freesurfer_surface/__init__.py
  2. 11 0
      tests/test_surface.py
  3. 10 0
      tests/test_vertex.py

+ 10 - 1
freesurfer_surface/__init__.py

@@ -9,9 +9,14 @@ https://raw.githubusercontent.com/freesurfer/freesurfer/release_6_0_0/utils/mris
 Freesurfer
 https://surfer.nmr.mgh.harvard.edu/
 
->>> from freesurfer_surface import Surface
+>>> from freesurfer_surface import Surface, Vertex
 >>>
 >>> surface = Surface.read_triangular('bert/surf/lh.pial'))
+>>>
+>>> vertex_index = surface.add_vertex(Vertex(0.0, -3.14, 21.42))
+>>> print(surface.vertices[vertex_index])
+>>>
+>>> surface.write_triangular('somewhere/else/lh.pial')
 """
 
 import collections
@@ -124,3 +129,7 @@ class Surface:
             for command_line in self.command_lines:
                 surface_file.write(self._TAG_CMDLINE + struct.pack('>Q', len(command_line) + 1)
                                    + command_line + b'\0')
+
+    def add_vertex(self, vertex: Vertex) -> int:
+        self.vertices.append(vertex)
+        return len(self.vertices) - 1

+ 11 - 0
tests/test_surface.py

@@ -94,3 +94,14 @@ def test_write_read_triangular_same(tmpdir):
                                       creation_datetime=expected_surface.creation_datetime)
     resulted_surface = Surface.read_triangular(output_path)
     assert vars(expected_surface) == vars(resulted_surface)
+
+
+def test_add_vertex():
+    surface = Surface()
+    assert not surface.vertices
+    assert surface.add_vertex(Vertex(1.0, 1.5, 2.0)) == 0
+    assert len(surface.vertices) == 1
+    assert surface.vertices[0].anterior == pytest.approx(1.5)
+    assert surface.add_vertex(Vertex(-3.0, 0.0, 4.0)) == 1
+    assert len(surface.vertices) == 2
+    assert surface.vertices[1].right == pytest.approx(-3.0)

+ 10 - 0
tests/test_vertex.py

@@ -0,0 +1,10 @@
+import pytest
+
+from freesurfer_surface import Vertex
+
+
+def test_init():
+    vertex = Vertex(-4.0, 0.5, 21.42)
+    assert vertex.right == pytest.approx(-4.0)
+    assert vertex.anterior == pytest.approx(0.5)
+    assert vertex.superior == pytest.approx(21.42)