import json
import os
import allure
import pytest
from common.handle_test import Handle
from config import Conf
from config.Conf import ConfigYaml
from utils.Assert import Assertions
from utils.YamlUtil import YamlReader

getDataIndexValueTreeList = os.path.join(Conf.get_data_path(), "TITAN_12getDataIndexValueTreeList.yml")
data_index_value_tree_list = YamlReader(getDataIndexValueTreeList).read_data_all()

def get_indicator_data(response_data):
    conf_indicator_list = ConfigYaml().get_titan_info()
    for indicator_name in conf_indicator_list:
        test_indicator_list = list()
        file = Conf.get_titan_indicator_path() + os.sep + indicator_name + ".txt"
        with open(file, "r", encoding="utf-8") as f:
            standard_indicator_list = eval(f.read())
            for first_data in response_data:
                disease_title = first_data["title"]
                if disease_title == indicator_name:
                    if "children" in first_data and first_data["children"]:
                        for second_data in first_data["children"]:
                            if second_data["title"] is None:
                                second_data["title"] = " "
                            primary_classification_title = second_data["title"]
                            if "children" in second_data and second_data["children"]:
                                for third_data in second_data["children"]:
                                    if third_data["title"] is None or "title" not in third_data.keys():
                                        secondary_classification_title = " "
                                    else:
                                        secondary_classification_title = third_data["title"]
                                    if "children" in third_data and third_data["children"]:
                                        for fourth_data in third_data["children"]:
                                            if "title" not in fourth_data.keys():
                                                fourth_data["title"] = " "
                                            elif fourth_data["title"] is None:
                                                fourth_data["title"] = " "
                                            tertiary_classification_title = fourth_data["title"]
                                            if "children" in fourth_data and fourth_data["children"]:
                                                for fifth_data in fourth_data["children"]:
                                                    if "title" not in fifth_data.keys():
                                                        fifth_data["title"] = " "
                                                    elif fifth_data["title"] is None:
                                                        fifth_data["title"] = " "
                                                    fourth_classification_title = fifth_data["title"]
                                                    if "children" in fifth_data and fifth_data["children"]:
                                                        for sixth_data in fifth_data["children"]:
                                                            if "title" not in sixth_data.keys():
                                                                sixth_data["title"] = " "
                                                            elif sixth_data["title"] is None:
                                                                sixth_data["title"] = " "
                                                            fifth_classification_title = sixth_data["title"]
                                                            if "children" in sixth_data and sixth_data["children"]:
                                                                for seventh_data in sixth_data["children"]:
                                                                    if seventh_data["title"] is None:
                                                                        seventh_data["title"] = " "
                                                                    seventh_classification_title = seventh_data["title"]
                                                                    title = disease_title + " -- " + primary_classification_title + " -- " + secondary_classification_title + " -- " + tertiary_classification_title + " -- " + fourth_classification_title + " -- " + fifth_classification_title + " -- " + seventh_classification_title
                                                                    test_indicator_list.append(title)
                                                            else:
                                                                title = disease_title + " -- " + primary_classification_title + " -- " + secondary_classification_title + " -- " + tertiary_classification_title + " -- " + fourth_classification_title + " -- " + fifth_classification_title
                                                                test_indicator_list.append(title)
                                                    else:
                                                        title = disease_title + " -- " + primary_classification_title + " -- " + secondary_classification_title + " -- " + tertiary_classification_title + " -- " + fourth_classification_title
                                                        test_indicator_list.append(title)
                                            else:
                                                title = disease_title + " -- " + primary_classification_title + " -- " + secondary_classification_title + " -- " + tertiary_classification_title
                                                test_indicator_list.append(title)
                                    else:
                                        title = disease_title + " -- " + primary_classification_title + " -- " + secondary_classification_title
                                        test_indicator_list.append(title)
            Assertions().assert_in_titan_data(standard_indicator_list, test_indicator_list, indicator_name)
    # return test_indicator_list, standard_indicator_list

# title_list = list()
#
# def get_data(first_data, data_list, title3):
#     if first_data["title"] is None or "title" not in first_data.keys():
#         title1 = ""
#     else:
#         title1 = first_data["title"]
#     if "children" in first_data and first_data["children"]:
#         for second_data in first_data["children"]:
#             # if second_data["title"] is None or "title" not in second_data.keys():
#             #     title2 = ""
#             # else:
#             #     title2 = second_data["title"]
#             title3 = title3 + "--" + title1
#             return get_data(second_data, data_list, title3)
#     else:
#         data_list.append(title3)
#         # b = "--".join(data_list)
#         # data_list.append(b)
#         return data_list

# def test(data):
#     test_list = list()
#     title = ""
#     for i in data:
#         p = i
#         # while "child" in p.keys():
#         while "children" in p and p["children"]:
#             title = p["title"] + "--" + title
#             p = p["children"]
#         test_list.append(title)
#     return test_list

# def get_indicator_data(response_data):
#     conf_indicator_list = ConfigYaml().get_titan_info()
#     for indicator_name in conf_indicator_list:
#         test_indicator_list = list()
#         file = Conf.get_titan_indicator_path() + os.sep + indicator_name + ".txt"
#         with open(file, "r", encoding="utf-8") as f:
#             standard_indicator_list = eval(f.read())
#             for first_data in response_data:
#                 disease_title = first_data["title"]
#                 if disease_title == indicator_name:
#                     test_indicator_list = test(first_data)
#                     print(test_indicator_list)
#             Assertions().assert_in_titan_data(standard_indicator_list, test_indicator_list, indicator_name)

@allure.feature("Titan筛选")
class TestTitan:
    @pytest.mark.parametrize("index_value_tree_list", data_index_value_tree_list)
    def test_data_index_value_tree_list(self, index_value_tree_list):
        body = Handle().handle_res_body(index_value_tree_list)
        response_data = body["responseData"]
        # test_indicator_list, standard_indicator_list = get_indicator_data(response_data)
        get_indicator_data(response_data)
        # print(test_indicator_list)
        # write_titan_file(test_indicator_list)
        # standard_indicator_list = read_titan_file()
        # Assertions().assert_in_titan_data(test_indicator_list, standard_indicator_list)


# ===========================================================================
# +++primitives.py+++|
# ___________________|
#
# Author: Sean Frischmann
# Class: Cse 305
# ===========================================================================
import sys

def get_local(value):
	local = ''
	i = 1
	isString = False
	while i < len(value):
		if value[i] == '"':
			if isString:
				isString = False
			else:
				isString = True
		if value[i] == ',' and (not isString):
			break
		local = local + value[i]
		i += 1
	return local

def get_not_local(value):
	ret = ''
	i = 1
	isBuild = False
	isString = False
	while i < len(value):
		if value[i] == '>' and isString == False:
			break
		if value[i] == '"':
			if isString == True:
				isString = False
			else:
				isString = True
		if value[i] == ',' and isBuild == False and isString == False:
			i += 1
			isBuild = True
			continue
		if isBuild:
			ret = ret + value[i]
		i += 1
	return ret

def if_func(env):
	ret = True
	if len(env['stack']) >= 3:
		topValue = env['stack'][0]
		if env['stack'][0][0] == '<':
			topValue = get_not_local(env['stack'][0])
		if topValue == ':true:':
			env['stack'].pop(1)
			env['stack'].pop(0)
		elif topValue == ':false:':
			env['stack'].pop(2)
			env['stack'].pop(0)
		else:
			ret = False
	else:
		ret = False
	return ret

def add_func(env):
	topValue = env['stack'][0]
	bottomValue = env['stack'][1]
	if topValue[0] == '<':
		topValue = get_not_local(topValue)
	if bottomValue[0] == '<':
		bottomValue = get_not_local(bottomValue)
	return int(topValue) + int(bottomValue)

def sub_func(env):
	topValue = env['stack'][0]
	bottomValue = env['stack'][1]
	if topValue[0] == '<':
		topValue = get_not_local(topValue)
	if bottomValue[0] == '<':
		bottomValue = get_not_local(bottomValue)
	return int(bottomValue) - int(topValue)

def div_func(env):
	topValue = env['stack'][0]
	bottomValue = env['stack'][1]
	if topValue[0] == '<':
		topValue = get_not_local(topValue)
	if bottomValue[0] == '<':
		bottomValue = get_not_local(bottomValue)
	temp = 0
	temp = int(bottomValue) // int(topValue)
	if int(bottomValue) < 0 and int(topValue) < 0:
		temp = temp + 1
	return temp

def rem_func(env):
	topValue = env['stack'][0]
	bottomValue = env['stack'][1]
	if topValue[0] == '<':
		topValue = get_not_local(topValue)
	if bottomValue[0] == '<':
		bottomValue = get_not_local(bottomValue)
	temp = 0
	if int(bottomValue) < 0 and int(topValue) < 0:
		temp2 = int(bottomValue) // int(topValue)
		temp2 = temp2 + 1
		temp = int(bottomValue) - (temp2*int(topValue))
	else:
		temp = int(bottomValue) % int(topValue)
	return temp

def mul_func(env):
	topValue = env['stack'][0]
	bottomValue = env['stack'][1]
	if topValue[0] == '<':
		topValue = get_not_local(topValue)
	if bottomValue[0] == '<':
		bottomValue = get_not_local(bottomValue)
	return int(bottomValue) * int(topValue)

def neg_func(env):
	topValue = env['stack'][0]
	if topValue[0] == '<':
		topValue = get_not_local(topValue)
	return int(topValue) * int(-1)

def equal_func(env):
	topValue = env['stack'][0]
	topLocal = ''
	topNot = ''
	bottomValue = env['stack'][1]
	bottomLocal = ''
	bottomNot = ''
	isBinding = False
	if topValue[0] == '<':
		topLocal = get_local(topValue)
		topNot = get_not_local(topValue)
		isBinding = True
	if bottomValue[0] == '<':
		bottomLocal = get_local(bottomValue)
		bottomNot = get_not_local(bottomValue)
		isBinding = True
	if (topValue == ':closure:' or bottomValue == ':closure:'
			or topNot == ':closure:' or bottomNot == ':closure:'):
		return False
	if isBinding:
		if (topLocal == bottomLocal and topNot == bottomNot):
			return True
		else:
			return False
	else:
		if topValue == bottomValue:
			return True
		else:
			return False

def len_func(env):
	topValue = env['stack'][0]
	if topValue[0] == '<':
		topValue = get_not_local(topValue)
	if topValue[0] == '"':
		ret = len(topValue) - 2
		ret = str(ret)
		env['stack'].insert(0,ret)
		return True
	else:
		return False

def cat_func(env):
	topValue = env['stack'][0]
	bottomValue = env['stack'][1]
	if topValue[0] == '<':
		topValue = get_not_local(topValue)
	if bottomValue[0] == '<':
		bottomValue = get_not_local(bottomValue)
	if topValue[0] == '"' and bottomValue[0] == '"':
		ret = bottomValue + topValue
		ret = ret.replace('"','')
		ret = '"'+ret+'"'
		env['stack'].pop(0)
		env['stack'].pop(0)
		env['stack'].insert(0,ret)
		return True
	else:
		return False

def less_func(env):
	topValue = env['stack'][0]
	bottomValue = env['stack'][1]
	if topValue[0] == '<':
		topValue = get_not_local(topValue)
	if bottomValue[0] == '<':
		bottomValue = get_not_local(bottomValue)
	if isinstance(int(bottomValue),int) and isinstance(int(topValue),int):
		if int(bottomValue) < int(topValue):
			env['stack'].insert(0,':true:')
		else:
			env['stack'].insert(0,':false:')
		env['stack'].pop(1)
		env['stack'].pop(1)
		return True
	else:
		return False


# -*- coding:utf-8 -*-

