import os
import random
from io import TextIOWrapper
from typing import Any
from typing import Callable
from typing import Dict
from typing import List
from typing import Optional
from typing import Tuple
from typing import Type
from typing import Union

from ..agents.base import Agent
from ..events import EventABC
from ..events import EventHook
from ..index_market import IndexMarket
from ..logs.base import CancelLog
from ..logs.base import ExecutionLog
from ..logs.base import Log
from ..logs.base import Logger
from ..logs.base import MarketStepBeginLog
from ..logs.base import MarketStepEndLog
from ..logs.base import OrderLog
from ..logs.base import SessionBeginLog
from ..logs.base import SessionEndLog
from ..logs.base import SimulationBeginLog
from ..logs.base import SimulationEndLog
from ..market import Market
from ..order import Cancel
from ..order import Order
from ..session import Session
from ..simulator import Simulator
from ..utils.class_finder import find_class
from ..utils.json_extends import json_extends
from .base import Runner

class SequentialRunner(Runner):
    """Sequential runner class."""

    def __init__(
        self,
        settings: Union[Dict, TextIOWrapper, os.PathLike, str],
        prng: Optional[random.Random] = None,
        logger: Optional[Logger] = None,
        simulator_class: Type[Simulator] = Simulator,
    ):
        """initialize.

        Args:
            settings (Union[Dict, TextIOWrapper, os.PathLike, str]): runner configuration.
            prng (random.Random, Optional): pseudo random number generator for this runner.
            logger (Logger, Optional): logger instance.
            simulator_class (Type[Simulator]): type of simulator.

        Returns:
            None
        """
        super().__init__(settings, prng, logger, simulator_class)
        self._pending_setups: List[Tuple[Callable, Dict]] = []

    def _generate_markets(self, market_type_names: List[str]) -> None:
        """generate markets. (Internal method)

        Args:
            market_type_names (List[str]): name list of market type.

        Returns:
            None
        """
        i_market = 0
        for name in market_type_names:
            if name not in self.settings:
                raise ValueError(f"{name} setting is missing in config")
            market_settings: Dict = self.settings[name]
            market_settings = json_extends(
                whole_json=self.settings,
                parent_name=name,
                target_json=market_settings,
                excludes_fields=["from", "to"],
            )
            # TODO: warn "from" and "to" is included in parent setting and not set to this setting.
            n_markets = 1
            id_from = 0
            id_to = 0
            if "numMarkets" in market_settings:
                n_markets = int(market_settings["numMarkets"])
                id_to = n_markets - 1
            if "from" in market_settings or "to" in market_settings:
                if "from" not in market_settings or "to" not in market_settings:
                    raise ValueError(
                        f"both {name}.from and {name}.to are required in json file if you use"
                    )
                if "numMarkets" in market_settings:
                    raise ValueError(
                        f"{name}.numMarkets and ({name}.from or {name}.to) cannot be used at the same time"
                    )
                n_markets = int(market_settings["to"]) - int(market_settings["from"])
                id_from = int(market_settings["from"])
                id_to = int(market_settings["to"])
            if "numMarkets" in market_settings:
                del market_settings["numMarkets"]
            if "from" in market_settings:
                del market_settings["from"]
            if "to" in market_settings:
                del market_settings["to"]
            prefix: str
            if "prefix" in market_settings:
                prefix = market_settings["prefix"]
                del market_settings["prefix"]
            else:
                prefix = name + ("-" if n_markets > 1 else "")
            if "class" not in market_settings:
                raise ValueError(f"class is not defined for {name}")
            market_class: Type[Market] = find_class(
                name=market_settings["class"],
                optional_class_list=self.registered_classes,
            )
            if not issubclass(market_class, Market):
                raise ValueError(
                    f"market class for {name} does not inherit Market class"
                )
            if "fundamentalPrice" in market_settings:
                fundamental_price = float(market_settings["fundamentalPrice"])
            elif "marketPrice" in market_settings:
                fundamental_price = float(market_settings["marketPrice"])
            else:
                raise ValueError(
                    f"fundamentalPrice or marketPrice is required for {name}"
                )
            fundamental_drift: float = 0.0
            if "fundamentalDrift" in market_settings:
                fundamental_drift = float(market_settings["fundamentalDrift"])
            fundamental_volatility: float = 0.0
            if "fundamentalVolatility" in market_settings:
                fundamental_volatility = float(market_settings["fundamentalVolatility"])

            for i in range(id_from, id_to + 1):
                market = market_class(
                    market_id=i_market,
                    prng=random.Random(self._prng.randint(0, 2**31)),
                    simulator=self.simulator,
                    logger=self.logger,
                    name=prefix + (str(i) if n_markets != 1 else ""),
                )
                i_market += 1
                self.simulator._add_market(market=market, group_name=name)
                if not isinstance(market, IndexMarket):
                    self.simulator.fundamentals.add_market(
                        market_id=market.market_id,
                        initial=fundamental_price,
                        drift=fundamental_drift,
                        volatility=fundamental_volatility,
                    )
                self._pending_setups.append(
                    (market.setup, {"settings": market_settings})
                )

    def _generate_agents(self, agent_type_names: List[str]) -> None:
        """generate agents. (Internal method)

        Args:
            agent_type_names (List[str]): name list of agent type.

        Returns:
            None
        """
        i_agent = 0
        for name in agent_type_names:
            if name not in self.settings:
                raise ValueError(f"{name} setting is missing in config")
            agent_settings: Dict = self.settings[name]
            agent_settings = json_extends(
                whole_json=self.settings,
                parent_name=name,
                target_json=agent_settings,
                excludes_fields=["from", "to"],
            )
            # TODO: warn "from" and "to" is included in parent setting and not set to this setting.
            n_agents = 1
            id_from = 0
            id_to = 0
            if "numAgents" in agent_settings:
                n_agents = int(agent_settings["numAgents"])
                id_to = n_agents - 1
            if "from" in agent_settings or "to" in agent_settings:
                if "from" not in agent_settings or "to" not in agent_settings:
                    raise ValueError(
                        f"both {name}.from and {name}.to are required in json file if you use"
                    )
                if "numAgents" in agent_settings:
                    raise ValueError(
                        f"{name}.numMarkets and ({name}.from or {name}.to) cannot be used at the same time"
                    )
                n_agents = int(agent_settings["to"]) - int(agent_settings["from"])
                id_from = int(agent_settings["from"])
                id_to = int(agent_settings["to"])
            if "numAgents" in agent_settings:
                del agent_settings["numAgents"]
            if "from" in agent_settings:
                del agent_settings["from"]
            if "to" in agent_settings:
                del agent_settings["to"]
            prefix: str
            if "prefix" in agent_settings:
                prefix = agent_settings["prefix"]
                del agent_settings["prefix"]
            else:
                prefix = name + ("-" if n_agents > 1 else "")

            if "class" not in agent_settings:
                raise ValueError(f"class is not defined for {name}")
            agent_class: Type[Agent] = find_class(
                name=agent_settings["class"],
                optional_class_list=self.registered_classes,
            )
            if not issubclass(agent_class, Agent):
                raise ValueError(f"agent class for {name} does not inherit Agent class")
            if "markets" not in agent_settings:
                raise ValueError(f"markets is required in {name}")
            accessible_market_names: List[str] = agent_settings["markets"]
            accessible_market_ids: List[int] = sum(
                [
                    list(
                        map(
                            lambda m: m.market_id,
                            self.simulator.markets_group_name2market[x],
                        )
                    )
                    for x in accessible_market_names
                ],
                [],
            )
            for i in range(id_from, id_to + 1):
                agent = agent_class(
                    agent_id=i_agent,
                    prng=random.Random(self._prng.randint(0, 2**31)),
                    simulator=self.simulator,
                    logger=self.logger,
                    name=prefix + (str(i) if n_agents != 1 else ""),
                )
                i_agent += 1
                self.simulator._add_agent(agent=agent, group_name=name)
                self._pending_setups.append(
                    (
                        agent.setup,
                        {
                            "settings": agent_settings,
                            "accessible_markets_ids": accessible_market_ids,
                        },
                    )
                )

    def _set_fundamental_correlation(self) -> None:
        """set fundamental correlation. (Internal method)"""
        if "fundamentalCorrelations" in self.settings["simulation"]:
            corr_settings: Dict = self.settings["simulation"]["fundamentalCorrelations"]
            for key, value in corr_settings.items():
                if key == "pairwise":
                    if (
                        not isinstance(value, list)
                        or sum([len(x) != 3 for x in value]) > 0
                    ):
                        raise ValueError(
                            "simulation.fundamentalCorrelations.pairwise has invalid format data"
                        )
                    for (market1_name, market2_name, corr) in value:
                        market1 = self.simulator.name2market[market1_name]
                        market2 = self.simulator.name2market[market2_name]
                        for m in [market1, market2]:
                            if (
                                self.simulator.fundamentals.volatilities[m.market_id]
                                == 0.0
                            ):
                                raise ValueError(
                                    f"For applying fundamental correlation fo {m.name}, "
                                    f"fundamentalVolatility for {m.name} is required"
                                )
                        self.simulator.fundamentals.set_correlation(
                            market_id1=market1.market_id,
                            market_id2=market2.market_id,
                            corr=float(corr),
                        )
                else:
                    raise NotImplementedError(
                        f"{key} for simulation.fundamentalCorrelations is not supported"
                    )

    def _generate_sessions(self) -> None:
        """generate sessions. (Internal method)"""
        if "sessions" not in self.settings["simulation"]:
            raise ValueError("sessions is missing under 'simulation' config")
        session_settings: Dict = self.settings["simulation"]["sessions"]
        if not isinstance(session_settings, list):
            raise ValueError("simulation.sessions must be list[dict]")
        i_session = 0
        i_event = 0
        session_start_time: int = 0
        for session_setting in session_settings:
            if "sessionName" not in session_setting:
                raise ValueError(
                    "for each element in simulation.sessions must have sessionName"
                )
            session = Session(
                session_id=i_session,
                prng=random.Random(self._prng.randint(0, 2**31)),
                session_start_time=session_start_time,
                simulator=self.simulator,
                name=str(session_setting["sessionName"]),
                logger=self.logger,
            )
            i_session += 1
            if "iterationSteps" not in session_setting:
                raise ValueError(
                    "iterationSteps is required in each element of simulation.sessions"
                )
            session_start_time += session_setting["iterationSteps"]
            self.simulator._add_session(session=session)
            self._pending_setups.append((session.setup, {"settings": session_setting}))
            if "events" in session_setting:
                event_names: List[str] = session_setting["events"]
                for event_name in event_names:
                    event_setting = self.settings[event_name]
                    event_setting = json_extends(
                        whole_json=self.settings,
                        parent_name=event_name,
                        target_json=event_setting,
                        excludes_fields=["numMarkets", "from", "to", "prefix"],
                    )
                    if "class" not in event_setting:
                        raise ValueError(f"class is required in {event_name}")
                    event_class_name = event_setting["class"]
                    event_class: Type[EventABC] = find_class(
                        name=event_class_name,
                        optional_class_list=self.registered_classes,
                    )
                    event = event_class(
                        event_id=i_event,
                        prng=random.Random(self._prng.randint(0, 2**31)),
                        session=session,
                        simulator=self.simulator,
                        name=event_name,
                    )
                    i_event += 1
                    self._pending_setups.append(
                        (event.setup, {"settings": event_setting})
                    )

                    def event_hook_setup(_event: EventABC):
                        event_hooks: List[EventHook] = _event.hook_registration()
                        for event_hook in event_hooks:
                            self.simulator._add_event(event_hook)

                    self._pending_setups.append((event_hook_setup, {"_event": event}))

    def _setup(self) -> None:
        """runner setup. (Internal method)"""
        if "simulation" not in self.settings:
            raise ValueError("simulation is required in json file")

        if "markets" not in self.settings["simulation"]:
            raise ValueError("simulation.markets is required in json file")
        market_type_names: List[str] = self.settings["simulation"]["markets"]
        if (
            not isinstance(market_type_names, list)
            or sum([not isinstance(m, str) for m in market_type_names]) > 0
        ):
            raise ValueError("simulation.markets in json file have to be list[str]")
        self._generate_markets(market_type_names=market_type_names)
        self._set_fundamental_correlation()

        if "agents" not in self.settings["simulation"]:
            raise ValueError("agents.markets is required in json file")
        agent_type_names: List[str] = self.settings["simulation"]["agents"]
        if (
            not isinstance(agent_type_names, list)
            or sum([not isinstance(m, str) for m in agent_type_names]) > 0
        ):
            raise ValueError("simulation.agents in json file have to be list[str]")
        self._generate_agents(agent_type_names=agent_type_names)

        if "sessions" not in self.settings["simulation"]:
            raise ValueError("simulation.sessions is required in json file")
        session_settings: List[Dict[str, Any]] = self.settings["simulation"]["sessions"]
        if (
            not isinstance(session_settings, list)
            or sum([not isinstance(m, dict) for m in session_settings]) > 0
        ):
            raise ValueError("simulation.sessions in json file have to be List[Dict]")
        self._generate_sessions()

        _ = [func(**kwargs) for func, kwargs in self._pending_setups]

    def _collect_orders_from_normal_agents(
        self, session: Session
    ) -> List[List[Union[Order, Cancel]]]:
        """collect orders from normal_agents. (Internal method)
        orders are corrected until the total number of orders reaches max_normal_orders

        Args:
            session (Session): session.

        Returns:
            List[List[Union[Order, Cancel]]]: orders lists.
        """
        agents = self.simulator.normal_frequency_agents
        agents = self._prng.sample(agents, len(agents))
        n_orders = 0
        all_orders: List[List[Union[Order, Cancel]]] = []
        for agent in agents:
            if n_orders >= session.max_normal_orders:
                break
            orders = agent.submit_orders(markets=self.simulator.markets)
            if len(orders) > 0:
                if not session.with_order_placement:
                    raise AssertionError("currently order is not accepted")
                if sum([order.agent_id != agent.agent_id for order in orders]) > 0:
                    raise ValueError(
                        "spoofing order is not allowed. please check agent_id in order"
                    )
                all_orders.append(orders)
                # TODO: currently the original impl is used
                # n_orders += len(orders)
                n_orders += 1
        return all_orders

    def _handle_orders(
        self, session: Session, local_orders: List[List[Union[Order, Cancel]]]
    ) -> List[List[Union[Order, Cancel]]]:
        """handle orders. (Internal method)
        processing local orders and correct and process the orders from high frequency agents.

        Args:
            session (Session): session.
            local_orders (List[List[Union[Order, Cancel]]]): local orders.

        Returns:
            List[List[Union[Order, Cancel]]]: order lists.
        """
        sequential_orders = self._prng.sample(local_orders, len(local_orders))
        all_orders: List[List[Union[Order, Cancel]]] = [*sequential_orders]
        for orders in sequential_orders:
            for order in orders:
                if not session.with_order_placement:
                    raise AssertionError("currently order is not accepted")
                market: Market = self.simulator.id2market[order.market_id]
                if isinstance(order, Order):
                    self.simulator._trigger_event_before_order(order=order)
                    log: OrderLog = market._add_order(order=order)
                    agent: Agent = self.simulator.id2agent[order.agent_id]
                    agent.submitted_order(log=log)
                    self.simulator._trigger_event_after_order(order_log=log)
                elif isinstance(order, Cancel):
                    self.simulator._trigger_event_before_cancel(cancel=order)
                    log_: CancelLog = market._cancel_order(cancel=order)
                    agent = self.simulator.id2agent[order.order.agent_id]
                    agent.canceled_order(log=log_)
                    self.simulator._trigger_event_after_cancel(cancel_log=log_)
                else:
                    raise NotImplementedError
                if session.with_order_execution:
                    logs: List[ExecutionLog] = market._execution()
                    self.simulator._update_agents_for_execution(execution_logs=logs)
                    for execution_log in logs:
                        agent = self.simulator.id2agent[execution_log.buy_agent_id]
                        agent.executed_order(log=execution_log)
                        agent = self.simulator.id2agent[execution_log.sell_agent_id]
                        agent.executed_order(log=execution_log)
                        self.simulator._trigger_event_after_execution(
                            execution_log=execution_log
                        )

            if session.high_frequency_submission_rate < self._prng.random():
                continue

            n_high_freq_orders = 0
            agents = self.simulator.high_frequency_agents
            agents = self._prng.sample(agents, len(agents))
            for agent in agents:
                if n_high_freq_orders >= session.max_high_frequency_orders:
                    break

                high_freq_orders: List[Union[Order, Cancel]] = agent.submit_orders(
                    markets=self.simulator.markets
                )
                if len(high_freq_orders) > 0:
                    if not session.with_order_placement:
                        raise AssertionError("currently order is not accepted")
                    if (
                        sum(
                            [
                                order.agent_id != agent.agent_id
                                for order in high_freq_orders
                            ]
                        )
                        > 0
                    ):
                        raise ValueError(
                            "spoofing order is not allowed. please check agent_id in order"
                        )
                    all_orders.append(high_freq_orders)
                    # TODO: currently the original impl is used
                    n_high_freq_orders += 1
                    # n_high_freq_orders += len(high_freq_orders)
                    for order in high_freq_orders:
                        market = self.simulator.id2market[order.market_id]
                        if isinstance(order, Order):
                            self.simulator._trigger_event_before_order(order=order)
                            log = market._add_order(order=order)
                            agent = self.simulator.id2agent[order.agent_id]
                            agent.submitted_order(log=log)
                            self.simulator._trigger_event_after_order(order_log=log)
                        elif isinstance(order, Cancel):
                            self.simulator._trigger_event_before_cancel(cancel=order)
                            log_ = market._cancel_order(cancel=order)
                            agent = self.simulator.id2agent[order.order.agent_id]
                            agent.canceled_order(log=log_)
                            self.simulator._trigger_event_after_cancel(cancel_log=log_)
                        else:
                            raise NotImplementedError
                        if session.with_order_execution:
                            logs = market._execution()
                            self.simulator._update_agents_for_execution(
                                execution_logs=logs
                            )
                            for execution_log in logs:
                                agent = self.simulator.id2agent[
                                    execution_log.buy_agent_id
                                ]
                                agent.executed_order(log=execution_log)
                                agent = self.simulator.id2agent[
                                    execution_log.sell_agent_id
                                ]
                                agent.executed_order(log=execution_log)
                                self.simulator._trigger_event_after_execution(
                                    execution_log=execution_log
                                )
        return all_orders

    def _update_markets(self, session: Session) -> None:
        """update markets. (Internal method)

        Args:
            session (Session): session.

        Returns:
            None
        """
        local_orders: List[
            List[Union[Order, Cancel]]
        ] = self._collect_orders_from_normal_agents(session=session)
        self._handle_orders(session=session, local_orders=local_orders)

    def _iterate_market_updates(self, session: Session) -> None:
        """iterate market updates. (Internal method)

        Args:
            session (Session): session.

        Returns:
            None
        """
        markets: List[Market] = self.simulator.markets
        for market in markets:
            market._is_running = session.with_order_execution

        for _ in range(session.iteration_steps):
            for market in markets:
                self.simulator._trigger_event_before_step_for_market(market=market)
                if self.logger is not None:
                    log: Log = MarketStepBeginLog(
                        session=session, market=market, simulator=self.simulator
                    )
                    log.read_and_write_with_direct_process(logger=self.logger)
            if session.with_order_placement:
                self._update_markets(session=session)
            for market in markets:
                if self.logger is not None:
                    log = MarketStepEndLog(
                        session=session, market=market, simulator=self.simulator
                    )
                    log.read_and_write_with_direct_process(logger=self.logger)
                self.simulator._trigger_event_after_step_for_market(market=market)
            self.simulator._update_times_on_markets(self.simulator.markets)  # t++

    def _run(self) -> None:
        """main process. (Internal method)"""
        if self.logger is not None:
            log: Log = SimulationBeginLog(simulator=self.simulator)  # must be blocking
            log.read_and_write(logger=self.logger)
            self.logger._process()
        self.simulator._update_times_on_markets(self.simulator.markets)  # t: -1 -> 0

        for session in self.simulator.sessions:
            self.simulator.current_session = session
            self.simulator._trigger_event_before_session(session=session)
            if self.logger is not None:
                log = SessionBeginLog(
                    session=session, simulator=self.simulator
                )  # must be blocking
                log.read_and_write(logger=self.logger)
                self.logger._process()
            self._iterate_market_updates(session=session)
            self.simulator._trigger_event_after_session(session=session)
            if self.logger is not None:
                log = SessionEndLog(
                    session=session, simulator=self.simulator
                )  # must be blocking
                log.read_and_write(logger=self.logger)
                self.logger._process()
        if self.logger is not None:
            log = SimulationEndLog(simulator=self.simulator)  # must be blocking
            log.read_and_write(logger=self.logger)
            self.logger._process()


