import collections
import threading

import numpy as np

from tensorflow.core.protobuf import debug_event_pb2
from tensorflow.python.debug.lib import op_callbacks_common
from tensorflow.python.debug.lib import source_utils
from tensorflow.python.eager import monitoring
from tensorflow.python.framework import op_callbacks
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gen_debug_ops
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.util import compat
from tensorflow.python.util import object_identity
from tensorflow.python.util.tf_export import tf_export

# Many ops have benign NaN outputs, and running them with check_numerics
# on will create unwanted errors
# TODO(b/142497024): Replace this allowlist with function decorators in the ops
IGNORE_OP_OUTPUTS = (
    # For FusedBatchNorm, if the input tensor is empty then batch_mean and
    # batch_variance will be NaN. reserve_space holds intermediate values
    # derived from batch_mean and batch_variance used for gradient calculation
    (b"FusedBatchNorm", 1),  # batch_mean
    (b"FusedBatchNorm", 2),  # batch_variance
    (b"FusedBatchNorm", 3),  # reserve_space_1
    (b"FusedBatchNorm", 4),  # reserve_space_2

    # Same as above
    (b"FusedBatchNormV2", 1),  # batch_mean
    (b"FusedBatchNormV2", 2),  # batch_variance
    (b"FusedBatchNormV2", 3),  # reserve_space_1
    (b"FusedBatchNormV2", 4),  # reserve_space_2

    # Same as above, but reserve_space_3 holds additional intermediate values
    (b"FusedBatchNormV3", 1),  # batch_mean
    (b"FusedBatchNormV3", 2),  # batch_variance
    (b"FusedBatchNormV3", 3),  # reserve_space_1
    (b"FusedBatchNormV3", 4),  # reserve_space_2
    (b"FusedBatchNormV3", 5),  # reserve_space_3
)

# Some frequently used ops are generally safe and we can skip them to reduce
# overhead. NOTE: This list is compiled by observing operations called by
# models in practice and is not a comprehensive list of safe operations.
SAFE_OPS = (
    b"Concat",
    b"ConcatV2",
    b"ExpandDims",
    b"Fill",
    b"Gather",
    b"Maximum",
    b"Minimum",
    b"Reshape",
    b"Slice",
    b"Squeeze",
    b"Stack",
    b"StridedSlice",
    b"StridedSliceGrad",
    b"TensorListConcatV2",
    b"TensorListGather",
    b"TensorListGetItem",
    b"TensorListPopBack",
    b"TensorListStack",
    b"Transpose",
    b"Unpack",
)

_state = threading.local()

_check_numerics_callback_create_counter = monitoring.Counter(
    "/tensorflow/api/python/debugging/check_numerics_callback_create_counter",
    "Counter for number of times the check_numerics op callback is created.")

def limit_string_length(string, max_len=50):
  """Limit the length of input string.

  Args:
    string: Input string.
    max_len: (int or None) If int, the length limit. If None, no limit.

  Returns:
    Possibly length-limited string.
  """
  if max_len is None or len(string) <= max_len:
    return string
  else:
    return "..." + string[len(string) - max_len:]

# A dictionary that supports looking up the original input tensor names.
_CHECK_NUMERICS_INPUT_LOOKUP = collections.defaultdict(dict)

def _maybe_lookup_original_input_tensor(graph, tensor):
  if (graph and
      graph in _CHECK_NUMERICS_INPUT_LOOKUP and
      tensor.name in _CHECK_NUMERICS_INPUT_LOOKUP[graph]):
    return _CHECK_NUMERICS_INPUT_LOOKUP[graph][tensor.name]
  else:
    return tensor

def get_check_numerics_error_message(slot,
                                     num_outputs,
                                     op_type,
                                     tensor,
                                     inputs,
                                     graph=None,
                                     traceback=None,
                                     stack_height_limit=30,
                                     path_length_limit=50):
  """Create a meaningful and user-friendly error message about offending tensor.

  The error message reveals the following info about the op that outputs
  NaN/Infinity: dtype, shape (to the extent known at graph-construction time),
  input tensors, stack trace for op creation (if is graph mode).

  Args:
    slot: (int) slot index of the tensor output.
    num_outputs: (int) total number of outputs of the op.
    op_type: (str) Type of the that generates `tensor`.
    tensor: (Tensor) the offending tensor, i.e., the tensor that contains
      Infinities or NaNs.
    inputs: (array of Tensor) inputs to the op that generates `tensor`.
    graph: (tf.Graph) the graph object that `tensor` belongs to. Available only
      under graph mode.
    traceback: (list of trace frames) the stack trace of the op's creation.
      Available only under graph model.
    stack_height_limit: (int or None) If int, limit to the height of the stack
      trace printed in the error message. If None, no limit to the height.
    path_length_limit: (int or None) Length limit for file paths included in the
      formatted stack trace.

  Returns:
    (str) A formatted error message.
  """
  eager_vs_graph_qualifier = "graph" if graph else "eagerly-executing"
  message = "\n"
  message += (
      "\n!!! Detected Infinity or NaN in output %d of "
      "%s op \"%s\" (# of outputs: %d) !!!\n" %
      (slot, eager_vs_graph_qualifier, op_type, num_outputs))

  message += "  dtype: %s\n" % tensor.dtype
  message += "  shape: %s\n" % (tensor.shape,)

  if not graph:
    # This is an eager tensor. We can get its numpy value and count
    # NaNs and Infs.
    is_inf = np.isinf(tensor)

    num_neg_inf = np.sum(np.logical_and(np.less(tensor, 0.), is_inf))
    num_pos_inf = np.sum(np.logical_and(np.greater(tensor, 0.), is_inf))
    num_nan = np.sum(np.isnan(tensor))
    if num_neg_inf > 0:
      message += "  # of -Inf elements: %s\n" % num_neg_inf
    if num_pos_inf > 0:
      message += "  # of +Inf elements: %s\n" % num_pos_inf
    if num_nan:
      message += "  # of +NaN elements: %s\n" % num_nan

  if len(inputs) > 1:
    message += "\n  Input tensors (%d):\n" % len(inputs)
    for slot, input_tensor in enumerate(inputs):
      message += "         %d: %s\n" % (
          slot, _maybe_lookup_original_input_tensor(graph, input_tensor))
  elif len(inputs) == 1:
    message += "\n  Input tensor: %s\n" % (
        _maybe_lookup_original_input_tensor(graph, inputs[0]))
  if graph and hasattr(graph, "name") and graph.name:
    message += "  Graph name: \"%s\"\n" % graph.name

  # Format the stack trace for the op's creation. We omit files that
  # belong to tensorflow itself.
  if graph and traceback:
    message += (
        "\n  Stack trace of op's creation (\"->\": inferred user code):\n")
    if stack_height_limit is not None and len(traceback) > stack_height_limit:
      num_omitted_frames = len(traceback) - stack_height_limit
      message += "    + ... (Omitted %d frames)\n" % num_omitted_frames
    for filepath, lineno, function_name, source_line in traceback[
        -stack_height_limit:]:
      user_code_indicator = "    "
      if not source_utils.guess_is_tensorflow_py_library(filepath):
        user_code_indicator = " -> "

      message += "    + %s (L%d) %s\n" % (
          limit_string_length(filepath, path_length_limit), lineno,
          function_name)
      if source_line is not None:
        message += "%s|   %s\n" % (user_code_indicator, source_line)
  message += "\n"
  return message

def _debug_summary(x):
  return gen_debug_ops.debug_numeric_summary_v2(
      x,
      tensor_debug_mode=(
          debug_event_pb2.TensorDebugMode.REDUCE_INF_NAN_THREE_SLOTS))

class CheckNumericsCallback(object):
  """Wrapper for the numerics-checking callback for thread locality."""

  def __init__(self, stack_height_limit, path_length_limit):
    self._stack_height_limit = stack_height_limit
    self._path_length_limit = path_length_limit
    # A dict mapping Placeholder tensors to their instrumenting debug tensors.
    # Used only under V1 graph mode, where we can't rely on auto control
    # dependency to execute the debug tensors and hence need to attach the debug
    # tensors as control dependencies of the ops that consume the Placeholder.
    self._placeholder_to_debug_tensor = (
        object_identity.ObjectIdentityDictionary())

  def callback(self,
               op_type,
               inputs,
               attrs,
               outputs,
               op_name=None,
               graph=None):
    """Eager-function unified callback for checking numerics."""
    del attrs, op_name  # Unused
    op_type_bytes = compat.as_bytes(op_type)
    is_v1_graph_mode = not ops.executing_eagerly_outside_functions()
    if (op_type_bytes in op_callbacks_common.OP_CALLBACK_SKIP_OPS or
        op_type_bytes in SAFE_OPS):
      return None
    if graph:
      # Under graph mode. Insert check_numerics op.
      instrumented_outputs = []
      if is_v1_graph_mode:
        for input_tensor in inputs:
          if input_tensor in self._placeholder_to_debug_tensor and outputs:
            outputs[0].op._add_control_input(  # pylint: disable=protected-access
                self._placeholder_to_debug_tensor[input_tensor].op)
      for slot, output in enumerate(outputs):
        if (output.dtype.is_floating and
            (op_type_bytes, slot) not in IGNORE_OP_OUTPUTS):
          checked_output = array_ops.check_numerics_v2(
              # TF v2 has automatic control dependencies added to stateful async
              # ops, which allows us to run check_numerics asynchronously.
              # In the above case we use debug_summary to reduce all output
              # tensors asynchronously from the op being checked and then
              # process the tensor summary with check_numerics.
              output if is_v1_graph_mode else _debug_summary(output),
              get_check_numerics_error_message(
                  slot,
                  len(outputs),
                  op_type,
                  output,
                  inputs,
                  graph=graph,
                  traceback=output.op.traceback,
                  stack_height_limit=self._stack_height_limit,
                  path_length_limit=self._path_length_limit))
          _CHECK_NUMERICS_INPUT_LOOKUP[graph][checked_output.name] = output
          instrumented_outputs.append(self._get_output_tensor(
              op_type_bytes, output, checked_output, is_v1_graph_mode))
        else:
          instrumented_outputs.append(output)
      return instrumented_outputs
    else:
      if op_type_bytes == b"CheckNumericsV2":
        # TODO(b/140334369): Remove this special casing logic once op_callback.
        # automatically prevents infinite recursion in eager mode.
        return None
      # Under eager mode. Eagerly execute check_numerics op.
      for slot, output in enumerate(outputs):
        if (output.dtype.is_floating and
            (op_type_bytes, slot) not in IGNORE_OP_OUTPUTS):
          array_ops.check_numerics_v2(
              output,
              get_check_numerics_error_message(
                  slot, len(outputs), op_type, output, inputs,
                  stack_height_limit=self._stack_height_limit,
                  path_length_limit=self._path_length_limit))

  def _get_output_tensor(self,
                         op_type,
                         tensor,
                         checked_tensor,
                         is_v1_graph_mode):
    """Determine what tensor to output from callback.

    Args:
      op_type: Type of the op that outputs the original symbolic tensor, as
        `bytes`.
      tensor: The original output symbolic tensor.
      checked_tensor: The debugger-instrumented, numerics-checking tensor.
      is_v1_graph_mode: Whether the debugged proggram is running under V1 graph
        mode.

    Returns:
      A symbolic tensor to be returned by the dumping op_callback.
    """
    if is_v1_graph_mode:
      # Placeholders need special treatment under V1 graph mode. The
      # callback can't simply override the Placeholder tensor to the debug
      # tensor, as that would cause the Placeholder op to lack a value.
      # The debug tensor is remembered and will be attached as control
      # inputs to ops that consumer the Placeholders later.
      if op_type == b"Placeholder":
        self._placeholder_to_debug_tensor[tensor] = checked_tensor
        return tensor
      else:
        return checked_tensor
    else:
      # Under non-v1 graph mode, rely on auto control dependency to run the
      # checked tensor.
      return tensor