class UnionFind():
    def __init__(self, N):
        self.parent = [-1 for i in range(N)]  # 親のときは負の値をとり、-(そのグループのサイズ)

    def find(self, x):
        """ xがどのグループに属するかを返す """
        if self.parent[x] < 0: return x

        self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def unite(self, x, y):
        """ xとyのグループを併合する """
        gx = self.find(x)
        gy = self.find(y)

        if gx == gy: return

        if self.size(gx) < self.size(gy):
            self.parent[gy] -= self.size(gx)
            self.parent[gx] = gy
        else:
            self.parent[gx] -= self.size(gy)
            self.parent[gy] = gx

    def size(self, x):
        """ xが属するグループのサイズ """
        gx = self.find(x)
        return -self.parent[gx]

    def is_same_group(self, x, y):
        """ xとyが同じグループに属するかを調べる """
        return self.find(x) == self.find(y)

def solve():
    """ 方針
    ・「つなげる処理は簡単。切り離す処理は難しい」

    すべてが崩落している状態から、 時間を巻き戻すように橋をかけていき不便度を計算していく。

    UnionFindで、島aと島bが同じグループに属するということは、いずれかの橋を渡っていけば島aから島bまで到達可能という意味
    """
    N, M = list(map(int, input().split()))
    A, B = [], []
    for m in range(M):
        a, b = list(map(int, input().split()))
        A.append(a-1)
        B.append(b-1)

    uf = UnionFind(N)
    ans = [(N-1)*N/2] * (M) # すべてが崩落した状態を初期状態とする

    for i in range(M-1, 0, -1):
        if not uf.is_same_group(A[i], B[i]):
            # 同じグループではないので、つながっていない
            # ので、つなげる
            ans[i-1] = ans[i] - uf.size(A[i]) * uf.size(B[i])
            uf.unite(A[i], B[i])
        else:
            ans[i-1] = ans[i]

    for i in range(M):
        print(int(ans[i]))

if __name__ == "__main__":
    solve()


import os
from pprint import pprint

from pytorch_lightning import Trainer, seed_everything
from pytorch_lightning.callbacks import LearningRateMonitor
from pytorch_lightning.loggers import WandbLogger

from MV_MA_SSL.args.setup import parse_args_pretrain
from MV_MA_SSL.methods import METHODS
from MV_MA_SSL.utils.auto_resumer import AutoResumer
import torch
import pickle
from torchvision import datasets

try:
    from MV_MA_SSL.methods.dali import PretrainABC
except ImportError as e:
    print(e)
    _dali_avaliable = False
else:
    _dali_avaliable = True

try:
    from MV_MA_SSL.utils.auto_umap import AutoUMAP
except ImportError:
    _umap_available = False
else:
    _umap_available = True

import types
from MVAR_Dino.main_mvar_dino import DataAugmentationMVAR_DINO
from MV_MA_SSL.utils.checkpointer import Checkpointer
from MV_MA_SSL.utils.classification_dataloader import prepare_data as prepare_data_classification
from MV_MA_SSL.utils.pretrain_dataloader import (
    prepare_dataloader,
    prepare_datasets,
    prepare_n_crop_transform_mv_ma,
    prepare_transform,
)

from MV_MA_SSL.utils.value_schedule import (
    Alpha_schedule,
    WD_cosine_schedule,
    Beta_schedule,
    batch_size_schedule,
    Stochastic_Weight_Avg
)

## For multiNode training bash
#--network host
#MASTER_ADDR=10.0.0.6 MASTER_PORT=3300 NODE_RANK=1 WORLD_SIZE=2 bash DINO_full_data.sh
def main():
    seed_everything(5)

    args = parse_args_pretrain()

    assert args.method in METHODS, f"Choose from {METHODS.keys()}"
    #args.method=="massl"
    print(args.num_large_crops)
    # if args.num_large_crops != 2:
    #     ## Adding for multi-Views
    #     assert args.method in METHODS#=="wmse"

    MethodClass = METHODS[args.method]
    if args.dali:
        assert (
            _dali_avaliable
        ), "Dali is not currently avaiable, please install it first with [dali]."
        MethodClass = types.new_class(f"Dali{MethodClass.__name__}", (PretrainABC, MethodClass))

    model = MethodClass(**args.__dict__)

    # pretrain dataloader
    # note that, to support plugin, i modify the prepare_transform func (i didn't unpack the dict!!)
    if not args.dali:
        # asymmetric augmentations
        if args.unique_augs >= 1:  # note : --brightness 0.4 0.4 0.4 0.4 \  # 4 params to bypass inner chk mechnaism in sh file
            # pluggin proposed multiple-DA
            if args.dataset == "mulda" or "mulda_v1" or "mv_ma":
                transform = prepare_transform(args.dataset, args.num_augment_trategy,args.transform_kwargs, args.mulda_kwargs)
            else: # normal case, this way plz ~ ~
                transform = [
                    prepare_transform(args.dataset, kwargs) for kwargs in args.transform_kwargs
                ]
        else:
            transform = [prepare_transform(args.dataset, args.transform_kwargs)]
        ## My Goal is Get X--> Crop it --> Two crop --> Transform

        transform = prepare_n_crop_transform_mv_ma(transform,  num_crops_per_aug=args.num_crops_per_aug,num_crop_glob=args.num_crop_glob, crop_size_glob=args.crop_size_glob,
                                               num_crop_loc=args.num_crop_loc, crop_size_loc=args.crop_size_loc, crop_type=args.crop_type,
                                               min_loc=args.min_scale_loc, max_loc=args.max_scale_loc,  min_glob=args.min_scale_glob, max_glob=args.max_scale_glob, shuffle_crop_transform=args.shuffle_transforms_crops
                                               )

        if args.debug_augmentations:
            print("Transforms:")
            pprint(transform)

        if args.subset_classes:
            print(f"Using Subset Dataset with {args.subset_classes} Classes")

        train_dataset = prepare_datasets(
            args.dataset,
            transform,
            data_dir=args.data_dir,
            train_dir=args.train_dir,
            no_labels=args.no_labels,
            subset_class_num=args.subset_classes,
        )
        train_loader = prepare_dataloader(
            train_dataset, batch_size=args.batch_size, num_workers=args.num_workers
        )

    # normal dataloader for when it is available
    if args.dataset == "custom" and (args.no_labels or args.val_dir is None):
        val_loader = None

    ## pluggin proposed multiple-DA
    # i'm not sure about the below line, but i also add our ds into it!!
    elif args.dataset in ["imagenet100", "imagenet",   "mulda", "mulda_v1", "mv_ma"] and args.val_dir is None:
        val_loader = None
    else:
        _, val_loader = prepare_data_classification(
            args.dataset,
            data_dir=args.data_dir,
            train_dir=args.train_dir,
            val_dir=args.val_dir,
            subset_class_num=args.subset_classes,
            batch_size=args.batch_size,
            num_workers=args.num_workers,
        )

    callbacks = []

    # wandb logging
    if args.wandb:
        wandb_logger = WandbLogger(
            name=args.name,
            project=args.project,
            entity=args.entity,
            offline= False, #args.offline,
            group = args.experiment_type,
            job_type = args.job_name,
        )
        wandb_logger.watch(model, log="gradients", log_freq=100)
        wandb_logger.log_hyperparams(args)

        # lr logging
        # lr_monitor = LearningRateMonitor(logging_interval="epoch")
        # callbacks.append(lr_monitor)

    if args.save_checkpoint:
        # save checkpoint on last epoch only
        ckpt = Checkpointer(
            args,
            logdir=os.path.join(args.checkpoint_dir, args.method),
            frequency=args.checkpoint_frequency,
        )
        callbacks.append(ckpt)

    if args.auto_umap:
        assert (
            _umap_available
        ), "UMAP is not currently avaiable, please install it first with [umap]."
        auto_umap = AutoUMAP(
            args,
            logdir=os.path.join(args.auto_umap_dir, args.method),
            frequency=args.auto_umap_frequency,)

        callbacks.append(auto_umap)

    # 1.7 will deprecate resume_from_checkpoint, but for the moment
    # the argument is the same, but we need to pass it as ckpt_path to trainer.fit

    ckpt_path = None
    if args.auto_resume and args.resume_from_checkpoint is None:
        auto_resumer = AutoResumer(
            checkpoint_dir=os.path.join(args.checkpoint_dir, args.method),
            max_hours=args.auto_resumer_max_hours,)

        resume_from_checkpoint = auto_resumer.find_checkpoint(args)
        if resume_from_checkpoint is not None:
            print(
                "Resuming from previous checkpoint that matches specifications:",
                f"'{resume_from_checkpoint}'",)
            ckpt_path = resume_from_checkpoint

    elif args.resume_from_checkpoint is not None:
        ckpt_path = args.resume_from_checkpoint
        del args.resume_from_checkpoint

    alpha_schedule = Alpha_schedule(args,args.max_epochs, 0.6,args.alpha)

    Weight_Decay_schedule= WD_cosine_schedule(args, args.max_epochs, args.wd_init, args.wd_final, args.weight_decay_scheduler)
    callbacks.append(alpha_schedule)
    callbacks.append(Weight_Decay_schedule)

    trainer = Trainer.from_argparse_args(
        args,
        #fast_dev_run= True,
        # gradient_clip_val=0.6,
        # gradient_clip_algorithm="value",
        num_nodes=1,
        logger=wandb_logger if args.wandb else None,
        callbacks=callbacks,
        enable_checkpointing=False,)

    print("\n\nI'm in here \n\n")
    # it's very not good... the issue occurs in train_loader, i'm not sure which da-method cause the img have invalid size
    # # while i will go deep into each trfs 'Composer'
    # for x1, x2, x3 in train_loader:
    #     #print(im.shape)
    #     # unpack
    #     #x1, x2, x3, x4 = im
    #     print(len(x2))
    #     torch.save(x2, "visualize_tensor_1", pickle_module=pickle)
    #     print("Rick Double Check Global Views shape", x2[args.num_large_crops-2].shape )
    #     print("Rick Double Check  Local Views shape", x2[args.num_large_crops+2].shape)
    # #     #x1_=x2[7]
    # #     print(x1.shape)
    # #     #print(x1_.shape)
    # #     print(x3.shape)
    #     break

    # for x1, x2, x3 in train_loader:
    #     ## X1, X3 will be the Index of image , X2 is Batch Tensors feeding duiring training.
    #     ## Saving the Tensor Image + Image Transform Tensors

    #     ## Noted You must Uncomment the Visualization and Debug section in pretrain_dataloader
    #     ##--> Class [FullTransformPipeline_ma_mv]
    #     print("len of Batch tensor", len(x2))
    #     torch.save(x2, "visualize_tensor_image_Global_local_transform", pickle_module=pickle)
    #     print("Rick Double Check Global Views shape", x2[args.num_large_crops-2].shape )
    #     print("Rick Double Check  Local Views shape", x2[args.num_large_crops+3].shape)

    if args.dali:
        trainer.fit(model, val_dataloaders=val_loader, ckpt_path=ckpt_path)
    else:
        trainer.fit(model, train_loader, val_loader, ckpt_path=ckpt_path)

if __name__ == "__main__":
    main()


import json, time, argparse, os, logging, datetime
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver import FirefoxOptions, ChromeOptions
from dotenv import load_dotenv
from os.path import join, dirname
from discord import Webhook, RequestsWebhookAdapter

############
## CONFIG ##
############

# Logging
logging.basicConfig(
    filename='bot.log',
    filemode='a',
    format='%(asctime)s %(message)s',
    datefmt='%d/%m/%Y %I:%M:%S %p',
    level=logging.INFO
)

# Load env
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
WEBHOOK = os.getenv("DISCORD_WEBHOOK_URL")
DEFAULT_ITEM = os.getenv("DEFAULT_ITEM")
AVAILABILITY_VALUE = os.getenv("AVAILABILITY_VALUES")
TIMEOUT = int(os.getenv("TIMEOUT", 60))
ALERT_DELAY = float(os.getenv("ALERT_DELAY", 6))
ALERT_DELAY = 60 * 60 * ALERT_DELAY # hours to seconds
BROWSER = os.getenv("BROWSER", "Chrome")

# Load items from json
with open('items.json') as f:
  watchlist = json.load(f)
# Global variables
itemName = DEFAULT_ITEM
isAvailableValues = AVAILABILITY_VALUE.split(",") # Values that appear in the website when 1 or more products are available
lastTimestamp = datetime.datetime(1970, 1, 1)

