#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#name: Yusen Wu
#Email: wuyusen@bu.edu
#Assigment: 8
#File Description: This file contains a set of user defined class to build a option pricing
#table and calculate the implied volatility under a specific price of the option

from a8task1 import *

def generate_option_value_table(s, x, t, sigma, rf, div):
    """
    generate a printout to illustrate the change in option prices with r
    espect to the change in the underlying stock price
    """
    #Initialize call and put option for testing
    call = BSMEuroCallOption(s, x, t, sigma, rf, div)
    put = BSMEuroPutOption(s, x, t, sigma, rf, div)

    #print out the headlines
    print(call)
    print(put)
    print('\n')

    #Iterate over each possible price to print put undelaying call and put value
    print('Change in option values w.r.t. change in stock price:')
    print('  price        call value        put value        call delta        put delta')
    for price in range(s - 10, s + 11):
      call.s = price
      put.s = price
      print(f'${call.s:>7.2f}{call.value():>15.4f}{put.value():>18.4f}{call.delta():>17.4f}{put.delta():>18.4f}')

def calculate_implied_volatility(option, value, accuracy = 0.001):
    """
    calculate the implied volatility of an observed option
    """
    assert isinstance(option, BSMOption)
    upper = 1
    lower = 0

    #a bisection algorithm to find suitable value
    while True:
      test_rate = (upper + lower)/2
      option.sigma = test_rate
      test_value = option.value()

      #if test value is accurate then return
      if abs(test_value - value) <= accuracy:
        return test_rate

      #else adjust the searching range
      else:
        if test_value > value:
          upper = test_rate
        else:
          lower = test_rate

Bond Utils/Black Scholes Option Pricing/a8task2.py


import pygame
from settings import Settings
# from blob import Blob
import game_function as gf
from pygame.sprite import Group

def run_game():
    # Инициализирует pygame, settings и создает объект ээкрана
    pygame.init()
    settings = Settings()
    screen = pygame.display.set_mode((
                                      settings.screen_width,
                                      settings.screen_height))
    pygame.display.set_caption("Дождь")
    # Создание группы капель
    blobs = Group()
    # Создание ряда капель
    gf.create_blobs_line(settings, screen, blobs)

    # Запуск основного цикла игры.
    while True:
        # Отслеживание событий клавиатуры и мыши
        gf.check_events()
        gf.update_blobs(settings, screen, blobs)
        gf.update_screen(settings, screen, blobs)

run_game()


from enum import Enum
import functools
import weakref
import numpy as np

from tensorflow.python.compat import compat
from tensorflow.python.distribute import distribute_lib
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor
from tensorflow.python.framework import tensor_conversion
from tensorflow.python.keras import backend
from tensorflow.python.keras.utils import losses_utils
from tensorflow.python.keras.utils import tf_utils
from tensorflow.python.keras.utils.generic_utils import to_list
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import array_ops_stack
from tensorflow.python.ops import check_ops
from tensorflow.python.ops import clip_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import gen_math_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import nn_ops
from tensorflow.python.ops import variables as variables_module
from tensorflow.python.ops import weights_broadcast_ops
from tensorflow.python.ops.parallel_for import control_flow_ops as parallel_control_flow_ops
from tensorflow.python.ops.ragged import ragged_tensor
from tensorflow.python.util import tf_decorator

NEG_INF = -1e10

class Reduction(Enum):
  """Types of metrics reduction.

  Contains the following values:

  * `SUM`: Scalar sum of weighted values.
  * `SUM_OVER_BATCH_SIZE`: Scalar sum of weighted values divided by
        number of elements.
  * `WEIGHTED_MEAN`: Scalar sum of weighted values divided by sum of weights.
  """
  SUM = 'sum'
  SUM_OVER_BATCH_SIZE = 'sum_over_batch_size'
  WEIGHTED_MEAN = 'weighted_mean'

def update_state_wrapper(update_state_fn):
  """Decorator to wrap metric `update_state()` with `add_update()`.

  Args:
    update_state_fn: function that accumulates metric statistics.

  Returns:
    Decorated function that wraps `update_state_fn()` with `add_update()`.
  """

  def decorated(metric_obj, *args, **kwargs):
    """Decorated function with `add_update()`."""
    strategy = distribute_lib.get_strategy()

    for weight in metric_obj.weights:
      if (backend.is_tpu_strategy(strategy) and
          not strategy.extended.variable_created_in_scope(weight)
          and not distribute_lib.in_cross_replica_context()):
        raise ValueError(
            'Trying to run metric.update_state in replica context when '
            'the metric was not created in TPUStrategy scope. '
            'Make sure the keras Metric is created in TPUstrategy scope. ')

    with tf_utils.graph_context_for_symbolic_tensors(*args, **kwargs):
      update_op = update_state_fn(*args, **kwargs)
    if update_op is not None:  # update_op will be None in eager execution.
      metric_obj.add_update(update_op)
    return update_op

  return tf_decorator.make_decorator(update_state_fn, decorated)

def result_wrapper(result_fn):
  """Decorator to wrap metric `result()` function in `merge_call()`.

  Result computation is an idempotent operation that simply calculates the
  metric value using the state variables.

  If metric state variables are distributed across replicas/devices and
  `result()` is requested from the context of one device - This function wraps
  `result()` in a distribution strategy `merge_call()`. With this,
  the metric state variables will be aggregated across devices.

  Args:
    result_fn: function that computes the metric result.

  Returns:
    Decorated function that wraps `result_fn()` in distribution strategy
    `merge_call()`.
  """

  def decorated(metric_obj, *args):
    """Decorated function with merge_call."""
    has_strategy = distribute_lib.has_strategy()
    replica_context = distribute_lib.get_replica_context()

    # The purpose of using `merge_call` to call `result()` is to trigger cross
    # replica aggregation of metric state variables (SyncOnReadVariable). After
    # we introduced `variable_sync_on_read_context`, in principle there is no
    # need to use `merge_call` here. However the branch still exists because:
    #
    # 1. Keras V1 training code sometimes assumes `result_t` is the same tensor
    #    across replicas (achieved by `merge_call`). With
    #    `variable_sync_on_read_context` each replica gets their own tensors
    #    residing on replica's device, thus breaking the assumption.
    # 2. Keras c/fit creates a tf.function (a.k.a, train_function) that returns
    #    the metric values of the first replica. With
    #    `variable_sync_on_read_context` since each replica gets their own
    #    tensors, the metric result tensors on the non-first replicas are not in
    #    the return value of train_function, making TF graph optimizer prune the
    #    branch that computes and aggregates those metric results. As a result,
    #    if NCCL is used to do the aggregation, the program will hang because
    #    NCCL ops are only launched on the non-pruned first replica.
    #
    # We condition on strategy.extended._use_merge_call() since we know if it is
    # false, the program uses `jit_compile` to compile replica fn, meaning it is
    # not V1 training (hence #1 is okay), and no pruning will happen as
    # compiled functions are not inlined (hence #2 is okay).

    if (not has_strategy or replica_context is None or
        not distribute_lib.get_strategy(
        ).extended._use_merge_call()):
      with distribute_lib.variable_sync_on_read_context():
        raw_result = result_fn(*args)
        # Results need to be wrapped in a `tf.identity` op to ensure
        # correct execution order.
        if isinstance(raw_result,
                      (tensor.Tensor, variables_module.Variable, float, int)):
          result_t = array_ops.identity(raw_result)
        elif isinstance(raw_result, dict):
          result_t = {
              key: array_ops.identity(value)
              for key, value in raw_result.items()
          }
        else:
          try:
            result_t = array_ops.identity(raw_result)
          except (ValueError, TypeError):
            raise RuntimeError(
                'The output of `metric.result()` can only be a single '
                'Tensor/Variable, or a dict of Tensors/Variables. '
                'For metric %s, got result %s.' % (metric_obj.name, raw_result))
    else:
      # TODO(psv): Test distribution of metrics using different distribution
      # strategies.

      # Creating a wrapper for merge_fn. merge_call invokes the given merge_fn
      # with distribution object as the first parameter. We create a wrapper
      # here so that the result function need not have that parameter.
      def merge_fn_wrapper(distribution, merge_fn, *args):
        # We will get `PerReplica` merge function. Taking the first one as all
        # are identical copies of the function that we had passed below.
        result = distribution.experimental_local_results(merge_fn)[0](*args)

        # Wrapping result in identity so that control dependency between
        # update_op from `update_state` and result works in case result returns
        # a tensor.
        return array_ops.identity(result)

      # Wrapping result in merge_call. merge_call is used when we want to leave
      # replica mode and compute a value in cross replica mode.
      result_t = replica_context.merge_call(
          merge_fn_wrapper, args=(result_fn,) + args)

    # We are saving the result op here to be used in train/test execution
    # functions. This basically gives the result op that was generated with a
    # control dep to the updates for these workflows.
    metric_obj._call_result = result_t
    return result_t

  return tf_decorator.make_decorator(result_fn, decorated)

def weakmethod(method):
  """Creates a weak reference to the bound method."""

  cls = method.im_class
  func = method.im_func
  instance_ref = weakref.ref(method.im_self)

  @functools.wraps(method)
  def inner(*args, **kwargs):
    return func.__get__(instance_ref(), cls)(*args, **kwargs)

  del method
  return inner

def assert_thresholds_range(thresholds):
  if thresholds is not None:
    invalid_thresholds = [t for t in thresholds if t is None or t < 0 or t > 1]
    if invalid_thresholds:
      raise ValueError(
          'Threshold values must be in [0, 1]. Invalid values: {}'.format(
              invalid_thresholds))

def parse_init_thresholds(thresholds, default_threshold=0.5):
  if thresholds is not None:
    assert_thresholds_range(to_list(thresholds))
  thresholds = to_list(default_threshold if thresholds is None else thresholds)
  return thresholds

class ConfusionMatrix(Enum):
  TRUE_POSITIVES = 'tp'
  FALSE_POSITIVES = 'fp'
  TRUE_NEGATIVES = 'tn'
  FALSE_NEGATIVES = 'fn'

class AUCCurve(Enum):
  """Type of AUC Curve (ROC or PR)."""
  ROC = 'ROC'
  PR = 'PR'

  @staticmethod
  def from_str(key):
    if key in ('pr', 'PR'):
      return AUCCurve.PR
    elif key in ('roc', 'ROC'):
      return AUCCurve.ROC
    else:
      raise ValueError('Invalid AUC curve value "%s".' % key)

class AUCSummationMethod(Enum):
  """Type of AUC summation method.

  https://en.wikipedia.org/wiki/Riemann_sum)

  Contains the following values:
  * 'interpolation': Applies mid-point summation scheme for `ROC` curve. For
    `PR` curve, interpolates (true/false) positives but not the ratio that is
    precision (see Davis & Goadrich 2006 for details).
  * 'minoring': Applies left summation for increasing intervals and right
    summation for decreasing intervals.
  * 'majoring': Applies right summation for increasing intervals and left
    summation for decreasing intervals.
  """
  INTERPOLATION = 'interpolation'
  MAJORING = 'majoring'
  MINORING = 'minoring'

  @staticmethod
  def from_str(key):
    if key in ('interpolation', 'Interpolation'):
      return AUCSummationMethod.INTERPOLATION
    elif key in ('majoring', 'Majoring'):
      return AUCSummationMethod.MAJORING
    elif key in ('minoring', 'Minoring'):
      return AUCSummationMethod.MINORING
    else:
      raise ValueError('Invalid AUC summation method value "%s".' % key)