# https://docs.opencv.org/3.4/df/d0c/tutorial_py_fast.html
# an even faster algorithm for real time
import cv2
import numpy as np

# filename = 'logo/gray/c280.jpg'
# gray = cv2.imread(filename)

filename = 'in/logo.jpg'
img = cv2.imread(filename)
# gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# Initiate FAST object with default values
fast = cv2.FastFeatureDetector_create()

# find and draw the keypoints
kp = fast.detect(img, None)

img2 = cv2.drawKeypoints(img, kp, None, color=(255,0,0))

# Print all default params
print( "Threshold: {}".format(fast.getThreshold()) )
print( "nonmaxSuppression:{}".format(fast.getNonmaxSuppression()) )
print( "neighborhood: {}".format(fast.getType()) )
print( "Total Keypoints with nonmaxSuppression: {}".format(len(kp)) )
cv2.imwrite('out/fast_keypoints_with_nomax.png',img2)

# Disable nonmaxSuppression
fast.setNonmaxSuppression(0)
kp = fast.detect(img,None)
print( "Total Keypoints without nonmaxSuppression: {}".format(len(kp)) )
img3 = cv2.drawKeypoints(img, kp, None, color=(255,0,0))

cv2.imwrite('out/fast_keypoints_without_nomax.png',img3)


#!/usr/bin/env python
# -*- coding: utf-8  -*-
#
# Do a quick comparison of the Planck HFI polarisation angles
#
# Version history:
#
# 14-Apr-2020  M. Peel       Started

import healpy as hp
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
from scipy import odr
from astrocode.astroutils import *
from compare_polang import *
from astrocode.spectra import *
def dodiff(map1, map2):
	diff = map1-map2
	diff[diff < -90] += 180.0
	diff[diff > 90] -= 180.0
	return diff

indirectory = '/Volumes/Toshiba5TB2/maps/planck2018_tqu/'
outdirectory = '/Users/mpeel/Documents/maps/planck2018_tqu_weight/'

beta_d = 1.53 # From Planck 2018 IV
T_d = 19.6 # Ditto
const = get_spectrum_constants()
val_353 = thermaldust_comm(const, 353.0, 1.0, beta_d, T_d)*planckcorr(const, 353.0)
val_217 = thermaldust_comm(const, 217.0, 1.0, beta_d, T_d)*planckcorr(const, 217.0)
val_143 = thermaldust_comm(const, 143.0, 1.0, beta_d, T_d)*planckcorr(const, 143.0)

