import sys
import datetime
from project import app
from common_utilities import Constant
from project.users.models import Counter, Users
from flask_login import login_required, logout_user, current_user
from flask import Blueprint, render_template, session, make_response, jsonify, request, redirect, url_for

sys.path.append('../../')
from project.users.memcache_ctrl import client, CONSTANT

users_blueprint = Blueprint('users', __name__, template_folder='templates')

@users_blueprint.route('/request_accepted_counter', methods=['GET', 'POST'])
@login_required
def request_accepted_counter():
    if request.method == "POST":
        instagram_username = session.get("username").get("username")
        counter_id = session.get("current_counter_id")
        if counter_id is None:
            counter_id_obj = Counter().get_last_counter_info(instagram_username)
            if counter_id_obj is not None:
                counter_id = counter_id_obj.id
                session["current_counter_id"] = counter_id_obj.id
                session.modified = True

        total_success_request, total_request, failed_request = 0, 0, 0
        is_complete = False
        last_date_time = None
        try:
            dict_index_name = instagram_username.lower() + Constant.CONSTANT().ALL_INFO
            all_info = client.get(dict_index_name)
            if isinstance(all_info, dict):
                total_request = all_info[Constant.CONSTANT().TOTAL_REQUEST_TO_BE_ACCEPT]
                total_success_request = all_info[Constant.CONSTANT().SUCCESSFUL_ACCEPTED]
                failed_request = all_info[Constant.CONSTANT().REQUEST_FAILED]
                is_complete = all_info[Constant.CONSTANT().IS_REQUEST_COMPLETE]
                if is_complete == True:
                    last_date_time = all_info.get("update_date")
            else:
                counter_stats_row = Counter.get_one_counter(counter_id)
                if counter_stats_row is not None:
                    total_success_request = counter_stats_row.total_accepted_request
                    total_request = counter_stats_row.input_request_count
                    failed_request = counter_stats_row.input_request_count
                    is_complete = counter_stats_row.is_request_complete
                    last_date_time = counter_stats_row.update_date

        except Exception as error:
            print("Not able to get count ", error)

        response_dict = {"successful": total_success_request,
                         "username": instagram_username.upper(),
                         "failed": failed_request,
                         "isComplete": is_complete,
                         "total": total_request,
                         "lastDateTime": last_date_time.strftime("%a %B %d %Y %I:%M:%S %p") if last_date_time is not None else None}
        return make_response(jsonify(response_dict), 200)

@users_blueprint.route('/accept_pending_requests', methods=["GET", "POST"])
@login_required
def accept():
    if request.method == "POST":
        instagram_username = session["username"]["username"]
        bot_obj = client.get(instagram_username)["bot_obj"]
        no_to_accept = request.form.get("customUserInputNumber", 0)

        init_dict_items = {
            Constant.CONSTANT().TOTAL_REQUEST_TO_BE_ACCEPT: no_to_accept,
            Constant.CONSTANT().IS_REQUEST_COMPLETE: False,
            Constant.CONSTANT().SUCCESSFUL_ACCEPTED: 0,
            Constant.CONSTANT().REQUEST_FAILED: 0,
        }

        dict_get_index = instagram_username.lower() + Constant.CONSTANT().ALL_INFO

        client.set(dict_get_index, init_dict_items)
        new_user_count_req = Counter(
            insta_username=instagram_username,
            input_request_count=no_to_accept,
            total_accepted_request=0,
            total_failed_request=0
        )
        new_user_count_req.save()

        counter_id = new_user_count_req.id
        session["current_counter_id"] = counter_id
        session.modified = True

        ctr_item = Counter.get_one_counter(session["current_counter_id"])
        resp = bot_obj.approve_pending_follow_requests(number_of_requests=int(no_to_accept), ctr_item=ctr_item, init_dict_items=init_dict_items, dict_get_index=dict_get_index, counter_ctr=0)

        if resp == "No request to accept":
            return "No request to accept"

        if resp == None:
            return "True"
        return "True"

    elif request.method == "GET":
        instagram_username = session.get("username").get("username")
        user_obj = Users.query.filter_by(insta_username=instagram_username).first()
        last_day = str(days_between(user_obj.till_date)) + " days"
        return render_template("AcceptRequests.html", last_day=last_day)

@users_blueprint.route('/logout', methods=["GET", "POST"])
@login_required
def logout():
    try:
        instagram_username = current_user.insta_username
        if current_user.is_authenticated():
            client.delete(instagram_username)
            session.clear()
        logout_user()
        client.delete(instagram_username)
        session.clear()
    except:
        pass
    return redirect(url_for('core.index'))

def days_between(d1):
    d1 = datetime.datetime.strptime(str(d1.date()), "%Y-%m-%d")
    d2 = datetime.datetime.strptime(str(datetime.datetime.utcnow().date()), "%Y-%m-%d")
    return abs((d2 - d1).days)

default_args = {}
footer_var = {"cp_year": datetime.datetime.now().year}

@app.before_first_request
def load_default():
    default_args["footer_content"] = render_template("footer.html", **footer_var)
    return default_args


""" Run Length Decoding """
def main():
    """ print decoded password """
    text = input()
    result = ""
    for i in range(len(text)):
        if ord(text[i]) > 65:
            result += text[i]
            temp = len(result) - 1
            print(int(result[:temp])*result[-1], end="")
            result = ""
        else:
            result += text[i]
main()


import os,sys

sys.path.append("..")

from tools.iPrint import *
from misc.Color import Colors

class Shell:
    def __init__(self) -> None:
        self.runShell()

    def runShell(self):
        while True:
            try:
                command = input(Colors.RED + "IShell ~$shell> " + Colors.END)
                if command in ["q", "quit", "exit", "exit()"]:
                    return 0
                else:
                    os.system(command)
            except KeyboardInterrupt:
                return 0
            except Exception as e:
                print(Colors.RED + "[!]" + Colors.END + "Error: " + e)

Shell()


import requests
from bs4 import BeautifulSoup
# 製作一個串列裝所有可輸入的星座代號
# astros = ["牡羊座","金牛座","雙子座","巨蟹座","獅子座","處女座","天秤座","天蠍座","射手座","摩羯座","水瓶座","雙魚座"]

astrosDict = {"牡羊座":"0","金牛座":"1","雙子座":"2","巨蟹座":"3","獅子座":"4","處女座":"5","天秤座":"6","天蠍座":"7","射手座":"8","摩羯座":"9","水瓶座":"10","雙魚座":"11"}
# 組合字串，並印出所有可選星座
# text = ''
# for index,value in enumerate(astros):
#     text += value+":"+str(index)+" "
# print(text)

# 儲存使用者輸入之星座
astrosInput = input("請輸入欲查詢的星座名稱:")

# 將使用者輸入之數字結合網址，並送出請求取得網頁
# 透過觀察得知
# 網址的daily_0 iAstro=0 數字與星座相關
# 讓使用者輸入星座
url = "https://astro.click108.com.tw/daily_"+astrosDict[astrosInput]+".php?iAstro="+astrosDict[astrosInput]
r = requests.get(url)
# 將回傳之網頁HTML轉換為可操作格式
soup = BeautifulSoup(r.text, 'html.parser')

# 利用選擇器，選取到今日整體運勢
data = soup.select(".TODAY_CONTENT > p")

# 印出今日星座運勢
print("\n"+astrosInput+"今日的整體運勢為:\n")
print(data[0].text)
print(data[1].text)

# astros[0]="牡羊座";
# astros[1]="金牛座";
# astros[2]="雙子座";
# astros[3]="巨蟹座";
# astros[4]="獅子座";
# astros[5]="處女座";
# astros[6]="天秤座";
# astros[7]="天蠍座";
# astros[8]="射手座";
# astros[9]="摩羯座";
# astros[10]="水瓶座";
# astros[11]="雙魚座";


import boto.exception
from awscompat import config, util
from awscompat.connections import ec2_conn
from awscompat.tests.base import TestNode

class TestDescribeImages(TestNode):
    """Failing test for https://bugs.launchpad.net/nova/+bug/755829"""

    def pre(self):
        assert ec2_conn.get_all_images(
            [config['ec2']['test_image_id']]
        )[0].name

class TestSecurityGroups(TestNode):
    """Test security group generation."""

    def pre(self):
        self.group_name = self.make_uuid('group_name')
        self.group_desc = self.make_uuid('group_desc')

        self.group = ec2_conn.create_security_group(
            self.group_name,
            self.group_desc
        )

        groups = util.retry(
            lambda: ec2_conn.get_all_security_groups(
                groupnames=[self.group_name])
        )

        assert len(groups)

    def post(self):
        self.group.delete()

        # TODO: this might not raise because of delay.
        # so I can't use the retry controller
        # I should write a general purpose request wrapper
        # which polls until it gets a different response.
        @self.assert_raises(boto.exception.EC2ResponseError)
        def test_throw():
            ec2_conn.get_all_security_groups(groupnames=[self.group_name])

class TestKeyPairs(TestNode):
    """Test keypair generation."""

    def pre(self):
        self.key_name = self.make_uuid('key_name')
        self.keypair = ec2_conn.create_key_pair(self.key_name)

        assert len(ec2_conn.get_all_key_pairs(keynames=[self.key_name]))

    def post(self):
        ec2_conn.delete_key_pair(self.key_name)

        @self.assert_raises(boto.exception.EC2ResponseError)
        def test_boto_throw():
            ec2_conn.get_all_key_pairs(keynames=[self.key_name])

class TestInstance(TestNode):
    """Test EC2 image launch and termination."""

    depends = {
        'key_pairs': TestKeyPairs,
        'security_group': TestSecurityGroups
    }

    def pre(self, key_pairs=None, security_group=None):
        image_id = config['ec2']['test_image_id']
        instance_config = config['ec2']['test_instance']

        self.image = ec2_conn.get_all_images(image_ids=[image_id])[0]
        self.security_group = security_group
        self.key_pairs = key_pairs

        self.security_group.group.authorize('tcp', 22, 22, '0.0.0.0/0')
        self.reservation = self.image.run(
            security_groups=[self.security_group.group_name],
            key_name=self.key_pairs.key_name,
            **instance_config
        )

        util.wait(
            lambda: self.reservation.instances[0].update() == 'running',
            timeout=60 * 3
        )

        util.wait(
            lambda: self.testTelnet(
                self.reservation.instances[0].public_dns_name,
                22
            ),
            timeout = 60 * 5
        )

        assert util.retry(
            lambda: self.testSSH(
                self.key_pairs.keypair.material.encode('ascii'),
                config['ec2']['test_username'],
                self.reservation.instances[0].public_dns_name
            ),
            wait_exp=2
        )

    def post(self):

        self.reservation.instances[0].terminate()
        #ec2_conn.terminate_instances([self.reservation.instances[0].id])
        util.wait(
            lambda: self.reservation.instances[0].update() == 'terminated',
            timeout=60 * 2
        )

        assert util.retry(
            lambda: not self.testSSH(
                self.key_pairs.keypair.material.encode('ascii'),
                config['ec2']['test_username'],
                self.reservation.instances[0].public_dns_name
            ),
            wait_exp=2
        )


def lying_down(R):
    tot = 0
    for row in R:
        cnt = 0
        for j in range(N):
            if row[j] == '.':
                cnt += 1
                if j == N-1 and cnt >= 2:
                    tot += 1
            else:
                if cnt >= 2:
                    tot += 1
                cnt = 0

    return tot

N = int(input())
room = [list(input()) for _ in range(N)]
room90 = list(map(list, zip(*reversed(room))))

vertical = lying_down(room90)
horizontal = lying_down(room)

print(horizontal, vertical)


from flask import render_template,request,redirect,url_for
from .import main
from ..request import get_sources,get_articles,search_news

from ..models import Source

@main.route("/")
def index():
    """
    View root function that returns the index page and its data
    """
    popular_news = get_sources("popular")
    title = "Welcome to the best news outlet"

    search_news = request.args.get("article_query")

    if search_news:
        return redirect(url_for('main.search',news_feed = search_news))

    else:

        return render_template("index.html",title = title,popular = popular_news)

