Browse Source

move/rename read_hippocampal_volume_file_dataframe -> freesurfer.HippocampalSubfieldsVolumeFile.read_volumes_dataframe()

Fabian Peter Hammerle 5 years ago
parent
commit
8bfbe75024

+ 1 - 2
README.md

@@ -32,11 +32,10 @@ or
 
 ```python
 from freesurfer_volume_reader.freesurfer import HippocampalSubfieldsVolumeFile
-from freesurfer_volume_reader import read_hippocampal_volume_file_dataframe
 
 for volume_file in HippocampalSubfieldsVolumeFile.find('/my/freesurfer/subjects'):
     print(volume_file.read_volumes_mm3())
-    print(read_hippocampal_volume_file_dataframe(volume_file))
+    print(volume_file.read_volumes_dataframe())
 ```
 
 ## Tests

+ 21 - 0
conftest.py

@@ -0,0 +1,21 @@
+import pandas
+import pytest
+
+
+def _assert_volume_frames_equal(left: pandas.DataFrame, right: pandas.DataFrame):
+    sort_by = ['volume_mm^3', 'analysis_id']
+    left.sort_values(sort_by, inplace=True)
+    right.sort_values(sort_by, inplace=True)
+    left.reset_index(inplace=True, drop=True)
+    right.reset_index(inplace=True, drop=True)
+    pandas.util.testing.assert_frame_equal(
+        left=left,
+        right=right,
+        # ignore the order of index & columns
+        check_like=True,
+    )
+
+
+@pytest.fixture(scope='module')
+def assert_volume_frames_equal():
+    return _assert_volume_frames_equal

+ 2 - 3
examples/barplot.py

@@ -5,7 +5,6 @@ import pandas
 import seaborn
 
 from freesurfer_volume_reader.freesurfer import HippocampalSubfieldsVolumeFile
-from freesurfer_volume_reader import read_hippocampal_volume_file_dataframe
 
 
 def abbreviate_analysis_id(analysis_id):
@@ -17,8 +16,8 @@ def main():
     argparser.add_argument('--subject', required=True)
     argparser.add_argument('--subjects-dir', dest='subjects_dir_path', required=True)
     args = argparser.parse_args()
-    volume_frame = pandas.concat(read_hippocampal_volume_file_dataframe(f)
-                                 for f in HippocampalSubfieldsVolumeFile.find(args.subjects_dir_path))
+    volume_frame = pandas.concat(f.read_volumes_dataframe() for f in
+                                 HippocampalSubfieldsVolumeFile.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,

+ 1 - 15
freesurfer_volume_reader/__init__.py

@@ -18,20 +18,6 @@ def remove_group_names_from_regex(regex_pattern: str) -> str:
     return re.sub(r'\?P<.+?>', '', regex_pattern)
 
 
-def read_hippocampal_volume_file_dataframe(volume_file: HippocampalSubfieldsVolumeFile,
-                                           ) -> pandas.DataFrame:
-    volumes_frame = pandas.DataFrame([
-        {'subfield': s, 'volume_mm^3': v}
-        for s, v in volume_file.read_volumes_mm3().items()
-    ])
-    volumes_frame['subject'] = volume_file.subject
-    volumes_frame['hemisphere'] = volume_file.hemisphere
-    # volumes_frame['hemisphere'] = volumes_frame['hemisphere'].astype('category')
-    volumes_frame['T1_input'] = volume_file.t1_input
-    volumes_frame['analysis_id'] = volume_file.analysis_id
-    return volumes_frame
-
-
 def main():
     argparser = argparse.ArgumentParser(description=__doc__)
     argparser.add_argument('--filename-regex', type=re.compile,
@@ -52,7 +38,7 @@ def main():
                         root_dir_path=d, filename_regex=args.filename_regex)]
     volume_frames = []
     for volume_file in volume_files:
-        volume_frame = read_hippocampal_volume_file_dataframe(volume_file)
+        volume_frame = volume_file.read_volumes_dataframe()
         volume_frame['source_path'] = volume_file.absolute_path
         volume_frames.append(volume_frame)
     united_volume_frame = pandas.concat(volume_frames, ignore_index=True)

+ 15 - 0
freesurfer_volume_reader/freesurfer.py

@@ -2,6 +2,9 @@ import os
 import re
 import typing
 
