# importing DataFrames Lib - Pandas
import pandas as pd
# Decision Tree Model
from sklearn.tree import DecisionTreeRegressor

# Path of the file to read
iowa_file_path = '../input/home-data-for-ml-course/train.csv'
home_data = pd.read_csv(iowa_file_path)

# Definindo Target(y) e Features(X)
y = home_data.SalePrice
feature_columns = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']
X = home_data[feature_columns]

# Import the train_test_split function
from sklearn.model_selection import train_test_split
# Spliting DataSet( X e y ) into training (train_) and Validating(val_) Sets
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state = 1)

# Specify Model
name_model = DecisionTreeRegressor(random_state=1)   #(random_state=num_inteiro)
# Fit Model
name_model.fit(train_X, train_y)

# Predict with all validation observations
val_predictions = name_model.predict(val_X)  #para parte do DataFrame, val_X.head() por exemplo.
#print("First in-sample predictions:", iowa_model.predict(X.head()))
print("Actual target values for those homes:", y.head().tolist())

# print the top few validation predictions
print(iowa_model.predict(val_X.head()))
# print the top few actual prices from validation data
print(val_y.head().tolist())

print(mean_absolute_error(val_y, val_predictions))

# Make validation predictions and calculate mean absolute error
val_predictions = name_model.predict(val_X)
#print(mean_absolute_error(val_y, val_predictions))
val_mae = mean_absolute_error(val_predictions, val_y)
print("Validation MAE: {:,.0f}".format(val_mae))

#===== iteração 1 =============
candidate_max_leaf_nodes = [5, 25, 50, 100, 500, 600]
# Write loop to find the ideal tree size from candidate_max_leaf_nodes
best_leaf_nodes_value = 0
min_mae = 100000
for max_leaf_nodes in candidate_max_leaf_nodes:
    my_mae = get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y)
    print("Max leaf nodes: %d  \t\t Mean Absolute Error:  %d" %(max_leaf_nodes, my_mae))
    if my_mae < min_mae:
        best_leaf_nodes_value = max_leaf_nodes
        min_mae = my_mae
# Store the best value of max_leaf_nodes (it will be either 5, 25, 50, 100, 250 or 500)
best_tree_size = best_leaf_nodes_value

#===== iteração 2 =============
scores = {leaf_size: get_mae(leaf_size, train_X, val_X, train_y, val_y) for leaf_size in candidate_max_leaf_nodes}
best_tree_size = min(scores, key=scores.get)

R


from model_selection_framework.search import *
from sklearn.linear_model import LogisticRegression
import warnings
warnings.simplefilter("ignore")

lr_configs = {
    'folder': 'tests',
    'model_init': LogisticRegression,
    'name': 'LogisticRegression',
    'init_parameters': {
        'solver': ['saga'],
        'penalty': ['l1', 'l2', 'elasticnet'],
        'tol': [0.1, 0.01, 0.001, 0.0001, 0.00001],
        'C': [0.01, 0.1, 0.5, 1.0, 1.5, 2.5, 10, 25, 50],
        'fit_intercept': [True, False],
        'l1_ratio': [0.5]
    }
}

from project_code.data_utils import *

users_df, cls_embedding, mean_embedding = read_data('data')
users_df[y_col] = (users_df[y_col] > 0) * 1
merged_df = merge_tables(users_df, cls_embedding, mean_embedding)
cls_cols = [c for c in merged_df.columns if c.endswith('_cls')]
mean_cols = [c for c in merged_df.columns if c.endswith('_mean')]
train, test = split_data(merged_df, test_size=0.3, random_state=42, regular=False)
preprocess = Preprocess()
train[facebook_cols + demo_cols] = preprocess.scale(train[facebook_cols + demo_cols], True)
test[facebook_cols + demo_cols] = preprocess.scale(test[facebook_cols + demo_cols], False)
train[facebook_cols + demo_cols] = preprocess.fillna(train[facebook_cols + demo_cols], True)
test[facebook_cols + demo_cols] = preprocess.fillna(test[facebook_cols + demo_cols], False)
X_cols = facebook_cols + demo_cols + personal_cols + psychosocial_cols + psychiatric_cols + cls_cols + mean_cols
X_train, X_test, y_train, y_test = train[X_cols], test[X_cols], train[y_col], test[y_col]
results = search(lr_configs, (X_train, y_train), (X_test, y_test), search_type='greedy',
                max_iter=100, metric='accuracy', max_rounds=3, random_start=10, beam_size=3)


from datetime import datetime
from mtgoxHttpInterface import MtgoxHttpInterface

class MtgoxTranslator(object):
	def __init__(self, auth):
		self.httpInterface = MtgoxHttpInterface(auth['key'],auth['secret'])
		self.orderTypes = {}

	def get_ticker(self):
		tickQry = self.httpInterface.get_ticker()
		if 'error' not in tickQry:
			ticker = {'time':datetime.today().strftime('%y-%m-%d %H:%M:%S'),
			          'price':float(tickQry['ticker']['last']),
					  'bid':float(tickQry['ticker']['buy']),
					  'ask':float(tickQry['ticker']['sell'])}
			tickQry = ticker
		return tickQry

	def get_depth(self):
		depthQry = self.httpInterface.get_depth()
		if 'error' not in depthQry:
			depth = {'time':datetime.today().strftime('%y-%m-%d %H:%M:%S'),
			         'bid':[],'ask':[]}
			for order in depthQry['asks']:
				depth['ask'].append(order)
			depthQry['bids'].reverse() # normally low to high (price)
			for order in depthQry['bids']:
				depth['bid'].append(order)
			depthQry = depth
		return depthQry

	def get_account(self):
		accountQry = self.httpInterface.get_info()
		if 'error' not in accountQry:
			balance = {'usd':float(accountQry['Wallets']['USD']['Balance']['value']),
					   'btc':float(accountQry['Wallets']['BTC']['Balance']['value'])}
			fee = float(accountQry['Trade_Fee'])
			accountQry = {'time':datetime.today().strftime('%y-%m-%d %H:%M:%S'),
			              'balance':balance,
			              'fee':fee}
		return accountQry

	def get_orders(self):
		orderQry = self.httpInterface.get_orders()
		if 'error' not in orderQry:
			self.orderTypes = {}
			orderList = []
			for order in orderQry['orders']:
				otype = 'buy' if order['type'] == 2 else 'sell'
				self.orderTypes[order['oid']] = order['type']
				orderList.append({'id':order['oid'],
								  'type':otype,
								  'amount':float(order['amount']),
								  'price':float(order['price'])})
			orderQry = orderList
		return orderQry

	def buy_btc(self,amount,price = 0):
		if amount < 0.01:
			return {'error':'Must trade at least 0.01'}
		buyQry = self.httpInterface.buy_btc(amount,price)
		if 'error' not in buyQry:
			self.orderTypes[buyQry['oid']] = 2
			order = {'id':buyQry['oid'],
					 'type':'buy',
					 'amount':amount,
					 'price':price}
			buyQry = order
		return buyQry

	def sell_btc(self,amount,price = 0):
		if amount < 0.01:
			return {'error':'Must trade at least 0.01'}
		sellQry = self.httpInterface.sell_btc(amount,price)
		if 'error' not in sellQry:
			self.orderTypes[sellQry['oid']] = 1
			order = {'id':sellQry['oid'],
					 'type':'sell',
					 'amount':amount,
					 'price':price}
			sellQry = order
		return sellQry

	def cancel_order(self,oid):
		if oid not in self.orderTypes:
			return {'error':'That is not a valid order id'}
		cancelQry = self.httpInterface.cancel_order(oid,self.orderTypes[oid])
		if 'error' not in cancelQry:
			self.orderTypes.pop(oid)
			cancelQry = {'id':oid}
		return cancelQry


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('daily', '0002_auto_20150403_1213'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='article_detail',
            name='article',
        ),
        migrations.DeleteModel(
            name='Article_Detail',
        ),
    ]


import pytest

from caluma.caluma_analytics import (
    models as analytics_models,
    schema as analytics_schema,
)
from caluma.caluma_form import models as form_models, schema as form_schema
from caluma.caluma_user.models import OIDCUser
from caluma.caluma_workflow import models as workflow_models, schema as workflow_schema
from caluma.caluma_workflow.api import complete_work_item
from caluma.extensions.settings import settings
from caluma.extensions.visibilities import MySAGWVisibility

@pytest.mark.usefixtures("_caluma_data")
@pytest.mark.parametrize(
    "user_name,visibility_map",
    [
        (
            "admin",
            {
                "Form": 8,
                "WorkItem": 16,
                "Document": 10,
                "Case": 6,
                "Question": 22,
                "Answer": 7,
                "AnalyticsTable": 1,
            },
        ),
        (
            "test",
            {
                "Form": 8,
                "WorkItem": 5,
                "Document": 3,
                "Case": 3,
                "Question": 22,
                "Answer": 4,
                "AnalyticsTable": 0,
            },
        ),
    ],
)
def test_visibilities_default(
    db,
    case_access_event_mock,
    create_document_review_case,
    admin_info,
    admin_user,
    requests_mock,
    analytics_table,
    user_name,
    visibility_map,
    work_item_factory,
    snapshot,
):
    admin_info.context.user.username = user_name
    admin_info.context.user.groups = [user_name]
    admin_info.context.user.group = user_name

    admin_user = OIDCUser(
        b"sometoken",
        {"sub": "admin", settings.OIDC_GROUPS_CLAIM: ["admin"]},
    )
    user = OIDCUser(
        b"sometoken",
        {"sub": user_name, settings.OIDC_GROUPS_CLAIM: [user_name]},
    )

    def prepare_case(case_identifier):
        case = create_document_review_case()
        case.meta = {"identifier": case_identifier}
        case.save()
        complete_work_item(case.work_items.get(task_id="submit-document"), admin_user)
        case.work_items.get(task_id="review-document").document.answers.create(
            question_id="review-document-decision",
            value="review-document-decision-continue",
        )
        complete_work_item(case.work_items.get(task_id="review-document"), admin_user)
        return case

    # case_1 with access
    case_1 = prepare_case("access")
    data = {"data": [{"attributes": {"case-id": str(case_1.pk)}}]}
    requests_mock.get(f"{settings.API_BASE_URI}/case/accesses", json=data)

    # case_2 with circulation invite
    case_2 = prepare_case("circulation")
    case_2_circulation = case_2.work_items.get(task_id="circulation").child_case
    work_item_factory(
        case=case_2_circulation,
        assigned_users=[user.username],
        task=workflow_models.Task.objects.get(slug="circulation-decision"),
        document__form=form_models.Form.objects.get(slug="circulation-form"),
        child_case=None,
    )

    # case_3 no access for non-admin
    prepare_case("only admin")

    test_map = [
        (
            form_models.Form,
            form_schema.Form,
            ["pk"],
        ),
        (
            workflow_models.WorkItem,
            workflow_schema.WorkItem,
            ["task_id", "case__workflow_id"],
        ),
        (
            form_models.Document,
            form_schema.Document,
            ["work_item__task_id", "case__workflow_id"],
        ),
        (
            workflow_models.Case,
            workflow_schema.Case,
            ["family__meta__identifier", "workflow__pk"],
        ),
        (
            form_models.Question,
            form_schema.Question,
            ["pk"],
        ),
        (
            form_models.Answer,
            form_schema.Answer,
            ["question__pk", "value"],
        ),
        (
            analytics_models.AnalyticsTable,
            analytics_schema.AnalyticsTable,
            ["pk"],
        ),
    ]

    vis = MySAGWVisibility()

    for model, node, values_list in test_map:
        queryset = model.objects.all()
        filtered = vis.filter_queryset(node, queryset, admin_info)
        assert filtered.count() == visibility_map[model.__name__]
        values_to_snapshot = [
            tuple(xi for xi in v if xi is not None)
            for v in filtered.values_list(*values_list)
        ]

        assert sorted(values_to_snapshot) == snapshot(name=model.__name__)


