"""Tests for dataset functions."""

import unittest

from ..data_processing.dataset import load_dataset
from ..critical_success_factors import ALL_CSFS
from ..success_metrics import ALL_SUCCESS_METRICS
from ..utils import extract_csf_values, extract_success_values

class TestDataset(unittest.TestCase):
    """Tests for dataset functions."""

    def test_dataset_load(self):
        """Test loading the dataset."""
        data = load_dataset()

        # There should be 101 projects in the dataset
        self.assertEqual(len(data), 101)

        # Each project should have 42 columns
        len_success_metrics = len(data[0][1])
        len_critical_success_factors = len(data[0][0])
        total_columns = len_critical_success_factors + len_success_metrics

        expected_columns = len(ALL_CSFS) + len(ALL_SUCCESS_METRICS)

        self.assertEqual(total_columns, expected_columns)

    def test_dataset_format(self):
        """Test the format of the output of the dataset."""
        data = load_dataset()

        critical_success_factors, success_metrics = data[0]

        extracted_csf_values = extract_csf_values(critical_success_factors)
        extracted_sm_values = extract_success_values(success_metrics)

        # These are ordered by the order of the CSFs and SuccessMetrics
        actual_csf_values = [4, 1, 5, 5, 5, 5]  # OF
        actual_csf_values += [5, 5, 5, 5, 5, 5, 4]  # TF
        actual_csf_values += [3, 3, 3, 4]  # CF
        actual_csf_values += [5, 5, 4, 5, 3, 5, 5]  # PF

        actual_sm_values = [5, 4, 5, 5]  # Process
        actual_sm_values += [5, 5, 5, 5, 5, 5, 5, 5, 5, 2]  # Product
        actual_sm_values += [5, 5, 5]  # Stakeholder
        actual_sm_values += [4.76]  # Overall

        self.assertEqual(extracted_csf_values, actual_csf_values)
        self.assertEqual(extracted_sm_values, actual_sm_values)

if __name__ == '__main__':
    unittest.main()


import sqlalchemy as sa
from sqlalchemy import BOOLEAN, FunctionElement, TypeDecorator
from sqlalchemy.dialects import postgresql, sqlite
from sqlalchemy.ext.compiler import compiles

from ..util import json_dumps

class json_contains(FunctionElement):
    """
    Platform independent json_contains operator, tests if the
    `left` expression contains the `right` expression.

    On postgres this is equivalent to the @> containment operator.
    https://www.postgresql.org/docs/current/functions-json.html
    """

    type = BOOLEAN
    name = "json_contains"
    inherit_cache = False

    def __init__(self, left, right):
        self.left = left
        self.right = right
        super().__init__()

@compiles(json_contains, "postgresql")
def _json_contains_postgresql(element, compiler, **kwargs):
    return compiler.process(
        sa.type_coerce(element.left, postgresql.JSONB).contains(
            sa.type_coerce(element.right, postgresql.JSONB)
        ),
        **kwargs,
    )

def _json_contains_sqlite_fn(left, right, compiler, **kwargs):
    if isinstance(left, (list, dict, tuple, str)):
        left = json_dumps(left)

    if isinstance(right, (list, dict, tuple, str)):
        right = json_dumps(right)

    json_each_left = sa.func.json_each(left).alias("left")
    json_each_right = sa.func.json_each(right).alias("right")

    distinct_matches = (
        sa.select(sa.func.count(sa.distinct(sa.literal_column("left.value"))))
        .select_from(json_each_left)
        .join(
            json_each_right,
            sa.literal_column("left.value") == sa.literal_column("right.value"),
        )
        .scalar_subquery()
    )

    distinct_keys = (
        sa.select(sa.func.count(sa.distinct(sa.literal_column("right.value"))))
        .select_from(json_each_right)
        .scalar_subquery()
    )

    return compiler.process(distinct_matches >= distinct_keys)

@compiles(json_contains, "sqlite")
def _json_contains_sqlite(element, compiler, **kwargs):
    return _json_contains_sqlite_fn(element.left, element.right, compiler, **kwargs)

class json_has_any_key(FunctionElement):
    """
    Platform independent json_has_any_key operator.

    On postgres this is equivalent to the ?| existence operator.
    https://www.postgresql.org/docs/current/functions-json.html
    """

    type = BOOLEAN  # type: ignore
    name = "json_has_any_key"
    inherit_cache = False

    def __init__(self, json_expr, values: list):
        self.json_expr = json_expr
        if not all(isinstance(v, str) for v in values):
            raise ValueError("json_has_any_key values must be strings")
        self.values = values
        super().__init__()

@compiles(json_has_any_key, "postgresql")
@compiles(json_has_any_key)
def _json_has_any_key_postgresql(element, compiler, **kwargs):

    values_array = postgresql.array(element.values)
    # if the array is empty, postgres requires a type annotation
    if not element.values:
        values_array = sa.cast(values_array, postgresql.ARRAY(sa.String))

    return compiler.process(
        sa.type_coerce(element.json_expr, postgresql.JSONB).has_any(values_array),
        **kwargs,
    )

@compiles(json_has_any_key, "sqlite")
def _json_has_any_key_sqlite(element, compiler, **kwargs):
    json_each = sa.func.json_each(element.json_expr).alias("json_each")
    return compiler.process(
        sa.select(1)
        .select_from(json_each)
        .where(
            sa.literal_column("json_each.value").in_(
                sa.bindparam(key="json_each_values", value=element.values, unique=True)
            )
        )
        .exists(),
        **kwargs,
    )

class json_has_all_keys(FunctionElement):
    """Platform independent json_has_all_keys operator.

    On postgres this is equivalent to the ?& existence operator.
    https://www.postgresql.org/docs/current/functions-json.html
    """

    type = BOOLEAN  # type: ignore
    name = "json_has_all_keys"
    inherit_cache = False

    def __init__(self, json_expr, values: list):
        self.json_expr = json_expr
        if isinstance(values, list) and not all(isinstance(v, str) for v in values):
            raise ValueError(
                "json_has_all_key values must be strings if provided as a literal list"
            )
        self.values = values
        super().__init__()

@compiles(json_has_all_keys, "postgresql")
@compiles(json_has_all_keys)
def _json_has_all_keys_postgresql(element, compiler, **kwargs):
    values_array = postgresql.array(element.values)

    # if the array is empty, postgres requires a type annotation
    if not element.values:
        values_array = sa.cast(values_array, postgresql.ARRAY(sa.String))

    return compiler.process(
        sa.type_coerce(element.json_expr, postgresql.JSONB).has_all(values_array),
        **kwargs,
    )

@compiles(json_has_all_keys, "sqlite")
def _json_has_all_keys_sqlite(element, compiler, **kwargs):
    # "has all keys" is equivalent to "json contains"
    return _json_contains_sqlite_fn(
        left=element.json_expr,
        right=element.values,
        compiler=compiler,
        **kwargs,
    )

class JSON(TypeDecorator):
    """
    JSON type that returns SQLAlchemy's dialect-specific JSON types, where
    possible. Uses generic JSON otherwise.

    The "base" type is postgresql.JSONB to expose useful methods prior
    to SQL compilation
    """

    impl = postgresql.JSONB
    cache_ok = True

    def load_dialect_impl(self, dialect):
        if dialect.name == "postgresql":
            return dialect.type_descriptor(postgresql.JSONB(none_as_null=True))
        elif dialect.name == "sqlite":
            return dialect.type_descriptor(sqlite.JSON(none_as_null=True))
        else:
            return dialect.type_descriptor(sa.JSON(none_as_null=True))

    class comparator_factory(sa.JSON.Comparator):
        def contains(self, other, **kw):
            json_contains(self, other)

        def has_any_key(self, other):
            return json_has_any_key(self, other)

        def has_all_keys(self, other):
            return json_has_all_keys(self, other)


from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.core.paginator import Paginator
from django.db.models import Q

from .forms import *
from .models import *

from constel.apps.controle_acessos.decorator import permission
from ...menu import menu_principal, menu_cadastros, menu_entradas, menu_saidas, menu_consultas, menu_consultas_modelos

from constel.forms import FormFiltraQ

@login_required
@permission('patrimonio', )
def cadastra_ferramenta(request):
    menu = menu_cadastros(request)

    if request.method == 'POST':
        form = FormCadastraFerramenta(request.POST)

        if form.is_valid():
            ferramenta = Ferramenta(
                nome=form.cleaned_data['nome'],
                descricao=form.cleaned_data['descricao'],
                user=request.user,
            )
            ferramenta.save()
            FerramentaQuantidade(
                ferramenta=ferramenta,
                quantidade=0,
            ).save()

            return HttpResponseRedirect('/patrimonio/cadastros/ferramenta')
    else:
        form = FormCadastraFerramenta()

    itens = Ferramenta.objects.values(
        'nome',
        'data',
        'user__first_name',
        'user__last_name',
    ).order_by('-data')

    paginator = Paginator(itens, 50)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)

    context = {
        'page_obj': page_obj,
        'form': form,
        'form_submit_text': 'Cadastrar nova ferramenta',
    }
    context.update(menu)

    return render(request, 'ferramenta/v2/cadastra_ferramenta.html', context)

@login_required
@permission('patrimonio', )
def entrada_ferramenta(request):
    menu = menu_entradas(request)

    if request.method == 'POST':
        form = FormEntradaFerramenta(request.POST)

        if form.is_valid():
            FerramentaEntrada(
                ferramenta=form.cleaned_data['ferramenta'],
                quantidade=form.cleaned_data['quantidade'],
                valor=form.cleaned_data['valor'],
                observacao=form.cleaned_data['observacao'],
                user=request.user,
            ).save()
            ferramenta = form.cleaned_data['ferramenta']
            ferramenta.quantidade.quantidade += form.cleaned_data['quantidade']
            ferramenta.quantidade.save()

            return HttpResponseRedirect('/patrimonio/entradas/ferramenta')

    else:
        form = FormEntradaFerramenta()

    context = {
        'form': form,
        'form_submit_text': 'Registrar entrada',
    }
    context.update(menu)

    return render(request, 'patrimonio/v2/entrada.html', context)

@login_required
@permission('patrimonio', )
def saida_ferramenta(request):
    menu = menu_saidas(request)

    if request.method == 'POST':
        form = FormSaidaFerramenta(request.POST)

        if form.is_valid():
            saida = FerramentaSaida(
                ferramenta=form.cleaned_data['ferramenta'],
                quantidade=form.cleaned_data['quantidade'],
                observacao=form.cleaned_data['observacao'],
                user_to=form.cleaned_data['user_to'],
                user=request.user,
            )
            ferramenta = form.cleaned_data['ferramenta']
            ferramenta.quantidade.quantidade -= form.cleaned_data['quantidade']

            if FerramentaQuantidadeFuncionario.objects.filter(
                user=form.cleaned_data['user_to'],
                ferramenta=ferramenta
            ).exists():

                carga = FerramentaQuantidadeFuncionario.objects.get(
                    user=form.cleaned_data['user_to'],
                    ferramenta=ferramenta
                )
                carga.quantidade += form.cleaned_data['quantidade']

            else:
                carga = FerramentaQuantidadeFuncionario(
                    user=form.cleaned_data['user_to'],
                    ferramenta=ferramenta,
                    quantidade=form.cleaned_data['quantidade']
                )

            ferramenta.quantidade.save()
            saida.save()
            carga.save()

            return HttpResponseRedirect('/patrimonio/saidas/ferramenta')

    else:
        form = FormSaidaFerramenta()

    context = {
        'form': form,
        'form_submit_text': 'Registrar entrega',
    }
    context.update(menu)

    return render(request, 'patrimonio/v2/entrada.html', context)

