from explore_primary import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns;

sns.set()  # for plot styling

# read the csv file
sdata = pd.read_csv('secondary_data.csv', sep=';')

# view part of the file to see what the data looks like
head = sdata.head()
shape = sdata.shape

# look for more details in the file
describe = sdata.describe()
isnull = sdata.isnull().sum()

# put the index of names of not null columns(that dont have null values) in array to use them
index_notnull_col = []
for i in range(21):
    if isnull[i] == 0:
        index_notnull_col.append(i)

# take the not null columns from the data
columns = sdata.columns
columns_not_null = columns[index_notnull_col]
name_notnull_col = [c for c in columns_not_null]  # put the names of not null columns in array
not_null_data = sdata.loc[:, name_notnull_col]  # new df without null values

target = not_null_data['class']

num_target = np.array([1 if x == 'p' else 0 for x in target])  # replace the values with numbers: p = 1 , e = 0

def print_more_info():
    print("head")
    print(head)
    print("shape")
    print(shape)
    print("descripe:")
    print(describe)
    print("info")
    sdata.info()
    print("is_null")
    print(isnull)
    print("index of not null col")
    print(index_notnull_col)
    print("name of not null col:")
    print(name_notnull_col)

# print_more_info()

# taking specific columns from the data and show the relation between them and the target by drawing
cap_diameter = not_null_data['stem-height']
stem_hieght = not_null_data['stem-height']
stem_width = not_null_data['stem-width']

cap_shape = not_null_data['cap-shape']
cap_color = not_null_data['cap-color']
does_bruise_or_bleed = not_null_data['does-bruise-or-bleed']

gill_color = not_null_data['gill-color']
stem_color = not_null_data['stem-color']

has_ring = not_null_data['has-ring']
habitat = not_null_data['habitat']
season = not_null_data['season']

# convert the columns to np array
cap_diameter_arr = np.array([x for x in cap_diameter])
stem_height_arr = np.array([x for x in stem_hieght])
stem_width_arr = np.array([x for x in stem_width])

cap_shape_arr = np.array([x for x in cap_shape])
cap_color_arr = np.array([x for x in cap_color])
does_bruise_or_bleed_arr = np.array([x for x in does_bruise_or_bleed])
gill_color_arr = np.array([x for x in gill_color])
stem_color_arr = np.array([x for x in stem_color])
has_ring_arr = np.array([x for x in has_ring])
habitat_arr = np.array([x for x in habitat])
season_arr = np.array([x for x in season])

index = np.arange(0, len(target), 1, dtype=int)

def test_part(target, arr1, arr2, n1=0, n2=len(target)):
    """
    :param target: the target of the data set (class column: p/e)
    :param arr1: x
    :param arr2: y
    :param n1: first index or default = 0
    :param n2: last index or default = len = 61069
    :return: drawing slice of arr1 and arr2 with coloring the poison vals in red and the edible vals in blue
    """
    part_target = target[n1:n2]
    part_arr1 = arr1[n1:n2]
    part_arr2 = arr2[n1:n2]

    plt.plot(part_arr1[part_target == 'p'], part_arr2[part_target == 'p'], 'ro',
             part_arr1[part_target == 'e'], part_arr2[part_target == 'e'], 'bx')

def plot_test_part():
    """
    :return: plot all the columns using test part function
    """
    plt.subplot(1, 3, 1)
    plt.title("cap_diameter")
    test_part(target, index, cap_diameter_arr)
    plt.subplot(1, 3, 2)
    plt.title("stem_height")
    test_part(target, index, stem_height_arr)
    plt.subplot(1, 3, 3)
    plt.title("stem_width")
    test_part(target, index, stem_width_arr)
    plt.show()

    plt.subplot(2, 2, 1)
    plt.title("cap_shape")
    test_part(target, index, cap_shape_arr)
    plt.subplot(2, 2, 2)
    plt.title("cap_color")
    test_part(target, index, cap_color_arr)
    plt.subplot(2, 2, 3)
    plt.title("gill_color")
    test_part(target, index, gill_color_arr)
    plt.subplot(2, 2, 4)
    plt.title("stem_color")
    test_part(target, index, stem_color_arr)
    plt.show()

    plt.subplot(2, 1, 1)
    plt.title("bruise_or_bleed")
    test_part(target, index, does_bruise_or_bleed_arr)
    plt.subplot(2, 1, 2)
    plt.title("has_ring")
    test_part(target, index, has_ring_arr)
    plt.show()

    plt.subplot(2, 1, 1)
    plt.title("habitat")
    test_part(target, index, habitat_arr)
    plt.subplot(2, 1, 2)
    plt.title("season")
    test_part(target, index, season_arr)
    plt.show()

# plot_test_part()

"""

poisonous 1:
has ring = t
gill color = white
cap color = red = e
stem color = red = e

"""

"""

poisonous 2:
has ring = t
gill color = white
cap color = red / orange = e / o
stem color = red / white = e / w

"""

has_ring_t = has_ring_arr == 't'
gill_color_white = gill_color_arr == 'w'
cap_color_red = cap_color_arr == 'e'
stem_color_red = stem_color_arr == 'e'

cap_color_orange = cap_color_arr == 'o'
stem_color_white = stem_color_arr == 'w'

cap_stem_red = np.any([cap_color_red, stem_color_red], axis=0)
poisonous_any = np.any([has_ring_t, gill_color_white, cap_color_red, stem_color_red], axis=0)
poisonous_all = np.all([has_ring_t, gill_color_white, cap_color_red, stem_color_red], axis=0)

poisonous_any2 = np.any(
    [has_ring_t, gill_color_white, cap_color_red, stem_color_red, cap_color_orange, stem_color_white], axis=0)

def plot_poison_arr(arr):
    plt.plot(target[arr == False], 'g.',
             target[arr], 'r*')
    # plt.show()

def plot_poisonous():
    plot_poison_arr(stem_color_red)
    plt.title("stem_color_red")
    plt.show()
    plot_poison_arr(cap_color_red)
    plt.title("cap_color_red")
    plt.show()
    plot_poison_arr(gill_color_white)
    plt.title("gill_color_white")
    plt.show()
    plot_poison_arr(has_ring_t)
    plt.title("has_ring_t")
    plt.show()
    plot_poison_arr(poisonous_any)
    plt.title("poisonous_any")
    plt.show()
    plot_poison_arr(poisonous_all)
    plt.title("poisonous_all")
    plt.show()
    plot_poison_arr(poisonous_any2)
    plt.title("poisonous_any2")
    plt.show()
    plot_poison_arr(cap_color_orange)
    plt.title("cap color orange")
    plt.show()

# plot_poisonous()

"""
#if we separate the data by families
#each family (name/kind) is 353 rows
By searching on the Internet and the information we have in the csv files,
we will see certain features of certain types of mushrooms to know if they
are poisonous or edible.
"""

# this func is to find the index of specific kind of mashroom
# we use primary data to find the index
def get_index_sec(str):
    i = get_index_pri(str)
    return (i) * 353

# Fly Agaric (p)
"""
Fly agaric has a bright red cap with white spots and white gills.
It can grow to 20cm across and 30cm tall and has a savoury smell.
"""

# Death Cap (p)
"""
What does deathcap look like?
A large fungus growing up to 15cm across and 15cm tall
with a domed or white cap – depending on age – on an off-white stem.
Although it looks fairly inoffensive and similar to a number of edible mushrooms,
it is deadly poisonous.
"""

# False Deathcap

def plot_test_part_examples():
    ind = get_index_sec("Fly Agaric")
    plt.subplot(2, 2, 1)
    plt.title("Fly Agaric cap_color")
    test_part(target, index, cap_color_arr, ind, ind + 353)
    plt.subplot(2, 2, 2)
    plt.title("Fly Agaric stem_color")
    test_part(target, index, stem_color_arr, ind, ind + 353)
    plt.subplot(2, 2, 3)
    plt.title("Fly Agaric bruise_or_bleed")
    test_part(target, index, does_bruise_or_bleed_arr, ind, ind + 353)
    plt.subplot(2, 2, 4)
    plt.title("Fly Agaric has_ring")
    test_part(target, index, has_ring_arr, ind, ind + 353)
    plt.show()

    ind = get_index_sec("Death Cap")
    plt.subplot(2, 2, 1)
    plt.title("Death Cap stem_color")
    test_part(target, index, stem_color_arr, ind, ind + 353)
    plt.subplot(2, 2, 2)
    plt.title("Death Cap season")
    test_part(target, index, season_arr, ind, ind + 353)

    ind = get_index_sec("False Death Cap")
    plt.subplot(2, 2, 3)
    plt.title("False Death Cap stem_color")
    test_part(target, index, stem_color_arr, ind, ind + 353)
    plt.subplot(2, 2, 4)
    plt.title("False Death Cap season")
    test_part(target, index, season_arr, ind, ind + 353)
    plt.show()

# plot_test_part_examples()


from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from api.models.chat_message import ChatMessage
from api.serializers.chat_message import ChatMessageSerializer

class ChatMessageView(APIView):
    """
    API View to retrieve, create, update, or delete a chat message.
    """
    def get_object(self, pk):
        """
        Get a chat message object by its primary key.

        Parameters:
        - pk (int): The primary key of the chat message.

        Returns:
        - ChatMessage: The chat message object.

        Raises:
        - Http404: If the chat message with the given primary key does not exist.
        """
        try:
            return ChatMessage.objects.get(pk=pk)
        except ChatMessage.DoesNotExist:
            raise Http404

    def get(self, request, pk):
        """
        Retrieve a chat message.

        Parameters:
        - pk (int): The primary key of the chat message.

        Returns:
        - Response: Response containing the serialized data of the chat message.

        Raises:
        - status.HTTP_404_NOT_FOUND: If the chat message with the given primary key does not exist.
        """
        chat_message = self.get_object(pk)
        serializer = ChatMessageSerializer(chat_message)
        return Response(serializer.data)

    def post(self, request):
        """
        Create a new chat message.

        Parameters:
        - request.data:
            - sender_id (int): The ID of the sender.
            - text (str): The text of the chat message.
            - chat_id (int): The ID of the chat.
            - time (str): The timestamp of the chat message.
            - diagnosis_id (optional, str): The ID of the associated diagnosis (if applicable).
            - appointment_id (optional, str): The ID of the associated appointment (if applicable).

        Returns:
        - Response: Response containing the serialized data of the created chat message.

        Raises:
        - status.HTTP_500_INTERNAL_SERVER_ERROR: If an error occurs while creating the chat message.
        """
        try:
            data = {}
            data['sender_id'] = int(request.data.get('sender_id'))
            data['text'] = request.data.get('text')
            data['chat_id'] = int(request.data.get('chat_id'))
            data['time'] = request.data.get('time')
            if request.data.get('diagnosis_id'):
                data['diagnosis_id'] = request.data.get('diagnosis_id')
            if request.data.get('appointment_id'):
                data['appointment_id'] = request.data.get('appointment_id')
            chat_message = ChatMessage.objects.create(**data)
            chat_message.save()
            return Response(ChatMessageSerializer(chat_message).data, status=status.HTTP_201_CREATED)
        except Exception as e:
            print(e)
            return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)

    def put(self, request, pk):
        """
        Update a chat message.

        Parameters:
        - request.data: The updated data of the chat message.

        Returns:
        - Response: Response containing the serialized data of the updated chat message.

        Raises:
        - status.HTTP_400_BAD_REQUEST: If the request data is invalid.
        """
        chat_message = self.get_object(pk)
        serializer = ChatMessageSerializer(chat_message, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk):
        """
        Delete a chat message.

        Parameters:
        - pk (int): The primary key of the chat message.

        Returns:
        - Response: Empty response with status HTTP_204_NO_CONTENT.

        Raises:
        - status.HTTP_404_NOT_FOUND: If the chat message with the given primary key does not exist.
        """
        chat_message = self.get_object(pk)
        chat_message.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


