Python absl.app.UsageError() Examples

The following are 30 code examples of absl.app.UsageError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module absl.app , or try the search function .
Example #1
Source File: build_graph.py    From neural-structured-learning with Apache License 2.0 6 votes vote down vote up
def _main(argv):
  """Main function for invoking the `nsl.tools.build_graph` function."""
  flag = flags.FLAGS
  flag.showprefixforinfo = False
  if len(argv) < 3:
    raise app.UsageError(
        'Invalid number of arguments; expected 2 or more, got %d' %
        (len(argv) - 1))

  build_graph_from_config(
      argv[1:-1], argv[-1],
      nsl_configs.GraphBuilderConfig(
          id_feature_name=flag.id_feature_name,
          embedding_feature_name=flag.embedding_feature_name,
          similarity_threshold=flag.similarity_threshold,
          lsh_bits=flag.lsh_bits,
          random_seed=flag.random_seed)) 
Example #2
Source File: example_ternary.py    From qkeras with Apache License 2.0 6 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')

  # x = np.arange(-3.0, 3.0, 0.01)
  # x = np.random.uniform(-0.01, 0.01, size=1000)
  x = np.random.uniform(-10.0, 10.0, size=1000)
  # x = np.random.uniform(-1, 1, size=1000)
  x = np.sort(x)
  tr = np.zeros_like(x)
  t = np.zeros_like(x)
  iter_count = 500
  for _ in range(iter_count):
    y = _ternary(x)
    yr = _ternary(x, sto=True)
    t = t + y
    tr = tr + yr

  plt.plot(x, t/iter_count)
  plt.plot(x, tr/iter_count)
  plt.ylabel('mean (%s samples)' % iter_count)
  plt.show() 
Example #3
Source File: jwt-provider.py    From android-emulator-container-scripts with Apache License 2.0 6 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')

  global users
  global private_key

  with open(FLAGS.passwd) as f:
    users = json.load(f)

  logging.info('Loaded: %s', users.keys())

  # Note you really shouldn't have multiple keys in the jwks, as we will only use the last one.
  with open(FLAGS.jwk) as f:
    jwks = json.load(f)
    for key in jwks['keys']:
      private_key = (key['kid'], jwt.algorithms.RSAAlgorithm.from_jwk(json.dumps(key)))


  api.run(host='0.0.0.0', port=FLAGS.port) 
Example #4
Source File: jwt-provider.py    From Android-Malware-Sandbox with Apache License 2.0 6 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')

  global users
  global private_key

  with open(FLAGS.passwd) as f:
    users = json.load(f)

  logging.info('Loaded: %s', users.keys())

  # Note you really shouldn't have multiple keys in the jwks, as we will only use the last one.
  with open(FLAGS.jwk) as f:
    jwks = json.load(f)
    for key in jwks['keys']:
      private_key = (key['kid'], jwt.algorithms.RSAAlgorithm.from_jwk(json.dumps(key)))


  api.run(host='0.0.0.0', port=FLAGS.port) 
Example #5
Source File: app.py    From clgen with GNU General Public License v3.0 6 votes vote down vote up
def Run(main: Callable[[], None]):
  """Begin executing the program.

  Args:
    main: The main function to execute. It takes no arguments. If any command
    line arguments remain after flags parsing, an error is raised. If it
    returns an integer, it is used as the process's exit code.
  """

  def RunWithoutArgs(argv: List[str]):
    """Run the given function without arguments."""
    if len(argv) > 1:
      raise UsageError("Unknown arguments: '{}'.".format(" ".join(argv[1:])))
    main()

  RunWithArgs(RunWithoutArgs)


# Logging functions. 
Example #6
Source File: score_main.py    From lasertagger with Apache License 2.0 6 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')
  flags.mark_flag_as_required('prediction_file')

  sources, predictions, target_lists = score_lib.read_data(
      FLAGS.prediction_file, FLAGS.case_insensitive)
  logging.info(f'Read file: {FLAGS.prediction_file}')
  exact = score_lib.compute_exact_score(predictions, target_lists)
  sari, keep, addition, deletion = score_lib.compute_sari_scores(
      sources, predictions, target_lists)
  print(f'Exact score:     {100*exact:.3f}')
  print(f'SARI score:      {100*sari:.3f}')
  print(f' KEEP score:     {100*keep:.3f}')
  print(f' ADDITION score: {100*addition:.3f}')
  print(f' DELETION score: {100*deletion:.3f}') 
Example #7
Source File: heavy_hitters_utils.py    From federated with Apache License 2.0 6 votes vote down vote up
def tokenize(ds, dataset_name):
  """Tokenizes a line into words with alphanum characters."""

  def extract_strings(example):
    if dataset_name == 'shakespeare':
      return tf.expand_dims(example['snippets'], 0)
    elif dataset_name == 'stackoverflow':
      return example['tokens']
    else:
      raise app.UsageError('Dataset not supported: ', dataset_name)

  def tokenize_line(line):
    return tf.data.Dataset.from_tensor_slices(tokenizer.tokenize(line)[0])

  def mask_all_symbolic_words(word):
    return tf.math.logical_not(
        tf_text.wordshape(word, tf_text.WordShape.IS_PUNCT_OR_SYMBOL))

  tokenizer = tf_text.WhitespaceTokenizer()
  ds = ds.map(extract_strings)
  ds = ds.flat_map(tokenize_line)
  ds = ds.map(tf_text.case_fold_utf8)
  ds = ds.filter(mask_all_symbolic_words)
  return ds 
Example #8
Source File: aggregate_lending_recall_values.py    From ml-fairness-gym with Apache License 2.0 6 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')

  recall_values = {}
  for setting in ['static', 'dynamic']:
    recall = []
    for seed in range(FLAGS.num_trials):
      with file_util.open(
          os.path.join(FLAGS.source_path, str(seed), 'cumulative_recall_%s.txt')
          % setting, 'r') as infile:
        recall.append(np.loadtxt(infile))
      recall_values[setting] = np.vstack(recall)

  lending_plots.plot_cumulative_recall_differences(
      recall_values,
      path=os.path.join(FLAGS.plotting_dir, 'combined_simpsons_paradox.pdf')) 
Example #9
Source File: train_emnist_classifier_model.py    From federated with Apache License 2.0 6 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')

  # Datasets.
  preprocessed_train_dataset, preprocessed_test_dataset = (
      _load_and_preprocess_datasets())

  # Train and Evaluate Model.
  model = _train_and_evaluate(
      preprocessed_train_dataset,
      preprocessed_test_dataset,
      epochs=FLAGS.epochs)

  # Save Model.
  _save(model, FLAGS.path_to_save_model_checkpoint) 
Example #10
Source File: export_model.py    From models with Apache License 2.0 6 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')

  export_path = FLAGS.export_path
  if os.path.exists(export_path):
    raise ValueError(f'Export_path {export_path} already exists. Please '
                     'specify a different path or delete the existing one.')

  module = _ExtractModule(FLAGS.block3_strides, FLAGS.iou)

  # Load the weights.
  checkpoint_path = FLAGS.ckpt_path
  module.LoadWeights(checkpoint_path)
  print('Checkpoint loaded from ', checkpoint_path)

  # Save the module
  tf.saved_model.save(module, export_path) 
Example #11
Source File: college_admission_main.py    From ml-fairness-gym with Apache License 2.0 6 votes vote down vote up
def main(argv):
  # TODO(): add tests to check correct plots generated.
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')
  deltas = None

  # Casting to float since these are sometimes passed through flags as strings.
  feature_mu = [float(mu) for mu in FLAGS.feature_mu]
  if FLAGS.noisy_features or FLAGS.noisy_threshold:
    results, thresholds, deltas, stdevs = run_noisy_experiment(
        noise_dist=FLAGS.noisy_dist,
        noisy_features=FLAGS.noisy_features,
        noisy_threshold=FLAGS.noisy_threshold,
        feature_mu=feature_mu)
    plot_deltas(deltas)
    for sd in stdevs:
      plot_thresholds(results[sd], thresholds, name=sd)
      plot_metrics(results[sd], thresholds, name=sd)

  else:
    results, thresholds = run_baseline_experiment(FLAGS.feature_mu)
    results_noise, _, _, _ = run_noisy_experiment(
        noisy_features=True, stdevs=[0.1])
    plot_threshold_pair([results, results_noise[0.1]],
                        ['Noiseless Features', 'Noisy Features'], thresholds) 
Example #12
Source File: pack_nbrs.py    From neural-structured-learning with Apache License 2.0 6 votes vote down vote up
def _main(argv):
  """Main function for invoking the `nsl.tools.pack_nbrs` function."""
  flag = flags.FLAGS
  flag.showprefixforinfo = False
  # Check that the correct number of arguments have been provided.
  if len(argv) != 5:
    raise app.UsageError('Invalid number of arguments; expected 4, got %d' %
                         (len(argv) - 1))

  pack_nbrs(
      labeled_examples_path=argv[1],
      unlabeled_examples_path=argv[2],
      graph_path=argv[3],
      output_training_data_path=argv[4],
      add_undirected_edges=flag.add_undirected_edges,
      max_nbrs=flag.max_nbrs,
      id_feature_name=flag.id_feature_name) 
