from django.core.paginator import Paginator, EmptyPage
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseNotFound
from .models import User, Account

def index(request):
	users = User.objects.all().order_by('id')
	limit = 20

	page = request.GET.get('page', 1)
	paginator = Paginator(users, limit)
	paginator.baseurl = '/shop/?page='	#

	try:
		page = paginator.page(page)					# Page
	except EmptyPage:						# Exception as e:
		page = paginator.page(paginator.num_pages)

	return render(request, 'index.html', {
		'list_users' : page.object_list,
		'paginator' : paginator,
		'page' : page,
		})

def user_page(request, pk):
	user = get_object_or_404(User, pk=pk)
	users_accounts = Account.objects.all().filter(user_account=user)

	query_name = request.GET.get('query')						# Выбираем из reques'a запрос (Имя пользователя)
	list_found_users = User.objects.filter(name=query_name)		# <QuerySet [<User: name>]>

	not_found = ''
	if query_name and not list_found_users:
		not_found = 'Такого пользователя не существует. Уточните поиск.'

	list_accounts = []

	for users in list_found_users:
		list_accounts.append(users.account_set.all())

#	print('\n\n')
#	print(list_accounts)
##	print(list_accounts[0][0], list_accounts[0][1])
##	print(list_accounts[1])
#	print('\n\n')

	return render(request, 'user_detail.html', {
		'user' : user,
		'users_accounts' : users_accounts,

		'list_found_users' : list_found_users,
		'not_found' : not_found
		})

def accounts_page(request):
	pk_list = request.GET.getlist("accounts")
	print('\n\n   pk_list = {}   \n\n'.format(pk_list))

	if not pk_list:
		return HttpResponseNotFound("Ни один счёт не выбран")

	accounts = []
	total_value = 0
	for pk in pk_list:
		account = get_object_or_404(Account, pk=pk)			# Выбираю первый отмеченный счёт
		accounts.append(account)							# Собирают list (список) счетов
		total_value = total_value + account.value 			# Считаю сумму на счетах

	user_id = Account.objects.get(pk=pk).user_account_id	# Получаю id user'a у которого этот аккаунт
	user = get_object_or_404(User, pk=user_id)				# На основе user_id получаю самого user'a

	return render(request, 'account_detail.html', {
		'user' : user,
		'accounts' : accounts,
		'total_value' : total_value
		})

def account_page(request, pk):
	account = get_object_or_404(Account, pk=pk)
	us_id = Account.objects.get(pk=pk).user_account_id
	user = get_object_or_404(User, pk=us_id)

	return render(request, 'account_detail.html', {
		'user' : user,
		'account' : account
		})

#----------------------------------

def search_page(request):
	not_found = ''
	query = request.GET.get('q')
	list_users = User.objects.all().order_by('name')
	list_users = User.objects.filter(name=query)

	if query and not list_users:
		not_found = 'Такого пользователя не существует. Уточните поиск.'

	return render(request, 'search_page.html', {
		'list_users' : list_users,
		'not_found' : not_found
		})


import re

def solution(dartResult):
    bonus = {'S':1, 'D':2, 'T':3}
    points = []
    for iter, (g, b, o) in enumerate(re.findall('([0-9]+)(S|D|T)(\*|\#)?', dartResult)):
        points.append(pow(int(g), bonus[b]))
        if o == '*':
            if iter == 0:
                points[0] *= 2
            else:
                points[iter] *= 2
                points[iter-1] *= 2
        elif o == '#':
            points[iter] = -points[iter]
    answer = sum(points)
    return answer


import bisect

N, K = map(int, input().split())
A = list(map(int, input().split()))

d = {}
for i, a in enumerate(A):
    if a in d:
        d[a].append(i)
    else:
        d[a] = [i]

loop_cnt = 0
sp = []
p = 0
fp = 1
top = A[0]
while True:
    loop_cnt += 1
    while fp < N:
        np = bisect.bisect_left(d[top], fp)
        if np < len(d[top]):
            p = d[top][np] + 1
            fp = p+1
            if p < N:
                top = A[p]
        else:
            break
    if p >= N:
        break
    top = A[p]
    sp.append(p)
    fp = 0

k = K % loop_cnt - 1
if k == -1:
    exit()
p = sp[k]
s = []
while p < N:
    top = A[p]
    while True:
        np = bisect.bisect_left(d[top], p+1)
        if np < len(d[top]):
            p = d[top][np] + 1
            if p < N:
                top = A[p]
        else:
            break
    if p < N:
        s.append(top)
        p += 1

print(' '.join(map(str, s)))


"""
Script automating backtest on kryll.io and sending results to an API
Made by Thomas with the help of torkium
"""
import time
import random
import sys
from selenium.webdriver.support.ui import Select
from custom.user_config import UserConfig
from custom.utilities import UtilityTools
from custom.css_const import CssConst
from custom.selenium_utilities import SeleniumUtilities
from custom.backtest_config import BacktestConfig
from custom.api import Api
from getpass import getpass

def set_input_date(start_input_date, end_input_date):
    """
    Takes a start date, end date and set them in inputs
    """
    dates_inputs = sel_tools.get_elements(css.DATES_INPUTS)
    start_input = dates_inputs[0]
    end_input = dates_inputs[1]
    sel_tools.driver.execute_script('arguments[0].removeAttribute("readonly")', start_input)
    sel_tools.driver.execute_script('arguments[0].removeAttribute("readonly")', end_input)
    start_input.clear()
    start_input.send_keys(start_input_date)
    end_input.clear()
    end_input.send_keys(end_input_date)

def exec_backtest(backtest_config):
    """
    Takes a strategy name, a strategy id, a pair, and a backtest date
    return True if no errors else return False
    """
    if not api.backtest_has_failed(backtest_config) and not api.backtest_already_did(backtest_config):
        tools.log("[ℹ] Test in progress, Please Wait...")
        # set date into input
        set_input_date(backtest_config.getStart(), backtest_config.getEnd())
        test_btn = sel_tools.get_element(css.BACKTEST_START_BTN)
        sel_tools.clean_network_calls()
        sel_tools.click_on_element(test_btn)

        error_during_backtest = sel_tools.check_error_during_backtest()
        if error_during_backtest:
            raise Exception("Error during backtest")
        sel_tools.clean_network_calls()
        hold = sel_tools.get_element_double(css.ANALYSE_TAB_HOLD)
        # click on depth analysis button
        sel_tools.click_on_element(sel_tools.get_element(css.ANALYSE_TAB_DEEP_ANALYSE_LINK))
        windows_handle = sel_tools.wait_for_windows_handle(300)
        if not windows_handle:
            tools.log("[❌] depth analysis button is break on kryll side, period canceled")
            raise Exception("depth analysis button is break on kryll side, period canceled")
        send_ok = False
        try:
            sel_tools.driver.switch_to.window(sel_tools.driver.window_handles[1])
            # wait for the advanced bt page to load
            depth_analysis_page_loaded = sel_tools.wait_for_element(css.ADVANCED_ANALYSE_TRADE, 300)
            if not depth_analysis_page_loaded:
                tools.log("[⚠] depth analysis tab seems to don't load, refresh and retry...")
                # retry
                sel_tools.refresh()
                depth_analysis_page_loaded = sel_tools.wait_for_element(css.ADVANCED_ANALYSE_TRADE, 120)
            if depth_analysis_page_loaded:
                send_ok = api.send_result(backtest_config, {"hold": hold})

        except Exception as error:
            tools.log("[❌] Sending backtest : Exception occured : " + str(error))

        if not send_ok:
            screenshot_name = "backtest_fail_deep_analysis_" + str(backtest_config.getId()) + ".png"
            sel_tools.save_screenshot(screenshot_name)
            tools.log("[❌] You can see the screenshot on this file : " + screenshot_name)
        sel_tools.driver.close()
        sel_tools.driver.switch_to.window(sel_tools.driver.window_handles[0])
        if send_ok:
            return True
        tools.log("[❌] Error during sending the result, period canceled.")
        raise Exception("Error during backtest")
    return False

def install_strat_if_needed():
    try:
        tools.log("[ℹ] Checking if strategy is installed...")
        sel_tools.get_element(css.BACKTEST_BTN)
    except Exception:
        tools.log("[ℹ] Install in progress...")
        install_btn = sel_tools.get_element(css.INSTALL_BTN)
        sel_tools.click_on_element(install_btn)
    return True

def update_strat_if_needed():
    tools.log("[ℹ] Checking if strategy needs to be updated")
    time.sleep(5)
    if "btn-warning" in sel_tools.get_element(css.UPDATE_BTN).get_attribute("class").split():
        tools.log("[ℹ] Updating the strategy...")
        update_btn = sel_tools.get_element(css.UPDATE_BTN)
        sel_tools.click_on_element(update_btn)
    else:
        tools.log("[ℹ] Strategy is up to date")

def get_recommended_pairs():
    try:
        recommended_pairs = sel_tools.get_elements(css.RECOMMEND_PAIRS)
    except Exception:
        tools.log("[⚠] No recommanded pair")
        recommended_pairs = {}
    recommended_pairs_list = []
    for i in recommended_pairs:
        recommended_pairs_list.append(i)
    return recommended_pairs_list

def get_exchanges_to_test():
    if "exchanges" in user.config_file:
        exchange_options = sel_tools.get_element(css.EXCHANGE).find_elements_by_tag_name("option")
        user_exchanges = user.config_file["exchanges"]
        final_exchanges = []
        for exchange in exchange_options:
            if exchange.text in user_exchanges:
                final_exchanges.append(exchange)
    else:
        final_exchanges = [sel_tools.get_element(css.BINANCE_EXCHANGE)]
    return final_exchanges