@tf_export("debugging.enable_check_numerics")
def enable_check_numerics(stack_height_limit=30,
                          path_length_limit=50):
  r"""Enable tensor numerics checking in an eager/graph unified fashion.

  The numerics checking mechanism will cause any TensorFlow eager execution or
  graph execution to error out as soon as an op's output tensor contains
  infinity or NaN.

  This method is idempotent. Calling it multiple times has the same effect
  as calling it once.

  This method takes effect only on the thread in which it is called.

  When a op's float-type output tensor contains any Infinity or NaN, an
  `tf.errors.InvalidArgumentError` will be thrown, with an error message that
  reveals the following information:
    - The type of the op that generated the tensor with bad numerics.
    - Data type (dtype) of the tensor.
    - Shape of the tensor (to the extent known at the time of eager execution
      or graph construction).
    - Name of the containing graph (if available).
    - (Graph mode only): The stack trace of the intra-graph op's creation,
      with a stack-height limit and a path-length limit for visual clarity.
      The stack frames that belong to the user's code (as opposed to
      tensorflow's internal code) are highlighted with a text arrow ("->").
    - (Eager mode only): How many of the offending tensor's elements are
      `Infinity` and `NaN`, respectively.

  Once enabled, the check-numerics mechanism can be disabled by using
  `tf.debugging.disable_check_numerics()`.

  Example usage:

  1. Catching infinity during the execution of a `tf.function` graph:

     ```py
     import tensorflow as tf

     tf.debugging.enable_check_numerics()

     @tf.function
     def square_log_x_plus_1(x):
       v = tf.math.log(x + 1)
       return tf.math.square(v)

     x = -1.0

     # When the following line runs, a function graph will be compiled
     # from the Python function `square_log_x_plus_1()`. Due to the
     # `enable_check_numerics()` call above, the graph will contain
     # numerics checking ops that will run during the function graph's
     # execution. The function call generates an -infinity when the Log
     # (logarithm) op operates on the output tensor of the Add op.
     # The program errors out at this line, printing an error message.
     y = square_log_x_plus_1(x)
     z = -y
    ```

  2. Catching NaN during eager execution:

     ```py
     import numpy as np
     import tensorflow as tf

     tf.debugging.enable_check_numerics()

     x = np.array([[0.0, -1.0], [4.0, 3.0]])

     # The following line executes the Sqrt op eagerly. Due to the negative
     # element in the input array, a NaN is generated. Due to the
     # `enable_check_numerics()` call above, the program errors immediately
     # at this line, printing an error message.
     y = tf.math.sqrt(x)
     z = tf.matmul(y, y)
     ```

  NOTE: If your code is running on TPUs, be sure to call
  `tf.config.set_soft_device_placement(True)` before calling
  `tf.debugging.enable_check_numerics()` as this API uses automatic outside
  compilation on TPUs. For example:

  ```py
  tf.config.set_soft_device_placement(True)
  tf.debugging.enable_check_numerics()

  resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='')
  strategy = tf.distribute.TPUStrategy(resolver)
  with strategy.scope():
    # ...
  ```

  Args:
    stack_height_limit: Limit to the height of the printed stack trace.
      Applicable only to ops in `tf.function`s (graphs).
    path_length_limit: Limit to the file path included in the printed stack
      trace. Applicable only to ops in `tf.function`s (graphs).
  """
  if not hasattr(_state, "check_numerics_callback"):
    _state.check_numerics_callback = CheckNumericsCallback(
        stack_height_limit, path_length_limit)
  op_callbacks.add_op_callback(_state.check_numerics_callback.callback)

  logging.info(
      "Enabled check-numerics callback in thread %s",
      threading.current_thread().name)
  _check_numerics_callback_create_counter.get_cell().increase_by(1)

@tf_export("debugging.disable_check_numerics")
def disable_check_numerics():
  """Disable the eager/graph unified numerics checking mechanism.

  This method can be used after a call to `tf.debugging.enable_check_numerics()`
  to disable the numerics-checking mechanism that catches infinity and NaN
  values output by ops executed eagerly or in tf.function-compiled graphs.

  This method is idempotent. Calling it multiple times has the same effect
  as calling it once.

  This method takes effect only on the thread in which it is called.
  """
  if not hasattr(_state, "check_numerics_callback"):
    return
  try:
    op_callbacks.remove_op_callback(_state.check_numerics_callback.callback)
    delattr(_state, "check_numerics_callback")
    logging.info(
        "Disabled check-numerics callback in thread %s",
        threading.current_thread().name)
  except KeyError:
    # Tolerate disabling the check numerics callback without
    # enable_check_numerics() being called first.
    pass


import pytest

from app.posts.dao.bookmarks_dao import BookmarksDAO

class TestBookmarksDAO:

    @pytest.fixture
    def bookmarks_dao(self):
        return BookmarksDAO('data/bookmarks.json')

    @pytest.fixture
    def keys_expected(self):
        return {"poster_name", "poster_avatar", "pic", "content", "views_count", "likes_count", "pk"}

    @pytest.fixture
    def post(self):
        return {
            "poster_name": "larry",
            "poster_avatar": "https://randus.org/avatars/m/81898dbdbdffdb18.png",
            "pic": "https://images.unsplash.com/photo-1494952200529-3ceb822a75e2?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=880&q=80",
            "content": "Утром отправились на катере #Путешествия обследовать ближайшие острова – острова в основном каменные, бесполезные и необитаемые. На обратном пути попали в бурю, и нас чуть не унесло в океан. В течение 10 минут наш катер несся к отмели, а потом мы стали дрейфовать между скал, держась за трос. Наконец погода наладилась и мы смогли совершить обратный путь. Когда уже прибыли домой, я попросил, чтобы на следующий день нам устроили на катере экскурсию по морю. Нас провели по морскому дну от одного острова к другому, показали различные интересные объекты, которые встречаются в этом районе.",
            "views_count": 141,
            "likes_count": 45,
            "pk": 8
        }

    # Все посты
    def test_get_bookmarks_all_check_type(self, bookmarks_dao):
        bookmarks = bookmarks_dao.get_bookmarks_all()
        assert type(bookmarks) == list, 'Все закладки должны быть списком'
        for bookmark in bookmarks:
            assert type(bookmark) == dict, 'Каждая закладка должен быть словарём'

    def test_get_bookmarks_all_keys(self, keys_expected, bookmarks_dao):
        bookmarks = bookmarks_dao.get_bookmarks_all()
        for i in range(len(bookmarks)):
            bookmark = bookmarks[i]
            bookmark_keys = set(bookmark.keys())
            assert bookmark_keys == keys_expected, 'Полученные ключи не верны'

    def test_post_add_to_bookmarks(self, bookmarks_dao, post):
        bookmarks = bookmarks_dao.get_bookmarks_all()
        bookmarks_dao.post_add_to_bookmarks(post)
        bookmarks_added = bookmarks_dao.get_bookmarks_all()
        assert len(bookmarks_added) > len(bookmarks), 'Пост не добавлен'

    def test_post_remove_bookmarks(self, bookmarks_dao, post):
        bookmarks = bookmarks_dao.get_bookmarks_all()
        bookmarks_dao.post_remove_bookmarks(post)
        bookmarks_deleted = bookmarks_dao.get_bookmarks_all()
        assert len(bookmarks_deleted) < len(bookmarks), 'Пост не удалён'


import json
import logging as log
from flask import Blueprint, jsonify, request, make_response, current_app
from cerberus import Validator

from app import db
from app.habits_api.models import Habit, Activity
from app.habits_api.encoders import HabitEncoder, DeepHabitEncoder

habits_api = Blueprint('habits_api', __name__, url_prefix='/habits')

@habits_api.route('/', methods=['GET'])
def habits():
    habits = Habit.query.all()
    habits_json = json.dumps(habits, cls=DeepHabitEncoder)

    log.info('Sending list of habits: {}'.format(habits_json))
    return make_response(habits_json, 200)

@habits_api.route('/', methods=['POST'])
def add_habit():
    data = request.get_json(silent=True)
    validator = Validator()
    schema = {'name': {'required': True, 'type': 'string', 'maxlength': 50}}

    if data is None or not validator.validate(data, schema):
        log.info('Received bad data from user: {}'.format(request.data))
        log.info('Sending 400 Bad Request')
        return make_response("Bad Request", 400)

    new_habit = Habit(name=data['name'])
    new_habit.save()
    new_habit_json = json.dumps(new_habit, cls=HabitEncoder)

    log.info('Added a new habit: {}'.format(new_habit))
    log.info('Sending 201 Created with new habit: {}'.format(new_habit))
    return make_response(new_habit_json, 201)

@habits_api.route('/<id>', methods=['GET'])
def show_habit(id):
    habit = Habit.query.get(id)
    if habit is None:
        log.info('Habit not found. id: {}'.format(id))
        log.info('Sending 404 Not Found')
        return make_response("Not Found", 404)

    habit_json = json.dumps(habit, cls=HabitEncoder)

    log.info('Sending habit: {}'.format(habit))
    return make_response(habit_json, 200)

@habits_api.route('/<id>', methods=['DELETE'])
def delete_habit(id):
    habit = Habit.query.get(id)
    if habit is None:
        log.info('Habit not found. id: {}'.format(id))
        log.info('Sending 404 Not Found')
        return make_response("Not Found", 404)

    habit.delete()
    habit_json = json.dumps(habit, cls=HabitEncoder)

    log.info('Deleted habit: {}'.format(habit))
    return make_response(habit_json, 200)

@habits_api.route('/<id>', methods=['PUT'])
def update_habit(id):
    habit = Habit.query.get(id)

    if habit is None:
        log.info('Habit not found. id: {}'.format(id))
        log.info('Sending 404 Not Found')
        return make_response('Not Found', 404)

    data = request.get_json(silent=True)
    validator = Validator()
    schema = {'name': {'required': True, 'type': 'string', 'maxlength': 50}}

    if data is None or not validator.validate(data, schema):
        log.info('Received bad data from user: {}'.format(request.data))
        log.info('Sending 400 Bad Request')
        return make_response('Bad Request', 400)

    habit.name = data['name']
    db.session.commit()
    habit_json = json.dumps(habit, cls=HabitEncoder)

    log.info('Habit with id {} updated: {}'.format(id, habit))
    return make_response(habit_json, 200)

@habits_api.route('/batch-delete/', methods=['POST'])
def batch_delete():
    data = request.get_json(silent=True)

    if data is None or len(data) == 0:
        log.info('Received bad data from user: {}'.format(request.data))
        log.info('Sending 400 Bad Request')
        return make_response('Bad Request', 400)

    result = {'success': [], 'failure': []}

    for habit_id in data:
        habit = Habit.query.get(habit_id)

        if habit is None:
            result['failure'].append(habit_id)
        else:
            habit.delete()
            result['success'].append(habit_id)

    if len(result['success']) == 0:
        log.info('No habits found, ids: {}'.format(data))
        log.info('Sending 404 Not Found')
        return make_response('Not Found', 404)

    log.info('Batch deleted some/all of the habits, results: {}'.format(result))
    return make_response(json.dumps(result), 200)

