from __future__ import annotations

import logging
import sys
from typing import Dict, Optional, Any, List

from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import JSONResponse

from same_thing.db import purge_data_dbs
from same_thing.exceptions import UriNotFound
from same_thing.query import get_cluster, UriCluster

debug = '--debug' in sys.argv
if debug:
    # assume this is run in a single process
    purge_data_dbs()

app = Starlette(debug=debug)

@app.on_event('startup')
def log_ready_message() -> None:
    logger = logging.getLogger('uvicorn')
    logger.info('Same Thing Service is ready for lookups.')

@app.route('/lookup/', methods=['GET'])
def lookup(request: Request) -> JSONResponse:
    single_uri = request.query_params.get('uri')
    uris = request.query_params.getlist('uris') or [single_uri]
    if not any(uris):
        return JSONResponse({
            'uri': 'The `uri` parameter must be provided.'
        }, status_code=400)

    fields_by_uri: Dict[str, Optional[UriCluster]] = {}
    for uri in uris:
        try:
            fields_by_uri[uri] = get_cluster(uri)
        except UriNotFound:
            fields_by_uri[uri] = None

    response_fields: Dict[str, Any] = {}
    if not any(fields_by_uri.values()):
        if single_uri:
            return single_uri_not_found(single_uri)
        else:
            return multiple_uris_not_found(uris)
    elif single_uri:
        response_fields = fields_by_uri[single_uri]
    else:
        response_fields['uris'] = fields_by_uri

    meta = request.query_params.get('meta')
    if not (meta and meta == 'off'):
        response_fields['meta'] = {
            'documentation': 'http://dev.dbpedia.org/Global%20IRI%20Resolution%20Service',
            'github': 'https://github.com/dbpedia/dbp-same-thing-service',
            'license': 'http://purl.org/NET/rdflicense/cc-by3.0',
            'license_comment': 'Free service provided by DBpedia. Usage and republication of data implies that you '
                               'attribute either http://dbpedia.org as the source or reference the latest general '
                               'DBpedia paper or the specific paper mentioned in the GitHub Readme.',
            'comment': """
                The service resolves any IRI to its cluster and displays the global IRI and its cluster members.
                Cluster members can change over time as the DBpedia community,
                data providers and professional services curate the linking space.

                Usage note:
                1. Save the global ID AND the local IRI that seems most appropriate.
                   It is recommended that you become a data provider, in which case the local IRI would be your IRI.
                2. Use the global ID to access anything DBpedia.
                3. Use the stored local ID to update and re-validate linking and clusters.
            """,
        }

    return JSONResponse(response_fields)

def single_uri_not_found(uri: str) -> JSONResponse:
    return JSONResponse({
        'uri': uri
    }, status_code=404)

def multiple_uris_not_found(uris: List[str]) -> JSONResponse:
    return JSONResponse({
        'uris': uris
    }, status_code=404)


# coding=utf-8
# author:xsl

from bson import ObjectId
from mongoengine import (
        StringField,
        IntField,
        BooleanField,
        ObjectIdField,
        ListField,
        DictField,
        DateTimeField,
        Document,
        )

class Source(Document):
    meta = {
        'db_alias': 'testdb',
        'index_background': True,
        'indexes': [],
    }

    id = ObjectIdField(primary_key=True, default=ObjectId)
    userid = ObjectIdField()
    name = StringField()
    desc = StringField()
    url = StringField()
    type = StringField()
    thirdid = StringField()
    extra = StringField()

    def save(self, *args, **kwargs):
        return super(Source, self).save(*args, **kwargs)


from selenium import webdriver
import requests
from lxml import etree
import time
from bs4 import BeautifulSoup
import json as js

if __name__ == '__main__':
    chromedriver = "/Users/heweiwen/Downloads/work/Solf/chrome-mac/chromedriver"
    driver = webdriver.Chrome(executable_path=chromedriver)
    home_url = "https://drugs.dxy.cn"
    driver.get(home_url)  # 打开网址
    driver.get(home_url)
    drugs_1 = etree.HTML(driver.page_source)
    drugs_1_list = drugs_1.xpath('//div[@class="category-tab__37us"]/span/text()')
    drugs_1_tab = drugs_1.xpath('//div[@class="ant-tabs-tab"]')

    html = driver.page_source
    # BeautifulSoup转换页面源码
    bs = BeautifulSoup(html, 'lxml')
    # 获取Script标签下的完整json数据，并通过json加载成字典格式
    contents = bs.find("script", {"id": "__NEXT_DATA__"}).contents
    # json 父子级别
    js_test = {}
    pageProps = {}
    # 父级
    firstLevelCategoryList = []
    # 子级
    secondLevelCategoryList = []
    # firstjson
    for i in range(0, len(contents)):
        print(contents[i])
        js_test = js.loads(contents[i])
        pageProps = js_test['props']['pageProps']
        # pageProps = js.dumps(contents[i]).find('props')
    firstLevelCategoryList = pageProps['firstLevelCategoryList']
    secondLevelCategoryList = pageProps['secondLevelCategoryList']

    for f in firstLevelCategoryList:
        id = f['id']
        name = f['name']
        # 获取所有子集
        secondsList = []
        for s in secondLevelCategoryList:
            if s['supId'] == id:
                seconds = {}
                seconds['id'] = s['id']
                seconds['name'] = s['name']
                seconds['supId'] = s['supId']
                seconds['recommendDrugs'] = s['recommendDrugs']
                secondsList.append(seconds)

    for i in range(0, len(drugs_1_tab)):
        driver.find_element_by_xpath('//div[@style="margin-bottom:0"]').click()
    drugs_1_tabpanel = drugs_1.xpath('//div[@role="tabpanel"]')

    for div in drugs_1_tab:
        # driver.find_element_by_xpath('//div[@class="ant-tabs-tab"]').click()
        # bottom = div.xpath('./div[@class="ant-tabs-tab-btn"]/@id')
        id = div.xpath('./div[@class="ant-tabs-tab-btn"]/@id')
        print(id)
    for div in drugs_1_tabpanel:
        tabpanel_names = div.xpath('./ul/li[@class="category-item__ZnzJ"]')
        for div_1 in tabpanel_names:
            nams = div_1.xpath('./h3/a/text()')
            print(nams)


import os
import sys
from configparser import ConfigParser
from n6wells.db import *
from n6wells.container import fixtures
from n6wells.xaction import Xaction
from n6wells.sample import Sample, SampleType, Plating
from n6wells.container import ContainerType, Container, ContainerInst
from configparser import ConfigParser

def get_config():
    fname = os.path.join( os.path.dirname( sys.modules[__name__].__file__ ), 'intgrtest.ini')
    config = ConfigParser()
    config.read( fname )
    return config

@committer
def populate_plate(db):
    xaction = Xaction()
    db.add(xaction)
    db.commit()
    typ1, typC = SampleType(name='patient'), SampleType(name='control')
    s1, s2 = Sample(sample_type=typ1), Sample(sample_type=typC)
    plate96_t = db.query(ContainerType).filter(ContainerType.name=='Plate-96_well').one()
    plate = ContainerInst( name='my_plate', type=plate96_t )
    db.add(plate)
    db.commit()

    well1 = db.query(Container).filter(Container.type == plate96_t).filter(Container.name == 'A01').one()
    well2 = db.query(Container).filter(Container.type == plate96_t).filter(Container.name == 'A02').one()
    p1 = Plating( plate=plate, well=well1, sample=s1, xaction=xaction )
    p2 = Plating( plate=plate, well=well2, sample=s2, xaction=xaction )
    db.add( p1 )
    db.add( p2 )

def main():
    print('@@ helper main')
    config = get_config()
    destroydb(config['DB']['engine'])
    initdb(config['DB']['engine'])
    DB << fixtures.load()
    DB << populate_plate()


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

from django.db import migrations, models
import filebrowser.fields