# wgt_filename = outdirectory+'planck2018_hfi_tqu_60arcmin_v0_combine.fits'
# wgt = hp.read_map(wgt_filename,field=None)
# polang_map_wgt = calc_polang(wgt[1],wgt[2])
p353_filename = indirectory+'2048_60.0smoothed_PlanckR3fullbeam_353_2048_2018_mKCMBunits.fits'
p353 = hp.read_map(p353_filename,field=None)
# polang_map_p353 = calc_polang(p353[1],p353[2])
p217_filename = indirectory+'2048_60.0smoothed_PlanckR3fullbeam_217_2048_2018_mKCMBunits.fits'
p217 = hp.read_map(p217_filename,field=None)
# polang_map_p217 = calc_polang(p217[1],p217[2])
p143_filename = indirectory+'2048_60.0smoothed_PlanckR3fullbeam_143_2048_2018_mKCMBunits.fits'
p143 = hp.read_map(p143_filename,field=None)
# polang_map_p143 = calc_polang(p143[1],p143[2])

# hp.mollview(polang_map_wgt,cmap=plt.get_cmap('hsv'))
# plt.savefig(outdirectory+'_polang_wgt.pdf')
# hp.mollview(polang_map_p353,cmap=plt.get_cmap('hsv'))
# plt.savefig(outdirectory+'_polang_p353.pdf')
# hp.mollview(polang_map_p217,cmap=plt.get_cmap('hsv'))
# plt.savefig(outdirectory+'_polang_p217.pdf')
# hp.mollview(polang_map_p143,cmap=plt.get_cmap('hsv'))
# plt.savefig(outdirectory+'_polang_p143.pdf')
# plt.clf()

# diff_353_217 = dodiff(polang_map_p353,polang_map_p217)
# diff_353_143 = dodiff(polang_map_p353,polang_map_p143)
# diff_217_143 = dodiff(polang_map_p217,polang_map_p143)

# diff_353_217_val = np.median(diff_353_217)
# diff_353_143_val = np.median(diff_353_143)
# diff_217_143_val = np.median(diff_217_143)
# print(diff_353_217_val)
# print(diff_353_143_val)
# print(diff_217_143_val)

# hp.mollview(diff_353_217,cmap=plt.get_cmap('hsv'))
# plt.savefig(outdirectory+'_polang_p353_p217.pdf')
# hp.mollview(diff_353_143,cmap=plt.get_cmap('hsv'))
# plt.savefig(outdirectory+'_polang_p353_p143.pdf')
# hp.mollview(diff_217_143,cmap=plt.get_cmap('hsv'))
# plt.savefig(outdirectory+'_polang_p217_p143.pdf')

# threshold = 30
# hp.mollview(diff_353_217,cmap=plt.get_cmap('hsv'),min=-threshold,max=threshold)
# plt.savefig(outdirectory+'_polang_p353_p217_cut.pdf')
# hp.mollview(diff_353_143,cmap=plt.get_cmap('hsv'),min=-threshold,max=threshold)
# plt.savefig(outdirectory+'_polang_p353_p143_cut.pdf')
# hp.mollview(diff_217_143,cmap=plt.get_cmap('hsv'),min=-threshold,max=threshold)
# plt.savefig(outdirectory+'_polang_p217_p143_cut.pdf')

# hp.gnomview(diff_353_143,reso=20,min=-15,max=15)
# plt.savefig(outdirectory+'_polang_p353_p143_zoom.pdf')

p217 *= val_353/val_217
p143 *= val_353/val_143

hp.mollview(np.sqrt(((p353[1]-p217[1])**2)+((p353[2]-p217[2])**2)),min=0,max=0.1)
plt.savefig(outdirectory+'_diff_p353_p217.pdf')
plt.clf()
hp.mollview(np.sqrt(((p353[1]-p143[1])**2)+((p353[2]-p143[2])**2)),min=0,max=0.1)
plt.savefig(outdirectory+'_diff_p353_p143.pdf')
plt.clf()
hp.mollview(np.sqrt(((p217[1]-p143[1])**2)+((p217[2]-p143[2])**2)),min=0,max=0.1)
plt.savefig(outdirectory+'_diff_p217_p143.pdf')
plt.clf()

hp.write_map(outdirectory+'_diff_p353_p217.fits',np.sqrt(((p353[1]-p217[1])**2)+((p353[2]-p217[2])**2)),overwrite=True)
hp.write_map(outdirectory+'_diff_p353_p143.fits',np.sqrt(((p353[1]-p143[1])**2)+((p353[2]-p143[2])**2)),overwrite=True)
hp.write_map(outdirectory+'_diff_p217_p143.fits',np.sqrt(((p217[1]-p143[1])**2)+((p217[2]-p143[2])**2)),overwrite=True)

Pp353 = np.sqrt(p353[1]**2+p353[2]**2)
Pp217 = np.sqrt(p217[1]**2+p217[2]**2)
Pp143 = np.sqrt(p143[1]**2+p143[2]**2)
hp.mollview(Pp353-Pp217,min=-0.1,max=0.1)
plt.savefig(outdirectory+'_diff2_p353_p217.pdf')
plt.clf()
hp.mollview(Pp353-Pp143,min=-0.1,max=0.1)
plt.savefig(outdirectory+'_diff2_p353_p143.pdf')
plt.clf()
hp.mollview(Pp217-Pp143,min=-0.1,max=0.1)
plt.savefig(outdirectory+'_diff2_p217_p143.pdf')
plt.clf()

hp.write_map(outdirectory+'_diff2_p353_p217.fits',Pp353-Pp217,overwrite=True)
hp.write_map(outdirectory+'_diff2_p353_p143.fits',Pp353-Pp143,overwrite=True)
hp.write_map(outdirectory+'_diff2_p217_p143.fits',Pp217-Pp143,overwrite=True)


"""
Collect metrics with low memory requirements (avoid collecting superfluous data)
"""
from typing import Any
from ray.util.iter import LocalIterator
from ray.rllib.evaluation.metrics import collect_episodes, summarize_episodes
from ray.rllib.execution.common import STEPS_SAMPLED_COUNTER
from ray.rllib.evaluation.worker_set import WorkerSet
from ray.rllib.execution.metric_ops import OncePerTimeInterval
from ray.tune.integration.wandb import wandb_mixin

def LowMemoryMetricsReporting(train_op: LocalIterator[Any], workers: WorkerSet,
                             config: dict) -> LocalIterator[dict]:
    output_op = train_op \
        .filter(OncePerTimeInterval(max(2, config["min_iter_time_s"]))) \
        .for_each(CollectMetricsLowMem(
            workers, min_history=config["metrics_smoothing_episodes"],
            timeout_seconds=config["collect_metrics_timeout"]))
    return output_op

class CollectMetricsLowMem:
    def __init__(self, workers, min_history=100, timeout_seconds=180, log_to_neptune=False):
        self.workers = workers
        self.episode_history = []
        self.to_be_collected = []
        self.min_history = min_history
        self.timeout_seconds = timeout_seconds
        self.log_to_neptune = log_to_neptune

    def __call__(self, _):
        # Collect worker metrics.
        episodes, self.to_be_collected = collect_episodes(
            self.workers.local_worker(),
            self.workers.remote_workers(),
            self.to_be_collected,
            timeout_seconds=self.timeout_seconds)
        orig_episodes = list(episodes)
        missing = self.min_history - len(episodes)
        if missing > 0:
            episodes.extend(self.episode_history[-missing:])
            assert len(episodes) <= self.min_history
        self.episode_history.extend(orig_episodes)
        self.episode_history = self.episode_history[-self.min_history:]
        res = summarize_episodes(episodes, orig_episodes)

        # Add in iterator metrics.
        metrics = LocalIterator.get_metrics()
        timers = {}
        counters = {}
        info = {}
        info.update(metrics.info)
        for k, counter in metrics.counters.items():
            counters[k] = counter
        for k, timer in metrics.timers.items():
            timers["{}_time_ms".format(k)] = round(timer.mean * 1000, 3)
            if timer.has_units_processed():
                timers["{}_throughput".format(k)] = round(
                    timer.mean_throughput, 3)
        res.update({
            "num_healthy_workers": len(self.workers.remote_workers()),
            "timesteps_total": metrics.counters[STEPS_SAMPLED_COUNTER],
        })
        res["timers"] = timers
        res["info"] = info
        res["info"].update(counters)
        relevant = ["info", "custom_metrics", "sampler_perf", "timesteps_total", "policy_reward_mean",
                    "episode_len_mean"]

        d = {
            k: res[k] for k in relevant
        }
        d["evaluation"] = res.get("evaluation", {})

        if self.log_to_neptune:
            metrics_to_be_logged = ["info", "evaluation"]
            def log_metric(metrics, base_string=''):
                    if isinstance(metrics, dict):
                        for k in metrics:
                            log_metric(metrics[k], base_string + '{}_'.format(k))
                    else:
                        neptune.log_metric(base_string, metrics)
            for k in d:
                if k in metrics_to_be_logged:
                    log_metric(d[k], base_string='{}_'.format(k))

        return d


from scale_client.sensors.virtual_sensor import VirtualSensor

import time
import copy
import logging
log = logging.getLogger(__name__)

class FakeLocationSensor(VirtualSensor):
	def __init__(self, broker, exp=600, lat=0.0, lon=0.0, alt=None, event_type="fake_location", **kwargs):
		super(FakeLocationSensor, self).__init__(broker, event_type=event_type, **kwargs)
		self._exp = exp
		self._tag = {}
		if type(lat) != type(0.0) or type(lon) != type(0.0):
			raise TypeError
		self._tag["lat"] = lat
		self._tag["lon"] = lon
		if alt is not None:
			if type(alt) != type(0.0):
				raise TypeError
			self._tag["alt"] = alt

	DEFAULT_PRIORITY = 3

	def read_raw(self):
		raw = copy.copy(self._tag)
		raw["exp"] = time.time() + self._exp
		return raw

	def policy_check(self, data):
		return True


from flask import Flask, request, render_template, redirect, url_for, session
from flask_cors import CORS
from src.dashboard.infrastructure.controller import DashboardController
import requests, uuid

#from app import app
from __main__ import app

CORS(app)

@app.route('/tipoevento_home', methods=['GET'])
def tipoevento_home():
    _dashCL = DashboardController()
    cod_cuenta = session["datauser"]["CodCuenta"]
    cod_cliente = session["cod_admin"]
    data = _dashCL.listTablaEventos(cod_cuenta, cod_cliente)
    return data

@app.route('/cards_clientes', methods=['GET'])
def cards_clientes():
    _dashCL = DashboardController()
    cod_cuenta = session["datauser"]["CodCuenta"]
    cod_cliente = session["cod_admin"]
    data = _dashCL.cardsClientes(cod_cuenta, cod_cliente)
    return data

@app.route('/cards_cuentas', methods=['GET'])
def cards_cuentas():
    _dashCL = DashboardController()
    cod_cuenta = session["datauser"]["CodCuenta"]
    cod_cliente = session["datauser"]["CodCliente"]
    data = _dashCL.cardsCuentas(cod_cuenta, cod_cliente)
    return data

@app.route('/graficos_home_cuentas', methods=['GET'])
def graficos_home_cuentas():
    _dashCL = DashboardController()
    cod_cuenta = session["datauser"]["CodCuenta"]
    cod_cliente = session["datauser"]["CodCliente"]
    data = _dashCL.graficosCuentas(cod_cuenta, cod_cliente)
    print(data)
    return data


