from pathlib import Path
import numpy as np

data_folder = Path(".")

class IntCodeProgram:
    """A Class for the state of an IntCode program"""

    def __init__(self, instr):
        self.instructions = dict(zip(list(range(len(instrs))), instrs))
        self.rel_base = 0
        self.instr_ptr = 0
        self.input = None
        self.output = None

    def get(self, ptr, mode):
        loc = self._find_loc(ptr, mode)
        if loc in self.instructions:
            return self.instructions[loc]
        else:
            return 0

    def set(self, ptr, mode, value):
        loc = self._find_loc(ptr, mode)
        self.instructions[loc] = value

    def _find_loc(self, ptr, mode):
        if mode == 1:
            return ptr
        elif mode == 0:
            return self.get(ptr, 1)
        elif mode == 2:
            return self.get(ptr, 1) + self.rel_base

class Robot:
    """A class for a hull painting robot"""

    def __init__(self, prog, initial_panel_color=0):
        self.location = (0, 0)
        self.panel_colors = {(0, 0): initial_panel_color}
        self.times_painted = {(0, 0): 0}
        self.orientation = 0
        self.prog = IntCodeProgram(prog)

    def paint_panel(self, color_int):
        self.panel_colors[self.location] = color_int
        self.times_painted[self.location] += 1

    def move(self, right):
        if right:
            self.orientation = (self.orientation + 90) % 360
        else:
            self.orientation = (self.orientation - 90) % 360

        if self.orientation == 0:
            self.location = (self.location[0] - 1, self.location[1])
        elif self.orientation == 90:
            self.location = (self.location[0], self.location[1] + 1)
        elif self.orientation == 180:
            self.location = (self.location[0] + 1, self.location[1])
        elif self.orientation == 270:
            self.location = (self.location[0], self.location[1] - 1)

        if self.location not in self.panel_colors:
            self.panel_colors[self.location] = 0
            self.times_painted[self.location] = 0

    def get_n_tiles_painted(self):
        tiles_painted = 0
        for tile in self.times_painted:
            if self.times_painted[tile] > 0:
                tiles_painted += 1
        return tiles_painted

    def print_hull(self):
        row_dim = [0, 0]
        column_dim = [0, 0]
        tile_rows = []
        tile_columns = []
        colors = []
        for tile in self.panel_colors:
            if tile[0] > row_dim[1]:
                row_dim[1] = tile[0]
            elif tile[0] < row_dim[0]:
                row_dim[0] = tile[0]
            if tile[1] > column_dim[1]:
                column_dim[1] = tile[1]
            elif tile[1] < column_dim[0]:
                column_dim[0] = tile[1]
            tile_rows.append(tile[0])
            tile_columns.append(tile[1])
            colors.append(self.panel_colors[tile])

        tile_rows = np.array(tile_rows)
        tile_columns = np.array(tile_columns)
        hull = np.zeros(
            (row_dim[1] - row_dim[0] + 1, column_dim[1] - column_dim[0] + 1), dtype=int
        )
        hull[tile_rows - row_dim[0], tile_columns - column_dim[0]] = colors

        # Assumes text color is white on the system
        print(
            "\n".join(
                [
                    "".join([str(d) for d in row])
                    .replace("0", " ")
                    .replace("1", u"\u2588")
                    for row in hull
                ]
            )
        )

def add(prog, modes):
    n_params = 3
    modes = modes + [0] * (n_params - len(modes))
    prog.set(
        prog.instr_ptr + 2,
        modes[2],
        prog.get(prog.instr_ptr, modes[0]) + prog.get(prog.instr_ptr + 1, modes[1]),
    )
    prog.instr_ptr += n_params
    return None

def mult(prog, modes):
    n_params = 3
    modes = modes + [0] * (n_params - len(modes))
    prog.set(
        prog.instr_ptr + 2,
        modes[2],
        prog.get(prog.instr_ptr, modes[0]) * prog.get(prog.instr_ptr + 1, modes[1]),
    )
    prog.instr_ptr += n_params

def inp(prog, modes):
    n_params = 1
    modes = modes + [0] * (n_params - len(modes))
    prog.set(prog.instr_ptr, modes[0], prog.input)
    prog.instr_ptr += n_params

def outp(prog, modes):
    n_params = 1
    modes = modes + [0] * (n_params - len(modes))
    prog.output = prog.get(prog.instr_ptr, modes[0])
    prog.instr_ptr += n_params

def jump_if_true(prog, modes):
    n_params = 2
    modes = modes + [0] * (n_params - len(modes))

    if prog.get(prog.instr_ptr, modes[0]) > 0:
        prog.instr_ptr = prog.get(prog.instr_ptr + 1, modes[1])
    else:
        prog.instr_ptr += n_params

def jump_if_false(prog, modes):
    n_params = 2
    modes = modes + [0] * (n_params - len(modes))

    if prog.get(prog.instr_ptr, modes[0]) == 0:
        prog.instr_ptr = prog.get(prog.instr_ptr + 1, modes[1])
    else:
        prog.instr_ptr += n_params

def less_than(prog, modes):
    n_params = 3
    modes = modes + [0] * (n_params - len(modes))
    if prog.get(prog.instr_ptr, modes[0]) < prog.get(prog.instr_ptr + 1, modes[1]):
        prog.set(prog.instr_ptr + 2, modes[2], 1)
    else:
        prog.set(prog.instr_ptr + 2, modes[2], 0)
    prog.instr_ptr += n_params

def equals(prog, modes):
    n_params = 3
    modes = modes + [0] * (n_params - len(modes))
    if prog.get(prog.instr_ptr, modes[0]) == prog.get(prog.instr_ptr + 1, modes[1]):
        prog.set(prog.instr_ptr + 2, modes[2], 1)
    else:
        prog.set(prog.instr_ptr + 2, modes[2], 0)
    prog.instr_ptr += n_params

def adj_rel_base(prog, modes):
    n_params = 1
    modes = modes + [0] * (n_params - len(modes))
    prog.rel_base += prog.get(prog.instr_ptr, modes[0])
    prog.instr_ptr += n_params

operations = {
    1: add,
    2: mult,
    3: inp,
    4: outp,
    5: jump_if_true,
    6: jump_if_false,
    7: less_than,
    8: equals,
    9: adj_rel_base,
}

def run_robot(robot):
    end_of_program = False
    output_type = "color"
    while not end_of_program:
        digits = [int(d) for d in str(robot.prog.get(robot.prog.instr_ptr, 1))]
        if len(digits) == 1:
            op_mode = digits[-1]
        else:
            op_mode = digits[-2] * 10 + digits[-1]
        if op_mode == 99:
            return
        else:
            modes = digits[-3::-1]
            robot.prog.instr_ptr += 1
            if op_mode == 3:
                robot.prog.input = robot.panel_colors[robot.location]
            operations[op_mode](robot.prog, modes)

            if op_mode == 4:
                if output_type == "color":
                    robot.paint_panel(robot.prog.output)
                    output_type = "direction"
                elif output_type == "direction":
                    robot.move(robot.prog.output)
                    output_type = "color"

file = data_folder / "input.txt"
instrs = [int(instr) for instr in file.read_text().split(",")]

def main():
    # Part 1
    robot = Robot(instrs, 0)
    run_robot(robot)
    print("Part 1:")
    print(f"{robot.get_n_tiles_painted()} tiles were painted at least once.")
    print()

    # Part 2
    robot = Robot(instrs, 1)
    run_robot(robot)
    print("Part 2:")
    print(f"The completed hull looks like:")
    robot.print_hull()

if __name__ == "__main__":
    main()


# coding=utf-8
#
# created by kpe on 04.11.2019 at 2:07 PM
#

from __future__ import division, absolute_import, print_function

import unittest

import os
import tempfile

import numpy as np
import tensorflow as tf

import bert

from .test_common import AbstractBertTest, MiniBertFactory

class TestExtendSegmentVocab(AbstractBertTest):

    def setUp(self) -> None:
        tf.compat.v1.reset_default_graph()
        tf.compat.v1.enable_eager_execution()
        print("Eager Execution:", tf.executing_eagerly())

    def test_extend_pretrained_tokens(self):
        model_dir = tempfile.TemporaryDirectory().name
        os.makedirs(model_dir)
        save_path = MiniBertFactory.create_mini_bert_weights(model_dir)
        tokenizer = bert.bert_tokenization.FullTokenizer(vocab_file=os.path.join(model_dir, "vocab.txt"), do_lower_case=True)

        ckpt_dir = os.path.dirname(save_path)
        bert_params = bert.params_from_pretrained_ckpt(ckpt_dir)

        self.assertEqual(bert_params.token_type_vocab_size, 2)
        bert_params.extra_tokens_vocab_size = 3

        l_bert = bert.BertModelLayer.from_params(bert_params)

        # we dummy call the layer once in order to instantiate the weights
        l_bert([np.array([[1, 1, 0]]), np.array([[1, 0, 0]])], mask=[[True, True, False]])

        mismatched = bert.load_stock_weights(l_bert, save_path)
        self.assertEqual(0, len(mismatched), "token_type embeddings should have mismatched shape")

        l_bert([np.array([[1, -3, 0]]), np.array([[1, 0, 0]])], mask=[[True, True, False]])


import matplotlib.pyplot as plt
import numpy as np

fp = open('/home/zt/Maillage/sibson/errors.txt', 'r')
errors = fp.readline().split(' ')[:-1]
fp.close()
errors = np.array(errors).reshape((-1, 3)).T

plt.figure(figsize=(15, 8))
plt.plot(errors[0, :-10], color = 'red')
plt.plot(errors[1, :-10], color = 'green')
plt.plot(errors[2, :-10], color = 'blue')
plt.xlabel('iteration')
plt.ylabel('mae')
plt.grid()
plt.title('Training Error')
plt.show()


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('todo', '0003_auto_20140919_2201'),
    ]

    operations = [
        migrations.AddField(
            model_name='todo',
            name='category',
            field=models.CharField(default=b'Do Now', max_length=32, choices=[(b'Do Now', b'Do Now'), (b'Later', b'later')]),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='todo',
            name='link',
            field=models.URLField(default=None, null=True, blank=True),
            preserve_default=True,
        ),
    ]


import xml.etree.ElementTree as ET

from ..track import Track

def parser(
    file_path,
    *,
    require_title=True,
    require_duration=False,
    require_year=False,
    require_bpm=False,
    require_fp=False,
    default_artist="",
    verbose=False,
):
    """
    Traktor supports:
    - title
    - artist
    - year
    - playtime
    - bpm
    """
    if require_fp:
        raise NotImplementedError("Traktor parser doesn't support file paths.")

    tracks = []
    traktor_xml = ""
    counter = 0

    with open(file_path) as file:
        traktor_xml = ET.fromstring(file.read())

    entries = traktor_xml.findall("COLLECTION/ENTRY")

    for track in entries:
        playtime = 0
        year = ""
        bpm = 0

        try:
            track_title = track.get("TITLE", "").strip()
            track_artist = track.get("ARTIST", "").strip()
            if not track_artist:
                track_artist = default_artist

            meta = track.find("INFO")
            if meta is not None:
                playtime = int(meta.get("PLAYTIME", ""))
                # key = meta.get("KEY", "")
                year = meta.get("RELEASE_DATE", "")
                if verbose:  # pragma: no cover
                    print(f"found year: {year}, playtime: {playtime}")

            tempometa = track.find("TEMPO")
            if tempometa is not None:
                bpm = int(float(tempometa.get("BPM", 0)))

            tracks.append(
                Track(
                    title=track_title,
                    artist=track_artist,
                    year=year,
                    duration=playtime,
                    bpm=bpm,
                )
            )
        except Exception as e:  # pragma: no cover
            if verbose:
                print(f"Skipping line {counter}", e)

        counter += 1

    return tracks


