import numpy as np
import sys
from decimal import *

'''
Naming convention
Files ending without _ are my files
Files ending with _ are Sneh's files
'''

'''
Without DP
Hold out and cross validation
vip_cleaned.csv
vip_cleaned_.csv

With DP
Hold out and cross validation
vip_cleaned_dp_0_1.csv
vip_cleaned_dp_1.csv
vip_cleaned_dp_2.csv
vip_cleaned_dp_0_1_.csv
vip_cleaned_dp_1_.csv
vip_cleaned_dp_2_.csv
'''
vip_filename = 'vip_files/vip_cleaned_dp_2_.csv'

vip_const_filename = 'vip_files/vip_cleaned_.csv'

d1_filename = 'd1_cleaned.csv'
idp_filename = 'idp_cleaned.csv'

'''
Without DP
Hold out
prediction_results.txt
prediction_results_.txt
Cross validation
prediction_results_2.txt
prediction_results_2_.txt

With DP (0.1, 1, 2)
Hold out
prediction_results_dp_0_1.txt
prediction_results_dp_1.txt
prediction_results_dp_2.txt
prediction_results_dp_0_1_.txt
prediction_results_dp_1_.txt
prediction_results_dp_2_.txt
Cross validation
prediction_results_2_dp_0_1.txt
prediction_results_2_dp_1.txt
prediction_results_2_dp_2.txt
prediction_results_2_dp_0_1_.txt
prediction_results_2_dp_1_.txt
prediction_results_2_dp_2_.txt
'''
prediction_results_filename = 'results_neural_network/prediction_results_dp_2_.txt'

'''
Without DP
Hold out
network_params.json
network_params_.json
Cross validation
network_params_2.json
network_params_2_.json

With DP (0.1, 1, 2)
Hold out
neural_network_dp_0_1.json
neural_network_dp_1.json
neural_network_dp_2.json
neural_network_dp_0_1_.json
neural_network_dp_1_.json
neural_network_dp_2_.json
Cross validation
neural_network_2_dp_0_1.json
neural_network_2_dp_1.json
neural_network_2_dp_2.json
neural_network_2_dp_0_1_.json
neural_network_2_dp_1_.json
neural_network_2_dp_2_.json
'''
neural_network_model_save_filename = 'saved_models_neural_network/neural_network_dp_2_.json'

def quantize_float(num):
    return float(Decimal(num).quantize(Decimal('1.00')))

def get_train_test_split():
    pids = []
    with open(idp_filename, 'r') as idp:
        line = idp.readline()
        line = idp.readline()
        while line is not '':
            line = line.strip().split(',')
            pids.append(int(line[0]))
            line = idp.readline()
    train_test_split_point = int(len(pids) * 0.8)
    train_pids = pids[:train_test_split_point]
    test_pids = pids[train_test_split_point:]
    return (train_pids, test_pids)

def get_train_test_split_2():
    pids = []
    train_pids_list = []
    test_pids_list = []
    with open(idp_filename, 'r') as idp:
        line = idp.readline()
        line = idp.readline()
        while line is not '':
            line = line.strip().split(',')
            pids.append(int(line[0]))
            line = idp.readline()
    train_test_split_point = int(len(pids) * 0.8)
    train_pids_list.append(pids[:train_test_split_point])
    test_pids_list.append(pids[train_test_split_point:])

    for iterator in range(3, 0, -1):
        train_test_split_point_1 = int(len(pids) * 0.2 * iterator)
        train_test_split_point_2 = int(len(pids) * 0.2 * (iterator + 1))
        train_pids_list.append(pids[:train_test_split_point_1] + pids[train_test_split_point_2:])
        test_pids_list.append(pids[train_test_split_point_1:train_test_split_point_2])

    train_test_split_point = int(len(pids) * 0.2)
    train_pids_list.append(pids[train_test_split_point:])
    test_pids_list.append(pids[:train_test_split_point])

    return (train_pids_list, test_pids_list)

def vectorized_result(j):
    e = np.zeros((250, 1))
    e[int(j)] = 1.0
    return e

def get_training_data(train_pids):
    train_sbp_values = []
    train_dbp_values = []
    train_times = []
    with open(vip_filename, 'r') as vip, open(vip_const_filename, 'r') as vip_other:
        line = vip.readline()
        line = vip.readline()
        line_other = vip_other.readline()
        line_other = vip_other.readline()
        while line is not '' or line_other is not '':
            line = line.strip().split(',')
            line_other = line_other.strip().split(',')
            if int(line[0]) in train_pids:
                # train_sbp_values.append(int(line[3]))
                # train_sbp_values.append(float(line[3]))
                train_sbp_values.append(float(line_other[3]))
                # train_dbp_values.append(int(line[4]))
                train_dbp_values.append(float(line[4]))
                # train_times.append(int(line[-1]))
                train_times.append(float(line[-1]))
            line = vip.readline()
            line_other = vip_other.readline()
    training_inputs = [np.array(np.reshape([dbp, time], (2, 1)), dtype=np.float64) \
        for dbp, time in zip(train_dbp_values, train_times)]
    training_results = [vectorized_result(sbp) for sbp in train_sbp_values]
    return zip(training_inputs, training_results)

def get_test_data(test_pids):
    for pid in test_pids:
        print(pid, end=' ')
    print('\n')
    pid = int(input('Enter pid by selecting one from the above: '))
    while pid not in test_pids:
        pid = int(input('Enter pid by selecting one from the above: '))
    print('\n')

    dates = []
    found = False
    with open(d1_filename, 'r') as d1:
        line = d1.readline()
        line = d1.readline()
        while line is not '' and not found:
            line = line.strip().split(',')
            while int(line[0]) == pid:
                found = True
                dates.append(line[1])
                print(line[1], end=' ')
                line = d1.readline().strip().split(',')
                if line[0] is '':
                    break
            line = d1.readline()
    print('\n')
    if dates == []:
        print('Rerun the code and select another pid, this pid does not have any associated dates')
        sys.exit(0)
    date = input('Enter date by selecting one from the above: ')
    while date not in dates:
        date = input('Enter date by selecting one from the above: ')
    print('\n')

    times = []
    sbp_values = []
    dbp_values = []
    found = False
    with open(vip_const_filename, 'r') as vip:
        line = vip.readline()
        line = vip.readline()
        while line is not '' and not found:
            line = line.strip().split(',')
            while line[0] + ' ' + line[1] == str(pid) + ' ' + date:
                found = True
                # times.append(int(line[-1]))
                times.append(float(line[-1]))
                # sbp_values.append(int(line[3]))
                sbp_values.append(float(line[3]))
                # dbp_values.append(int(line[4]))
                dbp_values.append(float(line[4]))
                print(line[-1], end=' ')
                line = vip.readline().strip().split(',')
                if line[0] is '':
                    break
            line = vip.readline()
    print('\n')
    if times == []:
        print('Rerun the code and select another date or another pid if this is the only listed date, this date does not have any associated times')
        sys.exit(0)
    # time = int(input('Enter time by selecting one from the above: '))
    time = float(input('Enter time by selecting one from the above: '))
    # while time not in times or time == times[-1]:
    while time not in times:
        # time = int(input('Enter time by selecting one from the above: '))
        time = float(input('Enter time by selecting one from the above: '))
    print('\n')

    test_input = np.array(np.reshape([dbp_values[times.index(time)], times[times.index(time)]], (2, 1)), dtype=np.float64)
    test_result = sbp_values[times.index(time)]

    return (test_input, test_result)

def get_prediction(network, test_input):
    return np.argmax(network.feedforward(test_input)) + 1

def compute_save_prediction_results(network, test_pids):
    pid_dates = {}
    total_error = 0
    num_test_cases = 0
    for test_pid in test_pids:
        pid_dates[test_pid] = []
    with open(d1_filename, 'r') as d1:
        line = d1.readline()
        line = d1.readline()
        while line is not '':
            line = line.strip().split(',')
            if int(line[0]) in pid_dates.keys():
                pid_dates[int(line[0])].append(line[1])
            line = d1.readline()
    with open(prediction_results_filename, 'w') as results:
        results.write('Pid Date Time Actual_SBP Predicted_SBP Absolute_Percentage_Error\n')
        with open(vip_const_filename, 'r') as vip:
            line = vip.readline()
            line = vip.readline()
            while line is not '':
                line = line.strip().split(',')
                if int(line[0]) in pid_dates.keys():
                    if line[1] in pid_dates[int(line[0])]:
                        num_test_cases += 1
                        # test_input = np.array(np.reshape([int(line[4]), int(line[-1])], (2, 1)), dtype=np.float64)
                        test_input = np.array(np.reshape([float(line[4]), float(line[-1])], (2, 1)), dtype=np.float64)
                        # test_result = int(line[3])
                        test_result = float(line[3])
                        prediction = get_prediction(network, test_input)
                        # error = quantize_float(abs(prediction - test_result) / test_result * 100)
                        error = quantize_float(abs((prediction - test_result) / test_result * 100))
                        total_error += error
                        results.write(line[0] + ' ' + line[1] + ' ' + line[-1] + ' ' + line[3] + ' ' + str(prediction) + ' ' + str(error) + '\n')
                line = vip.readline()
    return quantize_float(total_error / num_test_cases)

def compute_save_prediction_results_2(network, test_pids, fold):
    pid_dates = {}
    total_error = 0
    num_test_cases = 0
    for test_pid in test_pids:
        pid_dates[test_pid] = []
    with open(d1_filename, 'r') as d1:
        line = d1.readline()
        line = d1.readline()
        while line is not '':
            line = line.strip().split(',')
            if int(line[0]) in pid_dates.keys():
                pid_dates[int(line[0])].append(line[1])
            line = d1.readline()
    mode = 'a'
    if fold == 1:
        mode = 'w'
    with open(prediction_results_filename, mode) as results:
        results.write('Fold ' + str(fold) + '\n')
        results.write('Pid Date Time Actual_SBP Predicted_SBP Absolute_Percentage_Error\n')
        with open(vip_const_filename, 'r') as vip:
            line = vip.readline()
            line = vip.readline()
            while line is not '':
                line = line.strip().split(',')
                if int(line[0]) in pid_dates.keys():
                    if line[1] in pid_dates[int(line[0])]:
                        num_test_cases += 1
                        # test_input = np.array(np.reshape([int(line[4]), int(line[-1])], (2, 1)), dtype=np.float64)
                        test_input = np.array(np.reshape([float(line[4]), float(line[-1])], (2, 1)), dtype=np.float64)
                        # test_result = int(line[3])
                        test_result = float(line[3])
                        prediction = get_prediction(network, test_input)
                        # error = quantize_float(abs(prediction - test_result) / test_result * 100)
                        error = quantize_float(abs((prediction - test_result) / test_result * 100))
                        total_error += error
                        results.write(line[0] + ' ' + line[1] + ' ' + line[-1] + ' ' + line[3] + ' ' + str(prediction) + ' ' + str(error) + '\n')
                line = vip.readline()
        results.write('\n')
    return quantize_float(total_error / num_test_cases)

if __name__ == '__main__':
    train_pids, test_pids = get_train_test_split()
    print('Pids for training: ' + str(train_pids))
    print('Pids for testing: ' + str(test_pids))

    training_data = list(get_training_data(train_pids))
    print('Training data: ' + str(training_data))

    test_input, test_result = get_test_data(test_pids)
    print('Test input: ' + str(test_input))
    print('Test result: ' + str(test_result))


from flask import Flask
from flask import render_template,request
from pymongo import MongoClient
import json
from bson import json_util
from bson.json_util import dumps

app = Flask(__name__,template_folder='/home/sri/Downloads/AAL-94_dataset/AALtorch/template')
print(app)
MONGOD_HOST = 'localhost'
MONGOD_PORT = 27017
DBS_NAME = 'pymongo_test'
COLLECTION_NAME = 'posts'
FIELDS = {'Class': True, 'Conf': True,'Dates': True}
#var date,timestamp

