from async_scrape import Scrape
import requests
import json

from selenium.webdriver import Edge
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url = "https://order.marstons.co.uk/"
base_dir = "C:/Users/robert.franklin/Desktop/local_projects/random/marstons"

# GET ALL RESTAURANT DATA - selenium
browser = Edge()
browser.get(url)
wait = WebDriverWait(browser, 100).until(
    EC.presence_of_all_elements_located((By.CLASS_NAME, "venues-list"))
)
elements = browser.find_elements(By.CLASS_NAME, "venue-card")
hrefs = [e.get_dom_attribute("href") for e in elements]
browser.close()
print(f"Fetched {len(hrefs)} hrefs from {url}")

def post_process_func(html, resp, *args, **kwargs):
    # Save to file
    fn = resp.url.split("/")[-1]
    content = json.loads(resp.content)
    with open(f"{base_dir}/data/raw/{fn}.json", "w") as f:
        json.dump(content, f, indent=4)

base_url = "https://api-cdn.orderbee.co.uk/venues"
urls = [base_url + href for href in hrefs]
scrape = Scrape(post_process_func=post_process_func)
print(f"Begin scrape of {len(urls)} - Example: {urls[0]}")
scrape.scrape_all(urls)


from enum import IntEnum, auto
from typing import List, Mapping, Union, Tuple, Optional

from .aetg import AETGGenerator
from .matrix import MatrixGenerator
from ...model import int_enum_loads
from ...reflection import progressive_for

__all__ = ['tmatrix']

@int_enum_loads(enable_int=False, name_preprocess=str.upper)
class MatrixMode(IntEnum):
    AETG = auto()
    MATRIX = auto()

def tmatrix(ranges: Mapping[Union[str, Tuple[str, ...]], List],
            mode='aetg', seed: Optional[int] = 0, level: int = 2) -> Tuple[List[str], List[Tuple]]:
    """
    Overview:
        Test matrix generator, which can be used in ``pytest.mark.parameterize``.

    :param ranges: Ranges of the values
    :param mode: Mode of the matrix, should be one of the ``aetg`` or ``matrix``. Default is ``aetg``.
    :param seed: Random seed, default is ``0`` which means the result is fixed (recommended).
    :param level: Lavel of AETG generating algorithm, default is ``2``.
    :returns: A tuple - ``(names, values)``.

    Examples::
        >>> from hbutils.testing import tmatrix
        >>> names, values = tmatrix(
        ...     {
        ...         'a': [2, 3],
        ...         'e': ['a', 'b', 'c'],
        ...         ('b', 'c'): [(1, 7), (4, 6), (9, 12)],
        ...     }
        ... )
        >>> print(names)
        ['a', 'e', 'b', 'c']
        >>> for i, v in enumerate(values):
        ...     print(i, v)
        0 (2, 'c', 9, 12)
        1 (3, 'c', 4, 6)
        2 (2, 'c', 1, 7)
        3 (3, 'b', 9, 12)
        4 (2, 'b', 4, 6)
        5 (3, 'b', 1, 7)
        6 (3, 'a', 9, 12)
        7 (2, 'a', 4, 6)
        8 (3, 'a', 1, 7)

    .. note::
        This can be directly used in ``pytest.mark.parametrize`` function.

        >>> @pytest.mark.unittest
        ... class TestTestingGeneratorFunc:
        ...     @pytest.mark.parametrize(*tmatrix({
        ...         'a': [2, 3],
        ...         'e': ['a', 'b', 'c'],
        ...         ('b', 'c'): [(1, 7), (4, 6), (9, 12)],
        ...     }))
        ...     def test_tmatrix_usage(self, a, e, b, c):
        ...         print(a, e, b, c)
    """
    mode = MatrixMode.loads(mode)

    key_map = {}
    final_names = []
    final_values = {}
    for ki, (key, value) in enumerate(ranges.items()):
        kname = f'key-{ki}'
        key_map[kname] = key
        final_names.append(kname)
        final_values[kname] = value

    names = []
    for key in ranges.keys():
        if isinstance(key, str):
            names.append(key)
        elif isinstance(key, tuple):
            for k in key:
                names.append(k)

    if mode == MatrixMode.MATRIX:
        generator = MatrixGenerator(final_values, final_names)
    elif mode == MatrixMode.AETG:
        generator = AETGGenerator(
            final_values, final_names, rnd=seed,
            pairs=list(progressive_for(final_names, min(level, len(names)))),
        )
    else:
        raise ValueError(f'Invalid mode - {mode!r}.')  # pragma: no cover

    pairs = []
    for case in generator.cases():
        _v_case = {}
        for name in final_names:
            key = key_map[name]
            if isinstance(key, str):
                _v_case[key] = case[name]
            elif isinstance(key, tuple):
                for ikey, ivalue in zip(key, case[name]):
                    _v_case[ikey] = ivalue

        pairs.append(tuple(_v_case[name] for name in names))

    return names, pairs


import cgi
import sys
import io
import genshin.database.operation as gdo

form = cgi.FieldStorage()
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

template = """

    <meta charset="utf-8">

Deleting...

"""

def delete_character_data(name):
    gdo.delete_character(name)

def main():
    name = form.getvalue("del")
    delete_character_data(name)

    print("Content-type: text/html\n")
    print(template.format(name=name))

main()


# Plotting solution of x''(t) + x(t) = 0 equation
import numpy as np
import matplotlib.pyplot as plt
import os
from io import StringIO
import pandas as pd
from find_solution import find_solution
from plot_utils import create_dir

def plot_solution(plot_dir, t_end, delta_t):
    data = find_solution(t_end=t_end, delta_t=delta_t, print_last=False)

    create_dir(plot_dir)

    if data is None:
        return

    df = pd.read_csv(StringIO(data), skipinitialspace=True)
    plt.plot(df['t'], df['x'], label='Approximation')

    exact_t = np.arange(0.0, t_end, 0.01)
    exact_x = np.cos(exact_t)

    plt.plot(exact_t, exact_x, label='Exact $x=\cos(t)$', linestyle='--')
    plt.title(r'Solution of $\ddot{x} + x = 0, x(0)=1, \dot{x}(0)=0$ for dt=' + f'{delta_t}')
    plt.xlabel('t')
    plt.ylabel(r'x')
    plt.legend()
    plt.grid()
    plt.tight_layout()
    plotfile = os.path.join(plot_dir, f"approx_vs_exact_dt_{delta_t}.pdf")
    plt.savefig(plotfile)
    plt.show()

if __name__ == '__main__':
    plot_solution(plot_dir="plots", t_end=6.28, delta_t=1)
    plot_solution(plot_dir="plots", t_end=6.28, delta_t=0.1)


import redneuronal

import random
import time
import statistics

class RedNeuronalGA:
    def __init__(self, size, config:list, inputsize, mut_rate = 0.01, lastbest_rate = 0.5, tour_size = 10):
        '''

        :param size:
        :param n_genes:
        :param config: lista que indica [numero de layers, [numero de neuronas por layer]]
        :param mut_rate:
        :param lastbest_rate:
        :param tour_size:
        '''
        self.totalfitness = []
        self.inputsize = inputsize
        self.lastbest_rate = lastbest_rate
        self.pop_size = size
        self.mutation_rate = mut_rate
        self.tournament_size = tour_size
        self.current_generation = [] #lista de redes
        self.current_fitness = [] #valores de fitness encontrados despues de jugar
        self.final_ind = None
        self.config = config

    def set_tournamentsize(self, size):
        self.tournament_size = size

    def set_mutationrate(self, rate):
        self.mutation_rate = rate

    def set_survivalrate(self, rate):
        self.lastbest_rate = rate

    def initialize(self):
        for i in range(self.pop_size):
            red = []
            n_layers = self.config[0]
            for j in range(n_layers): #numero de layers
                layer = []
                n_neuronasj = self.config[1][j]
                for k in range(n_neuronasj):
                    neuron = []
                    if j == 0:
                        for p in range(self.inputsize):
                            neuron.append(random.random()*2)
                    else:
                        for p in range(self.config[1][j-1]):
                            neuron.append(random.random()*2)
                    neuron.append(0.8)
                    layer.append(neuron)
                red.append(layer)
            self.current_generation.append(redneuronal.RedN(red, i))

    def tournament_selection(self, population: list, k):
        ''' Randomly select the best individual after k iterations'''
        N = len(population)
        best = None
        for i in range(k):
            ind = population[random.randint(0, N - 1)]
            if best == None or self.fitness(ind) > self.fitness(best):
                best = ind
        return best

    def reproduce(self, red1:redneuronal.RedN, red2:redneuronal.RedN, index):
        nlayers = len(red1.red)
        new = []
        for i in range(nlayers):
            layer1 = red1.red[i]
            layer2 = red2.red[i]
            l = len(layer1)
            r = random.randint(1, l - 1)
            rep = layer1[0:r] + layer2[r:l]
            baby = []
            for i in range(l):
                if random.random() < self.mutation_rate:
                    baby.append(self.mutneuron(rep[i]))
                else:
                    baby.append(rep[i])
            new.append(baby)
        return redneuronal.RedN(new, index)

    def find(self):
        # best individuals of last generation
        best = []
        size = self.pop_size
        # selecciono a los posibles mejores
        while (len(best) < size*self.lastbest_rate):
            sel = self.tournament_selection(self.current_generation, self.tournament_size)
            if sel not in best:
                best.append(sel)
                self.current_generation.remove(sel)
        # crear nueva generacion a partir de los mejores anteriores
        gen = []
        count = 0
        while (len(gen) < size):
            ind1, ind2 = random.sample(best, 2)
            baby = self.reproduce(ind1, ind2,count)
            count+=1
            gen.append(baby)
        self.current_generation = gen
        self.savefitness()
        self.current_fitness = []

    def fitness(self, ind:redneuronal.RedN):
        return self.current_fitness[ind.index]

    def savefitness(self):
        self.totalfitness.append(statistics.mean(self.current_fitness))

    def mutneuron(self, neuron:list):
        print('mutante!')
        new = []
        for i in range(len(neuron)-1):
            if random.random()<0.5:
                new.append(random.random() * 2)
            else:
                new.append(neuron[i])
        new.append((neuron[-1]+random.random())%2)
        return new


#Library
import datetime
import time
from tkinter import *
import tkinter.ttk as ttk
from urllib.request import urlretrieve
import serial
import os
import RPi.GPIO as GPIO
#End Library

#Firmwares
ultra1 = serial.Serial("/dev/ttyUSB0",baudrate=9600, timeout=1)
gsm = serial.Serial("/dev/ttyAMA0",baudrate=9600, timeout=1)

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(31,GPIO.OUT) #Relay 1
GPIO.setup(33,GPIO.OUT) #Relay 2
GPIO.setup(35,GPIO.OUT) #Relay 3
GPIO.setup(37,GPIO.OUT) #Relay 4
GPIO.setup(12,GPIO.OUT) #Sensor1 Enable
#End Firmwares

root=Tk()
root.geometry("%dx%d+%d+%d"%(800,480,100,50)) #x,y,horizental,vertical
root.title('SAJAB')
root.configure(background='lightblue')

f1=open("relay1_source.txt","r")
f2=open("relay2_source.txt","r")
f3=open("relay3_source.txt","r")
f4=open("relay4_source.txt","r")

v1 = IntVar()
v1.set(int(f1.read()))  # initializing the choice, i.e. Python

v2 = IntVar()
v2.set(int(f2.read()))  # initializing the choice, i.e. Python

v3 = IntVar()
v3.set(int(f3.read()))  # initializing the choice, i.e. Python

v4 = IntVar()
v4.set(int(f4.read()))  # initializing the choice, i.e. Python

GPIO.output(31,v1.get())
GPIO.output(33,v2.get())
GPIO.output(35,v3.get())
GPIO.output(37,v4.get())

f1.close()
f2.close()
f3.close()
f4.close()

#Variables
station_name = "Golestan Uni"

pass_main='1120'
time_xloc= 20
time_yloc= 5

date_xloc=10
date_yloc=30

table_x=20
table_y=150

global default_sampling_rate
global default_bias_value
global default_coefficent
global default_max_level
global default_hysteresis_level
global default_mobile_phone1
global default_mobile_phone2
global default_mobile_phone3

step = 20
stepx = 40
global sampling_rate
sampling_rate=2
#End Variables

#Functions

def relay1():
    relay1_source = open("relay1_source.txt","w+")
    GPIO.setup(31,GPIO.OUT) #Relay 1
    if v1.get()==0:
        print("is off")
        GPIO.output(31,0)
        relay1_source.write("0")
    else:
        print("in on")
        GPIO.output(31,1)
        relay1_source.write("1")
    relay1_source.close()
