__main__.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 typing
  10. import pandas
  11. from freesurfer_volume_reader import __version__, ashs, freesurfer, parse_version_string, \
  12. remove_group_names_from_regex
  13. def concat_dataframes(dataframes: typing.Iterable[pandas.DataFrame]
  14. ) -> pandas.DataFrame: # pragma: no cover
  15. # pylint: disable=unexpected-keyword-arg
  16. if parse_version_string(pandas.__version__) < (0, 23):
  17. return pandas.concat(dataframes, ignore_index=True)
  18. return pandas.concat(dataframes, ignore_index=True, sort=False)
  19. VOLUME_FILE_FINDERS = {
  20. 'ashs': ashs.HippocampalSubfieldsVolumeFile,
  21. # https://github.com/freesurfer/freesurfer/tree/release_6_0_0/HippoSF
  22. 'freesurfer-hipposf': freesurfer.HippocampalSubfieldsVolumeFile,
  23. }
  24. def main():
  25. argparser = argparse.ArgumentParser(description=__doc__,
  26. formatter_class=argparse.RawDescriptionHelpFormatter)
  27. argparser.add_argument('--source-types', nargs='+', default=['freesurfer-hipposf'],
  28. choices=VOLUME_FILE_FINDERS.keys(),
  29. help='default: [freesurfer-hipposf]')
  30. for source_type, file_class in VOLUME_FILE_FINDERS.items():
  31. argparser.add_argument('--{}-filename-regex'.format(source_type),
  32. dest='filename_regex.{}'.format(source_type),
  33. metavar='REGULAR_EXPRESSION', type=re.compile,
  34. default=remove_group_names_from_regex(file_class.FILENAME_PATTERN),
  35. help='default: %(default)s')
  36. argparser.add_argument('--output-format', choices=['csv'], default='csv',
  37. help='default: %(default)s')
  38. subjects_dir_path = os.environ.get('SUBJECTS_DIR', None)
  39. argparser.add_argument('root_dir_paths',
  40. metavar='ROOT_DIR',
  41. nargs='*' if subjects_dir_path else '+',
  42. default=[subjects_dir_path],
  43. help='default: $SUBJECTS_DIR ({})'.format(subjects_dir_path))
  44. argparser.add_argument('--version', action='version', version=__version__)
  45. args = argparser.parse_args()
  46. filename_regexs = {k[len('filename_regex.'):]: v for k, v in vars(args).items()
  47. if k.startswith('filename_regex.')}
  48. volume_frames = []
  49. for source_type in args.source_types:
  50. find_volume_files = lambda dir_path: VOLUME_FILE_FINDERS[source_type].find(
  51. root_dir_path=dir_path, filename_regex=filename_regexs[source_type])
  52. for root_dir_path in args.root_dir_paths:
  53. for volume_file in find_volume_files(root_dir_path):
  54. volume_frame = volume_file.read_volumes_dataframe()
  55. volume_frame['source_type'] = source_type
  56. volume_frame['source_path'] = volume_file.absolute_path
  57. volume_frames.append(volume_frame)
  58. if not volume_frames:
  59. print('Did not find any volume files matching the specified criteria.', file=sys.stderr)
  60. return os.EX_NOINPUT
  61. united_volume_frame = concat_dataframes(volume_frames)
  62. print(united_volume_frame.to_csv(index=False))
  63. return os.EX_OK
  64. if __name__ == '__main__':
  65. sys.exit(main())