# This file is part of eventmq.
#
# eventmq is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# eventmq is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with eventmq.  If not, see <http://www.gnu.org/licenses/>.
"""
:mod:`receiver` -- Receiver
===========================
The receiver is responsible for receiveing messages
"""
import logging

import six
import zmq

from . import conf, constants
from .utils.classes import ZMQReceiveMixin, ZMQSendMixin
from .utils.devices import generate_device_name

logger = logging.getLogger(__name__)

class Receiver(ZMQReceiveMixin, ZMQSendMixin):
    """
    Receives messages and pass them to a on_recv.

    Attributes:
        name (str): Name of this socket
        zcontext (:class:`zmq.Context`): socket context
        zsocket (:class:`zmq.Socket`):
    """
    def __init__(self, *args, **kwargs):
        """
        .. note::
           All args are optional unless otherwise noted.

        Args:
            name (str): name of this socket. By default a uuid will be
                generated
            context (:class:`zmq.Context`): Context to use when buliding the
                socket
            socket (:class:`zmq.Socket`): Should be one of :attr:`zmq.REP` or
                :attr:`zmq.ROUTER`. By default a `ROUTER` is used
        Raises:
            :class:`TypeError`: when `callable` is not callable
        """
        from .utils import settings
        settings.import_settings()

        self.zcontext = kwargs.get('context', zmq.Context.instance())
        self.zcontext.set(zmq.MAX_SOCKETS, conf.MAX_SOCKETS)

        self.name = six.ensure_binary(kwargs.get('name',
                                                 generate_device_name()))

        self.zsocket = kwargs.get('socket', self.zcontext.socket(zmq.ROUTER))
        self.zsocket.setsockopt(zmq.IDENTITY, self.name)
        self.zsocket.setsockopt(zmq.ROUTER_MANDATORY, 1)

        self.status = constants.STATUS.ready

    def listen(self, addr=None):
        """
        start listening on `addr`

        Args:
            addr (str): Address to listen on as a connction string

        Raises:
            :class:`Exception`
        """
        if self.ready:
            self.zsocket.bind(addr)
            self.status = constants.STATUS.listening
            logger.info('Receiver %s: Listening on %s' % (self.name, addr))
        else:
            raise Exception('Receiver %s not ready. status=%s' %
                            (self.name, self.status))

    def connect(self, addr=None):
        """
        Connect to address defined by `addr`

        Args:
            addr (str): Address to connect to as a connection string

        Raises:
            :class:`Exception`
        """
        if self.ready:
            self.zsocket.connect(addr)
            self.status = constants.STATUS.connected
            logger.debug('Receiver %s: Connected to %s' % (self.name, addr))
        else:
            raise Exception('Receiver %s not ready. status=%s' %
                            (self.name, self.status))

    def unbind(self, addr):
        """
        Unbinds current socket

        Args:
            addr (str): Address to disconnect from as a string

        Raises:
           :class:`Exception`
        """

        if self.status == constants.STATUS.listening:
            self.zsocket.unbind(addr)
            self.status = constants.STATUS.ready
        else:
            raise Exception('Receiver %s is %s, but is trying to unbind'
                            % (self.name, self.status))

    @property
    def ready(self):
        """
        Property used to check if this receiver is ready.

        Returns:
            bool: True if the receiver is ready to connect or listen, otherwise
                False
        """
        return self.status == constants.STATUS.ready


import yaml

num_runs_per_experiment = 10

config_file = 'configurations/double_spring_mass/train_compositional_phnode_double_spring_mass.yml'

for i in range(num_runs_per_experiment):
    # Load the relevant configuration dictionary
    with open(config_file, 'r') as f:
        config = yaml.safe_load(f)

    from run_experiment import ex
    ex.run(config_updates=config)

    ex = None
    del ex


import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow import keras
mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
fashion_mnist = tf.keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
plt.figure()
plt.imshow(train_images[1])##this is color
plt.subplot(1,2)
plt.show()

for i in range(4):
    plt.subplot(2, 2, i + 1)
    plt.imshow(train_images[i],cmap=plt.cm.binary)##camp is gald

plt.show()


import numpy as np
import matplotlib.pyplot as plt

#パラメータ
E=np.array([0,0,1])
q=1.0
m=1.0
#初期条件 (x,v,a)=(位置,速度,加速度)
x=np.array([1,0,0])
#斜め上に発射
v=np.array([1,1,0])

#a=np.array([-1,0,0])

#3次元のグラフ（散布図）を描くための設定
fig = plt.figure()
ax = fig.add_subplot(projection='3d')

#初期時間
t=0
#微小時間の時間間隔(小さくするほど精度は上がるが計算時間が比例して増大する)
delta_t=0.01
while(t<10):
    a=+q*E/m
    #速度の微小変化
    v=v+a*delta_t
    #位置の微小変化
    x=x+v*delta_t
    #電場による加速

    #時間の更新
    t=t+delta_t
    #print(x)
    #粒子の位置を記録
    ax.scatter(x[0], x[1], x[2], color='blue')
#グラフを保存する
plt.savefig("houbutusen.png")
#グラフの表示
plt.show()


from setuptools import setup

ds_model_commons_version = "0.3.11.3"

# Parsing "training_requirements.txt" to be included in setup.py
# "training_requirements.txt" is still required to make SageMaker training run properly
with open("training_requirements.txt") as tr:
    training_requirements = [
        x for x in tr.read().splitlines() if not x.startswith("--")
    ]

setup(
    name="labels-model",
    version="0.2.1",
    url="https://git.yolt.io/datascience/labels/labels-model.git",
    description="Labels model",
    setup_requires=["pytest-runner"],
    install_requires=[
        f"datascience_model_commons=={ds_model_commons_version}",
        "protobuf==3.20.*",
    ],
    extras_require={
        "test": ["pytest-ordering==0.6", "pytest==5.3.5"],
        "preprocessing": [
            "pyspark==3.1.1",
        ],
        "training": training_requirements,
        "dev": [
            "black==20.8b1",
            "flake8==4.0.0",
        ],
    },
    classifiers=["Programming Language :: Python :: 3.7"],
)


# -*- coding: utf-8 -*-
import os
import maya.cmds as mc
import json

class HierarchyOpt(object):
    def __init__(self, group=None):
        self.group = group

    def shapes(self):
        all_shape = mc.listRelatives(self.group, ad=1, f=1, type="mesh")
        return all_shape

    def transforms(self):
        shapes = self.shapes()
        transforms = [mc.listRelatives(shape, f=1, parent=1)[0] for shape in shapes]
        transforms = list(set(transforms))
        return transforms

    def get_topology(self):
        topology_dict = dict()
        transforms = self.transforms()
        for transform in transforms:
            vertex_num = mc.polyEvaluate(transform, v=1)
            edge_num = mc.polyEvaluate(transform, e=1)
            face_num = mc.polyEvaluate(transform, f=1)
            topology_dict[transform] = dict(vertex=vertex_num, edge=edge_num, face=face_num)
        return topology_dict

    def write_out(self, json_file):
        json_dir = os.path.dirname(json_file)
        if not os.path.isdir(json_dir):
            os.makedirs(json_dir)
        topology = self.get_topology()
        with open(json_file, "w") as f:
            json.dump(topology, f)

    @staticmethod
    def read(json_file):
        with open(json_file, 'r') as f:
            return json.load(f)

    def compare_hierarchy(self, json_file):
        old_dict = self.read(json_file)
        new_dict = self.get_topology()
        if old_dict == new_dict:
            return [], []
        old_hierarchy_list = old_dict.keys()
        new_hierarchy_list = new_dict.keys()
        new_hierarchy_list = ["|".join(("", self.group, obj.split("%s|" % self.group)[1])) for obj in new_hierarchy_list]
        decrease_list = list(set(new_hierarchy_list)-set(old_hierarchy_list))
        increase_list = list(set(old_hierarchy_list)-set(new_hierarchy_list))
        return increase_list, decrease_list

    def compare_topology(self, json_file):
        changed_list = list()
        old_dict = self.read(json_file)
        new_dict = self.get_topology()
        temp_dict = dict()
        for key in new_dict:
            new_name = "|".join(("", self.group, key.split("%s|" % self.group)[1]))
            temp_dict[new_name] = new_dict[key]
        if old_dict == temp_dict:
            return []
        for key in temp_dict:
            if old_dict[key] != temp_dict[key]:
                changed_list.append(key)
        return changed_list


import time
import math
from abc import *
from threading import Lock
import logging

from tools.symbol_util import split_symbol
from config.cst import TradeOrderSide, OrderStatus, TraderOrderType, SIMULATOR_LAST_PRICES_TO_CHECK, \
    ExchangeConstantsTickersColumns as eC

""" Order class will represent an open order in the specified exchange
In simulation it will also define rules to be filled / canceled
It is also use to store creation & fill values of the order """

