import datetime
import logging

import numpy as np
import os
import skimage.io as io
from sklearn.cluster import KMeans
from skimage import img_as_float
from skimage.color import rgb2lab
from skimage.exposure import equalize_adapthist
from skimage.filters import gaussian
from skimage.transform import resize

from src.data_loader import sample_names, images, root_dir, FOLDER_EXPERIMENTS
from src.utils import apply_on_normalized_luminance, colormap, outline_regions, average_color

MAX_PATIENTS = 1
MAX_IMAGES_PER_PATIENT = 1
MAX_PATCHES_PER_IMAGE = 2
RESIZE_IMAGES = None    # (300, 300)  # None to deactivate

if __name__ == "__main__":
    execution_id = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
    results_dir = root_dir(FOLDER_EXPERIMENTS(version=3), execution_id)
    os.makedirs(results_dir, exist_ok=True)
    logging.basicConfig(
        level=logging.INFO,
        handlers=[
            logging.FileHandler(os.path.join(results_dir, 'log.txt')),
            logging.StreamHandler()
        ]
    )

    p_names = sample_names()
    for idx_p, p_name in enumerate(p_names[0:MAX_PATIENTS]):

        for idx_img, (path_image, image) in enumerate(images(patient_name=p_name, max_images=MAX_IMAGES_PER_PATIENT)):
            results_p_dir = os.path.join(results_dir, p_name, str(idx_img))
            os.makedirs(results_p_dir, exist_ok=True)
            logging.info(f'Processing: {p_name}-{idx_img}')

            # 01 Preprocessing
            ###

            if RESIZE_IMAGES:
                logging.info('Resizing image')
                image = resize(img_as_float(image), RESIZE_IMAGES + (3, ))
            io.imsave(fname=os.path.join(results_p_dir, '01 01 Original.jpg'),
                      arr=image)

            logging.info('Gaussian filter')
            image = apply_on_normalized_luminance(
                operation=gaussian,
                image_rgb=image)
            io.imsave(fname=os.path.join(results_p_dir, '01 02 Gaussian filter.jpg'),
                      arr=image)

            logging.info('CLAHE')
            image = apply_on_normalized_luminance(
                lambda img: equalize_adapthist(img, clip_limit=0.02),
                image_rgb=image)
            io.imsave(fname=os.path.join(results_p_dir, '01 03 CLAHE.jpg'),
                      arr=image)

            # 02 Lab visualization
            ###

            image_lab = rgb2lab(image)
            luminance = image_lab[:, :, 0]
            a = np.min(luminance)
            b = np.max(luminance - a)
            luminance = (luminance - a) / b
            io.imsave(fname=os.path.join(results_p_dir, '02 01 Luminance.png'),
                      arr=luminance)
            channel_a = image_lab[:, :, 1]
            a = np.min(channel_a)
            b = np.max(channel_a - a)
            channel_a = (channel_a - a) / b
            io.imsave(fname=os.path.join(results_p_dir, '02 01 channel_a.png'),
                      arr=channel_a)
            channel_b = image_lab[:, :, 2]
            a = np.min(channel_b)
            b = np.max(channel_b - a)
            channel_b = (channel_b - a) / b
            io.imsave(fname=os.path.join(results_p_dir, '02 01 channel_b.png'),
                      arr=channel_b)

            # 03-04 Lab separation
            ###

            logging.info('Positive separation (k-means clustering on `a channel`)')
            channel_a = image_lab[:, :, 1]
            clustering = KMeans(n_clusters=2, random_state=0).fit(channel_a.reshape(-1, 1))
            idx_positive_cluster = np.argmax(clustering.cluster_centers_)
            positive_mask = np.equal(clustering.labels_, idx_positive_cluster).reshape(channel_a.shape[0:2])
            io.imsave(fname=os.path.join(results_p_dir, f'03 K-means - Positives - Mask.jpg'),
                      arr=colormap(positive_mask))
            io.imsave(fname=os.path.join(results_p_dir, f'03 K-means - Positives - regions.jpg'),
                      arr=outline_regions(image=image, region_labels=positive_mask))
            io.imsave(fname=os.path.join(results_p_dir, f'03 K-means - Positives - average color.jpg'),
                      arr=average_color(image=image, region_labels=positive_mask))

            logging.info('Negative separation (k-means clustering on `b channel`)')
            channel_b = image_lab[:, :, 2]
            nonpositive_mask = np.logical_not(positive_mask)
            clustering = KMeans(n_clusters=2, random_state=0).fit(channel_b[nonpositive_mask].reshape(-1, 1))
            idx_negative_cluster = np.argmin(clustering.cluster_centers_)
            negative_mask_wrt_nonpositive = np.equal(clustering.labels_, idx_negative_cluster)
            negative_mask = np.full(shape=positive_mask.shape, dtype=bool, fill_value=False)
            nonpositive_mask_as_flatten_idcs = np.where(nonpositive_mask.flatten())
            negative_mask_wrt_nonpositive_flatten = nonpositive_mask_as_flatten_idcs[0][negative_mask_wrt_nonpositive]
            negative_mask = negative_mask.flatten()
            negative_mask[negative_mask_wrt_nonpositive_flatten] = True
            negative_mask = negative_mask.reshape(positive_mask.shape)
            io.imsave(fname=os.path.join(results_p_dir, f'04 K-means - Negatives - Mask.jpg'),
                      arr=colormap(negative_mask))
            io.imsave(fname=os.path.join(results_p_dir, f'04 K-means - Negatives - regions.jpg'),
                      arr=outline_regions(image=image, region_labels=negative_mask))
            io.imsave(fname=os.path.join(results_p_dir, f'04 K-means - Negatives - average color.jpg'),
                      arr=average_color(image=image, region_labels=negative_mask))

            # 05 Results visualization
            ###

            region_labels = negative_mask + 2*positive_mask
            io.imsave(fname=os.path.join(results_p_dir, '05 Results - labels.jpg'),
                      arr=colormap(region_labels))
            io.imsave(fname=os.path.join(results_p_dir, '05 Results - regions.jpg'),
                      arr=outline_regions(image=image, region_labels=region_labels))
            io.imsave(fname=os.path.join(results_p_dir, '05 Results - average_color.jpg'),
                      arr=average_color(image=image, region_labels=region_labels))


import sys

arguments = sys.argv[1:]

def isVowel(char):
    if(char in 'aeiou'):
        return True
    return False

def isAlpha(char):
    return not isVowel(char)

if(len(arguments) < 2):
    print('''let\'s try this.
    python count.py [type] [string]
    [type] is type of char which you want to count in string.
        (vowel / alphabet / digit / lowercase / uppercase)
    [string] is input string.''')
else:
    typechar = arguments[0]
    string = arguments[1]
    count = 0
    if(typechar == 'vowel'):
        for char in string:
            if(isVowel(char)):
                count = count+1
    elif(typechar == 'alphabet'):
        for char in string:
            if(char.isalpha() and isAlpha(char)):
                count = count+1
    elif(typechar == 'digit'):
        for char in string:
            if(char.isdigit()):
                count = count+1
    elif(typechar == 'lowercase'):
        for char in string:
            if(char.islower()):
                count = count+1
    elif(typechar == 'uppercase'):
        for char in string:
            if(char.isupper()):
                count = count+1
    print(count)


def solution(n, k):
    k -= 1
    i = k
    A = list(range(1, n + 1))
    res = []
    while len(A) > 1:
        res.append(A.pop(i))
        i = (i + k) % (len(A))
    return res

print(solution(5, 3))


"""
Writes MESS input for a monte carlo partition function calculation
"""

import os
from mako.template import Template
from mess_io.writer import util

# OBTAIN THE PATH TO THE DIRECTORY CONTAINING THE TEMPLATES #
SRC_PATH = os.path.dirname(os.path.realpath(__file__))
TEMPLATE_PATH = os.path.join(SRC_PATH, 'templates')
SECTION_PATH = os.path.join(TEMPLATE_PATH, 'sections')
MONTE_CARLO_PATH = os.path.join(SECTION_PATH, 'monte_carlo')

def monte_carlo(geom, formula,
                flux_mode_str, data_file_name,
                ground_energy, reference_energy):
    """ Writes a monte carlo species section
    """

    # Format the molecule specification section
    natoms, atom_list = util.molec_spec_format(geom)

    # Indent various strings string if needed
    flux_mode_str = util.indent(flux_mode_str, 4)

    # Create dictionary to fill template
    monte_carlo_keys = {
        'formula': formula,
        'natoms': natoms,
        'atom_list': atom_list,
        'flux_mode_str': flux_mode_str,
        'data_file_name': data_file_name,
        'ground_energy': ground_energy,
        'reference_energy': reference_energy
    }

    # Set template name and path for a monte carlo species section
    template_file_name = 'monte_carlo.mako'
    template_file_path = os.path.join(MONTE_CARLO_PATH, template_file_name)

    # Build monte carlo section string
    mc_str = Template(filename=template_file_path).render(**monte_carlo_keys)

    return mc_str

def fluxional_mode(atom_indices, span=360.0):
    """ Writes the string for each fluxional mode
    """

    # Format the aotm indices string
    atom_indices = util.format_flux_mode_indices(atom_indices)

    # Create dictionary to fill template
    flux_mode_keys = {
        'atom_indices': atom_indices,
        'span': span,
    }

    # Set template name and path for a monte carlo species section
    template_file_name = 'fluxional_mode.mako'
    template_file_path = os.path.join(MONTE_CARLO_PATH, template_file_name)

    # Build monte carlo section string
    flux_str = Template(filename=template_file_path).render(**flux_mode_keys)

    return flux_str


#!/usr/bin/python
# -*- coding: utf-8 -*-
# encoding: utf-8
#客户端调用，用于查看API返回结果

import json
import os
import hashlib
import json

import platform

def getSysType():
    sysSystem = platform.system()
    if sysSystem == 'Windows':  #mac系统
        return 'win'
    elif sysSystem == 'Darwin':
        return 'mac'
    elif sysSystem == 'Linux':
        return 'linux'

def isSignOK(msgdic,secretkey):
    data = json.dumps(msgdic['data']) + str(msgdic['time']) + secretkey
    csgin = msgdic['sign']
    if csgin == 'test':
        return True
    sgin = hashlib.sha256(data.encode("utf8")).hexdigest().upper()
    if csgin == sgin:
        return True
    else:
        return False

def signMsg(msgdic,ptime,secretkey):
    if type(msgdic) == str:
        data = msgdic + str(ptime) + secretkey
        sgin = hashlib.sha256(data.encode("utf8")).hexdigest().upper()
        return sgin
    else:
        data = json.dumps(msgdic) + str(ptime) + secretkey
        sgin = hashlib.sha256(data.encode("utf8")).hexdigest().upper()
        return sgin


import copy
import logging
import traceback
from typing import Any, Dict, Optional, List

from .base_trigger import TriggerEvent, BaseTrigger
from .playbook_utils import merge_global_params
from .playbooks_event_handler import PlaybooksEventHandler
from ..model.events import ExecutionBaseEvent
from ..reporting.base import Finding
from ...model.playbook_action import PlaybookAction
from ...model.config import Registry
from .trigger import Trigger

