from PySide2 import QtWidgets
from numpy import double

class App(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.base_dict = {"DECI" : 10, "HEXA" : 16, "BIN" : 2, "OCTAL" : 8}
        self.base_list = ["DECI","HEXA", "BIN", "OCTAL"]
        self.input_mask_list = [".","0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
        self.setWindowTitle("Mathematique appliqué en informatique")
        self.setup_ui()
        self.set_default_values()
        self.setup_connections()
        self.setup_css()

    def setup_ui(self):
        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout2 = QtWidgets.QHBoxLayout(self)
        self.spn_number_to_convert = QtWidgets.QLineEdit()
        self.cbb_base_start = QtWidgets.QComboBox()
        self.text_to = QtWidgets.QLabel(" to ")
        self.cbb_base_target = QtWidgets.QComboBox()
        self.btn_convert =QtWidgets.QPushButton("Convertir, explication apparaitrons en dessous : ")
        self.text_box = QtWidgets.QLabel("")
        self.text_box.setFrameStyle(QtWidgets.QFrame.Panel | QtWidgets.QFrame.Plain)
        self.text_box.setLineWidth(1)

        self.layout2.addWidget(self.spn_number_to_convert)
        self.layout2.addWidget(self.cbb_base_start)
        self.layout2.addWidget(self.text_to)
        self.layout2.addWidget(self.cbb_base_target)

        self.layout.addLayout(self.layout2)
        self.layout.addWidget(self.btn_convert)
        self.layout.addWidget(self.text_box)

    def set_default_values(self):

        self.cbb_base_target.addItems(self.base_list)
        self.cbb_base_start.addItems(self.base_list)
        self.text_to.adjustSize()

    def setup_connections(self):
        self.cbb_base_start.activated.connect(self.compute_base)
        self.spn_number_to_convert.textChanged.connect(self.compute_base)
        self.btn_convert.clicked.connect(self.compute_calcul)

    def compute_base(self):
        base_chosen = self.cbb_base_start.currentText()

        if (base_chosen == "DECI"):
            set = 10
            self.spn_number_to_convert.setText(self.input_mask_correction(set, self.spn_number_to_convert.text()))
        if(base_chosen == "HEXA"):
            set = 16
            self.spn_number_to_convert.setText(self.input_mask_correction(set, self.spn_number_to_convert.text()))
        if(base_chosen == "BIN"):
            set = 2
            self.spn_number_to_convert.setText(self.input_mask_correction(set, self.spn_number_to_convert.text()))
        if(base_chosen == "OCTAL"):
            set = 8
            self.spn_number_to_convert.setText(self.input_mask_correction(set, self.spn_number_to_convert.text()))

    def compute_calcul(self):

        self.hexa_dict = {10 : "A", 11 : "B", 12 : "C", 13 : "D", 14 : "E", 15 : "F"}
        if (self.cbb_base_start.currentText() == "DECI"):
           self.deci_to_()

    def deci_to_(self):
        #Compteur pour ne pas overstack
            string_result = ""
            memoire = 0

            #Variable entier
            diviseur = self.base_dict.get(self.cbb_base_target.currentText())
            string_list = self.spn_number_to_convert.text().split(".")
            resultat_division = int(string_list[0])

            resultat_multiplication = 0.0
            #Variable decimale
            if (len(string_list) > 1):
                if(string_list[1] != ""):
                    resultat_multiplication = string_list[1]
            multiplicateur = diviseur

            #Premiere ligne du string
            string_result += " Le resultat est a lire de bas vers le haut pour les"
            string_result += "\n entiers puis de haut vers le bas pour les decimals!"
            string_result += "\n A droite pour les entiers et a gauche pour les decimals!"
            string_result += "\n Ceci est un aide au devoir pour vous verifier!"
            string_result += "\n _________________________________________________________"
            string_result += "\n Entier           Decimal"
            string_result += f"\n{resultat_division} | {diviseur}"

            i = len(string_result)
            while (i < 20):
                string_result += " "
                i = i+1
            string_result += f"       . {resultat_multiplication} x {multiplicateur}\n"

            #Deuxieme ligne du string
            for i in f"{resultat_division} | {diviseur}":
                string_result += "-"
            i = len(f"{resultat_division} | {diviseur}")
            while (i < 20):
                string_result += " "
                i = i+1
            for i in f"0 ,| {resultat_multiplication} x {multiplicateur}":
                string_result += "-"
            string_result += "\n"

            carac_min = len(f"{resultat_division}")
            #Deux opération en parralle
            while (resultat_division != 0 or (resultat_multiplication != 0 and memoire < 20)):

                curr_string = ""

                #Calcul Entier
                if(resultat_division != 0):
                    reste_division = resultat_division % diviseur

                    if (reste_division >9):
                        reste_division = self.hexa_dict.get(reste_division)

                    resultat_division = resultat_division // diviseur
                    i=len(f"{resultat_division}")
                    while (i < carac_min):
                        curr_string += "  "
                        i=i+1
                    curr_string +=f"{resultat_division} | {reste_division}"

                #Filling_space
                i = len(curr_string)
                while (i < 20):
                    curr_string += " "
                    i = i+1

                #Calcul decimal avec un stack overflow limit de 20 si périodique
                if(resultat_multiplication != 0 and memoire < 20):
                    memoire = memoire +1
                    resultat_multiplication = float(f"0.{resultat_multiplication}")
                    resultat_multiplication = resultat_multiplication*multiplicateur

                    res_list = f"{resultat_multiplication}".split(".")
                    res_entier = int(res_list[0])
                    resultat_multiplication = int(res_list[1])

                    if (res_entier >9):
                        res_entier = self.hexa_dict.get(res_entier)

                    curr_string += f" {res_entier} | {resultat_multiplication}"
                curr_string += "\n"
                string_result += curr_string
            self.text_box.setText(string_result)

    def input_mask_correction(self, input_mask_range, input_to_correct):
        onePoint= False
        input_corrected = ""
        for e in input_to_correct:
            i=0
            while (i <=input_mask_range):

                if (self.input_mask_list[i] == e.upper()):
                    if (i==0):
                        if(onePoint == False):
                            onePoint=True
                            input_corrected += e
                        else:
                            i = 20
                    else :
                        input_corrected += e
                        i = 20
                i = i+1

        return input_corrected

    def setup_css(self):
        pass

app = QtWidgets.QApplication([])
win = App()
win.show()
app.exec_()


"""Pyrgo configuration."""
from __future__ import annotations

import pathlib
import sys
from dataclasses import dataclass
from typing import Any

import loguru
import tomli

class PyProjectNotFoundError(Exception):
    """Raised when `pyproject.toml` not found."""

    def __init__(self, path: pathlib.Path) -> None:  # noqa: D107
        super().__init__(f"`pyproject.toml` not found at {path.as_posix()}")

def _configure_logger(logger: loguru.Logger) -> loguru.Logger:
    logger.remove()
    fmt = "<lvl>[{level}]</lvl> {message} <green>{name}:{function}:{line}</green> @ {time:HH:mm:ss}"  # noqa: E501
    logger.add(sys.stderr, format=fmt)
    return logger

@dataclass(frozen=True)
class PyrgoConf:
    """Pyrgo configuration."""

    cwd: pathlib.Path
    requirements: pathlib.Path
    logger: loguru.Logger
    relevant_paths: list[str]
    artifacts: list[pathlib.Path]
    caches: list[pathlib.Path]
    core_deps_alias: str
    env_groups: set[str]

    @classmethod
    def new(cls: type[PyrgoConf]) -> PyrgoConf:
        """Create new configuration instance."""
        logger = _configure_logger(logger=loguru.logger)
        cwd = pathlib.Path().cwd()
        pyproject_path = cwd.joinpath("pyproject.toml")
        if not (pyproject_path.exists() and pyproject_path.is_file()):
            raise PyProjectNotFoundError(path=cwd)

        pyproject_data = tomli.loads(pyproject_path.read_text())
        relevant_paths: list[str] = [
            pyproject_data["project"]["name"].strip().replace("-", "_"),
        ]
        relevant_paths.extend(
            pyproject_data["tool"]["pytest"]["ini_options"]["testpaths"],
        )
        pyrgo_config = pyproject_data["tool"].get("pyrgo", None)
        caches = [
            cwd.joinpath(".pytest_cache"),
            cwd.joinpath(".ruff_cache"),
            cwd.joinpath(".mypy_cache"),
        ]
        if pyrgo_config is not None:
            extra_paths = pyrgo_config.get("extra-paths", None)
            extra_caches = pyrgo_config.get("extra-caches", None)
            if extra_paths is not None:
                relevant_paths.extend(extra_paths)
            if extra_caches is not None:
                caches.extend(cwd.joinpath(extra) for extra in extra_caches)

        core_deps_alias = "core"
        env_groups = {core_deps_alias}
        op_deps: dict[str, Any] | None = pyproject_data["project"].get(
            "optional-dependencies",
            None,
        )
        if op_deps is not None:
            env_groups = env_groups.union(op_deps.keys())

        return cls(
            cwd=cwd,
            requirements=cwd.joinpath("requirements"),
            logger=logger,
            relevant_paths=relevant_paths,
            artifacts=[cwd.joinpath("dist")],
            caches=caches,
            core_deps_alias=core_deps_alias,
            env_groups=env_groups,
        )

    def locked_envs(self) -> set[str]:
        """Get a set of available locked envs."""
        locked_envs: set[str] = set()
        for env in self.requirements.glob(pattern="*.txt"):
            locked_envs.add(
                env.relative_to(self.requirements).as_posix().removesuffix(".txt"),
            )

        return locked_envs


import unittest
import os
import json
import time
import uuid

from os import environ
from ConfigParser import ConfigParser
from pprint import pprint

from installed_clients.WorkspaceClient import Workspace as workspaceService
from kb_gblocks.kb_gblocksImpl import kb_gblocks

class kb_gblocksTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        token = environ.get('KB_AUTH_TOKEN', None)
        cls.ctx = {'token': token, 'provenance': [{'service': 'kb_gblocks',
            'method': 'please_never_use_it_in_production', 'method_params': []}],
            'authenticated': 1}
        config_file = environ.get('KB_DEPLOYMENT_CONFIG', None)
        cls.cfg = {}
        config = ConfigParser()
        config.read(config_file)
        for nameval in config.items('kb_gblocks'):
            cls.cfg[nameval[0]] = nameval[1]
        cls.wsURL = cls.cfg['workspace-url']
        cls.wsClient = workspaceService(cls.wsURL, token=token)
        cls.serviceImpl = kb_gblocks(cls.cfg)

    @classmethod
    def tearDownClass(cls):
        if hasattr(cls, 'wsName'):
            cls.wsClient.delete_workspace({'workspace': cls.wsName})
            print('Test workspace was deleted')

    def getWsClient(self):
        return self.__class__.wsClient

    def getWsName(self):
        if hasattr(self.__class__, 'wsName'):
            return self.__class__.wsName
        suffix = int(time.time() * 1000)
        wsName = "test_kb_gblocks_" + str(suffix)
        ret = self.getWsClient().create_workspace({'workspace': wsName})
        self.__class__.wsName = wsName
        return wsName

    def getImpl(self):
        return self.__class__.serviceImpl

    def getContext(self):
        return self.__class__.ctx

    def test_kb_gblocks_run_Gblocks_01(self):
        # Prepare test objects in workspace if needed using
        # self.getWsClient().save_objects({'workspace': self.getWsName(), 'objects': []})
        #
        # Run your method by
        # ret = self.getImpl().your_method(self.getContext(), parameters...)
        #
        # Check returned data with
        # self.assertEqual(ret[...], ...) or other unittest methods

        obj_basename = 'gblocks'
        obj_out_name = obj_basename+'.'+"test_output.MSA"
        obj_out_type = 'KBaseTrees.MSA'

        # MSA
        MSA_json_file = os.path.join('data', 'DsrA.MSA.json')
        with open (MSA_json_file, 'r', 0) as MSA_json_fh:
            MSA_obj = json.load(MSA_json_fh)

        provenance = [{}]
        MSA_info = self.getWsClient().save_objects({
            'workspace': self.getWsName(),
            'objects': [
                {
                    'type': 'KBaseTrees.MSA',
                    'data': MSA_obj,
                    'name': 'test_MSA',
                    'meta': {},
                    'provenance': provenance
                }
            ]})[0]

        [OBJID_I, NAME_I, TYPE_I, SAVE_DATE_I, VERSION_I, SAVED_BY_I, WSID_I, WORKSPACE_I, CHSUM_I, SIZE_I, META_I] = range(11)  # object_info tuple
        MSA_ref = str(MSA_info[WSID_I])+'/'+str(MSA_info[OBJID_I])+'/'+str(MSA_info[VERSION_I])

        parameters = { 'workspace_name': self.getWsName(),
		       'desc':           'test_Gblocks',
                       'input_ref':      MSA_ref,
                       'output_name':    obj_out_name,
                       'trim_level':                  "1",
		       'min_seqs_for_conserved':      "0",
		       'min_seqs_for_flank':          "0",
                       'max_pos_contig_nonconserved': "8",
                       'min_block_len':               "10",
                       'remove_mask_positions_flag':  "0"
                     }

        ret = self.getImpl().run_Gblocks(self.getContext(), parameters)[0]
        self.assertIsNotNone(ret['report_ref'])

        # check created obj
        #report_obj = self.getWsClient().get_objects2({'objects':[{'ref':ret['report_ref']}]})[0]['data']
        report_obj = self.getWsClient().get_objects([{'ref':ret['report_ref']}])[0]['data']
        self.assertIsNotNone(report_obj['objects_created'][0]['ref'])

        created_obj_0_info = self.getWsClient().get_object_info_new({'objects':[{'ref':report_obj['objects_created'][0]['ref']}]})[0]
        self.assertEqual(created_obj_0_info[NAME_I], obj_out_name)
        self.assertEqual(created_obj_0_info[TYPE_I].split('-')[0], obj_out_type)

        pass


from ..abstract_sensor import AbstractSensor
import ni_engine.config as config
from ni_engine.storage import DataContainer,data
import quantities as pq
class TemperatureContainer(DataContainer):
    """
    Container for holding temperature measurements. Implements `AbstractMeasurementContainer`
    """

    def __init__(self,ID,temperature,max_stored_data=-1):
        super(TemperatureContainer,self).__init__(ID,max_stored_data=-1)
        self['temperature'] = temperature

    @property
    def temperature(self):
        """
        Returns
        -------
        data
            returns the temperature measurement
        """

        return self['temperature']

class LabJackInternalSensor(AbstractSensor):
    """
    Labjack internal temperature temperature sensor. Is built into the labjack.
    Configuration has options(required if not otherwise noted):

    **Required Parameters:**

    **None**

    *Optional Parameters:**

    **None**
    """
    code = "LABINT"
    name = "Labjack internal temperature sensor"
    description = "Built-in labjack sensor measures temperature"

    def __init__(self,ID,device,name=name,description=description,max_stored_data=-1):
        """
        Parameters
        ----------
        ID : str
        device : AbstractHardware
        name : str
        description : str
        """
        self._device = device
        self._id = ID
        self._name = name
        self._description = description
        self._max_stored_data = -1

    def connect(self):
        """
        Intialize device and connect it to hardware if required
        """
        raise NotImplementedError('Abstract method has not been implemented')

    def measure(self):
        """
        Get a measurement from the device

        Returns
        -------
        TemperatureContainer
        """
        temp = self._device.getTemperature()*pq.K
        measurement = data(self._id,LabJackInternalSensor.code,"Temperature",temp)
        container = TemperatureContainer(self._id,measurement,self._max_stored_data)
        return container

    def disconnect(self):
        """
        Disconnects device from hardware
        """
        raise NotImplementedError('Abstract method has not been implemented')

    def delete(self):
        """
        Deletes itself
        """
        del self

    @classmethod
    def create(cls,configuration,data_handler,device):
        """
        Creates object from configuration and hardware

        Parameters
        ----------

        configuration : Configuration

        device : U3LV

        Returns
        -------

        LabJackInternalSensor

        """
        ID = configuration[config.ID]
        n = LabJackInternalSensor.name
        d = LabJackInternalSensor.description
        if config.NAME in configuration:
            n= configuration[config.NAME]
        if config.DESCRIPTION in configuration:
            n= configuration[config.DESCRIPTION]
        return LabJackInternalSensor(ID,device,n,d)


import os

import cv2
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image, ImageChops

from config_parser.config import CONFIG, IMAGE_PARAMS

try:
    import deformation
    HAS_DEFORMATION_LIB = True
except ModuleNotFoundError:
    HAS_DEFORMATION_LIB = False

W = IMAGE_PARAMS["W"]
H = IMAGE_PARAMS["H"]

TRANSPOSE = lambda x: _transpose_augment_cv(x)
ROTATE = lambda x: _rotate_augment_cv(x, 10, -10)
OFFSET = lambda x: _offest_augment_cv(x, (40, 0), (-40, 0), (0, 40), (0, -40))
NOISE = lambda x: _noise_augment(x, density_noise=0.02)
RANDOM_BLOCK = lambda x: _random_block_augment(x, num_block=30, block_size=(40, 40))
AREA_BLOCK = lambda x: _area_block_augment(x)
DEFORMATION = lambda x: _deformation_augment(x, k=500, kernel_size=(225, 225), sigma=15)
MEDIAN_BLUR = lambda x: _median_blur_augment(x)
GAUSSIAN_BLUR = lambda x: _gaussian_blur_augment(x)
BILATERAL_BLUR = lambda x: _bilateral_blur_augment(x)
AVERAGE_BLUR = lambda x: _average_blur_augment(x)
ALL = [TRANSPOSE, (ROTATE, OFFSET, NOISE, RANDOM_BLOCK, AREA_BLOCK, DEFORMATION)]

def image2array(img_path, augment=ALL):
    """ 将图像转化为向量，可扩增
    augment: list<method or tuple<method>> """

    arrays = []
    origin = _read_img_cv(img_path)
    img_arrs = [origin]

    if augment:
        for mets in augment:
            if isinstance(mets, tuple):
                img_arrs_tmp = []
                for method in mets:
                    img_arrs_tmp += method(img_arrs)
                img_arrs += img_arrs_tmp
            else:
                img_arrs += mets(img_arrs)

    for img_arr in img_arrs:
        arr = _resize_cv(img_arr, (W, H)).reshape((H, W, 1))
        arrays.append(arr)

    return arrays

def _read_img(img_path):
    """ 读取图片 """
    return np.array(Image.open(img_path).convert('L'), dtype=np.uint8)

def _read_img_cv(img_path):
    """ 读取图片 opencv 版 """
    img_path = os.path.normpath(img_path)
    return cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2GRAY)

def _resize(img_arr, shape):
    """ 图片矩阵改变大小 """
    return np.array(Image.fromarray(img_arr, "L").resize(shape), dtype=np.uint8)

def _resize_cv(img_arr, shape, interpolation=cv2.INTER_LINEAR):
    """ 图片矩阵改变大小 opencv 版 """
    return cv2.resize(img_arr, shape, interpolation=cv2.INTER_LINEAR)

def augment(func):
    """ 扩增装饰器，在原图片列表的基础上进行扩增 """
    def new_func(ori_img_arrs, *args, **kw):
        img_arrs = []
        for img_arr in ori_img_arrs:
            img_arrs.extend(func(img_arr, *args, **kw))
        return img_arrs
    return new_func

@augment
def _transpose_augment(img_arr):
    """ 对称扩增 """
    img = Image.fromarray(img_arr, "L")
    return [np.asarray(img.transpose(Image.FLIP_LEFT_RIGHT))]

@augment
def _transpose_augment_cv(img_arr):
    """ 对称扩增 opencv 版 """
    return [cv2.flip(img_arr, 1)]

@augment
def _rotate_augment(img_arr, *angles):
    """ 旋转扩增，可同时扩增多个角度 """
    img = Image.fromarray(img_arr, "L")
    return [np.asarray(img.rotate(angle)) for angle in angles]

@augment
def _rotate_augment_cv(img_arr, *angles):
    """ 旋转扩增 opencv 版 """
    h, w = img_arr.shape
    center = (w//2, h//2)
    return [cv2.warpAffine(img_arr, cv2.getRotationMatrix2D(center, angle, 1), (w, h)) for angle in angles]

@augment
def _offest_augment(img_arr, *offsets):
    """ 平移扩增 """
    img = Image.fromarray(img_arr, "L")
    return [np.asarray(ImageChops.offset(img, off_x, off_y)) for off_x, off_y in offsets]

@augment
def _offest_augment_cv(img_arr, *offsets):
    """ 平移扩增 opencv 版 """
    h, w = img_arr.shape
    return [cv2.warpAffine(img_arr, np.float32([[1, 0, off_x], [0, 1, off_y]]), (w, h)) for off_x, off_y in offsets]

@augment
def _noise_augment(img_arr, density_noise):
    """ 椒盐噪声扩增 """
    h, w = img_arr.shape
    noise_arr = img_arr.copy()
    noise_white_mask = np.random.rand(h, w) < density_noise
    noise_black_mask = np.random.rand(h, w) > density_noise
    noise_white_mask = noise_white_mask.astype(np.uint8) * 255
    noise_black_mask = noise_black_mask.astype(np.uint8) * 255
    noise_arr |= noise_white_mask
    noise_arr &= noise_black_mask
    return [noise_arr]

@augment
def _random_block_augment(img_arr, num_block, block_size):
    """ 随机遮挡扩增 """
    h, w = img_arr.shape
    block_arr = img_arr.copy()
    thumbnail_mask = np.random.randint(
        0, num_block, ((h//block_size[0], w//block_size[1]))) > 1
    block_mask = _resize_cv(thumbnail_mask.astype(np.uint8), (w, h)) * 255
    block_arr &= block_mask
    return [block_arr]

@augment
def _area_block_augment(img_arr):
    """ 区域遮挡扩增 """
    h, _ = img_arr.shape
    block_arr = img_arr.copy()
    x_start = np.random.randint(h//3, h//3*2)
    x_end = np.random.randint(x_start, min(x_start+h//5, h))
    block_arr[x_start: x_end] = 0
    return [block_arr]

@augment
def _deformation_augment(img_arr, k=500, kernel_size=(225, 225), sigma=15):
    """ 弹性形变扩增 """
    h, w = img_arr.shape
    matrix_h = np.random.uniform(low=-1.0*k, high=1.0*k, size=(h, w))
    matrix_v = np.random.uniform(low=-1.0*k, high=1.0*k, size=(h, w))
    matrix_h = cv2.GaussianBlur(matrix_h, kernel_size, sigma).astype(np.int32)
    matrix_v = cv2.GaussianBlur(matrix_v, kernel_size, sigma).astype(np.int32)
    defor_arr = np.zeros((h, w), dtype=np.uint8)

    if HAS_DEFORMATION_LIB:
        deformation.deformation_change_matrix(defor_arr, img_arr, matrix_h ,matrix_v)
    else:
        for i in range(h):
            for j in range(w):
                new_i = i + int(matrix_v[i][j])
                new_j = j + int(matrix_h[i][j])
                if new_i in range(h) and new_j in range(w):
                    defor_arr[new_i][new_j] = img_arr[i][j]
    return [defor_arr]

@augment
def _deformation_augment_v2(img_arr, k=500, kernel_size=(225, 225), sigma=15):
    """ 弹性形变扩增 v2 ，暂不可用 """
    h, w = img_arr.shape
    matrix_h = np.random.uniform(low=-1.0*k, high=1.0*k, size=(h, w))
    matrix_v = np.random.uniform(low=-1.0*k, high=1.0*k, size=(h, w))
    matrix_h = cv2.GaussianBlur(matrix_h, kernel_size, sigma).astype(np.int32)
    matrix_v = cv2.GaussianBlur(matrix_v, kernel_size, sigma).astype(np.int32)
    matrix_x = np.concatenate([np.arange(0, h).reshape((h, 1))
                               for _ in range(w)], axis=1)
    matrix_y = np.concatenate([np.arange(0, w).reshape((1, w))
                               for _ in range(h)], axis=0)
    matrix_x += matrix_v
    matrix_y += matrix_h
    matrix_x = np.where(matrix_x > 0, matrix_x, 0)
    matrix_x = np.where(matrix_x < h, matrix_x, h)
    matrix_y = np.where(matrix_y > 0, matrix_y, 0)
    matrix_y = np.where(matrix_y < w, matrix_y, w)
    img_triplet = np.stack([matrix_x, matrix_y, img_arr],
                           axis=-1).reshape((w*h, 3))
    img_triplet = np.array(sorted(img_triplet, key=lambda t: t[0]*10000+t[1]))
    defor_arr = img_triplet.reshape((h, w, 3))[:, :, 2].astype(np.uint8)
    return [defor_arr]

@augment
def _median_blur_augment(img_arr):
    """ 中值滤波扩增 """
    return [cv2.medianBlur(img_arr, 5)]

@augment
def _gaussian_blur_augment(img_arr):
    """ 高斯滤波扩增 """
    return [cv2.GaussianBlur(img_arr, (7, 7), 0)]

@augment
def _bilateral_blur_augment(img_arr):
    """ 双边滤波扩增 """
    return [cv2.bilateralFilter(img_arr, 40, 75, 75)]

@augment
def _average_blur_augment(img_arr):
    """ 均值滤波扩增 """
    return [cv2.blur(img_arr, (5, 5))]

def conv2(img, kernel):
    """ 对图片矩阵进行卷积处理
    ref: https://blog.csdn.net/u013044310/article/details/82786957
    """
    H, W = img.shape
    kernel_size = kernel.shape
    col = np.zeros((H, kernel_size[1]//2))
    row = np.zeros((kernel_size[0]//2, W + kernel_size[1]//2 * 2))
    img = np.insert(img, [W], values=col, axis=1)
    img = np.insert(img, [0], values=col, axis=1)
    img = np.insert(img, H, values=row, axis=0)
    img = np.insert(img, 0, values=row, axis=0)
    res = np.zeros((H, W))
    for i in range(H):
        for j in range(W):
            temp = img[i:i + kernel_size[0], j:j + kernel_size[1]]
            temp = np.multiply(temp, kernel)
            res[i][j] = temp.sum()
    return res

def img_plot(*img_arrays, shape=(H, W)):
    """ 每张图片 reshape，之后同时并列显示多张图片 """
    img_arrays = [np.reshape(img_array, shape) for img_array in img_arrays]
    img = np.concatenate(img_arrays, axis=1)
    plt.imshow(img, cmap='gray')
    plt.show()


import argparse
import json

import numpy
import os
from gensim.models import KeyedVectors
from gensim.utils import to_unicode

from Fusion_embedding.src.evaluation.check_pivot_word_neighbors import create_save_pivot
from Fusion_embedding.src.evaluation.vector_space_comparison import create_save_vector_space_comparison
from embedding_vector_similarity.src.embeddings.embeddingmodel import GensimEmbeddingModel
from embedding_vector_similarity.src.post_filtering.postfilters import PostFilters

parser = argparse.ArgumentParser()

parser.add_argument('--vec-1', action='store', dest='vec1_path',
                    help='word2vec model 1', required=True)

parser.add_argument('--vec-2', action='store', dest='vec2_path',
                    help='word2vec model 2', required=True)

parser.add_argument('--min-s', action='store', dest='sim_min', type=float,
                    help='sim_min', required=True)
parser.add_argument('--max-s', action='store', dest='sim_max', type=float,
                    help='sim_max', required=True)
parser.add_argument('--step-s', action='store', dest='sim_steps', type=float,
                    help='sim_steps', required=True)

parser.add_argument('--terms', action='store', dest='terms_path',
                    help='query term list', required=True)

parser.add_argument('--out', action='store', dest='out_path',
                    help='output folder', required=True)

parser.add_argument('--out-eval', action='store', dest='out_eval_path',
                    help='output folder', required=True)

parser.add_argument('--name', action='store', dest='name',
                    help='run nam', required=True)

args = parser.parse_args()

#
# query terms
#
query_term_list = open(args.terms_path, 'r').read().splitlines()
for i in range(len(query_term_list)):
    query_term_list[i] = to_unicode(query_term_list[i])
query_term_list = list(set(query_term_list))  # remove duplicates

#
# gensim models
#
model1 = KeyedVectors.load_word2vec_format(args.vec1_path, binary=False)
model2 = KeyedVectors.load_word2vec_format(args.vec2_path, binary=False)

embedding1 = GensimEmbeddingModel()
embedding1.load_model_in_memory(model1, '')

embedding2 = GensimEmbeddingModel()
embedding2.load_model_in_memory(model2, '')

print(args.vec1_path)
print(args.vec2_path)

#
# eval
#

create_save_vector_space_comparison(base_vectors=embedding1.get_vectors_all(),
                                    experiment_vectors=embedding2.get_vectors_all(),
                                    filename=args.out_eval_path + "all_space_comparison.csv",
                                    name="two_retrofitted")

create_save_pivot(embedding1.word_vectors,
                  embedding2.word_vectors,
                  os.path.join(args.out_eval_path, 'all_pivot_queryterm_'".csv"),
                  "two_retrofitted",
                  query_term_list,
                  numpy.arange(args.sim_min, args.sim_max + 0.0001, args.sim_steps))

pivot_file = open(args.out_eval_path+"all_neighbors_combined.csv", 'w')
pivot_file.write(",v1_weight,v2_weight,neighbor_threshold,,count_total, count_avg\n")

#print("embedding equals: ", numpy.array_equal(embedding1.get_vectors_all(),embedding2.get_vectors_all()))

#
# per threshold - compute similar
#

vec_1_weight = 0.5

for vec_1_weight in numpy.arange(0.1, 0.901, 0.1):

    vec_2_weight = 1 - vec_1_weight

    for filter_value in numpy.arange(args.sim_min, args.sim_max + 0.0001, args.sim_steps):

        term_relterms_withweight1 = embedding1.search_neighbors_cosine(query_term_list, 400)
        term_relterms_withweight2 = embedding2.search_neighbors_cosine(query_term_list, 400)

        result = {}

        for term in term_relterms_withweight1:
            result[term] = [[], []]

            d1 = term_relterms_withweight1[term]
            d2 = term_relterms_withweight2[term]

            if d1[0] == d2[0] and d1[1] == d2[1]:
                print("same result @",term , filter_value,"for:", args.vec2_path)

            for i, sim_term in enumerate(d1[0]):

                sim_score_1 = d1[1][i]
                sim_score_2 = 0

                for t, sim_term_2 in enumerate(d2[0]):
                    if sim_term_2 == sim_term:
                        sim_score_2 = d2[1][i]
                        break

                result[term][0].append(sim_term)
                # arithmetic mean
                result[term][1].append((sim_score_1 * vec_1_weight + sim_score_2 * vec_2_weight)/(vec_1_weight + vec_2_weight))
                # harmonic mean
                #result[term][1].append(pow(
                #    (pow(sim_score_1,-1) * vec_1_weight + pow(sim_score_2, -1) * vec_2_weight)/(vec_1_weight + vec_2_weight),
                #    -1))

        result = PostFilters.filter_embedding_threshold(result, filter_value)

        neighbors = 0
        terms = 0
        sims = 0
        for term, related in result.items():
            terms +=1
            neighbors += len (related[0])
            sims += numpy.sum(related[1])

        #print('avg @',args.vec2_path,vec_1_weight,filter_value,"val: ",neighbors/terms)
        #print('sum @',args.vec2_path,vec_1_weight,filter_value,"val: ",sims)

        #",v1_weight,v2_weight,neighbor_threshold,,count_total, count_avg\n"
        pivot_file.write(os.path.basename(args.vec2_path) + ","+str(vec_1_weight)+ ","+str(vec_2_weight)+
                         "," + str(filter_value) + ",," + str(neighbors)+"," + str(neighbors/terms)+ "\n")

        open(os.path.join(args.out_path,
                          args.name + "w" +
                          str("%0.3f" % vec_1_weight).replace('.', '-') + "_" +
                          str("%0.3f" % filter_value).replace('.', '-') + ".txt"),
             'w') \
            .write(json.dumps(result))


"""
plugin for Trac to give anonymous users authenticated access
using a CAPTCHA
"""

# Plugin for trac 0.11

import random
import sys
import time
import urllib

from componentdependencies import IRequireComponents

from genshi.builder import Markup
from genshi.builder import tag
from genshi.filters.transform import Transformer

from pkg_resources import resource_filename

from skimpyGimpy import skimpyAPI

from trac.core import *
from trac.db import Table, Column
from trac.env import IEnvironmentSetupParticipant
from trac.web import IRequestFilter
from trac.web import ITemplateStreamFilter
from trac.web.api import IAuthenticator
from trac.web.auth import LoginModule
from trac.web.chrome import add_warning
from trac.web.chrome import Chrome
from trac.web.chrome import INavigationContributor
from trac.web.chrome import ITemplateProvider
from trac.config import ListOption
from trac.config import Option

from tracsqlhelper import create_table
from tracsqlhelper import execute_non_query
from tracsqlhelper import get_scalar
from tracsqlhelper import get_table
from tracsqlhelper import insert_update

from utils import random_word
from web_ui import ImageCaptcha

try:
    from acct_mgr.api import AccountManager
    from acct_mgr.web_ui import RegistrationModule
except:
    AccountManager = None

class AuthCaptcha(Component):

    ### class data
    implements(IRequestFilter,
               ITemplateStreamFilter,
               ITemplateProvider,
               IAuthenticator,
               IEnvironmentSetupParticipant,
               IRequireComponents,
               INavigationContributor
               )

    dict_file = Option('captchaauth', 'dictionary_file',
                           default="http://java.sun.com/docs/books/tutorial/collections/interfaces/examples/dictionary.txt")
    captcha_type = Option('captchaauth', 'type',
                          default="png")
    realms = ListOption('captchaauth', 'realms',
                        default="wiki, newticket")
    permissions = { 'wiki': [ 'WIKI_CREATE', 'WIKI_MODIFY' ],
                    'newticket': [ 'TICKET_CREATE' ] }

    xpath = { 'ticket.html': "//div[@class='buttons']" }
    delete = { 'ticket.html': "//div[@class='field']" }

    ### IRequestFilter methods

    def pre_process_request(self, req, handler):
        """Called after initial handler selection, and can be used to change
        the selected handler or redirect request.

        Always returns the request handler, even if unchanged.
        """

        if req.method == 'GET':
            if req.path_info.strip('/') in ['register', 'login'] and req.authname != 'anonymous':
                login_module = LoginModule(self.env)
                login_module._do_logout(req)
                req.redirect(req.href(req.path_info))

        if req.method == 'POST':

            realm = self.realm(req)

            # set the session data for name and email if CAPTCHA-authenticated
            if 'captchaauth' in req.args:
                name, email = self.identify(req)
                for field in 'name', 'email':
                    value = locals()[field]
                    if value:
                        req.session[field] = value
                req.session.save()
                if req.authname != 'anonymous' and realm == 'newticket':
                    req.args['author'] = name
                    if email:
                        req.args['author'] += ' <%s>' % email

            # redirect anonymous user posts that are not CAPTCHA-identified
            if req.authname == 'anonymous' and realm in self.realms:

                if 'captchaauth' in req.args and 'captchaid' in req.args:
                    # add warnings from CAPTCHA authentication
                    captcha = self.captcha(req)
                    if req.args['captchaauth'] != captcha:
                        add_warning(req, "You typed the wrong word. Please try again.")
                        try:
                            # delete used CAPTCHA
                            execute_non_query(self.env, "DELETE FROM captcha WHERE id=%s", req.args['captchaid'])
                        except:
                            pass

                    name, email = self.identify(req)
                    if not name:
                        add_warning(req, 'Please provide your name')
                    if AccountManager and name in AccountManager(self.env).get_users():
                        add_warning(req, '%s is already taken as by a registered user.  Please login or use a different name' % name)

                # redirect to previous location
                location = req.get_header('referer')
                if location:
                    location, query = urllib.splitquery(location)

                    if realm == 'newticket':
                        args = [(key.split('field_',1)[-1], value)
                                for key, value in req.args.items()
                                if key.startswith('field_')]
                        location += '?%s' % urllib.urlencode(args)
                else:
                    location =  req.href()
                req.redirect(location)

        return handler

    # for ClearSilver templates
    def post_process_request(self, req, template, content_type):
        """Do any post-processing the request might need; typically adding
        values to req.hdf, or changing template or mime type.

        Always returns a tuple of (template, content_type), even if
        unchanged.

        Note that `template`, `content_type` will be `None` if:
         - called when processing an error page
         - the default request handler did not return any result

        (for 0.10 compatibility; only used together with ClearSilver templates)
        """
        return (template, content_type)

    # for Genshi templates
    def post_process_request(self, req, template, data, content_type):
        """Do any post-processing the request might need; typically adding
        values to the template `data` dictionary, or changing template or
        mime type.

        `data` may be update in place.

        Always returns a tuple of (template, data, content_type), even if
        unchanged.

        Note that `template`, `data`, `content_type` will be `None` if:
         - called when processing an error page
         - the default request handler did not return any result

        (Since 0.11)
        """
        return (template, data, content_type)

    ### ITemplateStreamFilter method

    def filter_stream(self, req, method, filename, stream, data):
        """Return a filtered Genshi event stream, or the original unfiltered
        stream if no match.

        `req` is the current request object, `method` is the Genshi render
        method (xml, xhtml or text), `filename` is the filename of the template
        to be rendered, `stream` is the event stream and `data` is the data for
        the current template.

        See the Genshi documentation for more information.
        """

        # only show CAPTCHAs for anonymous users
        if req.authname != 'anonymous':
            return stream

        # only put CAPTCHAs in the realms specified
        realm = self.realm(req)
        if realm not in self.realms:
            return stream

        # add the CAPTCHA to the stream
        if filename in self.xpath:

            # store CAPTCHA in DB and session
            word = random_word(self.dict_file)
            insert_update(self.env, 'captcha', 'id', req.session.sid, dict(word=word))
            req.session['captcha'] = word
            req.session.save()

            # render the template
            chrome = Chrome(self.env)
            template = chrome.load_template('captcha.html')
            _data = {}

            # CAPTCHA type
            if self.captcha_type == 'png':
                captcha = tag.img(None, src=req.href('captcha.png'))
            else:
                captcha = Markup(skimpyAPI.Pre(word).data())

            _data['captcha'] = captcha
            _data['email'] = req.session.get('email', '')
            _data['name'] = req.session.get('name', '')
            _data['captchaid'] = req.session.sid
            xpath = self.xpath[filename]
            stream |= Transformer(xpath).before(template.generate(**_data))
            if filename in self.delete:
                stream |= Transformer(self.delete[filename]).remove()

        return stream

    ### methods for ITemplateProvider

    """Extension point interface for components that provide their own
    ClearSilver templates and accompanying static resources.
    """

    def get_htdocs_dirs(self):
        """Return a list of directories with static resources (such as style
        sheets, images, etc.)

        Each item in the list must be a `(prefix, abspath)` tuple. The
        `prefix` part defines the path in the URL that requests to these
        resources are prefixed with.

        The `abspath` is the absolute path to the directory containing the
        resources on the local file system.
        """
        return []

    def get_templates_dirs(self):
        """Return a list of directories containing the provided template
        files.
        """
        return [resource_filename(__name__, 'templates')]

    ### method for IAuthenticator

    """Extension point interface for components that can provide the name
    of the remote user."""

    def authenticate(self, req):
        """Return the name of the remote user, or `None` if the identity of the
        user is unknown."""

        # check for an authenticated user
        login_module = LoginModule(self.env)
        remote_user = login_module.authenticate(req)
        if remote_user:
            return remote_user

        # authenticate via a CAPTCHA
        if 'captchaauth' in req.args and 'captchaid' in req.args:

            # ensure CAPTCHA identification
            captcha = self.captcha(req)
            if captcha != req.args['captchaauth']:
                return

            # ensure sane identity
            name, email = self.identify(req)
            if name is None:
                return
            if AccountManager and name in AccountManager(self.env).get_users():
                return

            # delete used CAPTCHA on success
            try:
                execute_non_query(self.env, "DELETE FROM captcha WHERE id=%s", req.args['captchaid'])
            except:
                pass

            # log the user in
            req.environ['REMOTE_USER'] = name
            login_module._do_login(req)

    ### methods for INavigationContributor

    """Extension point interface for components that contribute items to the
    navigation.
    """

    def get_active_navigation_item(self, req):
        """This method is only called for the `IRequestHandler` processing the
        request.

        It should return the name of the navigation item that should be
        highlighted as active/current.
        """
        return None

    def get_navigation_items(self, req):
        """Should return an iterable object over the list of navigation items to
        add, each being a tuple in the form (category, name, text).
        """
        if req.authname != 'anonymous' and 'captcha' in req.session:
            return [('metanav', '_login', tag.a("Login",
                                               href=req.href.login())),
                    ('metanav', '_register', tag.a("Register",
                                                  href=req.href.register()))]
        return []

    ### methods for IEnvironmentSetupParticipant

    """Extension point interface for components that need to participate in the
    creation and upgrading of Trac environments, for example to create
    additional database tables."""

    def environment_created(self):
        """Called when a new Trac environment is created."""
        if self.environment_needs_upgrade(None):
            self.upgrade_environment(None)

    def environment_needs_upgrade(self, db):
        """Called when Trac checks whether the environment needs to be upgraded.

        Should return `True` if this participant needs an upgrade to be
        performed, `False` otherwise.
        """
        try:
            get_table(self.env, 'captcha')
        except:
            return True
        return False

    def upgrade_environment(self, db):
        """Actually perform an environment upgrade.

        Implementations of this method should not commit any database
        transactions. This is done implicitly after all participants have
        performed the upgrades they need without an error being raised.
        """

        # table of CAPTCHAs
        captcha_table = Table('captcha', key='key')[
            Column('id'),
            Column('word')]
        create_table(self.env, captcha_table)

    ### method for IRequireComponents

    def requires(self):
        """list of component classes that this component depends on"""
        return [ImageCaptcha]

    ### internal methods

    def identify(self, req):
        """
        identify the user, ensuring uniqueness (TODO);
        returns a tuple of (name, email) or success or None
        """
        name = req.args.get('name', None).strip()
        email = req.args.get('email', None).strip()
        return name, email

    def realm(self, req):
        """
        returns the realm according to the request
        """
        path = req.path_info.strip('/').split('/')
        if not path:
            return
        # TODO: default handler ('/')
        return path[0]

    def captcha(self, req):
        return get_scalar(self.env, "SELECT word FROM captcha WHERE id=%s", 0, req.args['captchaid'])


import wx
import service

class Layout(wx.Frame):
    def __init__(self, parent=None, size=(1000, 700), title="Video Temporal Labeling Tool"):
        wx.Frame.__init__(self, parent=parent, size=size, title=title)
        self.service = service.Service(self)

        self.initUI()

    def initUI(self):
        self.initMenu()

        # set up dataset info inputs
        self.datasetInput = wx.TextCtrl(self)

        inputStyle = wx.ALIGN_CENTER | wx.ALL
        datasetInputBox = wx.BoxSizer()
        datasetInputBox.Add(wx.StaticText(self, label="Dataset Path"), 1, inputStyle, border=10)
        datasetInputBox.Add(self.datasetInput, 5, inputStyle, border=10)

        # set up store path inputs
        self.storeInput = wx.TextCtrl(self)
        storeInputBox = wx.BoxSizer()
        storeInputBox.Add(wx.StaticText(self, label="Store Path"), 1, inputStyle, border=10)
        storeInputBox.Add(self.storeInput, 5, inputStyle, border=10)

        # set up input box (left)
        inputBoxLeftStyle = wx.EXPAND | wx.ALL
        inputBoxLeft = wx.BoxSizer(wx.VERTICAL)
        inputBoxLeft.Add(datasetInputBox, 1, inputBoxLeftStyle, border=2)
        inputBoxLeft.Add(storeInputBox, 1, inputBoxLeftStyle, border=2)

        # set up input confirm button
        inputConfirm = wx.Button(self, label="Confirm")
        inputConfirm.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "confirm_input"))

        # set up input box
        inputBoxStyle = wx.ALIGN_CENTER | wx.ALL
        inputBox = wx.BoxSizer()
        inputBox.Add(inputBoxLeft, 6, inputBoxStyle, border=10)
        inputBox.Add(inputConfirm, 1, inputBoxStyle, border=10)

        # default image
        img = wx.Image("./resource/test_frame.jpg", wx.BITMAP_TYPE_JPEG)
        img = wx.Bitmap(img)

        # video infos box
        videoInfoStaticBox = wx.StaticBox(self, label='Video Information')
        videoInfoStaticBoxSizer = wx.StaticBoxSizer(videoInfoStaticBox, wx.VERTICAL)

        self.selectVideo = wx.Choice(self)
        self.selectVideo.Bind(wx.EVT_CHOICE, lambda x: self.service.button_event(x, "selectVideo"))
        videoBoxStyle = wx.EXPAND | wx.ALL

        self.videoFrame = wx.StaticText(self)

        videoGridBox = wx.FlexGridSizer(2, 2, 5, 5)
        videoGridBox.Add(wx.StaticText(self, label="Video: "), 1, videoBoxStyle)
        videoGridBox.Add(self.selectVideo, 3, videoBoxStyle)
        videoGridBox.Add(wx.StaticText(self, label="Current Frame: "), 1, videoBoxStyle)
        videoGridBox.Add(self.videoFrame, 1, videoBoxStyle)

        videoInfoStaticBoxSizer.Add(videoGridBox, 1, wx.EXPAND | wx.ALL, border=5)

        # video control box
        prevVideo = wx.Button(self, label="Previous")
        prevVideo.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "prevVideo"))

        nextVideo = wx.Button(self, label="Next")
        nextVideo.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "nextVideo"))

        videoControlBox = wx.StaticBox(self, label="Video Control")
        videoControlBoxSizer = wx.StaticBoxSizer(videoControlBox, wx.HORIZONTAL)
        videoControlStyle = wx.EXPAND | wx.ALL
        videoControlBoxSizer.Add(prevVideo, 1, videoControlStyle, border=5)
        videoControlBoxSizer.Add(nextVideo, 1, videoControlStyle, border=5)

        # label info box
        labelInfoBox = wx.StaticBox(self, label="Label Information")
        labelInfoBoxSizer = wx.StaticBoxSizer(labelInfoBox, wx.VERTICAL)

        labelInfoGirdBox = wx.GridSizer(2, 2, 5, 5)

        self.selectedStart = wx.StaticText(self, label='0')
        self.selectedEnd = wx.StaticText(self, label='0')

        labelInfoGirdBox.Add(wx.StaticText(self, label="Selected Start Frame: "), videoBoxStyle)
        labelInfoGirdBox.Add(self.selectedStart, videoBoxStyle)
        labelInfoGirdBox.Add(wx.StaticText(self, label="Selected End Frame: "), videoBoxStyle)
        labelInfoGirdBox.Add(self.selectedEnd, videoBoxStyle)

        labelInfoBoxSizer.Add(labelInfoGirdBox, 1, wx.EXPAND | wx.ALL, border=5)

        # control box
        controlStaticBox = wx.StaticBox(self, label="Frame Control")
        controlStaticBoxSizer = wx.StaticBoxSizer(controlStaticBox, wx.VERTICAL)

        prevNFrame = wx.Button(self, label="-FPI")
        prevNFrame.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "prevNFrame"))

        prevFrame = wx.Button(self, label="-1")
        prevFrame.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "prevFrame"))

        nextFrame = wx.Button(self, label="+1")
        nextFrame.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "nextFrame"))

        nextNFrame = wx.Button(self, label="+FPI")
        nextNFrame.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "nextNFrame"))

        buttonStyle = wx.EXPAND | wx.ALL

        controlGridBox = wx.GridSizer(2, 2, 5, 5)
        controlGridBox.Add(prevFrame, buttonStyle)
        controlGridBox.Add(nextFrame, buttonStyle)
        controlGridBox.Add(prevNFrame, buttonStyle)
        controlGridBox.Add(nextNFrame, buttonStyle)

        controlStaticBoxSizer.Add(controlGridBox, 1, wx.EXPAND, border=5)

        # label box
        labelBox = wx.StaticBox(self, label="Labelling Box")
        labelBoxSizer = wx.StaticBoxSizer(labelBox, wx.VERTICAL)

        selectStart = wx.Button(self, label="Start")
        selectStart.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "selectStart"))

        selectEnd = wx.Button(self, label="End")
        selectEnd.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "selectEnd"))

        # select label box
        selectLabelBox = wx.BoxSizer()
        selectLabelBox.Add(selectStart, 1, buttonStyle, border=5)
        selectLabelBox.Add(selectEnd, 1, buttonStyle, border=5)

        selectClose = wx.Button(self, label="Close")
        selectClose.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "selectClose"))

        selectMid = wx.Button(self, label="Mid")
        selectMid.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "selectMid"))

        selectFar = wx.Button(self, label="Far")
        selectFar.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "selectFar"))

        saveLabel = wx.Button(self, label="Save")
        saveLabel.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "saveLabel"))

        selectTypeBox = wx.BoxSizer()
        selectTypeBox.Add(selectClose, 1, buttonStyle, border=5)
        selectTypeBox.Add(selectMid, 1, buttonStyle, border=5)
        selectTypeBox.Add(selectFar, 1, buttonStyle, border=5)

        labelBoxSizer.Add(selectLabelBox, 1, wx.EXPAND, border=5)
        labelBoxSizer.Add(selectTypeBox, 1, wx.EXPAND, border=5)
        labelBoxSizer.Add(saveLabel, 1, wx.EXPAND | wx.ALL, border=5)

        # action type selection
        actionTypeStaticBox = wx.StaticBox(self, label="Action Type")
        self.actionTypeChoice = wx.Choice(self)
        self.actionTypeChoice.Bind(wx.EVT_CHOICE, lambda x: self.service.button_event(x, "selectAction"))
        self.actionTypeChoice.SetItems(['tripping', 'faceoff'])
        self.actionTypeChoice.Selection = 0
        actionTypeStaticBoxSizer = wx.StaticBoxSizer(actionTypeStaticBox)
        actionTypeStaticBoxSizer.Add(self.actionTypeChoice, 1, wx.EXPAND, border=5)

        # info box
        infoBox = wx.BoxSizer(wx.VERTICAL)
        infoBoxStyle = wx.EXPAND
        infoBox.Add(videoInfoStaticBoxSizer, 2, infoBoxStyle, border=5)
        infoBox.Add(videoControlBoxSizer, 2, infoBoxStyle, border=5)
        infoBox.Add(labelInfoBoxSizer, 2, infoBoxStyle, border=5)
        infoBox.Add(controlStaticBoxSizer, 2, infoBoxStyle, border=5)
        infoBox.Add(actionTypeStaticBoxSizer, 1, infoBoxStyle, border=5)
        infoBox.Add(labelBoxSizer, 3, infoBoxStyle, border=5)

        # display box
        self.display = wx.StaticBitmap(parent=self)
        self.display.SetBitmap(img)
        displayBoxStyle = wx.ALIGN_CENTER | wx.ALL
        displayBox = wx.BoxSizer()

        frameBox = wx.StaticBox(self, label="Video Frame")
        frameBoxSizer = wx.StaticBoxSizer(frameBox)
        frameBoxSizer.Add(self.display, 1, wx.EXPAND)

        displayBox.Add(frameBoxSizer, 5, displayBoxStyle, border=10)
        displayBox.Add(infoBox, 2, displayBoxStyle, border=10)

        # set up feedback message
        self.feedbackMessage = wx.StaticText(self, label="Welcome to use Video Temporal Labelling Tool v1.1")
        self.feedbackMessage.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL))

        # construct main layout
        myStyle = wx.ALL | wx.ALIGN_CENTER
        mainBox = wx.BoxSizer(wx.VERTICAL)
        mainBox.Add(self.feedbackMessage, 1, myStyle, border=1)
        mainBox.Add(inputBox, 6, myStyle, border=1)
        mainBox.Add(displayBox, 20, myStyle, border=1)

        # set main box in frame
        self.SetSizer(mainBox)
        self.Centre()

        # accelerator table

    def initMenu(self):
        menuBar = wx.MenuBar()

        fileMenu = wx.Menu()

        # fps selection menu
        fpsMenu = wx.Menu()
        fpsMenu.Append(wx.MenuItem(fileMenu, id=11, text="5", kind=wx.ITEM_RADIO))
        fpsMenu.Append(wx.MenuItem(fileMenu, id=12, text="10", kind=wx.ITEM_RADIO))
        fpsMenu.Append(wx.MenuItem(fileMenu, id=13, text="15", kind=wx.ITEM_RADIO))
        fpsMenu.Append(wx.MenuItem(fileMenu, id=14, text="20", kind=wx.ITEM_RADIO))
        fpsMenu.Append(wx.MenuItem(fileMenu, id=15, text="30", kind=wx.ITEM_RADIO))

        fileMenu.Append(wx.ID_ANY, "Frame Per Interval", fpsMenu)

        menuBar.Append(fileMenu, title="Setting")

        self.SetMenuBar(menuBar)

        self.Bind(wx.EVT_MENU, self.service.menu_event)

    def updateMessage(self, msg):
        self.feedbackMessage.SetLabel("Message: "+msg)
        self.feedbackMessage.SetForegroundColour((0,0,0))

    def updateError(self, error):
        self.feedbackMessage.SetLabel("Error: "+error)
        self.feedbackMessage.SetForegroundColour((255,0,0))

    def updateFrame(self, cv2_frame, no):
        h, w = cv2_frame.shape[:2]
        self.display.SetBitmap(wx.Bitmap.FromBuffer(w, h, cv2_frame))
        self.videoFrame.SetLabel(str(no))

    def updateStartLabel(self, start):
        self.selectedStart.SetLabel(str(start))

    def updateEndLabel(self, end):
        self.selectedEnd.SetLabel(str(end))