#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Created on 2017/12/16

__author__ = "Jianguo Jin (jinjianguosky@hotmail.com)"

"""
    Description:

"""
import unittest
from selenium import webdriver

class SearchTest(unittest.TestCase):
    """【天猫产品搜索】单元测试版本 """

    def test_search_by_name(self):
        driver = webdriver.Chrome()
        driver.implicitly_wait(20)
        driver.get('https://www.tmall.com')

        search_field = driver.find_element_by_name('q')
        search_field.clear()
        search_field.send_keys('小米 Note')
        search_field.submit()

        products = driver.find_elements_by_xpath('//*[@id="J_ItemList"]/div/div/div/a[1]')
        self.assertEqual(158, len(products))

if __name__ == '__main__':
    unittest.main()


import xarray as xa
from data import eisc
from data import zc
from data import dcm

test_run = True
input_dir = "/discover/nobackup/projects/eis_freshwater/swang9/OL_1km/OUTPUT.RST.2013"
month = "201303" if test_run else  "*"
eisc( cache = "/gpfsm/dnb43/projects/p151/zarr", mode = "eis.freshwater.swang9" )
input = f"{input_dir}/SURFACEMODEL/{month}/LIS_HIST" + "_{time}.d01.nc"

if __name__ == '__main__':

    dcm().init_cluster()
    dset: xa.Dataset = zc().get_input( input, merge_dim="time" )

    time = dset['time'].values
    path = dset['_eis_source_path'].values
    for idx in range(time.size):
       print( f"{time[idx]}: {path[idx]}")


import os
import sys
import shutil
import time
import random
import torch
import logging
from pathlib import Path

import numpy as np
import statistic
from torch import multiprocessing
from torch.nn import functional as F
import nibabel as nib
from tensorboardX import SummaryWriter
from skimage.measure import label

def mkdir(path, level=2, create_self=True):
    """ Make directory for this path,
    level is how many parent folders should be created.
    create_self is whether create path(if it is a file, it should not be created)

    e.g. : mkdir('/home/parent1/parent2/folder', level=3, create_self=False),
    it will first create parent1, then parent2, then folder.

    :param path: string
    :param level: int
    :param create_self: True or False
    :return:
    """
    p = Path(path)
    if create_self:
        paths = [p]
    else:
        paths = []
    level -= 1
    while level != 0:
        p = p.parent
        paths.append(p)
        level -= 1

    for p in paths[::-1]:
        p.mkdir(exist_ok=True)

def seed_reproducer(seed=2022):
    """Reproducer for pytorch experiment.

    Parameters
    ----------
    seed: int, optional (default = 2020)
        Radnom seed.

    Example
    -------
    seed_reproducer(seed=2020).
    """
    random.seed(seed)
    os.environ["PYTHONHASHSEED"] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)#set all gpus seed
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False#if input data type and channels' changes arent' large use it improve train efficient
        torch.backends.cudnn.enabled = True

def cutmix_config_log(save_path, tensorboard=False):
    writer = SummaryWriter(str(save_path), filename_suffix=time.strftime('_%Y-%m-%d_%H-%M-%S')) if tensorboard else None

    save_path = str(Path(save_path) / 'log.txt')
    formatter = logging.Formatter('%(levelname)s [%(asctime)s] %(message)s')

    logger = logging.getLogger(save_path.split('/')[-2])
    logger.setLevel(logging.INFO)

    handler = logging.FileHandler(save_path)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    sh = logging.StreamHandler(sys.stdout)
    handler.setFormatter(formatter)
    logger.addHandler(sh)

    return logger, writer

class AverageMeter(object):
    """Computes and stores the average and current value"""

    def __init__(self):
        self.reset()

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

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

class Measures():
    def __init__(self, keys, writer, logger):
        self.keys = keys
        self.measures = {k: AverageMeter() for k in self.keys}
        self.writer = writer
        self.logger = logger

    def reset(self):
        [v.reset() for v in self.measures.values()]

class CutPreMeasures(Measures):
    def __init__(self, writer, logger):
        keys = ['ce_loss', 'dice_loss', 'loss_all', 'train_dice']
        super(CutPreMeasures, self).__init__(keys, writer, logger)

    def update(self, out, lab, *args):
        args = list(args)
        masks = get_mask(out)
        train_dice1 = statistic.dice_ratio(masks, lab)
        args.append(train_dice1)

        dict_variables = dict(zip(self.keys, args))
        for k, v in dict_variables.items():
            self.measures[k].update(v)

    def log(self, epoch, step):
        # self.logger.info('epoch : %d, step : %d, train_loss: %.4f, train_dice: %.4f' % (
        #     epoch, step, self.measures['loss_all'].avg, self.measures['train_dice'].avg))

        log_string, params = 'Epoch : {}', []
        for k in self.keys:
            log_string += ', ' + k + ': {:.4f}'
            params.append(self.measures[k].val)
        self.logger.info(log_string.format(epoch, *params))

        for k, measure in self.measures.items():
            k = 'pretrain/' + k
            self.writer.add_scalar(k, measure.avg, step)
        self.writer.flush()

def get_mask(out, thres=0.5):
    probs = F.softmax(out, 1)
    masks = (probs >= thres).float()
    masks = masks[:, 1, :, :].contiguous()
    return masks

def save_net_opt(net, optimizer, path, epoch):
    state = {
        'net': net.state_dict(),
        'opt': optimizer.state_dict(),
        'epoch': epoch,
    }
    torch.save(state, str(path))

def load_net_opt(net, optimizer, path):
    state = torch.load(str(path))
    net.load_state_dict(state['net'])
    optimizer.load_state_dict(state['opt'])

def save_net(net, path):
    state = {
        'net': net.state_dict(),
    }
    torch.save(state, str(path))

def load_net(net, path):
    state = torch.load(str(path))
    net.load_state_dict(state['net'])

def generate_mask(img, patch_size):
    batch_l = img.shape[0]
    #batch_unlab = unimg.shape[0]
    loss_mask = torch.ones(batch_l, 96, 96, 96).cuda()
    #loss_mask_unlab = torch.ones(batch_unlab, 96, 96, 96).cuda()
    mask = torch.ones(96, 96, 96).cuda()
    w = np.random.randint(0, 96 - patch_size)
    h = np.random.randint(0, 96 - patch_size)
    z = np.random.randint(0, 96 - patch_size)
    mask[w:w+patch_size, h:h+patch_size, z:z+patch_size] = 0
    loss_mask[:, w:w+patch_size, h:h+patch_size, z:z+patch_size] = 0
    #loss_mask_unlab[:, w:w+patch_size, h:h+patch_size, z:z+patch_size] = 0
    #cordi = [w, h, z]
    return mask.long(), loss_mask.long()

def config_log(save_path, tensorboard=False):
    writer = SummaryWriter(str(save_path), filename_suffix=time.strftime('_%Y-%m-%d_%H-%M-%S')) if tensorboard else None

    save_path = str(Path(save_path) / 'log.txt')
    formatter = logging.Formatter('%(levelname)s [%(asctime)s] %(message)s')

    logger = logging.getLogger(save_path.split('/')[-2])
    logger.setLevel(logging.INFO)

    handler = logging.FileHandler(save_path)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    sh = logging.StreamHandler(sys.stdout)
    handler.setFormatter(formatter)
    logger.addHandler(sh)

    return logger, writer

class CutmixFTMeasures(Measures):
    def __init__(self, writer, logger):
        keys = ['mix_loss_lab', 'mix_loss_unlab', 'loss_all']
        super(CutmixFTMeasures, self).__init__(keys, writer, logger)

    def update(self, *args):
        args = list(args)
        # masks = get_mask(out[0])
        # train_dice = statistic.dice_ratio(masks, lab)
        # args.append(train_dice)

        dict_variables = dict(zip(self.keys, args))
        for k, v in dict_variables.items():
            self.measures[k].update(v)

    def log(self, epoch, step):
        # self.logger.info('epoch : %d, step : %d, train_loss: %.4f, train_dice: %.4f' % (
        #     epoch, step, self.measures['loss_all'].avg, self.measures['train_dice'].avg))

        log_string, params = 'Epoch : {}', []
        for k in self.keys:
            log_string += ', ' + k + ': {:.4f}'
            params.append(self.measures[k].val)
        self.logger.info(log_string.format(epoch, *params))

        for k, measure in self.measures.items():
            k = 'pretrain/' + k
            self.writer.add_scalar(k, measure.avg, step)
        self.writer.flush()

def to_cuda(tensors, device=None):
    res = []
    if isinstance(tensors, (list, tuple)):
        for t in tensors:
            res.append(to_cuda(t, device))
        return res
    elif isinstance(tensors, (dict,)):
        res = {}
        for k, v in tensors.items():
            res[k] = to_cuda(v, device)
        return res
    else:
        if isinstance(tensors, torch.Tensor):
            if device is None:
                return tensors.cuda()
            else:
                return tensors.to(device)
        else:
            return tensors

def get_cut_mask(out, thres=0.5, nms=True, connect_mode=1):
    probs = F.softmax(out, 1)
    masks = (probs >= thres).type(torch.int64)
    masks = masks[:, 1, :, :].contiguous()
    if nms==True:
        masks = LargestCC_pancreas(masks, connect_mode=connect_mode)
    return masks

def LargestCC_pancreas(segmentation, connect_mode=1):
    N = segmentation.shape[0]
    batch_list = []
    for n in range(N):
        n_prob = segmentation[n].detach().cpu().numpy()
        labels = label(n_prob, connectivity=connect_mode)
        if labels.max() != 0:
            largestCC = labels == np.argmax(np.bincount(labels.flat)[1:])+1
        else:
            largestCC = n_prob
        batch_list.append(largestCC)

    return torch.Tensor(batch_list).cuda()

@torch.no_grad()
def update_ema_variables(model, ema_model, alpha):
    for ema_param, param in zip(ema_model.parameters(), model.parameters()):
        ema_param.data.mul_(alpha).add_((1 - alpha) * param.data)


from django.urls import path
from django.http import HttpResponse
from django.template import Template, Context

def index(request):
    return HttpResponse('''

Welcome to my homepage

        My favorite Game of Thrones characters

        About me

    ''')

def characters(request):
    return HttpResponse('''

My favorite Game of Thrones Characters

Daenerys Targaryen

Jon Snow

Tyrion Lannister

Arya Stark

        Back to home page
    ''')

def about_me(request):
    return HttpResponse('''

About me

Just your average django dev

        Back to home page
    ''')

urlpatterns = [
    path('', index),
    path('my-favorite-characters', characters),
    path('ABout-/-/meeeee', about_me),
]

# Boilerplate -- Don't worry about understanding anything from here down
def main():
    import sys
    from django.conf import settings
    from django.core.management import execute_from_command_line

    settings.configure(
        DEBUG=True,
        ROOT_URLCONF=sys.modules[__name__],
    )

    execute_from_command_line(sys.argv)

if __name__ == '__main__':
    main()


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

# 1. Import the dataset using Pandas from the given URL
url = 'https://raw.githubusercontent.com/SR1608/Datasets/main/covid-data.csv'
df = pd.read_csv(url)

# 2. High Level Data Understanding
# a. Find no. of rows & columns in the dataset
rows, columns = df.shape
print("Number of rows:", rows)
print("Number of columns:", columns)

