from cProfile import label
from itertools import count
from unittest import result
import numpy as np
import matplotlib.pyplot as plt
import argparse
from splitted_sbp_dbp_features import getFeatures_SBP_DBP

def softmax(x):
    """
    Compute softmax function for a batch of input values.
    The first dimension of the input corresponds to the batch size. The second dimension
    corresponds to every class in the output. When implementing softmax, you should be careful
    to only sum over the second dimension.

    Important Note: You must be careful to avoid overflow for this function. Functions
    like softmax have a tendency to overflow when very large numbers like e^10000 are computed.
    You will know that your function is overflow resistent when it can handle input like:
    np.array([[10000, 10010, 10]]) without issues.

    Args:
        x: A 2d numpy float array of shape batch_size x number_of_classes

    Returns:
        A 2d numpy float array containing the softmax results of shape batch_size x number_of_classes
    """
    # *** START CODE HERE ***
    result = np.zeros(x.shape)
    for i in range(x.shape[0]):
        row = np.array(x[i])
        diff = np.max(row)
        row = row - diff
        row = np.exp(row)
        s = np.sum(row)
        result[i] = row/s

    # exp_batch = np.exp(x)
    # for i in range(exp_batch.shape[0]):
    #     exp_batch[i] = exp_batch[i]/sum(exp_batch[i])
    return result
    # *** END CODE HERE ***

def sigmoid(x):
    """
    Compute the sigmoid function for the input here.

    Args:
        x: A numpy float array

    Returns:
        A numpy float array containing the sigmoid results
    """
    # *** START CODE HERE ***
    def sigmoid_helper(x):
        if x<0:
            return np.exp(x)/(1+np.exp(x))
        else:
            return 1/(1+np.exp((-1)*x))

    sigmoid_helper_vec = np.vectorize(sigmoid_helper)
    return sigmoid_helper_vec(x)
    # *** END CODE HERE ***

def get_initial_params(input_size, num_hidden, num_output):
    """
    Compute the initial parameters for the neural network.

    This function should return a dictionary mapping parameter names to numpy arrays containing
    the initial values for those parameters.

    There should be four parameters for this model:
    W1 is the weight matrix for the hidden layer of size input_size x num_hidden
    b1 is the bias vector for the hidden layer of size num_hidden
    W2 is the weight matrix for the output layers of size num_hidden x num_output
    b2 is the bias vector for the output layer of size num_output

    As specified in the PDF, weight matrices should be initialized with a random normal distribution
    centered on zero and with scale 1.
    Bias vectors should be initialized with zero.

    Args:
        input_size: The size of the input data
        num_hidden: The number of hidden states
        num_output: The number of output classes

    Returns:
        A dict mapping parameter names to numpy arrays
    """

    # *** START CODE HERE ***
    w1 = np.zeros((input_size, num_hidden))
    for i in range(input_size):
        for j in range(num_hidden):
            w1[i,j] = np.random.normal()
    w2 = np.zeros((num_hidden, num_output))
    for i in range(num_hidden):
        for j in range(num_output):
            w2[i,j] = np.random.normal()
    b1 = np.zeros(num_hidden)
    b2 = np.zeros(num_output)
    return{
        "W1": w1,
        "W2": w2,
        "b1": b1,
        "b2": b2
    }
    # *** END CODE HERE ***

def forward_prop(data, labels, params):
    """
    Implement the forward layer given the data, labels, and params.

    Args:
        data: A numpy array containing the input
        labels: A 2d numpy array containing the labels
        params: A dictionary mapping parameter names to numpy arrays with the parameters.
            This numpy array will contain W1, b1, W2 and b2
            W1 and b1 represent the weights and bias for the hidden layer of the network
            W2 and b2 represent the weights and bias for the output layer of the network

    Returns:
        A 3 element tuple containing:
            1. A numpy array of the activations (after the sigmoid) of the hidden layer
            2. A numpy array The output (after the softmax) of the output layer
            3. The average loss for these data elements
    """
    # *** START CODE HERE ***
    W1T = np.transpose(params["W1"])
    W2T = np.transpose(params["W2"])
    b1 = params["b1"]
    b2 = params["b2"]

    z_1 = np.transpose(np.matmul(W1T, np.transpose(data))) + np.tile(b1, (data.shape[0], 1))
    activations = sigmoid(z_1)

    z = np.transpose(np.matmul(W2T, np.transpose(activations))) + np.tile(b2, (data.shape[0], 1))
    outputs = softmax(z)

    total_loss = 0

    for i in range(labels.shape[0]):
        for j in range(labels.shape[1]):
            if(outputs[i,j]>0):
                total_loss += (labels[i,j]*np.log(outputs[i,j]))

    avg_loss = -1*(total_loss/data.shape[0])
    # activitions = np.full((data.shape[0], b1.shape[0]), 0)
    # outputs = np.full(labels.shape, 0)

    # total_loss = 0

    # for i in range(data.shape[0]):
    #     d = data[i, :]
    #     a = sigmoid(np.matmul(W1T, d)+b1)
    #     z = np.matmul(W2T, a) + b2
    #     print(z.shape)
    #     output = softmax(z)
    #     l = sum(labels[i]*np.log(output))
    #     activitions[i] = a
    #     outputs[i] = output
    #     total_loss += l
    #     break

    # avg_loss = -1*(total_loss/data.shape[0])

    return (activations, outputs, avg_loss)
    # *** END CODE HERE ***

def backward_prop(data, labels, params, forward_prop_func):
    """
    Implement the backward propegation gradient computation step for a neural network

    Args:
        data: A numpy array containing the input
        labels: A 2d numpy array containing the labels
        params: A dictionary mapping parameter names to numpy arrays with the parameters.
            This numpy array will contain W1, b1, W2 and b2
            W1 and b1 represent the weights and bias for the hidden layer of the network
            W2 and b2 represent the weights and bias for the output layer of the network
        forward_prop_func: A function that follows the forward_prop API above

    Returns:
        A dictionary of strings to numpy arrays where each key represents the name of a weight
        and the values represent the gradient of the loss with respect to that weight.

        In particular, it should have 4 elements:
            W1, W2, b1, and b2
    """
    # *** START CODE HERE ***
    W1 = params["W1"]
    W2 = params["W2"]
    b1 = params["b1"]
    b2 = params["b2"]

    (activations, outputs, avg_loss) = forward_prop_func(data, labels, params)

    grad_losses = np.zeros(labels.shape)
    total_grad_loss = np.zeros(labels.shape[1])

    for i in range(labels.shape[0]):
        grad_losses[i]= outputs[i] - labels[i]
        total_grad_loss += outputs[i] - labels[i]
    delta_2 = total_grad_loss/data.shape[0]
    dj_db_2 = delta_2

    # print(grad_losses.shape, activations.shape)
    dj_dw_2 = np.matmul(np.transpose(grad_losses), activations)/data.shape[0]

    sigmoid_prime = np.multiply(activations,(np.ones(activations.shape)-activations))
    delta_1 = np.multiply(np.matmul(grad_losses, np.transpose(W2)), sigmoid_prime)

    dj_dw_1 = np.matmul(np.transpose(delta_1), data)/data.shape[0]
    dj_db_1 = np.sum(delta_1, axis=0)/data.shape[0]

    # print(dj_dw_1.shape, W1.shape)
    dW1 = np.transpose(dj_dw_1)
    # print(dj_db_1.shape, b1.shape)
    dW2 = np.transpose(dj_dw_2)
    # print(dj_dw_2.shape, W2.shape)
    db1 = dj_db_1
    # print(dj_db_2.shape, b2.shape)
    db2 = dj_db_2
    # print(dj_dw_2.shape, dj_db_2.shape, W2.shape, b2.shape)

    return{
        "W1": dW1,
        "W2": dW2,
        "b1": db1,
        "b2": db2
    }
    # *** END CODE HERE ***

def backward_prop_regularized(data, labels, params, forward_prop_func, reg):
    """
    Implement the backward propegation gradient computation step for a neural network

    Args:
        data: A numpy array containing the input
        labels: A 2d numpy array containing the labels
        params: A dictionary mapping parameter names to numpy arrays with the parameters.
            This numpy array will contain W1, b1, W2 and b2
            W1 and b1 represent the weights and bias for the hidden layer of the network
            W2 and b2 represent the weights and bias for the output layer of the network
        forward_prop_func: A function that follows the forward_prop API above
        reg: The regularization strength (lambda)

    Returns:
        A dictionary of strings to numpy arrays where each key represents the name of a weight
        and the values represent the gradient of the loss with respect to that weight.

        In particular, it should have 4 elements:
            W1, W2, b1, and b2
    """
    # *** START CODE HERE ***
    W1 = params["W1"]
    W2 = params["W2"]
    b1 = params["b1"]
    b2 = params["b2"]

    (activations, outputs, avg_loss) = forward_prop_func(data, labels, params)

    grad_losses = np.zeros(labels.shape)
    total_grad_loss = np.zeros(labels.shape[1])

    for i in range(labels.shape[0]):
        grad_losses[i]= outputs[i] - labels[i]
        total_grad_loss += outputs[i] - labels[i]
    delta_2 = total_grad_loss/data.shape[0]
    dj_db_2 = delta_2

    # print(grad_losses.shape, activations.shape)
    dj_dw_2 = np.matmul(np.transpose(grad_losses), activations)/data.shape[0]

    sigmoid_prime = np.multiply(activations,(np.ones(activations.shape)-activations))
    delta_1 = np.multiply(np.matmul(grad_losses, np.transpose(W2)), sigmoid_prime)

    dj_dw_1 = np.matmul(np.transpose(delta_1), data)/data.shape[0]
    dj_db_1 = np.sum(delta_1, axis=0)/data.shape[0]

    # print(dj_dw_1.shape, W1.shape)
    dW1 = np.transpose(dj_dw_1) + 2*reg*W1
    # print(dj_db_1.shape, b1.shape)
    dW2 = np.transpose(dj_dw_2) + 2*reg*W2
    # print(dj_dw_2.shape, W2.shape)
    db1 = dj_db_1
    # print(dj_db_2.shape, b2.shape)
    db2 = dj_db_2
    # print(dj_dw_2.shape, dj_db_2.shape, W2.shape, b2.shape)

    return{
        "W1": dW1,
        "W2": dW2,
        "b1": db1,
        "b2": db2
    }
    # *** END CODE HERE ***

def gradient_descent_epoch(train_data, train_labels, learning_rate, batch_size, params, forward_prop_func, backward_prop_func):
    """
    Perform one epoch of gradient descent on the given training data using the provided learning rate.

    This code should update the parameters stored in params.
    It should not return anything

    Args:
        train_data: A numpy array containing the training data
        train_labels: A numpy array containing the training labels
        learning_rate: The learning rate
        batch_size: The amount of items to process in each batch
        params: A dict of parameter names to parameter values that should be updated.
        forward_prop_func: A function that follows the forward_prop API
        backward_prop_func: A function that follows the backwards_prop API

    Returns: This function returns nothing.
    """

    # *** START CODE HERE ***
    start = 0
    while(start<train_data.shape[0]):
        end = start + batch_size
        derivatives = backward_prop_func(train_data[start:end, :], train_labels[start:end, :], params, forward_prop_func)
        params["W1"] -= learning_rate * derivatives["W1"]
        params["W2"] -= learning_rate * derivatives["W2"]
        params["b1"] -= learning_rate * derivatives["b1"]
        params["b2"] -= learning_rate * derivatives["b2"]
        start = end
    print("epoch")
    # *** END CODE HERE ***

    # This function does not return anything
    return

