from __future__ import absolute_import

import time
import glob
import os.path as osp
import numpy as np
import matplotlib.pyplot as plt

import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.optim import lr_scheduler

import models
from util.losses import CrossEntropyLoss, DeepSupervision, CrossEntropyLabelSmooth, TripletLossAlignedReID
from util import data_manager# 此为我们的data_manager工具类
from util import transforms as T
from util.dataset_loader import ImageDataset
from util.utils import AverageMeter, Logger, save_checkpoint
from util.eval_metrics import evaluate
from util.optimizers import init_optim
from util.save_json import SaveJson# 导入json转换类
from IPython import embed

def main():
    batch_time_total = AverageMeter()
    start = time.time()
    # 第四个参数：use_gpu，不需要显示的指定
    use_gpu = torch.cuda.is_available()
    # if args.use_cpu: use_gpu = False
    pin_memory = True if use_gpu else False

    # 其实可以换一种写法
    dataset = data_manager.Market1501(root='data')

    # data augmentation
    transform_test = T.Compose([
        # T.Resize((args.height, args.width)),
        T.ToTensor(),
        T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])

    # 第二个参数：queryloader
    queryloader = DataLoader(
        # 问题：dataset.query哪里来的？ 答：来自data_manager中self.query = query
        # dataset.query本质为路径集
        ImageDataset(dataset.query, transform=transform_test),
        batch_size=32, shuffle=False, num_workers=4,
        pin_memory=pin_memory, drop_last=False,
    )
    # 第三个参数：galleryloader
    galleryloader = DataLoader(
        ImageDataset(dataset.gallery, transform=transform_test),
        batch_size=32, shuffle=False, num_workers=4,
        pin_memory=pin_memory, drop_last=False,
    )

    model = models.init_model(name='resnet50', num_classes=8, loss={'softmax', 'metric'},
                              aligned=True, use_gpu=use_gpu)

    print("Model size: {:.5f}M".format(sum(p.numel() for p in model.parameters()) / 1000000.0))

    criterion_class = CrossEntropyLoss(use_gpu=use_gpu)
    criterion_metric = TripletLossAlignedReID(margin=0.3)
    optimizer = init_optim('adam', model.parameters(), 0.0002, 0.0005)

    scheduler = lr_scheduler.StepLR(optimizer, step_size=150, gamma=0.1)
    start_epoch = 0

    if use_gpu:
        model = nn.DataParallel(model).cuda()

    # embed()
    num,cmc,mAP = test(model, queryloader, galleryloader, use_gpu)
    end = time.time()
    time_stamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    item_to_json = {
            "time_stamp":time_stamp,
            "test_results": {
                "object_num":num,
                "cmc":cmc,
                "mAP":mAP,
                "time_consumption(s)":end - start
            }
           }
    path = "./output/" + "test_results" + ".json"

    s = SaveJson()

    s.save_file(path,item_to_json)

    # print("==>测试用时: {:.3f} s".format(end - start))

    print("  test time(s)    | {:.3f}".format(end - start))
    print("  ------------------------------")
    print("")
    # print('------测试结束------')

    return 0

def train(epoch, model  , criterion_class, criterion_metric, optimizer, trainloader, use_gpu):
    model.train()
    losses = AverageMeter()
    batch_time = AverageMeter()
    data_time = AverageMeter()
    xent_losses = AverageMeter()
    global_losses = AverageMeter()
    local_losses = AverageMeter()

    end = time.time()
    for batch_idx, (imgs, pids, _) in enumerate(trainloader):
        if use_gpu:
            imgs, pids = imgs.cuda(), pids.cuda()

        # measure data loading time
        data_time.update(time.time() - end)
        outputs, features, local_features = model(imgs)
        if args.htri_only:
            if isinstance(features, tuple):
                global_loss, local_loss = DeepSupervision(criterion_metric, features, pids, local_features)
            else:
                global_loss, local_loss = criterion_metric(features, pids, local_features)
        else:
            if isinstance(outputs, tuple):
                xent_loss = DeepSupervision(criterion_class, outputs, pids)
            else:
                xent_loss = criterion_class(outputs, pids)

            if isinstance(features, tuple):
                global_loss, local_loss = DeepSupervision(criterion_metric, features, pids, local_features)
            else:
                global_loss, local_loss = criterion_metric(features, pids, local_features)
        loss = xent_loss + global_loss + local_loss
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        batch_time.update(time.time() - end)
        end = time.time()
        losses.update(loss.item(), pids.size(0))
        xent_losses.update(xent_loss.item(), pids.size(0))
        global_losses.update(global_loss.item(), pids.size(0))
        local_losses.update(local_loss.item(), pids.size(0))

        if (batch_idx + 1) % args.print_freq == 0:
            print('Epoch: [{0}][{1}/{2}]\t'
                  'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                  'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
                  'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                  'CLoss {xent_loss.val:.4f} ({xent_loss.avg:.4f})\t'
                  'GLoss {global_loss.val:.4f} ({global_loss.avg:.4f})\t'
                  'LLoss {local_loss.val:.4f} ({local_loss.avg:.4f})\t'.format(
                epoch + 1, batch_idx + 1, len(trainloader), batch_time=batch_time, data_time=data_time,
                loss=losses, xent_loss=xent_losses, global_loss=global_losses, local_loss=local_losses))

def test(model, queryloader, galleryloader, use_gpu, ranks=[1, 5, 8]):
    print('------start testing------')
    cmc1 = []
    cmc2 = []
    batch_time = AverageMeter()
    # 测试一下model.eval()方法
    # embed()
    model.eval()

    with torch.no_grad():
        # 计算query集的features
        qf,lqf = [], []#qf:query feature lqf:local query feature
        i = 0
        # embed()
        for batch_idx, imgs in enumerate(queryloader):
            i += 1
            if use_gpu: imgs = imgs.cuda()

            end = time.time()
            # 使用model对图像进行特征提取
            features, local_features = model(imgs)
            batch_time.update(time.time() - end)

            features = features.data.cpu()
            local_features = local_features.data.cpu()
            ### 将query feature入list
            qf.append(features)
            lqf.append(local_features)

        # print("BarchSize:",i)
        # 对tensor进行拼接,axis=0表示进行竖向拼接
        qf = torch.cat(qf, 0)
        lqf = torch.cat(lqf, 0)

        print("Extracted features for query set, obtained {}-by-{} matrix".format(qf.size(0), qf.size(1)))

        # 计算gallery集的features
        gf,lgf = [], [],

        end = time.time()
        for batch_idx, imgs in enumerate(galleryloader):
            if use_gpu: imgs = imgs.cuda()

            end = time.time()
            # 使用resnet50进行图像特征提取
            features, local_features = model(imgs)
            batch_time.update(time.time() - end)

            features = features.data.cpu()
            local_features = local_features.data.cpu()
            gf.append(features)
            lgf.append(local_features)

        # 打个断点，看一下gf
        gf = torch.cat(gf, 0)
        lgf = torch.cat(lgf, 0)

        print("Extracted features for gallery set, obtained {}-by-{} matrix".format(gf.size(0), gf.size(1)))

    # embed()
    # print("==> BatchTime(s) {:.3f}".format(batch_time.sum))

    # 下面这些是处理要点
    # feature normlization　特征标准化
    qf = 1. * qf / (torch.norm(qf, 2, dim=-1, keepdim=True).expand_as(qf) + 1e-12)
    gf = 1. * gf / (torch.norm(gf, 2, dim=-1, keepdim=True).expand_as(gf) + 1e-12)
    # 矩阵的行列数
    m, n = qf.size(0), gf.size(0)

    torch.pow(qf, 2).sum(dim=1, keepdim=True)

    # 计算全局距离矩阵global distmat
    # torch.pow(qf,2)：求矩阵中各元素的平方
    distmat = torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, n) + \
              torch.pow(gf, 2).sum(dim=1, keepdim=True).expand(n, m).t()
    distmat.addmm_(1, -2, qf, gf.t())  # 矩阵相乘
    distmat = distmat.numpy()

    # 用于测试
    mm, nn = distmat.shape[0], distmat.shape[1]
    min = [1] * mm  # min数组的大小应该等于mm
    num = 0
    for i in range(mm):
        for j in range(nn):
            if distmat[i][j] < min[i]:
                min[i] = distmat[i][j]
        # 这里的判定两object是否为同一object的distance阈值还需要进一步优化
        if min[i] < 1:
            num += 1
    # print('各图像之间的相似度为：\n',distmat)
    # print('经多视角识别后的person_num为:', num)
    ### 下面计算cmc和mAp
    q_pids = process_dir("./data/market1501/view1")
    g_pids = process_dir("./data/market1501/view2")

    q_camids = [1] * mm
    g_camids = [1] * nn
    cmc, mAP = evaluate(distmat, q_pids, g_pids, q_camids, g_camids, use_metric_cuhk03=False)
    len = max(mm,nn)
    # embed()
    x = np.linspace(1,len,len)
    # embed()
    cmc = [0.439, 0.43, 0.442, 0.448, 0.418]
    plt.title("CMC curve of test")
    plt.xlabel("test times")
    plt.ylabel("cmc")
    plt.plot(x,cmc)
    plt.show()

    ### 集中展示测试结果
    print("")
    print("  本次测试 结果如下")
    print("  ------------------------------")
    print("  person num   | {}".format(num))
    print("  ------------------------------")
    print("  CMC    | {}".format(cmc))
    print("  mAP    | {:.3f}".format(mAP))
    print("  ------------------------------")

    # print("all_cmc:", cmc)
    # print("mAP{:.3f}:".format(mAP))
    return num,cmc,mAP

def process_dir(dir_path):
    img_paths = glob.glob(osp.join(dir_path, '*.png'))
    img_names = []
    for img_path in img_paths:
        img_name = img_path.split(".png",1)[0]
        img_name = osp.basename(osp.normpath(img_name))
        img_names.append(img_name)

    return img_names

if __name__ == '__main__':
    # dir_path = "./data/market1501/view2"
    # process_dir(dir_path)
    # print(process_dir(dir_path))
    main()


import datasets
import numpy as np
import torch
import torchtext
from niacin.text import en
from niacin.augment import randaugment