class PlaybooksEventHandlerImpl(PlaybooksEventHandler):
    def __init__(self, registry: Registry):
        self.registry = registry

    def handle_trigger(self, trigger_event: TriggerEvent) -> Optional[Dict[str, Any]]:
        playbooks = self.registry.get_playbooks().get_playbooks(trigger_event)
        if not playbooks:  # no registered playbooks for this event type
            return

        execution_response = None
        execution_event: Optional[ExecutionBaseEvent] = None
        findings: Dict[str, Finding] = {}
        for playbook in playbooks:
            fired_trigger = self.__get_fired_trigger(trigger_event, playbook.triggers)
            if fired_trigger:
                execution_event = fired_trigger.build_execution_event(
                    trigger_event, findings
                )
                if execution_event:  # might not exist for unsupported k8s types
                    execution_event.named_sinks = (
                        playbook.sinks
                        if playbook.sinks is not None
                        else self.registry.get_playbooks().get_default_sinks()
                    )

                    playbook_resp = self.__run_playbook_actions(
                        execution_event,
                        playbook.get_actions(),
                    )
                    if (
                        playbook_resp
                    ):  # For now, only last response applies. (For simplicity reasons)
                        execution_response = playbook_resp
                    if playbook.stop or execution_event.stop_processing:
                        break

        if execution_event:
            self.__handle_findings(execution_event)

        return execution_response

    def run_actions(
        self,
        execution_event: ExecutionBaseEvent,
        actions: List[PlaybookAction],
    ) -> Optional[Dict[str, Any]]:
        if execution_event.named_sinks is None:
            execution_event.named_sinks = (
                self.registry.get_playbooks().get_default_sinks()
            )

        execution_response = self.__run_playbook_actions(
            execution_event,
            actions,
        )
        self.__handle_findings(execution_event)

        return execution_response

    def __prepare_execution_event(self, execution_event: ExecutionBaseEvent):
        execution_event.set_scheduler(self.registry.get_scheduler())

    def run_external_action(
        self,
        action_name: str,
        action_params: Optional[dict],
        sinks: Optional[List[str]],
    ) -> Optional[Dict[str, Any]]:
        action_def = self.registry.get_actions().get_action(action_name)
        if not action_def:
            return self.__error_resp(f"External action not found {action_name}")

        if not action_def.from_params_func:
            return self.__error_resp(
                f"Action {action_name} cannot run using external event"
            )

        if sinks:
            if action_params:
                action_params["named_sinks"] = sinks
            else:
                action_params = {"named_sinks": sinks}
        try:
            instantiation_params = action_def.from_params_parameter_class(
                **action_params
            )
        except Exception:
            return self.__error_resp(
                f"Failed to create execution instance for"
                f" {action_name} {action_def.from_params_parameter_class}"
                f" {action_params} {traceback.format_exc()}"
            )

        execution_event = action_def.from_params_func(instantiation_params)
        if not execution_event:
            return self.__error_resp(
                f"Failed to create execution event for "
                f"{action_name} {action_params}"
            )

        playbook_action = PlaybookAction(
            action_name=action_name, action_params=action_params
        )
        return self.run_actions(execution_event, [playbook_action])

    @classmethod
    def __error_resp(cls, msg: str) -> dict:
        logging.error(msg)
        return {"success": False, "msg": msg}

    def __run_playbook_actions(
        self,
        execution_event: ExecutionBaseEvent,
        actions: List[PlaybookAction],
    ) -> Dict[str, Any]:
        self.__prepare_execution_event(execution_event)
        execution_event.response = {"success": True}
        for action in actions:
            if execution_event.stop_processing:
                return execution_event.response

            registered_action = self.registry.get_actions().get_action(
                action.action_name
            )

            if (
                not registered_action
            ):  # Might happen if manually trying to trigger incorrect action
                msg = f"action {action.action_name} not found. Skipping for event {type(execution_event)}"
                execution_event.response = self.__error_resp(msg)
                continue

            if not isinstance(execution_event, registered_action.event_type):
                msg = f"Action {action.action_name} requires {registered_action.event_type}"
                execution_event.response = self.__error_resp(msg)
                continue

            if not registered_action.params_type:
                registered_action.func(execution_event)
            else:
                action_params = None
                try:
                    action_params = merge_global_params(
                        self.get_global_config(), action.action_params
                    )
                    params = registered_action.params_type(**action_params)
                except Exception:
                    msg = (
                        f"Failed to create {registered_action.params_type} "
                        f"using {action_params} for running {action.action_name} "
                        f"exc={traceback.format_exc()}"
                    )
                    execution_event.response = self.__error_resp(msg)
                    continue

                registered_action.func(execution_event, params)

        return execution_event.response

    @classmethod
    def __get_fired_trigger(
        cls, trigger_event: TriggerEvent, playbook_triggers: List[Trigger]
    ) -> Optional[BaseTrigger]:
        for trigger in playbook_triggers:
            if trigger.get().should_fire(trigger_event):
                return trigger.get()
        return None

    def __handle_findings(self, execution_event: ExecutionBaseEvent):
        for finding in execution_event.findings.values():
            for sink_name in execution_event.named_sinks:
                try:
                    sink = self.registry.get_sinks().sinks.get(sink_name)
                    if not sink:
                        logging.error(
                            f"sink {sink_name} not found. Skipping event finding {finding}"
                        )
                        continue
                    # create deep copy, so that iterating on one sink won't affect the others
                    finding_copy = copy.deepcopy(finding)
                    sink.write_finding(finding_copy, self.registry.get_sinks().platform_enabled)
                except Exception:  # Failure to send to one sink shouldn't fail all
                    logging.error(
                        f"Failed to publish finding to sink {sink_name}", exc_info=True
                    )

    def get_global_config(self) -> dict:
        return self.registry.get_playbooks().get_global_config()


"""
    功能： 实现百度翻译的界面（纯粹模仿百度翻译页面）
    作者： Miller
    日期： 2019-04-10
"""
from PyQt5.QtWidgets import *

class BaiduDialog(QDialog):
    def __init__(self, crawler, parent=None):
        super().__init__(parent=parent)
        self.__crawler = crawler

        # ------------- 设计与构建UI界面开始
        # 设计布局
        layout_outer = QVBoxLayout()

        layout_layer1 = QHBoxLayout()
        layout_layer2 = QGridLayout()
        layout_layer3 = QGridLayout()

        layout_outer.addLayout(layout_layer1)
        layout_outer.addLayout(layout_layer2)
        layout_outer.addLayout(layout_layer3)

        self.setLayout(layout_outer)

        # 添加组件
        self.edt_english = QLineEdit('英文', self)
        self.edt_english.setEnabled(False)
        self.btn_exchange = QPushButton('<->', self)
        self.edt_chinese = QLineEdit('中文', self)
        self.edt_chinese.setEnabled(False)
        self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        self.btn_translate = QPushButton('翻译', self)
        layout_layer1.addWidget(self.edt_english)
        self.edt_english.setMinimumSize(80, 0)     # 最小缩小到多小
        layout_layer1.addWidget(self.btn_exchange)
        layout_layer1.addWidget(self.edt_chinese)
        self.edt_chinese.setMinimumSize(80, 0)
        layout_layer1.addSpacing(50)                    # 这里是那个翻译前面的间隔
        layout_layer1.addWidget(self.btn_translate)

        self.edt_input = QTextEdit(self)
        self.edt_output = QTextEdit(self)
        self.edt_output.setEnabled(False)
        layout_layer2.addWidget(self.edt_input, 0, 0)
        layout_layer2.addWidget(self.edt_output, 0, 1)

        self.scr_info = QScrollArea(self)
        self.scr_info.setMinimumSize(0, 200)
        # layout_layer3.addWidget(self.scr_info, 0, 0)
        # layout_layer3.addWidget(QLabel(self), 0, 1)   # 放假的控件
        layout_layer2.addWidget(self.scr_info, 1, 0)

        self.setWindowTitle('百度翻译')
        # ------------- 设计与构建UI界面完毕

        # 绑定事件
        self.btn_translate.clicked.connect(self.translate)

    # 槽（slot）函数
    def translate(self):
        print('开始翻译')


# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

import ctypes
import sys
import bpy
import time
import io
import numpy as np
import timeit
from . holoplay_service_api_commands import *
from . import cbor
from . import cffi

hardwareVersion = None

def ensure_site_packages(packages):
    """ `packages`: list of tuples (<import name>, <pip name>) """

    if not packages:
        return

    import site
    import importlib.util

    sys.path.append(site.getusersitepackages())

    modules_to_install = [module[1] for module in packages if not importlib.util.find_spec(module[0])]

    if modules_to_install:
        import subprocess

        if bpy.app.version < (2,91,0):
            python_binary = bpy.app.binary_path_python
        else:
            python_binary = sys.executable

        subprocess.run([python_binary, '-m', 'ensurepip'], check=True)
        subprocess.run([python_binary, '-m', 'pip', 'install', *modules_to_install, "--user"], check=True)

def send_message(sock, inputObj):
    import pynng
    from . import cbor

    out = cbor.dumps(inputObj)
    print("---------------")
    print("Command (" + str(len(out)) + " bytes, "+str(len(inputObj['bin']))+" binary): ")
    print(inputObj['cmd'])
    print("---------------")
    sock.send(out)
    # Driver will respond with a CBOR-formatted error message / information packet
    response = sock.recv()
    print("Response (" + str(len(response)) + " bytes): ")
    response_load = cbor.loads(response)
    print(response_load)
    print("---------------")
    return response_load

def send_quilt(sock, quilt, duration=10):
    print("===================================================")
    print("Sending quilt to HoloPlay Service")

    wm = bpy.context.window_manager
    aspect = wm.aspect
    W = wm.quiltX
    H = wm.quiltY
    vx = wm.tileX
    vy = wm.tileY
    vtotal = vx*vy

    from PIL import Image, ImageOps

    start_time = timeit.default_timer()
    print("Show a single quilt for " + str(duration) + " seconds, then wipe.")
    print("===================================================")

    # we need to get the data from a Blender image datablock because this is where we would put the image aquired from OpenGL
    # in the live view solution
    img0 = quilt
    W,H = img0.size

    # pre-allocate numpy array for better performance
    px0 = np.zeros(H*W*4, dtype=np.float32)
    # foreach_get is probably the fastest method to aquire the pixel values from a Blender image datablock
    img0.pixels.foreach_get(px0)
    print("Reading image from Blender image datablock: %.6f" % (timeit.default_timer() - start_time))

    # we need to convert the floats to integers from 0-255 for most image formats like PNG or BMP which can be send to HoloPlay Service
    # np.multiply(px0, 255, out=px0, casting="unsafe")
    px0 = px0 * 255
    pixels=px0.astype(np.uint8, order="C")

    pimg_time = timeit.default_timer()
    # for some reason the following only works when we create a PIL Image from a bytes-stream
    # so we need to convert the numpy array to bytes and read that
    pimg = Image.frombytes("RGBA", (W,H),  pixels.tobytes())

    # the result is flipped, probably due to numpy, flip it back
    pimg_flipped = ImageOps.flip(pimg)
    print("Converting pixels to bytes-stream and flipping took: %.6f" % (timeit.default_timer() - pimg_time))

    # the idea is that we convert the PIL image to a simple file format HoloPlay Service / stb_image can read
    # and store it in a BytesIO object instead of disk
    output = io.BytesIO()
    pimg_flipped.convert('RGBA').save(output, 'BMP')

    # the contents of the BytesIO object becomes our blob we send to HoloPlay Service
    blob = output.getvalue()
    settings = {'vx': vx,'vy': vy,'vtotal': vtotal,'aspect': aspect}
    send_message(sock, show_quilt(blob, settings))
    print("Reading quilt from Blender image datablock and sending it to HoloPlay Service took: %.6f" % (timeit.default_timer() - start_time))

# def send_quilt_from_np(sock, quilt, W=4096, H=4096, duration=10):
def send_quilt_from_np(sock, quilt, W=4096, H=4096, duration=10):
    print("===================================================")
    print("Sending quilt to HoloPlay Service")

    wm = bpy.context.window_manager
    aspect = wm.aspect
    W = wm.quiltX
    H = wm.quiltY
    vx = wm.tileX
    vy = wm.tileY
    vtotal = vx*vy

    from PIL import Image, ImageOps

    start_time = timeit.default_timer()

    # we get the data from the live view as numpy array
    px0 = quilt
    #W,H = px0.size

    # we need to convert the 0-1 floats to integers from 0-255
    # np.multiply(px0, 255, out=px0, casting="unsafe")
    pixels=px0.astype(np.uint8, order="C")

    pimg_time = timeit.default_timer()
    # for some reason the following only works when we create a PIL Image from a bytes-stream
    # so we need to convert the numpy array to bytes and read that
    pimg = Image.frombytes("RGBA", (W,H),  pixels.tobytes())

    # the result is flipped, probably due to numpy, flip it back
    pimg_flipped = ImageOps.flip(pimg)
    print("Converting pixels to bytes-stream and flipping took: %.6f" % (timeit.default_timer() - pimg_time))

    # the idea is that we convert the PIL image to a simple file format HoloPlay Service / stb_image can read
    # and store it in a BytesIO object instead of disk
    output = io.BytesIO()
    pimg_flipped.convert('RGBA').save(output, 'BMP')

    # the contents of the BytesIO object becomes our blob we send to HoloPlay Service
    blob = output.getvalue()
    settings = {'vx': vx,'vy': vy,'vtotal': vtotal,'aspect': aspect}
    send_message(sock, show_quilt(blob, settings))
    print("Reading quilt from numpy array and sending it to HoloPlay Service took in total: %.6f" % (timeit.default_timer() - start_time))