import flight
import srv

def exit_(list):
  exit()

def add(list):
  code = input("Code: ")
  duration = input("Duration: ")
  departure = input("Departure city: ")
  destination = input("Destination city: ")

  fl = flight.new(code, duration, departure, destination)

  try:
    srv.add(list, fl)
  except Exception as ex:
    print(str(ex))

def delete(list):
  code = input("Code to delete: ")

  try:
    srv.delete(list, code)
  except Exception as ex:
    print(str(ex))

def show_all_departure(list):
  departure = input("Departure city to show: ")
  print(srv.show_all_departure(list, departure))

def increase_time(list):
  departure = input("Departure city: ")
  amount = input("Amount of time: ")

  try:
    srv.increase_time(list, departure, amount)
  except Exception as ex:
    print(str(ex))


"""Utility functions throughout the project for: GPU mangement, checkpointing, TB, plots, etc."""

import os
import numpy as np
import jax
import jax.numpy as jnp
from flax.core import freeze, unfreeze
from flax import traverse_util
from flax.training import checkpoints
from trax.jaxboard import SummaryWriter
from PIL import Image
import matplotlib.pyplot as plt
from collections import defaultdict
from hparams import HParams

hparams = HParams.get_hparams_by_name("efficient_vdvae")

def assert_CUDA_and_hparams_gpus_are_equal():
    try:
        assert hparams.run.num_gpus == jax.device_count('gpu')
    except RuntimeError:
        assert hparams.run.num_gpus == 0

def create_checkpoint_dir(working_dir='.'):
    return os.path.join(working_dir, f'checkpoints-{hparams.run.name}')

def get_logdir(working_dir='.'):
    return os.path.join(working_dir, f'logs-{hparams.run.name}')

def create_tb_writer(mode):
    logdir = get_logdir()
    tbdir = os.path.join(logdir, mode)
    os.makedirs(logdir, exist_ok=True)
    os.makedirs(tbdir, exist_ok=True)
    writer = SummaryWriter(log_dir=tbdir)

    return writer, logdir

def get_effective_n_pixels():
    if hparams.data.dataset_source == 'binarized_mnist':
        return 28 * 28 * hparams.data.channels
    else:
        return hparams.data.target_res * hparams.data.target_res * hparams.data.channels

def compute_latent_dimension():
    assert np.prod(hparams.model.down_strides) == np.prod(hparams.model.up_strides)

    return hparams.data.target_res // np.prod(hparams.model.down_strides)

def plot_image(writer, outputs, targets, step, denormalizer):
    step = step
    if hparams.data.dataset_source == 'binarized_mnist':
        assert targets.shape[-1] == outputs.shape[-1] == 1
        targets = (targets[2:-2, 2:-2, :] * 255).astype(jnp.uint8)  # Target is still not cropped
        outputs = (outputs * 255).astype(jnp.uint8)
    else:
        assert targets.shape[-1] == outputs.shape[-1] == 3
        targets = denormalizer(targets)
        outputs = denormalizer(outputs)

    writer.image(tag=f"{step}/Original_{step}", image=targets, step=step)
    writer.image(tag=f"{step}/Generated_{step}", image=outputs, step=step)
    writer.flush()

def tensorboard_log(writer, global_step, losses, outputs, targets, means=None, log_scales=None, lr_schedule=None, updates=None,
                    global_norm=None):
    mode = 'train' if updates is not None else 'val'
    global_step = global_step

    for key, value in losses.items():
        writer.scalar(tag=f"Losses/{key}", value=value, step=global_step)

    writer.histogram(tag="Distributions/target", values=targets, bins=20, step=global_step)
    writer.histogram(tag="Distributions/output", values=jnp.clip(outputs, a_min=-1., a_max=1.), bins=20, step=global_step)

    if means is not None:
        assert log_scales is not None

        writer.histogram(tag='OutputLayer/means', values=means, bins=30, step=global_step)
        writer.histogram(tag='OutputLayer/log_scales', values=log_scales, bins=30, step=global_step)

    if mode == 'train':
        # Get the learning rate from the optimizer
        writer.scalar(tag="Schedules/learning_rate", value=lr_schedule(global_step), step=global_step)

        assert global_norm is not None
        writer.scalar(tag="Mean_Max_Updates/Global_norm", value=global_norm, step=global_step)

        if updates != {}:
            for layer, update in updates.items():
                writer.scalar(tag="Updates/{}".format(layer), value=update, step=global_step)

            max_updates = jnp.max(jnp.array(list(updates.values()), dtype=jnp.float32))
            writer.scalar(tag="Mean_Max_Updates/Max_updates", value=max_updates, step=global_step)

    writer.flush()

def save_checkpoint(checkpoint_dir, state):
    state = jax.device_get(jax.tree_map(lambda x: x[0], state))
    step = int(state.step)
    checkpoints.save_checkpoint(checkpoint_dir, state, step=step, keep=hparams.run.max_allowed_checkpoints)

def load_checkpoint_if_exists(checkpoint_dir, state, replace_params_with_emaparams):
    if checkpoints.latest_checkpoint(checkpoint_dir) is not None:
        print(f"\tLoading checkpoint state: {checkpoints.latest_checkpoint(checkpoint_dir)}")
        state = checkpoints.restore_checkpoint(checkpoint_dir, state)

        if replace_params_with_emaparams:
            # Affect ema params to trainable parameters
            state = state.replace(params=state.ema_params)

        return state

    print(f"\tCouldn't find state checkpoints to load at: {checkpoint_dir}")
    return state

def get_l2_mask_from_params(params):
    flat_params = {'/'.join(k): v for k, v in traverse_util.flatten_dict(unfreeze(params)).items()}
    mask = {k: not ('bias' in k or 'input_conv' in k or 'output_conv' in k or 'ln' in k or 'trainable_h' in k
                    or 'embedding_table' in k or 'projection' in k or 'trainable_prior' in k) for k in flat_params.keys()}

    mask = traverse_util.unflatten_dict({tuple(k.split('/')): v for k, v in mask.items()})
    return freeze(mask)

def get_variate_masks(stats):
    thresh = np.quantile(stats, 1. - hparams.synthesis.variate_masks_quantile)
    return stats > thresh

def _denormalize(image, denormalizer):
    if hparams.data.dataset_source == 'binarized_mnist':
        assert image.shape[-1] == 1
        image = (image * 255).astype(jnp.uint8)
        image = jnp.tile(image, [1, 1, 3])
    else:
        assert image.shape[-1] == 3
        image = denormalizer(image)
    return image

def write_image_to_disk(filepath, image, denormalizer):
    assert len(image.shape) == 3
    image = _denormalize(image, denormalizer)

    im = Image.fromarray(np.array(image).astype(np.uint8))
    im.save(filepath, format='png')

def write_grid_to_disk(filepath, metric_grid, image_grid, denormalizer):
    assert len(metric_grid.shape) == 1
    assert len(image_grid.shape) == 4
    image_grid = _denormalize(image_grid, denormalizer)

    fig = plt.figure(figsize=(12, 8))
    n_cols = 3
    n_rows = int(np.ceil(len(image_grid) / n_cols))
    for i, (metric, image) in enumerate(zip(metric_grid, image_grid)):
        fig.add_subplot(n_rows, n_cols, i + 1)
        plt.imshow(image)
        plt.axis('off')
        plt.title(f'gen') if i == 0 else plt.title(f'{metric:.4f}↑')
    plt.tight_layout()
    plt.savefig(filepath, format='png', dpi=300)
    plt.close()

def transpose_dicts(dct):
    d = defaultdict(dict)
    for key1, inner in dct.items():
        for key2, value in inner.items():
            d[key2][key1] = value
    return d


from test.factories.registry import Apps
from test.tools import del_key_by_value, normalize_data

from behave import given
from core.utils import get_model_app
from hamcrest import assert_that, not_none

@given(u'the "{var}" update with')
def the_var_update_with(context, var):
    model = get_model_app(var)

    for row in context.table:
        row = normalize_data(del_key_by_value(row.as_dict()))
        args_search = dict()
        args_search = {'id': row.get('id')}
        args_search['defaults'] = row
        obj, created = model.objects.update_or_create(**args_search)
        assert_that(obj, not_none)

@given(u'the "{var}" with')
def the_var_with(context, var):
    factory_class = Apps.get_factory(var)

    for row in context.table:
        factory_object = None
        row = normalize_data(del_key_by_value(row.as_dict()))
        factory_object = factory_class.create(**row)
        assert_that(factory_object, not_none)


print ("My name is %s and my age is %d" %("Thivi", 25))

a = "  This is my email id xyz@gmail.com  "
list = a.split(" ")
print(list)

print (a.strip())

#Recurssion function for factorial
def fact(num):
    if num>1:
        num = num * fact(num-1)
    return num
print (fact(4))


from sympy import sympify, solve  # py -m pip install sympy

class EquationSolver():
    ops = ['*', '/', '-', '+']

    def __rsolve(eq1: str,  eq2: str, op) -> int:
        eq1 = eq1.strip()
        if eq1 == '':
            v1 = 0
        else:
            req1 = EquationSolver.solve_equation(eq1)
            v1 = int(req1)
        v2 = int(EquationSolver.solve_equation(eq2))
        print(f'Got {v1} {op} {v2}')
        if op == '*':
            return v1 * v2
        if op == '/':
            return v1 / v2
        if op == '-':
            return v1 - v2
        if op == '+':
            return v1 + v2

    def solve_equation(eq: str) -> str:
        sym_eq = sympify(f'Eq(x,{eq})')
        res = solve(sym_eq)
        return str(res[0])

        # eq = eq.strip()
        # for op in EquationSolver.ops:
        #     if op in eq:
        #         l, r = eq.split(op, 1)
        #         return str(EquationSolver.__rsolve(l,r,op))
        # return eq


from gevent import wsgi
import urlparse

import os
import sys
from beach.beach_api import Beach

if 2 != len( sys.argv ):
    print( "Usage: http_endpoint.py beachConfigFile" )
    sys.exit(-1)

def handle_beacon( environment, start_response ):
    global vHandle
    isSuccess = False
    response = ''
    status = '400 Bad Request'

    try:
        params = [ x for x in environment[ 'wsgi.input' ].read() ]

        params = urlparse.parse_qs( ''.join( params ).lstrip( '&' ), strict_parsing = True )
    except:
        params = {}

    if 'pl' in params:
        clean_params = { 'payload' : params[ 'pl' ][ 0 ],
                         'public_ip' : str( environment.get( 'X-FORWARDED-FOR',
                                                        environment.get( 'REMOTE_ADDR', '' ) ) ) }

        resp = vHandle.request( 'beacon', data = clean_params )

        if resp.isSuccess:
            isSuccess = True
            response = '
%s
' % resp.data[ 'resp' ]

    if isSuccess:
        status = '200 OK'

    start_response( status, [ ( 'Content-Type', 'text/html' ),
                              ( 'Content-Length', str( len( response ) ) ) ] )

    return [ str( response ) ]

beach = Beach( sys.argv[ 1 ], realm = 'hcp' )
vHandle = beach.getActorHandle( 'c2/beacon', nRetries = 3, timeout = 30, ident = 'http/5bc10821-2d3f-413a-81ee-30759b9f863b' )

server = wsgi.WSGIServer( ( '', 80 ), handle_beacon, spawn = 100 )

server.serve_forever()


# import the necessary packages
from pyimagesearch.motion_detection.singlemotiondetector import SingleMotionDetector
from imutils.video import VideoStream
from flask import Response
from flask import Flask
from flask import render_template
import threading
import argparse
import datetime
import imutils
import time
import cv2

outputFrame = None
lock = threading.Lock()
app = Flask(__name__)
vs = VideoStream(src=0).start()
time.sleep(2.0)

