# -*- coding: utf-8 -*-
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from torchvision import utils
from model_select import model_select
from arguments import arguments
from dataset import DatasetFolderPH
import torchvision.transforms as transforms
import torch
import torchvision.datasets as datasets
from sklearn.metrics import confusion_matrix,classification_report,precision_recall_fscore_support,matthews_corrcoef

args = arguments()

if __name__== "__main__":

    print(args.val, args.img_size, args.crop_size)
    test_transform = transforms.Compose([])
    normalize = transforms.Normalize(mean=[x / 255.0 for x in [125.3, 123.0, 113.9]],
                                    std=[x / 255.0 for x in [63.0, 62.1, 66.7]])
    if args.img_size > args.crop_size:
        test_transform.transforms.append(transforms.Resize(args.img_size, interpolation=transforms.InterpolationMode.BILINEAR))
    test_transform.transforms.append(transforms.CenterCrop(args.crop_size))
    test_transform.transforms.append(transforms.ToTensor())
    test_transform.transforms.append(normalize)
    test_dataset = datasets.ImageFolder(args.val, transform=test_transform)
    print(len(test_dataset), f"validation images loaded from {args.val}.")
    print(test_dataset.class_to_idx)
    test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=args.batch_size, shuffle=False,
                                                num_workers=args.num_workers, pin_memory=True, drop_last=False)

    model = model_select(args, len(test_dataset.classes), renew_fc=False)
    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")
    model.to(device)

    model.eval()
    with torch.no_grad():
        i=0
        for (data, targets) in test_loader:
            batch_size = targets.size(0)
            data = data.to(device,non_blocking=True)
            outputs = model(data)
            topk = min(outputs.shape[1],5)
            _, pred = outputs.topk(topk, 1, True, True)
            pred = pred.t().to('cpu') # top-k predictions
            correct = (pred == targets.unsqueeze(dim=0)).expand_as(pred)
            pred = pred.detach().numpy()
            if i==0:
                res = np.zeros(topk)
                prediction=[[] for k in range(topk)]
            for k in range(topk):
                res[k] += correct[:k].reshape(-1).float().sum(0, keepdim=True)
                prediction[k].extend(pred[k].tolist())
            i += batch_size

    fns = [f for f,t in test_dataset.imgs]
    truth = [t for f,t in test_dataset.imgs]
    #print(len(fns),len(truth),len(prediction[0]))
    print(confusion_matrix(truth,prediction[0]))
    print(classification_report(truth,prediction[0],digits=3))
    print(matthews_corrcoef(truth,prediction[0]))

    pd.DataFrame({'filename': fns, 'truth': truth, 'prediction_1': prediction[0], 'prediction_2': prediction[1]}).to_csv("predictions.csv")


class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: list[list[int]]) -> int:
        """
        Return the number of possible unique paths that the robot can take to
        reach the bottom-right corner.

        You are given an m x n integer array grid. There is a robot initially
        located at the top-left corner (i.e., grid[0][0]). The robot tries to
        move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot
        can only move either down or right at any point in time.

        An obstacle and space are marked as 1 or 0 respectively in grid. A path
        that the robot takes cannot include any square that is an obstacle.

        :param list[list[int]] obstacleGrid: grid of obstacles and spaces
        :returns int unique_paths: the number of possible unique paths that the robot can take to
        reach the bottom-right corner

        Time complexity: O(n^2)
        Space complexity: O(n^2)

        LeetCode: Beats 90.91%
        """
        if obstacleGrid[0][0] == 1:
            return 0

        dp = []

        for _ in range(len(obstacleGrid)):
            dp.append([0] * len(obstacleGrid[0]))

        dp[0][0] = 1

        for i in range(len(dp)):
            for j in range(len(dp[0])):
                if obstacleGrid[i][j] != 1:
                    if i > 0:
                        dp[i][j] += dp[i - 1][j]
                    if j > 0:
                        dp[i][j] += dp[i][j - 1]

        return dp[-1][-1]


import ast
from unittest import mock

from surveyor.visitors import BaseClassVisitor, ImportFromVisitor

class TestBaseClassVisitor:
    BASE_CLASS_NAME = 'base'
    CLASS_NAME = 'implement'

    def run_class_def(self, expected_result, base_id=None, base_spec=None):
        if base_id is None:
            base_id = self.BASE_CLASS_NAME

        visitor = BaseClassVisitor(self.BASE_CLASS_NAME)
        node = mock.MagicMock()
        base = mock.Mock(spec=base_spec)
        base.configure_mock(id=base_id)
        node.configure_mock(name=self.CLASS_NAME, bases=[base])

        visitor.visit_ClassDef(node)
        assert visitor.classes == expected_result

    def test_visit_class_def(self):
        self.run_class_def({self.CLASS_NAME}, base_spec=ast.Name)

    def test_visit_class_def_incorrect_base(self):
        self.run_class_def(set())

    def test_visit_class_def_no_match(self):
        self.run_class_def(set(), base_id='bad', base_spec=ast.Name)

class TestImportFromVisitor:
    def test_visit_import_from(self):
        target = 'test'
        visitor = ImportFromVisitor([target])
        node = mock.MagicMock()
        name_mock = mock.Mock()
        name_mock.configure_mock(name=target)
        node.configure_mock(names=[name_mock])
        visitor.visit_ImportFrom(node)
        assert visitor.contains_target

    def test_visit_import_from_no_match(self):
        visitor = ImportFromVisitor(['hello'])
        node = mock.MagicMock()
        name_mock = mock.Mock()
        name_mock.configure_mock(name='bye')
        node.configure_mock(names=[name_mock])
        visitor.visit_ImportFrom(node)
        assert not visitor.contains_target


import json
import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

def wake_dir(scale, partition, run, qdx):
    if qdx == 26 or qdx == 27:
        return f"/results/wake/scale={scale}/partition={partition}/cleaned-parquet/run={run}/q{qdx}"
    else:
        return f"/results/wake/scale={scale}/partition={partition}/parquet/run={run}/q{qdx}"

def wanderjoin_file(scale, partition, run, qdx):
    return f"/results/wanderjoin/scale={scale}/partition={partition}/run={run}/{qdx}.csv"

def wake_read_error(scale, partition, num_runs, qdx):
    all_time_ns = []
    for run in range(1, num_runs + 1):
        with open(os.path.join(wake_dir(scale, partition, run, qdx), "meta.json")) as f:
            result = json.load(f)
            all_time_ns.append(result["time_measures_ns"])
    all_time_s = np.array(all_time_ns) / 1e9
    all_time_s = all_time_s.mean(axis=0)
    with open(os.path.join(wake_dir(scale, partition, 1, qdx), "results.json")) as f:
        result = json.load(f)
        mape_p = result["mape_p"]
    assert len(all_time_s) == len(mape_p)
    return all_time_s[1:], mape_p[1:]

def wanderjoin_read_error(scale, partition, num_runs, qdx):
    readings = []
    for run in range(1, num_runs + 1):
        result_df = pd.read_csv(wanderjoin_file(scale, partition, run, qdx), skipfooter=1)
        for i, row in result_df.iterrows():
            readings.append(((float(row["time (ms)"])/1000.0), float(row["rel. CI"])))
    combined_df = pd.DataFrame(readings, columns = ["time", "error"])
    result_df = combined_df.groupby("time").agg({ "error": ["mean"] }).sort_values("time", ascending=True).reset_index()
    time = result_df.values[:,0]
    mape = result_df.values[:,1]
    return time, mape

def polars_read_time(scale, partition, qdx):
    result_file = f"/results/polars/scale={scale}/partition={partition}/timings.csv"
    result_df = pd.read_csv(result_file)
    result_df = result_df[result_df["query"] == qdx]
    if len(result_df) > 0:
        agg_result = result_df.groupby("query").agg({ "time": ["mean", "std"] })
        return agg_result.values
    else:
        return [[0.0, 0.0]]

def progressive_read_error(scale, partition, num_runs, qdx):
    return [0.0], [0.0]

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('scale', type=int)
    parser.add_argument('partition', type=int)
    parser.add_argument('num_runs', type=int)
    args = parser.parse_args()

    polars_results = [
        polars_read_time(args.scale, args.partition, 26),
        polars_read_time(args.scale, args.partition, 27),
        polars_read_time(args.scale, args.partition, 23),
        polars_read_time(args.scale, args.partition, 24),
        polars_read_time(args.scale, args.partition, 25),
    ]

    wake_results = [
        wake_read_error(args.scale, args.partition, args.num_runs, 26),
        wake_read_error(args.scale, args.partition, args.num_runs, 27),
        wake_read_error(args.scale, args.partition, args.num_runs, 23),
        wake_read_error(args.scale, args.partition, args.num_runs, 24),
        wake_read_error(args.scale, args.partition, args.num_runs, 25),
    ]

    ola_results = [
        progressive_read_error(args.scale, args.partition, args.num_runs, 26),
        progressive_read_error(args.scale, args.partition, args.num_runs, 27),
        wanderjoin_read_error(args.scale, args.partition, args.num_runs, 23),
        wanderjoin_read_error(args.scale, args.partition, args.num_runs, 24),
        wanderjoin_read_error(args.scale, args.partition, args.num_runs, 25),
    ]

    titles = [
        "Modified Q1",
        "Modified Q6",
        "Modified Q3",
        "Modified Q7",
        "Modified Q10",
    ]

    ola_labels = [
        "ProgressiveDB",
        "ProgressiveDB",
        "Wanderjoin",
        "Wanderjoin",
        "Wanderjoin",
    ]

    # Plots results.
    SZ = 2.0
    fig, axs = plt.subplots(ncols=len(ola_labels), figsize=(len(ola_labels) * 1.2 * SZ, SZ))
    for idx, ax in enumerate(axs):
        ## Add Polars time as a vertical line.
        polars_time = polars_results[idx][0][0]
        ax.axvline(polars_time)

        wake_time, wake_mape_p = wake_results[idx]
        ola_time, ola_mape_p = ola_results[idx]

        lns1 = ax.plot(wake_time, wake_mape_p, 'b-x', label="WAKE")
        lns2 = ax.plot(ola_time, ola_mape_p, 'r.-', label=ola_labels[idx])

        lns = lns1 + lns2
        labs = [l.get_label() for l in lns]
        ax.legend(lns, labs, loc='lower center', bbox_to_anchor=(0.5, 1.2), ncols=len(lns))
        ax.set_xlabel("Elapsed Time (s)")
        ax.set_ylabel("MAPE (%)")
        ax.set_xscale("log")
        ax.set_yscale("log")
        ax.set_title(titles[idx])
    fig.tight_layout()
    fig.savefig("/results/viz/fig9_tpch_ola.png")


#!/usr/local/bin/python
# -*- coding: utf-8 -*-

from tensorflow2caffe_shakkala import Tensorflow2CaffeShakkala
import numpy as np

if __name__ == '__main__':
    tensorflow_model_path = "second_model6.h5"
    caffe_model_path_prefix = "shakkala"
    caffe_model_name = "shakkala"

    rotation = Tensorflow2CaffeShakkala(tensorflow_model_path, caffe_model_path_prefix, caffe_model_name,
                        315,
                       False, True)
    data = {}
    data["input"] = np.ones([1,315])
    rotation.generate(data)


def Main():
    n_tests = int(input())

    for _ in range(n_tests):
        score = input_tree()

        for i in range(len(score)-1):
                score[i].append(split(score[i][1]))

        for i in range(len(score)-1):
            score[i].append(len(score[i][1]))

        score.pop()
        score.sort(key = lambda score: score[2])
        score.sort(key = lambda score: score[3])

        for i in range(len(score)):
            for j in range(len(score)):
                if i == j:
                    break
                if score[i][1] == score[j][1]:
                    status = False
                else:
                    status = True

        tree = []

        print(score)

def split(word):
    return [char for char in word]

def input_tree():
    scores = input().split(' ')

    scores = [sub.replace('(', '') for sub in scores]
    scores = [sub.replace(')', '') for sub in scores]

    for i in range(len(scores)):
        scores[i] = scores[i].split(",")

    return scores

if __name__ == '__main__':
    Main()


from os.path import dirname, join, abspath
import platform

def get_lib_name():
    """Get the name of the sme C library"""
    system = platform.system().lower()

    if system == "windows":
        return "libsme-5.dll"

    arch = platform.machine()
    bits = 64  # platform.architecture()[0][:-3]

    return "sme_synth.so.{system}.{arch}.{bits}".format(
        system=system, arch=arch, bits=bits
    )