@main.route("/sources/<sources_id>")
def sources(sources_id):
    '''
    View news page function that returns the movie details page and its data
    '''

    news_source = get_articles(sources_id)
    title = f"{sources_id}"
    return render_template("news.html",id = sources_id,title = title,news = news_source)

@main.route("/search/<news_feed>")
def search(news_feed):
    '''
    View function to display the search results
    '''

    news_name_list = news_feed.split(" ")
    news_name_format = "+".join(news_name_list)

    searched_news = search_news(news_name_format)
    title = "News results"
    #search_news = request.args.get('article_query')

    return render_template("search.html",article = searched_news)


# quizz, homework, test college calculator
# 12/09/20
# luis Velasquez
students = {

    "frank": {"name": "frank",
              "homework": [90.0, 97.0, 75.0, 92.0],
              "quizzes": [88.0, 40.0, 94.0],
              "tests": [75.0, 90.0]},
    "alice": {"name": "alice",
              "homework": [100.0, 92.0, 98.0, 100.0],
              "quizzes": [82.0, 83.0, 91.0],
              "tests": [89.0, 97.0]},
    "sarah": {"name": "sarah",
              "homework": [0.0, 87.0, 75.0, 22.0],
              "quizzes": [0.0, 75.0, 78.0],
              "tests": [100.0, 100.0]},
}

for d in students:
    print(students[d]["name"])
    sumofhw = sum(students[d]["homework"])
    hwavg = sumofhw / len(students[d]["homework"])

    quizzes = sum(students[d]["quizzes"])
    quizavg = quizzes / len(students[d]["quizzes"])

    tests = sum(students[d]["tests"])
    testavg = tests / len(students[d]["tests"])

    hwfinal = 0.10 * hwavg + 0.30 * quizavg + 0.60 * testavg

    if hwfinal >= 90:
        print("A", )
    elif hwfinal >= 80:
        print("B")
    elif hwfinal >= 70:
        print("C")
    elif hwfinal >= 60:
        print("D")
    else:
        print("F")


import sys

n = int(input())
l = []

for i in range(n):
  l.append(int(input()))

lsf = sys.maxsize
pist = 0
maxprofit = 0

for i in l:
  if(i < lsf):
    lsf = i

  pist = i - lsf
  if(pist > maxprofit):
    maxprofit = pist

print(maxprofit)


"""
The basic framework of the Iterative Closest Points Matching is provided by Albert-Ludwigs-Universität Freiburg,
the course Introduction to Mobile Robotics (engl.) - Autonomous Mobile Systems
Lecturer: Prof. Dr. Wolfram Burgard, Dr. Michael Tangermann, Dr. Daniel Büscher, Lukas Luft
Co-organizers: Marina Kollmitz, Iman Nematollahi
"""
import numpy as np
import math
import matplotlib.pyplot as plt
import time

def plot_icp(X, P, P0, i, rmse):
  plt.cla()
  plt.scatter(X[0,:], X[1,:], c='k', marker='o', s=50, lw=0)
  plt.scatter(P[0,:], P[1,:], c='r', marker='o', s=50, lw=0)
  plt.scatter(P0[0,:], P0[1,:], c='b', marker='o', s=50, lw=0)
  plt.legend(('X', 'P', 'P0'), loc='lower left')
  plt.plot(np.vstack((X[0,:], P[0,:])), np.vstack((X[1,:], P[1,:])) ,c='k')
  plt.title("Iteration: " + str(i) + "  RMSE: " + str(rmse))
  plt.axis([-10, 15, -10, 15])
  plt.gca().set_aspect('equal', adjustable='box')
  plt.draw()
  plt.pause(0.5)
  return

def generate_data():

  # create reference data
  X = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,-1,-2,-3,-4,-5]])

  # add noise
  P = X + 0.05 * np.random.normal(0, 1, X.shape)

  # translate
  P[0,:] = P[0,:] + 1
  P[1,:] = P[1,:] + 1

  # rotate
  theta1 = ( 10.0 / 360) * 2 * np.pi
  theta2 = (110.0 / 360) * 2 * np.pi
  rot1 = np.array([[math.cos(theta1), -math.sin(theta1)],
                   [math.sin(theta1),  math.cos(theta1)]])
  rot2 = np.array([[math.cos(theta2), -math.sin(theta2)],
                   [math.sin(theta2),  math.cos(theta2)]])

  # sets with known correspondences
  P1 = np.dot(rot1, P)
  P2 = np.dot(rot2, P)

  # sets with unknown correspondences
  P3 = np.random.permutation(P1.T).T
  P4 = np.random.permutation(P2.T).T

  return X, P1, P2, P3, P4

def closest_point_matching(X, P):
  """
  Performs closest point matching of two point sets.

  Arguments:
  X -- reference point set
  P -- point set to be matched with the reference

  Output:
  P_matched -- reordered P, so that the elements in P match the elements in X
  """

  P_matched = np.empty(np.shape(P))
  num_pts = np.shape(X)[1]
  dist_mat = np.empty((num_pts,num_pts))
  for i in range(num_pts):
    for j in range(num_pts):
      dist_mat[i,j] = np.linalg.norm(X[:,i] - P[:,j])

  rows, cols = [], []
  while(len(rows) != num_pts):
    min_ind = np.unravel_index(np.argmin(dist_mat, axis=None), dist_mat.shape)
    if (min_ind[0] in rows) or (min_ind[1] in cols):
      dist_mat[min_ind] = math.inf
    else:
      P_matched[:,min_ind[0]] = P[:,min_ind[1]]
      dist_mat[min_ind] = math.inf
      rows.append(min_ind[0])
      cols.append(min_ind[1])

  # for i in range(num_pts):
  #   min_ind = np.unravel_index(np.argmin(dist_mat, axis=None), dist_mat.shape)
  #   P_matched[:,min_ind[0]] = P[:,min_ind[1]]
  #   dist_mat[min_ind[0],:] = math.inf
  #   dist_mat[:,min_ind[1]] = math.inf

  return P_matched

def icp(X, P, do_matching):

  P0 = P
  for i in range(10):
    # calculate RMSE
    rmse = 0
    for j in range(P.shape[1]):
      rmse += math.pow(P[0,j] - X[0,j], 2) + math.pow(P[1,j] - X[1,j], 2)
    rmse = math.sqrt(rmse / P.shape[1])

    # print and plot
    print("Iteration:", i, " RMSE:", rmse)
    plot_icp(X, P, P0, i, rmse)

    # data association
    if do_matching:
      P = closest_point_matching(X, P)

    # substract center of mass
    mx = np.transpose([np.mean(X, 1)])
    mp = np.transpose([np.mean(P, 1)])
    X_prime = X - mx
    P_prime = P - mp

    # singular value decomposition
    W = np.dot(X_prime, P_prime.T)
    U, _, V = np.linalg.svd(W)

    # calculate rotation and translation
    R = np.dot(U, V.T)
    t = mx - np.dot(R, mp)

    # apply transformation
    P = np.dot(R, P) + t

  return

def main():

  X, P1, P2, P3, P4 = generate_data()

  # icp(X, P1, False)
  # icp(X, P2, False)
  # icp(X, P3, True)
  icp(X, P4, True)

  plt.waitforbuttonpress()

if __name__ == "__main__":
  main()


import sys
import math
from math import *
from numpy import *
import matplotlib.pyplot as plt
from D52 import R_lst, t_lst, L_lst
from mat import A2195_T84 as mat
from D53 import buckling_opt
from D54 import getConfigs

def format_list(beam_configurations):
    cor_list = []
    for i in range(len(m_viable)):
        cor_list.append(beam_configurations[i][3])
    return cor_list

def tankandbeam_opt(m_viable, beam_viable):
    viable_list = buckling_opt(1590, R_lst, t_lst, L_lst, mat)[0]  #get list of all dimensions
    m_final = []
    m_beam_config = []
    m_viable_int = []
    for k in range(len(m_viable)):
        m_total = 8 * beam_viable[k] + 1590                        #Adjust total mass
        m_tank = buckling_opt(m_total,R_lst,t_lst,L_lst,mat)[0][3] #Take specifc mass of tank
        if k >= len(m_tank):                                       #Ignore numbers that don't exist in filtered list
            break
        m_viable_int.append(m_tank[k])
        m_beam_config.append(8*beam_viable[k])
    for i in range(len(m_viable_int)):
        tank_int = m_viable_int[i]                                 #Tank mass
        beam_int = m_beam_config[i]                                #Total Beam mass
        m_int = tank_int + beam_int                                #add masses together
        m_final.append(m_int)
    m_min = min(m_final)
    m_min_index = m_final.index(m_min)
    L_min = viable_list[2][m_min_index]
    R_min = viable_list[0][m_min_index]
    t_min = viable_list[1][m_min_index]

    return(m_min, L_min, R_min, t_min, m_min_index)

m_viable = buckling_opt(1590,R_lst, t_lst, L_lst, mat)[0][3]       #get the tank mass list from D53
beam_configurations = getConfigs(1)
beam_viable = format_list(beam_configurations)                     #get the beam mass list from D54

print(tankandbeam_opt(m_viable, beam_viable))


import json
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torchvision
import os
import argparse

from pathlib import Path
import tqdm

#import models
from models.backbone import Backbone
from models.classification_head import ClassificationHead

#import dataloaders
from dataloader.cifar10 import CIFAR10
from dataloader.fashion_mnist import FashionMNIST
from dataloader.multi_task_batch_scheduler import BatchSchedulerSampler

# import dataset classes for concatenation purposes
from torch.utils.data.dataset import ConcatDataset

#import progressbar
from utils.utils import progress_bar
from utils.variables import classifier_dict

# trying to figure out how to enumerate over the two dataloaders
parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')

parser.add_argument("--batch-size", type=int, default=128,
                    help="Training Batch size")
parser.add_argument("--n_epochs", type=int, default=700,
                    help="No of epochs")
parser.add_argument('--lr', default=0.1, type=float, help='learning rate')
parser.add_argument("--cifar_checkpoint_path", type=str, default="",
                    help="CIFAR10's checkpoint")
parser.add_argument("--fashion_mnist_checkpoint_path", type=str, default="",
                    help="FASHION-MNIST's checkpoint")
parser.add_argument("--training_type", type=str, default="conditioned",
                    help="type of training (conditioned")
parser.add_argument("--num-workers", type=int, default=2,
                    help="Number of workers for dataloaders")
parser.add_argument("--backbone", type=str, default="resnet18",
                    help="BACKBONE TO TRAIN WITH:resnet18/resnet50/resnest50")
args = parser.parse_args()

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

#global variables

best_cifar_acc = 0  # best test accuracy for cifar
best_fashion_mnist_acc = 0  # best test accuracy for fashion mnist
start_epoch = 0  # start from epoch 0 or last checkpoint epoch

# define learning rate
learning_rate = 0

# returns trainloader and two testloaders

def get_dataloaders():

    # combined trainloader for cifar & fash-mnist
    # samples alternately from the two batches
    cifar = CIFAR10(data_root="dataset/cifar10",
                    transform=None,
                    mode='train',
                    )

    fashion_mnist = FashionMNIST(data_root="dataset/fashion-mnist",
                                 transform=None,
                                 mode='train',
                                 )

    concat_dataset = ConcatDataset([cifar, fashion_mnist])

    trainloader = torch.utils.data.DataLoader(dataset=concat_dataset,
                                              sampler=BatchSchedulerSampler(dataset=concat_dataset,
                                                                            batch_size=args.batch_size),
                                              batch_size=args.batch_size,
                                              )

    testset_cifar = CIFAR10(data_root="dataset/cifar10",
                            transform=None,
                            mode='test')

    testloader_cifar = torch.utils.data.DataLoader(
        testset_cifar, batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers)

    testset_fashion_mnist = FashionMNIST(data_root="dataset/fashion-mnist",
                                         transform=None,
                                         mode='test')

    testloader_fashion_mnist = torch.utils.data.DataLoader(
        testset_fashion_mnist, batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers)

    return trainloader, testloader_cifar, testloader_fashion_mnist

