import threading
import time
import math
from jira import JIRA
from everisJIO import everisJIO
from fastUtils import fastUtils
from getSubtasks import getSubtasks
from workloadSQL import workloadSQL
import logging
from logger import logger

options = {
    'server': 'https://steps.everis.com/jiraesp2/'
    }

log = logger.getCustomLogger("workloadThreads")
log.debug(" -- Iniciando workloadThreads --")

numThreads = 48
allrunEsp = 'ALLRUN-1079'

cargaInicial = "project='Servicio RUN' and issuetype not in subTaskIssueTypes()"
lastDay = "project='Servicio RUN' and issuetype not in subTaskIssueTypes() and ( updated>-1d  or issuefunction in parentsOf('updated > -1d'))"
busqAllRUNEsp = "Project='Servicio RUN' and key='"+allrunEsp+"'"

print(" -- WORKLOADTHREADS START --")

jira = JIRA(options, auth=('mespinof', '!L1berate45'))

totalIssues = jira.search_issues(lastDay,startAt=0, maxResults=1).total
batchPerThread = int(math.ceil(totalIssues/numThreads))

print("Division: {}".format(batchPerThread))

allruns = []

def getIssues(startPoint, batchResults, thread):
    print("\n---- Se inicia el Thread{} ----\n".format(thread))
    print("\n---- Thread{}:\n---- startPoint:{}\n---- batchResults{}:".format(thread,startPoint,batchResults))
    #issues = jira.search_issues("project='Servicio RUN' and issuetype not in subTaskIssueTypes()",startAt=startPoint, maxResults=batchResults)
    issues = jira.search_issues(lastDay,startAt=startPoint, maxResults=batchResults, expand='changelog')
    for x in issues:
        allrun = everisJIO(x)
        allrun.worklog = jira.worklogs(allrun.id)
        count = 0
        for y in x.fields.subtasks:
            try:
                allrun.subtasks.append(getSubtasks.getSubtask(y.key, jira))
                allrun.subtasks[count].worklog = jira.worklogs(y.key)
            except AttributeError:
                print("Ha habido un problema recuperando el worklog de la tarea: {}".format(y.key))
            count+=1
        allruns.append(allrun)

threads_list = []
startPoint = 0
startTime = time.time()

print("\n\n ----------  INICIAMOS EXTRACCIÓN ---------- \n\n")
for i in range(numThreads):
    t = threading.Thread(target = getIssues,
                         name = 'thread{}'.format(i),
                         args = (startPoint,batchPerThread,i))
    threads_list.append(t)
    t.start()
    startPoint += batchPerThread

for t in threads_list:
    t.join()

endTime = time.time()
print("\n\n Allruns extraídas: {}  -- Tiempo transcurrido: {} \n\n".format(len(allruns),(endTime-startTime)))
print("\n\n ----------  FINALIZAMOS EXTRACCIÓN ---------- \n\n")

print("\n\n ----------  INSERTAMOS EN BBDD  -----------\n\n")
workloadSQL.insertRegisters(allruns)
endTime = time.time()
print("\n\n Allruns grabadas en BBDD: {}  -- Tiempo transcurrido: {} \n\n".format(len(allruns),(endTime-startTime)))
print("\n\n ----------  GRABADO EN BBDD  -----------\n\n")


# 多线程UDP聊天器
"""
编写一个两线程的程序
线程1负责接收和显示信息
线程2用来获取键盘数据然后通过udp发送数据
"""
import threading
import socket

def sendmsg(sock):
    text = input("输入：")
    # 发送的数据必须是bytes格式
    code = text.encode()
    # 发送
    sock.sendto(code, ("127.0.0.1", 7852))

def recvmsg(sock):
# 接收 200表示本次接收的最大字节

    date,addr= sock.recvfrom(200)
    date = date.decode()
    print(date)

def main():
    # 建立socket通讯连接
    sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    sock.bind(("127.0.0.1",7852))
    sen=threading.Thread(target=sendmsg,args=(sock,))
    revc=threading.Thread(target=recvmsg,args=(sock,))

    sen.start()
    revc.start()

if __name__=="__main__":
    main()


# coding:utf-8

from bottle import route, post, request, redirect, jinja2_template as template
import app.models.Scheduler
model = app.models.Scheduler.Scheduler()

@route('/')
def index():
    result = model.load()
    return template('index', result = result)

@route('/new')
def new():
    return template('new')

@route('/edit/<id:int>')
def edit(id):
    return template('edit', i=model.edit(id))

@post('/done')
def done():
    post_data = {}
    post_data['name'] = request.forms.get('name')
    post_data['des'] = request.forms.get('des')
    post_data['id'] = request.forms.get('id')
    post_data['del'] = request.forms.get('del')
    model.done(post_data)

    redirect('/')


# -*- coding: utf-8 -*-

"""main module.

@route '/'
@route 'web_hook'
"""

import os
import requests
import json

from flask import Flask
from flask import request

from doco.client import Client

from models import db
from models import User
from models import Flag
from models import Reply
from modules.bot import Bot
from modules.message import Quiz
from modules.utils import GeneralUtility
from modules.utils import Config
from modules.utils import Header
from modules.webhook import ActionEvents
from modules.webhook import Events
from modules.yahoo_api import KeyPhrase

app = Flask(__name__)

# set database
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']

db.init_app(app)

config = Config('./setting.conf')
ACCESS_TOKEN = config.get_access_token()
ENDPOINT_URL = config.get_endpoint_url()
APP_ID = config.get_appid()

PROXY = {
    "https": os.environ.get('FIXIE_URL', ''),
    "http": os.environ.get('FIXIE_URL', '')
}

@app.before_first_request
def create_database():
    db.create_all()

    if not db.session.query(Flag).filter(Flag.key == "reply_flag").count():
        reg = Flag("reply_flag")
        reg.set_value(0)
        db.session.add(reg)
        db.session.commit()

@app.route('/')
def index_route():
    return 'Running!'

@app.route('/web_hook', methods=['POST'])
def webhook_route():
    bot = Bot()

    # create header
    header = Header(ACCESS_TOKEN)
    content_header = header.content_header()

    # request event
    event = request.json['events'][0]

    if 'postback' in event:
        try:
            events = ActionEvents(event)
            reply_token = events.replyToken
        except:
            raise

        if events.postback.data == "o":
            content = bot.text_message_content(reply_token, "せいかいだよ！")
        else:
            content = bot.text_message_content(reply_token, "いいえ")

    else:
        try:
            events = Events(event)
            reply_token = events.replyToken
        except:
            raise

        userid = events.source.userId
        if not db.session.query(User).filter(User.userid == userid).count():
            reg = User(events.source.userId)
            db.session.add(reg)
            db.session.commit()

        # extract user
        user = User.query.filter(User.userid == userid).first()
        # extract reply flag
        reply_flag = Flag.query.filter(Flag.key == "reply_flag").first()

        reg = Reply(events.source.userId, events.message.text)

        try:
            keyphrase = KeyPhrase(APP_ID, events.message.text)
            keywords = keyphrase.keywords
        except:
            keywords = {}

        # search image
        utility = GeneralUtility()
        if len(keywords) != 0 and utility.get_match_key(keywords, "画像"):
            image_url = bot.get_image(' '.join(keywords.keys()))
            content = bot.image_message_content(reply_token, image_url)
        elif events.message.text == "なぞなぞ":
            quiz = Quiz("data/quiz.csv")
            quiz_text, actions = quiz.create_quiz()
            content = bot.buttons_message_content(
                reply_token,
                quiz_text,
                actions
            )
        elif reply_flag.value == 1:
            reply_flag.set_value(0)
            user.set_username(events.message.text)

            db.session.add(reply_flag)
            db.session.add(user)
            db.session.commit()

            content = bot.text_message_content(
                reply_token,
                "わかった！よろしくね、%s!" % user.username
            )
        elif user.username is None:
            reply_flag.set_value(1)

            db.session.add(reply_flag)
            db.session.commit()

            content = bot.text_message_content(
                reply_token,
                "あなたのお名前はなあに？"
            )
        else:
            docomo_client = Client(apikey=config.get_docomo_apikey())
            content = bot.text_message_content(
                reply_token,
                docomo_client.send(
                    utt=events.message.text,
                    apiname='Dialogue',
                    **{'nickname': user.username}
                )['utt']
            )

    request_body = content.get_request_body()

    # post request
    res = requests.post(
        ENDPOINT_URL,
        data=json.dumps(request_body),
        headers=content_header,
        proxies=PROXY
    )
    print(res)

    return 'Running'

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


# Freddie He
# 1560733
# heyingge

import sys
import time

class Graph(object):
    def __init__(self):
        self.bc = []
        self.ap = set()
        self.nodes = dict()
        self.dfsCounter = 0
        self.dfsNum = dict()
        self.low = dict()
        self.stack = []
        self.rootChildNum = 0

    # a helper function for dfs()
    def dfs_helper(self, v, parent):
        self.dfs(v, parent)

    # recursively explore the graph using DFS
    def dfs(self, v, parent):
        # initialization
        self.dfsCounter += 1
        self.dfsNum[v] = self.dfsCounter
        self.low[v] = self.dfsNum[v]

        for x in self.nodes[v]:
            # x is first time discovered
            if self.dfsNum[x] == -1:
                self.stack.append((v, x))  # first time see this edge, add to edge stack
                self.dfs(x, v)  # recursively find DFS of x
                self.low[v] = min(self.low[v], self.low[x])

                # v is the articulation point splitting x
                if self.low[x] >= self.dfsNum[v]:
                    # if not root, then v is an articulation point
                    if self.dfsNum[v] != 1:
                        self.ap.add(v)
                    self.bc.append([])
                    end = 1
                    # keep popping out edges until find the pair(v, x)
                    while end:
                        edge = self.stack.pop()
                        self.bc[-1].append(edge)
                        if edge == (v, x):
                            end = 0

                # increment the child number
                if self.dfsNum[v] == 1:
                    self.rootChildNum += 1
            elif x != parent:
                self.low[v] = min(self.low[v], self.dfsNum[x])
                if self.dfsNum[v] > self.dfsNum[x] and self.dfsNum[x] != -1:
                    self.stack.append((v, x))  # first time see this edge, add to edge stack

        # check if the root is the articulation point
        if self.rootChildNum > 1:
            self.ap.add(v)

# read vertices and edges
def readfile(filename, gr):
    with open(filename) as f:
        content = f.read().splitlines()
    for line in content:
        vertex = line.split()

        # if is the first time
        if len(vertex) == 1:
            print('There are ' + vertex[0] + ' nodes.')
        else:
            v0 = int(vertex[0])
            v1 = int(vertex[1])

            # fist time see v0
            if v0 not in gr.nodes:
                gr.nodes[v0] = set()
                gr.dfsNum[v0] = -1
                gr.low[v0] = -1
            gr.nodes[v0].add(v1)

            # first time see v1
            if v1 not in gr.nodes:
                gr.nodes[v1] = set()
                gr.dfsNum[v1] = -1
                gr.low[v1] = -1
            gr.nodes[v1].add(v0)
    print('There are ' + str(len(content) - 1) + ' edges.')

def main():
    for filename in sys.argv[1:]:
        gr = Graph()
        readfile(filename, gr)

        # start timing
        start = time.time()
        gr.dfs_helper(gr.nodes.keys()[0], -1)
        end = time.time()

        print('There are ' + str(len(gr.bc)) + ' biconnected components.')
        print('There are ' + str(len(gr.ap)) + ' articulation points.')
        print('Articulation points are ' + str(gr.ap) + '.')
        print('Edges in each biconnected component are ' + str(gr.bc) + '.')
        print('Run time: ' + str(end - start) + "s.")

if __name__ == "__main__":
    main()


# coding:utf-8
from os.path import abspath, dirname
project_dir = dirname(dirname(abspath(__file__)))

