import os
import copy
import numpy as np

from gurobipy import GRB
from scipy import sparse
from clfcbfcontroller import ClfCbfController

FOLDER = os.getcwd().split('\\')[-1]
if FOLDER == 'overtake':
    from overtake_settings import *
elif FOLDER == 'simple':
    from simple_settings import *

FXTS  = True
ALPHA = 1.3
kG    = 1

###############################################################################
################################## Functions ##################################
###############################################################################

def acbf(x,t,alpha=ALPHA):
    if cbf(x,t)[0] >= alpha:
        acbf1 = alpha ** 2
    else:
        acbf1 = (alpha ** 2) - (cbf(x,t)[0] - alpha) ** 2

    if cbf(x,t)[1] >= alpha:
        acbf2 = alpha ** 2
    else:
        acbf2 = (alpha ** 2) - (cbf(x,t)[1] - alpha) ** 2

    return np.array([acbf1,acbf2])

def acbf_partialx(x,t,alpha=ALPHA):
    if cbf(x,t)[0] >= alpha:
        acbf1_partial = np.array([0,0])
    else:
        acbf1_partial = 2 * cbf_partialx(x,t)[0] * (alpha - cbf(x,t)[0])

    if cbf(x,t)[1] >= alpha:
        acbf2_partial = np.array([0,0])
    else:
        acbf2_partial = 2 * cbf_partialx(x,t)[1] * (alpha - cbf(x,t)[1])

    return np.array([acbf1_partial,acbf2_partial])

def acbf_partialpsi(x,t,alpha=ALPHA):
    return np.zeros(x.shape)

def acbf_partialt(x,t,alpha=ALPHA):
    return cbf_partialt(x,t)

def aclf(x,xd):
    return clf(x,xd)

def aclf_partialx(x,xd):
    return clf_partial(x,xd)

def aclf_partialtheta(x,theta):
    return np.zeros(x.shape)

def f_clf(x,t,theta,Gamma):
    return f(x) + regressor(x,t) @ (theta + Gamma @ aclf_partialtheta(x,theta).T)

def f_cbf(x,t,psi,Gamma):
    return np.array([f(x) + regressor(x,t) @ (p - Gamma @ acbf_partialpsi(x,t,p).T) for p in psi])
    return f(x) + regressor(x,t) @ (psi - Gamma @ acbf_partialpsi(x,t,psi).T)

###############################################################################
#################################### Class ####################################
###############################################################################

class TaylorController(ClfCbfController):

    def __init__(self,u_max):
        super().__init__(u_max)

        # Create the decision variables
        self.decision_variables = np.zeros((len(DECISION_VARS),),dtype='object')
        decision_vars_copy      = copy.deepcopy(DECISION_VARS)
        for i,v in enumerate(decision_vars_copy):
            name = v['name']

            # Specify the input constraints as an explict model constraint
            if name == 'ux' or name == 'uy':
                v['lb'] = -1e+100
                v['ub'] =  1e+100

            lb   = v['lb']
            ub   = v['ub']

            self.decision_variables[i] = self.m.addVar(lb=lb,ub=ub,name=name)

        # Set the objective function
        self.m.setObjective(OBJ(self.decision_variables),GRB.MINIMIZE)

        # Relaxation on input constraints
        self.m.addConstr(self.decision_variables[0] <=  U_MAX[0] + self.decision_variables[3])
        self.m.addConstr(self.decision_variables[0] >= -U_MAX[0] - self.decision_variables[3])
        self.m.addConstr(self.decision_variables[1] <=  U_MAX[1] + self.decision_variables[4])
        self.m.addConstr(self.decision_variables[1] >= -U_MAX[1] - self.decision_variables[4])

    def set_initial_conditions(self,**kwargs):
        """ """
        self._set_initial_conditions(**kwargs)

        self.err_max   = self.theta_max - self.theta_min
        c              = np.linalg.norm(self.err_max)
        self.Gamma     = kG * c**2 / (2 * cbf(self.x,self.t)) * np.eye(self.theta_hat.shape[0])

        self.psi_hat   = np.array([self.psi_hat,self.psi_hat]).T

        self.name      = "TAY"

    def update_unknown_parameter_estimates(self):
        """
        """

        # Update theta_hat
        theta_hat_dot  = self.Gamma @ (aclf_partialx(self.x,self.xd) @ regressor(self.x,self.t)).T
        self.theta_hat = self.theta_hat + (self.dt * theta_hat_dot)

        # Update psi_hat
        psi_hat_dot    = self.Gamma @ (-acbf_partialx(self.x,self.t) @ regressor(self.x,self.t)).T
        self.psi_hat   = self.psi_hat + (self.dt * psi_hat_dot)

    def update_qp(self,x,t,multiplier=1):
        """
        """
        # Remove old constraints
        if self.performance is not None:
            self.m.remove(self.performance)
        if self.safety is not None:
            self.m.remove(self.safety)

        # Update CLF and CBF
        self.V   = V    = aclf(x,self.xd)
        self.B   = B    = acbf(x,t)

        # Update Partial Derivatives of CLF and CBF
        self.dV  = dV   = aclf_partialx(x,self.xd)
        self.dBx = dBx  = acbf_partialx(x,t)

        # Impose bounds on Theta set
        thetaV = np.zeros(self.theta_hat.shape)
        psiB   = np.zeros((2,self.theta_hat.shape[0]))
        upper  = self.theta_max
        lower  = self.theta_min

        # Evaluate Lie Derivatives for g, Delta
        self.LgV = LgV  = np.dot(dV,g(x))
        self.LdV = LdV  = np.dot(dV,regressor(x,t))
        self.LgB = LgB  = np.dot(dBx,g(x))
        self.LdB = LdB  = np.dot(dBx,regressor(x,t))

        # Determine supremum of LdV*theta_err
        for i,v in enumerate(LdV):
            thetaV[i]  = lower[i]*(v*lower[i] > v*upper[i]) + upper[i]*(v*lower[i] <= v*upper[i])

        # Determine infimum of LdrB*theta_err
        for i,bb in enumerate(LdB):
            for j,b in enumerate(bb):
                psiB[i,j] = upper[j]*(b*lower[j] > b*upper[j]) + lower[j]*(b*lower[j] <= b*upper[j])

        # Update Lie Derivatives for f based on worst-case theta estimate
        LfV  = np.dot(dV,f_clf(x,t,thetaV,self.Gamma))
        LfB  = np.einsum('ij,ij->i',dBx,f_cbf(x,t,psiB,self.Gamma))

        # CLF (FxTS) Conditions: LfV + LgV*u <= perf_max  + delta0
        if FXTS:
            perf_max = - c1_c*V**(1 - 1/mu_c) - c2_c*V**(1 + 1/mu_c)
        else:
            perf_max = -V

        if np.sum(LgV.shape) > 1:
            p = self.m.addConstr(LfV + np.sum(np.array([gg*self.decision_variables[i] for i,gg in enumerate(LgV)]))
                                 <=
                                 perf_max + self.decision_variables[2]
            )
            self.performance = p
        else:
            p = self.m.addConstr(LfV + LgV*self.decision_variables[0]
                                 <=
                                 perf_max + self.decision_variables[1]
            )
            self.performance = p

        # CBF (Safety) Conditions
        # LfB + LgB*u >= 0
        self.safety = []
        for ii,(ff,gg) in enumerate(zip(LfB,LgB)):
            if np.sum(LgV.shape) > 1:
                s = self.m.addLConstr(ff + np.sum(np.array([g*self.decision_variables[i] for i,g in enumerate(gg)]))
                                      >=
                                      0
                )
                self.safety.append(s)
            else:
                s = self.m.addLConstr(ff + gg*self.decision_variables[0]
                                      >=
                                      0
                )
                self.safety.append(s)

        # Store CBF/CLF values
        self.clf  = clf(x,self.xd)
        self.cbf  = cbf(x,t)

        self.aclf  = V
        self.acbf  = B

        self.LfV = LfV
        self.LgV = LgV
        self.LfB = LfB
        self.LgB = LgB

    def compute(self,
                not_skip: bool = True):
        """ Computes the solution to the Quadratic Program by first updating
        the unknown parameter estimates and then by calling the parent
        Controller._compute method.

        INPUTS
        ------
        not_skip: (bool) - True -> update parameters, False -> do not update

        RETURNS
        ------
        return parameter for Controller._compute()

        """
        if not_skip:
            # Update Unknown Parameter Estimates
            self.update_unknown_parameter_estimates()

        self._compute()

        self.safety_check()

        return self.sol

    def safety_check(self):
        if self.sol is not None:
            perf = -self.LfV - self.LgV[0]*self.sol[0] - self.LgV[1]*self.sol[1] - self.aclf + self.sol[2]
            safe = []
            for ii,(ff,gg) in enumerate(zip(self.LfB,self.LgB)):
                safe.append(ff + gg[0]*self.sol[0] + gg[1]*self.sol[1])
            safe = np.array(safe)

            if (safe < 0).any():
                pass
                # print("Time:        {:.3f}".format(self.t))
                # print("Performance: {} = -{} - {}ux - {}uy - {} + {} ".format(perf,self.LfV,self.LgV[0],self.LgV[1],self.aclf,self.sol[2]))
                # print("Safety1:     {} = {} + {}ux + {}uy".format(safe[0],self.LfB[0],self.LgB[0,0],self.LgB[0,1]))
                # print("Safety2:     {} = {} + {}ux + {}uy".format(safe[1],self.LfB[1],self.LgB[1,0],self.LgB[1,1]))
                # print("(ux,uy): ({},{})".format(self.sol[0],self.sol[1]))


"""
Daemon to collect elasticsearch bulk indexing requests from kafka
and push them into elasticsearch.
"""
import argparse
import logging

import mjolnir.kafka.bulk_daemon

def arg_parser():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        '-b', '--brokers', dest='brokers', required=True, type=str,
        help='Kafka brokers to bootstrap from as a comma separated list of <host>:<port>')
    parser.add_argument(
       '-c', '--es-clusters', dest='es_clusters', required=True, type=str,
       help='Elasticsearch servers to bootstrap from as a comma separated list of <host>:<port>')
    parser.add_argument(
        '-t', '--topic', dest='topics', required=True, type=str, nargs='+',
        help='Kafka topic(s) to read indexing requests from. Multiple topics may be provided.')
    parser.add_argument(
        '-g', '--group-id', dest='group_id', type=str, default='TODO',
        help='Kafka consumer group to join')
    parser.add_argument(
        '--prometheus-port', dest='prometheus_port', default=9170, type=int, required=False,
        help='Port to export prometheus metrics over.')
    return parser

main = mjolnir.kafka.bulk_daemon.run

if __name__ == "__main__":
    logging.basicConfig()
    kwargs = dict(vars(arg_parser().parse_args()))
    main(**kwargs)


#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#   https://openclassrooms.com/courses/apprenez-a-programmer-en-python/les-dictionnaires-2
# Author:      Jean
#
# Created:     26/08/2017
# Copyright:   (c) Jean 2017
# Licence:     <your licence>
#-------------------------------------------------------------------------------

def fonction_inconnue(**parametres_nommes):
     """Fonction permettant de voir comment récupérer les paramètres nommés
     dans un dictionnaire"""
     print("J'ai reçu en paramètres nommés : {}.".format(parametres_nommes))
    #Pour capturer tous les paramètres nommés non précisés dans un dictionnaire,
    #il faut mettre deux étoiles ** avant le nom du paramètre.

def main():
    fruits = {"pommes":21, "melons":3, "poires":31}
    for cle in fruits:
        print(cle)
    fruits = {"pommes":21, "melons":3, "poires":31}
    for cle in fruits.keys():                   #méthode keys()
        print(cle)
    for val in fruits.values():                 #méthode values()
        print(val)

    for cle, valeur in fruits.items():          #méthode items()
        print("La clé {} contient la valeur {}.".format(cle, valeur))

    #Récupérer les paramètres nommés dans un dictionnaire

    fonction_inconnue() # Aucun paramètre

    fonction_inconnue(p=4, j=8)
    #fonction_inconnue(4, 8)             #erreur exception

    """
    Ainsi, pour avoir une fonction qui accepte n'importe quel type de paramètres,
    nommés ou non, dans n'importe quel ordre, dans n'importe quelle quantité,
    il faut la déclarer de cette manière :
    def fonction_inconnue(*en_liste, **en_dictionnaire):
    """

    parametres = {"sep":" >> ", "end":" -\n"}
    #print attend des paramètres nommés sep et end
    print("Voici", "un", "exemple", "d'appel", **parametres)

if __name__ == '__main__':
    main()


#!/usr/bin/env python
#! -*- coding: utf-8 -*-
# Bu araç @keyiflerolsun tarafından | @BetikSonu için yazılmıştır.

from pyrogram import Client, Filters
from time import strftime
import json
from Kaziyicilar.discUdemy import TR, EN