def get_pairs_to_test():
    recommended_pairs_list = get_recommended_pairs()
    final_pairs = []
    pairs_input = sel_tools.get_element(css.PAIRS_INPUT)
    pairs_list = pairs_input.find_elements_by_tag_name("option")
    random.shuffle(pairs_list)
    # Backtest recommended first
    for i in pairs_list:
        if i in recommended_pairs_list:
            pairs_list.remove(i)
    total_pairs_list = recommended_pairs_list + pairs_list
    for i in total_pairs_list:
        # Get infos
        pair = i.text.strip()
        recommended = 0
        force_pair = 0
        if i in recommended_pairs_list:
            recommended = 1

        if "pair" in user.config_file:
            if not pair.replace(" / ", "/") in user.config_file["pair"]:
                continue
            if pair.replace(" / ", "/") in user.config_file["pair"]:
                force_pair = 1
        if "accu" in user.config_file and not pair.split(" / ")[1] in user.config_file["accu"]:
            continue

        # check if user want to test every pairs
        if force_pair == 0 and recommended == 0 and user.config_file["every_pairs"] == "n":
            continue
        if recommended == 1 and "skip_recommended_pair" in user.config_file and user.config_file["skip_recommended_pair"] == "y" and force_pair == 0:
            continue
        final_pairs.append(i)
    return final_pairs

def select_exchange(exchange):
    sel_tools.clean_network_calls()
    exchange_text = exchange.text
    exchange_select = Select(sel_tools.get_element(css.EXCHANGE))
    exchange_select.select_by_visible_text(exchange_text)
    sel_tools.wait_network_calls_loaded(5)
    return True

def select_pair(pair):
    sel_tools.clean_network_calls()
    pair_text = pair.text
    pairs_input = Select(sel_tools.get_element(css.PAIRS_INPUT))
    pairs_input.select_by_value(pair_text.replace(" / ", "-"))
    sel_tools.wait_network_calls_loaded(5)

def is_pair_listed(pair):
    pairs_input = Select(sel_tools.get_element(css.PAIRS_INPUT))
    try:
        pairs_input.select_by_value(pair.text.strip().replace(" / ", "-"))
    except Exception:
        return False
    return True

def get_periods(backtest_config):
    dates_inputs = sel_tools.get_elements(css.DATES_INPUTS)
    start_input = dates_inputs[0]
    min_date = sel_tools.wait_for_attribute_value(start_input, "min")
    tools.log(f"[ℹ] min date : {min_date}", verbose=True)
    backtest_dates = api.get_backtest_dates(min_date=min_date, backtest_config=backtest_config)
    return backtest_dates

def click_on_backtest_button():
    backtest_page_loaded = False
    while not backtest_page_loaded:
        try:
            sel_tools.click_on_element(sel_tools.get_element(css.BACKTEST_BTN), True)
            sel_tools.get_element_text(css.STRAT_NAME_BACKTEST)
            sel_tools.get_element_text(css.STRAT_VERSION)
            backtest_page_loaded = True
        except:
            tools.log("[ℹ] White page... Retry")
            sel_tools.refresh()
            continue

def is_recommended_pair(pair):
    recommended_pairs_list = get_recommended_pairs()
    return pair in recommended_pairs_list

def run_backtest(backtest_config):
    backtest_done = False
    backtest_failed = False
    backtest_failed_screenshot = True
    retry_order_skipped = 0
    while True not in (backtest_done, backtest_failed):
        # Try to exec the backtest
        try:
            exec_backtest(backtest_config)
            backtest_done = True
        # If backtest failed, check if order too small or too many order skipped. If yes, retry x3 with more start wallet
        except:
            log = ""
            try:
                log = sel_tools.get_element_text(css.LOGS_LAST_LINE)
                if retry_order_skipped <= 3 and ("order too small" in log or "Too many orders skipped" in log):
                    backtest_failed_screenshot = False
                    retry_order_skipped = retry_order_skipped + 1
                    if retry_order_skipped > 3:
                        backtest_failed = True
                        tools.log(f"[❌] {log}")
                    else:
                        sel_tools.click_on_element(sel_tools.get_element(sel_tools.css.BALANCE_BUTTON, 2))
                        multiplicator = 1000 * retry_order_skipped
                        try:
                            amount_one_input = sel_tools.get_element(sel_tools.css.STARTING_AMOUNT_ONE)
                            amount_one = amount_one_input.get_attribute("value")
                            amount_one = float(amount_one)
                            amount_one = amount_one * multiplicator
                            amount_one_input.clear()
                            amount_one_input.send_keys(str(amount_one))
                        except:
                            pass
                        try:
                            amount_two_input = sel_tools.get_element(sel_tools.css.STARTING_AMOUNT_TWO)
                            amount_two = amount_two_input.get_attribute("value")
                            amount_two = float(amount_two)
                            amount_two = amount_two * multiplicator
                            amount_two_input.clear()
                            amount_two_input.send_keys(str(amount_two))
                        except:
                            pass
                        tools.log(f"[❌] Too many order skipped, retry with x{multiplicator} amount {retry_order_skipped}/3.")
                else:
                    backtest_failed = True
                    tools.log(f"[❌] {log}")
            except:
                backtest_failed = True
            if "missing data" in log:
                tools.log(f"[❌] {log}")
                backtest_failed_screenshot = False
                backtest_failed = True
            if backtest_failed:
                api.backtest_add_failed(backtest_config=backtest_config, log=log)
                tools.log("[❌] Backtest Failed.")
                if backtest_failed_screenshot:
                    screenshot_name = "backtest_fail_" + str(backtest_config.getId()) + ".png"
                    sel_tools.save_screenshot(screenshot_name)
                    tools.log("[❌] You can see the screenshot on this file : " + screenshot_name)

def run():
    # If more 1 tab open, closed useless tabs
    sel_tools.close_unused_tabs()
    for strat_id in strat_ids:
        backtest_config = BacktestConfig()
        backtest_config.setStratId(strat_id)
        # Go to strategy page
        tools.log("[ℹ] Go to strategy page...")
        sel_tools.get("https://platform.kryll.io/marketplace/" + strat_id)
        # Check if strategy is installed, and install it
        install_strat_if_needed()
        # Check if strategy needs to be updated
        update_strat_if_needed()
        # Click on backtest button
        tools.log("[ℹ] Click on backtest button...", True)
        click_on_backtest_button()
        # Get strategy name, strategy version
        tools.log("[ℹ] Get strategy name and version...", True)
        backtest_config.setStratName(sel_tools.get_element_text(css.STRAT_NAME_BACKTEST).strip())
        backtest_config.setStratVersion(sel_tools.get_element_text(css.STRAT_VERSION).split(" ")[1])
        # Get recommended pairs list
        tools.log("[ℹ] Get recommended pair list...", True)
        recommended_pairs = get_recommended_pairs()
        # Get Exchanges to test
        tools.log("[ℹ] Get exchanges to test...", True)
        exchanges = get_exchanges_to_test()
        # For each exchange
        for exchange in exchanges:
            backtest_config.setExchange(exchange.text)
            # Select exchange and wait network pair load
            tools.log(f"[ℹ] Select exchange {backtest_config.getExchange()}...")
            select_exchange(exchange)
            # Get pairs to test
            tools.log("[ℹ] Get pairs...", True)
            pairs = get_pairs_to_test()
            # For each pairs
            for pair in pairs:
                backtest_config.setPair(pair.text)
                backtest_config.setRecommended(pair in recommended_pairs)
                # Check if pair exist on exchange
                if not is_pair_listed(pair):
                    tools.log(f"[ℹ] Pair {backtest_config.getPair()} not listed...")
                    continue
                # Select pair wait network dates load and get min date
                tools.log(f"[ℹ] Select pair {backtest_config.getPair()}...")
                select_pair(pair)
                # Get periods to test
                tools.log("[ℹ] Get periods to test...", True)
                periods = get_periods(backtest_config)
                # For each period
                for period in periods:
                    backtest_config.setPeriod(period["period"])
                    backtest_config.setStart(period["start"])
                    backtest_config.setEnd(period["end"])
                    tools.log("[ℹ] **************************************")
                    tools.log("[ℹ] *         Running backtest           *")
                    tools.log("[ℹ] **************************************")
                    tools.log(f"[ℹ] * Strategy name : {backtest_config.getStratName()}")
                    tools.log(f"[ℹ] * Strategy version : {backtest_config.getStratVersion()}")
                    tools.log(f"[ℹ] * Exchange : {backtest_config.getExchange()}")
                    tools.log(f"[ℹ] * Backtest pair : {backtest_config.getPair()}")
                    tools.log(f"[ℹ] * Backtest period : {backtest_config.getPeriod()}")
                    tools.log(f"[ℹ] * From : {backtest_config.getStart()} to {backtest_config.getEnd()}")
                    tools.log("[ℹ] ***************************************")
                    # Run backtest
                    run_backtest(backtest_config)
        # strategy backtested, next
        tools.log("[ℹ] strategy backtested !")
    return True

# ----------------------
# Start of the program
# ----------------------
configFile = "config.yaml"
if len(sys.argv) > 1:
    configFile = sys.argv[1] + ".yaml"
    # print("Use config file " + configFile)
user = UserConfig(configFile)
tools = UtilityTools(user_config_file=user.config_file)
if configFile != "config.yaml":
    tools.log("[ℹ] Config File: " + configFile)
css = CssConst()

if "browser" in user.config_file:
    config_browser = user.config_file["browser"].lower()
else:
    config_browser = False
client_driver = tools.detect_browsers(user.config_file["headless"], config_browser)
api = Api(user_config_file=user.config_file, driver=client_driver)
sel_tools = SeleniumUtilities(user_config_file=user.config_file, driver=client_driver)
sel_tools.driver.get("https://platform.kryll.io/login")
if user.login:
    tools.log("[ℹ] Login using account :" + user.login["email"])
    sel_tools.get_element(css.EMAIL_INPUT).send_keys(user.login["email"])
    sel_tools.get_element(css.PASSWORD_INPUT).send_keys(user.login["password"])
else:
    sel_tools.get_element(css.EMAIL_INPUT).send_keys(input("email :"))
    sel_tools.get_element(css.PASSWORD_INPUT).send_keys(getpass("password :"))

if user.config_file["ask_2fa"] == "n":
    sel_tools.click_on_element(sel_tools.get_element(css.LOG_IN_BTN))
else:
    twofa = input("2FA :").lower()
    sel_tools.get_element(css.TWO_FA_INPUT).send_keys(twofa)

sel_tools.wait_for_element(css.USER_DROPDOWN, 100000)

