__init__.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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) -> typing.Iterator[str]:
  29. if not filename_regex:
  30. filename_regex = cls.FILENAME_REGEX
  31. for dirpath, _, filenames in os.walk(root_dir_path):
  32. for filename in filter(filename_regex.search, filenames):
  33. yield cls(path=os.path.join(dirpath, filename))