# Data Model:
# ===========
# lines is a list of "\n" terminated strings from the input file
# Each line is the location and velocity of a point light source.
# parsing returns a ((x,y),(vx,vy)) tuple, which will be converted
# to a mutable [x,y,(vx,vy)] list.
#
# Strategy: Divide the location by the velocity to get number of time steps to
# get to +/-1 (close enough to zero).  The answer does not have to be
# centered on 0,0 but it appears that it is.  Take the average, and apply
# that time step to all locations.  Find the x and y extents.  Try moving
# forward 1 time step.  If the extents shrink, keep stepping until the
# extents are minimal, otherwise, step backwards (apply negative velocity),
# until the minimum is found.  Assuming the minimal X and Y will coincide
# with each other, and with the solution.  If not, visually explore both the
# minimal y and x extents, and all nearby timesteps.

def part1(lines):
    pts = parse(lines)
    pts = find_message(pts)
    display(pts)
    return "FBHKLEAG"

def part2(lines):
    pts = parse(lines)
    pts2 = find_message(pts)
    ((x1,y1),_) = pts[0]
    x2,y2,(vx,vy) = pts2[0]
    steps = (x2-x1)//vx
    # print(x1,x2,vx,steps)
    # print(y1,y2,vy,(y2-y1)//vy)
    return steps

def parse(lines):
    pts = []
    for line in lines:
        # position=<-9, -1> velocity=< 0,  2>
        line = line.strip().replace("position=<","").replace("> velocity=<",";").replace(">","")
        c,v = line.split(";")
        x,y = [int(n) for n in c.split(",")]
        vx,vy = [int(n) for n in v.split(",")]
        pt = ((x,y),(vx,vy))
        pts.append(pt)
    return pts

def find_message(pts):
    pts = normalize(pts)
    dx, dy = span(extents(pts))
    step(pts, forward=True)
    dx2, dy2 = span(extents(pts))
    forward = dx2 < dx and dy2 < dy
    # FIXME: Need to change check if forward is initially not true
    # fortunately, it is for the test and puzzle input.
    while dx2 < dx and dy2 < dy:
        step(pts, forward=forward)
        dx, dy = dx2, dy2
        dx2, dy2 = span(extents(pts))
    step(pts, forward=(not forward))
    return pts

def normalize(pts):
    # divide by the average number of timesteps to get to +/-1
    ts_x, ts_y = 0, 0
    for pt in pts:
        ((x,y),(vx,vy)) = pt
        if vx == 0 or vy == 0:
            # I think this is only in test data
            # if the v is 0, then it will be a final position in 0 time steps
            continue
        ts_x += (x/vx)
        ts_y += (y/vy)
    ts_x_avg = ts_x/len(pts)
    ts_y_avg = ts_y/len(pts)
    ts = -1 * int((ts_x_avg+ts_y_avg)/2)
    new_pts = []
    for pt in pts:
        ((x,y),(vx,vy)) = pt
        x = x + vx*ts
        y = y + vy*ts
        new_pt = [x,y,(vx,vy)]
        new_pts.append(new_pt)
    return new_pts

def span(extent):
    x_min, x_max, y_min, y_max = extent
    return x_max - x_min, y_max - y_min

def extents(pts):
    ys = [y for [x,y,v] in pts]
    xs = [x for [x,y,v] in pts]
    return min(xs), max(xs), min(ys), max(ys)

def step(pts, forward=True):
    delta = 1 if forward else -1
    for pt in pts:
        vx,vy = pt[2]
        pt[0] += (vx * delta)
        pt[1] += (vy * delta)

def display(pts):
    x1,x2,y1,y2 = extents(pts)
    cols = 1+x2-x1
    rows = 1+y2-y1
    lines = [[' ']*cols for _ in range(rows)]
    for pt in pts:
        x,y,_ = pt
        r,c = y-y1, x-x1
        lines[r][c] = "*"
    for line in lines:
        print("".join(line))

if __name__ == '__main__':
    # lines = open("test.txt").readlines() # as a list of line strings
    lines = open("input.txt").readlines() # as a list of line strings
    print(f"Part 1: {part1(lines)}")
    print(f"Part 2: {part2(lines)}")


from compras import Compras

compras = Compras

def opcoesMenuInicial():

    while True:

        menuInitial = int(input("\nO que você deseja fazer? \n1. Comprar um pet.\n2.Comprar um produto para o meu pet.\n3.Serviços para o pet.\n4. Sair\n"))

        if(menuInitial == 1):
            compras.opcoesAnimais()
            break

        elif(menuInitial == 2):
            compras.opcoesProdutos()
            break

        elif(menuInitial == 3):
            print("Função indisponível no momento!\n")

        elif(menuInitial == 4):
            break


"""
Whatson db

This module handles talking to Postgres via `psycopg2`.
"""

import logging
import os
import psycopg2
from psycopg2.extras import RealDictCursor
from dotenv import load_dotenv

load_dotenv()

LOG = logging.getLogger("whatson.db")
LOG.setLevel(logging.DEBUG)

DB = psycopg2.connect(os.environ["DATABASE_URL"], cursor_factory=RealDictCursor)

def reset_database(db):
    """Resets the database to its basic schema"""
    with db as conn:
        cursor = conn.cursor()
        cursor.execute("DROP TABLE IF EXISTS shows")
        cursor.execute(
            """CREATE TABLE shows (
                id SERIAL PRIMARY KEY,
                theatre VARCHAR(255) NOT NULL,
                title VARCHAR(255) NOT NULL,
                image_url TEXT NOT NULL,
                link_url TEXT NOT NULL,
                start_date DATE NOT NULL,
                end_date DATE NOT NULL,
                created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
                )"""
        )
        cursor.execute(
            """CREATE UNIQUE INDEX _idx_shows_theatre_title
                ON shows (theatre, title)
                """
        )

        cursor.execute(
            """CREATE OR REPLACE FUNCTION total_months(date)
            RETURNS int AS
                'select (extract(year from $1) * 12 + extract(month from $1))::int'
                    language sql immutable
            """
        )


import cv2
import numpy as np
from PIL import Image
import database as dbs
from pytesseract import pytesseract

def tesseract():
    path_to_tesseract = r'D:\Programs\Tesseract\tesseract.exe'
    image_path = 'test.jpg'
    pytesseract.tesseract_cmd = path_to_tesseract
    text = pytesseract.image_to_string(Image.open(image_path))
    print(text[:-1])

camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)
amarilloBajo = np.array([15,100,20],np.uint8)
amarilloAlto = np.array([45,255,255],np.uint8)

while True:
    #  _,image = camera.read()
     ret, frame = camera.read()

     if ret == True:
         frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
         mask = cv2.inRange(frameHSV, amarilloBajo,amarilloAlto)

        # hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        # # Threshold of blue in HSV space
        # lower_blue = np.array([60, 35, 140])
        # upper_blue = np.array([180, 255, 255])
        # mask = cv2.inRange(hsv, lower_blue, upper_blue)

         cv2.imshow('frame',frame)
         res = cv2.bitwise_and(frame, frame, mask=mask)
         cv2.imshow("res", res)
         gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)

         if cv2.waitKey(1)& 0xFF==ord('s'):
            cv2.imwrite('test.jpg',cv2.blur(gray,(3,3)))
            break

camera.release()
cv2.destroyAllWindows()
tesseract()


# -*- coding: utf-8 -*-
# @Time    : 2018/6/22 9:59
# @Author  : Jason
# @FileName: Lyric.py

import re
import gluonbook as gb
from mxnet import autograd, nd
from mxnet.gluon import loss as gloss
import random
import zipfile

# 解压文件
with zipfile.ZipFile('./data/jaychou_lyrics.txt.zip', 'r') as zin:
    zin.extractall('./data/')
# 打开文件
with open('./data/jaychou_lyrics.txt', encoding='utf-8') as f:
    all_chars = f.read()

# 处理数据集，将换行符替换成空格且只保留中文字符，使用前两万个字符当作训练集
all_chars = all_chars.replace('\n', ' ').replace('\r', ' ').replace('\\\\', '')
all_chars = re.sub('[A-Za-z0-9\.\*\+\?\]\[＞＜<】〇〗〖\\\\【>!?>><<~/\u3000》,☆。！《》、`,～？…]', '', all_chars)
# print(len(all_chars))  # 63282
train_set = all_chars[0:20000]
# print(train_set[0:50])

# 将数据集中所有不同字符提取出来做成字典
index_to_char = list(set(all_chars))  # 不同字符列表
# print(index_to_char)
char_to_index = dict([(char, i) for i, char in enumerate(index_to_char)])  # 字符:频次  字典
vocab_size = len(char_to_index)
# print(char_to_index)
# print(vocab_size)  # 一共有2514个不同的汉字

# 将每个汉字转成从0开始的索引
chars_indices = [char_to_index[char] for char in all_chars]  # 索引
sample = chars_indices[:50]
# print('chars:\n', ''.join([index_to_char[idx] for idx in sample]))
# print('\nindices:\n', sample)

# 随机采样
def data_iter_random(chars_indices, batch_size, num_steps, ctx=None):
    num_examples = (len(chars_indices) - 1) // num_steps  # 一共有多少个时间步
    epoch_size = num_examples // batch_size  # 一共循环多少次
    example_indices = list(range(num_examples))  # 时间布列表
    random.shuffle(example_indices)  # 打乱

    def _data(pos):
        return chars_indices[pos:pos + num_steps]

    for i in range(epoch_size):
        # 每次读取batch_size个随机样本
        i = i * batch_size
        batch_indices = example_indices[i:i + batch_size]
        X = nd.array([_data(j * num_steps) for j in batch_indices], ctx=ctx)
        Y = nd.array([_data(j * num_steps + 1) for j in batch_indices], ctx=ctx)
        yield X, Y

my_seq = list(range(30))
for X, Y in data_iter_random(my_seq, batch_size=2, num_steps=3):
    print('X:', X, '\nY:', Y, '\n')

# 顺序采样
def data_iter_consecutive(chars_indices, batch_size, num_steps, ctx=None):
    chars_indices = nd.array(chars_indices, ctx=None)
    data_len = len(chars_indices)
    batch_len = data_len // batch_size
    indices = chars_indices[0:data_len].reshape((batch_size, batch_len))
    epoch_size = (batch_len - 1) // num_steps
    for i in range(epoch_size):
        i = i * num_steps
        X = indices[:, i:i + num_steps]
        Y = indices[:, i + 1:i + num_steps + 1]
        yield X, Y

for X, Y in data_iter_consecutive(my_seq, batch_size=2, num_steps=3):
    print('X:', X, '\nY:', Y)

def to_onehot(X, size):
    return [nd.one_hot(x, size) for x in X.T]

get_inputs = to_onehot
inputs = get_inputs(X, vocab_size)
print(len(inputs), inputs[0].shape)

# 初始化模型
ctx = gb.try_gpu()
print('will use ', ctx)

num_inputs = vocab_size
num_hiddens = 256  # 隐藏单元个数
num_outputs = vocab_size

def get_params():
    # 隐藏层参数
    W_xh = nd.random.normal(scale=0.01, shape=(num_inputs, num_hiddens), ctx=ctx)
    W_hh = nd.random.normal(scale=0.01, shape=(num_hiddens, num_hiddens), ctx=ctx)
    b_h = nd.zeros(num_hiddens, ctx=ctx)
    # 输出层参数
    W_hy = nd.random.normal(scale=0.01, shape=(num_hiddens, num_outputs), ctx=ctx)
    b_y = nd.zeros(num_outputs, ctx=ctx)
    params = [W_xh, W_hh, b_h, W_hy, b_y]
    for param in params:
        param.attach_grad()
    return params

def rnn(inputs, state, *params):
    H = state
    W_xh, W_hh, b_h, W_hy, b_y = params
    outputs = []
    for X in inputs:
        H = nd.tanh(nd.dot(X, W_xh) + nd.dot(H, W_hh) + b_h)
        Y = nd.dot(H, W_hy) + b_y
        outputs.append(Y)
    return outputs, H

# 做个简单的测试
state = nd.zeros(shape=(X.shape[0], num_hiddens), ctx=ctx)
params = get_params()
outputs, state_new = rnn(get_inputs(X.as_in_context(ctx), vocab_size), state, *params)
print(len(outputs), outputs[0].shape, state_new.shape)

