#!/user/bin/env python3
# Note that all the tests in this module require dataset (either network access or cached)
import os
import glob
import shutil
import torchtext.data as data
from torchtext.datasets import AG_NEWS
import torch
from ..common.torchtext_test_case import TorchtextTestCase

def conditional_remove(f):
    for path in glob.glob(f):
        if os.path.isfile(path):
            os.remove(path)
        elif os.path.isdir(path):
            shutil.rmtree(path)

class TestDataset(TorchtextTestCase):
    def test_wikitext2_legacy(self):
        from torchtext.datasets import WikiText2
        # smoke test to ensure wikitext2 works properly

        # NOTE
        # test_wikitext2 and test_wikitext2_legacy have some cache incompatibility.
        # Keeping one's cache make the other fail. So we need to clean up the cache dir
        cachedir = os.path.join(self.project_root, ".data", "wikitext-2")
        conditional_remove(cachedir)

        ds = WikiText2
        TEXT = data.Field(lower=True, batch_first=True)
        train, valid, test = ds.splits(TEXT)
        TEXT.build_vocab(train)
        train_iter, valid_iter, test_iter = data.BPTTIterator.splits(
            (train, valid, test), batch_size=3, bptt_len=30)

        train_iter, valid_iter, test_iter = ds.iters(batch_size=4,
                                                     bptt_len=30)

        conditional_remove(cachedir)

    def test_wikitext2(self):
        from torchtext.experimental.datasets import WikiText2
        # smoke test to ensure wikitext2 works properly

        # NOTE
        # test_wikitext2 and test_wikitext2_legacy have some cache incompatibility.
        # Keeping one's cache make the other fail. So we need to clean up the cache dir
        cachedir = os.path.join(self.project_root, ".data", "wikitext-2")
        conditional_remove(cachedir)
        cachefile = os.path.join(self.project_root, ".data", "wikitext-2-v1.zip")
        conditional_remove(cachefile)

        train_dataset, test_dataset, valid_dataset = WikiText2()
        self.assertEqual(len(train_dataset), 2049990)
        self.assertEqual(len(test_dataset), 241859)
        self.assertEqual(len(valid_dataset), 214417)

        vocab = train_dataset.get_vocab()
        tokens_ids = [vocab[token] for token in 'the player characters rest'.split()]
        self.assertEqual(tokens_ids, [2, 286, 503, 700])

        conditional_remove(cachedir)
        conditional_remove(cachefile)

    def test_penntreebank_legacy(self):
        from torchtext.datasets import PennTreebank
        # smoke test to ensure penn treebank works properly
        TEXT = data.Field(lower=True, batch_first=True)
        ds = PennTreebank
        train, valid, test = ds.splits(TEXT)
        TEXT.build_vocab(train)
        train_iter, valid_iter, test_iter = data.BPTTIterator.splits(
            (train, valid, test), batch_size=3, bptt_len=30)

        train_iter, valid_iter, test_iter = ds.iters(batch_size=4,
                                                     bptt_len=30)

    def test_penntreebank(self):
        from torchtext.experimental.datasets import PennTreebank
        # smoke test to ensure penn treebank works properly
        train_dataset, test_dataset, valid_dataset = PennTreebank()
        self.assertEqual(len(train_dataset), 924412)
        self.assertEqual(len(test_dataset), 82114)
        self.assertEqual(len(valid_dataset), 73339)

        vocab = train_dataset.get_vocab()
        tokens_ids = [vocab[token] for token in 'the player characters rest'.split()]
        self.assertEqual(tokens_ids, [2, 2550, 3344, 1125])

    def test_text_classification(self):
        # smoke test to ensure ag_news dataset works properly

        datadir = os.path.join(self.project_root, ".data")
        if not os.path.exists(datadir):
            os.makedirs(datadir)
        ag_news_train, ag_news_test = AG_NEWS(root=datadir, ngrams=3)
        self.assertEqual(len(ag_news_train), 120000)
        self.assertEqual(len(ag_news_test), 7600)
        self.assertEqual(ag_news_train[-1][1][:10],
                         torch.tensor([3525, 319, 4053, 34, 5407, 3607, 70, 6798, 10599, 4053]).long())
        self.assertEqual(ag_news_test[-1][1][:10],
                         torch.tensor([2351, 758, 96, 38581, 2351, 220, 5, 396, 3, 14786]).long())

    def test_imdb(self):
        from torchtext.experimental.datasets import IMDB
        from torchtext.vocab import Vocab
        # smoke test to ensure imdb works properly
        train_dataset, test_dataset = IMDB()
        self.assertEqual(len(train_dataset), 25000)
        self.assertEqual(len(test_dataset), 25000)
        self.assertEqual(train_dataset[0][1][:10],
                         torch.tensor([13, 1568, 13, 246, 35468, 43, 64, 398, 1135, 92]).long())
        self.assertEqual(train_dataset[-1][1][:10],
                         torch.tensor([2, 71, 4555, 194, 3328, 15144, 42, 227, 148, 8]).long())
        self.assertEqual(test_dataset[0][1][:10],
                         torch.tensor([13, 125, 1051, 5, 246, 1652, 8, 277, 66, 20]).long())
        self.assertEqual(test_dataset[-1][1][:10],
                         torch.tensor([13, 1035, 14, 21, 28, 2, 1051, 1275, 1008, 3]).long())

        # Test API with a vocab input object
        old_vocab = train_dataset.get_vocab()
        new_vocab = Vocab(counter=old_vocab.freqs, max_size=2500)
        new_train_data, new_test_data = IMDB(vocab=new_vocab)

    def test_multi30k(self):
        from torchtext.experimental.datasets import Multi30k
        # smoke test to ensure multi30k works properly
        train_dataset, valid_dataset, test_dataset = Multi30k()
        self.assertEqual(len(train_dataset), 29000)
        self.assertEqual(len(valid_dataset), 1000)
        self.assertEqual(len(test_dataset), 1014)

        de_vocab, en_vocab = train_dataset.get_vocab()
        de_tokens_ids = [
            de_vocab[token] for token in
            'Zwei Männer verpacken Donuts in Kunststofffolie'.split()
        ]
        self.assertEqual(de_tokens_ids, [19, 29, 18703, 4448, 5, 6240])

        en_tokens_ids = [
            en_vocab[token] for token in
            'Two young White males are outside near many bushes'.split()
        ]
        self.assertEqual(en_tokens_ids,
                         [17, 23, 1167, 806, 15, 55, 82, 334, 1337])

        datafile = os.path.join(self.project_root, ".data", "train*")
        conditional_remove(datafile)
        datafile = os.path.join(self.project_root, ".data", "val*")
        conditional_remove(datafile)
        datafile = os.path.join(self.project_root, ".data", "test*")
        conditional_remove(datafile)
        datafile = os.path.join(self.project_root, ".data",
                                "multi30k_task*.tar.gz")
        conditional_remove(datafile)

    def test_udpos_sequence_tagging(self):
        from torchtext.experimental.datasets import UDPOS

        # smoke test to ensure imdb works properly
        train_dataset, valid_dataset, test_dataset = UDPOS()
        self.assertEqual(len(train_dataset), 12543)
        self.assertEqual(len(valid_dataset), 2002)
        self.assertEqual(len(test_dataset), 2077)
        self.assertEqual(train_dataset[0][0][:10],
                         torch.tensor([262, 16, 5728, 45, 289, 701, 1160, 4436, 10660, 585]).long())
        self.assertEqual(train_dataset[0][1][:10],
                         torch.tensor([8, 3, 8, 3, 9, 2, 4, 8, 8, 8]).long())
        self.assertEqual(train_dataset[0][2][:10],
                         torch.tensor([5, 34, 5, 27, 7, 11, 14, 5, 5, 5]).long())
        self.assertEqual(train_dataset[-1][0][:10],
                         torch.tensor([9, 32, 169, 436, 59, 192, 30, 6, 117, 17]).long())
        self.assertEqual(train_dataset[-1][1][:10],
                         torch.tensor([5, 10, 11, 4, 11, 11, 3, 12, 11, 4]).long())
        self.assertEqual(train_dataset[-1][2][:10],
                         torch.tensor([6, 20, 8, 10, 8, 8, 24, 13, 8, 15]).long())

        self.assertEqual(valid_dataset[0][0][:10],
                         torch.tensor([746, 3, 10633, 656, 25, 1334, 45]).long())
        self.assertEqual(valid_dataset[0][1][:10],
                         torch.tensor([6, 7, 8, 4, 7, 2, 3]).long())
        self.assertEqual(valid_dataset[0][2][:10],
                         torch.tensor([3, 4, 5, 16, 4, 2, 27]).long())
        self.assertEqual(valid_dataset[-1][0][:10],
                         torch.tensor([354, 4, 31, 17, 141, 421, 148, 6, 7, 78]).long())
        self.assertEqual(valid_dataset[-1][1][:10],
                         torch.tensor([11, 3, 5, 4, 9, 2, 2, 12, 7, 11]).long())
        self.assertEqual(valid_dataset[-1][2][:10],
                         torch.tensor([8, 12, 6, 15, 7, 2, 2, 13, 4, 8]).long())

        self.assertEqual(test_dataset[0][0][:10],
                         torch.tensor([210, 54, 3115, 0, 12229, 0, 33]).long())
        self.assertEqual(test_dataset[0][1][:10],
                         torch.tensor([5, 15, 8, 4, 6, 8, 3]).long())
        self.assertEqual(test_dataset[0][2][:10],
                         torch.tensor([30, 3, 5, 14, 3, 5, 9]).long())
        self.assertEqual(test_dataset[-1][0][:10],
                         torch.tensor([116, 0, 6, 11, 412, 10, 0, 4, 0, 6]).long())
        self.assertEqual(test_dataset[-1][1][:10],
                         torch.tensor([5, 4, 12, 10, 9, 15, 4, 3, 4, 12]).long())
        self.assertEqual(test_dataset[-1][2][:10],
                         torch.tensor([6, 16, 13, 16, 7, 3, 19, 12, 19, 13]).long())

        # Assert vocabs
        self.assertEqual(len(train_dataset.get_vocabs()), 3)
        self.assertEqual(len(train_dataset.get_vocabs()[0]), 19674)
        self.assertEqual(len(train_dataset.get_vocabs()[1]), 19)
        self.assertEqual(len(train_dataset.get_vocabs()[2]), 52)

        # Assert token ids
        word_vocab = train_dataset.get_vocabs()[0]
        tokens_ids = [word_vocab[token] for token in 'Two of them were being run'.split()]
        self.assertEqual(tokens_ids, [1206, 8, 69, 60, 157, 452])

    def test_conll_sequence_tagging(self):
        from torchtext.experimental.datasets import CoNLL2000Chunking

        # smoke test to ensure imdb works properly
        train_dataset, test_dataset = CoNLL2000Chunking()
        self.assertEqual(len(train_dataset), 8936)
        self.assertEqual(len(test_dataset), 2012)
        self.assertEqual(train_dataset[0][0][:10],
                         torch.tensor([11556, 9, 3, 1775, 17, 1164, 177, 6, 212, 317]).long())
        self.assertEqual(train_dataset[0][1][:10],
                         torch.tensor([2, 3, 5, 2, 17, 12, 16, 15, 13, 5]).long())
        self.assertEqual(train_dataset[0][2][:10],
                         torch.tensor([3, 6, 3, 2, 5, 7, 7, 7, 7, 3]).long())
        self.assertEqual(train_dataset[-1][0][:10],
                         torch.tensor([85, 17, 59, 6473, 288, 115, 72, 5, 2294, 2502]).long())
        self.assertEqual(train_dataset[-1][1][:10],
                         torch.tensor([18, 17, 12, 19, 10, 6, 3, 3, 4, 4]).long())
        self.assertEqual(train_dataset[-1][2][:10],
                         torch.tensor([3, 5, 7, 7, 3, 2, 6, 6, 3, 2]).long())

        self.assertEqual(test_dataset[0][0][:10],
                         torch.tensor([0, 294, 73, 10, 13582, 194, 18, 24, 2414, 7]).long())
        self.assertEqual(test_dataset[0][1][:10],
                         torch.tensor([4, 4, 4, 23, 4, 2, 11, 18, 11, 5]).long())
        self.assertEqual(test_dataset[0][2][:10],
                         torch.tensor([3, 2, 2, 3, 2, 2, 5, 3, 5, 3]).long())
        self.assertEqual(test_dataset[-1][0][:10],
                         torch.tensor([51, 456, 560, 2, 11, 465, 2, 1413, 36, 60]).long())
        self.assertEqual(test_dataset[-1][1][:10],
                         torch.tensor([3, 4, 4, 8, 3, 2, 8, 4, 17, 16]).long())
        self.assertEqual(test_dataset[-1][2][:10],
                         torch.tensor([6, 3, 2, 4, 6, 3, 4, 3, 5, 7]).long())

        # Assert vocabs
        self.assertEqual(len(train_dataset.get_vocabs()), 3)
        self.assertEqual(len(train_dataset.get_vocabs()[0]), 19124)
        self.assertEqual(len(train_dataset.get_vocabs()[1]), 46)
        self.assertEqual(len(train_dataset.get_vocabs()[2]), 24)

        # Assert token ids
        word_vocab = train_dataset.get_vocabs()[0]
        tokens_ids = [word_vocab[token] for token in 'Two of them were being run'.split()]
        self.assertEqual(tokens_ids, [970, 5, 135, 43, 214, 690])

    def test_squad1(self):
        from torchtext.experimental.datasets import SQuAD1
        from torchtext.vocab import Vocab
        # smoke test to ensure imdb works properly
        train_dataset, dev_dataset = SQuAD1()
        self.assertEqual(len(train_dataset), 87599)
        self.assertEqual(len(dev_dataset), 10570)
        self.assertEqual(train_dataset[100]['question'],
                         torch.tensor([7, 24, 86, 52, 2, 373, 887, 18, 12797, 11090, 1356, 2, 1788, 3273, 16]).long())
        self.assertEqual(train_dataset[100]['ans_pos'][0],
                         torch.tensor([72, 72]).long())
        self.assertEqual(dev_dataset[100]['question'],
                         torch.tensor([42, 27, 669, 7438, 17, 2, 1950, 3273, 17252, 389, 16]).long())
        self.assertEqual(dev_dataset[100]['ans_pos'][0],
                         torch.tensor([45, 48]).long())

        # Test API with a vocab input object
        old_vocab = train_dataset.get_vocab()
        new_vocab = Vocab(counter=old_vocab.freqs, max_size=2500)
        new_train_data, new_test_data = SQuAD1(vocab=new_vocab)

    def test_squad2(self):
        from torchtext.experimental.datasets import SQuAD2
        from torchtext.vocab import Vocab
        # smoke test to ensure imdb works properly
        train_dataset, dev_dataset = SQuAD2()
        self.assertEqual(len(train_dataset), 130319)
        self.assertEqual(len(dev_dataset), 11873)
        self.assertEqual(train_dataset[200]['question'],
                         torch.tensor([84, 50, 1421, 12, 5439, 4569, 17, 30, 2, 15202, 4754, 1421, 16]).long())
        self.assertEqual(train_dataset[200]['ans_pos'][0],
                         torch.tensor([9, 9]).long())
        self.assertEqual(dev_dataset[200]['question'],
                         torch.tensor([41, 29, 2, 66, 17016, 30, 0, 1955, 16]).long())
        self.assertEqual(dev_dataset[200]['ans_pos'][0],
                         torch.tensor([40, 46]).long())

        # Test API with a vocab input object
        old_vocab = train_dataset.get_vocab()
        new_vocab = Vocab(counter=old_vocab.freqs, max_size=2500)
        new_train_data, new_test_data = SQuAD2(vocab=new_vocab)