class Order:
    __metaclass__ = ABCMeta

    def __init__(self, trader):
        super().__init__()
        self.trader = trader
        self.exchange = self.trader.get_exchange()
        self.is_simulated = self.trader.simulate
        self.side = None
        self.symbol = None
        self.origin_price = 0
        self.origin_stop_price = 0
        self.origin_quantity = 0
        self.market_total_fees = 0
        self.currency_total_fees = 0
        self.filled_quantity = 0
        self.filled_price = 0
        self.currency, self.market = None, None
        self.order_id = None
        self.status = None
        self.order_type = None
        self.creation_time = 0
        self.canceled_time = 0
        self.executed_time = 0
        self.last_prices = None
        self.created_last_price = None
        self.order_profitability = None
        self.linked_to = None
        self.is_from_this_octobot = True
        self.linked_portfolio = None

        self.order_notifier = None

        self.linked_orders = []
        self.lock = Lock()

    # Disposable design pattern
    def __enter__(self):
        self.lock.acquire()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.lock.release()

    # create the order by setting all the required values
    def new(self, order_type, symbol, current_price, quantity,
            price=None,
            stop_price=None,
            status=None,
            order_notifier=None,
            order_id=None,
            quantity_filled=None,
            timestamp=None,
            linked_to=None,
            linked_portfolio=None):

        self.order_id = order_id
        self.origin_price = price
        self.status = status
        self.created_last_price = current_price
        self.origin_quantity = quantity
        self.origin_stop_price = stop_price
        self.symbol = symbol
        self.order_type = order_type
        self.order_notifier = order_notifier
        self.currency, self.market = split_symbol(symbol)
        self.filled_quantity = quantity_filled
        self.linked_to = linked_to
        self.linked_portfolio = linked_portfolio

        if timestamp is None:
            self.creation_time = time.time()
        else:
            # if we have a timestamp, it's a real trader => need to format timestamp if necessary
            self.creation_time = self.exchange.get_uniform_timestamp(timestamp)

        if status is None:
            self.status = OrderStatus.OPEN
        else:
            self.status = status

        if self.trader.simulate:
            self.filled_quantity = quantity

    # update_order_status will define the rules for a simulated order to be filled / canceled
    @abstractmethod
    def update_order_status(self, simulated_time=False):
        raise NotImplementedError("Update_order_status not implemented")

    # check_last_prices is used to collect data to perform the order update_order_status process
    def check_last_prices(self, price, inferior, simulated_time=False):
        if self.last_prices is not None:
            prices = [p["price"]
                      for p in self.last_prices[-SIMULATOR_LAST_PRICES_TO_CHECK:]
                      if not math.isnan(p["price"]) and (p[eC.TIMESTAMP.value] >= self.creation_time or simulated_time)]

            if prices:
                if inferior:
                    if float(min(prices)) < price:
                        logging.getLogger(self.get_name()).info(f"{self.symbol} last prices: {prices}, "
                                                                f"ask for {'inferior' if inferior else 'superior'} "
                                                                f"to {price}")
                        return True
                else:
                    if float(max(prices)) > price:
                        logging.getLogger(self.get_name()).info(f"{self.symbol} last prices: {prices}, "
                                                                f"ask for {'inferior' if inferior else 'superior'} "
                                                                f"to {price}")
                        return True
        return False

    def cancel_order(self):
        self.status = OrderStatus.CANCELED
        self.canceled_time = time.time()

        # if real order
        if not self.is_simulated and not self.trader.check_if_self_managed(self.get_order_type()):
            self.exchange.cancel_order(self.order_id, self.symbol)

        self.trader.notify_order_cancel(self)

    def cancel_from_exchange(self):
        self.status = OrderStatus.CANCELED
        self.canceled_time = time.time()
        self.trader.notify_order_cancel(self)
        self.trader.notify_order_close(self, cancel_linked_only=True)
        self.trader.get_order_manager().remove_order_from_list(self)

    def close_order(self):
        self.trader.notify_order_close(self)

    def add_linked_order(self, order):
        self.linked_orders.append(order)

    def get_linked_orders(self):
        return self.linked_orders

    def get_currency_and_market(self):
        return self.currency, self.market

    def get_side(self):
        return self.side

    def get_id(self):
        return self.order_id

    def get_market_total_fees(self):
        return self.market_total_fees

    def get_currency_total_fees(self):
        return self.currency_total_fees

    def get_filled_quantity(self):
        return self.filled_quantity

    def get_filled_price(self):
        return self.filled_price

    def get_status(self):
        return self.status

    def get_order_type(self):
        return self.order_type

    def get_order_symbol(self):
        return self.symbol

    def get_exchange(self):
        return self.exchange

    def get_origin_quantity(self):
        return self.origin_quantity

    def get_origin_price(self):
        return self.origin_price

    def get_order_notifier(self):
        return self.order_notifier

    def get_canceled_time(self):
        return self.canceled_time

    def get_executed_time(self):
        return self.executed_time

    def get_creation_time(self):
        return self.creation_time

    def set_last_prices(self, last_prices):
        self.last_prices = last_prices

    def get_create_last_price(self):
        return self.created_last_price

    def get_linked_portfolio(self):
        return self.linked_portfolio

    def is_filled(self):
        return self.status == OrderStatus.FILLED

    def is_cancelled(self):
        return self.status == OrderStatus.CANCELED

    def get_is_from_this_octobot(self):
        return self.is_from_this_octobot

    def set_is_from_this_octobot(self, is_from_this_octobot):
        self.is_from_this_octobot = is_from_this_octobot

    def get_profitability(self):
        if self.get_filled_price() is not 0 and self.get_create_last_price() is not 0:
            if self.get_filled_price() >= self.get_create_last_price():
                self.order_profitability = 1 - self.get_filled_price() / self.get_create_last_price()
                if self.side == TradeOrderSide.SELL:
                    self.order_profitability *= -1
            else:
                self.order_profitability = 1 - self.get_create_last_price() / self.get_filled_price()
                if self.side == TradeOrderSide.BUY:
                    self.order_profitability *= -1
        return self.order_profitability

    @classmethod
    def get_name(cls):
        return cls.__name__

    def default_exchange_update_order_status(self):
        result = self.exchange.get_order(self.order_id, self.symbol)
        new_status = self.trader.parse_status(result)
        if new_status == OrderStatus.FILLED:
            self.trader.parse_exchange_order_to_trade_instance(result, self)
        elif new_status == OrderStatus.CANCELED:
            self.cancel_from_exchange()

    def generate_executed_time(self, simulated_time=False):
        if not simulated_time or not self.last_prices:
            return time.time()
        else:
            return self.last_prices[-1][eC.TIMESTAMP.value]

class BuyMarketOrder(Order):
    def __init__(self, exchange):
        super().__init__(exchange)
        self.side = TradeOrderSide.BUY

    def update_order_status(self, simulated_time=False):
        if not self.trader.simulate:
            self.default_exchange_update_order_status()
        else:
            # ONLY FOR SIMULATION
            self.status = OrderStatus.FILLED
            self.filled_price = self.created_last_price
            self.filled_quantity = self.origin_quantity
            self.executed_time = self.generate_executed_time(simulated_time)

class BuyLimitOrder(Order):
    def __init__(self, exchange):
        super().__init__(exchange)
        self.side = TradeOrderSide.BUY

    def update_order_status(self, simulated_time=False):
        if not self.trader.simulate:
            self.default_exchange_update_order_status()
        else:
            # ONLY FOR SIMULATION
            if self.check_last_prices(self.origin_price, True, simulated_time):
                self.status = OrderStatus.FILLED
                self.filled_price = self.origin_price
                self.filled_quantity = self.origin_quantity
                self.executed_time = self.generate_executed_time(simulated_time)

class SellMarketOrder(Order):
    def __init__(self, exchange):
        super().__init__(exchange)
        self.side = TradeOrderSide.SELL

    def update_order_status(self, simulated_time=False):
        if not self.trader.simulate:
            self.default_exchange_update_order_status()
        else:
            # ONLY FOR SIMULATION
            self.status = OrderStatus.FILLED
            self.filled_price = self.created_last_price
            self.filled_quantity = self.origin_quantity
            self.executed_time = self.generate_executed_time(simulated_time)

class SellLimitOrder(Order):
    def __init__(self, exchange):
        super().__init__(exchange)
        self.side = TradeOrderSide.SELL

    def update_order_status(self, simulated_time=False):
        if not self.trader.simulate:
            self.default_exchange_update_order_status()
        else:
            # ONLY FOR SIMULATION
            if self.check_last_prices(self.origin_price, False, simulated_time):
                self.status = OrderStatus.FILLED
                self.filled_price = self.origin_price
                self.filled_quantity = self.origin_quantity
                self.executed_time = self.generate_executed_time(simulated_time)

class StopLossOrder(Order):
    def __init__(self, exchange):
        super().__init__(exchange)
        self.side = TradeOrderSide.SELL

    def update_order_status(self, simulated_time=False):
        if self.check_last_prices(self.origin_price, True, simulated_time):
            self.status = OrderStatus.FILLED
            self.filled_price = self.origin_price
            self.filled_quantity = self.origin_quantity
            self.executed_time = self.generate_executed_time(simulated_time)
            if not self.trader.simulate:
                market_sell = self.trader.create_order_instance(order_type=TraderOrderType.SELL_MARKET,
                                                                symbol=self.symbol,
                                                                current_price=self.origin_price,
                                                                quantity=self.origin_quantity,
                                                                price=self.origin_price,
                                                                linked_portfolio=self.linked_portfolio)
                with self.trader.get_portfolio() as pf:
                    self.trader.create_order(market_sell, pf)

# TODO
class StopLossLimitOrder(Order):
    def __init__(self, exchange):
        super().__init__(exchange)
        self.side = TradeOrderSide.SELL

    def update_order_status(self, simulated_time=False):
        pass

# TODO
class TakeProfitOrder(Order):
    def __init__(self, exchange):
        super().__init__(exchange)
        self.side = TradeOrderSide.SELL

    def update_order_status(self, simulated_time=False):
        pass

# TODO
class TakeProfitLimitOrder(Order):
    def __init__(self, exchange):
        super().__init__(exchange)
        self.side = TradeOrderSide.SELL

    def update_order_status(self, simulated_time=False):
        pass

class OrderConstants:
    TraderOrderTypeClasses = {
        TraderOrderType.BUY_MARKET: BuyMarketOrder,
        TraderOrderType.BUY_LIMIT: BuyLimitOrder,
        TraderOrderType.TAKE_PROFIT: TakeProfitOrder,
        TraderOrderType.TAKE_PROFIT_LIMIT: TakeProfitLimitOrder,
        TraderOrderType.STOP_LOSS: StopLossOrder,
        TraderOrderType.STOP_LOSS_LIMIT: StopLossLimitOrder,
        TraderOrderType.SELL_MARKET: SellMarketOrder,
        TraderOrderType.SELL_LIMIT: SellLimitOrder,
    }


import configparser
import logging
import os
import sys
import subprocess
# Upon the first import of non standard libraries, if not found
# Start pip install
try:
    import pyttsx3
    import requests
    from yaspin import yaspin
except ModuleNotFoundError:
    subprocess.run("pip3 install -r requirements.txt", shell=True)

from logging.handlers import RotatingFileHandler

from connections import test_tor, tor_request
from welcome import logo
from dashboard import main_dashboard

from ansi_management import (warning, success, error, info, clear_screen, bold,
                             muted, yellow)

# Main Variables
basedir = os.path.abspath(os.path.dirname(__file__))
debug_file = os.path.join(basedir, 'debug.log')

def load_config(quiet=False):
    # Load Config
    config_file = os.path.join(basedir, 'config.ini')
    CONFIG = configparser.ConfigParser()
    if quiet:
        CONFIG.read(config_file)
        return (CONFIG)

    with yaspin(text="Loading config.ini", color="cyan") as spinner:

        # Check that config file exists
        if os.path.isfile(config_file):
            CONFIG.read(config_file)
            spinner.ok("âœ… ")
            spinner.write(success("    Config Loaded [Success]"))
            print("")
            return (CONFIG)
        else:
            spinner.fail("ðŸ’¥ ")
            spinner.write(
                warning("    Config file could not be loaded [ERROR]"))
            print(error("    WARden requires config.ini to run. Quitting..."))
            exit()

def launch_logger():
    # Config of Logging
    formatter = "[%(asctime)s] %(message)s"
    logging.basicConfig(handlers=[
        RotatingFileHandler(filename=debug_file,
                            mode='w',
                            maxBytes=120,
                            backupCount=0)
    ],
                        level=logging.INFO,
                        format=formatter,
                        datefmt='%I:%M:%S %p')

def create_tor():
    # ----------------------------------------------
    #                 Test Tor
    # ----------------------------------------------
    with yaspin(text="Testing Tor", color="cyan") as spinner:
        tor = test_tor()
        if tor['status']:
            logging.info(success("Tor Connected"))
            spinner.ok("âœ… ")
            spinner.write(success("    Tor Connected [Success]"))
            print("")
            return (tor)
        else:
            logging.error(error("Could not connect to Tor"))
            spinner.fail("ðŸ’¥ ")
            spinner.write(warning("    Tor NOT connected [ERROR]"))
            print(
                error(
                    "    Could not connect to Tor. WARden requires Tor to run. Quitting..."
                ))
            print(
                info(
                    "    Download Tor at: https://www.torproject.org/download/"
                ))
            print("")
            exit()