def _update_confusion_matrix_variables_optimized(
    variables_to_update,
    y_true,
    y_pred,
    thresholds,
    multi_label=False,
    sample_weights=None,
    label_weights=None,
    thresholds_with_epsilon=False):
  """Update confusion matrix variables with memory efficient alternative.

  Note that the thresholds need to be evenly distributed within the list, eg,
  the diff between consecutive elements are the same.

  To compute TP/FP/TN/FN, we are measuring a binary classifier
    C(t) = (predictions >= t)
  at each threshold 't'. So we have
    TP(t) = sum( C(t) * true_labels )
    FP(t) = sum( C(t) * false_labels )

  But, computing C(t) requires computation for each t. To make it fast,
  observe that C(t) is a cumulative integral, and so if we have
    thresholds = [t_0, ..., t_{n-1}];  t_0 < ... < t_{n-1}
  where n = num_thresholds, and if we can compute the bucket function
    B(i) = Sum( (predictions == t), t_i <= t < t{i+1} )
  then we get
    C(t_i) = sum( B(j), j >= i )
  which is the reversed cumulative sum in tf.cumsum().

  We can compute B(i) efficiently by taking advantage of the fact that
  our thresholds are evenly distributed, in that
    width = 1.0 / (num_thresholds - 1)
    thresholds = [0.0, 1*width, 2*width, 3*width, ..., 1.0]
  Given a prediction value p, we can map it to its bucket by
    bucket_index(p) = floor( p * (num_thresholds - 1) )
  so we can use tf.math.unsorted_segment_sum() to update the buckets in one
  pass.

  Consider following example:
  y_true = [0, 0, 1, 1]
  y_pred = [0.1, 0.5, 0.3, 0.9]
  thresholds = [0.0, 0.5, 1.0]
  num_buckets = 2   # [0.0, 1.0], (1.0, 2.0]
  bucket_index(y_pred) = tf.math.floor(y_pred * num_buckets)
                       = tf.math.floor([0.2, 1.0, 0.6, 1.8])
                       = [0, 0, 0, 1]
  # The meaning of this bucket is that if any of the label is true,
  # then 1 will be added to the corresponding bucket with the index.
  # Eg, if the label for 0.2 is true, then 1 will be added to bucket 0. If the
  # label for 1.8 is true, then 1 will be added to bucket 1.
  #
  # Note the second item "1.0" is floored to 0, since the value need to be
  # strictly larger than the bucket lower bound.
  # In the implementation, we use tf.math.ceil() - 1 to achieve this.
  tp_bucket_value = tf.math.unsorted_segment_sum(true_labels, bucket_indices,
                                                 num_segments=num_thresholds)
                  = [1, 1, 0]
  # For [1, 1, 0] here, it means there is 1 true value contributed by bucket 0,
  # and 1 value contributed by bucket 1. When we aggregate them to together,
  # the result become [a + b + c, b + c, c], since large thresholds will always
  # contribute to the value for smaller thresholds.
  true_positive = tf.math.cumsum(tp_bucket_value, reverse=True)
                = [2, 1, 0]

  This implementation exhibits a run time and space complexity of O(T + N),
  where T is the number of thresholds and N is the size of predictions.
  Metrics that rely on standard implementation instead exhibit a complexity of
  O(T * N).

  Args:
    variables_to_update: Dictionary with 'tp', 'fn', 'tn', 'fp' as valid keys
      and corresponding variables to update as values.
    y_true: A floating point `Tensor` whose shape matches `y_pred`. Will be cast
      to `bool`.
    y_pred: A floating point `Tensor` of arbitrary shape and whose values are in
      the range `[0, 1]`.
    thresholds: A sorted floating point `Tensor` with value in `[0, 1]`.
      It need to be evenly distributed (the diff between each element need to be
      the same).
    multi_label: Optional boolean indicating whether multidimensional
      prediction/labels should be treated as multilabel responses, or flattened
      into a single label. When True, the valus of `variables_to_update` must
      have a second dimension equal to the number of labels in y_true and
      y_pred, and those tensors must not be RaggedTensors.
    sample_weights: Optional `Tensor` whose rank is either 0, or the same rank
      as `y_true`, and must be broadcastable to `y_true` (i.e., all dimensions
      must be either `1`, or the same as the corresponding `y_true` dimension).
    label_weights: Optional tensor of non-negative weights for multilabel
      data. The weights are applied when calculating TP, FP, FN, and TN without
      explicit multilabel handling (i.e. when the data is to be flattened).
    thresholds_with_epsilon: Optional boolean indicating whether the leading and
      tailing thresholds has any epsilon added for floating point imprecisions.
      It will change how we handle the leading and tailing bucket.

  Returns:
    Update op.
  """
  num_thresholds = thresholds.shape.as_list()[0]

  if sample_weights is None:
    sample_weights = 1.0
  else:
    sample_weights = weights_broadcast_ops.broadcast_weights(
        math_ops.cast(sample_weights, dtype=y_pred.dtype), y_pred)
    if not multi_label:
      sample_weights = array_ops.reshape(sample_weights, [-1])
  if label_weights is None:
    label_weights = 1.0
  else:
    label_weights = array_ops.expand_dims(label_weights, 0)
    label_weights = weights_broadcast_ops.broadcast_weights(label_weights,
                                                            y_pred)
    if not multi_label:
      label_weights = array_ops.reshape(label_weights, [-1])
  weights = math_ops.multiply(sample_weights, label_weights)

  # We shouldn't need this, but in case there are predict value that is out of
  # the range of [0.0, 1.0]
  y_pred = clip_ops.clip_by_value(y_pred,
                                  clip_value_min=0.0, clip_value_max=1.0)

  y_true = math_ops.cast(math_ops.cast(y_true, dtypes.bool), y_true.dtype)
  if not multi_label:
    y_true = array_ops.reshape(y_true, [-1])
    y_pred = array_ops.reshape(y_pred, [-1])

  true_labels = math_ops.multiply(y_true, weights)
  false_labels = math_ops.multiply((1.0 - y_true), weights)

  # Compute the bucket indices for each prediction value.
  # Since the predict value has to be strictly greater than the thresholds,
  # eg, buckets like [0, 0.5], (0.5, 1], and 0.5 belongs to first bucket.
  # We have to use math.ceil(val) - 1 for the bucket.
  bucket_indices = math_ops.ceil(y_pred * (num_thresholds - 1)) - 1

  if thresholds_with_epsilon:
    # In this case, the first bucket should actually take into account since
    # the any prediction between [0.0, 1.0] should be larger than the first
    # threshold. We change the bucket value from -1 to 0.
    bucket_indices = nn_ops.relu(bucket_indices)

  bucket_indices = math_ops.cast(bucket_indices, dtypes.int32)

  if multi_label:
    # We need to run bucket segment sum for each of the label class. In the
    # multi_label case, the rank of the label is 2. We first transpose it so
    # that the label dim becomes the first and we can parallel run though them.
    true_labels = array_ops.transpose_v2(true_labels)
    false_labels = array_ops.transpose_v2(false_labels)
    bucket_indices = array_ops.transpose_v2(bucket_indices)

    def gather_bucket(label_and_bucket_index):
      label, bucket_index = label_and_bucket_index[0], label_and_bucket_index[1]
      return math_ops.unsorted_segment_sum(
          data=label, segment_ids=bucket_index, num_segments=num_thresholds)
    tp_bucket_v = parallel_control_flow_ops.vectorized_map(
        gather_bucket, (true_labels, bucket_indices))
    fp_bucket_v = parallel_control_flow_ops.vectorized_map(
        gather_bucket, (false_labels, bucket_indices))
    tp = array_ops.transpose_v2(
        math_ops.cumsum(tp_bucket_v, reverse=True, axis=1))
    fp = array_ops.transpose_v2(
        math_ops.cumsum(fp_bucket_v, reverse=True, axis=1))
  else:
    tp_bucket_v = math_ops.unsorted_segment_sum(
        data=true_labels, segment_ids=bucket_indices,
        num_segments=num_thresholds)
    fp_bucket_v = math_ops.unsorted_segment_sum(
        data=false_labels, segment_ids=bucket_indices,
        num_segments=num_thresholds)
    tp = math_ops.cumsum(tp_bucket_v, reverse=True)
    fp = math_ops.cumsum(fp_bucket_v, reverse=True)

  # fn = sum(true_labels) - tp
  # tn = sum(false_labels) - fp
  if (ConfusionMatrix.TRUE_NEGATIVES in variables_to_update or
      ConfusionMatrix.FALSE_NEGATIVES in variables_to_update):
    if multi_label:
      total_true_labels = math_ops.reduce_sum(true_labels, axis=1)
      total_false_labels = math_ops.reduce_sum(false_labels, axis=1)
    else:
      total_true_labels = math_ops.reduce_sum(true_labels)
      total_false_labels = math_ops.reduce_sum(false_labels)

  update_ops = []
  if ConfusionMatrix.TRUE_POSITIVES in variables_to_update:
    variable = variables_to_update[ConfusionMatrix.TRUE_POSITIVES]
    update_ops.append(variable.assign_add(tp))
  if ConfusionMatrix.FALSE_POSITIVES in variables_to_update:
    variable = variables_to_update[ConfusionMatrix.FALSE_POSITIVES]
    update_ops.append(variable.assign_add(fp))
  if ConfusionMatrix.TRUE_NEGATIVES in variables_to_update:
    variable = variables_to_update[ConfusionMatrix.TRUE_NEGATIVES]
    tn = total_false_labels - fp
    update_ops.append(variable.assign_add(tn))
  if ConfusionMatrix.FALSE_NEGATIVES in variables_to_update:
    variable = variables_to_update[ConfusionMatrix.FALSE_NEGATIVES]
    fn = total_true_labels - tp
    update_ops.append(variable.assign_add(fn))
  return control_flow_ops.group(update_ops)

def is_evenly_distributed_thresholds(thresholds):
  """Check if the thresholds list is evenly distributed.

  We could leverage evenly distributed thresholds to use less memory when
  calculate metrcis like AUC where each individual threshold need to be
  evaluted.

  Args:
    thresholds: A python list or tuple, or 1D numpy array whose value is ranged
      in [0, 1].

  Returns:
    boolean, whether the values in the inputs are evenly distributed.
  """
  # Check the list value and see if it is evenly distributed.
  num_thresholds = len(thresholds)
  if num_thresholds < 3:
    return False
  even_thresholds = np.arange(num_thresholds,
                              dtype=np.float32) / (num_thresholds - 1)
  return np.allclose(thresholds, even_thresholds, atol=backend.epsilon())