+import pandas
+
+
 class HippocampalSubfieldsVolumeFile:
 
     # https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfields
@@ -37,6 +40,18 @@ class HippocampalSubfieldsVolumeFile:
                 subfield_volumes[subfield_name] = float(subfield_volume_mm3_str)
         return subfield_volumes
 
+    def read_volumes_dataframe(self) -> pandas.DataFrame:
+        volumes_frame = pandas.DataFrame([
+            {'subfield': s, 'volume_mm^3': v}
+            for s, v in self.read_volumes_mm3().items()
+        ])
+        volumes_frame['subject'] = self.subject
+        volumes_frame['hemisphere'] = self.hemisphere
+        # volumes_frame['hemisphere'] = volumes_frame['hemisphere'].astype('category')
+        volumes_frame['T1_input'] = self.t1_input
+        volumes_frame['analysis_id'] = self.analysis_id
+        return volumes_frame
+
     @classmethod
     def find(cls, root_dir_path: str,
              filename_regex: typing.Pattern = FILENAME_REGEX) -> typing.Iterator[str]:

+ 33 - 0
tests/freesurfer_test.py

@@ -1,6 +1,8 @@
 import os
 import re
+import typing
 
+import pandas
 import pytest
 
 from freesurfer_volume_reader.freesurfer import HippocampalSubfieldsVolumeFile
@@ -76,6 +78,37 @@ def test_hippocampal_subfields_volume_file_read_volumes_mm3_not_found():
         volume_file.read_volumes_mm3()
 
 
+@pytest.mark.parametrize(('volume_file_path', 'expected_dataframe'), [
+    (os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
+     pandas.DataFrame({
+         'subfield': ['Hippocampal_tail', 'subiculum', 'CA1', 'hippocampal-fissure',
+                      'presubiculum', 'parasubiculum', 'molecular_layer_HP', 'GC-ML-DG',
+                      'CA3', 'CA4', 'fimbria', 'HATA', 'Whole_hippocampus'],
+         'volume_mm^3': [173.456789, 734.567891, 34.567891, 345.678917, 456.789173, 45.678917,
+                         56.789173, 567.891734, 678.917345, 789.173456, 89.173456, 91.734567,
+                         1734.567899],
+         'subject': 'alice',
+         'hemisphere': 'left',
+         'T1_input': True,
+         'analysis_id': None,
+     })),
+])
+def test_hippocampal_subfields_volume_file_read_volumes_dataframe(
+        assert_volume_frames_equal: typing.Callable,
+        volume_file_path: str, expected_dataframe: pandas.DataFrame):
+    assert_volume_frames_equal(
+        left=expected_dataframe,
+        right=HippocampalSubfieldsVolumeFile(path=volume_file_path).read_volumes_dataframe(),
+    )
+
+
+def test_hippocampal_subfields_volume_file_read_volumes_dataframe_not_found():
+    volume_file = HippocampalSubfieldsVolumeFile(
+        path=os.path.join(SUBJECTS_DIR, 'non-existing', 'lh.hippoSfVolumes-T1.v10.txt'))
+    with pytest.raises(FileNotFoundError):
+        volume_file.read_volumes_dataframe()
+
+
 @pytest.mark.parametrize(('root_dir_path', 'expected_file_paths'), [
     (SUBJECTS_DIR,
      {os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),

+ 13 - 44
tests/hippocampus_test.py

@@ -23,46 +23,8 @@ def test_remove_group_names_from_regex(source_pattern, expected_pattern):
     )
 
 
-@pytest.mark.parametrize(('volume_file_path', 'expected_dataframe'), [
-    (os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
-     pandas.DataFrame({
-         'subfield': ['Hippocampal_tail', 'subiculum', 'CA1', 'hippocampal-fissure',
-                      'presubiculum', 'parasubiculum', 'molecular_layer_HP', 'GC-ML-DG',
-                      'CA3', 'CA4', 'fimbria', 'HATA', 'Whole_hippocampus'],
-         'volume_mm^3': [173.456789, 734.567891, 34.567891, 345.678917, 456.789173, 45.678917,
-                         56.789173, 567.891734, 678.917345, 789.173456, 89.173456, 91.734567,
-                         1734.567899],
-         'subject': 'alice',
-         'hemisphere': 'left',
-         'T1_input': True,
-         'analysis_id': None,
-     })),
-])
-def test_read_hippocampal_volume_file_dataframe(volume_file_path, expected_dataframe):
-    volume_file = freesurfer_volume_reader.freesurfer.HippocampalSubfieldsVolumeFile(
-        path=volume_file_path)
-    assert_volume_frames_equal(
-        left=expected_dataframe,
-        right=freesurfer_volume_reader.read_hippocampal_volume_file_dataframe(
-            volume_file=volume_file),
-    )
-
-
-def assert_volume_frames_equal(left: pandas.DataFrame, right: pandas.DataFrame):
-    sort_by = ['volume_mm^3', 'analysis_id']
-    left.sort_values(sort_by, inplace=True)
-    right.sort_values(sort_by, inplace=True)
-    left.reset_index(inplace=True, drop=True)
-    right.reset_index(inplace=True, drop=True)
-    pandas.util.testing.assert_frame_equal(
-        left=left,
-        right=right,
-        # ignore the order of index & columns
-        check_like=True,
-    )
-
-
-def assert_main_volume_frame_equals(capsys, argv: list, expected_frame: pandas.DataFrame,
+def assert_main_volume_frame_equals(capsys, assert_volume_frames_equal,
+                                    argv: list, expected_frame: pandas.DataFrame,
                                     subjects_dir: typing.Optional[str] = None):
     if subjects_dir:
         os.environ['SUBJECTS_DIR'] = subjects_dir
@@ -87,11 +49,13 @@ def assert_main_volume_frame_equals(capsys, argv: list, expected_frame: pandas.D
       os.path.join(SUBJECTS_DIR, 'bert')],
      os.path.join(SUBJECTS_DIR, 'all-hippocampal-volumes.csv')),
 ])