class Migration(migrations.Migration):

    dependencies = [
        ('newsroom', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Tweet',
            fields=[
                ('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)),
                ('wait_time', models.PositiveIntegerField(help_text='Number of minutes after publication till tweet.')),
                ('status', models.CharField(choices=[('scheduled', 'Scheduled'), ('sent', 'Sent'), ('failed', 'Failed'), ('paused', 'Paused')], max_length=20, default='scheduled')),
                ('tweet_text', models.CharField(max_length=117, blank=True)),
                ('image', filebrowser.fields.FileBrowseField(blank=True, max_length=200, null=True)),
                ('characters_left', models.IntegerField(default=116)),
                ('article', models.ForeignKey(to='newsroom.Article')),
            ],
            options={
                'ordering': ['article__published', 'wait_time'],
            },
        ),
        migrations.CreateModel(
            name='TwitterHandle',
            fields=[
                ('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)),
                ('name', models.CharField(unique=True, max_length=200)),
                ('slug', models.SlugField(unique=True, max_length=200)),
            ],
            options={
                'verbose_name': 'Twitter handle',
                'ordering': ['name'],
                'verbose_name_plural': 'Twitter Handles',
            },
        ),
        migrations.AddField(
            model_name='tweet',
            name='tag_accounts',
            field=models.ManyToManyField(to='socialmedia.TwitterHandle', blank=True),
        ),
    ]


import base.invoke.tasks.run
import invoke
import re
import sys
import yaml

@invoke.task
def update_base():
    invoke.run('git pull https://github.com/fogies/web-jekyll-base.git master', encoding=sys.stdout.encoding)
    invoke.run('git pull https://github.com/fogies/invoke-base.git master', encoding=sys.stdout.encoding)

@invoke.task
def update_dependencies():
    # Parameters to keep everything silent
    params_silent = {
        # Some of the commands output characters that cause Unicode errors on Windows,
        # so we set an encoding that will help the command line behave
        'encoding': sys.stdout.encoding,
        'hide': 'both',
        'warn': True
    }

    # Parse our config
    with open('_base_config.yml') as f:
        base_config_yaml = yaml.safe_load(f)

    # Python dependencies
    print('Checking Python dependencies')

    # Ensure we have a current version of pip (e.g., as needed by pip-tools)
    pip_version_desired = base_config_yaml['config']['python']['pip_version']
    result = invoke.run('pip --disable-pip-version-check show pip', **params_silent)
    base.invoke.tasks.run.check_result(result, 'check pip version')

    pip_version_current = re.search('^Version: ([\d.]+)', result.stdout, re.MULTILINE).group(1)
    if pip_version_current != pip_version_desired:
        result = invoke.run(
            'python -m pip install --upgrade pip=={}'.format(
                pip_version_desired
            ),
            **params_silent
        )
        base.invoke.tasks.run.check_result(result, 'update pip version')

    # Ensure we have pip-tools, which will be in our requirements file
    # pip-sync also currently does not respect options in the requirements file,
    # so installing the entire file ensures pip-sync only needs to delete
    result = invoke.run('pip --disable-pip-version-check install -r requirements3.txt', **params_silent)
    base.invoke.tasks.run.check_result(result, 'install pip requirements')
    # Ensure we have exactly our dependencies
    result = invoke.run('pip-sync requirements3.txt', **params_silent)
    base.invoke.tasks.run.check_result(result, 'sync pip requirements')

    # Node dependencies
    if base_config_yaml['config'].get('node', {}).get('required', False):
        print('Checking Node.js dependencies')
        result = invoke.run('npm install', **params_silent)
        base.invoke.tasks.run.check_result(result, 'install Node.js dependencies')

    # Ruby dependencies
    if base_config_yaml['config'].get('ruby', {}).get('required', False):
        print('Checking Ruby dependencies')

        # Check we have the correct Bundler version
        bundler_version_desired = base_config_yaml['config']['ruby']['bundler_version']

        result = invoke.run('gem list -i bundler -v {}'.format(bundler_version_desired), **params_silent)
        # expected to fail if the desired version is not installed
        if result.failed:
            result = invoke.run('gem install bundler -v {}'.format(bundler_version_desired), **params_silent)
            base.invoke.tasks.run.check_result(result, 'install bundler')

        # And only the correct Bundler version
        # Expected to fail if no other versions of bundler are installed
        invoke.run('gem uninstall bundler -v "!={}"'.format(bundler_version_desired), **params_silent)

        # Check we have our Ruby dependencies
        result = invoke.run('bundle check', **params_silent)
        # expected to fail if we are missing dependencies
        if result.failed:
            result = invoke.run('bundle install', **params_silent)
            base.invoke.tasks.run.check_result(result, 'bundle install')


from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('headless')

try:
    driver = webdriver.Remote(
        command_executor='http://chrome:4444/wd/hub',
        options=chrome_options
    )
    driver.get("http://www.baidu.com")
    print(driver.title)
    driver.quit()
except Exception as e:
    print("An error occurred: ", e)


# encoding: utf-8
# Created on 2014-5-22
# @author: binge

from scrapy.selector import Selector
from scrapy import log
from utils.item_helper import ItemHelper
from scrapy.http.request import Request
import datetime
from utils.common import SpiderHelper, TimeHelper
from utils.conns_helper import RedisHelper
from spiders.common_spider import CommonSpider

class K17Spiser(CommonSpider):

    name = 'k17'

    def __init__(self, **kwargs):
        self.start_urls = [
                           'http://all.17k.com/all'
                           ]
        self.source_name = u'17K小说网'
        self.domain = 'http://www.17k.com'
        self.next_page_domain = 'http://all.17k.com'
        self.home_spider = SpiderHelper.get_source_home_spider(self.name)
        last_crawl_time_str = RedisHelper.get_last_crawl_time(self.name)
        if not last_crawl_time_str:
            self.last_crawl_time = TimeHelper.time_2_str(frt='%Y-%m-%d') + ' 00:00:00'
        else:
            self.last_crawl_time = last_crawl_time_str
        next_crawl_time = TimeHelper.time_2_str(delta= -SpiderHelper.get_every_crawl_timedelta_mins(), delta_unit='minutes')
        RedisHelper.set_next_crawl_time(self.name, next_crawl_time)
        self.source_short_name = 'k17'

    def parse(self, response):
        is_continue = True
        hxs = Selector(response)
        book_nodes = hxs.xpath('//div[@class="alltable"]//tr[position() > 2]')
        if not book_nodes:
            self.log(message='%s spider get nothing in home page, current url is %s' % (self.name, response._get_url()), level=log.WARNING)
            is_continue = False
        else:
            for bn in book_nodes:
                u_time = bn.xpath('td[@class="td7"]/child::text()').extract()[0] + ":00"
                if u_time >= self.last_crawl_time:
                    source = bn.xpath('td[@class="td3"]//a/@href').extract()[0]  # first link
                    name = bn.xpath('td[@class="td3"]//a/child::text()').extract()[0]  # book name
                    author = bn.xpath('td[@class="td6"]/a/child::text()').extract()[0]

                    yield ItemHelper.gene_book_item(name, source, author, self.source_name, self.home_spider, self.source_short_name)
                else:
                    is_continue = False  # if the section publish time is less than last crawl time, can't continue.
                    break
        if is_continue:
            next_page_nodes = hxs.xpath('//a[@class="on"]/following-sibling::a')
            if next_page_nodes:
                next_page = self.next_page_domain + next_page_nodes[0].xpath('@href').extract()[0]
                yield Request(next_page, callback=self.parse)
            else:
                self.log(message='%s spider cannot get next page, current url is %s' % (self.name, response._get_url()), level=log.WARNING)
        else:
            self.log(message='%s spider sleep wait for next round.' % self.name, level=log.INFO)
            self.last_crawl_time = RedisHelper.get_last_crawl_time(self.name)
            next_crawl_time = TimeHelper.time_2_str(delta= -SpiderHelper.get_every_crawl_timedelta_mins(), delta_unit='minutes')
            RedisHelper.set_next_crawl_time(self.name, next_crawl_time)
            SpiderHelper.source_spider_sleep()
            yield Request(self.start_urls[0], callback=self.parse)


# -*- coding: utf-8 -*-
# @Time    : 07/11/2021 19:37
# @Author  : Ming

import pickle
import json
import numpy as np
import argparse

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--dataset', type=str, default='dunnhumby', help='Dataset')
    parser.add_argument('--avg_type', type=str, default='mean', help='x')
    parser.add_argument('--aug_pos', type=int, default=1, help='x')
    parser.add_argument('--adj_neg', type=int, default=1, help='x')
    parser.add_argument('--cfm', type=int, default=1, help='x')
    parser.add_argument('--freq', type=int, default=1, help='x')
    args = parser.parse_args()
    dataset = args.dataset
    candidate_filter = args.cfm
    freq_signal =args.freq
    aug_pos=args.aug_pos
    adjust_neg=args.adj_neg
    val_can_num = 50
    avg_type = args.avg_type

    def get_performance_dict(results_data):
        all_gather_dict = dict()
        # for can_num in [20, 50, 100, 200, 500]:
        for can_num in [20, 50, 100, 200, 500]:
            recall = []
            ndcg = []
            ihr = []
            ihn = []
            gather_dict = dict()
            for foldk in range(5):
                result = results_data[str(foldk)][str(can_num)]
                recall.append(result['Recall'])
                ndcg.append(result['NDCG'])
                ihr.append(result['HitRatio'])
                ihn.append(result['Correct'])
            gather_dict['Recall'] = np.mean(recall)
            gather_dict['NDCG'] = np.mean(ndcg)
            gather_dict['IHR'] = np.mean(ihr)
            gather_dict['IHN'] = np.mean(ihn)
            all_gather_dict[can_num] = gather_dict
        return all_gather_dict

    print(dataset)
    save_folder = f"test_results_{avg_type}/{dataset}/cand{candidate_filter}_freq{freq_signal}_aug{aug_pos}_adjust{adjust_neg}/"

    val_best_performance = 0
    for epoch in range(7):
        val_results_file = f'test_results_{avg_type}/{dataset}/cand{candidate_filter}_freq{freq_signal}_aug{aug_pos}_adjust{adjust_neg}/val_epoch_{epoch}.json'
        with open(val_results_file, 'r') as f:
            val_result_data = json.load(f)
        val_performance_dict = get_performance_dict(val_result_data)

        test_results_file = f'test_results_{avg_type}/{dataset}/cand{candidate_filter}_freq{freq_signal}_aug{aug_pos}_adjust{adjust_neg}/test_epoch_{epoch}.json'
        with open(test_results_file, 'r') as f:
            test_result_data = json.load(f)
        test_performance_dict = get_performance_dict(test_result_data)
        if val_performance_dict[val_can_num]['Recall'] > val_best_performance:
            val_best_performance = val_performance_dict[val_can_num]['Recall']
            print('Epoch:{}'.format(epoch))
            print(test_performance_dict)


from keras.applications import ResNet50
from keras.layers import (
    Activation,
    BatchNormalization,
    Conv2D,
    Conv2DTranspose,
    UpSampling2D,
    add,
)
from tensorflow import keras

def Encoder():
    """
    Encoder returns a singular model that is setup to take in an image
    and then a decoder undoes it.
    """
    input_shape = (256, 256, 3)
    inputs = keras.Input(shape=input_shape)

    outputs = ResNet50(
        include_top=False,
        weights="imagenet",
        input_shape=input_shape
    )(inputs)

    model = keras.Model(inputs=inputs, outputs=outputs, name="encoder")
    return model

def Decoder():
    """
    This takes the output of the Encoder and attempts to recreate the
    input.
    """
    # The output shape of the ResNet50 top layers is(batch, 8, 8, 2048)
    input_shape = (8, 8, 2048)
    inputs = keras.Input(shape=input_shape)

    net = Conv2D(1024, 3, padding="same")(inputs)
    net = BatchNormalization()(net)
    net = Activation("leaky_relu")(net)

    skip_memory = net

    # Let's build up and outwards
    for filters in [512, 256, 128, 64, 32]:
        net = Conv2DTranspose(filters, 3, padding="same",
                              activation="leaky_relu")(net)
        net = BatchNormalization()(net)
        net = Conv2DTranspose(filters, 3, padding="same",
                              activation="leaky_relu")(net)
        net = BatchNormalization()(net)
        net = UpSampling2D(2)(net)

        # Handle skip layer
        skip_memory = UpSampling2D(2)(skip_memory)
        skip_memory = Conv2D(filters, 1, padding="same")(skip_memory)
        net = add([net, skip_memory])
        skip_memory = net

    # 3 filters for 3 channels of our output
    outputs = Conv2DTranspose(3, 3, padding="same", activation="tanh")(net)

    model = keras.Model(inputs=inputs, outputs=outputs, name="decoder")

    return model

def EncoderDecoder(encoder, decoder):
    encoder_input = keras.Input((256, 256, 3), name="encoder input")
    encoder_output = encoder(encoder_input)
    decoder_output = decoder(encoder_output)
    return keras.Model(encoder_input, decoder_output, name="encoder/decoder")


import numpy as np
from dataclasses import dataclass

@dataclass
class Wave:
    frequency: int
    num_samples: int
    sampling_rate: int
    amplitude: float

    def create_sine(self) -> np.array:
        sine_wave = np.zeros((self.num_samples,), dtype=float)
        for x in range(self.num_samples):
            sine_wave[x] = np.sin(2 * np.pi * self.frequency * x/self.sampling_rate)
        return sine_wave

    def create_fm_sine(self, freq_fm_mod) -> np.array:
        fm_sine_wave = np.zeros((self.num_samples,), dtype=float)
        modulator_index = 0
        if freq_fm_mod != 0.0:
            modulator_index = self.frequency / freq_fm_mod
        shift = lambda t, fmod, fs : np.cos(2.0 * np.pi * t * fmod / fs)
        for x in range(self.num_samples):
            fm_sine_wave[x] = np.sin(2 * np.pi * self.frequency * x/self.sampling_rate + modulator_index* shift(x,freq_fm_mod, self.sampling_rate))
        return fm_sine_wave

def limit_signal(signal: np.array, max: float = 1.0, min: float = -1.0) -> np.array:
    limit_signal = np.zeros((signal.size,), dtype=float)
    for idx in range(0,signal.size):
        sample = signal[idx]
        if sample > max:
            sample = max
        if sample < min:
            sample = min
        limit_signal[idx] = sample
    return limit_signal

def generate_white_noise(nsamples: int) -> np.array:
    mean = 0.0
    scale = 1.0
    noise_signal = np.zeros((nsamples,), dtype=float)
    for idx in range(0,nsamples):
        noise_signal[idx] = np.random.normal(mean,scale)
    return noise_signal

def mix_signals(signals: [np.array]) -> np.array:
    num_signals = len(signals)
    num_samples = signals[0].size
    mixed_signal = np.zeros((num_samples,), dtype=float)

    for sample_idx in range(0,num_samples):
        sum_sample: float = 0.0
        mul_sample: float = 1.0
        for sign_idx in range(0,num_signals):
            sum_sample += signals[sign_idx][sample_idx]
            mul_sample *= signals[sign_idx][sample_idx]
        mixed_signal[sample_idx] = sum_sample - mul_sample * num_signals
    return mixed_signal


import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import traceback

import functions as fn
import algorithms as alg
import numpy as np
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
from mpl_toolkits.mplot3d import Axes3D  # This import HAS to stay here !!!

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.initialized = False
        self.master = master
        self.initVars()
        self.createWorkspace()

    def initVars(self):
        # Default padding for GUI elements
        self.defPad = 2

        self.maxIterations = tk.IntVar()
        self.maxIterations.set(100)

        # Selection boxes
        self.functions = ['Sphere', 'Schwefel', 'Rosenbrock', 'Rastrigin', 'Griewank', 'Levy', 'Michalewicz',
                          'Zakharov', 'Ackley']
        self.selectedFunction = tk.StringVar()
        self.selectedFunction.set(self.functions[0])

        self.defaultNotebookFrameWidth = 200
        self.defaultNotebookFrameHeight = 200

        self.renderDelay = tk.DoubleVar()
        self.renderDelay.set(0.05)

    def createWorkspace(self):
        # Frames
        mainFrame = ttk.Frame(self.master)
        mainFrame.grid(row=0, column=0, padx=self.defPad, pady=self.defPad)

        mainConfigFrame = ttk.LabelFrame(mainFrame, text="Config")
        mainConfigFrame.grid(row=0, column=0, padx=self.defPad, pady=self.defPad, sticky=tk.NW + tk.NE)

        commonConfigFrame = ttk.LabelFrame(mainConfigFrame, text="Common")
        commonConfigFrame.grid(row=0, column=0, padx=self.defPad, pady=self.defPad, sticky=tk.NW + tk.NE)

        algoConfigFrame = ttk.LabelFrame(mainConfigFrame, text="Algorithms")
        algoConfigFrame.grid(row=1, column=0, padx=self.defPad, pady=self.defPad, sticky=tk.NW + tk.NE)

        controlsConfigFrame = ttk.LabelFrame(mainConfigFrame, text="Control")
        controlsConfigFrame.grid(row=2, column=0, padx=self.defPad, pady=self.defPad, sticky=tk.NW + tk.NE)

        #
        # Common config
        #

        functionLabel = ttk.Label(commonConfigFrame, text="Function")
        functionLabel.grid(row=0, column=0, padx=self.defPad, pady=self.defPad, sticky=tk.E)

        functionOption = ttk.OptionMenu(commonConfigFrame, self.selectedFunction, None, *self.functions)
        functionOption.grid(row=0, column=1, padx=self.defPad, pady=self.defPad, sticky=tk.W)

        functionOption.bind("<Configure>", self.onSelectFunction)

        iterationLabel = ttk.Label(commonConfigFrame, text="Iterations")
        iterationLabel.grid(row=1, column=0, padx=self.defPad, pady=self.defPad, sticky=tk.E)

        iterationEntry = ttk.Entry(commonConfigFrame, textvariable=self.maxIterations)
        iterationEntry.grid(row=1, column=1, padx=self.defPad, pady=self.defPad, sticky=tk.W)

        renderDelayLabel = ttk.Label(commonConfigFrame, text="Render delay")
        renderDelayLabel.grid(row=2, column=0, padx=self.defPad, pady=self.defPad, sticky=tk.E)

        renderDelaySlider = ttk.Scale(commonConfigFrame, from_=0.0, to=5.0, variable=self.renderDelay)
        renderDelaySlider.grid(row=2, column=1, padx=self.defPad, pady=self.defPad, sticky=tk.W + tk.E)

        renderDelayEntry = ttk.Entry(commonConfigFrame, textvariable=self.renderDelay)
        renderDelayEntry.grid(row=3, column=1, padx=self.defPad, pady=self.defPad, sticky=tk.W + tk.E)

        # style = ttk.Style(root)
        # style.configure('lefttab.TNotebook', tabposition='wn')

        # self.tabsFrame = ttk.Notebook(algoConfigFrame, style='lefttab.TNotebook')
        self.tabsFrame = ttk.Notebook(algoConfigFrame)

        blindFrame = self.createBlindFrame(self.tabsFrame)
        hillClimbFrame = self.createHillClimbFrame(self.tabsFrame)
        annealingFrame = self.createAnnealingFrame(self.tabsFrame)
        tspFrame = self.createTSPFrame(self.tabsFrame)
        dgaFrame = self.createDGAFrame(self.tabsFrame)
        somaFrame = self.createSomaFrame(self.tabsFrame)
        antFrame = self.createAntFrame(self.tabsFrame)
        fireflyFrame = self.createFireflyFrame(self.tabsFrame)
        tlbaFrame = self.createTLBAFrame(self.tabsFrame)

        self.tabsFrame.add(blindFrame, text='Blind')
        self.tabsFrame.add(hillClimbFrame, text='Hill Climb')
        self.tabsFrame.add(annealingFrame, text='Annealing')
        self.tabsFrame.add(tspFrame, text='TSP')
        self.tabsFrame.add(dgaFrame, text='Differential GA')
        self.tabsFrame.add(somaFrame, text='SOMA')
        self.tabsFrame.add(antFrame, text='Ants')
        self.tabsFrame.add(fireflyFrame, text='Firefly')
        self.tabsFrame.add(tlbaFrame, text='TLBA')

        self.tabsFrame.grid(row=0, column=0, padx=self.defPad, pady=self.defPad, sticky=tk.NW + tk.NE)

        runButton = ttk.Button(controlsConfigFrame, text="Run", command=self.run)
        runButton.grid(row=0, column=1, padx=self.defPad, pady=self.defPad, sticky=tk.E)

        graph3dFrame = ttk.LabelFrame(mainFrame, text="3D plot")
        graph3dFrame.grid(row=0, column=1, padx=self.defPad, pady=self.defPad, sticky=tk.NW + tk.NE)

        self.fig3D = Figure(figsize=(5, 4), dpi=100)
        self.canvas3D = FigureCanvasTkAgg(self.fig3D, master=graph3dFrame)
        # self.canvas3D.draw()

        self.graph3Dax = self.fig3D.gca(projection="3d")

        t = np.arange(0, 3, .01)
        self.graph3Dax.plot(t, 2 * np.sin(2 * np.pi * t))

        self.canvas3D.get_tk_widget().grid()

        graph2dFrame = ttk.LabelFrame(mainFrame, text="Fitness history")
        graph2dFrame.grid(row=0, column=2, padx=self.defPad, pady=self.defPad, sticky=tk.NW + tk.NE)

        # self.onSelectFunction(None)
        self.initialized = True

    def onSelectFunction(self, evt):
        if self.initialized is False:
            return
        try:
            func = fn.functionsMap[self.selectedFunction.get()]
            print(f"{func} selected")
            func.plot(axes=self.graph3Dax)
            self.canvas3D.draw()
            self.canvas3D.flush_events()
        except:
            print("Unable to update graph")
            traceback.print_exc()

    def quitApp(self):
        root.destroy()

    def createBlindFrame(self, master):
        frame = ttk.Frame(master, width=self.defaultNotebookFrameWidth, height=self.defaultNotebookFrameHeight)

        self.blindOptions = {
            "pointCloud": tk.IntVar()
        }
        self.blindOptions["pointCloud"].set(60)

        self.getFrameWithEntry(frame, text="Point cloud size", variable=self.blindOptions["pointCloud"]).grid(row=0,
                                                                                column=0,
                                                                                columnspan=2,
                                                                                sticky=tk.E)

        return frame

    def createHillClimbFrame(self, master):
        frame = ttk.Frame(master, width=self.defaultNotebookFrameWidth, height=self.defaultNotebookFrameHeight)

        self.hillClimbOptions = {
            "pointCloud": tk.IntVar(),
            "sigma": tk.DoubleVar()
        }
        self.hillClimbOptions["pointCloud"].set(60)
        self.hillClimbOptions["sigma"].set(0.05)

        self.getFrameWithEntry(
            master=frame,
            text="Point cloud size",
            variable=self.hillClimbOptions["pointCloud"]
        ).grid(row=0, column=0, columnspan=2, sticky=tk.E)
        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Sigma",
            variable=self.hillClimbOptions["sigma"],
            from_=0,
            to=1
        ).grid(row=1, column=0, columnspan=2, sticky=tk.E)

        return frame

    def createAnnealingFrame(self, master):
        frame = ttk.Frame(master, width=self.defaultNotebookFrameWidth, height=self.defaultNotebookFrameHeight)

        self.annealingOptions = {
            "pointCloud": tk.IntVar(),
            "temp": tk.DoubleVar(),
            "tempMin": tk.DoubleVar(),
            "alpha": tk.DoubleVar(),
            "sigma": tk.DoubleVar(),
            "elitism": tk.IntVar(),
            "repeats": tk.IntVar(),
        }
        self.annealingOptions["pointCloud"].set(10)
        self.annealingOptions["temp"].set(5000)
        self.annealingOptions["tempMin"].set(0.1)
        self.annealingOptions["alpha"].set(0.99)
        self.annealingOptions["sigma"].set(0.1)
        self.annealingOptions["elitism"].set(True)
        self.annealingOptions["repeats"].set(5)

        self.getFrameWithEntry(
            master=frame,
            text="Point cloud size",
            variable=self.annealingOptions["pointCloud"]
        ).grid(row=0, column=0, columnspan=2, sticky=tk.E)
        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Sigma",
            variable=self.annealingOptions["sigma"],
            from_=0,
            to=1
        ).grid(row=1, column=0, columnspan=2, sticky=tk.E)
        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Temperature",
            variable=self.annealingOptions["temp"],
            from_=1,
            to=10000
        ).grid(row=2, column=0, columnspan=2, sticky=tk.E)
        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Min temperature",
            variable=self.annealingOptions["tempMin"],
            from_=0.0,
            to=1
        ).grid(row=3, column=0, columnspan=2, sticky=tk.E)
        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Alpha",
            variable=self.annealingOptions["alpha"],
            from_=0.01,
            to=0.99
        ).grid(row=4, column=0, columnspan=2, sticky=tk.E)
        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Repeats for T",
            variable=self.annealingOptions["repeats"],
            from_=0.0,
            to=100
        ).grid(row=5, column=0, columnspan=2, sticky=tk.E)
        return frame

    def createTSPFrame(self, master):
        frame = ttk.Frame(master, width=self.defaultNotebookFrameWidth, height=self.defaultNotebookFrameHeight)
        self.tspOptions = {
            'citiesCount': tk.IntVar(),
            'dimensions': tk.IntVar(),
            'populationSize': tk.IntVar(),
            'workspaceSize': [10000, 10000, 10000],
            'mutationChance': tk.DoubleVar(),
        }

        self.tspOptions['citiesCount'].set(20)
        self.tspOptions['dimensions'].set(3)
        self.tspOptions['populationSize'].set(20)
        self.tspOptions['mutationChance'].set(0.5)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Cities count",
            variable=self.tspOptions["citiesCount"],
            from_=4,
            to=100
        ).grid(row=0, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Population",
            variable=self.tspOptions["populationSize"],
            from_=1,
            to=100
        ).grid(row=1, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Mutation chance",
            variable=self.tspOptions["mutationChance"],
            from_=0,
            to=1
        ).grid(row=2, column=0, columnspan=2, sticky=tk.E)

        return frame

    def createDGAFrame(self, master):
        frame = ttk.Frame(master, width=self.defaultNotebookFrameWidth, height=self.defaultNotebookFrameHeight)
        self.dgaOptions = {
            'populationSize': tk.IntVar(),
            'dimensions': tk.IntVar(),
            'scalingFactorF': tk.DoubleVar(),
            'crossoverCR': tk.DoubleVar(),
        }

        self.dgaOptions['populationSize'].set(20)
        self.dgaOptions['dimensions'].set(3)
        self.dgaOptions['scalingFactorF'].set(0.7)
        self.dgaOptions['crossoverCR'].set(0.7)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Population size",
            variable=self.dgaOptions["populationSize"],
            from_=4,
            to=100
        ).grid(row=0, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Scaling factor F",
            variable=self.dgaOptions["scalingFactorF"],
            from_=0.1,
            to=1.1
        ).grid(row=1, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Crossover factor CR",
            variable=self.dgaOptions["crossoverCR"],
            from_=0.0,
            to=1.0
        ).grid(row=2, column=0, columnspan=2, sticky=tk.E)

        return frame

    def createSomaFrame(self, master):
        frame = ttk.Frame(master, width=self.defaultNotebookFrameWidth, height=self.defaultNotebookFrameHeight)
        self.somaOptions = {
            'populationSize': tk.IntVar(),
            'dimensions': tk.IntVar(),
            'pathLength': tk.DoubleVar(),
            'step': tk.DoubleVar(),
            'perturbation': tk.DoubleVar(),
            'minDiv': tk.DoubleVar(),
        }

        self.somaOptions['populationSize'].set(20)
        self.somaOptions['dimensions'].set(3)
        self.somaOptions['pathLength'].set(3)
        self.somaOptions['step'].set(0.11)
        self.somaOptions['perturbation'].set(0.1)
        self.somaOptions['minDiv'].set(-0.1)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Population size",
            variable=self.somaOptions["populationSize"],
            from_=10,
            to=100
        ).grid(row=0, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Path length",
            variable=self.somaOptions["pathLength"],
            from_=1.1,
            to=10
        ).grid(row=1, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Step",
            variable=self.somaOptions["step"],
            from_=0.11,
            to=self.somaOptions["pathLength"].get()
        ).grid(row=2, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Perturbation",
            variable=self.somaOptions["perturbation"],
            from_=0,
            to=1
        ).grid(row=3, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Min Div",
            variable=self.somaOptions["minDiv"],
            from_=1,
            to=100
        ).grid(row=4, column=0, columnspan=2, sticky=tk.E)

        return frame

    def createAntFrame(self, master):
        frame = ttk.Frame(master, width=self.defaultNotebookFrameWidth, height=self.defaultNotebookFrameHeight)
        self.antOptions = {
            'dimensions': tk.IntVar(),
            'citiesCount': tk.IntVar(),
            'evaporization': tk.DoubleVar(),
            'initialFeromone': tk.DoubleVar(),
            'alpha': tk.DoubleVar(),
            'beta': tk.DoubleVar(),
            'workspaceSize': [100, 100, 100],
        }

        self.antOptions['dimensions'].set(3)
        self.antOptions['citiesCount'].set(20)
        self.antOptions['evaporization'].set(0.5)
        self.antOptions['initialFeromone'].set(1)
        self.antOptions['alpha'].set(1)
        self.antOptions['beta'].set(2)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Cities count",
            variable=self.antOptions["citiesCount"],
            from_=4,
            to=100
        ).grid(row=0, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Evaporization",
            variable=self.antOptions["evaporization"],
            from_=0.001,
            to=0.99
        ).grid(row=1, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Initial pheromone",
            variable=self.antOptions["initialFeromone"],
            from_=0.1,
            to=10
        ).grid(row=2, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Pheromone importance",
            variable=self.antOptions["alpha"],
            from_=0,
            to=10
        ).grid(row=3, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Distance importance",
            variable=self.antOptions["beta"],
            from_=0,
            to=10
        ).grid(row=4, column=0, columnspan=2, sticky=tk.E)

        return frame

    def createFireflyFrame(self, master):
        frame = ttk.Frame(master, width=self.defaultNotebookFrameWidth, height=self.defaultNotebookFrameHeight)
        self.fireflyOptions = {
            'dimensions': tk.IntVar(),
            "populationSize": tk.IntVar(),
            'alpha': tk.DoubleVar(),
            'betaAtractivness': tk.DoubleVar(),
        }

        self.fireflyOptions['dimensions'].set(3)
        self.fireflyOptions['populationSize'].set(20)
        self.fireflyOptions['alpha'].set(0.6)
        self.fireflyOptions['betaAtractivness'].set(1.0)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Number of fireflies",
            variable=self.fireflyOptions["populationSize"],
            from_=4,
            to=100
        ).grid(row=0, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Random factor (alpha)",
            variable=self.fireflyOptions["alpha"],
            from_=0,
            to=1
        ).grid(row=1, column=0, columnspan=2, sticky=tk.E)

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Atractivness (beta)",
            variable=self.fireflyOptions["betaAtractivness"],
            from_=0,
            to=1
        ).grid(row=2, column=0, columnspan=2, sticky=tk.E)
        return frame

    def createTLBAFrame(self, master):
        frame = ttk.Frame(master, width=self.defaultNotebookFrameWidth, height=self.defaultNotebookFrameHeight)
        self.tlbaOptions = {
            'dimensions': tk.IntVar(value=3),
            "populationSize": tk.IntVar(value=20),
        }

        self.getFrameWithSliderAndEntry(
            master=frame,
            text="Population size",
            variable=self.tlbaOptions["populationSize"],
            from_=4,
            to=100
        ).grid(row=0, column=0, columnspan=2, sticky=tk.E)
        return frame

    def getFrameWithEntry(self, master, text, variable):
        frame = ttk.Frame(master)

        valLabel = ttk.Label(frame, text=text)
        valLabel.grid(row=0, column=0, padx=self.defPad, pady=self.defPad, sticky=tk.E)
        valEntry = ttk.Entry(frame, textvariable=variable)
        valEntry.grid(row=0, column=1, padx=self.defPad, pady=self.defPad, sticky=tk.W + tk.E)

        return frame

    def getFrameWithSliderAndEntry(self, master, text, variable, from_, to):
        frame = ttk.Frame(master)

        valLabel = ttk.Label(frame, text=text)
        valLabel.grid(row=0, column=0, padx=self.defPad, pady=self.defPad, sticky=tk.E)
        valSlider = ttk.Scale(frame, from_=from_, to=to, variable=variable)
        valSlider.grid(row=0, column=1, padx=self.defPad, pady=self.defPad, sticky=tk.W + tk.E)
        valEntry = ttk.Entry(frame, textvariable=variable)
        valEntry.grid(row=1, column=1, padx=self.defPad, pady=self.defPad, sticky=tk.W + tk.E)

        return frame

    def getAlgorithm(self):
        currentTabIdx = self.tabsFrame.index("current")
        func = fn.functionsMap[self.selectedFunction.get()]
        func.clearDict()

        algo = {
            0: alg.BlindAlgorithm(function=func,
                                  pointCloudSize=self.blindOptions["pointCloud"].get()),
            1: alg.HillClimbAlgorithm(function=func,
                                      pointCloudSize=self.hillClimbOptions["pointCloud"].get(),
                                      sigma=self.hillClimbOptions["sigma"].get()),
            2: alg.AnnealingAlgorithm(function=func,
                                      options=self.annealingOptions),
            3: alg.TravelingSalesmanGeneticAlgorithm(options=self.tspOptions),
            4: alg.DifferentialGeneticAlgorithm(function=func, options=self.dgaOptions),
            5: alg.SelfOrganizingMigrationAlgorithm(function=func, options=self.somaOptions),
            6: alg.TravelingSalesmanAntColonyAlgorithm(options=self.antOptions),
            7: alg.FireflyAlgorithm(function=func, options=self.fireflyOptions),
            8: alg.TeachingLearningBasedAlgorithm(function=func, options=self.tlbaOptions),
        }.get(currentTabIdx, None)

        print(f"Func: {func}")
        print(f"Current index: {currentTabIdx}")
        print(f"Alg: {algo}")

        return algo

    def run(self):
        algo = self.getAlgorithm()
        algo.renderDelay = self.renderDelay.get()
        algo.solve(maxIterations=self.maxIterations.get(), ax3d=self.graph3Dax, canvas=self.canvas3D)
        tk.messagebox.showinfo("Done", f"Best found value: {algo.fitness}. For more info check console output.")
        algo.plotFitnessHistory()

