import gc
import os
import sys
import threading
import time

from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.distribute import multi_worker_test_base
from tensorflow.python.distribute import parameter_server_strategy_v2
from tensorflow.python.distribute import test_util
from tensorflow.python.distribute.cluster_resolver.cluster_resolver import SimpleClusterResolver
from tensorflow.python.distribute.coordinator import cluster_coordinator
from tensorflow.python.eager import context
from tensorflow.python.eager import def_function
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import errors
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import check_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import random_ops
from tensorflow.python.ops import variables
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.training import coordinator as thread_coordinator
from tensorflow.python.training import server_lib

_RPC_ERROR_FROM_WORKER = "GRPC error information from remote target /job:worker"
_RPC_ERROR_FROM_PS = "GRPC error information from remote target /job:ps"
_WORKER_PREEMPTION_THREAD_NAME = "WorkerPreemptionHandler"
_WORKER_THREAD_PREFIX = "WorkerClosureProcessingLoop"

class Model(object):

  def __init__(self, coordinator):
    self.cluster_coord = coordinator
    self.strategy = self.cluster_coord.strategy
    with self.cluster_coord.strategy.scope():
      self.build()

  def build(self):
    self.w = variables.Variable(
        initial_value=random_ops.random_uniform((10, 10)), dtype=dtypes.float32)
    self.iterations = variables.Variable(initial_value=0, dtype=dtypes.int32)
    # Allow external control to make the model run its train_fn in an infinite
    # loop. This allows us to reliably test worker preemption in the middle of
    # function execution.
    self.do_infinite_step = variables.Variable(False)

    self.rebuild_iterators()

  def rebuild_iterators(self, use_dataset_fn=True):
    if use_dataset_fn:

      def dataset_fn():
        data = random_ops.random_uniform((10, 10))
        dataset = dataset_ops.DatasetV2.from_tensors([data]).repeat()
        return dataset

      def distribute_dataset_fn():
        return self.cluster_coord.strategy.distribute_datasets_from_function(
            lambda _: dataset_fn())

      self.iterator = iter(
          self.cluster_coord.create_per_worker_dataset(distribute_dataset_fn))
      self.iterator2 = iter(
          self.cluster_coord.create_per_worker_dataset(distribute_dataset_fn))
    else:
      data = random_ops.random_uniform((10, 10))
      dataset = dataset_ops.DatasetV2.from_tensors([data]).repeat()

      self.iterator = iter(
          self.cluster_coord.create_per_worker_dataset(dataset))
      self.iterator2 = iter(
          self.cluster_coord.create_per_worker_dataset(dataset))

  def _train_fn_internal(self, iterator, iterator2):
    x = math_ops.matmul(array_ops.squeeze(next(iterator)), self.w)
    x = math_ops.matmul(array_ops.squeeze(next(iterator2)), x)
    x = math_ops.matmul(random_ops.random_uniform((10, 10)), x)
    self.w.assign_add(x)

  @def_function.function
  def train_fn(self, iterator, iterator2):
    self._train_fn_internal(iterator, iterator2)
    while self.do_infinite_step:
      self._train_fn_internal(iterator, iterator2)
    self.iterations.assign_add(1)

  def schedule_training_functions(self, num_steps):
    with self.strategy.scope():
      for _ in range(num_steps):
        self.cluster_coord.schedule(
            self.train_fn, args=(self.iterator, self.iterator2))

  def join_training_functions(self):
    self.do_infinite_step.assign(False)
    self.cluster_coord.join()