def get_lib_directory():
    if platform.system() in ["Windows"]:
        dirpath = "bin"
    else:
        # For Linux/MacOS
        dirpath = "lib"
    return dirpath

def get_full_libfile():
    """Get the full path to the sme C library"""
    localdir = dirname(dirname(__file__))
    libfile = get_lib_name()
    dirpath = get_lib_directory()
    libfile = join(localdir, dirpath, libfile)
    return libfile


import time
import datetime

class TimeTask:
    def __init__(self):
        self.dt_start = datetime.datetime.now()
        print(self.dt_start)

    def end(self):
        self.dt_end = datetime.datetime.now()
        return self.dt_end - self.dt_start

task = TimeTask()
time.sleep(95)
time = task.end()
print(time)


# В одномерном массиве целых чисел определить два наименьших элемента. Они
# могут быть как равны между собой (оба минимальны), так и различаться.

from random import randint
import sys
from size import info_size

array = [randint(1, 100) for num in range(20)]
print(array)

min_els = [array[0], array[1]]
i = 2
while i < len(array):
    s = array[i]
    if array[i] < min_els[0]:
        min_els[0] = array[i]
    elif array[i] < min_els[1]:
        min_els[1] = array[i]
    i += 1

print(f'Минимальные элементы {min_els[0]} и {min_els[1]}')

print(50 * '=')
print(sys.platform, sys.version)
print(50 * '=')
info_size(min_els, 'min_els')
print(50 * '=')
info_size(array)
# С использованием 2х переменных типа int
# ==================================================
# linux 3.8.2 (default, Apr 27 2020, 15:53:34)
# [GCC 9.3.0]
# ==================================================
# Имя, тип и размер объекта: min1, <class 'int'>, 28 байт
# Объем информации в объекте: 4 байт
# Размер пустого объекта: 24 байт
# ==================================================
# Имя, тип и размер объекта: min2, <class 'int'>, 28 байт
# Объем информации в объекте: 4 байт
# Размер пустого объекта: 24 байт

# С использованием списка
# ==================================================
# linux 3.8.2 (default, Apr 27 2020, 15:53:34)
# [GCC 9.3.0]
# ==================================================
# Имя, тип и размер объекта: min_els, <class 'list'>, 72 байт
# Объем информации в объекте: 16 байт
# Размер пустого объекта: 56 байт

# Разница в объеме использованной памяти 16 байт


import numpy as np
from metal.analysis import lf_summary
from scipy import sparse
from torch.utils.tensorboard import SummaryWriter

def make_learning_function_matrix(data, labeling_functions):
    """Returns a labeling function matrix.

    Args:
        data (pandas df) : the text to be labeled.
        labeling_functions (list) : a list of labeling functions to be
        performed on the text.

    Returns:
        noisy_labels (ndarray) : an nd numpy array of the labels for
        the text.

    """
    noisy_labels = np.empty((len(data), len(labeling_functions)))

    for i, row in data.iterrows():
        for j, labeling_function in enumerate(labeling_functions):
            noisy_labels[i][j] = labeling_function(row.values[0].lower())
    return noisy_labels

def make_large_learning_function_matrix(data, labeling_function):
    """Returns a labeling function matrix calculated using  generator
    functions.

    Args:
        data (pandas df) : the text to be labeled with.
        labeling_function (generator) : a generator labeling function to be
        performed on
        the text.

    Returns:
        noisy_labels (ndarray) : an nd numpy array of the labels for t
        he text.
    """

    names = get_names(labeling_function(""))

    noisy_labels = np.empty((len(data), len(names)))

    for i, row in data.iterrows():
        gen = labeling_function(row.values[0].lower())
        j = 0
        while j < len(names):
            noisy_labels[i][j] = next(gen)[0]
            j += 1

    return noisy_labels, names

def get_names(generator):
    """Returns the regex used in the labeling functions."""
    names = []
    j = 0
    while j < 1000:
        try:
            name = next(generator)[1]
            names.append(name)
        except StopIteration:
            break
    return names

def analysis_of_weak_labeling(
    data,
    true_labels,
    labeling_functions,
    labeling_function_names,
    generator_labeling_functions,
):
    """Displays the summary of labeling functions.

    Args:
        data (pandas df) : the text to be labeled with.
        true_labels (pandas series) : the current labels of the data.
        labeling_functions (list) : a list of labeling functions to be
        performed on the text.
        labeling_function_names (list) : a list of the name of the
        labeling functions to be performed on the text.
        generator_labeling_functions (list) : a list of labeling
         to be performed on the text.

    Returns:
        Displays a summary of the labeling function's success on the
        text.
    """
    SummaryWriter()
    labeling_function_matrix = make_learning_function_matrix(data, labeling_functions)

    generator_labeling_function_matrix = np.empty((len(data), 1))
    names = []
    for labeling_function in generator_labeling_functions:
        new_matrix, new_names = make_large_learning_function_matrix(
            data, labeling_function
        )
        generator_labeling_function_matrix = np.hstack(
            (generator_labeling_function_matrix, new_matrix)
        )
        names = names + new_names

    concat_matrix = np.hstack(
        (labeling_function_matrix, generator_labeling_function_matrix[:, 1:])
    )
    true_labels = np.array(true_labels)
    print(
        lf_summary(
            sparse.csr_matrix(concat_matrix),
            Y=true_labels,
            lf_names=labeling_function_names + names,
        )
    )


# Exercício 1: Verificação de Número Positivo

# Escreva um programa em Python que solicite ao
# usuário um número inteiro e
# verifique se o número é positivo, negativo ou zero.
# Em seguida, exiba uma mensagem apropriada de acordo
# com o resultado da verificação.

a = int(input("Digite um numero inteiro qualquer :"))

if a < 0:
    print(f"O numero {a} é negativo !")
elif a > 0:
    print(f"O numero {a} é positivo !")
else:
    print(f"O numero da variavel é {a}!")

# Exercício 2: Classificação de Triângulos

# Escreva um programa em Python que receba três comprimentos de lados de um triângulo e
# determine se o triângulo
# é equilátero, isósceles ou escaleno.

a = float(input("Digite um numero qualquer :"))
b = float(input("Digite um numero qualquer :"))
c = float(input("Digite um numero qualquer :"))

if a == b and b == c:
    print("Este tringulo é EQUILATERO !")
elif a == b and b != c:
    print("Este tringulo é ISOSCELES !")
else:
    print("Este trinagulo é ESCALENO!")

# Exercício 3: Verificação de Ano Bissexto

# Um ano é bissexto se for divisível por 4, exceto para anos que são divisíveis por 100.
# No entanto, anos divisíveis por 400
# também são bissextos. Escreva um programa em Python que solicite um ano ao usuário
# e determine se é um ano bissexto ou não.

a = int(input("Digite aqui o ano :"))

if a % 4 == 0 and not a % 100 == 0:
    print("Esse ano É Bissexto !")
elif a % 400 == 0:
    print("Esse ano É Bissexto !")
else:
    print("Esse ano NÃO é Bissexto !")


#author_by zhuxiaoliang
#2018-08-19 下午4:11

#sklearn 库 基于tfidf文本分类

#oding:utf-8
import codecs
import json
import math
import os
import jieba
import nltk
import DataSpider
from  sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cross_validation import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.externals import joblib

class Preprocessor:
    def __init__(self):
        self.features = []

    def get_new_data(self):
        DataSpider.DataSpider().get_data()

    def process_data(self, pos_file='./corpus/pos.txt', neg_file='./corpus/neg.txt', feature_num=30):
        if not os.path.exists(pos_file) or not os.path.exists(neg_file):
            self.get_new_data()
        # process the data
        stopwords = self.loan_stopwords()
        pos = self.loan_txt(pos_file)
        neg = self.loan_txt(neg_file)

        pos_seg_list = self.get_seg_list(pos, stopwords)
        neg_seg_list = self.get_seg_list(neg, stopwords)
        with open('postrain.txt','w') as f :
            for ps in pos_seg_list:
                f.write(' '.join(ps)+'\n')

        #文本分类模型训练
        '''
        #print(pos_seg_list)
        seg_list = pos_seg_list[:900]+neg_seg_list[:900]
        with open('seglist.txt','w') as f :
            for s in seg_list:
                f.write(' '.join(s))
        train=[]
        for s in seg_list:
            train.append(' '.join(s))

        print(len(train))
        #print('train:',train)
        label=['1']*900+['-1']*900
        #print(label)
        x_train,x_test,y_train,y_test  = train_test_split(train,label,test_size=0.25,random_state=33)

        #
        #vectorizer = CountVectorizer(min_df=1,token_pattern=r'\w+')
        ##
        # bag_of_words = vectorizer.get_feature_names()
        #print(bag_of_words)
        v = TfidfVectorizer(token_pattern=r'\w+')
        train_data= v.fit_transform(x_train)
        test_data= v.transform(x_test)

        lr = LogisticRegression()
        lr.fit(train_data,y_train)
        print(lr.score(test_data,y_test))
        text = ['好好']
        mytest = v.transform(text)
        print(lr.predict(mytest))

        #save
        joblib.dump(value=lr,filename='lr.gz',compress=True)
        print('model saved! ')
        return v.vocabulary_'''

    def save_feature_model(self, features, filename='./model/feature.json'):
        with codecs.open(filename, 'w', 'utf-8') as f:
            f.write(json.dumps(features, ensure_ascii=False))
            f.close()

    def load_feature_model(self, filename='./model/feature.json'):
        with codecs.open(filename, 'r', 'utf-8') as f:
            self.features = json.loads(f.read())

    def sentence2vec(self, sentence):
        if len(self.features) == 0:
            self.load_feature_model()
        print(self.features)
        seg_list = jieba.cut('手机值得拥有，放心购买', False)
        print('jieba',seg_list)
        freq_dist = nltk.FreqDist(seg_list)
        local_list = []
        for each in self.features:
            local_list.append(freq_dist[each])
        print(local_list)
        return local_list

    def loan_stopwords(self):
        stopwords = []
        with codecs.open('./corpus/stopwords.txt', 'r', 'utf-8') as f:
            for each in f.readlines():
                each = each.strip('\n')
                each = each.strip('\r')
                stopwords.append(each)
        return stopwords

    def loan_txt(self, filename):
        lists = []
        with codecs.open(filename, 'r', 'utf-8') as f:
            for each in f.readlines():
                if each.strip() != '':
                    lists.append(each.strip('\n'))
        return lists

    def get_seg_list(self, array, stopwords):
        seg_list = []
        for each in array:
            local_list = jieba.cut(each, False)
            final_list = []
            for word in local_list:
                if word not in stopwords and word != ' ':
                    final_list.append(word)
            seg_list.append(final_list)
        return seg_list

    def get_freq_dist(self, seg_list):
        freq_dist = []
        for each in seg_list:
            freq_dist.append(nltk.FreqDist(each))
        return freq_dist

    def get_words_set(self, seg_list):
        word_set = set()
        for each in seg_list:
            for word in each:
                word_set.add(word)
        return word_set

    def compute_words_num(self, seg_list):
        total = 0
        for each in seg_list:
            total += len(each)
        return total

    def compute_TF_IDF(self, word_set, freq_dist, words_num, total_seg_list):
        word_dist = {}
        for word in word_set:
            tf = 0
            for each in freq_dist:
                tf += each[word]
            tf /= words_num
            total_num = len(total_seg_list)
            exist_num = 0
            for each in total_seg_list:
                if word in each:
                    exist_num += 1
                    continue
            idf = math.log(total_num / exist_num)
            word_dist[word] = tf * idf
        return word_dist

    def sort_by_value(self, d):
        items=d.items()
        backitems=[[v[1], v[0]] for v in items]
        backitems.sort(reverse=True)
        return [backitems[i] for i in range(0, len(backitems))]

    def feature_list(self, pos_words_tfidf, pos_feature_num, neg_words_tfidf, neg_feature_num):
        features = []
        for each in pos_words_tfidf[:pos_feature_num]:
            features.append(each[1])
        for each in neg_words_tfidf[:neg_feature_num]:
            features.append(each[1])
        return features

    def create_train_csv(self, features, pos_freq_dist, neg_freq_dist):
        with codecs.open('./corpus/train.csv', 'w', 'utf-8') as f:
            f.write(','.join(features) + ',sentiment\n')
            pos_words_vec = self.words2vec(features, pos_freq_dist)
            neg_words_vec = self.words2vec(features, neg_freq_dist)
            for each in pos_words_vec:
                f.write(','.join(each) + ',1\n')
            for each in neg_words_vec:
                f.write(','.join(each) + ',-1\n')
            f.close()

    def words2vec(self, features, freq_dist):
        word_vec = []
        for sentence in freq_dist:
            local_vec = []
            for each in features:
                #print(sentence[each])
                local_vec.append(str(sentence[each]))
            word_vec.append(local_vec)
        return word_vec

    def PMI(self, words_set, seg_list, prob):
        PMI_words = {}
        for each in words_set:
            occur = 0
            for sent in seg_list:
                if each in sent:
                    occur += 1
            word_prob = occur / len(seg_list)
            PMI_words[each] = math.log(word_prob / prob)
        return PMI_words

    def Chi_square(self, words, seg_list, total_seg_list):
        total_num = len(total_seg_list)
        prob = len(seg_list) / len(total_seg_list)
        Chi_square_words = {}
        for word in words:
            occur_in_class = 0
            occur_in_all = 0
            for each in seg_list:
                if word in each:
                    occur_in_class += 1
            for each in total_seg_list:
                if word in each:
                    occur_in_all += 1
            prob_in_class = occur_in_class / len(seg_list)
            prob_in_all = occur_in_all / len(total_seg_list)
            Chi_square_words[word] = total_num * prob_in_all ** 2 * (prob_in_class - prob) ** 2 /\
                                    (prob_in_all * (1 - prob_in_all) * prob * (1 - prob))
        return Chi_square_words