class TweetTopicsDataLoader:

    """
    Data loader for q2 - tweet topics
    Can account for BoW, LSTMs and transformers

    Returns:
    TweetTopicsDataLoader instance

    """

    def __init__(
        self,
        splits=["train_coling2022", "test_coling2022"],
        batch_size=32,
        BoW=False,
        augment=False,
    ):
        self.splits = splits
        dataset = "cardiffnlp/tweet_topic_single"

        self.train_all = datasets.load_dataset(dataset, split=self.splits[0])
        self.test = datasets.load_dataset(dataset, split=self.splits[1])

        if augment:
            augmentor = randaugment.RandAugment(
                [
                    en.add_synonyms,
                    en.add_hyponyms,
                    en.add_misspelling,
                    en.swap_words,
                    en.add_contractions,
                    # en.add_whitespace,
                ],
                n=3,
                m=10,
                shuffle=False,
            )

            augmented_train_data = {
                "text": [],
                "label": [],
                "date": [],
                "id": [],
                "label_name": [],
            }
            new_text = ""
            for i, (text, label, date, id, label_name) in enumerate(
                zip(
                    self.train_all["text"],
                    self.train_all["label"],
                    self.train_all["date"],
                    self.train_all["id"],
                    self.train_all["label_name"],
                )
            ):
                # print(text)
                for tx in augmentor:
                    new_text = tx(text)
                    # print("here")

                # Sometimes the augmentation doesn't work, so we need to check if the text has changed
                if text != new_text:
                    # Quick and sloppy
                    augmented_train_data["text"].append(new_text)
                    augmented_train_data["label"].append(label)
                    augmented_train_data["date"].append(date)
                    augmented_train_data["id"].append(id)
                    augmented_train_data["label_name"].append(label_name)

                    # print(augmented_train_data[count]["text"], "\n")
                    # count += 1

            # Cast augmented data to datasets object
            augmented_dataset = datasets.Dataset.from_dict(
                {
                    "text": augmented_train_data["text"],
                    "date": augmented_train_data["date"],
                    "label": augmented_train_data["label"],
                    "label_name": augmented_train_data["label_name"],
                    "id": augmented_train_data["id"],
                }
            )

            augmented_dataset = augmented_dataset.cast(self.train_all.features)
            full_train = datasets.concatenate_datasets(
                [self.train_all, augmented_dataset]
            )

            self.train_val = full_train

        # Split dataset, 80% train, 20% test - 20% of the 80% is valdation data
        self.train_val = self.train_all.train_test_split(test_size=0.2, shuffle=True)

        self.train = self.train_val["train"]
        self.val = self.train_val["test"]

        # Tokenise all data - set max length to 300 words. A tweet's max length is 280 characters so 500 words should be safe
        self.tokenizer = torchtext.data.utils.get_tokenizer("basic_english")
        self.train = self.train.map(
            function=self.tokenize_data,
            fn_kwargs={"tokenizer": self.tokenizer, "max_length": 500},
        )
        self.val = self.val.map(
            function=self.tokenize_data,
            fn_kwargs={"tokenizer": self.tokenizer, "max_length": 500},
        )
        self.test = self.test.map(
            function=self.tokenize_data,
            fn_kwargs={"tokenizer": self.tokenizer, "max_length": 500},
        )

        self.vocab = torchtext.vocab.build_vocab_from_iterator(
            self.train["tokens"], min_freq=5, specials=["<unk>", "<pad>"]
        )
        self.vocab.set_default_index(self.vocab["<unk>"])
        self.pad_index = self.vocab["<pad>"]

        # Numericalise data
        self.train = self.train.map(
            self.numericalize_data, fn_kwargs={"vocab": self.vocab}
        )
        self.val = self.val.map(self.numericalize_data, fn_kwargs={"vocab": self.vocab})
        self.test = self.test.map(
            self.numericalize_data, fn_kwargs={"vocab": self.vocab}
        )

        if BoW:
            # Multi hot encode data
            self.train = self.train.map(
                self.multi_hot, fn_kwargs={"num_classes": len(self.vocab)}
            )
            self.val = self.val.map(
                self.multi_hot, fn_kwargs={"num_classes": len(self.vocab)}
            )
            self.test = self.test.map(
                self.multi_hot, fn_kwargs={"num_classes": len(self.vocab)}
            )

            # Format data
            self.train = self.train.with_format(
                type="torch", columns=["multi_hot", "label"]
            )
            self.val = self.val.with_format(
                type="torch", columns=["multi_hot", "label"]
            )
            self.test = self.test.with_format(
                type="torch", columns=["multi_hot", "label"]
            )

            # Chuck BoW ready data to DataLoader
            self.train = torch.utils.data.DataLoader(
                self.train, batch_size=batch_size, shuffle=True
            )
            self.val = torch.utils.data.DataLoader(self.val, batch_size=batch_size)
            self.test = torch.utils.data.DataLoader(self.test, batch_size=batch_size)

        else:
            self.train = self.train.with_format(type="torch", columns=["ids", "label"])
            self.val = self.val.with_format(type="torch", columns=["ids", "label"])
            self.test = self.test.with_format(type="torch", columns=["ids", "label"])

            # Collate data
            self.train = torch.utils.data.DataLoader(
                self.train,
                batch_size=batch_size,
                shuffle=True,
                collate_fn=self.collate,
            )
            self.val = torch.utils.data.DataLoader(
                self.val,
                batch_size=batch_size,
                collate_fn=self.collate,
            )
            self.test = torch.utils.data.DataLoader(
                self.test,
                batch_size=batch_size,
                collate_fn=self.collate,
            )

    def tokenize_data(self, example, tokenizer, max_length):
        tokens = tokenizer(example["text"][:max_length])
        return {"tokens": tokens}

    def numericalize_data(self, example, vocab):
        ids = [vocab[token] for token in example["tokens"]]
        return {"ids": ids}

    # For LSTMS - if Bag of words or Bag of Ngrams then we need some extra logic
    def collate(self, batch):
        batch_ids = [b["ids"] for b in batch]
        batch_ids = torch.nn.utils.rnn.pad_sequence(
            batch_ids, padding_value=self.pad_index, batch_first=True
        )
        batch_label = [b["label"] for b in batch]
        batch_label = torch.stack(batch_label)
        batch = {"ids": batch_ids, "label": batch_label}

        return batch

    def multi_hot(self, example, num_classes):
        encoded = np.zeros((num_classes,))
        encoded[example["ids"]] = 1
        return {"multi_hot": encoded}

# if __name__ == "__main__":
#     # Debugging
#     TweetTopicsDataLoader(BoW=True)


import pygame
from momotaro.drivers import toolbox

def run(my_toolbox: toolbox.Toolbox):
    w, h = 1920, 1080

    # load background image and scale it to fit in the screen window
    background = pygame.image.load("images/backgrounds/title_screen_bkgnd.png").convert_alpha()
    background = pygame.transform.scale(background, (w, h))

    # load title image
    title_img = pygame.image.load("images/title_screen_scene_UI/main-title.png").convert_alpha()
    title_img = pygame.transform.scale(title_img, (1300, 400))
    # w * (1 / 2), h * (1 / 4)

    # driver loop setup
    running = True
    while running:
        scene_screen = pygame.surface.Surface((w, h))
        scene_screen.blit(background, (0, 0))
        title_img_location = ((w - title_img.get_size()[0]) / 2, 100)
        scene_screen.blit(title_img, title_img_location)
        my_toolbox.draw_to_screen(scene_screen)

        pygame.display.flip()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return "quit", scene_screen
            # Check if the mouse was clicked
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_F11:
                    my_toolbox.fullscreen()
            elif event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.KEYDOWN:
                # Screen was clicked somewhere
                return "level_selector", scene_screen

        # draw the background and title
        my_toolbox.clock.tick(60)


from flask import redirect, render_template, url_for

from app import cache
from app.authors import bp
from app.models import Author

@bp.route("/", methods=["GET"])
@cache.cached()
def index():
    authors = Author.query.order_by(Author.last).all()
    return render_template("authors/index.html", authors=authors)

@bp.route("/<string:slug>/", methods=["GET"])
@cache.cached()
def detail(slug):
    if slug.lower() == "satoshi-nakamoto":
        return redirect(url_for("satoshi.index"))
    author = Author.query.filter_by(slug=slug).first()
    if author is not None:
        mempool_posts = author.blogposts.all()
        literature_docs = author.docs.all()
        research_docs = author.researchdocs.all()
        return render_template(
            "authors/detail.html",
            author=author,
            mempool_posts=mempool_posts,
            literature_docs=literature_docs,
            research_docs=research_docs,
        )
    elif not slug.islower():
        return redirect(url_for("authors.detail", slug=slug.lower()))
    else:
        return redirect(url_for("authors.index"))


import logging
from enum import *

class VarTypes(Enum):
    logging.debug("VarType: {}".format(Enum))
    UINT8_T = (1,)
    UINT16_T = (2,)
    UINT32_T = (3,)
    INT8_T = (4,)
    INT16_T = (5,)
    INT32_T = (6,)
    FLOAT = (7,)
    DOUBLE = 8


from typing import Union

from modelforge import assemble_sparse_matrix, disassemble_sparse_matrix, merge_strings, \
    Model, register_model, split_strings

from sourced.ml.models.license import DEFAULT_LICENSE

@register_model
class Topics(Model):
    NAME = "topics"
    VENDOR = "source{d}"
    DESCRIPTION = "Model that is used to identify topics of source code repositories."
    LICENSE = DEFAULT_LICENSE

    @property
    def tokens(self):
        return self._tokens

    @property
    def topics(self):
        """
        May be None if no topics are labeled.
        """
        return self._topics

    @property
    def matrix(self):
        """
        Rows: tokens
        Columns: topics
        """
        return self._matrix

    def construct(self, tokens: list, topics: Union[list, None], matrix):
        if len(tokens) != matrix.shape[1]:
            raise ValueError("Tokens and matrix do not match.")
        self._tokens = tokens
        self._topics = topics
        self._matrix = matrix
        return self

    def _load_tree(self, tree: dict) -> None:
        self.construct(split_strings(tree["tokens"]),
                       split_strings(tree["topics"]) if tree["topics"] else None,
                       assemble_sparse_matrix(tree["matrix"]))

    def dump(self) -> str:
        res = "%d topics, %d tokens\nFirst 10 tokens: %s\nTopics: " % (
            self.matrix.shape + (self.tokens[:10],))
        if self.topics is not None:
            res += "labeled, first 10: %s\n" % self.topics[:10]
        else:
            res += "unlabeled\n"
        nnz = self.matrix.getnnz()
        res += "non-zero elements: %d  (%f)" % (
            nnz, nnz / (self.matrix.shape[0] * self.matrix.shape[1]))
        return res

    def _generate_tree(self):
        return {"tokens": merge_strings(self.tokens),
                "topics": merge_strings(self.topics) if self.topics is not None else False,
                "matrix": disassemble_sparse_matrix(self.matrix)}

    def __len__(self):
        """
        Returns the number of topics.
        """
        return self.matrix.shape[0]

    def __getitem__(self, item):
        """
        Returns the keywords sorted by significance from topic index.
        """
        row = self.matrix[item]
        nnz = row.nonzero()[1]
        pairs = [(-row[0, i], i) for i in nnz]
        pairs.sort()
        return [(self.tokens[pair[1]], -pair[0]) for pair in pairs]

    def label_topics(self, labels):
        if len(labels) != len(self):
            raise ValueError("Sizes do not match: %d != %d" % (len(labels), len(self)))
        if not isinstance(labels[0], str):
            raise TypeError("Labels must be strings")
        self._topics = list(labels)


"""
Goal: Using oil prices and oil production, try and do forcasting on the CPI value of each month

Model experimenting ideas
1. Naive model
2. Dense model with linear output
3. CNN model
4. RNN model
"""

# Importing libraries
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from Forecasting_Neural_Network import *
from sklearn.preprocessing import MinMaxScaler
tf.random.set_seed(42)

# Import the data from the csv files
CPI_data = pd.read_csv('Data/CPI_data_1997_to_2022.csv')
Energy_Prices = pd.read_csv('Data/Energy_Prices.csv')
Oil_Production = pd.read_csv('Data/International_PetroleumProduction_Consumption_and_Inventories.csv')

# Merge the dataframe into a single dataframe
All_data = CPI_data.merge(Energy_Prices, on='DATE').merge(Oil_Production, on='DATE')

# Turn the date from YY-MM-DD into 1, 2, 3... consecutive months
month_seq = np.arange(1, len(All_data) + 1)
All_data['DATE'] = month_seq

# Calculating the metrics for naive model
naive_predict = naives(All_data['VALUE'].dropna())
naive_metric = metrics(All_data.loc[1:, 'VALUE'], naives(All_data['VALUE']).dropna())
naive_metric

# Plot the auto-correlation of CPI
acf_plot(All_data['VALUE'])

# Min-Max scale all data
Scaler = MinMaxScaler()
scaled_sliding_window_data = pd.DataFrame(Scaler.fit_transform(All_data.iloc[:, 1:]))

# Lag the data by one day
scaled_sliding_window_data = scaled_sliding_window_data.shift(1).dropna()

# 12 Months sliding window data
train, label = sliding_window(scaled_sliding_window_data.iloc[:, 1:].values,
                              scaled_sliding_window_data.iloc[:, 0].values,
                              window=12,
                              horizon=1)

# Split data int 80/20 train test configuration
x_train, val_data, y_label, val_label = train_test_split(train,
                                                         label,
                                                         shuffle=False,
                                                         test_size=0.2)

