__main__.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # freesurfer-surface - Read and Write Surface Files in Freesurfer’s TriangularSurface Format
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import argparse
  18. import csv
  19. import sys
  20. from freesurfer_surface import Annotation, Surface
  21. def annotation_labels():
  22. """
  23. List Labels Stored in Freesurfer's Annotation File
  24. (i.e., label/lh.aparc.annot)
  25. """
  26. argparser = argparse.ArgumentParser(description=annotation_labels.__doc__.strip())
  27. argparser.add_argument("--delimiter", default="\t", help="default: %(default)r")
  28. argparser.add_argument("annotation_file_path")
  29. args = argparser.parse_args()
  30. annotation = Annotation.read(args.annotation_file_path)
  31. csv_writer = csv.writer(sys.stdout, delimiter=args.delimiter)
  32. csv_writer.writerow(("index", "color", "name"))
  33. labels = sorted(annotation.labels.values(), key=lambda l: l.index)
  34. csv_writer.writerows(
  35. (
  36. l.index,
  37. l.hex_color_code,
  38. l.name,
  39. )
  40. for l in labels
  41. )
  42. def unite_surfaces():
  43. """
  44. Unite Multiple Surfaces in Freesurfer's TriangularSurface Format
  45. Into a Single TriangularSurface File (i.e., lh.pial, lh.white)
  46. """
  47. argparser = argparse.ArgumentParser(description=unite_surfaces.__doc__.strip())
  48. argparser.add_argument(
  49. "--output", metavar="OUTPUT_PATH", dest="output_path", required=True
  50. )
  51. argparser.add_argument("input_paths", metavar="INPUT_PATH", nargs="+")
  52. args = argparser.parse_args()
  53. union = Surface.unite(Surface.read_triangular(p) for p in args.input_paths)
  54. union.write_triangular(args.output_path)