class BaseFaultToleranceTest(object):  # pylint: disable=missing-docstring

  def setUp(self, num_workers, num_ps, use_cs=False):
    super(BaseFaultToleranceTest, self).setUp()

    self._cluster = multi_worker_test_base.create_multi_process_cluster(
        num_workers=num_workers,
        num_ps=num_ps,
        rpc_layer="grpc",
        stream_output=True,
    )
    self._cluster_def = self._cluster.cluster_resolver.cluster_spec().as_dict()
    self._cluster_def["chief"] = [
        "localhost:%d" % multi_worker_test_base.pick_unused_port()
    ]
    cluster_resolver = SimpleClusterResolver(
        server_lib.ClusterSpec(self._cluster_def), rpc_layer="grpc"
    )

    if use_cs:
      os.environ["TF_PSS_ENABLE_COORDINATION_SERVICE"] = "1"
    # The strategy's constructor would connect to the cluster.
    self.strategy = parameter_server_strategy_v2.ParameterServerStrategyV2(
        cluster_resolver
    )
    self.cluster_coord = cluster_coordinator.ClusterCoordinator(self.strategy)

    self.thread_coord = thread_coordinator.Coordinator(
        clean_stop_exception_types=[]
    )
    self.num_workers = num_workers
    self.num_ps = num_ps

  def tearDown(self):
    super(BaseFaultToleranceTest, self).tearDown()
    self._cluster.stop()
    self._cluster = None

  def _restart(self, downtime_secs, job):
    """Kills `job` (index: 0) and restarts it after `downtime_secs`.

    Args:
      downtime_secs: secs before restarting the job.
      job: a string specifying the job to restart.
    """
    self._cluster.kill_task(job, 0)
    time.sleep(downtime_secs)
    self.assertFalse(context.check_alive("/job:%s/replica:0/task:0" % job))
    self._cluster.start_task(job, 0)
    while not context.check_alive("/job:%s/replica:0/task:0" % job):
      time.sleep(1)

  def _restart_in_thread(self, downtime_secs, restart_job):

    def _restart_fn():
      with self.thread_coord.stop_on_exception():
        self._restart(downtime_secs, restart_job)

    restart_thread = threading.Thread(target=_restart_fn)
    restart_thread.start()
    return restart_thread

  def _ensure_threads_closed(self):
    """Ensures worker and preemption threads are closed."""
    # Worker and preemption threads should exist before releasing
    # ClusterCoordinator.
    running_threads = test_util.get_running_threads()
    self.assertTrue(
        test_util.has_thread(_WORKER_THREAD_PREFIX, running_threads))
    self.assertIn(_WORKER_PREEMPTION_THREAD_NAME, running_threads)

    # Print object graph if ClusterCoordinator may leak.
    if sys.getrefcount(self.cluster_coord) > 2:
      try:
        test_util.show_backref(self.cluster_coord)
      except:  # pylint: disable=bare-except
        pass

    # Wait for threads to close.
    self.cluster_coord = None
    self.strategy = None
    gc.collect()
    time.sleep(1)

    # Verify thread names.
    running_threads = test_util.get_running_threads()
    self.assertNotIn(_WORKER_PREEMPTION_THREAD_NAME, running_threads)
    self.assertFalse(
        test_util.has_thread(_WORKER_THREAD_PREFIX, running_threads),
        "Worker thread is not stopped properly.")

  def _create_model_and_run_indefinitely(self):
    model = Model(self.cluster_coord)
    model.do_infinite_step.assign(True)
    model.schedule_training_functions(10)
    # Model does infinite training step, so at this moment, we expect to have
    # `self.num_workers` infinite closures inflight, and `10-self.num_workers`
    # closures in the queue.
    while (self.cluster_coord._cluster.closure_queue._inflight_closure_count <
           self.num_workers):
      time.sleep(0.1)
    return model

  def testClusterCoordinatorDestroyed(self):
    self._ensure_threads_closed()

  def testWorkerPreemptionBetweenFunctions(self):
    model = Model(self.cluster_coord)
    model.schedule_training_functions(2)
    model.join_training_functions()
    self.assertEqual(model.iterations.numpy(), 2)

    self._restart(downtime_secs=2, job="worker")

    model.schedule_training_functions(2)
    model.join_training_functions()
    self.assertEqual(model.iterations.numpy(), 4)

  def testWorkerPreemptionMidstFunction(self):
    model = Model(self.cluster_coord)
    model.do_infinite_step.assign(True)

    model.schedule_training_functions(4)
    # Model does infinite training step, so at this moment, we expect to have
    # `self.num_workers` infinite closures inflight, and `4-self.num_workers`
    # closures in the queue.
    while (self.cluster_coord._cluster.closure_queue._inflight_closure_count <
           self.num_workers):
      time.sleep(0.1)
    self.assertFalse(self.cluster_coord.done())
    self._restart(downtime_secs=2, job="worker")
    model.join_training_functions()
    self.assertGreaterEqual(model.iterations.numpy(), 4)

  def testOneWorkerPreemptionWithCancellation(self):

    @def_function.function
    def normal_function():
      x = random_ops.random_uniform((2, 10))
      y = random_ops.random_uniform((10, 2))
      return math_ops.reduce_mean(math_ops.matmul(x, y))

    @def_function.function
    def error_function():
      x = random_ops.random_uniform((2, 10))
      y = random_ops.random_uniform((10, 2))
      check_ops.assert_non_positive_v2(
          math_ops.reduce_sum(math_ops.matmul(x, y)))
      return x

    @def_function.function
    def long_function():
      x = random_ops.random_uniform((1000, 1000))
      for _ in math_ops.range(10000):
        a = random_ops.random_uniform((1000, 1000))
        b = random_ops.random_uniform((1000, 1000))
        x += math_ops.matmul(a, b)
      return x

    for _ in range(3):
      self.cluster_coord.schedule(normal_function)
    long_function_result = self.cluster_coord.schedule(long_function)
    self.cluster_coord.schedule(error_function)

    time.sleep(1)  # Let it run a couple steps.
    self._restart(2, "worker")

    # InvalidArgumentError thrown from the error_function.
    with self.assertRaises(errors.InvalidArgumentError):
      self.cluster_coord.join()

    # CancelledError thrown by ClusterCoordinator after cancelling due to user
    # error.
    with self.assertRaises(errors.CancelledError):
      long_function_result.fetch()

    for _ in range(3):
      self.cluster_coord.schedule(normal_function)
    self.cluster_coord.join()

    # The cluster is likely still being recovered since `join` returned early
    # due to the error_function.
    failure_handler = self.cluster_coord._cluster.failure_handler
    failure_handler.stop()
    failure_handler._preemption_handler_thread.join()

  def testHandleDatasetCreationFailureWithDatasetFn(self):
    model = Model(self.cluster_coord)

    restart_thread = self._restart_in_thread(5, "worker")

    model.schedule_training_functions(3)
    model.rebuild_iterators()
    model.schedule_training_functions(3)
    model.rebuild_iterators()
    model.schedule_training_functions(3)

    model.join_training_functions()

    self.thread_coord.join([restart_thread])
    self.assertGreaterEqual(model.iterations.numpy(), 3)

  # TODO(yuefengz): consider using combinations when there is more code
  # duplication.
  def testHandleDatasetCreationFailureWithDataset(self):
    model = Model(self.cluster_coord)

    restart_thread = self._restart_in_thread(5, "worker")

    model.schedule_training_functions(3)
    model.rebuild_iterators(use_dataset_fn=False)
    model.schedule_training_functions(3)
    model.rebuild_iterators(use_dataset_fn=False)
    model.schedule_training_functions(3)

    model.join_training_functions()

    self.thread_coord.join([restart_thread])
    self.assertGreaterEqual(model.iterations.numpy(), 3)

  def testWorkerPreemptionErrorType(self):

    @def_function.function
    def worker_train_fn():
      x = random_ops.random_uniform((2, 10))
      y = random_ops.random_uniform((10, 2))
      return math_ops.reduce_mean(math_ops.matmul(x, y))

    def run_fn():
      with self.thread_coord.stop_on_exception():
        with ops.device("/job:worker/replica:0/task:0"):
          for _ in range(3):
            for _ in range(3):
              worker_train_fn()
            time.sleep(5)

    run_thread = threading.Thread(target=run_fn)
    run_thread.start()
    time.sleep(1)  # Let it run a couple steps.
    self._restart(2, "worker")

    try:
      self.thread_coord.join([run_thread])
    except (errors.UnavailableError, errors.AbortedError) as e:
      logging.info("Got exception %r, error message is %s", e, e)

      self.assertIn(_RPC_ERROR_FROM_WORKER, str(e))  # pylint: disable=g-assert-in-except
      self.assertNotIn(_RPC_ERROR_FROM_PS, str(e))

      self.assertTrue("failed to connect to all addresses" in str(e) or
                      "Unable to find a context_id" in str(e) or
                      "Socket closed" in str(e) or
                      "Connection reset by peer" in str(e) or
                      "Transport closed" in str(e))

  def testWorkerPreemptionErrorTypeWithPythonFunction(self):

    def worker_train_fn():
      x = random_ops.random_uniform((2, 10))
      y = random_ops.random_uniform((10, 2))
      return math_ops.reduce_mean(math_ops.matmul(x, y))

    def run_fn():
      with self.thread_coord.stop_on_exception():
        with ops.device("/job:worker/replica:0/task:0"):
          for _ in range(3):
            for _ in range(3):
              worker_train_fn()
            time.sleep(5)

    run_thread = threading.Thread(target=run_fn)
    run_thread.start()
    time.sleep(1)  # Let it run a couple steps.
    self._restart(2, "worker")

    try:
      self.thread_coord.join([run_thread])
    except (errors.UnavailableError, errors.AbortedError) as e:
      logging.info("Got exception %r, error message is %s", e, e)

      self.assertIn(_RPC_ERROR_FROM_WORKER, str(e))  # pylint: disable=g-assert-in-except
      self.assertNotIn(_RPC_ERROR_FROM_PS, str(e))

      self.assertTrue("failed to connect to all addresses" in str(e) or
                      "Unable to find a context_id" in str(e) or
                      "Socket closed" in str(e) or
                      "Connection reset by peer" in str(e) or
                      "Transport closed" in str(e))

  def testPSPreemptionErrorType(self):

    with ops.device("/job:ps/replica:0/task:0"):
      v = variables.Variable(
          initial_value=random_ops.random_uniform((2, 10)),
          dtype=dtypes.float32)

    @def_function.function
    def worker_train_fn():
      y = random_ops.random_uniform((10, 2))
      return math_ops.reduce_mean(math_ops.matmul(v, y))

    def run_fn():
      with self.thread_coord.stop_on_exception():
        with ops.device("/job:worker/replica:0/task:0"):
          for _ in range(3):
            for _ in range(3):
              worker_train_fn()
            time.sleep(5)

    run_thread = threading.Thread(target=run_fn)
    run_thread.start()
    time.sleep(1)  # Let it run a couple steps.

    # Use a short restart delay to cover the case that RPC channel is reused
    self._restart(1, "ps")

    try:
      self.thread_coord.join([run_thread])
    except (errors.UnavailableError, errors.AbortedError) as e:
      logging.info("Got exception %r, error message is %s", e, e)
      self.assertIn(_RPC_ERROR_FROM_PS, str(e))  # pylint: disable=g-assert-in-except

      if isinstance(e, errors.UnavailableError):
        self.assertTrue("failed to connect to all addresses" in str(e) or
                        "Socket closed" in str(e) or
                        "Connection reset by peer" in str(e) or
                        "Transport closed" in str(e))

      if isinstance(e, errors.AbortedError):
        self.assertTrue(
            "RecvTensor expects a different device incarnation" in str(e) or
            "Unable to find a context_id" in str(e))
      self._ensure_threads_closed()

  def testTwoWorkersPreempted(self):
    if self.num_workers < 2:
      self.skipTest("Worker number is less than 2.")
    model = self._create_model_and_run_indefinitely()

    self.assertFalse(self.cluster_coord.done())
    self._cluster.kill_task("worker", 0)
    self._cluster.kill_task("worker", 1)
    time.sleep(2)
    self.assertFalse(context.check_alive("/job:worker/replica:0/task:0"))
    self.assertFalse(context.check_alive("/job:worker/replica:0/task:1"))
    self._cluster.start_task("worker", 0)
    self._cluster.start_task("worker", 1)
    time.sleep(2)
    self.assertTrue(context.check_alive("/job:worker/replica:0/task:0"))
    self.assertTrue(context.check_alive("/job:worker/replica:0/task:1"))

    model.join_training_functions()
    self.assertGreaterEqual(model.iterations.numpy(), 10)

  def testWorkerContinuousFailure(self):
    model = self._create_model_and_run_indefinitely()

    self.assertFalse(self.cluster_coord.done())
    self._cluster.kill_task("worker", 0)
    time.sleep(2)
    self.assertFalse(context.check_alive("/job:worker/replica:0/task:0"))
    self._cluster.start_task("worker", 0)
    time.sleep(2)
    self.assertTrue(context.check_alive("/job:worker/replica:0/task:0"))
    self._cluster.kill_task("worker", 0)
    time.sleep(2)
    self.assertFalse(context.check_alive("/job:worker/replica:0/task:0"))
    self._cluster.start_task("worker", 0)
    time.sleep(2)
    self.assertTrue(context.check_alive("/job:worker/replica:0/task:0"))

    model.join_training_functions()
    self.assertGreaterEqual(model.iterations.numpy(), 10)

  def testPSFailureWhileRecoveryFromWokerFailure(self):
    model = self._create_model_and_run_indefinitely()

    time.sleep(1)
    self.assertFalse(self.cluster_coord.done())

    def kill(task):
      self._cluster.kill_task(task, 0)
      self.sleep(1)
      self._cluster.start_task(task, 0)

    kill_thread_1 = threading.Thread(target=kill, args=("worker",))
    kill_thread_2 = threading.Thread(target=kill, args=("ps",))
    kill_thread_1.start()
    kill_thread_2.start()
    kill_thread_1.join()
    kill_thread_2.join()

    with self.assertRaises(
        (errors.UnavailableError, errors.InvalidArgumentError)):
      model.join_training_functions()

  def testNumpyFetchedAfterWorkerFailure(self):

    with self.strategy.scope():
      v = variables.Variable(initial_value=0, dtype=dtypes.int32)

    @def_function.function
    def worker_fn():
      return v + 1, v - 1

    remote_value = self.cluster_coord.schedule(worker_fn)
    # Attempt to fetch before killing worker task should succeed.
    self.assertEqual((1, -1), remote_value.fetch())
    self._cluster.kill_task("worker", 0)
    # So should attempt to fetch after killing worker task.
    self.assertEqual((1, -1), remote_value.fetch())

  def testTensorGotAfterWorkerFailure(self):

    with self.strategy.scope():
      v = variables.Variable(initial_value=0, dtype=dtypes.int32)

    @def_function.function
    def worker_fn():
      return v + 1, v - 1

    remote_value = self.cluster_coord.schedule(worker_fn)

    # Attempt to fetch before killing worker task should succeed.
    fetched = remote_value.get()[0]
    self.assertIsInstance(fetched, tensor.Tensor)
    self.assertEqual(fetched.device, "/job:chief/replica:0/task:0/device:CPU:0")
    self.assertEqual((1, -1), remote_value.get())
    remote_value.get()[0].numpy()

    # As well as the remote tensors that point to worker0 or worker1.
    values = remote_value._values[0]
    self.assertIsInstance(values, tensor.Tensor)
    self.assertRegex(values.device,
                     "/job:worker/replica:0/task:[0-1]/device:CPU:0")
    self.assertEqual((1, -1), remote_value._values)
    remote_value._values[0].numpy()

    # Terminate the workers and wait a little so that they are indeed killed.
    for i in range(self.num_workers):
      self._cluster.kill_task("worker", i)
    time.sleep(5)

    # Attempt to fetch after killing worker tasks should succeed as well.
    remote_value.get()[0].numpy()
    self.assertEqual((1, -1), remote_value.get())

    # Attempting to copy the tensor from worker now should fail.
    with self.assertRaises(errors.UnavailableError) as cm:
      remote_value._values[0].numpy()
    self.assertIn("failed to connect to all addresses", cm.exception.message)
    self.assertIn("/job:worker/replica:0/task:", cm.exception.message)

  def testFetchFromPSAfterWorkerFailure(self):
    # Test for flaky failures when reading from a parameter server while a
    # worker is recovering.
    # Place some variables on PSes, kill a worker, and continuously poll one of
    # those variables.

    model = Model(self.cluster_coord)

    # kill the worker after a delay to make sure variable reading runs while
    # worker is up, while it's down, and while it restarts
    def kill_after_delay():
      time.sleep(3)
      logging.info("Killing worker 0")
      self._cluster.kill_task("worker", 0)
      time.sleep(1)
      logging.info("Restarting worker 0")
      self._cluster.start_task("worker", 0)

    kill_thread = threading.Thread(target=kill_after_delay)
    kill_thread.start()

    model.do_infinite_step.assign(True)
    model.schedule_training_functions(1)

    num_reads = 0
    num_reads_after_restart = 0
    read_interval_secs = 0.1
    worker_has_stopped = False
    # limit runtime of the test: stop after doing a few reads after worker
    # is back up, or after a fixed maximum number of reads
    while num_reads_after_restart <= 5 and num_reads < 200:
      worker_up = context.check_alive("/job:worker/replica:0/task:0")
      if not worker_up:
        worker_has_stopped = True
      if worker_up and worker_has_stopped:
        num_reads_after_restart += 1

      model.join_training_functions()
      start = time.time()
      while time.time() < start + read_interval_secs:
        model.iterations.read_value()

      num_reads += 1
      # run another epoch
      model.do_infinite_step.assign(True)
      model.schedule_training_functions(1)

  def testClusterStateNotDisrupted(self):
    # This test has side effects and can disrupt other tests, even if the
    # resource created by it will not be used in following tests.
    # TODO(b/155209534): enable this test.
    # self.testPSPreemptionErrorType()

    self.thread_coord = thread_coordinator.Coordinator(
        clean_stop_exception_types=[])
    self.testWorkerPreemptionMidstFunction()

    self.thread_coord = thread_coordinator.Coordinator(
        clean_stop_exception_types=[])
    self.testWorkerPreemptionErrorType()

    # In previous tests, workers may fail after training is done. But the
    # following tests start with creating resources where failure is not
    # handled.
    # TODO(b/153888707): enable the following two tests.
    # self.testTwoWorkersPreempted()
    # self.testWorkerContinuousFailure()

  def _run_and_kill_ps_task(self):
    self._create_model_and_run_indefinitely()
    self._cluster.kill_task("ps", 0)
    while self.cluster_coord._cluster.closure_queue._error is None:
      time.sleep(1)
    logging.info("Trying to join, expecting error")

  def testJoinRaisesUnavailableErrorAtPsFailure(self):
    self._run_and_kill_ps_task()
    with self.assertRaises((errors.UnavailableError, errors.NotFoundError,
                            errors.FailedPreconditionError)):
      self.cluster_coord.join()

  def testScheduleRaisesUnavailableErrorAtPsFailure(self):
    self._run_and_kill_ps_task()
    with self.assertRaises((errors.UnavailableError, errors.NotFoundError,
                            errors.FailedPreconditionError)):
      self.cluster_coord.schedule(def_function.function(lambda: None))

  def testWorkerExecutionAfterPsFailureRaisesExpectedError(self):
    model = self._create_model_and_run_indefinitely()
    for i in range(self.num_ps):
      self._cluster.kill_task("ps", i)
    while self.cluster_coord._cluster.closure_queue._error is None:
      time.sleep(1)

    @def_function.function
    def trivial_function():
      return model.iterations + 1

    for i in range(self.num_workers):
      try:
        with ops.device("/job:worker/replica:0/task:{}".format(i)):
          trivial_function()
      except Exception as e:  # pylint: disable=broad-except
        if cluster_coordinator._is_ps_failure(e):  # pylint: disable=protected-access
          if i < self.num_workers - 1:
            continue
          return
      raise AssertionError("Executing a function after PS fails, should "
                           "result in a PS failure.")

  def testAsyncWaitIsNoOp(self):
    if self.num_workers < 2:
      self.skipTest("Worker number is less than 2.")
    model = self._create_model_and_run_indefinitely()

    self.assertFalse(self.cluster_coord.done())
    self._cluster.kill_task("worker", 0)
    time.sleep(2)
    self.assertFalse(context.check_alive("/job:worker/replica:0/task:0"))
    # Should pass without exception even with failed remote workers
    context.async_wait()

    model.join_training_functions()
    self.assertGreaterEqual(model.iterations.numpy(), 10)

    self._cluster.start_task("worker", 0)


# -*- coding: utf-8 -*-
"""
Created on Mon Apr 20 16:08:15 2020

@author: lv13916
"""

