import numpy as np
import matplotlib.pyplot as plt

#===============================Carregar Arquivo===================================
#Usando csv.reader
#with open('Advertising.csv') as f:
#    dados = list(csv.reader(f, delimiter=','))

#Usando NumPy
dados = np.genfromtxt("Adversiting.csv", delimiter=',', skip_header=1)
print(dados)
X = dados[:,1:4]
y = dados[:,4]
#===================================================================================

#=========================Método de regressão simples===========================================================
class SampleLinearRegressor():
  #construtor
  def __init__(self):
    pass

  #Calculo de B1 e B0
  def fit(self, X, y):
    xMean = np.mean(X)
    self.yMean = np.mean(y)

    sumUpB1 = 0
    sumDownB1 = 0
    i = 0
    while(i < X.shape[0] ):
        sumUpB1 += (X[i] - xMean) * (y[i] - self.yMean)
        sumDownB1 += (X[i] - xMean) ** 2
        self.B1 = sumUpB1/sumDownB1
        self.B0 = self.yMean - (self.B1 * xMean)
        i = i+1

  #Aplicar a equação no X que se recebe como entrada
  def predictEq(self, x):
      return (self.B0 + (self.B1 * x))

  #Aplica o predict em cada item do vetor da entrada
  def predict(self, x):
    return self.B0 + self.B1*x

   #===================================================

   #==============Medição de Erros======================
  def RSS(self, X, y):
       error = 0
       i = 0
       while i < len(X):
           error = error + ((y[i] - self.predictEq(X[i])) ** 2)
           i = i + 1
       return error

  def RSE(self, X, y):
      return (self.RSS(X, y)/len(X)) ** 0.5

  def TSS(self, X, y):
      error = 0
      i = 0
      while(i < len(X)):
          error += ((y[i] - self.yMean) ** 2)
          i += 1
      return error

  def R2(self, X, y):
      return (1 - (self.RSS(X, y)/self.TSS(X, y)))
   #====================================================

#======================================================================================

#=============================Metodo de Regressão Multipla=============================
class MultipleLinearRegressor():
  #construtor
  def __init__(self):
    pass

  #Calculo de B1 e B0
  def fit(self, X, y):
      bias = np.ones((X.shape[0], 1))
      X = np.hstack((bias,X))
      inv = np.linalg.inv(np.matmul(np.transpose(X),X))
      self.b = np.matmul(np.matmul(inv,np.transpose(X)), y)

  #Aplicar a equação no X que se recebe como entrada
  def predict(self, X):
      vetorResult = [self.b[0]]
      i = 1
      while i < self.b.shape[0]:
          vetorResult += (self.b[i]*X[:,i-1])
          i = i+1
      return vetorResult

  def RSS(self, X):
        totalSum = 0
        for i in range(0, X.shape[0]):
           roundMinus = y[i]
           for j in range(0, self.b.shape[0]-1):
               roundMinus = roundMinus - (self.b[j]*X[i,j])
           totalSum = totalSum + (roundMinus ** 2)
        return totalSum

  def RSE(self, X):
      return (self.RSS(X)/len(X)) ** 0.5

  def TSS(self, X):
      error = 0
      i = 0
      while(i < len(X)):
          error += ((y[i] - np.mean(y)) ** 2)
          i += 1
      return error

  def R2(self, X):
      return (1 - (self.RSS(X)/self.TSS(X)))

   #===================================================

#==========================================================================================

# Simplifica chamada do objeto
slr = SampleLinearRegressor()
mlr = MultipleLinearRegressor()

print(dados)
#================================Front===============================================================================
print("Regressão Linear")
option = input("Opções: 0 - TV x Sales |  1 - Radio x Sales  |  2 - Newspaper x Sales  |  3 - Investimentos x Sales (Regressao Multipla)\
    ")

# Treinar meu modelo usando fit de acordo com a opção
if option == '0' or option == '1' or option == '2':
    slr.fit(X[:,int(option)], y)

    #Plotagem=======================================================================

    #De acordo com a opcao selecionada, produz o plot
    if option == '0':
        plt.title("TVs X Sales - B0: " + str(slr.B0) + " - B1: " + str(slr.B1))
        plt.xlabel("TV")
        plt.ylabel("Sales")
        plt.plot(dados[:,1], y, 'go')
        plt.plot(slr.predict(np.array(range(1,300))))

        # Medir erros
        print("RSS: " + str(slr.RSS(dados[:,1], y)))
        print("RSE: " + str(slr.RSE(dados[:,1], y)))
        print("R2: " + str(slr.R2(dados[:,1], y)))
        print("TSS: " + str(slr.TSS(dados[:,1], y)))

    elif option == '1':
        plt.title("Radio X Sales - B0: " + str(slr.B0) + " - B1: " + str(slr.B1))
        plt.xlabel("Radio")
        plt.ylabel("Sales")
        plt.plot(dados[:,2], y, 'go')
        plt.plot(slr.predict(np.array(range(1,50))))

        # Medir erros
        print("RSS: " + str(slr.RSS(dados[:,2], y)))
        print("RSE: " + str(slr.RSE(dados[:,2], y)))
        print("R2: " + str(slr.R2(dados[:,2], y)))
        print("TSS: " + str(slr.TSS(dados[:,2], y)))

    elif option == '2':
        plt.title("Newspaper X Sales - B0: " + str(slr.B0) + " - B1: " + str(slr.B1))
        plt.xlabel("Newspaper")
        plt.ylabel("Sales")
        plt.plot(dados[:,3], y, 'go')
        plt.plot(slr.predict(np.array(range(1,120))))

        # Medir erros
        print("RSS: " + str(slr.RSS(X, y)))
        print("RSE: " + str(slr.RSE(X, y)))
        print("R2: " + str(slr.R2(X, y)))
        print("TSS: " + str(slr.TSS(X, y)))

    # B1 e B0
    print("B1: " + str(slr.B1))
    print("B0: " + str(slr.B0))

    # Plotar na Tela
    plt.show()

elif option == '3':
    mlr.fit(X, y)
    plt.title("Regressão Linear Múltipla")
    plt.xlabel("Investimentos")
    plt.ylabel("Sales")
    plt.plot(mlr.predict(X), 'go')
    plt.show()

    print(mlr.predict(X))

    print("B3: " + str(mlr.b[3]))
    print("B2: " + str(mlr.b[2]))
    print("B1: " + str(mlr.b[1]))
    print("B0: " + str(mlr.b[0]))

    print("RSS: " + str(mlr.RSS(X)))
    print("RSE: " + str(mlr.RSE(X)))
    print("TSS: " + str(mlr.TSS(X)))
    print("R2: " + str(mlr.R2(X)))

else:
    print("nao foi")

#=====================================================================================


import sqlite3

db = sqlite3.connect('sqltest.db')
ptr = db.cursor()
QUERY="INSERT INTO test (ID,Nombre,DNI) VALUES (NULL, ?, ?);"

print("Ingrese nombre")
a = raw_input()
print("Ingrese DNI")
b = raw_input()

ptr.execute(QUERY,(str(a), b))
print(QUERY,(str(a), b))

db.commit()
db.close()


import discord

intents = discord.Intents.default()

client = discord.Client(intents=intents)

# チャンネル入退室時の通知処理
@client.event
async def on_voice_state_update(member, before, after):

    # チャンネルへの入室ステータスが変更されたとき（ミュートON、OFFに反応しないように分岐）
    if before.channel != after.channel:
        # 通知メッセージを書き込むテキストチャンネル（チャンネルIDを指定）
        botRoom = client.get_channel("***********************")

        # 入退室を監視する対象のボイスチャンネル（チャンネルIDを指定）
        # 複数選択が可能
        announceChannelIds = ["****************","+++++++++++++++++++"]

        # 退室通知
        if before.channel is not None and before.channel.id in announceChannelIds:
            await botRoom.send("**" + before.channel.name + "** から、__" + member.name + "__  が抜けました！")
        # 入室通知
        if after.channel is not None and after.channel.id in announceChannelIds:
            await botRoom.send("**" + after.channel.name + "** に、__" + member.name + "__  が参加しました！")

# Botのトークンを指定（デベロッパーサイトで確認可能）
client.run("*************************************************************")


from urllib import response
from django.http import HttpResponse, HttpResponseRedirect, FileResponse
from django.shortcuts import redirect, render
from django.core.files import File
from django.core.files.storage import FileSystemStorage

# Tempory files
import tempfile
import os

from .forms import ReaderForm, DataForm

# Imaginary function to handle an uploaded file.
from .lectorWhatsapp import Archivo

def index(response):
    return render(response, 'main/index.html')

def forms(response):
    if response.method == 'POST':
        form = DataForm(response.POST)
        if form.is_valid():
            result = []
            size = int(form.cleaned_data.get('size'))
            redirect_file = False
            for option in range(size):
                actual_option = form.cleaned_data.get(f'options_{option+1}')
                option_result = {}
                option_result['option'] = actual_option

                if (actual_option=='2'):
                    option_result['words'] = form.cleaned_data.get('words')
                    option_result['letters'] = form.cleaned_data.get('letters')
                elif (actual_option=='3'):
                    option_result['date'] = form.cleaned_data.get('date')
                    option_result['cant'] = form.cleaned_data.get('cant')
                elif (actual_option=='4'):
                    option_result['phrase'] = form.cleaned_data.get('phrase')
                elif (actual_option=='1'):
                    ## Redirect to show file
                    redirect_file = True

                result.append(option_result)

            response.session['redirect_file'] = redirect_file
            response.session['data'] = result

        return redirect('upload')
    else:
        form = DataForm()

    return render(response, 'main/forms.html', {'form': form})

def upload(response):

    # Redirect if not exist the data
    data = response.session.get('data', None)
    if data==None:
        return redirect('forms')
    redirect_file = response.session.get('redirect_file', None)

    if response.method == 'POST':
        form = ReaderForm(response.POST, response.FILES)
        if form.is_valid():
            file = form.cleaned_data.get('file')
            obj = form.save(commit=False)
            obj.file = file
            obj.save()

            file = Archivo(obj.file.path)

            content = []

            # Analize data
            for i,action in enumerate(data):
                if int(action['option']) == 1:

                    # Tempory file
                    tmp = tempfile.NamedTemporaryFile()

                    fd, path = tempfile.mkstemp()
                    try:
                        with os.fdopen(fd, 'w+') as tmp:
                            # do stuff with temp file
                            file.generateFile(tmp)
                            tmp.seek(0)
                            content.append((1,tmp.read()))
                    finally:
                        os.remove(path)

                elif int(action['option']) == 2:
                    words = data[i].get('words')
                    letters = data[i].get('letters')
                    results = file.bestWords(int(words), int(letters))

                    content.append((2,results))

                elif int(action['option']) == 3:
                    periods = ["day", "month", "year", "hour"]
                    date = periods[int(data[i].get("date"))-1]
                    cant = data[i].get("cant")

                    results = file.frequencyPeriod(tuple=(date,int(cant)))

                    content.append((3,results))

                elif int(action['option']) == 4:
                    phrase = data[i].get('phrase')

                    results = file.countPhrases(phrase)

                    content.append((4,results))

                elif int(action['option']) == 5:
                    results = file.mostTalked()

                    content.append((5,results))

                elif int(action['option']) == 6:
                    results = file.returnTupleWordPeople(10)

                    content.append((6,results))

                elif int(action['option']) == 7:
                    results = file.messageDelete()

                    content.append((7,results))
            del file
            os.remove(obj.file.path)

            response.session['results'] = content;
            return redirect('results')

    else:
        form = ReaderForm()
    return render(response, 'main/upload.html', {'form': form, 'redirect_file': redirect_file})

def results(response):

    # Redirect if not exist the data
    redirect_file = response.session.get('redirect_file', None)

    results = response.session.get('results', None)
    if results==None:
        return redirect('forms')

    send_dict = {'results': results, 'check': False}

    if redirect!=None:
        send_dict['redirect'] = redirect
        send_dict['check'] = True

    return render(response, 'main/results.html', send_dict)