@login_required
@permission('patrimonio', )
def fechamento_ferramenta(request):
    menu = menu_principal(request)

    if request.method == 'POST':
        form = FormFechamentoFerramenta(request.POST)

        if form.is_valid():
            ferramenta = form.cleaned_data['ferramenta']
            status = form.cleaned_data['status']

            fechamento = FerramentaFechamento(
                ferramenta=ferramenta,
                status=form.cleaned_data['status'],
                quantidade=form.cleaned_data['quantidade'],
                observacao=form.cleaned_data['observacao'],
                user_from=form.cleaned_data['user_from'],
                user=request.user,
            )

            if int(status) == 0:
                ferramenta.quantidade.quantidade += form.cleaned_data['quantidade']
                messages.success(request, 'Fechamento realizado e ferramentas (RE)inseridas no estoque')

            else:
                messages.success(request, 'Fechamento realizado')

            carga = FerramentaQuantidadeFuncionario.objects.get(
                user=form.cleaned_data['user_from'],
                ferramenta=ferramenta
            )
            carga.quantidade -= form.cleaned_data['quantidade']

            fechamento.save()
            ferramenta.quantidade.save()
            carga.save()

            return HttpResponseRedirect('/patrimonio/fechamento/ferramenta')

    else:
        form = FormFechamentoFerramenta()

    context = {
        'form': form,
        'form_submit_text': 'Registrar fechamento',
    }
    context.update(menu)

    return render(request, 'patrimonio/v2/entrada.html', context)

@login_required
@permission('patrimonio', )
def consulta_ferramenta(request):
    menu = menu_consultas_modelos(request)

    ferramenta = request.GET.get('q', '')

    form = FormFiltraQ(
        descricao="ferramenta",
        initial={
            'q': ferramenta,
        }
    )

    query = Q()

    if ferramenta != '':
        query = query & Q(nome__icontains=ferramenta)

    itens = Ferramenta.objects.filter(
        query
    ).values(
        'id',
        'nome',
        'descricao',
        'data',
        'user__first_name',
        'user__last_name',
    ).order_by('nome')

    paginator = Paginator(itens, 50)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)

    context = {
        'form': form,
        'form_submit_text': 'Filtrar',
        'page_obj': page_obj,
    }
    context.update(menu)

    return render(request, 'ferramenta/v2/consulta_ferramenta.html', context=context)

def edita_modelo_ferramenta(request, modelo_id):
    menu = menu_consultas_modelos(request)

    modelo = Ferramenta.objects.get(id=modelo_id)

    form = FormEditaModeloFerramenta(data=request.POST or None, instance=modelo)

    if request.method == 'POST':

        if form.is_valid():
            form.save()

            messages.success(request, 'Alterações salvas com sucesso')

            return HttpResponseRedirect(f'/patrimonio/edicao/ferramenta/modelo/{modelo_id}')

    context = {
        'form': form,
        'form_submit_text': 'Salvar alterações',
    }
    context.update(menu)

    return render(request, 'ferramenta/v2/edita_modelo_ferramenta.html', context)

@login_required
@permission('patrimonio', )
def consulta_ferramenta_estoque(request):
    menu = menu_consultas(request)

    ferramenta = request.GET.get('q', '')

    form = FormFiltraQ(
        descricao="ferramenta",
        initial={
            'q': ferramenta,
        }
    )

    query = Q()

    if ferramenta != '':
        query = query & Q(ferramenta__nome__icontains=ferramenta)

    itens = FerramentaQuantidade.objects.filter(
        query
    ).values(
        'ferramenta__nome',
        'quantidade',
    ).exclude(
        quantidade__lte=0,
    ).order_by('ferramenta__nome')

    paginator = Paginator(itens, 50)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)

    context = {
        'form': form,
        'form_submit_text': 'Filtrar',
        'page_obj': page_obj,
    }
    context.update(menu)

    return render(request, 'ferramenta/v2/consulta_ferramenta_estoque.html', context=context)


import itertools
import math
import os.path
import re
import time

from collections import defaultdict, Counter

def monad(num, steps):
    variables = defaultdict(int)
    counter = 0
    for step in steps:
        if step[0] == "inp":
            variables[step[1]] = int(num[counter])
            counter += 1
        else:
            first = variables[step[1]]
            second = (
                int(step[2]) if step[2].lstrip("-").isdigit() else variables[step[2]]
            )
            if step[0] == "mul":
                variables[step[1]] *= second
            elif step[0] == "eql":
                variables[step[1]] = 1 if first == second else 0
            elif step[0] == "add":
                variables[step[1]] += second
            elif step[0] == "div":
                if second == 0:
                    return 1
                variables[step[1]] //= second
            elif step[0] == "mod":
                if variables[step[1]] < 0 or second <= 0:
                    return 1
                variables[step[1]] %= second
    print(num)
    print(variables)
    return variables["z"]

def part1(steps):
    for num in range(11111111111111, 99999999999999):
        # for num in range(99999999999999,11111111111110,-1):
        n = str(num)
        if not "0" in n:
            res = monad(n, steps)
            if not res:
                break
    return num

if __name__ == "__main__":
    replit_start_time = time.perf_counter()
    counter = 0
    for i in range(1, 717):
        counter += len(str(i))
        print(i, counter)
        if counter >= 2040:
            print(i, counter)
            break
    print(counter)

    with open(os.path.join(os.path.dirname(__file__), "data.txt")) as f:
        data = [d.split() for d in f.read().splitlines()]
        # print(monad(str(99999999927392-7645130729),data))
        print(sum(int(digit) for digit in str(11111111119195)))

        # print(part1(data))
    # Keep this line at the end of your code
    replit_end_time = time.perf_counter()
    print("Elapsed time:", replit_end_time - replit_start_time)