@app.route("/")
def demo1():
    return render_template("demo.html")

@app.route("/pymongo_test/posts")
def donor_projects():
    connection = MongoClient(MONGOD_HOST, MONGOD_PORT)
    collection = connection[DBS_NAME][COLLECTION_NAME]
    projects = collection.find(projection=FIELDS)
    #print(timestamp)
    cursor = []
    cursor = collection.aggregate([{"$group":{"_id":"$Class","Count":{"$sum":1}}}])
    json_projects = []
    count = []
    for document in cursor:
    	count.append(document)
    for project in projects:
        json_projects.append(project)
    json_projects = json.dumps(json_projects, default=json_util.default)
    count = json.dumps(count, default=json_util.default)
    #print(count)
    connection.close()
    return json_projects

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


__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

import os

from qgis.PyQt.QtGui import QIcon

from processing.core.ProcessingConfig import ProcessingConfig, Setting
from processing.core.ProcessingLog import ProcessingLog
from processing.core.AlgorithmProvider import AlgorithmProvider
from processing.gui.EditScriptAction import EditScriptAction
from processing.gui.DeleteScriptAction import DeleteScriptAction
from processing.gui.CreateNewScriptAction import CreateNewScriptAction
from processing.script.WrongScriptException import WrongScriptException
from processing.gui.GetScriptsAndModels import GetRScriptsAction
from processing.tools.system import isWindows

from .RUtils import RUtils
from .RAlgorithm import RAlgorithm

pluginPath = os.path.normpath(os.path.join(
    os.path.split(os.path.dirname(__file__))[0], os.pardir))

class RAlgorithmProvider(AlgorithmProvider):

    def __init__(self):
        AlgorithmProvider.__init__(self)
        self.activate = False
        self.actions.append(CreateNewScriptAction(
            'Create new R script', CreateNewScriptAction.SCRIPT_R))
        self.actions.append(GetRScriptsAction())
        self.contextMenuActions = \
            [EditScriptAction(EditScriptAction.SCRIPT_R),
             DeleteScriptAction(DeleteScriptAction.SCRIPT_R)]

    def initializeSettings(self):
        AlgorithmProvider.initializeSettings(self)
        ProcessingConfig.addSetting(Setting(
            self.getDescription(), RUtils.RSCRIPTS_FOLDER,
            self.tr('R Scripts folder'), RUtils.defaultRScriptsFolder(),
            valuetype=Setting.MULTIPLE_FOLDERS))
        if isWindows():
            ProcessingConfig.addSetting(Setting(
                self.getDescription(),
                RUtils.R_FOLDER, self.tr('R folder'), RUtils.RFolder(),
                valuetype=Setting.FOLDER))
            ProcessingConfig.addSetting(Setting(
                self.getDescription(),
                RUtils.R_LIBS_USER, self.tr('R user library folder'),
                RUtils.RLibs(), valuetype=Setting.FOLDER))
            ProcessingConfig.addSetting(Setting(
                self.getDescription(),
                RUtils.R_USE64, self.tr('Use 64 bit version'), False))

    def unload(self):
        AlgorithmProvider.unload(self)
        ProcessingConfig.removeSetting(RUtils.RSCRIPTS_FOLDER)
        if isWindows():
            ProcessingConfig.removeSetting(RUtils.R_FOLDER)
            ProcessingConfig.removeSetting(RUtils.R_LIBS_USER)
            ProcessingConfig.removeSetting(RUtils.R_USE64)

    def getIcon(self):
        return QIcon(os.path.join(pluginPath, 'images', 'r.svg'))

    def getDescription(self):
        return 'R scripts'

    def getName(self):
        return 'r'

    def _loadAlgorithms(self):
        folders = RUtils.RScriptsFolders()
        self.algs = []
        for f in folders:
            self.loadFromFolder(f)

        folder = os.path.join(os.path.dirname(__file__), 'scripts')
        self.loadFromFolder(folder)

    def loadFromFolder(self, folder):
        if not os.path.exists(folder):
            return
        for path, subdirs, files in os.walk(folder):
            for descriptionFile in files:
                if descriptionFile.endswith('rsx'):
                    try:
                        fullpath = os.path.join(path, descriptionFile)
                        alg = RAlgorithm(fullpath)
                        if alg.name.strip() != '':
                            self.algs.append(alg)
                    except WrongScriptException as e:
                        ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, e.msg)
                    except Exception as e:
                        ProcessingLog.addToLog(
                            ProcessingLog.LOG_ERROR,
                            self.tr('Could not load R script: %s\n%s' % (descriptionFile, unicode(e))))


import bibtexparser
import re
import os
from os import path
if path.exists('mytitles.txt'):
    os.remove('mytitles.txt')
with open('MyCollection.bib') as bibtex_file:
    bib_database = bibtexparser.load(bibtex_file)

with open("mytitles.txt","a") as file1:
    for entry in bib_database.entries:
        title = entry['title']
        title = title.replace('{','')
        title = title.replace('}','')
        print(title)
        file1.write('%s\n' % title)


from django.conf.urls import url

from ..views.oj import (SearchGroupByKeyWordAPI,
                        JoinGroupBySearchAPI,
                        GroupListAndDetailAPI,
                        HomeWorkListAndDetailAPI
                        )

urlpatterns = [
    url(r"^search_group/?$", SearchGroupByKeyWordAPI.as_view(), name="search_group_api"),
    url(r"^join_group/?$", JoinGroupBySearchAPI.as_view(), name="join_group_api"),
    url(r"^groups/?$", GroupListAndDetailAPI.as_view(), name="group_api"),
    url(r"^homework/?$", HomeWorkListAndDetailAPI.as_view(), name="homework_api"),
]


from selenium import webdriver
import pandas as pd
from IPython.display import display
from selenium.webdriver.chrome.options import Options

# inicializa o programa sem aparecer na tela, em segundo plano
chrome_options = Options()
chrome_options.headless = True

# abre o navegador com as opções definidas acima
navegador = webdriver.Chrome(options=chrome_options)

# passo 1: pegar a cotação das moedas
# dolar
navegador.get('https://www.google.com/search?q=cotacao+dolar&oq=cotacao+dolar&aqs=chrome..69i57j35i39j0i512l3j0i433i512j0i512l4.1498j0j9&sourceid=chrome&ie=UTF-8')
cot_dolar = navegador.find_element('xpath', '//*[@id="knowledge-currency__updatable-data-column"]/div[1]/div[2]/span[1]').get_attribute('data-value')

# euro
navegador.get('https://www.google.com/search?q=cotacao+euro&oq=cotacao+euro&aqs=chrome..69i57j0i512l4j0i10i512j0i512l4.1450j0j7&sourceid=chrome&ie=UTF-8')
cot_euro = navegador.find_element('xpath', '//*[@id="knowledge-currency__updatable-data-column"]/div[1]/div[2]/span[1]').get_attribute('data-value')

# ouro
navegador.get('https://www.melhorcambio.com/ouro-hoje#:~:text=O%20valor%20do%20grama%20do,em%20R%24%20314%2C92.')
cot_ouro = navegador.find_element('xpath', '//*[@id="comercial"]').get_attribute('value')
cot_ouro = cot_ouro.replace(',', '.')

# fecha o navegador após pegar a cotação
navegador.quit()

# passo 2: importar e atualizar a base de dados
df = pd.read_excel('Produtos.xlsx')

# atualizar a cotação das moedas no dataframe
df.loc[df['Moeda'] == 'Dólar', 'Cotação'] = float(cot_dolar)    # loc[linha, coluna]
df.loc[df['Moeda'] == 'Euro', 'Cotação'] = float(cot_euro)
df.loc[df['Moeda'] == 'Ouro', 'Cotação'] = float(cot_ouro)

# atualizar o preço de compra e preço de venda
df['Preço de Compra'] = df['Preço Original'] * df['Cotação']
df['Preço de Venda'] = df['Preço de Compra'] * df['Margem']

# formatar os preços
df['Preço de Compra'], df['Preço de Venda'] = df['Preço de Compra'].map('R${:.2f}'.format), df['Preço de Venda'].map('R${:.2f}'.format)
df['Cotação'] = df['Cotação'].map('R${:.2f}'.format)

# passo 3: exportar a base de dados
df.to_excel('Produtos Novo.xlsx', index=False)   # nome do arquivo a ser criado | index=false para não exportar o index junto

df_novo = pd.read_excel('Produtos Novo.xlsx')
display(df_novo)


from django.test import TestCase, tag
from django.urls.base import reverse
from edc_model_wrapper import ModelWrapper

from ..models import ActionItem, ActionType
from ..templatetags.action_item_extras import add_action_item_popover
from ..view_mixins import ActionItemViewMixin
from .models import SubjectIdentifierModel

class MyModelWrapper(ModelWrapper):
    next_url_name = 'dashboard_url'

class TestAction(TestCase):

    def setUp(self):
        self.subject_identifier_model = ActionItem.subject_identifier_model
        ActionItem.subject_identifier_model = 'edc_action_item.subjectidentifiermodel'
        self.subject_identifier = '12345'
        SubjectIdentifierModel.objects.create(
            subject_identifier=self.subject_identifier)
        ActionItemViewMixin.action_item_model_wrapper_cls = MyModelWrapper

    def test_view_populates_action_type(self):
        self.assertEqual(ActionType.objects.all().count(), 0)
        ActionItemViewMixin()
        self.assertGreater(ActionType.objects.all().count(), 0)
        ActionItemViewMixin()
        self.assertGreater(ActionType.objects.all().count(), 0)

    def test_view_context(self):
        view = ActionItemViewMixin()
        view.kwargs = dict(subject_identifier=self.subject_identifier)
        context = view.get_context_data()
        self.assertEqual(context.get('open_action_items'), [])

        for action_type in ActionType.objects.all():
            ActionItem.objects.create(
                subject_identifier=self.subject_identifier,
                action_type=action_type)

        view = ActionItemViewMixin()
        view.kwargs = dict(subject_identifier=self.subject_identifier)
        context = view.get_context_data()
        self.assertEqual(len(context.get('open_action_items')),
                         ActionItem.objects.all().count())

    def test_templatetag(self):
        context = add_action_item_popover(
            self.subject_identifier, 'subject_dashboard_url')
        reverse(context.get('action_item_add_url'))


from glob import glob
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI
import os, sys, openai
from dotenv import load_dotenv
from langchain.tools import tool, Tool
from retrieval import Retrieval
from pyepsilla import cloud

load_dotenv()

retrieval = Retrieval()

client = cloud.Client(
  project_id=os.getenv("PROJECT_ID"),
  api_key=os.getenv("EPSILLA_API_KEY")
)
db = client.vectordb(db_id=os.getenv("DB_ID"))

@tool
def search_api(question: str) -> str:
    """Searches the relevant information from the document set to answer the question."""
    qs = retrieval.rephrase(question=question)
    query_score_dict = {}
    item = retrieval.vector_search(db, question)
    # print(item)
    query_score_dict[question] = item
    for q in qs:
      item = retrieval.vector_search(db, q)
      query_score_dict[q] = item
    # print(query_score_dict)

    ranking_result = retrieval.ranking_fusion(original_query=question, query_score_dict=query_score_dict)
    final_result = retrieval.generate_content_based_on_ranking(ranking_result)
    return final_result