from .plugin import Plugin
import os,time
from nltk.corpus import stopwords
import collections
import asyncio

class Stats(Plugin):
    """
    Plugin template class

    API reference here:
        http://rapptz.github.io/discord.py/api.html#
    """

    title = "Name of my plugin"
    desc = "What is my purpose?"
    commands = ["!top"]

    def __init__(self): pass

    def on_message(self, client, message):

        if message.content.startswith('!top'):
            authors = []
            words = []
            for message in client.logs_from(message.channel,limit=2000):
                if message.author.name != "ButlerBot":
                    authors.append(str(message.author))
                if message.author != client.user and not message.content.startswith("!"):
                    for word in str(message.content).split():
                            words.append(str(word))
            yield from client.send_message(message.channel,"__Most Popular Words:__ \n\n"+self.popularWords(words))
            yield from client.send_message(message.channel,"__Most Spammy Users:__ \n\n"+self.popularWords(authors))

    def popularWords(self,words):
        filtered_words = [word for word in words if word.lower() not in stopwords.words('english')]
        counter = collections.Counter(filtered_words).most_common(10)
        message = ""
        count = 1
        for key,value in (counter):
            message+="**"+str(count)+"**. "+str(key)+" : *"+str(value)+"*\n"
            count+=1
        return message


# Streamux main class
#
# The Streamux main class aggregates the functionality of the other
# components and handles initialization.

import sys
import logging
import optparse
import ConfigParser

import streamux.db

__version__ = "dev build"

def error_and_exit(msg):
  """Log the given message as critical and exit with exit code 1."""
  logging.critical(msg)
  sys.exit(1)

class StreamuxApp(object):
  """Main Streamux class. Handles application initialization."""

  def _parse_args(self):
    """Parse the commandline arguments and store them in the app
    object. Exits if something is wrong, otherwise returns after
    assigning self.options and self.config_file."""

    usage = "usage: %prog [options] <configuration file>"
    version = "%%prog %s" % __version__

    parser = optparse.OptionParser(usage=usage, version=version)
    parser.set_defaults(debug=False)
    parser.add_option("-d", "--debug", action="store_true", dest="debug",
                      help=("Enable verbose logging to stdout, "
                            "and don't fork into the background."))
    self.options, args = parser.parse_args()
    if len(args) != 1:
      parser.error("You must supply a configuration file.")
    self.config_file = args[0]

  def _load_config(self):
    """Load the configuration file into self.config."""

    self.config = ConfigParser.SafeConfigParser()
    fd = open(self.config_file, 'r')

    try:
      self.config.readfp(fd)
    except ConfigParser.ParsingError:
      error_and_exit("Parse error while reading the configuration file.")
    except ConfigParser.InterpolationError:
      error_and_exit("Variable interpolation error while reading the configuration file.")

    fd.close()

  def _setup_logging(self):
    """Set up the logging infrastructure using the given configuration
    information in the 'logging' section."""

    log_format = '%(asctime)s (%(levelname)s) %(message)s'
    date_format = '%c'

    # If in debug mode, completely ignore the configuration and setup
    # a debug logger on stdout.
    if self.options.debug:
      logging.basicConfig(format=log_format,
                          datefmt=date_format,
                          level=logging.DEBUG,
                          stream=sys.stderr)
    else:

      log_levels = { 'error': logging.ERROR,
                     'warn': logging.WARN,
                     'info': logging.INFO,
                     'debug': logging.DEBUG }

      # Check configuration
      if not self.config.has_section('logging'):
        error_and_exit("Section [logging] is missing from the configuration.")
      elif not self.options.debug and (not self.config.has_option('logging', 'level') or
                                       not self.config.has_option('logging', 'file')):
        error_and_exit("Missing configuration attributes for logging.")

      level_str = self.config.get('logging', 'level')
      if level_str not in log_levels.keys():
        error_and_exit("Log level must be one of: %s" % ', '.join(log_levels.keys()))

      # Set up logging according to the configuration
      logging.basicConfig(format=log_format,
                          datefmt=date_format,
                          level=log_levels[level_str],
                          filename=self.config.get('logging', 'file'))

    logging.info("Logger initialized%s" % (self.options.debug and " in debug mode" or ""))

  def _start_components(self):
    """Start the components of Streamux in the correct init order."""
    db = streamux.db.StreamuxDatabase(self.config)

  def run(self):
    self._parse_args()
    self._load_config()
    self._setup_logging()
    self._start_components()


__author__ = 'Administrator'
from django.utils.safestring import mark_safe
class Page:
    '''
    current_page当前页
    data_count总数据
    per_page_count每页显示数据个数
    pager_num每页显示页码数
    @property 装饰一个方法变成属性来调用
    '''
    def __init__(self,current_page,data_count,per_page_count=10,pager_num=7):
        self.current_page = current_page
        self.data_count = data_count
        self.per_page_count = per_page_count
        self.pager_num = pager_num
    #当前页显示的起始页
    @property
    def start(self):
        return (self.current_page-1) * self.per_page_count
    #当前页面显示的末页
    @property
    def end(self):
        return self.current_page * self.per_page_count
    #总页数
    @property
    def total_count(self):
        #divmod取商和余数v为商，y为余数
        v,y = divmod(self.data_count, self.per_page_count)
        #当余数不为0时，总页数加一
        if y:
            v += 1
        return v
    #base_url就是url路由
    def page_str(self, base_url):
        page_list = []
        #当总页数小于，每页显示的页数时
        if self.total_count < self.pager_num:
            start_index = 1
            end_index = self.total_count + 1
        #当总页数大于，每页显示的页数时
        else:
            if self.current_page <= (self.pager_num+1)/2:
                start_index = 1
                end_index = self.pager_num + 1
            else:
                start_index = self.current_page - (self.pager_num-1)/2
                end_index = self.current_page + (self.pager_num+1)/2
                if (self.current_page + (self.pager_num-1)/2) > self.total_count:
                    end_index = self.total_count + 1
                    start_index = self.total_count - self.pager_num + 1
        #上一页，如果当前页数为1时，点击上一页href="javascript:void(0);"代表不做任何操作
        if self.current_page == 1:
            prev = '上一页'
        else:
            prev = '上一页' %(base_url,self.current_page-1,)
        page_list.append(prev)

        for i in range(int(start_index),int(end_index)):
            if i ==self.current_page:
                temp = '%s' %(base_url,i,i)
            else:
                temp = '%s' %(base_url,i,i)
            page_list.append(temp)
        #下一页，如果下一页等于总页数时，点击下一页href="javascript:void(0);"代表不做任何操作
        if self.current_page == self.total_count:
            nex = '下一页'
        else:
            nex = '下一页' %(base_url,self.current_page+1,)
        page_list.append(nex)

        #跳转 val是获取当前输入的页码值，然后location.href跳转到后面的base+val字符串拼接
        # jump = """
        # <input type='text'  />GO
        #
        # """ %(base_url,)
        jump = """
        <input type='text'  /><input type='button' value='GO' onclick='jumpTo(this, "%s?p=");' />

        """ %(base_url,)

        page_list.append(jump)

        #以下加mark_safe，是为了确保后台定义字符串包含特殊的html字符，前端可以做解析
        page_str = mark_safe("".join(page_list))

        return page_str


import pymysql

class Modelo_inicio:
    def __init__(self):
        self.conexion  = pymysql.connect(
            host="localhost",
            user="root",
            password="",
            db="bd_prueba"

        )
        self.cursor = self.conexion.cursor()

    def iniciar_sesion(self, usuario, contraseña):
        try:
            query = "SELECT contraseña FROM usuario WHERE usuario = %s and contraseña=%s"
            data =(usuario, contraseña)
            self.cursor.execute(query, data)
            return self.cursor.fetchall()
        except Exception as e:
            print("El error esta en: ", e)
    def cerrar_conexion(self):
        self.conexion.close()


#!/usr/bin/env python
"""Example of using Cockroach DB with SQLAlchemy."""

import uuid
from sqlalchemy import create_engine, Column, String, Integer, DateTime, Boolean
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import func
from cockroachdb.sqlalchemy import run_transaction

Base = declarative_base()

class ShoppingListItem(Base):
    """Mapping class for shopping list item table rows."""
    # set the table name
    __tablename__ = 'shopping_lists'
    # use a composite primary key
    username = Column(String, primary_key=True)
    item_id = Column(UUID(as_uuid=True), primary_key=True)
    # automatically set the time to when the item is added
    added = Column(DateTime(timezone=True), server_default=func.now())
    item = Column(String)
    quantity = Column(Integer)
    # defaults to False as the item has just been added
    bought = Column(Boolean, default=False)

# we are not using security in this example
connect_args = {'sslmode': 'disable'}

# connect to the chores database running locally
engine = create_engine(
    'cockroachdb://root@localhost:26257/chores',
    connect_args=connect_args,
    echo=True                   # Log SQL queries to stdout
)

# automatically create the database table based on the ShoppingListItem class
Base.metadata.create_all(engine)

def create_shopping_list(session):
    """Create a shopping list row."""
    apples = ShoppingListItem(
        username="alice",
        item_id=uuid.uuid4(),
        item="Gala apples",
        quantity=3
    )
    session.add(apples)
    print("\n*** Added shopping list item. ***")
    print(apples.username, apples.item_id, apples.item, apples.quantity)
    print("\n")

# call the create function
run_transaction(sessionmaker(bind=engine), create_shopping_list)

def read_shopping_list(session):
    """Read the rows in the shopping list table."""

    # there is only one row, so we can retrieve the first result
    apples = session.query(ShoppingListItem).filter_by(
        username='alice').first()
    print("\n*** Retrieved a row from shopping_lists. ***")
    print(apples.username, apples.item_id, apples.item, apples.added,
          apples.quantity, apples.bought)
    print("\n")

# call the read function
run_transaction(sessionmaker(bind=engine), read_shopping_list)

def update_shopping_list(session):
    """Update, or modify, the contents of a row in the shopping list table."""

    apples = session.query(ShoppingListItem).filter_by(
        username='alice').first()
    # modify the shopping list item, which triggers an update
    apples.bought = True

    # show the modified shopping list item
    print("\n*** Modified a shopping list item. ***")
    print(apples.username, apples.item_id, apples.item, apples.added,
          apples.quantity, apples.bought)
    print("\n")