markets = {
    'sz': {'id': 0, 'name': 'sz', 'filter': lambda x: True if x.startswith('00') or x.startswith('30') else False},
    'sh': {'id': 1, 'name': 'sh', 'filter': lambda x: True if x.startswith('60') else False},
    'hk': {'id': 31, 'name': 'hk'}
}
categories = {
    'index_month': 6,
    'index_week': 5,
    'index_day': 4,
    'index_min5': 0,
    'index_min15': 1,
    'index_min30': 2,
    'index_min60': 3,
    'kline_month': 6,
    'kline_week': 5,
    'kline_day': 4,
    'kline_min5': 0,
    'kline_min15': 1,
    'kline_min30': 2,
    'kline_min60': 3,
}
index_dict = {
    # 'sz': ['399001'],
    'sz': ['399001', '399106', '399005', '399101', '399006', '399102'],
    # 深圳 深证成指（399001） 深圳综指（399106）中小板指（399005）中小板综（399101）创业板指（399006）创业板综（399102）
    'sh': ['000001', '000300', '000905', '000016'],}  # 上海 上证指数（000001） 沪深500（000300）中证500（000905） 上证50（000016）
def detect_market_id(stock_code):
    if markets['sh']['filter'](stock_code):
        return markets['sh']['id']
    elif markets['sz']['filter'](stock_code):
        return markets['sz']['id']
    else:
        return None

unit_conversion = {
    'month': 1,
    'week': 1,
    'day': 1,
    'min60': 4,
    'min30': 8,
    'min15': 16,
    'min5': 48
}


from pathlib import Path

import pytest

from ogc import run
from ogc.enums import SpecCore
from ogc.exceptions import SpecProcessException
from ogc.spec import SpecJobMatrix, SpecJobPlan, SpecLoader, SpecPlugin
from ogc.state import app

fixtures_dir = Path(__file__).parent / "fixtures"

@pytest.fixture(scope="module")
def runners():
    """ Fixture with the parsed runners
    """
    spec = SpecLoader.load([fixtures_dir / "spec.yml"])
    return [job for job in spec["plan"]]

def test_matrix_combos(mocker):
    """ Test all combos are present
    """
    mocker.patch("ogc.state.app.log")
    spec = SpecLoader.load([fixtures_dir / "spec-matrix.yml"])
    matrixes = SpecJobMatrix(spec[SpecCore.MATRIX])
    combos = matrixes.generate()
    assert {
        "snap_version": "1.18/edge",
        "series": "focal",
        "channel": "stable",
        "arch": "arm64",
    } in combos
    assert {
        "snap_version": "1.17/stable",
        "series": "bionic",
        "channel": "stable",
        "arch": "arm64",
    } in combos
    assert {
        "snap_version": "1.15/edge",
        "series": "xenial",
        "channel": "edge",
        "arch": "amd64",
    } in combos

def test_matrix_replace_env_var(mocker):
    """ Tests that a matrix variable updates an referenced environment variable
    """
    mocker.patch("ogc.state.app.log")
    spec = SpecLoader.load([fixtures_dir / "spec-matrix.yml"])
    matrixes = SpecJobMatrix(spec[SpecCore.MATRIX])
    jobs = [SpecJobPlan(spec[SpecCore.PLAN], matrix) for matrix in matrixes.generate()]
    jobs[0].env()
    assert app.env["JUJU_CONTROLLER"] == "validate-ck-{}".format(
        jobs[0].matrix["series"]
    )
    assert app.env["JUJU_DEPLOY_CHANNEL"] == jobs[0].matrix["channel"]

def test_yml_include_spec(mocker):
    mocker.patch("ogc.state.app.log")
    spec = SpecLoader.load([fixtures_dir / "spec-base.yml"])
    assert "juju" in spec["plan"][0]["before-script"][0]

def test_nested_assets(mocker):
    mocker.patch("ogc.state.app.log")
    spec = SpecLoader.load([fixtures_dir / "spec.yml"])
    plug = SpecPlugin(spec[SpecCore.PLAN][0]["script"][3]["runner"])
    assets = plug.opt("assets")
    assert assets[0]["name"] == "pytest configuration"

def test_cmd_to_env(mocker):
    mocker.patch("ogc.state.app.log")
    spec = SpecLoader.load([fixtures_dir / "spec.yml"])
    _env = {}
    _env["BONZAI"] = "bonzai-test"
    _env["ANOTHERTIME"] = "this happened late"
    _env["VAR_NICE"] = "interesting concept"
    _env["CONTROLLER"] = "juju-controller"
    _env["MODEL"] = "juju-model"
    app.env = _env
    spec = SpecPlugin(spec[SpecCore.PLAN][0]["script"][0]["runner"])
    cmd = spec.opt("cmd")
    assert cmd == (
        "echo bonzai-test lthis happened "
        "late envinteresting concept "
        "juju-controller:juju-model "
        "juju-controller juju-model"
    )

def test_get_option_env_key(mocker):
    """ Tests that an environment variable set for a given option is converted
    into the hosts environment setting """
    mocker.patch("ogc.state.app.log")
    spec = SpecLoader.load([fixtures_dir / "spec.yml"])
    plug = SpecPlugin(spec[SpecCore.PLAN][0]["before-script"][0]["juju"])

    _env = {}
    _env["JUJU_CLOUD"] = "aws/us-east-1"
    _env["JUJU_CONTROLLER"] = "test-controller"
    _env["JUJU_MODEL"] = "test-model"
    app.env = _env
    assert plug.opt("cloud") == "aws/us-east-1"
    assert plug.opt("controller") == "test-controller"
    assert plug.opt("model") == "test-model"

def test_get_option_env_key_bool(mocker):
    """ Tests that get_plugin_option handles boolean values correctly
    """
    mocker.patch("ogc.state.app.log")
    spec = SpecLoader.load([fixtures_dir / "spec.yml"])
    plug = SpecPlugin(spec[SpecCore.PLAN][0]["before-script"][0]["juju"])

    _env = {}
    _env["JUJU_CLOUD"] = "aws/us-east-1"
    _env["JUJU_CONTROLLER"] = "test-controller"
    _env["JUJU_MODEL"] = "test-model"
    app.env = _env
    assert plug.opt("deploy.reuse") is True

def test_run_script_passes_check(mocker):
    """ Tests that we can run shell commands
    """
    mocker.patch("ogc.state.app.log")
    run.script("ls -l", env=app.env.copy())

def test_run_script_blob_passes_check(mocker):
    """ Tests that we can run shell scripts
    """
    mocker.patch("ogc.state.app.log")
    blob = """
#!/bin/bash
set -x
ls -l
"""
    run.script(blob, env=app.env.copy())

def test_run_script_fails_check(mocker):
    """ Tests that we can run shell scripts
    """
    mocker.patch("ogc.state.app.log")
    with pytest.raises(SpecProcessException):
        run.script("ls -l\necho HI\nexit 1", env=app.env.copy())

@pytest.mark.skip
def test_condition_if(mocker):
    """ Tests that a condition will skip a job item
    """
    mocker.patch("ogc.state.app.log")
    spec = SpecLoader.load([fixtures_dir / "spec-condition.yml"])
    matrixes = [SpecJobMatrix(matrix) for matrix in app.spec[SpecCore.MATRIX]]

    jobs = [SpecJobPlan(app.spec[SpecCore.PLAN], matrix) for matrix in matrixes]
    assert jobs[0].condition_if()
    assert jobs[1].condition_if()
    assert jobs[2].condition_if() is False


import numpy as np
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
from dataset.ModelNet40 import RegistrationData,ModelNet40
#from model.Pointnet import PointNet
from model.iPCRNet import iPCRNet
from model.MeshNet import MeshNet
from losses.chamfer_distance import ChamferDistanceLoss
import transforms3d
from losses.rmse_features import RMSEFeaturesLoss
from losses.frobenius_norm import FrobeniusNormLoss
from operations.transform_functions import PCRNetTransform

BATCH_SIZE=20
EVAL=False
START_EPOCH=0
MAX_EPOCHS=200
pretrained='checkpoints/ipcrnet/models2/best_model.t7'       #使用最好的模型参数测试

# Find error metrics.
def find_errors(igt_R, pred_R, igt_t, pred_t):
    # igt_R:				Rotation matrix [3, 3] (source = igt_R * template)
    # pred_R: 			Registration algorithm's rotation matrix [3, 3] (template = pred_R * source)
    # igt_t:				translation vector [1, 3] (source = template + igt_t)
    # pred_t: 			Registration algorithm's translation matrix [1, 3] (template = source + pred_t)

    # Euler distance between ground truth translation and predicted translation.
    igt_t = -np.matmul(igt_R.T, igt_t.T).T			# gt translation vector (source -> template)
    translation_error = np.sqrt(np.sum(np.square(igt_t - pred_t)))

    # Convert matrix remains to axis angle representation and report the angle as rotation error.
    error_mat = np.dot(igt_R, pred_R)							# matrix remains [3, 3]
    _, angle = transforms3d.axangles.mat2axangle(error_mat)
    return translation_error, abs(angle*(180/np.pi))

def compute_accuracy(igt_R, pred_R, igt_t, pred_t):
    errors_temp = []
    for igt_R_i, pred_R_i, igt_t_i, pred_t_i in zip(igt_R, pred_R, igt_t, pred_t):
        errors_temp.append(find_errors(igt_R_i, pred_R_i, igt_t_i, pred_t_i))
    return np.mean(errors_temp, axis=0)

def test_one_epoch(device, model, test_loader):
    model.eval()
    test_loss = 0.0
    count = 0
    errors = []

    for i, data in enumerate(tqdm(test_loader)):
        template, source, igt, igt_R, igt_t = data

        for index in range(5):
            template[index] = template[index].to(device)  # [B,N,3]
            source[index] = source[index].to(device)  # [B,N,3]

        # template = template.to(device)
        # source = source.to(device)
        igt = igt.to(device)

        # source_original = source.clone()
        # template_original = template.clone()
        igt_t = igt_t - torch.mean(source[4], dim=1).unsqueeze(1)
        # source[4] = source[4] - torch.mean(source[4], dim=1, keepdim=True)
        # template[4] = template[4] - torch.mean(template[4], dim=1, keepdim=True)
        for index in range(3):
            source[index] = source[index] - torch.mean(source[index], dim=2, keepdim=True)
            template[index] = template[index] - torch.mean(template[index], dim=2, keepdim=True)
        source[4] = source[4] - torch.mean(source[4], dim=1, keepdim=True)
        template[4] = template[4] - torch.mean(template[4], dim=1, keepdim=True)

        output = model(device, template, source)
        est_R = output['est_R']
        est_t = output['est_t']
        est_T = output['est_T']

        errors.append(compute_accuracy(igt_R.detach().cpu().numpy(), est_R.detach().cpu().numpy(),
                                       igt_t.detach().cpu().numpy(), est_t.detach().cpu().numpy()))

        # transformed_source = torch.bmm(est_R, source.permute(0, 2, 1)).permute(0,2,1) + est_t
        loss_val = ChamferDistanceLoss()(template[4], output['transformed_source'])

        test_loss += loss_val.item()
        count += 1

    test_loss = float(test_loss)/count
    errors = np.mean(np.array(errors), axis=0)
    return test_loss, errors[0], errors[1]

if __name__ == '__main__':
    testset = RegistrationData('PCRNet', ModelNet40(part='test'),is_testing=True)
    testloader = DataLoader(testset, batch_size=BATCH_SIZE, shuffle=False, drop_last=False, num_workers=4)
    device = torch.device('cpu')

    #ptnet = PointNet(emb_dims=1024)
    model = iPCRNet(feature_model=MeshNet)
    model = model.to(device)

    if pretrained:
        model.load_state_dict(torch.load(pretrained, map_location='cpu'))

    model.to(device)
    test_loss, translation_error, rotation_error = test_one_epoch(device, model, testloader)
    print("Test Loss: {}, Rotation Error: {} & Translation Error: {}".format(test_loss, rotation_error,translation_error))


