Ver código fonte

move/rename find_hippocampal_volume_files() to freesurfer.FreesurferHippocampalVolumeFile.find()

Fabian Peter Hammerle 5 anos atrás
pai
commit
da56fb510e

+ 3 - 3
examples/barplot.py

@@ -4,8 +4,8 @@ from matplotlib import pyplot
 import pandas
 import seaborn
 
-from freesurfer_volume_reader import find_hippocampal_volume_files, \
-                                     read_hippocampal_volume_file_dataframe
+from freesurfer_volume_reader.freesurfer import FreesurferHippocampalVolumeFile
+from freesurfer_volume_reader import read_hippocampal_volume_file_dataframe
 
 
 def abbreviate_analysis_id(analysis_id):
@@ -18,7 +18,7 @@ def main():
     argparser.add_argument('--subjects-dir', dest='subjects_dir_path', required=True)
     args = argparser.parse_args()
     volume_frame = pandas.concat(read_hippocampal_volume_file_dataframe(p)
-                                 for p in find_hippocampal_volume_files(args.subjects_dir_path))
+                                 for p in FreesurferHippocampalVolumeFile.find(args.subjects_dir_path))
     volume_frame = volume_frame[volume_frame['subject'] == args.subject]
     volume_frame['subfield_segmentation_mode'] = volume_frame.apply(
         lambda row: ' & '.join(filter(None, ('T1' if row['T1_input'] else None,

+ 8 - 15
freesurfer_volume_reader/__init__.py

@@ -11,22 +11,13 @@ import typing
 
 import pandas
 
-# https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfields
-HIPPOCAMPAL_VOLUME_FILENAME_PATTERN = r'^(?P<h>[lr])h\.hippoSfVolumes' \
-                                      r'(?P<T1>-T1)?(-(?P<analysis_id>.+?))?\.v10.txt$'
-HIPPOCAMPAL_VOLUME_FILENAME_REGEX = re.compile(HIPPOCAMPAL_VOLUME_FILENAME_PATTERN)
-DEFAULT_HIPPOCAMPAL_VOLUME_FIND_FILENAME_PATTERN = re.sub(r'\?P<.+?>', '',
-                                                          HIPPOCAMPAL_VOLUME_FILENAME_PATTERN)
+from freesurfer_volume_reader.freesurfer import FreesurferHippocampalVolumeFile
 
 VOLUME_FILENAME_HEMISPHERE_MAP = {'l': 'left', 'r': 'right'}
 
 
-def find_hippocampal_volume_files(root_dir_path: str,
-                                  filename_regex: typing.Pattern = HIPPOCAMPAL_VOLUME_FILENAME_REGEX
-                                  ) -> typing.Iterator[str]:
-    for dirpath, _, filenames in os.walk(root_dir_path):
-        for filename in filter(filename_regex.search, filenames):
-            yield os.path.join(dirpath, filename)
+def remove_group_names_from_regex(regex_pattern: str) -> str:
+    return re.sub(r'\?P<.+?>', '', regex_pattern)
 
 
 def read_hippocampal_volumes_mm3(volume_file_path: str) -> dict:
@@ -42,7 +33,8 @@ def read_hippocampal_volumes_mm3(volume_file_path: str) -> dict:
 
 def parse_hippocampal_volume_file_path(volume_file_path: str) -> dict:
     subject_dir_path = os.path.dirname(os.path.dirname(os.path.abspath(volume_file_path)))
-    filename_match = HIPPOCAMPAL_VOLUME_FILENAME_REGEX.match(os.path.basename(volume_file_path))
+    filename_match = FreesurferHippocampalVolumeFile.FILENAME_REGEX.match(
+        os.path.basename(volume_file_path))
     assert filename_match, volume_file_path
     filename_groups = filename_match.groupdict()
     assert filename_groups['T1'] or filename_groups['analysis_id'], volume_file_path
@@ -68,7 +60,8 @@ def read_hippocampal_volume_file_dataframe(volume_file_path: str) -> pandas.Data
 def main():
     argparser = argparse.ArgumentParser(description=__doc__)
     argparser.add_argument('--filename-regex', type=re.compile,
-                           default=DEFAULT_HIPPOCAMPAL_VOLUME_FIND_FILENAME_PATTERN,
+                           default=remove_group_names_from_regex(
+                               FreesurferHippocampalVolumeFile.FILENAME_PATTERN),
                            help='default: %(default)s')
     argparser.add_argument('--output-format', choices=['csv'], default='csv',
                            help='default: %(default)s')
@@ -80,7 +73,7 @@ def main():
                            help='default: $SUBJECTS_DIR ({})'.format(subjects_dir_path))
     args = argparser.parse_args()
     volume_file_paths = [p for d in args.root_dir_paths
-                         for p in find_hippocampal_volume_files(
+                         for p in FreesurferHippocampalVolumeFile.find(
                              root_dir_path=d, filename_regex=args.filename_regex)]
     volume_frames = []
     for volume_file_path in volume_file_paths:

+ 19 - 0
freesurfer_volume_reader/freesurfer.py

@@ -0,0 +1,19 @@
+import os
+import re
+import typing
+
+
+# pylint: disable=too-few-public-methods
+class FreesurferHippocampalVolumeFile:
+
+    # https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfields
+    FILENAME_PATTERN = r'^(?P<h>[lr])h\.hippoSfVolumes' \
+                       r'(?P<T1>-T1)?(-(?P<analysis_id>.+?))?\.v10.txt$'
+    FILENAME_REGEX = re.compile(FILENAME_PATTERN)
+
+    @staticmethod
+    def find(root_dir_path: str,
+             filename_regex: typing.Pattern = FILENAME_REGEX) -> typing.Iterator[str]:
+        for dirpath, _, filenames in os.walk(root_dir_path):
+            for filename in filter(filename_regex.search, filenames):
+                yield os.path.join(dirpath, filename)

+ 40 - 0
tests/freesurfer_test.py

@@ -0,0 +1,40 @@
+import os
+import re
+
+import pytest
+
+from freesurfer_volume_reader.freesurfer import FreesurferHippocampalVolumeFile
+
+SUBJECTS_DIR = os.path.join(os.path.dirname(__file__), 'subjects')
+
+
+@pytest.mark.parametrize(('root_dir_path', 'expected_file_paths'), [
+    (SUBJECTS_DIR,
+     {os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
+      os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
+      os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
+    (os.path.join(SUBJECTS_DIR, 'bert'),
+     {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
+      os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
+    (os.path.join(SUBJECTS_DIR, 'bert', 'mri'),
+     {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
+      os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
+])
+def test_find_hippocampal_volume_files(root_dir_path, expected_file_paths):
+    assert expected_file_paths == set(
+        FreesurferHippocampalVolumeFile.find(root_dir_path=root_dir_path))
+
+
+@pytest.mark.parametrize(('root_dir_path', 'filename_pattern', 'expected_file_paths'), [
+    (SUBJECTS_DIR,
+     r'hippoSfVolumes-T1\.v10',
+     {os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
+      os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
+    (os.path.join(SUBJECTS_DIR, 'bert'),
+     r'hippoSfVolumes-T1-T2',
+     {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt')}),
+])
+def test_find_hippocampal_volume_files_pattern(root_dir_path, filename_pattern,
+                                               expected_file_paths):
+    assert expected_file_paths == set(FreesurferHippocampalVolumeFile.find(
+        root_dir_path=root_dir_path, filename_regex=re.compile(filename_pattern)))

+ 8 - 30
tests/hippocampus_test.py

@@ -1,6 +1,5 @@
 import io
 import os
-import re
 import typing
 import unittest.mock
 
@@ -13,36 +12,14 @@ import freesurfer_volume_reader
 SUBJECTS_DIR = os.path.join(os.path.dirname(__file__), 'subjects')
 
 
-@pytest.mark.parametrize(('root_dir_path', 'expected_file_paths'), [
-    (SUBJECTS_DIR,
-     {os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
-      os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
-      os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
-    (os.path.join(SUBJECTS_DIR, 'bert'),
-     {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
-      os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
-    (os.path.join(SUBJECTS_DIR, 'bert', 'mri'),
-     {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
-      os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
+@pytest.mark.parametrize(('source_pattern', 'expected_pattern'), [
+    (r'^(?P<h>[lr])h\.hippoSfVolumes', r'^([lr])h\.hippoSfVolumes'),
+    (r'(?P<a>a(?P<b>b))', r'(a(b))'),
 ])
-def test_find_hippocampal_volume_files(root_dir_path, expected_file_paths):
-    assert expected_file_paths == set(
-        freesurfer_volume_reader.find_hippocampal_volume_files(root_dir_path=root_dir_path))
-
-
-@pytest.mark.parametrize(('root_dir_path', 'filename_pattern', 'expected_file_paths'), [
-    (SUBJECTS_DIR,
-     r'hippoSfVolumes-T1\.v10',
-     {os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
-      os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
-    (os.path.join(SUBJECTS_DIR, 'bert'),
-     r'hippoSfVolumes-T1-T2',
-     {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt')}),
-])
-def test_find_hippocampal_volume_files_pattern(root_dir_path, filename_pattern,
-                                               expected_file_paths):
-    assert expected_file_paths == set(freesurfer_volume_reader.find_hippocampal_volume_files(
-        root_dir_path=root_dir_path, filename_regex=re.compile(filename_pattern)))
+def test_remove_group_names_from_regex(source_pattern, expected_pattern):
+    assert expected_pattern == freesurfer_volume_reader.remove_group_names_from_regex(
+        regex_pattern=source_pattern,
+    )
 
 
 @pytest.mark.parametrize(('volume_file_path', 'expected_volumes'), [
@@ -147,6 +124,7 @@ def assert_volume_frames_equal(left: pandas.DataFrame, right: pandas.DataFrame):
         check_like=True,
     )
 
+
 def assert_main_volume_frame_equals(capsys, argv: list, expected_frame: pandas.DataFrame,
                                     subjects_dir: typing.Optional[str] = None):
     if subjects_dir: