r"""Trainer for RL environments.

For now we only support PPO as RL algorithm.

Sample invocation:

.. code-block:: bash

    TRAIN_BATCH_SIZE=32
    python trax/rl_trainer.py \
      --config_file=trax/rl/configs/ppo_acrobot.gin \
      --train_batch_size=${TRAIN_BATCH_SIZE} \
      --output_dir=${HOME}/ppo_acrobot \
      --alsologtostderr
"""

import faulthandler
import multiprocessing
import os

from absl import app
from absl import flags
from absl import logging
import gin
import jax
from jax.config import config
from tensor2tensor import envs  # pylint: disable=unused-import
from tensor2tensor.envs import env_problem_utils
from trax import fastmath
from trax import rl  # pylint: disable=unused-import
from trax import trainer_flags  # pylint: disable=unused-import
from trax.rl import task as rl_task
from trax.rl import training as light_trainers
from trax.tf_numpy import numpy as tf_np

FLAGS = flags.FLAGS

# Not just 'train' to avoid a conflict with trax.train in GIN files.
@gin.configurable(blacklist=[
    'output_dir', 'train_batch_size', 'eval_batch_size', 'trajectory_dump_dir'
])
def train_rl(
    output_dir,
    train_batch_size,
    eval_batch_size,
    env_name='Acrobot-v1',
    max_timestep=None,
    clip_rewards=False,
    rendered_env=False,
    resize=False,
    resize_dims=(105, 80),
    trainer_class=None,
    n_epochs=10000,
    trajectory_dump_dir=None,
    num_actions=None,
    light_rl=True,
    light_rl_trainer=light_trainers.PolicyGradient,
):
  """Train the RL agent.

  Args:
    output_dir: Output directory.
    train_batch_size: Number of parallel environments to use for training.
    eval_batch_size: Number of parallel environments to use for evaluation.
    env_name: Name of the environment.
    max_timestep: Int or None, the maximum number of timesteps in a trajectory.
      The environment is wrapped in a TimeLimit wrapper.
    clip_rewards: Whether to clip and discretize the rewards.
    rendered_env: Whether the environment has visual input. If so, a
      RenderedEnvProblem will be used.
    resize: whether to do resize or not
    resize_dims: Pair (height, width), dimensions to resize the visual
      observations to.
    trainer_class: RLTrainer class to use.
    n_epochs: Number epochs to run the training for.
    trajectory_dump_dir: Directory to dump trajectories to.
    num_actions: None unless one wants to use the discretization wrapper. Then
      num_actions specifies the number of discrete actions.
    light_rl: whether to use the light RL setting (experimental).
    light_rl_trainer: which light RL trainer to use (experimental).
  """
  tf_np.set_allow_float64(FLAGS.tf_allow_float64)

  if light_rl:
    task = rl_task.RLTask()
    env_name = task.env_name
  else:
    # TODO(lukaszkaiser): remove the name light and all references.
    # It was kept for now to make sure all regression tests pass first,
    # so that if we need to revert we save some work.
    raise ValueError('Non-light RL is deprecated.')

  if FLAGS.jax_debug_nans:
    config.update('jax_debug_nans', True)

  if FLAGS.use_tpu:
    config.update('jax_platform_name', 'tpu')
  else:
    config.update('jax_platform_name', '')

  if light_rl:
    trainer = light_rl_trainer(task=task, output_dir=output_dir)
    def light_training_loop():
      """Run the trainer for n_epochs and call close on it."""
      try:
        logging.info('Starting RL training for %d epochs.', n_epochs)
        trainer.run(n_epochs, n_epochs_is_total_epochs=True)
        logging.info('Completed RL training for %d epochs.', n_epochs)
        trainer.close()
        logging.info('Trainer is now closed.')
      except Exception as e:
        raise e
      finally:
        logging.info('Encountered an exception, still calling trainer.close()')
        trainer.close()
        logging.info('Trainer is now closed.')

    if FLAGS.jax_debug_nans or FLAGS.disable_jit:
      fastmath.disable_jit()
      with jax.disable_jit():
        light_training_loop()
    else:
      light_training_loop()
    return

  # TODO(pkozakowski): Find a better way to determine this.
  train_env_kwargs = {}
  eval_env_kwargs = {}
  if 'OnlineTuneEnv' in env_name:
    envs_output_dir = FLAGS.envs_output_dir or os.path.join(output_dir, 'envs')
    train_env_output_dir = os.path.join(envs_output_dir, 'train')
    eval_env_output_dir = os.path.join(envs_output_dir, 'eval')
    train_env_kwargs = {'output_dir': train_env_output_dir}
    eval_env_kwargs = {'output_dir': eval_env_output_dir}

  parallelism = multiprocessing.cpu_count() if FLAGS.parallelize_envs else 1

  logging.info('Num discretized actions %s', num_actions)
  logging.info('Resize %d', resize)

  train_env = env_problem_utils.make_env(
      batch_size=train_batch_size,
      env_problem_name=env_name,
      rendered_env=rendered_env,
      resize=resize,
      resize_dims=resize_dims,
      max_timestep=max_timestep,
      clip_rewards=clip_rewards,
      parallelism=parallelism,
      use_tpu=FLAGS.use_tpu,
      num_actions=num_actions,
      **train_env_kwargs)
  assert train_env

  eval_env = env_problem_utils.make_env(
      batch_size=eval_batch_size,
      env_problem_name=env_name,
      rendered_env=rendered_env,
      resize=resize,
      resize_dims=resize_dims,
      max_timestep=max_timestep,
      clip_rewards=clip_rewards,
      parallelism=parallelism,
      use_tpu=FLAGS.use_tpu,
      num_actions=num_actions,
      **eval_env_kwargs)
  assert eval_env

  def run_training_loop():
    """Runs the training loop."""
    logging.info('Starting the training loop.')

    trainer = trainer_class(
        output_dir=output_dir,
        train_env=train_env,
        eval_env=eval_env,
        trajectory_dump_dir=trajectory_dump_dir,
        async_mode=FLAGS.async_mode,
    )
    trainer.training_loop(n_epochs=n_epochs)

  if FLAGS.jax_debug_nans or FLAGS.disable_jit:
    fastmath.disable_jit()
    with jax.disable_jit():
      run_training_loop()
  else:
    run_training_loop()

def main(argv):
  del argv
  logging.info('Starting RL training.')

  gin_configs = FLAGS.config if FLAGS.config is not None else []
  gin.parse_config_files_and_bindings(FLAGS.config_file, gin_configs)

  logging.info('Gin config:')
  logging.info(gin_configs)

  train_rl(
      output_dir=FLAGS.output_dir,
      train_batch_size=FLAGS.train_batch_size,
      eval_batch_size=FLAGS.eval_batch_size,
      trajectory_dump_dir=(FLAGS.trajectory_dump_dir or None),
  )

  # TODO(afrozm): This is for debugging.
  logging.info('Dumping stack traces of all stacks.')
  faulthandler.dump_traceback(all_threads=True)

  logging.info('Training is done, should exit.')

if __name__ == '__main__':
  app.run(main)


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import uuid

from six.moves import xrange  # pylint: disable=redefined-builtin

import tensorflow as tf
from tensorflow.contrib.linear_optimizer.python.ops.sdca_ops import SdcaModel
from tensorflow.python.framework.test_util import TensorFlowTestCase
from tensorflow.python.platform import googletest

_MAX_ITERATIONS = 100

def make_example_proto(feature_dict, target, value=1.0):
  e = tf.train.Example()
  features = e.features

  features.feature['target'].float_list.value.append(target)

  for key, values in feature_dict.items():
    features.feature[key + '_indices'].int64_list.value.extend(values)
    features.feature[key + '_values'].float_list.value.extend([value] *
                                                              len(values))

  return e

def make_example_dict(example_protos, example_weights):

  def parse_examples(example_protos):
    features = {
        'target': tf.FixedLenFeature(shape=[1],
                                     dtype=tf.float32,
                                     default_value=0),
        'age_indices': tf.VarLenFeature(dtype=tf.int64),
        'age_values': tf.VarLenFeature(dtype=tf.float32),
        'gender_indices': tf.VarLenFeature(dtype=tf.int64),
        'gender_values': tf.VarLenFeature(dtype=tf.float32)
    }
    return tf.parse_example(
        [e.SerializeToString() for e in example_protos], features)

  sparse_merge = lambda ids, values: tf.sparse_merge(ids, values, ids.shape[1])

  parsed = parse_examples(example_protos)
  sparse_features = [
      sparse_merge(parsed['age_indices'], parsed['age_values']),
      sparse_merge(parsed['gender_indices'], parsed['gender_values'])
  ]
  return dict(sparse_features=sparse_features,
              dense_features=[],
              example_weights=example_weights,
              example_labels=tf.reshape(parsed['target'], [-1]),
              example_ids=['%d' % i for i in xrange(0, len(example_protos))])

def make_dense_examples_dict(dense_feature_values, weights, labels):
  dense_feature_tensors = ([
      tf.convert_to_tensor(values,
                           dtype=tf.float32) for values in dense_feature_values
  ])
  return dict(sparse_features=[],
              dense_features=dense_feature_tensors,
              example_weights=weights,
              example_labels=labels,
              example_ids=['%d' % i for i in xrange(0, len(labels))])

def make_variable_dict(max_age, max_gender):
  # TODO(sibyl-toe9oF2e):  Figure out how to derive max_age & max_gender from
  # examples_dict.
  age_weights = tf.Variable(tf.zeros([max_age + 1], dtype=tf.float32))
  gender_weights = tf.Variable(tf.zeros([max_gender + 1], dtype=tf.float32))
  return dict(sparse_features_weights=[age_weights, gender_weights],
              dense_features_weights=[])

def make_dense_variable_dict(num_dense_features, num_examples):
  feature_weights = ([
      tf.Variable(tf.zeros([1],
                           dtype=tf.float32))
      for _ in xrange(0, num_dense_features)
  ])
  return dict(sparse_features_weights=[],
              dense_features_weights=feature_weights,
              dual=tf.Variable(tf.zeros(
                  [num_examples],
                  dtype=tf.float32)),
              primal_loss=tf.Variable(tf.zeros(
                  [],
                  dtype=tf.float64)))

def get_binary_predictions_for_logistic(predictions, cutoff=0.5):
  return tf.cast(
      tf.greater_equal(predictions, tf.ones_like(predictions) * cutoff),
      dtype=tf.int32)

def get_binary_predictions_for_hinge(predictions):
  return tf.cast(
      tf.greater_equal(predictions, tf.zeros_like(predictions)),
      dtype=tf.int32)

# Setup the single container shared across all tests. This is testing proper
# isolation across optimizers instantiated in each of the tests below.
CONTAINER = uuid.uuid4().hex

# Clear the shared container.
def tearDown():
  # TODO(sibyl-Mooth6ku): Proper cleanup of Containers when possible.
  pass

# TODO(sibyl-Mooth6ku): Add tests that exercise L1 and Shrinking.
# TODO(sibyl-vie3Poto): Refactor tests to avoid repetition of boilerplate code.
class SdcaOptimizerTest(TensorFlowTestCase):
  """Base SDCA optimizer test class for any loss type."""

  def _single_threaded_test_session(self):
    config = tf.ConfigProto(inter_op_parallelism_threads=1,
                            intra_op_parallelism_threads=1)
    return self.test_session(use_gpu=False, config=config)

  # The following tests, check that operations raise errors when certain
  # preconditions on the input data are not satisfied. These errors are raised
  # regardless of the loss type.
  def testNoWeightedExamples(self):
    # Setup test data with 1 positive, and 1 negative example.
    example_protos = [
        make_example_proto(
            {'age': [0],
             'gender': [0]}, 0),
        make_example_proto(
            {'age': [1],
             'gender': [1]}, 1),
    ]
    # Zeroed out example weights.
    example_weights = [0.0, 0.0]
    with self._single_threaded_test_session():
      examples = make_example_dict(example_protos, example_weights)
      variables = make_variable_dict(1, 1)
      options = dict(symmetric_l2_regularization=1,
                     symmetric_l1_regularization=0,
                     loss_type='logistic_loss')

      lr = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      self.assertAllClose([0.5, 0.5], lr.predictions(examples).eval())
      lr.minimize().run()
      self.assertAllClose([0.5, 0.5], lr.predictions(examples).eval())
      with self.assertRaisesOpError(
          'No examples found or all examples have zero weight.'):
        lr.approximate_duality_gap().eval()

class SdcaWithLogisticLossTest(SdcaOptimizerTest):
  """SDCA optimizer test class for logistic loss."""

  def testSimple(self):
    # Setup test data
    example_protos = [
        make_example_proto(
            {'age': [0],
             'gender': [0]}, 0),
        make_example_proto(
            {'age': [1],
             'gender': [1]}, 1),
    ]
    example_weights = [1.0, 1.0]
    with self._single_threaded_test_session():
      examples = make_example_dict(example_protos, example_weights)
      variables = make_variable_dict(1, 1)
      options = dict(symmetric_l2_regularization=1,
                     symmetric_l1_regularization=0,
                     loss_type='logistic_loss')

      lr = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      unregularized_loss = lr.unregularized_loss(examples)
      loss = lr.regularized_loss(examples)
      predictions = lr.predictions(examples)
      self.assertAllClose(0.693147, unregularized_loss.eval())
      self.assertAllClose(0.693147, loss.eval())
      train_op = lr.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()
      # The high tolerance in unregularized_loss comparisons is due to the
      # fact that it's possible to trade off unregularized_loss vs.
      # regularization and still have a sum that is quite close to the
      # optimal regularized_loss value.  SDCA's duality gap only ensures that
      # the regularized_loss is within 0.01 of optimal.
      # 0.525457 is the optimal regularized_loss.
      # 0.411608 is the unregularized_loss at that optimum.
      self.assertAllClose(0.411608, unregularized_loss.eval(), atol=0.05)
      self.assertAllClose(0.525457, loss.eval(), atol=0.01)
      predicted_labels = get_binary_predictions_for_logistic(predictions)
      self.assertAllEqual([0, 1], predicted_labels.eval())
      self.assertAllClose(0.01,
                          lr.approximate_duality_gap().eval(),
                          rtol=1e-2,
                          atol=1e-2)

  def testSimpleNoL2(self):
    # Same as test above (so comments from above apply) but without an L2.
    # The algorithm should behave as if we have an L2 of 1 in optimization but
    # 0 in regularized_loss.
    example_protos = [
        make_example_proto(
            {'age': [0],
             'gender': [0]}, 0),
        make_example_proto(
            {'age': [1],
             'gender': [1]}, 1),
    ]
    example_weights = [1.0, 1.0]
    with self._single_threaded_test_session():
      examples = make_example_dict(example_protos, example_weights)
      variables = make_variable_dict(1, 1)
      options = dict(symmetric_l2_regularization=0,
                     symmetric_l1_regularization=0,
                     loss_type='logistic_loss')

      lr = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      unregularized_loss = lr.unregularized_loss(examples)
      loss = lr.regularized_loss(examples)
      predictions = lr.predictions(examples)
      self.assertAllClose(0.693147, unregularized_loss.eval())
      self.assertAllClose(0.693147, loss.eval())
      train_op = lr.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()

      # There is neither L1 nor L2 loss, so regularized and unregularized losses
      # should be exactly the same.
      self.assertAllClose(0.40244, unregularized_loss.eval(), atol=0.01)
      self.assertAllClose(0.40244, loss.eval(), atol=0.01)
      predicted_labels = get_binary_predictions_for_logistic(predictions)
      self.assertAllEqual([0, 1], predicted_labels.eval())
      self.assertAllClose(0.01,
                          lr.approximate_duality_gap().eval(),
                          rtol=1e-2,
                          atol=1e-2)

  def testSomeUnweightedExamples(self):
    # Setup test data with 4 examples, but should produce the same
    # results as testSimple.
    example_protos = [
        # Will be used.
        make_example_proto(
            {'age': [0],
             'gender': [0]}, 0),
        # Will be ignored.
        make_example_proto(
            {'age': [1],
             'gender': [0]}, 0),
        # Will be used.
        make_example_proto(
            {'age': [1],
             'gender': [1]}, 1),
        # Will be ignored.
        make_example_proto(
            {'age': [1],
             'gender': [0]}, 1),
    ]
    example_weights = [1.0, 0.0, 1.0, 0.0]
    with self._single_threaded_test_session():
      # Only use examples 0 and 2
      examples = make_example_dict(example_protos, example_weights)
      variables = make_variable_dict(1, 1)
      options = dict(symmetric_l2_regularization=1,
                     symmetric_l1_regularization=0,
                     loss_type='logistic_loss')

      lr = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      unregularized_loss = lr.unregularized_loss(examples)
      loss = lr.regularized_loss(examples)
      predictions = lr.predictions(examples)
      train_op = lr.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()

      self.assertAllClose(0.411608, unregularized_loss.eval(), atol=0.05)
      self.assertAllClose(0.525457, loss.eval(), atol=0.01)
      predicted_labels = get_binary_predictions_for_logistic(predictions)
      self.assertAllClose([0, 1, 1, 1], predicted_labels.eval())
      self.assertAllClose(0.01,
                          lr.approximate_duality_gap().eval(),
                          rtol=1e-2,
                          atol=1e-2)

  def testFractionalExampleLabel(self):
    # Setup test data with 1 positive, and 1 mostly-negative example.
    example_protos = [
        make_example_proto(
            {'age': [0],
             'gender': [0]}, 0.1),
        make_example_proto(
            {'age': [1],
             'gender': [1]}, 1),
    ]
    example_weights = [1.0, 1.0]
    with self._single_threaded_test_session():
      examples = make_example_dict(example_protos, example_weights)
      variables = make_variable_dict(1, 1)
      options = dict(symmetric_l2_regularization=1,
                     symmetric_l1_regularization=0,
                     loss_type='logistic_loss')

      lr = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      with self.assertRaisesOpError(
          'Only labels of 0.0 or 1.0 are supported right now.'):
        lr.minimize().run()

  def testImbalanced(self):
    # Setup test data with 1 positive, and 3 negative examples.
    example_protos = [
        make_example_proto(
            {'age': [0],
             'gender': [0]}, 0),
        make_example_proto(
            {'age': [2],
             'gender': [0]}, 0),
        make_example_proto(
            {'age': [3],
             'gender': [0]}, 0),
        make_example_proto(
            {'age': [1],
             'gender': [1]}, 1),
    ]
    example_weights = [1.0, 1.0, 1.0, 1.0]
    with self._single_threaded_test_session():
      examples = make_example_dict(example_protos, example_weights)
      variables = make_variable_dict(3, 1)
      options = dict(symmetric_l2_regularization=1,
                     symmetric_l1_regularization=0,
                     loss_type='logistic_loss')

      lr = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      unregularized_loss = lr.unregularized_loss(examples)
      loss = lr.regularized_loss(examples)
      predictions = lr.predictions(examples)
      train_op = lr.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()

      self.assertAllClose(0.226487 + 0.102902,
                          unregularized_loss.eval(),
                          atol=0.08)
      self.assertAllClose(0.328394 + 0.131364, loss.eval(), atol=0.01)
      predicted_labels = get_binary_predictions_for_logistic(predictions)
      self.assertAllEqual([0, 0, 0, 1], predicted_labels.eval())
      self.assertAllClose(0.01,
                          lr.approximate_duality_gap().eval(),
                          rtol=1e-2,
                          atol=1e-2)

  def testImbalancedWithExampleWeights(self):
    # Setup test data with 1 positive, and 1 negative example.
    example_protos = [
        make_example_proto(
            {'age': [0],
             'gender': [0]}, 0),
        make_example_proto(
            {'age': [1],
             'gender': [1]}, 1),
    ]
    example_weights = [3.0, 1.0]
    with self._single_threaded_test_session():
      examples = make_example_dict(example_protos, example_weights)
      variables = make_variable_dict(1, 1)
      options = dict(symmetric_l2_regularization=1,
                     symmetric_l1_regularization=0,
                     loss_type='logistic_loss')

      lr = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      unregularized_loss = lr.unregularized_loss(examples)
      loss = lr.regularized_loss(examples)
      predictions = lr.predictions(examples)
      train_op = lr.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()

      self.assertAllClose(0.284860, unregularized_loss.eval(), atol=0.08)
      self.assertAllClose(0.408044, loss.eval(), atol=0.012)
      predicted_labels = get_binary_predictions_for_logistic(predictions)
      self.assertAllEqual([0, 1], predicted_labels.eval())
      self.assertAllClose(0.01,
                          lr.approximate_duality_gap().eval(),
                          rtol=1e-2,
                          atol=1e-2)

  def testInstancesOfOneClassOnly(self):
    # Setup test data with 1 positive (ignored), and 1 negative example.
    example_protos = [
        make_example_proto(
            {'age': [0],
             'gender': [0]}, 0),
        make_example_proto(
            {'age': [1],
             'gender': [0]}, 1),  # Shares gender with the instance above.
    ]
    example_weights = [1.0, 0.0]  # Second example "omitted" from training.
    with self._single_threaded_test_session():
      examples = make_example_dict(example_protos, example_weights)
      variables = make_variable_dict(1, 1)
      options = dict(symmetric_l2_regularization=1,
                     symmetric_l1_regularization=0,
                     loss_type='logistic_loss')

      lr = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      unregularized_loss = lr.unregularized_loss(examples)
      loss = lr.regularized_loss(examples)
      predictions = lr.predictions(examples)
      train_op = lr.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()
      self.assertAllClose(0.411608, unregularized_loss.eval(), atol=0.05)
      self.assertAllClose(0.525457, loss.eval(), atol=0.01)
      predicted_labels = get_binary_predictions_for_logistic(predictions)
      self.assertAllEqual([0, 0], predicted_labels.eval())
      self.assertAllClose(0.01,
                          lr.approximate_duality_gap().eval(),
                          rtol=1e-2,
                          atol=1e-2)

  # TODO(katsiaspis): add a test for the case when examples at the end of an
  # epoch are repeated, since example id may be duplicated.

class SdcaWithLinearLossTest(SdcaOptimizerTest):
  """SDCA optimizer test class for linear (squared) loss."""

  def testSimple(self):
    # Setup test data
    example_protos = [
        make_example_proto(
            {'age': [0],
             'gender': [0]}, -10.0),
        make_example_proto(
            {'age': [1],
             'gender': [1]}, 14.0),
    ]
    example_weights = [1.0, 1.0]
    with self._single_threaded_test_session():
      examples = make_example_dict(example_protos, example_weights)
      variables = make_variable_dict(1, 1)
      options = dict(symmetric_l2_regularization=1,
                     symmetric_l1_regularization=0,
                     loss_type='squared_loss')

      lr = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      predictions = lr.predictions(examples)
      train_op = lr.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()

      # Predictions should be 2/3 of label due to minimizing regularized loss:
      #   (label - 2 * weight)^2 / 2 + L2 * 2 * weight^2
      self.assertAllClose([-20.0 / 3.0, 28.0 / 3.0],
                          predictions.eval(),
                          rtol=0.005)
      self.assertAllClose(0.01,
                          lr.approximate_duality_gap().eval(),
                          rtol=1e-2,
                          atol=1e-2)

  def testL2Regularization(self):
    # Setup test data
    example_protos = [
        # 2 identical examples
        make_example_proto(
            {'age': [0],
             'gender': [0]}, -10.0),
        make_example_proto(
            {'age': [0],
             'gender': [0]}, -10.0),
        # 2 more identical examples
        make_example_proto(
            {'age': [1],
             'gender': [1]}, 14.0),
        make_example_proto(
            {'age': [1],
             'gender': [1]}, 14.0),
    ]
    example_weights = [1.0, 1.0, 1.0, 1.0]
    with self._single_threaded_test_session():
      examples = make_example_dict(example_protos, example_weights)
      variables = make_variable_dict(1, 1)
      options = dict(symmetric_l2_regularization=16,
                     symmetric_l1_regularization=0,
                     loss_type='squared_loss')

      lr = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      predictions = lr.predictions(examples)

      train_op = lr.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()

      # Predictions should be 1/5 of label due to minimizing regularized loss:
      #   (label - 2 * weight)^2 + L2 * 16 * weight^2
      optimal1 = -10.0 / 5.0
      optimal2 = 14.0 / 5.0
      self.assertAllClose(
          [optimal1, optimal1, optimal2, optimal2],
          predictions.eval(),
          rtol=0.01)

  def testL1Regularization(self):
    # Setup test data
    example_protos = [
        make_example_proto(
            {'age': [0],
             'gender': [0]}, -10.0),
        make_example_proto(
            {'age': [1],
             'gender': [1]}, 14.0),
    ]
    example_weights = [1.0, 1.0]
    with self._single_threaded_test_session():
      examples = make_example_dict(example_protos, example_weights)
      variables = make_variable_dict(1, 1)
      options = dict(symmetric_l2_regularization=1.0,
                     symmetric_l1_regularization=4.0,
                     loss_type='squared_loss')
      lr = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      prediction = lr.predictions(examples)
      loss = lr.regularized_loss(examples)

      train_op = lr.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()

      # Predictions should be -4.0, 48/5 due to minimizing regularized loss:
      #   (label - 2 * weight)^2 / 2 + L2 * 2 * weight^2 + L1 * 4 * weight
      self.assertAllClose([-4.0, 20.0 / 3.0], prediction.eval(), rtol=0.08)

      # Loss should be the sum of the regularized loss value from above per
      # example after plugging in the optimal weights.
      self.assertAllClose(308.0 / 6.0, loss.eval(), atol=0.01)

  def testFeatureValues(self):
    # Setup test data
    example_protos = [
        make_example_proto(
            {'age': [0],
             'gender': [0]}, -10.0, -2.0),
        make_example_proto(
            {'age': [1],
             'gender': [1]}, 14.0, 2.0),
    ]
    example_weights = [1.0, 1.0]
    with self._single_threaded_test_session():
      examples = make_example_dict(example_protos, example_weights)

      variables = make_variable_dict(1, 1)
      options = dict(symmetric_l2_regularization=1,
                     symmetric_l1_regularization=0,
                     loss_type='squared_loss')

      lr = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      predictions = lr.predictions(examples)

      train_op = lr.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()

      # Predictions should be 8/9 of label due to minimizing regularized loss:
      #   (label - 2 * 2 * weight)^2 / 2 + L2 * 2 * weight^2
      self.assertAllClose([-10.0 * 8 / 9, 14.0 * 8 / 9],
                          predictions.eval(),
                          rtol=0.07)

  def testDenseFeatures(self):
    with self._single_threaded_test_session():
      examples = make_dense_examples_dict(
          dense_feature_values=[[-2.0, 0.0], [0.0, 2.0]],
          weights=[1.0, 1.0],
          labels=[-10.0, 14.0])
      variables = make_dense_variable_dict(2, 2)
      options = dict(symmetric_l2_regularization=1,
                     symmetric_l1_regularization=0,
                     loss_type='squared_loss')
      lr = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      predictions = lr.predictions(examples)

      train_op = lr.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()

      # Predictions should be 4/5 of label due to minimizing regularized loss:
      #   (label - 2 * weight)^2 / 2 + L2 * weight^2
      self.assertAllClose([-10.0 * 4 / 5, 14.0 * 4 / 5],
                          predictions.eval(),
                          rtol=0.01)

      loss = lr.regularized_loss(examples)
      self.assertAllClose(148.0 / 10.0, loss.eval(), atol=0.01)

class SdcaWithHingeLossTest(SdcaOptimizerTest):
  """SDCA optimizer test class for hinge loss."""

  def testSimple(self):
    # Setup test data
    example_protos = [
        make_example_proto(
            {'age': [0],
             'gender': [0]}, 0),
        make_example_proto(
            {'age': [1],
             'gender': [1]}, 1),
    ]
    example_weights = [1.0, 1.0]
    with self.test_session(use_gpu=False):
      examples = make_example_dict(example_protos, example_weights)
      variables = make_variable_dict(1, 1)
      options = dict(symmetric_l2_regularization=1.0,
                     symmetric_l1_regularization=0,
                     loss_type='hinge_loss')
      model = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()

      # Before minimization, the weights default to zero. There is no loss due
      # to regularization, only unregularized loss which is 0.5 * (1+1) = 1.0.
      predictions = model.predictions(examples)
      self.assertAllClose([0.0, 0.0], predictions.eval())
      unregularized_loss = model.unregularized_loss(examples)
      regularized_loss = model.regularized_loss(examples)
      self.assertAllClose(1.0, unregularized_loss.eval())
      self.assertAllClose(1.0, regularized_loss.eval())

      # After minimization, the model separates perfectly the data points. There
      # are 4 sparse weights: 2 for age (say w1, w2) and 2 for gender (say w3
      # and w4). Solving the system w1 + w3 = 1.0, w2 + w4 = -1.0 and minimizing
      # wrt to \|\vec{w}\|_2, gives w1=w3=1/2 and w2=w4=-1/2. This gives 0.0
      # unregularized loss and 0.25 L2 loss.
      train_op = model.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()

      binary_predictions = get_binary_predictions_for_hinge(predictions)
      self.assertAllEqual([-1.0, 1.0], predictions.eval())
      self.assertAllEqual([0, 1], binary_predictions.eval())
      self.assertAllClose(0.0, unregularized_loss.eval())
      self.assertAllClose(0.25, regularized_loss.eval(), atol=0.05)

  def testDenseFeaturesPerfectlySeparable(self):
    with self._single_threaded_test_session():
      examples = make_dense_examples_dict(
          dense_feature_values=[[1.0, 1.0], [1.0, -1.0]],
          weights=[1.0, 1.0],
          labels=[1.0, 0.0])
      variables = make_dense_variable_dict(2, 2)
      options = dict(symmetric_l2_regularization=1.0,
                     symmetric_l1_regularization=0,
                     loss_type='hinge_loss')
      model = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      predictions = model.predictions(examples)
      binary_predictions = get_binary_predictions_for_hinge(predictions)

      train_op = model.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()

      self.assertAllClose([1.0, -1.0], predictions.eval(), atol=0.05)
      self.assertAllEqual([1, 0], binary_predictions.eval())

      # (1.0, 1.0) and (1.0, -1.0) are perfectly separable by x-axis (that is,
      # the SVM's functional margin >=1), so the unregularized loss is ~0.0.
      # There is only loss due to l2-regularization. For these datapoints, it
      # turns out that w_1~=0.0 and w_2~=1.0 which means that l2 loss is ~0.25.
      unregularized_loss = model.unregularized_loss(examples)
      regularized_loss = model.regularized_loss(examples)
      self.assertAllClose(0.0, unregularized_loss.eval(), atol=0.02)
      self.assertAllClose(0.25, regularized_loss.eval(), atol=0.02)

  def testDenseFeaturesSeparableWithinMargins(self):
    with self._single_threaded_test_session():
      examples = make_dense_examples_dict(
          dense_feature_values=[[1.0, 1.0], [0.5, -0.5]],
          weights=[1.0, 1.0],
          labels=[1.0, 0.0])
      variables = make_dense_variable_dict(2, 2)
      options = dict(symmetric_l2_regularization=1.0,
                     symmetric_l1_regularization=0,
                     loss_type='hinge_loss')
      model = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      predictions = model.predictions(examples)
      binary_predictions = get_binary_predictions_for_hinge(predictions)

      train_op = model.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()

      # (1.0, 0.5) and (1.0, -0.5) are separable by x-axis but the datapoints
      # are within the margins so there is unregularized loss (1/2 per example).
      # For these datapoints, optimal weights are w_1~=0.0 and w_2~=1.0 which
      # gives an L2 loss of ~0.25.
      self.assertAllClose([0.5, -0.5], predictions.eval(), rtol=0.05)
      self.assertAllEqual([1, 0], binary_predictions.eval())
      unregularized_loss = model.unregularized_loss(examples)
      regularized_loss = model.regularized_loss(examples)
      self.assertAllClose(0.5, unregularized_loss.eval(), atol=0.02)
      self.assertAllClose(0.75, regularized_loss.eval(), atol=0.02)

  def testDenseFeaturesWeightedExamples(self):
    with self._single_threaded_test_session():
      examples = make_dense_examples_dict(
          dense_feature_values=[[1.0, 1.0], [0.5, -0.5]],
          weights=[3.0, 1.0],
          labels=[1.0, 0.0])
      variables = make_dense_variable_dict(2, 2)
      options = dict(symmetric_l2_regularization=1.0,
                     symmetric_l1_regularization=0,
                     loss_type='hinge_loss')
      model = SdcaModel(CONTAINER, examples, variables, options)
      tf.initialize_all_variables().run()
      predictions = model.predictions(examples)
      binary_predictions = get_binary_predictions_for_hinge(predictions)
      train_op = model.minimize()
      for _ in xrange(_MAX_ITERATIONS):
        train_op.run()

      # Point (1.0, 0.5) has higher weight than (1.0, -0.5) so the model will
      # try to increase the margin from (1.0, 0.5). Due to regularization,
      # (1.0, -0.5) will be within the margin. For these points and example
      # weights, the optimal weights are w_1~=0.4 and w_2~=1.2 which give an L2
      # loss of 0.5 * 0.25 * 0.25 * 1.6 = 0.2. The binary predictions will be
      # correct, but the boundary will be much closer to the 2nd point than the
      # first one.
      self.assertAllClose([1.0, -0.2], predictions.eval(), atol=0.05)
      self.assertAllEqual([1, 0], binary_predictions.eval())
      unregularized_loss = model.unregularized_loss(examples)
      regularized_loss = model.regularized_loss(examples)
      self.assertAllClose(0.2, unregularized_loss.eval(), atol=0.02)
      self.assertAllClose(0.4, regularized_loss.eval(), atol=0.02)

if __name__ == '__main__':
  googletest.main()


from pyexpat import model
from django import forms
from django.db.models import fields
from .models import quizquestion, quiztopics, useranswers, users
from django.core import validators

class AddQuiz(forms.ModelForm):
    class Meta:
        model = quiztopics
        fields = ['quiz_cat','quiz_title','quiz_desc','quiz_url','quiz_thumb']

class AddQuestion(forms.ModelForm):
    class Meta:
        model = quizquestion
        fields = ['qn_cat','qn_title','qn_name','qn_opt1','qn_opt2','qn_opt3','qn_opt4','qn_ans']

class UserAnswer(forms.ModelForm):
    class Meta:
        model = useranswers
        fields = [
            'full_name',
            'username',
            'email_id',
            'qn_cat',
            'qn_name',
            'qn_ans',
        ]


import os
import csv
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC

count_record = 631
host = 'https://shopee.co.id'
DRIVER_PATH = '/driver/chromedriver'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)

# go to login page and scan qr
driver.get('https://shopee.co.id/buyer/login')
WebDriverWait(driver, 100).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')

time.sleep(10)
driver.get('https://shopee.co.id/mall/brands/11042849')

WebDriverWait(driver, 100).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')
total_alphabet_toko = 3 # skip for unexpected problem
# total_alphabet_toko = len(driver.find_elements(By.XPATH, '//*[@id="main"]/div/div[2]/div/div/div/div[3]/div'))
print(total_alphabet_toko)

while total_alphabet_toko >= 1:
    link = []

    chat_outside = []
    follower_outside = []
    rating_outside = []
    bergabung_outside = []
    nama_toko_outside = []
    jml_produk_outside = []
    diskon_inside = []
    product_href = []
    nama_produk_outside = []
    diskon_outside = []
    murah_outside = []
    harga_awal_outside = []
    harga_akhir_outside = []
    terjual_inside = []
    lokasi_inside = []
    gratis_ongkir_inside = []
    favorit_inside = []
    total_sub_toko = len(driver.find_elements(By.XPATH, f'//*[@id="main"]/div/div[2]/div/div/div/div[3]/div[{total_alphabet_toko}]/div/div[2]/div'))

    while total_sub_toko >= 1:
        link.append(driver.find_element(by=By.XPATH, value=f'//*[@id="main"]/div/div[2]/div/div/div/div[3]/div[{total_alphabet_toko}]/div/div[2]/div[{total_sub_toko}]/a').get_attribute('href'))
        total_sub_toko -= 1

    for i, toko in enumerate(link):
        driver.execute_script("window.open('');")
        driver.switch_to.window(driver.window_handles[i+1])
        driver.get(toko)

    skip_tab = []
    for i, toko in enumerate(link):
        driver.switch_to.window(driver.window_handles[i+1])

        # link_produk field
        WebDriverWait(driver, 100).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')

        try:
            nama_toko_outside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div/div[1]/div/div[1]/div[3]/div[1]/div/h1').get_attribute("innerText"))
        except NoSuchElementException:
            nama_toko_outside.append('')

        try:
            jml_produk_outside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div/div[1]/div/div[2]/div[1]/div[2]/div[2]').get_attribute("innerText"))
        except NoSuchElementException:
            jml_produk_outside.append('')

        try:
            chat_outside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div/div[1]/div/div[2]/div[3]/div[2]/div[2]').get_attribute("innerText"))
        except NoSuchElementException:
            chat_outside.append('')

        try:
            follower_outside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div/div[1]/div/div[2]/div[4]/div[2]/div[2]').get_attribute("innerText"))
        except NoSuchElementException:
            follower_outside.append('')

        try:
            rating_outside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div/div[1]/div/div[2]/div[5]/div[2]/div[2]').get_attribute("innerText"))
        except NoSuchElementException:
            rating_outside.append('')

        try:
            bergabung_outside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div/div[1]/div/div[2]/div[6]/div[2]/div[2]').get_attribute("innerText"))
        except NoSuchElementException:
            bergabung_outside.append('')

        try:
            nama_produk_outside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div/div[3]/div[2]/div[2]/div/div[2]/div/div[1]/a/div/div/div[2]/div[1]/div[1]/div').get_attribute("innerText"))
        except NoSuchElementException:
            nama_produk_outside.append('')

        try:
            diskon_outside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div/div[3]/div[2]/div[2]/div/div[2]/div/div[1]/a/div/div/div[2]/div[1]/div[2]/div[1]/div').get_attribute("innerText"))
        except NoSuchElementException:
            diskon_outside.append('')

        try:
            murah_outside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div/div[3]/div[2]/div[2]/div/div[2]/div/div[1]/a/div/div/div[2]/div[1]/div[2]/div[2]').get_attribute("innerText"))
        except NoSuchElementException:
            murah_outside.append('')

        try:
            harga_awal_outside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div/div[3]/div[2]/div[2]/div/div[2]/div/div[1]/a/div/div/div[2]/div[2]/div[1]').get_attribute("innerText"))
        except NoSuchElementException:
            harga_awal_outside.append('')

        try:
            harga_akhir_outside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div/div[3]/div[2]/div[2]/div/div[2]/div/div[1]/a/div/div/div[2]/div[2]/div[2]').get_attribute("innerText"))
        except NoSuchElementException:
            harga_akhir_outside.append('')

        # click product
        not_found = True
        while not_found:
            try:
                # check if all product sold
                if driver.find_elements(By.XPATH, value='/html/body/div[1]/div/div[2]/div/div/div/div[2]/div/div[3]/div[2]/div[2]/div[2]/div/div[2]/div[1]/div[1]/a/div/div/div[1]/div/div[1]/div/div'):
                    skip_tab.append(i)
                    not_found = False
                    break

                harga_akhir_outside.append('')
                product = driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div/div[3]/div[2]/div[2]/div/div[2]/div/div[1]/a').get_attribute('href')
                product_href.append(product)
                driver.get(product)
                not_found = False

            except NoSuchElementException:
                not_found = True

    for i, toko in enumerate(link):
        if i in skip_tab:
            break

        driver.switch_to.window(driver.window_handles[1])

        # check if page need login refresh
        # timer_login = 2
        # login_page = True
        # while login_page:
        #     try:
        #         WebDriverWait(driver, 100).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')
        #         if driver.find_elements(By.XPATH, value='/html/body/div[1]/div/div[2]/div/div/div/div[2]/form/div/div[2]/div[2]/div[1]/input'):
        #             driver.get(product_href[i])
        #             time.sleep(timer_login)
        #             timer_login += 1
        #         else:
        #             login_page = False
        #     except NoSuchElementException:
        #         print('false element')

        WebDriverWait(driver, 100).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')
        try:
            diskon_inside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div[1]/div/div[1]/div[2]/div[1]/div[3]/div/div[3]/div/div/div[1]/div/div[2]/div[2]').get_attribute("innerText"))
        except NoSuchElementException:
            diskon_inside.append('')

        try:
            terjual_inside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div[1]/div/div/div/div[1]/div[3]/div/div[2]/div[3]/div[1]').get_attribute("innerText"))
        except NoSuchElementException:
            terjual_inside.append('')

        try:
            lokasi_inside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div[1]/div/div[1]/div[2]/div[2]/div[2]/div[1]/div[1]/div[1]/div[2]/div[5]/div').get_attribute("innerText"))
        except NoSuchElementException:
            lokasi_inside.append('')

        try:
            gratis_ongkir_inside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div[1]/div/div/div/div[1]/div[3]/div/div[4]/div/div[3]/div/div[1]/div[2]').get_attribute("innerText"))
        except NoSuchElementException:
            gratis_ongkir_inside.append('')

        try:
            favorit_inside.append(driver.find_element(by=By.XPATH, value=f'/html/body/div[1]/div/div[2]/div[1]/div/div/div/div[1]/div[2]/div[2]/div[2]/button/div').get_attribute("innerText"))
        except NoSuchElementException:
            favorit_inside.append('')

        ## mapping csv
        count_record += 1
        cur_time = time.time()
        order = '{0}-{1}'.format(str(cur_time).split('.')[0], count_record)
        start_url = 'https://shopee.co.id/mall/brands/11042849'
        link_toko = toko.replace('https://shopee.co.id/','')
        link_toko_href = toko
        nama_toko = nama_toko_outside[i]
        jml_produk = jml_produk_outside[i]
        chat = chat_outside[i]
        follower = follower_outside[i]
        rating = rating_outside[i]
        bergabung = bergabung_outside[i]
        # promo = '' #hide
        alamat_toko = lokasi_inside[i]
        link_produk = f'{diskon_inside[i]}{nama_produk_outside[i]}{diskon_outside[i]}{murah_outside[i]}{harga_awal_outside[i]}{harga_akhir_outside[i]}{terjual_inside[i]}{lokasi_inside[i]}'
        link_produk_href = product_href[i]
        nama_produk = nama_produk_outside[i]
        promo_produk = diskon_outside[i]
        terjual = terjual_inside[i]
        gratis_ongkir = gratis_ongkir_inside[i].replace('Gratis Ongkir dengan', '')
        favorit = favorit_inside[i]

        with open('export/master.csv', 'a+', newline='', encoding="utf-8") as file:
            writer = csv.writer(file)
            # writer.writerow(['web-scraper-order','web-scraper-start-url','link_toko','link_toko-href','nama_toko','Jml_produk','chat','follower','rating_toko','bergabung','promo','alamat_toko','link_produk','link_produk-href','nama_produk','promo_produk','terjual','gratis_ongkir','favorit'])
            writer.writerow([order, start_url, link_toko, link_toko_href, nama_toko, jml_produk, chat, follower, rating, bergabung, alamat_toko, link_produk, link_produk_href, nama_produk, promo_produk, terjual, gratis_ongkir, favorit])

        print(f'{diskon_inside[i]}{nama_produk_outside[i]}{diskon_outside[i]}{murah_outside[i]}{harga_awal_outside[i]}{harga_akhir_outside[i]}{terjual_inside[i]}{lokasi_inside[i]}')
        driver.close()

    for i in skip_tab:
        driver.switch_to.window(driver.window_handles[1])
        driver.close()

    driver.switch_to.window(driver.window_handles[0])
    total_alphabet_toko -= 1

# shutdown pc after app done
os.system("shutdown /s /t 1")


#!/usr/bin/python
# SPDX-License-Identifier: GPL-2.0

import argparse
import sys

# Basic sanity check of perf CSV output as specified in the man page.
# Currently just checks the number of fields per line in output.

ap = argparse.ArgumentParser()
ap.add_argument('--no-args', action='store_true')
ap.add_argument('--interval', action='store_true')
ap.add_argument('--system-wide-no-aggr', action='store_true')
ap.add_argument('--system-wide', action='store_true')
ap.add_argument('--event', action='store_true')
ap.add_argument('--per-core', action='store_true')
ap.add_argument('--per-thread', action='store_true')
ap.add_argument('--per-die', action='store_true')
ap.add_argument('--per-node', action='store_true')
ap.add_argument('--per-socket', action='store_true')
ap.add_argument('--separator', default=',', nargs='?')
args = ap.parse_args()

Lines = sys.stdin.readlines()

def check_csv_output(exp):
  for line in Lines:
    if 'failed' not in line:
      count = line.count(args.separator)
      if count != exp:
        sys.stdout.write(''.join(Lines))
        raise RuntimeError(f'wrong number of fields. expected {exp} in {line}')

try:
  if args.no_args or args.system_wide or args.event:
    expected_items = 6
  elif args.interval or args.per_thread or args.system_wide_no_aggr:
    expected_items = 7
  elif args.per_core or args.per_socket or args.per_node or args.per_die:
    expected_items = 8
  else:
    ap.print_help()
    raise RuntimeError('No checking option specified')
  check_csv_output(expected_items)

except:
  sys.stdout.write('Test failed for input: ' + ''.join(Lines))
  raise


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
from tbw_item import tbw_item
from detail_dialog import detail_dialog
from ui_dialog_1 import Dialog_1
import xlwt

class TableWidgetSearch:
    def __init__(self, widget):
        self.datas = []    #包含了表格的各行，类型为tbw_item。
        self.selected_year = []
        self.widget = widget
        self.contain = []
        self.modify_for_row = []
        self.detail_for_row = []

    def view_detail(self, id):
        dialog_detail = detail_dialog(id = id, item = self.datas[id])
        name = self.datas[id].realname
        dialog_detail.setTitle(name)
        dialog_detail.setInfo("Here are some information.")
        dialog_detail.exec_()

    def buttonForRow(self, id):
        """
        设置表格中每一行的两个按钮
        :param id: 行号
        """
        widget = QWidget()
        detailBtn = QPushButton("查看详情")
        detailBtn.setStyleSheet("color: rgb(255, 255, 255); background-color: rgb(75, 225, 75); font-family:'微软雅黑'; font-Point Size:10")
        detailBtn.clicked.connect(lambda:self.view_detail(id))
        self.detail_for_row.append(detailBtn)

        modifyBtn = QPushButton("修正")
        modifyBtn.setStyleSheet("color: rgb(255, 255, 255); background-color: rgb(225, 174, 0); font-family:'微软雅黑'; font-Point Size:10")
        self.modify_for_row.append(modifyBtn)
        modifyBtn.clicked.connect(lambda:self.modify(id))
        hLayout = QHBoxLayout()
        hLayout.addWidget(detailBtn)
        hLayout.addWidget(modifyBtn)
        hLayout.setContentsMargins(5,2,5,2)
        widget.setLayout(hLayout)

        return widget

    def clear(self):
        self.widget.clear()
        self.datas = []
        self.selected_year = []
        self.widget.setRowCount(0)
        self.widget.setColumnCount(0)

    def add_data(self, datas, selected_years):
        self.datas = datas
        self.selected_year = selected_years
        for i in range(len(self.datas)):
            datas[i].setRow(i)
            datas[i].setRealName()

    def add_data_dialog(self, data):
        flag = -1
        for index, d in enumerate(self.datas):
            if d['编码'] == data[0] and d['地域'] == data[1]:
                flag = index

        if flag >= 0:
            # 6个级别 从第2列开始
            for cat_index in range(2, 8):
                self.datas[flag]['级别%d' % (cat_index - 1)] = data[cat_index]
            # 从第8列开始
            for year_index in range(len(self.selected_year)):
                value = data[8+year_index]
                self.datas[flag][self.selected_year[year_index]] = value
        else:
            temp = {}
            for i in range(len(self.header)):
                temp[self.header[i]] = data[i]
            self.datas.append(temp)

    def delete_data(self, index, region):
        for i in range(len(self.datas)):
            if self.datas[i]['编码'] == index and self.datas[i]['地域'] == region:
                del self.datas[i]
                break

    def display(self):
        """
        显示表格内容
        """
        self.widget.clear()
        self.widget.setRowCount(len(self.datas))
        self.widget.setColumnCount(2)
        self.header = ['路径','操作']
        self.widget.setHorizontalHeaderLabels(self.header)
        self.widget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)

        row = 0
        col = 0
        for point in self.datas:
            item = QtWidgets.QTableWidgetItem(point.realname)
            item.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
            self.widget.setItem(row, col, item)
            row += 1

    def export_excel(self, filepath):
        workbook = xlwt.Workbook(encoding='utf-8')
        worksheet = workbook.add_sheet('Data')

        style_hv = xlwt.XFStyle()
        font = xlwt.Font()
        font.name = '微软雅黑'

        alignment_hv = xlwt.Alignment()
        alignment_hv.horz = xlwt.Alignment.HORZ_CENTER
        alignment_hv.vert = xlwt.Alignment.VERT_CENTER

        style_hv.alignment = alignment_hv
        style_hv.font = font

        worksheet.write(0, 0, '编码', style_hv)
        worksheet.write(0, 1, '地域', style_hv)
        for i in range(1, 7):
            worksheet.write(0, 1+i, '级别%d' % i, style_hv)
        for year_index in range(len(self.selected_year)):
            worksheet.write(0, 8+year_index, self.selected_year[year_index], style_hv)

        # sum = 0
        # for item in self.datas:
        #     sum += len(item.data)
        # print(sum)

        row = 1
        for item in self.datas:
            for record in item.data:
                col = 0
                for key in record.keys():
                    if key == 'time':
                        continue
                    if key in ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019']:
                        # print(key)
                        if key in self.selected_year:
                            data = record[key]
                        else:
                            continue
                    else:
                        data = record[key]
                    try:
                        data = float(data)
                    except ValueError:
                        pass
                    worksheet.write(row, col, data, style_hv)
                    col += 1
                row += 1
        # for row in range(sum):
        # # for row in range(len(self.datas)):
        #     for col in range(len(self.datas[0].data[0].keys())-1):
        #         data = self.widget.item(row, col).text()
        #         try:
        #             data = float(data)
        #         except ValueError:
        #             pass
        #         worksheet.write(row+1, col, data, style_hv)

        worksheet.col(0).width = 3000
        worksheet.col(1).width = 3000
        for col in range(2, 8):
            worksheet.col(col).width = 6200
        for col in range(8, 8 + len(self.selected_year) + 1):
        # for col in range(8, len(self.datas[0].keys())-1):
            worksheet.col(col).width = 3000
        workbook.save(filepath)

    def refresh(self, id):
        """
        刷新表格内容
        """
        item = QtWidgets.QTableWidgetItem(self.datas[id].realname)
        item.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
        self.widget.setItem(id, 0, item)