class DocAgent:
    def __init__(self):
        api_key = os.getenv("OPENAI_KEY")
        llm = OpenAI(temperature=0, openai_api_key=api_key)
        tools = [search_api]
        tools = tools + load_tools(["llm-math"], llm=llm)
        self.agent_executor = initialize_agent(
            tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
        )

    def list_docs(self):
        # List PDF files under ./documents/ folder
        ret = []
        files = glob("./documents/*.pdf")
        for pdf in files:
            ret.append(os.path.basename(pdf))
        return ret

    def solve(self, question):
        response = self.agent_executor.invoke(
            {
                "input": question
            }
        )
        return response['output']

    def rephrase(self, question):
        return retrieval.rephrase(question=question)

    def solve_one(self, file, question, questions):
        # Step 1. Search the relevant information from the document to answer the question.
        query_score_dict = {}
        item = retrieval.vector_search_one_doc(db, question, file)
        # print(item)
        query_score_dict[question] = item
        for q in questions:
          item = retrieval.vector_search_one_doc(db, q, file)
          query_score_dict[q] = item
        # print(query_score_dict)

        ranking_result = retrieval.ranking_fusion(original_query=question, query_score_dict=query_score_dict)
        context = retrieval.generate_content_based_on_ranking(ranking_result)

        # Step 2. Use the prompt to answer the question for the document.
        openai.api_key = os.getenv("OPENAI_KEY")
        response = openai.ChatCompletion.create(
          model="gpt-4",
          messages=[
            {
              "role": "system",
              "content": "You are an assistant answering questions for a given document."
            },
            {
              "role": "user",
              "content": f'''
                Answer the Question based on the given Context. Please don't make things up. Ask for more information when needed.

                Context:
                {context}

                Question:
                {question}

                Answer:
                Let's work this out in a step by step way to be sure we have the right answer.
                '''
            }
          ],
          temperature=0,
          max_tokens=256,
          top_p=1,
          frequency_penalty=0,
          presence_penalty=0
        )

        return response['choices'][0]['message']['content']

    def summary(self, question, concated):
        openai.api_key = os.getenv("OPENAI_KEY")
        response = openai.ChatCompletion.create(
          model="gpt-4",
          messages=[
            {
              "role": "system",
              "content": "You are an assistant answering questions for a given document."
            },
            {
              "role": "user",
              "content": f'''
                Answer the Question based on the Analysis Of Each Document. If some documents are not related to the question, please ignore them.

                Analysis Of Each Document:
                {concated}

                Question:
                {question}

                Answer:
                '''
            }
          ],
          temperature=0,
          max_tokens=256,
          top_p=1,
          frequency_penalty=0,
          presence_penalty=0
        )

        return response['choices'][0]['message']['content']

    def can_loop(self, question):
        openai.api_key = os.getenv("OPENAI_KEY")
        response = openai.ChatCompletion.create(
          model="gpt-4",
          messages=[
            {
              "role": "system",
              "content": "You are an assistant answering questions for a large set of documents."
            },
            {
              "role": "user",
              "content": "For the provided question, determine if we can check the documents one by one and make the judgement and answer it purely based on facts from this file, or we need to cross validate with other files. If former, response \"YES\"; if later, response \"NO\"\n\nQuestion: " + question
            }
          ],
          temperature=0,
          max_tokens=256,
          top_p=1,
          frequency_penalty=0,
          presence_penalty=0
        )
        decision = response['choices'][0]['message']['content'] or 'YES'
        return decision == 'YES'

# agent = DocAgent()
# print(agent.list_docs())


#!/usr/bin/env python3
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import sys

# crawling
def download_page(url):
    resp = requests.get(url)
    while resp.status_code != 200:
        resp = requests.get(url)
    return resp.text

def parse_html(url, html):
    path = urlparse(url).path.split('/')
    uid = path[-3]
    soup = BeautifulSoup(html, 'html.parser')
    selected = soup.select('div#thing_t3_{0} div.md'.format(uid))[0]
    return selected.get_text()

if __name__ == '__main__':
    url = sys.argv[1]
    html = download_page(url)
    content = parse_html(url, html)
    print(content)


#!/usr/bin/env python
# coding: utf-8

# In[3]:

'''
1부터 n까지의 수 중 짝수의 합을 구하시오..
'''
a=int(input())
b=[]
for i in range(1,a+1):
    if i%2==0:
        b.append(i)
print(sum(b))

# In[ ]:


# Author: Baozi
#-*- codeing:utf-8 -*-
"""题目描述
将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能，但是string不符合数字要求时返回0)，
要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。
输入描述:输入一个字符串,包括数字字母符号,可以为空
输出描述:如果是合法的数值表达则返回该数字，否则返回0
"""
"""解题思路：
Python中str无相减，所以要利用ord()函数，将其转换成ascii码
"""
class Solution:
    def StrToInt(self, s):
        # write code here
        if not s or not isinstance(s,str):
            return 0
        if s[0]=='+':
            return self.StrToInt2(s[1:])
        elif s[0]=='-':
            return -1*self.StrToInt2(s[1:])
        else:
            return self.StrToInt2(s)

    def StrToInt2(self,s):
        if not s:
            return 0
        num = 0
        for i in s:
            if ord(i)<ord('0') or ord(i) > ord('9'):
                return 0
            else:
                num = 10*num +(ord(i) - ord('0'))#转换过程！
        return num

if __name__ == '__main__':
    s = Solution()
    print(s.StrToInt('-1314'))


import copy

class Optimizer:
    def __init__(self, fitness_function, population_size=20, max_iterations=100,**kwargs):

        #set paramaters for users problem
        self.fitness_function = fitness_function
        self.additional_parameters = kwargs #parameters for the users fitness function

        #set general algorithm paramaters
        self.population_size = population_size
        self.max_iterations = max_iterations

        #enable logging by default
        self.logging = True

        #set initial values that are used internally
        self.iteration = 0
        self.evaluation_runs = 0
        self.best_solution = None
        self.best_fitness = None
        self.solution_found = False
        self._fitness_dict = {}

        # Parameters for algorithm specific functions
        self.initial_pop_args = []
        self.new_pop_args = []

    def initialize(self):
        pass

    def create_initial_population(self, *args, **kwargs):
        raise NotImplementedError("create_initial_population is not implemented.")

    def new_population(self, *args, **kwargs):
        raise NotImplementedError("new_population is not implemented.")

    def optimize(self):
        self.evaluation_runs = 0
        self.solution_found = False
        self._fitness_dict = {}

        best_solution = {'solution': [], 'fitness': 0.0}
        self.initialize()
        population = self.create_initial_population(self.population_size)

        for self.iteration in range(self.max_iterations):
            fitnesses, finished = self.get_fitnesses(population)
            if max(fitnesses) > best_solution['fitness']:
                best_solution['fitness'] = max(fitnesses)
                best_solution['solution'] = copy.copy(population[fitnesses.index(max(fitnesses))])

            if self.logging:
                print ('Iteration: ' + str(self.iteration))
            if finished:
                self.solution_found = True
                break

            population = self.new_population(population, fitnesses)

        self._fitness_dict = {}

        self.best_solution = best_solution['solution']
        self.best_fitness = best_solution['fitness']
        return (self.best_solution,population)

    def get_fitnesses(self, population):
        fitnesses = []
        finished = False
        for solution in population:
            try:
                #attempt to retrieve the fitness from the internal fitness memory
                fitness = self._fitness_dict[str(solution)]
            except KeyError:
                #if the fitness is not remembered
                #calculate the fitness, pass in any saved user arguments
                fitness = self.fitness_function(solution, **self.additional_parameters)
                #if the user supplied fitness function includes the "finished" flag
                #unpack the results into the finished flag and the fitness
                if isinstance(fitness, tuple):
                    finished = fitness[1]
                    fitness = fitness[0]
                self._fitness_dict[str(solution)] = fitness
                self.evaluation_runs += 1 #keep track of how many times fitness is evaluated

            fitnesses.append(fitness)
            if finished:
                break

        return fitnesses, finished


import keras
import numpy as np
from keras.layers import Input, Dense
from keras.models import Model
from keras.optimizers import Adam
from sklearn.cluster import KMeans
from keras.models import load_model
import csv
import sys

path_train = sys.argv[1]
path_test = sys.argv[2]
path_out = sys.argv[3]

def load_data():
    with open(path_test, 'r') as file:
        reader = csv.reader(file, delimiter=',')
        test_x = []
        iter = 0
        for line in reader:
            if iter == 0:
                iter += 1
                continue
            test = []
            test.append(int(line[1]))
            test.append(int(line[2]))
            test_x.append(test)
            iter += 1
        test_x = np.array(test_x)
        print(test_x)
        return test_x

def build_model():
    input_img = Input(shape=(784, ))
    encoded = Dense(128, activation='relu')(input_img)
    encoded = Dense(64, activation='relu')(encoded)
    encoded = Dense(32, activation='relu')(encoded)

    decoded = Dense(64, activation='relu')(encoded)
    decoded = Dense(128, activation='relu')(decoded)
    decoded = Dense(784, activation='sigmoid')(decoded)

    encoder = Model(input = input_img, output=encoded)

    adam = Adam(lr=5e-4)
    autoencoder = Model(input=input_img, output=decoded)
    autoencoder.compile(optimizer=adam, loss='mse')
    autoencoder.summary()
    return encoder, autoencoder

def main():
    x = np.load(path_train)
    x = x.astype('float32') / 255.
    #train_num = 130000
    #print(x.shape)
    #train_x = x[:train_num]
    #valid_x = x[train_num:]

    """encoder, autoencoder = build_model()
    autoencoder.fit(train_x, train_x, epochs=1000, batch_size=256, shuffle=True, validation_data=(valid_x, valid_x))
    autoencoder.save('autoencoder.h5')
    encoder.save('encoder.h5')"""
    encoder = load_model('encoder.h5')
    encoder_imgs = encoder.predict(x)
    encoder_imgs = encoder_imgs.reshape(encoder_imgs.shape[0], -1)
    kmeans = KMeans(n_clusters=2, random_state=0).fit(encoder_imgs)

    test_x = load_data()
    same = []
    test_y = np.zeros(shape=test_x.shape)
    for i in range(test_x.shape[0]):
        test_y[i][0] = i
        a = kmeans.labels_[test_x[i][0]]
        b = kmeans.labels_[test_x[i][1]]
        if a == b:
            test_y[i][1] = 1
        else:
            test_y[i][1] = 0

    with open(path_out, 'w') as file:
        writer = csv.writer(file, delimiter=',')
        writer.writerow(['ID', 'Ans'])
        for i in range(test_y.shape[0]):
            writer.writerow([int(test_y[i][0]), int(test_y[i][1])])

if __name__ == "__main__":
    main()


from wireless import wifi
from espressif.esp8266wifi import esp8266wifi as wifi_driver
import streams
import threading

# Import the Zerynth APP library
from zerynthapp import zerynthapp

streams.serial()

sleep(1000)
print("STARTING...")

# save the index.html in the board flash
new_resource("template/index.html")

# set the LED pins as outputs
pinMode(D0,OUTPUT) # red
pinMode(D1,OUTPUT) # green
pinMode(D2,OUTPUT) # blue

# variables for the current status of the LED
current_r = LOW
current_g = LOW
current_b = LOW

# global variables
on = False
stopped = False

# RPC functions to manage the LED
def turn_on():
    global on
    on = True
    digitalWrite(D0,current_r)
    digitalWrite(D1,current_g)
    digitalWrite(D2,current_b)

def turn_off():
    global on
    on = False
    digitalWrite(D0,LOW)
    digitalWrite(D1,LOW)
    digitalWrite(D2,LOW)

def red_led():
    if (current_r == LOW):
        current_r = HIGH
    else:
        current_r = LOW
    global on
    if on:
        digitalWrite(D0,current_r)

def green_led():
    if (current_g == LOW):
        current_g = HIGH
    else:
        current_g = LOW
    global on
    if on:
        digitalWrite(D1,current_g)

