Browse Source

refactor: reduce code duplication

Fabian Peter Hammerle 1 year ago
parent
commit
9f3d402358

+ 4 - 4
freesurfer_volume_reader/__init__.py

@@ -17,6 +17,7 @@ https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfields
 
 import abc
 import os
+import pathlib
 import re
 import typing
 import warnings
@@ -48,12 +49,11 @@ class VolumeFile(metaclass=abc.ABCMeta):
 
     @abc.abstractmethod
     def __init__(self, path: str) -> None:
-        pass
+        self._absolute_path = pathlib.Path(path).absolute()
 
     @property
-    @abc.abstractmethod
-    def absolute_path(self):
-        raise NotImplementedError()
+    def absolute_path(self) -> str:
+        return str(self._absolute_path)
 
     @classmethod
     def find(

+ 2 - 13
freesurfer_volume_reader/ashs.py

@@ -18,7 +18,6 @@ https://sites.google.com/site/hipposubfields/home
 >>>     print(volume_file.read_volume_series())
 """
 
-import pathlib
 import re
 import typing
 
@@ -32,15 +31,10 @@ class IntracranialVolumeFile(freesurfer_volume_reader.VolumeFile):
     FILENAME_REGEX = re.compile(r"^(?P<s>\w+)_icv.txt$")
 
     def __init__(self, path: str):
-        self._absolute_path = pathlib.Path(path).absolute()
+        super().__init__(path=path)
         filename_match = self.FILENAME_REGEX.match(self._absolute_path.name)
         assert filename_match, self._absolute_path
         self.subject = filename_match.groupdict()["s"]
-        super().__init__(path=path)
-
-    @property
-    def absolute_path(self):
-        return str(self._absolute_path)
 
     def read_volume_mm3(self) -> float:
         subject, icv = (
@@ -67,18 +61,13 @@ class HippocampalSubfieldsVolumeFile(freesurfer_volume_reader.SubfieldVolumeFile
     FILENAME_REGEX = re.compile(FILENAME_PATTERN)
 
     def __init__(self, path: str):
-        self._absolute_path = pathlib.Path(path).absolute()
+        super().__init__(path=path)
         filename_match = self.FILENAME_REGEX.match(self._absolute_path.name)
         assert filename_match, self._absolute_path
         filename_groups = filename_match.groupdict()
         self.subject = filename_groups["s"]
         self.hemisphere = filename_groups["h"]
         self.correction = filename_groups["c"]
-        super().__init__(path=path)
-
-    @property
-    def absolute_path(self):
-        return str(self._absolute_path)
 
     def read_volumes_mm3(self) -> typing.Dict[str, float]:
         subfield_volumes = {}

+ 1 - 7
freesurfer_volume_reader/freesurfer.py

@@ -11,7 +11,6 @@ https://github.com/freesurfer/freesurfer/tree/release_6_0_0/HippoSF
 >>>     print(volume_file.read_volumes_dataframe())
 """
 
-import pathlib
 import re
 import typing
 
@@ -32,7 +31,7 @@ class HippocampalSubfieldsVolumeFile(freesurfer_volume_reader.SubfieldVolumeFile
     FILENAME_HEMISPHERE_PREFIX_MAP = {"l": "left", "r": "right"}
 
     def __init__(self, path: str):
-        self._absolute_path = pathlib.Path(path).absolute()
+        super().__init__(path=path)
         subject_dir_path = self._absolute_path.parent.parent
         self.subject = subject_dir_path.name
         filename_match = self.FILENAME_REGEX.match(self._absolute_path.name)
@@ -44,11 +43,6 @@ class HippocampalSubfieldsVolumeFile(freesurfer_volume_reader.SubfieldVolumeFile
         self.hemisphere = self.FILENAME_HEMISPHERE_PREFIX_MAP[filename_groups["h"]]
         self.t1_input = filename_groups["T1"] is not None
         self.analysis_id = filename_groups["analysis_id"]
-        super().__init__(path=path)
-
-    @property
-    def absolute_path(self):
-        return str(self._absolute_path)
 
     def read_volumes_mm3(self) -> typing.Dict[str, float]:
         subfield_volumes = {}

+ 8 - 16
tests/init_test.py

@@ -54,6 +54,14 @@ def test_remove_group_names_from_regex(source_pattern, expected_pattern):
     )
 
 
+def test_volume_file_abstract():
+    with pytest.raises(
+        TypeError,
+        match=r"^Can't instantiate abstract class VolumeFile with abstract methods? __init__$",
+    ):
+        VolumeFile(path="/tmp/test")  # pylint: disable=abstract-class-instantiated
+
+
 class DummyVolumeFile(VolumeFile):
 
     # pylint: disable=useless-super-delegation
@@ -61,16 +69,6 @@ class DummyVolumeFile(VolumeFile):
     def __init__(self, path: str) -> None:
         super().__init__(path=path)
 
-    @property
-    def absolute_path(self):
-        return super().absolute_path
-
-
-def test_volume_file_abstractmethod():
-    volume_file = DummyVolumeFile(path="dummy")
-    with pytest.raises(NotImplementedError):
-        assert volume_file.absolute_path
-
 
 class DummySubfieldVolumeFile(SubfieldVolumeFile):
 
@@ -79,10 +77,6 @@ class DummySubfieldVolumeFile(SubfieldVolumeFile):
     def __init__(self, path: str) -> None:
         super().__init__(path=path)
 
-    @property
-    def absolute_path(self):
-        return super().absolute_path
-
     def read_volumes_mm3(self):
         return super().read_volumes_mm3()
 
@@ -92,8 +86,6 @@ class DummySubfieldVolumeFile(SubfieldVolumeFile):
 
 def test_subfield_volume_file_abstractmethod():
     volume_file = DummySubfieldVolumeFile(path="subfield-dummy")
-    with pytest.raises(NotImplementedError):
-        assert volume_file.absolute_path
     with pytest.raises(NotImplementedError):
         volume_file.read_volumes_mm3()
     with pytest.raises(NotImplementedError):