import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import pandas as pd
import json

# Note: basemap can be pretty complicated to install on windows
def plotlatlong(matlab_ind):
    filename = 'Temple.txt'
    columns = ["temple name", "location","index","lat","long" ]
    temples = pd.read_csv(filename, delimiter='\t', names=columns)

    lat_coords = temples.values[:,3]
    long_coords = temples.values[:,4] # long is negative in the US

    lat_coords = lat_coords[matlab_ind]
    long_coords = long_coords[matlab_ind]

    fig = plt.figure(figsize=(8, 8))
    m = Basemap(projection='lcc', resolution=None,
                width=6E6, height=6E6,
                lat_0=45, lon_0=-100,)
    m.etopo(scale=1.0, alpha=0.5)

    # Map (long, lat) to (x, y) for plotting
    x,y = m(long_coords,lat_coords)
    plt.plot(x, y)
    plt.show()

if __name__ == "__main__":
    experiment_name = "pastResults/endowment2"
    with open('results/'+ experiment_name +'history.json', 'r') as outfile:
        all_history = json.load(outfile)
    with open('results/'+ experiment_name +'fitness.json', 'r') as outfile:
        all_fitness = json.load(outfile)
    with open('results/'+ experiment_name +'time.json', 'r') as outfile:
        all_times = json.load(outfile)
    with open('results/'+ experiment_name +'fopt.json', 'r') as outfile:
        fopts = json.load(outfile)
    with open('results/'+ experiment_name +'xopt.json', 'r') as outfile:
        xopts = json.load(outfile)
    with open('results/'+ experiment_name +'iterations.json', 'r') as outfile:
        all_iterations = json.load(outfile)
    # Plot a run from an experiment
    #to_plot = np.array(all_history[0][300]).astype(int)

    # Plot Optimum from an experiment
    xopts = xopts[0]
    xopts.append(xopts[0])
    to_plot = np.array(xopts).astype(int)
    to_plot = np.subtract(yes,1).astype(int)
    plotlatlong(to_plot)


# Django Imports
from django.core.urlresolvers import reverse

# DC Imports
import dcmembers.display as display
import dcmembers.utilities as utilities
from dcmembers.utilities import plural as pl

'''
Text generation for tasks.
'''

def buildfreighter(amount):
    pluralise = ('%s ' % amount if amount > 1 else '')
    return 'Your %s%s will be ready in' % (pluralise, pl('freighter', amount))

def buildship(shiptype, amount):
    name = utilities.resname(shiptype+10, amount, lower=True)
    return 'Your %s %s will be ready in' % (amount, name)

def buildpersonalship(shiptype):
    name = display.personalshipname(shiptype)
    return 'Your %s will be ready in' % name

def moveship(shiptype, amount, regionfrom, regionto):

    shipname = utilities.resname(shiptype+10, amount, lower=True)

    fromname = display.region_display(regionfrom)
    toname = display.region_display(regionto)

    toreturn = 'Your %(amount)s %(ship)s will warp from %(from)s to %(to)s in' \
        % {'amount':amount, 'ship':shipname, 'from':fromname, 'to':toname}

    return toreturn, shipname

def movepersonalship(regionfrom, regionto):

    fromname = display.region_display(regionfrom)
    toname = display.region_display(regionto)

    toreturn = 'Your personal ship will warp from %(from)s to %(to)s in' \
        % {'from':fromname, 'to':toname}

    return toreturn

def movefreighter(amount, regionfrom, regionto):

    fromname = display.region_display(regionfrom)
    toname = display.region_display(regionto)

    return 'Your %(amount)s %(pl)s will warp from %(from)s to %(to)s in' \
        % {'amount':amount, 'pl':pl('freighter', amount), 'from':fromname, 'to':toname}

def mothball(shiptype, amount, direction):

    shipname = utilities.resname(shiptype+10, amount, lower=True)
    pluralise = ('is' if amount == 1 else 'are')

    if direction == 'plus':
        message = 'Your %s %s %s decommissioning, and will enter the orbital hangars in' % (amount, shipname, pluralise)
    elif direction == 'minus':
        message = 'Your %s %s %s preparing to resume service, and will be ready in' % (amount, shipname, pluralise)

    return message, shipname

def directaidarrival(world, resname, amount):
    linkworld = reverse('stats_ind', args=(world.worldid,))
    fullworld = '%(world)s' % {'link':linkworld,'world':world.world_name}

    return 'We are being sent %(amount)s %(name)s by %(world)s! The shipment will arrive in' \
        % {'amount':amount, 'name':resname, 'world':fullworld}

def tradeaccepterarrival(world, resname, amount):
    linkworld = reverse('stats_ind', args=(world.worldid,))
    fullworld = '%(world)s' % {'link':linkworld,'world':world.world_name}

    return 'The %(amount)s %(name)s from our trade with %(world)s will arrive in' \
        % {'amount':amount, 'name':resname, 'world':fullworld}

def tradeownerarrival(world, resname, amount):
    linkworld = reverse('stats_ind', args=(world.worldid,))
    fullworld = '%(world)s' % {'link':linkworld,'world':world.world_name}

    return '%(world)s has accepted our trade! The %(amount)s %(name)s will arrive in' \
        % {'amount':amount, 'name':resname, 'world':fullworld}


import cv2
from model import LSCCNN
from download_from_google import download_file_from_google_drive
import os
import gradio as gr
from time import time
if not os.path.exists("weights/weights.pth"):
    os.mkdir("weights")
    output = 'weights/weights.pth'
    file_id = '1QbPwRXcrONMuBL_39gvnvGGsFCnNyjEm'
    download_file_from_google_drive(file_id, output)

checkpoint_path = './weights/weights.pth'
network = LSCCNN(checkpoint_path=checkpoint_path)
network.cuda()
network.eval()
emoji = cv2.imread("images/blm_fist.png", -1)

def predict(img):
    if img.shape[2] > 3:
        img = img[:, :, :3]
    pred_dot_map, pred_box_map, img_out = \
        network.predict_single_image(img, emoji, nms_thresh=0.25)
    return img_out

thumbnail="https://i.ibb.co/bzwSBzw/Screen-Shot-2020-08-24-at-7-05-36-AM.png"
examples=[
    ["images/1.png"],
    ["images/2.png"],
    ["images/3.png"],
    ["images/4.png"],
    ["images/5.png"]
]

gr.Interface(predict, "image", "image", title="BLM Photo "
                                              "Anonymization",
             description="Anonymize photos to protect BLM "
                         "protesters. Faces will be covered with the "
                         "black fist emoji. Model developed by the Stanford ML Group and LSC-CNN.", examples=examples,
             thumbnail=thumbnail).launch()


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

# There are some errors of this code

class Solution:
    def __init__(self):
        self.cnt = True # true represents left, false represent right
        self.cnt1 = 0
        self.cnt2 = 0

    def findTarget(self, root, target, cnt):
        if root == None:
            return False, self.cnt1 if cnt else self.cnt2

        if cnt:
            self.cnt1 += 1
        elif not cnt:
            self.cnt2 += 1

        if root.val == target:
            return True
        elif root.val > target:
            self.findTarget(root.left, target, cnt)
        else:
            self.findTarget(root.right, target, cnt)

    def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
        if root == None:
            return False

        # make sure x < y
        if x > y:
            x, y = y, x

        # Judge the interval (binary search)
        if x > root.val:
            self.isCousins(root.right, x, y)
        elif y < root.val:
            self.isCousins(root.left, x, y)

        xBound, cnt1 = self.findTarget(root.left, x, True)
        yBound, cnt2 = self.findTarget(root.right, y, False)

        if xBound and yBound and cnt1==cnt2 and cnt1!=0 and cnt2!=0:
            return True
        else:
            return False


import sys
import logging
import numpy as np
import pandas as pd
import constants as const
from src.yolo_metrics_img import YoloMetricImg

logging.basicConfig(format=const.LOG_FORMAT,
                    level=const.LOG_LEVEL,
                    stream=sys.stdout)

class YoloMetricsImgs:
    def __init__(self,
                 df_pred,
                 df_origin,
                 test_set,
                 classes,
                 model,
                 iou_thr=0.5):
        self._origin = df_origin
        self._pred = df_pred
        self._test_set = test_set
        self._iou_thr = iou_thr
        self._classes = classes
        self._model = model

    def get_metrics_img(self, by_class=None):
        origin = self._origin
        pred = self._pred

        if by_class is not None:
            origin = origin[origin.type_disk == by_class]
            pred = pred[pred.type_disk == by_class]
        metric_verbose_list = []
        metric_list = []
        for img in self._test_set:
            origin_img = origin[origin.file == img]
            pred_img = pred[pred.file == img]

            if origin_img.shape[0] == 0 & \
                    pred_img.shape[0] == 0:
                continue

            metrics_obj = YoloMetricImg(df_img_origin=origin_img,
                                        df_img_pred=pred_img,
                                        iou_thr=self._iou_thr)

            metric_verbose_img, metric_img = metrics_obj.get_metrics()

            metric_verbose_list.append(metric_verbose_img)
            metric_list.append(metric_img)

        metric_verbose = pd.concat(metric_verbose_list,
                                   axis=0,
                                   sort=True)
        metric = pd.concat(metric_list,
                           axis=0,
                           sort=True)

        metric_cum = self.get_prec_recall_cumulative(metric_verbose)

        ap = self.calc_average_precisions(rec=metric_cum.recall,
                                          prec=metric_cum.precision)
        metric['ap'] = ap
        return metric_cum, metric

    def get_metrics_by_class(self, summary=True):
        metrics_verbose_list = []
        metrics_list = []
        summary_df = None

        logging.info('Metrics for model = {}'.format(self._model))

        for class_id in self._classes:
            metric_verbose_class, metrics_class = self.get_metrics_img(by_class=class_id)

            metric_verbose_class['type_disk'] = class_id
            metrics_class['type_disk'] = class_id

            metrics_verbose_list.append(metric_verbose_class)
            metrics_list.append(metrics_class)

        metric_verbose = pd.concat(metrics_verbose_list,
                                   axis=0,
                                   sort=True)
        metric = pd.concat(metrics_list,
                           axis=0,
                           sort=True)

        metric_verbose['model'] = self._model

        if summary:
            logging.info('Create summary')
            summary_df = self.get_summarize(metric=metric)

        return metric_verbose, metric, summary_df

    def get_summarize(self, metric):

        metric_gr = metric.groupby(by='type_disk').agg({'pb': 'sum',
                                                        'ab': 'sum',
                                                        'tp': 'sum',
                                                        'ap': 'mean'}).reset_index()
        metric_all = metric[['pb', 'tp', 'ab']].sum()
        metric_all['ap'] = metric_gr['ap'].mean()
        metric_all['type_disk'] = 'all'
        metric_all = pd.DataFrame(metric_all).transpose()

        metric_gen = pd.concat([metric_gr, metric_all],
                               axis=0,
                               sort=False)

        metric_gen['precision'] = metric_gen.tp / metric_gen.pb
        metric_gen['recall'] = metric_gen.tp / metric_gen.ab
        metric_gen['f1'] = 2 * metric_gen.precision * metric_gen.recall / \
                           (metric_gen.precision + metric_gen.recall)
        metric_gen['model'] = self._model

        return metric_gen

    @staticmethod
    def get_prec_recall_cumulative(metric):
        metric = metric.sort_values(by='iou',
                                    ascending=False)
        metric['acc_tp'] = metric.tp.cumsum(axis=0)
        metric = metric.reset_index()

        metric['ind'] = metric.index + 1

        cnt = metric.shape[0]

        metric['precision'] = metric.acc_tp / metric.ind
        metric['recall'] = metric.acc_tp / cnt

        return metric

    @staticmethod
    def calc_average_precisions(rec, prec):
        mrec = []
        mrec.append(0)
        [mrec.append(e) for e in rec]
        mrec.append(1)
        mpre = []
        mpre.append(0)
        [mpre.append(e) for e in prec]
        mpre.append(0)
        for i in range(len(mpre) - 1, 0, -1):
            mpre[i - 1] = max(mpre[i - 1], mpre[i])
        ii = []
        for i in range(len(mrec) - 1):
            if mrec[1:][i] != mrec[0:-1][i]:
                ii.append(i + 1)
        ap = 0
        for i in ii:
            ap = ap + np.sum((mrec[i] - mrec[i - 1]) * mpre[i])
        # return [ap, mpre[0:len(mpre) - 1], mrec[0:len(mpre) - 1], ii]
        return ap


def correct (s):
	s = " ".join(s.split())
	l = []
	for i in range(len(s)):
		if s[i] == '.' and s[i + 1] != ' ':
			l.append('. ')
		else:
			l.append(s[i])
	return "".join(l)