def check_version():
    from dashboard import version
    current_version = version()
    with yaspin(
            text=f"Checking for updates. Running version: {current_version}",
            color="green") as spinner:

        url = 'https://raw.githubusercontent.com/pxsocs/warden_terminal/master/version.txt'
        try:
            remote_version = tor_request(url).text
        except Exception:
            print(yellow("  [!] Could not reach GitHub to check for upgrades"))
            return

        if str(remote_version).strip() == str(current_version).strip():
            spinner.ok("âœ… ")
            spinner.write(success("    You are running latest version"))
        else:
            spinner.fail("ðŸ’¥ ")
            spinner.write(
                warning(f"    Update available - version: {remote_version}"))
            import click
            if click.confirm(warning('    [?] Would you like to upgrade?'),
                             default=False):
                print(" ---------------------------------------")
                print(yellow(f"Upgrading from GitHub: ")),
                import subprocess
                subprocess.run("git fetch --all", shell=True)
                subprocess.run("git reset --hard origin/master", shell=True)
                print(yellow(f"Installing Python Package Requirements")),
                subprocess.run("pip3 install -r requirements.txt", shell=True)
                print(" ---------------------------------------")
                print(success("  âœ… Done Upgrading"))

        print("")

def greetings():
    # Welcome Sound
    if config['MAIN'].getboolean('welcome_sound'):
        with yaspin(text=config['MAIN'].get('welcome_text'),
                    color="cyan") as spinner:
            from yaspin.spinners import Spinners
            spinner.spinner = Spinners.moon
            try:
                engine = pyttsx3.init()
                engine.setProperty('rate', 270)
                engine.say(config['MAIN'].get('welcome_text'))
                engine.runAndWait()
            except Exception:
                pass
            spinner.stop()
            spinner.write("")

def exception_handler(exctype, value, tb):
    os.execv(sys.executable, ['python3'] + [sys.argv[0]] + ['quiet'])
    # print(exctype)
    # print(tb.print_last())

if __name__ == '__main__':
    if 'quiet' not in sys.argv:
        clear_screen()
        logo()
        print("")
        try:
            os.remove(debug_file)
        except Exception:
            pass
        launch_logger()
        logging.info(muted("Starting main program..."))
        config = load_config()
        tor = create_tor()
        check_version()
        greetings()
    else:
        launch_logger()
        config = load_config(True)
        tor = {
            "pre_proxy": 'Restarting...',
            "post_proxy": 'Restarting...',
            "post_proxy_ping": 'Restarting...',
            "pre_proxy_ping": 'Restarting...',
            "difference": 'Restarting...',
            "status": True,
            "port": 'Restarting...'
        }
    sys.excepthook = exception_handler
    print(success("   Launching the Dashboard..."))
    main_dashboard(config, tor)


import numpy
import skimage.io
from PIL import Image
import os
from sys import platform

def get_image_paths(path):
    files = []
    for r, d, f in os.walk(path):
        for file in f:
            if '.png' in file or '.jpg' in file:
                files.append(os.path.join(r, file))
    return files

def image_name_parser(filename):

    if platform == 'darwin':
        splitval = "/"
    else:
        splitval = "\\"

    file_path = filename.split(splitval)
    print(file_path)
    return int(file_path[-1][3:6])

def read_letter_images(filepath):
    load_letters = get_image_paths(filepath)

    #load_letters = load_letters[:10]
    print(len(load_letters))

    data_array = []
    progress = 0
    for file in load_letters:
        print(f"Progress: {progress*100/len(load_letters)}%")
        if filepath != "non_letters":
            classifier = image_name_parser(file)
        else:
            classifier = 0

        #print(classifier)
        letter = skimage.io.imread(file, as_gray=True)
        if len(data_array) == 0:
            data_array = numpy.array(sampler(letter, classifier))
        else:
            data_array = numpy.concatenate((data_array, sampler(letter, classifier)), axis=0)
        progress += 1

    data_array = numpy.array(data_array)
    # print(data_array[-1])

    # numpy.save("Unshuffled_datafile", data_array)
    return data_array

def image_resize(image_path):
    image = Image.open(image_path)
    new_image = image.resize((32,32))
    new_image.save(image_path)

def image_whitener(file):

    if platform == 'darwin':
        splitval = "/"
    else:
        splitval = "\\"

    nonletter = skimage.io.imread(file, as_gray=True)
    print("Image pixel value mean:", numpy.mean(nonletter))
    nonletter = nonletter + numpy.mean(nonletter)
    image = Image.fromarray(nonletter)
    if image.mode != 'RGB':
        image = image.convert('RGB')
    new_file_path = file.split(splitval)
    new_file = new_file_path[-1].split(".")
    s = splitval
    new_file_path = s.join(new_file_path[:-1])
    new_file_path = new_file_path + splitval +str(new_file[0]) + "_whitened.jpg"
    print(new_file_path)
    image.save(new_file_path)

def sampler(image, classifier):
    image_sample = []
    temp_array = []
    for column in range(0, len(image[0]), 8):
        for row in range(0, len(image[0]), 8):
            image_sample.append(image[column:column + 8, row:row + 8])
            temp_array.append(numpy.concatenate((numpy.array([num for sublist in image_sample[-1] for num in sublist]), [classifier])))
    return temp_array

def delete_bad_image(file):
    print(file)
    if "whitened.jpg" in file:
        print("yes")
        os.remove(file)

def read_nonletter_images(filepath):
    load_nonletters = get_image_paths(filepath)
    print(len(load_nonletters))
    #load_nonletters = load_nonletters[:2]

    data_array = []
    count = 0
    for file in load_nonletters:
        print("Progress:", str(count*100/len(load_nonletters)), "%")

        classifier = 0

        #print(file)
        nonletter = skimage.io.imread(file, as_gray=True)

        if len(data_array) == 0:
            data_array = numpy.array(sampler(nonletter, classifier))
        else:
            data_array = numpy.concatenate((data_array, sampler(nonletter, classifier)), axis=0)
        count += 1

    return data_array

    #print(len(data_array))
    #print(len(data_array[0]))
    #print(len(data_array))

    # iimage = Image.fromarray(data_array[0])
    # if iimage.mode != 'RGB':
    #     iimage = iimage.convert('RGB')
    # iimage.save("test-pic-2.jpg")

def add_border(image):
    size = len(image)
    filler = numpy.full((size, int((256 - size) / 2)), 255)
    image = numpy.concatenate((filler, image), axis=1)
    image = numpy.concatenate((image, filler), axis=1)

    filler = numpy.full((int((256 - size) / 2), 256), 255)
    image = numpy.concatenate((filler, image), axis=0)
    image = numpy.concatenate((image, filler), axis=0)
    return image

def average_pooling(image):
    print(len(image))
    # use max pooling to shrink image
    new_image = []
    for i in range(0, len(image), 2):
        row = []
        for j in range(0, len(image[i]), 2):
            calculate_avg = image[i][j] + image[i + 1][j] + image[i][j + 1] + image[i + 1][j + 1]
            calculate_avg = calculate_avg / 4
            row.append(calculate_avg)
        new_image.append(row)
    print(len(image))
    return numpy.array(new_image)

if __name__ == "__main__":
    #letters = read_letter_images()
    nonletters = read_nonletter_images()

    #letters_length = len(letters)
    nonletters_length = len(nonletters)

    #print(len(letters))
    print(len(nonletters))

    #unshuffled_data = numpy.concatenate((letters, nonletters), axis=0)
    numpy.save("NonLetterData/nonletter_data_array.npy", nonletters)

    #class_one = numpy.full((letters_length, 1), 1)
    #class_two = numpy.full((nonletters_length, 1), 0)
    #classes = numpy.concatenate((class_one, class_two), axis=0)

    #unshuffled_data = numpy.concatenate((unshuffled_data, classes), axis=1)
    #numpy.save("unshuffled_labeled_data", unshuffled_data)


from configparser import SafeConfigParser
import argparse
import datetime as dt
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

if __name__ == '__main__':

    # we might want to use it for controlling speed of the script
    # https://docs.python.org/3.6/library/profile.html

    if os.path.isfile('config.ini'):
        os.remove('config.ini')

    for dir in ['results', 'figs']:
        if not os.path.isdir(dir):
            os.makedirs(dir)

    parser = argparse.ArgumentParser()
    parser.add_argument('-p', dest='prompt', help='wanna see comments?')
    parser.add_argument('-v', action='store_true', dest='validation', help='data validation?')
    parser.add_argument('-b', '--batch_size', dest='batch_size', type=int)
    parser.add_argument('-t', '--test_batch_size', dest='test_batch_size', type=int)
    parser.add_argument('-e', '--episode_num', dest='episode_num', type=int)
    parser.add_argument('--acceleration', dest='acceleration')
    parser.add_argument('-c', '--channels', dest='channels')
    parser.add_argument('--prosocial', dest='prosocial')
    parser.add_argument('-te', '--test_every', dest='test_every', type=int)
    parser.add_argument('-f', '--filename', dest='filename')
    parser.set_defaults(validation=False, prompt='none', batch_size=2, test_batch_size=2, episode_num=2,
                        acceleration=False, channels='proposal', prosocial=False, test_every=50,
                        filename=str(dt.datetime.today()).replace(' ', '').replace(':', '').replace('.', ''))
    args = parser.parse_args()

    prompt = args.__dict__['prompt']
    validation = args.__dict__['validation']
    batch_size = args.__dict__['batch_size']
    test_batch_size = args.__dict__['test_batch_size']
    episode_num = args.__dict__['episode_num']
    acceleration = args.__dict__['acceleration']
    channels = args.__dict__['channels'].split(',')
    prosocial = args.__dict__['prosocial']
    test_every = args.__dict__['test_every']
    filename = args.__dict__['filename']

    config = SafeConfigParser()
    config.read('config.ini')
    config.add_section('project_settings')
    config.set('project_settings', 'prompt', prompt)
    config.set('project_settings', 'validation', str(validation))
    config.set('project_settings', 'acceleration', str(acceleration))

    config.add_section('game_settings')
    config.set('game_settings', 'batch_size', str(batch_size))
    config.set('game_settings', 'test_batch_size', str(test_batch_size))
    config.set('game_settings', 'episode_num', str(episode_num))
    config.set('game_settings', 'prosocial', str(prosocial))
    config.set('game_settings', 'test_every', str(test_every))
    config.set('game_settings', 'filename', str(filename))

    config.add_section('agent_settings')
    config.set('agent_settings', 'linguistic_channel', str('linguistic' in channels))
    config.set('agent_settings', 'proposal_channel', str('proposal' in channels))

    with open('config.ini', 'a') as f:
        config.write(f)

    # emergent module has to be imported after config.init file is created.
    import emergent
    from emergent.utils import print_status, print_all

    # project_settings = emergent.settings.ProjectSettings(
    #     prompt=prompt,
    #     validation=validation,
    #     acceleration=acceleration
    # )

    project_settings, agent_settings, game_settings = emergent.settings.load_settings()

    for settings in [project_settings, agent_settings, game_settings]:
        print(settings)

    print_status('### Agents initialization. ###\n')
    agents = emergent.Agent.create_agents(n=2, **agent_settings.as_dict())
    print_all('agents summary')
    for agent in agents:
        print('\nsummary {}'.format(agent.id))
        print(agent.core_layer.model.summary())

    print_status('\n### Game initialization. ###\n')
    game = emergent.Game(agents=agents, **game_settings.as_dict())

    print_status('\n### Starting experiment. ###\n')
    # import cProfile
    # cProfile.run('game.play()')
    game.play()
    print_status('\n### ### Done. ### ###\n')
    emergent.settings.save_config(project_settings=project_settings, agent_settings=agent_settings, game_settings=game_settings, filename=filename)
    print('Results should be saved here: results/{}.pkl'.format(filename))