Example #13
Source File: gke_launch.py    From lingvo with Apache License 2.0 6 votes vote down vote up
def validate_args(argv):
  """Validates the input arguments. Raises a UsageError if invalid."""
  valid_commands = ["up", "down", "print", "reload"]
  error = ""
  if len(argv) < 2:
    error = ("Command not provided. "
             "Command must be one of %s" % valid_commands)
  elif argv[1] not in valid_commands:
    error = "Command must be one of %s" % valid_commands

  if len(argv) > 3:
    print("Too many arguments.")

  if len(argv) == 3:
    valid_targets = ["trainer", "decoder", "tensorboard", "all"]
    if argv[2] not in valid_targets:
      error = "Target must be one of %s" % valid_targets

  if error:
    raise app.UsageError(error) 
Example #14
Source File: gftools-compare-font.py    From gftools with Apache License 2.0 6 votes vote down vote up
def main(_):
  if len(sys.argv) < 3:
    raise app.UsageError('Must pass at least two arguments, font file or font'
                         ' dir to diff')

  font1 = sys.argv[1]
  font2 = sys.argv[2]
  dirs = os.path.isdir(font1) and os.path.isdir(font2)
  files = os.path.isfile(font1) and os.path.isfile(font2)

  if not dirs and not files:
    print('%s and %s must both point to directories or font files' % (
        font1, font2))
    sys.exit(1)

  if dirs:
    CompareDirs(font1, font2)

  if files:
    CompareFiles(font1, font2) 
Example #15
Source File: tfhub_export.py    From models with Apache License 2.0 5 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError("Too many command-line arguments.")

  export_tfhub(FLAGS.model_path, FLAGS.export_path) 
Example #16
Source File: rouge.py    From compare-mt with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError("Too many command-line arguments.")
  scorer = rouge_scorer.RougeScorer(FLAGS.rouge_types, FLAGS.use_stemmer)
  aggregator = scoring.BootstrapAggregator() if FLAGS.aggregate else None
  io.compute_scores_and_write_to_csv(
      FLAGS.target_filepattern,
      FLAGS.prediction_filepattern,
      FLAGS.output_filename,
      scorer,
      aggregator,
      delimiter=FLAGS.delimiter) 
Example #17
Source File: update_resign_threshold.py    From training with Apache License 2.0 5 votes vote down vote up
def main(argv):
    if len(argv) > 1:
        raise app.UsageError('Too many command-line arguments.')

    games_nr = bigtable_input.GameQueue(
        FLAGS.cbt_project, FLAGS.cbt_instance, FLAGS.cbt_table + '-nr')

    while True:
        new_pct = get_95_percentile_bleak(games_nr)
        update_flagfile(fsdb.flags_path(), new_pct)
        time.sleep(60 * 3) 
Example #18
Source File: flags_parsers.py    From clgen with GNU General Public License v3.0 5 votes vote down vote up
def convert(self, argument: str) -> DatabaseFlag:
    """Returns the value of this argument."""
    if not argument:
      raise absl_app.UsageError("Database flag must be set")
    return DatabaseFlag(self.database_class, argument, self.must_exist) 
Example #19
Source File: flags_parsers.py    From clgen with GNU General Public License v3.0 5 votes vote down vote up
def __call__(self):
    try:
      return self.database_class(url=self.url, must_exist=self.must_exist)
    except Exception as e:
      raise absl_app.UsageError(
        f"Failed to construct database {self.database_class.__name__}({self.url}): {e}"
      ) 
Example #20
Source File: export_global_model.py    From models with Apache License 2.0 5 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')

  export_path = FLAGS.export_path
  if os.path.exists(export_path):
    raise ValueError('export_path %s already exists.' % export_path)

  if FLAGS.input_scales_list is None:
    input_scales_tensor = None
  else:
    input_scales_tensor = tf.constant(
        [float(s) for s in FLAGS.input_scales_list],
        dtype=tf.float32,
        shape=[len(FLAGS.input_scales_list)],
        name='input_scales')
  module = _ExtractModule(FLAGS.multi_scale_pool_type,
                          FLAGS.normalize_global_descriptor,
                          input_scales_tensor)

  # Load the weights.
  checkpoint_path = FLAGS.ckpt_path
  module.LoadWeights(checkpoint_path)
  print('Checkpoint loaded from ', checkpoint_path)

  # Save the module
  if FLAGS.input_scales_list is None:
    served_function = module.ExtractFeatures
  else:
    served_function = module.ExtractFeaturesFixedScales

  tf.saved_model.save(
      module, export_path, signatures={'serving_default': served_function}) 
Example #21
Source File: count_features.py    From language with Apache License 2.0 5 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')
  count_features(FLAGS.preprocessed_dir, FLAGS.output_file) 