def blue_led():
    if (current_b == LOW):
        current_b = HIGH
    else:
        current_b = LOW
    global on
    if on:
        digitalWrite(D2,current_b)

def rainbow():
    global stopped
    stopped = False
    def run():
        while not stopped:
            # purple
            digitalWrite(D0,HIGH)
            digitalWrite(D2,HIGH)
            sleep(200)
            # blue
            digitalWrite(D0,LOW)
            sleep(200)
            # sky blue
            digitalWrite(D1,HIGH)
            sleep(200)
            # green
            digitalWrite(D2,LOW)
            sleep(200)
            # yellow
            digitalWrite(D0,HIGH)
            sleep(200)
            # red
            digitalWrite(D1,LOW)
            sleep(200)
            if stopped:
                global on
                if on:
                    digitalWrite(D0,current_r)
                    digitalWrite(D1,current_g)
                    digitalWrite(D2,current_b)
                else:
                    digitalWrite(D0,LOW)
                    digitalWrite(D1,LOW)
                    digitalWrite(D2,LOW)
                break
    thread = threading.Thread(target=run)
    thread.start()

def rainbow_stop():
    global stopped
    stopped = True

# Device UID and TOKEN can be created in the ADM panel
zapp = zerynthapp.ZerynthApp("UID", "TOKEN", log=True)

# link Python functions to Javascript
zapp.on("turn_on", turn_on)
zapp.on("turn_off",turn_off)
zapp.on("RED", red_led)
zapp.on("GREEN", green_led)
zapp.on("BLUE", blue_led)
zapp.on("RAINBOW", rainbow)
zapp.on("STOP", rainbow_stop)

try:
    # connect to the wifi network (Set your SSID and password below)
    wifi_driver.auto_init()
    for i in range(0,5):
        try:
            wifi.link("SSID",wifi.WIFI_WPA2,"PASSWORD")
            break
        except Exception as e:
            print("Can't link",e)
    else:
        print("Impossible to link!")
        while True:
            sleep(1000)

    # Start the Zerynth app instance!
    # Remember to create a template with the files under the "template" folder you just cloned
    # upload it to the ADM and associate it with the connected device
    zapp.run()

    # Do whatever you need here
    while True:
        print(".")
        sleep(5000)

except Exception as e:
    print(e)


def main():
    outfile = open('counting.txt', 'w')

    print('This program will create a text file with counting numbers')

    N = int(input('How many numbers would you like to store in this file: '))

    for number in range(N):  # the variable number will get every value from 0 to N-1 in each iteration
        outfile.write(str(number + 1) + '\n')

    outfile.close()
    print('Data has been written to counting.txt')

if __name__ == "__main__":
    main()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/9/8 12:39
# @Author  : JJkinging
# @File    : utils.py
from torch.utils.data import Dataset, DataLoader
from data.code.predict.test_utils import load_vocab, collate_to_max_length

class CCFDataset(Dataset):
    def __init__(self, filename, intent_filename, slot_filename, slot_none_filename, vocab, intent_dict,
                 slot_none_dict, slot_dict, max_length=512):
        '''
        :param filename:读取数据文件名，例如：train_seq_in.txt
        :param intent_filename: train_intent_label.txt or dev_intent_label.txt
        :param slot_filename: train_seq_out.txt
        :param slot_none_filename: train_slot_none.txt or dev_slot_none.txt
        :param slot_none_dict: slot_none的字典
        :param slot_dict: slot_label的字典
        :param vocab: 词表，例如：bert的vocab.txt
        :param intent_dict: intent2id的字典
        :param max_length: 单句最大长度
        '''
        self.filename = filename
        self.intent_filename = intent_filename
        self.slot_filename = slot_filename
        self.slot_none_filename = slot_none_filename
        self.vocab = vocab
        self.intent_dict = intent_dict
        self.slot_none_dict = slot_none_dict
        self.slot_dict = slot_dict
        self.max_length = max_length

        self.result = []

        # 读取数据
        with open(self.filename, 'r', encoding='utf-8') as fp:
            sen_data = fp.readlines()
        sen_data = [item.strip('\n') for item in sen_data]  # 删除句子结尾的换行符('\n')

        # 读取intent
        with open(self.intent_filename, 'r', encoding='utf-8') as fp:
            intent_data = fp.readlines()
        intent_data = [item.strip('\n') for item in intent_data]  # 删除结尾的换行符('\n')
        intent_ids = [intent_dict[item] for item in intent_data]

        # 读取slot_none
        with open(self.slot_none_filename, 'r', encoding='utf-8') as fp:
            slot_none_data = fp.readlines()
        # 删除结尾的空格和换行符('\n')
        slot_none_data = [item.strip('\n').strip(' ').split(' ') for item in slot_none_data]
        # 下面列表表达式把slot_none转为id
        slot_none_ids = [[self.slot_none_dict[ite] for ite in item] for item in slot_none_data]

        # 读取slot
        with open(self.slot_filename, 'r', encoding='utf-8') as fp:
            slot_data = fp.readlines()
        slot_data = [item.strip('\n') for item in slot_data]  # 删除句子结尾的换行符('\n')
        # slot_ids = [self.slot_dict[item] for item in slot_data]

        idx = 0
        for utterance in sen_data:
            utterance = utterance.split(' ')  # str变list
            slot_utterence = slot_data[idx].split(' ')
            # 最大长度检验
            if len(utterance) > self.max_length-2:
                utterance = utterance[:max_length]
                slot_utterence = slot_utterence[:max_length]

            # input_ids
            utterance = ['[CLS]'] + utterance + ['[SEP]']
            input_ids = [int(self.vocab[i]) for i in utterance]

            length = len(input_ids)

            # slot_ids
            slot_utterence = ['[START]'] + slot_utterence + ['[EOS]']
            slot_ids = [int(self.slot_dict[i]) for i in slot_utterence]

            # input_mask
            input_mask = [1] * len(input_ids)

            # intent_ids
            intent_id = intent_ids[idx]

            # slot_none_ids
            slot_none_id = slot_none_ids[idx]  # slot_none_id 为 int or list

            idx += 1

            self.result.append((input_ids, slot_ids, input_mask, intent_id, slot_none_id))

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

    def __getitem__(self, index):
        input_ids, slot_ids, input_mask, intent_id, slot_none_id = self.result[index]

        return input_ids, slot_ids, input_mask, intent_id, slot_none_id

if __name__ == "__main__":
    filename = '../dataset/final_data/train_seq_in.txt'
    vocab_file = '../dataset/pretrained_model/erine/vocab.txt'
    intent_filename = '../dataset/final_data/train_intent_label.txt'
    slot_filename = '../dataset/final_data/train_seq_out.txt'
    slot_none_filename = '../dataset/final_data/train_slot_none.txt'
    intent_label = '../dataset/final_data/intent_label.txt'
    slot_label = '../dataset/final_data/slot_label.txt'
    slot_none_vocab = '../dataset/final_data/slot_none_vocab.txt'
    intent_dict = load_vocab(intent_label)
    slot_dict = load_vocab(slot_label)
    slot_none_dict = load_vocab(slot_none_vocab)
    vocab = load_vocab(vocab_file)

    dataset = CCFDataset(filename, intent_filename, slot_filename, slot_none_filename, vocab, intent_dict,
                         slot_none_dict, slot_dict)

    dataloader = DataLoader(dataset, shuffle=False, batch_size=8, collate_fn=collate_to_max_length)

    for batch in dataloader:
        print(batch)
        break


import logging
from dataclasses import dataclass
from http import HTTPStatus

from paddington import (
    Joint, Track, ErrorEvent, ErrorTypeSwitch, RouteNotFound, SequentialSwitch,
)
from web_framework.app import App, WsgiContext
from web_framework.rest_view import RestWheelSet, HttpResponse
from web_framework.wsgi_switch import WsgiSwitch

error_router = ErrorTypeSwitch(default=SequentialSwitch())
router = WsgiSwitch(error_track=error_router)

@dataclass
class User:
    id: int
    name: str

@dataclass
class Error:
    error: str

class UserManager:
    def __init__(self):
        self.users = []

class ManagerJoint(Joint):
    def __init__(self, track: Track):
        super().__init__(track)
        self.user_manager = UserManager()

    def __call__(self, event, context: WsgiContext):
        context.data["user_manager"] = self.user_manager
        return self.track(event, context)

@router.track("/", methods=["GET"])
def index(environ, context: WsgiContext) -> HttpResponse[dict[str, str]]:
    return HttpResponse(
        body={"ok": "Index"}
    )

@router.track("/users", methods=["GET"])
def get_users(environ, context: WsgiContext) -> HttpResponse[list[User]]:
    user_manager: UserManager = context.data["user_manager"]
    return HttpResponse(body=user_manager.users)

@router.track("/users", methods=["POST"])
def add_user(environ, context: WsgiContext) -> HttpResponse[User]:
    user_manager: UserManager = context.data["user_manager"]

    user = User(
        id=len(user_manager.users),
        name=f"User {len(user_manager.users)}",
    )

    user_manager.users.append(user)
    return HttpResponse(body=user)

@error_router.track(RouteNotFound)
def handle_not_found_error(
        environ: ErrorEvent, context: WsgiContext,
) -> HttpResponse[Error]:
    logging.error(f"Resource {environ.event['PATH_INFO']} not found")
    return HttpResponse(
        status=HTTPStatus.NOT_FOUND,
        body=Error(error=f"Resource {environ.event['PATH_INFO']} not found"),
    )

@error_router.default.track()
def handle_any_error(
        environ: ErrorEvent, context: WsgiContext,
) -> HttpResponse[Error]:
    logging.error("Unhandled error in HTTP")
    return HttpResponse(
        status=HTTPStatus.INTERNAL_SERVER_ERROR,
        body=Error(error=str(environ.exception)),
    )

router = ManagerJoint(router)
app = App(RestWheelSet(router))


a,b = map(int,input().split())
s = 1
for i in range(1,1001):
    n = a*i
    if n%10==0 or (n%10==b):
        print(s)
        break

    else:
        s+=1


from locust import HttpUser, task
from urllib3.exceptions import InsecureRequestWarning
import urllib3

urllib3.disable_warnings(InsecureRequestWarning)

__version__ = "1"

params = {}

params["all"] = {
    "types[0]": "software-catalog",
}

params["all_components"] = {
    "types[0]": "software-catalog",
    "filters[kind]": "Component",
}

params["not_found"] = {
    "types[0]": "software-catalog",
    "term": "n/a"
}

params["components_by_lifecycle"] = {
    "types[0]": "software-catalog",
    "filters[kind]": "Component",
    "filters[lifecycle][0]": "experimental",
}

base_path = "/api/search/query"

class SearchCatalogTest(HttpUser):

    def on_start(self):
        self.client.verify = False

    def search(self, query="all") -> None:
        self.client.get(base_path,
                        verify=False,
                        params=params[query])

    @task
    def searchAll(self) -> None:
        self.search("all")

    @task
    def searchAllComponents(self) -> None:
        self.search("all_components")

    @task
    def searchNotFound(self) -> None:
        self.search("not_found")

    @task
    def searchComponentsByLifecycle(self) -> None:
        self.search("components_by_lifecycle")


# coding: utf-8

# In[34]:

#Mandelbrot fractal creation program
#e.g. complex fractal shapes with recursive detail at increasing magnifications
#code adapted from example found @ docs.scipy.org/doc/numpy/user/quickstart.html

import matplotlib.pyplot as plt
import numpy as np

def mbrot(h,w,maxit=125): #higher iterations = more complex edges; longer run times

    a,b=np.mgrid[-1.5:1.5:h*1j,-2:2.1:w*1j] #placement of object in plane
    c=a+b*1j
    d=c
    divtime=maxit+np.zeros(d.shape,dtype=float)

    for thing in range(maxit):
        d=d**2+c
        diverge=d*np.conj(d)>2**2
        div_now=diverge & (divtime==maxit)
        divtime[div_now]=thing
        d[diverge]=1

    return divtime