def relay2():
    relay2_source = open("relay2_source.txt","w+")
    GPIO.setup(33,GPIO.OUT) #Relay 1
    if v2.get()==0:
        print("is off")
        GPIO.output(33,0)
        relay2_source.write("0")
    else:
        print("in on")
        GPIO.output(33,1)
        relay2_source.write("1")
    relay2_source.close()
def relay3():
    relay3_source = open("relay3_source.txt","w+")
    GPIO.setup(35,GPIO.OUT) #Relay 1
    if v3.get()==0:
        print("is off")
        GPIO.output(35,0)
        relay3_source.write("0")
    else:
        print("in on")
        GPIO.output(35,1)
        relay3_source.write("1")
    relay3_source.close()
def relay4():
    relay4_source = open("relay4_source.txt","w+")
    GPIO.setup(37,GPIO.OUT) #Relay 1
    if v4.get()==0:
        print("is off")
        GPIO.output(37,0)
        relay4_source.write("0")
    else:
        print("in on")
        GPIO.output(37,1)
        relay4_source.write("1")
    relay4_source.close()

#Send Data
def send_data():
    gsm.write("AT\r\n".encode('ascii'))
    rcv = gsm.read(10)
    print (rcv)
    gsm.write("AT+CSQ\r\n".encode('ascii'))
    time.sleep(1)
    gsm.write("AT+CGATT?\r\n".encode('ascii'))
    time.sleep(1)
    gsm.write("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n".encode('ascii'))
    time.sleep(1)
    gsm.write("AT+SAPBR=3,1,\"APN\",\"mtnirancell\"\r\n".encode('ascii'))
    time.sleep(4)
    gsm.write("AT+SAPBR=1,1\r\n".encode('ascii'))
    time.sleep(2)
    gsm.write("AT+HTTPINIT\r\n".encode('ascii'))
    time.sleep(2)
    data_link = "AT+HTTPPARA=\"URL\",\"http://sajab.sazabgolestan.com/server.php?action=save&station_index=3&ha=%d&hb=3&imei=9359374362\"\r\n" %(sensor1_read(sen1))
    gsm.write(data_link.encode('ascii'))
    time.sleep(1)
    gsm.write("AT+HTTPACTION=0\r\n".encode('ascii'))
    time.sleep(10)
#End Send Data
#Sensor Read
def sensor1_read(sen):
    def count():
        GPIO.output(12,0)
        global u1
        u1 = ultra1.read(12)
        u1 = str(u1)
        loc = u1.find('R')
        u1 = u1[loc+1:loc+6]
        GPIO.output(12,1)
        sen.config(text=str(u1))
        sen.after(sampling_rate*500,count)
    count()
    return int(u1)
#End Sensor read

#Samle Rate read
#def sr_read(sr_label):
#    def count5():
#        sr_label.config(text=str(sampling_rate))
#        sr_label.after(sampling_rate*500,count5)
#    count5()
#End Samle Rate read

def pass_check():
    global passcheck
    passcheck = Toplevel()
    passcheck.geometry("%dx%d+%d+%d"%(200,140,100,50)) #x,y,horizental,vertical
    passcheck.title('Setting')
    passcheck.configure(background='lightblue')
    Label(passcheck,text="Enter Password:",fg=top_bg_color,bg=color,width=0).place(x=40,y=20)
    global pass_in
    pass_in=Entry(passcheck,width=18)
    pass_in.place(x=25,y=50)

    Button(passcheck,text="OK",command=pass_check2).place(x=80,y=80)

def pass_check2():
    if pass_in.get()==pass_main:
        passcheck.destroy()
        setting()
    else:
        Label(passcheck,text="Password is wrong!",fg=top_bg_color,bg=color,width=0).place(x=40,y=110)

def setting():
    global setting_frame
    setting_frame = Toplevel()
    setting_frame.geometry("%dx%d+%d+%d"%(700,420,100,50)) #x,y,horizental,vertical
    setting_frame.title('Setting')
    setting_frame.configure(background='lightblue')
    Label(setting_frame,text="Station name:",fg=top_bg_color,bg=color,width=0).grid(row=0,column=0,ipadx=30,pady=8)
    Label(setting_frame,text=station_name,fg=top_bg_color,bg=color,width=0).grid(row=0,column=1,ipadx=30,pady=8)
    Label(setting_frame,text="Sampling rate:",fg=top_bg_color,bg=color,width=0).grid(row=1,column=0,ipadx=30,pady=8)
    global samp_rate
    samp_rate=Entry(setting_frame,width=8)
    samp_rate.place(x=140,y=45)
    samp_rate.insert(10,str(sampling_rate))
    Label(setting_frame,text="Sec.",fg=top_bg_color,bg=color,width=0).place(x=220,y=46)
    Label(setting_frame,text="Calibration:",fg=top_bg_color,bg=color,width=0).grid(row=2,column=0,ipadx=30,pady=8)
    Label(setting_frame,text="Bias Value:",fg=top_bg_color,bg=color,width=0).grid(row=3,column=0,ipadx=30,pady=8)
    global bs_value
    bs_value=Entry(setting_frame,width=8)
    bs_value.place(x=140,y=118)
    bs_value.insert(10,str(default_bias_value))
    Label(setting_frame,text="Coefficent:",fg=top_bg_color,bg=color,width=0).grid(row=4,column=0,ipadx=30,pady=8)
    global coef
    coef=Entry(setting_frame,width=8)
    coef.place(x=140,y=154)
    coef.insert(10,str(default_coefficent))
    Label(setting_frame,text="Alert:",fg=top_bg_color,bg=color,width=0).grid(row=0,column=3,ipadx=30,pady=8)
    Label(setting_frame,text="Max level:",fg=top_bg_color,bg=color,width=0).grid(row=1,column=3,ipadx=30,pady=8)
    global mx_level
    mx_level=Entry(setting_frame,width=8)
    mx_level.place(x=465,y=45)
    mx_level.insert(10,str(default_max_level))
    Label(setting_frame,text="m.m.",fg=top_bg_color,bg=color,width=0).place(x=545,y=46)
    Label(setting_frame,text="Hysteresis level:",fg=top_bg_color,bg=color,width=0).grid(row=2,column=3,ipadx=30,pady=8)
    global hys_level
    hys_level=Entry(setting_frame,width=8)
    hys_level.place(x=465,y=81)
    hys_level.insert(10,str(default_hysteresis_level))
    Label(setting_frame,text="m.m.",fg=top_bg_color,bg=color,width=0).place(x=545,y=82)
    Label(setting_frame,text="Mobile Phone 1:",fg=top_bg_color,bg=color,width=0).grid(row=3,column=3,ipadx=30,pady=8)
    global mob_phone1
    mob_phone1=Entry(setting_frame,width=15)
    mob_phone1.place(x=465,y=117)
    mob_phone1.insert(10,str(default_mobile_phone1))
    Label(setting_frame,text="Mobile Phone 2:",fg=top_bg_color,bg=color,width=0).grid(row=4,column=3,ipadx=30,pady=8)
    global mob_phone2
    mob_phone2=Entry(setting_frame,width=15)
    mob_phone2.place(x=465,y=153)
    mob_phone2.insert(10,str(default_mobile_phone2))
    Label(setting_frame,text="Mobile Phone 3:",fg=top_bg_color,bg=color,width=0).grid(row=5,column=3,ipadx=30,pady=8)
    global mob_phone3
    mob_phone3=Entry(setting_frame,width=15)
    mob_phone3.place(x=465,y=189)
    mob_phone3.insert(10,str(default_mobile_phone3))
    Button(setting_frame,text="OK",command=ok).place(x=30,y=220)
    Button(setting_frame,text="Set as default",command=setasdefault).place(x=120,y=220)
    Button(setting_frame,text="Default values",command=defaultvals).place(x=300,y=220)
    Button(setting_frame,text="Cancel",command=cncl).place(x=500,y=220)

def ok():
    global sampling_rate
    sampling_rate = int(samp_rate.get())
    print(int(samp_rate.get()))
    print(sampling_rate)
    bias_value = int(bs_value.get())
    coefficent = int(coef.get())
    max_level = int(mx_level.get())
    hysteresis_value = int(hys_level.get())
    mobile_phone1 = mob_phone1.get()
    mobile_phone2 = mob_phone2.get()
    mobile_phone3 = mob_phone3.get()

    conf_str = "http://sajab.sazabgolestan.com/server.php?action=station&imei=9359374362&station_index=3&status=1&sampling_rate=%d&bias_value=%d&coefficent=%d&max_level=%d&hysteresis_value=%d&mobile_phone1=%s&mobile_phone2=%s&mobile_phone3=%s" %(sampling_rate,bias_value,coefficent,max_level,hysteresis_value,mobile_phone1,mobile_phone2,mobile_phone3)

    conf_file = open("conf.txt","w+")
    conf_file.write(conf_str)
    conf_file.close()

    setting_frame.destroy()
def cncl():
    setting_frame.destroy()

def defaultvals():

    samp_rate.delete(0,END)
    samp_rate.insert(10,str(default_sampling_rate))
    bs_value.delete(0,END)
    bs_value.insert(10,str(default_bias_value))
    coef.delete(0,END)
    coef.insert(10,str(default_coefficent))
    mx_level.delete(0,END)
    mx_level.insert(10,str(default_max_level))
    hys_level.delete(0,END)
    hys_level.insert(10,str(default_hysteresis_level))
    mob_phone1.delete(0,END)
    mob_phone1.insert(10,str(default_mobile_phone1))
    mob_phone2.delete(0,END)
    mob_phone2.insert(10,str(default_mobile_phone2))
    mob_phone3.delete(0,END)
    mob_phone3.insert(10,str(default_mobile_phone3))

def setasdefault():
    default_conf_str = "http://sajab.sazabgolestan.com/server.php?action=station&imei=9359374362&station_index=3&status=1&sampling_rate=%d&bias_value=%d&coefficent=%d&max_level=%d&hysteresis_value=%d&mobile_phone1=%s&mobile_phone2=%s&mobile_phone3=%s" %(default_sampling_rate,default_bias_value,default_coefficent,default_max_level,default_hysteresis_value,default_mobile_phone1,default_mobile_phone2,default_mobile_phone3)

    default_conf_file = open("default_conf.txt","w+")
    default_conf_file.write(default_conf_str)
    default_conf_file.close()

#Functions for splitting the different components of date and time
def nowYear():
    now = datetime.datetime.now()
    year = now.year
    return str(year)

def nowMonth():
    now = datetime.datetime.now()
    month = now.month
    return str(month)

def nowDay():
    now = datetime.datetime.now()
    day = now.day
    return str(day)

def nowHour():
    now = datetime.datetime.now()
    hour = now.hour
    return str(hour)

def nowMinute():
    now = datetime.datetime.now()
    minute = now.minute
    return str(minute)

def nowSecond():
    now = datetime.datetime.now()
    second = now.second
    return str(second)

def year_label(label):
  def count1():
    label.config(text=nowYear())
    label.after(1000, count1)
  count1()

def month_label(label):
  def count2():
    label.config(text=nowMonth())
    label.after(1000, count2)
  count2()

def day_label(label):
  def count3():
    label.config(text=nowDay())
    label.after(1000, count3)
  count3()

def hour_label(label):
  def count4():
    label.config(text=nowHour())
    label.after(1000, count4)
  count4()

def minute_label(label):
  def count5():
    label.config(text=nowMinute())
    label.after(1000, count5)
  count5()

def second_label(label):
  def count6():
    label.config(text=nowSecond())
    label.after(1000, count6)
  count6()

def about():
   filewin = Toplevel(root)
   tx ="""
    Development by: Sina Meshkini
    +98 911 380 6028
    SinaMeshkini7@gmail.com
    @SinaMeshkini
    """
   message = Message(filewin, text=tx, relief = RIDGE , width = 400)
   message.pack(fill="both", expand="yes")
#End Functions

#Desigen Param

color = 'lightblue'
top_fg_color = 'lightblue'
top_bg_color = '#111131'
#End Desigen Param

#Header
w = Canvas(root,width= 800,height= 100)
w.pack()
w.create_rectangle(0,0,800,100,fill=top_bg_color)

Label(root,text='SAJAB Management System',fg=top_fg_color,bg=top_bg_color,font="tahoma 24 bold",pady=10).place(x=150,y=5)

#Time
hourLabel = Label(root,text=nowHour(),fg=top_fg_color,bg=top_bg_color,font=("Ravie", 10))
hourLabel.place(x=time_xloc,y=time_yloc)
hour_label(hourLabel)

colon = Label(root, text = ":",fg=top_fg_color,bg=top_bg_color,font=("Ravie",14))
colon.place(x=time_xloc+step,y=time_yloc-5)