"""empty message

Revision ID: 78f06413e634
Revises: c06b522e6f62
Create Date: 2020-08-20 19:30:42.142551

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '78f06413e634'
down_revision = 'c06b522e6f62'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('author',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('author_name', sa.String(length=255), nullable=True),
    sa.Column('article_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['article_id'], ['article.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.add_column('article', sa.Column('parsed', sa.Boolean(), nullable=True))
    op.add_column('article', sa.Column('published', sa.Boolean(), nullable=True))
    op.add_column('article', sa.Column('upload_date', sa.TIMESTAMP(timezone=True), nullable=True))
    op.add_column('file', sa.Column('digital_object_id', sa.String(length=255), nullable=True))
    op.add_column('file', sa.Column('download_url', sa.String(length=255), nullable=True))
    op.create_unique_constraint(None, 'file', ['download_url'])
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'file', type_='unique')
    op.drop_column('file', 'download_url')
    op.drop_column('file', 'digital_object_id')
    op.drop_column('article', 'upload_date')
    op.drop_column('article', 'published')
    op.drop_column('article', 'parsed')
    op.drop_table('author')
    # ### end Alembic commands ###


import matplotlib.pyplot as plt

crimes_list = []
with open("violent_crimes.txt", 'r') as f:
    for l in f:
        crimes_list.append(int(l))

dates = range(2000, 2020)

plt.plot(dates, crimes_list, 'o:r')
plt.title("Violent Crimes Throughout the 21st Century")
plt.xlabel("Time (Years)")
plt.ylabel("Crimes (Millions)")
plt.show()


import logging
import time
import random
import subprocess
from threading import Timer
from rich import print
from gtts import gTTS
import os
import re
from pydub import AudioSegment, playback
from datetime import datetime

FNULL = open(os.devnull, 'w')
_subprocess_call = playback.subprocess.call
playback.subprocess.call = lambda cmd: _subprocess_call(
    cmd, stdout=FNULL, stderr=subprocess.STDOUT)

class VoiceUtility():

    def __init__(self, silent) -> None:
        self.silent = silent

    def remove_ansi_escape_seq(text):
        if text:
            text = re.sub(r'''(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]''', '', text)
        return text

    speak_stack = []

    def _espeak(self, msg):
        try:
            tts = gTTS(text=msg, lang='en')
            filename = "{}-veronica.temp.mp3".format(
                datetime.now().timestamp())
            tts.save(filename)
            playback.play(AudioSegment.from_mp3(filename))
            os.remove(filename)
        except Exception as e:
            logging.error(str(e))
            cmd = ["espeak", '-s130', '-ven+f5', msg]
            subprocess.run(cmd)

    def speak(self, response, responsetts=None, interval=0):
        if responsetts is not None:
            response = responsetts
        # Special thanks to @Darkonaut
        # https://stackoverflow.com/questions/52786288/python-running-code-simultaneously-tts-and-print-functions

        Timer(
            interval=interval,
            function=self._espeak,
            args=(
                response,
            )).start()

    def print(self, msg, speakMsg=False, speak=True):
        time.sleep(random.uniform(0.5, 2))
        print(msg)
        if(speakMsg and not self.silent):
            self.speak(speakMsg)
        elif(speak and not self.silent):
            self.speak(msg)


"""
    ref_product_type models tests
