app.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # MODULE: grass.benchmark
  2. #
  3. # AUTHOR(S): Vaclav Petras <wenzeslaus gmail com>
  4. #
  5. # PURPOSE: Benchmarking for GRASS GIS modules
  6. #
  7. # COPYRIGHT: (C) 2021 Vaclav Petras, and by the GRASS Development Team
  8. #
  9. # This program is free software under the GNU General Public
  10. # License (>=v2). Read the file COPYING that comes with GRASS
  11. # for details.
  12. """CLI for the benchmark package"""
  13. import argparse
  14. import sys
  15. from pathlib import Path
  16. from grass.benchmark import (
  17. join_results_from_files,
  18. load_results_from_file,
  19. num_cells_plot,
  20. save_results_to_file,
  21. )
  22. class CliUsageError(ValueError):
  23. """Raised when error is in the command line arguments.
  24. Used when the error is discovered only after argparse parsed the arguments.
  25. """
  26. # ArgumentError from argparse may work too, but it is not documented and
  27. # takes a reference argument which we don't have access to after the parse step.
  28. pass
  29. def join_results_cli(args):
  30. """Translate CLI parser result to API calls."""
  31. if args.prefixes and len(args.results) != len(args.prefixes):
  32. raise CliUsageError(
  33. f"Number of prefixes ({len(args.prefixes)}) needs to be the same"
  34. f" as the number of input result files ({len(args.results)})"
  35. )
  36. results = join_results_from_files(
  37. source_filenames=args.results,
  38. prefixes=args.prefixes,
  39. )
  40. save_results_to_file(results, args.output)
  41. def plot_cells_cli(args):
  42. """Translate CLI parser result to API calls."""
  43. results = load_results_from_file(args.input)
  44. num_cells_plot(
  45. results.results,
  46. filename=args.output,
  47. title=args.title,
  48. show_resolution=args.resolutions,
  49. )
  50. def get_executable_name():
  51. """Get name of the executable and module.
  52. This is a workaround for Python issue:
  53. argparse support for "python -m module" in help
  54. https://bugs.python.org/issue22240
  55. """
  56. executable = Path(sys.executable).stem
  57. return f"{executable} -m grass.benchmark"
  58. class ExtendAction(argparse.Action):
  59. """Support for agrparse action="extend" before Python 3.8
  60. Each parser instance needs the action to be registered.
  61. """
  62. # pylint: disable=too-few-public-methods
  63. def __call__(self, parser, namespace, values, option_string=None):
  64. items = getattr(namespace, self.dest) or []
  65. items.extend(values)
  66. setattr(namespace, self.dest, items)
  67. def add_subcommand_parser(subparsers, name, description):
  68. """Add parser for a subcommand into subparsers."""
  69. # help is in parent's help, description in subcommand's help.
  70. return subparsers.add_parser(name, help=description, description=description)
  71. def add_subparsers(parser, dest):
  72. """Add subparsers in a unified way.
  73. Uses title 'subcommands' for the list of commands
  74. (instead of the 'positional' which is the default).
  75. The *dest* should be 'command', 'subcommand', etc. with appropriate nesting.
  76. """
  77. if sys.version_info < (3, 7):
  78. # required as parameter is only in >=3.7.
  79. return parser.add_subparsers(title="subcommands", dest=dest)
  80. return parser.add_subparsers(title="subcommands", required=True, dest=dest)
  81. def add_results_subcommand(parent_subparsers):
  82. """Add results subcommand."""
  83. main_parser = add_subcommand_parser(
  84. parent_subparsers, "results", description="Manipulate results"
  85. )
  86. main_subparsers = add_subparsers(main_parser, dest="subcommand")
  87. join = main_subparsers.add_parser("join", help="Join results")
  88. join.add_argument("results", help="Files with results", nargs="*", metavar="file")
  89. join.add_argument("output", help="Output file", metavar="output_file")
  90. if sys.version_info < (3, 8):
  91. join.register("action", "extend", ExtendAction)
  92. join.add_argument(
  93. "--prefixes",
  94. help="Add prefixes to result labels per file",
  95. action="extend",
  96. nargs="*",
  97. metavar="text",
  98. )
  99. join.set_defaults(handler=join_results_cli)
  100. def add_plot_subcommand(parent_subparsers):
  101. """Add plot subcommand."""
  102. main_parser = add_subcommand_parser(
  103. parent_subparsers, "plot", description="Plot results"
  104. )
  105. main_subparsers = add_subparsers(main_parser, dest="subcommand")
  106. join = main_subparsers.add_parser("cells", help="Plot for variable number of cells")
  107. join.add_argument("input", help="file with results (JSON)", metavar="input_file")
  108. join.add_argument(
  109. "output", help="output file (e.g., PNG)", nargs="?", metavar="output_file"
  110. )
  111. join.add_argument(
  112. "--title",
  113. help="Title for the plot",
  114. metavar="text",
  115. )
  116. join.add_argument(
  117. "--resolutions",
  118. help="Use resolutions for x axis instead of cell count",
  119. action="store_true",
  120. )
  121. join.set_defaults(handler=plot_cells_cli)
  122. def define_arguments():
  123. """Define top level parser and create subparsers."""
  124. parser = argparse.ArgumentParser(
  125. description="Process results from module benchmarks.",
  126. prog=get_executable_name(),
  127. )
  128. subparsers = add_subparsers(parser, dest="command")
  129. add_results_subcommand(subparsers)
  130. add_plot_subcommand(subparsers)
  131. return parser
  132. def main(args=None):
  133. """Define and parse command line parameters then run the appropriate handler."""
  134. parser = define_arguments()
  135. args = parser.parse_args(args)
  136. try:
  137. args.handler(args)
  138. except CliUsageError as error:
  139. # Report a usage error and exit.
  140. sys.exit(f"ERROR: {error}")
  141. if __name__ == "__main__":
  142. main()