import os
import sys

_minimum_version = (3, 5)

if sys.version_info < _minimum_version:
    raise RuntimeError('Required Python {}'.format(
        '.'.join([str(i) for i in _minimum_version])
    ))

version      = '1.5.1'
primary_cmd  = 'bin/gallium'
shortcut_cmd = 'bin/g{version}'.format(version = sys.version_info.major)
install_cmds = [primary_cmd]

if os.path.exists(shortcut_cmd):
    install_cmds.append(shortcut_cmd)

from distutils.core import setup

setup(
    name         = 'gallium',
    version      = version,
    description  = 'A micro CLI development framework',
    license      = 'MIT',
    author       = 'Juti Noppornpitak',
    author_email = 'juti_n@yahoo.co.jp',
    url          = 'https://github.com/shiroyuki/gallium',
    packages     = [
        'gallium',
        'gallium.cli',
        'gallium.ext',
        'gallium.loader',
        'gallium.model',
    ],
    classifiers = [
        'Development Status :: 5 - Production/Stable',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
        'Topic :: Software Development :: Libraries'
    ],
    scripts          = install_cmds,
    install_requires = [
        'kotoba',
        'imagination>=3.0.0',
        'pyyaml',
    ],
    python_requires = '>=3.5',
)


# -*- coding: utf-8 -*-
# Copyright (c) 2018, Vinayak Patil and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe import msgprint, _
from frappe.model.document import Document

class TPMonthsforApp(Document):
	pass

@frappe.whitelist()
def get_user(branch):
	branch="'"+branch+"'";

	return frappe.db.sql("""SELECT name,full_name FROM 1bd3e0294da19198.tabUser
where enabled=1 and branch={0}""".format(branch), as_dict=1);

@frappe.whitelist()
def get_tp_months(user_id):
	#datasets = [];
	user_id="'"+user_id+"'";
	tp_months_found=frappe.db.sql("""select concat(year,"-",if(month<10,concat('0',month),month))as "ym"
	from `tabTP Months for App` where active=1 and name in(select parent from `tabTP Months Active user`
	where user_id={0})""".format(user_id), as_dict=1);

	datasets1=[];
	for f in tp_months_found:

		###datasets1=[];
        	#frappe.msgprint(_(f.ym));
		#datasets1.append(f.ym);

		ym="'"+f.ym+"'";
		ym_="'"+f.ym+"-%'";
		days=frappe.db.sql(""" SELECT DAY(LAST_DAY(concat({0},'-01'))) as "dd" """.format(ym), as_dict=1);
		holiday_sun_cnt=frappe.db.sql("""SELECT count(case when description='Sunday' then 1 end)as "sunday",
		count(case when description!='Sunday' then 1 end)as "holiday_day" FROM 1bd3e0294da19198.`tabHoliday`
		where parent=(select holiday_list from tabEmployee where name =(SELECT employee_code from tabUser
		where name={0})) and holiday_date like {1} """.format(user_id,ym_), as_dict=1);
		tp_days_cnt=frappe.db.sql(""" select ifnull(count(case when tp_flag=1 then 1 end),0)as "tp_days_cnt",
		ifnull(count(case when doctor_flag=1 then 1 end),0)as "cnt_dcr",
		ifnull(count(case when meeting_flag=1 then 1 end),0)as "cnt_meeting",
		ifnull(count(case when camp_flag=1 then 1 end),0)as "cnt_camp",
		ifnull(count(case when leave_flag=1 then 1 end),0)as "cnt_leave"
		from 1bd3e0294da19198.`tabObjective`
		where select_date like {0}
		and user= {1} and tp_flag=1
		ORDER BY `tabObjective`.`name` DESC LIMIT 1""".format(ym_,user_id), as_dict=1);''', as_dict=1'''

		#frappe.msgprint(_('"' + 'days' + '":' + '"' + str(days[0].dd) + '"'));

		datasets1.append({ 'days':str(days[0].dd),
				  'sunday':str(holiday_sun_cnt[0].sunday)
				  ,'holiday_day':str(holiday_sun_cnt[0].holiday_day)
				  ,'tp_days_cnt':str(tp_days_cnt[0].tp_days_cnt)
				 ,'cnt_dcr':str(tp_days_cnt[0].cnt_dcr)
				 ,'cnt_meeting':str(tp_days_cnt[0].cnt_meeting)
				 ,'cnt_leave':str(tp_days_cnt[0].cnt_leave)
				 ,'ym':str(f.ym)});

		#datasets.append(datasets1);
		pass;
	return (datasets1);''', as_dict=1 datasets'''


import csv
from pprint import pprint

# incluyo la libreria "csv" en mi script
# Tambien el script pprint de la libreria pprint
# nos permite printear cosas por consola de forma mas ordenada

# Instancio una variable como un diccionario vacio
# https://docs.python.org/3/tutorial/datastructures.html#dictionaries
series = {}

# Abro el archivo .csv
with open('NetflixViewingHistory.csv') as csv_file:
	# Lo vuelvo legible
    csv_reader = csv.reader(csv_file, delimiter=',')

    # Comienzo a iterar por las filas del csv
    for fila in csv_reader:

        # "Spliteo" la el titulo de la fila en cada caracter ":", obtengo una lista
        # Ejemplo: fila[0] = "Community: Temporada 1: Modern Warfare"
        # "Community: Temporada 1: Modern Warfare".split(":") -> ["Community", " Temporada 1", " Modern Warfare"]
        lis = fila[0].split(":")
        if len(lis) >= 3: # si la longitud de la lista es >= 3, es una serie

            # Asigno el primer elemento de la lista a una variable titulo
            titulo = lis[0]
            # Ej: ["Community", " Temporada 1", " Modern Warfare"][0] -> "Community"

            # Si mi titulo ya es una key en mi diccionario lo incremento
            if titulo in series:
                series[titulo] += 1
            # Si no, creo la llave y le asigno el valor 1
            else:
                series[titulo] = 1

# Ej: series = {"Community": 992, "Friends" : 235, "HIMYM":600}
#     series.items() -> [('Community', 992), ('Friends', 235), ('HIMYM', 600)]
#     series.items() retorna una lista de tuplas, de las parejas key/value

# Puedo ordenar esta lista de tuplas en funcion al segundo elemento de cada tupla
series_ord = sorted(series.items(), key = lambda item : item[1], reverse = True)

nro = 2

# Obtengo el enesimo elemento de la lista de tuplas
print(series_ord[nro])
print("La 3ra serie que mas veces viste es: " + series_ord[nro][0])


# -*- coding: utf-8 -*-

def max_present_value(matrix):

    row = len(matrix)
    col = len(matrix[0])

    # dp = [[0 for _ in range(col)] for _ in range(row)]
    #
    # for r in range(row):
    #     for c in range(col):
    #         if r == 0 and c == 0:
    #             dp[r][c] = matrix[r][c]
    #             continue
    #         elif r == 0:
    #             dp[r][c] = dp[r][c - 1] + matrix[r][c]
    #         elif c == 0:
    #             dp[r][c] = dp[r - 1][c] + matrix[r][c]
    #         else:
    #             dp[r][c] = max(dp[r - 1][c], dp[r][c - 1]) + matrix[r][c]
    #
    # return dp[-1][-1]

    dp = [0 for _ in range(col * row)]

    for r in range(row):
        for c in range(col):
            if r == 0 and c == 0:
                res = 0
            elif r == 0:
                res = dp[r * col + c - 1]
            elif c == 0:
                res = dp[(r - 1) * col + c]
            else:
                res = max(dp[(r - 1) * col + c], dp[r * col + c - 1])
            dp[r * col + c] = res + matrix[r][c]
    print(dp)
    return dp[-1]

max_present_value([

    [1, 10, 3, 8],
    [12, 2, 9, 6],
    [5, 7, 4, 11],
    [3, 7, 16, 5]
])


import webapp2
from google.appengine.api import users
import jinja2
import os
import string

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class Error(webapp2.RequestHandler):
    def get(self):

        user = users.get_current_user()
        if user:

            #set stylesheets needed per page
            specific_urls = """
                <link type="text/css" rel="stylesheet" href="/stylesheets/""" + self.__class__.__name__ + """.css" />
            """

            #add the page query to the html
            url = self.request.url
            url = string.split(url, '/')
            Error_template_values = {
                'page': url[len(url) - 1]
            }

            template = jinja_environment.get_template('Page_Content/error.html')
            Error_template = template.render(Error_template_values)

            url = users.create_logout_url(self.request.uri)
            nav = """

Dashboard

Design

About

Logout

            """ % url

            template_values = {
                'specific_urls':specific_urls,
                'nav': nav,
                'content': Error_template
            }

            template = jinja_environment.get_template('index.html')
            self.response.out.write(template.render(template_values))
        else:
            self.redirect('/')

app = webapp2.WSGIApplication([('/.*', Error)], debug=True)


import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error
from sklearn.externals import joblib
import argparse
import os
# 加上48小时前obs 信息
# 处理 RAIN 值 去除 35以上数值
target_list=['t2m','rh2m','w10m']
from datetime import timedelta
from datetime import datetime
def datelist(beginDate, endDate):
    date_l=[datetime.strftime(x,'%Y-%m-%d') for x in list(pd.date_range(start=beginDate, end=endDate))]
    return date_l

