#coding:utf-8
"""
从配置文件反射拿到learner
"""
import os
import sys
import logging
sys.path.append("training")
from learner import Trainer

logger = logging.getLogger(__name__)

class LearnerLoader(object):

    @classmethod
    def from_params(cls, model, train_iter, dev_iter, learner_conf, test_iter=None, fields=None,
                        model_conf=None):
        if fields is not None:
            if "label_tag" in learner_conf:
                label_index = fields["LABEL"][1].vocab.stoi[learner_conf["label_tag"]]
                logger.info("Load label index")
                logger.info("Label index: %s" % label_index)
                logger.info("Label vocab: %s" % fields["LABEL"][1].vocab.stoi)
                return Trainer(model, train_iter, dev_iter,
                        **learner_conf, test_iter=test_iter, label_index=label_index, model_conf=model_conf, fields=fields)
        if "sequence_model" in learner_conf and learner_conf["sequence_model"]:
            assert fields is not None, "sequence model need target vocab"
            vocab = fields["LABEL"][1].vocab.itos

            return Trainer(model, train_iter, dev_iter,
                        **learner_conf, test_iter=test_iter, fields=fields, model_conf=model_conf, label_vocab=vocab)

        return Trainer(model, train_iter, dev_iter,
                        **learner_conf, test_iter=test_iter, fields=fields, model_conf=model_conf)


from django.urls import path
from  .views import *

urlpatterns = [
         path('', home, name='home'),
         path('cart/', cart, name='cart'),
         path('checkout/', checkout, name='checkout'),
         path('contact/', contact, name='contact'),
         path('detail/', detail, name='detail'),
         path('shop/', shop, name='shop'),
         path('base/', base, name='base'),
]


import torch
import random
from torchvision import datasets
from torchvision import transforms
import numpy as np

def get_data(batch_size):

    normalize = transforms.Normalize(mean=[0.4914, 0.4824, 0.4467],
                                          std=[0.2471, 0.2435, 0.2616])

    transform_train = transforms.Compose([
                                transforms.Pad(4, padding_mode = 'reflect'),
                                transforms.RandomCrop(32),
                                transforms.RandomHorizontalFlip(),
                                transforms.ToTensor(),
                                normalize])
    transform_test = transforms.Compose([
                                transforms.ToTensor(),
                                normalize])

    train_dataset = datasets.CIFAR100(
                        root = "cifar",
                        train = True,
                        download = True,
                        transform = transform_train
                        )
    test_dataset = datasets.CIFAR100(
                        root = "cifar",
                        train = False,
                        download = False,
                        transform = transform_test
                        )

    train_data = torch.utils.data.DataLoader(train_dataset, batch_size = batch_size, shuffle = True, num_workers = 1, pin_memory = True)
    test_data = torch.utils.data.DataLoader(test_dataset, batch_size = batch_size, shuffle = False, num_workers = 1, pin_memory = True)

    return train_data, test_data


from collections import OrderedDict

quantities = OrderedDict([(1, 0.8 / 9.5), (2, 1.6 / 12), (4, 3.2 / 17)])

type = {'Fire', 'Water', 'Grass', 'Elect', 'Wind', 'Ice', 'Rock', 'Freeze'}

interval = 0.1

class Affect:
    def __init__(self, q0, speed_config, type):
        self.q = q0
        self.speed_config = speed_config
        self.type = type

    def speed(self, t):
        if type == 'Freeze':
            return 0.4 + 0.1 * (t - self.speed_config['t0'])

        else:
            return quantities[self.speed_config['quantity_level']]

    def consume(self, q):
        self.q -= q
        return self

    def __repr__(self):
        return f"Affect(type={self.type}, q={round(self.q, 1)})"

class State:
    def __init__(self, affects):
        self.affects = affects

    def get_types(self):
        return [a.type for a in self.affects]

    def __repr__(self):
        return f"State({self.affects})"

    def get_affect_with_type(self, type):
        affects = [a for a in self.affects if a.type == type]
        if len(affects) > 0:
            return affects[0]
        else:
            return None

# Affect X Affect -> Affect
def affect_addition(A1, A2):
    assert A1.type == A2.type

    if A1.type == 'Freeze':
        return Affect(q0=A1.q + A2.q, speed_config=A1.speed_config, type=A1.type)
    else:
        return Affect(q0=max(A1.q, A2.q), speed_config=A1.speed_config, type=A1.type)

# State X State -> State
def state_addition(s1, s2):
    types = {a.type for a in s1.affects + s2.affects}

    result = State([])

    for type in types:
        a1 = s1.get_affect_with_type(type)
        a2 = s2.get_affect_with_type(type)

        if a1 is not None and a2 is not None:
            result.affects.append(affect_addition(a1, a2))
        elif a1 is not None:
            result.affects.append(a1)
        else:
            result.affects.append(a2)

    return result

# Affect X Affect -> State
def binary_reaction(A, B, t):
    tA = A.type
    tB = B.type

    qA = A.q
    qB = B.q

    if (tA, tB) in {('Water', 'Elect'), ('Elect', 'Water')}:

        if qA >= 0.4 and qB >= 0.4:
            print(f'Reaction Happened: {tA} and {tB}.')
            A.q -= 0.4
            B.q -= 0.4
        return State([a for a in [A, B] if a.q > 0])

    elif (tA, tB) in {('Water', 'Ice'), ('Ice', 'Water')}:
        print(f'Reaction Happened: {tA} and {tB}.')

        if qA < qB:
            return State([B.consume(qA), Affect(qA * 2, speed_config={'t0': t}, type='Freeze')])
        elif qA > qB:
            return State([A.consume(qB), Affect(qB * 2, speed_config={'t0': t}, type='Freeze')])
        else:
            return State([Affect(qA * 2, speed_config={'t0': t}, type='Freeze')])

    else:
        print(f'Reaction Happened: {tA} and {tB}.')

        k = get_multiplier(tA, tB)
        if qA > k * qB:
            return State([A.consume(k * qB)])
        elif qA < k * qB:
            return State([B.consume(qA / k)])
        else:
            return State([])

# State X Type -> State
def clean_latter_affect(s, type):
    types = [a.type for a in s.affects]

    if 'Freeze' in types and type in {'Ice', 'Water'}:
        return s
    elif 'Water' in types and 'Elect' in types and type in {'Water', 'Elect'}:
        return s
    else:
        return State([a for a in s.affects if a.type != type])

# State X Affect -> State
def state_reaction(s, A, time):
    orders = get_orders(s, A)

    if len(orders) == 2:
        B = s.get_affect_with_type(orders[0])
        C = s.get_affect_with_type(orders[1])
        return clean_latter_affect(
            state_reaction(
                binary_reaction(B, A, time),
                C, time
            ), A.type
        )
    elif len(orders) == 1 and len(s.affects) == 2:
        B = s.get_affect_with_type(orders[0])
        remains = State([aff for aff in s.affects if aff.type != orders[0]])
        return clean_latter_affect(
            state_addition(
                remains,
                binary_reaction(B, A, time)
            ), A.type
        )
    elif len(orders) == 1 and len(s.affects) == 1:
        B = s.get_affect_with_type(orders[0])
        return clean_latter_affect(
            binary_reaction(B, A, time),
            A.type
        )
    else:
        return State([A.consume(0.2 * A.q)])

def get_multiplier(ft, lt):
    if (ft, lt) in {
        ('Water', 'Fire'), ('Fire', 'Ice'), ('Fire', 'Freeze'),
        ('Water', 'Rock'), ('Fire', 'Rock'), ('Ice', 'Rock'), ('Elect', 'Rock'),
        ('Water', 'Wind'), ('Fire', 'Wind'), ('Ice', 'Wind'), ('Elect', 'Wind'),
    }:
        return 0.5
    elif (ft, lt) in {('Fire', 'Water'), ('Ice', 'Fire'), ('Freeze', 'Fire')}:
        return 2.0
    else:
        return 1.0

def get_orders(s, a):
    state_types = [a.type for a in s.affects]
    affect_type = a.type
    dictionary = {
        ('Water', 'Elect'): {
            'Ice': ['Elect', 'Water'],
            'Fire': ['Elect', 'Water'],
            'Wind': ['Elect', 'Water'],
            'Solid': ['Elect', 'Water'],
        },
        ('Water', 'Freeze'): {
            'Ice': ['Water'],
            'Fire': ['Freeze'],
            'Elect': ['Freeze'],
            'Wind': ['Water', 'Freeze'],
            'Solid': ['Freeze', 'Water'],
        },
        ('Ice', 'Freeze'): {
            'Water': ['Ice'],
            'Fire': ['Ice', 'Freeze'],
            'Elect': ['Ice', 'Freeze'],
            'Wind': ['Ice', 'Freeze'],
            'Solid': ['Freeze', 'Ice'],
        }
    }

    if len(state_types) == 0:
        return []
    elif len(state_types) == 1:
        return [state_types[0]]
    else:
        if tuple(state_types) not in dictionary:
            state_types = state_types[::-1]

        return dictionary[tuple(state_types)][affect_type]

# Test
def get_dummy_affect(type, quantity_level):
    return Affect(q0=quantity_level, speed_config={'quantity_level': quantity_level}, type=type)

if __name__ == '__main__':
    affects = [
        get_dummy_affect('Water', quantity_level=4),
        get_dummy_affect('Fire', quantity_level=2),
        get_dummy_affect('Ice', quantity_level=2),
        get_dummy_affect('Elect', quantity_level=5)
    ]

    s = State([])
    for affect in affects:
        s = state_reaction(s, affect, time=0)
        print(s)


#!/usr/bin/python3

'''
Author: Adam Frederik Ingwersen Linnemann
Date: March, 2017

This module intends to keep the fixed parts of the webcrawlers built seperate from the moving parts.

Why?
    1) Consistency
    2) Error-avoidance
    3) Readability
    4) Replication

Big Dollar Data Cloud ftw
'''

### Import libraries
import json, time, re
from datetime import datetime
from pymongo import MongoClient
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

### Regular expression for pattern matching on strings:
regx_numeric = re.compile('([0-9].*?(?=[\s]))')
regx_numeric_simple = re.compile('[0-9]*')
regx_numeric_d = re.compile('\d+')
regx_price = re.compile('([0-9]{2}.*(?=[\s]))')
regx_left = re.compile('(^.*?(?=[\n]))')
regx_right = re.compile('(?<=[\n]).*')
regx_gb = re.compile('(\n[0-9].*[\sGB])')
regx_name = re.compile('(^.*?(?=[\n]))')
regx_gb_lookbehind = re.compile('(.*?(?=[data]))')

### Regex for splitting html by tags:
regx_br = re.compile(r'
')
regx_p = re.compile(r'.*?(?=[
]|[
])')

### Utilize the numeric regex from TWU and cast string to int:
def cast_numeric(text):
    try:
        return(float(regx_numeric_d.search(text).group()))
    except ValueError:
        return(0)

### Define a driver object to be used for dynamic javascript rendering (does pretty much what it says)
def init_driver():
    driver = webdriver.Chrome(executable_path="/usr/lib/chromium-browser/chromedriver")
    driver.wait = WebDriverWait(driver, 10)
    driver.maximize_window()
    return(driver)