@app.route("/")
def index():
    return render_template("index.html")

def detect_motion(frameCount, option):
    global vs, outputFrame, lock
    md = SingleMotionDetector(accumWeight=0.1)
    total = 0

    while True:
        frame = vs.read()
        frame = imutils.resize(frame, width=400)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (7, 7), 0)

        cv2.putText(img=frame, text="jaroslaw kudzia", org=(100, 250), fontFace=cv2.FONT_ITALIC,
                    fontScale=1, color=(245, 108, 66), thickness=2)
        if(option == 'motion'):
            if total > frameCount:
                motion = md.detect(gray)
                if motion is not None:
                    (thresh, (minX, minY, maxX, maxY)) = motion
                    cv2.rectangle(frame, (minX, minY), (maxX, maxY),
                                  (0, 0, 255), 2)
        elif(option == 'szarosc'):
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        elif(option == 'blur'):
            frame = cv2.blur(frame, (10,30))
        elif (option == 'edge'):
            frame = cv2.Canny(frame, 50, 100)

        md.update(gray)
        total += 1
        with lock:
            outputFrame = frame.copy()

def generate():
    global outputFrame, lock
    while True:
        with lock:
            if outputFrame is None:
                continue
            (flag, encodedImage) = cv2.imencode(".jpg", outputFrame)
            if not flag:
                continue
        yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
               bytearray(encodedImage) + b'\r\n')

@app.route("/video_feed")
def video_feed():
    return Response(generate(),
                    mimetype="multipart/x-mixed-replace; boundary=frame")

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--ip", type=str, required=True,
                help="ip address of the device")
ap.add_argument("-o", "--port", type=int, required=True,
                help="ephemeral port number of the server (1024 to 65535)")
ap.add_argument("-f", "--frame-count", type=int, default=32,
                help="# of frames used to construct the background model")
args = vars(ap.parse_args())

########## JAKI TRYB STEAMINGu
# szarosc, motion, blur, edge
option = 'blur'

# start a thread that will perform motion detection
t = threading.Thread(target=detect_motion, args=(
    args["frame_count"],option))
t.daemon = True
t.start()
# start the flask app
app.run(host=args["ip"], port=args["port"], debug=True,
        threaded=True, use_reloader=False)
# release the video stream pointer
vs.stop()


"""Functions to run the maximum entropy inversion method fitting for the
PFISR ion production rate.

@author Riley Troyer
science@rileytroyer.com
"""

import logging
import numpy as np

def estimate_initial_number_flux(energy_bins:np.ndarray, matrix_A:np.ndarray) -> np.ndarray:
    """Function to estimate the intial number flux for each energy bin
    INPUT
    energy_bins - energy values defining energy bins in eV
    matrix_A - inversion matrix
    OUTPUT
    initial_num_flux - estimated number flux in m^-2 s^-1 for each energy bin
    """

    # Make an initial guess of the number flux
    initial_num_flux = np.ones(len(energy_bins))*(1e12/len(energy_bins))

    # Divide by energy bin widths
    bin_widths = energy_bins - np.roll(energy_bins, shift=1)

    # Fix first value
    bin_widths[0] = energy_bins[0] - 0

    # Set initial guess
    initial_num_flux = initial_num_flux/bin_widths

    # If full column of A matrix is zero set initial flux to zero
    for j in range(len(energy_bins)):

        if np.sum(matrix_A[:, j]) == 0:
            initial_num_flux[j] = 0

    return initial_num_flux

def maximum_entropy_iteration(initial_num_flux:np.ndarray, altitude_bins:np.ndarray,
                              energy_bins:np.ndarray, matrix_A:np.ndarray,
                              q_estimate:np.ndarray, dq_estimate:np.ndarray,
                              max_iterations:int,
                              convergence:float=1e-2) -> 'np.ndarray, float, int':
    """Function to perform the maximum entropy iterative process to
    approximate inversion of matrix A.
    Process is outlined in Semeter & Kamalabadi 2005 and Troyer et al. (2022).
    INPUT
    initial_num_flux - initial guess of number flux for each energy bin
               in m^-2 s^-1
    altitude_bins - altitude values in meters defining altitude bins
    energy_bins - energy values in eV defining energy bins
    matrix_A - matrix that iteration is trying to invert
    q_estimate - estimated ion production rate from ISR m^-2 s^-1
    dq_estimate - error in ion production rate of ISR
    max_iterations - don't do more than this if can't converge
    convergence - change in chi squared value to aim for.
    OUTPUT
    new_num_flux - estimated number flux for energy bins in m^-2 s^-1
    reduced_chi_square - error in modeled fit
    good_alt_index - lower than this won't be good data
    """

    # Set previous value to initial at start
    old_num_flux = initial_num_flux
    new_num_flux = np.zeros(len(initial_num_flux))

    # Create array to store all minimum j values
    min_js = np.zeros(len(altitude_bins), dtype=int)

    # Find all nonzero indices of A matrix
    nonzero_args = np.argwhere(matrix_A > 0)

    for i in range(len(min_js)):

        non_zeros = nonzero_args[nonzero_args[:, 0] == i]

        # If there are no non zero values in row, then set to
        #...first instance
        if len(non_zeros) == 0:
            min_js[i] = 0

        # Otherwise find the minimum j
        else:
            min_js[i] = min(non_zeros[:, 1])

    # Initialize values
    old_chi_square = 1e3
    chi_square = 0
    old_chi2_diff = 1e9
    converged = True
    count = 0

    # Run interations until convergence or count is met
    while (old_chi2_diff > convergence):

        # Check count
        if count > max_iterations:
            logging.warning(f'Unable to converge. '
                            f'Max iterations reached with chi2 = {chi_square:0.2f}')
            break

        # Construct the t vector
        t = 1/np.dot(matrix_A[:, min_js], old_num_flux[min_js])

        # Adjust for infinite values in regions without a nonzero j
        t[t == np.inf] = 0

        for j in range(len(energy_bins)):

            # Construct c vector
            frac = np.inner(matrix_A, old_num_flux)/q_estimate
            c = 20 * (1 - frac) * t

            # Account for nan and infinite values
            #...this is why warning is raised
            c[np.isnan(c)] = 0
            c[c == -np.inf] = 0
            c[c == np.inf] = 0

            # Define w constant
            w = np.ones(len(altitude_bins))/len(altitude_bins)

            # Summation of matrix elements
            i_sum = np.sum(w*c*matrix_A[:, j])

            # New guess
            new_num_flux[j] = old_num_flux[j]/(1-old_num_flux[j]*i_sum)

        # Check chi squared, but only on altitudes that A is defined for
        diff=q_estimate-np.dot(matrix_A, new_num_flux)
        chi_square_array = diff**2/dq_estimate**2

        # Set undefined values to zero
        chi_square_array[np.isnan(chi_square_array)] = 0
        chi_square_array[chi_square_array == np.inf] = 0
        chi_square_array[chi_square_array == -np.inf] = 0

        # Get the chi squared value
        chi_square = np.sum(chi_square_array)

        # Do a convergence test, make sure it isn't blowing up
        if (old_chi2_diff
            < abs(old_chi_square - chi_square)) & (count > 1000):
            logging.warning(f'Not converging. Stopping. '
                            f'chi2 = {chi_square:0.2f}')
            converged = False
            break

        # Set old values to new
        old_num_flux = np.copy(new_num_flux)
        old_chi2_diff = abs(old_chi_square - chi_square)
        old_chi_square = chi_square

        # Set count
        count = count + 1

    # Get reduced chi square, which should be around 1
    diff=q_estimate-np.dot(matrix_A, new_num_flux)
    dof = len(q_estimate[dq_estimate < q_estimate]) - matrix_A.shape[1]

    # Notify of convergence
    if ((count < max_iterations) & (converged == True)):
        logging.info(f'Convergence reached. Iterations: {count-1}. '
                     f'Reduced chi2: {chi_square/dof:0.2f}')

    return new_num_flux, chi_square, dof, converged


import unittest
import webtest
from rdflib import BNode, ConjunctiveGraph, Graph, Literal, URIRef
from rdflib.namespace import RDF, RDFS, FOAF, XSD
import bottle
from flask_rdf.bottle import returns_rdf, output, Decorator

def make_graph():
	graph = Graph('IOMemory', BNode())
	person = URIRef('http://example.com/#person')
	graph.add((person, RDF.type, FOAF.Person))
	graph.add((person, FOAF.age, Literal(15, datatype=XSD.integer)))
	return graph
graph = make_graph()

def make_ctx_graph():
	context = URIRef('http://example.com/#root')
	graph = ConjunctiveGraph('IOMemory', context)
	person = URIRef('http://example.com/#person')
	graph.add((person, RDF.type, FOAF.Person))
	graph.add((person, FOAF.age, Literal(15, datatype=XSD.integer)))
	return graph
ctx_graph = make_ctx_graph()

def make_unicode_graph():
	mygraph = make_graph()
	mygraph.add((BNode(), FOAF.name, Literal('\u2603')))
	return mygraph
unicode_graph = make_unicode_graph()

application = bottle.Bottle()
@application.route('/test')
@returns_rdf
def ctxless():
	return graph
@application.route('/ctx')
@returns_rdf
def ctx():
	return ctx_graph
@application.route('/text')
@returns_rdf
def text():
	return 'This is a test string'
@application.route('/manual')
def manual():
	return output(graph, bottle.request.headers.get('Accept', ''))
@application.route('/unicode')
@returns_rdf
def unicode():
	return unicode_graph
@application.route('/202')
@returns_rdf
def custom():
	bottle.response.set_header('CustomHeader', 'yes')
	bottle.response.status = '202 Custom'
	return graph
app = webtest.TestApp(application)