def nn_train(
    train_data, train_labels, dev_data, dev_labels,
    get_initial_params_func, forward_prop_func, backward_prop_func,
    num_hidden=300, learning_rate=5, num_epochs=30, batch_size=1000):

    (nexp, dim) = train_data.shape

    params = get_initial_params_func(dim, num_hidden, 5)

    cost_train = []
    cost_dev = []
    accuracy_train = []
    accuracy_dev = []
    for epoch in range(num_epochs):
        gradient_descent_epoch(train_data, train_labels,
            learning_rate, batch_size, params, forward_prop_func, backward_prop_func)

        h, output, cost = forward_prop_func(train_data, train_labels, params)
        cost_train.append(cost)
        accuracy_train.append(compute_accuracy(output,train_labels))
        h, output, cost = forward_prop_func(dev_data, dev_labels, params)
        cost_dev.append(cost)
        accuracy_dev.append(compute_accuracy(output, dev_labels))

    return params, cost_train, cost_dev, accuracy_train, accuracy_dev

def nn_test(data, labels, params):
    h, output, cost = forward_prop(data, labels, params)
    accuracy = compute_accuracy(output, labels)
    return accuracy

def compute_accuracy(output, labels):
    accuracy = (np.argmax(output,axis=1) ==
        np.argmax(labels,axis=1)).sum() * 1. / labels.shape[0]
    return accuracy

def one_hot_labels(labels):
    one_hot_labels = np.zeros((labels.size, 5))
    one_hot_labels[np.arange(labels.size),labels.astype(int)] = 1
    return one_hot_labels

def read_data(images_file, labels_file):
    x = np.loadtxt(images_file, delimiter=',')
    y = np.loadtxt(labels_file, delimiter=',')
    return x, y

def run_train_test(name, all_data, all_labels, backward_prop_func, num_epochs, plot=True):
    params, cost_train, cost_dev, accuracy_train, accuracy_dev = nn_train(
        all_data['train'], all_labels['train'],
        all_data['dev'], all_labels['dev'],
        get_initial_params, forward_prop, backward_prop_func,
        num_hidden=300, learning_rate=5, num_epochs=num_epochs, batch_size=1000
    )

    t = np.arange(num_epochs)
    np.save(name+"_params.npy", params) #my code for saving
    if plot:
        fig, (ax1, ax2) = plt.subplots(2, 1)

        ax1.plot(t, cost_train,'r', label='train')
        ax1.plot(t, cost_dev, 'b', label='dev')
        ax1.set_xlabel('epochs')
        ax1.set_ylabel('loss')
        if name == 'baseline':
            ax1.set_title('Without Regularization')
        else:
            ax1.set_title('With Regularization')
        ax1.legend()

        ax2.plot(t, accuracy_train,'r', label='train')
        ax2.plot(t, accuracy_dev, 'b', label='dev')
        ax2.set_xlabel('epochs')
        ax2.set_ylabel('accuracy')
        ax2.legend()

        fig.savefig('./' + name + '.png')

    accuracy = nn_test(all_data['test'], all_labels['test'], params)
    print('For model %s, got accuracy: %f' % (name, accuracy))

    return accuracy

def main(plot=True):
    parser = argparse.ArgumentParser(description='Train a nn model.')
    parser.add_argument('--num_epochs', type=int, default=30)

    args = parser.parse_args()

    data = getFeatures_SBP_DBP()
    features = data["features"]
    labels = data["one-hot-labels"]

    np.random.seed(100)
    train_data, train_labels = features[0:6000,:], labels[0:6000,:]#read_data('./images_train.csv', './labels_train.csv')
    # train_labels = one_hot_labels(train_labels)
    # p = np.random.permutation(60000)
    # train_data = train_data[p,:]
    # train_labels = train_labels[p,:]

    dev_data = train_data[0:2000,:]
    dev_labels = train_labels[0:2000,:]
    train_data = train_data[2000:,:]
    train_labels = train_labels[2000:,:]

    mean = np.mean(train_data)
    std = np.std(train_data)
    train_data = (train_data - mean) / std
    dev_data = (dev_data - mean) / std

    test_data, test_labels = features[6000:6500,:], labels[6000:6500,:]#read_data('./images_test.csv', './labels_test.csv')
    # test_labels = one_hot_labels(test_labels)
    test_data = (test_data - mean) / std

    all_data = {
        'train': train_data,
        'dev': dev_data,
        'test': test_data
    }

    all_labels = {
        'train': train_labels,
        'dev': dev_labels,
        'test': test_labels,
    }

    baseline_acc = run_train_test('baseline', all_data, all_labels, backward_prop, args.num_epochs, plot)
    reg_acc = run_train_test('regularized', all_data, all_labels,
        lambda a, b, c, d: backward_prop_regularized(a, b, c, d, reg=0.0001),
        args.num_epochs, plot)

    return baseline_acc, reg_acc

if __name__ == '__main__':
    main()


import socket
import sampler
import numpy
from multiprocessing import Process, Queue, Manager, Pipe
import time
import loader
from encode import *
import threading
import signal, os, sys
from mylog import *

ADDRESS = ('127.0.0.1', 8712)  # 绑定地址

# 终止进程信号处理
def _signal_handler(signum, frame):
    sys.exit(0)