minuteLabel = Label(root, text = nowMinute(),fg=top_fg_color,bg=top_bg_color,font=("Ravie",10))
minuteLabel.place(x=time_xloc+2*step,y=time_yloc)
minute_label(minuteLabel)

colon = Label(root, text = ":",fg=top_fg_color,bg=top_bg_color,font=("Ravie",14))
colon.place(x=time_xloc+3*step,y=time_yloc-5)

secondLabel = Label(root, text = nowSecond(),fg=top_fg_color,bg=top_bg_color,font=("Ravie",10))
secondLabel.place(x=time_xloc+4*step,y=time_yloc)
second_label(secondLabel)
#End Time

#Date
yearLabel = Label(root,text=nowYear(),fg=top_fg_color,bg=top_bg_color,font=("Ravie", 10))
yearLabel.place(x=date_xloc,y=date_yloc)
year_label(yearLabel)

colon = Label(root, text = "/",fg=top_fg_color,bg=top_bg_color)
colon.place(x=date_xloc+36,y=date_yloc)

monthLabel = Label(root,text=nowMonth(),fg=top_fg_color,bg=top_bg_color,font=("Ravie", 10))
monthLabel.place(x=date_xloc+45,y=date_yloc)
month_label(monthLabel)

colon = Label(root, text = "/",fg=top_fg_color,bg=top_bg_color)
colon.place(x=date_xloc+60,y=date_yloc)

dayLabel = Label(root,text=nowDay(),fg=top_fg_color,bg=top_bg_color,font=("Ravie", 10))
dayLabel.place(x=date_xloc+68,y=date_yloc)
day_label(dayLabel)
#End Date

#Temp
temp_label = Label(root,text="Temp: ",fg=top_fg_color,bg=top_bg_color,font=("Ravie", 10))
temp_label.place(x=date_xloc,y=date_yloc+20)
#End Temp
#End Header

#Body
sensors = ['Sensor 1:','Sensor 2:','Sensor 3:','Sensor 4:']
relays = ['Relay 1','Relay 2','Relay 3','Relay 4']
r=0
for c in sensors:
    Label(root,text=c,fg=top_bg_color,bg=color,width=0).place(x=table_x,y=table_y+r*stepx)
    r=r+1
r=0
for c in relays:
    Label(root,text=c,fg=top_bg_color,bg=color,width=0).place(x=table_x+300,y=table_y+r*stepx)
    r=r+1
#Sensors Display
global sen1
sen1 = Label(root,fg=top_bg_color,bg=color)
sen1.place(x= table_x+130,y=table_y)
sensor1_read(sen1)
#End Sensors Display

#Relay control
Radiobutton(root,text="OFF",variable=v1,command=relay1,value=1).place(x= table_x+400,y=table_y)
Radiobutton(root,text="ON",variable=v1,command=relay1,value=0).place(x= table_x+500,y=table_y)

Radiobutton(root,text="OFF",variable=v2,command=relay2,value=1).place(x= table_x+400,y=table_y+stepx)
Radiobutton(root,text="ON",variable=v2,command=relay2,value=0).place(x= table_x+500,y=table_y+stepx)

Radiobutton(root,text="OFF",variable=v3,command=relay3,value=1).place(x= table_x+400,y=table_y+2*stepx)
Radiobutton(root,text="ON",variable=v3,command=relay3,value=0).place(x= table_x+500,y=table_y+2*stepx)

Radiobutton(root,text="OFF",variable=v4,command=relay4,value=1).place(x= table_x+400,y=table_y+3*stepx)
Radiobutton(root,text="ON",variable=v4,command=relay4,value=0).place(x= table_x+500,y=table_y+3*stepx)

#End Relay control

Label(root,text="Sampling rate:",fg=top_bg_color,bg=color,width=0).place(x=table_x+50,y=350)
global sr_label
sr_label = Label(root,fg=top_bg_color,bg=color)
sr_label.place(x=table_x+150,y=350)
#sr_read(sr_label)
Label(root,text="Sec.",fg=top_bg_color,bg=color,width=0).place(x=table_x+230,y=351)

Button(root,text="Setting",command=pass_check).place(x=700,y=410)
Button(root,text="Send Data",command=send_data).place(x=200,y=410)

#End Body

root.mainloop()


from typing import cast, Any

from aea.skills.behaviours import TickerBehaviour
from aea.helpers.search.models import Constraint, ConstraintType, Query

from packages.fetchai.protocols.oef_search.message import OefSearchMessage
from packages.fetchai.skills.tac_control.dialogues import (
    OefSearchDialogues,
)

from packages.gdp8.skills.agent_action_each_turn.strategy import BasicStrategy

DEFAULT_REGISTER_AND_SEARCH_INTERVAL = 5.0
environmentFound = False

DEFAULT_SEARCH_QUERY = {
    "search_key": "env",## is that the key of the environment ?
    "search_value": "v1",
    "constraint_type": "==",
}

class EnvSearchBehaviour(TickerBehaviour):
    """This class scaffolds a behaviour."""

    def setup(self) -> None:
        """
        Implement the setup.
        :return: None
        """

    def act(self) -> None:
        """
        Implement the act.
        :return: None
        """
        if not environmentFound:
            self._search_for_environment()

    def teardown(self) -> None:
        """
        Implement the task teardown.
        :return: None
        """

    def _search_for_environment(self) -> None:
        """
        Search for active environment (simulation controller).
        We assume that the environment is registered as a service
        (and with an attribute version = expected_version_id ## ??? do we really need to have that attribute ?)
        :return: None
        """
        ## can add a filter: close to my service if there are too many results
        service_key_filter = Constraint(
           DEFAULT_SEARCH_QUERY["search_key"],
            ConstraintType(
                DEFAULT_SEARCH_QUERY["constraint_type"],
                DEFAULT_SEARCH_QUERY["search_value"],
            ),
        )
        query = Query([service_key_filter],)
        oef_search_dialogues = cast(
            OefSearchDialogues, self.context.oef_search_dialogues
        )
        oef_search_msg, _ = oef_search_dialogues.create(
            counterparty=self.context.search_service_address,
            performative=OefSearchMessage.Performative.SEARCH_SERVICES,
            query=query,
        )
        self.context.outbox.put_message(message=oef_search_msg)
        self.context.logger.info(
            "searching for environment, search_id={}".format(oef_search_msg.dialogue_reference)
        )

class AgentLogicBehaviour(TickerBehaviour):
    """Behaviour looks at if actions required in each tick:
       is there agent asking for water info? if so, tell them
       is the round done (on my end)? if so, stop
       is there enough info for making a decision? if so, do so,
       if not, might have to send message to ask for info"""

    def setup(self) -> None:
        """
        Implement the setup.

        :return: None
        """
        pass

    def act(self) -> None:

        strategy = cast(BasicStrategy, self.context.strategy)

        there_is_agent_asking_for_water_info = True
        while there_is_agent_asking_for_water_info:
            there_is_agent_asking_for_water_info = strategy.deal_with_an_agent_asking_for_water_info

        if not strategy.is_round_done:
            info_is_enough = strategy.enough_info_to_make_decision
            if info_is_enough:
                strategy.make_decision_send_to_env()
            else:
                asking_for_info = True
                while asking_for_info:
                    asking_for_info = strategy.potentially_ask_for_info

    def teardown(self) -> None:
        """
        Implement the task teardown.

        :return: None
        """
        pass


import numpy as np
import matplotlib.pyplot as plt

# seulement deux etats

def epidemie_1(temps = 50, population = 10**6):
    propagation = np.array( [[0.9 , 0.1], [0.3, 0.7]]) # 0 -> infecte, 1 -> sain
    popu = np.array([0, 1])
    X_temps = np.linspace(0, temps, temps)
    Y_infectes = []
    for t in range(temps):
        Y_infectes.append(popu[0]*population)
        popu = np.dot(popu, propagation)
    plt.plot(X_temps, Y_infectes)

# illustration de markov deux etats
# modele irrealiste a cause de la pente, voir site worldometers.info juste pour donner une impression de ce que ca devrait donner pas pour donner

#epidemie_1()
#plt.show()

# 5 etats cette fois:
def epidemie_2(temps = 100, population = 10**6):
    propagation = np.array( [
        [0.7, 0.2, 0, 0, 0.0001, 0.0999], # 0 -> infecte vaccine
        [0.2, 0.8, 0, 0, 0, 0], # 1 -> sain vaccine
        [0 , 0.2, 0.1, 0.7, 0, 0], # 2 -> sain non vaccin
        [0,0.2, 0, 0.7, 0.001, 0.099], # 3 -> infecte non vaccine
        [0, 0, 0, 0, 1, 0 ], # 4 -> mort
        [0, 0, 0, 0, 0, 1 ] # 5 -> immunise
    ])
    popu = np.array([0, 0, 1, 0, 0, 0])
    X_temps = np.linspace(0, temps, temps)
    Y_infectes = []
    for t in range(temps):
        Y_infectes.append(popu[0]*population)
        popu = np.dot(popu, propagation)
    plt.plot(X_temps, Y_infectes)

#epidemie_2()
#plt.show()

# modele bcp plus complique car bcp d'etats mais
# resultats bien plus satisfaisant
# on peut jouer sur la propagation de l'epidemie
# qui prennent en compte la reaction des gens, des gouvernements, le confinement ( notamment probabilite d'infection qui descend, celle de vaccination monte, mais aussi mutation aleatoire)

# Chaine de Markov cachee
# changement de domaine

def max_arg(liste):
    """ renvoie le max de la liste et le plus petit indice ou il a ete realise"""
    m = liste[0]
    i_max = 0
    for n in range(len(liste)):
        if liste[n] > m:
            m = liste[n]
            i_max = n
    return m, i_max

def Viterbi(A, B, Obs):
    # A matrice de transition
    # B les probabilites d'observation tq b_j(o_t) = B[j][t]
    # On travaille avec des logarithmes
    logA = np.log(A)
    logB = np.log(B)
    N = len(A)
    T = len(Obs)
    pointeurs =  np.reshape(np.zeros(T*N), (N,T)) # sert a retracer le chemin a la fin
    alpha_prec =  np.array(B[:][Obs[0]])
    alpha_suiv = np.zeros(N)
    for t in range(T):
        nouv_alpha = np.zeros(N)
        for j in range(N):
            nouv_alpha[j], pointeurs[j][t] = max_arg( np.array( np.log(alpha_suiv[i]) + logA[i][j] + logB[j][Obs[t]] for i in range(N)))
            # log est croissante, conserve donc le max
            # on met en pointeur l'etat i qui realise le maximum : c'etait l'etat precedent
        alpha_prec = alpha_suiv[:]
        alpha_suiv = nouv_alpha[:]

    pmax, i_final = max_arg(alpha_suiv)
    pmax = np.exp(pmax)
    etats_successifs = np.zeros(T)

    i = i_final
    for t in range(1, T+1, -1):
        etats_successifs[t] = i
        i = pointeurs[i][t-1]
    return pmax, etats_successifs

def forward(A, B, Obs):
    N = len(A)
    T = len(B)
    alpha_prec = np.array(B[:][Obs[0]])
    alpha_suiv = np.zeros(N)
    for t in range(T):
        nouv_alpha = np.zeros(N)
        for j in range(N):
            nouv_alpha[j] = B[j][Obs[t]] * sum( alpha_suiv[i] * A[i][j] for i in range(N))
        alpha_prec = alpha_suiv[:]
        alpha_suiv = nouv_alpha[:]
    return sum(alpha_suiv)

# def baum-welch(A,B, Obs):
#     if # condition de convergence

#     else:
#         N = len(A)
#         T = len(Obs)
#         alphas = np.reshape(np.zeros(N*T), (N, T))
#         betas = np.reshape(np.zeros(N*T), (N, T))
#         # a initialiser correctement avec un for ICI
#         C = sum(alphas)
#         D = sum(beta)
#         alphas = alphas/C
#         beta = beta/D
#         for t in range(1, T): # on construit
#             for i in range(N):
#                 alphas[i][t] = B[i][Obs[t]] *
#                 betas[i][t] =

# ABANDON MOMOENTANE