# 定义预测函数,预测基于前缀prefix接下来的num_chars个字符，用它根据训练得到的循环神经网络rnn来创作歌词
def predict_rnn(rnn, prefix, num_chars, params, num_hiddens, vocab_size, ctx, index_to_char, char_to_index, get_inputs,
                is_lstm=False):
    prefix = prefix.lower()  # 前缀字符，可以是单个字符，也可以是词语字符集
    state_h = nd.zeros(shape=(1, num_hiddens), ctx=ctx)
    if is_lstm:
        # 当rnn使用lstm时会用到
        state_c = nd.zeros(shape=(1, num_hiddens), ctx=ctx)
    outputs = [char_to_index[prefix[0]]]
    for i in range(num_chars + len(prefix)):
        X = nd.array([outputs[-1]], ctx=ctx)
        # 在序列中循环迭代隐藏状态。
        if is_lstm:
            # 当 RNN 使用 LSTM 时才会用到（后面章节会介绍），本节可以忽略。
            Y, state_h, state_c = rnn(get_inputs(X, vocab_size), state_h,
                                      state_c, *params)
        else:
            Y, state_h = rnn(get_inputs(X, vocab_size), state_h, *params)
        if i < len(prefix) - 1:
            next_input = char_to_index[prefix[i + 1]]
        else:
            next_input = int(Y[0].argmax(axis=1).asscalar())
        outputs.append(next_input)
    return ''.join([index_to_char[i] for i in outputs])

# 裁剪梯度

def grad_clipping(params, state_h, Y, theta, ctx):
    if theta is not None:
        norm = nd.array([0.0], ctx)
        for param in params:
            norm += (param.grad ** 2).sum()
        norm = norm.sqrt().asscalar()
        if norm > theta:
            for param in params:
                param.grad[:] *= theta / norm

# 定义模型训练函数
def train_and_predict_rnn(rnn, is_random_iter, num_epochs, num_steps,
                          num_hiddens, lr, clipping_theta, batch_size,
                          vocab_size, pred_period, pred_len, prefixes,
                          get_params, get_inputs, ctx, corpus_indices,
                          idx_to_char, char_to_idx, is_lstm=False):
    if is_random_iter:
        data_iter = data_iter_random
    else:
        data_iter = data_iter_consecutive
    params = get_params()
    loss = gloss.SoftmaxCrossEntropyLoss()

    for epoch in range(1, num_epochs + 1):
        # 如使用相邻采样，隐藏变量只需在该 epoch 开始时初始化。
        if not is_random_iter:
            state_h = nd.zeros(shape=(batch_size, num_hiddens), ctx=ctx)
            if is_lstm:
                state_c = nd.zeros(shape=(batch_size, num_hiddens), ctx=ctx)
        train_l_sum = nd.array([0], ctx=ctx)
        train_l_cnt = 0
        for X, Y in data_iter(corpus_indices, batch_size, num_steps, ctx):
            # 如使用随机采样，读取每个随机小批量前都需要初始化隐藏变量。
            if is_random_iter:
                state_h = nd.zeros(shape=(batch_size, num_hiddens), ctx=ctx)
                if is_lstm:
                    state_c = nd.zeros(shape=(batch_size, num_hiddens),
                                       ctx=ctx)
            # 如使用相邻采样，需要使用 detach 函数从计算图分离隐藏状态变量。
            else:
                state_h = state_h.detach()
                if is_lstm:
                    state_c = state_c.detach()
            with autograd.record():
                # outputs 形状：(batch_size, vocab_size)。
                if is_lstm:
                    outputs, state_h, state_c = rnn(
                        get_inputs(X, vocab_size), state_h, state_c, *params)
                else:
                    outputs, state_h = rnn(
                        get_inputs(X, vocab_size), state_h, *params)
                # 设 t_ib_j 为时间步 i 批量中的元素 j：
                # y 形状：（batch_size * num_steps,）
                # y = [t_0b_0, t_0b_1, ..., t_1b_0, t_1b_1, ..., ]。
                y = Y.T.reshape((-1,))
                # 拼接 outputs，形状：(batch_size * num_steps, vocab_size)。
                outputs = nd.concat(*outputs, dim=0)
                l = loss(outputs, y)
            l.backward()
            # 裁剪梯度。
            grad_clipping(params, state_h, Y, clipping_theta, ctx)
            gb.sgd(params, lr, 1)
            train_l_sum = train_l_sum + l.sum()
            train_l_cnt += l.size
        if epoch % pred_period == 0:
            print("\nepoch %d, perplexity %f"
                  % (epoch, (train_l_sum / train_l_cnt).exp().asscalar()))
            for prefix in prefixes:
                print(' - ', predict_rnn(
                    rnn, prefix, pred_len, params, num_hiddens, vocab_size,
                    ctx, idx_to_char, char_to_idx, get_inputs, is_lstm))

num_epochs = 200
num_steps = 35
batch_size = 32
lr = 0.2
clipping_theta = 5
prefixes = ['分开', '不分开']
pred_period = 40
pred_len = 100

train_and_predict_rnn(rnn, True, num_epochs, num_steps, num_hiddens, lr,
                      clipping_theta, batch_size, vocab_size, pred_period,
                      pred_len, prefixes, get_params, get_inputs, ctx,
                      chars_indices, index_to_char, char_to_index)


import json
import uvicorn
from fastapi import FastAPI
import logging
from logging.config import dictConfig
from pydantic import BaseModel
import pandas as pd
import pickle
from sklearn.neighbors import NearestNeighbors
from sentence_transformers import SentenceTransformer
import math

class LogConfig(BaseModel):
    """Logging configuration to be set for the server"""

    LOGGER_NAME: str = "api"
    LOG_FORMAT: str = "%(levelprefix)s | %(asctime)s | %(message)s"
    LOG_LEVEL: str = "DEBUG"

    # Logging config
    version = 1
    disable_existing_loggers = False
    formatters = {
        "default": {
            "()": "uvicorn.logging.DefaultFormatter",
            "fmt": LOG_FORMAT,
            "datefmt": "%Y-%m-%d %H:%M:%S",
        },
    }
    handlers = {
        "default": {
            "formatter": "default",
            "class": "logging.StreamHandler",
            "stream": "ext://sys.stderr",
        },
    }
    loggers = {
        LOGGER_NAME: {"handlers": ["default"], "level": LOG_LEVEL},
    }

dictConfig(LogConfig().dict())
logger = logging.getLogger("api")

app = FastAPI()
df = pd.read_csv(open("movie_data.csv"))
embeddings = pickle.load(open("movie_embeddings_simple.pickle", "rb"))
nbrs = NearestNeighbors(n_neighbors=20, algorithm="ball_tree").fit(embeddings)
embedder = SentenceTransformer('all-mpnet-base-v2')

def movie_series_to_dict(series: pd.Series) -> dict:
    out = series.to_dict()
    out = {k: None if not isinstance(v, str) and math.isnan(v) else v for k, v in out.items()}
    for k in ["genres", "keywords", "countries"]:
        out[k] = json.loads(out[k])
    return out

@app.get("/")
async def root(sentence: str):
    sentence_embedding = embedder.encode(sentence, convert_to_tensor=True)
    dists, indices = nbrs.kneighbors(sentence_embedding.unsqueeze(0))
    movie_output = [
        movie_series_to_dict(df.loc[i])
        for _, i in zip(dists[0], indices[0])
    ]

    return movie_output

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)


import harmonic_oscillator as ho
import matplotlib.pyplot as plt

ho1 = ho.HarmonicOscillator(18,8,3,7)
ho1_period = ho1.period_num(0.001)
print("Period je {} sekundi".format(ho1_period))
ho2 = ho.HarmonicOscillator(7,11,5,-4)

xlist = []
ylist = []
dt = 0.2
k = 1.1
n = 60

for i in range(n):
    num = ho2.period_num(dt)
    analitic = ho2.period_analitic()
    rel_error = (abs(num - analitic)/analitic)*100
    xlist.append(dt)
    ylist.append(rel_error)
    dt = dt/k

plt.plot(xlist,ylist)
plt.xlabel("dt [s]")
plt.ylabel("error [%]")
plt.xscale("log")
plt.show()


#10.6,#10.7
"""
while True:
    num1 = input("Please enter the first number: ")
    if num1 == 'q':
        break
    num2 = input("Please enter the second number: ")
    if num2 == 'q':
        break
    try:
        answer = int(num1) + int(num2)
    except ValueError:
        print(" Please enter integers!")
    else:
        print(answer)

"""
#10.8
"""
def read_files(filenames):
    try:
        with open(filenames,'r') as file_object:
            contents = file_object.readlines()
    except FileNotFoundError:
        pass
            #print(" Sorry the file is not found!")
    else:
        print(contents)

filenames =["C:\\Users\Vani\Desktop\dogs.txt"]
#for filename in filenames:
    #print(filename)
read_files(str("C:\\Users\Vani\Desktop\Cas.txt"))
"""

#10.10

def count_words(filename):
    try:
        with open(filename,'r')as file_object:
            contents = file_object.read().rstrip()
            print(contents)
    except FileNotFoundError:
        print("Sorry File os not foumd!")
    else:
         contents = str(contents.count('dog'))
         #print(str(content.count('dog')))
        #print(contents)
         print("The word dog appears " + str(contents) + " times")

filenames =["C:\\Users\Vani\Desktop\dgs.txt"]
#for filename in filenames:
    #print(filename)
count_words(str("C:\\Users\Vani\Desktop\cats.txt"))


from PIL import Image, ImageDraw
import sys
s = sys.argv[1]

img = Image.open(s)
width, height = img.size
white = (255, 255, 255)
image = Image.new("RGB", (width, height), white)
draw = ImageDraw.Draw(image)

for y in range(height):
    for x in range(width):
        rgb = img.getpixel((x,y))
        RGB = []

        for i in range(len(rgb)):
        		RGB.append(int(rgb[i] / 2) )

        color = (RGB[0],RGB[1],RGB[2])
        draw.point((x,y), color)

image.save("Q2.png")


from os import sched_get_priority_min
from subprocess import call
import re
import matplotlib.pyplot as plt
import numpy as np

from timeit import default_timer as timer

def main():
    X = [28, 14, 7]
    Y = [28, 14, 7]
    Z = []
    T = []

    # try out all combinations of width/height in [28, 14, 7]
    for width in X:
        currZ = []
        currT = []
        for height in Y:
            start = timer()
            # run emd.py and collect results
            call(["python3", "./emd.py",
                  "-d", "../originalSpace/verySmallData",
                  "-q", "../originalSpace/tinyData",
                  "-l1", "../originalSpace/verySmallLabels",
                  "-l2", "../originalSpace/tinyLabels",
                  "-o", "./temp",
                  "-width", str(width),
                  "-height", str(height)])
            end = timer()

            # collect output from file and store into directory
            outputFile = open("./temp", "r")
            lines = outputFile.readlines()
            # get results
            emdString = lines[0][re.search(r"\d", lines[0]).start():]
            manhattanString = lines[1][re.search(r"\d", lines[1]).start():]

            # since manhattan is consistent, keep the ratio instead
            emdCorrectness = float(emdString)
            manhattanCorrectness = float(manhattanString)
            emdToManhattanRatio = emdCorrectness / manhattanCorrectness

            currZ.append(emdToManhattanRatio)
            currT.append(end - start)

        Z.append(currZ)
        T.append(currT)

    # make x and y appropriate
    meshX, meshY = np.meshgrid(X, Y)

    # now that we have all the data, plot ratio of correctness
    ax = plt.axes(projection='3d')
    ax.plot_surface(meshX, meshY, np.array(Z), rstride=1, cstride=1,
                    cmap='viridis', edgecolor='none')
    ax.set_title('EMD / Manhattan correctness')
    ax.set_xlabel('width')
    ax.set_ylabel('height')
    plt.show()

    # also plot time
    ax = plt.axes(projection='3d')
    ax.plot_surface(meshX, meshY, np.array(T), rstride=1, cstride=1,
                    cmap='viridis', edgecolor='none')
    ax.set_title('Runtime')
    ax.set_xlabel('width')
    ax.set_ylabel('height')
    plt.show()

if __name__ == "__main__":
    main()


from PyQt5 import QtCore, QtWidgets

import util
from fa import maps
from notifications.ns_dialog import NotificationDialog
from notifications.ns_settings import NsSettingsDialog, IngameNotification

"""
The Notification Systems reacts on events and displays a popup.
Each event_type has a NsHook to customize it.
"""