def init(ip, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((ip, port))
    s.listen(5)
    return s

def message_handle(client, task_queue, task_lock, response_queue):
    while True:
        try:
            size_byte = client.recv(SIZE_CNT)
            if(len(size_byte) == 0):
                break

            size = decode_size(size_byte)

            packet_size = 1024
            task_byte = b''
            while size > packet_size:
                data = client.recv(packet_size)
                task_byte += data
                size -= packet_size
            task_byte += client.recv(size)

            task = decode_data(task_byte)

            name = task[0]

            with task_lock:
                while not response_queue.empty():
                    response_queue.get_nowait()
                task_queue.put(task)
                res_name, resp = response_queue.get(True)
                assert(res_name == name)
            size_byte, resp_byte = encode(resp)
            client.send(size_byte)
            client.send(resp_byte)
        except:
            return

def accept_client(s, task_queue, task_lock, response_queue):
    while True:
        try:
            client, addr = s.accept()
            logging.info("server accept a client: %s", addr)
            thread = threading.Thread(target=message_handle, args=(client, task_queue, task_lock, response_queue))
            thread.start()
        except:
            break

def start_sampler(task_queue, response_queue):
    p = Process(target=sampler.Sampler.sampler, args=(task_queue, response_queue))
    p.start()
    assert(p.is_alive() == True)
    return p

def stop_process(p):
    p.terminate()
    while p.is_alive == True:
        time.sleep(0.1)
    # assert(p.is_alive() != True)
    p.close()

if __name__ == '__main__':
    signal.signal(signal.SIGINT, _signal_handler)
    # start a Sampler process
    task_queue = Queue()
    response_queue = Queue()
    task_lock = threading.Lock()

    sampler = start_sampler(task_queue, response_queue)

    # start server to listen socket
    s = init(ADDRESS[0], ADDRESS[1])
    accept_client(s, task_queue, task_lock, response_queue)

    s.close()
    stop_process(sampler)
    print("---------- exited ----------")


import pickle
import pandas as pd

from sklearn.externals import joblib
from sklearn import preprocessing

clf = joblib.load("train_model.m")

data_test = pd.read_csv("cleaning_test.csv")
df = pd.read_csv("data/test.csv")

f_names = ['OverallQual', 'GrLivArea', 'TotalBsmtSF', 'GarageArea', '1stFlrSF', 'FullBath', 'TotRmsAbvGrd',
           'YearRemodAdd', 'YearBuilt', 'CentralAir', 'Neighborhood', 'RoofMatl', 'HouseStyle', 'KitchenQual',
           'SaleCondition', 'SaleType']

for key in f_names:
    data_test[key].fillna(data_test[key].mode()[0], inplace=True)

# 读取模型参数，对测试进行再编码

x = data_test.values
y_te_pred = clf.predict(x)

y_scaler = joblib.load('scalarY')

prediction = pd.DataFrame(y_te_pred, columns=['SalePrice'])

p = y_scaler.inverse_transform(prediction)

result = pd.concat([df['Id'], pd.DataFrame(p, columns=['SalePrice'])], axis=1)
print(type(p), type(result), type(prediction))

result.to_csv('./Predictions.csv', index=False)


from PIL import Image
import sys, os
from random import randint

print("Welcome to Joseph's Photo Editor!")
photo = input("Please enter the file path of the photo you would like to edit:")
pic = Image.open(photo)
og = Image.open(photo)
while True:
    print("--Main Menu--")
    print("To revert to the original photo at anytime input \'o\'")
    menu = input(
        "Please input the edit you would like to make. Convert file to jpg(j), crop(c), recolour(r), rotate(d), resize(s), pixels(p), or exposure(e).")
    if menu == "j":
        for pic in sys.argv[1:]:
            f, e = os.path.splitext(pic)
            pic = f + ".jpg"
            if pic != pic:
                try:
                    with Image.open(pic) as pic:
                        pic.save(pic)
                except OSError:
                    print("cannot convert", pic)
    elif menu == "c":
        box = input("Input the new coordinates of your photo:(100 100 300 300)")
        box = box.split()
        box = (int(box[0]), int(box[1]), int(box[2]), int(box[3]))
        pic = pic.crop(box)
    elif menu == "r":
        colour = input("Input the new recolour of the photo:(blue, green, red, gray)")
        r, g, b = pic.split()
        if colour == "blue":
            pic = Image.merge("RGB", (b, g, r))
        if colour == "green":
            pic = Image.merge("RGB", (b, r, g))
        if colour == "red":
            pic = Image.merge("RGB", (r, b, g))
        if colour == "gray":
            pic = pic.convert("L")
        #  colour = colour.split()r = int(colour[0])g = int(colour[1])b = int(colour[2])pic.show()
    elif menu == "d":
        degrees = input("Please input how many degrees you would like to rotate your photo counter-clockwise:")
        pic = pic.rotate(int(degrees))
    elif menu == "e":
        high = input("Would you like high exposure or low?(h/l)")
        if high == "h":
            pic = pic.point(lambda i: i * 3)
        if high == "l":
            pic = pic.point(lambda i: i * 0.2)
    elif menu == "s":
        size = input("Input the new size(100 100):")
        size = size.split()
        pic = pic.resize((int(size[0]), int(size[1])))
    elif menu == "p":
        pixel = input("Input the ratio of pixels you want to black(2):")
        double = int(input("Select the number of pixels you want the ratio to make black(5):"))
        pixels = pic.load()
        for i in range(0, pic.size[0], int(pixel)):  # for every pixel:
            for j in range(0, pic.size[1], int(pixel)):
                for k in range(0, double):
                    if i + double > pic.size[0]:
                        continue
                    pixels[i + k, j] = (0, 0, 0)
    elif menu == "o":
        pic = og
    else:
        break
    pic.show()
num = randint(0, 1000)
print("Saved in Temp as edited_image", num, sep="")
pic.save(r"C:\Temp\edited_image" + str(num) + ".jpg")


import PySimpleGUI as sg

class TelaUsuario:
    def __init__(self):
        sg.ChangeLookAndFeel('Material2')
        self.__window = None

    def cadastro_senhas(self):
        layout = [
            [sg.Text("Cadastro de Senhas")],
            [sg.Text("Senha gerente", size=(15, 1)),
             sg.InputText(key='input_senha_gerente')],
            [sg.Text("Senha funcionário", size=(15, 1)),
             sg.InputText(key='input_senha_funcionario')],
            [sg.Button('Confirmar')]
        ]

        self.__window = sg.Window('GoChef').layout(layout)

        while True:
            event, values = self.__window.read()
            if event == sg.WIN_CLOSED:
                exit(0)

            if event == 'Confirmar' and (not values['input_senha_gerente'] or not values['input_senha_funcionario']):
                self.mostra_mensagem('Por favor, digite as senhas!')
            else:
                self.fechar_tela()
                return values

    def atualizacao_senhas(self):
        layout = [
            [sg.Text("Senhas Restaurante")],
            [sg.Text("Tipo usuário"), sg.Combo(['Gerente', 'Funcionário'],
                                               default_value='Gerente', key='tipo_usuario', readonly=True)],
            [sg.Text("Senha atual", size=(15, 1)),
             sg.InputText(key='senha_atual')],
            [sg.Text("Nova senha", size=(15, 1)),
             sg.InputText(key='senha_nova')],
            [sg.Button('Voltar'), sg.Button('Confirmar')]
        ]

        self.__window = sg.Window('GoChef').layout(layout)

        while True:
            event, values = self.__window.read()
            if event == sg.WIN_CLOSED:
                exit(0)

            if event == 'Voltar':
                self.fechar_tela()
                return {
                    'voltar': True
                }

            if event == 'Confirmar' and (not values['senha_atual'] or not values['senha_nova']):
                self.mostra_mensagem('Por favor, digite as senhas!')
            else:
                self.fechar_tela()
                return {
                    'senha_atual': values['senha_atual'],
                    'senha_nova': values['senha_nova'],
                    'gerente': values['tipo_usuario'] == 'Gerente'
                }

    def login(self):
        layout = [
            [sg.Text("Realize seu Login")],
            [sg.Radio('Gerente', 'tipo_usuario', default=True, key='gerente'),
             sg.Radio('Funcionário', 'tipo_usuario', key='funcionario')],
            [sg.Text("Senha", size=(15, 1)), sg.InputText(key='input_senha')],
            [sg.Button('Confirmar')]
        ]

        self.__window = sg.Window('GoChef').layout(layout)

        while True:
            event, values = self.__window.read()
            if event == sg.WIN_CLOSED:
                exit(0)
            if event == 'Confirmar':
                self.fechar_tela()
                return values

    def fechar_tela(self):
        self.__window.Close()

    def mostra_mensagem(self, mensagem: str):
        sg.popup(mensagem)


# scikit-learnを用いてGWASデータのPCAを行う

import numpy as np
import pandas as pd
import time
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA

npz = np.load("SNP_data.npz", allow_pickle=True)
data = pd.DataFrame(npz["arr_0"])
# data

start = time.time()
pca = PCA()  # PCA を行ったり PCA の結果を格納したりするための変数を、pca として宣言
pca.fit(data)  # PCA を実行
end = time.time()

# print(end-start)

loadings = pd.DataFrame(pca.components_.T)
# loadings

score = pd.DataFrame(pca.transform(data))
# score

# 寄与率を算出
contribution_ratios = pd.DataFrame(pca.explained_variance_ratio_)
# contribution_ratios

# 累積寄与率を算出（.cusum()で累積和を計算 .sum()では総和しか得られない）
cumulative_contribution_ratios = contribution_ratios.cumsum()
# cumulative_contribution_ratios

# 以下、グラフの描画をする
from pyplink import PyPlink
pyp = PyPlink('Illu_East-Asian_HapMap3_JPT+CHB_set2') # plink形式ファイルの読み込み
fam = pyp.get_fam()
status = fam["status"] # 地域を取り出す

# 各地域に割り当てる色を決める
region = "Okinawa" # 任意の地域
num = 0
color = []
for i in range(len(status)):
    if region == status[i]:
        color.append(num)
    else:
        region = status[i]
        num += 1
        color.append(num)
        print(region)
# color

score_status = pd.concat([score, status], axis=1)

# 地域ごとにスコアを取り出す
Okinawa = score_status[score_status["status"] == "Okinawa"].drop("status", axis=1)
Miyako = score_status[score_status["status"] == "Miyako"].drop("status", axis=1)
Yaeyama = score_status[score_status["status"] == "Yaeyama"].drop("status", axis=1)
MainlandJapan = score_status[score_status["status"] == "Mainland-Japan"].drop("status", axis=1)
Korea = score_status[score_status["status"] == "Korea"].drop("status", axis=1)
HapMap3_JPT = score_status[score_status["status"] == "HapMap3_JPT"].drop("status", axis=1)
HapMap3_CHB = score_status[score_status["status"] == "HapMap3_CHB"].drop("status", axis=1)

fig = plt.figure()
regions = ["Okinawa", "Miyako", "Yaeyama", "Mainland-Japan", "Korea", "HapMap3_JPT", "HapMap3_CHB"]
score_regions = [Okinawa, Miyako, Yaeyama, MainlandJapan, Korea, HapMap3_JPT, HapMap3_CHB]
for i in range(len(regions)):
    plt.scatter(score_regions[i].iloc[:,0], score_regions[i].iloc[:,1], cmap=plt.get_cmap("jet"), s=10, label=regions[i]) # 第一主成分、第二主成分を用いる
    plt.xlabel('Component1')
    plt.ylabel('Component2')
    plt.legend(loc="upper right")
plt.show()
fig.savefig("skpca1_2.png") # skpca1_2.pngとして画像を保存


import unittest

if __name__ == "__main__":
    import utils
    utils.import_depends()

from brokertest import TestBrokerCommand

class TestRebindMetaCluster(TestBrokerCommand):

    def testfailinvalidcluster(self):
        command = ["update_cluster", "--cluster=cluster-does-not-exist",
                   "--metacluster=utmc1"]
        out = self.notfoundtest(command)
        self.matchoutput(out,
                         "Cluster cluster-does-not-exist not found.",
                         command)

    def testfailinvalidmetacluster(self):
        command = ["update_cluster", "--cluster=utecl1",
                   "--metacluster=metacluster-does-not-exist"]
        out = self.notfoundtest(command)
        self.matchoutput(out,
                         "Metacluster metacluster-does-not-exist not found.",
                         command)

    def testfailfullmetacluster(self):
        command = ["update_cluster", "--cluster=utecl4", "--metacluster=utmc3"]
        out = self.badrequesttest(command)
        self.matchoutput(out, "Metacluster utmc3 has 1 clusters bound, which "
                         "exceeds the requested limit of 0.", command)

    def testfailrebindsandboxcl1(self):
        command = ["update_cluster", "--cluster=sandboxcl1",
                   "--metacluster=utmc1"]
        out = self.badrequesttest(command)
        self.matchoutput(out, "ESX Cluster sandboxcl1 sandbox %s/"
                         "utsandbox does not match ESX metacluster utmc1 "
                         "domain unittest." % self.user, command)

    def testrebindutecl3(self):
        command = ["update_cluster", "--cluster=utecl3", "--metacluster=utmc1"]
        self.noouttest(command)

    def testverifyrebindutecl3(self):
        command = ["cat", "--cluster=utecl3", "--data"]
        out = self.commandtest(command)
        self.matchoutput(out, "structure template clusterdata/utecl3;", command)
        self.matchoutput(out, '"system/cluster/name" = "utecl3";', command)
        self.matchoutput(out, '"system/cluster/metacluster/name" = "utmc1";', command)
        self.matchoutput(out, '"system/metacluster/name" = "utmc1";', command)
        self.matchclean(out, "resources/virtual_machine", command)

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestRebindMetaCluster)
    unittest.TextTestRunner(verbosity=2).run(suite)


import setuptools

with open("package-description.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="jenkins-head-controller",
    version="0.0.0.dev3",
    license='MIT',
    author="Hermann Leinweber",
    author_email="hermann.leinweber@leinies.de",
    description="Deamon to visualize the state of jenkins jobs with a 'jenkins head' (BLE controlled lights)",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/EugenWiens/jenkins-head",
    packages=setuptools.find_packages(where='src'),
    package_dir={'': 'src'},
    classifiers=[
        'Development Status :: 3 - Alpha',
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
    ],
    python_requires='>=3.6',
    entry_points={
        'console_scripts': [
            'jenkins-head-controller=jenkins_head_controller:main'
        ]
    },
    install_requires=[
        'pyyaml',
        'python-jenkins',
        'pygatt',
        'pexpect'
    ],
)


import os, sys

# ensures my project is in my path

path1 = '/home/delane/UW/Python/UWPython/Python2'
path2 = '/home/delane/UW/Python/UWPython/Python2/apitest'
sys.path.insert(0, path1)
sys.path.insert(1, path2)
os.environ['DJANGO_SETTINGS_MODULE'] = 'apitest.settings'

# This has to be done here so that django will be in a directory that's in the python path
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()


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

# In[ ]:

from selenium import webdriver
from tqdm import tqdm_notebook
driver = webdriver.Firefox()

driver.get("https://web.whatsapp.com/")

name = input("Enter the name of the chat or the group: ")
msg = input("Enter the message: ")
count = int(input("Enter the number of times: "))

a=input("Press enter after scanning")

user = driver.find_element_by_xpath('//span[@title="{}"]'.format(name))
user.click()

msg_box = driver.find_element_by_xpath('/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div[2]/div/div[2]')

for i in tqdm_notebook(range(count)):
    msg_box.send_keys(msg)
    button = driver.find_element_by_xpath('/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div[3]/button')
    button.click()

print("Done")


import os
import torch
import logging
import argparse

import numpy as np

from torch.distributions.categorical import Categorical

from torch.utils.data.dataset import Dataset
from torch.utils.data import DataLoader
from machine.util.agent import ModelAgent

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
BATCH_SIZE = 64

class DiagnosticDataset(Dataset):
    """
    Dataset used in training the diagnostic classifier.
    """

    def __init__(self, dirname):
        files = [f for f in os.listdir(
            dirname) if os.path.isfile(os.path.join(dirname, f))]
        self.embeddings = []
        self.labels = []
        for file in files:
            data = np.load(os.path.join(dirname, file))
            self.embeddings.extend(data[:, 0])
            self.labels.extend(data[:, 1])

    def __getitem__(self, index):
        X = torch.Tensor(self.embeddings[index])
        y = torch.Tensor([self.labels[index]])
        return X, y

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

def validation_eval(val_set, model, do_entropy=False, limit=-1):
    """
    Perform evaluation on :val_set for :limit batches with :model
    """
    val_loader = DataLoader(val_set, batch_size=BATCH_SIZE,
                            shuffle=True, num_workers=0)
    correct = 0
    ents = []
    for n, batch in enumerate(val_loader):
        if limit > 0 and n > limit:
            break
        data_in = batch[0].to(device)
        label = batch[1].type(torch.long).to(device)
        h = model(data_in)
        if do_entropy:
            x = torch.distributions.Categorical(h).entropy()
            ents.append(x.mean().item())
        _, output = h.max(1)
        local_acc = sum(label.flatten() == output)
        correct += local_acc.item()
    if limit > 0:
        div = limit * BATCH_SIZE
    else:
        div = len(val_set)

    if do_entropy:
        return correct / div, np.average(ents)
    else:
        return correct / div, np.nan

