#!/usr/bin/env python
## coding: UTF-8

import spidev
import time
import RPi.GPIO as GPIO
import struct
import binascii
import rospy
from std_msgs.msg import Float32

def float2hex(f):
  return struct.unpack('>I', struct.pack('>f', f))[0]

def hex2float(s):
  # print([hex(i)[2:] for i in s])
  strdhex = "".join(["{:02x}".format(i) for i in s[::-1]])
  # print(strdhex)
  return struct.unpack('>f', binascii.unhexlify(strdhex))[0]

class MtlmotorSpi:
  def __init__(self, bus=0, device=0):
    self.spi = spidev.SpiDev()
    self.spi.open(bus, device)

    self.spi.mode = 3
    self.spi.max_speed_hz = 1000000

    self.s_data = [0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00]
    self.r_data = [0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]

    self.motor_current = 0
    self.encoder_pulse = 0

  def fill_checksum(self):
    sm = 0x00
    for i in range(5):
      tmp = (self.s_data[2*i+1]<<8) + self.s_data[2*i]
      sm = sm + tmp
    sm = sm & 0xffff
    if not sm & 0x8000:
      sm = sm + 0x8000
    self.s_data[10] = sm&0xff
    self.s_data[11] = sm>>8

  def send(self, current):
    # Fill current data
    hexed = float2hex(current)
    for i in range(3, 7):
      self.s_data[i] = hexed & 0xff
      hexed = hexed >> 8
    # Fill checksum
    self.fill_checksum()
    # Send data
    self.r_data = self.spi.xfer3(self.s_data)
    self.encoder_pulse = hex2float(list(self.r_data[1:5]))
    self.motor_current = hex2float(list(self.r_data[5:9]))

  def __del__(self):
    self.spi.close()

def Motor1Callback(message):
  ddmotor1.send(float(-message.data))

def Motor2Callback(message):
  rospy.loginfo("got current:[%f]", message.data)
  ddmotor2.send(float(-message.data))

ddmotor1 = MtlmotorSpi(bus=0, device=0)
ddmotor2 = MtlmotorSpi(bus=0, device=1)

rospy.init_node("spi_comm")
sub = rospy.Subscriber("current_controller1", Float32, Motor1Callback)
sub = rospy.Subscriber("current_controller2", Float32, Motor2Callback)
rospy.spin()


import requests
from django.shortcuts import render
import re
from tax_calculation.models import CustomTariff

class Ett_handler:
    def __init__(self, request):
        self.code = request.POST.get('code')
        self.code = self.code.replace(' ', '')
        self.specific_metric = request.POST.get('specific_metric')
        self.specific_label = request.POST.get('specific_label')
        self.valute = request.POST.get('valute')
        self.cource = request.POST.get('cource')
        self.usertype_cource = request.POST.get('usertype_cource')
        self.price = request.POST.get('price')
        self.tax = CustomTariff.objects.get(tnved_code=self.code).tax

    def get_parameters(self):  # Возврат контекста для шаблонов.
        return {'code' : self.code, 'specific_metric' : self.specific_metric, 'specific_label' : self.specific_label, 'valute' : self.valute,'price' : self.price, 'tax' : self.tax, 'cource' : self.cource, 'usertype_cource' : self.usertype_cource}

    def tax_type_checker(self):
        if (len(self.tax) <= 4 and self.tax[-1] == "%") or self.tax == "0":
            self.tax_type = 'advalore'
        elif len(self.tax.split()) == 1 and '+' not in self.tax:
            self.tax_type = 'specific'
        else:
            self.tax_type = 'combine'
        return self.tax_type

    def get_label(self): # Получение единицы измерения из специфической ставки.
        return re.search(r'(?<=\/).*', self.tax).group(0)

    def get_specific_tax(self): # Получение количественных данных из специфической ставки.
        return re.search(r'.{1,}\d', self.tax).group(0)

    def get_advalore_from_combine(self): # Получение адвалорной ставки из комбинированной.
        return re.search(r'.{1,}(?=%)', self.tax).group(0)

    def get_specific_from_combine(self): # Получение специфической ставки из комбинированной.
        return re.search(r'((?<= )|(?<=\+))\d{1,}.\d{1,}|(?<= )\d{1,}', self.tax).group(0)

    def get_combine_type(self): # Получение типа комбинированной ставки. Первый тип, содержит структуру "Но не менее", второй тип "% +"
        if '+' in self.tax:
            return 2
        else:
            return 1

    def get_valute(self): # Получение типа валюты из специфической ставки.
        if 'евро' in self.tax:
            return 'евро'
        else:
            return 'долл'

    def get_cource(self, valute): # Получение текущего курса валюты.
        if valute == 'долл':
            cource = requests.get('https://api.coingate.com/v2/rates/merchant/USD/RUB').json()
            return cource
        if valute == 'евро':
            cource = requests.get('https://api.coingate.com/v2/rates/merchant/EUR/RUB').json()
            return cource

    def calculate_tax(self, specific_metric=None, combine_type=None): # Непосредственный рассчет таможенной пошлины, в зависимости от ее типа.
        tax_type = self.tax_type_checker()
        if tax_type == 'advalore':
            if self.tax != '0':
                tax = float(self.price) * (float(self.tax[:-1]) / 100)
                return round(tax, 2)
            else:
                return 0
        elif tax_type == 'specific':
            if self.usertype_cource == '':  # Если не введен кастомный курс валюты
                specific_tax = self.get_specific_tax()
                tax = float(specific_tax) * float(specific_metric) * float(self.cource)
            else:
                specific_tax = self.get_specific_tax()
                tax = float(specific_tax) * float(specific_metric) * float(self.usertype_cource)
            return round(tax, 2)
        elif tax_type == 'combine':
            advalore_tax = self.get_advalore_from_combine()
            advalore_result = float(advalore_tax) / 100 * float(self.price)

            specific_tax = self.get_specific_from_combine()
            if self.usertype_cource == '':
                specific_result = float(specific_tax) * float(specific_metric) * float(self.cource)
            else:
                specific_result = float(specific_tax) * float(specific_metric) * float(self.usertype_cource)
            if combine_type == 1:
                if advalore_result > specific_result:
                    return {'result' : round(advalore_result, 2), 'formula_type' : 'advalore'}
                else:
                    return {'result' : round(specific_result, 2), 'formula_type' : 'specific'}
            elif combine_type == 2:
                return {'result' : round(advalore_result + specific_result, 2), 'formula_type' : 'summary'}

def index(request):
    return render(request, 'tax_calculation/index.html')

def calculate_tax(request):
    handler = Ett_handler(request)
    tax_type = handler.tax_type_checker()
    parameters = handler.get_parameters()
    parameters['tax_type'] = tax_type
    #Проверка на достаточность данных для рассчета по адвалорной формуле. Если данных недостаточно, запрашиваем дополнительные, если достаточно, производим рассчет.
    if tax_type == 'advalore':
        if parameters['price'] == '':
            return render(request, 'tax_calculation/result.html', context=parameters)
        else:
            calculated_tax = handler.calculate_tax()
            parameters['calculated_tax'] = calculated_tax
            return render(request, 'tax_calculation/result.html', context=parameters)
    #Проверка на достаточность данных для рассчета по специфической формуле. Если данных недостаточно, запрашиваем дополнительные если достаточно, производим рассчет.
    elif tax_type == 'specific':
        if parameters['specific_metric'] == '':
            parameters['specific_label'] = handler.get_label()
            parameters['valute'] = handler.get_valute()
            parameters['cource'] = handler.get_cource(valute=parameters['valute'])
            return render(request, 'tax_calculation/result.html', context=parameters)
        else:
            calculated_tax = handler.calculate_tax(parameters['specific_metric'])
            parameters['calculated_tax'] = calculated_tax
            return render(request, 'tax_calculation/result.html', context=parameters)
    #Проверка на достаточность данных для рассчета по комбинированной формуле. Если данных недостаточно, запрашиваем дополнительные если достаточно, производим рассчет.
    elif tax_type == 'combine':
        combine_type = handler.get_combine_type()
        if parameters['specific_metric'] == '' or parameters['price'] == '':
            parameters['specific_label'] = handler.get_label()
            parameters['valute'] = handler.get_valute()
            parameters['cource'] = handler.get_cource(valute=parameters['valute'])
            parameters['combine_type'] = combine_type
            return render(request, 'tax_calculation/result.html', context=parameters)
        else:
            calculated_tax = handler.calculate_tax(specific_metric=parameters['specific_metric'], combine_type=combine_type)
            parameters['calculated_tax'] = calculated_tax['result']
            parameters['combine_formula_type'] = calculated_tax['formula_type']
            return render(request, 'tax_calculation/result.html', context=parameters)


import numpy as np
import matplotlib.pyplot as plt
import tclab
import time

n = 600  # Number of second time points (10 min)
tm = np.linspace(0,n,n+1) # Time values

# data
lab = tclab.TCLab()
T1 = [lab.T1]
T2 = [lab.T2]
lab.Q2(100)
for i in range(n):
    time.sleep(1)
    print(lab.T1,lab.T2)
    T1.append(lab.T1)
    T2.append(lab.T2)
lab.close()

# Disturbance Gain
Kd = (T1[-1]-T1[0]) / (T2[-1]-T2[0])

# Plot results
plt.figure(1)
plt.plot(tm/60.0,T1,'r.',label=r'Measured $T_1$')
plt.plot(tm/60.0,T2,'b.',label=r'Measured $T_2$')
plt.text(3,40,'Disturbance Gain (Kd): '+str(round(Kd,2)))
plt.ylabel(r'Temperature ($^o$C)')
plt.xlabel('Time (min)')
plt.legend()
plt.savefig('Disturbance_gain.png')
plt.show()


import numpy as np
import matplotlib.pyplot as plt
from cycler import *
from matplotlib.ticker import FuncFormatter

__metaclass__ = type

width = 455.24411

def set_size( width, fraction=1, subplots=(1, 1), golden_ratio=0.6180339887498949 ):
    """
    Set figure dimensions to avoid scaling in LaTeX.

    Parameters
    ----------
    width: float
            Document textwidth or columnwidth in pts
    fraction: float, optional
            Fraction of the width which you wish the figure to occupy

    Returns
    -------
    fig_dim: tuple
            Dimensions of figure in inches
    """
    # Width of figure (in pts)
    fig_width_pt = width * fraction
    # Convert from pt to inches
    inches_per_pt = 1 / 72.27

    # Figure width in inches
    fig_width_in = fig_width_pt * inches_per_pt
    # Figure height in inches
    fig_height_in = fig_width_in * golden_ratio * (subplots[0] / subplots[1])

    fig_dim = (fig_width_in, fig_height_in)

    return fig_dim

# scientific notation on each ticks
def sci_format( x ):
    """
    Format float to scientific notation in Latex syntax.
    Parameters
    ----------
    x: float
        Float to be formated

    Returns
    -------
    string
    """
    if x == 0:
        return r'$0$'
    return r'$%.5g\times10^{%.0f}$' % (np.sign(x)*10**(-np.floor(np.log10(abs(x)))+np.log10(abs(x))), np.floor(np.log10(abs(x))))
    #The first argument of the format gives the first significant digits of the number with the sign preserved and brought to a range between [1-10), The next argument gives the  numbers integer exponent of 10
    #Both the first and second arguments are formatted to display only 2 decimal places due to the lack of space.