def baum_welch_naif(A, B, Obs):
    N = len(A)
    T = len(Obs)
    alphas = np.reshape(np.zeros(N*T), (T, N))
    betas = np.reshape(np.zeros(N*T), (T, N))
    # trouver toutes les valeurs des alphas et betas
    alphas[:][0] = B[:][Obs[0]]
    betas[T-1][:] = np.ones(N)

    for t in range(1, T-2):
        for j in range(N):
            alphas[t][j] =  B[j][Obs[t]] * sum( alphas[t-1][i] * A[i][j]  for i in range(N))
            betas[T-1-t][j] = B[Obs[T-t]][j] * sum( betas[T-t][i] * A[j][i] for i in range(N))

    Pobs = sum(alphas[T-1][:])
    # step Expectations
    zeta = np.reshape(np.zeros(N*N*T), (T,N, N))
    gamma = np.reshape(np.zeros(N*T), (T,N))

    for t in range(T-1):
        for i in range(N):
            for j in range(N):
                zeta[t][i][j] = alphas[t][i] * betas[t+1][j] * A[i][j] * B[Obs[t]][j] / Pobs
    for t in range(T):
        for i in range(N):
            gamma[t][i] = (alphas[t][j] * betas[t][j]) / Pobs

    #step S

    nouvA = np.reshape(np.zeros(N**2), (N,N))
    nouvB = np.reshape(np.zeros(N * len(B[0])), (N, len(B[0])))

    for i in range(N):
        denom = sum( sum( zeta[t][i][k] for k in range(N)) for t in range(T))
        for j in range(N):
            nouvA[i][j] = sum( zeta[t][i][j] for t in range(T)) / denom

    for j in range(N):
        for k in range(len(B)):
            denom = sum(gamma[t][j] for t in range(T))
            for t in range(T):
                if Obs[t] == k:
                    nouvB[j][k] = nouvB[j][k] + gamma[t][j] / denom

    return nouvA, nouvB

def traite_fichier_adn():
    nucleotide = open("adn_pur.txt", "r")
    nombres = open("adn_traite", "a")
    lignes = nucleotide.readlines()
    N = ['a', 'c', 't', 'g']

    for l in lignes:
        for carac in l:
            if carac == 'a':
                nombres.write("0 ")
            if carac == 'c':
                nombres.write("1 ")
            if carac == 't':
                nombres.write("2 ")
            if carac == 'g':
                nombres.write("3 ")
    nucleotide.close()
    nombres.close()

adn = open("adn_traite", "r")
sequence = adn.readlines()
Ob = []
for ligne in sequence:
    for nclt in ligne:
        if nclt in ['0', '1', '2', '3']:
            Ob.append(int(nclt))
adn.close()

def sequencageADN(Obs):
    precision = 0.1
    A =  0.25 * np.reshape(np.ones(16), (4, 4))
    B =  0.25 * np.reshape(np.ones(16), (4, 4))

    Ap, Bp = baum_welch_naif(A, B, Obs)
    while np.linalg.norm(A - Ap) < precision or np.linalg.norm(B - Bp)<precision:
        A = Ap
        B = Bp
        Ap, Bp = baum_welch_naif(A, B, Obs)

    return A, B

#print(sequencageADN(Ob))

def ruine_du_joueur(N, p, T = 100):
    X_t = np.zeros(2*N+1)
    X_t[N] = 1.0
    T = list(range(1, T))
    A = np.reshape(np.zeros((2*N+1)**2), ((2*N+1),(2*N+1)))
    A[0][0] = 1
    A[-1][-1] = 1

    for i in range(1, 2*N):
        A[i][i-1] = 1-p
        A[i][i+1] = p

    print(A)
    Argent = []
    for t in T:
        m = max(X_t)
        for k in range(2*N+1):
            if X_t[k] == m:
                Argent.append(k)
                break
        X_t = np.dot(X_t, A)
    plt.plot(T, Argent)

import random as rd
def vol_du_joueur(N, p):
    for _ in range(3):
        X = [0]
        Y = [N]
        temps = 0
        A = N
        while A > 0 and A < 2*N:
            temps += 1
            if rd.random()< p:
                A += 1
            else:
                A -= 1
            X.append(temps)
            Y.append(A)
        plt.plot(X, Y)

    plt.xlabel("temps")
    plt.ylabel("Pieces")
    plt.show()

#vol_du_joueur(20, 0.5)

def temps_de_vol(N,p):
    Y = []
    nb_essais = 100000
    for k in range(nb_essais):
        temps = 0
        A = N
        while A > 0 and A < 2*N:
            temps += 1
            if rd.random()< p:
                A += 1
            else:
                A -= 1
        Y.append(temps)
    Yp = [0]*(max(Y)+1)
    for y in Y:
        Yp[y] += 1
    plt.bar(list(range(max(Y)+1)), Yp, width=1.0, edgecolor = "#981FFA")
    plt.show()

#temps_de_vol(200, 0.7)

import cmath

def mouvement_brownien(N):
    position = 0 + 0j
    X = [position]
    t = 1
    i = 1j
    direction = 1
    while t < N:
        dir = rd.random()
        dist = rd.random()

        if dir < 0.05 or  0.50> dir > 0.45:
            if dist <0.01:
                direction *= cmath.exp(dir*2*np.pi*1j)
                position = position + dist * direction

                X.append(position)
                t += 1
    plt.plot( [ z.real for z in X], [z.imag for z in X])
    plt.show()

mouvement_brownien(10000)

def baum_welch(A, B, Obs):
    N = len(A)
    T = len(Obs)
    alphas = np.reshape(np.zeros(N*T), (T, N))
    betas = np.reshape(np.zeros(N*T), (T, N))
    # trouver toutes les valeurs des alphas et betas
    alphas[:][0] = B[:][Obs[0]]
    betas[T-1][:] = np.ones(N)
    for t in range(1, T-2):
        for j in range(N):
            alphas[t][j] =  B[j][Obs[t]] * sum( alphas[t-1][i] * A[i][j]  for i in range(N))
            betas[T-1-t][j] = B[Obs[T-t]][j] * sum( betas[T-t][i] * A[j][i] for i in range(N))
    constantesC = []
    for t in range(T):
        C = sum(alphas[t][:])**(-1)
        constantes.append(C)
        alphas[t][:] = alphas[t][:] / C
    constancesc = []
    for t in range(T):
        c = 0
        for y in range(N):
            c += np.dot(alphas[t][:], A[y]) * B[t][y]
        c = 1 / c
        constantesc.append(c)
    Pobs = sum(alphas[T-1][:])
    # step Expectations
    zeta = np.reshape(np.zeros(N*N*T), (T,N, N))
    gamma = np.reshape(np.zeros(N*T), (T,N))

    for t in range(T-1):
        for i in range(N):
            for j in range(N):
                zeta[t][i][j] = alphas[t][i] * betas[t+1][j] * A[i][j] * B[Obs[t]][j] / Pobs
    for t in range(T):
        for i in range(N):
            gamma[t][i] = (alphas[t][j] * betas[t][j]) / Pobs
    #step S
    nouvA = np.reshape(np.zeros(N**2), (N,N))
    nouvB = np.reshape(np.zeros(N * len(B[0])), (N, len(B[0])))
    for i in range(N):
        denom = sum( sum( zeta[t][i][k] for k in range(N)) for t in range(T))
        for j in range(N):
            nouvA[i][j] = sum( zeta[t][i][j] for t in range(T)) / denom
    for j in range(N):
        for k in range(len(B)):
            denom = sum(gamma[t][j] for t in range(T))
            for t in range(T):
                if Obs[t] == k:
                    nouvB[j][k] = nouvB[j][k] + gamma[t][j] / denom

    return nouvA, nouvB


import torch
from torch import Tensor
from kornia.utils import one_hot
import torch.nn.functional as F
import numpy as np
from matplotlib import pyplot as plt

def reg_loss(prediction, ED, ES, device):
    # print(prediction)
    prediction_toSyn = prediction.squeeze().detach().cpu().numpy()
    y_k = synthetic_label(prediction_toSyn, ED.numpy(), ES.numpy())

    # print(prediction.squeeze())
    # print(y_k)
    # print(ED)
    # print(ES)
    # print('-----')
    mse_loss = F.mse_loss(prediction.squeeze(), y_k.to(device))
    temp_loss = ltemp(y_k, prediction_toSyn)

    loss = mse_loss + temp_loss

    return loss

def synthetic_label(prediction, ED, ES):
    y_k = []
    for k in range(len(prediction)):
        if (int(ED) < k) and (k <= int(ES)):
            y_k.append((abs((k-ES)/(ES-ED)))**3)
            # print(1)
        else:
            y_k.append((abs((k-ES)/(ES-ED)))**(1/3))
    # print(y_k)
    # plt.plot(y_k)
    # plt.savefig('y_k.png')
    return torch.from_numpy(np.array(y_k, dtype= "float32"))

def ltemp(y_k, prediction):
    Linc = linc(y_k, prediction)
    Ldec = ldec(y_k, prediction)
    ltemp = (Linc+Ldec)/2
    # print(ltemp)
    return torch.from_numpy(np.array(ltemp, dtype= "float32"))

def linc(y_k, prediction):
    Linc = 0
    for k in range(len(prediction)-1):
        if y_k[k+1] > y_k[k]:
            Linc = Linc + max(0,prediction[k]-prediction[k+1])
            # print('linc')
    return Linc/len(prediction)

def ldec(y_k, prediction):
    Ldec = 0
    for k in range(len(prediction)-1):
        if y_k[k+1] < y_k[k]:
            Ldec = Ldec + max(0,prediction[k+1]-prediction[k])
            # print('ldec')
    return Ldec/len(prediction)


# Code courtesy: https://towardsdatascience.com/support-vector-machine-python-example-d67d9b63f1c8
# Theory: https://www.youtube.com/watch?v=_PwhiWxHK8o
import numpy as np
import cvxopt
from sklearn.datasets.samples_generator import make_blobs
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt
from sklearn.svm import LinearSVC
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score

class SVM:
    """

    """
    def __init__(self):
        self.alpha = None
        self.w = None
        self.b = None
        self.support_vectors = None
        self.support_vector_y = None

    def fit(self, X_train, y_train):
        n_samples, n_features = X_train.shape
        # P = X_train^T X_train
        K = np.zeros((n_samples, n_samples))
        for i in range(n_samples):
            for j in range(n_samples):
                K[i, j] = np.dot(X_train[i], X_train[j])

        P = cvxopt.matrix(np.outer(y_train, y_train) * K)
        # q = -1 (1xN)
        q = cvxopt.matrix(np.ones(n_samples) * -1)
        # A = y_train^T
        A = cvxopt.matrix(y_train, (1, n_samples))
        # b = 0
        b = cvxopt.matrix(0.0)
        # -1 (NxN)
        G = cvxopt.matrix(np.diag(np.ones(n_samples) * -1))
        # 0 (1xN)
        h = cvxopt.matrix(np.zeros(n_samples))

        solution = cvxopt.solvers.qp(P, q, G, h, A, b)
        # Lagrange multipliers
        a = np.ravel(solution['x'])
        # Lagrange have non zero lagrange multipliers
        sv = a > 1e-5
        ind = np.arange(len(a))[sv]
        self.alpha = a[sv]
        self.support_vectors = X_train[sv]
        self.support_vector_y = y_train[sv]
        # Intercept
        self.b = 0
        for n in range(len(self.alpha)):
            self.b += self.support_vector_y[n]
            self.b -= np.sum(self.alpha * self.support_vector_y * K[ind[n], sv])
        self.b /= len(self.alpha)
        # Weights
        self.w = np.zeros(n_features)
        for n in range(len(self.alpha)):
            self.w += self.alpha[n] * self.support_vector_y[n] * self.support_vectors[n]

    def predict(self, X_test):
        return self.sign(np.dot(X_test, self.w) + self.b)

    def f1_score(self, X_test, y_test):
        pass

if __name__ == '__main__':
    X, y = make_blobs(n_samples=250, centers=2, random_state=100, cluster_std=1)
    y[y == 0] = -1
    tmp = np.ones(len(X))
    y = tmp * y

    plt.scatter(X[:, 0], X[:, 1], c=y, cmap='winter')
    plt.show()

    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

    svm = SVM()
    svm.fit(X_train, y_train)

    def f(x, w, b, c=0):
        return (-w[0] * x - b + c) / w[1]

    plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap='winter')
    # w.x + b = 0
    a0 = -4
    a1 = f(a0, svm.w, svm.b)
    b0 = 4;
    b1 = f(b0, svm.w, svm.b)
    plt.plot([a0, b0], [a1, b1], 'k')
    # w.x + b = 1
    a0 = -4;
    a1 = f(a0, svm.w, svm.b, 1)
    b0 = 4;
    b1 = f(b0, svm.w, svm.b, 1)
    plt.plot([a0, b0], [a1, b1], 'k--')
    # w.x + b = -1
    a0 = -4;
    a1 = f(a0, svm.w, svm.b, -1)
    b0 = 4;
    b1 = f(b0, svm.w, svm.b, -1)
    plt.plot([a0, b0], [a1, b1], 'k--')
    plt.show()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 15 2021