def stop_on_accs(n, validation_accuracy, repeat_stop=5, print_every=300):
    """
    Check whether we had the same validation accuracy for :repeat_stop prints,
    return a signal we should stop if that is the case.
    """
    if (n // print_every) > repeat_stop:
        accs = np.array(validation_accuracy[-repeat_stop:])
        red = accs - accs[-1]
        trth = np.isclose(red, np.zeros(red.shape))
        return trth.all()

def main(args):
    # Create dataset
    dataset = DiagnosticDataset(args.data_dir)
    # Split data
    split = int(np.floor(0.2 * len(dataset)))
    train_set, val_set = torch.utils.data.random_split(
        dataset, [len(dataset) - split, split])
    train_loader = DataLoader(train_set, batch_size=BATCH_SIZE,
                              shuffle=True, num_workers=0)

    logging.info(f"Initialized dataset: {len(train_loader)} batches")

    model = torch.nn.Sequential(
        torch.nn.Linear(128, args.n_targets),
        # torch.nn.ReLU(),
        # torch.nn.Linear(32, 18),
    ).to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
    criterion = torch.nn.CrossEntropyLoss()

    val_accs = []
    t = 0
    stop = False

    for i in range(args.epochs):
        if stop is True:
            break
        logging.info(f"Start of epoch: {i}")
        for n, batch in enumerate(train_loader):
            t += 1
            data_in = batch[0].to(device)
            label = batch[1].type(torch.long).to(device)
            optimizer.zero_grad()
            output = model(data_in)
            loss = criterion(output, label.flatten())
            loss.backward()
            optimizer.step()
            if n % args.print_every == 0:
                val_acc, val_ent = validation_eval(
                    val_set, model, args.show_entropy)
                train_acc, train_ent = validation_eval(
                    train_set, model, args.show_entropy, limit=-1)
                val_accs.append(val_acc)

                if args.show_entropy:
                    logging.info(
                        f"Epoch: {i:2} - Step {n:5} - Loss: {loss:1.3f} - TAcc: {train_acc:1.5f}({train_ent:1.5f}) - VAcc: {val_acc:1.5f} ({val_ent:1.5f})")
                else:
                    logging.info(
                        f"Epoch: {i:2} - Step {n:5} - Loss: {loss:1.3f} - TAcc: {train_acc:1.5f} - VAcc: {val_acc:1.5f}")

                if stop_on_accs(t, val_accs, args.repeat_stop, args.print_every):
                    logging.warning("Stopping after stuck validation accuracy")
                    stop = True
                    break

    if args.machine:
        print(f"{args.outfile.replace('.pt','')},{train_acc:1.5f},{val_acc:1.5f}")
    else:
        print(
            f"Saving trained model to {args.outfile} with T:{train_acc:1.5f} V:{val_acc:1.5f}")
    torch.save(model.state_dict(), args.outfile)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--data_dir", default="data/reason_dataset/", type=str,
                        help="Directory pointing to training data (REQUIRED)")
    parser.add_argument("--n_targets", default=18, type=int,
                        help="number of targets for diagnostic classifier")
    parser.add_argument("--epochs", default=50, type=int,
                        help="How many epochs to train")
    parser.add_argument("--lr", default=0.001, type=float,
                        help="Learning rate for Adam optimizer")
    parser.add_argument("--show_entropy", default=False, action='store_true',
                        help="Show entropy of prediction tensor")
    parser.add_argument("--seed", default=1, type=int,
                        help="Seeding")
    parser.add_argument("--repeat_stop", default=10, type=int,
                        help="How many steps to check for validation accuracy stuck")
    parser.add_argument("--print_every", default=100, type=int,
                        help="Print every X batches")
    parser.add_argument("--outfile", default='trained_diag.pt', type=str,
                        help="Trained network output name")
    parser.add_argument("--machine", default=False, action='store_true',
                        help="no print, machine output of stats")

    args = parser.parse_args()

    torch.manual_seed(args.seed)
    np.random.seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)

    LOG_FORMAT = '%(asctime)s %(name)-6s %(levelname)-6s %(message)s'
    logging.basicConfig(format=LOG_FORMAT,
                        filename='train_diag.log',
                        level=getattr(logging, 'info'.upper()))
    if args.machine:
        logging.disable(logging.CRITICAL)
    config = vars(args)
    logging.info("Parameters:")
    for k, v in config.items():
        logging.info(f"  {k:>21} : {v}")

    main(args)


import pygame
import neat
import os
from Bird import Bird
from theBase import theBase
from greenPipe import greenPipe
pygame.font.init()

Window_Width = 570
Window_Height = 770
Gen=0

# makes img 2 times bigger and reads it
BIRD_IMAGES = [pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird1.png"))),
               pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird2.png"))),
               pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird3.png")))]
PIPE_IMAGE = pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "pipe.png")))
BASE_IMAGE = pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "base.png")))
BACKGROUND_IMAGE = pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bg.png")))

FONT = pygame.font.SysFont("comicsans", 45)

def draw_window(win, multiple_birds, pipes, base, score, gen):
    if gen == 0:
        gen = 1
    win.blit(BACKGROUND_IMAGE, (0, 0))

    for pipe in pipes:
        pipe.draw(win)

    text = FONT.render("Sc0re: " + str(score), 1, (0, 0, 0))
    win.blit(text, (Window_Width - 10 - text.get_width(), 10))

    text = FONT.render("Gen: " + str(gen -1), 1, (0, 0, 0))
    win.blit(text, (10, 10))

    text = FONT.render("Alive: " + str(len(multiple_birds)), 1, (0, 0, 0))
    win.blit(text, (10, 50))

    base.draw(win)
    for bird in multiple_birds:
        bird.draw(win)

    pygame.display.update()

def evalute_genomes(genomes, config):
    """
    runs the game with fixed population of flapy birds and change fitness based on where they reached in the game
    """
    global Gen

    # making a list of neural networks of the genomes,list that have genomes and the objects of the birds that use this
    # network to play
    networks = []
    genomes_list = []
    multiple_birds = []
    Gen += 1

    for _, genome in genomes:
        network = neat.nn.FeedForwardNetwork.create(genome, config)
        networks.append(network)
        multiple_birds.append(Bird(210, 230, BIRD_IMAGES))
        # start with 0 fitness
        genome.fitness = 0
        genomes_list.append(genome)

    base = theBase(700, BASE_IMAGE)
    pipes = [greenPipe(570, PIPE_IMAGE)]
    win = pygame.display.set_mode((Window_Width, Window_Height))
    clock = pygame.time.Clock()
    score = 0
    run = True
    while run:
        clock.tick(25)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                quit()

        pipe_index = 0
        if len(multiple_birds) > 0:
            if len(pipes) > 1 and multiple_birds[0].x > pipes[0].x + pipes[0].PIPE_TOP.get_width():
                pipe_index = 1
        else:
            run = False
            break

        for x, bird in enumerate(multiple_birds):
            # if bird survived we need to increase its fitness because its doing well
            genomes_list[x].fitness += 0.08
            bird.move()
            network_output = networks[x].activate((bird.y, abs(bird.y - pipes[pipe_index].height), abs(bird.y - pipes[pipe_index].bottom)))

            if network_output[0] > 0.5:
                bird.jump()

        add_pipe = False
        rem = []
        # creating pipes,when pipe reach certain point we need to create another pipe
        for pipe in pipes:
            for x, bird in enumerate(multiple_birds):
                if pipe.collide(bird):
                    genomes_list[x].fitness -= 1
                    multiple_birds.pop(x)
                    networks.pop(x)
                    genomes_list.pop(x)

                if not pipe.passed and pipe.x < bird.x:
                    pipe.passed = True
                    add_pipe = True

            if pipe.x + pipe.PIPE_TOP.get_width() <0:
                rem.append(pipe)
            pipe.move()

        if add_pipe:
            score += 1
            for genome in genomes_list:
                genome.fitness += 5
            pipes.append(greenPipe(570, PIPE_IMAGE))

        for r in rem:
            pipes.remove(r)

        for x, bird in enumerate(multiple_birds):
            if bird.y + bird.img.get_height() >= 700 or bird.y < 0:
                multiple_birds.pop(x)
                networks.pop(x)
                genomes_list.pop(x)

        base.move()
        draw_window(win, multiple_birds, pipes, base, score ,Gen)

def run(config_path):
    """
      runs the NEAT algorithm to train a neural network.
      :param config_file: location of config file
      :return: None
      """
    config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path)
    population = neat.Population(config)
    population.add_reporter(neat.StdOutReporter(True))
    statistics = neat.StatisticsReporter()
    population.add_reporter(statistics)
    # calls main function 50 times and generates the game with all genomes
    winner_bird = population.run(evalute_genomes, 50)

if __name__ == "__main__":
    local_directory = os.path.dirname(__file__)
    config_path = os.path.join(local_directory, "Configuration.txt")
    run(config_path)


import django.http

def django_setitem():
    rfs_header = django.http.request.GET.get("rfs_header")
    response = django.http.HttpResponse()
    response.__setitem__('HeaderName', rfs_header)
    return response

def django_response():
    rfs_header = django.http.request.GET.get("rfs_header")
    response = django.http.HttpResponse()
    response['HeaderName'] = rfs_header
    return response


from src.generators.timetable_gen import TimeTablePermuteGenerator
from data import default_timetable
from src.timetable_module.days import Day
from itertools import permutations
import time

if __name__ == "__main__":
    default_timetable = default_timetable.get_timetable()

    monday = default_timetable[Day.MONDAY]
    array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    start = time.time()
    per_list = list(permutations(array))
    end = time.time()
    print("Permutace obyc listu: ", end - start)

    start = time.time()
    per_monday = list(permutations(monday))
    end = time.time()
    print("Permutace dataclass: ", end - start)


import requests
import pprint
import json
# from bog import register
# namee = register().split()[0]
# bearer_token = 'AAAAAAAAAAAAAAAAAAAAAH7tMwEAAAAASSQEjZvKGl4bLt8CWxRd3BEHBzg%3DEsUX6N1SBtdBuZMkbKqQiMBGjW3yL5dGVZTjNXl3XVP1l06Qsg'

def generate_1(bearer_token, namee):

    base_url = 'https://api.twitter.com/'

    search_url = '{}1.1/friends/list.json'.format(base_url) #endpoint
    search_headers = {
        'Authorization': 'Bearer {}'.format(bearer_token)
    }
    search_params = {
        'screen_name': f'@{namee}',
        'count': 100
    }
    response = requests.get(search_url, headers=search_headers, params=search_params)

    json_response = response.json()
    return json_response

# pprint.pprint(json_response)

def write_to_file(bearer_token, namee):
    """
    To write to file in process
    """
    what_to_write = generate_1(bearer_token, namee)
    with open("friends_here.json", "w") as file_1:
        return json.dump(what_to_write, file_1, ensure_ascii=False, indent=4)
# print(write_to_file())


from utils import gensym, is_list, match
from phase1_scheme import stringify

def Tc(expr, c):
    if is_atomic(expr):
        return (c, M(expr))
    elif expr[0] == 'begin' and len(expr) == 2:
        return Tc(expr[1], c)
    elif expr[0] == 'begin':
        return Tk(expr[1], lambda _: Tc(('begin',) + expr[2:], c))
    elif expr[0] == 'if':
        k = gensym('k', namespace='cps')
        return (('lambda', (k,), Tk(expr[1], lambda aexp: ('if', aexp,
                                                           Tc(expr[2], k),
                                                           Tc(expr[3], k)))),
                c)
    elif expr[0] == 'set!':
        return Tk(expr[2],
                  lambda aexp: ('set-then!', expr[1], aexp, (c, 'undef')))
    # WARNING: This transformation is not hygienic if the continuation c
    #          references any of the bound variables!
    # Use this transformation only on alphatized programs!
    elif expr[0] == 'letrec':
        return (
         'letrec', tuple((v, M(a)) for v, a in expr[1]), Tc(expr[2], c))
    elif is_primitive(expr[0]):
        return Tsk(expr[1:], lambda es_: (('cps', expr[0])) + es_ + (c,))
    else:
        f, es = expr[0], expr[1:]
        return Tk(f, lambda f_: Tsk(es, lambda es_: (f_,) + es_ + (c,)))