major_formatter = FuncFormatter(sci_format)


import sys
import importlib
import threading

import tkinter as tk
from tkinter import scrolledtext
from os.path import abspath, dirname
from tkinter import ttk

import ctypes

ctypes.windll.shcore.SetProcessDpiAwareness(2)
ScaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(2) / 100

CURRENT_DIR = dirname(__file__)
path = abspath(CURRENT_DIR + "/../")
sys.path.append(path)
import listener
import script

class App_start:
    def __init__(self, frame):
        self.frame = frame
        self.on_move_text = tk.StringVar()
        self.script_mode_text = tk.StringVar()
        self.button()
        self.label()
        self.log_area()
        self.spinbox()
        self.grid_layout()

        # after create
        self.log = self.Log(self.log_tk)
        self.mouse_record_spinbox_tk.set(1)
        self.mouse_record_speed_spinbox_tk.set(0.30)

    def get_attr(self, attr):
        return getattr(self, attr, None)

    def label(self):
        self.note_tk = tk.Label(
            self.frame,
            text="Start:\n\tF2開始/結束腳本\n\t執行腳本\n滑鼠錄製:\n\t錄製滑鼠->次數->間隔\n\t執行錄製\nESC:結束",
            font=("標楷體", 12),
            justify="left",
            width=30,
            height=7,
            background="#add8e6",
        )
        self.on_move_tk = tk.Label(
            self.frame,
            font=("標楷體", 12),
            width=20,
            height=1,
            textvariable=self.on_move_text,
        )
        self.script_mode_tk = tk.Label(
            self.frame,
            font=("標楷體", 12),
            width=20,
            height=1,
            textvariable=self.script_mode_text,
        )

    def button(self):
        self.btn_start_tk = ttk.Button(
            self.frame,
            text="Start",
            width=20,
            cursor="hand2",
            command=self.btn_Start_tk_click,
        )
        self.btn_record_mouse_tk = ttk.Button(
            self.frame,
            text="錄製滑鼠",
            width=20,
            cursor="hand2",
            command=self.btn_record_mouse_tk_click,
        )
        self.btn_record_mouse_tk_start = ttk.Button(
            self.frame,
            text="執行錄製",
            width=20,
            cursor="hand2",
            command=self.btn_record_mouse_tk_start_click,
        )

    def log_area(self):
        self.log_tk = scrolledtext.ScrolledText(
            self.frame,
            width=25,
            height=5,
            wrap="word",
            font=("標楷體", 12),
            bg="#f5f5f5",
            fg="#00008a",
            selectforeground="#ffa500",
            selectbackground="#8b008b",
            borderwidth=2,
        )

    def spinbox(self):
        self.mouse_record_spinbox_tk = ttk.Spinbox(
            self.frame,
            from_=-1,
            to=999999999,
            increment=1,
            wrap=True,
        )
        self.mouse_record_speed_spinbox_tk = ttk.Spinbox(
            self.frame,
            from_=0.05,
            to=999999999,
            increment=0.05,
            wrap=True,
        )

    # command function
    def btn_Start_tk_click(self):
        importlib.reload(script)
        t = threading.Thread(target=listener.start, args=(self.get_attr, script))
        t.setName("bin_start")
        t.start()
        self.btn_disable()

    def btn_record_mouse_tk_click(self):
        t = threading.Thread(target=listener.start, args=(self.get_attr, script, "mouse_record"))
        t.setName("mouse_record")
        t.start()
        self.btn_disable()

    def btn_record_mouse_tk_start_click(self):
        t = threading.Thread(
            target=listener.start, args=(self.get_attr, script, "mouse_record_start")
        )
        t.setName("mouse_record_start")
        t.start()
        self.btn_disable()

    def btn_disable(self):
        self.btn_start_tk.config(state="disable")
        self.btn_record_mouse_tk.config(state="disable")
        self.mouse_record_spinbox_tk.config(state="disable")
        self.btn_record_mouse_tk_start.config(state="disable")
        self.mouse_record_speed_spinbox_tk.config(state="disable")

    def btn_active(self):
        self.btn_start_tk.config(state="active")
        self.btn_record_mouse_tk.config(state="active")
        self.mouse_record_spinbox_tk.config(state="active")
        self.btn_record_mouse_tk_start.config(state="active")
        self.mouse_record_speed_spinbox_tk.config(state="active")

    class Log:
        def __init__(self, log_tk):
            self.log_tk = log_tk

        def replace_all(self, text):
            self.log_tk.delete(1.0, "end")
            self.log_tk.insert(1.0, text)

        def replace_line(self, text, start, end):
            self.log_tk.delete(start, end)
            self.log_tk.insert(start, text)

    # layout
    def grid_layout(self):
        self.note_tk.grid(row=0, column=1, pady=2)
        self.on_move_tk.grid(row=1, column=1, pady=1)
        self.script_mode_tk.grid(row=2, column=1, pady=1)
        self.log_tk.grid(row=3, column=1, pady=2)
        self.btn_start_tk.grid(row=4, column=1, pady=2)
        self.btn_record_mouse_tk.grid(row=5, column=1, pady=2)
        self.mouse_record_spinbox_tk.grid(row=6, column=1, pady=2)
        self.mouse_record_speed_spinbox_tk.grid(row=7, column=1, pady=2)
        self.btn_record_mouse_tk_start.grid(row=8, column=1, pady=2)

def closeWindow():
    import os

    win.destroy()
    os._exit(0)

if __name__ == "__main__":
    win = tk.Tk()
    w = int(260 * ScaleFactor)
    h = int(400 * ScaleFactor)
    dw = int(100 * ScaleFactor)
    dh = int(100 * ScaleFactor)
    geo = f"{w}x{h}+{dw}+{dh}"
    win.geometry(geo)
    win.title("鍵鼠自動腳本程式")
    win.attributes("-topmost", 1)
    frame = tk.Frame(win)
    frame.pack()

    App_start(frame)
    win.protocol("WM_DELETE_WINDOW", closeWindow)
    win.mainloop()


"""
Convert bags of patch features into tile level outputs.
From .H5PY to .PT.

Usage:
  h5_bag2tiles.py [options] [--help] [<args>...]
  h5_bag2tiles.py --version
  h5_bag2tiles.py (-h | --help)

Options:
  -h --help                   Show this string.
  --version                   Show version.

  --input_dir=<string>        Path to input directory containing h5 bag-level features.
  --output_dir=<string>       Path to output directory to save pt tile-level features.

Use `h5_bag2tiles.py --help` to show their options and usage
"""

from docopt import docopt
import os
import glob
import h5py
import torch
from tqdm import tqdm

if __name__ == '__main__':
    args = docopt(__doc__, help=False, options_first=True)

    if args['--help']:
        print(__doc__)
        exit()

    if args['--input_dir']:
        input_folder = args['--input_dir']
    else:
        input_folder = '/data/ANTICIPATE/outcome_prediction/MIL/github_testdata/output/features/0.5-mpp_512_256_epith-0.5/nuclear/h5_files/'

    if args['--output_dir']:
        output_folder = args['--output_dir']
    else:
        output_folder = '/data/ANTICIPATE/outcome_prediction/MIL/github_testdata/output/features/0.5-mpp_512_256_epith-0.5/nuclear/tiles_pt_files/'

    for wsi_path in tqdm(sorted(glob.glob(input_folder + "/*.h5"))):
        case = os.path.basename(wsi_path).split('.')[0]
        wsi_ftr_dict = h5py.File(wsi_path, 'r')
        for idx, coords in enumerate(wsi_ftr_dict['coords']):
            tile_name = f'{case}_{coords[0]}_{coords[1]}_{coords[2]}_{coords[3]}'
            ftrs = wsi_ftr_dict['features'][idx]
            output_dir = os.path.join(output_folder, case)
            os.makedirs(output_dir, exist_ok=True)
            torch.save(ftrs, os.path.join(output_dir, f'{tile_name}.pt'))


'''container module for TestDatabaseEvent'''
import json

from ...agent.database_event_row import DatabaseEventRow,ImageEventRow,TagEventRow

def _build_evt_msg(**kwargs):
    msg = {}
    msg[kwargs["id_nm"]] = kwargs["id_val"]
    msg["table_name"] = kwargs["t_nm"]
    msg["table_primary_key"] = kwargs["pk_vals"]
    msg["operation"] = kwargs["oper"]
    if "before" in kwargs:
        msg["before"] = kwargs["before"]
    if "after" in kwargs:
        msg["after"] = kwargs["after"]
    return msg

def _build_evt_row(**kwargs):
    row = {"values": {}}
    row["values"]["message_type"] = kwargs["m_type"]
    row["values"]["message"] = json.dumps(_build_evt_msg(**kwargs))
    return row

class TestDatabaseEventRow:
    '''tests for database event dtos in database_event module'''
    def test_image_metadata_event_from_json(self):
        '''tests the basic functioning of parsing generic metadata event from json string'''
        evt_msg = _build_evt_row(m_type="IMG_METADATA",id_nm="image_id",id_val=1
                ,t_nm="image_tag",pk_vals=[1,1],oper="INSERT")
        evt_dto = DatabaseEventRow.from_json(evt_msg["values"]["message_type"], evt_msg["values"]["message"])

        assert isinstance(evt_dto, ImageEventRow)
        assert not evt_dto.db_event_data
        assert evt_dto.record_id == 1
        assert evt_dto.table_name == "image_tag"
        assert evt_dto.table_primary_key == [1,1]
        assert evt_dto.db_event_type == "INSERT"
        assert evt_dto.image_id == evt_dto.record_id

    def test_tag_event_from_json(self):
        '''tests the basic functioning of parsing tag event from json string'''
        evt_msg = _build_evt_row(m_type="TAGS",id_nm="tag_id",id_val=1
                ,t_nm="tags",pk_vals=[1],oper="INSERT")
        evt_dto = DatabaseEventRow.from_json(evt_msg["values"]["message_type"], evt_msg["values"]["message"])

        assert isinstance(evt_dto, TagEventRow)
        assert not evt_dto.db_event_data
        assert evt_dto.record_id == 1
        assert evt_dto.table_name == "tags"
        assert evt_dto.table_primary_key == [1]
        assert evt_dto.db_event_type == "INSERT"
        assert evt_dto.tag_id == evt_dto.record_id

    def test_tag_update_from_json(self):
        '''tests the functioning of parsing an update event on a tag'''
        before_data={"name": "before_nm"}
        after_data={"name": "after_nm"}
        evt_msg = _build_evt_row(m_type="TAGS",id_nm="tag_id",id_val=1
            ,t_nm="tags",pk_vals=[1],oper="UPDATE",before=before_data,after=after_data)
        evt_dto = DatabaseEventRow.from_json(evt_msg["values"]["message_type"], evt_msg["values"]["message"])

        assert isinstance(evt_dto, TagEventRow)
        assert evt_dto.record_id == 1
        assert evt_dto.table_name == "tags"
        assert evt_dto.table_primary_key == [1]
        assert evt_dto.db_event_type == "UPDATE"
        assert evt_dto.tag_id == evt_dto.record_id
        assert evt_dto.db_event_data["UPDATE"]["before"]["name"] == "before_nm"
        assert evt_dto.db_event_data["UPDATE"]["after"]["name"] == "after_nm"


