ashs.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """
  2. Read hippocampal subfield volumes computed by ASHS
  3. https://sites.google.com/site/hipposubfields/home
  4. >>> from freesurfer_volume_reader.ashs import HippocampalSubfieldsVolumeFile
  5. >>>
  6. >>> for volume_file in HippocampalSubfieldsVolumeFile('/my/ashs/subjects'):
  7. >>> print(volume_file.subject, volume_file.hemisphere, volume_file.correction)
  8. >>> print(volume_file.read_volumes_mm3())
  9. >>> print(volume_file.read_volumes_dataframe())
  10. >>> from freesurfer_volume_reader.ashs import IntracranialVolumeFile
  11. >>>
  12. >>> for volume_file in IntracranialVolumeFile('/my/ashs/subjects'):
  13. >>> print(volume_file.subject)
  14. >>> print(volume_file.read_volume_mm3())
  15. >>> print(volume_file.read_volume_series())
  16. """
  17. import pathlib
  18. import re
  19. import typing
  20. import pandas
  21. import freesurfer_volume_reader
  22. class IntracranialVolumeFile(freesurfer_volume_reader.VolumeFile):
  23. FILENAME_REGEX = re.compile(r"^(?P<s>\w+)_icv.txt$")
  24. def __init__(self, path: str):
  25. self._absolute_path = pathlib.Path(path).absolute()
  26. filename_match = self.FILENAME_REGEX.match(self._absolute_path.name)
  27. assert filename_match, self._absolute_path
  28. self.subject = filename_match.groupdict()["s"]
  29. super().__init__(path=path)
  30. @property
  31. def absolute_path(self):
  32. return str(self._absolute_path)
  33. def read_volume_mm3(self) -> float:
  34. subject, icv = (
  35. self._absolute_path.read_text(encoding="ascii").rstrip().split(" ")
  36. )
  37. assert subject == self.subject, (subject, self.subject)
  38. return float(icv)
  39. def read_volume_series(self) -> pandas.Series:
  40. return pandas.Series(
  41. data=[self.read_volume_mm3()],
  42. name="intercranial_volume_mm^3",
  43. index=pandas.Index(data=[self.subject], name="subject"),
  44. )
  45. class HippocampalSubfieldsVolumeFile(freesurfer_volume_reader.SubfieldVolumeFile):
  46. # https://sites.google.com/site/hipposubfields/tutorial#TOC-Viewing-ASHS-Segmentation-Results
  47. FILENAME_PATTERN = (
  48. r"^(?P<s>\w+)_(?P<h>left|right)"
  49. r"_(heur|corr_(?P<c>nogray|usegray))_volumes.txt$"
  50. )
  51. FILENAME_REGEX = re.compile(FILENAME_PATTERN)
  52. def __init__(self, path: str):
  53. self._absolute_path = pathlib.Path(path).absolute()
  54. filename_match = self.FILENAME_REGEX.match(self._absolute_path.name)
  55. assert filename_match, self._absolute_path
  56. filename_groups = filename_match.groupdict()
  57. self.subject = filename_groups["s"]
  58. self.hemisphere = filename_groups["h"]
  59. self.correction = filename_groups["c"]
  60. super().__init__(path=path)
  61. @property
  62. def absolute_path(self):
  63. return str(self._absolute_path)
  64. def read_volumes_mm3(self) -> typing.Dict[str, float]:
  65. subfield_volumes = {}
  66. for line in (
  67. self._absolute_path.read_text(encoding="ascii").rstrip().split("\n")
  68. ):
  69. # > echo $ASHS_SUBJID $side $SUB $NBODY $VSUB >> $FNBODYVOL
  70. # https://github.com/pyushkevich/ashs/blob/515ff7c2f50928adabc4e64bded9a7e76fc750b1/bin/ashs_extractstats_qsub.sh#L94
  71. (
  72. subject,
  73. hemisphere,
  74. subfield_name,
  75. slices_number_str,
  76. volume_mm3_str,
  77. ) = line.split(" ")
  78. assert self.subject == subject
  79. assert self.hemisphere == hemisphere
  80. assert int(slices_number_str) >= 0
  81. subfield_volumes[subfield_name] = float(volume_mm3_str)
  82. return subfield_volumes
  83. def read_volumes_dataframe(self) -> pandas.DataFrame:
  84. volumes_frame = self._read_volume_series().reset_index()
  85. volumes_frame["subject"] = self.subject
  86. volumes_frame["hemisphere"] = self.hemisphere
  87. volumes_frame["correction"] = self.correction
  88. return volumes_frame