# Browser setup
driver_path = os.path.abspath( os.getcwd())
if (BROWSER == "Firefox"):
    opts = FirefoxOptions()
    opts.add_argument("--headless")
    driver = webdriver.Firefox(executable_path=join(driver_path, "geckodriver"), options=opts)
elif (BROWSER == "Chrome"):
    opts = ChromeOptions()
    opts.add_argument('--no-sandbox')
    opts.add_argument('--headless')
    opts.add_argument('--disable-gpu')
    opts.add_argument('--disable-dev-shm-usage')
    opts.add_argument('--profile-directory=Default')
    opts.add_argument('--user-data-dir=~/.config/google-chrome')
    opts.add_argument("--remote-debugging-port=9222")
    driver = webdriver.Chrome(executable_path=join(driver_path, "chromedriver"), options=opts)
else:
    logging.info(f"{BROWSER} browser is not supported yet")
    print(f"{BROWSER} browser is not supported yet")
    os._exit(1)

webhook = Webhook.from_url(WEBHOOK, adapter=RequestsWebhookAdapter())

###############
## FUNCTIONS ##
###############

def startup():
    try:
        driver.get(watchlist[itemName]["url"]) # Downloads HTML page
        time.sleep(2) # Just to be sure the page is loaded and ready
        driver.find_element_by_id("didomi-notice-agree-button").click() # Clicks 'accept cookies'
    except Exception as e:
        logging.error(e)
        driver.quit()
        # driver.refresh()
        # time.sleep(1)
        # startup()

def parseArgs():
    global itemName
    parser = argparse.ArgumentParser(description='Decathlon Alerts')
    parser.add_argument("--item", default=DEFAULT_ITEM)
    args = parser.parse_args()
    itemName = args.item

def product_checker():
    driver.refresh()
    time.sleep(2) # Just to be sure

    try:
        # Product info parse
        content = driver.find_element_by_class_name("product-summary").get_attribute("outerHTML") # Gets HTML content
        soup = BeautifulSoup(content, 'html.parser')
        soup.encode('utf-8')
        driver.find_element_by_class_name("svelte-h9duyn").click() # Clicks dropdown list with sizes and availabilities
        time.sleep(0.5)
        title = soup.find(class_="title").get_text() # Name of the product (just to check)
        price = soup.find(class_='prc__active-price')['data-price'] # Price of the product (for additional info)

        # Size availability parse
        size = driver.find_element_by_id(watchlist[itemName]["target_size"]).get_attribute("outerHTML") # Get HTML content of dropdown option with correct size
        partial = BeautifulSoup(size, 'html.parser') # Parse again the new partial content
        available = partial.find(class_='stock').get_text() # Get InnerHTML text
        size = partial.find(class_='size').get_text() # Get size text

        # Availability check
        if available.strip().lower() in isAvailableValues: # Checks if that size is available
            send_notification(title, price, size)
        else:
            logging.info("Product not available yet")
    except Exception as e:
        # Catch errors and refresh. This is needed for example when the website is on maintenance and the parsing fails
        logging.error(e)
        if ( str(e).find("didomi") != -1 ):
            logging.info("Removing cookies popup")
            driver.find_element_by_id("didomi-notice-agree-button").click()
            driver.refresh()
        else:
            send_error()
            driver.quit()

def send_error():
    text = f'An error occurred, bot might have crashed'
    logging.info(text)
    webhook.send(text)

def send_notification(title, price, size):
    global lastTimestamp

    if (shoud_send_alert()):
        text = f'{title} (size {size}) is available at {price} €'
        logging.info(text)
        logging.info('Sending notification...')
        webhook.send(text)
        lastTimestamp = datetime.datetime.now()
    else:
        logging.info('Item is available but not enough time is passed since the last notification')

def shoud_send_alert():
    return date_difference() > ALERT_DELAY

def date_difference():
    diff = datetime.datetime.now() - lastTimestamp
    return diff.total_seconds()

################
## ENTRYPOINT ##
################

if __name__ == '__main__':

    logging.info("Bot starting...")
    print("Bot starting...")

    parseArgs()
    startup()

    text = f"Bot will check item '{itemName}' every {TIMEOUT} seconds"
    logging.info(text)
    print(text)
    webhook.send(text)

    while(True):
        logging.info("Checking product...")
        product_checker()
        time.sleep(TIMEOUT)


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def foo(self, node: TreeNode):
      if node is None: return
      tmp = node.left
      node.left=node.right
      node.right=tmp
      self.foo(node.left)
      self.foo(node.right)
    def invertTree(self, root: TreeNode) -> TreeNode:
      self.foo(root)
      return root


from decapawn_moves import get_decapawn_moves
from minimax import minimax

def get_move(board, depth):
    legal_moves = get_decapawn_moves(board)

    best_move = legal_moves[0]
    best_score = -10

    for move in legal_moves:
        board.push(move)
        score = minimax(board, depth, -10, 10, 1)
        board.pop()

        if not board.turn:
            score *= -1

        if score > best_score:
            best_score = score
            best_move = move

    return best_move


import json, re
from datetime import datetime

with open("Google Chat/Groups/DM uZ8_QQAAAAE/messages.json", "r") as d:
    dict = json.load(d)

    messages = dict['messages']

    dates = []

    for message in messages:
        try:
            sender = message['creator']['name']
            date = message['created_date']
            text = message['text']

            # parse created_date string to datetime
            date_time_obj = datetime.strptime(date, '%A, %B %d, %Y at %X %p UTC')

            dates.append(date_time_obj.date())

            print(sender, ' - ' , date, '\n', text, '\n\n --- \n')
        except KeyError:
            continue

# firstDate = dates[0].__str__()
# lastDate = dates[-1].__str__()
# chat_date_duration = firstDate + ' - ' + lastDate

# print(chat_date_duration)


from pathlib import Path
import os
import sys
home_dir = Path.home()
modules_dir = os.path.join(home_dir, 'services/milestones_chains/modules/')
if modules_dir not in sys.path: sys.path.append(modules_dir)
from config import *

# Directories
if 'run_dir' in os.listdir(working_dir):
	shutil.rmtree(run_dir_path)
os.mkdir(run_dir_path)
run_dirs = os.listdir(run_dir_path)
if 'chunks' not in run_dirs:
	os.mkdir(chunks_path)
if 'scaffolds' not in run_dirs:
	os.mkdir(scaffolds_path)
if 'sub_graphs' not in run_dirs:
	os.mkdir(sub_graphs_path)


'''
    example book api call
'''
import urllib.request
import json

ISBN = '9780007550258'
NAME = "I%20Robot"
NAME = "V%20for%20Vendetta"

while True:

    BASE_API_LINK = "https://www.googleapis.com/books/v1/volumes?q=ISBN:"
    # title search
    BASE_API_LINK = "https://www.googleapis.com/books/v1/volumes?q="
    # USER_INPUT = input("Enter ISBN: ").strip()
    # USER_INPUT = ISBN
    USER_INPUT = NAME
    # this is a test

    with urllib.request.urlopen(BASE_API_LINK + USER_INPUT) as f:
        text = f.read()

    decoded_text = text.decode('utf-8-sig')
    obj = json.loads(decoded_text)
    # deserializes decoded_text to a Python object
    volume_info = obj["items"][0]
    authors = obj["items"][0]["volumeInfo"]["authors"]

    # displays title, summary, author, domain, page count and language
    print("\nTitle:", volume_info["volumeInfo"]["title"])
    print("\n")
    # print("\nISBN: ", volume_info["volumeInfo"]["industryIdentifiers"]
    #                    ["ISBN_13"])
    # print("\nSummary:\n")
    # print(textwrap.fill(volume_info["searchInfo"]["textSnippet"], width=65))
    print("\nAuthor(s):", ",".join(authors))
    # print("\nPublic Domain:", volume_info["accessInfo"]["publicDomain"])
    # print("\nPage count:", volume_info["volumeInfo"]["pageCount"])
    # print("\nLanguage:", volume_info["volumeInfo"]["language"])
    print("\n***")

    status_update = input("\nEnter another ISBN? y or n: ").lower().strip()

    if status_update == "n":
        print("\nThank you! Have a nice day.")
        break


import os
import shutil
import sys

import requests
from bs4 import BeautifulSoup

# Takes the url of the codeforces contest
def get_problem_ids(contest):
    r = requests.get(contest)
    soup = BeautifulSoup(r.text, 'lxml')
    table = soup.find('table', class_='problems')
    rows = table.find_all('tr')[1:]
    return [row.find('td').text.strip() for row in rows]

def create_file(file_name, content):
    with open(file_name, 'wt') as f:
        f.write(f'{content}\n')

def mcd(dir_name):
    if dir_name not in os.listdir('.'):
        os.mkdir(dir_name)
    os.chdir(dir_name)

def get_contest_dir(contest_id):
    contest_dir = f'contest-{contest_id}'

    if os.path.exists(contest_dir):
        print(
            f'{contest_dir} already exists. Wanna redownload the input and output files?'
        )
        print(
            "This won't affect any of the source code in that dir [yes/no] : ",
            end='')
        response = input()
        if response.lower() not in ['yes', 'y']:
            sys.exit(1)

    return contest_dir

def main():
    os.chdir(sys.argv[1])
    contest_id = sys.argv[2]
    contest = f'https://codeforces.com/contest/{contest_id}'

    contest_dir = get_contest_dir(contest_id)
    print(f'Creating {contest_dir} Directory for contest-{contest_id}...')
    mcd(contest_dir)

    problem_ids = get_problem_ids(contest)

    source = os.path.expanduser('~/dotfiles/cp_outline.cpp')
    template_exists = os.path.exists(source)
    for problem_id in problem_ids:
        print(f'Creating a Directory for Problem-{problem_id}...')
        mcd(problem_id)

        problem_file = f'{problem_id}.cpp'
        if template_exists and problem_file not in os.listdir('.'):
            shutil.copy(source, problem_file)

        problem = f'{contest}/problem/{problem_id}'
        r = requests.get(problem)
        soup = BeautifulSoup(r.text, 'lxml')

        cases = soup.find_all('div', class_='input')
        for ind, case in enumerate(cases):
            divs = case.pre.find_all('div')
            if not divs:
                content = case.pre.text.strip()
            else:
                content = ''.join([f'{div.text}\n' for div in divs]).strip()
            create_file(f'input{ind + 1}.txt', content)

        outputs = soup.find_all('div', class_='output')
        for ind, output in enumerate(outputs):
            content = output.pre.text.strip()
            create_file(f'output{ind + 1}.txt', content)

        os.chdir('..')

    if not template_exists:
        print(
            '\nCreate a cp_outline.cpp file in the home folder to create the template files for each problem\n'
        )

if __name__ == '__main__':
    main()


import os
import numpy as np
import pandas as pd
import joblib
from sklearn.model_selection import StratifiedShuffleSplit, cross_val_score, GridSearchCV
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error

DATA_PATH = os.path.join("datasets", "housing")

def load_data(data_path=DATA_PATH):
    csv_path = os.path.join(data_path, "housing.csv")
    return pd.read_csv(csv_path)

housing = load_data()
housing["income_cut"] = pd.cut(housing["median_income"], bins=[0., 1.5, 3., 4.5, 6., np.inf], labels=[1, 2, 3, 4, 5])
split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)
for train_index, test_index in split.split(housing, housing["income_cut"]):
    strat_train_set = housing.loc[train_index]
    strat_test_set = housing.loc[test_index]
for set_ in [strat_train_set, strat_test_set]:
    set_.drop("income_cut", axis=1, inplace=True)
housing = strat_train_set.copy()

rooms_ix, bedrooms_ix, pop_ix, house_ix = 3, 4, 5, 6

class CombinedAttributeAdder(BaseEstimator, TransformerMixin):
    def __init__(self, add_bedrooms_per_rooms=True):
        self.add_bedrooms_per_rooms = add_bedrooms_per_rooms

    def fit(self, X, y=None):
        return self

    def transform(self, X, y=None):
        pop_per_house = X[:, pop_ix]/X[:, house_ix]
        bedrooms_per_house = X[:, bedrooms_ix]/X[:, house_ix]
        if self.add_bedrooms_per_rooms:
            bedrooms_per_rooms = X[:, bedrooms_ix]/X[:, rooms_ix]
            return np.c_[X, pop_per_house, bedrooms_per_house, bedrooms_per_rooms]
        else:
            return np.c_[X, pop_per_house, bedrooms_per_house]