if __name__=='__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--log-level', dest='log_level', default='info', type=str,
                        help='Logging level.')
    parser.add_argument('--model_dir', dest='model_dir',
                        default='../checkpoints/lgb',
                        type=str)
    parser.add_argument('--data_dir', dest='data_dir',
                        default='../data/testb7/merge', type=str)

    parser.add_argument('--dst_dir', dest='dst_dir',
                        default='../result/lgb')
    parser.add_argument('--first_day', dest='first_day',
                        default='20181028', type=str)
    parser.add_argument('--last_day', dest='last_day',
                        default='20181103', type=str)

    opt = parser.parse_args()

    feature_columns = ['t2m_obs', 'rh2m_obs', 'w10m_obs', 'psur_obs', 'q2m_obs', 'u10m_obs',
                       'v10m_obs', 'RAIN_obs',
                       't2m_prophet', 'rh2m_prophet', 'w10m_prophet',
                       't2m_M', 'rh2m_M', 'w10m_M', 'hour_sin', 'hour_cos', 'month_sin', 'month_cos',
                       'psfc_M', 'q2m_M', 'u10m_M', 'v10m_M',
                       'SWD_M', 'GLW_M', 'HFX_M', 'RAIN_M', 'PBLH_M', 'TC975_M', 'TC925_M',
                       'TC850_M', 'TC700_M', 'TC500_M', 'wspd925_M', 'wspd850_M', 'wspd700_M', 'wspd500_M']
    if opt.model_dir.endswith('_q'):
        feature_columns = feature_columns + ['Q975_M', 'Q925_M', 'Q850_M', 'Q700_M', 'Q500_M', 'LH_M']

    history_num = 24

    begin_dates = datelist(pd.to_datetime(opt.first_day) - timedelta(days=2),
                           pd.to_datetime(opt.last_day) - timedelta(days=2))
    dst_dates = datelist(opt.first_day, opt.last_day)
    end_dates = datelist(pd.to_datetime(opt.first_day) + timedelta(days=1),
                         pd.to_datetime(opt.last_day) + timedelta(days=1))

    model_dir = opt.model_dir
    data_dir = opt.data_dir
    dst_dir = opt.dst_dir
    if not os.path.exists(dst_dir):
        os.mkdir(dst_dir)

    for begin_date,dst_date,end_date in zip(begin_dates,dst_dates,end_dates):
        end_date=end_date+' 12-00-00'
        whole_submit = None
        for i in range(90001, 90011):
            index_list = []
            for j in range(37):
                index_list.append(str(i) + '_' + '{:02d}'.format(j))
            index = pd.DataFrame(index_list, columns=['FORE_data'])

            results = []
            for feature_index in range(3):

                lgb_model = joblib.load(os.path.join(model_dir,'model_' + str(i) + '_feature_' + str(feature_index) + '.m'))

                data_pre = pd.read_csv(os.path.join(data_dir,'merged_' + str(i) + '.csv'), index_col=0)

                data_pre.index = pd.to_datetime(data_pre.index)
                data_pre['hour'] = data_pre.index.hour
                data_pre['month'] = data_pre.index.month
                hour_period = 24 / (2 * np.pi)
                data_pre['hour_cos'] = np.cos(data_pre.index.hour / hour_period)
                data_pre['hour_sin'] = np.sin(data_pre.index.hour / hour_period)

                month_period = 12 / (2 * np.pi)
                data_pre['month_cos'] = np.cos(data_pre.index.month / month_period)
                data_pre['month_sin'] = np.sin(data_pre.index.month / month_period)

                data_pre['u10m_obs']=data_pre['u10m_obs']/data_pre['w10m_obs']
                data_pre['v10m_obs']=data_pre['v10m_obs']/data_pre['w10m_obs']
                data_pre['u10m_M']=data_pre['u10m_M']/data_pre['w10m_M']
                data_pre['v10m_M']=data_pre['v10m_M']/data_pre['w10m_M']

                data_pre = pd.DataFrame(data_pre, columns=feature_columns)
                data_pre = data_pre[begin_date:end_date]
                for col in data_pre.columns:
                    data_pre[col] = data_pre[col].fillna(data_pre[col].mean())

                pre_data_float = np.array(data_pre)
                # print(pre_data_float.shape)
                history = list(pre_data_float[24:48, feature_index])
                result = []
                for k in range(37):
                    row_data = pre_data_float[48 + k:48 + k + 1, 8:]
                    curr_history = np.array(history)[k:k + history_num]
                    obs_48h_ago = pre_data_float[k:k + 1, :8]
                    # print(row_data.shape)
                    # print(curr_history.shape)
                    curr_history = curr_history.reshape((1, -1))
                    X = np.c_[row_data, curr_history, obs_48h_ago]
                    #print(X.shape)
                    y = lgb_model.predict(X)
                    # print(y.shape)
                    if k < 4:
                        result.append(pre_data_float[48 + k, feature_index])
                        history.append(pre_data_float[48 + k, feature_index])
                    else:
                        result.append(y[0])
                        history.append(y[0])
                # print('result:', result)
                rmse = np.sqrt(mean_squared_error(pre_data_float[48:, feature_index], np.array(result)))
                rmse_M = np.sqrt(
                    mean_squared_error(pre_data_float[48:, feature_index], pre_data_float[48:, feature_index +11]))
                results.append(result)

                print('rmse:', rmse)
                print('rmse_M:', rmse_M)
                print('score:', (rmse_M - rmse) / rmse_M)
            suffix=opt.model_dir.split('/')[-1]
            submit = pd.DataFrame(np.array(results).T, columns=['t2m_'+suffix, 'rh2m_'+suffix, 'w10m_'+suffix])
            submit = pd.concat([index, submit], axis=1)
            if whole_submit is None:
                whole_submit = submit
            else:
                whole_submit = pd.concat([whole_submit, submit], axis=0)

        whole_submit.to_csv(os.path.join(dst_dir,dst_date + '.csv'), index=False)


from elepoke import *

class SuperElepoke(Elepoke):
    def __init__(self, datas=pd.read_csv("./data/pokemon.csv", index_col=0)):
        super().__init__(datas)

    def _calc(self) -> list:
        """アルゴリズムのテンプレート

        Returns:
            list -- 計算結果
        """
        # 初期化
        self.result = [[], []]

        # 結果格納用の配列
        myPokeList = []
        opPokeList = []

        # ここに計算処理を記述する

        # 結果をセット
        self.result = [myPokeList, opPokeList]

        return self.result