from gpaw import *
from ase.io import read, write
from ase.eos import EquationOfState
from ase.optimize import BFGS
import numpy as np
import math as m
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

# User inputs
#################################################################
kpoints = [4, 4, 2]
gpoints = [48, 48, 96]
xc = 'BEEF-vdW'
spin = True    # Initialize magnetic moments
niter = 3

ele1 = 'Co'
magmom1 = 0.0
ele2 = 'Mn'
magmom2 = 3.0
ele3 = 'Ni'
magmom3 = 2.0
#################################################################

input_structure = "str.cif"
output_structure = "opt_str.cif"
step = 0.025
nstep = 5
maxstep = 0.1
fmaxx = 0.03

# Other variables
atoms = read(input_structure)
volume = atoms.get_volume()
magmom = [0.0]*len(atoms)
sym = atoms.get_chemical_symbols()
a=[0]*nstep	# Scaling factor for lattice parameter a
b=[0]*nstep	# Scaling factor for lattice parameter b
c=[0]*nstep	# Scaling factor for lattice parameter c
e=[0]*nstep	# energy
v=[0]*nstep	# volume

# Initialize magnetic moments
for i in range(len(magmom)):
        if sym[i]==ele1:
                magmom[i]=magmom1
        if sym[i]==ele2:
                magmom[i]=magmom2
        if sym[i]==ele3:
                magmom[i]=magmom3