def Tk(expr, k):
    if not is_list(expr):
        return k(M(expr))
    elif expr[0] == 'lambda':
        return k(M(expr))

    elif expr[0] == 'if':
        rv = gensym('rv', namespace='cps')
        cont = ('lambda', (rv,), k(rv))
        k = gensym('k', namespace='cps')
        return (('lambda', (k,), Tk(expr[1], lambda aexp: ('if', aexp,
                                                           Tc(expr[2], k),
                                                           Tc(expr[3], k)))),
                cont)
    # WARNING: This transformation is not hygienic if the continuation c
    #          references any of the bound variables!
    # Use this transformation only on alphatized programs!
    elif expr[0] == 'letrec':
        rv = gensym('rv', namespace='cps')
        cont = ('lambda', (rv,), k(rv))
        return (
            'letrec', tuple((v, M(a)) for v, a in expr[1]), Tc(expr[2], cont))
    elif is_primitive(expr[0]):
        rv = gensym('rv', namespace='cps')
        cont = ('lambda', (rv,), k(rv))
        return Tsk(expr[1:], lambda es_: (('cps', expr[0])) + es_ + (cont,))
    else:
        f, es = expr[0], expr[1:]
        rv = gensym('rv', namespace='cps')
        cont = ('lambda', (rv,), k(rv))
        return Tk(f, lambda f_: Tsk(es, lambda es_: (f_,) + es_+ (cont,)))

def Tsk(exprs, k):
    if len(exprs) == 0:
        return k(())
    return Tk(exprs[0], lambda hd: Tsk(exprs[1:], lambda tl: k((hd,) + tl)))

def M(expr):
    lam = match(expr, ('lambda', ('..args',), '..body'))
    if lam is not None:
        k = gensym('k', namespace='cps')
        return ['lambda', lam['args'] + (k,), Tc(('begin',) + lam['body'], k)]
    else:
        # TODO: distinguish non-atomic expressions and raise an error
        return expr

def is_atomic(expr):
    return (not is_list(expr)) or expr[0] == 'lambda'

def is_primitive(expr):
    return expr in ['+', '-', '*', '/']

if __name__ == '__main__':
    print(Tc(('g', 'a'), 'halt'))
    print(stringify(Tc(('begin', ('print', '"A"'), ('print', '"B"'), ('print', '"C"')), 'halt')))
    print(stringify(M(('lambda', ('x',), ('begin', ('print', '"A"'), ('print', '"B"'), ('print', '"C"'))))))
    print(stringify(Tc((('lambda', ('x', 'y'), ('print', 'x'), ('print', 'y')), 5, 3), 'print')))
    print(stringify(Tc((('lambda', ('x', 'y'), ('sqrt', ('+', ('*', 'x', 'x'), ('*', 'y', 'y')))), 2, 3), 'print')))
    print(stringify(Tc(('letrec', (('a', 42), ('b', ('+', 1, 2))), ('+', 'a', 'b')), 'print')))

    # This should print the value of x passed to the lambda and 3
    # but after the transformation it will print 3 3
    # because letrec rebinds x and the print is moved into the continuation of letrec.
    # This demonstrates the unhygienic behavior of the letrec transform
    print(stringify(M(('lambda', ('x',), ('print', 'x', ('letrec', (('x', 3),), 'x'))))))

    print(stringify(M(('lambda', ('x',), ('if', ('even?', 'x'), ('print', '"even"'), ('print', '"odd"'))))))
    print(stringify(M(('lambda', ('x',), ('print', ('if', ('even?', 'x'), '"even"', '"odd"'))))))


from datetime import datetime as dt
from time import time

fio = None
number_phone = None

def add_model_phonebook(user_data):
    global fio
    global number_phone
    fio = user_data[0]
    number_phone = user_data[1]
    log_data_user(fio, number_phone)

def search_phone_in_phonebook(user_data_search):
    all_data_user = give_the_data_user(user_data_search)
    if all_data_user.find(user_data_search) == -1:
        return 'No person found with these parametrs.'
    else:
        all_data_user = all_data_user.split(f'\n')
        answer = -1
        answer_new = []
        for i in range(0, len(all_data_user)):
            answer = all_data_user[i].find(user_data_search)
            if answer != -1:
                answer_new.append(all_data_user[i])
        return answer_new

def delete_user(user):
    all_data_user = give_the_data_user(user)
    if all_data_user.find(user) == -1:
        return 'No person found with these parametrs.'
    else:
        all_data_user = all_data_user.split(f'\n')
        answer = -1
        answer_new = []
        for i in range(0, len(all_data_user)):
            answer = all_data_user[i].find(user)
            if answer != -1:
                answer_new.append(all_data_user[i])
        if len(answer_new) != 1:
            return 'To delete, enter more complete data or phone number!'
        else:
            log_delete_user(user)
            return 'Removal was successful!'

def get_view_model():
    fio = input('Enter FIO: ')
    while True:
        number_phone = input('Enter number phone: ')
        if number_phone.isdigit():
            break
    return fio, number_phone

def get_user_search():
    data_user = input('Enter FIO or numberphone: ')
    return data_user

def get_user_delete():
    delete_user = input('Enter who you want to delete: ')
    print('Are you sure you want to delete?')
    return delete_user

def Button_click():
    user_data = get_view_model()
    add_model_phonebook(user_data)

def Button_click_search():
    user_data_search = get_user_search()
    all_user_data = search_phone_in_phonebook(user_data_search)
    return all_user_data

def Button_click_delete():
    user_data_search = get_user_delete()
    answer = delete_user(user_data_search)
    return answer

def log_data_user(fio, phone_number):
    file_log_user_data = 'phonebook.txt'
    time = dt.now().strftime("%d/%m/%Y Time %H:%M")
    with open(file_log_user_data, 'a') as file:
        file.write(f'{time} \nFIO: {fio} - NumberPhone: {phone_number}\n')

def give_the_data_user(user_data_search):
    file_log_user_data = 'phonebook.txt'
    with open(file_log_user_data, 'r') as file:
        all_data_user = file.read()
        return all_data_user

def log_delete_user(user):
    file_log_user_data = 'phonebook.txt'
    with open(file_log_user_data, 'r') as file:
        all_data_user = file.read().split(f'\n')
        wh = len(all_data_user)
        for i in range(0, wh):
            ind = all_data_user[i].find(user)
            if ind != -1:
                tmp = i
                break
        all_data_user.pop(tmp)
        all_data_user.pop(tmp - 1)
        for i in range(0, len(all_data_user)):
            if len(all_data_user[i]) == 0:
                all_data_user.pop(i)
                break
        with open(file_log_user_data, 'w') as file_new:
            for i in range(0, len(all_data_user)):
                file_new.write(f'{all_data_user[i]}\n')

while True:
    result = int(input('\n\nВведите число для выбора действия\n 0 - выход, 1 - добавление контакта,'
                       ' 2 - поиск контакта, 3 - удаление контакта\n'))
    if result == 0:
        print('Программа завершена')
        break
    elif result == 1:
        Button_click()
        print('\nКонтакт добавлен\n')
    elif result == 2:
        all_user_data = Button_click_search()
        if all_user_data == 'No person found with these parametrs.':
            print(all_user_data)
        else:
            print('\nКонтакт найден\n')
            for i in range(0, len(all_user_data)):
                print(all_user_data[i])
    elif result == 3:
        answer = Button_click_delete()
        print(answer)
        print('\nКонтакт удалён\n')
    else:
        print('Некорректный ввод. Повторите ещё раз')


#!/usr/bin/env python3

import xml.etree.ElementTree as ET

def get_target():
	return SVG()

class SVG:
	def __init__(self):
		self.svg = ET.parse('skeleton.svg')
		self.mmpx = 3.543307

	def output(self, path):
		self.svg.write(path)

	def add_package(self, package):
		'''
		Target SVG only handles one drawing at a time, only last added drawing will be part of output
		'''
		self.svg = ET.parse('skeleton.svg')

		self.package = \
			{
				'name': package['name'],
				'pads': [],
				'mnt_pads': [],
				'holes': [],
				'lines': [],
				'circles': [],
				'rectangles': [] ,
				'texts': []
			}

	def output(self, fout):

		package = self.package

		for pad in package['pads']:
			self.gen_pac_pad(pad)

		for mnt_pad in package['mnt_pads']: # TODO, adding mnt_pads not done
			self.gen_pac_mnt_pad(mnt_pad)

		for hole in package['holes']:
			self.gen_pac_hole(hole)

		for line in package['lines']:
			self.gen_pac_line(line)

		if(0):

			for circle in package['circles']:
				self.gen_pac_circle(circle)

			for rect in package['rectangles']:
				self.gen_pac_rectangle(rect)

			for text in package['texts']:
				self.gen_pac_text(text)

		self.svg.write(fout)

	def add_pac_pad(self, type, angle, size, pos, number):

		self.package['pads'].append(
			{
				'type': type,
				'angle': angle,
				'size': size,
				'pos': pos,
				'number': number
			})

	def add_pac_hole(self, diameter, pos):

		self.package['holes'].append(
			{
				'd': diameter,
				'pos': pos
			})

	def add_pac_line(self, layer, width, vertices):

		self.package['lines'].append(
			{
				'layer': layer,
				'width': width,
				'vertices': vertices
			})

	def gen_pac_pad(self, pad): # type, angle, size, pos, number

		top_layer = self.svg.find('.//g[@id="Top"]')

		# TODO: Types and angle

		el = ET.SubElement(top_layer, 'rect')
		el.set('style', 'fill:#ff0000;fill-opacity:1;stroke:none;stroke-width:10;stroke-linecap:square;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1')
		el.set('id', 'pin_{}'.format(pad['number']))
		el.set('width', '{}'.format(pad['size'][0]*self.mmpx))
		el.set('height', '{}'.format(pad['size'][1]*self.mmpx))
		el.set('x', '{}'.format((pad['pos'][0] - pad['size'][0]/2)*self.mmpx))
		el.set('y', '{}'.format((pad['pos'][1] - pad['size'][1]/2)*self.mmpx))

	def gen_pac_hole(self, hole):

		top_layer = self.svg.find('.//g[@id="Holes"]')

		circle = ET.SubElement(top_layer, 'circle')

		circle.set('style', 'fill:#eeee00;fill-opacity:1;stroke:none;stroke-width:0.0;stroke-linecap:square;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"')

		circle.set('cx', '{}'.format(hole['pos'][0]*self.mmpx))
		circle.set('cy', '{}'.format(hole['pos'][1]*self.mmpx))
		circle.set('r', '{}'.format(hole['d']/2*self.mmpx))

	def gen_pac_line(self, line):

		layer = self.svg.find('.//g[@id="{}"]'.format(line['layer']))

		if(line['layer'] == 'Courtyard'):
			color = '#e63a81'
		elif(line['layer'] == 'Silk'):
			color = '#111111'
		else:
			color = '#000000'

		el = ET.SubElement(layer, 'path')
		el.set('style', 'fill:none;fill-rule:evenodd;stroke:{color};stroke-width:{}mm;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;stroke-miterlimit:4;stroke-dasharray:none'.format(line['width'], color=color))

		pathdata = ''
		first = True

		for (x,y) in line['vertices']:
			if(first):
				pathdata += 'M ' + '{},{}'.format(x*self.mmpx,y*self.mmpx)
				first = False
			elif(x == 'end'):
				pathdata += ' z'
			else:
				pathdata += ' L ' + '{},{}'.format(x*self.mmpx,y*self.mmpx)

		el.set('d', pathdata)

	def gen_circle(self, layer_name, diameter, pos):

		layer = self.svg.find('.//g[@id="{}"]'.format(layer_name))

		if(layer_name == 'Courtyard'):
			color = '#e63a81'
		elif(layer_name == 'Silk'):
			color = '#111111'
		else:
			color = '#000000'

		circle = ET.SubElement(layer, 'circle')
		circle.set('style', 'fill:#{color};fill-opacity:1;stroke:none;stroke-width:0.0;stroke-linecap:square;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"'.format(color=color))

		circle.set('cx', '{}'.format(pos[0]*self.mmpx))
		circle.set('cy', '{}'.format(pos[1]*self.mmpx))
		circle.set('r', '{}'.format(diameter/2*self.mmpx))