#!/usr/bin/env python

import jinja2
import os

from google.appengine.ext import db

from base_handler import BaseHandler
from model import Wish
from user_management import isUserAdmin, LOGIN_NEXT_PAGE_KEY

jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class WishPage(BaseHandler):
	URL = '/wish'
	def post(self):
		if(not isUserAdmin(self)):
			self.session[LOGIN_NEXT_PAGE_KEY] = self.URL
			self.redirect("/")
		if ((self.request.get('wishKey') != None) and (self.request.get('wishKey') != "")):
		#Modification of basic data
			wish = db.get(self.request.get('wishKey'))
			wish.title = self.request.get('title')
			wish.description = self.request.get('description')
			wish.put()
			self.redirect('/wish?wishKey=%s' % self.request.get('wishKey'))
		else:
			wish = Wish()
			wish.title = self.request.get('title')
			wish.description = self.request.get('description')
			wish.put()
			self.redirect('/wish?wishKey=%s' % wish.key())
	def get(self):
		if(not isUserAdmin(self)):
			self.redirect("/")
		if ((self.request.get('wishKey') != None) and (self.request.get('wishKey') != "")):
		# A single wish with editable ingredient list
			wish = db.get(self.request.get('wishKey'))
			template_values = {
				'wish': wish,
			}
			template = jinja_environment.get_template('templates/wish.html')
			self.printPage(wish.title, template.render(template_values), False, False)
		else:
		# All the wishes
			wishes = Wish.gql("ORDER BY title")
			template_values = {
			  'wishes': wishes,
			}
			template = jinja_environment.get_template('templates/wish_list.html')
			self.printPage("Fejlesztesi kivansagok", template.render(template_values), False, False)

class DeleteWishPage(BaseHandler):
	URL = '/deleteWish'
	def post(self):
		if(not isUserAdmin(self)):
			self.redirect("/")
		wish = db.get(self.request.get('wishKey'))
		wish.delete()
		self.redirect('/wish')


import openai
import os
from dotenv import load_dotenv
import os

def set_api_key() -> None:
    '''Set OpenAI api key'''
    load_dotenv()
    # get env variable
    openai.api_key = os.environ.get("API_KEY")

def send_prompt(prompt: str, model: str = "gpt-3.5-turbo") -> str:
    """
    Sends a prompt to the OpenAI chat model and returns the response.

    Args:
    - prompt (str): The prompt to send to the chat model.
    - model (str): The ID of the OpenAI chat model to use.

    Returns:
    - The response generated by the chat model as a string.
    """
    response = openai.ChatCompletion.create(
        model=model,
        messages=[
            {"role": "system", "content": "You are an AI assistant to help sellers improve their products given customer complaints."},
            {"role": "user", "content": prompt}
        ]
        )
    return response['choices'][0]['message']['content']


from flask import Flask, render_template, url_for, redirect,request,flash
from flask_mysqldb import MySQL

app = Flask(__name__)#to get current file and consider this file as main file
#MySQL Connection
app.config["MYSQL_HOST"] ="localhost"
app.config["MYSQL_USER"] ="root"
app.config["MYSQL_PASSWORD"] ="1430"
app.config["MYSQL_DB"] ="crud"
app.config["MYSQL_CURSORCLASS"] ="DictCursor" #To retrive the data in Dictionary format
mysql=MySQL(app) #connection string

#Loading Home Page
@app.route('/')
def home():
    con=mysql.connection.cursor()
    sql = "SELECT * FROM users"
    con.execute(sql)
    res=con.fetchall()
    return render_template("home.html",datas = res)

#Adding New User
@app.route("/addusers", methods=['POST', 'GET'])
def add_users():
    if request.method == "POST":
        name = request.form['name']
        age = request.form['age']
        city = request.form['city']
        con = mysql.connection.cursor()
        sql = "Insert into users(Name,city,age) values(%s,%s,%s)"
        con.execute(sql, [name, city, age])
        mysql.connection.commit()
        con.close()
        flash("User Details Added")
        return redirect(url_for("home"))
    return render_template("add_users.html")

#Edit Existing Users
@app.route("/editusers/<string:id>", methods=['POST', 'GET'])

def edit_users(id):
    con = mysql.connection.cursor()
    if request.method == "POST":
        name = request.form['name']
        age = request.form['age']
        city = request.form['city']
        con = mysql.connection.cursor()
        sql = "update  users set Name=%s,city=%s,age=%s where ID=%s"
        con.execute(sql, [name, city, age,id])
        mysql.connection.commit()
        con.close()
        flash("User Details Updated")
        return redirect(url_for("home"))
    sql = "select * from users where ID=%s"
    con.execute(sql,[id])
    res=con.fetchone() #bcoz fetching single value

    return render_template("edit_users.html",datas=res)

#Delete Existing Users
@app.route("/deleteusers/<string:id>", methods=['POST', 'GET'])
def delete_users(id):
    con = mysql.connection.cursor()
    sql = "delete from users where ID=%s"
    con.execute(sql, [id])
    mysql.connection.commit()
    con.close()
    flash("User Details Deleted")
    return redirect(url_for("home"))

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


import threading
import cv2
import torch, time
import mediapipe as mp
import numpy as np
from easydict import EasyDict as edict
from .action_model.action_rec import get_parameters
from .action_model.src.processor import Processor
# from skeleton_process.lib.camera import uv2xyz_ourcamera
# from skeleton_process.lib.camera_param import camera_param
pose = mp.solutions.pose.Pose(static_image_mode=False,
                              model_complexity=1,
                              smooth_landmarks=True,
                              enable_segmentation=True,
                              min_detection_confidence=0.5,
                              min_tracking_confidence=0.5)

def process1(img, c):
    neighbor_2base = np.array([(7, 3), (3, 2), (2, 1), (1, 0), (0, 4), (4, 5), (5, 6), (6, 8), (9, 10),
                               (11, 12), (11, 23), (23, 24), (24, 12),
                               (11, 13), (13, 15), (15, 21), (15,
                                                              19), (15, 17), (17, 19),
                               (12, 14), (14, 16), (16, 22), (16,
                                                              20), (16, 18), (18, 20),
                               (23, 25), (25, 27), (27, 31), (27, 29), (31, 29),
                               (24, 26), (26, 28), (28, 30), (28, 32), (30, 32)])
    w = img.shape[1]
    h = img.shape[0]
    for i in range(33):
        cx = int(c[i][0]*w)
        cy = int(c[i][1]*h)
        img = cv2.circle(img, (cx, cy), 5, (0, 255, 0), -1)
    for i in range(35):
        a = neighbor_2base[i][0]
        b = neighbor_2base[i][1]
        cx_0 = int(c[a][0]*w)
        cy_0 = int(c[a][1]*h)
        cx_1 = int(c[b][0]*w)
        cy_1 = int(c[b][1]*h)
        img = cv2.line(img, [cx_0, cy_0], [cx_1, cy_1], (255, 255, 255), 3)
    return img

def skeleton_tran(c):
    n = [-1, -1, -1, -1, 12, 14, 16, 22, 11, 13, 15, 21,
         24, 26, 28, 32, 23, 25, 27, 31, -1, 20, 22, 19, 21]
    skeleton = np.zeros([25, 3])
    for i in range(3):
        skeleton[0][i] = (c[23][i] + c[24][i]) / 2
        skeleton[1][i] = (c[11][i] + c[12][i] + c[23][i] + c[24][i]) / 4
        skeleton[2][i] = (c[9][i] + c[10][i] + c[11][i] + c[12][i]) / 4
        skeleton[3][i] = (c[1][i] + c[4][i]) / 2
        skeleton[20][i] = (c[11][i] + c[12][i]) / 2
    for i in range(25):
        if n[i] != -1:
            skeleton[i] = c[n[i]]
    a = skeleton[0].copy()
    for i in range(25):
        skeleton[i] -= a
    return skeleton

# def get2dpose(img):
#     img_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#     h, w = img.shape
#     results = pose.process(img_RGB)
#     keypoints_2d = np.zeros(shape=(33, 2))
#     if results.pose_landmarks:
#         for i in range(33):
#             keypoints_2d[i][0] = results.pose_landmarks.landmark[i].x * w
#             keypoints_2d[i][1] = results.pose_landmarks.landmark[i].y * h
#     return keypoints_2d

# def get3dpose(pose2d_1, pose2d_2):
#     keypoints_3d = np.zeros((33, 3))
#     for j in range(33):
#         keypoints_3d[j] = uv2xyz_ourcamera(lx=pose2d_1[1][j][0], ly=pose2d_1[1][j][1],
#                                            rx=pose2d_2[1][j][0], ry=pose2d_2[1][j][1],
#                                            camera_param=camera_param).squeeze()
#     return keypoints_3d

class Action_Recognizer(Processor):
    def __init__(self):
        args = get_parameters(2001)
        super().__init__(args)
        skeleton_test = np.zeros([3, 200, 25, 1])
        skeleton_test = torch.tensor(skeleton_test)
        self.eval(skeleton_test)
        self.action_class = {0: '出拳', 1: '格挡', 2: '肘击', 3: '出拳',
                                4: '踢腿', 5: '提膝', 6: '戒备', 7: '闪躲'}

class camera_thread(threading.Thread):
    def __init__(self, stop_event, timequeue, imgqueue, camera1=0, camera2=None):
        super().__init__()
        self.timequeue = timequeue
        self.q = imgqueue
        self.stop_event = stop_event
        try:
            self.cap1 = cv2.VideoCapture(camera1)
            self.cap2 = cv2.VideoCapture(camera2) if camera2 else None
        except:
            print('error camera')

    def run(self):
        if self.cap2:
            while not self.stop_event.is_set():
                if self.timequeue.qsize() > 0:
                    t, flag = self.timequeue.get()
                    ret1, img1 = self.cap1.read()
                    ret2, img2 = self.cap2.read()
                    if ret1 and ret2:
                        self.q.put((t, img1, img2, flag))
            pass
        else:
            while not self.stop_event.is_set():
                if self.timequeue.qsize() > 0:
                    t, flag = self.timequeue.get()
                    ret, img = self.cap1.read()
                    if ret:
                        self.q.put((t, img, flag))

class skeleton_single_Thread(threading.Thread):
    def __init__(self, stop_event, timequeue, corqueue):
        super().__init__()
        self.timequeue = timequeue
        self.co_queue = corqueue
        self.stop_event = stop_event
        self.cap = cv2.VideoCapture(0)

    def run(self):
        while not self.stop_event.is_set():
            time1 = time.time()
            if self.timequeue.qsize() > 0:
                t, flag = self.timequeue.get()
                ret, img = self.cap.read()
                cv2.imshow('ggg',img)
                cv2.waitKey(1)
                print('read time:',time.time() - time1)
                img_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                results = pose.process(img_RGB)
                skeleton = np.zeros((33, 3))
                if results.pose_world_landmarks:
                    for i in range(33):
                        skeleton[i][0] = results.pose_world_landmarks.landmark[i].x
                        skeleton[i][1] = results.pose_world_landmarks.landmark[i].y
                        skeleton[i][2] = results.pose_world_landmarks.landmark[i].z

                skeleton = skeleton_tran(skeleton)
                print('process time:',time.time() - time1)
                self.co_queue.put((t, skeleton, flag))