if __name__ == '__main__':
    root = tk.Tk()
    root.title("BIA GUI")
    app = Application(master=root)
    root.resizable(False, False)
    app.mainloop()


import os

try:
    from qgis.PyQt import uic
    from qgis.PyQt import QtWidgets
except:
    from PyQt5 import uic, QtWidgets

# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), r'ui\config_dialog.ui'))

class ConfigDialog(QtWidgets.QDialog, FORM_CLASS):
    def __init__(self, controller, parent=None):
        """Constructor."""
        super(ConfigDialog, self).__init__(parent)

        self.setupUi(self)

        self.setWindowTitle(f"Configurações - {self.windowTitle()}")

        self.controller = controller


import time
import requests
import datetime
from multiprocessing.pool import ThreadPool
from .config import n_threads, spot_region_map

def call_end_point(url):
    return requests.get(url).json()['Items']

multi_result = {}
def pull_price(region_name, sizes, start, end, now_time=str(datetime.datetime.now()), is_windows_instance=False):
    is_windows_instance = is_windows_instance

    data = []
    end_point = "https://prices.azure.com/api/retail/prices?$filter="
    params = ''
    for count, instance_size in enumerate(sizes):
        params += "skuName eq '" + str(instance_size).replace('Standard_', '').replace('_', ' ') + " Spot' or "
        if (count+1)%19 == 0:
            spot_end_point = end_point + params[:-3] + "and armRegionName eq '" + str(region_name) + "'"
            data += call_end_point(spot_end_point)
            params = ''

    if params:
        spot_end_point = end_point + params[:-3] + "and armRegionName eq '" + str(region_name) + "'"
        data += call_end_point(spot_end_point)

    params = ''
    for count, instance_size in enumerate(sizes):
        params += "skuName eq '" + str(instance_size).replace('Standard_', '').replace('_', ' ') + "' or "
        if (count+1)%19 == 0:
            regular_end_point = end_point + params[:-3] + "and armRegionName eq '" + str(region_name) + "'"
            data += call_end_point(regular_end_point)
            params = ''

    if params:
        regular_end_point = end_point + params[:-3] + "and armRegionName eq '" + str(region_name) + "'"
        data += call_end_point(regular_end_point)

    if region_name not in multi_result:
        multi_result [region_name] = {}

    for each_instance in data:
        instance_size = each_instance['armSkuName']

        if instance_size not in multi_result[region_name]:
            multi_result[region_name][instance_size] = [now_time, region_name, instance_size, float('-inf'), float('-inf'), float('-inf'), float('-inf')]

        if not is_windows_instance and 'windows' not in each_instance['productName'].lower() and \
            'spot' in each_instance['meterName'].lower() and each_instance['type'] == 'Consumption':
            multi_result[region_name][instance_size][3] = each_instance['unitPrice']

        if not is_windows_instance and 'windows' not in each_instance['productName'].lower() and \
            'spot' not in each_instance['meterName'].lower() and each_instance['type'] == 'Consumption':
            multi_result[region_name][instance_size][4] = each_instance['unitPrice']

        if not is_windows_instance and 'windows' not in each_instance['productName'].lower() and \
            each_instance['type'] == 'Reservation' and each_instance['reservationTerm'] == '1 Year':
            multi_result[region_name][instance_size][5] = each_instance['unitPrice']/(365*24)

        if not is_windows_instance and 'windows' not in each_instance['productName'].lower() and \
            each_instance['type'] == 'Reservation' and each_instance['reservationTerm'] == '3 Years':
            multi_result[region_name][instance_size][6] = each_instance['unitPrice']/(365*24*3)