if user.config_file["get_strategies_from_marketplace"] == "y":
    tools.log("[ℹ] Getting strategies from the marketplace...")
    sel_tools.driver.get("https://platform.kryll.io/marketplace/top")
    ids = sel_tools.get_elements(
        "div.col-sm-12 > app-card-strategy-user:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > h3:nth-child(1) > a:nth-child(1)"
    )
    strat_ids = []
    for strategy_id in ids:
        strat_ids.append(strategy_id.get_attribute("href").split("/")[4])
else:
    if "strat_ids" in user.config_file:
        strat_ids = user.config_file["strat_ids"]
    else:
        strat_ids = tools.ask_strat()

COUNT_QUICK_FAIL = 0
EXECUTION_ID = 0
while COUNT_QUICK_FAIL < 3:
    EXECUTION_ID = EXECUTION_ID + 1
    start = time.time()
    try:
        random.shuffle(strat_ids)
        run()
    except Exception as error:
        SCREENSHOT_NAME = "screen_fail_" + str(EXECUTION_ID) + ".png"
        sel_tools.save_screenshot(SCREENSHOT_NAME)
        tools.log("[❌] Exception occured on execution " + str(EXECUTION_ID) + " : " + str(error))
        tools.log("[❌] You can see the screenshot on this file : " + SCREENSHOT_NAME)
        tools.log("[❌] Retry...")
    end = time.time()
    elapsed = end - start
    if elapsed < 30:
        COUNT_QUICK_FAIL = COUNT_QUICK_FAIL + 1
        tools.log("[❌] Quick fail : " + str(COUNT_QUICK_FAIL) + "/3")
    else:
        COUNT_QUICK_FAIL = 0


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: ShenSiyuan
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn as sk
from sklearn import preprocessing
import itertools
from sklearn.preprocessing import StandardScaler
from sklearn.compose import ColumnTransformer
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.model_selection import train_test_split
from sklearn import datasets
from keras.layers import Activation, Dense
from sklearn.preprocessing import MinMaxScaler
from keras.layers import TimeDistributed, RepeatVector
import random
import seaborn as sns
import keras.utils

def import_csv(filename):
    data = pd.read_csv(filename)
    time_transformed = []
    for i in range(data.shape[0]):
        timestamp = data["Time"][i]
        time = timestamp.split(':')
        h = (int)(time[0]) * 60 * 60
        m = (int)(time[1]) * 60
        s = (float)(time[2])
        time = (h + m + s)
        time_transformed.append(time)
    data["time_transformed"] = time_transformed
    data_sorted = data.sort_values(by=['DayOfWeek', 'time_transformed'])
    data_sorted = data_sorted.reset_index(drop=True)
    return data_sorted

#data_norm = import_csv("normal_data.csv")
#data_abnormal = import_csv("abnormal_data.csv")
def pre_processing(filename, timestep = 20):
    data = pd.read_csv(filename)
    data['Motion'] = data['Motion'].map({'inactive': 0, 'active': 1})
    data['Acceleration'] = data['Acceleration'].map({'inactive': 0, 'active': 1})
    data['DayOfWeek'] = data['DayOfWeek'] / 6.0
    time_transformed = []
    for i in range(data.shape[0]):
        timestamp = data["Time"][i]
        time = timestamp.split(':')
        h = (int)(time[0]) * 60 * 60
        m = (int)(time[1]) * 60
        s = (float)(time[2])
        time = (h + m + s)/ 86400
        time_transformed.append(time)

    data["time_transformed"] = time_transformed
    data = data.drop(["Time"], axis = 1)
    data["Temperature"] = (data["Temperature"] - data["Temperature"].min()) / (data["Temperature"].max() - data["Temperature"].min())
    data_sorted = data.sort_values(by=['DayOfWeek', 'time_transformed'])
    data_sorted = data_sorted.reset_index(drop=True)
    data_input = np.asarray(data_sorted)
    X = []
    Y_labels = []
    return X, Y_labels

def pre_processing2(filename, timestep = 20, normal = True):
    data = pd.read_csv(filename)
    data['Motion'] = data['Motion'].map({'inactive': 0, 'active': 1})
    data['Acceleration'] = data['Acceleration'].map({'inactive': 0, 'active': 1})
    data['DayOfWeek'] = data['DayOfWeek'] / 6.0
    time_transformed = []
    for i in range(data.shape[0]):
        timestamp = data["Time"][i]
        time = timestamp.split(':')
        h = (int)(time[0]) * 60 * 60
        m = (int)(time[1]) * 60
        s = (float)(time[2])
        time = (h + m + s)/ 86400
        time_transformed.append(time)

    data["time_transformed"] = time_transformed
    data = data.drop(["Time"], axis = 1)
    data["Temperature"] = (data["Temperature"] - data["Temperature"].min()) / (data["Temperature"].max() - data["Temperature"].min())
    data_sorted = data.sort_values(by=['DayOfWeek', 'time_transformed'])
    data_sorted = data_sorted.reset_index(drop=True)
    data_input = np.asarray(data_sorted)
    X = []
    Y_labels = []

    for i in range(timestep - 1, data_input.shape[0] - 1 ):
        temp = []
        for j in range(timestep):
            temp.append(data_input[i-j])
        X.append(temp)
        if (normal == True):
            Y_labels.append(0)
        else:
            Y_labels.append(1)
    X = np.asarray(X)
    Y_labels = np.asarray(Y_labels)
    return X, Y_labels

# transform the features

def train_predict(X, Y_labels):
    X_train, X_test, y_train, y_test = train_test_split(X, Y_labels, test_size=0.3, random_state=0)
    model = Sequential()
    model.add(LSTM(35, input_shape=(20, 5)))
    model.add(Dense(1, activation = 'tanh'))
    model.compile(loss='mean_squared_error', optimizer='adam',metrics = ['accuracy'])
    print(model.summary())

    history = model.fit(X_train, y_train, epochs=5, batch_size=10, verbose=2, shuffle=False)
    history
    y_pred = model.predict(X_test, verbose=0)
    return y_pred

def train_predict2(X, Y_labels):
    # train test split
    X_train, X_test, y_train, y_test = train_test_split(X, Y_labels, test_size=0.2, random_state=0)
    # validation set
    model = Sequential()
    model.add(LSTM(35, input_shape=(20, 5)))
    model.add(Dense(1, activation = 'softmax'))
    model.compile(loss='binary_crossentropy', optimizer='adam',metrics = ['accuracy'])
    print(model.summary())

    history = model.fit(X_train, y_train, epochs=5, batch_size=5, verbose=2, shuffle=False)
    history
    y_pred = model.predict(X_test, verbose=0)
    return y_pred

'''
X, Y_labels = pre_processing()
y_pred = train_predict(X, Y_labels)
X = np.concatenate((X_norm, X_abnormal), axis=0)
Y = Y_norm + Y_abnormal
'''

def visualize_mse(MSEs_normal, MSEs_abnormal):
    #one distribution and one scatter plot
    index_normal = list(range(len(MSEs_normal)))
    index_abnormal = list(range(len(MSEs_abnormal)))
    plt.scatter(index_normal, MSEs_normal, color = "blue", alpha = "0.5", s = 5)
    plt.scatter(index_abnormal, MSEs_abnormal, color = "orange", alpha = "0.5", s = 5)
    plt.title("mse vs. time (blue: normal, orange: abnormal)")
    plt.show()
    sns.distplot(MSEs_normal, hist=False, rug=True)
    sns.distplot(MSEs_abnormal, hist=False, rug=True)
    plt.title("distribution of MSE for normal and abnormal data")
    plt.show()
    return 0

def visualize_input(norm, abnormal):
    index_normal = list(range(norm.shape[0]))
    plt.plot(index_normal, norm[:,0])
    plt.title("normal dataset feature 1")
    plt.show()
    index_abnormal = list(range(abnormal.shape[0]))

    plt.plot(index_abnormal, abnormal[:,0])
    plt.xlim(right = norm.shape[0])
    plt.title("abnormal dataset feature 1")
    plt.show()

    plt.plot(index_normal, norm[:,1])
    plt.title("normal dataset feature 2")
    plt.show()
    plt.plot(index_abnormal, abnormal[:,1])
    plt.xlim(right = norm.shape[0])
    plt.title("abnormal dataset feature 2")
    plt.show()

    plt.plot(index_normal, norm[:,2])
    plt.title("normal dataset feature 3")
    plt.show()
    plt.plot(index_abnormal, abnormal[:,2])
    plt.xlim(right = norm.shape[0])
    plt.title("abnormal dataset feature 3")
    plt.show()

    plt.plot(index_normal, norm[:,3])
    plt.title("normal dataset feature 4")
    plt.show()
    plt.plot(index_abnormal, abnormal[:,3])
    plt.xlim(right = norm.shape[0])
    plt.title("abnormal dataset feature 4")
    plt.show()

    plt.plot(index_normal, norm[:,4])
    plt.title("normal dataset feature 5")
    plt.show()
    plt.plot(index_abnormal, abnormal[:,4])
    plt.xlim(right = norm.shape[0])
    plt.title("abnormal dataset feature 5")
    plt.show()
    return 0

'''
split the normal data into training and validation sets,
and the abnormal data as testing set
'''
X_norm, Y_norm = pre_processing2("normal_data.csv", 20)
# validation set
X_abnormal, Y_abnormal = pre_processing2("abnormal_data.csv", 20)
### train validation split
X_train, X_vali, y_train, y_vali = train_test_split(X_norm, Y_norm, test_size=0.2, random_state=0)

visualize_input(Y_norm, Y_abnormal)

model = Sequential()
model.add(LSTM(64, activation='relu', return_sequences=True, input_shape=(20,5)))
model.add(LSTM(32, activation='relu'))
model.add(RepeatVector(20))
model.add(LSTM(32, activation='relu', return_sequences = True))
model.add(LSTM(64, activation='relu', return_sequences = True))
model.add(TimeDistributed(Dense(5)))

print(model.summary())
model.compile(loss='mse', optimizer='adam')
history = model.fit(X_train, X_train, epochs=10, validation_data=(X_vali, X_vali), batch_size=32, verbose=1, shuffle=True)
history
X_vali_pred = model.predict(X_vali, verbose=0)