#sizing, visualization
plt.imshow(mbrot(1500,1500),cmap='winter') #cmap options: matplotlib.org/tutorials/colors/colormaps.html
plt.show()


import arviz as az

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pymc as pm
import pytensor
import pytensor.tensor as pt

print(f"Running on PyMC v{pm.__version__}")
def my_model(theta, x):
    m, c = theta
    return m * x + c

def my_loglike(theta, x, data, sigma):
    model = my_model(theta, x)
    model_2 = my_model(theta, x)
    lh_1=-(0.5 / sigma**2) * np.sum((data - model) ** 2)
    lh_2=-(0.5 / sigma**2) * np.sum((data - model) ** 2)
    return lh_1+lh_2
# define a pytensor Op for our likelihood function
class LogLike(pt.Op):

    """
    Specify what type of object will be passed and returned to the Op when it is
    called. In our case we will be passing it a vector of values (the parameters
    that define our model) and returning a single "scalar" value (the
    log-likelihood)
    """

    itypes = [pt.dvector]  # expects a vector of parameter values when called
    otypes = [pt.dscalar]  # outputs a single scalar value (the log likelihood)

    def __init__(self, loglike, data, x, sigma):
        """
        Initialise the Op with various things that our log-likelihood function
        requires. Below are the things that are needed in this particular
        example.

        Parameters
        ----------
        loglike:
            The log-likelihood (or whatever) function we've defined
        data:
            The "observed" data that our log-likelihood function takes in
        x:
            The dependent variable (aka 'x') that our model requires
        sigma:
            The noise standard deviation that our function requires.
        """

        # add inputs as class attributes
        self.likelihood = loglike
        self.data = data
        self.x = x
        self.sigma = sigma

    def perform(self, node, inputs, outputs):

        # the method that is used when calling the Op
        (theta,) = inputs  # this will contain my variables

        # call the log-likelihood function
        logl = self.likelihood(theta, self.x, self.data, self.sigma)
        outputs[0][0] = np.array(logl)  # output the log-likelihood

# set up our data
N = 10  # number of data points
sigma = 1.0  # standard deviation of noise
x = np.linspace(0.0, 9.0, N)

mtrue = 0.4  # true gradient
ctrue = 3.0  # true y-intercept

truemodel = my_model([mtrue, ctrue], x)

# make data
rng = np.random.default_rng(716743)
data = sigma * rng.normal(size=N) + truemodel

# create our Op
logl = LogLike(my_loglike, data, x, sigma)

# use PyMC to sampler from log-likelihood
for j in range(0, 1):
    with pm.Model():
        # uniform priors on m and c
        #m = pm.Uniform("m", lower=-10.0, upper=10.0)
        #c = pm.Uniform("c", lower=-10.0, upper=10.0)
        #print(vars(m))
        v=[pm.Uniform(x, lower=-10, upper=10) for x in ["m"]]
        q=[pm.Uniform(x, lower=-10, upper=10) for x in ["c"]]
        # convert m and c to a tensor vector
        theta = pt.as_tensor_variable(v+q)
        save_str= "/home/henney/Documents/Oxford/General_electrochemistry/heuristics/file_{0}.nc".format(j)

        # use a Potential to "call" the Op and include it in the logp computation
        pm.Potential("likelihood", logl(theta))

        # Use custom number of draws to replace the HMC based defaults
        idata_mh = pm.sample(3000, tune=1000)
        idata_mh.to_netcdf(save_str)

# plot the traces
az.plot_trace(idata_mh, lines=[("m", {}, mtrue), ("c", {}, ctrue)])


# coding: utf-8

# In[1]:

import shapefile
import matplotlib.pyplot as plt
import numpy as np

# In[16]:

import pandas as pd
path_to_netatmo_coords_df = (r'X:\hiwi\ElHachem\Prof_Bardossy\Extremes'
                             r'\NetAtmo_BW'
                             r'\rain_bw_1hour'
                             r'\netatmo_bw_1hour_coords.csv')
df_c = pd.read_csv(path_to_netatmo_coords_df, sep=';', index_col=0)

plt.ioff()
fig = plt.figure(figsize=(15, 15), dpi=200)

ax = fig.add_subplot(111)

path_to_shpfile = (
    r"X:\exchange\ElHachem\Netatmo\Landesgrenze_ETRS89\Landesgrenze_10000_ETRS89_lon_lat.shp")
shp_de = shapefile.Reader(path_to_shpfile)
# read and plot shapefile (BW or Germany) should be lon lat
for shape_ in shp_de.shapeRecords():
    lon = [i[0] for i in shape_.shape.points[:][::-1]]
    lat = [i[1] for i in shape_.shape.points[:][::-1]]

    ax.scatter(lon, lat, marker='.', c='lightgrey',
               alpha=0.25, s=1)

df_coords = pd.read_csv(
    r"X:\hiwi\ElHachem\Prof_Bardossy\Extremes\DWD_coords_BW.csv", sep=',', index_col=0)

ax.scatter(df_c[' lon'], df_c[' lat'], c='b', alpha=0.85,
           marker='o', s=25, label='Netatmo stations')
ax.scatter(df_coords['lon'], df_coords['lat'], c='r', alpha=0.85,
           s=25,  marker='d', label='DWD stations')
plt.axis('equal')
plt.grid(alpha=.05)
plt.legend(loc=0, fontsize=12)
plt.xlabel('Longitude', fontsize=10)
plt.ylabel('Latitude', fontsize=10)
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)
plt.tight_layout()
plt.savefig(r"X:\hiwi\ElHachem\Prof_Bardossy\Extremes\stations.png",
            frameon=True, papertype='a4',
            bbox_inches='tight', pad_inches=.2)

# In[7]:

plt.savefig(r"X:\hiwi\ElHachem\Prof_Bardossy\Extremes\stations.png",
            frameon=True, papertype='a4',
            bbox_inches='tight', pad_inches=.2)


from astropy.utils.data import get_pkg_data_filename

from ..catalogues import Catalogue
from ..lr import LRMatch

def set_catalogues():
    mocfile = get_pkg_data_filename('data/testcat_moc_1.moc')

    pcat_datafile = get_pkg_data_filename('data/testcat_moc_1.fits')
    pcat = Catalogue(pcat_datafile, area=mocfile, name='pcat')

    scat_datafile = get_pkg_data_filename('data/testcat_3.fits')
    scat = Catalogue(scat_datafile, area=mocfile, name='scat',
                     coord_cols=['RA', 'DEC'], poserr_cols=['raErr', 'decErr'],
                     poserr_type='rcd_dec_ellipse', mag_cols=['uMag', 'gMag'])

    scat_poserr_circle = scat.poserr.transform_to(errtype='circle')
    scat.poserr = scat_poserr_circle

    return pcat, scat

def test_lr_rndprior():
    pcat, scat = set_catalogues()

    xm = LRMatch(pcat, scat)
    match = xm.run(prior_method='random')

    assert len(match) >= len(pcat)
    assert all(match['prob_has_match'] >= 0)
    assert all(match['prob_has_match'] <= 1)
    assert all(match['prob_this_match'] >= 0)
    assert all(match['prob_this_match'] <= 1)

    match_mask = ~match['LR_BEST'].mask
    assert all(match['LR_BEST'][match_mask] >= 0)
    assert all(match['Separation_pcat_scat'][match_mask] >= 0)

def test_lr_maskprior():
    pcat, scat = set_catalogues()

    xm = LRMatch(pcat, scat)
    match = xm.run(prior_method='mask')

    assert len(match) >= len(pcat)
    assert all(match['prob_has_match'] >= 0)
    assert all(match['prob_has_match'] <= 1)
    assert all(match['prob_this_match'] >= 0)
    assert all(match['prob_this_match'] <= 1)

    match_mask = ~match['LR_BEST'].mask
    assert all(match['LR_BEST'][match_mask] >= 0)
    assert all(match['Separation_pcat_scat'][match_mask] >= 0)


""" This is a modified version of
    https://github.com/ifeherva/optimizer-benchmark/blob/master/optimizers/__init__.py   """

import argparse
import torch.optim as optim
import math

from .coolmom_pytorch import Coolmomentum

__all__ = ['parse_optimizer', 'supported_optimizers']

optimizer_defaults = {
    'coolmomentum': (Coolmomentum, 'Coolmomentum', {
        'lr': 0.01,
        'momentum': 0.99,
        'weight_decay': 5e-4,
        'beta': (1 - 0.99)**(1/(200*math.ceil(50000/128))),
        'dropout': 0.0,
    })
}

def supported_optimizers():
    return list(optimizer_defaults.keys())

def required_length(nargs):
    class RequiredLength(argparse.Action):
        def __call__(self, parser, args, values, option_string=None):
            if len(values) != nargs:
                msg = 'argument "{}" requires exactly {} arguments'.format(self.dest, nargs)
                raise argparse.ArgumentTypeError(msg)
            setattr(args, self.dest, values)

    return RequiredLength

def parse_optim_args(args, default_args):
    parser = argparse.ArgumentParser(description='Optimizer parser')
    for k, v in default_args.items():
        if type(v) == bool:
            kwargs = {'action': 'store_false' if v else 'store_true'}
        elif type(v) == list:
            kwargs = {'type': type(v[0]), 'nargs': '+', 'default': v}
        elif type(v) == tuple:
            kwargs = {'type': type(v[0]), 'nargs': '+', 'action': required_length(len(v)), 'default': v}
        else:
            kwargs = {'type': type(v), 'default': v}
        parser.add_argument('--{}'.format(k), **kwargs)
    opt = parser.parse_args(args)

    opt_params_name = ''
    for k, v in default_args.items():
        if opt.__getattribute__(k) != v:
            param_format = '' if type(v) == bool else '_{}'.format(opt.__getattribute__(k))
            opt_params_name += '_{}{}'.format(k, param_format)

    return opt, opt_params_name

def parse_optimizer(optimizer, optim_args, model_params):
    if optimizer not in optimizer_defaults:
        raise RuntimeError('Optimizer {} is not supported'.format(optimizer))

    optim_func, optim_name, def_params = optimizer_defaults[optimizer]

    optim_opts, opt_name = parse_optim_args(optim_args, def_params)
    opt_name = '{}{}'.format(optim_name, opt_name)
    return optim_func(model_params, **vars(optim_opts)), opt_name


from django.contrib.auth.models import User
from django.http import HttpResponse
from django.shortcuts import redirect, render

from .HospitalDBConnect import *

def home(request):
    ''' the home for hosptial admins '''

    appt_count = view_appt_count()
    doc_count = view_doc_count()
    room_count = view_room_count()

    appt_count = appt_count[0]['count(PatientID)']
    doc_count = doc_count[0]['count(DocID)']
    room_count = room_count[0]['count(RoomNumber)']

    context = {
        "appt_count": appt_count,
        'doc_count': doc_count,
        'room_count': room_count
    }
    # handle get only
    return HttpResponse(render(request, 'Hosptial/home.html', context))

def search(request):
    ''' search for firstname, lastname, email '''

    # handle post
    if request.method == 'POST':

        # get data
        first_name = request.POST.get("firstName", "")
        last_name = request.POST.get("lastName", "")

        matching_patients = view_person_search_count(first_name, last_name)
        context = {'patients': matching_patients}

        # reorganize data and pass as context
        return HttpResponse(render(request, 'Hosptial/search.html', context))

    else:
        return HttpResponse(render(request, 'Hosptial/search.html'))

def appointments(request):
    ''' handles appointments '''

    # get all appointments
    all_appointments = view_Appointments()
    context = {'appts': all_appointments}

    return HttpResponse(render(request, 'Hosptial/appointments.html', context))