def update_confusion_matrix_variables(variables_to_update,
                                      y_true,
                                      y_pred,
                                      thresholds,
                                      top_k=None,
                                      class_id=None,
                                      sample_weight=None,
                                      multi_label=False,
                                      label_weights=None,
                                      thresholds_distributed_evenly=False):
  """Returns op to update the given confusion matrix variables.

  For every pair of values in y_true and y_pred:

  true_positive: y_true == True and y_pred > thresholds
  false_negatives: y_true == True and y_pred <= thresholds
  true_negatives: y_true == False and y_pred <= thresholds
  false_positive: y_true == False and y_pred > thresholds

  The results will be weighted and added together. When multiple thresholds are
  provided, we will repeat the same for every threshold.

  For estimation of these metrics over a stream of data, the function creates an
  `update_op` operation that updates the given variables.

  If `sample_weight` is `None`, weights default to 1.
  Use weights of 0 to mask values.

  Args:
    variables_to_update: Dictionary with 'tp', 'fn', 'tn', 'fp' as valid keys
      and corresponding variables to update as values.
    y_true: A `Tensor` whose shape matches `y_pred`. Will be cast to `bool`.
    y_pred: A floating point `Tensor` of arbitrary shape and whose values are in
      the range `[0, 1]`.
    thresholds: A float value, float tensor, python list, or tuple of float
      thresholds in `[0, 1]`, or NEG_INF (used when top_k is set).
    top_k: Optional int, indicates that the positive labels should be limited to
      the top k predictions.
    class_id: Optional int, limits the prediction and labels to the class
      specified by this argument.
    sample_weight: Optional `Tensor` whose rank is either 0, or the same rank as
      `y_true`, and must be broadcastable to `y_true` (i.e., all dimensions must
      be either `1`, or the same as the corresponding `y_true` dimension).
    multi_label: Optional boolean indicating whether multidimensional
      prediction/labels should be treated as multilabel responses, or flattened
      into a single label. When True, the valus of `variables_to_update` must
      have a second dimension equal to the number of labels in y_true and
      y_pred, and those tensors must not be RaggedTensors.
    label_weights: (optional) tensor of non-negative weights for multilabel
      data. The weights are applied when calculating TP, FP, FN, and TN without
      explicit multilabel handling (i.e. when the data is to be flattened).
    thresholds_distributed_evenly: Boolean, whether the thresholds are evenly
      distributed within the list. An optimized method will be used if this is
      the case. See _update_confusion_matrix_variables_optimized() for more
      details.

  Returns:
    Update op.

  Raises:
    ValueError: If `y_pred` and `y_true` have mismatched shapes, or if
      `sample_weight` is not `None` and its shape doesn't match `y_pred`, or if
      `variables_to_update` contains invalid keys.
  """
  if multi_label and label_weights is not None:
    raise ValueError('`label_weights` for multilabel data should be handled '
                     'outside of `update_confusion_matrix_variables` when '
                     '`multi_label` is True.')
  if variables_to_update is None:
    return
  if not any(
      key for key in variables_to_update if key in list(ConfusionMatrix)):
    raise ValueError(
        'Please provide at least one valid confusion matrix '
        'variable to update. Valid variable key options are: "{}". '
        'Received: "{}"'.format(
            list(ConfusionMatrix), variables_to_update.keys()))

  variable_dtype = list(variables_to_update.values())[0].dtype

  y_true = math_ops.cast(y_true, dtype=variable_dtype)
  y_pred = math_ops.cast(y_pred, dtype=variable_dtype)

  if thresholds_distributed_evenly:
    # Check whether the thresholds has any leading or tailing epsilon added
    # for floating point imprecision. The leading and tailing threshold will be
    # handled bit differently as the corner case.
    # At this point, thresholds should be a list/array with more than 2 items,
    # and ranged between [0, 1]. See is_evenly_distributed_thresholds() for more
    # details.
    thresholds_with_epsilon = thresholds[0] < 0.0 or thresholds[-1] > 1.0

  thresholds = tensor_conversion.convert_to_tensor_v2_with_dispatch(
      thresholds, dtype=variable_dtype
  )
  num_thresholds = thresholds.shape.as_list()[0]

  if multi_label:
    one_thresh = math_ops.equal(
        math_ops.cast(1, dtype=dtypes.int32),
        array_ops.rank(thresholds),
        name='one_set_of_thresholds_cond')
  else:
    [y_pred,
     y_true], _ = ragged_assert_compatible_and_get_flat_values([y_pred, y_true],
                                                               sample_weight)
    one_thresh = math_ops.cast(True, dtype=dtypes.bool)

  invalid_keys = [
      key for key in variables_to_update if key not in list(ConfusionMatrix)
  ]
  if invalid_keys:
    raise ValueError(
        'Invalid keys: {}. Valid variable key options are: "{}"'.format(
            invalid_keys, list(ConfusionMatrix)))

  with ops.control_dependencies([
      check_ops.assert_greater_equal(
          y_pred,
          math_ops.cast(0.0, dtype=y_pred.dtype),
          message='predictions must be >= 0'),
      check_ops.assert_less_equal(
          y_pred,
          math_ops.cast(1.0, dtype=y_pred.dtype),
          message='predictions must be <= 1')
  ]):
    if sample_weight is None:
      y_pred, y_true = losses_utils.squeeze_or_expand_dimensions(
          y_pred, y_true)
    else:
      sample_weight = math_ops.cast(sample_weight, dtype=variable_dtype)
      y_pred, y_true, sample_weight = (
          losses_utils.squeeze_or_expand_dimensions(
              y_pred, y_true, sample_weight=sample_weight))
  y_pred.shape.assert_is_compatible_with(y_true.shape)

  if top_k is not None:
    y_pred = _filter_top_k(y_pred, top_k)
  if class_id is not None:
    y_true = y_true[..., class_id]
    y_pred = y_pred[..., class_id]

  if thresholds_distributed_evenly and compat.forward_compatible(2021, 6, 8):
    # The new approach will take effect after 2021/6/8, to give enough time
    # for Brella release to pick up the new op tf.math.cumsum with float32.
    return _update_confusion_matrix_variables_optimized(
        variables_to_update, y_true, y_pred, thresholds,
        multi_label=multi_label, sample_weights=sample_weight,
        label_weights=label_weights,
        thresholds_with_epsilon=thresholds_with_epsilon)

  pred_shape = array_ops.shape(y_pred)
  num_predictions = pred_shape[0]
  if y_pred.shape.ndims == 1:
    num_labels = 1
  else:
    num_labels = gen_math_ops.Prod(input=pred_shape[1:], axis=0)
  thresh_label_tile = array_ops.where_v2(one_thresh, num_labels,
                                         array_ops.ones([], dtype=dtypes.int32))

  # Reshape predictions and labels, adding a dim for thresholding.
  if multi_label:
    predictions_extra_dim = array_ops.expand_dims(y_pred, 0)
    labels_extra_dim = array_ops.expand_dims(
        math_ops.cast(y_true, dtype=dtypes.bool), 0)
  else:
    # Flatten predictions and labels when not multilabel.
    predictions_extra_dim = array_ops.reshape(y_pred, [1, -1])
    labels_extra_dim = array_ops.reshape(
        math_ops.cast(y_true, dtype=dtypes.bool), [1, -1])

  # Tile the thresholds for every prediction.
  if multi_label:
    thresh_pretile_shape = [num_thresholds, 1, -1]
    thresh_tiles = [1, num_predictions, thresh_label_tile]
    data_tiles = [num_thresholds, 1, 1]
  else:
    thresh_pretile_shape = [num_thresholds, -1]
    thresh_tiles = [1, num_predictions * num_labels]
    data_tiles = [num_thresholds, 1]

  thresh_tiled = array_ops.tile(
      array_ops.reshape(thresholds, thresh_pretile_shape),
      array_ops_stack.stack(thresh_tiles))

  # Tile the predictions for every threshold.
  preds_tiled = array_ops.tile(predictions_extra_dim, data_tiles)

  # Compare predictions and threshold.
  pred_is_pos = math_ops.greater(preds_tiled, thresh_tiled)

  # Tile labels by number of thresholds
  label_is_pos = array_ops.tile(labels_extra_dim, data_tiles)

  if sample_weight is not None:
    sample_weight = weights_broadcast_ops.broadcast_weights(
        math_ops.cast(sample_weight, dtype=variable_dtype), y_pred)
    weights_tiled = array_ops.tile(
        array_ops.reshape(sample_weight, thresh_tiles), data_tiles)
  else:
    weights_tiled = None

  if label_weights is not None and not multi_label:
    label_weights = array_ops.expand_dims(label_weights, 0)
    label_weights = weights_broadcast_ops.broadcast_weights(label_weights,
                                                            y_pred)
    label_weights_tiled = array_ops.tile(
        array_ops.reshape(label_weights, thresh_tiles), data_tiles)
    if weights_tiled is None:
      weights_tiled = label_weights_tiled
    else:
      weights_tiled = math_ops.multiply(weights_tiled, label_weights_tiled)

  update_ops = []

  def weighted_assign_add(label, pred, weights, var):
    label_and_pred = math_ops.cast(
        math_ops.logical_and(label, pred), dtype=var.dtype)
    if weights is not None:
      label_and_pred *= math_ops.cast(weights, dtype=var.dtype)
    return var.assign_add(math_ops.reduce_sum(label_and_pred, 1))

  loop_vars = {
      ConfusionMatrix.TRUE_POSITIVES: (label_is_pos, pred_is_pos),
  }
  update_tn = ConfusionMatrix.TRUE_NEGATIVES in variables_to_update
  update_fp = ConfusionMatrix.FALSE_POSITIVES in variables_to_update
  update_fn = ConfusionMatrix.FALSE_NEGATIVES in variables_to_update

  if update_fn or update_tn:
    pred_is_neg = math_ops.logical_not(pred_is_pos)
    loop_vars[ConfusionMatrix.FALSE_NEGATIVES] = (label_is_pos, pred_is_neg)

  if update_fp or update_tn:
    label_is_neg = math_ops.logical_not(label_is_pos)
    loop_vars[ConfusionMatrix.FALSE_POSITIVES] = (label_is_neg, pred_is_pos)
    if update_tn:
      loop_vars[ConfusionMatrix.TRUE_NEGATIVES] = (label_is_neg, pred_is_neg)

  for matrix_cond, (label, pred) in loop_vars.items():

    if matrix_cond in variables_to_update:
      update_ops.append(
          weighted_assign_add(label, pred, weights_tiled,
                              variables_to_update[matrix_cond]))

  return control_flow_ops.group(update_ops)

def _filter_top_k(x, k):
  """Filters top-k values in the last dim of x and set the rest to NEG_INF.

  Used for computing top-k prediction values in dense labels (which has the same
  shape as predictions) for recall and precision top-k metrics.

  Args:
    x: tensor with any dimensions.
    k: the number of values to keep.

  Returns:
    tensor with same shape and dtype as x.
  """
  _, top_k_idx = nn_ops.top_k(x, k, sorted=False)
  top_k_mask = math_ops.reduce_sum(
      array_ops.one_hot(top_k_idx, array_ops.shape(x)[-1], axis=-1), axis=-2)
  return x * top_k_mask + NEG_INF * (1 - top_k_mask)

def ragged_assert_compatible_and_get_flat_values(values, mask=None):
  """If ragged, it checks the compatibility and then returns the flat_values.

     Note: If two tensors are dense, it does not check their compatibility.
     Note: Although two ragged tensors with different ragged ranks could have
           identical overall rank and dimension sizes and hence be compatible,
           we do not support those cases.
  Args:
     values: A list of potentially ragged tensor of the same ragged_rank.
     mask: A potentially ragged tensor of the same ragged_rank as elements in
       Values.

  Returns:
     A tuple in which the first element is the list of tensors and the second
     is the mask tensor. ([Values], mask). Mask and the element in Values
     are equal to the flat_values of the input arguments (if they were ragged).
  """
  if isinstance(values, list):
    is_all_ragged = \
        all(isinstance(rt, ragged_tensor.RaggedTensor) for rt in values)
    is_any_ragged = \
        any(isinstance(rt, ragged_tensor.RaggedTensor) for rt in values)
  else:
    is_all_ragged = isinstance(values, ragged_tensor.RaggedTensor)
    is_any_ragged = is_all_ragged
  if (is_all_ragged and
      ((mask is None) or isinstance(mask, ragged_tensor.RaggedTensor))):
    to_be_stripped = False
    if not isinstance(values, list):
      values = [values]
      to_be_stripped = True

    # NOTE: we leave the flat_values compatibility to
    # tf.TensorShape `assert_is_compatible_with`
    # check if both dynamic dimensions are equal and then use the flat_values.
    nested_row_split_list = [rt.nested_row_splits for rt in values]
    assertion_list = _assert_splits_match(nested_row_split_list)

    # if both are ragged sample_weights also should be ragged with same dims.
    if isinstance(mask, ragged_tensor.RaggedTensor):
      assertion_list_for_mask = _assert_splits_match(
          [nested_row_split_list[0], mask.nested_row_splits])
      with ops.control_dependencies(assertion_list_for_mask):
        mask = array_ops.expand_dims(mask.flat_values, -1)

    # values has at least 1 element.
    flat_values = []
    for value in values:
      with ops.control_dependencies(assertion_list):
        flat_values.append(array_ops.expand_dims(value.flat_values, -1))

    values = flat_values[0] if to_be_stripped else flat_values

  elif is_any_ragged:
    raise TypeError('One of the inputs does not have acceptable types.')
  # values are empty or value are not ragged and mask is ragged.
  elif isinstance(mask, ragged_tensor.RaggedTensor):
    raise TypeError('Ragged mask is not allowed with non-ragged inputs.')

  return values, mask

def _assert_splits_match(nested_splits_lists):
  """Checks that the given splits lists are identical.

  Performs static tests to ensure that the given splits lists are identical,
  and returns a list of control dependency op tensors that check that they are
  fully identical.

  Args:
    nested_splits_lists: A list of nested_splits_lists, where each split_list is
      a list of `splits` tensors from a `RaggedTensor`, ordered from outermost
      ragged dimension to innermost ragged dimension.

  Returns:
    A list of control dependency op tensors.
  Raises:
    ValueError: If the splits are not identical.
  """
  error_msg = 'Inputs must have identical ragged splits'
  for splits_list in nested_splits_lists:
    if len(splits_list) != len(nested_splits_lists[0]):
      raise ValueError(error_msg)
  return [
      check_ops.assert_equal(s1, s2, message=error_msg)  # pylint: disable=g-complex-comprehension
      for splits_list in nested_splits_lists[1:]
      for (s1, s2) in zip(nested_splits_lists[0], splits_list)
  ]