import pandas as pd
import numpy as np
import sys
import csv
import os
from fuzzywuzzy import fuzz
from io import StringIO
import matplotlib.pyplot as plt

path = r"C:\Users\lv13916\University of Bristol\AMR Database - Documents\Lucy\synergy\lucy_data"
path = path.replace("\\", "/")

sfh_df = pd.read_csv(path + os.sep + "synergy_abs_descriptions.csv")

# method to prevent unicoding problem
whole_file = list()
with open(path + os.sep + "abs_medicines_database.csv") as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count > 276:
            break
        else:
            whole_file.append(row)
            line_count +=1

db_df = pd.DataFrame.from_records(whole_file[1:], columns = whole_file[0])

med = sfh_df['sale_item_description'][29]

sfh_name = sfh_name.split(" ")

db_med = db_df['Medicine'][0]
db_name = db_name.split(" ")

best_match_list =  []
best_match_score_list = []

# each synergy medicine
for med in sfh_df['sale_item_description']:
    med_split = med.split(" ")
    print(med)
    final_match_list = list()
    final_score_list = list()
    # loop each database medicine
    for db_med in db_df['Medicine']:
        #print(db_med)
        db_med_split = db_med.split(" ")
        count = 0
        n = len(med_split)
        status = "okay"
        if n == 1:
            score_weights = [1*n]
        if n > 1:
            score_weights = [0.75*n, 0.25*n]

        if n > 2:
            score_weights = [0.5*n, 0.25*n] + list(np.repeat(0.25/(n-2), n-2)*n)
        else:
            print("Error")
            status = "error"

        if status == "okay":
            max_score_token_str = ""
            max_score_num_total = 0

            # loop each token of synergy medicine
            for token in med_split:
                score_list = list()
                matched_list = list()

                count += 1
                #print("Token: " + token)
                #loop each token in db_med
                for db_token in db_med_split:
                    score = fuzz.ratio(token, db_token.lower()) * score_weights[count-1]
                    score_list.append(score)
                    matched_list.append(db_token)
                    #print(db_token.lower() + " :" + token + " =" + str(score))
                    #print(count)

                # put scores into a dataframe
                df_scores = pd.DataFrame(score_list, index = matched_list, columns =['score'])
                #get maximum scored word for this token
                #print("Matched: " + df_scores.idxmax()[0])
                max_score_token = df_scores.idxmax()[0]
                max_score_num = df_scores.max()[0]

                ser = pd.Series(db_med_split)
                ind = ser[ser == max_score_token].index[0]

                # remove as that token has been matched
                db_med_split[ind] = ''

                max_score_token_str = max_score_token_str + " " + max_score_token
                max_score_num_total = max_score_num_total + max_score_num

            final_score = max_score_num_total/(n*100)
            #print(db_med + ": " + str(final_score))

            final_match_list.append(db_med)
            final_score_list.append(final_score)

        else:
            final_score = 0
            final_match_list.append('NA')
            final_score_list.append(final_score)

    #whole df of scores for that sfh medicine
    df_final_matched = pd.DataFrame(list(zip(final_match_list, final_score_list)), columns =['db_match', 'score'])

    best_score = max(df_final_matched['score'])

    best_match = df_final_matched['db_match'][df_final_matched['score'].idxmax()]

    best_match_list.append(best_match)
    best_match_score_list.append(best_score)

df_final = pd.DataFrame(list(zip(sfh_df['sale_item_description'], best_match_list, best_match_score_list)), columns =['sfh_med', 'db_match', 'score'])
df_final.to_csv('C:/Users/lv13916/University of Bristol/AMR Database - Documents/Lucy/synergy/df_final.csv')

np.histogram(df_final['score'])
plt.show()

df_final.to_csv('C:/Users/lv13916/University of Bristol/AMR Database - Documents/Lucy/synergy/df_final.csv')


import sys

input = sys.stdin.readline

bracket = dict()
bracket['('] = ')'
bracket['['] = ']'

while True:
  stack = []
  breaker = False
  data = input().rstrip()
  if data == '.':
    break
  for i in range(len(data)):
    if data[i] == '[' or data[i] == '(':
      stack.append(data[i])
    elif data[i] == ']' or data[i] == ')':
      if len(stack) == 0 or bracket[stack.pop()] != data[i]:
        breaker = True
        break
  if not breaker and len(stack) == 0:
    print('yes')
  else:
    print('no')

이경훈_4949_균형잡힌 세상.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct  3 16:55:19 2022

@author: Jack
"""
## https://towardsdatascience.com/beginners-guide-to-custom-environments-in-openai-s-gym-989371673952

## There will specific file structuring if I want to "register" my gym env.

# import gym
# class BasicEnv(gym.Env):
# def __init__(self):
#         self.action_space = gym.spaces.Discrete(5)
#         self.observation_space = gym.spaces.Discrete(2)
# def step(self, action):
#         state = 1

#         if action == 2:
#             reward = 1
#         else:
#             reward = -1

#         done = True
#         info = {}
#         return state, reward, done, info
# def reset(self):
#         state = 0
#         return state

## Sure there us a way to implement gym environments. But are there any
## specifications for me to follow for structuring agents/algorithms?

## From Erik's code it seems I code as I like, as a usual class?

## And then code my own runs to use the two.

import numpy as np
import matplotlib.pyplot as plt
import math
import random

## PROBLEM 2 : BANDITS
## In this section, we have given you a template for coding each of the
## exploration algorithms: epsilon-greedy, optimistic initialization, UCB exploration,
## and Boltzmann Exploration

## You will be implementing these algorithms as described in the “10-armed Testbed” in Sutton+Barto Section 2.3
## Please refer to the textbook or office hours if you have any confusion.

## note: you are free to change the template as you like, do not think of this as a strict guideline
## as long as the algorithm is implemented correctly and the reward plots are correct, you will receive full credit

# This is the optional wrapper for exploration algorithm we have provided to get you started
# this returns the expected rewards after running an exploration algorithm in the K-Armed Bandits problem
# we have already specified a number of parameters specific to the 10-armed testbed for guidance
# iterations is the number of times you run your algorithm

# WRAPPER FUNCTION
def explorationAlgorithm(explorationAlgorithm, param, iters):
    cumulativeRewards = []
    for i in range(iters):
        # number of time steps
        t = 1000
        # number of arms, 10 in this instance
        k = 10
        # real reward distribution across K arms
        rewards = np.random.normal(1,1,k)
        # counts for each arm
        n = np.zeros(k)
        # extract expected rewards by running specified exploration algorithm with the parameters above
        # param is the different, specific parameter for each exploration algorithm
        # this would be epsilon for epsilon greedy, initial values for optimistic intialization, c for UCB, and temperature for Boltmann
        currentRewards = explorationAlgorithm(param, t, k, rewards, n)
        cumulativeRewards.append(currentRewards)
    # TO DO: CALCULATE AVERAGE REWARDS ACROSS EACH ITERATION TO PRODUCE EXPECTED REWARDS
    expectedRewards = np.array(cumulativeRewards).mean(axis=0)
    return expectedRewards

# BOLTZMANN EXPLORATION TEMPLATE
def boltzmannE(temperature, steps, k, realRewards, n):
    # TO DO: initialize structure to hold expected rewards per step
    currentRewards = np.zeros(steps)

    # TO DO: initialize q values per arm
    Q_table = np.zeros(k)

    # TO DO: initialize probability values for each arm
    action_probs = np.ones(k) / k

    # TO DO: implement the Boltzmann Exploration algorithm over all steps and return the expected rewards across all steps
    for step in range(steps):

        # calculate Boltzmann action probs based on Q table
        for a in range(k):
            action_probs[a] = np.exp(temperature * Q_table[a]) / np.exp(temperature * Q_table).sum()

        # log expected reward going into this time step
        currentRewards[step] = action_probs.dot(realRewards)

        # choose action
        action = np.random.choice(k, p=action_probs)

        # collect reward
        reward = realRewards[action] + np.random.normal()
        # track times action has been taken
        n[action] += 1
        # update action value estimate
        Q_table[action] += (1 / n[action]) * (reward - Q_table[action])

    #raise NotImplementedError()
    return currentRewards

# PLOT TEMPLATE
def plotExplorations(paramList, exploration_algo):
    # TO DO: for each parameter in the param list, plot the returns from the exploration Algorithm from each param on the same plot
    x = np.arange(1,1001)
    # calculate your Ys (expected rewards) per each parameter value
    # plot all the Ys on the same plot
    # include correct labels on your plot!

    plt.figure()

    for param in paramList:
        Y = explorationAlgorithm(exploration_algo, param, iters=100)
        plt.plot(x, Y, label=param)

    plt.xlabel('Steps')
    plt.ylabel('Average expected reward')

    if (exploration_algo == boltzmannE):               plt.legend(title='temperature')

    #raise NotImplementedError()

# RUN IT ALL
if __name__ == "__main__":
    plotExplorations([1, 3, 10, 30, 100], boltzmannE)

    plt.figure()


import math
import numpy as np

class EquityFundReturn:
    """This class stores the parameters for generating returns for a class of equities.
    It also stores the current state (volatility) of an equity class and provides
    a method to generate the return for the next period, given the current state
    and shocks to the return and its volatility.
    """

    def __init__(self, target_vol, current_vol, min_vol, max_vol_before, max_vol_after,
                 mean_rev_strength, vol_std_dev, a, b, c, SET_median_return, SET_volatility):
        self.target_vol = target_vol
        self.current_vol = current_vol
        self.min_vol = min_vol
        self.max_vol_before = max_vol_before
        self.max_vol_after = max_vol_after
        self.mean_rev_strength = mean_rev_strength
        self.vol_std_dev = vol_std_dev
        self.a = a
        self.b = b
        self.c = c
        self.SET_median_return = SET_median_return
        self.SET_volatility = SET_volatility

    def get_next_return(self, return_shock, vol_shock):
        """Calculate the next return based on the current volatility and shocks to the return and volatility."""
        curr_log_vol = math.log(self.current_vol)
        target_log_vol = math.log(self.target_vol)

        # First update the volatility
        curr_log_vol = (1 - self.mean_rev_strength) * curr_log_vol + (self.mean_rev_strength * target_log_vol)
        self.current_vol = math.exp(curr_log_vol)
        self.current_vol = min(self.max_vol_before, self.current_vol)
        curr_log_vol = math.log(self.current_vol)

        curr_log_vol = curr_log_vol + vol_shock * self.vol_std_dev
        self.current_vol = math.exp(curr_log_vol)
        self.current_vol = min(self.max_vol_after, self.current_vol)
        self.current_vol = max(self.min_vol, self.current_vol)

        # Next compute the log return based on the volatility and shock
        next_mean_return = self.a + self.b * self.current_vol + self.c * self.current_vol * self.current_vol
        next_return = (next_mean_return / 12) + return_shock * (self.current_vol / (12 ** 0.5))

        return math.exp(next_return) - 1

    def get_next_return_SET(self, return_shock):
        """Calculate the next return based on the SET median return and return shock."""
        return ((1 + self.SET_median_return) ** (1 / 12) - 1) + return_shock * (self.SET_volatility / (12 ** 0.5))

# All public properties of the original VBA class have been converted to attributes of the Python class, which are set in the __init__ method.
# All function procedures have been converted to methods of the Python class.
# VBA's Application.WorksheetFunction.min() and Application.WorksheetFunction.max() functions have been replaced with Python's built-in min() and max() functions.
# The Log() and Exp() functions have been replaced with math.log() and math.exp(), respectively.


# nums = [5, 2, 6, 1, -3, 2]
# smaller = nums[0]
# for num in nums:
#   if num < smaller:
#     smaller = num
# smallest = smaller
# print('The smallest number is '+ str(smallest))

nums = [5, 2, 6, 1, -3, 2]
smaller = None
for num in nums:
  if smaller is None:
    smaller = num
  elif num < smaller:
    smaller = num
smallest = smaller
print('The smallest number is '+ str(smallest))

# "is" operator
# implies "is the same as"
# similar to, but strong than "=="
# there is also an "is not" operator
# us "is" for True False None

# ie. "is" checks to see if the object is the same object, while == just checks if they are equivalent.


import glob, os, re, datetime, json, zipfile
import dateutil.parser, time

command_dispatched_pattern = re.compile(r"\s*Key: type\s*Value: CommandDispatched")
timestamp_pattern = re.compile(r"\s*Key: timestamp\s*Value: (.*Z)")
event_payload_pattern = re.compile(r"(\{.*\})", re.S)
csv_content = "timestamp,command_type\n"

for file in glob.glob("logs-*.zip"):
    with zipfile.ZipFile(file, "r") as zip_ref:
        with zip_ref.open("results/logs/kaf-command.txt") as kafka_log:
            kafka_log = kafka_log.read().decode("utf-8")
            events = kafka_log.split("Headers:")
            commands_dispatched = [event for event in events if command_dispatched_pattern.search(event)]
            for event in commands_dispatched:
                event_payload = json.loads(event_payload_pattern.search(event).group(1))
                timestamp = dateutil.parser.isoparse(timestamp_pattern.search(event).group(1))
                command_type = event_payload["commandType"]
                csv_content += f"{int(timestamp.timestamp())},{command_type}\n"

f = open("results/case-study-dungeon/commands.csv", "w")
f.write(csv_content)
f.close()


# -*- coding: utf-8 -*-
"""
Created on Thu May 28 22:53:52 2020