"""
Display BoundingBoxes on image.
"""
import cv2
import numpy as np

BBOX_COLOR = (0, 255, 0)
BBOX_THICKNESS = 2

def plot_box(
    boxes: list,
    image: np.ndarray,
    waittime: int = 0,
    save_img: bool = False,
    path: str = "plot_imgs",
    quiet_output: bool = False,
) -> None:
    """
    Display BoundingBoxes on image.

    Parameters
    ----------
    boxes: list[BoundingBox]
        List of bounding boxes to plot on image.
    image: np.ndarray
        image to detect obstacles in
    waittime: int, default=0
        cv2.waitKey parameter, number of seconds to show window 0=inf.
    save_img: bool
        Whether to save the image with the boxes plotted.
    path: str
        Where to save image to if save_img is True. Should include filename and extension.
    """
    if not isinstance(boxes, list):
        raise ValueError(f"Expected list of BoundingBox, got {type(boxes)} instead")
    if not isinstance(image, np.ndarray):
        raise ValueError(
            f"Expected argument of type ObstacleFinder, got {type(image)} instead"
        )

    output = np.copy(image)  # Important so dont have to copy before passing here
    for bbox in boxes:
        if not bbox.vertices:
            continue
        # print(bbox.vertices)
        X, Y = [], []
        for pair in bbox.vertices:
            x, y = pair[0], pair[1]

            x, y = int(x), int(y)

            X.append(x)
            Y.append(y)

        cv2.rectangle(
            output, (min(X), min(Y)), (max(X), max(Y)), BBOX_COLOR, BBOX_THICKNESS
        )

        if not quiet_output:
            print("\tKeypoint:", bbox.pt[0], bbox.pt[1])

        # visualize keypoints
        # output = cv2.drawKeypoints(image, keypoints, outImage=np.array([]), color=(255, 0, 255), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

    if not save_img:
        cv2.imshow("Obstacles", output)
        cv2.waitKey(waittime)
    else:
        cv2.imwrite(filename=path, img=output)


"""
This module holds the keypress control system class, Fovea setup, and linalg methods
used to transform disc data for the PCA user story.
"""

from __future__ import absolute_import, print_function

from PyDSTool.Toolbox import data_analysis as da

import numpy as np
import matplotlib.pyplot as plt

from PyDSTool import *
import retina.core.axes
import random

def translate(X, axis, amount):
    A = np.zeros((len(X), len(X[0])))
    A[:,axis] = np.ones(len(X))*amount
    Y = X + A
    return Y

def rotate_x(X, theta):
    R = np.array([[1, 0, 0], [0, np.cos(theta), -np.sin(theta)], [0, np.sin(theta), np.cos(theta)]])
    Y = np.dot(R, X.transpose())
    return Y.transpose()

def rotate_y(X, theta):
    R = np.array([[np.cos(theta), 0, -np.sin(theta)], [0, 1, 0], [np.sin(theta), 0, np.cos(theta)]])
    Y = np.dot(R, X.transpose())
    return Y.transpose()

def rotate_z(X, theta):
    R = np.array([[np.cos(theta), np.sin(theta), 0], [-np.sin(theta), np.cos(theta), 0], [0, 0, 1]])
    Y = np.dot(R, X.transpose())
    return Y.transpose()

def stretch(X, axis, amount):
    X[:,axis] = X[:,axis]*amount
    return X

def noise(X, axis, percent, loc, scale):
    for i in range(0, int(round(len(X)*percent))):
        X[i][axis] = np.random.normal(loc, scale, 1)
    return X

def ortho_proj_mat(n, m):
    Z = npy.random.rand(n, m)
    Q, R = npy.linalg.qr(Z)
    return Q

def compute(X, new_dim, layer, style, before, after, variance, proj_vecsLO = None, proj_vecsHI = None):

    data_dict = {}
    data_dict['X'] = X

    p = da.doPCA(X, len(X[0]), len(X[0])) #Creates a pcaNode object.

    pcMat = p.get_projmatrix()

    pcPts = np.concatenate(([pcMat.transpose()[0,:]*15],
                            [-pcMat.transpose()[0,:]*15]), axis=0)

    for j in range(1, new_dim):
        pcPts = np.concatenate((pcPts, [pcMat.transpose()[j,:]*15],
                                [-pcMat.transpose()[j,:]*15]), axis=0)

    Y = p._execute(X, new_dim) #Get dimensionality reduced data.
    data_dict['Y'] = Y

    #Produce proj vecs if none supplied
    if proj_vecsHI is None:
        proj_vecsHI = ortho_proj_mat(len(X[0]), 3)

    if proj_vecsLO is None:
        proj_vecsLO = ortho_proj_mat(len(Y[0]), 2)

    #If data are high-dimensional, use the projection vectors.
    if len(X[0]) > 3:
        X = npy.dot(X, proj_vecsHI)
        pcPts = npy.dot(pcPts, proj_vecsHI)
        data_dict['X_projected'] = X

    if len(Y[0]) > 2:
        proj_vecsLO = proj_vecsLO[0:new_dim, :] #Scrape the needed rows from projection matrix, so we can page between dimensionalities.
        Y = npy.dot(Y, proj_vecsLO)
        data_dict['Y_projected'] = Y

    #Create line plot for variance explained by each component.
    layer_obj = variance.get_layer(layer)
    layer_obj.add_data(range(1, len(p.d)+1), p.d/sum(p.d))
    layer_obj.set_style(style + "-o")

    #Create plot of high-dimensional data and its PC's.
    layer_obj = before.get_layer(layer)
    layer_obj.add_data(X[:,0], X[:,1], X[:,2])
    layer_obj.set_style(style + '.')

    for j in range(0, len(pcPts), 2):
        before.add_layer(layer + " PC")
        layer_obj = before.get_layer(layer + " PC")
        layer_obj.add_data(pcPts[j+0:j+2,0], pcPts[j+0:j+2,1], pcPts[j+0:j+2,2])
        layer_obj.add_data(pcPts[j+2:j+4,0], pcPts[j+2:j+4,1], pcPts[j+2:j+4,2])
        layer_obj.set_style(style)

    #Create plot of low-dimensional data.

    layer_obj = after.get_layer(layer)
    layer_obj.add_data(Y[:,0], Y[:,1])
    layer_obj.set_style(style + ".")

    print("Variance Explained in", layer,"with first",new_dim,"components:")
    print(sum(p.d[0:new_dim])/sum(p.d))

    before.build_layers()
    after.build_layers()
    variance.build_layers()

    return data_dict