class TestCases(unittest.TestCase):
	def test_format_simple(self):
		turtle = graph.serialize(format='turtle')
		headers = {'Accept': 'text/n3;q=0.5, text/turtle;q=0.9'}
		response = app.get('/test', headers=headers)
		self.assertEqual(turtle, response.body)
		self.assertEqual('text/turtle; charset=utf-8', response.headers['content-type'])
		self.assertEqual('Accept', response.headers['vary'])
		self.assertEqual(200, response.status_int)

	def test_format_unacceptable(self):
		turtle = graph.serialize(format='turtle')
		headers = {'Accept': 'text/html;q=0.9'}
		response = app.get('/test', headers=headers, status=406)
		self.assertEqual(406, response.status_int)

	def test_format_manual(self):
		turtle = graph.serialize(format='turtle')
		headers = {'Accept': 'text/n3;q=0.5, text/turtle;q=0.9'}
		response = app.get('/manual', headers=headers)
		self.assertEqual(turtle, response.body)
		self.assertEqual('text/turtle; charset=utf-8', response.headers['content-type'])
		self.assertEqual('Accept', response.headers['vary'])
		self.assertEqual(200, response.status_int)

	def test_format_quads_context(self):
		g = ctx_graph
		self.assertTrue(g.context_aware)
		quads = g.serialize(format='nquads')
		headers = {'Accept': 'application/n-quads;q=0.9'}
		response = app.get('/ctx', headers=headers)
		self.assertEqual(quads, response.body)
		self.assertEqual('application/n-quads', response.headers['content-type'])
		self.assertEqual('Accept', response.headers['vary'])
		self.assertEqual(200, response.status_int)

	def test_format_quads_lowprio(self):
		""" Test that quads are not used even if possible """
		g = ctx_graph
		quads = g.serialize(format='turtle')
		headers = {'Accept': 'text/turtle;q=0.9, application/n-quads;q=0.4'}
		response = app.get('/ctx', headers=headers)
		self.assertEqual(quads, response.body)
		self.assertEqual('text/turtle; charset=utf-8', response.headers['content-type'])
		self.assertEqual('Accept', response.headers['vary'])
		self.assertEqual(200, response.status_int)

	def test_format_quads_highprio(self):
		""" Test that quads are used with alternative """
		g = ctx_graph
		quads = g.serialize(format='nquads')
		headers = {'Accept': 'text/turtle;q=0.4, application/n-quads;q=0.9'}
		response = app.get('/ctx', headers=headers)
		self.assertEqual(quads, response.body)
		self.assertEqual('application/n-quads', response.headers['content-type'])
		self.assertEqual('Accept', response.headers['vary'])
		self.assertEqual(200, response.status_int)

	def test_format_quads_unavailable(self):
		""" Test that quads are not used with contextless store """
		g = graph
		quads = g.serialize(format='turtle')
		headers = {'Accept': 'text/turtle;q=0.4, application/n-quads;q=0.9'}
		response = app.get('/test', headers=headers)
		self.assertEqual(quads, response.body)
		self.assertEqual('text/turtle; charset=utf-8', response.headers['content-type'])
		self.assertEqual('Accept', response.headers['vary'])
		self.assertEqual(200, response.status_int)

	def test_empty_format_headers(self):
		xml = graph.serialize(format='xml')
		headers = {'Accept': ''}
		response = app.get('/test', headers=headers)
		self.assertEqual('application/rdf+xml', response.headers['content-type'])
		self.assertEqual('Accept', response.headers['vary'])

	def test_text(self):
		test_str = 'This is a test string'
		headers = {'Accept': 'text/n3;q=0.5, text/turtle;q=0.9'}
		response = app.get('/text', headers=headers)
		self.assertEqual(test_str.encode('utf-8'), response.body)

	def test_unicode(self):
		mygraph = unicode_graph
		turtle = mygraph.serialize(format='turtle')
		headers = {'Accept': 'text/turtle'}
		response = app.get('/unicode', headers=headers)
		self.assertEqual(turtle, response.body)
		self.assertEqual('text/turtle; charset=utf-8', response.headers['content-type'])
		self.assertEqual('Accept', response.headers['vary'])
		self.assertEqual(200, response.status_int)
		self.assertTrue('\u2603' in response.body.decode('utf-8'))

	def test_custom_response(self):
		turtle = graph.serialize(format='turtle')
		headers = {'Accept': 'text/turtle'}
		response = app.get('/202', headers=headers)
		self.assertEqual(turtle, response.body)
		self.assertEqual('text/turtle; charset=utf-8', response.headers['content-type'])
		self.assertEqual('Accept', response.headers['vary'])
		self.assertEqual('yes', response.headers['CustomHeader'])
		self.assertEqual(202, response.status_int)

	def test_decorators(self):
		turtle = graph.serialize(format='turtle')
		xml = graph.serialize(format='xml')
		view = graph
		accepts = 'text/n3;q=0.5, text/turtle;q=0.9'
		decorator = Decorator()
		response = decorator.output(view, accepts)
		self.assertEqual(turtle, response)
		# use the decorator
		decoratee = lambda *args: view
		decorated = decorator.decorate(decoratee)
		response = decorated()
		self.assertEqual(turtle, response)
		decorated = decorator(decoratee)
		response = decorated()
		self.assertEqual(turtle, response)


import json
import re

from urllib import parse, response
from lxml import etree
import scrapy
import time
import random

from douyu1scrapy.items import Douyu1ScrapyItem

class Douyu1Spider(scrapy.Spider):
    name = 'douyu'

    custom_settings = {
        'DEFAULT_REQUEST_HEADERS': {
            'Connection': 'keep-alive',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
        }
    }
    base_url='https://www.douyu.com/gapi/rkc/directory/2_201/{}'
    #  https://www.douyu.com/gapi/rkc/directory/2_201/2
    def start_requests(self):
        for i in range(1,6):
            url=self.base_url.format(i)
            yield scrapy.Request(url, callback=self.get_json,dont_filter=True)

    def get_json(self, response):
        text = response.text
        json_data = json.loads(text)
        kk = json_data['data']['rl']
        for j in kk:
            meta={ 'tupian':j['rs1'],
            'name':j['nn'],
            'fangjianhao':j['rid'],
            'hot' : j['ol']}
            if j['ol'] > 999999:
                rq = j['ol']
            else:
                rq = j['ol'] / 10000
            url='https://www.douyu.com/swf_api/h5room/{}'.format(j['rid'])
            yield scrapy.Request(url,callback=self.get_fans,meta=meta)

    def get_fans(self,response):
        text = response.text
        json_data=json.loads(text)
        fans=json_data['data']['fans']
        item = Douyu1ScrapyItem()
        item['tupian'] = response.meta['tupian']
        item['name'] = response.meta['name']
        item['fangjianhao'] = response.meta['fangjianhao']
        item['hot']=response.meta['hot']
        item['fans']=fans
        yield item


from django.urls import path
from django.conf.urls import url
from .views import SearchResultsView
from . import views

app_name = 'mainapp'

urlpatterns = [
    path('', views.index),
    path('myaccount', views.myaccount),
    path('register', views.register),
    path('login', views.login),
    path('logout', views.logout),
    path('post/new', views.post_new),
    path('post/create_post', views.create_post),
    path('post/destroy/<int:id>',views.destroy_post),
    path('post/<int:id>', views.post_detail),
    path('post/edit/<int:id>', views.post_edit),
    path('post/update_post/<int:id>', views.update_post),
    path('update', views.update),
    url(r'^calendar/$', views.CalendarView.as_view(), name='calendar'),
    path('search/', SearchResultsView.as_view(), name='search_results'),
]


# mongo.py

from flask import Flask
from flask import jsonify
from flask import request
from flask_pymongo import PyMongo

app = Flask(__name__)

app.config['MONGO_DBNAME'] = 'blog'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/blog'

mongo = PyMongo(app)

@app.route('/star', methods=['GET'])
def get_all_stars():
  star = mongo.db.sendlist
  output = []
  for s in star.find():
    output.append({'send_title' : s['send_title'], 'send_acro' : s['send_acro'], 'send_explanation' : s['send_explanation']})
  return jsonify({'result' : output})

@app.route('/star/<name>', methods=['GET'])
def get_one_star(name):
  star = mongo.db.sendlist
  s = star.find_one({'send_acro' : name})
  if s:
    output = {'send_title' : s['send_title'], 'send_acro' : s['send_acro'], 'send_explanation' : s['send_explanation']}
  else:
    output = "No such name"
  return jsonify({'result' : output})

@app.route('/star', methods=['POST'])
def add_star():
  star = mongo.db.sendlist
  send_title = request.json['send_title']
  send_acro = request.json['send_acro']
  send_explanation = request.json['send_explanation']
  star_id = star.insert({'send_title': send_title, 'send_acro': send_acro, 'send_explanation': send_explanation})
  new_star = star.find_one({'send_title': send_title })
  output = {'send_title' : new_star['send_title'], 'send_acro' : new_star['send_acro'], 'send_explanation' : new_star['send_explanation']}
  return jsonify({'result' : output})

if __name__ == '__main__':
    app.run(debug=True)


#!/usr/bin/env python
from discodos.config import Config
from discodos.cmd23 import helper, import_, mix, search, setup, stats, suggest
import logging
import click

# globals we use for logging, argparser and user interaction object
log = logging.getLogger('discodos')
args = None
user = None

@click.group(
    invoke_without_command=True,
    context_settings=dict(help_option_names=["-h", "--help"]))
@click.option(
    "-v", "--verbose", "verbose_count", count=True, default=0,
    help="""increases output verbosity / shows what DiscoDOS is doing under
    the hood (-v is INFO level, -vv is DEBUG level).""")
@click.option(
    "-o", "--offline", "offline_mode", is_flag=True,
    help="""DiscoDOS checks for connectivity to online services
    (Discogs, MusicBrainz, AcousticBrainz) itself. This option
    forces offline mode. A lot of options work in on- and
    offline mode. Some behave differently, depending on connection state.""")
@click.pass_context
def main_cmd(context, verbose_count, offline_mode):
    conf = Config()
    log.handlers[0].setLevel(conf.log_level)  # set configured console log lvl
    context.obj = helper.User(conf, verbose_count, offline_mode)

# Add commands
main_cmd.add_command(mix.mix_cmd)
main_cmd.add_command(search.search_cmd)
main_cmd.add_command(import_.import_cmd)
main_cmd.add_command(suggest.suggest_cmd)
main_cmd.add_command(stats.stats_cmd)
main_cmd.add_command(setup.setup_cmd)


import argparse
import hashlib
import json
import logging
import os
import sys
from pathlib import Path

from api import Client

def sha1sum(filename):
    with open(filename, 'rb', buffering=0) as f:
        return hashlib.file_digest(f, 'sha1').hexdigest()

class Importer:
    def __init__(self, client):
        self.client = client

    def import_albums_from_directory(self, path):
        albums_path = Path(path)
        if not albums_path.exists():
            logging.getLogger().error("does not exist: %s", path)
            return False

        for directory in os.scandir(albums_path):
            logging.getLogger().info("Importing directory: %s", directory)
            self.import_album_from_directory(directory)

    def import_album_from_directory(self, path, metadata_filename="metadata.json"):
        album_path = Path(path)
        metadata_file = album_path.joinpath(metadata_filename)

        if not album_path.exists():
            logging.getLogger().error("does not exist: %s", path)
            return False

        if not album_path.is_dir():
            logging.getLogger().error("not a directory: %s", path)
            return False

        if not metadata_file.exists():
            logging.getLogger().error("does not exist: %s", metadata_file)
            return False

        metadata = self._read_metadata(metadata_file)
        if not metadata:
            logging.getLogger().error("unable to parse metadata %s", metadata_file)
            return False

        album = self.create_album(metadata)
        if album:
            self.import_album_images(album_path, album)

    def import_album_images(self, album_path, album, suffixes=None):
        if suffixes is None:
            suffixes = [".jpg", ".jpeg", ".png"]

        for file in album_path.glob("*"):
            if not file.is_file():
                logging.getLogger().debug("ignoring: %s", file)
                continue

            if file.suffix not in suffixes:
                logging.getLogger().debug("ignoring: %s", file)
                continue

            sha = sha1sum(file)
            photo = self.client.get_photo(sha)
            if photo:
                photos = self.client.add_album_photo(album, photo)
                logging.getLogger().debug("added %d photos to album %s", len(photos), album.title)

    def create_album(self, metadata):
        return self.client.create_album(metadata.get("title"),
                                        metadata.get("description"))

    @staticmethod
    def _read_metadata(metadata_file):
        with open(metadata_file) as f:
            metadata = json.loads(f.read())
            return metadata

def main(args):
    client = Client(args.get("host"))
    try:
        if not client.authenticate(args.get("username"), args.get("password")):
            logging.getLogger().error("authentication failed")
            sys.exit(-1)
        importer = Importer(client)
        importer.import_albums_from_directory(args.get("directory"))
    finally:
        client.logout()