import pygame
import sys
from constants import *

class Piece(pygame.sprite.Sprite):
    def __init__(self, alliance, position, image):
        super(Piece, self).__init__()
        self.alliance = alliance
        self.position = position
        self.clicked = False
        self.image = pygame.transform.scale(pygame.image.load('sprites/' + image), (BLOCK_LENGTH, BLOCK_LENGTH))
        self.rect = self.image.get_rect()
        self.snap_piece()
        sprites.add(self)
        B.grid[self.position[0]][self.position[1]] = self

    def snap_piece(self):
        self.rect.topleft = [(self.position[1] * BLOCK_LENGTH), (self.position[0] * BLOCK_LENGTH)]

    def check_move(self, xpos_end, ypos_end):
        if self.alliance == T.get_turn():
            for vector in self.move_vector:
                for i in range(self.distance_limit):
                    try:
                        v0 = vector[0] * (i + 1)
                        v1 = vector[1] * (i + 1)
                        current_alliance = getattr(B.grid[self.position[0] + v1][self.position[1] + v0], "alliance", 0)
                        if current_alliance == 0:
                            if (ypos_end == self.position[0] + v1) and (xpos_end == self.position[1] + v0):
                                self.place_piece([ypos_end, xpos_end])
                                break
                        elif current_alliance == self.alliance * -1:
                            if (ypos_end == self.position[0] + v1) and (xpos_end == self.position[1] + v0):
                                self.take_piece([ypos_end, xpos_end])
                            break
                        else:
                            break
                    except IndexError:
                        continue
        self.snap_piece()

    def place_piece(self, new_position):
        B.grid[self.position[0]][self.position[1]] = None
        self.position = new_position
        B.grid[self.position[0]][self.position[1]] = self
        if isinstance(self, Pawn):
            self.set_distance_limit()
        T.set_turn()

    def take_piece(self, new_position):
        sprites.remove(B.grid[new_position[0]][new_position[1]])
        self.place_piece(new_position)

class Pawn(Piece):
    def __init__(self, alliance, position, image):
        super().__init__(alliance, position, image)
        self.distance_limit = 2
        self.move_vector = [[0, -1]]
        self.take_vector = [[-1, -1], [1, -1]]
        if self.alliance == -1:
            self.move_vector = [[i * -1 for i in j] for j in self.move_vector]
            self.take_vector = [[i * -1 for i in j] for j in self.take_vector]

    def set_distance_limit(self):
        self.distance_limit = 1

    # def check_move(self, xpos_end, ypos_end):
    #     if self.alliance == T.get_turn():
    #         for vector in self.move_vector:
    #             for i in range(self.distance_limit):
    #                 try:
    #                     v0 = vector[0] * (i + 1)
    #                     v1 = vector[1] * (i + 1)
    #                     current_alliance = getattr(B.grid[self.position[0] + v1][self.position[1] + v0], "alliance", 0)
    #                     if current_alliance == 0:
    #                         if (ypos_end == self.position[0] + v1) and (xpos_end == self.position[1] + v0):
    #                             self.place_piece([ypos_end, xpos_end])
    #                             break
    #                     elif current_alliance == self.alliance * -1:
    #                         if (ypos_end == self.position[0] + v1) and (xpos_end == self.position[1] + v0):
    #                             self.take_piece([ypos_end, xpos_end])
    #                         break
    #                     else:
    #                         break
    #                 except IndexError:
    #                     continue
    #     self.snap_piece()

class Rook(Piece):
    def __init__(self, alliance, position, image):
        super().__init__(alliance, position, image)
        self.move_vector = [[0, -1], [1, 0], [0, 1], [-1, 0]]
        self.distance_limit = 8

class Knight(Piece):
    def __init__(self, alliance, position, image):
        super().__init__(alliance, position, image)
        self.move_vector = [[1, -2], [2, -1], [2, 1], [1, 2], [-1, 2], [-2, 1], [-2, -1], [-1, -2]]
        self.distance_limit = 1

class Bishop(Piece):
    def __init__(self, alliance, position, image):
        super().__init__(alliance, position, image)
        self.move_vector = [[1, -1], [1, 1], [-1, 1], [-1, -1]]
        self.distance_limit = 8

class Queen(Piece):
    def __init__(self, alliance, position, image):
        super().__init__(alliance, position, image)
        self.move_vector = [[0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1]]
        self.distance_limit = 8

class King(Piece):
    def __init__(self, alliance, position, image):
        super().__init__(alliance, position, image)
        self.move_vector = [[0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1]]
        self.distance_limit = 1

class Board:
    def __init__(self):
        self.ROWS, self.COLUMNS = (8, 8)
        # creating a 2D array of size 8 by 8 filled with Empty classes
        self.grid = [[None for _ in range(self.ROWS)] for _ in range(self.COLUMNS)]

class Turn:
    def __init__(self):
        self.turn = 1

    def get_turn(self):
        return self.turn

    def set_turn(self):
        self.turn *= -1

def board_init():
    Rook(-1, [0, 0], 'b_rook.png')
    Knight(-1, [0, 1], 'b_knight.png')
    Bishop(-1, [0, 2], 'b_bishop.png')
    Queen(-1, [0, 3], 'b_queen.png')
    King(-1, [0, 4], 'b_king.png')
    Bishop(-1, [0, 5], 'b_bishop.png')
    Knight(-1, [0, 6], 'b_knight.png')
    Rook(-1, [0, 7], 'b_rook.png')
    for i in range(8):
        Pawn(-1, [1, i], 'b_pawn.png')
    Rook(1, [7, 0], 'w_rook.png')
    Knight(1, [7, 1], 'w_knight.png')
    Bishop(1, [7, 2], 'w_bishop.png')
    Queen(1, [7, 3], 'w_queen.png')
    King(1, [7, 4], 'w_king.png')
    Bishop(1, [7, 5], 'w_bishop.png')
    Knight(1, [7, 6], 'w_knight.png')
    Rook(1, [7, 7], 'w_rook.png')
    for i in range(8):
        Pawn(1, [6, i], 'w_pawn.png')

def minmax():
    pass

def zero_div(n, d):
    return n / d if d else 0

def draw_board(surface, grid):
    for je, j in enumerate(grid):
        for ie, i in enumerate(j):
            myrect = pygame.Rect(ie * BLOCK_LENGTH, je * BLOCK_LENGTH, BLOCK_LENGTH, BLOCK_LENGTH)
            if (je + ie) % 2 == 1:
                pygame.draw.rect(surface, BLACK, myrect)
            else:
                pygame.draw.rect(surface, WHITE, myrect)

def update(surface, grid):
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                if event.button == 1:
                    for sprite in sprites.sprites():
                        if sprite.rect.collidepoint(pos):
                            sprite.clicked = True
                            sprites.move_to_front(sprite)

            if event.type == pygame.MOUSEBUTTONUP:
                for sprite in sprites.sprites():
                    if sprite.clicked:
                        pos = pygame.mouse.get_pos()
                        xpos_end = int(pos[0] / BLOCK_LENGTH)
                        ypos_end = int(pos[1] / BLOCK_LENGTH)
                        sprite.check_move(xpos_end, ypos_end)
                        sprite.clicked = False

        for sprite in sprites.sprites():
            if sprite.clicked:
                pos = pygame.mouse.get_pos()
                sprite.rect.topleft = [pos[0] - (sprite.rect.width / 2), pos[1] - (sprite.rect.height / 2)]

        pygame.display.update()
        draw_board(surface, grid)
        sprites.draw(surface)
        clock.tick(FPS)

pygame.init()
clock = pygame.time.Clock()
surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
sprites = pygame.sprite.LayeredUpdates()
B = Board()
T = Turn()
board_init()
update(surface, B.grid)

# https://opengameart.org/content/chess-pieces-and-board-squares


import re
import string
import pandas as pd
from sklearn.metrics import accuracy_score, log_loss
from sklearn.model_selection import cross_val_predict
import tensorflow as tf
from tensorflow.keras import layers
import tensorflow_datasets as tfds
from scikeras.wrappers import KerasClassifier

SEED = 123456  # for reproducibility

#加载数据集
f = open("/home/shenyujie/dataset/有效警告修改(modified).txt")
s = f.read()
f.close()
raw_full_texts = s.split("\n")
for i in range(0,len(raw_full_texts)-1):
    temp = raw_full_texts[i].split(" ")
    raw_full_texts[i] = ""
    for j in range(0,7):
        raw_full_texts[i] = raw_full_texts[i] + temp[j] +" "

f = open("/home/shenyujie/dataset/无效警告修改(modified).txt")
s = f.read()
f.close()
temp = s.split("\n")
for i in range(0,len(temp)):
    raw_full_texts.append(temp[i])

full_labels = []
for i in range(0,1967):
    full_labels.append(1)
for i in range(0,2186):
    full_labels.append(0)

# 打乱数据集
import numpy as np

np.random.seed(123456)
np.random.shuffle(raw_full_texts)
np.random.seed(123456)
np.random.shuffle(full_labels)
np.random.seed(123456)

# 设定将文本参数化的参数，并将输入文本参数化
max_features = 10000
sequence_length = 100

vectorize_layer = layers.TextVectorization(

    max_tokens=max_features,
    output_mode="int",
    output_sequence_length=sequence_length,
)

vectorize_layer.adapt(raw_full_texts)
full_texts = vectorize_layer(raw_full_texts)
full_texts = full_texts.numpy()

# 构建神经网络
def get_net():
    net = tf.keras.Sequential(
        [
            tf.keras.Input(shape=(None,), dtype="int64"),
            layers.Embedding(max_features + 1, 16),
            layers.Dropout(0.2),
            layers.GlobalAveragePooling1D(),
            layers.Dropout(0.2),
            layers.Dense(num_classes),
            layers.Softmax()
        ]
    )

    net.compile(
        optimizer="adam",
        loss=tf.keras.losses.SparseCategoricalCrossentropy(),
        metrics=tf.keras.metrics.CategoricalAccuracy(),
    )
    return net

# 初始化模型
num_classes = len(set(full_labels))
model = KerasClassifier(get_net(), epochs=10)

# 设定k值并进行k折交叉验证
k_folds = 10
pred_probs = cross_val_predict(
    model,
    full_texts,
    full_labels,
    cv=k_folds,
    method="predict_proba",
)

loss = log_loss(full_labels, pred_probs)  #对于k-折交叉验证基于loss值进行评估
print(f"Cross-validated estimate of log loss: {loss:.3f}")

# 使用cleanlab寻找噪声
from cleanlab.filter import find_label_issues