if __name__ == '__main__':
    processor = Preprocessor()
    voc  = processor.process_data()#这个voc需要写到一个文件中
    #print(voc)
    #import json
    #with open('modelvoc.json','w') as f:

     #json.dump(voc,f)
    #import pickle
    #with open('modelvec.txt','w') as f :
    #pickle.dump(voc,f)

    '''
    text = ['手机 性能 不错']
    model = joblib.load(filename='lr.gz')
    print(type(model))
    v = TfidfVectorizer(token_pattern=r'\w+',vocabulary=voc)
    mytest = v.fit_transform(text)
    print(model.predict(mytest))'''


# Write a function that computes the running total of a list.

def sum_of_list(lists):
    list_sum = 0
    for i in range(0, len(lists)):
            list_sum += lists[i]
    return list_sum

# test
lis = [0, 2, 45, 78, 65, 32, 66]
print(sum_of_list(lis))


from lxml import etree

class XPathElementLocator():

    def __init__(self, html):
        self.html = html
        self.etree = etree.HTML(self.html)

    def find_xpath(self, xpath_query):
        result = self.etree.xpath(xpath_query)

        if isinstance(result, list):
            if len(result) > 1:
                result = ' '.join(result)
            elif len(result) == 1:
                result = result[0]

        return result


#User function Template for python3

class Solution:
    def segregate0and1(self, arr, n):
        i=0
        j=n-1
        while(i<j):
            if(arr[i]==1):
                 arr[i],arr[j]=arr[j],arr[i]
                 j=j-1
                 continue
            i=i+1

#{
#  Driver Code Starts
#Initial Template for Python 3

if __name__ == '__main__':
    tc = int(input())
    while tc > 0:
        n = int(input())
        arr = list(map(int, input().strip().split()))
        ob = Solution()
        ob.segregate0and1(arr, n)
        print(*arr)
        tc -= 1

# } Driver Code Ends

Segregate 0s and 1s - GFG/segregate-0s-and-1s.py


class LossFunctionOptions(object):

    @staticmethod
    def setup(parser, classification=False):
        loss_functions = ['squared', 'quantile', 'epsilon_insensitive', 'squared_epsilon_insensitive', 'huber']
        if classification:
            loss_functions += ['hinge', 'log', 'squared_hinge', 'modified_huber']
        parser.add_argument('-loss', '-loss_function',
                            choices=loss_functions,
                            default='squared',
                            help='loss function (default: squared)')
        return parser


import warnings
import logging
from pathlib import Path

import torch
from seq2seq.data import Dictionary, Sentence, Token, SentenceSrc
from seq2seq import device
from seq2seq.modules import Attention

import random

from typing import List, Union

log = logging.getLogger('seq2seq')

class Encoder(torch.nn.Module):
    def __init__(self, input_dim, emb_dim, hid_dim, n_layers, dropout,
                 rnn_type: str = "LSTM", bidirectional: bool = False):
        super().__init__()

        self.input_dim = input_dim
        self.emb_dim = emb_dim
        self.hid_dim = hid_dim
        self.n_layers = n_layers
        self.dropout = dropout

        self.embedding = torch.nn.Embedding(input_dim, emb_dim)

        if rnn_type in ['LSTM', 'GRU', 'RNN']:
            self.rnn = getattr(torch.nn, rnn_type)(self.emb_dim, self.hid_dim,
                                                        num_layers=self.n_layers,
                                                        dropout=dropout,
                                                        bidirectional=bidirectional)

        self.dropout = torch.nn.Dropout(dropout)

    def forward(self, src):

        embedded = self.dropout(self.embedding(src))
        outputs, (hidden, cell) = self.rnn(embedded)

        return hidden, cell

class Decoder(torch.nn.Module):
    def __init__(self, output_dim, emb_dim, hid_dim, n_layers, dropout,
                 rnn_type: str = "LSTM", bidirectional: bool = False, use_attention: bool = False):
        super().__init__()

        self.emb_dim = emb_dim
        self.hid_dim = hid_dim
        self.output_dim = output_dim
        self.n_layers = n_layers
        self.dropout = dropout
        self.bidirectional = bidirectional
        self.use_attention = use_attention

        if self.use_attention:
            self.attention = Attention(self.hid_dim)

        self.embedding = torch.nn.Embedding(output_dim, emb_dim)

        if rnn_type in ['LSTM', 'GRU', 'RNN']:
            self.rnn = getattr(torch.nn, rnn_type)(self.emb_dim, self.hid_dim,
                                                        num_layers=self.n_layers,
                                                        dropout=dropout,
                                                        bidirectional=bidirectional)

        self.out = torch.nn.Linear(hid_dim, output_dim)

        self.dropout = torch.nn.Dropout(dropout)

    def forward(self, input_tensor, hidden, cell):

        input_tensor = input_tensor.unsqueeze(0)
        embedded = self.dropout(self.embedding(input_tensor))
        output, (hidden, cell) = self.rnn(embedded, (hidden, cell))
        if self.use_attention:
            output, _ = self.attention(output)
        prediction = self.out(output.squeeze(0))
        return prediction, hidden, cell

class Seq2Seq(torch.nn.Module):

    def __init__(self,
                 encoder: torch.nn.Module,
                 decoder: torch.nn.Module,
                 src_dict: Dictionary,
                 trg_dict: Dictionary,
                 ):

        super(Seq2Seq, self).__init__()

        self.src_dict = src_dict
        self.trg_dict = trg_dict

        self.trg_vocab = trg_dict.idx2item

        self.PAD_IDX = self.src_dict.item2idx['_PAD_'.encode('utf-8')]
        self.END_IDX = self.src_dict.item2idx['<eos>'.encode('utf-8')]

        self.encoder = encoder
        self.decoder = decoder
        self.loss_function = torch.nn.CrossEntropyLoss(ignore_index=self.PAD_IDX)

        self.max_len = 50

        assert self.encoder.hid_dim == self.decoder.hid_dim, \
            "Hidden dimensions of encoder and decoder must be equal!"
        assert self.encoder.n_layers == self.decoder.n_layers, \
            "Encoder and decoder must have equal number of layers!"

        self.apply(self.init_weights)

        self.to(device)

    @staticmethod
    def init_weights(m):
        for name, param in m.named_parameters():
            torch.nn.init.uniform_(param.data, -0.08, 0.08)

    def sentences_to_idx(self, sentences: List[Sentence], map_dict) -> torch.LongTensor:
        sentences_length = [len(sent) for sent in sentences]
        max_length = max(sentences_length)
        sentences_idx_tensor = torch.zeros(
            [len(sentences), max_length], dtype=torch.int64, device=device).fill_(self.PAD_IDX)
        for i, sent_zip in enumerate(zip(sentences, sentences_length)):
            sent, length = sent_zip
            sent_idx = [map_dict[t.text.encode('utf-8')] for t in sent]
            sentences_idx_tensor[i][:length] = torch.LongTensor(sent_idx)
        sentences_idx_tensor = torch.einsum('ij->ji', sentences_idx_tensor)
        return sentences_idx_tensor

    def forward(self, src_idx_tensor, trg_idx_tensor, teacher_forcing_ratio=0.5):

        batch_size = trg_idx_tensor.shape[1]

        batch_max_len = trg_idx_tensor.shape[0]

        if batch_max_len > self.max_len:
            batch_max_len = self.max_len
            trg_idx_tensor = trg_idx_tensor[:batch_max_len]

        elif batch_max_len == 1:
            batch_max_len = self.max_len

        trg_vocab_size = self.decoder.output_dim

        outputs = torch.zeros(batch_max_len, batch_size, trg_vocab_size).to(device)

        hidden, cell = self.encoder(src_idx_tensor)

        trg_input = trg_idx_tensor[0, :]

        for t in range(1, batch_max_len):

            output, hidden, cell = self.decoder(trg_input, hidden, cell)
            outputs[t] = output
            teacher_force = random.random() < teacher_forcing_ratio
            top1 = output.max(1)[1]
            trg_input = (trg_idx_tensor[t] if teacher_force else top1)

        outputs = torch.einsum('ijk->jki', outputs[1:])

        return outputs, trg_idx_tensor

    def forward_loss(self, sentences: Union[SentenceSrc, List[SentenceSrc]], teacher_forcing_ratio=0.5):
        if isinstance(sentences, SentenceSrc):
            sentences = [SentenceSrc]

        trg = [sent.trg for sent in sentences]
        trg_idx_tensor = self.sentences_to_idx(trg, self.trg_dict.item2idx)

        src = [sent.src for sent in sentences]
        src_idx_tensor = self.sentences_to_idx(src, self.src_dict.item2idx)

        outputs, trg_idx_tensor = self.forward(src_idx_tensor, trg_idx_tensor, teacher_forcing_ratio)
        loss = self.loss_function(outputs, torch.einsum('ij->ji', trg_idx_tensor[1:]))
        return loss

    def save(self, model_file: Union[str, Path]):
        model_state = {
            'state_dict': self.state_dict(),
            'src_dict': self.src_dict,
            'trg_dict': self.trg_dict,
            'encoder': self.encoder,
            'decoder': self.decoder
        }

        torch.save(model_state, str(model_file), pickle_protocol=4)

    def predict(self, sentences: Union[List[SentenceSrc], SentenceSrc, str], mini_batch_size: int = 32):
        output_str = False
        if isinstance(sentences, SentenceSrc):
            sentences = [sentences]
        if isinstance(sentences, str):
            sentences = [SentenceSrc(Sentence(sentences))]
            output_str = True

        trg = [sent.trg for sent in sentences]
        trg_idx_tensor = self.sentences_to_idx(trg, self.trg_dict.item2idx)

        src = [sent.src for sent in sentences]
        src_idx_tensor = self.sentences_to_idx(src, self.src_dict.item2idx)

        with torch.no_grad():
            batches = [sentences[x:x + mini_batch_size] for x in range(0, len(sentences), mini_batch_size)]
            for batch in batches:
                outputs, _ = self.forward(src_idx_tensor, trg_idx_tensor, teacher_forcing_ratio=0)
                predicted_seq = torch.argmax(outputs, dim=1)
                for i, sent in enumerate(batch):
                    for idx in predicted_seq[i][1:]:
                        if idx.item() != self.END_IDX:
                            sent.trg.add_token(Token(self.trg_vocab[idx].decode('utf-8')))
                        else:
                            break
        if not output_str:
            return sentences
        else:
            outs = [sent.trg.to_plain_string() for sent in sentences]
            return outs

    @staticmethod
    def _filter_empty_sentences(sentences: List[Sentence]) -> List[Sentence]:
        filtered_sentences = [sentence for sentence in sentences if sentence.tokens]
        if len(sentences) != len(filtered_sentences):
            log.warning('Ignore {} sentence(s) with no tokens.'.format(len(sentences) - len(filtered_sentences)))
        return filtered_sentences

    @classmethod
    def load_from_file(cls, model_file: Union[str, Path]):

        state = Seq2Seq._load_state(model_file)

        model = Seq2Seq(
            encoder=state['encoder'],
            decoder=state['decoder'],
            src_dict=state['src_dict'],
            trg_dict=state['trg_dict']
        )

        model.load_state_dict(state['state_dict'])
        model.eval()
        model.to(device)

        return model

    @classmethod
    def _load_state(cls, model_file: Union[str, Path]):

        with warnings.catch_warnings():
            warnings.filterwarnings("ignore")
            state = torch.load(str(model_file), map_location=device)
            return state

    @staticmethod
    def load(model_file: str):

        model: Seq2Seq = Seq2Seq.load_from_file(model_file)

        return model