if __name__ == '__main__':
    logging.basicConfig()
    logging.getLogger().setLevel(logging.INFO)

    parser = argparse.ArgumentParser(description="Import Google Takeout export to Photoprism",
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("-H", "--host", help="Base URL", default="http://localhost:2342")
    parser.add_argument("-u", "--username", help="Username", default="username")
    parser.add_argument("-p", "--password", help="Password", default="password")
    parser.add_argument("directory", help="Google Photos directory (containing albums)", type=Path)

    args = vars(parser.parse_args())
    main(args)


import random

from k_choice.graphical.two_choice.strategies.greedy_strategy import GreedyStrategy
from k_choice.graphical.two_choice.strategies.full_knowledge_DQN_strategy import FullKnowledgeDQNStrategy
from k_choice.graphical.two_choice.strategies.dp_strategy import DPStrategy

from k_choice.graphical.two_choice.graphs.cycle import Cycle
from k_choice.graphical.two_choice.graphs.hypercube import HyperCube
from k_choice.graphical.two_choice.graphs.random_regular_graph import RandomRegularGraph
from k_choice.graphical.two_choice.graphs.complete_graph import CompleteGraph

M = 25
N = 4
D = 2
GRAPH = CompleteGraph(n=N)
STRATEGY = GreedyStrategy(GRAPH, M)  # , use_pre_trained=False) Greedy(GRAPH, M) #
RUNS = 1000
PRINT_BEHAVIOUR = True

def REWARD_FUN(x):
    return -max(x)

def run_strategy(graph=GRAPH, m=M, strategy=STRATEGY, reward_fun=REWARD_FUN, print_behaviour=PRINT_BEHAVIOUR):
    loads = [0] * graph.n
    random_edges = random.choices(graph.edge_list, k=m)
    for i in range(m):
        bin1, bin2 = random_edges[i]
        if print_behaviour:
            print(f"Ball number {i}, bin1 load {loads[bin1]}, bin2 load {loads[bin2]}, chosen", end=": ")
        if strategy.decide_(bin1, bin2):
            chosen_bin = bin1
            if print_behaviour:
                print(f"FIRST")
        else:
            chosen_bin = bin2
            if print_behaviour:
                print(f"SECOND")

        loads[chosen_bin] += 1

    score = reward_fun(loads)
    return score

def run_strategy_multiple_times(graph=GRAPH, m=M, runs=RUNS, strategy=STRATEGY, reward_fun=REWARD_FUN,
                                print_behaviour=PRINT_BEHAVIOUR):
    scores = []
    for _ in range(runs):
        score = run_strategy(graph=graph, m=m, strategy=strategy, reward_fun=reward_fun, print_behaviour=print_behaviour)
        scores.append(score)
        strategy.reset_()
    avg_score = sum(scores) / runs
    if print_behaviour:
        print(f"The average score of this strategy is {avg_score}")
        print(f"The average normalised max load of this strategy is {-avg_score - m / graph.n}.")
    return scores

if __name__ == "__main__":
    run_strategy_multiple_times()


from collections import Counter
from typing import List

class Solution:
    def commonChars(self, words: List[str]) -> List[str]:
        freq_ch = Counter(words[0])

        for i in range(1, len(words)):
            cur = Counter(words[i])
            for key in freq_ch:
                if key in cur:
                    freq_ch[key] = min(freq_ch[key], cur[key])
                else:
                    freq_ch[key] = -1

        res = []
        for key in freq_ch:
            if freq_ch[key] != -1:
                res += [key] * freq_ch[key]

        return res

if __name__ == "__main__":
    instance = Solution()
    print(instance.commonChars(["bella", "label", "roller"]))  # ["e","l","l"]


import socket
import time
import picamera
import threading

class Stream(threading.Thread):

    def __init__(self):
        super(Stream, self).__init__()
        self._running = False

    def run(self):
        self._running = True
        print("start")
        try:
            with picamera.PiCamera() as camera:
                camera.resolution = (320, 240)
                camera.rotation = 180
                camera.framerate = 38

                server_socket = socket.socket()
                server_socket.bind(('0.0.0.0', 8034))
                server_socket.listen(0)
                connection = server_socket.accept()[0].makefile('wb')

                time.sleep(2)
                # Start recording, sending the output to the connection for 60
                # seconds, then stop
                camera.start_recording(connection, format='h264')
                while(True):
                    print("streammmm")
                    time.sleep(5)
                    #stopStream = True

                    if self._running == False:
                        camera.stop_recording()
                        return
        except e:
            print(e)
        finally:
            connection.close()
            server_socket.close()

    def stop(self):
        print("Stop")
        self._running = False


def doRectsOverlap(rect1, rect2):
    for a, b in [(rect1, rect2), (rect2, rect1)]:
        # Check if a's corners are inside b
        if ((isPointInsideRect(a.left, a.top, b)) or
            (isPointInsideRect(a.left, a.bottom, b)) or
            (isPointInsideRect(a.right, a.top, b)) or
            (isPointInsideRect(a.right, a.bottom, b))):
            return True

    return False

#used the by the doRectsOverlap function (won't be called directly from game code)
def isPointInsideRect(x, y, rect):
    if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom):
        return True
    else:
        return False

import pygame, sys, random
pygame.init()
screen = pygame.display.set_mode([640,480])
black = [0, 0, 0]

#the game's variables
paddle_x = 200
paddle_y = 450
paddle_speed = 10
score = 0

# The lazer variables
lasers = []
laser_speed = 8
laser_width = 5
laser_height = 10
laser_color = [255, 255, 0]

# The Trump variables
trump_x = 50
trump_y = 50
trump_xspeed = random.randint(5, 15)
trump_yspeed = random.randint(5, 15)
trump_stop = 0

trumpus_maximus_x = 300
trumpus_maximus_y = 50
trumpus_maximus_xspeed = random.randint(5, 15)
trumpus_maximus_yspeed = random.randint(5, 15)
trumpus_maximus_stop = 0

# importing images and sprites
derp = pygame.image.load('derp.png')
derp = pygame.transform.scale(derp, (10, 10))
trump = pygame.image.load('trump.jpeg')
trump = pygame.transform.scale(trump, (40, 40))
trump_max = pygame.image.load('trump2.jpg')
trump_max = pygame.transform.scale(trump_max, (40, 40))
myfont = pygame.font.SysFont("Arial", 35)

#pygame.mixer.music.load('freddys.mp3')
#pygame.mixer.music.play(-1)

running = True
#game loop
while running:
    keys = pygame.key.get_pressed()  #checking pressed keys
    if keys[pygame.K_LEFT]:
        paddle_x = paddle_x - paddle_speed
    if keys[pygame.K_RIGHT]:
        paddle_x = paddle_x + paddle_speed
    for event in pygame.event.get():
        #check if you've exited the game
        if event.type == pygame.QUIT:
            running = False

        #check if you pressed a key
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                lasers.append([paddle_x, paddle_y])

    #pause for 20 milliseconds
    pygame.time.delay(20)
    #make the screen completely white
    screen.fill(black)

    #make the paddle phase through the wall
    if paddle_x > screen.get_width():
        paddle_x = -60
    if paddle_x < -60:
        paddle_x = 640

    # making lasers go up
    for laser in lasers:
        laser[1] -= laser_speed

    if trump_stop == 1:
      trump_y = 0
    if trumpus_maximus_stop == 1:
      trumpus_maximus_y = 0

    # making Trump move
    if trump_stop <= 0:
      trump_x += trump_xspeed
      trump_y += trump_yspeed
      if trump_y <= 0:
        trump_yspeed = random.randint(1, 15)
      if trump_y >= 481:
        trump_yspeed = random.randint(-15, -1)
        trump_stop = 20
      if trump_x <= 0:
        trump_xspeed = random.randint(3, 15)
      if trump_x >= 600:
        trump_xspeed = random.randint(-15, -3)

    # making Trumpus Maximus move
    if trumpus_maximus_stop <= 0:
      trumpus_maximus_x += trumpus_maximus_xspeed
      trumpus_maximus_y += trumpus_maximus_yspeed
      if trumpus_maximus_y <= 0:
        trumpus_maximus_yspeed = random.randint(1, 15)
      if trumpus_maximus_y >= 481:
        trumpus_maximus_yspeed = random.randint(-15, -1)
        trumpus_maximus_stop = 20
      if trumpus_maximus_x <= 0:
        trumpus_maximus_xspeed = random.randint(3, 15)
      if trumpus_maximus_x >= 600:
        trumpus_maximus_xspeed = random.randint(-15, -3)

    trump_stop -= 1
    trumpus_maximus_stop -= 1

    #create imaginary rectangles
    derp_rectangle = pygame.Rect(paddle_x, paddle_y, 10, 10)
    trump_rectangle = pygame.Rect(trump_x, trump_y, 40, 40)
    trumpus_maximus_rectangle = pygame.Rect(trumpus_maximus_x, trumpus_maximus_y, 40, 40)
    for laser in lasers:
      laser_rectangle = pygame.Rect(laser[0], laser[1], 5, 10)
      if doRectsOverlap(laser_rectangle, trump_rectangle) == True:
          score += 10
          trump_stop = 20
          trump_y = 600
      if doRectsOverlap(laser_rectangle, trumpus_maximus_rectangle) == True:
          score += 20
          trumpus_maximus_stop = 20
          trumpus_maximus_y = 600

    if doRectsOverlap(derp_rectangle, trump_rectangle) == True:
        running = False
        label = myfont.render("Game Over", 1, pygame.color.THECOLORS['red'])
        screen.blit(label, (260, 100))

    if doRectsOverlap(derp_rectangle, trumpus_maximus_rectangle) == True:
        trumpus_maximus_stop = 20
        trumpus_maximus_y = 600

    #draw everything on the screen

    #draw derp
    screen.blit(derp, [paddle_x, paddle_y])

    #draw trump
    screen.blit(trump, [trump_x, trump_y])
    screen.blit(trump_max, [trumpus_maximus_x, trumpus_maximus_y])

    #draw lasers
    for laser in lasers:
        pygame.draw.rect(screen, laser_color, [laser[0], laser[1], laser_width, laser_height])

    label2 = myfont.render("Score: " + str(score), 1, pygame.color.THECOLORS['white'])
    screen.blit(label2, (460, 20))
    #update the entire display
    pygame.display.update()
pygame.quit()


import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing

mnist = keras.datasets.mnist
BATCH_SIZE = 200
EPOCHS = 3

(x_train, y_train), (x_test, y_test) = mnist.load_data() #x is the image, y is the number

#
x_train = keras.utils.normalize(x_train, axis=1).reshape(60000,28,28,1)
x_test  = keras.utils.normalize(x_test , axis=1).reshape(10000,28,28,1)
y_train = keras.utils.to_categorical(y_train, num_classes=10)
y_test = keras.utils.to_categorical(y_test, num_classes=10)

MODEL = keras.Sequential([

	#First Block
	layers.BatchNormalization(renorm=True),
	layers.Conv2D(filters=32,kernel_size=3, activation='relu',padding='same',input_shape=(28,28,1)),
	layers.Dropout(0.1),
	layers.MaxPool2D(2,2),

	#Second Block
	layers.BatchNormalization(renorm=True),
	layers.Conv2D(filters=32, kernel_size=3, activation='relu', padding='same'),
	layers.Dropout(0.1),
	layers.MaxPool2D(2,2),

	#Head Block
	layers.BatchNormalization(renorm=True),
	layers.Flatten(),
	layers.Dense(128, activation='relu'),
	layers.Dense(128, activation='relu'),
	layers.Dense(10, activation='softmax'),
])

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

MODEL.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs = EPOCHS)

MODEL.save('MNIST-CONVOLUTIONAL')


import torch
import torch.nn as nn

def make_dense_leaky_relu(latent_dim, slope):
    return nn.Sequential(nn.Linear(latent_dim, latent_dim),
                         nn.LeakyReLU(slope))

class UNet_block(nn.Module):
    def __init__(self, latent_dim, slope, middle_block):
        super(UNet_block, self).__init__()
        self.slope = slope
        self.activation = nn.LeakyReLU(slope)
        self.in_block = make_dense_leaky_relu(latent_dim, slope)
        self.out_block = make_dense_leaky_relu(latent_dim, slope)
        self.middle_block = middle_block

    def forward(self, x):
        identity = x
        x = self.activation(self.in_block(x))
        x = self.activation(self.middle_block(x))
        x = self.activation(self.out_block(x))
        return x + identity