'''
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
lstm_14 (LSTM)               (None, 20, 128)           68608
_________________________________________________________________
lstm_15 (LSTM)               (None, 20, 128)           131584
_________________________________________________________________
lstm_16 (LSTM)               (None, 64)                49408
_________________________________________________________________
dense_6 (Dense)              (None, 5)                 325
=================================================================
Total params: 249,925
Trainable params: 249,925
Non-trainable params: 0
_________________________________________________________________
None
Train on 79984 samples, validate on 19996 samples
'''

## Plot the graph of training and validation loss
plt.plot(history.history['loss'], linewidth=2, label='Train')
plt.plot(history.history['val_loss'], linewidth=2, label='Validation')
plt.legend(loc='upper right')
plt.show()

def flatten(X):
    '''
    Flatten a 3D array.

    Input
    X            A 3D array for lstm, where the array is sample x timesteps x features.

    Output
    flattened_X  A 2D array, sample x features.
    '''
    flattened_X = np.empty((X.shape[0], X.shape[2]))  # sample x features array.
    for i in range(X.shape[0]):
        flattened_X[i] = X[i, (X.shape[1]-1), :]
    return(flattened_X)

#### using mean square error to set a threshold for testing
#### the percentile is set to  here
mse_list = []
for i in range(len(X_vali_pred)):
    mse = np.mean(np.square(np.subtract(X_vali_pred[19], X_vali[19])))
    mse_list.append(mse)
    #mse_list.append(np.mean(np.square(np.subtract(y_vali_pred[i], y_vali[i]))))
threshold = np.percentile(mse_list, 95)
print("mse threshold is: ", threshold)

t = 0
abnormal_mse = []
X_abnormal_pred = model.predict(X_abnormal)
pred_binary = []
for i in range(len(X_abnormal)):
  mse = np.mean(np.square(np.subtract(X_abnormal_pred[19], X_abnormal[19])))
  if mse < threshold:

    pred_binary.append(0)
  else:
    t = t + 1
    pred_binary.append(1)
  abnormal_mse.append(mse)

print("number of samples predicted to be normal in abnormal data: ",len(X_abnormal)- t)
print("number of abnormal data: ", len(X_abnormal))
print("abnormality accuracy():",  t/ len(X_abnormal))

visualize_mse(mse_list, abnormal_mse)

(len(X_vali_pred) * 0.95 + t) / (len(X_vali_pred) + len(X_abnormal_pred))

(len(X_vali_pred) * 0.95 + len(X_abnormal_pred) * 0.83) / (len(X_vali_pred) + len(X_abnormal_pred))

'''
Suppose positive = abnormal   negative = normal

When the threshold percentile is set to 95% (false positive = 0.05),
the abnormality accuracy (true positive) is 0.8314
mse threshold is:  0.3038325941684792
number of samples predicted to be normal in abnormal data:  1665
number of abnormal data:  9881
abnormality accuracy(true positive rate): 0.8314947879769254

when set the threshold percentile to be 93% (false positive =  0.07),
we get abnormality accuracy (true positive) of 0.9086124886145127
mse threshold is:  0.20700430233242026
number of samples predicted to be normal in abnormal data:  903
number of abnormal data:  9881
abnormality accuracy(): 0.9086124886145127

when set the threshold percentile to be 90% (false positive =  0.1),
we get abnormality accuracy (true positive) of 0.942212
threshold is:  0.1613987153072845
number of samples predicted to be normal in abnormal data:  571
number of abnormal data:  9881
abnormality accuracy(): 0.9422123266875823

'''

'''
generate the ROC curve
'''
true_positive_rate = []
false_positive_rate = []
thresholds = []
for i in range(0, 100, 1):
  threshold = np.percentile(mse_list, i)
  t = 0
  for mse in abnormal_mse :
    if mse < threshold:
      t = t + 1

  true_positive_rate.append((len(y_abnormal) - t) / len(y_abnormal))
  false_positive_rate.append((100- i)/100)
  thresholds.append(threshold)

print("true positive rate: \n", true_positive_rate)
print("\n")
print("false positive rate: \n ", false_positive_rate)

plt.plot(false_positive_rate, true_positive_rate)
plt.xlabel("false_positive_rate")
plt.ylabel("true_positive_rate")
plt.title("ROC curve")
plt.show()

##### prediction with a mixture of normal and abnormal data

from scipy.stats import truncnorm

def get_truncated_normal(mean=0, sd=1, low=0, upp=10):
    return truncnorm(
        (low - mean) / sd, (upp - mean) / sd, loc=mean, scale=sd)

##### generate testing data by inserting abnormal data into normal behavior
##### data

#

index = 0
final_dayofweek = data_norm.iloc[-1]["DayOfWeek"]
final_time_transformed = data_norm.iloc[-1]["time_transformed"]
data_mixed = data_norm
labels= [1] * data_mixed.shape[0]
data_mixed["label"] = labels
data_abnormal_with_labels = data_abnormal
labels= [0] * data_abnormal.shape[0]
data_abnormal_with_labels["label"] = labels
data_norm_shape = data_norm.shape[0]
data_append = pd.DataFrame(columns = data_mixed.columns)
while (index < data_mixed.shape[0]):
    X = get_truncated_normal(mean = 30, sd = 20, low = 5, upp = 130)
    rand_int = int(X.rvs())
    if (index + rand_int < data_mixed.shape[0]):
        dayofweek = data_mixed.iloc[index]["DayOfWeek"]
        time_transformed = data_mixed.iloc[index]["time_transformed"]
        dayofweek2 = data_mixed.iloc[index+rand_int]["DayOfWeek"]
        time_transformed2 = data_mixed.iloc[index+rand_int]["time_transformed"]
        data_mixed = data_mixed.drop(data_mixed.index[index: index+rand_int])
        abnormal = data_abnormal_with_labels[(data_abnormal_with_labels["DayOfWeek"] == dayofweek) & (data_abnormal_with_labels["time_transformed"] > time_transformed) & (data_abnormal_with_labels["time_transformed"]< time_transformed2)]
        data_append = data_append.append(abnormal)
        index = index + rand_int
    else:
        break


#!/usr/bin/env python3
from pidog import Pidog
from time import sleep
from preset_actions import shake_head

my_dog = Pidog()
sleep(0.1)

def loop_around(amplitude=60, interval=0.5, speed=100):
    my_dog.head_move([[amplitude,0,0]], immediately=True, speed=speed)
    my_dog.wait_all_done()
    sleep(interval)
    my_dog.head_move([[-amplitude,0,0]], immediately=True, speed=speed)
    my_dog.wait_all_done()
    sleep(interval)
    my_dog.head_move([[0,0,0]], immediately=True, speed=speed)
    my_dog.wait_all_done()

def is_sound():
    if my_dog.ears.isdetected():
        direction = my_dog.ears.read()
        if direction != 0:
            return True
        else:
            return False
    else:
        return False

def rest():
    my_dog.wait_all_done()
    my_dog.do_action('lie', speed=50)
    my_dog.wait_all_done()

    while True:
        # Sleeping
        my_dog.rgb_strip.set_mode('breath', 'pink', bps=0.3)
        my_dog.head_move([[0,0,-40]], immediately=True, speed=5)
        my_dog.do_action('doze_off', speed=92)
        # Cleanup sound detection
        sleep(1)
        is_sound()

        # keep sleeping
        while is_sound() is False:
            my_dog.do_action('doze_off', speed=92)
            sleep(0.2)

        # If heard anything, wake up
        # Set light to yellow and stand up
        my_dog.rgb_strip.set_mode('boom', 'yellow', bps=1)
        my_dog.body_stop()
        my_dog.do_action('stand', speed=90)
        my_dog.head_move([[0, 0, 0]], immediately=True, speed=80)
        my_dog.wait_all_done()
        # Look arround
        loop_around(60, 1, 60)
        sleep(0.5)
        # tilt head and being confused
        my_dog.speak('confused_3', volume=80)
        my_dog.do_action('tilting_head_left', speed=80)
        my_dog.wait_all_done()
        sleep(1.2)
        my_dog.head_move([[0, 0, -10]], immediately=True, speed=80)
        my_dog.wait_all_done()
        sleep(0.4)
        # Shake head , mean to ignore it
        shake_head(my_dog)
        sleep(0.2)

        # Lay down again
        my_dog.rgb_strip.set_mode('breath', 'pink', bps=1)
        my_dog.do_action('lie', speed=50)
        my_dog.wait_all_done()
        sleep(1)

if __name__ == "__main__":
    try:
        rest()
    except KeyboardInterrupt:
        pass
    except Exception as e:
        print(f"\033[31mERROR: {e}\033[m")
    finally:
        my_dog.close()


# Title:  Python Script Sample To Manage BPS VE Licenses.
# Actions:
#   1. Login to BPS box
#   2. Get a list of the configured license servers
#   3. Add a specific license server if is not already added
#   4. Check the active licenses on the specific server
#   5. Activate deactivate licenses
#   6. Check floating license feature statistics (check how many are available and taken)

#================

########################################
import sys
import os
# Add bps_restpy libpath *required if the library is not installed
libpath = os.path.abspath(__file__+"/../../..")
sys.path.insert(0, libpath)
import logging

from restPyWrapper import BPS, pp
from bpsVELicense import BPSVELicenseManagement
########################################

########################################
# Demo script global variables
########################################
# bps system info
bps_system  = '<BPS_BOX_IP/HOSTNAME>'
bpsuser     = 'bps user'
bpspass     = 'bps pass'

mainLicenseServer = '<License_Server_IP/HOSTNAME>'
activationCode = '<Activation_Code>'

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

########################################
# Login to BPS box
bps = BPS(bps_system, bpsuser, bpspass)
bps.login()

# create an instance of the BPSVELicenseManagement admin class
licenseMngr = BPSVELicenseManagement(bps)

# list license servers added
licenseServers = licenseMngr.getLicenseServers()
print ("The following license servers are added: " + str(licenseServers))

# add desired server if not already there
if not mainLicenseServer in [item['host'] for item in licenseServers if 'host' in item]:
    licenseMngr.addLicenseServer(mainLicenseServer)

# identify active server, if is not the one desired set it
activeServer = ''
for server in licenseServers:
    if 'isActive' in server and server['isActive']:
        activeServer = server

if not activeServer == mainLicenseServer:
    licenseMngr.setLicenseServerActive(mainLicenseServer)