"""
In this file we load csv file and store it to sqlite database
"""
from typing import Iterator

import dotenv
import os
from pathlib import Path
from datetime import datetime
import sqlite3
import pandas as pd
from sqlalchemy import Index
from tqdm import tqdm

from models import BehaviorData, MemberData, Base
from utils.db_utils import create_db_session, engine

dotenv.load_dotenv()
data_dir_path = Path(os.getenv("DATA_DIR_PATH"))
assert data_dir_path.exists(), f"please set DATA_DIR_PATH in .env file."

# create database
_ = sqlite3.connect('database.db')

# Define the SQLAlchemy session
session = create_db_session()

def get_member_df() -> pd.DataFrame:
    global data_dir_path
    data_types = {
        'ShopMemberId': str,
        'RegisterSourceTypeDef': str,
        'RegisterDateTime': str,
        'Gender': str,
        'Birthday': str,
        'APPRefereeId': str,
        'APPRefereeLocationId': str,
        'IsAppInstalled': bool,
        'IsEnableEmail': bool,
        'IsEnablePushNotification': bool,
        'IsEnableShortMessage': bool,
        'FirstAppOpenDateTime': str,
        'LastAppOpenDateTime': str,
        'MemberCardLevel': int,
        'CountryAliasCode': str,
    }
    df = pd.read_csv(data_dir_path / 'MemberData.csv', dtype=data_types)
    df['RegisterDateTime'] = pd.to_datetime(df['RegisterDateTime'], format='ISO8601')
    df['Birthday'] = df['Birthday'].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))
    df = df.drop(columns=['APPRefereeId', 'APPRefereeLocationId', 'FirstAppOpenDateTime', 'LastAppOpenDateTime'])
    return df

def get_behavior_df_iter(file_name: [str, Path]) -> Iterator[pd.DataFrame]:
    """Load behavior data from a CSV file and return a DataFrame iterator.

    Args:
        file_name: str or Path object, the name of the file to load.

    Yields:
        pd.DataFrame: Each chunk of the loaded DataFrame.
    """
    global data_dir_path
    data_types = {
        'Tunnel': str,
        'Device': str,
        'FullvisitorId': str,
        'DeviceId': str,
        'ShopMemberId': str,
        'HitTime': float,
        'Language': str,
        'CountryAliasCode': str,
        'Version': str,
        'UTMSource': str,
        'UTMMedium': str,
        'UTMName': str,
        'Behavior': str,
        'RegisterTunnel': str,
        'CategoryId': str,
        'SalePageId': str,
        'UnitPrice': float,
        'Qty': float,
        'TotalSalesAmount': float,
        'CurrencyCode': str,
        'TradesGroupCode': str,
        'SearchTerm': str,
        'ContentType': str,
        'ContentName': str,
        'ContentId': str,
        'PageType': str,
        'EventTime': float
    }
    chunk_size = 1_000_000  # Number of rows per chunk

    for chunk in pd.read_csv(data_dir_path / 'BehaviorData' / file_name, chunksize=chunk_size, dtype=data_types):
        chunk = chunk.dropna(subset=['HitTime', 'EventTime'])
        chunk['HitTime'] = chunk['HitTime'].astype(int)
        chunk['EventTime'] = chunk['EventTime'].astype(int)
        chunk['HitTime'] = pd.to_datetime(chunk['HitTime'], unit='ms')
        chunk['EventTime'] = pd.to_datetime(chunk['EventTime'], unit='ms')
        yield chunk

def df_to_db(model_cls, df: pd.DataFrame):
    """Load a dataframe into a database table using the given model class."""
    if not hasattr(df_to_db, 'cls_existingpks'):
        df_to_db.cls_existingpks = dict()

    chunk_size = 50_000  # Number of records to add in each chunk
    primary_key = model_cls.__table__.primary_key.columns.keys()[0]  # Get the primary key column name
    primary_key_attr = getattr(model_cls, primary_key)  # Get the primary key attribute of the model class

    # Collect existing primary keys from the database
    if model_cls not in df_to_db.cls_existingpks:
        print('create skip list..')
        existing_primary_keys = set()  # Keep track of existing primary keys in the database
        for existing_record in session.query(primary_key_attr).all():
            existing_primary_keys.add(existing_record[0])
        df_to_db.cls_existingpks[model_cls] = existing_primary_keys
        print('skip list created')

    records = []
    for index, record in enumerate(tqdm(df.to_dict(orient='records'))):
        primary_key_value = record[primary_key]
        if primary_key_value in df_to_db.cls_existingpks[model_cls]:
            continue

        records.append(model_cls(**record))

        if len(records) % chunk_size == 0:
            session.bulk_save_objects(records)
            session.commit()
            records = []  # Reset the records list after committing the chunk

    # Add any remaining records
    if records:
        session.bulk_save_objects(records)
        session.commit()

def main():
    print('import MemberData.csv to database')
    df_to_db(MemberData, get_member_df())

    behavior_file_names = sorted(f for f in os.listdir(data_dir_path / 'BehaviorData') if ".csv" in f)
    num = 0
    for file_name in behavior_file_names:
        print(f'import {file_name} to database')
        this_num = 0
        for df_chunk in get_behavior_df_iter(file_name):
            df_chunk['id'] = (df_chunk.index + num)
            this_num += len(df_chunk)
            df_to_db(BehaviorData, df_chunk)
        num += this_num

if __name__ == '__main__':
    main()