class App(wx.App):
    """
        This is only for displaying the UI
    """

    def OnInit(self):
        a = Layout()
        a.Show()
        return True

if __name__ == '__main__':
    try:
        debug = App()
        debug.MainLoop()
    except:
        exit(0)


import os
import soco
from evdev import InputDevice, categorize, ecodes

def play(nfc_number):
    print('start play "' + nfc_number + '"')
    try:
        sonos = {
            s for s in soco.discover() if s.player_name == os.environ["SPEAKER"]
        }.pop()
        playlist = next(
            p
            for p in sonos.music_library.get_music_library_information(
                "sonos_playlists"
            )
            if p.title.endswith(nfc_number)
        )
        sonos.unjoin()
        sonos.clear_queue()
        sonos.add_to_queue(playlist)
        sonos.play_from_queue(0)
    except StopIteration:
        print('found no Playlist for "' + nfc_number + '"')

scancodes = {
    2: u"1",
    3: u"2",
    4: u"3",
    5: u"4",
    6: u"5",
    7: u"6",
    8: u"7",
    9: u"8",
    10: u"9",
    11: u"0",
}

def main():
    accumluator = ""
    devicePath = "/dev/input/event0"
    dev = InputDevice(devicePath)
    dev.grab()

    print("Start listining on " + devicePath)
    for event in dev.read_loop():
        if event.type == ecodes.EV_KEY:
            data = categorize(event)
            if data.keystate == 1:
                if data.scancode == 28:
                    play(accumluator)
                    accumluator = ""
                else:
                    accumluator += scancodes.get(data.scancode)