"""# Creating a tensorboard callback
log_dir = 'logs/fit' + datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
tb_callback = TensorBoard(log_dir=log_dir, histogram_freq=1)
"""
# Creating model checkpoint
checkpoint = keras.callbacks.ModelCheckpoint('checkpoint/model',
                                             monitor='val_loss',
                                             mode='auto')

# Use the keras callback for to reduce learning rate
reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
                                              patience=20,
                                              factor=0.5,
                                              min_lr=1e-6)

# Keras call back for stopping early
stop_early = keras.callbacks.EarlyStopping(monitor='val_loss',
                                           patience=20)

# Creating a dense fully connected that has a linear output
dense_model = dense_linear()

dense_model.compile(loss='mae',
                    optimizer='adam',
                    metrics='mae')

hist_dense = dense_model.fit(x_train,
                             y_label,
                             epochs=200,
                             validation_data=(val_data, val_label),
                             callbacks=[reduce_lr, stop_early])

# Plot the dense model
plot_hist(hist_dense)

# Find the mse, mae, rmse of the dense model
dense_metrics = metrics(val_label, dense_model.predict(val_data))

dense_pred = rescale_data(val_data, Scaler, dense_model)

# Plot the prediction on validation data vs validation label
plot_pred(rescale_data(val_label, Scaler), dense_pred)

"""# Creating a Convoluted Neural Network
cnn_model = CNN_model()
cnn_model.compile(loss='mae',
                  optimizer='adam',
                  metrics='mae',)"""

# Loading the best model after training
cnn_model = keras.models.load_model('Linear_Models/cnn_model.h5')

hist_cnn = cnn_model.fit(x_train,
                         y_label,
                         epochs=500,
                         validation_data=(val_data, val_label),
                         callbacks=[reduce_lr, stop_early])

#cnn_model.save('cnn_model.h5')

plot_hist(cnn_model)

# Finding the mse, mae, rmse of the CNN model
cnn_metrics = metrics(val_label, cnn_model.predict(val_data))

cnn_pred = rescale_data(val_data, Scaler, cnn_model)

# Plot the prediction from the CNN model vs the validation label
plot_pred(rescale_data(val_label, Scaler), cnn_pred)

# Creating a Bi-Directional LSTM model
"""lstm = lstm_model()
lstm.compile(loss='mae',
             optimizer='adam',
             metrics='mae')
"""

# Load the best trained model
lstm = keras.models.load_model('Linear_Models/Bi_LSTM.h5')

hist_lstm = lstm.fit(x_train,
                     y_label,
                     epochs=300,
                     validation_data=(val_data, val_label),
                     callbacks=[reduce_lr, stop_early])

#lstm.save('Bi_LSTM.h5')

plot_hist(hist_lstm)

# Find the mae, mse, rmse of the LSTM model
lstm_metrics = metrics(val_label, lstm.predict(val_data))

lstm_pred = rescale_data(val_data, Scaler, lstm)

# Plot the prediction from LSTM model vs the validation label
plot_pred(rescale_data(val_label, Scaler), lstm_pred)

# Plot the prediction from all 4 models against validation label
plt.figure(figsize=(10, 7))
month_range = np.arange(245, 304)
plt.ion()
plt.plot(month_range, np.array(rescale_data(val_label, Scaler)))
plt.plot(naive_predict[245:304])
plt.plot(month_range, dense_pred)
plt.plot(month_range, cnn_pred)
plt.plot(month_range, lstm_pred)
plt.xlabel(f'{len(dense_pred)} Months from April 2022')
plt.ylabel('Predicted CPI')
plt.legend(['Validation Label', 'Naive Model', 'Dense Linear Model', 'CNN Model', 'Bi-Directional LSTM'])
plt.show()

# Plot the error from each models
naive_error = [naive_metric[error] for error in naive_metric]
dense_error = [dense_metrics[error] for error in dense_metrics]
cnn_error = [cnn_metrics[error] for error in cnn_metrics]
lstm_error = [lstm_metrics[error] for error in lstm_metrics]

plt.bar(np.arange(len(naive_error)), naive_error, label='Naive', width=0.2)
plt.bar(np.arange(len(naive_error)) + 0.2, dense_error, label='Dense Model', width=0.2)
plt.bar(np.arange(len(naive_error)) + 0.4, cnn_error, label='CNN Model', width=0.2)
plt.bar(np.arange(len(naive_error)) + 0.6, lstm_error, label='Bi-Directional LSTM', width=0.2)
plt.xticks([0.3, 1.3, 2.3], ['MAE', 'MSE', 'RMSE'])
plt.legend()


from os.path import realpath

def main():
    inp = open(f'{realpath(__file__)[:-2]}txt').read()

    areas = []

    for area in inp.split('\n')[1:]:
        lx, ly, ux, uy = map(int, area.split(','))
        # List of tiles in a given area
        areas.append(tuple((x,y) for x in range(lx, ux) for y in range(ly, uy)))

    l = len(areas)

    tiles = set()

    for i in range(l):
        for j in range(i + 1, l):
            # If a common tiles in 2 area then add to set
            if any(x in areas[j] for x in areas[i]):
                tiles = tiles.union(areas[i]).union(areas[j])

    print(len(tiles))

if __name__ == '__main__':
    main()


import os
import pandas as pd
import pyodbc

# Getting the current working directory
directory_in_str = os.getcwd()
for file in os.listdir(directory_in_str+r"\\chandu"):
     filename = os.fsdecode(file)
     #Looping in inner directory
     innerDirectory = directory_in_str + r"\\chandu\\"+filename+r"\\"
     for file in os.listdir(innerDirectory):
        filename = os.fsdecode(file)
        if filename.endswith(".txt"):
            # Getting the filename
            filename = os.path.join(innerDirectory, filename)
            df = pd.read_csv(filename,sep=" ", header = None)
            df.columns = ["First Name","Last Name","Gender","Course","Department","Section"]
            i = 0
            while i < len(df):
                # SQL Query to Insert Data
                query = "insert into data_student values ('%s','%s','%s','%s','%s','%s')" % (df.iloc[i]['First Name'], df.iloc[i]['Last Name'], df.iloc[i]['Gender'], df.iloc[i]['Course'], df.iloc[i]['Department'], df.iloc[i]['Section'])
                # SQL Connection String
                conn = pyodbc.connect('DRIVER={SQL Server};SERVER=COMPUTER\SQLEXPRESS;DATABASE=default; Trusted_Connection=yes;')
                cursor = conn.cursor()
                cursor.execute(query)
                conn.commit()
                i+=1
                conn.close()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations

def election_to_election_model(apps, schema_editor):
    Election = apps.get_model('elections', 'election')
    Document = apps.get_model('official_documents', 'officialdocument')
    for doc in Document.objects.all():
        doc.election_model = Election.objects.get(slug=doc.election)
        doc.save()

def election_model_to_election(apps, schema_editor):
    Document = apps.get_model('official_documents', 'officialdocument')
    for doc in Document.objects.all():
        doc.election = doc.election_model.id
        doc.save()

class Migration(migrations.Migration):

    dependencies = [
        ('official_documents', '0012_officialdocument_election_model'),
    ]

    operations = [
        migrations.RunPython(
            election_to_election_model,
            election_model_to_election,
        ),
    ]


n = int(input())
arr = [0]
arr = arr + list(map(int, input().split(' ')))

def getCounts(arr):
    last = {}
    ans = 0.0
    prev = 0.0
    res = 0.0
    for i in range(1, len(arr)):
        if arr[i] not in last:
            ans = prev + i
        else:
            ans = prev + i - last[arr[i]]
        prev = ans
        res += ans
        last[arr[i]] = i
    return res

ans = (2 * getCounts(arr) - n)/(n*n)
print("%.6f" % ans)


from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk

# frame initiation
root = Tk()
root.geometry("1920x1000")
root.propagate(False)

# frame style
style = ttk.Style()
style.configure("ok.TButton", font=("Calibri",20,"bold"), justify='center')
style.configure("desc.TLabel", font=("Calibri",16))

# load the image

# for main page
image_data = ['Image\\image1.png']
image = []
for path in image_data:
    image1 = Image.open(path)
    image1 = image1.resize((1000,600))
    image1 = ImageTk.PhotoImage(image1)
    image.append(image1)

# for stories
image_data = ['Image\\image2.png',
    'Image\\image3a.png','Image\\image3b.png',
    'Image\\image4a.png','Image\\image4b.png','Image\\image4c.png','Image\\image4d.png',
    'Image\\image5a.png','Image\\image5b.png','Image\\image5c.png','Image\\image5d.png',
    'Image\\image5e.png','Image\\image5f.png','Image\\image5g.png','Image\\image5h.png']
for path in image_data:
    image1 = Image.open(path)
    image1 = image1.resize((900,550))
    image1 = ImageTk.PhotoImage(image1)
    image.append(image1)

# Define the main window for each page
page1 = ttk.Frame(root)
page2 = ttk.Frame(root)

page3 =[]
for data in range(2):
     page3.append(ttk.Frame(root))

page4 = []
for data in range(4):
    page4.append(ttk.Frame(root))

page5 = []
for data in range(8):
    page5.append(ttk.Frame(root))

# Create a function to easily use tkraise method
def raise_frame(frame):
    frame.tkraise()

def next_step():
    if mandatory_entry.get():
        # the user entered data in the mandatory entry: proceed to next step
        raise_frame(page2)
    else:
        # the mandatory field is empty
        mandatory_entry.focus_set()

#------PAGE 1---------------------

# Create the widgets
mandatory_entry = ttk.Entry(page1, width=30, font=('Calibri',16))
page1image = ttk.Label(page1, text='Image Page 1', image=image[0])
page1_entrylabel = ttk.Label(page1, text='Input Name', style="desc.TLabel")
page1button = ttk.Button(page1, text='Start', style="ok.TButton", command=next_step)

# Place the widgets
page1['padding'] = (250,50)

page1.grid(column=0, row=0)
page1image.grid(column=0,row=1, columnspan=3, pady=(0,25))
page1_entrylabel.grid(column=0, row = 2)
mandatory_entry.grid(column=1, row = 2)
page1button.grid(column=2, row=2)

def create_page_story(root_frame, title_text, image, button_a_label, button_b_label, page_back_act, page_a_act, page_b_act, lastpage = False):
    # Create the widgets
    frame_a = ttk.Frame(root_frame, width=1920,height=200)
    page_label = ttk.Label(frame_a, text=title_text, style="desc.TLabel")

    frame_b = ttk.Frame(root_frame, width=1920, height=800)
    page_image = ttk.Label(frame_b, text='Image Page', image=image)
    page_button1 = ttk.Button(frame_b, text='Back', style="ok.TButton", command=lambda:raise_frame(page_back_act))

    if not lastpage:
        page_button2 = ttk.Button(frame_b, text=button_a_label, style="ok.TButton", command=lambda:raise_frame(page_a_act))
        page_button3 = ttk.Button(frame_b, text=button_b_label, style="ok.TButton", command=lambda:raise_frame(page_b_act))
    else:
        page_button2 = ttk.Button(frame_b, text="Restart", style="ok.TButton", command=lambda:raise_frame(page1))

    # Place the widgets
    root_frame['padding'] = (250,0)
    frame_a['padding'] = (0,0,0,10)

    root_frame.grid(column=0, row=0)
    frame_a.grid(column=0, row=0)
    frame_b.grid(column=0, row=1)

    page_label.grid(column=0, row=0)

    if not lastpage:
        page_image.grid(column=0, row=2, columnspan=3, pady=(0,10))
        page_button1.grid(column=0, row=3)
        page_button2.grid(column=1, row=3)
        page_button3.grid(column=2, row=3)
    else:
        page_image.grid(column=0, row=2, columnspan=2, pady=(0,40))
        page_button1.grid(column=0, row=3)
        page_button2.grid(column=1, row=3)

    return root_frame