"""empty message

Revision ID: 991797027fb4
Revises: c3c72f618bcb
Create Date: 2021-01-08 12:56:35.813528

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '991797027fb4'
down_revision = 'c3c72f618bcb'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('pokeballs', sa.Column('img_url', sa.String(length=500), nullable=False))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('pokeballs', 'img_url')
    # ### end Alembic commands ###


# PyBank main

# import dependencies
import csv
import os

dates = []
changes = []
pre_mon = 0

# pathway into csv file
csvpath = os.path.join("Resources","budget_data.csv")

# pathway out to csv file
output_path = os.path.join("Analysis","analysis.txt")

# Step 1: read in csv X
# Step 2: initializations X
    # Step 2.1: initialize dates list, dates = [] X
    # Step 2.2: initialize prev_mon = 0 X
    # Step 2.3: initialize changes list, changes = [] X
# Step 3: go through the loop X
    # Step 3.1: pull dates out of the row of the csv X
        # Step 3.1.1: pull the date from the 0th row position, row[0] X
        # Step 3.1.2: append date to dates X
    # Step 3.2: determine the change from month to month X
        # Step 3.2.1: pull amount from 1st column position, row[1]
        # Step 3.2.2: append amount to cur_mon X
        # Step 3.2.3: subtract cur_mon - prev_mon to get change X
        # Step 3.2.4: cur_mon becomes prev_mon X
        # Step 3.2.5: append change to changes list X
# Step 4: calculate average of change
# Step 5:

# Read in csv
with open(csvpath, encoding = "UTF-8") as csvfile:
    csvreader = csv.reader(csvfile, delimiter = ",")

    csv_header = next(csvfile)

    for row in csvreader:
        date = row[0]
        dates.append(date)

        cur_mon = int(row[1])
        change = cur_mon - pre_mon
        pre_mon = cur_mon
        changes.append(change)

    avg = sum(changes) / len(changes)

with open(output_path, 'w') as text:
    text.writelines("Financial Analysis\n")
    text.writelines("-------------------------------\n")
    text.writelines(f"Total months: {len(changes)}\n")
    text.writelines(f"Total: ${sum(changes)}\n")
    text.writelines(f"Average change: ${round(avg,0)}\n")
    text.writelines(f"Greatest increase in profit : ${max(changes)}\n")
    text.writelines(f"Greatest decrease in profit : ${min(changes)}\n")

print("Financial Analysis")
print("-------------------------------")
print(f"Total months: {len(changes)}")
print(f"Total: ${sum(changes)}")
print(f"Average change: ${round(avg,0)}")
print(f"Greatest increase in profit : ${max(changes)}")
print(f"Greatest decrease in profit : ${min(changes)}")


# coding: utf-8

# # ISO-SWS data preprocessing: convert to pickled dataframes

# In[1]:

import glob
import numpy as np
import os
import pandas as pd

from astropy.io import fits
from astropy.table import Table

from swsnet.dataframe_utils import     isosws_fits_to_dataframe, convert_fits_to_pickle, create_swsmeta_dataframe

# ***

# ## Find out how many files we're working with

# In[2]:

spec_dir = 'sws_irs/'
spec_files = np.sort(glob.glob(spec_dir + '*.fit'))

# In[3]:

len(spec_files)

# ### Identify the 'good' TDTs

# In[4]:

good_tdts = np.loadtxt('good_tdts.txt').astype(int)

# In[5]:

good_tdts

# In[6]:

len(good_tdts)

# ## Convert spectra to dataframes and save to disk as pickles

# In[13]:

perform_conversion = True

# In[14]:

# Note the break I've added; remove for full conversion.
if perform_conversion:
    print('=============================\nConverting fits files...\n=============================\n')

    # Iterate over all the fits files and convert them.
    for index, fits_file in enumerate(spec_files):

        tdt = int(fits_file.split('/')[-1].split('_irs')[0])
        if tdt not in good_tdts:
            continue

        if index % 20 == 0:
            print(index, '/', len(spec_files))

        pickle_path = convert_fits_to_pickle(fits_file, verify_pickle=True, verbose=False)

    print('\n=============================\nComplete.\n=============================')

# ## Build dataframe containing metadata (including labels) and paths to pickled files.

# ###### Creates isosws_metadata_df.pkl.

# In[7]:

# Only do this once.
recreate_meta_pickle = True

# In[8]:

if recreate_meta_pickle:
    df = create_swsmeta_dataframe(good_tdts=good_tdts)

# In[9]:

df

# In[10]:

np.unique(df['group'].values)

# In[11]:

np.unique(df['subgroup'].values)

# In[12]:

np.unique(df['uncertainty_flag'].values)

# In[13]:

np.unique(df['data_ok'].values)

# In[14]:

np.unique(df['object_type'].values)

# In[15]:

df.to_pickle('../metadata_step0.pkl')

# In[16]:

df.head()

# In[17]:

mdf = pd.read_pickle('../metadata_step0.pkl')
mdf

# ***

# ***

# ***

# # Appendix A -- Example transformation from .fits to pd.dataframe

# #### Convert spectrum file to dataframe, header

# In[63]:

# Grab the first file from the glob list.
test_spec = spec_files[0]
test_spec

# In[64]:

# Read it in with astropy.io.fits, check dimensions.
test_hdu = fits.open(test_spec)
test_hdu.info()

# In[68]:

# Utilize our defined function to transform a string of the .fits filename to a pandas dataframe and header.
# 'header' will be an astropy.io.fits.header.Header object; see a couple subsections below for conversion options.
df, header = isosws_fits_to_dataframe(test_spec)

# #### Inspect dataframe

# In[69]:

df.shape

# In[70]:

df.head()

# In[71]:

df.describe()

# #### Header from the .fits file

# In[72]:

type(header)

# In[73]:

# Uncomment below to see full header of one file as an example.
header

# In[74]:

# Can convert to other formats if we want to use the header information for something.
# See http://docs.astropy.org/en/stable/io/fits/api/headers.html

# header_str = header.tostring()
# header.totextfile('test_header.csv')

# ***


''' How many circular primes are there below one million? '''

from sympy.ntheory import isprime
from pyprimesieve import primes

def main():
    ''' Find number of circular primes below 1,000,000 '''
    count = 0
    nums = set(primes(1000000))
    while nums:
        num = str(nums.pop())
        perms = {int(num[i:]+num[:i]) for i in range(len(num))}
        if {isprime(p) for p in perms} == {True}:
            count += len(perms)
        nums.difference_update(perms)
    print(count)

if __name__ == "__main__":
    main()


import logging, struct
from helpers import *

class MessageParser:
	"""
	Commonly used parser to encode and decode communication messages.

	Author:
		Maximilian Hess <mail@maximilianhess.com>
	"""

	def encode(self, type, params):
		"""
		Encodes a message.

		Args:
			type: the type of the message
			params: a dictionary of parameters that will be encoded separately

		Returns:
			Returns the hexadecimal encoded message size and the message itself as a utf-8 string.
		"""

		result = "type:%s;" % type
		for param, value in params.items():
			result = "%s%s:%s;" % (result, param, value)

		return struct.pack('>H', len(result)) + b(result)

	def decode(self, message):
		"""
		Decodes a message.

		Args:
			message: a string representative of the message

		Returns:
			Returns the type of the message and the parameters as a dictionary.
		"""

		messageType = None
		params = {}
		tuples = message.split(";")
		for t in tuples:
			if t is not "":
				tokens = t.split(":")
				if tokens[0].strip() == "type":
					messageType = tokens[1].strip()
				else:
					try:
						params[tokens[0].strip()] = tokens[1].strip()
					except IndexError:
						params[tokens[0].strip()] = None

		return messageType, params


from pathlib import Path

from io import StringIO

from tests.parse_csv import parse_csv
from vendetta import Config, Vendetta

def test_vendetta_single(test_data: Path):
    config = Config(columns={
        'firstname': 'first_name',
        'lastname': 'last_name',
        'birthday': 'date',
        'address': 'street_address',
        'city': 'city',
        'state': 'state',
        'country': 'country',
        'email': 'email',
        'phone': 'phone_number',
    })

    v = Vendetta(config=config)

    output_file = StringIO()
    with (test_data / 'people.csv').open('r') as input_file:
        v(input_file, output_file)

    output_file.seek(0)
    output_data = parse_csv(output_file)

    first, second = output_data
    assert first['lastname'] == second['lastname']
    assert first['city'] == second['city']

    assert first['email'] != second['email']
    assert first['phone'] != second['phone']
    assert first['firstname'] != second['firstname']


import urllib.request,json
from .models import source
from config import Config

api_key = Config.NEWS_API_KEY

base_url = Config.NEWS_API_BASE_URL

def configure_request(app):
    global api_key,base_url
    api_key = app.config['NEWS_API_KEY']
    base_url = app.config['NEWS_API_BASE_URL']

def get_news():
    get_news_url = (base_url).format(api_key)

    with urllib.request.urlopen(get_news_url) as url:
        get_news_data = url.read()
        get_news_response = json.loads(get_news_data)
        result= getResult(get_news_response['sources'])
        return result

def getResult(list):
    get_list=[]
    for newsitem in list:
        id = newsitem.get('id')
        name = newsitem.get('name')
        description = newsitem.get('description')
        url = newsitem.get('url')
        obj = source(id,name,description,url)
        get_list.append(obj)
    return get_list

def get_news_sources(news_sources):
    get_news_sources_url = (base_url).format(news_source,api_key)

    with urllib.request.urlopen(get_news_sources_url) as url:
        get_news_sources_data = url.read()
        get_news_sources_response = json.loads(get_news_sources_data)
        result= getResult(get_news_sources_response['aritcles'])
        return result

def process_results(news_source_results):

    news_sources_results = []

    for news_source_item in news_sources_results:
        id = news_source_item.get('id')
        author = news_source_item.get('author')
        title = news_source_item.get('title')
        description = news_source_item.get('description')
        url = news_source_item.get('url')
        urlToImage = news_source_item.get('urlToImage')
        publishedAt = news_source_item.get('publishedAt')
        content = news_source_item.get('content')

        news_source_object = news_Source(id,author,title,description,url,urlToImage,publishedAt,content)
        news_sources_results.append(news_source_object)

    return news_source_results


'''
usage example:
>> python hetero_nn.py --lr 1e-2 --epochs 10 --no-shuffle --no-cipher --seed 7
'''

from IPython.core import debugger as idb
import trio
from torch import nn
from torch.nn import functional as F

from simufml.hetero_ml.hetero_nn import HeteroNN_ACT, HeteroNN_PAS
from simufml.common.optimizer import SGD
from simufml.hetero_ml.learner.party_learner import PartyLearner
from simufml.hetero_ml.learner.fed_learner import FedLearner
from simufml.utils.util import RANDOM_SEED
from simufml.utils.util import Role
from simufml.demo.util import get_args, csv2dl, auc_func, abs_data_path

data_path = abs_data_path(__file__)
ACT_csv=data_path/'breast_hetero_guest.csv'
PAS_csv=data_path/'breast_hetero_host.csv'

class FcReLU(nn.Module):
    def __init__(self, in_size, out_size):
        super().__init__()
        self.fc = nn.Linear(in_size, out_size)

    def forward(self, x):
        return F.relu(self.fc(x))

    def initialize(self):
        self.fc.reset_parameters()

def hetero_nn_demo(
    skip_cipher, epochs,
    train_bs, valid_bs, rand_seed, lr, wd, shuffle
):
    #==== data ====
    ACT_train_dl, ACT_valid_dl, PAS_train_dl, PAS_valid_dl, ACT_fs, PAS_fs = csv2dl(
        active_csv=ACT_csv,
        passive_csv=PAS_csv,
        train_bs=train_bs,
        valid_bs=valid_bs,
        rand_seed=rand_seed,
        tensor_data=True
    )

    #==== Model ====
    ACT_model = HeteroNN_ACT(
        bottom_model=FcReLU(10, 3),
        top_model=FcReLU(2, 1),
        mix_layer_io_shape=(3,3,2),
        loss_func=F.binary_cross_entropy_with_logits,
        lr=lr
    )
    PAS_model = HeteroNN_PAS(
        bottom_model=FcReLU(20, 3),
    )

    #==== Optimizer ====
    ACT_opt = SGD(lr=lr, wd=wd)
    PAS_opt = SGD(lr=lr, wd=wd)

    #==== Party Learner ====
    ACT_learner = PartyLearner(
        role=Role.active,
        model=ACT_model,
        train_dataloader=ACT_train_dl,
        valid_dataloader=ACT_valid_dl,
        optimizer=ACT_opt
    )
    PAS_learner = PartyLearner(
        role=Role.passive,
        model=PAS_model,
        train_dataloader=PAS_train_dl,
        valid_dataloader=PAS_valid_dl,
        optimizer=PAS_opt
    )

    #==== metrics ====
    metric_funcs = {"auc": auc_func}

    #==== Learner ====
    learner = FedLearner(
        parties=[ACT_learner, PAS_learner]
    )
    learner.fit(epochs, metric_funcs, shuffle, skip_cipher, True)

    return ACT_learner.epoch_loss, ACT_learner.metric_results[0]

if __name__=='__main__':
    args = get_args()
    RANDOM_SEED.seed = args.seed

    hetero_nn_demo(
        skip_cipher=args.skip_cipher, epochs=args.epochs,
        train_bs=args.train_bs, valid_bs=args.valid_bs, rand_seed=args.seed, lr=args.lr, wd=args.wd, shuffle=args.shuffle
    )


import sys

from shapify.palette.palette_builder import PaletteBuilder

if __name__ == '__main__':
    pic_name = 'test.png'
    if len(sys.argv) > 1:
        pic_name = sys.argv[1]

    pb = PaletteBuilder(None, filename=pic_name)

    palette = pb.get_new_palette()
    PaletteBuilder.show_palette(palette)


'''crie um programa que faça o computador jogar JOKEMPÔ com você

pedra > tesoura > papel > pedra

\033[style;text;back m
'''
import random
import time
computador = random.choice(['pedra', 'tesoura', 'papel'])

# print(computador)

print('Vamos jogar \033[32mJOKEMPÔ\033[m!!!')
user = str(input('Digite pedra, papel ou tesoura: '))

time.sleep(1)
print('JO')
time.sleep(1)
print('KEN')
time.sleep(1)
print('PÔ')

if user == computador:
    print('\033[30mempatou!\033[m')
elif user == 'pedra':
    if computador == 'papel':
        print('\033[31mputz! você perdeu! O computador jogou papel!\033[m')
    else:
        print('\033[32mHA! o computador jogou tesoura.. você venceu!!!\033[m')

elif user == 'papel':
    if computador == 'tesoura':
        print('\033[31mputz! você perdeu! O computador jogou tesoura!\033[m')
    else:
        print('\033[32mHA! o computador jogou pedra.. você venceu!!!\033[m')

elif user == 'tesoura':
    if computador == 'pedra':
        print('\033[31mputz! você perdeu! O computador jogou pedra!\033[m')
    else:
        print('\033[32mHA! o computador jogou papel.. você venceu!!!\033[m')
else:
    print('\033[33mTalvez você tenha jogado errado, tente novamente\033[m')


"""
ここから分かち書き
"""

import MeCab
import sys

#tagger = MeCab.Tagger('-F\s%f[6] -U\s%m -E\\n')

import MeCab

tagger = MeCab.Tagger("-Owakati")

f = open("komachi.txt", encoding = "utf_8")
read_text = f.readlines()
f.close()

for text in read_text:
    result = tagger.parse(text)
    with open("koamchi_wakati.txt", encoding = "utf_8", mode="a") as write_file:
        write_file.writelines(result)


from stock_monitor import AlphaVantageClient
from stock_monitor.models import *
from pony import *
import datetime
# from credentials import API_KEY

# Para coletar dados de outras ações, adicione o símbolo na lista STOCK_SYMBOLS
STOCK_SYMBOLS = ['PETR4.SAO', 'B3SA3.SAO']

# Caminho do arquivo que armazenará os dados das ações listadas em STOCK_SYMBOLS
DATABASE_FILE_PATH = 'database.db'

#Chave para uso da AlphaVantageAPI
API_KEY = ''

# Caso uma determinada ação não exista no BD, um registro no banco de dados é criado. Caso já exista, o registro é
# obtido do BD.
def get_or_create_ticker(symbol):
    ticker = StockSymbol.get(ticker=symbol)
    if ticker is None:
        ticker = StockSymbol(ticker=symbol)

    return ticker

def main():
    database_controller = connect_to_database(DATABASE_FILE_PATH)
    stock_api = AlphaVantageClient(API_KEY)

    with db_session:
        for ticker in STOCK_SYMBOLS:

            # Obtendo os dados dos ultimos 7 dias disponiveis na alphavantage api
            try:
                api_data = stock_api.get_time_series_daily(ticker, period=7)
            except Exception as e:
                print('Error:', ticker)
                continue

            # Obtendo os dados da acao do banco de dados
            db_stock = get_or_create_ticker(ticker)

            # Percorrendo os datas disponíveis na api
            for stock_date in api_data:
                stock_date_price = StockPrices.get(date=stock_date, fk_stock_id=db_stock)
                api_price = float(api_data[stock_date]['4. close'])

                if stock_date_price is None:
                    StockPrices(
                        last_update=datetime.datetime.utcnow(),
                        close_price=api_price,
                        date=stock_date,
                        fk_stock_id=db_stock
                    )
                elif stock_date_price != api_price:
                    stock_date_price.close_price = api_price
            print('Success:', ticker)

if __name__ == '__main__':
    main()


import json
from datetime import datetime, timedelta

from clients.bronbro_api_client import BronbroApiClient
from clients.db_client import DBClient

class WarpService:

    def __init__(self):
        self.db_client = DBClient()
        self.bronbro_api_client = BronbroApiClient()

    def get_pairs(self):
        result = self.db_client.get_pairs_liquidity_pool()
        return [item._asdict() for item in result]

    def set_exponent_for_liquidity(self, liquidity, denom_traces):
        exponent = 0
        if 'ibc/' in liquidity['denom']:
            denom_trace = next((item.base_denom for item in denom_traces if item.denom_hash in liquidity['denom']), None)
        else:
            denom_trace = liquidity['denom']
        if denom_trace.startswith('u'):
            exponent = -6
        elif denom_trace.startswith('a'):
            exponent = -18
        elif denom_trace.startswith('milli'):
            exponent = -3
        elif denom_trace == 'gravity0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2':
            exponent = -18
        elif denom_trace == 'aevmos':
            exponent = -18
        liquidity['exponent'] = exponent

    def convert_liquidity_amount_to_boot(self, liquidity, tickers, hydrogen_to_boot):
        if liquidity['denom'] == 'boot':
            return
        elif liquidity['denom'] != 'hydrogen':
            last_price = next((ticker.last_price for ticker in tickers if
                              ticker.target_currency == liquidity['denom'] and ticker.base_currency == 'hydrogen'),
                             None)
            liquidity['denom'] = 'hydrogen'
            liquidity['amount'] = liquidity['amount'] * 10**liquidity['exponent'] / last_price
        liquidity['denom'] = 'boot'
        liquidity['amount'] = liquidity['amount'] * hydrogen_to_boot

    def get_boot_price(self, tickers):
        prices = self.bronbro_api_client.get_exchange_rates()
        atom_exchange_rate = next((rate.get('price') for rate in prices if rate.get('symbol') == 'ATOM'), None)
        hydrogen_to_atom_price = next(ticker.last_price for ticker in tickers if ticker.ticker_id == 'hydrogen_ibc/15E9C5CF5969080539DB395FA7D9C0868265217EFC528433671AAF9B1912D159')
        hydrogen_price = atom_exchange_rate / hydrogen_to_atom_price / 10**6
        boot_to_hydrogen_price = next(ticker.last_price for ticker in tickers if ticker.ticker_id == 'boot_hydrogen')
        boot_exchange_rate = hydrogen_price / boot_to_hydrogen_price
        return boot_exchange_rate

    def set_total_liquidity(self, boot_price, ticker):
        ticker['liquidity_in_usd'] = boot_price*(ticker['liquidity_a']['amount'] + ticker['liquidity_b']['amount'])

    def update_price_based_on_exponent(self, ticker):
        ticker['last_price'] = ticker['last_price'] / 10**ticker['liquidity_a']['exponent'] * 10**ticker['liquidity_b']['exponent']

    def get_tickers(self, show_all):
        tickers = self.db_client.get_base_for_tickers()
        boot_price = self.get_boot_price(tickers)
        hydrogen_to_boot = next((ticker.last_price for ticker in tickers if
                                 ticker.base_currency == 'boot' and ticker.target_currency == 'hydrogen'), None)
        ticker_dicts = [ticker._asdict() for ticker in tickers]
        denom_traces = self.db_client.get_denom_traces()
        # TODO: FIX AFTER DB UPDATED
        datetime_24_hour_ago = datetime.now() - timedelta(hours=24)
        datetime_24_hour_ago = datetime.strftime(datetime_24_hour_ago, '%Y-%m-%d %H:%M:%S')
        height = self.db_client.get_height_after_timestamp(datetime_24_hour_ago)
        height_from_search_volume = height.height if height else None
        if height_from_search_volume:
            offer_coins_volume = self.db_client.get_offer_coins_volume(height_from_search_volume)
            demand_coins_volume = self.db_client.get_demand_coins_volume(height_from_search_volume)
        else:
            offer_coins_volume = []
            demand_coins_volume = []
        for ticker in ticker_dicts:
            ticker['liquidity_a'] = json.loads(ticker['liquidity_a'])
            ticker['liquidity_b'] = json.loads(ticker['liquidity_b'])
            self.set_exponent_for_liquidity(ticker['liquidity_a'], denom_traces)
            self.set_exponent_for_liquidity(ticker['liquidity_b'], denom_traces)
            self.update_price_based_on_exponent(ticker)
            self.convert_liquidity_amount_to_boot(ticker['liquidity_a'], tickers, hydrogen_to_boot)
            self.convert_liquidity_amount_to_boot(ticker['liquidity_b'], tickers, hydrogen_to_boot)
            self.set_total_liquidity(boot_price, ticker)
            base_offer_coin_volume = next((item.sum for item in offer_coins_volume if ticker['pool_id'] == item.pool_id and ticker['base_currency'] == item.offer_coin_denom), 0)
            target_offer_coin_volume = next((item.sum for item in offer_coins_volume if ticker['pool_id'] == item.pool_id and ticker['target_currency'] == item.offer_coin_denom), 0)
            base_demand_coin_volume = next((item.sum for item in demand_coins_volume if ticker['pool_id'] == item.pool_id and ticker['base_currency'] == item.demand_coin_denom), 0)
            target_demand_coin_volume = next((item.sum for item in demand_coins_volume if ticker['pool_id'] == item.pool_id and ticker['target_currency'] == item.demand_coin_denom), 0)
            ticker['base_volume'] = base_demand_coin_volume + base_offer_coin_volume
            ticker['target_volume'] = target_demand_coin_volume + target_offer_coin_volume
            ticker.pop('liquidity_a')
            ticker.pop('liquidity_b')
        if not show_all:
            ticker_dicts = list(filter(lambda x: (x['pool_id'] not in [13, 15, 19, 21, 22]), ticker_dicts))
        return ticker_dicts

    def get_historical_trades(self, ticker_id, limit, offset, type, start_time, end_time):
        return [item._asdict() for item in self.db_client.get_historical_trades(ticker_id, limit, offset, type, start_time, end_time)]


# -*- coding: utf-8 -*-
"""
Created on Tue Jan 31 15:56:54 2023
This code reads a summary list file and loads a specific ROI
- calculate the cfos_value of a specific ROI according to a defined behaviour group
- save values as npz
@author: Alizee Kastler
"""

# -----------------------------------------------------------------------------

# Set Library Path
lib_path = r'C:/Repos/Social_Pain/libs'

import sys
sys.path.append(lib_path)

# Import useful libraries
import os
import numpy as np
import matplotlib.pyplot as plt
import SP_cfos as SPCFOS

# -----------------------------------------------------------------------------

# Set Summary List
summaryListFile = r'S:/WIBR_Dreosti_Lab/Alizee/LSZ1/Registration/Cfos_Summary/Cfos_Summary_512_2.xlsx'

# Set ROI Path
roi_path = 'F:/ATLAS/MASK_CFOS/PTN.tif'
roi_name = r'PTN'

#Set analysis folder and filename
analysis_folder ='S:/WIBR_Dreosti_Lab/Alizee/LSZ1/Registration/Analysis/ROI/'+ roi_name

# Create output folder (if doesn't exist)
if not os.path.exists(analysis_folder):
   os.makedirs(analysis_folder)

analysis_path_A = analysis_folder + '/Baseline_cfos.npz'
analysis_path_B = analysis_folder + '/Heat_cfos.npz'
analysis_path_C = analysis_folder + '/AITC_cfos.npz'
analysis_path_D = analysis_folder + '/Social_cfos.npz'

# -----------------------------------------------------------------------------

# Read summary list
cfos_paths, group_names = SPCFOS.read_summarylist(summaryListFile, normalized=True)
cfos_paths = np.array(cfos_paths)
group_names = np.array(group_names)

group_A = (group_names == 'Baseline')
cfos_values_A = SPCFOS.cfos_value_ROI(group_A, cfos_paths, roi_path)
np.savez(analysis_path_A, cfos_values=cfos_values_A, group_name=group_A, roi_name=roi_name)

group_B = (group_names == 'Heat')
cfos_values_B = SPCFOS.cfos_value_ROI(group_B, cfos_paths, roi_path)
np.savez(analysis_path_B, cfos_values=cfos_values_B, group_name=group_B, roi_name=roi_name)

group_C = (group_names == 'AITC')
cfos_values_C = SPCFOS.cfos_value_ROI(group_C, cfos_paths, roi_path)
np.savez(analysis_path_C, cfos_values=cfos_values_C, group_name=group_C, roi_name=roi_name)

group_D = (group_names == 'Social')
cfos_values_D = SPCFOS.cfos_value_ROI(group_D, cfos_paths, roi_path)
np.savez(analysis_path_D, cfos_values=cfos_values_D, group_name=group_D, roi_name=roi_name)

#FIN


'''
Application to scrape flights from kayak.cl (CLP is currency)
As input, a destination or list of destinations are options
As input, dates to look for flights are specified through 1st flight, staying days, and tolerance of staying days
Working with Mozilla Firefox 106.0.1 and geckodriver.exe for that Firefox version
Depending on internet speed, sleep values, must be changed for each case
Code not free of bugs

santiagovalenciam@outlook.com  01/09/2022
'''

# Librerías
# from lib2to3.pgen2 import driver
from selenium import webdriver
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.common.by import By
import pandas as pd
from time import sleep
from random import randint
from datetime import date, timedelta
import pandas as pd

def abrir_navegador():
    # Options
    options =  webdriver.FirefoxOptions()
    options.add_argument('--disable-extensions')
    options.add_argument('--disable-gpu')
    options.add_argument('--no-sandbox')
    options.add_argument('--start-maximized')
    #Driver
    driver = webdriver.Firefox(options=options) # When geckodriver.exe is in the path.
    return driver

def iniciar_pagina(driver,origen,destino,fecha_ida,fecha_vuelta,n):
    #url
    composed_url='https://www.kayak.cl/flights/'+origen+'-'+destino+'/'+fecha_ida+'/'+fecha_vuelta+'?sort=bestflight_a'
    # Open website
    driver.get(composed_url)
    print('Scraping mejor vuelo ',origen,destino,fecha_ida,fecha_vuelta)
    sleep(randint(24,30))
    #Closing pop up if required
    try:
        xp_popup_close = '//button[contains(@id,"dialog-close") and contains(@class,"Button-No-Standard-Style close ")]'
        driver.find_elements_by_xpath(xp_popup_close)[5].click()
    except Exception as e:
        pass

    #Saving scraped info in s1
    s1=scrape(driver,origen,destino,fecha_ida,fecha_vuelta,'mejor',n)

    #Same but cheapest flights
    composed_url='https://www.kayak.cl/flights/'+origen+'-'+destino+'/'+fecha_ida+'/'+fecha_vuelta+'?sort=price_a'
    driver.get(composed_url)
    print('Scraping vuelo más barato ',origen,destino,fecha_ida,fecha_vuelta)
    sleep(randint(22,30))
    #Closing pop up if required
    try:
        xp_popup_close = '//button[contains(@id,"dialog-close") and contains(@class,"Button-No-Standard-Style close ")]'
        driver.find_elements_by_xpath(xp_popup_close)[5].click()
    except Exception as e:
        pass

    s2=scrape(driver,origen,destino,fecha_ida,fecha_vuelta,'mas_barato',n)

    #Merging s1 and s2 as one for each fight
    df_parcial= s1.append(s2,ignore_index=True)

    return df_parcial

def scrape(driver,origen,destino,fecha_ida,fecha_vuelta,seleccion,n):

    # getting the Prices
    xp_prices = '//div[@class="booking"]//span[@class="price option-text"]'
    prices = driver.find_elements_by_xpath(xp_prices)
    prices_list = [0]*n
    try:
        prices_list = [price.text.replace('$','') for price in prices if price.text != '']
        prices_list = [price.replace('.','') for price in prices_list if price != '']
        prices_list = list(map(int, prices_list))
        prices_list = prices_list[:n]
    except:
        pass

    #Flight times (2 for each option)
    tiempos_vuelo = '//div[@class="section duration allow-multi-modal-icons"]/div[@class="top"]'
    times_list = driver.find_elements_by_xpath(tiempos_vuelo)
    times_list = [time_.text for time_ in times_list]
    times_list = times_list[:2*n]
    tiempo_ida=[]
    tiempo_vuelta=[]
    for i in range(len(times_list)):
        if i%2==0:
            tiempo_ida.append(times_list[i])
        else:
            tiempo_vuelta.append(times_list[i])

    #Stops (escalas)
    escalas = '//div[@class="top"]/span[@class="stops-text"]'
    escalas_list = driver.find_elements_by_xpath(escalas)
    escalas_list = [esca.text for esca in escalas_list]
    escalas_list = escalas_list[:2*n]
    escalas_ida=[]
    escalas_vuelta=[]
    for i in range(len(escalas_list)):
        if i%2==0:
            escalas_ida.append(escalas_list[i])
        else:
            escalas_vuelta.append(escalas_list[i])

    #Departure time
    salidas = '//span[@class="time-pair"]/span[@class="depart-time base-time"]'
    salidas_list = driver.find_elements_by_xpath(salidas)
    salidas_list = [sali.text for sali in salidas_list]
    salidas_list = salidas_list[:2*n]
    salidas_ida=[]
    salidas_vuelta=[]
    for i in range(len(salidas_list)):
        if i%2==0:
            salidas_ida.append(salidas_list[i])
        else:
            salidas_vuelta.append(salidas_list[i])

    #Arrival time
    llegadas = '//span[@class="time-pair"]/span[@class="arrival-time base-time"]'
    llegadas_list = driver.find_elements_by_xpath(llegadas)
    llegadas_list = [llega.text for llega in llegadas_list]
    llegadas_list = llegadas_list[:2*n]
    llegadas_ida=[]
    llegadas_vuelta=[]
    for i in range(len(llegadas_list)):
        if i%2==0:
            llegadas_ida.append(llegadas_list[i])
        else:
            llegadas_vuelta.append(llegadas_list[i])

    #Aerolines
    aerolinea = '//div[@class="section codeshares allow-airlines"]/span[@class="codeshares-airline-names"]'
    aerolinea_list = driver.find_elements_by_xpath(aerolinea)
    aerolinea_list = [aerol.text for aerol in aerolinea_list]
    aerolinea_list = aerolinea_list[:n]

    #Dataframe to save
    df_info=pd.DataFrame(columns=['fecha_ida','fecha_vuelta','origen','destino','precio','tiempo_ida','tiempo_vuelta','escalas_ida','escalas_vuelta','aerolinea','tipo_busqueda'])
    if len(prices_list)>0:
        df_info['fecha_ida']=[fecha_ida]*n
        df_info['fecha_vuelta']=[fecha_vuelta]*n
        df_info['origen']=[origen]*n
        df_info['destino']=[destino]*n
        df_info['precio']=prices_list
        df_info['tiempo_ida']=tiempo_ida
        df_info['tiempo_vuelta']=tiempo_vuelta
        df_info['escalas_ida']=escalas_ida
        df_info['escalas_vuelta']=escalas_vuelta
        df_info['salida_ida']=salidas_ida
        df_info['llegada_ida']=llegadas_ida
        df_info['salida_vuelta']=salidas_vuelta
        df_info['llegada_vuelta']=llegadas_vuelta
        df_info['aerolinea']=aerolinea_list
        df_info['tipo_busqueda']=seleccion

    return df_info

#Creating the list of dates to scrape
def lista_fechas(fecha_inicio,fecha_fin,salto,dias_estadia,dias_tolerancia):
    fecha_inicio=date(int(fecha_inicio.split('-')[2]),int(fecha_inicio.split('-')[1]),int(fecha_inicio.split('-')[0]))
    fecha_fin=date(int(fecha_fin.split('-')[2]),int(fecha_fin.split('-')[1]),int(fecha_fin.split('-')[0]))
    dates=pd.date_range(fecha_inicio,fecha_fin,freq=str(salto)+'d')
    df_busqueda=pd.DataFrame(columns=['ida','vuelta'])
    for i in dates:
        for j in range(dias_estadia-dias_tolerancia,dias_estadia+dias_tolerancia+1):
            ff=i+timedelta(days=j)
            df_busqueda=df_busqueda.append({'ida':i.strftime('%Y-%m-%d'),'vuelta':ff.strftime('%Y-%m-%d')},ignore_index=True)

    return df_busqueda

def main():
    #Inputs
    origen='SCL' #From
    destino=['RIO'] #To
    fecha_inicio='15-02-2023' #Inital date to search the first flight
    fecha_fin='15-02-2023'  #Final date to search the first flight
    salto=14 #Look for trips every x days
    dias_estadia=5 #How many days between flights to complete round-trip
    dias_tolerancia=0 #Days before and after return to search (if 1, 3 days of returning flights are searched, the day, 1 before and 1 after)
    cantidad_vuelos_por_pagina=5 #How many flights are scraped for each 'best' and 'cheapest' selection

    #Getting a dates matrix to look.
    fechas=lista_fechas(fecha_inicio,fecha_fin,salto,dias_estadia,dias_tolerancia)
    driver=abrir_navegador()

    df_resultado=pd.DataFrame()

    for i in destino:
        for j in fechas.index:
            add=iniciar_pagina(driver,origen,i,fechas.loc[j,'ida'],fechas.loc[j,'vuelta'],cantidad_vuelos_por_pagina)
            df_resultado = df_resultado.append(add ,ignore_index=True)

    #At the end...
    print('Finalizado, guardando resultados')
    #Saving results in the same folder
    df_resultado.to_excel('resultados_scrape.xlsx',index=False)
    #Showing results
    print(df_resultado)

if __name__=='__main__':
    main()


# # # -*- coding: utf-8 -*-
# # """
# # Created on Fri Nov  4 14:26:26 2022

