__main__.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """
  2. Read hippocampal subfield volumes computed by Freesurfer
  3. and export collected data as CSV.
  4. """
  5. import argparse
  6. import os
  7. import re
  8. import pandas
  9. from freesurfer_volume_reader.freesurfer import HippocampalSubfieldsVolumeFile
  10. def remove_group_names_from_regex(regex_pattern: str) -> str:
  11. return re.sub(r'\?P<.+?>', '', regex_pattern)
  12. def main():
  13. argparser = argparse.ArgumentParser(description=__doc__,
  14. formatter_class=argparse.RawDescriptionHelpFormatter)
  15. argparser.add_argument('--filename-regex', type=re.compile,
  16. default=remove_group_names_from_regex(
  17. HippocampalSubfieldsVolumeFile.FILENAME_PATTERN),
  18. help='default: %(default)s')
  19. argparser.add_argument('--output-format', choices=['csv'], default='csv',
  20. help='default: %(default)s')
  21. subjects_dir_path = os.environ.get('SUBJECTS_DIR', None)
  22. argparser.add_argument('root_dir_paths',
  23. metavar='ROOT_DIR',
  24. nargs='*' if subjects_dir_path else '+',
  25. default=[subjects_dir_path],
  26. help='default: $SUBJECTS_DIR ({})'.format(subjects_dir_path))
  27. args = argparser.parse_args()
  28. volume_files = [f for d in args.root_dir_paths
  29. for f in HippocampalSubfieldsVolumeFile.find(
  30. root_dir_path=d, filename_regex=args.filename_regex)]
  31. volume_frames = []
  32. for volume_file in volume_files:
  33. volume_frame = volume_file.read_volumes_dataframe()
  34. volume_frame['source_path'] = volume_file.absolute_path
  35. volume_frames.append(volume_frame)
  36. united_volume_frame = pandas.concat(volume_frames, ignore_index=True)
  37. print(united_volume_frame.to_csv(index=False))
  38. if __name__ == '__main__':
  39. main()