title_text = '''Boby is a 23-year-old male. He is from Canada and his tour group has fled away. You are now
in charge of bossing him around the city. Remember, you're not touring him; you're bossing him
because, apparently, he doesn't have a choice in this game, unlike you.'''

# page2
page2 = create_page_story(page2, title_text, image[1], "Bring him to\nSanta Monica Pier", "Bring him to\nThe Griffith Observatory", page1, page3[0], page3[1])

# # page3
title_text='''
You've decided to take Bop to the Santa Monica Pier. He is exhausted and sweating profusely
as a result of the hot weather. He needs food.'''
create_page_story(page3[0], title_text, image[2], "Ask him to\nFish", "5-star\nrestaurant", page2, page4[0], page4[1])

title_text='''
You've decided to take Bop to Griffith Observatory. You arrived a little early, so sunset will be
just around the corner by the time his free parking runs out.'''
create_page_story(page3[1], title_text, image[3], "Wait for\nthe sunset", "We should\ngo!", page2, page4[2], page4[3])

# page4
title_text='''
Oh no! His fishing rod was bitten and dragged away by a shark. Bop has entered the waters.
He meets a cute shark. What should he do now?'''
create_page_story(page4[0], title_text, image[4], "Pet the\nShark", "Fight the\nShark", page3[0], page5[0], page5[1])

title_text='''
The restaurant charged Bop $740 for his wagyu steak. He can’t afford to pay.
What should Bop do?'''
create_page_story(page4[1], title_text, image[5], "Run\nAway", "Sell all\nBop's assets", page3[0], page5[2], page5[3])

title_text='''
Poor Bop got his car towed away.
He is now carless, what should he do now?'''
create_page_story(page4[2], title_text, image[6], "Steal \na Car", "Run back\nhome", page3[1], page5[4], page5[5])

title_text='''
Bop sprained his ankle rushing to his car to avoid the parking fee. When he tripped, he fractured
his ribs. Remember, medical school taught you nothing, you didn’t graduate and you got broke. How to start?'''
create_page_story(page4[3], title_text, image[7], "Call\n911", "Use your\nKnowledge", page3[1], page5[6], page5[7])

# page5
title_text='''
The shark has made the conscious decision to interpret Bop's following movement as a hostile
one. The cuteness of his appearance belies his unfriendly nature. Poor Bop.'''
create_page_story(page5[0], title_text, image[8], "", "", page4[0], page4[0], page4[0], lastpage=True)

title_text='''
What was Bop thinking?! Oh well, you managed to kill him with your domineering ways. Pray
that Bop finally finds some peace.'''
create_page_story(page5[1], title_text, image[9], "", "", page4[0], page4[0], page4[0], lastpage=True)

title_text='''
Bop might as well be the steak now.
Rest in peace Bop.'''
create_page_story(page5[2], title_text, image[10], "", "", page4[1], page4[0], page4[0], lastpage=True)

title_text='''
Terrible! Bop sold his one and only valuable item: the ring he stole from the pawn shop.
This might as well be his karma.'''
create_page_story(page5[3], title_text, image[11], "", "", page4[1], page4[0], page4[0], lastpage=True)

title_text='''
Sadly, Bop attempted to rob an undercover cop's car.
What an adventure, Bop!'''
create_page_story(page5[4], title_text, image[12], "", "", page4[2], page4[0], page4[0], lastpage=True)

title_text='''
Bop encountered a cardiac arrest after walking 20km back home.
What a tragic and unfortunate death.'''
create_page_story(page5[5], title_text, image[13], "", "", page4[2], page4[0], page4[0], lastpage=True)

title_text='''
They didn’t get there on time.
Internal bleeding got Bop dead.'''
create_page_story(page5[6], title_text, image[14], "", "", page4[3], page4[0], page4[0], lastpage=True)

title_text='''
Yucks! You accidentally poked his heart
during the surgery.'''
create_page_story(page5[7], title_text, image[15], "", "", page4[3], page4[0], page4[0], lastpage=True)

#---RAISING PAGE 1 TO START PROGRAM--------
raise_frame(page1)
root.mainloop()


def main():
    n = int(input("inserire un numero intero dispari---> "))
    while n%2!=1:
        n= int(input("inserire un numero intero dispari capra! ---> "))
    table=cuboMagico(n)

def cuboMagico(n):
    # crea matrice
    table = []
    for i in range(n):
        row = [0]*n
        table.append(row)
    # inserisce i valori
    riga = n-1
    colonna = n//2
    for k in range(1, n*n+1):
        table[riga][colonna] = k
        riga += 1
        colonna += 1
        riga1 = riga
        colonna1 = colonna
        if riga == n: riga = 0
        if colonna == n: colonna = 0
        if table[riga][colonna]!=0:
            riga = riga1
            colonna = colonna1
            riga -= 1

    print(table)

tabelle crea cubo magico con i lati desiderati.py


# Given two strings s and t, return true if t is an anagram of s, and false otherwise.
# An Anagram is a word or phrase formed by rearranging the letters of a different
# word or phrase, typically using all the original letters exactly once.

class Solution(object):
  def validAnagram(self,s,t ) -> bool:
    hashmap = {}
    for x in s:
      if x in hashmap:
        hashmap[x] += 1
      else:
        hashmap[x] = 1

    for y in range(len(t)):
      if y in hashmap:
        hashmap[y] += 1
      else:
        return False

    return True


import uuid

from django import http
from django.core.urlresolvers import reverse
from django.utils.datastructures import SortedDict

from mox import IsA

from openstack_dashboard import api
from openstack_dashboard.test import helpers as test

