Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
refactor: reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
fphammerle committed May 7, 2022
1 parent 8fde0e3 commit 9f3d402
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 40 deletions.
8 changes: 4 additions & 4 deletions freesurfer_volume_reader/__init__.py
Expand Up @@ -17,6 +17,7 @@

import abc
import os
import pathlib
import re
import typing
import warnings
Expand Down Expand Up @@ -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(
Expand Down
15 changes: 2 additions & 13 deletions freesurfer_volume_reader/ashs.py
Expand Up @@ -18,7 +18,6 @@
>>> print(volume_file.read_volume_series())
"""

import pathlib
import re
import typing

Expand All @@ -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 = (
Expand All @@ -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 = {}
Expand Down
8 changes: 1 addition & 7 deletions freesurfer_volume_reader/freesurfer.py
Expand Up @@ -11,7 +11,6 @@
>>> print(volume_file.read_volumes_dataframe())
"""

import pathlib
import re
import typing

Expand All @@ -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)
Expand All @@ -44,11 +43,6 @@ def __init__(self, path: str):
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 = {}
Expand Down
24 changes: 8 additions & 16 deletions tests/init_test.py
Expand Up @@ -54,23 +54,21 @@ 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

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):

Expand All @@ -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()

Expand All @@ -92,8 +86,6 @@ def read_volumes_dataframe(self):

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):
Expand Down

0 comments on commit 9f3d402

Please sign in to comment.