class UNet(nn.Module):
    def __init__(self, t_prev=1, t_next=1,
                 input_embedding=None,
                 output_embedding=None,
                 latent_dim=None,
                 slope=0.15,
                 level_number=1
                ):
        """
        Шаблон нейронной сети, принимающей на вход t_prev шагов времени
        и предсказывающая на t_next шагов вперёд.
        Сеть состоит из трёх блоков:
        1) Входной блок (input_embedding)
        2) Промежуточный блок c полносвязной Unet архитектурой
        3) Выходной блок (output_embedding)
        """
        super(UNet, self).__init__()

        self.t_prev = t_prev
        self.t_next = t_next
        self.slope = slope
        self.latent_dim = latent_dim
        self.level_number = level_number

        self.input_embedding = input_embedding
        self.output_embedding = output_embedding

        if level_number == 0:
            #структура сети максимально проста - один слой
            self.unet = make_dense_leaky_relu(self.latent_dim, slope)

        elif level_number > 0:
            #создаём unet структуру рекурсивно по иерархии, начиная с самого глубокого
            #самый глубокий уровень
            unet_block = UNet_block(latent_dim=self.latent_dim, slope=0.15,
                                    middle_block=make_dense_leaky_relu(self.latent_dim, slope))
            #остальные уровни
            for level in range(level_number-1):
                unet_block = UNet_block(latent_dim=self.latent_dim, slope=0.15,
                                        middle_block=unet_block)
            self.unet = unet_block

        else:
            print('error: level_number < 0')

    def forward_several_steps(self, x, t_next):
        latent = x
        # запустим сеть в рекуррентном режиме, т.е. несколько раз
        # будем подавать выход на вход
        # для удобства выведем только самый первый и последний шаги
        for t in range(t_next):
            latent = self.forward(latent)
        return latent

    def forward_several_steps_and_save(self, x, t_next):
        latent = x
        predictions = []
        # запустим сеть в рекуррентном режиме, т.е. несколько раз
        # будем подавать выход на вход
        # для удобства выведем только самый первый и последний шаги
        for t in range(t_next):
            latent = self.forward(latent)
            predictions.append(latent)
        predictions = torch.stack(predictions, dim=1)
        predictions = predictions.squeeze(dim=2)
        return predictions

    def forward(self, x):
        identity = x
        latent = self.input_embedding(x)
        # запустим внутреннюю часть self.unet
        latent = self.unet(latent)
        output = self.output_embedding(latent)
        output += identity[:,:,:-2]
        #добавим исходные значения и припишем на выход входные значения аргона и азота,
        #которые не должны меняться
        output = torch.cat((output, x[:,:,-2:]), dim=-1)
        #output = F.relu(output)
        return output


import numpy as np
import cv2
import detect_aruco as da
from sympy import symbols, Eq, solve
import math

def vector_between_points( x , y ):
    """ Vectors from x -> y """
    return [ y[0] - x[0] , y[1] - x[1] ]

def get_param_mtx( x , y ):
    """
        Get parametrization of line through points x , y starting at x
        then convert to 2x2 matrix.
    """
    v = vector_between_points( x , y )
    return [x,v]

def euclid_distance( x , y ):
    """ Euclidiean Distance from x -> y"""
    return math.sqrt( math.pow((y[0] - x[0]), 2) + math.pow((y[1] - x[1]),2) )

def dot_prod( x , y ):
    """ Dot product of two vectors in R^2 """
    return x[0]*y[0] + x[1]*y[1]

def norm( x ):
    return math.sqrt( math.pow( x[0] , 2 ) + math.pow( x[1] , 2 ) )

def projection( a , b ):
    """ Vector projection of a onto b """
    coeff = dot_prod( a , b ) / math.pow( norm(b) , 2 )
    return [ b[0] * coeff , b[1] * coeff ]