def profile(request, patient_id):
    ''' shows patient profile '''

    # get the data for that id
    patient_profile = view_history(patient_id)
    context = {"profile": patient_profile}

    return HttpResponse(render(request, 'Hosptial/profile.html', context))

def treatment(request, patient_id):
    ''' Handles treatment stuff '''

    # handle post, redirect to patient profile
    if request.method == 'POST':

        doc_id = request.POST.get("doctorName", "")
        aliment = request.POST.get("aliment", "")
        pre_date = request.POST.get("pdate", "")
        expected = request.POST.get("expected", "")
        warnings = request.POST.get("warnings", "")

        InsertTreatment(aliment, str(pre_date), str(expected), str(warnings))
        return redirect('/hosptial/profile/' + patient_id)

    else:
        # get profile data for side display
        patient_profile = view_history(patient_id)

        # get all doctors
        all_doctors = view_Doctors()

        # set context
        context = {"profile": patient_profile[-1], "doctors": all_doctors}

        return HttpResponse(
            render(request, 'Hosptial/create_treatment.html', context))

def update_appointment(request, patient_id, doc_id):
    ''' handles appointments update '''

    # handle get
    if request.method == 'POST':
        prefered_doctor = request.POST.get("preferedDoctor", "")
        prefered_date = request.POST.get("preferedDate", "")
        reason = request.POST.get("reason", "")

        Update_Appointment(
            int(patient_id), int(doc_id.strip('/')), int(prefered_doctor),
            str(prefered_date), reason)
        return redirect('/hosptial/appointments/')

    else:

        # get old apt data and pass it as context

        # get all doctors
        all_doctors = view_Doctors()
        context = {
            'doctors': all_doctors,
            'PatientID': patient_id,
            'DocID': doc_id
        }
        return HttpResponse(
            render(request, 'Hosptial/update_appt.html', context))

def patients(request):
    ''' shows all patients and leads to profile '''
    # get data
    all_patients = view_Patients()
    context = {'patients': all_patients}

    return HttpResponse(render(request, 'Hosptial/patients.html', context))

def doctors(request):
    ''' shows all doctors and their info '''

    # get all the data
    all_doctors = view_Doctors()
    context = {'doctors': all_doctors}
    return HttpResponse(render(request, 'Hosptial/doctors.html', context))

def nurses(request):
    ''' shows all doctors and their info '''

    # get all the data
    all_nurses = view_Nurses()
    context = {'nurses': all_nurses}
    return HttpResponse(render(request, 'Hosptial/nurses.html', context))

def employees(request):
    ''' shows all doctors and their info '''

    # get all the data
    all_employees = view_Employees()
    context = {'employees': all_employees}
    return HttpResponse(render(request, 'Hosptial/employees.html', context))

def rooms(request):
    ''' shows info about rooms: handles get only '''

    # get all data and set context
    all_rooms = view_Rooms()
    context = {'rooms': all_rooms}

    return HttpResponse(render(request, 'Hosptial/rooms.html', context))

def departments(request):
    ''' shows dep info: handles get only '''
    # get data
    all_departments = view_Departments()

    context = {'departments': all_departments}

    return HttpResponse(render(request, 'Hosptial/departments.html', context))

def bills(request):
    ''' just shows all the bills '''

    # get all the data
    all_bills = view_Bills()
    context = {'bills': all_bills}
    # print(context)

    return HttpResponse(render(request, 'Hosptial/bills.html', context))

def bills_more(request, billNumber):
    ''' shows the user profile '''
    all_bills = view_Bill_more(billNumber)
    # print (all_bills)
    context = {'bill': all_bills[0]}
    print(context)
    return HttpResponse(render(request, 'Hosptial/bills_more.html', context))

def create_bill(request, patient_id):
    ''' handles the view and creation of bills '''

    if request.method == 'POST':
        due_date = request.POST.get("dueDate", "")
        re_date = request.POST.get("reDate", "")
        amount = request.POST.get("amount", "")
        description = request.POST.get("description", "")

        InsertBill(
            int(patient_id), str(re_date), int(amount), str(description),
            str(due_date))

        return redirect('/hosptial/bills')

    context = {'PatientID': patient_id}
    return HttpResponse(render(request, 'Hosptial/create_bill.html', context))

def delete_bill(request, bill_num):
    ''' deletes a bill given the bill_id '''

    # call the delete function
    DeleteBill(bill_num)

    return redirect('/hosptial/bills')


import sys

from towhee.runtime import register, pipe, ops, accelerate, AutoConfig, AutoPipes
from towhee.data_loader import DataLoader
from towhee.serve.triton import triton_client
from towhee.utils.lazy_import import LazyImport

# Legacy towhee._types
from towhee import types
_types = types  # pylint: disable=protected-access
sys.modules['towhee._types'] = sys.modules['towhee.types']

datacollection = LazyImport('datacollection', globals(), 'towhee.datacollection')
server_builder = LazyImport('server_builder', globals(), 'towhee.serve.server_builder')
api_service = LazyImport('api_service', globals(), 'towhee.serve.api_service')

__all__ = [
    'dataset',
    'pipe',
    'triton_client',
    'AutoConfig',
    'build_docker_image',
    'build_pipeline_model',
    'AutoConfig',
    'AutoPipes',
    'DataLoader'
]

__import__('pkg_resources').declare_namespace(__name__)

def build_docker_image(
        dc_pipeline: 'towhee.RuntimePipeline',
        image_name: str,
        cuda_version: str,
        format_priority: list,
        parallelism: int = 8,
        inference_server: str = 'triton',
    ):
    """
    Wrapper for lazy import build_docker_image.

    Args:
        dc_pipeline ('towhee.RuntimPipeline'):
            The pipeline to build as a model in the docker image.
        image_name (`str`):
            The name of the docker image.
        cuda_version (`str`):
            Cuda version.
        format_priority (`list`):
            The priority order of the model format.
        parallelism (`int`):
            The parallel number.
        inference_server (`str`):
            The inference server.

    Examples:
        >>> import towhee
        >>> from towhee import pipe, ops

        >>> p = (
        ...     pipe.input('url')
        ...         .map('url', 'image', ops.image_decode.cv2_rgb())
        ...         .map('image', 'vec', ops.image_embedding.timm(model_name='resnet50'))
        ...         .output('vec')
        ... )

        >>> towhee.build_docker_image(
        ...     dc_pipeline=p,
        ...     image_name='clip:v1',
        ...     cuda_version='11.7',
        ...     format_priority=['onnx'],
        ...     parallelism=4,
        ...     inference_server='triton'
        ... )
    """
    return server_builder.build_docker_image(dc_pipeline, image_name, cuda_version, format_priority, parallelism, inference_server)

def build_pipeline_model(
        dc_pipeline: 'towhee.RuntimePipeline',
        model_root: str,
        format_priority: list,
        parallelism: int = 8,
        server: str = 'triton'
    ):
    """
    Wrapper for lazy import build_pipeline_model.

    Args:
        dc_pipeline ('towhee.RuntimePipeline'):
            The piepline to build as a model.
        model_root (`str`):
            The model root path.
        format_priority (`list`):
            The priority order of the model format.
        parallelism (`int`):
            The parallel number.
        server (`str`):
            The server type.

    Examples:
        >>> import towhee
        >>> from towhee import pipe, ops

        >>> p = (
        ...     pipe.input('url')
        ...         .map('url', 'image', ops.image_decode.cv2_rgb())
        ...         .map('image', 'vec', ops.image_embedding.timm(model_name='resnet50'))
        ...         .output('vec')
        ... )

        >>> towhee.build_pipeline_model(
        ...     dc_pipeline=p,
        ...     model_root='models',
        ...     format_priority=['onnx'],
        ...     parallelism=4,
        ...     server='triton'
        ... )
    """
    return server_builder.build_pipeline_model(dc_pipeline, model_root, format_priority, parallelism, server)

def DataCollection(data):  # pylint: disable=invalid-name
    """
    Wrapper for lazy import DataCollection

    DataCollection is a pythonic computation and processing framework for unstructured
    data in machine learning and data science. It allows a data scientist or researcher
    to assemble data processing pipelines and do their model work (embedding,
    transforming, or classification) with a method-chaining style API.

    Args:
        data ('towhee.runtime.DataQueue'):
            The data to be stored in DataColletion in the form of DataQueue.
    """
    return datacollection.DataCollection(data)

def dataset(name: str, *args, **kwargs) -> 'TorchDataSet':
    """Get a dataset by name, and pass into the custom params.
    Args:
        name (str): Name of a dataset.
        *args (any): Arguments of the dataset construct method.
        **kwargs (any): Keyword arguments of the dataset construct method.
    Returns:
        TorchDataSet: The corresponding `TorchDataSet`.
    Examples:
        >>> from towhee import dataset
        >>> type(dataset('fake', size=10))
        <class 'towhee.data.dataset.dataset.TorchDataSet'>
    """
    from torchvision import datasets
    from towhee.data.dataset.dataset import TorchDataSet
    dataset_construct_map = {
        'mnist': datasets.MNIST, 'cifar10': datasets.cifar.CIFAR10, 'fake': datasets.FakeData
        # 'imdb': IMDB  # ,()
    }
    torch_dataset = dataset_construct_map[name](*args, **kwargs)
    return TorchDataSet(torch_dataset)


import cv2, math
from math import *
import numpy as np
import sys
from decimal import *
sys.path.append("../")
from libs.configs import cfgs
IMG_LOW = 1100
black = (0,0,0)
red = (0, 0, 255)

def convert_rect_origin(rect):
    if rect[4] == 90 or rect[4] == -90:
        new_rect = [rect[0],rect[1],rect[3],rect[2], 0]
    elif rect[4] > 0:
        new_rect = [rect[0], rect[1], rect[3], rect[2], -90 + rect[4]]
        # new_rect = (rect[0], (rect[1][1], rect[1][0]), -90 + rect[2])
    elif rect[4] < 0:
        new_rect = [rect[0],rect[1], rect[3], rect[2], 90 + rect[4]]
        # new_rect = (rect[0], (rect[1][1], rect[1][0]), 90 + rect[2])
    else: #rect[2] == 0
        new_rect = [rect[0],rect[1], rect[3], rect[2], 0]
        # new_rect = (rect[0], (rect[1][1], rect[1][0]), 0)
    return new_rect

#boxの向きを写真の下までの直線の点を得る
def check_rect_line(img, rect):
    # print(img.shape)
    bb = ((rect[0], rect[1]), (rect[2], rect[3]), rect[4])
    # print(rect)
    # print(img.shape)
    if bb[2] > 0:
        flag = 1
    else:
        flag = -1
    x = bb[0][0]
    yoko, tate = hanbun(bb)
    h = img.shape[0]-rect[1]
    if bb[2] == 0 or abs(bb[2]) == 90:
        a = x - yoko
        b = x + yoko
    else:
        a = x + flag * h*cos(radians(abs(bb[2])))/sin(radians(abs(bb[2])))
        b = x + flag * -1 * yoko

    if a < b:
        xmin = a
        xmax = b
    else:
        xmin = b
        xmax = a
    # img = cv2.line(img,(bb[0][0],bb[0][1]),(int(a),924),(0,255,0),5)
    return img, a

#この関数に投げたら、適したboxに変更してくれる
def check_rect(img, rect):
    rect2 = convert_rect_origin(rect)
    img, a  = check_rect_line(img, rect)
    img, a2 = check_rect_line(img, rect2)
    diff  = abs(a  - rect[0])
    diff2 = abs(a2 - rect[0])
    if diff > diff2:
        return img, rect2
    else:
        return img, rect

