import argparse

parser = argparse.ArgumentParser(description='Header file to use.')

parser.add_argument("-f", "--file", help="Header file to use.", type=str, required=True)
parser.add_argument("-t", "--target", help="Source file to write.", type=str, required=True)

args = parser.parse_args()

header_prelog = "\n#include <ProcessingUnit/A64InstructionSet/A64InstructionSet.h>\n#include <Utility/StreamableEnum.h>\n#include <ostream>\n#include <string_view>\n\nBEGIN_NAMESPACE\n\n"
header_epilog = "END_NAMESPACE\n"
map_prelog = "\nstatic constexpr SmallConstMap< "
map_name_epilog = "StringMap { { {"
map_epilog = "} } };\n"

ToChar_func_prelog = "template <>\nstatic std::string_view Enum::ToChar("
ToChar_func = " elem) { \n return "
ToChar_func_epilog = "StringMap.At(elem);\n}\n"

stream_func_prelog = "template<>\nstd::ostream& operator<<(std::ostream&os, "
stream_func_epilog = " elem) {\nos << Enum::ToChar(elem);\nreturn os;\n}"

enums = dict()
first_enum_name = ""

with open(args.file, "r") as f:
    lines = f.readlines()

    first_enum = True
    enum_found = False
    enum_just_found = False

    current_enum = ""

    for line in lines:
        if(enum_found):
            line = line.replace(" ", "")
            line = line.replace(",", "")
            line = line.replace("\n", "")

            if "//" in line:
                loc = line.find("//")
                line = line[0:loc]

            if line == "ARM_EMU_COUNT":
                enum_found = False
                continue
            else:
                if(len(line) != 0):
                    enums[current_enum].append(line)

        if(enum_just_found):
            enum_found = True
            enum_just_found = False
            continue

        if "enum class" in line and (not enum_found or not enum_just_found):
            enum_just_found = True
            line = line.replace(" ", "")
            line = line.removeprefix("enumclass")
            line = line.replace(":std::uint32_t\n", "")
            enums[line] = list()
            current_enum = line
            if(first_enum):
                first_enum_name = line
                first_enum_name = first_enum_name.replace("A64", "")
                first_enum = False

with open(args.target, "w") as f:
    f.write(header_prelog)
    f.write("namespace {\n\n")
    f.write("using namespace std::literals::string_view_literals;\n")

    for key, values in enums.items():
        f.write(map_prelog)
        tmp_key = key.replace("A64", "")

        if(first_enum_name != tmp_key):
            f.write(first_enum_name)
            f.write("::")
        f.write(key)

        f.write(", std::string_view, enum_size_v< ")
        if(first_enum_name != tmp_key):
            f.write(first_enum_name)
            f.write("::")
        f.write(key)

        f.write(" > >")
        f.write(key)
        f.write(map_name_epilog)
        f.write("\n")
        for value in values:
            f.write("{ ")

            if(first_enum_name != tmp_key):
                f.write(first_enum_name)
                f.write("::")
            f.write(key)
            f.write("::")
            f.write(value)
            f.write(", \"")
            f.write(value)
            f.write("\"sv }, ")
        f.write(map_epilog)
    f.write("\n}\n\n")

    for key, _ in enums.items():
        f.write(ToChar_func_prelog)

        tmp_key = key.replace("A64", "")
        if(first_enum_name != tmp_key):
            f.write(first_enum_name)
            f.write("::")

        f.write(key)
        f.write(ToChar_func)
        f.write(key)
        f.write(ToChar_func_epilog)
        f.write("\n")

        f.write(stream_func_prelog)
        tmp_key = key.replace("A64", "")
        if(first_enum_name != tmp_key):
            f.write(first_enum_name)
            f.write("::")
        f.write(key)
        f.write(stream_func_epilog)
        f.write("\n")
        f.write("\n")

    f.write(header_epilog)


from bs4 import BeautifulSoup
import requests
import time
import os
url = 'https://www.azlyrics.com/lyrics/hozier/{}.html'
songs = ["takemetochurch","someonenew", "tobealone", "fromeden", "worksong",
         "likerealpeopledo", "cherrywine", "arsonistslullabye", "shrike",
         "ninacriedpower", "almostsweetmusic", "be", "talk", "wouldthati",
         "sunlight", "firsttime", "francesca", "icarrionicarian", "eatyouryoung",
         "damagegetsdone", "whoweare", "allthingsend", "unknownnth", "firstlight",
         "abstractpsychopomp"]

# create a folder to store the data
folder_name = 'songs'

try:
    if not os.path.exists(folder_name):
        os.mkdir(folder_name)
    else:
        print('Directory already exist')
except Exception as e:
    print(f'Something went wrong with creating the directory: {str(e)}')

# get the html of each song and store inside the folder
with open("song_lyrics.txt", "a") as output_file:
    # get the html of each song and store inside the folder
    for song in songs:
        url_song = url.format(song)
        data = requests.get(url_song)  # request to get the content of the html
        time.sleep(5)  # buffer to avoid time out error

        # store the raw html inside the created folder.
        with open(os.path.join(folder_name, f"{song}.html"), "w+") as f:
            f.write(data.text)

        # clean up the html
        with open(os.path.join(folder_name, f"{song}.html"), "r") as f:
            page = f.read()

            upper_bound = '<!-- Usage of azlyrics.com content by any third-party lyrics provider is prohibited by our licensing agreement. Sorry about that. -->'
            lower_bound = '<!-- MxM banner -->'

            starting_pos = page.find(upper_bound)
            end_pos = page.find(lower_bound)

            if starting_pos != -1 and end_pos != -1:
                extract_content = page[starting_pos + len(upper_bound):end_pos]

                # parse the extracted content
                soup = BeautifulSoup(extract_content, 'html.parser')

                # erase html tags
                for tag in soup.find_all():
                    tag.decompose()

                lyrics_only = soup.get_text().strip()

                # Append lyrics to the output file
                output_file.write(lyrics_only + "\n")


import numpy as np
from scipy.optimize import fsolve
import pandas as pd
from tabulate import tabulate

# function to calculate pressure drop with Churchill eqn
def pdrop(q,l):
    re = (4 * q * density) / (np.pi * viscosity * diameter)
    a = (2.457 * np.log(1/((7/re)**0.9 + 0.27 * eps / diameter))) ** 16
    b = (37530/re)**16
    f = 2 * ((8/re)**12 + 1/((a+b)**1.5))**(1/12)
    return 2 * f * density * ((4*q)/(np.pi*diameter**2))**2 * l / diameter

# function with mass balances and energy balances
def equations(q):
    return [q[0]-q[1]-q[2],
            q[1]-q[3]-q[4],
            q[3]-q[5]-endflow,
            q[2]+q[4]-q[6]-q[7],
            q[5]+q[6]-q[8]-endflow,
            q[7]-q[9]-endflow,
            q[8]+q[9]-endflow,
            pdrop(q[1],lengths[1]) + pdrop(q[4],lengths[4]) - pdrop(q[2],lengths[2]),
            pdrop(q[3],lengths[3]) + pdrop(q[5],lengths[5]) - pdrop(q[4],lengths[4]) - pdrop(q[6],lengths[6]),
            pdrop(q[6],lengths[6]) + pdrop(q[8],lengths[8]) - pdrop(q[7],lengths[7]) - pdrop(q[9],lengths[9])]

# initialize variables
diameter = 0.0254 # metres
density = 1113 * 0.65 + 998 * 0.35 # kg/m^3
endflow = 72 / (60 * 1000) # m^3/s at 3,5,6,7
viscosity = 0.0161 * 0.65 + 0.0010 * 0.35 # Pa s
eps = 5E-5 # metres
lengths = np.array([230,460,585,460,380,380,460,380,380,460]) # metres

# initial guesses
q0 = [endflow * 4]
for i in range(1,10):
    if i == 1 or i == 2:
        q0.append(q0[0]/2)
    elif i == 3 or i == 4:
        q0.append(q0[1]/2)
    elif i == 5:
        q0.append(q0[3])
    elif i == 6 or i == 7:
        q0.append((q0[2]+q0[4])/2)
    elif i == 8:
        q0.append(q0[5]+q0[6])
    else:
        q0.append(q0[7])

# solve for q
x = fsolve(equations,q0)

# append results into a pandas dataframe
if __name__ =="__main__":
    pressures = [pdrop(x[i],lengths[i]) for i in range(10)]
    labels = ['Pipe 01', 'Pipe 12', 'Pipe 14', 'Pipe 23','Pipe 24', 'Pipe 35', 'Pipe 45', 'Pipe 46','Pipe 57', 'Pipe 67']
    df = pd.DataFrame(labels,columns=['Pipe Section'])
    df['Flowrate (m^3/s)'] = x
    df['Pressure Drop (Pa)'] = pressures
    print(tabulate(df, headers='keys', tablefmt='fancy_grid'))


import pytest
import torch
from datasets import Dataset, load_dataset
from transformers import AutoConfig, AutoModel, AutoTokenizer

from primeqa.mrc.models.heads.extractive import EXTRACTIVE_HEAD
from primeqa.mrc.models.task_model import ModelForDownstreamTasks
from primeqa.mrc.processors.preprocessors.base import BasePreProcessor
from primeqa.mrc.data_models.target_type import TargetType
from tests.primeqa.mrc.common.parameterization import parameterize_fixture_with_model_name