import discord, time, datetime, os
from discord.ext import tasks
import requests

from dotenv import load_dotenv
load_dotenv()

print('StalkerNews discord bot \n')

def get_variable(var_name: str) -> bool: # dotenv_smart_bool.py
    TRUE_=('true', '1', 't') # Add more entries if you want, like: `y`, `yes`, ...
    FALSE_=('false', '0', 'f')
    value = os.getenv(var_name, 'False').lower()  # return `False` if variable is not set. To raise an error, change `'False'` to `None`
    if value not in TRUE_ + FALSE_:
        raise ValueError(f'Invalid value `{value}` for variable `{var_name}`')
    return value in TRUE_

def get_timestamp():
    return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

# TELEGRAM
TOKEN_TELEGRAM_BOT = str(os.getenv('TOKEN_TELEGRAM_BOT'))
CHANNEL_CHAT_ID = str(os.getenv('CHANNEL_CHAT_ID'))

debug = get_variable('DEBUG')
is_mobile = get_variable(os.getenv('IS_MOBILE'))
autoclose = get_variable('AUTOCLOSE_MODE')

if debug: print('DEBUG IS ON!!1!')
if is_mobile: print('MOBILE MODE')
if debug: print('loaded dotenv')

TOKEN_DISCORD = os.getenv('DISCORD_TOKEN')
CANALE_NEWS = int(os.getenv('CHANNEL_NEWS_ID'))
MOD_ID = int(os.getenv('MOD_ID'))
INTERVAL_CHECK_TIME = int(os.getenv('INTERVAL_CHECK_TIME'))
extra_message = str(os.getenv('EXTRA_MESSAGE'))

class MyClient(discord.Client):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.extra_message = str(os.getenv('EXTRA_MESSAGE'))
        # self.counter = 0 # an attribute we can access from our task

    async def setup_hook(self) -> None:
        if debug: print('init pusherNews task')
        self.checkNews.start() # start the task to run in the background

    async def on_ready(self):
        print(f'Logged in as {self.user} (ID: {self.user.id})')
        print('------')

    @tasks.loop(seconds=INTERVAL_CHECK_TIME)  # task runs every 5000 seconds
    async def checkNews(self):
        if debug: print('\n*********\nBegin new loop\n*********\n')
        channel_news = self.get_channel(CANALE_NEWS)  # channel ID goes here

        await channel_news.send(extra_message) # invia su discord

        #invia su telegram
        url = f"https://api.telegram.org/bot{TOKEN_TELEGRAM_BOT}/sendMessage?chat_id={CHANNEL_CHAT_ID}&text={self.extra_message}"
        requests.get(url)
        if autoclose:
            if not is_mobile: quit()

    @checkNews.before_loop
    async def before_my_task(self):
        await self.wait_until_ready()  # wait until the bot logs in

client = MyClient(intents=discord.Intents.default())
client.run(TOKEN_DISCORD)


import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as st
import pymc3 as pm
import arviz as az
import pandas as pd
import scipy.linalg as sp
from scipy.special import boxcox, inv_boxcox
import seaborn as sns
print('Running on PyMC3 v{}'.format(pm.__version__))
import theano
from sklearn.model_selection import train_test_split
import shelve
import warnings

#matplotlib inline
sns.set()
warnings.filterwarnings('ignore')

## ==== Functions ==== ##
def nuts_advi(X, y, ofp, y_dist, opt, test_size=0.33):

    k = X.shape[1]
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=42)
    y_tensor = theano.shared(y_train.astype('float64'))
    X_tensor = theano.shared(X_train.astype('float64'))

    with pm.Model() as base_model:

        # Intercept term
        β0 = pm.Normal('β0', mu=0, sd=1e5)

        # Beta coefficients for predictor variables
        β = pm.MvNormal('β', mu=np.zeros(k), cov=np.eye(k), shape=k)

        # Calcuate mean from the normal variables, and add intercept
        mu = pm.math.dot(X_tensor,β) + β0

        # Pass the mu and beta with the observed data
        if y_dist == 'Gumbel':
            # Gumbel distribution
            beta = pm.HalfCauchy('beta', 1e5)
            y_likelihood = pm.Gumbel('fMSE', mu=mu, beta=beta, observed=y_tensor)
        elif y_dist == 'Beta':
            # Beta distribution
            # Intercept term
            β0x = pm.Normal('β0x', mu=0, sd=1e5)

            # Beta coefficients for predictor variables
            βx = pm.MvNormal('βx', mu=np.zeros(k), cov=np.eye(k), shape=k)

            # Calcuate mean from the normal variables, and add intercept
            mux = pm.math.dot(X_tensor,βx) + β0x

            y_likelihood = pm.Beta('fMSE', alpha=np.abs(mu)+1/1e10, beta=np.abs(mux)+1/1e10, observed=y_tensor)
        #start = pm.find_MAP()

    #pm.model_to_graphviz(base_model)

    if opt == 'nuts':
        with base_model:
        # Variational inference with ADVI optimization
            step       = pm.NUTS(target_accept=0.95)
            trace_nuts = pm.sample(draws=4000, step=step, tune=1000, cores=4, init='adapt_diag')
            idata_nuts = az.from_pymc3(trace_nuts)

        filename = ofp + '_nuts.out'
        my_shelf = shelve.open(filename, 'n')
        my_shelf['base_model'] = base_model
        my_shelf['trace_nuts'] = trace_nuts
        my_shelf['idata_nuts'] = idata_nuts
        my_shelf['X_tensor']   = X_tensor
        my_shelf['y_tensor']   = y_tensor
        my_shelf['X_train']    = X_train
        my_shelf['y_train']    = y_train
        my_shelf['X_test']     = X_test
        my_shelf['y_test']     = y_test
        my_shelf.close()

    elif opt == 'advi':
        map_tensor_batch = {y_tensor: pm.Minibatch(y_train, 1000), X_tensor: pm.Minibatch(X_train, 1000)}

        with base_model:
             fit_advi= pm.fit(method=pm.ADVI(), n=1000000, more_replacements = map_tensor_batch)

        trace_advi = fit_advi.sample(10000)

        filename = ofp + '_advi.out'
        my_shelf = shelve.open(filename, 'n')
        my_shelf['base_model'] = base_model
        my_shelf['fit_advi']   = fit_advi
        my_shelf['trace_advi'] = trace_advi
        my_shelf['X_tensor']   = X_tensor
        my_shelf['y_tensor']   = y_tensor
        my_shelf['X_train']    = X_train
        my_shelf['y_train']    = y_train
        my_shelf['X_test']     = X_test
        my_shelf['y_test']     = y_test
        my_shelf.close()

## ==== data load ==== ##
# load the data sets
pr_file   = "/home/hyung/Lab/data/DL_Error/predictors.csv"
res_file  = "/home/hyung/Lab/data/DL_Error/responses.csv"
pr_data   = pd.read_csv(pr_file)
res_data  = pd.read_csv(res_file)

# Rainf_f/Precip/SWdown_min have some issue because they are all zeros
pr_data.drop(columns=['Rainf_min', 'Rainf_f_min', 'Rainf_f_max','Rainf_f_tavg', 'TotalPrecip_min'], inplace=True)
pr_data.drop(columns=['Evap_min', 'Evap_max', 'Evap_tavg'], inplace=True)
pr_data.drop(columns=['LWdown_f_max', 'LWdown_f_min', 'LWdown_f_tavg'], inplace=True)
pr_data.drop(columns=['Qair_f_max',	'Qair_f_min', 'Qh_max',	'Qh_min'], inplace=True)
pr_data.drop(columns=['Qle_min', 'Qle_max', 'Qle_tavg'], inplace=True)
pr_data.drop(columns=['SWdown_f_min', 'SWdown_f_max', 'SWdown_f_tavg'], inplace=True)
pr_data.drop(columns=['SMOS_RFI_min', 'SoilMoist_max', 'SoilMoist_min',	'SoilMoist_tavg'], inplace=True)
pr_data.drop(columns=['Tair_f_max', 'Tair_f_min', 'Tair_f_tavg', 'aspect'], inplace=True)
pr_data.drop(columns=['Wind_f_max',	'Wind_f_min', 'Wind_f_tavg'], inplace=True)
pr_data.drop(columns=['LAI_min', 'LAI_max','Greenness_min', 'Greenness_max', 'AvgSurfT_min', 'AvgSurfT_max'],inplace=True)
pr_data.drop(columns=['SoilTemp_min', 'SoilTemp_max','RadT_min', 'RadT_max'],inplace=True)
pr_data.drop(columns=['SMAP_vo_min', 'SMAP_vo_max','SMAP_rc_min', 'SMAP_rc_max'],inplace=True)
pr_data.drop(columns=['albedo_max', 'albedo_min','albedo_std', 'TotalPrecip_max','Rainf_max','SMOS_RFI_max'],inplace=True)

# TC estimations with std value larger than 0.2 might be unstable
std_thred     = 0.1
mask_std_A2   = res_data['AMSR2_std'] <= std_thred
mask_std_AS   = res_data['ASCAT_std'] <= std_thred
mask_std_SMOS = res_data['SMOS_std'] <= std_thred
mask_std_SMAP = res_data['SMAP_std'] <= std_thred

# 2 clean the data sets
selected_predictors = list(pr_data.columns.values)

sel_A2   = selected_predictors.copy()
sel_A2.append('AMSR2_fMSE')
sel_AS   = selected_predictors.copy()
sel_AS.append('ASCAT_fMSE')
sel_SMOS = selected_predictors.copy()
sel_SMOS.append('SMOS_fMSE')
sel_SMAP = selected_predictors.copy()
sel_SMAP.append('SMAP_fMSE')

A2_fMSE   = pr_data.join(res_data['AMSR2_fMSE'])[sel_A2]
AS_fMSE   = pr_data.join(res_data['ASCAT_fMSE'])[sel_AS]
SMOS_fMSE = pr_data.join(res_data['SMOS_fMSE'])[sel_SMOS]
SMAP_fMSE = pr_data.join(res_data['SMAP_fMSE'])[sel_SMAP]

# select fMSE <= threshold
A2_fMSE   = A2_fMSE[mask_std_A2]
AS_fMSE   = AS_fMSE[mask_std_AS]
SMOS_fMSE = SMOS_fMSE[mask_std_SMOS]
SMAP_fMSE = SMAP_fMSE[mask_std_SMAP]

# drop N/A
A2_fMSE.dropna(axis=0, how='any', inplace=True)
AS_fMSE.dropna(axis=0, how='any', inplace=True)
SMOS_fMSE.dropna(axis=0, how='any', inplace=True)
SMAP_fMSE.dropna(axis=0, how='any', inplace=True)

# Numeric, categorical predictors and response, y`
# AMSR2
A2_num   = A2_fMSE.drop(columns=['ltype', 'AMSR2_fMSE'])
A2_cat   = A2_fMSE['ltype']
A2_y     = A2_fMSE['AMSR2_fMSE']
# ASCAT
AS_num   = AS_fMSE.drop(columns=['ltype', 'ASCAT_fMSE'])
AS_cat   = AS_fMSE['ltype']
AS_y     = AS_fMSE['ASCAT_fMSE']
# SMOS
SMOS_num = SMOS_fMSE.drop(columns=['ltype', 'SMOS_fMSE'])
SMOS_cat = SMOS_fMSE['ltype']
SMOS_y   = SMOS_fMSE['SMOS_fMSE']
# SMAP
SMAP_num = SMAP_fMSE.drop(columns=['ltype', 'SMAP_fMSE'])
SMAP_cat = SMAP_fMSE['ltype']
SMAP_y   = SMAP_fMSE['SMAP_fMSE']

all_predictors = SMAP_num.columns.values