def file_result(response):
    data = response.session.get('results')
    if data==None:
        return redirect('forms')

    content = None
    for i in range(len(data)):
        if (int(data[i][0])==1):
            content = data[i][1]

    if content==None:
        return redirect('forms')

    return HttpResponse(content, content_type="text/plain")


from contextlib import contextmanager, asynccontextmanager
from unittest.mock import Mock

from pytest import mark, raises

from injector.exiter import DefaultExiter

@mark.asyncio
async def test_exiter(mocker):
    @contextmanager
    def ctx():
        nonlocal var
        var += 1
        yield
        var -= 1

    @asynccontextmanager
    async def actx():
        nonlocal var
        var += 1
        yield
        var -= 1

    var = 0
    with raises(AssertionError):
        with DefaultExiter() as sync_exiter:
            assert sync_exiter._async is False
            ctx_, actx_ = ctx(), actx()
            ctx_.__enter__(), await actx_.__aenter__()
            assert var == 2
            sync_exiter.register_exit(ctx_.__exit__)
            sync_exiter.register_exit(actx_.__aexit__)
    var = 0
    async with DefaultExiter() as async_exiter:
        assert async_exiter._async is True
        ctx_, actx_ = ctx(), actx()
        ctx_.__enter__(), await actx_.__aenter__()
        assert var == 2
        async_exiter.register_exit(ctx_.__exit__)
        async_exiter.register_exit(actx_.__aexit__)
    assert var == 0

    exiter = DefaultExiter()
    exiter.register_exit(Mock())
    warn = mocker.patch("warnings.warn")
    del exiter
    warn.assert_called_once()


from tkinter import *
from tkinter import messagebox
from bin.settings import update_settings
from bin.gui_components.advanced_settings_window import AdvancedSettingsWindow

class SettingsFrame(LabelFrame):
    def __init__(self, parent, scale_factor=1):
        super().__init__(parent, text="Settings")
        self.parent = parent
        self.scale_factor = scale_factor

        self.reset_button = Button(self, text='Reset settings', command=self.reset_settings)
        self.reset_button.pack(side=LEFT, padx=30)

        self.advanced_settings = Button(self, text='Advanced settings', command=self.open_advanced_settings)
        self.advanced_settings.pack(side=RIGHT, padx=30)

        self.pack(fill='x', pady=4, ipady=4)

    def reset_settings(self):
        answer = messagebox.askyesno(title='Confirm', message="Are you sure you want to reset connection settings?")
        if answer:
            self.parent.reset_settings()
            update_settings("", 0, "", "")

    def open_advanced_settings(self):
        AdvancedSettingsWindow(self.parent, self.scale_factor)


import collections

import numpy as np
import webcolors

from moldesign import units as u

class VolumetricGrid(object):
    """
    Helper object for preparing gaussian CUBE files
    """
    UNITS = u.angstrom
    def __init__(self, positions, padding=2.5*u.angstrom, npoints=25):
        mins = positions.min(axis=0) - padding
        maxes = positions.max(axis=0) + padding
        self.npoints = npoints
        self.xr = (mins[0], maxes[0])
        self.yr = (mins[1], maxes[1])
        self.zr = (mins[2], maxes[2])
        self._origin = mins.value_in(self.UNITS)
        self.dx = (self.xr[1] - self.xr[0]).value_in(self.UNITS) / (float(npoints) - 1)
        self.dy = (self.yr[1] - self.yr[0]).value_in(self.UNITS) / (float(npoints) - 1)
        self.dz = (self.zr[1] - self.zr[0]).value_in(self.UNITS) / (float(npoints) - 1)
        self.fxyz = None

    def xyzlist(self):
        stride = self.npoints * 1j
        grids = np.mgrid[self.xr[0]:self.xr[1]:stride,
                self.yr[0]:self.yr[1]:stride,
                self.zr[0]:self.zr[1]:stride]
        return grids * self.UNITS

    def origin(self):
        return tuple(self._origin)

def get_all_atoms(*objects):
    """ Given Atoms, AtomContainers, lists of Atoms, and lists of AtomContainers,
    return a flat list of all atoms contained therein.

    A given atom is only returned once, even if it's found more than once.

    Args:
        *objects (moldesign.Atom OR moldesign.AtomContainer OR List[moldesign.Atom] OR
            List[moldesign.AtomContainer]): objects to take atoms from

    """
    from moldesign import molecules

    atoms = collections.OrderedDict()

    for obj in objects:
        if isinstance(obj, molecules.Atom):
            atoms[obj] = None
        elif hasattr(obj, 'atoms'):
            atoms.update((x,None) for x in obj.atoms)
        else:
            for item in obj:
                if isinstance(item, molecules.Atom):
                    atoms[item] = None
                elif hasattr(item, 'atoms'):
                    atoms.update((x, None) for x in item.atoms)

    return molecules.AtomList(atoms.iterkeys())

def kinetic_energy(momenta, masses):
    return 0.5 * (momenta*momenta/masses).sum()

def kinetic_temperature(ke, dof):
    from moldesign.units import k_b
    t = (2.0*ke)/(k_b*dof)
    return t.defunits()

# def get_residues(obj, **queries):
#     """
#
#     Args:
#         obj ():
#         **queries ():
#
#     Returns:
#
#     """
#     for residue in obj.residues:
#         pass
#

DEF_CATEGORICAL = 'Paired'
DEF_SEQUENTIAL = None  # should be inferno, but that's only MPL >1.5

def colormap(cats, mplmap='auto'):
    # should make it easy to choose one for:
    #  categorical data
    #  sequential (low, high important)
    #  diverging data (low, mid, high important)
    # Can deal with numerical and categorical data
    # we'll treat ints as categories for now
    global DEF_SEQUENTIAL
    from matplotlib import cm

    if hasattr(cm, 'inferno'):
        DEF_SEQUENTIAL = 'inferno'
    else:
        DEF_SEQUENTIAL = 'BrBG'

    # strip units
    units = None
    if hasattr(cats[0], 'magnitude'):
        arr = u.array(cats)
        units = arr.units
        cats = arr.magnitude

    if not isinstance(cats, np.ndarray) and not isinstance(cats[0], float):  # treat as
        # categorical
        values = np.zeros(len(cats), dtype='float')
        to_int = collections.OrderedDict()
        for i, item in enumerate(cats):
            if item not in to_int:
                to_int[item] = len(to_int)
            values[i] = to_int[item]
        if mplmap == 'auto':
            mplmap = DEF_CATEGORICAL
    else:  # it's numerical
        values = np.array(cats, dtype='float')
        if mplmap == 'auto':
            mplmap = DEF_SEQUENTIAL

    cmap = getattr(cm, mplmap)
    mx = values.max()
    mn = values.min()
    r = (values - mn) / (mx - mn)  # rescale to [0.0,1.0]
    rgb = cmap(r)
    hexcolors = [webcolors.rgb_to_hex(np.array(r[:3]) * 256) for r in rgb]
    return hexcolors


import os
import numpy as np
import cv2
from PIL import Image
from torch.utils import data
from torchvision import transforms
from torchvision.transforms.functional import to_tensor
from config.parameters import *
import torch
import random
from .rec_img_aug import warp

nums = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
lower_char = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
upper_char = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

# 将str标签转化为数值label
def StrtoLabel(Str):
    # print(Str)
    label = []
    for i in range(0, charNumber):
        if Str[i] >= '0' and Str[i] <= '9':
            # ord函数返回ASCII码值
            label.append(ord(Str[i]) - ord('0'))
        elif Str[i] >= 'a' and Str[i] <= 'z':
            label.append(ord(str(Str[i])) - ord("a") + 10)
        else:
            label.append(ord(str(Str[i])) - ord("A") + 36)
    return label

# 将label转化为标签
def LabeltoStr(Label):
    Str = ""
    for i in Label:
        if i <= 9:
            Str += chr(ord('0') + i)
        elif i <= 35:
            Str += chr(ord('a') + i - 10)
        else:
            Str += chr(ord('A') + i - 36)
    return Str

# 加载数据集
class Captcha(data.Dataset):
    # 输入已经读取的CSV表格, 图片地址, 是否测试集
    def __init__(self, csv, img_path, data_mode='train'):
        # 表格数据
        self.data = csv
        # 图片地址
        self.img_path = img_path
        # 是否是测试集
        self.data_mode = data_mode
        # 图像处理
        self.transform = transforms.Compose([
            transforms.Resize((ImageHeight, ImageWidth)),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.843, 0.846, 0.842], std=[0.196, 0.193, 0.198])
        ])

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

    def __getitem__(self, index):
        # 获取图片名称及label
        ImgName, label = self.data.iloc[index, :]
        # 获取某一张图片地址
        imgPath = os.path.join(self.img_path, ImgName)
        # 打开图片 (有的图片4通道)
        # image = Image.open(imgPath).convert('RGB')
        image = cv2.cvtColor(cv2.imread(imgPath), cv2.COLOR_BGR2RGB)

        if self.data_mode == 'train':
            # 训练集要做数据增强
            image = warp(image)
            # resize + Norm归一化
            image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
            image = self.transform(image)
            # image = to_tensor(image)
            labelTensor = torch.Tensor(StrtoLabel(label))
            return image, labelTensor
        elif self.data_mode == 'val':
            # 验证集要有标签
            image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
            image = self.transform(image)
            # image = to_tensor(image)
            # 将标签转化为数值label
            labelTensor = torch.Tensor(StrtoLabel(label))
            return image, labelTensor
        elif self.data_mode == 'test':
            # 测试集正常处理
            image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
            image = self.transform(image)
            # image = to_tensor(image)
            return image, ImgName


from typing import List

class Solution:
    def longestCommonPrefix(self, strs: List[str]):
        strs_size = len(strs)

        if strs_size == 0:
            return ""

        prefix = strs[-1]
        if prefix == "":
            return ""
        for word in strs:
            while not word.startswith(prefix):
                prefix = prefix[:-1]
                if prefix == "":
                    return ""
        return prefix

if __name__ == '__main__':
    strs = ["flower", "flow", "flight"]
    print(Solution().longestCommonPrefix(strs))

# Description od this question:
# Цикл, проверка, начинается ли текущий элемент с (prefix)
# если нет, удалите с конца prefix,
# до тех пор, пока .startswith (prefix) не будет равно True

# в ( prefix = strs[-1] ) мы начинаем проверять элементы с конца

# В контексте f.readline()[:-1]это означает: «Я почти уверен, что строка заканчивается новой строкой,
# и я хочу ее удалить».

# Когда объект является строкой, len()функция возвращает количество символов в строке.

# https://leetcode.com/problems/longest-common-prefix/


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def closestValue(self, root: Optional[TreeNode], target: float) -> int:
        self.minv = float('inf')
        self.ans = 0

        def dfs(root):
            if not root:
                return

            if abs(root.val - target) < self.minv:
                self.minv = abs(root.val - target)
                self.ans = root.val
            if abs(root.val - target) == self.minv:
                self.ans = min(self.ans, root.val)

            dfs(root.left)
            dfs(root.right)

        dfs(root)
        return self.ans


# -*- coding: utf-8 -*-

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1)
import tensorflow as tf
tf.set_random_seed(1)
from tensorflow.contrib.data import Dataset
from tensorflow.contrib.layers import xavier_initializer
from tensorflow.examples.tutorials.mnist import input_data

CONV_1_FILTER_SIZE = 5
CONV_1_CHANNEL_N = 32
CONV_2_FILTER_SIZE = 5
CONV_2_CHANNEL_N = 64
BATCH_SIZE = 64
EPOCH_N = 5

def gen_noise_dataset(mnist_dataset):
    """Generate a TF Dataset with additional "noisy data" as the 11th class.

    :param mnist_dataset: mnist.DataSet, which has attributes images/labels
    :return: TF (API) Dataset
    """

    # Create noisy data, which cannot be seen as a valid digit (almost)
    feature_n = mnist_dataset.images.shape[1]
    noisy_n = mnist_dataset.num_examples
    noisy_X = np.random.rand(noisy_n, feature_n)
    noisy_y = np.zeros((noisy_n, 11))
    noisy_y += np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])

    # Expand the original data labels to 11 classes
    orig_y = np.hstack(
        (mnist_dataset.labels,
         np.zeros((mnist_dataset.num_examples, 1))))

    final_dataset = Dataset.from_tensor_slices((
        np.vstack((mnist_dataset.images, noisy_X)),
        np.vstack((orig_y, noisy_y))
    ))

    assert final_dataset.output_shapes[0] == (784, )
    assert final_dataset.output_shapes[1] == (11, )

    return final_dataset