# identify active server and store the details in  activeServer
for server in licenseServers:
    if 'isActive' in server and server['isActive']:
        activeServer = server

# extracting the id from the activeServer dictionary. We'll  use the id to identify teh server
licenseServerId = activeServer['id']
print("Server ID: %s"%licenseServerId)

# get active licenses on the active server
serverActiveLicenses = licenseMngr.retrieveLicenses(activeServer['id'])
print("The following licenses are active: " + str(serverActiveLicenses))

# is online activation possible?
status = licenseMngr.isServerOnline(activeServer['id'])
if not 'canConnectToIxiaBackend' in status or not status['canConnectToIxiaBackend']:
    print ("This server requires offline activation/deactivation process.\
         Please open license administration and follow the steps indicated.")
    print ("The rest of the steps are only aplicable to online servers. Existing...")

# retrieve information about an activation code
activationCodeStatus = licenseMngr.retrieveActivationCodeInfo(licenseServerId, activationCode)
print ("Activation code description and status: "+str(activationCodeStatus))

# activate the code
activationResult = licenseMngr.activateActivationCode(licenseServerId, activationCode, quantity=2)
print ("Activation result: "+str(activationCodeStatus))

serverActiveLicenses = licenseMngr.retrieveLicenses(activeServer['id'])
print("The following licenses are active: " + str(serverActiveLicenses))

# deactivate the code
activationResult = licenseMngr.activateActivationCode(licenseServerId, activationCode, quantity=2)
print ("Activation result: "+str(activationCodeStatus))

# disable logging
logger = logging.getLogger('bps_restpy')
logger.setLevel(logging.CRITICAL)

serverActiveLicenses = licenseMngr.retrieveLicenses(activeServer['id'])
print("The following licenses are active: " + str(serverActiveLicenses))

# retrieve license statistics. The amount of licenses used and available
licenseCountStats = licenseMngr.retrieveCountedFeatureStats(activeServer['id'])
print("The license statistics: " + str(licenseCountStats))


def check_string(valid, question):
    answer = input(question).lower()
    if answer == "x":
        return "quit"
    for item in valid.keys():
        if answer in valid[item]:
            return item
    return "Not valid"

snacks_dict = {"popcorn": ["popcorn", "corn", "1", "p"],
               "m&ms": ["m&ms", "mms", "m", "2"],
               "pita chips": ["pita chips", "chips", "pc", "pita", "c", "3"],
               "water": ["water", "w", "4"]}
while True:
    snack = check_string(snacks_dict, "What snack would you like? ")
    if snack == "quit":
        quit()
    else:
        print(snack)


'''
K-Means聚类
1、	确定K值，即将数据聚集成K个类簇或小组
2、	从数据集中随机选择K个数据点作为质心（Centroid）或数据中心
3、	分别计算每个点到每个质心之间的距离，并将每个点划分到离最近质心的小组。
4、	当每个质心都聚集了一些点后，重新定义算法选出新的质心。（对于每个簇，计算其均值，即得到新的K个质心点）
5、	迭代执行第三步到第四步，直到迭代终止条件满足为止（聚类结果不再变化）
'''

from sklearn.cluster import KMeans

"""
第一部分：数据集
X表示二维矩阵数据，篮球运动员比赛数据
总共20行，每行两列数据
第一列表示玩家每局击杀数：assault_per_minute
第二列表示玩家每局死亡数：dead_per_minute
第三列表示玩家每局助攻数：assists_per_minute
"""
X = [[6,11,13],
     [10,11,10],
     [4,8,19],
     [9,11,10],
     [0,11,17],
     [6,4,24],
     [16,9,19],
     [8,6,29],
     [14,5,21],
     [7,5,24]
     ]

print(X)

#第二部分：KMeans聚类
clf = KMeans(n_clusters=3,n_init='auto')                                           # clf即赋值为KMeans；n_clusters=3) 表示类簇数为3，聚成3类数据；n_init='auto'是将KMeans改为自动运行模式，随着KM1.4版本更新，这样就能避免掉Futurewarning的报错
y_pred = clf.fit_predict(X)                                           # 载入数据集X，并且将聚类的结果赋值给y_pred

print(clf)                                                            # 输出完整Kmeans函数

print("y_pred = ", y_pred)                                            # 输出聚类预测结果

#第三部分：可视化绘图

import numpy as np
import matplotlib.pyplot as plt

x = [n[0] for n in X]                                                # 获取数据集的第一列//第三列 使用for循环获取 n[0]表示X第一列
print(x)
y = [n[2] for n in X]                                                # n[0]表示X的第三列
print(y)

'''
绘制散点图
参数：x横轴; y纵轴; c=y_pred聚类预测结果; marker类型:o表示圆点,*表示星型,x表示点;
'''
plt.scatter(x, y ,c=y_pred, marker='x')

plt.title("Kmeans-Gameplayer Data")                                      # 绘制标题

plt.xlabel("assault_per_minute")                                         # 绘制x轴和y轴坐标
plt.ylabel("assists_per_minute")

plt.legend(["A", "B", "C"])                                              # 设置右上角图例

plt.show()                                                               # 显示图形

from warnings import simplefilter

simplefilter("ignore", category=FutureWarning)


# -*- coding: utf-8 -*-
# © <YEAR(S)> <AUTHOR(S)>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import _, api, fields, models

class ResCurrency(models.Model):
    _inherit = 'res.currency'

    def _get_current_rate(self, cr, uid, ids, name, arg, context=None):
        if context is None:
            context = {}
        res = {}

        date = context.get('date') or fields.Datetime.now()
        company_id = context.get('company_id') or self.pool[
            'res.users']._get_company(cr, uid, context=context)
        for id in ids:
            cr.execute("""SELECT rate FROM res_currency_rate
                           WHERE currency_id = %s
                             AND CAST(name AS DATE) <= %s
                             AND (company_id is null
                                 OR company_id = %s)
                        ORDER BY company_id, name desc LIMIT 1""",
                       (id, date, company_id))
            if cr.rowcount:
                res[id] = cr.fetchone()[0]
            else:
                res[id] = 1
        return res

    def _get_conversion_rate(self, cr, uid, from_currency,
                             to_currency, context=None):
        if context is None:
            context = {}
        ctx = context.copy()
        if 'date' in ctx.keys():
            if ctx['date']:
                if len(ctx['date']) < 11:
                    ctx['date'] = ctx['date'] + ' 23:00:00'
        from_currency = self.browse(cr, uid, from_currency.id, context=ctx)
        to_currency = self.browse(cr, uid, to_currency.id, context=ctx)
        return to_currency.rate / from_currency.rate

class ResCurrencyRate(models.Model):
    _inherit = 'res.currency.rate'

    rate_mex = fields.Float(
        string=_('Rate mexico'),
        digits=(12, 6),
    )

    @api.onchange('rate_mex')
    def _onchange_rate_mex(self):
        if self.rate_mex != 0.00:
            self.rate = 1 / self.rate_mex
        else:
            self.rate = 0.00


# -*- coding: UTF-8 -*-
# !/usr/bin/python
# @time     :2020/4/30 20:51
# @author   :Mo
# @function :graph of base

# from macadam.base.preprocess import ListGenerator, FileGenerator, ListPrerocessXY, Tokenizer4Macadam
# from macadam.conf.path_config import path_model, path_model_dir, path_fineture, path_model_info
from macadam.base.utils import load_json, save_json, txt_read, txt_write
from macadam.conf.constant_params import CLS, SEP, PAD, UNK, MASK
from macadam.conf.constant_params import SL, TC, RE
from macadam.conf.constant_params import Config
from macadam.conf.logger_config import logger
from macadam import keras, K, L, M, O, C
from macadam import __version__
## optimizers
# from bert4keras.optimizers import extend_with_gradient_accumulation
# from bert4keras.optimizers import extend_with_piecewise_linear_lr
# from bert4keras.optimizers import extend_with_layer_adaptation
# from bert4keras.optimizers import extend_with_lazy_optimization
# from bert4keras.optimizers import extend_with_weight_decay
# from bert4keras.optimizers import extend_with_lookahead
# from bert4keras.optimizers import AdaFactor
# from bert4keras.optimizers import Adam
## common
import numpy as np
import os