@author: Andrew
"""

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

import datetime

plt.style.use('ggplot')

# df = pd.read_csv(r"C:\Users\Andrew\Documents\Python Scripts\data set\nyc open data\311_Service_Requests_from_2019_to_Present.csv")

# df["Created (Date Only)"] = df["Created Date"].dt.date
# df["Closed (Date Only)"] = df["Closed Date"].dt.date
# df["time to close out"] = df["Closed (Date Only)"] - df["Created (Date Only)"]
# df = df[pd.notnull(df["time to close out"])]
# df["time to close out"] = df["time to close out"].dt.days

# df.to_pickle(r'C:\Users\Andrew\Documents\Python Scripts\data set\nyc open data\311_Service_Requests_from_2019_to_Present.pkl')

df = pd.read_pickle(r'C:\Users\Andrew\Documents\Python Scripts\data set\nyc open data\311_Service_Requests_from_2019_to_Present.pkl')

'''some exploration of data'''
df["Agency"].value_counts()
(df["Borough"].value_counts()[:10]/df.shape[0]).plot(kind="bar", figsize= (10,10))

(df[df["Agency"]=='NYPD']['Descriptor'].value_counts()/df.shape[0]).plot(kind="bar", figsize= (10,10))

# fig, ax = plt.subplots(figsize = (10,10))
# sns.barplot(x=df["Establishment Borough"], y = df["Number Of Employees"], hue = df["Business Sector"]
#             , ax = ax, ci = None)

'''What's happened to 311 calls since Covid Started?'''
pivot = df.pivot_table(index="Descriptor", columns = "Borough", values = "Unique Key", aggfunc = "count")
pivot = pivot.sort_values(by=['BRONX'],ascending=False)
pivot.iloc[:10,:5].plot(kind="bar", figsize=(10,10)).set(title="Number of Complaints by Type and Borough", ylabel = "Count")

pivot = df.pivot_table(index="Created (Date Only)", columns = "Borough", values = "Unique Key", aggfunc = "count")
pivot = pivot.rolling(7).mean()
pivot.iloc[-300:,:5].plot(kind="line", figsize=(10,10)).set(title="Number of Complaints by Borough", ylabel = "Count")

pivot = df[df["Created (Date Only)"] <= datetime.date(2020, 2, 27)].pivot_table(index="Created (Hour Only)", columns = "Descriptor", values = "Unique Key", aggfunc = "count")
pivot = pivot.T
pivot = pivot.sort_values(by=pivot.columns[-1],ascending=False)
pivot = pivot.T
# pivot = pivot.rolling(7).mean()
pivot.iloc[:,0:4].plot(kind="line", figsize=(10,10)).set(title="Number of Complaints by Type", ylabel = "Count")

'''who is partying?'''
pivot = df[df["Created (Date Only)"] >= datetime.date(2020, 2, 27)].pivot_table(index="Descriptor", columns = "Borough", values = "Unique Key", aggfunc = "count")
pivot = pivot.sort_values(by=['BRONX'],ascending=False)
pivot.iloc[:10,:5].plot(kind="bar", figsize=(10,10)).set(title="Number of Complaints by Type and Borough", ylabel = "Count")

'''What's happened to how long it takes to get a response or close?'''
pivot = df.pivot_table(index="Created (Date Only)",
                       columns = "Borough", values = "time to close out", aggfunc = np.mean)
pivot = pivot.rolling(7).mean()
pivot.iloc[:,:5].plot(kind="line", figsize=(10,10)).set(title="Days to Close Out Complaint", ylabel = "Days")

pivot = df[df["Created (Date Only)"] <= datetime.date(2020, 2, 27)].pivot_table(index="Created (Hour Only)",
                       columns = "Borough", values = "time to close out", aggfunc = np.mean)
# pivot = pivot.rolling(7).mean()
pivot.iloc[:,:5].plot(kind="line", figsize=(10,10)).set(title="Days to Close Out Complaint", ylabel = "Days")

'''GIS plotting GIF'''
descriptors = ["Social Distancing"]
plot_df = df[df["Descriptor"].isin(descriptors)]

BBox = ((df.Longitude.min(),   df.Longitude.max(),
         df.Latitude.min(), df.Latitude.max()))

#map from openstreetmap
ruh_m = plt.imread(r'C:\Users\Andrew\Documents\Python Scripts\Medium Charts\NYC Open Data\StreetMap.png')

import glob
import moviepy.editor as mpy
from collections import OrderedDict

#mapping consistent colors
unique = plot_df["Descriptor"].unique()
cmap = plt.cm.get_cmap('Set1')
cmap = [cmap(0.1),cmap(0.2),cmap(0.3),cmap(0.4),cmap(0.5)]
palette = dict(zip(unique, cmap))

dates = df["Created (Date Only)"].unique()
for date in dates[-70:]:
    fig, ax = plt.subplots(figsize = (10,10))
    plot_df_single_day = plot_df[plot_df["Created (Date Only)"] == date]

    #plotting nyc complaints
    sns.scatterplot(x = plot_df_single_day["Longitude"], y = plot_df_single_day["Latitude"]
                , hue = plot_df_single_day["Descriptor"], alpha = 0.7, ax = ax,zorder = 10,
                size = 0.1, palette = palette)

    #plotting nyc arrests

    #creating legend that is consistent and doesn't repeat
    handles, labels = plt.gca().get_legend_handles_labels()
    by_label = OrderedDict(sorted(zip(labels[1:-3], handles[1:-3])))
    ax.legend(by_label.values(), by_label.keys(), loc = "upper left")

    ax.set_title('Social Distancing 311 Calls NYC on: ' + str(date))
    ax.set_xlim(BBox[0],BBox[1])
    ax.set_ylim(BBox[2],BBox[3])
    ax.imshow(ruh_m, zorder=0, extent = BBox, aspect= 'auto')
    fig.savefig(r"C:\Users\Andrew\Documents\Python Scripts\Medium Charts\GIF creation\{}.png".format(date), quality = 85)

gif_name = 'COVID NYC 311 Social Distancing Calls'
fps = 6
file_list = glob.glob(r'C:\Users\Andrew\Documents\Python Scripts\Medium Charts\GIF creation\*')
clip = mpy.ImageSequenceClip(file_list, fps=fps)
clip.write_gif(r'C:\Users\Andrew\Documents\Python Scripts\Medium Charts\GIF creation\{}.gif'.format(gif_name), fps=fps)


from django.forms import ModelForm
from .models import Reservation

class ReservationForm(ModelForm):

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        self.film = kwargs.pop('film')
        print(kwargs)
        super(ReservationForm, self).__init__(*args, **kwargs)

    def save(self, *args, **kwargs):
        instance = super(ReservationForm, self).save(commit=False)
        instance.user = self.user
        instance.film = self.film
        instance.save(*args, **kwargs)
        return instance

    class Meta:
        model = Reservation
        exclude = ('user', 'film')


from collections import OrderedDict
from functools import lru_cache

from numpy import int32, float64
from pandas import Series

class PoloniexTicker:
    ref = OrderedDict([
        ('id', int32),
        ('last', float64),
        ('lowestAsk', float64),
        ('highestBid', float64),
        ('percentChange', float64),
        ('baseVolume', float64),
        ('quoteVolume', float64),
        ('isFrozen', bool),
        ('high24hr', float64),
        ('low24hr', float64)
    ])

    def __init__(self, data: list):
        self.data = data

    @lru_cache(maxsize=16)
    def __getattr__(self, item: str):
        try:
            idx = list(self.ref.keys()).index(item)
        except ValueError:
            raise AttributeError(f"{item} is not in {self.__class__.__name__}")

        return self.ref[item](self.data[idx])

    @lru_cache(maxsize=2)
    def as_series(self):
        return Series({key: _type(val) for (key, _type), val in zip(self.ref.items(), self.data)})


import pygame
from pygame.locals import *
from pygame.color import *
import pymunk
import pygame_util
import pymunk.util
from pymunk import Vec2d
from math import *
import random
from time import sleep
import copy

from walker import walker

class simulation():
    def __init__(self, scr_w = 800, scr_h = 600, \
                    angle=pi/10, gravity=200, \
                    show=True):
        # Window size
        self.scr_w = scr_w
        self.scr_h = scr_h
        # Graphics initialization (if show)
        self.show = show
        if self.show:
            pygame.init()
            self.screen = pygame.display.set_mode((self.scr_w, self.scr_h))
            self.clock = pygame.time.Clock()
        # Create the space
        self.space = pymunk.Space()
        self.space.gravity = (0, -gravity)
        # Create the floor
        self._create_floor(angle)

    def _create_floor(self, angle):
        # Create the floor
        body = pymunk.Body()
        body.position = self.scr_w/2, self.scr_h/4
        v = [(-self.scr_w,-(self.scr_h/2)*sin(angle)), \
             (self.scr_w,(self.scr_h/2)*sin(angle)), \
             (self.scr_w, -self.scr_h/2), \
             (-self.scr_w, -self.scr_h/2)]
        floor = pymunk.Poly(body, v)
        floor.friction = 1.0
        floor.elasticity = 0.4
        self.space.add(floor)

    def _invy(self, pos):
        return pos[0], self.scr_h - pos[1]

    def step(self, delta, time, sim_time, generation, individuo):
        # Simulation step
        self.space.step(delta)
        # Draw stuff (if show)
        if self.show:
            self.screen.fill(THECOLORS['black'])
            pygame_util.draw_space(self.screen, self.space)
            pygame_util.text(self.screen, time, sim_time, generation, individuo)
            pygame.display.flip()
            self.clock.tick(1/delta)

    def interactive(self):
        # Interactive mode
        running = True
        robot = None
        while running:
            # Deal with clicks and other events
            for event in pygame.event.get():
                if event.type == QUIT:
                    running = False
                if event.type == MOUSEBUTTONDOWN:
                    robot = walker(self.space, self._invy((537,226)), 80, 60, 40, pi, pi/2, 0, 0)

            self.step(0.02, 0, 0, 0,0)

    def put_robot(self, robot):
        self.robot = robot

    def get_ke(self):
        # Calculates the kinect energy of the simulation
        k = 0.0
        for body in self.space.bodies:
            k += body.kinetic_energy
        return k

    # Simulation of an individual
    def individual_sim(self, pos, ul, ll, w, lua, lla, rua, rla, generation, individuo):
        robot = walker(self.space, pos, ul, ll, w, lua, lla, rua, rla)

        sim_time = 2500                  # 300 ~= 6 segundos
        time = 0
        walk_time = 0

        last_x = [(0,0), (0,0), (0,0), (0,0)]

        while True:
            self.step(0.02, time, sim_time, generation, individuo)
            time += 1

            # Get data from simulation
                # self.space.bodies[0].position = upper_leg_1
                # self.space.bodies[1].position = lower_leg_1
                # self.space.bodies[2].position = lower_leg_2
                # self.space.bodies[3].position = upper_leg_2

            ke = self.get_ke()
            cur_x = [(self.space.bodies[0].position.x, self.space.bodies[0].position.y),\
                     (self.space.bodies[1].position.x, self.space.bodies[1].position.y),\
                     (self.space.bodies[2].position.x, self.space.bodies[2].position.y),\
                     (self.space.bodies[3].position.x, self.space.bodies[3].position.y)]

            # Se o individuo estar se mexendo ou ainda tiver tempo ou se estiver dentro do cenario
            if time < sim_time and ke > 30000 and min(cur_x[:][0]) > 20:

                #Se a upper_leg estiver acima da lower_leg, em relacao ao eixo Y
                if (cur_x[0][1] - cur_x[1][1] > ll/2) and (cur_x[3][1] - cur_x[2][1] > ll/2):

                    #Se a posicao em x da lower_leg for diferente do passo anterior
                    if cur_x[1][0] < last_x[1][0] or cur_x[2][0] < last_x[2][0]:
                        walk_time += 1

            #Caso tempo acabe ou walker fique parado
            else:
                #retorna o tempo de caminhada e a o valor mais proximo da borda esquerda
                return walk_time, (800 - min(cur_x[1][0], cur_x[2][0]))

            last_x = copy.copy(cur_x)

# Uncomment this lines, change the values from the robot on line 73, run simulation.py, and use mouse click to create a robot
#s = simulation()
#s.interactive()


import genanki
from tinydb import TinyDB, Query

import sys, getopt

listdb = TinyDB('./db/words.json')

dictdb = TinyDB('./db/dict.json')

CSS = """.card {
 font-family: arial;
 font-size: 20px;
 text-align: center;
 color: black;
 background-color: white;
}
"""

# Card model
WordCard = genanki.Model(
        1276008402,
        "Card Model",
        fields=[
            {'name': 'Word'},
            {'name': 'Phone'},
            {'name': 'SentenceEn'},
            {'name': 'SentenceZh'},
            {'name': 'Meaning'},
            ],
        templates=[
            {
                'name': 'Card 1',
                'qfmt': '

{{Word}}
{{Phone}}

{{SentenceEn}}',
                'afmt': '{{FrontSide}}

{{SentenceZh}}

{{Meaning}}'
                },
            ],
        css=CSS
        )

# deck model
WordDeck = genanki.Deck(
    2054490786,
    'Words')

# Add word to listdb
def new_word(word):
    Word = Query()
    if len(listdb.search(Word.word == word)) == 0:
        listdb.insert({'word': word})
        print('Word "' + word + '" inserted' + ', ' + str(len(listdb.all())) + ' words in total.')
    else:
        print('Word "' + word + '" already exists, skipping.')

# Go through listdb and generate a list of NOTES
def make_notes():
    notes = []
    words = listdb.all()
    Dict = Query()
    if len(words) != 0:
        for word in words:
            item = dictdb.search(Dict.headWord == word['word'])
            if len(item) == 0:
                print('Word "' + word['word'] + '" is not in current dictionary, skipping.')
                continue

            print('Adding word "' + word['word'] + '" to deck...')

            content = []
            # 单词
            content.append(item[0]['headWord'])
            # 音标
            if 'usphone' in item[0]['content']['word']['content']:
                content.append('[' + item[0]['content']['word']['content']['usphone'] + ']')
            else:
                content.append('
')
            # 英句
            if 'sentence' in item[0]['content']['word']['content'].keys():

                content.append(item[0]['content']['word']['content']['sentence']['sentences'][0]['sContent'])
            # 中句
                content.append(item[0]['content']['word']['content']['sentence']['sentences'][0]['sCn'])
            else:
                content.append('
')
                content.append('
')
            # 词性
            # 意思
            meaning_str = ''
            for con in item[0]['content']['word']['content']['trans']:
                meaning_str += con['pos']
                meaning_str += ' '
                meaning_str += con['tranCn']
                meaning_str += ' '
            content.append(meaning_str)

            note = genanki.Note(
                    model=WordCard,
                    fields=content
                    )
            notes.append(note)
    else:
        print('The list is empty, skipping.')

    return notes

# Using list of NOTES to generate deck and save
def gen_deck(deck, notes):
    if len(notes) == 0:
        print('Notes list is empty, skipping.')
    else:
        for note in notes:
            deck.add_note(note)
        genanki.Package(deck).write_to_file('output.apkg')

def print_help():
    print('Usage: python3 main.py [opt] <arg>')
    print('\tOptions:')
    print('\t\t-h print this help')
    print('\t\t-a <word> required, add a word to list')
    print('\t\t-g generate word deck using current list')
    print('\t\t-r <filepath> required, read text file and write them to listdb')

def main(argv):
    if len(sys.argv) <= 1:
        print('len < 1')
        print_help()
        sys.exit(1)
    try:
        opts, args = getopt.getopt(argv,"hga:r:")
    except getopt.GetoptError:
        print_help()
        sys.exit(1)

    for opt, arg in opts:
        if opt == '-h':
            print_help()
            sys.exit()

        elif opt == '-a':
            new_word(arg)
            sys.exit()

        elif opt == '-g':
            notes = make_notes()
            gen_deck(WordDeck, notes)
            sys.exit()

        elif opt == '-r':
            filename = arg
            with open(filename) as fin:
                for line in fin.readlines():
                    word = line.strip()
                    new_word(word)

        else:
            print('[ERROR] Unknown option "' + opt + '"')
            print_help()

if __name__ == '__main__':
    main(sys.argv[1:])


from controller import ControllerAPI,BaseController
import math

class WiggleController(BaseController):
    """A controller that wiggles each of the robot's joints between their
    extrema"""
    def __init__(self,robot,period=2):
        self.robot = robot
        self.qmin,self.qmax = robot.getJointLimits()
        self.q = robot.getConfig()
        self.index = 0
        self.startTime = None
        self.period = period
    def output(self,**inputs):
        api = ControllerAPI(inputs)
        t = api.time()
        if self.startTime == None:
            self.startTime = t
        u = (t - self.startTime)/self.period
        #pick a good index
        while self.qmin[self.index]==self.qmax[self.index]:
            self.index += 1
            if self.index >= self.robot.numLinks():
                self.index = 0

        qdes = self.q[:]
        #wiggle between joint extrema
        if u < 0.25:
            s = math.sin(u*4*math.pi*0.5)
            qdes[self.index] += s*(self.qmax[self.index]-qdes[self.index])
            pass
        elif u < 0.75:
            s = (-math.sin(u*4*math.pi*0.5)+1.0)*0.5
            qdes[self.index] = self.qmax[self.index]+s*(self.qmin[self.index]-self.qmax[self.index])
        elif u < 1.0:
            s = math.sin(u*4*math.pi*0.5)+1.0
            qdes[self.index] = self.qmin[self.index]+s*(qdes[self.index]-self.qmin[self.index])
        else:
            #go to next index
            self.startTime = t
            self.index += 1
            if self.index >= self.robot.numLinks():
                self.index = 0
        return api.makePositionCommand(qdes)

    def signal(self,type,**inputs):
        if type=='reset':
            self.index = 0
            self.startTime = None

def make(robot):
    return WiggleController(robot)


# @Time    : 2019/3/2 9:27
# @Author  : Xu Huipeng
# @Blog    : https://brycexxx.github.io/

def merge_count(nums: list):
    inverse_degree = 0

    def partition(nums: list, low: int, high: int):
        if high - low == 1: return
        medium = low + ((high - low) >> 1)
        partition(nums, low, medium)
        partition(nums, medium, high)
        merge(nums, low, medium, high)

    def merge(nums: list, low: int, medium: int, high: int):
        nonlocal inverse_degree
        i, j, k = low, medium, 0
        temp = [0] * (high - low)
        while i < medium and j < high:
            if nums[i] <= nums[j]:
                temp[k] = nums[i]
                i += 1
                k += 1
            else:
                temp[k] = nums[j]
                j += 1
                k += 1
                inverse_degree += 1
        if i == medium:
            while j < high:
                temp[k] = nums[j]
                j += 1
                k += 1
        elif j == high:
            inverse_degree += (medium - i - 1) * (high - medium)
            while i < medium:
                temp[k] = nums[i]
                i += 1
                k += 1
        m = 0
        while m < len(temp):
            nums[low + m] = temp[m]
            m += 1

    partition(nums, 0, len(nums))
    return inverse_degree

if __name__ == "__main__":
    a = [6, 5, 4, 3, 2, 1]
    print(merge_count(a))


import copy
import json
from django.http import HttpResponse
import time
import calendar
import datetime

from reports_mjt.utils.decime import to_string

def utc(local_st):
    utc_st = time.localtime(local_st - 28800)
    result = time.strftime("%Y-%m-%d %H:%M:%S", utc_st)
    return result
day_now = time.localtime()
day_begin = '%d-%02d-01' % (day_now.tm_year, day_now.tm_mon)  # 月初肯定是1号
wday, monthRange = calendar.monthrange(day_now.tm_year, day_now.tm_mon)  # 得到本月的天数 第一返回为月第一日为星期几（0-6）, 第二返回为此月天数
day_end = '%d-%02d-%02d' % (day_now.tm_year, day_now.tm_mon, monthRange)

#获取各种时间戳
tday = datetime.date.today()
yesterday = tday - datetime.timedelta(days=1)
tomorrow = tday + datetime.timedelta(days=1)
weekday = tday - datetime.timedelta(days=6)
month = tday - datetime.timedelta(days=29)
months = tday - datetime.timedelta(days=89)
yesterday_start = int(time.mktime(time.strptime(str(yesterday), '%Y-%m-%d')))
yesterday_end = int(time.mktime(time.strptime(str(tday), '%Y-%m-%d'))) - 1
weekday_start = int(time.mktime(time.strptime(str(weekday), '%Y-%m-%d')))
month_start = int(time.mktime(time.strptime(str(month), '%Y-%m-%d')))
months_start = int(time.mktime(time.strptime(str(months), '%Y-%m-%d')))
today_start = yesterday_end + 1
today_end = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d'))) - 1
datedict = {
    'month': [utc(month_start), utc(today_end)],
    'week': [utc(weekday_start), utc(today_end)],
    'total': ['2018-11-25', utc(today_end)],
    'today': [utc(today_start), utc(today_end)],
    'yesday': [utc(yesterday_start), utc(yesterday_end)],
    'months': [utc(months_start), utc(today_end)],
    'day_begin': [day_begin, day_begin],
    'day_end': [day_end, day_end],
     }

def time_stamp():
    # 获取各种时间戳
    tday = datetime.date.today()
    yesterday = tday - datetime.timedelta(days=1)
    tomorrow = tday + datetime.timedelta(days=1)
    weekday = tday - datetime.timedelta(days=6)
    month = tday - datetime.timedelta(days=29)
    months = tday - datetime.timedelta(days=89)
    yesterday_start = int(time.mktime(time.strptime(str(yesterday), '%Y-%m-%d')))
    yesterday_end = int(time.mktime(time.strptime(str(tday), '%Y-%m-%d'))) - 1
    weekday_start = int(time.mktime(time.strptime(str(weekday), '%Y-%m-%d')))
    month_start = int(time.mktime(time.strptime(str(month), '%Y-%m-%d')))
    months_start = int(time.mktime(time.strptime(str(months), '%Y-%m-%d')))
    today_start = yesterday_end + 1
    today_end = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d'))) - 1
    datedict = {
        'month': [utc(month_start), utc(today_end)],
        'week': [utc(weekday_start), utc(today_end)],
        'total': ['2018-11-25', utc(today_end)],
        'today': [utc(today_start), utc(today_end)],
        'yesday': [utc(yesterday_start), utc(yesterday_end)],
        'months': [utc(months_start), utc(today_end)],
        'day_begin': [day_begin, day_begin],
        'day_end': [day_end, day_end],
    }
    return datedict

class FunctionObject(object):
    # 转换时间类型拼接字典
    def timedict(self, startTime, endTime):
        self.startTime = startTime
        self.endTime = endTime
        datestart = datetime.datetime.strptime(self.startTime, '%Y-%m-%d')
        dateend = datetime.datetime.strptime(self.endTime, '%Y-%m-%d')
        date_list = []
        datestart = datestart - datetime.timedelta(days=1)
        while datestart < dateend:
            datestart += datetime.timedelta(days=1)
            qu = datestart.strftime('%Y-%m-%d')
            date_list.append(qu)
        total_list = [0 for _ in range(len(date_list))]
        dict = {
            'date': date_list,
            'number': copy.deepcopy(total_list),
        }
        return dict

    def buytake(self, takeorder, buyorder, splilist, pages, numberbars):
        self.takeorder = takeorder
        self.buyorder = buyorder
        self.splilist = splilist
        self.pages = pages
        self.numberbars = numberbars
        for takekey, take in enumerate(self.takeorder):
            for key, id in enumerate(self.splilist):
                if take[0] == id['id']:
                    self.splilist[key]['tabkegoods'] = self.takeorder[takekey][1]
                    break
        for buykey, take in enumerate(self.buyorder):
            for key, id in enumerate(self.splilist):
                if take[0] == id['id']:
                    self.splilist[key]['purchase'] = self.buyorder[buykey][1]
                    self.splilist[key]['total'] = to_string(self.buyorder[buykey][2])
                    break
        dict = {'data': self.splilist[(self.pages - 1) * self.numberbars:self.pages * self.numberbars]}
        totalnum = len(self.splilist)
        dict['totalnum'] = totalnum
        data = {'data': dict, "errmsg": "成功", "errno": "0"}
        return data

    def startend(self, start_day, end_day):
        self.start_day = start_day
        self.end_day = end_day
        s_time = time.mktime(time.strptime(self.start_day, '%Y-%m-%d'))
        e_time = time.mktime(time.strptime(self.end_day, '%Y-%m-%d'))
        if (float(s_time) > float(e_time)):
            return HttpResponse(json.dumps({"errmsg": "订单统计时间顺序不对", "errno": "4001"}), content_type="application/json")
        return self.start_day, self.end_day

#时间戳转日期
def localutc(local_st):
    utc_st = time.localtime(local_st)
    result = time.strftime("%Y-%m-%d %H:%M:%S", utc_st)
    return result

#时间戳转日期
def yearmonthday(local_st):
    utc_st = time.localtime(local_st)
    result = time.strftime("%Y-%m-%d", utc_st)
    return result

#时间戳转日期
def monthtime(local_st):
    utc_st = time.localtime(local_st)
    result = time.strftime("%Y-%m", utc_st)
    return result

#时间戳转日期
def yeartime(local_st):
    utc_st = time.localtime(local_st)
    result = time.strftime("%Y", utc_st)
    return result

#时间戳转日期减去3小时
def userutc(local_st):
    usertime = datetime.datetime.strptime(local_st, "%Y-%m-%d %H:%M:%S")
    data = time.mktime(usertime.timetuple())-10800
    utc_st = time.localtime(data)
    result = time.strftime("%Y-%m-%d %H:%M:%S", utc_st)
    return result

#29100   633600
#代言券 已入账时间设置
def commission_price(local_st):
    utc_st = time.localtime(local_st - 633600)
    result = time.strftime("%Y-%m-%d %H:%M:%S", utc_st)
    return result

#字符串转时间戳
def str_time_stamp(data):
    timeArray = time.strptime(data, "%Y-%m-%d %H:%M:%S")
    timeStamp = int(time.mktime(timeArray))
    return timeStamp

#字符串转时间戳
def str_time_stamp_date(data):
    timeArray = time.strptime(data, "%Y-%m-%d")
    timeStamp = int(time.mktime(timeArray))
    return timeStamp


number = {
    "1": "One",
    "2": "Two",
    "3": "Three",
    "4": "Four",
    "5": "Five",
    "6": "Six",
    "7": "Seven",
    "8": "Eight",
    "9": "Nine",
    "0": "Zero"
}
input = input('10-Digit Phone Number:')
final = ""
for x in input:
    final += number.get(x) + " "
print(final)


# for letter in "Helo world":
#     print(letter)

import random

class LotteryMachine:
    #pull six random numbers 1 -> 49
    #check if number already pulled before returning

    def __iter__(self):
        #initial setup
        #return an object that implements __next__()
        self.numbers = []
        return self

    def __next__(self):
        # get the next value and return it
        if len(self.numbers) == 6:
            raise StopIteration
        else:
            number = random.randint(1, 49)
            while number in self.numbers:
                number = random.randint(1, 49)
            self.numbers.append(number)
            return number

machine = LotteryMachine()

for number in machine:
    print(number)


from __future__ import division
import sys;import math; import re; import subprocess; import os
from random import choice, randrange
import matplotlib.pyplot as plt; import numpy as np; import scipy as sc; from scipy import stats; import scipy.stats.distributions
import csv; import pandas as pd; import itertools; import Bio

'''def chooseseason(month):
    row = ''
    if 1 <= month <= 3:
        row = 'q1'
    elif 4 <= month <= 6:
        row = 'q2'
    elif 7 <= month <= 9:
        row = 'q3'
    elif 10 <= month <= 12:
        row = 'q4'
    else:
        row = "??"
    return row'''
def chooseseason(month):
    row = ''
    if 4 <= month <= 9:
        row = 'q1'
    elif 1 <= month <= 3 or 10 <= month <= 12:
        row = 'q2'
    else:
        row = "??"
    return row

def makets(thedf, OP):
    thedf['season'] = pd.Series([chooseseason(x) for x in thedf['month']], index=thedf.index)#add a column to the df with
    #month translated into season
    groups = thedf.groupby(['year', 'season'])#group the df by year and season
    temp = list(range(0, len(groups), 1))#make a list from 0 to number.of.groups
    qdf = pd.DataFrame(columns=['year', 'season'], index=temp)#intialize an empty df with the right column names
    for index,guy in enumerate(groups):#for each group in the grouped data
        qdf.loc[index]=[guy[0][0],guy[0][1]]#populate a row of the empty df with the year and season
    #for index, row in enumerate(qdf.iterrows()):
     #   row = [groups[index][0][0], groups[index][0][1]]
    qdf['Nminavg']=pd.Series([groups['netnh4'].mean()[x] for x in temp], index=qdf.index)#now add a column with avg
    #soil respiration for each year&season group

    OP.write(pd.DataFrame.to_csv(qdf))
    return qdf

#mydf = pd.read_csv("C:\\Users\\rmoge\\GitHub\\QB2017_DivPro\\Data\\HF_nmin.csv", infer_datetime_format=True)
mydf = pd.read_csv("C:\\Users\\rmoge\\GitHub\\QB2017_DivPro\\Data\\HF_nmin_NA.omit_RZM.csv", infer_datetime_format=True)
'''cdfna = mydf.loc[mydf['trt'] == "C"]
cdf=cdfna.loc[cdfna['netnh4']!="nan"]
hdf = mydf.loc[mydf['trt'] == "H" and mydf['netnh4'] != "NA"]
hndf = mydf.loc[mydf['trt'] == "HN" and mydf['netnh4'] != "NA"]
ndf = mydf.loc[mydf['trt'] == "N" and mydf['netnh4'] != "NA"]'''

cdf = mydf.loc[mydf['trt'] == "C"]
hdf = mydf.loc[mydf['trt'] == "H"]
hndf = mydf.loc[mydf['trt'] == "HN"]
ndf = mydf.loc[mydf['trt'] == "N"]
mincdf=cdf.loc[cdf['hor']=='mineral']
minhdf=hdf.loc[hdf['hor']=='mineral']
minhndf=hndf.loc[hndf['hor']=='mineral']
minndf=ndf.loc[ndf['hor']=='mineral']
orgcdf=cdf.loc[cdf['hor']=='organic']
orghdf=hdf.loc[hdf['hor']=='organic']
orghndf=hndf.loc[hndf['hor']=='organic']
orgndf=ndf.loc[ndf['hor']=='organic']

'''for i in (cdfna['netnh4']):
    print(i)
    print(type(i))
'''

try:
    orgNminc=open("C:\\Users\\rmoge\\GitHub\\QB2017_DivPro\\Data\\orgNminc.csv",'w')
    orgNminh=open("C:\\Users\\rmoge\\GitHub\\QB2017_DivPro\\Data\\orgNminh.csv",'w')
    orgNminhn=open("C:\\Users\\rmoge\\GitHub\\QB2017_DivPro\\Data\\orgNminhn.csv",'w')
    orgNminn=open("C:\\Users\\rmoge\\GitHub\\QB2017_DivPro\\Data\\orgNminn.csv",'w')
    minNminc=open("C:\\Users\\rmoge\\GitHub\\QB2017_DivPro\\Data\\minNminc.csv",'w')
    minNminh=open("C:\\Users\\rmoge\\GitHub\\QB2017_DivPro\\Data\\minNminh.csv",'w')
    minNminhn=open("C:\\Users\\rmoge\\GitHub\\QB2017_DivPro\\Data\\minNminhn.csv",'w')
    minNminn=open("C:\\Users\\rmoge\\GitHub\\QB2017_DivPro\\Data\\minNminn.csv",'w')

    orgc=makets(orgcdf,orgNminc); orgh=makets(orghdf,orgNminh); orghn=makets(orghndf,orgNminhn); orgn=makets(orgndf,orgNminn)
    minc=makets(mincdf,minNminc); minh=makets(minhdf,minNminh); minhn=makets(minhndf,minNminhn); minn=makets(minndf,minNminn)

finally:
    orgNminc.close(); orgNminh.close(), orgNminhn.close(), orgNminn.close()
    minNminc.close(); minNminh.close(), minNminhn.close(), minNminn.close()

'''grouped = ndf.groupby(['year', 'month'])
meanspermonth = grouped['co2flux'].mean()
tester = list(range(0, len(grouped), 1))#make a list from 0 to number.of.groups

ndfg = ndf.groupby(['year', 'month']).groups
type(ndf.groupby(['year', 'month']).groups)
sl = (ndf['month'])
ndff = ndf
ndff['e'] = pd.Series([chooseseason(x) for x in ndff['month']], index=ndff.index)

print(chooseseason(6))'''


from rl_framework.agent.q_learning import QLearningAgent
from rl_framework.environment.gym_environment import GymEnvironmentWrapper
from rl_framework.util.util import evaluate_agent

ENV_ID = "Taxi-v3"
MODEL_ARCHITECTURE = "QLearning"
PARALLEL_ENVIRONMENTS = 32

DOWNLOAD_EXISTING_AGENT = True
MODEL_NAME = f"{MODEL_ARCHITECTURE}-{ENV_ID}"
REPO_ID = f"zap-thamm/{MODEL_NAME}"
COMMIT_MESSAGE = f"Upload of a new agent trained with {MODEL_ARCHITECTURE} on {ENV_ID}"

N_TRAINING_EPISODES = 100000
N_EVALUATION_EPISODES = 100

LEARNING_RATE = 0.1
DISCOUNT_FACTOR = 0.99
MAX_EPSILON = 1.0
MIN_EPSILON = 0.05

if __name__ == "__main__":
    # Create environment
    environment = GymEnvironmentWrapper(ENV_ID)

    # Print some environment information (observation and action space)
    print("_____OBSERVATION SPACE_____ \n")
    print("Observation Space Shape", environment.observation_space.shape)
    print("Sample observation", environment.observation_space.sample())

    print("\n _____ACTION SPACE_____ \n")
    print("Action Space Shape", environment.action_space.n)
    print("Action Space Sample", environment.action_space.sample())

    print("\n _____REWARD RANGE_____ \n")
    print("Reward Range Interval", environment.reward_range)

    seeds = [
        16,
        54,
        165,
        177,
        191,
        191,
        120,
        80,
        149,
        178,
        48,
        38,
        6,
        125,
        174,
        73,
        50,
        172,
        100,
        148,
        146,
        6,
        25,
        40,
        68,
        148,
        49,
        167,
        9,
        97,
        164,
        176,
        61,
        7,
        54,
        55,
        161,
        131,
        184,
        51,
        170,
        12,
        120,
        113,
        95,
        126,
        51,
        98,
        36,
        135,
        54,
        82,
        45,
        95,
        89,
        59,
        95,
        124,
        9,
        113,
        58,
        85,
        51,
        134,
        121,
        169,
        105,
        21,
        30,
        11,
        50,
        65,
        12,
        43,
        82,
        145,
        152,
        97,
        106,
        55,
        31,
        85,
        38,
        112,
        102,
        168,
        123,
        97,
        21,
        83,
        158,
        26,
        80,
        63,
        5,
        81,
        32,
        11,
        28,
        148,
    ]

    # Create new agent
    agent = QLearningAgent(
        alpha=LEARNING_RATE,
        gamma=DISCOUNT_FACTOR,
        epsilon=MAX_EPSILON,
        epsilon_min=MIN_EPSILON,
        n_actions=environment.action_space.n,
        n_observations=environment.observation_space.n,
        randomize_q_table=False,
    )

    if DOWNLOAD_EXISTING_AGENT:
        agent.download_from_huggingface_hub(repository_id=REPO_ID, filename="q-learning.pkl")

    else:
        # Train agent
        agent.train(training_environments=[environment], n_episodes=N_TRAINING_EPISODES)

    mean_reward, std_reward = evaluate_agent(agent=agent,
                                             evaluation_environment=environment,
                                             n_eval_episodes=N_EVALUATION_EPISODES,
                                             seeds=seeds)
    print(f"Mean_reward={mean_reward:.2f} +/- {std_reward:.2f}")

    # model_dictionary = {
    #     "env_id": ENV_ID,
    #     "max_steps": 99,
    #     "n_training_episodes": N_TRAINING_EPISODES,
    #     "n_eval_episodes": N_EVALUATION_EPISODES,
    #     "eval_seed": seeds,
    #     "learning_rate": LEARNING_RATE,
    #     "gamma": DISCOUNT_FACTOR,
    #     "max_epsilon": MAX_EPSILON,
    #     "min_epsilon": MIN_EPSILON,
    #     "qtable": agent.q_table
    # }
    #
    # agent.upload_to_huggingface_hub(repository_id=REPO_ID,
    #                                 environment=environment,
    #                                 environment_name=ENV_ID,
    #                                 evaluation_seeds=seeds,
    #                                 model_dictionary=model_dictionary)


import unittest
import import_ipynb
import pandas as pd
import pandas.testing as pd_testing
import numpy.testing as np_testing
import os
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics

class Test(unittest.TestCase):

    def _dirname_if_file(self, filename):
        if os.path.isdir(filename):
            return filename
        else:
            return os.path.dirname(os.path.abspath(filename))

    def setUp(self):
        import Exercise1_04
        self.exercise = Exercise1_04

        dirname = self._dirname_if_file('../data/OSI_feats_e3.csv')
        self.feats_loc = os.path.join(dirname, 'OSI_feats_e3.csv')
        self.target_loc = os.path.join(dirname, 'OSI_target_e2.csv')

        self.feats = pd.read_csv(self.feats_loc)
        self.target = pd.read_csv(self.target_loc)

        test_size = 0.2
        random_state = 42
        self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
            self.feats, self.target, test_size=test_size, random_state=random_state)
        model = LogisticRegression(random_state=42, max_iter=10000)
        model.fit(self.X_train, self.y_train['Revenue'])
        self.y_pred = model.predict(self.X_test)

        self.accuracy = metrics.accuracy_score(y_pred=self.y_pred, y_true=self.y_test)

    def test_feats_df(self):
        pd_testing.assert_frame_equal(self.exercise.feats, self.feats)

    def test_target_df(self):
        pd_testing.assert_frame_equal(self.exercise.target, self.target)

    def test_X_train_df(self):
        pd_testing.assert_frame_equal(self.exercise.X_train, self.X_train)

    def test_y_train_df(self):
        pd_testing.assert_frame_equal(self.exercise.y_train, self.y_train)

    def test_X_test_df(self):
        pd_testing.assert_frame_equal(self.exercise.X_test, self.X_test)

    def test_y_test_df(self):
        pd_testing.assert_frame_equal(self.exercise.y_test, self.y_test)

    def test_accuracy(self):
        assert self.exercise.accuracy == self.accuracy

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


from math import sin
from color import random_hue, hue_to_color, random_color, black, gradient_wheel, rgb_to_hsv
from random import random, randint, choice

import HelperFunctions as helpfunc

def get_cell(coord):
    h, x, y = coord
    x = int(x)
    y = int(y)
    x_half = int(x / 2)

    if x == 0:
        return h, 0, y
    elif x > 0:

        hex1 = helpfunc.hex_in_direction((h, 0, y), 1, x_half + (x % 2))
        return helpfunc.hex_in_direction(hex1, 0, x_half)
    else:
        x *= -1
        hex1 = helpfunc.hex_in_direction((h, 0, y), 3, x_half + (x % 2))
        return helpfunc.hex_in_direction(hex1, 4, x_half)

def get_box(coord, width, height):
    """Returns a list of cells within an box (x,y coord of the lower left corner)"""
    h, x, y = coord
    return [get_cell((h, x + dx, y + dy)) for dx in range(int(width)) for dy in range(int(height))]

def get_triangle(coord, length, width):
    """Returns a list of cells within an upright triangle or trapezoid
    (x,y coord of the lower left corner), length of edge, width of trapezoid (0 = triangle)"""
    current = coord
    cells = []

    for w in range(width):
        for l in range(length):
            cells.append(helpfunc.hex_in_direction(current, 1, l))  # 2 is up
        length -= 1
        length = max([length, 0])
        current = helpfunc.hex_in_direction(current, 1, 1)
        current = helpfunc.hex_in_direction(current, 0, 1)

    return cells

class Stuff(object):
    def __init__(self, hexmodel, h):
        """Subclass all this!"""
        self.hexes = hexmodel
        self.h = h  # hex_number
        self.x_coord = 7.0  # right off the board
        self.y_coord = 0.0  # default changed by subclass
        self.speed = 0

    def draw_stuff(self, clock):
        """Subclass!"""
        return

    def move_stuff(self, car_speed):
        stuff_speed = (self.y_coord + car_speed) / 7.0
        self.x_coord -= stuff_speed
        return self.x_coord >= -20

    def get_level(self):
        return self.y_coord + 6

    def draw_line(self, start, direction, length, color):
        self.hexes.set_cells(helpfunc.hex_line_in_direction(coord=start, direction=direction, distance=length), color)

class Tree(Stuff):
    def __init__(self, hexmodel, h):
        super(Tree, self).__init__(hexmodel, h)
        self.y_coord = randint(0, 5)
        self.color = rgb_to_hsv((0, randint(100, 255), 0))  # Some sort of green
        self.trunk = rgb_to_hsv((randint(40, 60), 0, 0))  # Dark Brown

    def draw_stuff(self, clock):
        pos = get_cell((self.h, self.x_coord, self.y_coord))

        # Draw leaves
        leaf_top = helpfunc.hex_in_direction(pos, 2, 2)

        self.hexes.set_cell(leaf_top, self.color)  # Draw the center
        self.hexes.set_cells(helpfunc.hex_ring(leaf_top, 1), self.color)  # Tree bulb
        if self.y_coord > 2:
            self.hexes.set_cells(helpfunc.hex_ring(leaf_top, 2), self.color)  # Bigger bulb

        self.hexes.set_cell(helpfunc.hex_in_direction(pos, 2, 1), self.trunk)
        self.hexes.set_cell(pos, self.trunk)

class RedTree(Tree):
    def __init__(self, hexmodel, h):
        super(RedTree, self).__init__(hexmodel, h)
        red = randint(200, 255)
        self.color = rgb_to_hsv((randint(200, 255), randint(0, red), 0))  # Some sort of yellow/red

class Mountain(Stuff):
    def __init__(self, hexmodel, h):
        super(Mountain, self).__init__(hexmodel, h)
        self.y_coord = 0
        self.gray = randint(50, 100)
        self.size = randint(4, 10)

    def draw_stuff(self, clock):
        pos = get_cell((self.h, self.x_coord, self.y_coord))
        color = rgb_to_hsv((self.gray, self.gray, self.gray))

        self.hexes.set_cells(get_triangle(pos, self.size, self.size), color)

class House(Stuff):
    def __init__(self, hexmodel, h):
        super(House, self).__init__(hexmodel, h)
        self.y_coord = randint(0, 6)
        self.roof = random_color()
        self.wall = random_color()

    def draw_stuff(self, clock):
        pos = get_cell((self.h, self.x_coord, self.y_coord))

        width = int((self.y_coord / 2) + 2)
        height = int((self.y_coord / 2) + 1)
        self.hexes.set_cells(get_box(pos, width, height), self.wall)

        hex1 = helpfunc.hex_in_direction(pos, 2, height)
        roof_corner = helpfunc.hex_in_direction(hex1, 4, 1)
        self.hexes.set_cells(get_triangle(roof_corner, height+1, width), self.roof)

class Bird(Stuff):
    def __init__(self, hexmodel, h):
        super(Bird, self).__init__(hexmodel, h)
        self.y_coord = randint(-6, 6)
        self.color = black()
        self.x_dir = randint(-1, 1)
        self.y_dir = randint(-1, 1)

    def draw_stuff(self, clock):
        pos = get_cell((self.h, self.x_coord, self.y_coord))

        self.hexes.set_cell(pos, self.color)  # Draw the center

        if clock % 2:
            self.hexes.set_cell(helpfunc.hex_in_direction(pos, 0, 1), self.color)
            self.hexes.set_cell(helpfunc.hex_in_direction(pos, 4, 1), self.color)
        else:
            self.hexes.set_cell(helpfunc.hex_in_direction(pos, 3, 1), self.color)
            self.hexes.set_cell(helpfunc.hex_in_direction(pos, 1, 1), self.color)

    def get_level(self):
        return 12  # Override here draws birds on top of everything

    def move_stuff(self, car_speed):
        stuff_speed = 0.5
        self.x_coord -= stuff_speed
        self.x_coord -= (self.x_dir / 2.0)
        self.y_coord -= (self.y_dir / 2.0)

        if helpfunc.one_in(6):
            self.x_dir = randint(-1, 1)
            self.y_dir = randint(-1, 1)

        return self.x_coord >= -20

class Cloud(Stuff):
    def __init__(self, hexmodel, h):
        super(Cloud, self).__init__(hexmodel, h)
        self.y_coord = randint(-6, -2)
        self.gray = randint(220, 255)
        self.wind = 0.1

    def draw_stuff(self, clock):
        pos = get_cell((self.h, self.x_coord, self.y_coord))
        color = rgb_to_hsv((self.gray, self.gray, self.gray))

        # Draw two lines
        self.hexes.set_cells(helpfunc.hex_line_in_direction(pos, 1, 1), color)
        hex1 = helpfunc.hex_in_direction(pos, 0, 1)
        self.hexes.set_cells(helpfunc.hex_line_in_direction(hex1, 1, 1), color)

    def move_stuff(self, car_speed):
        self.x_coord -= self.wind
        return self.x_coord >= -10

class Driving(object):
    def __init__(self, hexmodel):
        self.name = "Driving"
        self.hexes = hexmodel
        self.stuffs = []  # List that holds stuffs objects
        self.sky_color = black()  # Set for each season
        self.ground_color = rgb_to_hsv((255, 255, 0))  # Set for each season
        self.cars_speed = 1.0  # How fast the scene moves
        self.clock = 0
        self.scene = 0
        self.max_scene = 6

    def next_frame(self):

        self.set_colors()

        while True:

            self.generate_stuffs()
            self.move_stuffs()
            self.draw_ground()
            self.draw_stuffs()

            self.clock += 1
            if self.clock % 1000 == 0:
                self.scene += 1
                self.scene = self.scene % self.max_scene
                self.set_colors()
            if self.clock > 10000:
                self.clock = 0
            self.cars_speed = sin(self.clock / (64 * 3.14159)) + 1.0

            yield 0.05

    def move_stuffs(self):
        for stuff in self.stuffs:
            if not stuff.move_stuff(self.cars_speed):
                self.stuffs.remove(stuff)

    def draw_stuffs(self):
        # draw further away things first
        for distance in range(12):
            for stuff in self.stuffs:
                if stuff.get_level() == distance:
                    stuff.draw_stuff(self.clock)

    def draw_ground(self):
        self.hexes.set_all_cells(self.sky_color)
        for h in helpfunc.all_hexes():
            self.hexes.set_cells(get_box((h, -5, 1), 11, 6), self.ground_color)

    def generate_stuffs(self):
        kinds = [Tree, RedTree, Mountain, House, Bird, Cloud]

        freq = [ [10,0,2,2,0,0],
                 [1,10,5,0,2,0],
                 [2,0,1,10,0,0],
                 [0,3,10,0,20,0],
                 [5,0,0,2,0,0],
                ]

        for h in helpfunc.all_hexes():
            for obj in range(len(kinds)):
                if randint(0, 100) < freq[self.scene][obj]:
                    if kinds[obj] in [House]:
                        self.stuffs.append(kinds[obj](self.hexes, h))

        #if len(self.stuffs) < 20:
        #	if randint(0,5) == 1:
        #		new_mountain = Mountain(self.hexes)
        #		self.stuffs.append(new_mountain)
        #	else:
        #		new_tree = Bird(self.hexes)
        #		self.stuffs.append(new_tree)

    def set_colors(self):

        skies = [(0,0,200), (50,50,255), (0,0,255), (0,0,100), (100,100,255)]
        grounds = [(255,255,0), (200,255,0), (0,25,0), (200,200,0), (100,200,0)]

        self.sky_color = rgb_to_hsv(skies[self.scene])
        self.ground_color = rgb_to_hsv(grounds[self.scene])


import re

def expand_rule(num, rules):
    rule = rules[num]
    if re.match(r'[a-zA-Z|]+$', rule) is not None:
        return rule

    rule = rule.split(" ")
    for i, subrule in enumerate(rule):
        if subrule.isnumeric():
            rule[i] =  expand_rule(int(subrule), rules)
    return "(" + "".join(rule) + ")"

with open("input.txt") as file:
    data = file.read().split("\n\n")

rules = {}
for line in data[0].split("\n"):
    num, rule = line.split(": ")
    rules[int(num)] = rule.replace('"', "")

count = 0
pattern = re.compile("^" + expand_rule(0, rules) + "$")
messages = data[1].split("\n")
for msg in messages:
    if re.match(pattern, msg) is not None:
        count += 1

print(count)


from werkzeug import url_decode

class HTTPMethodOverrideMiddleware(object):
    """The HTTPMethodOverrideMiddleware middleware implements the hidden HTTP
    method technique. Not all web browsers support every HTTP method, such as
    DELETE and PUT. This middleware class allows clients to provide a method
    override parameter via an HTTP header value or a querystring parameter. This
    middleware will look for the header paramter first followed by the
    querystring. The default HTTP header name is `X-HTTP-METHOD-OVERRIDE` and
    the default querystring parameter name is `__METHOD__`. These can be changed
    via the constructor parameters `header_name` and `querystring_param`
    respectively. Additionally, a list of allowed HTTP methods may be specified
    via the `allowed_methods` constructor parameter. The default allowed methods
    are GET, HEAD, POST, DELETE, PUT, PATCH, and OPTIONS.
    """

    bodyless_methods = frozenset(['GET', 'HEAD', 'OPTIONS', 'DELETE'])

    def __init__(self, app, header_name=None,
                 querystring_param=None, allowed_methods=None):
        header_name = header_name or 'X-HTTP-METHOD-OVERRIDE'

        self.app = app
        self.header_name = 'HTTP_' + header_name.replace('-', '_')
        self.querystring_param = querystring_param or '__METHOD__'
        self.allowed_methods = frozenset(allowed_methods or
            ['GET', 'HEAD', 'POST', 'DELETE', 'PUT', 'PATCH', 'OPTIONS'])

    def _get_from_querystring(self, environ):
        if self.querystring_param in environ.get('QUERY_STRING', ''):
            args = url_decode(environ['QUERY_STRING'])
            return args.get(self.querystring_param)
        return None

    def _get_method_override(self, environ):
        return environ.get(self.header_name, None) or \
               self._get_from_querystring(environ) or ''

    def __call__(self, environ, start_response):
        method = self._get_method_override(environ).upper()

        if method in self.allowed_methods:
            method = method.encode('ascii', 'replace')
            environ['REQUEST_METHOD'] = method

        if method in self.bodyless_methods:
            environ['CONTENT_LENGTH'] = '0'

        return self.app(environ, start_response)


from django.http import JsonResponse
from django.core.paginator import Paginator
from .models import Post,Classify
def post(request):
    '''
    根据页数，排序方式，所属分类来返回文章列表页，
    json格式返回
    p:页码
    c:所属分类id
    s:排序方式 1：按默认推荐排序，2：按时间排序，3：按欢迎度排序
    '''
    p = request.GET.get('p')
    c = request.GET.get('c')
    s = request.GET.get('s')
    if p is None:
        p = 1
    if c is None:
        c = 0
    if s is None:
        s = 1
    p=int(p)
    c=int(c)
    s=int(s)
    if c == 0:
        if s == 1:  # 按默认推荐排序
            posts = Post.my_objects.active()
        if s == 2:
            posts = Post.my_objects.active().order_by('-publish')
        if s ==3:
            posts = Post.my_objects.active().order_by('-click')

    paginator = Paginator(posts, per_page=5)
    page = paginator.page(p)

    l =[ post.seri() for post in page]

    d = {'posts':l}
    return JsonResponse(d)

def classify(request):
    u = request.GET.get('u')
    c_l= Classify.objects.filter(owner_id=int(u))

    l = [c.seri() for c in c_l]
    d = {'data':l}
    return JsonResponse(d)

def get_posts_by_cid(request,c):
    uid = request.session.get('uid')
    c = int(c)
    posts = Post.my_objects.filter(author_id=uid).filter(isDelete=False).filter(classify_id=c)
    l = [ p.seri() for p in posts]
    d = {'data':l}
    return  JsonResponse(d)

def get_post(request,pid):
    pid=int(pid)
    post = Post.my_objects.get(id=pid)
    d = post.seri()
    return JsonResponse(d)


"""training.py