def main ():

	text = "Humberto     house.hi"
	print (correct(text))

main()


"""
Python-作业（第一次）
有一段文字需要处理，文字内容如下：
"When the aerials are down, and your spirit is covered with snows of cynicism and
the ice of pessimism, then you are grown old, even at twenty, but as long as your aerials
are up, to catch the waves of optimism, there is hope you may die young at eighty."

"""
text = """
"When the aerials are down, and your spirit is covered with snows of cynicism and
the ice of pessimism, then you are grown old, even at twenty, but as long as your aerials
are up, to catch the waves of optimism, there is hope you may die young at eighty."

"""
wordList = text.replace(',','')
wordList = wordList.split()
# 请根据上述文字，编写程序，输出以下问题的结果。

# 问题一：该段文字每个单词出现的次数是多少？【3分，其中注释清晰1分,必须用字典输出】

wordsUniqueSet = set(wordList)
wordsUnquelist = list(wordsUniqueSet)
wordsCountDict = {}
wordscountList = []
for wordUnique in wordsUnquelist:
        count = wordList.count(wordUnique)
        wordsCountDict[wordUnique] = count
print(wordsCountDict)
# 问题二：其中单词长度超过5个字母的单词出现的次数是多少？【2分，其中注释清晰1分,必须用字典输出】
# 输出格式为"{***:***,***:***}"
wordCountFive = {}
for key, value in wordsCountDict.items():
        if len(key) > 5 :
                wordCountFive[key] = value
print(wordCountFive)

# 问题三：设计一个交互式程序，能够根据用户输入的单词将单词出现的次数打印在屏幕上？【3分，其中注释清晰1分】
# 输出格式为"***在文章中出现的次数是:*"
word = input('请输入文章中的单词:')
wordtxt =wordsCountDict.get(word,0)
print("{}在文章中出现的次数是：{}".format(word, wordtxt))

"""
注：
提交的文件名称请使用原文件名加学号姓名的方式。【1分】
如同学的学号为“00001”，姓名为“张三”
提交的文件名为“python-作业-02-00001-张三.py”（注意中间的横杠为英文且没有空格）
"""


import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
import numpy as np
from scipy.io import loadmat
from options import Option
from pytorch3d.structures import Meshes
from pytorch3d.renderer import (
    FoVPerspectiveCameras,
    look_at_rotation,
    PointLights,
    Materials,
    RasterizationSettings,
    MeshRenderer,
    MeshRasterizer,
    SoftPhongShader,
    TexturesUV,
    TexturesVertex,
    TensorProperties,
    specular
)
#import option as opt
import math as m

class PointLightsNew(TensorProperties):
    def __init__(
        self,
        diffuse_color_per_vertex,
        ambient_color=((0.5, 0.5, 0.5),),
        diffuse_color=((0.3, 0.3, 0.3),),
        specular_color=((0.2, 0.2, 0.2),),
        location=((0, 1, 0),),
        device: str = "cpu",
    ):
        """
        Args:
            ambient_color: RGB color of the ambient component
            diffuse_color: RGB color of the diffuse component
            specular_color: RGB color of the specular component
            location: xyz position of the light.
            device: torch.device on which the tensors should be located
        The inputs can each be
            - 3 element tuple/list or list of lists
            - torch tensor of shape (1, 3)
            - torch tensor of shape (N, 3)
        The inputs are broadcast against each other so they all have batch
        dimension N.
        """
        super().__init__(
            diffuse_color_per_vertex=diffuse_color_per_vertex,
            device=device,
            ambient_color=ambient_color,
            diffuse_color=diffuse_color,
            specular_color=specular_color,
            location=location,
        )
        _validate_light_properties(self)
        # pyre-fixme[16]: `PointLights` has no attribute `location`.
        if self.location.shape[-1] != 3:
            msg = "Expected location to have shape (N, 3); got %r"
            raise ValueError(msg % repr(self.location.shape))

    def clone(self):
        other = self.__class__(device=self.device)
        return super().clone(other)

    def diffuse(self, normals, points) -> torch.Tensor:
        # pyre-fixme[16]: `PointLights` has no attribute `location`.
        direction = self.location - points
        # pyre-fixme[16]: `PointLights` has no attribute `diffuse_color`.
        return self.diffuse_color_per_vertex
    def specular(self, normals, points, camera_position, shininess) -> torch.Tensor:
        # pyre-fixme[16]: `PointLights` has no attribute `location`.
        direction = self.location - points
        return specular(
            points=points,
            normals=normals,
            # pyre-fixme[16]: `PointLights` has no attribute `specular_color`.
            color=self.specular_color,
            direction=direction,
            camera_position=camera_position,
            shininess=shininess,
        )

def _validate_light_properties(obj):
    props = ("ambient_color", "diffuse_color", "specular_color")
    for n in props:
        t = getattr(obj, n)
        if t.shape[-1] != 3:
            msg = "Expected %s to have shape (N, 3); got %r"
            raise ValueError(msg % (n, t.shape))

class BFM():
    def __init__(self, model_path='./BFM/BFM_model_front.mat'):
        model = loadmat(model_path)
        self.opt = Option()
        self.meanshape = torch.tensor(model['meanshape']).to(self.opt.device)
        self.idBase = torch.tensor(model['idBase']).to(self.opt.device)
        self.exBase = torch.tensor(model['exBase'].astype(np.float32)).to(self.opt.device)
        self.meantex = torch.tensor(model['meantex']).to(self.opt.device)
        self.texBase = torch.tensor(model['texBase']).to(self.opt.device)
        self.point_buf = torch.tensor(model['point_buf']).to(self.opt.device)
        self.face_buf = torch.tensor(model['tri']).to(self.opt.device)
        self.front_mask_render = torch.squeeze(torch.tensor(model['frontmask2_idx'])).to(self.opt.device)
        self.mask_face_buf = torch.tensor(model['tri_mask2']).to(self.opt.device)
        self.skin_mask = torch.squeeze(torch.tensor(model['skinmask'])).to(self.opt.device)
        self.keypoints = torch.squeeze(torch.tensor(model['keypoints'])).to(self.opt.device)