ranked_label_issues = find_label_issues(
    labels=full_labels, pred_probs=pred_probs, return_indices_ranked_by="self_confidence"
)
print(ranked_label_issues)

res = ""
for i in range(0,len(ranked_label_issues)):
    res = res + raw_full_texts[ranked_label_issues[i]] +" "+full_labels[ranked_label_issues[i]] +"\n"

f = open("/home/shenyujie/dataset/result-0.txt","w")
f.write(res)
f.close()

#--------------------------------------------------------------
# 下面将cleanlab与常规模型的拟合程度进行比对
#--------------------------------------------------------------

# 加载数据集
f = open("/home/shenyujie/dataset/有效警告修改(modified).txt")
s = f.read()
f.close()
raw_full_texts = s.split("\n")

f = open("/home/shenyujie/dataset/无效警告修改(modified).txt")
s = f.read()
f.close()
temp = s.split("\n")
print(len(temp))
for i in range(0,len(temp)):
    raw_full_texts.append(temp[i])
print(len(raw_full_texts))

full_labels = []
for i in range(0,1967):
    full_labels.append(1)
for i in range(0,2186):
    full_labels.append(0)

# 打乱数据集
import numpy as np

np.random.seed(123456)
np.random.shuffle(raw_full_texts)
np.random.seed(123456)
np.random.shuffle(full_labels)
np.random.seed(123456)

# 划分训练集和测试集
raw_train_texts = raw_full_texts[0:3200]
train_labels = full_labels[0:3200]
raw_test_texts = raw_full_texts[3200:len(raw_full_texts)]
test_labels = full_labels[3200:len(full_labels)]

# 向量化数据
vectorize_layer.reset_state()
vectorize_layer.adapt(raw_train_texts)

train_texts = vectorize_layer(raw_train_texts)
test_texts = vectorize_layer(raw_test_texts)

train_texts = train_texts.numpy()
test_texts = test_texts.numpy()

# 常规模型进行训练并测试拟合度
model = KerasClassifier(get_net(), epochs=10)
model.fit(train_texts, train_labels)

preds = model.predict(test_texts)
acc_og = accuracy_score(test_labels, preds)
print(f"\n Test accuracy of original neural net: {acc_og}")

# 使用cleanlab进行训练并测试拟合度
from cleanlab.classification import CleanLearning

model = KerasClassifier(get_net(), epochs=10)  # Note we first re-instantiate the model
cl = CleanLearning(clf=model, seed=SEED)  # cl has same methods/attributes as model

_ = cl.fit(train_texts, train_labels)

pred_labels = cl.predict(test_texts)
acc_cl = accuracy_score(test_labels, pred_labels)
print(f"Test accuracy of cleanlab's neural net: {acc_cl}")


#
# @lc app=leetcode id=1089 lang=python3
#
# [1089] Duplicate Zeros
#

# @lc code=start
from typing import List
class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        """
        Do not return anything, modify arr in-place instead.
        """
        n = len(arr)
        temp = list(arr)
        i = 0
        while i < len(temp):
            if temp[i] == 0:
                temp.insert(i, 0)
                i += 2
            else:
                i += 1
        for i in range(n):
            arr[i] = temp[i]

# @lc code=end


import argparse

lookup = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

def separate_groups(lines):
    for i in range(0, len(lines), 3):
        yield lines[i:i+3]

def find_badge_priority(group):
    for item in group[0]:
        if item in group[1] and item in group[2]:
            return(lookup.index(item))

def calculate(line):
    midpoint = int((len(line) - 1) / 2)
    part1 = line[:midpoint]
    part2 = line[midpoint:]
    for item in part1:
        if item in part2:
            return lookup.index(item)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("filename")
    args = parser.parse_args()
    with open(args.filename) as f:
        print(sum(map(find_badge_priority, separate_groups(f.readlines()))))

if __name__ == '__main__':
    main()


import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','formproject3.settings')
import django
django.setup()
from Testapp.models import StudentInfo
from random import randint
from faker import Faker
fake=Faker()
def Age():
    num=randint(20,100)
    return int(num)
def Papu(n):
    for i in range(n):
        fname=fake.name()
        fage=Age()
        faddress=fake.address()
        femail=fake.email()
        kemo=StudentInfo.objects.get_or_create(name=fname,age=fage,address=faddress,email=femail)
Papu(25)


import os
from zipfile import ZipFile
from fuse.utils.file_io.file_io import create_dir
import wget
from typing import Hashable, Optional, Sequence, List, Tuple
import torch
import numpy as np
import pytorch_lightning as pl
from fuse.data import DatasetDefault
from fuse.data.ops.ops_cast import OpToTensor
from fuse.data.utils.sample import get_sample_id
from fuse.data.pipelines.pipeline_default import PipelineDefault
from fuse.data.ops.op_base import OpBase
from fuse.data.datasets.caching.samples_cacher import SamplesCacher
from fuse.data.ops.ops_aug_common import OpSample, OpRandApply
from fuse.data.ops.ops_read import OpReadDataframe
from fuse.data.ops.ops_common import OpLambda, OpOverrideNaN, OpConcat
from fuseimg.data.ops.color import OpToRange
from fuse.data.ops.ops_aug_tabular import OpAugOneHot
from fuse.data.utils.split import dataset_balanced_division_to_folds
from fuse.utils import NDict

from fuseimg.data.ops.image_loader import OpLoadImage
from fuseimg.data.ops.aug.color import OpAugColor, OpAugGaussian
from fuseimg.data.ops.aug.geometry import OpResizeTo, OpAugAffine2D
from fuse.utils.rand.param_sampler import Uniform, RandInt, RandBool
from fuse.data.utils.samplers import BatchSamplerDefault
from torch.utils.data import DataLoader
from fuse.data.utils.collates import CollateDefault

class OpISICSampleIDDecode(OpBase):
    def __call__(self, sample_dict: NDict) -> NDict:
        """
        decodes sample id into image file name
        """
        sid = get_sample_id(sample_dict)

        img_filename_key = "data.input.img_path"
        sample_dict[img_filename_key] = sid + ".jpg"

        return sample_dict

def derive_label(sample_dict: NDict) -> NDict:
    """
    Takes the sample's ndict with the labels as key:value and assigns to sample_dict['data.label'] the index of the sample's class.
    Also delete all the labels' keys from sample_dict.

    for example:
        If the sample contains {'MEL': 0, 'NV': 1, 'BCC': 0, 'AK': 0, ... }
        will assign, sample_dict['data.label'] = 1 ('NV's index).
        Afterwards the sample_dict won't contain the class' names & values.
    """
    classes_names = ISIC.CLASS_NAMES

    label = 0
    for idx, cls_name in enumerate(classes_names):
        if int(sample_dict[f"data.cls_labels.{cls_name}"]) == 1:
            label = idx

    sample_dict["data.label"] = label
    return sample_dict