Helper functions for running experiments.
Author: Alex Kyllo
Date: 2021-06-06
"""
import logging
import os
from datetime import datetime
from pathlib import Path

from tensorflow.keras import callbacks

from musiclearn import processing, vae_models

LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)

def build_callbacks(exp_path: os.PathLike, patience: int = 10):
    """Build an array of callbacks for model training."""
    os.makedirs(exp_path, exist_ok=True)
    cbacks = [
        callbacks.ModelCheckpoint(
            exp_path / "checkpoints",
            save_weights_only=True,
            monitor="val_loss",
            save_best_only=True,
        ),
        callbacks.EarlyStopping(
            monitor="val_loss",
            patience=patience,
            verbose=1,
            restore_best_weights=True,
        ),
        callbacks.CSVLogger(exp_path / "history.csv", separator=",", append=True),
        callbacks.TensorBoard(log_dir=exp_path / "tensorboard", histogram_freq=1),
    ]

    return cbacks

def log_start(exp_name, model_name, **hparams):
    LOG.info(f"Running experiment {exp_name} for model {model_name} with hparams: {hparams}")

def log_end(exp_name, model_name, start_time):
    end_time = datetime.now()
    duration = end_time - start_time
    LOG.info(f"Experiment {exp_name} for model {model_name} finished in {duration}")

def train_mtvae(
    exp_name,
    model_class,
    ticks_per_beat,
    beats_per_phrase,
    epochs,
    batch_size,
    learning_rate,
    lstm_units,
    latent_dim,
    embedding_dim,
    dropout_rate,
    gru,
    bidirectional,
    patience=10,
    augment=False,
):
    """Train the MultiTrack VAE once with a given set of hyperparameters."""
    # String quartet MIDI programs
    x = processing.get_string_quartets(ticks_per_beat)
    if augment:
        x = processing.augment(x)
    mtvae = model_class(
        lstm_units,
        embedding_dim,
        latent_dim,
        batch_size,
        learning_rate,
        dropout_rate,
        gru,
        bidirectional,
    )
    model_name = type(mtvae).__name__
    start_time = datetime.now()
    start_str = start_time.strftime("%Y-%m-%dT%H:%M:%S")
    exp_path = Path(f"experiments/{exp_name}/{start_str}")
    cbacks = build_callbacks(exp_path, patience=patience)
    log_start(
        exp_name,
        model_name,
        ticks_per_beat=ticks_per_beat,
        beats_per_phrase=beats_per_phrase,
        epochs=epochs,
        batch_size=batch_size,
        learning_rate=learning_rate,
        lstm_units=lstm_units,
        latent_dim=latent_dim,
        embedding_dim=embedding_dim,
        dropout_rate=dropout_rate,
        patience=patience,
        gru=gru,
        bidirectional=bidirectional,
        augment=augment,
    )
    mtvae.train(x, ticks_per_beat, beats_per_phrase, epochs, callbacks=cbacks)
    log_end(exp_name, model_name, start_time)
    mtvae.save(exp_path / "saved_model")
    LOG.info(f"Model configuration saved to {exp_path / 'saved_model'}")
    return mtvae


cor = str(input("Digite uma cor[AMARELO | VERDE | VERMELHO]:")).upper().strip()
red1 = "VERMELHO"
red2 = "VERMELHA"
yellow1 = "AMARELO"
yellow2 = "AMARELA"
green= "VERDE"

if cor == red1 or cor == red2:
    print("PARE!!!")
elif cor == yellow1 or cor == yellow2:
    print("MANTENHA ATENÇÃO!!")
elif cor == green:
    print("SIGA EM FRENTE!")
else:
    print("COR INVÁLIDA")

Módulo de Lógica de Programação_SUPER/Aula_02_Turma_315_fullstack_INFINITY_SCHOOL/semaforo_if_else.py


'''

Initilizing maze and point-based features
No walls, no collision with features
Known correspondence which means all features have unique labels
screen visualization
'''
import pygame
from visualization import *
from shapely.geometry import *

#### Variables ####
map_width = 500
map_height = 600
borders = [Point(20, 20), Point(20, map_height-20), Point(map_width-20, map_height-20), Point(map_width-20, 20), Point(20, 20)]
wall1 = [Point(20, 100), Point(map_width-150, 100)]
wall2 = [Point(map_width-20, 200), Point(200, 200), Point(200, map_height-120)]
wall3 = [Point(map_width-150, map_height-20), Point(map_width-150, 300)]
maze = [borders, wall1, wall2, wall3]
features = []
[[features.append(j) for j in i] for i in maze]

testfeatures = [Point(20,map_height-20)]
beacons = [features]

def draw_borders(win):
    for i in range(len(borders) - 1):
        pygame.draw.line(win, (255, 0, 0), (int(borders[i].x), int(borders[i].y)),
                         (int(borders[i + 1].x), int(borders[i + 1].y)), 5)

def draw_walls(win):
    #### Wall1 ####
    for i in range(len(wall1)-1):
        pygame.draw.line(win, (255, 0, 0), (int(wall1[i].x), int(wall1[i].y)),
                         (int(wall1[i + 1].x), int(wall1[i + 1].y)), 2)
    #### Wall2 ####
    for i in range(len(wall2)-1):
        pygame.draw.line(win, (255, 0, 0), (int(wall2[i].x), int(wall2[i].y)),
                         (int(wall2[i + 1].x), int(wall2[i + 1].y)), 2)
    #### Wall3 ####
    for i in range(len(wall3)-1):
        pygame.draw.line(win, (255, 0, 0), (int(wall3[i].x), int(wall3[i].y)),
                         (int(wall3[i + 1].x), int(wall3[i + 1].y)), 2)

def draw_beacons(win):
    for i in range(len(features)):
        pygame.draw.circle(win, (0,140,140), (int(features[i].x), int(features[i].y)), 5)

def update_screen(win, bot_c, angle, radius, deltaT=30):

    ### Draw robot ###
    pygame.draw.circle(win, (0, 255, 0), (int(bot_c.x), int(bot_c.y)), radius)
    pygame.draw.line(win, (255, 255, 0), (int(bot_c.x), int(bot_c.y)),
                     (bot_c.x + radius * -np.cos(angle), (bot_c.y + radius * np.sin(angle))),
                     int(radius / 10))
    pygame.display.update()
    pygame.time.delay(deltaT)

def draw_predict_trail(win, index, start, end,width):
    pygame.draw.aaline(win, (252, 3, 236), start, end, width)

def draw_true_trail(win, index, start, end, width):
    pygame.draw.aaline(win, (0, 0, 0), start, end, width)

def quit_screen(): #TOBE TEST
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()