class Env():

    def __init__( self ):
        self.aruco = da.Detect_Aruco()
        self.ARUCO_TAG_SIZE = 10 #cm
        self.dimensions = None
        self.aruco_tag_loc = dict()
        self.aruco_tag_centers = dict()
        self.unit_vector_x = None
        self.unit_vector_y = None

        for i in range(1,6):
            self.aruco_tag_loc[i] = None
            self.aruco_tag_centers[i] = None
        self.origin = None
        self.sim_origin = None

    def measure_workspace( self , image_or_path ):
        """
        Get dimensions of workspace from 5 Aruco Tags
        There will be one Aruco Tag in each corner, and one at the center top of the
        workspace so we can orient. Each tag will have a specific ID.
        Each tag will also have a specific size, for now we will default to 10cm
        ______________________________________________________
        |[1]                    [2] +x->                  [3]|
        |                        -y                          |
        |                        |                           |
        |                       \ /                          |
        |                                                    |
        |                                                    |
        |                                                    |
        |[4]______________________________________________[5]|

        For this problem, we will use projective geometry and homography to be able to convert pixel distances to actual measurements from an image.
        https://en.wikipedia.org/wiki/Homography
        """
        #Grab corners and id's of all 5 aruco tags
        corners , ids , image , image_cpy = self.aruco.detect_tags( image_or_path )

        #Put corresponding corners into aruco_tag_loc

        for i in range( 1 , 6 ):
            id_indx = np.where(ids == i) # ids.index(i)
            id_indx = int(id_indx[0])
            self.aruco_tag_loc[i] = corners[id_indx]

        #Get centers of tags using midpoint formula
        for i in range( 1 , 6 ):
            center = [ (self.aruco_tag_loc[i][0][0][0] + self.aruco_tag_loc[i][0][2][0]) / 2 , (self.aruco_tag_loc[i][0][0][1] + self.aruco_tag_loc[i][0][2][1]) / 2 ]
            self.aruco_tag_centers[i] = [ int(center[0]) , int(center[1])]
            cv2.circle( image , ( self.aruco_tag_centers[i][0] , self.aruco_tag_centers[i][1]) , 3, (0,0,255) , 5 )

        self.origin = self.aruco_tag_centers[2]
        self.unit_vector_x = vector_between_points( self.origin , self.aruco_tag_centers[3] )

        tmp = vector_between_points( self.origin , self.aruco_tag_centers[4] )
        tmp2 = vector_between_points( self.aruco_tag_centers[1] , self.origin )
        self.unit_vector_y = [tmp[0] + tmp2[0] , tmp[1] + tmp2[1]]

        del tmp
        del tmp2

        """
        Cross Ratio:
        (1-3)_topleft * (1-3)_topright / closedist_between * long_distbetween
        """
        euclid_dist_1 = euclid_distance(self.aruco_tag_loc[1][0][0],self.aruco_tag_loc[1][0][1])#Size of tag 1
        euclid_dist_3 = euclid_distance(self.aruco_tag_loc[3][0][0],self.aruco_tag_loc[3][0][1])#Size of tag 3
        euclid_dist_1_to_3 = euclid_distance(self.aruco_tag_loc[1][0][1] , self.aruco_tag_loc[3][0][0]  ) #distance from top right of 1 to top left of 3
        cross_ratio_pixel = (euclid_dist_1 + euclid_dist_1_to_3) * (euclid_dist_3+euclid_dist_1_to_3) #numerator
        cross_ratio_pixel = cross_ratio_pixel / ((euclid_dist_1_to_3) * (euclid_dist_1_to_3 + euclid_dist_1 + euclid_dist_3 )) #Denominator
        W = symbols('W')
        eq1 = Eq( ((5+W)*(5+W)) / (W*(W+5+5)) , cross_ratio_pixel)
        dist_cm_1_3 = max(solve(eq1,W)) #In cm, max because there it is sqrt, and sometimes are negative values

        #Now let's do it for 4->5
        euclid_dist_4 = euclid_distance(self.aruco_tag_loc[4][0][0],self.aruco_tag_loc[4][0][1])#Size of tag 4
        euclid_dist_5 = euclid_distance(self.aruco_tag_loc[5][0][0],self.aruco_tag_loc[5][0][1])#Size of tag 5
        euclid_dist_4_to_5 = euclid_distance(self.aruco_tag_loc[4][0][1] , self.aruco_tag_loc[5][0][0]  ) #distance from top right of 4 to top left of 5
        cross_ratio_pixel = (euclid_dist_4 + euclid_dist_4_to_5) * (euclid_dist_5+euclid_dist_4_to_5) #numerator
        cross_ratio_pixel = cross_ratio_pixel / ((euclid_dist_4_to_5) * (euclid_dist_4_to_5 + euclid_dist_4 + euclid_dist_5 )) #Denominator
        eq1 = Eq( ((5+W)*(5+W)) / (W*(W+5+5)) , cross_ratio_pixel)
        dist_cm_4_5 = max(solve(eq1,W)) #In cm, max because there it is sqrt, and sometimes are negative values

        #Now let's do it for 1->4
        euclid_dist_1 = euclid_distance(self.aruco_tag_loc[1][0][1],self.aruco_tag_loc[1][0][2])#Size of tag 1
        euclid_dist_4 = euclid_distance(self.aruco_tag_loc[4][0][1],self.aruco_tag_loc[4][0][2])#Size of tag 4
        euclid_dist_1_to_4 = euclid_distance(self.aruco_tag_loc[1][0][2] , self.aruco_tag_loc[4][0][1]  ) #distance from top right of 1 to top left of 4
        cross_ratio_pixel = (euclid_dist_1 + euclid_dist_1_to_4) * (euclid_dist_4+euclid_dist_1_to_4) #numerator
        cross_ratio_pixel = cross_ratio_pixel / ((euclid_dist_1_to_4) * (euclid_dist_1_to_4 + euclid_dist_1 + euclid_dist_4 )) #Denominator
        eq1 = Eq( ((5+W)*(5+W)) / (W*(W+5+5)) , cross_ratio_pixel)
        dist_cm_1_4 = max(solve(eq1,W)) #In cm, max because there it is sqrt, and sometimes are negative values

        #Now let's do it for 3->5
        euclid_dist_3 = euclid_distance(self.aruco_tag_loc[3][0][1],self.aruco_tag_loc[3][0][2])#Size of tag 3
        euclid_dist_5 = euclid_distance(self.aruco_tag_loc[5][0][1],self.aruco_tag_loc[5][0][2])#Size of tag 5
        euclid_dist_3_to_5 = euclid_distance(self.aruco_tag_loc[3][0][3] , self.aruco_tag_loc[5][0][0]  ) #distance from top right of 3 to top left of 5
        cross_ratio_pixel = (euclid_dist_3 + euclid_dist_3_to_5) * (euclid_dist_5+euclid_dist_3_to_5) #numerator
        cross_ratio_pixel = cross_ratio_pixel / ((euclid_dist_3_to_5) * (euclid_dist_3_to_5 + euclid_dist_3 + euclid_dist_5 )) #Denominator
        eq1 = Eq( ((5+W)*(5+W)) / (W*(W+5+5)) , cross_ratio_pixel)
        dist_cm_3_5 = max(solve(eq1,W)) #In cm, max because there it is sqrt, and sometimes are negative values

        print("\n=============================Workspace Dimensions=============================\n")
        print("                                 {0:10f}cm                             ".format(float(dist_cm_1_3)))
        print("           ______________________________________________________     ")
        print("           |[1]                    [2]                       [3]|     ")
        print("           |                                                    |     ")
        print("           |                                                    |     ")
        print("{0:5f}cm|                                                    |{0:5f}cm".format(float(dist_cm_1_4) ,float(dist_cm_3_5) ))
        print("           |                                                    |     ")
        print("           |                                                    |     ")
        print("           |                                                    |     ")
        print("           |[4]______________________________________________[5]|     ")
        print("                                 {0:5f}cm                             ".format(float(dist_cm_4_5)))
        print("\n=================================================================================\n\n")

        # Outline workspace on image
        cv2.line( image , tuple(self.aruco_tag_loc[1][0][1]) , tuple(self.aruco_tag_loc[3][0][0]), (0,255,0) , 2 )
        cv2.line( image , tuple(self.aruco_tag_loc[4][0][1]) , tuple(self.aruco_tag_loc[5][0][0]), (0,255,0) , 2 )
        cv2.line( image , tuple(self.aruco_tag_loc[1][0][2]) , tuple(self.aruco_tag_loc[4][0][1]), (0,255,0) , 2 )
        cv2.line( image , tuple(self.aruco_tag_loc[3][0][3]) , tuple(self.aruco_tag_loc[5][0][0]), (0,255,0) , 2 )
        cv2.line( image , tuple(self.aruco_tag_loc[1][0][2]) , tuple(self.aruco_tag_loc[1][0][1]), (0,255,0) , 2 )
        cv2.line( image , tuple(self.aruco_tag_loc[3][0][3]) , tuple(self.aruco_tag_loc[3][0][0]), (0,255,0) , 2 )

        #Distance list is [ 1->3 , 3->5 , 5->4 , 1->4], going around square clockwise.
        self.dimensions = [dist_cm_1_3 , dist_cm_3_5 , dist_cm_4_5 , dist_cm_1_4]
        return self.dimensions , image

    def px_to_cm( self , coord ):
        """
            With input in pixels, convert it to cm in the simulator
        """
        """ TODO change so that the the point is measured from two closest vectors on square """

        v = vector_between_points( self.aruco_tag_loc[1][0][1] , coord )# vector between top right of tag 1 to coordinate

        w = vector_between_points(self.aruco_tag_loc[1][0][1] , self.aruco_tag_loc[3][0][0] ) #vector between top right of tag 1 to top left of tag 3
        z = vector_between_points( self.aruco_tag_loc[1][0][2] , self.aruco_tag_loc[4][0][1] ) # vector between bottom right of tag 1 to top right of tag 4

        u = projection( v , w ) #x cooordinate in px
        p = projection( v , z ) #y coordinate in px

        return [norm(u)*(self.dimensions[0]/norm(w)) , norm(p)*(self.dimensions[3]/norm(z)) ]

    def define_sim_ws( self , origin_m ):
        """
            Given the center point of the table in the simulator,
            define the measured workspace in terms of coordinates in the simulator
            Coord is origin of table in sim
        """
        self.sim_origin = origin_m
        midpt_x = (self.aruco_tag_loc[1][0][1][0] + self.aruco_tag_loc[5][0][0][0] ) + (self.aruco_tag_loc[3][0][0][0]  + self.aruco_tag_loc[4][0][1][0] )
        midpt_x /= 4

        midpt_y = (self.aruco_tag_loc[1][0][1][1] + self.aruco_tag_loc[5][0][0][1] ) + (self.aruco_tag_loc[3][0][0][1]  + self.aruco_tag_loc[4][0][1][1] )
        midpt_y /= 4
        origin = [ round(midpt_x) , round(midpt_y) ] #Measured origin in pixels
        arr = []

        dist_v = self.px_to_cm( origin ) #Distance from top right of tag 1 to origin in cm
        dist_v = np.array(dist_v) / 100 #Convert to meters
        dist_v[0] = dist_v[0]  #Since the origin is in the center, this vector is in the 2nd quadrant
        arr.append(dist_v)

        bottom_left = vector_between_points( self.aruco_tag_loc[4][0][1] , origin )
        bottom_left = np.array(bottom_left) / norm(bottom_left) #turn to unit vector
        bottom_left = bottom_left *( math.sqrt( math.pow(self.dimensions[3],2) + math.pow(self.dimensions[2],2) ) / 2 )

        bottom_left[0] = -abs(bottom_left[0])
        bottom_left[1] = abs(bottom_left[1])
        top_right = vector_between_points( self.aruco_tag_loc[3][0][0] , origin )
        top_right = np.array(top_right) / norm(top_right) #turn to unit vector
        top_right = top_right * ( math.sqrt( math.pow(self.dimensions[0],2) + math.pow(self.dimensions[1],2) ) / 2 )
        top_right[0] = abs(top_right[0])
        top_right[1] = (-1)*top_right[1]
        arr.append(top_right /100)
        arr.append(bottom_left/100)

        bottom_right = np.array(self.px_to_cm( self.aruco_tag_loc[5][0][0] )) / 200 #Distance from top right of 1 to top left of 5 / 2
        bottom_right[1] = bottom_right[1]*(-1)
        bottom_right[0] = bottom_right[0]*(-1)
        arr.append(bottom_right)
        arr2 = []
        for i in range(4):
            arr2.append( np.array([arr[i][0] + origin_m[0], arr[i][1] + origin_m[1] , 0.54]) )
        arr2.append( np.array([dist_v[0] + origin_m[0], dist_v[1] + origin_m[1] , 0.7]) )
        arr2.append( np.array([bottom_left[0]/100 + origin_m[0], bottom_left[1]/100 + origin_m[1] , 0.9]) )
        print("Top Left: " , dist_v )
        print("Top Right: " , top_right / 100 )
        print("Bottom Right: " , bottom_right )
        print("Bottom Left: " , bottom_left / 100 )
        return arr2

        #Distance list is [ 1->3 , 3->5 , 5->4 , 1->4], going around square clockwise.

    def point_to_sim( self , point ):
        """ Point in m """
        print(point)
        point += np.array( self.sim_origin )
        return point

    def alter_table_urdf( self , dim ):
        """ Change sdf file in cairo sim so that the table dimensions are the same as the measured one above. """
        f = open( "/home/epiphyte/Documents/cairo_simulator/assets/table.sdf" , "r" )
        lines = f.readlines()
        #Lines to alter 10 , 26
        x_size = (dim[0] + dim[2]) / 200
        y_size = ( dim[1] + dim[3]) / 200
        lines[9] = "            <size>{} {} 0.03</size>\n".format(x_size , y_size)
        lines[25] = "            <size>{} {} 0.03</size>\n".format(x_size , y_size)
        f.close()
        f = open("/home/epiphyte/Documents/cairo_simulator/assets/table.sdf" , "w")
        f.writelines( lines )
        f.close()

    def distance_from_cam_to_workspace( self ):
        """ Returns distance from camera to workspace """
        pass

    def track_tag( self, image_or_path , id=28 ):
        corners , ids , image , image_cpy = self.aruco.detect_tags( image_or_path )

        return image
    def watch_to_sim( self , image_path ):
        """
            Assuming Eagle Eye View
            Returns x,y,z coordinates
        """
        corners , ids , image , _ = self.aruco.detect_tags( image_path )

        if( ids is None ):
            return None , None
        if( [28] not in ids ):
            print("Watch not found in photo.")
            return None , None

        #Get values of watch
        watch_indx = list(ids).index([28])
        watch_corners = corners[watch_indx][0]
        watch_center = [ int( ( watch_corners[0][0] + watch_corners[2][0] ) / 2 ) , int( ( watch_corners[0][1] + watch_corners[2][1] ) / 2 ) ]

        """
        print("999999999999999999999999999999999999999999999999999999999999999999999999999")
        ia = 0
        for a in corners:
            print("Corner " , a[0])
            print("Id: " , ids[ia])
            ia += 1
            print("")

        print("===========================")
        print("Watch Indx " , watch_indx)
        print("===========================")
        print("Watch Corners " , watch_corners )
        print("===========================")

        print("===========================")
        print(self.aruco_tag_loc[1][0][1] )
        print(self.px_to_cm([self.aruco_tag_loc[1][0][1][0] + 100 , self.aruco_tag_loc[1][0][1][1] + 100] ))
        print("999999999999999999999999999999999999999999999999999999999999999999999999999")
        """

        watch_cm = self.px_to_cm( watch_center )
        print("Top Left " , self.px_to_cm(  self.aruco_tag_loc[1][0][1] ))
        print("Bottom Right " , self.px_to_cm( self.aruco_tag_loc[5][0][0] ))
        print("Bottom Left " , self.px_to_cm( self.aruco_tag_loc[4][0][1] ))
        print("Top Right " , self.px_to_cm( self.aruco_tag_loc[3][0][0] ))
        print("Watch cm: " , watch_cm)
        print( self.aruco_tag_centers )
        print("Center " , watch_center)
        print("\n\n")
        watch_cm_three = np.array([self.sim_origin[0] + (watch_cm[0] / 100) , self.sim_origin[1] + (watch_cm[1] / 100) , self.sim_origin[2] + 0.2])

        #draw circle on watch
        cv2.circle( image , tuple(watch_center) , 7 , (0, 0, 255) , 5)

        return watch_cm_three , image

"""
    References:

    Shortest distance between two skew lines in 3D space. Watch just for the scottish accent ---> https://www.youtube.com/watch?v=HC5YikQxwZA
"""

""" FIND VANISHING POINT

        #Get parametrization of lines 1->3 and 4->5
        #u = get_param_mtx( self.aruco_tag_loc[1][0][0] , self.aruco_tag_loc[3][0][0] )
        #v = get_param_mtx( self.aruco_tag_loc[4][0][0] , self.aruco_tag_loc[5][0][0] )

        #eq_x = Eq( t*u[1][0] + u[0][0] - s*v[1][0] - v[0][0] ) # These equations are: (1->3) - (4->5)
        #eq_y = Eq( t*u[1][1] + u[0][1] - s*v[1][1] - v[0][1] ) # For each parametrization
        #eq_z = Eq( t*u[1][2] + u[0][2] - s*v[1][2] - v[0][2] )

        ### Find the cloeset point between two possibly skew lines. ###
        #t, s = symbols('t s')
        #eq1 = Eq( ( u[1][0] * ( t*u[1][0] + u[0][0] - s*v[1][0] - v[0][0] ) + u[1][1] * ( t*u[1][1] + u[0][1] - s*v[1][1] - v[0][1] ) ) , 0 )
        #eq2 =  Eq( ( v[1][0] * ( t*u[1][0] + u[0][0] - s*v[1][0] - v[0][0] ) + v[1][1] * ( t*u[1][1] + u[0][1] - s*v[1][1] - v[0][1] ) ) , 0 )
        #t_s = solve((eq1,eq2),(t,s))
        #if( len(t_s) == 1 ): #Two lines are parallel.
        #   return None
        #Now that we have the vanishing pt, we can use homography to get the actual distance between tags
        #vanishing_pt = (u[0][0] + u[1][0] * t_s[t]  , u[0][1] + u[1][1] * t_s[t] )

"""


#!/usr/bin/env python

# start compatibility with IPython Jupyter 4.0
try:
    from jupyter_client import BlockingKernelClient
except ImportError:
    from IPython.kernel import BlockingKernelClient

# python3/python2 nonsense
try:
    from Queue import Empty
except:
    from queue import Empty

import atexit
import subprocess
import uuid
import time
import os
import sys
import json
import re

dirname = os.path.dirname(os.path.abspath(__file__))