import configparser
import os
import json
import urllib.request as urlRequest
from bs4 import BeautifulSoup
import time
import datetime
import traceback
from threading import Thread as thread
import threading
class aqi(thread):
    def __init__(self,file,conn):
        thread.__init__(self)
        self.__flag = threading.Event()
        self.__flag.set()
        self.__running = threading.Event()
        self.__running.set()

        self.configFile = os.getcwd() + file
        self.conn = conn
        self.exception = None

    def pause(self):
        self.__flag.clear()
    def resume(self):
        self.__flag.set()
    def stop(self):
        self.__flag.set()
        self.__running.clear()
    def spider(self):
        self.conn.saveLogInfo("aqi", "正在抓取AQI空气信息....",
                                  datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 1)
        print(datetime.datetime.now().strftime(
            "%Y-%m-%d %H:%M:%S") + " aqi " + "正在抓取AQI空气信息...  " + "运行状态: " + str(1))
        url = self.url + self.token
        req = urlRequest.Request(url)
        res = urlRequest.urlopen(req)
        if res != None:
            soup = BeautifulSoup(res,"lxml")
            aqiDetailJson = json.loads(soup.text)
            ## 使用json 文件测试
            # jsonFile = os.getcwd()+"/json/ariQuality.json"
            # fp = open(jsonFile,encoding="utf-8")
            # aqiDetailJson = json.load(fp)
            if "error" in aqiDetailJson:
                self.conn.saveLogInfo("aqi", aqiDetailJson["error"], datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 1)
                print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " aqi " + aqiDetailJson["error"] +"  " + "运行状态: " + str(1))
                return -1
            for aqi in aqiDetailJson:
                if aqi["position_name"] == None:
                    aqiDetailJson.remove(aqi)
            self.conn.saveAqiData(aqiDetailJson,self.dbNameList)

        # tabelContent = soup.find("table", {"id": self.tabelId})
        # timeClass = soup.find("div",{"class":"live_data_time"})
        # time = re.findall("时间：(.*)",timeClass.find("p").text)[0]
        # tb = tabelContent.find("tbody")

        # if tb:
        #     for tr in tb.findAll("tr"):
        #
        #         order = 1
        #         resultDict = dict()
        #         for td in tr.findAll("td"):
        #             if "class" in td.attrs:
        #                 continue
        #             resultDict[self.tdOrder[order]] = td.text
        #             order += 1
        #         resultDict["timepoint"]=time
        #         resultDict["co_24h"]= '0.7'
        #         resultDict['no_24h'] = "0.591"
        #         resultDict['o3_24h'] = ''
        #         resultDict['o3_24h'] =47
        #         resultDict['pm10_24h'] = 35
        #         resultDict['pm25_24h'] = 28
        #         ## TODO 插入数据库
        #         print("插入数据库")
        #         sqlStatment = "INSERT INTO media.airquality ('positionname','timepoint','co','co_24h','no','no_24h'," \
        #                       "'o3','o3_24h','pm10','pm10_24h','pm25','pm25_24h','so2','so2_24h','primarypollutant','quality')" \
        #                       "VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" \
        #                       %(resultDict['positionname'],resultDict['timepoint'],resultDict['co'],resultDict['co_24h'],resultDict['no'],
        #                         resultDict['no_24h'],resultDict['o3'],resultDict['o3_24h'],resultDict['pm10'],resultDict['pm10_24h'],resultDict['pm25'],
        #                         resultDict['pm25_24h'],resultDict['so2'],resultDict['so2_24h'],resultDict['primarypollutant'],resultDict['quality'])
        #         self.conn.sqlExec(sqlStatment,type=1)

    def loadConf(self):
        self.conn.saveLogInfo("aqi", "读取AQI配置文件", datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 1)
        print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " aqi " + "读取AQI配置文件  " + "运行状态: " + str(1))
        conf = configparser.ConfigParser()
        conf.read(self.configFile,"utf-8")
        self.url = conf.get(conf.sections()[0],"url")

        self.dbNameList = conf.get(conf.sections()[1],"dbDict").split(",")

        self.token = conf.get(conf.sections()[1],"token")
    ## Aqi数据每半小时抓取一次
    def run(self):
        self.conn.saveLogInfo("aqi", "AQI线程开始运行",datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 1)
        print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " aqi " + "AQI线程开始运行  " + "运行状态: " + str(1))
        try:
            while self.__running.isSet():
                self.__flag.wait()
                self.conn.saveLogInfo("aqi", "AQI信息开始抓取", datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 1)
                print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " aqi " + "AQI信息开始抓取  " + "运行状态: " + str(1))
                self.spider()
                self.conn.saveLogInfo("aqi", "AQI信息抓取完毕", datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 1)
                print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " aqi " + "AQI信息抓取完毕  " + "运行状态: " + str(1))
                time.sleep(2700)
        except Exception as e:
            self.exTrace = traceback.format_exc()
            self.exception = e
            traceback.print_exc()
    def getException(self):
        if self.exception:
            exString = ""
            for arg in self.exception.args:
                exString += str(arg)
            if not self.exTrace:
                exString += self.exTrace
            exString = exString.replace("'", "")
            self.conn.saveLogInfo("aqi", "AQI线程异常,异常内容为%s"%exString, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 0)
            print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " aqi " + "AQI线程异常,异常内容为%s  "%exString + "运行状态: " + str(0))
        else:
            self.conn.saveLogInfo("aqi", "aqi线程异常内容为空", datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 0)
            print(datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S") + " aqi " + "AQI线程异常,异常内容为空  " + "运行状态: " + str(0))
    def dbReset(self,db):
        self.conn = db


import asyncpg
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Text
from aiogram.types import Message, ReplyKeyboardRemove, InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery

from keyboards.default import menu
from keyboards.inline.callback_data import select_dictionary_callback, add_translate_callback
from loader import dp, db
from states import CreateNewTranslate

@dp.callback_query_handler(add_translate_callback.filter(is_selected='True'))
async def new_translate_callback(call: CallbackQuery):
    """Query for input word"""
    await call.answer()
    await call.message.delete()
    await call.message.answer("Напиши слово на английском", reply_markup=ReplyKeyboardRemove())
    await CreateNewTranslate.SetEnglishWord.set()

@dp.message_handler(Text("📌Новый перевод"))
async def new_translate(message: Message):
    """Query for input word"""
    await message.answer("Напиши слово на английском", reply_markup=ReplyKeyboardRemove())
    await CreateNewTranslate.SetEnglishWord.set()

@dp.message_handler(state=CreateNewTranslate.SetEnglishWord)
async def set_english_word(message: Message, state: FSMContext):
    """Query for input translate for word"""
    await state.update_data(english_word=message.text)
    await message.answer("Теперь введи перевод своего слова")
    await CreateNewTranslate.SetRussianWord.set()

@dp.message_handler(state=CreateNewTranslate.SetRussianWord)
async def set_translate(message: Message, state: FSMContext):
    await state.update_data(russian_word=message.text)
    data = await state.get_data()
    english_word = data.get("english_word")
    russian_word = data.get("russian_word")
    user_id = message.from_user.id

    try:
        ########################################################################
        #                        DATABASE Queries                              #
        current_dictionary = await db.select_current_dictionary(user_id)
        await db.add_translate(english_word, russian_word, current_dictionary)
        translate = await db.select_last_translate(current_dictionary)
        translate_id = translate[0]
        await db.set_learning_translate(current_dictionary, translate_id)
        ########################################################################
        await message.answer(f"Добавлен новый перевод:\n"
                             f"{english_word} - {russian_word}", reply_markup=menu)
        await state.finish()

    except asyncpg.exceptions.NotNullViolationError:
        select_dictionaries = await db.select_dictionaries(user_id)
        if len(select_dictionaries) > 0:
            """It raised when user delete current dictionary and try to add new translate"""
            show_dictionaries_keyboard = InlineKeyboardMarkup(
                inline_keyboard=[
                    [InlineKeyboardButton(
                        text=item[2],
                        callback_data=select_dictionary_callback.new(
                            dictionary_id=item[0],
                            dictionary_name=item[2]))] for item in select_dictionaries]
            )  # Generation dictionaries into inline buttons
            await message.answer("Так как вы удалили свой текущий словарь, \n"
                                 "то вам придется выбрать новый текущий словарь",
                                 reply_markup=show_dictionaries_keyboard)  # Showing all user dictionaries for select
        else:
            """It raised when user delete all dictionaries and try to add new translate"""
            await message.answer("У вас нету словарей! Добавьте хоть один словарь.", reply_markup=menu)
            await state.finish()
    except:
        await message.answer("Упс, какая-то ошибка!\n ", reply_markup=menu)
        await state.finish()

@dp.callback_query_handler(select_dictionary_callback.filter(), state=CreateNewTranslate.SetRussianWord)
async def changing_dictionary(call: CallbackQuery, callback_data: dict, state: FSMContext):
    """Setting current dictionary from exception"""
    await call.message.delete()
    name_selected_dictionary = callback_data['dictionary_name']
    user_id = call.from_user.id
    id_selected_dictionary = int(callback_data['dictionary_id'])
    ########################################################################
    #                        DATABASE Queries                              #
    await db.set_current_dictionary(user_id, id_selected_dictionary)
    ########################################################################
    await call.answer(f"Выбран словарь '{name_selected_dictionary}'", cache_time=5)
    await call.message.answer("Теперь вы можете создать перевод!", reply_markup=menu)
    await state.finish()


# Author: Leyla Noroozbabaee
# Date: 12/12/2021
# To reproduce the data needed for Figure 8 in associated original paper,
# execute this script in the Python console in OpenCOR. This can be done
# with the following commands at the prompt in the OpenCOR Python console:
#
#     In [1]: cd path/to/folder_this_file_is_in
#     In [2]: run Fig8_sim.py
import opencor as oc
import matplotlib.pyplot as plt
import numpy as np
# The prefix of the saved output file name

prefilename = 'Fig8'
# Load the simulation file
simfile = 'C:/Users/lnor300/Documents/CellML/12_Labors/Tong_2011_V1/Simulations/CaVAR_Tong_2011.sedml'
simulation = oc.open_simulation(simfile)
data = simulation.data()
# Set constant values
start = 0
end = 5000
pointInterval = 0.1
data.set_starting_point(start)
data.set_ending_point(end)
data.set_point_interval(pointInterval)
ca_i = [0.000005, 0.00001, 0.00005, 0.00008, 0.0001, 0.0005, 0.0008, 0.001, 0.005, 0.008, 0.01, 0.05, 0.1, 0.5, 0.8, 1.0, 5.0, 8, 10, 50, 80, 100]
for j in range(len(ca_i)):
    # Reset states variables and parameters
    simulation.reset(True)
    data.constants()['interface/membrane_potential/v_init'] = 260
    data.constants() [ 'interface/Ca_Concentrations/cai' ] = ca_i [ j ]
    simulation.run()
    # Access simulation results
    results = simulation.results()
    # Data to save
    varName = np.array(["Time", "xass_z", "xass_vh", "xatc", "xass", "iBKa","xa", "v", "xabss_z", "xabss_vh",
                        "xabtc", "xabss", "iBKab","xab"])
    vars = np.reshape(varName, (1,14))
    rows = end * 10 + 1
    # Grab some algebraic results
    r = np.zeros((rows, len(varName)))
    r [ :, 0 ] = results.voi().values()
    r [ :, 1 ] = results.constants()['interface/I_BKa/xass_z'].values()
    r [ :, 2 ] = results.constants()['interface/I_BKa/xass_vh'].values()
    r [ :, 3 ] = results.algebraic() [ 'interface/I_BKa/xatc'].values()
    r [ :, 4 ] = results.algebraic() [ 'interface/I_BKa/xass'].values()
    r [ :, 5 ] = results.algebraic() [ 'interface/I_BKa/iBKa'].values()
    r [ :, 6 ] = results.states() ['interface/I_BKa/xa'].values()
    r [ :, 7] = results.states() ['interface/membrane_potential/v' ].values()
    r [ :, 8 ] = results.constants() [ 'interface/I_BKab/xabss_z' ].values()
    r [ :, 9 ] = results.constants() [ 'interface/I_BKab/xabss_vh' ].values()
    r [ :, 10 ] = results.algebraic() [ 'interface/I_BKab/xabtc' ].values()
    r [ :, 11] = results.algebraic() [ 'interface/I_BKab/xabss' ].values()
    r [ :, 12] = results.algebraic() [ 'interface/I_BKab/iBKab' ].values()
    r [ :, 13] = results.states() [ 'interface/I_BKab/xab' ].values()
    filename = '%s_%s.csv' % (prefilename, j)
    np.savetxt(filename, vars, fmt='%s', delimiter=",")
    with open(filename, "ab") as f:
        np.savetxt(f, r, delimiter=",")
    f.close


# https://github.com/eriklindernoren/Keras-GAN/blob/master/ccgan/ccgan.py
from __future__ import print_function, division
import os
import keras
import numpy
import scipy
import scipy.misc
import skimage
import matplotlib.pyplot as plt

class CCGAN():
    def __init__(self):
        self.img_rows = 32
        self.img_cols = 32
        self.channels = 1
        self.img_shape = (self.img_rows, self.img_cols, self.channels)
        self.mask_height = 10
        self.mask_width = 10
        self.num_classes = 10

        self.gf = 32
        self.df = 32

        optimizer = keras.optimizers.Adam(2e-4, 5e-1)
        self.discriminator = self.build_discriminator()
        self.discriminator.compile(loss=['mse', 'categorical_crossentropy'],
                                   loss_weights=[0.5, 0.5],
                                   metrics=['accuracy'],
                                   optimizer=optimizer)
        self.generator = self.build_generator()
        '''
        self.generator.compile(loss=[],
                               metrics=[],
                               optimizer=optimizer)
        '''
        masked_input = keras.layers.Input(shape=self.img_shape)
        gen_img = self.generator(masked_input)
        self.discriminator.trainable = False
        valid, _ = self.discriminator(gen_img)
        self.combined = keras.models.Model(masked_input, valid)
        self.combined.compile(loss=['mse'],
                              optimizer=optimizer)

    def build_generator(self):
        def conv2d(layer_input, filters, f_size=4, bn=True):
            d = keras.layers.Conv2D(filters=filters, kernel_size=f_size, strides=2, padding='same')(layer_input)
            d = keras.layers.LeakyReLU(alpha=0.2)(d)
            if bn:
                d = keras.layers.BatchNormalization(momentum=0.8)(d)
            return d

        def deconv2d(layer_input, skip_input, filters, f_size=4, dropout_rate=0):
            u = keras.layers.UpSampling2D(size=2)(layer_input)
            u = keras.layers.Conv2D(filters, kernel_size=f_size, strides=1, padding='same', activation='relu')(u)
            if dropout_rate:
                u = keras.layers.Dropout(dropout_rate)(u)
            u = keras.layers.BatchNormalization(momentum=8e-1)(u)
            u = keras.layers.Concatenate()([u, skip_input])
            return u

        img = keras.layers.Input(shape=self.img_shape)
        d1 = conv2d(img, self.gf, bn=False)
        d2 = conv2d(d1, self.gf * 2)
        d3 = conv2d(d2, self.gf * 4)
        d4 = conv2d(d3, self.gf * 8)

        u1 = deconv2d(d4, d3, self.gf * 4)
        u2 = deconv2d(u1, d2, self.gf * 2)
        u3 = deconv2d(u2, d1, self.gf)
        u4 = keras.layers.UpSampling2D(size=2)(u3)
        output_img = keras.layers.Conv2D(self.channels, kernel_size=4, strides=1, padding='same', activation='tanh')(u4)
        return keras.models.Model(img, output_img)

    def build_discriminator(self):
        img = keras.layers.Input(shape=self.img_shape)
        model = keras.Sequential()
        model.add(keras.layers.Conv2D(64, kernel_size=4, strides=2, padding='same', input_shape=self.img_shape))
        model.add(keras.layers.LeakyReLU(alpha=0.8))
        model.add(keras.layers.Conv2D(128, kernel_size=4, strides=2, padding='same'))
        model.add(keras.layers.LeakyReLU(alpha=0.2))
        model.add(keras.layers.BatchNormalization())
        model.add(keras.layers.Conv2D(256, kernel_size=4, strides=2, padding='same'))
        model.add(keras.layers.LeakyReLU(alpha=0.2))
        model.add(keras.layers.BatchNormalization(momentum=8e-1))
        model.summary()

        img = keras.layers.Input(shape=self.img_shape)
        features = model(img)
        validity = keras.layers.Conv2D(1, kernel_size=4, strides=1, padding='same')(features)
        label = keras.layers.Flatten()(features)
        label = keras.layers.Dense(self.num_classes + 1, activation='softmax')(label)
        return keras.models.Model(img, [validity, label])

    def mask_randomly(self, imgs):
        y1 = numpy.random.randint(0, self.img_rows - self.mask_height, imgs.shape[0])
        y2 = y1 + self.mask_height
        x1 = numpy.random.randint(0, self.img_rows - self.mask_height, imgs.shape[0])
        x2 = x1 + self.mask_width
        masked_imgs = numpy.empty_like(imgs)
        for i, img in enumerate(imgs):
            masked_img = img.copy()
            _y1, _y2, _x1, _x2 = y1[i], y2[i], x1[i], x2[i]
            masked_img[_y1: _y2, _x1: _x2, :] = 0
            masked_imgs[i] = masked_img
        return masked_imgs

    def train(self, epochs=6, batch_size=128, sample_interval=2):
        (X_train, Y_train), (_, _) = keras.datasets.mnist.load_data()
        # X_train = numpy.array([scipy.misc.imresize(x, [self.img_rows, self.img_cols]) for x in X_train])
        X_train = numpy.array([skimage.transform.resize(x, [self.img_rows, self.img_cols]) for x in X_train])
        X_train = (X_train.astype(numpy.float32) - 127.5) / 127.5
        X_train = numpy.expand_dims(X_train, axis=3)
        Y_train = Y_train.reshape(-1, 1)

        valid = numpy.ones((batch_size, 4, 4, 1))
        fake = numpy.zeros((batch_size, 4, 4, 1))

        for epoch in range(epochs):
            idx = numpy.random.randint(0, X_train.shape[0], batch_size)
            imgs = X_train[idx]
            labels = Y_train[idx]
            masked_imgs = self.mask_randomly(imgs)
            gen_imgs = self.generator.predict(masked_imgs)
            labels = keras.utils.to_categorical(labels, num_classes=self.num_classes + 1)
            fake_labels = keras.utils.to_categorical(numpy.full((batch_size, 1), self.num_classes), num_classes=self.num_classes + 1)
            d_loss_real = self.discriminator.train_on_batch(imgs, [valid, labels])
            d_loss_fake = self.discriminator.train_on_batch(gen_imgs, [fake, fake_labels])
            d_loss = 0.5 * numpy.add(d_loss_real, d_loss_fake)
            g_loss = self.combined.train_on_batch(masked_imgs, valid)
            print('%d [D loss: %f, op_acc: %.2f%%] [G loss: %f]' % (epoch, d_loss[0], 100 * d_loss[4], g_loss))

            if epoch % sample_interval == 0:
                idx = numpy.random.randint(0, X_train.shape[0], 6)
                imgs = X_train[idx]
                self.sample_images(epoch, imgs)
                self.save_model()

    def sample_images(self, epoch, imgs):
        r, c = 3, 6
        masked_imgs = self.mask_randomly(imgs)
        gen_imgs = self.generator.predict(masked_imgs)
        imgs = (imgs + 1.0) * 0.5
        masked_imgs = (masked_imgs + 1.0) * 0.5
        gen_imgs = (gen_imgs + 1.0) * 0.5
        gen_imgs = numpy.where(gen_imgs < 0, 0, gen_imgs)
        fig, axs = plt.subplots(r, c)
        for i in range(c):
            axs[0, i].imshow(imgs[i, :, :, 0], cmap='gray')
            axs[0, i].axis('off')
            axs[1, i].imshow(masked_imgs[i, :, :, 0], cmap='gray')
            axs[1, i].axis('off')
            axs[2, i].imshow(gen_imgs[i, :, :, 0], cmap='gray')
            axs[2, i].axis('off')
        fig.savefig('figimages%d.png' % epoch)
        plt.close()

    def save_model(self):
        def save(model, model_name):
            model_path = 'model_name[%s].json' % model_name
            weights_path = 'saved_model[%s]_weights.hdf5' % model_name
            options = {'file_arch': model_path,
                       'file_weight': weights_path}
            json_string = model.to_json()
            open(options['file_arch'], 'w').write(json_string)
            model.save_weights(options['file_weight'])
        save(self.generator, 'ccgan_generator')
        save(self.discriminator, 'ccgan_discriminator')

print('Start')
ccgan = CCGAN()
ccgan.train(batch_size=32)
print('End')


#!/usr/bin/python3
""" """
import unittest
from models.state import State

class test_state(unittest.TestCase):
    """ """

    def __init__(self, *args, **kwargs):
        """ """
        super().__init__(*args, **kwargs)
        self.name = "State"
        self.value = State

    def test_name(self):
        """ """
        new = self.value()
        self.assertEqual(type(new.name), str)


class Solution:
    def minimumTime(self, N : int, cur : int, pos : List[int], time : List[int]) -> int:
        # code here
        mn = float('inf')
        for i in range(N):
            mn = min(abs(cur-pos[i])*time[i],mn)
        return mn

"""N: an integer representing the total number of positions
cur: an integer representing the current position
pos: a list of integers representing the positions of all the items
time: a list of integers representing the time taken to pick up each item
The function iterates through each position in the "pos" list and calculates the time taken to reach that position from the current position. The time taken is calculated as the absolute difference between the current position and the position of the item multiplied by the time taken to pick up that item.

The minimum time taken to pick up any item is then stored in the "mn" variable using the "min" function. Finally, the function returns the minimum time taken to pick up any item.

In short, the function calculates the minimum time required to pick up an item from a list of positions and their respective pickup times, given the current position."""


import sys
sys.stdin = open('07_input.txt')

T = int(input())

for tc in range(1, T+1):
    numbers = input()
    n = len(numbers)//2
    a = numbers[0:n]
    b = numbers[n:]
    x = 0
    y = 0
    for i in a:
        x += int(i)
    for i in b:
        y += int(i)

    if x == y:
        print('Lucky')
    else:
        print('Ready')

T = int(input())

for tc in range(1, T+1):
    numbers = input()
    n = len(numbers)//2
    a = 0
    b = 0
    for i in numbers[0:n]:
        a += int(i)
    for i in numbers[n:]:
        b += int(i)
    if a == b:
        print('Lucky')
    else:
        print('Ready')


from classes import Agent
from numpy.random import choice

class PlayerMC(Agent):
    '''agent learning with on-policy first-visit Monte Carlo method'''
    def __init__(self, epsilon = 0.1, discount_rate = 1):
        super().__init__(values_type = 'action')
        self.epsilon = epsilon # epsilon value for epsilon-greedy policy
        self.appearances = dict() # number of appearances of state-action pairs
        self.episode_steps = [] # states-action pairs that appeared during one hand
        self.discount_rate = discount_rate # discount rate for future rewards

    def select_action(self, training_phase = True):
        '''epsilon-greedy action selection in a given state
        with respect to current state-action values'''
        # for hand values less than 12 always hit
        if self.hand.value <= 11:
            return 'hit'
        # during test phase the agent always chooses greedy action
        epsilon = self.epsilon if training_phase else 0
        # exploration vs. exploitation decision
        if choice([0,1], p = [epsilon, 1 - epsilon]) == 0:
            # random action - exploration
            return choice(['hit', 'stand'], p = [0.5, 0.5])
        else:
            # greedy action - exploitation
            if self.values[self.state]['hit'] > self.values[self.state]['stand']:
                return 'hit'
            elif self.values[self.state]['hit'] < self.values[self.state]['stand']:
                return 'stand'
            else:
                return choice(['hit', 'stand'], p = [0.5, 0.5])

    def play(self, epoch = -1, training_phase = True):
        '''simulation of one episode/hand'''
        self.episode_steps.clear()
        if training_phase:
            self.hands_played += 1
        while self.hand.value <= 21:
            action = self.select_action(training_phase)
            self.episode_steps.append((self.state, action))
            if action == 'stand':
                break
            self.deal_card()
        return self.hand.value

    def propagate_reward(self, reward):
        '''changing values of state-action pairs, reward is one of {-1,0,1}'''
        if reward == 1:
            self.hands_won += 1
        elif reward == 0:
            self.hands_drawn += 1
        # reward is available at the end of the hand, intermediate rewards = 0
        self.episode_steps.reverse()
        for state, action in self.episode_steps:
            # first -> update number of appearances for state-action pair
            if (state, action) not in self.appearances:
                self.appearances[(state, action)] = 1
            else:
                self.appearances[(state, action)] += 1
            # running average value for state-action pair expected return
            self.values[state][action] += (reward - self.values[state][action])/self.appearances[(state, action)]
            reward = self.discount_rate*reward # discounted reward

First visit Monte Carlo method/first_visit_MC.py


import torch
from nuclr.data import prepare_nuclear_data
from nuclr.config import NUCLR
import matplotlib.pyplot as plt
from nuclr.model import get_model_and_optim
from lib.config_utils import parse_arguments_and_get_name
import os

args, name = parse_arguments_and_get_name(NUCLR)
torch.manual_seed(args.SEED)

data = prepare_nuclear_data(args)

print(args)

def load(path):
    return torch.load(path, map_location=torch.device('cpu'))

basedir = os.path.join(args.ROOT, name)
modelpath = os.path.join(basedir, "model_NUCLR_best.pt")
model, _ = get_model_and_optim(data, args)
model.load_state_dict(load(modelpath))
model.eval()

DEVICE = args.DEV

def get_idx(data, target):
    output_map = data.output_map
    if target not in output_map:
        raise ValueError(f"Target {target} not in output map")
    target_size = output_map[target]
    if target_size > 1:
        raise ValueError(f"Target {target} is not a scalar")
    target_idx = 0
    for prev_target, size in output_map.items():
        if prev_target == target:
            return target_idx
        target_idx += size

def plot(target="binding_energy", split="both", rescale=False):
    true = data.y
    with torch.no_grad(): pred = model(data.X)
    if rescale:
        true = torch.tensor(data.regression_transformer.inverse_transform(true))
        pred = torch.tensor(data.regression_transformer.inverse_transform(pred))
    target_idx = get_idx(data, target)
    target_true = true[:, target_idx]
    target_pred = pred[:, target_idx]
    if split == "train":
        split_mask = data.train_mask
    elif split == "val":
        split_mask = data.val_mask
    elif split == "both":
        split_mask = data.train_mask | data.val_mask
    else:
        raise ValueError(f"Unknown split {split}")
    nanmask = ~target_true.isnan()
    mask = split_mask & nanmask

    _, axes = plt.subplots(2, 2, figsize=(20, 14))
    axes = axes.flatten()
    for ax, target in zip(axes, [target_true, target_true - target_pred]):
        sc = ax.scatter(data.X[:, 0][mask], data.X[:, 1][mask], c=target[mask], s=10, cmap="bwr")
        ax.set_xlabel("Z")
        ax.set_ylabel("N")
        #center the colorbar around 0
        if ax == axes[1]:
          max_ = max(abs(sc.get_clim()[0]), abs(sc.get_clim()[1]))
          sc.set_clim(-max_, max_)
        plt.colorbar(sc, ax=ax)
    for ax, title in zip(axes, ["True", "True - Pred"]):
        ax.set_title(title)
    for ax, x in zip(axes[2:], data.X.T):
        ax.scatter(x, target_true, s=10, label="True")
        ax.scatter(x, target_pred, s=10, label="Pred")
        ax.set_xlabel("N" if ax == axes[2] else "Z")
        ax.set_ylabel("Binding energy")
        ax.legend()
    plt.tight_layout()
    plt.show()

if __name__ == "__main__":
    plot("binding_energy", split="val", rescale=True)


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

import cupy     as cp

from .util import (
    judge_dtype,
    elementwise_not,
    ternary_operation,
    checkfinite,
)

#############################
#   fixpix
#############################

def fixpix(data,mask,out=None,dtype=None,fix_NaN=False):
    '''
    fill the bad pixel with linear interpolation using surrounding pixels

    Parameters
    ----------
    data : array-like
        An array of image
        If a 3D array containing multiple images,
        the images must be stacked along the 1st dimension (axis=0).
    mask : array-like
        An array indicates bad pixel positions
        The shape must be same as image.
        The value of bad pixel is nonzero, and the others is 0.
        If all pixels are bad, raise ValueError.
    out : cupy.ndarray, default None
        Alternate output array in which to place the result. The default
        is None; if provided, it must have the same shape as the
        expected output, but the type will be cast if necessary.
    dtype : str or dtype, default None
        dtype of ndarray used internally
        If None, use eclair.common.default_dtype.
        If the input dtype is different, use a casted copy.
    fix_NaN : bool, default False
        If true, fix NaN pixel even if it's not specified
        as bad pixel in mask

    Returns
    -------
    fixed : ndarray
        An array of images fixed bad pixel

    Notes
    -----
    NaN is ignored in interpolation calculations,
    but is not fixed if fix_NaN is False.
    '''
    dtype = judge_dtype(dtype)

    data = cp.asarray(data,dtype=dtype)
    mask = cp.asarray(mask,dtype=dtype)

    dshape = data.shape
    imshape = data.shape[-2:]
    if imshape != mask.shape[-2:]:
        raise ValueError('shape differs between data and mask')
    if mask.all(axis=(-2,-1)).any():
        raise ValueError('No available pixel')

    if out is None:
        out = data.copy()
    else:
        cp.copyto(out,data)

    tout = cp.array(out,copy=False,ndmin=3)
    filt = cp.array(mask,ndmin=3)
    elementwise_not(filt,filt)

    ternary_operation(filt,tout,0,tout)

    dconv = cp.empty_like(tout)
    nconv = cp.empty_like(filt)

    for _ in range(max(imshape)):
        conv_kernel(tout,dconv)
        conv_kernel(filt,nconv)
        fix_core(filt,dconv,nconv,fix_NaN,tout)
        if nconv.all():
            break
        else:
            cp.sign(nconv,out=filt)

    return out

fix_core = cp.ElementwiseKernel(
    in_params='F f, T d, F n, bool fn',
    out_params='T z',
    operation='''
        int flag = (!fn | isfinite(z)) & (f!=0);
        T val = d / (n+(n==0));
        z = (flag ? z : val);
    ''',
    name='fix_core'
)

conv_kernel = cp.ElementwiseKernel(
    in_params='raw T input',
    out_params='T output',
    operation='''
        int ly = input.shape()[1];
        int lx = input.shape()[2];
        int iyz = i / lx;
        int i_x = i % lx;
        int i_y = iyz % ly;
        int i_z = iyz / ly;
        int idx[3] = {i_z};
        int *t_y = &(idx[1]), *t_x = &(idx[2]);
        int s_x = max(i_x-1,0), e_x = min(i_x+2,lx);
        int s_y = max(i_y-1,0), e_y = min(i_y+2,ly);
        int fn, c = 0;
        T val, tmp, summed = 0;
        for (*t_y=s_y; *t_y<e_y; (*t_y)++) {
            tmp = 0;
            for (*t_x=s_x; *t_x<e_x; (*t_x)++) {
                val = input[idx];
                fn = isfinite(val);
                tmp += (fn ? val : 0);
                c += fn;
            }
            summed += tmp;
        }
        output = summed / (c + (c==0))
    ''',
    name='convolution'
)


from distutils.core import setup
from setuptools import find_packages

install_requires = [
    'decorator<5.0.0',  # For networkx
    'networkx>=2.5',
    'numpy>1.7.0',
    'Pillow>=8.3.2',
    'pynput>=1.7.3',
    'pyzmq>=20.0.0',
    'av>=8.0.1',
    'sounddevice>=0.4.3',
    'soundfile>=0.10.3',
    'msgpack>1.0.0',
    'windows-capture-devices; platform_system == "Windows"'
]

setup(
    name='multisensor-pipeline',
    version='2.1.1',
    author='Michael Barz',
    author_email='michael.barz@dfki.de',
    license='CC BY-NC-SA 4.0',
    packages=find_packages(
        include=('multisensor_pipeline.*', 'multisensor_pipeline')
    ),
    url="https://github.com/DFKI-Interactive-Machine-Learning/multisensor-pipeline",
    description="The core library of the DFKI multisensor pipeline framework.",
    python_requires='>=3.6.0',
    install_requires=install_requires,
    keywords=[
        'multimodality', 'streaming', 'multisensor', 'sensors', 'multimodal interaction',
        'pipeline', 'stream processing', 'multiprocessing'
    ]
)


from pyspark.sql import SparkSession
from pyspark.sql.functions import *

import sys

spark = SparkSession.builder.appName("A3T1").getOrCreate()

# Retrive word and the value of K
wrd = sys.argv[1]
k = int(sys.argv[2])

# Reading the two csv files
shape = spark.read.option("header",True).csv(sys.argv[3])
shape_stat = spark.read.option("header",True).csv(sys.argv[4])

# Joining the Dataframes
joined = shape_stat.join(shape, ['key_id','word'])

# Filters

# Check presence of word
joined = joined.filter(joined.word.contains(wrd))

# Check recog status = False
joined = joined.filter(joined.recognized.contains("False"))

# Check num strokes < K
joined = joined.filter(joined.Total_Strokes < k)

# Grouping Entries by country and sorting them
final = joined.groupBy("countrycode").count()
final = final.orderBy("countrycode")

if(len(final.collect()) > 0):
	for i in final.collect():
		print(i[0], i[1], sep=",")
else:
	print("0")


import io
import mimetypes
import posixpath
import shutil
import sys
from functools import partial
from urllib.parse import urlparse, urlsplit, unquote
from pathlib import Path
from typing import Union, Tuple
from http import HTTPStatus
from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO

from zipreport.report import ReportFile, ReportFileLoader, ReportFileBuilder, const
from zipreport.template import JinjaRender

# shared ReportFile Object to be reused between requests
_zpt = None

class ReportFileHandler(BaseHTTPRequestHandler):
    """Simple HTTP request handler with GET and HEAD commands.

    This class is based on the SimpleHTTPRequestHandler in http.server
    Due to its simplified implementation, the code is not thread-safe!!!
    """

    server_version = "ZipReport HTTP Server"
    root_paths = [
        "/",
        "/index.html",
        "/index.htm",
        "/report.html",
        "/report.htm",
    ]
    extensions_map = {
        "": "application/octet-stream",
    }

    def __init__(self, *args, report_path=None, extra_mime_types=None, **kwargs):
        """
        Constructor
        :param args:
        :param report_path: path of the directory or zpt file to process
        :param extra_mime_types: optional extra mime types to register
        :param kwargs:
        """
        self.bootstrap_mime_types(extra_mime_types)
        if report_path is None:
            raise RuntimeError("Debug HTTP Server must work with a generated report")
        self.report_path = Path(report_path)
        self.is_report_file = self.report_path.is_file()
        self.enc = sys.getfilesystemencoding()

        super().__init__(*args, **kwargs)

    def bootstrap_mime_types(self, extra_mime_types: dict = None):
        """
        Bootstrap mime types
        :param mime_types: dictionary of entries to add/replace
        :return:
        """
        # try to use system mimetypes
        if not mimetypes.inited:
            mimetypes.init()
        self.extensions_map.update(mimetypes.types_map)
        # add extra mimetypes if necessary
        if extra_mime_types is not None:
            self.extensions_map.update(extra_mime_types)

    def build_report(self) -> Tuple[bool, Union[io.BytesIO, None]]:
        """
        Assembles, builds and renders the report file
        In case of errors, response is (False, io.BytesIO(response_message)), with headers already processed
        In case of success, response is (True, io.BytesIO()); response should be processed from the zpt path for the report file
        :return: (bool, io.BytesIO)
        """
        global _zpt
        _zpt = None
        try:
            # load file or build file, according to path
            if self.is_report_file:
                sys.stdout.write("Reloading report file...\n")
                _zpt = ReportFileLoader.load_file(self.report_path)
            else:
                sys.stdout.write("Rebuilding report from path...\n")
                bresult, zpt = ReportFileBuilder.build_zipfs(self.report_path)
                if not bresult.success():
                    return False, self.error_500(";".join(bresult.get_errors()))
                # create ReportFile from zipfs
                _zpt = ReportFileLoader.load_zipfs(zpt)
            # render template to REPORT_FILE_NAME
            # returns a dummy BytesIO object
            JinjaRender(_zpt).render()
            return True, io.BytesIO()

        except Exception as e:
            return False, self.error_500(str(e))

    def do_GET(self):
        """
        Process a GET request
        :return:
        """
        response = self.process_request()
        if response:
            try:
                shutil.copyfileobj(response, self.wfile)
            finally:
                response.close()

    def do_HEAD(self):
        """
        Process a HEAD request
        :return:
        """
        response = self.process_request()
        if response:
            response.close()

    def process_request(self):
        """
        Common request processing logic
        :return: io.BytesIO()
        """
        global _zpt
        path = self.clean_path(self.path)
        response = None

        if _zpt is None:
            success, response = self.build_report()
            if not success:
                return response

        if path in self.root_paths:
            if response is None:
                success, response = self.build_report()
                if not success:
                    return response
            # rewrite path to point to report file
            path = "/" + const.REPORT_FILE_NAME

        if _zpt.exists(path):
            return self.handle_file(Path(path).name, _zpt.get(path))

        # path not found
        return self.error_404(path)

    def clean_path(self, path: str) -> str:
        """
        Cleans up the request path
        :param path:str
        """
        # remove ignored parameters
        path = path.split("?", 1)[0]
        path = path.split("#", 1)[0]
        try:
            path = unquote(path, errors="surrogatepass")
        except UnicodeDecodeError:
            path = unquote(path)
        return posixpath.normpath(path)

    def guess_type(self, fname: str):
        """
        Tries to determine a filename mime type
        """
        base, ext = posixpath.splitext(fname)
        if ext in self.extensions_map:
            return self.extensions_map[ext]

        ext = ext.lower()
        if ext in self.extensions_map:
            return self.extensions_map[ext]

        return self.extensions_map[""]

    def handle_file(self, fname: str, contents: io.BytesIO) -> io.BytesIO:
        """
        Generates response headers for a given file
        :param fname:
        :param contents:
        :return: io.BytesIO
        """
        self.send_response(HTTPStatus.OK)
        self.send_header("Content-type", self.guess_type(fname))
        self.send_header("Content-Length", str(contents.getbuffer().nbytes))
        self.end_headers()
        contents.seek(0)
        return contents

    def error_500(self, item: str):
        """
        Generates a customized 500 response
        :param item: optional error message
        :return: io.BytesIO
        """
        if item:
            response = (
                "
Internal Server Error: {}
".format(
                    item
                )
            )
        else:
            response = "