@Client.on_message(Filters.command(['udemy'],['!','.','/']))
def udemy(client, message):
    cevaplanan_mesaj = message.reply_to_message
    if cevaplanan_mesaj is None:
        kekik = message.reply("Bekleyin..")
    else:
        kekik = message.reply("Bekleyin..", reply_to_message_id=cevaplanan_mesaj.message_id)

    girilen_yazi = message.text                                         # komut ile birlikle mesajı tut
    if len(girilen_yazi.split()) == 1:                                  # eğer sadece komut varsa
        kekik.edit("`TR` veya `EN` belirtmeniz gerekir.")
        return

    dil = " ".join(girilen_yazi.split()[1:2]).lower()                   # dil'i komuttan ayrıştır (birinci kelime)

    if str(dil) == "tr":
        mesaj = f"Taranan Adres : `discudemy.com/language/Turkish/1`\n\n"

        try:
            veri = json.load(open("Kaziyicilar/jsonDosyalari/Udemy/TR.json", "r+", encoding='utf8'))
        except FileNotFoundError:
            print("\tTR.json | Dosya Bulunamadı!")
            raise

        for bilgi in veri['udemyTR']:
            mesaj += f"📼 [{bilgi['kurs_adi']}]({bilgi['kurs_linki']})\n"
        mesaj += f"\n\tGüncellenme Zamanı : `{veri['sunucu_saat']}`"

    elif str(dil) == "en":
        mesaj = f"Taranan Adres : `discudemy.com/language/English/1`\n\n"

        try:
            veri = json.load(open("Kaziyicilar/jsonDosyalari/Udemy/EN.json", "r+", encoding='utf8'))
        except FileNotFoundError:
            print("\tEN.json | Dosya Bulunamadı!")
            raise

        for bilgi in veri['udemyEN']:
            mesaj += f"📼 [{bilgi['kurs_adi']}]({bilgi['kurs_linki']})\n"
        mesaj += f"\n\tGüncellenme Zamanı : `{veri['sunucu_saat']}`"

    elif str(dil) == "update":
        kekik.edit('**Udemy Kupon Listesini Güncelliyorum...**\n\n\t__Bu işlem biraz uzun sürebilir.__')
        TR(1); EN(1)
        kekik.edit(f'__Kupon Listesi Güncellendi :)__\n\n\t`{strftime("%d/%m %H:%M:%S")}`')
        return

    else: mesaj = "`TR` veya `EN` belirtmeniz gerekir.!"

    try:
        kekik.edit(mesaj, disable_web_page_preview=True, parse_mode="Markdown")
    except Exception as hata_mesaji:
        kekik.edit(hata_mesaji)


import glob, os
import json

os.chdir("data/")
count = 0
for file in glob.glob("*.json"):
    print(os.path.splitext(file)[0])
    f = open(file)
    json_array = json.load(f)
    if len(json_array) > 15:
        print("Had to split")
        count += 1
    chunkSize = 15

    for i in range(0, len(json_array), chunkSize):
        filename = f"{os.path.splitext(file)[0]}_{str(i // chunkSize)}"
        with open(
            f"./splitted/{filename}.json",
            "w",
        ) as f:
            json.dump(json_array[i : i + chunkSize], f)

print(count)


#-*- encoding:utf-8 -*-
#main object
#weather information
import requests
import json
import re
country=raw_input("城市：")
weather = requests.get("http://api.avatardata.cn/Weather/Query?key=74c44e326f8c4c68b485b04a49f7efa9&cityname=" + country + "&format=false")
swap =str(weather.content)

todaybegin =swap.find('"weather":[')
todayend = swap.find("}", todaybegin)
result = swap[todaybegin + len('"weather":[')+1:todayend]

datebegin = result.find('"date":"')
dateend = result.find('"',datebegin + len('"date":"'))
date = result[datebegin + len('"date":"'):dateend]

weekbegin = result.find('"week":"')
weekend = result.find('"',weekbegin + len('"week":"'))
week = result[weekbegin + len('"week":"'):weekend]
print("日期: " + date + "  星期" + week)

nonglibegin = result.find('"nongli":"')
nongliend = result.find('"',nonglibegin + len('"nongli":"'))
nongli = result[nonglibegin + len('"nongli":"'):nongliend]
print("农历: " + nongli)

daybegin = result.find('"day":[')
dayend = result.find(']',daybegin + len('"day":['))
day = result[daybegin + len('"day":['):dayend]
day.strip('"')
day_list = day.strip(',').split(',')
#得到的字符串两端带有双引号，可以使用eval（）函数去除
print("温度：" + eval(day_list[0]) + " ~~ " + eval(day_list[2]))
print("风向：" + eval(day_list[1]) + "级别：" + eval(day_list[3]))
if len(day_list)>6 :
	print(eval(day_list[6]))


import xlrd
from html.parser import HTMLParser

# SALVAR EM C:/.../emacs-24.3/bin/validar_tabelas/data/
# Movimento > Lista de precos > Lista disponivel

DIR_PREFIX = 'c:/Users/Heitor/Desktop/emacs-24.3/bin/validar_tabelas/data/'

def test():
    print("Run validar(filename...)")

def read_geral(filename_geral):
    cv_geral = {}
    timestamp = ""
    try:
        book = xlrd.open_workbook(filename_geral)
        sh = book.sheet_by_index(0)

        timestamp = sh.cell_value(rowx=1, colx=12)

        nrows = sh.nrows
        for row in range(nrows):
            cod_cell = sh.cell_value(rowx=row, colx=0)

            if isinstance(cod_cell, float):
                cod = str(int(cod_cell))

            elif isinstance(cod_cell, str):
                cod_cell = cod_cell.strip()
                if cod_cell == "" or cod_cell[0] == "C":
                    continue

                cod = cod_cell
            else:
                print(cod_type, "not recognized")

            try:
                cv_cell = sh.cell_value(rowx=row, colx=8)
            except ValueError:
                print("Error getting from lista geral cv cell on row", row)
                cv_cell = -1

            cv_geral[cod] = "%.2f" % cv_cell
    except FileNotFoundError as err:
        print(err)
        print("Could not load all files.")
    return timestamp, cv_geral

def read_xls(filename):
    cv_xls = {}
    disp_xls = {}
    resv_xls = {}

    timestamp = ""

    # cod cv disp resv
    # 0,  8,   9,  10

    try:
        book = xlrd.open_workbook(filename)
        sh = book.sheet_by_index(0)

        timestamp = sh.cell_value(rowx=1, colx=10)

        nrows = sh.nrows
        for row in range(nrows):
            cod_cell = sh.cell_value(rowx=row, colx=0)

            if isinstance(cod_cell, float):
                cod = str(int(cod_cell))

            elif isinstance(cod_cell, str):
                cod_cell = cod_cell.strip()
                if cod_cell == "" or cod_cell[0] == "C":
                    continue

                cod = cod_cell
            else:
                print(cod_type, "not recognized")

            try:
                cv_cell = sh.cell_value(rowx=row, colx=8)
            except ValueError:
                print("Error getting from XLS cv cell on row", row)
                cv_cell = -1

            try:
                disp_cell = int(sh.cell_value(rowx=row, colx=9))
            except ValueError:
                print("Error getting disponivel in xls row", row)
                disp_cell = -1000000

            try:
                resv_cell = int(sh.cell_value(rowx=row, colx=10))
            except ValueError:
                print("Error getting reserva cell in xls row", row)
                resv_cell = -1000000
            cv_left = "%.2f" % (int(cv_cell.split("/")[0].strip()) / 100)
            cv_xls[cod] = cv_left
            disp_xls[cod] = disp_cell
            resv_xls[cod] = resv_cell

    except FileNotFoundError as err:
        print(err)
        print("Could not load all files.")
    return (timestamp, cv_xls, disp_xls, resv_xls)

def find_cod(html_row):
    trial = html_row[0].strip()
    if len(trial) < 5 or trial[0] == 'C' or trial[0] == 'L':
        return None
    else:
        return trial

def add_dot(s):
    return s[:-2] + "." + s[-2:]

def find_cv(html_row):
    for td in reversed(html_row):
        if td.find("/") > -1:
            return add_dot(td.split("/")[0])

def read_html(filename):
    cv_html = {}
    disp_html = {}
    resv_html = {}

    html_rows = []

    class MyHTMLParser(HTMLParser):
        def __init__(self):
            HTMLParser.__init__(self)
            self.td_idx = 0
            self.cur_row = []
            self.in_td = False
            self.row_ct = 0
            self.timestamp = ""

        def handle_starttag(self, tag, attrs):
            if tag == "tr":
                self.td_idx = 0
                html_rows.append(self.cur_row)
                self.cur_row = []
                self.row_ct += 1
                self.in_td = False
            elif tag == "font":
                self.td_idx += 1
                self.in_td = True
            else:
                self.in_td = False

        def handle_data(self, data):
            if self.in_td:
                if data.strip():
                    self.cur_row.append(data.strip().replace("\xa0", "").replace(".", ""))
            # get timestamp
            if self.row_ct == 3:
                try:
                    self.timestamp = self.cur_row[-1]
                except IndexError:
                    pass

    parser = MyHTMLParser()
    html_str = open(filename).read()
    parser.feed(html_str)
    for r in html_rows:
        if len(r) > 1:
            # print(r)
            cod = find_cod(r)
            if cod:
                cv = find_cv(r)
                cv_html[cod] = cv
                try:
                    disp_html[cod] = int(r[-2])
                    resv_html[cod] = int(r[-1])
                except ValueError:
                    disp_html[cod] = 0
                    resv_html[cod] = 0

    return (parser.timestamp, cv_html, disp_html, resv_html)

def validar(filename):
    filename_geral = DIR_PREFIX + "listageral.xls"
    filename_xls = DIR_PREFIX + filename + ".xls"
    filename_html = DIR_PREFIX + filename + ".HTM"

    timestamp_geral, cv_geral = read_geral(filename_geral)
    timestamp_xls, cv_xls, disp_xls, resv_xls = read_xls(filename_xls)
    timestamp_html, cv_html, disp_html, resv_html = read_html(filename_html)

    print()
    print()

    print("Lista Geral Timestamp:", timestamp_geral)
    print()

    print("XLS Timestamp:", timestamp_xls)
    print("HTML Timestamp:", timestamp_html)

    print()

    print("CV Geral len:", len(cv_geral))
    print("CV XLS len:", len(cv_xls))
    print("CV HTML len:", len(cv_html))

    print()

    print("Disp XLS/HTML len:", len(disp_xls), len(disp_html))
    print("Resv XLS/HTML len:", len(resv_xls), len(resv_html))

    print()
    print()

    if cv_geral != cv_xls:
        for k in cv_geral:
            try:
                if cv_geral[k] != cv_xls[k]:
                    print("CV Geral/XLS differ:", k)
            except KeyError:
                print("ERR: CV > XLS Key Error:", k)

    if cv_geral != cv_html:
        for k in cv_geral:
            try:
                if cv_geral[k] != cv_html[k]:
                    print("ERR: CV Geral/HTML differ:", k, cv_geral[k], cv_html[k])
            except KeyError:
                print("ERR: CV > HTML Key Error:", k)

    if disp_xls != disp_html:
        for k in disp_xls:
            try:
                if disp_xls[k] != disp_html[k]:
                    print("ERR: Disp XLS/HTML differ:", k, disp_xls[k], disp_html[k])
            except KeyError:
                print("ERR: XLS > HTML disp Key Error:", k)

    if resv_xls != resv_html:
        for k in resv_xls:
            try:
                if resv_xls[k] != resv_html[k]:
                    print("ERR: Resv XLS/HTML differ:", k, resv_xls[k], resv_html[k])
            except KeyError:
                print("ERR: XLS > HTML resv Key Error:", k)


# -*- coding: utf-8 -*-
"""
Created on Sat Nov 22 12:04:30 2014

@author: Ducoli & Mercuri
"""

import xlrd
import sys
import os
import math
import numpy
from abc import ABCMeta, abstractmethod
from scipy import interpolate

import ExcelLib as EL
import DateLib as DL
import Calendar as CL
import datetime as DT
import NumericalRecipies as NP

###############################################################
########  MAP BBG TICKER TO INSTRUMENT CONVENTIONS  ###########
###############################################################

class InstrumentConvention:

    def __init__(self,ticker,theCalendar,periodFromOriginToSpot,periodFromSpotToStart,periodFromSpotToEnd,adjustmentRule,dayCountBasis):
        self.Calendar = theCalendar
        self.Ticker = ticker
        self.PeriodFromOriginToSpot = periodFromOriginToSpot
        self.PeriodFromSpotToStart = periodFromSpotToStart
        self.PeriodFromSpotToEnd = periodFromSpotToEnd
        self.AdjustmentRule = adjustmentRule
        self.DayCountBasis = dayCountBasis

    def ticker(self):
        return self.Ticker
    def periodFromOriginToSpot(self):
        return self.PeriodFromOriginToSpot
    def periodFromSpotToStart(self):
        return self.PeriodFromSpotToStart
    def periodFromStartToEnd(self):
        return self.PeriodFromSpotToEnd
    def adjustmentRule(self):
        return self.AdjustmentRule
    def dayCountBasis(self):
        return self.DayCountBasis
    def calendar(self):
        return self.Calendar

    def computeDatesDayCountFraction(self,refDate):
        # Return a tuble containing ( spotDate , startDate , endDate , dayCountFraction )
        spotDate = self.Calendar.dateAddPeriod(refDate,self.PeriodFromOriginToSpot,self.AdjustmentRule)
        startDate = self.Calendar.dateAddPeriod(spotDate,self.PeriodFromSpotToStart,self.AdjustmentRule)
        endDate = self.Calendar.dateAddPeriod(spotDate,self.PeriodFromSpotToEnd,self.AdjustmentRule)
        dayCountFraction = DL.DayCountFraction(startDate,endDate,self.DayCountBasis)
        return (spotDate,startDate,endDate,dayCountFraction)