def init():
    global hp
    global sock
    global numDevices
    global screenW
    global screenH
    global aspect
    global hardwareVersion

    print("Init Settings")
    start_time = timeit.default_timer()

    wm = bpy.context.window_manager

    ws_url = "ws://localhost:11222/driver"
    driver_url = "ipc:///tmp/holoplay-driver.ipc"

    ensure_site_packages([
        ("pynng","pynng"),
        ("PIL", "Pillow")
    ])

    import pynng

    # This script should work identically whether addr = driver_url or addr = ws_url
    addr = driver_url

    sock = pynng.Req0(recv_timeout=2000)
    try:
        sock.dial(addr, block = True)
    except:
        print("Could not open socket. Is driver running?")
        sock = None
        return False

    response = send_message(sock, {'cmd':{'info':{}},'bin':''})
    if response != None:
        # create a dictionary with an index for this device
        devices = response['devices']
        if devices == []:
            print("No Looking Glass devices found")
        else:
            print("Reading settings from device")
            screenW = devices[0]['calibration']['screenW']['value']
            screenH = devices[0]['calibration']['screenH']['value']
            quiltX = devices[0]['defaultQuilt']['quiltX']
            quiltY = devices[0]['defaultQuilt']['quiltY']
            tileX = devices[0]['defaultQuilt']['tileX']
            tileY = devices[0]['defaultQuilt']['tileY']
            hardwareVersion = devices[0]['hardwareVersion'] # not storing this in wm because we need to change this to support multiple devices in the future
            aspect = screenW / screenH
            wm.screenW = screenW
            wm.screenH = screenH
            wm.aspect = aspect
            wm.quiltX = quiltX
            wm.quiltY = quiltY
            wm.tileX = tileX
            wm.tileY = tileY
            if hardwareVersion == 'portrait':
                wm.viewX = 420
                wm.viewY = 560
                wm.quiltX = 3360
                wm.quiltY = 3360
            # print(devices)
            # print(hardwareVersion)
            wm.numDevicesConnected = 1 # temporarily support only one device due to the way we globally store vars in the wm

    print("Number of devices found: " + str(wm.numDevicesConnected))

class looking_glass_reconnect_to_holoplay_service(bpy.types.Operator):
    """ Reconnects to Holoplay Service """
    bl_idname = "lookingglass.reconnect_to_holoplay_service"
    bl_label = "Reconnect to Service"
    bl_description = "Re-Initializes the connection to HoloPlay Service"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        init()
        return {'FINISHED'}


from enum import Enum
from pprint import PrettyPrinter

PREFIX = 'Unit'
SUFIX = 'Event'

def parse_unit_event_string(input):
    min_length = len(PREFIX) + len(SUFIX)
    if len(input) > min_length:
        begin = len(PREFIX)
        end = len(input) - len(SUFIX)
        return input[begin:end]
    else:
        return None


# -*- encoding: utf-8 -*-

import os
import time
import pyautogui as auto
import PyHook3 as pyhook
import pythoncom
from PIL import Image
from pytesseract import image_to_string
from googletrans import Translator
import asyncio
import tkinter as tk
# 引入字体模块
import tkinter.font as tkFont
from concurrent.futures import ThreadPoolExecutor

translator = Translator(service_urls=['translate.google.cn'])

px=-1000
py=-1000
x=-1000
y=-1000
last=0

root = tk.Tk("Advise")
root.overrideredirect(True)
sw=root.winfo_screenwidth()
sh=root.winfo_screenheight()
root_x=sw-200
root_y=sh-120
root.geometry("200x50+%d+%d"%(root_x,root_y))
root.wm_attributes('-topmost',1)
root.wm_attributes('-alpha',0.15)
font =  tkFont.Font(family="Microsoft YaHei",size=9);
english = tk.Label(root,text="English",font=font)
english.pack()
chinese = tk.Label(root,text="Chinese",font=font)
chinese.pack()

async def translateZh(text):
    global translator,english
    text=text.replace("\n"," ")
    english['text']=translator.translate(text,src="zh-cn",dest="en").text
    await asyncio.sleep(0.5)

async def translateEn(text):
    global translator
    text=text.replace("\n"," ")
    chinese['text']=translator.translate(text,src="en",dest="zh-cn").text
    await asyncio.sleep(0.5)

async def translate(text):
    global root
    task1 = asyncio.create_task(translateEn(text))
    task2 = asyncio.create_task(translateZh(text))
    await task1
    await task2
    root.wm_attributes('-alpha',0.4)
    root.update()
    await hide(5)

async def hide(delay):
    global root
    await asyncio.sleep(delay)
    root.wm_attributes('-alpha',0)
    root.update()

def getClickPosition(_x,_y):
    global x,y,px,py
    px=x;py=y;
    x=_x;y=_y;
    l=min(x,px)
    t=min(y,py)
    w=abs(px-x)
    h=abs(py-y)
    if w<1000 and h<1000:
        auto.screenshot("temp.png",region=(l,t,w,h))
        img=Image.open("temp.png")
        img=img.convert("L")
        text = image_to_string(img,"chi_sim+eng")
        if text:
            asyncio.run(translate(text))
        x=-1000
        y=-1000

def onMouseEvent(event):
    try:
        global last,loop
        if event.Message==514:
            # Judge Double Click
            if event.Time-last<300:
                getClickPosition(event.Position[0],event.Position[1])
            last=event.Time
        return True
    except KeyboardInterrupt:
        exit(0)

def main():
    hm = pyhook.HookManager()
    hm.MouseAll = onMouseEvent
    hm.HookMouse()
    pythoncom.PumpMessages()

if __name__ == "__main__":
    main()


from scipy import stats
import numpy as np
import csv

sentiment_scores = np.random.random(52)

# input should be a numpy array of sentiment scores for the 50 states
def  calculateRegressions(sentiment_scores):

	white_ratios = []
	male_ratios = []
	educations = []
	populations = []
	incomes = []

	criteria = ("white ratio", "male ratio", "education level", "population", "median household income")
	r_values = []
	extents_of_correlation = []
	demographics = [("white ratio", white_ratios, 1),
					("male ratio", male_ratios, 2),
					("education level", educations, 6),
					("population", populations, 7),
					("median household income", incomes, 8)]

	with open('USdemographics_by_state.csv', 'r') as file:
		reader = csv.reader(file)
		# skip header
		next(reader)

		for row in reader:
			for (_, data, col) in demographics:
				num = row[col].replace(",", "", 10)
				num = num.replace("$", "")
				data.append(float(num))

	correlation = [[0.3, "not"],
					[0.5, "weakly"],
					[0.7, "moderately"],
					[1.0, "strongly"]]

	for (colname, data, _) in demographics:
		slope, intercept, r_value, p_value, std_err = stats.linregress(sentiment_scores, data)
		# print for debugging purposes
		# print(colname + " r value: " + str(r_value))
		r_values.append(str(r_value))

		abs_r = abs(r_value)
		for (num, extent) in correlation:
			if abs_r < num:
				# print for debugging purposes
				# print(extent + " correlated")
				extents_of_correlation.append(extent + " correlated")
				break

	file.close()
	# output is a tuple of 3 tuples of strings
	return (criteria, tuple(r_values), tuple(extents_of_correlation))

print(calculateRegressions(sentiment_scores))


import math

def distance_from_fft(point, ratio, image_shape):
    '''This is a function used to calculate the distance\
    between one point and origin. The ratio should be in\
    unit of pixel/nm'''
    cord_list = []
    for i in range(len(point)):
        cord_list.append(point[i]**2)
    pixel_length = math.sqrt(sum(cord_list))

    factor = ratio/image_shape
    inverse_length = 1/(factor*pixel_length)*10  # units 1/angstrom
    return inverse_length


"""To run this: $ python3 parse.py test.xml
The script will pase a XML file and print its node tags.
Compatible with Python 3; changing the print statements should make this
compatible with Python 2.
"""

import sys

# http://docs.python.org/library/xml.etree.elementtree.html
from xml.etree import ElementTree

def operation(node):
    """Just a sample function that prints the tag of a node."""
    return print(node.tag)

def recur_node(node, f):
    """Applies function f on given node and goes down recursively to its
       children.

       Keyword arguments:
       node - the root node
       f - function to be applied on node and its children

    """
    if node != None:
        f(node)
        for item in node.getchildren():
            recur_node(item, f)
    else:
        return 0

def main(fileName):
    try:
        root = ElementTree.parse(fileName).getroot()
    except:
        # there should be some proper exception handling here
        return -1
    return recur_node(root, operation)

if __name__ == "__main__":
  sys.exit(main(sys.argv[1]))

###
###


import os
import re
import logging
import json
from datetime import datetime, timedelta
from pathlib import Path

import requests
import spotipy
from spotipy import SpotifyOAuth
from spotipy.oauth2 import SpotifyClientCredentials
from dagster_cron import SystemCronScheduler
from dagster import (
    RepositoryDefinition,
    ScheduleDefinition,
    pipeline,
    schedules,
    solid,
)

from env import FEEDLY_REFRESH_TOKEN, SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, SPOTIPY_REDIRECT_URI

class FeedlyApiRequestError(Exception):
    pass

class MusicFeeder:
    scopes = " ".join([
        "ugc-image-upload",
        "user-read-playback-state",
        "user-modify-playback-state",
        "user-read-currently-playing",
        "streaming",
        "app-remote-control",
        "user-read-email",
        "user-read-private",
        "playlist-read-collaborative",
        "playlist-modify-public",
        "playlist-read-private",
        "playlist-modify-private",
        "user-library-modify",
        "user-library-read",
        "user-top-read",
        "user-read-recently-played",
        "user-follow-read",
        "user-follow-modify"
    ])
    feedly_username = '5066bdab-2d04-4094-b7ac-cd19047daffe'
    spotify_username = "21bpmv7tcs4av5sakwfxkof2a"
    spotify_playlist_id = "6qsodHkxMB36VhWfIwvylQ"
    stop_words = ["MV"]

    def __init__(self):
        # feedly setup
        self.feedly_access_token = self._get_feedly_access_token()
        # spotify setup
        self.cache_path = Path(__file__).parent / f".cache-{self.spotify_username}"
        self.sp_oauth = SpotifyOAuth(
            SPOTIPY_CLIENT_ID,
            SPOTIPY_CLIENT_SECRET,
            SPOTIPY_REDIRECT_URI,
            scope=self.scopes,
            cache_path=self.cache_path,
            username=self.spotify_username
        )
        self.token_info = self.sp_oauth.get_cached_token()
        self.token = self.token_info["access_token"]
        self.spotify = spotipy.Spotify(auth=self.token)
        self.titles, self.tracks, self.current_tracks, self.tracks_to_add = None, None, None, None

    def execute(self):
        self.titles = self._get_titles()
        # extract latest 30 titles
        self.titles = self.titles[:30]
        self.tracks = self._get_taget_tracks(self.titles)
        self.current_tracks = self._get_current_tracks()
        self.tracks_to_add = self._get_tracks_to_add(self.tracks, self.current_tracks)
        self._add_to_spotify(self.tracks_to_add)

    def _get_feedly_access_token(self):
        url = "https://cloud.feedly.com/v3/auth/token"
        payload = dict(
            refresh_token=FEEDLY_REFRESH_TOKEN,
            client_id="feedlydev",
            client_secret="feedlydev",
            grant_type="refresh_token"
        )
        resp = requests.post(url, json=payload).json()
        return resp['access_token']

    def _request_to_feedly(self, url, params={}):
        headers = {'Authorization': f"OAuth {self.feedly_access_token}"}
        resp = requests.get(url, headers=headers, params=params)
        if resp.status_code != 200:
            raise FeedlyApiRequestError(json.dumps(resp.json()))

        return resp.json()

    def _get_titles(self):
        url = f"https://cloud.feedly.com/v3/streams/contents?streamId=user/{self.feedly_username}/category/Music"
        params = dict(unreadOnly=False, count=100)
        resp = self._request_to_feedly(url, params)
        return [item['title'] for item in resp['items'] if item['title']]

    def _get_taget_tracks(self, titles):
        one_week_ago = datetime.today() - timedelta(days=7)
        tracks = []
        for title in titles:
            terms = re.findall("[a-zA-Z ]+", title)
            terms = [term.strip() for term in terms if term.strip()]
            terms = [term for term in terms if term not in self.stop_words]
            query = " ".join(terms)
            logging.info(f"searching: {query}")
            result = self.spotify.search(query)['tracks']['items']
            if result:
                track = result[0]
                try:
                    release_date = datetime.strptime(track['album']['release_date'], "%Y-%m-%d")
                except ValueError:
                    continue

                # Don't append if the release date of the track is older than one week ago
                if release_date < one_week_ago:
                    continue
                tracks.append(track['id'])

        return tracks

    def _get_current_tracks(self):
        return self.spotify.user_playlist(self.spotify_username, self.spotify_playlist_id)['tracks']['items']

    def _get_tracks_to_add(self, tracks, current_tracks):
        current_track_ids = [c_track['track']['id'] for c_track in current_tracks]
        tracks = [track for track in tracks if track not in current_track_ids]
        return tracks

    def _add_to_spotify(self, tracks):
        if not tracks:
            return
        self.spotify.user_playlist_add_tracks(self.spotify_username, self.spotify_playlist_id, tracks, position=0)