pipeline_num = Pipeline([
    ["impute", SimpleImputer(strategy="median")],
    ["add", CombinedAttributeAdder()],
    ["stand", StandardScaler()],
])

housing_num = housing.drop(["median_house_value", "ocean_proximity"], axis=1, inplace=False)
attri_num = list(housing_num)
attri_cat = ["ocean_proximity"]

pipeline_full = ColumnTransformer([
    ("num", pipeline_num, attri_num),
    ("cat", OneHotEncoder(), attri_cat),
])

columns_new = attri_num+["pop_per_house", "bedrooms_per_house", "bedrooms_per_rooms"]+["1", "2", "3", "4", "5"]
housing_labels = housing["median_house_value"].copy()
housing_prepared = pd.DataFrame(pipeline_full.fit_transform(housing.drop("median_house_value", axis=1, inplace=False)), columns=columns_new, index=housing.index)

rand_forest_reg = RandomForestRegressor()
lin_reg = LinearRegression()
tree_reg = DecisionTreeRegressor()

def show_scores(scores):
    print(scores.mean())
    print(scores.std())

scores = cross_val_score(lin_reg, housing_prepared, housing_labels, scoring="neg_mean_squared_error", cv=10)
scores = np.sqrt(-scores)
show_scores(scores)

scores = cross_val_score(tree_reg, housing_prepared, housing_labels, scoring="neg_mean_squared_error", cv=10)
scores = np.sqrt(-scores)
show_scores(scores)

para_grid = [
    {'n_estimators': [3, 10, 30], 'max_features': [2, 4, 6, 8]},
    {'bootstrap': [False], 'n_estimators': [3, 10], 'max_features': [2, 3, 4]},
]

grid_search = GridSearchCV(rand_forest_reg, para_grid, scoring="neg_mean_squared_error", cv=5, return_train_score=True)
grid_search.fit(housing_prepared, housing_labels)
print(grid_search.best_estimator_)
print(grid_search.best_params_)
cv_results = grid_search.cv_results_
for scores, params in zip(cv_results["mean_test_score"], cv_results["params"]):
    print(np.sqrt(-scores), params)

feature_importances = grid_search.best_estimator_.feature_importances_
print(sorted(zip(feature_importances, columns_new), reverse=True))

final_model = grid_search.best_estimator_
joblib.dump(final_model, "final_model.pkl")

final = strat_test_set.copy()
final_labels = final["median_house_value"].copy()
final_prepared = pd.DataFrame(pipeline_full.fit_transform(final.drop("median_house_value", axis=1, inplace=False)), columns=columns_new, index=final.index)
final_predicts = final_model.predict(final_prepared)
final_mse = mean_squared_error(final_labels, final_predicts)
final_rmse = np.sqrt(final_mse)
print(final_rmse)


def encode(self, lname, fname='.', omit_fname=False):
        """Return Davidson's Consonant Code.

        Parameters
        ----------
        lname : str
            Last name (or word) to be encoded
        fname : str
            First name (optional), of which the first character is included in
            the code.
        omit_fname : bool
            Set to True to completely omit the first character of the first
            name

        Returns
        -------
        str
            Davidson's Consonant Code

        Example
        -------
        >>> pe = Davidson()
        >>> pe.encode('Gough')
        'G   .'
        >>> pe.encode('pneuma')
        'PNM .'
        >>> pe.encode('knight')
        'KNGT.'
        >>> pe.encode('trice')
        'TRC .'
        >>> pe.encode('judge')
        'JDG .'
        >>> pe.encode('Smith', 'James')
        'SMT J'
        >>> pe.encode('Wasserman', 'Tabitha')
        'WSRMT'

        """
        lname = text_type(lname.upper())
        code = self._delete_consecutive_repeats(
            lname[:1] + lname[1:].translate(self._trans)
        )
        code = code[:4] + (4 - len(code)) * ' '

        if not omit_fname:
            code += fname[:1].upper()

        return code


#!/usr/bin/env python

# ----------------------------------------------------------------------
# editnotification.py
#-----------------------------------------------------------------------

import services.database as database
from services.database import _engine, Notification
import classes.notification as notificationmod
from classes.listing import Listing

import sqlalchemy.orm
import sqlalchemy

from config import config_cloudinary
config_cloudinary()
from cloudinary import uploader

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

def remove_notification(id):

    with sqlalchemy.orm.Session(_engine) as session:
        stmt = (sqlalchemy.delete(database.Notification).where(
            database.Notification.id == id))
        session.execute(stmt)
        session.commit()

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

def add_notification(notif_type, listing_id, to_user_id, from_user_id,
    date_created, transaction_id, msg):

    with sqlalchemy.orm.Session(_engine) as session:
        new_notification = database.Notification(notif_type=notif_type,
            listing_id=listing_id, to_user_id=to_user_id, from_user_id=from_user_id,
            date_created=date_created, transaction_id=transaction_id, msg=msg)
        session.add(new_notification)
        session.commit()
        session.flush()
        session.refresh(new_notification)

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

def _test():

    # add a new test listing to database
    with sqlalchemy.orm.Session(_engine) as session:

        new_listing = database.Listing(user_id=3329, name='katie',
            price=100, category='shirt', size='small', subcategory='what',
            description='test listing', status='Available', pickup_location=None,
            likes=0)
        session.add(new_listing)
        session.commit()
        session.flush()
        session.refresh(new_listing)


"""
A fake implementation of the glucose reading store built
on top of a dictionary.

This should be used for unit tests.

"""
from types import TracebackType
from typing import Iterator, Type, Union
from uuid import UUID

from .base import AbstractGlucoseReadingStore
from ..common import parse_uuid
from ..exceptions import DuplicateReading, NoSuchReading
from ..models import GlucoseReading

class FakeGlucoseReadingStore(AbstractGlucoseReadingStore):
    """
    A fake glucose reading store built on top of a Python dictionary.

    """

    def __init__(self):
        self._readings = {}

    def add_reading(self, reading: GlucoseReading):
        reading_uuid = reading.reading_uuid
        if reading_uuid in self._readings:
            raise DuplicateReading(repr(reading_uuid))

        self._readings[reading_uuid] = reading

    def update_reading(self, reading: GlucoseReading):
        reading_uuid = reading.reading_uuid
        if reading_uuid not in self._readings:
            raise NoSuchReading(repr(reading_uuid))

        self._readings[reading_uuid] = reading

    def get_reading(self, reading_uuid: Union[int, str, UUID]) -> GlucoseReading:
        reading_uuid = parse_uuid(reading_uuid)
        try:
            return self._readings[reading_uuid]
        except KeyError as err:
            raise NoSuchReading(repr(reading_uuid)) from err

    def delete_reading(self, reading_uuid: Union[int, str, UUID]):
        reading_uuid = parse_uuid(reading_uuid)
        try:
            del self._readings[reading_uuid]
        except KeyError as err:
            raise NoSuchReading(reading_uuid) from err

    def iterate_readings(self) -> Iterator[GlucoseReading]:
        yield from self._readings.values()

    def __enter__(self):
        pass

    def __exit__(
        self, exc_type: Type[Exception], exc_value: Exception, traceback: TracebackType
    ):
        pass


import time
import requests
from bs4 import BeautifulSoup
import datetime

start = time.time()
timerHasBeenSetpip = False

def split(word):
    return [char for char in word]
now = time.time()
while(True):

    now = time.time()
    if now - start > 2:
        start = time.time()
        response = requests.get('https://markets.businessinsider.com/index/nasdaq_100')
        soup = BeautifulSoup(response.text, 'html.parser')
        el = soup.find_all(class_='push-data')
        currentDT = datetime.datetime.now()
        stockPrice = el[0].get_text()
        stockPrice = stockPrice.replace(",", "")
        value = str(currentDT) + "," + stockPrice + "\n"
        print(value)

        f = open("stockdata.csv", "a")
        f.write(value)
        f.close()


import wikipedia, time
import PySimpleGUI as sg
#
#      ____        _  ___                _ _
#     |  _ \ _   _| |/ (_)_ __   ___  __| (_) __ _
#     | |_) | | | | ' /| | '_ \ / _ \/ _` | |/ _` |
#     |  __/| |_| | . \| | |_) |  __/ (_| | | (_| |
#     |_|    \__, |_|\_\_| .__/ \___|\__,_|_|\__,_|
#            |___/       |_|
#
#     Este software foi escrito inteiramente em Python e utiliza a API wikipedia para fazer as buscas
#     Software criado por Elizeu Barbosa Abreu
#     Veja meus projetos no GitHub https://github.com/elizeubarbosaabreu

wikipedia.set_lang("pt")
x = wikipedia.summary("Wikipedia")

sg.theme('Reddit')

layout=[ [sg.Stretch(),
          sg.Input(key='-pesquisa-', size=(60,1), font=('Arial', 12)),
          sg.Button('Pesquisar', size=(12, 1), font=('Arial', 12)),
          sg.Stretch()],
         [sg.Stretch(),
          sg.Multiline(x, key='-conteudo-', size=(75, 25), font=('Arial', 12)),
          sg.Stretch()],
         [sg.Stretch(), sg.Text('PyKipedia - A Wikipedia Feita com Python')]
         ]
window = sg.Window('PyKipedia - A Wikipedia Feita com Python', layout, resizable=True)

while True:

    event, values = window.read()

    if event == sg.WINDOW_CLOSED:
        break
    if event == 'Pesquisar':
        try:
            window['-conteudo-'].update(wikipedia.summary(values['-pesquisa-']))
            time.sleep(5)
            window['-pesquisa-'].update('')
        except wikipedia.exceptions.WikipediaException as e:
            texto = f'''Ocorreu o seguinte erro durante a pesquisa: [{e}].
Tente usar outras palavras na pesquisa ou verifique sua conexão com a internet...
'''
            window['-conteudo-'].update(texto)
            time.sleep(5)
            window['-pesquisa-'].update('')

window.close()


'''
# yum install -y python3

 nohup python3 -u Fio-Threads.py > nohup.log &

'''
# Must use Python 3.6 or above
# yum install centos-release-scl
# yum install rh-python36
# scl enable rh-python36 bash
# python --version

import threading
import subprocess
import time
import os
import math
import re

dut_port1='/dev/sdi'
dut_port2='/dev/sdq'

FIO_4k = 'fio --filename=' + dut_port1 +' --name=RandomperformancetestPort1  --blocksize=4k --iodepth=8 --rw=rw --numjobs=1 --ioengine=libaio --nice=-19 --prio=0 --prioclass=1 --rwmixread=100 --time_based --runtime=60 --direct=1 --norandommap --randrepeat=0 --ramp_time=10 --log_avg_msec=1000 --write_iops_log=TC7.RandomperformancetestDualPort.NumPorts_1.Port_1.Testsize_4k.QD_8.NW_1.Read_0.RT_60.iops.log --group_reporting --output-format=json --random_generator=tausworthe64 --output=TC_7.RandomperformancetestDualPort.NumPorts_1.Port_1.Testsize_4k.QD_8.NW_1.Read_0.RT_60.output'

FIO_512 = 'fio --filename=' + dut_port2 +' --name=RandomperformancetestPort1  --blocksize=512 --iodepth=1 --rw=rw --numjobs=1 --ioengine=libaio --nice=-19 --prio=0 --prioclass=1 --rwmixread=100 --time_based --runtime=60 --direct=1 --norandommap --randrepeat=0 --ramp_time=10 --log_avg_msec=1000 --write_iops_log=TC7.RandomperformancetestDualPort.NumPorts_1.Port_1.Testsize_512.QD_1.NW_1.Read_0.RT_60.iops.log --group_reporting --output-format=json --random_generator=tausworthe64 --output=TC_7.RandomperformancetestDualPort.NumPorts_1.Port_1.Testsize_512.QD_1.NW_1.Read_0.RT_60.output'