class Face3D(nn.Module):
    def __init__(self):
        super().__init__()
        facemodel = BFM()
        self.facemodel = facemodel
        self.rasterization_setting = RasterizationSettings(
            image_size=224
        )
        self.mesh_rasterizer = MeshRasterizer(cameras=None, raster_settings=self.rasterization_setting)
        self.shader = SoftPhongShader()

        self.renderer = MeshRenderer(self.mesh_rasterizer, self.shader)
        self.opt = Option()

    def Reconstruction_Block(self, coeff, opt):
        id_coeff,ex_coeff,tex_coeff,angles,translation,gamma,camera_scale,f_scale = self.Split_coeff(coeff)

        face_shape = self.Shape_formation_block(id_coeff, ex_coeff, self.facemodel)
        face_texture = self.Texture_formation_block(tex_coeff, self.facemodel)
        rotation = self.Compute_rotation_matrix(angles)
        face_norm = self.Compute_norm(face_shape, self.facemodel)
        norm_r = torch.matmul(face_norm, rotation)

        face_shape_t = self.Rigid_transform_block(face_shape, rotation, translation)

        face_landmark_t = self.Compute_landmark(face_shape_t, self.facemodel)
        landmark_p = self.Projection_block(face_landmark_t, camera_scale, f_scale)

        face_color = self.Illumination_block(face_texture, norm_r, gamma)
        #need opt
        render_block = self.Render_block(face_shape_t, norm_r, face_color, camera_scale, f_scale, self.facemodel, opt.batch_size, opt.is_train)

        self.id_coeff = id_coeff
        self.ex_coeff = ex_coeff
        self.tex_coeff = tex_coeff
        self.f_scale = f_scale
        self.gamma = gamma
        self.face_shape = face_shape
        self.face_shape_t = face_shape_t
        self.face_texture = face_texture
        self.face_color = face_color
        self.landmark_p = landmark_p
        self.render_block = render_block
        #self.render_imgs = render_imgs
        #self.img_mask = img_mask
        #self.img_mask_crop = img_mask_crop

    def Split_coeff(self, coeff):
        id_coeff = coeff[:, :80]
        ex_coeff = coeff[:, 80:144]
        tex_coeff = coeff[:, 144:224]
        angles = coeff[:, 224:227]
        gamma = coeff[:, 227:254]
        translation = coeff[:, 254:257]
        camerascale = torch.ones((int(coeff.shape[0]), 1)).to(self.opt.device)
        f_scale = torch.ones((int(coeff.shape[0]), 1)).to(self.opt.device)
        return id_coeff, ex_coeff, tex_coeff, angles, translation, gamma, camerascale, f_scale

    def Shape_formation_block(self, id_coeff, ex_coeff, facemodel):
        face_shape = torch.einsum('ij,aj->ai', facemodel.idBase, id_coeff) + torch.einsum('ij,aj->ai', facemodel.exBase, ex_coeff) + facemodel.meanshape

        face_shape = torch.reshape(face_shape, (int(face_shape.shape[0]), -1, 3))
        face_shape = face_shape - torch.reshape((torch.mean(torch.reshape(facemodel.meanshape, (-1, 3)) ,0)), (1,1,3))

        return face_shape

    def Compute_norm(self, face_shape, facemodel):
        shape = face_shape
        face_id = facemodel.face_buf
        point_id = facemodel.point_buf

        face_id = (face_id - 1).type(torch.int32)
        point_id = (point_id - 1).type(torch.int32)

        # print(face_id.shape)
        # print(shape.shape)
        vv1 = shape[:, face_id[:, 0].type(torch.long), :]
        vv2 = shape[:, face_id[:, 1].type(torch.long), :]
        vv3 = shape[:, face_id[:, 2].type(torch.long), :]

        # v1 = torch.gather(shape, 1, face_id[:, 0])
        # v2 = torch.gather(shape, 1, face_id[:, 1])
        # v3 = torch.gather(shape, 1, face_id[:, 2])
        e1 = vv1 - vv2
        e2 = vv2 - vv3
        face_norm = torch.cross(e1, e2)

        face_norm = F.normalize(face_norm, p=2, dim=2)
        face_norm = torch.cat((face_norm,
        torch.zeros((int(face_shape.shape[0]), 1, 3)).to(self.opt.device)
        ), 1)

        #print(face_norm.shape)
        tmp = face_norm[:, point_id.type(torch.long), :]
        #print(tmp.shape)

        v_norm = torch.sum(tmp, 2)
        #v_norm = torch.sum(torch.gather(face_norm, 1, point_id), 2)
        v_norm = F.normalize(v_norm, dim=2)

        return v_norm

    def Texture_formation_block(self, tex_coeff, face_model):
        # print(tex_coeff.shape)
        # print(face_model.texBase.shape)
        face_texture = torch.einsum('ij,aj->ai', face_model.texBase, tex_coeff) + face_model.meantex
        face_texture = torch.reshape(face_texture, (
            int(face_texture.shape[0]),
            -1,
            3
        ))

        return face_texture

    def Compute_rotation_matrix(self, angles):
        n_data = int(angles.shape[0])
        rotation_X = torch.cat((
            torch.ones((n_data, 1)).to(self.opt.device),
            torch.zeros((n_data, 3)).to(self.opt.device),
            torch.reshape(torch.cos(angles[:, 0]), (n_data, 1)),
            -torch.reshape(torch.sin(angles[:, 0]), (n_data, 1)),
            torch.zeros((n_data, 1)).to(self.opt.device),
            torch.reshape(torch.sin(angles[:, 0]), (n_data, 1)),
            torch.reshape(torch.cos(angles[:, 0]), (n_data, 1)),
        ), 1).to(self.opt.device)

        rotation_Y = torch.cat(
            (torch.reshape(torch.cos(angles[:,1]),(n_data,1)),
			torch.zeros((n_data,1)).to(self.opt.device),
			torch.reshape(torch.sin(angles[:,1]),(n_data,1)),
			torch.zeros((n_data,1)).to(self.opt.device),
			torch.ones((n_data,1)).to(self.opt.device),
			torch.zeros((n_data,1)).to(self.opt.device),
			-torch.reshape(torch.sin(angles[:,1]),(n_data,1)),
			torch.zeros((n_data,1)).to(self.opt.device),
			torch.reshape(torch.cos(angles[:,1]),(n_data,1))),
			1
		)

        rotation_Z = torch.cat(
            (torch.reshape(torch.cos(angles[:,2]),(n_data,1)),
			-torch.reshape(torch.sin(angles[:,2]),(n_data,1)),
			torch.zeros((n_data,1)).to(self.opt.device),
			torch.reshape(torch.sin(angles[:,2]),(n_data,1)),
			torch.reshape(torch.cos(angles[:,2]),(n_data,1)),
			torch.zeros([n_data,3]).to(self.opt.device),
			torch.ones((n_data,1)).to(self.opt.device)),
			1
		)

        rotation_X = torch.reshape(rotation_X,(n_data,3,3))
        rotation_Y = torch.reshape(rotation_Y,(n_data,3,3))
        rotation_Z = torch.reshape(rotation_Z,(n_data,3,3))

        rotation = torch.matmul(torch.matmul(rotation_Z, rotation_Y), rotation_X)

        rotation = torch.transpose(rotation, 2, 1)

        return rotation

    def Projection_block(self, face_shape, camera_scale, f_scale):
        focal = torch.tensor(1015.0)
        focal = focal * f_scale
        focal = torch.reshape(focal, (-1, 1))
        batchsize = int(focal.shape[0])

        camera_pos = torch.reshape(torch.tensor([0.0, 0.0, 10.0]).to(self.opt.device), (1, 1, 3)) * torch.reshape(camera_scale, (-1, 1, 1))
        reverse_z = torch.tile(torch.reshape(torch.tensor([1.0,0,0,0,1,0,0,0,-1.0]), (1, 3, 3)), (int(face_shape.shape[0]), 1, 1)).to(self.opt.device)

        p_matrix = torch.cat(
            (
                focal,
                torch.zeros((batchsize, 1)).to(self.opt.device),
                112.0 * torch.ones((batchsize, 1)).to(self.opt.device),
                torch.zeros((batchsize, 1)).to(self.opt.device),
                focal,
                112.0 * torch.ones((batchsize, 1)).to(self.opt.device),
                torch.zeros((batchsize, 2)).to(self.opt.device),
                torch.ones((batchsize, 1)).to(self.opt.device),
            ),
            1
        )
        p_matrix = torch.reshape(p_matrix, (-1, 3, 3))

        face_shape = torch.matmul(face_shape, reverse_z) + camera_pos
        aug_projection = torch.matmul(face_shape, torch.transpose(p_matrix, 2, 1))

        face_projection = aug_projection[:, :, 0:2] / torch.reshape(aug_projection[:, :, 2], (int(face_shape.shape[0]), int(aug_projection.shape[1]), 1))

        return face_projection

    def Compute_landmark(self, face_shape, facemodel):

        keypoints_idx = facemodel.keypoints
        keypoints_idx = (keypoints_idx - 1).type(torch.long)
        #print(face_shape.shape)
        #face_landmark = torch.gather(face_shape, 1, keypoints_idx)
        face_landmark = face_shape[:, keypoints_idx, :]
        return face_landmark

    def Illumination_block(self, face_texture, norm_r, gamma):
        n_data = int(gamma.shape[0])
        n_point = int(norm_r.shape[1])
        gamma = torch.reshape(gamma, (n_data, 3, 9))

        init_lit = torch.tensor([0.8,0,0,0,0,0,0,0,0]).to(self.opt.device)
        gamma = gamma + torch.reshape(init_lit, (1, 1, 9))

        a0 = m.pi
        a1 = 2*m.pi / torch.sqrt(torch.tensor(3.0).type(torch.float32)).to(self.opt.device)
        a2 = 2*m.pi / torch.sqrt(torch.tensor(8.0).type(torch.float32)).to(self.opt.device)
        c0 = 1/torch.sqrt(torch.tensor(4 * m.pi).type(torch.float32)).to(self.opt.device)
        c1 = torch.sqrt(torch.tensor(3.0)) / torch.sqrt(torch.tensor(4 * m.pi)).to(self.opt.device)
        c2 = 3 * torch.sqrt(torch.tensor(5.0)) / torch.sqrt(torch.tensor(12 * m.pi)).to(self.opt.device)

        Y = torch.cat(
            (
                torch.tile(torch.reshape(a0 * c0, (1, 1, 1)), (n_data, n_point, 1)),
                torch.unsqueeze(-a1*c1*norm_r[:,:,1], 2),
                torch.unsqueeze(a1*c1*norm_r[:,:,2], 2),
			    torch.unsqueeze(-a1*c1*norm_r[:,:,0], 2),
			    torch.unsqueeze(a2*c2*norm_r[:,:,0]*norm_r[:,:,1], 2),
			    torch.unsqueeze(-a2*c2*norm_r[:,:,1]*norm_r[:,:,2], 2),
                torch.unsqueeze(a2*c2*0.5/torch.sqrt(torch.tensor(3.0))*(3*torch.square(norm_r[:,:,2])-1),2),
			    torch.unsqueeze(-a2*c2*norm_r[:,:,0]*norm_r[:,:,2],2),
                torch.unsqueeze(a2*c2*0.5*(torch.square(norm_r[:,:,0])-torch.square(norm_r[:,:,1])),2)
            ),
            2
        )

        color_r = torch.squeeze(torch.matmul(Y,torch.unsqueeze(gamma[:,0,:],2)),axis = 2)
        color_g = torch.squeeze(torch.matmul(Y,torch.unsqueeze(gamma[:,1,:],2)),axis = 2)
        color_b = torch.squeeze(torch.matmul(Y,torch.unsqueeze(gamma[:,2,:],2)),axis = 2)

        face_color = torch.stack([color_r*face_texture[:,:,0],color_g*face_texture[:,:,1],color_b*face_texture[:,:,2]], 2)

        return face_color

    def Rigid_transform_block(self, face_shape, rotation, translation):
        face_shape_r = torch.matmul(face_shape, rotation)
        face_shape_t = face_shape_r + torch.reshape(translation, (int(face_shape.shape[0]), 1, 3))

        return face_shape_t

    def Render_block(self, face_shape, face_norm, face_color, camera_scale, f_scale, facemodel, batchsize, is_train=True):

        n_vex = int(facemodel.idBase.shape[0]/3)

        fov_y = 2 * torch.atan(112.0 / (1015.0 * f_scale)) * 180.0 / m.pi
        # print(fov_y)
        # fov_y = torch.reshape(fov_y, (batchsize, 1))
        fov_y = float(fov_y[0, 0])

        face_shape = torch.reshape(face_shape, (batchsize, n_vex, 3))
        face_norm = torch.reshape(face_norm, (batchsize, n_vex, 3))
        face_color = torch.reshape(face_color, (batchsize, n_vex, 3))

        # mask_face_shape = torch.gather(face_shape, 1, (facemodel.front_mask_render - 1).type(torch.int32))
        # mask_face_norm = torch.gather(face_norm, 1, (facemodel.front_mask_render - 1).type(torch.int32))
        # mask_face_color = torch.gather(face_color, 1, (facemodel.front_mask_render - 1).type(torch.int32))

        mask_face_shape = face_shape[:, (facemodel.front_mask_render - 1).type(torch.long), :]
        mask_face_norm = face_norm[:, (facemodel.front_mask_render - 1).type(torch.long), :]
        mask_face_color = face_color[:, (facemodel.front_mask_render - 1).type(torch.long), :]

        camera_position = torch.tensor([0, 0, 10.0]).to(self.opt.device) * torch.reshape(camera_scale, (-1, 1))
        camera_look_at = torch.tensor([0, 0, 0.0]).to(self.opt.device)
        camera_up = torch.tensor([0, 1.0, 0]).to(self.opt.device)

        camera_look_at = torch.reshape(camera_look_at, (1, 3))
        camera_up = torch.reshape(camera_up, (1, 3))

        light_positions = torch.tile(torch.reshape(torch.tensor([0, 0, 1e5]).to(self.opt.device), (1, 1, 3)), (batchsize, 1, 1))
        light_intensities = torch.tile(torch.reshape(torch.tensor([0.0, 0.0, 0.0]).to(self.opt.device), (1, 1, 3)), (batchsize, 1, 1))
        ambient_color = torch.tile(torch.reshape(torch.tensor([1.0, 1, 1]), (1, 3)), (batchsize, 1)).to(self.opt.device)

        #goi 3dpytorch

        return {
            'n_vex': n_vex,
            'fov_y': fov_y,
            'face_shape': face_shape,
            'face_norm': face_norm,
            'face_color': face_color,
            'mask_face_shape': mask_face_shape,
            'mask_face_norm': mask_face_norm,
            'mask_face_color': mask_face_color,
            'camera_position': camera_position,
            'camera_look_at': camera_look_at,
            'camera_up': camera_up,
            'light_positions': light_positions,
            'light_intensities': light_intensities,
            'ambient_color': ambient_color
        }

    def forward(self, x):
        coeff = x['coeff']
        opt = x['opt']
        self.Reconstruction_Block(coeff, opt)
        #goi 3dtorch
        # print(self.render_block['camera_position'].shape)
        # print(self.render_block['camera_look_at'].shape)
        # print(self.render_block['camera_up'].shape)
        camera_look_at_rotation = look_at_rotation(self.render_block['camera_position'], self.render_block['camera_look_at'], self.render_block['camera_up'])

        camera = FoVPerspectiveCameras(fov=self.render_block['fov_y'], znear=0.01, zfar=50.0, R=camera_look_at_rotation, device=self.opt.device)

        lights = PointLightsNew(diffuse_color_per_vertex=self.render_block['face_color'], ambient_color=self.render_block['ambient_color'])

        lights_mask = PointLightsNew(diffuse_color_per_vertex=self.render_block['mask_face_color'], ambient_color=self.render_block['ambient_color'])

        faces = (self.facemodel.face_buf-1).type(torch.long)
        faces_tmp = torch.unsqueeze(faces, 0)
        # tmp = -1*torch.ones((3, faces.shape[0], faces.shape[1])).to(self.opt.device)
        # #print(faces.shape)
        # #print(tmp)
        # faces_tmp = torch.cat([faces_tmp, tmp], dim=0).type(torch.long)
        #print(faces_tmp.shape)
        faces_tmp2 = torch.unsqueeze(faces, 0)
        faces_tmp2 = faces_tmp2.repeat(4, 1, 1)
        #print(faces_tmp2.shape)

        #print(self.render_block['mask_face_shape'].shape)
        #print(faces.shape)

        #print(faces_tmp.shape)
        #print(self.render_block['mask_face_shape'].shape)

        faces_mask = (self.facemodel.mask_face_buf-1).type(torch.long)
        faces_tmp_mask = torch.unsqueeze(faces_mask, 0)
        #print(faces_tmp.shape)
        faces_tmp2_mask = torch.unsqueeze(faces_mask, 0)
        faces_tmp2_mask = faces_tmp2_mask.repeat(4, 1, 1)

        meshes_world = Meshes(verts=self.face_shape, faces=faces_tmp2)

        meshes_world_mask = Meshes(verts=self.render_block['mask_face_shape'], faces=faces_tmp2_mask)

        out_img_raw = self.renderer(meshes_world, cameras=camera, lights=lights)

        img = out_img_raw[:, :, :, :3]
        mask = out_img_raw[:, :, :, 3:]

        img = torch.flip(img, 3).type(torch.float32)
        mask = mask.type(torch.float32)

        if (self.opt.is_train):
            mask_crop = self.renderer(meshes_world_mask, cameras=camera, lights=lights_mask)
            mask_f = mask_crop[:, :, :, 3:]
            mask_f = mask_f.type(torch.float32)

            return {
                'rendered_img': img,
                'img_mask': mask,
                'img_mask_crop': mask_f,
                'landmark_p': self.landmark_p,
                'id_coeff': self.id_coeff,
                'ex_coeff': self.ex_coeff,
                'tex_coeff': self.tex_coeff,
                'face_texture': self.face_texture,
                'facemodel': self.facemodel,
                'gamma': self.gamma
            }

        return {
            'rendered_img': img,
            'img_mask': mask,
            'img_mask_crop': mask,
            'landmark_p': self.landmark_p,
            'id_coeff': self.id_coeff,
            'ex_coeff': self.ex_coeff,
            'tex_coeff': self.tex_coeff,
            'face_texture': self.face_texture,
            'facemodel': self.facemodel,
            'gamma': self.gamma
        }


from sklearn.linear_model import LogisticRegression
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score

data = pd.read_csv('iris-data.csv')
data.head()
data.describe()
data.info()

# drop na values
data = data.dropna()
data.info()

# make classes consistent
data['class'].value_counts()
data['class'].replace(["Iris-setossa", "versicolor"],
                      ["Iris-setosa", "Iris-versicolor"], inplace=True)
data['class'].value_counts()

# select 2 categories only
final_data = data[data['class'] != 'Iris-virginica']
final_data['class'].value_counts()

# sns.pairplot(final_data, hue='class', height=2.5)
# plt.show()

final_data.hist(column='sepal_length_cm', bins=20, figsize=(10, 5))
# remove outliests
final_data.loc[final_data.sepal_length_cm < 1, [
    'sepal_length_cm']] = final_data['sepal_length_cm']*100