MapTickerToInstrumentConventions = {
    'EUSWE1Z' : InstrumentConvention("EUSWE1Z",CL.EURCalendar(),"2BD","0BD","1W","MF","ACT/360"),
    'EUSWE2Z' : InstrumentConvention("EUSWE2Z",CL.EURCalendar(),"2BD","0BD","2W","MF","ACT/360"),
    'EUSWE3Z' : InstrumentConvention("EUSWE3Z",CL.EURCalendar(),"2BD","0BD","3W","MF","ACT/360"),
    'EUSWEA' : InstrumentConvention("EUSWEA",CL.EURCalendar(),"2BD","0BD","1M","MF","ACT/360"),
    'EUSWEB' : InstrumentConvention("EUSWEB",CL.EURCalendar(),"2BD","0BD","2M","MF","ACT/360"),
    'EUSWEC' : InstrumentConvention("EUSWEC",CL.EURCalendar(),"2BD","0BD","3M","MF","ACT/360"),
    'EUSWED' : InstrumentConvention("EUSWED",CL.EURCalendar(),"2BD","0BD","4M","MF","ACT/360"),
    'EUSWEE' : InstrumentConvention("EUSWEE",CL.EURCalendar(),"2BD","0BD","5M","MF","ACT/360"),
    'EUSWEF' : InstrumentConvention("EUSWEF",CL.EURCalendar(),"2BD","0BD","6M","MF","ACT/360"),
    'EUSWEG' : InstrumentConvention("EUSWEG",CL.EURCalendar(),"2BD","0BD","7M","MF","ACT/360"),
    'EUSWEH' : InstrumentConvention("EUSWEH",CL.EURCalendar(),"2BD","0BD","8M","MF","ACT/360"),
    'EUSWEI' : InstrumentConvention("EUSWEI",CL.EURCalendar(),"2BD","0BD","9M","MF","ACT/360"),
    'EUSWEJ' : InstrumentConvention("EUSWEJ",CL.EURCalendar(),"2BD","0BD","10M","MF","ACT/360"),
    'EUSWEK' : InstrumentConvention("EUSWEK",CL.EURCalendar(),"2BD","0BD","11M","MF","ACT/360"),
    'EUSWE1' : InstrumentConvention("EUSWE1",CL.EURCalendar(),"2BD","0BD","1Y","MF","ACT/360"),
    'EUSWE1C' : InstrumentConvention("EUSWE1C",CL.EURCalendar(),"2BD","0BD","15M","MF","ACT/360"),
    'EUSWE1F' : InstrumentConvention("EUSWE1F",CL.EURCalendar(),"2BD","0BD","18M","MF","ACT/360"),
    'EUSWE1I' : InstrumentConvention("EUSWE1I",CL.EURCalendar(),"2BD","0BD","21M","MF","ACT/360"),
    'EUSWE2' : InstrumentConvention("EUSWE2",CL.EURCalendar(),"2BD","0BD","2Y","MF","ACT/360"),
    'EUSWE3' : InstrumentConvention("EUSWE3",CL.EURCalendar(),"2BD","0BD","3Y","MF","ACT/360"),
    'EUSWE4' : InstrumentConvention("EUSWE4",CL.EURCalendar(),"2BD","0BD","4Y","MF","ACT/360"),
    'EUSWE5' : InstrumentConvention("EUSWE5",CL.EURCalendar(),"2BD","0BD","5Y","MF","ACT/360"),
    'EUSWE6' : InstrumentConvention("EUSWE6",CL.EURCalendar(),"2BD","0BD","6Y","MF","ACT/360"),
    'EUSWE7' : InstrumentConvention("EUSWE7",CL.EURCalendar(),"2BD","0BD","7Y","MF","ACT/360"),
    'EUSWE8' : InstrumentConvention("EUSWE8",CL.EURCalendar(),"2BD","0BD","8Y","MF","ACT/360"),
    'EUSWE9' : InstrumentConvention("EUSWE9",CL.EURCalendar(),"2BD","0BD","9Y","MF","ACT/360"),
    'EUSWE10' : InstrumentConvention("EUSWE10",CL.EURCalendar(),"2BD","0BD","10Y","MF","ACT/360"),
    'EUSWE11' : InstrumentConvention("EUSWE11",CL.EURCalendar(),"2BD","0BD","11Y","MF","ACT/360"),
    'EUSWE12' : InstrumentConvention("EUSWE12",CL.EURCalendar(),"2BD","0BD","12Y","MF","ACT/360"),
    'EUSWE13' : InstrumentConvention("EUSWE13",CL.EURCalendar(),"2BD","0BD","13Y","MF","ACT/360"),
    'EUSWE14' : InstrumentConvention("EUSWE14",CL.EURCalendar(),"2BD","0BD","14Y","MF","ACT/360"),
    'EUSWE15' : InstrumentConvention("EUSWE15",CL.EURCalendar(),"2BD","0BD","15Y","MF","ACT/360"),
    'EUSWE16' : InstrumentConvention("EUSWE16",CL.EURCalendar(),"2BD","0BD","16Y","MF","ACT/360"),
    'EUSWE17' : InstrumentConvention("EUSWE17",CL.EURCalendar(),"2BD","0BD","17Y","MF","ACT/360"),
    'EUSWE18' : InstrumentConvention("EUSWE18",CL.EURCalendar(),"2BD","0BD","18Y","MF","ACT/360"),
    'EUSWE19' : InstrumentConvention("EUSWE19",CL.EURCalendar(),"2BD","0BD","19Y","MF","ACT/360"),
    'EUSWE20' : InstrumentConvention("EUSWE20",CL.EURCalendar(),"2BD","0BD","20Y","MF","ACT/360"),
    'EUSWE21' : InstrumentConvention("EUSWE21",CL.EURCalendar(),"2BD","0BD","21Y","MF","ACT/360"),
    'EUSWE22' : InstrumentConvention("EUSWE22",CL.EURCalendar(),"2BD","0BD","22Y","MF","ACT/360"),
    'EUSWE23' : InstrumentConvention("EUSWE23",CL.EURCalendar(),"2BD","0BD","23Y","MF","ACT/360"),
    'EUSWE24' : InstrumentConvention("EUSWE24",CL.EURCalendar(),"2BD","0BD","24Y","MF","ACT/360"),
    'EUSWE25' : InstrumentConvention("EUSWE25",CL.EURCalendar(),"2BD","0BD","25Y","MF","ACT/360"),
    'EUSWE26' : InstrumentConvention("EUSWE26",CL.EURCalendar(),"2BD","0BD","26Y","MF","ACT/360"),
    'EUSWE27' : InstrumentConvention("EUSWE27",CL.EURCalendar(),"2BD","0BD","27Y","MF","ACT/360"),
    'EUSWE28' : InstrumentConvention("EUSWE28",CL.EURCalendar(),"2BD","0BD","28Y","MF","ACT/360"),
    'EUSWE29' : InstrumentConvention("EUSWE29",CL.EURCalendar(),"2BD","0BD","29Y","MF","ACT/360"),
    'EUSWE30' : InstrumentConvention("EUSWE30",CL.EURCalendar(),"2BD","0BD","30Y","MF","ACT/360"),
    'EUSWE50' : InstrumentConvention("EUSWE50",CL.EURCalendar(),"2BD","0BD","50Y","MF","ACT/360"),
    'EUR003M' : InstrumentConvention("EUR003M",CL.EURCalendar(),"2BD","0BD","3M","MF","ACT/360"),
    'EUFR0AD' : InstrumentConvention("EUFR0AD",CL.EURCalendar(),"2BD","1M","4M","MF","ACT/360"),
    'EUFR0BE' : InstrumentConvention("EUFR0BE",CL.EURCalendar(),"2BD","2M","5M","MF","ACT/360"),
    'EUFR0CF' : InstrumentConvention("EUFR0CF",CL.EURCalendar(),"2BD","3M","6M","MF","ACT/360"),
    'EUFR0DG' : InstrumentConvention("EUFR0DG",CL.EURCalendar(),"2BD","4M","7M","MF","ACT/360"),
    'EUFR0EH' : InstrumentConvention("EUFR0EH",CL.EURCalendar(),"2BD","5M","8M","MF","ACT/360"),
    'EUFR0FI' : InstrumentConvention("EUFR0FI",CL.EURCalendar(),"2BD","6M","9M","MF","ACT/360"),
    'EUFR0GJ' : InstrumentConvention("EUFR0GJ",CL.EURCalendar(),"2BD","7M","10M","MF","ACT/360"),
    'EUFR0HK' : InstrumentConvention("EUFR0HK",CL.EURCalendar(),"2BD","8M","11M","MF","ACT/360"),
    'EUFR0I1' : InstrumentConvention("EUFR0I1",CL.EURCalendar(),"2BD","9M","12M","MF","ACT/360")
}

###############################################################
########  Intrument Set  ######################################
###############################################################

class BootstrapIntrumentSet:

    def __init__(self,fileName):
        currentDir = os.getcwd()
        sys.path.append(currentDir)
        xl_Name = currentDir + fileName

        self.Data = {}
        self.Data['Ref_Date'] = DT.datetime(*xlrd.xldate_as_tuple(EL.xl_Get_Data(xl_Name, 0, 0, 3, 0, 3)[0], 0))
        self.Data['Tickers'] = EL.xl_Get_Data_ToUpperCaseString(xl_Name, 0, 5, 2, 48, 2)
        self.Data['MarketQuotes'] = EL.xl_Get_Data(xl_Name, 0, 5, 3, 48, 3)

        self.Data['InstrumentsConventions'] = []
        for idx in range(0, len(self.Data['Tickers'])):
            #instConvention = MapTickerToInstrumentConventions.get(self.Data['Tickers'][idx])
            instConvention = MapTickerToInstrumentConventions[self.Data['Tickers'][idx]]
            if(instConvention is None):
                raise ValueError('Null instrument convention from input data')
            self.Data['InstrumentsConventions'].append(instConvention)

    def refDate(self):
        return self.Data['Ref_Date']
    def tickers(self):
        return self.Data['Tickers']
    def instrumentsConventions(self):
        return self.Data['InstrumentsConventions']
    def instrumentConvention(self,ticker):
        instConventions = self.Data['InstrumentsConventions']
        for idx in range(0, len(instConventions)):
            instConvention = instConventions[idx]
            if(ticker == instConvention.ticker()):
                return instConvention
        raise ValueError('Ticker not found in InstrumentsConventions list')

    def marketQuotes(self):
        return self.Data['MarketQuotes']
    def numInstruments(self):
        return len(self.Data['InstrumentsConventions'])

class Curve:
    __metaclass__ = ABCMeta

    def __init__(self,insSet):
        self.InsSet = insSet
        self.DayCountFractions = []
        self.Maturities = []
        self.DiscountFactors = []
        self.ZeroRates = []

    def name(self):
        return self.Name
    def discountFactors(self):
        return self.DiscountFactors
    def zeroRates(self):
        return self.ZeroRates
    def refDate(self):
        return self.InsSet.refDate()
    def instrumentsConventions(self):
        return self.InsSet.instrumentsConventions()
    def marketQuotes(self):
        return self.InsSet.marketQuotes()
    def numPillars(self):
        return self.InsSet.numInstruments()
    def maturities(self):
        return self.Maturities
    def dayCountFractions(self):
        return self.DayCountFractions

    def dictDataToStore(self):
        data = {}
        data[self.name] = {
            'RefDate' : self.refDate(),
            'Discount' : self.DiscountFactors,
            'Zero Rate' : self.ZeroRates,
            'Maturities' : self.maturities()
            }
        dataToStore = {self.refDate():data}
        return dataToStore

    def getDiscountFactor(self,fixingDate,basis):
        if (self.Interpolator is None):
            raise ValueError('Interpolator not yet implemented in bootstap method')
        dcf = DL.DayCountFraction(self.refDate(),fixingDate,basis)
        zero_rate = self.Interpolator(dcf)
        return math.exp(-zero_rate*dcf)

    @abstractmethod
    def bootstrap(self): pass

