Browse Source

added ashs.IntracranialVolumeFile.read_volume_mm3()

Fabian Peter Hammerle 4 years ago
parent
commit
61180f2674

+ 6 - 0
freesurfer_volume_reader/ashs.py

@@ -34,6 +34,12 @@ class IntracranialVolumeFile(freesurfer_volume_reader.VolumeFile):
     def absolute_path(self):
         return self._absolute_path
 
+    def read_volume_mm3(self) -> float:
+        with open(self.absolute_path, 'r') as volume_file:
+            subject, icv = volume_file.read().rstrip().split(' ')
+            assert subject == self.subject, (subject, self.subject)
+            return float(icv)
+
 
 class HippocampalSubfieldsVolumeFile(freesurfer_volume_reader.SubfieldVolumeFile):
 

+ 21 - 0
tests/ashs_test.py

@@ -32,6 +32,27 @@ def test_intracranial_volume_file_init_invalid_filename(volume_file_path):
         IntracranialVolumeFile(path=volume_file_path)
 
 
+@pytest.mark.parametrize(('volume_file_path', 'expected_volume'), [
+    (os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_icv.txt'), 1234560),
+    (os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_icv.txt'), 1.23456e06),
+    (os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_icv.txt'), 1.23456e+06),
+    (os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_icv.txt'), float('1.23456e+06')),
+    (os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_icv.txt'), 1543200),
+])
+def test_intracranial_volume_file_read_volume_mm3(volume_file_path, expected_volume):
+    volume_file = IntracranialVolumeFile(path=volume_file_path)
+    assert expected_volume == pytest.approx(volume_file.read_volume_mm3())
+
+
+@pytest.mark.parametrize('volume_file_path', [
+    os.path.join(SUBJECTS_DIR, 'noone', 'final', 'noone_icv.txt'),
+])
+def test_intracranial_volume_file_read_volume_mm3_not_found(volume_file_path):
+    volume_file = IntracranialVolumeFile(path=volume_file_path)
+    with pytest.raises(FileNotFoundError):
+        volume_file.read_volume_mm3()
+
+
 @pytest.mark.parametrize(('volume_file_path', 'expected_attrs'), [
     ('ashs/final/bert_left_heur_volumes.txt',
      {'subject': 'bert', 'hemisphere': 'left', 'correction': None}),

+ 1 - 0
tests/subjects/alice/final/alice_icv.txt

@@ -0,0 +1 @@
+alice 1.5432e+06

+ 1 - 0
tests/subjects/bert/final/bert_icv.txt

@@ -0,0 +1 @@
+bert 1.23456e+06