class Kernel(object):
    # kernel config is stored in a dot file with the active directory
    def __init__(self, config, active_dir, pyspark):
        # right now we're spawning a child process for IPython. we can
        # probably work directly with the IPython kernel API, but the docs
        # don't really explain how to do it.
        log_file = None
        if pyspark:
            os.environ["IPYTHON_OPTS"] = "kernel -f %s" % config
            pyspark = os.path.join(os.environ.get("SPARK_HOME"), "bin/pyspark")
            spark_log = os.environ.get("SPARK_LOG", None)
            if spark_log:
                log_file = open(spark_log, "w")
            spark_opts = os.environ.get("SPARK_OPTS", "")
            args = [pyspark] + spark_opts.split()  # $SPARK_HOME/bin/pyspark <SPARK_OPTS>
            p = subprocess.Popen(args, stdout=log_file, stderr=log_file)
        else:
            args = [sys.executable, '-m', 'IPython', 'kernel', '-f', config]
            p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        # when __this__ process exits, we're going to remove the ipython config
        # file and kill the ipython subprocess
        atexit.register(p.terminate)

        def remove_config():
            if os.path.isfile(config):
                os.remove(config)
        atexit.register(remove_config)

        # i found that if i tried to connect to the kernel immediately, so we'll
        # wait until the config file exists before moving on
        while os.path.isfile(config)==False:
            time.sleep(0.1)

        def close_file():
            if log_file:
                log_file.close()
        atexit.register(close_file)

        # fire up the kernel with the appropriate config
        self.client = BlockingKernelClient(connection_file=config)
        self.client.load_connection_file()
        self.client.start_channels()
        # load our monkeypatches...
        self.client.execute("%matplotlib inline")
        python_patch_file = os.path.join(dirname, "langs", "python-patch.py")
        self.client.execute("%run " + python_patch_file)

    def _run_code(self, execution_id, code, timeout=0.1):
        # this function executes some code and waits for it to completely finish
        # before returning. i don't think that this is neccessarily the best
        # way to do this, but the IPython documentation isn't very helpful for
        # this particular topic.
        #
        # 1) execute code and grab the ID for that execution thread
        # 2) look for messages coming from the "iopub" channel (this is just a
        #    stream of output)
        # 3) when we get a message that is one of the following, save relevant
        # data to `data`:
        #       - execute_result - content from repr
        #       - stream - content from stdout
        #       - error - ansii encoded stacktrace
        # the final piece is that we check for when the message indicates that
        # the kernel is idle and the message's parent is the original execution
        # ID (msg_id) that's associated with our executing code. if this is the
        # case, we'll return the data and the msg_id and exit
        msg_id = self.client.execute(code, allow_stdin=False)
        request = { "id": execution_id, "msg_id": msg_id, "code": code, "status": "started" }
        sys.stdout.write(json.dumps(request) + '\n')
        sys.stdout.flush()
        output = {
            "id": execution_id,
            "msg_id": msg_id,
            "output": "",
            "stream": None,
            "image": None,
            "error": None
        }
        while True:
            try:
                reply = self.client.get_iopub_msg(timeout=timeout)
            except Empty:
                continue

            if "execution_state" in reply['content']:
                if reply['content']['execution_state']=="idle" and reply['parent_header']['msg_id']==msg_id:
                    if reply['parent_header']['msg_type']=="execute_request":
                        request["status"] = "complete"
                        sys.stdout.write(json.dumps(request) + '\n')
                        sys.stdout.flush()
                        return
            elif reply['header']['msg_type']=="execute_result":
                output['output'] = reply['content']['data'].get('text/plain', '')
                output['stream'] = reply['content']['data'].get('text/plain', '')
            elif reply['header']['msg_type']=="display_data":
                if 'image/png' in reply['content']['data']:
                    output['image'] = reply['content']['data']['image/png']
                elif 'text/html' in reply['content']['data']:
                    output['html'] = reply['content']['data']['text/html']
            elif reply['header']['msg_type']=="stream":
                output['output'] += reply['content'].get('text', '')
                output['stream'] = reply['content'].get('text', '')
            elif reply['header']['msg_type']=="error":
                output['error'] = "\n".join(reply['content']['traceback'])

            # TODO: if we have something non-trivial to send back...
            sys.stdout.write(json.dumps(output) + '\n')
            sys.stdout.flush()
            # TODO: should probably get rid of all this
            output['stream'] = None
            output['image'] = None
            output['html'] = None

    def _complete(self, execution_id, code, timeout=0.5):
        # Call ipython kernel complete, wait for response with the correct msg_id,
        # and construct appropriate UI payload.
        # See below for an example response from ipython kernel completion for 'el'
        #
        # {
        # 'parent_header':
        #     {u'username': u'ubuntu', u'version': u'5.0', u'msg_type': u'complete_request',
        #     u'msg_id': u'5222d158-ada8-474e-88d8-8907eb7cc74c', u'session': u'cda4a03d-a8a1-4e6c-acd0-de62d169772e',
        #     u'date': datetime.datetime(2015, 5, 7, 15, 25, 8, 796886)},
        # 'msg_type': u'complete_reply',
        # 'msg_id': u'a3a957d6-5865-4c6f-a0b2-9aa8da718b0d',
        # 'content':
        #     {u'matches': [u'elif', u'else'], u'status': u'ok', u'cursor_start': 0, u'cursor_end': 2, u'metadata': {}},
        # 'header':
        #     {u'username': u'ubuntu', u'version': u'5.0', u'msg_type': u'complete_reply',
        #     u'msg_id': u'a3a957d6-5865-4c6f-a0b2-9aa8da718b0d', u'session': u'f1491112-7234-4782-8601-b4fb2697a2f6',
        #     u'date': datetime.datetime(2015, 5, 7, 15, 25, 8, 803470)},
        # 'buffers': [],
        # 'metadata': {}
        # }
        #
        msg_id = self.client.complete(code)
        request = { "id": execution_id, "msg_id": msg_id, "code": code, "status": "started" }
        sys.stdout.write(json.dumps(request) + '\n')
        sys.stdout.flush()
        output = { "id": execution_id, "msg_id": msg_id, "output": None, "image": None, "error": None }
        while True:
            try:
                reply = self.client.get_shell_msg(timeout=timeout)
            except Empty:
                continue

            if "matches" in reply['content'] and reply['msg_type']=="complete_reply" and reply['parent_header']['msg_id']==msg_id:
                results = []
                for completion in reply['content']['matches']:
                    result = {
                        "value": completion,
                        "dtype": "---"
                    }
                    if "." in code:
                        # result['text'] = result['value'] # ".".join(result['value'].split(".")[1:])
                        result['text'] = result['value'] #.split('.')[-1]
                        result["dtype"] = "function"
                    else:
                        result['text'] = result['value']
                        result["dtype"] = "" # type(globals().get(code)).__name__
                    results.append(result)
                output['output'] = results
                output['status'] = "complete"
                sys.stdout.write(json.dumps(output) + '\n')
                sys.stdout.flush()
                return

    def execute(self, execution_id, code, complete=False):
        if complete==True:
            return self._complete(execution_id, code)
        else:
            result = self._run_code(execution_id, code)
            if re.match("%?reset", code):
                # load our monkeypatches...
                k.client.execute("%matplotlib inline")
                k.client.execute(vars_patch)
            return result

    def get_packages(self):
        return self.execute("__get_packages()")

if __name__=="__main__":
    active_dir = os.path.realpath('.')
    k = Kernel(sys.argv[1], active_dir, False)

    while True:
        line = sys.stdin.readline()

        # watch for kill command
        if line.strip()=="EXIT":
            sys.exit(0)

        data = json.loads(line)
        k.execute(data['id'], data['code'], data.get('complete', False))


import tensorflow as tf
import numpy as np
import math
from scipy import sparse
import pickle
from time import time
import logging

# ------------------------------------
logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('2021-11-pre-training-dims128.log')
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(fh)
logger.addHandler(ch)
# ------------------------------------

def load_pkl(pkl_name):
    with open(pkl_name + '.pkl', 'rb') as f:
        return pickle.load(f)

# load question graph information
# load question-relations

# if you use npz file
question_question_relation = sparse.load_npz('ednet/pro_pro.npz')  # read question relation matrix
print('The edges of questions are %d' % question_question_relation.nnz)
logger.info('The edges of questions are %d' % question_question_relation.nnz)
question_num = question_question_relation.shape[0]
print('question-nums %d' % question_num)
logger.info('question-nums %d' % question_num)
question_question_dense = question_question_relation.toarray()  # num_questions * num_questions

# # if you use txt file
# question_question_relation = np.loadtxt('ednet/pro_pro.txt')  # read question relation matrix
# question_question_dense = question_question_relation

# load question difficulty-levles
diff = load_pkl('ednet/problem_difficulty')
print('question-nums-diff %d' % len(diff))  # num_questions * 1
logger.info('question-nums-diff %d' % len(diff))

# set the parameters
keep_prob = 0.5
lr = 0.001
bs = 256
epochs = 20
embed_dim = 128

# set placeholder
tf_question = tf.placeholder(tf.int32, [None])  # input question id
tf_question_question_targets = tf.placeholder(tf.float32, [None, question_num], name='tf_question_question')  # input question similarity
tf_keep_prob = tf.placeholder(tf.float32, [1], name='tf_keep_prob')
tf_diff = tf.placeholder(tf.float32, [None], name='tf_diff')  # input question difficulty

# initialize question representations of the factor interaction subspace
question_embedding_matrix = tf.Variable(tf.random_normal([question_num, embed_dim], 0.0, 0.01), name='item_embeddings')
# initialize question representations of the factor subspace
question_bias_matrix = tf.Variable(tf.random_normal([question_num, embed_dim], 0.0, 0.01), name='item_bias')

# embeded question
question_embed = tf.nn.embedding_lookup(question_embedding_matrix, tf_question)  # [bs, embed_dim]
question_bias = tf.nn.embedding_lookup(question_bias_matrix, tf_question)  # [bs, embed_dim]

# calculate the difficulty of question embeddings ------------------------------
question_diff_embeddings = tf.reshape(tf.layers.dense(question_embed, units=1), [-1])
question_diff_bias = tf.reshape(tf.layers.dense(question_bias, units=1), [-1])

# squared loss of question difficulty-level
mse_emb_diff = tf.reduce_mean(tf.square(question_diff_embeddings - tf_diff))
mse_bias_diff = tf.reduce_mean(tf.square(question_diff_bias - tf_diff))

# calculate the similarity based on the inner product or cosine
question_question_logits_embeddings = tf.reshape(tf.matmul(question_embed, tf.transpose(question_embedding_matrix)), [-1])
question_question_logits_bias = tf.reshape(tf.matmul(question_bias, tf.transpose(question_bias_matrix)), [-1])

# question-relation labels
tf_question_question_targets_reshape = tf.reshape(tf_question_question_targets, [-1])  # contious values like 1/3, 1/6...

# squared loss of question relations
loss_question_question_bias = tf.reduce_mean(tf.square(tf.subtract(question_question_logits_bias, tf_question_question_targets_reshape)))
loss_question_question_embeddings = tf.reduce_mean(tf.square(tf.subtract(question_question_logits_embeddings, tf_question_question_targets_reshape)))
# cross-entropy loss is only used for binary similarities and our paper uses squared loss
# loss_question_question_embeddings = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf_question_question_targets_reshape, logits=question_question_logits_embeddings))
# loss_question_question_bias = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf_question_question_targets_reshape, logits=question_question_logits_bias))

# the total loss, and we can set different weight on them
loss = loss_question_question_embeddings + loss_question_question_bias + mse_emb_diff + mse_bias_diff

# begin training
optimizer = tf.train.AdamOptimizer(learning_rate=lr)
train_op = optimizer.minimize(loss)
print('finish building graph')
logger.info('finish building graph')

saver = tf.train.Saver()
train_steps = int(math.ceil(question_num / float(bs)))
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(epochs):
        t_initial = time()
        train_loss = 0
        for m in range(train_steps):
            b, e = m * bs, min((m + 1) * bs, question_num)
            batch_question = np.arange(b, e).astype(np.int32)  # read batch question id
            batch_question_question_targets = question_question_dense[b:e, :]  # read batch question-relation labels
            batch_diff = [diff[i] for i in range(b, e)]
            feed_dict = {tf_question: batch_question,
                         tf_question_question_targets: batch_question_question_targets,
                         tf_keep_prob: [keep_prob],
                         tf_diff: batch_diff}

            _, loss_ = sess.run([train_op, loss], feed_dict=feed_dict)
            train_loss += loss_

        train_loss /= train_steps
        print("epoch %d, loss %.8f [%.1f s]" % (i, train_loss, time() - t_initial))
        logger.info("epoch %d, loss %.8f [%.1f s]" % (i, train_loss, time() - t_initial))
    saver.save(sess, 'ednet/pre_train/problem_embedding_inner_diff_128dims')

    print('finish training')
    logger.info("finish training")