@habits_api.route('/<id>/trigger/', methods=['GET'])
def trigger_habit(id):
    habit = Habit.query.get(id)

    if habit is None:
        log.info('Habit not found, id: {}'.format(id))
        log.info('Sending 404 Not Found')
        return make_response('Not Found', 404)

    activity = Activity(habit_id=id)
    db.session.add(activity)
    db.session.commit()

    activity_json = json.dumps(activity.as_dict())

    log.info('Added activity: {}'.format(activity_json))
    return make_response(activity_json, 200)

@habits_api.route('/<id>/activities/', methods=['GET'])
def list_triggers(id):
    habit = Habit.query.get(id)

    if habit is None:
        log.info('Habit not found, id: {}'.format(id))
        log.info('Sending 404 Not Found')
        return make_response('Not Found', 404)

    activities = habit.activities

    if activities is None:
        log.info('No activities found')
        log.info('Sending 404 Not Found')
        return make_response(json.dumps([]), 200)

    activities_json = json.dumps(
        list(map(lambda a: a.as_dict(), activities)))

    log.info('Sending activities: {}'.format(activities_json))
    return make_response(activities_json, 200)

@habits_api.route('/<idh>/activities/<ida>', methods=['GET'])
def get_activity_by_id(idh, ida):
    habit = Habit.query.get(idh)

    if habit is None:
        log.info('Habit not found, id: {}'.format(idh))
        log.info('Sending 404 Not Found')
        return make_response('Not Found', 404)

    activity = Activity.query.get(ida)

    if activity is None:
        log.info('Activity not found, id: {}'.format(ida))
        log.info('Sending 404 Not Found')
        return make_response('Not Found', 404)

    activity_json = json.dumps(activity.as_dict())

    log.info('Sending activity: {} for habit with id {}'.format(
        activity_json, id))
    return make_response(activity_json, 200)


import sys
sys.stdin = open('bj12100.txt','r')

import copy,itertools
N=int(input())
A=[[int(x)for x in input().split()]for y in range(N)]
R=0
Per=list(itertools.product(([0,1,2,3]),repeat=5))
for C in Per:
    M=copy.deepcopy(A)
    for c in C:
        for _ in range(c):M=list(map(list,zip(*M[::-1])))
        for y in range(N):
            for x in range(0,N-1):
                if M[y][x]:
                    for Q in range(x+1,N):
                        if M[y][Q]:
                            if M[y][x]==M[y][Q]:
                                M[y][x]*=2
                                M[y][Q]=0
                            break
        for j in range(N):
            for i in range(N-1):
                if not M[j][i]:
                    cnt=i+1
                    while cnt!=N:
                        if M[j][cnt]:
                            M[j][i]=M[j][cnt]
                            M[j][cnt]=0
                            break
                        cnt+=1
        if not c:
            for _ in range(4-c):M=list(map(list,zip(*M[::-1])))
    for y in range(N):
        for x in range(N):
            if M[y][x] and M[y][x]>R:R=M[y][x]
print(R)


from typing import Callable, Coroutine, Tuple

from aiohttp import web

from aiohttp_tokenauth import utils

def token_auth_middleware(user_loader: Callable,
                          request_property: str = 'user',
                          auth_scheme: str = 'Bearer',
                          exclude_routes: Tuple = tuple(),
                          exclude_methods: Tuple = tuple()) -> Coroutine:
    """Checks a auth token and adds a user from user_loader in request.

    Aiohttp token auth middleware, that checks the "Authorization" http header
    for token and, if it valid, runs the "user_loader" callback. If user loader
    returns a user, then middleware adds the user to request with key that
    contain the "request_property" variable, else it will raise an HTTPForbidden
    exception.

    Args:
        user_loader (Callable): User loader callback. Must return a user or
            None if user doesn't found by token.
        request_property (str, optional): Key for save in request object.
            Defaults to 'user'.
        auth_scheme (str, optional): Prefix for value in "Authorization" header.
            Defaults to 'Bearer'.
        exclude_routes: (Tuple, optional): Tuple of pathes that will be excluded.
            Defaults to empty tuple.
        exclude_methods(Tuple, optional): Tuple of http methods that will be
            excluded. Defaults to empty tuple.

    Raises:
        TypeError: If user_loader isn't callable object.
        web.HTTPUnauthorized: If "Authorization" token is missing.
        web.HTTPForbidden: Wrong token, schema or header.

    Returns:
        Coroutine: Aiohttp middleware.
    """
    if not callable(user_loader):
        raise TypeError('Must be callable')

    @web.middleware
    async def middleware(request, handler):
        if (utils.is_exclude(request, exclude_routes) or
                request.method in exclude_methods):
            return await handler(request)

        try:
            scheme, token = request.headers['Authorization'].strip().split(' ')
        except KeyError:
            raise web.HTTPUnauthorized(reason='Missing authorization header',)
        except ValueError:
            raise web.HTTPForbidden(reason='Invalid authorization header',)

        if auth_scheme.lower() != scheme.lower():
            raise web.HTTPForbidden(reason='Invalid token scheme',)

        user = await user_loader(token)
        if user:
            request[request_property] = user
        else:
            raise web.HTTPForbidden(reason='Token doesn\'t exist')

        return await handler(request)

    return middleware


import pandas as pd
from tqdm import tqdm
import os
import sys
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.pipeline import Pipeline
from sklearn import svm
from sklearn.metrics import classification_report as report
from sklearn.feature_extraction.text import CountVectorizer
import argparse
from sklearn.multioutput import MultiOutputClassifier
from sklearn.preprocessing import MultiLabelBinarizer

from transformers import BertTokenizerFast, XLMRobertaTokenizerFast
# from enc_t5 import EncMT5ForSequenceClassification, EncT5Tokenizer

from ast import literal_eval
import re

import emoji

from sklearn.model_selection import StratifiedKFold
import numpy as np

class LabelBinarizer():
    def __init__(self, *, pathtolabelmap = './processed_data/frame_map.tsv'):
        self.pathtolabelmap = pathtolabelmap
        #load frame map
        self.df_framelabels = pd.read_csv(pathtolabelmap, sep = '\t', header = 0, names = ['frame_number', 'frame_label'])
        #note the first label 'Economic' has an index of 0 but a frame_number of 1 (frame_number is 1-indexed, so Economic is frame_1)

    #string ('Economic, Health, Jurisprudence') to binary array (e.g. [1,0,0,1,0...,1,0,0]).
    def frame_to_label(self, frame_string: str):
        labels = [0] * len(self.df_framelabels)

        for index, row in self.df_framelabels.iterrows():
            if row['frame_label'] in frame_string:
                labels[index] = 1
            else:
                labels[index] = 0

        return labels

    #returns frames as array of strings
    def label_to_frame(self, label_arrray):
        frames = []
        if type(label_arrray) == float:
            print("WARNING FLOAT")
            print(label_arrray)
        for idx, val in enumerate(label_arrray):
            if val == 1:
                frames.append(self.df_framelabels['frame_label'].iloc[idx])

        return frames

#loads from tsv, adds a column 'label' (vector form of the string of frames)
def load_df_from_tsv(pathtodata, use_translations = 'original', convert_labels = True, merge_labels = False):
    df = pd.read_csv(pathtodata, sep = '\t')

    if use_translations != 'original':
        print('Replacing original langauge with translations in ', use_translations)
        print('Before: ', df.iloc[0])

        col_name = 'text_' + use_translations

        df['text'] = df[col_name]
        print('After: ', df.iloc[0])

    if convert_labels:
        encoder = LabelBinarizer()

        if 'frames' in df.columns:
            df['label']= df['frames'].apply(encoder.frame_to_label)
    return df

def load_data(pathtodata, basemodel, maxlength, convert_labels = True, use_translations = 'original', truncation_side = 'right'):

    #load data
    df = load_df_from_tsv(pathtodata, use_translations, convert_labels)

    print('Loaded dataframe. Number of entries: ', df.shape[0])

    #load tokenizer - probably the better way to do this would be to pass the instantiated tokenizer... oh well
    if 'google/mt5' in basemodel:
        tokenizer = EncT5Tokenizer.from_pretrained("google/mt5-base", truncation_side=truncation_side)
    elif 'xlm-roberta' in basemodel:
        tokenizer = XLMRobertaTokenizerFast.from_pretrained(basemodel, truncation_side=truncation_side)
    else:
        tokenizer = BertTokenizerFast.from_pretrained(basemodel, truncation_side=truncation_side)

    #construct preprocess function
    def tokenize_function(text_col):
        tokenized = tokenizer(text_col, truncation=True, padding="max_length", max_length=maxlength)
        return tokenized

    df['input_ids'] = df['text'].apply(lambda x: tokenize_function(x)['input_ids'])
    df['attention_mask'] = df['text'].apply(lambda x: tokenize_function(x)['attention_mask'])

    return df

#returns dataframe with two columns -> pred_labels (array of 1 or 0), pred_frames (array of frames)
def logits_to_preds(logits, df_inference):
    predictions = (logits[:]>=0).astype(int)
    # print('Predictions from logits')
    # print(predictions)

    encoder = LabelBinarizer()

    df = pd.DataFrame()

    df["pred_labels"] = predictions.tolist()
#     print('just labels')
#     print(df)

    df["pred_frames"] = df["pred_labels"].apply(encoder.label_to_frame)
#     print('with frames')
#     print(df)

    return df

def make_dataframe(input_folder, labels_folder=None):
    #MAKE TXT DATAFRAME
    text = []

    for fil in tqdm(filter(lambda x: x.endswith('.txt'), os.listdir(input_folder))):
        print(fil)
        print(input_folder+fil)
        iD, txt = fil[7:].split('.')[0], open(input_folder +fil, 'r', encoding='utf-8').read()
        print(txt)
        text.append((iD, txt))

    df_text = pd.DataFrame(text, columns=['id','text']).set_index('id')
    df = df_text

    #MAKE LABEL DATAFRAME
    if labels_folder:
        labels = pd.read_csv(labels_folder, sep='\t', header=None)
        labels = labels.rename(columns={0:'id',1:'frames'})
        labels.id = labels.id.apply(str)
        labels = labels.set_index('id')

        #JOIN
        df = labels.join(df_text)[['text','frames']]

    return df

def produce_joined_data(path_to_data_folder = 'data/', save_location = 'st2/processed_data/'):
    joined_df = pd.DataFrame(columns = ['id', 'lang', 'text', 'frames', 'dataset_origin'])
    for lang in ['en', 'fr', 'ge', 'it', 'po', 'ru']:
        for dataset_origin in ['train', 'dev']:
            suffix = '-subtask-2'

            articles_path = path_to_data_folder + lang + '/' + dataset_origin + '-articles' + suffix + '/'
            labels_path = path_to_data_folder + lang + '/' + dataset_origin + '-labels' + suffix + '.txt'
            df = make_dataframe(articles_path, labels_path)
            df['dataset_origin'] = df['text'].apply(lambda x: dataset_origin)
            df['lang'] = df['text'].apply(lambda x: lang)

            save_path = save_location + dataset_origin + '.tsv'
            df.to_csv(save_path, sep = '\t')

            joined_df = pd.concat([joined_df, df], ignore_index = True)

    save_path = save_location + 'joined.tsv'
    joined_df.to_csv(save_path, sep = '\t')