def process_savings(data):
    per_saving_pay_as_you_go = float('-inf')
    if data[4] and data[3]:
        per_saving_pay_as_you_go = (data[4] - data[3])/data[4]*100

    per_saving_1y_reserved = float('-inf')
    if data[5] and data[3]:
        per_saving_1y_reserved = (data[5] - data[3])/data[5]*100

    per_saving_3y_reserved = float('-inf')
    if data[6] and data[3]:
        per_saving_3y_reserved = (data[6] - data[3])/data[6]*100

    if data[3] > 0 and data[4] > 0:
        return {
            'time': data[0],
            'region': data[1],
            'size': data[2],
            'api_price': data[3],
            'cli_price': float('-inf'),
            'pay_as_you_go_price': data[4],
            '1_year_reserved_price': data[5],
            '3_year_reserved_price': data[6],
            '%_saving_pay_as_you_go': per_saving_pay_as_you_go,
            '%_saving_1y_reserved': per_saving_1y_reserved,
            '%_saving_3y_reserved': per_saving_3y_reserved
        }

def pull_multiple(now_time, is_windows_instances=False):
    global multi_result
    multi_result = {}

    p = ThreadPool(n_threads)

    return_data = []
    count = 0
    for region in spot_region_map.keys():
        price = p.apply_async(pull_price, args=(region, spot_region_map[region], now_time, is_windows_instances, ))

    p.close()
    p.join()

    for region  in multi_result.keys():
        for size in multi_result[region].keys():
            result = process_savings(multi_result[region][size])
            if result:
                return_data.append(result)

    return return_data