def setupDisplay(clus_layers, clus_styles, DOI):
    fig = plt.figure(figsize=(14,6))
    fig.suptitle('PCA Disc')
    fig.canvas.set_window_title('Master')

    before = plt.subplot('131', projection='Fovea3D')
    before.set_title('Before')
    after = plt.subplot('132', projection='Fovea2D')
    after.set_title('After')
    variance = plt.subplot('133', projection='Fovea2D')
    variance.set_title('Variance by Components')
    subplots = [before, after, variance]

    #Setup all layers
    before.add_layer('orig_data')
    before.add_layer('meta_data', kind='text')

    for clus in clus_layers:
        before.add_layer(clus)
        after.add_layer(clus)
        variance.add_layer(clus)

    for subplot in subplots:
        subplot.build_layers()

    return fig, subplots

proj_thresh = 20

class ControlSys(object):
    def __init__(self, fig, data, clus_layers, clus_styles, d, before, after, variance, proj_vecsLO=None, proj_vecsHI=None):
        self.fig = fig
        self.before = before
        self.after = after
        self.variance = variance
        self.data = data
        self.data_dict = None
        self.proj_thresh = 4
        self.clus_layers = clus_layers
        self.clus_styles = clus_styles
        self.proj_vecsLO = proj_vecsLO
        self.proj_vecsHI = proj_vecsHI
        self.d = d
        self.c = 0
        self.m = False
        self.fig.canvas.mpl_connect('key_press_event', self.keypress)

        for i in range(len(clus_layers)):
            self.data_dict = compute(data[i], d, clus_layers[i], clus_styles[i], self.before, self.after, self.variance, proj_vecsLO, proj_vecsHI)

        self.highlight_eigens(self.variance)

        #Initialize Bombardier callbacks on 2D subplot.
        #gui.current_domain_handler.assign_criterion_func(self.get_projection_distance)
        #gui.assign_user_func(self.get_displacements)

        #User tips
        print("Press left or right arrow keys to view different rotations of Hi-D data and their PC's.")
        print("Press m to display or hide all layers.")
        print("Press h to show or hide original data.")

    def highlight_eigens(self, variance):
        for i in range(len(self.clus_layers)):
            for j in range(1, self.d+1):
                layer_obj = variance.get_layer(self.clus_layers[i])
                layer_obj.add_vline(j)

    def get_projection_distance(self, pt_array, fsign=None):
        #Judge by nearest point rather than points inside. Otherwise, I would need to access the whole polygon,
        #which would be difficult.
        nearest_pt = []
        index = []

        try:
            pts = self.data_dict['Y_projected']
        except KeyError:
            pts = self.data_dict['Y']

        #Find closest projected point.
        dist = 10000000
        for i, row in enumerate(pts):
            if np.linalg.norm(pt_array - row) < dist:
                dist = np.linalg.norm(pt_array - row)
                nearest_pt = row
                index = i

        #Need more formal way to do this. Standard deviation? This doesn't seem to accomplish anything right now anyways.
        if dist > 2 and fsign is not None:
            return -fsign

        #Calculate distance between projected point and loD point.
        orig_pt = self.data_dict['Y'][i]
        proj_pt = np.append(nearest_pt, np.zeros(len(orig_pt)-len(nearest_pt)))
        if np.linalg.norm(orig_pt - proj_pt) > self.proj_thresh:
            return -1
        else:
            return 1

    def keypress(self, event):

        if event.key == 'left' or event.key == 'right':
            if event.key == 'left':
                self.c += 1
            else:
                self.c -= 1

            layer_obj = self.before.get_layer(self.clus_layers[self.c%len(self.clus_layers)])
            layer_obj.toggle_display()

        if event.key == 'm':
            self.m = not self.m
            for clus in self.clus_layers:
                layer_obj = self.before.get_layer(clus)
                if self.m:
                    layer_obj.show()
                else:
                    layer_obj.hide()

        if event.key == 'h':
            layer_obj = self.before.get_layer('orig_data')
            layer_obj.toggle_display()

        if self.proj_vecsHI is not None:
            if (event.key == 'up' and self.d is not len(self.proj_vecsHI)) or (event.key == 'down' and self.d is not 2):
                if event.key == 'up':
                    self.d += 1
                elif event.key == 'down':
                    self.d -= 1

                print("Attempting to display", self.d,"-dimensional data...")

                for i in range(len(self.clus_layers)):
                    self.data_dict = compute(self.data[i], self.d, self.clus_layers[i], self.clus_styles[i], self.before, self.after, self.variance, self.proj_vecsLO, self.proj_vecsHI)

                self.highlight_eigens(self.variance)

    def get_displacements(self, x, y):
        print("Last output = (mag dict, vector dict)")
        Fxs = []
        Fys = []
        Fs = []

        try:
            pts = self.data_dict['Y_projected']
        except KeyError:
            pts = self.data_dict['Y']

        ixs = range(len(pts))
        for i in ixs:
            Fx = x - pts[i][0]
            Fy = y - pts[i][1]
            Fxs.append(Fx)
            Fys.append(Fy)
            Fs.append(sqrt(Fx*Fx+Fy*Fy))
        return dict(zip(ixs, Fs)), dict(zip(ixs, zip(Fxs, Fys)))


'''Scorrer for the decision list'''

#Date: 22nd April 2021.
#Amit Raj

import re
import pandas as pd
import sys
import os
import bs4

def extract_line(file):
    with open(file, "r", encoding ='utf-8') as tags:
        dd = tags.read()
        dcode = str(dd).strip()
        dcode = dcode.replace('\n','')
    return dcode

def extract_data(data):
    instances, senses =[],[]
    for vv in bs4.BeautifulSoup(data,'html.parser'):
        instances.append(vv.attrs['instance'])
    for ss in bs4.BeautifulSoup(data, 'html.parser'):
        senses.append(ss.attrs['senseid'])
    return instances, senses

def accuracy(pred_file, gd_file):
	pod,ww = {}, {}

	for vv in bs4.BeautifulSoup(pred_file, "html.parser"):
	    vv.find_all("answer")
	    pod.update({vv.get("instance"):vv.get("senseid")})

	for sppd in bs4.BeautifulSoup(gd_file, "html.parser"):
	    ww.update({sppd.get("instance"):sppd.get("senseid")})

	mnv =0
	for keys1,keys2 in zip(pod.items(), ww.items()):
	    if (keys1 == keys2):
	        mnv+=1

	# print(mnv)
	# print(len(ww))

	accuracy = ((mnv/len(ww)) * 100)
	print("The accuracy of the decision list is :{0}".format(accuracy))
	return pod, ww

#     print 'Not', x_values, y_values
    #     if(list(pod.keys)[i] == list(ww.keys)[i]):
#         if(list(pod.value)[i] == list(ww.value)[i])
#             print("vv")

def confusion_matrix(pod, ww):
	prd_senses = (list(pod.items()))
	gld_senses = (list(ww.items()))

	prd_sens_list,gld_sense_list = [],[]

	for i in range(0, len(pod.keys())):
	    prd_sens_list.append((prd_senses[i][1]))

	for j in range(0, len(pod.keys())):
	    gld_sense_list.append((gld_senses[j][1]))

	# print(pd.Series(prd_sens_list))

	# print(pd.Series(gld_sense_list))

	mMatrix = pd.crosstab(pd.Series(prd_sens_list), pd.Series(gld_sense_list))
	print(mMatrix)

def main():
	predicted = sys.argv[1]
	gold_std_file = sys.argv[2]

	pred_file = extract_line(predicted)

	gd_file = extract_line(gold_std_file)

	dict1, dict2 = accuracy(pred_file,gd_file)

	confusion_matrix(dict1, dict2)

if __name__ == '__main__':
	main()