# class skeletonGet2_thread(threading.Thread):
#     def __init__(self, stop_event, imgqueue, corqueue):
#         super().__init__()
#         self.stop_event = stop_event
#         self.img_queue = imgqueue
#         self.co_queue = corqueue

#     def run(self):
#         while not self.stop_event.is_set():
#             if self.img_queue.qsize() > 0:
#                 t, img1, img2, flag = self.img_queue.get()
#                 pose2d_1 = get2dpose(img1)
#                 pose2d_2 = get2dpose(img2)
#                 skeleton = np.zeros((25, 3))
#                 if np.any(pose2d_1) and np.any(pose2d_2):
#                     pose3d = get3dpose(pose2d_1, pose2d_2)
#                     skeleton = skeleton_tran(pose3d)
#                 self.co_queue.put((t, skeleton, flag))

class skeletonGet_thread(threading.Thread):
    def __init__(self, stop_event, imgqueue, corqueue):
        super().__init__()
        self.stop_event = stop_event
        self.img_queue = imgqueue
        self.co_queue = corqueue

    def run(self):
        while not self.stop_event.is_set():

            if self.img_queue.qsize() > 0:
                t, img, flag = self.img_queue.get()
                img_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                results = pose.process(img_RGB)
                skeleton = np.zeros((33, 3))
                if results.pose_world_landmarks:
                    for i in range(33):
                        skeleton[i][0] = results.pose_world_landmarks.landmark[i].x
                        skeleton[i][1] = results.pose_world_landmarks.landmark[i].y
                        skeleton[i][2] = results.pose_world_landmarks.landmark[i].z

                skeleton = skeleton_tran(skeleton)
                self.co_queue.put((t, skeleton, flag))

class skSeg_thread(threading.Thread):
    def __init__(self, stop_event, corqueue, skeletonqueue):
        super().__init__()
        self.stop_event = stop_event
        self.co_queue = corqueue
        self.sk_queue = skeletonqueue

        seg_arg = edict()
        """
        window： 求方差的窗口大小
        min_action： 动作持续的最短时间
        var_threshold1：动作烈度较低时的方差阈值
        var_threshold2：动作烈度较高时的方差阈值
        var_diff_threshold：判断动作烈度的阈值
        """
        seg_arg.window = 10
        seg_arg.min_action = 5
        seg_arg.var_threshold = 300
        # seg_arg.var_threshold1 = 300
        # seg_arg.var_threshold2 = 400
        seg_arg.var_diff_threshold = 800
        self.args = seg_arg

    def run(self):
        args = self.args

        sk_list, t_list = [], []
        stop_flag = True
        var_threshold = args.var_threshold
        count = 0
        first_action = True

        while not self.stop_event.is_set():
            if self.co_queue.empty():
                continue
            t, skeleton, t_flag = self.co_queue.get()
            # 判断是否已经进行了一次动作识别
            # 若已经进行了一次识别，舍弃这一检查点区间中余下的骨架数据
            if not first_action:
                if t_flag == -2:
                    first_action = True
                else:
                    continue

            sk_list.append(skeleton)
            t_list.append(t)
            count += 1
            # 区间开始,初始化start
            if t_flag == -2:
                start = 0
                start_time = t_list[start]

            # 区间结束,若已经进行一次识别,不作处理
            # 未进行识别,截断序列,进行动作识别
            if t_flag == -1:
                if count - start - 5 <= args.min_action:
                    # 重置动作起始标志
                    print('动作长度不够')
                    stop_flag = True
                    # self.sk_queue.put((0, 0, 0))
                # 正常结束
                else:
                    end = count - 5
                    end_time = t_list[end]
                    skeleton_n = np.zeros([1, 200, 25, 3])
                    # print(1, end - start)
                    skeleton_n[0, :end-start, :,
                               :] = np.array(sk_list[start:end])
                    skeleton_n = skeleton_n.transpose(3, 1, 2, 0)
                    # self.sk_queue.put((skeleton_n, start_time, end_time))

                # 等待下一区间，回到初始状态
                count = 0
                sk_list = []
                t_list = []

            # 通过方差判断动作是否结束
            if count >= args.window:
                sk_temp = np.array(sk_list[-args.window:]) * 100
                var = 0
                for i in range(sk_temp.shape[1]):
                    for j in range(sk_temp.shape[2]):
                        var += sk_temp[:, i, j].var()
                # print(var)
                # 动作开始
                if var > var_threshold and stop_flag:
                    start = count - 5
                    start_time = t_list[start]
                    stop_flag = False

                # 动作结束
                elif var < var_threshold and not stop_flag:
                    # 持续时间过短，不认为是完整动作，不进行识别
                    if count - start - 5 <= args.min_action:

                        # 重置动作起始标志，保留部分骨架数据
                        stop_flag = True
                        count = args.window-1
                        sk_list = sk_list[-args.window+1:]
                        t_list = t_list[-args.window+1:]

                    # 正常结束
                    else:
                        end = count - 5
                        end_time = t_list[end]
                        skeleton_n = np.zeros([1, 200, 25, 3])
                        # print(2, end - start)
                        skeleton_n[0, :end-start, :,
                                   :] = np.array(sk_list[start:end])
                        skeleton_n = skeleton_n.transpose(3, 1, 2, 0)
                        # self.sk_queue.put((skeleton_n, start_time, end_time))
                        stop_flag = True
                        # 当前区间骨架分割完毕，等待下一区间，回到初始状态
                        first_action = False
                        count = 0
                        sk_list = []
                        t_list = []

class actionRec_thread(threading.Thread):
    def __init__(self, stop_event, skeletonqueue, actionqueue):
        super().__init__()
        self.stop_event = stop_event
        self.sk_queue = skeletonqueue
        self.ac_queue = actionqueue
        self.Action = Action_Recognizer()
        self.action_class = {-1: '无动作', 0: '出拳', 1: '格挡', 2: '肘击', 3: '出拳',
                                4: '踢腿', 5: '提膝', 6: '戒备', 7: '闪躲'}
    def run(self):
        while not self.stop_event.is_set():
            if self.sk_queue.qsize() > 0:
                skeleton, start, end = self.sk_queue.get()
                if not(end):
                    pro, action = 0, -1
                    self.ac_queue.put((start, end, action, pro))
                else:
                    skeleton = torch.tensor(skeleton)
                    pro, action = self.Action.eval(skeleton)
                    self.ac_queue.put((start, end, action, pro))
                print(start, end, self.action_class[action], pro)


from sklearn.linear_model import LinearRegression
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import statsmodels.api as sm

def p_value(y, x):
    X = sm.add_constant(x)
    est = sm.OLS(y, X)
    fit = est.fit()
    ttest = pd.DataFrame({'t': fit.tvalues, 'P>|t|':fit.pvalues})
    ttest.index = [f'x{i}' if i > 0 else 'const' for i in range(len(fit.tvalues)) ]
    ttest
    return ttest, fit.pvalues

def hedge_ratio(v_hat,u_hat):
    return v_hat.T.dot(u_hat)/v_hat.T.dot(v_hat)

def regress(x, y, N_predictions, window=0, plot=True, savefig=True, name="", plot_index=None):
    N = len(x)
    predictions = []
    scores = []
    mses = []
    p_values= []
    true_values = x[-N_predictions:]
    for i in range(N_predictions):
        w = window if (window == 0) else N - N_predictions - window + i
        X_train = x[w:N + i - N_predictions]
        X_test = x[N + i - N_predictions]
        y_train = y[w:N + i - N_predictions]
        y_test = y[N + i - N_predictions]
        reg = LinearRegression().fit(X_train, y_train)
        predictions.append(reg.predict(X_test.reshape(1, -1))[0])
        scores.append(reg.score(X_train, y_train))
        mses.append(mean_squared_error(y_test.reshape(1, -1), reg.predict(X_test.reshape(1, -1))))
        # xxx
        #X2 = sm.add_constant(X_train)
        #est = sm.OLS(y_train, X_train)
        #est2 = est.fit()
        #print(est2.summary())
        #print(dir(est2))
        #print(["%#6.3f" % (est2.pvalues[i]) for i in range(N_predictions)])
        _, pvs = p_value(y, x)
        for p in pvs:
            p_values.append(p)

    if plot:
        fig = plt.figure(figsize=(6,6))
        plt.scatter(y[-N_predictions:], predictions)
        plt.xlabel("True Values")
        plt.ylabel("Predictions")
        if savefig:
            plt.savefig(f'images/predictions_vs_true_{name}_window_{window}.png',bbox_inches='tight', pad_inches=0)

        fig = plt.figure(figsize=(14,5))
        plt.scatter(true_values, predictions, s=5, label="Predictions", color='orange')
        if plot_index is not None:
            plt.xticks(x[::500], [date.strftime("%d.%m.%Y") for date in plot_index[::500]])
        lower_bound = np.subtract(predictions, np.sqrt(mses))
        upper_bound = np.add(predictions, np.sqrt(mses))
        plt.fill_between(true_values.flatten(), lower_bound, upper_bound, facecolor='red', alpha=0.2)
        plt.plot(true_values.flatten(), lower_bound, linewidth=0.5, alpha=0.8, color='red')
        plt.plot(true_values.flatten(), upper_bound, linewidth=0.5, alpha=0.8, color='red', label='Mean squared error')
        plt.plot(x, y, linewidth=1, label="Training data")
        fig.autofmt_xdate()
        plt.grid(alpha=0.5)
        plt.ylabel("Normalized prices")
        plt.legend()
        plt.title(f'Generalized model 1 for predicting {name} prices')
        if savefig:
            plt.savefig(f'images/generalized_model_{name}_prices_window_{window}.png',bbox_inches='tight', pad_inches=0)

    print(f"All p-values > 0.05: {np.sum(np.array(p_values) > 0.05)}")
    return true_values, predictions, scores, mses


# -*- coding: utf-8 -*-
import traceback
from datetime import datetime
import time
import requests
# pls, look https://github.com/ping/instagram_private_api (19.01.2019)
# pip install git+https://git@github.com/ping/instagram_private_api.git@1.4.0
from instagram_private_api import Client, ClientCompatPatch
from PIL import Image
#
import utils