Example #22
Source File: deploy_impl.py    From loaner with Apache License 2.0 5 votes vote down vote up
def __init__(
      self, app_servers, build_target, deployment_type, loaner_path, version,
      web_app_dir, yaml_files):
    super(AppEngineServerConfig, self).__init__(deployment_type, loaner_path)
    self._build_target = build_target
    self._version = version
    self._web_app_dir = web_app_dir
    self._yaml_files = yaml_files
    self._app_servers = _ParseAppServers(app_servers)
    if self._deployment_type not in list(self._app_servers.keys()):
      raise app.UsageError(
          'Application name provided is not in the list of App Servers.\n'
          'Please check the name and/or the deploy.sh configuration.') 
Example #23
Source File: util.py    From language with Apache License 2.0 5 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.') 
Example #24
Source File: tokenization.py    From language with Apache License 2.0 5 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.') 
Example #25
Source File: gen-passwords.py    From android-emulator-container-scripts with Apache License 2.0 5 votes vote down vote up
def main(argv):
    if len(argv) > 1:
        raise app.UsageError("Too many command-line arguments.")

    # Create salted passwords
    unsalted = pairwise(FLAGS.pairs)
    salted = {}
    for pair in unsalted:
        logging.info("%s : %s", pair[0], pair[1])
        salted[pair[0]] = generate_password_hash(pair[1])

    # And write them to a file
    with open(FLAGS.passwords, "w") as f:
        f.write(json.dumps(salted))

    # Create the jwks secrets and export them
    keys = jwk.JWK.generate(kty="RSA", size=2048)

    # Python 2 does signed crc32, unlike Python 3
    kid = hex(binascii.crc32(keys.export_public().encode('utf-8')) & 0xFFFFFFFF)

    public = json.loads(keys.export_public())
    private = json.loads(keys.export_private())
    public["kid"] = kid
    private["kid"] = kid
    public_jwks = {"keys": [public]}
    private_jwks = {"keys": [private]}

    with open(FLAGS.jwks + '_pub.jwks', 'w') as f:
        f.write(json.dumps(public_jwks, indent=2))

    with open(FLAGS.jwks + '_priv.jwks', 'w') as f:
        f.write(json.dumps(private_jwks, indent=2)) 
Example #26
Source File: compute_dp_sgd_privacy_lib.py    From privacy with Apache License 2.0 5 votes vote down vote up
def compute_dp_sgd_privacy(n, batch_size, noise_multiplier, epochs, delta):
  """Compute epsilon based on the given hyperparameters."""
  q = batch_size / n  # q - the sampling ratio.
  if q > 1:
    raise app.UsageError('n must be larger than the batch size.')
  orders = ([1.25, 1.5, 1.75, 2., 2.25, 2.5, 3., 3.5, 4., 4.5] +
            list(range(5, 64)) + [128, 256, 512])
  steps = int(math.ceil(epochs * n / batch_size))

  return apply_dp_sgd_analysis(q, noise_multiplier, steps, orders, delta) 
Example #27
Source File: eval_coco_format.py    From MAX-Image-Segmenter with Apache License 2.0 5 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')

  eval_coco_format(FLAGS.gt_json_file, FLAGS.pred_json_file, FLAGS.gt_folder,
                   FLAGS.pred_folder, FLAGS.metric, FLAGS.num_categories,
                   FLAGS.ignored_label, FLAGS.max_instances_per_category,
                   FLAGS.intersection_offset, FLAGS.normalize_by_image_size,
                   FLAGS.num_workers, FLAGS.print_digits) 
Example #28
Source File: r2d2_main.py    From seed_rl with Apache License 2.0 5 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')
  if FLAGS.run_mode == 'actor':
    actor.actor_loop(env.create_environment)
  elif FLAGS.run_mode == 'learner':
    learner.learner_loop(env.create_environment,
                         create_agent,
                         create_optimizer)
  else:
    raise ValueError('Unsupported run mode {}'.format(FLAGS.run_mode)) 
Example #29
Source File: vtrace_main.py    From seed_rl with Apache License 2.0 5 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')
  if FLAGS.run_mode == 'actor':
    actor.actor_loop(env.create_environment)
  elif FLAGS.run_mode == 'learner':
    learner.learner_loop(env.create_environment,
                         create_agent,
                         create_optimizer)
  else:
    raise ValueError('Unsupported run mode {}'.format(FLAGS.run_mode)) 
Example #30
Source File: vtrace_main.py    From seed_rl with Apache License 2.0 5 votes vote down vote up
def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')
  if FLAGS.run_mode == 'actor':
    actor.actor_loop(env.create_environment)
  elif FLAGS.run_mode == 'learner':
    learner.learner_loop(env.create_environment,
                         create_agent,
                         create_optimizer)
  else:
    raise ValueError('Unsupported run mode {}'.format(FLAGS.run_mode))