123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- # pylint: disable=missing-module-docstring
- import os
- import re
- import pandas
- import pytest
- from freesurfer_volume_reader.freesurfer import HippocampalSubfieldsVolumeFile
- # pylint: disable=wrong-import-order; false positive
- from conftest import SUBJECTS_DIR, assert_volume_frames_equal
- @pytest.mark.parametrize(
- ("volume_file_path", "expected_attrs"),
- [
- (
- "bert/mri/lh.hippoSfVolumes-T1.v10.txt",
- {
- "subject": "bert",
- "hemisphere": "left",
- "t1_input": True,
- "analysis_id": None,
- },
- ),
- (
- "bert/mri/lh.hippoSfVolumes-T1-T2.v10.txt",
- {
- "subject": "bert",
- "hemisphere": "left",
- "t1_input": True,
- "analysis_id": "T2",
- },
- ),
- (
- "bert/mri/lh.hippoSfVolumes-T2.v10.txt",
- {
- "subject": "bert",
- "hemisphere": "left",
- "t1_input": False,
- "analysis_id": "T2",
- },
- ),
- (
- "bert/mri/lh.hippoSfVolumes-T1-T2-high-res.v10.txt",
- {
- "subject": "bert",
- "hemisphere": "left",
- "t1_input": True,
- "analysis_id": "T2-high-res",
- },
- ),
- (
- "bert/mri/lh.hippoSfVolumes-T2-high-res.v10.txt",
- {
- "subject": "bert",
- "hemisphere": "left",
- "t1_input": False,
- "analysis_id": "T2-high-res",
- },
- ),
- (
- "bert/mri/lh.hippoSfVolumes-PD.v10.txt",
- {
- "subject": "bert",
- "hemisphere": "left",
- "t1_input": False,
- "analysis_id": "PD",
- },
- ),
- (
- "bert/mri/rh.hippoSfVolumes-T1.v10.txt",
- {
- "subject": "bert",
- "hemisphere": "right",
- "t1_input": True,
- "analysis_id": None,
- },
- ),
- (
- "bert/mri/rh.hippoSfVolumes-T1-T2.v10.txt",
- {
- "subject": "bert",
- "hemisphere": "right",
- "t1_input": True,
- "analysis_id": "T2",
- },
- ),
- (
- "freesurfer/subjects/bert/mri/lh.hippoSfVolumes-T1.v10.txt",
- {
- "subject": "bert",
- "hemisphere": "left",
- "t1_input": True,
- "analysis_id": None,
- },
- ),
- (
- "../../bert/mri/lh.hippoSfVolumes-T1.v10.txt",
- {
- "subject": "bert",
- "hemisphere": "left",
- "t1_input": True,
- "analysis_id": None,
- },
- ),
- ],
- )
- 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/mri/lh.hippoSfLabels-T1.v10.mgz",
- "bert/mri/lh.hippoSfVolumes-T1.v9.txt",
- "bert/mri/lh.hippoSfVolumes.v10.txt",
- "bert/mri/mh.hippoSfVolumes-T1.v10.txt",
- "bert_left_corr_nogray_volumes.txt",
- ],
- )
- def test_hippocampal_subfields_volume_file_init_invalid_path(volume_file_path):
- with pytest.raises(Exception):
- HippocampalSubfieldsVolumeFile(path=volume_file_path)
- @pytest.mark.parametrize(
- ("volume_file_path", "expected_volumes"),
- [
- (
- os.path.join(SUBJECTS_DIR, "bert/mri/lh.hippoSfVolumes-T1.v10.txt"),
- {
- "Hippocampal_tail": 123.456789,
- "subiculum": 234.567891,
- "CA1": 34.567891,
- "hippocampal-fissure": 345.678912,
- "presubiculum": 456.789123,
- "parasubiculum": 45.678912,
- "molecular_layer_HP": 56.789123,
- "GC-ML-DG": 567.891234,
- "CA3": 678.912345,
- "CA4": 789.123456,
- "fimbria": 89.123456,
- "HATA": 91.234567,
- "Whole_hippocampus": 1234.567899,
- },
- )
- ],
- )
- def test_hippocampal_subfields_volume_file_read_volumes_mm3(
- volume_file_path, expected_volumes
- ):
- volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
- assert volume_file.t1_input
- assert expected_volumes == volume_file.read_volumes_mm3()
- def test_hippocampal_subfields_volume_file_read_volumes_mm3_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_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(
- 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"
- ),
- 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_hippocampal_subfields_volume_file_find(root_dir_path, expected_file_paths):
- volume_files = list(
- HippocampalSubfieldsVolumeFile.find(root_dir_path=root_dir_path)
- )
- assert all(
- "hippoSfVolumes" in os.path.basename(f.absolute_path) for f in volume_files
- )
- assert expected_file_paths == set(f.absolute_path for f in volume_files)
- @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_hippocampal_subfields_volume_file_find_pattern(
- root_dir_path, filename_pattern, expected_file_paths
- ):
- assert expected_file_paths == set(
- f.absolute_path
- for f in HippocampalSubfieldsVolumeFile.find(
- root_dir_path=root_dir_path, filename_regex=re.compile(filename_pattern)
- )
- )
|