# call the update function
run_transaction(sessionmaker(bind=engine), update_shopping_list)

def delete_shopping_list(session):
    """Delete a shopping list row."""

    apples = session.query(ShoppingListItem).filter_by(
        username='alice').first()
    # delete the shopping list item
    session.delete(apples)

    # show that there's no more rows in the table
    print("\n*** Deleted a row in shopping_lists. Rows in shopping_lists: ***",
          session.query(ShoppingListItem).filter_by(username='alice').count(),
          "\n")

# call the delete function
run_transaction(sessionmaker(bind=engine), delete_shopping_list)


from __future__ import annotations

import json
import logging
import os
import time
from typing import Any, Dict, Optional, List

from .. import utils
from .. import version
from ..exceptions import HTTPConnectionError, SettingsError, SettingsNotFoundError
from ..exceptions import InstantiationError
from ..foundation.setting.SettingsAddon import SettingsAddon
from ..foundation.setting.SettingsAddonFactory import SettingsAddonFactory
from ..webhooks.GitHubApi import GitHubApi

AVAILABLE_DEBUG_LEVELS: List[str] = ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "EXCEPTION"]
AVAILABLE_MANAGERS: List[str] = ["docker", "kubernetes"]

ONE_WEEK: int = 604800

DEFAULTS: Dict[str, Any] = {
    "image": 'kathara/base',
    "manager_type": 'docker',
    "terminal": utils.exec_by_platform(lambda: '/usr/bin/xterm', lambda: '', lambda: 'Terminal'),
    "open_terminals": True,
    "device_shell": '/bin/bash',
    "net_prefix": 'kathara',
    "device_prefix": 'kathara',
    "debug_level": 'INFO',
    "print_startup_log": True,
    "enable_ipv6": False
}
SETTINGS_FILENAME = "kathara.conf"
DEFAULT_SETTINGS_PATH: str = os.path.join(utils.get_current_user_home(), ".config", SETTINGS_FILENAME)

class Setting(object):
    """Class responsible for interacting with Kathara Settings."""

    __slots__ = ['image', 'manager_type', 'terminal', 'open_terminals', 'device_shell', 'net_prefix',
                 'device_prefix', 'debug_level', 'print_startup_log', 'enable_ipv6', 'last_checked', 'addons']

    __instance: Setting = None

    @staticmethod
    def get_instance() -> Setting:
        """Return an instance of Setting.

        Returns:
            Kathara.setting.Setting: An instance of Setting.

        Raises:
            InstantiationError: If two instances of the class are created.
        """
        if Setting.__instance is None:
            Setting()

        return Setting.__instance

    def __init__(self) -> None:
        if Setting.__instance is not None:
            raise InstantiationError("This class is a singleton!")
        else:
            # Load default settings to use
            for (name, value) in DEFAULTS.items():
                setattr(self, name, value)

            self.addons: Optional[SettingsAddon] = None
            self.last_checked: float = time.time() - ONE_WEEK

            self.load_settings_addon()  # Load default addon

            Setting.__instance = self

    def __getattr__(self, item: str) -> Any:
        return self.addons.get(item)

    def __setattr__(self, name: str, value: Any) -> None:
        if name in self.__slots__:
            super(Setting, self).__setattr__(name, value)
            return

        setattr(self.addons, name, value)

    def load_from_disk(self, path: Optional[str] = None) -> None:
        """Load settings from a specific path on disk.

        Args:
            path (Optional[str]): A path where the kathara.conf file is stored. If None, default path is used.

        Returns:
            None

        Raises:
            SettingsNotFound: If the Settings file is not found in specified path.
            SettingsError: If the specified file is not a valid JSON.
        """
        settings_path = os.path.join(path, SETTINGS_FILENAME) if path is not None else DEFAULT_SETTINGS_PATH

        if not os.path.exists(settings_path):  # Requested settings file doesn't exist, throw exception
            raise SettingsNotFoundError(settings_path)
        else:  # Requested settings file exists, read it and check values
            settings = {}
            with open(settings_path, 'r') as settings_file:
                try:
                    settings = json.load(settings_file)
                except ValueError:
                    raise SettingsError("Not a valid JSON.")

            for name, value in settings.items():
                if hasattr(self, name):
                    setattr(self, name, value)

            self.load_settings_addon()  # Manager may be changed with loaded settings, reload addon
            self.addons.load(settings)  # Load values into the addon object

    def save_to_disk(self, path: Optional[str] = None) -> None:
        """Saves settings to a kathara.conf file in the specified path on disk.

        Args:
            path (Optional[str]): A path where the kathara.conf file will be stored. If None, default path is used.

        Returns:
            None
        """
        settings_path = os.path.join(path, SETTINGS_FILENAME) if path is not None else DEFAULT_SETTINGS_PATH
        settings_dirname = os.path.dirname(settings_path)

        if not os.path.isdir(settings_dirname):  # Create folder if it doesn't exist
            os.mkdir(settings_dirname)

        to_save = self.addons.merge(self._to_dict())

        with open(settings_path, 'w') as settings_file:
            settings_file.write(json.dumps(to_save, indent=True))

        def unix_permissions():
            (uid, gid) = utils.get_current_user_uid_gid()

            os.chmod(settings_path, 0o600)
            os.chown(settings_path, uid, gid)

        # If Linux or Mac, set the right permissions and ownership to the settings file.
        utils.exec_by_platform(unix_permissions, lambda: None, unix_permissions)

    def load_from_dict(self, settings: Dict[str, Any]) -> None:
        """Load settings from a dict.

        Args:
            settings (Dict[str, Any]): A dict containing the settings name as key and its value.

        Returns:
            None
        """
        for name, value in settings.items():
            if hasattr(self, name):
                setattr(self, name, value)

        self.load_settings_addon()  # Manager may be changed with loaded settings, reload addon
        self.addons.load(settings)  # Load values into the addon object

    @staticmethod
    def wipe_from_disk() -> None:
        """Remove settings from the default settings path on disk.

        Returns:
            None
        """
        if os.path.exists(DEFAULT_SETTINGS_PATH):
            os.remove(DEFAULT_SETTINGS_PATH)

    def check(self) -> None:
        """Check if Kathara is correctly working.

        Check if the selected manager is available. Check the presence of Kathara updates.
        Check the correctness and validity of the net_prefix, device_prefix and debug level.

        Returns:
            None

        Raises:
            SettingsError: If the Networks Prefix does not contain only lowercase letters and underscore.
            SettingsError: If the Device Prefix does not contain only lowercase letters and underscore.
            SettingsError: If the Debug Level specified is not allowed.
        """
        self._check_manager()

        current_time = time.time()
        # After 1 week, check if a new Kathara version has been released.
        if current_time - self.last_checked > ONE_WEEK:
            logging.debug("Checking Updates...")
            checked = True

            try:
                logging.debug("Checking Kathara release...")

                latest_remote_release = GitHubApi.get_release_information()
                latest_version = latest_remote_release["tag_name"]

                if version.less_than(version.CURRENT_VERSION, latest_version):
                    logging.info("A new version of Kathara has been released.")
                    logging.info("Current: %s - Latest: %s" % (version.CURRENT_VERSION, latest_version))
                    logging.info("Please update it from https://github.com/KatharaFramework/Kathara")
            except HTTPConnectionError:
                logging.debug("Connection to GitHub failed, passing...")
                checked = False

            if checked:
                self.last_checked = current_time
                self.save_to_disk()

        try:
            utils.re_search_fail(r"^[a-z]+_?[a-z_]+$", self.net_prefix)
        except ValueError:
            raise SettingsError("Networks Prefix must only contain lowercase letters and underscore.")

        try:
            utils.re_search_fail(r"^[a-z]+_?[a-z_]+$", self.device_prefix)
        except ValueError:
            raise SettingsError("Device Prefix must only contain lowercase letters and underscore.")

        if self.debug_level not in AVAILABLE_DEBUG_LEVELS:
            raise SettingsError("Debug Level must be one of the following: %s." % (", ".join(AVAILABLE_DEBUG_LEVELS)))

    def _check_manager(self) -> None:
        """Check if the selected manager is available.

        Returns:
            None

        Raises:
            SettingsError: If the Manager Type is not allowed.
        """
        from ..manager.Kathara import Kathara
        managers = Kathara.get_available_managers_name()

        if self.manager_type not in managers.keys():
            raise SettingsError("Manager Type not allowed.")

    def check_image(self, image: str = None) -> None:
        """Check if the specified image is valid.

        Args:
            image (str): The name of the image to check. If None, check the default image.

        Returns:
            None

        Raises:
            ConnectionError: If the image is not locally available and there is no connection to a remote image repository.
            ImageNotFoundError: If the image is not found.
        """
        image = self.image if not image else image

        # Required to import here because otherwise there is a cyclic dependency
        from ..manager.Kathara import Kathara
        Kathara.get_instance().check_image(image)

    def check_terminal(self, terminal: str = None) -> bool:
        """Check that the selected terminal is available.

        Args:
            terminal (str): The selected terminal path. If None, check the availability of the default terminal.

        Returns:
            bool: True if the selected terminal is TMUX (that do not require path), else False.

        Raises:
            SettingError: If the terminal emulator specified is not found.
        """
        terminal = self.terminal if not terminal else terminal

        # Skip check for TMUX (special value)
        if terminal == "TMUX":
            return True

        def check_unix():
            return os.path.isfile(terminal) and os.access(terminal, os.X_OK)

        def check_osx():
            import appscript
            import aem
            try:
                appscript.app(terminal)
            except aem.findapp.ApplicationNotFoundError:
                return False

            return True

        if not utils.exec_by_platform(check_unix, lambda: True, check_osx):
            raise SettingsError("Terminal Emulator `%s` not valid! Install it before using it." % terminal)

        return True

    def load_settings_addon(self) -> None:
        """Load a setting addon to the base settings.

        Returns:
            None
        """
        self.addons = SettingsAddonFactory().create_instance(class_args=(self.manager_type.capitalize(),))

    def _to_dict(self) -> Dict[str, Any]:
        return {"image": self.image,
                "manager_type": self.manager_type,
                "terminal": self.terminal,
                "open_terminals": self.open_terminals,
                "device_shell": self.device_shell,
                "net_prefix": self.net_prefix,
                "device_prefix": self.device_prefix,
                "debug_level": self.debug_level,
                "print_startup_log": self.print_startup_log,
                "enable_ipv6": self.enable_ipv6,
                "last_checked": self.last_checked
                }


# -*- coding: utf-8 -*-
import random
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import warnings
warnings.filterwarnings('ignore')

import os
import pymorphy2
import re
import chardet

import nltk

nltk.download('popular', quiet=True) # for downloading packages

# uncomment the following only the first time
#nltk.download('punkt') # first-time use only
#nltk.download('wordnet') # first-time use only
morph = pymorphy2.MorphAnalyzer()