import sys
import asyncio
import logging
import argparse
import socket
import signal

from . import IRC, Server

logging.basicConfig(
    level=logging.DEBUG,
    format='[%(asctime)s] %(name)s - %(levelname)s - %(message)s',
)

log = logging.getLogger("ircd.main")

CLIENT_PORT = "9999"
CLIENT_LISTEN_ADDRESS = "0.0.0.0:" + CLIENT_PORT

LINK_PORT = "6666"
LINK_LISTEN_ADDRESS = "0.0.0.0:" + LINK_PORT

def parse_address(s):
    parts = s.split(":")
    host, port = parts
    return host, int(port)

async def resolve_address(host, port):
    addresses = await asyncio.get_event_loop().getaddrinfo(host, port)
    return addresses[0][-1][0] if addresses else host

async def main(args):
    addr, port = args.listen
    irc = IRC(args.host)
    server = Server(irc)

    async def _shutdown():
        log.info("shutdown")
        await server.shutdown()

    for sig in (signal.SIGHUP, signal.SIGINT, signal.SIGTERM):
        asyncio.get_running_loop().add_signal_handler(sig, lambda: asyncio.create_task(_shutdown()))

    await server.run(
        client_listen_addr=args.listen[0],
        client_listen_port=args.listen[1],
        link_listen_addr=args.link[0],
        link_listen_port=args.link[1],
        peer_addr=await resolve_address(args.peer[0], args.peer[1]) if args.peer else None,
        peer_port=args.peer[1] if args.peer else None,
        ws_addr=args.ws[0] if args.ws else None,
        ws_port=args.ws[1] if args.ws else None,
    )

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="irc server")
    parser.add_argument("--host", help="host name", default=socket.gethostname())
    parser.add_argument("--listen", help="listen address", type=parse_address, default=CLIENT_LISTEN_ADDRESS)
    parser.add_argument("--link", help="link address", type=parse_address, default=LINK_LISTEN_ADDRESS)
    parser.add_argument("--peer", help="peer address", type=parse_address)
    parser.add_argument("--ws", help="websocket listen address", type=parse_address)
    parser.add_argument("--verbose", help="verbose mode", action="store_true")
    args = parser.parse_args(sys.argv[1:])

    try:
        asyncio.run(main(args), debug=args.verbose)
    except asyncio.CancelledError:
        pass


import argparse
import shlex

import greenlet as gl
import numpy as np
import tensorflow as tf

from pong.mechanics.pong import Pong, S
from pong.mechanics import policies
from pong.mechanics import constants as c
from pong.utils import common
from pong.utils.tf_machinery import NeuralNetwork

class NNPolicy(policies.PolicyRight):

    @classmethod
    def create_from_commandline(cls, side, argv):
        parser = argparse.ArgumentParser()
        parser.add_argument("save_path")
        parser.add_argument("--name", "-n", default="NeuralNetwork")
        args = parser.parse_args(shlex.split(argv))
        return cls(side, **vars(args))

    def __init__(self, side, layers=[50, 50, 50], save_path=None,
        name="Neural"):
        super().__init__(side, name)
        self.save_path = save_path

        self.g = tf.Graph()
        with self.g.as_default():
            self.nn = NeuralNetwork(Pong.STATE_DIM, Pong.NUM_ACTIONS, layers)
            if save_path is not None:
                self.nn.load(save_path)

    def get_action_right(self, state, *args):
        return gl.getcurrent().parent.switch((self.side, state))

    def evaluate_all(self, states):
        return self.nn.predict_argmax(states)

class TargetNNPolicy(policies.PolicyRight):

    @classmethod
    def create_from_commandline(cls, side, argv):
        parser = argparse.ArgumentParser()
        parser.add_argument("save_path")
        parser.add_argument("--name", "-n", default="NeuralNetwork")
        args = parser.parse_args(shlex.split(argv))
        return cls(side, **vars(args))

    def __init__(self, side, layers=[50, 50, 50], save_path=None, name="Neural"):
        super().__init__(side, name)
        self.save_path = save_path

        self.g = tf.Graph()
        with self.g.as_default():
            self.nn = NeuralNetwork(Pong.STATE_DIM, 1, layers)
            if save_path is not None:
                self.nn.load(save_path)

    def get_action_right(self, state, *args):
        return gl.getcurrent().parent.switch((self.side, state))

    def evaluate_all(self, states):
        y = states[:, S.R_Y]
        low = y - 0.5 * c.HPL
        high = y + 0.5 * c.HPL

        T = self.nn.predict_raw(states)[:, 0]
        A = np.zeros(states.shape[0], np.int32)
        A[T < low] = c.A_DOWN
        A[T > high] = c.A_UP
        return A

class FastEvaluate:
    def __init__(self, l_pol, r_pol, disc=False):
        self.l_pol = l_pol
        self.r_pol = r_pol
        self.disc = disc

    def run_episode(self, *args):
        if self.disc:
            sim = Pong(random_positions=True, f=self.r_pol.discretization)
        else:
            sim = Pong(random_positions=True)

        while not sim.done:
            state = sim.get_state()
            l_a = self.l_pol.get_action(state)
            r_a = self.r_pol.get_action(state)
            sim.step(l_a, r_a)

        return sim.win_string()

    def run_episodes(self, n):
        eval_states = np.zeros((n, Pong.STATE_DIM), np.float32)

        threads = []
        for i in range(n):
            t = gl.greenlet(self.run_episode)
            threads.append(t)

        A = [None] * n
        alive = np.ones(n, np.bool)

        while alive.any():
            flags = {"l": np.zeros(n, np.bool), "r": np.zeros(n, np.bool)}

            for i in range(n):
                if alive[i]:
                    data = threads[i].switch(A[i])
                    if not threads[i].dead:
                        side, state = data
                        eval_states[i] = state
                        flags[side][i] = True
                    else:
                        alive[i] = False
                        self.score[data] += 1

            A = np.zeros(n, np.int32)
            if flags["l"].any():
                A[flags["l"]] = self.l_pol.evaluate_all(eval_states[flags["l"]])
            if flags["r"].any():
                A[flags["r"]] = self.r_pol.evaluate_all(eval_states[flags["r"]])

    def estimate(self, n):
        self.score = {"l": 0, "r": 0, "draw": 0}
        self.run_episodes(n)
        return self.score