File not found
"
        return self._error(HTTPStatus.INTERNAL_SERVER_ERROR, response)

    def error_404(self, item: str = None) -> io.BytesIO:
        """
        Generates a customized 404 response
        :param item: optional error message
        :return: io.BytesIO
        """
        if item:
            response = "
File not found: {}
".format(
                item
            )
        else:
            response = "
File not found
"
        return self._error(HTTPStatus.NOT_FOUND, response)

    def _error(self, code: int, contents: str) -> io.BytesIO:
        """
        Common error response logic
        :param code: HTTP status code
        :param contents: optional HTML response
        :return: io.BytesIO
        """
        size = len(contents)
        response = io.BytesIO(bytes(contents, encoding="utf-8"))
        self.send_response(code)
        self.send_header(
            "Content-type", "text/html; charset=%s" % sys.getfilesystemencoding()
        )
        self.send_header("Content-Length", str(size))
        self.end_headers()
        response.seek(0)
        return response

class DebugServer:
    DEFAULT_ADDR = "localhost"
    DEFAULT_PORT = 8001

    def __init__(self, addr: str = DEFAULT_ADDR, port: int = DEFAULT_PORT):
        self._addr = addr
        self._port = port

    def set_addr(self, addr: str):
        self._addr = addr

    def set_port(self, port: int):
        self._port = port

    def run(self, report_path: str):
        server_address = (self._addr, int(self._port))
        handler_class = partial(ReportFileHandler, report_path=report_path)
        sys.stdout.write(
            "\nStarted debug server at http://{addr}:{port}\nServing from: {path}\nUse Ctrl+C to stop...\n\n".format(
                addr=self._addr, port=int(self._port), path=Path(report_path).absolute()
            )
        )
        httpd = HTTPServer(server_address, handler_class)
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            pass
        return