class Notifications:
    USER_ONLINE = 'user_online'
    NEW_GAME = 'new_game'

    def __init__(self, client, gameset):
        self.client = client

        self.settings = NsSettingsDialog(self.client)
        self.dialog = NotificationDialog(self.client, self.settings)
        self.events = []
        self.disabledStartup = True
        self.game_running = False

        client.gameEnter.connect(self.gameEnter)
        client.gameExit.connect(self.gameExit)

        gameset.newLobby.connect(self._newLobby)

        self.user = util.THEME.icon("client/user.png", pix=True)

    def _newLobby(self, game):
        self.on_event(self.NEW_GAME, game.to_dict())

    def gameEnter(self):
        self.game_running = True

    def gameExit(self):
        self.game_running = False
        # kick the queue
        if self.settings.ingame_notifications == IngameNotification.QUEUE:
            self.checkEvent()

    def isDisabled(self):
        return (
            self.disabledStartup
            or self.game_running and self.settings.ingame_notifications == IngameNotification.DISABLE
            or not self.settings.enabled
        )

    def setNotificationEnabled(self, enabled):
        self.settings.enabled = enabled
        self.settings.saveSettings()

    @QtCore.pyqtSlot()
    def on_event(self, eventType, data):
        """
        Puts an event in a queue, can trigger a popup.
        Keyword arguments:
        eventType -- Type of the event
        data -- Custom data that is used by the system to show a detailed popup
        """
        if self.isDisabled() or not self.settings.popupEnabled(eventType):
            return

        doAdd = False

        if eventType == self.USER_ONLINE:
            userid = data['user']
            if self.settings.getCustomSetting(eventType, 'mode') == 'all' or self.client.me.isFriend(userid):
                doAdd = True
        elif eventType == self.NEW_GAME:
            if self.settings.getCustomSetting(eventType, 'mode') == 'all' or ('host' in data and self.client.me.isFriend(data['host'])):
                doAdd = True

        if doAdd:
            self.events.append((eventType, data))

        self.checkEvent()

    @QtCore.pyqtSlot()
    def on_showSettings(self):
        """ Shows a Settings Dialg with all registered notifications modules  """
        self.settings.show()

    def showEvent(self):
        """
        Display the next event in the queue as popup

        Pops event from queue and checks if it is showable as per settings
        If event is showable, process event data and then feed it into notification dialog

        Returns True if showable event found, False otherwise
        """

        event = self.events.pop(0)

        eventType = event[0]
        data = event[1]
        pixmap = None
        text = str(data)
        if eventType == self.USER_ONLINE:
            userid = data['user']
            pixmap = self.user
            text = '%s
joined %s' % \
                   (self.client.players[userid].login, data['channel'])
        elif eventType == self.NEW_GAME:

            preview = maps.preview(data['mapname'], pixmap=True)
            if preview:
                pixmap = preview.scaled(80, 80)

            # TODO: outsource as function?
            mod = data.get('featured_mod')
            mods = data.get('sim_mods')

            modstr = ''
            if mod != 'faf' or mods:
                modstr = mod
                if mods:
                    if mod == 'faf':
                        modstr = ", ".join(list(mods.values()))
                    else:
                        modstr = mod + " & " + ", ".join(list(mods.values()))
                    if len(modstr) > 20:
                        modstr = modstr[:15] + "..."

            modhtml = '' if (modstr == '') else '
mods %s' % modstr
            text = '%s
on %s%s' % \
                   (data['title'], maps.getDisplayName(data['mapname']), modhtml)

        self.dialog.newEvent(pixmap, text, self.settings.popup_lifetime, self.settings.soundEnabled(eventType))

    def checkEvent(self):
        """
        Checks that we are in correct state to show next notification popup

        This means:
            * There need to be events pending
            * There must be no notification showing right now (i.e. notification dialog hidden)
            * Game isn't running, or ingame notifications are enabled

        """
        if (len(self.events) > 0 and self.dialog.isHidden() and
                (not self.game_running or self.settings.ingame_notifications == IngameNotification.ENABLE)):
            self.showEvent()


# -*- coding: utf-8 -*-
from behave import *
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time

@given('website "{url}"')
def open_page(context, url):
    context.browser.get(url)

@when("website is open")
def is_open(context):
    WebDriverWait(context.browser, 120)
    assert context.browser.title == "Google"

@then('enter the "{text}" into search box')
def enter_text(context, text):

    context.browser.find_element(By.ID, 'lst-ib').send_keys(text)
    time.sleep(3)
    context.browser.find_element(By.ID, "_fZl").click()


import numpy as np
import cv2.cv2 as cv
import matplotlib.pyplot as plt
from parametric_geometric_transformation import backward_transformation
from parametric_geometric_transformation import translation, rotation, euclidean, affine

# two-dimension Gaussian function
def func_gaussian_2d(x, y, sigma):
    return np.exp(-(x**2 + y**2)/(2*sigma**2)) / (2*np.pi*sigma**2)

# generate 2-dimension distance matrix
def generate_distance_matrix_2d(size):
    distance_matrix = []
    for i in range(size):
        distance_matrix.append([])
        for j in range(size):
            distance_matrix[i].append(((j - (size-1)/2), (((size-1)/2) - i)))
    return distance_matrix

# generate 2-dimension Gaussian filtering kernel
def generate_gaussian_kernel_2d(distance_matrix, size, sigma):
    gaussian_kernel = np.zeros((size, size))
    for i in range(size):
        for j in range(size):
            tmp = distance_matrix[i][j]
            gaussian_kernel[i][j] = func_gaussian_2d(tmp[0], tmp[1], sigma)
    total_value = np.sum(gaussian_kernel)
    gaussian_kernel = gaussian_kernel / total_value
    return gaussian_kernel

# 高斯滤波
def gaussian_filetring(src, kernel_size, gaussian_kernel):
    padding_size = int((kernel_size-1)/2)
    h, w = src.shape
    dst = np.zeros((h+kernel_size-1, w+kernel_size-1), dtype=np.float64)
    dst[padding_size:h+padding_size, padding_size:w+padding_size] = src[:, :]
    res = np.zeros((h, w), dtype=np.float64)
    for y in range(h):
        for x in range(w):
            res[y, x] = np.sum(dst[y:y+kernel_size, x:x+kernel_size]*gaussian_kernel)
    return res

# 遍历移动函数
def moving(g, gg, j, k, m, n):
    """
    :param g: 待遍历的图
    :param gg: 遍历与否的记录图，0为已遍历，1为未遍历
    :param j: 遍历到的点的行坐标
    :param k: 遍历到的点的列坐标
    :param m: 图的行数
    :param n:  图的列数
    :return: 当前点所在连通域的所有点
    """
    q_row = [j]  # 遍历到的点的行坐标
    q_col = [k]  # 遍历到的点的列坐标
    site_list = []
    while len(q_row) != 0:
        j = q_row[0]; k = q_col[0]
        site_list.append((j, k))
        gg[j, k] = 0
        if k-1 >= 0:
            if g[j, k-1] != 0 and gg[j, k-1] == 1:
                q_row.append(j); q_col.append(k-1); gg[j, k-1] = 0
        if k+1 < n:
            if g[j, k+1] != 0 and gg[j, k+1] == 1:
                q_row.append(j); q_col.append(k+1); gg[j, k+1] = 0
        if j-1 >= 0:
            if g[j-1, k] != 0 and gg[j-1, k] == 1:
                q_row.append(j-1); q_col.append(k); gg[j-1, k] = 0
        if j+1 < m:
            if g[j+1, k] != 0 and gg[j+1, k] == 1:
                q_row.append(j+1); q_col.append(k); gg[j+1, k] = 0
        del q_row[0]
        del q_col[0]
    return site_list

# get image
img = cv.imread('C:/Users/Admin/Desktop/test.png', cv.IMREAD_GRAYSCALE)
h, w = img.shape
img1 = np.zeros((h, w), dtype=np.uint8)
img1[:, :] = img[:, :]

# translation_matrix = euclidean(60, 100, 200)
# img2 = backward_transformation(img, translation_matrix, 1)

# set parameters
sigma = 0.6
kernel_size = 3

# 生成高斯滤波核
distance_matrix = generate_distance_matrix_2d(kernel_size)
g_kernel= generate_gaussian_kernel_2d(distance_matrix, kernel_size, sigma)
# 高斯滤波
res1 = gaussian_filetring(img1, kernel_size, g_kernel)
res = res1.astype(np.uint8)

t1 = cv.getTickCount()

# 梯度计算
grad_x = cv.Sobel(res, cv.CV_64F, 1, 0)  # 计算水平梯度，Sobel算子：[[-1 0 1],[-2 0 2],[-1 0 1]]
grad_y = cv.Sobel(res, cv.CV_64F, 0, 1)  # 计算垂直梯度，Sobel算子：[[-1 -2 -1],[0 0 0],[1 2 1]]
grad_xx = grad_x * grad_x
grad_xy = grad_x * grad_y
grad_yy = grad_y * grad_y
# 对二阶差分梯度矩阵做高斯滤波
gradient_xx = gaussian_filetring(grad_xx, kernel_size, g_kernel)
gradient_xy = gaussian_filetring(grad_xy, kernel_size, g_kernel)
gradient_yy = gaussian_filetring(grad_yy, kernel_size, g_kernel)

# 构造二阶矩矩阵M
alpha = 0.05
win_size = 5  # 移动窗口大小
h, w = img.shape
win_func = np.ones((win_size, win_size), dtype=np.float64)  # 窗口函数
M = np.zeros((h, w, 3), dtype=np.float64)  # [Ixx, Ixy, Iyy]
det_M = np.zeros((h, w), dtype=np.float64)  # 矩阵M的行列式
trace_M = np.zeros((h, w), dtype=np.float64)  # 矩阵M的迹
R = np.zeros((h, w), dtype=np.float64)
s = int((win_size-1)/2)
mark_points = np.zeros((h, w))  # 标记角点
for y in range(s, h-s):
    for x in range(s, w-s):
        M[y, x, 0] = np.sum(win_func*gradient_xx[y-s:y+s+1, x-s:x+s+1])
        M[y, x, 1] = np.sum(win_func*gradient_xy[y-s:y+s+1, x-s:x+s+1])
        M[y, x, 2] = np.sum(win_func*gradient_yy[y-s:y+s+1, x-s:x+s+1])
        det_M[y, x] = M[y, x, 0]*M[y, x, 2] - M[y, x, 1]*M[y, x, 1]
        trace_M[y, x] = M[y, x, 0] + M[y, x, 2]
        R[y, x] = det_M[y, x] - alpha*trace_M[y, x]*trace_M[y, x]

# 设置正阈值分割角点
T = np.max(R) * 0.01  # 正阈值
for y1 in range(h):
    for x1 in range(w):
        if R[y1, x1] > T:
            mark_points[y1, x1] = 1

# 非极大值抑制
record_map = np.ones((h, w))  # 遍历记录图
mark_list = []  # 某一连通域的标记点集合
value_list = []  # 对应连通域的标记点值的集合
for m in range(h):
    for n in range(w):
        print((m, n))
        if record_map[m, n] == 0:
            continue
        elif mark_points[m, n] == 0:
            record_map[m, n] = 0
        else:
            p = m
            q = n
            mark_list = []
            value_list = []
            mark_list = moving(mark_points, record_map, m, n, h, w)  # 步进到连通域中
            for z1 in range(len(mark_list)):
                value_list.append(R[mark_list[z1][0], mark_list[z1][1]])
            max_value = max(value_list)
            for z2 in range(len(mark_list)):
                # 选取连通域中值最大的点为角点
                if R[mark_list[z2][0], mark_list[z2][1]] != max_value:
                    mark_points[mark_list[z2][0], mark_list[z2][1]] = 0
            m = p
            n = q

# 在原图中标记角点
img = cv.cvtColor(img, cv.COLOR_GRAY2RGB)
for y2 in range(h):
    for x2 in range(w):
        if mark_points[y2, x2] == 1:
            img[y2, x2] = [0, 0, 255]

# t1 = cv.getTickCount()

# opencv cornerHarris
res = res.astype(np.float32)
res1 = cv.cornerHarris(res, 3, 3, k=0.04)
res1 = cv.dilate(res1, None)
img = cv.cvtColor(img, cv.COLOR_GRAY2RGB)
img[res1 > 0.01*res1.max()] = [0, 0, 255]

# t2 = cv.getTickCount()
# print((t2-t1)/cv.getTickFrequency())

# # 生成高斯滤波核
# distance_matrix = generate_distance_matrix_2d(kernel_size)
# g_kernel= generate_gaussian_kernel_2d(distance_matrix, kernel_size, sigma)
# # 高斯滤波
# res2 = gaussian_filetring(img2, kernel_size, g_kernel)
# res2 = res2.astype(np.uint8)
#
# # 梯度计算
# grad_x2 = cv.Sobel(res2, cv.CV_64F, 1, 0)  # 计算水平梯度，Sobel算子：[[-1 0 1],[-2 0 2],[-1 0 1]]
# grad_y2 = cv.Sobel(res2, cv.CV_64F, 0, 1)  # 计算垂直梯度，Sobel算子：[[-1 -2 -1],[0 0 0],[1 2 1]]
# grad_xx2 = grad_x2 * grad_x2
# grad_xy2 = grad_x2 * grad_y2
# grad_yy2 = grad_y2 * grad_y2
#
# # 对二阶差分梯度矩阵做高斯滤波
# gradient_xx2 = gaussian_filetring(grad_xx2, kernel_size, g_kernel)
# gradient_xy2 = gaussian_filetring(grad_xy2, kernel_size, g_kernel)
# gradient_yy2 = gaussian_filetring(grad_yy2, kernel_size, g_kernel)
#
# # 构造二阶矩矩阵M
# alpha = 0.05
# win_size = 3  # 移动窗口大小
# h, w = img2.shape
# win_func = np.ones((win_size, win_size), dtype=np.float64)  # 窗口函数
# M2 = np.zeros((h, w, 3), dtype=np.float64)  # [Ixx, Ixy, Iyy]
# det_M2 = np.zeros((h, w), dtype=np.float64)  # 矩阵M的行列式
# trace_M2 = np.zeros((h, w), dtype=np.float64)  # 矩阵M的迹
# R2 = np.zeros((h, w), dtype=np.float64)
# s = int((win_size-1)/2)
# mark_points2 = np.zeros((h, w))  # 标记角点
# for y in range(s, h-s):
#     for x in range(s, w-s):
#         M2[y, x, 0] = np.sum(win_func*gradient_xx2[y-s:y+s+1, x-s:x+s+1])
#         M2[y, x, 1] = np.sum(win_func*gradient_xy2[y-s:y+s+1, x-s:x+s+1])
#         M2[y, x, 2] = np.sum(win_func*gradient_yy2[y-s:y+s+1, x-s:x+s+1])
#         det_M2[y, x] = M2[y, x, 0]*M2[y, x, 2] - M2[y, x, 1]*M2[y, x, 1]
#         trace_M2[y, x] = M2[y, x, 0] + M2[y, x, 2]
#         R2[y, x] = det_M2[y, x] - alpha*trace_M2[y, x]*trace_M2[y, x]
#
# # 设置正阈值分割角点
# T = np.max(R2) * 0.01  # 正阈值
# mark_list = []
# img2 = cv.cvtColor(img2, cv.COLOR_GRAY2RGB)
# for y1 in range(h):
#     for x1 in range(w):
#         if R2[y1, x1] > T:
#             mark_points2[y1, x1] = 1
#             img2[y1, x1] = [0, 0, 255]