def calc_x_range(img, rect):
    # print(img.shape)
    x_min_maxs = []
    for i in range(cfgs.STRIDE_NUM):
        img, rect = check_rect(img, rect)
        bb = ((rect[0] + (i-2) * cfgs.STRIDE , rect[1]), (rect[2], rect[3]), rect[4])
        # print(rect)
        # print(img.shape)
        if bb[2] > 0:
            flag = 1
        else:
            flag = -1
        x = bb[0][0]
        yoko, tate = hanbun(bb)
        h = IMG_LOW-rect[1]
        thre = 950
        if h > thre:
            h = thre
        if bb[2] == 0 or abs(bb[2]) == 90:
            a = x - yoko
            b = x + yoko
        else:
            # a = x + flag * (img.shape[0]-rect[1])*cos(radians(abs(bb[2])))/sin(radians(abs(bb[2])))
            a = x + flag * h *cos(radians(abs(bb[2])))/sin(radians(abs(bb[2])))
            b = x + flag * -1 * yoko

        if a < b:
            xmin = a
            xmax = b
        else:
            xmin = b
            xmax = a
        x_min_maxs.append([xmin, xmax])
    img = cv2.line(img,(bb[0][0],bb[0][1]),(int(a),950),(0,255,0),5)
    return img, x_min_maxs

def hanbun(rect):
    if rect[2] == 0:
        yoko_hanbun = 0.5*rect[1][0]
        tate_hanbun = 0.5*rect[1][1]
    elif rect[2] == 90 or rect[2] == -90:
        yoko_hanbun = 0.5*rect[1][1]
        tate_hanbun = 0.5*rect[1][0]
    else:
        yoko_hanbun = 0.5*(rect[1][0]*cos(radians(abs(rect[2]))) + rect[1][1]*sin(radians(abs(rect[2]))))
        tate_hanbun = 0.5*(rect[1][0]*sin(radians(abs(rect[2]))) + rect[1][1]*cos(radians(abs(rect[2]))))
	# elif rect[2] < 0:
	# 	yoko_hanbun = 0.5*(rect[1][0]*cos(radians(abs(rect[2]))) + rect[1][1]*sin(radians(abs(rect[2]))))
	# 	tate_hanbun = 0.5*(rect[1][0]*sin(radians(abs(rect[2]))) + rect[1][1]*cos(radians(abs(rect[2]))))
	# else:
	# 	yoko_hanbun = 0.5*(rect[1][1]*sin(radians(abs(rect[2]))) + rect[1][0]*sin(radians(abs(rect[2]))))
	# 	tate_hanbun = 0.5*(rect[1][1]*cos(radians(abs(rect[2]))) + rect[1][0]*cos(radians(abs(rect[2]))))
    return [yoko_hanbun, tate_hanbun]

def convert_rect(rect):
	if rect[2] == 90 or rect[2] == -90:
		new_rect = (rect[0], (rect[1][1], rect[1][0]), 0)
	elif rect[2] > 0:
		new_rect = (rect[0], (rect[1][1], rect[1][0]), -90 + rect[2])
	elif rect[2] < 0:
		new_rect = (rect[0], (rect[1][1], rect[1][0]), 90 + rect[2])
	else: #rect[2] == 0
		new_rect = (rect[0], (rect[1][1], rect[1][0]), 0)
	return new_rect

def calc_book_range(img, rect):
    return_boxes = []
    for i in range(cfgs.STRIDE_NUM):
        print(rect)
        img, rect = check_rect(img, rect)
        print(rect)
        bb = ((rect[0] + (i-2) * cfgs.STRIDE , rect[1]), (rect[2], rect[3]), rect[4])
        if bb[2] > 0:
            flag = 1
        else:
            flag = -1
        x,y = bb[0][0], bb[0][1]
        yoko, tate = hanbun(bb)
        print("tate : " + str(tate))
        print("yoko : " + str(yoko))
        h2 = IMG_LOW-rect[1]

        thre = img.shape[0]
        print("tres : " + str(thre))

        if h2 > thre:
            h2 = thre
        if bb[2] == 0 or abs(bb[2]) == 90:
            if bb[2] == 0:
                bb = (bb[0], (bb[1][1], bb[1][0]), 90)
            h1 = tate
            line_h = h1 + h2
            line_w = 1000000
            cnt_x = bb[0][0]
            cnt_y = bb[0][1] + line_h/2-h1
            line_norm = line_h

        else:
            h1 = yoko * tan(radians(abs(bb[2])))
            line_h = h1 + h2
            line_w = line_h / tan(radians(abs(bb[2])))
            line_norm = line_h / sin(radians(abs(bb[2])))
            cnt_x = x + flag * (-yoko + line_w /2)
            cnt_y = y - h1 + line_h /2

        print("cnt_x : " + str(cnt_x))
        print("cnt_y : " + str(cnt_y))
        print("line_h : " + str(line_h))
        print("line_w : " + str(line_w))
        print("line_norm : " + str(line_norm))
        img = cv2.line(img, (0, int(cnt_y + line_h/2)), (800, int(cnt_y + line_h/2)), (255,0,0), 5)
        img = cv2.circle(img,(int(cnt_x), int(cnt_y)), 6, (0,255,0), -1)

        box1 = ((cnt_x, cnt_y), (line_norm, bb[1][1]), bb[2])
        # box1 = [cnt_x, cnt_y, bb[1][1], line_norm,bb[2]]
        # img, box1 = check_rect(img, box1)
        # box1 = ((box1[0], box1[1]), (box1[2], box1[3]), box1[4])
        box = cv2.boxPoints(box1)
        box = np.int0(box)
        img = cv2.drawContours(img,[box],-1,black,2)
        return_boxes.append(box1)
    # img = cv2.line(img,(bb[0][0],bb[0][1]),(int(a),950),(0,255,0),5)
    return img, return_boxes

# rect = [500, 500, 50, 100, 80]
# rect1= [503, 800, 100, 50, 10]
# rect2 = ((rect[0], rect[1]), (rect[2], rect[3]), rect[4])
# rect1 = ((rect1[0], rect1[1]), (rect1[2], rect1[3]), rect1[4])
#
# img = cv2.imread("hon.jpg")
# img = cv2.circle(img,(int(rect1[0][0]), int(rect1[0][1])), 3, (0,255,0), -1)
# img,x = calc_x_range(img, rect)
# box = cv2.boxPoints(rect2)
# box = np.int0(box)
# img = cv2.drawContours(img,[box],-1,red,2)
# box = cv2.boxPoints(rect1)
# box = np.int0(box)
# img = cv2.drawContours(img,[box],-1,red,2)
# img, boxes = calc_book_range(img, rect)
# area_bb2 = rect1[1][0] * rect1[1][1]
#
# for i in range(len(boxes)):
#     int_pts = cv2.rotatedRectangleIntersection(boxes[i], rect1)[1]
#     inter = 0.0
#     if int_pts is not None:
#         #convexhull は　凸法を計算
#         order_pts = cv2.convexHull(int_pts, returnPoints=True)
#         #order_ptsの面積を計算
#         int_area = cv2.contourArea(order_pts)
#         inter = int_area * 1.0 / area_bb2
#         print(inter)
# # cv2.imshow("te3", img)
# cv2.imwrite("res.jpg", img)
# cv2.waitKey(0)

# rect = np.array([500, 500, 50, 100, 80])
# rect1= np.array([503, 800, 100, 50, 10])
rect = np.array([1,1])
rect1= np.array([1,1])
rects = np.array([rect, rect1])
print(np.var(rects))


import os
import time

import numpy as np
import tensorflow as tf

import avod
from avod.core import trainer_utils
import avod.builders.config_builder_util as config_builder
from avod.builders.dataset_builder import DatasetBuilder
from avod.core.models.avod_model import AvodModel
from avod.core.models.dt_avod_model import DtAvodModel
from avod.core.dt_inference_utils import get_avod_pred, \
                                convert_pred_to_kitti_format

from wavedata.tools.obj_detection.evaluation import three_d_iou

os.environ['CUDA_VISIBLE_DEVICES'] = '1'

def build_dataset(dataset_config, dataset_name='tracking'):
    # Overwrite the defaults
    dataset_config = config_builder.proto_to_obj(dataset_config)
    dataset_config.data_split = 'test'
    dataset_config.data_split_dir = 'testing'
    dataset_config.has_labels = False
    # Remove augmentation during evaluation in test mode
    dataset_config.aug_list = []
     # Build the dataset object
    if dataset_name == 'tracking':
        dataset = DatasetBuilder.build_kitti_tracking_dataset(dataset_config,
                                                        use_defaults=False)
    else:
        dataset = DatasetBuilder.build_kitti_dataset(dataset_config,
                                                    use_defaults=False)
    return dataset

def model_setup(model_config, dataset, dataset_name='tracking'):
    # Overwrite repeated field
    model_config = config_builder.proto_to_obj(model_config)
    # Switch path drop off during evaluation
    model_config.path_drop_probabilities = [1.0, 1.0]
    if dataset_name == 'tracking':
        model = DtAvodModel(model_config, train_val_test='test', dataset=dataset)
    else:
        model = AvodModel(model_config, train_val_test='test', dataset=dataset)
    return model

def create_sesson(model_config, cpkt_idx):
    checkpoint_dir = model_config.paths_config.checkpoint_dir
    saver = tf.train.Saver()
    trainer_utils.load_checkpoints(checkpoint_dir, saver)
    checkpoint_to_restore = saver.last_checkpoints[cpkt_idx]

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)

    saver.restore(sess, checkpoint_to_restore)
    return sess

def get_kitti_pred(predictions, idx, dataset, box_rep):
    all_pred = get_avod_pred(predictions, box_rep)
    frame_mask_0 = np.where(all_pred[:, -1] == 0)[0]
    frame_mask_1 = np.where(all_pred[:, -1] == 1)[0]
    pred_frame_0 = all_pred[frame_mask_0]
    pred_frame_1 = all_pred[frame_mask_1]
    sample_name_0 = dataset.sample_names[idx][0]
    sample_name_1 = dataset.sample_names[idx][1]
    score_threshold = 0.6
    pred_frame_kitti_0 = convert_pred_to_kitti_format(
        pred_frame_0[:, :9], sample_name_0, dataset, score_threshold)
    pred_frame_kitti_1 = convert_pred_to_kitti_format(
        pred_frame_1[:, :9], sample_name_1, dataset, score_threshold)

    mask = pred_frame_0[:, 7] >= score_threshold
    corr_offset = pred_frame_0[mask][:, 9:12]
    return pred_frame_kitti_0, pred_frame_kitti_1, corr_offset

def cal_iou_mat(kitti_pred1, kitti_pred2, offsets):
    # [l, w, h, x, y, z, ry]
    box3d_1 = np.array(kitti_pred1[:, 8:16], dtype=np.float32)
    box3d_2 = np.array(kitti_pred2[:, 8:16], dtype=np.float32)
    # frame 1 add offsets
    box3d_1[:, 3:6] = box3d_1[:, 3:6]  + offsets
    # convert to [ry, l, h, w, tx, ty, tz]
    box3d_1 = box3d_1[:, [-2, 0, 2, 1, 3, 4, 5]]
    box3d_2 = box3d_2[:, [-2, 0, 2, 1, 3, 4, 5]]
    iou_3d = np.zeros((box3d_1.shape[0], box3d_2.shape[0]), dtype=np.float32)
    for i in range(len(box3d_1)):
        iou_3d[i] = three_d_iou(box3d_1[i], box3d_2)
    return iou_3d

def get_config(checkpoint_name):
    # Read the config from the experiment folder
    experiment_config_path = avod.root_dir() + '/data/outputs/' +\
            checkpoint_name + '/' + checkpoint_name + '.config'

    model_config, _, eval_config, dataset_config = \
            config_builder.get_configs_from_pipeline_file(
                experiment_config_path, dataset_name='', is_training=False)
    return model_config, dataset_config