class ISIC:
    """
    ISIC 2019 challenge to classify dermoscopic images and clinical data among nine different diagnostic categories.
    """

    # bump whenever the static pipeline modified
    DATASET_VER = 0

    CLASS_NAMES = ["MEL", "NV", "BCC", "AK", "BKL", "DF", "VASC", "SCC"]
    SEX_INDEX = {"male": 0, "female": 1, "N/A": 2}
    ANATOM_SITE_INDEX = {
        "anterior torso": 0,
        "upper extremity": 1,
        "posterior torso": 2,
        "lower extremity": 3,
        "lateral torso": 4,
        "head/neck": 5,
        "palms/soles": 6,
        "oral/genital": 7,
        "N/A": 8,
    }

    @staticmethod
    def download(
        data_path: str, sample_ids_to_download: Optional[Sequence[str]] = None
    ) -> None:
        """
        Download images and metadata from ISIC challenge.
        Doesn't download again if data exists.
        :param data_path: location to store the data
        :param sample_ids_to_downlad: use if you want to download subset of the data. None will download and extract all the data.
        """
        create_dir(data_path)
        create_dir(os.path.join(data_path, "ISIC2019"))
        path = os.path.join(data_path, "ISIC2019/ISIC_2019_Training_Input")
        if not os.path.exists(path):
            print(
                "\nExtract ISIC-2019 training input ... (this may take a few minutes)"
            )

            url = "https://isic-challenge-data.s3.amazonaws.com/2019/ISIC_2019_Training_Input.zip"
            filename = os.path.join(data_path, "ISIC_2019_Training_Input.zip")
            print(f"Extract to {path}")
            if not os.path.exists(filename):
                wget.download(
                    url, os.path.join(data_path, "ISIC_2019_Training_Input.zip")
                )

            if sample_ids_to_download is not None:
                members = [
                    os.path.join("ISIC_2019_Training_Input", m + ".jpg")
                    for m in sample_ids_to_download
                ]
            else:
                members = None

            with ZipFile(filename, "r") as zipObj:
                # Extract all the contents of zip file in current directory
                zipObj.extractall(
                    path=os.path.join(data_path, "ISIC2019"), members=members
                )

            print("Extracting ISIC-2019 training input: done")

        path = os.path.join(data_path, "ISIC2019/ISIC_2019_Training_GroundTruth.csv")

        if not os.path.exists(path):
            print("\nExtract ISIC-2019 training gt ... (this may take a few minutes)")

            url = "https://isic-challenge-data.s3.amazonaws.com/2019/ISIC_2019_Training_GroundTruth.csv"
            wget.download(
                url,
                os.path.join(data_path, "ISIC2019/ISIC_2019_Training_GroundTruth.csv"),
            )

            print("Extracting ISIC-2019 training gt: done")

        path = os.path.join(data_path, "ISIC2019/ISIC_2019_Training_Metadata.csv")

        if not os.path.exists(path):
            print("\nExtract ISIC-2019 metadata ... (this may take a few minutes)")

            url = "https://isic-challenge-data.s3.amazonaws.com/2019/ISIC_2019_Training_Metadata.csv"
            wget.download(
                url, os.path.join(data_path, "ISIC2019/ISIC_2019_Training_Metadata.csv")
            )

            print("Extracting ISIC-2019 metadata: done")

    @staticmethod
    def sample_ids(data_path: str) -> List[str]:
        """
        Gets the samples ids in trainset.
        """
        images_path = os.path.join(data_path, "ISIC2019/ISIC_2019_Training_Input")

        samples = [
            f.split(".")[0]
            for f in os.listdir(images_path)
            if f.split(".")[-1] == "jpg"
        ]
        return samples

    @staticmethod
    def static_pipeline(data_path: str) -> PipelineDefault:
        rename_cls_labels = {c: f"data.cls_labels.{c}" for c in ISIC.CLASS_NAMES}
        rename_cls_labels[
            "image"
        ] = "data.cls_labels.sample_id"  # also extract image (sample_id)
        rename_metadata = {
            "age_approx": "data.input.clinical.age_approx",
            "anatom_site_general": "data.input.clinical.anatom_site_general",
            "sex": "data.input.clinical.sex",
            "image": "data.input.clinical.sample_id",
        }

        static_pipeline = PipelineDefault(
            "static",
            [
                # Decoding sample ID
                (OpISICSampleIDDecode(), dict()),
                # Load Image
                (
                    OpLoadImage(
                        os.path.join(data_path, "ISIC2019/ISIC_2019_Training_Input")
                    ),
                    dict(key_in="data.input.img_path", key_out="data.input.img"),
                ),
                # Normalize Images to range [0, 1]
                (
                    OpToRange(),
                    dict(key="data.input.img", from_range=(0, 255), to_range=(0, 1)),
                ),
                # Read labels into sample_dict. Each class will have a different entry.
                (
                    OpReadDataframe(
                        data_filename=os.path.join(
                            data_path, "ISIC2019/ISIC_2019_Training_GroundTruth.csv"
                        ),
                        key_column="data.cls_labels.sample_id",
                        columns_to_extract=list(rename_cls_labels.keys()),
                        rename_columns=rename_cls_labels,
                    ),
                    dict(),
                ),
                # Read metadata into sample_dict
                (
                    OpReadDataframe(
                        data_filename=os.path.join(
                            data_path, "ISIC2019/ISIC_2019_Training_Metadata.csv"
                        ),
                        key_column="data.input.clinical.sample_id",
                        columns_to_extract=list(rename_metadata.keys()),
                        rename_columns=rename_metadata,
                    ),
                    dict(),
                ),
                (
                    OpOverrideNaN(),
                    dict(
                        key="data.input.clinical.anatom_site_general",
                        value_to_fill="N/A",
                    ),
                ),
                (
                    OpOverrideNaN(),
                    dict(key="data.input.clinical.sex", value_to_fill="N/A"),
                ),
                (
                    OpOverrideNaN(),
                    dict(key="data.input.clinical.age_approx", value_to_fill=-1.0),
                ),
                # Squeeze labels into sample_dict['data.label']
                (OpLambda(func=derive_label), dict()),
                # Encode meta-data
                (
                    OpEncodeMetaData(),
                    dict(
                        key_site="data.input.clinical.anatom_site_general",
                        key_sex="data.input.clinical.sex",
                        key_age="data.input.clinical.age_approx",
                        out_prefix="data.input.clinical.encoding",
                    ),
                ),
            ],
        )
        return static_pipeline

    @staticmethod
    def dynamic_pipeline(
        train: bool = False, append: Optional[Sequence[Tuple[OpBase, dict]]] = None
    ) -> PipelineDefault:
        """
        Get suggested dynamic pipeline. including pre-processing that might be modified and augmentation operations.
        :param train: add augmentation if True
        :param append: pipeline steps to append at the end of the suggested pipeline
        """

        dynamic_pipeline = [
            # Resize images to 300x300x3
            (
                OpResizeTo(channels_first=True),
                dict(
                    key="data.input.img",
                    output_shape=(300, 300, 3),
                    mode="reflect",
                    anti_aliasing=True,
                ),
            ),
            # Convert to tensor for the augmentation process
            (OpToTensor(), dict(key="data.input.img", dtype=torch.float)),
        ]

        if train:
            dynamic_pipeline += [
                # Image Augmentation
                # Augmentation
                (
                    OpSample(OpAugAffine2D()),
                    dict(
                        key="data.input.img",
                        rotate=Uniform(-180.0, 180.0),
                        scale=Uniform(0.9, 1.1),
                        flip=(RandBool(0.3), RandBool(0.3)),
                        translate=(RandInt(-50, 50), RandInt(-50, 50)),
                    ),
                ),
                # Color augmentation
                (
                    OpSample(OpAugColor()),
                    dict(
                        key="data.input.img",
                        gamma=Uniform(0.9, 1.1),
                        contrast=Uniform(0.85, 1.15),
                        add=Uniform(-0.06, 0.06),
                        mul=Uniform(0.95, 1.05),
                    ),
                ),
                # Add Gaussian noise
                (OpAugGaussian(), dict(key="data.input.img", std=0.03)),
                # Meta-data Augmentation
                # Drop age with prob
                (
                    OpRandApply(OpAugOneHot(), probability=0.05),
                    dict(key="data.input.clinical.encoding.sex", idx=2, mode="default"),
                ),
                # switch age class with prob, except from unknowns
                (
                    OpRandApply(OpAugOneHot(), probability=0.05),
                    dict(
                        key="data.input.clinical.encoding.age",
                        mode="ranking",
                        freeze_indices=[6],
                    ),
                ),
            ]

        final = [
            # concat tabular data to one vector and cast it to tensor
            (
                OpConcat(),
                dict(
                    keys_in=[
                        f"data.input.clinical.encoding.{c}"
                        for c in ["site", "sex", "age"]
                    ],
                    key_out="data.input.clinical.all",
                ),
            ),
            (OpToTensor(), dict(key="data.input.clinical.all", dtype=torch.float)),
        ]

        if append is not None:
            dynamic_pipeline += append

        dynamic_pipeline += final

        return PipelineDefault("dynamic", dynamic_pipeline)

    @staticmethod
    def dataset(
        data_path: str,
        cache_path: str,
        train: bool = False,
        reset_cache: bool = False,
        num_workers: int = 10,
        append_dyn_pipeline: Optional[Sequence[Tuple[OpBase, dict]]] = None,
        samples_ids: Optional[Sequence[Hashable]] = None,
    ) -> DatasetDefault:
        """
        Get cached dataset
        :param train: if true, apply augmentations in the dynamic pipeline
        :param reset_cache: set to True to reset the cache
        :param num_workers: number of processes used for caching
        :param append_dyn_pipeline: pipeline steps to append at the end of the suggested dynamic pipeline
        :param samples_ids: dataset including the specified samples_ids or None for all the samples.
        """
        # Download data if doesn't exist
        ISIC.download(data_path=data_path, sample_ids_to_download=samples_ids)

        if samples_ids is None:
            samples_ids = ISIC.sample_ids(data_path)

        static_pipeline = ISIC.static_pipeline(data_path)
        dynamic_pipeline = ISIC.dynamic_pipeline(train, append=append_dyn_pipeline)

        cacher = SamplesCacher(
            f"isic_cache_ver{ISIC.DATASET_VER}",
            static_pipeline,
            [cache_path],
            restart_cache=reset_cache,
            workers=num_workers,
        )

        my_dataset = DatasetDefault(
            sample_ids=samples_ids,
            static_pipeline=static_pipeline,
            dynamic_pipeline=dynamic_pipeline,
            cacher=cacher,
        )

        my_dataset.create()
        return my_dataset

class ISICDataModule(pl.LightningDataModule):
    """
    Example of a custom Lightning datamodule that use FuseMedML tools.
    """

    def __init__(
        self,
        data_dir: str,
        cache_dir: str,
        num_workers: int,
        batch_size: int,
        train_folds: List[int],
        validation_folds: List[int],
        infer_folds: List[int],
        split_filename: str,
        sample_ids: Optional[Sequence[Hashable]] = None,
        reset_cache: bool = False,
        reset_split: bool = False,
        use_batch_sampler: bool = True,
        verify_folds_total_size: bool = True,
    ):
        """
        :param data_dir: path to data directory
        :param cache_dir: path to cache directory
        :param num_workers: number of process to use
        :param batch_size: model's batch size
        :param train_folds: which folds will be used for training
        :param validation_folds: which folds will be used for validation
        :param infer_folds: which folds will be used for inference (final evaluation)
        :param split_filename: path to file that will contain the data's split to folds
        :param sample_ids: subset of the sample ids to use, if None - use all.
        :param reset_cache: set True to reset the cache data
        :param reset_split: set True to reset the split file
        :param use_batch_sample: set True to use Fuse's custom balanced batch sampler (see BatchSamplerDefault class)
        :param verify_folds_total_size: (bool), when true - if a split file was found, will verify the the number of samples in the folds sum up to the amount of samples in the given dataset
        """
        super().__init__()
        self._data_dir = data_dir
        self._cache_dir = cache_dir
        self._num_workers = num_workers
        self._batch_size = batch_size
        self._use_batch_sampler = use_batch_sampler

        # divide into balanced train, validation and evaluation folds
        self._train_ids = []
        self._validation_ids = []
        self._infer_ids = []

        all_data = ISIC.dataset(
            self._data_dir,
            self._cache_dir,
            num_workers=self._num_workers,
            train=False,
            reset_cache=reset_cache,
            samples_ids=sample_ids,
        )

        folds = dataset_balanced_division_to_folds(
            dataset=all_data,
            keys_to_balance=["data.label"],
            nfolds=len(train_folds + validation_folds + infer_folds),
            output_split_filename=split_filename,
            reset_split=reset_split,
            verify_folds_total_size=verify_folds_total_size,
        )

        for fold in train_folds:
            self._train_ids += folds[fold]

        for fold in validation_folds:
            self._validation_ids += folds[fold]

        for fold in infer_folds:
            self._infer_ids += folds[fold]

    def setup(self, stage: str) -> None:
        """
        creates datasets by stage
        called on every process in DDP

        :param stage: trainer stage
        """
        # assign train/val datasets
        if stage == "fit":
            self._train_dataset = ISIC.dataset(
                self._data_dir,
                self._cache_dir,
                num_workers=self._num_workers,
                train=True,
                samples_ids=self._train_ids,
            )
            self._validation_dataset = ISIC.dataset(
                self._data_dir,
                self._cache_dir,
                num_workers=self._num_workers,
                train=False,
                samples_ids=self._validation_ids,
            )

        # assign prediction (infer) dataset
        if stage == "predict":
            self._predict_dataset = ISIC.dataset(
                self._data_dir,
                self._cache_dir,
                num_workers=self._num_workers,
                train=False,
                samples_ids=self._infer_ids,
            )

    def train_dataloader(self) -> DataLoader:
        """
        returns train dataloader with class args
        """
        if self._use_batch_sampler:
            # Create a batch sampler for the dataloader
            batch_sampler = BatchSamplerDefault(
                dataset=self._train_dataset,
                balanced_class_name="data.label",
                num_balanced_classes=8,
                batch_size=self._batch_size,
                workers=self._num_workers,
                verbose=True,
            )
            batch_size = 1  # should not provide batch_size for custom batch_sampler (1 is default)

        else:
            batch_sampler = None
            batch_size = self._batch_size

        # Create dataloader
        train_dl = DataLoader(
            dataset=self._train_dataset,
            batch_sampler=batch_sampler,
            batch_size=batch_size,
            collate_fn=CollateDefault(),
            num_workers=self._num_workers,
        )

        return train_dl

    def val_dataloader(self) -> DataLoader:
        """
        returns validation dataloader with class args
        """
        # Create dataloader
        validation_dl = DataLoader(
            dataset=self._validation_dataset,
            collate_fn=CollateDefault(),
            num_workers=self._num_workers,
            batch_size=self._batch_size,
        )

        return validation_dl

    def predict_dataloader(self) -> DataLoader:
        """
        returns validation dataloader with class args
        """
        # Create dataloader
        predict_dl = DataLoader(
            dataset=self._predict_dataset,
            collate_fn=CollateDefault(),
            num_workers=self._num_workers,
            batch_size=self._batch_size,
        )
        return predict_dl