@author: sagrana
"""
from rest_framework import status
from rest_framework.generics import CreateAPIView, RetrieveAPIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from .models import User
from .serializers import UserRegistrationSerializer
from .serializers import UserLoginSerializer

class UserRegistrationView(CreateAPIView):
    """"UserRegistrationView
    """
    serializer_class = UserRegistrationSerializer
    permission_classes = (AllowAny,)

    def post(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        response = {
            'success': 'True',
            'status code': status.HTTP_200_OK,
            'message': 'User registered  successfully',
            }
        status_code = status.HTTP_200_OK
        return Response(response, status=status_code)

class UserLoginView(RetrieveAPIView):
    """UserLoginView
    """

    permission_classes = (AllowAny,)
    serializer_class = UserLoginSerializer
    queryset = User.objects.all()

    def post(self, request):
        """post

        :param request:
        :return:
        """
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = {
            'success': 'True',
            'status code': status.HTTP_200_OK,
            'message': 'User logged in  successfully',
            'token': serializer.data['token'],
            }
        status_code = status.HTTP_200_OK

        return Response(response, status=status_code)


from rest_framework.decorators import api_view
from rest_framework.response import Response
from base.serializers import ProductSerializer, UserSerializer, UserSerializerWithToken
from base.models import Product

@api_view(['GET'])
def getProducts(request):
    query = request.query_params.get('keyword')
    if query == None:
      query = ""

    products = Product.objects.filter(name__icontains=query)
    serializer = ProductSerializer(products, many=True)
    return Response(serializer.data)

@api_view(['GET'])
def getTopProducts(request):
    products = Product.objects.filter(rating__gte=4).order_by('-rating')[0:5]
    serializer = ProductSerializer(products,  many=True)
    return Response(serializer.data)

@api_view(['GET'])
def getProduct(request, pk):
    product = Product.objects.get(_id=pk)
    serializer = ProductSerializer(product, many=False)
    return Response(serializer.data)


from . import parse as parser
from modules import YaraRules, GeoIP, ProjectHoneyPot, LangDetect

class Scanner:
  def __init__(self):
    self.yara_manager = YaraRules.YaraManager()

  def parse_email(self, email_content: str):
    return parser.parse_email(email_content)

  def scan(self, email: str):
    # parse it
    parsed_email = self.parse_email(email)

    # Use LangDetect on the body of the email, check if it's HTML or not
    # If it's HTML, use BeautifulSoup to parse it
    # If it's not HTML, just continue like usual

    content = parsed_email.get_payload()
    #lang = LangDetect.detect_language(content)
    potentialLanguage = []

    # Loop around the content if it has multiple parts
    # Then use the LangDetect to detect the language of each part
    # Append the result to the potentialLanguage list
    if parsed_email.is_multipart():
      for part in parsed_email.walk():
        content_type = part.get_content_type()
        content_disposition = str(part.get("Content-Disposition"))

        # Extract text/plain content
        if "attachment" not in content_disposition and "text/plain" in content_type:
          content = part.get_payload(decode=True)
          print("Content -> ", content)
          # turn content into string but also fix some encoding issues, and make it prettier for it to read
          content = content.decode('utf-8', 'ignore')
          content = content.replace("\r\n", "")
          content = content.replace("\n", "")
          content = content.replace("\t", "")

          lang = LangDetect.detect_language(content)
          potentialLanguage.append(lang)
        else:
          continue

    print("Language -> ", potentialLanguage)

    # get ip and geoip
    ip = parsed_email.get("Received-SPF").split("client-ip=")[1].split(";")[0]
    print("IP Address -> " + str(ip))

    #geoip = GeoIP.GeoIP(ip)
    #print("GeoIP -> ", geoip)

    # check if ip is in honeypot
    honeypot = ProjectHoneyPot.ProjectHoneyPot(ip)
    print("Honeypot -> " + str(honeypot))

    # analyze it
    analysis_result = self.yara_manager.analyze_email(email)

    # return the result
    return {
      "analysis_result": analysis_result,
      "parsed_email": parsed_email,
      #"geoip": geoip,
      "honeypot": honeypot,

    }


import numpy as np
import scipy.integrate
import scipy.optimize
import bokeh.plotting
from bokeh.plotting import figure, output_file, show
import bokeh.io
from bokeh.models import Span

def dilute(molecule_diluted,molecules_0,DR=0.2):  #input Object want to dilute and where the parameters is stored
        molecules_0[molecule_diluted.idx] *= (1-DR)

def replenish(molecule_replenished, molecules_0, DR=0.2) : #input Object want to replenish and where the parameters is stored
    molecules_0[molecule_replenished.idx] += molecule_replenished.lc * DR

def dilute_species(molecules_diluted,molecules_0,DR=0.2):  #dilute a list of molecules
    for molecule in (molecules_diluted):
        dilute(molecule,molecules_0,DR)

def replenish_species(molecules_replenished, molecules_0, DR=0.2) :   #replenish a list of molecules
   for molecule in (molecules_replenished):
        replenish(molecule,molecules_0,DR)

def run_model(model,t,parameters_list,molecules_0,dilute_list,replenish_list,result_all,DR=0.2):
    start_cycle,end_cycle = np.array(t)*4
    for n in range (start_cycle,end_cycle):
        #define time
        t_start= n*15
        t_end = (n+1)*15
        t= np.linspace(t_start,t_end,2)

        #solve equation and save result
        result = scipy.integrate.odeint(model, molecules_0, t, args=parameters_list)
        result_all = np.append(result_all,result[1])

        #update parameter
        molecules_0 = result.transpose()[:,-1]

        #dilution
        ###diute out
        dilute_species((dilute_list),molecules_0,DR)

        ###replenish
        replenish_species((replenish_list),molecules_0,DR)
    return result_all,molecules_0

def plot_result(molecule):
    t = np.linspace(0, 15*(len(molecule)-1), len(molecule))
    p = bokeh.plotting.figure(
        plot_width=800,
        plot_height=400,
        x_axis_label="t",
        y_axis_type="linear",
    )

    colors = bokeh.palettes.d3["Category10"][3]

    # Populate glyphs
    p.line(
        t/60, molecule, line_width=2, color=colors[0]
    )
    vline1 = Span(location=4, dimension='height', line_color='black', line_width=1,line_dash='dashed')
    vline2 = Span(location=16, dimension='height', line_color='black', line_width=1,line_dash='dashed')
    p.add_layout(vline1)
    p.add_layout(vline2)
    show(p)

def plot_result_two_state(molecule):
    t = np.linspace(0, 15*(len(molecule)-1), len(molecule))
    p = bokeh.plotting.figure(
        plot_width=800,
        plot_height=400,
        x_axis_label="t",
        y_axis_type="linear",
    )

    colors = bokeh.palettes.d3["Category10"][3]

    # Populate glyphs
    p.line(
        t/60, molecule, line_width=2, color=colors[0]
    )
    vline1 = Span(location=4, dimension='height', line_color='black', line_width=1,line_dash='dashed')
    #vline2 = Span(location=16, dimension='height', line_color='black', line_width=1,line_dash='dashed')
    p.add_layout(vline1)
    #p.add_layout(vline2)
    show(p)


from collections import defaultdict
def createGraph():
    g=defaultdict(list)
    return g

def topoSort(g,indeg,q,cnt,n,res):
    for i in range(n):
        if indeg[i] is 0:
            q.append(i)
    while(q):
        cur=q.pop(0)
        for i in g[cur]:
            indeg[i]-=1
            if(indeg[i] is 0):
                q.append(i)
        res.append(cur)
        cnt+=1
    if cnt is n:
        return True
    return False

def kahnsAlgo(g,indeg,n):
    q,res=[],[]
    if topoSort(g,indeg,q,0,n,res) is True:
        return res
    return []

if __name__ == "__main__":
    # prequisites=[[1,0],[2,0],[3,1],[3,2]]
    prequisites=[[1,0],[2,1],[3,2],[1,3]]
    g=createGraph()
    n=4
    indeg=[0]*n
    for i,j in prequisites:
        g[j].append(i)
        indeg[i]+=1
    ans=kahnsAlgo(g,indeg,n)
    print(ans)


from lib.contents_reader import ContentsReader
import asyncio

CLEAR_SCREEN = "\u001b[2J"
NEW_LINE = "\r\n"

class ZineFunctions:
    def __init__(self, reader, writer, index_file_path):
        self.reader = reader
        self.writer = writer
        self.contents_reader = ContentsReader(index_file_path)

    async def run_index(self):
        for welcome_line in self.contents_reader.read_hello_file():
            self.writer.write(welcome_line)
        await self.writer.drain()
        # Read one byte (any key)
        await self.reader.read(1)
        running = True
        while (running):
            for index_line in self.contents_reader.read_index_lines():
                self.writer.write(index_line)
            item_choice = await self.reader.read(1)
            item_choice_int = -1
            if item_choice.upper() == 'X':
                running = False
                continue
            item_choice_int = self.contents_reader.map_input_to_numerical_index(item_choice)
            if item_choice_int == -1:
                self.writer.write(f"{NEW_LINE}{NEW_LINE}Pick a story, or X to quit.{NEW_LINE}")
                continue
            self.writer.write(f"{NEW_LINE}{NEW_LINE}...you picked: %s" % (item_choice))
            self.writer.write(f"{NEW_LINE}{NEW_LINE}...press RETURN to start reading, and to continue after each page")
            await self.reader.read(1)
            self.writer.write(NEW_LINE + CLEAR_SCREEN)
            await asyncio.sleep(1)
            await self.run_story(item_choice_int)
        self.disconnect()

    async def run_story(self, story_number):
        page_number = 1

        story_lines = self.contents_reader.read_story(story_number, page_number)
        while len(story_lines) > 0:
            self.writer.write(CLEAR_SCREEN)
            for story_line in story_lines:
                self.writer.write(story_line)
            await self.writer.drain()
            char_read = await self.reader.readline()
            page_number += 1
            story_lines = self.contents_reader.read_story(story_number, page_number)

    def disconnect(self):
        self.writer.close()


"""Extract data on near-Earth objects and close approaches from CSV and JSON files.

The `load_neos` function extracts NEO data from a CSV file, formatted as
described in the project instructions, into a collection of `NearEarthObject`s.

The `load_approaches` function extracts close approach data from a JSON file,
formatted as described in the project instructions, into a collection of
`CloseApproach` objects.

The main module calls these functions with the arguments provided at the command
line, and uses the resulting collections to build an `NEODatabase`.