"""
import os
import sys
import unittest
#import mock
import sqlite3

sys.path.insert(0, os.path.abspath(os.path.dirname(
    os.path.realpath(__file__)) + '/../../../../../../lib/'))
sys.path.insert(0, os.path.abspath(os.path.dirname(
    os.path.realpath(__file__)) + '/../../../../../../conf/'))
sys.path.insert(0, os.path.abspath(os.path.dirname(
    os.path.realpath(__file__)) + '/../../../../../../tests/lib_tests/'))
sys.path.insert(0, os.path.abspath(os.path.dirname(
    os.path.realpath(__file__)) + '/../../../../../../../'))

from inspired_config import SQLALCHEMY_DATABASE_URI
TEST_URI = SQLALCHEMY_DATABASE_URI + '_test'

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.exc import IntegrityError

from database import Base, init_engine, db_session, init_models
from inspired.v1.lib.ref_product_types.models import RefProductType

class TestRefProductTypeModel(unittest.TestCase):
    """ test the ref_product_type model """

    @classmethod
    def setUpClass(cls):
        """Bootstrap test environment by creating the db engine """
        cls.engine = init_engine(TEST_URI)
        cls.connection = cls.engine.connect()
        cls.db_session = db_session
        #cls.db_session = scoped_session(sessionmaker(autocommit=False,
                                         #autoflush=False,
                                         #bind=cls.engine))
        Base.query = cls.db_session.query_property()
        Base.metadata.create_all(cls.engine)

    @classmethod
    def tearDownClass(cls):
        """Delete the test schema and connection """
        Base.metadata.drop_all(cls.engine)
        cls.db_session.close()

    def setUp(self):
        """ use subsessions and do a rollback after each test. """
        self.db_session.begin(subtransactions=True)

    def tearDown(self):
        """ use subsessions and do a rollback after each test. """
        self.db_session.rollback()
        self.db_session.close()
        self.engine.dispose()

    def test_create_ref_product_type(self):
        """ test creating a ref_product_type """
        args = {
            'name': 'abc',
        }
        ref_product_type = RefProductType(**args)
        self.db_session.add(ref_product_type)
        self.assertEqual(self.db_session.commit(), None)

    def test_create_and_delete_ref_product_type(self):
        """ test creating and deleting a ref_product_type """
        args = {
            'name': 'abc',
        }
        ref_product_type = RefProductType(**args)
        self.db_session.add(ref_product_type)
        self.db_session.commit()
        self.db_session.delete(ref_product_type)
        self.assertEqual(self.db_session.commit(), None)

    def test_create_ref_product_type_with_wrong_attribute(self):
        """ test creating a ref_product_type with wrong attribute """
        args = {
            'name': 'abc',
            'asdfas': 'asdfs'
        }
        self.assertRaises(TypeError, lambda: RefProductType(**args))
        self.db_session.commit()

    def test_create_two_ref_product_types_same_name(self):
        """ test creating two ref_product_types with the same name """
        args = {
            'name': 'abc',
        }
        ref_product_type = RefProductType(**args)
        self.db_session.add(ref_product_type)
        self.db_session.commit()
        args = {
            'name': 'abc',
        }
        ref_product_type = RefProductType(**args)
        self.db_session.add(ref_product_type)
        self.assertRaises(IntegrityError, lambda: self.db_session.commit())

    def test_query_all_one_ref_product_type(self):
        """ test querying a ref_product_type """
        args = {
            'name': 'abc',
        }
        ref_product_type = RefProductType(**args)
        self.db_session.add(ref_product_type)
        self.db_session.commit()
        ref_product_types = [ref_product_type]
        self.assertEqual([ref_product_type],
            self.db_session.query(RefProductType).all())

    def test_query_all_two_ref_product_types(self):
        """ test querying two ref_product_types """
        args = {
            'name': 'abc',
        }
        ref_product_type1 = RefProductType(**args)
        args = {
            'name': 'abc1',
        }
        ref_product_type2 = RefProductType(**args)
        self.db_session.add(ref_product_type1)
        self.db_session.add(ref_product_type2)
        self.db_session.commit()
        self.assertEqual([ref_product_type1, ref_product_type2],
            self.db_session.query(RefProductType).all())

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


import sys
import facenet_mv
import time
import cv2

if __name__ == "__main__":
    filename = sys.argv[1]
    facenet = facenet_mv.face_ncs()
    image = cv2.imread(filename)
    tic = time.time()
    processed_image = facenet.process_image(image)
    toc = time.time()
    elapsed = int((toc-tic)*1000)
    print('elapsed_time=', elapsed)
    cv2.imshow('image', processed_image)
    cv2.waitKey()


import cv2
from cv2 import VideoWriter, VideoWriter_fourcc, imread, resize
import os
from PIL import Image

def Video2Pic():
    videoPath = "video.MP4"  # 读取视频路径
    imgPath = "image_for_seg/"  # 保存图片路径

    cap = cv2.VideoCapture(videoPath)
    # fps = cap.get(cv2.CAP_PROP_FPS)  # 获取帧率
    # width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))  # 获取宽度
    # height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))  # 获取高度
    suc = cap.isOpened()  # 是否成功打开
    frame_count = 0
    while suc:
        frame_count += 1
        suc, frame = cap.read()
        cv2.imwrite(imgPath + str(frame_count).zfill(4)+".png", frame)
        cv2.waitKey(1)
    cap.release()
    print("视频转图片结束！")

def Pic2Video():
    imgPath = "test_results/"  # 读取图片路径
    videoPath = "video_seg.MP4"  # 保存视频路径

    images = os.listdir(imgPath)
    images = sorted(images, key= lambda x: int(x[0:4]))
    fps = 30  # 每秒30帧数

    # VideoWriter_fourcc为视频编解码器 ('I', '4', '2', '0') —>(.avi) 、('P', 'I', 'M', 'I')—>(.avi)、('X', 'V', 'I', 'D')—>(.avi)、('T', 'H', 'E', 'O')—>.ogv、('F', 'L', 'V', '1')—>.flv、('m', 'p', '4', 'v')—>.mp4
    fourcc = VideoWriter_fourcc('m', 'p', '4', 'v')

    image = Image.open(imgPath + images[0])
    videoWriter = cv2.VideoWriter(videoPath, fourcc, fps, image.size)
    for im_name in range(len(images)):
        frame = cv2.imread(imgPath + images[im_name])  # 这里的路径只能是英文路径
        # frame = cv2.imdecode(np.fromfile((imgPath + images[im_name]), dtype=np.uint8), 1)  # 此句话的路径可以为中文路径
        videoWriter.write(frame)
    print("图片转视频结束！")
    videoWriter.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    Pic2Video()
    # Video2Pic()


import cv2 as cv #imports computer vision library as cv2

THRESHOLD_LOW = (15, 210, 20);# sets the minimum threshold for yellow in b/w
THRESHOLD_HIGH = (35, 255, 255);# sets the maximum threshold for yellow in b/w

MIN_RADIUS = 10 #creates a int value later to be used in creating a circle

cam = cv.VideoCapture(0)# assigns the first cam(0) to the variable cam
print ("Camera Initialized:")# prints to commanWin when camera initialzes 4 debugin

cam_width = 120 #stores cam_width/height with the interger value corresponding to
cam_height = 720#pixel dim of video input feed from cam(0)

#while cam recieves a feed from camera(0); stores still images in memory, converts
#to binary, then passes binary still through filters checking for pixels within
#our predetermined threshold values (yello in this case), selescts pixels within
#threshold and converts those pixels to white, and all others to black.
#Then dialates white/black img for better processing, then this image is sent
#through cv.contour detection which finds our yello obj. and circles in green
while True:
    ret_val, img = cam.read()#stores cam(0) still image to variable img,
                             #and something about a ret_val...

    img_filter = cv.GaussianBlur(img.copy(), (3, 3), 0)

    img_filter = cv.cvtColor(img_filter, cv.COLOR_BGR2HSV)

    img_binary = cv.inRange(img_filter.copy(), THRESHOLD_LOW, THRESHOLD_HIGH)

    img_binary = cv.dilate(img_binary, None, iterations = 1)

    img_contours = img_binary.copy()

    contours = cv.findContours(img_contours, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)[-2]

    cv.drawContours(img_contours, contours, -1, (0, 255, 0), 5)

    center = None
    radius = 0
    if len(contours) > 0:
        c = max(contours, key=cv.contourArea)
        ((x ,y), radius) = cv.minEnclosingCircle(c)
        M = cv.moments(c)
        if M["m00"] > 0:
            center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
            if radius < MIN_RADIUS:
                center = None

    if center != None:
        cv.circle(img, center, int(round(radius)), (0, 255, 0))

    cv.imshow("webcam", img)
    cv.imshow("binary", img_binary)
    cv.imshow("contours", img_contours)

    if cv.waitKey(1) == 27:
       break

cv.destroyAllWindows()


import json
import os.path
import math
import sys
from os import path
from pathlib import Path

PRINT_PROPS = False
USE_BLENDER = False
try:
	import bpy
	USE_BLENDER = True
except ImportError:
	USE_BLENDER = False

hexes = []
fac_x = 0
fac_y = 0

with open('D:/Planetside2/Builder/indar_zones.txt', 'r') as f:
    zones = json.load(f)

    #for key in props['objects']:
    #    print(key['actorDefinition'])

def in_hexes(x, y):
	for hex in hexes:
		min_x = (float(hex['x']) - 1) * 200
		max_x = (float(hex['x']) + 1) * 200
		min_y = (float(hex['y']) - 1) * 200
		max_y = (float(hex['y']) + 1) * 200
		if (min_x < x and max_x > x
				and min_y < y and max_y > y):
			print(hex, min_x, max_x, min_y, max_y, x, y)
			return True
	return False

def get_collection(name, layer = None):
	if (USE_BLENDER == False):
		return None

	if (layer == None):
		layer = bpy.context.view_layer.layer_collection

	if (layer.name == name):
		return layer

	found = None
	for layer_kids in layer.children:
		found = get_collection(name, layer_kids)
		if found:
			return found

def set_active_collection(name):
	if (USE_BLENDER == False):
		return
	found = get_collection(name)
	if (found == None):
		print('Failed to find', name)
		return

	bpy.context.view_layer.active_layer_collection = found

def in_range(x, y):
	ax = (x - fac_x) ** 2
	ay = (y - fac_y) ** 2
	return ax + ay < 200 ** 2

def get_model_path(actor):
	return 'D:/Planetside2/Models/' + actor_name + '_LOD0/' + actor_name + '_LOD0.obj'

def has_model(actor):
	path = get_model_path(actor_name)
	return os.path.exists(path)

def deg(radian):
	return radian * 180 / math.pi

def rad(deg):
	return deg * math.pi / 180

#base = input('Base name: ')
base = 'Regent Rock Garrison'
print('Looking for: %s' % (base))

found = False

for types in zones['zone_list'][0]['regions']:
	for zone in zones['zone_list'][0]['regions'][types]:
		#print('Checking %s' % zone['facility_name'])
		if (zone['facility_name'] == base):
			found = True
			fac_x = float(zone['location_x'])
			fac_y = float(zone['location_z'])
			print('Found base, at (%s, %s) loading hexes' % (fac_x, fac_y))
			for hex in zone['hex']:
				print(hex)
				hexes.append(hex)
			break

if (found == False):
	print("Failed to find", base)
	sys.exit()

with open('D:/Planetside2/Builder/indar_old_props.json', 'r') as f:
    props = json.load(f)

count = 0
meshes = dict()
missing_actors = set()
base_collection = None
base_layer_collection = None

# Create the base collection where all sub-collections will go
if (USE_BLENDER == True):
	base_collection = bpy.data.collections.new(base)
	bpy.context.scene.collection.children.link(base_collection)
	set_active_collection(base_collection.name)

for prop in props['objects']:
	# Has no mesh
	if "Occluder" in prop['actorDefinition']:
		continue
	if "AntiGravity" in prop['actorDefinition']:
		continue

	for inst in prop['instances']:
		inst_x = float(inst['position'][0])
		inst_y = float(inst['position'][2])

		#if (in_hexes(inst_x, inst_y)):
		if (in_range(inst_x, inst_y)):
			count += 1
			actor_name = str(Path(prop['actorDefinition']).with_suffix(''))
			model_name = get_model_path(actor_name)
			model_exists = has_model(actor_name)
			if (PRINT_PROPS == True):
				print('Found %s/%s\n\tLOC: (%f, %f, %f)\n\tROT: (%f, %f, %f)\n\tSCL: (%f, %f, %f)'
					% (actor_name, prop['actorDefinition'],
					inst_x, inst['position'][1], inst_y,
					deg(inst['rotation'][1]) + 90, deg(inst['rotation'][2]), deg(inst['rotation'][0]),
					inst['scale'][0],inst['scale'][1],inst['scale'][2]))
				print('\tMDL: ' + str(has_model(actor_name)))

			if (model_exists == True):
				if (USE_BLENDER == True):

					# Place each prop in a separate collection to avoid lists of hundreds of props
					actor_coll = base_collection.children.get(actor_name)
					if (actor_coll == None):
						actor_coll = bpy.data.collections.new(actor_name)
						base_collection.children.link(actor_coll)
					set_active_collection(actor_coll.name)

					# Copy a mesh instead of importing it if possible, lot faster
					if actor_name in meshes:
						#print('Found %s under %s' % (actor_name, meshes[actor_name]))
						og_obj = bpy.data.objects[meshes[actor_name]]
						obj = og_obj.copy()
						obj.data = og_obj.data.copy()
						bpy.context.collection.objects.link(obj)
					else:
						bpy.ops.import_scene.obj(filepath=model_name)
						obj = bpy.context.selected_objects[0]
						meshes[actor_name] = obj.name

					obj.scale = [
						math.fmod(inst['scale'][0], math.pi),
						math.fmod(inst['scale'][1], math.pi),
						math.fmod(inst['scale'][2], math.pi)
					]
					obj.rotation_mode = 'XYZ'
					obj.rotation_euler = [inst['rotation'][1] + rad(90), inst['rotation'][2], inst['rotation'][0]]
					obj["ps2_id"] = str(inst["id"])

					loc = [inst_x, -inst_y, inst['position'][1]]
					obj.location = loc
			else:
				missing_actors.add(model_name)

print("Missing models: ", missing_actors)
print(count)
print("Copy this to run in Blender:")
print("exec(compile(open('D:/Planetside2/Builder/run.py').read(), '', 'exec'))")


'''
Created on Jun 4, 2018

@author: zli
'''
import sys, os, json, argparse, logging, shutil
import panicle_detection
from datetime import date
from globus_sdk import NativeAppAuthClient, TransferClient, TransferData, RefreshTokenAuthorizer

CLIENT_ID = '53669bb5-f588-4c3a-8556-e416ed228a36'

class Transfer:
    def __init__(self, src_endpoint_name, dst_endpoint_name, log_lv=logging.INFO):
        log_format = '%(asctime)-15s %(levelname)s:\t  class:%(name)s %(message)s'
        logging.basicConfig(format=log_format)
        self.logger = logging.getLogger(self.__class__.__name__)
        self.logger.setLevel(log_lv)
        self.logger.debug('CLIENT_ID: {0}'.format(CLIENT_ID))
        self.client = NativeAppAuthClient(CLIENT_ID)
        self.client.oauth2_start_flow(refresh_tokens=True)

        authorize_url = self.client.oauth2_get_authorize_url()
        print('Please go to this URL and login: {0}'.format(authorize_url))

        get_input = getattr(__builtins__, 'raw_input', input)
        auth_code = get_input(
            'Please enter the code you get after login here: ').strip()
        token_response = self.client.oauth2_exchange_code_for_tokens(auth_code)
        self.globus_auth_data = token_response.by_resource_server['auth.globus.org']
        self.globus_transfer_data = token_response.by_resource_server['transfer.api.globus.org']
        auth_token = self.globus_auth_data['access_token']
        transfer_token = self.globus_transfer_data['access_token']
        transfer_rt = self.globus_transfer_data['refresh_token']
        transfer_at = self.globus_transfer_data['access_token']
        expires_at_s = self.globus_transfer_data['expires_at_seconds']
        self.authorizer = RefreshTokenAuthorizer(transfer_rt, self.client,
                                                 access_token=transfer_at,
                                                 expires_at=expires_at_s)
        self.transferClient = TransferClient(authorizer=self.authorizer)
        self.src_endpoint = None
        self.dst_endpoint = None
        for ep in self.transferClient.endpoint_search(filter_scope="shared-with-me"):
            if ep["display_name"] == src_endpoint_name:
                self.src_endpoint = ep
                self.logger.info('Source endpoint: [{0}] {1}'
                                 .format(self.src_endpoint['id'], self.src_endpoint['display_name']))
        if self.src_endpoint is None:
            self.logger.error('No endpoint shared with you with name: {0}'.format(src_endpoint_name))
            raise LookupError
        for ep in self.transferClient.endpoint_search(filter_scope="my-endpoints"):
            if ep['display_name'] == dst_endpoint_name:
                self.dst_endpoint = ep
                self.logger.info('Destination endpoint: [{0}] {1}'
                                 .format(self.dst_endpoint['id'], self.dst_endpoint['display_name']))
        if self.dst_endpoint is None:
            self.logger.error('You don\'t have endpoint named: {0}'.format(dst_endpoint_name))
            raise LookupError

    def transfer_dir(self, src_dir, dst_dir):
        transfer_data = TransferData(self.transferClient, self.src_endpoint['id'], self.dst_endpoint['id'])
        transfer_data.add_item(src_dir, dst_dir, recursive=True)
        result = self.transferClient.submit_transfer(transfer_data)
        self.logger.info('task [{0}] {1}'.format(result['task_id'], result['code']))
        return result

    def transfer_file(self, src_file, dst_file):
        transfer_data = TransferData(self.transferClient, self.src_endpoint['id'], self.dst_endpoint['id'])
        transfer_data.add_item(src_file, dst_file)
        result = self.transferClient.submit_transfer(transfer_data)
        self.logger.info('task_id [{0}] {1}'.format(result['task_id'], result['code']))
        return result

    def ls_src_dir(self, path, ls_filter=''):
        # using iteration to get every entry from result
        # an entry contain two keys: 'name' and 'type'
        # type define the entry is a file or folder
        result = self.transferClient.operation_ls(self.src_endpoint['id'], path=path, filter=ls_filter)
        for entry in result:
            self.logger.debug('name: {0}\ttype: {1}'.format(entry["name"], entry["type"]))
        return result

    def task_list(self, num_results=10):
        result = self.transferClient.task_list(num_results=num_results)
        for task in result:
            self.logger.debug('task_id: [{0}]\t status: {1}'.format(task['task_id'], task['status']))
        return result

def options():

    parser = argparse.ArgumentParser(description='local workflow for Panicle detection from laser data',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument("-m", "--model_path", help="model path")
    parser.add_argument("-p", "--ply_dir", help="ply directory")
    parser.add_argument("-j", "--json_dir", help="json directory")
    parser.add_argument("-o", "--out_dir", help="output directory")
    parser.add_argument("-y", "--year", help="specify which year to process")
    parser.add_argument("-d", "--month", help="specify month")
    parser.add_argument("-s", "--start_date", help="specify start date")
    parser.add_argument("-e", "--end_date", help="specify end date")

    args = parser.parse_args()

    return args

def main():

    print("start...")

    args = options()

    print("create transfer connection")
    t = Transfer('Terraref', 'Zongyang Linux desktop', log_lv=logging.DEBUG)
    tc = t.transferClient

    for day in range(int(args.start_date), int(args.end_date)+1):
        target_date = date(int(args.year), int(args.month), day)
        str_date = target_date.isoformat()
        print(str_date)

        # globus data transfer
        globus_raw_dir = os.path.join('/ua-mac/raw_data/scanner3DTop/', str_date)
        local_raw_dir = os.path.join('/media/zli/data/Terra/ua-mac/raw_data/scanner3DTop/', str_date)

        if not os.path.isdir(local_raw_dir):
            print('start %s raw data transfer'%(str_date))
            task_info = t.transfer_dir(globus_raw_dir, local_raw_dir)
            while not tc.task_wait(task_info['task_id'], timeout=60*5):
                print("Another 5 minute went by without {0} terminating".format(task_info['task_id']))

            print('finish raw data transfer')

        globus_ply_dir = os.path.join('/ua-mac/Level_1/scanner3DTop/', str_date)
        local_ply_dir = os.path.join('/media/zli/data/Terra/ua-mac/Level_1/scanner3DTop/', str_date)

        if not os.path.isdir(local_raw_dir):
            print('start %s ply data transfer'%(str_date))
            task_info = t.transfer_dir(globus_ply_dir, local_ply_dir)
            while not tc.task_wait(task_info['task_id'], timeout=60*5):
                print("Another 5 minute went by without {0} terminating".format(task_info['task_id']))

            print('finish ply data transfer')

        # start processing
        print('start full day processing...')
        out_dir = os.path.join(args.out_dir, str_date)
        model_file_path = args.model_path
        try:
            panicle_detection.full_day_gen_hist(local_ply_dir, local_raw_dir, out_dir, model_file_path)

            if os.path.isdir(out_dir):
                    panicle_detection.full_day_output_integrate(out_dir, out_dir)

        except Exception as ex:
            panicle_detection.fail(str_date +str(ex))
        print('finish processing')

        # delete globus files
        try:
            shutil.rmtree(local_raw_dir)
            shutil.rmtree(local_ply_dir)
        except Exception as ex:
            panicle_detection.fail(str_date +str(ex))

    return

if __name__ == "__main__":

    main()


import decimal
import math
from dateutil.relativedelta import *
from datetime import date

def calc_age(birth_date):
    if birth_date and birth_date < date.today():
        age = relativedelta(date.today(), birth_date)
        age = {"lata:": age.years,
               'miesiące:': age.months,
               'dni:': age.days}
        return age

def calc_glucose(glucose_val, glucose_unit):
    if glucose_val:
        glucose_calculated = (glucose_val, 'mmol/l', glucose_val * 18, 'mg/dl') \
            if glucose_unit == 'mmol/l' else \
            (glucose_val, 'mg/dl', round(glucose_val / 18, 2), 'mmol/l')
    else:
        glucose_calculated = None
    return glucose_calculated

def calc_bmi(bmi, weight, height):
    if bmi:
        if weight and height:
            calc_bmi = round(weight / ((height / 100) * (height / 100)), 1)
            if calc_bmi < 4 or calc_bmi > 110:
                calculated_bmi = f'{calc_bmi}  kg/m2  - wynik mało prawdopodobny, sprawdź poprawność wprowadzonych danych'
            else:
                calculated_bmi = f'{calc_bmi}  kg/m2'
        else:
            calculated_bmi = 'podaj wagę i wzrost '
        return calculated_bmi

def calc_heart_rate(rr, speed):
    if rr:
        return round(1500 / rr) if speed == '25' else round(3000 / rr)

def calc_qtc(rr, qt, speed):
    if rr:
        if qt:
            rr_s = round(rr * decimal.Decimal(0.04), 3) if speed == '25' else round(rr * decimal.Decimal(0.02), 3)
            qt_s = round(qt * decimal.Decimal(0.04), 3) if speed == '25' else round(qt * decimal.Decimal(0.02), 3)
            qtc_bazzet = round(qt_s / decimal.Decimal(math.sqrt(rr_s)), 3)
            qtc_frederic = round(qt_s / decimal.Decimal(math.pow(rr_s, 1 / 3)), 3)
            return [qtc_bazzet, qtc_frederic]


import torch
from collections import OrderedDict

class Mask:
    def __init__(self, model_state_dict):
        super().__init__()
        self.state_dict_ = self.init_mask(model_state_dict)

    def init_mask(self, model_state_dict):
        mask = OrderedDict()
        for name, param in model_state_dict.items():
            parameter_type = name.split('.')[-1]
            if 'weight' in parameter_type and param.dim() > 1:
                mask[name] = param.new_ones(param.size(), dtype=torch.bool)
        return mask

    def freeze_grad(self, model):
        for name, param in model.named_parameters():
            parameter_type = name.split('.')[-1]
            if 'weight' in parameter_type and param.dim() > 1:
                mask_i = self.state_dict()[name]
                param.grad.data = torch.where(mask_i.to(param.device), param.grad.data,
                                              torch.tensor(0, dtype=torch.float, device=param.device))
        return

    def load_state_dict(self, state_dict):
        self.state_dict_ = state_dict
        return

    def state_dict(self):
        return self.state_dict_


import xml.etree.ElementTree as ET
from urllib.parse import (
    urlparse,
    parse_qs,
)

import requests
from django.conf import settings
from telegram import Bot

from cards.utils import download_image
from ghost.ghost_admin_request import (
    get_post,
    update_post,
    upload_image,
)

TASK_RETRY_COUNTDOWN = 5 * 60

def _exit_on_failed_response(response):
    try:
        response.raise_for_status()
    except requests.HTTPError:
        exit()

def get_video_snippet(video_id: str):
    url = 'https://www.googleapis.com/youtube/v3/videos'
    response = requests.get(url, params={
        'part': 'snippet',
        'id': video_id,
        'key': settings.YOUTUBE_API_KEY,
    })

    _exit_on_failed_response(response)

    return response.json()['items'][0]['snippet']

def get_last_youtube_video():
    response = requests.get(f'https://www.youtube.com/feeds/videos.xml?channel_id={settings.YOUTUBE_CHANNEL_ID}')

    _exit_on_failed_response(response)

    tree = ET.fromstring(response.text)
    video = tree.find('{http://www.w3.org/2005/Atom}entry')
    video_id = video.find('{http://www.youtube.com/xml/schemas/2015}videoId').text
    return video_id, get_video_snippet(video_id)

def update_video_banner(post, video_id, snippet) -> None:
    cover = download_image(snippet['thumbnails']['medium']['url'])

    response = upload_image(
        image_name=f'{post["id"]}_youtube_banner.jpeg',
        image=cover,
    )

    _exit_on_failed_response(response)

    response = update_post(
        post_id=post['id'],
        post_updated_at=post['updated_at'],
        data={
            'status': 'published',
            'feature_image': response.json()['images'][0]['url'],
            'feature_image_caption': f'https://www.youtube.com/watch?v={video_id}',
            'title': snippet['title'],
        },
    )

    _exit_on_failed_response(response)

def notify(text):
    Bot(token=settings.TELEGRAM_TOKEN).send_message(
        settings.TELEGRAM_ADMIN_ID,
        text=text,
    )

def check_for_new_video():
    post: dict = get_post(post_id=settings.YOUTUBE_BANNER_POST_ID)
    try:
        current_video_id = parse_qs(urlparse(post['feature_image_caption']).query)['v'][0]
    except (KeyError, IndexError, ValueError):
        current_video_id = ''

    video_id, snippet = get_last_youtube_video()
    is_published = post['status'] == 'published'
    is_live = snippet['liveBroadcastContent'] in ('live', 'upcoming')
    is_same_id = current_video_id == video_id

    if is_live and is_published and is_same_id:
        return

    if not is_live and is_published:
        update_post(
            post_id=post['id'],
            post_updated_at=post['updated_at'],
            data={
                'status': 'draft',
            },
        )

        notify(f'Unpublished\n{snippet["title"]}\nhttps://youtube.com/watch?v={video_id}')
        return

    if is_live and not is_published:
        update_video_banner(
            post=post,
            video_id=video_id,
            snippet=snippet,
        )
        notify(f'Published\n{snippet["title"]}\nhttps://youtube.com/watch?v={video_id}')
        return


#
# Code to render sample images and count annotations in the Gray et al.
# drone image dataset:
#
# https://zenodo.org/record/5004596#.ZChnr3ZBxD8
#
# Annotations are points in a .csv file.
#

#%% Constants and imports

import pandas as pd
import operator
import os
import shutil
from collections import defaultdict

from md_utils import path_utils
from md_visualization import visualization_utils as visutils

base_folder = r'c:\drone-data\07 - gray'
annotation_file = 'turtle_image_metadata.csv'

output_file_annotated = r'g:\temp\gray_turtles_sample_image_annotated.jpg'
output_file_unannotated = r'g:\temp\gray_turtles_sample_image_unannotated.jpg'

# The location columns in the metadata are labeled "top" and "left", which implies boxes, but
# widths and heights are not provided.  Points are definitely to the top-left of the turtles,
# so it looks like maybe there were boxes at some point. These are reasonable approximations.

box_width = 100
box_height = 100

#%% List all images

image_files = path_utils.find_images(base_folder,recursive=True)
image_files_relative = [os.path.relpath(fn,base_folder) for fn in image_files]

print('Enumerated {} images'.format(len(image_files_relative)))

#%% Read the annotation file

df = pd.read_csv(os.path.join(base_folder,annotation_file))

print('Read {} annotations'.format(len(df)))

#%% Map images to valid annotations

image_to_annotations = defaultdict(list)

n_annotations = 0

all_images = set()

# i_row = 0; row = df.iloc[i_row]
for i_row,row in df.iterrows():

    image_file_relative = os.path.join(row['file_location'],row['filename'])
    all_images.add(image_file_relative)

    # These are false positives
    if row['label'] != 'Certain Turtle':
        continue

    n_annotations += 1

    ann = {}
    for s in ['label','ImageHeight','ImageWidth','top','left']:
        ann[s] = row[s]
        ann['file_name'] = image_file_relative

    image_to_annotations[image_file_relative].append(ann)

print('Found {} annotations on {} images (of {})'.format(
    n_annotations,len(image_to_annotations),len(all_images)))

#%% Render annotations for an image that has a decent number of annotations

image_name_to_count = {}
for image_name in image_to_annotations:
    image_name_to_count[image_name] = len(image_to_annotations[image_name])

# Sort in descending order by value
images_sorted_by_count = dict(sorted(image_name_to_count.items(),
                                 key=operator.itemgetter(1),reverse=True))

images_sorted_by_count = list(images_sorted_by_count)
image_name = images_sorted_by_count[0]

image_full_path = os.path.join(base_folder,image_name)
assert os.path.isfile(image_full_path)

image_annotations = image_to_annotations[image_name]

print('Found {} annotations for image {}'.format(
    len(image_annotations),image_full_path))

#%% Render boxes

detection_formatted_boxes = []

pil_im = visutils.open_image(image_full_path)
image_w = pil_im.size[0]
image_h = pil_im.size[1]

# ann = image_annotations[0]
for ann in image_annotations:

    assert ann['ImageHeight'] == image_h
    assert ann['ImageWidth'] == image_w

    det = {}
    det['conf'] = None
    det['category'] = '0'

    # Convert to relative x/y/w/h
    box = [ann['left']/image_w,
           ann['top']/image_h,
           box_width/image_w,
           box_height/image_h]

    det['bbox'] = box
    detection_formatted_boxes.append(det)

visutils.draw_bounding_boxes_on_file(image_full_path, output_file_annotated, detection_formatted_boxes,
                                     confidence_threshold=0.0,detector_label_map=None)
path_utils.open_file(output_file_annotated)

shutil.copyfile(image_full_path,output_file_unannotated)


from functools import reduce

with open('input', 'r') as f:
    puzzle_input: list[list[int]] = [[int(n) for n in ln if n!='\n'] for ln in f.readlines()]

def part_one(puzzle_input=puzzle_input) -> int:
    ans: int = 0
    ROWS: int = len(puzzle_input)
    COLS: int = len(puzzle_input[0])

    for row in range(ROWS):
        for col in range(COLS):
            curr_tree_hgt: tuple[int] = puzzle_input[row][col]
            # if edge tree, visible
            if (row == 0 or col == 0 or row == ROWS-1 or col == COLS-1):
                ans += 1
            else:
                '''
                    Set of tuple (r, c, Direction) to set
                '''
                tallest_tree_west =  max((puzzle_input[row][:col]))
                tallest_tree_north = max((puzzle_input[r][col] for r in range(0, row)))
                tallest_tree_east =  max((puzzle_input[row][col+1:]))
                tallest_tree_south = max((puzzle_input[r][col] for r in range(row+1, ROWS)))

                if any((curr_tree_hgt > t for t in [tallest_tree_west, tallest_tree_north, tallest_tree_east, tallest_tree_south])):
                    ans += 1
    return ans

def part_two(puzzle_input=puzzle_input) -> int:
    '''
        Bruteforce... ЪциЪци
    '''
    best_scenic_score: int = 0

    ROWS: int = len(puzzle_input)
    COLS: int = len(puzzle_input[0])

    tree_dict: dict[tuple[int, int]] = {(y, x): puzzle_input[y][x] for x in range(COLS) for y in range(ROWS)}

    def find_score_in_direction(v: int, trees: list[tuple[int, int]], tree_dict=tree_dict) -> int:
        scenic_score: int = 0

        for y, x in trees:
            scenic_score += 1
            if tree_dict[(y, x)] >= v:
                break
        return scenic_score

    for r in range(1, ROWS-1):
        for c in range(1, COLS-1):

            north: list[tuple[int, int]] = [(y, c) for y in range(r-1, -1, -1)]
            east: list[tuple[int, int]]  = [(r, x) for x in range(c+1, COLS)]
            south: list[tuple[int, int]] = [(y, c) for y in range(r+1, ROWS)]
            west: list[tuple[int, int]]  = [(r, x) for x in range(c-1, -1, -1)]

            scenic_score: int = reduce(lambda a, b: (a*b), [find_score_in_direction(tree_dict[(r, c)], s) for s in [north, east, south, west]])
            best_scenic_score = max(scenic_score, best_scenic_score)

    return best_scenic_score

print(f'Part One: {part_one()} trees visible.')
print(f'Part two: {part_two()} best scenic score.')


from pyspark.sql import SparkSession

spark = SparkSession \
    .builder \
    .appName("Python Spark SQL basic example") \
    .config("spark.some.config.option", "some-value") \
    .getOrCreate()

df = spark.read.csv('School_Directory.csv')
print(df.take(1))


from discord.ext import commands
import pp_functions
from datetime import datetime as t

class StatsCog(commands.Cog, name="Stats commands"):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name="stats", brief="Shows current pp stats")
    async def stats(self, ctx):
        print(f"[{t.now()}]... [STATS]\t\t\tcommand coming from server [{ctx.guild.name}] id [{ctx.guild.id}]")
        message = await pp_functions.pp_stats(str(ctx.author), str(ctx.guild.id))
        await ctx.send(message)

    @commands.command(name="health", brief="Shows current health value as well as a health bar")
    async def health(self, ctx):
        print(f"[{t.now()}]... [HEALTH]\t\tcommand coming from server [{ctx.guild.name}] id [{ctx.guild.id}]")
        final_message = await pp_functions.pp_health(str(ctx.author), str(ctx.guild.id))
        await ctx.send(final_message)

def setup(bot):
    bot.add_cog(StatsCog(bot))


from app.blueprints import home, user
from flask import Flask
from app.extentions import db

app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///project.db"
db.init_app(app)

app.register_blueprint(home.bp)
app.register_blueprint(user.bp)

with app.app_context():
    db.create_all()


#!/usr/bin/env python
#-*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

n = 8
x = np.arange(1,9)
y = np.random.randint(10,35,n)

plt.bar(x,y,width=0.2,color='blue')
plt.plot(x,y/2, color='green',linewidth=10)
plt.show()


import requests
import json
from PIL import Image
from io import BytesIO
import config

api_key = config.api_key

def get_champion_image(championId):
    '''
    parameter
    championId: string으로 된 id
    ---
    output: png 형식의 챔피언의 이미지
    '''

    championName = get_champion_name(championId)

    api_data = requests.get("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/champion/{0}.png".format(championName))

    img = Image.open(BytesIO(api_data.content))

    return img

def get_champion_name(championId):

    api_data = requests.get("http://ddragon.leagueoflegends.com/cdn/9.12.1/data/ko_KR/champion.json")

    json_data = json.loads(api_data.content.decode("utf-8"))

    champions_info = list(json_data.values())[3]

    # champion Id를 key, champion name을 value로 하는 dictionary 생성

    champion_dict = {}

    for champion, data in champions_info.items():

        champion_dict[data['key']] = data['id']

    # parameter로 전달된 championId에 매치되는 name 찾기

    for champion, champion_name in champion_dict.items():

        if championId == champion:

            championName = champion_name

    return championName
#챔피언 Id를 챔피언 Name으로 바꿔주는 함수 --> 숙련도로 챔피언 뽑아왔을 때 Id로 나오기 때문에 이를 Name으로 바꿔줘야 image를 불러올 수 있다

if '__name__' == '__main__':
    get_champion_image('266')


import torch
from torch import nn
import torchvision
from torch.nn.utils.weight_norm import weight_norm
import torch.nn.functional as F

if torch.cuda.is_available():
    device = torch.device("cuda")
else:
    device = torch.device("cpu")

class AblationAttModule(nn.Module):
    def __init__(self, featureSize, decodeSize, attSize, dropout=0.5):

        super(AblationAttModule, self).__init__()

        # FC with bottom up features
        self.att_feat = weight_norm(nn.Linear(featureSize, attSize))

        # FC with hidden state values (hidden 1)
        self.att_decoder = weight_norm(nn.Linear(decodeSize, attSize))

        self.att = weight_norm(nn.Linear(attSize, 1))

        # overfitting, classification, and activation.
        self.relu = nn.ReLU()
        self.dropout = nn.Dropout(p=dropout)
        self.softmax = nn.Softmax(dim=1)

    def forward(self, BU_feats, h1):

        # FC layer with hidden states
        h1_att = self.att_decoder(h1)

        # FC layer with bottom up features obtained.
        img_att = self.att_feat(BU_feats)

        # Attention module forward step
        att1 = self.dropout(self.relu(h1_att.unsqueeze(1) + img_att))
        att2 = self.att(att1)
        attention = att2.squeeze(2)
        sigmoid = self.softmax(attention)

        # Obtained attended image features (attention weighted image features)
        aw_images = torch.sum((BU_feats * sigmoid.unsqueeze(2)), dim=1)

        return aw_images

class DecoderAblationAttModule(nn.Module):

    def __init__(self, attSize, embedSize, decodeSize, vocabSize, featureSize=2048, dropout=0.5):

        super(DecoderAblationAttModule, self).__init__()

        # Decleration of the needed sizes
        self.attSize = attSize
        self.decodeSize = decodeSize
        self.vocabSize = vocabSize
        self.featureSize = featureSize
        self.embedSize = embedSize
        self.dropout = dropout

        # Attention module to create weighted features
        self.attModule = AblationAttModule(featureSize, decodeSize, attSize)

        self.embedding = nn.Embedding(vocabSize, embedSize)

        # In this model we have used only 1 Layer of LSTM combining images and word embbedings.
        self.LSTM = nn.LSTMCell(embedSize + featureSize, decodeSize, bias=True)

        # Dropout
        self.dropout = nn.Dropout(p=self.dropout)

        #FC with hidden layer to obtain predictions
        self.linear_hidden = weight_norm(nn.Linear(decodeSize, vocabSize))

        #FC with attention weighted features to obtain predictions
        self.linear_image = weight_norm(nn.Linear(featureSize, vocabSize))

        # Initialize weights
        self.init_weights()

    def init_weights(self):

        # Initialization weights uniformly with some small non-zero values.
        self.linear_hidden.bias.data.fill_(0)
        self.linear_hidden.weight.data.uniform_(-0.1, 0.1)
        self.linear_image.bias.data.fill_(0)
        self.linear_image.weight.data.uniform_(-0.1, 0.1)
        self.embedding.weight.data.uniform_(-0.1, 0.1)

    def init_hidden_state(self, batchSize):

        # Initialization of hidden states of LSTM layer
        hidden_states = torch.zeros(batchSize,self.decodeSize).to(device)
        cell_states = torch.zeros(batchSize,self.decodeSize).to(device)

        return hidden_states, cell_states

    def forward(self, feats, sequences, sizes):

        vocabSize = self.vocabSize

        batchSize = feats.size(0)

        # Flatten image
        featsAvg = torch.mean(feats, dim=1).to(device)

        # We sort the data in order to implement the 'timestep' strategy described in the paper (3.2.2)
        sizes, positions = torch.sort((torch.squeeze(sizes, 1)), dim=0, descending=True)

        # We will not encode the end of sentence token <end>.
        decode_lengths = torch.Tensor.tolist((sizes - 1))

        # Sorted parameters
        sequences = sequences[positions]
        feats = feats[positions]
        featsAvg = featsAvg[positions]

        embeddings = self.embedding(sequences)

        # Initialize LSTM state
        hidden, cell = self.init_hidden_state(batchSize)

        # Prediction score initialization
        preds = torch.zeros(batchSize, max(decode_lengths), vocabSize).to(device)

        # Sequence generation step considering the max length in the batch size
        for timestep in range(max(decode_lengths)):
            bSize = sum([seq_length > timestep for seq_length in decode_lengths])
            # 1) (bSize, featureSize)
            aw_images = self.attModule(feats[:bSize],hidden[:bSize])
            # 2) (bsize, decoderSize)
            hidden,cell = self.LSTM(torch.cat([aw_images[:bSize], embeddings[:bSize, timestep, :]], dim=1),(hidden[:bSize], cell[:bSize]))
            # 3) (bSize, vocabSize)
            predictions = self.linear_hidden(self.dropout(hidden)) + self.linear_image(self.dropout(aw_images))

            preds[:bSize, timestep, :] = predictions

        return preds, sequences, decode_lengths, positions


from tkinter import *
import tkinter.font as tkFont
from PIL import ImageTk,Image
from tkinter import filedialog
import os
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from scipy import ndimage, misc
import numpy as np
import os
import cv2
import tkinter as tk
from math import log
class HoverButton(tk.Button):
    def __init__(self, master, **kw):
        tk.Button.__init__(self,master=master,**kw)
        self.defaultBackground = self["background"]
        self.bind("<Enter>", self.on_enter)
        self.bind("<Leave>", self.on_leave)
    def on_enter(self, e):
        self['background'] = self['activebackground']
    def on_leave(self, e):
        self['background'] = self.defaultBackground
def read_img(path):
    img=tf.io.read_file(path)
    img=tf.image.decode_jpeg(img,3)
    img=tf.image.convert_image_dtype(img,tf.float32)
    img=tf.image.resize(img,(224,224))
    img=tf.clip_by_value(img,0.0,1.0)
    return img
def start():
    global model_ss
    status = Label(frame5, text ="Model Initiation", anchor=W, bd=0, bg="black", fg="white")
    status.grid(row=2, sticky="w", padx=350, ipadx=500)
    model_ss=tf.keras.models.load_model(r'C:\MLCourse\CNN\3')
def insert_image():
    status = Label(frame5, text ="Insert Image", anchor=W, bd=0, bg="black", fg="white")
    status.grid(row=2, sticky="w", padx=350, ipadx=500)
    root.filename=filedialog.askopenfilename(initialdir="/", title="Select Image", filetypes=(("jpeg files","*.jpg"),("all files","*.*")))
    global IMAGE
    IMAGE = root.filename
    my_img2=Image.open(IMAGE)
    my_img3 = my_img2.resize((400, 410),Image.ANTIALIAS)
    photo=ImageTk.PhotoImage(my_img3)
    my_img3=Label(frame3, image=photo, width =450, height=450, bg="#F5F5F5", borderwidth=5)
    my_img3.image=photo
    my_img3.grid(row =1, column=2, rowspan=3, sticky="new", ipady=0)
def inspect():
    status = Label(frame5, text ="Inspection Results", anchor=W, bd=0, bg="black", fg="white")
    status.grid(row=2, sticky="w", padx=350, ipadx=500)
    img=read_img(IMAGE)
    img=tf.expand_dims(img,0)
    pred=model_ss(img,training=False)
    if pred > 0.5:
        y =1
        sum =0
        my_label2 = Label(frame4, text = "Gear is damaged", font=helv34, fg="red")
        my_label2.grid(row=1, column=3,  sticky="w")
        sum = sum + (y*log(pred) + (1-y)*log(1-pred))
        c = (-1)*sum
        acc= '%2.2f' % (pred)
        acc2=float(acc) * 100
        mylabel3 = Label(frame4, text = str(acc2)+"%", font=helv34, fg="red")
        mylabel3.grid(row=2, column=3, sticky="w")
        c2= '%2.2f' % c
        c3=float(c2) * 100
        mylabel4 = Label(frame4, text = str(c3)+"%", font=helv34, fg="red")
        mylabel4.grid(row=3, column=3, sticky="w")
        my_label21 = Label(frame4, text = "Helical", font=helv34, fg="red")
        my_label21.grid(row=1, column=5,  sticky="w")
        my_label31 = Label(frame4, text = "Automotive/Transmission", font=helv34, fg="red")
        my_label31.grid(row=2, column=5,  sticky="w")
        my_label51 = Label(frame4, text = "Stainless-Steel", font=helv34, fg="red")
        my_label51.grid(row=3, column=5,  sticky="w")
    else:
        y =0
        sum =0
        my_label2 = Label(frame4, text = "Gear is in healthy condition", font=helv34, fg="red")
        my_label2.grid(row=1, column=3,  sticky="w")
        sum = sum + (y*log(pred) + (1-y)*log(1-pred))
        c = (-1)*sum
        acc= '%2.2f' % (1-pred)
        acc2=float(acc) * 100
        mylabel3 = Label(frame4, text = str(acc2)+"%", font=helv34, fg="red")
        mylabel3.grid(row=2, column=3, sticky="w")
        c2= '%2.2f' % c
        c3=float(c2) * 100
        mylabel4 = Label(frame4, text = str(c3)+"%", font=helv34, fg="red")
        mylabel4.grid(row=3, column=3, sticky="w")
        my_label21 = Label(frame4, text = "Helical", font=helv34, fg="red")
        my_label21.grid(row=1, column=5,  sticky="w")
        my_label31 = Label(frame4, text = "Automotive/Transmission", font=helv34, fg="red")
        my_label31.grid(row=2, column=5,  sticky="w")
        my_label51 = Label(frame4, text = "Stainless-Steel", font=helv34, fg="red")
        my_label51.grid(row=3, column=5,  sticky="w")
def donothing():
    return
root = tk.Tk()
root.geometry('{}x{}'.format(800,600))
root.title("Gears Inspector")
root.iconbitmap(r"C:\MLCourse\CNN\GUI\icon.ico")
root.configure(background='#F5F5F5')
root.resizable(False, False)
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New Inspection", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.destroy)
menubar.add_cascade(label="File", menu=filemenu)
reportmenu = Menu(menubar, tearoff=0)
reportmenu.add_command(label="Create a new report", command=donothing)
reportmenu.add_command(label="Load a report", command=donothing)
reportmenu.add_command(label="Window", command=donothing)
menubar.add_cascade(label="Report", menu=reportmenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Style", command=donothing)
editmenu.add_command(label="Fonts", command=donothing)
editmenu.add_command(label="Window", command=donothing)
menubar.add_cascade(label="Edit", menu=editmenu)
viewmenu = Menu(menubar, tearoff=0)
viewmenu.add_command(label="Zoom", command=donothing)
viewmenu.add_command(label="Zoom fit", command=donothing)
menubar.add_cascade(label="View", menu=viewmenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help index", command=donothing)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)
root.config(menu=menubar)
frame1 = tk.Frame(root, bg="black", width=800, height=200, pady =3)
frame1.grid(row=0, columnspan=3,sticky="ew")
frame12= tk.Frame(root, bg="black", width=800, height=1, pady =3)
frame12.grid(row=0, columnspan=3,sticky="s")
frame2 = tk.Frame(root, bg="#F5F5F5", width=400, height = 400)
frame2.grid(row=1,sticky="nw", ipadx=56, columnspan=3, ipady=96)
frame2.grid_rowconfigure(0, weight=1)
frame2.grid_columnconfigure(1, weight=1)
frame3 = tk.Frame(root, bg="#F5F5F5", width=500, height = 330)
frame3.grid(row=1, column=2, sticky="en")
frame31 = tk.Frame(root, bg="black", width=1, height = 550)
frame31.grid(row=1,column=1, pady=30)
frame4 = tk.Frame(root, bg="#F5F5F5", width=500, height = 150)
frame4.grid(row=2, sticky="ne", ipadx=115, columnspan=3)
frame5= tk.Frame(root, bg="black", width=800, height=20, pady =3)
frame5.grid(row=3, columnspan=3, sticky="s")
frame6= tk.Frame(root, bg="black", width=3, height=800)
frame6.grid(row=0, column=4, rowspan=4, sticky="ne")
frame7= tk.Frame(root, bg="black", width=3, height=800)
frame7.grid(row=0, column=0, rowspan=4, sticky="nw")
my_img = ImageTk.PhotoImage(Image.open(r'C:\MLCourse\CNN\GUI\icon2.png'))
my_img1=Label(frame1, image=my_img, width =50, height=50, bg="black")
helv35 = tkFont.Font(family="Lucida Console", size=30, weight="bold")
mylabel=Label(frame1, text="Gears Inspector", font=helv35, fg="white",pady=2, bg="black")
my_img1.grid(row =0, column=0)
mylabel.grid(row=0, column= 1)
my_img2=Image.open(r'C:\MLCourse\CNN\GUI\start12.png')
my_img3 = my_img2.resize((120, 90),Image.ANTIALIAS)
login_btn1=ImageTk.PhotoImage(my_img3)
my_img4=Image.open(r'C:\MLCourse\CNN\GUI\INSERT12.png')
my_img5 = my_img4.resize((120, 90),Image.ANTIALIAS)
login_btn2=ImageTk.PhotoImage(my_img5)
my_img6=Image.open(r'C:\MLCourse\CNN\GUI\INSPECT12.png')
my_img7 = my_img6.resize((120, 90),Image.ANTIALIAS)
login_btn3=ImageTk.PhotoImage(my_img7)
my_img8=Image.open(r'C:\MLCourse\CNN\GUI\EXIT12.png')
my_img9 = my_img8.resize((120, 90),Image.ANTIALIAS)
login_btn4=ImageTk.PhotoImage(my_img9)
helv36 = tkFont.Font(family="Time", size=10, weight="bold")
mybutton1 = HoverButton(frame2, image=login_btn1, command=start, fg="black", font=helv36, padx=30, pady=10, width = 105, height=50, borderwidth=0, activebackground="#F7EA89")
mybutton2 = Button(frame2, image=login_btn2, width = 105, height=50, padx=30, pady=10, command=insert_image, fg="black", font=helv36, borderwidth=0) #activebackground="#FFA500"
mybutton3 = Button(frame2, image=login_btn3, padx=30, pady=10, command=inspect, fg="black", font=helv36, width = 105, height=50, borderwidth=0)
mybutton4 = Button(frame2, image=login_btn4, padx = 30, pady = 10, command=root.destroy, fg="black", font=helv36,  width = 105, height=50, borderwidth=0)
mybutton1.grid(row=0, column=2, sticky="e", pady=10)
mybutton2.grid(row=2, column=2, sticky="e", pady=10)
mybutton3.grid(row=3, column=2, sticky="e", pady=10)
mybutton4.grid(row=5, column=2, sticky="e", pady=40)
helv32 = tkFont.Font(family="Time", size=15, weight="bold")
label2= Label(frame3, text="Inspected Gear", font=helv32, pady=10, fg="Black", bg="#F5F5F5")
label2.grid(row=0, column=1, columnspan=3, ipadx=200)
helv33 = tkFont.Font(family="Time", size=13, weight="bold", underline=True)
label3= Label(frame4, text="Inspection Details", font=helv33, padx=3, pady=1, fg="black", bg="#F5F5F5")
label3.grid(row=0, column = 4)
helv34 = tkFont.Font(family="Time", size=11, weight="bold")
label4= Label(frame4, text="Gear Status:", font=helv34, pady=5, fg="black", bg="#F5F5F5")
label4.grid(row=1, column=2, ipadx=10, sticky="e")
label6= Label(frame4, text="Model Accuracy:", font=helv34, pady=5, fg="black", bg="#F5F5F5")
label6.grid(row=2, column = 2, ipadx=10, sticky="e")
label8= Label(frame4, text="Model Loss:", font=helv34, pady=5, padx=4, fg="black", bg="#F5F5F5")
label8.grid(row=3, ipadx=10, column=2, sticky="e")
label41= Label(frame4, text="Gear Type:", font=helv34, pady=5, fg="black", bg="#F5F5F5")
label41.grid(row=1, column=4, ipadx=10, sticky="e")
label61= Label(frame4, text="Service Type:", font=helv34, pady=5, fg="black", bg="#F5F5F5")
label61.grid(row=2, column = 4, ipadx=10, sticky="e")
label81= Label(frame4, text="Material:", font=helv34, pady=5, padx=4, fg="black", bg="#F5F5F5")
label81.grid(row=3, ipadx=10, column=4, sticky="e")
label7= Entry(frame4, font=helv34, fg="red", bg="#F5F5F5", borderwidth=0)
label7.grid(row=1, column=3)
label9= Entry(frame4, font=helv34, fg="red", bg="#F5F5F5", borderwidth=0)
label9.grid(row=2, column=3)
label10= Entry(frame4,font=helv34, fg="red", bg="#F5F5F5", borderwidth=0)
label10.grid(row=3, column=3)
label71= Entry(frame4, font=helv34, fg="red", bg="#F5F5F5", borderwidth=0)
label71.grid(row=1, column=5)
label91= Entry(frame4, font=helv34, fg="red", bg="#F5F5F5", borderwidth=0)
label91.grid(row=2, column=5)
label11= Entry(frame4,font=helv34, fg="red", bg="#F5F5F5", borderwidth=0)
label11.grid(row=3, column=5)
root.mainloop()


# -*- coding: utf-8 -*-
import scrapy
import os
import selectorlib
from dateutil import parser as dateparser
from dateutil import relativedelta
import datetime
from scrapy.crawler import CrawlerProcess, CrawlerRunner
from twisted.internet import reactor
from scrapy.exporters import CsvItemExporter
from scrapy.utils.project import get_project_settings

class BBSpider(scrapy.Spider):

	def __init__(self, start_urls, **kwargs):
		self.name = 'bb'
		self.start_urls = start_urls
		self.allowed_domains = ['bestbuy.com/']
		# Create Extractor for product page
		self.product_page_extractor = selectorlib.Extractor.from_yaml_file(os.path.join(os.path.dirname(__file__),'../yaml/best_buy_selectors.yml'))

		self.author_data = set()
		super().__init__(**kwargs)  # python3

	def parse(self, response):
		# Extract data using Extractor
		response_text = self.product_page_extractor.extract(response.text)
		#print(data)
		row = {}
		data_bulk = response_text["bulk_review_block"]
		#print(response_text["num_ratings"])
		if data_bulk:
			for data in data_bulk:
				#print("data reviews: " + str(data.keys()))
				for r in data['reviews']:
					size = len(self.author_data)
					self.author_data.add(r['author'])
					if(len(self.author_data) > size):
						#print("new author")
						row["product"] = response_text["product_title"]
						if 'Verified Purchase' in r['verified']:
							row['verified'] = 1
						else:
							row['verified'] = 0

						row['rating'] = r['rating'].split(' out of')[0]
						row['rating'] = row['rating'].split(' ')[1]

						review_time = datetime.date.today()
						time_num = int(r['date'].split(" ")[0])
						if("month" in r['date']):
							review_time -= relativedelta.relativedelta(months=time_num)
						elif("hour" in r['date']):
							review_time -= relativedelta.relativedelta(hours=time_num)
						elif("day" in r['date']):
							review_time -= relativedelta.relativedelta(days=time_num)
						else: #year
							review_time -= relativedelta.relativedelta(years=time_num)
						row['date'] = review_time.strftime('%d %b %Y')

						if("Helpful") in r:
							row['helpful'] = r['helpful'].split("Helpful (")[0]

						row["author"] = r["author"]
						row["content"] = r["content"]
						row["title"] = r["title"]

						yield row
					else:
						print("duplicate author", len(self.author_data))
		# sleep(5)