class graph(Config):
    def __init__(self, hyper_parameters):
        self.model = None
        self.trans = None
        super().__init__(hyper_parameters)

    def build_model(self, inputs, outputs):
        self.model = None

    def create_compile(self):
        """
          构建优化器、损失函数和评价函数
        :return:
        """
        # optimizer_ = Adam if self.optimizer.upper() == "ADAM" else AdaFactor
        # if "gradient_accumulation" in self.optimizer_extend:
        #     optimizer_ = extend_with_gradient_accumulation(optimizer_)
        # if "piecewise_linear_lr" in self.optimizer_extend:
        #     optimizer_ = extend_with_piecewise_linear_lr(optimizer_)
        # if "layer_adaptation" in self.optimizer_extend:
        #     optimizer_ = extend_with_layer_adaptation(optimizer_)
        # if "lazy_optimization" in self.optimizer_extend:
        #     optimizer_ = extend_with_lazy_optimization(optimizer_)
        # if "weight_decay" in self.optimizer_extend:
        #     optimizer_ = extend_with_weight_decay(optimizer_)
        # if "lookahead" in self.optimizer_extend:
        #     optimizer_ = extend_with_lookahead(optimizer_)

        self.model.compile(optimizer=O.Adam(lr=self.learning_rate, beta_1=0.9, beta_2=0.999, decay=0.0),
                           loss=self.loss,
                           metrics=[self.metrics])

    def callback(self, monitor="val_loss"):
        """
          评价函数、早停
        :return:
        """
        cb_em = [C.TensorBoard(log_dir=os.path.join(self.path_model_dir, "logs"), batch_size=self.batch_size, update_freq='batch'),
                 C.EarlyStopping(monitor=monitor, mode="auto", min_delta=1e-9, patience=self.early_stop),
                 C.ModelCheckpoint(monitor=monitor, mode="auto", filepath=os.path.join(self.path_model_dir, "macadam.h5"),
                                   verbose=1, save_best_only=True, save_weights_only=False)]
        return cb_em

    def fit(self, preprocess_xy, generator_xy, train_data, dev_data=None, rate=1.0):
        """
        fit_generator, 迭代器训练
        Args:
            preprocess_xy: class, alreadly init
            generator_xy: class, not init
            train_data: List or path
            dev_data: List or path
        Returns:
            None
        """
        # 保存所有需要的参数, 包括macadam版本/模型标签字典
        self.hyper_parameters["train"]["is_training"] = False  # 预测时候这些设为False
        self.hyper_parameters["train"]["trainable"] = False
        self.hyper_parameters["graph"]["dropout"] = 0
        # 保存的超参数, info
        model_info = {"__class__": self.__class__.__name__,
                      "__version__": __version__,
                      "hyper_parameters": self.hyper_parameters,
                      "label": {"l2i": preprocess_xy.l2i,
                                "i2l": preprocess_xy.i2l},
                      "vocab": {"token2idx": preprocess_xy.embedding.token2idx
                                },
                      }
        save_json(lines=model_info, path=os.path.join(self.path_model_dir, "macadam.info"))
        # 训练数据集的数据条数
        len_train = preprocess_xy.analysis_len_data(train_data)
        len_train = int(len_train * rate)
        lg_train = generator_xy(train_data, preprocess_xy,
                                batch_size=self.batch_size, len_data=len_train)
        lg_dev = None
        # monitor是早停和保存模型的依据, "loss", "acc", "val_loss", "val_acc"等
        monitor = "val_loss"
        if dev_data:
            len_dev = preprocess_xy.analysis_len_data(dev_data)
            len_dev= int(len_dev * rate)
            lg_dev = generator_xy(dev_data, preprocess_xy,
                                  batch_size=self.batch_size, len_data=len_dev)
        else:
            monitor = "loss"
        # 训练模型
        self.model.fit_generator(generator=lg_train.forfit(),
                                 steps_per_epoch=lg_train.__len__(),
                                 callbacks=self.callback(monitor),
                                 epochs=self.epochs,
                                 validation_data=lg_dev.forfit() if lg_dev else None,
                                 validation_steps=lg_dev.__len__() if lg_dev else None,
                                 )
        # 保存crf状态转移矩阵
        if self.use_crf and self.trans:
            model_info = {"__class__": self.__class__.__name__,
                          "__version__": __version__,
                          "hyper_parameters": self.hyper_parameters,
                          "label": {"l2i": preprocess_xy.l2i,
                                    "i2l": preprocess_xy.i2l},
                          SL: {"trans": self.trans},
                          "vocab": {"token2idx": preprocess_xy.embedding.token2idx},
                          }
            save_json(lines=model_info, path=os.path.join(self.path_model_dir, "macadam.info"))

    def add_metrics(self, metrics):
        self.metrics = metrics

    def add_loss(self, loss):
        self.loss = loss


# csv 파일 크롤링하기

#ihongss.com/csv/exam1.csv

import csv
import pymongo

conn = pymongo.MongoClient('192.168.99.100', 32766)
db = conn.get_database("db1") # db1생성
coll = db.get_collection("exam1") # 테이블 생성

f = open('./resources/exam1.csv', 'r', encoding="utf-8")
rdr = csv.reader(f)
next(rdr, None) # 컬럼 skip

for line in rdr:
    print(type(line))
    print(line)
    dict1 = dict()
    dict1[line[0]] = line
    coll.insert_one(dict1)

conn.close
# DB에 넣는 방법


# Import folder where sorting algorithms
import sys
import unittest

# For importing from different folders
# OBS: This is supposed to be done with automated testing, hence relative to folder we want to import from
sys.path.append("Algorithms/graphtheory/kahns-toposort/")

# If run from local:
# sys.path.append('../../Algorithms/graphtheory/kahns-toposort')

from kahn_topological_ordering import topological_ordering
from collections import defaultdict

class test_TopologicalOrdering(unittest.TestCase):
    def setUp(self):
        self.G1 = {}
        self.degree_incoming1 = defaultdict(int, {})
        self.correct_isDAG1 = False
        self.correct_path1 = []

        self.G2 = {"1": ["2"], "2": ["3"], "3": ["4"], "4": ["5"], "5": []}
        self.degree_incoming2 = defaultdict(int, {"2": 1, "3": 1, "4": 1, "5": 1})
        self.correct_isDAG2 = True
        self.correct_path2 = ["1", "2", "3", "4", "5"]

        self.G3 = {
            "1": ["2", "3", "4", "5"],
            "2": ["3", "4", "5"],
            "3": ["4", "5"],
            "4": ["5"],
            "5": [],
        }
        self.degree_incoming3 = defaultdict(int, {"2": 1, "3": 2, "4": 3, "5": 4})
        self.correct_isDAG3 = True
        self.correct_path3 = ["1", "2", "3", "4", "5"]

        self.G4 = {
            "1": ["2", "3", "4", "5"],
            "2": ["3", "4", "5"],
            "3": ["2", "4", "5"],
            "4": ["5"],
            "5": [],
        }
        self.degree_incoming4 = defaultdict(int, {"2": 2, "3": 2, "4": 3, "5": 4})
        self.correct_isDAG4 = False
        self.correct_path4 = []

    def test_emptygraph(self):
        path_to_take, is_DAG = topological_ordering(self.G1, self.degree_incoming1)
        self.assertEqual(path_to_take, self.correct_path1)
        self.assertEqual(is_DAG, self.correct_isDAG1)

    def test_clear_ordering(self):
        path_to_take, is_DAG = topological_ordering(self.G2, self.degree_incoming2)
        self.assertEqual(path_to_take, self.correct_path2)
        self.assertEqual(is_DAG, self.correct_isDAG2)

    def test_more_complicated_graph(self):
        path_to_take, is_DAG = topological_ordering(self.G3, self.degree_incoming3)
        self.assertEqual(path_to_take, self.correct_path3)
        self.assertEqual(is_DAG, self.correct_isDAG3)

    def test_no_topological_ordering(self):
        path_to_take, is_DAG = topological_ordering(self.G4, self.degree_incoming4)
        self.assertEqual(path_to_take, self.correct_path4)
        self.assertEqual(is_DAG, self.correct_isDAG4)

if __name__ == "__main__":
    print("Running Topological Ordering tests:")
    unittest.main()


from flask import Flask, render_template, request, make_response
app = Flask(__name__)

@app.route('/cookie')
def index():
   return render_template('cookie_index.html')

@app.route('/setcookie', methods = ['POST', 'GET'])
def setcookie():
    if request.method == 'POST':
        user = request.form['nm']

        #get response object from return value of a view function
        resp = make_response(render_template('readcookie.html',cook =user))
        #sets a Cookie name userID and form value.
        resp.set_cookie('userID', user)

        return resp

@app.route('/getcookie')
def getcookie():
   #reads back and displays the cookie value in browser
   name = request.cookies.get('userID')
   return '
welcome '+name+'
'

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


from django.urls import path
from . import views

urlpatterns = [
    path('', views.cart, name='cart'),
    path('add_to_cart', views.add_to_cart, name='add_to_cart'),
    path('remove_item', views.remove_item, name='remove_item'),
    path('check_out', views.check_out, name='check_out'),
    path('customr_details', views.customer_details, name='customer_details'),
    path('clear_cart/<int:cart_id>', views.clear_cart, name='clear_cart'),

]


from urllib.request import urlopen
import time
import json
import os
import re

from plant_fba.core.fetch_plantseed_impl import FetchPlantSEEDImpl

PS_url = 'https://raw.githubusercontent.com/ModelSEED/PlantSEED/'
PS_tag = 'v3.0.0'

# These should be retrieved from the Template data
Template_Compartment_Mapping={'c':'cytosol', 'g':'golgi', 'w':'cellwall',
                              'n':'nucleus', 'r':'endoplasm',
                              'v':'vacuole', 'cv':'vacuole',
                              'd':'plastid', 'cd':'plastid',
                              'm':'mitochondria','cm':'mitochondria',
                              'mj':'mitointer', 'ce':'extracellular',
                              'x':'peroxisome', 'cx':'peroxisome',
                              'e':'extracellular','de':'plastid'}