class DiscountCurve(Curve):

    def __init__(self,insSet):
        super(DiscountCurve, self).__init__(insSet)
        self.Name = 'EONIA'

    def rangePillars(self):
        return range(0,33)

    def bootstrap(self):
        ref_date = self.refDate()
        self.Maturities.append(ref_date)
        self.DayCountFractions.append(0.0)
        self.DiscountFactors.append(1.0)
        self.ZeroRates.append(0.0)
        for i in self.rangePillars():
            instConvention = self.instrumentsConventions()[i]
            (spotDate,startDate,maturity,dayCountFraction) = instConvention.computeDatesDayCountFraction(ref_date)
            dcfFromRefDate = DL.DayCountFraction(ref_date,maturity,instConvention.dayCountBasis())

            mktQuote_i = self.InsSet.marketQuotes()[i]
            if dcfFromRefDate <=1.0:
                df = 1./(1+dcfFromRefDate*mktQuote_i)
            else:
                interpolator = NP.PiecewiseLinearInterpol1D(numpy.array(self.DayCountFractions),numpy.array(self.ZeroRates))
                bpv = self.__discountFactorFromOneYearOn(ref_date,spotDate,maturity,instConvention,interpolator)
                df = (1.-mktQuote_i*bpv)/(1+dayCountFraction*mktQuote_i)

            print (i,instConvention.ticker(),maturity,df)
            zeroRate = -math.log(df)/dcfFromRefDate

            self.Maturities.append(maturity)
            self.DayCountFractions.append(dcfFromRefDate)
            self.DiscountFactors.append(df)
            self.ZeroRates.append(zeroRate)
        self.Interpolator = NP.PiecewiseLinearInterpol1D(numpy.array(self.DayCountFractions),numpy.array(self.ZeroRates))

    def __discountFactorFromOneYearOn(self,refDate,spotDate,maturity,instConvention,interpolator):
        if(type(interpolator) is not NP.PiecewiseLinearInterpol1D):
            raise ValueError('Invalid interpolator')
        calendar = instConvention.calendar()
        dayCountBasis = instConvention.dayCountBasis()
        settlementDate = maturity
        while(settlementDate >= spotDate):
            settlementDate = calendar.dateAddPeriod(settlementDate,"-1Y","MP")
        settlementDate = calendar.dateAddPeriod(settlementDate,"1Y","MF")
        startDate = spotDate
        bpv = 0.
        while(settlementDate < maturity):
            dcf = DL.DayCountFraction(startDate,settlementDate,dayCountBasis)
            dcfFromRefDate = DL.DayCountFraction(refDate,settlementDate,dayCountBasis)
            df = interpolator.interpolate1D(dcfFromRefDate)
            bpv = bpv + dcf*df
            settlementDate = calendar.dateAddPeriod(settlementDate,"1Y","MF")
        return bpv

class ForwardCurve(Curve):

    def __init__(self,insSet):
        super(ForwardCurve, self).__init__(insSet)
        self.Name = 'EURIBOR_3M'
        lenght = len(self.rangePillars())
        self.DayCountFractions = [0]*lenght
        self.Maturities = [0]*lenght
        self.DiscountFactors = [0]*lenght
        self.ZeroRates = [0]*lenght
        self.MapTickerToIndex = {
            'EUR003M' : 2,
            'EUFR0AD' : 0,
            'EUFR0BE' : 1,
            'EUFR0CF' : 5,
            'EUFR0DG' : 3,
            'EUFR0EH' : 4,
            'EUFR0FI' : 8,
            'EUFR0GJ' : 6,
            'EUFR0HK' : 7,
            'EUFR0I1' : 9}

    def rangePillars(self):
        return range(33,43)
    def rangeDepo(self):
        rng = self.rangePillars()
        return [rng[0]]
    def rangePrincipalFras(self):
        rng = self.rangePillars()
        return (rng[3],rng[6],rng[9])
    def rangeSecondaryFras(self):
        rng = self.rangePillars()
        return (rng[1],rng[2],rng[4],rng[5],rng[7],rng[8])

    def bootstrap(self):
        df_pre = 0.
        ref_date = self.refDate()

        # Deposit Bootstrap
        for i in self.rangeDepo():
            instConvention = self.instrumentsConventions()[i]
            index = self.MapTickerToIndex[instConvention.ticker()]
            (spotDate,startDate,maturity,dayCountFraction) = instConvention.computeDatesDayCountFraction(ref_date)
            dcfFromRefDate = DL.DayCountFraction(ref_date,maturity,instConvention.dayCountBasis)
            dcf = DL.DayCountFraction(startDate,maturity,instConvention.dayCountBasis)
            mktQuote = self.InsSet.marketQuotes()[i]
            df = 1.0/(1.0+dcf*mktQuote)

            self.Maturities[index] = maturity
            self.DayCountFractions[index] = dcfFromRefDate
            self.DiscountFactors[index] = df
            self.ZeroRates[index] = -math.log(df)/dcfFromRefDate
            df_pre = df

        # FRA expiry 6M,9M,12M Bootstrap
        for i in self.rangePrincipalFras() :
            instConvention = self.instrumentsConventions()[i]
            index = self.MapTickerToIndex[instConvention.ticker()]
            (spotDate,startDate,maturity,dayCountFraction) = instConvention.computeDatesDayCountFraction(ref_date)
            dcfFromRefDate = DL.DayCountFraction(ref_date,maturity,instConvention.dayCountBasis)
            dcf = DL.DayCountFraction(startDate,maturity,instConvention.dayCountBasis)
            mktQuote = self.InsSet.marketQuotes()[i]
            df_fwd_start = 1.0/(1.0+dcf*mktQuote)
            df = df_pre*df_fwd_start

            self.Maturities[index] = maturity
            self.DayCountFractions[index] = dcfFromRefDate
            self.DiscountFactors[index] = df
            self.ZeroRates[index] = -math.log(df)/dcfFromRefDate
            df_pre = df

        # FRA expiry 1M,2M,4M,5M,7M,8M,10M,11M Bootstrap
        rngSecFras = self.rangeSecondaryFras()
        self.__FraCrabBootstrap(rngSecFras[0],'EUR003M','EUFR0CF') #1x4
        self.__FraCrabBootstrap(rngSecFras[1],'EUR003M','EUFR0CF') #2x5
        self.__FraCrabBootstrap(rngSecFras[2],'EUFR0CF','EUFR0FI') #4x7
        self.__FraCrabBootstrap(rngSecFras[3],'EUFR0CF','EUFR0FI') #5x8
        self.__FraCrabBootstrap(rngSecFras[4],'EUFR0FI','EUFR0I1') #7x10
        self.__FraCrabBootstrap(rngSecFras[5],'EUFR0FI','EUFR0I1') #8x11

        #self.Interpolator = interpolate.Akima1DInterpolator(numpy.array(self.DayCountFractions),numpy.array(self.ZeroRates))

    def __FraCrabBootstrap(self,instIndex,tickerPrePillar,tickerPostPillar):
        # FRA expiry 1M,2M,4M,5M,7M,8M,10M,11M Bootstrap
        ref_date = self.refDate()
        instConvention = self.instrumentsConventions()[instIndex]
        indexPrePillar = self.MapTickerToIndex[tickerPrePillar]
        indexPostPillar = self.MapTickerToIndex[tickerPostPillar]

        index = self.MapTickerToIndex[instConvention.ticker()]
        (spotDate,startDate,maturity,dayCountFraction) = instConvention.computeDatesDayCountFraction(ref_date)
        dcfMaturityFromRefDate = DL.DayCountFraction(ref_date,maturity,instConvention.dayCountBasis)
        dcfstartDateFromRefDate = DL.DayCountFraction(ref_date,startDate,instConvention.dayCountBasis)

        #linear Interpolation
        xPrePillar = DL.DayCountFraction(ref_date,self.maturities()[indexPrePillar],instConvention.dayCountBasis)
        xPostPillar = DL.DayCountFraction(ref_date,self.maturities()[indexPostPillar],instConvention.dayCountBasis)
        yPrePillar = self.zeroRates()[indexPrePillar]
        yPostPillar = self.zeroRates()[indexPostPillar]
        yInterp = (xPostPillar-dcfMaturityFromRefDate)*yPrePillar + (dcfMaturityFromRefDate-xPrePillar)*yPostPillar
        yInterp = yInterp/(xPostPillar-xPrePillar)
        dfInterp = math.exp(-yInterp*dcfMaturityFromRefDate)

        dcf = DL.DayCountFraction(startDate,maturity,instConvention.dayCountBasis)
        mktQuote = self.InsSet.marketQuotes()[instIndex]
        df_fwd_start = 1.0/(1.0+dcf*mktQuote)
        df = dfInterp/df_fwd_start

        self.Maturities[index] = startDate
        self.DayCountFractions[index] = dcfstartDateFromRefDate
        self.DiscountFactors[index] = df
        self.ZeroRates[index] = -math.log(df)/dcfstartDateFromRefDate


# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from azure.cli.core.util import sdk_no_wait
from azure.mgmt.loganalytics.models import Cluster, ClusterSku, KeyVaultProperties, Identity, ClusterPatch

def create_log_analytics_cluster(client, resource_group_name, cluster_name, sku_capacity,
                                 sku_name='CapacityReservation', identity_type='SystemAssigned',
                                 location=None, tags=None, no_wait=False):
    sku = ClusterSku(capacity=sku_capacity, name=sku_name)
    identity = Identity(type=identity_type)
    cluster_instance = Cluster(location=location,
                               tags=tags,
                               sku=sku,
                               identity=identity)

    return sdk_no_wait(no_wait, client.begin_create_or_update, resource_group_name, cluster_name, cluster_instance)

def update_log_analytics_cluster(client, resource_group_name, cluster_name,
                                 key_vault_uri=None, key_name=None, key_version=None,
                                 sku_capacity=None, tags=None):
    cluster_patch = ClusterPatch()
    if key_vault_uri is not None and key_name is not None and key_version is not None:
        cluster_patch.key_vault_properties = KeyVaultProperties(key_vault_uri=key_vault_uri, key_name=key_name,
                                                                key_version=key_version)
    if sku_capacity is not None:
        cluster_patch.sku = ClusterSku(capacity=sku_capacity, name='CapacityReservation')
    if tags is not None:
        cluster_patch.tags = tags

    return client.update(resource_group_name, cluster_name, cluster_patch)

def list_log_analytics_clusters(client, resource_group_name=None):
    if resource_group_name:
        return client.list_by_resource_group(resource_group_name)
    return client.list()


import imgaug as ia
from imgaug import augmenters as iaa
from imgaug.augmentables.segmaps import SegmentationMapsOnImage
import numpy as np
import torch

import CONST

# -----------------------------------------------------------------------------
# imgaug probailities
# -----------------------------------------------------------------------------
rotation_prob = 0.3
affine_prob = 0.3
flip_lr_prob = 0.5
flip_ud_prob = 0.0

arithmetic_prob = 0.3
arithmetic_same_time_ops = 2

cutout_prob = 0.3
quality_prob = 0.4

jigsaw_prob = 0.3

# -----------------------------------------------------------------------------
# Define the transformations
# -----------------------------------------------------------------------------
seq = iaa.Sequential([
        iaa.Sometimes(rotation_prob, iaa.Affine(rotate=(-360, 360))),
        iaa.Sometimes(affine_prob, iaa.Affine(scale={"x": (0.9, 1.1), "y": (0.9, 1.1)},
                                      translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)},
                                      shear=(-16, 16))),
        iaa.Fliplr(flip_lr_prob),
        iaa.Flipud(flip_ud_prob),
        iaa.Sometimes(arithmetic_prob, iaa.SomeOf(arithmetic_same_time_ops, [
            iaa.Add((-20, 20), per_channel=0.5),
            iaa.Multiply((0.7, 1.3), per_channel=0.5),
            iaa.AdditiveGaussianNoise(scale=3)], random_order=True)),

        iaa.Sometimes(cutout_prob, iaa.SomeOf(1, [
            iaa.CoarseDropout(0.02, size_percent=0.5),
            iaa.CoarseDropout(0.02, size_percent=0.15, per_channel=0.5),
            iaa.SaltAndPepper(0.05),
            iaa.SaltAndPepper(0.05, per_channel=True),
            iaa.CoarseSaltAndPepper(0.02, size_percent=(0.01, 0.05),
                per_channel=True)])),
        iaa.Sometimes(quality_prob, iaa.SomeOf(1, [
            iaa.JpegCompression(compression=(70, 99)),
            iaa.BlendAlpha((0.0, 1.0), iaa.Grayscale(1.0)),
            iaa.BlendAlpha([0.25, 0.75], iaa.MedianBlur(5)),
            iaa.BlendAlphaElementwise((0, 1.0), iaa.AddToHue(20)),
            iaa.GaussianBlur(sigma=(0.0, 1.5)),
            iaa.MedianBlur(k=(3, 5)),
            iaa.MotionBlur(k=7),
            iaa.WithHueAndSaturation(
                iaa.WithChannels(0, iaa.Add((0, 20)))),
            iaa.MultiplyHue((0.85, 1.25)),
            iaa.MultiplySaturation((0.75, 1.35)),
            iaa.ChangeColorTemperature((3000, 10000)),
            iaa.GammaContrast((0.8, 1.2)),
            iaa.AllChannelsHistogramEqualization(),
            iaa.Sharpen(alpha=(0.05, 0.2), lightness=(0.75, 1.0)),
            iaa.Emboss(alpha=(0.3, 0.6), strength=(0.5, 1.5)),
            iaa.AveragePooling(2),
            iaa.MedianPooling(2)
            ])),

        iaa.Sometimes(jigsaw_prob, iaa.Jigsaw(nb_rows=(1, 2), nb_cols=(1, 2), allow_pad=False)),
        #iaa.CropToFixedSize(width=CONST.WIDTH_TRAIN, height=CONST.HEIGHT_TRAIN, position="uniform")
    ])