def send_fio1(num):
	print(FIO_4k)
	p1=subprocess.Popen(FIO_4k, stdout=subprocess.PIPE, encoding='utf-8', shell=True)
	(output, err) = p1.communicate()
	p_status = p1.wait()
	#print(output)

def send_fio2(num):
	print(FIO_512)
	p1=subprocess.Popen(FIO_512, stdout=subprocess.PIPE, encoding='utf-8', shell=True)
	(output, err) = p1.communicate()
	p_status = p1.wait()
	#print(output)

if __name__ == "__main__":
    # creating thread
    t1 = threading.Thread(target=send_fio1, args=(10,))
    t2 = threading.Thread(target=send_fio2, args=(10,))

    # starting thread 1
    t1.start()
    # starting thread 2
    t2.start()

    # wait until thread 1 is completely executed
    t1.join()
    # wait until thread 2 is completely executed
    t2.join()

    # both threads completely executed
    print("Done!")


import random

n = random.randint(1,10)
i = 5

while i>0:
	i = i - 1
	guess = input('Enter a number ')
	g = int(guess)
	if n == g:
		print('Bomb')
		break
	elif g > n:
		print('Smaller')
		print('Bigger')
		if i > 0:
			print('One more guess')
		else:
			print('No more chance')
	elif g < n:
		print('Bigger')
		if i > 0:
			print('One more guess')
		else:
			print('No more chance')


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

resp_Df = pd.read_csv("C:/Users/HP/Desktop/EDAs/responses.csv")
print(resp_Df.info())
print(resp_Df.head().T)

resp_Df.loc[:, resp_Df.notnull().all()].columns # Only 6 cols have no null

## Build Data Tables and Profiles
# 1. Histogram-Numerical Variables
resp_hist = resp_Df.hist(bins = 10, figsize = (20, 10))
plt.savefig("C:/Users/HP/PycharmProjects/Complete_EDA/respiratory Histogram.png")
plt.show()

# 2. Boxplot
plt.figure(figsize = (80, 10))

num_cols = resp_Df.select_dtypes(include = "int64").columns # adjust with value_counts*
num_cols = num_cols.append(resp_Df.select_dtypes(include = "float").columns)
for i,col in enumerate(num_cols):
    plt.subplot(15, 10, i+1)
    sns.boxplot(resp_Df[col], color = 'xkcd:lime')
    plt.title(f"{col}", fontsize = 10)
    plt.xlabel(' ')
plt.savefig("C:/Users/HP/PycharmProjects/Complete_EDA/respiratory Boxplot.png")
plt.show()

# 3. PairPlots
# resp_pp = sns.pairplot(resp_Df)
# plt.savefig("C:/Users/HP/PycharmProjects/Complete_EDA/respiratory Pairplot.png")
# plt.show()

## Variable Measurement
# 1. Categorical Variables
print()
print('CAT Variables')
print()
n_cat = []
non_col = []
for col in resp_Df.columns:
    if resp_Df[col].dtypes == object and resp_Df[col].nunique() < 15:
        n_cat.append(col)
        print('=='*7)
        print(f"{resp_Df[col].value_counts()}")
    elif resp_Df[col].dtypes == object:
        non_col.append(col)
print(non_col)
print(f'no. of CAT Variables: {len(n_cat)}')

# 2. Numerical Variables
print()
print('Num Variables')
n_num = []
for col in resp_Df.columns:
    if resp_Df[col].dtypes != object and resp_Df[col].nunique() < 20:
        n_num.append(col)
        print('=+='*7)
        print(f"{resp_Df[col].value_counts()}")
print(f'no. of NUM Variables: {len(n_num)}')

# 3. Continuous Variables
print()
print('Continuous Variables')
n_cont = []
for col in resp_Df.columns:
    if resp_Df[col].dtypes != object and resp_Df[col].nunique() >=20:
        n_cont.append(col)
        print("+=+"*7)
        print(f"{col}: Min_{resp_Df[col].min()} and Max_{resp_Df[col].max()}")
print(f'no. of CONT Variables: {len(n_cont)}')
print(f"Skipped: {(len(resp_Df.columns) - (len(n_cat) + len(n_num) + len(n_cont)))} columns")

## Exploring Data Relationships
resp_corr = resp_Df.corr()
print(resp_corr.abs())

mask = np.zeros_like(resp_corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True

plt.figure(figsize = (20, 10))
sns.heatmap(resp_corr, mask = mask, center =0, vmin =-1.2, vmax = 1.2,
            linewidths= .1, cbar_kws = {'shrink': .5}, cmap = 'coolwarm',
            square =False, annot = True)
plt.savefig("C:/Users/HP/PycharmProjects/Complete_EDA/responses Heatmap.png")
plt.show()

# This heatmap looks like swarm of bees, a better means has to be taken to clean it better.

## Conclusion
# Obvoiusly this is a very large dataset, and making a comprehensive report or conclusion is a long shot,
# I avioded this totally.

## Moving Forward
# This kind of data is tailor made for AI engineering, it is perculiarly a social science affair,
# which often yield unpredictable results but will suffice for some intense engineering.


# coding=utf-8
import numpy as np
from numba import jit

def findMax(arr):
    """
    in comparison to argrelmax() more simple and  reliable peak finder
    """
    out = np.zeros(shape=arr.shape, dtype=bool)
    _calcMax(arr, out)
    return out

@jit(nopython=True)
def _calcMax(arr, out):
    g0 = arr.shape[0]

    up = False
    last = arr[0]
    for i in range(1, g0):

        px = arr[i]
        if up and px < last:
            out[i - 1] = True
            up = False
        elif px > last:
            up = True
        last = px

def findMin(arr):
    """
    in comparison to argrelmax() more simple and  reliable peak finder
    """
    out = np.zeros(shape=arr.shape, dtype=bool)
    _calcMin(arr, out)
    return out

@jit(nopython=True)
def _calcMin(arr, out):
    g0 = arr.shape[0]

    down = False
    last = arr[0]
    for i in range(1, g0):

        px = arr[i]
        if down and px > last:
            out[i - 1] = True
            down = False
        elif px < last:
            down = True
        last = px

if __name__ == '__main__':

    a = np.array([0, 0, 1, 2, 2, 3, 2, 4, 3, 2, 2, 3, 3, 3, 1])
    # peaks values...
    assert np.allclose(a[findMax(a)], [3, 4, 3])
    assert np.allclose(a[findMin(a)], [2, 2])


# -*- coding:utf-8 -*-
class Solution:
    def NumberOf1Between1AndN_Solution(self, n):
        # write code here
        if (n<=0): return 0
        k = 10
        digits = []
        while(abs(n) >= 1e-5):
            m = n%k
            n = int(n/k)
            digits.append(m)
        return self.Count1(digits)
    def Count1(self, digit):
        if (len(digit) == 0): return 0
        L, first = len(digit), digit[-1]
        if (L == 1):
            if (first <= 0): return 0
            elif(first > 0): return 1

        number_of_first_digit = 0
        if (first > 1):
            number_of_first_digit = self.FindNumberOfOneDigit(L - 1)
        elif (first == 1):
            number_of_first_digit = self.DigitsToNumber(digit[0:L - 1]) + 1
        number_of_other_digit = first*(L - 1)*self.FindNumberOfOneDigit(L - 2)
        number_of_last_digit = self.Count1(digit[0:L - 1])

        return number_of_first_digit + number_of_other_digit + number_of_last_digit
    def FindNumberOfOneDigit(self, L):
        return 10**L
    def DigitsToNumber(self, di):
        L = len(di)
        return sum([di[x]*10**x for x in range(0, L)])

A = Solution()
b = A.NumberOf1Between1AndN_Solution(10)
print(b)


#!/usr/bin/env python3
from res import aimmaze
import math
import sys

def my_quit_ok(points, text):
	_, exit_found, _, made_moves, _, made_ops, _, code_length, _, raw_score, _, maze_weight = text.split()
	if exit_found != str(True):
		text = 'Exit not found'
	else:
		brevity = round(100 * coefs[1] / sum(coefs))
		parts = [int(maze_weight) * c for c in coefs]
		text = f'OUR={points:.3f}={parts[0]:.0f}+{parts[1]:.0f}\tMOVES={made_moves}\tOPS={made_ops}\tCODE={code_length}\tBREV={brevity}%'
	print(text, file=sys.stderr)
	sys.exit(7)

def my_log(x):
	coefs.append(saved_log(x))
	return saved_log(x)

saved_log = math.log2
coefs = []

def main():
	aimmaze.CELL_SIZE = 4
	aimmaze.WALL_WIDTH = 1
	aimmaze.SOLUTION_WIDTH = 1
	aimmaze.ROBOT_COLOR = "gray"
	aimmaze.WALL_COLOR = "lightgray"
	aimmaze.GRID_COLOR = "white"
	aimmaze.EXIT_COLOR = "red"
	aimmaze.KEY_COLOR = "blue"
	aimmaze.quit_ok = my_quit_ok
	math.log2 = my_log
	aimmaze.main()

if __name__ == '__main__':
	main()


import sys
import RPi.GPIO
import time
import os
from flask import Flask, render_template, Response, request
import signal
import atexit

from getcpu import *

# import camera driver
if os.environ.get('CAMERA'):
    Camera = import_module('camera_' + os.environ['CAMERA']).Camera
else:
    from camera import Camera

# Raspberry Pi camera module (requires picamera package)
from camera_pi import Camera

global lcontrol
global rcontrol
global ucontrol
global dcontrol
lcontrol = 6.25
rcontrol = 6.25
ucontrol = 6.25
dcontrol = 6.25

RPi.GPIO.setmode(RPi.GPIO.BCM)
RPi.GPIO.setup(18,RPi.GPIO.OUT)
RPi.GPIO.setup(23,RPi.GPIO.OUT)

app = Flask(__name__)

@app.route("/")
def main():
        return render_template("main.html")

def gen(camera):
    """Video streaming generator function."""
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

@app.route("/on")
def ledon():
        RPi.GPIO.setmode(RPi.GPIO.BCM)
        RPi.GPIO.setup(18,RPi.GPIO.OUT)
        RPi.GPIO.setup(23,RPi.GPIO.OUT)

        RPi.GPIO.output(23,True)

        return render_template("main.html")

@app.route("/off")
def ledoff():

        RPi.GPIO.setmode(RPi.GPIO.BCM)
        RPi.GPIO.setup(18,RPi.GPIO.OUT)
        RPi.GPIO.setup(23,RPi.GPIO.OUT)

        RPi.GPIO.output(23,False)
        RPi.GPIO.cleanup()

        return render_template("main.html")

@app.route("/shining")
def shining():
        RPi.GPIO.setmode(RPi.GPIO.BCM)
        RPi.GPIO.setup(18,RPi.GPIO.OUT)
        RPi.GPIO.setup(23,RPi.GPIO.OUT)

        pwm = RPi.GPIO.PWM(23,180)
        pwm.start(0)
        for i in range(0,5):
            for x in range (0,101,1):
                pwm.ChangeDutyCycle(x)
                time.sleep(.02)

            for x in range(100,-1,-1):
                pwm.ChangeDutyCycle(x)
                time.sleep(.02)
        pwm.stop()
        RPi.GPIO.cleanup()
        time.sleep(0.2)

        return render_template("main.html")

@app.route("/cpu")
def cpu():

    return getCPUtemperature()

@app.route('/cmd',methods=['POST'])
def cmd():

    atexit.register(RPi.GPIO.cleanup)
    RPi.GPIO.setmode(RPi.GPIO.BCM)
    RPi.GPIO.setup(12, RPi.GPIO.OUT)
    RPi.GPIO.setup(21, RPi.GPIO.OUT)
    b = RPi.GPIO.PWM(21,50)
    p = RPi.GPIO.PWM(12,50) #50HZ
    p.start(0)
    b.start(0)
    time.sleep(0.2)
    cont = str(request.get_data())

    if "left" in cont:
          global lcontrol
          if lcontrol <=12.5:
            lcontrol = lcontrol + 0.25
            atexit.register(RPi.GPIO.cleanup)
            p.ChangeDutyCycle(lcontrol)
            time.sleep(0.2)

    if "right" in cont:

        if lcontrol >=2.5:
            lcontrol = lcontrol - 0.25
            atexit.register(RPi.GPIO.cleanup)
            p.ChangeDutyCycle(lcontrol)
            time.sleep(0.2)
    if "up" in cont:

        global ucontrol
        if ucontrol >= 2.5:
            ucontrol -= 0.25
            atexit.register(RPi.GPIO.cleanup)
            b.ChangeDutyCycle(ucontrol)
            time.sleep(0.2)

    if "down" in cont:

        if ucontrol <= 12.5:
            ucontrol += 0.25
            atexit.register(RPi.GPIO.cleanup)
            b.ChangeDutyCycle(ucontrol)
            time.sleep(0.2)
    if "stop" in cont:
        atexit.register(RPi.GPIO.cleanup)
        b.ChangeDutyCycle(6.25)
        p.ChangeDutyCycle(6.25)
        time.sleep(0.2)

    return request.get_data()

@app.route('/video_feed')

def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(gen(Camera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == "__main__":
    app.run(host="0.0.0.0",port=1112)


from django import forms
from django.contrib.auth import login
from django.contrib.auth import logout as auth_logout
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from TWSITUE.forms import ImageUploadForm, AuthForm, UserCreateForm, TextUploadForm

# Create your views here.
from TWSITUE.models import Image, Text

def home(request):
    return render(request, "home.html", {})

def about(request):
    return render(request, "about.html", {})

def imageSearcher(request):
    context = {}

    if request.method == 'GET':
        data = Image.objects.all()

        if request.GET.get('keyword') != '':
            data = data.filter(keyword=request.GET.get('keyword'))

        context = {
            'images': data
        }

    return render(request, "image-search.html", context)

def textSearcher(request):
    context = {}

    if request.method == 'GET':
        data = Text.objects.all()

        if request.GET.get('keyword') != '':
            data = data.filter(keyword=request.GET.get('keyword'))

        context = {
            'texts': data
        }

    return render(request, "text-search.html", context)

def imageUploader(request):
    if request.method == 'POST':
        form = ImageUploadForm(request.POST, request.FILES)

        if form.is_valid():
            imageToUpload = form.save(commit=False)
            imageToUpload.uploader = request.user
            imageToUpload.save()
            imageToUpload.raw_name = imageToUpload.image.url[14:]
            imageToUpload.save()

            imageName = imageToUpload.raw_name.split('.')[0]
            imageExtension = imageToUpload.raw_name.split('.')[1]

            return redirect(imageUploadedView, imageName, imageExtension)
    else:
        form = ImageUploadForm()

    return render(request, 'image-uploader.html', {'form': form})

def imageUploadedView(request, imageUploadedName, imageUploadedExtension):
    context = {
        'image_path': imageUploadedName,
        'image_extension': imageUploadedExtension,
        'image_combined': imageUploadedName + '.' + imageUploadedExtension,
        'link_path': '127.0.0.1:8000/media/images/' + imageUploadedName + '.' + imageUploadedExtension,
        'media_path': 'media/images/' + imageUploadedName + '.' + imageUploadedExtension
    }
    return render(request, 'image-uploaded.html', context)

def textUploader(request):
    if request.method == 'POST':
        form = TextUploadForm(request.POST)

        if form.is_valid():
            textToUpload = form.save(commit=False)
            textToUpload.uploader = request.user
            textToUpload.save()

            textID = textToUpload.generated_name

            return redirect(textUploadedView, textID)
    else:
        form = TextUploadForm()

    return render(request, 'text-uploader.html', {'form': form})

def textUploadedView(request, textID):
    textObject = Text.objects.all().filter(generated_name=textID)
    context = {
        'text_title': textObject.values()[0]['title'],
        'text_actual': textObject.values()[0]['text']
    }
    return render(request, 'text-uploaded.html', context)

def loginPage(request):
    if request.method == 'POST':
        form = AuthForm(data=request.POST)

        if form.is_valid():
            # log in the user
            user = form.get_user()
            login(request, user)

            return redirect(home)
    else:
        if request.user.is_authenticated:
            return redirect(home)
        else:
            form = AuthForm()

    return render(request, "login.html", {'form': form})

def signupPage(request):
    if request.method == 'POST':
        form = UserCreateForm(request.POST)

        if form.is_valid():
            user = form.save()

            # log the user in
            login(request, user)

            return redirect(home)
    else:
        form = UserCreateForm()

    return render(request, "signup.html", {'form': form})

def logoutView(request):
    auth_logout(request)
    return redirect(loginPage)


'''
Created on 17.05.2021

@author: pagai
'''
import networkx as nx
import time
# import own helper-modules
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.realpath(__file__),"../../networkx_modules")))
from helpers.generalStuff import *