img1 = cv.cvtColor(img1, cv.COLOR_BGR2RGB)
# img2 = cv.cvtColor(img2, cv.COLOR_BGR2RGB)
plt.subplot(121), plt.imshow(img1)
plt.xticks([]), plt.yticks([])
plt.title('source')
# plt.subplot(122), plt.imshow(img2)
# plt.xticks([]), plt.yticks([])
# plt.title('euclidean transformation')
plt.show()


#!/usr/bin/env python3
# -- coding: utf-8 --

import os
import random
import copy
import csv
import numpy as np
import time
import matplotlib.pyplot as plt
from collections import Counter
import sys

def readFile(fname):
    file = open("20newsgroups/" + fname , 'r');
    for line in file:
        temp = line.split(" ")
    return temp

def readDirectory(directory):
    data = []
    files = []
    for file in os.listdir(directory):
        if file != "index.csv" :
            data.append(readFile(file))
            files.append(file)
    return data, files

def preprocess(data):
    words  = []
    for doc in data:
        for word in doc:
            words.append(word)
    cntWords = Counter(words)
    uniqueWords = []
    for uniqueWord in cntWords:
        uniqueWords.append(uniqueWord)

    return words ,uniqueWords

def createCSV(lists):
    with open('topicwords.csv', 'w', newline='') as file:
     write = csv.writer(file)
     for i in range(K):
         write.writerow([i ,lists[i]])

def optimization(w,r,d,phiTrain):
    phiT = phiTrain.T
    a = 0.01
    I = np.eye(len(phiTrain[0]), dtype = float)
    x = phiT.dot(r)
    x = -x.dot(phiTrain) - I.dot(a)
    H = np.linalg.inv(x)
    g = phiT.dot(d) - w.dot(a)
    Wn = w - H.dot(g)
    return Wn

def LogisticRegression(phiTrain,phiTarget):
    wList = []
    position = []
    timee = []
    w = [0] * int(np.shape(phiTrain[0])[1])
    w = np.array(w)
    for i in range(len(phiTrain)):
        start = time.time()
        for j in range(500):
            phi = phiTrain[i]
            phi = np.array(phi)
            x = phi.dot(w)
            y = 1/(1 + np.exp(-x))
            t = phiTarget[i]
            d = t - y
            r = y.dot(1-y)
            Wn = optimization(w,r,d,phi)
            temp = w
            w = Wn
            wCon = np.sqrt(np.sum(np.square(w - temp)))/np.sqrt(np.sum(np.square(temp)))
            if wCon <= 0.001 or j == 499:
                position.append(j)
                end = time.time()
                duration = end - start
                timee.append(duration)
                break
        wList.append(w)
    return wList, position, timee

def generateData(phiTrainData):
    PhiTrainData = []
    PhiTargetData = []
    phiTrain = phiTrainData[:,:-1]
    phiTarget = phiTrainData[:,-1]
    for i in np.arange(0.1,1.1,0.1):
        PhiTrainData.append(phiTrain[:int(len(phiTrain)*i)])
        PhiTargetData.append(phiTarget[:int(len(phiTarget)*i)])

    return PhiTrainData, PhiTargetData

def calMeanStd(listt):
    mean = np.mean(np.array(listt),axis=0)
    std = np.std(np.array(listt),axis=0)

    return mean,std

def LogisticRegressionTest(phiTestData, wList):
    error = []
    phiTest = phiTestData[:,:-1]
    phiTestLabel = phiTestData[:,-1]
    for i in range(len(wList)):
        err = 0
        x = phiTest.dot(wList[i])
        y = 1/(1+np.exp(-x))
        yFinal = []
        for j in range(len(y)):
            if y[j] >= 0.5:
                yFinal.append(1)
            else:
                yFinal.append(0)
        for k in range(len(y)):
            if yFinal[k] != phiTestLabel[k]:
                err = err + 1
        error.append(1 - (err/len(phiTestLabel)))
    return error

def logistic(CD ,labels, alpha, method):
    data = []
    if method == 1:
        for i in range(D):
            temp = [0] * K
            data.append(temp)
        for i in range(D):
            for j in range(K):
                num = CD[i][j] + alpha
                den = K*alpha + sum(CD[i])
                data[i][j] = num/den
    else :
        data = CD
    data = np.array(data)
    labels = np.array(labels).reshape(len(labels),1)
    errorList = []
    for i in range(30):
        index = np.random.permutation(len(labels))
        data, labels =  data[index], labels[index]
        data1 = np.concatenate((data, labels), axis = 1)
        phiTrainData = data1[:int(len(data1)*(0.67))]
        phiTestData = data1[int(len(data)*(0.67)):]

        PhiTrainData, PhiTargetData = generateData(phiTrainData)
        wList, position, timee  = LogisticRegression(PhiTrainData, PhiTargetData)
        error = LogisticRegressionTest(phiTestData, wList)
        errorList.append(error)

    mean, std = calMeanStd(errorList)
    return mean, std

def calCT(K, V, words, uniqueWords, tags):
    ct = []
    for i in range(K):
        temp = [0] * V
        ct.append(temp)

    for i in range(len(words)):
        for j in range(V):
            if words[i] == uniqueWords[j]:
                for k in range(len(tags)):
                    if topicList[i] == tags[k]:
                        ct[k][j] += 1
                        break
    return ct

def calCD(D, K, topics, tags):
    cd = []
    for i in range(D):
        temp = [0] * K
        cd.append(temp)

    for i in range(len(topics)):
        for j in range(len(topics[i])):
            for k in range(len(tags)):
                if topics[i][j] == tags[k]:
                    cd[i][k] += 1
                    break
    return cd

def gibbsSampling(CD, CT, words, uniqueWords, pi, K, doc, topicList):
    iterations = 500
    beta = 0.01
    alpha = 5/K
    prob = [0] * K

    for i in range(iterations):
        #print(i)
        for j in range(len(words)):
            index = pi[j]
            currWord = words[index]
            currWordIndex = uniqueWords.index(currWord)
            document = doc[pi[j]]
            topic = topicList[pi[j]]
            CD[document][topic] -= 1
            CT[topic][currWordIndex] -= 1

            for k in range(K):
                numerator = (CT[k][currWordIndex] + beta) * (CD[document][k] + alpha)
                temp1 = sum(CT[k])
                temp2 = sum(CD[document])
                denominator = (V*beta + temp1) * (K*alpha + temp2)
                prob[k] = numerator/denominator

            temp3 = sum(prob)
            prob = list(np.array(prob)/temp3)
            ran = random.uniform(0,1)
            temp4 = 0
            for x in range(len(prob)):
                temp4 = sum(prob[:x+1])
                if ran <= temp4 :
                    ind = x
                    break

            topic = ind
            topicList[pi[j]] = topic
            CD[document][topic] += 1
            CT[topic][currWordIndex] += 1

    return CD, CT, topicList

def alternateMethod(D, V, data, uniqueWords):
    altMethod = []
    for i in range(D):
        temp = [0] * V
        altMethod.append(temp)

    for i in range(len(data)):
        for j in range(len(data[i])):
            for k in range(len(uniqueWords)):
                if data[i][j] == uniqueWords[k]:
                    altMethod[i][k] += 1
                    break
    return altMethod

def toCSV(uniqueWords, CT):
    temp = copy.deepcopy(CT)
    lists = []
    for i in range(len(CT)):

        temp[i].sort(reverse = True)
        temp1 = temp[i]
        max1 = temp1[0]
        ind1 = CT[i].index(max1)
        tags1 = uniqueWords[ind1]
        max2 = temp1[1]
        ind2 = CT[i].index(max2)
        tags2 = uniqueWords[ind2]
        max3 = temp1[2]
        ind3 = CT[i].index(max3)
        tags3 = uniqueWords[ind3]
        max4 = temp1[3]
        ind4 = CT[i].index(max4)
        tags4 = uniqueWords[ind4]
        max5 = temp1[4]
        ind5 = CT[i].index(max5)
        tags5 = uniqueWords[ind5]
        temp2 = [tags1, tags2,tags3,tags4,tags5]
        lists.append(temp2)
    createCSV(lists)

def plotGraph(mean1, std1, method1, mean2, std2, method2):
    x = [i for i in np.arange(0.1,1.1,0.1)]
    plt.errorbar(x, mean1, std1, label = method1)
    plt.errorbar(x , mean2, std2, label = method2)
    plt.xlabel("DataSize")
    plt.ylabel("Accuracy")
    plt.legend(loc="lower right")
    plt.show()

directory = os.path.dirname(os.path.abspath(__file__))
data , files = readDirectory(directory+"/20newsgroups")
words , uniqueWords = preprocess(data)
V = len(uniqueWords)
D = 200
K = 20
pi = []
for i in range(len(words)):
    pi.append(i)
random.shuffle(pi)

doc = []
for i in range(len(data)):
    for j in range(len(data[i])):
        doc.append(i)

alpha = 5/K

topicList = []
topics = []
for i in range(len(data)):
    temp = []
    for j in range(len(data[i])):
        r = random.randint(0,19)
        topicList.append(r)
        temp.append(r)
    topics.append(temp)

tags = [i for i in range(0,20)]
CD = calCD(D, K, topics, tags)
CT = calCT(K, V, words, uniqueWords, tags)
CD, CT, topicList = gibbsSampling(CD, CT, words, uniqueWords, pi, K, doc, topicList)

toCSV(uniqueWords , CT)

label = np.genfromtxt(directory + "20newsgroups/index.csv", delimiter = ",")

labels = []
for i in range(len(files)):
    labels.append(label[int(files[i]) - 1][1])

mean1 , std1 = logistic(CD , labels, alpha, 1)

data2 = alternateMethod(D, V, data, uniqueWords)

mean2 , std2 = logistic(data2 , labels, alpha, 0)

method1 = "LDA"
method2= "Bag of Words"
plotGraph(mean1 , std1, method1, mean2, std2, method2)


"""
Vector Signal Generator LTE Functions
### Options: K55  LTE Rel  8: LTE 20MHz 64QAM
###          K84  LTE Rel  9: MBMS & MBSFN generation
###          K85  LTE Rel 10: CA; Enhanced SC-FDMA; UL MIMO
###          K112 LTE Rel 11: TDD Config9&7
###          K113 LTE Rel 12: DL 256QAM; TDD/FDD Mixed CA
###          K119 LTE Rel 13/14/15: UL 256QAM; LAA
#####################################################################
"""
from rssd.VSG.Common import VSG     #pylint: disable=E0611,E0401