checkpoint_name = 'pyramid_cars_with_aug_dt_5_tracking_test'
# checkpoint_name = 'pyramid_cars_with_aug_example'
dataset_name = 'tracking'
model_config, dataset_config = get_config(checkpoint_name)
dataset = build_dataset(dataset_config, dataset_name)
box_rep = model_config.avod_config.avod_box_representation

with tf.Graph().as_default():
    model = model_setup(model_config, dataset, dataset_name)
    prediction_dict = model.build()
    sess = create_sesson(model_config, cpkt_idx=10)
    times = []
    for i in range(200):
        feed_dict = model.create_feed_dict(sample_index=i)
        start = time.time()
        predictions = sess.run(prediction_dict, feed_dict=feed_dict)
        end = time.time()
        times.append(end-start)
        # pred_frame_kitti_0, pred_frame_kitti_1, corr_offset = \
        #     get_kitti_pred(predictions, i, dataset, box_rep)
        # iou_3d = cal_iou_mat(pred_frame_kitti_0, pred_frame_kitti_1, corr_offset)
        # print(iou_3d)
    print(np.min(times))


import pandas as pd
import plotly as py
import numpy as np
from plotly.graph_objs import *
from os import path

trace1 = Choropleth(
    z=['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'],
    showlegend=True,
    autocolorscale=False,
    colorscale=[[0, 'rgb(255,255,255)'], [1, '#a0db8e']],
    hoverinfo='text',
    locationmode='USA-states',
    locations=['AL','FL','GA','MS','NC','SC','TN'],
    name='Southeast',
    text='Southeast',
    showscale=False,
    zauto=False,
    zmax=1,
    zmin=0,
    marker=dict(line=dict(color='white')),

)
trace2 = Choropleth(
    z=['1', '1'],
    autocolorscale=False,
    colorscale=[[0, 'rgb(255,255,255)'], [1, 'rgb(255,167,0)']],
    hoverinfo='text',
    locationmode='USA-states',
    locations=['LA','TX'],
    name='Gulf',
    showscale=False,
    zauto=False,
    zmax=1,
    zmin=0,
    marker=dict(line=dict(color='white'))
)
trace3 = Choropleth(
    z=['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1','1','1','1'],
    autocolorscale=False,
    colorscale=[[0, 'rgb(255,255,255)'], [1, 'rgb(141,85,36)']],
    hoverinfo='text',
    locationmode='USA-states',
    locations=['AR','IL','IN','IA','KS','MI','MN','MO','NE','ND','OK','SD','WI'],
    name='Midwest',
    text='Midwest',
    showscale=False,
    zauto=False,
    zmax=1,
    zmin=0,
    marker=dict(line=dict(color='white'))
)

trace4 = Choropleth(
    z=['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1','1','1','1','1','1'],
    autocolorscale=False,
    colorscale=[[0, 'rgb(255,255,255)'], [1, 'rgb(241,194,125)']],
    hoverinfo='text',
    locationmode='USA-states',
    locations=['CT','KY','ME','MA','NH','NJ','NY','OH','PA','RI','VT','DE','MD','VA','WV'],
    name='Northeast',
    legendgroup='Northeast',
    showscale=False,
    zauto=False,
    zmax=1,
    zmin=0,
    marker=dict(line=dict(color='white'))
)

trace5 = Choropleth(
    z=['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'],
    autocolorscale=False,
    colorscale=[[0, 'rgb(255,255,255)'], [1, '#7aceeb']],
    hoverinfo='text',
    locationmode='USA-states',
    locations=['AZ','CA','CO','ID','MT','NV','NM','OR','UT','WA','WY'],
    name='Western',
    showscale=False,
    zauto=False,
    zmax=1,
    zmin=0,
    marker=dict(line=dict(color='white')),
    showlegend=True
)

layout = Layout(
    geo=dict(
        countrycolor='rgb(102, 102, 102)',
        countrywidth=0.1,
        lakecolor='rgb(255, 255, 255)',
        landcolor='rgba(255, 255, 255, 0.28)',
        lonaxis=dict(
            gridwidth=1.5999999999999999,
            range=[-180, -50],
            showgrid=False
        ),
        projection=dict(
            type='albers usa'
        ),
        scope='usa',
        showland=True,
        showrivers=False,
        showsubunits=True,
    ),

    showlegend=True,
    title='Federal Energy Regulatory Commission (FERC) Natural Gas Market Classification',
    legend = dict(
           traceorder = 'reversed'
    )
)
fig = dict( data=([trace1, trace2, trace3, trace4, trace5]), layout=layout )
# py.plotly.image.save_as(fig, filename='US_map.png')
py.offline.plot(fig, validate=False, filename=path.basename(__file__)+".html")


import math
import logging
from datetime import datetime

from drive_controller import DrivingController

# 제한 속도
SPEED_LIMIT = 100

logging.basicConfig(filename='{}.log'.format(datetime.now().strftime('%Y-%m-%d-%H-%M')), level=logging.DEBUG)

class DrivingClient(DrivingController):
    def __init__(self):
        # =========================================================== #
        #  Area for member variables =============================== #
        # =========================================================== #
        # Editing area starts from here
        #

        self.is_debug = False
        self.collision_flag = False

        #
        # Editing area ends
        # ==========================================================#
        super().__init__()

    def control_driving(self, car_controls, sensing_info):

        # =========================================================== #
        # Area for writing code about driving rule ================= #
        # =========================================================== #
        # Editing area starts from here
        #

        logging.debug("=========================================================")
        logging.debug("to middle: {}".format(sensing_info.to_middle))

        logging.debug("collided: {}".format(sensing_info.collided))
        logging.debug("car speed: {} km/h".format(sensing_info.speed))

        logging.debug("is moving forward: {}".format(sensing_info.moving_forward))
        logging.debug("moving angle: {}".format(sensing_info.moving_angle))
        logging.debug("lap_progress: {}".format(sensing_info.lap_progress))

        logging.debug("track_forward_angles: {}".format(sensing_info.track_forward_angles))
        logging.debug("track_forward_obstacles: {}".format(sensing_info.track_forward_obstacles))
        logging.debug("opponent_cars_info: {}".format(sensing_info.opponent_cars_info))
        logging.debug("=========================================================")

        ###########################################################################
        emergency_break = False
        # 1. 박았는지 상태 체크
        # 1-1. 장애물에 박았을 때
        if is_collided(self, sensing_info):
            car_controls.steering = -1 * avoid_obstacles(self, sensing_info)
            car_controls.throttle = -1
        # 1-2. 도로 밖 팬스에 박았을 때
        else:
            # 2. 피해야할 대상이 있는지 확인
            # 2-1. 피해야할 장애물이 있는지 확인
            if is_avoid_obstacles(sensing_info):
                car_controls.steering = avoid_obstacles(self, sensing_info)
            # 2-2. 피해야할 상대 자동차가 있는지 확인

            # 3. 직선, 코너 주행 확인
            else:
                for i in range(2):
                    if abs(sensing_info.track_forward_angles[i]) > 20:
                        emergency_break = True
                        break

                angle = -(sensing_info.moving_angle - sensing_info.track_forward_angles[0]) / 110
                middle = -sensing_info.to_middle / 50 if abs(sensing_info.to_middle) > self.half_road_limit - 3 else 0

                # 각 앵글값 및 미들값으로 구한 핸들값 중 더 큰 값을 선택
                # car_controls.steering = angle if abs(angle) > abs(middle) else middle
                car_controls.steering = angle + middle if angle + middle < 1 else 1
                if emergency_break:
                    car_controls.steering = car_controls.steering + (sensing_info.speed / 250) if car_controls.steering > 0 else car_controls.steering - (sensing_info.speed / 250)
            # 4. 상대 차량이 있다면 추월 가능한지

            # 제한 속도 이상이면 악셀값 조정
            car_controls.throttle = 0 if sensing_info.speed > SPEED_LIMIT else 1
            car_controls.brake = 0
            if emergency_break and car_controls.throttle == 1 and sensing_info.speed > 60:
                car_controls.throttle = 0

        logging.debug("steering:{}, throttle:{}, brake:{}".format(car_controls.steering, car_controls.throttle, car_controls.brake))

        #
        # Editing area ends
        # ==========================================================#
        return car_controls

    # ============================
    # If you have NOT changed the <settings.json> file
    # ===> player_name = ""
    #
    # If you changed the <settings.json> file
    # ===> player_name = "My car name" (specified in the json file)  ex) Car1
    # ============================
    def set_player_name(self):
        player_name = ""
        return player_name

# 1-1. 장애물에 박았을 때
def is_collided(self, sensing_info):
    if len(sensing_info.track_forward_obstacles) > 0:
        if self.collision_flag:
            if sensing_info.track_forward_obstacles[0]['dist'] < 10 and sensing_info.speed < 10:
                return True
        if sensing_info.collided:
            self.collision_flag = True
            return True
    self.collision_flag = False
    return False

# 피해야할 장애물이 있는지 확인
def is_avoid_obstacles(sensing_info):
    # 전방에 장애물이 하나이상 있는지 확인
    if len(sensing_info.track_forward_obstacles) > 0:
        # 가장 가까운 장애물과의 거리 확인
        if 5 < sensing_info.track_forward_obstacles[0]['dist'] < sensing_info.speed * 0.8:
            logging.debug("장애물 발견")
            print("장애물 발견")
            # 피하지 않아도 되는지 확인
            tangent = math.tan(math.radians(sensing_info.moving_angle))
            temp_to_middle = sensing_info.track_forward_obstacles[0]['dist'] * tangent
            if abs(sensing_info.to_middle + temp_to_middle - sensing_info.track_forward_obstacles[0]['to_middle']) > 3:
                logging.debug("그냥 가세요")
                print("그냥 가세요")
                return False
            logging.debug("피해라")
            logging.debug("temp_to_middle:{}".format(temp_to_middle))
            print("피해라")
            return True
    return False

# 기본적으로 장애물의 반대로 이동, 중간에 위치할 경우 왼쪽 or 오른쪽 구석으로 이동
def avoid_obstacles(self, sensing_info):
    steering = -sensing_info.track_forward_obstacles[0]['to_middle'] / 50
    if abs(sensing_info.track_forward_obstacles[0]['to_middle']) < 2.5:
        if sensing_info.to_middle > 0:
            steering = (sensing_info.to_middle + (self.half_road_limit / 3)) / 60
        else:
            steering = (sensing_info.to_middle - (self.half_road_limit / 3)) / 60
    return steering

if __name__ == '__main__':
    client = DrivingClient()
    client.run()


# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from ncar.items import NcarItem

class GetsomeSpider(CrawlSpider):
    global li
    li = list()
    name = 'getsome'
    allowed_domains = ['ncar.cc']
    start_urls = ['http://bbs.ncar.cc/forum.php?mod=forumdisplay&fid=129']

    rules = (
        Rule(LinkExtractor(restrict_xpaths="//div[@class='xbs xbs_4 block move-span']//li/a"), callback='parse_item', follow=True),
    )

    def parse_start_url(self,response):
        for sel in response.xpath("//div[@class='xbs xbs_4 block move-span']//li/a"):
            item = NcarItem()
            item['name'] = sel.xpath("text()").extract()
            item['url'] = sel.xpath("@href").extract()
            item['links'] = dict()
            li.append(item)
#        print li

    def parse_item(self, response):
        for l in li:
#            print response.url.find(l['url'][0]),l['url'][0],response.url,type(l['url']),type(response.url.find(l['url'][0]))
            #if(l['url']==response.url):
            if(response.url.find(l['url'][0])!=-1):
                for sel in response.xpath("//td[@class='t_f']/div[3]/a"):
                    l['links'][sel.xpath("text()").extract()[0]]=sel.xpath("@href").extract()[0]
                yield l