def algo_pagerank(G, functionType, weightInput=None, verbose=False, maxLineOutput=25, idDict=None):
    start_time_all = time.time()
    if (functionType == "default"):
        start_time = time.time()
        calculation = nx.pagerank(G, alpha=0.85, max_iter=100, weight=weightInput, tol=1.0e-7)
        print("RUNTIME PageRank " + functionType + " : "+ to_ms(time.time() - start_time) + " s.")
    if (functionType == "numpy"):
        start_time = time.time()
        calculation = nx.pagerank_numpy(G, alpha=0.85)
        print("RUNTIME PageRank: " + functionType + " : "+ to_ms(time.time() - start_time) + " s.")
    if (functionType == "scipy"):
        start_time = time.time()
        calculation = nx.pagerank_scipy(G, alpha=0.85, max_iter=100, weight=weightInput, tol=1.0e-7)
        print("RUNTIME PageRank: " + functionType + " : "+ to_ms(time.time() - start_time) + " s.")
    if (verbose):
        print("Result:")
        i=1
        #print(calculation)
        for node in sorted(calculation.items(), key=lambda item: item[1], reverse=True):
            #value=[y for x,y in dict(sorted(calculation.items(), key=lambda item: item[1], reverse=True)) if x == node]
            print(G.nodes[node[0]]['name'],node[1])
            #print(str([x for x,y in idDict.items() if y == node[0]]) + " SCORE: " + str(node))
            #print(str(G.nodes()[node[0]]) + " DEGREE: " + str(G.degree(node[0])))
            #print(str(G.nodes(node)) + " --- " + str(dict(sorted(calculation.get(node)))))
            if maxLineOutput != 0:
                i += 1
                if i > maxLineOutput:
                    break
        print("RUNTIME ALL: " + functionType + " : " + to_ms(time.time() - start_time) + " s.")


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import warnings
from sklearn.linear_model import LogisticRegression # model we are going to use
from datetime import datetime
from Class_ML.ML_Pipeline import MLPipeline # this is the class for the ML Pipeline I created in the folder Class_ML

warnings.filterwarnings("ignore")

########################################## MODEL EVALUATION ##########################################

if __name__ == "__main__":

      # --------------------- Importing the cleaned dataset we created in 'feature_engineering.py' ---------------------

      data_clean = pd.read_csv('data/BTCUSDT_CLEANED.csv')
      data_clean = data_clean.set_index('timestamp')

      # --------------------- Evaluating the model through our Machine Learning Pipeline Class ---------------------

      # Features we are going to use for prediction

      features = ['excursion',
                  'BOP',
                  'momentum',
                  'typical_price',
                  'money_flow',
                  'MACD',
                  'perc_K',
                  'perc_D',
                  'volume',
                  'number of trades',
                  'taker buy base asset volume',
                  'volatility']

      target_array = data_clean[['label']].values

      model = LogisticRegression(random_state=42)
      parameters = {                                       # We give a range of reasonable parameters to be tested through the Grid Search
                    'C': [0.001,0.01,0.1,1,10,100],        # (in the parameter_tuning function) of the MLPipeline class
                    'class_weight' : [None, 'Balanced'],
                    'max_iter' : [100,1000,10000]
                    }

      pipeline = MLPipeline(model=model)
      pipeline.load_data(dataset=data_clean[features],target=target_array)
      pipeline.split_data()
      pipeline.preprocessing()
      pipeline.parameter_tuning(parameters)
      pipeline.model_evaluation()
      pipeline.k_fold_cross_validation()
      pipeline.report()


import streamlit as st
from PIL import Image
import pandas as pd
import matplotlib.pyplot as plt
from plotly.subplots import make_subplots
import plotly.subplots as sp
import plotly_express as px
import plotly.graph_objs as go
from streamlit_folium import st_folium
import altair as alt