class VSG(VSG):                     #pylint: disable=E0102
    """ Rohde & Schwarz Vector Signal Generator LTE Object """
    def __init__(self):
        super(VSG,self).__init__()    #Python2/3
        self.Model  = "SMW"
        self.ldir   = "UL"
        self.User   = 0
        self.cc     = 1

    #####################################################################
    ### VSG Query
    #####################################################################
    def Get_LTE_CC(self):
        rdStr = self.query(f':SOUR:BB:EUTR:{self.ldir}:CA:CELL0:STAT?')
        return rdStr

    def Get_LTE_ChBW(self):
        rdStr = self.query(f':SOUR:BB:EUTR:{self.ldir}:BW?')
        return rdStr

    def Get_LTE_Direction(self):
        rdStr = self.query(f':SOUR:BB:EUTR:LINK?')
        if rdStr == 'DOWN':
            self.ldir = "DL"
        elif rdStr == 'UP':
            self.ldir = "UL"
        else:
            print('Get_LTE_Direction Error')
        return self.ldir

    def Get_LTE_Duplex(self):
        rdStr = self.query(f':SOUR:BB:EUTR:DUPL?')
        return rdStr

    def Get_LTE_Modulation(self):
        if self.ldir == 'UL':
            rdStr = self.query(f':SOUR:BB:EUTR:{self.ldir}:CELL0:SUBF0:ALL0:CW1:PUSC:MOD?')     # UL Only
        else:
            rdStr = self.query(f':SOUR:BB:EUTR:{self.ldir}:DUMD:MOD?')
        return rdStr

    def Get_LTE_ResBlock(self):
        if self.ldir == 'UL':
            rdStr = self.query(f':SOUR:BB:EUTR:{self.ldir}:CELL0:SUBF0:ALL0:PUSC:SET1:RBC?')    # UL Only
        else:
            rdStr = self.query(f':SOUR:BB:EUTR:DL:SUBF0:ALL0:CW1:RBC?')                         # DL Only
        return rdStr

    def Get_LTE_ResBlockOffset(self):
        if self.ldir == 'UL':
            rdStr = self.query(f':SOUR:BB:EUTR:{self.ldir}:CELL0:SUBF0:ALL0:PUSC:SET1:VRB?')    # UL Only
        else:
            rdStr = self.query(f':SOUR:BB:EUTR:DL:SUBF0:ALL0:CW1:RBOF?')                        # DL Only
        return rdStr

    #####################################################################
    ### VSG Setting
    #####################################################################
    def Set_LTE_BBState(self,iEnable):
        if (iEnable == 1) or (iEnable == 'ON'):
            self.jav_OPC_Wait(':SOUR1:BB:EUTR:STAT 1')
    #         self.query('*OPC?')        # Wait for calculation
        else:
            self.write(':SOUR1:BB:EUTR:STAT 0')

    def Set_LTE_CC(self,iCC):
        # self.write(''%())
        if iCC > 0:
            self.write(f':SOUR:BB:EUTR:{self.ldir}:CA:STAT 1')

    def Set_LTE_ChBW(self,iChBW):
        self.write(f':SOUR:BB:EUTR:{self.ldir}:BW BW{iChBW:02d}_00')
        self.write(f':SOUR:BB:EUTR:{self.ldir}:CA:CELL{self.cc - 1}:BW BW{iChBW:02d}_00')

    def Set_LTE_Direction(self, sDir):
        """UL | DL | UP | DOWN """
        if (sDir == 'UL') or (sDir == 'UP'):
            self.write(f':SOUR:BB:EUTR:LINK UP')
            self.ldir = 'UL'
        elif (sDir == 'DL') or (sDir == 'DOWN'):
            self.write(f':SOUR:BB:EUTR:LINK DOWN')
            self.ldir = 'DL'
        else:
            print('Set_LTE_Direction Error.  Must be UL or DL')

    def Set_LTE_Duplex(self,sDuplex):
        #FDD TDD
        self.write(f':SOUR:BB:EUTR:DUPL {sDuplex}')

    def Set_LTE_Modulation(self, sMod):
        if self.ldir == 'UL':
            self.write(f':SOUR:BB:EUTR:{self.ldir}:CELL0:SUBF0:ALL0:CW1:PUSC:MOD {sMod}')
        else:
            self.write(f':SOUR:BB:EUTR:{self.ldir}:DUMD:MOD {sMod}')

    def Set_LTE_ResBlock(self, iRB):
        if self.ldir == 'UL':
            self.write(f':SOUR:BB:EUTR:{self.ldir}:CELL0:SUBF0:ALL0:PUSC:SET1:RBC {iRB}')
        else:
            pass

    def Set_LTE_ResBlockOffset(self,iRBO):
        if self.ldir == 'UL':
            self.write(f':SOUR:BB:EUTR:{self.ldir}:CELL0:SUBF0:ALL0:PUSC:SET1:VRB {iRBO}')
        else:
            pass


from urllib.request import urlopen
import json
import sys

out = ""
with open("token.txt", "r") as tokenin:
    token = tokenin.readline()
    with open("in.txt", "r") as filein:
        grades = filein.readlines()
        for line in grades:
            line_data = line.split(",")
            stud = json.loads(urlopen("https://ois2.ttu.ee/uusois/!uus_ois2.ois_public.get_json?j_code=" + token +
                                      "&p_where=tparam0%3D" + line_data[0] +
                                      "%26daparam0%3D%26dlparam0%3D%26wparam0%3Dtud_kood%252FS").read().decode("UTF-8"))
            name = stud[1]["andmed"][0]["t_nimi"]
            out += name + (27 - len(name))*" " + line_data[2]
        text_file = open("out.txt", "w")
        text_file.write(out)


# -*- coding: utf-8 -*-
# File: viz.py

import numpy as np

# import cv2
import matplotlib.pyplot as plt

label_colours = [[128, 64, 128], [244, 35, 231], [69, 69, 69]
                # 0 = road, 1 = sidewalk, 2 = building
                ,[102, 102, 156], [190, 153, 153], [153, 153, 153]
                # 3 = wall, 4 = fence, 5 = pole
                ,[250, 170, 29], [219, 219, 0], [106, 142, 35]
                # 6 = traffic light, 7 = traffic sign, 8 = vegetation
                ,[152, 250, 152], [69, 129, 180], [219, 19, 60]
                # 9 = terrain, 10 = sky, 11 = person
                ,[255, 0, 0], [0, 0, 142], [0, 0, 69]
                # 12 = rider, 13 = car, 14 = truck
                ,[0, 60, 100], [0, 79, 100], [0, 0, 230]
                # 15 = bus, 16 = train, 17 = motocycle
                ,[119, 10, 32]]
                # 18 = bicycle

def decode_labels(mask, img_shape, num_classes):
    color_table = label_colours

    color_mat = np.array(color_table, dtype=np.float32)
    mask = np.ravel(mask)
    pred = np.zeros((mask.size, 3))
    for ii, c in enumerate(color_table):
        midx = np.where(mask == ii)[0]
        pred[midx, :] = np.reshape(c, (1, 3))

    pred = np.reshape(pred, (img_shape[0], img_shape[1], 3))

    return pred

def draw_predictions(img, preds, num_classes):
    """
    """
    result = decode_labels(preds, preds.shape, num_classes)
    plt.imshow(result / 255.0)
    plt.show()


import socket
from threading import Thread
import time
class Server():

    def __init__(self,argIp : str,argPort : int):
        self.ip                         = argIp
        self.port                       = argPort
        self.address                    = (self.ip , self.port)
        self.receivingFlag              = True
        self.socketInstance             = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.receptionThread            = Thread(target= self.receive)
        self.receptionThread.setDaemon(True)
        self.clientAddress              = ()

    def bind(self):
        self.socketInstance.bind(self.address)

    def receive(self):
        print("started receiving")
        while self.receivingFlag:
            data,addr = self.socketInstance.recvfrom(1024)
            data      = data.decode()
            self.clientAddress  = addr
            print("received = ", data)
            time.sleep(0.005)

    def send(self,data):
        #check if there is already a message received from the client
        #in order to get it's address
        self.socketInstance.sendto(data.encode(), self.clientAddress)

    def startReception(self):
        self.receptionThread.start()

    def stopReception(self):
        print('Reception Stopped')
        self.receivingFlag = False
        self.receptionThread.join()
        self.socketInstance.close()


#! /usr/bin/env python
#Basic tools (system).
from __future__ import absolute_import
import sys, termios
#import atexit
import shutil
from select import select
from .util import *

#Copy a file from src to dst.  dst can not be a directory name.
#If a parent directory of dst does not exist, we create it.
#  src: source file name.
#  dst: destination path.
#  interactive: ask user if creating the parent dir.
#    If False, we create the parent dir without asking.
def CopyFile(src, dst, dst_file=None, interactive=True):
  #print '__file__:',__file__
  parent= os.path.dirname(dst)
  if parent!='' and not os.path.exists(parent):
    if interactive:
      CPrint(2,'CopyFile: Parent directory does not exist:',parent)
      CPrint(2,'Create? (if No, raise IOError)')
      if AskYesNo():  os.makedirs(parent)
      else:  raise IOError(2,'Can not open the file',dst)
    else:
      CPrint(2,'CopyFile: Creating parent directory:',parent)
      os.makedirs(parent)
  shutil.copy(src, dst)

#Detecting a keyboard hit without interrupting
#cf: http://code.activestate.com/recipes/572182-how-to-implement-kbhit-on-linux/
class TKBHit(object):
  def __init__(self,activate=True):
    self.is_curses_term= False
    self.termios= termios  #Ensure to use termios in __del__
    self.ask_timeout= 6000  #Timeout in Ask* functions.

    if activate:
      self.Activate()

  def __del__(self):
    self.Deactivate()

  def __enter__(self, *args, **kwargs):
    return self

  def __exit__(self, *args, **kwargs):
    self.Deactivate()

  def IsActive(self):
    return self.is_curses_term

  #Activate a new terminal for kbhit
  def Activate(self):
    if not hasattr(sys.stdin,'fileno'):  #Not a regular stdin.
      sys.stdin.is_curses_term= True
      self.is_curses_term= True
      return

    # save the terminal settings
    self.fd= sys.stdin.fileno()
    self.new_term= termios.tcgetattr(self.fd)
    self.old_term= termios.tcgetattr(self.fd)

    # new terminal setting unbuffered
    self.new_term[3]= (self.new_term[3] & ~termios.ICANON & ~termios.ECHO)

    #atexit.register(self.SetNormalTerm)
    self.SetCursesTerm()

  #Deactivate the terminal
  def Deactivate(self):
    self.SetNormalTerm()

  #Switch to normal terminal
  def SetNormalTerm(self):
    if not hasattr(sys.stdin,'fileno'):  #Not a regular stdin.
      sys.stdin.is_curses_term= False
      self.is_curses_term= False
      return

    if self.is_curses_term:
      self.termios.tcsetattr(self.fd, self.termios.TCSAFLUSH, self.old_term)
      del self.fd
      del self.new_term
      del self.old_term
      self.is_curses_term= False

  #Switch to unbuffered terminal
  def SetCursesTerm(self):
    if self.is_curses_term:
      self.SetNormalTerm()
    termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.new_term)
    self.is_curses_term= True

  #Print a character
  def PutCh(self,ch):
    sys.stdout.write(ch)

  #Get a character
  def GetCh(self):
    return sys.stdin.read(1)

  #Get a character with echoing it
  def GetChE(self):
    ch= self.GetCh()
    self.PutCh(ch)
    return ch

  #Check a keyboard hit
  def CheckKBHit(self, timeout=0):
    if not hasattr(sys.stdin,'fileno'):  #Not a regular stdin.
      return sys.stdin.wait_readable(timeout)
    dr,dw,de= select([sys.stdin], [], [], timeout)
    #print 'dr:',dr
    #print 'dw:',dw
    #print 'de:',de
    return dr != []

  #Get a keyboard hit
  def KBHit(self, echo=False, timeout=0):
    if self.CheckKBHit(timeout):
      if echo:  return self.GetChE()
      else:  return self.GetCh()
      ##Try to clear buffer: (it does not work)
      #while self.CheckKBHit(0):
        #c= self.GetCh()
        #sys.stdout.write('>> %s\n'%ch)
        #sys.stdout.flush()
    return None

  #KBHit compatible AskYesNo.
  def AskYesNo(self):
    if self.IsActive():
      while 1:
        sys.stdout.write('  (y|n) > ')
        sys.stdout.flush()
        if self.CheckKBHit(self.ask_timeout):
          ans= self.GetChE()
          sys.stdout.write('\n')
          if ans=='y' or ans=='Y':  return True
          elif ans=='n' or ans=='N':  return False
    else:
      return AskYesNo()

  #KBHit compatible AskGen.
  #Usage: AskGen('y','n','c')
  def AskGen(self,*argv):
    assert(len(argv)>0)
    if self.IsActive():
      while 1:
        sys.stdout.write('  (%s) > ' % '|'.join(argv))
        sys.stdout.flush()
        if self.CheckKBHit(self.ask_timeout):
          ans= self.GetChE()
          sys.stdout.write('\n')
          for a in argv:
            if ans==a:  return a
    else:
      return AskGen(*argv)

#Wait for a key input (immediately quit after a key input).
def KBHitOnce():
  sys.stdout.flush()
  with TKBHit() as kbhit:
    while True:
      if kbhit.IsActive():
        key= kbhit.KBHit()
        if key is not None:
          return key
      else:  break
      time.sleep(0.005)
  return None

#KBHit version of AskYesNo (does not wait for pressing return key).
def KBHAskYesNo():
  sys.stdout.flush()
  with TKBHit() as kbhit:
    return kbhit.AskYesNo()
  return None

#KBHit version of AskGen (does not wait for pressing return key).
def KBHAskGen(*argv):
  sys.stdout.flush()
  with TKBHit() as kbhit:
    return kbhit.AskGen(*argv)
  return None