You'll edit this file in Task 2.
"""
import csv
import json

from models import NearEarthObject, CloseApproach

def load_neos(neo_csv_path):
    """Read near-Earth object information from a CSV file.

    :param neo_csv_path: A path to a CSV file containing data about near-Earth objects.
    :return: A collection of `NearEarthObject`s.
    """
    neo_list = []
    neo_collection = []

    with open(neo_csv_path) as csv_file:
        csv_reader = csv.DictReader(csv_file)
        for row in csv_reader:
             neo_list.append((dict([('designation',row['pdes']), ('name', row['name']),('diameter', row['diameter']),('hazardous', row['pha'])])))

    for neo_dict in neo_list:
        neo_collection.append(NearEarthObject(**neo_dict))
    return neo_collection

def load_approaches(cad_json_path):
    """Read close approach data from a JSON file.

    :param cad_json_path: A path to a JSON file containing data about close approaches.
    :return: A collection of `CloseApproach`es.
    """
    cap_list = []
    cap_collection = []

    with open(cad_json_path, 'r') as json_file:
        json_reader = json.load(json_file)

    for i in range(len(json_reader['data'])):
        cap_list += [dict(zip(['_designation', 'time', 'distance', 'velocity'], [json_reader['data'][i][0], json_reader['data'][i][3], json_reader['data'][i][4], json_reader['data'][i][7]]))]

    for cap_dict in cap_list:
        cap_collection.append(CloseApproach(**cap_dict))

    return cap_collection


from setuptools import setup

package_name = 'leg_controller'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='pi',
    maintainer_email='jamespetri28@gmail.com',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            "servo_node = leg_controller.servoController:main",
            "kin_node = leg_controller.pointToAngle:main",
            "animation_node = leg_controller.simpleCommands:main"
        ],
    },
)


from fastapi import APIRouter, Depends
from app.dependencies import verify_api_key
from app.libraries.libpoller import Poller
from app.schemas.poller import PollerModel, PollerUpdateModel, PollerCreateModel

router = APIRouter(tags=["poller"])
oPoller = Poller()

# Poller Requests ( API_KEY required )

@router.get("/poller/devices")
async def get_poller_devices(poller = Depends(verify_api_key)):
    return await oPoller.get_poller_devices(poller)

# CRUID Poller Requests ( JWT required )

@router.get("/poller/schema")
async def get_poller_schema(joined: bool = False):
  return await oPoller.get_poller_schema(joined=joined)

@router.get("/poller")
async def get_poller_list(joined: bool = False, limit: int = 100, offset: int = 0, sortField: str = None, sortOrder: str = "asc", search: str = ""):
    return await oPoller.get_poller_list(joined=joined, limit=limit, offset=offset, sortField=sortField, sortOrder=sortOrder, search=search)

@router.get("/poller/{pollerid}")
async def get_poller(pollerid: int, joined: bool = False):
    return await oPoller.get_poller(pollerid, joined=joined)

@router.post("/poller")
async def create_poller(poller: PollerCreateModel):
    return await oPoller.create_poller(poller)

@router.put("/poller/{pollerid}")
async def update_poller(pollerid: int, poller: PollerUpdateModel):
    return await oPoller.update_poller(pollerid, poller)

@router.delete("/poller/{pollerid}")
async def delete_poller(pollerid: int):
    return await oPoller.delete_poller(pollerid)


from mangaki.models import Artist, Manga, Genre
from django.db.utils import IntegrityError, DataError
import re
from collections import Counter

def run():
    with open('../data/manga-news/manga.csv') as f:
        next(f)
        artists = {}
        hipsters = Counter()
        for i, line in enumerate(f):
            # print(len(line.split(';;')))
            title, vo_title, writer, mangaka, editor, origin, genre1, genre2, manga_type, synopsis, poster = line.split(';;')
            for artist in [writer, mangaka]:
                if artist in artists:
                    continue
                m = re.match('^([A-ZÔÛÏ\'-]+) (.*)$', writer)
                if m:
                    last_name, first_name = m.groups()
                    last_name = last_name.lower().capitalize()
                if not m:
                    first_name = ''
                    last_name = artist
                if Artist.objects.filter(first_name=first_name, last_name=last_name).count() == 0:
                    a = Artist(first_name=first_name, last_name=last_name)
                    a.save()
                else:
                    a = Artist.objects.get(first_name=first_name, last_name=last_name)
                artists[artist] = a
    with open('../data/manga-news/manga.csv') as f:
        next(f)
        for i, line in enumerate(f):
            title, vo_title, writer, mangaka, editor, origin, genre1, genre2, manga_type, synopsis, poster = line.split(';;')
            try:
                if Manga.objects.filter(title=title, vo_title=vo_title).count() == 0:
                    manga = Manga(title=title, vo_title=vo_title, mangaka=artists[mangaka], writer=artists[writer], editor=editor, origin=origin.lower().replace('hong kong', 'hong-kong').replace('international', 'intl'), manga_type=manga_type.lower(), source='', poster=poster, synopsis=synopsis)
                    manga.save()
                else:
                    manga = Manga.objects.get(title=title, vo_title=vo_title)
                if genre1:
                    manga.genre.add(Genre.objects.get(title=genre1))
                if genre2:
                    manga.genre.add(Genre.objects.get(title=genre2))
            except IntegrityError as err:
                print(line)
                print(writer)
                print(err)
                break
            except DataError as err:
                print(line)
                print(origin)
                print(err)
                break
            except Genre.DoesNotExist as err:
                print(line)
                print('Genres: [%s] [%s]' % (genre1, genre2))
                print(err)
                break

run()


# Test the models with LG_chem stock
# If the prediction is success, Expand the number of stock
import math
import os
import pdb
from datetime import datetime

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm

import torch
import torch.nn as nn
from torch.utils.data import DataLoader, Dataset

from sklearn.preprocessing import MinMaxScaler

base = os.path.abspath(__file__)
base = base.split('/')

def save_stock_plot(rawdata, stock_name="LG_chme"):
    global base
    try:
        plt.figure(figsize=(20,5))
        plt.plot(range(len(rawdata)), rawdata['Close'])
        path = "/".join(base[:-2]+["models"])
        file_name = f"/{stock_name}.jpg"
        path += file_name
        plt.savefig(path)
        print("Save Success!!")
    except Exception as e:
        print(f"Save Stock plot Failed!!: {e}")

class windowDataset(Dataset):
    def __init__(self, y, input_window=80, output_window=20, stride=5, n_attr=1):
        #총 데이터의 개수

        L = y.shape[0]
        #stride씩 움직일 때 생기는 총 sample의 개수
        num_samples = (L - input_window - output_window) // stride + 1

        if n_attr == 1:
            #input과 output
            X = np.zeros([input_window, num_samples])
            Y = np.zeros([output_window, num_samples])
            for i in np.arange(num_samples):
                start_x = stride*i
                end_x = start_x + input_window
                X[:,i] = y[start_x:end_x]

                start_y = stride*i + input_window
                end_y = start_y + output_window
                Y[:,i] = y[start_y:end_y]
            X = X.reshape(X.shape[0], X.shape[1], n_attr)
            Y = Y.reshape(Y.shape[0], Y.shape[1], n_attr)
            X = X.transpose((1,0,2))
            Y = Y.transpose((1,0,2))
            self.x = X
            self.y = Y
        else:
            #input과 output
            X = np.zeros([input_window, n_attr, num_samples])
            Y = np.zeros([output_window, n_attr, num_samples])

            for i in np.arange(num_samples):
                start_x = stride*i
                end_x = start_x + input_window
                X[:,:,i] = y[start_x:end_x]

                start_y = stride*i + input_window
                end_y = start_y + output_window
                Y[:,:,i] = y[start_y:end_y]
            X = X.reshape(X.shape[2], X.shape[0], X.shape[1])
            Y = Y.reshape(Y.shape[2], Y.shape[0], Y.shape[1])
            self.x = X
            self.y = Y
        self.len = len(X)
    def __getitem__(self, i):
        return self.x[i], self.y[i]
        #return self.x[i], self.y[i, :-1], self.y[i,1:]
    def __len__(self):
        return self.len

class TFModel(nn.Module):
    def __init__(self,iw, ow, d_model, nhead, nlayers, dropout=0.5, n_attr=1):
        super(TFModel, self).__init__()
        self.encoder_layer = nn.TransformerEncoderLayer(d_model=d_model, nhead=nhead, dropout=dropout)
        self.transformer_encoder = nn.TransformerEncoder(self.encoder_layer, num_layers=nlayers)
        self.pos_encoder = PositionalEncoding(d_model, dropout)

        self.encoder = nn.Sequential(
            nn.Linear(n_attr, d_model//2),
            nn.ReLU(),
            nn.Linear(d_model//2, d_model)
        )

        self.linear =  nn.Sequential(
            nn.Linear(d_model, d_model//2),
            nn.ReLU(),
            nn.Linear(d_model//2, n_attr)
        )

        self.linear2 = nn.Sequential(
            nn.Linear(iw, (iw+ow)//2),
            nn.ReLU(),
            nn.Linear((iw+ow)//2, ow)
        )

    def generate_square_subsequent_mask(self, sz):
        mask = (torch.triu(torch.ones(sz, sz)) == 1).transpose(0, 1)
        mask = mask.float().masked_fill(mask == 0, float('-inf')).masked_fill(mask == 1, float(0.0))
        return mask

    def forward(self, src, srcmask):
        src = self.encoder(src)
        src = self.pos_encoder(src)
        output = self.transformer_encoder(src.transpose(0,1), srcmask).transpose(0,1)
        output = self.linear(output)[:,:,0]
        output = self.linear2(output)
        return output

class TFModel2(nn.Module):
    def __init__(self,d_model, nhead, nhid, nlayers, dropout=0.5, n_attr=7):
        super(TFModel2, self).__init__()
        self.transformer = nn.Transformer(d_model=d_model, nhead=nhead, dim_feedforward=nhid, num_encoder_layers=nlayers, num_decoder_layers=nlayers,dropout=dropout)
        self.pos_encoder = PositionalEncoding(d_model, dropout)
        self.pos_encoder_d = PositionalEncoding(d_model, dropout)
        self.linear = nn.Linear(d_model, n_attr)
        self.encoder = nn.Linear(n_attr, d_model)
        self.encoder_d = nn.Linear(n_attr, d_model)

    def generate_square_subsequent_mask(self, sz):
        mask = (torch.triu(torch.ones(sz, sz)) == 1).transpose(0, 1)
        mask = mask.float().masked_fill(mask == 0, float('-inf')).masked_fill(mask == 1, float(0.0))
        return mask

    def forward(self, src, tgt, srcmask, tgtmask):
        src = self.encoder(src)
        src = self.pos_encoder(src)

        tgt = self.encoder_d(tgt)
        tgt = self.pos_encoder_d(tgt)
        output = self.transformer(src.transpose(0,1), tgt.transpose(0,1), srcmask, tgtmask)
        output = self.linear(output)
        return output

class PositionalEncoding(nn.Module):
    def __init__(self, d_model, dropout=0.1, max_len=5000):
        super(PositionalEncoding, self).__init__()
        self.dropout = nn.Dropout(p=dropout)

        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        pe = pe.unsqueeze(0).transpose(0, 1)
        self.register_buffer('pe', pe)

    def forward(self, x):
        x = x + self.pe[:x.size(0), :]
        return self.dropout(x)

def gen_attention_mask(x):
    mask = torch.eq(x, 0)
    return mask

def evaluate(data_train, device, model, iw, n_attr, length):
    # 마지막 30*2일 입력으로 넣어서 그 이후 30일 예측 결과 얻음.
    input = torch.tensor(data_train[-iw:]).reshape(1,-1,n_attr).to(device).float().to(device)
    model.eval()

    src_mask = model.generate_square_subsequent_mask(input.shape[1]).to(device)
    predictions = model(input, src_mask)
    return predictions.detach().cpu().numpy()
    """
    input = torch.tensor(data_train[-iw:]).reshape(1,-1,n_attr).to(device).float().to(device)
    output = torch.tensor(data_train[-1].reshape(1,-1,n_attr)).float().to(device)
    model.eval()
    for i in range(length):
        src_mask = model.generate_square_subsequent_mask(input.shape[1]).to(device)
        tgt_mask = model.generate_square_subsequent_mask(output.shape[1]).to(device)

        predictions = model(input, output, src_mask, tgt_mask).transpose(0,1)
        predictions = predictions[:, -1:, :]
        output = torch.cat([output, predictions.to(device)], axis=1)
    return torch.squeeze(output, axis=0).detach().cpu().numpy()[1:]
    """

def predict(stock, period):
    global base
    print(f"Notice: Since it is in the initial stage of the service, \
          we predict only the stock price of LG Chem, not the stock price \
          of the designated company.\n\n")
    # 이 코드대신 지수형이 spl로 얻어온 data가 rawdata가 되어야 함.
    # 추가적인 정보 없는건 1729일

    print(f"Loading Stock Data ...")
    n_attr = 1
    path = "/".join(base[:-3]+["data","lg_chem_closing_prices.csv"])
    model_path = "/".join(base[:-2]+["Prediction", f"{stock}_{datetime.now().date()}.pth"])
    rawdata = pd.read_csv(path)

    print(f"Saving Stock data as .png ...")
    save_stock_plot(rawdata, stock)
    #pdb.set_trace()
    print(f"Preprocessing Data with MinMaxScaling ...")
    min_max_scaler = MinMaxScaler()
    rawdata["Close"] = min_max_scaler.fit_transform(rawdata["Close"].to_numpy().reshape(-1,n_attr))

    print(f"Spliting Data ...")
    iw = 30*7
    ow = 10

    train = rawdata[:-iw]
    data_train = train["Close"].to_numpy()
    test = rawdata[-iw:]
    data_test = test["Close"].to_numpy()

    print(f"Preparing Dataset ...")
    train_dataset = windowDataset(data_train, input_window=iw, output_window=ow, stride=1, n_attr=n_attr)
    train_loader = DataLoader(train_dataset, batch_size=64)
    #test_dataset = windowDataset(data_test, input_window=iw, output_window=ow, stride=1, n_attr=n_attr)
    #test_loader = DataLoader(test_dataset)
    """
    # 성능 올리기위해 종가말고 다른 것도 같이 넣음.
    # 총 1720일의 data있음
    print(f"Loading Stock Data ...")
    n_attr = 7
    path = "/".join(base[:-3]+["data","lg_chem_prices.csv"])
    rawdata = pd.read_csv(path)

    #print(f"Saving Stock data as .png ...")
    #save_stock_plot(rawdata, stock)
    print(f"Preprocessing Data with MinMaxScaling ...")
    min_max_scaler = MinMaxScaler()
    rawdata.loc[:,rawdata.columns] = min_max_scaler.fit_transform(rawdata.to_numpy())

    print(f"Spliting Data ...")
    iw = 60
    ow = 5
    #pdb.set_trace()
    train = rawdata[:-(iw)]
    data_train = train.to_numpy()
    test = rawdata[-(iw):]
    data_test = test.to_numpy()

    print(f"Preparing Dataset ...")
    train_dataset = windowDataset(data_train, input_window=iw, output_window=ow, stride=1, n_attr=n_attr)
    train_loader = DataLoader(train_dataset, batch_size=64)
    #test_dataset = windowDataset(data_test, input_window=iw, output_window=ow, stride=1, n_attr=n_attr)
    #test_loader = DataLoader(test_dataset)
    """
    print(f"Model Constructing ...")
    device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
    lr = 1e-4
    #model = TFModel2(256, 8, 256, 2, 0.1, n_attr).to(device)
    model = TFModel(iw, ow, 512, 8, 4, 0.4, n_attr).to(device)
    criterion = nn.MSELoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=lr)

    if not os.path.exists(model_path):
        print("Trainig ...")
        epoch = 10

        model.train()
        for i in range(epoch):
            batchloss = 0.0
            for (inputs, outputs) in tqdm(train_loader):
                optimizer.zero_grad()
                src_mask = model.generate_square_subsequent_mask(inputs.shape[1]).to(device)
                result = model(inputs.float().to(device),  src_mask)
                loss = criterion(result, outputs[:,:,0].float().to(device))
                loss.backward()
                optimizer.step()
                batchloss += loss
            print(f"{i+1}th epoch MSEloss:" + "{:0.6f}".format(batchloss.cpu().item() / len(train_loader)))
        torch.save(model, model_path)
        """
        model.train()
        progress = tqdm(range(epoch))
        for i in progress:
            batchloss = 0.0

            for (inputs, dec_inputs, outputs) in train_loader:
                optimizer.zero_grad()
                src_mask = model.generate_square_subsequent_mask(inputs.shape[1]).to(device)
                tgt_mask = model.generate_square_subsequent_mask(dec_inputs.shape[1]).to(device)

                result = model(inputs.float().to(device), dec_inputs.float().to(device), src_mask, tgt_mask)
                loss = criterion(result.permute(1,0,2), outputs.float().to(device))

                loss.backward()
                optimizer.step()
                batchloss += loss
            progress.set_description("{:0.5f}".format(batchloss.cpu().item() / len(train_loader)))

        """
    torch.save(model.state_dict(), model_path)
    print("Predicting ...")
    result = evaluate(data_test, device, model, iw, n_attr, ow)

    result = min_max_scaler.inverse_transform(result)[0]
    real = rawdata["Close"].to_numpy()
    real = min_max_scaler.inverse_transform(real.reshape(-1,1))[:,0]

    #pdb.set_trace()
    """
    tmp = np.zeros((10,7))
    tmp[:,:] = result.reshape(10,-1)
    result = tmp
    result = min_max_scaler.inverse_transform(result).reshape(-1,10)[3]
    real = rawdata.to_numpy()
    real = min_max_scaler.inverse_transform(real)[:,3]
     """
    plt.figure(figsize=(20,5))
    #plt.plot(range(1419,1719),real[1420:], label="real")
    plt.plot(range(1419,1719),real[1418:],label="real")
    plt.plot(range(1719-ow,1719),result, label="predict")
    plt.legend()
    path = "/".join(base[:-2]+["models","prediction2.jpg"])
    plt.savefig(path)
    print(f"Complete!!")

    # 예측된 가격의 평균과, 직전의 값을 비교했을 때, 평균이 크면 사라, 작으면 사지 마라.
    mean_pred = np.mean(result)
    if mean_pred >= real[-1]:
        answer = f"""You should buy the stock you want to know the price, because we predict the price will rise.
        Maybe it will be {mean_pred}won."""
    else:
        answer = f"""You shouldn't buy the stock you want to know the price, because we predict the price will go down.
        Maybe it will be {mean_pred}won."""

    return answer

if __name__=="__main__":
    print(predict("", ""))


#!/usr/bin/env python3

import asyncio
from mavsdk import System
from mavsdk.gimbal import GimbalMode, ControlMode

async def run():
    # Init the drone
    drone = System()
    await drone.connect(system_address="udp://:14540")

    # Start printing gimbal position updates
    print_gimbal_position_task = \
        asyncio.ensure_future(print_gimbal_position(drone))

    print("Taking control of gimbal")
    await drone.gimbal.take_control(ControlMode.PRIMARY)

    # Set the gimbal to YAW_LOCK (= 1) mode (see docs for the difference)
    # Other valid values: YAW_FOLLOW (= 0)
    # YAW_LOCK will fix the gimbal pointing to an absolute direction,
    # whereas YAW_FOLLOW will point relative to vehicle heading.
    print("Setting gimbal mode")
    await drone.gimbal.set_mode(GimbalMode.YAW_FOLLOW)

    print("Look forward first")
    await drone.gimbal.set_pitch_and_yaw(0, 0)
    await asyncio.sleep(1)

    print("Look down")
    await drone.gimbal.set_pitch_and_yaw(-90, 0)
    await asyncio.sleep(2)

    print("Back to horizontal")
    await drone.gimbal.set_pitch_and_yaw(0, 0)
    await asyncio.sleep(2)

    print("Slowly look up")
    await drone.gimbal.set_pitch_rate_and_yaw_rate(10, 0)
    await asyncio.sleep(3)

    print("Back to horizontal")
    await drone.gimbal.set_pitch_and_yaw(0, 0)
    await asyncio.sleep(2)

    print("Look right")
    await drone.gimbal.set_pitch_and_yaw(0, 90)
    await asyncio.sleep(2)

    print("Look forward again")
    await drone.gimbal.set_pitch_and_yaw(0, 0)
    await asyncio.sleep(2)

    print("Slowly look to the left")
    await drone.gimbal.set_pitch_rate_and_yaw_rate(0, -20)
    await asyncio.sleep(3)

    print("Look forward again")
    await drone.gimbal.set_pitch_and_yaw(0, 0)
    await asyncio.sleep(2)

    # Set the gimbal to track a region of interest (lat, lon, altitude)
    # Units are degrees and meters MSL respectively
    print("Look at a ROI (region of interest)")
    await drone.gimbal.set_roi_location(47.39743832, 8.5463316, 488)
    await asyncio.sleep(3)

    print("Look forward again")
    await drone.gimbal.set_pitch_and_yaw(0, 0)
    await asyncio.sleep(2)

    print("Release control of gimbal again")
    await drone.gimbal.release_control()

    print_gimbal_position_task.cancel()

async def print_gimbal_position(drone):
    # Report gimbal position updates asynchronously
    # Note that we are getting gimbal position updates in
    # euler angles; we can also get them as quaternions
    async for angle in drone.telemetry.camera_attitude_euler():
        print(f"Gimbal pitch: {angle.pitch_deg}, yaw: {angle.yaw_deg}")

if __name__ == "__main__":
    # Run the asyncio loop
    asyncio.run(run())


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

from fealpy.mesh.uniform_mesh_2d import UniformMesh2d
from scipy.sparse.linalg import spsolve
#from ..decorator import cartesian

class MembraneOscillationPDEData: # 点击这里可以查看 FEALPy 中的代码

    def __init__(self, D=[0, 1, 0, 1], T=[0, 5]):
        """
        @brief 模型初始化函数

        @param[in] D 模型空间定义域
        @param[in] T 模型时间定义域
        """
        self._domain = D
        self._duration = T

    def domain(self):
        """
        @brief 空间区间
        """
        return self._domain

    def duration(self):
        """
        @brief 时间区间
        """
        return self._duration

    def source(self, p, t):
        """
        @brief 方程右端项

        @param[in] p numpy.ndarray, 空间点
        @param[in] t float, 时间点

        @return 0
        """
        return np.zeros_like(p[..., 0])

    def init_solution(self, p):
        """
        @brief 初值条件

        @param[in] p numpy.ndarray, 空间点
        @param[in] t float, 时间点

        @return 返回 val
        """

        x, y = p[..., 0], p[..., 1]
        val = x**2*(x+y)
        return val

    def init_solution_diff_t(self, p):
        """
         @brief 初值条件的导数

         @param[in] p numpy.ndarray, 空间点
        """
        return np.zeros_like(p[..., 0])

    #@cartesian
    def dirichlet(self, p, t):
        """
        @brief Dirichlet 边界条件

        @param[in] p numpy.ndarray, 空间点
        @param[in] t float, 时间点

        @return 边界条件函数值
        """
        return np.zeros_like(p[..., 0])

pde = MembraneOscillationPDEData()

# 空间离散
domain = pde.domain()
nx = 100
ny = 100
hx = (domain[1] - domain[0])/nx
hy = (domain[3] - domain[2])/ny
mesh = UniformMesh2d([0, nx, 0, ny], h=(hx, hy), origin=(domain[0], domain[2]))

# 时间离散
duration = pde.duration()
nt = 1000
tau = (duration[1] - duration[0])/nt

# 准备初值
uh0 = mesh.interpolate(pde.init_solution, 'node') # （nx+1, ny+1)
vh0 = mesh.interpolate(pde.init_solution_diff_t, 'node') # (nx+1, ny+1)
uh1 = mesh.function('node') # (nx+1, ny+1)

def advance_explicit(n, *frags):
    """
    @brief 时间步进为显格式

    @param[in] n int, 表示第 n 个时间步
    """
    t = duration[0] + n*tau
    if n == 0:
        return uh0, t
    elif n == 1:
        rx = tau/hx
        ry = tau/hy
        uh1[1:-1, 1:-1] = 0.5*rx**2*(uh0[0:-2, 1:-1] + uh0[2:, 1:-1]) + \
                0.5*ry**2*(uh0[1:-1, 0:-2] + uh0[1:-1, 2:]) + \
                (1 - rx**2 - ry**2)*uh0[1:-1, 1:-1] + tau*vh0[1:-1, 1:-1]
        gD = lambda p: pde.dirichlet(p, t)
        mesh.update_dirichlet_bc(gD, uh1)
        return uh1, t
    else:
        A = mesh.wave_operator_explicit(tau)
        source = lambda p: pde.source(p, t + tau)
        f = mesh.interpolate(source, intertype='node')
        f *= tau**2
        uh2 = A@uh1.flat - uh0.flat

        uh0[:] = uh1[:]
        uh1.flat = uh2

        gD = lambda p: pde.dirichlet(p, t + tau)
        mesh.update_dirichlet_bc(gD, uh1)

        #solution = lambda p: pde.solution(p, t + tau)
        #e = mesh.error(solution, uh1, errortype='max')
        #print(f"the max error is {e}")

        return uh1, t
def advance_implicit(n, *frags):
    """
    @brief 时间步进为隐格式

    @param[in] n int, 表示第 n 个时间步
    """
    t = duration[0] + n*tau
    if n == 0:
        return uh0, t
    elif n == 1:
        rx = tau/hx
        ry = tau/hy
        uh1[1:-1, 1:-1] = 0.5*rx**2*(uh0[0:-2, 1:-1] + uh0[2:, 1:-1]) + \
                0.5*ry**2*(uh0[1:-1, 0:-2] + uh0[1:-1, 2:]) + \
                (1 - rx**2 - ry**2)*uh0[1:-1, 1:-1] + tau*vh0[1:-1, 1:-1]
        gD = lambda p: pde.dirichlet(p, t)
        mesh.update_dirichlet_bc(gD, uh1)
        return uh1, t
    else:
        A0, A1, A2 = mesh.wave_operator_implicit(tau)
        source = lambda p: pde.source(p, t + tau)
        f = mesh.interpolate(source, intertype='node')
        f *= tau**2
        f.flat += A1@uh1.flat + A2@uh0.flat

        uh0[:] = uh1[:]
        gD = lambda p: pde.dirichlet(p, t + tau)
        A0, f = mesh.apply_dirichlet_bc(gD, A0, f)
        uh1.flat = spsolve(A0, f)

        #solution = lambda p: pde.solution(p, t + tau)
        #e = mesh.error(solution, uh1, errortype='max')
        #print(f"the max error is {e}")

        return uh1, t
"""
box = [0, 1, 0, 1, 0, 5]
fig, axes = plt.subplots()
mesh.show_animation(fig, axes, box, advance_explicit,
                    fname='explicit.mp4', plot_type='imshow', frames=nt+1)