final_data.hist(column='sepal_length_cm', bins=20, figsize=(10, 5))
final_data = final_data.drop(final_data[(
    final_data['class'] == "Iris-setosa") & (final_data['sepal_width_cm'] < 2.5)].index)

# sns.pairplot(final_data, hue='class', size=2.5)
# plt.show()

# replace class names with values
final_data['class'].replace(
    ["Iris-setosa", "Iris-versicolor"], [1, 0], inplace=True)
final_data.head()

# model construction
# get values for matrix
inp_data = final_data.iloc[:, :-1].values
out_data = final_data.iloc[:, -1].values

# scale
scalar = StandardScaler()
inp_data = scalar.fit_transform(inp_data)

# split into test and train
X_train, X_test, y_train, y_test = train_test_split(
    inp_data, out_data, test_size=0.2, random_state=42)

# X_tr_arr = X_train
# X_ts_arr = X_test
# y_tr_arr = y_train.as_matrix()
# y_ts_arr = y_test.as_matrix()
print('Input Shape', (X_train.shape))
print('Output Shape', X_test.shape)

def weight_init(n_features):
    w = np.zeros((1, n_features))
    b = 0
    return w, b

def sigmoid_activation(result):
    return 1/(1+np.exp(-result))

def model_optimize(w, b, X, Y):
    m = X.shape[0]

    # Prediction
    final_result = sigmoid_activation(np.dot(w, X.T)+b)
    Y_T = Y.T
    cost = (-1/m)*(np.sum((Y_T*np.log(final_result)) +
                          ((1-Y_T)*(np.log(1-final_result)))))

    # Gradient calculation
    dw = (1/m)*(np.dot(X.T, (final_result-Y.T).T))
    db = (1/m)*(np.sum(final_result-Y.T))

    grads = {"dw": dw, "db": db}

    return grads, cost

def model_predict(w, b, X, Y, learning_rate, no_iterations):
    costs = []
    for i in range(no_iterations):
        #
        grads, cost = model_optimize(w, b, X, Y)
        #
        dw = grads["dw"]
        db = grads["db"]
        # weight update
        w = w - (learning_rate * (dw.T))
        b = b - (learning_rate * db)
        #

        if (i % 100 == 0):
            costs.append(cost)
            print("Cost after %i iteration is %f" % (i, cost))

    # final parameters
    coeff = {"w": w, "b": b}
    gradient = {"dw": dw, "db": db}

    return coeff, gradient, costs

def predict(final_pred, m):
    y_pred = np.zeros((1, m))
    for i in range(final_pred.shape[1]):
        if final_pred[0][i] > 0.5:
            y_pred[0][i] = 1
    return y_pred

# Get number of features
n_features = X_train.shape[1]
print('Number of Features', n_features)
w, b = weight_init(n_features)
# Gradient Descent
coeff, gradient, costs = model_predict(
    w, b, X_train, y_train, learning_rate=0.0001, no_iterations=4500)
# Final prediction
w = coeff["w"]
b = coeff["b"]
print('Optimized weights', w)
print('Optimized intercept', b)
#
final_train_pred = sigmoid_activation(np.dot(w, X_train.T)+b)
final_test_pred = sigmoid_activation(np.dot(w, X_test.T)+b)
#
m_tr = X_train.shape[0]
m_ts = X_test.shape[0]
#
y_tr_pred = predict(final_train_pred, m_tr)
print('Training Accuracy', accuracy_score(y_tr_pred.T, y_train))
#
y_ts_pred = predict(final_test_pred, m_ts)
print('Test Accuracy', accuracy_score(y_ts_pred.T, y_test))

plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations (per hundreds)')
plt.title('Cost reduction over time')
plt.show()

# Sklearn
clf = LogisticRegression()
clf.fit(X_tr_arr, y_tr_arr)
print(clf.intercept_, clf.coef_)
pred = clf.predict(X_ts_arr)
print('Accuracy from sk-learn: {0}'.format(clf.score(X_ts_arr, y_ts_arr)))


"""
Definition of Interval.
class Interval(object):
    def __init__(self, start, end):
        self.start = start
        self.end = end
"""

class Solution:
    """
    @param intervals: a collection of intervals
    @return: the minimum number of intervals you need to remove
    """

    def eraseOverlapIntervals(self, intervals):
        # write your code here
        erased, end = 0, -float('inf')
        for i in sorted(intervals, key=lambda x: x.end):
            if i.start >= end:
                end = i.end
            else:
                erased += 1
        return erased


# encoding:utf-8
import json
import requests
from requests.exceptions import RequestException
import re
import time
def get_one_page(url):
    # 添加头信息
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.3964.2 Safari/537.36'}
    html = requests.get(url, headers=headers)
    if html.status_code == 200:
        return html.text
    return None