import torch
import torch.nn as nn
import torch.utils.data as data
import numpy as np
import random
from tokenization.word_tokenizer import WordTokenizer
import sklearn
from sklearn import metrics
class BagREDataset(data.Dataset):

    def __init__(self,path,opt,rel2id,token2id):
        super().__init__()
        self.path = path
        self.tokenizer = WordTokenizer(opt=opt,vocab=token2id)
        self.rel2id = rel2id
        self.entpair_as_bag = opt.entpair_bag
        self.bag_size = opt.bag_size
        self.data = []
        with open(self.path) as f:
            for line in f:
                line = line.rstrip()
                if len(line) > 0:
                    self.data.append(eval(line))

        if opt.mode == None:
            self.weight = np.zeros((len(self.rel2id)),dtype=np.float32)
            self.bag_scope = []
            self.name2id = {}
            self.bag_name = []
            self.facts = {}

            for idx,item in enumerate(self.data):
                fact = (item['h']['id'], item['t']['id'], item['relation'])
                # print(type(fact))
                # print(fact)
                if item['relation'] != 'NA':
                    self.facts[fact] = 1
                if self.entpair_as_bag:
                    name = (item['h']['id'], item['t']['id'])
                else:
                    name = fact

                if name not in self.name2id:
                    self.name2id[name] = len(self.name2id)
                    self.bag_scope.append([])
                    self.bag_name.append(name)
                self.bag_scope[self.name2id[name]].append(idx)
                self.weight[self.rel2id[item['relation']]] += 1.0

            self.weight = 1.0 / (self.weight ** 0.05)
            self.weight = torch.from_numpy(self.weight)

            #print(self.bag_scope)
            #print(self.bag_scope.size())
        else:
            pass

    def __len__(self):
        return len(self.bag_scope)

    def __getitem__(self,index):

        bag = self.bag_scope[index]
        if self.bag_size is not None:
            if self.bag_size <= len(bag):
                resize_bag = random.sample(bag, self.bag_size)
            else:
                resize_bag = bag + list(np.random.choice(bag, self.bag_size - len(bag)))
            bag = resize_bag

        seqs = None
        rel = self.rel2id[self.data[bag[0]]['relation']]
        for sent_id in bag:
            item = self.data[sent_id]
            seq = list(self.tokenizer.tokenizer(item))
            #print(len(seq))
            #print(seq)
            if seqs is None:
                seqs = []
                for i in range(len(seq)):
                    seqs.append([])

            for i in range(len(seq)):
                seqs[i].append(seq[i])

        for i in range(len(seqs)):
            seqs[i] = torch.cat(seqs[i], 0)

        return [rel,self.bag_name[index], len(bag)] + seqs

    def collate_fn(data):
        """
        It operates the data from __getitem__
        get a list,which size is batch_size and item is the result of __getitem__
        get a list
        :return:
        """
        data = list(zip(*data))
        label, bag_name, count = data[:3]
        seqs = data[3:]
        #print(seqs[0])
        for i in range(len(seqs)):   #The style is not clear,it can be rewrite
            seqs[i] = torch.cat(seqs[i],0)

        scope = [] # (B, 2)
        start = 0
        for c in count:
            scope.append((start, start + c))
            start += c
        assert(start == seqs[0].size(0))

        label = torch.tensor(label).long() # (B)
        return [label, bag_name, scope] + seqs

    def eval(self, pred_result):
        """
        Args:
            pred_result: a list with dict {'entpair': (head_id, tail_id), 'relation': rel, 'score': score}.
                Note that relation of NA should be excluded.
        Return:
            {'prec': narray[...], 'rec': narray[...], 'mean_prec': xx, 'f1': xx, 'auc': xx}
                prec (precision) and rec (recall) are in micro style.
                prec (precision) and rec (recall) are sorted in the decreasing order of the score.
                f1 is the max f1 score of those precison-recall points
        """
        sorted_pred_result = sorted(pred_result, key=lambda x: x['score'], reverse=True)
        prec = []
        rec = []
        correct = 0
        total = len(self.facts)
        for i, item in enumerate(sorted_pred_result):
            if (item['entpair'][0], item['entpair'][1], item['relation']) in self.facts:
                correct += 1
            prec.append(float(correct) / float(i + 1))
            rec.append(float(correct) / float(total))
        auc = metrics.auc(x=rec, y=prec)

        np_prec = np.array(prec)
        np_rec = np.array(rec)
        f1 = (2 * np_prec * np_rec / (np_prec + np_rec + 1e-20)).max()
        mean_prec = np_prec.mean()
        return {'prec': np_prec, 'rec': np_rec, 'mean_prec': mean_prec, 'f1': f1, 'auc': auc}

def BagRELoader(path,opt,rel2id, token2id, collate_fn=BagREDataset.collate_fn):

    dataset = BagREDataset(path,opt, rel2id,token2id)

    data_loader = data.DataLoader(dataset=dataset,
            batch_size=opt.batch_size,
            shuffle=opt.shuffle,
            pin_memory=True,
            num_workers=opt.num_workers,
            collate_fn=collate_fn
            )

    return data_loader

# def test():
#     a = torch.tensor([[1,2,3],[2,4,6],[1,4,6],[4,6,8]])
#     print(a)
#     print("---------------")
#     print(a[1:])
#     b = torch.argmax(a[1:],dim=0)
#     print(b)
#
#
# test()

def test():
    from tqdm import tqdm