### 🔴 Znowu wyrażenia listowe?!

# Wiesz już, że wyrażenia listowe mogą przybierać jedną z dwóch form:
#   [_ for _ in _]
# lub:
#   [_ for _ in _ if _]

# Okazuje się jednak, że wyrażenia listowe pozwalają na zastąpienie nawet dwóch lub więcej zagnieżdżonych pętli for, dzięki czemu możesz iterować po zagnieżdżonych listach!

# Weźmy poniższy problem - chcemy zsumować te liczby:

matrix = [
    [1, 5, 3],
    [4, 9, 10],
    [9, 7, 5],
]

# print(sum(matrix))  # ❌ nie zadziała

# Niestety, powyższy kod nie działa, bo funkcja sum zakłada, że ma doczynienia z PŁASKĄ listą, tzn. po prostu listą liczb. Tymczasem my mamy zagnieżdżone listy.

# Jak możemy stworzyć płaską listę? Na przykład tak jak poniżej:

flat = []
for row in matrix:
    for elem in row:
        flat.append(elem)
print('flat =', flat)
print(sum(flat))

# Ale można szybciej, używając wyrażeń listowych:

flat = [elem for row in matrix for elem in row]
print(sum(flat))

### 🔴 Ćwiczenie

# Masz listę komentarzy. Każdy komentarz reprezentujesz jako listę słów.