# conditional training
def train(epoch):
    print('\nEpoch: %d' % epoch)
    print('\nLearning Rate: %f'%args.lr)
    # print('Total Epochs: %d' % args.n_epochs)
    print('Training Type: : %s' % args.training_type)

    model.train()
    classifier_cifar.train()
    classifier_fashion_mnist.train()
    train_loss = 0
    correct = 0
    total = 0

    for batch_idx, (inputs, targets, meta) in enumerate(trainloader):
        inputs, targets = inputs.to(device), targets.to(device)
        inputs = inputs.permute(0, 3, 1, 2)
        embedding_labels = meta['conditioning_label'].to(device)
        embedding_labels = embedding_labels.unsqueeze(1)
        optim_model.zero_grad()
        optim_classifier_cifar.zero_grad()
        optim_classifier_fashion_mnist.zero_grad()

        # fwd pass through common backbone
        output = model(inputs, embedding_labels)
        output_cifar = classifier_cifar(output)
        output_fashion_mnist = classifier_fashion_mnist(output)
        loss_cifar = criterion(output_cifar, targets)
        loss_fashion_mnist = criterion(output_fashion_mnist, targets)

        # print(meta['conditioning_label'][0],"label")

        #case: dataset is cifar
        if meta['conditioning_label'][0] == 0:
            #print("dataset: cifar")
            loss = loss_cifar
            outputs = output_cifar
            loss.backward()
            optim_classifier_cifar.step()
            optim_model.step()

        #case: dataset is fashion_mnist
        if meta['conditioning_label'][0] == 1:
            #print("dataset: fashion_mnist")
            loss = loss_fashion_mnist
            outputs = output_fashion_mnist
            loss.backward()
            optim_classifier_fashion_mnist.step()
            optim_model.step()
        train_loss += loss.item()
        _, predicted = outputs.max(1)
        # print("predicted",predicted)
        total += targets.size(0)
        correct += predicted.eq(targets).sum().item()

        progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
                     % (train_loss/(batch_idx+1), 100.*correct/total, correct, total))

        # break for debugging

# code to dump config at the path

def dump_config(epoch, save_dir):

    config = {
        'epoch:': epoch,
        'learning_rate': learning_rate,
        'cifar_acc': best_cifar_acc,
        'fashion_mnist_acc': best_fashion_mnist_acc,
        'n_epochs': args.n_epochs
    }
    with open(save_dir+'/config.json', 'w') as fp:
        json.dump(config, fp)

def test(epoch):
    print("in testing code")
    global best_cifar_acc
    global best_fashion_mnist_acc
    model.eval()
    classifier_cifar.eval()
    classifier_fashion_mnist.eval()

    ########## EVALUATE IN CIFAR TESTLOADER ONCE ############################

    test_loss = 0
    correct = 0
    total = 0
    with torch.no_grad():
        for batch_idx, (inputs, targets, meta) in enumerate(testloader_cifar):
            inputs, targets = inputs.to(device), targets.to(device)
            inputs = inputs.permute(0, 3, 1, 2)
            embedding_labels = meta['conditioning_label'].to(device)
            embedding_labels = embedding_labels.unsqueeze(1)
            outputs = model(inputs, embedding_labels)
            outputs = classifier_cifar(outputs)
            loss = criterion(outputs, targets)

            test_loss += loss.item()
            _, predicted = outputs.max(1)
            total += targets.size(0)
            correct += predicted.eq(targets).sum().item()

            progress_bar(batch_idx, len(testloader_cifar), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
                         % (test_loss/(batch_idx+1), 100.*correct/total, correct, total))
    # Save checkpoint.
    acc = 100.*correct/total
    if acc > best_cifar_acc:
        print('Saving..')
        # state = {
        #     'model': model.state_dict(),
        #     'classifier_cifar': classifier_cifar.state_dict(),
        #     'classifier_fashion_mnist': classifier_fashion_mnist.state_dict(),
        #     'cifar_acc': acc,
        #     'fashion_mnist_acc': best_fashion_mnist_acc,
        #     'epoch': epoch,
        # }
        # dump only the data for cifar
        state = {
            'model': model.state_dict(),
            'classifier_cifar': classifier_cifar.state_dict(),
            'classifier_fashion_mnist': classifier_fashion_mnist.state_dict(),
            'cifar_acc': acc,
            'epoch': epoch,
        }
        # if not os.path.isdir('checkpoint'):
        #     os.mkdir('checkpoint')
        # dump the dictionary to the
        torch.save(state, str(save_dir/'cifar'/'checkpoint.pth'))
        best_cifar_acc = acc
    dump_config(epoch, str(save_dir/'cifar'))

    ########## EVALUATE IN FASHION MNIST  TESTLOADER ONCE ############################
    acc = 0
    test_loss = 0
    correct = 0
    total = 0
    with torch.no_grad():
        for batch_idx, (inputs, targets, meta) in enumerate(testloader_fashion_mnist):
            inputs, targets = inputs.to(device), targets.to(device)
            inputs = inputs.permute(0, 3, 1, 2)
            embedding_labels = meta['conditioning_label'].to(device)
            embedding_labels = embedding_labels.unsqueeze(1)
            outputs = model(inputs, embedding_labels)
            outputs = classifier_fashion_mnist(outputs)
            # print("targets",targets)

            loss = criterion(outputs, targets)

            test_loss += loss.item()
            _, predicted = outputs.max(1)
            # print("predicted",predicted)
            total += targets.size(0)
            correct += predicted.eq(targets).sum().item()

            progress_bar(batch_idx, len(testloader_fashion_mnist), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
                         % (test_loss/(batch_idx+1), 100.*correct/total, correct, total))

    # Save checkpoint.
    acc = 100.*correct/total
    if acc > best_fashion_mnist_acc:
        print('Saving..')
        # state = {
        #     'model': model.state_dict(),
        #     'classifier_cifar': classifier_cifar.state_dict(),
        #     'classifier_fashion_mnist': classifier_fashion_mnist.state_dict(),
        #     'cifar_acc': best_cifar_acc,
        #     'fashion_mnist_acc': acc,
        #     'epoch': epoch,
        # }
        #dump ony fashioon mnist data
        state = {
            'model': model.state_dict(),
            'classifier_cifar': classifier_cifar.state_dict(),
            'classifier_fashion_mnist': classifier_fashion_mnist.state_dict(),
            'fashion_mnist_acc': acc,
            'epoch': epoch,
        }
        # if not os.path.isdir('checkpoint'):
        #     os.mkdir('checkpoint')
        # dump the dictionary to the
        torch.save(state, str(save_dir/'fashion_mnist'/'checkpoint.pth'))
        best_fashion_mnist_acc = acc

    dump_config(epoch, str(save_dir/'fashion_mnist'))

###################################### TRAINING STARTS HERE ############################
local_data_path = Path('.').absolute()
# create experiment
experiment = args.training_type
save_dir = (local_data_path/'experiments'/args.backbone/experiment)
(save_dir/'cifar').mkdir(exist_ok=True, parents=True)
(save_dir/'fashion_mnist').mkdir(exist_ok=True, parents=True)

# get dataloaders
trainloader, testloader_cifar, testloader_fashion_mnist = get_dataloaders()

# get model with embedding
model = Backbone(backbone=args.backbone,apply_embedding=True).to(device)

# get two separate classifiers
classifier_cifar = ClassificationHead(num_classes=10,in_channels=classifier_dict[args.backbone]).to(device)
classifier_fashion_mnist = ClassificationHead(num_classes=10,in_channels=classifier_dict[args.backbone]).to(device)

# create loss
criterion = nn.CrossEntropyLoss()

# create optimizers
optim_model = optim.SGD(model.parameters(), lr=args.lr,
                        momentum=0.9, weight_decay=5e-4)
optim_classifier_cifar = optim.SGD(classifier_cifar.parameters(), lr=args.lr,
                                   momentum=0.9, weight_decay=5e-4)
optim_classifier_fashion_mnist = optim.SGD(classifier_fashion_mnist.parameters(), lr=args.lr,
                                           momentum=0.9, weight_decay=5e-4)

############ CODE FOR RESUMING THE TRAINING ###########################################
if args.cifar_checkpoint_path != "" and args.fashion_mnist_checkpoint_path!= "":

    # Load data from cifar checkpoint.
    print('==> Resuming from cifar..')
    checkpoint = torch.load(args.cifar_checkpoint_path)
    # LOAD THE MODEL FROM CIFAR BEST WEIGHT FOR NOW, TRY LOADING FROM FASHION-MNIST IN ANOTHER EXPERIMENT
    model.load_state_dict(checkpoint['model'])
    classifier_cifar.load_state_dict(checkpoint['classifier_cifar'])
    best_cifar_acc = checkpoint['cifar_acc']
    cifar_epoch = checkpoint['epoch']

    # Load data from fashion-mnist checkpoint.
    print('==> Resuming from fashion mnist..')
    checkpoint = torch.load(args.fashion_mnist_checkpoint_path)
    #model.load_state_dict(checkpoint['model'])
    classifier_fashion_mnist.load_state_dict(checkpoint['classifier_fashion_mnist'])
    best_fashion_mnist_acc = checkpoint['fashion_mnist_acc']
    fashion_mnist_epoch = checkpoint['epoch']

    # Resolve conflicts in loading data from two separate checkpoints
    start_epoch= min(cifar_epoch, fashion_mnist_epoch)

def update_learning_rate(epoch, n_epochs):
    # update model lr
    ratio = epoch/n_epochs
    global learning_rate
    if ratio < 0.4:
        learning_rate = 0.1
    elif 0.4 <= ratio < 0.7:
        learning_rate = 0.01
    else:
        learning_rate = 0.001
    # update learning rate
    for param_group in optim_model.param_groups:
        param_group['lr'] = learning_rate
    # update classifier_cifar learning rate
    for param_group in optim_classifier_cifar.param_groups:
        param_group['lr'] = learning_rate
    # update classifier_fashion_mnist learning rate
    for param_group in optim_classifier_fashion_mnist.param_groups:
        param_group['lr'] = learning_rate

    print("ratio: ", ratio, " lr: ", learning_rate)

def main():

    # apply the training schedue
    for epoch in range(start_epoch, start_epoch+400):
        # call train
        #update_learning_rate(epoch, args.n_epochs)
        train(epoch)
        test(epoch)

        print("epoch: ", epoch, "Cifar best accuracy found is: ", best_cifar_acc,
              "fashion mnist best accuracy found is: ", best_fashion_mnist_acc)

    print("Cifar best accuracy found is: ", best_cifar_acc,
          "fashion mnist best accuracy found is: ", best_fashion_mnist_acc)

if __name__ == '__main__':
    main()


#!/usr/bin/env python

import rospy
import tf

if __name__ == '__main__':
    rospy.init_node('pose_saver')
    listener = tf.TransformListener()

    listener.waitForTransform('/map', '/base_footprint', rospy.Time(0), rospy.Duration(1.0))
    rate = rospy.Rate(20.0)

    with open('/media/hao/hao/dataset/ros_pose.txt', 'w') as f:
        while not rospy.is_shutdown():
            now = rospy.Time.now()
            listener.waitForTransform('/map', '/base_footprint', now, rospy.Duration(10.0))
            (trans, rot) = listener.lookupTransform('/map', '/base_footprint', now)

            euler = tf.transformations.euler_from_quaternion(rot)

            f.writelines(str(now) + '\t' + str(trans[0]) + '\t' + str(trans[1]) + '\t' + str(euler[2]) + '\n')
            rate.sleep()


import time

from flask import request

from data_sheet import session, ShortMessage
from utils.tool import short_message
from ..user import bp

@bp.route('/mobile_text')
def test():
    phone = request.json.get("phone")
    if phone is None:
        return {'code':201,'message':'请输入手机号码'}
    try:
         indonesia = short_message(phone)
    except Exception as e:
        print(e)
        return {'code':202,'meaasge':'发送失败，请稍后再试'}
    try:
        result = session.query(ShortMessage).filter(ShortMessage.phonenumber == phone).first()
        if result is None:
            newMessage = ShortMessage(phonenumber=phone,meaasge=indonesia,time=str(time.time()))
            session.add(newMessage)
            session.commit()
        else:
            result.meaasge = indonesia
            result.time = str(time.time())
            session.add(result)
            session.commit()
    except Exception as e:
        session.rollback()
        return {'code':203,'message':'验证码无效'}
    return  {'code':200,'message':'success'}