class Instagram:
    """
    Class for implementation of special functions by instagram
    """

    def __init__(self, login, password):
        self.API = Client(login, password)

    def spam_in_users_comments(self, user_ids, comment_text, last_photos_count=3):
        """
        Spam <comment_text> to <last_photos_count> photos for each user in <user_ids>

        :param
            - user_ids: User id list
            - comment_text: Spam text
            - last_photos_count: Number of last user's photos processed
        """

        # statistic
        count_of_posted_comments = 0
        total_processed = 0

        for user_counter, user_id in enumerate(user_ids):
            utils.print_message("Spam to account #{} (id={}, total {})".format(user_counter + 1, user_id, len(user_ids)))
            posted_comments = 0
            _first = True
            next_max_id = None
            while (next_max_id or _first) and posted_comments < last_photos_count:
                try:
                    _first = False
                    results = self.API.user_feed(user_id=user_id, max_id=next_max_id)
                    for item in results.get('items', []):
                        try:
                            id = item["id"]
                            #date = item["caption"]["created_at"]
                            #photo_url = item["image_versions2"]["candidates"][0]["url"]
                        except:
                            continue
                        utils.print_message("Posting comment #{} (total {}) to photo (id={})...".format(
                            posted_comments + 1, last_photos_count, id), 2, end="\r")
                        total_processed += 1
                        if self.API.post_comment( id, comment_text ).get("status") == "ok":
                            count_of_posted_comments += 1
                            posted_comments += 1
                            utils.print_message("Comment #{} (total {}) successfully posted to photo (id={})".format(
                                posted_comments + 1, last_photos_count, id), 2)
                        else:
                            utils.print_message("Comment #{} (total {}) was not posted to photo (id={})".format(
                                posted_comments + 1, last_photos_count, id), 2)
                    next_max_id = results.get('next_max_id')
                except:
                    utils.print_message(traceback.format_exc())
        return count_of_posted_comments

    def spam_in_timeline_comments(self, comment_text, K=5):
        """
        Post spam comments to <K> photos from feed timeline

        :param
            - comment_text: Spam text
            - K: Count of post in timeline
        """

        # statistic
        count_of_posted_comments = 0
        total_processed = 0

        _first = True
        next_max_id = None
        counter = K
        utils.print_message("Process {} posts from feed timeline".format(K))
        while (next_max_id or _first) and counter > 0:
            try:
                _first = False
                results = self.API.feed_timeline(max_id=next_max_id)
                for item in results.get('feed_items', []):
                    try:
                        id = item["media_or_ad"]["id"]
                        #date = item["media_or_ad"]["caption"]["created_at"]
                        #photo_url = item["media_or_ad"]["image_versions2"]["candidates"][0]["url"]
                    except:
                        continue
                    if counter <= 0: return
                    counter -= 1
                    utils.print_message("Posting comment to photo #{} (total {}) (id={})...".format(
                        K - counter, K, id), 2, end="\r")
                    total_processed += 1
                    if self.API.post_comment( id, comment_text ).get("status") == "ok":
                        utils.print_message("Comment #{} (total {}) successfully posted to photo (id={})".format(
                            K - counter, K, id), 2)
                        count_of_posted_comments += 1
                    else:
                        utils.print_message("Comment #{} (total {}) was not posted to photo (id={})".format(
                            K - counter, K, id), 2)
                next_max_id = results.get('next_max_id')
            except:
                utils.print_message(traceback.format_exc())
        return count_of_posted_comments

    def get_followings_accounts(self):
        """ Get followings accounts from current user """
        return self.API.user_following(self.API.authenticated_user_id).get('users')

    def spam_to_following_accounts_photo(self, comment_text, last_photos_count=3):
        """
        Post spam comments to <last_photos_count> last photos for each following account

        :param
            - comment_text: Spam text
            - last_photos_count: Number of last user's photos processed
        """
        count_of_loaded_photo = 0
        utils.print_message("Get followings accounts...")
        user_ids = [account['pk'] for account in self.get_followings_accounts()]
        utils.print_message("Process following accounts (total {})...".format(len(user_ids)))
        return self.spam_in_users_comments(user_ids, comment_text, last_photos_count)

    def get_id_by_username(self, username):
        """
        Get user id by username

        :param
            - username: Login of account
        """
        info = self.API.username_info(username).get("user")
        return info["pk"] if info else None

    def post_photo(self, fname, caption):
        """
        Post new photo

        :param
            - fname: File name of photo
            - caption: Description of the photo
        """
        utils.print_message("Posting photo '{}'...".format(fname), 2, end="\r")
        try:
            if self.API.post_photo(open(fname, "rb").read(), Image.open(fname).size, caption).get("status") == "ok":
                utils.print_message("Photo '{}' is successfully posted".format(fname), 2)
                return True
        except:
            utils.print_message(traceback.format_exc())
        utils.print_message("Photo '{}' is not posted".format(fname), 2)
        return False


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

import os, threading, sys
from datetime import datetime, date
import time
import conexion, zipfile
import variables

def backup():
    """
    Crea una copia de la bbdd.

    Realiza una copia de la base de datos y la comprime en formato zip en el destino solicitado.
    :return: neobackup: contiene la fecha de realización y nombre del backup
    Excepciones: Error al obtener ruta, imprime "Error backup"
    """
    try:
        conexion.Conexion().cerrarbbdd()
        backup = 'backup.zip'
        #destino = '/home/pruebas/copias/'
        copia = zipfile.ZipFile(backup, 'w')
        copia.write('empresa.sqlite', compress_type = zipfile.ZIP_DEFLATED)
        copia.close()
        neobackup = str(datetime.now()) + str(backup)
        os.rename(backup, neobackup)
        conexion.Conexion().abrirbbdd()
        return neobackup
    except:
        print('error backup')

def controlhab():
    """

    :return: void
    """
    variables.t = threading.Timer(0.5, controlhab)
    variables.t.daemon = True
    variables.t.start()
    fechahoy = time.strftime('%H:%M:%S')
    fechacontrol = '20:08:00'
    if str(fechacontrol) == str(fechahoy):
        actualizarhab()

def cerrartimer():
    """
    Cierra el timer.

    :return: void
    """
    variables.t.join(0)

'''
def controlhab():
    try:
        time_of_day = time.strftime("%H:%M:%S")
        target_time = '19:57:00'
        print(time_of_day)
        print(target_time)
        while True:
            while str(time_of_day) != str(target_time):
                time.sleep(.1)
            actualizarhab()
            while time_of_day == target_time:
                print('hola')
                time.sleep(.1)
    except:
        print('error control habitacion')

'''
def actualizarhab():
    """
    Aviso por consola.

    Muestra por consola que la actualización de una habitación se ha realizado correctamente.
    :return:void
    """
    print('hola actualizasdor de habitaciones')


from __future__ import absolute_import, division, print_function, unicode_literals

import argparse
import io
import math
import os
from collections import defaultdict
from os import path
from timeit import default_timer as timer

import numpy as np
import tensorflow as tf

from batcher import Batcher
from querysum_model import QuerySumModel
from vocabulary import Vocabulary

class RunContext:
    pass

class ProcessedBatch:
    def __init__(self, documents, document_lengths, queries, query_lengths, references=None, reference_lengths=None,
                 pointer_reference=None, pointer_reference_lengths=None, pointer_switch=None,
                 pointer_switch_lengths=None):
        self.documents = documents
        self.document_lengths = document_lengths
        self.queries = queries
        self.query_lengths = query_lengths
        self.references = references
        self.reference_lengths = reference_lengths
        self.pointer_reference = pointer_reference
        self.pointer_reference_lengths = pointer_reference_lengths
        self.pointer_switch = pointer_switch
        self.pointer_switch_lengths = pointer_switch_lengths

class Hypothesis:
    def __init__(self, probability, voc_argmax, attention_argmax, next_decoder_state, attention_softmax,
                 pointer_enabled):
        self.probability = probability
        self.voc_argmax = voc_argmax
        self.attention_argmax = attention_argmax
        self.next_decoder_state = next_decoder_state
        self.attention_softmax = attention_softmax
        self.pointer_enabled = pointer_enabled

def main(unused):
    parser = argparse.ArgumentParser()
    parser.add_argument('embedding_path')
    parser.add_argument('vocabulary_dir')
    parser.add_argument('--training_dir')
    parser.add_argument('--validation_dir')
    parser.add_argument('--decode_dir')
    parser.add_argument('--decode_out_dir')
    parser.add_argument('--mode', choices=['train', 'validate', 'decode'], default='train')
    parser.add_argument('--logdir')
    parser.add_argument('--batch_size', type=int, default=30)
    parser.add_argument('--validation_interval', type=int, default=20000)
    parser.add_argument('--beam_width', type=int, default=5)
    parser.add_argument('--max_output_length', type=int, default=32)
    parser.add_argument('--target_vocabulary_size', type=int, default=20000)
    parser.add_argument('--synthetic', action='store_true')
    parser.add_argument('--allow_gpu_growth', action='store_true')
    parser.add_argument('--collect_run_metadata', action='store_true')
    parser.add_argument('--log_weight_images', action='store_true')
    options = parser.parse_args()

    if options.mode == 'decode':
        # Batching not supported in decoding
        options.batch_size = 1

    embedding_words, word_dict, word_embedding_dim = load_word_embeddings(options.embedding_path)

    vocabulary = Vocabulary()
    summary_vocabulary_path = path.join(options.vocabulary_dir, 'summary_vocabulary.txt')
    vocabulary.add_from_file(summary_vocabulary_path, options.target_vocabulary_size - len(vocabulary.words))

    document_vocabulary_path = path.join(options.vocabulary_dir, 'document_vocabulary.txt')

    # Add the most common words from vocabulary
    vocabulary.add_from_file(document_vocabulary_path, 150000)

    # Add additional common words from loaded embeddings
    vocabulary.add_words(embedding_words[:100000])

    run(options, word_dict, word_embedding_dim, vocabulary)

def load_word_embeddings(path_):
    words = []
    word_dict = {}
    with io.open(path_, encoding='utf-8') as file:
        for line in file:
            elements = line.split()
            word = elements[0]
            embedding = np.array(elements[1:], dtype=np.float32)
            words.append(word)
            word_dict[word] = embedding

    # Get dimension from arbitrary element
    n_dim = len(word_dict[next(iter(word_dict))])
    return words, word_dict, n_dim

def create_embedding_matrix(vocabulary, word_dict, word_embedding_dim):
    embeddings_matrix = np.asarray([arr for arr in word_dict.values()], dtype=np.float32)
    mean = np.mean(embeddings_matrix, axis=0)
    sd = embeddings_matrix.std(axis=0)

    embeddings = np.random.normal(mean, sd, [len(vocabulary.words), word_embedding_dim]).astype(np.float32)

    for index, word in enumerate(vocabulary.words):
        embedding = word_dict.get(word)
        if embedding is not None:
            embeddings[index] = embedding
    return embeddings