def create_weight_var(shape, initializer=xavier_initializer(seed=1)):
    """Create TF Variable for weights

    :param shape: shape of the variable
    :param initializer: (optional) by default, xavier initializer
    :return: the TF trainable variable
    """
    return tf.get_variable(name='W', shape=shape, initializer=initializer)

def create_bias_var(shape, initializer=tf.zeros_initializer()):
    """Create TF Variable for bias

    :param shape: shape of the variable
    :param initializer: (optional) by default, initialize to 0's
    :return: the TF trainable variable
    """
    return tf.get_variable(name='b', shape=shape, initializer=initializer)

def build_cnn_graph(X, keep_prob, class_n):
    """Build a CNN model using placeholders/input

    :param X: TF placeholder
    :param keep_prob: TF placeholder for (1 - dropout rate)
    :param class_n: class number, int.
    :return: A dictionary of TF Variables in this CNN model
    """

    X_image = tf.reshape(X, (-1, 28, 28, 1))

    # Layer 1: Conv
    # https://www.tensorflow.org/api_docs/python/tf/nn/conv2d
    with tf.variable_scope('conv1'):
        W_conv1 = create_weight_var((CONV_1_FILTER_SIZE, CONV_1_FILTER_SIZE,
                                     1, CONV_1_CHANNEL_N))
        b_conv1 = create_bias_var([CONV_1_CHANNEL_N])
    h_conv1 = tf.nn.relu(
        tf.nn.conv2d(X_image, W_conv1, strides=[1, 1, 1, 1], padding='SAME')
        + b_conv1
    )
    # https://www.tensorflow.org/api_docs/python/tf/nn/max_pool
    h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                             padding='SAME')

    # Layer 2: Conv
    with tf.variable_scope('conv2'):
        W_conv2 = create_weight_var((CONV_2_FILTER_SIZE, CONV_2_FILTER_SIZE,
                                     CONV_1_CHANNEL_N, CONV_2_CHANNEL_N))
        b_conv2 = create_bias_var([CONV_2_CHANNEL_N])
    h_conv2 = tf.nn.relu(
        tf.nn.conv2d(h_pool1, W_conv2, strides=[1, 1, 1, 1], padding='SAME')
        + b_conv2
    )
    h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                             padding='SAME')
    # h_pool2 is Tensor("MaxPool_1:0", shape=(?, 7, 7, 64), dtype=float32)

    # Layer 3: Fully connected
    h_pool2_flat = tf.reshape(h_pool2, (-1, 7*7*CONV_2_CHANNEL_N))
    with tf.variable_scope('fc1'):
        W_fc1 = create_weight_var((7*7*CONV_2_CHANNEL_N, 1024))
        b_fc1 = create_bias_var([1024])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    # Layer 4: final layer (for softmax later)
    with tf.variable_scope('fc2'):
        W_fc2 = create_weight_var((1024, class_n))
        b_fc2 = create_bias_var([class_n])
    y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2

    # FIXME: is this a good idea?
    return {'y_conv': y_conv,
            'h_conv1': h_conv1,
            'h_conv2': h_conv2,
            'h_fc1': h_fc1}

def get_highact_images(degrees_tensor, X, keep_prob, outname, first_n=None,
                       debug_W_filters=None, debug_b_filters=None,
                       sort_by_degree=True):
    """Generate a image leading to the highest activation of a filter/neuron

    :param degrees_tensor: A 1D Tensor of degrees of interests
    :param X: TF placeholder from the model graph
    :param keep_prob: TF placeholder from the model graph
    :param outname: Filename of the group of result images
    :param first_n: Pick the first N filters/neurons (not necessarily highest
                    degree, just the first N indices)
    :param debug_W_filters: the "W" in np.ndarray of the conv layer
    :param debug_b_filters: the "b" in np.ndarray of the conv layer
    :param sort_by_degree: sort the result images or not
    :return:
    """
    if first_n is None:
        first_n = degrees_tensor.shape.as_list()[0]

    image_and_scores = []
    for filter_i in range(first_n):
        print('Generating image for filter {}'.format(filter_i))
        # X_init = np.ones((1, 784)) * 0.5
        X_init = np.random.rand(1, 784)

        degree_tensor = degrees_tensor[filter_i]
        dlc2_dx = tf.gradients(degree_tensor, X)
        # gradient ascent to find the argmax input image
        # FIXME: fixed iteration number: should've used diff < epsilon
        for i in range(3000):
            degree, grad_list = tf.get_default_session().run(
                [degree_tensor, dlc2_dx], feed_dict={X: X_init, keep_prob: 1.0})
            # tf.gradients returns a list of size 1 even when there's one tensor
            grad = grad_list[0]
            X_init += 0.05 * grad
            # make it a realistic image rather than scaling afterwards
            np.clip(X_init, 0.0, 1.0, out=X_init)
            # if i % 100 == 0:
            #     print('layer activation degree: {}'.format(degree))

        if debug_b_filters is not None and debug_W_filters is not None:
            image_and_scores.append((X_init, degree,
                                     debug_W_filters[:, filter_i].reshape(5, 5),
                                     debug_b_filters[filter_i]))
        else:
            image_and_scores.append((X_init, degree, None, None))

    if sort_by_degree:
        image_and_scores = sorted(image_and_scores, key=lambda t: t[1],
                                  reverse=True)

    for i, (im, sc, w, b) in enumerate(image_and_scores):
        plt.subplot(int(np.ceil(first_n/6)), 6*2, i*2+1)
        plt.axis('off')
        plt.title('d:{:.2f}'.format(sc), fontsize=6)
        plt.imshow(im.reshape(28, 28), cmap='gray', vmin=0, vmax=1)

        if w is not None and b is not None:
            plt.subplot(int(np.ceil(first_n/6)), 6*2, i*2+2)
            plt.axis('off')
            abs_max = np.max(np.abs(w))
            color_norm = matplotlib.colors.Normalize(-abs_max, abs_max)
            plt.title('w:{:.3f}\nb:{:.3f}'.format(np.sum(w), b), fontsize=6)
            plt.imshow(w, cmap=plt.cm.coolwarm, norm=color_norm)

    # plt.tight_layout()
    plt.savefig('{}.png'.format(outname))
    plt.close()

    return image_and_scores

def predict_data(mnist_dataset, add_negative, accuracy_op):
    """Predict a test data set using the metric in a TF operator

    :param mnist_dataset: mnist_data.test
    :param add_negative: add noisy data in test or not
    :param accuracy_op: the TF operator for accuracy
    :return:
    """
    if add_negative:
        test_labels = np.hstack(
            (mnist_dataset.labels,
             np.zeros((mnist_dataset.num_examples, 1))))
    else:
        test_labels = mnist_dataset.labels

    acc = accuracy_op.eval(feed_dict={
        'X:0': mnist_dataset.images, 'y:0': test_labels, 'keep_prob:0': 1.0})
    print('test accuracy {:.4f}'.format(acc))

def main(add_negative):
    mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True)
    if add_negative:
        train_set = gen_noise_dataset(mnist_data.train)
        class_n = 11
    else:
        train_set = Dataset.from_tensor_slices(
            (mnist_data.train.images, mnist_data.train.labels))
        class_n = 10

    print('Done generating dataset')

    # Input placeholder
    X = tf.placeholder(tf.float32, shape=(None, 28*28), name='X')
    keep_prob = tf.placeholder(tf.float32, name='keep_prob')
    y = tf.placeholder(tf.float32, shape=(None, class_n), name='y')

    # Model
    model_var_dict = build_cnn_graph(X, keep_prob=keep_prob, class_n=class_n)
    y_conv = model_var_dict['y_conv']
    h_conv2 = model_var_dict['h_conv2']

    # metrics
    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits=y_conv, labels=y))
    correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # Train OP
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

    # Tensorboard summary
    tf.summary.scalar('cross entropy', cross_entropy)
    tf.summary.scalar('training accuracy', accuracy)
    tf.summary.histogram(
        'L2 activation degree by filters',
        tf.reduce_sum(tf.reduce_mean(h_conv2, axis=0), axis=[0, 1]))
    merged_summary = tf.summary.merge_all()

    # RUN!
    dataset = train_set
    dataset = dataset.shuffle(buffer_size=8192)
    dataset = dataset.batch(BATCH_SIZE)
    iterator = dataset.make_initializable_iterator()
    next_batch = iterator.get_next()

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        summary_writer = tf.summary.FileWriter('mnist/logs/', sess.graph)

        step_i = 0
        for epoch_i in range(EPOCH_N):
            sess.run(iterator.initializer)
            while True:
                try:
                    batch_X, batch_y = sess.run(next_batch)
                    sess.run(train_step,
                             feed_dict={X: batch_X, y: batch_y, keep_prob: 0.5})

                    # monitor
                    if step_i % 100 == 0:
                        train_accuracy, summary = sess.run(
                            [accuracy, merged_summary],
                            feed_dict={X: batch_X, y: batch_y, keep_prob: 1.0})
                        print('step {}, training accuracy {}'.format(
                            step_i, train_accuracy))
                        summary_writer.add_summary(summary, step_i)

                    step_i += 1
                except tf.errors.OutOfRangeError:
                    break

        # predict
        predict_data(mnist_data.test, add_negative, accuracy)

        # Visualize: what input images lead to high activation?
        debug_W1, debug_b1, debug_W2, debug_b2 = sess.run(
            ['conv1/W:0', 'conv1/b:0', 'conv2/W:0', 'conv2/b:0'])
        fn_prfx = '11_' if add_negative else '10_'

        # reduce mean axis=0 for eliminate mini-batch dimension
        h_conv1 = model_var_dict['h_conv1']
        degrees = tf.reduce_sum(tf.reduce_mean(h_conv1, axis=0), axis=[0, 1])
        get_highact_images(
            degrees, X, keep_prob, fn_prfx+'max_conv1', debug_b_filters=debug_b1,
            debug_W_filters=debug_W1.reshape(-1, CONV_1_CHANNEL_N))

        h_conv2 = model_var_dict['h_conv2']
        degrees = tf.reduce_sum(tf.reduce_mean(h_conv2, axis=0), axis=[0, 1])
        # average L2 filter over input (L1) depth when showing in 2D
        debug_W2 = np.mean(debug_W2, axis=2)
        get_highact_images(
            degrees, X, keep_prob, fn_prfx+'max_conv2', debug_b_filters=debug_b2,
            debug_W_filters=debug_W2.reshape(-1, CONV_2_CHANNEL_N), first_n=30)

        h_fc1 = model_var_dict['h_fc1']
        degrees = tf.reduce_mean(h_fc1, axis=0)
        get_highact_images(degrees, X, keep_prob, fn_prfx+'max_fc1', first_n=30)

        y_conv = model_var_dict['y_conv']
        degrees = tf.reduce_mean(y_conv, axis=0)
        img_and_scores = get_highact_images(
            degrees, X, keep_prob, fn_prfx+'max_y', sort_by_degree=False)
        for i, (im, sc, w, b) in enumerate(img_and_scores):
            y_out = sess.run(y_conv, feed_dict={X: im.reshape(1, 28*28),
                                                keep_prob: 1.0})
            print('{}: {}'.format(i, np.exp(y_out)/np.sum(np.exp(y_out))))

if __name__ == '__main__':
    main(add_negative=False)
    # main(add_negative=True)


from crypt import methods
from unicodedata import name
import redis
from flask import jsonify,request
from rq import Worker, Queue
import time
from tasks.task1 import count_words_at_url, get_all_db, add_db
from datetime import datetime
from core.coreWorker import conn
from model.city import City
from main import app, db