if(__name__ == '__main__'):

	target = get_target()

	target.output('test.svg')


"""
Contain pivoting routines commonly used in the Simplex Algorithm and
Lemke-Howson Algorithm routines.

"""
import numpy as np
from numba import jit

TOL_PIV = 1e-10
TOL_RATIO_DIFF = 1e-15

@jit(nopython=True, cache=True)
def _pivoting(tableau, pivot_col, pivot_row):
    """
    Perform a pivoting step. Modify `tableau` in place.

    Parameters
    ----------
    tableau : ndarray(float, ndim=2)
        Array containing the tableau.

    pivot_col : scalar(int)
        Pivot column index.

    pivot_row : scalar(int)
        Pivot row index.

    Returns
    -------
    tableau : ndarray(float, ndim=2)
        View to `tableau`.

    """
    nrows, ncols = tableau.shape

    pivot_elt = tableau[pivot_row, pivot_col]
    for j in range(ncols):
        tableau[pivot_row, j] /= pivot_elt

    for i in range(nrows):
        if i == pivot_row:
            continue
        multiplier = tableau[i, pivot_col]
        if multiplier == 0:
            continue
        for j in range(ncols):
            tableau[i, j] -= tableau[pivot_row, j] * multiplier

    return tableau

@jit(nopython=True, cache=True)
def _min_ratio_test_no_tie_breaking(tableau, pivot, test_col,
                                    argmins, num_candidates,
                                    tol_piv, tol_ratio_diff):
    """
    Perform the minimum ratio test, without tie breaking, for the
    candidate rows in `argmins[:num_candidates]`. Return the number
    `num_argmins` of the rows minimizing the ratio and store thier
    indices in `argmins[:num_argmins]`.

    Parameters
    ----------
    tableau : ndarray(float, ndim=2)
        Array containing the tableau.

    pivot : scalar(int)
        Pivot.

    test_col : scalar(int)
        Index of the column used in the test.

    argmins : ndarray(int, ndim=1)
        Array containing the indices of the candidate rows. Modified in
        place to store the indices of minimizing rows.

    num_candidates : scalar(int)
        Number of candidate rows in `argmins`.

    tol_piv : scalar(float)
        Pivot tolerance below which a number is considered to be
        nonpositive.

    tol_ratio_diff : scalar(float)
        Tolerance to determine a tie between ratio values.

    Returns
    -------
    num_argmins : scalar(int)
        Number of minimizing rows.

    """
    ratio_min = np.inf
    num_argmins = 0

    for k in range(num_candidates):
        i = argmins[k]
        if tableau[i, pivot] <= tol_piv:  # Treated as nonpositive
            continue
        ratio = tableau[i, test_col] / tableau[i, pivot]
        if ratio > ratio_min + tol_ratio_diff:  # Ratio large for i
            continue
        elif ratio < ratio_min - tol_ratio_diff:  # Ratio smaller for i
            ratio_min = ratio
            num_argmins = 1
        else:  # Ratio equal
            num_argmins += 1
        argmins[num_argmins-1] = i

    return num_argmins

@jit(nopython=True, cache=True)
def _lex_min_ratio_test(tableau, pivot, slack_start, argmins,
                        tol_piv=TOL_PIV, tol_ratio_diff=TOL_RATIO_DIFF):
    """
    Perform the lexico-minimum ratio test.

    Parameters
    ----------
    tableau : ndarray(float, ndim=2)
        Array containing the tableau.

    pivot : scalar(int)
        Pivot.

    slack_start : scalar(int)
        First index for the slack variables.

    argmins : ndarray(int, ndim=1)
        Empty array used to store the row indices. Its length must be no
        smaller than the number of the rows of `tableau`.

    tol_piv : scalar(float), optional
        Pivot tolerance below which a number is considered to be
        nonpositive. Default value is {TOL_PIV}.

    tol_ratio_diff : scalar(float), optional
        Tolerance to determine a tie between ratio values. Default value
        is {TOL_RATIO_DIFF}.

    Returns
    -------
    found : bool
        False if there is no positive entry in the pivot column.

    row_min : scalar(int)
        Index of the row with the lexico-minimum ratio.

    """
    nrows = tableau.shape[0]
    num_candidates = nrows

    found = False

    # Initialize `argmins`
    for i in range(nrows):
        argmins[i] = i

    num_argmins = _min_ratio_test_no_tie_breaking(
        tableau, pivot, -1, argmins, num_candidates, tol_piv, tol_ratio_diff
    )
    if num_argmins == 1:
        found = True
    elif num_argmins >= 2:
        for j in range(slack_start, slack_start+nrows):
            if j == pivot:
                continue
            num_argmins = _min_ratio_test_no_tie_breaking(
                tableau, pivot, j, argmins, num_argmins,
                tol_piv, tol_ratio_diff
            )
            if num_argmins == 1:
                found = True
                break
    return found, argmins[0]

_lex_min_ratio_test.__doc__ = _lex_min_ratio_test.__doc__.format(
    TOL_PIV=TOL_PIV, TOL_RATIO_DIFF=TOL_RATIO_DIFF
)


from menubutton import MenuButton
from typing import List, Callable
from abc import ABC, abstractmethod
from gameobject import GameObject, GameObjectLoader
from snake import Snake
from fruit import Fruit

class GameState(ABC):
    gameObjects: List[GameObject]

    def __init__(self) -> None:
        super().__init__()
        self.gameObjects = list[GameObject]()

    @abstractmethod
    def onEnter(self):
        pass

    @abstractmethod
    def onExit(self):
        pass

    @abstractmethod
    def update(self):
        pass

class MenuState(GameState):
    callbacks: List[Callable]

    def addCallback(self, cb:Callable):
        self.callbacks.append(cb)

    def executeCallback(self, index:int):
        self.callbacks[index]()

class MainMenuState(MenuState):
    def onEnter(self):
        playButton = MenuButton("Play", 0, 0, 0)
        playButton.load(GameObjectLoader(100, 100, 200, 80, 255, 0, 0))
        playButton.setCallback(self.menuToPlay)
        playButton.setHoverBackground(0, 255, 0)

        exitButton = MenuButton("Exit", 0, 0, 0)
        exitButton.load(GameObjectLoader(100, 300, 200, 80, 255, 0, 0))
        exitButton.setCallback(self.exit)
        exitButton.setHoverBackground(200, 50, 50)

        self.gameObjects.append(playButton)
        self.gameObjects.append(exitButton)

        print("Entering Main Menu")

    def onExit(self):
        print("Exiting Main Menu")

    def update(self):
        for gameObj in self.gameObjects:
            gameObj.update()

    def menuToPlay(self):
        from game import TheGame
        TheGame().gameStateMachine.changeState(PlayState())

    def exit(self):
        from game import TheGame
        self.onExit()
        TheGame().quit()

class PauseState(MenuState):
    def onEnter(self):
        mainBtn = MenuButton("Menu", 0, 0, 0)
        mainBtn.load(GameObjectLoader(100, 100, 200, 80, 255, 0, 0))
        mainBtn.setHoverBackground(0, 255, 0)
        mainBtn.setCallback(self.toMenu)

        resumeBtn = MenuButton("Resume", 0, 0, 0)
        resumeBtn.load(GameObjectLoader(100, 300, 200, 80, 255, 0, 0))
        resumeBtn.setHoverBackground(0, 255, 0)
        resumeBtn.setCallback(self.resume)

        self.gameObjects.append(mainBtn)
        self.gameObjects.append(resumeBtn)

        print("Entering Pause Menu")

    def onExit(self):
        print("Exiting PauseMenu")

    def toMenu(self):
        from game import TheGame
        TheGame().gameStateMachine.popState()
        TheGame().gameStateMachine.changeState(MainMenuState())

    def resume(self):
        from game import TheGame
        TheGame().gameStateMachine.popState()

    def update(self):
        for gameObj in self.gameObjects:
            gameObj.update()

class PlayState(GameState):
    snake: Snake
    fruit: Fruit

    def onEnter(self):
        from game import TheGame

        self.snake = Snake()
        self.fruit = Fruit()

        self.fruit.load(GameObjectLoader(0, 0, 10, 10, 0, 0, 255))
        self.fruit.setRandomPosition(TheGame().window_x, TheGame().window_y)

        self.gameObjects.append(self.snake)
        self.gameObjects.append(self.fruit)

        print("Entering Play State")

    def onExit(self):
        print("Exiting Play State")

    def handleEvents(self):
        from inputhandler import TheInputHandler, Keyboard
        from game import TheGame
        if TheInputHandler().keyDown[Keyboard.ESC]:
            TheGame().gameStateMachine.pushState(PauseState())

    def objCollision(self, p1:GameObject, p2:GameObject) -> bool:
        leftA = p1.getX()
        rightA = p1.getX() + p1.width
        topA = p1.getY()
        bottomA = p1.getY() + p1.height

        leftB = p2.getX()
        rightB = p2.getX() + p2.width
        topB = p2.getY()
        bottomB = p2.getY() + p2.height

        if bottomA <= topB:
            return False
        if topA >= bottomB:
            return False
        if rightA <= leftB:
            return False
        if leftA >= rightB:
            return False

        return True

    def update(self):
        from game import TheGame
        self.handleEvents()

        if(self.objCollision(self.fruit, self.snake)):
            self.fruit.setRandomPosition(TheGame().window_x, TheGame().window_y)
            self.snake.addSegment()
            TheGame().score += 10

        for gameObj in self.gameObjects:
            gameObj.update()

class GameOverState(MenuState):
    def onEnter(self):
        from game import TheGame
        gameOverText = MenuButton("GAME OVER!", 255, 0, 0)
        gameOverText.load(GameObjectLoader(100, 10, 500, 80, 0, 0, 0))

        score = MenuButton(f"Your score is: {str(TheGame().score)}", 255, 0, 0)
        score.load(GameObjectLoader(100, 200, 500, 80, 0, 0, 0))

        menuBtn = MenuButton("Menu", 0, 0, 0)
        menuBtn.setHoverBackground(0, 255, 0)
        menuBtn.load(GameObjectLoader(100, 100, 300, 80, 255, 0, 0))
        menuBtn.setCallback(self.toMenu)

        playBtn = MenuButton("Play again", 0, 0, 0)
        playBtn.setHoverBackground(0, 255, 0)
        playBtn.load(GameObjectLoader(100, 300, 300, 80, 255, 0, 0))
        playBtn.setCallback(self.toPlay)

        self.gameObjects.append(menuBtn)
        self.gameObjects.append(playBtn)
        self.gameObjects.append(score)
        self.gameObjects.append(gameOverText)

        print("Entering GameOver Menu")

    def toMenu(self):
        from game import TheGame
        TheGame().gameStateMachine.changeState(MainMenuState())

    def toPlay(self):
        from game import TheGame
        TheGame().gameStateMachine.changeState(PlayState())

    def onExit(self):
        print("Exiting GameOver Menu")

    def update(self):
        for gameObj in self.gameObjects:
            gameObj.update()

{'TheGame': 'game.TheGame', 'TheInputHandler': 'inputhandler.TheInputHandler', 'Keyboard': 'inputhandler.Keyboard'}


import copy
from utils import tocuda, evaluate
from meta import teacher_backward, teacher_backward_ms
from tqdm import tqdm

import torch
import torch.distributed as dist
from torch.utils.tensorboard import SummaryWriter