def run(options, word_dict, word_embedding_dim, vocabulary):
    embeddings = create_embedding_matrix(vocabulary, word_dict, word_embedding_dim)

    model = QuerySumModel(options.mode, word_dict, word_embedding_dim, vocabulary, embeddings,
                          options.target_vocabulary_size)

    training_batcher = None
    validation_batcher = None
    decode_batcher = None
    if options.mode == 'decode':
        decode_batcher = Batcher(options.decode_dir, options.batch_size, options.synthetic, reference_looping=False)
    else:
        if options.mode == 'train':
            training_batcher = Batcher(options.training_dir, options.batch_size, options.synthetic)
        validation_batcher = Batcher(options.validation_dir, options.batch_size, options.synthetic, max_count=3000)

    training_summary_op = None
    if options.mode == 'train':
        tf.summary.scalar('main_train_loss', model.main_train_loss, collections=['training'])
        tf.summary.scalar('final_train_loss', model.final_train_loss, collections=['training'])
        training_summary_op = tf.summary.merge_all('training')

    weight_images_summary_op = None
    if options.log_weight_images:
        for variable in tf.trainable_variables():
            name = variable.name
            shape = variable.get_shape()

            height = shape.dims[0].value
            if shape.ndims == 1:
                width = 1
            elif shape.ndims == 2:
                width = shape.dims[1].value
            else:
                continue

            if width < height:
                variable = tf.transpose(variable)
                width, height = height, width

            tensor_as_image = tf.reshape(variable, [1, height, width, 1])

            tf.summary.image(name, tensor_as_image, collections=['weight_images'])
        weight_images_summary_op = tf.summary.merge_all('weight_images')

    saver = tf.train.Saver(max_to_keep=2)
    best_validation_saver = tf.train.Saver(max_to_keep=2)

    checkpoint_interval = 0
    if options.mode == 'train':
        checkpoint_interval = 60

    supervisor_saver = best_validation_saver if options.mode == 'decode' else saver

    sv = tf.train.Supervisor(logdir=options.logdir,
                             saver=supervisor_saver,
                             summary_op=None,
                             summary_writer=None,
                             save_model_secs=checkpoint_interval,
                             global_step=model.global_step)

    config = tf.ConfigProto(allow_soft_placement=True)
    config.gpu_options.allow_growth = options.allow_gpu_growth

    with sv.managed_session(config=config) as sess:
        if len(sv.saver.last_checkpoints) > 0:
            print("Restored from saved model '{}'".format(sv.saver.last_checkpoints[-1]))

        training_writer = tf.summary.FileWriter(path.join(options.logdir, 'training'))
        validation_writer = tf.summary.FileWriter(path.join(options.logdir, 'validation'))

        initial_epoch = sess.run(model.epoch)

        run_context = RunContext()

        run_context.options = options
        run_context.model = model
        run_context.session = sess
        run_context.saver = saver
        run_context.best_validation_saver = best_validation_saver
        run_context.training_batcher = training_batcher
        run_context.validation_batcher = validation_batcher
        run_context.decode_batcher = decode_batcher
        run_context.vocabulary = vocabulary
        run_context.word_dict = word_dict
        run_context.word_embedding_dim = word_embedding_dim
        run_context.training_writer = training_writer
        run_context.validation_writer = validation_writer
        run_context.training_summary_op = training_summary_op
        run_context.weight_images_summary_op = weight_images_summary_op
        run_context.collect_run_metadata = options.collect_run_metadata
        run_context.epoch = initial_epoch
        run_context.validation_batch_interval = math.ceil(
            run_context.options.validation_interval / run_context.options.batch_size)

        # Log weight images once before starting (or resuming) training
        if options.mode == 'train' and run_context.options.log_weight_images:
            [weight_images_summary, global_step_value] = sess.run(
                [run_context.weight_images_summary_op, model.global_step])
            run_context.training_writer.add_summary(weight_images_summary, global_step_value)

        try:
            if options.mode == 'train':
                while not sv.should_stop():
                    run_epoch(run_context, training=True)
                    print("Finished epoch {}".format(run_context.epoch))
            elif options.mode == 'validate':
                raise Exception("Validate mode is no longer supported")
                # run_epoch(run_context, training=False)
                print("Finished validating.")
            else:
                run_decode(run_context)
                print("Finished decoding.")

        finally:
            # Manually save after ctrl-c signal
            run_context.training_writer.flush()
            run_context.validation_writer.flush()
            if options.mode == 'train':
                saver.save(sess, sv.save_path, global_step=model.global_step)

                print("Model saved as '{}'".format(sv.saver.last_checkpoints[-1]))

def run_epoch(run_context, training):
    batcher = run_context.training_batcher if training else run_context.validation_batcher
    model = run_context.model
    sess = run_context.session

    if not training:
        total_validation_samples = 0
        total_validation_loss = 0

    performance_count_batches = 0
    performance_count_samples = 0
    performance_count_input_tokens = 0
    logging_timer_start = timer()

    for batch_index, raw_batch in enumerate(batcher.get_batches(shuffle_batches=training)):
        documents, queries, references, entities = raw_batch[:4]

        batch = process_batch(run_context.options, run_context.vocabulary, documents, queries, references, entities)
        actual_batch_size = batch.documents.shape[0]
        document_length = batch.documents.shape[1]

        feed_dict = {
            model.documents_placeholder: batch.documents,
            model.document_lengths_placeholder: batch.document_lengths,
            model.queries_placeholder: batch.queries,
            model.query_lengths_placeholder: batch.query_lengths,
            model.references_placeholder: batch.references,
            model.reference_lengths_placeholder: batch.reference_lengths,
            model.pointer_reference_placeholder: batch.pointer_reference,
            model.pointer_switch_placeholder: batch.pointer_switch
        }

        if training:
            run_options = None
            run_metadata = None
            if run_context.collect_run_metadata:
                run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
                run_metadata = tf.RunMetadata()

            _, loss_value, summary, global_step_value = sess.run(
                [model.minimize_operation, model.final_train_loss, run_context.training_summary_op, model.global_step],
                feed_dict=feed_dict, options=run_options, run_metadata=run_metadata)

            run_context.training_writer.add_summary(summary, global_step_value)

            if run_context.collect_run_metadata:
                run_context.training_writer.add_graph(sess.graph, global_step_value)
                run_context.training_writer.add_run_metadata(run_metadata, 'train_{}'.format(global_step_value),
                                                             global_step_value)
                # Only collect metadata once
                run_context.collect_run_metadata = False
        else:
            if run_context.options.mode == 'train':
                (loss_value, vocabulary_argmax_value, attention_argmax_values, pointer_enabled_values,
                 global_step_value) = sess.run(
                    [model.main_train_loss, model.train_vocabulary_argmax, model.train_attention_argmax,
                     model.train_pointer_enabled, model.global_step],
                    feed_dict=feed_dict)
            else:
                (loss_value, vocabulary_argmax_value, attention_argmax_values, pointer_enabled_values,
                 global_step_value) = sess.run(
                    [model.main_loss, model.vocabulary_argmax, model.attention_argmax, model.pointer_enabled,
                     model.global_step],
                    feed_dict=feed_dict)
            total_validation_samples += actual_batch_size
            total_validation_loss += loss_value * actual_batch_size

        performance_count_batches += 1
        performance_count_samples += actual_batch_size
        performance_count_input_tokens += actual_batch_size * document_length

        now = timer()
        since_log = now - logging_timer_start
        if since_log >= 10.:
            print("============================")

            if training:
                # Alert if weights grow very large
                variables = tf.trainable_variables()
                for variable in variables:
                    variable_value = sess.run(variable)
                    min_value = np.min(variable_value)
                    max_value = np.max(variable_value)

                    if max(abs(min_value), abs(max_value)) > 100.:
                        print("{}\n  min: {}\n  max: {}".format(variable.name, min_value, max_value))

            run_type = "training" if training else "validation"
            print("Run type: {}".format(run_type))
            print("Epoch: {}, step: {}, batch: {}".format(run_context.epoch + 1,
                                                          global_step_value + 1,
                                                          batch_index + 1))

            epoch_progress = 100 * batch_index / batcher.num_batches
            print("Epoch progress: {0:.2f}%".format(epoch_progress))

            if not training:
                batch_debug_output = list(
                    zip(vocabulary_argmax_value, attention_argmax_values, pointer_enabled_values, documents, queries,
                        references))

                # Only print first item in batch
                batch_debug_output = [batch_debug_output[0]]

                for (output_summary, attention_argmax_value, pointer_enabled_value, document, query,
                     reference) in batch_debug_output:
                    print("Q: {}".format(" ".join(query)))
                    print("I: {}".format(" ".join(document[:20])))
                    print("R: {}".format(" ".join(reference)))
                    output_train_string = vocabularized_to_string(output_summary, attention_argmax_value,
                                                                  pointer_enabled_value, document,
                                                                  run_context.vocabulary)
                    print("O: {}".format(output_train_string))
            print("Loss: {}".format(loss_value))

            ms_per_batch = 1000 * since_log / performance_count_batches
            ms_per_sample = 1000 * since_log / performance_count_samples
            ms_per_input_token = 1000 * since_log / performance_count_input_tokens

            print("Time per batch: {0:.3f} ms".format(ms_per_batch))
            print("Time per sample: {0:.3f} ms".format(ms_per_sample))
            print("Time per input token: {0:.3f} ms".format(ms_per_input_token))

            print("============================")

            performance_count_batches = 0
            performance_count_samples = 0
            performance_count_input_tokens = 0
            logging_timer_start = now

        if training:
            if global_step_value % run_context.validation_batch_interval == 0:
                run_epoch(run_context, training=False)

                performance_count_batches = 0
                performance_count_samples = 0
                performance_count_input_tokens = 0
                logging_timer_start = timer()

    if not training:
        mean_validation_loss = total_validation_loss / total_validation_samples
        mean_validation_loss_summary = tf.Summary(value=[
            tf.Summary.Value(tag="main_loss", simple_value=mean_validation_loss),
        ])
        run_context.validation_writer.add_summary(mean_validation_loss_summary, global_step_value)
        run_context.training_writer.flush()
        run_context.validation_writer.flush()

        # Save checkpoint if validation loss has improved
        best_validation_loss_value = sess.run(model.best_validation_loss)
        if mean_validation_loss < best_validation_loss_value:
            # Update stored best validation
            sess.run(model.best_validation_assign,
                     feed_dict={
                         model.new_best_validation: mean_validation_loss
                     })

            best_validation_path = path.join(run_context.options.logdir, 'best_validation.ckpt')
            run_context.best_validation_saver.save(sess, best_validation_path, global_step=model.global_step)

    if training:
        run_context.epoch = sess.run(model.increment_epoch_op)

        # Log weight matrices only each epoch. It seems that logging them frequently often gives very large
        # events.out.tfevents.* files.
        if run_context.options.log_weight_images:
            weight_images_summary = sess.run(run_context.weight_images_summary_op)
            run_context.training_writer.add_summary(weight_images_summary, global_step_value)