"""add original title

Revision ID: daf0305d8ed1
Revises: 29afac58afde
Create Date: 2020-10-16 11:56:34.109483

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'daf0305d8ed1'
down_revision = '29afac58afde'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('videos', sa.Column('original_name', sa.Text(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('videos', 'original_name')
    # ### end Alembic commands ###


__author__ = 'HyNguyen'

import numpy as np
import copy
import sys

def sigmoid(Z):
    """
    Computes sigmoid function for each element of numpy array Z.
    """
    return 1.0 / (1.0 + np.exp(-Z))

def softmax(Z):
    """
    Computes softmax function for each row of numpy array Z.
    """
    A = np.exp(Z)
    A /= np.sum(A, axis=1, keepdims=True)
    return A

def one_hot(Y, K):
    """
    Convert Y from digits (0...9) into corresponding desired binany output
    """
    one_hot_Y = np.zeros((Y.shape[0], K))
    one_hot_Y[np.arange(Y.shape[0]), Y] = 1.0
    return one_hot_Y

def forward_prop(X, Ws):
    """
    Forward propagates X through layers of neural nets to get the final outputs.

    Parameters
    ----------
    X : numpy array, shape (N, d+1)
        The matrix of input vectors (each row corresponds to an input vector); the first column of
        this matrix is all ones (corresponding to x_0).
    Ws : list of numpy arrays
        The list of each layer's Ws; W of layer l will have the shape of (d^(l-1)+1, d^(l)) where
        d^(l-1) is the number of neurons (not count the +1 neuron) of layer l-1, and
        d^(l) is the number of neurons (not count the +1 neuron) of layer l.

    Returns
    -------
    (Zs, As): tuple
        As : list, length (numlayers+1)
            The list of Neural Net's output matrices; each element is a matrix of outputs of a layer.
        Zs : list, length (numlayers)
            The list of Neural Net's pre-activation matrices; each element is a matrix of
            pre-activations of a layer (the result of affine operator).
    """
    # Initialize the list of pre-activations and the list of outputs
    As = [X]
    Zs = []

    # Assign the first element of list of outputs to the input values
    A = X

    # Do feedforward propagation throughout the network
    for l in range(len(Ws)):
        # Compute the output of current layer by using affine operator
        Z = A.dot(Ws[l])

        # Check if current layer is the last layer
        if l == len(Ws) - 1:
            # use softmax regression in the last layer
            A = softmax(Z)
        else:
            # use sigmoid as activation function in hidden layers
            A = sigmoid(Z)

            # insert a column vector all element of which are ones
            # this comlumn correspond to the biases
            A = np.insert(A, 0, 1.0, axis=1)

        # Insert current outputs and preactivation into the lists
        Zs.append(Z)
        As.append(A)

    # Return the tuple containing the list of preactivations and the list of outputs
    return (Zs, As)

def backward_prop(Zs, As, Y, Ws):
    """
    Backward propagates X through layers of neural nets to get the gradient
    of weights for the cost function.

    Parameters
    ----------
    Zs : list, length (numlayers)
        The list of Neural Net's pre-activation matrices; each element is a matrix of
        pre-activations of a layer (the result of affine operator).
    As : list, length (numlayers+1)
        The list of Neural Net's output matrices; each element is a matrix of outputs of a layer.
    Y : numpy array, shape (N)
        The vector of outputs.
    Ws : list of numpy arrays
        The list of each layer's Ws; W of layer l will have the shape of (d^(l-1)+1, d^(l)) where
        d^(l-1) is the number of neurons (not count the +1 neuron) of layer l-1, and
        d^(l) is the number of neurons (not count the +1 neuron) of layer l.

    Returns
    -------
    grads : list, length (numlayers-1)
        The list of gradient of weight in each layers - grads; each element - grad will have
        the shape of (d^(l-1)+1, d^(l)) where d^(l-1) is the number of neurons (not count the +1 neuron)
        of layer l-1, and d^(l) is the number of neurons (not count the +1 neuron) of layer l
    """
    # Initialize gradient of weighs
    grads = [np.zeros(W.shape) for W in Ws]

    # Compute delta corresponding to the last layer - the 'delta' term measures
    # how much that each neuron was responsible for any error
    delta = As[-1] - Y

    # Compute the gradient of weight corresponding the last layer
    grads[-1] = As[-2].T.dot(delta)

    # Next, we compute gradient of weights corresponding to the rest layer
    for l in range(2, len(Ws)+1):
        # compute delta corresponding to the current layer
        delta = delta.dot(Ws[-l+1][1:].T) * (As[-l][:, 1:] * (1 - As[-l][:, 1:]))

        # compute gradient of weights corresponding to the current layer
        grads[-l] = As[-l-1].T.dot(delta)

    # Return the gradient of weights throughout the network
    return grads

def evaluate(X, Y, Ws):
    """
    Evaluate the performance of Neural Network by computing the Mean Binary Error.

    Parameters
    ----------
    X : numpy array, shape (N, d + 1)
        The matrix of input vectors (each row corresponds to an input vector); the first column of
        this matrix is all ones (corresponding to x_0).
    Y : numpy array, shape (N)
        The vector of outputs.
    Ws : list of numpy arrays
        The list of each layer's W's; W of layer l will have the shape of (d^(l-1)+1, d^(l)) where
        d^(l-1) is the number of neurons (not count the +1 neuron) of layer l-1, and
        d^(l) is the number of neurons (not count the +1 neuron) of layer l.
    ----------
    """
    # Forward propagation for computing the output computing by the network
    (Zs, As) = forward_prop(X, Ws)

    # Compute the probalities correponding to each class
    p_y = As[-1]

    # Get predicted classes
    pred_Y = np.argmax(p_y, axis=1)

    # Compute mean binary error between predicted classes and true classes
    mbe = np.mean(pred_Y != Y)

    return As[-1],pred_Y,mbe

def train_neural_net(X_train, Y_train, X_val, Y_val, layer_sizes, learning_rate, mnb_size, max_patience,
                     l2_reg_level):
    """
    Trains Neural Net on the dataset (X_train, Y_train).
    Cost function: Mean Negative Log Likelihood + L2 regularization.
    Optimization algorithm: Stochastic Gradient Descent (SGD) with early stopping.

    Parameters
    ----------
    X_train : numpy array, shape (N, d + 1)
        The matrix of input vectors (each row corresponds to an input vector) of the training set;
        the first column of this matrix is all ones (corresponding to x_0).
    Y_train : numpy array, shape (N,)
        The vector of outputs of the training set.
    X_val : numpy array, shape (N_val, d + 1)
        The matrix of input vectors (each row corresponds to an input vector) of the validation set;
        the first column of this matrix is all ones (corresponding to x_0).
    Y_val : numpy array, shape (N_val,)
        The vector of outputs of the validation set.
    layer_sizes : list of ints
        The list of each layer' size (not count the +1 neurons).
        E.g. layer_sizes = [784, 30, 10] means: the 1st layer (input layer) has 784 neurons,
        the 2nd layer (hidden layer) has 30 neurons, the 3rd layer (output layer) has 10 neurons.
    learning_rate : float
        Learning rate of SGD.
    mnb_size : int
        Minibatch size of SGD.
    max_patience : int
        The parameter of early stopping. You'll have a `patience` variable with initial value equal to
        `max_patience`. During the training, you'll keep track of the best MBE (Mean Binary Error)
        on the validation set; if the MBE on the validation set at the current epoch < the current
        best one, you'll reset `patience` to `max_patience`; otherwise, `patience` -= 1.
        When `patience` = 0, you'll terminate SGD.
    l2_reg_level : float
        The level (the coefficient) of L2 regularization.

    Returns
    -------
    (best_Ws, train_errs, val_errs) : tuple
        best_Ws : list of numpy arrays
            The list of each layer's W; W of layer l will have the shape of (d^(l-1)+1, d^(l)) where
            d^(l-1) is the number of neurons (not count the +1 neuron) of layer l-1, and
            d^(l) is the number of neurons (not count the +1 neuron) of layer l.
            It's the parameters having smallest MBE on the validation set.
        train_errs: list of floats
            List of MBEs on the training set after each epoch.
        val_errs: list of floats
            List of MBEs on the validation set after each epoch.

    Notes
    -----
    After each epoch, you need to print out:
    - The MBE on the training set and validation set.
    - The value of `patience`.
    E.g., 'Epoch ..., training err ..., val err ..., patience ...'.

    After the training, you need to print out:
    - The best MBE on the validation set.
    - The corresponding epoch.
    - The corresponding MBE on the training set.
    E.g., "Best val err ..., at epoch ..., corresponding train err ...".
    """
    # Init weights
    np.random.seed(0) # Note: this will fix the randomization so that we'll get the same random numbers each run;
                      # it make comparisons (e.g. between different values of `l2_reg_level`) more accurate.
    Ws = [np.random.randn(layer_sizes[l]+1, layer_sizes[l+1]) / np.sqrt(layer_sizes[l]+1) for l in range(len(layer_sizes)-1)]

    # Convert the vector of outputs into one_hot style
    one_hot_Y_train = one_hot(Y_train, layer_sizes[-1])

    # Get the number of examples in the training set
    N_train = X_train.shape[0]

    # Create a indexing array for shuffling examples in the training set
    rand_idxs= list(range(N_train))

    # Initialize lists of validation error and training error after each epoch
    train_errs = []
    val_errs = []

    # Initialize patience for tracking the peformance of the neuralnet
    patience = max_patience

    # Initialize some information corresponding to the epoch having best error on validation set
    best_epoch = 1
    best_mbe_train = 100
    best_mbe_val = 100
    best_Ws = None

    # Initialize the first epoch
    epoch = 1
    sys.stdout.write ("===> Start Training\n" )
    # Excute training procedure to update the weights of the network
    while 1:
        # Shuffle the example in training set by shuffling its indexs
        np.random.shuffle(rand_idxs)

        # Use Stochastic Gradient Descent Algorithm to update the weights of the network
        for start_idx in range(0, N_train, mnb_size):
            # Get a mini-batch from dataset
            mnb_X = X_train[rand_idxs[start_idx:start_idx+mnb_size]]
            mnb_Y = one_hot_Y_train[rand_idxs[start_idx:start_idx+mnb_size],:]

            # Forward propagation for getting ouputs and pre-activation throughout the network
            (Zs, As) = forward_prop(mnb_X, Ws)

            # Backward propagation for getting gradients of weights throughout the network
            grads = backward_prop(Zs, As, mnb_Y, Ws)

            # Update the weights throughout the network based on its gradient
            for i in range(len(Ws)):
                # update the weights of i-th layer
                Ws[i][1:, :] = (1 - learning_rate * l2_reg_level) * Ws[i][1:, :] - (learning_rate / mnb_size) * grads[i][1:, :]

                # update the biases of the i-th layer
                Ws[i][0, :] = Ws[i][0, :] - (learning_rate / mnb_size) * Ws[i][0, :]

        # Evaluate the performance of the network on training set and validation set at current epoch
        _,_,mbe_train = evaluate(X_train, Y_train, Ws)
        _,_,mbe_val = evaluate(X_val, Y_val, Ws)

        # Add current error into tracking list
        train_errs.append(mbe_train)
        val_errs.append(mbe_val)

        # print out some information of current epoch
        sys.stdout.write ('Epoch #{0}, training err = {1}, val err = {2}, patience = {3}\n'.format(epoch, mbe_train, mbe_val, patience))

        # Use Early Stopping strategy to have generalization
        # First, save some information for tracking if the the validation error of current epoch is the best
        if (mbe_val < best_mbe_val or epoch == 1):
            best_Ws = copy.deepcopy( Ws)
            best_mbe_train = mbe_train
            best_mbe_val = mbe_val

            # reset patience ((or initialize in case the first epoch))
            patience = max_patience
        else:
            patience -= 1

        # Add up epoch
        epoch += 1

        # Stop when cannot wait anymore
        if patience == 0:
            break

    # Print out some information corresponding to the epoch having best error on validation set
    sys.stdout.write ("===> Training Finish\n"    )
    sys.stdout.write ('Best val err: {0}, at epoch #{1}, corresponding train err: {2}\n'.format(best_mbe_val, best_epoch, best_mbe_train))

    # Return the tuple of learned weights, lists of error on training set and validation set
    return (best_Ws, train_errs, val_errs)

if __name__ == "__main__":
    sys.stdout.write("ttdtilu")
    # X_train, Y_train , X_valid, Y_valid = load_data()
    #
    #
    # # (best_Ws_1, train_errs_1, val_errs_1) = train_neural_net(X_train, Y_train, X_valid, Y_valid, layer_sizes=[1342, 500, 3], learning_rate=0.1, mnb_size=100, max_patience=20, l2_reg_level=0)
    # #
    # # with open("model/mynn.pickle", mode="wb") as f:
    # #     pickle.dump(best_Ws_1,f)
    #
    # print("valid model")
    # with open("model/mynn.pickle", mode="rb") as f:
    #     best_ws = pickle.load(f)
    #
    # Zs, As = forward_prop(X_valid,best_ws)
    #
    # pred_Y = np.argmax(As[-1], axis=1)
    # print(pred_Y)
    #
    # print (metrics.classification_report(Y_valid, pred_Y))
    #
    #
    # print("valid model")
    # X_test, Y_test = load_test_data()
    #
    # Zs, As = forward_prop(X_test,best_ws)
    #
    # pred_test_Y = np.argmax(As[-1], axis=1)
    #
    # print(pred_test_Y)
    # print(Y_test)
    # print (metrics.classification_report(Y_test, pred_test_Y))


# -*- coding: utf-8 -*-
"""***************************************************************************
*   Copyright (C) 2009 by Derek Kite   *
*   dkite@shaw.ca   *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************"""

from PyQt4 import QtGui, QtCore
import idfglobals

class newClassDialog(QtGui.QDialog):
    def __init__(self,parent=None,flags=0):
        QtGui.QDialog.__init__(self,parent)
        hl = QtGui.QHBoxLayout()
        vl = QtGui.QVBoxLayout(self)
        self.classlist = QtGui.QTreeWidget()
        self.classlist.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
        self.memo = QtGui.QLabel('Class Description')
        ccl = QtGui.QHBoxLayout()
        self.copycount = QtGui.QSpinBox()
        self.copycount.setMinimum(1)
        ccl.addWidget(QtGui.QLabel('Number or Copies to Create:'))
        ccl.addWidget(self.copycount)
        ccl.addStretch()
        rv = QtGui.QVBoxLayout()
        rv.addWidget(self.memo)
        #lrv.addSpacing()
        rv.addLayout(ccl)
        rv.addStretch()

        buttonBox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel);

        self.connect(buttonBox, QtCore.SIGNAL('accepted()'), self.accept);
        self.connect(buttonBox, QtCore.SIGNAL('rejected()'), self.reject);

        rv.addWidget(buttonBox)
        hl.addWidget(self.classlist)
        hl.addLayout(rv)
        vl.addLayout(hl)

        self.populateClassList()
        self.selected = []

    def populateClassList(self):
        self.classlist.setColumnCount(1)
        self.classlist.setHeaderLabel('IDD Classes')
        olist = idfglobals.getObjectTree()
        klist = olist.keys()
        klist.sort()
        for k in klist :
            items = olist[k]
            items.sort()
            i = QtGui.QTreeWidgetItem([k])
            self.classlist.addTopLevelItem(i)
            for ei in items :
                j = QtGui.QTreeWidgetItem(i,[ei])
                self.classlist.addTopLevelItem(j)

    def accept(self):
        s = self.classlist.selectedItems()
        self.selected = []
        for i in s :
            for x in range(self.copycount.value()):
                self.selected.append(i.text(0))

        self.done(1)

    def reject(self):
        self.selected = []
        self.done(0)


class Hero():  # template atau class
    #class variable
    jumlah_hero = 0

    def __init__(self, dictionary):
        self.name = 'name'
        self.health = 0
        self.attackPower = 0
        self.armorNumber = 0
        Hero.jumlah_hero += 1
        for k, v in dictionary.items():
            if k in self.__dict__:
                setattr(self, k, v)

    # void function atau method
    def serang(self,lawan):
        print(self.name + ' menyerang ' + lawan.name)
        lawan.diserang(self)
    def diserang(self,lawan):
        print(self.name + ' diserang ' + lawan.name)
        attack_diterima = lawan.attackPower/self.armorNumber
        print('serangan terasa : ' + str(attack_diterima))
        self.health -= attack_diterima
        print(f'darah {self.name} tersisa {str(self.health)}')
    def siapa(self):
        print("namaku adalah " + self.name)

    def healthUp(self, up):
        self.health += up

    def getHealth(self):
        return self.health

heroes = [
    {
        "name": "mirana",
        "health": 100,
        "attackPower": 50,
        "armorNumber": 10
    },
    {
        "name": "sven",
        "health": 200,
        "attackPower": 50,
        "armorNumber": 10
    }
]

heroes.append({
    "name":"sniper",
    "health":100,
    "attackPower":50,
    "armorNumber":10
})

listHero = {}
for i, hero in enumerate(heroes):
    listHero[f"hero{i}"] = Hero(hero)

# akses class variable dari hero1
# print('akses class variable dari hero : ', Hero.jumlah_hero)
loop = 0
listHero['hero2'].serang(listHero['hero1'])
print("\n")
listHero['hero2'].serang(listHero['hero1'])
print("\n")
listHero['hero2'].serang(listHero['hero1'])
print("\n")
listHero['hero2'].serang(listHero['hero1'])
print("\n")
listHero['hero2'].serang(listHero['hero1'])
print("\n")

# for key, val in listHero.items():
#     loop += 1
#     if val.name is 'sniper':
#         val.serang()
#     val.healthUp(10)
#     print(val.getHealth())

value_at_index = listHero.values()
value_list = list(value_at_index)

# for val in value_list:
#     print(val)
#     print(f'ini valuenya  {val.name} dan {val.health}')


#    Copyright 2018 Rackspace, US Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from octavia.tests.functional.api.v2 import base

class TestProvider(base.BaseAPITest):

    root_tag_list = 'providers'

    def setUp(self):
        super(TestProvider, self).setUp()

    def test_get_all_providers(self):
        octavia_dict = {u'description': u'Octavia driver.',
                        u'name': u'octavia'}
        amphora_dict = {u'description': u'Amp driver.', u'name': u'amphora'}
        noop_dict = {u'description': u'NoOp driver.', u'name': u'noop_driver'}
        providers = self.get(self.PROVIDERS_PATH).json.get(self.root_tag_list)
        self.assertEqual(3, len(providers))
        self.assertTrue(octavia_dict in providers)
        self.assertTrue(amphora_dict in providers)
        self.assertTrue(noop_dict in providers)

    def test_get_all_providers_fields(self):
        octavia_dict = {u'name': u'octavia'}
        amphora_dict = {u'name': u'amphora'}
        noop_dict = {u'name': u'noop_driver'}
        providers = self.get(self.PROVIDERS_PATH, params={'fields': ['name']})
        providers_list = providers.json.get(self.root_tag_list)
        self.assertEqual(3, len(providers_list))
        self.assertTrue(octavia_dict in providers_list)
        self.assertTrue(amphora_dict in providers_list)
        self.assertTrue(noop_dict in providers_list)


import discord
from discord.ext import commands

client = commands.Bot(command_prefix = '.')

@client.event
async def on_ready():
    print('Hello World!')

####### METHODS #######

#Watch list
#Adds users to the specified group for mentioning later
@client.command(aliases=['bought'])
async def watch(ctx):
    role = discord.utils.get(ctx.guild.roles, name="Stalnks")
    user = ctx.message.author
    userMessage = ctx.message
    botMessage = user.mention + ', here\'s hoping for a nice surprise!'
    await user.add_roles(role)
    botMessageEvent = await ctx.send(botMessage)
    await userMessage.delete(delay=1)
    await botMessageEvent.delete(delay=5)

#Unwatch
#Removes users from the specified group
@client.command(aliases=['sold'])
async def unwatch(ctx):
    role = discord.utils.get(ctx.guild.roles, name="Stalnks")
    user = ctx.message.author
    userMessage = ctx.message
    botMessage = user.mention + ', pleasure doin\' business with ya!'
    await user.remove_roles(role)
    botMessageEvent = await ctx.send(botMessage)
    await userMessage.delete(delay=1)
    await botMessagEvent.delete(delay=5)

#buying
#Mentions the role with the price and the dodo code for travelling
#PARAMETERS:
    # bellAmount: Price at Nook's Cranny on the island
    # dodoCode: Dodo Code from the Airport for travelling

@client.command()
async def buying(ctx, bellAmount, dodoCode: str):
    authorUser = ctx.message.author
    mentionRole = discord.utils.get(ctx.guild.roles, name="Stalnks")
    openRole = discord.utils.get(ctx.guild.roles, name="Open Town")
    await authorUser.add_roles(openRole)
    if(bellAmount and dodoCode):
        await ctx.send(mentionRole.mention + ' ' + authorUser.mention + '\'s island is buying turnips for ' + str(bellAmount) + ' bells!' +
                    '\n Their Dodo Code is: ' + dodoCode)

#Stop selling
#Edits the previous message to say that the island is closed
@client.command(aliases=['stop'])
async def stop_selling(ctx):
    channel = ctx.message.channel
    authorUser = ctx.message.author
    openRole = discord.utils.get(ctx.guild.roles, name="Open Town")
    #I have a feeling once a channel has a lot of messages this will become unwieldy
    async for m_id in channel.history().filter(lambda m: authorUser in m.mentions).filter(lambda m: 'island is' in m.content).map(lambda m:m):
        messageToEdit = m_id
        await authorUser.remove_roles(openRole)
        await messageToEdit.edit(content=authorUser.mention + ' has closed their town.')
        await messageToEdit.delete(delay=3600)
    #I have a feeling once a channel has a lot of messages this will become unwieldy
    async for m_id in channel.history().filter(lambda m: m.author == authorUser).filter(lambda m: '.selling' in m.content).map(lambda m:m):
        botSummonMessage = m_id
        await botSummonMessage.delete(delay=3)
    #I have a feeling once a channel has a lot of messages this will become unwieldy
    async for m_id in channel.history().filter(lambda m: m.author == authorUser).filter(lambda m: '.buying' in m.content).map(lambda m:m):
        botSummonMessage = m_id
        await botSummonMessage.delete(delay=3)
    await ctx.message.delete(delay=3)

#Set Fruit role
#Set the role in the server based on your Fruit
#PARAMETERS:
    # Fruit: name of the fruit to get role
@client.command()
async def fruit(ctx, fruit: str):
    fruit = fruit.lower()
    authorUser = ctx.message.author
    peachRole = discord.utils.get(ctx.guild.roles, name="just peachy")
    orangeRole = discord.utils.get(ctx.guild.roles, name="orange you glad")
    appleRole = discord.utils.get(ctx.guild.roles, name="apple a day...")
    pearRole = discord.utils.get(ctx.guild.roles, name="thank you pear-y much")
    cherryRole = discord.utils.get(ctx.guild.roles,name="so cherry sweet")

    if(peachRole in authorUser.roles or orangeRole in authorUser.roles or appleRole in authorUser.roles or
            pearRole in authorUser.roles or cherryRole in authorUser.roles):
        await ctx.send("You already have a fruit!")

    else:
        if(fruit == 'cherry'):
            await authorUser.add_roles(cherryRole)
            await ctx.send("You have set your fruit to Cherry!")
        elif(fruit == 'orange'):
            await authorUser.add_roles(orangeRole)
            await ctx.send("You have set your fruit to Orange!")
        elif(fruit == 'peach'):
            await authorUser.add_roles(peachRole)
            await ctx.send("You have set your fruit to Peach!")
        elif(fruit == 'apple'):
            await authorUser.add_roles(appleRole)
            await ctx.send("You have set your fruit to Apple!")
        elif(fruit == 'pear'):
            await authorUser.add_roles(pearRole)
            await ctx.send("You have set your fruit to Pear!")

#Help
@client.command()
async def dmhelp(ctx):
    await ctx.send("Why hullo there! I'm Daisy Mae!")
    await ctx.send("Need some help?")

    await ctx.send("Prefix: '.'\nAdd Fruit: **.fruit** [*cherry*,*orange*,*peach*,*apple*,*pear*]\n" +
    "\nAdd to Watchlist: **.bought**/**.watch**\n\nRemove from watchlist: **.sold**/**.unwatch**\n"+
    "\nAnnounce buying & being open: **.buying**\n\nClose town (use after .buying): **.stop**")

client.run('')


"""
   Copyright 2020 Universitat Politècnica de Catalunya & AGH University of Science and Technology

                                        BSD 3-Clause License

   Redistribution and use in source and binary forms, with or without modification, are permitted
   provided that the following conditions are met:
    1. Redistributions of source code must retain the above copyright notice, this list of conditions
       and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, this list of
       conditions and the following disclaimer in the documentation and/or other materials provided
       with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
       or promote products derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
    ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
    TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
