__init__.py 1.5 KB

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