__main__.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """
  2. Read hippocampal subfield volumes computed by Freesurfer and/or ASHS
  3. and export collected data as CSV.
  4. """
  5. import argparse
  6. import os
  7. import re
  8. import sys
  9. import pandas
  10. from freesurfer_volume_reader import (
  11. __version__,
  12. ashs,
  13. freesurfer,
  14. remove_group_names_from_regex,
  15. )
  16. VOLUME_FILE_FINDERS = {
  17. "ashs": ashs.HippocampalSubfieldsVolumeFile,
  18. # https://github.com/freesurfer/freesurfer/tree/release_6_0_0/HippoSF
  19. "freesurfer-hipposf": freesurfer.HippocampalSubfieldsVolumeFile,
  20. }
  21. def main():
  22. argparser = argparse.ArgumentParser(
  23. description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
  24. )
  25. argparser.add_argument(
  26. "--source-types",
  27. nargs="+",
  28. default=["freesurfer-hipposf"],
  29. choices=VOLUME_FILE_FINDERS.keys(),
  30. help="default: [freesurfer-hipposf]",
  31. )
  32. for source_type, file_class in VOLUME_FILE_FINDERS.items():
  33. argparser.add_argument(
  34. f"--{source_type}-filename-regex",
  35. dest=f"filename_regex.{source_type}",
  36. metavar="REGULAR_EXPRESSION",
  37. type=re.compile,
  38. default=remove_group_names_from_regex(file_class.FILENAME_PATTERN),
  39. help="default: %(default)s",
  40. )
  41. argparser.add_argument(
  42. "--output-format", choices=["csv"], default="csv", help="default: %(default)s"
  43. )
  44. subjects_dir_path = os.environ.get("SUBJECTS_DIR", None)
  45. argparser.add_argument(
  46. "root_dir_paths",
  47. metavar="ROOT_DIR",
  48. nargs="*" if subjects_dir_path else "+",
  49. default=[subjects_dir_path],
  50. help=f"default: $SUBJECTS_DIR ({subjects_dir_path})",
  51. )
  52. argparser.add_argument("--version", action="version", version=__version__)
  53. args = argparser.parse_args()
  54. filename_regexs = {
  55. k[len("filename_regex.") :]: v
  56. for k, v in vars(args).items()
  57. if k.startswith("filename_regex.")
  58. }
  59. volume_frames = []
  60. for source_type in args.source_types:
  61. finder = VOLUME_FILE_FINDERS[source_type]
  62. for root_dir_path in args.root_dir_paths:
  63. for volume_file in finder.find(
  64. root_dir_path=root_dir_path, filename_regex=filename_regexs[source_type]
  65. ):
  66. volume_frame = volume_file.read_volumes_dataframe()
  67. volume_frame["source_type"] = source_type
  68. volume_frame["source_path"] = volume_file.absolute_path
  69. volume_frames.append(volume_frame)
  70. if not volume_frames:
  71. print(
  72. "Did not find any volume files matching the specified criteria.",
  73. file=sys.stderr,
  74. )
  75. return os.EX_NOINPUT
  76. united_volume_frame = pandas.concat(volume_frames, ignore_index=True, sort=False)
  77. print(united_volume_frame.to_csv(index=False))
  78. return os.EX_OK
  79. if __name__ == "__main__":
  80. sys.exit(main())