__init__.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """
  2. Read hippocampal subfield volumes computed by Freesurfer
  3. https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfields
  4. >>> from freesurfer_volume_reader.freesurfer import HippocampalSubfieldsVolumeFile
  5. >>>
  6. >>> for volume_file in HippocampalSubfieldsVolumeFile.find('/my/freesurfer/subjects'):
  7. >>> print(volume_file.read_volumes_mm3())
  8. >>> print(volume_file.read_volumes_dataframe())
  9. """
  10. import abc
  11. import os
  12. import typing
  13. import pandas
  14. class VolumeFile(metaclass=abc.ABCMeta):
  15. FILENAME_REGEX = NotImplemented
  16. @property
  17. @abc.abstractmethod
  18. def absolute_path(self):
  19. raise NotImplementedError()
  20. @abc.abstractmethod
  21. def read_volumes_mm3(self) -> typing.Dict[str, float]:
  22. raise NotImplementedError()
  23. @abc.abstractmethod
  24. def read_volumes_dataframe(self) -> pandas.DataFrame:
  25. raise NotImplementedError()
  26. @classmethod
  27. def find(cls, root_dir_path: str,
  28. filename_regex: typing.Optional[typing.Pattern] = None,
  29. ) -> typing.Iterator[VolumeFile]:
  30. if not filename_regex:
  31. filename_regex = cls.FILENAME_REGEX
  32. for dirpath, _, filenames in os.walk(root_dir_path):
  33. for filename in filter(filename_regex.search, filenames):
  34. yield cls(path=os.path.join(dirpath, filename))