# # @author: vegard
# # """

import pandas as pd
import os
import numpy as np
import tensorflow as tf
import keras
from utils import *
from regression_classifier import get_regression_classifier
from weak_learner import get_weak_learner
from keras.layers import Dense,RepeatVector,Lambda,multiply,Activation,Input, LSTM, GRU, Dropout, Flatten,BatchNormalization, Permute
from keras.models import Model
from scikeras.wrappers import KerasRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.utils import shuffle
from keras import backend as K

# # =============================================================================
# # Pre Processing
# # =============================================================================

nof_stocks = 88
root = "./stocknet-dataset-master"
price_dir = os.path.join(root, 'price/raw')
price_files = os.listdir(price_dir)
price_files = [file for file in price_files if file != "BRK-A.csv"] # skip the BRK-A stock
dfs = []
for f in price_files[:nof_stocks]:
    df = pd.read_csv(os.path.join(price_dir, f))
    df['Name'] = os.path.splitext(f)[0]
    dfs.append(df)
df_stocks_p = pd.concat(dfs, ignore_index=True)

tweets_dir = os.path.join(root, 'tweet/preprocessed/')

# df_tweets = getTweetsRaw(tweets_dir, nof_stocks)
df_tweets = getTweetsPreprocessed(tweets_dir, nof_stocks)