class Trainer:

    def __init__(self, rank, args, main_net, meta_net,
                enhancer, gold_loader, silver_loader,
                valid_loader, test_loader, num_classes,
                logger, injection_mode, mix_g, exp_id=None):
        self.rank = rank
        self.args = args
        self.main_net = main_net
        self.meta_net = meta_net
        self.enhancer = enhancer
        self.gold_loader = gold_loader
        self.silver_loader = silver_loader
        self.valid_loader = valid_loader
        self.test_loader = test_loader
        self.num_classes = num_classes
        self.logger = logger
        self.injection_mode = injection_mode
        self.mix_g = mix_g
        self.exp_id = exp_id

        if rank == 0:
            self.writer = SummaryWriter(args.logdir + '/' + exp_id)

        self._setup_training()

    def _setup_training(self):
        ''' Fetch optimizers and schedulers for
          training the student, teacher and its enhancer'''
        args = self.args
        main_params = self.main_net.parameters()
        meta_params = self.meta_net.parameters()
        enhancer_params = self.enhancer.parameters()

        self.main_opt = torch.optim.SGD(main_params, lr=args.main_lr, weight_decay=args.wdecay, momentum=args.momentum)
        self.meta_opt = torch.optim.SGD(meta_params, lr=args.meta_lr)
        self.enhancer_opt = torch.optim.SGD(enhancer_params, lr=args.meta_lr)

        milestones = [int(m) for m in args.sched_milestones.split(',')]
        gamma = args.sched_gamma
        self.main_schdlr = torch.optim.lr_scheduler.MultiStepLR(self.main_opt, milestones=milestones, gamma=gamma)
        self.meta_schdlr = torch.optim.lr_scheduler.MultiStepLR(self.meta_opt, milestones=milestones, gamma=gamma)
        self.enhancer_schdlr = torch.optim.lr_scheduler.MultiStepLR(self.enhancer_opt, milestones=milestones, gamma=gamma)

    def _training_iter(self, data_s, target_s, data_g, target_g):
        ''' Perform a single training iteration'''
        data_g, target_g = tocuda(self.rank, data_g), tocuda(self.rank, target_g)
        data_s, target_s = tocuda(self.rank, data_s), tocuda(self.rank, target_s)

        # bi-level optimization stage
        eta = self.main_schdlr.get_last_lr()[0]
        kwargs = {'rank': self.rank, 'args': self.args,
                'main_net': self.main_net, 'main_opt': self.main_opt,
                'teacher': self.meta_net, 'teacher_opt': self.meta_opt,
                'enhancer': self.enhancer, 'enhancer_opt': self.enhancer_opt,
                'data_s': data_s, 'target_s': target_s,
                'data_g': data_g, 'target_g': target_g,
                'eta': eta, 'num_classes': self.num_classes,
                'injection_mode' : self.injection_mode, 'mix_g' : self.mix_g}
        teacher_backward_fn = teacher_backward if self.args.gradient_steps == 1 else teacher_backward_ms
        loss_g, loss_s, t_loss = teacher_backward_fn(**kwargs)

        # Update metrics
        for metric, name in zip([loss_g, loss_s, t_loss], ['loss_g', 'loss_s', 't_loss']):
            dist.reduce(metric, dst=0)
            metric /= self.args.n_gpus
            if self.rank == 0:
                self.writer.add_scalar(f'train/{name}', metric.item(), self.args.steps)

        if self.rank == 0:
            main_lr = self.main_schdlr.get_last_lr()[0]
            self.writer.add_scalar('train/main_lr', main_lr, self.args.steps)
            self.writer.add_scalar('train/gradient_steps', self.args.gradient_steps, self.args.steps)

    def train(self):
        ''' Training loop '''
        if self.rank == 0:
            self.best_val_main = 0
        self.epoch = 0

        self.args.steps = 0
        all_data_s, all_target_s = [], []

        for epoch in range(self.args.epochs):
            self.logger.info('Epoch %d:' % epoch)
            self.meta_net.train()
            self.main_net.train()

            self.epoch += 1
            batch_idx = 0

            if self.rank == 0:
                self.pbar = tqdm(total=len(self.silver_loader.dataset))

            for *data_s, target_s in self.silver_loader:

                # Setup training iteration
                self.args.steps += 1
                batch_idx += 1
                if type(data_s) is list and len(data_s) == 1:
                    data_s = data_s[0]
                *data_g, target_g = next(self.gold_loader)
                if self.args.gradient_steps > 1:
                    all_data_s.append(data_s)
                    all_target_s.append(target_s)

                # Actual training step
                if batch_idx % self.args.gradient_steps == 0:
                    if self.args.gradient_steps == 1:
                        self._training_iter(data_s, target_s, data_g, target_g)
                    else:
                        data_s = torch.cat(all_data_s)
                        target_s = torch.cat(all_target_s)
                        self._training_iter(data_s, target_s, data_g, target_g)
                        all_data_s, all_target_s = [], []

                # Update progress bar
                if self.rank == 0:
                    self.pbar.update(n=self.args.bs)

            # Per epoch evaluation
            if self.rank == 0:
                self.pbar.close()
                self._epoch_evaluation()
            dist.barrier()

            self.main_schdlr.step()
            self.meta_schdlr.step()
            self.enhancer_schdlr.step()

    def _epoch_evaluation(self):
        ''' Evaluate epoch in terms of main/test val/test accuracy
            Save best model if necessary '''
        val_acc_main = evaluate(self.rank, self.main_net, self.valid_loader)
        val_acc_meta = evaluate(self.rank, self.meta_net, self.valid_loader)
        test_acc_main = evaluate(self.rank, self.main_net, self.test_loader)
        test_acc_meta = evaluate(self.rank, self.meta_net, self.test_loader)

        self.logger.info('Val acc: %.4f\tTest acc: %.4f' % (val_acc_main, test_acc_main))
        self.writer.add_scalar('validation/main', val_acc_main, self.epoch)
        self.writer.add_scalar('test/meta', test_acc_meta, self.epoch)
        self.writer.add_scalar('validation/meta', val_acc_meta, self.epoch)
        self.writer.add_scalar('test/main', test_acc_main, self.epoch)

        if val_acc_main > self.best_val_main:
            self.best_val_main = val_acc_main

            self.best_main_params = copy.deepcopy(self.main_net.state_dict())
            self.best_meta_params = copy.deepcopy(self.meta_net.state_dict())

            self.logger.info('Saving best models...')
            torch.save({
                'epoch': self.epoch,
                'best val main': self.best_val_main,
                'main_net': self.best_main_params,
                'meta_net': self.best_meta_params,
            }, 'models/%s_best.pth' % self.exp_id)

    def final_eval(self):
        ''' Compute final test score '''
        if self.rank == 0:
            self.main_net.load_state_dict(self.best_main_params)
            self.meta_net.load_state_dict(self.best_meta_params)

            test_acc_main = evaluate(self.rank, self.main_net, self.test_loader)

            self.writer.add_scalar('test/acc', test_acc_main, self.args.steps)
            self.logger.info('Test acc: %.4f' % test_acc_main)
            return test_acc_main


from pyspark import SparkContext,SparkConf
import os
import csv
import time
import sys
from collections import deque,defaultdict
from operator import add
import time
import copy

def readfile(DATA_FILE):
    with open(DATA_FILE, 'r') as file:
        csv_reader = csv.reader(file, delimiter=',')
        out_list = []
        ishead = 1
        for row in csv_reader:
            if ishead:
                ishead = 0
                continue
            else:
                out_list.append(row)
    return out_list

def find_relation(src, dst, id_dict, THRESHOLD):
    set1, set2 = id_dict[src], id_dict[dst]
    len_intersect = len(set1.intersection(set2))
    if len_intersect >= THRESHOLD:
        return "follow"
    else:
        return "not follow"

def level_order(root, edge_map):
    '''
    :param root: str bid
    :param edge_map: dict, the default edge adjacent matrix
    :return res_dict: dict {level_num: {bid, bid...}}
    '''
    path_num = defaultdict(float)
    direct_from = defaultdict(str)
    node_level = defaultdict(int)

    queue = deque()
    visited = set()
    visited.add(root)
    queue.append(root)

    res_dict = defaultdict(set)
    cur_level = 0
    res_dict[cur_level].add(root)
    # total_num = 1

    node_level[root] = cur_level
    path_num[root] = 1
    direct_from[root] = "I'm root"

    while len(queue):

        this_level = len(queue)
        cur_level += 1

        for _ in range(this_level):

            head = queue.popleft()
            for next_level_node in edge_map[head]:

                if next_level_node not in visited:
                    queue.append(next_level_node)
                    visited.add(next_level_node)

                    res_dict[cur_level].add(next_level_node)

                    node_level[next_level_node] = cur_level
                    path_num[next_level_node] = path_num[head]
                    direct_from[next_level_node] = head

                elif next_level_node in visited and node_level[next_level_node] == cur_level and direct_from[next_level_node] != head:
                    path_num[next_level_node] += path_num[head]

        # total_num += len(res_dict[cur_level])

    return res_dict, path_num

def create_node_credit(user_list):

    user_init_credit = defaultdict(float)
    for user in user_list:
        user_init_credit[user] = 1.0
    return user_init_credit

def create_edge_credit(res_dict, edge_map, user_list, path_num):

    user_credit = create_node_credit(user_list)

    edge_list = []

    cur_level = max(res_dict.keys())
    while cur_level > 0:
        for node_lower in res_dict[cur_level]:
            node_set = edge_map[node_lower].intersection(res_dict[cur_level-1])
            # set_size = len(node_set)

            for node_upper in node_set:

                # assign credit to edge
                node_pair = sorted([node_upper, node_lower])
                edge_score = user_credit[node_lower] * (path_num[node_upper] / path_num[node_lower])

                # assign credit to upper level node
                user_credit[node_upper] += edge_score

                edge_list.append([tuple(node_pair), edge_score])
        cur_level -= 1
    return edge_list

def delete_max_edges(cur_edge_map, top_betweenness_pair):

    cur_edge_map[top_betweenness_pair[0]].remove(top_betweenness_pair[1])
    cur_edge_map[top_betweenness_pair[1]].remove(top_betweenness_pair[0])

    return

def community_generator(user_list, cur_edge_map):
    '''
    :param user_list: distinct user list
    :param cur_edge_map: modified edge map
    :return res_community: List(List())
    '''
    res_community = []
    node_visited = []
    for user in user_list:

        if user not in node_visited:
            queue = deque()
            visited = set()

            visited.add(user)
            queue.append(user)

            while len(queue):

                head = queue.popleft()
                for neighbor in cur_edge_map[head]:
                    if neighbor not in visited:

                        visited.add(neighbor)
                        queue.append(neighbor)

            node_visited.extend(visited)
            res_community.append(visited)

    return res_community

def get_modularity(edge_map, res_community, edge_m):

    Q_modularity = float(0)

    for single_community in res_community:
        for user_i in single_community:
            for user_j in single_community:

                if user_i in edge_map[user_j]:
                    A_ij = 1
                else:
                    A_ij = 0

                k_i, k_j = len(edge_map[user_i]), len(edge_map[user_j])
                Q_modularity += A_ij - (k_i * k_j) / (2 * edge_m)

    return Q_modularity / (2 * edge_m)

def betweenness_to_file(betweenness, OUTPUT_PATH):
    with open(OUTPUT_PATH, 'w') as fp:
        for single_list in betweenness:
            string = str(single_list)[1:][:-1]
            fp.write(string + '\n')
    fp.close()
    return

def community_to_file(community_store, COMMUNITY_OUTPUT_PATH):

    res = [sorted(list(x)) for x in community_store]
    res_comm = sorted(res, key=lambda res: [len(res), res])

    with open(COMMUNITY_OUTPUT_PATH, 'w') as fp:
        for line in res_comm:
            fp.write(str(line)[1:][:-1] + '\n')
    fp.close()

    return