def Lemmatizer(text):
    # Detecting codepage, recoding, cleaning, lemmatizing
    codepage = chardet.detect(text.encode())['encoding']
    text = (text.encode()).decode(codepage)
    text = " ".join(word.lower() for word in text.split())  # lowercasing and removing short words
    text = re.sub('\-\s\r\n\s{1,}|\-\s\r\n|\r\n', '', text)  # deleting newlines and line-breaks
    text = re.sub('[.,:;%©?*,!@#$%^&()\d]|[+=]|[[]|[]]|[/]|"|\s{2,}|-', ' ', text)  # deleting symbols
    text = " ".join(morph.parse(str(word))[0].normal_form for word in text.split())
    text = text.encode("utf-8")
    return nltk.word_tokenize(text.decode())

with open('chatbot.txt', 'r', encoding='utf-8', errors='ignore') as fin:
    raw = fin.read()
with open('stopwords.txt', 'r', encoding='utf-8', errors='ignore') as fin:
    stopwords = fin.read()
#TOkenisation
sent_tokens = nltk.sent_tokenize(raw)# converts to list of sentences
word_tokens = nltk.word_tokenize(raw)# converts to list of words
stop_words = nltk.word_tokenize(stopwords)

# Keyword Matching
GREETING_INPUTS = ("здравствуй", "добрый день", "доброе утро", "добрый вечер", "доброго времени суток", "приветствую", "привет", "мое почтение", "приветик", "хей", "здарова", "салют", "Сколько лет, сколько зим!", "рад видеть", "ку")
GREETING_RESPONSES = ["Здравствуй", "Доброго времени суток", "Приветствую", "Мое почтение", "Сколько лет, сколько зим!", "Рад видеть"]

def greeting(sentence):
    for word in sentence.split():
        if word.lower() in GREETING_INPUTS:
            return random.choice(GREETING_RESPONSES)

# Generating response
def response(user_response):
    robo_response = ''
    sent_tokens.append(user_response)
    TfidfVec = TfidfVectorizer(tokenizer=Lemmatizer, stop_words=stop_words)
    tfidf = TfidfVec.fit_transform(sent_tokens)
    vals = cosine_similarity(tfidf[-1], tfidf)
    idx = vals.argsort()[0][-2]
    flat = vals.flatten()
    flat.sort()
    req_tfidf = flat[-2]
    if(req_tfidf == 0):
        robo_response = robo_response + "Хмм...Я пока не готов ответить на этот вопрос, нужно еще поразмыслить. Может сменим тему?"
        return robo_response
    else:
        robo_response = robo_response + sent_tokens[idx]
        return robo_response

flag = True
print("Приветствую. Я философ-бот и постараюсь поддержать ваш разговор.")
while(flag == True):
    user_response = input()
    user_response = user_response.lower()
    if(user_response != 'пока'):
        if(user_response == 'спасибо'):
            flag = False
            print("Приходи еще!")
        else:
            if(greeting(user_response) != None):
                print(greeting(user_response))
            else:
                print(end="")
                print(response(user_response))
                sent_tokens.remove(user_response)
    else:
        flag = False
        print("Бывай! Береги себя..")


from datetime import datetime, timedelta
import random

from fastapi import APIRouter, status, Depends, HTTPException, Form, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

from models import PomodoroResponse, ResponseUser, Satisfaction
from data import DB
import query as q
from utils import get_current_user, get_satisfaction_int

templates = Jinja2Templates(directory="templates")

router = APIRouter(
    prefix='/pomodoros',
    tags=["Pomodoros"]
)

# Pomodoro paths
@router.get(
    path="/",
    response_class=HTMLResponse,
    status_code=status.HTTP_200_OK,
    include_in_schema=False
)
def get_pomodoros_home(request:Request, current_user:ResponseUser = Depends(get_current_user)):
    user_id = current_user['user_id']

    previous_30_days = datetime.today() - timedelta(days=30)
    values = (user_id, previous_30_days)
    df = q.get_pomodoros(values, all=True)

    pomodoros = df.to_dict('records')
    context = {
        'request': request,
        'pomodoros': pomodoros
    }

    return templates.TemplateResponse("general_pages/pomodoros.html", context=context)

@router.post(
    path="/",
    status_code=status.HTTP_201_CREATED,
    summary="Create pomodoro",
    response_class=HTMLResponse
)
def create_pomodoro(
        category_id:int = Form(...),
        project_id:int = Form(...),
        duration:int = Form(...),
        current_user:ResponseUser = Depends(get_current_user)
    ):
    user_id = current_user['user_id']
    pomodoro_date = datetime.today()
    values = (
        category_id, project_id, duration,
        pomodoro_date, user_id
    )
    query = q.create_pomodoro()

    # Execute query
    DB.execute_query(query, values)

    color = "#{:06x}".format(random.randint(0, 0xFFFFFF))

    return f'
Pomodoro created, Start the timer
'

@router.put(
    path="/",
    status_code=status.HTTP_200_OK,
    summary="Measure pomodoro satisfaction",
    response_class=HTMLResponse,
)
def update_pomodoro_satisfaction(request: Request, satisfaction:Satisfaction = Form(...), current_user:ResponseUser = Depends(get_current_user)):
    user_id = current_user['user_id']
    values = (user_id,)
    df = q.get_latest_pomodoro(values)
    pomodoro = df.to_dict('records')[0]

    satisfaction = get_satisfaction_int(satisfaction.value)
    values = (satisfaction, pomodoro['pomodoro_id'], user_id)
    query = q.update_pomodoro_satisfaction()

    # Execute query
    DB.execute_query(query, values)

    return '
Sent
'

@router.get(
    path="/satisfaction",
    status_code=status.HTTP_200_OK,
    response_class=HTMLResponse,
    include_in_schema=False,
)
def get_satisfaction_modal(request:Request, current_user:ResponseUser = Depends(get_current_user)):
    return '

'

@router.get(
    path="/{category_id}/{project_id}",
    response_model=list[PomodoroResponse],
    status_code=status.HTTP_200_OK,
    summary="Get pomodoros in a project"
)
def get_pomodoro(category_id:int, project_id:int, current_user:ResponseUser = Depends(get_current_user)):
    user_id = current_user['user_id']
    values = (user_id, category_id, project_id)

    df = q.get_pomodoros(values)
    if df.empty:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="There are not pomodoros for this project"
        )

    # Satisfaction
    df.pomodoro_satisfaction.fillna("", inplace=True)
    satisfaction = {0: "missing", 1: "good", 2: "bad"}
    df.pomodoro_satisfaction.replace(satisfaction, inplace=True)

    return df.to_dict('records')


#!/usr/bin/env python3
""" Module tests basic/introductory MondoDB features with pymongo """
from typing import Collection, Iterable

def top_students(mongo_collection: Collection) -> Iterable:
    """ Returns all students sorted by average score """
    # Create aggregate() query object from MongoDB collection
    # This is to make the next line shorter and human-readable
    aggr = mongo_collection.aggregate

    # Query database and return results with sorted average score
    return aggr([{'$project': {'_id': '$_id', 'name': '$name',
                               'averageScore': {'$avg': '$topics.score'}}},
                 {'$sort': {'averageScore': -1}}])

if __name__ == '__main__':
    """ Tests the code in this module """
    from pymongo import MongoClient
    list_all = __import__('8-all').list_all
    insert_school = __import__('9-insert_school').insert_school
    with MongoClient() as client:
        students_collection = client.my_db.students
        j_students = [
            {'name': 'John', 'topics': [{'title': 'Algo', 'score': 10.3},
                                        {'title': 'C', 'score': 6.2},
                                        {'title': 'Python', 'score': 12.1}]},
            {'name': 'Bob', 'topics': [{'title': 'Algo', 'score': 5.4},
                                       {'title': 'C', 'score': 4.9},
                                       {'title': 'Python', 'score': 7.9}]},
            {'name': 'Sonia', 'topics': [{'title': 'Algo', 'score': 14.8},
                                         {'title': 'C', 'score': 8.8},
                                         {'title': 'Python', 'score': 15.7}]},
            {'name': 'Amy', 'topics': [{'title': 'Algo', 'score': 9.1},
                                       {'title': 'C', 'score': 14.2},
                                       {'title': 'Python', 'score': 4.8}]},
            {'name': 'Julia', 'topics': [{'title': 'Algo', 'score': 10.5},
                                         {'title': 'C', 'score': 10.2},
                                         {'title': 'Python', 'score': 10.1}]}]
        for j_student in j_students:
            insert_school(students_collection, **j_student)

        students = list_all(students_collection)
        for student in students:
            print(
                '[{}] {} - {}'.format(student.get('_id'), student.get('name'),
                                      student.get('topics')))

        top_students = top_students(students_collection)
        for student in top_students:
            print(
                '[{}] {} => {}'.format(student.get('_id'), student.get('name'),
                                       student.get('averageScore')))


import torch
import numpy as np
from torch.utils.data import Dataset
import os
import random
import cv2
import string

from data import consts, vocabulary

class VSADisentangle(Dataset):
    def __init__(self, mode, shuffle=True):
        if mode not in ("train", "test"):
            raise Exception("invalid dataset mode")
        self.mode = mode
        self.data_root = consts.DATA_ROOT + "/vsa"
        self.flow_root = consts.DATA_ROOT + "/vsa_flow"
        if mode == "train":
            users = consts.VSA_TRAIN_USERS
            group = [50, 35, 1]
        else:
            users = consts.VSA_TEST_USERS
            group = [20, 2, 20]
        user_list = ["user" + str(i) for i in users]

        self.user_list = user_list

        file_list = []
        random_appear_dict = {}
        fixed_appear_dict = {}
        for user in self.user_list:
            fixed_utterances = os.listdir(os.path.join(self.data_root, user, "fixed"))
            samples_0 = group[0] if len(fixed_utterances) >= group[0] else len(fixed_utterances)
            fixed_utterances = random.sample(fixed_utterances, samples_0)
            for fixed_utt in fixed_utterances:
                other_users = list(filter(lambda x: x != user, self.user_list))
                samples_1 = group[1] if len(other_users) >= group[1] else len(other_users)
                other_users = random.sample(other_users, samples_1)
                for other_user in other_users:
                    if fixed_utt not in os.listdir(os.path.join(self.data_root, other_user, "fixed")):
                        continue
                    random_utterances = os.listdir(os.path.join(self.data_root, user, "random"))
                    samples_2 = group[2] if len(random_utterances) >= group[2] else len(random_utterances)
                    random_utterances = random.sample(random_utterances, samples_2)
                    for random_utt in random_utterances:
                        random_path = "/".join([user, "random", random_utt])
                        fixed_path = "/".join([user, "fixed", fixed_utt])
                        item = [fixed_path, random_path, "/".join([other_user, "fixed", fixed_utt])]
                        if random_path not in random_appear_dict:
                            random_appear_dict[random_path] = [item]
                        else:
                            random_appear_dict[random_path].append(item)
                        if fixed_path not in fixed_appear_dict:
                            fixed_appear_dict[fixed_path] = [item]
                        else:
                            fixed_appear_dict[fixed_path].append(item)
        for k, v in random_appear_dict.items():
            sample_num = 1 if len(v) >= 1 else len(v)
            file_list.extend(random.sample(v, sample_num))
        for k, v in fixed_appear_dict.items():
            sample_num = 1 if len(v) >= 1 else len(v)
            file_list.extend(random.sample(v, sample_num))

        if shuffle:
            random.shuffle(file_list)
        self.file_list = file_list

    def get_images_and_token(self, img_path):
        s = img_path.split("/")
        horizon_flip = self.mode == "train" and random.random() > 0.5
        img_sequence_list = []
        for index, img_name in enumerate(sorted(os.listdir(img_path))):
            img = cv2.imread(os.path.join(img_path, img_name))  # (H, W, c)
            img = img / 255.0
            img_tensor = torch.from_numpy(np.transpose(img, (2, 0, 1))).float()
            img_sequence_list.append(img_tensor)
        img_sequence = torch.stack(img_sequence_list[:50], 1)
        flow_data = np.load(os.path.join(self.flow_root, s[-3], s[-2], s[-1] + ".npy"))
        flow_data = flow_data[:49, :, :, :]
        flow_data = np.transpose(flow_data, (1, 0, 2, 3))
        flow_sequence = torch.from_numpy(flow_data)

        if horizon_flip:
            img_sequence = torch.flip(img_sequence, dims=[3])
            flow_sequence = torch.flip(flow_sequence, dims=[3])

        label = s[-1]
        user_id = self.user_list.index(s[-3])
        full = " ".join([consts.NUMBER[n] for n in label])
        token = vocabulary.txt2token(full, consts.VOCAB)
        token.insert(0, consts.BOS)
        token.append(consts.EOS)
        for i in range(consts.TOKEN_MAX_LEN - len(token)):
            token.append(consts.PAD)

        token = torch.tensor(token, dtype=torch.int64)
        return img_sequence, token, user_id, flow_sequence

    def __getitem__(self, index):
        img_files = self.file_list[index]
        return [self.get_images_and_token(os.path.join(self.data_root, f)) for f in img_files]

    def __len__(self):
        return len(self.file_list)