class UnitTest:
    """
    Base class for all unit test classes
    """

    @parameterize_fixture_with_model_name
    def model_name_and_config(self, request):
        model_name = request.param
        _ = AutoModel.from_pretrained(model_name)  # Pre-download LM inside flaky fixture so other tests have it
        return model_name, AutoConfig.from_pretrained(model_name)

    @pytest.mark.flaky(reruns=5, reruns_delay=2)  # Account for intermittent S3 errors downloading HF data
    @pytest.fixture(scope='session')
    def tokenizer(self, model_name_and_config):
        model_name, _ = model_name_and_config
        return AutoTokenizer.from_pretrained(model_name)

    @pytest.fixture(scope='session')
    def config_and_model_with_extractive_head(self, model_name_and_config):
        model_name, config = model_name_and_config
        model = ModelForDownstreamTasks.from_config(config,
                                                    model_name,
                                                    task_heads=EXTRACTIVE_HEAD)
        head_name = next(iter(EXTRACTIVE_HEAD))
        model.set_task_head(head_name)
        return config, model

    @pytest.fixture(scope='session')
    def extractive_training_inputs(self, config_and_model_with_extractive_head):
        _, model = config_and_model_with_extractive_head
        bs, seq_len = model.dummy_inputs['input_ids'].shape
        kwargs = dict(start_positions=torch.randint(0, seq_len, (bs, 1)),
                      end_positions=torch.randint(0, seq_len, (bs, 1)),
                      target_type=torch.randint(0, len(TargetType), (bs, 1)))
        kwargs.update(model.dummy_inputs)
        return kwargs

    @pytest.fixture(scope='session')
    def train_examples(self):
        question = ["Who walked the dog?", "What time is it?"]
        context = [["Alice walks the cat", "Bob walks the dog"],
                   ["The quick brown fox jumps over the lazy dog", "Glenn the otter lives at the aquarium", "Go"]]
        example_id = ["foo-abc", "bar-123"]
        start_positions = [[0], [-1]]
        end_positions = [[2], [-1]]
        passage_indices = [[1], [-1]]
        yes_no_answer = [["NONE"], ["NONE"]]
        examples_dict = dict(question=question, context=context, example_id=example_id,
                             target=[dict(start_positions=s, end_positions=e, passage_indices=p, yes_no_answer=yn)
                                     for s, e, p, yn in
                                     zip(start_positions, end_positions, passage_indices, yes_no_answer)])
        examples_dataset = Dataset.from_dict(examples_dict)
        return examples_dataset

    @pytest.fixture(scope='session')
    def eval_examples(self, train_examples):
        return train_examples.remove_columns('target')

    @pytest.fixture(scope='session')
    def preprocessor(self, tokenizer):
        return BasePreProcessor(
            tokenizer,
            stride=128,
            load_from_cache_file=False,
            negative_sampling_prob_when_has_answer=0.5,
            negative_sampling_prob_when_no_answer=0.5,
        )

    @pytest.fixture(scope='session')
    def train_examples_and_features(self, train_examples, preprocessor):
        return preprocessor.process_train(train_examples)

    @pytest.fixture(scope='session')
    def eval_examples_and_features(self, eval_examples, preprocessor):
        return preprocessor.process_eval(eval_examples)

    @staticmethod
    def _assert_is_floating_point_tensor(tensor):
        assert isinstance(tensor, torch.Tensor)
        assert tensor.dtype.is_floating_point

    @pytest.mark.flaky(reruns=10, reruns_delay=30)  # Account for intermittent S3 errors downloading HF data
    @pytest.fixture(scope='session')
    def tydiqa_train_examples(self):
        raw_dataset = load_dataset("tydiqa", "primary_task", split='train', streaming=True)
        iterable_dataset = raw_dataset.take(100)
        examples = {}
        for e in iterable_dataset:
            for key in e.keys():
                if key not in examples:
                    examples[key] = []
                examples[key].append(e[key])
        return Dataset.from_dict(examples)

    @pytest.mark.flaky(reruns=10, reruns_delay=30)  # Account for intermittent S3 errors downloading HF data
    @pytest.fixture(scope='session')
    def tydiqa_eval_examples(self):
        raw_dataset = load_dataset("tydiqa", "primary_task", split='validation', streaming=True)
        iterable_dataset = raw_dataset.take(100)
        examples = {}
        for e in iterable_dataset:
            for key in e.keys():
                if key not in examples:
                    examples[key] = []
                examples[key].append(e[key])
        return Dataset.from_dict(examples)

    @pytest.mark.flaky(reruns=10, reruns_delay=30)  # Account for intermittent S3 errors downloading HF data
    @pytest.fixture(scope='session')
    def squad_train_examples(self):
        raw_dataset = load_dataset("squad", "plain_text", split='train', streaming=True)
        iterable_dataset = raw_dataset.take(100)
        examples = {}
        for e in iterable_dataset:
            for key in e.keys():
                if key not in examples:
                    examples[key] = []
                examples[key].append(e[key])
        return Dataset.from_dict(examples)

    @pytest.mark.flaky(reruns=10, reruns_delay=30)  # Account for intermittent S3 errors downloading HF data
    @pytest.fixture(scope='session')
    def squad_eval_examples(self):
        raw_dataset = load_dataset("squad", "plain_text", split='validation', streaming=True)
        iterable_dataset = raw_dataset.take(100)
        examples = {}
        for e in iterable_dataset:
            for key in e.keys():
                if key not in examples:
                    examples[key] = []
                examples[key].append(e[key])
        return Dataset.from_dict(examples)


import json

import tabulate
import pandas as pd
import numpy as np
import os
import os.path as osp
import glob
import argparse

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--dir', type=str, help='the log directory')
    args = parser.parse_args()

    path = args.dir

    files = sorted(glob.glob(osp.join(path, '*.log')))

    def parse_name(s):
        s = s.replace('-snapshot','')
        l = s.split('-')
        ds_name = ''
        for x in l[:-2]:
            if len(ds_name) > 0:
                ds_name = ds_name + '-'
            ds_name = ds_name + x
        return ds_name, l[-2], l[-1]

    def get_diff(x, y):
        if x == 'nan':
            return -1e9
        if y == 'nan':
            return 1e9
        x = x.strip('**')
        y = y.strip('**')
        x_acc, x_std = x.split(' +- ')
        y_acc, y_std = y.split(' +- ')
        return float(x_acc) - float(y_acc)
        if x_acc != y_acc:
            return float(x_acc) - float(y_acc)
        else:
            return float(y_std) - float(x_std)

    acc_map, time_map, diff_map, best_map = {}, {}, {}, {}

    for filename in files:
        with open(filename, 'r') as f:
            filename = filename.replace(path + '/', '')
            filename = filename.strip('.log')

            try:
                json_dict = json.load(f)
            except json.decoder.JSONDecodeError:
                continue
            ds_name, filter_method, snapshot = parse_name(filename)
            for kk, v in json_dict.items():
                k = kk
                if 'WL_' in kk:
                    k = 'WL'
                if 'GL_' in kk:
                    k = 'GL'
                if 'WLOA_' in kk:
                    k = 'WLOA'
                if 'CSM_' in kk:
                    k = 'CSM'
                if 'PM_' in kk:
                    k = 'PM'
                if 'MLG' in kk:
                    k = 'MLG'
                if k not in acc_map:
                    acc_map[k] = {}
                    time_map[k] = {}
                if ds_name not in acc_map[k]:
                    acc_map[k][ds_name] = {}
                    time_map[k][ds_name] = {}

                if filter_method + '-' + snapshot not in acc_map[k][ds_name]:
                    acc_map[k][ds_name][filter_method + '-' + snapshot] = 'nan'
                    time_map[k][ds_name][filter_method + '-' + snapshot] = 'nan'
                if 'acc' in v and get_diff(acc_map[k][ds_name][filter_method + '-' + snapshot], v['acc']) < 0:
                    acc_map[k][ds_name][filter_method + '-' + snapshot] = v['acc']
                    time_map[k][ds_name][filter_method + '-' + snapshot] = v['time']

    for k, ma in acc_map.items():
        print(f'## {k}')
        df = pd.DataFrame(ma)
        print(tabulate.tabulate(
            df,
            tablefmt='github',
            headers='keys'
        ))
        print()

    diff_map = {}
    for kernel_method, ma in acc_map.items():
        diff_map[kernel_method] = {}
        for ds_name, mama in ma.items():
            if 'intact-001' not in mama:
                mama['intact-001'] = 'nan'
            intact_1 = mama['intact-001']
            diff_map[kernel_method][ds_name] = -1e9
            for method, res in mama.items():
                if method == 'intact-001':
                    continue
                diff_map[kernel_method][ds_name] = max(get_diff(res, intact_1), diff_map[kernel_method][ds_name])

    print(f'## improvement')
    df = pd.DataFrame(diff_map)
    print(tabulate.tabulate(
        df,
        tablefmt='github',
        headers='keys',
        floatfmt='+.2f'
    ))
    print()

    best_map = {}
    for kernel_method, ma in acc_map.items():
        for ds_name, mama in ma.items():
            if ds_name not in best_map:
                best_map[ds_name] = {}
                best_map[ds_name]['name'] = 'nan'
                best_map[ds_name]['acc'] = 'nan'
                best_map[ds_name]['improvement'] = -1e9
            for method, res in mama.items():
                if get_diff(best_map[ds_name]['acc'], res) < 0:
                    best_map[ds_name]['name'] = kernel_method + '-' + method
                    best_map[ds_name]['acc'] = res
    ds_names = set()
    for kernel_method, ma in acc_map.items():
        for ds_name, _ in ma.items():
            ds_names.add(ds_name)

    for ds_name in ds_names:
        intact_1 = 'nan'
        for kernel_method, ma in acc_map.items():
            if get_diff(intact_1, ma[ds_name]['intact-001']) < 0:
                intact_1 = ma[ds_name]['intact-001']

        for kernel_method, ma in acc_map.items():
            for d_name, mama in ma.items():
                if ds_name != d_name:
                    continue
                for method, res in mama.items():
                    if method == 'intact-001':
                        continue
                    best_map[ds_name]['improvement'] = max(best_map[ds_name]['improvement'], get_diff(res,intact_1))

    print('## best method')
    df = pd.DataFrame(best_map)
    print(tabulate.tabulate(
        df.transpose(),
        tablefmt='github',
        headers='keys',
        floatfmt='+.2f'
    ))


from panda3d.core import *
from direct.showbase import DirectObject
from direct.interval.IntervalGlobal import *
from toontown.toonbase import ToontownGlobals
from . import MovingPlatform
from direct.task.Task import Task
from toontown.suit import Suit
from toontown.suit import SuitDNA

class Ouch(DirectObject.DirectObject):

    def __init__(self, keyEvent, callback):
        DirectObject.DirectObject.__init__(self)
        self.accept(keyEvent, callback)

    def destroy(self):
        self.ignoreAll()

class CyclePlacer(DirectObject.DirectObject):

    def __init__(self, locations, keyEvent, startIndex = 0):
        DirectObject.DirectObject.__init__(self)
        self.locations = locations
        self.index = startIndex
        self.accept(keyEvent, self.gotoNextLocation)

    def destroy(self):
        self.locations = None
        self.ignoreAll()
        return

    def gotoNextLocation(self):
        self.index = (self.index + 1) % len(self.locations)
        self.gotoLocation()

    def gotoLocation(self, index = None):
        if index is None:
            index = self.index
        pos, h = self.locations[index]
        base.localAvatar.reparentTo(render)
        base.localAvatar.setPos(*pos)
        base.localAvatar.setH(h)
        return

class ToonLifter(DirectObject.DirectObject):
    SerialNum = 0

    def __init__(self, keyDownEvent, speed = 2):
        DirectObject.DirectObject.__init__(self)
        self.serialNum = ToonLifter.SerialNum
        ToonLifter.SerialNum += 1
        self.taskName = 'ToonLifter%s' % self.serialNum
        self.keyDownEvent = keyDownEvent
        self.keyUpEvent = self.keyDownEvent + '-up'
        self.speed = speed
        self.accept(self.keyDownEvent, self.startLifting)

    def destroy(self):
        self.ignoreAll()
        taskMgr.remove(self.taskName)

    def startLifting(self):

        def liftTask(task, self = self):
            base.localAvatar.setZ(base.localAvatar.getZ() + self.speed)
            return Task.cont

        def stopLifting(self = self):
            taskMgr.remove(self.taskName)
            self.ignore(self.keyUpEvent)
            self.accept(self.keyDownEvent, self.startLifting)

        self.ignore(self.keyDownEvent)
        self.accept(self.keyUpEvent, stopLifting)
        taskMgr.add(liftTask, self.taskName)


import time
from turtle import Screen
from paddle import Paddle
from Ball import Ball
from Score import Score

game_is_on = True
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("Peddle Game")

paddle1 = Paddle(370, 0)
paddle2 = Paddle(-375, 0)
ball = Ball()
score = Score()

screen.listen()
screen.onkeypress(key="w", fun=paddle2.up)
screen.onkeypress(key="s", fun=paddle2.down)
screen.onkeypress(key="o", fun=paddle1.up)
screen.onkeypress(key="k", fun=paddle1.down)

screen.tracer()

while game_is_on:
    ball.move_ball()

    if ball.ycor() > 280 or ball.ycor() < -280:
        ball.bounce()

    if ball.xcor() > 380:
        score.increase_l_ply_s()
        ball.reset_ball()

    elif ball.xcor() < -380:
        score.increase_r_ply_s()
        ball.reset_ball()

    if (ball.distance(paddle1) < 50 and ball.xcor() > 340) or (ball.distance(paddle2) < 50 and ball.xcor() < -340):
        ball.bounce_from_paddle()

screen.exitonclick()


import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.switch_backend('agg')
from subprocess import Popen,PIPE

def draw_acf(acf):
    plt.plot(np.arange(51),acf)
    plt.savefig('acf.png')