def holdout_lang(df, lang = 'it'):
    should_holdout = df['lang'] == lang

    df_holdout = df[should_holdout]
    df_filtered = df[~should_holdout]

    return {'held_out': df_holdout, 'filtered': df_filtered}


import asyncio
import aiohttp
import aiofiles
import json
import logging
import signal
import sys

import utils
import configs
from flight import Flight

logging.basicConfig(
    format="%(asctime)s %(levelname)s:%(name)s: %(message)s",
    datefmt="%H:%M:%S",
    filename=configs.LOGGING_FILE,
    level=logging.DEBUG
)
logger = logging.getLogger('search_flight')

async def shutdown(sig, event_loop):
    """
    Cancel all coroutines after killing process by signal and stop the event loop

    Attributes:
        sig (enum): type of signal
        event_loop: event loop that run asynchronous tasks and callbacks
    """
    logger.info('Received exit signal {}'.format(sig.name))
    tasks = [t for t in asyncio.Task.all_tasks() if t is not
             asyncio.tasks.Task.current_task()]
    for task in tasks:
        task.cancel()
    logger.info('Cancelling {} outstanding tasks'.format(tasks))
    results = await asyncio.gather(*tasks, return_exceptions=True)
    logger.info('Finished awaiting cancelled tasks, results: {}'.format(results))
    event_loop.stop()

class Crawler:
    """
    Web Crawler periodically search flights from Kiwi Api and validates them.

    Attributes:
        session (ClientSession): interface for making HTTP requests
        semaphore (Semaphore): allow us to protect resources from being overused
        flights (dict): contains all found flights, where key is combination of fly_from, fly_to, dep_date
        date_from (str): search flights from this date (dd/mm/YYYY) which is current day
        date_to (str): search flights upto this date (dd/mm/YYYY) which is next month first day
        flight_directions (list): list of need to search flight directions
    """

    def __init__(self, session):
        """
        The constructor for Flight class
        """
        self.session = session
        self.semaphore = asyncio.Semaphore(configs.MAX_TASKS)
        self.flights = {}
        self.date_from, self.date_to = utils.get_date_range()
        self.flight_directions = configs.FLIGHT_DIRECTIONS

    async def send_request(self, url):
        """
        Send GET request and converts response data to dict

        Attributes:
            url (str) : url where need to send get request
        Returns:
            dict: response data where all found flight information
        """
        try:
            response = await self.session.request(method='GET', url=url)
            response.raise_for_status()
            logger.info("Got response [%s] for URL: %s", response.status, url)
            data = await response.read()
            return json.loads(data)

        except aiohttp.ClientError as e:
            logger.error(
                "aiohttp exception for %s [%s]: %s",
                url,
                getattr(e, "status", None),
                getattr(e, "message", None),
            )

    async def search_flight(self, fly_from, fly_to, date_from, date_to):
        """
        Search flight with specific parameters and store in flights dict
        (the cheapest flight for one particular day)

        Attributes:
            fly_from (str): departure place
            fly_to (str): destination
            date_from (str): departure date
            date_to (str): arrival date
        """
        search_url = configs.SEARCH_FLIGHT_URL.format(fly_from, fly_to,
                                                      date_from, date_to)
        response_data = await self.send_request(search_url)
        for json_data in response_data['data']:
            flight = Flight(json_data)
            key = '-'.join([flight.fly_from, flight.fly_to, str(flight.dep_date)])
            if key in self.flights and self.flights[key].is_valid and \
                    self.flights[key].price < flight.price:
                continue
            self.flights[key] = flight

    async def check_flight(self, flight):
        """
        Validate found flight and search new one in case of invalid flight
        or change price with new price

        Attributes:
            flight (Flight): instance of class Flight
        """
        check_url = configs.CHECK_FLIGHT_URL.format(flight.booking_token)
        retries = 0
        while True:
            async with self.semaphore:
                data = await self.send_request(check_url)
            if data['flights_checked']:
                break
            retries += 1
            await asyncio.sleep(configs.DELAY_TIME * retries)

        if data['flights_invalid']:
            flight.is_valid = False
            await self.search_flight(flight.fly_from, flight.fly_to,
                                     flight.dep_date, flight.arr_date)
        elif data['price_change']:
            flight.price = data['total']

    async def check_flights_periodically(self):
        """
        Periodically after CHECK_FLIGHT_PERIOD seconds checks flights
        """
        while True:
            tasks = [self.check_flight(flight) for flight in self.flights.values()]
            await asyncio.gather(*tasks)

            logger.info('Checked number of %d flights', len(tasks))
            await self.write_to_file()
            await asyncio.sleep(configs.CHECK_FLIGHT_PERIOD)

    async def write_to_file(self):
        """
        Write the found flights to file
        """
        async with aiofiles.open(configs.OUTPUT_FILE, "w") as f:
            for value in self.flights.values():
                await f.write(str(value) + '\n')
            logger.info('Wrote results to file')

    async def search_flights_periodically(self):
        """
        Periodically at midnight updates flights and writes results to output file
        """
        while True:
            self.flights = {}
            self.date_from, self.date_to = utils.get_date_range()
            tasks = [self.search_flight(flight_dir[0], flight_dir[1],
                                        self.date_from, self.date_to)
                     for flight_dir in self.flight_directions]
            await asyncio.gather(*tasks)

            logger.info('Found %d flights for given directions', len(self.flights))
            await self.write_to_file()
            sleep_period = utils.get_sleep_period()
            await asyncio.sleep(sleep_period)

async def main():
    """
    Initialize crawler and start coroutines
    """
    async with aiohttp.ClientSession() as session:
        crawler = Crawler(session)
        await asyncio.gather(*[crawler.search_flights_periodically(),
                               crawler.check_flights_periodically()])

if __name__ == '__main__':
    assert sys.version_info >= (3, 6), 'Script requires Python 3.6+.'

    loop = asyncio.get_event_loop()

    # types of signal need to listen in order to properly shutdown
    signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT)
    for s in signals:
        loop.add_signal_handler(s, lambda s=s: asyncio.ensure_future(shutdown(s, loop)))

    asyncio.ensure_future(main())
    try:
        loop.run_forever()
    finally:
        loop.close()


from django.urls import path
from blog_board.views import *
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    path('', BlogListView.as_view(), name='blog_list'),
    path('<int:blog_id>/', detail_blog, name='blog_view'),
    path('create/', CreateBlogView.as_view(), name='blog_creation'),
    path('create/from_csv/', CreateBlogFromCSV.as_view(), name='blog_csv_creation'),
    path('<int:blog_id>/add_com/', CreateCommentView.as_view(), name='comment_creation'),
    path('<int:blog_id>/edit_com/<int:comm_id>/', EditCommentView.as_view(), name='edit_comment'),
]+ static(
        settings.MEDIA_URL, document_root=settings.MEDIA_ROOT
    )

#конструкция +static... помогает корректно обрабатывать ссылки на файлы


import xlsxwriter
import random

def create_workbook(unique_number):
    workbook = xlsxwriter.Workbook('excel_files/opdrachtbon_claus_' + unique_number + '.xlsx')
    worksheet = workbook.add_worksheet('opdrachtbon_blad')
    bold = workbook.add_format({'bold': True})
    format = workbook.add_format({'bold': True, 'font_color': 'red'})

    cell_ccl = workbook.add_format()
    #cell_ccl.set_pattern(1)
    #cell_ccl.set_bg_color(chb_red)
    cell_ccl.set_font_size(9)
    cell_ccl.set_align('right')
    #cell_ccl.set_font_color('white')

    cell_header = workbook.add_format()
    cell_header.set_font_size(9)

    cell_top = workbook.add_format()
    cell_top.set_align('top')
    cell_top.set_font_size(9)

    #create_contents_column_A():
    worksheet.write('A1', 'CLAUS BV', bold)
    worksheet.write('A2', 'In samenwerking met', cell_header)
    worksheet.write('A3',  'H.L. van den Boom B.V', cell_header)
    worksheet.insert_image('A4', 'image_files/claus_logo_klein.png')
    worksheet.insert_image('A9', 'image_files/hb_logo_klein.png')

    worksheet.write('A18', 'Opdrachtomschrijving', cell_top)
    worksheet.write('A19', 'Voorwaarden', cell_top)

    worksheet.write('A29', 'Didamseweg 6', cell_top)
    worksheet.write('A30', '6983 BB Doesburg', cell_top)

    worksheet.write('A32', '0313-472 475', cell_top)
    worksheet.write('A33', 'info@clausdoesburg.nl', cell_top)
    worksheet.write('A36', 'KvK 09096007', cell_top)

    worksheet.set_column('A:A', 20)

    #create_contents_column_B():
    worksheet.write('B1', 'Opdrachtnummer vermelden op afleverbon en factuur >', bold)
    worksheet.write('B4', 'Datum:', cell_ccl)

    worksheet.write('B6', 'Werkomschrijving:', cell_ccl)
    worksheet.write('B7', 'Werknummer:', cell_ccl)

    worksheet.write('B9', 'Afleveradres:',cell_ccl)
    worksheet.write('B10', 'Plaats:',cell_ccl)

    worksheet.write('B12', 'Leverancier:', cell_ccl)
    worksheet.write('B13', 'Contactpersoon:', cell_ccl)

    worksheet.write('B15', 'Leveringsdatum:', cell_ccl)
    worksheet.write('B16', 'Behandeld door:', cell_ccl)

    worksheet.set_row(17, 100)

    worksheet.merge_range('B18:C18','', cell_top)
    worksheet.merge_range('B19:D19', 'De genoemde prijzen zijn exclusief btw.', cell_top)
    worksheet.merge_range('B21:D21', '*uitsluitend voor zover niet in afwijking van bovenstaande gegevens en', cell_top)
    worksheet.merge_range('B22:D22', 'of de in of via deze opdracht vermelde omschrijving en toepasselijk', cell_top)

    worksheet.merge_range('B23:D23', 'verklaarde voorwaarden', cell_top)

    worksheet.merge_range('B25:D25', 'Uitsluitend volgens de algemene voorwaarden van onderaanneming ', cell_top)
    worksheet.merge_range('B26:D26', 'AVBB / FAANB 1993 dragen wij u de werkzaamheden omschreven  ', cell_top)
    worksheet.merge_range('B27:D27', 'bij opdrachtomschrijving op.  ', cell_top)

    worksheet.merge_range('B30:D30', 'Betaling zal plaatsvinden 30+5 dagen na levering', cell_top)
    worksheet.merge_range('B31:D31',  'gereed werkzaamheden en ontvangst van uw factuur', cell_top)

    worksheet.merge_range('B33:D33', 'Uw factuur met vermelding van opdrachtnummer en werknummer te verzenden naar', cell_top)
    worksheet.merge_range('B34:D34', 'facturen@clausdoesburg.nl', cell_top)

    worksheet.merge_range('B36:D36', 'Statiegeld pallets dienen retour genomen te worden', cell_top)

    worksheet.set_column('B:B',20)

    #create_contents_column_C():
    worksheet.set_column('C:C', 30)

    #create_contents_column_D():
    worksheet.write('D1', str(unique_number), bold)

    worksheet.set_column('D:D',10)

    #create_contents_column_A()
    #create_contents_column_B()
    #create_contents_column_C()
    #create_contents_column_D()

    workbook.close()

for i in range(0, 100):
    create_workbook(unique_number=str(i))


from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier

def get_model(model_name):
    models = {
    "KNN":KNeighborsClassifier,
    "SVM":SVC,
    "Random Forest":RandomForestClassifier
    }

    return models.get(model_name)