class GenerateTableImpl:

    def log(self,message, prefix_newline=False):
        time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(time.time()))
        print(('\n' if prefix_newline else '') + time_str + ': ' + message)

    def __init__(self):
        pass

    def generate_table(self, reactions, complexes=None):

        html_lines=list()
        html_lines.append('
')

        header_list = ["Reaction","Compartment","Roles","Enzymes","EC numbers","Subsystems","Classes"]

        html_lines.append('
')
        internal_header_line = "	".join(header_list)
        html_lines.append('
'+internal_header_line+'
')
        html_lines.append('
')

        html_lines.append("
")

        #######################################################################
        # Each row in the table is unique to the reaction/compartment combination
        for reaction in sorted(reactions.keys()):
            for compartment in reactions[reaction]['compartments']:
                compartment_str = Template_Compartment_Mapping[compartment]

                compartmentalized_complexes=list()

                #######################################################################
                # The code below is taken from the code for generating a ModelTemplate
                # It identifies the single letter reaction id for transport reactions
                # Accordingly, the compartments should all be sorted
                # So a compartment index of 0 matches the first position in the compartment list
                # The order is curated in the PlantSEED database

                reaction_cpt_id = compartment

                # If its a transporter, need to update the reaction compartment id
                if(len(compartment)==2):

                    # The rule is that it is always the non-cytosolic compartment
                    if('c' in compartment):
                        for cpt in compartment:
                            if(cpt != 'c'):
                                reaction_cpt_id = cpt

                    # With two main exceptions:
                    # 1) whether its an extracellular transporter
                    if('e' in compartment):
                        for cpt in compartment:
                            if(cpt != 'e'):
                                reaction_cpt_id = cpt

                    # 2) whether its an intraorganellar transporter
                    if('j' in compartment):
                        reaction_cpt_id = 'j'

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

                model_reaction_id = reaction+"_"+reaction_cpt_id+"0"
                if(complexes is not None and \
                       model_reaction_id in complexes and \
                       complexes[model_reaction_id] not in compartmentalized_complexes):
                    compartmentalized_complexes.append(complexes[model_reaction_id])

                complexes_str="; ".join(compartmentalized_complexes)
                roles_str="; ".join(sorted(reactions[reaction]['roles']))
                ecs_str="; ".join(sorted(reactions[reaction]['ecs']))
                classes_str="; ".join(sorted(reactions[reaction]['classes']))

                subsystems_str ="; ".join(sorted(reactions[reaction]['subsystems']))
                subsystems_str = subsystems_str.replace("_in_plants","")
                subsystems_str = subsystems_str.replace("_"," ")

                html_lines.append("
")
                internal_row_line = "	".join([reaction,compartment_str,roles_str,
                                                      complexes_str,ecs_str,subsystems_str,classes_str])
                html_lines.append(""+internal_row_line+"	")
                html_lines.append("
")

        html_lines.append("
")
        html_lines.append("
")

        return "\n".join(html_lines)

def main():

    # Load these directly from PlantSEED_Roles.json
    PS_Roles = json.load(urlopen(PS_url+PS_tag+'/Data/PlantSEED_v3/PlantSEED_Roles.json'))

    plantseed = FetchPlantSEEDImpl()
    reactions_data = plantseed.fetch_reactions(PS_Roles)

    table = GenerateTableImpl()
    table_html_string = table.generate_table(reactions_data)

    with open(os.path.join('../../../data','app_report_templates',
                           'integrate_abundances_report_tables_template.html')) as report_template_file:
        report_template_string = report_template_file.read()

    # Insert html table
    table_report_string = report_template_string.replace('*TABLES*', table_html_string)

    table_html_file="annotation_table_output.html"
    with open(os.path.join('../../../data','app_report_templates',
                           table_html_file),'w') as table_file:
        table_file.write(table_report_string)

    table.log(message="Table written to file: "+table_html_file)

if(__name__ == "__main__"):
    main()


"""
Encapsulates ground truth.
"""

import csv
from os.path import basename

import numpy as np

BPM_LO = 30.
BPM_HIGH = 285.
BPM_VALUES=BPM_HIGH - BPM_LO + 1.
LOG2_RANGE = np.log2(BPM_HIGH / BPM_LO)

def linear_to_label(index, nb_classes):
    return BPM_VALUES * index / nb_classes + BPM_LO

def linear_to_index(label, nb_classes):
    return round((label - BPM_LO) * nb_classes / BPM_VALUES)

def log_to_label(index, nb_classes):
    factor = (nb_classes-1) / LOG2_RANGE
    return BPM_LO * np.power(2., index/factor)

def log_to_index(label, nb_classes):
    if label == 0:
        return 0.
    factor = (nb_classes-1) / LOG2_RANGE
    return round(factor * np.log2(label/BPM_LO))

class GroundTruth:
    """
    Tempo ground truth.
    """

    def __init__(self, file, nb_classes=256, log_scale=False) -> None:
        """
        Creates a ground truth based on the given ``.tsv`` file. The file must contain the columns
        id, BPM.

        :param file: tsv file
        :param nb_classes: number of classes
        :param log_scale: use log scale (otherwise linear scale)
        """
        super().__init__()
        self.file = file
        self.nb_classes = nb_classes
        self.labels = {}
        self.labels.update(self._read_label_file(file))
        self.name = basename(file).replace('.tsv', '')
        self.log_scale = log_scale
        if log_scale:
            self.to_index = log_to_index
            self.to_label = log_to_label
        else:
            self.to_index = linear_to_index
            self.to_label = linear_to_label

    def __len__(self):
        return len(self.labels)

    def classes(self):
        return [i for i in range(self.nb_classes)]

    def get_label(self, index):
        if index < 0 or index >= self.nb_classes:
            return None
        return self.to_label(index, self.nb_classes)

    def get_index(self, label):
        index = self.to_index(label, self.nb_classes)
        # enforce valid range
        if index < 0:
            return 0
        elif index >= self.nb_classes:
            return self.nb_classes-1
        else:
            return index

    def get_index_for_key(self, key, scale_factor=1.):
        if scale_factor is None:
            scale_factor = 1.
        label = self.labels[key]
        if label is None:
            return None
        else:
            return self.get_index(label*scale_factor)

    def _read_label_file(self, file):
        labels = {}
        with open(file, mode='r', encoding='utf-8') as text_file:
            reader = csv.reader(text_file, delimiter='\t')
            for row in reader:
                id = row[0]
                bpm = float(row[1])
                labels[id] = bpm
        return labels


class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        if not digits:
            return []
        nb = digits[-1]
        if nb != 9:
            digits[-1] += 1
        else:
            retained = True
            for i in range(len(digits) - 1, -1, -1):
                digits[i] += retained
                retained = False
                if digits[i] == 10:
                    digits[i] = 0
                    retained = True
            if retained == True:
                digits.insert(0, 1)
        return digits


import sys
from heapq import nlargest

sys.stdin=open('input.txt')
input=sys.stdin.readline

D = {}  # D[pid] = [salary, C, J, P]

for _ in range(int(input())):
    cmd = input().split()
    cmd[1:] = map(int, cmd[1:])
    #print(cmd)
    if cmd[0]=='register':
        D[cmd[1]] = cmd[2:]
        #print(cmd[1], D[cmd[1]])

    elif cmd[0]=='cancel':
        if cmd[1] in D: del D[cmd[1]]

    elif cmd[0]=='update':
        pid, flag, X = cmd[1:]
        if pid in D:
            D[pid][flag+1] = X

    elif cmd[0]=='hire_min':
        pid = min(D.items(), key=lambda I : (I[1][0],I[0]))[0]    # (pid, [salary, C, J, P])
        #pid = min(D.keys(), key= lambda pid : (D[pid][0], pid))
        print(pid)
        del D[pid]

    else:
        pids = nlargest(3,D.keys(),key=lambda pid: (sum(D[pid][1:]), pid))
        #items = nlargest(3, D.items(), key=lambda I: (sum(I[1][1:]), I[0]))
        #pids = list(map(lambda I: I[0], items))
        print(*pids)
        for pid in pids: del D[pid]


from pymavlink import mavutil
from pymavlink.dialects.v20 import common as mavlink
import struct
import array
import time
import os
import sys
import pickle
import numpy as np
import matplotlib.pyplot as plt
import VFControl
import utm
import copy
ignore_list = ["ATTITUDE_QUATERNION", "HIGHRES_IMU", "ATTITUDE", "GLOBAL_POSITION_INT", "LOCAL_POSITION_NED",
               "POSITION_TARGET_LOCAL_NED", "HIGHRES_IMU", "ATTITUDE_TARGET", "POSITION_TARGET_GLOBAL_INT", "VFR_HUD",
               "SYS_STATUS", "BATTERY_STATUS", "HEARTBEAT", "GPS_RAW_INT", "ALTITUDE", "WIND_COV", "EXTENDED_SYS_STATE",
               "ESTIMATOR_STATUS", "VIBRATION", "HOME_POSITION","SERVO_OUTPUT_RAW"]

target_system = 1  # 255
target_component = 0  # 0
mission_type = 0

def LoadWPFile(filename):
    fileread = open(filename, 'rb')
    wpCountMsg = pickle.load(fileread)
    wpList = []
    for i in range(0, wpCountMsg.count):
        wpList.append(pickle.load(fileread))

    fileread.close()
    return wpList

def GetWPList(mav, savefile):
    mav.mav.mission_request_list_send(target_system, target_component, mission_type)

    if (savefile is not None):
        fileout = open(savefile, 'wb')

    num_wp = -1
    print("Requesting WP List")
    while True:
        msg = mav.recv_msg()
        if msg is None:
            continue
        skip_msg = False
        for strnames in ignore_list:
            if (msg.name is strnames):
                skip_msg = True
        if (skip_msg is True):
            continue

        if msg.name is "MISSION_COUNT":
            num_wp = msg.count
            print("#WP:" + str(num_wp))
            if (num_wp == 0):
                mav.mav.mission_ack_send(target_system, target_component,
                                         type=0)  # , mission_type=0)#type=msg.seq, mission_type=0)
                break
            else:
                mav.mav.mission_request_send(target_system, target_component, seq=0)  # , mission_type=0)
            if (savefile is not None):
                pickle.dump(msg, fileout)
        elif msg.name is "MISSION_ITEM":
            print(msg)
            if (savefile is not None):
                pickle.dump(msg, fileout)

            if (msg.seq >= 0 and msg.seq + 1 < num_wp):
                print("--> REQ # " + str(msg.seq))
                mav.mav.mission_request_send(target_system, target_component, seq=msg.seq + 1)  # , mission_type=0)
            elif (msg.seq + 1 == num_wp):
                mav.mav.mission_ack_send(target_system, target_component,
                                         type=0)  # , mission_type=0)#type=msg.seq, mission_type=0)
                if (savefile is not None):
                    fileout.close()
                break
        elif msg.name is "MISSION_CURRENT":
            # print(msg)
            i = 1
        else:
            print(msg)

def WriteWPtoMAV(mavlink, WPmsg):
    count = len(WPmsg)
    target_component = 0  # 0

    mav.mav.mission_count_send(target_system, target_component, count)
    print("Send count " + str(count))
    MAV_MISSION_ACCEPTED = 0  # mission accepted OK
    seqNUM = 0
    while True:
        msg = mav.recv_msg()
        if msg is None:
            continue
        skip_msg = False
        for strnames in ignore_list:
            if (msg.name is strnames):
                skip_msg = True
        if (skip_msg is True):
            continue
        if msg.name is "MISSION_ACK":
            print(msg.name + "->GOOD")
            break
        elif msg.name is "MISSION_REQUEST":
            print(msg.name)
            # mav.mav.mission_ack_send(target_system, target_component, type=MAV_MISSION_ACCEPTED)  # , mission_type=0)#type=msg.seq, mission_type=0)
            msg = WPmsg[seqNUM]

            # if (msg.command == 22):
            #    msg.x = globalpos.lat / 1e7
            #    msg.y = globalpos.lon / 1e7
            #    msg.z = globalpos.alt / 1000

            # target_system = msg.target_system
            target_component = msg.target_component
            seq = seqNUM
            frame = msg.frame
            # if(SendFile=='Set2'):
            #    command = 16
            # else:
            command = msg.command
            current = msg.current
            autocontinue = msg.autocontinue
            param1 = msg.param1
            param2 = msg.param2
            param3 = msg.param3
            param4 = msg.param4
            x = msg.x
            y = msg.y
            z = 10  # msg.z
            print("Send SEQ " + str(seqNUM) + " " + str(msg))
            mav.mav.mission_item_send(target_system, target_component, seq, frame, command, current,
                                      autocontinue, param1, param2,
                                      param3, param4, x, y, z)
            seqNUM = seqNUM + 1

        elif msg.name is "STATUSTEXT":
            print(msg.name + " " + str(msg.text))
            break
        elif msg.name is "MISSION_CURRENT":
            # print(msg)
            z = 1
        else:
            print(msg.name)

def GetHomePositionWait(mav):
    return GetMessageData(mav, "HOME_POSITION")

def GetCurrentGlobalPositionWait(mav):
    return GetMessageData(mav, "GLOBAL_POSITION_INT")

def GetCurrentLocalPositionWait(mav):
    # print("Waiting on Local Position")
    while True:
        msg = mav.recv_msg()
        if msg is None:
            continue

        if msg.name is "LOCAL_POSITION_NED":
            return msg

def GetPositionTargetLocalNEDWait(mav):
    return GetMessageData(mav, 'POSITION_TARGET_LOCAL_NED')

def GetAttitudeWait(mav):
    return GetMessageData(mav, 'ATTITUDE')

def GetMessageData(mav, msg_name):
    while True:
        msg = mav.recv_msg()
        if msg is None:
            continue

        if msg.name is msg_name:
            return msg

mav = mavutil.mavlink_connection('udpin::14552')
# mav = mavutil.mavlink_connection("COM7",baud=115200)
print(mav.address)
print('wait')
mav.wait_heartbeat()
print('got beat')

# SET_POSITION_TARGET_LOCAL_NED
# set_position_target_local_ned_send(self, time_boot_ms, target_system, target_component, coordinate_frame, type_mask, x,
#                                   y, z, vx, vy, vz, afx, afy, afz, yaw, yaw_rate, force_mavlink1=False):

# mav.mav.set_position_target_local_ned_send()
dt = 0.01
VFTarget = [500, 500]
cvf = VFControl.CircleVectorField('Gradient')
cvf.mCircleRadius = 20
cvf.xc = VFTarget[0]
cvf.yc = VFTarget[1]
# cvf.bUsePathFunc = True
# cvf.velPathFunc = expPathFunc
# MAV_FRAME_LOCAL_NED

# Save the WP (not used)
#GetWPList(mav,'twoWPtemplate.pickl')
#sys.exit(1)
wpBlank = LoadWPFile('twoWPtemplate.pickl')

HP = GetHomePositionWait(mav)
print(HP)
CGP = GetCurrentGlobalPositionWait(mav)
print(CGP)
# WPList = GetWPList(mav)
plt.figure()
plt.ion()
uav = VFControl.VFUAV(dt)
BasePosLatLon = GetCurrentGlobalPositionWait(mav)

while True:
    CHP = GetHomePositionWait(mav)
    CLP = GetCurrentLocalPositionWait(mav)
    CGP = GetCurrentGlobalPositionWait(mav)

    ATT = GetAttitudeWait(mav)

    # print(CLP)
    plt.cla()
    plt.scatter(CLP.x, CLP.y)
    TargetLocal = GetPositionTargetLocalNEDWait(mav)

    # plt.scatter(TargetLocal.x,TargetLocal.y)
    # uav.SetPosition([CLP.x,CLP.y])
    curr_theta = ATT.yaw  # np.arctan2(CLP.vy,CLP.vx)

    new_vector = {'pos': [CLP.x, CLP.y], 'theta': curr_theta, 'velocity_vec': [CLP.vx, CLP.vy]}
    dir_theta = uav.ExportNewTurnAngleFromVF(new_vector, cvf, t=0)
    new_arm = 25.0  # m

    target_x = CLP.x + new_arm * np.cos(dir_theta)
    target_y = CLP.y + new_arm * np.sin(dir_theta)
    print("Now: " + str(curr_theta) + ' [' + str(CLP.x) + ', ' + str(CLP.y) + ']')
    print("New: " + str(dir_theta) + ' [' + str(target_x) + ', ' + str(target_y) + ']')

    vf = cvf.GetVF_XYUV(0, dt, uav, IncludeUAVPos=True)
    Q = plt.quiver(vf['x'], vf['y'], vf['u'], vf['v'])

    plt.scatter(target_x, target_y, marker='x')

    circle1 = plt.Circle((vf['xc'], vf['yc']), cvf.mCircleRadius, color='b', fill=False)
    ax = plt.gca()

    ax.add_artist(circle1)
    ax.add_artist(plt.scatter(vf['xc'], vf['yc']))
    plt.scatter(cvf.xc, cvf.yc)
    plt.axis('equal')
    plt.pause(0.000000000001)

    #continue #use to avoid waypoint changes

    # get vehicle XYZ (on earth)

    vlat = BasePosLatLon.lat / 1e7
    vlon = BasePosLatLon.lon / 1e7

    Gxy = utm.from_latlon(vlat, vlon)

    x = CLP.x
    y = CLP.y
    z = CLP.z

    #test for same coor
    #CPLL = utm.to_latlon(x + Gxy[0], y + Gxy[1], Gxy[2], Gxy[3])
    #actual data needed
    #CPLL = utm.to_latlon(Gxy[0], Gxy[1], Gxy[2], Gxy[3])

    CTLL = utm.to_latlon(target_x + Gxy[0], target_y + Gxy[1], Gxy[2], Gxy[3])
    CirCenter = utm.to_latlon(cvf.xc + Gxy[0], cvf.yc + Gxy[1], Gxy[2], Gxy[3])

    print("Base Pos",vlat, vlon)
    print("My Pos", CGP.lat/1e7, CGP.lon/1e7)
    print("Target Pos",CTLL[0],CTLL[1])
    print("Circ Pos",CirCenter[0],CirCenter[1])

    #print(CPLL[0], CPLL[1])

    wpBlank = LoadWPFile('twoWPtemplate.pickl')
    wpOut = wpBlank[:]
    wpOut = []
    wpOut.append([])
    wpOut[0] = copy.deepcopy(wpBlank[0])
    wpOut.append([])
    wpOut[1] = copy.deepcopy(wpBlank[1])
    wpOut.append([])
    wpOut[2] = copy.deepcopy(wpOut[1])
    #print(str(len(wpOut))+"#######################################"+str(len(wpBlank)))
    # sys.exit(1)
    wpOut[0].x = CGP.lat/1e7#CPLL[0]
    wpOut[0].y = CGP.lon/1e7#CPLL[1]
    wpOut[0].z = CLP.z
    wpOut[0].current = 0

    # wpOut[0].frame = 1 #MAV_FRAME_LOCAL_NED

    wpOut[1].x = CTLL[0]
    wpOut[1].y = CTLL[1]
    wpOut[1].z = CLP.z
    wpOut[1].seq = 1
    wpOut[1].current = 1

    # wpOut[1].frame = 1

    #wpOut.append([])
    #wpOut[2] = copy.deepcopy(wpOut[1])
    wpOut[2].x = CirCenter[0]
    wpOut[2].y = CirCenter[1]
    wpOut[2].z = CLP.z
    wpOut[2].seq = 2
    wpOut[2].current = 0

    WriteWPtoMAV(mav, wpOut)
    print('Sent WPs')
    plt.pause(2.0)

sys.exit(1)


# -*- coding: utf-8 -*-
"""some helper functions """
import csv
import numpy as np

def load_data(data_path, sub_sample=False):
    """
    Loads data and returns a matrix with 1000 rows corresponding to items and 10 000 colums to users.
    """
    X = np.zeros((1000,10000))
    ids= np.genfromtxt(data_path, delimiter =",",skip_header=1, dtype=str,usecols=0)
    users=np.zeros(ids.shape[0])
    items=np.zeros(ids.shape[0])
    rates= np.genfromtxt(data_path, delimiter =",",skip_header=1, dtype=int,usecols=1)

    for i in range (ids.shape[0]) :
        users[i] = (ids[i])[1:ids[i].find("_")]
        items[i] = (ids[i])[ids[i].find("c")+1:]

    for i in range(rates.shape[0]) :
        X[int(items[i])-1,int(users[i])-1]=rates[i]

    return X

def create_csv_submission(ids, pred, name):
    """
    Creates an output file in csv format for submission to kaggle
    Arguments: ids (event ids associated with each rating prediction (user and item))
               pred (predicted rating)
               name (string name of .csv output file to be created)
    """
    with open(name, 'w') as csvfile:
        fieldnames = ['Id', 'Prediction']
        writer = csv.DictWriter(csvfile, delimiter=",", fieldnames=fieldnames)
        writer.writeheader()
        for r1, r2 in zip(ids, pred):
            writer.writerow({'Id':int(r1),'Prediction':int(r2)})


# https://leetcode.com/problems/moving-average-from-data-stream/description/

"""
Given a stream of integers and a window size,
calculate the moving average of all integers in the sliding window.

Implement the MovingAverage class:
    * MovingAverage(int size) Initializes the object with the size of the window size.

    * double next(int val) Returns the moving average of the last size values of the stream.

"""

from collections import deque
from utils import display_func_value

class MovingAverage:

    def __init__(self, size: int):
        self.current_ma_value = 0
        self.data = deque()
        self.size = size

    @display_func_value
    def next(self, val: int) -> float:
        self.data.appendleft(val)
        self.current_ma_value += val

        # достигли размера окна (size) => нужно удалить самый первый элемент очереди
        if len(self.data) > self.size:
            self.current_ma_value -= self.data.pop()

        return self.current_ma_value / min(len(self.data), self.size)

movingAverage = MovingAverage(3)
movingAverage.next(1)  # return 1.0 = 1 / 1
movingAverage.next(10) # return 5.5 = (1 + 10) / 2
movingAverage.next(3)  # return 4.66667 = (1 + 10 + 3) / 3
movingAverage.next(5)  # return 6.0 = (10 + 3 + 5) / 3

Problems Solutions/346. Moving Average from Data Stream.py