if __name__ == "__main__":

    start_time = time.time()

    DATA_FILE = "///Users/tieming/inf553/hw4-py/data/ub_sample_data.csv"
    THRESHOLD = 7
    BETWEEN_OUTPUT_PATH = "///Users/tieming/inf553/hw4-py/task2_betweenness_train.txt"
    COMMUNITY_OUTPUT_PATH = "///Users/tieming/inf553/hw4-py/task2_community_train.txt"

    # THRESHOLD = int(sys.argv[1])
    # DATA_FILE = sys.argv[2]
    # BETWEEN_OUTPUT_PATH = sys.argv[3]
    # COMMUNITY_OUTPUT_PATH = sys.argv[4]

    conf = SparkConf().setMaster("local[*]")\
            .setAppName("task2")\
            .set("spark.executor.memory", "4g")\
            .set("spark.driver.memory", "4g")
    sc = SparkContext(conf=conf)
    sc.setLogLevel("ERROR")

    data_list = readfile(DATA_FILE)
    raw_data_rdd = sc.parallelize(data_list)\
                    .groupByKey()\
                    .mapValues(lambda x: set(x))\
                    .filter(lambda x: len(x[1]) >= THRESHOLD)

    id_dict = raw_data_rdd.collectAsMap()

    user_rdd = raw_data_rdd.map(lambda line: line[0]).distinct().coalesce(2)

    combined_rdd = user_rdd.cartesian(user_rdd)\
                            .filter(lambda line: line[0] < line[1])

    graph_rdd = combined_rdd.map(lambda line: (line[0], line[1], find_relation(line[0], line[1], id_dict, THRESHOLD)) )\
                .filter(lambda line: line[2] == "follow")

    user_distinct = graph_rdd.map(lambda line: (line[0], line[1]))\
                .flatMap(lambda x: x)\
                .distinct()\
                .map(lambda x: (x,))

    graph_with_reverse = graph_rdd.map(lambda line: ((line[0], line[1]), (line[1], line[0])) )\
                .flatMap(lambda x: x)

    edge_map = graph_with_reverse.groupByKey()\
                            .map(lambda line: (line[0], set(line[1])))\
                            .collectAsMap()

    # calculate betweenness

    user_list = user_distinct.map(lambda x: x[0]).collect()

    betweenness = user_distinct.map(lambda line: level_order(line[0], edge_map))\
            .map(lambda x: create_edge_credit(x[0], edge_map, user_list, x[1]))\
            .flatMap(lambda x:x)\
            .reduceByKey(add)\
            .map(lambda line: [line[0], line[1] / 2])\
            .sortBy(lambda line: [-line[1], line[0][0], line[0][1]])\
            .collect()

    betweenness_to_file(betweenness, BETWEEN_OUTPUT_PATH)

    # community detection
    max_Q = float(-sys.maxsize)
    cur_edge_map = copy.deepcopy(edge_map)
    edge_m = graph_with_reverse.count() / 2

    # adjust edge_map by delete the max betweenness until the map is empty
    modified_m = edge_m

    while modified_m > 0:

        betweenness = user_distinct.map(lambda line: level_order(line[0], cur_edge_map))\
                    .map(lambda x: create_edge_credit(x[0], cur_edge_map, user_list, x[1]))\
                    .flatMap(lambda x:x)\
                    .reduceByKey(add)\
                    .map(lambda line: [line[0], line[1] / 2])\
                    .max(key=lambda line: line[1])
        top_betweenness_pair = betweenness[0]
        delete_max_edges(cur_edge_map, top_betweenness_pair)

        modified_m -= 1

        res_community = community_generator(user_list, cur_edge_map)
        local_Q = get_modularity(edge_map, res_community, edge_m)

        if local_Q > max_Q:

            max_Q = local_Q
            community_store = copy.deepcopy(res_community)

    community_to_file(community_store, COMMUNITY_OUTPUT_PATH)

    print("Duration: ", time.time() - start_time)


import pandas
import requests
import schedule
import time
from twilio.rest import Client
from datetime import date

companies_data = pandas.read_csv("nasdaq_screener_1639426793803.csv")
COMPANY = None
STOCK = None
name_input = None

stock_api_key = open("stock_api_key.txt", "r").read()
news_api_key = open("news_api_key.txt", "r").read()
account_sid = open("account_sid.txt", "r").read()
auth_token = open("auth_token.txt", "r").read()
client = Client(account_sid, auth_token)
today = date.today()

STOCK_ENDPOINT = "https://www.alphavantage.co/query"
NEWS_ENDPOINT = "https://newsapi.org/v2/everything"

# --------------SEARCH FOR THE COMPANY-----------------#

found = False
while not found:
    name_input = input("Which company's stock price would you like to receive alerts on?\n"
                       "(Please type the full name of the company. e.g. 'International Business Machines' instead of "
                       "IBM) ").title()
    input_length = len(name_input)
    symbol_list = list(companies_data["Symbol"])
    name_list = list(companies_data["Name"])

    row = 0
    interested_index = []
    which_index = None
    for name in list(companies_data["Name"]):
        if name[:input_length].upper() == name_input.upper():
            interested_index.append(row)
        row += 1

    if len(interested_index) == 0:
        print(f"Cannot find a company named '{name_input}'. Please try again. ")
    else:
        if len(interested_index) == 1:
            which_index = interested_index[0]
        if len(interested_index) > 1:
            print("We've got a multiple result. ")
            number = 1
            for i in interested_index:
                print(f"{number}. {name_list[i]}")
                number += 1

            ans_nb = int(input("Please type the number of the company you would like to receive an alert. "))
            which_index = interested_index[ans_nb - 1]

        COMPANY = name_list[which_index]
        answer = input(f"Would you like to receive an alert for {COMPANY}? "
                       f"Please type Yes or No.").title()

        if answer == "Yes":
            found = True
            STOCK = symbol_list[which_index]

phone_number = input("Please insert your phone number including country code. (in the format of +123456789012) ")

# -----------NEWS MESSAGE GENERATOR----------------#

def send_news(past_price, present_price, interval):
    """Takes price in past, price in present and either "a day" or "two hours" and returns a string of live news."""
    percentage_change = present_price / past_price - 1
    if percentage_change > 0:
        news = f"{STOCK} ðŸ”º{int(abs(percentage_change) * 100)}%"
    else:
        news = f"{STOCK} ðŸ”»{int(abs(percentage_change) * 100)}%"
    news += f" overall in {interval}"

    news_parameters = {
        "q": name_input,
        "from": today,
        "sortBy": "popularity",
        "apiKey": news_api_key
    }

    response = requests.get(NEWS_ENDPOINT, params=news_parameters)
    response.raise_for_status()
    news_data = response.json()
    try:
        for n in range(3):
            news_dict = news_data["articles"][n]
            news += f"\nHeadline: {news_dict['title']}\nBrief: {news_dict['description']}\n{news_dict['url']}\n"
    except KeyError:
        news += "No relevant news found"
    finally:
        print(news)
        message = client.messages \
            .create(
            body=news,
            from_='+14752502580',
            to=phone_number
        )
        print(message.status)

# ----------MORNING ALERT-------------#

def send_message_morning():
    """Send SMS to user."""
    stock_parameters = {
        "function": "TIME_SERIES_DAILY",
        "symbol": STOCK,
        "outputsize": "compact",
        "apikey": stock_api_key
    }

    r = requests.get(STOCK_ENDPOINT, stock_parameters)
    stock_data = r.json()["Time Series (Daily)"]
    data_list = [value for (key, value) in stock_data.items()]
    ytd_closing_price = float(data_list[0]["4. close"])
    dbytd_closing_price = float(data_list[1]["4. close"])

    if abs(ytd_closing_price - dbytd_closing_price) > dbytd_closing_price * 0.04:
        send_news(dbytd_closing_price, ytd_closing_price, "a day")

# ---------------DAYTIME ALERT---------------------#

def send_message_alert():
    """Send SMS to user."""
    stock_parameters_ = {
        "function": "TIME_SERIES_INTRADAY",
        "symbol": STOCK,
        "interval": "60min",
        "apikey": stock_api_key
    }

    r_ = requests.get(STOCK_ENDPOINT, stock_parameters_)
    stock_data_ = r_.json()["Time Series (60min)"]
    data_list_ = [value for (key, value) in stock_data_.items()]
    cur_price = float(data_list_[0]["1. open"])
    prev_price = float(data_list_[2]["1. open"])

    if abs(cur_price - prev_price) > prev_price * 0.04:
        send_news(prev_price, cur_price, "two hours")

schedule.every().monday.at("09:30").do(send_message_morning)  # in CET
schedule.every().tuesday.at("09:30").do(send_message_morning)
schedule.every().wednesday.at("09:30").do(send_message_morning)
schedule.every().thursday.at("09:30").do(send_message_morning)
schedule.every().friday.at("09:30").do(send_message_morning)

schedule.every().monday.at("17:30").do(send_message_alert)  # in CET
schedule.every().monday.at("19:30").do(send_message_alert)
schedule.every().monday.at("21:30").do(send_message_alert)
schedule.every().tuesday.at("17:30").do(send_message_alert)
schedule.every().tuesday.at("19:30").do(send_message_alert)
schedule.every().tuesday.at("21:30").do(send_message_alert)
schedule.every().wednesday.at("17:30").do(send_message_alert)
schedule.every().wednesday.at("19:30").do(send_message_alert)
schedule.every().wednesday.at("21:30").do(send_message_alert)
schedule.every().thursday.at("17:30").do(send_message_alert)
schedule.every().thursday.at("19:30").do(send_message_alert)
schedule.every().thursday.at("21:30").do(send_message_alert)
schedule.every().friday.at("17:30").do(send_message_alert)
schedule.every().friday.at("19:30").do(send_message_alert)
schedule.every().friday.at("21:30").do(send_message_alert)

while True:
    schedule.run_pending()
    time.sleep(1)


# -*- coding: utf-8 -*
"""
      ┏┓       ┏┓
    ┏━┛┻━━━━━━━┛┻━┓
    ┃      ☃      ┃
    ┃  ┳┛     ┗┳  ┃
    ┃      ┻      ┃
    ┗━┓         ┏━┛
      ┗┳        ┗━┓
       ┃          ┣┓
       ┃          ┏┛
       ┗┓┓┏━━━━┳┓┏┛
        ┃┫┫    ┃┫┫
        ┗┻┛    ┗┻┛
    God Bless,Never Bug
"""

from typing import List, Optional

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from core.trip_handler import TripHandler
from database.db_handler import DBHandler
from utils import response_models
from utils.auth_tools import AuthValidator
from utils.payload_schemas import CreateTrip
from utils.response_models import Response, ResponseHandler

router = APIRouter(prefix='/trips', tags=['trip'])
general_auth = AuthValidator()

@router.get('/', response_model=Response[response_models.Trip])
async def get_trip(is_my_trip: Optional[bool] = None, page: Optional[int] = 1, per_page: Optional[int] = 10,
                   charger: Optional[str] = None, start: Optional[str] = None, end: Optional[str] = None,
                   model: Optional[str] = None, spec: Optional[str] = None, user: dict = Depends(general_auth),
                   db: Session = Depends(DBHandler.get_db)):
    result, pager = await TripHandler.get_trips(
        db=db,
        user_id=user['id'],
        is_my_trip=is_my_trip,
        charger=charger,
        start=start,
        end=end,
        model=model,
        spec=spec,
        page=page,
        per_page=per_page
    )
    return ResponseHandler.response(result=result, pager=pager)

@router.post('/', response_model=response_models.SuccessOrNot)
async def create_trip(trips: List[CreateTrip], user: dict = Depends(general_auth),
                      db: Session = Depends(DBHandler.get_db)):
    result = await TripHandler.create_trip(
        db=db,
        user_id=user['id'],
        trips=trips
    )
    return ResponseHandler.response(result=result)