plt.show()
"""
box = [0, 1, 0, 1, -2, 2]
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
axes = fig.add_subplot(111, projection='3d')
mesh.show_animation(fig, axes, box, advance_explicit,
                    fname='explicit.mp4', plot_type='surface', frames=nt+1)
plt.show()
"""
box = [0, 1, 0, 1, -1, 1]
fig, axes = plt.subplots()
mesh.show_animation(fig, axes, box, advance_implicit,fname='implicit.mp4', plot_type='imshow', frames=nt+1)
plt.show()

box = [0, 1, 0, 1, -2.0, 2.0]

fig = plt.figure()
axes = fig.add_subplot(111, projection='3d')
mesh.show_animation(fig, axes, box, advance_implicit,fname='implicit.mp4', plot_type='surface', frames=nt+1)
plt.show()
"""


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

# # Import Library

# In[387]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from glob import glob
from sklearn.metrics import mean_absolute_error
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn.metrics import accuracy_score , classification_report
import pandas_profiling
from category_encoders import OneHotEncoder
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import cross_validate

# In[388]:

t=pd.read_csv("test.csv")
t.info()

# # Import Data

# In[389]:

def wrangel(path):
    # read data
    df=pd.read_csv(path)
    #extract the social name
    df["title"]=df["Name"].str.extract("([A-Za-z]+)\.",expand=False)
    #convert title categorcal data
    df.loc[df["title"]=="Mr"   , "title"]   = 0
    df.loc[df["title"]=="Miss"   , "title"]   = 1
    df.loc[df["title"]=="Mrs"   , "title"]   = 2
    df.loc[df["title"]=="Master"   , "title"]   = 3
    conditions = (df["title"] == 'Ms') | (df["title"] == 'Col') | (df["title"] == 'Rev') | (df["title"] == 'Dr') | (df["title"] == 'Dona')
    df.loc[conditions, "title"] = 4
    #fill NAN Value of Fare Accorging to Social Name
    df["Fare"].fillna(df.groupby("Pclass")["Fare"].transform("median"),inplace=True)
    #fill NAN Value of Age Accorging to Social Name
    df["Age"].fillna(df.groupby("title")["Age"].transform("median"),inplace=True)
    #fill NAN Value of Embarked Accorging to Median
    df["Embarked"]=df["Embarked"].fillna("S")
    #remove nan columns
    drop=[]
    drop.append("Cabin")
    drop.append("Name")
    drop.append("Ticket")
    drop.append("title")
    df.drop(columns=drop,inplace=True)
    #convert Sex categorcal data
    df.loc[df["Sex"]=="male"     , "Sex"]   = 0         # Male   ---> 0
    df.loc[df["Sex"]=="female"   , "Sex"]   = 1         # Female ---> 1
    #convert Embarked categorcal data
    df.loc[df["Embarked"]=="S"   , "Embarked"]   = 0    # S ---> 1
    df.loc[df["Embarked"]=="C"   , "Embarked"]   = 1    # C ---> 2
    df.loc[df["Embarked"]=="Q"   , "Embarked"]   = 2    # Q ---> 3

    return df

# In[390]:

test  =  wrangel("test.csv")
df    =  wrangel("train.csv")

# In[340]:

df.head()

# In[341]:

df.info()

# In[391]:

pandas_profiling.ProfileReport(df)

# In[343]:

df["Embarked"].value_counts()

# In[344]:

test.info()

# In[352]:

test.isnull().sum()

# # Exploer Data

# In[353]:

print("Survive :",(df["Survived"]==1).sum())
print("Deceased :",(df["Survived"]==0).sum())

# In[354]:

df.describe()

# In[355]:

# Create the pie chart
values=df["Survived"].value_counts()
label=["Deceased ","Survive "]
plt.pie(values, labels=label,autopct='%1.1f%%')
# Add a title
plt.title('Distribution of Survived')
# Display the chart
plt.show()

# In[356]:

plt.hist(df["Parch"],bins=5, edgecolor='black');
plt.xlabel('Values')
plt.ylabel('Frequancy')
plt.title("Values of Parch")
plt.show();

# In[357]:

survive=df[df["Survived"]==1]["SibSp"].value_counts()
death=df[df["Survived"]==0]["SibSp"].value_counts()
dx=pd.DataFrame([survive,death],index=["survive","death"])
dx.plot(kind="bar");
plt.title("Survive of SibSp ");

# In[358]:

survive=df[df["Survived"]==1]["Pclass"].value_counts()
death=df[df["Survived"]==0]["Pclass"].value_counts()
dx=pd.DataFrame([survive,death],index=["survive","death"])
dx.plot(kind="bar");
plt.title("Survive of Pclass ");

# In[359]:

class1=df[df["Pclass"]==1]["Embarked"].value_counts()
class2=df[df["Pclass"]==2]["Embarked"].value_counts()
class3=df[df["Pclass"]==3]["Embarked"].value_counts()
dx=pd.DataFrame([class1,class2,class3],index=["class 1","class 2","class 3"])
dx.plot(kind="bar",stacked=True);
plt.title("Survive of Pclass ");

# We Found that Embarked from S in 1st & 2nd & 3rd Class

# In[360]:

# Create the pie chart
values=df["Sex"].value_counts()
label=["male","female"]
plt.pie(values, labels=label,autopct='%1.1f%%')
# Add a title
plt.title('Distribution of Survived')
# Display the chart
plt.show()

# In[361]:

survive = df[df["Survived"]==1]["Sex"].value_counts()
death   = df[df["Survived"]==0]["Sex"].value_counts()
dx      = pd.DataFrame([survive,death],index=["survive","death"])
dx=dx.rename(columns={0:"male",1:"female"})
dx.plot(kind="bar")
plt.legend()
plt.title("Survive of Sex");

# In[ ]:

# In[362]:

corrleation = df.drop(columns="Survived").corr()
sns.heatmap(corrleation)

# # Split Data

# In[ ]:

# In[363]:

df

# In[364]:

target="Survived"
y = df[target]
X = df.drop(columns=target)
x_train , x_test , y_train , y_test = train_test_split(X,y,test_size=0.2,random_state=42)
print("X_train shape:", x_train.shape)
print("y_train shape:", y_train.shape)
print("X_test shape:", x_test.shape)
print("y_test shape:", y_test.shape)

# # Baseline

# In[365]:

y_train_mean = y_train.mean()
print ("Baseline :",round(y_train_mean,2))

# # Logestic Regression

# # Itrate

# In[366]:

log_model = LogisticRegression(max_iter=10000)

# In[367]:

log_model.fit(x_train,y_train)

# #

# # Evaluate

# In[368]:

accuracy=classification_report(y_test,log_model.predict(x_test))
print(accuracy)

# In[369]:

acc_test = accuracy_score(y_test,log_model.predict(x_test))
acc_test = accuracy_score(y_test,log_model.predict(x_test))
acc_train= accuracy_score(y_train,log_model.predict(x_train))
print("Accuracy test:",round(acc_test,2))
print("Accuracy train:",round(acc_train,2))

# # KNN Classfier

# In[370]:

knn= KNeighborsClassifier(n_neighbors=13)
knn.fit(x_train,y_train)

# In[371]:

accuracy=classification_report(y_test,knn.predict(x_test))
print(accuracy)

# In[372]:

scoring="accuracy"
score = cross_validate(knn , x_train.drop(columns=["PassengerId"],axis=1),y_train,cv=k_fold, n_jobs=1,scoring=scoring)
print(score['test_score'])

# In[373]:

print("Accuracy :",round(np.mean(score['test_score']),2))

# # Descion Tree

# In[374]:

# Create a decision tree classifier
dec_tree=  DecisionTreeClassifier()
# Train the classifier
dec_tree.fit(x_train, y_train)

# In[375]:

accuracy=classification_report(y_test,dec_tree.predict(x_test))
print(accuracy)

# In[376]:

acc_test = accuracy_score(y_test,dec_tree.predict(x_test))
print("Accuracy test:",round(acc_test,2))

# In[377]:

scoring="accuracy"
score = cross_validate(dec_tree , x_train.drop(columns=["PassengerId"],axis=1),y_train,cv=k_fold, n_jobs=1,scoring=scoring)
print("Accuracy :",round(np.mean(score['test_score']),2))

# # Random Forest

# In[378]:

# Create a Random Forest classifier
rf_classifier = RandomForestClassifier()
# Train the classifier
rf_classifier.fit(x_train, y_train)

# In[379]:

# Calculate the accuracy
accuracy = accuracy_score(y_test, rf_classifier.predict(x_test))
print("Accuracy:", round(accuracy,2))

# In[380]:

scoring="accuracy"
score = cross_validate(rf_classifier , x_train.drop(columns=["PassengerId"],axis=1),y_train, n_jobs=1,scoring=scoring)
print("Accuracy :",round(np.mean(score['test_score']),1))

# # Naive Bayes

# In[381]:

nav= GaussianNB()
# Train the classifier
nav.fit(x_train, y_train)

# In[382]:

# Calculate the accuracy
accuracy = accuracy_score(y_test, nav.predict(x_test))
print("Accuracy:", round(accuracy,2))

# In[383]:

scoring="accuracy"
score = cross_validate(nav , x_train.drop(columns=["PassengerId"],axis=1),y_train, n_jobs=1,scoring=scoring)
print("Accuracy :",round(np.mean(score['test_score']),2))

# # Communicat

# The best model is Random Forest with Accuracy : 82

# In[384]:

pred_test=rf_classifier.predict(test)

data = pd.DataFrame({'PassengerId': test["PassengerId"], 'Survived': pred_test})

# In[385]:

data.head()

# In[386]:

data.to_csv(r'D:\projects\gender_submission.csv', index=False)

# In[ ]:


import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

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

class LocalizationNetwork(nn.Module):
    def __init__(self, numOfControlPoints=10):
        super().__init__()

        self.numOfControlPoints = numOfControlPoints

        self.pool = nn.MaxPool2d(2, 2)
        self.aPool = nn.AdaptiveAvgPool2d(1)

        self.conv1 = nn.Conv2d(3, 64, 3, padding=1)
        self.conv2 = nn.Conv2d(64, 128, 3, padding=1)
        self.conv3 = nn.Conv2d(128, 256, 3, padding=1)
        self.conv4 = nn.Conv2d(256, 512, 3, padding=1)

        self.bn1 = nn.BatchNorm2d(64)
        self.bn2 = nn.BatchNorm2d(128)
        self.bn3 = nn.BatchNorm2d(256)
        self.bn4 = nn.BatchNorm2d(512)

        self.relu = nn.ReLU(inplace=True)
        self.fc1 = nn.Linear(512, 256)

        self.fc2 = nn.Linear(256, numOfControlPoints * 2)

        self.init_stn()

    def forward(self, x):

        x = self.bn1(F.relu(self.conv1(x)))
        x = self.pool(x)
        x = self.bn2(F.relu(self.conv2(x)))
        x = self.pool(x)
        x = self.bn3(F.relu(self.conv3(x)))
        x = self.pool(x)
        x = self.bn4(F.relu(self.conv4(x)))
        x = self.aPool(x)

        x = x.view(x.size()[0], -1)

        x = self.fc1(x)
        x = self.relu(x)

        x = self.fc2(x)

        x = x.view(-1, 2, self.numOfControlPoints)

        return x

    def init_stn(self):

        interval = np.linspace(0.05, 0.95, self.numOfControlPoints // 2)
        controlPoints = [[],[]]

        for y in [0.1,0.9]:
            for i in range(self.numOfControlPoints // 2):
                controlPoints[1].append(y)
            for x in interval:
                controlPoints[0].append(x)

        self.fc2.weight.data.zero_()
        self.fc2.bias.data = torch.Tensor(controlPoints).view(-1).float().to(device)


import pandas as pd
import numpy as np
import pickle as pkl
import matplotlib.pyplot as plt
import re
import jieba
import subprocess
from gensim.test.utils import get_tmpfile, common_texts
from gensim.models import Word2Vec, KeyedVectors
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.manifold import TSNE
from matplotlib.font_manager import FontManager
from pylab import mpl

jieba.load_userdict('C:/Users/choose/venv/Lib/site-packages/jieba/dict.blue.txt')

def load_stopwords():
    with open('util/stopwords.pkl', 'rb') as f:
         stopwords = pkl.load(f)
    return stopwords

def load_symbols():
    ret = []
    with open('util/symbols_20181216.txt', 'r', encoding='utf-8') as f:
        rows = f.readlines()
    f.close()
    for row in rows:
        if row[:-1] not in ret:
            ret.append(row[:-1])
    return ret

def load_pattern():
    symbols = load_symbols()
    symbols += ['\n', '\r\n', '\r']
    symbols_str = ''
    for symbol in symbols:
        if symbol in '[]()-': symbol = '\\' + symbol
        symbols_str += symbol
    return re.compile(r'([0-9]+|\.+|[a-zA-Z])|[{}]+'.format(symbols_str))

def to_sentence(document):
    ret = list()
    rule = re.compile('[\W]+')
    result = rule.split(document)
    for sentence in result:
        if len(sentence) > 0:
            ret.append(sentence)
    return ret

def tokenize(corpus, stopwords=load_stopwords(), pattern=re.compile(r'[\WA-Za-z0-9]+'), length_constraint=2):
    tokenized_corpus = []
    for doc in corpus:
        tokenized_doc = jieba.lcut(doc)
        words = []
        for word in tokenized_doc:
            if word in stopwords or pattern.match(word): continue
            elif len(word) < length_constraint: continue
            else: words.append(word)
        tokenized_corpus.append(words)
    return tokenized_corpus