# Standardize numeric/response columns, to mean 0 variance 1
# AMSR2
A2_mean       = A2_num.mean()
A2_std        = A2_num.std()
A2_num_scaled = np.array((A2_num - A2_mean) / A2_std)
A2_y_mean     = A2_y.mean()
A2_y_std      = A2_y.std()
A2_y_scaled   = np.array((A2_y - A2_y_mean) / A2_y_std)
# ASCAT
AS_mean       = AS_num.mean()
AS_std        = AS_num.std()
AS_num_scaled = np.array((AS_num - AS_mean) / AS_std)
AS_y_mean     = AS_y.mean()
AS_y_std      = AS_y.std()
AS_y_scaled   = np.array((AS_y - AS_y_mean) / AS_y_std)
# SMOS
SMOS_mean       = SMOS_num.mean()
SMOS_std        = SMOS_num.std()
SMOS_num_scaled = np.array((SMOS_num - SMOS_mean) / SMOS_std)
SMOS_y_mean     = SMOS_y.mean()
SMOS_y_std      = SMOS_y.std()
SMOS_y_scaled   = np.array((SMOS_y - SMOS_y_mean) / SMOS_y_std)
# SMAP
SMAP_mean        = SMAP_num.mean()
SMAP_std         = SMAP_num.std()
SMAP_num_scaled  = np.array((SMAP_num - SMAP_mean) / SMAP_std)
SMAP_y_mean      = SMAP_y.mean()
SMAP_y_std       = SMAP_y.std()
SMAP_y_scaled    = np.array((SMAP_y - SMAP_y_mean) / SMAP_y_std)

## ==== Take Inputs from Console ==== ##
product = str(input('Product?'))
method = str(input('Method?'))
dist = str(input('Distribution?'))

## ==== NUTS or ADVI ==== ##
test_size = 0.33
ofp = '/home/hyung/Lab/libs/python/DL_Error_data/'+product+'_ywos_cp_'+method+'_'+dist
if product == 'SMAP':
    nuts_advi(SMAP_num_scaled, SMAP_y.values, ofp, dist, method, test_size)
elif product == 'SMOS':
    nuts_advi(SMOS_num_scaled, SMOS_y.values, ofp, dist, method, test_size)
elif product == 'ASCAT':
    nuts_advi(AS_num_scaled, AS_y.values, ofp, dist, method, test_size)
elif product == 'AMSR2':
    nuts_advi(A2_num_scaled, A2_y.values, ofp, dist, method, test_size)


import uvicorn
from fastapi import FastAPI, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
from model import ModelServe
from pydantic import BaseModel

app = FastAPI(
    title="Chinese Alpaca API",
    description="Run Alpaca with API",
    version="0.0.1",
)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
model = ModelServe(load_8bit=False)

@app.get("/")
async def read_root():
    return {"message": "Chinese Alpaca API is ready"}

@app.post("/completion/")
async def completion(
    instruction: str = Query(description="給模型的角色設定或指令", example="為用戶生成三種可能的投資標的建議", required=True),
    input: str = Query(description="給模型的 context 或輸入", example="用戶是剛畢業的碩士生", default=None),
):
    res = model.generate(instruction=instruction, input=input)
    return res

if __name__ == "__main__":
    uvicorn.run("api:app", host="127.0.0.1", port=9889)


# -*- coding: utf-8 -*-
"""
@author: Z659190
提供数据预处理的公共方法

"""

from datetime import date,datetime
import pandas as pd

mg_grade = ['Management Group 1', 'Management Group 2', 'Management Group 3', 'Management Group 4',
                 'ZF TRW OIP Pool', 'Executive Mgmt Group', 'Global Executive Team', 'Board of Management']

def conv_date(x):
    # print(type(x))
    # print(x)
    if not pd.isna(x):
        # print(x.date().strftime("%Y%m"))
        # print(pd.Period(x,'M'))
        return pd.Period(x, 'M')

# 获取有效日期。将assignment的日期写入到日期字段，将公司集团日期覆盖原入职日期
def update_date(date1, date2):
    try:
        if pd.isnull(date2):
            if isinstance(date1,datetime):
                return date1
            else:
                return pd.NaT
        elif pd.isnull(date1):
            if isinstance(date2, datetime):
                return date2
            else:
                return pd.NaT
        else:
            if isinstance(date1,datetime) and isinstance(date2, datetime):
                if date2 >= date1:
                    return date2
                else:
                    return date1
            elif isinstance(date1,datetime) and (not isinstance(date2, datetime)):
                return date1
            elif (not isinstance(date1,datetime)) and isinstance(date2, datetime):
                return date2
            # else:
            #     return ''
    except Exception as e:
        print('Convert Exception:', e, date1, date2)
        try:
            return pd.to_datetime(date1)
        except Exception as e:
            return pd.to_datetime(date2)

def get_service_year_termination(l_date, l_end):
    try:
        if pd.isna(l_date):
            return ''
        if pd.isna(l_end):
            return ''
        if isinstance (l_date, datetime) and isinstance(l_end, datetime):
            l_date_day = pd.to_datetime(l_date)
            l_end_day = pd.to_datetime(l_end)
            # year = l_end.year - l_date_day.year - ((l_end.month, l_end.day) < (l_date_day.month, l_date_day.day))
            lv_months = (l_end_day.year - l_date_day.year) * 12 + \
                         l_end_day.month - l_date_day.month + round((l_end_day.day - l_date_day.day + 1) / 31,2)
                         # l_end_day.month - l_date_day.month + round((l_end_day.day - l_date_day.day + 1) / pd.Period(pd.Timestamp(l_date_day)).days_in_month,2)

            return lv_months
        else:
            return 0
    except Exception as e:
        print('Calculate service month failed:', l_date,e)
        return 'NoDate'

def get_year(l_date):
    try:
        if pd.isna(l_date):
            return 'NoDate'
        if isinstance(l_date, datetime):
            l_date_day = pd.to_datetime(l_date)
            today = date.today()
            year = today.year - l_date_day.year - ((today.month, today.day) < (l_date_day.month, l_date_day.day))
            return year
        else:
            return 0
    except Exception as e:
        print('Convert date failed:', l_date,e)
        return 'NoDate'

def get_age_range(l_date):
    try:
        l_year = get_year(l_date)
        if l_year == 'NoDate':
            return 'NoDate'
        elif l_year < 25:
            return '[00--25)'
        elif l_year < 35:
            return '[25--35)'
        elif l_year < 45:
            return '[35--45)'
        elif l_year < 55:
            return '[45--55)'
        elif l_year < 70:
            return '[55--70)'
        else: # age >= 70:
            return '70+'
    except Exception as e:
        print('error log', l_date, e)
        return 'Unknown'

def get_service_year(l_date):
    try:
        l_year = get_year(l_date)
        if l_year < 1:
            return '[00--01)'
        elif l_year < 3:
            return '[01--03)'
        elif l_year < 5:
            return '[03--06)'
        elif l_year < 10:
            return '[06--10)'
        elif l_year < 15:
            return '[10--15)'
        else:
            return '[15--50)'
    except Exception as e:
        print('error log', l_date,e)
        return 'Unkown'

def get_mgr(employment_type):
    if employment_type in mg_grade:
        return "Manager_Level"
    else:
        return "Employee_level"

def get_period(l_date):
    try:
        ld = datetime(l_date)

    except Exception as e:
        print('error log', l_date,e)
        return 'Unkown'


'''
1. self is used to refer the instance of the class to the method.
2. Based on self only the method will get to know for which instace it has to response.
3. We can give any name in place of self
'''

class One():
    def add(k):
         print(' \n ID of self is : ',id(k))

    def sub(r, a, b):
         print(' \n ID of self is : ',id(r))
         print(' \n subtraction of two values is :', a-b)

ins = One()
ins2 = One()

print(' \n ID of ins is  : ',id(ins))
ins.sub(5,3)
ins.add()
ins.sub(10,5)
ins2.sub(100,5)


import yfinance
' some text '.strip

a = "AB"
print(a.lower)

asx_value = 7111.4
date = "2021-01-25"
units = 1
currency = "AUD"
print(f"The closing value of {units} unit of the All Ordinaries on {date} was {asx_value} {currency}.")

# Downloads Qantas share price beginning 1 January 2020

tic = "QAN.AX"
start = '2020-01-01'
end = None
df = yfinance.download(tic, start, end)
print(df)
df.to_csv('qan_stk_prc.csv')


from __future__ import annotations
#
import sys
#
import torch
import torchvision
#
from collections import OrderedDict
#
import TaskClass as TaskClass
import Task as Task

def tasml_nn_classifier_learn(
        test_net: torch.nn.Module,
        tasks: list[Task.Task],
        target_task: Task.Task,
        alpha_weights: torch.Tensor,
        convergence_diff: float = 0.0001,
        max_meta_epochs = 10,
        inner_epochs: int = 1,
        inner_lr: float = 0.001,
        outer_lr: float = 0.001,
        loss_function = torch.nn.CrossEntropyLoss()):
    #
    # Init.
    inner_optimiser = lambda x, g: x - inner_lr * g
    outer_optimiser = torch.optim.Adam(test_net.parameters(), lr=outer_lr)
    #
    # Common initialization state cache.
    backup_named_parameters = OrderedDict()
    #
    def cache_theta():
        for name, params in test_net.named_parameters():
            backup_named_parameters[name] = params.clone()
    #
    def load_named_params_of(named_params: OrderedDict):
        for name, params in test_net.named_parameters():
            params.data.copy_(named_params[name])
    #
    def load_theta():
        load_named_params_of(backup_named_parameters)
    #
    def load_theta_prime(theta_prime: OrderedDict):
        load_named_params_of(theta_prime)
    #
    test_net.train()
    #
    # Meta training status.
    meta_epochs_counter = 0
    prev_loss = 0 # Accumulated loss 2 rounds ago.
    last_loss = sys.maxsize # Accumulated loss last round.
    # TASML.
    losses = []
    while (abs(prev_loss - last_loss) > convergence_diff and meta_epochs_counter < max_meta_epochs):
        #
        # Store this epoch's theta.
        cache_theta()
        #
        outer_optimiser.zero_grad()
        test_net.zero_grad()
        #
        outer_loss_sum = 0
        #
        for task_index in range(len(tasks)):
            task = tasks[task_index]
            #
            # Inner loss.
            for _ in range(inner_epochs):
                supp_train_inner_loss = loss_function(test_net(task.supp_train), task.supp_targets)
                test_net.zero_grad()
                grads = torch.autograd.grad(supp_train_inner_loss, test_net.parameters(), create_graph=True)
                #
                # Compute theta prime.
                named_params = OrderedDict()
                for (pname, param), grad in zip(test_net.named_parameters(), grads):
                    named_params[pname] = inner_optimiser(param, grad)
                # Load theta prime.
                load_theta_prime(named_params)
            #
            # Other loss
            outer_loss_sum += alpha_weights[task_index] * loss_function(test_net(task.query_train), task.query_targets)
            #
            # Reload theta.
            load_theta()
        #
        #
        # Extra task L((Alg(D), D) | D = data from tasked task, only from support dataset.
        for _ in range(inner_epochs):
            supp_train_inner_loss = loss_function(test_net(target_task.supp_train), target_task.supp_targets)
            test_net.zero_grad()
            grads = torch.autograd.grad(supp_train_inner_loss, test_net.parameters(), create_graph=True)
            #
            # Compute theta prime.
            named_params = OrderedDict()
            for (pname, param), grad in zip(test_net.named_parameters(), grads):
                named_params[pname] = inner_optimiser(param, grad)
            # Load theta prime.
            load_theta_prime(named_params)
        #
        outer_loss_sum += loss_function(test_net(target_task.supp_train), target_task.supp_targets)
        #
        # Reload theta.
        load_theta()
        #
        print("Epoch " + str(meta_epochs_counter) + ": "+ str(outer_loss_sum.item()))
        losses.append(outer_loss_sum.item())
        outer_loss_sum.backward()
        outer_optimiser.step()
        #
        prev_loss = last_loss
        last_loss = outer_loss_sum.item()
        meta_epochs_counter += 1
    return losses


import datetime

from auth.config.ticket import TICKET_TYPE_CHOICES
from auth.constants import (
    FAILED,
    PENDING,
    PROCESSING,
    STATUS,
    STOPPED,
    SUBJECTS,
    SUCCEEDED,
    SYSTEM_USER,
)
from auth.core.signal import ticket_finished, ticket_processing
from auth.exceptions import (
    ApprovalRangeError,
    AuthAPIError,
    NoPermissionError,
    NotInApprovalProcessError,
    TicketStateHasBeenOperatedError,
)
from auth.models.fields import JsonField, SemicolonSplitField
from common.log import logger
from django.db import models, transaction
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _

class ApprovalController:
    """
    控制审批流程的控制器
    """

    def __init__(self):
        pass

    @classmethod
    def approve(cls, state, status, process_message, processed_by):
        """
        审批
        @param state:
        @param status:
        @param process_message:
        @param processed_by:
        @return:
        """
        ticket = state.ticket
        state.check_approval(processed_by, status)
        with transaction.atomic(using="basic"):
            state.process(status=status, processed_by=processed_by, process_message=process_message)
            if status == FAILED:
                ticket.reject()
            elif status == SUCCEEDED:
                ticket.next_process()

    @classmethod
    def auto_approve(cls, ticket):
        """
        自动审批，默认不抛出异常
        @param ticket:
        @return:
        """
        try:
            system_states = ticket.states.filter(
                process_step=ticket.process_step,
                processors__contains=Ticket.generate_sub_member(SYSTEM_USER),
                status__in=(PROCESSING, PENDING),
            ).all()
            for state in system_states:
                cls.approve(state, status=SUCCEEDED, processed_by=SYSTEM_USER, process_message=_("系统自动审批"))

            personal_states = ticket.states.filter(
                process_step=ticket.process_step,
                processors__contains=Ticket.generate_sub_member(ticket.created_by),
                status__in=(PROCESSING, PENDING),
            ).all()
            for state in personal_states:
                cls.approve(
                    state, status=SUCCEEDED, processed_by=ticket.created_by, process_message=_("创建人与审批人相同，自动过单")
                )

            if len(personal_states) + len(system_states) > 0:
                return True
        except AuthAPIError as e:
            logger.exception(f"[APPROVE ERROR] Auto approve failed, ticket_id={ticket.id}, error={e}")

        return False

class Ticket(models.Model):
    """
    单据信息
    """

    ticket_type = models.CharField(_("单据类型"), max_length=255, choices=TICKET_TYPE_CHOICES)
    created_by = models.CharField(_("创建人"), max_length=255)
    created_at = models.DateTimeField(_("创建时间"), auto_now_add=True)
    reason = models.CharField(_("申请理由"), max_length=255, blank=True, default="")
    status = models.CharField(_("当前申请状态"), max_length=255, choices=STATUS, default=PROCESSING)
    end_time = models.DateTimeField(_("结束时间"), null=True, default=None)
    process_length = models.IntegerField(_("处理总步骤数"))
    extra = JsonField(_("额外的内容"), null=True, blank=True)
    process_step = models.IntegerField(_("当前处理步骤"), default=0)
    process_id = models.CharField(_("通用单据标识，目前仅用于通用单据"), max_length=255, null=True)

    class Meta:
        managed = False
        app_label = "auth"
        db_table = "auth_ticket"
        ordering = ["-created_at", "-end_time"]

    def is_process_finish(self):
        """
        判断当前阶段是否完成
        @return:
        """
        return self.states.filter(process_step=self.process_step, status=PROCESSING).count() == 0

    def reject(self):
        """
        驳回审批
        @return:
        """
        self.status = FAILED
        self.end_time = datetime.datetime.now()
        self.save()
        TicketState.objects.filter(ticket_id=self.pk, status__in=[PENDING, PROCESSING]).update(status=STOPPED)
        ticket_finished.send(sender=self.__class__, ticket=self, status=FAILED)

    def start_process(self, auto_approve=True):
        """
        启动当前单据的第一步审核

        @param {boolean} auto_approve 是否启用自动审批
        """
        # 没有待审批步骤，直接标记成功
        if self.process_length <= self.process_step:
            self.status = SUCCEEDED
            self.end_time = datetime.datetime.now()
            ticket_finished.send(sender=self.__class__, status=SUCCEEDED, ticket=self)
            self.save()
        else:
            TicketState.objects.filter(ticket_id=self.pk, process_step=0).update(status=PROCESSING)
            if not (auto_approve and ApprovalController.auto_approve(self)):
                ticket_processing.send(sender=self.__class__, status=PROCESSING, ticket=self)

    def next_process(self):
        """
        下一步骤
        @return:
        """
        self.process_step += 1
        if self.process_length <= self.process_step:
            self.status = SUCCEEDED
            self.end_time = datetime.datetime.now()
            ticket_finished.send(sender=self.__class__, status=SUCCEEDED, ticket=self)
        else:
            TicketState.objects.filter(ticket_id=self.pk, process_step=self.process_step).update(status=PROCESSING)

            # 如果自动审批通过，则不需要发送通知邮件
            if not ApprovalController.auto_approve(self):
                ticket_processing.send(sender=self.__class__, status=PROCESSING, ticket=self)
        self.save()

    def current_state(self):
        """
        单据当前最新的步骤

        @note 如果同一步骤存在多个 state，比如一个 RT 需要两个人同时审批通过时，则该逻辑是有缺陷的
        """
        current_step = (self.process_step - 1) if self.process_length == self.process_step else self.process_step
        return self.states.filter(process_step=current_step).first()

    @classmethod
    def get_permission_queryset(cls, username):
        """
        获取有权限的queryset
        @param username:
        @return:
        """
        return cls.objects.filter(created_by=username).all()

    @classmethod
    def has_permission(cls, username, ticket_id):
        """
        校验用户是否有相关操作权限
        @param username:
        @param ticket_id:
        @return:
        """
        if Ticket.objects.filter(pk=ticket_id).count() == 0:
            return False

        o_ticket = Ticket.objects.get(pk=ticket_id)
        if username == o_ticket.created_by:
            return True

        users = []
        for state in o_ticket.states.all():
            users.extend(state.processors)

        return username in users

    @staticmethod
    def generate_sub_member(username):
        """
        生成子成员字符串，为了规避直接用 __contains过滤如
        zhangsan时同时过滤出 zhangsang，
        或者 hanhong 过滤出 zhanhong
        故在前后都加上';'
        @return:
        """
        return f";{username};"

    def has_owner_permission(self, processed_by, raise_exception=False):
        """
        检查撤销操作的执行者是否拥有该单据(即是单据的创建人)
        @param processed_by:
        @param raise_exception:
        @return:
        """
        if not (self.created_by == processed_by):
            if raise_exception:
                raise NoPermissionError()
            else:
                return False
        return True

    def withdraw(self, process_message, processed_by):
        """
        撤销单据操作(更新单据状态)
        @param process_message:处理信息
        @param processed_by: 处理人
        @return:
        """
        state = self.states.all()
        for one_state in state:
            one_state.status = STOPPED
            one_state.process_message = process_message
            one_state.processed_by = processed_by
            one_state.processed_at = datetime.datetime.now()
            one_state.save()

        self.status = STOPPED
        self.end_time = datetime.datetime.now()
        self.save()
        TicketState.objects.filter(ticket_id=self.pk, status__in=[PENDING, PROCESSING]).update(status=STOPPED)
        ticket_finished.send(sender=self.__class__, ticket=self, status=STOPPED)

class TicketStateManager(models.Manager):
    def filter(self, *args, **kwargs):
        # 关联查询，减少DB查询次数
        return super().select_related("ticket").filter(*args, **kwargs)

class TicketState(models.Model):
    """
    单据流转的状态节点
    """

    ticket = models.ForeignKey("Ticket", on_delete=models.CASCADE, related_name="states")
    processors = SemicolonSplitField(_("可处理人"), max_length=255)
    processed_by = models.CharField(_("处理人"), max_length=255, null=True, blank=True)
    processed_at = models.DateTimeField(_("处理时间"), null=True)
    process_step = models.IntegerField(_("所属步骤"))
    process_message = models.CharField(_("处理信息"), max_length=255, blank=True, default="")
    status = models.CharField(_("处理状态"), max_length=32, choices=STATUS, default=PENDING)
    objects = TicketStateManager()

    class Meta:
        managed = False
        app_label = "auth"
        db_table = "auth_ticket_state"
        ordering = ["ticket"]

    def check_approval(self, processed_by, status):
        """
        校验审批信息
        @param processed_by:
        @param status:
        @return:
        """
        if not TicketState.has_permission(processed_by, self.pk):
            # 无操作权限
            raise NoPermissionError()
        if self.status != PROCESSING:
            # 未在操作阶段或已操作
            raise TicketStateHasBeenOperatedError()
        if status not in [SUCCEEDED, FAILED]:
            # 审批范围错误
            raise ApprovalRangeError()
        if self.ticket.process_step != self.process_step:
            # 未在可审批阶段
            raise NotInApprovalProcessError()

    def process(self, status, process_message, processed_by):
        """
        处理状态节点
        @param status:
        @param process_message:
        @param processed_by:
        @return:
        """
        self.status = status
        self.process_message = process_message
        self.processed_by = processed_by
        self.processed_at = datetime.datetime.now()
        self.save()

    @classmethod
    def get_permission_queryset(cls, username):
        """
        获取相关权限的state
        @param username:
        @return:
        """
        return cls.objects.filter(
            Q(processed_by=username)
            | Q(processors__contains=Ticket.generate_sub_member(username))
            | Q(ticket__created_by=username)
        ).all()

    @classmethod
    def has_permission(cls, username, ticket_state_id):
        """
        判断用户对单据状态是否有权限
        @param username:
        @param ticket_state_id:
        @return:
        """
        return cls.objects.filter(
            Q(processed_by=username) | Q(processors__contains=Ticket.generate_sub_member(username)), pk=ticket_state_id
        ).count()

    @classmethod
    def todo_count(cls, username):
        """
        待办数量统计
        @param username:
        @return:
        """
        return cls.objects.filter(processors__contains=Ticket.generate_sub_member(username), status=PROCESSING).count()

class DataTicketPermissionManager(models.Manager):
    def create(self, **kwargs):
        scope = kwargs.pop("scope", None)
        if isinstance(scope, dict):
            for key, value in list(scope.items()):
                kwargs["key"] = key
                kwargs["value"] = value
                break

        super().create(**kwargs)

    def filter(self, *args, **kwargs):
        scope = kwargs.pop("scope", None)
        if scope is not None:
            for key, value in list(scope.items()):
                kwargs["key"] = key
                kwargs["value"] = value
                break
        return super().select_related("ticket").filter(*args, **kwargs)

class DataTicketPermission(models.Model):
    """
    申请数据的单据
    """

    ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
    subject_id = models.CharField(_("申请主体标识"), max_length=64)
    subject_name = models.CharField("申请主体名称", max_length=255, default="", blank=True)
    subject_class = models.CharField(_("申请主体"), max_length=32, choices=SUBJECTS)
    action = models.CharField(_("申请操作方式"), max_length=255)
    object_class = models.CharField(_("申请对象"), max_length=255)
    key = models.CharField(_("资源key"), max_length=64)
    value = models.CharField(_("资源值"), max_length=255)

    objects = DataTicketPermissionManager()

    @property
    def scope(self):
        return {self.key: self.value}

    class Meta:
        managed = False
        db_table = "auth_data_ticket"
        app_label = "auth"

class RoleTicketPermission(models.Model):
    """
    申请角色相关的单据
    """

    ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
    action = models.CharField(_("申请操作方式"), max_length=255)
    user_id = models.CharField(_("用户标识"), max_length=64)
    role_id = models.CharField(_("角色ID"), max_length=64)
    scope_id = models.CharField(_("资源id"), max_length=255)

    @property
    def scope(self):
        return {"scope_id": self.scope_id, "role_id": self.role_id}

    class Meta:
        db_table = "auth_role_ticket"
        managed = False
        app_label = "auth"

class TokenTicketPermission(models.Model):
    """
    申请accessToken的单据
    """

    ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
    subject_id = models.CharField(_("申请主体标识"), max_length=255)
    subject_name = models.CharField("申请主体名称", max_length=255, default="", blank=True)
    subject_class = models.CharField(_("申请主体"), max_length=32, choices=SUBJECTS)
    action = models.CharField(_("申请操作方式"), max_length=255)
    object_class = models.CharField(_("申请对象"), max_length=255)
    key = models.CharField(_("资源key"), max_length=64)
    value = models.CharField(_("资源值"), max_length=255)

    objects = DataTicketPermissionManager()

    @property
    def scope(self):
        return {self.key: self.value}

    class Meta:
        db_table = "auth_token_ticket"
        managed = False
        app_label = "auth"