from typing import List, Optional, Tuple

import networkx as nx
import pennylane as qml
from pennylane import numpy as np

def clustered_chain_graph(
    n: int, r: int, k: int, q1: float, q2: float, seed: Optional[int] = None
) -> Tuple[nx.Graph, List[List[int]], List[List[int]]]:
    """
    Function to build clustered chain graph

    Args:
        n (int): number of nodes in each cluster
        r (int): number of clusters
        k (int): number of vertex separators between each cluster pair
        q1 (float): probability of an edge connecting any two nodes in a cluster
        q2 (float): probability of an edge connecting a vertex separator to any node in a cluster
        seed (Optional[int]=None): seed for fixing edge generation

    Returns:
        nx.Graph: clustered chain graph
    """

    if r <= 0 or not isinstance(r, int):
        raise ValueError("Number of clusters must be an integer greater than 0")

    clusters = []
    for i in range(r):
        _seed = seed * i if seed is not None else None
        cluster = nx.erdos_renyi_graph(n, q1, seed=_seed)
        nx.set_node_attributes(cluster, f"cluster_{i}", "subgraph")
        clusters.append(cluster)

    separators = []
    for i in range(r - 1):
        separator = nx.empty_graph(k)
        nx.set_node_attributes(separator, f"separator_{i}", "subgraph")
        separators.append(separator)

    G = nx.disjoint_union_all(clusters + separators)

    cluster_nodes = [
        [n[0] for n in G.nodes(data="subgraph") if n[1] == f"cluster_{i}"] for i in range(r)
    ]
    separator_nodes = [
        [n[0] for n in G.nodes(data="subgraph") if n[1] == f"separator_{i}"] for i in range(r - 1)
    ]

    rng = np.random.default_rng(seed)

    for i, separator in enumerate(separator_nodes):
        for s in separator:
            for c in cluster_nodes[i] + cluster_nodes[i + 1]:
                if rng.random() < q2:
                    G.add_edge(s, c)

    return G, cluster_nodes, separator_nodes

def get_qaoa_circuit(
    G: nx.Graph,
    cluster_nodes: List[List[int]],
    separator_nodes: List[List[int]],
    params: Tuple[Tuple[float]],
    layers: int = 1,
) -> qml.tape.QuantumTape:
    """
    Function to build QAOA max-cut circuit tape from graph including `WireCut`
    operations

    Args:
        G (nx.Graph): problem graph to be solved using QAOA
        cluster_nodes (List[List[int]]): nodes of the clusters within the graph
        separator_nodes (List[List[int]]): nodes of the separators in the graph
        params (Tuple[Tuple[float]]): parameters of the QAOA circuit to be optimized
        layers (int): number of layer in the QAOA circuit

    Returns:
        QuantumTape: the QAOA tape containing `WireCut` operations
    """
    wires = len(G)
    r = len(cluster_nodes)

    cost, _ = qml.qaoa.maxcut(G)

    with qml.tape.QuantumTape() as tape:
        for w in range(wires):
            qml.Hadamard(wires=w)

        for l in range(layers):
            gamma, beta = params[l]

            for i, c in enumerate(cluster_nodes):
                if i == 0:
                    current_separator = []
                    next_separator = separator_nodes[0]
                elif i == r - 1:
                    current_separator = separator_nodes[-1]
                    next_separator = []
                else:
                    current_separator = separator_nodes[i - 1]
                    next_separator = separator_nodes[i]

                for cs in current_separator:
                    qml.WireCut(wires=cs)

                nodes = c + current_separator + next_separator
                subgraph = G.subgraph(nodes)

                for edge in subgraph.edges:
                    qml.IsingZZ(2*gamma, wires=edge) # multiply param by 2 for consistency with analytic cost

            # mixer layer
            for w in range(wires):
                qml.RX(2*beta, wires=w)

            # reset cuts
            if l < layers - 1:
                for s in separator_nodes:
                    qml.WireCut(wires=s)

        [qml.expval(op) for op in cost.ops if not isinstance(op, qml.ops.identity.Identity)]

    return tape


'''
Created on Dec 19, 2017

@author: ghazy
'''

from root.nested.dataAccess.quandl_data_object import QuandlDataObject
from root.nested.interestRateModeling.interest_rates import HeathJarrowMortonModel

import pandas as pd
import statsmodels.tsa.api as smt
import statsmodels.api as sm
import scipy.stats as scs

import matplotlib.pyplot as plt

p=print

def ts_plot(y, lags=None, figsize=(10, 8), style='bmh', title='Time Series Analysis Plots'):
    if not isinstance(y, pd.Series):
        y = pd.Series(y)
    with plt.style.context(style):
        fig = plt.figure(figsize=figsize)
        layout = (3,2)
        ts_ax = plt.subplot2grid(layout, (0,0), colspan=2)
        acf_ax = plt.subplot2grid(layout, (1,0))
        pacf_ax = plt.subplot2grid(layout, (1,1))
        qq_ax = plt.subplot2grid(layout, (2,0))
        pp_ax = plt.subplot2grid(layout, (2,1))
        y.plot(ax=ts_ax)
        ts_ax.set_title(title)
        smt.graphics.plot_acf(y, lags=lags, ax=acf_ax, alpha=0.5)
        smt.graphics.plot_pacf(y, lags=lags, ax=pacf_ax, alpha=0.5)
        sm.qqplot(y, line='s', ax=qq_ax)
        qq_ax.set_title('QQ Plot')
        scs.probplot(y, sparams=(y.mean(), y.std()), plot=pp_ax)
        plt.tight_layout()
        plt.show()
    return

qdo = QuandlDataObject('INTEREST_RATES',
                       'US_TREASURY_YIELD_CURVE',
                       '.csv')

yield_curve_df = qdo.get_df()
yield_curve_df.columns = [c.replace(' ', '_') for c in yield_curve_df.columns]
print(yield_curve_df.info())
#yield_curve_df.dropna(subset=[yield_curve_df.columns[0]], inplace=True) # drop any rows where first column is nan
#yield_curve_df.dropna(inplace=True)
two_yr_y = yield_curve_df['10_YR']
#_ = ts_plot(two_yr_y, lags=30)

hjm = HeathJarrowMortonModel(yield_curve_df)
hjm.get_df_info()
yield_curve_df = hjm.vectorized_create_yield_curve() # the returned Dataframe here contains the yield curve object for each date

# interpolate first with Piecewise Cubic hermite spline funciton, just as the treasury.gov data does.
# this is the first step toward getting the spot curve from the par curve in treasury.gov data
hjm.do_interpolation(interp_method='PCS') # PCS is the default, but just for clarity sake, we pass in the arguement
# once we have the interpolated par yield curve, now we can bootstrap to get spot rates.
hjm.do_bootstrap_spot()
#hjm.do_interpolation(interp_method='NSS')
hjm.do_NSS_params_analysis()
# at this point, we've got the spot rates of the interoplated maturities
# now we can continue with NSS and the forecasting process.
#hjm.calc_fmin_stats()


import paho.mqtt.client as mqtt
import time
import RPi.GPIO as GPIO
import datetime
GPIO.setmode(GPIO.BOARD)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
a=open("sensor.txt","w")
def led1act():
        GPIO.output(18,True)
        time.sleep(0.5)
        a.write(hactual+str("LED 1 ENCENDIDO")+"\n")
        c=("LED 1 ACTIVADO")
        mqtt.publish("mdpilatuna.fie@unach.edu.ec/servidor",str(c))
def led1dest():
        GPIO.output(18,False)
        time.sleep(1)
        a.write(hactual+str("LED 1 PAGADO")+"\n")
        c=("LED 1 APAGADO")
        mqtt.publish("mdpilatuna.fie@unach.edu.ec/servidor",str(c))
def led2act():
        GPIO.output(22,True)
        time.sleep(0.5)
        a.write(hactual+str("LED 2 ENCENDIDO")+"\n")
        c=("LED 2 ACTIVADO")
        mqtt.publish("mdpilatuna.fie@unach.edu.ec/servidor",str(c))
def led2dest():
        GPIO.output(22,False)
        time.sleep(1)
        a.write(hactual+str("LED 2 PAGADO")+"\n")
        c=("LED 2 APAGADO")
        mqtt.publish("mdpilatuna.fie@unach.edu.ec/servidor",str(c))
def on_message(client,obj,msg):
        accion=(msg.payload.decode("utf").split("")[0])
        intclave=(msg.payload.decode("utf-8").split("")[1])
        inttext=(msg.payload.decode("utf-8").split("")[2])
        print(accion)
        if intclave1==inttext1:
            if accion=="led1encendida":
                 led1act()
        print(intclave1)
        print(inttext1)
        if intclave1==inttext1:
            if accion=="led1apagada":
                 led1dest()
        print(intclave1)
        print(inttext1)
        if intclave1==inttext1:
            if accion=="led2encendida":
                 led2act()
        print(intclave1)
        print(inttext1)
        if intclave1==inttext1:
            if accion=="led2apagada":
                 led2dest()
        print(intclave1)
        print(inttext1)