# Zbuduj listę zawierającą wszystkie słowa ze wszystkich recenzji. Chodzi o płaską, tzn. niepozagnieżdżaną listę. Taka lista może być później używana dalej.

# Napisz testy!

def get_unique_words(comments):
    return list(set([w for words in comments for w in words]))

def main():
    comments = [
        ['pierwszy', 'komentarz'],
        ['drugi', 'komentarz'],
    ]
    words = get_unique_words(comments)
    print('words=', words)

if __name__ == '__main__':
    main()


from random import randint

import pygame

pygame.init()
W = 300
H = 300
WHITE = (255, 255, 255)

class Car(pygame.sprite.Sprite):
    def __init__(self, x, filename):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(filename).convert_alpha()
        self.rect = self.image.get_rect(center=(x, 0))

sc = pygame.display.set_mode((W, H))
# координата x будет случайна
car1 = Car(randint(1, W), 'data/hero.png')
car1.rect.x, car1.rect.y = 100, 100
while 1:
    for i in pygame.event.get():
        if i.type == pygame.QUIT:
            exit()
        elif i.type == pygame.KEYDOWN:
            if i.key == pygame.K_UP:
                car1.rect.y -= 10
            if i.key == pygame.K_DOWN:
                car1.rect.y += 10
            if i.key == pygame.K_RIGHT:
                car1.rect.x += 10
            if i.key == pygame.K_LEFT:
                car1.rect.x -= 10
    sc.fill(WHITE)
    sc.blit(car1.image, car1.rect)
    pygame.display.update()


page =('

')

def link_parser(page):
    search_criteria = ['']
    #Points set where program will look for link_parser
    start_link = str(page).find(str(search_criteria[0]))
    #first match for the start part of search
    end_link = str(page)[start_link: ].find(str(search_criteria[1]))
    #first match for second part of search
    link = str(page)[(start_link+len(search_criteria[0])+1):(start_link+end_link-1)]
    #information found between search criteria
    return link

print(link_parser(page))


import pkuseg
import torch
from string import punctuation
from format_utils import format_text, get_train_on_gpu
import sys

# method to predict the sentiment of chinese text

def predict(text, no_stopwords):
    print(f'Text: {text}')

    feature_tensor = format_text(text, no_stopwords)

    batch_size = feature_tensor.size(0)

    # initialize hidden state
    h = net.init_hidden(batch_size)

    # move to gpu if on gpu
    if(get_train_on_gpu()):
        feature_tensor = feature_tensor.cuda()

    # get output from model
    output, h = net(feature_tensor, h)

    # normalize output probs to 0 or 1
    pred = torch.round(output.squeeze())

    # printing output value, before rounding
    print('Prediction value, pre-rounding: {:.6f}'.format(output.item()))

    if pred == 0:
        print('negative')
        return 'Text is negative'
    elif pred == 1:
        print('positive')
        return 'Text is positive'

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

batch_size = 50

net = torch.load('models/torch_rnn_trained.model',
                 map_location=torch.device('cpu'))

predict(sys.argv[1], sys.argv[2])


"""Main CLI method."""
import sys

import click

from eddington import EddingtonException, __version__

class CatchEddingtonExceptions(click.Group):
    """Implementation for a commands group that catches Eddington exceptions."""

    def __call__(self, *args, **kwargs):
        """
        Override of the call method that catches Eddington exceptions.

        :param args: Arguments for the subcommand
        :type args: list
        :param kwargs: Keyword argument for the subcommand.
        :type kwargs: dict
        :return: Returns the output of the subcommand
        """
        try:
            return self.main(*args, **kwargs)
        except EddingtonException as exc:
            click.echo(
                f'{click.style(exc.__class__.__name__, fg="yellow")}: '
                f'{click.style(exc, fg="red")}'
            )
            sys.exit(1)

@click.group("eddington", cls=CatchEddingtonExceptions)
@click.version_option(version=__version__)
def eddington_cli():
    """Command line for Eddington."""


# 병행성(Concurrency)
# 이터레이너, 제너레이터
# iterator, generator

# 파이썬 반복 가능한 타입
# collections, text file, list, Dict, Set, Tuple, unpacking, *args ...: iterable

t = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

# 반복 가능한 이유는 내부적으로 iter(x) 함수가 호출되었다.
for c in t:
    print(c) # A B C D ...

# while문
w = iter(t)
while True:
    try:
        print(next(w)) # 위와 출력값이 같음
    except StopIteration:
        break
# c와 출력값이 똑같은데 while문이 더 길다. 실제로는 for문 사용하면 되는데 어떻게 동작하는지 메커니즘만 확인한 것.

from collections import abc # abc(Abstract Base Class)
# 반복형 확인
# print(dir(t)) # 이러한 방식으로 하면 내가 원하는 값을 찾기 힘듦
print(hasattr(t, '__iter__')) # True
# hasattr(has attribute) : iter속성을 가지고있니?
print(isinstance(t, abc.Iterable)) # true
# isinstance: 인스턴스가 특정 클래스/데이터 타입인지 검사하는 함수.

# next 패턴
class WordSplitter:
    def __init__(self, text):
        self._idx = 0
        self._text = text.split(' ')

    def __next__(self):
        print('Called __next__')
        try:
            word = self._text[self._idx]
        except IndexError:
            raise StopIteration('Stopped Iteration')
        self._idx += 1
        return word

    def __repr__(self):
        return 'WordSplit(%s)' % (self._text)

wi = WordSplitter('ABC DEF GHI JKL')

print(wi) # WordSplit(['ABC', 'DEF', 'GHI', 'JKL'])
print(next(wi))
# Called __next__
# ABC
print(next(wi))
# Called __next__
# DEF
print(next(wi))
# Called __next__
# GHI
print(next(wi))
# Called __next__
# JKL

# generator 패턴
# 1. 지능형 리스트, 딕셔너리, 집합 등을 만들 때 데이터 양 증가하고 메소리 사용량도 증가한다. -> generator 사용 권장
# 2. 단위 실행 가능한 코루틴(Corotine) 구현과 연동
# 3. 작은 메모리 조각 사용

class WordSplitGenerator:
    def __init__(self, text):
        self._text = text.split(' ')

    def __iter__(self):
        for word in self._text:
            yield word # yield가 다음에 나올 위치값 기억
        return

    def __repr__(self):
        return 'WordSplitGenerator(%s)' % (self._text)

wg = WordSplitGenerator('ABC DEF GHI JKL')


import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, precision_recall_curve, average_precision_score
from sklearn.metrics import auc
import numpy as np

def caculate_metric(pred_prob, label_pred, label_real):
    test_num = len(label_real)
    tp = 0
    fp = 0
    tn = 0
    fn = 0

    for index in range(test_num):
        if label_real[index] == 1:
            if label_real[index] == label_pred[index]:
                tp = tp + 1
            else:
                fn = fn + 1
        else:
            if label_real[index] == label_pred[index]:
                tn = tn + 1
            else:
                fp = fp + 1

    # Accuracy
    ACC = float(tp + tn) / test_num

    # Sensitivity
    if tp + fn == 0:
        Recall = Sensitivity = 0
    else:
        Recall = Sensitivity = float(tp) / (tp + fn)

    # Specificity
    if tn + fp == 0:
        Specificity = 0
    else:
        Specificity = float(tn) / (tn + fp)

    # MCC
    if (tp + fp) * (tp + fn) * (tn + fp) * (tn + fn) == 0:
        MCC = 0
    else:
        MCC = float(tp * tn - fp * fn) / (np.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)))

    # ROC and AUC
    FPR, TPR, thresholds = roc_curve(label_real, pred_prob, pos_label=1)  # Default 1 is positive sample
    AUC = auc(FPR, TPR)

    # PRC and AP
    # precision, recall, thresholds = precision_recall_curve(label_real, pred_prob, pos_label=1)
    # AP = average_precision_score(label_real, pred_prob, average='macro', pos_label=1, sample_weight=None)

    # ROC(FPR, TPR, AUC)
    # PRC(Recall, Precision, AP)

    performance = [ACC, Sensitivity, Specificity, AUC, MCC]
    roc_data = None
    prc_data = None
    # roc_data = [FPR, TPR, AUC]
    # prc_data = [recall, precision, AP]
    return performance, roc_data, prc_data