#! /usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Joy
#import serial
#arduinoData=serial.Serial("/dev/ttyACM0",9600)

velocity=0
y=0
def callback(data):
    global velocity
    global y
    twist = Twist()
    left_stick_up_down=data.axes[1]
    left_stick_left_right=data.axes[0]
    right_stick_up_down=data.axes[4]
    right_stick_left_right=data.axes[3]
    x=data.axes[7]
    Max=180
    Min=1

    if(x==1):
        velocity+=1
        x=velocity
        if(velocity>=180):
            velocity=Max
    elif(x==-1):
        velocity-=1
        if(velocity<=1):
            velocity=Min

    twist.linear.x=left_stick_up_down
    twist.angular.x=left_stick_left_right
    twist.linear.y=right_stick_up_down
    twist.angular.y=right_stick_left_right
    '''while(left_stick_up_down > 0):
        y +=1
        if(y > 180):
            y = 180
    while(left_stick_up_down < 0):
        y-= 1
        if(y < 0):
            y = 0

    twist.linear.x = y'''

    #z=arduinoData.write(format(twist.linear.x))
    #print(z)
    rospy.loginfo('velocity=%s',velocity)
    #twist.angular.z = 4*data.axes[0]
    pub.publish(twist)

# Intializes everything
def start():
    # publishing to "turtle1/cmd_vel" to control turtle1
    global pub
    pub = rospy.Publisher('/cmd_vel', Twist)
    # subscribed to joystick inputs on topic "joy"
    rospy.Subscriber("joy", Joy, callback)
    # starts the node
    rospy.init_node('Joy2Turtle')
    rospy.spin()

if __name__ == '__main__':
    start()


import pytest

def dec_to_base(number, base):
    """
    Input: number is the number to be converted
           base is the new base  (eg. 2, 6, or 8)
    Output: the converted number in the new base without the prefix (eg. '0b')
    """
    result = ""
    while True:
        remainder = number % base
        number = number // base
        result += str(remainder)
        if number == 0:
            break

    return int(result[::-1])


#/src/views/ProjectView

from flask import request, json, Response, Blueprint, g
from ..models.MessageModel import MessageModel, MessageSchema
from ..services.AuthorizationService import Auth
from ..apiviews import custom_response

message_api = Blueprint('message_api', __name__)
message_schema = MessageSchema()
messages_schema = MessageSchema(many=True)

@message_api.route('/', methods=['POST'])
def create():
  """
  Create new message
  """
  req_data = request.get_json(force=True)
  data, error = message_schema.load(req_data)

  if error:
    return custom_response(error, 400)

  message = MessageModel(data)
  message.save()
  message_data = message_schema.dump(message).data
  return custom_response(message_data, 201)

@message_api.route('to/<string:to_id>', methods=['GET'])
def get(to_id):
  """Get project Log"""
  messages = MessageModel.get_message_to(to_id)
  log_data = messages_schema.dump(messages).data
  return custom_response(log_data, 200)


import urllib.request
import requests
from bs4 import BeautifulSoup
import xlsxwriter
import random
import time

# import jieba
# import jieba.analyse
# from openpyxl import load_workbook
# import wordcloud

class GetAgent:#获取代理IP

    def __init__(self):  # 构造函数定义这个类的属性（变量）、函数

        self.IPlist = []
        self.open_net()
        self.scratch()

    def open_net(self):  # url 可不可以在主函数里面用全局变量去定义？

        req = urllib.request.Request(url, None, header)
        response = urllib.request.urlopen(req)
        html = response.read()
        return html

    def scratch(self):
        hhtml = BeautifulSoup(self.open_net(), "html.parser")
        hang = hhtml.findAll('tr')
        for each in range(0, len(hang)):
            try:
                lie = hang[each].findAll('td')
                self.IPlist.append([lie[0].text, lie[1].text, lie[3].text])
            except:
                continue

class TestProxy:

    def __init__(self):
        self.test()

    def test(self):

        proxy_host = ip + ":" + port
        proxy_temp = {Type: proxy_host}

        proxy_support = urllib.request.ProxyHandler(proxy_temp)
        opener = urllib.request.build_opener(proxy_support)

        try:
            req = urllib.request.Request("https://www.baidu.com", None, header)
            response = opener.open(req)
            print(str(proxy_temp)+"可以登陆")
            Proxy_list.append(proxy_temp)
            # Proxy_list.append([Type,ip,port])
        except:
            print(str(proxy_temp)+"无法登陆")
            return

class QiDian:  # 爬取每一页的安居客房价信息（爬取海口的房价信息）
    def __init__(self):
        self.msg = []
        self.open_net()
        self.scratch()

    def open_net(self):  # url 可不可以在主函数里面用全局变量去定义？
        diaoyong = random.choice(Proxy_list)  # 随机调用代理 IP

        # 这里我们尝试一下使用requests库的get方法来获取response
        # req = requests.get(page_url,headers = header,timeout = 5)

        #然后再更改一下Windows里防火墙的访问

        #封装好req 请求对象
        req = urllib.request.Request(url=page_url, data=None, headers=header)

        #创建代理对象
        proxy_support=urllib.request.ProxyHandler(diaoyong)
        opener=urllib.request.build_opener(proxy_support)

        #通过代理opener来发送请求
        response=opener.open(req)

        html=response.read()
        return html

    def scratch(self):
        Html = BeautifulSoup(self.open_net(), "html.parser")
        # 爬取 要使用的应该是Beautifulsoup
        div = Html.find('div',{'id':'threadlist'})
        tbodys = div.find_all('tbody')
        #返回tbody列表
        for each in range(1, len(tbodys)):
            try:
                way = tbodys[each].find('em').contents[1].text
                title = tbodys[each].find('a',{'class':'s xst'}).text
                author = tbodys[each].find('a',{'c':'1'}).text
                date = tbodys[each].find('span').text
                reply = tbodys[each].find_all('a',{'c':'1'})[1].text
                self.msg.append([way,title,author,date,reply])
                print(way,title,author,date,reply)
            except:
                continue