def dataAugmentation(image, mask):
    """This function receives an image and its masks and apply random data augmentation techniques to both the image and
       the mask"""
    # Transform to numpy
    image = image.numpy().transpose((1, 2, 0))
    mask = mask.numpy()

    # -----------------------------------------------------------------------------
    # Apply random transformation
    # -----------------------------------------------------------------------------
    image_aug, mask_aug = seq(image=image, segmentation_maps=SegmentationMapsOnImage(mask, shape=image.shape))
    mask_aug = mask_aug.get_arr()

    # Transform back to pytorch
    image_aug = torch.from_numpy(image_aug.transpose((2, 0, 1)))
    mask_aug = torch.from_numpy(mask_aug)

    return image_aug, mask_aug


#!/usr/bin/env python3
def main():
    N, X = map(int, input().split())
    nums = [None] * (N+1)# nums[i]: i次元バーガーの層の数
    pnums = [None] * (N+1)# pnums[i]: i次元バーガーのパティの枚数
    nums[0] = 1
    pnums[0] = 1
    for i in range(N):
        nums[i+1] = nums[i]*2 + 3
        pnums[i+1] = pnums[i]*2 + 1

    ans = 0
    while N >= 0:
        if N == 0:
            print(ans+1)
            break
        if X == 1:
            print(ans)
            break
        elif X <= nums[N-1]+1:# N-1次元バーガの中をさらに探索
            X -= 1
            N -= 1
        elif X == nums[N-1]+2:# ぴったり真ん中のパティまで到達
            print(ans+pnums[N-1]+1)
            break
        elif X < nums[N]:
            X -= nums[N-1]+2
            ans += pnums[N-1]+1
            N -= 1
        else:
            print(ans+pnums[N])
            break
    # print(N,X)

if __name__ == "__main__":
    main()


# Diameter in tree
# representation: undirected tree (acyclic connected graph) on list adjacency

from queue import Queue

# complexity:
# - time O(V+E)
# - space O(V)

def BFS_times(graph, n, b=0):
    time = 1
    # if dist == float("inf") -> vertex wasn't processed
    times = [float("inf")]*n
    path = [None]*n
    queue = Queue()
    queue.put(b)
    while not queue.empty():
        time += 1
        next_queue = Queue()
        while not queue.empty():
            u = queue.get()
            for v in graph[u]:
                if times[v] == float("inf"):
                    times[v] = time
                    if v != b:
                        path[v] = u
                    next_queue.put(v)
        queue = next_queue
    return times, path

def get_max(times):
    idx, time = 0, 0
    for i, t in enumerate(times):
        if t > time:
            time = t
            idx = i
    return idx

def get_path(path, u):
    p = []
    if u is not None:
        p.append(u)
        p.extend(get_path(path, path[u]))
    return p

# returning diameter length, and path one of the diameters
def diameter_BFS(graph):
    n = len(graph)
    # running 1st time BFS
    times, _ = BFS_times(graph, n)
    left_end = get_max(times)
    # running second time BFS
    times, path = BFS_times(graph, n, left_end)
    right_end = get_max(times)
    return times[right_end], get_path(path, right_end)

graph = [
    [2],
    [2],
    [0, 1, 3],
    [2, 4],
    [3, 5, 6],
    [4],
    [4]
]
print(diameter_BFS(graph))


import numpy as np

def getValidMoves(board, color):
    moves = set()
    for y,x in zip(*np.where(board==color)):
        for direction in [(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1),(0,1)]:
            flips = []
            for size in range(1, len(board)):
                ydir = y + direction[1] * size
                xdir = x + direction[0] * size
                if xdir >= 0 and xdir < len(board) and ydir >= 0 and ydir < len(board):
                    if board[ydir][xdir]==-color:
                        flips.append((ydir, xdir))
                    elif board[ydir][xdir]==0:
                        if len(flips)!=0:
                            moves.add((ydir, xdir))
                        break
                    else:
                        break
                else:
                    break
    return np.array(list(moves))

def isValidMove(board, color, position):
    valids=getValidMoves(board, color)
    if len(valids)!=0 and (valids==np.array(position)).all(1).sum()!=0:
        return True
    else:
        return False

def executeMove(board, color, position):
    y, x = position
    board[y][x] = color
    for direction in [(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1),(0,1)]:
        flips = []
        valid_route=False
        for size in range(1, len(board)):
            ydir = y + direction[1] * size
            xdir = x + direction[0] * size
            if xdir >= 0 and xdir < len(board) and ydir >= 0 and ydir < len(board):
                if board[ydir][xdir]==-color:
                    flips.append((ydir, xdir))
                elif board[ydir][xdir]==color:
                    if len(flips)>0:
                        valid_route=True
                    break
                else:
                    break
            else:
                break
        if valid_route:
            board[tuple(zip(*flips))]=color


import logging
from logging.handlers import QueueHandler, QueueListener
from queue import Queue
from atexit import register
from pathlib import Path
import os
import sys
from dotenv import load_dotenv
import discord
from discord.ext import commands
from discord.ext.commands.errors import (
    ExtensionNotFound,
    ExtensionAlreadyLoaded,
    ExtensionFailed,
    NoEntryPointError
)

class DiscordBot(commands.Bot):
    def __init__(self, command_prefix="$"):
        self.__make_logger(verbose=True)
        self.info("Initializing the DiscordBot.")
        super().__init__(command_prefix)
        self.__load_database()
        # embed color handling will be changed when send_embed() is implemented
        self.embed_color = discord.Color.blue()
        self.load_all_extensions()
        return

    def __make_logger(self, filename="bot.log", verbose=False) -> None:
        """Creates a logger with a queue handler and a queue listener. Other
        handlers are added as desired. This allows the addition of blocking
        handlers to be handled in their own thread."""
        # Set logging level based on whether verbose is set or not
        logging_level = logging.DEBUG if verbose else logging.WARNING
        # Create the logger instance
        self._logger = logging.getLogger(__name__)
        self._logger.setLevel(logging_level)
        # Create the queue and add a queue handler to the logger
        que = Queue(-1)  # -1 means no size limit
        self._logger.addHandler(QueueHandler(que))
        # Set the format for logs
        formatter = logging.Formatter(
            "%(asctime)s:%(name)s:%(levelname)s:\n\t%(message)s\n"
        )
        # Build a list of output handlers.
        handlers = list()
        handlers.append(logging.StreamHandler())
        handlers.append(logging.FileHandler(filename))
        # Add more handlers as necessary. . .
        # Format the handlers
        for handler in handlers:
            handler.setFormatter(formatter)
        # Build the queue listener with the output handlers, start
        # the listener, and atexit.register the listener's stop function
        listener = QueueListener(que, *handlers)
        listener.start()
        register(listener.stop)
        self.info(f"Logger successfully built with handlers: {handlers}")
        return

    def __load_database(self) -> None:
        """Not yet implemented"""
        self.warning("Database is not yet implemented.")
        return

    def __load_token(self) -> str:
        """Loads the bot token from the .env file."""
        self.info("Retrieving DISCORD_TOKEN from .env file.")
        if not load_dotenv():
            self.critical("No .env file exists. Cannot run bot.")
            sys.exit()
        token = os.getenv("DISCORD_TOKEN")
        if token is None:
            self.critical("No DISCORD_TOKEN found in .env. Cannot run bot.")
            sys.exit()
        self.info("DISCORD_TOKEN found.")
        return token

    def load_all_extensions(self) -> None:
        """Finds all .py files within the cogs folder tree and loads them as
        extensions."""
        self.info("Loading all extensions from cogs folder.")
        folder_path = Path("cogs")
        if folder_path.exists():
            self.debug("Path to cogs folder found.")
        else:
            self.warning("No cogs folder exists.")
            return
        for item in folder_path.glob("**/*.py"):
            # Generate the extension name in the proper format and load it
            path = item.relative_to(folder_path.parent)
            extension = str(path).replace("\\", ".")[:-3]  # [:-3] strips ".py"
            self.load_extension(extension)
        if not self.extensions:
            self.warning("No extensions are loaded.")
        self.info("Loading extensions is complete.")
        return

    def load_extension(self, extension: str) -> None:
        """Loads the given extension while handling errors by providing
        feedback to the logger and ignoring invalid extension names"""
        try:
            super().load_extension(extension)
        except (ExtensionNotFound, ExtensionAlreadyLoaded,
                ExtensionFailed, NoEntryPointError) as error:
            self.exception(f"In {extension}:\n{error}")
        else:
            self.info(f"{extension} loaded successfully.")
        return

    def run(self) -> None:
        """Runs the bot with the loaded discord token."""
        self.info("Running the bot.")
        super().run(self.__load_token())
        return

    async def on_connect(self):
        """Logs successful discord connection"""
        self.info("Successfully connected to discord.")
        return

    async def on_ready(self):
        """Logs that bot has logged in."""
        self.info(f"Logged in to discord as {self.user}.")
        return

    async def send_message(self, ctx, msg) -> None:
        """Sends a message to discord. If the message is too long, it breaks it
        up into several smaller messages. This should be used in place of
        ctx.send() whenever the length of the message may be greater than 2000
        characters long."""
        if len(msg) <= 2000:
            await ctx.send(msg)
            return
        message_slices = [msg[i:i+2000] for i in range(0, len(msg), 2000)]
        for message in message_slices:
            await ctx.send(message)
        return

    async def send_embed(self, ctx, embed) -> None:
        """Sends an embed in a standardized way so that all cogs produce
        embeds with a consistent style."""
        # Not yet implemented
        pass

    def debug(self, msg: str) -> None:
        """Generates a logger debug message."""
        self._logger.debug(msg)
        return

    def info(self, msg: str) -> None:
        """Generates a logger info message."""
        self._logger.info(msg)
        return

    def warning(self, msg: str) -> None:
        """Generates a logger warning message."""
        self._logger.warning(msg)
        return

    def error(self, msg: str) -> None:
        """Generates a logger error message."""
        self._logger.error(msg)
        return

    def critical(self, msg: str) -> None:
        """Generates a logger critical message."""
        self._logger.critical(msg)
        return

    def exception(self, msg: str) -> None:
        """Generates a logger exception message."""
        self._logger.exception(msg)
        return

# TODO: Implement send_embed()


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from scipy import misc
import sys
sys.path.append("./facenet-master/src/")

import os
import argparse
import tensorflow as tf
import numpy as np
import facenet
import align.detect_face
import random
from time import sleep