df_stocks = pd.merge(df_stocks_p, df_tweets, on=['Name', 'Date'], how='outer')
df_stocks = df_stocks.dropna(subset=['Open'])

df_stocks['Diff'] = df_stocks['Adj Close'].rolling(window=2).apply(lambda x: movementPercentage(x.values[0], x.values[1]))
#filter data where difference between adjusted closing price day d and d-1 > 0.5% or < -0.5%
df_stocks = df_stocks.loc[(df_stocks['Diff'] < - 0.5) | (df_stocks['Diff'] > 0.5)]

df_stocks.loc[df_stocks['Diff'] < -0.5, 'Label'] = 0
df_stocks.loc[df_stocks['Diff'] > 0.5, 'Label'] = 1

df_stocks['text'] = df_stocks['text'].apply(lambda x: join_text(x))
# apply vader sentiment analysis to tweets
df_stocks['text'] = df_stocks['text'].apply(sentiment_score)
# # use only compound score from sentiment analyzis
df_stocks['text'] = df_stocks['text'].apply(lambda x: x['compound'])

# count number of true or false labels
print(df_stocks.groupby(['Label']).size())

# split into training and testing
training = df_stocks.groupby('Name', as_index = False).apply(lambda x: x[:int(len(x)*0.8)])
testing = df_stocks.groupby('Name', as_index = False).apply(lambda x: x[int(len(x)*0.8):])

price = training.iloc[:,1:6]
# show heatmap of correlation between stock price features
show_heatmap(price)

# normalize
maximum = np.max(np.max(training.iloc[:,1:6]).values)
minimum = np.min(np.min(training.iloc[:,1:6]).values)

mean = (training.iloc[:,1:6]).values.mean(axis=0)
std = (training.iloc[:,1:6]).values.std(axis=0)

training.iloc[:, 1:6] = training.iloc[:, 1:6].apply(lambda x: normalize2(x.values, minimum, maximum))
testing.iloc[:, 1:6] = testing.iloc[:, 1:6].apply(lambda x: normalize2(x.values, minimum, maximum))

window_size = 5
x_train = training.groupby('Name').apply(lambda x: get_windows(x, window_size)).values
x_test = testing.groupby('Name').apply(lambda x: get_windows(x, window_size)).values
# y_train = training.groupby('Name').apply(lambda x: get_labels(x, window_size+1)).values
labels_test = testing.groupby('Name').apply(lambda x: get_labels(x, window_size+1)).values
labels_train = training.groupby('Name').apply(lambda x: get_labels(x, window_size+1)).values

y_train = training.groupby('Name').apply(lambda x: x[window_size:]).values[:,5]
y_test = testing.groupby('Name').apply(lambda x: x[window_size:]).values[:,5]

# # # reshape
x_train = np.array([tf.convert_to_tensor((val), dtype=np.float64) for sublist in x_train for val in sublist])
x_test = np.array([tf.convert_to_tensor(list(val), dtype=np.float64) for sublist in x_test for val in sublist])
labels_test = np.array([tf.convert_to_tensor([val], dtype=np.float64) for sublist in labels_test for val in sublist])
labels_train = np.array([tf.convert_to_tensor([val], dtype=np.float64) for sublist in labels_train for val in sublist])

y_train = [[i] for i in y_train]
y_train = np.array(y_train, dtype=np.float64)
y_test = np.array(y_test, dtype=np.float64)
x_train, y_train = shuffle(x_train, y_train, random_state=0)
n_features = 6

# =============================================================================
# Regression classifier implementation
# =============================================================================

clf = get_regression_classifier([window_size,n_features],128)

# using custom loss function
clf.fit(x_train, np.append(y_train, x_train[:,:,4], axis=1), epochs=500, batch_size = 32, validation_data=(x_test, y_test))

# using regular mse
# clf.fit(x_train, y_train, epochs=425, batch_size = 32, validation_data=(x_test, y_test))

pred = clf.predict(x_test)

predictions = pd.DataFrame(pred)
pred = []
# append 1 to account for not having a movement prediction on first day
pred.append(1)
for i in predictions.rolling(window=2):
    if(len(i) != 2):
        continue
    if(i.values[0]<i.values[1]):
        pred.append(1)
    else:
        pred.append(0)
acc = 0
for i in range(len(pred)):
    if(labels_test[i] == pred[i]):
        acc += 1

print("Accuracy: ", acc/(len(labels_test)-1))

# # =============================================================================
# # AdaBoost GRU Implementation
# # =============================================================================

regressor = get_weak_learner([window_size,n_features],128)

regressor = KerasRegressor(regressor, epochs = 60, batch_size = 32)
rgr = AdaBoostRegressor(base_estimator=regressor, learning_rate=0.001, n_estimators=50)
rgr.fit(x_train, y_train)

print("R2 score: ", rgr.score(x_test, y_test))

predictions = rgr.predict(x_test)

predictions = pd.DataFrame(predictions)

mae = tf.keras.losses.MeanAbsoluteError()

print("MAE: ", mae(predictions, y_test).numpy())

print("RMSE: ", root_mean_squared_error(predictions.values, y_test).numpy())


# -*- coding: utf-8 -*-
"""
Created on Mon Nov 29 10:17:55 2021

@author: ruben
"""

import numpy as np
import matplotlib.pyplot as plt

class Simulation():
    def __init__(self, system, n_steps, n_eqs, h):
        #Configurações iniciais
        self.n_steps = n_steps
        self.n_eqs = n_eqs
        self.n_rep = self.n_steps - 1
        self.h = h #step size (time)
        self.system = system

    #Definição do sistema de equações:
    def _F(self, t, n, n_0, i):
        n_ = np.copy(n)
        for j in range(self.n_eqs):
            n_[j][i] = n[j][i] + n_0[j]
        f = np.zeros(self.n_eqs)

        f[0] = self.system.R_prime_pump*(1 - n_[0][i]) - (self.system.tau_p/self.system.tau_21)*n_[0][i]*(1 + 2*n_[1][i])

        f[1] = - n_[1][i] + (n_[0][i]/self.system.n_th)*(n_[1][i] + self.system.eta_SE)

        return f

    #Constantes definidas no método de Runge-Kutta

    def _update_n(self, n,k1,k2,k3,k4,i):
        for j in range(self.n_eqs):
            n[j][i+1] = n[j][i] + (k1[j] + 2*k2[j] + 2*k3[j] + k4[j])/6

    def _calculate(self, n, t):
        #matriz-solução; tempos discretos
        for i in range(self.n_rep):
            k1= self._F(t[i]            , n , np.zeros(self.n_eqs)  , i)*self.h
            k2= self._F(t[i]+self.h/2   , n , k1/2                  , i)*self.h
            k3= self._F(t[i]+self.h/2   , n , k2/2                  , i)*self.h
            k4= self._F(t[i]+self.h     , n , k3                    , i)*self.h

            self._update_n(n,k1,k2,k3,k4,i)
            t[i+1] = t[i] + self.h

        return t,n

    def run(self, init_cond):
        n = np.zeros((self.n_eqs,self.n_steps))
        t = np.zeros(self.n_steps)
        #Condições iniciais das variáveis
        n[0][0] = init_cond[0]
        n[1][0] = init_cond[1]

        return self._calculate(n, t)