main()


# encoding=utf8
"""
    Author:  'jdwang'
    Date:    'create date: 2017-04-07'; 'last updated date: 2017-04-07'
    Email:   '383287471@qq.com'
    Describe:
"""
from __future__ import print_function
from sklearn.metrics import f1_score
import numpy as np

__version__ = '1.0'

def get_evaluate_score(y_true, y_predict):
    accuracy = np.mean(y_true == y_predict)
    print('准确度:%f' % accuracy)
    print('错误个数: %d (总:%d)' % (sum(y_true != y_predict), len(y_true)))
    f1 = f1_score(y_true, y_predict, average=None)
    print('F1 score:', f1)
    return accuracy


import random
import pygame

# Initializing Pygame
pygame.init()

# Game window
width, height = 800, 600
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake Game")

# Colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

# properties for building snake
snake_size = 14
snake_speed = 7

# properties for the food
food_size = 20
food_color = BLUE

# Initial position of the snake
x = width // 2
y = height // 2

# Starting velocity
dx = 0
dy = 0

# Food instance
normal_food = None
special_food = None

# Starting position of the food
food_x = random.randint(0, width - food_size)
food_y = random.randint(0, height - food_size)

snake_score = 0
font = pygame.font.Font(None, 24)

snake_segments = []

Time = pygame.time.Clock()
timer_font = pygame.font.Font(None, 16)