# draw ROC
def ROC(fpr, tpr, roc_auc):
    plt.figure()
    lw = 2
    plt.figure(figsize=(10, 10))
    plt.plot(fpr, tpr, color='darkorange',
             lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
    plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate', fontdict={'weight': 'normal', 'size': 30})
    plt.ylabel('True Positive Rate', fontdict={'weight': 'normal', 'size': 30})
    plt.title('Receiver operating characteristic github_project', fontdict={'weight': 'normal', 'size': 30})
    plt.legend(loc="lower right", prop={'weight': 'normal', 'size': 30})
    plt.show()

# draw PRC
def PRC(recall, precision, AP):
    plt.figure()
    plt.step(recall, precision, color='b', alpha=0.2,
             where='post')
    plt.fill_between(recall, precision, step='post', alpha=0.2,
                     color='b')

    plt.xlabel('Recall')
    plt.ylabel('Precision')
    plt.ylim([0.0, 1.05])
    plt.xlim([0.0, 1.05])
    plt.title('2-class Precision-Recall curve: AP={0:0.2f}'.format(AP))
    plt.show()


from builtins import range
from builtins import object
from proteus import Comm, Profiling
import numpy as np
import numpy.testing as npt
import pytest
import os

comm = Comm.init()
Profiling.procID = comm.rank()

class MockMat(object):
    """ petsc4py-based mock SuperLU Matrix for testing.  Filled like this:

    [ 1 1     ]
    [   1 2   ]
    [     1 3 ]
    [       1 ]
    """

    def __init__(self, n):
        from petsc4py import PETSc
        A = PETSc.Mat().create()
        A.setSizes(n)
        A.setType('aij')
        A.setUp()
        A.assemblyBegin()
        for i in range(n):
            A[i, i] = 1
            if i + 1 < n:
                A[i, i + 1] = i
        A.assemblyEnd()
        self.A = A

    def getCSRrepresentation(self):
        return self.A.getValuesCSR()

    def getSubMatCSRrepresentation(self, start, end):
        from petsc4py import PETSc
        ids = list(range(start, end))
        isg = PETSc.IS().createGeneral(ids)
        B = self.A.createSubMatrix(isg)
        return B.getValuesCSR()

@pytest.mark.LinearAlgebraTools
def test_vec_create():
    """test_vec_create

    Verifies that the proteus.LinearAlgebraTools.Vec constructor
    correctly creates one-dimensional arrays of the given length of
    type double precision and with entries set to zero for several
    trials.
    """
    from proteus.LinearAlgebraTools import Vec
    for n in [1, 10, 100, 1000]:
        x = Vec(n)
        # Vector of length n
        assert x.size == n
        # One-dimensional
        assert x.shape == (n,)
        # Of type double-precision
        assert x.dtype == np.double
        # All entries are zero
        assert np.count_nonzero(x) == 0
        # Verify assignment works
        x[:] = list(range(1, n+1))
        assert np.count_nonzero(x) == n

@pytest.mark.LinearAlgebraTools
def test_mat_create():
    """test_mat_create

    Verifies that the proteus.LinearAlgebraTools.Mat constructor
    correctly creates double-precision matrices of the given
    dimensions and with entries set to zero for several trials.
    """

    from proteus.LinearAlgebraTools import Mat
    for m, n in [(1, 1), (1, 10), (100, 1), (500, 500)]:
        x = Mat(m, n)
        # Matrix containing m*n entries
        assert x.size == m*n
        # Two-dimensional
        assert x.shape == (m, n)
        # Of type double-precision
        assert x.dtype == np.double
        # All entries are zero
        assert np.count_nonzero(x) == 0
        # Assign a row
        x[0, :] = list(range(1, n+1))
        # Assign a column
        x[:, 0] = list(range(1, m+1))
        assert np.count_nonzero(x) == m+n-1

@pytest.mark.LinearAlgebraTools
def test_vec_scalar_math():
    """test_vec_scalar_math

    Verifies that the Vec object created by
    proteus.LinearAlgebraTools.Vec behaves as expected when computing
    basic linear algebra for several trials.
    """

    from proteus.LinearAlgebraTools import Vec

    v1 = Vec(2)
    v1[:] = [1, 2]

    npt.assert_almost_equal(v1.sum(), 3)
    npt.assert_almost_equal(v1.prod(), 2)

    v2 = Vec(2)
    v2[:] = [2, 0.5]

    # v1 + v2
    v_sum = np.asarray([3, 2.5])
    npt.assert_almost_equal(v_sum, v1+v2)

    # v1 + 2*v2
    v_scaled_sum = np.asarray([5, 3])
    npt.assert_almost_equal(v_scaled_sum, v1+2*v2)

    # 0.5*v1 + 1
    v_scalar_sum = np.asarray([1.5, 2])
    npt.assert_almost_equal(v_scalar_sum, 0.5*v1+1)

@pytest.mark.LinearAlgebraTools
def test_mat_vec_math():
    """test_mat_vec_math

    Verifies that the Mat and Vec objects from
    proteus.LinearAlgebraTools behave as expected together when
    computing basic linear algebra for one trial.
    """

    from proteus.LinearAlgebraTools import Vec
    from proteus.LinearAlgebraTools import Mat

    v1 = Vec(2)
    v1[:] = [1, 1]

    m1 = Mat(3, 2)
    m1[0, :] = [1, 2]
    m1[1, :] = [3, 2]
    m1[2, :] = [4, 5]

    # m1*v1
    dot_product = np.asarray([3, 5, 9])
    npt.assert_almost_equal(dot_product, m1.dot(v1))

@pytest.mark.LinearAlgebraTools
def test_superlu_mat():
    """test_superlu_mat

    Simple verification of functionality of SuperLU SparseMat operators.
    """

def compute_norms(h, A, vecs):
    from proteus.LinearAlgebraTools import l2Norm, l1Norm, lInfNorm, rmsNorm
    from proteus.LinearAlgebraTools import wl2Norm, wl1Norm, wlInfNorm
    from proteus.LinearAlgebraTools import energyNorm
    for norm in [l2Norm, l1Norm, lInfNorm, rmsNorm]:
        yield norm.__name__, [norm(x) for x in vecs]
    for norm in [wl2Norm, wl1Norm, wlInfNorm]:
        yield norm.__name__, [norm(x, h) for x in vecs]
    for norm in [energyNorm]:
        yield norm.__name__, [energyNorm(x, A) for x in vecs]

@pytest.mark.LinearAlgebraTools
def test_norm_correctness():
    from math import sqrt
    from proteus.LinearAlgebraTools import Mat
    from proteus.LinearAlgebraTools import l2Norm, l1Norm, lInfNorm, rmsNorm
    from proteus.LinearAlgebraTools import wl2Norm, wl1Norm, wlInfNorm
    from proteus.LinearAlgebraTools import energyNorm

    x = np.ones(2)
    h = np.ones(2)
    A = Mat(2, 2)
    A[0, 0] = 1
    A[1, 1] = 1

    assert l1Norm(x)==2
    assert l2Norm(x)==sqrt(2)
    assert lInfNorm(x)==1
    assert rmsNorm(x)==1
    assert wl1Norm(x,h)==2
    assert wl2Norm(x,h)==sqrt(2)
    assert wlInfNorm(x,h)==1
    assert energyNorm(x,A)==sqrt(2)

@pytest.mark.LinearAlgebraTools
def test_norm_zero():
    """test_norm_zero

    Norm of a zero vector better be close to zero.
    """

    from proteus.LinearAlgebraTools import Vec
    from proteus.LinearAlgebraTools import Mat

    h = Vec(2)
    h[:] = [0.5, 0.5]

    A = Mat(2, 2)
    #needs to be SPD
    A[0, :] = [5, 2]
    A[1, :] = [2, 6]

    v = Vec(2)
    v[:] = [0, 0]

    for name, norms in compute_norms(h, A, [v]):
        n = norms[0]
        assert np.allclose(n,0)

@pytest.mark.LinearAlgebraTools
def test_norm_homogeneity():
    """test_norm_homogeneity

    Test if defined norms in proteus.LinearAlgebraTools obey
    absolute homoegeneity for several trials.
    """

    from proteus.LinearAlgebraTools import Vec
    from proteus.LinearAlgebraTools import Mat

    h = Vec(2)
    h[:] = [0.5,0.5]

    A = Mat(2,2)
    #needs to be SPD
    A[0, :] = [5, 2]
    A[1, :] = [2, 6]

    v1 = Vec(2)
    v1[:] = [1, 1]

    for a in [0.5, -2]:
        for name, norms in compute_norms(h, A, [v1, a*v1]):
            t1, t2 = norms
            assert np.allclose(abs(a)*t1,t2)

@pytest.mark.LinearAlgebraTools
def test_norm_triangle_inequality():
    """test_norm_triangle_inequality

    Test if defined norms in proteus.LinearAlgebraTools obey
    the triangle inequality for several trials.
    """

    from proteus.LinearAlgebraTools import Vec
    from proteus.LinearAlgebraTools import Mat

    h = Vec(2)
    h[:] = [1.0, 1.0]

    #need an SPD matrix for this to be a norm
    A = Mat(2, 2)
    A[0, :] = [4, 2]
    A[1, :] = [2, 5]

    v1 = Vec(2)
    v1[:] = [1, 1]

    v2 = Vec(2)
    v2[:] = [-1, 4]

    for name, norms in compute_norms(h, A, [v1+v2, v1, v2]):
        t1, t2, t3 = norms
        assert t1 <= t2 + t3

@pytest.mark.LinearAlgebraTools
def test_petsc_binary_mat_io():
    """test_petsc_binary_mat_io

    Verifies that binary save/loads on PETSc Mats works correctly.
    """

    from proteus.LinearAlgebraTools import ParMat_petsc4py

    n = 10
    par_bs = 1
    par_n = n
    par_N = n
    par_nghost = 0
    subdomain2global = list(range(n))
    ghosted_csr_mat = MockMat(n)

    M = ParMat_petsc4py(ghosted_csr_mat, par_bs, par_n, par_N, par_nghost, subdomain2global)

    M.save('M.petsc_mat')

    from petsc4py import PETSc

    viewer = PETSc.Viewer().createBinary('M.petsc_mat', 'r')
    A = PETSc.Mat().load(viewer)

    mi, mj, mv = M.csr_rep
    ai, aj, av = A.getValuesCSR()
    npt.assert_equal(mi, ai)
    npt.assert_equal(mj, aj)
    npt.assert_equal(mv, av)

@pytest.mark.LinearAlgebraTools
def test_petsc_load_matrix(tmpdir):
    """test petsc_load_matrix """
    from petsc4py import PETSc as p4pyPETSc
    from proteus import LinearAlgebraTools as LAT

    vals_A =     [5.5,7.1,1.0]
    col_idx_A =  [0 , 1 , 2  ]
    row_idx_A =  [0, 1, 2, 3]
    A = LAT.csr_2_petsc(size = (3,3),
                        csr = (row_idx_A,col_idx_A,vals_A))

    A_tmp = tmpdir.join('A.petsc_mat')
    LAT._petsc_view(A,A_tmp.strpath)
    A_test = LAT.petsc_load_matrix(A_tmp.strpath)
    csr_values = A_test.getValuesCSR()

    assert np.allclose(csr_values[0], row_idx_A)
    assert np.allclose(csr_values[1], col_idx_A)
    assert np.allclose(csr_values[2], vals_A)

    A_test = LAT.petsc_load_matrix('dne.txt')
    assert A_test is None

@pytest.mark.LinearAlgebraTools
def test_petsc_load_vec(tmpdir):
    """test petsc_load_matrix """
    from petsc4py import PETSc as p4pyPETSc
    from proteus import LinearAlgebraTools as LAT

    vals_A = np.array([5.5,7.1,1.0])
    A = p4pyPETSc.Vec()
    A.createWithArray(vals_A)

    A_tmp = tmpdir.join('A.petsc_vec')
    LAT._petsc_view(A,A_tmp.strpath)
    A_test = LAT.petsc_load_vector(A_tmp.strpath)
    vec_values = A_test.getArray()

    assert np.allclose(vec_values, vals_A)

    A_test = LAT.petsc_load_vector('dne.txt')
    assert A_test is None


import os
from pathlib import Path

import click

from nornir_cli.common_commands import (
    WRITE_FILE_OPTIONS,
    common_options,
)

@click.command()
@common_options(WRITE_FILE_OPTIONS)
@click.pass_context
def cli(ctx, filename, content, append, line_feed):
    """
    Write_file, but not from nornir_utils
    """

    try:

        dirname = os.path.dirname(filename)
        Path(dirname).mkdir(parents=True, exist_ok=True)

        content = line_feed * "\n" + content if line_feed else content

        mode = "a+" if append else "w+"

        with open(filename, mode=mode) as f:
            f.write(content)

    except Exception as err:
        raise click.ClickException(err)


from django import forms
from django.utils.translation import gettext_lazy as _

from . import models

class GameControlAdminForm(forms.ModelForm):
    """
    Form for the GameControl object, designed to be used in GameControlAdmin.
    """

    # Ticks longer than 1 hours are possible but don't seem reasonable and would require addtional cleaning
    # logic below
    tick_duration = forms.IntegerField(min_value=1, max_value=3559, help_text=_('Duration of one tick in '
                                                                                'seconds'))

    class Meta:
        model = models.GameControl
        exclude = ('current_tick', 'cancel_checks')
        help_texts = {
            'competition_name': _('Human-readable title of the CTF'),
            'services_public': _('Time at which information about the services is public, but the actual '
                                 'game has not started yet'),
            'valid_ticks': _('Number of ticks a flag is valid for'),
            'flag_prefix': _('Static text prepended to every flag'),
            'registration_confirm_text': _('If set, teams will have to confirm to this text (e.g. a link to '
                                           'T&C) when signing up. May contain HTML.'),
            'min_net_number': _('If unset, team IDs will be used as net numbers'),
            'max_net_number': _('(Inclusive) If unset, team IDs will be used as net numbers'),
        }

    def clean_tick_duration(self):
        tick_duration = self.cleaned_data['tick_duration']

        # The timer of the gameserver's Controller component is configured with conditions for the minute
        # and seconds values
        if (tick_duration < 60 and 60 % tick_duration != 0) or \
           (tick_duration > 60 and tick_duration % 60 != 0):
            raise forms.ValidationError(_('The tick duration has to be a multitude of 60!'))

        return tick_duration

    def clean(self):
        cleaned_data = super().clean()

        services_public = cleaned_data.get('services_public')
        start = cleaned_data.get('start')
        end = cleaned_data.get('end')

        if start is not None:
            if services_public is not None and services_public > start:
                raise forms.ValidationError(_('Services public time must not be after start time'))
            if end is not None and end <= start:
                raise forms.ValidationError(_('End time must be after start time'))

        return cleaned_data


from typing import List

class Solution:
    def orangesRotting(self, grid: List[List[int]]) -> int:

        n = len(grid)
        m = len(grid[0])
        m3 = set()
        res = 0

        def dfs(i, j):
            if 0 <= i < n and 0 <= j < m and grid[i][j] == 1:

                grid[i][j] = -1
                if (i < n - 1 and grid[i+1][j] == 2) or (i > 0 and grid[i-1][j] == 2) or (j > 0 and grid[i][j-1] == 2) or (j < m-1 and grid[i][j+1]) == 2:
                    grid[i][j] == 3
                    m3.add((i, j))

                dfs(i+1, j)
                dfs(i-1, j)
                dfs(i, j-1)
                dfs(i, j+1)

                grid[i][j] = 1

        while True:
            m3.clear()
            for i in range(n):
                for j in range(m):
                    if grid[i][j] == 1:
                        dfs(i, j)

            if len(m3) == 0:
                break

            for i, j in m3:
                grid[i][j] = 2

            res += 1

        if any(v == 1 for v in row for row in grid):
            return -1

        return res

g = [[2, 1, 1], [1, 1, 0], [0, 1, 1]]
s = Solution()
print(s.orangesRotting(g))


import cv2
import numpy as np
import os
import random

def trans_label_yolo(img,sx,ex,sy,ey) :
    '''
    sx    # 좌측 최상단 x좌표
    sy    # 좌측 최상단 y좌표
    ex - sx   # Box 가로
    ey - sy   # Box 세로
    '''
    box = [sx,ex,sy,ey]
    img_h,img_w = img.shape[:2]
    dw = 1./img_w     ## 이미지 width
    dh = 1./img_h     ## 이미지 heigh
    x = (box[0] + box[1])/2.0
    y = (box[2] + box[3])/2.0
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = round(x*dw,6)
    w = round(w*dw,6)
    y = round(y*dh,6)
    h = round(h*dh,6)
    return (x,y,w,h)

def create_folder(directory):
    # 폴더 생성 함수
    try:
        if not os.path.exists(directory):
            os.makedirs(directory)
    except OSError:
        print('Error: Creating directory. ' + directory)

# src_path = 'c:/xray/'
result_path_img = 'c:/xray_gen/image/'
result_path_txt = 'c:/xray_gen/label/'
create_folder(result_path_img)
create_folder(result_path_txt)

folder = 'c:/xray/'
foldlist = os.listdir(folder)

for fold in foldlist :
    src_path = folder + fold + '/'
    flist = os.listdir(src_path)

    for file in flist :
        points = []
        count = 0
        src = cv2.imread(src_path+file,1)

        # if src.shape[0] != 512 :
        h,w = src.shape[:2]
        centerh = h/2
        centerw = w/2
        randomx = random.randint(centerh-200,centerh+200)
        randomy = random.randint(centerh-200,centerh+200)
        rand_length = random.randint(30,60)
        triger = random.randint(0,1)
        th = random.randint(80,120)
        dice = random.randint(1,4)
        for i in range(1,dice+1) :
            value = i * 100
            sx = randomx
            sy = randomy + value
            ey = sy + rand_length
            if triger == 0 :
                ex = sx + rand_length
            else :
                ex = sx - rand_length

            if src[sy,sx][0] < 242 and src[ey,ex][0] < 242:

                cv2.line(src, (sx, sy), (ex, ey),(th,th,th),3) # 두께 5
                if triger == 0 :
                    # cv2.rectangle(src, (sx-10,sy-10),(ex+10,ey+10),(255,0,0),3)
                    x,y,w,h = trans_label_yolo(src,sx-10,ex+10,sy-10,ey+10)
                else :
                    # cv2.rectangle(src, (sx+10,sy-10),(ex-10,ey+10),(255,0,0),3)
                    x,y,w,h = trans_label_yolo(src,sx+10,ex-10,sy-10,ey+10)
                points.append(f'0 {x} {y} {w} {h}\n')
                count += 1
            else :
                pass
        if count > 0 :
            label = open(f"{result_path_txt}/{file[:-4]}.txt", 'w')
            cv2.imwrite(result_path_img+file,src)
            for point in points :
                label.write(point)
            label.close()

    # cv2.imshow('img',src)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()


"""This module provides a simple tcp server to connect to the database."""
import argparse
import asyncio
import logging
import os
import signal
import socket
from functools import partial
import hnswlib

from cryptography.fernet import InvalidToken
from luxdb.connection import gen_key, receive_command, send_close, send_result
from luxdb.index import Index
from luxdb.knn_store import KNNStore, open_store

LOG = logging.getLogger('server')

def shutdown(server, signum):
    """Gracefully shut the server down."""
    LOG.info('%s, shutting down', signal.strsignal(signum))
    server.shutdown()

class Server:
    """TCP Server that allows clients to connect to the database.

    Receives commands, executes them on the database and sends the result back.

    """
    def __init__(self, host: str, port: int, store: KNNStore, secret):
        """Define the host and port where the server will listen, and the store that should be used."""
        self.host = host
        self.port = port
        self.store = store
        self.server = None
        self.secret = gen_key(secret)

    async def _next_command(self, reader, writer):
        try:
            return await receive_command(reader, self.secret)
        except InvalidToken:
            LOG.error('Invalid token received, closing connection.')
            await send_close(writer)
            writer.close()
            return None

    async def connection(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
        """Handles a single incoming connection asynchronously"""
        LOG.debug('New connection established.')

        LOG.debug('Waiting for command')
        command = await self._next_command(reader, writer)

        LOG.debug('Command received: %s', command)

        while command is not None:
            result = await command.execute(self.store)
            LOG.debug('Sending result to')

            await send_result(writer, result, self.secret)
            LOG.debug('Sent result to client.')

            LOG.debug('Waiting for command')
            command = await self._next_command(reader, writer)

        writer.close()
        await writer.wait_closed()

    async def create_server(self):
        """Creates the server socket but doesn't start listening yet."""
        self.server = await asyncio.start_server(self.connection, self.host, self.port)

        for server_socket in self.server.sockets:
            host, port = socket.getnameinfo(server_socket.getsockname(), socket.NI_NUMERICSERV | socket.NI_NUMERICHOST)
            LOG.info('Serving on %s:%s', host, port)
            self.host = host
            self.port = int(port)

    def shutdown(self):
        """Gracefully terminate the database server."""
        self.server.close()

    async def start(self, callback=None):
        """Start the server and process incoming connections.

        The callback is called shortly before the server starts listening, you can use it to notify
        clients that the server is listening, so they can connect without an error.
        """
        if self.server is None:
            await self.create_server()

        async with self.server:
            loop = asyncio.get_running_loop()
            if callback is not None:
                loop.call_soon(callback)

            await self.server.start_serving()
            await self.server.wait_closed()

async def serve(args: dict):
    """Main method for the server thread."""

    with open_store(args.path) as store:
        loop = asyncio.get_running_loop()
        server = Server(host=args.host, port=args.port, store=store, secret=args.secret)

        for signum in [signal.SIGTERM, signal.SIGINT]:
            loop.add_signal_handler(signum, partial(shutdown, server, signum))

        await server.start()

async def import_index(args: dict):
    """Command to import the index."""
    with open_store(args.path) as store:
        index = hnswlib.Index(space=args.import_space, dim=args.import_dim)
        index.load_index(args.import_path)
        index = Index(index)
        store.import_index(args.import_name, index)

def main():
    """Main method for the server."""
    parser = argparse.ArgumentParser(description='Multidimensional vector database (server).')

    parser.add_argument('--host', type=str, default='127.0.0.1', help='Host where the server should listen.')
    parser.add_argument('--port', type=int, default=None, help='Port to listen on.')
    parser.add_argument(
        '-log',
        '--loglevel',
        default='warning',
        help='Provide logging level. Example --loglevel debug, default=warning',
    )
    parser.add_argument(
        '--import-path',
        type=str,
        default=None,
        help="Don't run the server, but import an index. Specify the path",
    )
    parser.add_argument(
        '--import-name',
        type=str,
        default=None,
        help="Don't run the server, but import an index. Specify the name",
    )
    parser.add_argument(
        '--import-space',
        type=str,
        default=None,
        help="Don't run the server, but import an index. Specify the space",
    )
    parser.add_argument(
        '--import-dim',
        type=int,
        default=None,
        help="Don't run the server, but import an index. Specify the dimension",
    )
    parser.add_argument('--secret',
                        type=str,
                        default=os.environ.get('LUXDB_SECRET', ''),
                        help='Pass the secret the server should use, if not specified it is taken from $LUXDB_SECRET')
    parser.add_argument('path', type=str, help='Path where the database is stored or should be stored.')
    args = parser.parse_args()
    logging.basicConfig(level=args.loglevel.upper())

    if args.import_path is not None:
        return asyncio.run(import_index(args), debug=args.loglevel.upper() == 'DEBUG')

    return asyncio.run(serve(args), debug=args.loglevel.upper() == 'DEBUG')


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from hmmlearn import hmm
import string
from gensim import corpora
from gensim.utils import simple_preprocess
from utils import prep_text, vec_translate, prep_data
import pickle

def go_through_topics():
    data = pd.read_csv("../merged_summary_topics.csv")
    total = data.shape[0]
    topic_list = [column for column in data.columns if column not in
                  ['Year', 'Speaker', 'Title', 'File', 'Month', 'topic_lists', 'Train',
                   'Month_letter', 'Month', 'Kicker', 'Unnamed: 0', 'Unnamed: 0.1']]
    topics_used = []
    dictionaries = []
    test_data = None
    for topic in topic_list:
        file_name = ('_').join(topic.split(' '))
        df = pd.read_csv(f"../Topic_Data/{file_name}.csv")
        #use only topics that have more than 50 talks
        if df.shape[0] >=50:
            topics_used.append((topic, df.shape[0]))
        else:
            continue

        df_train = df[df['Train'] == 1]
        df_test = df[df['Train'] == 0]
        if test_data is None:
            test_data = df_test
        else:
            test_data.append(df_test)
        #read in all the talks on topic topic
        df_talks = []
        #go through all the talks in the training set
        for filename in df_train["File"]:
            with open("../" + filename, "r") as f:
                text = f.read()
                processed = simple_preprocess(text)
                if len(text):
                    df_talks.append(processed)
        #concatenate the talks
        df_text = sum(df_talks, start=[])

        #create the dictionary
        dictionary = corpora.Dictionary([df_text])
        dictionaries.append(dictionary)
        #minimize the aic to choose the optimal number of components
        components, AIC = hyperparameter_states(df_text, dictionary, np.arange(2, 6), df_talks)

        #create the best model
        best_model = hmm.MultinomialHMM(n_components=components, n_iter=100)
        #train the model
        best_model.fit(prep_text(df_text, dictionary))

        #save the model
        with open(f"{topic}bestModel", 'wb') as file:
            pickle.dump(best_model, file)
    return topic_list, test_data, dictionaries, total

def hyperparameter_states(text, dictionary, list_of_states, talks):
    def calculate_aic(K, score):
        aic = 2*K - 2*score
        return aic
    best_aic = np.inf
    best_state = None
    for num in list_of_states:
        model = hmm.MultinomialHMM(n_components=num, n_iter=100, tol=1e-3)
        model.fit(prep_text(text, dictionary))
        score = model.score(prep_text(talks[-1], dictionary))
        new_aic = calculate_aic(num, score)
        if new_aic < best_aic:
            best_aic = new_aic
            best_state = num
    return best_state, best_aic

topic_list, test_data, dictionaries, total = go_through_topics()

with open('topic_list.txt', 'a') as file:
    file.write(str(topic_list))
test_data.to_json('topic_hmm_test_data.json')