policies.POLICIES["nn"] = NNPolicy
policies.POLICIES["targetnn"] = TargetNNPolicy


import asyncio
import hashlib
import hmac
import json
from unittest import TestCase
from unittest.mock import MagicMock

from typing_extensions import Awaitable

from hummingbot.connector.exchange.woo_x.woo_x_auth import WooXAuth
from hummingbot.core.web_assistant.connections.data_types import RESTMethod, RESTRequest

class WooXAuthTests(TestCase):
    def setUp(self) -> None:
        self._api_key = "testApiKey"
        self._secret = "testSecret"

    def async_run_with_timeout(self, coroutine: Awaitable, timeout: float = 1):
        ret = asyncio.get_event_loop().run_until_complete(asyncio.wait_for(coroutine, timeout))

        return ret

    def test_rest_authenticate(self):
        mock_time_provider = MagicMock()

        mock_time_provider.time.return_value = 1686452155.0

        data = {
            "symbol": "SPOT_BTC_USDT",
            "order_type": "LIMIT",
            "side": "BUY",
            "order_price": 20000,
            "order_quantity": 1,
        }

        timestamp = str(int(mock_time_provider.time.return_value * 1e3))

        auth = WooXAuth(api_key=self._api_key, secret_key=self._secret, time_provider=mock_time_provider)

        request = RESTRequest(method=RESTMethod.POST, data=json.dumps(data), is_auth_required=True)

        configured_request = self.async_run_with_timeout(auth.rest_authenticate(request))

        signable = '&'.join([f"{key}={value}" for key, value in sorted(data.items())]) + f"|{timestamp}"

        signature = (
            hmac.new(
                bytes(self._secret, "utf-8"),
                bytes(signable, "utf-8"),
                hashlib.sha256
            ).hexdigest().upper()
        )

        headers = {
            'x-api-key': self._api_key,
            'x-api-signature': signature,
            'x-api-timestamp': timestamp,
            'Content-Type': 'application/x-www-form-urlencoded',
            'Cache-Control': 'no-cache',
        }

        self.assertEqual(timestamp, configured_request.headers['x-api-timestamp'])

        self.assertEqual(signature, configured_request.headers['x-api-signature'])

        self.assertEqual(headers, configured_request.headers)


from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth.decorators import permission_required
from .forms import CreatePollForm
from .models import Poll
from django.contrib.auth.admin import User

def index(request):
    polls = Poll.objects.all()
    context = {
        'polls' : polls
    }
    return render(request, 'polls/index.html', context)

@permission_required('polls.poll_can_create', '/login')
def create(request):
    if request.method == 'POST':
        form = CreatePollForm(request.POST)
        if form.is_valid():
            poll = form.save(commit=False);
            current_user = request.user

            poll.author = current_user
            poll.save()
            return redirect('polls:index')
    else:
        form = CreatePollForm()
    context = {
        'form' : form
    }
    return render(request, 'polls/create.html', context)

def vote(request, id):
    poll = Poll.objects.get(pk=id)

    if request.method == 'POST':
        selected_option = request.POST['poll']
        current_user = request.user

        poll.voter.add(current_user)
        poll.save()
        if selected_option == 'option1':
            poll.option_one_count += 1
        elif selected_option == 'option2':
            poll.option_two_count += 1
        elif selected_option == 'option3':
            poll.option_three_count += 1
        else:
            return HttpResponse(400, 'Invalid form')

        poll.save()

        return redirect('polls:index')

    context = {
        'poll' : poll
    }
    return render(request, 'polls/vote.html', context)

def results(request, id):
    poll = Poll.objects.get(pk=id)
    context = {
        'poll' : poll
    }
    return render(request, 'polls/results.html', context)


# System
import argparse
import io
import pickle

# Utils
from tqdm import tqdm, trange
import pandas as pd
import numpy as np
import ijson
import random
from dgl.data.utils import load_graphs

# ML Libraries
import torch
import pytorch_lightning as pl
from sklearn.preprocessing import MultiLabelBinarizer
# from sklearn.model_selection import train_test_split
from transformers import BertTokenizer
from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping

# Modules
from bert_data_module import KenmeshDataModule
from bert_classifier_lightning import KenmeshClassifier

# Global Variables
BERT_MODEL_NAME = 'microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext'
BATCH_SIZE = 16
MAX_LEN = 512
# LR = 2e-05

def split_train_test_val(texts, labels, mesh_masks, test_size = 0.1):
    """
    * This Custom function can take as much arguments as required which
    * train_test_split from sklearn doesn't support.
    * x_train,x_test,y_train,y_test = train_test_split(texts, yt, test_size=0.1, random_state=42,shuffle=True)
    """
    text_train, label_train, mesh_mask_train, text_test, label_test, mesh_mask_test = [], [], [], [], [], []
    total_len = len(texts)
    test_len = int(total_len * test_size)
    train_len = total_len - test_len

    nums = list(range(0, total_len))
    random.shuffle(nums)
    print(test_len, train_len)

    for i, num in enumerate(nums):
        if i < train_len:
            text_train.append(texts[num])
            label_train.append(labels[num])
            mesh_mask_train.append(mesh_masks[num])
        else:
            text_test.append(texts[num])
            label_test.append(labels[num])
            mesh_mask_test.append(mesh_masks[num])
        # if i == 0:
        #     print("First doc: ", type(label_train), type(mesh_mask_train))
        #     print(label_train[0], type(label_train[0]), np.count_nonzero(label_train[0]))

    print(f"Total number of documents: {total_len}, train set: {len(text_train)}, test set: {len(text_test)}")

    return text_train, label_train, mesh_mask_train, text_test, label_test, mesh_mask_test

def load_dataset(graph_file, device):
    print("Loading data...")

    text_train = pickle.load(open("text_train_full.pkl", 'rb'))
    label_train = pickle.load(open("label_train_full.pkl", 'rb'))
    mesh_mask_train = pickle.load(open("mesh_mask_train_full.pkl", 'rb'))

    text_test = pickle.load(open("text_test_full.pkl", 'rb'))
    label_test = pickle.load(open("label_test_full.pkl", 'rb'))
    mesh_mask_test = pickle.load(open("mesh_mask_test_full.pkl", 'rb'))

    text_val = pickle.load(open("text_val_full.pkl", 'rb'))
    label_val = pickle.load(open("label_val_full.pkl", 'rb'))
    mesh_mask_val = pickle.load(open("mesh_mask_val_full.pkl", 'rb'))

    print("Data Load Done...")

    n_classes = len(mesh_mask_train[0][0])

    print("n_classes: ", n_classes)

    print('Loading graph...')
    G = load_graphs(graph_file)[0][0]

    print("Batch size: ", BATCH_SIZE)
    steps_per_epoch = len(text_train)//BATCH_SIZE

    # Instantiate and set up the data_module
    Bert_tokenizer = BertTokenizer.from_pretrained(BERT_MODEL_NAME)

    kenmesh_data_Module = KenmeshDataModule(text_train, label_train, mesh_mask_train,\
         text_val, label_val, mesh_mask_val, text_test, label_test, mesh_mask_test, Bert_tokenizer,\
          device, G, G.ndata['feat'], BATCH_SIZE, MAX_LEN)
    kenmesh_data_Module.setup()

    print('prepare dataset and labels graph done!')
    # return len(meshIDs), mlb, train_dataset, valid_dataset, vectors, G
    # return text_train, label_train, mesh_mask_train, text_val, label_val, mesh_mask_val, text_test, label_test, mesh_mask_test
    return kenmesh_data_Module, steps_per_epoch, n_classes

def prepare_dataset(dataset_path, MeSH_id_pair_file, graph_file, device):
    """ Load Dataset and Preprocessing """

    print('Start loading training data')

    mesh_mask = []
    texts = []
    label_id = []
    pubmed_ids = []
    titles = []

    f = open(dataset_path, encoding="utf8")
    objects = ijson.items(f, 'articles.item')

    for i, obj in enumerate(tqdm(objects)):
        title = obj["title"]
        abstractText = obj["abstractText"]

        if len(title) < 1 or len(abstractText) < 1:
            continue

        # titles = title.split(" ")
        # abstractTexts = abstractText.split(" ")
        text = title + " " +abstractText
        pubmed_ids.append(obj["pmid"])
        mesh_mask.append(obj["meshMask"])
        texts.append(text)
        label_id.append(list(obj["meshID"].keys()))
        titles.append(title)
        # if i == 0:
        #     # print("Mesh Mask: ", len(mesh_mask[0]), len(mesh_mask[0][0]), mesh_mask[0].count(1), mesh_mask[0][0].count(1))
        #     print("Label: ", obj["meshID"])
        #     print("Label ID: ", len(label_id[0]), label_id[0])

    print('Finish loading training data')
    f.close()

    print('number of training data %d' % len(texts))

    print('load and prepare Mesh')

    mapping_id = {}
    with open(MeSH_id_pair_file, 'r') as f:
        for line in f:
            (key, value) = line.split('=')
            mapping_id[key] = value.strip()
    meshIDs = list(mapping_id.values())

    # print("meshIDs: ", meshIDs)

    n_classes = len(meshIDs)

    print('Total number of labels %d' % n_classes)

    # Encode the tags(labels) in a binary format in order to be used for training

    mlb = MultiLabelBinarizer(classes=meshIDs)
    mlb.fit(meshIDs)
    yt = mlb.fit_transform(label_id)

    # print()
    # print("title: ", titles[0])
    # print("pmid: ", pubmed_ids[0])
    # print("1: ", np.count_nonzero(yt[0])) # yt[0] -> [28415] -> count_of_nonzero == number of lebels for the doc
    # print("2: ", mlb.inverse_transform(yt[0].reshape(1,-1)))
    # print("3: ", mlb.classes_)

    # print("4: ", label_id[0])
    # print("5: ", mesh_mask[0][0].count(1)) # mesh_mask -> [1, 28415]

    # Prepare label features
    print('Loading graph...')
    G = load_graphs(graph_file)[0][0]
    # print('graph', G.ndata['feat'].shape) # [28415, 768]

    # Preparing training and test datasets
    print('prepare training and test sets')

    # First Split for Train and Test
    text_train, label_train, mesh_mask_train, text_test, label_test, mesh_mask_test = split_train_test_val(texts, yt, mesh_mask, test_size = 0.2)
    text_train, label_train, mesh_mask_train, text_val, label_val, mesh_mask_val = split_train_test_val(text_train, label_train, mesh_mask_train, test_size = 0.02)

    # print("text_test, label_test, mesh_mask_test: ", text_test, label_test, mesh_mask_test)
    # Saving test dataset to pickle for using in evaluation
    # pickle.dump(text_train, open("text_train_full.pkl", 'wb'))
    # pickle.dump(label_train, open("label_train_full.pkl", 'wb'))
    # pickle.dump(mesh_mask_train, open("mesh_mask_train_full.pkl", 'wb'))

    pickle.dump(text_test, open("text_test_full.pkl", 'wb'))
    pickle.dump(label_test, open("label_test_full.pkl", 'wb'))
    pickle.dump(mesh_mask_test, open("mesh_mask_test_full.pkl", 'wb'))

    # pickle.dump(text_val, open("text_val_full.pkl", 'wb'))
    # pickle.dump(label_val, open("label_val_full.pkl", 'wb'))
    # pickle.dump(mesh_mask_val, open("mesh_mask_val_full.pkl", 'wb'))

    print("-"*10, "pickle data dumpled", "-"*10)

    print("Batch size: ", BATCH_SIZE)
    steps_per_epoch = len(text_train)//BATCH_SIZE

    # Instantiate and set up the data_module
    Bert_tokenizer = BertTokenizer.from_pretrained(BERT_MODEL_NAME)

    kenmesh_data_Module = KenmeshDataModule(text_train, label_train, mesh_mask_train,\
         text_val, label_val, mesh_mask_val, text_test, label_test, mesh_mask_test, Bert_tokenizer,\
          device, G, G.ndata['feat'], BATCH_SIZE, MAX_LEN)
    kenmesh_data_Module.setup()

    print('prepare dataset and labels graph done!')
    # return len(meshIDs), mlb, train_dataset, valid_dataset, vectors, G
    # return text_train, label_train, mesh_mask_train, text_val, label_val, mesh_mask_val, text_test, label_test, mesh_mask_test
    return kenmesh_data_Module, steps_per_epoch, n_classes