# Create json-dict, print to console and insert into predefined collection (this is done for each script atm)
def dbInsert(mongoCollection, name, gb, price, talk, min_price, des, vas, brand_name, domain, vas_list):
    json = {"product_name"  : name,
            "gb_included"   : gb,
            "talk_included" : talk,
            "vas_included"  : vas,
            "sub_price"     : price,
            "min_price"     : min_price,
            "description"   : des,
            "provider"      : brand_name,
            "domain"        : domain,
            "vas_wildcard"  : vas_list,
            "timestamp"     : datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
    print(json)
    mongoCollection.insert_one(json).inserted_id

### Sanity checking the number of subscriptions:
def checkBrand(html, _subscription_count, extra):
    if((len(html) + extra) < _subscription_count):
        print("\nOBS: Does ", brand_name,  " have less than", str(_subscription_count), " mobilephone products?")
    else:
        print("\nSucces!")


import requests
from blockchain.blockchain import BlockChain

LOG = print

class BlockChain_Client:

    def __init__(self, ip: str="127.0.0.1", port: int=8000):
        self.ip_address = f"{ ip }:{ port }"

    def get_invited_transaction(self, transaction_id: str, ip_address: str) -> dict:

        try:
            address = f"http://{ address }/get_transaction"
            req = requests.get(address, {"transaction_id": transaction_id})
            if req.ok:
                transaction_data = req.json
                return transaction_data

        except Exception as e:
            LOG(e)

        return None

    def get_invited_block(self, block_id: str, ip_address: str) -> dict:

        try:
            address = f"http://{ ip_address }/get_block"
            req = requests.get(address, {"block_id": block_id})
            if req.ok:
                block_data = req.json
                return block_data

        except Exception as e:
            LOG(e)
        return None

    def send_transaction_invite(self, transaction_id: str, connected_nodes: list) -> None:
        for nodes in connected_nodes:
            address = f"http://{ node }/invite_for_transaction"
            LOG(f"Sending Transaction invite to { node }")
            try:
                req = requests.get(address, {
                    'transaction_id': transaction_id,
                    'ip_address': f"{ IP }:{ PORT }"
                })
            except Exception as e:
                LOG(e)

    def send_block_invite(self, block_id: str, connected_nodes: list) -> None:
        for nodes in connected_nodes:
            address = f"http://{ node }/invite_for_block"
            LOG(f"Sending Block invite to { node }")
            try:
                req = requests.get(address, {
                    'block_id': block_id,
                    'ip_address': f"{ self.ip_address }"
                })
            except Exception as e:
                LOG(e)


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

from django.db import models, migrations
import sorl.thumbnail.fields
import django.utils.timezone
import geoposition.fields
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Picture',
            fields=[
                ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')),
                ('title', models.CharField(max_length=200, verbose_name='Название')),
                ('picture', sorl.thumbnail.fields.ImageField(null=True, verbose_name='Загрузить картинку', upload_to='%Y/%m/%d', blank=True)),
                ('comment', models.TextField(null=True, verbose_name='Комментарий', blank=True)),
                ('created_date', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Дата создания')),
                ('published_date', models.DateTimeField(null=True, verbose_name='Дата публикации', blank=True)),
                ('author', models.ForeignKey(verbose_name='Автор', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name_plural': 'картинки',
                'verbose_name': 'картинка',
            },
        ),
        migrations.CreateModel(
            name='Post',
            fields=[
                ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')),
                ('title', models.CharField(max_length=200, verbose_name='Заголовок')),
                ('text', models.TextField(null=True, verbose_name='Текст поста', blank=True)),
                ('tag', models.CharField(choices=[(None, 'Выбрать раздел'), ('accessories', 'Аксессуары')], max_length=50, verbose_name='Раздел')),
                ('created_date', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Дата создания')),
                ('published_date', models.DateTimeField(null=True, verbose_name='Дата публикации', blank=True)),
                ('author', models.ForeignKey(verbose_name='Автор', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name_plural': 'посты',
                'verbose_name': 'пост',
            },
        ),
        migrations.CreateModel(
            name='Review',
            fields=[
                ('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')),
                ('title', models.CharField(max_length=200, verbose_name='Заголовок')),
                ('text', models.TextField(null=True, verbose_name='Текст поста', blank=True)),
                ('slug', models.SlugField(max_length=60, verbose_name='Slug', blank=True)),
                ('tag', models.CharField(choices=[(None, 'Выбрать раздел'), ('shops', 'Mагазины'), ('cafes', 'Кафе'), ('museums', 'Музеи')], max_length=50, verbose_name='Раздел')),
                ('address', models.CharField(null=True, max_length=300, verbose_name='Адрес (сохраняется автоматически)', blank=True)),
                ('position', geoposition.fields.GeopositionField(max_length=42, verbose_name='Координаты')),
                ('created_date', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Дата создания')),
                ('published_date', models.DateTimeField(null=True, verbose_name='Дата публикации', blank=True)),
                ('author', models.ForeignKey(verbose_name='Автор', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name_plural': 'обзоры',
                'verbose_name': 'обзор',
            },
        ),
    ]


from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
import os
from dotenv import load_dotenv

load_dotenv()
uri = os.getenv("MONGO_URI")
# Create a new client and connect to the server
client = MongoClient(uri, server_api=ServerApi('1'))
# Send a ping to confirm a successful connection
try:
    client.admin.command('ping')
    print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
    print(e)

db = client['rose']
users=db.get_collection('users')

def ins(id):
    r=users.find_one({"user_id":int(id)},max_time_ms=500)
    if not r:
        users.insert_one({"user_id":int(id)})

def get_count():
    return users.count_documents(filter={})


import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

class multiplewindows():
    def windows(self):
        driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
        driver.get("https://www.yatra.com/")
        driver.maximize_window()
        time.sleep(4)
        parent_handle = driver.current_window_handle
        print(parent_handle)
        driver.find_element(By.XPATH, "//a[@title='Yatra Gift Cards']//img[@class='conta iner large-banner']").click()
        time.sleep(4)
        all_handles = driver.window_handles
        print(all_handles)
        for handle in all_handles:
            if handle != parent_handle:
                driver.switch_to.window(handle)
                driver.find_element(By.XPATH, "//a[normalize-space()='Shop Now']").click()
                time.sleep(4)
                driver.close()
                time.sleep(4)
                break

        driver.switch_to.window(parent_handle)
        driver.find_element(By.XPATH, "//a[@title='Yatra Gift Cards']//img[@class='conta iner large-banner']").click()

a = multiplewindows()
a.windows()


"""
.. _loader_docstring_args:

Loader
======

Loader is in charge to resolve page item options and to create UrlPattern objects from
a given page registry.

Basically you could use it like this: ::

    staticpages_loader = StaticpagesLoader()
    staticpages_loader.build_urls()

This will return you a list of UrlPattern objects, note than without any argument
the loader will use the settings for default values from settings and especially
the setting ``STATICPAGES``.

In the following sample, you can see a more concrete way that will build
UrlPattern objects directly mounted as urls, something you can use in a Django
``urls.py``: ::

    staticpages_loader = StaticpagesLoader()

    urlpatterns = [
        ...
        # Add pages urls using the same template
        *staticpages_loader.build_urls([
            "index",
            "foo",
            "bar",
        ])
    ]

That will respectively configure pages on urls ``/``, ``/foo/`` and ``/bar/``.

"""
import os

from django.conf import settings
from django.urls import path, re_path

from .exceptions import StaticpagesResolverError
from .views import StaticPageView

class StaticpagesLoader:
    """
    A ``TemplateView`` inheriter with some more attributes to implement staticpages
    features.

    Keyword Arguments:
        template_basepath (string): Path to prefix a page template path.
            If empty, default value comes from ``STATICPAGES_DEFAULT_TEMPLATEPATH``
            setting.
        name_base (string): Name to prefix a page url name. If empty, default
            value comes from ``STATICPAGES_DEFAULT_NAME_BASE`` setting.
        url_basepath (string): Base url path to prefix a page url path. If empty,
            default value comes from ``STATICPAGES_DEFAULT_URLPATH`` setting.
        view_class (object): A Class based view to use instead of the default
            ``staticpages.views.StaticPageView`` that is an extend of Django class
            base view ``TemplateView``. Given class must respect expected
            attributes ``template_name``, ``page_options`` and ``staticpages``.
    """
    def __init__(self, template_basepath=None, name_base=None, url_basepath=None,
                 view_class=None):
        self.template_basepath = (
            template_basepath or settings.STATICPAGES_DEFAULT_TEMPLATEPATH
        )
        self.name_base = name_base or settings.STATICPAGES_DEFAULT_NAME_BASE
        self.url_basepath = url_basepath or settings.STATICPAGES_DEFAULT_URLPATH
        self.view_class = view_class or StaticPageView

    def validate_item(self, data):
        """
        Validate item data is correct.

        Arguments:
            data (dict): Item options to validate. See ``resolve_item`` for expected
                data.
        """
        if "template" not in data and "template_path" not in data:
            raise StaticpagesResolverError((
                "Either 'template' or 'template_path' must be defined in a staticpage "
                "registry item."
            ))

        if data.get("template_path") and "name" not in data:
            raise StaticpagesResolverError((
                "When a staticpage registry item defines 'template_path' the 'name' "
                "option must be defined too."
            ))

    def _is_index(self, data, payload):
        """
        Resolve if item is an index or not.

        Arguments:
            data (dict): Given item options to perform resolving. See ``resolve_item``
                for expected data.
            payload (dict): Resolved item options.

        Returns:
            bool: True if an index, else False.
        """
        return payload["name"] == settings.STATICPAGES_INDEX_NAME

    def _is_regex(self, data, payload):
        """
        Resolve if item path is a regex.

        Arguments:
            data (dict): Given item options to perform resolving. See ``resolve_item``
                for expected data.
            payload (dict): Resolved item options.

        Returns:
            bool: True if a regex, else False.
        """
        return data.get("re_path") not in ("", None)

    def _resolve_template(self, data, payload):
        """
        Resolve template path from options.

        Arguments:
            data (dict): Given item options to perform resolving. See ``resolve_item``
                for expected data.
            payload (dict): Resolved item options.

        Returns:
            string: Resolved template path.
        """
        # 'template_path' option have highest priority.
        if "template_path" in data:
            resolved_template = data["template_path"]
        # Else we use 'template' option with HTML extension suffix
        else:
            resolved_template = "{}.html".format(data["template"])

        # Finally prefix with template basepath if not empty
        if self.template_basepath:
            resolved_template = os.path.join(
                self.template_basepath,
                resolved_template
            )

        return resolved_template

    def _resolve_name(self, data, payload):
        """
        Resolve name from options.

        Arguments:
            data (dict): Given item options to perform resolving. See ``resolve_item``
                for expected data.
            payload (dict): Resolved item options.

        Returns:
            string: Resolved name.
        """
        return data.get("name") or data["template"]

    def _resolve_urlname(self, data, payload):
        """
        Resolve url name from options.

        Arguments:
            data (dict): Given item options to perform resolving. See ``resolve_item``
                for expected data.
            payload (dict): Resolved item options.

        Returns:
            string: Resolved url name.
        """
        urlname = payload["name"]

        if self.name_base:
            urlname = "{}{}".format(self.name_base, urlname)

        return urlname

    def _resolve_path(self, data, payload):
        """
        Resolve url path from options.

        Arguments:
            data (dict): Given item options to perform resolving. See ``resolve_item``
                for expected data.
            payload (dict): Resolved item options.

        Returns:
            string: Resolved url path.
        """
        if payload["is_index"]:
            path = ""
        else:
            path = (
                data.get("re_path") or
                data.get("path", "{}/".format(payload["name"]))
            )

        if self.url_basepath:
            path = os.path.join(self.url_basepath, path)

        return path

    def resolve_item(self, data):
        """
        Resolve a registry item.

        Expected data may be something like: ::

            {
                "path": "foo/",
                "re_path": r"foo/$",
                "template": "foo",
                "template_path": "foo.html",
                "name": "foo",
                "extra": "anything anykind",
            }

        See :ref:`Option specifications <usage_options_specs>` for more details.

        Arguments:
            data (dict): Item options.
            Returns:
                dict: A dictionnary of resolved options. See
                :ref:`usage_template_context` for more details.
        """
        self.validate_item(data)

        payload = {}

        payload["template"] = self._resolve_template(data, payload)
        payload["name"] = self._resolve_name(data, payload)
        payload["is_index"] = self._is_index(data, payload)
        payload["urlname"] = self._resolve_urlname(data, payload)
        payload["is_regex"] = self._is_regex(data, payload)
        payload["path"] = self._resolve_path(data, payload)

        # Optional extra context to pass to view
        payload["extra"] = data.get("extra", None)

        return payload

    def resolve_registry(self, registry=None):
        """
        From given registry, resolve each registry item to a tuple suitable to build
        urls.

        Keyword Arguments:
            registry (list): List of either string or dict. Dictionnary is the verbose
                way to define options opposed to a String which is expected to be a
                template name (without leading path and ending HTML suffix) that will
                be used to resolve options. If this argument is empty, default value
                will comes from ``STATICPAGES`` setting.

        Returns:
            list: A list of item options as dictionnary.
        """
        resolved = []

        registry = registry or settings.STATICPAGES

        # TODO: An error may be raised if there is duplicate path or url name ?

        for item in registry:
            # If a simple string, resolve every options
            if isinstance(item, str):
                resolved.append(
                    self.resolve_item({"template": item})
                )
            # Assume it's a dict with one or more options
            else:
                resolved.append(
                    self.resolve_item(item)
                )

        return resolved

    def build_url(self, page, pages):
        """
        Build an url object for given page item.

        Arguments:
            page (dict): Page options.
            pages (list): List of all pages options.

        Returns:
            django.urls.resolvers.URLPattern: URLPattern object to mount in urls.
        """
        # Use the right object to define url depending it's a regex path or not
        path_object = path
        if page["is_regex"]:
            path_object = re_path

        view = self.view_class.as_view(
            template_name=page["template"],
            page_options=page,
            staticpages=pages,
        )

        url_object = path_object(page["path"], view, name=page["urlname"])

        return url_object

    def build_urls(self, registry=None):
        """
        Build view url objects for page registry.

        Keyword Arguments:
            registry (list): List of page items.

        Returns:
            list: List of URLPattern objects.
        """
        resolved = self.resolve_registry(registry=registry)

        urls = []

        for item in resolved:
            urls.append(
                self.build_url(item, resolved)
            )

        return urls


import os
import requests
import tweepy

from cat_request import get_random_cat_url

from auth import (
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)

def twitter_api ():
    # Authenticate to Twitter
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    # Create API object
    api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)

    # Verify twitter api credentials (checking if all api keys are correct)
    try:
        api.verify_credentials()
    except tweepy.error.TweepError as err:
        print("TweepError during authentication: {}".format(err))
    except:
        print("Unexpected error during authentication")
        raise
    else:
        print("Authentication OK")

    return api

def get_image(file, url):
    # Download that image from url link
    img_data = requests.get(url).content
    with open(file, 'wb') as handler:
        handler.write(img_data)

def tweet_random_cat_image ():

    image_file = 'cat_image.jpg'

    # Get random cat image url link using get_random_car_url
    try:
        image_url = get_random_cat_url()
    except:
        print ("Unexpected error while getting url for cat image")
        raise

    get_image(image_file, image_url)

    # Create an API object
    api = twitter_api ()

    message = u"\u1F604"

    message += '\n\n(from "TheCatAPI")'

    # Tweet an image
    try:
        api.update_with_media(image_file, status=message)
        os.remove(image_file)
    except:
        print("Unexpected error while uploading an image")
        raise

    print ("Image tweeted.\nURL: {}\nMessage: {}".format(image_url, message))

def check_mentions (since_id):

    api = twitter_api()

    new_since_id = since_id

    for tweet in tweepy.Cursor(api.mentions_timeline, since_id=since_id).items():
        new_since_id = max(tweet.id, new_since_id)

        if tweet.in_reply_to_status_id is not None:
            continue

        print("Answering to {}".format(tweet.user.name))

        # if not tweet.user.following:
        #   tweet.user.follow()

        image_file = 'cat_image.jpg'

        # Get random cat image url link using get_random_car_url
        try:
            image_url = get_random_cat_url()
        except:
            print ("Unexpected error while getting url for cat image")
            raise

        get_image(image_file, image_url)

        # Tweet an image
        try:
            user = tweet.user
            message = "@{} Meow".format(user.screen_name)

            api.update_with_media(image_file,
                                  status = message,
                                  in_reply_to_status_id=tweet.id)
            os.remove(image_file)
        except:
            print("Unexpected error while uploading an image")
            raise

        print ("Replied.\nURL: {}\n".format(image_url))

    return new_since_id


# !usr/bin/env python
# -*- coding:utf-8 -*-
# # @Author: Yin Tao
# @File: record.py
# @Time: 2020/05/08
#

import pyaudio
import wave
import time
import threading

class Recorder(object):
    def __init__(self, chunk=1024, channels=1, rate=8000):
        self.chunk = chunk
        self.format = pyaudio.paInt16
        self.channels = channels
        self.rate = rate
        self.filename = "test"
        self.continue_flag = True
        self.frames = []

    def start(self):
        # 单独一个线程录音
        t = threading.Thread(target=self.recording, args=())
        t.start()

    def recording(self):
        self.continue_flag = True
        self.frames = []
        p = pyaudio.PyAudio()
        stream = p.open(format=self.format,
                        channels=self.channels,
                        rate=self.rate,
                        input=True,
                        frames_per_buffer=self.chunk)
        while self.continue_flag:
            data = stream.read(self.chunk)
            self.frames.append(data)
        stream.stop_stream()
        stream.close()
        p.terminate()

    def stop(self):
        self.continue_flag = False

    def save(self):     # 自动命令
        self.filename = time.strftime("%Y%m%d%H%M%S")
        self.save_to_file(self.filename)

    def save_to_file(self, filename: str):  # 可手动命令
        if not filename.endswith(".wav"):
            filename += ".wav"
        p = pyaudio.PyAudio()
        wav_file = wave.open(filename, 'wb')
        wav_file.setnchannels(self.channels)
        wav_file.setsampwidth(p.get_sample_size(self.format))
        wav_file.setframerate(self.rate)
        wav_file.writeframes(b''.join(self.frames))
        wav_file.close()
        print("录音已保存为{}!".format(filename))

def play_wav(_filename, chunk=1024):
    if not _filename.endswith(".wav"):
        _filename += ".wav"
    wav_file = wave.open(_filename, 'rb')
    p = pyaudio.PyAudio()
    stream = p.open(format=p.get_format_from_width(wav_file.getsampwidth()),
                    channels=wav_file.getnchannels(),
                    rate=wav_file.getframerate(),
                    output=True)
    frames = wav_file.readframes(chunk)
    while frames != b'':
        stream.write(frames)
        frames = wav_file.readframes(chunk)
    stream.stop_stream()
    stream.close()
    p.terminate()
    print("播放结束")

if __name__ == '__main__':
    record = Recorder()
    record.start()

    time1 = time.time()
    b = input("录音中......按Enter停止")

    record.stop()
    time2 = time.time()
    print("录音时长：{}秒".format(round(time2-time1, 1)))
    record.save()
    play_wav(record.filename)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Author: Li Yuanming
Email: yuanmingleee@gmail.com
Date: Feb 20, 2023
"""
import shutil
from pathlib import Path

import click

import dataci
from dataci.command import (
    dataset, stage, workflow, workspace, connect
)

@click.group()
@click.version_option(dataci.__version__)
def cli():
    pass

@cli.command()
def init():
    """DataCI initialization."""
    from dataci.config import init as init_config

    init_config()

@cli.command()
def start():
    """Start DataCI server."""
    from dataci.server.server import main

    main()

@cli.command()
def reset():
    """Reset DataCI meta database."""
    # By import this module, the database will be reset.
    from dataci.db import init  # noqa

    # Remove all workspace
    from dataci.config import CACHE_ROOT

    workflow_names = list()
    for p in CACHE_ROOT.iterdir():
        if p.is_dir():
            workflow_names.append(p.name)
            shutil.rmtree(p)

    # Remove all workflow published dag to airflow
    for workflow_name in workflow_names:
        dag_path = Path.home() / 'airflow' / 'dags' / workflow_name
        if dag_path.exists():
            shutil.rmtree(dag_path)

@cli.command()
def standalone():
    """Initialize and start all DataCI services."""
    from dataci.config import init as init_config
    from dataci.models import Workspace
    from dataci.server.server import main

    click.echo('Initializing DataCI...')
    init_config()

    click.echo('Create a default workspace...')
    workspace_name = click.prompt(
        'Workspace name', type=str, default='testspace', show_default=True, confirmation_prompt=False)
    Workspace(workspace_name).use()

    click.echo('Start DataCI server...')
    main()

cli.add_command(dataset.dataset)
cli.add_command(stage.stage)
cli.add_command(workflow.workflow)
cli.add_command(workspace.workspace)
cli.add_command(connect.connect)

if __name__ == '__main__':
    cli()


import collections
from typing import List

# Approach 1: Naive
class Solution1:
    def shortestDistance(self, words: List[str], word1: str, word2: str) -> int:
        idx_map = collections.defaultdict(list)
        for i, word in enumerate(words):
            if word not in [word1, word2]: continue
            idx_map[word].append(i)

        idxs1, idxs2 = idx_map[word1], idx_map[word2]
        result = float('inf')
        for i in idxs1:
            for j in idxs2:
                result = min(result, abs(i - j))

        return result

# Approach 2: Naive with Binary Search
class Solution2:
    def shortestDistance(self, words: List[str], word1: str, word2: str) -> int:
        idx_map = collections.defaultdict(list)
        for i, word in enumerate(words):
            if word not in [word1, word2]: continue
            idx_map[word].append(i)

        idxs_1, idxs_2 = idx_map[word1], idx_map[word2]
        result = float('inf')
        for i in idxs_1:
            di = self.find_closest(idxs_2, i)
            result = min(abs(idxs_2[di] - i), result)

        return result

    def find_closest(self, idxs, target):
        n = len(idxs)
        l, r = 0, n - 1

        while l < r - 1:
            mid = (l + r) // 2

            if idxs[mid] == target: return mid
            elif idxs[mid] > target: r = mid
            else: l = mid

        if abs(idxs[l] - target) < abs(idxs[r] - target):
            return l

        return r

# Approach 3: One Pass
class Solution:
    def shortestDistance(self, words: List[str], word1: str, word2: str) -> int:
        idx1, idx2 = -1, -1
        result = float('inf')

        for i, w in enumerate(words):
            if w == word1:
                idx1 = i
                if idx2 != -1: result = min(result, abs(idx1 - idx2))

            if w == word2:
                idx2 = i
                if idx1 != -1: result = min(result, abs(idx2 - idx1))

        return result

# 243. Shortest Word Distance
# https://leetcode.com/problems/shortest-word-distance/description/

# Approach 1: Naive
# Time: O(n * n)
# Space: O(n)

# Approach 2: Naive with Binary Search
#

# Approach 3: One Pass
# - For each a, we need to get the recent position of b.
# - For each b, we need to get the recent position of a.


import os
import xml.etree.ElementTree as ET
from lib.utils import get_tweets

'''
This code 'prepares' the data by taking the xml files with the tweets for each author
and creates the train and dev partitions by taking the tweets and putting them in two
text files with a row for each tweet. Where each row has the tweet text and the label
(0 or 1) separated by a space. Note that the tweets for each author can only be in one
partition because the partitioning is made at author level.
'''

# Path to the folder with all the data subfolders for each language
full_data_path = "../data"
# Path to each laguage folder
es_data_path = os.path.join(full_data_path, "es")
en_data_path = os.path.join(full_data_path, "en")

# Percentaje for the development set
dev_partition = 0.2
# List of all the laguage folders with data to prepare
dataset = [es_data_path, en_data_path]

for data_path in dataset:
    labels_path = os.path.join(data_path, "truth.txt")
    labels_dict = {}
    with open(labels_path) as labels:
        for line in labels:
            user_id, label = line.split(":::")
            labels_dict[user_id] = int(label)

    # Get the list of all the author xml files
    all_authors = [author_xml for author_xml in os.listdir(data_path) if author_xml.endswith(".xml")]
    # Get the total number of samples for the dev set
    n_dev_samples = int(len(all_authors)*dev_partition)
    # Make the partitioning in train and dev
    train_authors = all_authors[:-n_dev_samples]
    dev_authors = all_authors[-n_dev_samples:]
    '''
    Create train partition
    '''
    with open(os.path.join(data_path, "train_tweets.txt"), "w+") as preprocessed_f:
        for f_name in train_authors:
            xml_path = os.path.join(data_path, f_name)
            author_label = labels_dict[f_name[:-4]]
            for tweet in get_tweets(xml_path):
                preprocessed_f.write(f"{tweet} {author_label}\n")

    '''
    Create development partition
    '''
    with open(os.path.join(data_path, "dev_tweets.txt"), "w+") as preprocessed_f:
        for f_name in dev_authors:
            xml_path = os.path.join(data_path, f_name)
            author_label = labels_dict[f_name[:-4]]
            for tweet in get_tweets(xml_path):
                preprocessed_f.write(f"{tweet} {author_label}\n")


# -*- coding: utf-8 -*-
'''
 @FileName    : main.py
 @EditTime    : 2021-09-19 21:46:57
 @Author      : Buzhen Huang
 @Email       : hbz@seu.edu.cn
 @Description :
'''
from copy import deepcopy
import sys
import os

import os.path as osp

import time
import torch
import numpy as np
from cmd_parser import parse_config
from init import init
from utils.init_guess import init_guess, load_init, fix_params
from utils.non_linear_solver import non_linear_solver
from utils.utils import save_results, change
import cv2
keyps = []

def main(**args):
    global keyps

    dataset_obj, setting = init(**args)

    start = time.time()

    results = {}
    s_last = None  # the name of last sequence
    setting['seq_start'] = False  # indicate the first frame of the sequence
    for idx, data in enumerate(dataset_obj):

        serial = data['serial']
        if serial != s_last:
            setting['seq_start'] = True
            s_last = serial
        else:
            setting['seq_start'] = False
        # filter out the view without annotaion
        keypoints = data['keypoints']
        views = 0
        extrinsics = []
        intrinsics = []
        keyps = []
        img_paths = []
        imgs = []
        cameras = []
        for v in range(len(keypoints)):
            if keypoints[v] is not None:
                extrinsics.append(setting['extrinsics'][v])
                intrinsics.append(setting['intrinsics'][v])
                cameras.append(setting['cameras'][v])
                keyps.append(keypoints[v])
                img_paths.append(data['img_path'][v])
                imgs.append(data['img'][v])
                views += 1
        # viewpoint should greater than 1 if not use 3D annotation
        # if views < 2 and not setting["use_3d"] or len(keyps) < 1:
        #     s_last = None
        #     continue
        setting['views'] = views
        setting['extris'] = np.array(extrinsics)
        setting['intris'] = np.array(intrinsics)
        setting['camera'] = cameras
        data['img'] = imgs
        data['img_path'] = img_paths
        data['keypoints'] = keyps
        # print("keypoints: ", keypoints)
        print('Processing: {}'.format(data['img_path']))

        # if setting['adjustment']:
        #     change(img_paths, keyps)

        # init guess
        if setting['seq_start'] or not args.get('is_seq'):
            init_guess(setting, data, use_torso=True, **args)  # 运行这个
        else:
            load_init(setting, data, results, use_torso=True, **args)

        fix_params(
            setting, scale=setting['fixed_scale'], shape=setting['fixed_shape'])

        print("linear solve, to do...")

        results = non_linear_solver(setting, data, **args)

        # save results
        save_results(setting, data, results, **args)

    elapsed = time.time() - start
    time_msg = time.strftime('%H hours, %M minutes, %S seconds',
                             time.gmtime(elapsed))
    print('Processing the data took: {}'.format(time_msg))

if __name__ == "__main__":

    # sys.argv = ["", "--config=cfg_files/fit_smpl.yaml"]
    args = parse_config()
    main(**args)


from gensim.models import word2vec
import logging
import subprocess
import sys
import traceback

"""
第一引数に対象のテキストファイルを指定し第2引数に出力モデルファイルを指定
"""
inputfile = sys.argv[1]
outputmodel = sys.argv[2]

try:
    logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s',
                        level=logging.INFO)
    sentences = word2vec.Text8Corpus(inputfile)

    #model = word2vec.Word2Vec(sentences, size=200, min_count=20, window=15)
    #model = word2vec.Word2Vec(sentences, sg=1, size=500, min_count=2,
    #                          window=15, iter=10, workers=20)
    model = word2vec.Word2Vec(sentences, size=200, min_count=20, window=15)
    model.save(outputmodel)
    cmd = 'echo "Task End"'
    subprocess.check_output(cmd , shell=True)

except Exception as e:
       t, v, tb = sys.exc_info()
       print(traceback.format_exception(t,v,tb))
       print(traceback.format_tb(e.__traceback__))


import os
import sys
import torch
import torch.nn as nn
import transformers
from models.trajectory_gpt2 import GPT2Model
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
from log import Log

class DecisionTransformer(nn.Module):
    def __init__(self,
                 state_dim,
                 act_dim,
                 hidden_size,
                 max_ep_len,
                 seq_len=None,
                 action_tanh=True,
                 **kwargs):
        super(DecisionTransformer, self).__init__()

        self.state_dim = state_dim
        self.act_dim = act_dim
        self.seq_len = seq_len
        self.hidden_size = hidden_size

        config = transformers.GPT2Config(
            vocab_size=1,
            n_embd=hidden_size,
            **kwargs
        )

        self.transformer = GPT2Model(config)

        self.embed_timestep = nn.Embedding(max_ep_len, hidden_size)
        self.embed_return = nn.Linear(1, hidden_size)
        self.embed_state = nn.Sequential(
            nn.Conv2d(4, 16, 8, stride=4),
            nn.ReLU(),
            nn.Conv2d(16, 32, 5, stride=2),
            nn.ReLU(),
            nn.Flatten(),
            nn.Linear(1152, 256),
            nn.Linear(256, hidden_size),
        )
        self.embed_action = nn.Linear(*self.act_dim, hidden_size)
        self.embed_ln = nn.LayerNorm(hidden_size)

        # self.predict_state = nn.Linear(hidden_size, self.state_dim)
        self.predict_action = nn.Sequential(
            *([nn.Linear(hidden_size, *self.act_dim)] + ([nn.Tanh()] if action_tanh else []))
        )
        self.predict_return = nn.Linear(hidden_size, 1)

    def forward(self, states, actions, rewards, returns_to_go, timesteps, attention_mask=None):

        batch_size, seq_len = states.shape[0], states.shape[1]

        if attention_mask is None:
            attention_mask = torch.ones((batch_size, seq_len), dtype=torch.long)

        states = states.reshape(batch_size*seq_len, *self.state_dim).permute(0, 3, 1, 2)
        state_embeddings = self.embed_state(states)
        state_embeddings = state_embeddings.reshape(batch_size, seq_len, -1)
        action_embeddings = self.embed_action(actions)
        return_embeddings = self.embed_return(returns_to_go)
        time_embeddings = self.embed_timestep(timesteps)

        state_embeddings = state_embeddings + time_embeddings
        action_embeddings = action_embeddings + time_embeddings
        return_embeddings = return_embeddings + time_embeddings

        stacked_inputs = torch.stack(
            (return_embeddings, state_embeddings, action_embeddings), dim=1
        ).permute(0, 2, 1, 3).reshape(batch_size, 3*seq_len, self.hidden_size)
        stacked_inputs = self.embed_ln(stacked_inputs)

        stacked_attention_mask = torch.stack(
            (attention_mask, attention_mask, attention_mask), dim=1
        ).permute(0, 2, 1).reshape(batch_size, 3*seq_len)

        transformer_outputs = self.transformer(
            inputs_embeds=stacked_inputs,
            attention_mask=stacked_attention_mask,
        )

        x = transformer_outputs['last_hidden_state']

        x = x.reshape(batch_size, seq_len, 3, self.hidden_size).permute(0, 2, 1, 3)

        return_preds = self.predict_return(x[:, 2])
        # state_preds = self.predict_state(x[:, 2])
        action_preds = self.predict_action(x[:, 1])

        return action_preds, return_preds

    def get_action(self, states, actions, rewards, returns_to_go, timesteps, **kwargs):

        states = states.reshape(1, -1, *self.state_dim)
        actions = actions.reshape(1, -1, *self.act_dim)
        returns_to_go = returns_to_go.reshape(1, -1, 1)
        timesteps = timesteps.reshape(1, -1)

        if self.seq_len is not None:
            states = states[:, -self.seq_len:]
            actions = actions[:, -self.seq_len:]
            returns_to_go = returns_to_go[:, -self.seq_len:]
            timesteps = timesteps[:, -self.seq_len:]

            attention_mask = torch.cat([torch.zeros(self.seq_len - states.shape[1]), torch.ones(states.shape[1])])
            attention_mask = attention_mask.to(dtype=torch.long, device=states.device).reshape(1, -1)

            states = torch.cat(
                [torch.zeros((states.shape[0], self.seq_len - states.shape[1], *self.state_dim), device=states.device),
                 states], dim=1).to(dtype=torch.float32)

            actions = torch.cat(
                [torch.zeros((actions.shape[0], self.seq_len - actions.shape[1], *self.act_dim), device=actions.device),
                 actions], dim=1).to(dtype=torch.float32)

            returns_to_go = torch.cat(
                [torch.zeros((returns_to_go.shape[0], self.seq_len - returns_to_go.shape[1], 1), device=returns_to_go.device),
                 returns_to_go], dim=1).to(dtype=torch.float32)

            timesteps = torch.cat(
                [torch.zeros((timesteps.shape[0], self.seq_len - timesteps.shape[1]), device=timesteps.device),
                 timesteps], dim=1).to(dtype=torch.long)
        else:
            attention_mask = None

        action_preds, return_preds = self.forward(states, actions, None, returns_to_go, timesteps, attention_mask, **kwargs)

        return action_preds[0, -1]


"""
В этой задаче вам необходимо воспользоваться API сайта numbersapi.com

Вам дается набор чисел. Для каждого из чисел необходимо узнать,
существует ли интересный математический факт об этом числе.

Для каждого числа выведите Interesting, если для числа существует интересный факт, и Boring иначе.
Выводите информацию об интересности чисел в таком же порядке, в каком следуют числа во входном файле.

Пример запроса к интересному числу:
http://numbersapi.com/31/math?json=true

Пример запроса к скучному числу:
http://numbersapi.com/999/math?json=true

Пример входного файла:
31
999
1024
502

Пример выходного файла:
Interesting
Boring
Interesting
Boring

У вас есть неограниченное число попыток.
Время одной попытки: 5 mins
"""
from urllib.request import urlopen
import json

f_in = open('dataset_24476_3.txt', 'r', encoding='utf8')
lst_numbers = f_in.readlines()
f_in.close()

for number in lst_numbers:
    url = 'http://numbersapi.com/{}/math?json=true'.format(number.strip())
    resp = urlopen(url)
    resp_tmp = resp.read().decode('utf8')
    dict_resp = json.loads(resp_tmp)
    if dict_resp['found'] is True:
        print('Interesting')
    else:
        print('Boring')


import os, sys, torch, logging, yaml, time
import colorful as c
from torchsummary import summary
import torch.optim as optim
from torch.utils.data.dataloader import DataLoader
from warmup_scheduler import GradualWarmupScheduler
from my_utils import set_seed, Visualization_of_training_results, SSIM, PSNR, LPIPS, LPIPS_SET
from models.model import inpaint_model
from models.Discriminator import NLayerDiscriminator
from dataset import get_dataset
from tqdm import tqdm
from losses.L1 import l1_loss, masked_l1_loss
from losses.Perceptual import PerceptualLoss
from losses.Adversarial import NonSaturatingWithR1
from losses.HSV import HSV
from losses.Style import StyleLoss
from losses.Edge import Edge_MSE_loss
from tensorboardX import SummaryWriter

# set seed
set_seed(1234)
gpu = torch.device("cuda")

# open config file
with open('./config/model_config.yml', 'r') as config:
    args = yaml.safe_load(config)

logging.basicConfig(
    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
    datefmt="%m/%d/%Y %H:%M:%S",
    level=logging.INFO,
)

# set log
log_path = args['ckpt_path'] + args['name']
if not os.path.isdir(log_path):
    os.makedirs(log_path)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
sh = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(sh)
logger.propagate = False
fh = logging.FileHandler(os.path.join(log_path, args['name'] + '.txt'))
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)

# set tensorboardx log
log_dir = os.path.join(args['ckpt_path'], args['name'], 'log')
os.makedirs(log_dir, exist_ok=True)
writer = SummaryWriter(log_dir=log_dir, filename_suffix=f'_'+args['name'])

# Define the model
Inpaint_model = inpaint_model(args)
Discriminator = NLayerDiscriminator(input_nc=3)

# Define the dataset
train_dataset = get_dataset(args['data_path'], mask_path=args['mask_path'], is_train=True,image_size=args['image_size'])

test_dataset = get_dataset(args['val_path'], test_mask_path=args['val_mask_path'],is_train=False, image_size=args['image_size'])

# set initial
previous_epoch = -1
bestAverageF1 = 0
iterations = 0
best_psnr = 0
best_ssim = 0
best_lpips = 1
best_epoch_psnr = 0
best_epoch_ssim = 0
best_epoch_lpips = 0
Total_time = []

# loaded_ckpt
if os.path.exists(args['resume_ckpt']):
    data = torch.load(args['resume_ckpt'])
    D_data = torch.load(args['resume_D_ckpt'])
    Inpaint_model.load_state_dict(data['state_dict'],strict=False)
    Discriminator.load_state_dict(D_data['discriminator'])
    Inpaint_model = Inpaint_model.to(args['gpu'])
    Discriminator = Discriminator.to(args['gpu'])
    logger.info('Finished reloading the Epoch '+c.yellow(str(data['epoch']))+' model')
    # Optimizer
    raw_model = Inpaint_model.module if hasattr(Inpaint_model, "module") else Inpaint_model  # 要問一下
    optimizer = raw_model.configure_optimizers(args, new_lr=args['learning_rate'])
    # update optimizer info
    optimizer.load_state_dict(data['optimizer'])
    D_optimizer = optim.Adam(Discriminator.parameters(), lr=args['D_learning_rate'], betas=(0.9, 0.95))
    D_optimizer.load_state_dict(D_data['D_optimizer'])
    iterations = data['iterations']
    #bestAverageF1 = data['best_validation']
    previous_epoch = data['epoch']
    logger.info('Finished reloading the Epoch '+c.yellow(str(data['epoch']))+' optimizer')
    logger.info(c.blue('------------------------------------------------------------------'))
    logger.info('resume training with iterations: '+c.yellow(str(iterations))+ ', previous_epoch: '+c.yellow(str(previous_epoch)))
    logger.info(c.blue('------------------------------------------------------------------'))
else:
    # start form head
    Inpaint_model = Inpaint_model.to(args['gpu'])
    Discriminator = Discriminator.to(args['gpu'])
    logger.info(c.blue('------------------------------------------------------------------'))
    logger.info(c.red('Warnning: There is no trained model found. An initialized model will be used.'))
    logger.info(c.red('Warnning: There is no previous optimizer found. An initialized optimizer will be used.'))
    logger.info(c.blue('------------------------------------------------------------------'))
    # Optimizer
    raw_model = Inpaint_model.module if hasattr(Inpaint_model, "module") else Inpaint_model
    optimizer = raw_model.configure_optimizers(args, new_lr=args['learning_rate'] )
    D_optimizer = optim.Adam(Discriminator.parameters(), lr=args['D_learning_rate'], betas=(0.9, 0.95))

if args['lr_decay']:
    scheduler_cosine = optim.lr_scheduler.CosineAnnealingLR(optimizer, args['train_epoch'] - args['warmup_epoch'],
                                                            eta_min=float(args['lr_min']))
    scheduler = GradualWarmupScheduler(optimizer, multiplier=1, total_epoch=args['warmup_epoch'],
                                       after_scheduler=scheduler_cosine)
    scheduler.step()

    D_scheduler_cosine = optim.lr_scheduler.CosineAnnealingLR(D_optimizer, args['train_epoch'] - args['D_warmup_epoch'],
                                                            eta_min=float(args['D_lr_min']))
    D_scheduler = GradualWarmupScheduler(D_optimizer, multiplier=1, total_epoch=args['D_warmup_epoch'],
                                       after_scheduler=D_scheduler_cosine)
    D_scheduler.step()

    for i in range(1, previous_epoch):
        scheduler.step()
        D_scheduler.step()

# DataLoaders
train_loader = DataLoader(train_dataset, shuffle=True, pin_memory=True,
                                  batch_size=args['batch_size'] // args['world_size'],  # BS of each GPU
                                  num_workers=args['num_workers'])

test_loader = DataLoader(test_dataset, shuffle=False, pin_memory=True,
                         batch_size=args['batch_size'] // args['world_size'],
                         num_workers=args['num_workers'])

# Load pre_trained VGG model (for Perceptual_loss)
if args['Lambda_Perceptual'] is not 0 or not None:
    logger.info('Try load VGG mdoel for Perceptual_loss')
    VGG = PerceptualLoss()
if VGG is None:
    logger.info(c.red('Warnning: There is no pre_trained VGG model found. Try again or Check the Internet.'))
else:
    logger.info(c.blue('-----------------------------')+c.magenta(' Loaded! ')+c.blue('----------------------------'))
if args['Lambda_Style'] is not 0 or not None:
    logger.info('Try load VGG mdoel for Style_loss')
    style = StyleLoss()
if VGG is None:
    logger.info(c.red('Warnning: There is no pre_trained VGG model found. Try again or Check the Internet.'))
else:
    logger.info(c.blue('-----------------------------')+c.magenta(' Loaded! ')+c.blue('----------------------------'))

adv = NonSaturatingWithR1()

logger.info('Try load alex mdoel for LPIPS')
alex = LPIPS_SET()
alex = alex.to(args['gpu'])
if alex is None:
    logger.info(c.red('Warnning: There is no pre_trained alex model found. Try again or Check the Internet.'))
else:
    logger.info(c.blue('-----------------------------')+c.magenta(' Loaded! ')+c.blue('----------------------------'))

# print('==> Training start: ')
summary(raw_model, [(3, 256, 256), (1, 256, 256)])  # won't write in log (model show)

for epoch in range(args['train_epoch']):
    if previous_epoch != -1 and epoch < previous_epoch:
        continue
    if epoch == previous_epoch + 1:
        logger.info("Resume from Epoch %d" % epoch)

    epoch_start = time.time()
    raw_model.train()   # Train MODE
    Discriminator.train()
    loader = train_loader

    for it, items in enumerate(tqdm(loader, disable=False)):
        for k in items:
            if type(items[k]) is torch.Tensor:
                items[k] = items[k].to(args['gpu']).requires_grad_()    # img mask edge name

        Discriminator.zero_grad()
        D_img, _ = Discriminator(items['img'])
        LD1 = adv.discriminator_real_loss(items['img'], D_img)    # LD1+LGP (real loss)

        pred_img = raw_model(items['img'], items['mask'])
        pred_img_ = pred_img.detach()
        D_pred_img, _ = Discriminator(pred_img_)
        LD2 = adv.discriminator_fake_loss(D_pred_img, mask=items['mask'])  #LD23 (fake loss)
        D_loss = args['Lambda_LD1']*LD1 + args['Lambda_LD2']*LD2

        D_loss.backward()
        D_optimizer.step()

        raw_model.zero_grad()

        img = items['img']
        pred_img = pred_img
        mask = items['mask']
        Edge = items['edge']

        L1_loss = l1_loss(img, pred_img)
        Edge_loss = Edge_MSE_loss(img, pred_img, Edge)
        Perceptual_loss = VGG.forward(img, pred_img, mask=mask)
        style_loss = style(pred_img * mask, img * mask)
        HSV_loss_H, HSV_loss_S, HSV_loss_V, HSV_loss = HSV(img, pred_img, Edge)
        # HSV_edge_loss_H, HSV_edge_loss_S, HSV_edge_loss_V, HSV_edge_loss = HSV_edge(img, pred_img, Edge)
        # LHSV_H = args['Lambda_HSV'] * HSV_loss_H + args['Lambda_HSV_edge'] * HSV_edge_loss_H
        # LHSV_S = args['Lambda_HSV'] * HSV_loss_S + args['Lambda_HSV_edge'] * HSV_edge_loss_S
        # LHSV_V = args['Lambda_HSV']*HSV_loss_V + args['Lambda_HSV_edge']*HSV_edge_loss_V
        # LHSV = args['Lambda_HSV']*HSV_loss + args['Lambda_HSV_edge']*HSV_edge_loss
        LHSV = args['Lambda_HSV'] * HSV_loss_H + args['Lambda_HSV'] * HSV_loss_S

        D_pred_img, _ = Discriminator(pred_img)
        LG = adv.generator_loss(D_pred_img, mask=None)  # LG
        # G_loss = args['Lambda_L1']*L1_loss + args['Lambda_Perceptual']*Perceptual_loss + args['Lambda_LG']*LG
        G_loss = args['Lambda_L1'] * L1_loss + args['Lambda_Perceptual'] * Perceptual_loss + args['Lambda_LG'] * LG + \
                 args['Lambda_LHSV'] * LHSV + args['Lambda_Style'] * style_loss + args['Lambda_Edge'] * Edge_loss

        G_loss.backward()
        optimizer.step()

        iterations += 1  # number of iterations processed this step

        '''if iterations % args['print_freq'] == 0:
            logger.info(f"epoch {epoch + 1} iter {it}/{args['iterations_per_epoch']}: D1_loss {LD1.item():.5f}. | D2_loss {LD2.item():.5f}. | "
                        f"L1_loss {L1_loss.item():.5f} | Perceptual_loss {Perceptual_loss.item():.5f}. | HSV_Total_loss {LHSV.item():.5f}. | "
                        f"G_loss {LG.item():.5f}. | D_Total_loss {D_loss.item():.5f}. | G_Total_loss {G_loss.item():.5f}. | lr {scheduler.get_lr()[0]:e}")'''

        if iterations % 500 == 0:
            # Visualization of training results (Edge and GT)
            # Visualization_of_training_results(pred_edge, pred_img, items['img'], items['edge'], items['mask'], log_path, iterations)
            Visualization_of_training_results(pred_img, items['img'], items['mask'], log_path, iterations)
            writer.add_scalar('loss/LD1', LD1.item(), iterations)  # tensorboard for log (visualize)
            writer.add_scalar('loss/LD2', LD2.item(), iterations)  # tensorboard for log (visualize)
            writer.add_scalar('Total_loss/D_total_loss', D_loss.item(), iterations)  # tensorboard for log (visualize)
            writer.add_scalar('loss/L1_loss', L1_loss.item(), iterations)  # tensorboard for log (visualize)
            writer.add_scalar('loss/Perceptual_loss', Perceptual_loss.item(), iterations)
            writer.add_scalar('loss/style_loss', style_loss.item(), iterations)
            writer.add_scalar('loss/Edge_loss', Edge_loss.item(), iterations)
            writer.add_scalar('loss/LG', LG.item(), iterations)
            writer.add_scalar('HSV/H_loss', HSV_loss_H.item(), iterations)
            writer.add_scalar('HSV/S_loss', HSV_loss_S.item(), iterations)
            # writer.add_scalar('HSV/V_loss', HSV_loss_V.item(), iterations)
            # writer.add_scalar('HSV/H_edge_loss', HSV_edge_loss_H.item(), iterations)
            # writer.add_scalar('HSV/S_edge_loss', HSV_edge_loss_S.item(), iterations)
            # writer.add_scalar('HSV/V_edge_loss', HSV_edge_loss_V.item(), iterations)
            writer.add_scalar('loss/HSV_loss', HSV_loss.item(), iterations)
            # writer.add_scalar('loss/HSV_edge_loss', HSV_edge_loss.item(), iterations)
            writer.add_scalar('Total_loss/HSV_total_loss', LHSV.item(), iterations)
            writer.add_scalar('Total_loss/G_total_loss', G_loss.item(), iterations)

    logger.info(
        f"epoch {epoch + 1} iter {iterations}: D1_loss {LD1.item():.5f}. | D2_loss {LD2.item():.5f}. | {c.magenta('D_Total_loss')} {D_loss.item():.5f}. | "
        f"L1_loss {L1_loss.item():.5f} | Perceptual_loss {Perceptual_loss.item():.5f}. | style_loss {style_loss.item():.5f}. | Edge_loss {Edge_loss.item():.5f} | HSV_Total_loss {LHSV.item():.5f} | "
        f"G_loss {LG.item():.5f}. | {c.magenta('G_Total_loss')} {G_loss.item():.5f}. | lr {scheduler.get_lr()[0]:e}")

    # start EVAL
    logger.info(c.blue('-----------------------------')+c.cyan(' Start EVAL! ')+c.blue('-------------------------------------'))
    # Evaluation (Validation)
    raw_model.eval()    # eval MODE
    Discriminator.eval()
    loader = test_loader
    PSNR_center = []
    SSIM_center = []
    LPIPS_center = []

    for val_it, val_items in enumerate(loader):
        for k in val_items:
            if type(val_items[k]) is torch.Tensor:
                val_items[k] = val_items[k].to(args['gpu'])

        # in to the model (no_grad)
        with torch.no_grad():
            # pred_img, pred_edge = raw_model(val_items['img'], val_items['edge'], val_items['mask'])
            pred_img = raw_model(val_items['img'], val_items['mask'])

            PSNR_center.append(PSNR(val_items['img'], pred_img))
            SSIM_center.append(SSIM(val_items['img'], pred_img))
            LPIPS_center.append(LPIPS(val_items['img'], pred_img, alex))
            # FID_center.append(FID(val_items['img'], pred_img))

    PSNR_center = torch.stack(PSNR_center).mean().item()
    SSIM_center = torch.stack(SSIM_center).mean().item()
    LPIPS_center = torch.stack(LPIPS_center).mean().item()
    # FID_center = torch.stack(FID_center).mean().item()

    writer.add_scalar('Evaluation_Metrics/PSNR_center', PSNR_center, epoch + 1)     # tensorboard for log (visualize)
    writer.add_scalar('Evaluation_Metrics/SSIM_center', SSIM_center, epoch + 1)
    writer.add_scalar('Evaluation_Metrics/LPIPS_center', LPIPS_center, epoch + 1)
    # writer.add_scalar('Evaluation_Metrics/FID_center', FID_center, epoch + 1)

    # Save ckpt (best PSNR)
    if PSNR_center > best_psnr:
        best_psnr = PSNR_center
        best_epoch_psnr = epoch
        torch.save({'epoch': epoch + 1,
                    'state_dict': raw_model.state_dict(),
                    'optimizer': optimizer.state_dict(),
                    'iterations': iterations
                    }, os.path.join(log_path, "model_bestPSNR.pth"))

    # Save ckpt (best SSIM)
    if SSIM_center > best_ssim:
        best_ssim = SSIM_center
        best_epoch_ssim = epoch
        torch.save({'epoch': epoch + 1,
                    'state_dict': raw_model.state_dict(),
                    'optimizer': optimizer.state_dict(),
                    'iterations': iterations
                    }, os.path.join(log_path, "model_bestSSIM.pth"))

    # Save ckpt (best LPIPS)
    if LPIPS_center < best_lpips:
        best_lpips = LPIPS_center
        best_epoch_lpips = epoch
        torch.save({'epoch': epoch + 1,
                    'state_dict': raw_model.state_dict(),
                    'optimizer': optimizer.state_dict(),
                    'iterations': iterations
                    }, os.path.join(log_path, "model_bestLPIPS.pth"))

    # Save ckpt (each EPOCH)
    torch.save({'epoch': epoch + 1,
                'state_dict': raw_model.state_dict(),
                'optimizer': optimizer.state_dict(),
                'iterations': iterations
                }, os.path.join(log_path, "model_" + str(epoch + 1) + ".pth"))

    logger.info(f"current_epoch {epoch + 1} : current_PSNR {PSNR_center:.5f}. | "
                f"best_epoch {best_epoch_psnr+1} : best_PSNR {best_psnr:.5f}."
                f"\ncurrent_epoch {epoch + 1} : current_SSIM {SSIM_center:.5f}.  | "
                f"best_epoch {best_epoch_ssim+1} : best_SSIM {best_ssim:.5f}."
                f"\ncurrent_epoch {epoch + 1} : current_LPIPS {LPIPS_center:.5f}. | "
                f"best_epoch {best_epoch_lpips+1} : best_LPIPS {best_lpips:.5f}.")

    # Save D_ckpt (each EPOCH)
    torch.save({'discriminator': Discriminator.state_dict(),
                'D_optimizer': D_optimizer.state_dict(),
                }, os.path.join(log_path, "Discriminator_" + str(epoch + 1) + ".pth"))

    # Save the last model
    torch.save({'epoch': epoch + 1,
                'state_dict': raw_model.state_dict(),
                'optimizer': optimizer.state_dict(),
                'iterations': iterations
                }, os.path.join(log_path, "model_last.pth"))

    # Save D_ckpt (last EPOCH)
    torch.save({'discriminator': Discriminator.state_dict(),
                'D_optimizer': D_optimizer.state_dict(),
                }, os.path.join(log_path, "Discriminator_last.pth"))

    if args['lr_decay']:
        scheduler.step()
        D_scheduler.step()
        writer.add_scalar('lr/G_lr', scheduler.get_lr()[0], epoch + 1)
        writer.add_scalar('lr/D_lr', D_scheduler.get_lr()[0], epoch + 1)

    epoch_time = (time.time() - epoch_start)  # teain one epoch time
    logger.info(f"This epoch cost {epoch_time:.5f} seconds!")
    Total_time.append(epoch_time)
    logger.info(c.blue('------------------------------')+c.cyan(' End EVAL! ')+c.blue('--------------------------------------'))

writer.close()
Total_time = sum(Total_time)
logger.info(f"\n --------------------- Total time cost {Total_time/60/60:.5f} HR! ----------------------"
            f"\n -------------------------- ALL TRAINING IS DONE!! --------------------------")


# System Import
import sys
import json
import logging

# Application import
from handle_error import HandleError

__author__ = 'Manoj Jangid'
__version__ = '0.0.1'
__description__ = 'This is class is responsible for all Post operation'

class HandlePut(object):

    def __init__(self, collection, request):
        self.request = request
        self.collection = collection

    def put(self):
        req = self.request
        _key, _value = None, None
        json_obj = {}
        try:
            form_data = req['form']
            for data in form_data:
                key = data[0]
                val = data[1]
                if key == '_key':
                    _key = key
                    _value = val
                else:
                    json_obj[key] = val

            if _key == None:
                logging.debug('invalid input (_key is missing) data=%s' %
                              (json.dumps(json_obj)))
                error = HandleError(self.collection, self.request)
                return error.execute()

            query = {}
            query['_key'] = _value.encode('utf-8')

            response = self.collection.data.query(query=json.dumps(query))[0]

            logging.debug('New Value=%s' % (json.dumps(json_obj)))
            logging.debug('Old Value=%s' % (json.dumps(response)))

            try:
                logging.debug('Updating KV Store _key=%s data=%s' %
                              (_value, json.dumps(json_obj)))
                response = self.collection.data.update(
                    _value, json.dumps(json_obj))
            except:
                error = HandleError(self.collection, self.request)
                return error.execute()

            self.request['body'] = response

            logging.debug('end=put_operation')
            return {'payload': self.request, 'status': 200}

        except:
            logging.debug('error=%s' % (sys.exc_info()[0]))

    def execute(self):
        return self.put()


# importing PyTorch
import torch

def activation(x):
    """ Sigmoid activation function

        Arguments
        ---------
        x: torch.Tensor
    """
    return 1/(1+torch.exp(-x))
# Generate some data
torch.manual_seed(7) # set the random seed so it's predictable

# Features are 3 random normal variables
features = torch.randn((1,5))
# True weights for our data, random normal variables again
weights = torch.randn_like(features)
# and a true bias term
bias = torch.randn((1,1))

# passing the tensors through the activation function
y = activation(torch.sum(features * weights) + bias)
# y = activation((features * weights).sum() + bias)

# calculating output of network with matrix multiplication
weights = weights.view(5, 1) #resizing weights to fit ??
output = activation(torch.mm(features, weights) + bias) # using matrix multiplication
# output = activation(torch.mm(features, weights.size(5, 1)) + bias)

print(y)
print(output)


# This code is supporting material for the book
# Building Machine Learning Systems with Python
# by Willi Richert and Luis Pedro Coelho
# published by PACKT Publishing
#
# It is made available under the MIT License

from collections import defaultdict
from itertools import chain
from gzip import GzipFile
minsupport = 80

dataset = [[int(tok) for tok in line.strip().split()]
           for line in GzipFile('retail.dat.gz')]

counts = defaultdict(int)
for elem in chain(*dataset):
    counts[elem] += 1

# Only elements that have at least minsupport should be considered.
valid = set(el for el, c in counts.items() if (c >= minsupport))

# Filter the dataset to contain only valid elements
# (This step is not strictly necessary, but will make the rest of the code
# faster as the itemsets will be smaller):
dataset = [[el for el in ds if (el in valid)] for ds in dataset]

# Convert to frozenset for fast processing
dataset = [frozenset(ds) for ds in dataset]

itemsets = [frozenset([v]) for v in valid]
freqsets = itemsets[:]
for i in range(16):
    print("At iteration {}, number of frequent baskets: {}".format(
        i, len(itemsets)))
    nextsets = []

    tested = set()
    for it in itemsets:
        for v in valid:
            if v not in it:
                # Create a new candidate set by adding v to it
                c = (it | frozenset([v]))

                # Check if we have tested it already:
                if c in tested:
                    continue
                tested.add(c)

                # Count support by looping over dataset
                # This step is slow.
                # Check `apriori.py` for a better implementation.
                support_c = sum(1 for d in dataset if d.issuperset(c))
                if support_c > minsupport:
                    nextsets.append(c)
    freqsets.extend(nextsets)
    itemsets = nextsets
    if not len(itemsets):
        break
print("Finished!")

def rules_from_itemset(itemset, dataset, minlift=1.):
    nr_transactions = float(len(dataset))
    for item in itemset:
        consequent = frozenset([item])
        antecedent = itemset-consequent
        base = 0.0
        # acount: antecedent count
        acount = 0.0

        # ccount : consequent count
        ccount = 0.0
        for d in dataset:
          if item in d: base += 1
          if d.issuperset(itemset): ccount += 1
          if d.issuperset(antecedent): acount += 1
        base /= nr_transactions
        p_y_given_x = ccount/acount
        lift = p_y_given_x / base
        if lift > minlift:
            print('Rule {0} ->  {1} has lift {2}'
                  .format(antecedent, consequent,lift))

for itemset in freqsets:
    if len(itemset) > 1:
        rules_from_itemset(itemset, dataset, minlift=4.)


from typing import List

from PySide6 import QtCharts, QtCore, QtWidgets, QtGui
from PySide6.QtCore import Signal as qtSignal
from PySide6.QtCore import Slot as qtSlot

from cogip.utils.lidartablemodel import LidarTableModel

class LidarView(QtWidgets.QWidget):
    """
    This class is a widget that provides the graphic view and instantiates the polar chart,
    sliders and the table view.

    Attributes:
        min_distance: min value of the distance range (use on the chart)
        max_distance: max value of the distance range (use on the chart)
        max_intensity: max value of the intensity range (use on the chart)
        enable_plain_area: whether to display a line or an plain area for distance values
        max_intensity_threshold: max value of the intensity threshold slider (max value for YGLidar G2)
        new_filer: Qt signal emitted when the filter value is modified
        new_intensity_threshold: Qt signal emitted when the filter intensity threshold is modified
    """
    min_distance: int = 100
    max_distance: int = 5000
    max_intensity: int = 1024
    enable_plain_area: bool = True
    max_intensity_threshold: int = 1024
    new_filter: qtSignal(int) = qtSignal(int)
    new_intensity_threshold: qtSignal(int) = qtSignal(int)
    nb_angles: int = 360

    def __init__(
            self,
            table_model: LidarTableModel,
            angle_values: List[int],
            distance_values: List[int],
            intensity_values: List[int],
            distance_color: QtGui.QColor,
            intensity_color: QtGui.QColor,
            nb_angles: int,
            parent=None):
        """
        Class constructor.

        Arguments:
            table_model: table model object
            angle_values: angle values list
            distance_values: distance values list
            intensity_values: intensity values list
            distance_color: distance color
            intensity_color: intensity color
            nb_angles: number of angles
            parent: The parent widget
        """
        super().__init__(parent)

        self.table_model = table_model
        self.angle_values = angle_values
        self.distance_values = distance_values
        self.intensity_values = intensity_values
        self.distance_color = distance_color
        self.intensity_color = intensity_color
        self.nb_angles = nb_angles

        self.first_index = self.table_model.createIndex(0, 1)
        self.last_index = self.table_model.createIndex(self.nb_angles - 1, 2)

        layout = QtWidgets.QHBoxLayout(self)

        chart_layout = QtWidgets.QVBoxLayout()
        layout.addLayout(chart_layout)

        # Create polar chart and chart view
        self.chart = QtCharts.QPolarChart()
        self.chart.setTitle("Lidar Data")
        self.chart.setMinimumSize(QtCore.QSize(800, 800))
        self.chart_view = QtCharts.QChartView(self.chart)
        self.chart_view.setRenderHint(QtGui.QPainter.Antialiasing)
        chart_layout.addWidget(self.chart_view)

        # Add axis
        self.degree_axis = QtCharts.QValueAxis(self.chart)
        self.degree_axis.setRange(-180, 179)
        self.degree_axis.setLabelFormat("%d")
        self.degree_axis.setTickCount(17)
        self.degree_axis.setMinorTickCount(1)
        self.chart.addAxis(self.degree_axis, QtCharts.QPolarChart.PolarOrientationAngular)

        self.distance_axis = QtCharts.QValueAxis(self.chart)
        self.distance_axis.setRange(0, self.max_distance)
        self.distance_axis.setMinorTickCount(2)
        self.distance_axis.setTickCount(26)
        self.distance_axis.setTickAnchor(1)
        self.distance_axis.setLabelFormat("%d")
        self.chart.addAxis(self.distance_axis, QtCharts.QPolarChart.PolarOrientationRadial)

        self.intensity_axis = QtCharts.QValueAxis(self.chart)
        self.intensity_axis.setRange(0, self.max_intensity)
        self.intensity_axis.setLabelFormat("%d")
        self.intensity_axis.setLabelsVisible(False)
        self.chart.addAxis(self.intensity_axis, QtCharts.QPolarChart.PolarOrientationRadial)

        # Add series
        self.distance_serie = QtCharts.QLineSeries(name="Distance")
        # self.distance_serie.replace([QtCore.QPointF(i, 0) for i in range(self.nb_angles)])
        if self.enable_plain_area:
            self.center_serie = QtCharts.QLineSeries()
            # self.center_serie.replace([QtCore.QPointF(i, 0) for i in range(self.nb_angles)])
            self.distance_area_serie = QtCharts.QAreaSeries(
                self.distance_serie, self.center_serie, name="Distance"
            )
            self.chart.addSeries(self.distance_area_serie)
            self.distance_area_serie.attachAxis(self.degree_axis)
            self.distance_area_serie.attachAxis(self.distance_axis)
        else:
            self.chart.addSeries(self.distance_serie)
            self.distance_serie.attachAxis(self.degree_axis)
            self.distance_serie.attachAxis(self.distance_axis)

        self.intensity_serie = QtCharts.QLineSeries(name="Intensity")
        # self.intensity_serie.replace([QtCore.QPointF(i, 0) for i in range(self.nb_angles)])
        self.chart.addSeries(self.intensity_serie)
        self.intensity_serie.attachAxis(self.degree_axis)
        self.intensity_serie.attachAxis(self.intensity_axis)

        # Add a grid for sliders
        sliders_layout = QtWidgets.QGridLayout()
        sliders_layout.setColumnStretch(0, 1)
        sliders_layout.setColumnStretch(1, 10)
        sliders_layout.setColumnStretch(2, 10)
        sliders_layout.setColumnStretch(3, 200)
        chart_layout.addLayout(sliders_layout)

        # Distance Zoom slider
        distance_label = QtWidgets.QLabel("Max Distance:")
        sliders_layout.addWidget(distance_label, 0, 1)
        self.distance_value = QtWidgets.QLabel()
        sliders_layout.addWidget(self.distance_value, 0, 2)
        distance_zoom = QtWidgets.QSlider(
            QtCore.Qt.Horizontal, minimum=-self.max_distance, maximum=-self.min_distance
        )
        distance_zoom.valueChanged.connect(self.distance_zoom_changed)
        distance_zoom.setValue(-self.max_distance)
        distance_zoom.setStyleSheet(
            "QSlider::handle:horizontal {"
            "    border-radius: 5px; border: 2px solid #FFFFFF; width: 20px; margin: -5px 0;"
            f"   background: {hex(self.distance_color.rgb()).replace('0x', '#')}"
            "}"
            "QSlider::sub-page:horizontal {"
            f"   background: {hex(self.distance_color.rgb()).replace('0x', '#')};"
            "}"
        )
        sliders_layout.addWidget(distance_zoom, 0, 3)

        # Intensity Zoom slider
        intensity_label = QtWidgets.QLabel("Max Intensity:")
        sliders_layout.addWidget(intensity_label, 1, 1)
        self.intensity_value = QtWidgets.QLabel()
        sliders_layout.addWidget(self.intensity_value, 1, 2)
        intensity_zoom = QtWidgets.QSlider(QtCore.Qt.Horizontal, minimum=-self.max_intensity, maximum=0)
        intensity_zoom.valueChanged.connect(self.intensity_zoom_changed)
        intensity_zoom.setValue(-self.max_intensity)
        intensity_zoom.setStyleSheet(
            "QSlider::handle:horizontal {"
            "    border-radius: 5px; border: 2px solid #FFFFFF; width: 20px; margin: -5px 0;"
            f"   background: {hex(self.intensity_color.rgb()).replace('0x', '#')};"
            "}"
            "QSlider::sub-page:horizontal {"
            f"   background: {hex(self.intensity_color.rgb()).replace('0x', '#')};"
            "}"
        )
        sliders_layout.addWidget(intensity_zoom, 1, 3)

        # Distance filter slider
        filter_check = QtWidgets.QCheckBox()
        filter_check.stateChanged.connect(self.filter_check_changed)
        sliders_layout.addWidget(filter_check, 2, 0)
        self.filter_label = QtWidgets.QLabel("Distance filter:")
        sliders_layout.addWidget(self.filter_label, 2, 1)
        self.filter_value = QtWidgets.QLabel()
        sliders_layout.addWidget(self.filter_value, 2, 2)
        self.filter_slider = QtWidgets.QSlider(
            QtCore.Qt.Horizontal, minimum=self.min_distance, maximum=self.max_distance
        )
        self.filter_slider.valueChanged.connect(self.filter_slider_changed)
        self.filter_slider.setValue(500)
        sliders_layout.addWidget(self.filter_slider, 2, 3)

        filter_check.setChecked(False)
        self.filter_value.setEnabled(False)
        self.filter_slider.setEnabled(False)

        # Intensity threshold slider
        self.intensity_threshold_label = QtWidgets.QLabel("Intensity threshold:")
        sliders_layout.addWidget(self.intensity_threshold_label, 3, 1)
        self.intensity_threshold_value = QtWidgets.QLabel()
        sliders_layout.addWidget(self.intensity_threshold_value, 3, 2)
        self.intensity_threshold_slider = QtWidgets.QSlider(
            QtCore.Qt.Horizontal, minimum=0, maximum=self.max_intensity_threshold
        )
        self.intensity_threshold_slider.valueChanged.connect(self.intensity_threshold_slider_changed)
        self.intensity_threshold_slider.setValue(0)
        sliders_layout.addWidget(self.intensity_threshold_slider, 3, 3)

        # Create the table view
        table_view = QtWidgets.QTableView()
        table_view.setModel(self.table_model)
        table_view.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
        table_view.resizeColumnsToContents()
        table_view.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Maximum)
        layout.addWidget(table_view)

    @qtSlot(int)
    def distance_zoom_changed(self, value: int):
        """
        Qt Slot

        Function called when the distance zoom slider has changed.

        Arguments:
            value: new value
        """
        self.distance_axis.setMax(-value)
        self.distance_value.setText(str(-value))

    @qtSlot(int)
    def intensity_zoom_changed(self, value: int):
        """
        Qt Slot

        Function called when the intensity zoom slider has changed.

        Arguments:
            value: new value
        """
        self.intensity_axis.setMax(-value)
        self.intensity_value.setText(str(-value))

    @qtSlot(int)
    def filter_slider_changed(self, value: int):
        """
        Qt Slot

        Function called when the filter value slider has changed.

        Arguments:
            value: new value
        """
        self.filter_value.setText(str(value))
        self.new_filter.emit(value)

    @qtSlot(int)
    def filter_check_changed(self, state: int):
        """
        Qt Slot

        Function called when the filter checkbox is checked/unchecked.

        Arguments:
            state: new state
        """
        enabled = state != 0
        self.filter_value.setEnabled(enabled)
        self.filter_slider.setEnabled(enabled)

        self.new_filter.emit(self.filter_slider.value() if enabled else 0)

    @qtSlot(int)
    def intensity_threshold_slider_changed(self, value: int):
        """
        Qt Slot

        Function called when the intensity threshold value slider has changed.

        Arguments:
            value: new value
        """
        self.intensity_threshold_value.setText(str(value))
        self.new_intensity_threshold.emit(value)

    @qtSlot()
    def update_data(self) -> None:
        """
        Qt Slot

        Function called to update data on chart and table.
        """
        self.table_model.dataChanged.emit(self.first_index, self.last_index, [QtCore.Qt.DisplayRole])

        self.distance_serie.replace([
            QtCore.QPointF(angle, value) for angle, value in zip(self.angle_values, self.distance_values)
        ])
        self.intensity_serie.replace([
            QtCore.QPointF(angle, value) for angle, value in zip(self.angle_values, self.intensity_values)
        ])


# MultipleLinearRegression
import numpy as np
import pandas as pd
import quandl
import matplotlib.pyplot as plt
import statsmodels.formula.api as sm
quandl.read_key()

# Get Prices
aapl_table = quandl.get("WIKI/AAPL")
amzn_table = quandl.get("WIKI/AMZN")
msft_table = quandl.get("WIKI/MSFT")
wal_table = quandl.get("WIKI/WMT")
ebay_table = quandl.get("WIKI/EBAY")

# Get Closing Prices
aapl = aapl_table.loc["2016", ["Close"]]
amzn = amzn_table.loc["2016", ["Close"]]
msft = msft_table.loc["2016", ["Close"]]
wal = wal_table.loc["2016", ["Close"]]
ebay = ebay_table.loc["2016", ["Close"]]

# Get Log Returns
aapl_log = np.log(aapl.Close).diff().dropna()
amzn_log = np.log(amzn.Close).diff().dropna()
msft_log = np.log(msft.Close).diff().dropna()
wal_log = np.log(wal.Close).diff().dropna()
ebay_log = np.log(ebay.Close).diff().dropna()
df  = pd.concat([aapl_log, amzn_log, msft_log, wal_log, ebay_log], axis = 1).dropna()
df.columns = ["AAPL", "AMZN", "MSFT", "WMT", "EBAY"]
# print(df.tail())

# Create Stats Model
model = sm.ols(formula="AAPL ~ AMZN + MSFT + WMT + EBAY", data=df).fit()
print(model.summary())


import os

from PIL import Image
from glob import glob
from time import time

import cv2
import numpy as np
import torch
from detectron2 import model_zoo
from detectron2.config import get_cfg
from detectron2.data import MetadataCatalog
from detectron2.data.detection_utils import read_image
from detectron2.engine import DefaultPredictor
from detectron2.structures import Instances
from detectron2.utils.visualizer import Visualizer

save = 1
show_image = 0

threshold = 0.4
segment_type = "semantic"
# segment_type = "panoptic"
# segment_type = "detection"
# segment_type = "keypoint"

machine = "/host"
base = f"{machine}/media/z/0/MVPC10/DATA"
device = "device_03"
path = f"{base}/{device}"

# path = "{base}DOWNLOAD/1650497896553.jpg"
# path = f"{base}DOWNLOAD/1650444796136.jpg"
# path = f"{base}20220513-182408_03_RGB.jpg"
# dir = f"{path}/**/*."

# w, h = 400, 400

cfg = get_cfg()
if segment_type == "semantic":
    file = "COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml"
elif segment_type == "detection":
    file = "COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml"
elif segment_type == "keypoint":
    file = "COCO-Keypoints/keypoint_rcnn_R_101_FPN_3x.yaml"
elif segment_type == "panoptic":
    file = "COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml"
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = threshold
cfg.merge_from_file(model_zoo.get_config_file(file))
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(file)

def crop_img(arr):
    new_arr = arr
    side = w//5
    # # side cut
    new_arr[:, :side] = 0
    new_arr[:, w-side:] = 0
    # # top cut
    new_arr[:h//4, :] = 0

    # lr, tb = w//4, h//4
    # new_arr = new_arr[:, w//4:w-w//4]
    # new_arr = new_arr[h//4:, :]

    return new_arr

def output_selector(outputs):
    # global masks_bool
    # global boxes_arr
    obj = Instances(image_size=(w, h))
    cls = outputs['instances'].pred_classes
    indx_to_remove = (cls != 0).nonzero().flatten().tolist()

    cls = np.delete(cls.cpu().numpy(), indx_to_remove)
    cls = torch.tensor(cls).to('cuda:0')
    obj.set('pred_classes', cls)

    scores = outputs["instances"].scores
    scores = np.delete(scores.cpu().numpy(), indx_to_remove)
    scores = torch.tensor(scores).to('cuda:0')
    obj.set('scores', scores)

    masks = outputs['instances'].pred_masks
    masks = np.delete(masks.cpu().numpy(), indx_to_remove, axis=0)
    bool_mask = [mask for i in masks for mask in i]
    masks = torch.tensor(masks).to('cuda:0')
    obj.set('pred_masks', masks)

    # boxes = outputs['instances'].pred_boxes
    # boxes = np.delete(boxes.tensor.cpu().numpy(), indx_to_remove, axis=0)
    # boxes = torch.tensor(boxes).to('cuda:0')
    # obj.set('pred_boxes', boxes)

    return obj, bool_mask

files = glob(f'{path}/**/*.jpg', recursive=True)
# print(files)

for i, j in enumerate(files):
    result_img_path = j.replace(f"{device}", f"{device}/output/img")
    result_mask_path = j.replace(f"{device}", f"{device}/output/mask")
    # if j in result_img_path: break

    img = read_image(j, format="BGR")
    w, h = img.shape[0], img.shape[1]
    # img = Image.open(i)
    # arr = np.array(img)
    # img = mask_img(arr)

    img = crop_img(img)

    start = time()

    predictor = DefaultPredictor(cfg)
    v = Visualizer(img[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.0)

    if segment_type == "panoptic":
        panoptic_seg, segments_info = predictor(img)["panoptic_seg"]
        out = v.draw_panoptic_seg_predictions(panoptic_seg.to("cpu"), segments_info)
    else:
        outputs = predictor(img)
        obj, bool_mask = output_selector(outputs)
        out = v.draw_instance_predictions(obj.to("cpu"))

    if len(bool_mask) > 0:
        bool2arr = np.array(bool_mask, dtype=np.uint8)
        mask_img = bool2arr*255

        if save == 1:
            img_path = os.path.dirname(result_img_path)
            mask_path = os.path.dirname(result_mask_path)

            if not os.path.exists(img_path): os.makedirs(img_path)
            if not os.path.exists(mask_path): os.makedirs(mask_path)

            im = Image.fromarray(mask_img)
            im.save(result_mask_path)
            out.save(result_img_path)

        print(f"{i} {time()-start:.2f}")

        if show_image == 1:
            cv2.namedWindow(segment_type, cv2.WINDOW_NORMAL)
            cv2.imshow(segment_type, out.get_image()[:, :, ::-1])

            win_name = "mask"
            cv2.namedWindow(win_name, cv2.WINDOW_NORMAL)
            cv2.imshow(win_name, mask_img)

            if cv2.waitKey(0) == 27:
                cv2.destroyAllWindows()
            break
    else:
        print(f"{i} no mask")