# datetime object containing current date and time
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")

# Routes
@app.route('/', methods=['GET']) # home
def home():
    return None

@app.route('/gets',methods=['GET','POST'])
def gedata():
    # datetime object containing current date and time

    now = datetime.now()

    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")

    # creates the job
    q = Queue(connection=conn)
    workers = Worker.all(connection=conn)
    job = q.enqueue(count_words_at_url, 'http://nvie.com', result_ttl=5000)
    job.meta['creation_date'] = dt_string
    job.save_meta()

    # Returns the data of the job
    json = []
    json.append(
        {
            "value: ": job.result,
            "job_ID: ": job.id,
            "status: ": job.get_status(),
            "name":"hey",
            "x":1,
            "y":1
        }
    )
    time.sleep(2)
    json.append(
        {
            "value: ": job.result,
            "job_ID: ": job.id,
            "status: ": job.get_status(),
            "name":"hey",
            "x":1,
            "y":1,
            "worker":workers[0].name
        }
    )
    job2 = q.jobs
    for q in job2:
            resQ = None
            try:
                resQ = q.meta['creation_date']
            except: # this will only mean that the meta is not visible
                resQ = 'No creation date added'
            json.append(
            {
                "allJbs": {
                    "value: ": q.result,
                    "job_ID: ": q.id,
                    "status: ": q.get_status(),
                    "creation date: ": resQ
                }
            })
    # print("ressssssss",job2.result)
    for x in workers:
        for q in job2:
            resQ = None
            try:
                resQ = q.meta['creation_date']
            except: #this will only mean that the meta is not visible
                resQ = 'No creation date added'
            json.append(
            {
                "worker": {
                    "worker Name: ":x.name,
                    "worker State: ":x.state,
                    "working time: ":x.total_working_time,
                    "fail rate: ":x.failed_job_count,
                    "success rate":x.successful_job_count
                },
                "queue": {
                    "value: ": q.result,
                    "job_ID: ": q.id,
                    "status: ": q.get_status(),
                    "creation date: ": resQ
                }
            }
    )
    return jsonify(json)

@app.route('/add',methods=['POST'])
def add():
    data = request.get_json()
    # q = Queue(connection=conn)
    # job = q.enqueue(add_db, data, result_ttl=5000)
    # job.meta['creation_date'] = dt_string
    # job.save_meta()
    # status = job.result

    # city = City(data['city'], data['coords'])
    # db.session.add(city)
    # db.session.commit()
    city = City(data['city'], data['coords'])
    db.session.add(city)
    db.session.commit()
    # status = add_db(data)
    return jsonify({"status":"200"})

@app.route('/search',methods=['POST'])
def search():
    data = request.get_json()
    query_city = City.query.filter(City.name.like(f"{data['city']}%"))
    newCityData = []
    for city in query_city:
        newCityData.append({
            "id": city.id,
            "name": city.name,
            "coordinate_x": city.coordinate_x,
            "coordinate_y": city.coordinate_y
        })
    return jsonify({"city":newCityData})

@app.route('/all',methods=['GET'])
def all():
    # Create redis queue
    q = Queue(connection=conn)
    job = q.enqueue(get_all_db,job_id='queue get', result_ttl=5000)
    job.meta['creation_date'] = dt_string
    job.save_meta()
    newCityData = job.result

    # query_city = City.query.all()
    # newCityData = []
    # for city in query_city:
    #     newCityData.append({
    #         "id": city.id,
    #         "name": city.name,
    #         "coordinate_x": city.coordinate_x,
    #         "coordinate_y": city.coordinate_y
    #     })
    return jsonify({"city":newCityData})

@app.route('/db',methods=['GET','POST'])
def db_access():
    query_city = City.query.all()
    json = []
    for value in query_city:
        json.append(
            {
                "name":value.name,
                "x":value.coordinate_x,
                "y":value.coordinate_y
            }
        )
# Start the app as the main
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=3200, debug=True)


from cx_Freeze import setup, Executable

build_options = {
'includes': ['re', 'atexit', 'icons_rc'],
'include_files': [ r'.\bck', r'.\cfg'],
'packages': [],
'excludes': ['tkinter', 'ctypes', 'lzma', 'bz2'],
'include_msvcr': [ '.\libs\vcredist_x86.exe' ]
}

Exet = Executable(
	script = 'main.py',
	initScript = None,
	base = 'Win32GUI',
	targetName = 'Carmageddon.exe',
	compress = True,
	copyDependentFiles = True,
	appendScriptToExe = False,
	appendScriptToLibrary = False,
	icon = 'CmIcon.ico'
)

setup(
	name = 'Carmageddon Mod',
	version = '3.0.1.1',
	author = 'Gipphe',
	description = 'Carmageddon Mod',
	options = {'build_exe': build_options}, executables = [Exet]
)


#http://www.checkio.org/mission/task/destination-in-spiral/

import math

#return the cordinate of num in axis
def get_position(num):
    x = 0
    y = 0
    if num == 1:
        return (x, y)

    xpoint = num - 1
    n = math.ceil(0.5 * (1 + math.sqrt(1 + 4 * xpoint)))

    #delta between xpoint and n*(n+1)

    section = n - 1
    section_even = section % 2 == 0
    delta_in_section = section * (section + 1) - xpoint

    if section_even:
        sum_section = -1 * section / 2
    else: #section is odd
        sum_section = section / 2.0 + 0.5

#    print sum_section, section, delta_in_section

    if delta_in_section >= section:
        #remove section times of -1^(section+1)
        x = sum_section + section * ((-1) ** (section))
        y = sum_section + (delta_in_section - section) * ((-1) ** section)
    else:
        x = sum_section + delta_in_section * ((-1) ** (section))
        y = sum_section

#    print 'pos(' + str(num) + ")=", x, y
    return (x, y)

def checkio(data):
    "Find the destination"
    a, b = data
    xa, ya = get_position(a)
    xb, yb = get_position(b)

    return abs(xb - xa) + abs(yb - ya)

if __name__ == '__main__':

#    for i in range(1,40):
#        get_position(i)

    assert checkio([1, 9]) == 2, "First"
    assert checkio([9, 1]) == 2, "Reverse First"
    assert checkio([10, 25]) == 1, "Neighbours"
    assert checkio([5, 9]) == 4, "Diagonal"
    assert checkio([26, 31]) == 5, "One row"
    assert checkio([50, 16]) == 10, "One more test"


from chatgpt.main import Chatbot
from flask import Flask
from flask import request, jsonify

config = {
#    "proxy": "http://127.0.0.1:1081",
    'session_token':'',
}
chatbot = Chatbot(config, conversation_id=None)

def getMessage(q = '') :
    print('q = ', q)
    resp = chatbot.get_chat_response(q, output='text')
    print('a = ', resp['message'])
    return resp['message']

app = Flask(__name__)

@app.route("/api", methods=['POST'])
def hello_world():
    q = request.json['q']
    msg = getMessage(q = q)
    if '抱歉' in msg :
        msg = ''
    return jsonify(code=200, status=0, message='ok', data={
        'a': msg,
        'q': q,
    })

if __name__ == "__main__":
    app.run(host='0.0.0.0')


import numpy as np
from scipy.special import comb
from skimage import measure
import torch

def compute_ari(table):
    """
    Compute ari, given the index table
    :param table: (r, s)
    :return:
    """

    # (r,)
    a = table.sum(axis=1)
    # (s,)
    b = table.sum(axis=0)
    n = a.sum()

    comb_a = comb(a, 2).sum()
    comb_b = comb(b, 2).sum()
    comb_n = comb(n, 2)
    comb_table = comb(table, 2).sum()

    if (comb_b == comb_a == comb_n == comb_table):
        # the perfect case
        ari = 1.0
    else:
        ari = (
            (comb_table - comb_a * comb_b / comb_n) /
            (0.5 * (comb_a + comb_b) - (comb_a * comb_b) / comb_n)
        )

    return ari

def compute_mask_ari(mask0, mask1):
    """
    Given two sets of masks, compute ari
    :param mask0: ground truth mask, (N0, H, W)
    :param mask1: predicted mask, (N1, H, W)
    :return:
    """

    # will first need to compute a table of shape (N0, N1)
    # (N0, 1, H, W)
    mask0 = mask0[:, None].byte()
    # (1, N1, H, W)
    mask1 = mask1[None, :].byte()
    # (N0, N1, H, W)
    agree = mask0 & mask1
    # (N0, N1)
    table = agree.sum(dim=-1).sum(dim=-1)

    return compute_ari(table.numpy())

def _segmentation_cover(pred, gt):
    regionsGT = []
    regionsPred = []
    total_gt = 0

    cntR = 0
    sumR = 0
    cntP = 0
    sumP = 0

    propsGT = measure.regionprops(gt)
    for prop in propsGT:
        regionsGT.append(prop.area)
    regionsGT = np.array(regionsGT).reshape(-1, 1)
    total_gt = total_gt + np.max(gt)

    best_matchesGT = np.zeros((1, total_gt))

    matches = _match_segmentation(pred, gt)

    matchesPred = np.max(matches, axis=1).reshape(-1, 1)
    matchesGT = np.max(matches, axis=0).reshape(1, -1)

    propsPred = measure.regionprops(pred)
    for prop in propsPred:
        regionsPred.append(prop.area)
    regionsPred = np.array(regionsPred).reshape(-1, 1)

    for r in range(regionsPred.shape[0]):
        cntP += regionsPred[r] * matchesPred[r]
        sumP += regionsPred[r]

    for r in range(regionsGT.shape[0]):
        cntR += regionsGT[r] * matchesGT[:, r]
        sumR += regionsGT[r]

    best_matchesGT = np.maximum(best_matchesGT, matchesGT)

    R = cntR / (sumR + (sumR == 0))
    P = cntP / (sumP + (sumP == 0))

    return R[0], P[0]
def iou_binary(mask_A, mask_B):
    assert mask_A.shape == mask_B.shape
    assert mask_A.dtype == torch.bool
    assert mask_B.dtype == torch.bool
    intersection = (mask_A * mask_B).sum((1, 2, 3))
    union = (mask_A + mask_B).sum((1, 2, 3))
    # Return -100 if union is zero, else return IOU
    return torch.where(union == 0, torch.tensor(-100.0).cuda(),
                       intersection.float() / union.float())

def average_segcover(segA, segB, ignore_background=False):
    """
    Covering of segA by segB
    segA.shape = [batch size, 1, img_dim1, img_dim2]
    segB.shape = [batch size, 1, img_dim1, img_dim2]
    scale: If true, take weighted mean over IOU values proportional to the
           the number of pixels of the mask being covered.
    Assumes labels in segA and segB are non-negative integers.
    Negative labels will be ignored.
    """

    assert segA.shape == segB.shape, f"{segA.shape} - {segB.shape}"
    assert segA.shape[1] == 1 and segB.shape[1] == 1
    bsz = segA.shape[0]
    nonignore = (segA >= 0)

    mean_scores = torch.tensor(bsz*[0.0]).cuda()
    N = torch.tensor(bsz*[0]).cuda()
    scaled_scores = torch.tensor(bsz*[0.0]).cuda()
    scaling_sum = torch.tensor(bsz*[0]).cuda()

    # Find unique label indices to iterate over
    if ignore_background:
        iter_segA = torch.unique(segA[segA > 0]).tolist()
    else:
        iter_segA = torch.unique(segA[segA >= 0]).tolist()
    iter_segB = torch.unique(segB[segB >= 0]).tolist()
    # Loop over segA
    for i in iter_segA:
        binaryA = segA == i
        if not binaryA.any():
            continue
        max_iou = torch.tensor(bsz*[0.0]).cuda()
        # Loop over segB to find max IOU
        for j in iter_segB:
            # Do not penalise pixels that are in ignore regions
            binaryB = (segB == j) * nonignore
            if not binaryB.any():
                continue
            iou = iou_binary(binaryA, binaryB)
            max_iou = torch.where(iou > max_iou, iou, max_iou)
        # Accumulate scores
        mean_scores += max_iou
        N = torch.where(binaryA.sum((1, 2, 3)) > 0, N+1, N)
        scaled_scores += binaryA.sum((1, 2, 3)).float() * max_iou
        scaling_sum += binaryA.sum((1, 2, 3))

    # Compute coverage
    mean_sc = mean_scores / torch.max(N, torch.tensor(1)).float()
    scaled_sc = scaled_scores / torch.max(scaling_sum, torch.tensor(1)).float()

    # Sanity check
    assert (mean_sc >= 0).all() and (mean_sc <= 1).all(), mean_sc
    assert (scaled_sc >= 0).all() and (scaled_sc <= 1).all(), scaled_sc
    assert (mean_scores[N == 0] == 0).all()
    assert (mean_scores[nonignore.sum((1, 2, 3)) == 0] == 0).all()
    assert (scaled_scores[N == 0] == 0).all()
    assert (scaled_scores[nonignore.sum((1, 2, 3)) == 0] == 0).all()

    # Return mean over batch dimension
    return mean_sc.mean(0), scaled_sc.mean(0)