class System():
    def __init__(self, system):
        if system == 'Nd:YAG':
            #Constantes prévias
            R1 = 1
            R2 = 0.95
            l_g = 1 #m
            sigma_0 = 5.92e-23 #m²
            N = 1e25 #part/m³
            tau = 3e-6 #s
            R_pump = 1.8 #s-1
            #Definições do sistema
            self.tau_p = tau/(1-R1*R2)
            self.R_prime_pump = R_pump*self.tau_p
            self.tau_21 = 1.2e-3
            self.eta_SE = 1e-12
            self.n_th = (1-R1*R2)/(2*l_g*sigma_0*N)

# SISTEMA E PARÂMETROS DA SIMULAÇÃO
NdYAG = System('Nd:YAG')
Sim = Simulation(NdYAG,1200,2,15)

def plotPop(t, n, labels=None, normalize=False):
    if normalize == True:
        for i in range(len(n)):
            n[i] = (np.asarray(n[i])/max(n[i])).tolist()
    fig, ax = plt.subplots(2,1)
    if labels==None: labels=range(len(n))
    for i in range(len(n)):
        ax[i].plot(t, np.asarray(n[i]),'-',label=labels[i])
        ax[i].set_xlabel(r't$^\prime$ ($t/\tau_p$)', size='x-large')
        ax[i].tick_params(direction='in',which='both')

    ax[0].set_ylabel('$N_2/N$', size='x-large')
    ax[1].set_ylabel('$I/I_{sat}$', size='x-large')

def runSimulation():
    t,n = Sim.run([0,0])
    plotPop(t,n, normalize=False)

runSimulation()


# -*- coding:utf-8 -*-
class Solution:
    def NumberOf1Between1AndN_Solution(self, n):
        if n <= 0 : return 0

        operator = 1
        count = 0

        while n // operator:
            curr = n % (operator * 10) // operator
            high = n // (operator * 10)
            low = n % (operator)

            # 根据当前位的3种情况，进行计数
            if curr == 1:
                count += high * operator + low + 1
            if curr == 0:
                count += high * operator
            if curr > 1:
                count += (high+1) * operator

            operator *= 10

        return count


import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.autograd import Variable

class focal_loss(nn.Module):
    def __init__(self, alpha=0.25, gamma=2, num_classes=3, size_average=True):
        super(focal_loss, self).__init__()
        self.size_average = size_average
        if isinstance(alpha, list):
            assert len(alpha) == num_classes
            self.alpha = torch.Tensor(alpha)
        else:
            assert alpha < 1
            self.alpha = torch.zeros(num_classes)
            self.alpha[0] += alpha
            self.alpha[1:] += (1 - alpha)

        self.gamma = gamma

    def forward(self, preds, labels):
        # assert preds.dim()==2 and labels.dim()==1
        preds = preds.view(-1, preds.size(-1))
        self.alpha = self.alpha.to(preds.device)
        preds_softmax = F.softmax(preds, dim=1)
        preds_logsoft = torch.log(preds_softmax)

        # focal_loss func, Loss = -α(1-yi)**γ *ce_loss(xi,yi)
        preds_softmax = preds_softmax.gather(1, labels.view(-1, 1))
        preds_logsoft = preds_logsoft.gather(1, labels.view(-1, 1))
        self.alpha = self.alpha.gather(0, labels.view(-1))
        # torch.pow((1-preds_softmax), self.gamma) 为focal loss中 (1-pt)**γ
        loss = -torch.mul(torch.pow((1 - preds_softmax), self.gamma), preds_logsoft)

        loss = torch.mul(self.alpha, loss.t())
        if self.size_average:
            loss = loss.mean()
        else:
            loss = loss.sum()
        return loss

class FocalLoss(nn.Module):
    r"""
        This criterion is a implemenation of Focal Loss, which is proposed in
        Focal Loss for Dense Object Detection.

            Loss(x, class) = - \alpha (1-softmax(x)[class])^gamma \log(softmax(x)[class])

        The losses are averaged across observations for each minibatch.

        Args:
            alpha(1D Tensor, Variable) : the scalar factor for this criterion
            gamma(float, double) : gamma > 0; reduces the relative loss for well-classiﬁed examples (p > .5),
                                   putting more focus on hard, misclassiﬁed examples
            size_average(bool): By default, the losses are averaged over observations for each minibatch.
                                However, if the field size_average is set to False, the losses are
                                instead summed for each minibatch.

    """
    def __init__(self, class_num, alpha=None, gamma=2, size_average=True):
        super(FocalLoss, self).__init__()
        if alpha is None:
            self.alpha = Variable(torch.ones(class_num, 1))
        else:
            if isinstance(alpha, Variable):
                self.alpha = alpha
            else:
                self.alpha = Variable(alpha)
        self.gamma = gamma
        self.class_num = class_num
        self.size_average = size_average

    def forward(self, inputs, targets):
        N = inputs.size(0)
        C = inputs.size(1)
        P = F.softmax(inputs,dim=1)

        class_mask = inputs.data.new(N, C).fill_(0)
        class_mask = Variable(class_mask)
        ids = targets.view(-1, 1)
        class_mask.scatter_(1, ids.data, 1.)
        #print(class_mask)

        if inputs.is_cuda and not self.alpha.is_cuda:
            self.alpha = self.alpha.cuda()
        alpha = self.alpha[ids.data.view(-1)]

        probs = (P*class_mask).sum(1).view(-1,1)

        log_p = probs.log()
        #print('probs size= {}'.format(probs.size()))
        #print(probs)

        batch_loss = -alpha*(torch.pow((1-probs), self.gamma))*log_p
        #print('-----bacth_loss------')
        #print(batch_loss)

        if self.size_average:
            loss = batch_loss.mean()
        else:
            loss = batch_loss.sum()
        return loss

class LabelSmoothing(nn.Module):
    """
    NLL loss with label smoothing.
    """
    def __init__(self, smoothing=0.0):
        """
        Constructor for the LabelSmoothing module.
        :param smoothing: label smoothing factor
        """
        super(LabelSmoothing, self).__init__()
        self.confidence = 1.0 - smoothing
        self.smoothing = smoothing

    def forward(self, x, target):
        logprobs = torch.nn.functional.log_softmax(x, dim=-1)

        nll_loss = -logprobs.gather(dim=-1, index=target.unsqueeze(1))
        nll_loss = nll_loss.squeeze(1)
        smooth_loss = -logprobs.mean(dim=-1)
        loss = self.confidence * nll_loss + self.smoothing * smooth_loss
        return loss.mean()


import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
import os
import h5py
import multiprocessing
import time
import get_template

Finder = get_template.Finder()

Roman_joint_hf = h5py.File('data/Rubin_Roman_gal_cross_match.h5', 'r')
joint_gal_id = Roman_joint_hf['gal_id'][:]
Roman_gal_idx = Roman_joint_hf['roman_id_idx'][:]

def worker(return_dict,gal_id,gal_idx,tag):
    Roman_mag_list = []
    Rubin_mag_list = []
    name_list = []
    cross_idx = []
    for i in range(len(gal_id)):
        Roman_mags = Finder.calc_roman_color(gal_id=gal_id[i], roman_gal_idx=gal_idx[i],check = False)
        Rubin_mags = Finder.calc_color(gal_id=gal_id[i],colors = ['u','g','r','i','z','y'])
        Roman_mag_list.append(Roman_mags)
        Rubin_mag_list.append(Rubin_mags)
        name_list.append(gal_id)
        cross_idx.append(gal_idx)

    return_dict[tag] = (Roman_mag_list,Rubin_mag_list,name_list)

if __name__ == "__main__":
    start_time = time.perf_counter()
    manager = multiprocessing.Manager()
    return_dict = manager.dict()
    jobs = []
    for i_start in range(0,10,10):
        s = slice(i_start,i_start + 10)
    #for i in range(64*8):
        p = multiprocessing.Process(target=worker,
                                    args=(return_dict,joint_gal_id[s],
                                        Roman_gal_idx[s],i_start))
        jobs.append(p)
        p.start()

    for proc in jobs:
        proc.join()
    finish_time = time.perf_counter()
    print(f"Program finished in {finish_time-start_time} seconds")

    Roman_mags = list()
    Rubin_mags = list()
    Gal_id = list()
    cross_id = list()
    for i_start in return_dict.keys():
        Roman_mags.extend(return_dict[i_start][0])
        Rubin_mags.extend(return_dict[i_start][1])
        Gal_id.extend(return_dict[i_start][2][0])
        cross_id.extend(return_dict[i_start][3][0])

    np.save('data/gal_id', np.array(Gal_id))
    np.save('data/Rubin_mags', np.array(Rubin_mags))
    np.save('data/roman_mags', np.array(Roman_mags))
    np.save('data/cross_id', np.array(cross_id))


from typing import Dict, List

import jax
import numpy as onp
from jax import numpy as jnp
from matplotlib import pyplot as plt
from tqdm.auto import tqdm

jax_logdets = {
    "slogdet": lambda A: jnp.linalg.slogdet(A)[1],
    "cholesky": lambda A: 2 * jnp.sum(jnp.log(jnp.diag(jnp.linalg.cholesky(A)))),
    "eigh": lambda A: jnp.sum(jnp.log(jnp.linalg.eigvalsh(A))),
}
jax_logdets = {k: jax.jit(v) for k, v in jax_logdets.items()}

stds = 10.0 ** (-onp.arange(0.0, 10.0, 0.1))
delta_means: Dict[str, List[float]] = {}
delta_std_errors: Dict[str, List[float]] = {}
for method, logdet in jax_logdets.items():
    delta_means[method] = []
    delta_std_errors[method] = []
    for std in stds:
        delta_list = []
        for _ in tqdm(range(5000)):
            X = onp.random.normal(scale=std, size=(3, 3))
            A = X.T @ X
            _, logdet_64 = onp.linalg.slogdet(A)
            # logdet_64 = 2 * onp.sum(onp.log(onp.diag(onp.linalg.cholesky(A))))
            # logdet_64 = logdet_64 ** 2
            assert logdet_64.dtype == onp.float64
            logdet_32 = onp.array(logdet(A), dtype=onp.float64)
            delta_list.append(onp.abs(logdet_64 - logdet_32))

        delta_array = onp.array(delta_list)
        delta_array = delta_array[onp.logical_not(onp.isnan(delta_list))]
        delta_array = delta_array[onp.logical_not(onp.isinf(delta_list))]
        delta_array = delta_array[onp.logical_not(onp.isneginf(delta_list))]
        delta_means[method].append(onp.mean(delta_array))
        delta_std_errors[method].append(
            onp.std(delta_list) / onp.sqrt(len(delta_array))  # Standard error
        )

for method, delta_list in delta_means.items():
    assert len(stds) == len(delta_list)
    delta_array = onp.array(delta_list)
    delta_std_error_array = onp.array(delta_std_errors[method])

    plt.plot(stds, delta_list, label=method)
    plt.fill_between(
        stds,
        y1=delta_array - delta_std_error_array,
        y2=delta_array + delta_std_error_array,
        alpha=0.1,
    )
    print(delta_list)

plt.xscale("log")
# plt.yscale("log")
plt.legend()
plt.show()


import discord
import requests
from websockets import connect

import time
import asyncio

class MyClient(discord.AutoShardedClient):

    def __init__(self, *args, **kwargs):
        self.shard_ok = False
        while not self.shard_ok:
            self.shard_ok = self.check_shard(*args, **kwargs)
            if not self.shard_ok:
                time.sleep(5)

    def check_shard(self, *args, **kwargs):
        r = requests.get("http://localhost:8080/shards", timeout=5)
        payload = r.json()
        kwargs["shard_count"] = payload["shard_count"]
        if not payload["full"]:
            kwargs["shard_ids"] = [payload["shard_id"]]
            super().__init__(*args, **kwargs)
            return True
        return False

    async def heartbeat(self):
        self.ws = await connect("ws://localhost:8080/heartbeat")
        while True:
            await self.ws.recv()
            await self.ws.send("ping")
            await asyncio.sleep(2)


class TreeNode:
     def __init__(self, x):
         self.val = x
         self.left = None
         self.right = None

class Solution:
    # @param root, a tree node
    # @return a list of lists of integers
    def levelOrderBottom(self, root):
        if not root:
            return []
        current,res = [],[]
        current.append(root)
        while current:
            next,val = [], []
            for node in current:
                val.append(node.val)
                if node.left:
                    next.append(node.left)
                if node.right:
                    next.append(node.right)
            res.insert(0,val)
            current = next
        return res