mqttc = mqtt.Client()
mqttc.on_message = on_message
mqttc.username_pw_set("mdpilatuna.fie@unach.edu.ec","quitociudadhermosa")
mqttc.connect("maqiatto.com", 1883)
mqttc.subscribe("mdpilatuna.fie@unach.edu.ec/dispositivo", 0)
rc=0
print("iniciando.....")
i=0
while rc ==0:
        time.sleep(2)
        rc=mqttc.loop()
        i=i+1
        hactual=datetime.datetime.now().strftime("%a,%d,%b,%Y,%H,%S")
        print(hactual)


# # Importing Libraries

# ''' Visualization Libaries '''
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import plotly.express as px
import datetime

''' Data / Other Libraries '''
import numpy as np
import pandas as pd
import os
import math
import json

''' Classes Defined below '''

plot_h, plot_w, plot_m = 432, 920, 0

class PlotlyVisualizations:
    ''' Class containing functions to create plots '''

    def world_map(self, df, x_col, y_col, label_col, color, chart_title):

        map_json = {

            'data': [go.Choropleth(
                locations=df[x_col],
                z=df[y_col],
                text=df[label_col],
                colorscale=color,
                autocolorscale=False,
                reversescale=False,
                marker_line_color='darkgray',
                marker_line_width=0.5,
                colorbar_title="Suicides per 100k people"
            )],

            'layout': go.Layout(
                # title_text=chart_title,
                # margin={'b': 40, 't': 50, 'r': 10},
                width=plot_w,
                height=plot_h,
                margin={'b': 0, 't': 0, 'r': 0, 'l': 0},
                clickmode='event+select',
                geo=dict(
                    showframe=False,
                    showcoastlines=False,
                    landcolor = 'rgb(230, 230, 230)',
                    showland = True,
                    # showcountries = True,
                )
            )
        }

        return map_json

    def boxplot(self, df, category=0, selected_countries={}):
        suicide_rates = df['total_suicide_rate'].to_numpy()
        countries, countries_u = df['country'].to_numpy(), set(df['country'].dropna().unique())

        if category == 0:
            groups = df['Region'].to_numpy()
        else:
            groups = df[category].to_numpy()

        fig = go.Figure()
        if category == 0:
            boxes, selected_countries_data = dict(), []
            for c, g, sr in zip(countries, groups, suicide_rates):
                if g in boxes:
                    boxes[g].append(sr)
                else:
                    boxes[g] = [sr]
                if c in selected_countries:
                    selected_countries_data.append(sr)

            add_selected_countries = False
            if len(selected_countries_data) > 1:
                add_selected_countries = True
                y_selected_countries = np.percentile(selected_countries_data, (5, 25, 25, 50, 75, 75, 95))
                x_selected_countries = ['Selected Countries']*7
        else:
            boxes, boxes_group = dict(), dict()
            for c, g, sr in zip(countries, groups, suicide_rates):
                if g in boxes:
                    boxes[g].append(sr)
                else:
                    boxes[g] = [sr]
                if c in selected_countries:
                    if g in boxes_group:
                        boxes_group[g].append(sr)
                    else:
                        boxes_group[g] = [sr]

        x, y = [], []
        for k, v in boxes.items():
            percentiles = np.percentile(v, (5, 25, 25, 50, 75, 75, 95))
            y.extend(list(map(str, percentiles)))
            x.extend([k]*7)
        if category != 0 and len(selected_countries) > 0:
            x_group, y_group = [], []
            for k, v in boxes_group.items():
                percentiles = np.percentile(v, (5, 25, 25, 50, 75, 75, 95))
                y_group.extend(list(map(str, percentiles)))
                x_group.extend([k]*7)

        # Add traces
        if category == 0:
            if add_selected_countries:
                x = np.concatenate((x, x_selected_countries))
                y = np.concatenate((y, y_selected_countries))
            fig.add_trace(go.Box(
                x = x,
                y = y,
                boxpoints = False,
                marker_color='rgb(178,10,28)'
            ))
        else:
            if len(selected_countries) > 0:
                fig.add_trace(go.Box(
                    x = x,
                    y = y,
                    boxpoints = False,
                    name='All Countries',
                    marker_color='rgb(178,10,28)'
                ))
                fig.add_trace(go.Box(
                    x = x_group,
                    y = y_group,
                    boxpoints = False,
                    name='Selected Countries',
                    marker_color='rgb(245,160,105)'
                ))
            else:
                fig.add_trace(go.Box(
                    x = x,
                    y = y,
                    boxpoints = False,
                    marker_color='rgb(178,10,28)'
                ))

        fig.update_layout(
            yaxis_title='Suicides per 100k people',
            boxmode='group', # group together boxes of the different traces for each value of x
            width=plot_w,
            height=plot_h,
            margin={'b': 0, 't': 0, 'r': 0},
        )
        # fig.update_traces(orientation='v')

        return fig

    def scatter_plot(self, df, x_col, y_col, category_filter, label_col, chart_title, x_label):
        regions_trace_list = []
        for i in df[category_filter].unique():
            df_by_region = df[df[category_filter] == i]
            regions_trace_list.append(go.Scatter(x=df_by_region[x_col],
                                             y=df_by_region[y_col],
                                             text=df_by_region[label_col],
                                            #  mode='markers' if label_col == 'Region' else 'lines+markers',
                                             mode='markers',
                                             opacity=0.7,
                                             marker={
            'size': 10,
            'line': {'width': 1, 'color': 'white'}
        },
            name=i,
            ))

        scatter_json = {
            'data': regions_trace_list,
            'layout': go.Layout(
                # title_text=chart_title,
                xaxis={'title': x_label},
                yaxis={'title': 'Suicides per 100k people'},
                showlegend=True,
                hovermode='closest',
                transition={'duration': 500},
                width=plot_w,
                height=plot_h,
                margin={'t': 0, 'r': 0},
            )
        }

        return scatter_json

    def parallel_coordinates(self, df, color, labels, exclude=[], exclude_cat=[], select_cat=0, chart_title='Parallel Coordinates'):
        countries = df['country'].to_numpy()
        regions, regions_u = df['Region'].to_numpy(), list(df['Region'].dropna().unique())
        ages, ages_u = df['age'].to_numpy(), list(df['age'].dropna().unique())
        genders, genders_u = df['sex'].to_numpy(), list(df['sex'].dropna().unique())
        income_groups, income_groups_u = df['IncomeGroup'].to_numpy(), list(df['IncomeGroup'].dropna().unique())

        unemp = df['total_unemployment_rate'].to_numpy()
        life_expectancy = df['life_expectancy'].to_numpy()
        suicide_rates = df['total_suicide_rate'].to_numpy()

        age_order = ['5-14 years', '15-24 years', '25-34 years', '35-54 years', '55-74 years', '75+ years']
        income_groups_oder = ['High income', 'Upper middle income', 'Lower middle income']
        ages_u = [age for age in age_order if age in ages_u]
        income_groups_u = [income_group for income_group in income_groups_oder if income_group in income_groups_u]
        regions_u.sort()
        genders_u.sort()

        dimensions = []
        try:
            dimensions.append({
                'range': [int(life_expectancy.min()), int(math.ceil(life_expectancy.max()))],
                'label': 'Life expectancy (year)',
                'values': life_expectancy
            })
        except:
            pass
        try:
            if len(genders_u) > 1:
                dimensions.append({
                    'range': [-1.5, len(genders_u)-1+1],
                    'tickvals': list(range(len(genders_u))),
                    'ticktext': genders_u,
                    'label': "Gender",
                    'values': ordered_encoding(genders, genders_u)
                })
        except:
            pass
        try:
            if len(regions_u) > 1:
                dimensions.append({
                    'range': [0, len(regions_u)-1],
                    'tickvals': list(range(len(regions_u))),
                    'ticktext': regions_u,
                    'label': "Region",
                    'values': ordered_encoding(regions, regions_u)
                })
        except:
            pass
        try:
            if len(income_groups_u) > 1:
                dimensions.append({
                    'range': [-1.5, len(income_groups_u)-1+1],
                    'tickvals': list(range(len(income_groups_u))),
                    'ticktext': income_groups_u,
                    'label': "Income group",
                    'values': ordered_encoding(income_groups, income_groups_u)
                })
        except:
            pass
        try:
            dimensions.append({
                'range': [0, int(math.ceil(unemp.max()))],
                'label': 'Unemployment rate (%)',
                'values': unemp
            })
        except:
            pass
        try:
            if len(ages_u) > 1:
                dimensions.append({
                    'range': [0, len(ages_u)-1],
                    'tickvals': list(range(len(ages_u))),
                    'ticktext': ages_u,
                    'label': "Age",
                    'values': ordered_encoding(ages, ages_u)
                })
        except:
            pass
        dimensions.append({
            'range': [suicide_rates.min(), suicide_rates.max()],
            'label': 'Suicides per 100k people',
            'values': suicide_rates
        })

        color_values = suicide_rates.copy()
        if exclude:
            color_values = np.where(np.isin(countries, exclude), color_values, -100)
        if exclude_cat:
            if select_cat == 0:
                if exclude_cat[0] in regions_u:
                    print(0, select_cat, exclude_cat)
                    color_values = np.where(np.isin(regions, exclude_cat), -100, color_values)
                else:
                    print(1, select_cat, exclude_cat)
                    color_values = np.where(np.isin(countries, exclude_cat), -100, color_values)
            elif select_cat == 'age':
                color_values = np.where(np.isin(ages, exclude_cat), -100, color_values)
            elif select_cat == 'sex':
                color_values = np.where(np.isin(genders, exclude_cat), -100, color_values)
            elif select_cat == 'IncomeGroup':
                color_values = np.where(np.isin(income_groups, exclude_cat), -100, color_values)

        cmax = int(math.ceil(suicide_rates.max()))
        cmin = cmax - cmax/0.990
        fig = go.Figure(data=
            go.Parcoords(
                line = dict(
                    color = color_values,
                    colorscale=[
                        [0, 'rgb(230,230,230)'],
                        [0.01, 'rgb(230,230,230)'],
                        [0.01, 'rgb(255, 244, 238)'],
                        [0.2, 'rgb(245,195,157)'],
                        [0.4, 'rgb(245,160,105)'],
                        [1, 'rgb(178,10,28)']
                    ],
                    showscale = True,
                    cmin = cmin,
                    cmax = cmax
                ),
                dimensions = dimensions
            ),
            layout = dict(
                width=plot_w,
                height=plot_h,
                # margin={'b': 10, 't': 10, 'r': 10, 'l': 10}
            )
        )

        return fig