def _match_segmentation(pred, gt):
    total_gt = np.max(gt)
    cnt = 0
    matches = np.zeros((total_gt, np.max(pred)))

    num1 = np.max(gt) + 1
    num2 = np.max(pred) + 1
    confcounts = np.zeros((num1, num2))

    # joint histogram
    sumim = 1 + gt + pred * num1

    hs, _ = np.histogram(sumim.flatten(), bins=np.linspace(1, num1*num2+1, num=num1*num2+1))
    hs = hs.reshape(confcounts.shape[1], confcounts.shape[0]).T

    confcounts = confcounts + hs
    accuracies = np.zeros((num1, num2))

    for j in range(0, num1):
        for i in range(0, num2):
            gtj = np.sum(confcounts[j, :])
            resj = np.sum(confcounts[:, i])
            gtjresj = confcounts[j, i]
            if gtj + resj - gtjresj:
                value = gtjresj / (gtj + resj - gtjresj)
                accuracies[j, i] = value
    matches[cnt:cnt + np.max(gt), :] = accuracies[1:, 1:]

    return matches.T


"""
planex-clone: Checkout sources referred to by a pin file
"""
from __future__ import print_function

import argparse
import json
import logging
from os import getcwd, rename, walk
from os.path import abspath, exists, join, relpath
import re
from string import Template
import shutil
import subprocess
import sys
import tempfile
import tarfile

import git

# pylint: disable=relative-import
from six.moves.urllib.parse import urlparse, urlunparse, parse_qs, urlencode

try:
    from pathlib2 import Path
except ImportError:
    from pathlib import Path

from planex.cmd.args import common_base_parser
from planex.cmd.fetch import fetch_url
from planex.config import Configuration
from planex.link import Link
from planex.util import setup_logging
import planex.spec

SUPPORTED_URL_SCHEMES = ["http", "https"]

def parse_args_or_exit(argv=None):
    """
    Parse command line options
    """
    parser = argparse.ArgumentParser(
        description='Clone package sources',
        parents=[common_base_parser()])
    parser.add_argument("--jenkins", action="store_true",
                        help="Print Jenkinsfile fragment")
    parser.add_argument("--clone", action="store_true",
                        help="Only clone repositories, do not apply patches")
    parser.add_argument("--output", "-o",
                        default=join(getcwd(), "clone_sources.json"),
                        help="Choose output name for clone sources JSON file")
    parser.add_argument("-r", "--repos", metavar="DIR", default="repos",
                        help='Local path to the repositories')
    parser.add_argument("pins", metavar="PINS", nargs="*", help="pin file")
    parser.add_argument("--credentials", metavar="CREDS", default=None,
                        help="Credentials")
    return parser.parse_args(argv)

def find_link_pin(package):
    """
    From a package name locate the link or pin file
    """
    pin_search = Configuration.get('pin', 'search-path',
                                   default='SPECS').split(':')
    for suffix in ('.pin', '.lnk'):
        for subdir in pin_search:
            path = Path(subdir, package+suffix)
            if path.exists():
                return path
    return None

def find_spec(package):
    """
    From a package name locate the spec file
    """
    spec_search = Configuration.get('spec', 'search-path',
                                    default='SPECS').split(':')
    for subdir in spec_search:
        path = Path(subdir, package+'.spec')
        if path.exists():
            return path
    return None

def definitions_for(package):
    """
    Return the package name and paths for link/pin and spec files
    """
    # *.pin gets passed down
    # and wildcard expansion has not occured
    if "*" in package:
        print("No pin files found")
        sys.exit(0)

    if package.endswith('.pin') or package.endswith('.lnk'):
        # link/pin pathname
        link_pin = Path(package)
        package = link_pin.stem
    else:
        # package name
        link_pin = find_link_pin(package)

    spec = find_spec(package)
    if spec is None:
        sys.exit("No spec file for "+package)

    return package, spec, link_pin

CHECKOUT_TEMPLATE = Template("""checkout poll: true,
         changelog: true,
         scm: [$$class: 'GitSCM',
               branches: [[name: '$branch']],
               extensions: [[$$class: 'RelativeTargetDirectory',
                             relativeTargetDir: '$checkoutdir'],
                            [$$class: 'LocalBranch']],
               userRemoteConfigs: [
                 [credentialsId: '$credentials',
                  url: '$url']]]
""")

def clone_jenkins_json(package, filename, url, commitish):
    """
    Print JSON file containing repositories to clone
    """
    json_dict = {}
    if exists(filename):
        with open(filename, "r") as clone_sources:
            json_dict.update(json.load(clone_sources))
    json_dict[package] = {'URL': url, 'commitish': commitish,
                          'watching': 'true'}
    with open(filename, "w") as clone_sources:
        clone_sources.write(json.dumps(json_dict, indent=2, sort_keys=True,
                                       separators=(',', ': ')))
        print(file=clone_sources)

def clone_jenkins_groovy(package, destination, credentials, url, commitish):
    """
    Output Groovy SCM fragment for backwards compatibility
    """
    destination = join(destination, package)
    print(CHECKOUT_TEMPLATE.substitute(url=url,
                                       branch=commitish,
                                       checkoutdir=destination,
                                       credentials=credentials))

def clone(url, destination, commitish):
    """
    Clone git repository and checkout at commitish
    """

    if not destination.parent.exists():
        destination.parent.mkdir(parents=True)
    if destination.exists():
        # Take a fresh clone if we are re-cloning
        shutil.rmtree(str(destination))

    repo = git.Repo.clone_from(url, str(destination))
    if commitish in repo.remotes['origin'].refs:
        branch_name = commitish
        commit = repo.remotes['origin'].refs[commitish]

    elif commitish in repo.tags:
        branch_name = "planex/%s" % commitish
        commit = repo.refs[commitish]

    else:
        branch_name = "planex/%s" % commitish[:8]
        commit = repo.rev_parse(commitish)

    local_branch = repo.create_head(branch_name, commit)
    local_branch.checkout()

    return repo

def find_all_files(path):
    """
    Find all the files in a path (excluding .git contents)
    Git repo.untracked will skip ignores (which we need to
    add on first expansion of a tarball)
    """
    results = []
    for root, _, files in walk(path):
        if '.git' in root:
            continue
        results.extend([abspath(join(root, filename)) for filename in files])

    return results

def extract(url_str, destination):
    """
    Fetch a non-git resource and create a git repository
    """
    archive_file = None
    # pylint: disable=no-member
    destination.mkdir(parents=True, exist_ok=True)
    repo = git.Repo.init(str(destination))

    logging.debug("Extracting %s to new git repo", url_str)
    url = urlparse(url_str)
    if url.scheme in ('', 'file') and url.netloc == '':
        # local file
        archive_path = url.path
    else:
        # remote URL
        archive_file = tempfile.NamedTemporaryFile(prefix='clone-')
        archive_path = archive_file.name

        query = parse_qs(url.query)
        if 'prefix' in query:
            # strip off the directory prefix
            del query['prefix']
            query_str = urlencode(query, doseq=True)
            url_str = urlunparse((url.scheme, url.netloc, url.path, url.params,
                                  query_str, url.fragment))
            url = urlparse(url_str)

        fetch_url(url, archive_path, 5)

    # extract the archive
    if tarfile.is_tarfile(archive_path):
        logging.info("Fetch and extract %s into %s", url_str, destination)
        with tarfile.open(archive_path) as tara:
            tara.extractall(str(destination))
        index = repo.index
        index.add(find_all_files(str(destination)))
        index.commit("Repo generated by planex-clone")

    if archive_file:
        # delete temp file
        archive_file.close()

    return repo

def clone_resource(resource, destination):
    """
    Clone a resource as a git repository
    """
    if resource.is_repo:
        logging.info("Clone and checkout %s@%s into %s", resource.url,
                     resource.commitish, destination)
        repo = clone(resource.url, destination, resource.commitish)
    else:
        repo = extract(resource.url, destination)

    return repo

def clone_jenkins(args, spec):
    """
    Generate either a JSON object or a Groovy fragment that describes
    the git-based resources of a package
    """
    for resource in spec.resources():
        if resource.is_repo:
            package = resource.basename.rsplit(".git")[0]
            url = resource.url
            commitish = resource.commitish

            if args.credentials:
                # output Groovy fragment
                print('echo "Cloning %s#%s"' % (url, commitish))
                clone_jenkins_groovy(package, args.repos, args.credentials,
                                     url, commitish)
            else:
                # output JSON object
                clone_jenkins_json(package, args.output, url, commitish)

def clone_all_git(args, spec):
    """
    Clone all git repositories for a package
    """
    for resource in spec.resources():
        if resource.is_repo:
            # remove trailing '.git'
            destination = Path(args.repos, resource.basename[:-4])
            clone_resource(resource, destination)

def clone_all_fetchable(args, package, spec):
    """
    Clone all remote resources for a package
    """
    for resource in spec.resources():
        if resource.is_fetchable:
            if resource.is_repo:
                # remove trailing '.git'
                destination = Path(args.repos, resource.basename[:-4])
            else:
                destination = Path(args.repos, package)
            clone_resource(resource, destination)

def apply_patchqueue(base_repo, pq_repo, prefix):
    """
    Link and then apply a patchqueue repository to a source repository
    """
    status_path = Path(pq_repo.working_dir, prefix, 'status')
    patches_link = Path(base_repo.git_dir, 'patches',
                        base_repo.active_branch.name)

    # make the directory tree for the patches within the base repo
    # pylint: disable=no-member
    patches_link.parent.mkdir(parents=True)

    # link the patchqueue directory for the base repo branch
    rel_path = relpath(str(status_path.parent), str(patches_link.parent))
    patches_link.symlink_to(rel_path)

    # create an empty status file
    with status_path.open('w'):
        pass

    patches = subprocess.check_output(['guilt', 'series'],
                                      cwd=base_repo.working_dir)
    if patches:
        subprocess.check_call(['guilt', 'push', '--all'],
                              cwd=base_repo.working_dir)

def clone_with_patchq(repos, base_dest, base_res, pq_res):
    """
    Given a source and patchqueue resource clone repos and apply the patchqueue
    """
    base_repo = clone_resource(base_res, base_dest)

    pq_dest = Path(repos, re.sub(r'\.git$', '', pq_res.basename))
    pq_repo = clone_resource(pq_res, pq_dest)
    apply_patchqueue(base_repo, pq_repo, pq_res.prefix)

def archive_resource(resource, destination):
    """
    Write an archive of a resource
    """
    archive_path = Path(destination, resource.basename)
    if resource.is_repo:
        temp_dir = tempfile.mkdtemp(prefix='clone-')
        try:
            repo = clone(resource.url, temp_dir, resource.commitish)
            logging.debug("Archiving %s@%s to %s", resource.url,
                          resource.commitish, archive_path)
            with archive_path.open("wb") as output:
                repo.archive(output, treeish=str(resource.commitish),
                             prefix=resource.prefix)
        finally:
            shutil.rmtree(temp_dir, ignore_errors=True)
    else:
        url = urlparse(resource.url)
        if url.scheme in SUPPORTED_URL_SCHEMES:
            logging.debug("Fetching %s to %s", resource.url, archive_path)
            fetch_url(url, str(archive_path), 5)
        elif url.scheme in ['', 'file'] and url.netloc == '':
            logging.debug("Copying %s to %s", url.path, archive_path)
            shutil.copyfile(url.path, str(archive_path))
    # else: UnsupportedScheme

    return archive_path