def parse_one_page(html):
    pattern = re.compile('
.*?num .*?>(.*?)	.*?colorgray3.*?>(.*?).*?linka.*?>(.*?).*?colorgray9">(.*?)	.*?colorgray9">(.*?)	.*?fnt600">(.*?)	.*?colorgray9">(.*?)	.*?colorgray9">(.*?)	.*?
', re.S)
    items = re.findall(pattern, html)
    for item in items:
        yield {
            '排名': item[0],
            '姓名': item[1],
            '队名': item[2],
            '位置': item[3],
            '出场次数': item[4],
            '进球数': item[5],
            '点球数': item[6],
            '平均进球时间': item[7]
        }
def write_to_file(content):
    with open('result.txt', 'a', encoding='utf-8') as f:
        f.write(json.dumps(content, ensure_ascii=False) + '\n')
def main(dataPage):
    url = 'https://data.huanhuba.com/ucl/goals-410-1-37521-p' + str(dataPage) + '/'
    html = get_one_page(url)
    for item in parse_one_page(html):
        print(item)
        write_to_file(item)
if __name__ == '__main__':
    for dataPage in range(1,17):
        main(dataPage)
        time.sleep(1)


#encoding: UTF-8

# Autor: Saul Rodrigo Toral Luna #Matricula: A01745007
# Descripción:  El programa calculará el total y el porcentaje de hombres y mujeres inscritos.

# Calcular:
#	El número total de alumnos inscritos.
#	El porcentaje de mujeres.
#	El porcentaje de hombres.

# 1.- El usuario ingresara valores para cantidad de mujeres y de hombres.

mujeres = input("Ingresa la cantidad de muejres inscritas: ")
hombres = input("Ingresa la cantidad de hombres inscritos: ")

#2.- Calcular el total de personas inscritas

total = (int(mujeres) + int(hombres))

#3.- Calcular los porcentajes para hombres y mujeres.

porcentajeHombres = (int(hombres) * (100)) / (int(total))
porcentajeMujeres = (int(mujeres) * (100)) / (int(total))

#4.- Imprimir resultados

print("El total de personas inscritas es de: ", total)
print("El porcentaje de mujeres es de: ", int(porcentajeMujeres),"%")
print("El porcentaje de Hombres es de: ", int(porcentajeHombres),"%")


from typing import List

class Solution:
    def exist(self, board: List[List[int]], word: str) -> bool:
        rows = len(board)
        if rows == 0:
            return False
        cols = len(board[0])
        is_come = []
        for _ in range(rows):
            is_come.append([False] * cols)
        for i in range(rows):
            for j in range(cols):
                if self.check(board, word, is_come, i, j, 0, len(word)):
                    return True
        return False

    def check(self, board: List[List[int]], word: str, is_come: List[List[bool]], i: int, j: int, flag: int, max_flag: int) -> bool:
        print("check", i, j)
        if flag == max_flag:
            return True
        rows = len(board)
        if rows == 0:
            return False
        cols = len(board[0])
        is_next_in = False
        if i < 0 or j < 0 or i >= rows or j >= cols:
            return is_next_in
        if board[i][j] == word[flag] and not is_come[i][j]:
            is_come[i][j] = True
            flag += 1
            is_next_in = self.check(board, word, is_come, i-1, j, flag, max_flag) or\
                self.check(board, word, is_come, i, j-1, flag, max_flag) or\
                self.check(board, word, is_come, i+1, j, flag, max_flag) or\
                self.check(board, word, is_come, i, j+1, flag, max_flag)

            if not is_next_in:
                flag -= 1
                is_come[i][j] = False
        return is_next_in

if __name__ == "__main__":
    s = Solution()
    board = [["A", "B", "C", "E"], ["S", "F", "E", "S"], ["A", "D", "E", "E"]]
    word = "ABCESEEEFS"
    if s.exist(board, word):
        print("exists")
    else:
        print("not exist")


age = input("What is your current age?")

yearsLeft = 90 - int(age)

monthsLeft = yearsLeft * 12

weeksLeft = yearsLeft * 52

daysLeft = yearsLeft * 365

print(f'You have {daysLeft} days, {weeksLeft} weeks, and {monthsLeft} months left.')


import tkinter as tk
from tkinter import messagebox
import requests
import re

class UpdateGymLeaderPopup(tk.Frame):
    """ Popup Frame to Add Trainer """

    def __init__(self, parent, close_callback):
        """ constructor """

        tk.Frame.__init__(self, parent)
        self._close_cb = close_callback
        self.grid(rowspan=2, columnspan=2)

        tk.Label(self, text="Trainer Id").grid(row=1, column=1)
        self._trainer_id = tk.Entry(self)
        self._trainer_id.grid(row=1, column=2)
        tk.Label(self, text="Gym Leader Name").grid(row=2, column=1)
        self._name = tk.Entry(self)
        self._name.grid(row=2, column=2)
        tk.Label(
            self, text="Pokemon #1 - Required - Format: Pokemon, Level").grid(row=3, column=1)
        self._pokemon1 = tk.Entry(self)
        self._pokemon1.grid(row=3, column=2)
        self._pokemon1_level = tk.Entry(self)
        self._pokemon1_level.grid(row=3, column=3)
        tk.Label(
            self, text="Pokemon #2 - Optional - Format: Pokemon, Level").grid(row=4, column=1)
        self._pokemon2 = tk.Entry(self)
        self._pokemon2.grid(row=4, column=2)
        self._pokemon2_level = tk.Entry(self)
        self._pokemon2_level.grid(row=4, column=3)
        tk.Label(
            self, text="Pokemon #3 - Optional - Format: Pokemon, Level").grid(row=5, column=1)
        self._pokemon3 = tk.Entry(self)
        self._pokemon3.grid(row=5, column=2)
        self._pokemon3_level = tk.Entry(self)
        self._pokemon3_level.grid(row=5, column=3)
        tk.Label(
            self, text="Pokemon #4 - Optional - Format: Pokemon, Level").grid(row=6, column=1)
        self._pokemon4 = tk.Entry(self)
        self._pokemon4.grid(row=6, column=2)
        self._pokemon4_level = tk.Entry(self)
        self._pokemon4_level.grid(row=6, column=3)
        tk.Label(
            self, text="Pokemon #5 - Optional - Format: Pokemon, Level").grid(row=7, column=1)
        self._pokemon5 = tk.Entry(self)
        self._pokemon5.grid(row=7, column=2)
        self._pokemon5_level = tk.Entry(self)
        self._pokemon5_level.grid(row=7, column=3)
        tk.Label(
            self, text="Pokemon #6 - Optional - Format: Pokemon, Level").grid(row=8, column=1)
        self._pokemon6 = tk.Entry(self)
        self._pokemon6.grid(row=8, column=2)
        self._pokemon6_level = tk.Entry(self)
        self._pokemon6_level.grid(row=8, column=3)
        tk.Label(self, text="Pokecoins").grid(row=9, column=1)
        self._pokecoins = tk.Entry(self)
        self._pokecoins.grid(row=9, column=2)
        tk.Label(self, text="Location").grid(row=10, column=1)
        self._location = tk.Entry(self)
        self._location.grid(row=10, column=2)
        tk.Label(self, text="Badge").grid(row=11, column=1)
        self._badge = tk.Entry(self)
        self._badge.grid(row=11, column=2)
        tk.Label(self, text="Element").grid(row=12, column=1)
        self._element = tk.Entry(self)
        self._element.grid(row=12, column=2)
        tk.Label(self, text="Prize").grid(row=13, column=1)
        self._prize = tk.Entry(self)
        self._prize.grid(row=13, column=2)
        tk.Button(self, text="Submit", command=self._submit_cb).grid(
            row=15, column=2, pady=(10, 0))
        tk.Button(self, text="Close", command=self._close_cb).grid(
            row=15, column=3, pady=(10, 0))

    def _submit_cb(self):
        """ Submit the Update """

        if re.match("^\d+$", self._pokemon1_level.get()) is None:
            messagebox.showerror(
                "Error", "Pokemon level must be a valid integer")
            return

        if re.match("^\d+$", self._pokecoins.get()) is None:
            messagebox.showerror(
                "Error", "Pokecoins amount must be a valid integer")
            return

        if re.match("^\d+$", self._trainer_id.get()) is None:
            messagebox.showerror(
                "Error", "Trainer id amount must be a valid integer")
            return

        trainer_id = self._trainer_id.get()

        # declaring a temp variable to store pokemon team
        pokemon_team = {}

        # putting the trainer pokemon into a dict
        if self._pokemon1.get() != "":
            try:
                pokemon = self._pokemon1.get()
                level = int(self._pokemon1_level.get())
                pokemon_team[pokemon] = level
            except:
                messagebox.showerror(
                    "Error", "Pokemon #1 - level must be an integer")
                return
        else:
            messagebox.showerror(
                "Error", "Trainer must have at least one pokemon")
            return

        if self._pokemon2.get() != "":
            try:
                pokemon = self._pokemon2.get()
                level = int(self._pokemon2_level.get())
                pokemon_team[pokemon] = level
            except:
                messagebox.showerror(
                    "Error", "Pokemon #2 - level must be an integer")
                return

        if self._pokemon3.get() != "":
            try:
                pokemon = self._pokemon3.get()
                level = int(self._pokemon3_level.get())
                pokemon_team[pokemon] = level
            except:
                messagebox.showerror(
                    "Error", "Pokemon #3 - level must be an integer")
                return

        if self._pokemon4.get() != "":
            try:
                pokemon = self._pokemon4.get()
                level = int(self._pokemon4_level.get())
                pokemon_team[pokemon] = level
            except:
                messagebox.showerror(
                    "Error", "Pokemon #4 - level must be an integer")
                return

        if self._pokemon5.get() != "":
            try:
                pokemon = self._pokemon5.get()
                level = int(self._pokemon5_level.get())
                pokemon_team[pokemon] = level
            except:
                messagebox.showerror(
                    "Error", "Pokemon #5 - level must be an integer")
                return

        if self._pokemon6.get() != "":
            try:
                pokemon = self._pokemon6.get()
                level = int(self._pokemon6_level.get())
                pokemon_team[pokemon] = level
            except:
                messagebox.showerror(
                    "Error", "Pokemon #6 - level must be an integer")
                return

        if pokemon_team == {}:
            messagebox.showerror(
                "Error", "Trainer must have at least one pokemon")
            return

        # Create the dictionary for the JSON request body
        data = {}
        data['name'] = self._name.get()
        data['pokemon_team'] = pokemon_team
        data['trainer_class'] = "Gym Leader"
        data['pokecoins'] = int(self._pokecoins.get())
        data['location'] = self._location.get()
        data['badge'] = self._badge.get()
        data['element'] = self._element.get()
        data['prize'] = self._prize.get()
        data['type'] = "Gym Leader"

        headers = {"content-type": "application/json"}
        response = requests.put("http://127.0.0.1:5000/trainermanager/trainers/" + trainer_id,
                                json=data, headers=headers)

        print(data)

        if response.status_code == 200:
            messagebox.showinfo("Success", "Gym Leader has been updated")
            self._close_cb()
        else:
            messagebox.showwarning(
                "Error", "Gym Leader could not be updated because of invalid data or an incorrect trainer id number")


from __future__ import unicode_literals

from django.contrib.auth.models import AnonymousUser, User
from django.http import HttpRequest
from django.template import RequestContext, Template
from djblets.avatars.services.gravatar import GravatarService

from reviewboard.testing import TestCase
from reviewboard.avatars import avatar_services
from reviewboard.avatars.testcase import AvatarServicesTestMixin
from reviewboard.avatars.tests import DummyAvatarService

class TemplateTagTests(AvatarServicesTestMixin, TestCase):
    """Tests for reviewboard.avatars.templatetags."""

    fixtures = ['test_users']

    @classmethod
    def setUpClass(cls):
        super(TemplateTagTests, cls).setUpClass()
        avatar_services.enable_service(GravatarService.avatar_service_id,
                                       save=False)

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

        gravatar_service = avatar_services.get_avatar_service(
            GravatarService.avatar_service_id)
        avatar_services.enable_service(gravatar_service.avatar_service_id,
                                       save=False)
        avatar_services.set_default_service(gravatar_service, save=False)

        self.user = User.objects.get(username='doc')
        self.request = HttpRequest()
        self.request.user = AnonymousUser()

    def test_avatar_urls(self):
        """Testing {% avatar_urls %} template tag"""
        service = avatar_services.default_service
        self.assertIsNotNone(service)

        t = Template(
            '{% load avatars %}'
            '{% avatar_urls u 32 %}'
        )

        self.assertEqual(
            t.render(RequestContext(self.request, {
                'u': self.user,
                'service_id': service.avatar_service_id,
            })),
            '{'
            '"1x": "%(1x)s", '
            '"3x": "%(3x)s", '
            '"2x": "%(2x)s"'
            '}'
            % service.get_avatar_urls_uncached(self.user, 32)
        )

    def test_avatar_urls_with_service(self):
        """Testing {% avatar_urls %} template tag with avatar_service_id"""
        service = DummyAvatarService(use_2x=True)
        avatar_services.register(service)
        avatar_services.enable_service(service.avatar_service_id)

        t = Template(
            '{% load avatars %}'
            '{% avatar_urls u 32 service_id %}'
        )

        self.assertEqual(
            t.render(RequestContext(self.request, {
                'u': self.user,
                'service_id': service.avatar_service_id,
            })),
            '{'
            '"1x": "%(1x)s", '
            '"2x": "%(2x)s"'
            '}'
            % service.get_avatar_urls_uncached(self.user, 32)
        )

    def test_avatar_urls_no_service(self):
        """Testing {% avatar_urls %} template tag with no available services"""
        services = list(avatar_services)

        for service in services:
            avatar_services.unregister(service)

        t = Template(
            '{% load avatars %}'
            '{% avatar_urls u 32 %}'
        )

        self.assertEqual(
            t.render(RequestContext(self.request, {
                'u': self.user,
            })),
            '{}')

    def test_avatar_urls_service_not_found(self):
        """Testing {% avatar_urls %} template tag with an invalid service"""
        service = avatar_services.default_service

        self.assertIsNotNone(service)
        self.assertIsNone(avatar_services.get_avatar_service(
            DummyAvatarService.avatar_service_id))

        t = Template(
            '{% load avatars %}'
            '{% avatar_urls u 32 service_id %}'
        )

        self.assertEqual(
            t.render(RequestContext(self.request, {
                'u': self.user,
                'service_id': DummyAvatarService.avatar_service_id,
            })),
            '{'
            '"1x": "%(1x)s", '
            '"3x": "%(3x)s", '
            '"2x": "%(2x)s"'
            '}'
            % service.get_avatar_urls_uncached(self.user, 32)
        )


from django.conf.urls import include, url
from django.views.generic import TemplateView

from . import views

urlpatterns = [
    url(r'^$', views.Home.as_view(), name='home'),
    url(r'^appt$', views.Appt.as_view(), name='appt'),
    url(r'^p_login/(\d+)/(\d+)$', views.PatientCheckin.as_view(), name='p_login'),
    url(r'^demographic/(\d+)/(\d+)$', views.Demographics.as_view(), name='demog'),
    url(r'^checkin/(\d+)$', views.Checkin.as_view(), name='checkin'),
    url(r'^d_waitlist$', views.Waitlist.as_view(), name='d_waitlist'),
    url(r'^begin$', TemplateView.as_view(template_name='begin.html'), name='begin'),
    url(r'^d_login$', TemplateView.as_view(template_name='doctor_login.html'), name='d_login'),
    url(r'^logout$', views.Logout.as_view(), name='logout'),

    url(r'', include('social.apps.django_app.urls', namespace='social')),

    # included to provide easy access to checkin error page
    # url(r'^checkin_err$', TemplateView.as_view(template_name='checkin_error.html'), name='checkin_err'),
]


from reportlab.platypus import Paragraph
from reportlab.platypus import Table
from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus import Spacer

from reportlab.lib.styles import getSampleStyleSheet

from reportlab.lib.pagesizes import A4

from reportlab.lib import colors
import Basedd

class AlbaranVentas:

    def __init__(self, referencia, dni, fecha):
        """Genera el albaran de la venta.

        :param referencia: referencia de producto para buscar el precio
        :param dni: parametro de dni para filtrar al consulta por parametro en la bdd
        :param fecha: fecha de la venta

        """
        self.referencia = referencia
        self.fecha = fecha

        estiloHoja = getSampleStyleSheet()

        story = []

        Basedd.Basedd.conectar(self)

        self.ventas = Basedd.Basedd.get_venta(self, dni, self.fecha)

        print([self.ventas[0], self.ventas[1], self.ventas[2], self.ventas[3], self.ventas[4], self.ventas[5]])

        filaVenta = [self.ventas[0], self.ventas[1], self.ventas[2], self.ventas[3], self.ventas[4], self.ventas[5]]

        Basedd.Basedd.cerrar(self)

        fila1 = ['Refcia.', 'DNI', 'Nombre', 'Fecha', 'Unidades', 'Total euros']

        tabla = Table((fila1, filaVenta))

        tabla.setStyle([('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black)])

        estiloHoja = getSampleStyleSheet()
        cabecera = estiloHoja['Heading4']

        parrafo = Paragraph("Factura simplificada:", cabecera)
        story.append(parrafo)

        story.append(tabla)

        story.append(Spacer(0, 20))

        doc = SimpleDocTemplate("AlvaranVentas.pdf", pagesize=A4, showBoundary=1)

        doc.build(story)


# -*- coding: utf-8 -*-
"""
@author: Sergio Rozada Doval

@description: This scripts implements multiagent deep deterministic policy gradient algorithm.
This is an actor-critic architecture that aims to produce a continuous deterministic
action for the system to operate in the multiagent case. The critic helps to estimate how good actions
are. The actor is the part of the architecture that produces the action. Training is centralized
by using shared information with the critics. Operation is decentralized just using the actors. In
this script, cost information is included.

"""

import dynamics as dn
import rl
import tensorflow as tf
import numpy as np
import architectures

""" Definition of the model using tensorflow"""

tf.reset_default_graph()
h_size = 100

# Create actor, critic, main and target networks of the first agent
lstm_actor_1 = tf.contrib.rnn.BasicLSTMCell(num_units=h_size,state_is_tuple=True)
lstm_critic_1 = tf.contrib.rnn.BasicLSTMCell(num_units=h_size,state_is_tuple=True)
actor_1 = architectures.actor_maddpg_cost(h_size,lstm_actor_1,'actor_1',0)
critic_1 = architectures.critic_maddpg_cost(h_size,lstm_critic_1,'critic_1',len(tf.trainable_variables()))

lstm_actor_t_1 = tf.contrib.rnn.BasicLSTMCell(num_units=h_size,state_is_tuple=True)
lstm_critic_t_1 = tf.contrib.rnn.BasicLSTMCell(num_units=h_size,state_is_tuple=True)
actor_t_1 = architectures.actor_maddpg_cost(h_size,lstm_actor_t_1,'actor_t_1',len(tf.trainable_variables()))
critic_t_1 = architectures.critic_maddpg_cost(h_size,lstm_critic_t_1,'critic_t_1',len(tf.trainable_variables()))

# Create actor, critic, main and target networks of the second agent
lstm_actor_2 = tf.contrib.rnn.BasicLSTMCell(num_units=h_size,state_is_tuple=True)
lstm_critic_2 = tf.contrib.rnn.BasicLSTMCell(num_units=h_size,state_is_tuple=True)
actor_2 = architectures.actor_maddpg_cost(h_size,lstm_actor_2,'actor_2',len(tf.trainable_variables()))
critic_2 = architectures.critic_maddpg_cost(h_size,lstm_critic_2,'critic_2',len(tf.trainable_variables()))

lstm_actor_t_2 = tf.contrib.rnn.BasicLSTMCell(num_units=h_size,state_is_tuple=True)
lstm_critic_t_2 = tf.contrib.rnn.BasicLSTMCell(num_units=h_size,state_is_tuple=True)
actor_t_2 = architectures.actor_maddpg_cost(h_size,lstm_actor_t_2,'actor_t_2',len(tf.trainable_variables()))
critic_t_2 = architectures.critic_maddpg_cost(h_size,lstm_critic_t_2,'critic_t_2',len(tf.trainable_variables()))

# Instantiate the model to initialize the variables
init = tf.global_variables_initializer()

""" Training of the model """

# Parameters of the learning operation
gamma = 0.9
tau = 0.001
epsilon = 0.99
episodes = 50000
steps = 100
cumm_r = 0
trace = 8
batch = 4
n_var = 9
cumm_r_list = []

# Create op holder for the target networks
actor_t_1.createOpHolder(actor_1.network_params,tau)
critic_t_1.createOpHolder(critic_1.network_params,tau)

actor_t_2.createOpHolder(actor_2.network_params,tau)
critic_t_2.createOpHolder(critic_2.network_params,tau)

# Buffer to store the experience to replay
buffer = rl.recurrent_experience_buffer()

# Launch the learning
with tf.Session() as session:
    session.run(init)

    # Iterate all the episodes
    for i in range(episodes):
        print("\nEPISODE: ",i)

        # Store cummulative reward per episode
        cumm_r_list.append(cumm_r)
        cumm_r = 0

        # Store the experience from the episode
        episodeBuffer = []

        # Instances of the environment
        generator_1 = dn.Node_Secondary(1.5,alpha=2)
        generator_2 = dn.Node_Secondary(1.5,alpha=1)

        area = dn.Area_Secondary(frequencySetPoint=50,M=0.1,D=0.0160,Tg=30,Rd=0.1)
        area.setLoad(3.0 + (- 0.25 + np.random.rand()/2))
        area.setGeneration(3.0)
        area.calculateDeltaF()

        # Initial state for the LSTM
        state_1 = (np.zeros([1,h_size]),np.zeros([1,h_size]))
        state_2 = (np.zeros([1,h_size]),np.zeros([1,h_size]))

        # Iterate all over the steps
        for j in range(steps):

            # Get the action from the actor and the internal state of the rnn
            current_f = area.getDeltaF()
            current_Z_1 = generator_1.getZ()
            current_Z_2 = generator_2.getZ()

            # First agent
            a_1, new_state_1 = session.run([actor_1.a,actor_1.rnn_state],
                            feed_dict={actor_1.frequency: np.array(current_f).reshape(1,1),
                                       actor_1.power: np.array(current_Z_1).reshape(1,1),
                            actor_1.state_in: state_1, actor_1.batch_size:1,actor_1.trainLength:1})
            a_1 = a_1[0,0] + epsilon*np.random.normal(0.0,0.2)

            # Second agent
            a_2, new_state_2 = session.run([actor_2.a,actor_2.rnn_state],
                            feed_dict={actor_2.frequency: np.array(current_f).reshape(1,1),
                                       actor_2.power: np.array(current_Z_2).reshape(1,1),
                            actor_2.state_in: state_2, actor_2.batch_size:1,actor_2.trainLength:1})
            a_2 = a_2[0,0] + epsilon*np.random.normal(0.0,0.2)

            # Take the action, modify environment and get the reward
            generator_1.modifyZ(a_1)
            generator_2.modifyZ(a_2)
            Z = rl.getSumZ([generator_1,generator_2])
            area.calculatePg(Z)
            area.calculateDeltaF()
            new_f = area.getDeltaF()
            r = rl.getRatioReward(new_f,generator_1.getZ(),generator_2.getZ())
            cumm_r += r

            # Store the experience and print some data
            experience = np.array([current_f,current_Z_1,current_Z_2,generator_1.getZ(),
                                   generator_2.getZ(),new_f,a_1,a_2,r])
            episodeBuffer.append(experience)
            print("Delta f: ",round(current_f,2)," P1: ",current_Z_1,
                  " P2: ",current_Z_2, " Reward: ",r)

            # Update the model each 4 steps with a minibatch of 32
            if ((j % 4) == 0) & (i > 0) & (len(buffer.buffer)>0):

                # Sample the miniBatch
                miniBatch = buffer.sample(batch,trace,n_var)

                # Reset the recurrent layer's hidden state and get states
                state_train = (np.zeros([batch,h_size]),np.zeros([batch,h_size]))
                s = np.reshape(miniBatch[:,0],[32,1])
                Z1 = np.reshape(miniBatch[:,1],[32,1])
                Z2 = np.reshape(miniBatch[:,2],[32,1])
                Z1_prime = np.reshape(miniBatch[:,3],[32,1])
                Z2_prime = np.reshape(miniBatch[:,4],[32,1])
                s_prime = np.reshape(miniBatch[:,5],[32,1])
                actions_1 = np.reshape(miniBatch[:,6],[32,1])
                actions_2 = np.reshape(miniBatch[:,7],[32,1])
                rewards = np.reshape(miniBatch[:,8],[32,1])

                # Predict the actions of both actors
                a_target_1 = session.run(actor_t_1.a, feed_dict={actor_t_1.frequency: s_prime,
                            actor_t_1.power:Z1_prime, actor_t_1.state_in: state_train,
                            actor_t_1.batch_size:batch, actor_t_1.trainLength:trace})
                a_target_2 = session.run(actor_t_2.a, feed_dict={actor_t_2.frequency: s_prime,
                            actor_t_2.power:Z2_prime, actor_t_2.state_in: state_train,
                            actor_t_2.batch_size:batch, actor_t_2.trainLength:trace})

                # Predict Q of the critics
                Q_target_1 = session.run(critic_t_1.Q,feed_dict={critic_t_1.frequency: s_prime,
                                         critic_t_1.power:Z1_prime,critic_t_1.a: a_target_1,
                                         critic_t_1.power_o:Z2_prime,
                                         critic_t_1.a_o: a_target_2,critic_t_1.trainLength:trace,
                                         critic_t_1.batch_size:batch, critic_t_1.state_in:state_train})
                Q_target_2 = session.run(critic_t_2.Q,feed_dict={critic_t_2.frequency: s_prime,
                                         critic_t_2.power:Z2_prime,critic_t_2.a: a_target_2,
                                         critic_t_2.power_o:Z1_prime,
                                         critic_t_2.a_o: a_target_1,critic_t_2.trainLength:trace,
                                         critic_t_2.batch_size:batch, critic_t_2.state_in:state_train})
                Q_target_1 = rewards + gamma*Q_target_1
                Q_target_2 = rewards + gamma*Q_target_2

                # Update the critic networks with the new Q's
                session.run(critic_1.upd,feed_dict={critic_1.frequency: s, critic_1.a: actions_1,
                            critic_1.power:Z1,critic_1.a_o: actions_2, critic_1.target_Q: Q_target_1,
                            critic_1.power_o:Z2,
                            critic_1.trainLength:trace, critic_1.batch_size:batch, critic_1.state_in:state_train})
                session.run(critic_2.upd,feed_dict={critic_2.frequency: s, critic_2.a: actions_2,
                            critic_2.power:Z2, critic_2.a_o: actions_1, critic_2.target_Q: Q_target_2,
                            critic_2.power_o:Z1,
                            critic_2.trainLength:trace, critic_2.batch_size:batch, critic_2.state_in:state_train})

                # Sample the new actions
                new_a_1 = session.run(actor_1.a, feed_dict={actor_1.frequency: s, actor_1.power:Z1,
                                      actor_1.state_in: state_train, actor_1.batch_size:batch,actor_1.trainLength:trace})
                new_a_2 = session.run(actor_2.a, feed_dict={actor_2.frequency: s, actor_2.power:Z2,
                                      actor_2.state_in: state_train,actor_2.batch_size:batch,actor_2.trainLength:trace})

                # Calculate the gradients
                gradients_1 = session.run(critic_1.critic_gradients,feed_dict={critic_1.frequency:s,
                                     critic_1.a:new_a_1,critic_1.power:Z1, critic_1.a_o:new_a_2,
                                     critic_1.trainLength:trace,critic_1.batch_size:batch,
                                     critic_1.state_in:state_train,critic_1.power_o:Z2})
                gradients_2 = session.run(critic_2.critic_gradients,feed_dict={critic_2.frequency:s,
                                     critic_2.a:new_a_2,critic_2.power:Z2, critic_2.a_o:new_a_1,
                                     critic_2.trainLength:trace,critic_2.batch_size:batch,
                                     critic_2.state_in:state_train,critic_2.power_o:Z1})
                gradients_1 = gradients_1[0]
                gradients_2 = gradients_2[0]

                # Update the actors
                session.run(actor_1.upd, feed_dict={actor_1.frequency: s, actor_1.power:Z1,
                                        actor_1.state_in: state_train,actor_1.critic_gradient:gradients_1,
                                        actor_1.batch_size:batch,actor_1.trainLength:trace})
                session.run(actor_2.upd, feed_dict={actor_2.frequency: s, actor_2.power:Z2,
                                        actor_2.state_in: state_train, actor_2.critic_gradient:gradients_2,
                                        actor_2.batch_size:batch, actor_2.trainLength:trace})

                # Update target network parameters
                session.run(actor_t_1.update_network_params)
                session.run(critic_t_1.update_network_params)
                session.run(actor_t_2.update_network_params)
                session.run(critic_t_2.update_network_params)

            # Update the state
            state_1 = new_state_1
            state_2 = new_state_2

            # Update epsilon
            epsilon = rl.getNewEpsilon(epsilon)

            # End episode if delta f is too large
            if rl.endEpisode(area.getDeltaF()):
                break

        # Append episode to the buffer
        if len(episodeBuffer) >= 8:
            buffer.add(np.array(episodeBuffer))

    # Save the model and the data gathered
    saver = tf.train.Saver()
    saver.save(session,"models/maddpg_tertiary_ratioreward")
    rl.saveData("cummulative_reward_maddpg_tertiary_ratioreward.pickle",cumm_r_list)


# Plot linear SVM classifier in the iris dataset
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets

# load iris dataset
iris = datasets.load_iris()

# Take the first two features (sepal & petal) separated to simplify.
Xsepal = iris.data[:, :2]
Xpetal = iris.data[:, 2:]
y = iris.target

# we create an instance of SVM and fit out data.
# 1 model with sepal & 1 model with petal
datas = (Xsepal, Xpetal)
models = (svm.SVC(kernel='linear', C=1.0).fit(data, y) for data in datas)
# Set-up 1x2 grid for plotting.
fig, sub = plt.subplots(1, 2)
plt.subplots_adjust(wspace=0.4, hspace=0.4)

# title for the plots
titles = ('SVC with linear kernel Sepal',
          'SVC with linear kernel Petal')
for clf, data, title, ax in zip(models, datas, titles, sub.flatten()):
    # Create points to plot in
    X0, X1 = data[:, 0], data[:, 1]
    h = .02
    x_min, x_max = X0.min() - 1, X0.max() + 1
    y_min, y_max = X1.min() - 1, X1.max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
    # Plot the decision boundaries for a classifier
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    ax.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8)
    # plot points
    ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
    ax.set_xlim(xx.min(), xx.max())
    ax.set_ylim(yy.min(), yy.max())
    ax.set_xlabel('Length')
    ax.set_ylabel('Width')
    ax.set_xticks(())
    ax.set_yticks(())
    ax.set_title(title)

plt.show()


"""
    File name: main.py
    Author: Timothy Clark
    Date created: 6/27/2021
    Date last modified: 6/27/2021
    Python Version: 3.8

    Description: this file contains all utility methods used when connecting to the Alpaca API
"""

import alpaca_trade_api as tradeapi
import constants
import datetime
import pandas as pd

api = tradeapi.REST(
    constants.API_KEY,
    constants.API_SECRET_KEY,
    constants.MARKET
)

def isTomorrowOpen():
    result = False
    date = datetime.datetime.now() + datetime.timedelta(1)
    tomorrow = date.strftime("%Y-%m-%d %H:%M:%S")
    calendar = api.get_calendar(tomorrow,tomorrow)[0]

    if str(calendar.date)[:10] == tomorrow[:10]:
        result = True

    print(result)
    return result

def isTodayOpen():
    result = False
    date = datetime.datetime.now()
    today = date.strftime("%Y-%m-%d %H:%M:%S")
    calendar = api.get_calendar(today, today)[0]

    if str(calendar.date)[:10] == today[:10]:
        result = True

    print(result)
    return result

def createTransaction(symbol, qty, side, type, tif):
    api.submit_order(symbol,qty,side,type,tif)

def getHistory():
    history= api.get_portfolio_history(period='1W', timeframe='1H',).df
    return history.drop(columns=['profit_loss', 'profit_loss_pct'])

def getEquity():
    account = api.get_account()

    equity = account.equity

    return "$"+str(equity)


import pickle
import re
import nltk
pos=negat=neu=0
def get_words_in_tweet(tweet):
   wordList = re.sub("[^\w]", " ",tweet).split()
  # print wordList
   return wordList
def get_word_features(wordlist):
    wordlist = nltk.FreqDist(wordlist)
    word_features = wordlist.keys()
    return word_features

def extract_features(document):
    document_words = set(document)
    features = {}
    for word in word_features:
        features['contains(%s)' % word] = (word in document_words)
    return features

f = open('my_classifier.pickle0089607', 'rb')
classifier = pickle.load(f)
open('classified.txt', 'w').close()
open('pre_processing.txt', 'w').close()
execfile('pre_processing.py')
File=open("twitDB46.txt")

File1=open("pre_processing.txt")
N=50

for i in range(N):
    original_line=File.next().strip()
    original_tweet=original_line
    processed_line=File1.next().strip()
    processed_tweet=processed_line
    word_features = get_word_features(get_words_in_tweet(processed_tweet))
    classified_score=classifier.classify(extract_features(processed_tweet.split()))
    score=classified_score
    if(score=='4'):
       pos=pos+1
       classified_score='POSITIVE'

    if(score=='0'):
       negat=negat+1
       classified_score='NEGATIVE'

    if(score=='2'):
       neu=neu+1
       classified_score='NEUTRAL '

    classified_Tweet= classified_score+ ":  "+original_tweet
    saveFile=open('classified.txt','a')
    saveFile.write(classified_Tweet)
    saveFile.write('\n')

posfile=open('poscount.txt','w')
posfile.write(str(pos))
posfile.close()
negfile=open('negcount.txt','w')
negfile.write(str(negat))
negfile.close()
neutralfile=open('neutralcount.txt','w')
neutralfile.write(str(neu))
neutralfile.close()
f.close();
File.close();
File1.close();
saveFile.close()


from json import *

class AudioFeatures:

  def __init__(self, json=None):
    if json != None:
      self.fromJson(json)

  def fromJson(self, json):
    self.duration_ms = json['duration_ms']
    self.danceability = json['danceability']
    self.energy = json['energy']
    self.key = json['key']
    self.loudness = json['loudness']
    self.mode = json['mode']
    self.speechiness = json['speechiness']
    self.acousticness = json['acousticness']
    self.instrumentalness = json['instrumentalness']
    self.liveness = json['liveness']
    self.valence = json['valence']
    self.tempo = json['tempo']
    self.time_signature = json['time_signature']
    self.anlysis_url = json['analysis_url']

  def toSQLInsert(self, extra_fields = {}):
    keys = ",".join(extra_fields.keys())
    columns = "(acousticness,danceability,duration_ms,energy,instrumentalness,a_key,liveness,loudness,a_mode,speechiness,tempo,time_signature,valence,"+keys+")"
    values = (self.acousticness,self.danceability,self.duration_ms,self.energy,self.instrumentalness,self.key,self.liveness, self.loudness, self.mode,self.speechiness,self.tempo,self.time_signature,self.valence) #"('"+self.id+"','"+self.href+"','"+self.name+"','"+str(self.playbackURL)+"',"+str(self.popularity)+",'"+self.albumName+"','"+self.albumImageURL+"','"+self.artistId+"','"+self.artistName+"', "+valuesextra+")"
    for x in extra_fields:
      values = values + (extra_fields[x],)

    return columns, values


# Import libraries

from flask import Flask, request, jsonify, render_template, url_for
import numpy as np
import pickle
import pickle as p
import pandas as pd

app = Flask(__name__)
# app = Flask(__name__, template_folder='app')

# Load the model
# model = pickle.load(open('Model- sav/logisticregression.pickle','rb'))
# print(model)

# Load the model
model = p.load(open('Model- sav/logisticregression.sav','rb'))
print(model)

@app.route("/")
def home():
    return render_template("index.html")

# @app.route("/api")
# def api_list():
#     """List all available api routes."""
#     return (
#         f"Available Routes:<br>"
#         f"Here goes some api"
    # )

@app.route("/model")
def model_results():
    return render_template('model.html')

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

@app.route('/results/<crime>/<location>/<season>/<hour>/<lat>/<lon>/<domestic>')
def results(crime,location,season,hour,lat,lon,domestic):
    in_data = {
                'Domestic': 0.0,
                'Latitude': 0.0,
                'Longitude': 0.0,
                'Location Description_AIRPORT': 0.0,
                'Location Description_CHURCH': 0.0,
                'Location Description_COMMERCIAL BUILDING': 0.0,
                'Location Description_CTA': 0.0,
                'Location Description_EDUCATIONAL BUILDING': 0.0,
                'Location Description_FEDERAL PROPERTY': 0.0,
                'Location Description_HOSPITAL': 0.0,
                'Location Description_HOTEL': 0.0,
                'Location Description_OTHER': 0.0,
                'Location Description_PUBLIC ENTERTAINMENT': 0.0,
                'Location Description_PUBLIC OPEN SPACE': 0.0,
                'Location Description_RESIDENCE': 0.0,
                'Location Description_VEHICLE': 0.0,
                'Primary Type_ARSON': 0.0,
                'Primary Type_ASSAULT': 0.0,
                'Primary Type_BATTERY': 0.0,
                'Primary Type_BURGLARY': 0.0,
                'Primary Type_CRIM SEXUAL ASSAULT': 0.0,
                'Primary Type_CRIMINAL DAMAGE': 0.0,
                'Primary Type_CRIMINAL SEXUAL ASSAULT': 0.0,
                'Primary Type_CRIMINAL TRESPASS': 0.0,
                'Primary Type_DECEPTIVE PRACTICE': 0.0,
                'Primary Type_GAMBLING': 0.0,
                'Primary Type_HOMICIDE': 0.0,
                'Primary Type_INTERFERENCE WITH PUBLIC OFFICER': 0.0,
                'Primary Type_INTIMIDATION': 0.0,
                'Primary Type_KIDNAPPING': 0.0,
                'Primary Type_LIQUOR LAW VIOLATION': 0.0,
                'Primary Type_MOTOR VEHICLE THEFT': 0.0,
                'Primary Type_NARCOTICS': 0.0,
                'Primary Type_OFFENSE INVOLVING CHILDREN': 0.0,
                'Primary Type_OTHER': 0.0,
                'Primary Type_OTHER OFFENSE': 0.0,
                'Primary Type_PROSTITUTION': 0.0,
                'Primary Type_PUBLIC PEACE VIOLATION': 0.0,
                'Primary Type_ROBBERY': 0.0,
                'Primary Type_SEX OFFENSE': 0.0,
                'Primary Type_STALKING': 0.0,
                'Primary Type_THEFT': 0.0,
                'Primary Type_WEAPONS VIOLATION': 0.0,
                'HOUR_Afternoon': 0.0,
                'HOUR_Evening': 0.0,
                'HOUR_Morning': 0.0,
                'HOUR_Night': 0.0,
                'SEASON_Autumn': 0.0,
                'SEASON_Spring': 0.0,
                'SEASON_Summer': 0.0,
                'SEASON_Winter': 0.0}
    # print(model)
    crime = "Primary Type_"+crime.upper()
    location = "Location Description_"+location.upper()
    season = "SEASON_"+season.title()
    hour = "HOUR_"+hour.title()
    lat = float(lat)
    lon = float(lon)
    domestic=bool(domestic)

    in_data[crime] = 1.0
    in_data[location] = 1.0
    in_data[season] = 1.0
    in_data[hour] = 1.0
    in_data['Domestic'] = domestic
    in_data['Longitude'] = lon
    in_data['Latitude'] = lat

    data = np.array([x for x in in_data.values()]).reshape(1,-1)

    print(data)

    # data = np.array([crime,location,season,hour,lat,lon,domestic]).reshape(1,-1)
    output = model.predict(data)
    prob = model.predict_proba(data)
    print(prob)

    return jsonify({'result': output.tolist(), 'prob': prob.tolist()})

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


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

# Version: 0.1
# Author: Song Huang <huangxiaohen2738@gmail.com>
# License: Copyright(c) 2015 Song.Huang
# Summary:数据表定义

table_fields = [
	#table, field, foriegn_key, foregn_table
	("user", (("id", "int", 2), ("name", "varchar", 33), ("age", "int", 2)), "", ""),
]


import numpy as np
import pandas as pd
import ast
from collections import defaultdict
import os
from scipy import stats
import datetime as dt
# from catboost import CatBoostRegressor
# from lightgbm import LGBMRegressor
import matplotlib.pyplot as plt
import seaborn as sns
import xgboost as xgb
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error

train = pd.read_csv('./input/train.csv')
test = pd.read_csv('./input/test.csv')

# Fixing values that are missing/incorrect for the train and test sets
# The data was taken from the discussions

train.loc[train['id'] == 16,'revenue'] = 192864
train.loc[train['id'] == 90,'budget'] = 30000000
train.loc[train['id'] == 118,'budget'] = 60000000
train.loc[train['id'] == 149,'budget'] = 18000000
train.loc[train['id'] == 313,'revenue'] = 12000000
train.loc[train['id'] == 451,'revenue'] = 12000000
train.loc[train['id'] == 464,'budget'] = 20000000
train.loc[train['id'] == 470,'budget'] = 13000000
train.loc[train['id'] == 513,'budget'] = 930000
train.loc[train['id'] == 797,'budget'] = 8000000
train.loc[train['id'] == 819,'budget'] = 90000000
train.loc[train['id'] == 850,'budget'] = 90000000
train.loc[train['id'] == 1007,'budget'] = 2
train.loc[train['id'] == 1112,'budget'] = 7500000
train.loc[train['id'] == 1131,'budget'] = 4300000
train.loc[train['id'] == 1359,'budget'] = 10000000
train.loc[train['id'] == 1542,'budget'] = 1
train.loc[train['id'] == 1570,'budget'] = 15800000
train.loc[train['id'] == 1571,'budget'] = 4000000
train.loc[train['id'] == 1714,'budget'] = 46000000
train.loc[train['id'] == 1721,'budget'] = 17500000
train.loc[train['id'] == 1865,'revenue'] = 25000000
train.loc[train['id'] == 1885,'budget'] = 12
train.loc[train['id'] == 2091,'budget'] = 10
train.loc[train['id'] == 2268,'budget'] = 17500000
train.loc[train['id'] == 2491,'budget'] = 6
train.loc[train['id'] == 2602,'budget'] = 31000000
train.loc[train['id'] == 2612,'budget'] = 15000000
train.loc[train['id'] == 2696,'budget'] = 10000000
train.loc[train['id'] == 2801,'budget'] = 10000000
train.loc[train['id'] == 335,'budget'] = 2
train.loc[train['id'] == 348,'budget'] = 12
train.loc[train['id'] == 470,'budget'] = 13000000
train.loc[train['id'] == 513,'budget'] = 1100000
train.loc[train['id'] == 640,'budget'] = 6
train.loc[train['id'] == 696,'budget'] = 1
train.loc[train['id'] == 797,'budget'] = 8000000
train.loc[train['id'] == 850,'budget'] = 1500000
train.loc[train['id'] == 1199,'budget'] = 5
train.loc[train['id'] == 1282,'budget'] = 9
train.loc[train['id'] == 1347,'budget'] = 1
train.loc[train['id'] == 1755,'budget'] = 2
train.loc[train['id'] == 1801,'budget'] = 5
train.loc[train['id'] == 1918,'budget'] = 592
train.loc[train['id'] == 2033,'budget'] = 4
train.loc[train['id'] == 2118,'budget'] = 344
train.loc[train['id'] == 2252,'budget'] = 130
train.loc[train['id'] == 2256,'budget'] = 1
train.loc[train['id'] == 2696,'budget'] = 10000000

test.loc[test['id'] == 6733,'budget'] = 5000000
test.loc[test['id'] == 3889,'budget'] = 15000000
test.loc[test['id'] == 6683,'budget'] = 50000000
test.loc[test['id'] == 5704,'budget'] = 4300000
test.loc[test['id'] == 6109,'budget'] = 281756
test.loc[test['id'] == 7242,'budget'] = 10000000
test.loc[test['id'] == 7021,'budget'] = 17540562
test.loc[test['id'] == 5591,'budget'] = 4000000
test.loc[test['id'] == 4282,'budget'] = 20000000
test.loc[test['id'] == 3033,'budget'] = 250
test.loc[test['id'] == 3051,'budget'] = 50
test.loc[test['id'] == 3084,'budget'] = 337
test.loc[test['id'] == 3224,'budget'] = 4
test.loc[test['id'] == 3594,'budget'] = 25
test.loc[test['id'] == 3619,'budget'] = 500
test.loc[test['id'] == 3831,'budget'] = 3
test.loc[test['id'] == 3935,'budget'] = 500
test.loc[test['id'] == 4049,'budget'] = 995946
test.loc[test['id'] == 4424,'budget'] = 3
test.loc[test['id'] == 4460,'budget'] = 8
test.loc[test['id'] == 4555,'budget'] = 1200000
test.loc[test['id'] == 4624,'budget'] = 30
test.loc[test['id'] == 4645,'budget'] = 500
test.loc[test['id'] == 4709,'budget'] = 450
test.loc[test['id'] == 4839,'budget'] = 7
test.loc[test['id'] == 3125,'budget'] = 25
test.loc[test['id'] == 3142,'budget'] = 1
test.loc[test['id'] == 3201,'budget'] = 450
test.loc[test['id'] == 3222,'budget'] = 6
test.loc[test['id'] == 3545,'budget'] = 38
test.loc[test['id'] == 3670,'budget'] = 18
test.loc[test['id'] == 3792,'budget'] = 19
test.loc[test['id'] == 3881,'budget'] = 7
test.loc[test['id'] == 3969,'budget'] = 400
test.loc[test['id'] == 4196,'budget'] = 6
test.loc[test['id'] == 4221,'budget'] = 11
test.loc[test['id'] == 4222,'budget'] = 500
test.loc[test['id'] == 4285,'budget'] = 11
test.loc[test['id'] == 4319,'budget'] = 1
test.loc[test['id'] == 4639,'budget'] = 10
test.loc[test['id'] == 4719,'budget'] = 45
test.loc[test['id'] == 4822,'budget'] = 22
test.loc[test['id'] == 4829,'budget'] = 20
test.loc[test['id'] == 4969,'budget'] = 20
test.loc[test['id'] == 5021,'budget'] = 40
test.loc[test['id'] == 5035,'budget'] = 1
test.loc[test['id'] == 5063,'budget'] = 14
test.loc[test['id'] == 5119,'budget'] = 2
test.loc[test['id'] == 5214,'budget'] = 30
test.loc[test['id'] == 5221,'budget'] = 50
test.loc[test['id'] == 4903,'budget'] = 15
test.loc[test['id'] == 4983,'budget'] = 3
test.loc[test['id'] == 5102,'budget'] = 28
test.loc[test['id'] == 5217,'budget'] = 75
test.loc[test['id'] == 5224,'budget'] = 3
test.loc[test['id'] == 5469,'budget'] = 20
test.loc[test['id'] == 5840,'budget'] = 1
test.loc[test['id'] == 5960,'budget'] = 30
test.loc[test['id'] == 6506,'budget'] = 11
test.loc[test['id'] == 6553,'budget'] = 280
test.loc[test['id'] == 6561,'budget'] = 7
test.loc[test['id'] == 6582,'budget'] = 218
test.loc[test['id'] == 6638,'budget'] = 5
test.loc[test['id'] == 6749,'budget'] = 8
test.loc[test['id'] == 6759,'budget'] = 50
test.loc[test['id'] == 6856,'budget'] = 10
test.loc[test['id'] == 6858,'budget'] =  100
test.loc[test['id'] == 6876,'budget'] =  250
test.loc[test['id'] == 6972,'budget'] = 1
test.loc[test['id'] == 7079,'budget'] = 8000000
test.loc[test['id'] == 7150,'budget'] = 118
test.loc[test['id'] == 6506,'budget'] = 118
test.loc[test['id'] == 7225,'budget'] = 6
test.loc[test['id'] == 7231,'budget'] = 85
test.loc[test['id'] == 5222,'budget'] = 5
test.loc[test['id'] == 5322,'budget'] = 90
test.loc[test['id'] == 5350,'budget'] = 70
test.loc[test['id'] == 5378,'budget'] = 10
test.loc[test['id'] == 5545,'budget'] = 80
test.loc[test['id'] == 5810,'budget'] = 8
test.loc[test['id'] == 5926,'budget'] = 300
test.loc[test['id'] == 5927,'budget'] = 4
test.loc[test['id'] == 5986,'budget'] = 1
test.loc[test['id'] == 6053,'budget'] = 20
test.loc[test['id'] == 6104,'budget'] = 1
test.loc[test['id'] == 6130,'budget'] = 30
test.loc[test['id'] == 6301,'budget'] = 150
test.loc[test['id'] == 6276,'budget'] = 100
test.loc[test['id'] == 6473,'budget'] = 100
test.loc[test['id'] == 6842,'budget'] = 30

train.head()
test.head()

# Some columns have issues with dictionaries, so those need to be adjusted
issue_cols = ['belongs_to_collection', 'genres','production_companies','production_countries',
              'spoken_languages','Keywords','cast','crew']
def fix_cols(df, cols):
    for col in cols:
        df[col] = df[col].apply(lambda x: {} if pd.isna(x) else ast.literal_eval(x))
    return df

# Applying
train = fix_cols(train, issue_cols)
test = fix_cols(test, issue_cols)

# Adjusting the collection columns
train['collection_name'] = train['belongs_to_collection'].apply(lambda x: x[0]['name'] if x!={} else '')
test['collection_name'] = test['belongs_to_collection'].apply(lambda x: x[0]['name'] if x!={} else '')
train['has_collection'] = train['collection_name'].apply(lambda x: 0 if '' else 1)
test['has_collection'] = test['collection_name'].apply(lambda x: 0 if '' else 1)

# Applying
train = train.drop(['belongs_to_collection'], axis=1)
test = test.drop(['belongs_to_collection'], axis=1)

# Fixing columns
def fixing_columns(train, test, original_col, count_col, num_to_return):
    train[original_col] = train[original_col].apply(lambda x: sorted(i['name'] for i in x) if x != {} else '')
    train[count_col] = train[original_col].apply(lambda x: len(x))

    col_list = list(train[original_col].apply(lambda x: [i for i in x]))
    col_counts = defaultdict(int)
    for row in col_list:
        for i in row:
            col_counts[i] += 1

    top = [i[0] for i in sorted(col_counts.items(), key=lambda kv: kv[1], reverse=True)[:num_to_return]]

    # One hot encoding if the production company is in the top 20 and exists in the record
    for top_var in list(top):
        train[top_var] = train[original_col].apply(lambda x: 1 if top_var in x else 0)

    # For test. Fixing the production_companies column and adding a pc count column
    test[original_col] = test[original_col].apply(lambda x: sorted(i['name'] for i in x) if x != {} else '')
    test[count_col] = test[original_col].apply(lambda x: len(x))

    # One hot encoding if the production company is in the top 20 and exists in the record
    for top_var in list(top):
        test[top_var] = test[original_col].apply(lambda x: 1 if top_var in x else 0)

number_to_keep = [20, 20, 5, 3, 10, 10, 10]
issue_cols = ['genres', 'production_companies', 'production_countries',
              'spoken_languages', 'Keywords', 'cast', 'crew']
for i in range(len(issue_cols)):
    fixing_columns(train, test, issue_cols[i], 'num_' + issue_cols[i], number_to_keep[i])
# Dropping the columns that we are not considering at this moment
drop_columns = ['genres', 'homepage', 'imdb_id', 'original_title', 'overview', 'poster_path',
                'production_companies', 'production_countries', 'spoken_languages',
                'status', 'tagline', 'title', 'Keywords', 'cast', 'crew']

# Applying
train = train.drop(drop_columns, axis=1)
test = test.drop(drop_columns, axis=1)

# Removing the N/A values and just replacing them with 0
print("Test NA: ", test.columns[test.isna().sum() > 0].tolist())
print("\nTrain NA: ", train.columns[train.isna().sum() > 0].tolist())

train[train.isna().any(axis=1)]
train[train.isna().any(axis=1)]

# Using 0 to fill the na columns
train.fillna(0, inplace=True)
test.fillna(0, inplace=True)

# Adjusting the date values
def date_adjust(df):
    # Converting release date into proper format
    df[['release_month', 'release_day', 'release_year']] = df['release_date'].str.split('/', expand=True).replace(
        np.nan, 0).astype(int)

    releaseDate = pd.to_datetime(df['release_date'])
    df['release_week'] = releaseDate.dt.week
    df['release_dayofweek'] = releaseDate.dt.dayofweek
    df['release_quarter'] = releaseDate.dt.quarter
    df['original_english'] = df['original_language'].apply(lambda x: 1 if x == 'en' else 0)
    df = df.drop(['collection_name', 'original_language', 'release_date'], axis=1)

# Applying
date_adjust(train)
date_adjust(test)

train = train.drop(['collection_name', 'original_language', 'release_date', 'id'], axis=1)
test = test.drop(['collection_name', 'original_language', 'release_date', 'id'], axis=1)

# Creating some new features based on combinations of high performing movies

def feature_transform(df):
    df['budget_runtime'] = (df['budget'] + 1) / (df['runtime'] + 1)
    df['poularity_year'] = (df['popularity'] + 1) / (df['release_year'] + 1)
    df['budget_popularity'] = (df['budget'] + 1) / (df['popularity'] + 1)
    df['budget_year'] = (df['budget'] + 1) / (df['release_year'] + 1)
    df['runtime_year'] = (df['runtime'] + 1) / (df['release_year'] + 1)

feature_transform(train)
feature_transform(test)
train['budget'] = train['budget'].apply(np.log1p)
test['budget'] = test['budget'].apply(np.log1p)
X = np.asarray(train.drop(['revenue'], axis=1))
y = np.asarray(train.revenue.apply(np.log1p))

# Training and Running an xgboost model

# Setting Parameters

params = {'objective': 'reg:linear',
          'eta': 0.01,
          'max_depth': 6,
          'min_child_weight': 3,
          'subsample': 0.8,
          'colsample_bytree': 0.8,
          'colsample_bylevel': .5,
          'gamma': 1.45,
          'eval_metric': 'rmse',
          'seed': 1,
          'silent': True}

# CatBoostRegressor
# params = {'random_seed': 0,
#          'early_stopping_rounds': 500,
#          'eval_metric': 'RMSE',
#          'logging_level': 'Silent',
#          'n_estimators': 1000,
#          'depth': hp.choice('depth', np.arange(3, 15, dtype=int)),
#          'learning_rate': hp.uniform('learning_rate', 0, 0.75),
#          'colsample_bylevel': hp.uniform('colsample_bylevel', 0.5, 1)}

# LGBMRegressor
#params = {'seed': 0,
#          'tree_learner_type': hp.choice('tree_learner_type', ['serial', 'feature', 'data', 'voting']),
#          'n_estimators': hp.choice('n_estimators', [100, 250, 500, 750, 1000]),
#          'num_leaves': hp.choice('num_leaves', [50, 100, 150, 200, 250, 500, 750, 1000]),
#          'min_data_in_leaf' : hp.choice('min_data_in_leaf', [10, 20, 30, 40, 50, 100]),
#          'max_depth': hp.choice('max_depth', np.arange(3, 15, dtype=int)),
#          'learning_rate': hp.uniform('learning_rate', 0, 0.75),}

def train_xgb(X_train, y_train, X_test, y_test):
    xgb_data = [(xgb.DMatrix(X_train, y_train), 'train'), (xgb.DMatrix(X_test, y_test), 'valid')]
    xgb_model = xgb.train(params, xgb.DMatrix(X_train, y_train),
                          5000, xgb_data,
                          verbose_eval=100,
                          early_stopping_rounds=500)
    return xgb_model

error = []
kf = list(KFold(n_splits=10, random_state=1, shuffle=True).split(X))
for i, (train_index, test_index) in enumerate(kf):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]
    # cat_model = CatBoostRegressor(**args)
    # lgb_model = LGBMRegressor(**args)
    xgb_model = train_xgb(X_train, y_train, X_test, y_test)
    error.append(xgb_model.best_score)

print("Error: {} +/- {}".format(np.mean(error), np.std(error)))

# Plotting the feature importance of Last fold
xgb_model.feature_names = train.columns.tolist()
fig, ax = plt.subplots(figsize=(20,10))
xgb.plot_importance(xgb_model, max_num_features=30,ax=ax, height=.9)
plt.show()

# Fitting on the full training set
xgb_full_train = xgb.XGBRegressor(objective = 'reg:linear',
                                  eta = 0.01,
                                  max_depth = 6,
                                  min_child_weight = 3,
                                  subsample = 0.8,
                                  colsample_bytree = 0.7,
                                  eval_metric = 'rmse',
                                  seed = 1,
                                  n_estimators = 2800)

# lgb_full_train = LGBMRegressor(n_estimators=500,
#                      min_data_in_leaf=10,
#                      learning_rate=0.0100,
#                      bagging_fraction=0.427,
#                      max_depth=7,
#                      num_leaves=250)

# cat_full_train = CatBoostRegressor(iterations=1000,
#                          eval_metric='RMSE',
#                          early_stopping_rounds=200,
#                          metric_period=None,
#                          depth=8,
#                          learning_rate=0.043,
#                          colsample_bylevel=0.153,
#                          bagging_temperature=0.063)

xgb_full_train.fit(X, y)

# lgb_full_train.fit(X, y)
# cat_full_train.fit(X, y)

y_predictions = xgb_full_train.predict(test.values)

# y_predictions = lgb_full_train.predict(test.values)
# y_predictions = cat_full_train.predict(test.values)

#train = pd.read_csv('./input/train.csv')
submission = pd.read_csv('./input/sample_submission.csv')
submission['revenue'] = np.exp(y_predictions)

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