def draw_rate(wkdir,init):
    wkdir = f'result/rate/{wkdir}'
    for (p,r) in [(4,4)]:
    # for (p,r,dgp_s) in [(1,1,1),(0,1,1),(0,1,0),(0,0,1)]:
        df = pd.read_csv('{}/csv/p{}r{}.csv'.format(wkdir,p,r),header=None)
        df = df[df[1].str.contains(init)]
        T = [209,309,409,809]
        T0 = [9,9,9,9]
        s = [5,5,5,5]
        N=10
        # T1 = [100,200,300,500,700,1000]
        total_err = df[2]; est_err = df[3]; aprox_err = df[4]
        if not os.path.exists(f'{wkdir}/fig'):
            os.makedirs('{}/fig'.format(wkdir))

        plt.figure()
        plt.ylim([0,np.max(total_err)+0.1])
        x = np.divide(((r)*N+np.log(T0))*s,T)
        plt.xlim([0,np.max(x)+0.5])
        plt.plot(x,total_err,marker='o')
        plt.savefig(f'{wkdir}/fig/total_p{p}r{r}_{init}.png')

        plt.figure()
        plt.ylim([0,np.max(est_err)+0.1])
        x = np.divide(((r)*N+np.log(T0))*s,T)
        plt.xlim([0,np.max(x)+0.5])
        plt.plot(x,est_err,marker='o')
        plt.savefig(f'{wkdir}/fig/est_p{p}r{r}_{init}.png')

        plt.figure()
        plt.ylim([0,np.max(aprox_err)+0.1])
        x = np.divide(((r)*N+np.log(T0))*s,T)
        plt.xlim([0,np.max(x)+0.5])
        plt.plot(x,aprox_err,marker='o')
        plt.savefig(f'{wkdir}/fig/aprox_p{p}r{r}_{init}.png')

if __name__ == "__main__":
    # draw_rate('GDversusTrue','true')
    draw_rate('season_AR','noisetrue')

High-dimensional Low-rank Time series/main/utils/visualize.py


import discord
from discord.ext import commands
import asyncio
import time
import os
from discord.ext.commands import cooldown, BucketType
from discord.ext import commands
from discord.utils import get
import requests
from discord import CategoryChannel
import json

prefix = '?'
client = commands.Bot(command_prefix=prefix)
TOKEN = ''
SELLIXAPI = ''
MERCHANT = ''

with open('product_config.json', 'r') as f:
    PRODUCTDATA = json.load(f)

@client.event
async def on_ready():
    os.system('cls')
    print('Bot launched as {0.user}'.format(client))
    print('Prefix is {prefix}'.format(prefix))

@client.command()
async def claim(ctx, *, arg):
    if ctx.channel.type == discord.ChannelType.private:

        with open('oldorders.txt', 'r') as f:
            if arg in f.read():

                await ctx.send('Order already claimed!')
                await ctx.send('Verifying order...')

                headers = {
                    'Authorization': 'Bearer ' + SELLIXAPI,
                    'X-Sellix-Merchant': MERCHANT
                }

                r = requests.get('https://dev.sellix.io/v1/orders/' + arg, headers=headers)
                sellix = r.json()

                if sellix['error'] == 'Invoice Not Found':
                    await ctx.send('Order not found.')
                else:
                    if sellix['status'] == 200:

                        order_product = sellix['data']['order']['product_title']

                        for i in PRODUCTDATA:
                            if i['product'] == order_product:
                                await ctx.send('Order found, claiming...')
                                await ctx.send('Order is for {product}'.format(product=order_product))
                                with open('oldorders.txt', 'a') as f:
                                    f.write(arg + '\n')
                                filename = i['file']
                                await ctx.send(file=discord.File(filename))
                                await ctx.send('Enjoy!')

                    else:
                        await ctx.send('Failure while checking order, please contact support. Error code: ' + sellix['status'])
    else:
        await ctx.send('Please DM me to claim!')

client.run(TOKEN)


import torchvision as tv
import matplotlib.pyplot as plt
import torchvision.models as models
import numpy as np
from PIL import Image
from torch.autograd import Variable
from PIL import ImageFilter
import PIL.ImageOps
import torch.nn as nn
import scipy.ndimage as ndimage
from scipy import spatial
from sklearn import svm

transforms = tv.transforms

# Load the birds images and classify them
def sub2():
    print("=============sub2================")
    print("Running sub2: Bird classification")
    vgg16 = models.vgg16(pretrained=True)
    vgg16.eval()
    bird_0_o = Image.open('birds/bird_0.jpg')
    bird_0 = prep_image(bird_0_o)
    bird_1_o = Image.open('birds/bird_1.jpg')
    bird_1 = prep_image(bird_1_o)

    bird_0_class, _ = classify(vgg16, bird_0)
    bird_1_class, _ = classify(vgg16, bird_1)

    fig = plt.figure()
    plt.suptitle("sub2 - Bird classification")
    sb = fig.add_subplot(221)
    sb.imshow(bird_0_o)
    sb.axis('off')
    sb.set_title("Original bird_0")
    sb = fig.add_subplot(222)
    sb.imshow(bird_1_o)
    sb.axis('off')
    sb.set_title("Original bird_1")

    sb = fig.add_subplot(223)
    sb.imshow(np.moveaxis(np.squeeze(bird_0.data.numpy()), 0, -1))
    sb.axis('off')
    sb.set_title("bird_0 - class == " + str(bird_0_class))
    sb = fig.add_subplot(224)
    sb.imshow(np.moveaxis(np.squeeze(bird_1.data.numpy()), 0, -1))
    sb.axis('off')
    sb.set_title("bird_1 - class == " + str(bird_1_class))
    plt.show()
    return

# Load our image (ice cream) and classify it
def sub3():
    print("===============sub3===================")
    print("Running sub3: Ice-Cream classification")
    vgg16 = models.vgg16(pretrained=True)
    vgg16.eval()
    image_o = Image.open('our_images/ice_cream.jpg')
    image = prep_image(image_o)
    classification, _ = classify(vgg16, image)

    fig = plt.figure()
    plt.suptitle("sub3 - Ice-Cream classification")
    sb = fig.add_subplot(121)
    sb.set_title("Original image")
    sb.axis('off')
    sb.imshow(image_o)

    sb = fig.add_subplot(122)
    sb.imshow(np.moveaxis(np.squeeze(image.data.numpy()), 0, -1))
    sb.set_title("NN ready image - classified as: " + str(classification))
    sb.axis('off')
    plt.show()
    return

# Perform transformations and classify our image
def sub4():
    print("=================sub4=============================")
    print("Running sub4: Transformed Ice-Cream classification")
    vgg16 = models.vgg16(pretrained=True)
    vgg16.eval()

    image = Image.open('our_images/ice_cream.jpg')

    fig = plt.figure()
    plt.suptitle("sub4 - Classification under transformations")

    print("---Filtered Image---")
    image_filt_o = image.filter(ImageFilter.BLUR)
    image_filt = prep_image(image_filt_o)
    classification, _ = classify(vgg16, image_filt)
    sb = fig.add_subplot(131)
    sb.imshow(image_filt_o)
    sb.axis('off')
    sb.set_title("Blurred image - classified as: " + str(classification))

    print("---Different color Image---")
    image_color_o = PIL.ImageOps.invert(image)
    image_color = prep_image(image_color_o)
    classification, _ = classify(vgg16, image_color)
    sb = fig.add_subplot(132)
    sb.imshow(image_color_o)
    sb.axis('off')
    sb.set_title("Inverted color image - classified as: " + str(classification))

    print("---Geometric transform Image---")
    image_geo_o = image.rotate(45)
    image_geo = prep_image(image_geo_o)
    classification, _ = classify(vgg16, image_geo)
    sb = fig.add_subplot(133)
    sb.imshow(image_geo_o)
    sb.axis('off')
    sb.set_title("45deg. rotated image - classified as: " + str(classification))

    plt.show()
    return

def sub5():
    print("=====sub5=======")
    print("Accessing layers")
    vgg16 = models.vgg16(pretrained=True)
    for child in vgg16.children():
        layer = child[0]
        break
    layer_np = layer.weight.data.numpy()
    filters = layer_np[0:2, :, :, :]

    fig = plt.figure()
    plt.suptitle('sub5 - 1st Conv. layer filters')
    plt.axis('off')
    for i in range(2):
        filter_3d = filters[i, :, :, :]
        sb = fig.add_subplot(1, 2, i+1)
        sb.axis('off')
        sb.imshow(filter_3d)
    plt.show()

    # Prepare images
    ice_cream = Image.open('our_images/ice_cream.jpg')
    image_filt = ice_cream.filter(ImageFilter.BLUR)
    image_filt = prep_image(image_filt)
    image_filt = np.moveaxis(np.squeeze(image_filt.data.numpy()), 0, -1)
    image_color = PIL.ImageOps.invert(ice_cream)
    image_color = prep_image(image_color)
    image_color = np.moveaxis(np.squeeze(image_color.data.numpy()), 0, -1)
    image_geo = ice_cream.rotate(45)
    image_geo = prep_image(image_geo)
    image_geo = np.moveaxis(np.squeeze(image_geo.data.numpy()), 0, -1)
    image_list = list([image_filt, image_color, image_geo])

    fig2 = plt.figure()
    plt.suptitle('sub5 - 1st Conv. layer response')
    plt.axis('off')
    subplot = 1
    for j in range(3):
        image = image_list[j]
        for i in range(2):
            response = ndimage.convolve(image, filters[i, :, :, :])
            sb = fig2.add_subplot(3, 2, subplot)
            sb.axis('off')
            sb.imshow(response)
            subplot += 1
    plt.show()
    return

def sub6():
    print("==========sub6===========")
    print("Accessing features at FC7")

    cats_dogs = get_images()

    vgg16 = models.vgg16(pretrained=True)
    vgg16.classifier = nn.Sequential(*list(vgg16.classifier.children())[:-3])
    vgg16.eval()

    it = 0
    colors = ["#FF0000", "#000000"]

    for animal, images in cats_dogs.items():
        for image in images:
            result = vgg16(image)
            vector = np.squeeze(result.data.numpy())
            xx = range(1, np.size(vector)+1)
            plt.scatter(xx, vector, s=0.1, c=colors[it])
        it += 1
    plt.suptitle("sub6 - Feature vectors extracted from FC7")
    plt.show()
    return

def sub7():
    print("=============sub7==============")
    print("Classifying our own cat and dog")
    cat_o = Image.open('our_images/cat.jpg')
    dog_o = Image.open('our_images/dog.jpg')
    cat = prep_image(cat_o)
    dog = prep_image(dog_o)

    fig = plt.figure()
    fig.suptitle("sub7 - Our cat and dog")
    sp = fig.add_subplot(321)
    sp.imshow(cat_o)
    sp.axis('off')
    sp.set_title('Original cat')
    sp = fig.add_subplot(322)
    sp.imshow(dog_o)
    sp.axis('off')
    sp.set_title("Original dog")
    sp = fig.add_subplot(323)
    sp.imshow(np.moveaxis(np.squeeze(cat.data.numpy()), 0, -1))
    sp.axis('off')
    sp.set_title("NN ready cat")
    sp = fig.add_subplot(324)
    sp.imshow(np.moveaxis(np.squeeze(dog.data.numpy()), 0, -1))
    sp.axis('off')
    sp.set_title("NN ready dog")

    cats_dogs = get_images()

    vgg16 = models.vgg16(pretrained=True)
    vgg16.classifier = nn.Sequential(*list(vgg16.classifier.children())[:-3])
    vgg16.eval()

    features = np.zeros([20, 4096])
    i = 0
    for animal, images in cats_dogs.items():
        # Note: the first animal handled is first animal in the array. This is not safe, but we don't mind
        for image in images:
            result = vgg16(image)
            vector = result.data.numpy()
            features[i, :] = vector
            i += 1

    tree = spatial.KDTree(features)
    cat_features = vgg16(cat).data.numpy()
    dist, pos = tree.query(cat_features, p=2)  # this is euclidean distance
    pos = pos[0]
    print("Image of cat -")
    if pos < 10:
        animal = "cats"
    else:
        animal = "dogs"
    pos = pos % 10

    print("Closest to " + str(animal) + " at " + str(pos))

    sp = fig.add_subplot(325)
    n_image = (cats_dogs[animal])[pos]
    sp.imshow(np.moveaxis(np.squeeze(n_image.data.numpy()), 0, -1))
    sp.set_title("Nearest neighbor")
    sp.axis('off')

    dog_features = vgg16(dog).data.numpy()
    dist, pos = tree.query(dog_features, p=2)
    pos = pos[0]
    print("Image of dog -")
    if pos < 10:
        animal = "cats"
    else:
        animal = "dogs"
    pos = pos % 10

    print("Closest to " + str(animal) + " at " + str(pos))

    sp = fig.add_subplot(326)
    n_image = (cats_dogs[animal])[pos]
    sp.imshow(np.moveaxis(np.squeeze(n_image.data.numpy()), 0, -1))
    sp.axis('off')
    sp.set_title("Nearest neighbor")
    plt.show()
    return

