소스 검색

Merge branch 'fix/invalid-root-path'

https://github.com/fphammerle/freesurfer-volume-reader/pull/3/files
Fabian Peter Hammerle 5 년 전
부모
커밋
97dfff1122
9개의 변경된 파일137개의 추가작업 그리고 24개의 파일을 삭제
  1. 2 1
      Pipfile
  2. 9 1
      Pipfile.lock
  3. 1 0
      README.md
  4. 1 0
      examples/barplot.py
  5. 7 6
      freesurfer_volume_reader/__init__.py
  6. 1 0
      setup.py
  7. 61 16
      tests/hippocampus_test.py
  8. 15 0
      tests/subjects/alice/hippocampal-volumes.csv
  9. 40 0
      tests/subjects/all-hippocampal-volumes.csv

+ 2 - 1
Pipfile

@@ -7,8 +7,9 @@ name = "pypi"
 freesurfer-volume-reader = {editable = true, path = "."}
 
 [dev-packages]
-pytest = "*"
 pylint = ">=2.3.0"
+pytest = "*"
+pytest-timeout = "*"
 
 [requires]
 python_version = "3"

+ 9 - 1
Pipfile.lock

@@ -1,7 +1,7 @@
 {
     "_meta": {
         "hash": {
-            "sha256": "52976adb8c969665fe068d4030d9e1c2fc535bbe86cbfb5faaa4879164fc6f3c"
+            "sha256": "21ecadcfdbf53c5724b1c71278db767fe370cb29b6ad12846685fb4db30ed8de"
         },
         "pipfile-spec": 6,
         "requires": {
@@ -203,6 +203,14 @@
             "index": "pypi",
             "version": "==4.4.1"
         },
+        "pytest-timeout": {
+            "hashes": [
+                "sha256:4a30ba76837a32c7b7cd5c84ee9933fde4b9022b0cd20ea7d4a577c2a1649fb1",
+                "sha256:d49f618c6448c14168773b6cdda022764c63ea80d42274e3156787e8088d04c6"
+            ],
+            "index": "pypi",
+            "version": "==1.3.3"
+        },
         "six": {
             "hashes": [
                 "sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c",

+ 1 - 0
README.md

@@ -25,6 +25,7 @@ or
 
 ```sh
 freesurfer-volume-reader /my/freesurfer/subjects
+freesurfer-volume-reader /my/freesurfer/subjects /other/freesurfer/subjects
 ```
 
 or

+ 1 - 0
examples/barplot.py

@@ -7,6 +7,7 @@ import seaborn
 from freesurfer_volume_reader import find_hippocampal_volume_files, \
                                      read_hippocampal_volume_file_dataframe
 
+
 def abbreviate_analysis_id(analysis_id):
     return analysis_id.split('_')[0] if analysis_id else None
 

+ 7 - 6
freesurfer_volume_reader/__init__.py

@@ -66,20 +66,21 @@ def read_hippocampal_volume_file_dataframe(volume_file_path: str) -> pandas.Data
 
 def main():
     argparser = argparse.ArgumentParser(description=__doc__)
-    argparser.add_argument('--filename-regex', dest='filename_pattern',
+    argparser.add_argument('--filename-regex', type=re.compile,
                            default=DEFAULT_HIPPOCAMPAL_VOLUME_FIND_FILENAME_PATTERN,
                            help='default: %(default)s')
     argparser.add_argument('--output-format', choices=['csv'], default='csv',
                            help='default: %(default)s')
     subjects_dir_path = os.environ.get('SUBJECTS_DIR', None)
-    argparser.add_argument('root_dir_path',
-                           nargs='?' if subjects_dir_path  else 1,
+    argparser.add_argument('root_dir_paths',
+                           metavar='ROOT_DIR',
+                           nargs='*' if subjects_dir_path else '+',
                            default=[subjects_dir_path],
                            help='default: $SUBJECTS_DIR ({})'.format(subjects_dir_path))
     args = argparser.parse_args()
-    volume_file_paths = find_hippocampal_volume_files(
-        root_dir_path=args.root_dir_path[0],
-        filename_regex=re.compile(args.filename_pattern))
+    volume_file_paths = [p for d in args.root_dir_paths
+                         for p in find_hippocampal_volume_files(
+                             root_dir_path=d, filename_regex=args.filename_regex)]
     volume_frames = []
     for volume_file_path in volume_file_paths:
         volume_frame = read_hippocampal_volume_file_dataframe(volume_file_path)

+ 1 - 0
setup.py

@@ -44,5 +44,6 @@ setuptools.setup(
     tests_require=[
         'pylint>=2.3.0',
         'pytest',
+        'pytest-timeout',
     ],
 )

+ 61 - 16
tests/hippocampus_test.py

@@ -1,6 +1,7 @@
 import io
 import os
 import re
+import typing
 import unittest.mock
 
 import pandas
@@ -51,7 +52,7 @@ def test_find_hippocampal_volume_files_pattern(root_dir_path, filename_pattern,
       'CA1': 34.567891,
       'hippocampal-fissure': 345.678912,
       'presubiculum': 456.789123,
-      'parasubiculum':  45.678912,
+      'parasubiculum': 45.678912,
       'molecular_layer_HP': 56.789123,
       'GC-ML-DG': 567.891234,
       'CA3': 678.912345,
@@ -146,31 +147,75 @@ def assert_volume_frames_equal(left: pandas.DataFrame, right: pandas.DataFrame):
         check_like=True,
     )
 
-
-@pytest.mark.parametrize(('root_dir_path', 'expected_csv_path'), [
-    (os.path.join(SUBJECTS_DIR, 'bert'),
-     os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
-])
-def test_main_root_dir_param(capsys, root_dir_path, expected_csv_path):
-    with unittest.mock.patch('sys.argv', ['', root_dir_path]):
+def assert_main_volume_frame_equals(capsys, argv: list, expected_frame: pandas.DataFrame,
+                                    subjects_dir: typing.Optional[str] = None):
+    if subjects_dir:
+        os.environ['SUBJECTS_DIR'] = subjects_dir
+    elif 'SUBJECTS_DIR' in os.environ:
+        del os.environ['SUBJECTS_DIR']
+    with unittest.mock.patch('sys.argv', [''] + argv):
         freesurfer_volume_reader.main()
     out, _ = capsys.readouterr()
     assert_volume_frames_equal(
-        left=pandas.read_csv(expected_csv_path),
+        left=expected_frame,
         right=pandas.read_csv(io.StringIO(out)).drop(columns=['source_path']),
     )
 
 
+@pytest.mark.parametrize(('root_dir_paths', 'expected_csv_path'), [
+    ([os.path.join(SUBJECTS_DIR, 'alice')],
+     os.path.join(SUBJECTS_DIR, 'alice', 'hippocampal-volumes.csv')),
+    ([os.path.join(SUBJECTS_DIR, 'bert')],
+     os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
+    ([os.path.join(SUBJECTS_DIR, 'alice'),
+      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):
+    assert_main_volume_frame_equals(
+        argv=root_dir_paths,
+        expected_frame=pandas.read_csv(expected_csv_path),
+        capsys=capsys,
+    )
+
+
 @pytest.mark.parametrize(('root_dir_path', '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):
-    os.environ['SUBJECTS_DIR'] = root_dir_path
-    with unittest.mock.patch('sys.argv', ['']):
-        freesurfer_volume_reader.main()
-    out, _ = capsys.readouterr()
-    assert_volume_frames_equal(
-        left=pandas.read_csv(expected_csv_path),
-        right=pandas.read_csv(io.StringIO(out)).drop(columns=['source_path']),
+    assert_main_volume_frame_equals(
+        argv=[],
+        subjects_dir=root_dir_path,
+        expected_frame=pandas.read_csv(expected_csv_path),
+        capsys=capsys,
+    )
+
+
+@pytest.mark.timeout(8)
+@pytest.mark.parametrize(('root_dir_path', 'subjects_dir', 'expected_csv_path'), [
+    (os.path.join(SUBJECTS_DIR, 'bert'),
+     os.path.join(SUBJECTS_DIR, 'alice'),
+     os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
+    (os.path.join(SUBJECTS_DIR, 'bert'),
+     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):
+    assert_main_volume_frame_equals(
+        argv=[root_dir_path],
+        subjects_dir=subjects_dir,
+        expected_frame=pandas.read_csv(expected_csv_path),
+        capsys=capsys,
+    )
+
+
+def test_main_root_dir_filename_regex(capsys):
+    expected_volume_frame = pandas.read_csv(
+        os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv'))
+    assert_main_volume_frame_equals(
+        argv=['--filename-regex', r'^.*-T1-T2\.v10\.txt$',
+              os.path.join(SUBJECTS_DIR, 'bert')],
+        expected_frame=expected_volume_frame[expected_volume_frame['analysis_id'] == 'T2'].copy(),
+        capsys=capsys,
     )

+ 15 - 0
tests/subjects/alice/hippocampal-volumes.csv

@@ -0,0 +1,15 @@
+subfield,volume_mm^3,subject,hemisphere,T1_input,analysis_id
+Hippocampal_tail,173.456789,alice,left,True,
+subiculum,734.567891,alice,left,True,
+CA1,34.567891,alice,left,True,
+hippocampal-fissure,345.678917,alice,left,True,
+presubiculum,456.789173,alice,left,True,
+parasubiculum,45.678917,alice,left,True,
+molecular_layer_HP,56.789173,alice,left,True,
+GC-ML-DG,567.891734,alice,left,True,
+CA3,678.917345,alice,left,True,
+CA4,789.173456,alice,left,True,
+fimbria,89.173456,alice,left,True,
+HATA,91.734567,alice,left,True,
+Whole_hippocampus,1734.567899,alice,left,True,
+

+ 40 - 0
tests/subjects/all-hippocampal-volumes.csv

@@ -0,0 +1,40 @@
+subfield,volume_mm^3,subject,hemisphere,T1_input,analysis_id
+Hippocampal_tail,173.456789,alice,left,True,
+subiculum,734.567891,alice,left,True,
+CA1,34.567891,alice,left,True,
+hippocampal-fissure,345.678917,alice,left,True,
+presubiculum,456.789173,alice,left,True,
+parasubiculum,45.678917,alice,left,True,
+molecular_layer_HP,56.789173,alice,left,True,
+GC-ML-DG,567.891734,alice,left,True,
+CA3,678.917345,alice,left,True,
+CA4,789.173456,alice,left,True,
+fimbria,89.173456,alice,left,True,
+HATA,91.734567,alice,left,True,
+Whole_hippocampus,1734.567899,alice,left,True,
+Hippocampal_tail,123.456789,bert,left,True,
+subiculum,234.567891,bert,left,True,
+CA1,34.567891,bert,left,True,
+hippocampal-fissure,345.678912,bert,left,True,
+presubiculum,456.789123,bert,left,True,
+parasubiculum,45.678912,bert,left,True,
+molecular_layer_HP,56.789123,bert,left,True,
+GC-ML-DG,567.891234,bert,left,True,
+CA3,678.912345,bert,left,True,
+CA4,789.123456,bert,left,True,
+fimbria,89.123456,bert,left,True,
+HATA,91.234567,bert,left,True,
+Whole_hippocampus,1234.567899,bert,left,True,
+Hippocampal_tail,124.456789,bert,left,True,T2
+subiculum,244.567891,bert,left,True,T2
+CA1,44.567891,bert,left,True,T2
+hippocampal-fissure,445.678912,bert,left,True,T2
+presubiculum,456.789124,bert,left,True,T2
+parasubiculum,45.678912,bert,left,True,T2
+molecular_layer_HP,56.789124,bert,left,True,T2
+GC-ML-DG,567.891244,bert,left,True,T2
+CA3,678.912445,bert,left,True,T2
+CA4,789.124456,bert,left,True,T2
+fimbria,89.124456,bert,left,True,T2
+HATA,91.244567,bert,left,True,T2
+Whole_hippocampus,1244.567899,bert,left,True,T2