class OpEncodeMetaData(OpBase):
    """
    Encode ISIC 2019 meta-data
    """

    def __init__(self, items_to_encode: List[str] = ["site", "sex", "age"]):
        """
        :param  items_to_encode: which items will be encoded. by default takes all the options.
        """
        super().__init__()

        self._items_to_encode = items_to_encode

    def __call__(
        self,
        sample_dict: NDict,
        key_site: str,
        key_sex: str,
        key_age: str,
        out_prefix: str,
    ) -> NDict:
        """
        :param key_site: sample_dict's key for patient's anatom site data
        :param key_sex: sample_dict's key for patient's sex data
        :param key_age: sample_dict's key for patient's age data
        :param out_prefix: the encoded data will be located in sample_dict[f"{out_prefix}.{data_type}"]
        """

        # Encode anatom site into a one-hot vector of length 9
        # 8 anatom sites and 1 for N/A
        if "site" in self._items_to_encode:
            site = sample_dict[key_site]
            site_one_hot = np.zeros(len(ISIC.ANATOM_SITE_INDEX))
            if site in ISIC.ANATOM_SITE_INDEX:
                site_one_hot[ISIC.ANATOM_SITE_INDEX[site]] = 1

            sample_dict[f"{out_prefix}.site"] = site_one_hot

        # Encode sex into a one-hot vector of length 3
        # male, female, N/A
        if "sex" in self._items_to_encode:
            sex = sample_dict[key_sex]
            sex_one_hot = np.zeros(len(ISIC.SEX_INDEX))
            if sex in ISIC.SEX_INDEX:
                sex_one_hot[ISIC.SEX_INDEX[sex]] = 1

            sample_dict[f"{out_prefix}.sex"] = sex_one_hot

        # Encode age into one-hot vector 'u' with length 7 such that:
        # for i in (0, ..., 5), u[i] == 1 iff age in range (20*i, 20(i+1)),
        # and u[6] == 1 iff age has missing value (0> or 120<)
        # for examples:
        #   age 50 -> [0,0,1,0,0,0,0]
        #   age 90 -> [0,0,0,0,1,0,0]
        #   missing age -> [0,0,0,0,0,0,1]
        if "age" in self._items_to_encode:
            age = int(sample_dict[key_age])
            age_one_hot = np.zeros(7)

            encode_idx = age // 20 if (age > 0 and age < 120) else 6
            age_one_hot[encode_idx] = 1

            sample_dict[f"{out_prefix}.age"] = age_one_hot

        return sample_dict


import numpy as np
from neural_network import activate_functions
from json import loads

class Net:
    """Perceptron class"""

    def __init__(self, *args, activate_function: activate_functions.Function = activate_functions.Sigmoid,
                 file: str = None):
        """
        Initialization of perceptron class
        :param args: the number of neurons on the layers
        :param activate_function: activation function for a neuron(Sigmoid by default)
        :param file: filename with weights
        """
        self.function = activate_function
        if file:
            with open(file) as f:
                self.weights = [np.array(i) for i in loads(f.readline())]
                self.bias = [np.array(i) for i in loads(f.readline())]
        else:
            self.weights = [np.random.random((i, j)) - np.random.random((i, j)) for i, j in zip(args, args[1:])]
            self.bias = [np.random.random(i) - np.random.random(i) for i in args[1:]]

    def get(self, input: np.ndarray) -> np.ndarray:
        """
        Get the neural network result for input
        :param input: input of net
        :return: result of net
        """
        for weights, bias in zip(self.weights, self.bias):
            input = self.function.activate(input @ weights + bias)
        return input

    def train(self, input: np.ndarray, output: np.ndarray, k: float = 1.0):
        """
        Error back propagation method
        :param input: input of net
        :param output: correct result for input
        :param k: coefficient of training
        :return: None
        """
        z = []
        f = []
        for weights, bias in zip(self.weights, self.bias):
            f.append(input)
            input = input @ weights + bias
            z.append(input)
            input = self.function.activate(input)
        diff = []
        error = (input - output).reshape(1, input.shape[0])
        for i, (Z, F) in enumerate(zip(z[::-1], f[::-1])):
            error *= self.function.derivative(Z)
            diff.append(k * F.reshape(F.shape[0], 1) @ error)
            self.bias[-i - 1] -= k * error[0]
            error = error @ self.weights[-i - 1].T
        for i, dif in enumerate(diff[::-1]):
            self.weights[i] -= dif

    def save(self, name):
        """
        Save weights to file {name}.net
        :param name: filename
        :return: None
        """
        with open(f'{name}.net', 'w') as f:
            f.write(str([[list(w) for w in weight] for weight in self.weights]))
            f.write('\n')
            f.write(str([list(i) for i in self.bias]))


from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('home/', views.home, name='home'),
    path('logout/',views.logout_request, name='logout' ),
    path('billing/', views.billing, name='billing'),
    path('billingselect/', views.billingselect, name='billingselect'),
    path('billpayment/', views.billpayment, name='billpayment'),
    path('billpaymentselect/', views.billpaymentselect, name='billpaymentselect'),
    path('billselect/', views.billselect, name='billselect'),
    path('calculate/', views.calculate, name='calculate'),
    path('view/<int:id>', views.view, name='view'),
    path('edit/<int:pk>', views.edit, name='edit'),
    path('delete/<int:id>', views.delete, name='delete'),
    path('adddatabase/', views.adddatabase, name='adddatabase'),
    path('searchbase/', views.searchbase, name='searchbase'),
    path('searchbase/search/', views.search, name='search'),
    path('notification/', views.notification, name='notification'),
    ]


from django import forms
from .models import Plant, PlantDifficulty, PlantType

class PlantForm(forms.ModelForm):
    class Meta:
        model = Plant
        fields = "__all__"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        plant_difficulties = PlantDifficulty.objects.all()
        plant_types = PlantType.objects.all()
        difficulty_names = [(c.id, c.name) for c in plant_difficulties]
        types_names = [(c.id, c.name) for c in plant_types]

        self.fields["plant_difficulty"].choices = difficulty_names
        self.fields["plant_type"].choices = types_names
        for field_name, field in self.fields.items():
            field.widget.attrs["class"] = "border-black rounded-1"


if __name__ == "__main__":
    # %%
    # Load data and show distribution of activities
    # and importing the libraries
    #
    import pandas as pd
    import numpy as np
    from sklearn.neighbors import KNeighborsClassifier
    from sklearn.metrics import confusion_matrix
    import tools
    import plots

    train = pd.read_csv("data/train.csv")
    activities = train["Activity"].value_counts()
    activities = {
        "index": activities.index,
        "count": activities.values
    }

    plots.activities_distribution(activities)
    x_train = train.drop(["subject", "Activity"], axis=1).values
    y_train = train.Activity
    # Rename the activities to numbers
    labels = {
        'LAYING': 0,
        'SITTING': 1,
        'STANDING': 2,
        'WALKING': 3,
        'WALKING_DOWNSTAIRS': 4,
        'WALKING_UPSTAIRS': 5
    }
    y_train = y_train.map(labels)

    # %%
    # [task] Apply PCA to the data and visualize the results
    #
    n_eigenvectors = 154
    pca_proj = tools.PCA(x_train, n_eigenvectors)
    pca_data = np.matmul(x_train, pca_proj.T)
    # Plot the first three principal components
    plots.scatter_with_labels(pca_data, y_train, list(labels.keys()))

    # %%
    # [task] Apply LDA to the data and visualize the results
    #
    lda_proj = tools.LDA(pca_data, y_train, n_classes=6)
    lda_data = np.matmul(pca_data, lda_proj.T)
    plots.scatter_with_labels(lda_data, y_train, list(labels.keys()))

    # %%
    # [task] Use k-nearest neighbors to predict the activity of the test dataset
    #
    # 1. Create a KNN classifier
    knn = KNeighborsClassifier(n_neighbors=20)

    # 2. Fit the classifier to the training data
    knn.fit(lda_data, y_train)

    # %%
    # [task] Use the classifier to predict the activity of the test dataset
    test = pd.read_csv("data/test.csv")
    x_test = test.drop(["subject", "Activity"], axis=1).values
    y_test = test.Activity.map(labels).values
    x_test = np.matmul(x_test, pca_proj.T)
    x_test = np.matmul(x_test, lda_proj.T)
    y_pred = knn.predict(x_test)
    # Show the confusion matrix
    plots.confusion_matrix(confusion_matrix(y_test, y_pred))
    # Show the accuracy of the classifier
    print("Accuracy:", knn.score(x_test, y_test))


import dataclasses
import marshal
from enum import IntEnum, auto
from itertools import count
from os.path import join
from typing import Callable, Dict, Tuple

from traitlets import Any

from library.sio import FileWrapper, read_int, BufferType

MARSHAL_VERSION = 4
ENCODING = 'utf-8'

size_settings = {
    'size': 4,
    'byteorder': 'big',
    'signed': False,
}

data_type_settings = {
    'size': 1,
    'byteorder': 'big',
    'signed': False,
}

file_index_settings = {
    'size': 8,
    'byteorder': 'big',
    'signed': False,
}

class DataType(IntEnum):
    FUNCTION = auto()
    TITLE = auto()
    GET_REQUEST = auto()

@dataclasses.dataclass(init=True, repr=True, eq=False)
class ReadWriteWrapper:
    __slots__ = 'read', 'write'
    read: Callable[[FileWrapper], Any]
    write: Callable[[FileWrapper, Any], int]

KeyType = Tuple[DataType, bytes]