class DataPreperation:
    ''' Class containing functions which do the data preparation related tasks'''

    def __init__(self):
        self.path = os.getcwd()
        self.dataset_path = os.path.join(
            self.path, 'merged_suicide_rates_dataset.csv')
        self.suicide_rates_df = pd.read_csv(self.dataset_path)

    def get_df_complete(self):
        df_complete = self.suicide_rates_df
        df_complete['year'] = df_complete['year'].astype(
            str)
        df_complete['year'] = pd.to_datetime(
            df_complete['year'])
        df_complete['year'] = df_complete['year'].apply(
            lambda x: int(x.year))
        df_complete['life_expectancy'] = df_complete.apply(
            lambda row: row['male_life_expectancy_years'] if row['sex'] == 'male' else row['female_life_expectancy_years'],
            axis=1
        )
        return df_complete

    def get_unique_values(self, df):
        columns = ['year', 'country', 'country_code', 'Region', 'sex', 'age', 'IncomeGroup']
        return {col: df[col].dropna().unique() for col in columns}

    def agg_data_scatter_plot(self, df_input, select_y_axis, select_x_axis, select_category, country_list):
        df = df_input.copy()
        if country_list is None or country_list == []:
            if select_x_axis != 'year':
                if select_category != 0:
                    df = df.groupby(['Region', select_category]).agg(
                        {select_y_axis: sum, select_x_axis: 'mean', 'population': sum}).reset_index()
                else:
                    df = df.groupby(['Region']).agg(
                        {select_y_axis: sum, select_x_axis: 'mean', 'population': sum}).reset_index()
                df['total_suicide_rate'] = df['suicides_no'] / df['population'] * 100000
            else:
                if select_category != 0:
                    df = df.groupby(['Region', select_x_axis, select_category]).agg(
                        {select_y_axis: sum, 'population': sum}).reset_index()
                else:
                    df = df.groupby(['Region', select_x_axis]).agg(
                        {select_y_axis: sum, 'population': sum}).reset_index()
                df['total_suicide_rate'] = df['suicides_no'] / df['population'] * 100000
        else:
            df = df[df['country'].isin(country_list)]
            if select_x_axis != 'year':
                if select_category != 0:
                    df = df.groupby(['country', 'country_code', select_category]).agg(
                        {select_y_axis: sum, select_x_axis: 'mean', 'population': sum}).reset_index()
                else:
                    df = df.groupby(['country', 'country_code']).agg(
                        {select_y_axis: sum, select_x_axis: 'mean', 'population': sum}).reset_index()
                df['total_suicide_rate'] = df['suicides_no'] / df['population'] * 100000
            else:
                if select_category != 0:
                    df = df.groupby(['country', 'country_code', select_x_axis, select_category]).agg(
                        {select_y_axis: sum, 'population': sum}).reset_index()
                else:
                    df = df.groupby(['country', 'country_code', select_x_axis]).agg(
                        {select_y_axis: sum, 'population': sum}).reset_index()
            df['total_suicide_rate'] = df['suicides_no'] / df['population'] * 100000
        return df

def agg_data_world_map(df_input):
    df = df_input.copy()
    df = df.groupby(['country', 'country_code']).agg({'suicides_no': sum, 'population': sum}).reset_index() #'Region', 'IncomeGroup'
    df['total_suicide_rate'] = df['suicides_no'] / df['population'] * 100000

    return df

def agg_data_parallel_coordinates(df_input):
    df = df_input.copy()
    df = df.dropna(
        subset=['age', 'sex', 'Region', 'country', 'country_code', 'suicides_no', 'population', 'life_expectancy', 'gdp_per_capita ($)', 'total_unemployment_rate'],
        how='any',
        axis=0
    )
    df = df.groupby(
        ['age', 'sex', 'Region', 'country', 'country_code', 'IncomeGroup']
    ).agg({
        'suicides_no': sum,
        'population': sum,
        'life_expectancy': 'first',
        'gdp_per_capita ($)': 'first',
        'total_unemployment_rate': 'first'
    }).reset_index()
    df['total_suicide_rate'] = df['suicides_no'] / df['population'] * 100000

    return df

def agg_data_boxplot(df_input, select_x_axis='Region', country_code=None):
    df = df_input.copy()
    df['total_suicide_rate'] = df['suicides_no'] / df['population'] * 100000

    df = df[['country', select_x_axis, 'sex', 'age', 'IncomeGroup', 'total_suicide_rate']]

    return df

def ordered_encoding(array, order):
    y = list(order)
    return [y.index(x) for x in array]

# Create class objects
plotly_obj = PlotlyVisualizations()
data = DataPreperation()

# Call class functions
start_year = 1991
end_year = 2016
df_complete = data.get_df_complete()
unique_values = data.get_unique_values(df_complete)
label_dict = {'year':'Year', 'gdp_per_capita ($)': 'GDP per capita ($)', 'HDI for year':'Human Development Index', 'female_to_male_labor_rate':'Female to male labor rate', 'male_life_expectancy_years':'Male life expectancy', 'female_life_expectancy_years':'Female life expectancy', 'total_unemployment_rate':'Unemployment Rate (%)'}