class InstanceViewTest(test.BaseAdminViewTests):
    @test.create_stubs({api.nova: ('flavor_list', 'server_list',),
                        api.keystone: ('tenant_list',)})
    def test_index(self):
        servers = self.servers.list()
        flavors = self.flavors.list()
        tenants = self.tenants.list()
        api.keystone.tenant_list(IsA(http.HttpRequest), admin=True).\
                                 AndReturn(tenants)
        api.nova.server_list(IsA(http.HttpRequest),
                             all_tenants=True).AndReturn(servers)
        api.nova.flavor_list(IsA(http.HttpRequest)).AndReturn(flavors)
        self.mox.ReplayAll()

        res = self.client.get(reverse('horizon:admin:instances:index'))
        self.assertTemplateUsed(res, 'admin/instances/index.html')
        instances = res.context['table'].data
        self.assertItemsEqual(instances, servers)

    @test.create_stubs({api.nova: ('flavor_list', 'flavor_get',
                                    'server_list',),
                        api.keystone: ('tenant_list',)})
    def test_index_flavor_list_exception(self):
        servers = self.servers.list()
        tenants = self.tenants.list()
        flavors = self.flavors.list()
        full_flavors = SortedDict([(f.id, f) for f in flavors])

        api.nova.server_list(IsA(http.HttpRequest),
                             all_tenants=True).AndReturn(servers)
        api.nova.flavor_list(IsA(http.HttpRequest)). \
                            AndRaise(self.exceptions.nova)
        api.keystone.tenant_list(IsA(http.HttpRequest), admin=True).\
                                 AndReturn(tenants)
        for server in servers:
            api.nova.flavor_get(IsA(http.HttpRequest), server.flavor["id"]). \
                                AndReturn(full_flavors[server.flavor["id"]])

        self.mox.ReplayAll()

        res = self.client.get(reverse('horizon:admin:instances:index'))
        self.assertTemplateUsed(res, 'admin/instances/index.html')
        instances = res.context['table'].data
        self.assertItemsEqual(instances, servers)

    @test.create_stubs({api.nova: ('flavor_list', 'flavor_get',
                                    'server_list',),
                        api.keystone: ('tenant_list',)})
    def test_index_flavor_get_exception(self):
        servers = self.servers.list()
        flavors = self.flavors.list()
        tenants = self.tenants.list()
        # UUIDs generated using indexes are unlikely to match
        # any of existing flavor ids and are guaranteed to be deterministic.
        for i, server in enumerate(servers):
            server.flavor['id'] = str(uuid.UUID(int=i))

        api.nova.server_list(IsA(http.HttpRequest),
                             all_tenants=True).AndReturn(servers)
        api.nova.flavor_list(IsA(http.HttpRequest)). \
                            AndReturn(flavors)
        api.keystone.tenant_list(IsA(http.HttpRequest), admin=True).\
                                 AndReturn(tenants)
        for server in servers:
            api.nova.flavor_get(IsA(http.HttpRequest), server.flavor["id"]). \
                                AndRaise(self.exceptions.nova)
        self.mox.ReplayAll()

        res = self.client.get(reverse('horizon:admin:instances:index'))
        instances = res.context['table'].data
        self.assertTemplateUsed(res, 'admin/instances/index.html')
        self.assertMessageCount(res, error=len(servers))
        self.assertItemsEqual(instances, servers)

    @test.create_stubs({api.nova: ('server_list',)})
    def test_index_server_list_exception(self):
        api.nova.server_list(IsA(http.HttpRequest),
                             all_tenants=True).AndRaise(self.exceptions.nova)

        self.mox.ReplayAll()

        res = self.client.get(reverse('horizon:admin:instances:index'))
        self.assertTemplateUsed(res, 'admin/instances/index.html')
        self.assertEqual(len(res.context['instances_table'].data), 0)

    @test.create_stubs({api.nova: ('server_get', 'flavor_get',),
                        api.keystone: ('tenant_get',)})
    def test_ajax_loading_instances(self):
        server = self.servers.first()
        flavor = self.flavors.list()[0]
        tenant = self.tenants.list()[0]

        api.nova.server_get(IsA(http.HttpRequest), server.id).AndReturn(server)
        api.nova.flavor_get(IsA(http.HttpRequest),
                            server.flavor['id']).AndReturn(flavor)
        api.keystone.tenant_get(IsA(http.HttpRequest),
                                server.tenant_id,
                                admin=True).AndReturn(tenant)
        self.mox.ReplayAll()

        url = reverse('horizon:admin:instances:index') + \
                "?action=row_update&table=instances&obj_id=" + server.id

        res = self.client.get(url, {},
                               HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertTemplateUsed(res, "horizon/common/_data_table_row.html")

        self.assertContains(res, "test_tenant", 1, 200)
        self.assertContains(res, "instance-host", 1, 200)
        self.assertContains(res, "server_1", 1, 200)
        self.assertContains(res, "10.0.0.1", 1, 200)
        self.assertContains(res, "512MB RAM | 1 VCPU | 0 Disk", 1, 200)
        self.assertContains(res, "Active", 1, 200)
        self.assertContains(res, "Running", 1, 200)

    @test.create_stubs({api.nova: ('flavor_list', 'server_list',),
                        api.keystone: ('tenant_list',)})
    def test_index_options_before_migrate(self):
        api.keystone.tenant_list(IsA(http.HttpRequest), admin=True).\
                                 AndReturn(self.tenants.list())
        api.nova.server_list(IsA(http.HttpRequest),
                             all_tenants=True).AndReturn(self.servers.list())
        api.nova.flavor_list(IsA(http.HttpRequest)).\
                             AndReturn(self.flavors.list())
        self.mox.ReplayAll()

        res = self.client.get(reverse('horizon:admin:instances:index'))
        self.assertContains(res, "instances__migrate")
        self.assertNotContains(res, "instances__confirm")
        self.assertNotContains(res, "instances__revert")

    @test.create_stubs({api.nova: ('flavor_list', 'server_list',),
                        api.keystone: ('tenant_list',)})
    def test_index_options_after_migrate(self):
        server = self.servers.first()
        server.status = "VERIFY_RESIZE"
        api.keystone.tenant_list(IsA(http.HttpRequest), admin=True).\
                                 AndReturn(self.tenants.list())
        api.nova.server_list(IsA(http.HttpRequest),
                             all_tenants=True).AndReturn(self.servers.list())
        api.nova.flavor_list(IsA(http.HttpRequest)).\
                             AndReturn(self.flavors.list())
        self.mox.ReplayAll()

        res = self.client.get(reverse('horizon:admin:instances:index'))
        self.assertContains(res, "instances__confirm")
        self.assertContains(res, "instances__revert")
        self.assertNotContains(res, "instances__migrate")


import requests
import hashlib
import difflib

from panopuppet.pano.puppetdb.puppetdb import api_get as pdb_api_get, get_server, mk_puppetdb_query

__author__ = 'takeshi'

requests.packages.urllib3.disable_warnings()

def get_hash(data):
    m = hashlib.md5()
    m.update(data.encode('utf-8'))
    return m.hexdigest()

def get_file(request, certname, environment, rtitle, rtype, md5sum_from=None, md5sum_to=None, diff=False,
             file_status='from'):
    puppetdb_source, puppetdb_certs, puppetdb_verify = get_server(request=request)
    filebucket_source, filebucket_certs, filebucket_verify, filebucket_show = get_server(request=request,
                                                                                type='filebucket')
    fileserver_source, fileserver_certs, fileserver_verify, fileserver_show = get_server(request=request,
                                                                                type='fileserver')
    # If Clientbucket is enabled continue else return False

    def fetch_filebucket(url, method):
        headers = {
            'Accept': 's',
        }
        methods = {'get': requests.get,
                   'head': requests.head,
                   }
        if method not in methods:
            print('No can has method: %s' % method)
            return False
        resp = methods[method](url,
                               headers=headers,
                               verify=filebucket_verify,
                               cert=filebucket_certs)
        if resp.status_code != 200:
            return False
        else:
            return resp.text

    def fetch_fileserver(url, method):
        methods = {'get': requests.get,
                   }

        if method not in methods:
            print('No can has method: %s' % method)
            return False
        resp = methods[method](url,
                               verify=fileserver_verify,
                               cert=fileserver_certs)
        if resp.status_code != 200:
            return False
        else:
            return resp.text

    def get_resource(certname, rtype, rtitle):
        resource_params = {
            'query':
                {
                    'operator': 'and',
                    1: '["=", "certname", "' + certname + '"]',
                    2: '["=", "type", "' + rtype + '"]',
                    3: '["=", "title", "' + rtitle + '"]'

                },
        }
        data = pdb_api_get(
            api_url=puppetdb_source,
            path='resources',
            verify=puppetdb_verify,
            cert=puppetdb_certs,
            params=mk_puppetdb_query(resource_params, request))
        if not data:
            return False
        else:
            return data

    if not filebucket_show or not fileserver_source:
        return False
    if file_status == 'both':
        if md5sum_to and md5sum_from and certname and rtitle and rtype:
            if diff:
                # is the hash from puppetdb resource same as md5sum_to
                hash_matches = False

                md5sum_from = md5sum_from.replace('{md5}', '')
                md5sum_to = md5sum_to.replace('{md5}', '')

                from_url = filebucket_source + environment + '/file_bucket_file/md5/' + md5sum_from
                to_url = filebucket_source + environment + '/file_bucket_file/md5/' + md5sum_to

                if fetch_filebucket(from_url, 'head') is not False:
                    resource_from = fetch_filebucket(from_url, 'get')
                else:
                    # Could not find old MD5 in Filebucket
                    return False
                if fetch_filebucket(to_url, 'head') is not False:
                    resource_to = fetch_filebucket(to_url, 'get')
                # Try puppetdb resources if not found in filebucket.
                else:
                    resource_to = get_resource(certname=certname, rtype=rtype, rtitle=rtitle)
                    if resource_to is False:
                        # Could not find new file in Filebucket or as a PuppetDB Resource
                        return False
                    else:
                        resource_to = resource_to[0]
                    if 'content' in resource_to['parameters']:
                        resource_to = resource_to['parameters']['content']
                        hash_of_resource = get_hash(resource_to)
                        if hash_of_resource == md5sum_to:
                            # file from resource matches filebucket md5 hash
                            hash_matches = True
                    # Solve the viewing of source files by retrieving it from Puppetmaster
                    elif 'source' in resource_to['parameters'] and fileserver_show is True:
                        source_path = resource_to['parameters']['source']
                        if source_path.startswith('puppet://'):
                            # extract the path for the file
                            source_path = source_path.split('/')  # ['puppet:', '', '', 'files', 'autofs', 'auto.home']
                            source_path = '/'.join(source_path[3:])  # Skip first 3 entries since they are not needed
                            # https://puppetmaster.example.com:8140/production/file_content/files/autofs/auto.home
                            url = fileserver_source + environment + '/file_content/' + source_path
                            resource_to = fetch_fileserver(url, 'get')
                    else:
                        return False
                # now that we have come this far, we have both files.
                # Lets differentiate the shit out of these files.
                from_split_lines = resource_from.split('\n')
                to_split_lines = resource_to.split('\n')
                diff = difflib.unified_diff(from_split_lines, to_split_lines)
                diff = ('\n'.join(list(diff))).split('\n')
                return diff
            else:
                return False
        else:
            return False

    if file_status == 'from' and md5sum_from:
        md5sum = md5sum_from.replace('{md5}', '')
    elif file_status == 'to' and md5sum_to:
        md5sum = md5sum_to.replace('{md5}', '')
    else:
        return False
    # Creates headers and url from the data we got

    url_clientbucket = filebucket_source + environment + '/file_bucket_file/md5/' + md5sum
    if fetch_filebucket(url_clientbucket, 'head') is False:
        # Check if theres a resource available for the latest file available
        if file_status == 'to':
            resp_pdb = get_resource(certname=certname, rtype=rtype, rtitle=rtitle)
            # we got the data lets give the user the good news.
            if resp_pdb:
                resource_data = resp_pdb[0]
                if 'content' in resource_data['parameters']:
                    prepend_text = 'This file with MD5 %s was found in PuppetDB Resources.\n\n' % (
                        get_hash(resource_data['parameters']['content']))
                    return prepend_text + resource_data['parameters']['content']
                elif 'source' in resource_data['parameters'] and fileserver_show is True:
                    source_path = resource_data['parameters']['source']
                    if source_path.startswith('puppet://'):
                        # extract the path for the file
                        source_path = source_path.split('/')  # ['puppet:', '', '', 'files', 'autofs', 'auto.home']
                        source_path = '/'.join(source_path[3:])  # Skip first 3 entries since they are not needed
                        # https://puppetmaster.example.com:8140/production/file_content/files/autofs/auto.home
                        url = fileserver_source + environment + '/file_content/' + source_path
                        source_content = fetch_fileserver(url, 'get')
                        prepend_text = 'This file with MD5 %s was retrieved from the PuppetMaster Fileserver.\n\n' % (
                            get_hash(source_content))
                        return prepend_text + source_content
                    else:
                        return False
                else:
                    return False
            # the file can't be found as a resource and or fileserver support not enabled
            else:
                return False
        # We probably don't want to search for resources if its the old file.
        else:
            return False
    else:
        filebucket_results = fetch_filebucket(url_clientbucket, 'get')
        prepend_text = 'This file with MD5 %s was found in Filebucket.\n\n' % (md5sum)
        return prepend_text + filebucket_results


from .powerdict import PowerDict
from .league_year import LeagueYear

def create_league(name, desc='', website=''):
    return League(name, desc, website)

class League(PowerDict):

    def __init__(self, name='', desc='', website=''):
        league = {}
        league['name'] = name
        league['desc'] = desc
        league['website'] = website
        league['teams'] = None
        league['years'] = {}
        self._data = league

    def add_year(self, year):
        self.years[year] = LeagueYear(year)
        return self.years[year]


from hammingdistance import hamming_distance

with open('input.txt', 'r') as fh:
    k = int(fh.readline().strip())
    patterns = fh.read().splitlines()

def all_kmers(k):
    if k == 1:
        return ['A', 'C', 'G', 'T']
    else:
        result = []
        root_kmers = all_kmers(k-1)
        for kmer in root_kmers:
            for nuc in ['A', 'C', 'G', 'T']:
                result.append(kmer + nuc)
        return result

def min_hamming_distance(pattern, sequence):
    distance = float('inf')
    k = len(pattern)
    for i in range(len(sequence)-k+1):
        other_pattern = sequence[i:i+k]
        temp_distance = hamming_distance(pattern, other_pattern)
        if temp_distance < distance:
            distance = temp_distance
    return distance

def d(pattern, sequences):
    result = 0
    for seq in sequences:
        result += min_hamming_distance(pattern, seq)
    return result

def median_string(k, sequences):
    distance = float('inf')
    for kmer in all_kmers(k):
        temp_distance = d(kmer, sequences)
        if temp_distance < distance:
            distance = temp_distance
            result = kmer
    return result

print(median_string(k, patterns))


#%%
from numpy import testing
from numpy.lib.function_base import median
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import lightgbm as lgb
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# %%
data = pd.read_csv("hydropower_time_series.csv")

# Identify transition points

data['medianpow'] = data['power'].rolling(window=20,center=True).median()
data['mapow'] = data['medianpow'].rolling(window=20,center=True).mean()
#%%

next5= data['mapow'].iloc[5:]
next5.reset_index(drop=True, inplace=True)
prev5= data['mapow'].iloc[:-5]
prev5.reset_index(drop=True, inplace=True)

data.reset_index(drop=True, inplace=True)
data['deriv'] = np.abs(next5-prev5)

# %%
data['transition'] = np.where(data['deriv']>0.15,30,0)

# %%
# ax = data.plot(figsize=(500,10))
# fig = ax.get_figure()
# fig.savefig('task1_tran_id.png')
# %%

# %%

#Extract data with sound value

rawdata = data[data['octave_62_5']>0]
# %%
clist = list(rawdata.columns)
fclist = clist[2:7]
fclist.append('transition')
fclist
#%%
rawdata = rawdata[fclist]
# %%
rawdata['transition'] = rawdata['transition']/30
rawdata['transition'] = rawdata['transition'].astype(int)
# %%
rawdata.info()
# %%
#unbalance data, try to discard some data with trans. =0

tran_pos_data = rawdata.loc[rawdata.transition == 1]
tran_non_data = rawdata.loc[rawdata.transition == 0]

discard_data,used_tran_non_data = train_test_split(tran_non_data,test_size = 0.007)

#combined_data is final data set
combined_data = pd.concat([tran_pos_data,used_tran_non_data],ignore_index=True)

# %%
dftrain,dftest = train_test_split(combined_data,test_size=0.1)
# %%
lgb_train = lgb.Dataset(dftrain.drop(['transition'],axis = 1),label=dftrain['transition'])

lgb_valid = lgb.Dataset(dftest.drop(['transition'],axis = 1),label=dftest['transition'],reference=lgb_train)
# %%
params = {
    'boosting_type': 'gbdt',
    'objective':'binary',
    'metric': 'auc',
    'num_leaves': 50,
    'learning_rate': 0.05,
    'feature_fraction': 0.9,
    'bagging_fraction': 0.8,
    'bagging_freq': 5,
    'verbose': 0,
    'max_depth':25

}

results = {}
gbm = lgb.train(params,
                lgb_train,
                num_boost_round= 50,
                valid_sets=(lgb_valid, lgb_train),
                valid_names=('validate','train'),
                early_stopping_rounds = 10,
                evals_result= results)

#%%

y_pred_train = gbm.predict(dftrain.drop('transition',axis = 1), num_iteration=gbm.best_iteration)
y_pred_test = gbm.predict(dftest.drop('transition',axis = 1), num_iteration=gbm.best_iteration)

#all y_pred >0.5 is predicted as 1
print('train accuracy: {:.5} '.format(accuracy_score(dftrain['transition'],y_pred_train>0.5)))
print('valid accuracy: {:.5} \n'.format(accuracy_score(dftest['transition'],y_pred_test>0.5)))

lgb.plot_metric(results)
lgb.plot_importance(gbm,importance_type = "gain")


import atexit
import datetime
import logging
import re
import signal
import threading
import time
from typing import Any, List, Optional, Sequence, Tuple

import lxml
import lxml.etree
import requests

from ngk import config, parser_xyz
from ngk.comments_processor import CommentsProcessor
from ngk.log import get_logger, redirect_basic_logging
from ngk.html_util import inner_html_ru, normalize_text
from ngk.parse_error import ParseError
from ngk.parser import ParsedComment, ParsedPost, ParsedUser
from ngk.parser_ru import parse_sink, parse_post
from ngk.schema import Comment, CommentIdStorage, ScopedSession, SyncState

L = get_logger('scan_comments', logging.INFO)
redirect_basic_logging(L)

COMMENTS_URL = 'http://govnokod.ru/comments'
COMMENTS_URL_XYZ = 'https://govnokod.xyz/comments/'
FAST_DELAY = 8
SLOW_DELAY = 60
FAST_TO_SLOW_STEPS = 20

exit_event = threading.Event()
threads_exited_events: List[threading.Event] = []

def fetch_latest_comments() -> List[ParsedComment]:
    L.debug("Fetching comments from ru...")
    r = requests.get(COMMENTS_URL, headers=config.DEFAULT_HEADERS, timeout=30)
    r.raise_for_status()
    return parse_sink(r.content)

def fetch_latest_comments_xyz() -> List[parser_xyz.CommentXyz]:
    L.debug("Fetching comments from xyz...")
    r = requests.get(COMMENTS_URL_XYZ, headers=config.DEFAULT_HEADERS, timeout=30)
    r.raise_for_status()
    root = lxml.etree.HTML(r.content)
    return parser_xyz.parse_comments(root)

def update_sync_states(comments: Sequence[ParsedComment], processor: CommentsProcessor) -> bool:
    has_updates = False
    updated_comments: List[Comment] = []
    with ScopedSession() as session:
        for comment_sink in comments:
            post_id, comment_id, comment_text = comment_sink.post_id, comment_sink.id_ru, comment_sink.text
            comment_db = session.query(Comment).filter(Comment.comment_id == comment_id).first()
            if comment_db is not None and comment_db.text != comment_text:
                comment_db.text = comment_text
                updated_comments.append(comment_db)

            state = session.query(SyncState).filter(SyncState.post_id == post_id).one_or_none()
            if not state:
                L.info("Got new comment %d for new post %d", comment_id, post_id)
                has_updates = True
                state = SyncState(post_id=post_id, last_comment_id=comment_id, pending=True, priority=SyncState.PRIORITY_HAS_COMMENTS)
                session.add(state)
            else:
                if state.last_comment_id is None or comment_id > state.last_comment_id:
                    L.info("Got new comment %d for post %d", comment_id, post_id)
                    has_updates = True
                    state.last_comment_id = comment_id
                    state.pending = True
                    state.priority=SyncState.PRIORITY_HAS_COMMENTS

        if len(updated_comments) > 0:
            L.info(f'Fast-fetched {len(updated_comments)} updated ' + \
                   f'comment{"s" if len(updated_comments) > 1 else ""}: {[c.comment_id for c in updated_comments]}')
            session.commit()
            processor.on_comments_update([], updated_comments)

    return has_updates

def update_xyz_states(comments: Sequence[parser_xyz.CommentXyz], processor: CommentsProcessor) -> None:
    with ScopedSession() as session:
        updated_comments = []
        prefetched_comment_id_pairs = []
        for comment in comments:
            if comment.id_xyz is not None and comment.id_ru is not None:
                comment_db = session.query(Comment).filter(Comment.comment_id == comment.id_ru).first()
                if comment_db is None or comment_db.comment_id_storage is None:
                    id_storage = session.query(CommentIdStorage).filter(CommentIdStorage.comment_id_ru == comment.id_ru).first()
                    if id_storage is None:
                        id_storage = CommentIdStorage()
                        id_storage.comment_id_ru = comment.id_ru
                        id_storage.comment_id_xyz = comment.id_xyz
                        session.add(id_storage)
                        prefetched_comment_id_pairs.append((comment.id_ru, comment.id_xyz))
                    elif id_storage.comment_id_xyz != comment.id_xyz:
                        id_storage.comment_id_xyz = comment.id_xyz
                        prefetched_comment_id_pairs.append((comment.id_ru, comment.id_xyz))

                    if comment_db is not None:
                        comment_db.comment_id_storage = id_storage
                        updated_comments.append(comment_db)

                elif comment_db.comment_id_storage.comment_id_xyz != comment.id_xyz:
                    comment_db.comment_id_storage.comment_id_xyz = comment.id_xyz
                    session.merge(comment_db.comment_id_storage)
                    updated_comments.append(comment_db)

        if len(prefetched_comment_id_pairs) > 0:
            session.flush()
            to_print: list = prefetched_comment_id_pairs[:5]
            if len(prefetched_comment_id_pairs) > 5:
                to_print.append('...')
            L.info(f'Prefetched xyz ids for {len(prefetched_comment_id_pairs)} comments: [{", ".join(map(str, to_print))}]')

        if len(updated_comments) > 0:
            session.commit()
            to_print = [(c.comment_id, c.comment_id_storage.comment_id_xyz) for c in updated_comments[:5]]
            if len(updated_comments) > 5:
                to_print.append('...')
            L.info(f'Fetched xyz ids for {len(updated_comments)} comments: [{", ".join(map(str, to_print))}]')
            processor.on_comments_update([], updated_comments)

def worker_ru(thread_exited_event: threading.Event) -> None:
    thread_exited_event.clear()
    processor = CommentsProcessor(config.REDIS_HOST,
                                  config.REDIS_PORT,
                                  config.REDIS_PASSWORD,
                                  config.REDIS_CHANNEL,
                                  L)
    L.info("=== ru worker started ===")
    fast_requests = 0
    while True:
        try:
            comments = fetch_latest_comments()
            has_updates = update_sync_states(comments, processor)
            if has_updates:
                fast_requests = FAST_TO_SLOW_STEPS
        except Exception as e:
            L.exception(e)
            fast_requests = 0

        if fast_requests > 0:
            delay = FAST_DELAY
            fast_requests -= 1
        else:
            delay = SLOW_DELAY

        if exit_event.wait(delay):
            break

    thread_exited_event.set()

def worker_xyz(thread_exited_event: threading.Event) -> None:
    thread_exited_event.clear()
    processor = CommentsProcessor(config.REDIS_HOST,
                                  config.REDIS_PORT,
                                  config.REDIS_PASSWORD,
                                  config.REDIS_CHANNEL,
                                  L)
    L.info("=== xyz worker started ===")
    fast_requests = 0
    last_xyz_id: Optional[int] = -1
    while True:
        try:
            comments = fetch_latest_comments_xyz()
            update_xyz_states(comments, processor)

            if comments[0].id_xyz != last_xyz_id:
                fast_requests = FAST_TO_SLOW_STEPS
                last_xyz_id = comments[0].id_xyz
        except Exception as e:
            L.exception(e)
            fast_requests = 0

        if fast_requests > 0:
            delay = FAST_DELAY
            fast_requests -= 1
        else:
            delay = SLOW_DELAY

        if exit_event.wait(delay):
            break

    thread_exited_event.set()

def graceful_exit(signum: int, frames: Any) -> None:
    L.info('Exiting...')
    exit_event.set()
    while not all((e.is_set() for e in threads_exited_events)):
        for e in threads_exited_events:
            e.wait()
    L.info('All threads stopped. Goodbye!')

def main() -> None:
    threads_exited_events.append(threading.Event())
    thread_ru = threading.Thread(target=worker_ru, name='RU', args=(threads_exited_events[-1],))

    threads_exited_events.append(threading.Event())
    thread_xyz = threading.Thread(target=worker_xyz, name='XYZ', args=(threads_exited_events[-1],))

    signal.signal(signal.SIGINT, graceful_exit)
    signal.signal(signal.SIGTERM, graceful_exit)

    thread_ru.start()
    thread_xyz.start()

    thread_ru.join()
    thread_xyz.join()

if __name__ == '__main__':
    main()


from rest_framework import serializers
from proyectos.models import *
from kanban.serializers import *

class SprintSerializer(serializers.ModelSerializer):

    class Meta:
        model = Sprint
        fields = '__all__'

class ProyectoSerializer(serializers.ModelSerializer):
    sprints = serializers.SerializerMethodField()

    def get_sprints(self, obj):
        sprints = Sprint.objects.filter(proyecto_id=obj.id)
        serializer = SprintSerializer(sprints, many=True)
        return serializer.data

    class Meta:
        model = Proyecto
        fields = '__all__'

class EquipoSerializer(serializers.ModelSerializer):
    usuario = AutorSerializer(many=False, read_only=True)

    class Meta:
        model = Equipo
        fields = ['usuario']


from math import radians, cos, sin, asin, sqrt, fabs
from app.models import *
from django.db import transaction

@transaction.commit_manually
def manual_transaction():
    dec_cords = {}
    avaluos = Avaluo.objects.all()
    def iround(x):
        """iround(number) -> integer
        Round a number to the nearest integer."""
        y = round(x) - .5
        return int(y) + (y > 0)
    for avaluo in avaluos:
        if avaluo.LatitudG and avaluo.LatitudM and avaluo.LatitudS and avaluo.LongitudG and avaluo.LongitudM and avaluo.LongitudS:
            latsign = 1
            if(avaluo.LatitudG < 0):
                latsign = -1

            lonsign = 1
            if(avaluo.LongitudG < 0):
                lonsign = -1

            declat = round((
            fabs(iround(float(avaluo.LatitudG)* 1000000.00)) +
            (fabs(iround(float(avaluo.LatitudM)* 1000000.00))/60) +
            (fabs(iround(float(avaluo.LatitudS)* 1000000.00))/3600)
            )*latsign/1000000,5)

            declon = round((
            fabs(iround(float(avaluo.LongitudG)* 1000000.00)) +
            (fabs(iround(float(avaluo.LongitudM)* 1000000.00))/60) +
            (fabs(iround(float(avaluo.LongitudS)* 1000000.00))/3600)
            )*lonsign/1000000,5)

            avaluo.Declat = declat
            avaluo.Declon = declon
            avaluo.save()
            #print avaluo.FolioK, avaluo.Declat, avaluo.Declon
            transaction.commit()
        else:
            avaluo.Declat = 0
            avaluo.Declat = 0
            avaluo.save()
            #print avaluo.FolioK, avaluo.Declat, avaluo.Declon
            transaction.commit()


with open("input.txt") as file:
    count_a = count_b = 0
    for line in file:
        a0, a1, b0, b1 = map(int, line.replace(",","-").split("-"))

        if (a0 <= b0 <= b1 <= a1) or (b0 <= a0 <= a1 <= b1):
            count_a += 1

        if not (a1 < b0 or b1 < a0):
            count_b += 1

print("a:", count_a)
print("b:", count_b)


import pandas as pd
import numpy as np
import plotly.express as px  # (version 4.7.0)
import plotly.graph_objects as go

import dash_bootstrap_components as dbc
import dash  # (version 1.12.0) pip install dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from plotly.subplots import make_subplots
from dash.exceptions import PreventUpdate
import datetime

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

# ---------- Import and clean data (importing csv into pandas)
# df = pd.read_csv("intro_bees.csv")
df0 = pd.read_csv("data_cleaned.csv", parse_dates = ["date"])
# print(list(df0.columns))

df = df0.groupby(['iso_code','location','population'])[['total_cases', 'total_deaths']].max()
df.reset_index(inplace=True)
df['cases_per_100000']= 100000*df['total_cases']/df['population']
df['deaths_per_million']=1000000*df['total_deaths']/df['population']
# print(df[df['location']=='United States'])

df_time = df0.groupby(['iso_code','location','population',
    pd.Grouper(key="date", freq="M")])[['daily_cases','daily_deaths','daily_cases_per_100000','daily_deaths_per_1millions']].sum()
df_time.reset_index(inplace = True)
df_time['month_year'] = df_time['date'].dt.strftime('%Y-%m')
df_time = df_time.rename(columns={'daily_cases': "cases", 'daily_deaths': 'deaths',
    'daily_cases_per_100000': 'cases_per_100000','daily_deaths_per_1millions':'deaths_per_million'})

# ------------------------------------------------------------------------------
# App layout
app.layout = html.Div([
    dbc.Row(dbc.Col(html.H1("Covid 19 impact"),
                    #    width={'size': 6, 'offset': 3},
                        style={'color': '#002699', 'text-align': 'center', 'font-size': '18px', 'font-family':'verdana'},
                        ),
                ),

    dbc.Row([
        dbc.Col(
            dcc.Dropdown(id = 'world_map',
                        options =[
                            {"label": "Cases", "value": 'cases'},
                            {"label": "Deaths", "value": 'deaths'},
                            {"label": "Cases per 100 000", "value": "cases_per_100000"},
                            {"label": "Deaths per million", "value": "deaths_per_million"},],
                        multi = False,
                        value = 'cases',
                        style = {'width': "100%"}
                            ),
            width = {'size': 2, "offset": 0, 'order': 1}
            ),
        dbc.Col(
            dcc.Dropdown(id = 'start_month',
                        options =[
                            {"label": month, "value": month} for month in np.sort(df_time['month_year'].unique())],
                        multi = False,
                        value = '2019-12',
                        style = {'width': "100%"}
                            ),
            width = {'size': 1, "offset": 1, 'order': 2}
            ),
        dbc.Col(
            dcc.Dropdown(id = 'end_month',
                        options =[
                            {"label": month, "value": month} for month in np.sort(df_time['month_year'].unique())],
                        multi = False,
                        value = '2020-10',
                        style = {'width': "100%"}
                            ),
            width = {'size': 1, "offset": 0, 'order': 3}
            ),

        dbc.Col(
            dcc.Dropdown(id = 'country',
                        options = [{"label": label, "value": label} for label in list(df['location'])],
                        multi = True,
                        value = ["France", "Italy"],
                        style = {'width': "100%"}
                            ),
            width = {'size': 3, "offset": 0, 'order': 4}
            ),
        dbc.Col(
            dcc.Dropdown(id = 'variable',
                        options = [{"label": "Total cases", "value": "total_cases"},
                                  {"label": "Total deaths", "value": "total_deaths"},
                                  {"label": "Daily cases", "value": "daily_cases"},
                                  {"label": "Daily deaths", "value": "daily_deaths"},
                                  {"label": "Stringency", "value": "stringency_index"},
                                  {"label": "Daily cases per 100000", "value": "daily_cases_per_100000"},
                                  {"label": "Daily deaths per million", "value": "daily_deaths_per_1millions"},],
                        multi = True,
                        value = ["total_cases", "total_deaths"],
                        style = {"width": "100%"}
                            ),
            width = {"size": 4, "offset": 0, 'order': 5}
            ),
        ]),

    dbc.Row([
        dbc.Col(
            html.Div(id='container_map', children = {}),
            width = {'size': 6, "offset": 0, 'order': 1}
            ),
        dbc.Col(
            html.Div(id='container_country', children = {}),
            width = {'size': 2, "offset": 0, 'order': 2}
            ),
        dbc.Col(
            html.Div(id='container_var', children = {}),
            width = {'size': 4, "offset": 0, 'order': 2}
            ),
        ]),

    html.Br(),

    dbc.Row([
        dbc.Col(html.Div([
            dcc.Graph(id='my_map', figure={}),
            dcc.Graph(id='world_chart', figure={})]
            ),
            width = {'size': 6, "offset": 0, 'order': 1}
                ),
        dbc.Col(html.Div(dcc.Graph(id='my_chart', figure={}, style = {'height' : '900px'})
            ),
            width = {'size': 6, "offset": 0, 'order': 2}
            ),
        ]),

    ])

# ------------------------------------------------------------------------------
# Connect the Plotly graphs with Dash Components
@app.callback(
    [Output(component_id = 'container_map', component_property='children'),
    Output(component_id = 'my_map', component_property='figure'),
    Output(component_id = 'world_chart', component_property='figure'),
    Output(component_id='container_country', component_property='children'),
    Output(component_id='container_var', component_property='children'),
    Output(component_id='my_chart', component_property='figure')],
    [Input(component_id='world_map', component_property='value'),
    Input(component_id='country', component_property='value'),
    Input(component_id = 'variable', component_property = 'value'),
    Input(component_id = 'start_month', component_property = 'value'),
    Input(component_id = 'end_month', component_property = 'value')
    ]
)

def update_graph(option_case, option_country, option_variable, start_time, end_time):
    if option_case is None or start_time is None or end_time is None:
        raise PreventUpdate
    else:
        container1 = "Impact in the world during a certain timeline."

        dff = df_time.copy()
        dff = dff[(dff['date']>= pd.to_datetime(start_time)) & (dff['date']< pd.to_datetime(end_time))]
        dff = dff.groupby(['iso_code','location','population'])[['cases', 'deaths', 'cases_per_100000', 'deaths_per_million']].sum()
        dff.reset_index(inplace=True)

        # World map.
        fig1 = px.choropleth(
            data_frame=dff,
            locations='iso_code',
            scope='world',
            color=option_case,
            hover_name='location',
            color_continuous_scale=px.colors.sequential.YlOrRd
        )
        fig1.update_layout(title=dict(font=dict(size=28),x=0.5,xanchor='center'),
                          margin=dict(l=10, r=10, t=10, b=0))

    #Chart to compare chosen countries on certain properties.
    if len(option_country)==0 or len(option_variable)==0:
        raise PreventUpdate
    elif len(option_country) == 4:
        option_country = option_country[:3]
    else:
        container2 = "Please choose less than 4 countries"
        container3 = "Please choose characters"

        dff = df0.copy()
        dff = dff[dff['location'].isin(list(option_country))]
        #date_axis = dff[dff["location"] == "France"]["date"]

        # Plotly chart
        col = len(option_variable)
        font = dict(size=24, family='verdana', color='#0052cc')
        fig2 = make_subplots(rows=col, cols=1, shared_xaxes=False,
            subplot_titles=['{}'.format(var.capitalize().replace('_', ' ')) for var in option_variable])
        colours = ["#730099", "#009933", "#0099e6" ]
        for i, var in enumerate(option_variable):
            show_legend = False
            if i == 0:
                show_legend = True
            for j, country in enumerate(option_country):
                fig2.add_trace(go.Scatter(x=dff[dff["location"]==country]["date"], y=dff[dff["location"]==country][var],
                    name = '{}'.format(country), showlegend = show_legend, line = dict(color = colours[j]),
                        ),
                    row=i+1, col=1
                    )
        for i in fig2.layout.annotations:
            i["font"] = font

                # fig2.update_xaxes(title_text = '{}'.format(var.capitalize().replace('_', ' ')),
                #     title_font=dict(size=24, family='verdana', color='#0052cc'),
                #      row=i+1, col=1)

        #fig2.update_layout(title=dict(font=dict(size=28),x=0.5,xanchor='center'),
         #                 margin=dict(l=10, r=10, t=10, b=10))

        fig2.update_layout(margin=dict(l=10, r=10, t=40, b=10))

        fig2.update_layout(legend ={'x': 0, 'y': 1} )

    dff = df0.copy()
    dff = dff.groupby(['date'])[['total_cases', 'total_deaths', "daily_cases", "daily_deaths"]].sum()
    dff.reset_index(inplace=True)

    fig3 = go.Figure()

    fig3.add_trace(go.Scatter(
        x = dff["date"], y = dff["total_cases"],
        name="Total cases", line_color = "#1f77b4"
    ))

    fig3.add_trace(go.Scatter(
        x = dff["date"], y = dff["total_deaths"], name = " Total deaths",
        yaxis="y2", line_color = "#4633FF"
    ))

    fig3.add_trace(go.Scatter(
        x = dff["date"], y = dff["daily_cases"], name = "Daily cases",
        yaxis="y3", line_color = "#FFA533"
    ))

    fig3.add_trace(go.Scatter(
        x = dff["date"], y = dff["daily_deaths"], name = "Daily deaths",
        yaxis="y4", line_color = "#820A2F"
    ))

    fig3.update_layout(
        xaxis=dict(
        domain=[0.05, 0.9]
            ),
        yaxis=dict(
            title="Total cases",
            titlefont=dict(
                color="#1f77b4"
            ),
            tickfont=dict(
                color="#1f77b4"
            )
        ),
        yaxis2=dict(
            title="Total deaths",
            titlefont=dict(
                color="#4633FF"
            ),
            tickfont=dict(
                color="#4633FF"
            ),
            anchor="free",
            overlaying="y",
            side="left",
            position=0.025
        ),
        yaxis3=dict(
            title="Daily cases",
            titlefont=dict(
                color="#FFA533"
            ),
            tickfont=dict(
                color="#FFA533 "
            ),
            anchor="x",
            overlaying="y",
            side="right"
        ),
        yaxis4=dict(
            title="Daily deaths",
            titlefont=dict(
                color="#820A2F"
            ),
            tickfont=dict(
                color="#820A2F"
            ),
            anchor="free",
            overlaying="y",
            side="right",
            position=0.925
        )
    )

    fig3.update_layout(title=dict(font=dict(size=28),x=0.5,xanchor='center'),
                          margin=dict(l=1, r=1, t=5, b=5))
    fig3.update_layout(legend ={'x': 0.1, 'y': 1} )

    return container1, fig1, fig3, container2, container3, fig2

# ------------------------------------------------------------------------------
if __name__ == '__main__':
    app.run_server(debug=True)
    # above line give error http://x86_64-conda_cos6-linux-gnu:8050/
    #app.run_server(host='127.0.0.1', port='8050', debug=True)


#!/usr/bin/env python

"""Tests for Alpha neuron."""

import pytest
import snntorch as snn
import torch

@pytest.fixture(scope="module")
def input_():
    return torch.Tensor([0.25, 0]).unsqueeze(-1)

@pytest.fixture(scope="module")
def alpha_instance():
    return snn.Alpha(alpha=0.6, beta=0.5, reset_mechanism="subtract")

@pytest.fixture(scope="module")
def alpha_reset_zero_instance():
    return snn.Alpha(alpha=0.6, beta=0.5, reset_mechanism="zero")

@pytest.fixture(scope="module")
def alpha_reset_none_instance():
    return snn.Alpha(alpha=0.6, beta=0.5, reset_mechanism="none")

@pytest.fixture(scope="module")
def alpha_hidden_instance():
    return snn.Alpha(alpha=0.6, beta=0.5, init_hidden=True)

@pytest.fixture(scope="module")
def alpha_hidden_reset_zero_instance():
    return snn.Alpha(
        alpha=0.6, beta=0.5, init_hidden=True, reset_mechanism="zero"
    )

@pytest.fixture(scope="module")
def alpha_hidden_reset_none_instance():
    return snn.Alpha(
        alpha=0.6, beta=0.5, init_hidden=True, reset_mechanism="none"
    )

class TestAlpha:

    with pytest.raises(ValueError):
        snn.Alpha(0.5, 0.5)

    def test_alpha(self, alpha_instance, input_):
        syn_exc, syn_inh, mem = alpha_instance.init_alpha()

        syn_exc_rec = []
        syn_inh_rec = []
        mem_rec = []
        spk_rec = []

        for i in range(2):
            spk, syn_exc, syn_inh, mem = alpha_instance(
                input_[i], syn_exc, syn_inh, mem
            )

            syn_exc_rec.append(syn_exc)
            syn_inh_rec.append(syn_inh)
            mem_rec.append(mem)
            spk_rec.append(spk)

        assert spk_rec[0] == spk_rec[1]
        print(spk_rec)
        print(syn_exc_rec)
        print(syn_inh_rec)
        assert syn_exc_rec[0] + syn_inh_rec[0] == 0
        assert syn_exc_rec[1] + syn_inh_rec[1] > 0
        assert mem_rec[0] < mem_rec[1]

    def test_alpha_reset(
        self,
        alpha_instance,
        alpha_reset_zero_instance,
        alpha_reset_none_instance,
    ):
        lif1 = alpha_instance
        lif2 = alpha_reset_zero_instance
        lif3 = alpha_reset_none_instance

        assert lif1.reset_mechanism_val == 0
        assert lif2.reset_mechanism_val == 1
        assert lif3.reset_mechanism_val == 2

        lif1.reset_mechanism = "zero"
        lif2.reset_mechanism = "none"
        lif3.reset_mechanism = "subtract"

        assert lif1.reset_mechanism_val == 1
        assert lif2.reset_mechanism_val == 2
        assert lif3.reset_mechanism_val == 0

    def test_alpha_init_hidden(self, alpha_hidden_instance, input_):

        spk_rec = []

        for i in range(2):
            spk = alpha_hidden_instance(input_[i])
            spk_rec.append(spk)

        assert spk_rec[0] == spk_rec[1]

    def test_alpha_init_hidden_reset_zero(
        self, alpha_hidden_reset_zero_instance, input_
    ):

        spk_rec = []

        for i in range(2):
            spk = alpha_hidden_reset_zero_instance(input_[i])
            spk_rec.append(spk)

        assert spk_rec[0] == spk_rec[1]

    def test_alpha_init_hidden_reset_none(
        self, alpha_hidden_reset_none_instance, input_
    ):

        spk_rec = []

        for i in range(2):
            spk = alpha_hidden_reset_none_instance(input_[i])
            spk_rec.append(spk)

        assert spk_rec[0] == spk_rec[1]

    def test_alpha_cases(self, alpha_hidden_instance, input_):
        with pytest.raises(TypeError):
            alpha_hidden_instance(input_, input_)


"""
sum of primes below 2 million

miller rabin test
a = 31, 73

make n into n-1
divide n-1 by 2 until it wont divide evenly, record num of divisions
leftover multiplier is d
num of divisions is s

if a^d != 1 mod n
and a^2rd != -1 mod n  repeated where r is all possibilities from 1 to s-1
then n not prime

generate prime down list with prime tests built in
generate prime up list with prime tests built in
merge lists
"""

import time
import math

def divcounter (num):
    count = 0
    while num%2 == 0:
        count += 1
        num /= 2
    #print('dividing ',num*2**count,' and count is ',count)
    return count

def primetest(ugh):
    #print ('now prime testing ',ugh)
    s = divcounter(ugh-1) #the number of even divides by two
    #print(type(s))
    d = int((ugh-1) / 2**s) #the remaining multiplier after div two
    #print(type(d))
    #print ('s= ',s,' and d= ',d)
    result = True
    if 2**d%ugh != 1 and 3**d%ugh != 1 and 5**d%ugh != 1: #true if not prime
        for r in range (s):#ends before the end of s-1
            f = d*2**r
            g = ugh-1
            if (2**(f)%ugh) != (g) and (3**(f)%ugh) != (g) and (5**(f)%ugh) != (g):
                #print('using ', r,', ',2**(d*2**r)%ugh, ' and ',3**(d*2**r)%ugh,' and ',5**(d*2**r)%ugh,' should not be ', ugh-1)
                result = False
            else:
                #print('prime using ', r,', ',2**(d*2**r)%ugh, ' and ',3**(d*2**r)%ugh,' and ',5**(d*2**r)%ugh,' should not be ', ugh-1)
                result = True
                break
    #print(ugh, ' is ', result)
    return result

start = time.time()
m = 2000000
a = [2, 3, 5]
b = [6*n-1 for n in range(2, int((m+1)/6)+1) if primetest(6*n-1) == 1] #start at 2
c = [6*n+1 for n in range(1, int((m-1)/6)+1) if primetest(6*n+1) == 1] #start at 1
end = time.time()
print(sum(a+b+c))
print('total time: ',end - start)
exit()


from ._block import Block
from ..Utilities.Operations import generalized_rotation, XY_full_rotation, inversed_XY_full_rotation
from ..Utilities.Tools import pauliword2string

class EfficientCoupledCluster(Block):
    """

    """
    n_parameter = -1
    IS_INVERSE_DEFINED = True

    def __init__(self, qsubset, init_angle=None):
        n_parameter = len(qsubset) * 2 + 1
        Block.__init__(self, n_parameter=n_parameter, active_qubits=qsubset)
        self.qsubset = qsubset
        if init_angle == None:
            self.parameter = [0.0] * n_parameter
        else:
            self.parameter = init_angle

    def apply_forward_gate(self, parameter, wavefunction):
        XY_full_rotation(wavefunction, self.qsubset, [
            self.parameter[i] + parameter[i] for i in range(1, len(self.parameter))])
        generalized_rotation(wavefunction, self.qsubset,
                             [1] * len(self.qsubset), evolution_time=parameter[0] + self.parameter[0])
        inversed_XY_full_rotation(wavefunction, self.qsubset, [
            self.parameter[i] + parameter[i] for i in range(1, len(self.parameter))])
        return

    def apply_inverse_gate(self, parameter, wavefunction):
        XY_full_rotation(wavefunction, self.qsubset, [
            self.parameter[i] + parameter[i] for i in range(1, len(self.parameter))])
        generalized_rotation(wavefunction, self.qsubset,
                             [1] * len(self.qsubset), evolution_time=-(parameter[0] + self.parameter[0]))
        inversed_XY_full_rotation(wavefunction, self.qsubset, [
            self.parameter[i] + parameter[i] for i in range(1, len(self.parameter))])
        return

    def get_gate_used(self):
        return {"CNOT": len(self.qsubset) * 2, "SingleRotation": len(self.qsubset) * 2 + 1}

    def __str__(self):
        info = self.basic_info_string()
        info += "; Qsubset:" + str(self.qsubset)
        return info


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Aug  4 16:22:25 2022

@author: juan
"""

import pandas as pd
import numpy as np
from getData import sentinel
#%% reads dataset
f = open("SecretString.txt", "r")
NAME_DB=f.read()
#initializes the class
db=sentinel(NAME_DB)
#gets derived gas output dataframe
MIN_BLOCK=2000000
df=db.getGasfromBlock(minBlock=MIN_BLOCK,unique=False)
base_fee=db.getParentBaseFee(MIN_BLOCK)
df=df.sort_values('height')
#%% groups by block.
dfg=df.groupby(by=['height','actor_family','method'])['gas_used'].sum()
number_of_messages_per_block=df.groupby(by='height').count()
df_per_block=df.groupby(by=['height']).sum()
G_LIMIT=10_000_000_000.
G_TARGET=10_000_000_000./2.
#%%
import matplotlib.pyplot as plt

base_fees=np.array(df.groupby(by=['height'])['parent_base_fee'].unique())
base_fees=np.concatenate(base_fees)
plt.subplot(131)
plt.plot(base_fees/1E9)
plt.subplot(132)
G_tilde=8*np.diff(base_fees)/base_fees[:-1]
plt.plot(G_tilde)
plt.subplot(133)
plt.hist(G_tilde,bins=30)
plt.tight_layout()


import os
import trimesh
import pyrender
import numpy as np
from get_mesh import as_mesh
import matplotlib.pyplot as plt
import imageio

egypt = trimesh.load('../Dataset/models/model_normalized.obj')
egypt_trimesh = as_mesh(egypt)
egypt_mesh = pyrender.Mesh.from_trimesh(egypt_trimesh)

picture_folder = 'model_picture_data'
depth_folder = 'model_depth_data'
os.makedirs(picture_folder, exist_ok=True)
os.makedirs(depth_folder, exist_ok=True)
def generate_points(radius, num_points):
    theta_values = np.linspace(0, np.pi, num_points)
    phi_values = np.linspace(0, np.pi, num_points)
    points = []

    for phi in phi_values:
        for theta in theta_values:
            x = radius * np.cos(theta) * np.cos(phi)
            y = radius * np.sin(theta) * np.cos(phi)
            z = radius * np.sin(phi)
            points.append((x, z, y))

    return points

focal = 1.0

points_on_semicircle = generate_points(focal, 10)

camera_positions = []

for i, point in enumerate(points_on_semicircle):
    camera_position =  np.array(point)
    camera_target = np.array([0,0,0])
    D = camera_position - camera_target
    D = D / np.linalg.norm(D) if np.linalg.norm(D) != 0 else D
    # print(f'D {D}')

    up = np.array([0,1,0])
    R = np.cross(up, D)
    R = R / np.linalg.norm(R) if np.linalg.norm(R) != 0 else R
    # print(f'R {R}')

    U = np.cross(D, R)
    # print(f'U {U}')

    rotation = np.eye(4)
    rotation[0:3, 0] = R
    rotation[0:3, 1] = U
    rotation[0:3, 2] = D
    # print(rotation)

    import copy
    r_t = copy.deepcopy(rotation)
    r_t[0:3,3] = camera_position
    camera_positions.append(r_t)

np.savez('camera_positions.npz', arr1=camera_positions)

scene = pyrender.Scene(bg_color=[0, 0, 0, 0])
scene.add(egypt_mesh, name='model')

for i, camera_pose in enumerate(camera_positions):
    camera = pyrender.PerspectiveCamera(yfov=np.pi / 3.0, aspectRatio=1.0)
    camera_node = scene.add(camera, pose=camera_pose)

    DireLight = pyrender.DirectionalLight(color=np.ones(3), intensity=15.0)
    light_node = scene.add(DireLight, pose=camera_pose)

    r = pyrender.OffscreenRenderer(viewport_height=200, viewport_width=200)

    color, depth = r.render(scene)

    img_filename = os.path.join(picture_folder, f'model_image_{i}.png')
    imageio.imwrite(img_filename, color)

    depth_normalized = (depth - depth.min()) / (depth.max() - depth.min())
    depth_image = (255 * depth_normalized).astype(np.uint8)

    depth_filename = os.path.join(depth_folder, f'model_depth_{i}.png')
    imageio.imwrite(depth_filename, depth_image)

    scene.remove_node(camera_node)
    scene.remove_node(light_node)
    r.delete()

# Create Cloud Point Data
def sample_points(obj_path, n_points=10000):
    mesh = trimesh.load_mesh(obj_path)
    points, _ = trimesh.sample.sample_surface(mesh, n_points)
    return points

def rotate_model(points):
    rotated_points = []

    rotation_matrix = np.array([[1, 0, 0],
                                [0, 0, -1],
                                [0, 1, 0]])

    for point in points:
        rotated_point = np.dot(rotation_matrix, point)
        rotated_points.append(rotated_point)

    return rotated_points

def sample_points(obj_path, n_points=10000):
    # Load the content at obj_path
    loaded = trimesh.load(obj_path)

    # Check if it's a single Trimesh or a Scene
    if isinstance(loaded, trimesh.Scene):
        # Extract the first mesh from the scene (you may need to adjust if you want a different mesh)
        mesh = loaded.dump(concatenate=True)
    else:
        mesh = loaded

    points, _ = trimesh.sample.sample_surface(mesh, n_points)
    return points

obj_path = "../Dataset/egypt_model/baked_mesh.obj"
points = sample_points(obj_path, n_points=10000)
points = rotate_model(points)
with open("model_point.ply", 'w') as ply:
        ply.write("ply\n")
        ply.write("format ascii 1.0\n")
        ply.write(f"element vertex {len(points)}\n")
        ply.write("property float x\n")
        ply.write("property float y\n")
        ply.write("property float z\n")
        ply.write("end_header\n")
        for point in points:
            ply.write(" ".join(map(str, point)) + '\n')