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