##
## Example script for parallel selenium tests
##
import unittest
import xmlrunner
from time import sleep
from TestdroidAppiumTest import TestdroidAppiumTest, log
from selenium.common.exceptions import WebDriverException

import os

from appium import webdriver
from altunityrunner import *
class BitbarSampleAppTest(TestdroidAppiumTest):
    altdriver = None
    driver = None

    platform = "android"

    def setUp(self):
        # TestdroidAppiumTest takes settings (local or cloud) from environment variables
        super(BitbarSampleAppTest, self).setUp()
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['deviceName'] = 'device'
        desired_caps['app'] = os.environ.get('APPIUM_APPFILE')
        driver = self.get_driver()  # Initialize Appium connection to device
        self.altdriver = AltrunUnityDriver(self.driver, self.platform)

    # Test start.
    # def test_the_app(self):
    #
    #     sleep(10) # Wait that the app loads
    #     log("Start!")
    #     # Use this to get detected screen hierarchy
    #     # print self.driver.page_source
    #
    #     if self.isAndroid():
    #         try:
    #             log("Taking screenshot 0_appLaunch.png")
    #             driver.save_screenshot(self.screenshot_dir + "/0_appLaunch.png")
    #             log("Clicking element 'Use Testdroid Cloud'")
    #             if self.isSelendroid():
    #                 elem = driver.find_element_by_xpath("//LinearLayout[1]/FrameLayout[1]/ScrollView[1]/LinearLayout[1]/LinearLayout[1]/RadioGroup[1]/RadioButton[2]")
    #             else:
    #                 elem = self.driver.find_element_by_android_uiautomator('new UiSelector().text("Use Testdroid Cloud")')
    #             self.assertTrue(elem)
    #             elem.click()
    #             sleep(2) # always sleep before taking screenshot to let transition animations finish
    #             log("Taking screenshot: 1_radiobuttonPressed.png")
    #             driver.save_screenshot(self.screenshot_dir + "/1_radiobuttonPressed.png")
    #
    #             log("Sleeping 3 before quitting webdriver")
    #             sleep(3)
    #         except WebDriverException:
    #             log("Android testrun failed..")
    #     else: # iOS
    #         try:
    #             log("Taking screenshot 0_appLaunch.png")
    #             driver.save_screenshot(self.screenshot_dir + "/0_appLaunch.png")
    #             log("Finding buttons")
    #             buttons = driver.find_elements_by_class_name('UIAButton')
    #             log("Clicking button [2] - Radiobutton 2")
    #             buttons[2].click()
    #
    #             log("Taking screenshot 1_radiobuttonPressed.png")
    #             driver.save_screenshot(self.screenshot_dir + "/1_radiobuttonPressed.png")
    #
    #             log("Sleeping 3 before quitting webdriver")
    #             sleep(3)
    #         except WebDriverException:
    #             log("iOS testrun failed..")

    def test_tap_ui_object(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        self.altdriver.find_element('UIButton').tap()
        self.altdriver.wait_for_element_with_text('CapsuleInfo', 'UIButton clicked to jump capsule!', '', 1)

    def test_tap_object(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        capsule_element = self.altdriver.find_element('Capsule')
        capsule_element.tap()
        self.altdriver.wait_for_element_with_text('CapsuleInfo', 'Capsule was clicked to jump!', '', 1)

    def test_tap_at_coordinates(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        capsule_element = self.altdriver.find_element('Capsule')
        self.altdriver.tap_at_coordinates(capsule_element.x, capsule_element.y)
        self.altdriver.wait_for_element_with_text('CapsuleInfo', 'Capsule was clicked to jump!', '', 1)

    def test_load_and_wait_for_scene(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        self.altdriver.wait_for_current_scene_to_be('Scene 1 AltUnityDriverTestScene', 1)
        self.altdriver.load_scene('Scene 2 Draggable Panel')
        self.altdriver.wait_for_current_scene_to_be('Scene 2 Draggable Panel', 1)

    def test_find_element(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        self.altdriver.find_element('Plane')
        self.altdriver.find_element('Capsule')

    def test_wait_for_element_with_text(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        text_to_wait_for = self.altdriver.find_element('CapsuleInfo').get_text()
        self.altdriver.wait_for_element_with_text('CapsuleInfo', text_to_wait_for, '', 1)

    def test_find_elements(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        planes = self.altdriver.find_elements("Plane")
        assert len(planes) == 2
        assert len(self.altdriver.find_elements("something that does not exist")) == 0

    def test_find_element_where_name_contains(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        self.altdriver.find_element_where_name_contains('Pla')

    def test_find_element_by_name_and_parent(self):
        capsule_element = self.altdriver.find_element('Canvas/CapsuleInfo')
        assert capsule_element.name == 'CapsuleInfo'

    def test_find_element_by_component(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        self.assertEqual(self.altdriver.find_element_by_component("Capsule").name, "Capsule")

    def test_find_elements_by_component(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        self.assertEqual(len(self.altdriver.find_elements_by_component("UnityEngine.MeshFilter")), 3)

    def test_get_component_property(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        result = self.altdriver.find_element("Capsule").get_component_property("Capsule", "arrayOfInts")
        self.assertEqual(result, "[1,2,3]")

    def test_set_component_property(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        self.altdriver.find_element("Capsule").set_component_property("Capsule", "arrayOfInts", "[2,3,4]")
        result = self.altdriver.find_element("Capsule").get_component_property("Capsule", "arrayOfInts")
        self.assertEqual(result, "[2,3,4]")

    def test_call_component_method(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        result = self.altdriver.find_element("Capsule").call_component_method("Capsule", "Jump", "setFromMethod")
        self.assertEqual(result, "null")
        self.altdriver.wait_for_element_with_text('CapsuleInfo', 'setFromMethod')
        self.assertEqual('setFromMethod', self.altdriver.find_element('CapsuleInfo').get_text())

    def test_pointer_enter_and_exit(self):
        self.altdriver.load_scene('Scene 3 Drag And Drop')

        alt_element = self.altdriver.find_element('Drop Image')
        color1 = alt_element.get_component_property('DropMe', 'highlightColor')
        alt_element.pointer_enter()
        color2 = alt_element.get_component_property('DropMe', 'highlightColor')
        self.assertNotEqual(color1, color2)

        alt_element.pointer_exit()
        color3 = alt_element.get_component_property('DropMe', 'highlightColor')
        self.assertNotEqual(color3, color2)
        self.assertEqual(color3, color1)

    def test_multiple_swipes(self):
        self.altdriver.load_scene('Scene 3 Drag And Drop')

        image1 = self.altdriver.find_element('Drag Image1')
        box1 = self.altdriver.find_element('Drop Box1')

        self.altdriver.swipe(image1.x, image1.y, box1.x, box1.y, 5)

        image2 = self.altdriver.find_element('Drag Image2')
        box2 = self.altdriver.find_element('Drop Box2')

        self.altdriver.swipe(image2.x, image2.y, box2.x, box2.y, 2)

        image3 = self.altdriver.find_element('Drag Image3')
        box1 = self.altdriver.find_element('Drop Box1')

        self.altdriver.swipe(image3.x, image3.y, box1.x, box1.y, 3)

        time.sleep(6)

        image_source = image1.get_component_property('UnityEngine.UI.Image', 'sprite')
        image_source_drop_zone = self.altdriver.find_element('Drop Image').get_component_property(
            'UnityEngine.UI.Image', 'sprite')
        self.assertNotEqual(image_source, image_source_drop_zone)

        image_source = image2.get_component_property('UnityEngine.UI.Image', 'sprite')
        image_source_drop_zone = self.altdriver.find_element('Drop').get_component_property('UnityEngine.UI.Image',
                                                                                'sprite')
        self.assertNotEqual(image_source, image_source_drop_zone)

    def test_multiple_swipe_and_waits(self):
        self.altdriver.load_scene('Scene 3 Drag And Drop')

        image2 = self.altdriver.find_element('Drag Image2')
        box2 = self.altdriver.find_element('Drop Box2')

        self.altdriver.swipe_and_wait(image2.x, image2.y, box2.x, box2.y, 2)

        image3 = self.altdriver.find_element('Drag Image3')
        box1 = self.altdriver.find_element('Drop Box1')

        self.altdriver.swipe_and_wait(image3.x, image3.y, box1.x, box1.y, 1)

        image1 = self.altdriver.find_element('Drag Image1')
        box1 = self.altdriver.find_element('Drop Box1')

        self.altdriver.swipe_and_wait(image1.x, image1.y, box1.x, box1.y, 3)

        image_source = image1.get_component_property('UnityEngine.UI.Image', 'sprite')
        image_source_drop_zone = self.altdriver.find_element('Drop Image').get_component_property(
            'UnityEngine.UI.Image', 'sprite')
        self.assertNotEqual(image_source, image_source_drop_zone)

        image_source = image2.get_component_property('UnityEngine.UI.Image', 'sprite')
        image_source_drop_zone = self.altdriver.find_element('Drop').get_component_property('UnityEngine.UI.Image',
                                                                                'sprite')
        self.assertNotEqual(image_source, image_source_drop_zone)

    def test_set_player_pref_keys_int(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        self.altdriver.delete_player_prefs()
        self.altdriver.set_player_pref_key('test', 1, PlayerPrefKeyType.Int)
        value = self.altdriver.get_player_pref_key('test', PlayerPrefKeyType.Int)
        self.assertEqual(int(value), 1)

    def test_set_player_pref_keys_float(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        self.altdriver.delete_player_prefs()
        self.altdriver.set_player_pref_key('test', 1.3, PlayerPrefKeyType.Float)
        value = self.altdriver.get_player_pref_key('test', PlayerPrefKeyType.Float)
        self.assertEqual(float(value), 1.3)

    def test_set_player_pref_keys_string(self):
        self.altdriver.load_scene('Scene 1 AltUnityDriverTestScene')
        self.altdriver.delete_player_prefs()
        self.altdriver.set_player_pref_key('test', 'string value', PlayerPrefKeyType.String)
        value = self.altdriver.get_player_pref_key('test', PlayerPrefKeyType.String)
        self.assertEqual(value, 'string value')

    def test_wait_for_non_existing_object(self):
        try:
            alt_element = self.altdriver.wait_for_element("dlkasldkas", '', 1, 0.5)
            self.assertEqual(False, True)
        except WaitTimeOutException as e:
            self.assertEqual(e.args[0], "Element dlkasldkas not found after 1 seconds")

    def test_wait_forobject_to_not_exist_fail(self):
        try:
            alt_element = self.altdriver.wait_for_element_to_not_be_present("Capsule", '', 1, 0.5)
            self.assertEqual(False, True)
        except WaitTimeOutException as e:
            self.assertEqual(e.args[0], 'Element Capsule still found after 1 seconds')

    def test_wait_for_object_with_text_wrong_text(self):
        try:
            alt_element = self.altdriver.wait_for_element_with_text("CapsuleInfo", "aaaaa", '', 1, 0.5)
            self.assertEqual(False, True)
        except WaitTimeOutException as e:
            self.assertEqual(e.args[0],
                             'Element CapsuleInfo should have text `aaaaa` but has `Capsule Info` after 1 seconds')

    def test_wait_for_current_scene_to_be_a_non_existing_scene(self):
        try:
            alt_element = self.altdriver.wait_for_current_scene_to_be("AltUnityDriverTestScenee", 1, 0.5)
            self.assertEqual(False, True)
        except WaitTimeOutException as e:
            self.assertEqual(e.args[0], 'Scene AltUnityDriverTestScenee not loaded after 1 seconds')

    def testGetBool(self):
        alt_element = self.altdriver.find_element('Capsule')
        text = alt_element.get_component_property('Capsule', 'TestBool')
        self.assertEqual('true', text)

    def TestCallStaticMethod(self):
        self.altdriver.call_static_methods("UnityEngine.PlayerPrefs", "SetInt", "Test?1")
        a = int(self.altdriver.call_static_methods("UnityEngine.PlayerPrefs", "GetInt", "Test?2"))
        self.assertEquals(1, a)

    def TestCallMethodWithMultipleDefinitions(self):
        capsule = self.altdriver.find_element("Capsule")
        capsule.call_component_method("Capsule", "Test", "2", "System.Int32")
        capsuleInfo = self.altdriver.find_element("CapsuleInfo")
        self.assertEquals("6", capsuleInfo.get_text())
    # Test end.

if __name__ == '__main__':
    unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))


from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import redirect, render
from django.urls import reverse
from django.utils import timezone
from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView, UpdateView

from dfirtrack.settings import INSTALLED_APPS as installed_apps
from dfirtrack_artifacts.models import Artifact
from dfirtrack_config.models import MainConfigModel
from dfirtrack_main.forms import CaseForm
from dfirtrack_main.logger.default_logger import debug_logger
from dfirtrack_main.models import Case, Casepriority, Casestatus

def query_case(casestatus_list):
    """query cases with a list of specific casestatus"""

    # create empty case queryset
    cases_merged = Case.objects.none()

    # iterate over casestatus objects
    for casestatus in casestatus_list:
        # get cases with specific casestatus
        cases = Case.objects.filter(casestatus=casestatus)

        # add cases from above query to merge queryset
        cases_merged = cases | cases_merged

    # sort cases by id
    cases_sorted = cases_merged.order_by('case_id')

    # return sorted cases with specific casestatus
    return cases_sorted

class CaseList(LoginRequiredMixin, ListView):
    login_url = '/login'
    model = Case
    template_name = 'dfirtrack_main/case/case_list.html'
    context_object_name = 'case_list'

    def get_queryset(self):
        # call logger
        debug_logger(str(self.request.user), ' CASE_LIST_ENTERED')
        # get config
        main_config_model = MainConfigModel.objects.get(main_config_name='MainConfig')

        """ get all cases with casestatus to be considered open """

        # get 'open' casestatus from config
        casestatus_open = main_config_model.casestatus_open.all()
        # guery cases according to subset of casestatus open
        cases = query_case(casestatus_open)

        # return cases according to query
        return cases

class CaseClosed(LoginRequiredMixin, ListView):
    login_url = '/login'
    model = Case
    template_name = 'dfirtrack_main/case/case_closed.html'
    context_object_name = 'case_list'

    def get_queryset(self):
        # call logger
        debug_logger(str(self.request.user), ' CASE_CLOSED_ENTERED')
        # get config
        main_config_model = MainConfigModel.objects.get(main_config_name='MainConfig')

        """ get all cases with casestatus to be considered closed """

        # get all casestatus from database
        casestatus_all = Casestatus.objects.all()
        # get 'open' casestatus from config
        casestatus_open = main_config_model.casestatus_open.all()
        # get diff between all casestatus and open casestatus
        casestatus_closed = casestatus_all.difference(casestatus_open)

        # guery cases according to subset of casestatus closed
        cases = query_case(casestatus_closed)
        # return cases according to query
        return cases

class CaseAll(LoginRequiredMixin, ListView):
    login_url = '/login'
    model = Case
    template_name = 'dfirtrack_main/case/case_all.html'
    context_object_name = 'case_list'

    def get_queryset(self):
        # call logger
        debug_logger(str(self.request.user), ' CASE_ALL_ENTERED')
        return Case.objects.order_by('case_id')

class CaseDetail(LoginRequiredMixin, DetailView):
    login_url = '/login'
    model = Case
    template_name = 'dfirtrack_main/case/case_detail.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        case = self.object

        # set dfirtrack_artifacts for template
        if 'dfirtrack_artifacts' in installed_apps:
            context['dfirtrack_artifacts'] = True
            context['artifact_number'] = Artifact.objects.filter(case=case).count()
        else:
            context['dfirtrack_artifacts'] = False

        # call logger
        case.logger(str(self.request.user), " CASE_DETAIL_ENTERED")
        return context

def set_case_times(case):
    """set case times according to config"""

    # get config
    main_config_model = MainConfigModel.objects.get(main_config_name='MainConfig')

    # get relevant casestatus out of config
    casestatus_start = main_config_model.casestatus_start.all()
    casestatus_end = main_config_model.casestatus_end.all()

    # set start time if new casestatus of system is in casestatus_start of main config (and has not been set before)
    if case.casestatus in casestatus_start and case.case_start_time == None:
        case.case_start_time = timezone.now()

    # set end time if new casestatus of system is in casestatus_end of main config (and has not been set before)
    if case.casestatus in casestatus_end and case.case_end_time == None:
        case.case_end_time = timezone.now()
        # also set start time if it has not already been done
        if case.case_start_time == None:
            case.case_start_time = timezone.now()

    return case

class CaseCreate(LoginRequiredMixin, CreateView):
    login_url = '/login'
    model = Case
    form_class = CaseForm
    template_name = 'dfirtrack_main/case/case_generic_form.html'

    def get(self, request, *args, **kwargs):
        # get id of first status objects sorted by name
        casepriority = Casepriority.objects.order_by('casepriority_name')[
            0
        ].casepriority_id
        casestatus = Casestatus.objects.order_by('casestatus_name')[0].casestatus_id

        form = self.form_class(
            initial={
                'casepriority': casepriority,
                'casestatus': casestatus,
            }
        )
        debug_logger(str(request.user), " CASE_ADD_ENTERED")
        return render(
            request,
            self.template_name,
            {
                'form': form,
                'title': 'Add',
            },
        )

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            case = form.save(commit=False)
            case.case_created_by_user_id = request.user
            case.case_modified_by_user_id = request.user
            # set case times according to config
            case = set_case_times(case)
            case.save()
            form.save_m2m()
            case.logger(str(request.user), " CASE_ADD_EXECUTED")
            messages.success(request, 'Case added')
            return redirect(reverse('case_detail', args=(case.case_id,)))
        else:
            return render(
                request,
                self.template_name,
                {
                    'form': form,
                    'title': 'Add',
                },
            )

class CaseUpdate(LoginRequiredMixin, UpdateView):
    login_url = '/login'
    model = Case
    form_class = CaseForm
    template_name = 'dfirtrack_main/case/case_generic_form.html'

    def get(self, request, *args, **kwargs):
        case = self.get_object()
        form = self.form_class(instance=case)
        case.logger(str(request.user), " CASE_EDIT_ENTERED")
        return render(
            request,
            self.template_name,
            {
                'form': form,
                'title': 'Edit',
            },
        )

    def post(self, request, *args, **kwargs):
        case = self.get_object()
        form = self.form_class(request.POST, instance=case)
        if form.is_valid():
            case = form.save(commit=False)
            case.case_modified_by_user_id = request.user
            # set case times according to config
            case = set_case_times(case)
            case.save()
            form.save_m2m()
            case.logger(str(request.user), " CASE_EDIT_EXECUTED")
            messages.success(request, 'Case edited')
            return redirect(reverse('case_detail', args=(case.case_id,)))
        else:
            return render(
                request,
                self.template_name,
                {
                    'form': form,
                    'title': 'Edit',
                },
            )

class CaseSetUser(LoginRequiredMixin, UpdateView):
    login_url = '/login'
    model = Case

    def get(self, request, *args, **kwargs):
        case = self.get_object()
        case.case_assigned_to_user_id = request.user
        case.save()
        case.logger(str(request.user), " CASE_SET_USER_EXECUTED")
        messages.success(request, 'Case assigned to you')

        # redirect
        return redirect(reverse('case_detail', args=(case.case_id,)))

class CaseUnsetUser(LoginRequiredMixin, UpdateView):
    login_url = '/login'
    model = Case

    def get(self, request, *args, **kwargs):
        case = self.get_object()
        case.case_assigned_to_user_id = None
        case.save()
        case.logger(str(request.user), " CASE_UNSET_USER_EXECUTED")
        messages.warning(request, 'User assignment for case deleted')

        # redirect
        return redirect(reverse('case_detail', args=(case.case_id,)))


from collections import Counter, defaultdict, deque
from functools import reduce, lru_cache
from itertools import permutations
from typing import (
    List,
    Tuple,
    Set,
    Dict,
    Iterable,
    DefaultDict,
    Optional,
    Union,
    Generator,
)
from copy import deepcopy
import re
from heapq import heappop, heappush
from time import sleep

from intcode import IntCode
from termcolor import colored

def parse(filename: str) -> IntCode:
    matrix = []
    with open(filename, "r") as f:
        lines = f.read().strip().replace("\n", "")

    l = [int(x) for x in lines.split(",")]

    d = {i: l[i] for i in range(len(l))}
    return IntCode(d)

def str_to_cmd(string):
    cmd = [ord(i) for i in string]
    return cmd

def print_out(intcode):
    outs = [chr(char) for char in intcode.outs]
    for grid in "".join(outs).split("\n\n"):
        print(grid)
        from time import sleep
        sleep(0.3)
    # NOT B T
    # OR T J
    # NOT C T
    # OR J T
    # NOT D J
    # OR T J
    # WALK

def run_program(intcode):
    # cmd = "NOT A J\nNOT B J\nNOT C J\nNOT D J\nWALK\n"
    instr = """NOT C T
    NOT D J
    NOT J J
    AND T J
    NOT A T
    OR T J
    WALK
    """
    cmd = instr
    intcode.inputs.extend(str_to_cmd(cmd))
    intcode.run()
    try:
        print_out(intcode)
    except ValueError:
        return intcode.outs[-1]

def run_program_p2(intcode):
    instr = """NOT C J
    AND D J
    AND H J
    NOT B T
    AND D T
    OR T J
    NOT A T
    OR T J
    RUN
    """
    cmd = instr
    intcode.inputs.extend(str_to_cmd(cmd))
    intcode.run()
    try:
        print_out(intcode)
    except ValueError:
        return intcode.outs[-1]

def main(filename: str) -> Tuple[Optional[int], Optional[int]]:
    from time import time

    start = time()
    answer_a, answer_b = None, None
    intcode = parse(filename)
    answer_a = run_program(intcode)
    print(f"p1: {answer_a}")
    # p2
    intcode = parse(filename)
    answer_b = run_program_p2(intcode)
    print(f"p2: {answer_b}")
    end = time()
    print(end - start)
    return answer_a, answer_b

if __name__ == "__main__":
    from utils import submit_answer
    from aocd.exceptions import AocdError

    sample = "sample.txt"
    inp = "input.txt"

    # sample_a_answer = 1125899906842624
    # sample_b_answer = 18216

    # answer_a, answer_b = main(sample)
    # assert (
    #     answer_a == sample_a_answer
    # ), f"AnswerA incorrect: Actual: {answer_a}, Expected: {sample_a_answer}"
    # print("sampleA correct")
    # if answer_b:
    #     assert (
    #         answer_b == sample_b_answer
    #     ), f"AnswerB incorrect: Actual: {answer_b}, Expected: {sample_b_answer}"
    #     print("sampleB correct")

    # Test on your input and submit
    answer_a, answer_b = main(inp)
    print(f"Your input answers: \nA: {answer_a}\nB: {answer_b}")
    # try:
    #     submit_answer(answer_a, "a", day=15, year=2019)
    # except AocdError:
    #     submit_answer(answer_b, "b", day=15, year=2019)


"""
Sylvia Liu
122090359
"""

import os
from os import path
import errno
from glob import glob
import cv2
import numpy as np
import poisson

IMG_EXTENSIONS = ["png", "jpg", "jpeg", "JPG", "tif", "tiff", "bmp"]

# Define a function to find image files, and return a tuple.
def collect(img_path, extension=IMG_EXTENSIONS):
    files = sum(map(glob, [img_path + ext for ext in extension]), [])
    return files

# Define a function to normalize the mask.
def normalize(image):
    mask = np.atleast_3d(image).astype(float) / 255.
    mask[mask != 1] = 0
    mask = mask[:, :, 0]
    return mask

# Find the input folder.
subfolders = os.walk("input")
next(subfolders)

for dirpath, dirname, filenames in subfolders:

    # Select the latest input folder.
    input_dir = os.path.split(dirpath)[-1]
    output_dir = os.path.join("output", input_dir)

    # Show which folder is being executed.
    print("input " + input_dir)

    # Find the original images.
    source_path = collect(os.path.join(dirpath, 'source.'))
    target_path = collect(os.path.join(dirpath, 'target.'))
    mask_path = collect(os.path.join(dirpath, 'mask.'))

    # Read images.
    source_img = cv2.imread(source_path[0], cv2.IMREAD_COLOR)
    target_img = cv2.imread(target_path[0], cv2.IMREAD_COLOR)
    mask_img = cv2.imread(mask_path[0], cv2.IMREAD_GRAYSCALE)
    print("Catch the images.")

    # Execute the mask image.
    mask = normalize(mask_img)

    channels = source_img.shape[-1]  # Represents the number of image channels.
    composite = [poisson.img_edit(source_img[:, :, i], target_img[:, :, i], mask) for i in range(channels)]
    result = cv2.merge(composite)  # Merge the channels back.

    # Create the output directory.
    try:
        os.makedirs(output_dir)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

    # Write the result image.
    cv2.imwrite(path.join(output_dir, 'result.png'), result)
    print("Work Done.")


from collections import deque

t = int(input())

for tc in range(t):
    v, e = map(int, input().split())

    vertex = [0] * (v + 1)
    graph = [[] for _ in range(v + 1)]

    for i in range(e):
        x, y = map(int, input().split())
        graph[x].append(y)
        graph[y].append(x)

    queue = deque()
    group = 1
    result = True
    for i in range(1, v+1):
        if vertex[i] == 0:
            queue.append(i)
            vertex[i] = group
            while queue:
                a = queue.popleft()
                for temp in graph[a]:
                    if vertex[temp] == 0:
                        queue.append(temp)
                        vertex[temp] = -vertex[a]
                    elif vertex[temp] == vertex[a]:
                        result = False
                        break
                if not result:
                    break
        if not result:
            break

    if result:
        print("YES")
    else:
        print("NO")


from sentence_transformers import SentenceTransformer, LoggingHandler
import torch
from myDataset import *
from src.utils import KNNOverGenBatchBleuEvaluator

device = 'cuda' if torch.cuda.is_available() else 'cpu'

logging.basicConfig(format='%(asctime)s - %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    level=logging.INFO,
                    handlers=[LoggingHandler()])
logger = logging.getLogger(__name__)

def process_anchors(path, overgen_score_path, greedy_score_path, context_window=3, num_of_anchors=1000):
    f = open(path)
    dataset = json.load(f)
    f.close()

    f = open(overgen_score_path)
    scores = json.load(f)
    f.close()

    greedy_score_path = greedy_score_path
    f = open(greedy_score_path)
    greedy_scores = json.load(f)
    f.close()

    assert len(scores) == len(dataset)
    assert len(dataset[0]["over_gen"]) == len(scores[0])

    assert len(dataset) > num_of_anchors

    lex_delex_utt_map = load_damd_lex_delex_map()

    anchor_samples = []

    for entry, score, greedy_score in zip(dataset[-num_of_anchors:], scores[-num_of_anchors:],
                                        greedy_scores[-num_of_anchors:]):

        gold_delex = entry["resp_text"]
        over_gen_list_delex = entry["over_gen"]
        context_lex = entry["context_text"]

        context_delex = list(map(lambda x: lex_delex_utt_map[x], context_lex))
        sentc = str(" <turn_sep> ".join(context_delex[0 if context_window is None else -context_window:]))
        sentr = gold_delex
        sentn_list = list(map(lambda x: x.replace(" <eos_r>", ""), over_gen_list_delex))

        positive_list = []
        negative_list = []

        for i in range(len(sentn_list)):
            if (score[i] >= greedy_score):
                positive_list.append((sentc + " <turn_sep> " + sentn_list[i], 1))
            else:
                negative_list.append((sentc + " <turn_sep> " + sentn_list[i], 0))
        num_to_added = min(len(positive_list), len(negative_list))

        anchor_samples.extend(positive_list[:num_to_added])
        anchor_samples.extend(negative_list[:num_to_added])

    return anchor_samples

'''
Main function here
'''

def run_experiment():

    context_window = 3

    knn_k = [5000]

    num_of_anchors = [5000]

    anchor_sample_list = []

    for num_anchor in num_of_anchors:
        anchor_samples = process_anchors(path="./data/0.7_train.json",
                                         overgen_score_path="./data/sim_score_mpnet_train_0.7.json",
                                         greedy_score_path="./data/sim_score_mpnet_train_0.7_greedy.json",
                                          context_window=context_window, num_of_anchors=num_anchor)

        assert len(anchor_samples) > 0
        anchor_sample_list.append((num_anchor, anchor_samples))

    init_model_path = "./output/similarity-mintl-ct-3-BatchAllTripletLoss-e5-bs128-seed42-0.7_mpnet/"

    model = SentenceTransformer(init_model_path)

    for anchor_num, anchor_sample in anchor_sample_list:
        evaluator_test = KNNOverGenBatchBleuEvaluator(data_path="./data/0.7_test.json", context_window=context_window,
                                                     anchor_samples=anchor_sample,
                                                     knn_k=knn_k, num_of_anchors=anchor_num)
        evaluator_test.__call__(model)

def main():
    run_experiment()

if __name__ == '__main__':
    main()


import sys
read = sys.stdin.readline

def read_ints():
    return list(map(int, read().split()))

def read_a_int():
    return int(read())

def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]

def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]

def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret

# このグラフ構造の中で一番遠い連結成分では？
H, W = read_ints()

from collections import deque, defaultdict
from itertools import product
Tree = defaultdict(lambda: [])

# input data
S = read_map(H)
node = -1
for i, j in product(range(H), range(W)):
    node += 1
    if S[i][j] == '#':
        continue
    for ni, nj in zip([0, 1, 0, -1], [1, 0, -1, 0]):
        if -1 < i + ni < H and -1 < j + nj < W and S[i + ni][j + nj] == '.':
            Tree[node].append((node + (W * ni + nj), 1))
            Tree[node + (W * ni + nj)].append((node, 1))

def bfs(Tree, n_V, s):
    '''
    指定した点sからの単一始点で各点までの距離を返す。
    n_Vは頂点の数
    Treeはdefault dictで、valueには(隣接ノード,そこまでのコスト)がリスト形式で格納されているとする。
    '''
    INF = -10**9
    Dists = [INF] * n_V  # 距離の初期化 #こいつを更新して返すことにする
    is_visited = [False] * n_V
    is_visited[s] = True
    que = deque([(s, 0)])  # (ノード番号,そこまでたどり着くためのコスト)
    while que:
        cur, cost = que.popleft()
        Dists[cur] = cost
        for nx_node, nx_cost in Tree[cur]:
            if is_visited[nx_node]:
                continue
            que.append((nx_node, nx_cost + cost))
            is_visited[nx_node] = True

    return Dists

# 任意の点から一番遠い点を求めて、そこからさらに一番遠い点がほしい
for i, j in product(range(H), range(W)):
    if S[i][j] == '.':
        Dists = bfs(Tree, W * H, i * W + j)
        break

next_node = Dists.index(max(Dists))  # numpyでいうargmaxしてる
Dists = bfs(Tree, W * H, next_node)

print(max(Dists))


import uuid
from django.db import models
from django.utils import timezone

class Contact(models.Model):
    class Meta:
        db_table = 'contact'

    uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length = 150, null = False)
    email = models.EmailField(null = False, unique=True)
    phone = models.CharField(max_length=64,null=False)
    subject = models.CharField(max_length = 300, null = False)
    detail = models.TextField(null = False)
    status = models.IntegerField(null = False, default=0)

    deleted = models.BooleanField(default=False)
    created_at = models.DateField(auto_now_add=True)
    modified_at = models.DateField(auto_now=True)    # category = models.ForeignKey(Category, verbose_name='カテゴリ', on_delete=models.PROTECT,)

    def __str__(self):
        return self.subject


"""Module for test ignoring."""
import json
from typing import Optional

import constants
from helpers import output_handler

dis_reaction = output_handler.OutputReaction(
    prefix='test',
    module_name=__name__,
)

class BrokenTests(object):
    """Ignored tests representation."""

    def __init__(self, tests: list[dict], search_field: str = 'name'):
        """
        Init class instance.

        Args:
            tests (list[dict]): ignored tests
            search_field (str): field to process `self.get` method
        """
        self.search_field = search_field
        self._tests = tests

    @property
    def tests(self) -> list[dict]:
        """
        Getter for `self._tests`.

        Returns:
            self._tests (list[dict]): list of ignored tests
        """
        return self._tests

    @tests.setter
    def tests(self, tests_value: list[dict]):
        """
        Setter for `self._tests`.

        Args:
            tests_value (list[dict]): tests value to set up

        Raises:
            TypeError: if type error
            KeyError: if key error
        """
        if not isinstance(tests_value, list):
            raise TypeError('`tests` must be in `list` type.')

        for test_v in tests_value:
            if not isinstance(test_v, dict):
                raise TypeError('Each test must be in `dict` type.')

            if self.search_field not in test_v.keys():
                raise KeyError('Each test must have `name` field.')

        self._tests = tests_value

    def update(self, test_id: str):
        """
        Update test list.

        Args:
            test_id (str): test ID
        """
        if test_id not in self.get_tests_ids():

            try:
                self.tests.append(
                    {'name': test_id, 'reason': ''},
                )
            except Exception as exc:
                dis_reaction(
                    msg=['Failed to update tests.', exc],
                    severity=constants.SEV_ERROR,
                )

    def get(self, by_key: str) -> Optional[dict]:
        """
        Check for substring equality with test[self.search_field].

        Args:
            by_key (str): substring

        Returns:
            Optional[dict]: ignored test data if found
        """
        if self.tests:
            for test in self.tests:
                if test.get(self.search_field) == by_key:
                    return test

        return None

    def get_tests_ids(self) -> tuple:
        """
        Get all ignored tests IDs as tuple.

        Returns:
            ids (tuple): ignored tests ids
        """
        if self.tests:
            return tuple(test.get('name') for test in self.tests)

def load_broken_tests(source: str) -> BrokenTests:
    """
    Load ignored tests data.

    Args:
        source (str): source file with tests to load

    Returns:
        ignored_test_data (list[dict]): copy of ignored test data
    """
    try:
        with open(source, 'r') as dtf:
            return BrokenTests(
                json.load(dtf),
            )
    except Exception as exc:
        dis_reaction(
            msg=['Failed to load broken tests.', exc],
            severity=constants.SEV_WARNING,
        )


from typing import Optional, Dict, List
from dijkstra import Dijkstra
from weighted_graph import WeightedGraph
from weighted_edge import WeightedEdge
import sys

WeightedPath = List[WeightedEdge]  # type alias for paths

def default_data():
    city_graph2: WeightedGraph[str] = WeightedGraph(
        ["Seattle", "San Francisco", "Los Angeles", "Riverside", "Phoenix", "Chicago", "Boston", "New York", "Atlanta",
         "Miami", "Dallas", "Houston", "Detroit", "Philadelphia", "Washington"])
    city_graph2.add_edge_by_vertices("Seattle", "Chicago", 1737)
    city_graph2.add_edge_by_vertices("Seattle", "San Francisco", 678)
    city_graph2.add_edge_by_vertices("San Francisco", "Riverside", 386)
    city_graph2.add_edge_by_vertices("San Francisco", "Los Angeles", 348)
    city_graph2.add_edge_by_vertices("Los Angeles", "Riverside", 50)
    city_graph2.add_edge_by_vertices("Los Angeles", "Phoenix", 357)
    city_graph2.add_edge_by_vertices("Riverside", "Phoenix", 307)
    city_graph2.add_edge_by_vertices("Riverside", "Chicago", 1704)
    city_graph2.add_edge_by_vertices("Phoenix", "Dallas", 887)
    city_graph2.add_edge_by_vertices("Phoenix", "Houston", 1015)
    city_graph2.add_edge_by_vertices("Dallas", "Chicago", 805)
    city_graph2.add_edge_by_vertices("Dallas", "Atlanta", 721)
    city_graph2.add_edge_by_vertices("Dallas", "Houston", 225)
    city_graph2.add_edge_by_vertices("Houston", "Atlanta", 702)
    city_graph2.add_edge_by_vertices("Houston", "Miami", 968)
    city_graph2.add_edge_by_vertices("Atlanta", "Chicago", 588)
    city_graph2.add_edge_by_vertices("Atlanta", "Washington", 543)
    city_graph2.add_edge_by_vertices("Atlanta", "Miami", 604)
    city_graph2.add_edge_by_vertices("Miami", "Washington", 923)
    city_graph2.add_edge_by_vertices("Chicago", "Detroit", 238)
    city_graph2.add_edge_by_vertices("Detroit", "Boston", 613)
    city_graph2.add_edge_by_vertices("Detroit", "Washington", 396)
    city_graph2.add_edge_by_vertices("Detroit", "New York", 482)
    city_graph2.add_edge_by_vertices("Boston", "New York", 190)
    city_graph2.add_edge_by_vertices("New York", "Philadelphia", 81)
    city_graph2.add_edge_by_vertices("Philadelphia", "Washington", 123)

    _Path = Dijkstra()
    distances, path_dict = _Path.execute_alg(city_graph2, "Los Angeles")
    name_distance: Dict[str, Optional[int]] = _Path.distance_array_to_vertex_dict(city_graph2, distances)
    print("Расстояние от Los Angeles:")
    for key, value in name_distance.items():
        print(f"{key} : {value}")
    print("")  # blank line

    print("Кратчайшее расстояние от Los Angeles до Boston:")
    path: WeightedPath = _Path.path_dict_to_path(city_graph2.index_of("Los Angeles"), city_graph2.index_of("Boston"),
                                                 path_dict)
    _Path.print_weighted_path(city_graph2, path)

def create_graph() -> WeightedGraph:
    result: list = []
    input_city: str = ''
    while input_city != 'exit':
        print("Введите название вершины: ")
        input_city = input()
        if input_city == 'exit':
            break
        result.append(input_city)
    city_graph: WeightedGraph[str] = WeightedGraph(result)
    return city_graph

def create_edge(graph: WeightedGraph) -> None:
    a: str
    b: str
    weight: float
    print("Введите название вершины 1: ")
    a = input()
    print("Введите название вершины 2: ")
    b = input()
    print("Введите вес ребра: ")
    weight = float(input())
    graph.add_edge_by_vertices(a, b, weight)

def start_dijkstra():
    graph: WeightedGraph = create_graph()
    input_edge: str = ''
    while input_edge != 'exit':
        print("Для выхода напишите exit")
        input_edge = input()
        if input_edge == 'exit': break
        create_edge(graph)
    _Path = Dijkstra()
    print("Введите вершину из которой строить маршруты: ")
    start_point: str = input()
    distances, path_dict = _Path.execute_alg(graph, start_point)
    name_distance: Dict[str, Optional[int]] = _Path.distance_array_to_vertex_dict(graph, distances)
    print(f"Расстояние от {start_point}:")
    for key, value in name_distance.items():
        print(f"{key} : {value}")
    print("")  # blank line
    print("Введите вершину куда строить маршрут: ")
    end_point: str = input()
    print(f"Кратчайшее расстояние от {start_point} до {end_point}:")
    path: WeightedPath = _Path.path_dict_to_path(graph.index_of(start_point), graph.index_of(end_point),
                                                 path_dict)
    _Path.print_weighted_path(graph, path)

if __name__ == "__main__":
    command_line: str = ""
    while command_line != 'exit':
        print("Введите комманду (default): \n 1. create для создания своего графа \n "
              "2. default для использования данных по умолчанию"
              "\n 3. exit для выхода"
              "\n $> ")
        command_line = input()
        if command_line == "default":
            default_data()
        elif command_line == "create":
            start_dijkstra()
        elif command_line == "exit":
            try:
                sys.exit()
            except:
                print("Работы программы завершена")


import tensorflow as tf
import numpy as np
#import cv2
import matplotlib.pyplot as plt

from tensorflow.keras import Model, Sequential, layers, utils
from tensorflow.keras.layers import Conv2D, Flatten, Dense, Activation,MaxPooling2D, Dropout, BatchNormalization
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from collections import Counter
import time
import os

class D2CNN(Model):

    def __init__(self, *args, **kwargs):
        super(D2CNN,self).__init__()
        self.conv1 = Sequential([
        Conv2D(10,(11,11),strides=4,padding='same',name = 'conv1_1'),
        Activation("relu",name = 'act1'),
        BatchNormalization(name = 'batch_norm_1'),
        MaxPooling2D(pool_size=(3,3),strides = 2, name = 'pool_1')
        ])
        self.conv2 = Sequential([
        Conv2D(20,(5,5),strides=1,padding='same',name = 'conv2_1'),
        Activation("relu",name = 'act2'),
        BatchNormalization(name = 'batch_norm_2'),
        MaxPooling2D(pool_size=(2,2),strides = 2, name = 'pool_2')
        ])
        self.conv3 = Sequential([
        Conv2D(40,(5,5),strides=1,padding='same',name = 'conv3_1'),
        Activation("relu",name = 'act3'),
        BatchNormalization(name = 'batch_norm_3'),
        MaxPooling2D(pool_size=(2,2),strides = 2, name = 'pool_3')
        ])
#         self.conv4 = Sequential([
#         Conv2D(256,(5,5),strides=1,padding='same',name = 'conv4_1'),
#         Activation("relu",name = 'act4'),
#         BatchNormalization(name = 'batch_norm_4'),
#         MaxPooling2D(pool_size=(2,2),strides = 2, name = 'pool_4')
#         ])

        self.flatten = Flatten()

        self.classifier =  Sequential([
        Dense(1024,activation = 'relu'),
        layers.Dropout(0.5),
        Dense(1024,activation = 'relu'),
        layers.Dropout(0.5),
        Dense(10,activation='softmax')
        ])

    def call(self,x):
        #x = tf.expand_dims(x,0)
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)
        #x = self.conv4(x)
        x = self.flatten(x)
        x = self.classifier(x)
        return x

#     def make(self, input_shape=(227,227,1)):
#
#         x = tf.keras.layers.Input(shape=input_shape)
#         model = tf.keras.Model(inputs=[x], outputs=self.call(x), name='actor')
#         print(model.summary())
#         return model

def data_work():

    train_data = np.load('data_train.npy') #add the training npy file path
    train_data = train_data.T
    t_label = np.load('t_train_corrected.npy') # add the training label npy file path
    bad_labels = np.where(t_label == -1)
    t_correct = np.delete(t_label,bad_labels[0])
    t_correct = t_correct.astype(np.int32)
    num_class = len(set(t_correct))
    t_final = utils.to_categorical(t_correct,num_classes=num_class)
    train_data_correct = np.delete(train_data,bad_labels[0],axis = 0)
    num_samp,dim = np.shape(train_data_correct)
    dim_2 = int(np.sqrt(dim))
    train_data_correct = train_data_correct.reshape(num_samp,dim_2,dim_2)
    data_final = train_data_correct.reshape(train_data_correct.shape+(1,))
    data_final = tf.image.resize_with_pad(data_final,227,227)

    return data_final,t_final

def self_plot(train,test,l1,l2,sav):
    fig = plt.figure(figsize=(18,9))
    ax1 = fig.add_subplot(1,1,1)
    ax1.plot(train,label = l1)
    ax1.plot(test,label = l2)
    ax1.legend()
    plt.savefig(sav+'.png')

def run():

    ti = str(time.ctime()).split()
    fol = 'model_4_' + str.join('_',ti)
    os.mkdir(fol)
    fil = 'model_4_' + str.join('_',ti)
    pth = os.path.join(fol,fil)

    dat,tar = data_work()

    datagen = ImageDataGenerator(rotation_range = 45,
                            width_shift_range = 0.2,
                            height_shift_range = 0.2,
                            shear_range = 0.2,
                            zoom_range = 0.2,
                            horizontal_flip = True,
                            fill_mode = 'nearest',
                            validation_split = 0.2,
                            featurewise_center= True,
                            featurewise_std_normalization= True)

    datagen.fit(dat)
    np.save(fol+'/data_mean',datagen.mean)
    np.save(fol+'/data_std',datagen.std)

    #gg_mean, gg_std = datagen.mean,datagen.std

    train_generator = datagen.flow(dat,tar,batch_size=32,
                                    subset='training')

    val_generator = datagen.flow(dat,tar,batch_size=8,subset='validation')

    '''call_back = tf.keras.callbacks.EarlyStopping(monitor='val_loss',
                                                 min_delta=0,
                                                 patience=20,
                                                 verbose=0,
                                                 mode='auto',
                                                 baseline=None,
                                                 restore_best_weights=True)'''

    call_back_2 = tf.keras.callbacks.ModelCheckpoint(filepath=pth,
                                                    monitor='val_accuracy',
                                                    mode='max',
                                                    save_weights_only= True)

    n_epoch = 250
    model = D2CNN()

    model.compile(optimizer = 'adam',
                loss = tf.keras.losses.CategoricalCrossentropy(),
                metrics = ['accuracy'])

    history = model.fit_generator(train_generator,
                        steps_per_epoch= (0.8*len(dat))//32,
                        validation_data = val_generator,
                        validation_steps = 8,
                        epochs = n_epoch,
                        callbacks = [call_back_2])

    self_plot(history.history['loss'],history.history['val_loss'],'training loss','testing_loss',fol+'/loss')
    self_plot(history.history['accuracy'],history.history['val_accuracy'],'training accuracy','testing_accuracy',fol+'/accuracy')

    model.save_weights(pth)
    model.save(f'model_4.md')

if __name__ == '__main__':
    run()