def evaluation():
    pass
def main():
    #region All parser arguments

    parser = argparse.ArgumentParser()

    # data paths
    parser.add_argument('--dataset_path')
    parser.add_argument('----meSH_pair_path')
    parser.add_argument('--word2vec_path')
    parser.add_argument('--meSH_pair_path')
    parser.add_argument('--graph')
    parser.add_argument('--save-model-path')
    parser.add_argument('--model_name', default='Full', type=str)

    # environment
    parser.add_argument('--device', default='cuda', type=str)
    parser.add_argument('--nKernel', type=int, default=768) # Changed here for embed dimension
    parser.add_argument('--ksz', type=int, default=5)
    parser.add_argument('--hidden_gcn_size', type=int, default=768) # Changed here for embed dimension
    parser.add_argument('--embedding_dim', type=int, default=768) # Changed here for embed dimension
    parser.add_argument('--dropout', type=float, default=0.2)
    parser.add_argument('--atten_dropout', type=float, default=0.5)

    # hyper params
    # lr -> 0.001, 0.0001, 0.0003, 0.0005
    parser.add_argument('--num_epochs', type=int, default=10)
    parser.add_argument('--batch_sz', type=int, default=16)
    parser.add_argument('--num_workers', type=int, default=8)
    parser.add_argument('--lr', type=float, default=1e-4)
    parser.add_argument('--momentum', type=float, default=0.9)
    parser.add_argument('--weight_decay', type=float, default=0)
    parser.add_argument('--scheduler_step_sz', type=int, default=2)
    parser.add_argument('--lr_gamma', type=float, default=0.9)

    args = parser.parse_args()

    #endregion

    # GPU Device assignmentt
    torch.backends.cudnn.benchmark = True
    n_gpu = torch.cuda.device_count()  # check if it is multiple gpu
    print('{} gpu is avaliable'.format(n_gpu))
    torch.set_float32_matmul_precision('medium')
    device = torch.device(args.device if torch.cuda.is_available() else "cpu")
    print('Device:{}'.format(device))

    global BATCH_SIZE
    BATCH_SIZE = args.batch_sz
    num_epochs = args.num_epochs
    # Get dataset and label graph & Load pre-trained embeddings
    kenmesh_data_Module, steps_per_epoch, n_classes= prepare_dataset(args.dataset_path, args.meSH_pair_path, args.graph, device)
    # kenmesh_data_Module, steps_per_epoch, n_classes= load_dataset(args.graph, device)

    # Inittialising Bert Classifier Model
    model = KenmeshClassifier(num_labels=n_classes, ksz=args.ksz, steps_per_epoch=steps_per_epoch, n_epochs=num_epochs, lr=args.lr)

    model.to(device)

    #Initialize Pytorch Lightning callback for Model checkpointing

    # saves a file like: input/QTag-epoch=02-val_loss=0.32.ckpt
    checkpoint_callback = ModelCheckpoint(
        monitor='val_loss',# monitored quantity
        filename='QTag-{epoch:02d}-{val_loss:.2f}',
        save_top_k=3, #  save the top 3 models
        mode='min', # mode of the monitored quantity  for optimization
    )

    # Instantiate the Model Trainer
    trainer = pl.Trainer(max_epochs = num_epochs , devices = 1, accelerator='gpu', callbacks=[EarlyStopping(monitor="val_loss", mode="min", patience=3), checkpoint_callback])

    # trainer with more configurations:
    # trainer = pl.Trainer(max_epochs = N_EPOCHS , precision=32, auto_scale_batch_size='binsearch', devices = 1, accelerator='gpu', profiler="simple", callbacks=[EarlyStopping(monitor="val_loss", mode="min"), checkpoint_callback])

    # print("Best checkpoint path: ", checkpoint_callback.best_model_path)
    # pickle.dump(checkpoint_callback.best_model_path, open("checkpoint.pkl", "wb"))

    # For optimal learning rate:
    # lr_finder = trainer.tuner.lr_find(model,
    #                     min_lr=0.0005,
    #                     max_lr=0.005,
    #                     mode='linear')
    # model.learning_rate = lr_finder.suggestion()
    # trainer.tune(model) # for optimal batch size

    # Train the Classifier Model
    print("Training...")
    trainer.fit(model, kenmesh_data_Module)

    torch.save(model.state_dict(), args.save_model_path)

    # Evaluate the model performance on the test dataset
    print("Evaluation: ")
    print(trainer.test(model,datamodule=kenmesh_data_Module))

if __name__ == "__main__":
    main()


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

"""Contains the functions for handling the user interface

This file contains the functions for handling the command line user interface.
It mainly processses the command line arguments, loads the settings,
initializes a HugoPhotoSwipe instance, and passes the command to this instance.

Author: Gertjan van den Burg
License: GPL v3.

"""

import argparse
import logging
import os
import sys

from . import __version__
from .config import SETTINGS_FILENAME
from .config import settings
from .hugophotoswipe import HugoPhotoSwipe

def main():
    """Main HugoPhotoSwipe function to be called to run the program"""
    command, album = parse_args()

    if command == "init":
        logging.info("Dumping settings file.")
        settings.dump(".")
        print("Created settings file: %s" % SETTINGS_FILENAME)
        return

    if not os.path.exists(SETTINGS_FILENAME):
        print(
            "Can't find %s, please run `hps init` to create it"
            % SETTINGS_FILENAME
        )
        raise SystemExit(1)

    if not settings.validate():
        return

    hps = HugoPhotoSwipe()
    if command == "new":
        logging.info("Creating new album")
        hps.new(name=album)
    elif command == "update":
        logging.info("Running album update")
        hps.update(name=album)
    elif command == "clean":
        logging.info("Running clean")
        hps.clean(name=album)
    else:
        raise ValueError("Unknown command: %s" % command)
    logging.info("Dumping settings file.")
    settings.dump(".")

def parse_args():
    """Parse the command line arguments"""
    parser = argparse.ArgumentParser(
        prog="HugoPhotoSwipe", description="Integrate Hugo and PhotoSwipe"
    )
    parser.add_argument(
        "-v",
        "--verbose",
        help="Verbose mode",
        action="store_const",
        dest="loglevel",
        const=logging.INFO,
        default=logging.WARNING,
    )
    parser.add_argument(
        "-f",
        "--fast",
        action="store_true",
        help=("Fast mode (attempts fewer potential crops for thumbnails)"),
    )
    parser.add_argument(
        "-V",
        "--version",
        action="version",
        version=__version__,
    )
    parser.add_argument(
        "command",
        choices=["new", "update", "clean", "init"],
        help="action to do",
    )
    parser.add_argument(
        "album", nargs="?", help="album to apply the action to"
    )
    args = parser.parse_args()
    logging.basicConfig(
        level=args.loglevel,
        datefmt="[%Y-%m-%d %H:%M:%S]",
        format="%(asctime)s - %(message)s",
    )
    settings.verbose = args.loglevel == logging.INFO
    settings.fast = args.fast
    return args.command, args.album

if __name__ == "__main__":
    sys.exit(main())


from constants import *
import pygame
from bullet import Bullet

class Player(pygame.sprite.Sprite):
    def __init__(self, color, width, height, world):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.vel = [0,0]                   #speed of player
        self.face_right = True
        self.health = 100
        #group to hold bullets that fired by player
        self.bullet_group = pygame.sprite.Group()
        #world that hold player
        self.world = world
        #acc of the player's vel
        self.acc = 0.005
    def move_right(self):
        self.vel[0] = 0.8
        self.face_right = True
    def move_left(self):
        self.vel[0] = -0.8
        self.face_right = False
    def stop(self):
        self.vel[0] = 0
    def jump(self):
        self.rect.y += 2
        hit_list = pygame.sprite.spritecollide(self, self.world.platfrom_group, False)
        self.rect.y -= 2
        for sprite in hit_list:
            self.vel[1] = -1.5
    def fire(self):
        bullet = Bullet(RED, 20, 5, self)
        self.bullet_group.add(bullet)
    def update(self):
        ##############################################################################
        #handle collide, very important!!!!!!!!!
        ##############################################################################
        #first test the left and right
        self.rect.x += self.vel[0]*self.world.dt
        hit_list = pygame.sprite.spritecollide(self, self.world.platfrom_group, False)
        for sprite in hit_list:
            if self.vel[0] > 0:
                self.rect.right = sprite.rect.left
            if self.vel[0] < 0:
                self.rect.left = sprite.rect.right
        #then test the up and down
        self.vel[1] += self.acc * self.world.dt                        #gravity effect
        self.rect.y += self.vel[1]*self.world.dt
        hit_list = pygame.sprite.spritecollide(self, self.world.platfrom_group, False)
        for sprite in hit_list:
            if self.vel[1] > 0:
                self.rect.bottom = sprite.rect.top
            if self.vel[1] < 0:
                self.rect.top = sprite.rect.bottom
            self.vel[1] = 0
        ###############################################################################

        #update bullets
        for bullet in self.bullet_group:
            bullet.update()
            if bullet.rect.x < -1000 or bullet.rect.x > 20000:
                self.bullet_group.remove(bullet)