def unpack_patches(tarball, destination):
    """
    Extract contents of patches archive
    """
    logging.debug("Unpacking %s to %s", tarball, destination)
    tara = tarfile.open(str(tarball))
    try:
        tara.extractall(str(destination))
    finally:
        tara.close()

def create_repo_from_spec(spec_path, top_path, repo_path):
    """
    Invoke the prep phase of rpmbuild to generate a source directory then
    create a git repo from it
    """
    top_dir = top_path.resolve()
    cmd = ['rpmbuild', '-bp', '--nodeps',
           '--define', '_topdir '+str(top_dir), str(spec_path)]
    logging.debug("Running %s", ' '.join(cmd))
    subprocess.check_call(cmd)

    # move the created build directory under the repo directory
    build_path = list(Path(top_path, 'BUILD').glob('*'))[0]
    rename(str(build_path), str(repo_path))

    git_dir = Path(repo_path, '.git')
    if git_dir.exists():
        # setup already created a git repo
        repo = git.Repo(str(repo_path))
    else:
        repo = git.Repo.init(str(repo_path))
        index = repo.index
        index.add(repo.untracked_files)
        index.commit("Repo generated by planex-clone")

    return repo

def clone_with_patches(spec_path, base_dest, base_res, patches_res, pq_res):
    """
    Given a source, archive and optional patchqueue resource clone repos, apply
    the patches followed by the patchqueue
    """
    work_path = Path(tempfile.mkdtemp(prefix='clone-',
                                      dir=str(base_dest.parent)))
    source_path = Path(work_path, "SOURCES")
    source_path.mkdir(parents=True)

    try:
        archive_resource(base_res, source_path)
        patches_tarball = archive_resource(patches_res, source_path)
        unpack_patches(patches_tarball, work_path)
        base_repo = create_repo_from_spec(spec_path, work_path, base_dest)

        if pq_res:
            pq_dest = Path(str(base_dest)+'.pg')
            pq_repo = clone_resource(pq_res, pq_dest)
            apply_patchqueue(base_repo, pq_repo, pq_res.prefix)

    finally:
        shutil.rmtree(str(work_path), ignore_errors=True)

def get_non_repo_name(source, package):
    """
    Attempt to extract git repository name from archive URL.

    Fallback to package name if no format match found.
    """
    match = re.match(r'.*repos/([^/]*)/archive\?', source)

    if match:
        return match.group(1)

    return package

# pylint: disable=too-many-branches
def main(argv=None):
    """
    Entry point
    """
    args = parse_args_or_exit(argv)
    setup_logging(args)

    for pin in args.pins:
        package, spec_path, link_pin_path = definitions_for(pin)

        link_pin = None
        if link_pin_path:
            logging.debug("Reading link/pin file %s", link_pin_path)
            link_pin = Link(str(link_pin_path))
        logging.debug("Reading spec file %s", spec_path)
        spec = planex.spec.load(str(spec_path), link=link_pin,
                                check_package_name=False)

        if args.clone:
            # just clone git resources
            clone_all_git(args, spec)
        elif args.jenkins:
            # generate Jenkins information
            clone_jenkins(args, spec)
        else:
            resources = spec.resources_dict()
            src_res = resources['Source0']
            if src_res.is_repo:
                # remove trailing '.git'
                repo_path = Path(args.repos,
                                 src_res.basename.rsplit(".git")[0])
            else:
                repo_path = Path(args.repos,
                                 get_non_repo_name(src_res.url, package))
            if not repo_path.parent.exists():
                repo_path.parent.mkdir(parents=True)

            if "PatchQueue0" in resources:
                if "Archive0" in resources:
                    # component with patches and patchqueue
                    clone_with_patches(spec_path, repo_path,
                                       resources['Source0'],
                                       resources['Archive0'],
                                       resources['PatchQueue0'])
                else:
                    # component with patchqueue
                    clone_with_patchq(args.repos, repo_path,
                                      resources['Source0'],
                                      resources['PatchQueue0'])
            elif "Archive0" in resources:
                # component with patches
                clone_with_patches(spec_path, repo_path, resources['Source0'],
                                   resources['Archive0'], None)
            else:
                # clone all fetchable resources
                clone_all_fetchable(args, package, spec)


import os
import pyforms

import harvestlist
import hlpickle

from   pyforms          import BaseWidget
from   pyforms.Controls import ControlText
from   pyforms.Controls import ControlButton
from   pyforms.Controls import ControlLabel
from   pyforms.Controls import ControlList
from   pyforms.Controls import ControlEmptyWidget
from   pyforms.Controls import ControlDir
from   pyforms.Controls import ControlFile
from   pyforms.Controls import ControlSaveFile
from PyQt5 import QtCore

class Main:
    # Main window
    root = None
    # Stores the data of the harvest list
    harvest_list = None

    # The HarvestList class
    hl = harvestlist.HarvestList()

    # Stores the object to be pickled
    hl_pickle = hlpickle.HLPickle()

    output_dir = None
    output_path = None

class mainWindow(BaseWidget):
    def __init__(self):

        super().__init__('Oogstlijst Manager')
        self._panel = ControlEmptyWidget()
        self._panel.value = Home()

        Main.root = self

class Home(BaseWidget):
    def __init__(self):
        super().__init__('Home')

        self.button_new = ControlButton('Nieuwe oogstlijst')
        self.button_open = ControlButton('Open bestaande oogstlijst')
        self.formset = ['button_new', 'button_open']

        self.button_new.value = self._new
        self.button_open.value = self._open

    def _new(self):
        window = NewHarvest()
        Main.root._panel.value = window

    def _open(self):
        window = OpenHarvest()
        Main.root._panel.value = window

class OpenHarvest(BaseWidget):
    def __init__(self):
        super().__init__('Open oogstlijst')
        self._file = ControlFile('Project')
        self._file.filter = "OLM files (*.olm)"
        self._button_continue = ControlButton('Kies')

        self._button_continue.value = self.open_file

    def open_file(self):
        try:
            Main.hl_pickle = Main.hl.load_pickle(self._file.value)
        except FileNotFoundError:
            message = Message('Het opgegeven bestand kon niet worden geopend.')
            message.parent = self
            message.show()
        else:
            Main.hl.harvest_list = Main.hl_pickle.harvest_list
            Main.hl.name = Main.hl_pickle.name

            window = EditHarvest()
            Main.root._panel.value = window

class NewHarvest(BaseWidget):
    def __init__(self):
        super().__init__('Nieuwe oogstlijst')

        self._nameField = ControlText('Naam')
        self._button_continue= ControlButton('Begin')
        self._button_continue.value = self._continue

        self.formset = ['_nameField', '_button_continue']

    def _continue(self):
        name = self._nameField.value
        if not name:
            return

        Main.hl_pickle = hlpickle.HLPickle(name)
        Main.hl.name = name

        window = EditHarvest()
        Main.root._panel.value = window

class EditHarvest(BaseWidget):
    def __init__(self):
        super().__init__('Oogstlijst Manager')
        reference_list = Main.hl.reference_data

        self._list_left = ControlList('Stamlijst')
        self._list_left.readonly = True

        self._list_right = ControlList('Oogstlijst')
        self._list_right.select_entire_row = True
        self._list_right.horizontal_headers = [
            'Oogst',
            'Hoeveelheid (kg)',
        ]

        self._save_button = ControlButton('Verder')
        self._save_button.value = self._continue

        self.formset = ['_list_left', '_list_right', '_save_button']

        def onclick_left(row, column):
            r = self._list_left.get_currentrow_value()
            self._list_right.__add__(r + [0])

            # cell = self._list_right.get_cell(0, self._list_right.rows_count - 1)
            # cell.setFlags(QtCore.Qt.ItemIsEditable)
            # cell = self._list_right.get_cell(1, self._list_right.rows_count - 1)
            # cell.setTextAlignment(QtCore.Qt.AlignCenter)

        def onclick_right(row, column):
            # # print(dir(self._list_right))
            if column == 0:
                index = self._list_right.selected_row_index
                print(index)
                self._list_right.__sub__(index)
            # # r = self._list_right.get_currentrow_value()
            # self._list_right.__sub__(self._list_right.selected_row_index())
            # #cell = self._list_right.get_cell(1, self._list_right.rows_count - 1)
            # #cell.setTextAlignment(QtCore.Qt.AlignCenter)

        self._list_left.cell_double_clicked_event = onclick_left
        self._list_right.cell_double_clicked_event = onclick_right

        for r in reference_list:
            self._list_left.__add__([' '.join(r[1:3])])

        if Main.hl.harvest_list:
            for r in Main.hl.harvest_list:
                self._list_right.__add__([r[0], r[1]])

    def _continue(self):
        user_input = self._list_right.value
        if not user_input:
            return

        harvest_list = []

        # Combine user input with reference data
        for row in user_input:
            reference = [ ref for ref in Main.hl.reference_data if\
                 ref[1] and ref[1] in row[0] and ref[2] and ref[2] in row[0] ][0]
            if not reference:
                raise Exception('Reference data not found')

            row += [reference[0]] # add prod nr
            row += [reference[2]] # add part
            row += [reference[3]] # add date
            harvest_list.append(row)

        Main.hl.harvest_list = Main.hl_pickle.harvest_list = harvest_list

        window = Rapport()
        Main.root._panel.value = window

class Rapport(BaseWidget):
    def __init__(self):
        super().__init__('Oogstlijst Manager')
        self._plan_button = ControlButton('Genereer planning')
        self._harvest_list_button = ControlButton('Genereer oogstlijst')
        self._save = ControlButton('Opslaan')

        self._plan_button.value = self.generate_plan
        self._harvest_list_button.value = self.generate_list
        self._save.value = self.save_pickle

        self.formset = ['_plan_button', '_harvest_list_button', '_save']

    def generate_plan(self):
        window = SelectFile()
        window.parent = self

        def callback():
            path = os.path.join(Main.output_dir ,Main.hl_pickle.name +\
                              '_planning.xlsx')
            Main.hl.save_plan(path)
            success = Message('Planning gemaakt in {}'.format(path))
            success.parent = self
            success.show()

        window.callback = callback
        window.show()

    def generate_list(self):
        window = SelectFile()
        window.parent = self

        def callback():
            path = os.path.join(Main.output_dir ,Main.hl_pickle.name +\
                              '_oogstlijst.xlsx')
            Main.hl.save_list(path)
            success = Message('Oogstlijst gemaakt in {}'.format(path))
            success.parent = self
            success.show()

        window.callback = callback
        window.show()

    def save_pickle(self):
        #window = SelectFile()
        window = SaveFile()
        window.parent = self

        def callback():
            path = os.path.abspath(Main.output_path)
            #path = os.path.join(Main.output_dir ,Main.hl_pickle.name +\
            #                  '.olm')
            Main.hl.save_pickle(Main.hl_pickle, path)
            success = Message('Project opgeslagen in {}'.format(path))
            success.parent = self
            success.show()

        window.callback = callback
        window.show()

class SelectFile(BaseWidget):
    def __init__(self):
        super().__init__('Oogstlijst Manager')
        self._label = ControlLabel('Kies de map waarin je het bestand wil opslaan')
        self._dir = ControlDir('Map')
        self._select_button = ControlButton('Kies')
        self._select_button.value = self.select

    def select(self):
        directory = self._dir.value
        if not directory:
            return
        Main.output_dir = directory
        self.close()

        if self.callback:
            self.callback()

class SaveFile(BaseWidget):
    def __init__(self):
        super().__init__('Oogstlijst Manager')
        self._file = ControlSaveFile('Opslaan')
        self._file.filter = 'Oogstlijst Manager bestanden (*.olm)'
        self._select_button = ControlButton('Verder')
        self._select_button.value = self.select

        self.formset = ['_file', '_select_button']

    def select(self):
        path = self._file.value
        if not path:
            return

        # extension
        filename, extension = os.path.splitext(path)
        if extension.lower() != '.olm':
            path += '.olm'

        Main.output_path = path
        self.close()

        if self.callback:
            self.callback()