def run_decode(run_context):
    model = run_context.model
    sess = run_context.session
    batcher = run_context.decode_batcher

    initial_last_output = [run_context.vocabulary.word_to_id('<GO>')]

    print("Decoding...")

    for batch_index, raw_batch in enumerate(batcher.get_batches(shuffle_batches=False)):
        documents, queries, document_ids, query_ids = raw_batch

        batch = process_batch(run_context.options, run_context.vocabulary, documents, queries)

        feed_dict = {
            model.documents_placeholder: batch.documents,
            model.document_lengths_placeholder: batch.document_lengths,
            model.queries_placeholder: batch.queries,
            model.query_lengths_placeholder: batch.query_lengths
        }

        encoder_outputs, query_state, next_decoder_state, partial_query_score, partial_encoder_score = sess.run(
            [model.encoder_outputs, model.query_last, model.final_encoder_state, model.query_attention_partial_score,
             model.encoder_state_attention_partial_scores], feed_dict=feed_dict
        )

        beam_width = run_context.options.beam_width
        max_output_length = run_context.options.max_output_length
        num_finished_hypotheses = 0

        finished_hypotheses_probs = []
        finished_hypotheses_voc_argmax = []
        finished_hypotheses_attention_argmax = []
        finished_hypotheses_attention_softmax = []
        finished_hypotheses_pointer_enabled = []

        best_hypotheses = []

        feed_dict = {
            model.documents_placeholder: batch.documents,
            model.document_lengths_placeholder: batch.document_lengths,
            model.queries_placeholder: batch.queries,
            model.query_lengths_placeholder: batch.query_lengths,
            model.beam_width_placeholder: beam_width,
            model.decode_last_output_placeholder: initial_last_output,
            model.initial_decoder_state_placeholder: next_decoder_state,
            model.pre_computed_encoder_states_placeholder: encoder_outputs,
            model.pre_computed_query_state_placeholder: query_state,
            model.query_attention_partial_score_placeholder: partial_query_score,
            model.encoder_state_attention_partial_scores_placeholder: partial_encoder_score
        }

        (best_hypotheses_voc_argmax, best_hypotheses_probs, best_hypotheses_next_decoder_states,
         best_hypotheses_attention_argmax, attention_softmax, pointer_enabled) = sess.run(
            [model.top_k_vocabulary_argmax, model.top_k_probabilities, model.one_step_decoder_state,
             model.attention_argmax, model.attention_softmax, model.pointer_enabled],
            feed_dict=feed_dict)

        for i in range(beam_width):
            best_hypotheses.append(Hypothesis(best_hypotheses_probs[0, i],
                                              [best_hypotheses_voc_argmax[0, i]],
                                              [best_hypotheses_attention_argmax[0]],
                                              best_hypotheses_next_decoder_states[0],
                                              [attention_softmax[0]],
                                              [pointer_enabled[0]]))

        time_step = 1
        while num_finished_hypotheses < beam_width:

            hypotheses = []

            for hypothesis in best_hypotheses:
                attended_token = batch.documents[0, hypothesis.attention_argmax[-1]]
                vocabulary_token = hypothesis.voc_argmax[-1]
                last_pointer_enabled = hypothesis.pointer_enabled[-1]
                last_output_index = attended_token if last_pointer_enabled == 1 else vocabulary_token

                feed_dict = {
                    model.documents_placeholder: batch.documents,
                    model.document_lengths_placeholder: batch.document_lengths,
                    model.queries_placeholder: batch.queries,
                    model.query_lengths_placeholder: batch.query_lengths,
                    model.beam_width_placeholder: beam_width,
                    model.decode_last_output_placeholder: [last_output_index],
                    model.initial_decoder_state_placeholder: [hypothesis.next_decoder_state],
                    model.pre_computed_encoder_states_placeholder: encoder_outputs,
                    model.pre_computed_query_state_placeholder: query_state,
                    model.query_attention_partial_score_placeholder: partial_query_score,
                    model.encoder_state_attention_partial_scores_placeholder: partial_encoder_score
                }

                (top_k_voc_argmax, top_k_probs, next_decoder_state, attention_argmax, attention_softmax,
                 pointer_enabled) = sess.run(
                    [model.top_k_vocabulary_argmax, model.top_k_probabilities, model.one_step_decoder_state,
                     model.attention_argmax, model.attention_softmax, model.pointer_enabled],
                    feed_dict=feed_dict)

                for j in range(beam_width):
                    new_prob = hypothesis.probability * top_k_probs[0, j]
                    new_voc_argmax = hypothesis.voc_argmax + [top_k_voc_argmax[0, j]]
                    new_attention_argmax = hypothesis.attention_argmax + [attention_argmax[0]]
                    new_attention_softmax = hypothesis.attention_softmax + [attention_softmax[0]]
                    new_pointer_enabled = hypothesis.pointer_enabled + [pointer_enabled[0]]
                    hypotheses.append(Hypothesis(
                        new_prob, new_voc_argmax, new_attention_argmax, next_decoder_state[0], new_attention_softmax,
                        new_pointer_enabled))

            hypotheses.sort(key=lambda hyp: hyp.probability, reverse=True)

            added_to_next_beam = 0
            best_hypotheses = []

            num_considered_hypotheses = 0
            while num_finished_hypotheses < beam_width and added_to_next_beam < beam_width:

                hypothesis = hypotheses[num_considered_hypotheses]

                if hypothesis.voc_argmax[-1] == run_context.vocabulary.word_to_id(
                        '<EOS>') or time_step + 1 >= max_output_length:
                    num_finished_hypotheses += 1
                    finished_hypotheses_probs.append(hypothesis.probability)
                    finished_hypotheses_voc_argmax.append(hypothesis.voc_argmax)
                    finished_hypotheses_attention_argmax.append(hypothesis.attention_argmax)
                    finished_hypotheses_attention_softmax.append(hypothesis.attention_softmax)
                    finished_hypotheses_pointer_enabled.append(hypothesis.pointer_enabled)
                else:
                    best_hypotheses.append(hypothesis)
                    added_to_next_beam += 1

                num_considered_hypotheses += 1
            time_step += 1

        final_hypothesis_prob = -np.inf

        for i in range(beam_width):
            if finished_hypotheses_probs[i] > final_hypothesis_prob:
                final_hypothesis_prob = finished_hypotheses_probs[i]
                final_hypothesis_voc_argmax = finished_hypotheses_voc_argmax[i]
                final_hypothesis_attention_argmax = finished_hypotheses_attention_argmax[i]
                final_hypothesis_attention_softmax = finished_hypotheses_attention_softmax[i]
                final_hypothesis_pointer_enabled = finished_hypotheses_pointer_enabled[i]

        write_output_summaries(run_context,
                               [np.array(final_hypothesis_voc_argmax)],
                               [np.array(final_hypothesis_attention_argmax)],
                               [np.array(final_hypothesis_pointer_enabled)],
                               documents[0], document_ids, query_ids)

        document_query_id = '{}.{}.txt'.format(document_ids[0], query_ids[0])

        softmax_path = path.join(run_context.options.decode_out_dir, 'attention_softmax')
        if not path.isdir(softmax_path):
            os.makedirs(softmax_path)
        np.savetxt(path.join(softmax_path, document_query_id), final_hypothesis_attention_softmax)

        output_prob_path = path.join(run_context.options.decode_out_dir, 'output_probabilities')
        if not path.isdir(output_prob_path):
            os.makedirs(output_prob_path)
        np.savetxt(path.join(output_prob_path, document_query_id), [final_hypothesis_prob])

def process_batch(options, vocabulary, document_batch, query_batch, reference_batch=None, entities_batch=None):
    # Limit document size
    max_document_tokens = 800
    document_batch = [document[:max_document_tokens] for document in document_batch]

    # Add special <EOS> symbol
    for batch_texts in [query_batch, reference_batch]:
        if batch_texts is not None:
            for text in batch_texts:
                text.append('<EOS>')

    documents = vocabularize_batch(document_batch, vocabulary)
    queries = vocabularize_batch(query_batch, vocabulary)
    if reference_batch is not None:
        references, pointer_reference, pointer_switch = process_references(document_batch, reference_batch,
                                                                           entities_batch, vocabulary,
                                                                           options.target_vocabulary_size)
    actual_batch_size = len(document_batch)
    processed_batch_elements = []
    if reference_batch is None:
        loop_elements = (documents, queries)
    else:
        loop_elements = (documents, queries, references, pointer_reference, pointer_switch)
    for texts in loop_elements:
        data_shape = list(texts[0].shape[1:])

        longest_text_length = max(map(len, texts))
        padded = np.zeros([actual_batch_size, longest_text_length] + data_shape, dtype=np.int32)
        lengths = np.zeros([actual_batch_size], dtype=np.int32)

        for index, text in enumerate(texts):
            length = np.size(text, 0)
            padded[index, :length] = text
            lengths[index] = length

        processed_batch_elements.append(padded)
        processed_batch_elements.append(lengths)
    batch = ProcessedBatch(*processed_batch_elements)
    return batch

def process_references(document_batch, reference_batch, entities_batch, vocabulary, target_vocabulary_size):
    batch_size = len(document_batch)
    batched_reference = [None] * batch_size
    batched_pointer_reference = [None] * batch_size
    batched_pointer_switch = [None] * batch_size

    pad_id = vocabulary.word_to_id('<PAD>')
    unk_id = vocabulary.word_to_id('<UNK>')

    for batch_index, (document, reference, entities) in enumerate(zip(document_batch, reference_batch, entities_batch)):
        reference_length = len(reference)
        vocabularized_reference = vocabularize(reference, vocabulary, target_vocabulary_size - 1)
        pointer_reference = np.zeros([reference_length], dtype=np.int32)
        pointer_switch = np.zeros([reference_length], dtype=np.int32)

        # Build index to look up words in document
        document_index = defaultdict(list)
        for index, token in enumerate(document):
            document_index[token].append(index)

        # Sort to find longest entities first (e.g. 'President of America' before 'America')
        entities.sort(key=len, reverse=True)
        pointed_reference_entity_indices = set()
        for entity_tokens in entities:

            # Find the first occurrence of the whole entity in the reference
            for ref_start_index in range(len(reference)):
                ref_end_index = ref_start_index + len(entity_tokens)

                # Make sure that entities in the reference don't overlap
                if any([index in pointed_reference_entity_indices for index in range(ref_start_index, ref_end_index)]):
                    continue

                if entity_tokens == reference[ref_start_index:ref_end_index]:
                    # Find the first occurrence of the entity in the document
                    for doc_start_index in range(len(document)):
                        doc_end_index = doc_start_index + len(entity_tokens)
                        if entity_tokens == document[doc_start_index:doc_end_index]:
                            for entity_index in range(len(entity_tokens)):
                                reference_entity_index = ref_start_index + entity_index
                                pointed_reference_entity_indices.add(reference_entity_index)
                                pointer_switch[reference_entity_index] = 1
                                pointer_reference[reference_entity_index] = doc_start_index + entity_index
                                vocabularized_reference[reference_entity_index] = unk_id
                            break

        # Set pointer for words not in summary vocabulary
        for index, token in enumerate(reference):
            if vocabularized_reference[index] == unk_id and pointer_switch[index] == 0:
                occurrences = document_index.get(token)
                if occurrences is not None:
                    pointer_switch[index] = 1
                    pointer_reference[index] = occurrences[0]
                else:
                    # To avoid teaching the model to output <unk>, loss is masked for these time steps
                    vocabularized_reference[index] = pad_id

        batched_reference[batch_index] = vocabularized_reference
        batched_pointer_reference[batch_index] = pointer_reference
        batched_pointer_switch[batch_index] = pointer_switch
    return batched_reference, batched_pointer_reference, batched_pointer_switch

def write_output_summaries(run_context, vocabularized_summaries, batched_attention_indices, batched_pointer_enabled,
                           document, document_ids, query_ids):
    summary_dir = path.join(run_context.options.decode_out_dir, 'summaries')

    if not path.isdir(summary_dir):
        os.makedirs(summary_dir)
    output_summaries = list(zip(vocabularized_summaries, batched_attention_indices, batched_pointer_enabled,
                                document_ids, query_ids))
    for vocabularized_summary, attention_indices, pointer_enabled, document_id, query_id in output_summaries:
        out_name = '{}.{}.txt'.format(document_id, query_id)
        out_path = path.join(summary_dir, out_name)
        summary = vocabularized_to_string(vocabularized_summary, attention_indices, pointer_enabled, document,
                                          run_context.vocabulary)
        with io.open(out_path, 'w', encoding='utf-8') as file:
            file.write(summary)

def vocabularize(text, vocabulary, max_id=np.inf):
    unk_id = vocabulary.word_to_id('<UNK>')
    vocabularized = [vocabulary.word_to_id(word) for word in text]
    vocabularized = [id_ if id_ <= max_id else unk_id for id_ in vocabularized]
    return np.array(vocabularized, dtype=np.int32)

def vocabularize_batch(texts, vocabulary, max_id=np.inf):
    return [vocabularize(text, vocabulary, max_id) for text in texts]

