Browse Source

added ashs.HippocampalSubfieldsVolumeFile.read_volumes_mm3()

Fabian Peter Hammerle 5 years ago
parent
commit
0ca4226d54

+ 5 - 0
.pylintrc

@@ -1,3 +1,8 @@
 [MESSAGES CONTROL]
 
 disable=missing-docstring
+
+
+[SIMILARITIES]
+
+min-similarity-lines=8

+ 6 - 1
freesurfer_volume_reader/__init__.py

@@ -11,11 +11,16 @@ https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfields
 """
 
 import abc
+import typing
+
 
-# pylint: disable=too-few-public-methods
 class VolumeFile(metaclass=abc.ABCMeta):
 
     @property
     @abc.abstractmethod
     def absolute_path(self):
         raise NotImplementedError()
+
+    @abc.abstractmethod
+    def read_volumes_mm3(self) -> typing.Dict[str, float]:
+        raise NotImplementedError()

+ 16 - 1
freesurfer_volume_reader/ashs.py

@@ -7,14 +7,15 @@ https://sites.google.com/site/hipposubfields/home
 >>>
 >>> volume_file = HippocampalSubfieldsVolumeFile('ashs/final/bert_right_corr_nogray_volumes.txt')
 >>> print(volume_file.subject, volume_file.hemisphere, volume_file.correction)
+>>> print(volume_file.read_volumes_mm3())
 """
 
 import os
 import re
+import typing
 
 import freesurfer_volume_reader
 
-# pylint: disable=too-few-public-methods
 class HippocampalSubfieldsVolumeFile(freesurfer_volume_reader.VolumeFile):
 
     # https://sites.google.com/site/hipposubfields/tutorial#TOC-Viewing-ASHS-Segmentation-Results
@@ -34,3 +35,17 @@ class HippocampalSubfieldsVolumeFile(freesurfer_volume_reader.VolumeFile):
     @property
     def absolute_path(self):
         return self._absolute_path
+
+    def read_volumes_mm3(self) -> typing.Dict[str, float]:
+        subfield_volumes = {}
+        with open(self.absolute_path, 'r') as volume_file:
+            for line in volume_file.read().rstrip().split('\n'):
+                # > echo $ASHS_SUBJID $side $SUB $NBODY $VSUB >> $FNBODYVOL
+                # https://github.com/pyushkevich/ashs/blob/515ff7c2f50928adabc4e64bded9a7e76fc750b1/bin/ashs_extractstats_qsub.sh#L94
+                subject, hemisphere, subfield_name, slices_number_str, volume_mm3_str \
+                    = line.split(' ')
+                assert self.subject == subject
+                assert self.hemisphere == hemisphere
+                assert int(slices_number_str) >= 0
+                subfield_volumes[subfield_name] = float(volume_mm3_str)
+        return subfield_volumes

+ 24 - 0
tests/ashs_test.py

@@ -4,6 +4,8 @@ import pytest
 
 from freesurfer_volume_reader.ashs import HippocampalSubfieldsVolumeFile
 
+from conftest import SUBJECTS_DIR
+
 
 @pytest.mark.parametrize(('volume_file_path', 'expected_attrs'), [
     ('ashs/final/bert_left_heur_volumes.txt',
@@ -43,3 +45,25 @@ def test_hippocampal_subfields_volume_file_init(volume_file_path, expected_attrs
 def test_hippocampal_subfields_volume_file_init_invalid(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', 'final', 'bert_left_corr_nogray_volumes.txt'),
+     {'CA1': 678.901,
+      'CA2+3': 123.456,
+      'DG': 901.234,
+      'ERC': 678.901,
+      'PHC': 2345.876,
+      'PRC': 2345.678,
+      'SUB': 457.789}),
+])
+def test_hippocampal_subfields_volume_file_read_volumes_mm3(volume_file_path, expected_volumes):
+    volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
+    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, 'nobert', 'final', 'bert_left_corr_nogray_volumes.txt'))
+    with pytest.raises(FileNotFoundError):
+        volume_file.read_volumes_mm3()

+ 7 - 0
tests/subjects/bert/final/bert_left_corr_nogray_volumes.txt

@@ -0,0 +1,7 @@
+bert left CA1 26 678.901
+bert left CA2+3 26 123.456
+bert left DG 25 901.234
+bert left ERC 18 678.901
+bert left PHC 16 2345.876
+bert left PRC 26 2345.678
+bert left SUB 26 457.789