Browse Source

added ashs.HippocampalSubfieldsVolumeFile.__init__: parses an ASHS *volumes.txt filename

https://sites.google.com/site/hipposubfields/tutorial#TOC-Viewing-ASHS-Segmentation-Results
Fabian Peter Hammerle 5 years ago
parent
commit
1d9c516b63
2 changed files with 79 additions and 0 deletions
  1. 34 0
      freesurfer_volume_reader/ashs.py
  2. 45 0
      tests/ashs_test.py

+ 34 - 0
freesurfer_volume_reader/ashs.py

@@ -0,0 +1,34 @@
+"""
+Read hippocampal subfield volumes computed by ASHS
+
+https://sites.google.com/site/hipposubfields/home
+
+>>> from freesurfer_volume_reader.ashs import HippocampalSubfieldsVolumeFile
+>>>
+>>> volume_file = HippocampalSubfieldsVolumeFile('ashs/final/bert_right_corr_nogray_volumes.txt')
+>>> print(volume_file.subject, volume_file.hemisphere, volume_file.correction)
+"""
+
+import os
+import re
+
+# pylint: disable=too-few-public-methods
+class HippocampalSubfieldsVolumeFile:
+
+    # https://sites.google.com/site/hipposubfields/tutorial#TOC-Viewing-ASHS-Segmentation-Results
+    FILENAME_PATTERN = r'^(?P<s>\w+)_(?P<h>left|right)' \
+                       r'_(heur|corr_(?P<c>nogray|usegray))_volumes.txt$'
+    FILENAME_REGEX = re.compile(FILENAME_PATTERN)
+
+    def __init__(self, path: str):
+        self._absolute_path = os.path.abspath(path)
+        filename_match = self.FILENAME_REGEX.match(os.path.basename(path))
+        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']
+
+    @property
+    def absolute_path(self):
+        return self._absolute_path

+ 45 - 0
tests/ashs_test.py

@@ -0,0 +1,45 @@
+import os
+
+import pytest
+
+from freesurfer_volume_reader.ashs import HippocampalSubfieldsVolumeFile
+
+
+@pytest.mark.parametrize(('volume_file_path', 'expected_attrs'), [
+    ('ashs/final/bert_left_heur_volumes.txt',
+     {'subject': 'bert', 'hemisphere': 'left', 'correction': None}),
+    ('ashs/final/bert_left_corr_nogray_volumes.txt',
+     {'subject': 'bert', 'hemisphere': 'left', 'correction': 'nogray'}),
+    ('ashs/final/bert_left_corr_usegray_volumes.txt',
+     {'subject': 'bert', 'hemisphere': 'left', 'correction': 'usegray'}),
+    ('ashs/final/bert_right_heur_volumes.txt',
+     {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
+    ('ashs/final/bert_right_corr_nogray_volumes.txt',
+     {'subject': 'bert', 'hemisphere': 'right', 'correction': 'nogray'}),
+    ('ashs/final/bert_right_corr_usegray_volumes.txt',
+     {'subject': 'bert', 'hemisphere': 'right', 'correction': 'usegray'}),
+    ('somewhere/else/bert_right_heur_volumes.txt',
+     {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
+    ('somewhere/else/bert_right_corr_nogray_volumes.txt',
+     {'subject': 'bert', 'hemisphere': 'right', 'correction': 'nogray'}),
+    ('bert_right_heur_volumes.txt',
+     {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
+    ('/foo/bar/alice_20190503_right_corr_nogray_volumes.txt',
+     {'subject': 'alice_20190503', 'hemisphere': 'right', 'correction': 'nogray'}),
+])
+def test_hippocampal_subfields_volume_file_init(volume_file_path, expected_attrs):
+    volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
+    assert os.path.basename(volume_file_path) == os.path.basename(volume_file.absolute_path)
+    for attr, value in expected_attrs.items():
+        assert value == getattr(volume_file, attr)
+
+
+@pytest.mark.parametrize('volume_file_path', [
+    'bert_middle_heur_volumes.txt',
+    'bert_right_hear_volumes.txt',
+    'bert_right_heur_volumes.nii',
+    'bert_left_lfseg_corr_usegray.nii.gz',
+])
+def test_hippocampal_subfields_volume_file_init_invalid(volume_file_path):
+    with pytest.raises(Exception):
+        HippocampalSubfieldsVolumeFile(path=volume_file_path)