ashs.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 = self._absolute_path.read_text().rstrip().split(" ")
  35. assert subject == self.subject, (subject, self.subject)
  36. return float(icv)
  37. def read_volume_series(self) -> pandas.Series:
  38. return pandas.Series(
  39. data=[self.read_volume_mm3()],
  40. name="intercranial_volume_mm^3",
  41. index=pandas.Index(data=[self.subject], name="subject"),
  42. )
  43. class HippocampalSubfieldsVolumeFile(freesurfer_volume_reader.SubfieldVolumeFile):
  44. # https://sites.google.com/site/hipposubfields/tutorial#TOC-Viewing-ASHS-Segmentation-Results
  45. FILENAME_PATTERN = (
  46. r"^(?P<s>\w+)_(?P<h>left|right)"
  47. r"_(heur|corr_(?P<c>nogray|usegray))_volumes.txt$"
  48. )
  49. FILENAME_REGEX = re.compile(FILENAME_PATTERN)
  50. def __init__(self, path: str):
  51. self._absolute_path = pathlib.Path(path).absolute()
  52. filename_match = self.FILENAME_REGEX.match(self._absolute_path.name)
  53. assert filename_match, self._absolute_path
  54. filename_groups = filename_match.groupdict()
  55. self.subject = filename_groups["s"]
  56. self.hemisphere = filename_groups["h"]
  57. self.correction = filename_groups["c"]
  58. super().__init__(path=path)
  59. @property
  60. def absolute_path(self):
  61. return str(self._absolute_path)
  62. def read_volumes_mm3(self) -> typing.Dict[str, float]:
  63. subfield_volumes = {}
  64. for line in self._absolute_path.read_text().rstrip().split("\n"):
  65. # > echo $ASHS_SUBJID $side $SUB $NBODY $VSUB >> $FNBODYVOL
  66. # https://github.com/pyushkevich/ashs/blob/515ff7c2f50928adabc4e64bded9a7e76fc750b1/bin/ashs_extractstats_qsub.sh#L94
  67. (
  68. subject,
  69. hemisphere,
  70. subfield_name,
  71. slices_number_str,
  72. volume_mm3_str,
  73. ) = line.split(" ")
  74. assert self.subject == subject
  75. assert self.hemisphere == hemisphere
  76. assert int(slices_number_str) >= 0
  77. subfield_volumes[subfield_name] = float(volume_mm3_str)
  78. return subfield_volumes
  79. def read_volumes_dataframe(self) -> pandas.DataFrame:
  80. volumes_frame = self._read_volume_series().reset_index()
  81. volumes_frame["subject"] = self.subject
  82. volumes_frame["hemisphere"] = self.hemisphere
  83. volumes_frame["correction"] = self.correction
  84. return volumes_frame