"""myproject URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.urls import path
from . import views
from django.conf.urls import url,include
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.auth import views as auth_views

urlpatterns = [
    path(r'images/', views.image_list, name='image_list'),
    path(r'novels/', views.novel_list, name='novel_list'),
    path(r'blogs/', views.blog_list, name='blog_list'),
    path(r'blog_detail/<int:blog_id>/', views.blog_detail, name='blog_detail'),
    path(r'login/', views.user_login, name='login'),
    path(r'logout/', views.logout, name='logout'),
    path(r'register/', views.register, name='register'),
    path(r'captcha/', include('captcha.urls')),
    path(r'mdeditor/', include('mdeditor.urls')),
    path(r'searchimage/', views.search_by_category_image, name='search_by_category_image'),
    path(r'searchnovel/', views.search_by_category_novel, name='search_by_category_novel'),
    path(r'searchblog/', views.search_by_category_blog, name='search_by_category_blog'),
    path(r'test/', views.test_celery, name="test_celery")
]

if settings.DEBUG:
    # static files (images, css, javascript, etc.)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect
from django.urls import reverse
from .models import Libro
from .forms import LibroForm, UserRegisterForm
from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages

# Create your views here.
def index (request):
    return render(request, 'Inventario/index.html',{
        'libro': Libro.objects.all()
    })

def view_producto(request, id):
    libro=Libro.objects.get(px=id)
    return HttpResponseRedirect(reverse('index'))

def add(request):
    if request.method == 'POST':
        form = LibroForm(request.POST)
        if form.is_valid():
            nuevo_nombre_libro = form.cleaned_data['nombre_libro']
            nuevo_nombre_autor = form.cleaned_data['nombre_autor']
            nueva_categoria = form.cleaned_data['categoria']
            nuevo_precio = form.cleaned_data['precio']
            nuevo_codigo = form.cleaned_data['codigo']

            nuevolibro = Libro(
                nombre_libro = nuevo_nombre_libro,
                nombre_autor = nuevo_nombre_autor,
                categoria = nueva_categoria,
                precio = nuevo_precio,
                codigo = nuevo_codigo,
                estado = 'A'
            )

            nuevolibro.save()
            return render(request, 'inventario/add.html',{
                'form':LibroForm(),
                'success':True
            })
    else:
        form = LibroForm()
        return render(request,'inventario/add.html',{
            'form':LibroForm(),
        })

def edita(request,id):
    if request.method == 'POST':
        producto = Libro.objects.get(pk=id)
        form = LibroForm(request.POST, instance=producto)
        if form.is_valid():
            form.save()
            return render(request, 'inventario/edit.html',{
                'form':form,
                'success':True
            })
    else:
        producto = Libro.objects.get(pk=id)
        form = LibroForm(instance=producto)

    return render(request, 'inventario/edit.html',{
        'form':form
    })

def elimina(request,id):
    if request.method == 'POST':
        producto = Libro.objects.get(pk=id)
        producto.estado='I'
        producto.save()
    return HttpResponseRedirect(reverse('index'))

def registrar(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            usuario = form.cleaned_data['username']
            form.save()
            messages.success(request, f'Usuario {usuario} creado')
            return redirect(index)
    else:
        form = UserRegisterForm()
    contex = { 'form':form}
    return render(request, 'Inventario/register.html', contex)


d = {"Tisch": "table",
     "Stuhl": "chair",
     "Schreibtisch": "desk"}

# Ziel: Weiteres Wörterbuch aufbauen, das als Schlüssel englische Begriffe enthält.

# 1. Ansatz:
l = []
for tupel in d.items():
    l.append((tupel[1], tupel[0]))

print(l)
e = dict(l)
print(e["chair"])

# 2. Ansatz:
# l = [] # List Comprehension

# 3. Ansatz:
e = {}
for key, value in d.items():
    e[value] = key

print(e["chair"])


from PIL import Image
import random
i = Image.new("RGB",(1000, 1000))
x = i.load()
d = 1

while d < 1000000:
	q = random.randint(1, 999)
	a = random.randint(1, 999)
	w = random.randint(5, 255)
	e = random.randint(5, 255)
	r = random.randint(5, 255)
	x[q, a] = (w, e, r)
	d += 1
i.show()
for eggy in range (1, 31):
	for o in range(3, 999):
		for m in range(3, 999):
			q = random.random()
			if q <= 0.8:
				numb = random.randint(1, 8)
				if numb == 1:
					x[o, m] = x[o, m+1]
				elif numb == 2:
					x[o, m] = x[o+1, m+1]
				elif numb == 3:
					x[o, m] = x[o+1, m]
				elif numb == 4:
					x[o, m] = x[o+1, m-1]
				elif numb == 5:
					x[o, m] = x[o, m-1]
				elif numb == 6:
					x[o, m] = x[o-1, m-1]
				elif numb == 7:
					x[o, m] = x[o-1, m]
				elif numb == 8:
					x[o, m] = x[o-1, m+1]
				else:
					print("damn daniel")
i.show()


import pandas
import pickle

from tqdm import tqdm

INTERPOLATION_MAPPING_FILE = 'mapping_coordinates/traffic_map_sa_100.pkl'

DATA_FILE = 'data/traffic/traffic_data.csv'
GRID_FILE = 'data/interpolation_grid_100.csv'
GRID_INDEX = ['Index']
DATA_FIELDS = ['Volume', 'Occupancy', 'Speed']
DATA_INDEX = ['AssetNum']

# time filters
WEEKDAY_NAME = 'Weekday'
WEEKDAY_FILTER = 5

TIME_NAME = 'Time'
TIME_FILTER = 7

OUTPUT_DATA_PATH = 'data'
OUTPUT_DATA_SET_NAME = 'mapgen_traffic.csv'

grid = pandas.read_csv(GRID_FILE,index_col = 0)
raw_data = pandas.read_csv(DATA_FILE)
data = raw_data[(raw_data[WEEKDAY_NAME]==WEEKDAY_FILTER) &
                (raw_data[TIME_NAME]==TIME_FILTER)]
data.set_index(DATA_INDEX, inplace=True)

mapping_dict = pickle.load(open(INTERPOLATION_MAPPING_FILE,'rb'))

grid_interpolation_output = []
for point in tqdm(grid.index):
    weight_set = mapping_dict[point]['weight_vals']
    idx_set = mapping_dict[point]['triangle_idx']
    values = data[DATA_FIELDS].loc[idx_set].transpose()

    output_vals = [point] + list((values*weight_set).sum(axis=1)/sum(weight_set))
    grid_interpolation_output.append(output_vals)

out_df = pandas.DataFrame.from_records(grid_interpolation_output, columns=GRID_INDEX + DATA_FIELDS)

out_df.set_index(*GRID_INDEX,inplace=True)
out_df.to_csv(f'{OUTPUT_DATA_PATH}/{OUTPUT_DATA_SET_NAME}.csv')


import pandas as pd
import sqlite3
conn = sqlite3.connect('C:\\ machinemon.db')
#df.to_sql(name='tempdata', con=conn)
curr=conn.cursor()
#query="INSERT INTO TEMPERATURE VALUES(" +"'" + str(datetime.date(datetime.now())) + "'" +"," + "'" + str(datetime.time(datetime.now())) + "'"+ "," + "'" + str(tem) +  "'" + "," + "'" + tempstatus +"'" + ")"
df = pd.read_sql_query("select * from fielddata;", conn)
print(df)
#curr.execute(query)
#conn.commit()

#Looking at data
print(df.columns)
print(df.shape)
#Looking at datatypes
print(df.dtypes)
df.tail(1)

#Checking for missing values
print(df.isnull().any())

#EDA- Exploratory Data Analysis
import numpy as np
print("----------EDA STATISTICS---------------")
pd.option_context('display.max_columns', 40)
with pd.option_context('display.max_columns', 40):
    print(df.describe(include=[np.number]))

#Correlation results
print("----------Correlation---------------")
with pd.option_context('display.max_columns', 40):
    print(df.corr())

#Dividing data into features and target
target=df['Calldrop']
nm=['CID1','CID2','Speed','OutsideTemperature','OutsideHumidity','SignalStrength','BatteryLevel']
features=df[nm]
with pd.option_context('display.max_columns', 40):
    features.head(1)
    target.head(1)

#Building the Model
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split( features, target, test_size=0.25, random_state=0)

from sklearn.linear_model import LogisticRegression

lr  = LogisticRegression()
lr.fit(x_train, y_train)
# Returns a NumPy Array
# Predict for One Observation (image)
lr.predict(x_test)

predictions = lr.predict(x_test)

# Use score method to get accuracy of model
score = lr.score(x_test, y_test)
print(score)

import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import metrics
import numpy as np

cm = metrics.confusion_matrix(y_test, predictions)
print(cm)

plt.figure(figsize=(9,9))
sns.heatmap(cm, annot=True, fmt=".3f", linewidths=.5, square = True, cmap = 'Blues_r');
plt.ylabel('Actual Calldrops');
plt.xlabel('Predicted Calldrops');
all_sample_title = 'Accuracy Score: {0}'.format(score)
plt.title(all_sample_title, size = 15);

plt.figure(figsize=(7,7))
plt.imshow(cm, interpolation='nearest', cmap='Pastel1')
plt.title('Confusion matrix', size = 15)
plt.colorbar()
tick_marks = np.arange(1)
plt.xticks(tick_marks, ["0", "1"], rotation=45, size = 15)
plt.yticks(tick_marks, ["0", "1"], size = 15)
plt.tight_layout()
plt.ylabel('Actual Calldrops', size = 15)
plt.xlabel('Predicted Calldrops', size = 15)
width, height = cm.shape
for x in range(width):
 for y in range(height):
  plt.annotate(str(cm[x][y]), xy=(y, x),
  horizontalalignment='center',
  verticalalignment='center')
plt.show()


p = int(input("enter a prime number: "))
a = int(input("enter another number smaller than p: "))

def quadres(prime, num):
    result = pow(num, int((prime-1)/2), prime)
    if result == p-1:
        print("-1")
    if result == 1:
        print("1")
    if result == 0:
        return 0

print(quadres(p, a))


class Rectangle():
    def __init__(self, min_x=0, min_y=0, max_x=0, max_y=0) -> None:
        self.min_x = min_x
        self.min_y = min_y
        self.max_x = max_x
        self.max_y = max_y

    def area(self):
        return (self.max_x-self.min_x)*(self.max_y - self.min_y) if self.min_x< self.max_x and self.min_y < self.max_y else 0

def intersection_area(rec1: Rectangle,rec2:Rectangle):
    intersection = Rectangle()
    intersection.min_x = max(rec1.min_x, rec2.min_x)
    intersection.min_y = max(rec1.min_y, rec2.min_y)
    intersection.max_x = min(rec1.max_x, rec2.max_x)
    intersection.max_y = min(rec1.max_y, rec2.max_y)

    return intersection.area()

rec1 = Rectangle(0,0,3,2)
rec2 = Rectangle(1,1,3,3)

print(intersection_area(rec1,rec2))


import io
import csv
import zipfile
from urllib.parse import quote as urlquote

from django.http import StreamingHttpResponse

class CSVFileInMemory:
    def __init__(self, filename: str):
        self.filename = filename + '.csv'
        self.data = io.StringIO()
        self._file = csv.writer(self.data)

    def writerow(self, row: list):
        self._file.writerow(row)

    def writerows(self, rows: list):
        self._file.writerows(rows)

    @property
    def size(self):
        cu = self.data.tell()
        end_pos = self.data.seek(0, io.SEEK_END)
        self.data.seek(cu)
        return end_pos

    def to_bytes(self):
        value = self.data.getvalue()
        return value.encode(encoding='utf-8')

    def close(self):
        self.data.close()

    def to_zip(self):
        zf_buf = io.BytesIO()
        zf = zipfile.ZipFile(zf_buf, 'a', zipfile.ZIP_DEFLATED, False)

        # write the file to the in-memory zip
        self.data.seek(0)
        zf.writestr(zinfo_or_arcname=self.filename, data=self.data.read())
        # mark the files as having been created on Windows
        # so that Unix permissions are not inferred as 0000
        for zfile in zf.filelist:
            zfile.create_system = 0

        return zf_buf

def wrap_csv_file_response(filename: str, data):
    """
    :param filename: 这个是下载后的名字
    :param data: bytes, BytesIO， StringIO
    """
    if isinstance(data, bytes):
        content_type = 'application/octet-stream'
        content_length = len(data)
        data = io.BytesIO(data)
    elif isinstance(data, io.StringIO):
        content_type = 'text/csv'
        content_length = None
        data.seek(0)
    else:
        content_type = 'application/octet-stream'
        content_length = data.seek(0, io.SEEK_END)
        data.seek(0)

    filename = urlquote(filename)  # 中文文件名需要
    response = StreamingHttpResponse(data, charset='utf-8', status=200)
    if content_length:
        response['Content-Length'] = content_length           # byte length

    response['Content-Type'] = content_type
    response['Content-Disposition'] = f"attachment;filename*=utf-8''{filename}"  # 注意filename 这个是下载后的名字
    return response


from logging import getLogger
import re
import mojimoji
from app_jra.consts import *
import sys
import linecache
from app_jra.log_commons import *
from datetime import datetime as dt
from datetime import timedelta

logger = getLogger('jra_edit_delivery')
Common_log = Common_log(DEBUGLOG_NAME_TO_TYPE[logger.name])
log_err_msg_id = 3400

try:
    from app_jra_mddb.mddb_commons import Common
except Exception as e:
    Common_log.Out_Logs(log_err_msg_id, [e])
    logger.error(f'commonsファイル読み込み失敗 : {e}')

def failure(e):
    exc_type, exc_obj, tb=sys.exc_info()
    lineno=tb.tb_lineno
    return str(lineno) + ":" + str(type(e)) + str(e)

class Trn_M120_agari():

    def get_kaisaibi(self, jou_obj, kai, nichime):
        from app_jra.models import Mst_Schedule
        kaisaibi = None
        schedule_objs = Mst_Schedule.objects.filter(Jou=jou_obj, Kai=kai, Nichime=nichime)
        if schedule_objs.exists():
            kaisaibi = schedule_objs.last().Date
        return kaisaibi

    def chk_blank(self, record):
        # 受信データにはブランクのパターンがいくつかあるが、空データの場合は全てブランクとみなす処理
        blank_list = ['','　',None,[],{}]
        if record in blank_list or re.fullmatch('\s+', record):
            return False
        else:
            return True

    # 【共通処理】
    # １．半角int（またはdecimal）→ 全角str用の辞書と関数
    # （参考） https://qiita.com/YuukiMiyoshi/items/6ce77bf402a29a99f1bf
    def intToZen(self, i):
        HAN2ZEN = str.maketrans({"0": "０", "1": "１", "2": "２", "3": "３", "4": "４", "5": "５", "6": "６", "7": "７", "8": "８", "9": "９", ".": "．", "G": "Ｇ"})
        if i or i == 0:
            return str(i).translate(HAN2ZEN)
        else:
            return

    def get_umaban_fields(self, renum):
        umaban = "Time" + str(renum) + "_horse_ban"
        return str(umaban)

    def make_a4hakei(self, data, renum):
        a4hakei = None
        sec1 = getattr(data, "Time" + str(renum) + "_4_seconds")
        sec2 = getattr(data, "Time" + str(renum) + "_4_seconds2")
        if self.chk_blank(sec1) and self.chk_blank(sec2):
            if not sec1 + sec2 == "999":
                a4hakei = self.intToZen(int(sec1)) + "." + self.intToZen(sec2)
        return a4hakei

    def make_a3hakei(self, data, renum):
        a3hakei = None
        sec1 = getattr(data, "Time" + str(renum) + "_3_seconds")
        sec2 = getattr(data, "Time" + str(renum) + "_3_seconds2")
        if self.chk_blank(sec1) and self.chk_blank(sec2):
            if not sec1 + sec2 == "999":
                a3hakei = self.intToZen(int(sec1)) + "." + self.intToZen(sec2)
        return a3hakei

    def insert_or_update_M120_agari(self, datfilename, datDataFileFlg):
        try:
            from app_jra.models import (
                Trn_A200_1_JARIS_seiseki,
                Trn_A200_2_Seiseki_info,
                Trn_A326_Umabetsu_agari_time,
                M120_agari
            )
            Cmn = Common()

            # 受信ファイルごとに、M120レコードを新規作成・更新していく。
            # 正しく登録完了した場合、後続の待ち合わせ・配信処理で使うために、更新した中間DBのレコードを保持しておき、リストにして返す。
            # 登録失敗時には、False(ABNOMAL or 空のリスト)を返す。
            edit_mddb_list = []  # 初期値
            m120_obj = None

            if UMABETSU_AGARI_TIME == datDataFileFlg:
                # A326 馬別上がりタイム
                A326_list = Trn_A326_Umabetsu_agari_time.objects.filter(Receive_filename=datfilename)
                for data in A326_list:

                    jou_obj = Cmn.chk_master_Mst_Jou(data.Bangumi_Jou_code)
                    kaisuu = int(data.Bangumi_kai)
                    kainichime = int(data.Bangumi_day)
                    rebangou = int(data.Bangumi_race_number)

                    kaisai_date = self.get_kaisaibi(jou_obj,kaisuu,kainichime)
                    if not kaisai_date:
                        kaisai_date = dt.strptime(data.Soushin_date, '%y%m%d').date()

                    # A326は最大18頭分なので18回ループ
                    for i in range(18):
                        renum = i + 1

                        # ループごとの馬番で空チェックして無ければスキップ
                        if self.chk_blank(getattr(data, self.get_umaban_fields(renum))):
                            # M120から番組情報、馬番を元に情報を取得する
                            m120_list = M120_agari.objects.filter(
                                kaisaibi = kaisai_date,
                                joumei=jou_obj,
                                kaisuu=kaisuu,
                                kainichime=kainichime,
                                rebangou=rebangou,
                                uma=int(getattr(data, self.get_umaban_fields(renum)))
                                )

                            # 各馬番に該当するM120 馬別上がりタイムレコードを検索する
                            if not m120_list.exists():
                                # M120 馬別上がりタイムを新規作成
                                m120_obj = M120_agari.objects.create(
                                    kaisaibi = kaisai_date,
                                    joumei = jou_obj,
                                    kaisuu = kaisuu,
                                    kainichime = kainichime,
                                    rebangou = rebangou,
                                    uma = int(getattr(data, self.get_umaban_fields(renum))),
                                    a4hakei = self.make_a4hakei(data, renum),
                                    a3hakei = self.make_a3hakei(data, renum),
                                    heikin1ha = self.make_a3hakei(data, renum)
                                    )

                            else:
                                m120_obj = m120_list.last()

                                # m120_objに情報を追加していく
                                m120_obj.a4hakei = self.make_a4hakei(data, renum)
                                m120_obj.a3hakei = self.make_a3hakei(data, renum)
                                m120_obj.heikin1ha = self.make_a3hakei(data, renum)
                                m120_obj.save()

            if JARIS_SEISEKI == datDataFileFlg:
                # A200 JARIS成績
                A200_list = Trn_A200_1_JARIS_seiseki.objects.filter(Receive_filename=datfilename)
                for data in A200_list:

                    jou_obj = Cmn.chk_master_Mst_Jou(data.Bangumi_Jou_code)
                    kaisuu = int(data.Bangumi_kai)
                    kainichime = int(data.Bangumi_day)
                    rebangou = int(data.Bangumi_race_number)

                    # A200 JARIS成績を元に成績詳細を取得
                    A200_2_list = Trn_A200_2_Seiseki_info.objects.filter(A200_1=data)
                    if A200_2_list.exists():
                        for seiseki_data in A200_2_list:
                            if self.chk_blank(seiseki_data.Ketto_touroku_number):

                                kaisai_date = self.get_kaisaibi(jou_obj,kaisuu,kainichime)
                                if not kaisai_date:
                                    kaisai_date = dt.strptime(data.Soushin_date, '%y%m%d').date()

                                m120_list = M120_agari.objects.filter(
                                    kaisaibi = kaisai_date,
                                    joumei=jou_obj,
                                    kaisuu=kaisuu,
                                    kainichime=kainichime,
                                    rebangou=rebangou,
                                    uma=int(seiseki_data.Ban)
                                    )

                                if not m120_list.exists():
                                    # M120 馬別上がりタイムを新規作成
                                    m120_obj = M120_agari.objects.create(
                                        kaisaibi = kaisai_date,
                                        joumei = jou_obj,
                                        kaisuu = kaisuu,
                                        kainichime = kainichime,
                                        rebangou = rebangou,
                                        shougaikubun = Cmn.chk_master_Mst_Kyousou_shubetsu(data.Seiseki_data_kyoso_syubetsu_code),
                                        uma = int(seiseki_data.Ban),
                                        umajouhou = Cmn.chk_master_Mst_Horse(seiseki_data.Ketto_touroku_number),
                                    )
                                else:
                                    m120_obj = m120_list.last()

                                    # m120_objに情報を追加していく
                                    m120_obj.kaisaibi = kaisai_date
                                    m120_obj.shougaikubun = Cmn.chk_master_Mst_Kyousou_shubetsu(data.Seiseki_data_kyoso_syubetsu_code)
                                    m120_obj.umajouhou = Cmn.chk_master_Mst_Horse(seiseki_data.Ketto_touroku_number)
                                    m120_obj.save()

            edit_mddb_list.append(m120_obj)
            return edit_mddb_list

        except Exception as e:
            Common_log.Out_Logs(log_err_msg_id, [failure(e)])
            logger.error(failure(e))
            return False


import glob
import os
import attr
import numpy as np
import torch
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter
from torchvision.utils import make_grid

OPT_NAMES = ['adagrad', 'adadelta', 'adam', 'amsgrad', 'adamw']

@attr.s(kw_only=True)
class HParams:
    out_dir = attr.ib()
    z_dim = attr.ib()
    lr = attr.ib()
    epochs = attr.ib()
    opt_name = attr.ib()
    opt_kwargs = attr.ib(factory=dict)
    use_cuda = attr.ib(default=True)
    ckpt_freq = attr.ib(default=20)
    sample_freq = attr.ib(default=5)

class Logger:
    def __init__(self, log_dir):
        if os.path.exists(log_dir):
            assert len(glob.glob('events.out.tfevents.*')) == 0, (
                f'Tensorboard log already exists in {log_dir}')
        self.writer = SummaryWriter(log_dir=log_dir, flush_secs=30)

    def flush(self):
        self.writer.flush()

    def log_scalar(self, tag, val, step):
        if hasattr(val, 'item'):
            val = val.item()
        self.writer.add_scalar(tag, val, global_step=step)

    def log_scalars(self, tag_value_dict, step: int):
        for tag, val in tag_value_dict.items():
            self.log_scalar(tag, val, step)

    def log_image(self, tag: str, img: torch.Tensor, step: int):
        assert img.ndim == 3
        self.writer.add_image(tag, img, global_step=step, dataformats='CHW')

    def log_image_grid(self, tag, imgs, step: int, **kwargs):
        assert imgs.ndim == 4
        img_grid = make_grid(imgs, **kwargs)
        self.log_image(tag, img_grid, step)

    def add_graph(self, *args, **kwargs):
        self.writer.add_graph(*args, **kwargs)

def normal_logpdf(x, mu=None, sig=None):
    D = x.shape[1]
    mu = torch.zeros_like(x) if mu is None else mu
    sig = torch.ones_like(x) if sig is None else sig
    return -0.5 * D * np.log(2*np.pi) - (
        torch.log(sig) + 0.5 / (sig**2) * ((x - mu) ** 2)).sum(dim=1)

def sumflat(x):
    return x.flatten(1).sum(dim=1)

def print_model_info(model: torch.nn.Module):
    cnt_train = sum(np.prod(p.shape) for p in model.parameters() if p.requires_grad)
    cnt_total = sum(np.prod(p.shape) for p in model.parameters())
    print(f'Model has {cnt_train} trainable parameters ({cnt_total} total). Training = {model.training}')

def tanh_to_uint8(x):
    x = ((x + 1) / 2 * 255.).byte().cpu()
    assert x.dtype == torch.uint8
    return x

def get_optimizer(name, params, **kwargs):
    optim_cls = {
        'adagrad': optim.Adagrad,
        'adadelta': optim.Adadelta,
        'adam': optim.Adam,
        'amsgrad': optim.Adam,
        'adamw': optim.AdamW,
    }[name]

    if name in ('adam', 'amsgrad'):
        return optim_cls(params, amsgrad=(name == 'amsgrad'), **kwargs)
    else:
        return optim_cls(params, **kwargs)


import logging

import data_preparation

import numpy as np
import pandas as pd

from IPython.display import display
from datetime import datetime, timedelta
from definitions import yesterday, COLUMNS_ANALYSIS
from definitions import ROOT_DIR, STATUS_TYPES, VALID_DATASETS, COUNTRY

log = logging.getLogger(__name__)

def preprocess_data():
    datasets = set(VALID_DATASETS) - {'total'}
    data_dir = '{}/data/cleaned/'.format(ROOT_DIR)

    try:
        total = pd.read_csv('{}../cleaned/total.csv'.format(data_dir))
        total_dates = total['date'].unique()
    except FileNotFoundError:
        total = pd.DataFrame()
        total_dates = []

    all_data = []
    dates_per_dataset = {}
    for csv in datasets:
        log.info('Preprocessing {}'.format(csv))
        preprocesser = getattr(data_preparation, '{}Preprocessing'
                               .format(csv.capitalize()))(csv)
        preprocesser.reshape_data()
        data = preprocesser.make_consistent()
        assert all(c in data.columns for c in COLUMNS_ANALYSIS)
        dates_per_dataset[csv] = [date for date in data['date'].unique()
                                  if date not in total_dates]
        all_data.append(data)

    all_dates = list(set([date for list_of_dates in
                          [dates_per_dataset[dataset] for dataset in datasets]
                          for date in list_of_dates]))
    all_dates.sort(key=lambda d: datetime.strptime(d, '%m/%d/%y'))

    for d in all_dates:
        total = pd.concat([total, *[data[data['date'] == d] for data in all_data
                                    if d in data['date'].values]])

    log.info('Update success. Checking consistency...')
    # assert all columns equal
    for data in all_data:
        assert data.shape[1] == total.shape[1]
    # assert number of rows equal to the sum of all csvs
    try:
        assert sum([d.shape[0] for d in all_data]) == total.shape[0]
    except AssertionError:
        log.info('The total size seems to be not correct, check the dates of '
                 'the csvs, Italy may has one date more.')
        log.info('Shapes: italy {}, world {}, usa {}, total {}'
                 .format(*[d.shape[0] for d in all_data], total.shape[0]))

    for c in STATUS_TYPES:
        total[c] = total[c].fillna(-1)
    total.to_csv('{}total.csv'.format(data_dir), index=False,
                 float_format='%.5f')
    if check_consistency(total.copy()):
        log.info('New data loaded correctly')

    log.info('Total number of countries is {}'.format(
        len(list(total[COUNTRY].unique()))
    ))

def check_consistency(data):
    check = True
    new_date = '{0[1]:}/{0[2]:}/{0[0]}'.format(yesterday().timetuple())
    old = yesterday() - timedelta(days=1)
    old_date = '{0[1]:}/{0[2]:}/{0[0]}'.format(old.timetuple())
    data_old = data[data['date'] == old_date[:-2]]
    data_new = data[data['date'] == new_date[:-2]]
    cols = [COUNTRY, 'Province/State', 'date']

    try:
        assert set(data_old[COUNTRY].values) <= set(data_new[COUNTRY].values)
    except AssertionError:
        check = False
        log.warning('Fewer countries than yesterday!')

    for s in STATUS_TYPES:
        if data_old.shape[0] != data_new.shape[0]:
            log.debug('Data old has more or few cols than data new')
            break
        try:
            assert np.all(data_new[s].values >= data_old[s].values)
        except AssertionError:
            check = False
            countries = data[data.index.isin(
                data_new[s][~(data_new[s].values >= data_old[s].values)].index
            )][COUNTRY].unique()
            log.info("Inconsistency in the time series {} for countries {}"
                     .format(s, countries))
            d_old = data[data.index.isin(data_old[s].index)].reset_index(drop=True)
            d_new = data[data.index.isin(data_new[s].index)].reset_index(drop=True)
            for c in countries:
                if d_old.empty:
                    log.info('Country {} first observation today'.format(c))
                else:
                    old = d_old[d_old[COUNTRY] == c][s]
                    new = d_new[d_new[COUNTRY] == c][s]
                    wrong = old > new
                    df = d_old[d_old[COUNTRY] == c][wrong][cols]
                    df[s] = old[wrong]
                    df['date_new'] = d_new[d_new[COUNTRY] == c][wrong]['date']
                    df['{}_new'.format(s)] = new[wrong]
                    log.info('Country {} wrong values are'.format(c))
                    display(df)
    return check

if __name__ == '__main__':
    log.info('{0:} Starting data update {0:}'.format('='*80))
    preprocess_data()


"""Interactions with EGI's AppDB"""
import logging
import requests
import xmltodict