@solid
def load_to_spotify(context):
    MusicFeeder().execute()

@pipeline
def load_to_spotify_pipeline():
    load_to_spotify()

def etl_spotify_repository():
    return RepositoryDefinition(
        'etl_spotify_repository', pipeline_defs=[load_to_spotify_pipeline]
    )

@schedules(SystemCronScheduler)
def spotify_schedules():
    return [
        ScheduleDefinition(
            name='daily_spotify_batch',
            cron_schedule='0 * * * *',
            pipeline_name='load_to_spotify_pipeline',
            environment_dict={},
        )
    ]

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    MusicFeeder().execute()
    # EtlFeedlyspotify(context=context).execute()
    # result = execute_pipeline(load_to_spotify_pipeline)
    # assert result.success

# def test_hello_cereal_solid():
#     res = execute_solid(hello_cereal)
#     assert res.success
#     assert len(res.output_value()) == 77

# def test_hello_cereal_pipeline():
#     res = execute_pipeline(hello_cereal_pipeline)
#     assert res.success
#     assert len(res.result_for_solid('hello_cereal').output_value()) == 77


import pygame as pg
from numpy import zeros

from constants import consts as c
from id_mapping import id_map, reverse_id_map

class World:
    def __init__(self):
        self.grid = zeros((c.num_cells, c.num_cells), dtype=int)
        self.ore_locations = []

        self.grid[10:13, 10:12] = id_map["coal"]
        self.grid[15:17, 15:17] = id_map["iron_ore"]
        self.grid[15:17, 20:23] = id_map["copper_ore"]
        self.populate_ore_locations()

    def populate_ore_locations(self):
        for row in range(c.num_cells):
            for col in range(c.num_cells):
                if self.grid[row, col] > 0:
                    self.ore_locations.append((row, col))

    def render(self):
        for loc in self.ore_locations:
            row, col = loc
            x = col * c.cell_length - c.player_x
            y = row * c.cell_length - c.player_y
            pg.draw.rect(c.screen, c.ore_colors[self.grid[loc]], (x, y, c.cell_length, c.cell_length))

    def render_tooltip(self, row, col):
        x, y = pg.mouse.get_pos()
        ore = reverse_id_map[self.grid[row, col]].replace("_", " ").title()
        ore_text = c.merriweather.render(ore, True, pg.Color("white"))
        pg.draw.rect(c.screen, pg.Color("black"), (x + 10, y + 10, ore_text.get_width() + 20, ore_text.get_height() + 20))
        c.screen.blit(ore_text, (x + 20, y + 20))

world = World()


"""Analyze word-level LIWC scores between lucid and non-lucid dreams.
***Note this does not re-run LIWC, but loads in prior results.

IMPORTS
=======
    - original post info,     dreamviews-posts.tsv
    - word-level LIWC scores, validate-liwc_wordscores.tsv
    - LIWC dictionary,        dictionaries/custom.dic
EXPORTS
=======
    - effect sizes (d) for top words from each category, validate-liwc_wordscores-stats.tsv

Some of this is copy/pasted from the general liwc_stats script,
but this is way messier so better alone.
1. only a subset
2. need to load in numpy stuff
3. need to look up vocabs in whole lexicon for subsetting
4. don't need all the summary statistic details
5. don't want to run both stats on this, too out of hand just pick one
6. export is different, here it's the top N words of a few categories and their effect sizes
"""
import liwc
import numpy as np
import pandas as pd
import pingouin as pg
from scipy import sparse
import tqdm

import config as c

################################################################################
# SETUP
################################################################################

LIWC_CATEGORIES = ["insight", "agency"]
top_n = 20  # Top n contributing tokens/words for each category.

import_path_dict = c.DATA_DIR / "dictionaries" / "custom.dic"
import_path_data = c.DATA_DIR / "derivatives" / "validate-liwc_wordscores-data.npz"
import_path_attr = c.DATA_DIR / "derivatives" / "validate-liwc_wordscores-attr.npz"
export_path = c.DATA_DIR / "derivatives" / "validate-liwc_wordscores-stats.tsv"

#### load in the original posts file to get attributes lucidity and user_id
# and drop un-labeled posts.
# merge the clean data file and all its attributes with the liwc results
posts = c.load_dreamviews_posts()
posts = posts.set_index("post_id")[["user_id", "lucidity"]]
posts = posts[ posts["lucidity"].str.contains("lucid") ]

# #### load prior full LIWC results (i.e., category results)
# liwccats = pd.read_csv(import_fname_liwc, sep="\t", encoding="utf-8",
#     index_col="category", usecols=["category", "cohen-d"], squeeze=True)
# liwccats = liwccats.loc[LIWC_CATEGORIES]

#### load in dictionary lexicon
# and flip key/value from token/category to category/word_list
# and only for the liwc categories of interest
lexicon, _ = liwc.read_dic(import_fname_dict)
wordlists = { c: [ t for t, cats in lexicon.items() if c in cats ]
    for c in LIWC_CATEGORIES }
# and grab a subset of all the vocab so to reduce unnecessary memory later
vocab = set([ t for wlist in wordlists.values() for t in wlist ])

#### load in numpy arrays of token scores and generate dataframe
sparse_matr_attributes = np.load(import_fname_attr, allow_pickle=True)
tokens = sparse_matr_attributes["token"]
token_index = [ t in vocab for t in tokens ]
relevant_tokens = tokens[token_index]
sparse_matr = sparse.load_npz(import_fname_data)
scores = pd.DataFrame(sparse_matr[:,token_index].toarray(),
    columns=relevant_tokens,
    index=sparse_matr_attributes["post_id"])

# merge the post attributes with LIWC token scores
df = posts.join(scores, how="left")
assert len(df) == len(posts)

################################################################################
# STATISTICAL TESTS
################################################################################

# Average the LD and NLD scores of each token for each user.
# Some users might not have both dream types and they'll be removed.
avgs = df.groupby(["user_id", "lucidity"]
    ).mean().rename_axis(columns="token"
    ).pivot_table(index="user_id", columns="lucidity"
    ).dropna()

# We already have only relevant tokens, so get effect
# sizes for all of them.
effectsize_results = []
for tok in tqdm.tqdm(relevant_tokens, desc="stats on word-level LIWC scores"):
    ld, nld = avgs[tok][["lucid", "nonlucid"]].T.values
    stats = {}
    stats["cohen-d"] = pg.compute_effsize(ld, nld, paired=True, eftype="cohen")
    stats["cohen-d_lo"], stats["cohen-d_hi"] = pg.compute_bootci(ld, nld,
        paired=True, func="cohen", method="cper",
        confidence=.95, n_boot=2000, decimals=4)
    effectsize_results.append(pd.DataFrame(stats, index=[tok]))

es_df = pd.concat(effectsize_results).rename_axis("token")

# Each LIWC category will utilize a different set of tokens/words.
# Find the top N contributors to the overall effect.
# Find the direction of overall effect by looking at previous LIWC category output.
token_rank_results = []
for cat in LIWC_CATEGORIES:
    # generate an index of relevant tokens for this category
    cat_index = es_df.index.map(lambda t: t in wordlists[cat])
    # extract only rows in this category
    df_ = es_df.loc[cat_index]
    # # get direction of effect and remove rows not in line
    # d_sign = np.sign(liwccats.loc[cat])
    # if d_sign > 0:
    #     df_ = df_[ df_["cohen-d"] > 0 ]
    # else:
    #     df_ = df_[ df_["cohen-d"] < 0 ]
    # sort by effect size
    df_ = df_.sort_values("cohen-d", ascending=False, key=abs)
    # take top rows and clean up a bit
    df_ = df_[:TOP_N]
    df_[f"{cat}_rank"] = np.arange(TOP_N) + 1
    token_rank_results.append(df_)

out = pd.concat(token_rank_results)

# Export.
out.to_csv(export_path, float_format="%.4f", index=True, na_rep="NA", sep="\t", encoding="utf-8")


from collections import defaultdict
from datetime import time
from typing import Dict, List

import pandas as pd
from pandas import ExcelFile

class ConfigMaker:
    def make_regions_config(self, excel_file: ExcelFile) -> Dict[str, str]:
        regions = {}

        df = pd.read_excel(excel_file, sheet_name="Region-->TZB_Reg_code")

        for _, row in df.iterrows():
            regions[row["Unnamed: 0"].strip()] = row["из проекта ТЗБ"].strip()

        return regions

    def make_region_codes_config(self, excel_file: ExcelFile) -> Dict[str, int]:
        region_codes = {}

        df = pd.read_excel(excel_file, sheet_name="Region-->TZB_Reg_code")

        for _, row in df.iterrows():
            if pd.isna(row["Code_region_TZB"]):
                continue

            region_codes[row["Region_TZB"]] = int(row["Code_region_TZB"])

        return region_codes

    def make_operators_config(self, excel_file: ExcelFile) -> Dict[str, str]:
        operators = {}

        df = pd.read_excel(excel_file, sheet_name="Oper-->TZB_Oper_Code")

        for _, row in df.iterrows():
            operators[row["OPERATOR"]] = row["GrM_Name"]

        return operators

    def make_operator_codes_config(self, excel_file: ExcelFile) -> Dict[str, int]:
        operator_codes = {}

        df = pd.read_excel(excel_file, sheet_name="Oper-->TZB_Oper_Code")

        for _, row in df.iterrows():
            if pd.isna(row["GrS_Name"]):
                continue

            operator_codes[row["GrS_Name"]] = int(row["GrS_Code"])

        return operator_codes

    def make_time_difference_config(self, excel_file: ExcelFile) -> Dict[str, str]:
        time_difference_config = {}

        df = pd.read_excel(excel_file, sheet_name="Region-->UTC")

        for _, row in df.iterrows():
            time_difference_config[row["RegionName"]] = row["TimeDifference"]

        return time_difference_config

    def make_intervals_config(self, excel_file: ExcelFile) -> Dict[str, Dict[str, str]]:
        intervals = {}

        df = pd.read_excel(excel_file, sheet_name="Region-->Interval")

        for _, row in df.iterrows():
            intervals[row["RegionName"]] = {
                "begin": self.convert_time_to_string(row["CallIntervalBegin"]),
                "end": self.convert_time_to_string(row["CallIntervalEnd"]),
            }

        return intervals

    def convert_time_to_string(self, time: time) -> str:
        if pd.isna(time):
            return ""

        return time.strftime("%H:%M:%S")

    def make_ignore_config(self, excel_file: ExcelFile) -> List[str]:
        ignore = []

        df = pd.read_excel(excel_file, sheet_name="Region-->TZB_Reg_code")

        for _, row in df.iterrows():
            if row["Code_region_TZB"] == 0:
                ignore.append(row["Region_TZB"])

        return ignore

    def make_allowed_operators_config(self, excel_file: ExcelFile) -> Dict[str, list]:
        allowed_operators = defaultdict(list)

        df = pd.read_excel(excel_file, sheet_name="Oper-->Allowed_Region")

        for _, row in df.iterrows():
            allowed_operators[row["OPERATOR"]].append(row["REGION"])

        return allowed_operators

    def make_config_file(self, excel_file: ExcelFile) -> Dict:
        config = {}

        config["regions"] = self.make_regions_config(excel_file)
        config["region_codes"] = self.make_region_codes_config(excel_file)
        config["operators"] = self.make_operators_config(excel_file)
        config["operator_codes"] = self.make_operator_codes_config(excel_file)
        config["time_difference"] = self.make_time_difference_config(excel_file)
        config["intervals"] = self.make_intervals_config(excel_file)
        config["ignores"] = self.make_ignore_config(excel_file)
        config["allowed_operators"] = self.make_allowed_operators_config(excel_file)

        return config


