Browse Source

wrapped some lines with len > 80

Fabian Peter Hammerle 5 years ago
parent
commit
5093716d9e

+ 14 - 7
freesurfer_surface/__init__.py

@@ -129,7 +129,8 @@ class PolygonalChainsNotOverlapingError(ValueError):
 class PolygonalChain:
 
     def __init__(self, vertex_indices: typing.Iterable[int]):
-        self.vertex_indices: typing.Deque[int] = collections.deque(vertex_indices)
+        self.vertex_indices: typing.Deque[int] \
+            = collections.deque(vertex_indices)
 
     def __eq__(self, other: 'PolygonalChain') -> bool:
         return self.vertex_indices == other.vertex_indices
@@ -205,7 +206,8 @@ class Annotation:
         index, name_length = struct.unpack('>II', stream.read(4 * 2))
         name = stream.read(name_length - 1).decode()
         assert stream.read(1) == b'\0'
-        red, green, blue, transparency = struct.unpack('>IIII', stream.read(4 * 4))
+        red, green, blue, transparency \
+            = struct.unpack('>IIII', stream.read(4 * 4))
         return Label(index=index, name=name, red=red, green=green,
                      blue=blue, transparency=transparency)
 
@@ -215,7 +217,8 @@ class Annotation:
         annotations = [struct.unpack('>II', stream.read(4 * 2))
                        for _ in range(annotations_num)]
         assert stream.read(4) == self._TAG_OLD_COLORTABLE
-        colortable_version, _, filename_length = struct.unpack('>III', stream.read(4 * 3))
+        colortable_version, _, filename_length \
+            = struct.unpack('>III', stream.read(4 * 3))
         assert colortable_version > 0  # new version
         self.colortable_path = stream.read(filename_length - 1)
         assert stream.read(1) == b'\0'
@@ -309,7 +312,8 @@ class Surface:
         return surface
 
     def _triangular_creation_datetime_strftime(self) -> bytes:
-        fmt = self._DATETIME_FORMAT.replace('%d', '{:>2}'.format(self.creation_datetime.day))
+        padded_day = '{:>2}'.format(self.creation_datetime.day)
+        fmt = self._DATETIME_FORMAT.replace('%d', padded_day)
         with setlocale('C'):
             return self.creation_datetime.strftime(fmt).encode()
 
@@ -332,7 +336,8 @@ class Surface:
             for triangle in self.triangles:
                 assert all(vertex_index < len(self.vertices)
                            for vertex_index in triangle.vertex_indices)