# b. Data types of columns
print("Data types of columns:")
print(df.dtypes)

# c. Info & describe of data in dataframe
print("Info:")
print(df.info())
print("Describe:")
print(df.describe())

# 3. Low Level Data Understanding
# a. Find count of unique values in the location column
unique_locations = df['location'].nunique()
print("Count of unique values in the location column:", unique_locations)

# b. Find which continent has the maximum frequency using value counts
continent_frequency = df['continent'].value_counts()
max_continent_frequency = continent_frequency.idxmax()
print("Continent with maximum frequency:", max_continent_frequency)

# c. Find maximum & mean value in 'total_cases'
max_total_cases = df['total_cases'].max()
mean_total_cases = df['total_cases'].mean()
print("Maximum value in 'total_cases':", max_total_cases)
print("Mean value in 'total_cases':", mean_total_cases)

# d. Find 25%, 50%, and 75% quartile value in 'total_deaths'
quartiles = df['total_deaths'].quantile([0.25, 0.5, 0.75])
print("25% Quartile value in 'total_deaths':", quartiles[0.25])
print("50% Quartile value in 'total_deaths':", quartiles[0.5])
print("75% Quartile value in 'total_deaths':", quartiles[0.75])

# e. Find which continent has the maximum 'human_development_index'
max_hdi_continent = df.loc[df['human_development_index'].idxmax(), 'continent']
print("Continent with the maximum 'human_development_index':", max_hdi_continent)

# f. Find which continent has the minimum 'gdp_per_capita'
min_gdp_continent = df.loc[df['gdp_per_capita'].idxmin(), 'continent']
print("Continent with the minimum 'gdp_per_capita':", min_gdp_continent)

# 4. Filter the dataframe with only specific columns and update the data frame
columns_to_keep = ['continent', 'location', 'date', 'total_cases', 'total_deaths', 'gdp_per_capita', 'human_development_index']
df = df[columns_to_keep]

# 5. Data Cleaning
# a. Remove all duplicate observations
df = df.drop_duplicates()

# b. Find missing values in all columns
missing_values = df.isnull().sum()
print("Missing values in each column:")
print(missing_values)

# c. Remove all observations where continent column value is missing
df = df.dropna(subset=['continent'])

# d. Fill all missing values with 0
df = df.fillna(0)

# 6. Date time format
# a. Convert date column to datetime format
df['date'] = pd.to_datetime(df['date'])

# b. Create a new column 'month' by extracting month data from the date column
df['month'] = df['date'].dt.month

# 7. Data Aggregation
# a. Find max value in all columns using groupby function on 'continent' column
df_groupby = df.groupby('continent').max().reset_index()

# 8. Feature Engineering
# Create a new feature 'total_deaths_to_total_cases' by ratio of 'total_deaths' column to 'total_cases'
df_groupby['total_deaths_to_total_cases'] = df_groupby['total_deaths'] / df_groupby['total_cases']

# 9. Data Visualization
# a. Perform Univariate analysis on 'gdp_per_capita' column by plotting histogram using seaborn dist plot
sns.displot(df['gdp_per_capita'], kde=False)
plt.title('Histogram of GDP per capita')
plt.show()

# b. Plot a scatter plot of 'total_cases' & 'gdp_per_capita'
plt.scatter(df['total_cases'], df['gdp_per_capita'])
plt.xlabel('Total Cases')
plt.ylabel('GDP per capita')
plt.title('Scatter plot: Total Cases vs GDP per capita')
plt.show()

# c. Plot Pairplot on df_groupby dataset
sns.pairplot(df_groupby)
plt.title('Pairplot of df_groupby dataset')
plt.show()

# d. Plot a bar plot of 'continent' column with 'total_cases'
sns.catplot(x='continent', y='total_cases', kind='bar', data=df_groupby)
plt.title('Bar plot: Continent vs Total Cases')
plt.show()

# 10. Save the df_groupby dataframe in your local drive using pandas.to_csv function
df_groupby.to_csv('df_groupby.csv', index=False)