from splinter import Browser
from bs4 import BeautifulSoup
import time
import pandas as pd
import pymongo
import requests

def init_browser():
    executable_path = {"executable_path": "chromedriver.exe"}
    return Browser("chrome", **executable_path, headless=False)

def scrape():
    browser = init_browser()

    # NASA Mars News

    url= "https://mars.nasa.gov/news/"
    browser.visit(url)
    html= browser.html
    soup= BeautifulSoup(html,"html.parser")

    latest_news_title= soup.find("div", class_="bottom_gradient").find("h3").text
    p_text= soup.find("div", class_= "article_teaser_body").text

    # JPL Mars Space Images

    img_url= "https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars"
    browser.visit(img_url)
    time.sleep(5)
    img_html= browser.html
    i_soup= BeautifulSoup(img_html,"html.parser")
    img_path= i_soup.find("article", class_= "carousel_item")\
    ["style"].replace("background-image: url(","").replace(");", "")[1:-1]
    mars_url= "https://www.jpl.nasa.gov"
    featured_image_url= mars_url+img_path

    # Mars Weather

    w_url= "https://twitter.com/marswxreport?lang=en"
    browser.visit(w_url)
    time.sleep(5)
    html= browser.html
    soup= BeautifulSoup(html, "html.parser")
    mars_weather = (soup.find('div', attrs={"data-testid": "tweet"}).get_text()).split('InSight ')[1]

    # Mars Facts

    f_url= "https://space-facts.com/mars/"
    browser.visit(f_url)
    time.sleep(5)
    m_facts= pd.read_html(f_url)
    mars_df= m_facts[0]
    mars_facts= mars_df.rename(columns={0: "Aspect", 1: "Detail"}).set_index("Aspect")
    mars_df_table= mars_facts.to_html()
    mars_df_table.replace("\n","")

    # Mars Hemispheres

    h_url= "https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars"
    browser.visit(h_url)
    time.sleep(5)
    h_html= browser.html
    soup= BeautifulSoup(h_html, "html.parser")

    h_images= soup.find("div", class_= "collapsible results")
    hems= h_images.find_all("a")

    hems_list= []

    for hemisphere in hems:
        if hemisphere.h3:
            title=hemisphere.h3.text
            link=hemisphere["href"]
            home_url="https://astrogeology.usgs.gov/"
            next_url=home_url+link
            browser.visit(next_url)
            html = browser.html
            soup = BeautifulSoup(html, 'html.parser')
            hemisphere2=soup.find("div",class_= "downloads")
            img=hemisphere2.ul.a["href"]
            hemisphere_dict={}
            hemisphere_dict["Title"]=title
            hemisphere_dict["Image_URL"]=img
            hems_list.append(hemisphere_dict)
            browser.back()

    hems_list

    Mars_info={
        "Mars_news_title": latest_news_title,
        "Mars_news_text": p_text,
        "Mars_featured_img": featured_image_url,
        "Mars_weather": mars_weather,
        "Mars_facts": mars_df_table,
        "Mars_hemispheres": hems_list
    }

    browser.quit()

    return Mars_info

# if __name__ == "__main__":
#     data = scrape()
#     print(data)


from torch.utils.data import Dataset
from torchvision import transforms
import numpy as np
import pandas as pd
import random
from src.dataset import get_load_data
import torch
from tqdm import tqdm

def generate_train_set(train):
    train_set = []
    classes = [i[1] for i in train]
    classes = np.array(classes)
    for i in tqdm(range(len(train))):

        # positive_list = [j for j in range(len(classes)) if classes[j] == train[i][1]]
        positive_list = np.argwhere(classes == train[i][1])
        positive_ind = list(random.choice(positive_list))[0]
        while positive_ind == i:
            positive_ind = list(random.choice(positive_list))[0]
        negative_list = np.argwhere(classes != train[i][1])
        negative_ind = list(random.choice(negative_list))[0]
        train_set.append({"anchor": train[i][0], "positive": train[positive_ind][0], "negative": train[negative_ind][0], "anchor_label": train[i][1]})

    return train_set

class TripletLossDataset(Dataset):
    def __init__(self, train_set = None, train=True, root = "data", dataset = "FashionMNIST"):

        self.is_train = train

        if self.is_train:
            if train_set is None:
                train, _ = get_load_data(root = root, dataset = dataset)
                self.train_set = generate_train_set(train)
            else:
                self.train_set = train_set
        else:
            _, self.test = get_load_data(root = root, dataset = dataset)

    def __len__(self):
        if self.is_train:
            return len(self.train_set)
        else:
            return len(self.test)

    def __getitem__(self, item):

        if self.is_train:
            data = self.train_set[item]

            return data['anchor'], data['positive'], data['negative'], data['anchor_label']

        else:
            return self.test[item][0], self.test[item][1]


#!/usr/bin/python
# -*- encoding: utf-8 -*-

#Created by 'quanpower' on '15-4-4'
#Email:quanpower@gmail.com
#QQ:252527676
#Site:www.smartlinkcloud.com

__author__ = 'quanpower'

import numpy as np
import cv2

#获得视频的格式
videoCapture =cv2.VideoCapture('../files/video_test.rmvb')

#获得码率及尺寸
fps = videoCapture.get(cv2.cv.CV_CAP_PROP_FPS)
size = (int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
        int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))

#指定写视频的格式, I420-avi, MJPG-mp4
videoWriter = cv2.VideoWriter('oto_other.mp4', cv2.cv.CV_FOURCC('M', 'J', 'P', 'G'), fps, size)

#读帧
success, frame = videoCapture.read()

while success :
    cv2.imshow('Oto Video', frame) #显示
    cv2.waitKey(1000/int(fps)) #延迟
    videoWriter.write(frame) #写视频帧
    success, frame = videoCapture.read() #获取下一帧

    if cv2.waitKey(1)&0xFF == ord('q'):
        break

videoCapture.release()
cv2.destroyAllWindows()


import os
import re
import xlrd
import xlwt
from xlutils.copy import copy

class HandleExcel:
    def __init__(self):
        self.root = ''
        self.dir = ''
        self.files = ''
        self.allFile_url = ''
        self.final_file_name = '测试数据清洗版.xls'
        self.title = ['条码内容', '故障通道', '测试时间']
        self.file_name = '测试数据汇总.xls'

    def get_allfile_msg(self,file_dir):
        for root, dirs, files in os.walk(file_dir):
            return root, dirs, files

    def get_allfile_url(self,root, files):
        allFile_url = []
        for f in files:
            file_url = root + '\\' + f
            allFile_url.append(file_url)
        return allFile_url

    def all_to_one(self,root, allFile_url, files, file_name='allExcel.xls', title=None):
        file_name = root + '\\' + file_name
        self.create_excel(file_name, title)

        list_row_data = []
        for f in allFile_url:
            excel = xlrd.open_workbook(f)
            table = excel.sheet_by_index(0)
            for i, j in zip(range(table.nrows), files):
                if i == 0:
                    continue
                row = table.row_values(i)
                k = re.sub("\D", "", j)
                row.append(k)
                list_row_data.append(row)
        self.add_row(list_row_data, file_name)

    def create_excel(self,file_name, title):
        a = xlwt.Workbook()
        table = a.add_sheet('sheet1', cell_overwrite_ok=True)
        for i in range(len(title)):
            table.write(0, i, title[i])
        a.save(file_name)

    file_name = '测试数据汇总.xls'
    title = ['条码内容', '故障通道', '测试时间']

    def add_row(self,list_row_data, file_name):
        allExcel1 = xlrd.open_workbook(file_name)
        sheet = allExcel1.sheet_by_index(0)
        allExcel2 = copy(allExcel1)
        sheet2 = allExcel2.get_sheet(0)
        i = 0
        for row_data in list_row_data:
            for j in range(len(row_data)):
                sheet2.write(sheet.nrows + i, j, row_data[j])
            i += 1  # 覆盖原文件
        allExcel2.save(file_name)

    def clear_excel(self,file_dir,file_name):
        data = xlrd.open_workbook(file_dir + '\\' + file_name)
        table = data.sheets()[0]
        nrows = table.nrows
        ncols = table.ncols

        first_ncol = []
        second_ncol = []
        thrid_ncol = []

        for i in range(1, nrows):
            first_ncol_value = table.cell(i, 0).value
            first_ncol.append(first_ncol_value)

        for j in range(1, nrows):
            second_ncol_value = table.cell(j, 1).value
            second_ncol.append(second_ncol_value)

        for k in range(1, nrows):
            thrid_ncol_value = table.cell(k, 2).value
            thrid_ncol.append(thrid_ncol_value)

        clear_code_list = []
        clear_result_list = []
        clear_time_list = []

        for x, y, z in zip(first_ncol, second_ncol, thrid_ncol):
            if x not in clear_code_list and y == '\'PASS\'' or y == 'PASS' and x.startswith('A'):
                clear_code_list.append(x)
                clear_result_list.append(y)
                clear_time_list.append(z)

        a = xlwt.Workbook()
        table = a.add_sheet('sheet1', cell_overwrite_ok=True)
        # 写表头
        for i in range(len(self.title)):
            table.write(0, i, self.title[i])
        # 写内容
        for code_value, result_value, time_value in zip(range(len(clear_code_list)), range(len(clear_result_list)),
                                                        range(len(clear_time_list))):
            table.write(code_value + 1, 0, clear_code_list[code_value])
            table.write(result_value + 1, 1, clear_result_list[code_value])
            table.write(time_value + 1, 2, clear_time_list[code_value])

        a.save(file_dir + '\\' + self.final_file_name)


inputValue = ""
numberList = []
while (inputValue != 'x'):
    inputValue = input("Please enter numbers. If you enter x you will be shown all the numbers you have entered that repeat themselves ")
    if (inputValue != 'x'):
        numberList.append(inputValue)
repeatedNumbers = []
x = 0
for i in range(len(numberList)):
    for x in range(i, len(numberList)):
        if (i != x):
            if (numberList[int(i)] == numberList[x]):
                repeatedNumbers.append(numberList[i])
                break

print(repeatedNumbers)


#!/usr/bin/env python

import numpy as np
import rospy

from ar_commander.msg import State, ControllerCmd

RATE = 10