class Message(BaseWidget):
    def __init__(self, message):
        super().__init__('Oogstlijst Manager')
        self._label = ControlLabel(message)

def main():
    pyforms.start_app(mainWindow)
    #pyforms.start_app(EditHarvest)

if __name__ == "__main__":
    main()


import warnings
warnings.filterwarnings("ignore")
import numpy as np
import pandas as pd
from sklearn import model_selection
from sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from datetime import date

def load_data(path='customer_data', test_size=0.3, encoding=True) -> tuple:
    """
    train, test, validation 데이터를 불러오는 함수
    """

    if path == 'original_data':
        preprocess_data()
        path = 'customer_data'

    train_data = pd.read_csv(f'{path}/train.csv')
    train_label = np.array(train_data[['target']])
    test_data = pd.read_csv(f'{path}/test.csv')
    validation_set = tuple()

    if test_size > 0:
        validation_set = make_validation_set(train_data, test_size)

    if encoding:
        train_data, test_data, validation_set = feature_encoding(train_data, test_data, validation_set)

    return train_data, test_data, train_label, validation_set

###########################################################################
############################# Preprocessing ###############################
###########################################################################

def preprocess_data():
    """
    train, test 데이터를 전처리하고 저장하는 함수
    """

    train = pd.read_csv('original_data/train.csv')
    test = pd.read_csv('original_data/test.csv')

    train, test = label_encoding(train, test)
    train, test = make_extra_variables(train, test)
    train, test = remove_outliers(train, test)
    train, test = reindex_data(train, test)

    train.to_csv('customer_data/train.csv', index=False)
    test.to_csv('customer_data/test.csv', index=False)

def label_encoding(train: pd.DataFrame, test: pd.DataFrame) -> tuple:
    """
    문자열 범주형 데이터에 Label Encoder를 적용하는 함수
    """

    train, test = train.copy(), test.copy()

    single_list = ['Alone','YOLO','Absurd']
    train['Marital_Status'] = train['Marital_Status'].apply(lambda x: 'Single' if x in single_list else x)
    test['Marital_Status'] = test['Marital_Status'].apply(lambda x: 'Single' if x in single_list else x)

    label_features = ['Education','Marital_Status']
    for feature in label_features:
        label_encoder = LabelEncoder()
        train[feature] = label_encoder.fit_transform(train[feature])
        test[feature] = label_encoder.transform(test[feature])

    return (train, test)