# Optimize a and b simultaneuously
for i in range(nstep):
	a[i] = m.sqrt(1+((nstep//2)-i)*step)
	b[i] = m.sqrt(1+((nstep//2)-i)*step)
	c[i] = 1
	atoms = read(input_structure)

	# Scale the cell
	cell = atoms.get_cell()
	cell[0][:] = a[i]*cell[0][:]
	cell[1][:] = b[i]*cell[1][:]
	cell[2][:] = c[i]*cell[2][:]
	atoms.set_cell(cell,scale_atoms=True)

        # Calculate energy
	atoms.set_initial_magnetic_moments(magmom)
	calc = GPAW(xc=xc,eigensolver=Davidson(niter),spinpol=spin, gpts=gpoints, kpts=kpoints, txt='ab'+str(1+((nstep//2)-i)*step)+'.txt', mixer=MixerSum(0.05, 2, 100))
	atoms.set_calculator(calc)
	e[i] = atoms.get_potential_energy()
	v[i] = atoms.get_volume()

# Fit EOS
eos_a = EquationOfState(v,e,eos='birchmurnaghan')
v0, e0, B = eos_a.fit()
ab_scaling = m.sqrt(v0/volume)
eos_a.plot('eos_a.png', show=False)
plt.close()

# Optimize c
for i in range(nstep):
        a[i] = ab_scaling
        b[i] = ab_scaling
        c[i] = 1 + ((nstep//2)-i)*step
        atoms = read(input_structure)

	# Scale the cell along c
        cell = atoms.get_cell()
        cell[0][:] = a[i]*cell[0][:]
        cell[1][:] = b[i]*cell[1][:]
        cell[2][:] = c[i]*cell[2][:]
        atoms.set_cell(cell,scale_atoms=True)

	# Calculate energy
        atoms.set_initial_magnetic_moments(magmom)
        calc = GPAW(xc=xc, eigensolver=Davidson(niter), spinpol=spin, gpts=gpoints, kpts=kpoints, txt='c'+str(c[i])+'.txt', mixer=MixerSum(0.05, 2, 100))
        atoms.set_calculator(calc)
        e[i] = atoms.get_potential_energy()
        v[i] = atoms.get_volume()

# Fit EOS
eos_c = EquationOfState(v,e,eos='birchmurnaghan')
v0, e0, B = eos_c.fit()
c_scaling = v0/(volume*ab_scaling*ab_scaling)
eos_c.plot('eos_c.png', show=False)
plt.close()

# Set atoms to optimized cell parameters
atoms = read(input_structure)
calc = GPAW(xc=xc, eigensolver=Davidson(niter), spinpol=spin, gpts=gpoints, kpts=kpoints, mixer=MixerSum(0.05, 2, 100), txt='relax.txt')
atoms.set_calculator(calc)
cell = atoms.get_cell()
cell[0][:] = ab_scaling*cell[0][:]
cell[1][:] = ab_scaling*cell[1][:]
cell[2][:] = c_scaling*cell[2][:]
atoms.set_cell(cell,scale_atoms=True)
atoms.set_initial_magnetic_moments(magmom)

# Do atom relax and write outputs
write(output_structure,atoms)
dyn=BFGS(atoms=atoms, trajectory='traj.traj', logfile = 'qn.log', maxstep=maxstep)
dyn.run(fmax=fmaxx)


import requests
import json
from bs4 import BeautifulSoup
import re
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords

# Everthing in one file

stop_words = stopwords.words('english')

def scrap_content(url):
    page=requests.get(url)
    page_content= page.text
    content = BeautifulSoup(page_content, 'html.parser')
    # only paragraphs
    para= content.find_all('p')
    text_list = [p.text for p in para]
    complete_text = ' '.join(text_list)
    return complete_text

def clean_text(text):
    #text = remove_stopWords(text)
    text = ' '.join(text.split())
    text = remove_urls(text)
    text = text.lower()
    return text

#not used
def limit_to_2000_words(text):
    words = text.split()
    return ' '.join(words[:2000])

#any text size can fit now
def split_text(text):
    max_chunk_size = 2000
    chunks = []
    current_chunk = ""
    for sentence in text.split("."):
        if len(current_chunk) + len(sentence) < max_chunk_size:
            current_chunk += sentence + "."
        else:
            chunks.append(current_chunk.strip())
            current_chunk = sentence + "."
    if current_chunk:
        chunks.append(current_chunk.strip())
    return chunks

# I dont like links
def remove_urls(text):
    url_pattern = re.compile(r'https?://\S+|www\.\S+')
    return url_pattern.sub(r'', text)

#not used but ok
def remove_stopWords(text):
    splittedText=text.split()
    withoutStopWords=[word for word in splittedText if word not in stop_words]
    return ' '.join(withoutStopWords)

#  the API to the local llm
# the summary is a list of summary response concatenated
# I didnt use langchain but there is a colab notebook for another solution
def get_response(text):
    input_chunks = split_text(text)
    output_chunks = []
    url= "http://localhost:1234/v1/chat/completions"
    headers={
        "content-type": "application/json"
    }
    system_message = {
    "role": "system",
    "content": "### Instruction: You are a helpful assistant. provide a concise summary of the following text with less than 80 words:  "
    }
    messages=[system_message]
    for chunk in input_chunks:
        user_message ={
                       "role":"user",
                       "content": f"{chunk} \n###Response:" }
        messages.append(user_message)
        data={
        "messages":messages,
        "temperature": 0.7,
        "max_tokens": 500,
        "stream": False
        }
        response = requests.post(url,headers=headers, data=json.dumps(data))
        if response.status_code == 200:
            response = response.json()
            summary=response["choices"][0]["message"]["content"]
            output_chunks.append(summary)
        else:
            print(f"Error: {response.status_code}")
    return " ".join(output_chunks)

url=input("Enter a link to summarize content inside: ")

page_text = scrap_content(url)
cleanedText = clean_text(page_text)
print(f"The cleaned text is : {cleanedText}/n /n /n")
with open('cleanedText.txt', 'w',encoding="utf-8") as f:
    f.write(cleanedText)

summary_answer = get_response(cleanedText)
print(f"The Summary is : {summary_answer}")
with open('summary.txt', 'w',encoding="utf-8") as f:
    f.write(summary_answer)


import os
from pathlib import Path

dir_ = os.path.abspath(os.curdir)

p = Path(f'{dir_}/text2/text3/text4/text5/text6')

p.mkdir(parents=True, exist_ok=True)

def rec():
    for i in os.listdir():
        if Path(i).is_file():
            print('file-',i)
        else:
            print('dir-',i)
            os.chdir(i)
            rec()
    os.chdir('..')
    return

#rec()

for dirpath, dirnames, filenames in os.walk(os.getcwd()):
    print('path', dirpath)
    print('dirs', dirnames)
    print('filenames', filenames)
    print()


from django.urls import path, include

from my_plant_app.web.views import create_profile, show_index, details_profile, edit_profile, delete_profile, \
    show_catalogue, create_plant, details_plant, edit_plant, delete_plant

urlpatterns = (
    path('', show_index, name='show index'),
    path('profile/', include([
        path('create/', create_profile, name='create profile'),
        path('details/', details_profile, name='details profile'),
        path('edit/', edit_profile, name='edit profile'),
        path('delete/', delete_profile, name='delete profile'),
    ])),

    path('catalogue/', show_catalogue, name='show catalogue'),
    path('create/', create_plant, name='create plant'),
    path('details/<pk>/', details_plant, name='details plant'),
    path('edit/<pk>/', edit_plant, name='edit plant'),
    path('delete/<pk>/', delete_plant, name='delete plant'),
)


import aiopg

class DbSession(object):
    engine = None
    connection = None
    cursor = None
    status = None

    def __init__(self, engine, *, transactional=True):
        self.transactional = transactional
        self.engine = engine
        self.connection = None
        self.status = 'new'

    @property
    def pool(self):
        return self.engine.pool

    @classmethod
    def new(cls):
        return cls()

    async def commit(self):
        await self.cursor.execute('COMMIT')
        self.status = 'committed'

    async def rollback(self):
        await self.cursor.execute('ROLLBACK')
        self.status = 'rollbacked'

    async def __aenter__(self):
        self.connection = await self.pool.acquire()
        self.cursor = await self.connection.cursor()
        if self.transactional:
            await self.cursor.execute('BEGIN')
        self.status = 'ready'
        return self

    # noinspection PyUnusedLocal
    async def __aexit__(self, exc_type, exc_value, traceback):
        if self.transactional and self.status == 'ready':
            await self.commit()
        self.cursor.close()
        await self.pool.release(self.connection)

    async def execute(self, query, *, params=None):
        query = query.replace('?', '%s')
        try:
            await self.cursor.execute(query, params)
            return self.cursor
        # noinspection PyBroadException
        except:
            if self.transactional:
                await self.rollback()
            raise

class PostgreSqlEngine(object):
    def __init__(self, conf):
        self.conf = conf

    _pool = None

    @property
    def pool(self):
        if self._pool is None:
            raise ValueError('Engine is noy initialized')
        return self._pool

    @property
    def dsn(self):
        return 'dbname=%s user=%s password=%s host=%s port=%s' % (
            self.conf.get('postgres.db'),
            self.conf.get('postgres.user'),
            self.conf.get('postgres.password'),
            self.conf.get('postgres.host'),
            self.conf.get('postgres.port'),
        )

    async def initialize(self):

        if self._pool is not None:
            raise ValueError('Object is already initialized')

        self._pool = await aiopg.create_pool(
            self.dsn,
            minsize=10,
            timeout=60
        )

    async def shutdown(self):
        self.pool.terminate()
        await self.pool.wait_closed()
        self._pool = None

    async def begin(self):
        return DbSession(self, transactional=True)

    async def session(self):
        return DbSession(self, transactional=False)

    async def execute(self, query: str, *, params=None):
        async with await self.session() as session:
            return await session.execute(query, params=params)

    async def __aenter__(self):
        await self.initialize()
        return self

    # noinspection PyUnusedLocal
    async def __aexit__(self, exc_type, exc_value, traceback):
        await self.shutdown()


import numpy as np

from .vehicle_pose import VehiclePose
from .waypoint import Waypoint
from typing import List, Union
import sys
import math
import time
from queue import PriorityQueue
from io import StringIO

MAX_FLOAT = sys.float_info.max - 10

DIR_TOP = 0
DIR_TOP_LEFT = 1
DIR_TOP_RIGHT = 2
DIR_LEFT = 3
DIR_RIGHT = 4
DIR_BOTTOM_LEFT = 5
DIR_BOTTOM = 6
DIR_BOTTOM_RIGHT = 7

COMPUTE_DIRECTION_POS = [
    Waypoint(0, -1),
    Waypoint(-1, -1),
    Waypoint(1, -1),
    Waypoint(-1, 0),
    Waypoint(1, 0),
    Waypoint(0, 1),
    Waypoint(-1, 1),
    Waypoint(1, 1),
]

MOVING_COST = [
    1,  # up
    5,  # diag up
    5,  # diag up
    50,  # side
    50,  # side
    200,  # down!
    200,  # down!
    500  # down!
]

class QueuedPoint(Waypoint):
    cost: float

    def __init__(self, p: Waypoint, cost: float):
        super().__init__(p.x, p.z)
        self.cost = cost

    def __gt__(self, other):
        return self.cost > other.cost

    def __lt__(self, other):
        return self.cost < other.cost

    def __eq__(self, other):
        return self.cost == other.cost

class NpPlanGrid:
    grid: np.array
    costs: np.array

    def __init__(self, width: int, height: int):
        self.grid = np.full((height, width, 3), -1)
        self.costs = np.full((height, width, 3), MAX_FLOAT)

    def set_closed(self, point: Waypoint) -> None:
        self.grid[point.z, point.x, 0] = 1

    def is_closed(self, point: Waypoint) -> bool:
        return self.grid[point.z, point.x, 0] == 1

    def set_parent(self, point: Waypoint, parent: Waypoint) -> None:
        self.grid[point.z, point.x, 1] = parent.x
        self.grid[point.z, point.x, 2] = parent.z

    def set_parent_by_coord(self, point: Waypoint, coord: List[int]) -> None:
        self.grid[point.z, point.x, 1] = coord[0]
        self.grid[point.z, point.x, 2] = coord[1]

    def get_costs(self, point: Waypoint) -> np.array:
        return self.costs[point.z, point.x]

    def set_costs(self, point: Waypoint, lst: List) -> None:
        self.costs[point.z, point.x] = lst

    def get_parent(self, point: Waypoint) -> Waypoint:
        return Waypoint(self.grid[point.z, point.x][1], self.grid[point.z, point.x][2])

F_CURRENT_BEST_GUESS = 0
G_CHEAPEST_COST_TO_PATH = 1
H_DISTANCE_TO_GOAL = 2

class PlannerResult:
    path: List[Waypoint]
    start: Waypoint
    goal: Waypoint
    invalid_goal: bool
    valid: bool
    timeout: bool
    global_goal: VehiclePose

    def __init__(self) -> None:
        self.valid = False
        self.timeout = False
        self.invalid_goal = False
        self.path = None

    def __str__(self) -> str:
        str = StringIO()
        str.write("{\n")
        str.write(f"    start: {(self.start.x, self.start.z)}\n")
        if self.invalid_goal:
            str.write(f"    goal: (INVALID)\n")
        else:
            str.write(f"    goal: {(self.goal.x, self.goal.z)}\n")
        str.write("    path: ")

        if self.valid:
            first = True
            for p in self.path:
                if not first:
                    str.write(" -> ")
                str.write(f"{(p.x, p.z)}")
                first = False
            str.write("\n")
        else:
            str.write("(INVALID)\n")

        str.write("}\n")

        if self.timeout:
            str.write(f"    timeout: {self.timeout}")

        return str.getvalue()

class AStarPlanner:

    def _checkObstacle(self, frame: np.array, point: Waypoint) -> bool:
        if point.z < 0 or point.z >= frame.shape[0]:
            return True
        if point.x < 0 or point.x >= frame.shape[1]:
            return True
        return frame[point.z][point.x][2] == 0

    def _add_points(self, p1: Waypoint, p2: Waypoint) -> Waypoint:
        return Waypoint(p1.x + p2.x, p1.z + p2.z)

    def _compute_free_surroundings(self,
                                   frame: np.array,
                                   point: Waypoint) -> List[bool]:
        res: List[bool] = []

        for _ in range(0, 8):
            res.append(False)

        res[DIR_TOP] = not self._checkObstacle(
            frame,
            self._add_points(point, COMPUTE_DIRECTION_POS[DIR_TOP])
        )
        res[DIR_TOP_LEFT] = not self._checkObstacle(
            frame,
            self._add_points(point, COMPUTE_DIRECTION_POS[DIR_TOP_LEFT])
        )
        res[DIR_TOP_RIGHT] = not self._checkObstacle(
            frame,
            self._add_points(point, COMPUTE_DIRECTION_POS[DIR_TOP_RIGHT])
        )
        res[DIR_LEFT] = not self._checkObstacle(
            frame,
            self._add_points(point, COMPUTE_DIRECTION_POS[DIR_LEFT])
        )
        res[DIR_RIGHT] = not self._checkObstacle(
            frame,
            self._add_points(point, COMPUTE_DIRECTION_POS[DIR_RIGHT])
        )
        res[DIR_BOTTOM_LEFT] = not self._checkObstacle(
            frame,
            self._add_points(point, COMPUTE_DIRECTION_POS[DIR_BOTTOM_LEFT])
        )
        res[DIR_BOTTOM] = not self._checkObstacle(
            frame,
            self._add_points(point, COMPUTE_DIRECTION_POS[DIR_BOTTOM]))
        res[DIR_BOTTOM_RIGHT] = not self._checkObstacle(
            frame,
            self._add_points(point, COMPUTE_DIRECTION_POS[DIR_BOTTOM_RIGHT])
        )

        return res

    def _compute_euclidian_distance(self, p1: Waypoint, p2: Waypoint) -> float:
        dz = p2.z - p1.z
        dx = p2.x - p1.x
        return math.sqrt(dz * dz + dx * dx)

    def _timeout(self, start_time: int, max_exec_time_ms: int):
        return 1000*(time.time() - start_time) > max_exec_time_ms

    def plan(self,
             bev_frame: np.array,
             max_exec_time_ms: int,
             start: Waypoint,
             goal: Waypoint) -> PlannerResult:

        result = PlannerResult()

        result.start = start
        result.goal = goal

        if result.goal is None:
            result.invalid_goal = True
            return result

        start_time = time.time()
        plan_grid = NpPlanGrid(bev_frame.shape[1], bev_frame.shape[0])

        plan_grid.set_costs(start, [0, 0, 0])
        #plan_grid.set_parent_by_coord(start, [-1, -1])

        open_list = PriorityQueue()
        open_list.put(QueuedPoint(start, 0))

        best_possible = None
        best_distance_to_goal: float = MAX_FLOAT
        search = True

        while search and not open_list.empty():
            curr_point = open_list.get(block=False)

            if self._timeout(start_time, max_exec_time_ms):
                search = False
                continue

            plan_grid.set_closed(curr_point)

            free_surroundings = self._compute_free_surroundings(
                bev_frame, curr_point)

            f: float = MAX_FLOAT
            g: float = MAX_FLOAT
            h: float = MAX_FLOAT

            curr_costs = plan_grid.get_costs(curr_point)

            for dir in range(0, 6):
                if not free_surroundings[dir]:
                    continue

                next_point = self._add_points(
                    curr_point, COMPUTE_DIRECTION_POS[dir])

                if plan_grid.is_closed(next_point):
                    continue

                distance_to_goal = bev_frame[next_point.z, next_point.x, 1]

                if distance_to_goal < best_distance_to_goal:
                    best_distance_to_goal = distance_to_goal
                    best_possible = next_point

                if next_point.x == goal.x and next_point.z == goal.z:
                    best_possible = next_point
                    best_distance_to_goal = 0
                    search = False

                g = curr_costs[G_CHEAPEST_COST_TO_PATH] + MOVING_COST[dir] - bev_frame[next_point.z, next_point.x, 2]
                h = distance_to_goal
                f = g + h

                next_costs = plan_grid.get_costs(next_point)

                if next_costs[F_CURRENT_BEST_GUESS] > f:
                    plan_grid.set_costs(next_point, [f, g, h])
                    plan_grid.set_parent(next_point, curr_point)
                    open_list.put(QueuedPoint(next_point, f))

        if best_possible is None:
            result.invalid_goal = True
            result.valid = False
            return result

        path: List[Waypoint] = []

        path.append(best_possible)

        p = best_possible
        parent = plan_grid.get_parent(p)
        while parent.x >= 0:
            path.append(parent)
            p = parent
            parent = plan_grid.get_parent(p)

        path.append(start)
        path.reverse()

        result.path = path
        result.valid = True
        result.goal = best_possible
        return result


"""
Created on Tue Feb 28 11:44:11 2023.

@author: Vishal Philip

This script consists of functions for plotting line charts,
bar charts and pie charts using matplotlib library.
It also reads data from excel files using pandas.

"""

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

def line_plot(x_axis, y_axis, name, col):
    """
    Create Function to plot a line chart.

    Parameters :
        x_axis : The x-axis values for the plot.
        y_axis : The y-axis values for the plot.
        name (str): label the name of the the country to plot.
        color (str): The color of the plot.
    Returns The plot representing the line chart

    """
    return plt.plot(x_axis, y_axis, '.-', label=name, color=col)

def bar_plot(ypos, Year_data, a):
    """
    Create a function to plot bar chart.

    Parameters :
       ypos  : Name of the countries.
       year_data : The height of each bar.
       a : The width of each bar.
    Returns the plot representing the bar chart.

    """
    plt.figure(figsize=(10, 5))
    plt.xticks(ypos, Countries_data)
    return plt.bar(ypos, Year_data, width=a)

def plot_pie(Pie_Data, Pie_label):
    """
    Create a function for plotting pie chart.

    Parameters :
        Pie_data : The values to be plotted as slices of the pie chart.
        Pie_labels : The labels for each slice of the pie chart.

    Returns plot object representing the pie chart.

    """
    plt.figure(figsize=(10, 9))
    return plt.pie(Pie_Data, labels=Pie_label, autopct='%1.1f%%')

# Reading data from excel file

data1 = pd.read_excel("Mobile Cellular Subscription per 100.xlsx")

# dropping unwanted data

data2 = data1.drop(5, axis=0)

# Plotting line chart using the function line_plot

plt.figure()

# Calling function line_plot

line_plot(data2.Year, data2.Afghanistan, "Afghanistan", "red")
line_plot(data2.Year, data2.Bangladesh, "Bangladesh", "blue")
line_plot(data2.Year, data2.Bhutan, "Bhutan", "green")
line_plot(data2.Year, data2.India, "India", "black")
line_plot(data2.Year, data2.Nepal, "Nepal", "yellow")
line_plot(data2.Year, data2.Pakistan, "Pakistan", "orange")
line_plot(data2.Year, data2.SriLanka, "SriLanka", "violet")

# Creating labels

plt.xlabel("Years", size=15)
plt.ylabel("Subscription per 100 people", size=13)

# Creating Title

plt.title("Mobile Cellular Subscription of South Asian Countries")
plt.legend(fontsize=8)

# Saving image

plt.savefig("lineplot.png")
plt.show()

# Plotting bar graph using the function bar_plot
# Reading data from excel file

newdata = pd.read_excel("Mobile Cellular Subscription Copy.xlsx")
Countries_data = newdata.Countries
ypos = np.arange(len(Countries_data))
Year_data = newdata.Year_2005

# Calling Function bar_plot

bar_plot(ypos, Year_data, 0.5)

# Creating labels

plt.xlabel("Countries", size=15)
plt.ylabel("Subscription per 100 People", size=13)

# Creating Title

plt.title("Mobile Cellular Subscription of South Asian Countries in 2005")

# Saving image
plt.savefig("bargraph1.png")
plt.show()

# Plotting pie chart using the function plot_pie

pie_data = pd.read_excel("ForestArea.xlsx")
Countries_name = pie_data.Countries
Forest_Data2015 = pie_data.Year_2015
Forest_Data2005 = pie_data.Year_2005

# Calling Function plot_pie to create pie year for 2005

plot_pie(Forest_Data2015, Countries_name)

# Creating Title

plt.title("Forest Area of South Asian Countries (2015)", size=20)
plt.legend()

# Saveing fig

plt.savefig("piechart1.png")

# Calling Function plot_pie to create pie year for 2015

plot_pie(Forest_Data2005, Countries_name)

# Creating Title

plt.title("Forest Area of South Asian Countries (2005)", size=20)
plt.legend()

# Saving Image

plt.savefig("piechart2.png")
plt.show()


# Распознавание лиц с помощью нейронных сетей
# Необходимо установить следующие компоненты:
# pip install git+https://github.com/rcmalli/keras-vggface.git
# pip install mtcnn
# pip install keras-applications
# Заменить строку на from keras.utils.layer_utils import get_source_inputs
# В файле keras_vggface/models.py
from mtcnn import MTCNN
from matplotlib import pyplot
from PIL import Image
import numpy as np
from keras_vggface.vggface import VGGFace
from keras_vggface.utils import preprocess_input
from scipy.spatial.distance import cosine

def extract_faces(filename, required_size=(224, 224)):
    # load image from file
    pixels = pyplot.imread(filename)

    # create the detector, using default weights
    detector = MTCNN()
    # detect faces in the image
    results = detector.detect_faces(pixels)
    # extract the bounding box from the first face
    face_array = []
    for result in results:
        x1, y1, width, height = result['box']
        x2, y2 = x1 + width, y1 + height
        # extract the face
        face = pixels[y1:y2, x1:x2]

        # resize pixels to the model size
        image = Image.fromarray(face)
        image = image.resize((224, 224))
        face_array.append(np.asarray(image))
    return face_array

# load the photo and extract the face
pixels = extract_faces('buntov.jpg')
# plot the extracted face
pyplot.imshow(pixels[1])
# show the plot
pyplot.show()

# extract faces and calculate face embeddings for a list of photo files
def get_embeddings(filename):
    # extract faces
    faces = extract_faces(filename)
    # convert into an array of samples
    samples = np.asarray(faces, 'float32')
    # prepare the face for the model, e.g. center pixels
    samples = preprocess_input(samples, version=2)
    # create a vggface model
    model = VGGFace(model='resnet50', include_top=False, input_shape=(224, 224, 3), pooling='avg')
    # perform prediction
    yhat = model.predict(samples)
    return yhat

# determine if a candidate face is a match for a known face
def is_match(known_embedding, candidate_embedding, thresh=0.5):
    # calculate distance between embeddings
    score = cosine(known_embedding, candidate_embedding)
    if score <= thresh:
        print('>face is a Match (%.3f <= %.3f)' % (score, thresh))
    else:
        print('>face is NOT a Match (%.3f > %.3f)' % (score, thresh))

# define filenames
filename = 'buntov.jpg'
# get embeddings file filenames
embeddings = get_embeddings(filename)
# define sharon stone
buntov_id = get_embeddings('gt_db/buntov/Buntov.jpg')[0]
# verify known photos of sharon
print('Tests')
for embedding in embeddings:
    is_match(embedding, buntov_id)


def COOKEweights(SQ_array, TQ_array, realization, alpha, background_measure,
                 overshoot, cal_power):
    """Compute the weights with Cooke formulation

    Parameters
    ----------
    SQ_array : numpy array
        Array with answers to seed questions
    TQ_array : numpy array
        Array with answers to seed questions
    realization : list
        Python list with realization of the seed questions
    alpha : float
        Significance
    background_measure:
        Python list
    overshoot : float
    cal_power : float

    Returns
    -------
    W : numpy array
        an array with weights

    This function is based on the Matlab package
    ANDURIL (Authors:  Georgios Leontaris and
    Oswaldo Morales-Napoles)
    """

    import numpy as np

    # number of Seed questions
    N = SQ_array.shape[2]
    # number of experts
    E = SQ_array.shape[0]
    # number of realization in every bin
    M = np.zeros((E, 4))
    # score
    C = np.zeros((E))
    # unNormalized weight
    w = np.zeros((E))
    # array with information, score and normalized weight for each expert
    W = np.zeros((E, 5))

    # create numpy array M with the number of realizations captured in every
    # expert's bin that is formed by the provided quantiles
    for ex in np.arange(E):
        for i in np.arange(N):
            if realization[i] < SQ_array[ex, 0, i]:

                M[ex, 0] = M[ex, 0] + 1

            elif realization[i] == SQ_array[ex, 0, i]:

                M[ex, 0] = M[ex, 0] + 0.5
                M[ex, 1] = M[ex, 1] + 0.5

            elif realization[i] < SQ_array[ex, 1, i]:

                M[ex, 1] = M[ex, 1] + 1

            elif realization[i] == SQ_array[ex, 1, i]:

                M[ex, 1] = M[ex, 1] + 0.5
                M[ex, 2] = M[ex, 2] + 0.5

            elif realization[i] < SQ_array[ex, 2, i]:

                M[ex, 2] = M[ex, 2] + 1

            elif realization[i] == SQ_array[ex, 2, i]:

                M[ex, 2] = M[ex, 2] + 0.5
                M[ex, 3] = M[ex, 3] + 0.5

            else:

                M[ex, 3] = M[ex, 3] + 1
    """

    # create numpy array M with the number of realizations captured in every
    # expert's bin that is formed by the provided quantiles
    for ex in np.arange(E):
        for i in np.arange(N):
            if realization[i] < SQ_array[ex, 0, i]:

                M[ex, 0] = M[ex, 0] + 1

            elif realization[i] < SQ_array[ex, 1, i]:

                M[ex, 1] = M[ex, 1] + 1

            elif realization[i] < SQ_array[ex, 2, i]:

                M[ex, 2] = M[ex, 2] + 1

            else:

                M[ex, 3] = M[ex, 3] + 1

    """

    # calculate calibration and information score for every expert
    [I_real, I_tot] = calculate_information(SQ_array, TQ_array, realization,
                                            overshoot, background_measure)

    for ex in np.arange(E):

        C[ex] = calscore(M[ex, :], cal_power)

        W[ex, 0] = C[ex]
        W[ex, 1] = I_tot[ex]
        W[ex, 2] = I_real[ex]

        w[ex] = C[ex] * I_real[ex]

    # unNormalized weight
    W[:, 3] = w.T

    # set to zero based on calibration score
    w[C < alpha] = 0.0

    # set to zero based on weight
    # w[ (w/np.sum(w)) < alpha] = 0.0

    # Normalized weight
    W[:, 4] = w / np.sum(w)

    return W, C, I_real

def calscore(M, cal_power):
    """Compute single expert score

    Parameters
    ----------
    M : numpy array
        Number of realizations captured in every expert's bin
        that is formed by the provided quantiles
    cal_power : float

    Returns
    -------
    CS : float
         Score

    This function is based on the Matlab package
    ANDURIL (Authors:  Georgios Leontaris and
    Oswaldo Morales-Napoles)
    """

    import numpy as np
    from scipy.stats import chi2

    N = np.sum(M)  # number of seed items
    S = M / N

    if np.sum(np.isnan(S)) == len(S):

        CS = np.nan

    else:
        if len(S) == 4:
            P = np.array([0.05, 0.45, 0.45, 0.05])
        elif len(S) == 6:
            P = np.array([0.05, 0.20, 0.25, 0.25, 0.20, 0.05])

        E1 = S * np.log(S / P)
        MI = np.sum(E1[~np.isnan(E1)])
        E = 2.0 * N * MI * cal_power
        CS = chi2.sf(E, len(S) - 1)

    return CS

def calculate_information(SQ_array, TQ_array, realization, k,
                          background_measure):
    """
    This function is based on the Matlab package
    ANDURIL (Authors:  Georgios Leontaris and
    Oswaldo Morales-Napoles)
    """

    import numpy as np

    p = [0.05, 0.45, 0.45, 0.05]
    N = SQ_array.shape[2]
    E = SQ_array.shape[0]
    N_P = len(p)
    suma = np.zeros((N, E))
    x = np.zeros((E, N_P + 1))

    Nq = TQ_array.shape[2]

    lowval = np.zeros(N + Nq)
    highval = np.zeros(N + Nq)

    x_o = np.zeros(N + Nq)
    x_n = np.zeros(N + Nq)

    info_per_variable = np.zeros((N + Nq, E))
    Info_score_real = np.zeros((E))
    Info_score_tot = np.zeros((E))

    for i in np.arange(N):

        lowval[i] = np.minimum(np.amin(SQ_array[:, :, i]), realization[i])
        highval[i] = np.maximum(np.amax(SQ_array[:, :, i]), realization[i])

        for e in np.arange(E):

            if background_measure[i] == "uni":

                x_o[i] = lowval[i] - k * (highval[i] - lowval[i])
                x_n[i] = highval[i] + k * (highval[i] - lowval[i])

                tmp = np.insert(SQ_array[e, :, i], 0, x_o[i])
                tmp2 = np.append(tmp, x_n[i])
                x[e, :] = tmp2

            elif background_measure[i] == "log":

                if lowval[i] < 0 or highval[i] < 0:

                    raise ValueError(
                        "Log-Uniform background measure cannot be used "
                        "for item %d because an expert provided "
                        "negative values",
                        i,
                    )

                x_o[i] = np.log(
                    lowval[i]) - k * (np.log(highval[i]) - np.log(lowval[i]))
                x_n[i] = np.log(
                    highval[i]) + k * (np.log(highval[i]) - np.log(lowval[i]))
                tmp = np.insert(np.log(SQ_array[e, :, i]), 0, x_o[i])
                tmp2 = np.append(tmp, x_n[i])
                x[e, :] = tmp2

            for j in np.arange(N_P):

                suma[i, e] = suma[i, e] + p[j] * \
                    np.log(p[j] / (x[e, j + 1] - x[e, j]))

            info_per_variable[i, e] = np.log(x_n[i] - x_o[i]) + suma[i, e]

    for e in np.arange(E):
        # for the overall information of every expert take the average of each
        # expert e for all calibration variables N
        Info_score_real[e] = np.sum(info_per_variable[:, e]) / N

    N_items_tot = SQ_array.shape[2] + TQ_array.shape[2]
    sumb = np.zeros((N_items_tot, E))

    for i in np.arange(SQ_array.shape[2], N_items_tot):

        if not realization[i]:  # information of Target Questions

            lowval[i] = np.amin(TQ_array[:, :, i - SQ_array.shape[2]])
            highval[i] = np.amax(TQ_array[:, :, i - SQ_array.shape[2]])

            for e in np.arange(E):

                if background_measure[i] == "uni":

                    x_o[i] = lowval[i] - k * (highval[i] - lowval[i])
                    x_n[i] = highval[i] + k * (highval[i] - lowval[i])

                    tmp = np.insert(TQ_array[e, :, i - SQ_array.shape[2]], 0,
                                    x_o[i])
                    tmp2 = np.append(tmp, x_n[i])
                    x[e, :] = tmp2

                elif background_measure[i] == "log":

                    if lowval[i] < 0 or highval[i] < 0:

                        raise ValueError(
                            "Log-Uniform background measure cannot be used "
                            "for item %d because an expert provided "
                            "negative values",
                            i,
                        )

                    x_o[i] = np.log(lowval[i]) - k * (np.log(highval[i]) -
                                                      np.log(lowval[i]))
                    x_n[i] = np.log(highval[i]) + k * (np.log(highval[i]) -
                                                       np.log(lowval[i]))

                    tmp = np.insert(
                        np.log(TQ_array[e, :, i - SQ_array.shape[2]]), 0,
                        x_o[i])
                    tmp2 = np.append(tmp, x_n[i])
                    x[e, :] = tmp2

                for j in np.arange(N_P):

                    sumb[i - SQ_array.shape[2],
                         e] = sumb[i - SQ_array.shape[2],
                                   e] + p[j] * np.log(p[j] /
                                                      (x[e, j + 1] - x[e, j]))

                info_per_variable[i, e] = (np.log(x_n[i] - x_o[i]) +
                                           sumb[i - SQ_array.shape[2], e])

    # compute total information score
    for e in np.arange(E):

        # for the overall information of every expert take the average of each
        # expert e for all calibration variables N_items_tot
        Info_score_tot[e] = np.sum(info_per_variable[:, e]) / N_items_tot

    return Info_score_real, Info_score_tot


import pygame, sys, math, random, time, Object
pygame.init()

width = 1000
height = 1000
size = width, height
fps = 60

bgColor = (83,174,232)

screen = pygame.display.set_mode(size)
icon = pygame.image.load('icon.png')
pygame.display.set_caption("Ball Demo")
pygame.display.set_icon(icon)
clock = pygame.time.Clock()
Object.setRes(width, height)
#Object.Object(Surface, xVel, yVel, img, initPos, weight)
ball1 = Object.Object(screen,6,2,"ball.png",(300,300),1)
ball2 = Object.Object(screen,1,5,"ball2.png",(50,50),2)
square1 = Object.Object(screen,6,5,"square1.png",(100,100),3)
square2 = Object.Object(screen,9,4,"square2.png",(500,500),4)
penta1 = Object.Object(screen,3,4,"penta1.png",(600,600),5)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit();

    #Objects
    screen.fill(bgColor)
    objects = Object.getObjects()
    for obj in objects:
        obj.move()

    #Pygame
    clock.tick(fps)
    pygame.display.flip()


# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:

        helper_pointer = main_pointer = head
        prev = None

        while helper_pointer:
            if n < 1:
                prev = main_pointer
                main_pointer = main_pointer.next
                helper_pointer = helper_pointer.next
            else:
                n = n - 1
                helper_pointer = helper_pointer.next

        if prev is None and n < 1:
            head = head.next
            return head

        elif main_pointer and n < 1:
            prev.next = main_pointer.next
            main_pointer.next = None
            return head
        else:
            return head


import os
import random
import shutil

from src.club.club import *
from src.club.player import Player
from src.club.training_report import TrainingReport
from src.club.training_venue import TrainingVenue
from src.match.fixture import MatchType, Venue
from src.match.match_report import MatchReport
from src.utilities.save_utilities import save_club, load_club

TEST_PLAYERS = [["jack", "charlton"],["steven", "gerrard"], ["alex", "telles"], ["michael","ballack"],["peter","crouch"]]
TEST_VENUE_NAMES = ["nou camp", "old trafford", "anfield", "etihad"]
TEST_VENUE_COSTS = [5, 2.50, 6, 10]

def TEST_create_club_and_add_duplicate_player(test_folder):
    club_one = Club("test_club")

    first_name = "same"
    surname = "name"
    valid, error = club_one.add_player(Player(first_name, surname))

    if valid == False:
        print("TEST FAILED: failed to add player to club. Error: " + error)
        return False

    valid, error = club_one.add_player(Player(first_name, surname))
    if not valid and len(club_one.players) == 1:
        return True

    print("TEST FAILED: successfully added duplicate player to club")
    return False

def TEST_create_club_and_add_players(test_folder):
    club_one = Club("test_club")

    for player in TEST_PLAYERS:
        club_one.add_player(Player(player[0], player[1]))

    save_club(club_one, test_folder)

    club_two = Club("test_club")
    load_club(club_two, test_folder)

    num_players = len(club_one.players)
    if num_players != len(club_two.players):
        print("TEST FAILED: descrepancy in number of players at clubs")
        return False
    else:
        i = 0
        while i < num_players:
            if club_one.players[i] != club_two.players[i]:
                print("TEST FAILED: different players loaded than saved")
                return False
            i += 1
    return True

def TEST_create_match_report_and_save_and_load(test_folder):
    club_one = Club("test_club")
    match_report = MatchReport()

    while len(match_report.starting_lineup) < 11:
        match_report.add_starter(Player(random.choice(TEST_PLAYERS)[0], random.choice(TEST_PLAYERS)[1]))
    while len(match_report.subs) < 5:
        match_report.add_sub(Player(random.choice(TEST_PLAYERS)[0], random.choice(TEST_PLAYERS)[1]))

    match_report.match_type = random.choice(list(MatchType))
    match_report.venue = random.choice(list(Venue))

    club_one.add_match_report(match_report)
    save_club(club_one, test_folder)

    club_two = Club("test_club")
    load_club(club_two, test_folder)

    if len(club_two.match_reports) != 1:
        return False

    report = club_two.match_reports[0]
    if len(report.starting_lineup) != 11:
        return False
    if len(report.subs) != 5:
        return False
    if match_report.match_type != report.match_type:
        return False
    if match_report.venue != report.venue:
        return False

    return True

def TEST_create_training_venues_and_save_and_load(test_folder):
    num_venues_to_generate = 5

    club_one = Club("test_club")

    while len(club_one.training_venues) < num_venues_to_generate:
        club_one.training_venues.append(TrainingVenue(random.choice(TEST_VENUE_NAMES), random.choice(TEST_VENUE_COSTS)))

    save_club(club_one, test_folder)

    club_two = Club("test_club")
    load_club(club_two, test_folder)

    if len(club_two.training_venues) != num_venues_to_generate:
        print("TEST FAILED: didn't store and load the expected number of venues")
        return False

    if len(club_one.training_venues) != len(club_two.training_venues):
        print("TEST FAILED: failed to load the same amount of training venues back")
        return False

    i = 0
    while i < len(club_two.training_venues):
        club_one_test_venue = club_one.training_venues[i]
        club_two_test_venue = club_two.training_venues[i]

        if club_one_test_venue != club_two_test_venue:
            print("TEST FAILED: discrepancy in data after being saved and loaded")
            return False
        i += 1
    return True

def TEST_create_training_report_and_save_and_load(test_folder):
    num_reports_to_generate = 10

    club_one = Club("test_club")

    while len(club_one.training_venues) < num_reports_to_generate:
        club_one.training_venues.append(TrainingVenue(random.choice(TEST_VENUE_NAMES), random.choice(TEST_VENUE_COSTS)))

    while len(club_one.training_reports) < num_reports_to_generate:
        new_report = TrainingReport()
        num_attendees = random.randint(0, 20)
        i = 0
        while i < num_attendees:
            new_report.attendees.append(random.choice(TEST_PLAYERS))
            i += 1
        new_report.venue = TrainingVenue(random.choice(TEST_VENUE_NAMES), random.choice(TEST_VENUE_COSTS))
        club_one.training_reports.append(new_report)

    save_club(club_one, test_folder)

    club_two = Club("test_club")
    load_club(club_two, test_folder)

    if len(club_two.training_reports) != len(club_one.training_reports):
        print("TEST FAILED: discrepancy in number of training reports after save and load")
        return False

    # TO-DO: functionise this
    i = 0
    while i < len(club_two.training_reports):
        if club_one.training_reports[i] != club_two.training_reports[i]:
            print("TEST FAILED: discrepancy in object data after being saved and loaded")
            return False
        i += 1

    return True

def run_tests():
    count = 0
    successful = 0
    for test_def in tests:
        test_name = test_def.__name__
        print("Running " + test_name + " test")
        if test_def(test_folder_name):
            print(test_name + " passed")
            successful += 1
        else:
            print(test_name + " failed")
        count += 1
    print("we ran " + str(count) + " tests. " + str(successful) + " passes.")

    return count == successful

def clean_up():
    shutil.rmtree(test_folder_name)

test_folder_name = "test/"
os.mkdir("test")
tests = [TEST_create_club_and_add_players,
 TEST_create_club_and_add_duplicate_player,
 TEST_create_match_report_and_save_and_load,
 TEST_create_training_venues_and_save_and_load,
 TEST_create_training_report_and_save_and_load]

if run_tests():
    clean_up()


import pickle
import torch
import os

from ood_samplefree.datasets import __DATASETS__, get_transform

from ood_samplefree.architectures import __ARCHITECTURES__, \
    __ARCHITECTURES_224__
from ood_samplefree.experiments import OODStreamerWithSummaries

NUM_WORKER = 4
BATCH_SIZE = 256

def run(dataset, architecture, network_path, base_db, random_state):
    excluded = "in-dms:in-dss:in-dms-aos:in-nota:nota:proj+".split(":")

    # =========================== PATH =============================== #
    latent_folder = os.path.expanduser("~/data/latent/{}_{}_{}_{}"
                                       "".format(base_db, architecture,
                                                 random_state, dataset))

    folder = os.path.expanduser("~/data/ood_samplefree/")
    uniform_path = os.path.join(folder, "{}_{}_{}_uniform_full.npy"
                                        "".format(base_db,
                                                  architecture,
                                                  random_state))

    result_path = os.path.expanduser("~/data/results/{}_{}_{}_{}"
                                     "".format(base_db, architecture,
                                               random_state, dataset))

    # ============================= RAND ================================= #
    cuda = torch.cuda.is_available()
    torch.manual_seed(random_state + 42)
    if cuda:
        torch.cuda.manual_seed(random_state + 101)

    # =========================== ARCHITECTURES =============================== #
    ARCHITECTURES = __ARCHITECTURES__

    if architecture.endswith("_small"):
        ARCHITECTURES = __ARCHITECTURES__
    if architecture.endswith("_large"):
        ARCHITECTURES = __ARCHITECTURES_224__

    # ============================== MODELS ============================== #
    print("Loading model from", network_path)

    id_db_factory = __DATASETS__[base_db]
    print(id_db_factory, id_db_factory.get_default_n_outputs())
    model = ARCHITECTURES[architecture.split("_")[0]](
        n_outputs=id_db_factory.get_default_n_outputs())

    model.load_state_dict(torch.load(network_path, map_location="cpu"))
    model.eval()

    # =========================== DATA =================================== #
    id_db = id_db_factory(shape=model.input_size)

    actual_db = base_db if dataset in {"train", "val", "test"} else dataset
    whole_dataset = __DATASETS__[actual_db].same_as(id_db)

    train_set, val_set, test_set = whole_dataset.get_ls_vs_ts()

    if dataset == "train":
        investigated_set = train_set
    elif dataset == "val":
        investigated_set = val_set
    elif dataset == "test":
        investigated_set = test_set
    else:
        investigated_set = torch.utils.data.ConcatDataset([train_set,
                                                           val_set,
                                                           test_set])

    # ============================= COMPUTATION ================================ #
    streamer = OODStreamerWithSummaries.from_uniform(id_db,
                                                     excluded=excluded,
                                                     ref_save_path=uniform_path,
                                                     num_workers=NUM_WORKER,
                                                     batch_size=BATCH_SIZE)

    whole_cache = None
    for completion, cache in streamer.stream(model, investigated_set,
                                             latent_folder,
                                             batch_size=BATCH_SIZE,
                                             num_workers=NUM_WORKER):
        whole_cache = cache
        # `completion` hook to display progress

    if whole_cache is None:
        raise RuntimeError("Nothing to save.")

    # ============================= RESULTS ================================ #
    collector = {}
    for key, value in whole_cache:
        collector[key] = value

    with open(result_path, "wb") as hdl:
        pickle.dump(collector, hdl, protocol=pickle.HIGHEST_PROTOCOL)

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("dataset", help="OOD dataset")
    parser.add_argument("architecture", help="Name of the architecture")
    parser.add_argument("network_path", help="Path to the trained model")
    parser.add_argument("--base_db", help="ID dataset", default="cifar10")
    parser.add_argument("--random_state", help="ID dataset", default=0, type=int)

    args = parser.parse_args()

    run(args.dataset, args.architecture, args.network_path, args.base_db, args.random_state)


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

import numpy as np
import matplotlib.pyplot as plt

from sklearn.cluster import KMeans, DBSCAN
from sklearn import metrics
from sklearn.datasets import make_blobs
from scipy.spatial import Voronoi

# Aquí tenemos definido el sistema X de 1000 elementos de dos estados
# construido a partir de una muestra aleatoria entorno a unos centros:
centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples=1000, centers=centers, cluster_std=0.4,
                            random_state=0)

plt.plot(X[:,0],X[:,1],'ro', markersize=1)
plt.show()

"""
Obtén el coeficiente s de X para diferente número de vecindades
k ∈ {1, 2, 3, ..., 15} utilizando el algoritmo KMeans. Muestra gráficamente s
en función de k y decide cuál es el número óptimo de vecindades.
"""

# ################################# K-MEANS ###################################
print (' K-MEANS '.center(80,'#'))
# Representamos los valores del coef. de Silhouette frente al número de
# clusters k. Para un solo cluster tomamos el valor s=-1 (evaluando en la fórmula)

n_clusters_values, silhouette_values = np.arange(1,16), [-1]

for num_clusters in range (2,16): # Creamos el vector de coefs. de Silhouette
    kmeans = KMeans(n_clusters=num_clusters, random_state=0).fit(X)
    silhouette = metrics.silhouette_score(X, kmeans.labels_)
    silhouette_values.append(silhouette)
silhouette_values = np.array(silhouette_values)

fig, ax = plt.subplots()
ax.plot(n_clusters_values, silhouette_values, 'ro--')
ax.set(xlabel='Number of clusters', ylabel='Silhouette')
ax.grid()
plt.show()

print ('The maximum Silhouette Coef. is obtained with %d clusters'
       % n_clusters_values[silhouette_values.argmax()])

# Análisis de Silhouette y gráfico de dispersión para cada valor de n_clusters_
# con el fin de determinar de forma más clara el número de clusters k óptimo

plt.style.use(plt.style.available[1])
n_clusters_posibles = range (2, 6)

for n_clusters_ in n_clusters_posibles:
    fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(15,4))

    kmeans_ = KMeans(n_clusters=n_clusters_, random_state=0).fit(X)
    labels_ = kmeans_.labels_

    silhouette_ = metrics.silhouette_score(X, labels_)
    elements_silhouette = metrics.silhouette_samples(X, labels_) # Coef. de Silh.
                                                                 # para cada muestra
    y_lower = 10 # Coordenada y de inicio de la franja correspondiente al cluster
    unique_labels_ = set(labels_)
    colors_ = [plt.cm.Spectral(each)
               for each in np.linspace(0, 1, len(unique_labels_))]

    for k, col in zip(unique_labels_, colors_):
        if k == -1:
            col = [0, 0, 0, 1] # Negro para los outliers

        xy = X[labels_ == k]
        # Tomamos los coefs. de Silhouette de cada elemento del cluster y ordenamos
        xy_elements_silhouette = elements_silhouette[labels_==k]
        xy_elements_silhouette.sort()

        y_upper = y_lower + len(xy) # Coordenada y del final de la franja

        ax1.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
                 markeredgecolor='k', markersize=5)

        ax2.fill_betweenx(np.arange(y_lower, y_upper), 0,
                          xy_elements_silhouette, facecolor=tuple(col),
                          edgecolor=tuple(col), alpha=1)
        ax2.text(-0.05, y_lower + 0.5 * len(xy), str(k))

        y_lower = y_upper + 10

    ax2.axvline(silhouette, color="red", linestyle="--", lw=1.5)
    ax2.legend(['s=%0.3f' % silhouette_])
    ax2.set(xlabel='Shilhouette Coefficients', ylabel='Elements by clusters',
            yticklabels=([]),
            xlim=[-0.1,1], ylim=[0, len(X) + (n_clusters_ + 1)*10])

    ax1.set_xlim(-2.5, 2.2), ax1.set_ylim(-2.3, 2.3)

    plt.suptitle('Clusters: %s\nSilhouette: %0.3f'
                 % (n_clusters_, silhouette_), fontsize=11, fontweight='bold')
    plt.show()

    print('CLUSTERS CENTERS:')
    print(kmeans_.cluster_centers_)
    print("SILHOUETTE COEFFICIENT: %0.3f".upper() % silhouette_)

plt.style.use('default')

# Resultado con k = 3 (óptimo)

kmeans_ = KMeans(n_clusters=3, random_state=0)
kmeans_.fit(X)
vor = Voronoi(kmeans_.cluster_centers_)

fig = plt.figure(figsize=(8,4))
plt.scatter(X[:, 0], X[:, 1], marker='o',
            c=kmeans_.predict(X), s=5, cmap='summer')
plt.title('Optimal choice: k = 3')
plt.show()

"""
Obtén el coefi
ciente s para el mismo sistema X usando ahora el algoritmo DBSCAN
con la métrica euclidean y luego con manhattan. En este caso, el parámetro que
debemos explorar es el umbral de distancia, de
jando el número de elementos
mínimo en n0 = 10. Comparad gra
camente con el resultado del apartado anterior.
"""

############################# DBSCAN EUCLIDEAN ################################
print (' DBSCAN EUCLIDEAN METRIC '.center(80,'#'))
# Para eps > 0.43 surgen problemas con el cálculo del coef. de Silhouette,
# ya que las bolas de los elementos son lo suficientemente grandes como para
# que resulte un sólo cluster.

epsilon_values = np.arange(.10, .44, .01) # tomamos valores de eps < 0.44
silhouette_values = []

# Representamos s en función de épsilon
for eps in epsilon_values:
    db = DBSCAN(eps=eps, min_samples=10, metric='euclidean').fit(X)
    silhouette_ = metrics.silhouette_score(X, db.labels_)
    silhouette_values.append(silhouette_)
silhouette_values_ = np.array(silhouette_values)

fig = plt.figure(figsize=(8,4))
plt.plot(epsilon_values, silhouette_values_, 'ro--', lw=1, markersize=5,
         markeredgecolor='k')
plt.title('Euclidean Metric', fontsize=12, fontweight='bold')
plt.xlabel('Epsilon'), plt.ylabel('Shilhouette Coefficient')
plt.grid()
plt.show()

epsilon = epsilon_values[silhouette_values_.argmax()] # Tomamos el épsilon con
                                                      # mayor coef. silhouette

print ('Umbral de distancia elegido: epsilon = %0.2f' % epsilon)
print()

# Aplicamos el algoritmo con el épsilon óptimo elegido

db = DBSCAN(eps=0.24, min_samples=10, metric='euclidean').fit(X)
core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
core_samples_mask[db.core_sample_indices_] = True
labels = db.labels_

# Number of clusters in labels, ignoring noise if present.
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
n_noise_ = list(labels).count(-1)

print('Estimated number of clusters: %d' % n_clusters_)
print('Estimated number of noise points: %d' % n_noise_)
print("Adjusted Rand Index: %0.3f"
      % metrics.adjusted_rand_score(labels_true, labels))
print("Silhouette Coefficient: %0.3f"
      % silhouette_values_.max())

# Representamos el resultado con un plot

unique_labels = set(labels)
colors = [plt.cm.Spectral(each)
          for each in np.linspace(0, 1, len(unique_labels))]

plt.figure(figsize=(8,4))
for k, col in zip(unique_labels, colors):
    if k == -1:
        # Black used for noise.
        col = [0, 0, 0, 1]

    class_member_mask = (labels == k)

    xy = X[class_member_mask & core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
             markeredgecolor='k', markersize=5)

    xy = X[class_member_mask & ~core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
             markeredgecolor='k', markersize=3)

plt.title('Estimated number of DBSCAN clusters: %d' % n_clusters_)
plt.show()

# ########################### DBSCAN MANHATTAN ################################
print (' DBSCAN MANHATTAN METRIC '.center(80,'#'))
# Para eps > 0.51 surgen problemas, ya que nos conduce a clasificar los puntos
# en un sólo cluster y no puede calcular el coef. de Silhouette

epsilon_values = np.arange(.10, .52, .01) # epsilon < 0.53
silhouette_values = []

# Representamos los coeficientes de Silhouette en función del umbral distancia

for eps in epsilon_values:
    db = DBSCAN(eps=eps, min_samples=10, metric='manhattan').fit(X)
    silhouette_ = metrics.silhouette_score(X, db.labels_)
    silhouette_values.append(silhouette_)
silhouette_values_ = np.array(silhouette_values)

fig = plt.figure(figsize=(10,4))
plt.plot(epsilon_values, silhouette_values_, 'ro--', lw=1, markersize=5,
         markeredgecolor='k')
plt.title('Manhattan Metric', fontsize=12, fontweight='bold')
plt.xlabel('Épsilon'), plt.ylabel('Shilhouette Coefficient')
plt.grid()
plt.show()

epsilon = epsilon_values[silhouette_values_.argmax()] # Tomamos el épsilon cuyo
                                                      # silhouette es más elevado

print ('Chosen distance threshold: epsilon = %0.2f' % epsilon)
print()

# Aplicamos el algoritmo, esta vez con el épsilon óptimo

db = DBSCAN(eps=epsilon, min_samples=10, metric='manhattan').fit(X)
core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
core_samples_mask[db.core_sample_indices_] = True
labels = db.labels_

# Number of clusters in labels, ignoring noise if present.
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
n_noise_ = list(labels).count(-1)

print('Estimated number of clusters: %d' % n_clusters_)
print('Estimated number of noise points: %d' % n_noise_)
print("Adjusted Rand Index: %0.3f"
      % metrics.adjusted_rand_score(labels_true, labels))
print("Silhouette Coefficient: %0.3f"
      % silhouette_values_.max())

# Representamos el resultado con un plot

unique_labels = set(labels)
colors = [plt.cm.Spectral(each)
          for each in np.linspace(0, 1, len(unique_labels))]

plt.figure(figsize=(8,4))
for k, col in zip(unique_labels, colors):
    if k == -1:
        # Black used for noise.
        col = [0, 0, 0, 1]

    class_member_mask = (labels == k)

    xy = X[class_member_mask & core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
             markeredgecolor='k', markersize=5)

    xy = X[class_member_mask & ~core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
             markeredgecolor='k', markersize=3)

plt.title('Estimated number of DBSCAN clusters: %d' % n_clusters_)
plt.show()

"""
Se intentó aplicar un método para la elección de épsilon basado en la curvatura
de la gráfica que pinta el código de abajo sin llegar a resultados concluyentes.

neigh = NearestNeighbors(n_neighbors=2, metric='manhattan')
nbrs = neigh.fit(X)
distances, indices = nbrs.kneighbors(X)

distances = np.sort(distances, axis=0)
distances = distances[:,1]
fig = plt.figure()
plt.plot(distances)
plt.grid()
plt.show()
"""


import getChannelInfo
from flask import Flask, request, url_for, redirect, render_template

app = Flask(__name__)

@app.route("/", methods=["POST","GET"])
def home():
    if request.method == "POST":
        return redirect(url_for("results", youtubeUrl = request.form["url"].split('.com/')[-1]))
    else:
        return render_template("home.html")

@app.route("/<youtubeUrl>/", methods=["POST","GET"])
def results(youtubeUrl):
    if request.method == "POST":
        return redirect(url_for("results", youtubeUrl = request.form["url"].split('.com/')[-1]))
    elif 'watch?v=' in youtubeUrl:
        channelInfos = getChannelInfo.getChannelInfo(youtubeUrl)
        return render_template("result.html", channelNameToHtml = channelInfos[0], channelViewsToHtml = channelInfos[1],
                                              channelSubsToHtml = channelInfos[2], channelNumOfVideosToHtml = channelInfos[3])
    else:
        return redirect(url_for("wrongUrl", channelNameToHtml = youtubeUrl))

@app.route("/erro/", methods=["POST", "GET"])
def wrongUrl():
    if request.method == "POST":
        return redirect(url_for("results", youtubeUrl = request.form["url"].split('.com/')[-1]))
    else:
        return render_template("erro.html")

if __name__ == "__main__":
    app.run(debug=True)


import numpy as np
import cv2
import os
from cv2 import dnn

def ColorizeImage(sourceImg, save):
    if(os.path.exists(sourceImg) == False):
        return
    proto_file = 'Model\colorization_deploy_v2.prototxt'
    model_file = 'Model\colorization_release_v2.caffemodel'
    hull_pts = 'Model\pts_in_hull.npy'

    # Reading the model params
    net = dnn.readNetFromCaffe(proto_file, model_file)
    kernel = np.load(hull_pts)

    # Reading and processing the image
    img = cv2.imread(sourceImg)
    imgDimensions = img.shape
    scaled = img.astype("float32") / 255.0
    lab_img = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB)

    # Add the cluster centers as 1x1 convolutions to the model
    class8 = net.getLayerId("class8_ab")
    conv8 = net.getLayerId("conv8_313_rh")
    pts = kernel.transpose().reshape(2, 313, 1, 1)
    net.getLayer(class8).blobs = [pts.astype("float32")]
    net.getLayer(conv8).blobs = [np.full([1, 313], 2.606, dtype="float32")]

    # Resize image
    resized = cv2.resize(lab_img, (224, 224))

    # Split the L channel
    L = cv2.split(resized)[0]

    # Mean substraction
    L -= 50

    # Predicting the ab channels from the input L channel
    net.setInput(dnn.blobFromImage(L))
    ab_channel = net.forward()[0, :, :, :].transpose((1, 2, 0))

    # Resize the predicted ab volume to the same dimensions as the input image
    ab_channel = cv2.resize(ab_channel, (img.shape[1], img.shape[0]))

    # Take the L channel from the image
    L = cv2.split(lab_img)[0]
    colorized = np.concatenate((L[:, :, np.newaxis], ab_channel), axis=2)

    # Convert image from LAB to BGR
    colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)
    colorized = np.clip(colorized, 0, 1)

    # Change image to 0-255 range and convert it from float32 to int
    colorized = (255 * colorized).astype("uint8")

    # Resize the images and show them together
    img = cv2.resize(img, (640, 640))
    colorized = cv2.resize(colorized, (640, 640))

    result = cv2.hconcat([img, colorized])

    if(save == True):
        colorized = cv2.resize(colorized, (imgDimensions[1], imgDimensions[0]))
        colorizedImageName = os.path.basename(sourceImg)
        cv2.imwrite(f"ConvertedImages/Colorized-{colorizedImageName}", colorized)
    else:
        cv2.imshow("Grayscale -> Color", result)
        cv2.waitKey(0)
        cv2.destroyAllWindows()


import zmq

from time import sleep

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind('tcp://127.0.0.1:2000')

messages = [100,200,300]
i = 0

while True:
    sleep(1)
    socket.send_pyobj({i:messages[i]})
    i = 0 if i == 2 else i + 1


"""
The version of the package can be returned as a single string or a dict.

When a string, it comes from the package __version__.
When a dict, it also has __version__,
as well as versions of other depdency packages.
"""

from typing import Optional

import dls_mainiac_lib.version
import dls_servbase_lib.version
import dls_utilpack.version

from soakdb3_lib import __version__

# ----------------------------------------------------------
def version() -> str:
    """
    Version of the soakdb3 package as a string.
    """

    return __version__

# ----------------------------------------------------------
def meta(given_meta: Optional[dict] = None) -> dict:
    """
    Returns version information from the soakdb3 package
    and its dependencies as a dict.
    Adds version information to a given meta dict if it was provided.
    """

    meta = {}
    meta["soakdb3_lib"] = version()
    meta.update(dls_servbase_lib.version.meta())
    meta.update(dls_mainiac_lib.version.meta())
    meta.update(dls_utilpack.version.meta())

    if given_meta is not None:
        given_meta.update(meta)
    else:
        given_meta = meta
    return given_meta


import sys
input = sys.stdin.readline
N = int(input())
# num_arr = list(map(int,input().split()))
num_arr = input().split()

def reverse(x):
    new_num = []
    for i in range(len(x),0,-1):
        new_num.append(x[i-1])
    new_num = ''.join(new_num)
    return int(new_num)

def isPrime(x):
    count = 0
    if x == 1:
        return

    for i in range(1,int(x/2)):
        if x % i == 0 :
            count += 1
    if count <= 1:
        print(x,end=' ')

for num in num_arr:
    isPrime(reverse(num))
    # isPrime(1)


from datetime import datetime
import Player

class Result:
    def __init__(self, date: datetime, place: int, player: Player, id: int = None):
        self.id = id
        self.date = date
        self.place = place
        self.player = player


import requests
import json
import re

url = "http://www.juzimi.com/article/%E6%83%85%E4%B9%A6"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36",
    "Referer": "http://www.juzimi.com/search/node/%E5%82%B2%E6%85%A2%E4%B8%8E%E5%81%8F%E8%A7%81%20type%3Asentence",
}

def get_html(params):
    try:
        response = requests.get(url, params=params, headers=headers)
        response.raise_for_status()
        print(response.url)
        print(response.status_code)
    except Exception as e:
        print(e)
        print("connection error")
        return None

    response.encoding = response.apparent_encoding
    return response.text

def parse_html(html):
    data = {}

    sentences = re.findall(r'\"xlistju\">(.+?)', html)
    loveds = re.findall(r"(喜欢\(\d*?\))", html)
    comments = re.findall(r">(评论\(?\d*?\)?)", html)

    with open("love_letter.txt", "a", encoding="utf-8") as ob:
        for i in range(len(sentences)):
            data["sentence"] = sentences[i]
            data["loved"] = loveds[i]
            data["comment"] = comments[i]

            ob.write(json.dumps(data, ensure_ascii=False))
            ob.write("\n")

def main():
    for i in range(4):
        if i:
            params = {"page":i}
        else:
            params ={}
        html = get_html(params)
        if html:
            parse_html(html)
    print("结束")

if __name__ == "__main__":
    main()


"""create table vulnerabilities

Revision ID: 7c34e9a89bc0
Revises: b21ffaeed8e4
Create Date: 2016-06-29 10:11:36.057723

"""

# revision identifiers, used by Alembic.
revision = '7c34e9a89bc0'
down_revision = 'b21ffaeed8e4'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa
import datetime

def _get_date():
  return datetime.datetime.now()

def upgrade():
  op.create_table('vulnerabilities',
                  sa.Column('id', sa.Integer, primary_key=True, nullable=False),
                  sa.Column('name', sa.Text, nullable=False),
                  sa.Column('cvss_score', sa.Float, nullable=False),
                  sa.Column('bug_id', sa.Text),
                  sa.Column('family', sa.Text),
                  sa.Column('cve_id', sa.Text),
                  sa.Column('inventory_host_id', sa.Integer, sa.ForeignKey('inventory_hosts.id', ondelete='cascade')),
                  sa.Column('port', sa.Text),
                  sa.Column('threat_score', sa.Text),
                  sa.Column('severity_score', sa.Float),
                  sa.Column('xrefs', sa.Text),
                  sa.Column('tags', sa.Text),
                  sa.Column('validated', sa.BOOLEAN),
                  sa.Column('created_at', sa.TIMESTAMP(timezone=False), default=_get_date))

def downgrade():
  op.drop_table('vulnerabilities')


#!/usr/bin/env python                                                          #
#                                                                              #
# Autor: Milena Crnogorcevic                                                   #
# Date created: 7/22/2019                                                      #
# Last edit: 10/20/2020                                                        #
# Use: Fitting the ALP model to the simulated spectra.                         #
#------------------------------------------------------------------------------#
# Example to run the script (adjust the paths to the SED, energy, bkg, fak, and rsp):
# python ALP_fit.py

# Importing the necessary packages
import numpy as np
import argparse
from scipy.interpolate import interp1d

# importing XSPEC
import xspec as xs
xs.Xset.allowPrompting = False #keeps pyxspec from hanging, waiting a response to a prompt
xs.Xset.allowNewAttributes = True

__description__ = 'Fit ALP model for different normalization values for a given response and background.'

N = np.logspace(np.log10(8.4e-8), np.log10(8.4e2), 30) # normalization values, [1e-52 cm^-2]

E_LLE = np.load(args.emev) # LLE energy bins, [MeV]
SED = np.load(args.sed) # the output of Manuel's code, N = 1 cm^-2

f = interp1d(E_LLE*1000, SED/1000, 'cubic', fill_value="extrapolate") # in keV and per keV

# defining Xspec model
def ALP(engs, params, flux):
    energy_binsizes = np.ediff1d(engs)
    # finding the average bin value for energy
    energy = (np.asarray(engs[:-1])+np.asarray(engs[1:]))/2
    flux[:] = f(energy) * energy_binsizes

ALPInfo = ()
xs.AllModels.addPyMod(ALP, ALPInfo, 'add', spectrumDependent=False)

normALP = np.zeros((30,2001))
chi_stat = np.zeros((30,2001))
Fit_st = np.zeros((30,2001))

xs.AllData.clear()   # clear all data, if any.

for j in range(30):
    for i in range(1,2001):
        xs.AllData.clear()
        s=xs.Spectrum("bn150416773_fakeit_%s.fak{%s}" %(j,i))
        s.response = "bn150416773_LAT-LLE_weightedrsp.rsp"
        s.background= ("bn150416773_LAT-LLE_bkgspectra.bak{1}")
        xs.AllModels.clear()
        m=xs.Model("ALP")
        xs.Fit.statMethod = "pgstat"
        #List of value floats [val,delta,min,bot,top,max].
        #m.ALP.norm.values=[N[j], N[j]*0.1, N[j]*0.1, N[j]*0.95, N[j]*1.05, N[j]*1.95]
        m.ALP.norm.frozen = False
        xs.Fit.perform()
        normALP[j,i] = m.ALP.norm.values[0]
        chi_stat[j,i] = xs.Fit.testStatistic
        Fit_st[j,i] = xs.Fit.statistic

    np.save('TS_pgfit_ALP.npy', chi_stat)
    np.save('pgfit_ALP.npy', Fit_st)
    np.save('norm_ALP.npy', normALP)


filename = "Inputs/" + __file__.strip("py") + "txt"
with open(filename, "r") as file:
    data = file.readlines()

import operator

scanners = []

scannerIndex = 0
for line in data:
    if line.startswith("---"):
        scanners.append([])
        continue

    if line.isspace():
        scannerIndex += 1
        continue

    coords = line.split(",")
    scanners[scannerIndex].append((int(coords[0]), int(coords[1]), int(coords[2])))

# XYZ    = 0   YZX    = 4   ZXY    = 8
# XZIY   = 1   YXIZ   = 5   ZYIX   = 9
# XIYIZ  = 2   YIZIX  = 6   ZIXIY  = 10
# XIZY   = 3   YIXZ   = 7   ZIYX   = 11
#
# IXZY   = 12  IYXZ   = 16  IZYX   = 20
# IXYIZ  = 13  IYZIX  = 17  IZXIY  = 21
# IXIZIY = 14  IYIXIZ = 18  IZIYIX = 22
# IXIYZ  = 15  IYIZX  = 19  IZIXY  = 23

def RotateScanner(scanner, space):
    rotatedBeacons = []

    for beacon in scanner:
        x, y, z = 0, 0, 0

        if   space in range(4):        x =  beacon[0]
        elif space in range(4,   8):   x =  beacon[1]
        elif space in range(8,  12):   x =  beacon[2]
        elif space in range(12, 16):   x = -beacon[0]
        elif space in range(16, 20):   x = -beacon[1]
        else:                          x = -beacon[2]

        if   space in [5,  8, 16, 21]: y =  beacon[0]
        elif space in [0,  9, 13, 20]: y =  beacon[1]
        elif space in [1,  4, 12, 17]: y =  beacon[2]
        elif space in [7, 10, 18, 23]: y = -beacon[0]
        elif space in [2, 11, 15, 22]: y = -beacon[1]
        else:                          y = -beacon[2]

        if   space in [4, 11, 19, 20]: z =  beacon[0]
        elif space in [3,  8, 12, 23]: z =  beacon[1]
        elif space in [0,  7, 15, 16]: z =  beacon[2]
        elif space in [6,  9, 17, 22]: z = -beacon[0]
        elif space in [1, 10, 14, 21]: z = -beacon[1]
        else:                          z = -beacon[2]

        rotatedBeacons.append((x, y, z))

    return rotatedBeacons

def Part1And2():
    trueScanners = [[] for index in range(len(scanners))]
    trueScanners[0] = scanners[0]
    scannerPositions = [(0,0,0)]

    checkedScanners = {0}
    checkingScanners = [0]
    while len(checkingScanners) and len(checkedScanners) < len(trueScanners):
        latestScanner = checkingScanners.pop()
        for index in range(len(scanners)):
            if index in checkedScanners:
                continue

            for space in range(24):
                scanner = RotateScanner(scanners[index], space)

                scannerFound = False
                for fixedBeacon in trueScanners[latestScanner]:
                    for beacon in scanner[:-11]:
                        offset = tuple(map(operator.sub, fixedBeacon, beacon))
                        offsetScanner = [tuple(map(operator.add, offset, curBeacon)) for curBeacon in scanner]

                        if len(set(trueScanners[latestScanner]) & set(offsetScanner)) >= 12:
                            trueScanners[index] = offsetScanner
                            scannerPositions.append(offset)
                            checkedScanners.add(index)
                            checkingScanners.append(index)
                            scannerFound = True
                            break

                    if scannerFound:
                        break

                if scannerFound:
                    break

    beaconSet = set()
    beaconSet = beaconSet.union(*trueScanners)

    scannerDistances = [sum(tuple(map(abs, map(operator.sub, scanner1, scanner2)))) for scanner1 in scannerPositions for scanner2 in scannerPositions]

    return len(beaconSet), max(scannerDistances)

print(Part1And2())


'''Created on 2021-11-03

@author: Gabriel Bailey

This code will solve Burger's equation for a given equation using the leapfrog method. It will produce 4 graphs of the equation at different times, and also an animated graph over time if desired.
'''
# Started from laplace.py

from numpy import pi,zeros, sin,linspace
import matplotlib.pyplot as plt

# Constants
epsilon = 1      # Target accuracy
delta_x = 0.02      #
delta_t = 0.005     #
Lx = 2*pi
Tf = 2

Nx = int(Lx//delta_x)
Nt = int(Tf//delta_t)
# Create arrays to hold potential values
u = zeros([Nx, Nt],float)
# Set initial conditions
u[0,:] = 0
u[Nx-1,:] = 0
for x in range(1,Nx-1):
    u[x,0] = sin(x*delta_x)
    # Also forward Euler step:
    u[x,1] = u[x,0] - epsilon*delta_t/(4*delta_x)*(u[x+1,0]**2-u[x-1,0]**2)

# Fill out the rest of the points using leapfrog
beta = epsilon*delta_t/delta_x
for t in range(1,Nt-1):
    for x in range(1,Nx-1):
        u[x,t+1] = u[x,t-1] - beta/2*(u[x+1,t]**2-u[x-1,t]**2)
        '''try:
            u[x,t+1] = u[x,t-1] - beta/2*(u[x+1,t]**2-u[x-1,t]**2)
        except:
            print(t,x,u[x,t-1],u[x+1,t],u[x-1,t])'''

#This takes the array of values at x,t and the target time and produces a plot for that time.
def plot_result(u,time):
    t = int(time/Tf * Nt)
    #print(t)
    fig = plt.figure(figsize=[10,5])
    ax = fig.add_subplot(1,1,1)
    ax.set_xlim(0, Lx)
    ax.set_ylim(-1.6, 1.6)
    x = linspace(0, Lx, Nx)
    ax.plot(x,u[:,t])

    ax.grid(which='both', axis='y')
    ax.set_title('Burger\'s Equation at Time {}s'.format(time))
    ax.set_xlabel('x')
    ax.set_ylabel('u')
    return ax

# Plot the 4 times
plot_result(u,0.0)
plot_result(u,0.5)
plot_result(u,1.0)
plot_result(u,1.5)
plt.show()

# Plot it as an animation:
""" from matplotlib.animation import FuncAnimation, FFMpegWriter

fig, ax = plt.subplots()
ax.grid(which='both', axis='y')
ax.set_title('Burger\'s Equation')
ax.set_xlabel('x')
ax.set_ylabel('Value')
xdata, ydata = [], []
line, = plt.plot([], [])
time_text = ax.text(0.1, 0.1, 'time = ', transform=ax.transAxes)

def init():
    ax.set_xlim(0, 2*pi)
    ax.set_ylim(-2, 2)
    time_text.set_text('time = ')
    return line,

def update(i):
    i = int(i)
    x = linspace(0, Lx, Nx)
    y = u[:,i]
    line.set_data(x, y)
    time_text.set_text('time = {:1.2f}'.format(i*delta_t))
    return line, time_text

ani = FuncAnimation(fig, update, frames=Nt, interval=1,
                    init_func=init, blit=True)

# Save the animation (need ffmpeg)
# f = 'Q3_vid.mp4'
# writervideo = FFMpegWriter(fps=60)
# ani.save(f, writer=writervideo)

plt.show() """


import numpy as np
import matplotlib.pyplot as plt

def pie_charts(sizes,title):
    #adjust the size of figure
    plt.figure(figsize=(6,9))
    #define label
    labels = ['0-17 years','18-64 years','65+ years']

    colors = ['yellow','lightskyblue','yellowgreen']
    #The larger the value is, the larger the gap is
    explode = (0.02,0.02,0.005)
    patches,text1,text2 = plt.pie(sizes,
                          explode=explode,
                          labels=labels,
                          colors=colors,
                          labeldistance = 1.2,
                          autopct = '%3.2f%%',
                          shadow = False,
                          startangle =90,
                          pctdistance = 0.6)
    for t in text1:
        t.set_size(17)
    for t in text2:
        t.set_size(17)

    #x,y is consistent so ensure it's a circle
    plt.axis('equal')
    #plt.legend()
    plt.title(title,fontsize=25)
    plt.show()

death_proportion = [1282,9565,1621]
title1 = 'Deaths Proportion'
hosp_proportion  = [86813,160229,27263]
title2 = 'Hospitalizations Proportion'
cases_proportion = [19501004,35392931,5943813]
title3 = 'Cases Proportion'
pie_charts(death_proportion,title1)
pie_charts(hosp_proportion,title2)
pie_charts(cases_proportion,title3)


from itertools import compress

import numpy as np

from .object_intersect import partition, get_ob_2dbboxes, get_vert_co_2d, get_ob_loc_co_2d, do_selection
from .polygon_tests import point_inside_rectangles, points_inside_rectangle, segments_intersect_rectangle

def get_obs_mask_in_selbox(obs, obs_mask_check, depsgraph, region, rv3d, xmin, xmax, ymin, ymax):
    list_of_obs_to_check = compress(obs, obs_mask_check)
    bool_list = []

    for ob in list_of_obs_to_check:
        ob_eval = ob.evaluated_get(depsgraph)
        me = ob_eval.to_mesh(preserve_all_data_layers=False, depsgraph=None)
        vert_co_2d = get_vert_co_2d(me, ob_eval, region, rv3d)
        verts_mask_in_selbox = points_inside_rectangle(vert_co_2d, xmin, xmax, ymin, ymax)
        if np.all(verts_mask_in_selbox):
            bool_list.append(True)
        else:
            bool_list.append(False)
        ob_eval.to_mesh_clear()

    bools = np.fromiter(bool_list, "?", len(bool_list))
    return bools

def select_obs_in_box(context, mode, xmin, xmax, ymin, ymax, behavior):
    region = context.region
    rv3d = context.region_data
    depsgraph = context.evaluated_depsgraph_get()

    selectable_obs = context.selectable_objects

    if behavior == 'CONTAIN':
        mesh_obs, nonmesh_obs = partition(selectable_obs, lambda o: o.type in {'MESH', 'CURVE', 'FONT'})
        mesh_ob_count = len(mesh_obs)

        # Get coordinates of 2d bounding boxes of objects.
        (
            ob_2dbbox_xmin,
            ob_2dbbox_xmax,
            ob_2dbbox_ymin,
            ob_2dbbox_ymax,
            ob_2dbbox_points,
            ob_2dbbox_segments,
            obs_mask_2dbbox_entire_clip,
        ) = get_ob_2dbboxes(mesh_obs, mesh_ob_count, region, rv3d)

        # Check for bounding boxes intersections with selection box.
        # Speed up finding overlaps or intersections by doing polygon tests on bounding boxes.

        # Ob bbox intersects selection box.
        segment_bools = segments_intersect_rectangle(ob_2dbbox_segments, xmin, xmax, ymin, ymax, prefilter=True)
        segment_bools.shape = (mesh_ob_count, 4)
        obs_mask_2dbbox_isect_selbox = np.any(segment_bools, axis=1)

        # Ob bbox bbox entirely inside selection box.
        point_bools = points_inside_rectangle(ob_2dbbox_points, xmin, xmax, ymin, ymax)
        point_bools.shape = (mesh_ob_count, 4)
        obs_mask_2dbbox_entire_in_selbox = np.all(point_bools, axis=1)

        # Cursor is inside ob bbox.
        obs_mask_cursor_in_2dbbox = point_inside_rectangles(
            (xmin, ymin), ob_2dbbox_xmin, ob_2dbbox_xmax, ob_2dbbox_ymin, ob_2dbbox_ymax
        )

        obs_mask_dont_check = obs_mask_2dbbox_entire_in_selbox | obs_mask_2dbbox_entire_clip
        obs_mask_check = (
            obs_mask_2dbbox_isect_selbox | (obs_mask_cursor_in_2dbbox & ~obs_mask_2dbbox_isect_selbox)
        ) & ~obs_mask_dont_check

        mesh_obs_mask_in_selbox = obs_mask_2dbbox_entire_in_selbox
        mesh_obs_mask_in_selbox[obs_mask_check] = get_obs_mask_in_selbox(
            mesh_obs, obs_mask_check, depsgraph, region, rv3d, xmin, xmax, ymin, ymax
        )
        do_selection(mesh_obs_mask_in_selbox, mesh_obs, mode)

        nonmesh_ob_co_2d = get_ob_loc_co_2d(nonmesh_obs, region, rv3d)
        nonmesh_obs_mask_in_selbox = points_inside_rectangle(nonmesh_ob_co_2d, xmin, xmax, ymin, ymax)
        do_selection(nonmesh_obs_mask_in_selbox, nonmesh_obs, mode)

    if behavior == 'ORIGIN':
        ob_co_2d = get_ob_loc_co_2d(selectable_obs, region, rv3d)
        obs_mask_in_selbox = points_inside_rectangle(ob_co_2d, xmin, xmax, ymin, ymax)
        do_selection(obs_mask_in_selbox, selectable_obs, mode)