class EstimatorOL():
    def __init__(self):
        rospy.init_node('estimator')

        # incominng measurements (inputs)
        self.vel_cmd = None
        self.omega_cmd = None

        # robot state (output)
        self.state = State()
        self.state.pos.data = np.zeros(2)
        self.state.vel.data = np.zeros(2)
        self.state.theta.data = 0
        self.state.omega.data = 0

        # subscribers
        rospy.Subscriber('controller_cmds', ControllerCmd, self.controllerCmdCallback)

        # publishers
        self.pub_state = rospy.Publisher('estimator/state', State, queue_size=10)

    def controllerCmdCallback(self, msg):
        self.vel_cmd = np.array(msg.robot_vel.data)
        self.omega_cmd = msg.robot_omega.data

    def updateState(self):
        """
        main estimator function
        Uses measurements and previous state to estimate current state
        """

        # wait till we have pos and theta measurements
        if self.vel_cmd is None or self.omega_cmd is None:
            return

        dt = 1. / RATE
        self.state.pos.data = self.state.pos.data + self.vel_cmd*dt
        self.state.theta.data = self.state.theta.data + self.omega_cmd*dt

    def publish(self):
        self.pub_state.publish(self.state)

    def run(self):
        rate = rospy.Rate(RATE) # 10 Hz
        while not rospy.is_shutdown():
            self.updateState()
            self.publish()
            rate.sleep()

if __name__ == '__main__':
    estimator = EstimatorOL()
    estimator.run()


""" AndAgainAndAgainAndAgainAndAgainAndAgainAndAgain """
def main():
    """ a e i o u """
    words = input().strip().split()
    words_result = words.copy()
    result = []
    for num in range(len(words)):
        fixed_word = words[num].replace('a', '#').replace('e', '#').replace('i', '#') \
        .replace('o', '#').replace('u', '#')
        if fixed_word.count("#") > 1:
            result.append(words_result[num].replace('.', ''))
    if len(result) > 0:
        result.sort()
        print("\n".join(result))
    else:
        print("Nope")
main()


#!/usr/bin/env python3

import os
import logging
import argparse
import csv

def process_result(result_folder):
    status = "success"
    summary = []
    paths = []
    tests = ["TLPWhere", "TLPGroupBy", "TLPHaving", "TLPWhereGroupBy", "TLPDistinct", "TLPAggregate"]

    for test in tests:
        err_path = '{}/{}.err'.format(result_folder, test)
        out_path = '{}/{}.out'.format(result_folder, test)
        if not os.path.exists(err_path):
            logging.info("No output err on path %s", err_path)
            summary.append((test, "SKIPPED"))
        elif not os.path.exists(out_path):
            logging.info("No output log on path %s", out_path)
        else:
            paths.append(err_path)
            paths.append(out_path)
            with open(err_path, 'r') as f:
                if 'AssertionError' in f.read():
                    summary.append((test, "FAIL"))
                    status = 'failure'
                else:
                    summary.append((test, "OK"))

    logs_path = '{}/logs.tar.gz'.format(result_folder)
    if not os.path.exists(logs_path):
        logging.info("No logs tar on path %s", logs_path)
    else:
        paths.append(logs_path)
    stdout_path = '{}/stdout.log'.format(result_folder)
    if not os.path.exists(stdout_path):
        logging.info("No stdout log on path %s", stdout_path)
    else:
        paths.append(stdout_path)
    stderr_path = '{}/stderr.log'.format(result_folder)
    if not os.path.exists(stderr_path):
        logging.info("No stderr log on path %s", stderr_path)
    else:
        paths.append(stderr_path)

    description = "SQLancer test run. See report"

    return status, description, summary, paths

def write_results(results_file, status_file, results, status):
    with open(results_file, 'w') as f:
        out = csv.writer(f, delimiter='\t')
        out.writerows(results)
    with open(status_file, 'w') as f:
        out = csv.writer(f, delimiter='\t')
        out.writerow(status)

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
    parser = argparse.ArgumentParser(description="ClickHouse script for parsing results of sqlancer test")
    parser.add_argument("--in-results-dir", default='/test_output/')
    parser.add_argument("--out-results-file", default='/test_output/test_results.tsv')
    parser.add_argument("--out-status-file", default='/test_output/check_status.tsv')
    args = parser.parse_args()

    state, description, test_results, logs = process_result(args.in_results_dir)
    logging.info("Result parsed")
    status = (state, description)
    write_results(args.out_results_file, args.out_status_file, test_results, status)
    logging.info("Result written")


from flask import Flask
from flask import render_template, request
import subprocess, os, base64
from subprocess import PIPE, Popen

app = Flask(__name__)

@app.route('/')
def Compiler():
    check = ''
    return render_template('home.html', check=check)

@app.route('/submit', methods=['GET', 'POST'])
def submit():
    if request.method == 'POST':
        code = request.form['code']
        inp = request.form['input']
        chk = request.form.get('check')

        if chk == '1':
            # If checkbox was ckecked then the input field will stay the same and checkbox will be checked.
            check = 'checked'
        else:
            # If checkbox was not ckecked then the input field will be empty and checkbox will be unchecked.
            inp = ""
            check = ''
        output = cpp_complier_output(code, inp, chk)

    return render_template('home.html', code=code, input=inp, output=output, check=check)

def cpp_complier_output(code, inp, chk):
    if not os.path.exists('test.cpp'):
        with open('test.cpp', 'w') as file_test:
            file_test.write(code)

    application = "C:\\gcc\\bin\\g++.exe"

    # result = subprocess.run([application, "test.cpp", "-o", "new.exe"], stderr=PIPE)
    result = subprocess.run([application, "test.cpp", "-o", "new.exe"], stderr=PIPE)
    check = result.returncode

    if check != 0:
        if os.path.exists('test.cpp'):
            os.remove('test.cpp')

        if os.path.exists('new.exe'):
            os.remove('new.exe')
        return result.stderr
    else:
        if chk == '1':
            inp = str.encode(inp)
            r = subprocess.run(["new"], input=inp, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        else:
            r = subprocess.run(["new"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        if os.path.exists('test.cpp'):
            os.remove('test.cpp')

        if os.path.exists('new.exe'):
            os.remove('new.exe')
        outp = r.stdout
        return outp.decode("windows-1251")

if __name__ == '__main__':
    app.run()


#!/usr/bin/env python

import math
import copy

import matplotlib.pyplot as plt

def plot_obstacles(previous_obstacles, new_obstacles, updated_obstacles, updated_labels):
    # Extract x and y coordinates from the obstacles lists
    prev_x, prev_y, new_x, new_y, updated_x, updated_y  = [], [],[],[],[],[]
    if len(previous_obstacles)>0:
        prev_x, prev_y = zip(*previous_obstacles)
    if len(new_obstacles)>0:
        new_x, new_y = zip(*new_obstacles)
    if len(updated_obstacles)>0:
        updated_x, updated_y = zip(*updated_obstacles)
    # Create a new figure
    plt.figure()

    # Plot the obstacle positions from each list
    #plt.scatter(prev_x, prev_y, color='red', label='Previous Obstacles', s=100)
    #plt.scatter(new_x, new_y, color='blue', label='New Obstacles', s=80)
    plt.scatter(updated_x, updated_y, color='green', label='Updated Obstacles')

    for obstacle, label in zip(updated_obstacles, updated_labels):
        plt.text(obstacle[0], obstacle[1], label, ha='center', va='bottom')  # Display the label near the obstacle

    # Add labels and legend
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.xlim(0, 1.4)
    plt.ylim(0, 1.4)
    plt.legend()

    # Show the plot
    plt.show(block=False)

def calculate_distance(point1, point2):
    x1, y1 = point1
    x2, y2 = point2
    return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)

def update_obstacles(previous_obstacles, new_obstacles, previous_labels, new_labels, camera_position, camera_orientation=0.2, fov=160, task="task1"):
    # Calculate FOV boundaries
    camera_orientation = math.degrees(camera_orientation)
    fov_half_angle = math.radians(fov / 2)
    fov_left_boundary = math.radians(camera_orientation) - fov_half_angle
    fov_right_boundary = math.radians(camera_orientation) + fov_half_angle

    # Initialize the updated obstacle positions list
    updated_obstacles = []
    updated_labels = []

    # Identify previous obstacles in the FOV
    obstacles_in_fov = []
    labels_in_fov = []
    for obstacle, label in zip(previous_obstacles, previous_labels):
        angle = math.atan2(obstacle[1] - camera_position[1], obstacle[0] - camera_position[0])
        angle = math.degrees(angle)
        angle_diff = (angle - camera_orientation + 180) % 360 - 180
        if -fov / 2 <= angle_diff <= fov / 2:
            obstacles_in_fov.append(obstacle)
            labels_in_fov.append(label)
    print('obstacles_in_fov', obstacles_in_fov)

    copy_obstacles_in_fov = copy.deepcopy(obstacles_in_fov)
    copy_new_obstacles = copy.deepcopy(new_obstacles)

    obstacles_in_fov_used = []
    new_obstacles_used = []
    previous_obstacles_used = []
    # Check if the number of obstacles in FOV is less than the number of new obstacles
    # Update obstacle positions for close obstacles
    for new_obstacle, new_label in zip(new_obstacles, new_labels):
        min_distance = 0.25
        closest_obstacle = None
        closest_label = None
        for obstacle in previous_obstacles:
            distance = math.sqrt((new_obstacle[0] - obstacle[0]) ** 2 + (new_obstacle[1] - obstacle[1]) ** 2)
            if distance < min_distance:
                min_distance = distance
                closest_obstacle = obstacle
                closest_label = label

        # Calculate the updated position as the average for close obstacles
        if closest_obstacle is not None and closest_obstacle not in previous_obstacles_used:
            if calculate_distance(camera_position, closest_obstacle) < 0.6:
                updated_position = new_obstacle
            else:
                updated_position = ((new_obstacle[0] + closest_obstacle[0]) / 2, (new_obstacle[1] + closest_obstacle[1]) / 2) # Calculate the updated position as the average
            #obstacles_in_fov.remove(closest_obstacle) # Remove the closest obstacle to avoid duplicate updating
            # labels_in_fov.remove(closest_label)
            updated_obstacles.append(updated_position) # Append the updated obstacle position to the updated list
            updated_labels.append(new_label)
            #add to used
            previous_obstacles_used.append(closest_obstacle)
            obstacles_in_fov_used.append(closest_obstacle)
            new_obstacles_used.append(new_obstacle)

    # Append the remaining new obstacles and previosu obstacles as updated obstacles that did not find a match
    updated_obstacles.extend(obstacle for obstacle in new_obstacles if obstacle not in new_obstacles_used)
    updated_labels.extend(label for label, obstacle in zip(new_labels, new_obstacles) if obstacle not in new_obstacles_used) #do you want to add previosu points that are in FOV but not used?
    updated_obstacles.extend(obstacle for obstacle in previous_obstacles if obstacle not in previous_obstacles_used)
    updated_labels.extend(label for label, obstacle in zip(previous_labels, previous_obstacles) if obstacle not in previous_obstacles_used)

    plot_obstacles(previous_obstacles, copy_new_obstacles, updated_obstacles, updated_labels)
    return updated_obstacles, updated_labels


import os, sys, argparse
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
import pandas as pd

import tensorflow as tf
import tensorflow.keras.backend as K
from tensorflow.keras.layers import Add, Concatenate, Dense, Embedding, Flatten, Input, InputLayer, Lambda, Layer, Reshape, Subtract
tf.keras.backend.set_floatx('float64')

import util

elem_z = {
        'H'  : 1,
        'C'  : 6,
        'N'  : 7,
        'O'  : 8,
        'F'  : 9,
        'P'  : 15,
        'S'  : 16,
        'CL' : 17,
        'BR' : 35,
        }

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Train an atomic multipole model')
    parser.add_argument('dataset_train',
                        help='Dataset for training (must be in ./data/)')
    parser.add_argument('dataset_val',
                        help='Dataset for validation (must be in ./data/)')
    parser.add_argument('modelname',
                        help='Name for saving model')
    args = parser.parse_args(sys.argv[1:])

    if os.path.isfile(f'models/{args.modelname}.hdf5'):
        print(f'Model models/{args.modelname}.hdf5 already exists!')
        exit()

    # these are defined by the dataset
    pad_dim = 40
    nelem = 36

    # this is up to the user
    nembed = 10
    nepochs = 200
    nnodes = [256,128,64]
    nmessage = 3

    # make the model
    mus = np.linspace(0.8, 5.0, 43)
    etas = np.array([-100.0] * 43)
    model = util.get_model(mus, etas, pad_dim, nelem, nembed, nnodes, nmessage)

    model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
                  loss=['mse', util.mse_mp, util.mse_mp, util.mse_mp],
                  loss_weights=[1.0, 1.0, 1.0, 1.0],
                  metrics=[util.mae_mp])
    print(model.summary())

    # load data
    RT, ZT, yT = util.get_data(f'data/{args.dataset_train}.pkl', pad_dim)
    RV, ZV, yV = util.get_data(f'data/{args.dataset_val}.pkl', pad_dim)

    #RT, ZT, yT = RT[:800], ZT[:800], yT[:800]
    #RV, ZV, yV = RV[:800], ZV[:800], yV[:800]

    # monopole
    yV_ = yV[:,:,0]

    # dipole (mu_x, mu_y, mu_z)
    yV_i_ = yV[:,:,1:4]

    # quadrupole diagonal (Q_xx, Q_yy, Q_zz)
    yV_ii_ = yV[:,:,[4,7,9]]

    # quadrupole off-diagonal (Q_xy, Q_xz, Q_yz)
    yV_ij_ = yV[:,:,[5,6,8]]

    print('Validation Target Magnitudes (MAD):')
    for Z_subset, y_subset in [(ZV, [yV_, yV_i_, yV_ii_, yV_ij_])]:
        y_subset_pos = [ys[Z_subset > 0] for ys in y_subset]
        mad = [np.mean(np.abs(ys - np.mean(ys))) for ys in y_subset_pos]
        print(f'ALL ({y_subset_pos[0].shape[0]:6d}) : q({mad[0]:.4f}) mu({mad[1]:.4f}) Qii({mad[2]:.4f}) Qij({mad[3]:.4f})')
        for name, z in elem_z.items():
            mask = (Z_subset == z)
            y_element = [ys_[mask] for ys_ in y_subset]
            if np.sum(mask) > 0:
                mad = [np.mean(np.abs(ys - np.mean(ys))) for ys in y_element]
            else:
                mad = [np.nan for ys in y_element]
            print(f'{name:3s} ({np.sum(mask):6d}) : q({mad[0]:.4f}) mu({mad[1]:.4f}) Qii({mad[2]:.4f}) Qij({mad[3]:.4f})')
        print()

    print('Fitting Model...')
    callbacks = [tf.keras.callbacks.ModelCheckpoint(f'models/{args.modelname}.hdf5', save_best_only=True, monitor='val_loss', mode='min'),
                 tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=(10.0 ** (-1/4)), patience=10, verbose=1, mode='min', min_delta=0, cooldown=0, min_lr=(10.0 ** -5))]
    model.fit(x=util.RotationGenerator(RT, ZT, yT, batch_size=8),
              epochs=nepochs,
              validation_data=([RV, ZV], [yV_, yV_i_, yV_ii_, yV_ij_]),
              callbacks=callbacks,
              verbose=2)

    print('...Done')

    for w in model.get_layer(name='rbf').get_weights():
        print(w)

    yV_pred = model.predict([RV, ZV])

    yV_err = yV_pred[0] - yV_
    yV_i_err = yV_pred[1] - yV_i_
    yV_ii_err = yV_pred[2] - yV_ii_
    yV_ij_err = yV_pred[3] - yV_ij_

    print('Validation Prediction Magnitudes (MAE):')
    for Z_subset, y_subset in [(ZV, [yV_err, yV_i_err, yV_ii_err, yV_ij_err])]:
        y_subset_pos = [ys[Z_subset > 0] for ys in y_subset]
        mae = [np.mean(np.abs(ys)) for ys in y_subset_pos]
        print(f'ALL ({y_subset_pos[0].shape[0]:6d}) : q({mae[0]:.4f}) mu({mae[1]:.4f}) Qii({mae[2]:.4f}) Qij({mae[3]:.4f})')
        for name, z in elem_z.items():
            mask = (Z_subset == z)
            y_element = [ys_[mask] for ys_ in y_subset]
            if np.sum(mask) > 0:
                mae = [np.mean(np.abs(ys)) for ys in y_element]
            else:
                mae = [np.nan for ys in y_element]
            print(f'{name:3s} ({np.sum(mask):6d}) : q({mae[0]:.4f}) mu({mae[1]:.4f}) Qii({mae[2]:.4f}) Qij({mae[3]:.4f})')
        print()


