freesurfer.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """
  2. Read hippocampal subfield volumes computed by Freesurfer
  3. https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfields
  4. https://github.com/freesurfer/freesurfer/tree/release_6_0_0/HippoSF
  5. >>> from freesurfer_volume_reader.freesurfer import HippocampalSubfieldsVolumeFile
  6. >>>
  7. >>> for volume_file in HippocampalSubfieldsVolumeFile.find('/my/freesurfer/subjects'):
  8. >>> print(volume_file.read_volumes_mm3())
  9. >>> print(volume_file.read_volumes_dataframe())
  10. """
  11. import os
  12. import re
  13. import typing
  14. import pandas
  15. import freesurfer_volume_reader
  16. class HippocampalSubfieldsVolumeFile(freesurfer_volume_reader.VolumeFile):
  17. # https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfields
  18. FILENAME_PATTERN = r'^(?P<h>[lr])h\.hippoSfVolumes' \
  19. r'(?P<T1>-T1)?(-(?P<analysis_id>.+?))?\.v10.txt$'
  20. FILENAME_REGEX = re.compile(FILENAME_PATTERN)
  21. FILENAME_HEMISPHERE_PREFIX_MAP = {'l': 'left', 'r': 'right'}
  22. def __init__(self, path: str):
  23. self._absolute_path = os.path.abspath(path)
  24. subject_dir_path = os.path.dirname(os.path.dirname(self._absolute_path))
  25. self.subject = os.path.basename(subject_dir_path)
  26. filename_match = self.FILENAME_REGEX.match(os.path.basename(path))
  27. assert filename_match, self._absolute_path
  28. filename_groups = filename_match.groupdict()
  29. assert filename_groups['T1'] or filename_groups['analysis_id'], self._absolute_path
  30. self.hemisphere = self.FILENAME_HEMISPHERE_PREFIX_MAP[filename_groups['h']]
  31. self.t1_input = filename_groups['T1'] is not None
  32. self.analysis_id = filename_groups['analysis_id']
  33. @property
  34. def absolute_path(self):
  35. return self._absolute_path
  36. def read_volumes_mm3(self) -> typing.Dict[str, float]:
  37. subfield_volumes = {}
  38. with open(self.absolute_path, 'r') as volume_file:
  39. for line in volume_file.read().rstrip().split('\n'):
  40. # https://github.com/freesurfer/freesurfer/blob/release_6_0_0/HippoSF/src/segmentSubjectT1T2_autoEstimateAlveusML.m#L8
  41. # https://github.com/freesurfer/freesurfer/blob/release_6_0_0/HippoSF/src/segmentSubjectT1T2_autoEstimateAlveusML.m#L1946
  42. subfield_name, subfield_volume_mm3_str = line.split(' ')
  43. subfield_volumes[subfield_name] = float(subfield_volume_mm3_str)
  44. return subfield_volumes
  45. def read_volumes_dataframe(self) -> pandas.DataFrame:
  46. volumes_frame = pandas.DataFrame([
  47. {'subfield': s, 'volume_mm^3': v}
  48. for s, v in self.read_volumes_mm3().items()
  49. ])
  50. volumes_frame['subject'] = self.subject
  51. volumes_frame['hemisphere'] = self.hemisphere
  52. # volumes_frame['hemisphere'] = volumes_frame['hemisphere'].astype('category')
  53. volumes_frame['T1_input'] = self.t1_input
  54. volumes_frame['analysis_id'] = self.analysis_id
  55. return volumes_frame