def add_segment():
    snake_segments.append((0, 0))

def check_collision():
    if x < 0 or x + snake_size > width or y < 0 or y + snake_size > height:
        return True
    return False

# Timer
start_time = pygame.time.get_ticks()
game_duration = 60  # in secs
food_eaten = 0
max_food_eaten = 10

# Game loop
running = True
Game_over = False
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if not Game_over and event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                dx = -snake_speed
                dy = 0
            elif event.key == pygame.K_RIGHT:
                dx = snake_speed
                dy = 0
            elif event.key == pygame.K_UP:
                dx = 0
                dy = -snake_speed
            elif event.key == pygame.K_DOWN:
                dx = 0
                dy = snake_speed
    if not Game_over:
        current_timing = pygame.time.get_ticks()
        elapsed_timing = (current_timing - start_time) // 1000

        if elapsed_timing >= game_duration:
            Game_over = True

        x += dx
        y += dy

        if x < food_x + food_size and x + snake_size > food_x and y < food_y + food_size and y + snake_size > food_y:
            snake_score += 1
            food_x = random.randint(0, width - food_size)
            food_y = random.randint(0, height - food_size)
            add_segment()

        if len(snake_segments) > 0:
            for i in range(len(snake_segments) - 1, 0, -1):
                snake_segments[i] = snake_segments[i - 1]
            snake_segments[0] = (x, y)

        if check_collision():
            Game_over = True

        window.fill(BLACK)

        pygame.draw.rect(window, GREEN, (x, y, snake_size, snake_size))
        for segment in snake_segments:
            pygame.draw.rect(window, GREEN, (segment[0], segment[1], snake_size, snake_size))

        pygame.draw.rect(window, food_color, (food_x, food_y, food_size, food_size))

        snake_score_text = font.render("Score:" + str(snake_score), True, GREEN)
        window.blit(snake_score_text, (10, 10))

        timer_text = timer_font.render("Time:" + str(game_duration - elapsed_timing), True, GREEN)
        window.blit(timer_text, (10, 30))
        pygame.display.update()
        Time.tick(30)

    else:
        window.fill(BLACK)
        Game_over_text = font.render("Game over", True, GREEN)
        snake_score_text = font.render("Score:" + str(snake_score), True, GREEN)
        window.blit(Game_over_text, (width // 2 - 100, height // 2 - 50))
        window.blit(snake_score_text, (width // 2 - 70, height // 2))
        pygame.display.update()

pygame.quit()


import boto3

rds_data_client = boto3.client('rds-data')
sts_connection = boto3.client('sts')

# assumes mgmt role to set up cognito client associated with mgmt account
def create_cognito_client(mgmt_account_role_arn):
    mgmt_account = sts_connection.assume_role(
        RoleArn=mgmt_account_role_arn,
        RoleSessionName="mgmt_cognito_rds_sync_lambda"
    )
    # create cognito client using the assumed role credentials in mgmt acc
    global cognito_client
    cognito_client = boto3.client(
        'cognito-idp',
        aws_access_key_id=mgmt_account['Credentials']['AccessKeyId'],
        aws_secret_access_key=mgmt_account['Credentials']['SecretAccessKey'],
        aws_session_token=mgmt_account['Credentials']['SessionToken']
    )

# returns list of all users and their 'sub' attribute from userpool - includes enabled status
def get_users_in_userpool(user_pool_id):

    response = cognito_client.list_users(
        UserPoolId=user_pool_id,
    )
    users = response.get('Users')

    while response.get('PaginationToken') is not None:
        response = cognito_client.list_users(
            UserPoolId      = user_pool_id,
            PaginationToken = response.get('PaginationToken')
        )
        users.extend(response.get('Users'))

    return users

# connects to RDS instance and executes the SQL statement passed in
def execute_statement(sql, db_credentials_secrets_store_arn, database_name, db_cluster_arn):
    response = rds_data_client.execute_statement(
        secretArn=db_credentials_secrets_store_arn,
        database=database_name,
        resourceArn=db_cluster_arn,
        sql=sql
    )
    return response


from telegram.ext import Updater, CommandHandler
from selenium import webdriver
from urllib.parse import quote
import os

updater = Updater(token=os.getenv('TELEGRAM_TOKEN'), use_context=True)
dispatcher = updater.dispatcher
driver = webdriver.Chrome(executable_path='/Users/tung491/Downloads/chromedriver')

def start(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")

def uds(update, context):
    word = ' '.join(context.args)
    url = f'https://www.urbandictionary.com/define.php?term={quote(word)}'
    driver.get(url)
    def_panels = driver.find_elements_by_xpath('//div[@class="def-panel "]')
    if def_panels:
        def_panels.remove(def_panels[1])
        for def_panel in def_panels:
            for cls in ['row', 'mug-ad', 'def-footer', 'contributor']:
                rm_element = def_panel.find_element_by_class_name(cls)
                driver.execute_script("""
                                        var element = arguments[0];
                                        element.parentNode.removeChild(element);
                                        """, rm_element)
                with open('definition.png', 'wb') as f:
                    f.write(def_panel.screenshot_as_png)
            context.bot.send_photo(update.effective_chat.id, photo=open('definition.png', 'rb'))
    else:
        context.bot.send_message(chat_id=update.effective_chat.id, text='There is no definition for this word')

if __name__ == '__main__':
    start_handler = CommandHandler('start', start)
    dispatcher.add_handler(start_handler)

    uds_handler = CommandHandler('uds', uds)
    dispatcher.add_handler(uds_handler)
    updater.start_polling()


from django.contrib.auth import get_user_model
from django.test import TestCase

from ..models import Group, Post

User = get_user_model()

class PostModelTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.user = User.objects.create_user(username='auth')
        cls.group = Group.objects.create(
            title='Тестовая группа',
            slug='Тестовый слаг',
            description='Тестовое описание',
        )
        cls.post = Post.objects.create(
            author=cls.user,
            text='Тестовый пост для тестовых тестов',
        )

    def test_model_Group_has_correct_object_names(self):
        """Проверяем, что у моделей корректно работает __str__."""
        self.assertEqual(str(self.group), self.group.title)

    def test_model_Post_has_correct_object_names(self):
        """Проверяем, что у моделей корректно работает __str__."""
        self.assertEqual(str(self.post), self.post.text[:15])

    def test_model_Post_has_correct_verbose(self):
        """Проверка содержания verbose_name"""
        field_verboses = {
            'author': 'Автор',
            'pub_date': 'Время публикации',
            'text': 'Текст поста',
            'image': 'Картинка',
            'group': 'Группа'
        }
        for value, expected in field_verboses.items():
            with self.subTest(value=value):
                self.assertEqual(
                    self.post._meta.get_field(value).verbose_name, expected)

    def test_model_Post_has_correct_help_text(self):
        """Проверка содержания help_text"""
        field_helps = {
            'text': 'Введите текст поста',
            'image': 'Прикрепите картинку',
            'group': 'Выберите группу'
        }
        for value, expected in field_helps.items():
            with self.subTest(value=value):
                self.assertEqual(
                    self.post._meta.get_field(value).help_text, expected)


class Solution:
    def networkDelayTime(self, times, N, K):
        """
        :type times: List[List[int]]
        :type N: int
        :type K: int
        :rtype: int
        """
        from collections import defaultdict
        edges = defaultdict(dict)
        for u, v, w in times:
            edges[u][v] = w
        reach = {K: 0}
        frontier = {K}
        while frontier:
            u = frontier.pop()
            cur_dist = reach[u]
            for v, d in edges[u].items():
                if v not in reach or cur_dist + d < reach[v]:
                    reach[v] = cur_dist + d
                    frontier.add(v)
        if len(reach) == N:
            return max(reach.values())
        else:
            return -1

fn = Solution().networkDelayTime

print(fn([[1, 2, 3], [1, 3, 1], [3, 2, 3]], 3, 1))
print(fn([], 2, 1))


import io
import warnings

import asdf
import asdf_wcs_schemas
import gwcs.extension

import pytest

@pytest.mark.skipif(asdf_wcs_schemas.__version__ < "0.2.0", reason="version 0.2 provides the new manifests")
def test_empty_extension():
    """
    Test that an empty extension was installed for gwcs 1.0.0
    and that extensions are installed for gwcs 1.0.1 and 1.1.0
    """
    extensions = gwcs.extension.get_extensions()
    assert len(extensions) > 1

    extensions_by_uri = {ext.extension_uri: ext for ext in extensions}

    # check for duplicate uris
    assert len(extensions_by_uri) == len(extensions)

    # check that all 3 versions are installed
    for version in ('1.0.0', '1.0.1', '1.1.0'):
        assert f"asdf://asdf-format.org/astronomy/gwcs/extensions/gwcs-{version}" in extensions_by_uri

    # the 1.0.0 extension should support no tags or types
    legacy = extensions_by_uri["asdf://asdf-format.org/astronomy/gwcs/extensions/gwcs-1.0.0"]
    assert len(legacy.tags) == 0
    assert len(legacy.converters) == 0

def test_open_legacy_without_warning():
    """
    Opening a file produced with extension 1.0.0 should not produce any
    warnings because of the empty extension registered for 1.0.0
    """
    asdf_bytes = b"""#ASDF 1.0.0
#ASDF_STANDARD 1.5.0
%YAML 1.1
%TAG ! tag:stsci.edu:asdf/
--- !core/asdf-1.1.0
asdf_library: !core/software-1.0.0 {author: The ASDF Developers, homepage: 'http://github.com/asdf-format/asdf',
  name: asdf, version: 2.9.2}
history:
  extensions:
  - !core/extension_metadata-1.0.0
    extension_class: asdf.extension._manifest.ManifestExtension
    extension_uri: asdf://asdf-format.org/astronomy/gwcs/extensions/gwcs-1.0.0
    software: !core/software-1.0.0 {name: gwcs, version: 0.18.0}
foo: 1
..."""
    with warnings.catch_warnings():
        warnings.simplefilter("error")
        with asdf.open(io.BytesIO(asdf_bytes)) as af:
            assert af['foo'] == 1


from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from com.liu.wine import X_train_std,X_test_std,y_train,y_test
from sklearn.linear_model import LogisticRegression
from com.liu.sklearntest import plot_decision_regions
import matplotlib.pyplot as plt

'''
线性判别分析压缩无监督数据
与PCA相比,LDA是一种更优越的用于分类的特征提取技术
是一种监督降维技术，在线性特征空间中尝试使得类别最大可分时，需要使用
    训练数据集中的类别信息
'''
lda = LinearDiscriminantAnalysis(n_components=2)
print(X_train_std)
X_train_lda = lda.fit_transform(X_train_std,y_train)
lr = LogisticRegression()
lr = lr.fit(X_train_lda,y_train)
plot_decision_regions(X_train_lda,y_train,classifier=lr)
plt.xlabel('LD 1')
plt.ylabel('LD 2')
plt.legend(loc='lower left')
plt.show()

#显示模型在测试数据集上的效果
X_test_lda = lda.transform(X_test_std)
plot_decision_regions(X_test_lda,y_test,classifier=lr)
plt.xlabel('LD 1')
plt.ylabel('LD 2')
plt.legend(loc='lower left')
plt.show()


import numpy as np
import matplotlib.pyplot as plt

# Values of j
j_values = ["0.004000","0.014400","0.028800","0.047600","0.075000"]

# Loop over j values
for t in j_values:
    # Read data from output file
    filename = "q1_b_1024_" + str(t) + ".txt"
    data = np.loadtxt(filename)
    x = np.linspace(0, 1, len(data))
    u = data

    # Plot u vs x
    plt.plot(x, u, label="t = " + str(t))

# Add axis labels and legend
plt.xlabel("x")
plt.ylabel("u")
plt.title("u vs x for N=1024 with alpha=0.001")
plt.legend()

# Show plot
plt.show()

# comparison between 1(a) and 1(b) results
with open('../Q1_a/q1_a_1024_0.047600.txt','r') as file1:
    data1=np.loadtxt(file1)
    x1=np.linspace(0,1,len(data1))
    plt.plot(x1,data1,label="Euler-upwind with t=0.0476")
with open('q1_b_1024_0.047600.txt','r') as file2:
    data2=np.loadtxt(file2)
    x2=np.linspace(0,1,len(data2))
    plt.plot(x2,data2,label="Euler-central difference with t=0.0476")

plt.xlabel("x")
plt.ylabel("u")
plt.title("comparison for u vs x for N=1024 with alpha=0.001 and 0")
plt.legend()

# Show plot
plt.show()


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.graph_objects as go
import seaborn as sns
import plotly.express as px

import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv('/content/threads_reviews.csv')

df

df.head()

df.tail()

df.shape

df.columns

df.isnull().sum()

df.duplicated().sum()

df.drop_duplicates

df.describe()

df.info()

df.nunique()

df['source'].unique()

df['source'].value_counts()

plt.figure(figsize=(5,5))
counts = df['source'].value_counts()
plt.pie(counts,labels = counts.index,autopct='%1.1f%%', colors = sns.color_palette('hls'))
plt.title("Source of reviews")
plt.show()

fig = go.Figure(data=[go.Bar(x=df['source'].value_counts().index, y=df['source'].value_counts())])
fig.update_layout(title='Source of Reviews',xaxis_title="Source",yaxis_title="Count")
fig.show()

counts = df['source'].value_counts()
fig = go.Figure(data=[go.Pie(labels=counts.index, values=counts)])
fig.update_layout(title='Source of Reviews')
fig.show()

df['rating'].unique()

df['rating'].value_counts()

plt.figure(figsize=(15,6))
sns.countplot(x = df['rating'], data = df, palette = 'hls')
plt.show()

plt.figure(figsize=(10, 10))
counts = df['rating'].value_counts()
plt.pie(counts, labels=counts.index, autopct='%1.1f%%', colors=sns.color_palette('hls'))
plt.title('Rating')
plt.show()

fig = go.Figure(data=[go.Bar(x=df['rating'].value_counts().index, y=df['rating'].value_counts())])
fig.update_layout(title='Rating',xaxis_title="Rating",yaxis_title="Count")
fig.show()

counts = df['rating'].value_counts()
fig = go.Figure(data=[go.Pie(labels=counts.index, values=counts)])
fig.update_layout(title='Rating')
fig.show()

df['review_date'] = pd.to_datetime(df['review_date'])

df1 = df.copy()

df1.set_index('review_date', inplace=True)

start_date = df1.index.min()
end_date = df1.index.max()

print("Start Date of Reviews:", start_date)
print("End Date of Reviews:", end_date)

week_delta = pd.Timedelta(days=7)
current_date = start_date

while current_date <= end_date:
    start_week = current_date
    end_week = current_date + week_delta
    current_week_data = df1[(df1.index >= start_week) & (df1.index < end_week)]
    print(current_week_data)
    weekly_counts = current_week_data.resample('D').size()
    print(weekly_counts)
    fig = px.bar(weekly_counts, x=weekly_counts.index, y=weekly_counts.values,
                 labels={'x': 'Date', 'y': 'Number of Reviews'},
                 title=f'Reviews for Week {start_week.strftime("%Y-%m-%d")} to {end_week.strftime("%Y-%m-%d")}')
    fig.update_layout(xaxis_tickangle=-45)
    fig.show()
    current_date += week_delta

daily_counts = df1.resample('D').size()

fig = go.Figure()
fig.add_trace(go.Bar(x=daily_counts.index, y=daily_counts.values,
                     marker_color='skyblue'))
fig.update_layout(title='Number of Reviews Day-wise',
                  xaxis_title='Date',
                  yaxis_title='Number of Reviews',
                  xaxis_tickangle=-45)
fig.show()

fig = go.Figure()
for source_name, source_data in df1.groupby('source'):
    fig.add_trace(go.Scatter(x=source_data.resample('D').size().index, y=source_data.resample('D').size().values,
                            mode='lines', name=source_name))
fig.update_layout(title='Number of Reviews Day-wise',
                  xaxis_title='Date',
                  yaxis_title='Number of Reviews',
                  xaxis_tickangle=-45)
fig.show()

fig = go.Figure()
for source_name, source_data in df1.groupby('source'):
    fig.add_trace(go.Scatter(x=source_data.resample('D').size().index, y=source_data.resample('D').size().values,
                            mode='lines+markers', name=source_name))
fig.update_layout(title='Number of Reviews Day-wise',
                  xaxis_title='Date',
                  yaxis_title='Number of Reviews',
                  xaxis_tickangle=-45)
fig.show()

fig = go.Figure()
for source_name, source_data in df1.groupby('source'):
    fig.add_trace(go.Scatter(x=source_data.resample('D').size().index, y=source_data.resample('D').size().values,
                            mode='markers', name=source_name))
fig.update_layout(title='Number of Reviews Day-wise',
                  xaxis_title='Date',
                  yaxis_title='Number of Reviews',
                  xaxis_tickangle=-45)
fig.show()

fig = go.Figure()
for rating_val, rating_data in df1.groupby('rating'):
    fig.add_trace(go.Scatter(x=rating_data.resample('D').size().index, y=rating_data.resample('D').size().values,
                             mode='lines+markers', name=f'Rating {rating_val}'))
fig.update_layout(title='Number of Reviews Day-wise by Rating',
                  xaxis_title='Date',
                  yaxis_title='Number of Reviews',
                  xaxis_tickangle=-45)
fig.show()

fig = go.Figure()
for rating_val, rating_data in df1.groupby('rating'):
    fig.add_trace(go.Scatter(x=rating_data.resample('D').size().index, y=rating_data.resample('D').size().values,
                             mode='markers', name=f'Rating {rating_val}'))
fig.update_layout(title='Number of Reviews Day-wise by Rating',
                  xaxis_title='Date',
                  yaxis_title='Number of Reviews',
                  xaxis_tickangle=-45)
fig.show()

df['rating']=df['rating'].map({1:-1,2:-1,3:0,4:1,5:1})

df

df_new = df[['review_description', 'rating']]

df_new

df_new['rating'].unique()

df_new['rating'].value_counts()

fig = go.Figure(data=[go.Bar(x=df_new['rating'].value_counts().index, y=df_new['rating'].value_counts())])
fig.update_layout(title='Rating',xaxis_title="Rating",yaxis_title="Count")
fig.show()

counts = df_new['rating'].value_counts()
fig = go.Figure(data=[go.Pie(labels=counts.index, values=counts)])
fig.update_layout(title='Rating')
fig.show()

"""data preprocessing

"""

def clean_text(text):
    text = text.lower()
    return text.strip()

df_new.review_description = df_new.review_description.apply(lambda x: clean_text(x))

df_new.review_description

import string
string.punctuation

def remove_punctuation(text):
    punctuationfree="".join([i for i in text if i not in string.punctuation])
    return punctuationfree

df_new.review_description = df_new.review_description.apply(lambda x:remove_punctuation(x))

df_new.review_description

df_new.review_description = df_new.review_description.apply(lambda x: x.lower())

df_new.review_description

import re

def tokenization(text):
    tokens = re.split(' ',text)
    return tokens

df_new.review_description = df_new.review_description.apply(lambda x: tokenization(x))

df_new.review_description

import nltk
nltk.download('stopwords')
stopwords = nltk.corpus.stopwords.words('english')
stopwords[0:10]
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're"]

def remove_stopwords(text):
    output= " ".join(i for i in text if i not in stopwords)
    return output

df_new.review_description = df_new.review_description.apply(lambda x:remove_stopwords(x))

df_new.review_description

def clean_text(text):
    text = re.sub('\[.*\]','', text).strip()
    text = re.sub('\S*\d\S*\s*','', text).strip()
    return text.strip()

df_new.review_description = df_new.review_description.apply(lambda x: clean_text(x))

df_new.review_description

import spacy
nlp = spacy.load('en_core_web_sm')

stopwords = nlp.Defaults.stop_words
def lemmatizer(text):
    doc = nlp(text)
    sent = [token.lemma_ for token in doc if not token.text in set(stopwords)]
    return ' '.join(sent)

df_new.review_description =  df_new.review_description.apply(lambda x: lemmatizer(x))

def remove_urls(vTEXT):
    vTEXT = re.sub(r'(https|http)?:\/\/(\w|\.|\/|\?|\=|\&|\%)*\b', '', vTEXT, flags=re.MULTILINE)
    return(vTEXT)

df_new.review_description = df_new.review_description.apply(lambda x: remove_urls(x))

def remove_urls(vTEXT):
    vTEXT = re.sub(r'(https|http)?:\/\/(\w|\.|\/|\?|\=|\&|\%)*\b', '', vTEXT, flags=re.MULTILINE)
    return(vTEXT)

def remove_digits(text):
    clean_text = re.sub(r"\b[0-9]+\b\s*", "", text)
    return(text)

df_new.review_description = df_new.review_description.apply(lambda x: remove_digits(x))

def remove_digits1(sample_text):
    clean_text = " ".join([w for w in sample_text.split() if not w.isdigit()])
    return(clean_text)

df_new.review_description = df_new.review_description.apply(lambda x: remove_digits1(x))

def remove_emojis(data):
    emoji_pattern = re.compile("["
                               u"\U0001F600-\U0001F64F"
                               u"\U0001F300-\U0001F5FF"
                               u"\U0001F680-\U0001F6FF"
                               u"\U0001F1E0-\U0001F1FF"
                               "]+", flags=re.UNICODE)
    return re.sub(emoji_pattern, '', data)

df_new.review_description = df_new.review_description.apply(lambda x: remove_emojis(x))

df_new

import wordcloud

from wordcloud import WordCloud
data = df_new.review_description
plt.figure(figsize = (20,20))
wc = WordCloud(max_words = 1000 , width = 1600 , height = 800,
               collocations=False).generate(" ".join(data))
plt.imshow(wc)
plt.axis('off')
plt.show()

from wordcloud import WordCloud
data = df_new[df_new['rating'] == 1]['review_description']
plt.figure(figsize = (20,20))
wc = WordCloud(max_words = 1000 , width = 1600 , height = 800,
               collocations=False).generate(" ".join(data))
plt.imshow(wc)
plt.axis('off')
plt.show()

data = df_new[df_new['rating'] == 0]['review_description']
plt.figure(figsize = (20,20))
wc = WordCloud(max_words = 1000 , width = 1600 , height = 800,
               collocations=False).generate(" ".join(data))
plt.imshow(wc)
plt.axis('off')
plt.show()

data = df_new[df_new['rating'] == -1]['review_description']
plt.figure(figsize = (20,20))
wc = WordCloud(max_words = 1000 , width = 1600 , height = 800,
               collocations=False).generate(" ".join(data))
plt.imshow(wc)
plt.axis('off')
plt.show()

from sklearn.feature_extraction.text import TfidfVectorizer

tf1=TfidfVectorizer()
data_vec=tf1.fit_transform(df_new['review_description'])

data_vec

import pickle
model_filename = 'tfidf_vectorizer.pkl'
with open('mode_filename', 'wb') as model_file:
  pickle.dump(tf1, model_file)

y=df_new['rating'].values

from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.model_selection import train_test_split

X_train,X_test,y_train,y_test=train_test_split(data_vec,y,test_size=0.2,stratify = y, random_state=42)

from imblearn.over_sampling import SMOTE

smote = SMOTE(random_state=42)
X_balanced, y_balanced = smote.fit_resample(X_train, y_train)

X_balanced, y_balanced

from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

sv = SVC()

model_sv = sv.fit(X_balanced, y_balanced)

y_pred_sv = model_sv.predict(X_test)

accuracy = accuracy_score(y_test,y_pred_sv)
print(f'Accuracy: {accuracy:2f}')

model_filename = 'svm_model.pkl'
with open(model_filename, 'wb') as model_file:
  pickle.dump(model_sv, model_file)

dt = DecisionTreeClassifier()

model_dt = dt.fit(X_balanced, y_balanced)

y_pred_dt = model_dt.predict(X_test)

sv = SVC()
dt = DecisionTreeClassifier()
rf = RandomForestClassifier()
ad = AdaBoostClassifier()

models = [sv, dt, rf, ad]

accuracies = []

for model in models:
    print('Results for the model:', model.__class__.__name__)
    model.fit(X_balanced, y_balanced)
    y_pred = model.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)
    print('Accuracy:', accuracy)

    cm = confusion_matrix(y_test, y_pred)
    print('Confusion Matrix:\n', cm)

    report = classification_report(y_test, y_pred)
    print('Classification Report:\n', report)

    print('\n')

    accuracies.append(accuracy)

print('List of Accuracies:', accuracies)

model_names = ['SVC', 'DecisionTree', 'RandomForest', 'AdaBoost']
fig = go.Figure(data=go.Bar(x=model_names, y=accuracies))
fig.update_layout(title='Model Accuracies',
                  xaxis_title='Model',
                  yaxis_title='Accuracy',
                  yaxis_tickformat='.2%',
                  yaxis_range=[0, 1],
                  xaxis_tickangle=0)
fig.show()

import pickle
model_filename = 'svm_model.pkl'
with open(model_filename,'wb') as model_file:
    pickle.dump(sv,model_file)


# coding:utf-8

""" 価値に基づく手法 (Value-based Methods)
    今回は，価値に基づく代表的な手法として，
    ニューラルネットワークを用いて価値関数を近似した手法である．
    Deep Q-Network(DQN)を扱います．
"""

# 必要なライブラリのインポート．
import numpy as np
import copy
from collections import deque
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

""" Deep Q-Network(DQN)
    価値関数をニューラルネットワークによって近似することで，
    状態空間の離散化を行わずに連続値のまま扱うことができます．

    ・経験リプレイ(experience replay)の利用
    　リプレイバッファ(replay buffer)にこれまでの状態遷移を記録しておき，
    　そこからサンプルすることでQ関数のミニバッチ学習をします．

    ・固定したターゲットネットワーク(fixed target Q-network)の利用
    　学習する対象のQ関数と更新によって近づける目標値は，
    　同じパラメータを持つQ関数を利用しているため，そのまま勾配法による最適化を行うと，
    　元のQ値と目標値の両方が更新されてしまいます．
    　これを避けるために，目標値は固定した上でQ関数の最適化を行います．
"""

# Q関数の定義．
class QNetwork(nn.Module):
    def __init__(self, num_state, num_action, hidden_size=16):
        super(QNetwork, self).__init__()
        self.fc1 = nn.Linear(num_state, hidden_size)
        self.fc2 = nn.Linear(hidden_size, hidden_size)
        self.fc3 = nn.Linear(hidden_size, hidden_size)
        self.fc4 = nn.Linear(hidden_size, num_action)

    def forward(self, x):
        h = F.elu(self.fc1(x))
        h = F.elu(self.fc2(h))
        h = F.elu(self.fc3(h))
        y = F.elu(self.fc4(h))
        return y

# リプレイバッファの定義．
class ReplayBuffer:
    def __init__(self, memory_size):
        self.memory_size = memory_size
        self.memory = deque([], maxlen = memory_size)

    def append(self, transition):
        self.memory.append(transition)

    def sample(self, batch_size):
        batch_indexes = np.random.randint(0, len(self.memory), size=batch_size)
        states      = np.array([self.memory[index]['state'] for index in batch_indexes])
        next_states = np.array([self.memory[index]['next_state'] for index in batch_indexes])
        rewards     = np.array([self.memory[index]['reward'] for index in batch_indexes])
        actions     = np.array([self.memory[index]['action'] for index in batch_indexes])
        dones   = np.array([self.memory[index]['done'] for index in batch_indexes])
        return {'states': states, 'next_states': next_states, 'rewards': rewards, 'actions': actions, 'dones': dones}

# DQNエージェントのクラス．
class DqnAgent:
    def __init__(self, num_state, num_action, gamma=0.99, lr=0.001, batch_size=32, memory_size=50000):
        self.num_state = num_state
        self.num_action = num_action
        self.gamma = gamma                          # 割引率．
        self.batch_size = batch_size                # Q関数の更新に用いる遷移の数．
        self.qnet = QNetwork(num_state, num_action)
        self.target_qnet = copy.deepcopy(self.qnet) # ターゲットネットワーク．
        self.optimizer = optim.Adam(self.qnet.parameters(), lr=lr)
        self.replay_buffer = ReplayBuffer(memory_size)

    # Q関数を更新．
    def update_q(self):
        batch = self.replay_buffer.sample(self.batch_size)
        q = self.qnet(torch.tensor(batch["states"], dtype=torch.float))
        targetq = copy.deepcopy(q.data.numpy())

        # maxQの計算．
        maxq = torch.max(self.target_qnet(torch.tensor(batch["next_states"],dtype=torch.float)), dim=1).values

        # targetqのなかで，バッチのなかで実際に選択されていた行動 batch["actions"][i] に対応する要素に対して，Q値のターゲットを計算してセット．
        # 注意：選択されていない行動のtargetqの値はqと等しいためlossを計算する場合には影響しない．
        for i in range(self.batch_size):

            # 終端状態の場合はmaxQを0にしておくと学習が安定します（ヒント：maxq[i] * (not batch["dones"][i])）
            targetq[i, batch["actions"][i]] = batch["rewards"][i] + self.gamma * maxq[i] * (not batch["dones"][i])

        self.optimizer.zero_grad()

        # lossとしてMSEを利用．
        loss = nn.MSELoss()(q, torch.tensor(targetq))
        loss.backward()
        self.optimizer.step()

        # ターゲットネットワークのパラメータを更新．
        self.target_qnet = copy.deepcopy(self.qnet)

    # Q値が最大の行動を選択．
    def get_greedy_action(self, state):
        state_tensor = torch.tensor(state, dtype=torch.float).view(-1, self.num_state)
        action = torch.argmax(self.qnet(state_tensor).data).item()
        return action

    # ε-greedyに行動を選択．
    def get_action(self, state, episode):
        epsilon = 0.7 * (1/(episode+1))  # ここでは0.5から減衰していくようなεを設定．
        if epsilon <= np.random.uniform(0,1):
            action = self.get_greedy_action(state)
        else:
            action = np.random.choice(self.num_action)
        return action


import asyncio
import websockets

HOST = ''
PORT = 7777
lock = asyncio.Lock()

class UserManager:  # 사용자관리 및 채팅 메세지 전송을 담당하는 클래스
    # ① 채팅 서버로 입장한 사용자의 등록
    # ② 채팅을 종료하는 사용자의 퇴장 관리
    # ③ 사용자가 입장하고 퇴장하는 관리
    # ④ 사용자가 입력한 메세지를 채팅 서버에 접속한 모두에게 전송

    def __init__(self):
        self.users = {}  # 사용자의 등록 정보를 담을 사전 {사용자 이름:(소켓,주소),...}

    async def addUser(self, username, conn, addr):  # 사용자 ID를 self.users에 추가하는 함수
        if not username:
            await conn.send('사용자 이름을 등록해주세요\n')
            return None
        if username in self.users:  # 이미 등록된 사용자라면
            await conn.send('이미 등록된 사용자입니다.\n')
            return None
        await lock.acquire()
        try:
            self.users[username] = (conn, addr)
        finally:
            lock.release()

        await self.sendMessageToAll('[%s]님이 입장했습니다.' % username)
        print('+++ 대화 참여자 수 [%d]' % len(self.users))

        return username

    async def removeUser(self, username):  # 사용자를 제거하는 함수
        if username not in self.users:
            return

        await lock.acquire()
        try:
            del self.users[username]
        finally:
            lock.release()

        await self.sendMessageToAll('[%s]님이 퇴장했습니다.' % username)
        print('--- 대화 참여자 수 [%d]' % len(self.users))

    async def messageHandler(self, username, msg):  # 전송한 msg를 처리하는 부분
        if msg[0] != '/':  # 보낸 메세지의 첫문자가 '/'가 아니면
            await self.sendMessageToAll('[%s] %s' % (username, msg))
            return

        if msg.strip() == '/quit':  # 보낸 메세지가 'quit'이면
            await self.removeUser(username)
            return -1

    async def sendMessageToAll(self, msg):
            for username, conn in self.users.items():
                try:
                    await conn[0].send(msg)
                except:
                    self.removeUser(username)
                    pass

    async def registerUsername(self, websocket):
        while True:
            await websocket.send('로그인ID:')
            username = await websocket.recv()
            username = username.strip()
            if await self.addUser(username, websocket, websocket.remote_address[0]):
                return username

async def accept(websocket, path):

    print('[%s] 연결됨' % websocket.remote_address[0])
    try:
        username = await userman.registerUsername(websocket)
        msg = await websocket.recv()
        while msg:
            print(msg)
            print(userman.users.values())
            if await userman.messageHandler(username, msg) == -1:
                break
            msg = await websocket.recv()
        print('[%s] 접속종료' % websocket.remote_address[0])
        await userman.removeUser(username)

    except Exception as e:
        print(e)

#--------------------------------------------------------
    # --------------------------------------------------------
    # --------------------------------------------------------
try:
    userman = UserManager()
    start_server = websockets.serve(accept, "", 7777);

    # 웹 소켓 서버 생성.호스트는 localhost에 port는 9998로 생성한다.

    # 비동기로 서버를 대기한다.
    asyncio.get_event_loop().run_until_complete(start_server)
    asyncio.get_event_loop().run_forever()
except KeyboardInterrupt:
    print('--- 채팅 서버를 종료합니다.')


# Estudiante: Aaron Joel Limachi Quispe
# CI: 12732282
# Problema de Balanar

import numpy as np
import matplotlib.pyplot as plt
import sympy as sp

a, v, r = sp.symbols('a,v,r', real=True)
A, B, t, vo = sp.symbols('A,B,t,vo', positive=True)
# print(A, B, t, v, vo)

# Funcion original
a = A - B*v
print('Aceleracion')
print(a)
# Integrando para obtener la velocidad
i = 1 / (A - B*v)
i1 = sp.integrate(i, (v, vo, v))
# Obteninedo solucion de la ecuacion
v = sp.solve(i1 - t, v)
print('Velocidad')
print(v)

r = sp.integrate(v[0], (t, 0, t))
# print(r)

# Simplificando
r = r.collect((A - B*vo))
print('Desplazamiento')
print(r)

# Establecemos los valores para reemplazar en la ecuacion
val = {A: 10, B: 0.5, vo: 50}
# Creamos el espacio del grafico
t1 = np.linspace(0, 50, 50)

# Reemplazando valores Velocidad vs tiempo
v1 = v[0]
v2 = v1.subs(val)
V = sp.lambdify(t, v2, 'numpy')

# Reemplazando valores distancia vs tiempo
r = r.subs(val)
R = sp.lambdify(t, r, 'numpy')

# Reemplazando valores Aceleracion vs tiempo
Ac = A-B*v[0]
Ac = Ac.subs(val)
Ac = sp.lambdify(t, Ac, 'numpy')

val_4 = {A: 10, B: 0.5, vo: 50, t: 20}
v1_4 = v[0]
v2_4 = v1.subs(val_4)
print('La velocidad en t=20s es igual a: ', v2_4)

# Graficando
fig, axs = plt.subplots(3, figsize=(6, 6))
# fig.suptitle('Graficas para el movimiento de Balanar')
axs[0].plot(t1, Ac(t1))
axs[0].set_title('Aceleracion vs tiempo')
axs[0].set_xlabel('Tiempo(t)')
axs[0].set_ylabel('Aceleracion(a)')
axs[1].plot(V(t1), t1)
axs[1].set_title('Velocidad vs tiempo')
axs[1].set_xlabel('Tiempo(t)')
axs[1].set_ylabel('Velocidad(V)')
axs[2].plot(R(t1), t1)
axs[2].set_xlabel('Tiempo(t)')
axs[2].set_ylabel('Posicion(r)')
axs[2].set_title('Posicion vs tiempo')
plt.subplots_adjust(left=0.1, right=0.95, bottom=0.1, top=0.95, wspace=0.1, hspace=0.6)
plt.show()

# Estudiante: Aaron Joel Limachi Quispe
# CI: 12732282