import pathlib
import math
import pygame as pg
# bd is short for board
bd_size = 900
grid_size = bd_size // 19
stone_radius = grid_size // 2
cross_half_len = stone_radius // 3
center_circle = [(3, 3), (3, 9), (3, 15), (9, 3), (9, 9), (9, 15),
                    (15, 3), (15, 9), (15, 15)]
circle_radius = stone_radius // 5
base_dir = pathlib.Path(__file__).parent.parent
resource_dir = base_dir / 'resource'
bg_img_path = resource_dir / 'img' / 'back.png'
bg_music_path = resource_dir / 'music' / 'back.flac'
bg_music_volume = 0.6
bg_music_diff = 0.15
white_id = 0
black_id = 1

notice_width_grids = 8
notice_height_grids = 10
notice_width = grid_size * notice_width_grids
notice_height = grid_size * notice_height_grids
notice_leftup_coord = (5, 4)

header_font = pg.font.get_default_font()
header_size = grid_size
text_font = pg.font.get_default_font()
text_size = grid_size

class QuitException(Exception):
    pass


import os
import time

import aiofiles
import aiohttp
import asyncio
from retrying import retry

class Spider(object):
    """
    下载路径在实例化时候指定，比如:r'd:\test\\'，这个目录如果不存在，会出错。
    默认路径为当前文件下的downpic目录，此目录如果不存在会自动生成
    """

    def __init__(self, down_path='', ):
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36',
        }

        self.data = {"type": "1", "choose_type": "", "page": "1", "size": "20"}
        self.num = 0
        if down_path == "":
            if 'downpic' not in os.listdir('.'):  # 当前目录下的downpic目录
                os.mkdir('downpic')
            self.path = os.path.join(os.path.abspath('.'), 'downpic')
            os.chdir(self.path)  # 进入文件下载路径

        self.down_path = down_path
        self.url = "https://www.yituhuitu.com/api/newapi/SourceMaterial/index"
        self.beforeurl = 'https://admin.yituhuitu.com/'
        self.limit = 10  # tcp连接数
        self.page = 0
        self.sleep = 0  # 每页抓取间隔时间
        # self.headers['Cookie']=(self.getcookie(self.url[:-6]))

    def getcookie(self, url):
        from playwright.sync_api import sync_playwright
        with sync_playwright() as playwright:
            browser = playwright.chromium.launch(headless=True)
            context = browser.new_context()
            page = context.new_page()
            page.goto(url)
            cookie = context.cookies()
            # ---------------------
            context.close()
            browser.close()
            cookie = ';'.join([f'{eh["name"]}={eh["value"]}' for eh in cookie])
            return cookie

    @retry(stop_max_attempt_number=5, wait_fixed=10000)  # 如果出错10秒后重试，最多重试5次
    async def run(self, startpage, endpage):
        async with aiohttp.TCPConnector(limit=self.limit) as conn:  # 限制tcp连接数
            async with aiohttp.ClientSession(connector=conn, headers=self.headers, ) as session:
                if endpage == 0:
                    async with session.post(self.url, data=self.data) as respone:
                        r = await respone.json(content_type='text/html')
                        ncount = r["data"]["count"]
                        page_list = r["data"]["page_limit"]
                        total_page = ncount // int(page_list) + 1 if ncount % int(page_list) else 0
                        endpage = total_page
                        print(f'总页数：{total_page}')
                data = self.data
                for pagen in range(startpage, endpage + 1):
                    data.update({'page': str(pagen)})
                    async with session.post(self.url, data=data) as respone:
                        r = await respone.json(content_type='text/html')
                        # print(r)
                        urls = r['data']['archivesInfo']
                        # print((urls))
                        # 开始爬一页图片
                        tasks = [self._get_content(link) for link in urls]
                        await asyncio.gather(*tasks, return_exceptions=True)
                        await asyncio.sleep(self.sleep)  # 每页间隔时间，太快了，服务器不让抓
                        self.page += 1
                        print(f'爬取{self.page}页成功')

        print(f'一共下载成功{self.num}张图片')

    async def _get_img_links(self, page, session):  # 获取图片连接

        try:
            data = {'p': str(page)}
            async with session.get(url=self.url, data=data) as respone:
                r = await respone.text()
                print(r)
                # urls = r['data']
                # CONCURRENCY = 20
                # semaphore = asyncio.Semaphore(CONCURRENCY)
                # getpictasks = [self._get_content(ehurl, semaphore) for ehurl in urls]
                # await asyncio.gather(*getpictasks, return_exceptions=True)
                self.page += 1
                print(f'下载成功{self.page}页')

        except Exception as e:
            print(e)

    async def _get_content(self, link, ):  # 传入的是图片连接
        if link['litpic'].startswith('/'):
            link['litpic'] = self.beforeurl + link['litpic']
        # async with semaphore:
        async with aiohttp.ClientSession() as session:
            try:
                async with session.get(url=link['litpic']) as response:
                    content = await response.read()
                await self._write_img(f"{link['aid']}.{link['litpic'].split('.')[-1]}", content)
            except (asyncio.TimeoutError, ClientPayloadError):
                pass

    async def _write_img(self, file_name, content):
        file_name = os.path.join(self.down_path, file_name)
        # file_name += '.jpg'
        async with aiofiles.open(file_name, 'wb') as f:
            await f.write(content)
            # print('下载第%s张图片成功' % self.num)
            self.num += 1

if __name__ == '__main__':
    start_time = time.perf_counter()
    down_path = r'D:\Download'
    startpage = 1
    endpage = 0  # 0默认全部爬取
    spider = Spider(down_path)

    types = ['1', '2']  # 对应 新款图案浏览 满印图案浏览
    # 原创付费爆款 要vip，无法爬取
    for ehtype in types:
        spider.data['type'] = ehtype
        loop = asyncio.get_event_loop()
        loop.run_until_complete(spider.run(startpage, endpage, ))
    print(f'总用时：{time.perf_counter() - start_time:.0f}秒')


from extract_video_frames import *
from make_barcode import *

videopath = 'video.mp4'

if os.path.exists(videopath):
    frames_dir = extract_video_frames(videopath)
    barcode = make_barcode(frames_dir, bar_width=20, dims=(1920,1080))
else:
    print('ERROR: video file does not exist')


from os import path
from tkinter import Label, Frame, TOP, BOTTOM, RIGHT, LEFT, N, E, S, W, X, Y, BOTH, Tk, filedialog
import math
from pathlib import Path
import pyautogui
import numpy as np
from time import sleep

from PIL import Image, ImageTk
from pyautogui import position
import cv2

from src.auto_align import auto_align

from src.lib import state_manager
from src.lib.variables import Env, WATER_COMPANIES
from src.lib.tk_overlay import TkOverlay
from src.lib.pdf_processor import PdfProcessor
from src.lib.tk_resizer import TkResizer
from src.lib.utils import Utils
from src.lib.file_prompt import FilePromptSave