from django.db import models
from django.contrib.auth.models import User
from final.settings import AUTH_USER_MODEL as User
# 질문란
class Question(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    subject = models.CharField(max_length=200)
    content = models.TextField()
    create_date = models.DateTimeField()
    modify_date = models.DateTimeField(null=True, blank=True)
    # 작성한 질문이 제목으로 보여지게 함
    def __str__(self):
        return self.subject
# 댓글란
class Answer(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    content = models.TextField()
    create_date = models.DateTimeField()
    modify_date = models.DateTimeField(null=True, blank=True)


from streamer.database import StreamerDB
from pymongo.errors import WriteError

class StreamerUsers:

    def __init__(self):
        """
        BookdlUsers is the mongo collection for the documents that holds the details of the users.

        Functions:
            insert_user: insert new documents, that contains the details of the new users who started using the bot.

            get_user: return the document that contains the user_id for the the given telegram user id.

        """
        self.user_collection = StreamerDB().db['Users']

    async def insert_user(self, user_id: int):
        if self.user_collection.count_documents({'user_id': user_id}) > 0:
            return
        else:
            self.user_collection.insert_one({
                'user_id': user_id,
                'stream_locations': [],
            })

    async def get_user(self, user_id: int):
        return self.user_collection.find_one({'user_id': user_id})

    async def add_stream_loc(self, user_id: int, stream_chat_id: int,
                             name: str, stream_url: str, stream_key: str):
        try:
            res = self.user_collection.update_one({'user_id': user_id}, {
                '$push': {
                    'stream_locations': {
                        'name': name,
                        'stream_url': stream_url,
                        'stream_key': stream_key,
                        'stream_chat_id': stream_chat_id
                    }
                }
            })
        except WriteError as e:
            None, e.details
        return res, None

    async def remove_stream_loc(self,
                                user_id: int,
                                stream_chat_id: int = None):
        try:
            if stream_chat_id:
                query = {
                    '$pull': {
                        'stream_locations': {
                            'stream_chat_id': stream_chat_id
                        }
                    }
                }
            else:
                query = {'$set': {'stream_locations': []}}

            res = self.user_collection.update_one({'user_id': user_id}, query)

        except WriteError as e:
            None, e.details
        return res, None

    async def get_stream_loc(self, user_id: int, stream_chat_id: int = None):
        res = (await self.get_user(user_id))['stream_locations']
        if not stream_chat_id:
            return res
        else:
            return next(
                (loc
                 for loc in res if loc['stream_chat_id'] == stream_chat_id),
                None)


import random
import requests
import time

class LED_manager:
    def __new__(cls):
        if not hasattr(cls, 'instance'):
            cls.instance = super(LED_manager, cls).__new__(cls)
        return cls.instance

    def __init__(self) -> None:
        self.leds = []
        self.create_leds()

    def configure_wled(self,url, settings):
        try:
            response = requests.post(url, json=settings)
            response.raise_for_status()
            print("Konfigurace úspěšně odeslána.")
        except requests.exceptions.RequestException as e:
            print("Chyba při odesílání požadavku:", e)

    def create_leds(self,count=15):
        for i in range(count):
            self.leds.append(LED(i))

    def set_color(self,led_id,rgb):
        self.leds[led_id].color = rgb

    def set_brightness(self,led_id,brightness):
        self.leds[led_id].brightness = brightness

    def  set_state(self,led_id,state):
        self.leds[led_id].state = state

    def settings(self):
        settings = {"seg": []}
        for led in self.leds:
            settings["seg"].append({
                "on":led.state,
                "bri":led.brightness,
                "col":[[led.color[0],led.color[1],led.color[2],0],[],[]],
                "start":led.position,
                "stop": led.position+1
            })
        return settings
    def change_state(self,device_id:int, state: bool, rgb: list, brightness=128 ):
        wled_url = "http://10.10.3.24/json/state"  # Nahraďte <adresa_wled> IP adresou a portem WLED
        for i in range(15):
            self.set_state(i,state)
            self.set_color(i,rgb)
            self.set_brightness(i,brightness)
        self.configure_wled(wled_url,self.settings())

class LED():
    def __init__(self,position) -> None:
        self.state = True
        self.color = [0,0,255]
        self.brightness=128
        self.position = position

manager = LED_manager()
"""
time.sleep(1)
manager.change_state(1,False,[255,0,0])
time.sleep(1)
manager.change_state(1,True,[0,255,0],brightness=255)

time.sleep(1)
manager.change_state(1,True,[0,0,255])
manager.set_color(2,[0,0,0])
manager.change_state(2,True,[255,0,0])
manager.change_state(3,True,[0,255,0])
"""


import sys
import os
import argparse

from spinalcordtoolbox.utils import Metavar, SmartFormatter, init_sct, display_viewer_syntax, printv
from spinalcordtoolbox.image import Image
from spinalcordtoolbox.qmri.mt import compute_mtr

def get_parser():
    parser = argparse.ArgumentParser(
        description='Compute magnetization transfer ratio (MTR). Output is given in percentage.',
        add_help=None,
        formatter_class=SmartFormatter,
        prog=os.path.basename(__file__).strip(".py"))

    mandatoryArguments = parser.add_argument_group("\nMANDATORY ARGUMENTS")
    mandatoryArguments.add_argument(
        '-mt0',
        required=True,
        help='Image without MT pulse (MT0)',
        metavar=Metavar.float,
    )
    mandatoryArguments.add_argument(
        '-mt1',
        required=True,
        help='Image with MT pulse (MT1)',
        metavar=Metavar.float,
    )

    optional = parser.add_argument_group("\nOPTIONAL ARGUMENTS")
    optional.add_argument(
        "-thr",
        type=float,
        help="Threshold to clip MTR output values in case of division by small number. This implies that the output image"
             "range will be [-thr, +thr]. Default: 100.",
        default=100
    )
    optional.add_argument(
        "-h",
        "--help",
        action="help",
        help="Show this help message and exit"
    )
    optional.add_argument(
        '-v',
        type=int,
        choices=(0, 1, 2),
        help='Verbose: 0 = nothing, 1 = classic, 2 = expended',
        default=1
    )
    optional.add_argument(
        '-o',
        help='Path to output file.',
        metavar=Metavar.str,
        default=os.path.join('.', 'mtr.nii.gz')
    )
    return parser

def main():
    # Check input parameters
    parser = get_parser()
    args = parser.parse_args(args=None if sys.argv[1:] else ['--help'])
    fname_mtr = args.o
    verbose = args.v

    # compute MTR
    printv('\nCompute MTR...', verbose)
    nii_mtr = compute_mtr(nii_mt1=Image(args.mt1), nii_mt0=Image(args.mt0), threshold_mtr=args.thr)

    # save MTR file
    nii_mtr.save(fname_mtr, dtype='float32')

    display_viewer_syntax([args.mt0, args.mt1, fname_mtr])

if __name__ == "__main__":
    init_sct()
    main()


from pathlib import Path
import string

data_folder = Path(".").resolve()
type_to_priority = dict(zip(string.ascii_letters, range(1, 53)))

def parse_data(data):
    rucksacks = [[type_to_priority[l] for l in line] for line in data.split("\n")]
    return rucksacks

def find_common_type(rucksack):
    comp_size = len(rucksack) // 2
    common = list(set.intersection(set(rucksack[:comp_size]), set(rucksack[comp_size:])))[0]
    return common

def sum_elf_group_priorities(rucksacks):
    n_rucksacks = len(rucksacks)
    group_size = 3
    badge_priorities = []
    for group_id in range(0, n_rucksacks, group_size):
        group = [set(rucksack) for rucksack in rucksacks[group_id : group_id + group_size]]
        common = list(set.intersection(*group))[0]
        badge_priorities.append(common)
    return sum(badge_priorities)

def main():
    data = data_folder.joinpath("input.txt").read_text()
    rucksacks = parse_data(data)

    print("Part 1")
    priorities_sum = sum([find_common_type(rucksack) for rucksack in rucksacks])
    print(f"The sum of priorities of the common item types in both compartments is {priorities_sum}")

    print("Part 2")
    priorities_sum = sum_elf_group_priorities(rucksacks)
    print(f"The sum of priorities of the common item types for each elf group is {priorities_sum}")

if __name__ == "__main__":
    main()


from __future__ import print_function

import sys
from operator import add

from pyspark import SparkContext

def splitWith(rowString, rowIndex):
    colValue = list(map(int, rowString.split(',')))
    retValue = []

    for colIndex,item in enumerate(colValue):
        retValue.append((colIndex,(rowIndex,item)))

    return retValue

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: matrixVectorMultiply <file>", file=sys.stderr)
        exit(-1)
    sc = SparkContext(appName="matrixVectorMultiply")
    inputFile = sc.textFile(sys.argv[1], 1)
    rowCount,columnCount = map(int,inputFile.first().split(','))
    inputFileWithIndex = inputFile.zipWithIndex()
    matrixInput = inputFileWithIndex.filter(lambda x : x[1] > 0 and x[1] <= rowCount)
    vectorInput = inputFileWithIndex.filter(lambda x : x[1] > rowCount)
    colIndexedVector = vectorInput.map(lambda x : (x[1] - rowCount -1,int(x[0])))
    colIndexedMatrix = matrixInput.flatMap(lambda x: splitWith(x[0],x[1]-1))
    rowIndexedSum = colIndexedMatrix.join(colIndexedVector).map(lambda x: (x[1][0][0], x[1][0][1] * x[1][1])).reduceByKey(add)
    output = rowIndexedSum.sortByKey().map(lambda x: x[1]).collect()
    with open('output.txt', 'w') as outputFile:
        for item in output:
            outputFile.write("{0}\n".format(item))

    sc.stop()


from PyQt5.QtWidgets import QWidget,QLineEdit,QHBoxLayout, QVBoxLayout
from UI.Components.button_container import ButtonContainer #buttonClickNoise
from UI.KeyboardPage.completer import suggestWords

groupedChars = ['abc | def | ghi',
                'jkl | mno | pqr',
                'stu | vwx | yz0',
                '123 | 456 | 789']

groupedChars2 = [['a | b | c','d | e | f','g | h | i'],
                ['j | k | l','m | n | o','p | q | r'],
                ['s | t | u','v | w | x','y | z | 0'],
                ['1 | 2 | 3','4 | 5 | 6','7 | 8 | 9']]
class KeyboardWidget(QWidget):

    def __init__(self, parent):
        super().__init__(parent)
        layout = QVBoxLayout()
        self.setObjectName("Keyboard Page")

        layout.addWidget(self.keyboardWidgetUpper(parent))
        layout.addWidget(self.keyboardWidgetLower(parent))

        self.setLayout(layout)

        # #create auto completer
        # self.completer = AutoCompleter()
        # self.completer.setWidget(self)

    def keyboardWidgetUpper(self,parent):
        keyboardKeys = QWidget()
        layout = QHBoxLayout()
        keyboardKeys.setObjectName("Keyboard Widget")

        buttons = []
        for x in range(4):
            button = ButtonContainer(groupedChars[x], freqName=f"Keyboard {x+1}", checkable=False)
            buttons.append(button)
            layout.addWidget(button)

        buttons[0].clicked.connect(lambda: keyboardClick(parent,buttons, buttons[0]))
        buttons[1].clicked.connect(lambda: keyboardClick(parent,buttons, buttons[1]))
        buttons[2].clicked.connect(lambda: keyboardClick(parent,buttons, buttons[2], prediction=True))
        buttons[3].clicked.connect(lambda: keyboardClick(parent,buttons, buttons[3], prediction=True))

        keyboardKeys.setLayout(layout)
        return keyboardKeys

    def keyboardWidgetLower(self,parent):
        sidebar = QWidget()
        layout = QHBoxLayout()
        buttons = []

        sidebar.setObjectName("low keys")

        toggleBtn = ButtonContainer("Toggle Words", freqName="Word Toggle", checkable=False)
        toggleBtn.setObjectName("Toggle")
        buttons.append(toggleBtn)

        buttons.append(ButtonContainer("Space",freqName="Space",checkable=False))
        buttons.append(ButtonContainer("Backspace",freqName="Backspace",checkable=False))

        for button in buttons:
            layout.addWidget(button)

        buttons[0].clicked.connect(lambda: toggle(parent))
        buttons[1].clicked.connect(lambda: space(parent))
        buttons[2].clicked.connect(lambda: backspace(parent))

        sidebar.setLayout(layout)
        return sidebar

def clickedGroup(parent, buttons, text, level):
    print(level)

    if level == 1:
        nextGroup = groupedChars2[groupedChars.index(text)]

    elif level == 2:
        nextGroup = list(text.split(' | '))
        print(nextGroup)
    print(len(buttons))
    print("length")
    for x in range(len(buttons) - 1):
        print(x)
        buttons[x].label.setText(nextGroup[x])

    buttons[3].label.setText("Back")

def clickedBack(parent, buttons, text, level):
    if level == 2:
        for x in range(len(buttons)):
            buttons[x].label.setText(groupedChars[x])

    elif level == 3:
        currentCharGroup = ""
        for x in range(len(buttons) - 2):
            currentCharGroup +=  buttons[x].label.text()
            currentCharGroup +=  " | "
        currentCharGroup +=  buttons[2].label.text()
        print(currentCharGroup)

        for group in groupedChars2:
            if currentCharGroup in group:
                for x in range(len(buttons)-1):
                    buttons[x].label.setText(groupedChars2[groupedChars2.index(group)][x])
                break

def keyboardClick(parent,buttons,selected,prediction=False):
    #buttonClickNoise()

    toggleBtn = parent.findChild(ButtonContainer,"Toggle")
    btnText = selected.label.text()

    if btnText in groupedChars:
        clickedGroup(parent, buttons, btnText, 1)
    elif any(btnText in subl for subl in groupedChars2):
        clickedGroup(parent, buttons, btnText, 2)
    elif btnText == "Back":
        if any(buttons[0].label.text() in subl for subl in groupedChars2):
            clickedBack(parent, buttons, btnText, 2)
        else:
            clickedBack(parent, buttons, btnText, 3)

    elif prediction == True: # Different button functionality when using GTP3 for prediction

        writePredictionToInput(parent, buttons, btnText, charMode=toggleBtn.label.text() == "Toggle Words")
        suggestWords(parent)
    else:
        writeToInput(parent, buttons, btnText)

def writeToInput(parent, buttons, text):
    inputField = parent.findChild(QLineEdit,"Input")

    prevText = inputField.text()
    if len(text) == 1:
        for x in range(len(buttons)):
            buttons[x].label.setText(groupedChars[x])
    else:
        prevText =  " ".join(inputField.text().split()[0:-1]) + " "
        text = text + " "
    temp = text
    if prevText:
        temp = prevText + text

    inputField.setText(temp)

def writePredictionToInput(parent, buttons, text, charMode):
    inputField = parent.findChild(QLineEdit,"Input")
    prevText = inputField.text()

    # Deletes space between any words and puncuations
    if not text[0].isalpha():
        print("NOT:" + text + "END")
        prevText = prevText.rstrip()

    temp = text

    # If user is typing individual characters
    if charMode == True and len(text) == 1:
        for x in range(len(buttons)):
            buttons[x].label.setText(groupedChars[x])
    else:
        temp += ' '

    inputField.setText(temp)

def backspace(parent):
    #buttonClickNoise()

    inputField = parent.findChild(QLineEdit,"Input")

    temp = inputField.text()

    if len(temp) != 0 :
        inputField.setText(temp[:-1])

def space(parent):
    #buttonClickNoise()

    inputField = parent.findChild(QLineEdit,"Input")

    temp = inputField.text() + " "
    inputField.setText(temp)

def toggle(parent):
    #buttonClickNoise()

    toggleBtn = parent.findChild(ButtonContainer,"Toggle")
    keyboardWidget = parent.findChild(QWidget,"Keyboard Widget")
    keyboardBtns = keyboardWidget.findChildren(ButtonContainer)

    if toggleBtn.label.text() == "Toggle Words":
        toggleBtn.label.setText("Toggle Characters")
        # Request API call for GPT-3
        suggestWords(parent)
        keyLabels = ['word','word','this is a phrase','this is a phrase']

    else:
        toggleBtn.label.setText("Toggle Words")
        keyLabels = groupedChars
        for x in range(len(keyboardBtns)):
            keyboardBtns[x].label.setText(keyLabels[x])


import io
from pathlib import Path

import flask
import numpy as np
import pandas as pd

from .utils import list_dir
from .model import define_model

class ServeConfig:
    OPT_ML_DIR = Path("/opt/ml")
    MODELS_DIR = OPT_ML_DIR / "models"

    ASSETS_PATH = Path("./assets")
    ASSETS_PATH.mkdir(parents=True, exist_ok=True)
    AUDIO_PATH = ASSETS_PATH / "audio"
    AUDIO_PATH.mkdir(parents=True, exist_ok=True)
    META_PATH = ASSETS_PATH / "metadata"
    META_PATH.mkdir(parents=True, exist_ok=True)

class ScoringService(object):
    """
    A singleton for holding the model. This simply loads the model and holds it.
    It has a predict function that does a prediction based on the model and the input data.
    """

    models = None  # Where we keep the model when it's loaded

    @classmethod
    def get_model(cls):
        """Get the model object for this instance, loading it if it's not already loaded."""
        if cls.models == None:
            models = []
            model_list = list_dir(ServeConfig.MODELS_DIR)
            for name in model_list:
                cnn = define_model()
                cnn.load_weights(str(ServeConfig.IN_MODELS_DIR / name))
                models.append(cnn)
            cls.models = models
        return cls.models

    @classmethod
    def predict(cls, input: pd.DataFrame):
        """For the input, do the predictions and return them.

        Args:
            input (a pandas dataframe): The data on which to do the predictions. There will be
                one prediction per row in the dataframe"""
        models = cls.get_model()
        res = np.zeros(input.shape[0], dtype=float)
        input_features = input.values
        for model in models:
            res += model.predict(input_features)

        output = res / len(models)
        return output

app = flask.Flask(__name__)

@app.route("/ping", methods=["GET"])
def ping():
    """Determine if the container is working and healthy. In this sample container, we declare
    it healthy if we can load the model successfully."""
    health = ScoringService.get_model() is not None

    status = 200 if health else 404
    return flask.Response(response="\n", status=status, mimetype="application/json")

@app.route("/invocations", methods=["POST"])
def invocations():
    data = None

    # Convert from CSV to pandas
    if flask.request.content_type == "text/csv":
        data = flask.request.data.decode("utf-8")
        s = io.StringIO(data)
        data = pd.read_csv(s, header=None)
    else:
        return flask.Response(
            response="This predictor only supports CSV data",
            status=415,
            mimetype="text/plain",
        )

    print("Invoked with {} records".format(data.shape[0]))

    # Do the prediction
    predictions = ScoringService.predict(data)

    # Convert from numpy back to CSV
    out = io.StringIO()
    pd.DataFrame({"results": predictions}).to_csv(
        out, header=False, index=False)
    result = out.getvalue()

    return flask.Response(response=result, status=200, mimetype="text/csv")


import numpy as np
import tools
import matplotlib.pyplot as plt
from importlib import reload
from time import sleep
from tqdm import tqdm
import pickle
import sys

def load_obj(name ):
    with open('obj/' + name + '.pkl', 'rb') as f:
        return pickle.load(f)

during = int(sys.argv[1]) # Define the simulation time
existing = int(sys.argv[2]) # Allow to load the right number of years simulations
N = int(sys.argv[3])
Nyears = int(sys.argv[4])
seuil = int(sys.argv[5])

d = load_obj('all_data_{}part_{}years_{}pc'.format(N, existing*Nyears, seuil))

#------------------------------------------
# Loading parameters
N = int(d['position'].shape[1])
Nyears = d['Nyears']
dt = d['dt']
G = 6.67e-11/dt**2

pc = d['pc'] #m
Msun = 1.989e30 #kg
taille = d['taille']
L = d['L']
m = d['m']
nb = d['nb']
rho = np.zeros(((len(d['time']), nb, nb)))
Potential = tools.Potential(G)
Update = tools.Update(G)
#------------------------------------------

def density(X, t, nb) :

    border = np.linspace(-1.5*taille*pc, 1.5*taille*pc, nb)
    dens = np.zeros((len(border), len(border)))

    for i, j in enumerate(border):
        for k, l in enumerate(border):
            if i != nb-1 :
                if k != nb-1 :
                    ind = (X[t, :, 0] > border[i]) & (X[t, :, 0] < border[i+1]) & (X[t, :, 1] > border[k]) & (X[t, :, 1] < border[k+1])
                    dens[i, k] = np.sum(ind)
    return dens

def pos_init(N, r_lim):
    posi = np.zeros((N, 3))
    k=0
    while k < N :
        X = np.random.uniform(-r_lim, r_lim, (1, 3))

        #print(X)
        r = np.sqrt(X[0, 0]**2 + X[0, 1]**2 + X[0, 2]**2)
        if r < r_lim :
            #print(k)
            posi[k] = X
            k+=1
    return posi

old_pos = d['position']
old_vit = d['velocity']
old_rho = d['density']

new_pos = np.zeros(((during*d['position'].shape[0], N, 3)))
new_vit = np.zeros(((during*d['time'].shape[0], N, 3)))
new_rho = np.zeros(((during*len(d['time']), nb, nb)))

new_pos[:d['position'].shape[0]] = old_pos
new_vit[:d['velocity'].shape[0]] = old_vit
new_rho[:d['density'].shape[0]] = old_rho
#new_tab_pos = np.delete(new_tab_pos, len(d['time']), axis = 0)
#new_tab_vit = np.delete(new_tab_vit, len(d['time']), axis = 0)

time_array = np.arange(0, during*len(d['time'])*dt, dt)
#new_time = np.concatenate((d['time'], time_array), axis = 0)
#new_time = np.delete(new_time, len(d['time']))

print("\n\n======================== \n Computing trajectories \n======================== \n \n")

for t in tqdm(range(len(d['time']), len(time_array))) :
    #print(t)
    if t != len(time_array)-1:
        #print('computing new positions!')
        for i in range(N):
            r, ind, ax, ay, az = Update.update_acc(new_pos, m, t-1, i, seuil)

            if t == len(d['time']):
                new_pos[t, i, 0], new_vit[t, i, 0] = Update.first_step(new_pos[t-1, i, 0], new_vit[t-1, i, 0], ax, dt)
                new_pos[t, i, 1], new_vit[t, i, 1] = Update.first_step(new_pos[t-1, i, 1], new_vit[t-1, i, 1], ay, dt)
                new_pos[t, i, 2], new_vit[t, i, 2] = Update.first_step(new_pos[t-1, i, 2], new_vit[t-1, i, 2], az, dt)

            else:
                new_pos[t, i, 0] = Update.verlet(new_pos[t-1, i, 0], new_pos[t-2, i, 0], ax, dt)
                new_pos[t, i, 1] = Update.verlet(new_pos[t-1, i, 1], new_pos[t-2, i, 1], ay, dt)
                new_pos[t, i, 2] = Update.verlet(new_pos[t-1, i, 2], new_pos[t-2, i, 2], az, dt)

                new_vit[t, i, 0] = (new_pos[t-1, i, 0] - new_pos[t-2, i, 0])/dt
                new_vit[t, i, 1] = (new_pos[t-1, i, 1] - new_pos[t-2, i, 1])/dt
                new_vit[t, i, 2] = (new_pos[t-1, i, 2] - new_pos[t-2, i, 2])/dt

    # Last time step
    elif t == len(time_array)-1 :
        #print("last")
        for i in range(N):
            new_pos[t, i, 0] = new_pos[t-1, i, 0]
            new_pos[t, i, 1] = new_pos[t-1, i, 1]
            new_pos[t, i, 2] = new_pos[t-1, i, 2]

            new_vit[t, i, 0] = new_vit[t-1, i, 0]
            new_vit[t, i, 1] = new_vit[t-1, i, 1]
            new_vit[t, i, 2] = new_vit[t-1, i, 2]
    else:
        pass
        #print('No computing!')

    #old_pos = np.concatenate((old_pos, new_pos), axis = 0)
    #old_vit = np.concatenate((old_vit, new_vit), axis = 0)

print("\n\n======================== \n Computing density \n======================== \n \n")

for t in tqdm(range(len(d['time']), len(time_array))):
    if t >= len(d['time']):
        new_rho[t] = density(new_pos, t, nb)

new_dict = {
    'position' : new_pos,
    'velocity' : new_vit,
    'density' : new_rho,
    'taille' : taille,
    'L' : L,
    'pc' : pc,
    'Nyears' : Nyears,
    'dt' : dt,
    'seuil' : seuil,
    'nb' : nb,
    'time' : time_array,
    'm' : m}

def save_obj(obj, name ):
    with open('obj/'+ name + '.pkl', 'wb') as f:
        pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)

save_obj(new_dict, 'all_data_{}part_{}years_{}pc'.format(N, during*existing*Nyears, seuil))


from .transformer import Transformer
from musikla.core import Voice
from musikla.core.events import MusicEvent, NoteEvent, RestEvent, ChordEvent
from typing import List, Optional

class SlidingAverage():
    def __init__ ( self, capacity : int = 0 ):
        self.history : List[float] = []
        self.capacity = capacity
        self.sum : float = 0
        self.count : int = 0

    def __iadd__ ( self, value : float ):
        if self.count == 0:
            self.sum = value
            self.count = 1

            if self.capacity > 0:
                self.history.append( value )
        else:
            #  ( ( self.average / self.count ) + ( value / ( self.count + 1 ) ) )
            self.sum += value

            if self.capacity == 0 or self.count < self.capacity:
                self.count += 1
            else:
                self.sum -= self.history.pop( 0 )

            if self.capacity > 0:
                self.history.append( value )

        return self

    def __int__ ( self ):
        if self.count == 0:
            return 0

        return int( self.sum / self.count )

    def __float__ ( self ):
        if self.count == 0:
            return 0

        return float( self.sum / self.count )

class VoiceIdentifierVoice():
    def __init__ ( self, transformer : 'VoiceIdentifierTransformer', parent : Voice, index : int, staff : int = None ):
        self.transformer : VoiceIdentifierTransformer = transformer
        self.parent_name : str = parent.name
        self.parent_staff : Optional[int] = staff
        self.voice : Voice = parent.clone( parent.name + '/' + str( index ) )
        self.index = index
        self.average_pitch : SlidingAverage = SlidingAverage()
        self.auto_create_rests : bool = True
        self.auto_create_end_rests : bool = True
        self.last_event : Optional[MusicEvent] = None

    @property
    def last_timestamp ( self ):
        if self.last_event is None:
            return 0

        return self.last_event.timestamp

    @property
    def last_end_timestamp ( self ):
        if not self.last_event:
            return 0

        return self.last_event.end_timestamp

    def is_busy_at ( self, timestamp : int ) -> bool:
        return self.last_end_timestamp > timestamp

    def is_connected_at ( self, timestamp : int ) -> bool:
        return abs( timestamp - self.last_end_timestamp ) <= 3

    def distance ( self, event : NoteEvent ) -> float:
        return abs( int( event ) - int( self.average_pitch ) )

    def create_rest ( self, target : int, visible : bool = True ) -> RestEvent:
        duration = target - self.last_end_timestamp

        value = self.voice.from_duration_absolute( duration )

        return RestEvent(
            timestamp = self.last_end_timestamp,
            visible = visible,
            duration = duration,
            value = value,
            voice = self.voice,
            staff = self.parent_staff
        )

    def append ( self, *events : MusicEvent ):
        for event in events:
            if isinstance( event, NoteEvent ) or isinstance( event, ChordEvent ):
                self.average_pitch += int( event ) if isinstance( event, NoteEvent ) \
                                else  sum( event.pitches ) / len( event.pitches )

                # TODO Allow setting a minimum rest duration. If the empty space between the events is less than
                # said minimum, then no rest is created
                if self.auto_create_rests and self.last_end_timestamp < event.timestamp - 1:
                    self.transformer.add_output( self.create_rest( event.timestamp ) )

            self.transformer.add_output( event.clone( voice = self.voice ) )

            self.last_event = event

class VoiceIdentifierTransformer(Transformer):
    """
    This class receives a flat, ordered stream of musical events and splits them in multiple voices and identifies parallel notes/chords
    """
    def __init__ ( self, auto_create_rests : bool = True, auto_create_end_rests : bool = True ):
        super().__init__()

        self.voices : List[VoiceIdentifierVoice] = []

        self.auto_create_rests : bool = auto_create_rests

        self.auto_create_end_rests : bool = auto_create_end_rests

    def create_voice_for ( self, event : NoteEvent ) -> VoiceIdentifierVoice:
        index = max( ( voice.index for voice in self.voices if voice.parent_name == event.voice.name ), default = 0 )

        voice = VoiceIdentifierVoice( self, event.voice, index + 1, event.staff )

        voice.auto_create_rests = self.auto_create_rests
        voice.auto_create_end_rests = self.auto_create_end_rests

        self.voices.append( voice )

        return voice

    def find_voice_for ( self, event, auto_create : bool = True ) -> Optional[VoiceIdentifierVoice]:
        best_voice, best_voice_dst = None, None

        for voice in self.voices:
            if voice.parent_name != event.voice.name or voice.parent_staff != event.staff:
                continue

            # If the voice already has a sound playing at this timestamp
            if voice.is_busy_at( event.timestamp ):
                continue

            if event.staff is not None:
                return voice

            voice_dst = voice.distance( event )

            if best_voice is None or best_voice_dst > voice_dst:
                best_voice = voice
                best_voice_dst = voice_dst

        if best_voice is None and auto_create:
            best_voice = self.create_voice_for( event )

        return best_voice

    def find_voice_for_rest ( self, event, auto_create : bool = True ) -> VoiceIdentifierVoice:
        for voice in self.voices:
            if voice.parent_name != event.voice.name or voice.parent_staff != event.staff:
                continue

            # If the voice already has a sound playing at this timestamp
            if voice.is_connected_at( event.timestamp ):
                return voice

        return self.create_voice_for( event )

    def register_rest ( self, event : RestEvent ):
        voice = self.find_voice_for_rest( event )

        if voice is not None:
            voice.append( event )

    def register_note ( self, event : NoteEvent ):
        voice = self.find_voice_for( event )

        if voice is not None:
            voice.append( event )

    def register_chord ( self, event : ChordEvent ):
        voice = self.find_voice_for( event )

        if voice is not None:
            voice.append( event )

    def transform ( self ):
        """
        For now, NoteOn/NoteOff events will be ignored.
        Use the compose transformer before passing events into this notation builder.
        This might change in the future.
        """
        while True:
            done, event = yield

            if done: break

            if isinstance( event, NoteEvent ):
                self.register_note( event )
            elif isinstance( event, RestEvent ):
                self.register_rest( event )
            elif isinstance( event, ChordEvent ):
                self.register_chord( event )
            else:
                self.add_output( event )

        if self.auto_create_end_rests and self.voices:
            last_voice_timestamp : int = max( [ voice.last_end_timestamp for voice in self.voices ] )

            for voice in self.voices:
                if voice.last_end_timestamp < last_voice_timestamp:
                    self.add_output( voice.create_rest( last_voice_timestamp, visible = False ) )


import os
import sys
import importlib
import datetime
import time
import shutil
import numpy as np
import pandas as pd
from pathlib import Path

from evidence.post_processing import postprocess

# UltraNest imports
try:
    from ultranest import ReactiveNestedSampler
    import ultranest.stepsampler
except ImportError:
    raise ImportError("Install UltraNest to use this module.")

# MPI imports
try:
    from mpi4py import MPI
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    size = comm.Get_size()
except ImportError:
    print("Install mpi4py to use with OpenMPI. Continuing with one thread.")
    rank = 0
    size = 1

def run(model, rundict, priordict, ultrasettings=None):
    """
    Runs UltraNest on the chosen data and model. When UltraNest is finished
    running it automatically runs a post processing script on the output creating
    plots of the posterior and saving basic information like the evidence and
    run settings on a text file for future reference.

    Parameters
    ----------
    model : object
        Custom model class that contains your desired model. This class has to
        have a method called log_likelihood(x) which takes a parameter array x
        and returns the corresponding log Likelihood. The order of the array x is
        given by the order that results from calling list(priordict.keys()) (see
        description for priordict for more information.)
        Your custom model class should inherit from either RVModel if it's a
        radial velocities model or from BaseModel otherwise.
    rundict : dict
        Dictionary with basic information about the run itself. Keys it should
        include are:
            target : Name of target or function to analyse
            runid : String to identify the specific model or configuration being
                    used
            comment (optional) : Optional comment for a third layer of identification
            prior_names (optional) : List with the names and ranges of the priors
            nplanets (optional) : Number of planets in the RV model. If not
                                  provided, it will use the number of planets
                                  present in the configfile.
            star_params (optional) : Dictionary with the stars parameters like
                                     star_mass (mass of the star in solar masses),
                                     star_radius (radius of star in solar radii),
                                     star_rot (roation period of star). These
                                     can be be ufloats to include the uncertainty.
            savedir (optional) : Save directory for the output of UltraNest
            order_planets (optionsl) : Boolean indicating if the planets should
                                       be ordered by period. This is to avoid
                                       periods jumping between the different
                                       planets. Default is False.
    priordict : dict
        Dictionary with the priors to all free parameters. Keys are the names
        of the parameters. Values are object with a method .ppf(x) which is the
        inverse of the CDF of the chosen probability distribution of the prior.
        It takes a uniformly sampled number between 0 and 1 and returns the
        physical parameter distributed according to the prior.
        The method log_likelihood in your custom model should take the same order
        or parameters that results from calling list(priordict.keys()).
    ultrasettings : dict, optional
        Dictionary containing custom parameters for UltraNest setting like nlive
        or nrepeats. If None are given the defualt UltraNest settings are used.

    Returns
    -------
    output : Output object
        Object with the all sorts of outputs from the Nested Sampling run.
        Very similar in structure to the output from PolyChord but without all
        custom PolyChord objects.
        Several attributes are added before returning. These are used for the
        post processing script.
    """

    # Create list of parameter names
    parnames = model.parnames
    rundict_keys = list(rundict.keys())

    # Count "real" number of planets if not provided by rundict or model
    nplanets = 0
    if 'nplanets' in rundict_keys:
        # Check if nplanets is in rundict
        nplanets = rundict['nplanets']
    elif hasattr(model, 'nplanets'):
        # Check if nplanets is in the model
        nplanets = model.nplanets
    else:
        # Count number of planets by checking for periods
        for i, par in enumerate(parnames):
            if ('planet' in par) and ('period' in par):
                nplanets += 1

    planets = []
    planet_idxs = []
    for n in range(1, nplanets+1):
        planets.append([])
        for i, par in enumerate(parnames):
            if f'planet{n}' in par:
                planets[n-1].append(i)
                if 'period' in par:
                    planet_idxs.append(i)

    # Prepare run
    nderived = 0
    ndim = len(parnames)

    # Function to convert from hypercube to physical parameter space
    def prior(hypercube):
        """
        Converts a point in the unit hypercube to the physical parameters using
        their respective priors.
        """

        # Claculate physical parameters with ppf from prior
        theta = np.ones_like(hypercube)
        for i, x in enumerate(range(ndim)):
            param = parnames[i]
            theta[x] = priordict[param].ppf(hypercube[i])

        return theta

    # LogLikelihood

    def loglike(x):
        """
        Calculates de logarithm of the Likelihood given the parameter vector x.
        """

        return model.log_likelihood(x)

    # Starting time to identify this specific run
    # If it's being run with more than one core the isodate on the first core
    # is broadcasted to the rest so they all share the same variable
    isodate = datetime.datetime.today().isoformat()
    if size > 1:
        isodate = comm.bcast(isodate, root=0)

    # Create settings object for this run
    settings = set_ultrasettings(rundict, ultrasettings, ndim, nderived, isodate, parnames)

    # Find and indicate wrapped (circular) parameters like phases
    wrapped_params = np.zeros(ndim, dtype=bool)
    for i, par in enumerate(parnames):
        if 'omega' in par or 'ml0' in par:
            wrapped_params[i] = True

    # Set up the sampler
    sampler = ReactiveNestedSampler(parnames, loglike, prior,
                                    log_dir = settings['log_dir'],
                                    num_test_samples = 100,
                                    wrapped_params = wrapped_params,
                                    num_bootstraps = settings['num_bootstraps'],
                                     #resume=False,
                                    # vectorized = True
                                    )

    # Use a slice sampler instead of rejection sampling
    sampler.stepsampler = ultranest.stepsampler.RegionSliceSampler(nsteps=settings['nsteps'], adaptive_nsteps='move-distance')

    # Initialise clocks
    ti = time.process_time()

    # ----- Run UltraNest ------
    sampler.run(min_num_live_points = settings['nlive'],
                cluster_num_live_points = int(0.1*settings['nlive']),
                dlogz = settings['dlogz'],
                frac_remain = settings['frac_remain']
                )
    # --------------------------

    # Stop clocks
    tf = time.process_time()

    if size > 1:
        # Reduce clocks to min and max to get actual wall time
        ti = comm.reduce(ti, op=MPI.MIN, root=0)
        tf = comm.reduce(tf, op=MPI.MAX, root=0)

    # Print General results
    sampler.print_results()

    # Save results
    if rank == 0:
        output = Output()
        # Assign additional parameters to output
        output.runtime = datetime.timedelta(seconds=tf-ti)
        output.rundict = rundict.copy()
        output.datadict = dict(model.datadict)
        output.fixedpardict = dict(model.fixedpardict)
        output.model_name = str(model.model_path.stem)
        output.nlive = settings['nlive']
        output.nrepeats = settings['nsteps']
        output.isodate = isodate
        output.ncores = size
        output.parnames = parnames
        output.ndim = ndim
        output.sampler = 'UltraNest'
        output.base_dir = settings['log_dir']
        output.file_root = settings['file_root']
        output.logZ = sampler.results['logz']
        output.logZerr = sampler.results['logzerr']
        output.nlike = sampler.results['ncall']

        output.samples = pd.DataFrame(sampler.results['samples'], columns=parnames)

        # Add additional information if provided
        if 'prior_names' in rundict_keys:
            output.priors = rundict['prior_names']

        if 'star_params' in rundict_keys:
            output.starparams = rundict['star_params']

        # Print run time
        print(f'\nTotal run time was: {output.runtime}')

        # Plot results
        sampler.plot()

        base_dir_parent = str(Path(output.base_dir).parent.absolute())
        runid_dir = Path(output.base_dir).parent.parent.absolute()
        # Save model file
        shutil.copy(model.model_path, base_dir_parent)
        # Save output as pickle file
        # print(base_dir_parent)
        # run_label = os.path.basename(base_dir_parent)
        # print(run_label)
        dump2pickle(output, output.file_root+'.pkl')

        # Copy post processing script to this run's folder
        parent = Path(__file__).parent.parent.absolute()
        shutil.copy(os.path.join(parent, 'post_processing.py'), base_dir_parent)
        # Copy FIP criterion scirpt to parent of runid
        shutil.copy(os.path.join(parent, 'fip_criterion.py'), runid_dir)

        # Copy model file
        shutil.copy(model.model_path, base_dir_parent)

        # Run post processing script
        postprocess(base_dir_parent)

    return #output

def dump2pickle(output, filename, savedir=None):
    """
    Takes the output from UltraNest and saves it as a pickle file.

    Parameters
    ----------
    output : Output object
        Object file with the output infos from the UltraNest run
    filename : str
        Name of the saved file
    savedir : str, optional
        Directory on where to save the pickled file
    """

    # Try to import pickle. Raise warning if not installed.
    try:
        import pickle
    except ImportError:
        print('Install pickle to save the output. Try running:\n pip install pickle')
        return

    if savedir is None:
        # Save directory in parent of base dir
        pickledir = Path(output.base_dir).parent
    else:
        # Unless specified otherwhise
        pickledir = savedir

    # Create directory if it doesn't exist.
    os.makedirs(pickledir, exist_ok=True)

    full_path = os.path.join(pickledir, filename)
    with open(full_path, 'wb') as f:
        pickle.dump(output, f)

    return

def set_ultrasettings(rundict, ultrasettings, ndim, nderived, isodate, parnames):
    """
    Sets the correct settings for UltraNest and returns a dictionary with the
    settings. It combines the default settings and overwrites the used defined
    settings. It cointains the settings for both the initialization of the
    samples and the its run function.

    Parameters
    ----------
    rundict : dict
        Dictionary with information about the run itself.
    polysettings : dict
        Dictionary with the custom UltraNest settings to be set for this run
    ndim : int
        Number of free parameters
    nderived : int
        Number of derived parameters
    isodate : datetime
        Date stamp to identify the current run
    parnames : list
        List containing the names of the free parameters

    Returns
    -------
    settings : dict
        Object with all the information that UltraNest needs to run nested
        sampling on this model.
    """

    rundict_keys = list(rundict.keys())
    # Define UltraNest settings
    # Use the settings provided in ultrasettings otherwise use default
    # Definition of default values for the UltraNest settings
    default_settings = {'nlive': 25*ndim,
                        'nsteps': 3*ndim,
                        'dlogz': 0.5,
                        'frac_remain': 0.01,
                        'num_bootstraps': 30
                        }

    # Update default values with settings provided by user
    if ultrasettings != None:
        if type(ultrasettings) is not dict:
            raise TypeError("ultrasettings has to be a dictionary")
        else:
            default_settings.update(ultrasettings)

    # Define fileroot name (identifies this specific run)
    rundict['target'] = rundict['target'].replace(' ', '')  # Remove any whitespace
    rundict['runid'] = rundict['runid'].replace(' ', '')  # Remove any whitespace
    file_root = rundict['target']+'_'+rundict['runid']

    # Add comment if it exists and is not empty
    if 'comment' in rundict_keys:
        if rundict['comment'] != '':
            file_root += '-' + rundict['comment']

    # Add number of planets if it exists
    if 'nplanets' in rundict_keys:
        if rundict['nplanets'] is not None:
            file_root += f'_k{rundict["nplanets"]}'

    # Check if there is a drift and add "d{n}" to file name
    drift_order = 0
    for par in parnames:
        if 'drift' in par:
            if par[6:] in ['lin', 'quad', 'cub', 'quar']:
                drift_order += 1
    if drift_order > 0:
        file_root += f'_d{drift_order}'

    # Label the run with nr of planets, live points, nr of cores, sampler and date
    file_root += f'_nlive{default_settings["nlive"]}'
    file_root += f'_ncores{size}'
    file_root += '_ultranest'
    file_root += '_'+isodate

    # Base directory
    # Check if a save directory was provided
    if 'save_dir' in rundict_keys:
        save_dir = rundict['save_dir']
    else:
        save_dir = ''
    base_dir = os.path.join(save_dir, file_root, 'ultraresults')

    # Update settings dictionary with fileroot and basedir
    default_settings.update({'log_dir': base_dir, 'file_root': file_root})

    return default_settings

class Output:
    pass


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

# Load the cleaned data
data = pd.read_csv("C:\\Users\\wamm1\\Desktop\\midterm\\clean_pop.csv")

# List of countries for visualization
countries = ['Canada', 'India', 'Kenya', 'Brazil', 'Ukraine']

# Filter data for these countries
selected_countries = data[data['country'].isin(countries)]

# Plot
plt.figure(figsize=(12, 7))
sns.lineplot(data=selected_countries, x='year', y='Infant Mortality Rate', hue='country')
plt.title('Infant Mortality Rate Over the Years')
plt.ylabel('Infant Mortality Rate')
plt.xlabel('Year')
plt.legend(title='Country')
plt.show()


import collections
import math
from collections import deque

class Graph:
    def __init__(self):
        self.vertices = set()
        # makes the default value for all vertices an empty list
        self.edges = collections.defaultdict(list)
        self.weights = {}

    def add_vertex(self, value):
        self.vertices.add(value)

    def add_edge(self, from_vertex, to_vertex, distance):
        if from_vertex == to_vertex: pass
        self.edges[from_vertex].append(to_vertex)
        self.weights[(from_vertex, to_vertex)] = distance

    def __str__(self):
        string = "Vertices: " + str(self.vertices) + "\n"
        string += "Edges: " + str(self.edges) + "\n"
        string += "Weights: " + str(self.weights)
        return string

def dijkstra(graph, start):
    # initializations
    S = set()

    # lenght_of_shortest_path represents the length shortest distance paths from start to edge, for edge in lenght_of_shortest_path.
    #initialize every vertex with path of infinity
    lenght_of_shortest_path = dict.fromkeys(list(graph.vertices), math.inf)
    previous = dict.fromkeys(list(graph.vertices), None)

    # then we set the path length of the start vertex to 0
    lenght_of_shortest_path[start] = 0

    # while there exists a vertex v not in S
    while S != graph.vertices:
        # let v be the closest vertex that has not been visited,it will be 'start'
        v = min((set(lenght_of_shortest_path.keys()) - S), key=lenght_of_shortest_path.get)

        # for each neighbor of v not in S
        for neighbor in set(graph.edges[v]) - S:
            new_path = lenght_of_shortest_path[v] + graph.weights[v, neighbor]

            # check new path length with current
            if new_path < lenght_of_shortest_path[neighbor]:
                # since it's optimal, update the shortest path for neighbor
                lenght_of_shortest_path[neighbor] = new_path

                # set the previous vertex of neighbor to v
                previous[neighbor] = v

        S.add(v)

    return lenght_of_shortest_path, previous

def shortest_path(graph, start, end):
    visited, paths = dijkstra(graph, start)
    full_path = deque()
    _destination = paths[end]

    #while end point is not start point append destination to path
    while _destination != start:
        full_path.appendleft(_destination)
        _destination = paths[_destination]

    full_path.appendleft(start)
    full_path.append(end)

    #return shortest way
    return visited[end], list(full_path)

if __name__ == '__main__':

    graph = Graph()
    with open('input.txt') as read_file:
        n = int(read_file.readline())
        for i in range(n):
            node_list = read_file.readline().split()
            graph.add_vertex(node_list[0])
            graph.add_vertex(node_list[1])
            graph.add_edge(node_list[0], node_list[1], int(node_list[2]))
        points = read_file.readline().split()
        start_point = points[0]
        end_point = points[1]
        print( shortest_path(graph, start_point, end_point))


import argparse
import ast
import io
import os.path
import shlex
import shutil
import subprocess
import sys
import textwrap
import urllib.parse

def get_url_filename(url, suffix):
    filename = urllib.parse.urlparse(url).path
    filename = filename.split('/')[-1]
    if not filename.endswith(suffix):
        raise Exception("%r filename doesn't end with %r suffix; url=%r"
                        % (filename, suffix, url))
    return filename

def add_version(package_name, version):
    if version is not None:
        return '%s==%s' % (package_name, version)
    else:
        return package_name

class CI:
    def __init__(self):
        self.source_dir = os.path.abspath(os.path.dirname(__file__))
        self.root_dir = os.path.abspath('work')
        self.set_task_dir()

        self.python_warnings = []
        # self.python_warnings.append('error')
        self.python_dev_mode = True
        # self.python_bytes_warnings = None

        # 2 stands for '-bb'
        self.python_bytes_warnings = 0
        self.read_package_versions()

        if self.python_bytes_warnings and self.python_bytes_warnings > 1:
            self.python_warnings.append('error::BytesWarning')

        self.env = self.create_environ()
        self.python_options = []
        if self.python_bytes_warnings:
            self.python_options.append('-' + 'b' * self.python_bytes_warnings)
        if self.python_dev_mode:
            self.python_options.extend(['-X', 'dev'])
        for warn in self.python_warnings:
            self.python_options.append('-W%s' % warn)

        self._python_version = None
        self._python_version_str = None
        self._orig_python_args = None
        self.set_python(sys.executable)

    def create_environ(self):
        env = dict(os.environ)
        for key in list(env):
            if key.startswith('PYTHON'):
                del env[key]
        if self.python_dev_mode:
            env['PYTHONDEVMODE'] = '1'
        if self.python_warnings:
            env['PYTHONWARNINGS'] = ','.join(self.python_warnings)
        return env

    def log(self, message):
        print(message)

    def mkdir(self, path):
        if os.path.exists(path):
            return
        self.log("Create directory: %s" % path)
        os.mkdir(path)

    def set_python(self, python):
        python = os.path.abspath(python)
        self.python_args = [python] + self.python_options
        if self._orig_python_args is None:
            self._orig_python_args = self.python_args

    def run_command(self, args, quiet=False, **kw):
        cmd_str = ' '.join(shlex.quote(arg) for arg in args)
        cmd_str = cmd_str.replace("\n", "\\n")
        if not quiet:
            text = "Run command: %s" % cmd_str
            if 'cwd' in kw:
                text += ' in %s' % kw['cwd']
            self.log(text)
        if 'stdin' not in kw:
            kw['stdin'] = subprocess.DEVNULL
        if 'env' in kw:
            raise NotImplementedError("cannot override env")
        kw['env'] = self.env

        proc = subprocess.run(args, **kw)

        exitcode = proc.returncode
        if exitcode:
            raise Exception("%s command failed with exit code %s"
                            % (cmd_str, exitcode))

        return proc

    def run_python(self, args, **kw):
        return self.run_command(self.python_args + args, **kw)

    def patch(self, filename, directory=os.path.curdir, level=1):
        filename = os.path.join(self.source_dir, 'patches', filename)
        with open(filename) as fp:
            # --force: do not ask any questions
            self.run_command(['patch', '-p%s' % level, '--force'],
                             stdin=fp,
                             cwd=directory)

    def read_package_versions(self):
        # parse requirements.txt
        self.package_versions = {}
        filename = os.path.join(self.source_dir, 'requirements.txt')
        with io.open(filename, encoding='utf8') as fp:
            for line in fp:
                # strip comments
                line = line.split('#', maxsplit=1)[0]
                line = line.rstrip()
                if '==' in line:
                    name, version = line.split('==', 1)
                else:
                    name = line
                    version = None
                self.package_versions[name] = version

    def pip_install_update(self, packages):
        if not packages:
            raise ValueError
        args = ["-m", "pip", "install", "--upgrade"]
        for name in packages:
            # don't pin the version with --dev
            if not self.args.dev:
                try:
                    version = self.package_versions[name]
                except KeyError:
                    raise Exception("unversionned package: %r" % name)
                arg = add_version(name, version)
            args.append(arg)
        return self.run_python(args)

    def get_python_version(self):
        if self._python_version is not None:
            return self._python_version

        code = "import sys; print(sys.version_info[:3])"
        proc = self.run_python(["-c", code],
                               stdout=subprocess.PIPE,
                               universal_newlines=True,
                               quiet=True)
        line = proc.stdout.rstrip()
        self._python_version = ast.literal_eval(line)
        return self._python_version

    def get_python_version_str(self):
        # Get the Python version string including the Python implementation
        # name. Example: 'cpython-3.8.0b4'
        if self._python_version_str is not None:
            return self._python_version_str

        code = textwrap.dedent("""
            import sys

            if hasattr(sys, 'implementation'):
                name = sys.implementation.name
            else:
                import platform
                name = platform.python_implementation()
            name = name.lower()

            version = sys.version.split()[0]

            print("%s-%s" % (name, version))
        """)

        proc = self.run_python(["-c", code],
                               stdout=subprocess.PIPE,
                               universal_newlines=True,
                               quiet=True)
        self._python_version_str = proc.stdout.rstrip()
        return self._python_version_str

    def download(self, url, filename):
        self.mkdir(self.download_dir)

        filename = os.path.basename(filename)
        filename = os.path.join(self.download_dir, filename)

        # already downloaded: do nothing
        if os.path.exists(filename):
            return filename

        try:
            self.run_command(["wget", "-O", filename, url])
        except:  # noqa
            self.unlink(filename)
            raise

        return filename

        # FIXME: validate a checksum?

    def create_empty_file(self, filename):
        open(filename, 'wb', 0).close()

    def unlink(self, filename):
        if not os.path.exists(filename):
            return
        self.log("Remove file: %s" % filename)
        os.unlink(filename)

    def rmtree(self, dirname):
        if not os.path.exists(dirname):
            return
        self.log("Remove directory: %s" % dirname)
        shutil.rmtree(dirname)

    def package_directory(self, name):
        # Create an absolute path
        # Example: "/path/to/numpy-1.17.2"
        version = self.package_versions[name]
        dirname = "%s-%s" % (name, version)
        return os.path.join(self.task_dir, dirname)

    def task_directory_name(self, name):
        # Example: "cpython-3.8_numpy-1.16.2"
        python = self.get_python_version_str()
        version = self.package_versions[name]
        dirname = "%s_%s-%s" % (python, name, version)
        if self.args.dev:
            dirname += "-dev"
        return dirname

    def download_extract_zip(self, url, dirname):
        filename = get_url_filename(url, '.zip')
        filename = self.download(url, filename)

        self.rmtree(dirname)
        self.run_command(["unzip", "-d", self.task_dir, filename])

    def download_extract_tarball(self, url, dirname):
        filename = get_url_filename(url, '.tar.gz')
        filename = self.download(url, filename)

        self.rmtree(dirname)
        self.run_command(["tar", "-xf", filename])

    def apply_patches(self, patches, dirname):
        for filename in patches:
            self.patch(filename, dirname)

    def setup_venv(self):
        create_venv = not os.path.exists(self.venv_dir)

        if create_venv:
            try:
                self.run_python(["-m", "venv", self.venv_dir])
            except:   # noqa
                self.rmtree(self.venv_dir)
                raise
        else:
            self.log("venv already exists: %s" % self.venv_dir)

        self.set_python(os.path.join(self.venv_dir, "bin", "python"))

        if create_venv:
            self.pip_install_update(["setuptools", "pip"])

    def get_tasks(self):
        task_dir = os.path.join(self.source_dir, 'task')
        names = [name[:-3] for name in os.listdir(task_dir)
                 if not name.startswith("__init__.") and name.endswith(".py")]
        return names

    def parse_options(self):
        tasks = self.get_tasks()
        parser = argparse.ArgumentParser(description='Process some integers.')
        parser.add_argument('command',
                            choices='install test clean cleanall'.split())
        parser.add_argument('task',
                            choices=sorted(tasks))
        parser.add_argument('--dev', action="store_true")
        self.args = parser.parse_args()

    def set_task_dir(self, name=None):
        if name:
            self.task_dir = os.path.join(self.root_dir, name)
        else:
            self.task_dir = self.root_dir
        self.download_dir = os.path.join(self.root_dir, 'download')
        self.venv_dir = os.path.join(self.task_dir, 'venv')

    def chdir(self, path):
        self.log("Change directory: %s" % path)
        os.chdir(path)

    def setup_env(self):
        self.mkdir(self.root_dir)
        self.mkdir(self.task_dir)
        self.chdir(self.task_dir)
        self.setup_venv()

    def _get_task(self):
        task_name = self.args.task

        modname = 'pythonci.task.' + task_name
        mod = __import__(modname).task
        mod = getattr(mod, task_name)
        task_class = mod.Task
        return task_class(self)

    def patch_tox_basepython(self):
        # rely on the current working directory
        filename = 'tox.ini'

        with open(filename, encoding="utf-8") as fp:
            content = fp.read()

        # Don't pass arguments, only the executable
        # Don't use the Python of the venv, but the original Python
        #python = self._orig_python_args[0]
        python = self.python_args[0]
        line = f"basepython = {python}\n"

        testenv = "[testenv]\n"
        pos = content.find(testenv)
        if pos:
            pos += len(testenv)
            content = content[:pos] + line + content[pos:]
        else:
            content = f"{testenv}{line}" + content

        with open(filename, "w", encoding="utf-8") as fp:
            fp.write(content)
            fp.flush()

        self.log(f"basepython overriden in {filename}")

    def main(self):
        self.parse_options()
        if self.args.command == 'cleanall':
            self.rmtree(self.root_dir)
            return

        task = self._get_task()

        command = self.args.command
        if command == 'clean':
            self.rmtree(self.task_dir)
        else:
            if command == 'install':
                task.install()
            elif command == 'test':
                task.run_tests()
            else:
                raise Exception("unknown command: %r" % command)

    def remove_cython_files(self):
        # Force to run Cython: regenerate C files generated by Cython
        # in the current directory and subdirectories.
        cmd = r"rm -f -v $(grep -rl '/\* Generated by Cython') PKG-INFO"
        self.run_command([cmd], shell=True)

    def install_cython(self):
        if self.args.dev:
            url = "git+git://github.com/cython/cython.git@0.29x"
            self.run_python(["-m", "pip", "install", url])
        else:
            self.pip_install_update(["Cython"])


from reportlab.pdfgen.canvas import Canvas
from PollyReports import *
from testdata import data
import sqlite3
import os
from tkinter import messagebox
import webbrowser as vb
# ================== Variables=======
Qty_list = []
Price_list = []
Qty_sum=0
Price_sum=0
# ==========SQL connection============
con=sqlite3.connect(r"ims.db")
cur=con.cursor()

try:
    cur.execute("Select * from products")
    data=cur.fetchall()
    for row in data:
        Qty_list.append(int(row[5]))
        Price_list.append(int(row[6]))

    for x in Qty_list:
        Qty_sum=Qty_sum + x

    for x in Price_list:
        Price_sum=Price_sum + x
    rpt = Report(data)
# =============== Header ================
    rpt.pageheader = Band([
    Element((36, 0), ("Times-Bold", 20),
        text = "Page Header"),
    # category text,supplier text,name text,price text,qty text,Pstatus text
    Element((0, 24), ("Helvetica", 12),
        text = "Category Name"),
    Element((100, 24), ("Helvetica", 12),
        text = "Supplier Name"),
    Element((200, 24), ("Helvetica", 12),
        text = "Product Name"),
    Element((300, 24), ("Helvetica", 12),
        text = "Price"),
    Element((400, 24), ("Helvetica", 12),
        text = "Qty",),
    Element((500, 24), ("Helvetica", 12),
        text = "Total Price"),
    Element((600, 24), ("Helvetica", 12),
        text = "Status",),

    Rule((0, 42), 8*80, thickness = 2),])
    rpt.pagefooter = Band([
    Element((72*8, 0), ("Times-Bold", 20),
        text = "Page Footer", align = "right"),
    Element((36, 16), ("Helvetica-Bold", 12),
        sysvar = "pagenumber",
        format = lambda x: "Page %d" % x),
    ])

    # ==============Data inserted into Page=============
    rpt.detailband = Band([
        Element((0, 24), ("Helvetica", 12),key=1),
        # text = "Category Name"),
        Element((100, 24), ("Helvetica", 12),key=2),
            # text = "Supplier Name"),
        Element((200, 24), ("Helvetica", 12),key=3),
            # text = "Product Name"),
        Element((300, 24), ("Helvetica", 12),key=4),
            # text = "Price"),
        Element((400, 24), ("Helvetica", 12), key=5),
            # text = "Qty",),
        Element((500, 24), ("Helvetica", 12),key=6),
            # text = "Total Price"),
        Element((600, 24), ("Helvetica", 12),key=7),
        # text = "Status",),
    ])
    # ============Footer==========
    rpt.reportfooter = Band([
    Rule((300, 1), 300),
    Element((300, 4), ("Helvetica-Bold", 12),
        text = "Total Qty = "),
    Element((430, 4), ("Helvetica-Bold", 12),
        text = "Grand Total = "),
    Element((400,5), ("Helvetica-Bold", 12),
        text = f"{str(Qty_sum)}"),
    Element((510,5), ("Helvetica-Bold", 12),
        text = f"{str(Price_sum)}"),

])

    canvas = Canvas("ReportsFolder/sample02.pdf", (72*11, 72*8.5))
    rpt.generate(canvas)
    canvas.save()
    # vb.open_new("sample02.pdf")

    # if os.path.exists("sample02.pdf"):
    #     # os.close("sample02.pdf")   # error in this line
    #     os.remove("bill/sample02.pdf")
    #     canvas = Canvas("sample02.pdf", (72*11, 72*8.5))
    #     rpt.generate(canvas)
    #     canvas.save()
    #     vb.open_new("sample02.pdf")
    # else:
    #     print("The file does not exist")
    #     canvas = Canvas("bill/sample02.pdf", (72*11, 72*8.5))
    #     rpt.generate(canvas)
    #     canvas.save()
    #     vb.open_new("bill/sample02.pdf")
except Exception as ex:
    messagebox.showerror("Error",f"Error Due to {str(ex)}")


"""
Need to consider the switching on and off of physics schemes with reduced
precision. Show the active/deactive points as a function of precision for
selected physics schemes (Vertical Diffusion/Surface Fluxes/Convection)
"""

import numpy as np
import matplotlib.pyplot as plt
import iris.plot as iplt

from myscripts.statistics import count
from myscripts.models import speedy
from myscripts.projects.ithaca.tendencies import load_tendency

def main():
    variable = 'Temperature'
    sigma = speedy.sigma_levels[0]
    precisions = range(5, 24)

    # Create a two by two grid with shared x and y axes along rows and columns
    fig, axes = plt.subplots(nrows=2, ncols=2, sharex='col', figsize=[16, 10])

    plt.axes(axes[0, 0])
    scheme = 'Convection'
    make_plot(variable, scheme, sigma, precisions)
    plt.legend(title='Number of gridpoints')

    plt.axes(axes[0, 1])
    scheme = 'Surface Fluxes'
    make_plot(variable, scheme, sigma, precisions)

    plt.axes(axes[1, 0])
    scheme = 'Vertical Diffusion'
    make_plot(variable, scheme, sigma, precisions)

    plt.axes(axes[1, 1])
    variable = 'Specific Humidity'
    make_plot(variable, scheme, sigma, precisions)

    plt.xlabel('Precision [sbits]')
    fig.text(0.01, 0.5, 'Number of Gridpoints',
             rotation='vertical', va='center')

    plt.show()

    return

def make_plot(variable, scheme, sigma, precisions):
    rp = load_tendency(
        variable=variable, scheme=scheme,
        rp_scheme=scheme.replace(' ', '_').replace('-', '_').lower(),
        sigma=sigma, precision=precisions)

    fp = load_tendency(
        variable=variable, scheme=scheme,
        rp_scheme='all_parametrizations',
        sigma=sigma, precision=52)
    plt.title(scheme)
    plot_active(rp, fp)

    return

def plot_active(rp, fp, **kwargs):
    # Count the number of gridboxes with nonzero tendencies
    n_active, n_activated, n_deactivated, n_zeros = \
        count_active_deactive(rp, fp)

    for linestyle, label, data in [('-k', 'Active', n_active),
                                   ('--k', 'Activated', n_activated),
                                   (':k', 'Deactivated', n_deactivated)]:
        iplt.plot(data, linestyle, label=label, **kwargs)

    return

def count_active_deactive(rp, fp):
    # Ignore gridboxes where reduced precision has activated or deactivated the
    # physics scheme. Will give e=infinity or e=-1 respectively

    # Create a dummy cube to use the collapsed function
    cube = rp.copy()

    # Number of active gridpoints
    n_active = count(rp)

    # Number of activated gridpoints
    activated = np.logical_and(rp.data != 0, fp.data == 0)
    cube.data = activated
    n_activated = count(cube)

    # Number of deactivated gridpoints
    deactivated = np.logical_and(rp.data == 0, fp.data != 0)
    cube.data = deactivated
    n_deactivated = count(cube)

    # Number of zeros
    n_zeros = count(cube, func=lambda x: x == 0)

    return n_active, n_activated, n_deactivated, n_zeros

if __name__ == '__main__':
    main()


"""This is the utils module.

Module for storing information to the file between program launches.
"""

from os import path
import json

# '/your_absolute_path_here/notes.json'
JSON_FILE = path.abspath('notes.json')

def save_to_file(notes: list) -> None:
    """
    Save the list with notes to a file.

    :param notes: list with notes.
    :return: None
    """
    with open(JSON_FILE, 'w', encoding='utf-8') as outfile:
        json.dump(notes,
                  outfile,
                  ensure_ascii=False,
                  indent=4,
                  default=str,
                  separators=(',', ': '))

def load_from_json_file() -> list:
    """
    Load notes from file if file exists otherwise create empty JSON file.

    :return: list with notes
    """
    if not path.isfile(JSON_FILE):
        save_to_file([])

    with open(JSON_FILE, 'r', encoding='utf-8') as f:
        notes = json.load(f)
    return notes