def sub8():
    print("==============sub8================")
    print("Classifying our own tiger and wolf")
    tiger_o = Image.open('our_images/tiger.jpg')
    wolf_o = Image.open('our_images/wolf.jpg')
    tiger = prep_image(tiger_o)
    wolf = prep_image(wolf_o)

    fig = plt.figure()
    fig.suptitle("sub8 - Our tiger and wolf")
    sp = fig.add_subplot(321)
    sp.imshow(tiger_o)
    sp.axis('off')
    sp.set_title('Original tiger')
    sp = fig.add_subplot(322)
    sp.imshow(wolf_o)
    sp.axis('off')
    sp.set_title("Original wolf")
    sp = fig.add_subplot(323)
    sp.imshow(np.moveaxis(np.squeeze(tiger.data.numpy()), 0, -1))
    sp.axis('off')
    sp.set_title("NN ready tiger")
    sp = fig.add_subplot(324)
    sp.imshow(np.moveaxis(np.squeeze(wolf.data.numpy()), 0, -1))
    sp.axis('off')
    sp.set_title("NN ready wolf")

    cats_dogs = get_images()

    vgg16 = models.vgg16(pretrained=True)
    vgg16.classifier = nn.Sequential(*list(vgg16.classifier.children())[:-3])
    vgg16.eval()

    features = np.zeros([20, 4096])
    i = 0
    for animal, images in cats_dogs.items():
        # Note: the first animal handled is first animal in the array. This is not safe, but we don't mind
        for image in images:
            result = vgg16(image)
            vector = result.data.numpy()
            features[i, :] = vector
            i += 1

    tree = spatial.KDTree(features)
    tiger_features = vgg16(tiger).data.numpy()
    dist, pos = tree.query(tiger_features, p=2)  # this is euclidian distance
    pos = pos[0]
    print("Image of tiger -")
    if pos < 10:
        animal = "cats"
    else:
        animal = "dogs"
    pos = pos % 10
    print("Closest to " + str(animal) + " at " + str(pos))

    sp = fig.add_subplot(325)
    n_image = (cats_dogs[animal])[pos]
    sp.imshow(np.moveaxis(np.squeeze(n_image.data.numpy()), 0, -1))
    sp.set_title("Nearest neighbor")
    sp.axis('off')

    wolf_features = vgg16(wolf).data.numpy()
    dist, pos = tree.query(wolf_features, p=2)
    pos = pos[0]
    print("Image of wolf -")
    if pos < 10:
        animal = "cats"
    else:
        animal = "dogs"
    pos = pos % 10
    print("Closest to " + str(animal) + " at " + str(pos))

    sp = fig.add_subplot(326)
    n_image = (cats_dogs[animal])[pos]
    sp.imshow(np.moveaxis(np.squeeze(n_image.data.numpy()), 0, -1))
    sp.axis('off')
    sp.set_title("Nearest neighbor")
    plt.show()
    return

# Returns the SVM classifier created created in this function
# In this we cut off the network after the ReLU at FC7, because otherwise our tiger is a dog
def sub9():
    print("===========sub9============")
    print("Building our own classifier")

    cats_dogs = get_images()
    vgg16 = models.vgg16(pretrained=True)
    vgg16.classifier = nn.Sequential(*list(vgg16.classifier.children())[:-1])
    vgg16.eval()

    svm_classifier = svm.SVC()

    features = np.zeros([20, 4096])
    tags = np.zeros([20])
    i = 0
    tag = 0  # 0 is cat, 1 is dog
    for animal, images in cats_dogs.items():
        # Note: the first animal handled is first animal in the array. This is not safe, but we don't mind
        for image in images:
            result = vgg16(image)
            vector = result.data.numpy()
            features[i, :] = vector
            tags[i] = tag
            i += 1
        tag += 1  # update the tag
    # Train SVM
    svm_classifier.fit(features, tags)

    # Obtain features
    cat_o = Image.open('our_images/cat.jpg')
    dog_o = Image.open('our_images/dog.jpg')
    cat = prep_image(cat_o)
    dog = prep_image(dog_o)
    cat_features = vgg16(cat).data.numpy()
    dog_features = vgg16(dog).data.numpy()

    # Classify two images
    result = svm_classifier.predict(cat_features)
    print("Cat classified as: " + str(result))
    result = svm_classifier.predict(dog_features)
    print("Dog classified as: " + str(result))
    print("Legend: 0-cat, 1-dog")
    return svm_classifier

def sub10(classifier):
    print("==================sub10=====================")
    print("Classifying our own tiger and wolf using SVM")
    tiger_o = Image.open('our_images/tiger.jpg')
    wolf_o = Image.open('our_images/wolf.jpg')
    tiger = prep_image(tiger_o)
    wolf = prep_image(wolf_o)

    vgg16 = models.vgg16(pretrained=True)
    vgg16.classifier = nn.Sequential(*list(vgg16.classifier.children())[:-1])
    vgg16.eval()

    tiger_features = vgg16(tiger).data.numpy()
    result = classifier.predict(tiger_features)
    print("Tiger classified as: " + str(result))

    wolf_features = vgg16(wolf).data.numpy()
    result = classifier.predict(wolf_features)
    print("Wolf classified as: " + str(result))
    print("Legend: 0-cat, 1-dog")
    return

def prep_image(image):
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
    our_transform = transforms.Compose([
        transforms.Resize([224, 224]),
        transforms.ToTensor(),
        normalize,
    ])
    image = our_transform(image).float()
    image = image.unsqueeze(0)
    image = Variable(image, requires_grad=True)
    return image

def classify(model, image):
    result = model(image)
    net_out = result.data.numpy()
    return np.argmax(net_out), np.max(net_out)

# Returns a dict with all cat images under cats, and dog images under dogs
def get_images():
    # cats_dogs = list()
    cats_dogs = dict()
    cats = list()
    dogs = list()
    for i in range(10):
        dog = prep_image(Image.open("dogs\dog_" + str(i) + ".jpg"))
        cat = prep_image(Image.open("cats\cat_" + str(i) + ".jpg"))
        dogs.append(dog)
        cats.append(cat)
    # cats_dogs.append(cats)
    # cats_dogs.append(dogs)
    cats_dogs["cats"] = cats
    cats_dogs["dogs"] = dogs
    return cats_dogs

if __name__ == "__main__":
    sub2()
    sub3()
    sub4()
    sub5()
    sub6()
    sub7()
    sub8()
    sub9_svm = sub9()
    sub10(sub9_svm)  # don't run without sub9


"""Nerdle Game tree builder and analysis of distribution of #guesses over all answers."""
import collections
import numpy as np
import scipy.stats
from typing import Iterable, Dict

from . import solver

class Node:
    def __init__(self, key, guesses, answers, score, children, hint=None, parent=None):
        self.key = key
        self.guesses = guesses
        self.answers = answers
        self.score = score
        self.hint = hint
        self.children = children
        self.parent = parent

    def __repr__(self):
        return "Node[key={}, guesses={} answers={}{}, score={}, children={}]".format(
            self.key, len(self.guesses), len(self.answers),
            " " + str(self.answers) if len(self.answers) <= 10 else "",
            self.score.shape, len(self.children))

    def __str__(self):
        return repr(self)

    def guess_key(self, key):
        """Returns the root-level index of guess key(s) (intt or np.ndarray) of this node."""
        node = self
        while node.parent:
            node = node.parent
            key = node.guesses[key]
        return key

    def answer_key(self, key):
        """Returns the root-level index of answer key(s) (intt or np.ndarray) of this node."""
        node = self
        while node.parent:
            node = node.parent
            key = node.answers[key]
        return key

class GameTreeBuilder:
    def __init__(self, solver_data, max_answers: int = 10000000):
        self._solver_data = solver_data
        self._score_db = solver_data.score_db.copy()[:, :max_answers]
        self._solver = solver.NerdleSolver(solver_data)
        self._all_keys = solver_data.all_keys
        self._n = len(solver_data.all_keys)

    def build(self, debug: bool = False, strategy="minimax",
              min_sample_size: int = 2000, sample_factor: float = 1.7,
              guess_coarsening_factor: float = 4) -> Node:
        if strategy == "minimax":
            bucket_size_functor = max_bucket_sizes
        elif strategy == "multilevel":
            def bucket_size_functor(a):
                if a.shape[1] <= min_sample_size:
                    #print(a.shape, "max_bucket_sizes")
                    return max_bucket_sizes(a)
                else:
                    print(a.shape, "min_biased_multilevel_sampling")
                    return min_biased_multilevel_sampling(
                        a, max_bucket_sizes, min_sample_size=min_sample_size,
                        sample_factor=sample_factor)
        else:
            raise ValueError("Unknown max bucket calculation strategy {}".format(strategy))
        quantity = lambda a: bucket_size_functor(a) / a.shape[1]
        root = Node(None, self._all_keys, self._all_keys, self._score_db, [])
        pre_traversal(root, lambda node: self._process_node(
            node, quantity, guess_coarsening_factor=guess_coarsening_factor), debug=debug)
        return root

    def _process_node(self, node, bucket_size_functor, guess_coarsening_factor: float = 4):
        if len(node.answers) == 1:
            guess_is_answer = np.where(node.guesses == node.answers[0])[0]
            if len(guess_is_answer) != 1 or not self._solver.is_correct(node.score[guess_is_answer[0], 0]):
                raise ValueError("Failed to solve game")
        else:
            # Coarsen in rows (guesses).
            score = node.score
            if guess_coarsening_factor > 1 and len(node.answers) <= 0.1 * self._n:
                # Coarsen in rows.
                guesses = np.sort(
                    np.concatenate(
                    (node.answers,
                     np.random.choice(np.setdiff1d(node.guesses, node.answers),
                                      size=int((self._n - len(node.answers)) / guess_coarsening_factor),
                                      replace=False))))
                score = score[np.where(np.in1d(node.guesses, guesses))[0]]
            else:
                guesses = node.guesses

            # Find best next guess = argmin(max bucket size).
            answer_index = np.arange(len(node.answers), dtype=int)
            bucket_sizes = bucket_size_functor(score)
            feasible = np.logical_not(np.in1d(np.arange(len(bucket_sizes)), answer_index))
            bucket_size, _, guess_index_opt = min((b, k_feasible, k)
                                                  for k, (k_feasible, b) in enumerate(zip(feasible, bucket_sizes)))
            guess_opt = guesses[guess_index_opt]

            # TODO: use depth-first traversal and only keep leaf depth (=#guesses) and perhaps its solution path
            # to reduce memory of storing entire tree.
            info = _bucket_iterable(answer_index, score[guess_index_opt])
            node.key = (guess_opt, self._solver_data.answers[guess_opt], bucket_size)
            node.children = [
                Node(None, guesses, node.answers[bucket], score[:, bucket], [], hint=hint, parent=node)
                for hint, bucket in info.items()
            ]