''' Layout Code '''
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(
    [
        html.Div(
            [
                html.Div(
                    [
                        html.Div(
                            [
                                html.Div(
                                    [
                                        html.H1(
                                            children='Suicide Analysis',
                                            style={
                                                'fontSize': 32,
                                                'textAlign': 'center'
                                            }
                                        )
                                    ],
                                )
                            ],
                            className='row'
                        ),
                        html.Div(
                            [
                                html.Div(
                                    [
                                        dcc.Dropdown(
                                            id='select_x-axis',
                                            options=[
                                                {'label':'Year', 'value':'year'},
                                                {'label':'GDP per capita ($)', 'value':'gdp_per_capita ($)'},
                                                {'label':'Human Development Index', 'value':'HDI for year'},
                                                {'label':'Female to male labor rate', 'value':'female_to_male_labor_rate'},
                                                {'label':'Male life expectancy (y)', 'value':'male_life_expectancy_years'},
                                                {'label':'Female life expetancy (y)', 'value':'female_life_expectancy_years'},
                                                {'label':'Unemployment Rate (%)', 'value':'total_unemployment_rate'}
                                            ],
                                            value='year',
                                            multi=False,
                                            style={'fontSize': 12}
                                        )
                                    ],
                                    className='six columns',
                                ),
                                html.Div(
                                    [
                                        dcc.Dropdown(
                                            id='select_category',
                                            options=[
                                                {'label': 'Region', 'value': 0, 'disabled': False},
                                                {'label': 'Age', 'value': 'age', 'disabled': False},
                                                {'label': 'Gender', 'value': 'sex', 'disabled': False},
                                                {'label': 'Income Group', 'value': 'IncomeGroup', 'disabled': False}],
                                            value=0,
                                            multi=False,
                                            style={'fontSize':12}
                                        )
                                    ],
                                    className='six columns',
                                    id='dropdown-div'

                                )
                            ],
                            className='row'
                        ),
                    ],
                    className='three columns'
                ),

                html.Div(
                    [
                        html.Div(
                            [
                                html.P(
                                    'Region',
                                ),
                                dcc.Dropdown(
                                    id='filter_region',
                                    options=[
                                        {'label': option, 'value': option} for option in unique_values['Region']
                                    ],
                                    value=unique_values['Region'],
                                    multi=True,
                                    placeholder="Select a region",
                                    style={'fontSize': 12}
                                )
                            ],
                            # style={'border':'2px black solid'},
                            className='five columns'
                        ),
                        html.Div(
                            [
                                html.P(
                                    'Income Group',
                                ),
                                dcc.Dropdown(
                                    id='filter_ig',
                                    options=[
                                        {'label': option, 'value': option} for option in unique_values['IncomeGroup']
                                    ],
                                    value=unique_values['IncomeGroup'],
                                    multi=True,
                                    placeholder="Select an income group",
                                    style={'fontSize': 12}
                                )
                            ],
                            # style={'border':'2px black solid'},
                            className='three columns'
                        ),
                        html.Div(
                            [
                                html.P(
                                    'Age Group',
                                ),
                                dcc.Dropdown(
                                    id='filter_age',
                                    options=[
                                        {'label': option, 'value': option} for option in unique_values['age']
                                    ],
                                    value=unique_values['age'],
                                    multi=True,
                                    placeholder="Select an age group",
                                    style={'fontSize': 12}
                                )
                            ],
                            className='three columns'
                        ),
                        html.Div(
                            [
                                html.P(
                                    'Gender',
                                ),
                                dcc.Checklist(
                                    id='filter_gender',
                                    options=[
                                        {'label': option.title(), 'value': option} for option in unique_values['sex']
                                    ],
                                    value=unique_values['sex'],
                                    style={'fontSize': 12}
                                )
                            ],
                            className='one column'
                        ),
                    ],
                    className='nine columns'
                ),
            ],
            className='row'
        ),
        html.Div(
            [
                html.Div(dcc.RangeSlider(
                       id='range-slider',
                       min=start_year,
                       max=end_year,
                       value=[start_year, end_year],
                       marks={str(year): str(
                           year) for year in unique_values['year']},
                       ),
                       style={'width': '1500px',
                              'text-align': 'center', 'margin': 'auto'},
                       )
            ],
            style={'marginTop': '2px'},
            className='row'
        ),

        html.Div(
            [
                html.Div(
                    dcc.Graph(
                        id='world-map',
                        clickData=None,
                    ),
                    style={
                        'width': plot_w,
                        'margin-left': 960 - plot_w - int(plot_m//2),
                        'margin-right': plot_m,
                        'display': 'inline-block',
                        # 'border': '2px black solid'
                    }
                ),
                html.Div(
                    dcc.Graph(
                        id='parallel-coordinates',
                        figure=plotly_obj.parallel_coordinates(
                            df=agg_data_parallel_coordinates(df_complete),
                            color='total_suicide_rate',
                            labels={
                                'age': 'Age',
                                'sex': 'Gender',
                                'Region': 'Region',
                                'Population': 'Population',
                                'gdp_per_capita ($)': 'GDP per Capital ($)',
                                'total_suicide_rate': 'Suicides per 100k people'
                            }
                        )
                    ),
                    style={
                        'width': plot_w,
                        'display': 'inline-block',
                        # 'border': '2px black solid'
                    }
                )
            ],
            style={
                'display': 'inline-block'
            },
            className='row'
        ),
        html.Div(
            [
                html.Div(
                    dcc.Graph(
                        id='scatter-plot'
                    ),
                    style={
                        'width': plot_w,
                        'margin-left': 960 - plot_w - int(plot_m//2),
                        'margin-right': plot_m,
                        'display': 'inline-block',
                        # 'border': '2px black solid'
                    }
                ),
                html.Div(
                    dcc.Graph(
                        id='box-plot',
                        figure=plotly_obj.boxplot(
                            df=agg_data_boxplot(df_complete)
                        )
                    ),
                    style={
                        'width': plot_w,
                        'display': 'inline-block',
                        # 'border': '2px black solid'
                    }
                )
            ],
            style={
                'display': 'inline-block'
            },
            className='row'
        ),
        html.Div(id='intermediate-value', style={'display': 'none'})
    ],
)

@app.callback(
    Output('intermediate-value', 'children'),
    [
        dash.dependencies.Input('filter_region', 'value'),
        dash.dependencies.Input('filter_ig', 'value'),
        dash.dependencies.Input('filter_age', 'value'),
        dash.dependencies.Input('filter_gender', 'value'),
        dash.dependencies.Input('range-slider', 'value')
    ]
)
def clean_data(filter_region, filter_ig, filter_age, filter_gender, range_slider):
    df = df_complete.copy()
    df = df[
        (df['year'] >= range_slider[0]) &
        (df['year'] <= range_slider[1]) &
        (df['Region'].isin(filter_region)) &
        (df['IncomeGroup'].isin(filter_ig)) &
        (df['age'].isin(filter_age)) &
        (df['sex'].isin(filter_gender))
    ]

    return df.to_json(orient='split')

@app.callback(
    Output('dropdown-div', 'children'),
    [
        Input('select_x-axis', 'value'),
    ])
def update_type(value):
    if value != 'year':
        return dcc.Dropdown(
            id='select_category',
            options=[
                {'label': 'Region', 'value': 0, 'disabled': False},
                {'label': 'Age', 'value': 'age', 'disabled': True},
                {'label': 'Gender', 'value': 'sex', 'disabled': True},
                {'label': 'Income Group', 'value': 'IncomeGroup', 'disabled': True}],
            value=0,
            multi=False,
            style={'fontSize': 12}
        )
    else:
        return dcc.Dropdown(
            id='select_category',
            options=[
                {'label': 'Region', 'value': 0, 'disabled': False},
                {'label': 'Age', 'value': 'age', 'disabled': False},
                {'label': 'Gender', 'value': 'sex', 'disabled': False},
                {'label': 'Income Group', 'value': 'IncomeGroup', 'disabled': False}],
            value=0,
            multi=False,
            style={'fontSize': 12}
        )

@app.callback(
    dash.dependencies.Output('world-map', 'figure'),
    [
        dash.dependencies.Input('intermediate-value', 'children'),
        dash.dependencies.Input('range-slider', 'value')
    ]
)
def update_world_map(json, range_slider):
    df = pd.read_json(json, orient='split')
    df = agg_data_world_map(df)
    chart_title = f"Suicide Rate per 100k people ({range_slider[0]} - {range_slider[1]})"

    return plotly_obj.world_map(
        df, 'country_code', 'total_suicide_rate', 'country',
        [[0, 'rgb(255, 244, 238)'], [0.2, 'rgb(245,195,157)'], [0.4, 'rgb(245,160,105)'], [1, 'rgb(178,10,28)']],
        chart_title)

#
@app.callback(
    dash.dependencies.Output('scatter-plot', 'figure'),
    [
        dash.dependencies.Input('intermediate-value', 'children'),
        dash.dependencies.Input('select_x-axis', 'value'),
        dash.dependencies.Input('range-slider', 'value'),
        dash.dependencies.Input('select_category', 'value'),
        dash.dependencies.Input('world-map', 'clickData'),
        dash.dependencies.Input('world-map', 'selectedData'),
    ]
)
def update_scatter_plot(json, select_x_axis, range_slider, select_category, clickData, selectedData):
    country_list = []
    if selectedData != None:
        for i in range(len(selectedData['points'])):
            country_list.append(selectedData['points'][i]['text'])
        df = pd.read_json(json, orient='split')
        df = data.agg_data_scatter_plot(df, 'suicides_no', select_x_axis, select_category, country_list)
        chart_title = f"Suicide Rate per 100k people ({range_slider[0]} - {range_slider[1]})"
        if select_category == 0:
            select_category = 'country'
        if select_x_axis in label_dict:
            label = label_dict[select_x_axis]
        return plotly_obj.scatter_plot(df, select_x_axis, 'total_suicide_rate', select_category, 'country', chart_title, label)
    else:
        df = pd.read_json(json, orient='split')
        df = data.agg_data_scatter_plot(df, 'suicides_no', select_x_axis, select_category, None)
        chart_title = f"Suicide Rate per 100k people ({range_slider[0]} - {range_slider[1]})"
        if select_category == 0:
            select_category = 'Region'
        if select_x_axis in label_dict:
            label = label_dict[select_x_axis]
        return plotly_obj.scatter_plot(df, select_x_axis, 'total_suicide_rate', select_category, 'Region', chart_title, label)

@app.callback(
    dash.dependencies.Output('parallel-coordinates', 'figure'),
    [
        dash.dependencies.Input('intermediate-value', 'children'),
        dash.dependencies.Input('world-map', 'clickData'),
        dash.dependencies.Input('world-map', 'selectedData'),
        dash.dependencies.Input('scatter-plot', 'clickData'),
        dash.dependencies.Input('scatter-plot', 'selectedData'),
        dash.dependencies.Input('scatter-plot', 'restyleData'),
        dash.dependencies.Input('scatter-plot', 'figure'),
        dash.dependencies.Input('select_category', 'value')
    ]
)
def update_parallel_coordinates(json, clickData, selectedData, clickData_scat, selectedData_scat, extendData_scat, config_scat, select_category):
    df = pd.read_json(json, orient='split')
    country_list = []
    if selectedData != None:
        for i in range(len(selectedData['points'])):
            country_list.append(selectedData['points'][i]['text'])

    scat_data, exclude_cat = config_scat['data'], []
    for cat in scat_data:
        if 'visible' in cat and cat['visible'] == 'legendonly':
            exclude_cat.append(cat['name'])

    return plotly_obj.parallel_coordinates(
        df=agg_data_parallel_coordinates(df),
        color='total_suicide_rate',
        labels={
            'age': 'Age',
            'sex': 'Gender',
            'Region': 'Region',
            'Population': 'Population',
            'gdp_per_capita ($)': 'GDP per Capital ($)',
            'total_suicide_rate': 'Suicides per 100k people',
        },
        exclude=country_list,
        exclude_cat=exclude_cat,
        select_cat=select_category
    )

@app.callback(
    dash.dependencies.Output('box-plot', 'figure'),
    [
        dash.dependencies.Input('intermediate-value', 'children'),
        dash.dependencies.Input('select_category', 'value'),
        dash.dependencies.Input('world-map', 'clickData'),
        dash.dependencies.Input('world-map', 'selectedData')
    ]
)
def update_boxplot(json, category, clickData, selectedData):
    df = pd.read_json(json, orient='split')
    country_list = []
    if selectedData != None:
        for i in range(len(selectedData['points'])):
            country_list.append(selectedData['points'][i]['text'])

    return plotly_obj.boxplot(
        df=agg_data_boxplot(df),
        category=category,
        selected_countries=set(country_list)
    )

if __name__ == '__main__':
    app.run_server(debug=False)


import cv2
import numpy as np

from skill_scraping.image_matcher import ImageMatcher

class SkillClassifier:
    def __init__(self, images):
        self.image_locator = ImageMatcher()

        self.spells = {}
        self.point_history = []

        for filename, image in images.items():
            preprocessed = self.image_locator.preprocess(image)
            kp, desc = self.image_locator.kp_desc(preprocessed)

            self.spells[filename] = {
                "img_org": cv2.resize(image, (64, 64)),
                "img_pre": preprocessed,
                "kp": kp,
                "desc": desc,
                "hist": self.calc_hist(cv2.cvtColor(image, cv2.COLOR_BGR2HSV))
            }

    def calc_hist(self, img1_hsv):
        hist_img1 = cv2.calcHist([img1_hsv], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
        hist_img1 = cv2.normalize(hist_img1, None, norm_type=cv2.NORM_MINMAX).flatten()

        return hist_img1

    def comp_hist(self, h1, h2):
        return cv2.compareHist(h1, h2, cv2.HISTCMP_CORREL)

    def template_match(self, i1, i2):
        res = cv2.matchTemplate(i1, i2, cv2.TM_CCOEFF_NORMED)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

        return max_val

    def get_spell_name(self, skill_img):
        i1 = cv2.resize(skill_img, (64, 64))

        img1_hsv = cv2.cvtColor(skill_img, cv2.COLOR_BGR2HSV)
        h, s, v = cv2.split(img1_hsv)

        mean_s = np.mean(s)
        mean_v = np.mean(v)

        mean_t = (mean_s / 80 + mean_v / 60) / 2

        if mean_v < 25 or mean_t < 0.95:
            return None

        hist = self.calc_hist(img1_hsv)

        result_hist = {k: self.comp_hist(v["hist"], hist) for k, v in self.spells.items()}
        result_hist = {k: v for k, v in sorted(result_hist.items(), reverse=True, key=lambda item: item[1])}
        result_hist = dict(list(result_hist.items())[:100])

        results_match = {k: self.template_match(v["img_org"], i1) for k, v in self.spells.items() if k in result_hist}

        results_comb = {k: (v + result_hist[k] * 0.75) / 2 for k, v in results_match.items()}

        results = {k: v for k, v in sorted(results_comb.items(), reverse=True, key=lambda item: item[1])}

        spellname, score = list(results.items())[0]

        if score < 0.4:
            return None

        return spellname

    def find_spells(self, top_img):
        found_spells = []

        preprocessed_top = self.image_locator.preprocess(top_img)
        kp_top, desc_top = self.image_locator.kp_desc(preprocessed_top)

        for spell, data in self.spells.items():
            matches = self.image_locator.match_descriptors(data["desc"], desc_top)
            good = self.image_locator.good_matches(matches, 0.5)

            if len(good) > 4:
                points = self.image_locator.get_points(data["img_pre"], data["kp"], kp_top, good)

                if points is not None:
                    found_spells.append(spell)
                    self.point_history.append(points)

        return found_spells

    def get_skill_image(self, top_img, avg):
        pts2 = np.float32([[0, 0], [0, 64 - 1], [64 - 1, 64 - 1], [64 - 1, 0]]).reshape(-1, 1, 2)
        M = cv2.getPerspectiveTransform(avg.astype(np.float32), pts2)
        return cv2.warpPerspective(top_img, M, (64, 64))

    def get_skill_images(self, top_img, averages):
        skill_images = []

        for avg in averages:
            skill_images.append(self.get_skill_image(top_img, avg))

        return skill_images


from ctypes import c_size_t
from dataclasses import dataclass
from typing import ClassVar
from pytest import fixture
from puft.core.ie.ie import Ie
from puft.core.test.mock import Mock

@dataclass
class CustomIe(Ie):
    name: str
    age: int
    tags: set[str]

@dataclass
class CustomIeMock(Mock):
    name: str
    age: int
    tags: set[str]

@fixture
def custom_ie_mock() -> CustomIeMock:
    return CustomIeMock(name='Test', age=12, tags={'hero', 'reckless'})

@fixture
def custom_ie(custom_ie_mock: CustomIeMock) -> CustomIe:
    return CustomIe(
        name=custom_ie_mock.name,
        age=custom_ie_mock.age,
        tags=custom_ie_mock.tags)

class TestIe():
    def test_custom(self, custom_ie: CustomIe, custom_ie_mock: CustomIeMock):
        expected_json: dict = {
            'custom': {
                'name': custom_ie_mock.name,
                'age': custom_ie_mock.age,
                'tags': custom_ie_mock.tags
            }
        }

        assert custom_ie.name == custom_ie_mock.name
        assert custom_ie.age == custom_ie_mock.age
        assert custom_ie.tags == custom_ie_mock.tags

        assert custom_ie.formatted_name == 'custom'
        assert custom_ie.get_json() == expected_json

        # Validate self through class method
        custom_ie.validate(custom_ie.get_json())

        json_custom_name: dict = custom_ie.get_json('testname')
        assert 'testname' in json_custom_name