def main():
    os.environ['CUDA_VISIBLE_DEVICES'] = '5'

    model_dir  = '/net/per920a/export/das14a/satoh-lab/wangz/ins2018/src/facenet-master/model/'
    image_list_file_dir = '/net/per920a/export/das14a/satoh-lab/wangz/ins2018/face_dict/dict.txt'
    input_dir  = '/net/per920a/export/das14a/satoh-lab/wangz/ins2018/face_dict/'
    output_dir = '/net/per920a/export/das14a/satoh-lab/wangz/ins2018/aligned_face_dict/'
    output_feature_dir = '/net/per920a/export/das14a/satoh-lab/wangz/ins2018/data/face_dict/dict_single_feature.npy'

    detect_multiple_faces = False
    margin = 44
    image_size = 182
    minsize = 20 # minimum size of face
    threshold = [ 0.6, 0.7, 0.7 ]  # three steps's threshold
    factor = 0.709 # scale factor

    print('Creating networks and loading parameters')
    g1 = tf.Graph() # detect and align
    g2 = tf.Graph() # feature
    with g1.as_default():
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        sess1 = tf.Session(graph=g1)
        with sess1.as_default():
            pnet, rnet, onet = align.detect_face.create_mtcnn(sess1, None)
    with g2.as_default():
        sess2 = tf.Session(graph=g2)
        with sess2.as_default():
            facenet.load_model(model_dir)
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]

    print('loading image path')
    output_dir = os.path.expanduser(output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    image_paths = facenet.get_image_paths(input_dir)

    # emb_feature = np.zeros((len(image_paths), embedding_size))
    emb_feature = np.zeros((100, embedding_size))

    img_id = 0
    image_list_file = open(image_list_file_dir, 'r')
    for image_path in image_list_file.readlines():
        image_path = image_path.strip('\n')
        image_path = image_path.strip()

        img = misc.imread(input_dir + image_path)
        # throw small image, and change gray image to color image
        if img.ndim < 2:
            print('Unable to align "%s"' % image_path)
            continue
        if img.ndim == 2:
            img = facenet.to_rgb(img)
        img = img[:, :, 0:3]
        # face detection
        bounding_boxes, _ = align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
        nrof_faces = bounding_boxes.shape[0]
        if nrof_faces > 0:
            det = bounding_boxes[:, 0:4]
            det_arr = []
            img_size = np.asarray(img.shape)[0:2]

            if nrof_faces > 1:
                if detect_multiple_faces:
                    for i in range(nrof_faces):
                        det_arr.append(np.squeeze(det[i]))
                else:
                    bounding_box_size = (det[:, 2] - det[:, 0]) * (det[:, 3] - det[:, 1])
                    img_center = img_size / 2
                    offsets = np.vstack([(det[:, 0] + det[:, 2]) / 2 - img_center[1],
                                         (det[:, 1] + det[:, 3]) / 2 - img_center[0]])
                    offset_dist_squared = np.sum(np.power(offsets, 2.0), 0)
                    index = np.argmax(
                        bounding_box_size - offset_dist_squared * 2.0)  # some extra weight on the centering
                    det_arr.append(det[index, :])
            else:
                det_arr.append(np.squeeze(det))

            # for each detected face
            for i, det in enumerate(det_arr):
                det = np.squeeze(det)
                bb = np.zeros(4, dtype=np.int32)
                bb[0] = np.maximum(det[0] - margin / 2, 0)
                bb[1] = np.maximum(det[1] - margin / 2, 0)
                bb[2] = np.minimum(det[2] + margin / 2, img_size[1])
                bb[3] = np.minimum(det[3] + margin / 2, img_size[0])
                cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
                scaled = misc.imresize(cropped, (image_size, image_size), interp='bilinear')
                scaled = misc.imresize(scaled, (160, 160), interp='bilinear')

                # filename = os.path.splitext(os.path.split(image_path)[1])[0]
                # output_filename = os.path.join(output_dir, filename + '.png')
                # filename_base, file_extension = os.path.splitext(output_filename)
                # if detect_multiple_faces:
                #    output_filename_n = "{}_{}{}".format(filename_base, i, file_extension)
                # else:
                #     output_filename_n = "{}{}".format(filename_base, file_extension)
                # misc.imsave(output_filename_n, scaled)

                scaled_reshape = []
                pre_img = facenet.prewhiten(scaled)
                scaled_reshape.append(pre_img.reshape(-1, 160, 160, 3))
                emb_temp = np.zeros((1, embedding_size))
                emb_temp[0, :] = sess2.run(embeddings, feed_dict={images_placeholder: scaled_reshape[0], phase_train_placeholder: False})[0]
                emb_feature[img_id, :] = emb_temp[0, :]
                img_id = img_id+1
        else:
            print('Unable to align "%s"' % image_path)

    np.save(output_feature_dir, emb_feature)

if __name__ == '__main__':
    main()


#!/usr/bin/env python3

# Ensure compatibility with Python 2
from __future__ import absolute_import, division, print_function, unicode_literals

from datetime import datetime
import logging
import numpy as np
import pandas as pd
import time
import matplotlib.pyplot as plt

from fixed_sing_vals_problem import SingValProblem
import pycutest
import pybobyqa
import block_pybobyqa

# The basic set of problems
PROBLEMS = {}
PROBLEMS['ARGLALE'] = {'N':100}
PROBLEMS['ARGTRIG'] = {'N':100}
PROBLEMS['ARTIF'] = {'N':100}
PROBLEMS['ARWHDNE'] = {'N':100}
PROBLEMS['BDVALUES'] = {'NDP':102}
PROBLEMS['BRATU2D'] = {'P':10}
PROBLEMS['BROWNALE'] = {'N':100}
PROBLEMS['BROYDNBD'] = {'N':100}
PROBLEMS['INTEGREQ'] = {'N':100}
PROBLEMS['YATP1SQ'] = {'N':10}

KEY_FVALS = {}
KEY_FVALS['ARGLALE'] = (700, 300)
# KEY_FVALS['ARGLBLE'] = (546094423480400, 99.6254681645)
KEY_FVALS['ARGTRIG'] = (32.9964140424, 0)
KEY_FVALS['ARTIF'] = (36.5911453216, 0)
KEY_FVALS['ARWHDNE'] = (495, 27.6620299372)
KEY_FVALS['BDVALUES'] = (19434168.8669307, 0)
KEY_FVALS['BRATU2D'] = (0.1560737689, 0)
# KEY_FVALS['BRATU2DT'] = (0.4521310575, 0.0000185347366279219)
# KEY_FVALS['BRATU3D'] = (4.888529079, 0)
KEY_FVALS['BROWNALE'] = (252475.748047829, 0)
# KEY_FVALS['BROYDN3D'] = (111, 0)
KEY_FVALS['BROYDNBD'] = (2404, 0)
# KEY_FVALS['CBRATU2D'] = (0.4822530872, 0)
# KEY_FVALS['CHANDHEQ'] = (6.9233654432, 0)
# KEY_FVALS['CHEMRCTA'] = (3.0935, 0)
# KEY_FVALS['CHEMRCTB'] = (1.4465125, 0.001404424)
# KEY_FVALS['CHNRSBNE'] = (7635.84, 0)
# KEY_FVALS['DRCAVTY1'] = (0.4513888896, 0)
# KEY_FVALS['DRCAVTY2'] = (0.4513888896, 0.0054496021)
# KEY_FVALS['DRCAVTY3'] = (0.4513888896, 0)
# KEY_FVALS['EIGENA'] = (285, 0)
# KEY_FVALS['EIGENB'] = (19, 0)
# KEY_FVALS['FLOSP2HH'] = (519, 0.333333333333475)
# KEY_FVALS['FLOSP2HL'] = (519, 0.3333333333)
# KEY_FVALS['FLOSP2HM'] = (519, 0.3333333333)
# KEY_FVALS['FLOSP2TH'] = (516, 0)
# KEY_FVALS['FLOSP2TL'] = (516, 0)
# KEY_FVALS['FLOSP2TM'] = (516, 0)
# KEY_FVALS['FREURONE'] = (99556.5, 11964.5773486542)
# KEY_FVALS['HATFLDG'] = (27, 0)
# KEY_FVALS['HYDCAR20'] = (1341.6625208674, 0)
# KEY_FVALS['HYDCAR6'] = (704.1073340917, 0)
KEY_FVALS['INTEGREQ'] = (0.5730503033, 0)
# KEY_FVALS['METHANB8'] = (1.0431047718, 0)
# KEY_FVALS['METHANL8'] = (4345.0997644961, 0)
# KEY_FVALS['MOREBVNE'] = (0.00036331, 0)
# KEY_FVALS['MSQRTA'] = (212.7162185382, 0)
# KEY_FVALS['MSQRTB'] = (205.0846075884, 0)
# KEY_FVALS['OSCIGRNE'] = (612072002.25, 0)
# KEY_FVALS['PENLT1NE'] = (114480553325.063, 0.000000009)
# KEY_FVALS['PENLT2NE'] = (1591383.31095352, 0.9809376763)
# KEY_FVALS['POWELLSE'] = (41875, 0)
# KEY_FVALS['QR3D'] = (1.2, 0)
# KEY_FVALS['QR3DBD'] = (1.2, 0)
# KEY_FVALS['SEMICN2U'] = (20250.3738155292, 0)
# KEY_FVALS['SEMICON2'] = (20250.3738155292, 0)
# KEY_FVALS['SPMSQRT'] = (74.3354196588, 0)
# KEY_FVALS['VARDIMNE'] = (131058369689326, 0)
# KEY_FVALS['WATSONNE'] = (30, 0)
KEY_FVALS['YATP1SQ'] = (2073642.96508992, 0)
# KEY_FVALS['YATP2SQ'] = (183168.681946967, 0)
# KEY_FVALS['LUKSAN11'] = (626.0639857228, 0)
# KEY_FVALS['LUKSAN12'] = (32160, 4292.1968914065)
# KEY_FVALS['LUKSAN13'] = (64352, 25188.8595896452)
# KEY_FVALS['LUKSAN14'] = (26880, 123.9235407645)
# KEY_FVALS['LUKSAN15'] = (27015.8467607871, 3.5696968772)
# KEY_FVALS['LUKSAN16'] = (13068.4841811974, 3.5696968772)
# KEY_FVALS['LUKSAN17'] = (1687370.14892775, 0.4931612903)
# KEY_FVALS['LUKSAN21'] = (99.9875072003, 0)
# KEY_FVALS['LUKSAN22'] = (24876.864702602, 872.9229558894)

class Objfun(object):
    def __init__(self, problem):
        self._problem = problem
        self.nf = 0
        self.xs = []
        self.fs = []

    def __call__(self, x):
        self.nf += 1
        self.xs.append(x.copy())
        rx = self._problem.cons(x)
        fx = sumsq(rx)
        self.fs.append(fx)
        return fx

    def get_summary(self, with_xs=False):
        results = {}
        # results['objfun'] = self._problem.info['name']
        results['objfun'] = self._problem.name
        if with_xs:
            results['xvals'] = self.xs
        results['fvals'] = self.fs
        results['nf'] = self.nf
        # results['n'] = self._problem.info['n']
        results['n'] = self._problem.n
        results['neval'] = np.arange(1, self.nf+1)  # start from 1
        return pd.DataFrame.from_dict(results)

def sumsq(x):
    return np.dot(x, x)

def get_problem(probname):
    if probname in PROBLEMS:
        params = PROBLEMS[probname]
        # pycutest.clear_cache(probname, sifParams=params)
        p = pycutest.import_problem(probname, sifParams=params)
        return p
    elif probname.startswith('SINGVAL'):
        p = int(probname.replace('SINGVAL', ''))
        return SingValProblem(p)
    else:
        raise RuntimeError('Unknown problem: %s' % probname)

def n_based_string_to_int(n_str, n):
    # Some common shortcuts that I use
    if type(n_str) == int:
        return n_str

    if n_str == 'tenthn':
        return max(n // 10, 1)
    elif n_str == 'fifthn':
        return max(n // 5, 1)
    elif n_str == 'qtrn':
        return max(n // 4, 1)
    elif n_str == 'halfn':
        return max(n // 2, 1)
    elif n_str == 'n':
        return n
    elif n_str == 'np1':
        return n + 1
    elif n_str == 'np2':
        return n + 2
    elif n_str == '2n':
        return 2 * n
    elif n_str == '2np1':
        return 2 * n + 1
    elif n_str == 'nsq':
        return (n + 1) * (n + 2) // 2
    elif n_str.endswith('n'):
        # Format is: "kn", return k*n
        return int(n_str.replace('n', '')) * n
    elif n_str.endswith('(np1)'):
        # Format is: "k(np1)", return k*(n+1)
        return int(n_str.replace('(np1)', '')) * (n + 1)
    else:  # Don't know, just assume it's a string of an integer, e.g. '5'
        return int(n_str)

def run_pybobyqa(problem, solver_params={}, max_ngradients=100, rhoend=1e-8, seed=None, npt='2np1',
                           delta0=None, save_diagnostic_info=False, diagnostic_info_filestem=None, verbose=False):
    n = problem.n
    objfun = Objfun(problem)

    # Set random seed, for reproducibility
    if seed is None:
        new_seed = np.random.randint(0, high=1000000)  # must be in 32-bit unsigned range = [0, ..., 4,294,967,295]
        if verbose:
            logging.info("From here, using NumPy random seed = %g" % new_seed)
        np.random.seed(new_seed)
    else:
        if verbose:
            logging.info("From here, using NumPy random seed = %g" % seed)
        np.random.seed(seed)

    # Important - we want to save diagnostic info while growing
    params = {}
    params["logging.save_diagnostic_info"] = save_diagnostic_info
    params["logging.save_xk"] = False
    for k in solver_params:
        params[k] = solver_params[k]
    # if save_diagnostic_info:
    #     assert diagnostic_info_filestem is not None, "Need to specify diagnostic_info_filestem"
    #     assert '[problem]' in diagnostic_info_filestem, "Need to have [problem] in diagnostic_info_filestem"

    maxfun = int(max_ngradients * (n + 1))

    # Prepare and run
    x0 = problem.x0
    if verbose:
        logging.info("*** Parameter values used ***")
        for key in params:
            logging.info("%s: %s" % (key, params[key]))
        logging.info("*** End parameter list ***")
        logging.info("")

        logging.info("Problem %s: Started solve at " % (problem.name) + datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))

    tic = time.process_time()
    soln = pybobyqa.solve(objfun, x0, npt=n_based_string_to_int(npt, n), maxfun=maxfun, rhobeg=delta0, rhoend=rhoend,
                                objfun_has_noise=False, user_params=params)
    toc = time.process_time()

    if verbose:
        logging.info("Problem %s: Finished solve at " % (problem.name) + datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
        logging.info("Problem %s: Took %g seconds (actual CPU+sys time)" % (problem.name, toc - tic))

        logging.critical(soln.msg)

        logging.critical(
            "Solution is x = [...] with objective %.15g (found in %i function evaluations)" % (soln.f, soln.nf))
        logging.info("")

    # df = objfun.get_summary()

    if save_diagnostic_info:
        return soln.diagnostic_info
        # soln.diagnostic_info.to_csv(diagnostic_info_filestem.replace('[problem]', problem.name))
    return

def run_block_pybobyqa(problem, solver_params={}, fixed_block=None, max_ngradients=100, rhoend=1e-8, seed=None, delta0=None,
              save_diagnostic_info=False, diagnostic_info_filestem=None, verbose=False, npt='2np1'):
    n = problem.n
    objfun = Objfun(problem)

    fixed_block_to_use = None if fixed_block is None else n_based_string_to_int(fixed_block, n)

    # Set random seed, for reproducibility
    if seed is None:
        new_seed = np.random.randint(0, high=1000000)  # must be in 32-bit unsigned range = [0, ..., 4,294,967,295]
        if verbose:
            logging.info("From here, using NumPy random seed = %g" % new_seed)
        np.random.seed(new_seed)
    else:
        if verbose:
            logging.info("From here, using NumPy random seed = %g" % seed)
        np.random.seed(seed)

    # Important - we want to save diagnostic info while growing
    params = {}
    params["logging.save_diagnostic_info"] = save_diagnostic_info
    params["logging.save_xk"] = False
    for k in solver_params:
        params[k] = solver_params[k]
    # if save_diagnostic_info:
    #     assert diagnostic_info_filestem is not None, "Need to specify diagnostic_info_filestem"
    #     assert '[problem]' in diagnostic_info_filestem, "Need to have [problem] in diagnostic_info_filestem"

    maxfun = int(max_ngradients * (n + 1))

    # Prepare and run
    x0 = problem.x0
    if verbose:
        logging.info("*** Parameter values used ***")
        for key in params:
            logging.info("%s: %s" % (key, params[key]))
        logging.info("*** End parameter list ***")
        logging.info("")

        logging.info("Problem %s: Started solve at " % (problem.name) + datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))

    tic = time.process_time()
    soln = block_pybobyqa.solve(objfun, x0, fixed_block=fixed_block_to_use, maxfun=maxfun, rhobeg=delta0, rhoend=rhoend,
                                objfun_has_noise=False, user_params=params, npt_fun=npt)
    toc = time.process_time()

    if verbose:
        logging.info("Problem %s: Finished solve at " % (problem.name) + datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
        logging.info("Problem %s: Took %g seconds (actual CPU+sys time)" % (problem.name, toc - tic))

        logging.critical(soln.msg)

        logging.critical("Solution is x = [...] with objective %.15g (found in %i function evaluations)" % (soln.f, soln.nf))
        logging.info("")

    # df = objfun.get_summary()

    if save_diagnostic_info:
        return soln.diagnostic_info
        # soln.diagnostic_info.to_csv(diagnostic_info_filestem.replace('[problem]', problem.name))
    return

def make_diagnostic_plot(problem, df, xaxis_in_iters=True, show_restart_in_plots=True, obj_redn_in_tau=True, max_ngradients=None):
    # try:
    #     df = pd.DataFrame.from_csv(filename)
    # except OSError:
    #     print("Missing file: %s" % filename)
    #     exit()
    font_size = 'small'  # large if saving to file
    # logging.info("******************")
    # logging.info(df)
    # logging.info("******************")

    # from diagnostic_info.py
    ITER_VERY_SUCCESSFUL = "Very successful"  # ratio >= 0.7, no geometry update
    ITER_SUCCESSFUL = "Successful"  # 0.1 <= ratio < 0.7, no geometry update
    ITER_ACCEPTABLE = "Acceptable"  # 0 <= ratio < 0.1
    ITER_UNSUCCESSFUL = "Unsuccessful"  # ratio < 0
    ITER_SAFETY = "Safety"  # safety step taken (||s|| too small compared to rho)
    ITER_INIT = "Initial setup"

    # Info for plotting
    ITER_PLOTTING_INFO = {}
    ITER_PLOTTING_INFO[ITER_VERY_SUCCESSFUL] = 4  # value to plot, text
    ITER_PLOTTING_INFO[ITER_SUCCESSFUL] = 3  # value to plot, text
    ITER_PLOTTING_INFO[ITER_ACCEPTABLE] = 2  # value to plot, text
    ITER_PLOTTING_INFO[ITER_UNSUCCESSFUL] = 1  # value to plot, text
    ITER_PLOTTING_INFO[ITER_SAFETY] = 0  # value to plot, text
    ITER_PLOTTING_INFO["n/a"] = -1
    ITER_PLOTTING_INFO[None] = -1
    ITER_PLOTTING_INFO[ITER_INIT] = -1

    ITER_PLOT_LABELS = {-1: "", 0: "Safety", 1: "Bad", 2: "Ok", 3: "Succ", 4: "V. Succ"}

    nruns = df['nruns'].as_matrix()
    skips = nruns[1:] != nruns[:-1]
    idx_skips = np.where(skips)[0]

    plt.ioff()  # non-interactive mode
    # plt.figure(1, figsize=(12, 8))  # size in inches (usually save 100dpi, so pixels is multiplied by 100)
    plt.figure(1, figsize=(9, 6))  # size in inches (usually save 100dpi, so pixels is multiplied by 100)

    xaxis = (df['iters_total'] if xaxis_in_iters else df['nf'] / (df['n'] + 1)).as_matrix()
    xmin = np.min(xaxis)
    xmax = np.max(xaxis)
    if not xaxis_in_iters and max_ngradients is not None:
        xmax = max_ngradients  # normalise x-axis by total budget for easy comparison
    xlabel = "Iteration" if xaxis_in_iters else "Budget (gradients)"

    # plt.subplot(2, 3, 1)  # (2x2) grid, first subplot
    # ax = plt.gca()  # current axes
    # ax.semilogy(xaxis, df['J_norm'], color='b', linestyle='-', linewidth=2.0, label="||Jk||_F")
    # ax.semilogy(xaxis, df['J_cond'], color='r', linestyle='-', linewidth=2.0, label="cond(Jk)")
    # # ax.semilogy(xaxis, df['change_J_norm'], color='g', linestyle='-', linewidth=2.0, label="||chg Jk||_F")
    # if show_restart_in_plots and xaxis_in_iters:
    #     for i in range(len(idx_skips)):
    #         ax.axvline(x=idx_skips[i] + 0.5, linewidth=2.0, linestyle='-', color='k')
    # # ax.set_xlabel(xlabel)
    # ax.set_xlim(xmin, xmax)
    # leg = ax.legend(loc='lower right', fontsize=font_size, fancybox=True)
    # ax.set_title('Jacobian info')
    # ax.grid()

    plt.subplot(2, 2, 1)  # (2x2) grid, first subplot
    ax = plt.gca()  # current axes
    ax.semilogy(xaxis, df['delta'], color='b', linestyle='-', linewidth=2.0, label="Delta")
    ax.semilogy(xaxis, df['norm_gk'], color='r', linestyle='-', linewidth=2.0, label="||gk||")
    ax.semilogy(xaxis, df['norm_sk'], color='g', linestyle='-', linewidth=2.0, label="||sk||")
    if 'H_norm2' in df:
        gnorm = df['norm_gk'].as_matrix()
        H_norm2 = df['H_norm2'].as_matrix()
        # ax.semilogy(xaxis, H_norm2, color='m', linestyle='-', linewidth=2.0, label="||Hk||_2")
        ax.semilogy(xaxis, gnorm / H_norm2, color='m', linestyle='-', linewidth=2.0, label="||gk||/||Hk||_2")
    if show_restart_in_plots and xaxis_in_iters:
        for i in range(len(idx_skips)):
            ax.axvline(x=idx_skips[i] + 0.5, linewidth=2.0, linestyle='-', color='k')
    leg = ax.legend(loc='upper right', fontsize=font_size, fancybox=True)
    ax.set_xlim(xmin, xmax)
    # ax.set_xlabel(xlabel)
    ax.set_title('Convergence info')
    ax.grid()

    plt.subplot(2, 2, 2)  # (2x2) grid, first subplot
    ax = plt.gca()  # current axes
    iter_types = [ITER_PLOTTING_INFO[t] for t in df['iter_type'].tolist()]
    ax.plot(list(xaxis), iter_types, color='b', linestyle='none', linewidth=2.0, marker='.',
            markersize=10, label="Iteration type")
    if show_restart_in_plots and xaxis_in_iters:
        for i in range(len(idx_skips)):
            ax.axvline(x=idx_skips[i] + 0.5, linewidth=2.0, linestyle='-', color='k')
    tick_levels = []
    tick_labels = []
    for t in ITER_PLOTTING_INFO:
        if t != "n/a" and t is not None and t != ITER_INIT:
            tick_levels.append(ITER_PLOTTING_INFO[t])
            tick_labels.append(ITER_PLOT_LABELS[ITER_PLOTTING_INFO[t]])
    # ax.axis([df['iters_total'].min(), df['iters_total'].max(), min(tick_levels) - 0.1, max(tick_levels) + 0.1])  # (xlow, xhigh, ylow, yhigh)
    ax.set_xlim(xmin, xmax)
    ax.set_ylim(min(tick_levels) - 0.1, max(tick_levels) + 0.1)
    ax.set_yticks(tick_levels)
    ax.set_yticklabels(tick_labels)
    # ax.set_xlabel(xlabel)
    ax.set_title("Iteration type")
    ax.grid()

    plt.subplot(2, 2, 3)  # (2x2) grid, first subplot
    ax = plt.gca()  # current axes
    ax.axhline(y=df['n'][0], color='k')
    ax.plot(xaxis, np.abs(df['ndirs'].as_matrix()), color='b', linestyle='-', linewidth=2.0, label="p")
    ax.plot(xaxis, np.abs(df['npt'].as_matrix()), color='r', linestyle='--', linewidth=2.0, label="npt")
    if show_restart_in_plots and xaxis_in_iters:
        for i in range(len(idx_skips)):
            ax.axvline(x=idx_skips[i] + 0.5, linewidth=2.0, linestyle='-', color='k')
    # leg = ax.legend(loc='lower right', fontsize=font_size, fancybox=True)
    ax.set_xlim(xmin, xmax)
    ax.set_ylim(0, None)
    # ax.set_ylim(0, 1.05*df['n'][0])
    ax.set_xlabel(xlabel)
    ax.set_title('Block size')
    ax.grid()

    # plt.subplot(2, 3, 5)  # (2x2) grid, first subplot
    # ax = plt.gca()  # current axes
    # ax.plot(xaxis, df['J_rank'] / df['ndirs'], color='b', linestyle='-', linewidth=2.0, label="rank(Jk)/p")
    # # ax.plot(xaxis, df['change_J_rank'] / df['n'], color='r', linestyle='-', linewidth=2.0, label="rank(chg Jk)/p")
    # ax.plot(xaxis, df['J_srank'] / df['ndirs'], color='c', linestyle='--', linewidth=2.0, label="srank(Jk)/p")
    # # ax.plot(xaxis, df['change_J_srank'] / df['n'], color='m', linestyle='--', linewidth=2.0, label="srank(chg Jk)/p")
    # if show_restart_in_plots and xaxis_in_iters:
    #     for i in range(len(idx_skips)):
    #         ax.axvline(x=idx_skips[i] + 0.5, linewidth=2.0, linestyle='-', color='k')
    # leg = ax.legend(loc='lower right', fontsize=font_size, fancybox=True)
    # ax.set_xlim(xmin, xmax)
    # ax.set_ylim(-0.05, 1.05)
    # ax.set_xlabel(xlabel)
    # ax.set_title('Conditioning info')
    # ax.grid()

    plt.subplot(2, 2, 4)  # (2x2) grid, first subplot
    ax = plt.gca()  # current axes
    if problem.startswith('SINGVAL'):
        p = get_problem(problem)
        r0 = p.cons(p.x0)
        f0 = np.dot(r0, r0)
        fmin = 0.0
    else:
        f0, fmin = KEY_FVALS[problem]
    fopts = np.maximum(df['fk'].as_matrix(), 1e-12)  # floor values at 1e-12
    if obj_redn_in_tau:
        fopts = (fopts - fmin) / (f0 - fmin)  # switch to tau format
    ax.semilogy(xaxis, fopts, color='b', linestyle='-', linewidth=2.0, label="Objective reduction")
    if not obj_redn_in_tau:
        ax.axhline(y=max(fmin, 1e-12), linewidth=2.0, linestyle='--', color='r')
    if show_restart_in_plots and xaxis_in_iters:
        for i in range(len(idx_skips)):
            ax.axvline(x=idx_skips[i] + 0.5, linewidth=2.0, linestyle='-', color='k')
    ax.set_xlabel(xlabel)
    ax.set_xlim(xmin, xmax)
    # leg = ax.legend(loc='lower right', fontsize=font_size, fancybox=True)
    ax.set_title('Normalised obj redn' if obj_redn_in_tau else 'Objective reduction')
    ax.grid()

    # plt.savefig(filename.replace('.csv', '.png'), bbox_inches='tight')
    plt.show()
    return

if __name__ == '__main__':
    """
    export OPENBLAS_NUM_THREADS=1
    export NUMEXPR_NUM_THREADS=1
    """

    # problem = 'ARGLALE'
    # problem = 'ARGTRIG'
    # problem = 'ARTIF'
    # problem = 'ARWHDNE'
    problem = 'BDVALUES'
    # problem = 'BRATU2D'
    # problem = 'BROWNALE'
    # problem = 'BROYDNBD'
    # problem = 'INTEGREQ'
    # problem = 'YATP1SQ'
    # problem = 'SINGVAL2'
    # problem = 'SINGVAL126'
    p = get_problem(problem)

    # max_ngradients = 50
    max_ngradients = 30
    # max_ngradients = 2

    delta0 = None  # use default

    seed = 10

    verbose = True
    if verbose:
        logging.basicConfig(level=logging.INFO, format='%(message)s')
        # logging.basicConfig(level=logging.DEBUG, format='%(message)s')

    if False:
        df = run_pybobyqa(p, solver_params={}, max_ngradients=max_ngradients, seed=seed, npt='2np1', delta0=delta0, verbose=verbose)
        exit()

    params = {}
    params["geometry.use_coord_directions"] = False  # use random orthogonal directions for init setup (default)
    # params["geometry.use_coord_directions"] = True  # use random coordinate directions for init setup

    params["logging.save_poisedness"] = True
    params["logging.save_xk"] = False
    params["general.safety_step_thresh"] = 1e-16  # TODO no safety

    # fixed_block = None  # adaptive
    # fixed_block = 'n'  # fixed block
    fixed_block = 'qtrn'  # fixed block

    npt = '2np1'
    # npt = 'np2'
    # params["adaptive.save_hess"] = False  # old style

    params["geometry.safety_steps_before_redraw"] = 5
    # params["adaptive.condition"] = 'normg'
    params["adaptive.condition"] = 'normgh'
    # params["adaptive.condition"] = 'delta'
    # params["adaptive.condition"] = 'deltasqcurv'
    # params["adaptive.condition"] = 'modelred'  # avoid this option
    # params["adaptive.condition"] = 'armijo'
    params["adaptive.alpha"] = 0.2
    params["adaptive.ignore_condition_prob"] = 0.0  # always use full condition
    # params["adaptive.ignore_condition_prob"] = 0.1  # 0 = always use full condition
    params["adaptive.npt_save_frac"] = 0.9
    params["adaptive.dirn_save_frac"] = 0.9
    params["adaptive.adjust_alpha"] = True  # use alpha*(n-p)/n

    # params["tr_radius.update_method"] = 'storm'  # TODO alternative TR updating
    # params["tr_radius.gamma_inc_overline"] = 2.0

    df = run_block_pybobyqa(p, fixed_block=fixed_block, solver_params=params, max_ngradients=max_ngradients, delta0=delta0,
               seed=seed, save_diagnostic_info=True, verbose=verbose, npt=npt)
    make_diagnostic_plot(problem, df, xaxis_in_iters=False, obj_redn_in_tau=True, max_ngradients=max_ngradients)

    df.to_csv('%s.csv' % problem, index=False)

    # Get iterates in sensible form
    if False:
        xks = df['xk'].as_matrix()
        xks = list(xks)
        xks = np.array(xks)  # shape = (niter rows, n cols)
        niters  = xks.shape[0]
        grad_norms = np.zeros((niters,))
        for i in range(niters):
            xk = xks[i, :]
            rk, Jk = p.cons(xk, gradient=True)
            gk = np.dot(Jk.T, rk)
            grad_norms[i] = np.linalg.norm(gk)
        print(grad_norms)

    logging.critical("Done")


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

import numpy as np
import matplotlib.pyplot as plt
import thermopy.irb as irb
import thermopy.thermofd as fd

# Try to make prettier plots with seaborn (https://github.com/mwaskom/seaborn)
# if installed:
try:
    import seaborn as sns
    sns.set(
       style='whitegrid',
       rc = {
           'font.weight'   : '100',
           'axes.labelweight'   : '100',
           }
       )
except ImportError:
    pass

kPMMA = 1.1e-7 # Thermal diffusivity of PMMA
lPMMA = 0.2 # Thermal conductivity of PMMA
rhoPMMA = 1180. # Density of PMMA

# Setup measurement name. This equals the filename of the h5-file minus the .h5
name = 'AS_E0_P6',

# Construct complete filename
fname = '/data/kalle/TrierSeptember/%s.h5' % name
# Construct the cube
cube = irb.irCube(fname)
# Setup alpha, i.e. the proportionality factor of the Cauchy-Condition
alpha = 70
# set kappa o be the thermal diffusivity of PMMA
kappa = kPMMA

# Set the corresponding temperature measurement
cube.setWeather('/home/Seismologie/data/IRT/Trier-201309/Wetter_raw.mws',
        sensor='extern')
# Shortcut to the data
wetter = cube.weather.temperature
# Set the times (in seconds) at which the heating starts and stops again
cube.initBounds((566, 1406))

# Set an area over which the measurements will be averaged
cube.addArea(topleft=(512,  68), bottomright=(639,   0), name='R0')

# Set time increment
dt = 1.
# and create time vector
t = t = np.arange(0, cube.time.max(), dt)
# time dimension
M = len(t)

# Interpolate to that time increment
# Temperature of the surroundings
Te = np.interp(t, cube.time, wetter)
# Temperature of the selected area (averaged)
T_real = np.interp(t, cube.time, cube.areas[0].data)

# Set size of the object to be modelled
size = 0.2

# Until when is being heated?
heatTime = 1406. #TODO: Why does this differ from the upper boundary in initBounds?
# Whats the index in the interpolated data?
heatIdx = int(heatTime/dt)

# Optimal space increment
dx = 2*np.sqrt(dt*kPMMA)
# Space dimension
N = int(size/dx)

# Space vector
x = np.arange(N) * dx

# Guess initial thermal state of the object (function of depth)
T0 = np.ones(N) * T_real[0]

# Prepare result array, so we don't need to create a numpy.ndarray-Object from
# C++ (which is not implemented, so this step is essential)
res = np.zeros(M)

# Counter variable
i = 0
# Prepare misfit-vector
mf, T = fd.fd(
        t,
        x,
        alpha,
        kappa,
        T_real,
        T0,
        Te,
        heatTime,
        rho = rhoPMMA,
        l = lPMMA,
        )

# Plot the modelled surface temperature
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(t, T[:,0])
ax.set_xlabel('time [s]')
ax.set_ylabel('Temperature [$^\circ$C]')
fig.savefig('modelDemo.png', dpi=300)


import json
from pprint import pprint
import sys
from collections import defaultdict
import codecs

def classify(modelfile):
    f=open(modelfile,'r')
    model1 = json.load(f)
    model=defaultdict(lambda: defaultdict(int),model1)
    f.close()

    sys.stdin = codecs.getreader('utf8')(sys.stdin.detach(), errors='ignore')

    for line in sys.stdin:
        dic_predict=defaultdict(int)
        dic_words=defaultdict(int)
        words=line.split()
        for w in words:
            dic_words[w]=dic_words[w]+1
        for K in model.keys():
            V=0
            for k,v in dic_words.items():
                V=V+ v*int(model[K].get(k) or 0)
                dic_predict[K]=V
        predicted=max(dic_predict.values())
        for k,v in dic_predict.items():
            if predicted ==v:
                predicted_class=k
                break
        print(predicted_class)
        sys.stdout.flush()

def main():
    modelfile=sys.argv[1]
    classify(modelfile)

if __name__ == '__main__':
    main()


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

#Python 字典 setdefault() 函数和get() 方法类似, 如果键不存在于字典中，将会添加键并将值设为默认值。如果键在字典中，返回这个键所对应的值
#原型 dict.setdefault(key, default=None)

txt = 'I like python and i use python'
words = txt.split(' ')
print(words)

counts = dict()

for word in words:
    counts[word] = counts.setdefault(word,0)  +1
print(counts)

'''
defaultdict的定义如下class collections.defaultdict([default_factory[, ...]])
default_factory 接收一个工厂函数作为参数, 例如int str list set等.该参数用于初始化该字典中不存在的key的value
defaultdict在dict的基础上添加了一个missing(key)方法, 在调用一个不存的key的时候, defaultdict会调用__missing__, 返回一个根据default_factory参数的默认值, 所以不会返回Keyerror.
'''

from collections import defaultdict

if __name__ == "__main__":
    # 将句子里面的单词抽取成元组
    txt = 'I like python and i use python'
    words = txt.split(' ')
    print(words)

    # 开始统计单词出现次数
    counts = defaultdict(int)
    for word in words:
        counts[word] += 1
    print(counts)

    #collections.Couter类
    #Counter类的目的是用来跟踪值出现的次数。它是一个无序的容器类型，以字典的键值对形式存储，其中元素作为key，其计数作为value。计数值可以是任意的Interger（包括0和负数）。

from collections import Counter

counter = Counter(words)
print(dict(counter))


from django.db.models import get_model
from ncgmp.config import GeoServerConfig
from catalog import Catalog

class LayerGenerator:
    gsmlpModelNames = ["GeologicUnitView"]

    def __init__(self, geomap):
        self.gsmlpModelsToLoad = [ get_model("ncgmp", modelName) for modelName in self.gsmlpModelNames ]
        self.gm = geomap
        self.cat = Catalog(GeoServerConfig.BaseGeoserverUrl + "rest", GeoServerConfig.GeoserverAdminUser, GeoServerConfig.GeoserverAdminSecret)

        self.gsmlpWs = self.cat.get_workspace("gsmlp")
        if self.gsmlpWs is None:
            self.gsmlpWs = self.cat.create_workspace("gsmlp", "http://xmlns.geosciml.org/geosciml-portrayal/1.0")

        self.cat.create_postgis_datastore(self.gsmlpWs)

    def createNewLayers(self):
        newLayers = []

        for model in self.gsmlpModelsToLoad:
            newLayers.append(self.cat.create_postgis_sql_layer(self.gm, self.gsmlpWs, model))

        return newLayers


from django.db import models
from datetime import datetime

class Aluno(models.Model):
    num_mat = models.AutoField(primary_key=True)
    nome = models.CharField(max_length=100, null=False, blank=False, default="", verbose_name="* Nome do Aluno",
                            help_text="Insira o nome completo do Aluno, com limite de 100 caracteres.")

    def __str__(self):
        return self.nome

class Disciplina(models.Model):
    codigo = models.AutoField(primary_key=True)
    titulo = models.CharField(max_length=50, null=False, blank=False, default="", verbose_name="* Nome da Disciplina",
                              help_text="Insira o nome da Disciplina, com limite de 50 caracteres.")

    def __str__(self):
        return self.titulo

class Professor(models.Model):
    nfunc = models.AutoField(primary_key=True)
    nome = models.CharField(max_length=100, null=False, blank=False, default="", verbose_name="* Nome do Professor",
                            help_text="Insira o nome completo do Professor, com limite de 100 caracteres.")
    endereco_cidade = models.CharField(max_length=100, null=False, blank=False, default="", verbose_name="* (Endereço) Cidade",
                                       help_text="Insira a cidade de moradia do Professor, com limite de 100 caracteres.")
    endereco_bairro = models.CharField(max_length=100, null=False, blank=False, default="", verbose_name="* (Endereço) Bairro",
                                       help_text="Insira o bairro de moradia do Professor, com limite de 100 caracteres.")
    endereco_rua = models.CharField(max_length=100, null=False, blank=False, default="", verbose_name="* (Endereço) Rua",
                                    help_text="Insira a rua de moradia do Professor, com limite de 100 caracteres.")
    endereco_numero = models.CharField(max_length=10, null=False, blank=False, default="", verbose_name="* (Endereço) Número",
                                       help_text="Insira o número da moradia do Professor, com limite de 10 caracteres.")
    endereco_complemento = models.CharField(max_length=50, default="Sem complemento", verbose_name=" (Endereço) Complemento",
                                            help_text="Insira o complemento da moradia do Professor, com limite de 50 caracteres.")
    fone = models.CharField(max_length=20, null=False, blank=False, default="(99) 99999-9999",
                            verbose_name="* Telefone do Professor", help_text="Insira o telefone do Professor.")
    titulacao = models.CharField(max_length=50, null=False, blank=False, default="", verbose_name="* Titulação do Professor",
                                 help_text="Insira a titulação do Professor, com limite de 50 caracteres.")

    def __str__(self):
        return self.nome

class Matricula(models.Model):
    ano = models.IntegerField()
    faltas = models.IntegerField()
    nota_final = models.FloatField()
    aluno = models.ForeignKey(Aluno, on_delete=models.CASCADE)
    disciplina = models.ForeignKey(Disciplina, on_delete=models.CASCADE)

    def __str__(self):
        return self.aluno.nome

class Alocacao(models.Model):
    ano_letivo = models.IntegerField()
    carga = models.IntegerField()
    horario = models.TimeField(auto_now=True)
    disciplina = models.ForeignKey(Disciplina, on_delete=models.CASCADE)
    professor = models.ForeignKey(Professor, on_delete=models.CASCADE)

    def __str__(self):
        return self.professor.nome