import io
import sys

from django.shortcuts import render

from django.views.decorators.csrf import csrf_protect
from .forms import UserCreationForm
from Main import schedulingAlgorithm

@csrf_protect
def openSimulator(request) :
    # for printing the console into a variable
    old_stdout = sys.stdout
    new_stdout = io.StringIO()
    sys.stdout = new_stdout

    if request.method == 'POST':
        form = UserCreationForm()
        final_array = request.POST.get('final_array')
        interval = request.POST.get('interval')
        noOfTasks = request.POST.get('noOfTasks')
        #form['interval'].initial = interval

        # splitting the input into desired format
        algorithmInputArray = arraySplit(final_array)
        # calculating rate monotonic scheduling algorithm
        rmsArray, edfArray, fcfsArray, sjfArray = schedulingAlgorithm.schedule(algorithmInputArray)
        # formating the input array to send it back to screen
        algorithmInputArray = returnInputArray(algorithmInputArray)

        # writing standard output to variable
        outputConsole = new_stdout.getvalue()
        sys.stdout = old_stdout  # setting the standard output back to console

        return render(request, 'index.html',{'sjfArray': sjfArray, 'fcfsArray': fcfsArray, 'edfArray': edfArray, 'rmsArray': rmsArray, 'noOfTasks': noOfTasks,'algorithmInputArray': algorithmInputArray, 'form': form, 'outputConsole': outputConsole})
    else:
        form = UserCreationForm()
        outputConsole = new_stdout.getvalue()
        sys.stdout = old_stdout  # setting the standard output back to console
        return render(request, 'index.html', {'sjfArray': [], 'fcfsArray': [], 'edfArray': [], 'rmsArray': [], 'noOfTasks': [],'algorithmInputArray': [], 'form': form, 'outputConsole': outputConsole})

def arraySplit(algorithmInputs):
    algorithmInputs = algorithmInputs[:-1]
    algorithmInputs = algorithmInputs.split("|")
    algorithmInputArray = []
    for algorithmInput in algorithmInputs:
        algorithmInput = algorithmInput[:-1]
        algorithmInputArray.append(list(map(int, algorithmInput.split(",", -1))))
    return algorithmInputArray

def returnInputArray(algorithmInputArray):
    algorithmInputArray.sort(key=lambda x: x[1])
    for inputArray in algorithmInputArray:
        inputArray[1:]
    return algorithmInputArray


from losscape.plot import *
from losscape.models import *

import math

import torchvision
import matplotlib.pyplot as plt

cifar_data = torchvision.datasets.CIFAR10('sample_data/cifar/', download=True)

plt.imshow(cifar_data[1000][0])
plt.show()

CIFAR_MEAN = [0.4913997551666284, 0.48215855929893703, 0.4465309133731618]
CIFAR_STD = [0.24703225141799082, 0.24348516474564, 0.26158783926049628]

class CifarResnet(LandscapeProblem):
  def dataset(self, idx):
    img, label = self.data[self.batch_size*idx:self.batch_size*(idx+1)]
    img = jnp.array(img) / 255.
    img = (img - jnp.array(CIFAR_MEAN)[None,None,...]) / jnp.array(CIFAR_STD)[None,None,...]
    img = img[None,...]
    return img, label

  def dataset_len(self):
    return math.floor(len(cifar_data) / 10)

  def load_dataset(self):
    self.data = cifar_data

  def eval_params(self, params, batch, label):
    logits = self.model.apply(params, batch)
    return optax.softmax_cross_entropy_with_integer_labels(logits, label[None,...]).mean()

cifar_landscape = CifarResnet(ResNet50(num_classes=10), batch_size=2)
optimizer = optax.MultiSteps(optax.adam(1e-4), 2)
parameter_path = cifar_landscape.train_path(optimizer, epochs=10)

fig = LossVisualizer().plotly_plot(cifar_landscape, parameter_path, resolution=101)
fig.show()


import cv2
import numpy as np

dim = (300,300)

def verifique(imagem, ponto, conectividade):

    if conectividade == 4:
        print (ponto)
        if imagem[ponto[0]-1, ponto[1]] == 255:
            imagem[ponto[0]-1,ponto[1]] = 0
            print('0')
            return (ponto[0]-1, ponto[1])
        elif imagem[ponto[0], ponto[1]+1] == 255:
                imagem[ponto[0],ponto[1]+1] = 0
                print('1')
                return (ponto[0], ponto[1]+1)
        elif imagem[ponto[0]+1, ponto[1]] == 255:
                imagem[ponto[0]+1,ponto[1]] = 0
                print('2')
                return (ponto[0]+1, ponto[1])
        elif imagem[ponto[0], ponto[1]-1] == 255:
                imagem[ponto[0],ponto[1]-1] = 0
                print('3')
                return (ponto[0], ponto[1]-1)
        else:
            print('none')
    else:
        return ponto

def normalizaImagem(v):
    v = (v - v.min()) / (v.max() - v.min())
    resultado = (v * 255).astype(np.uint8)
    return resultado

imagem = cv2.imread('1_5.jpg')
imagemBin = 255 - imagem[:,:,0]

novaImagem = np.zeros(np.shape(imagemBin))
kernel = np.ones((3,3), np.uint8)
novaImagem = normalizaImagem((imagemBin>100)*1)

imCopy = np.copy(novaImagem)
imPlot = np.zeros(np.shape(imagem))
imPlot[:,:,0] = imPlot[:,:,1] = imPlot[:,:,2] = imCopy

novaImagem = cv2.dilate(novaImagem, kernel, iterations=1) - novaImagem
cv2.imshow('Imagem', imPlot)
cv2.waitKey(0)

max_xy = np.where(novaImagem == 255)
print (max_xy[0][0], max_xy[1][0])

novaImagemRGB = np.zeros(np.shape(imagem))
novaImagemRGB[:,:,0] = novaImagemRGB[:,:,1] = novaImagemRGB[:,:,2] = novaImagem

cv2.circle(novaImagemRGB, (max_xy[1][0], max_xy[0][0]), int(3), (0,0,255), 2)
iniciarPonto = (max_xy[0][0], max_xy[1][0])
ponto = verifique(novaImagem, iniciarPonto, 4)

while(ponto!=iniciarPonto):
    cv2.circle(novaImagemRGB, (ponto[1], ponto[0]), int(3), (0,0,255), 4)
    cv2.imshow('Imagem', imPlot)
    cv2.waitKey(1)

    cv2.circle(imPlot, (ponto[1], ponto[0]), int(3), (0,255,255), 6)
    ponto = verifique(novaImagem, ponto, 4)

cv2.imshow('Imagem', imPlot)
cv2.waitKey(0)


import sqlite3 as lite
import json
import sys
from secrets import WHITELISTED_USERS
import subreddit_notifications

def connect():
    db = lite.connect('users.db')
    cur = db.cursor()
    cur.execute(
        "create table if not exists users (username text, subreddits text)")
    cur.execute(
        "create table if not exists filters (username text, filter text, subreddit text)")
    db.commit()
    return db

def users(con):
    cur = con.cursor()
    cur.execute("select * from users")
    rows = cur.fetchall()
    return rows

def get_most_recent_time():
    try:
        file = open("most_recent_time.txt", 'r')
        text = file.read()
        file.close()
        return float(text)
    except:
        file = open("most_recent_time.txt", 'a')
        file.close()
        return 0

def set_most_recent_time(most_recent_time):
    file = open("most_recent_time.txt", 'w')
    file.write(str(most_recent_time))
    file.close()

def get_filter(db, username, sub):
    cur = db.cursor()
    cur.execute('select filter from filters where username="{}" and subreddit="{}"'.format(
        username, sub))
    return cur.fetchone()

def subreddits(db):
    cur = db.cursor()
    cur.execute('select subreddits from users')
    subs = []
    for subreddit_group in cur.fetchall():
        subs = subs + subreddit_group[0].split(',')
    return subs

def update_user(user_data, db):
    cur = db.cursor()
    username = user_data['username']
    subreddit_filter_list = user_data['subreddits']
    if username not in WHITELISTED_USERS:
        subreddit_notifications.log(
            "The user {} is not whitelisted".format(username))
        return
    subreddit_list = []
    for subreddit in subreddit_filter_list:
        subreddit_list.append(subreddit[0])

    subreddit_list = ','.join(subreddit_list)
    cur.execute(
        'select username from users where username="{}"'.format(username))
    if cur.fetchall():
        subreddit_notifications.log("Updating user: " + username)
        cur.execute('update users set subreddits="{}" where username="{}"'.format(
            subreddit_list, username))
        cur.execute('delete from filters where username="{}"'.format(username))
    else:
        subreddit_notifications.log("Creating new user: " + username)
        cur.execute('insert into users values ("{}", "{}")'.format(
            username, subreddit_list))
    for subreddit in subreddit_filter_list:
        cur.execute('insert into filters values ("{}", "(?i)({})", "{}")'.format(
            username, subreddit[1], subreddit[0]))
    db.commit()

def remove_user(user, con):
    cur = con.cursor()
    cur.execute("drop {} from users".format(user))
    con.commit()


import time
import board
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306

import subprocess

# Define the Reset Pin
oled_reset = digitalio.DigitalInOut(board.D4)

# Change these
# to the right size for your display!
WIDTH = 128
HEIGHT = 32  # Change to 64 if needed
BORDER = 5

# Use for I2C.
i2c = board.I2C()
oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset)

# Use for SPI
# spi = board.SPI()
# oled_cs = digitalio.DigitalInOut(board.D5)
# oled_dc = digitalio.DigitalInOut(board.D6)
# oled = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, oled_dc, oled_reset, oled_cs)

# Clear display.
oled.fill(0)
oled.show()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
image = Image.new("1", (oled.width, oled.height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

padding = -2
top = padding
bottom = oled.height-padding
x = 0

font = ImageFont.load_default()

while True:

    # Draw a black filled box to clear the image.
    draw.rectangle((0,0,oled.width,oled.height), outline=0, fill=0)

    # Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
    cmd = "hostname -I | cut -d\' \' -f1"
    IP = subprocess.check_output(cmd, shell = True )
    cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
    CPU = subprocess.check_output(cmd, shell = True )
    cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'"
    MemUsage = subprocess.check_output(cmd, shell = True )
    cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
    Disk = subprocess.check_output(cmd, shell = True )

    # Write two lines of text.

    draw.text((x, top),       "IP: " + str(IP)[2:-3],  font=font, fill=255)
    draw.text((x, top+8),     str(CPU)[2:-1], font=font, fill=255)
    draw.text((x, top+16),    str(MemUsage)[2:-1],  font=font, fill=255)
    draw.text((x, top+25),    str(Disk)[2:-1],  font=font, fill=255)

    # Display image.
    oled.image(image)
    oled.show()
    time.sleep(.1)