"""

from __future__ import print_function
import tensorflow as tf

class RouteNetModel(tf.keras.Model):
    """ Init method for the custom model.

    Args:
        config (dict): Python dictionary containing the diferent configurations
                       and hyperparameters.
        output_units (int): Output units for the last readout's layer.

    Attributes:
        config (dict): Python dictionary containing the diferent configurations
                       and hyperparameters.
        link_update (GRUCell): Link GRU Cell used in the Message Passing step.
        path_update (GRUCell): Path GRU Cell used in the Message Passing step.
        readout (Keras Model): Readout Neural Network. It expects as input the
                               path states and outputs the per-path delay.
    """

    def __init__(self, config, output_units=1):
        super(RouteNetModel, self).__init__()

        # Configuration dictionary. It contains the needed Hyperparameters for the model.
        # All the Hyperparameters can be found in the config.ini file
        self.config = config

        # GRU Cells used in the Message Passing step
        self.link_update = tf.keras.layers.GRUCell(int(self.config['HYPERPARAMETERS']['link_state_dim']))
        self.path_update = tf.keras.layers.GRUCell(int(self.config['HYPERPARAMETERS']['path_state_dim']))

        # Readout Neural Network. It expects as input the path states and outputs the per-path delay
        self.readout = tf.keras.Sequential([
            tf.keras.layers.Input(shape=int(self.config['HYPERPARAMETERS']['path_state_dim'])),
            tf.keras.layers.Dense(int(self.config['HYPERPARAMETERS']['readout_units']),
                                  activation=tf.nn.selu,
                                  kernel_regularizer=tf.keras.regularizers.l2(
                                      float(self.config['HYPERPARAMETERS']['l2'])),
                                  ),
            tf.keras.layers.Dense(int(self.config['HYPERPARAMETERS']['readout_units']),
                                  activation=tf.nn.relu,
                                  kernel_regularizer=tf.keras.regularizers.l2(
                                      float(self.config['HYPERPARAMETERS']['l2']))),
            tf.keras.layers.Dense(output_units,
                                  kernel_regularizer=tf.keras.regularizers.l2(
                                      float(self.config['HYPERPARAMETERS']['l2_2'])))
        ])

    def call(self, inputs, training=False):
        """This function is execution each time the model is called

        Args:
            inputs (dict): Features used to make the predictions.
            training (bool): Whether the model is training or not. If False, the
                             model does not update the weights.

        Returns:
            tensor: A tensor containing the per-path delay.
        """

        f_ = inputs

        links = f_['links']
        paths = f_['paths']
        seqs = f_['sequences']

        # Compute the shape for the  all-zero tensor for link_state
        shape = tf.stack([
            f_['n_links'],
            int(self.config['HYPERPARAMETERS']['link_state_dim']) - 1
        ], axis=0)

        # Initialize the initial hidden state for links
        link_state = tf.concat([
            tf.expand_dims(f_['link_capacity'], axis=1),
            tf.zeros(shape)
        ], axis=1)

        # Compute the shape for the  all-zero tensor for path_state
        shape = tf.stack([
            f_['n_paths'],
            int(self.config['HYPERPARAMETERS']['path_state_dim']) - 1
        ], axis=0)

        # Initialize the initial hidden state for paths
        path_state = tf.concat([
            tf.expand_dims(f_['bandwith'], axis=1),
            tf.zeros(shape)
        ], axis=1)

        # Iterate t times doing the message passing
        for _ in range(int(self.config['HYPERPARAMETERS']['t'])):

            # The following lines generate a tensor of dimensions [n_paths, max_len_path, dimension_link] with all 0
            # but the link hidden states
            h_tild = tf.gather(link_state, links)

            ids = tf.stack([paths, seqs], axis=1)
            max_len = tf.reduce_max(seqs) + 1
            shape = tf.stack([
                f_['n_paths'],
                max_len,
                int(self.config['HYPERPARAMETERS']['link_state_dim'])])

            lens = tf.math.segment_sum(data=tf.ones_like(paths),
                                       segment_ids=paths)

            # Generate the aforementioned tensor [n_paths, max_len_path, dimension_link]
            link_inputs = tf.scatter_nd(ids, h_tild, shape)

            # Define the RNN used for the message passing links to paths
            gru_rnn = tf.keras.layers.RNN(self.path_update,
                                          return_sequences=True,
                                          return_state=True)

            # First message passing: update the path_state
            outputs, path_state = gru_rnn(inputs=link_inputs,
                                          initial_state=path_state,
                                          mask=tf.sequence_mask(lens))

            # For every link, gather and sum the sequence of hidden states of the paths that contain it
            m = tf.gather_nd(outputs, ids)
            m = tf.math.unsorted_segment_sum(m, links, f_['n_links'])

            # Second message passing: update the link_state
            link_state, _ = self.link_update(m, [link_state])

        # Call the readout ANN and return its predictions
        r = self.readout(path_state, training=training)

        return r

def r_squared(labels, predictions):
    """Computes the R^2 score.

        Args:
            labels (tf.Tensor): True values
            labels (tf.Tensor): This is the second item returned from the input_fn passed to train, evaluate, and predict.
                                If mode is tf.estimator.ModeKeys.PREDICT, labels=None will be passed.

        Returns:
            tf.Tensor: Mean R^2
        """

    total_error = tf.reduce_sum(tf.square(labels - tf.reduce_mean(labels)))
    unexplained_error = tf.reduce_sum(tf.square(labels - predictions))
    r_sq = 1.0 - tf.truediv(unexplained_error, total_error)

    # Needed for tf2 compatibility.
    m_r_sq, update_rsq_op = tf.compat.v1.metrics.mean(r_sq)

    return m_r_sq, update_rsq_op

def model_fn(features, labels, mode, params):
    """model_fn used by the estimator, which, given inputs and a number of other parameters,
       returns the ops necessary to perform training, evaluation, or predictions.

    Args:
        features (dict): This is the first item returned from the input_fn passed to train, evaluate, and predict.
        labels (tf.Tensor): This is the second item returned from the input_fn passed to train, evaluate, and predict.
                            If mode is tf.estimator.ModeKeys.PREDICT, labels=None will be passed.
        mode (tf.estimator.ModeKeys): Specifies if this is training, evaluation or prediction.
        params (dict): Dict of hyperparameters. Will receive what is passed to Estimator in params parameter.

    Returns:
        tf.estimator.EstimatorSpec: Ops and objects returned from a model_fn and passed to an Estimator.
    """

    # Create the model.
    model = RouteNetModel(params)

    # Execute the call function and obtain the predictions.
    predictions = model(features, training=(mode == tf.estimator.ModeKeys.TRAIN))

    predictions = tf.squeeze(predictions)

    # If we are performing predictions.
    if mode == tf.estimator.ModeKeys.PREDICT:
        # Return the predicted values.
        return tf.estimator.EstimatorSpec(
            mode, predictions={
                'predictions': predictions
            })

    # Define the loss function.
    loss_function = tf.keras.losses.MeanSquaredError()

    # Obtain the regularization loss of the model.
    regularization_loss = sum(model.losses)

    # Compute the loss defined previously.
    loss = loss_function(labels, predictions)

    # Compute the total loss.
    total_loss = loss + regularization_loss

    tf.summary.scalar('loss', loss)
    tf.summary.scalar('regularization_loss', regularization_loss)
    tf.summary.scalar('total_loss', total_loss)

    # If we are performing evaluation.
    if mode == tf.estimator.ModeKeys.EVAL:
        # Define the different evaluation metrics
        label_mean = tf.keras.metrics.Mean()
        _ = label_mean.update_state(labels)
        prediction_mean = tf.keras.metrics.Mean()
        _ = prediction_mean.update_state(predictions)
        mae = tf.keras.metrics.MeanAbsoluteError()
        _ = mae.update_state(labels, predictions)
        mre = tf.keras.metrics.MeanRelativeError(normalizer=tf.abs(labels))
        _ = mre.update_state(labels, predictions)

        return tf.estimator.EstimatorSpec(
            mode, loss=loss,
            eval_metric_ops={
                'label/mean': label_mean,
                'prediction/mean': prediction_mean,
                'mae': mae,
                'mre': mre,
                'r-squared': r_squared(labels, predictions)
            }
        )

    # If we are performing training.
    assert mode == tf.estimator.ModeKeys.TRAIN

    # Compute the gradients.
    grads = tf.gradients(total_loss, model.trainable_variables)

    summaries = [tf.summary.histogram(var.op.name, var) for var in model.trainable_variables]
    summaries += [tf.summary.histogram(g.op.name, g) for g in grads if g is not None]

    # Define an exponential decay schedule.
    decayed_lr = tf.keras.optimizers.schedules.ExponentialDecay(float(params['HYPERPARAMETERS']['learning_rate']),
                                                                int(params['HYPERPARAMETERS']['decay_steps']),
                                                                float(params['HYPERPARAMETERS']['decay_rate']),
                                                                staircase=True)

    # Define an Adam optimizer using the defined exponential decay.
    optimizer = tf.keras.optimizers.Adam(learning_rate=decayed_lr)

    # Manually assign tf.compat.v1.global_step variable to optimizer.iterations
    # to make tf.compat.v1.train.global_step increased correctly.
    optimizer.iterations = tf.compat.v1.train.get_or_create_global_step()

    # Apply the processed gradients using the optimizer.
    train_op = optimizer.apply_gradients(zip(grads, model.trainable_variables))

    # Define the logging hook. It returns the loss, the regularization loss and the
    # total loss every 10 iterations.
    logging_hook = tf.estimator.LoggingTensorHook(
        {"Loss": loss,
         "Regularization loss": regularization_loss,
         "Total loss": total_loss}, every_n_iter=10)

    return tf.estimator.EstimatorSpec(mode,
                                      loss=loss,
                                      train_op=train_op,
                                      training_hooks=[logging_hook]
                                      )


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('train.csv')

X_train = dataset.iloc[:, 1:13].values
Y_train = dataset.iloc[:,[13]].values
df_y_train= pd.DataFrame(Y_train)
df_x_train  = pd.DataFrame(X_train)

### binarize Y train column
Y_train =np.where(Y_train == 'correct',1,0)

## handling missing values
from sklearn.preprocessing import Imputer
imputer_mode =Imputer(missing_values ='NaN', strategy ='most_frequent',axis =0)
imputer_mode.fit(X_train[:,[0]])
X_train[:,[0]]=imputer_mode.transform(X_train[:,[0]])

imputer = Imputer(missing_values ='NaN', strategy ='most_frequent',axis =0)
imputer.fit(X_train[:,[1,2,3,4,11]])
X_train[:,[1,2,3,4,11]] = imputer.transform(X_train[:,[1,2,3,4,11]])
df = pd.DataFrame(X_train)

xxc=pd.DataFrame(X_train)
xxc[5]=pd.to_datetime(xxc[5])
xxc[6]=pd.to_datetime(xxc[6])
xxc[5]=xxc[5].map(dt.datetime.toordinal)
xxc[6]=xxc[6].map(dt.datetime.toordinal)
X_train[:,5]=xxc[5]
X_train[:,6]=xxc[6]

##DO the same for Test set
test_dataset =pd.read_csv('test.csv')

X_test = test_dataset.iloc[:, 1:13].values
df_x_test  = pd.DataFrame(X_test)

xxc=pd.DataFrame(X_test)
xxc[5]=pd.to_datetime(xxc[5])
xxc[6]=pd.to_datetime(xxc[6])
xxc[5]=xxc[5].map(dt.datetime.toordinal)
xxc[6]=xxc[6].map(dt.datetime.toordinal)
X_test[:,5]=xxc[5]
X_test[:,6]=xxc[6]

Test_ids = test_dataset.iloc[:,0:1]

imputer_mode =Imputer(missing_values ='NaN', strategy ='most_frequent',axis =0)
imputer_mode.fit(X_test[:,[0]])
X_test[:,[0]]=imputer_mode.transform(X_test[:,[0]])

imputer = Imputer(missing_values ='NaN', strategy ='most_frequent',axis =0)
imputer.fit(X_test[:,[1,2,3,4,11]])
X_test[:,[1,2,3,4,11]] = imputer.transform(X_test[:,[1,2,3,4,11]])

## Now back to work................

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train[:,[0,1,2,3,4,5,6,7,8,9,10,11]])
X_test = scaler.fit_transform(X_test[:,[0,1,2,3,4,5,6,7,8,9,10,11]])

from sklearn.linear_model import LogisticRegression

classifier = LogisticRegression(random_state=0)
classifier.fit(X_train,Y_train)

#Import test values

y_preds = classifier.predict(X_test)
Y_pred_dataframe = pd.DataFrame(y_preds)
Y_test_ids_dataframe = pd.DataFrame(Test_ids)

submission =pd.concat([Y_test_ids_dataframe,Y_pred_dataframe],axis=1)
submission =submission.rename(columns={"tiripid":"tripid"})
submission =submission.rename(columns={0:"prediction"})

submission.to_csv('submission.csv',index=False)


# TestGame.py
#
# Copyright (C) 2016 Abhijit Patel Here
#
# This program is free software; you can redistribute it
# and/or modify it under the terms of the GNU General
# Public License as published by the Free Software
# Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even
# the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General
# Public License along with this program; if not, write
# to the Free Software Foundation, Inc., 51 Franklin
# St, Fifth Floor, Boston, MA 02110-1301  USA
#wormgame
import random, pygame, sys
from pygame.locals import *
from gi.repository import Gtk
FPS = 15
WINDOWWIDTH = 1000
WINDOWHEIGHT = 680
CELLSIZE = 20
assert WINDOWWIDTH % CELLSIZE == 0, "Window width must be a multiple of cell size."
assert WINDOWHEIGHT % CELLSIZE == 0, "Window height must be a multiple of cell size."
CELLWIDTH = int(WINDOWWIDTH / CELLSIZE)
CELLHEIGHT = int(WINDOWHEIGHT / CELLSIZE)

WHITE     = (255, 255, 255)
BLACK     = (  0,   0,   0)
RED       = (255,   0,   0)
GREEN     = (  0, 255,   0)
DARKGREEN = (  0, 155,   0)
DARKGRAY  = ( 40,  40,  40)
BGCOLOR = BLACK
BROWN     = (200, 128, 0)
BLUE      = (0, 0, 255)

UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'
FPSCLOCK= None
DISPLAYSURF=None
BASICFONT=None
HEAD = 0
HEAD = 0

def main():

    pygame.init()

    game = wormgame()

    game.run()

class wormgame:
    def __init__(self):
        pass
    def run(self):
        global FPSCLOCK, DISPLAYSURF, BASICFONT, WINDOWWIDTH, WINDOWHEIGHT
        DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
        BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
        pygame.display.set_caption('Wormy')
        FPSCLOCK = pygame.time.Clock()
        showStartScreen()
        startx = random.randint(2, CELLWIDTH - 6)
        starty = random.randint(1, 4)
        wormCoords = [{'x': startx,     'y': starty},
                      {'x': startx - 1, 'y': starty},
                      {'x': startx - 2, 'y': starty}]
        direction = RIGHT
        #showStartScreen()
        # Start the apple in a random place.
        apple = getRandomLocation()

        while True: # main game loop
            while Gtk.events_pending():
                Gtk.main_iteration()
            for event in pygame.event.get(): # event handling loop
                if event.type == QUIT:
                    terminate()
                elif event.type == pygame.KEYDOWN:

                    if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT:
                        direction = LEFT
                    elif (event.key == K_RIGHT or event.key == K_d) and direction != LEFT:
                        direction = RIGHT
                    elif (event.key == K_UP or event.key == K_w) and direction != DOWN:
                        direction = UP
                    elif (event.key == K_DOWN or event.key == K_s) and direction != UP:
                        direction = DOWN
                    elif event.key == K_ESCAPE:
                        terminate()

            if wormCoords[HEAD]['x'] in range(12,32) and wormCoords[HEAD]['y'] == 22:
                showGameOverScreen() # game over
                is_true = True
                while is_true:
                     while Gtk.events_pending():
                         Gtk.main_iteration()

                     for event in pygame.event.get():

                        if event.type == pygame.KEYDOWN:

                           startx = random.randint(5, CELLWIDTH - 10)
                           starty = random.randint(29, CELLHEIGHT - 2)
                           wormCoords = [{'x': startx,     'y': starty},
                                         {'x': startx - 1, 'y': starty},
                                         {'x': startx - 2, 'y': starty}]
                           direction = RIGHT
                           apple = getRandomLocation()
                           is_true = False

            elif wormCoords[HEAD]['x'] in range(18,38) and wormCoords[HEAD]['y'] == 12:
                showGameOverScreen() # game over
                is_true = True
                while is_true:
                     while Gtk.events_pending():
                         Gtk.main_iteration()

                     for event in pygame.event.get():

                        if event.type == pygame.KEYDOWN:

                           startx = random.randint(5, CELLWIDTH - 10)
                           starty = random.randint(29, CELLHEIGHT - 3)
                           wormCoords = [{'x': startx,     'y': starty},
                                         {'x': startx - 1, 'y': starty},
                                         {'x': startx - 2, 'y': starty}]
                           direction = RIGHT
                           apple = getRandomLocation()
                           is_true = False
            elif wormCoords[HEAD]['x'] in range(12,44) and wormCoords[HEAD]['y'] == 6:
                showGameOverScreen() # game over
                is_true = True
                while is_true:
                     while Gtk.events_pending():
                         Gtk.main_iteration()

                     for event in pygame.event.get():

                        if event.type == pygame.KEYDOWN:

                           startx = random.randint(5, CELLWIDTH - 5)
                           starty = random.randint(29, CELLHEIGHT - 2)
                           wormCoords = [{'x': startx,     'y': starty},
                                         {'x': startx - 1, 'y': starty},
                                         {'x': startx - 2, 'y': starty}]
                           direction = RIGHT
                           apple = getRandomLocation()
                           is_true = False
            elif wormCoords[HEAD]['x'] in range(6,44) and wormCoords[HEAD]['y'] == 28:
                showGameOverScreen() # game over
                is_true = True
                while is_true:
                     while Gtk.events_pending():
                         Gtk.main_iteration()

                     for event in pygame.event.get():

                        if event.type == pygame.KEYDOWN:

                           startx = random.randint(5, CELLWIDTH - 10)
                           starty = random.randint(29, CELLHEIGHT - 3)
                           wormCoords = [{'x': startx,     'y': starty},
                                         {'x': startx - 1, 'y': starty},
                                         {'x': startx - 2, 'y': starty}]
                           direction = RIGHT
                           apple = getRandomLocation()
                           is_true = False

            elif wormCoords[HEAD]['x'] == 6 and wormCoords[HEAD]['y'] in range(6,28):
                showGameOverScreen() # game over
                is_true = True
                while is_true:
                     while Gtk.events_pending():
                          Gtk.main_iteration()
                     for event in pygame.event.get():

                        if event.type == pygame.KEYDOWN:

                            startx = random.randint(5, CELLWIDTH - 10)
                            starty = random.randint(29, CELLHEIGHT - 3)
                            wormCoords = [{'x': startx,     'y': starty},
                                          {'x': startx - 1, 'y': starty},
                                          {'x': startx - 2, 'y': starty}]
                            direction = RIGHT
                            apple = getRandomLocation()
                            is_true = False
            elif wormCoords[HEAD]['x'] == 12 and wormCoords[HEAD]['y'] in range(12,22):
                showGameOverScreen() # game over
                is_true = True
                while is_true:
                     while Gtk.events_pending():
                          Gtk.main_iteration()
                     for event in pygame.event.get():

                        if event.type == pygame.KEYDOWN:

                           startx = random.randint(3, CELLWIDTH - 5)
                           starty = random.randint(1, 5)
                           wormCoords = [{'x': startx,     'y': starty},
                                         {'x': startx - 1, 'y': starty},
                                         {'x': startx - 2, 'y': starty}]
                           direction = RIGHT
                           apple = getRandomLocation()
                           is_true = False
            elif wormCoords[HEAD]['x'] == 38 and wormCoords[HEAD]['y'] in range(12,22):
                showGameOverScreen() # game over

                is_true = True
                while is_true:
                     while Gtk.events_pending():
                          Gtk.main_iteration()
                     for event in pygame.event.get():

                        if event.type == pygame.KEYDOWN:

                           startx = random.randint(2, 7)
                           starty = random.randint(1, 5)
                           wormCoords = [{'x': startx,     'y': starty},
                                         {'x': startx - 1, 'y': starty},
                                         {'x': startx - 2, 'y': starty}]
                           direction = RIGHT
                           apple = getRandomLocation()
                           is_true = False

            elif wormCoords[HEAD]['x'] == 44 and wormCoords[HEAD]['y'] in range(6,22):
                showGameOverScreen() # game over

                is_true = True
                while is_true:
                     while Gtk.events_pending():
                          Gtk.main_iteration()
                     for event in pygame.event.get():

                        if event.type == pygame.KEYDOWN:

                           startx = random.randint(3, CELLWIDTH - 5)
                           starty = random.randint(29, CELLHEIGHT - 2)
                           wormCoords = [{'x': startx,     'y': starty},
                                         {'x': startx - 1, 'y': starty},
                                         {'x': startx - 2, 'y': starty}]
                           direction = RIGHT
                           apple = getRandomLocation()
                           is_true = False
            for wormBody in wormCoords[1:]:
                if wormBody['x'] == wormCoords[HEAD]['x'] and wormBody['y'] == wormCoords[HEAD]['y']:
                    showGameOverScreen() # game over
                    is_true = True
                    while is_true:
                     while Gtk.events_pending():
                        Gtk.main_iteration()
                     for event in pygame.event.get():

                        if event.type == pygame.KEYDOWN:

                          startx = random.randint(5, CELLWIDTH - 10)
                          starty = random.randint(29, CELLHEIGHT - 2)
                          wormCoords = [{'x': startx,     'y': starty},
                                        {'x': startx - 1, 'y': starty},
                                        {'x': startx - 2, 'y': starty}]
                          direction = RIGHT
                          apple = getRandomLocation()
                          is_true = False
            # check if worm has eaten an apply
            if wormCoords[HEAD]['x'] == apple['x'] and wormCoords[HEAD]['y'] == apple['y']:
                # don't remove worm's tail segment
                apple = getRandomLocation() # set a new apple somewhere
            else:
                del wormCoords[-1] # remove worm's tail segment

            # move the worm by adding a segment in the direction it is moving
            if wormCoords[HEAD]['x'] == CELLWIDTH:
                   wormCoords[HEAD]['x'] = 0

            if wormCoords[HEAD]['x'] == -1:
                   wormCoords[HEAD]['x'] = CELLWIDTH

            if wormCoords[HEAD]['y'] == CELLHEIGHT:
                   wormCoords[HEAD]['y'] = 0

            if wormCoords[HEAD]['y'] == -1:
                   wormCoords[HEAD]['y'] = CELLHEIGHT

            if direction == UP:
                newHead = {'x': wormCoords[HEAD]['x'], 'y': wormCoords[HEAD]['y'] - 1}
            elif direction == DOWN:
                newHead = {'x': wormCoords[HEAD]['x'], 'y': wormCoords[HEAD]['y'] + 1}
            elif direction == LEFT:
                newHead = {'x': wormCoords[HEAD]['x'] - 1, 'y': wormCoords[HEAD]['y']}
            elif direction == RIGHT:
                newHead = {'x': wormCoords[HEAD]['x'] + 1, 'y': wormCoords[HEAD]['y']}
            wormCoords.insert(0, newHead)
            DISPLAYSURF.fill(WHITE)

            drawWorm(wormCoords)
            drawApple(apple)
            drawwalls()
            drawScore(len(wormCoords) - 3)
            pygame.display.update()
            FPSCLOCK.tick(FPS)

def drawPressKeyMsg():
     pressKeySurf = BASICFONT.render('Press a key to play.', True, BLACK)
     pressKeyRect = pressKeySurf.get_rect()
     pressKeyRect.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 30)
     DISPLAYSURF.blit(pressKeySurf, pressKeyRect)

def checkForKeyPress():

     for event1 in pygame.event.get():

       if event1.type == pygame.KEYUP:

         return True
def showGameOverScreen():
     gameOverFont = pygame.font.Font('freesansbold.ttf', 150)
     gameSurf = gameOverFont.render('Game', True, BLACK)
     overSurf = gameOverFont.render('Over', True, BLACK)
     gameRect = gameSurf.get_rect()
     overRect = overSurf.get_rect()
     gameRect.midtop = (WINDOWWIDTH / 2, 10)
     overRect.midtop = (WINDOWWIDTH / 2, gameRect.height + 10 + 25)

     DISPLAYSURF.blit(gameSurf, gameRect)
     DISPLAYSURF.blit(overSurf, overRect)
     drawPressKeyMsg()
     pygame.display.update()
     pygame.time.wait(500)
     return

def showStartScreen():
     snakefont = pygame.font.Font('freesansbold.ttf', 100)
     snakesurf1 = snakefont.render('Wormy', True, BLUE)
     snakesurf2 = snakefont.render('Game!', True, BLUE)
     movx = WINDOWWIDTH / 2
     movy = 50
     DISPLAYSURF.fill(WHITE)
     snakerect1 = snakesurf1.get_rect()
     snakerect1.topright = (movx,movy)
     DISPLAYSURF.blit(snakesurf1,snakerect1)
     snakerect2 = snakesurf2.get_rect()
     snakerect2.topright = (movx,movy+100)
     DISPLAYSURF.blit(snakesurf2,snakerect2)

     drawPressKeyMsg()
     movx += 10
     movy = 50

     pygame.display.update()
     FPSCLOCK.tick(10)
     si_true = True
     while si_true:
          while Gtk.events_pending():
                Gtk.main_iteration()
          for event in pygame.event.get():

                if event.type == pygame.KEYUP:
                    si_true = False
                    return

def terminate():
     pygame.quit()
     sys.exit()

def getRandomLocation():
    p = random.randint(0, CELLWIDTH - 1)
    q = random.randint(0, CELLHEIGHT - 1)
    if p in range(12,32) and q == 22:
       p = 5
       q = random.randint(0, CELLHEIGHT - 1)
    if p in range(18,38) and q == 12:
       p = 2
       q = random.randint(0, CELLHEIGHT - 1)
    if p in range(12,44) and q == 6:
       p = 25
       q = random.randint(13, 21)
    if p in range(6,44) and q == 28:
       p = 27
       q = random.randint(13, 19)
    elif p == 6 and q in range(6,28):
       p = 46
       q = random.randint(0, CELLHEIGHT - 1)
    elif p == 12 and q in range(12,22):
       p = 47
       q = random.randint(0, CELLHEIGHT - 1)
    elif p == 38 and q in range(12,22):
       p = 48
       q = random.randint(0, CELLHEIGHT - 1)
    elif p == 44 and q in range(6,22):
       p = 22
       q = random.randint(14, 20)
    else:
        p=p
        q=q
    return {'x': p, 'y': q}

def drawScore(score):
    scoreSurf = BASICFONT.render('Score: %s' % (score), True, BLACK)
    scoreRect = scoreSurf.get_rect()
    scoreRect.topleft = (WINDOWWIDTH - 120, 10)
    DISPLAYSURF.blit(scoreSurf, scoreRect)

def drawwalls():
    wallsrect = pygame.Rect(120, 120, CELLSIZE, 440)
    pygame.draw.rect(DISPLAYSURF, BROWN, wallsrect)

    wallsrecta = pygame.Rect(240, 240, CELLSIZE, 200)
    pygame.draw.rect(DISPLAYSURF, BROWN, wallsrecta)

    wallsrectb = pygame.Rect(120, 560, 760, CELLSIZE)
    pygame.draw.rect(DISPLAYSURF, BROWN, wallsrectb)

    wallsrectc = pygame.Rect(240, 440, 400, CELLSIZE)
    pygame.draw.rect(DISPLAYSURF, BROWN, wallsrectc)

    wallsrectc = pygame.Rect(240, 120, 640, CELLSIZE)
    pygame.draw.rect(DISPLAYSURF, BROWN, wallsrectc)

    wallsrectc = pygame.Rect(880, 120, CELLSIZE, 320)
    pygame.draw.rect(DISPLAYSURF, BROWN, wallsrectc)

    wallsrectc = pygame.Rect(360, 240, 400, CELLSIZE)
    pygame.draw.rect(DISPLAYSURF, BROWN, wallsrectc)

    wallsrectc = pygame.Rect(760, 240, CELLSIZE, 200)
    pygame.draw.rect(DISPLAYSURF, BROWN, wallsrectc)

def drawWorm(wormCoords):
    for coord in wormCoords:
        x = coord['x'] * CELLSIZE
        y = coord['y'] * CELLSIZE
        wormSegmentRect = pygame.Rect(x, y, CELLSIZE, CELLSIZE)
        pygame.draw.rect(DISPLAYSURF, GREEN, wormSegmentRect)
        wormInnerSegmentRect = pygame.Rect(x + 3, y + 3, CELLSIZE -6 , CELLSIZE - 6)
        pygame.draw.rect(DISPLAYSURF, BLUE, wormInnerSegmentRect)

def drawApple(apple):
    x = apple['x'] * CELLSIZE
    y = apple['y'] * CELLSIZE
    appleRect = pygame.Rect(x, y, CELLSIZE, CELLSIZE)
    pygame.draw.rect(DISPLAYSURF, RED, appleRect)

if __name__ == '__main__':
    main()