if __name__ == "__main__":

    header = {}  # header 可不可以在主函数里面用全局变量去定义？
    header[
        'User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'

    Proxy_list = []  # 保存可用的IP 地址
    #爬取代理IP并进行测试，测试函数将会把通过测试的代理IP存储到列表中仪表调用

    #爬取10页代理IP
    for i in range(1, 11):
        url = "http://www.66ip.cn/" + str(i) +'.html'
        shuchu1 = GetAgent()
        print(shuchu1.IPlist)
        for ip, port, Type in shuchu1.IPlist:
            shuchu2 = TestProxy()
    print(Proxy_list)

    #将爬取的IP地址写入文档 ，此处不进行存储，直接调用列表

    # workbook = xlsxwriter.Workbook('可用IP地址.xlsx')
    # worksheet = workbook.add_worksheet()
    #
    # First_row = ['Type', 'ip', 'port']
    # row = 0
    # col = 0
    # for each in First_row:
    #     worksheet.write(row, col, each)
    #     col = col + 1
    # row = row + 1
    # col = 0
    #
    # for each1 in range(0, len(Proxy_list)):
    #     for each2 in Proxy_list[each1]:
    #         worksheet.write(row, col, each2)
    #         col = col + 1
    #     row = row + 1
    #     col = 0
    #
    # workbook.close()
    # print("IP地址存入成功")

    # 利用代理爬取百度贴吧的帖子

    #创建IO流，打开文档，这里先不打开
    workbook2 = xlsxwriter.Workbook('海南大学起点招聘信息.xlsx')  # 创建Excel文件
    worksheet2 = workbook2.add_worksheet()

    data_list = ["招聘类型", "招聘主题", "发帖人", "发帖时间", "最后回复人"]
    row, col = 0, 0
    for data in data_list:
        worksheet2.write(row, col, data)
        col += 1

    row, col = 1, 0

    header_list = [
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0']
    header_list.append(
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36 Edg/94.0.992.31')
    header_list.append(
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36')

    # 调用帖子信息爬取的类函数

    url1 = 'http://www.ihain.cn/forum-255-'
    url2 = '.html'
    for i in range(1, 51):
        Message = []
        page_url = url1 + str(i) + url2
        header['User-Agent'] = random.choice(header_list)
        Spider = QiDian()
        Message = Spider.msg
        time.sleep(3)
        # print(Spider.msg)
        # 将爬取的信息存入 '海南大学起点论坛招聘.xlsx'
        for j in range(0, len(Message)):
            for each in Message[j]:
                worksheet2.write(row, col, each)
                col = col + 1
            row = row + 1
            col = 0
            print("论坛信息输入成功!")
    workbook2.close()
    print('------------爬虫结束-------------')


#!/usr/bin/env python
from __future__ import print_function
import argparse
import os
import boto3
from botocore.client import Config

ci_keys_file='/projects/irods/vsphere-testing/externals/amazon_web_services-CI.keypair'
s3region = 'us-east-1'

# arg parsing
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--configure', action="store_true", help='set up aws configuration')

args = parser.parse_args()

def set_up_aws_config_dir(keys_file):
    # read access keys from keypair file
    with open(keys_file) as f:
        aws_access_key_id = f.readline().rstrip()
        aws_secret_access_key = f.readline().rstrip()

    # make config dir
    aws_cfg_dir_path = os.path.join(os.path.expanduser('~'), '.aws')
    try:
        os.makedirs(aws_cfg_dir_path)
    except OSError:
        if not os.path.isdir(aws_cfg_dir_path):
            raise

    # make config file
    with open(os.path.join(aws_cfg_dir_path, 'config'), 'w') as cfg_file:
        cfg_file.write('[default]\n')
        cfg_file.write('region=' + s3region + '\n')

    # make credentials file
    with open(os.path.join(aws_cfg_dir_path, 'credentials'), 'w') as cred_file:
        cred_file.write('[default]\n')
        cred_file.write('aws_access_key_id = ' + aws_access_key_id + '\n')
        cred_file.write('aws_secret_access_key = ' + aws_secret_access_key + '\n')

# set up config files if needed
if args.configure:
    set_up_aws_config_dir(ci_keys_file)

s3 = boto3.resource('s3', config=Config(signature_version='s3v4'))

# cleanup buckets
for bucket in s3.buckets.all():
        if bucket.name.startswith('irods-ci-ubuntu') or bucket.name.startswith('irods-ci-centos') or bucket.name.startswith('irods-ci-opensuse'):
                for obj in bucket.objects.all():
                        obj.delete()
                print('deleting {bucket.name}'.format(**locals()))
                bucket.delete()


import numpy as np

from oxeo.core.constants import BAND_INFO

from .mtl_parser import parsemeta

def to_toa_radiance(arr: np.ndarray, mtl_dict: dict, constellation: str):

    bands = list(BAND_INFO[constellation].keys())
    arr = arr.astype(float)

    radiometric_rescaling = mtl_dict["L1_METADATA_FILE"]["RADIOMETRIC_RESCALING"]

    for i, b in enumerate(bands):
        radiance_add_band = f"RADIANCE_ADD_BAND_{b.replace('B','')}"
        radiance_mult_band = f"RADIANCE_MULT_BAND_{b.replace('B','')}"

        radiance_add = radiometric_rescaling.get(radiance_add_band)
        radiance_mult = radiometric_rescaling.get(radiance_mult_band)

        if radiance_add is not None and radiance_mult is not None:
            arr[i] = radiance_mult * arr[i] + radiance_add
    return arr

def to_toa_reflectance(arr: np.ndarray, mtl_dict: dict, constellation: str):
    bands = list(BAND_INFO[constellation].keys())
    arr = arr.astype(float)

    radiometric_rescaling = mtl_dict["L1_METADATA_FILE"]["RADIOMETRIC_RESCALING"]

    sun_elevation = mtl_dict["L1_METADATA_FILE"]["IMAGE_ATTRIBUTES"]["SUN_ELEVATION"]
    for i, b in enumerate(bands):
        reflectance_add_band = f"REFLECTANCE_ADD_BAND_{b.replace('B','')}"
        reflectance_mult_band = f"REFLECTANCE_MULT_BAND_{b.replace('B','')}"

        reflectance_add = radiometric_rescaling.get(reflectance_add_band)
        reflectance_mult = radiometric_rescaling.get(reflectance_mult_band)

        if reflectance_add is not None and reflectance_mult is not None:
            arr[i] = (reflectance_mult * arr[i] + reflectance_add) / np.sin(
                sun_elevation
            )
    return arr

def to_toa_brightness_temp(arr: np.ndarray, mtl_dict: dict, constellation: str):
    bands = list(BAND_INFO[constellation].keys())
    arr = arr.astype(float)

    radiometric_rescaling = mtl_dict["L1_METADATA_FILE"]["RADIOMETRIC_RESCALING"]

    for i, b in enumerate(bands):
        radiance_add_band = f"RADIANCE_ADD_BAND_{b.replace('B','')}"
        radiance_mult_band = f"RADIANCE_MULT_BAND_{b.replace('B','')}"

        k1_constant_band = f"K1_CONSTANT_BAND_{b.replace('B','')}"
        k2_constant_band = f"K1_CONSTANT_BAND_{b.replace('B','')}"

        k1 = radiometric_rescaling.get(k1_constant_band)
        k2 = radiometric_rescaling.get(k2_constant_band)

        radiance_add = radiometric_rescaling.get(radiance_add_band)
        radiance_mult = radiometric_rescaling.get(radiance_mult_band)

        if k1 is not None and k2 is not None:

            rad = radiance_mult * arr[i] + radiance_add

            arr[i] = k2 / np.log(k1 / rad + 1)

    return arr

def to_toa(arr: np.ndarray, mtl_dict: dict, constellation: str):
    ref = to_toa_reflectance(arr, mtl_dict, constellation)
    return to_toa_brightness_temp(ref, mtl_dict, constellation)

def get_mtl_dict(fs, mtl_path: str, revisit: int):
    f = fs.open([path for path in fs.ls(mtl_path)][revisit])
    return parsemeta(f.read().decode("utf-8"))


#!/usr/bin/python3
"""
sorted_dict module.
It contains two function.
"""

import signal
import re
import sys

def sorted_dict(d):
    if d:
        print("\n".join(["{}: {:d}".format(k, d[k])
                         for k in sorted(d.keys())]))

def imp_stdin():
    """reads stdin"""
    size = 0
    count = 0
    codes = {}
    possib = ["200", "301", "400", "401", "403", "404", "405", "500"]
    try:
        for line in sys.stdin:
            line_split = line.split()
            if line_split[-1].isdecimal():
                size += int(line_split[-1])
                if line_split[-2] in possib:
                    codes[line_split[-2]] = codes.get(
                        line_split[-2], 0) + 1
            count += 1
            if count % 10 == 0:
                print("File size: {:d}".format(size))
                sorted_dict(codes)
    except KeyboardInterrupt:
        print("File size: {:d}".format(size))
        sorted_dict(codes)
        raise
    print("File size: {:d}".format(size))
    sorted_dict(codes)

imp_stdin()


class Solution:
    #time - O(n)
    #space - O(1)
    def jump(self, nums: List[int]) -> int:
        l, r = 0, 0
        result = 0

        while r < len(nums)-1:
            farthest = 0
            for i in range(l, r + 1):
                farthest = max(farthest, nums[i] + i)
            l = r + 1
            r = farthest
            result += 1

        return result


import socket
import threading as th
import time
from Microphone import Mic
import numpy as np
import pickle
import struct
from Message import Message

class Server():
    def __init__(self):
        self.bind_ip = "192.168.1.24"
        self.bind_port = 3333

        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.bind((self.bind_ip, self.bind_port))

        self.server.listen(10)

        self.clients = []

        th.Thread(target=self.find_client).start()

    def find_client(self):
        while True:
            print("Client bekleniyor...")
            client, addr = self.server.accept()
            print(f"Client accepted - ({addr[0]}:{addr[1]})")
            self.clients.append(client)

            id = (len(self.clients) - 1)

            print(f"Bağlanan client ID : {id}")

            th.Thread(target=self.take_message, args=(id,)).start()

            client.sendall(struct.pack('>I', len(bytes("ID-"+str(id), "utf-8"))))
            client.sendall(bytes("ID-"+str(id), "utf-8"))

    def take_message(self, id):
        while True:
            data_size = struct.unpack('>I', self.clients[id].recv(4))[0]
            received_payload = b""
            reamining_payload_size = data_size
            while reamining_payload_size != 0:
                received_payload += self.clients[id].recv(reamining_payload_size)
                reamining_payload_size = data_size - len(received_payload)

            data = pickle.loads(received_payload)
            print(len(data.data))
            self.send_message(data.data, int(data.id))

    def send_message(self, message, client_id):
        serialized_data = pickle.dumps(Message(message, client_id))

        for i in range(0, len(self.clients)):
            if i != client_id:
                self.clients[i].sendall(struct.pack('>I', len(serialized_data)))
                self.clients[i].sendall(serialized_data)

server = Server()


import keras.backend as K
from keras.layers import Input, Lambda, Convolution2D, Dropout
from keras.models import Model, model_from_json

from xsertion import XCNNBuilder
from xsertion.data import get_cifar, val_split

input_shape = (3, 32, 32)
pr_axis = 1
if K.image_dim_ordering() == 'tf':
    input_shape = (32, 32, 3)
    pr_axis = 3

import logging
logging.basicConfig(level=logging.DEBUG)

import logging
logging.basicConfig(level=logging.DEBUG)

import argparse
parser = argparse.ArgumentParser('Tell p value')
parser.add_argument('p', help='p value', type=float)
parser.add_argument('c10', help='use c10?', type=int)
parser.add_argument('alpha', help='alpha hyper', type=float)
parser.add_argument('beta', help='beta hyper', type=float)
parser.add_argument('local', help='filter counts infered locally or globally', type=int)
args = parser.parse_args()

# args=lambda : None
# args.p = 1.0

pstr= str(args.p).replace('.','_')

use_c10 = bool(args.c10)
use_local = bool(args.local)
print('p', args.p, 'c10', use_c10, 'alpha', args.alpha, 'beta', args.beta, 'local', use_local)

nb_classes, X_train, Y_train, X_val, Y_val, X_test, Y_test = get_cifar(p=args.p, append_test=False, use_c10=use_c10)
X_t, Y_t, X_v, Y_v = val_split(0.2, X_train, Y_train)
print(input_shape,pr_axis)
from fitnet import get_fitnet, get_xfitnet
# nb_classes=100
model = get_fitnet(nb_classes, input_shape, pr_axis)
xmodel = get_xfitnet(nb_classes, input_shape, pr_axis)

inp = Input(shape=input_shape)
if K.image_dim_ordering() == 'tf':
    lb1 = Lambda(lambda x: x[:, :, :, 0:1], output_shape=(32, 32, 1))(inp)
    lb2 = Lambda(lambda x: x[:, :, :, 1:2], output_shape=(32, 32, 1))(inp)
    lb3 = Lambda(lambda x: x[:, :, :, 2:3], output_shape=(32, 32, 1))(inp)
else:
    lb1 = Lambda(lambda x: x[:, 0:1, :, :], output_shape=(1, 32, 32))(inp)
    lb2 = Lambda(lambda x: x[:, 1:2, :, :], output_shape=(1, 32, 32))(inp)
    lb3 = Lambda(lambda x: x[:, 2:3, :, :], output_shape=(1, 32, 32))(inp)
input_model = Model(input=inp, output=[lb1, lb2, lb3])

nb_batch = 128
nb_epoch = 230

xcnn_build=True

if xcnn_build:
    builder = XCNNBuilder(model, input_model, alpha=args.alpha, beta=args.beta)
    if use_local:
        builder.set_xspot_strategy(layernames=['poolspot_1', 'poolspot_2'], filters=-1)

    def new_con_form(nb_filter):
        i = Input(shape=input_shape)
        # b = BatchNormalization(axis=pr_axis, name='bn')(i)
        d = Dropout(.25, name='d')(i)
        c = Convolution2D(nb_filter, 1, 1, activation='relu', name='c')(d)
        # c = BatchNormalization(axis=pr_axis, name='bn')(c)
        return Model(input=i, output=c)

    builder.set_xcons(model_function=new_con_form, use_bn=False)
    print(builder.connection_factory)

    builder.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    from keras.callbacks import EarlyStopping
    # reduce_lr = ReduceLROnPlateau(monitor='val_acc', factor=.1, patience=8, cooldown=3, min_lr=1e-6, verbose=2)
    e_stop = EarlyStopping(monitor='val_acc', min_delta=1e-4, patience=15, verbose=2)

    builder.fit(X_t, Y_t, batch_size=2*nb_batch, nb_epoch=80, validation_data=(X_v, Y_v), verbose=2,
                callbacks=[e_stop])

    if args.p == 1. and not use_c10:
        builder.set_single_superlayer_accs([.5012, .3321, .3377])
    elif args.p ==.4 and not use_c10:
        builder.set_single_superlayer_accs([.3592, .2321, .2384])

    if args.p == 1. and use_c10:
        builder.set_single_superlayer_accs([.8561, .6648, .6540])
    elif args.p ==.4 and use_c10:
        builder.set_single_superlayer_accs([.7721, .5916, .5905])

    single_model = builder.build_scaled_single_xcon()

    # double_model = builder.build_scaled_double_xcon()

    with open('models/mtt_fit_singlea_{}.json'.format(pstr), 'w') as out:
        out.write(single_model.to_json(indent=4))

    # with open('models/mtt_fit_doublea_{}.json'.format(pstr), 'w') as out:
    #     out.write(double_model.to_json(indent=4))

else:
    with open('model_train_test_fit_single.json', 'r') as i:
        single_model = model_from_json(i.read())
    # with open('model_train_test_fit_double.json', 'r') as i:
    #     double_model = model_from_json(i.read())

model.name = 'baseline'
xmodel.name= 'XCNN baseline'
def md_train(model):
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    lbd_ob = lambda: None
    lbd_ob.max_acc = 0.0
    import sys
    try:
        from keras.callbacks import LambdaCallback

        def a(epoch, logs):
            if logs['val_acc'] > lbd_ob.max_acc:
                lbd_ob.max_acc = logs['val_acc']

        custom_lambda_clb = LambdaCallback(on_epoch_end=a)
        hist = model.fit(X_train, Y_train, batch_size=nb_batch, nb_epoch=nb_epoch, validation_data=(X_test, Y_test),
                         verbose=2, callbacks=[custom_lambda_clb])
    except KeyboardInterrupt:
        print("Interupt caught: curr best val_acc: {}".format(lbd_ob.max_acc))
        sys.exit(0)
    accs = sorted(hist.history['val_acc'])[-5:]
    accs = list(map(lambda x: round(x, 4), accs))
    acc = max(hist.history['val_acc'])
    print(model.name, acc)
    return acc, accs

# base_acc, base_accs = md_train(model)
# x_acc, x_accs = md_train(xmodel)
single_acc, single_accs = md_train(single_model)
# double_acc, double_accs = md_train(double_model)

print('----------RESULTS--------')
# print("Old model:", base_acc, base_accs)
# print("XCNN:", x_acc, x_accs)
print('X-CNN single_model: ', single_acc, single_accs)
# print('X-CNN double_model: ', double_acc, double_accs)
builder.print_report()

# 1


# SWDV 610: Data Structures and Algorithms
# Recursive function that reverses a list

def reverse_list(lst, start=0, stop=None):
    """Reverses the order of a list from start to stop indexes"""
    if stop == None: stop = len(lst) - 1

    if stop - start >= 1:
        lst[start], lst[stop] = lst[stop], lst[start]
        reverse_list(lst, start+1, stop-1)

    return lst

if __name__ == '__main__':
    test_1 = reverse_list([1, 2, 3, 4, 5])
    test_2 = reverse_list([14, 17, 21, 257, 4, 51])
    test_3 = reverse_list([1, 2])
    test_4 = reverse_list([])

    print(test_1)          # -> [5, 4, 3, 2, 1]
    print(test_2)          # -> [51, 4, 257, 21, 17, 14]
    print(test_3)          # -> [2, 1]
    print(test_4)          # -> []


array = {161,182,161,154, 176,170,167,171,170,174}

def average(array):
    sum = 0
    array = set(array)
    n = len(array)
    for num in array:
        sum += num
    avg = sum/n
    out = round(avg,3)
    return out

out = average(array)
print(out)