-                surface_file.write(struct.pack('>III', *triangle.vertex_indices))
+                surface_file.write(struct.pack('>III',
+                                               *triangle.vertex_indices))
             surface_file.write(self._TAG_OLD_USEREALRAS
                                + struct.pack('>I', 1 if self.using_old_real_ras else 0))
             surface_file.write(self._TAG_OLD_SURF_GEOM
@@ -356,10 +361,12 @@ class Surface:
         assert len(vertex_indices) == 3
         vertex_coords = [numpy.array(self.vertices[vertex_index])
                          for vertex_index in vertex_indices]
-        vertex_coords.append(vertex_coords[0] + vertex_coords[2] - vertex_coords[1])
+        vertex_coords.append(vertex_coords[0]
+                             + vertex_coords[2] - vertex_coords[1])
         vertex_indices.append(self.add_vertex(Vertex(*vertex_coords[3])))
         self.triangles.append(Triangle(vertex_indices[:3]))
-        self.triangles.append(Triangle(vertex_indices[2:] + vertex_indices[:1]))
+        self.triangles.append(Triangle(vertex_indices[2:]
+                                       + vertex_indices[:1]))
 
     def _find_label_border_segments(self, label: Label) -> typing.Iterator[_LineSegment]:
         for triangle in self.triangles:

+ 4 - 2
freesurfer_surface/__main__.py

@@ -10,8 +10,10 @@ def annotation_labels():
     List Labels Stored in Freesurfer's Annotation File
     (i.e., label/lh.aparc.annot)
     """
-    argparser = argparse.ArgumentParser(description=annotation_labels.__doc__.strip())
-    argparser.add_argument('--delimiter', default='\t', help='default: %(default)r')
+    argparser = argparse.ArgumentParser(
+        description=annotation_labels.__doc__.strip())
+    argparser.add_argument('--delimiter', default='\t',
+                           help='default: %(default)r')
     argparser.add_argument('annotation_file_path')
     args = argparser.parse_args()
     annotation = Annotation.read(args.annotation_file_path)

+ 2 - 1
tests/conftest.py

@@ -2,5 +2,6 @@ import os
 
 SUBJECTS_DIR = os.path.join(os.path.dirname(__file__), 'subjects')
 
-ANNOTATION_FILE_PATH = os.path.join(SUBJECTS_DIR, 'fabian', 'label', 'lh.aparc.annot')
+ANNOTATION_FILE_PATH = os.path.join(SUBJECTS_DIR, 'fabian',
+                                    'label', 'lh.aparc.annot')
 SURFACE_FILE_PATH = os.path.join(SUBJECTS_DIR, 'fabian', 'surf', 'lh.pial')

+ 8 - 5
tests/test_annotation.py

@@ -2,11 +2,11 @@ import os
 
 from freesurfer_surface import Annotation
 
-from conftest import SUBJECTS_DIR
+from conftest import ANNOTATION_FILE_PATH
 
 
 def test_load_annotation():
-    annotation = Annotation.read(os.path.join(SUBJECTS_DIR, 'fabian', 'label', 'lh.aparc.annot'))
+    annotation = Annotation.read(ANNOTATION_FILE_PATH)
     assert len(annotation.vertex_label_index) == 155622
     assert annotation.vertex_label_index[64290] == 22
     assert annotation.vertex_label_index[72160] == 22
@@ -24,11 +24,14 @@ def test_load_annotation():
                                           'red': 25, 'green': 5, 'blue': 25, 'transparency': 0}
     assert vars(annotation.labels[28]) == {'index': 28, 'name': 'superiorfrontal',
                                            'red': 20, 'green': 220, 'blue': 160, 'transparency': 0}
-    precentral, = filter(lambda l: l.name == 'precentral', annotation.labels.values())
-    postcentral, = filter(lambda l: l.name == 'postcentral', annotation.labels.values())
+    precentral, = filter(lambda l: l.name == 'precentral',
+                         annotation.labels.values())
+    postcentral, = filter(lambda l: l.name == 'postcentral',
+                          annotation.labels.values())
     assert vars(precentral) == {'index': 24, 'name': 'precentral',
                                 'red': 60, 'green': 20, 'blue': 220, 'transparency': 0}
     assert vars(postcentral) == {'index': 22, 'name': 'postcentral',
                                  'red': 220, 'green': 20, 'blue': 20, 'transparency': 0}
-    superiorfrontal, = filter(lambda l: l.color_code == 10542100, annotation.labels.values())
+    superiorfrontal, = filter(lambda l: l.color_code == 10542100,
+                              annotation.labels.values())
     assert superiorfrontal.name == 'superiorfrontal'

+ 10 - 5
tests/test_label.py

@@ -11,12 +11,14 @@ from freesurfer_surface import Label
     ( 20, 220, 160,   0, 10542100),
 ])
 def test_color_code(red, green, blue, transparency, color_code):
-    label = Label(index=21, name='name', red=red, green=green, blue=blue, transparency=transparency)
+    label = Label(index=21, name='name', red=red, green=green,
+                  blue=blue, transparency=transparency)
     assert color_code == label.color_code
 
 
 def test_color_code_unknown():
-    label = Label(index=0, name='unknown', red=21, green=21, blue=21, transparency=0)
+    label = Label(index=0, name='unknown', red=21,
+                  green=21, blue=21, transparency=0)
     assert label.color_code == 0
 
 
@@ -33,15 +35,18 @@ def test_color_code_unknown():
     ( 20, 220, 160, '#14dca0'),
 ])
 def test_hex_color_code(red, green, blue, hex_color_code):
-    label = Label(index=21, name='name', red=red, green=green, blue=blue, transparency=0)
+    label = Label(index=21, name='name', red=red,
+                  green=green, blue=blue, transparency=0)
     assert hex_color_code == label.hex_color_code.lower()
 
 
 def test_str():
-    label = Label(index=24, name='precentral', red=60, green=20, blue=220, transparency=0)
+    label = Label(index=24, name='precentral', red=60,
+                  green=20, blue=220, transparency=0)
     assert str(label) == 'Label(name=precentral, index=24, color=#3c14dc)'
 
 
 def test_repr():
-    label = Label(index=24, name='precentral', red=60, green=20, blue=220, transparency=0)
+    label = Label(index=24, name='precentral', red=60,
+                  green=20, blue=220, transparency=0)
     assert repr(label) == 'Label(name=precentral, index=24, color=#3c14dc)'

+ 2 - 1
tests/test_main.py

@@ -39,4 +39,5 @@ def test_annotation_labels_script():
     proc_info = subprocess.run(['freesurfer-annotation-labels', ANNOTATION_FILE_PATH],
                                check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
     assert not proc_info.stderr
-    check_rows(list(csv.reader(io.StringIO(proc_info.stdout.decode()), delimiter='\t')))
+    check_rows(list(csv.reader(io.StringIO(proc_info.stdout.decode()),
+                               delimiter='\t')))

+ 12 - 6
tests/test_polygonal_circuit.py

@@ -28,9 +28,15 @@ def test_eq():
 
 
 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)))

+ 16 - 8
tests/test_surface.py

@@ -11,7 +11,8 @@ from conftest import ANNOTATION_FILE_PATH, SURFACE_FILE_PATH
 def test_read_triangular():
     surface = Surface.read_triangular(SURFACE_FILE_PATH)
     assert surface.creator == b'fabianpeter'
-    assert surface.creation_datetime == datetime.datetime(2019, 5, 9, 22, 37, 41)
+    assert surface.creation_datetime \
+        == datetime.datetime(2019, 5, 9, 22, 37, 41)
     assert len(surface.vertices) == 155622
     assert len(surface.triangles) == 311240
     assert not surface.using_old_real_ras
@@ -55,7 +56,8 @@ def test_read_triangular():
 def test_read_triangular_locale():
     with setlocale('de_AT.utf8'):
         surface = Surface.read_triangular(SURFACE_FILE_PATH)
-    assert surface.creation_datetime == datetime.datetime(2019, 5, 9, 22, 37, 41)
+    assert surface.creation_datetime \
+        == datetime.datetime(2019, 5, 9, 22, 37, 41)
 
 
 @pytest.mark.parametrize(('creation_datetime', 'expected_str'), [
@@ -107,11 +109,13 @@ def test_write_triangular_same_locale(tmpdir):
     creation_datetime = datetime.datetime(2018, 12, 31, 21, 42)
     output_path = tmpdir.join('surface')
     with setlocale('de_AT.utf8'):
-        surface.write_triangular(output_path, creation_datetime=creation_datetime)
+        surface.write_triangular(output_path,
+                                 creation_datetime=creation_datetime)
     resulted_surface = Surface.read_triangular(output_path)
     assert resulted_surface.creation_datetime == creation_datetime
     with open(output_path, 'rb') as output_file:
-        assert output_file.read().split(b' on ')[1].startswith(b'Mon Dec 31 21:42:00 2018\n')
+        assert output_file.read().split(b' on ')[1] \
+            .startswith(b'Mon Dec 31 21:42:00 2018\n')
 
 
 def test_load_annotation():
@@ -146,7 +150,8 @@ def test_add_rectangle(vertices_coords, expected_extra_vertex_coords):
     for vertex_coords in vertices_coords:
         surface.add_vertex(Vertex(*(float(c) for c in vertex_coords)))
     surface.add_rectangle(range(3))
-    assert tuple(surface.vertices[3]) == pytest.approx(expected_extra_vertex_coords)
+    assert tuple(surface.vertices[3]) \
+        == pytest.approx(expected_extra_vertex_coords)
     assert len(surface.triangles) == 2
     assert surface.triangles[0].vertex_indices == (0, 1, 2)
     assert surface.triangles[1].vertex_indices == (2, 3, 0)
@@ -158,15 +163,18 @@ def test__find_label_border_segments():
     precentral_label, = filter(lambda l: l.name == 'precentral',
                                surface.annotation.labels.values())
     # pylint: disable=protected-access
-    border_segments = set(surface._find_label_border_segments(precentral_label))
+    border_segments = set(
+        surface._find_label_border_segments(precentral_label))
     assert len(border_segments) == 417
     assert _LineSegment((33450, 32065)) in border_segments
     assert _LineSegment((33454, 33450)) in border_segments
     for border_vertex_index in [33450, 33454, 32065]:
         assert surface.annotation.vertex_label_index[border_vertex_index] == precentral_label.index
         for other_vertex_index in [32064, 33449, 33455, 33449, 33455]:
-            assert _LineSegment((other_vertex_index, border_vertex_index)) not in border_segments
-            assert _LineSegment((border_vertex_index, other_vertex_index)) not in border_segments
+            assert _LineSegment((other_vertex_index, border_vertex_index)) \
+                not in border_segments
+            assert _LineSegment((border_vertex_index, other_vertex_index)) \
+                not in border_segments
 
 
 def test_find_label_border_polygonal_chains():