class CacheFolder:
    __slots__ = '_root_path', '_container_name'

    def __init__(self, root_path, container_name='container.bin'):
        self._root_path = root_path
        self._container_name = container_name

    def __read_container(self):
        path = join(self._root_path, self._container_name)
        data_dict = {}
        with FileWrapper.open(path, 'rb') as wrapper:
            data_size = wrapper.read_int(**size_settings)  # data size
            for step in data_size:
                data_type = DataType(read_int(**data_type_settings))  # data type
                data = wrapper.read_big_bytes()  # data
                file_index = wrapper.read_int(**file_index_settings)  # file index
                # update data_dict
                data_dict[(data_type, data)] = file_index
        return data_dict

    def __write_container(self, data_dict: Dict):
        path = join(self._root_path, self._container_name)
        with FileWrapper.open(path, 'wb') as wrapper:
            total = wrapper.write_int(len(data_dict), **size_settings)  # data size
            for key, value in data_dict.items():
                total += wrapper.write_int(key[0], **data_type_settings)  # data type
                total += wrapper.write_big_bytes(key[1])  # data
                total += wrapper.write_big_bytes(value)  # file index
        return total

    def __format_path(self, index: int):
        return join(self._root_path, f'{hex(index)[2:]}.data')

    def __read_file_index(self, index: int):
        path = self.__format_path(index)
        with open(path, 'rb') as infile:
            return infile.read()

    def __write_file_index(self, index: int, buffer: BufferType):
        path = self.__format_path(index)
        with open(path, 'wb') as outfile:
            return outfile.write(buffer)

    def __read_file_index_call_back(self, index: int, rw: ReadWriteWrapper):
        path = self.__format_path(index)
        with FileWrapper.open(path, 'rb') as wrapper:
            return rw.read(wrapper)

    def __write_file_index_call_back(self, index: int, rw: ReadWriteWrapper, instance: Any):
        path = self.__format_path(index)
        with FileWrapper.open(path, 'rb') as wrapper:
            return rw.write(wrapper, instance)

    @staticmethod
    def __search_new_index(data_dict: Dict):
        for data_index, index in zip(data_dict.values(), count()):
            if data_index != index:
                return index
        return 0

    def __write_new_index_and_container(self, data_dict: Dict, key: KeyType, buffer: BufferType):
        new_index = data_dict[key] = CacheFolder.__search_new_index(data_dict)
        self.__write_file_index(new_index, buffer)
        self.__write_container(data_dict)

    def __write_new_index_and_container_call_back(self, data_dict: Dict, key: KeyType, rw: ReadWriteWrapper, instance):
        new_index = data_dict[key] = CacheFolder.__search_new_index(data_dict)
        self.__write_file_index_call_back(new_index, rw, instance)
        self.__write_container(data_dict)

    def cached_call(self, rw: ReadWriteWrapper = None):
        def __decorator(func: Callable):
            def __wrapper(*args, **kwargs):
                func_codes = marshal.dumps(func, MARSHAL_VERSION)
                data_dict = self.__read_container()
                key = (DataType.FUNCTION, func_codes)
                try:
                    index = data_dict[key]
                except KeyError:
                    instance = func(*args, **kwargs)
                    if isinstance(instance, (bytes, bytearray)):
                        self.__write_new_index_and_container(data_dict, key, instance)
                    elif ReadWriteWrapper is None:
                        raise ValueError('for non-bytes return values you need to pass `rw` argument')
                    else:
                        self.__write_new_index_and_container_call_back(data_dict, key, rw, instance)
                    return instance
                return self.__read_file_index_call_back(index, rw)

            return __wrapper

        return __decorator

    def cache_title(self, title: str, buffer: BufferType):
        data_dict = self.__read_container()
        key = (DataType.TITLE, title.encode(ENCODING))
        self.__write_new_index_and_container(data_dict, key, buffer)


import gym
from gym import spaces
import numpy as np
from enum import Enum

class Action(Enum):
    RIGHT = 0
    UP = 1
    LEFT = 2
    DOWN = 3
    NOOP = 4

class WarehouseEnv(gym.Env):
    def __init__(self, obstacle_map, agent_map):
        super().__init__()
        assert obstacle_map.size == agent_map.size

        self.agent_state = {}
        self.agent_goal = {}
        rows, cols = np.nonzero(agent_map)
        for i, (row, col) in enumerate(zip(rows, cols)):
            self.agent_state[i] = (row, col)
            self.agent_goal[i] = None

        self.n_agents = len(self.agent_state)
        self.obstacle_map = obstacle_map
        self.agent_map = agent_map
        self.goal_map = np.zeros_like(agent_map)
        self.action_space = spaces.Discrete(5)

    def _observe(self, agent):
        goal = self.agent_goal[agent]
        state = self.agent_state[agent]

        agent_channel = np.zeros_like(self.agent_map)
        agent_channel[state] = 1
        other_agent_channel = np.zeros_like(self.agent_map)
        for k, v in self.agent_state.items():
            if k != agent:
                other_agent_channel[v] = 1
        goal_channel = np.zeros_like(self.goal_map)
        goal_channel[goal] = 1
        other_goal_channel = np.zeros_like(self.goal_map)
        for k, v in self.agent_goal.items():
            if k != agent:
                other_goal_channel[v] = 1
        return np.stack(
            [agent_channel, other_agent_channel, goal_channel, other_goal_channel],
            axis=0,
        )

    def _occupied(self, row, col):
        if self.obstacle_map[row, col] == 1:
            return True
        for _, v in self.agent_state.items():
            if v == (row, col):
                return True
        return False

    def assign_goal(self, agent, goal):
        self.agent_goal[agent] = goal

    def step(self, agent, action):
        row, col = self.agent_state[agent]
        R, C = self.agent_map.shape
        if action == Action.RIGHT:
            s_prime = row, min(col + 1, C)
        elif action == Action.UP:
            s_prime = max(0, row - 1), col
        elif action == Action.LEFT:
            s_prime = row, max(0, col - 1)
        elif action == Action.DOWN:
            s_prime = min(row + 1, R), col
        elif action == Action.NOOP:
            s_prime = row, col
        else:
            raise ValueError("Invalid action.")
        if not self._occupied(*s_prime):
            self.agent_state[agent] = s_prime
        observation = self._observe(agent)
        reward = 0
        if self.agent_state[agent] == self.agent_goal[agent]:
            reward = 1
            self.agent_goal[agent] = None
        done = False
        return observation, reward, done, {}

    def reset(self):
        self.agent_state = {}
        self.agent_goal = {}
        for i, (row, col) in enumerate(zip(np.nonzero(self.agent_map))):
            self.agent_state[i] = (row, col)
            self.agent_goal[i] = None
        return self._observe()

    def render(self, mode="human"):
        pass

    def close(self):
        pass


# @Author: Huang Sizhe
# @Date:   10-Apr-2017
# @Email:  hsz1273327@gmail.com
# @Last modified by:   Huang Sizhe
# @Last modified time: 10-Apr-2017
# @License: MIT

""" To run this example you need additional aioredis package
"""
from sanic import Sanic, response
from sanic.response import json, text
# import aioredis
from sanic_redis import Broadcast
import time

app = Sanic('redis_channel_test')

Broadcast.SetConfig(app,test_channels=("redis://localhost:6379/1",True))
Broadcast(app)

thread = None

def my_handler(x):
    print("my_handler")
    print(x)

@app.listener("before_server_start")
async def sub(app, loop):
    await app.channels["test_channels"].subscribe(my_handler)
    global thread
    print("befor")
    thread = app.channels["test_channels"].sub_in_thread(daemon=True)
@app.listener("before_server_stop")
async def sub_close(app, loop):
    global thread
    thread.stop()
    print("after")

@app.post("/test-my-key")
async def handle(request):
    data = request.json
    result = await app.channels["test_channels"].publish(data["msg"])
    return json({"result":'ok'})

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8000)


#!/usr/bin/python3.4
# -*- coding=utf-8 -*-
#本脚由亁颐堂现任明教教主编写，用于乾颐盾Python课程！
#教主QQ:605658506
#亁颐堂官网www.qytang.com
#乾颐盾是由亁颐堂现任明教教主开发的综合性安全课程
#包括传统网络安全（防火墙，IPS...）与Python语言和黑客渗透课程！
import sys
sys.path.append('/usr/local/lib/python3.4/dist-packages/PyQYT/ExtentionPackages')
sys.path.append('/usr/lib/python3.4/site-packages/PyQYT/ExtentionPackages')
sys.path.append('../../ExtentionPackages')

import cgi

form = cgi.FieldStorage()#读取客户输入
objname = cgi.escape(form['objname'].value)
objip = cgi.escape(form['objip'].value)
natip = cgi.escape(form['natip'].value)
tcpport = cgi.escape(form['tcpport'].value)

#objname = 'new_obj_515'
#objip = '10.1.1.155'
#natip = '202.100.1.155'
#tcpport = '80'

from PyQYT.Cisco.ASA_REST_Object_Network_Add import asa_object_network_add
from PyQYT.Cisco.ASA_REST_ACL import asa_acl
from PyQYT.Cisco.ASA_REST_NAT_Add import asa_nat_add
from io import StringIO

def print_result(final_result):
	print('Content-type:text/html\n')
	print('')
	print('<title>CGI响应测试页面</title>')
	print('')
	print('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />')
	print('')
	print('
' + final_result + '
')
	print('')

natobjname = objname + '_nat'

obj_result_final = 'init'
obj_nat_result_final = 'init'
obj_result_final = 'init'
acl_result_final = 'init'
nat_result_final = 'init'

tmp = sys.stdout
obj_result = StringIO()
sys.stdout = obj_result
asa_object_network_add('192.168.1.10','admin','cisco',objname,objip)
sys.stdout = tmp
obj_result_final = obj_result.getvalue().strip()

tmp = sys.stdout
obj_nat_result = StringIO()
sys.stdout = obj_nat_result
asa_object_network_add('192.168.1.10','admin','cisco',natobjname,natip)
sys.stdout = tmp
obj_nat_result_final = obj_nat_result.getvalue().strip()

print(obj_result_final)
print(obj_nat_result_final)
if obj_result_final == "b''" and obj_nat_result_final == "b''":
	tmp = sys.stdout
	acl_result = StringIO()
	sys.stdout = acl_result
	asa_acl('192.168.1.10','admin','cisco',1,'tcp','any',objname,int(tcpport))
	sys.stdout = tmp
	acl_result_final = acl_result.getvalue().strip()

else:
	if obj_result_final != "b''":
		final_result = "网络Object创建故障！" + obj_result_final
	else:
		final_result = "网络Object创建故障！" + obj_nat_result_final
	print_result(final_result)
	sys.exit(1)

print(acl_result_final)

if acl_result_final == "b''":
	tmp = sys.stdout
	nat_result = StringIO()
	sys.stdout = nat_result
	asa_nat_add('192.168.1.10','admin','cisco',objname,natobjname)
	sys.stdout = tmp
	nat_result_final = nat_result.getvalue().strip()

else:
	final_result = "访问控制列表创建故障！" + acl_result_final
	print_result(final_result)
	sys.exit(1)

print(nat_result_final)

if nat_result_final == "b''":
	final_result = "整个策略完全成功！"
	print_result(final_result)
	sys.exit(1)
else:
	final_result = "NAT创建故障！" + nat_result_final
	print_result(final_result)
	sys.exit(1)


from googleapiclient import discovery
import os
import logging
from logging.handlers import RotatingFileHandler
from gcp import get_key, get_projects

# logs User Accounts not part of the specified GCP Organization

if os.path.isfile(get_key()):
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = get_key()

alert = False
domain = '<example.com>'

path = os.path.expanduser('~/python-logs')
logfile = os.path.expanduser('~/python-logs/security.log')

if os.path.isdir(path):
    pass
else:
    os.mkdir(path)

logger = logging.getLogger("Rotating Log")
log_formatter = logging.Formatter('%(asctime)s\t %(levelname)s %(message)s')
logger.setLevel(logging.INFO)
handler = RotatingFileHandler(logfile, maxBytes=5*1024*1024, backupCount=5)
handler.setFormatter(log_formatter)
logger.addHandler(handler)

logger.info('-----Checking for non-organizational accounts in IAM-----')
for project in get_projects():
    user_list = []
    try:
        service = discovery.build('cloudresourcemanager', 'v1')
        request = service.projects().getIamPolicy(resource=project, body={})
        response = request.execute()
        bindings = response['bindings']

        for binding in bindings:
            for member in binding['members']:
                if member.startswith('user:') and domain not in member:
                    alert = True
                    if member not in user_list:
                        logger.warning(' Project "{0}" contains non-organizational account "{1}"'.format(project,
                                                                                member))
                        user_list.append(member)
                    else:
                        pass

    except KeyError as err:
        logger.info('No User Accounts found in project "{0}": {1}'.format(project, err))

    except Exception as err:
        logger.error(err)

if alert is False:
    logger.info('No non-organizational users found')