# Escreva um programa para aprovar o empréstimo bancario para a compra de uma casa. O valor da casa,
# o sálario do comprador e em quantos anos ela vai pagar.
# calcule o valor da prestação mensal, sabendo que ela não pode excerder 30 % do sálario ou então o
# emprestimo será negado.

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

valor_casa = float(input('\033[37;1m Digite o valor da casa:\033[m '))
renda = float(input('\033[37;1m Digite o valor da sua renda:\033[m '))
tempo = int(input('\033[37;1m Digite quantos anos você que pagar:\033[m'))

valor_prestaçao = valor_casa / (tempo * 12)
renda = (renda * 30) / 100

if valor_prestaçao <= renda:
    print('\033[34;1m O emprestimo foi aprovado!!\033[m')
else:
    print('\033[31;1m O emprestimo foi negado\n sua renda não e o suficiente!!\033[m')


# based on A Plus Coding's tutorial at: https://www.youtube.com/watch?v=GKe1aGQlKDY&list=PLryDJVmh-ww1OZnkZkzlaewDrhHy2Rli2

import pygame
import sys
from game_window_class import *
from button_class import *

FPS = 60 # max frames per second
EVALUATE_DAMPER = 10 # decrease to fo evaluations faster
WIDTH = 1150
HEIGHT = 800
GAME_WIN_X = 25
GAME_WIN_Y = 75
BG_COLOUR = (59, 55, 53)

RUN_BUTTON_COLOUR = (72, 107, 79)
RUN_BUTTON_HOVER_COLOUR = (82, 125, 91)
RUN_BUTTON_BORDER_COLOUR = (0,0,0) # (33,33,33)

PAUSE_BUTTON_COLOUR = (130, 113, 77)
PAUSE_BUTTON_HOVER_COLOUR = (150, 130, 87)
PAUSE_BUTTON_BORDER_COLOUR = (0,0,0)

RESET_BUTTON_COLOUR = (110, 69, 69)
RESET_BUTTON_HOVER_COLOUR = (135, 84, 84)
RESET_BUTTON_BORDER_COLOUR = (0,0,0)

#--------------------  SETTING FUNCTIONS  --------------------#
def get_events():
    global running
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()
            if mouse_on_grid(mouse_pos):
                click_cell(mouse_pos)
            else:
                for button in buttons:
                    button.click()

def update():
    game_window.update()
    for button in buttons:
        button.update(mouse_pos, game_state=state)

def draw():
    window.fill(BG_COLOUR)
    for button in buttons:
        button.draw()
    game_window.draw()

#--------------------  RUNNING FUNCTIONS  --------------------#
def running_get_events():
    global running
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()
            if mouse_on_grid(mouse_pos):
                click_cell(mouse_pos)
            else:
                for button in buttons:
                    button.click()