def vocabularized_to_string(vocabularized, attention_indices, pointer_enabled, document, vocabulary):
    eos_id = vocabulary.word_to_id('<EOS>')
    python_list = vocabularized.tolist()
    try:
        end_index = python_list.index(eos_id)
    except ValueError:
        end_index = len(python_list)

    out_tokens = []

    for index, token in enumerate(python_list[:end_index]):
        if pointer_enabled[index] == 1:
            out_token = document[attention_indices[index]]
            print("Pointer replace: {}".format(out_token))
        else:
            out_token = vocabulary.id_to_word(token)
        out_tokens.append(out_token)

    return " ".join(out_tokens)

if __name__ == '__main__':
    tf.app.run()


'''
Created on 03.05.2015

@author: michael
'''
from injector import singleton

REQ_SET_EVENT = "set event"
REQ_SET_DOCUMENT = "set document"
REQ_SAVE_CURRENT_DOCUMENT = "save current document"
REQ_GOTO_FIRST_EVENT = "goto first event"
REQ_GOTO_FIRST_DOCUMENT = "goto first document"
REQ_SAVE_CURRENT_EVENT = "save current event"
REQ_QUIT = "quit"
REQ_SAVE_ALL = "save_all"

CONF_DOCUMENT_CHANGED = 'document changed'
CONF_EVENT_CHANGED = 'event changed'

CONF_SETUP_FINISHED = 'setup successful finished'

ERROR_MESSAGE = 'error message'

class Message:

    def __init__(self, key, **params):

        self.key = key
        for name, value in params.items():
            setattr(self, name, value)

    def __eq__(self, other):
        '''
        The comparison allows to compare a message directly
        to its key (makes the code more readable)
        '''
        try:
            return self.key == other.key
        except AttributeError:
            return self.key == other

    def __str__(self):
        return self.key

@singleton
class MessageBroker:

    def __init__(self):

        self.receivers = []

    def subscribe(self, receiver):

        self.receivers.append(receiver)

    def send_message(self, message):

        for receiver in self.receivers:
            receiver.receive_message(message)

    def show_error(self, errormessage):

        self.send_message(Message(ERROR_MESSAGE,
                                  messagetype='error',
                                  message=errormessage))

    def show_warning(self, errormessage):

        self.send_message(Message(ERROR_MESSAGE,
                                  messagetype='warning',
                                  message=errormessage))

    def show_info(self, errormessage):

        self.send_message(Message(ERROR_MESSAGE,
                                  messagetype='info',
                                  message=errormessage))

    def show_debug(self, errormessage):

        self.send_message(Message(ERROR_MESSAGE,
                                  messagetype='debug',
                                  message=errormessage))


"""Product managing fixtures

Module contains fixtures for creating, deleting, getting products in different
testing scopes.
"""
from asyncio import TaskGroup
from typing import Optional

import pytest
from aiohttp import ClientResponse

from bookshop_test_framework.models.product import Product
from bookshop_test_framework.models.user import User
from bookshop_test_framework.tools.api import ProductApi
from bookshop_test_framework.tools.api import UserApi
from bookshop_test_framework.tools.data_generator.test_data_generator import TestDataGenerator

@pytest.fixture(scope="session")
async def session_product(
    products_api: ProductApi, users_api: UserApi, session_manager_user: User, http_task_group: TaskGroup
) -> Product:
    """Generate, create and return customer user in session scope

    :param products_api: initialized Product API
    :param users_api: initialized User API
    :param session_manager_user: manager user to use for authentication
    :param http_task_group: async task group for executing http requests
    :return: created product
    """
    product = TestDataGenerator.generate_basic_product()
    authentication_headers = await users_api.get_auth_header(session_manager_user)
    create_product_response = await products_api.create(product, headers=authentication_headers)
    assert create_product_response.ok, (
        "Failed to create session product.\n"
        f"Request status code: {create_product_response.status}\n"
        f"Response message: {await create_product_response.text()}"
    )

    create_product_response_json = await create_product_response.json()
    product.id = create_product_response_json["id"]

    yield product

    http_task_group.create_task(products_api.delete(product.id, headers=authentication_headers))

@pytest.fixture(scope="session")
async def session_get_product_response(
    products_api: ProductApi, users_api: UserApi, session_customer_user: User, session_product: Product
) -> ClientResponse:
    """Get session product with session customer user and return response

    :param products_api: initialized Product API
    :param users_api: initialized User API
    :param session_customer_user: customer user to use for authentication
    :param session_product: created product in the session scope
    :return: get product response
    """
    authentication_headers = await users_api.get_auth_header(session_customer_user)
    return await products_api.get(session_product.id, headers=authentication_headers)

@pytest.fixture(scope="session")
async def session_get_all_products_response(
    products_api: ProductApi, users_api: UserApi, session_customer_user: User
) -> ClientResponse:
    """Send request to get all products and return response

    :param products_api: initialized Product API
    :param users_api: initialized User API
    :param session_customer_user: customer user to use for authentication
    :return: get all products response
    """
    authentication_headers = await users_api.get_auth_header(session_customer_user)
    return await products_api.get_all(headers=authentication_headers)

@pytest.fixture(scope="session")
async def session_update_product_response(
    products_api: ProductApi, users_api: UserApi, session_manager_user: User, session_product: Product
) -> ClientResponse:
    """Send request to update given product and return response

    :param products_api: initialized Product API
    :param users_api: initialized User API
    :param session_manager_user: manager user to use for authentication
    :param session_product: created product to update
    :return: update product response
    """
    product_attributes_to_update = {"description": "new test book description"}
    authentication_headers = await users_api.get_auth_header(session_manager_user)
    return await products_api.update(session_product.id, product_attributes_to_update, headers=authentication_headers)

@pytest.fixture
async def created_product_response(
    products_api: ProductApi,
    users_api: UserApi,
    generated_product: Product,
    session_manager_user: User,
    http_task_group: TaskGroup,
) -> ClientResponse:
    """Create generated product and return response

    :param products_api: initialized Product API
    :param users_api: initialized User API
    :param generated_product: generated product
    :param session_manager_user: manager user model
    :param http_task_group: async task group for executing http requests
    :return: create product response
    """
    authentication_headers = await users_api.get_auth_header(session_manager_user)
    create_product_response = await products_api.create(generated_product, headers=authentication_headers)

    yield create_product_response

    if create_product_response.ok:
        create_product_response_json = await create_product_response.json()
        created_product_id = create_product_response_json["id"]
        http_task_group.create_task(products_api.delete(created_product_id, headers=authentication_headers))

@pytest.fixture
async def created_product(created_product_response: ClientResponse, generated_product: Product) -> Optional[Product]:
    """Create generated product and return generated product model

    :param created_product_response: response to create generated product
    :param generated_product: generated product
    :return: generated and created product model
    """
    if created_product_response.ok:
        created_product_response_json = await created_product_response.json()
        generated_product.id = created_product_response_json["id"]
        return generated_product
    return None

@pytest.fixture
async def deleted_product_response(
    products_api: ProductApi, users_api: UserApi, generated_product: Product, session_manager_user: User
) -> ClientResponse:
    """Delete generated and created product and return response

    :param products_api: initialized Product API
    :param users_api: initialized User API
    :param generated_product: generated product
    :param session_manager_user: manager user model
    :return: delete created product response
    """
    authentication_headers = await users_api.get_auth_header(session_manager_user)
    create_product_response = await products_api.create(generated_product, headers=authentication_headers)
    assert create_product_response.ok, (
        "Failed to create product to delete later.\n"
        f"Request status code: {create_product_response.status}\n"
        f"Response message: {await create_product_response.text()}"
    )

    create_product_response_json = await create_product_response.json()
    create_product_id = create_product_response_json["id"]
    return await products_api.delete(create_product_id, headers=authentication_headers)


#xgboost 与随机森林进行比较，采用10这交叉验证,
import xgboost as xgb
import numpy as np
from sklearn.model_selection import train_test_split,cross_val_score,cross_val_predict
from sklearn.datasets import fetch_openml
from sklearn.metrics import confusion_matrix,precision_recall_curve,roc_curve,mean_squared_error
from sklearn.metrics import precision_score, recall_score,accuracy_score,f1_score,roc_auc_score

mnist = fetch_openml('mnist_784', version=1, as_frame=False)
# print(mnist.keys())
X, y = mnist["data"], mnist["target"]
# print(type(X))
# print(X.shape)
# print(y.shape)
y = y.astype(np.uint8)

some_digit = X[0]
# X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42)
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]
y_train_5 = (y_train == 5)
y_test_5 = (y_test == 5)

train = xgb.DMatrix(X_train, label=y_train)

test = xgb.DMatrix(X_test, label=y_test)
params={
    'booster':'gbtree',
    'objective':'binary:logistic',
    'eval_metric':'auc',
    'learning_rate':0.1,
    'gamma':0.1,
    'subsample':0.8,
    'max_depth':5,
    'reg_alpha':50,
    'colsample_bytree':0.7,
    'min_child_weight':200,
    'seed':22
}

model_r = xgb.XGBRegressor(booster='gbtree',objective='binary:logistic',
    eval_metric='auc',
    learning_rate=0.1,
    gamma=0.1,
    subsample=0.8,
    max_depth=5,
    reg_alpha=50,
    colsample_bytree=0.7,
    min_child_weight=200,
    seed=22)

# model_r.fit(X_train, y_train)
#,callbacks=[xgb.callback.print_evaluation(show_stdv=True)]

print('正在跑 测试交叉验证 2')
y_train_score = cross_val_predict(model_r, X_train, y_train_5, cv=10)   #每个数据预测值
y_pred_train = (y_train_score >= 0.5)
print('训练集准确率:',accuracy_score(y_train_5, y_pred_train))
print('训练集精度:',precision_score(y_train_5, y_pred_train))
print('训练集召回率:',recall_score(y_train_5, y_pred_train))

print('混淆矩阵:',confusion_matrix(y_train_5, y_pred_train))
print('auc:',roc_auc_score(y_train_5, y_pred_train))


#!/data/data/com.termux/files/usr/bin/env python
# -*- coding: utf-8 -*-
from core.core import *
import requests
# Импорт модуля json Python
import json

cookies = {
    'S': 'ar7cN5HQykuyTdkNdUZwng',
    'Dark': 'true',
    'p': 'uYxK6VfvBeTsw3E2',
}

headers = {
    'User-Agent':
    'Mozilla/5.0 (X11; Ubuntu; Linux aarch64; rv:75.0) Gecko/20100101 Firefox/75.0',
    'Accept': '*/*',
    'Accept-Language': 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With': 'XMLHttpRequest',
    'Origin': 'https://nvspc.biz',
    'Connection': 'keep-alive',
    # Requests sorts cookies= alphabetically
    # 'Cookie': 'S=ar7cN5HQykuyTdkNdUZwng; Dark=true; p=uYxK6VfvBeTsw3E2',
    'TE': 'Trailers',
}

data = {
    'c': '4H5U2pnf2kOy7-fO-5Pnbw',
    'u': '1',
    'LNT': '2',
    'sortby': '0',
    'pad': '0',
    'count': '25',
}

r = requests.post('https://nvspc.biz/api/dummy/getShowcase',
                  cookies=cookies,
                  headers=headers,
                  data=data)

# Вызовите функцию dumps() и передайте список Python
json_str = json.dumps(r.json())

print(type(r.json()))
#  print(response.text)
print(type(json_str))