# Logging
logger = logging.getLogger(__name__)

def appdb_call(query):
    try:
        response = requests.get('https://appdb.egi.eu' + query)
        data = response.text
    except:
        logger.error('Unable to query AppDB with query: %s', query)
        return {}

    data.replace('\n', '')
    return xmltodict.parse(data)

def get_cloud_status_appdb():
    data = appdb_call('/rest/1.0/va_providers/nova')

    output = {}
    if 'appdb:appdb' not in data:
        return output

    if 'virtualization:provider' in data['appdb:appdb']:
        for provider in data['appdb:appdb']['virtualization:provider']:
            if '@service_status' in provider:
                output[provider['provider:name']] = provider['@service_status']

    return output


"""
# key value data store-you can create a key with a value related
# instead of working with an index
# with a dictionary we use a key value
# we pass in a key to get the value
"""
players = {         #dictionary stored in players variable
    "ss": "Correa", #created a key of "ss", value of "Correa"
    "2b": "Altuve",
    "3b": "Bregman",
    "DH": "Gattis",
    "OF": "Springer",
}

second_base = players['2b'] # to query- to grab the second base player-call the players dictionary then just pass in the string value of the key

print(second_base)

# remember what ever string based key is IDENTICAL MATCH TO THE KEY in the dictionary
#///////////////////////////////////////////////////////////
#From Benjamin Nicklaus to Everyone:  01:56 PM
teams = {                                      # variable teams
  "astros": ["Altuve", "Correa", "Bregman"],   # start with the key astros then colon and then a list wrapper in [] -value can be anything
  "angels": ["Trout", "Pujols"],
  "yankees": ["Judge", "Stanton"],
}

astros = teams['astros'] # storing them in a variable
print(astros)
print(teams['angels']) #  say print and call teams and then grab dictionary value ['angels']
print(teams['yankees'])


# -*- coding: utf-8 -*-
import time

class progressbarClass:
    def __init__(self, finalcount, progresschar=None):
        import sys

        self.finalcount=finalcount
        self.blockcount=0
        #
        # See if caller passed me a character to use on the
        # progress bar (like "*").  If not use the block
        # character that makes it look like a real progress
        # bar.
        #
        if not progresschar: self.block='='
        else:                self.block=progresschar
        #
        # Get pointer to sys.stdout so I can use the write/flush
        # methods to display the progress bar.
        #
        self.f=sys.stdout
        #
        # If the final count is zero, don't start the progress gauge
        #
        if not self.finalcount : return
        self.f.write('\n------------------- % Progress -------------------\n')
        self.f.flush()
        self.time = time.time()
        self.findn = 0 # how many cannot be calculated when evaluate the remaining time
        self.textleft = False
        return

    def progress(self, count, find=False):
        #
        # Make sure I don't try to go off the end (e.g. >100%)
        #
        count=min(count, self.finalcount)
        #
        # If finalcount is zero, I'm done
        #
        if self.finalcount:
            percentcomplete=round(100*count/self.finalcount,2)
        else:
            percentcomplete=100

        #print "percentcomplete=",percentcomplete
        blockcount=int(percentcomplete/2)
        #print "blockcount=",blockcount
#        if blockcount > self.blockcount:
#            for i in range(self.blockcount,blockcount):
#                self.f.write(self.block)
#                self.f.flush()
        self.findn += find

        txt = str(percentcomplete)+'%('
        if (count-self.findn)==0:
            txt += '-'
        else:
            left_s = (time.time()-self.time)/(count-self.findn)*(self.finalcount-count)
            if left_s>3600:
                txt += str(round(left_s/3600,2))+'h'
            elif left_s>60:
                txt += str(round(left_s/60,2))+'m'
            else:
                txt += str(round(left_s,2))+'s'
        txt+=' left)'
        if self.textleft:
            txt += self.block*(blockcount-len(txt))
        elif blockcount+len(txt)>50:
            txt += self.block*(blockcount-len(txt))
            self.textleft=True
        else:
            txt = self.block*blockcount + txt
        if len(txt)<50:
            txt+=' '*(50-len(txt))
        txt += '\r'
        self.f.write(txt)
        self.f.flush()
        self.blockcount=blockcount
        return
    def done(self):
        self.f.write("\n")
        self.f.flush()
if __name__ == "__main__":
    from time import sleep
    pb=progressbarClass(51,"=")
    count=0
    for count in range(52):
        pb.progress(count)
        sleep(0.2)
    pb.done()