def running_update():
    game_window.update()
    for button in buttons:
        button.update(mouse_pos, game_state=state)
    if frame_count%(FPS//EVALUATE_DAMPER) == 0:
        game_window.evaluate()

def running_draw():
    window.fill(BG_COLOUR)
    for button in buttons:
        button.draw()
    game_window.draw()

#--------------------  PAUSED FUNCTIONS  --------------------#
def paused_get_events():
    global running
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()
            if mouse_on_grid(mouse_pos):
                click_cell(mouse_pos)
            else:
                for button in buttons:
                    button.click()

def paused_update():
    game_window.update()
    for button in buttons:
        button.update(mouse_pos, game_state=state)

def paused_draw():
    window.fill(BG_COLOUR)
    for button in buttons:
        button.draw()
    game_window.draw()

def mouse_on_grid(pos):
    if (pos[0] > GAME_WIN_X) and (pos[0] < WIDTH-GAME_WIN_X):
        if (pos[1] > GAME_WIN_Y) and (pos[1] < GAME_WIN_Y+WIN_HEIGHT):
            return True
    return False

def click_cell(pos):
    grid_pos = [pos[0]-GAME_WIN_X, pos[1]-GAME_WIN_Y]
    grid_pos[0] = grid_pos[0]//CELL_SIZE
    grid_pos[1] = grid_pos[1]//CELL_SIZE
    if game_window.grid[grid_pos[1]][grid_pos[0]].alive:
        game_window.grid[grid_pos[1]][grid_pos[0]].alive = False
    else:
        game_window.grid[grid_pos[1]][grid_pos[0]].alive = True

def make_buttons():
    buttons = []
    # RUN
    buttons.append(Button(window, WIDTH//2-50, 25, 100, 30, text='RUN',
                         colour=RUN_BUTTON_COLOUR, hover_colour=RUN_BUTTON_HOVER_COLOUR,
                         border_colour=RUN_BUTTON_BORDER_COLOUR, bold_text=True,
                         function=run_game, state='setting'))
    # PAUSE
    buttons.append(Button(window, WIDTH//2-50, 25, 100, 30, text='PAUSE',
                         colour=PAUSE_BUTTON_COLOUR, hover_colour=PAUSE_BUTTON_HOVER_COLOUR,
                         border_colour=PAUSE_BUTTON_BORDER_COLOUR, bold_text=True,
                         function=pause_game, state='running'))
    # RESUME
    buttons.append(Button(window, WIDTH//5-50, 25, 100, 30, text='RESUME',
                         colour=RUN_BUTTON_COLOUR, hover_colour=RUN_BUTTON_HOVER_COLOUR,
                         border_colour=RUN_BUTTON_BORDER_COLOUR, bold_text=True,
                         function=run_game, state='paused'))
    # RESET
    buttons.append(Button(window, WIDTH//1.25-50, 25, 100, 30, text='RESET',
                         colour=RESET_BUTTON_COLOUR, hover_colour=RESET_BUTTON_HOVER_COLOUR,
                         border_colour=RESET_BUTTON_BORDER_COLOUR, bold_text=True,
                         function=reset_grid, state='paused'))
    return buttons

def run_game():
    global state
    state = 'running'

def pause_game():
    global state
    state = 'paused'

def reset_grid():
    global state
    state = 'setting'
    game_window.reset_grid()

pygame.init()
window = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
game_window = GameWindow(window, GAME_WIN_X, GAME_WIN_Y)
buttons = make_buttons()
state = 'setting'
frame_count = 0

running = True
while running:
    frame_count += 1
    mouse_pos = pygame.mouse.get_pos()
    if state == 'setting':
        get_events()
        update()
        draw()
    if state == 'running':
        running_get_events()
        running_update()
        running_draw()
    if state == 'paused':
        paused_get_events()
        paused_update()
        paused_draw()
    pygame.display.update()
    clock.tick(FPS)
pygame.quit()
sys.exit()


import PAsearchSites
import PAgenres
def search(results,encodedTitle,title,searchTitle,siteNum,lang,searchByDateActor,searchDate,searchAll,searchSiteID):
    searchResults = HTML.ElementFromURL(PAsearchSites.getSearchSearchURL(siteNum) + encodedTitle)
    for searchResult in searchResults.xpath('//div[@class="video-item-big"]'):

        #Log(searchResult.text_content())
        titleNoFormatting = searchResult.xpath('.//a[@class="v-title"]')[0].text_content()
        Log("Result Title: " + titleNoFormatting)
        curID = searchResult.xpath('.//a')[0].get("href")
        curID = curID.replace('/','+')
        Log("ID: " + curID)
        releasedDate = searchResult.xpath('.//span[@class="v-stat"]//span[@class="txt"]')[0].text_content()
        releasedDate = datetime.strptime(releasedDate, '%d.%m.%y').strftime('%Y-%m-%d')
        Log(releasedDate)
        Log(str(curID))
        lowerResultTitle = str(titleNoFormatting).lower()
        if searchByDateActor != True:
            score = 102 - Util.LevenshteinDistance(searchTitle.lower(), titleNoFormatting.lower())
        else:
            searchDateCompare = datetime.strptime(searchDate, '%Y-%m-%d').strftime('%Y-%m-%d')
            score = 102 - Util.LevenshteinDistance(searchDateCompare.lower(), releasedDate.lower())
        titleNoFormatting = "[" + releasedDate + "] " + titleNoFormatting + " [" + PAsearchSites.searchSites[siteNum][1] + "]"
        results.Append(MetadataSearchResult(id = curID + "|" + str(siteNum), name = titleNoFormatting, score = score, lang = lang))

    return results

def update(metadata,siteID,movieGenres):
    Log('******UPDATE CALLED*******')
    temp = str(metadata.id).split("|")[0].replace('+','/')

    url = PAsearchSites.getSearchBaseURL(siteID) + temp
    detailsPageElements = HTML.ElementFromURL(url)

    # Summary
    metadata.studio = "Porndoe Premium"
    metadata.summary = detailsPageElements.xpath('//p[@class="description"]')[0].text_content()
    metadata.title = detailsPageElements.xpath('//h1[@class="big-container-title"]')[0].text_content()
    releasedDate = detailsPageElements.xpath('//div[@class="col date"]')[0].text_content()[71:-37]
    Log(releasedDate)
    date_object = datetime.strptime(releasedDate, '%d.%m.%y')
    metadata.originally_available_at = date_object
    metadata.year = metadata.originally_available_at.year
    metadata.tagline = detailsPageElements.xpath('//div[@class="col channel"]//a')[0].text_content()
    Log(metadata.tagline)
    metadata.collections.clear()
    metadata.collections.add(metadata.tagline)

    # Genres
    movieGenres.clearGenres()
    genres = detailsPageElements.xpath('//a[contains(@href,"/videos/category/")]')

    if len(genres) > 0:
        for genreLink in genres:
            genreName = genreLink.text_content().strip('\n').lower()
            movieGenres.addGenre(genreName)

    genres = detailsPageElements.xpath('//a[contains(@href,"/videos/tag/")]')

    if len(genres) > 0:
        for genreLink in genres:
            genreName = genreLink.text_content().strip('\n').lower()
            movieGenres.addGenre(genreName)

    # Actors
    metadata.roles.clear()
    actors = detailsPageElements.xpath('//a[contains(@class,"pornstar")]')
    if len(actors) > 0:
        for actorLink in actors:
            role = metadata.roles.new()
            actorName = actorLink.text_content()
            role.name = actorName
            actorPageURL = "https://porndoepremium.com" + actorLink.get("href")
            actorPage = HTML.ElementFromURL(actorPageURL)
            actorPhotoURL = actorPage.xpath('//img[@alt="PS"]')[0].get("src")
            role.photo = actorPhotoURL

    # Posters/Background
    valid_names = list()
    metadata.posters.validate_keys(valid_names)
    metadata.art.validate_keys(valid_names)

    background = detailsPageElements.xpath('//img[@class="owl-lazy"]')[0].get("data-src").replace("thumb/0x250/","crop/1920x1080/")
    try:
        metadata.art[background] = Proxy.Preview(HTTP.Request(background).content, sort_order = 1)
    except:
        pass

    posters = detailsPageElements.xpath('//img[@class="owl-lazy"]')
    posterNum = 1
    for poster in posters:
        posterURL = poster.get("data-src")
        metadata.posters[posterURL] = Proxy.Preview(HTTP.Request(posterURL).content, sort_order = posterNum)
        posterNum += 1

    return metadata


from rest_framework import status
from rest_framework.authentication import BasicAuthentication, SessionAuthentication
from rest_framework.generics import RetrieveAPIView, ListAPIView
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.response import Response
from rest_framework_jwt.authentication import JSONWebTokenAuthentication

from accounts.models import Profile
from rates.api.serializers import FiatRateListSerializer
from rates.models import FiatRate

class FiatRateAPIView(RetrieveAPIView):
    permission_classes = (IsAuthenticatedOrReadOnly,)
    authentication_classes = [BasicAuthentication, SessionAuthentication, JSONWebTokenAuthentication]

    def get(self, request):
        try:
            user_profile_obj = Profile.objects.get(user=request.user)
            user_country = FiatRate.objects.get(country=user_profile_obj.country)
            status_code = status.HTTP_200_OK
            response = {
                'success': True,
                'status code': status_code,
                'message': 'User Fiat Fetched',
                'data': [{
                    'updated': user_country.updated,
                    'timestamp': user_country.timestamp,
                    'country': user_profile_obj.get_country(),
                    'dollar_rate': user_country.dollar_rate
                }]
            }

        except Exception as e:
            user_profile_obj = Profile.objects.get(user=request.user)
            user_country = FiatRate.objects.get(country='United States Of America')
            status_code = status.HTTP_200_OK
            response = {
                'success': True,
                'status code': status_code,
                'message': 'User Fiat Fetched',
                'data': [{
                    'updated': user_country.updated,
                    'timestamp': user_country.timestamp,
                    'country': user_profile_obj.get_country(),
                    'dollar_rate': user_country.dollar_rate
                }]
            }
        return Response(response, status=status_code)

class FiatListView(ListAPIView):
    authentication_classes = [BasicAuthentication, SessionAuthentication, JSONWebTokenAuthentication]
    serializer_class = FiatRateListSerializer
    queryset = FiatRate.objects.all()
    permission_classes = (IsAuthenticatedOrReadOnly,)
    paginate_by = 15


import logging

from rest_framework import viewsets, mixins, serializers, generics

from apps.proceso.models.forms.campo import Campo
from .campo_validation_view import CampoValidationSerializer
from .validation_view import ValidationSerializer
log = logging.getLogger(__name__)

class CampoSerializer(serializers.ModelSerializer):
    campovalidation_set = CampoValidationSerializer(many=True, read_only=True)
    # validation = ValidationSerializer(many=True, read_only=True)
    class Meta:
        model = Campo
        fields = ('id',
                'label', 'name', 'type', 'required',
                'width', 'placeholder',
                'model_name',
                'model_pk',
                'model_label',
                'json', 'formulario', 'icon',
                'prefix', 'hint_start', 'hint_end_count_text', 'disabled',
                'multiselect', 'order', 'accept_fileinput', 'multiple_fileinput',

                'campovalidation_set',
                # 'validation',
                'tipo_validador',
                'roles_validadores',

                'documento',
                'campos_validados',

                'fecha_creacion', 'fecha_actualizacion')
        read_only_fields = ('id', 'fecha_creacion', 'fecha_actualizacion',)

class CampoViewSet(viewsets.ModelViewSet):
    """
    A simple ViewSet for listing or retrieving Campo.
    """
    queryset = Campo.objects.all()
    serializer_class = CampoSerializer

class CampoList(generics.ListCreateAPIView):

    serializer_class = CampoSerializer
    def get_queryset(self):
        formulario_id = self.kwargs['formulario_id']
        if formulario_id is not None:
            return Campo.objects.filter(formulario__id=formulario_id).order_by('order')
        else:
            return Campo.objects.all().order_by('order')


import pygame
from pygame import font
class Button():
    def __init__(self,alien_setting,screen,msg):
        # 初始化按钮属性
        self.screen = screen
        self.screenRect = screen.get_rect()

        #设置按钮的尺寸和其他属性
        self.width,self.height = 200,50
        self.buttonColor = (0,255,0)
        self.textColor = (255,255,255)
        self.font = font.SysFont(None,48)

        # 创建按钮的rect对象，并使其居中
        self.rect = pygame.Rect(0,0,self.width,self.height)
        self.rect.center = self.screenRect.center

        #该按钮的标签只需要创建一次
        self.prepMsg(msg)

    def prepMsg(self,msg):
        '''将msg渲染为图像，并使其在按钮上居中'''
        self.msgImage = self.font.render(msg,True,self.textColor,self.buttonColor)
        self.msgImageRect = self.msgImage.get_rect()
        self.msgImageRect.center = self.rect.center

    def drawButton(self):
        self.screen.fill(self.buttonColor,self.rect)
        self.screen.blit(self.msgImage,self.msgImageRect)


import traceback
from icebox.billing.biller import BaseBiller
from icebox.billing.biller import RESOURCE_TYPE_BANDWIDTH

from densefog import logger
logger = logger.getChild(__file__)

class EipBiller(BaseBiller):

    def _collect_usages(self, project_id, eip_ids):
        from icebox.model.iaas import eip as eip_model
        page = eip_model.limitation(project_ids=[project_id],
                                    eip_ids=eip_ids)
        eips = page['items']

        resource_usages = []
        for eip in eips:
            resource_usages.append({
                'resource_id': eip['id'],
                'resource_name': eip['name'],
                'resource_usage': '%dMbps' % eip['bandwidth'],
            })
        return resource_usages

    def allocate_eips(self, project_id, eip_ids):
        logger.info('biller to allocate eips: %s' % eip_ids)

        if not project_id or not eip_ids:
            return

        resource_usages = self._collect_usages(project_id, eip_ids)

        try:
            resp = self.create_resources(project_id,
                                         RESOURCE_TYPE_BANDWIDTH,
                                         None,
                                         resource_usages)
            logger.info('create_resources resp code: %s, message: %s' % (
                        resp['retCode'], resp['message']))
            return resp

        except Exception:
            stack = traceback.format_exc()
            logger.trace(stack)
        pass

    def release_eips(self, project_id, eip_ids):
        logger.info('biller to delete eips: %s' % eip_ids)

        if not project_id or not eip_ids:
            return

        try:
            resp = self.delete_resources(project_id, eip_ids)

            logger.info('delete_resources resp code: %s, message: %s' % (
                        resp['retCode'], resp['message']))
            return resp

        except Exception:
            stack = traceback.format_exc()
            logger.trace(stack)

    def update_bandwidth(self, project_id, eip_ids):
        logger.info('biller to update brandwidth eips: %s' % eip_ids)

        if not project_id or not eip_ids:
            return

        resource_usages = self._collect_usages(project_id, eip_ids)

        try:
            resps = []
            for resource_usage in resource_usages:
                resource_id = resource_usage['resource_id']
                usage = resource_usage['resource_usage']
                resp = self.modify_resource_attributes(project_id,
                                                       resource_id,
                                                       None,
                                                       usage)

                logger.info('modify_resource_attributes resp code: %s, '
                            'message: %s' % (resp['retCode'], resp['message']))
                resps.append(resp)

            return resps

        except Exception:
            stack = traceback.format_exc()
            logger.trace(stack)


import numpy as np
import os

def read_obj(filename):

    faces = []
    vertices = []
    fid = open(filename, "r")
    node_counter = 0
    while True:

        line = fid.readline()
        if line == "":
            break
        while line.endswith("\\"):
            # Remove backslash and concatenate with next line
            line = line[:-1] + fid.readline()
        if line.startswith("v"):
            coord = line.split()
            coord.pop(0)
            node_counter += 1
            vertices.append(np.array([float(c) for c in coord]))

        elif line.startswith("f "):
            fields = line.split()
            fields.pop(0)

            # in some obj faces are defined as -70//-70 -69//-69 -62//-62
            cleaned_fields = []
            for f in fields:
                f = int(f.split("/")[0]) - 1
                if f < 0:
                    f = node_counter + f
                cleaned_fields.append(f)
            faces.append(np.array(cleaned_fields))
    fid.close()
    faces_np = np.row_stack(faces)
    vertices_np = np.row_stack(vertices)

    return vertices_np, faces_np

def write_to_obj(filename, vertices, faces=None):
    if not filename.endswith('obj'):
        filename += '.obj'
    name = filename.split('/')[-1]
    path = filename.strip(name)
    if path == '':
        path = './'
    if not os.path.exists(path):
        os.makedirs(path)
    num = vertices.shape[0]
    if faces is None:
        faces = np.loadtxt('./{:d}face.txt'.format(num), dtype=np.int)
    num_face = faces.shape[0]
    with open(filename, 'w') as f:
        f.write(('v {:f} {:f} {:f}\n'*num).format(*vertices.reshape(-1).tolist()))
        f.write(('f {:d} {:d} {:d}\n'*num_face).format(*faces.reshape(-1).tolist()))

def cal_rotation_matrix(rotation_angle=0, axis='x'):
    cos_value = np.cos(rotation_angle)
    sin_value = np.sin(rotation_angle)
    if axis == 'x':
        rotation_matrix = np.array(
            [
                [1., 0., 0.],
                [0., cos_value, -1*sin_value],
                [0., 1*sin_value, cos_value]
            ]
        )
    elif axis == 'y':
        rotation_matrix = np.array(
            [
                [cos_value, 0., sin_value],
                [0., 1., 0.],
                [-1*sin_value, 0., cos_value]
            ]
    )
    elif axis == 'z':
        rotation_matrix = np.array(
            [
                [cos_value, -1*sin_value, 0],
                [1*sin_value, cos_value, 0.],
                [0., 0., 1.]
            ]
        )

    else:
        print('axis input should in [\'x\', \'y\', \'z\']')

    return rotation_matrix

def mesh_3x(vert, faces):
    faces_list = faces.tolist()
    vert_list = vert.tolist()
    new_vert = []
    new_face = list()
    new_vert.extend(vert_list)
    start = len(vert_list)
    for face in faces_list:
        new_point = (vert[face[0]] + vert[face[1]] + vert[face[2]]) / 3
        new_vert.append([new_point[0], new_point[1], new_point[2]])
        new_face.append([face[0], face[1], start])
        new_face.append([face[0], start, face[2]])
        new_face.append([start, face[1], face[2]])
        start += 1
    new_verts = np.array(new_vert)
    new_faces = np.array(new_face, dtype=int)

    return new_verts, new_faces

def cal_distance(p1, p2):
    return np.sqrt(np.sum((p1-p2)**2))

if __name__ == "__main__":
    pass


'''
Generating and sampling shapes

u,v \in [0,1]
'''

# TODO Derive each class from PhiFlow Geometry class

from phi.torch.flow import *

# Helper function to sample initial and target position of 2 shapes
# Get O overlapping, and U non-necessarily overlapping ('unique') sample points
def get_points_for_shapes(shape_0, shape_target, O=30, U=30):
    sampler_union = ShapeSampler(shape_0, shape_target, N=O, h1=2, h2=7)
    sampler_0 = ShapeSampler(shape_0, N=U, h1=3, h2=11)
    sampler_target = ShapeSampler(shape_target, N=U, h1=3, h2=11)
    # Concatenate both the union and non-union points
    p_0 = math.concat((sampler_union.p, sampler_0.p), instance('i'))
    p_0 = shape_0.create_points(p_0)
    p_t = math.concat((sampler_union.p, sampler_target.p), instance('i'))
    p_t = shape_target.create_points(p_t)

    return (p_0, p_t)

'''
N: number of sample points
h1, h2: primes for halton sequence
A, B: shapes, of which the subsection is sampled
    if B is None, then only A is sampled
'''
class ShapeSampler:
    def __init__(self, A, B=None, N=16, h1=5, h2=3):
        self.A = A # shape 1
        self.B = B # shape 2
        self.N = N
        self.h1 = h1
        self.h2 = h2

        self.p = []

        # generate N points in [0,1]x[0,1]
        # Check wheter (u,v) is inside the shape
        # if not, then throw it away, and try another one
        for u, v in zip(self.halton(self.N, h1), self.halton(self.N, h2)):
            if A.sdf(u,v) < 0.0:
                if B == None or B.sdf(u,v) < 0.0:
                    # (u, v) is both in A and B (or either B is not present)
                    self.p.append([u,v])
            if len(self.p) == self.N:
                break
        self.p = math.tensor(self.p,
                            instance(i=N) & channel(vector='x,y'))

    def get_sample_points(self):
        return self.p

    # Generates the b base Halton-sequence
    # source: https://en.wikipedia.org/wiki/Halton_sequence
    def halton(self, n, b):
        n, d = 0, 1
        while True:
            x = d - n
            if x == 1:
                n = 1
                d *= b
            else:
                y = d // b
                while x <= y:
                    y //= b
                n = (b + 1) * y - x
            yield n / d

'''
pos: lower left corner of the encompassing rectangle
size: size by which to scale encompassing rectangle
'''
class Circle:
    def __init__(self, pos=(1,1), size=1):
        self.pos = pos
        self.size = size
        self.radius = size/2

    '''
    (u, v): sample point in [0,1]x[0,1]
    return: SDF(u,v) which is
        - > 0 outside the shape
        - = 0 on the border of the shape
        - < 0 inside the shape
        with a sphere r=0.5, centered at o=[0.5, 0.5]
    '''
    def sdf(self, u, v):
        o = [0.5, 0.5]
        r = 0.5
        # distance from center of the circle
        dist = math.sqrt((u-o[0])**2 + (v-o[1])**2)
        return dist - r

    def create_points(self, p_sample):
        self.p = self.f(p_sample)
        return self.p

    def f(self, p):
        scale = tensor([self.size, self.size], channel(p))
        tx, ty = self.pos
        translate = tensor([tx, ty], channel(p))
        return (p * scale) + translate

    def get_smoke(self, domain=Box(x=math.PI, y=math.PI), x=100, y=100):
        r = self.radius
        center = tensor([self.pos[0]+r, self.pos[1]+r], channel(vector='x,y'))
        smoke = CenteredGrid(
            Sphere(center=center, radius=r),
            extrapolation.BOUNDARY,
            x=x, y=y,
            bounds=(domain)
        )

        return smoke

    def get_trivial_points(self):
        return math.tensor([
            [0.5, 0.5], # center
            [0.885, 0.885], # upper right
            [0.115, 0.885], # upper left
            [0.115, 0.115], # lower left
            [0.885, 0.115], # lower right
        ], instance(i=5) & channel(vector='x,y'))

'''
pos: (x,y) position of lower left corner
size: side length of the square
'''
class Square:
    def __init__(self, pos=(1,1), size=1):
        self.pos  = pos
        self.size = size

    '''
    (u, v): sample point in [0,1]x[0,1]
    return: SDF(u,v) which is
        - > 0 outside the shape
        - = 0 on the border of the shape
        - < 0 inside the shape
        with a sphere r=0.5, centered at o=[0.5, 0.5]
    '''
    def sdf(self, u, v):
        # All of the unit square is inside
        return -1

    def create_points(self, p_sample):
        self.p = self.f(p_sample)
        return self.p

    def f(self, p):
        scale = tensor([self.size, self.size], channel(p))
        tx, ty = self.pos
        translate = tensor([tx, ty], channel(p))
        return (p * scale) + translate

    def get_smoke(self, domain=Box(x=math.PI, y=math.PI), x=100, y=100):
        lower = tensor([self.pos[0], self.pos[1]], channel(vector='x,y'))
        upper = tensor([self.pos[0]+self.size, self.pos[1]+self.size], channel(vector='x,y'))
        smoke = CenteredGrid(
            Box(lower=lower,
            upper=upper),
            extrapolation.BOUNDARY,
            x=x, y=y,
            bounds=(domain)
        )

        return smoke

    def get_trivial_points(self,):
        return math.tensor([
            [0.5, 0.5], # center
            [1, 1], # upper right
            [0, 1], # upper left
            [0, 0], # lower left
            [1, 0], # lower right
        ], instance(i=5) & channel(vector='x,y'))

'''
pos: lower left corner of the encompassing rectangle
'''
class Triangle():
    def __init__(self, pos=(1,1), size=1.0):
        self.pos   = pos
        self.size = size

    '''
    (u, v): sample point in [0,1]x[0,1]
    return: SDF(u,v) which is
        - > 0 outside the shape
        - = 0 on the border of the shape
        - < 0 inside the shape
        with a sphere r=0.5, centered at o=[0.5, 0.5]
    '''
    def sdf(self, u, v):
        # needs to be under both the left and right sides
        y_left  = +2*u
        y_right = -2*u + 2
        if v < y_left and v < y_right and v > 0:
            return -1 #inside triangle
        return 1 #outside triangle

    def create_points(self, p_sample):
        self.p = self.f(p_sample)
        return self.p

    def f(self, p):
        scale = tensor([self.size, self.size], channel(p))
        tx, ty = self.pos
        translate = tensor([tx, ty], channel(p))
        return (p * scale) + translate

    def get_smoke(self,domain=Box(x=math.PI, y=math.PI), x=100, y=100):
        smoke = CenteredGrid(
            self.get_marker,
            extrapolation.BOUNDARY,
            x=x, y=y,
            bounds=(domain)
        )
        return smoke

    def get_marker(self, p):
        u, v = p.vector['x'], p.vector['y']
        y_left  = +2*(u-self.pos[0]) + self.pos[1]
        y_right = -2*(u-self.pos[0]) + 2 + self.pos[1]
        bool_inside = (v < y_left) & (v < y_right) & (v > self.pos[1])
        bool_inside = math.all(bool_inside, 'vector')

        return bool_inside

    def get_trivial_points(self):
        return math.tensor([
            [0.5, 0.5], # center
            [0.5, 1], # upper right
            [0.5, 1], # upper left
            [0, 0], # lower left
            [1, 0], # lower right
        ], instance(i=5) & channel(vector='x,y'))

# TODO
def get_f_moon():
    return

# TODO BME logo


import argparse
import numpy as np
import torch
from copy import deepcopy

from gluonts.dataset.multivariate_grouper import MultivariateGrouper
from gluonts.dataset.repository.datasets import get_dataset
from gluonts.evaluation.backtest import make_evaluation_predictions
from gluonts.evaluation import MultivariateEvaluator

from tsdiff.forecasting.models import (
    ScoreEstimator,
    TimeGradTrainingNetwork_AutoregressiveOld, TimeGradPredictionNetwork_AutoregressiveOld,
    TimeGradTrainingNetwork_Autoregressive, TimeGradPredictionNetwork_Autoregressive,
    TimeGradTrainingNetwork_All, TimeGradPredictionNetwork_All,
    TimeGradTrainingNetwork_RNN, TimeGradPredictionNetwork_RNN,
    TimeGradTrainingNetwork_Transformer, TimeGradPredictionNetwork_Transformer,
    TimeGradTrainingNetwork_CNN, TimeGradPredictionNetwork_CNN,
)
from tsdiff.utils import NotSupportedModelNoiseCombination, TrainerForecasting

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

def energy_score(forecast, target):
    obs_dist = np.mean(np.linalg.norm((forecast - target), axis=-1))
    pair_dist = np.mean(
        np.linalg.norm(forecast[:, np.newaxis, ...] - forecast, axis=-1)
    )
    return obs_dist - pair_dist * 0.5

def train(
    seed: int,
    dataset: str,
    network: str,
    noise: str,
    diffusion_steps: int,
    epochs: int,
    learning_rate: float,
    batch_size: int,
    num_cells: int,
    hidden_dim: int,
    residual_layers: int,
):
    np.random.seed(seed)
    torch.manual_seed(seed)

    covariance_dim = 4 if dataset != 'exchange_rate_nips' else -4

    # Load data
    dataset = get_dataset(dataset, regenerate=False)

    target_dim = int(dataset.metadata.feat_static_cat[0].cardinality)

    train_grouper = MultivariateGrouper(max_target_dim=min(2000, target_dim))
    test_grouper = MultivariateGrouper(num_test_dates=int(len(dataset.test) / len(dataset.train)), max_target_dim=min(2000, target_dim))
    dataset_train = train_grouper(dataset.train)
    dataset_test = test_grouper(dataset.test)

    val_window = 20 * dataset.metadata.prediction_length
    dataset_train = list(dataset_train)
    dataset_val = []
    for i in range(len(dataset_train)):
        x = deepcopy(dataset_train[i])
        x['target'] = x['target'][:,-val_window:]
        dataset_val.append(x)
        dataset_train[i]['target'] = dataset_train[i]['target'][:,:-val_window]

    # Load model
    if network == 'timegrad':
        if noise != 'normal':
            raise NotSupportedModelNoiseCombination
        training_net, prediction_net = TimeGradTrainingNetwork_Autoregressive, TimeGradPredictionNetwork_Autoregressive
    elif network == 'timegrad_old':
        if noise != 'normal':
            raise NotSupportedModelNoiseCombination
        training_net, prediction_net = TimeGradTrainingNetwork_AutoregressiveOld, TimeGradPredictionNetwork_AutoregressiveOld
    elif network == 'timegrad_all':
        training_net, prediction_net = TimeGradTrainingNetwork_All, TimeGradPredictionNetwork_All
    elif network == 'timegrad_rnn':
        training_net, prediction_net = TimeGradTrainingNetwork_RNN, TimeGradPredictionNetwork_RNN
    elif network == 'timegrad_transformer':
        training_net, prediction_net = TimeGradTrainingNetwork_Transformer, TimeGradPredictionNetwork_Transformer
    elif network == 'timegrad_cnn':
        training_net, prediction_net = TimeGradTrainingNetwork_CNN, TimeGradPredictionNetwork_CNN

    estimator = ScoreEstimator(
        training_net=training_net,
        prediction_net=prediction_net,
        noise=noise,
        target_dim=target_dim,
        prediction_length=dataset.metadata.prediction_length,
        context_length=dataset.metadata.prediction_length,
        cell_type='GRU',
        num_cells=num_cells,
        hidden_dim=hidden_dim,
        residual_layers=residual_layers,
        input_size=target_dim * 4 + covariance_dim,
        freq=dataset.metadata.freq,
        loss_type='l2',
        scaling=True,
        diff_steps=diffusion_steps,
        beta_end=20 / diffusion_steps,
        beta_schedule='linear',
        num_parallel_samples=100,
        pick_incomplete=True,
        trainer=TrainerForecasting(
            device=device,
            epochs=epochs,
            learning_rate=learning_rate,
            num_batches_per_epoch=100,
            batch_size=batch_size,
            patience=10,
        ),
    )

    # Training
    predictor = estimator.train(dataset_train, dataset_val, num_workers=8)

    # Evaluation
    forecast_it, ts_it = make_evaluation_predictions(dataset=dataset_test, predictor=predictor, num_samples=100)
    forecasts = list(forecast_it)
    targets = list(ts_it)

    score = energy_score(
        forecast=np.array([x.samples for x in forecasts]),
        target=np.array([x[-dataset.metadata.prediction_length:] for x in targets])[:,None,...],
    )

    evaluator = MultivariateEvaluator(quantiles=(np.arange(20)/20.0)[1:], target_agg_funcs={'sum': np.sum})
    agg_metric, _ = evaluator(targets, forecasts, num_series=len(dataset_test))

    metrics = dict(
        CRPS=agg_metric['mean_wQuantileLoss'],
        ND=agg_metric['ND'],
        NRMSE=agg_metric['NRMSE'],
        CRPS_sum=agg_metric['m_sum_mean_wQuantileLoss'],
        ND_sum=agg_metric['m_sum_ND'],
        NRMSE_sum=agg_metric['m_sum_NRMSE'],
        energy_score=score,
    )
    metrics = { k: float(v) for k,v in metrics.items() }

    return metrics

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Train forecasting model.')
    parser.add_argument('--seed', type=int, default=1)
    parser.add_argument('--dataset', type=str)
    parser.add_argument('--network', type=str, choices=[
        'timegrad', 'timegrad_old', 'timegrad_all', 'timegrad_rnn', 'timegrad_transformer', 'timegrad_cnn'
    ])
    parser.add_argument('--noise', type=str, choices=['normal', 'ou', 'gp'])
    parser.add_argument('--diffusion_steps', type=int, default=100)
    parser.add_argument('--epochs', type=int, default=100)
    parser.add_argument('--learning_rate', type=int, default=1e-3)
    parser.add_argument('--batch_size', type=int, default=64)
    parser.add_argument('--num_cells', type=int, default=100)
    parser.add_argument('--hidden_dim', type=int, default=100)
    parser.add_argument('--residual_layers', type=int, default=8)
    args = parser.parse_args()

    metrics = train(**args.__dict__)

    for key, value in metrics.items():
        print(f'{key}:\t{value:.4f}')

# Example:
# python -m tsdiff.forecasting.train --seed 1 --dataset electricity_nips --network timegrad_rnn --noise ou --epochs 100


from trytond.model import ModelView, ModelSQL, fields
from trytond.pool import Pool

from sql import Table, Column, Literal, Desc, Asc, Expression, Flavor
from sql.functions import Now, Extract
from sql.operators import Or, And, Concat, ILike, Operator
from sql.conditionals import Coalesce
from sql.aggregate import Count, Max

from nereid import (
    request, abort, render_template, login_required, url_for, flash, jsonify,
    current_app, route
)
from nereid.contrib.pagination import Pagination, BasePagination
from nereid.ctx import has_request_context

__all__ = ['Artist']

class Artist(ModelSQL, ModelView):
    "Artist"
    __name__ = "songbook.artist"
    _rec_name = 'full_name'

    last_name = fields.Char('Last Name', required=True, select=True)
    first_name = fields.Char('First Name', select=True)
    full_name = fields.Function(
        fields.Char('Full Name'), 'get_full_name',
        searcher='search_full_name'
    )
    rev_name = fields.Function(
        fields.Char('Reversed Name'), 'get_rev_name'
    )
    songs = fields.One2Many(
        "songbook.song",
        'artist',
        'Songs by This Artist'
    )

    def serialize(self):
        """
        Serialize the artist object and return a dictionary.
        """
        object_json = {
            "url": url_for(
                'songbook.artist.render_html',
                id=self.id,
            ),
            "objectType": self.__name__,
            "id": self.id,
            "lastName": self.last_name,
            "firstName": self.first_name,
            "fullName": self.full_name,
        }
        return object_json

    @classmethod
    def __setup__(cls):
        super(Artist, cls).__setup__()
        cls._sql_constraints = [
            ('name_uniq', 'UNIQUE(last_name, first_name)',
             'An artist with that name already exists.')
        ]
        cls._order.insert(0, ('last_name', 'ASC'))
        cls._order.insert(1, ('first_name', 'ASC'))

    @classmethod
    def search_full_name(cls, name, clause):
        "Search Full Name"
        _, operator, value = clause
        Operator = fields.SQL_OPERATORS[operator]
        table = cls.__table__()
        fullname = Concat(
            Coalesce(table.first_name, Literal('')),
            Concat(
                Literal(' '),
                Coalesce(table.last_name, Literal(''))
            )
        )

        query = table.select(table.id, where=Operator(fullname, value))
        return [('id', 'in', query)]

    def get_full_name(self, name):
        if self.first_name is None:
            fullname = self.last_name
        else:
            fullname = "%s %s" % (self.first_name, self.last_name)
        return fullname.strip(" ")

    def get_rev_name(self, name):
        if self.first_name is None:
            revname = self.last_name
        else:
            revname = "%s, %s" % (self.last_name, self.first_name)
        return revname.strip(", ")

    @classmethod
    @route('/songbook/api/artists', methods=['GET', 'POST'])
    def call_api_index(cls):
        """
        JSON-formatted REST API to support 3rd party integration, apps
        and web page javascript such as search-as-you-type.
        """
        name_filter = '%' + request.args.get('namecontains', '') + '%'
        domain = [
            ('full_name', 'ilike', name_filter)
        ]
        artists = cls.search(domain, limit=int(request.args.get('limit', '5')))
        return jsonify(
            artists=[a.serialize() for a in artists]
        )

    @classmethod
    @route('/songbook/artists/<int:id>', methods=['GET'])
    def render_html(cls, id=0):
        """
        output details of a selected artist to web client
        """
        artist=cls.browse([id])[0]
        return render_template(
            'songbook_artist-detail.jinja',
            artist=artist
        )

    @classmethod
    @route('/songbook/artists', methods=['GET', 'POST'])
    def render_html_index(cls):
        """
        output artist list to web client
        """
        name_filter = '%' + request.args.get('namecontains', '') + '%'
        page = request.args.get('page', 1, int)
        domain = [
            ('full_name', 'ilike', name_filter)
        ]
        artists = Pagination(
            cls, domain, page, 25
        )

        return render_template(
            'songbook_artist-list.jinja',
            artists=artists
        )


from bert_models.base_bert_model import BaseBertModel
import joblib
from sklearn.ensemble import GradientBoostingClassifier
import os
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.metrics import f1_score

class BaseBertModelWithBoost(BaseBertModel):

    def __init__(self, output_dir, label_list, boost_model = "", gpu_device_num_hub=0,gpu_device_num = 1, batch_size = 16, max_seq_length = 256,\
        bert_model_hub = "https://tfhub.dev/google/bert_uncased_L-12_H-768_A-12/1", model_folder = "", label_column = "label",
        use_concat_results=False):
        BaseBertModel.__init__(self, output_dir, label_list, gpu_device_num_hub=gpu_device_num_hub,
            gpu_device_num = gpu_device_num, batch_size = batch_size, max_seq_length = max_seq_length,
            bert_model_hub = bert_model_hub, model_folder = model_folder, label_column = label_column,
            use_concat_results = use_concat_results)
        self.load_boost_model(boost_model)

    def load_boost_model(self, folder):
        self.sgBoost = joblib.load(os.path.join(folder,'GradientBoost.joblib')) if folder != "" else GradientBoostingClassifier()

    def save_boost_model(self, folder):
        if not os.path.exists(folder):
            os.makedirs(folder)
        joblib.dump(self.sgBoost, os.path.join(folder,'GradientBoost.joblib'))

    def prepare_dataset_for_boosting(self, train, use_tail = False):
        es_train_prob_study_type_11, res_train_full_study_type_11, train_y_study_type_11 = self.evaluate_model(train)
        if use_tail:
            es_train_prob_study_type_11_tail, res_train_full_study_type_11_tail, train_y_study_type_11_tail = self.evaluate_model(train, False)
            sg_boost_x = np.concatenate([ es_train_prob_study_type_11, es_train_prob_study_type_11_tail],axis=1)
            return sg_boost_x
        return es_train_prob_study_type_11

    def prepare_datasets_for_boosting(self, train, test, study_df, use_tail = False):
        sg_boost_x = self.prepare_dataset_for_boosting(train, use_tail = use_tail)
        sg_boost_test_x = self.prepare_dataset_for_boosting(test, use_tail = use_tail)
        sg_boost_study_x = self.prepare_dataset_for_boosting(study_df, use_tail = use_tail)
        return sg_boost_x, sg_boost_test_x, sg_boost_study_x

    def train_boost_model(self, train, test, study_df, use_tail = False, n_estimators = 60, max_depth = 8, for_train = True):
        sg_boost_x, sg_boost_test_x, sg_boost_study_x = self.prepare_datasets_for_boosting(train, test, study_df, use_tail = use_tail)
        train_y = list(train[self.label_column].values)
        test_y = list(test[self.label_column].values)
        study_df_y = list(study_df[self.label_column].values)
        if for_train:
            self.sgBoost = GradientBoostingClassifier(n_estimators = n_estimators, max_depth=max_depth)
            self.sgBoost.fit(sg_boost_test_x, test_y)
        print(self.sgBoost.score(sg_boost_test_x, test_y))
        print(self.sgBoost.score(sg_boost_x, train_y))
        print(self.sgBoost.score(sg_boost_study_x, study_df_y))
        print(confusion_matrix(study_df_y, self.sgBoost.predict(sg_boost_study_x)))
        print(classification_report(study_df_y, self.sgBoost.predict(sg_boost_study_x)))

    def evaluate_boost_model(self, test, use_tail = False):
        sg_boost_test_x =self.prepare_dataset_for_boosting(test, use_tail = use_tail)
        test_y = list(test[self.label_column].values)
        print(self.sgBoost.score(sg_boost_test_x, test_y))
        print(confusion_matrix(test_y, self.sgBoost.predict(sg_boost_test_x)))
        print(classification_report(test_y, self.sgBoost.predict(sg_boost_test_x)))

    def predict_with_boosting(self, df, with_head_tail = False):
        res_prob, res_label, res_y = self.predict_for_df(df)
        if with_head_tail:
            res_prob_tail, res_label_tail, res_y = self.predict_for_df(df, is_head = False)
            res_prob = np.concatenate([ res_prob, res_prob_tail],axis=1)
        res_prob, res_label = [self.softmax(x) for x in self.sgBoost.decision_function(res_prob)], self.sgBoost.predict(res_prob)
        return res_prob, res_label, res_y


"""
@author: Miguel Taibo Martínez

Date: Nov 2021
"""

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

import gpflow
import pandas as pd
import sobol_seq

from gpflow.utilities import print_summary
from frontutils import get_pareto_undominated_by

class frontGP(object):
    def __init__(self, O:int, C:int, d:int, lowerBounds: float, upperBounds: float, X = None, Y = None, noise_variance=0.01):
        self.O = O
        self.C = C
        self.d = d
        self.lowerBounds = lowerBounds
        self.upperBounds = upperBounds
        self.X = X
        self.Y = Y
        self.noise_variance = noise_variance
        self.multiGPR : MultiGPR = None

    def addSample(self, x, y, save=False, filename=None):
        if self.X is None or self.Y is None:
            self.X = np.array([x])
            self.Y = np.array([y])
            return
        self.X = np.append(self.X, [x], axis=0)
        self.Y = np.append(self.Y, [y], axis=0)
        if save and filename is not None:
            self.writeSample(filename, x,y)

    def updateGP(self):
        self.multiGPR = MultiGPR(X = self.X, Y = self.Y, noise_variance = self.noise_variance)

    def optimizeKernel(self):
        self.multiGPR.optimizeKernel()

    ## Visualization methods
    def plot(self):

        fig, axs = plt.subplots(nrows = self.O, ncols=self.d, figsize=(10,5))

        if self.d >1:
            for j in range(self.d):
                grid = np.ones((10_000,self.d))
                for k in range(self.d):
                    grid[:,k]=grid[:,k]*(self.upperBounds[k]+self.lowerBounds[k])/2
                xx = np.linspace(self.lowerBounds[j], self.upperBounds[j], 10_000).reshape(10_000, 1)
                grid[:,j]=xx[:,0]
                mean, var = self.multiGPR.predict_y(grid)

                if self.O==1:
                    axs[j].plot(self.X[:,j], self.Y[:,0], 'kx', mew=2)
                    axs[j].plot(grid[:,j], mean[:,0], 'C0', lw=2)
                    axs[j].fill_between(grid[:,j],
                                    mean[:,0] - 2*np.sqrt(var[:,0]),
                                    mean[:,0] + 2*np.sqrt(var[:,0]),
                                    color='C0', alpha=0.2)
                else:
                    for i in range(self.O):
                        axs[i, j].plot(self.X[:,j], self.Y[:,i], 'kx', mew=2)
                        axs[i, j].plot(grid[:,j], mean[:,i], 'C0', lw=2)
                        axs[i, j].fill_between(grid[:,j],
                                        mean[:,i] - 2*np.sqrt(var[:,i]),
                                        mean[:,i] + 2*np.sqrt(var[:,i]),
                                        color='C0', alpha=0.2)

        else:
            xx = np.linspace(self.lowerBounds[0], self.upperBounds[0], 10_000).reshape(10_000, 1)
            mean, var = self.multiGPR.predict_y(xx)
            if self.O==1:
                axs.plot(self.X, self.Y[:,0], 'kx', mew=2)
                axs.plot(xx[:,0], mean[:,0], 'C0', lw=2)
                axs.fill_between(xx[:,0],
                                mean[:,0] - 2*np.sqrt(var[:,0]),
                                mean[:,0] + 2*np.sqrt(var[:,0]),
                                color='C0', alpha=0.2)
            else:
                for i in range(self.O):
                    axs[i].plot(self.X, self.Y[:,i], 'kx', mew=2)
                    axs[i].plot(xx[:,0], mean[:,i], 'C0', lw=2)
                    axs[i].fill_between(xx[:,0],
                                    mean[:,i] - 2*np.sqrt(var[:,i]),
                                    mean[:,i] + 2*np.sqrt(var[:,i]),
                                    color='C0', alpha=0.2)

        return fig, axs

    def plotParetos(self, state):

        pareto_front = np.array(state.pareto_front)
        pareto_set = np.array(state.pareto_set)

        for idx, mm in enumerate(state.objective_mms):
            if mm:
                pareto_front[:,idx]=-pareto_front[:,idx]

        best_known_pareto_front = get_pareto_undominated_by(self.Y)
        best_known_pareto_set = getSetfromFront(self.X, self.Y, best_known_pareto_front)

        fig1, axs1 = plt.subplots(figsize=(8,8))
        if self.d>1:
            axs1.plot(pareto_set[:,state.input_names.index(state.setx)],pareto_set[:,state.input_names.index(state.sety)], 'bx', markersize=3, label=r"Estimated Pareto Set")
            axs1.plot(best_known_pareto_set[:,state.input_names.index(state.setx)], best_known_pareto_set[:,state.input_names.index(state.sety)], 'gx', markersize=10, label=r"Best Known Pareto Set")
            axs1.set_ylabel(state.sety, fontsize=14)
            axs1.set_xlabel(state.setx, fontsize=14)
        else:
            axs1.plot(pareto_set[:,0], [0 for _ in pareto_set[:,0]],'bx',  markersize=3, label=r"Estimated Pareto Set")
            axs1.plot(best_known_pareto_set[:,0], [0 for _ in best_known_pareto_set[:,0]],'gx', markersize=10, label=r"Best Known Pareto Set")
            axs1.set_xlabel(state.input_names[0], fontsize=14)
            axs1.set_yticks(ticks = [])
        axs1.legend(fontsize=14)

        fig2, axs2 = plt.subplots(figsize=(8,8))
        axs2.plot(pareto_front[:,state.objective_names.index(state.frontx)], pareto_front[:,state.objective_names.index(state.fronty)], 'xb', markersize=3, label=r"Estimated Pareto Front")
        axs2.plot(best_known_pareto_front[:,state.objective_names.index(state.frontx)],best_known_pareto_front[:,state.objective_names.index(state.fronty)], 'xg', markersize=10, label=r"Best Known Pareto Front")
        axs2.set_xlabel(state.frontx, fontsize=14)
        axs2.set_ylabel(state.fronty, fontsize=14)
        axs2.legend(fontsize=14)

        return fig1,fig2

    def plotMetrics(self, state):
        fig, axs = plt.subplots(figsize=(8,8))
        axs.plot(state.ns, state.agd, label=r"$AGD_1(\mathcal{Y}_E^*, \mathcal{Y}_{BK}^*)$")
        axs.plot(state.ns, state.adh, label=r"$d_{ADH}(\mathcal{Y}_E^*, \mathcal{Y}_{BK}^*)$")
        axs.set_ylabel("Log Metrics",fontsize=14)
        axs.set_xlabel("Algorithm Iteration",fontsize=14)
        axs.legend(fontsize=14)
        return fig

    def dlParetos(self, state):

        pareto_front = np.array(state.pareto_front)
        pareto_set = np.array(state.pareto_set)

        df =pd.DataFrame(data = np.append(pareto_set,pareto_front,axis=1),columns=state.input_names+state.objective_names)
        return df.to_csv()

    def dlParetoBestKnown(self, state):
        pareto_front = get_pareto_undominated_by(self.Y)
        pareto_set = getSetfromFront(self.X, self.Y, pareto_front)
        df =pd.DataFrame(data = np.append(pareto_set,pareto_front,axis=1),columns=state.input_names+state.objective_names)
        return df.to_csv()

class MultiGPR(object):
    def __init__(self, X = None, Y = None, noise_variance=0.01):
        self.GPRs = [
            gpflow.models.GPR(
                [X, Y[:,i:i+1]],
                kernel = gpflow.kernels.SquaredExponential(),
                mean_function = gpflow.mean_functions.Constant(),
                noise_variance = noise_variance
            )
            for i in range(Y.shape[-1])
        ]
        self.opt = gpflow.optimizers.Scipy()

    def optimizeKernel(self):
        for GPR in self.GPRs:
            self.opt.minimize(
                GPR.training_loss,
                variables=GPR.trainable_variables)

    def predict_y(self, xx):

        mean_vars = tf.concat([GPR.predict_y(xx) for GPR in self.GPRs], axis=-1)
        mean = mean_vars[0]
        var = mean_vars[1]
        return mean, var

    def predict_f_samples(self, xx, n_samples):
        presamples = [GPR.predict_f_samples(xx, n_samples) for GPR in self.GPRs]
        samples = tf.concat(presamples[:], axis=-1)
        return samples

    def printGPRs(self):
        for GPR in self.GPRs:
            print_summary(GPR)

def getSetfromFront(xvalues, yvalues, front):
    res = None
    for y in front:
        x = xvalues[np.where(np.all(yvalues==y,axis=1))[0]]
        if res is None:
            res = np.array(x)
        else:
            res = np.append(res,x, axis=0)

    return res


import os
import argparse
import gzip
import sys
import time
import numpy as np
from multiprocessing import Pool
from contextlib import closing
import csv
import tensorflow as tf
from six.moves import urllib

bin_freq = 23
spect_width = bin_freq  # Don't add one pixel of zeros on either side of the image
window_size = 100
dim_Y = 11
MEAN_SPEC = 10.786225977

# For MNIST
SOURCE_URL = 'http://yann.lecun.com/exdb/mnist/'

# MNIST embeddings directory
dataMnistdir = '../mnistAct/'
MNIST_DIM = 512

WORK_DIRECTORY = 'data'
IMAGE_SIZE = 28
NUM_CHANNELS = 1
PIXEL_DEPTH = 255

# mnist_data = {digit: np.loadtxt(dataMnistdir + str(digit) + ".txt") for digit in range(10)}

label_map = {'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'o':0,'z':0}

def maybe_download(filename):
    """Download the data from Yann's website, unless it's already here."""
    if not tf.gfile.Exists(WORK_DIRECTORY):
        tf.gfile.MakeDirs(WORK_DIRECTORY)
    filepath = os.path.join(WORK_DIRECTORY, filename)
    if not tf.gfile.Exists(filepath):
        filepath, _ = urllib.request.urlretrieve(SOURCE_URL + filename, filepath)
        with tf.gfile.GFile(filepath) as f:
            size = f.size()
        print('Successfully downloaded', filename, size, 'bytes.')
    return filepath

def extract_data(filename, num_images):
    """Extract the images into a 4D tensor [image index, y, x, channels].
    Values are rescaled from [0, 255] down to [-0.5, 0.5].
    """
    print('Extracting', filename)
    with gzip.open(filename) as bytestream:
        bytestream.read(16)
        buf = bytestream.read(IMAGE_SIZE * IMAGE_SIZE * num_images * NUM_CHANNELS)
        data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
        data = (data - (PIXEL_DEPTH / 2.0)) / PIXEL_DEPTH
        data = data.reshape(num_images, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS)
        return data

def extract_labels(filename, num_images):
    """Extract the labels into a vector of int64 label IDs."""
    print('Extracting', filename)
    with gzip.open(filename) as bytestream:
        bytestream.read(8)
        buf = bytestream.read(1 * num_images)
        labels = np.frombuffer(buf, dtype=np.uint8).astype(np.int64)
    return labels

# Organizing MNIST data into mnist images and labels
test_data_filename = maybe_download('t10k-images-idx3-ubyte.gz')
test_labels_filename = maybe_download('t10k-labels-idx1-ubyte.gz')

# Extract it into numpy arrays.
mnist_values = extract_data(test_data_filename, 10000)
mnist_labels = extract_labels(test_labels_filename, 10000)

mnist_data = {i:[] for i in range(10)}
for i in range(len(mnist_labels)):
    lab = mnist_labels[i]
    val = mnist_values[i]
    mnist_data[lab].append(val)

print(mnist_data[7][0].shape)

def load_from_file(f):
    '''Given a file, returns a list of the string values in that value'''
    data = []
    for line in f:
        vector = []
        line = line.replace("[", "")
        line = line.replace("]", "")
        line_chars = line.split()
        for char in line_chars:
            # vector.append(float(char)-MEAN_SPEC)
            vector.append(float(char))
        try:
            assert len(vector) == bin_freq
            data.append(vector)
        except AssertionError:
            if len(vector) == 0:
                pass
            else:
                # print len(vector)
                raise AssertionError

    # Now we have a list of length-23 vectors which we need to trim/pad to
    # window_size
    if len(data)>window_size:
        #cut excess rows
        cut = 1.*(len(data) - window_size)
        data = data[int(np.floor(cut/2)):-int(np.ceil(cut/2))]
    else:
        # pad data with excess rows of zeros about center
        cut = 1.*(window_size - len(data))
        data = [[0]*bin_freq]*int(np.floor(cut/2)) + data + [[0]*bin_freq]*int(np.ceil(cut/2))
    #Convert data to a numpy array and invert it
    data = np.flipud(np.array(data,dtype=np.float32))
    # #Pad one pixel of zeros on top and bottom of array
    # zero = np.zeros((bin_freq,))
    # data[0] = zero
    # data[-1] = zero
    return data.flatten().tolist()

def ld(rootdir,target):
    with open(target, 'wb') as datafile:
        writer = csv.writer(datafile)
        for subdir, dirs, files in os.walk(rootdir):
            for filename in files:
                y = filename[3]
                f = open(os.path.join(subdir, filename))
                row = load_from_file(f)
                f.close()
                writer.writerow([y] + row)

def get_mnist_embedding(label):
    digit = label[1]
    data = mnist_data[digit]
    i = np.random.randint(0,len(data))
    return label[0],data[i]

def get_label_map(s):
    return label_map[s]

def get_mismatch_mnist_embedding(label):
    i = label[1]
    out = None
    for j in range(10):
        if i != j:
            _, data = get_mnist_embedding((label[0],j))
            if out is None:
                out = data
            else:
                out = np.vstack([out, data])
    return label[0],out.flatten()

def generate_mnist_set(labels):
    matches = []
    mismatches = []
    labels = [(i, label_map[labels[i].decode('utf-8')]) for i in range(len(labels))]
    with closing(Pool()) as pool:
        matches = pool.map(get_mnist_embedding, labels)
        mismatches = pool.map(get_mismatch_mnist_embedding, labels)
    matches = np.array([match[1] for match in sorted(matches)])
    mismatches = np.array([mismatch[1] for mismatch in sorted(mismatches)])
    return matches, mismatches.reshape((len(labels),9,-1))


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

from __future__ import absolute_import, unicode_literals, print_function

import unittest
from nose.tools import assert_in, assert_raises

from wechatsogou.const import WechatSogouConst
from wechatsogou.request import WechatSogouRequest

class TestBasicGenSearchArticleURL(unittest.TestCase):
    def test_gen_hot_url(self):
        for hot_index in filter(lambda x: not x.startswith('__'), dir(WechatSogouConst.hot_index)):
            url = WechatSogouRequest.gen_hot_url(hot_index)
            assert_in('http://weixin.sogou.com/wapindex/wap/0612/wap_', url)
            assert_in('0.html', url)

            with assert_raises(AssertionError):
                WechatSogouRequest.gen_hot_url(hot_index, 0)

            for page in range(1, 5):
                url = WechatSogouRequest.gen_hot_url(hot_index, page)
                assert_in('http://weixin.sogou.com/wapindex/wap/0612/wap_', url)
                assert_in('{}.html'.format(page - 1), url)

if __name__ == '__main__':
    unittest.main()


SPACE4 = " "*4
SPACE_SIZE = 4
MAX_CELL_LENGTH = 50
CONTROL_STATEMENTS = 'if,elif,else,while,def,for,try,except'.split(",")
REMOVE_VARS = ["_input_as_print", "_this_func", "_params", "_namespace", "_inputs", "_code"]  # helpers for identifying scope
PRINT_INPUT_DEC = "!@#$@###"
SYMBOL_REPS = {">":"$gt",
            "<":"$lt",
            "!=":"$ineq",
            "==":"$eq",
            "+":"$add",
            "-": "$sub",
            "*": "$mul",
            "/": "$div",
            "%": "$mod",
            "(": "$oprb",
            ")": "$clrb",
            "[": "$opsb",
            "]": "$clsb"}