-def test_main_root_dir_param(capsys, root_dir_paths: list, expected_csv_path):
+def test_main_root_dir_param(capsys, assert_volume_frames_equal,
+                             root_dir_paths: list, expected_csv_path):
     assert_main_volume_frame_equals(
         argv=root_dir_paths,
         expected_frame=pandas.read_csv(expected_csv_path),
         capsys=capsys,
+        assert_volume_frames_equal=assert_volume_frames_equal,
     )
 
 
@@ -99,12 +63,14 @@ def test_main_root_dir_param(capsys, root_dir_paths: list, expected_csv_path):
     (os.path.join(SUBJECTS_DIR, 'bert'),
      os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
 ])
-def test_main_root_dir_env(capsys, root_dir_path, expected_csv_path):
+def test_main_root_dir_env(capsys, assert_volume_frames_equal,
+                           root_dir_path, expected_csv_path):
     assert_main_volume_frame_equals(
         argv=[],
         subjects_dir=root_dir_path,
         expected_frame=pandas.read_csv(expected_csv_path),
         capsys=capsys,
+        assert_volume_frames_equal=assert_volume_frames_equal,
     )
 
 
@@ -117,16 +83,18 @@ def test_main_root_dir_env(capsys, root_dir_path, expected_csv_path):
      os.path.abspath(os.sep),
      os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
 ])
-def test_main_root_dir_overwrite_env(capsys, root_dir_path, subjects_dir, expected_csv_path):
+def test_main_root_dir_overwrite_env(capsys, assert_volume_frames_equal,
+                                     root_dir_path, subjects_dir, expected_csv_path):
     assert_main_volume_frame_equals(
         argv=[root_dir_path],
         subjects_dir=subjects_dir,
         expected_frame=pandas.read_csv(expected_csv_path),
         capsys=capsys,
+        assert_volume_frames_equal=assert_volume_frames_equal,
     )
 
 
-def test_main_root_dir_filename_regex(capsys):
+def test_main_root_dir_filename_regex(capsys, assert_volume_frames_equal):
     expected_volume_frame = pandas.read_csv(
         os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv'))
     assert_main_volume_frame_equals(
@@ -134,4 +102,5 @@ def test_main_root_dir_filename_regex(capsys):
               os.path.join(SUBJECTS_DIR, 'bert')],
         expected_frame=expected_volume_frame[expected_volume_frame['analysis_id'] == 'T2'].copy(),
         capsys=capsys,
+        assert_volume_frames_equal=assert_volume_frames_equal,
     )