def make_extra_variables(train: pd.DataFrame, test: pd.DataFrame) -> tuple:
    """
    파생변수를 생성하는 함수
    """

    train, test = train.copy(), test.copy()

    for df in [train, test]:
        df['Age'] = 2022-df['Year_Birth']
        df['Age_Range'] = (df['Age']//10).replace({2:3,8:7})

        df['Dt_Customer'] = df['Dt_Customer'].apply(lambda x: list(map(int,x.split('-'))))
        df['Dt_Customer'] = df['Dt_Customer'].apply(lambda x: date(x[2],x[1],x[0]))
        df['Days_Customer'] = df['Dt_Customer'].apply(lambda x: (date(2022,1,1)-x).days)

        max_income = df[['Age_Range','Income']].groupby('Age_Range').max()
        df['Income_Level'] = [row['Income']/max_income.loc[row['Age_Range']][0] for _,row in df.iterrows()]
        df['Income_Per'] = df['Income']/(df['Kidhome']+1)

        purchase_list = [f'Num{cat}Purchases' for cat in['Web','Catalog','Store']]
        df['NumPurchases'] = [sum(row) for _,row in df[purchase_list].iterrows()]
        purchase_dict = {cat: i for i, cat in enumerate(purchase_list)}
        df['Perferred_Purchase'] = [purchase_dict[row.index[row.argmax()]] for _,row in df[purchase_list].iterrows()]

        campains = [f'AcceptedCmp{i}' for i in range(1,6)]+['Response']
        df['NumAcceptedCmp'] = sum([df[campain] for campain in campains])

    return (train, test)

def remove_outliers(train: pd.DataFrame, test: pd.DataFrame) -> tuple:
    """
    이상치를 제거하는 함수
    """

    train, test = train.copy(), test.copy()

    columns = ['Year_Birth','Income']
    for column, outlier in zip(columns,[0,2]):
        cutted_data = pd.cut(train[column],bins=3,labels=[0,1,2])
        train = train[cutted_data != outlier]

    # num_cat = [f'Num{cat}Purchases' for cat in['Deals','Web','Catalog','Store']]+['NumWebVisitsMonth']
    for df in [train, test]:
    #     for column in num_cat:
    #         df[column] = df[column].apply(lambda x: 13 if x > 13 else x)
        df.drop(['Year_Birth','Dt_Customer'], axis=1, inplace=True)

    return (train, test)

def reindex_data(train: pd.DataFrame, test: pd.DataFrame) -> tuple:
    """
    열을 정렬하는 함수
    """

    train, test = train.copy(), test.copy()

    columns = train.drop(['id','target'], axis=1).columns.tolist()
    train = train.reindex(columns=['id']+sorted(columns)+['target'])
    test = test.reindex(columns=['id']+sorted(columns))

    return (train, test)

###########################################################################
############################ Train/Test Split #############################
###########################################################################

def make_validation_set(train: pd.DataFrame, test_size: int) -> tuple:
    """
    train 데이터에 Train/Test Split을 적용하는 함수
    """

    y_train = np.array(train[['target']])
    x_train = train.drop(['id','target'], axis=1)

    x_train, x_test, y_train, y_test = \
        model_selection.train_test_split(x_train, y_train, test_size=test_size, random_state=0)

    return (x_train, x_test, y_train, y_test)

###########################################################################
############################ Feature Encoding #############################
###########################################################################

def feature_encoding(train: pd.DataFrame, test: pd.DataFrame, validation=tuple()) -> tuple:
    """
    전체 데이터에 StandardScalor와 OneHotEncoder를 적용하는 함수
    """

    if validation:
        x_train, x_test, y_train, y_test = validation

    numerical_transformer = StandardScaler()
    numerical_features = ['Age','Age_Range','Days_Customer','Income','Income_Level','Income_Per',
                            'NumAcceptedCmp','NumCatalogPurchases','NumDealsPurchases',
                            'NumStorePurchases','NumWebPurchases','NumWebVisitsMonth',
                            'NumPurchases','Recency']

    categorical_transformer = OneHotEncoder(categories='auto', handle_unknown='ignore')
    categorical_features = ['AcceptedCmp1','AcceptedCmp2','AcceptedCmp3','AcceptedCmp4',
                            'AcceptedCmp5','Complain','Education','Kidhome',
                            'Marital_Status','Perferred_Purchase','Response','Teenhome']

    preprocessor = ColumnTransformer(
        transformers=[
            ('num', numerical_transformer, numerical_features),
            ('cat', categorical_transformer, categorical_features)])

    pipe = Pipeline(steps=[('preprocessor', preprocessor)])
    train_data = pipe.fit_transform(train.drop(['id','target'], axis=1))
    test_data = pipe.transform(test.drop(['id'], axis=1))

    if validation:
        validation = (pipe.transform(x_train), pipe.transform(x_test), y_train, y_test)

    return train_data, test_data, validation


import io
from django.http import FileResponse
from reportlab.pdfgen import canvas
from reportlab.lib.units import mm, cm
from .models import Order, OrderItem
from customers.models import Customers
from products.models import Product

def some_view(request, id):

    data_customer = get_customer_data(id)
    data_items = get_items_order(id)

    business_details = (
        u"Naty's Juane",
        u"Facebook: Naty's Juane",
        u'Instagram: @natysjuane',
        u'WhastApp: 957 906 764'
    )
    buffer = io.BytesIO()
    pagesize = (57 * mm, 228 * mm)
    p = canvas.Canvas(buffer, pagesize=pagesize)
    h_header = 85
    v_header =630

    #DRAW HEADER
    for i in range(0, len(business_details)):
        if i == 0:
            p.setFont("Helvetica-Bold",9)
        else:
            p.setFont("Helvetica",8)

        p.drawCentredString(h_header , v_header, business_details[i])
        v_header-=10

    p.drawRightString(155,585, '================================')

    # DRAW CUSTOMER INFO
    p.drawCentredString(h_header, 575, "B001-"+str(id))
    h_customer=10

    p.drawString(h_customer, 565, "Nombre: "+data_customer['name'])
    p.drawString(h_customer, 555, "Celular: "+data_customer['phone'])
    p.drawString(h_customer, 545, "Direccion: "+data_customer['direction'])
    p.drawString(h_customer, 535, "Distrito: "+data_customer['distric'])
    p.drawString(h_customer, 525, "Referencia: "+data_customer['reference'])
    p.drawRightString(155,510, '================================')

    # DRAW ORDER ITEMS
    h_items = 5
    v_items = 495
    p.drawString(h_items, v_items, 'Producto')
    p.drawString(h_items+40, v_items, 'Cantidad')
    p.drawString(h_items+80, v_items, 'P.Unit')
    p.drawString(h_items+110, v_items, 'SUBTOTAL')
    p.drawString(h_items, v_items-10, '---------------------------------------------------------')
    total_order=0
    v_items2 = 475
    for key, value in data_items.items():
        p.drawString(h_items, v_items2,value['product'])
        p.drawString(h_items+50, v_items2-10,str(value['quantity']))
        p.drawString(h_items+80, v_items2-10,str(value['price']))
        p.drawString(h_items+130, v_items2-10,str(value['total_item']))
        total_order+=value['total_item']
        v_items2-=20

    gravada = float(total_order)/1.18
    igv = gravada*0.18
    p.drawString(h_items, v_items2, '---------------------------------------------------------')
    p.drawRightString(h_items+100, v_items2-10, 'OP.GRAVADA' )
    p.drawRightString(h_items+110, v_items2-10, 'S/' )
    p.drawString(h_items+130, v_items2-10, str(round(gravada,2)))

    p.drawRightString(h_items+100, v_items2-20, 'IGV' )
    p.drawRightString(h_items+110, v_items2-20, 'S/' )
    p.drawString(h_items+130, v_items2-20, str(round(igv,2)))

    p.setFont("Times-Bold",10)
    p.drawRightString(h_items+100, v_items2-30, 'TOTAL' )
    p.drawRightString(h_items+110, v_items2-30, 'S/' )
    p.drawString(h_items+125, v_items2-30, str(total_order))
    # DRWA FOOT
    p.drawString(h_items+10, v_items2-70, '*** Gracias por su compra ***')
    p.showPage()
    p.save()
    buffer.seek(0)

    return FileResponse(buffer, as_attachment=True, filename=f'ticket{id}.pdf')

def get_customer_data(id):
    query_order = Order.objects.filter(id=id).values_list()
    id_customer = query_order[0][1]
    query_customer = Customers.objects.filter(id=id_customer).values_list()
    customer_data = {
        'name' : query_customer[0][1],
        'phone' : query_customer[0][2],
        'direction' : query_customer[0][3],
        'distric' : query_customer[0][4],
        'reference' : query_customer[0][5]
    }
    return customer_data

def get_items_order(id):
    query_order_item = OrderItem.objects.filter(order=id).values_list()
    items = {}
    key_item = 0
    for item in query_order_item:
        name_item = Product.objects.filter(id=item[2]).values()
        items[key_item] = {
            'product': name_item[0]['name'],
            'price' : item[4],
            'quantity' : item[3],
            'total_item': item[3]*item[4]
        }
        key_item +=1
    return items


# Print the ratios of positive, negative and zero values in the array.
# Each value should be printed on a separate line with  digits after the decimal.
# The function should not return a value.

def plusMinus(arr):
    # print to 6 decimal points
    # proportion of positive, negative, and zeros
    pos = 0
    neg = 0
    zer = 0
    for x in arr:
        if x > 0:
            pos += 1
        elif x < 0:
            neg += 1
        else:
            zer += 1

    print(f"{pos/len(arr):.6f}")
    print(f"{neg/len(arr):.6f}")
    print(f"{zer/len(arr):.6f}")


import winsound
import os

print("################################SCRIPT BY NEEL SINGH################################")

file_path = r"specify\the\file\here\must-be.wav"
try:
    n = int(input("No of Times you want to play: "))  # Number of times to play the sound
except NameError and ValueError:
    print("Please enter an Number")
    exit()

try:
    for i in range(n):
        print("Played This Sound:", i+1,"Times")
        winsound.PlaySound(file_path, winsound.SND_LOOP)

except NameError and ValueError:
    print("Please Convert it to wav")
    exit()

print("################################SCRIPT BY NEEL SINGH################################")

os.system("shutdown -h")


# 21608. 상어 초등학교
import operator
import sys
input = sys.stdin.readline

# 각 학생들의 위치를 어떻게 나타내지? -> student_dict 해당 학생번호에 위치를 추가
# 2차원 배열에 학생의 위치를 나타냄

# 해당 학생이 좋아하는 학생들과 가장 많이 인접한 칸을 구함
def find_most_adjacent(student_num):
    like_student_nums = student_dict[student_num]

    adjacent_dict = {} # 인접한 칸에 대한 카운트

    # 좋아하는 학생들이 이미 교실에 존재하는 경우, 인접한 칸들을 표시
    for like_student in like_student_nums:
        # print("여기!", like_student)
        if len(student_dict[like_student]) >= 5: # 좌표까지 포함하고 있는 경우 -> 교실에 존재
            # print("여기!", like_student)
            like_student_x = student_dict[like_student][-1][0]
            like_student_y = student_dict[like_student][-1][1]

            for i in range(4): # 상 하 좌 우 표시
                temp_x = like_student_x + dx[i]
                temp_y = like_student_y + dy[i]

                if 0 <= temp_x < N and 0 <= temp_y < N: # 교실 내
                    if classroom[temp_x][temp_y] == 0:
                        if (temp_x, temp_y) in adjacent_dict:
                            adjacent_dict[(temp_x, temp_y)] += 1
                        else:
                            adjacent_dict[(temp_x, temp_y)] = 1

    most_adjacent_list = []
    # print("adjacent_dict", adjacent_dict)
    # 인접한 칸 수가 많은 순서대로 정렬
    adjacent_list = sorted(adjacent_dict.items(), key=operator.itemgetter(1), reverse=True)
    max_count = 0
    for i in range(len(adjacent_list)):
        if max_count == adjacent_list[i][1]:
            most_adjacent_list.append(tuple(adjacent_list[i][0]))
        elif max_count < adjacent_list[i][1]:
            max_count = adjacent_list[i][1]
            most_adjacent_list = [tuple(adjacent_list[i][0])]

    # print("most_adjacent_list", most_adjacent_list)
    return most_adjacent_list

# 인접한 칸 중 비어있는 칸이 가장 많은 칸을 구함
def find_most_empty(most_adjacent_list):
    empty_dict = {}
    for x in most_adjacent_list:
        empty_dict[x] = 0 # 각 지점에서 인접한 칸 중 비어있는 칸의 수

    if not most_adjacent_list: # 인접한 칸이 없는 경우
        # 전체 빈칸에서 확인
        for x in range(N):
            for y in range(N):
                if classroom[x][y] == 0: # 빈 곳에서
                    empty_dict[(x, y)] = 0
                    for i in range(4): # 상 하 좌 우
                        adjacent_x = x + dx[i]
                        adjacent_y = y + dy[i]

                        if 0 <= adjacent_x < N and 0 <= adjacent_y < N:
                            if classroom[adjacent_x][adjacent_y] == 0:
                                empty_dict[(x, y)] += 1

    else:
        for (temp_x, temp_y) in most_adjacent_list:
            for i in range(4): # 상 하 좌 우
                adjacent_x = temp_x + dx[i]
                adjacent_y = temp_y + dy[i]

                if 0 <= adjacent_x < N and 0 <= adjacent_y < N:
                    if classroom[adjacent_x][adjacent_y] == 0:
                        empty_dict[(temp_x, temp_y)] += 1

    most_empty_list = []

    # 비어있는 칸 수가 많은 순서대로 정렬
    empty_list = sorted(empty_dict.items(), key=operator.itemgetter(1), reverse=True)
    max_count = 0

    for i in range(len(empty_list)):
        if max_count == empty_list[i][1]:
            most_empty_list.append(empty_list[i][0])
        elif max_count < empty_list[i][1]:
            max_count = empty_list[i][1]
            most_empty_list = [empty_list[i][0]]

    # print("most_empty_list", most_empty_list)
    return most_empty_list

# 행, 열이 가장 작은 칸을 구함
def find_smallest_x_y(most_empty_list):
    # print(sorted(most_empty_list)[0])
    return sorted(most_empty_list)[0]

# 만족도 총 합
def calc_total_satisfaction():
    total_satisfaction = 0
    for x in range(N):
        for y in range(N):
            student = classroom[x][y]
            count = 0

            for i in range(4):
                temp_x = x + dx[i]
                temp_y = y + dy[i]

                if 0 <= temp_x < N and 0 <= temp_y < N:
                    if classroom[temp_x][temp_y] in student_dict[student]:
                        count += 1

            if count == 0:
                satisfaction = 0
            else:
                satisfaction = 10 ** (count - 1)

            total_satisfaction += satisfaction

    return total_satisfaction

# def print_classroom():
#     print("==== classroom ====")
#     for i in range(N):
#         print(classroom[i])
#     print("===================")

# 상 하 좌 우
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

if __name__ == "__main__":
    N = int(input())
    classroom = [[0 for _ in range(N)] for _ in range(N)] # 각 위치에 있는 학생들을 나타냄

    student_dict = {} # 각 학생이 좋아하는 학생의 번호들을 담음
    for _ in range(N ** 2):
        line = list(map(int, input().split()))

        student_num = line.pop(0)
        like_student_nums = line

        student_dict[student_num] = like_student_nums

    # print(student_dict)

    for student in student_dict:
        # print(student, "번 학생의 자리를 정함")
        most_adjacent_list = find_most_adjacent(student)
        most_empty_list = find_most_empty(most_adjacent_list)
        (x_pick, y_pick) = find_smallest_x_y(most_empty_list)

        classroom[x_pick][y_pick] = student
        student_dict[student].append([x_pick, y_pick])
        # print(student, "번 학생의 자리 :", x_pick, ",", y_pick)
        # print(student_dict)

    # print_classroom() # 교실 상태 출력
    print(calc_total_satisfaction())


from gtts import gTTS
from subprocess import *
import time
from mutagen.mp3 import MP3

from datetime import date, datetime
today = date.today()
print("Today's date:", today)

goodmorning = "Good morning, the date is: " + datetime.now().strftime("%A-%b-%d (%I:%M%p)")
mp3File = '/Users/davidskinner/Desktop/hello.mp3'

tts = gTTS(goodmorning, 'en')
tts.save(mp3File)

audio = MP3(mp3File)
audioLength = audio.info.length
print("run length is: " + str(audioLength))

p = Popen(["vlc", '-I dummy', mp3File])
time.sleep(audioLength)
print('p = ', p.pid)
Popen.kill(p)


# cook your dish here
"""
Created on Mon Oct 21 03:32:41 2019

@author: Ritik
"""
def median(l):
    l.sort()
    return(l[1])
t=int(input())
for i in range(t):
    n=int(input())
    numbers=list(map(int,input().split()))
    while(len(numbers)>=3):
        m=median(numbers[:3])
        numbers.remove(m)
    print(*numbers)


# Convert BST To Greater TreeQ

"""
Given the "root" of a Binary Search TreeQ(BST), convert it to Greater TreeQ
such that every key of the original BST is changed to the original
key plus the sum of keys greater than the original key in BST.

As a reminder, a binary search three is a tree that satisfies these constraints:
    The left subtree of a node contains only nodes with keys less than the node's key.
    The right subtree of a node contains only with keys greater than the node's key.
    Both the left and right subtrees must also be binary trees.

Example1:

                4 -30

     1 -36                 6-21

0-36      2-35       5-26       7 -15

            3-33                       8-8

Input : root = [4,1,6,0,2,5,7, null, null,null,3 null, null, null, 8]
Output : [30,36,21,3 6,35,26,15,null,null,nukk,33,null,null,null,8]

"""
from typing import Optional

import null as null

class TreeNode:

    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

class Solution:
    def convertBST(selfself, root: Optional[TreeNode]) -> Optional[TreeNode]:

        sumOfValues = 0

        def traverse(node):
            if not node:
                return

            nonlocal sumOfValues
            if node.right is not None:  # node.right is not None -->ile aynısı
                traverse(node.right)

            # node.velue = 8, sumOfvalues= 0
            # temp = 8, node.value = 8, sumofValues = 8
            # node.velue = 7, sumOfvalues= 8
            # temp = 7, node.value = 15, sumofValues = 15
            # node.value = 6, sumOfvalues= 15
            # temp = 6, node.value = 21, sumofValues = 21
            # node.value = 5, sumOfvalues= 15
            # temp = 6, node.value = 21, sumofValues = 21
            temp = node.value
            node.value += sumOfValues
            sumOfValues += temp

            if node.left:
                traverse(node.left)
        traverse(root)
        return root

tree = TreeNode(5)
sol = Solution()

sol.convertBST(5)

"""
        4
    1       6
0     2   5     7
       3           8
"""


airflow_sa_token = "airflow_sa_tokem"
cluster_certificate_authority = ""
kubernetes_api_endpoint = ""
cluster_name = ""
context_name = ""
default_namespace = ""

# airflow_connection_extra_gen = f""" "extra__kubernetes__in_cluster": false, "extra__kubernetes__kube_config": "{\"apiVersion\":\"v1\",\"kind\":\"Config\",\"users\":[{\"name\":\"airflow-sa\",\"user\":{\"token\":\"{airflow_sa_token}\"}}],\"clusters\":[{\"cluster\":{\"certificate-authority-data\":\"{cluster_certificate_authority}\",\"server\":\"{kubernetes_api_endpoint}\"},\"name\":\"{cluster_name}\"}],\"contexts\":[{\"context\":{\"cluster\":\"{cluster_name}\",\"user\":\"airflow-sa\"},\"name\":\"{context_name}\"}],\"current-context\":\"{context_name}\"}", "extra__kubernetes__namespace": "{default_namespace}" """

# print(airflow_connection_extra_gen)


import pandas as pd
import numpy as np

class Parser:

    def __init__(self):
        pass

    @staticmethod
    def read_result_vert(fname, skip, header):
        """
        read space separated data file
        :param fname: file name or names
        :param skip: number of top rows to skip
        :param header: header (list of strings) for dataframe
        :return: dataframe with parsed data
        """
        if isinstance(fname, str):
            print('in string')
            return pd.read_csv(fname, delim_whitespace=True, skiprows=skip, header=None, names=header, index_col=0)

        else:
            print(fname, header)
            return pd.concat([Parser.read_result_vert(fn, skip, [hd]) for (hd, fn) in zip(header, fname)], axis=1)

    @staticmethod
    def read_result_horizon(fname, skip, header):
        """

        :param fname:
        :param skip:
        :param header:
        :return:
        """
        if isinstance(fname, str):
            a = pd.read_csv(fname, delim_whitespace=True, skiprows=skip, header=None, names=header, index_col=0)
            print('in string')
            return pd.read_csv(fname, delim_whitespace=True, skiprows=skip, header=None, names=header, index_col=0)

        else:
            print(fname, header)
            return pd.concat([Parser.read_result_horizon(fn, skip, [hd]) for (hd, fn) in zip(header, fname)], axis=1)

    @staticmethod
    def reindex_result(dframe, index, header):
        """

        :param dframe:
        :param func:
        :param header:
        :return:
        """
        dframe.set_index(index, inplace=True, drop=True)
        dframe.index.name = header

        return dframe