def max_bucket_sizes(score) -> np.ndarray:
    if score.shape[1] <= 300:
        return np.array([collections.Counter(row).most_common(1)[0][1] for row in score])
    else:
        return scipy.stats.mode(score, axis=1, keepdims=False)[1]

def min_biased_multilevel_sampling(
        score, quantity, min_sample_size: int = 2000, sample_factor: float = 1.7,
        debug: bool = False):
    """Quantity is a functor that depends on the set of values of a row (i.e., its values is independent of column
    ordering."""
    # Ensure linear complexity.
    assert sample_factor < 2
    m, n = score.shape
    if n <= min_sample_size:
        return quantity(score)
    # 'rows' is the active set of rows. As we increase the sample, result[rows] becomes more accurate; we only
    # need a high accuracy in small result values, so we keep halving 'rows' in the loop below while increasing
    # the sample size. [log2(m)] + 1 repeats at most.
    rows, cols, result = np.arange(m, dtype=int), np.arange(n, dtype=int), np.zeros((m, ))
    sample_size = min_sample_size
    while rows.size:
        if debug:
            print("#rows", len(rows), "x sample_size", sample_size, "=", len(rows) * sample_size)
        # Select a random sample of the rows of size 'sample_size'.
        if sample_size > len(cols):
            raise ValueError("sample_size {} > #cols {}".format(sample_size, len(cols)))
        col_sample = np.random.choice(cols, size=sample_size, replace=False)
        # Calculate max bucket sizes for all elements in 'rows' using the current sample (overriding some old values).
        quantities = quantity(score[rows][:, col_sample])
        result[rows] = quantities
        if sample_size == n:
            break
        # Restrict rows to the smaller half of quantity value.
        #  If the quantity distribution is sufficiently spread so that the median is not repeated many times, the
        # loop halves len(rows), so there ~ log2(m) repeats. But what happens when the median is repeated?
        # Truncate the list 'i' to half the size of rows, filling it with values < median and some values = median.
        q_median = np.median(quantities)
        i = np.where(quantities < q_median)[0]
        i = np.concatenate((i, np.where(quantities == q_median)[0][:len(rows) // 2 - len(i)]))
        rows = rows[i]
        sample_size = min(int(sample_factor * sample_size), n)
    return result

class TreeDepthCalculator:
    def __init__(self, node: Node):
        self.depth = {}
        self._current_depth = 0
        self._calculate_tree_depth(node)

    def _calculate_tree_depth(self, node: Node):
        self._process(node)
        self._current_depth += 1
        for child in node.children:
            self._calculate_tree_depth(child)
        self._current_depth -= 1

    def _process(self, node):
        self.depth[node] = self._current_depth

def pre_traversal(
        node: Node,
        process_node,
        depth: int = 0,
        debug: bool = False):
    process_node(node)
    if debug: # and depth <= 1:
        print("\t" * depth, node)
    for child in node.children:
        pre_traversal(child, process_node, depth=depth + 1, debug=debug)

def _bucket_iterable(answers: Iterable, score: Iterable) -> Dict:
    s = collections.defaultdict(list)
    for a, v in zip(answers, score):
        s[v].append(a)
    return s


import sqlite3

def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d

class Db(object):
    def __init__(self, database, logger):
        self.connection = sqlite3.connect(database)
        self.connection.row_factory = dict_factory
        self.connection.execute('PRAGMA FOREIGN_KEYS=ON')
        self.logger = logger

    def close(self):
        self.connection.close()

    def insert_task(self, directory_key, task_type):
        with self.connection as connection:
            cursor = connection.cursor()
            try:
                cursor.execute("""
                INSERT INTO task
                (directory_key, type)
                VALUES (?, ?)""", (directory_key, task_type))
                self.logger.info('Insert task success.')
                connection.commit()
                return cursor.lastrowid
            except Exception as e:
                self.logger.error(e)
                connection.rollback()
                return 0
            finally:
                cursor.close()

    def insert_images(self, task_id, image_keys, annotations=None):
        if annotations is None:
            annotations = map(lambda _: None, image_keys)
        with self.connection as connection:
            cursor = connection.cursor()
            try:
                cursor.executemany("""
                INSERT INTO image
                (task_id, image_key, annotation)
                VALUES (?, ?, ?)""",
                [(task_id, image_key, annotation) for (image_key, annotation) in zip(image_keys, annotations)])
                connection.commit()
                self.logger.info('Insert images success.')
                return cursor.rowcount
            except Exception as e:
                self.logger.error(e)
                connection.rollback()
                return 0
            finally:
                cursor.close()

    def select_tasks(self):
        with self.connection as connection:
            cursor = connection.cursor()
            try:
                cursor.execute("""
                SELECT id task_id, directory_key, create_time
                FROM task
                ORDER BY task_id DESC""")
                self.logger.info('Select tasks success.')
                return cursor.fetchall()
            except Exception as e:
                self.logger.error(e)
                return []
            finally:
                cursor.close()

    def select_tasks_with_id(self, task_id):
        with self.connection as connection:
            cursor = connection.cursor()
            try:
                cursor.execute("""
                SELECT id task_id, directory_key, type, create_time
                FROM task
                WHERE id = ?""", (task_id,))
                self.logger.info('Select tasks success.')
                return cursor.fetchall()
            except Exception as e:
                self.logger.error(e)
                return []
            finally:
                cursor.close()

    def delete_tasks_with_id(self, task_id):
        with self.connection as connection:
            cursor = connection.cursor()
            try:
                cursor.execute("""
                DELETE FROM image
                WHERE task_id = ?""", (task_id,))
                cursor.execute("""
                DELETE FROM task
                WHERE id = ?""", (task_id,))
                self.logger.info('Delete tasks success.')
                return cursor.rowcount
            except Exception as e:
                self.logger.error(e)
                return None
            finally:
                cursor.close()

    def select_images_with_task(self, task_id, limit=None, offset=0):
        with self.connection as connection:
            cursor = connection.cursor()
            try:
                cmd = """
                SELECT id image_id, image_key, selected
                FROM image
                WHERE task_id = ?
                ORDER BY image_id ASC
                """
                args = [task_id,]
                if limit:
                    cmd += 'LIMIT ? '
                    args.append(limit)
                    if offset:
                        cmd += 'OFFSET ? '
                        args.append(offset)
                cursor.execute(cmd, args)
                self.logger.info('Select images success.')
                return cursor.fetchall()
            except Exception as e:
                self.logger.error(e)
                return []
            finally:
                cursor.close()

    def select_selected_images_with_task(self, task_id):
        with self.connection as connection:
            cursor = connection.cursor()
            try:
                cursor.execute("""
                SELECT image_key
                FROM image
                WHERE selected = 1
                AND task_id = ?
                ORDER BY id ASC""", (task_id,))
                return cursor.fetchall()
            except Exception as e:
                self.logger.error(e)
                return []
            finally:
                cursor.close()

    def select_images_with_id(self, image_id):
        with self.connection as connection:
            cursor = connection.cursor()
            try:
                cursor.execute("""
                SELECT id image_id, image_key, selected, task_id, annotation
                FROM image
                WHERE image_id = ?""", (image_id,))
                self.logger.info('Select images success.')
                return cursor.fetchall()
            except Exception as e:
                self.logger.error(e)
                return []
            finally:
                cursor.close()

    def update_image_selection(self, image_id, selected):
        with self.connection as connection:
            cursor = connection.cursor()
            try:
                cursor.execute("""
                UPDATE image
                SET selected = ?
                WHERE id = ?""", (selected, image_id))
                self.logger.info('Update image success.')
                connection.commit()
                return cursor.rowcount
            except Exception as e:
                self.logger.error(e)
                return 0
            finally:
                cursor.close()


print('program begins')
import numpy as np
import matplotlib.pyplot as plt
import time
from numba import jit

def integrand(a, b, x1, x2, y1, y2, z1, z2):
    '''
    Cartesian integral of the six dimensional case with limits a = -lambda and b = lambda
    '''
    tol = 1e-10
    x1 = (b-a)*x1/2 + (b+a)/2  # change of variables for [-1, 1] to [a, b]
    x2 = (b-a)*x2/2 + (b+a)/2
    y1 = (b-a)*y1/2 + (b+a)/2
    y2 = (b-a)*y2/2 + (b+a)/2
    z1 = (b-a)*z1/2 + (b+a)/2
    z2 = (b-a)*z2/2 + (b+a)/2

    r1 = np.sqrt(x1**2+y1**2+z1**2)
    r2 = np.sqrt(x2**2+y2**2+z2**2)

    distance = np.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2))

    if distance > tol:  # checking for singularities
        I = np.exp(-4*(r1+r2))/distance
    else:
        I = 0
    return I

def polar_integrand(a1, b1, a2, b2, r1, r2, t1, t2, p1, p2):
    """
    The spherical integration function which returns the function value of the six dimensional point
    """
    tol = 1e-10

    t1 = (b1-a1)*t1/2 + (b1+a1)/2
    t2 = (b1-a1)*t2/2 + (b1+a1)/2
    p1 = (b2-a2)*p1/2 + (b2+a2)/2
    p2 = (b2-a2)*p2/2 + (b2+a2)/2

    cosb = np.cos(t1)*np.cos(t2) + np.sin(t1)*np.sin(t2)*np.cos(p1-p2)

    r12 = r1**2+r2**2-2*r1*r2*cosb

    if r12 > tol:
        I = np.exp(-3*(r1+r2))*r1**2*r2**2*np.sin(t1)*np.sin(t2)/np.sqrt(r12)
    else:
        I = 0
    return I

def Gauss_leg(n):
    """
    Function which finds the roots and weights of the n-th legendre polynomial.
    """
    L_N = np.zeros(n+1)
    L_N[-1] = 1
    xi = np.polynomial.legendre.legroots(L_N)

    p1 = 1
    p2 = 0
    for j in range(n):
        p3 = p2
        p2 = p1
        p1 = ((2*j+1)*xi*p2 - j*p3)/(j+1)

    pp = n*(xi*p1-p2)/(xi**2-1)
    w = 2/((1-xi**2)*pp**2)

    return xi, w

def Gauss_lag(n):
    """
    Function which finds the roots and weights of the n-th laguerre polynomial.
    """
    L_N = np.zeros(n+1)
    L_N[-1] = 1
    xi = np.polynomial.laguerre.lagroots(L_N)

    p1 = 1
    p2 = 0
    for j in range(n+1):
        p3 = p2
        p2 = p1
        p1 = ((2*j+1-xi)*p2 - j*p3)/(j+1)

    w = xi/((n+1)**2*p1**2)

    return xi, w

if __name__ == '__main__':
    n = np.linspace(3,25,12)
    Leg_I = np.zeros(len(n))
    lamb = 3
    time_spent_leg = np.zeros(len(n))

    for g in range(len(n)):
        N = int(n[g])
        xi, w = Gauss_leg(N)

        a = -lamb
        b = lamb
        start = time.time()
        for i in range(N):  # the six-doubled for-loop to find the integration value
            for j in range(N):
                for k in range(N):
                    for l in range(N):
                        for m in range(N):
                            for h in range(N):
                                Leg_I[g] += w[i]*w[j]*w[k]*w[l]*w[m]*w[h]*integrand(a, b, xi[i], xi[j], xi[k], xi[l], xi[m], xi[h])
            print((i+1)/N*100)

        Leg_I[g] *= ((b-a)/2)**6
        end = time.time()
        time_spent_leg[g] = end-start

    plt.plot(n, Leg_I, 'bo-', label='Gauss Legendre for Cartesian')

    #  Laguerre part

    Lag_I = np.zeros(len(n))
    at = 0; bt = np.pi
    ap = 0; bp = 2 * np.pi
    time_spent_lag = np.zeros(len(n))

    for g in range(len(n)):
        N = int(n[g])
        ri, w_ri = Gauss_lag(N)
        angle, w_angle = Gauss_leg(N)

        start = time.time()
        for i in range(N):  # the six-doubled for-loop to find the integration value
            for j in range(N):
                for k in range(N):
                    for l in range(N):
                        for m in range(N):
                            for h in range(N):
                                Lag_I[g] += w_ri[i] * w_ri[j] * w_angle[k] * w_angle[l] * w_angle[m] * w_angle[h] \
                                        * polar_integrand(at, bt, ap, bp, ri[i], ri[j], angle[k], angle[l], angle[m], angle[h])
            print((i+1)/N*100)
        Lag_I[g] *= ((bt - at) / 2) ** 2 * ((bp - ap)/2)**2
        end = time.time()
        time_spent_lag[g] = end-start

    plt.plot(n, Lag_I, 'ro-', label='Gauss Laguerre for radial')
    plt.axhline(y=5*np.pi**2/16**2)
    plt.xlabel('mesh points, N')
    plt.ylabel('integration value')
    plt.legend()
    plt.show()

    file = open('solution_GQ.txt', 'w') #  Writing our solutions to a text file

    for i in range(len(n)):
        file.write('N=%g, Gauss_leg = %g, time = %g, Gauss_lag = %g, time = %g \n' % (n[i], Leg_I[i], time_spent_leg[i],
                                                                                Lag_I[i], time_spent_lag[i]))


#!/usr/bin/env python3

import json
import os
from datetime import datetime

overall_stations = {}

def process_file_contents(timestamp, contents):
    if "message" in contents:
        print(f"Message: \"{contents['message']}\" at time {timestamp}")
        return
    stations = contents["features"]
    processed = {}
    # print(datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S'))
    for station in stations:
        coords = station["geometry"]["coordinates"]
        coords.reverse()
        station_id = station["properties"]["station"]["id"]

        is_active = all(station["properties"]["station"][property_name]
                        for property_name in ["installed", "renting", "returning"])
        name = station["properties"]["station"]["name"]
        bikes = station["properties"]["station"]["bikes_available"]
        docks = station["properties"]["station"]["docks_available"]
        capacity = station["properties"]["station"]["capacity"]
        processed_entry = [is_active, bikes, docks, capacity]
        if "bike_angels" in station["properties"]:
            processed_entry.append(station["properties"]["bike_angels"]["score"])
        processed[station_id] = processed_entry

        station_entry = {
            "name": [[timestamp, name]],
            "coords": [[timestamp, coords]],
            "timestamp_added": timestamp
        }

        if station_id not in overall_stations:
            print(f"Adding station {name} ({station_id}) at time {datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')}")
            overall_stations[station_id] = station_entry
        else:
            for field_name in ["name", "coords"]:
                if overall_stations[station_id][field_name][-1][1] != station_entry[field_name][0][1]:
                    print(f"Station {name} ({station_id}) has changed its {field_name} from "
                          f"{overall_stations[station_id][field_name][-1][1]} to {station_entry[field_name][0][1]}")
                    overall_stations[station_id][field_name].append([timestamp, station_entry[field_name][0][1]])
    with open(f"data/processed_output/{timestamp}.json", "w") as file_stream:
        json.dump(processed, file_stream)

def main():
    for filename in os.listdir("raw_output/"):
        if os.stat(f"raw_output/{filename}").st_size == 0:
            continue
        with open(f"raw_output/{filename}") as file_stream:
            timestamp = int(filename.split(".")[0])
            contents = json.load(file_stream)
        process_file_contents(timestamp, contents)
    with open("data/overall_stations.json", "w") as file_stream:
        json.dump(overall_stations, file_stream)

if __name__ == "__main__":
    main()


import sys
import zipfile
from zipfile import ZipFile
import os
from os.path import basename
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job

import boto3
from botocore.exceptions import ClientError

## @params: [JOB_NAME]
args = getResolvedOptions(sys.argv, ['JOB_NAME'])

sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
logger = glueContext.get_logger()
job = Job(glueContext)
job.init(args['JOB_NAME'], args)

# Parameters
TARGET_BUCKET='your-bucket-name-with-rw-acces'

# Send file to S3
def upload_file(file_name, bucket, object_name=None):
    """Upload a file to an S3 bucket

    :param file_name: File to upload
    :param bucket: Bucket to upload to
    :param object_name: S3 object name. If not specified then file_name is used
    :return: True if file was uploaded, else False
    """

    # If S3 object_name was not specified, use file_name
    if object_name is None:
        object_name = os.path.basename(file_name)

    # Upload the file
    s3_client = boto3.client('s3')
    try:
        response = s3_client.upload_file(file_name, bucket, object_name)
    except ClientError as e:
        logger.error(e)
        return False
    return True

def zipfolder(zipFileName, target_dir):
    zipobj = zipfile.ZipFile(zipFileName, 'w', compression=zipfile.ZIP_DEFLATED)
    rootlen = len(target_dir) + 1
    for base, dirs, files in os.walk(target_dir):
        for file in files:
            fn = os.path.join(base, file)
            zipobj.write(fn, fn[rootlen:])

def env():
    logger.info('Env:')
    e = ""
    for k, v in sorted(os.environ.items()):
        e = e + f'\n{k} : {v}'
    logger.info(e)

def content():
    logger.info('Content:')
    out = ""
    for root, dirs, files in os.walk("/opt/amazon"):
        path = root.split(os.sep)
        out = out + f'\n{(len(path) - 1) * "---"} {os.path.basename(root)}'
        for file in files:
            out = out + f'\n{len(path) * "---"} {file}'
    logger.info(out)

content()
env()
zipfolder('/tmp/glue3.zip', '/opt/amazon')
upload_file('/tmp/glue3.zip', TARGET_BUCKET)

job.commit()


from curses import flash
from flask import Flask, Request, jsonify, redirect, request, url_for
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask import render_template
from datetime import datetime

from sqlalchemy import desc, func

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///library.db'
db = SQLAlchemy(app)

# Initialize Flask-Migrate with your Flask app and database
migrate = Migrate(app, db)

class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    isbn = db.Column(db.String(13), unique=True, nullable=False)
    title = db.Column(db.String(100), nullable=False)
    author = db.Column(db.String(100), nullable=False)

class Member(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    email = db.Column(db.String(100), unique=True, nullable=False)

class Borrowed(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    isbn_number = db.Column(db.Integer, db.ForeignKey('book.isbn'), nullable=False)
    book_name = db.Column(db.Integer, db.ForeignKey('book.title'), nullable=False)
    author_name = db.Column(db.Integer, db.ForeignKey('book.author'), nullable=False)
    member_name = db.Column(db.Integer, db.ForeignKey('member.name'), nullable=False)
    borrowed_date = db.Column(db.Date, nullable=False)

# book html adjustments
@app.route('/add_book', methods=['POST'])
def add_book():
    if request.method == 'POST':
        isbn = request.form['isbn']
        title = request.form['title']
        author = request.form['author']

        # Check if a book with the same ISBN already exists
        existing_book = Book.query.filter_by(isbn=isbn).first()

        if existing_book:
            # If the book already exists, update its information
            existing_book.title = title
            existing_book.author = author
            db.session.commit()
            # flash('Book updated successfully', 'success')
        else:
            # If the book doesn't exist, create a new record
            new_book = Book(isbn=isbn, title=title, author=author)
            db.session.add(new_book)
            db.session.commit()
            # flash('Book added successfully', 'success')

        return redirect(url_for('books'))

@app.route('/delete_book', methods=['POST'])
def delete_book():
    if request.method == 'POST':
        book_id = request.form['book_id']

        # Get the book to be deleted from the database
        book_to_delete = Book.query.get(book_id)
        if book_to_delete:
            # Delete the book
            db.session.delete(book_to_delete)
            db.session.commit()
        #     flash('Book deleted successfully', 'success')  # Flash a success message
        # else:
        #     flash('Book not found', 'error')  # Flash an error message

        return redirect(url_for('books'))  # Redirect back to the books page

# member html adjustments
@app.route('/add_member', methods=['POST'])
def add_member():
    if request.method == 'POST':
        id = request.form['memberID']
        name = request.form['Membername']  # Changed field name to match HTML form
        email = request.form['email']  # Changed field name to match HTML form

        # Check if a member with the same email already exists
        existing_member = Member.query.filter_by(email=email).first()

        if existing_member:
            # If the member already exists, update their information
            existing_member.name = name
            db.session.commit()
            # flash('Member updated successfully', 'success')
        else:
            # If the member doesn't exist, create a new member record
            new_member = Member(id=id, name=name, email=email)
            db.session.add(new_member)
            db.session.commit()
            # flash('Member added successfully', 'success')
    return redirect(url_for('members'))

@app.route('/delete_member', methods=['POST'])
def delete_member():
    if request.method == 'POST':
        member_id = request.form['member_id']

        # Get the book to be deleted from the database
        member_to_delete = Member.query.get(member_id)
        if member_to_delete:
            # Delete the book
            db.session.delete(member_to_delete)
            db.session.commit()
        #     flash('Book deleted successfully', 'success')  # Flash a success message
        # else:
        #     flash('Book not found', 'error')  # Flash an error message

        return redirect(url_for('members'))  # Redirect back to the books page

# Add a route to handle lending books
@app.route('/lend_book', methods=['POST'])
def lend_book():
    if request.method == 'POST':
        isbn = request.form['isbn']
        title = request.form['bookTitle']
        author = request.form['author']
        member = request.form['memberName']
        due_date_str = request.form['dueDate']

        # Convert the due_date_str to a Python date object
        due_date = datetime.strptime(due_date_str, '%Y-%m-%d').date()

        # Create a new Borrowed record in the database
        new_borrowed = Borrowed(
            isbn_number=isbn,
            book_name=title,
            author_name=author,
            member_name=member,
            borrowed_date=due_date
        )
        db.session.add(new_borrowed)
        db.session.commit()

        # Redirect to the checkout page
        return redirect(url_for('checkout'))

# Add a route to handle returning books
@app.route('/return_book', methods=['GET', 'POST'])
def return_book(book_id):
    if request.method == 'POST':
        # Get the borrowed book record by ID and delete it from the database
        borrowed_book = Borrowed.query.get(book_id)
        if borrowed_book:
            db.session.delete(borrowed_book)
            db.session.commit()

    # Redirect to the checkout page
    return redirect(url_for('checkout'))

@app.route('/search')
def search():
    search_query = request.args.get('q')
    # Perform the search in your database (e.g., books)
    search_results = Book.query.filter(Book.title.ilike(f"%{search_query}%")).all()
    return render_template('search_results.html', search_query=search_query, results=search_results)

def highlight_keywords(text, keywords):
    for keyword in keywords:
        text = text.replace(keyword, f'{keyword}')
    return text

@app.route('/')
def index():
    return render_template('Dashboard.html')

@app.route('/Books.html')
def books():
    # Fetch all books from the database
    books = Book.query.all()
    book_count = Book.query.count()
    return render_template('Books.html', books=books, book_count=book_count)

@app.route('/Members.html')
def members():
    # Fetch all members from the database
    members = Member.query.all()
    member_count = Member.query.count()
    return render_template('Members.html', members=members, member_count=member_count)

@app.route('/checkout.html')
def checkout():
    books = Book.query.all()
    members = Member.query.all()
    borrowed_books = Borrowed.query.all()
    borrowed_count = len(borrowed_books)  # Calculate the count of borrowed books
    return render_template('checkout.html', books=books, members=members, borrowed_books=borrowed_books, borrowed_count=borrowed_count)

@app.route('/Dashboard.html')
def dashboard():
    # Fetch the top 5 latest books based on their creation date
    latest_books = Book.query.limit(5).all()
    book_count = Book.query.count()
    top_borrowers = db.session.query(Member, func.count(Borrowed.id).label('total_borrowed'))\
        .join(Borrowed, Member.name == Borrowed.member_name)\
        .group_by(Member.id)\
        .order_by(desc('total_borrowed'))\
        .limit(3)\
        .all()

    return render_template('Dashboard.html', books=latest_books, book_count=book_count, top_borrowers=top_borrowers)

if __name__ == '__main__':
    app.run()


from __future__ import annotations

from io import BytesIO
from typing import Any, Tuple

import better_bencode  # type: ignore

class BencoderError(Exception):
    pass

def encode(obj: Any) -> bytes:
    try:
        return better_bencode.dumps(obj)
    except Exception as e:
        raise BencoderError(e)

def decode(bytes_object: bytes) -> dict:
    try:
        return better_bencode.loads(bytes_object)
    except Exception as e:
        raise BencoderError(e)

def decode2(bytes_object: bytes) -> Tuple[Any, int]:
    bytes_io = BytesIO(bytes_object)
    try:
        return better_bencode.load(bytes_io), bytes_io.tell()
    except Exception as e:
        raise BencoderError(e)


# used for late-initialize wandb in case of crash before a full evaluation (which add an idle log on the monitoring panel)
import collections
import wandb
import os
from utils import pickle_util

cache = collections.defaultdict(list)

is_inited = False

def save(the_path):
    if is_inited:
        wandb.save(the_path)
    else:
        cache["save"].append(the_path)

def update_config(obj):
    if is_inited:
        wandb.config.update(obj)
    else:
        cache["config"].append(obj)

def log(obj):
    if is_inited:
        wandb.log(obj)
    else:
        cache["log"].append(obj)

def init(args):
    init_core(args.project, args.name, args.dryrun)

def init_core(project, name, dryrun):
    global is_inited
    if is_inited:
        return
    is_inited = True

    if dryrun:
        os.environ['WANDB_MODE'] = 'dryrun'
        wandb.log = do_nothing
        wandb.save = do_nothing
        wandb.watch = do_nothing
        wandb.config = {}
        print("wb dryrun mode")
        return

    init_based_on_config_file(project, name)

def init_based_on_config_file(project, name, config_path=".wb_config.json"):
    assert os.path.exists(config_path)
    json_dict = pickle_util.read_json(config_path)

    # use self-hosted wb server
    key = "WANDB_BASE_URL"
    if key in json_dict:
        os.environ[key] = json_dict[key]

    # login
    wandb.login(key=json_dict["WB_KEY"])
    wandb.init(project=project, name=name)
    print("wandb inited")

    # supplement config and logs
    for obj in cache["config"]:
        wandb.config.update(obj)

    for log in cache["log"]:
        wandb.log(log)

    for the_path in cache["save"]:
        wandb.save(the_path)

def do_nothing(v):
    pass


from collections import defaultdict
from array import array

from subroutines.core import core
from subroutines.commons import *
from utilities.time_measure import ExecutionTime

def breadth_first(multilayer_graph, print_file, distinct_flag):
    # measures
    number_of_cores = 0
    number_of_computed_cores = 0

    # start of the algorithm
    execution_time = ExecutionTime()

    # create the vector of zeros from which start the computation
    start_vector = tuple([0] * multilayer_graph.number_of_layers)

    # dict of cores
    cores = {}

    # core [0]
    if print_file is not None and not distinct_flag:
        print_file.print_core(start_vector, array('i', multilayer_graph.get_nodes()))
    elif distinct_flag:
        cores[start_vector] = array('i', multilayer_graph.get_nodes())
    number_of_cores += 1

    # initialize the queue of vectors with the descendants of start_vector and the structure that for each vector saves its ancestor vectors
    vectors_queue = deque()
    ancestors = {}
    for index in multilayer_graph.layers_iterator:
        descendant_vector = build_descendant_vector(start_vector, index)
        vectors_queue.append(descendant_vector)
        ancestors[descendant_vector] = [start_vector]

    # initialize the dictionary that for each vector counts the number of descendants in the queue
    descendants_count = defaultdict(int)

    # while vectors_queue is not empty
    while len(vectors_queue) > 0:
        # remove a vector from vectors_queue (FIFO policy)
        vector = vectors_queue.popleft()

        # if the number of non zero indexes of vector is equal to the number of its ancestors, build the intersection of its ancestor cores
        number_of_non_zero_indexes = len([index for index in vector if index > 0])
        number_of_ancestors = len(ancestors[vector])
        if number_of_non_zero_indexes == number_of_ancestors:
            ancestors_intersection = build_ancestors_intersection(ancestors[vector], cores, descendants_count, distinct_flag, multilayer_graph=multilayer_graph)

            # if the intersection of its ancestor cores is not empty
            if len(ancestors_intersection) > 0:
                # compute the core from it
                k_core = core(multilayer_graph, vector, ancestors_intersection)
                number_of_computed_cores += 1
            # otherwise
            else:
                # delete its entry from ancestors and continue
                del ancestors[vector]
                continue

            # if the core is not empty
            if len(k_core) > 0:
                # add the core to the dict of cores and increment the number of cores
                cores[vector] = k_core
                number_of_cores += 1
                if print_file is not None and not distinct_flag:
                    print_file.print_core(vector, k_core)

                # compute its descendant vectors
                for index in multilayer_graph.layers_iterator:
                    descendant_vector = build_descendant_vector(vector, index)

                    try:
                        # update the list of the ancestors of the descendant vector
                        ancestors[descendant_vector].append(vector)

                    # if the descendant vector has not already been found
                    except KeyError:
                        # add the descendant vector to the queue
                        vectors_queue.append(descendant_vector)
                        ancestors[descendant_vector] = [vector]

                    # increment descendants_count
                    descendants_count[vector] += 1
        else:
            # for each ancestor of vector
            for ancestor in ancestors[vector]:
                # decrement its number of descendants
                decrement_descendants_count(ancestor, cores, descendants_count, distinct_flag)

        # delete vector's entry from ancestors
        del ancestors[vector]

    # end of the algorithm
    execution_time.end_algorithm()

    # print algorithm's results
    print_end_algorithm(execution_time.execution_time_seconds, number_of_cores, number_of_computed_cores)

    # execute the post processing
    post_processing(cores, distinct_flag, print_file)


import numpy as np

from Common import get_plot_type, get_db_connection, close_db, get_logger, show_information_message, execute_inquiry, \
    get_row_model, init_tableview

from PyQt5.QtWidgets import QMainWindow, QApplication, QGridLayout, QHeaderView

from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar

import seaborn as sns  #https://seaborn.apachecn.org/#/docs/12   seaborn中文文档

import pandas as pd

from PyQt5.QtCore import pyqtSlot, QItemSelectionModel, QModelIndex

from PyQt5.QtGui import QStandardItem, QStandardItemModel

from UI.Ui_PlotSubWindow import Ui_PlotSubWindow

from Util.Plot import Myplot2D

#https://seaborn.apachecn.org/#/docs/12   seaborn中文文档

#这块需要信号的方式传递横纵坐标，还有折线图与饼状图的选择#

class PlotSubWindow(QMainWindow):

    def __init__(self, plot_type=-1, row = None, line = None, data = None, type_label = None, parent=None):
        super(PlotSubWindow, self).__init__(parent)
        self.UI = Ui_PlotSubWindow()
        self.UI.setupUi(self)

        self.row = row  #横轴
        self.line = line  #纵轴
        self.plot_type = plot_type  #画图种类 1：代表折线图 0：代表柱状图
        self.type_label = type_label  #画图标签

        self.fig_line = None
        #self.plot_type = None

        self.init_plot_frame()
        if plot_type == 0:  # 柱状图
            self.plt_multi_colums(row, line, type_label)
        elif plot_type == 1:
            self.plt_multi_line(row, line, type_label)

        #self.plot_line_or_pie(plot_type, row, line, type_label)

        ##################################加tableView功能######################
        if data is not None:
            self.init_model = None
            init_tableview(self.UI.tableView, hor_size=50, ver_size=50)
            self.get_initial_model(data, type_label)
            data_model = self.init_model
            selection_model = QItemSelectionModel(data_model)

            self.UI.tableView.setModel(data_model)
            self.UI.tableView.setSelectionModel(selection_model)
            # 设置表格充满这个布局QHeaderView
            self.UI.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)  # 所有列自动拉伸，充满界面

        #####################################################################

    # 初始化画图区域

    def init_plot_frame(self):
        self.fig_line = Myplot2D()
        #tool = NavigationToolbar(self.fig_line, self.UI.frame_5)
        layout = QGridLayout()
        layout.addWidget(self.fig_line)
        #layout.addWidget(tool)
        self.UI.frame_5.setLayout(layout)

    #根据横纵列表以及画图种类进行画图
    def plot_line_or_pie(self, plot_type: int, row: list, line: list, type_label: str):

        if plot_type == -1:
            return
        elif plot_type == 0:  #折线图
            if len(line) == 11:
                line = list(['低', '低', '较低', '较高', '高', '较高', '低', '低', '较低', '较高', '较低'])
            else:
                line = list(['较高'])
            #self.plot_line(row, line, type_label)
            self.plot_colums(row, line, type_label, 'red')
            self.plot_colums(row, list(['较低', '较高', '较低','低', '低', '较低', '较高', '高', '较高', '低', '低']), type_label, 'blue')
        else:
            #self.plot_pie(row, line, type_label)
            self.plot_line(row, line, type_label)

    # 绘制折线图，三根线 有最高、平均、最低，折线图 比较简单，容易理解
    def plot_line(self, x: list, values: list, type_label: str, grid=True):
        self.fig_line.axes.plot(x,
                                values[0],
                                '-',
                                color='r',
                                marker='h',
                                label=type_label + "折线图")

        self.fig_line.axes.set_title(type_label + "折线图")
        self.fig_line.axes.set_xticks(x, minor=True)

        if type == '安装时长':
            self.fig_line.axes.set_xlabel(type + '(月)')
        elif type == '环境温度':
            self.fig_line.axes.set_xlabel(type + '(℃)')
        self.fig_line.axes.set_ylabel('风险等级')

        # self.fig_line.axes.set_gcf().autofmt_xdate() # 自动旋转日期标记
        if grid:
            self.fig_line.axes.grid(True, linestyle='-.')

        self.fig_line.axes.legend(loc='best', ncol=1)
        self.fig_line.draw()

    # 绘制柱状图，柱状图该怎么画出那种高、中、低效果，待研究2022/2/22 23:56
    def plot_colums(self, x: list, values: list, type: str, color: str, grid=False):
        self.fig_line.axes.bar(x=x,
                               height=values,
                               align='center',
                               label='asdadssadasd',
                               color=color)
        self.fig_line.axes.set_title(type + "柱状图")
        if grid:
            self.fig_line.axes.grid(True)

        self.fig_line.axes.legend()
        self.fig_line.draw()

    def plt_multi_line(self, row=None, line=None, type_label=None):
        reward = list([])
        reward2 = list([])
        reward3 = list([])

        for i in range(0, len(row)):
            if type_label == '安装时长':
                row[i] = float(row[i])
            reward.append(float(line[0][i]))
            reward2.append(float(line[1][i]))
            reward3.append(float(line[2][i]))
        episode = row
        '''
        episode = [23]
        reward = [35]
        reward2 = [34]
        reward3 = [33]
        '''
        RL1_date = pd.DataFrame({'iteration': episode, 'reward': reward, 'algo': '最高指数', 'style': '*'})
        RL2_data = pd.DataFrame({'iteration': episode, 'reward': reward2, 'algo': '平均指数', 'style': 'h'})
        RL3_data = pd.DataFrame({'iteration': episode, 'reward': reward3, 'algo': '最低指数', 'style': 'v'})

        frames = [RL1_date, RL2_data, RL3_data]
        # pd.concat(frames).plot(kind="bar", x="iteration", y="reward", ax=self.fig_line.axes)
        dataset = pd.concat(frames)
        self.fig_line.axes.grid(True, linestyle='-.')

        sns.set_context("paper", font_scale=1.3, rc={"lines.linewidth": 1.5})

        #  palette：调色板
        ax = sns.relplot(x="iteration", y="reward", ax=self.fig_line.axes, palette=['r', 'b', 'g'], hue="algo", ci="5",
                         kind="line", markers=True, style='style',
                         data=dataset, legend='brief')

        x_ticks = None

        if type_label == '温度':  # 温度规定，温度范围从-20℃~50℃，以2℃间隔段为一个点。
            x_ticks = np.arange(-20, 50, 2.0)
            self.fig_line.axes.set_xticks(x_ticks)
            self.fig_line.axes.set_xlabel("温度（℃）", fontsize=13)
        elif type_label == '安装时长':
            #x_ticks = np.arange(1, 4, 1)
            #self.fig_line.axes.set_xticks(x_ticks)
            self.fig_line.axes.set_xlabel("安装时长（月）", fontsize=13)
        elif type_label == '使用频率':
            self.fig_line.axes.set_xlabel("使用频率", fontsize=13)

        self.fig_line.axes.set_ylabel("风险等级指数", fontsize=13)

        self.fig_line.axes.legend(title='', loc='lower right', labels=["最高指数", "平均指数", "最低指数"])

    # 绘制柱状图，画出那种高、中、低效果 主要
    def plt_multi_colums(self, row=None, line=None, type_label=None):
        '''
        episode = [1, 2, 3]
        reward = [5, 8, 5]
        reward2 = [4, 6, 6]
        reward3 = [6, 4, 7]
        '''
        episode = row
        reward = line[0]
        reward2 = line[1]
        reward3 = line[2]

        RL1_date = pd.DataFrame({'iteration': episode, 'reward': reward, '等级指数': '最高指数', 'palette': 'r'})
        RL2_data = pd.DataFrame({'iteration': episode, 'reward': reward2, '等级指数': '平均指数', 'palette': 'b'})
        RL3_data = pd.DataFrame({'iteration': episode, 'reward': reward3, '等级指数': '最低指数', 'palette': 'g'})

        frames = [RL1_date, RL2_data, RL3_data]
        #pd.concat(frames).plot(kind="bar", x="iteration", y="reward", ax=self.fig_line.axes)
        dataset = pd.concat(frames)

        #  palette：调色板

        ax = sns.catplot(x="iteration", y="reward", ax=self.fig_line.axes, palette=['r', 'b', 'g'], hue="等级指数", ci="sd", kind="bar", data=dataset, legend=True)

        #sns.despine(top=False, right=False, left=False, bottom=False)
        #ax.set(xlabel='Iteration', ylabel='风险等级指数')
        self.fig_line.axes.set_ylabel("风险等级指数", fontsize=13)
        if type_label == '生产厂家':
            self.fig_line.axes.set_xlabel("生产厂家", fontsize=13)
        elif type_label == '运营商':
            self.fig_line.axes.set_xlabel("运营商", fontsize=13)
        elif type_label == '使用频率':
            self.fig_line.axes.set_xlabel("使用频率", fontsize=13)
        #self.fig_line.axes.legend(title='', loc='upper right', labels=["最高指数", "平均指数", "最低指数"])
        #plt.show()
        #self.fig_line.draw_idle()

    # 绘制饼状图
    def plot_pie(self, x: list, values: list, type: str, grid=False):
        self.fig_line.axes.pie(values,
                              labels=x,
                              autopct='%1.1f%%',
                              shadow=True,
                              startangle=150)

        self.fig_line.axes.set_title(type + "饼状图")
        if grid:
            self.fig_line.axes.grid(True)
        self.fig_line.axes.legend()
        self.fig_line.draw()

    # 添加表格到tableView区 2022/2/27

    # 获取最初的数据模型
    def get_initial_model(self, data=None, type_label=None):
        head = None
        if type_label == '安装时长':
            head = list(['安装时长（月）', '最高（风险等级指数）台数', '平均（...）', '最低（...）'])
        elif type_label == '生产厂家':
            head = list(['生产厂家', '最高（风险等级指数）台数', '平均（...）', '最低（...）'])
        elif type_label == '运营商':
            head = list(['运营商', '最高（风险等级指数）台数', '平均（...）', '最低（...）'])
        elif type_label == '使用频率':
            head = list(['使用频率', '最高（风险等级指数）台数', '平均（...）', '最低（...）'])
        elif type_label == '温度':
            head = list(['温度（℃）', '最高（风险等级指数）台数', '平均（...）', '最低（...）'])
        model = get_row_model(2, header=head)
        self.init_model = self.add_data(model, data)

    # 向模型添加数据
    def add_data(self, model: QStandardItemModel, data) -> QStandardItemModel:
        #line1 = list(['', 0, 3, 0])
        for i in data:
            row = []
            for j in range(len(i)):
                item = QStandardItem(str(i[j]))
                row.append(item)
            model.appendRow(row)
        return model

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    win = PlotSubWindow()
    win.show()
    sys.exit(app.exec())


from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit, Row, Column
from django.forms import (modelform_factory, ModelForm, )
from django.utils.translation import gettext_lazy as _

from . import models as bm

class _PessoaFisicaForm(ModelForm):
    class Meta:
        model = bm.PessoaFisica

        fields = (
            'cpf',
            'nome_razao_social',
            'sexo',
            'estado_civil',
            'tipo_sanguineo',
            'natural_cidade',
            'natural_uf',
            'nacionalidade',
            'falecido',
        )

        labels = {
            'cpf': _('CPF'),
            'nome_razao_social': _('Nome completo'),
            'tipo_sanguineo': _('Tipo Sanguíneo'),
            'natural_uf': _('Natural UF'),
        }

        extra_required = {
            'sexo': True,
            'estado_civil': True,
            'tipo_sanguineo': True,
            'natural_cidade': True,
            'natural_uf': True,
            'nacionalidade': True,
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # set extra required fields
        for field, required in self.Meta.extra_required.items():
            self.fields[field].required = required

        self.helper = FormHelper()
        self.helper.layout = Layout(
            Row(
                Column('cpf', css_class='form-group col-md-4 mb-0'),
                Column('nome_razao_social', css_class='form-group col-md-8 mb-0'),
            ),
            Row(
                Column('sexo', css_class='form-group col-md-4 mb-0'),
                Column('estado_civil', css_class='form-group col-md-5 mb-0'),
                Column('tipo_sanguineo', css_class='form-group col-md-3 mb-0'),
            ),
            Row(
                Column('nacionalidade', css_class='form-group col-md-3 mb-0'),
                Column('natural_cidade', css_class='form-group col-md-6 mb-0'),
                Column('natural_uf', css_class='form-group col-md-3 mb-0'),
            ),
            'falecido',
            Submit('submit', 'Salvar'),
        )

PessoaFisicaForm = modelform_factory(bm.PessoaFisica, form=_PessoaFisicaForm)


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

import os
import datetime
import operations

def date_to_string(date):
    """ Convert date to string 'Month-Year'. """
    date = datetime.datetime(date.year, date.month, date.day)
    date = date.strftime("%B-%Y")
    return date

def get_dates_of_item(item, path):
    """ Return created and modified date of folder or file. """
    cdate = datetime.datetime.fromtimestamp(
        os.path.getctime(
            os.path.join(path, item)
        )
    )
    mdate = datetime.datetime.fromtimestamp(
        os.path.getmtime(
            os.path.join(path, item)
        )
    )
    cdate = date_to_string(cdate)
    mdate = date_to_string(mdate)
    return cdate, mdate

def get_sets_of_dates(path):
    """ Return set of created dates and set of modified dates. """
    list_of_files_and_folders = operations.get_list_of_files_and_folders(path)

    list_of_cdates, list_of_mdates = [], []
    for item in list_of_files_and_folders:
        cdate, mdate = get_dates_of_item(item, path)

        list_of_cdates.append(cdate)
        list_of_mdates.append(mdate)

    return set(list_of_cdates), set(list_of_mdates)

def get_names_of_new_folders(mdate, path):
    """ Return list of names of folders to create. """
    list_of_cdates, list_of_mdates = get_sets_of_dates(path)
    return list_of_mdates if mdate else list_of_cdates

def move_files_and_folders(mdate, source, target):
    """ Move all files and folders to given target. """
    files_and_folders = operations.get_list_of_files_and_folders(source)

    list_of_tuples = []
    if mdate:
        for obj in files_and_folders:
            _, obj_mdate = get_dates_of_item(obj, source)
            list_of_tuples.append((obj, obj_mdate))
    else:
        for obj in files_and_folders:
            obj_cdate, _ = get_dates_of_item(obj, source)
            list_of_tuples.append((obj, obj_cdate))

    operations.move_all(list_of_tuples, source, target)

def by_cdate(source, target):
    """ Group files and folders by created date. """
    folders_name = get_names_of_new_folders(False, source)
    operations.create_new_folders(folders_name, target)
    move_files_and_folders(False, source, target)

def by_mdate(source, target):
    """ Group files and folders by modified date. """
    folders_name = get_names_of_new_folders(True, source)
    operations.create_new_folders(folders_name, target)
    move_files_and_folders(True, source, target)


import numpy as np
from cv2 import cv2

def data2img(data, normalization = 1, histogram = 0):  # normalization, type castinf(uint8). gray2rgb
    if normalization == 1:
        data = cv2.normalize(src=data, dst=None, beta=0, alpha=255, norm_type=cv2.NORM_MINMAX)
    data = np.uint8(data)
    if histogram == 1:
        data = cv2.equalizeHist(data)
    if len(data.shape) == 2:
        data = np.stack((data,)*3, axis=2)
    return data

def main():
    # video setting
    capture = cv2.VideoCapture(2)   # 비디오 캡처, video 장치 번호
    capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)  # width
    capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) # height

    # model setting
    windowsize = 3
    left_matcher = cv2.StereoSGBM_create(minDisparity=0,blockSize=5,P1=8 * 3 * windowsize ** 2, P2=32 * 3 * windowsize ** 2,)
    right_matcher = cv2.ximgproc.createRightMatcher(left_matcher)
    wls_filter = cv2.ximgproc.createDisparityWLSFilter(matcher_left=left_matcher)

    while True:
        # input img
        ret, frame = capture.read() # camera 읽기
        h, w, c = frame.shape
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        imgL = gray[:,:int(w/2)]
        imgR = gray[:,int(w/2):]

        # compute
        displ = left_matcher.compute(imgL, imgR)
        dispr = right_matcher.compute(imgR, imgL)
        displ = np.int16(displ)
        dispr = np.int16(dispr)
        filteredImg = wls_filter.filter(displ, imgL, None, dispr)

        # change to show img
        displ2 = data2img(displ)
        dispr2 = data2img(dispr)
        filteredImg1 = data2img(filteredImg, 0)
        filteredImg2 = data2img(filteredImg, 1, 1)

        # display
        final_frame = np.hstack([frame, filteredImg1]) # concatenation
        disps = np.hstack([displ2, dispr2, filteredImg2])   # concatenation
        final_frame = np.vstack([final_frame, disps])   # concatenation
        cv2.imshow("VideoFrame", final_frame) # 영상 출력

        if cv2.waitKey(1) > 0: break    # 키보드 입력이 들어오면 종료

    capture.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()