def Visualisations():
    image3 = Image.open('streamlit/images/GettyImages-155141288-polar-bear-1200x630-2.jpeg')
    st.image(image3)
    st.title("Analyses et Visualisations")

    st.subheader("Analyse des anomalies de températures")
    st.write("Hypothèse: nous faisons l’hypothèse que les variations de températures enregistrées ont tendance à être positives, ce qui signifie que la température du globe à tendance à augmenter de manière générale. Nous essaierons de déterminer les zones où les températures ont tendance à le plus augmenter et à savoir si cette tendance haussière est homogène au fil du temps.")
    st.info("Etude des anomalies globales")

    # selecteur pour slider
    values = [1, 2, 3]
    for i, value in enumerate(values):
        key = f"value_slider_{i}"

    ### Anomalies de la température globale et de l'incertitude
    ### Notebook Analyse Temp Globales + Hémisphèriques Berkeley

    # import Data
    df = pd.read_csv("Datasets/températures_globales_hémisphériques.csv", index_col=0)

    # Data Processing
    Temp_country_mean = df.groupby('Year').mean().reset_index()

    # Define the x values for the curves
    x = Temp_country_mean.Year
    # Define the two curves to plot
    y1 = Temp_country_mean['Anomaly-Gl']
    y2 = Temp_country_mean['Uncertainty-Gl']

    # Ploting
    # Create the plot trace for the first curve
    trace1 = go.Scatter(x=x, y=y1, mode='lines', name='Anomalie', showlegend=True)

    # Create the plot trace for the second curve
    trace2 = go.Scatter(x=x, y=y2, mode='lines', name='Incertitude', visible=False, showlegend=True)

    # create the layout and add the traces
    layout = go.Layout(
        title="Anomalies de la température globale et de l'incertitude de 1753 à 2022",
        xaxis=dict(title='Année'),
        yaxis=dict(title='Valeur', range=[-3, 3.5]),
        updatemenus=[dict(
            type='buttons',
            buttons=[dict(
                label="Afficher l'Incertitude",
                method='update',
                args=[{'visible': [True, not trace2.visible]}],
                args2=[{'visible': [True, trace2.visible]}],
            )]
        )]
    )

    # create the figure and plot it
    fig = go.Figure(data=[trace1, trace2], layout=layout)

    # define a callback function to update the layout
    def update_layout(trace, points, state):
        if trace.name == 'Incertitude':
            fig.layout.title = 'Anomalies de températures globales et Incertitude (Incertitude is ' + ('visible' if trace.visible else 'hidden') + ')'

    # add the callback function to the figure
    fig.data[0].on_click(update_layout)
    fig.data[1].on_click(update_layout)
    st.write(fig)

    st.write("Nous constatons que l’incertitude était très élevée lors des premières mesures, ici 1753 et qu’elle a grandement diminué pour être pratiquement nulle depuis les années 1960. Cela est dû au fait que les équipements de mesures sont de plus en plus précis et que leur fiabilité est croissante.")

    ### Anomalies des températures hémisphériques et des incertitudes
    # Define the two curves to plot
    y1 = Temp_country_mean['Anomaly-NH']
    y2 = Temp_country_mean['Uncertainty-NH']
    y3 = Temp_country_mean['Anomaly-SH']
    y4 = Temp_country_mean['Uncertainty-SH']

    # Create the plot trace for the first curve
    trace1 = go.Scatter(x=x, y=y1, mode='lines', name='Anomalie Hémisphère Nord', line=dict(color='blue'), showlegend=True)

    # Create the plot trace for the second curve
    trace2 = go.Scatter(x=x, y=y2, mode='lines', name='Incertitude Hémisphère Nord', line=dict(color='green'), visible=False, showlegend=True)

    # Create the plot trace for the first curve
    trace3 = go.Scatter(x=x, y=y3, mode='lines', name='Anomalie Hémisphère Sud', line=dict(color='orange'), showlegend=True)

    # Create the plot trace for the second curve
    trace4 = go.Scatter(x=x, y=y4, mode='lines', name='Incertitude Hémisphère Sud', line=dict(color='purple'), visible=False, showlegend=True)

    # create the layout and add the traces
    layout = go.Layout(
        title='Anomalies des températures hémisphériques et des incertitudes de 1840 à 2022',
        xaxis=dict(title='Année', range=[1840, max(x)]),
        yaxis=dict(title='Valeur', range=[-1.5, 2]),
        updatemenus=[dict(
            type='buttons',
            buttons=[dict(
                label="Afficher l'Incertitude",
                method='update',
                args=[{'visible': [True, False, True, False]}],
                args2=[{'visible': [True, True, True, True]}],
            )]
        )]
    )

    # create the figure and plot it
    fig = go.Figure(data=[trace1, trace2, trace3, trace4], layout=layout)

    # define a callback function to update the layout
    def update_layout(trace, points, state):
        if trace.name == 'Incertitude':
            fig.layout.title = 'Anomalies de températures globales et Incertitude (Incertitude is ' + ('visible' if trace.visible else 'hidden') + ')'

    # add the callback function to the figure
    fig.data[0].on_click(update_layout)
    fig.data[3].on_click(update_layout)
    st.write(fig)

    st.write("Comme pour le graphe précédent, l’incertitude était très importante au moment des premières mesures mais elle a beaucoup diminué au fur et à mesure du temps. Les données ont été collectées plus tôt dans l’hémisphère Nord et nous constatons, comme avec les données de la NASA que l’hémisphère Nord est plus impacté par le réchauffement climatique que l’hémisphère Sud")
    st.write("Nous constatons que la variation des températures est à la hausse depuis 1880, c'est-à-dire la fin de la Révolution Industrielle.. Après la fin de cette période et la fin de l’utilisation massive du charbon, la température a légèrement diminué; mais à partir de 1910 la température a suivi une courbe légèrement exponentielle à la hausse. Cette tendance haussière s’est accentuée à partir des années 1970. La crise COVID a légèrement influé sur les températures à court terme mais sur le long terme, la tendance reste la même. Cette augmentation est homogène et suit la même évolution quelque soit la zone et le mois de l’année.")
    st.write("Nous constatons également que cette hausse est plus élevée dans l’hémisphère nord que dans l’hémisphère sud. Et enfin nous constatons que c’est aux pôles que les variations de températures sont les plus importantes et c’est au pôle Nord que les températures ont le plus augmenté.")

    st.write("Avec le graphique suivant, vous pouvez choisir l'année et avoir un aperçu des anomalies sur les mois de l'année. Vous constaterez que les anomalies ont tendance à augmenter quel que soit le mois.")

    # import Data
    df_temp_pays = pd.read_csv("Datasets/Températures_par_pays_Vertical.csv")

    # Data Processing
    # Calcul de température moyenne par pays
    Temp_country_mean = df_temp_pays.groupby(['Country', 'Year']).mean().reset_index()
    # Selection de l'année à partir de 1880
    Temp_country_mean = Temp_country_mean[Temp_country_mean['Year'] >= 1880]
    # create a dictionary to map numeric values to month names
    month_names = {1: 'Janvier', 2: 'Février', 3: 'Mars', 4: 'Avril', 5: 'Mai', 6: 'Juin',
                   7: 'Juillet', 8: 'Aout', 9: 'Septembre', 10: 'Octobre', 11: 'Novembre', 12: 'Décembre'}

    # replace numeric values in Month column with month names
    df['Month_str'] = df['Month'].replace(month_names)

    ### Diagramme polaire des Anomalies de température par mois
    # create subplots with polar charts
    fig = make_subplots(rows=1, cols=1, subplot_titles=('',),
                        specs=[[{'type': 'polar'}]])

    # create traces for each year, with a slider to select the year
    for i, year in enumerate(df['Year'].unique()):
        trace = go.Scatterpolar(r=df[df['Year']==year]['Anomaly-Gl'],
                                theta=df[df['Year']==year]['Month_str'],
                                name=str(year), # convert Year in string
                                visible=(i==0), # set first year to visible
                                showlegend=False)
        fig.add_trace(trace)

    # adjust polar plot layout
    fig.update_layout(polar=dict(radialaxis=dict(range=[-3, 3])),
                      updatemenus=[dict(type='buttons',
                                        showactive=False,
                                        buttons=[dict(label='Play',
                                                      method='animate',
                                                      args=[None, {'frame': {'duration': 100, 'redraw': True},
                                                                   'fromcurrent': True,
                                                                   'transition': {'duration': 0}}]),
                                                 dict(label='Pause',
                                                      method='animate',
                                                      args=[[None], {'frame': {'duration': 0, 'redraw': False},
                                                                     'mode': 'immediate',
                                                                     'transition': {'duration': 0}}])])],
                      sliders=[dict(active=0,
                                     yanchor='top',
                                     xanchor='left',
                                     currentvalue=dict(font=dict(size=16), prefix='Year: ', visible=True, xanchor='right'),
                                     pad=dict(t=35, r=20),
                                     len=0.9,
                                     steps=[dict(label=str(year),
                                                 method='update',
                                                 args=[{'visible': [(i == j) for j in range(len(df['Year'].unique()))]},
                                                       {'title': "Anomalies de température globales pour l'année {}".format(year)}])
                                            for i, year in enumerate(df['Year'].unique())])])

    fig.update_layout(width=700, height=480)
    st.write(fig)

    st.write(" ")
    st.markdown("**Analyse des anomalies de température depuis 2000 ans**")
    st.write("A partir des données PAGES2K, nous vons obtenu le graphique suivant qui montre les anomalies de températures depuis l'année 0 jusqu'en 2014.")
    st.write("Visualisation avec uniquement les valeurs de la Full Ensemble Median ainsi que la moyenne filtrée sur une période de 31 ans (31-year filtered full ensemble median)")
    image_pages2K = Image.open('streamlit/Graphs/Courbe Temp depuis 2000 ans V2.png')
    st.image(image_pages2K)
    st.write("Nous constatons que la courbe est assez plate pendant le 1er millénaire, avec une légère hausse vers la période 1000-1200 puis une baisse allant d’environ 1500 à 1800, puis une montée très prononcée à partir du 20 ème siècle.")
    st.write("Les données montrent que la période moderne est très différente de ce qui s'est passé dans le passé. La période chaude médiévale et le petit âge glaciaire souvent cités sont des phénomènes réels, mais petits par rapport aux changements récents.")
    st.write("Le réchauffement au cours des 50 dernières années est brutal par rapport aux variations qui se sont produites naturellement au cours des 2000 dernières années.")
    st.write(" ")

    # import Data
    df_temp_pays = pd.read_csv("Datasets/Températures_par_pays_Vertical.csv")

    # Data Processing
    # Calcul de température moyenne par pays
    Temp_country_mean = df_temp_pays.groupby(['Country', 'Year']).mean().reset_index()
    # Selection de l'année à partir de 1880
    Temp_country_mean = Temp_country_mean[Temp_country_mean['Year'] >= 1880]
    # create a dictionary to map numeric values to month names
    month_names = {1: 'Janvier', 2: 'Février', 3: 'Mars', 4: 'Avril', 5: 'Mai', 6: 'Juin',
                   7: 'Juillet', 8: 'Aout', 9: 'Septembre', 10: 'Octobre', 11: 'Novembre', 12: 'Décembre'}

    # replace numeric values in Month column with month names
    df['Month_str'] = df['Month'].replace(month_names)

    st.text("")
    st.info("Etude des anomalies par pays")

    st.text("")
    st.write("Avec le sélecteur suivant, vous pouvez afficher les anomalies de température par pays.")

    # Creation d'un selecteur mulit-pays pour afficher les courbes de températures
    countries = st.multiselect('Sélectionner un ou plusieurs pays', Temp_country_mean['Country'].unique())

    # Filter the data by selected countries
    df_countries = Temp_country_mean[Temp_country_mean['Country'].isin(countries)]

    # Create a line plot of temperature anomaly and CO2 for selected countries
    fig = go.Figure()
    for country in countries:
        df_temp = df_countries[df_countries['Country'] == country]
        fig.add_trace(go.Scatter(x=df_temp['Year'], y=df_temp['Anomaly'], name=f'{country}'))
    fig.update_layout(title='Anomalies de températures', xaxis_title='Anomalies', yaxis_title='Valeur')
    st.plotly_chart(fig)
    st.text("")

    st.write("Nous constatons que les tendances des anomalies de températures sont sensiblement identiques sur tous les pays et que ce phénomène est donc globale dans le temps.")

    ### Données par pays
    ### Notebook Analyse Température par pays - Cartes interactives

    # creation d'une fonction pour créer une colonne classe en fonction des variations
    def map_anomaly(x):
        if x < 1:
            return 'moins de 1°'
        elif x < 2:
            return '1° à 2°'
        elif x < 3:
            return '2° à 3°'
        else:
            return 'plus de 3°'

    # use the apply() function to create a new column based on the values in the 'Anomaly' column
    Temp_country_mean['Classe'] = Temp_country_mean['Anomaly'].apply(map_anomaly)

    # Pays avec les anomalies les plus importantes
    top_15 = Temp_country_mean.groupby('Country').sum().sort_values('Anomaly', ascending=False)[:15].reset_index()['Country']

    # Pays avec les anomalies les moins importantes
    bottom_15 = Temp_country_mean.groupby('Country').sum().sort_values('Anomaly', ascending=True)[:15].reset_index()['Country']

    st.write("")
    st.write("A partir des données à notre disposition, nous avons sélectionner 30 pays, 15 pays les plus impactés et 15 pays les moins impactés. La sélection a été réalisée à partir de la valeur moyenne des anomalies de températures.")
    st.write("Vous pouvez voir un aperçu des pays les plus et les moins impactés par le changement climatiques. Avec les 2 graphiques suivants, nous voyons que la hausse des températures affectent tous les pays, sur tous les continents.")
    # Visualisation des anomalies de température pour les pays les plus impactés
    Temp_country_mean_top = Temp_country_mean[Temp_country_mean['Country'].isin(top_15)]
    fig = px.bar(Temp_country_mean_top, x='Anomaly', y='Country', animation_frame='Year', hover_name='Anomaly',
    range_x=[-3,4],color='Country', labels={
                  "Anomaly": "Anomalie",
                  "Country": "Pays"}, title = "Anomalies de température pour les 15 pays les plus impactés",width=800, height=500)
    fig.update_layout(showlegend=False)
    st.write(fig)

    # Visualisation des anomalies de température pour les pays les moins impactés
    Temp_country_mean_bottom = Temp_country_mean[Temp_country_mean['Country'].isin(bottom_15)]
    fig = px.bar(Temp_country_mean_bottom,x='Anomaly', y='Country', animation_frame='Year', hover_name='Anomaly',
      range_x=[-3,4], color='Country', labels={
                            "Anomaly": "Anomalie", "Country": "Pays"},
          title = "Anomalies de température pour les 15 pays les moins impactés", width=800, height=500)
    fig.update_layout(showlegend=False)
    st.write(fig)

    st.write("Nous constatons que tous les pays sont impactés par la hausse des températures. Nous faisons l'hypothèse que la hausse est plus importante pour les pays les plus industrialisés de l'hémisphère Nord, tandis que les hausses les plus faibles sont constatés sur des territoires peu industrialisés et situé majoritairement dans l'hémisphère Sud.")
    st.write("")

    ### carte interactive
    st.markdown("**Carte interactive des anomalies de températures**")
    st.write("Avec la carte suivante, vous pouvez afficher les anomalies de températures en sélectionnant l'année. En zoomant et en pointant sur un pays, vous pouvez avoir une vision précise des anomalies à un instant T.")
    st.write("Choississez l'année pour voir les mesures:")
    # remplacement de noms de pays en corrélation avec les nom s dans notre fichier geaojson
    Temp_country_mean = Temp_country_mean.replace(to_replace=["Dominican", "Burma", "Bosnia And Herzegovina", "Czech Republic", 'United States', "Ivorycoast", "Congo (Democratic Republic Of The)", 'Tanzania'], value=["Dominican Republic", "Myanmar", "Bosnia and Herzegovina", "Czechia", "United States of America", "Ivory Coast", "Democratic Republic of the Congo", "United Republic of Tanzania"])

    # Create the slider for the year
    year = st.slider("Année", min_value=1880, max_value=2020, value=1880)

    # Filter the data by the selected year
    data = Temp_country_mean[Temp_country_mean["Year"] == year]

    # Create the figure
    fig = px.choropleth(data,
                        locations="Country",
                        locationmode="country names",
                        color="Anomaly",
                        color_continuous_scale=px.colors.sequential.Plasma_r,
                        range_color=[-2.5, 4],
                        labels={"Anomaly": "Anomalies de température en °C"})

    # Update the layout
    fig.update_layout(title=f"Anomalies de température pour l'année {year}", margin=dict(t=50, b=50),height=400)

    # Display the figure
    st.plotly_chart(fig)

    st.write("Nous constatons que globalement l'ensemble des pays subissent une hausse des températures. Cette évolution n'est pas similaire pour tous les pays et certains sont très touchés par le réchauffement climatique alors que d'autres le sont beaucoup moins.")
    st.write("A partir de l’analyse et de la visualisation des données, nous démontrons que la hausse des températures est apparue à la fin de la Révolution Industrielle, qu’elle s’est accentuée à partir des années 1970, quel que soit le mois de l’année. Les pays de l'hémisphère Nord sont les plus impactés car on trouve dans cette zone les pays les plus industrialisés. Le pôle Nord est l’endroit le plus impacté par le changement climatique.")

    ###Analyse du C02
    st.text("")
    st.subheader("Analyse des émissions de CO2")

    st.markdown("**Analyse du CO2 dans l’atmosphère**")
    st.write("Pour poursuivre dans notre étude du réchaffement climatique, nous avons fait l'hypothèse que la hausse des températures est liée à la hausse des gaz à effet de serre dans l'atmosphère.")

    st.write("Nous avons tout d'abord utilisé les données du Climate & Energy College. Après traitement des données nous faisons apparaître 2 graphiques:")
    st.write("Le 1er graphique montre le taux de C02 globale dans l’atmosphère depuis l’année 0. Ce taux était assez stationnaire mais nous constatons que depuis le début du 19ème siècle et l’avènement de la révolution industrielle le taux a très fortement augmenté.")
    image_co2_1 = Image.open('streamlit/Graphs/C02 0 à 2020.png')
    st.image(image_co2_1)
    st.write("Le 2ème graphique est un zoom sur la période 1880 à 2014, période similaire à notre jeu de données sur les anomalies de températures.Ce graphique nous montre que le taux à très largement augmenté depuis 1880 avec une tendance haussière encore plus prononcée depuis 1960. Nous constatons également que le taux de CO2 est légèrement plus élevé dans le l’hémisphère Nord que dans le l’hémisphère Sud et que cet écart à tendance à augmenter au fil du temps.")
    image_co2_2 = Image.open('streamlit/Graphs/C02 1880 à 2020.png')
    st.image(image_co2_2)

    st.markdown("**Emissions de CO2 par pays**")

    # import Data CO2
    df_co2 = pd.read_csv("Datasets/co2_par_pays.csv")

    #Data processing
    df_co2 = df_co2.rename({'country':'Country', 'year':'Year'}, axis=1)
    df_temp_co2 = Temp_country_mean.merge(right = df_co2, on = ['Year', 'Country'], how = 'outer')
    st.write("Avec le sélecteur suivant, vous pouvez afficher les émissions de C02 par pays.")

    # Creation d'un selecteur mulit-pays pour afficher les courbes de températures
    countries = st.multiselect('Sélectionner un ou plusieurs pays', df_co2['Country'].unique())

    # Filter the data by selected countries
    df_countries_co2 = df_co2[df_co2['Country'].isin(countries)]

    # Create a line plot of temperature anomaly and CO2 for selected countries
    fig = go.Figure()
    for country in countries:
        df_co2_2 = df_countries_co2[df_countries_co2['Country'] == country]
        fig.add_trace(go.Scatter(x=df_co2_2['Year'], y=df_co2_2['co2'], name=f'{country}'))
    fig.update_layout(title='Emissions de CO2', xaxis_title='Année', yaxis_title='Emissions en millions de tonnes')
    st.plotly_chart(fig)

    st.markdown("**Carte interactive des émissions de CO2**")
    st.write("Avec la carte suivante, vous pouvez afficher les émissions de CO2 en sélectionnant l'année. En zoomant et en pointant sur un pays, vous pouvez avoir une vision précise des émissions à un instant T.")

    st.write("Choississez l'année pour voir les mesures:")

           ### Carte interactive émissions de C02 par pays en fonction de l'année

    # Create the slider for the year
    year_co2 = st.slider("Année", min_value=1880, max_value=2021, value=1880)

    # Filter the data by the selected year co2
    data_2 = df_co2[df_co2["Year"] == year_co2]

    # Create the figure
    fig = px.choropleth(data_2,
                        locations="Country",
                        locationmode="country names",
                        color="co2",
                        color_continuous_scale=px.colors.sequential.Plasma_r,
                        range_color=[0, 8000],
                        labels={"co2": "Emissions de CO2 en millions de tonnes"})

    # Update the layout
    fig.update_layout(title=f"Emissions de CO2 pour l'année {year_co2}", margin=dict(t=50, b=50),height=400)

    # Display the figure
    st.plotly_chart(fig)

    st.write("Le taux de C02 dans l’atmosphère suit les mêmes tendances que les anomalies de C02. Ce n’est qu’à partir de la fin du XIXème siècle que le taux de CO2 à commencer à fortement augmenté, porté par la Révolution Industrielle et le développement des pays industrialisés.Ces émissions de CO2 n’ont rien d'égal avec ce que la terre a connu depuis 2 millénaires. Nous allons donc nous pencher un peu plus sur la relation entre anomalies de températures et émissions de C02.")
    st.write("")
    st.write("")

    ### Notebook: Analyse CO2 par pays V2
    st.write("L’évolution des émissions de CO2 dans les grands pays industrialisés et les grands pôles d’activités n’est pas la même. Dans les pays d’Europe et les Etats-Unis, les émissions de CO2 ont globalement reculées, contrairement à la Chine ou l’Asie.")
    st.write("Pour les pays avec un taux de revenu élevé (High-income countries), les émissions sont plus importantes, mais depuis 2008 et le choc économique les émissions ont baissé. Pour les pays avec un plus faible taux de revenu (Upper-middle-income et lower-middle-income countries), les émissions étaient très faibles avant 1945 mais elles ont très fortement augmenté depuis, sans pour autant atteindre celle de l’autre groupe de pays. On constate une inflexion sur l'ensemble des courbes despays lors de la crise du COVID-19.")
    st.write("Si l'on observe l’évolution des émissions des membres de l’OCDE, celle-çi est très similaire à celle des pays ayant le plus de revenus. Elle suit une tendance haussière jusqu’en 2008 puis à tendance à baisser. Alors que la courbe des pays non-membres suit une tendance haussière exponentielle, portée principalement par les émissions de la Chine. A partir du début des années 2000, ceux ce sont désormais les pays non-membres de l’OCDE qui sont les plus émetteurs de CO2.")
    st.write("")

    ### Comparaison C02 et température
    st.subheader("Comparaison entre les anomalies de température et les émissions de C02")
    st.write("Dans ce graphique, nous superposons le taux de CO2 et les anomalies de température sur la période de 1880 à nos jours en mergeant ce dataset avec celui des températures globales de la Nasa (GLB.Ts+dSST). Nous constatons que les 2 courbes suivent la même tendance haussière avec la même inflexion vers le haut depuis le milieu du XXème siècle. Il y a donc de très fortes probabilités que ces 2 paramètres soient liés et que la hausse du taux de gaz à effet de serre dans l’atmosphère contribue à faire augmenter la température à la surface du globe. Nous décidons d’utiliser une méthode statistique pour montrer la corrélation entre la hausse des températures et l'augmentation des émissions de CO2. Nous avons donc effectué un test de corrélation de Pearson entre le taux de CO2 globale et les anomalies globales de température.")
    image_co2_3 = Image.open('streamlit/Graphs/C02+Temp 1880 à 2020.png')
    st.image(image_co2_3)
    st.write("Nous obtenons les résultats de corrélation suivants:")
    st.write("Coefficient de corrélation: 0.9276260482581129")
    st.write("P Value : 9.717225418853804e-59")
    st.write("Les résultats prouvent qu’il existe une forte corrélation positive et statistiquement significative  entre le taux de CO2 et les anomalies de température.")

    st.write("")
    st.markdown("**Anomalies de température et émissions de CO2 par pays**")

    st.write("Avec le graphique interactif suivant, vous pouvez comparer les anomalies de températures et les émissions de CO2 en sélectionnant le pays.")

    # Create the multi-select widget for the countries
    countries = st.multiselect("Sélectionner un ou plusieurs pays", df_temp_co2["Country"].unique())

    # Filter the data by the selected countries
    data_co2_temp = df_temp_co2[df_temp_co2["Country"].isin(countries)]

    chart_anomaly = (
        alt.Chart(data_co2_temp)
        .mark_line(color="red")
        .encode(
            x="Year:O",
            y=alt.Y("Anomaly:Q", scale=alt.Scale(domain=[-2, 4])),
            color=alt.Color("Country:N", legend=alt.Legend(title="Pays"))
        )
        .properties(width=300, height=350, title="Anomalies de températures")
    )

    chart_co2 = (
        alt.Chart(data_co2_temp)
        .mark_line(color="blue")
        .encode(
            x="Year:O",
            y=alt.Y("co2:Q", scale=alt.Scale(domain=[0,11000])),
            color=alt.Color("Country:N", legend=alt.Legend(title="Pays"))
        )
        .properties(width=300, height=350, title="Emissions de co2 en millions de tonnes")
    )

    # Display charts
    st.altair_chart(chart_anomaly | chart_co2)

    st.write("Cette étude des émissions de CO2 nous montre que les anomalies de température sont très fortement liées à la hausse des émissions de CO2 au niveau mondial. On constate que des pays ont fait des efforts pour réduire leurs émissions de CO2 mais la montée en puissance de la Chine et des pays en voie de développement a contrebalancé ce phénomène. La hausse des émissions est croissante et ce phénomène va sûrement provoquer une hausse de la température globale de la Terre.")