class Snap(TkOverlay):

  def __init__(self, root, pipe_type, water_company):

    super().__init__(root)

    self.initial_img = Image.open(path.join(Env.appdata_path, "images/initial.png"))

    state = state_manager.get()
    self.initial_x = 0
    self.initial_y = 0
    self.previous_rotation = int(state["rotation"] if "rotation" in state else 0)

    self.water_company = water_company
    self.capture_x = int(state["x"] if "x" in state else 0)
    self.capture_y = int(state["y"] if "y" in state else 0)
    self.capture_size = int(state["size"] if "size" in state else 0)
    self.capture_rotation = int(state["rotation"] if "rotation" in state else 0)

    self.pipe_type = pipe_type
    self.img_path = path.join(Env.appdata_path, f"images/{pipe_type}.png")

    self.generate_frames()

    self.root.attributes("-fullscreen", True)
    self.root.attributes("-alpha", 0.5)

    self.image_frame = Frame(
      self.back_frame,
      bg="#212121"
    )

    self.Resizer = TkResizer(self.image_frame)

    initial_photoimage = ImageTk.PhotoImage(self.initial_img)

    self.image_label = Label(
      self.image_frame,
      image=initial_photoimage,
      bg="#212121",
      borderwidth=0,
      cursor="fleur"
    )
    self.image_label.image = initial_photoimage
    self.image_label.pack()

    self.top_label = Label(
      self.back_frame,
      text="Click and drag to align, resize and rotate",
      font=("Courier", 16),
      bg="black",
      fg="white",
      padx=10, pady=10
    )
    self.top_label.pack(side=TOP, pady=(100, 0))

    self.top_info_label = Label(
      self.back_frame,
      font=("Calibri", 12),
      bg="black",
      fg="white",
      padx=10, pady=5
    )
    self.top_info_label.pack(side=TOP, pady=(5, 0))

    self.right_label = Label(
      self.back_frame,
      text="ARROW KEYS - Align\nPLUS - Enlarge\nMINUS - Shrink\n\nENTER - Confirm\nESC - Cancel",
      font=("Courier", 16),
      bg="black",
      fg="white",
      padx=10, pady=10
    )
    self.right_label.pack(side=RIGHT, padx=(0, 50), anchor=N)

    self.root.bind("<Key>", self.key_press)
    self.root.bind("<Button-1>", self.mouse_1_down)
    self.root.bind("<ButtonRelease-1>", self.mouse_1_up)

    self.image_label.bind("<B1-Motion>", self.mouse_1_move)
    self.image_frame.bind("<Configure>", self.update_top_info_label)

    self.Resizer.bind_events(self.corner_resize, self.on_rotate)

    self.back_frame.after(1, self.back_frame_after)

  def on_destroy(self):
    self.save_state()

  def corner_resize(self, event, corner):
    mouse_x = position()[0]
    mouse_y = position()[1]

    corner_x = self.capture_x
    corner_y = self.capture_y
    if "w" in corner:
      corner_x = self.capture_x + self.capture_size
    if "n" in corner:
      corner_y = self.capture_y + self.capture_size

    relative_x = mouse_x - corner_x
    relative_y = mouse_y - corner_y
    if "w" in corner:
      relative_x = np.invert(relative_x)
    if "n" in corner:
      relative_y = np.invert(relative_y)

    size = relative_x if relative_x > relative_y else relative_y
    size_difference = self.capture_size - size

    if size < 0:
      return

    new_x = self.capture_x
    new_y = self.capture_y
    if "w" in corner:
      new_x = self.capture_x + size_difference
    if "n" in corner:
      new_y = self.capture_y + size_difference

    self.move_initial_img_x(new_x)
    self.move_initial_img_y(new_y)

    self.resize_initial_img(size, True)

  def on_rotate(self, event, side):
    x = position()[0]
    y = position()[1]

    rotation = self.previous_rotation
    if side == "n":
      rotation += (self.initial_x - x) / 4
    elif side == "e":
      rotation += (self.initial_y - y) / 4
    elif side == "s":
      rotation += (x - self.initial_x) / 4
    elif side == "w":
      rotation += (y - self.initial_y) / 4

    self.rotate_initial_img(rotation)

  def mouse_1_down(self, event):
    self.initial_x = position()[0]
    self.initial_y = position()[1]

  def mouse_1_move(self, event):
    self.capture_x = int(self.image_frame.winfo_x())
    self.capture_y = int(self.image_frame.winfo_y())
    mouse_x = position()[0]
    mouse_y = position()[1]

    self.move_initial_img_x(mouse_x - (self.capture_size / 2))
    self.move_initial_img_y(mouse_y - (self.capture_size / 2))

  def mouse_1_up(self, event):
    self.previous_rotation = self.capture_rotation
    self.save_state()

  def save_state(self):
    state_manager.update({
      "x": self.capture_x,
      "y": self.capture_y,
      "size": self.capture_size
    })

  def key_right(self):
    self.move_initial_img_x(self.capture_x + 1)

  def key_left(self):
    self.move_initial_img_x(self.capture_x - 1)

  def move_initial_img_x(self, x):
    self.capture_x = int(x)
    if x <= 0:
      x = 0
    if x >= Env.res_x - self.capture_size:
      x = Env.res_x - self.capture_size
    self.image_frame.place(x=x + math.floor(self.capture_size / 2))

  def key_up(self):
    self.move_initial_img_y(self.capture_y - 1)

  def key_down(self):
    self.move_initial_img_y(self.capture_y + 1)

  def move_initial_img_y(self, y):
    self.capture_y = int(y)
    if y <= 0:
      y = 0
    if y >= Env.res_y - self.capture_size:
      y = Env.res_y - self.capture_size
    self.image_frame.place(y=y + math.floor(self.capture_size / 2))

  def update_top_info_label(self, event):
    self.top_info_label.config(
      text=f"({self.capture_x}, {self.capture_y}) | " +
      f"{self.capture_size} x {self.capture_size}"
    )

  def key_plus(self):
    self.resize_initial_img(self.capture_size + 2, False)

  def key_minus(self):
    self.resize_initial_img(self.capture_size - 2, False)

  def finish(self):

    self.root.destroy()

    self.take_screenshot()
    self.convert_to_alpha()
    self.apply_masks()

    PdfProcess = PdfProcessor(path.join(Env.index_dir, f"./pdf_templates/{self.pipe_type}_template.pdf"))
    PdfProcess.insert_img(
      path.join(Env.appdata_path, f"images/{self.pipe_type}_final.png"),
      ( 60, 46, 472, 472 ), 0
    )
    PdfProcess.insert_img(
      path.join(Env.index_dir, "./images/copyright.png"),
      ( 60, 510, 210, 9 ), 0
    )
    print("Processed PDF")

    state = state_manager.get()

    output_path = map_path = FilePromptSave(
      f"Save {self.pipe_type} PDF file", state["save_dir"] if "save_dir" in state else "/",
      [("PDF File", "*.pdf")], ".pdf", state["reference"] if "reference" in state else "" + (" CC" if self.pipe_type == "clean" else " DD")
    ).path

    if output_path:
      PdfProcess.pdf.save(output_path, deflate=True)
      Utils.send_toast(
        f"Created {path.basename(output_path)} at",
        output_path
      )

  def resize_initial_img(self, size, no_offset=False):
    size = int(size)
    self.capture_size = size
    if size <= 0 or size > Env.res_x or size > Env.res_y:
      return

    initial_photoimage = ImageTk.PhotoImage(
      self.initial_img
        .rotate(self.capture_rotation, Image.BILINEAR, expand=True)
        .resize((size, size), Image.BILINEAR)
      )
    self.image_label.config(image=initial_photoimage)
    self.image_label.image = initial_photoimage
    offset = -1 if size > self.image_label.winfo_width() else 1
    if no_offset:
      offset = 0
    self.move_initial_img_x(self.capture_x + offset)
    self.move_initial_img_y(self.capture_y + offset)

  def rotate_initial_img(self, rotation):
    rotation = int(rotation)
    self.capture_rotation = rotation

    initial_photoimage = ImageTk.PhotoImage(
      self.initial_img
        .rotate(rotation, Image.BILINEAR, expand=True)
        .resize((self.capture_size, self.capture_size), Image.BILINEAR)
    )
    self.image_label.config(image=initial_photoimage)
    self.image_label.image = initial_photoimage

  def take_screenshot(self):
    Path(Path(self.img_path).parent).mkdir(parents=True, exist_ok=True)

    print(f"{self.capture_size}, {self.capture_size}")

    pyautogui.screenshot(self.img_path, (
      self.capture_x + 1, self.capture_y + 1,
      self.capture_size, self.capture_size
    ))

    print(f"{self.pipe_type} screenshot taken at: {self.capture_x}, {self.capture_y}\n  Size: {self.capture_size} x {self.capture_size}")

  def convert_to_alpha(self):
    if not path.exists(self.img_path):
      raise f"No {self.pipe_type} image found"
    img = cv2.imread(self.img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
    cv2.imwrite(self.img_path, img)

  def apply_masks(self):
    final_img = Image.open(path.join(Env.appdata_path, "images/initial.png"))
    img = cv2.imread(self.img_path, flags=cv2.IMREAD_UNCHANGED)
    mask_dir_path = path.join(Env.appdata_path, f"images/masks/{self.pipe_type}")

    Path(mask_dir_path).mkdir(parents=True, exist_ok=True)
    bgra_bounds = WATER_COMPANIES[self.water_company][self.pipe_type]
    for i in bgra_bounds:
      mask_path = path.join(mask_dir_path, f"{i}.png")
      bgra_bound = bgra_bounds[i]
      masked_img = self.apply_mask(img, bgra_bound)
      print(f"Generated {i} mask")
      masked_img = cv2.resize(
        masked_img,
        (final_img.width, final_img.height),
        interpolation=cv2.INTER_CUBIC
      )
      cv2.imwrite(mask_path, masked_img)
      masked_img = Image.open(mask_path)
      final_img.paste(masked_img, (0, 0), masked_img)

    final_img.save(path.join(Env.appdata_path, f"images/{self.pipe_type}_final.png"), "PNG")

  def apply_mask(self, img, bgra_bound):
    mask = cv2.inRange(
      img,
      np.asarray(bgra_bound if type(bgra_bound) == list else bgra_bound[0]),
      np.asarray(bgra_bound if type(bgra_bound) == list else bgra_bound[1])
    )
    return cv2.bitwise_and(
      img,
      img,
      mask=mask
    )

  def key_press(self, event):
    key_events = {
      27: self.root.destroy,
      37: self.key_left,
      38: self.key_up,
      39: self.key_right,
      40: self.key_down,
      107: self.key_plus,
      109: self.key_minus,
      13: self.finish
    }
    try:
      key_events[event.keycode]()
    except Exception as err:
      print(err)
      pass

  def back_frame_after(self):
    self.back_frame.focus_force()
    with open(path.join(Env.appdata_path, "state.json"), "r", encoding='utf-8') as state_file:
      self.resize_initial_img(self.capture_size, True)
      self.image_frame.place(
        anchor="center",
        x=self.capture_x + (self.capture_size / 2),
        y=self.capture_y + (self.capture_size / 2)
      )


__author__ = 'vamsee'

import geojson as geo

Center = (-83.79706,42.268138)
Center_Point = geo.Point(Center)

def get_locations(filename,lat_idx,long_idx, write_filename,point_properties):
    opened_file = open(filename)
    features_list = []
    for line in opened_file:
        line_contents = line.split(',')
        (longitude,latitude) = (float(line_contents[lat_idx])/10000000,float(line_contents[long_idx])/10000000 )
        f = geo.Feature(geometry=geo.Point( (longitude,latitude)), \
            properties = point_properties )
        features_list.append(f)
        #break
    F = geo.FeatureCollection(features_list)
    # generate data
    geo_data_str = geo.dumps(F,sort_keys=True)

    # Write into file
    json_file  = open('../Mapbox/data/'+write_filename,'w')
    json_file.write(geo_data_str)
    json_file.close()

# get_locations('../../Transportation-Data/Data/RSE_BSM/RSE_BSM_trimmed.csv')
get_locations('../BSM-RSE.csv',7,6,'vehicle-locations.json',{'title':'Vehicle Location',  "marker-symbol": "car", "marker-size": "medium", "marker-color": "#09f"})
get_locations('../Geometry.csv',4,3,'rse-data-locations.json',{'title':'RSE Location',  "marker-symbol": "commercial", "marker-size": "large", "marker-color": "#f83"})