import numpy as np
from tslearn.shapelets import LearningShapelets
import timeit
import files,seqs#,feats

def make_feats(in_path,out_path):
    model=train_model(in_path)
    def helper(name_i,data_i):
        print(name_i)
        print(data_i.shape)
        frames=seqs.inter(data_i,36)
        frames= np.squeeze(frames)
        frames=np.expand_dims(frames,axis=0)
        distances = model.transform(frames)
        return distances
    seqs.to_feats(in_path,helper,out_path=out_path)

def train_model(in_path):
    ts=seqs.read_seqs(in_path) #from_paths(paths)
    ts=ts.split()[0]
    ts.resize(36)
    model = LearningShapelets(n_shapelets_per_size=None,max_iter=2) #{3: 40})
    ts=ts.random_subset(k=10)
    X,y,names=ts.as_dataset()
    model.fit(X,y)
    return model

def compute_shaplets(in_path,out_path,n_feats=40):
    ts=seqs.read_seqs(in_path)
    ts.resize(64)
    train,test=ts.split()
    model = LearningShapelets(n_shapelets_per_size={3: n_feats})
    train_X,train_y,train_names=train.as_dataset()
    model.fit(train_X,train_y)
    X,y,names=ts.as_dataset()
    print(X.shape)
    distances = model.transform(X)
    dist_feat=feats.Feats()
    for i,x_i in enumerate(distances):
        dist_feat[names[i]]=x_i
        print(x_i.shape)
    dist_feat.save(out_path)

in_path="3DHOI/shape_32"
out_path="3DHOI/feats"
make_feats(in_path,out_path)