# Listing 1

from datetime import datetime
from matplotlib import cm, pyplot
import numpy as np
from pysteps.cascade.bandpass_filters import filter_gaussian
from pysteps import io
from pysteps.io.importers import import_fmi_pgm
from pysteps.cascade.decomposition import decomposition_fft
from pysteps.utils import conversion, transformation

date       = datetime.strptime("201609281600", "%Y%m%d%H%M")
#root_path  = "/home/pysteps-data/radar/fmi"
root_path  = "/top/college/academic/ECE/spulkkin/home/ohjelmistokehitys/pySTEPS/pysteps-data/radar/fmi"
fn_pattern = "%Y%m%d%H%M_fmi.radar.composite.lowest_FIN_SUOMI1"
fn_ext     = "pgm.gz"

# find the input files from the archive
fns = io.archive.find_by_date(date, root_path, "%Y%m%d", fn_pattern, fn_ext, 5,
                              num_prev_files=9)

# read the radar composites and apply thresholding
Z, _, metadata = io.read_timeseries(fns, import_fmi_pgm, gzipped=True)
R = conversion.to_rainrate(Z, metadata, 223.0, 1.53)[0]
R = transformation.dB_transform(R, threshold=0.1, zerovalue=-15.0)[0]
R[~np.isfinite(R)] = -15.0

# construct bandpass filter and apply the cascade decomposition
filter = filter_gaussian(R.shape[1:], 7)
decomp = decomposition_fft(R[-1, :, :], filter)

# plot the normalized cascade levels
for i in range(7):
    mu = decomp["means"][i]
    sigma = decomp["stds"][i]
    decomp["cascade_levels"][i] = (decomp["cascade_levels"][i] - mu) / sigma

fig, ax = pyplot.subplots(nrows=2, ncols=4)

ax[0, 0].imshow(R[-1, :, :], cmap=cm.RdBu_r, vmin=-3, vmax=3)
ax[0, 1].imshow(decomp["cascade_levels"][0], cmap=cm.RdBu_r, vmin=-3, vmax=3)
ax[0, 2].imshow(decomp["cascade_levels"][1], cmap=cm.RdBu_r, vmin=-3, vmax=3)
ax[0, 3].imshow(decomp["cascade_levels"][2], cmap=cm.RdBu_r, vmin=-3, vmax=3)
ax[1, 0].imshow(decomp["cascade_levels"][3], cmap=cm.RdBu_r, vmin=-3, vmax=3)
ax[1, 1].imshow(decomp["cascade_levels"][4], cmap=cm.RdBu_r, vmin=-3, vmax=3)
ax[1, 2].imshow(decomp["cascade_levels"][5], cmap=cm.RdBu_r, vmin=-3, vmax=3)
ax[1, 3].imshow(decomp["cascade_levels"][6], cmap=cm.RdBu_r, vmin=-3, vmax=3)

ax[0, 0].set_title("Observed")
ax[0, 1].set_title("Level 1")
ax[0, 2].set_title("Level 2")
ax[0, 3].set_title("Level 3")
ax[1, 0].set_title("Level 4")
ax[1, 1].set_title("Level 5")
ax[1, 2].set_title("Level 6")
ax[1, 3].set_title("Level 7")

for i in range(2):
    for j in range(4):
        ax[i, j].set_xticks([])
        ax[i, j].set_yticks([])

pyplot.savefig("cascade_decomp.png", dpi=300, bbox_inches="tight")


import sqlite3
import os
from datetime import datetime

class Database:

    def __init__(self):

        file_ = ''
        files = [f for f in os.listdir() if f.endswith('.db')]

        if len(files) == 1:
            file_ = files[0]
            self.reset = False
        elif len(files) == 0:
            file_ = f'playlists_record.db'
            self.reset = True

        self.conn = sqlite3.connect(file_)
        self.cur = self.conn.cursor()

        if self.reset:
            self.cur.executescript(open('C:/Users/franc/Desktop/MP3_Player/tables.sql', 'r').read())
            self.conn.commit()

    # Register New Playlist on the Database
    def register_playlist(self, pl_name):

        self.cur.execute("SELECT * FROM Playlists WHERE P_Name = :p_name", {'p_name': pl_name})

        repeated_playlists = self.cur.fetchall()

        if len(repeated_playlists) > 0:
            return -1

        self.cur.execute("SELECT * FROM Musics WHERE P_Name = :p_name", {'p_name': pl_name})

        musics = self.cur.fetchall()

        num_musics = len(musics)

        self.cur.execute("""INSERT INTO Playlists (P_Name, N_Musics) VALUES (:name, :n_musics)""", {'name': pl_name, 'n_musics': num_musics})
        self.conn.commit()

        return 0

    # Get All Playlists on the Database
    def get_playlists(self):

        self.cur.execute("SELECT * FROM Playlists")

        playlists = self.cur.fetchall()

        return playlists

    # Delete Playlist from Database
    def delete_playlist(self, p_name):

        self.cur.execute("SELECT * FROM Playlists WHERE P_Name = :name", {'name': p_name})

        playlists = self.cur.fetchall()

        if len(playlists) == 1:

            self.cur.execute("DELETE FROM Playlists WHERE P_Name = :name", {'name': p_name})
            self.cur.execute("DELETE FROM Musics WHERE P_Name = :name", {'name': p_name})
            self.conn.commit()
            return 1

        else:
            return 0

    # Identify if is an existing Playlist
    def playlist_exists(self, p_name):

        self.cur.execute("SELECT * FROM Playlists WHERE P_Name = :name", {'name': p_name})

        playlists = self.cur.fetchall()

        if len(playlists) == 1:
            return 1

        else:
            return 0

    # Get All Musics belonging to the Playlist
    def get_musics_from_playlist(self, p_name):

        self.cur.execute("SELECT * FROM Musics WHERE P_Name = :name", {'name': p_name})

        musics = self.cur.fetchall()

        return musics

    # Check if there is any directory in the database and get it
    def DB_dir(self):

        self.cur.execute("SELECT * FROM directory")

        dir = self.cur.fetchall()

        return dir

    # Change Directory to a new one
    def change_dir(self, new_path):

        self.cur.execute("SELECT * FROM directory")

        dir = self.cur.fetchall()

        if len(dir) == 0:

            self.cur.execute("""INSERT INTO directory (folder_path) VALUES (:path)""", {'path': new_path})
            self.conn.commit()

        elif len(dir) == 1:

            self.cur.execute("UPDATE directory SET folder_path = :path WHERE oid = 1", {'path': new_path})
            self.conn.commit()

    # Add Music to Playlist
    def add_to_playlist(self, playlist, music, path):

        self.cur.execute("""INSERT INTO Musics (M_Name, M_Path, P_Name) VALUES
            (:m_name, :m_path, :p_name)""", {'m_name': music, 'm_path': path, 'p_name': playlist})

        self.conn.commit()

        self.cur.execute("SELECT N_Musics from Playlists WHERE P_Name = :p_name", {'p_name': playlist})

        result = self.cur.fetchall()

        f_result = result[0]

        num_musics = int(f_result[0]) + 1

        self.cur.execute("UPDATE Playlists SET N_Musics = :musics_n WHERE P_Name = :p_name", {'musics_n': num_musics, 'p_name': playlist})

        self.conn.commit()

    # Delete Music From Playlist
    def del_from_playlist(self, playlist, music):

        self.cur.execute("DELETE FROM Musics WHERE P_Name = :name AND M_Name = :m_name", {'name': playlist, 'm_name': music})

        self.cur.execute("SELECT N_Musics from Playlists WHERE P_Name = :p_name", {'p_name': playlist})

        result = self.cur.fetchall()

        f_result = result[0]

        num_musics = int(f_result[0]) - 1

        self.cur.execute("UPDATE Playlists SET N_Musics = :musics_n WHERE P_Name = :p_name", {'musics_n': num_musics, 'p_name': playlist})

        self.conn.commit()

    # Clear Playlist
    def clear_playlist(self, playlist):

        self.cur.execute("DELETE FROM Musics WHERE P_Name = :name", {'name': playlist})

        self.cur.execute("UPDATE Playlists SET N_Musics = 0 WHERE P_Name = :p_name", {'p_name': playlist})

        self.conn.commit()


import os
from .lookup import Lookup

class CapabilitiesLookup(Lookup):
    _LINUX_RELEASE = os.uname().release
    _KERNEL_SRC = f'/usr/src/linux-headers-{_LINUX_RELEASE}'
    _CAPABILITIES_SRC = f'{_KERNEL_SRC}/include/uapi/linux/capability.h'

    def __init__(self):
        Lookup.__init__(self)
        for line in self._capabilities_src_lines:
            if line.startswith('#define CAP_'):
                definition = line.split()
                try:
                    id_ = int(definition[2])
                    name = definition[1]
                    self._add(id_, name)
                except (KeyError, ValueError):
                    continue

    @property
    def _capabilities_src_lines(self):
        with open(self._CAPABILITIES_SRC, 'r', encoding='ascii') as src:
            return src.readlines()

    def _normalize_name(self, name: str):
        if isinstance(name, bytes):
            name = name.decode()
        name = name.lower()
        if name.startswith('cap_'):
            name = name[len('cap_'):]
        return name


import keras.backend as K
import tensorflow as tf
import numpy as np
import math

def yolo_loss(args, anchors, n_classes, ignore_thresh=0.5):
    n_layers = len(args) // 2
    y_preds = args[:n_layers]     # [B,H,W,3*(4+1+c)], level0->level2
    y_trues = args[n_layers:]     # [B,H,W,3,4+1+c],   level0->level2

    anchor_mask = [[6,7,8], [3,4,5], [0,1,2]]     # big object---> P0
    input_shape = [y_trues[0]._keras_shape[i] * 32 for i in [1,2]]
    grid_shapes = [y_trues[i]._keras_shape[1:3] for i in range(3)]

    loss = 0.
    xy_loss_, wh_loss_, ciou_loss_, conf_loss_, cls_loss_, = 0., 0., 0., 0., 0.
    m = K.shape(y_preds[0])[0]     # batch size
    mf = K.cast(m, K.dtype(y_preds[0]))         # batch size to float
    for i in range(n_layers):
        anchors_l = anchors[anchor_mask[i]]

        conf_gt = y_trues[i][...,4:5]
        cls_gt = y_trues[i][...,5:]

        feats = K.reshape(y_preds[i], (-1, grid_shapes[i][0], grid_shapes[i][1], len(anchors_l), 4+1+n_classes))
        grid_coords, pred_xy, pred_wh, pred_conf, pred_cls = bbox(feats, anchors_l, n_classes, input_shape)     # normed
        pred_box = K.concatenate([pred_xy, pred_wh])       # normed

        conf_gt = y_trues[i][...,4:5]
        cls_gt = y_trues[i][...,5:]
        xy_gt = y_trues[i][...,:2] * grid_shapes[i][::-1] - grid_coords        # offset to grid
        wh_gt = K.log(y_trues[i][...,2:4] * input_shape[::-1] / anchors_l)     # offset to grid
        # wh being too small would cause log(0)=-inf, in this case replace the infs with 0
        wh_gt = K.switch(conf_gt, wh_gt, K.zeros_like(wh_gt))
        box_loss_scale = 2 - y_trues[i][...,2:3]*y_trues[i][...,3:4]

        # box_loss: xy_loss+wh_loss / iou loss
        # xy_loss: bce, based on grid center
        xy_loss = conf_gt * box_loss_scale * K.binary_crossentropy(xy_gt, feats[...,0:2], from_logits=True)
        # wh_loss: l2, based on anchor shape
        wh_loss = conf_gt * box_loss_scale * 0.5 * K.square(wh_gt-feats[...,2:4])
        # ciou_loss: iou
        ciou = tf.expand_dims(bbox_ciou(y_trues[i][...,:4], pred_box, grid_coords, grid_shapes[i], input_shape, anchors_l), axis=-1)
        ciou_loss = conf_gt * box_loss_scale * (1 - ciou)

        # conf_loss: bce
        # ignore mask: objects on gt mask which has iou<ignore_thresh with anchors
        ignore_mask = tf.TensorArray(K.dtype(y_trues[0]), size=1, dynamic_size=True)  # 动态size数组
        object_mask = tf.cast(conf_gt, tf.bool)
        def loop_body(b, ignore_mask):
            true_box = tf.boolean_mask(y_trues[i][b,...,0:4], object_mask[b,...,0])   # flattened(h*w*a*mask) gt boxes for current sample [N,4]
            iou = box_iou(pred_box[b], true_box)     # [H,W,a,N]
            best_iou = K.max(iou, axis=-1, keepdims=True)     # [H,W,a,1]
            ignore_mask = ignore_mask.write(b, K.cast(best_iou<ignore_thresh, K.dtype(true_box)))
            return b+1, ignore_mask
        _, ignore_mask = tf.while_loop(lambda b,*args: b<m, loop_body, [0, ignore_mask])
        ignore_mask = ignore_mask.stack()        # # [b,H,W,a,1]
        conf_loss = conf_gt * K.binary_crossentropy(conf_gt, feats[...,4:5], from_logits=True)+ \
                    (1-conf_gt) * ignore_mask* K.binary_crossentropy(conf_gt, feats[...,4:5], from_logits=True)

        # cls_loss: bce
        cls_loss = conf_gt * K.binary_crossentropy(cls_gt, feats[...,5:], from_logits=True)

        # xy_loss_ += K.sum(xy_loss) / mf
        # wh_loss_ += K.sum(wh_loss) / mf
        ciou_loss_ += K.sum(ciou_loss_) / mf
        conf_loss_ += K.sum(conf_loss) / mf
        cls_loss_ += K.sum(cls_loss) / mf

    # loss = xy_loss_ + wh_loss_ + conf_loss_ + cls_loss_
    loss = ciou_loss_ + conf_loss_ + cls_loss_

    # return loss
    return tf.stack([loss, xy_loss_, wh_loss_, conf_loss_, cls_loss_], axis=0)

# for each level, offset outputs to normed outputs, logit outputs to posiblity outputs
def bbox(feats, anchors, n_classes, input_shape):
    # feats: [B,H,W,3,4+1+c]
    # anchors: anchors for current level, [3,2] arr
    n_anchors = len(anchors)
    anchors_tensor = K.reshape(K.constant(anchors), (1,1,1,n_anchors,2))

    grid_shape = K.int_shape(feats)[1:3]
    h, w = grid_shape
    x, y = np.meshgrid(np.arange(0, w), np.arange(0, h))
    grid_coords = np.stack([x, y], axis=-1).astype(np.float32)
    grid_coords = K.expand_dims(grid_coords, axis=-2)     # expand dim for anchors dim

    box_xy = (K.sigmoid(feats[...,:2]) + grid_coords) / tf.constant(grid_shape[::-1], dtype=tf.float32)
    box_wh = anchors_tensor * K.exp(feats[...,2:4]) / tf.constant(input_shape[::-1], dtype=tf.float32)
    box_conf = K.sigmoid(feats[...,4:5])
    box_cls = K.sigmoid(feats[...,5:])

    return grid_coords, box_xy, box_wh, box_conf, box_cls

# for each level, for each grid, calculate iou with all gt boxes
def box_iou(b1, b2):
    # b1: normed_pred: [H,W,a,4]
    # b2: gt: [N,4]
    # return: [H,W,a,N]
    # Expand dim to apply broadcasting.
    b1 = K.expand_dims(b1, -2)       # [H,W,a,1,4]
    b1_xy = b1[..., :2]
    b1_wh = b1[..., 2:4]
    b1_wh_half = b1_wh/2.
    b1_mins = b1_xy - b1_wh_half
    b1_maxes = b1_xy + b1_wh_half     # [H,W,a,1,2]

    # Expand dim to apply broadcasting.
    b2 = K.expand_dims(b2, 0)        # [1,N,4]
    b2_xy = b2[..., :2]
    b2_wh = b2[..., 2:4]
    b2_wh_half = b2_wh/2.
    b2_mins = b2_xy - b2_wh_half
    b2_maxes = b2_xy + b2_wh_half      # [1,N,2]

    intersect_mins = K.maximum(b1_mins, b2_mins)
    intersect_maxes = K.minimum(b1_maxes, b2_maxes)
    intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.)
    intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1]
    b1_area = b1_wh[..., 0] * b1_wh[..., 1]
    b2_area = b2_wh[..., 0] * b2_wh[..., 1]
    iou = intersect_area / (b1_area + b2_area - intersect_area)

    return iou

def bbox_ciou(gt_xywh, pred_xywh, grid_coords, grid_shape, input_shape, anchors):
    # ciou = iou - p2/c2 - av

    # convert to abs xywh    [b,h,w,3,4]
    gt_xy = gt_xywh[...,:2] * input_shape[::-1]
    gt_wh = gt_xywh[...,2:] * input_shape[::-1]
    boxes1 = K.concatenate([gt_xy, gt_wh], axis=-1)
    pred_xy = (pred_xywh[...,:2] + grid_coords) / grid_shape[::-1] * input_shape[::-1]
    pred_wh = pred_xywh[...,2:] * input_shape[::-1]
    boxes2 = K.concatenate([pred_xy, pred_wh], axis=-1)

    # 变成左上角坐标、右下角坐标
    boxes1_x0y0x1y1 = tf.concat([boxes1[..., :2] - boxes1[..., 2:] * 0.5,
                                 boxes1[..., :2] + boxes1[..., 2:] * 0.5], axis=-1)
    boxes2_x0y0x1y1 = tf.concat([boxes2[..., :2] - boxes2[..., 2:] * 0.5,
                                 boxes2[..., :2] + boxes2[..., 2:] * 0.5], axis=-1)
    # 避免一开始w h 是负数，导致x0y0成了右下角坐标，x1y1成了左上角坐标。
    boxes1_x0y0x1y1 = tf.concat([tf.minimum(boxes1_x0y0x1y1[..., :2], boxes1_x0y0x1y1[..., 2:]),
                                 tf.maximum(boxes1_x0y0x1y1[..., :2], boxes1_x0y0x1y1[..., 2:])], axis=-1)
    boxes2_x0y0x1y1 = tf.concat([tf.minimum(boxes2_x0y0x1y1[..., :2], boxes2_x0y0x1y1[..., 2:]),
                                 tf.maximum(boxes2_x0y0x1y1[..., :2], boxes2_x0y0x1y1[..., 2:])], axis=-1)

    # 两个矩形的面积
    boxes1_area = (boxes1_x0y0x1y1[..., 2] - boxes1_x0y0x1y1[..., 0]) * (boxes1_x0y0x1y1[..., 3] - boxes1_x0y0x1y1[..., 1])
    boxes2_area = (boxes2_x0y0x1y1[..., 2] - boxes2_x0y0x1y1[..., 0]) * (boxes2_x0y0x1y1[..., 3] - boxes2_x0y0x1y1[..., 1])

    # 相交矩形的左上角坐标、右下角坐标，shape 都是 (8, 13, 13, 3, 2)
    left_up = tf.maximum(boxes1_x0y0x1y1[..., :2], boxes2_x0y0x1y1[..., :2])
    right_down = tf.minimum(boxes1_x0y0x1y1[..., 2:], boxes2_x0y0x1y1[..., 2:])

    # 相交矩形的面积inter_area。iou
    inter_section = tf.maximum(right_down - left_up, 0.0)
    inter_area = inter_section[..., 0] * inter_section[..., 1]
    union_area = boxes1_area + boxes2_area - inter_area
    iou = inter_area / (union_area + 1e-9)

    # 包围矩形的左上角坐标、右下角坐标，shape 都是 (8, 13, 13, 3, 2)
    enclose_left_up = tf.minimum(boxes1_x0y0x1y1[..., :2], boxes2_x0y0x1y1[..., :2])
    enclose_right_down = tf.maximum(boxes1_x0y0x1y1[..., 2:], boxes2_x0y0x1y1[..., 2:])

    # 包围矩形的对角线的平方
    enclose_wh = enclose_right_down - enclose_left_up
    enclose_c2 = K.pow(enclose_wh[..., 0], 2) + K.pow(enclose_wh[..., 1], 2)

    # 两矩形中心点距离的平方
    p2 = K.pow(boxes1[..., 0] - boxes2[..., 0], 2) + K.pow(boxes1[..., 1] - boxes2[..., 1], 2)

    # 增加av。加上除0保护防止nan。
    atan1 = tf.atan(boxes1[..., 2] / (boxes1[..., 3] + 1e-9))
    atan2 = tf.atan(boxes2[..., 2] / (boxes2[..., 3] + 1e-9))
    v = 4.0 * K.pow(atan1 - atan2, 2) / (math.pi ** 2)
    a = v / (1 - iou + v)

    ciou = iou - 1.0 * p2 / enclose_c2 - 1.0 * a * v
    return ciou


import sys

N,K = map(int, sys.stdin.readline().split())

MOD = 1000000007

N_pac, K_pac, N_K_pac = 0,0,0
temp = 1
for i in range(0,N+1):
    temp = (temp * i if i!=0 else 1) % MOD
    if i==(N-K):
        N_K_pac = temp
    if i==K:
        K_pac = temp
    if i==N:
        N_pac = temp

def new_pow(a,b,MOD): #calculate a^b mod MOD
    temp = a
    ans = 1
    for char in bin(b)[2:][::-1]:
        if char=="1": ans = (ans * temp) % MOD
        temp = (temp**2) % MOD
    return ans
print((N_pac * new_pow(N_K_pac*K_pac,MOD-2,MOD))%MOD)


class Student_Record():
	def _init_(self, Name, Course, Roll_num, Age):
		self.Name = Name
		self.Course = Course
		self.Roll_num = Roll_num
		self.Age = Age

	def Display(self):
		print(self.Name,end='\t\t')
		print(self.Course,end='\t\t')
		print(self.Roll_num,end='\t\t')
		print(self.Age,end)

student = list()
n = int(input("Enter the number of student:"))
print("Student Detail's")
for i in range(n):
	print("Student : ", i+1)
	Name=input("\tName: ")
	Course=input("\tCourse: ")
	Roll_num=input("\tRoll_num: ")
	Age=input("\tAge: ")
	student.append(Student_Record(Name, Course, Roll_num, Age))

for i in range(n):
    for j in range(i+1,n):
        if(student[i].Name>student[j].Name):
            temp=student[i]
            student[i]=student[j]
            student[j]=temp

print("Student Information")
print("Name\t\tCourse\tRoll_num\tAge")

for i in range(n):
	student.Display()


import json
from tornado.web import RequestHandler
from database import db
from models import UserModel

# other handlers should extend this one.
# this (should) allow cross origin requests from our client
# thanks to: https://stackoverflow.com/a/40431557/7220351
class Handler(RequestHandler):
    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Headers", "x-requested-with, Content-Type, Cache-Control")
        self.set_header('Access-Control-Allow-Methods', 'PUT, DELETE, OPTIONS')

    def options(self):
        # no body
        self.set_status(204)
        self.finish()

def send_error(handler, status, msg):
    handler.write({
        'error': msg
    })
    handler.set_status(status)

# route at "/"
class Base(Handler):
    def get(self):
        self.write("API is running.")

class GetUsers(Handler):
    def get(self):
        users = db.get_results("SELECT * FROM users ORDER BY id")
        self.write({"payload": users})

class AddUser(Handler):
    def post(self):

        # not a good way to handle json request data,
        # but not sure how to set it up correctly in tornado
        body = json.loads(self.request.body)

        user = UserModel(
            None,
            body.get('username', ''),
            body.get('first_name', ''),
            body.get('last_name', ''),
            body.get('date_of_birth', ''),
        )

        # handling no username is done below
        if user.username:

            sql = "SELECT * FROM users WHERE username = ?"
            results = db.get_results(sql, [str(user.username)])

            if len(results):
                send_error(self, 200, "Username is already taken.")
                return

        # check req fields etc.
        valid, msg = user.validate()

        if not valid:
            send_error(self, 200, msg)
            return

        try:
            result = user.insert()
        except:
            send_error(self, 200, "Unknown Error.")
            return

        self.write({
            "success": True,
            "payload": result
        })

class UpdateUser(Handler):
    def post(self):

        body = json.loads(self.request.body)

        userDict = UserModel.get_by_id(body.id)

        if userDict is False:
            send_error(self, 200, "User not found.")

        user = UserModel(
            userDict.get('id'),
            body.get('username', ''),
            body.get('first_name', ''),
            body.get('last_name', ''),
            body.get('date_of_birth', ''),
        )

        valid, msg = user.validate()

        if not valid:
            send_error(self, 200, msg)
            return

        try:
            result = user.update()
        except:
            send_error(self, 200, "Unknown Error.")
            return

        self.write({
            "success": True,
            "payload": result
        })

class DeleteUsers(Handler):

    # deletes multiple users by ID
    def delete(self):
        # ie. ?ids=2,5,20
        ids = self.get_query_argument('ids', '').split(',')
        ids = map(lambda x: int(x) if x.isdigit() else False, ids)
        ids = filter(bool, ids)
        ids = list(ids)

        # no need to treat this as error IMO. Nothing needs to be done.
        if not len(ids):
            self.write({
                "success": True,
            })
            return

        # ie. "?, ?, ?"
        placeholders = ",".join(map(lambda x: '?', ids))

        # ie. "... where id IN (?, ?, ?)"
        sql = '''
               DELETE FROM users WHERE
               id IN ({str})
               '''.format(str=placeholders)

        # not bothering with try/catch for now.
        cursor = db.conn.execute(sql, ids)
        db.conn.commit()

        self.write({
            "success": True
        })

routes = [
    ("/", Base),
    ("/api/users", GetUsers),
    ("/api/users/add", AddUser),
    ("/api/users/update", UpdateUser),
    ("/api/users/delete", DeleteUsers),
]


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""dftimewolf main entrypoint."""

import argparse
import logging
# Some AttributeErrors occurred when trying to access logging.handlers, so
# we import them separately
from logging import handlers
import os
import signal
import sys

# Make dftimewolf faster by only importing modules if we're not actually
# just asking for help
_ASKING_FOR_HELP = '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) < 2

# pylint: disable=wrong-import-position
from dftimewolf import config

from dftimewolf.lib import errors
from dftimewolf.lib import utils

if not _ASKING_FOR_HELP:
  # Import the collector modules.
  # These will be registered automatically upon import
  # pylint: disable=unused-import
  from dftimewolf.lib import collectors
  from dftimewolf.lib.collectors import azure
  from dftimewolf.lib.preflights import cloud_token
  from dftimewolf.lib.preflights import ssh_multiplexer
  from dftimewolf.lib.preflights import sanity_checks
  from dftimewolf.lib.collectors import aws
  from dftimewolf.lib.collectors import filesystem
  from dftimewolf.lib.collectors import gcloud
  from dftimewolf.lib.collectors import gcp_logging
  from dftimewolf.lib.collectors import grr_hosts
  from dftimewolf.lib.collectors import grr_hunt
  from dftimewolf.lib.enhancers import timesketch as timesketch_enhancer
  from dftimewolf.lib.exporters import gce_disk_export
  from dftimewolf.lib.exporters import local_filesystem
  from dftimewolf.lib.exporters import scp_ex
  from dftimewolf.lib.exporters import timesketch
  from dftimewolf.lib.processors import gcp_logging_timesketch
  from dftimewolf.lib.processors import grepper
  from dftimewolf.lib.processors import localplaso
  from dftimewolf.lib.processors import turbinia_artifact
  from dftimewolf.lib.processors import turbinia_gcp

from dftimewolf.lib.recipes import manager as recipes_manager
from dftimewolf.lib.state import DFTimewolfState
from dftimewolf.lib import logging_utils

logger = logging.getLogger('dftimewolf')

class DFTimewolfTool(object):
  """DFTimewolf tool."""

  _DEFAULT_DATA_FILES_PATH = os.path.join(
      os.sep, 'usr', 'local', 'share', 'dftimewolf')

  def __init__(self):
    """Initializes a DFTimewolf tool."""
    super(DFTimewolfTool, self).__init__()
    self._command_line_options = None
    self._data_files_path = None
    self._recipes_manager = recipes_manager.RecipesManager()
    self._recipe = None
    self._state = None

    self._DetermineDataFilesPath()

  @property
  def state(self):
    """Returns the internal state object."""
    return self._state

  def _AddRecipeOptions(self, argument_parser):
    """Adds the recipe options to the argument group.

    Args:
      argument_parser (argparse.ArgumentParser): argparse argument parser.
    """
    subparsers = argument_parser.add_subparsers()

    for recipe in self._recipes_manager.GetRecipes():
      description = recipe.description
      subparser = subparsers.add_parser(
          recipe.name, formatter_class=utils.DFTimewolfFormatterClass,
          description=description)
      subparser.set_defaults(recipe=recipe.contents)

      for switch, help_text, default in recipe.args:
        if isinstance(default, bool):
          subparser.add_argument(switch, help=help_text, default=default,
                                 action='store_true')
        else:
          subparser.add_argument(switch, help=help_text, default=default)

      # Override recipe defaults with those specified in Config
      # so that they can in turn be overridden in the commandline
      subparser.set_defaults(**config.Config.GetExtra())

  def _DetermineDataFilesPath(self):
    """Determines the data files path.

    Data path is specified in the DFTIMEWOLF_DATA environment variable. If the
    variable is not specified, dfTimewolf checks if any of the following
    locations are valid:

    * Cloned repository base
    * Local package data files
    * sys.prefix
    * Hardcoded default /usr/local/share
    """

    data_files_path = os.environ.get('DFTIMEWOLF_DATA')

    if not data_files_path or not os.path.isdir(data_files_path):
      # Figure out if the script is running out of a cloned repository
      data_files_path = os.path.realpath(__file__)
      data_files_path = os.path.dirname(data_files_path)
      data_files_path = os.path.dirname(data_files_path)
      data_files_path = os.path.dirname(data_files_path)
      data_files_path = os.path.join(data_files_path, 'data')

      # Use local package data files (python setup.py install)
      if not os.path.isdir(data_files_path):
        data_files_path = os.path.dirname(data_files_path)
        data_files_path = os.path.join(data_files_path, 'share', 'dftimewolf')

      # Use sys.prefix for user installs (e.g. pip install ...)
      if not os.path.isdir(data_files_path):
        data_files_path = os.path.join(sys.prefix, 'share', 'dftimewolf')

      # If all else fails, fall back to hardcoded default
      if not os.path.isdir(data_files_path):
        logger.debug('{0:s} not found, defaulting to /usr/local/share'.format(
            data_files_path))
        data_files_path = self._DEFAULT_DATA_FILES_PATH

    logger.debug("Recipe data path: {0:s}".format(data_files_path))
    self._data_files_path = data_files_path

  def _GenerateHelpText(self):
    """Generates help text with alphabetically sorted recipes.

    Returns:
      str: help text.
    """
    recipes = self._recipes_manager.GetRecipes()
    if not recipes:
      help_text = '\nNo recipes found.'
    else:
      help_text = '\nAvailable recipes:\n\n'
      for recipe in recipes:
        short_description = recipe.contents.get(
            'short_description', 'No description')
        help_text += ' {0:<35s}{1:s}\n'.format(recipe.name, short_description)

    return help_text

  def _LoadConfigurationFromFile(self, configuration_file_path):
    """Loads a configuration from file.

    Args:
      configuration_file_path (str): path of the configuration file.
    """
    try:
      if config.Config.LoadExtra(configuration_file_path):
        logger.debug('Configuration loaded from: {0:s}'.format(
            configuration_file_path))

    except errors.BadConfigurationError as exception:
      logger.warning('{0!s}'.format(exception))

  def LoadConfiguration(self):
    """Loads the configuration.

    The following paths are tried. Values loaded last take precedence.

    * <_data_files_path>/config.json
    * /etc/dftimewolf.conf
    * /usr/share/dftimewolf/dftimewolf.conf
    * ~/.dftimewolfrc
    * If set, wherever the DFTIMEWOLF_CONFIG environment variable points to.

    """
    configuration_file_path = os.path.join(self._data_files_path, 'config.json')
    self._LoadConfigurationFromFile(configuration_file_path)

    configuration_file_path = os.path.join('/', 'etc', 'dftimewolf.conf')
    self._LoadConfigurationFromFile(configuration_file_path)

    configuration_file_path = os.path.join(
        '/', 'usr', 'share', 'dftimewolf', 'dftimewolf.conf')
    self._LoadConfigurationFromFile(configuration_file_path)

    user_directory = os.path.expanduser('~')
    configuration_file_path = os.path.join(user_directory, '.dftimewolfrc')
    self._LoadConfigurationFromFile(configuration_file_path)

    env_config = os.environ.get('DFTIMEWOLF_CONFIG')
    if env_config:
      self._LoadConfigurationFromFile(env_config)

  def ParseArguments(self, arguments):
    """Parses the command line arguments.

    Args:
      arguments (list[str]): command line arguments.

    Raises:
      CommandLineParseError: If arguments could not be parsed.
    """
    help_text = self._GenerateHelpText()

    argument_parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=help_text)

    self._AddRecipeOptions(argument_parser)

    self._command_line_options = argument_parser.parse_args(arguments)

    if not getattr(self._command_line_options, 'recipe', None):
      error_message = '\nPlease specify a recipe.\n' + help_text
      raise errors.CommandLineParseError(error_message)

    self._recipe = self._command_line_options.recipe

    self._state = DFTimewolfState(config.Config)
    logger.info('Loading recipe {0:s}...'.format(self._recipe['name']))
    # Raises errors.RecipeParseError on error.
    self._state.LoadRecipe(self._recipe)

    number_of_modules = len(self._recipe['modules'])
    logger.info('Loaded recipe {0:s} with {1:d} modules'.format(
        self._recipe['name'], number_of_modules))

    self._state.command_line_options = vars(self._command_line_options)

  def RunPreflights(self):
    """Runs preflight modules."""
    logger.info('Running preflights...')
    self._state.RunPreflights()

  def ReadRecipes(self):
    """Reads the recipe files."""
    if os.path.isdir(self._data_files_path):
      recipes_path = os.path.join(self._data_files_path, 'recipes')
      if os.path.isdir(recipes_path):
        self._recipes_manager.ReadRecipesFromDirectory(recipes_path)

  def RunModules(self):
    """Runs the modules."""
    logger.info('Running modules...')
    self._state.RunModules()
    logger.info('Recipe {0:s} executed successfully!'.format(
        self._recipe['name']))

  def SetupModules(self):
    """Sets up the modules."""
    # TODO: refactor to only load modules that are used by the recipe.

    logger.info('Setting up modules...')
    self._state.SetupModules()
    logger.info('Modules successfully set up!')

  def CleanUpPreflights(self):
    """Calls the preflight's CleanUp functions."""
    self._state.CleanUpPreflights()

def SignalHandler(*unused_argvs):
  """Catches Ctrl + C to exit cleanly."""
  sys.stderr.write("\nCtrl^C caught, bailing...\n")
  sys.exit(0)

def SetupLogging():
  """Sets up a logging handler with dftimewolf's custom formatter."""
  # Clear root handlers (for dependencies that are setting them)
  root_log = logging.getLogger()
  root_log.handlers = []

  # Add a silent default stream handler, this is automatically set
  # when other libraries call logging.info() or similar methods.
  root_handler = logging.StreamHandler()
  root_handler.addFilter(lambda x: False)
  root_log.addHandler(root_handler)

  # We want all DEBUG messages and above.
  # TODO(tomchop): Consider making this a parameter in the future.
  logger.setLevel(logging.DEBUG)

  # File handler needs go be added first because it doesn't format messages
  # with color
  file_handler = handlers.RotatingFileHandler(
      logging_utils.DEFAULT_LOG_FILE,
      maxBytes=logging_utils.MAX_BYTES,
      backupCount=logging_utils.BACKUP_COUNT)
  file_handler.setFormatter(logging_utils.WolfFormatter(colorize=False))
  logger.addHandler(file_handler)

  console_handler = logging.StreamHandler()
  colorize = not bool(os.environ.get('DFTIMEWOLF_NO_RAINBOW'))
  console_handler.setFormatter(logging_utils.WolfFormatter(colorize=colorize))
  logger.addHandler(console_handler)
  logger.debug(
      'Logging to stdout and {0:s}'.format(logging_utils.DEFAULT_LOG_FILE))

def Main():
  """Main function for DFTimewolf.

  Returns:
    bool: True if DFTimewolf could be run successfully, False otherwise.
  """
  SetupLogging()

  version_tuple = (sys.version_info[0], sys.version_info[1])
  if version_tuple[0] != 3 or version_tuple < (3, 6):
    logger.critical(('Unsupported Python version: {0:s}, version 3.6 or higher '
                     'required.').format(sys.version))
    return False

  tool = DFTimewolfTool()

  # TODO: log errors if this fails.
  tool.LoadConfiguration()

  try:
    tool.ReadRecipes()
  except (KeyError, errors.RecipeParseError) as exception:
    logger.critical('{0!s}'.format(exception))
    return False

  try:
    tool.ParseArguments(sys.argv[1:])
  except (errors.CommandLineParseError, errors.RecipeParseError) as exception:
    sys.stderr.write('{0!s}'.format(exception))
    return False

  tool.state.LogExecutionPlan()

  tool.RunPreflights()

  # TODO: log errors if this fails.
  tool.SetupModules()

  # TODO: log errors if this fails.
  tool.RunModules()

  tool.CleanUpPreflights()

  return True

if __name__ == '__main__':
  signal.signal(signal.SIGINT, SignalHandler)
  if Main():
    sys.exit(0)
  else:
    sys.exit(1)


from aiogram import Bot
from aiogram.types import Message
from aiogram.fsm.context import FSMContext

from ...enums import MediaPosition
from ...states import PostState
from ...utils.send_message import send_message
from ...utils.send_panel import send_panel
from ...utils.media import update_media, set_media
from ...utils.telegraph import Telegraph

async def parse_media(
        message: Message,
        state: FSMContext,
        bot: Bot
):
    chat_id = message.chat.id
    state_name = await state.get_state()

    if state_name == "PostState:message":
        await set_media(state, message)
    elif state_name == "PostState:edit_media" or state_name == "PostState:edit":
        state_data = await state.get_data()
        file_id = message.photo[-1].file_id

        if state_data.get("media_position") == MediaPosition.UP:
            await update_media(
                state=state,
                photo=file_id,
            )
            await state.update_data({
                "caption": state_data.get("text"),
                "text": None
            })
        elif state_data.get("media_position") == MediaPosition.DOWN:
            telegraph = Telegraph(bot=bot)
            media_url = await telegraph.upload_file_from_telegram(file_id=file_id)
            await state.update_data({
                "media_url": media_url
            })

    await send_message(
        chat_id=chat_id,
        state=state,
        bot=bot
    )

    await send_panel(
        chat_id=chat_id,
        state=state,
        bot=bot
    )

    await state.set_state(PostState.edit)


"""
library of character-based RNN
@Author Chanwoo Gwon, Yonsei Univ. Researcher, since 2020.05. ~
@Date 2020.10.22
"""

import tensorflow as tf
import os
import numpy as np
import random

from model.core import ModelCore, LOSS, Net
from ko.character.spelling import HanJaMo

class CharRNN(ModelCore):
    """
    RNN Library Class

    1. Train and Predict using RNN based on Character
    2. Could be used like the encoder
    3. Can many-to-many or many-to-one RNN
    """
    def __init__(self, data_path, emb=100, loss=LOSS.SPARSE_CATEGORICAL_CROSSENTROPY, is_classify=False):

        self._time_step = 0
        self._text_set = None
        self._emb = emb
        self._char_set = []
        self._char2idx = None
        self._idx2char = None
        self._text_as_int = None
        self._last_dim = 0

        super().__init__(data_path, loss=loss, is_classify=is_classify)

    def read_data(self):
        char_path = os.path.join(self._data_path, 'data.txt')

        self._text_set = open(char_path, 'rb').read().decode(encoding='utf-8')

        self._make_word_world()

    def _make_word_world(self):
        self._set_char()

    def _set_char(self, system_word_list=None):
        self._char_set = sorted(set(self._text_set))

        if system_word_list is not None:
            self._char_set = self._char_set + system_word_list

        self._char2idx = {u: i for i, u in enumerate(self._char_set)}
        self._idx2char = np.array(self._char_set)

    def build_model(self):
        super().build_model()

    def to_vector(self, sequence, add_margin=True):
        if add_margin:
            if len(sequence) < self._time_step:
                sequence = "{0}{1}".format(sequence, ''.join([' '] * (self._time_step - len(sequence))))
        return [self._char2idx[c] for c in sequence]

    def to_text(self, sequence):
        return repr("".join(self._idx2char[sequence]))


# Escreva um programa que permita manipular uma lista de inteiros. Inicialmente o programa deve ler os inteiros separados por espaço em branco e armazenar em uma lista. Dado que os valores inteiros estão armazenados, o programa deve aceitar 4 comandos: inserir, remover, parcial e final. O comando inserir é acompanhado na mesma linha do valor inteiro a ser inserido na lista. Da mesma forma, o comando remover é acompanhado do valor a ser removido da lista. Já o comando parcial deve fazer com que os dados contidos na lista sejam impressos na tela, separados por espaços em branco e dispostos em ordem crescente(numérica). O mesmo deve fazer o comando final, exceto que este encerra a execução do programa.

def main():
    # recebe valores
    numbers = list(map(int, input().split()))

    while True:
        command = input().split()

        if command[0] == 'inserir':
            numbers.append(int(command[1]))
        if command[0] == 'remover':
            del numbers[numbers.index(int(command[1]))]
        if command[0] == 'parcial':
            numbers.sort()
            print(' '.join(list(map(str, numbers))))
        if command[0] == 'final':
            numbers.sort()
            print(' '.join(list(map(str, numbers))))
            break

    return

main()


from selenium.webdriver.common.by import By
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver_path = "C:\operadriver.exe"

driver = webdriver.Chrome(driver_path)
driver.get("https://tinder.com/")
time.sleep(3)

login = driver.find_element(by=By.LINK_TEXT, value='Log in')
login.click()
time.sleep(2)
cookie = driver.find_element(by=By.XPATH, value='//*[@id="c849239686"]/div/div[2]/div/div/div[1]/div[1]/button')
cookie.click()
google = driver.find_element(by=By.XPATH, value='//*[@id="c-879141390"]/div/div/div[1]/div/div/div[3]/span/div[3]/button')
google.click()
time.sleep(20)
number = driver.find_element(by=By.XPATH, value='//*[@id="c-879141390"]/div/div/div[1]/div/div[2]/div/input')
number.send_keys("your number")
time.sleep(8)
submit = driver.find_element(by=By.XPATH, value='//*[@id="c-879141390"]/div/div/div[1]/div/button')
submit.click()
time.sleep(10)
conti = driver.find_element(by=By.XPATH, value='//*[@id="c-879141390"]/div/div/div[1]/div/button')
conti.click()
for clic in range(101):
    like = driver.find_element(by=By.XPATH,value='//*[@id="c849239686"]/div/div[1]/div/main/div[1]/div/div/div[1]/div[1]/div/div[4]/div/div[4]/button/span/span/svg/path')
    like.click()
    time.sleep(1)


"""
    Get sensor data from IMU, current sensor, microphone.
    Perform Pre-processing.
    Obtain a single pre-processed tensor.
"""
import pandas as pd
import get_IMU_current
import time

try:
    data_getter = get_IMU_current.Current_IMU()
except RuntimeError as e:
    print("Could not start IMU")
    exit()

class Preprocess:
    """
    Get live raw data, calculate statistics, output one tensor to send for inference
    """

    def __init__(self):
        self.sensors = ['accX', 'accY', 'accZ', 'wx', 'wy',
                        'wz', 'bx', 'by', 'bz', 'Isens', 'Srms']
        self.stats = ['min', 'max', 'mean', 'kurt', 'sem',
                      'std', 'var', 'skew', 'mad', 'sum']
        self._col_dict = {sensor: None for sensor in self.sensors}
        self._raw_df = pd.DataFrame([0]*len(self.sensors)).transpose()
        self._raw_df.columns = self.sensors

        self._processed_df = pd.DataFrame()
        self._num_features = len(self.stats)
        self._num_sensors = len(self.sensors)
        self._tensor = None
        self._raw_buffer_time = 10        # time of serial buffer data
        # self._processed_buffer_time = 1     # time for processed data

        # processed_buffer_time = time.perf_counter() + self._processed_buffer_time
        # while time.perf_counter() <= processed_buffer_time:
        self.get_raw_df()
        self.get_processed_df()
        # print("Done one round of processing!")

    # @staticmethod
    # def get_statistics(sensor_dict):
    #     stat_dict = defaultdict(float)
    #     sensors = [k for k in sensor_dict.keys()]
    #     stats = ['min', 'max', 'mean', 'kurt', 'sem', 'std', 'var', 'skew', 'mad', 'sum']
    #     complete_stats = product(sensors, stats)

    def get_raw_df(self):
        """
        CREATES
            One pandas DataFrame containing buffer_time worth raw data
        """

        buffer_time = time.perf_counter() + self._raw_buffer_time
        print("Starting buffer")
        while time.perf_counter() <= buffer_time:
            accX, accY, accZ, wx, wy, wz, bx, by, bz, isens, mic = data_getter.get_data()

            col_dict = {
                'accX': accX, 'accY': accY, 'accZ': accZ, 'wx': wx,
                'wy': wy, 'wz': wz, 'bx': bx, 'by': by, 'bz': bz, 'Isens': isens, 'Srms': mic
            }

            # add these two after attaching current sensor and mic
            # isens = np.random.rand(1,)
            # srms = np.random.rand(1,)

            self._raw_df = pd.concat([self._raw_df, pd.DataFrame(
                [col_dict.values()], columns=self.sensors)], axis=0, ignore_index=True)
        print(f"Total elapsed buffer time: {time.perf_counter() - buffer_time}")
        self._raw_df = self._raw_df.tail(-1)
        self._raw_df.dropna()

    @staticmethod
    def mad(df: pd.DataFrame):
        return (df - df.mean()).abs().mean()

    def get_processed_df(self):
        """
        CREATES
            One pandas dataframe row containing the following statistics across self.sensors:
            - min, max, mean, kurtosis, sem, std, variance, skew, mad, sum
        """

        assert self._raw_df.size != 0, f"Invalid size for dataframe: {self._raw_df.size}"
        assert len(
            self._raw_df.columns) == self._num_sensors, f"Some sensors are missing! Number of sensors detected: {len(self._raw_df.columns)}. Needed {self._num_sensors}!"

        # print(raw_df.describe())
        stat_df = self._raw_df.agg(
            ['min', 'max', 'mean', 'kurt', 'sem', 'std', 'var', 'skew', Preprocess.mad, 'sum'])
#         stat_df.transpose()
        stat_df = stat_df.transpose()

        self._processed_df = stat_df.unstack().to_frame().T
        self._processed_df.columns = self._processed_df.columns.map('_'.join)

    def get_tensor(self):
        return self._processed_df.to_numpy()


"""
Given a string A of size N.
Return the string A after reversing the string word by word.

NOTE:
A sequence of non-space characters constitutes a word.
Your reversed string should not contain leading or trailing spaces, even if it is present in the input string.
If there are multiple spaces between words, reduce them to a single space in the reversed string.

Problem Constraints
1 <= N <= 3 * 10^5

Example
Input: "the sky is blue"
Output: "blue is sky the"
"""

# @param A : string
# @return a strings
def solve(A):
    words = A.split()
    left, right = 0, len(words) - 1
    while left <= right:
        words[left], words[right] = words[right], words[left]
        left += 1
        right -= 1
    return " ".join(words)


def ControlleCode(gegokte_code, targetcode):
    #Gegokte code is de code die je toeleverd en de targetcode is de code waar je naar toe vergelijkt
    Blackpin = 0
    Whitepin = 0
    Collourblindpin = 0

    #Het omzetten van list naar sets om de overeenkomsten te isoleren
    #set1 = set(GlobalGamemastercode)
    set1 = set(targetcode)
    set2 = set(gegokte_code)
    list3 = list(set1 & set2)
    for i in list3:
        # van alle overeenkosmten bepalen hoeveel witte pinnen er moeten zijn (ook zwarte pinnen zijn nog wit)
        if targetcode.count(i) <= gegokte_code.count(i):
            Whitepin += targetcode.count(i)
            Collourblindpin += targetcode.count(i)    #niet gebruikte variable, overgebleven van een eventuele optie voor anderen algoritme die alleen kijkt of er pinnen zijn
    for i in range(len(gegokte_code)):
        #het eventueel omzetten van witte pinnen naar zwarte pinnen
        if gegokte_code[i] == targetcode[i]:
            Blackpin += 1
            Whitepin -= 1
    if Whitepin < 0:
        Whitepin = 0
    feedbck= [Blackpin, Whitepin]
    #feedbck= [Blackpin, Whitepin,Collourblindpin] #ongebruikte variable terug roepen om eventueel een extra algoritme te maken
    return feedbck

print(ControlleCode(['rood', 'zwart', 'paars', 'rood'], ['blauw', 'zwart', 'paars', 'blauw']))


"""Module for quickly loading VACUMM content"""

# System

import os
import sys
import shutil
import re
import glob
import gc
import warnings
from collections import OrderedDict
import argparse
from traceback import format_exc

# Numeric

import numpy
npy = N = np = numpy
testing = N.testing
assert_allclose = testing.assert_allclose
assert_raises = testing.assert_raises

# CDAT

import MV2
import cdms2
import cdtime
import cdutil
from cdutil import averager
from genutil import minmax

# Matplotlib

import matplotlib.pyplot as plt
import pylab as P
mp = P
from matplotlib import use, rc, rcdefaults, rcParams
from matplotlib.dates import DayLocator, HourLocator, WeekdayLocator, MinuteLocator,  \
    YearLocator, MonthLocator, DateFormatter, SecondLocator
from matplotlib.ticker import AutoLocator, MaxNLocator, FixedLocator, IndexLocator, \
    LinearLocator, LogLocator, MultipleLocator, AutoMinorLocator

# VACUMM

from vacumm import (VACUMMError, VACUMMWarning, help as vchelp, vacumm_warning,
    vacumm_warn, vcwarn)

# - config

from vacumm.config import (
    edit_user_conf_file, print_config, get_config_value, data_sample
    )

# - misc

from vacumm.misc.misc import (
    lonlab, latlab, deplab, kwfilter, broadcast, ArgList, closeto, dict_merge,
    dict_check_defaults, cp_atts, dict_filter,  cp_props, numod, set_atts, get_atts,
    set_lang, geodir, auto_scale, MV2_concatenate, MV2_axisConcatenate, phaselab,
    zoombox, squeeze_variable, set_lang_fr, scalebox, checkdir, create_selector,
    numod, deg2str, deg_from_dec, dict_filter_out, dict_copy_items, geo_scale,
    grow_depth, grow_lat, grow_variables, history, intersect, is_iterable,
    isempty, isnumber, lunique, main_geodir, selector2str, rm_html_tags,
    tunique, split_selector, xls_style, Att, splitidx, CaseChecker, check_case,
    indices2slices, filter_level_selector, filter_selector, match_atts,
    match_string, ArgTuple, dicttree_get, dicttree_set, minbox,
    bound_ops, squarebox, scan_format_string, bivariate_normal)

from vacumm.misc.axes import (
    create_lon, create_lat, create_time, create_depth,
    create_dep, islon, islat, isdep, isaxis, istime, create_axis,
    check_axes, check_id, check_order, get_axis_type, get_checker,
    guess_timeid, is_geo_axis, is_geo_axis_type, merge_orders,
    order_match, set_order,
    )

from vacumm.misc.atime import (
    comptime, strftime, strptime, Intervals, IterDates, now, Gaps, DateSorter,
    time_split, time_split_nmax, to_utc, tz_to_tz, utc_to_paris, paris_to_utc,
    lindates, ch_units, add_margin, round_date, midnight_date, midnight_interval,
    mpl, tic, toc, pat2freq, pat2glob, has_time_pattern,
    is_interval, is_time, is_axistime, is_cdtime, is_datetime, is_in_time_interval,
    is_numtime, is_strtime, itv_intersect, itv_union, julday, reltime,
    are_valid_units, SpecialDateFormatter, check_range, numtime,
    daily, daily_bounds, day_of_the_year, unit_type, toc,
    hourly, hourly_bounds, hourly_exact, monthly, tsel2slice,
    reduce, yearly, are_same_units, ascii_to_num, detrend, from_utc,
    time_selector, selector, filter_time_selector,
    plot_dt, strtime, interp_clim, round_interval,
    datetime as adatetime, compress as compress,
    add,
    )
datetime_ = adatetime
add_time = add
is_time_interval = is_interval
tcompress = compress
round_time_interval = round_interval
round_time = round_date

from vacumm.misc.bases import (
    psinfo, code_base_name, code_file_name, code_dir_name, describe, Class, Object,
    add_logging_proxies, func_name, get_noconflict_metaclass, get_prog_dir,
    remove_redundant, skip_redundant, stack_trace, classmaker,
    classinstancemethod, isfreezed, prog,
    )

from vacumm.misc.io import (
    list_forecast_files, netcdf3, netcdf4, ncread_files, ncread_var,
    ncread_axis, ncget_var, ncget_axis, ncfind_var, ncfind_axis, NcIterBestEstimate,
    ncget_fgrid, grib2nc, grib_get_names, ncfind_obj, ncget_grid, ncget_level,
    ncget_lon, ncget_lat, ncget_time, ncget_var, ncmatch_obj, ncread_best_estimate,
    XYZ, XYZMerger, Shapes, NcIterBestEstimate, NcIterBestEstimateError,
    NcFileObj, ColoredFormatter, TermColors,
    Logger as SimpleLogger,
    )

from vacumm.misc.filters import (
    generic1d, generic2d, shapiro1d, shapiro2d, blackman1d,
    bartlett1d, kaiser1d, hanning1d, blackman1d, gaussian1d, gaussian2d,
    deriv, deriv2d,
    )

from vacumm.misc.remote import InputWorkFiles, OutputWorkFile

from vacumm.misc.log import (Logger, ColoredStreamHandler, LoggerAdapter,
    StreamLogWrapper, get_str_level, get_int_level)

from vacumm.misc.config import (ConfigManager, cfgargparse, ConfigException,
    ValidationWarning, cfg2rst, cfgoptparse, filter_section, get_secnames,
    get_spec, list_options, opt2rst, option2rst, print_short_help,
    register_config_validator, get_validator,
    get_spec as getspec)

from vacumm.misc.stats import (StatAccum, qtmax, qtmin, qtminmax, ensrank,
    corr_proba, StatAccumError)

from vacumm.misc.phys.units import (
    convert_units, kt2ms, ms2kt, deg2m, m2deg, ms2bf, dms2deg, deg2dms, mph2ms,
    ms2mph, tometric, kel2degc, degc2kel, basic_proj, rad2deg, uuconvert,
    vect2dir, vect2mod, vect2moddir, moddir2vectx, moddir2vecty, moddir2vectxy,
    strfsize, strpsize, basic_proj,
    )

from vacumm.misc.phys.constants import (
    EARTH_RADIUS, R, G, GRAVITATIONAL_CONSTANT, M, EARTH_MASS, g, GRAVITY,
    )

from vacumm.misc.file import (
    mkdirs, mkfdirs, rollover, strfsize, strpsize, walk, xefind, xfind, find, efind,
    )

from vacumm.misc.namelist import Namelist

from vacumm.misc.xml import (XmlConfig, XmlConfigDict, XmlConfigList)

# - plot

from vacumm.misc.plot import (
    map2, section2, hov2, curve2, bar2, plot2d, stick2, make_movie,
    savefigs, add_grid, xhide, yhide, xrotate, yrotate, add_key, taylor, dtaylor, dtarget,
    add_shadow, add_glow, add_map_lines, add_map_line, add_map_point, minimap,
    add_logo, add_left_label, add_right_label, add_top_label, add_bottom_label,
    ellipsis, get_cls, hldays, rotate_xlabels, rotate_ylabels,
    scale_xlim, scale_ylim, wedge, set_major_locator, set_minor_locator,
    xdate, ydate, get_quiverkey_value, add_lightshading,
    add_param_label, add_map_box
    )
curve = curve2
map = map2
hov = hov2
stick = stick2
section = section2
bar = bar2

from vacumm.misc.core_plot import (
    Plot, Plot1D, Plot2D, ScalarMappable, Stick, Bar, Hov, Curve, Map, Section,
    loc2align, loc2tuple, loc2offset, best_loc_map,
    )

from vacumm.misc.color import (
    plot_cmap, show_cmap, get_cmap, simple_colors, RGB, RGBA,
    cmap_srs, Scalar2RGB, darken, whiten, cmaps_mpl,
    cmap_gmt, cmaps_registered, cmaps_vacumm, print_cmaps_gmt, plot_cmaps,
    anamorph_cmap, discretise_cmap, StepsNorm,
    cmap_ajete, cmap_ajets, cmap_bathy, cmap_br, cmap_bwr, cmap_bwre, cmap_custom,
    cmap_chla, cmap_currents, cmap_dynamic_cmyk_hymex, cmap_eke, cmap_jet,
    cmap_jete, cmap_jets, cmap_land, cmap_linear, cmap_magic, cmap_mg,
    cmap_ncview_rainbow, cmap_nice_gfdl, cmap_pe, cmap_previmer, cmap_previmer2,
    cmap_rainbow, cmap_rainbow_sst_hymex, cmap_rb, cmap_red_tau_hymex,
    cmap_regular_steps, cmap_rnb2_hymex, cmap_rs, cmap_smoothed_regular_steps,
    cmap_smoothed_steps, cmap_ss, cmap_ssec, cmap_steps, cmap_topo,
    cmap_white_centered_hymex, cmap_wjet, cmap_wjets, cmap_wr, cmap_wre,
    to_shadow, to_grey, saturate, desaturate, change_saturation,
    change_luminosity, change_value, pastelise,
    discretise_cmap as discretize_cmap,
    pastelise as pastelize,
    bistre, land, ocean, sea,
    land as land_color,
    ocean as ocean_color,
    sea as sea_color,
    )

# - grid

from vacumm.misc.grid.misc import (
    get_grid, set_grid, create_grid, create_grid2d, resol, monotonic, isdepthup, depth2dz,
    meshcells, meshbounds, meshgrid, bounds1d, bounds2d, coord2slice, isregular,
    isrect, curv2rect, isgrid, get_xy, create_axes2d, gridsel, varsel, xshift, rotate_grid,
    get_closest, get_closest_depth, makedepthup, get_axis, transect_specs,
    get_axis_slices, get_distances, dz2depth, meshweights, axis1d_from_bounds,
    bounds2mesh, cells2grid, check_xy_shape, curv_grid, create_var2d,
    get_geo_area, get_grid_axes, get_resolution, get_zdim, isoslice, mask2ind,
    merge_axis_slice, merge_axis_slices, num2axes2d, t2uvgrids, xextend,
    xshift, clone_grid, haversine, are_same_grids,
    get_tri, get_tri_mask, get_tri_type, create_ugrid,
    create_curv_grid, create_aux_axes, issgrid, isugrid, get_grid_type,
    get_unstruct_indices, GriddedSelector,
    )

from vacumm.misc.grid.regridding import (
    regrid1d, regrid2d, interp1d, interp2d, cellave1d, cellave2d, cellerr1d,
    cargen, xy2xy, shift1d, shift2d, extend1d, extend2d, regrid_method,
    transect, griddata, CDATRegridder, grid2xy, cubic1d,
    nearest1d, shiftgrid, extendgrid, fill1d, fill2d, CurvedInterpolator,
    )

from vacumm.misc.grid.basemap import (
    merc, create_map, get_proj, reset_cache, gshhs_autores, GSHHS_RESLIST,
    RSHPERE_WGS84
    )

from vacumm.misc.grid.masking import (
    erode_coast, polygon_mask, GetLakes, get_coast, polygons, polygon_select, zcompress,
    envelop, get_coastal_indices, grid_envelop, create_polygon, clip_shape, proj_shape,
    plot_polygon, merge_masks, masked_polygon, mask2d, get_dist_to_coast,
    )

from vacumm.misc.grid.kriging import (
    OrdinaryCloudKriger, variogram, variogram_fit, variogram_model,
    variogram_model_type, variogram_multifit, cloud_split,
    KrigingError, VariogramModel, VariogramModelError, SimpleCloudKriger,
    VARIOGRAM_MODEL_TYPES, DEFAULT_VARIOGRAM_MODEL_TYPE,
    )

# - data

from vacumm.data import setup_dataset, DS, register_dataset

from vacumm.data.cf import (
    format_var, format_axis, format_grid, match_var, change_loc, match_obj,
    set_loc, get_loc, GENERIC_NAMES, GENERIC_AXIS_NAMES, GENERIC_VAR_NAMES,
    AXIS_SPECS, VAR_SPECS, GRID_SPECS, ARAKAWA_SUFFIXES, HIDDEN_CF_ATTS,
    match_known_var, match_known_axis, get_cf_cmap, CF_AXIS_SPECS, CF_VAR_SPECS,
    register_cf_variable, register_cf_variables_from_cfg,
    register_cf_axis, register_cf_axes_from_cfg,
    CF_VAR_NAMES, CF_AXIS_NAMES, is_cf_known,
    )

from vacumm.data.misc.sigma import NcSigma, sigma2depths, SigmaGeneralized, SigmaStandard

from vacumm.data.misc.arakawa import (
    ArakawaGrid, ARAKAWA_LOCATIONS as arakawa_locations,
    CGrid, AGrid, ArakawaGridTransfer
    )

from vacumm.data.misc.dataset import (
    Dataset, OceanDataset, AtmosDataset, GenericDataset)

# - diag

from vacumm.diag.thermdyn import density, mixed_layer_depth

from vacumm.diag.dynamics import (
    barotropic_geostrophic_velocity, coriolis_parameter, kinetic_energy
    )


import os

import pandas as pd
from gym_brt.data.config.configuration import FREQUENCY
from matplotlib import pyplot as plt

def set_new_model_id(path):
    model_id = 0
    for (_, dirs, files) in os.walk(path):
        for dir in dirs:
            try:
                if int(dir[:3]) >= model_id:
                    model_id = int(dir[:3]) + 1
            except:
                continue
    path = os.path.join(path, str(model_id).zfill(3))
    os.mkdir(path)
    return model_id

def num_epochs(path, epoch_length=None, frequency=FREQUENCY):
    number_of_epochs = 0
    for root, dirs, files in os.walk(path):
        for file in files:
            if ".zip" in file:
                number_of_epochs += 1
    print("Number of epochs: %d" % number_of_epochs)
    if epoch_length is not None:
        steps = number_of_epochs * epoch_length
        print("Steps: %d" % steps)
        if frequency is not None:
            time = steps / frequency / 60
            print("Time (min): %.2f" % time)

def visualize_progress(path):
    columns = ['approxkl', 'clipfrac', 'ep_len_mean', 'ep_reward_mean',
               'explained_variance', 'fps', 'n_updates', 'policy_entropy',
               'policy_loss', 'serial_timesteps', 'time_elapsed', 'total_timesteps',
               'value_loss']
    # try:
    result_log = pd.read_csv(path + "/result_log.csv")
    fig = plt.figure(figsize=(30, 10))
    for i, column in enumerate(columns):
        ax = fig.add_subplot(3, 5, i + 1)
        ax.plot(result_log[column])
        ax.set_title(column)
    plt.show()

def save_progress(path):
    progress_file = path + "/progress.csv"
    columns = ['approxkl', 'clipfrac', 'ep_len_mean', 'ep_reward_mean',
               'explained_variance', 'fps', 'n_updates', 'policy_entropy',
               'policy_loss', 'serial_timesteps', 'time_elapsed', 'total_timesteps',
               'value_loss']
    if os.path.exists(progress_file):
        try:
            progress = pd.read_csv(progress_file)
            if os.path.exists(path + "/result_log.csv"):
                result_log = pd.read_csv(path + "/result_log.csv")
            else:
                result_log = pd.DataFrame(columns=columns)

            progress = progress.reindex(sorted(progress.columns), axis=1)
            result_log = pd.concat([result_log, progress])
            pd.DataFrame.fillna(result_log, value=0, inplace=True)
            result_log.to_csv(path + "/result_log.csv", index=False)
            print("result data saved ...")
        except:
            pass
        os.remove(progress_file)


import pprint
import requests
from hamcrest import assert_that, equal_to, has_entries, has_item
from ycmd.tests.javascriptreact import setUpModule, tearDownModule # noqa
from ycmd.tests.javascriptreact import PathToTestFile, SharedYcmd
from ycmd.tests.test_utils import CombineRequest
from ycmd.utils import ReadFile
from unittest import TestCase

def RunTest( app, test ):
  contents = ReadFile( test[ 'request' ][ 'filepath' ] )
  filetype = test[ 'request' ].get( 'filetype', 'javascriptreact' )
  app.post_json(
    '/event_notification',
    CombineRequest( test[ 'request' ], {
      'contents': contents,
      'filetype': filetype,
      'event_name': 'BufferVisit'
    } )
  )

  response = app.post_json(
    '/completions',
    CombineRequest( test[ 'request' ], {
      'contents': contents,
      'filetype': 'javascriptreact',
      'force_semantic': True
    } )
  )

  print( f'completer response: { pprint.pformat( response.json ) }' )

  assert_that( response.status_code,
               equal_to( test[ 'expect' ][ 'response' ] ) )

  assert_that( response.json, test[ 'expect' ][ 'data' ] )

class GetCompletionsTest( TestCase ):
  @SharedYcmd
  def test_GetCompletions_JavaScriptReact_DefaultTriggers( self, app ):
    filepath = PathToTestFile( 'test.jsx' )
    RunTest( app, {
      'description': 'No need to force after a semantic trigger',
      'request': {
        'line_num': 7,
        'column_num': 12,
        'filepath': filepath,
        'filetype': 'javascriptreact'
      },
      'expect': {
        'response': requests.codes.ok,
        'data': has_entries( {
          'completions': has_item( has_entries( {
            'insertion_text':  'alinkColor',
            'extra_menu_info': '(property) Document.alinkColor: string',
             'detailed_info':  '(property) Document.alinkColor: string\n'
                               '\n'
                               'Sets or gets the color of all active links '
                               'in the document.',
            'kind':            'property',
          } ) )
        } )
      }
    } )


import requests
import time
from bs4 import BeautifulSoup

# %%
# 获取网页
link = r'http://www.santostang.com/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36\
                  (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36',
}
# 有时爬虫会遇到服务器长时间不返回， 这时爬虫程序就会一直等待，
# 造成爬虫程序没有顺利地执行。 因此， 可以用Requests在timeout参数设定的秒数结束之后停止等待响应。
# 意思就是， 如果服务器在 timeout 秒内没有应答， 就返回 异常。
r = requests.get(link, headers=headers, timeout=1.01)

# print(r.text)

# 提取数据
soup = BeautifulSoup(r.text, 'lxml')
title = soup.find('h1', class_='post-title').a.text.strip()

#
# 1) r.tex t是服务器响应的内容， 会自动根据响应头部的字符编码进行解码。
# 2) r.encoding是服务器内容使用的文本编码。
# 3) r.status_code用于检测响应的状态码， 如果返回200， 就表示请求成功了：
# 如果返回的是4xx， 就表示客户端错误； 返回5xx则表示服务器错误响应。
# 我们可以用r.status code来检测请求是否正确响应。
# 4) r.content 是字节方式的响应体， 会自动解码gzip和deflate编码的响应数据。
# 5) r.json（）是Requests中内置的JSON解码器。
# print('文本编码', r.encoding)
# print('响应状态码', r.status_code)
# print('字符串方式的响应体', r.text)

# 为了请求特定的数据， 我们需要在URL的查询字符串中加入某些数据。如果
# 你是自己构建URL， 那么数据一般会跟在一个问号后面， 并且以键／值的形式放在
# URL中， 如https://www.lesmao.co/plugin.php?id=group&page=2在Requests中，
# 可以直接把这些参数保存在字典中， 用params构建至URL中。
key_dicts = {'id': 'group', 'page': '2'}
r = requests.get('https://www.lesmao.co/plugin.php', params=key_dicts)
print('URL已经正确编码', r.url)
print('字符串方式的响应体\n', r.text)


import pandas
import numpy
import logging

class EWM_Model:

    def __init__(self, dataset, timesteps):
        self.dataset = pandas.DataFrame(dataset)
        self.tsteps = timesteps
        self.logger = logging.getLogger(f"{self.__class__.__name__}")

    def get_error(self):
        error = self.dataset.ewm(span=self.tsteps).mean()
        error = error.as_matrix()
        error = numpy.abs(self.dataset.as_matrix() - error)
        error = numpy.mean(error, axis=1)
        return error

    def do_tests(self):
        self.logger.info(f"Beginning tests for EWMA_{self.tsteps}")
        self.logger.info(f"Error is {numpy.mean(self.get_error())}")


#Импортирование библиотек
import pandas as pd
import matplotlib.pyplot as pl

#   Функция очистки и превращения значений строковых в значения целочисленные
def toint(a):
    if isinstance(a, str):
        b = a.find(',')
        if b == -1:
            b = a.find('.')
        return int(a[:b])
    if isinstance(a, float):
        return(int(a))
    return 1

#   Функция удаления лишних пробелов
def spacing(string):
    if isinstance(string, str):
        return string.lstrip(' ').rstrip(' ')
    return string

#   Функция "обрезки" колонок
def status_deleting(status):
    return status[6:]

#!  Начало подготовки
df = pd.read_csv('Space_Corrected.csv')

#   Удаление двух ненужных столбцов
df = df.drop(['Unnamed: 0', 'Unnamed: 0.1'], axis='columns')

#   Переименовывание столбцов
df.rename(columns = {' Rocket' : 'Rocket'}, inplace = True)
df.rename(columns = {'Status Rocket' : 'Rocket status'}, inplace = True)
df.rename(columns = {'Status Mission' : 'Mission status'}, inplace = True)
df.rename(columns = {'Datum' : 'Date'}, inplace = True)
df.rename(columns = {'Company Name' : 'Company name'}, inplace = True)

#   Удаление пробелов среди данных всех столбцов
df['Company name'] = df['Company name'].apply(spacing)
df['Location'] = df['Location'].apply(spacing)
df['Date'] = df['Date'].apply(spacing)
df['Detail'] = df['Detail'].apply(spacing)
df['Rocket Status'] = df['Rocket status'].apply(spacing)
df['Rocket'] = df['Rocket'].apply(spacing)
df['Mission Status'] = df['Mission status'].apply(spacing)

#   Редактирование столбцов типа "Статус"
df['Rocket status'] = df['Rocket status'].apply(status_deleting)

#   Очистка данных в столбце "Рокеты"
df['Rocket'].fillna(1, inplace = True)
df['Rocket'] = df['Rocket'].apply(toint)

#! Первый этап
#   Функция-определитель типа компании
def company_type(company):
    a1 = (company.find('US Navy') == -1) #США
    a2 = (company.find('Roscosmos') == -1) #Россия
    a3 = (company.find('USSR') == -1) #СССР
    a4 = (company.find('JAXA') == -1) #Япония
    a5 = (company.find('ExPace') == -1) #Китай
    a6 = (company.find('ISA') == -1) #Израиль
    a7 = (company.find('ISRO') == -1) #Индия
    a8 = (company.find('CASIC') == -1) #Китай
    a9 = (company.find('KCST') == -1) #Северная Корея
    a10 = (company.find('Sandia') == -1) #США
    a11 = (company.find('KARI') == -1) #Южная Корея
    a12 = (company.find('ESA') == -1) #Европейское космическое агенство
    a13 = (company.find('NASA') == -1) #США
    a14 = (company.find('ISAS') == -1) #Израиль
    a15 = (company.find('AEB') == -1) #Бразилия
    a16 = (company.find('Yuzhmash') == -1) #Россия
    a17 = (company.find('ASI') == -1) #Италия
    a18 = (company.find('CNES') == -1) #Франция
    a19 = (company.find('US Air Force') == -1) #США
    a20 = (company.find('OKB-586') == -1) #Россия
    a21 = (company.find("Arm??e de l'Air") == -1) #Франция
    if a1 and a2 and a3 and a4 and a5 and a6 and a7  and a8 and a9 and a10 and a11 and a12 and a13 and a14 and a15 and a16 and a17 and a18 and a19 and a20:
        return 'Private'
    else:
        return 'State'

#   Создание столбца "Тип компании" основного DataFrame и его заполнение
df.insert(1, 'Company type', '')
df['Company type'] = df['Company name'].apply(company_type)
Rockets = (df.groupby(by = 'Company type')['Rocket'].agg('sum'))
Rockets_Success = (df[df['Mission status'] == 'Success'].groupby(by = 'Company type')['Rocket'].agg('sum'))

#   Сощдвние итогового DataFrame и его заполнение
result1_list = {'Type': ['Private', 'State']}
Result1 = pd.DataFrame(result1_list)
Result1.insert(1, 'Persentage', '')
for i in range(len(result1_list)+1):
    Result1['Persentage'][i] = Rockets_Success[i] / Rockets[i] * 100

#   Показ первого графика
Result1.plot.bar(x='Type', y='Persentage', grid=True)
pl.show()

#! Второй этап
#   Вспомогательный список
most_countries_list = ['Russia', 'Kazakhstan', 'USA', 'Japan', 'China', 'France', 'India', 'Pacific Ocean']

#   Функция очистки
def countries(location):
    a = location.rfind(',')
    if most_countries_list.count(location[a+2:]) != 0:
        return location[a+2:]
    return 'Others'

#   Создание итоговой колонки основного DataFrame
df.insert(4, 'Location country', '')
df['Location country'] = df['Location'].apply(countries)

#   Показ второго графика
df['Location country'].value_counts().plot(kind='pie')
pl.show()

#! Третий этап
#   Вспомогательный словарь
months_dict = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}

#   Функции очистки и сортировки столбцов из основного DataFrame
def baikonur(location):
    if location.find('Baikonur') != -1:
        return 1
def month(date):
    return date[4:7]

#   Создание вспомогательного и итогового DataFrame
data3 = pd.DataFrame({'Mission status': [], 'Month': []})
Result3 = pd.DataFrame({'Months': [], 'Month order': []})

#   Заполнение вспомогательного DataFrame
data3['Month'] = df[df['Location'].apply(baikonur) == 1]['Date'].apply(month)
data3['Mission status'] = df[df['Location'].apply(baikonur) == 1]['Mission status']

#   Заполнение итогового Data Frame
Result3['Months'] = data3['Month'].value_counts()

#   Заполнение и сортировка по столбцу "Месяцы"
for i in range(12):
    a = Result3.index[i]
    Result3['Month order'][a] = str(Result3.index[i])
Result3 = Result3.replace({'Month order': months_dict})
Result3 = Result3.sort_values(by='Month order')
Result3['Rcokets per month'] = data3['Month'].value_counts()

#   Показ третьего графика
Result3.plot(x='Month order', y='Rcokets per month', kind='scatter')
pl.show()


# -*- coding: utf-8 -*-
"""
Created on Wed Dec  8 11:03:58 2021

@author: calum

this file contains code from [1]
"""

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import utils
from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras.layers import Dense, LeakyReLU, BatchNormalization, Reshape, Flatten,LSTM
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import time
import os
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import seaborn as sns
import sys
import collections

from tensorflow.keras import backend as K
from tabulate import tabulate
import random

project_location = "C:/Users/calum/OneDrive/Documents/Year_3_Project/Python/oldGAN/"
src="C:/Users/calum/OneDrive/Documents/Year_3_Project/Python/spectrogram/"

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.getcwd()
names=['labels','activities','time series','x', 'y', 'z']

activity = ['A', 'B', 'D', 'Q']
act_group=['Motionless','Low acceleration motion','Moderate acceleration motion','High  acceleration motion']

ACT_N = 4

""" WISDM data is sampled every 50ms = 20 Hz """
frequency=20
""" number of training steps """
step = 200
""" batch size per epoch """
BATCH_SIZE = 64
BUFFER_SIZE = 60000

def recall_m(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
    recall = true_positives / (possible_positives + K.epsilon())
    return recall

def precision_m(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    return precision

def f1_m(y_true, y_pred):
    precision = precision_m(y_true, y_pred)
    recall = recall_m(y_true, y_pred)
    return 2*((precision*recall)/(precision+recall+K.epsilon()))

def import_real(x_start, x_split): # x_split datasets, starting at x_start
    x_start = x_start+1600
    data_raw = pd.read_csv(
        r"C:\Users\calum\OneDrive\Documents\Year_3_Project\Python\oldGAN\dataset\train\data_"+str(x_start)+"_accel_watch.txt"
        ,names=names, lineterminator=';', skip_blank_lines=True)
    #np.savetxt('C:/Users/calum/OneDrive/Documents/Year_3_Project/Python/oldGAN/sumx.txt',data_raw,delimiter=',',fmt = '%s')

    data_raw = data_raw[:-1]
    data_raw['labels']=str(x_start)

    for i in range(x_start+1,x_start+x_split): #or 1651
        #print(i)
        filename='C:/Users/calum/OneDrive/Documents/Year_3_Project/Python/oldGAN/dataset/train/data_'+str(i)+'_accel_watch.txt'
        x=pd.read_csv(filename,names=names, lineterminator=';')
        x['labels']=str(i)
        data_raw=pd.concat([data_raw, x[:-1]], axis=0)

    return data_raw

def import_inverted(x_start, x_split, rf): # x_split datasets, starting at x_start
    x_start = x_start+1600

    data_raw = pd.read_csv(
        r"C:\Users\calum\OneDrive\Documents\Year_3_Project\Python\spectrogram\dataset\raw\\" +str(rf)+ "\data_"+str(x_start)+"_accel_watch.txt"
        ,names=names, lineterminator=';', skip_blank_lines=True)
    #np.savetxt('C:/Users/calum/OneDrive/Documents/Year_3_Project/Python/oldGAN/sumx.txt',data_raw,delimiter=',',fmt = '%s')
    data_raw['labels']=str(x_start)
    #data_raw = data_raw[]

    for i in range(x_start+1,x_start+x_split): #or 1651
        #print(i)
        filename=src+'/dataset/raw/'+str(rf)+'/data_'+str(i)+'_accel_watch.txt'
        x=pd.read_csv(filename,names=names, lineterminator=';')
        x['labels']=str(i)
        data_raw=pd.concat([data_raw, x], axis=0)

    #print(data_raw)
    return data_raw

def pre_fake():
    data_raw = pd.read_csv(
        r"C:\Users\calum\OneDrive\Documents\Year_3_Project\Python\oldGAN\dataset\gen_10\data_1660_accel_watch.txt"
        ,names=names, skip_blank_lines=True)
    #np.savetxt('C:/Users/calum/OneDrive/Documents/Year_3_Project/Python/oldGAN/sumx.txt',data_raw,delimiter=',',fmt = '%s')
    data_raw['labels']=str(1660)

    for i in range(1661,1660+10):
        filename='C:/Users/calum/OneDrive/Documents/Year_3_Project/Python/oldGAN/dataset/gen_10/data_'+str(i)+'_accel_watch.txt'
        x=pd.read_csv(filename,names=names)
        x['labels']=str(i)
        data_raw=pd.concat([data_raw, x], axis=0)

    return data_raw

def multivariate_data(dataset, step):
    data = []
    start = step
    for i in range(start, len(dataset)+1, step):
        indices = range(i-step, i)
        data.append(dataset[indices])
    return np.array(data)

def pre_proc_real(data_raw):
    data_full=data_raw
    data_full=data_full.loc[((data_full['activities'] == 'A') | (data_full['activities'] == 'B')
          | (data_full['activities'] == 'D') | (data_full['activities'] == 'Q'))]

    data_clean = pd.DataFrame(columns=names)

    l_1 = int(data_full['labels'].values[1])
    for i in range(50):

        if(i!=16):
            d_sel=data_full.loc[((data_full['labels'] == str(l_1+i)))]

        else:
            d_sel=data_full.loc[((data_full['labels'] == str(1650)))]
        #print(d_sel.shape)
        dataA=d_sel.loc[((d_sel['activities'] == 'A'))].head(18*200)
        dataB=d_sel.loc[((d_sel['activities'] == 'B'))].head(18*200)
        dataD=d_sel.loc[((d_sel['activities'] == 'D'))].head(18*200)
        dataQ=d_sel.loc[((d_sel['activities'] == 'Q'))].head(18*200)
        while (dataD.shape[0] < 3600):
            print(i, dataD.shape)
            new_index=dataD.index[-1]+1
            dataD=dataD.append(pd.DataFrame(index=[new_index], data=dataD.tail(1).values, columns=dataD.columns))
            #dataD = pd.concat([dataD, dataD.iloc[-1]], axis=0)
            #print(dataD)

        d_sel=pd.concat([dataA, dataB, dataD, dataQ], axis=0)
        #print(d_sel.shape)
        if(d_sel.shape[0]<14400):
            print(i)
            #print(dataA.shape, dataB.shape, dataD.shape, dataQ.shape)

        data_clean=pd.concat([data_clean, d_sel], axis=0)

    data_clean.drop(['labels', 'time series'],axis=1,inplace=True)
    #only use 3-axis data
    #features_considered = ['x', 'y', 'z']
    #features=data_raw[features_considered]
    label=data_clean.iloc[:,0] #extract activity labels
    #data_full=data_full[features_considered]
    data=data_clean.iloc[:,1:] #extract x,y,z data
    s_total = data.values
    data=pd.DataFrame(s_total)

    scaler=preprocessing.MinMaxScaler(feature_range=(-1,1)).fit(data)

    return scaler

def pre_proc(data_raw, scaler):
    data_full=data_raw

    data_full=data_full.loc[((data_full['activities'] == 'A') | (data_full['activities'] == 'B')
          | (data_full['activities'] == 'D') | (data_full['activities'] == 'Q'))]

    #"clean data" - ensure all activities same size to prevent overlap when mini samples taken
    l_1 = int(data_full['labels'].values[1])

    data_clean = pd.DataFrame(columns=names)
    #test 10
    for i in range(10):
        d_sel=data_full.loc[((data_full['labels'] == str(l_1+i)))]
        dataA=d_sel.loc[((d_sel['activities'] == 'A'))].head(18*200)
        dataB=d_sel.loc[((d_sel['activities'] == 'B'))].head(18*200)
        dataD=d_sel.loc[((d_sel['activities'] == 'D'))].head(18*200)
        dataQ=d_sel.loc[((d_sel['activities'] == 'Q'))].head(18*200)
        d_sel=pd.concat([dataA, dataB, dataD, dataQ], axis=0)
        #print(d_sel.shape)
        data_clean=pd.concat([data_clean, d_sel], axis=0)

    #data_clean=data_full

    data_clean.drop(['labels', 'time series'],axis=1,inplace=True)
    #only use 3-axis data
    #features_considered = ['x', 'y', 'z']
    #features=data_raw[features_considered]
    label=data_clean.iloc[:,0] #extract activity labels
    #data_full=data_full[features_considered]
    data=data_clean.iloc[:,1:] #extract x,y,z data
    s_total = data.values
    data=pd.DataFrame(s_total)

    """ one hot encoding to replace labels.
    """

    ## data type transformation
    label=label.replace('A','0')
    label=label.replace('B','1')
    label=label.replace('D','2')
    label=label.replace('Q','3')

    label=pd.DataFrame(label,dtype=int)
    ## MinMaxScaler normalization
    #scaler=preprocessing.MinMaxScaler(feature_range=(-1,1))
    #print(data.shape)
    data=scaler.transform(data)
    data=pd.DataFrame(data, columns=['x','y','z'])
    ## Concat label and data
    label=label.reset_index(drop=True)
    data=data.reset_index(drop=True)
    dataset=pd.concat([label,data],axis=1)
    dataset=np.array(dataset)

    step=200

    x_train_single = multivariate_data(dataset, step)

    #show_plot(x_train_single[0,:,1:], str(activity)+'_Min-max_Normalization')
    #show_plot(dataset[:36000,1:], 'A_Full')

    data_f=dataset[:,1:]
    label_f=dataset[:,0]

    data_n=x_train_single[:,:,1:]
    label_n=x_train_single[:,0,0]

    label_n=tf.keras.utils.to_categorical(label_n)
    label_f=tf.keras.utils.to_categorical(label_f)

    return data_n, label_n

def show_plot(plot_data, title):
    time_steps = create_time_steps(plot_data.shape[0])
    plt.figure()
    plt.title(title)
    plt.plot(time_steps, plot_data[:, 0].flatten(), color='red', label='Accel-X')
    plt.plot(time_steps, plot_data[:, 1].flatten(), color='blue', label='Accel-Y')
    plt.plot(time_steps, plot_data[:, 2].flatten(), color='yellow', label='Accel-Z')
    plt.legend()
    plt.xlabel('Time (seconds)')
    plt.ylabel('Acceleration ($m/s^2$)')
    plt.show()
    plt.savefig(project_location+'plots/GAN/'+title+'.png')
    return plt

def create_time_steps(length):
    return list(np.arange(0, length/frequency,1/frequency))

def show_plot(plot_data, title):
    time_steps = create_time_steps(plot_data.shape[0])
    plt.figure()
    plt.title(title)
    plt.plot(time_steps, plot_data[:, 0].flatten(), color='red', label='Accel-X')
    plt.plot(time_steps, plot_data[:, 1].flatten(), color='blue', label='Accel-Y')
    plt.plot(time_steps, plot_data[:, 2].flatten(), color='yellow', label='Accel-Z')
    plt.legend()
    plt
    plt.xlabel('Time (seconds)')
    plt.ylabel('Acceleration ($m/s^2$)')
    plt.show()
    #plt.savefig(project_location+'plots/GAN/'+title+'.png')
    return plt

def test_model(model, d_value, d_label, real):
    label_test=d_label
    s=558
    show_plot(d_value.reshape(-1, 3), "1647_Q_Min_max")

    data_test, label_test = d_value,d_label
    #print(d_label.shape, d_value.shape)
    #sys.exit()
    rng_state = np.random.get_state()
    np.random.shuffle(data_test)
    np.random.set_state(rng_state)
    np.random.shuffle(label_test)

    #test model on test dataset
    predictions=model.predict(data_test)

    #get array of test labels
    max_true = np.argmax(label_test, axis = 1)

    max_prediction = np.argmax(predictions, axis = 1)

    #total is number of entries in test dataset
    correct = 0
    skip = 0
    total = max_prediction.shape[0]
    #print(total)

    #set all activity accuracy to 0 by default
    #[correct_n, incorrect_n]

    activity_accuracy = np.zeros((ACT_N, 2))

    #loop through set of predictions, comparing predicted label to true label
    for i in range(total):
        if max_true[i] >= 4:
            #skip
            skip = skip + 1
            continue
        #print(max_true[i])
        if max_true[i] == max_prediction[i]:
            correct = correct + 1
            #increment correct for this activity
            activity_accuracy[max_true[i], 0] = activity_accuracy[max_true[i], 0] + 1
        else:
            #increment incorrect
            activity_accuracy[max_true[i], 1] = activity_accuracy[max_true[i], 1] + 1

    total_accuracy = (correct/(total-skip))

    #print(str(correct)+'/'+str(total)+' = '+str(round(100*correct/total, 2))+'%')

    #set accuracy based off correct/total
    for i in range(ACT_N):
        activity_accuracy[i, 0] = activity_accuracy[i, 0] / (activity_accuracy[i, 0]+activity_accuracy[i, 1])
    #plot CM
    font = {'family' : 'normal',
        'weight' : 'bold',
        'size'   : 22}

    mpl.rc('font', **font)

    CM = confusion_matrix(max_true, max_prediction)
    plt.figure(figsize=(16,14))
    sns.heatmap(CM, xticklabels = activity, yticklabels = activity,
    annot = True, fmt = 'd',cmap='Blues')
    plt.title('Confusion Matrix')
    plt.xlabel('Predicted Label')
    plt.ylabel('True Label')
    plt.show()

    #return table of activity accuracy + total accuracy
    return np.append(activity_accuracy[:,0], total_accuracy)

def metrics():
    """ load models """
    r_model=keras.models.load_model(project_location+'models/cnn_opt/CNN40RR_clean2', custom_objects={'f1_m':f1_m, 'recall_m':recall_m, 'precision_m':precision_m})
    #r_model=keras.models.load_model(project_location+'models/CNN40RRfv', custom_objects={'f1_m':f1_m, 'recall_m':recall_m, 'precision_m':precision_m})
    #r_model=keras.models.load_model(project_location+'models/CNN40RR', custom_objects={'f1_m':f1_m, 'recall_m':recall_m, 'precision_m':precision_m})
    #f_model=keras.models.load_model(project_location+'models/CNN40FR', custom_objects={'f1_m':f1_m, 'recall_m':recall_m, 'precision_m':precision_m})

    """ load datasets """

    min_max_scaler=pre_proc_real(import_real(0, 51))

    real, real_label = pre_proc(import_real(40, 10),min_max_scaler)

    real_inv, real_inv_label = pre_proc(import_inverted(40, 10, "real_inv"),min_max_scaler)

    r1 = np.random.normal(0, 0.2, (720, 200, 3))

    spec_fake, spec_fake_label = pre_proc(import_inverted(40, 10, "fake"),min_max_scaler)

    time_fake, time_fake_label = pre_proc(pre_fake(),min_max_scaler)

    """ compare predictions to true labels with test"""
    act_acc_real = test_model(r_model, real, real_label, False)
    act_acc_rInv = test_model(r_model, real_inv, real_inv_label, False)
    act_acc_spec = test_model(r_model, spec_fake, spec_fake_label, False)
    act_acc_time = test_model(r_model, time_fake, time_fake_label, False)

    """ printout table """
    act_type = ['Motionless','Low acceleration motion','Moderate acceleration motion motion','High  acceleration motion']
    #act=
    a_a = np.concatenate((np.append(activity, 'Total Motion')
                          , np.round(act_acc_real, 4)
                          , np.round(act_acc_rInv, 4)
                          , np.round(act_acc_spec, 4)
                          , np.round(act_acc_time, 4)
                          ))
    a_a = np.reshape(a_a, (ACT_N+1, 5), order='F')

    print(tabulate(a_a, headers = ['Activity', 'TRTR Accuracy', 'TRTR Invert', 'TRTF Spec', 'TRTF TimeSeries']))

    return

metrics()


from random import randint
from time import sleep
from operator import itemgetter
# jogadas = dict()
# input()
#
# print('VALORES SORTEADOS')
# for c in range(1, 6):
#     jogadas[f'jogador{c}'] = randint(0, 6)
# for k, v in jogadas.items():
#     print(f'O {k} tirou {v}.')
#
# print(jogadas)
#
#
#
# print('RANKING DOS JOGADORES')
# for c in range(1, 5):
#     print(f'{c}º lugar: ')
jogo = {'jogador1': randint(1, 6),
        'jogador2': randint(1, 6),
        'jogador3': randint(1, 6),
        'jogador4': randint(1, 6)}
ranking = dict()

print('VALORES SORTEADOS')

for k, v in jogo.items():
    print(f'O {k} tirou {v}.')
    # sleep(0.5)
ranking = sorted(jogo.items(), key=itemgetter(1), reverse=True)
print(ranking)

for i, v in enumerate(ranking):
    print(f'{i+1}º lugar: {v[0]} com {v[1]}.')
    sleep(0.5)


import datetime
import logging
from typing import Any

import h5py
from PyQt5 import QtCore
from PyQt5.QtCore import pyqtSignal as Signal, pyqtSlot as Slot

from .backend import HaakePhoenixBackend
from ...device.frontend import DeviceFrontend, DeviceType
from ....sensors.thermometer import Thermometer

class HaakePhoenix(DeviceFrontend):
    backendclass = HaakePhoenixBackend
    devicename = 'HaakePhoenix'
    devicetype = DeviceType.Thermostat
    vendor = 'Haake'
    temperatureChanged = Signal(float)
    startStop = Signal(bool)
    loglevel = logging.INFO

    def __init__(self, name: str, host: str, port: int, **kwargs):
        super().__init__(name, host, port, **kwargs)
        self.sensors = [Thermometer(f'temperature', self.name, 0, '°C'),
                        Thermometer(f'internal', self.name, 1, '°C'),
                        Thermometer(f'external', self.name, 2, '°C'),
                        ]

    def temperature(self) -> float:
        return self['temperature']

    def highlimit(self) -> float:
        return self['highlimit']

    def lowlimit(self) -> float:
        return self['lowlimit']

    def setpoint(self) -> float:
        return self['setpoint']

    def startCirculator(self):
        self.issueCommand('start')

    def stopCirculator(self):
        self.issueCommand('stop')

    def setSetpoint(self, setpoint: float):
        self.issueCommand('setpoint', setpoint)

    def setLowLimit(self, lowlimit: float):
        self.issueCommand('lowlimit', lowlimit)

    def setHighLimit(self, highlimit: float):
        self.issueCommand('highlimit', highlimit)

    def setDate(self, date: datetime.date):
        self.issueCommand('setdate', date)

    def setTime(self, time: datetime.time):
        self.issueCommand('settime', time)

    def isRunning(self) -> bool:
        return self['__status__'] == HaakePhoenixBackend.Status.Running

    def onVariableChanged(self, variablename: str, newvalue: Any, previousvalue: Any):
        super().onVariableChanged(variablename, newvalue, previousvalue)
        if variablename == '__status__':
            if newvalue == HaakePhoenixBackend.Status.Running:
                self.startStop.emit(True)
            elif newvalue == HaakePhoenixBackend.Status.Stopped:
                self.startStop.emit(False)
            else:
                pass
        elif variablename == 'temperature':
            self.temperatureChanged.emit(float(newvalue))
            self.sensors[0].update(float(newvalue))
        elif variablename == 'temperature_internal':
            self.sensors[1].update(float(newvalue))
        elif variablename == 'temperature_external':
            self.sensors[2].update(float(newvalue))

    def toNeXus(self, grp: h5py.Group) -> h5py.Group:
        grp = super().toNeXus(grp)
        self.create_hdf5_dataset(grp, 'type', self['firmwareversion'])
        self.create_hdf5_dataset(grp, 'description', 'Haake Phoenix 2P circulating bath')
        sensorgrp = grp.create_group('temperature')
        sensorgrp.attrs['NX_class'] = 'NXsensor'
        self.create_hdf5_dataset(sensorgrp, 'model', 'Haake Phoenix')
        self.create_hdf5_dataset(sensorgrp, 'name', 'temperature')
        self.create_hdf5_dataset(sensorgrp, 'short_name', 'sample block temperature')
        self.create_hdf5_dataset(sensorgrp, 'measurement', 'temperature')
        self.create_hdf5_dataset(sensorgrp, 'run_control', False)
        self.create_hdf5_dataset(sensorgrp, 'value', self['temperature'], units='°C')
        return grp


import allure
from selenium.common.exceptions import TimeoutException

from page_factory.button import Button
from utils.logger import logger
from utils.webdriver.driver.page import Page

class CookiesModal:
    def __init__(self, page: Page) -> None:
        self.accept_button = Button(
            page,
            locator='//div[@id="accept-choices"]',
            name='Accept all & visit the site'
        )
        self.customize_button = Button(
            page,
            locator='//div[@id="sn-b-custom"]',
            name='Customise choices'
        )

    @allure.step('Accepting all cookies')
    def accept_cookies(self):
        try:
            if self.accept_button.is_displayed() and self.customize_button.is_displayed():
                self.accept_button.click()
        except TimeoutException:
            logger.error('Cookies modal did not appear')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 30 10:51:43 2017

@author: apple
"""

import cx_Oracle
con=cx_Oracle.connect('cl')
print(con.version)
con.close()


import sys

names = ["Bill", "Charlie", "Fred", "George", "Ginny", "Percy", "Ron"]

if "Ron" in names:
    print("Encontrado")
else:
    print("Não encontrado")

# Se tivermos um dicionário, um conjunto de pares de chaves-valores, também
# podemos verificar se há uma chave específica e examinar o valor armazenado para ela:

people = {
    "Brian": "+1-617-495-1000",
    "David": "+1-949-468-2750"
}

name = input("Nome: ")
if name in people:
    print(f"Número: {people[name]}")
else:
    print("Não encontrado")


# -*- coding: utf-8 -*-
"""
Created on Tue Oct  5 20:32:13 2021

@author: eduar
"""

import numpy as np
import pandas as pd
import seaborn as sns
import csv
import matplotlib.pyplot as plt

# Abrimos el archivo, especificando la ruta y usando pd.read_csv()
synergyFile = 'synergy_logistics_database.csv'
df = pd.read_csv(synergyFile, index_col='register_id')

#Podemos comenzar a agrupar por transport_mode, para este ejercicio vamos a
#sacar cuenta de cuantos envíos hay por categoría:
transp_mode_count = df['transport_mode'].value_counts()
print(transp_mode_count)

#Para algo más interesante grafiquemos ahora la ganancia promedio de estas
#categorías de transporte:
prom_val_transp = df.groupby('transport_mode')['total_value'].mean()

'''***************************************************************************
1- Rutas de importación y exportación
******************************************************************************'''
rutasUnicas = df[['direction', 'origin', 'destination', 'transport_mode', 'total_value']]
rutasConteoDF = rutasUnicas.groupby(['direction', 'origin', 'destination', 'transport_mode']).sum()
rutasConteoDF = rutasConteoDF.sort_values(by = 'total_value', ascending = False)

#Separarlos por exportaciones e importaciones:
top10exp = rutasConteoDF.xs('Exports').head(10)
top10imp = rutasConteoDF.xs('Imports').head(10)

#Graficación de resultados en gráficas de barras.Se tienen que generar la
#etiqueta para el eje x. Se agrega en el DataFrame creado.
top10exp['xlabel'] = top10exp.index.to_list()
top10imp['xlabel'] = top10imp.index.to_list()

#------------Se genera la gráfica de barras para las exportaciones-------------
sns.set(rc = {"figure.figsize": (18, 6)}) #width = 18, height = 6
sns.barplot(data = top10exp, x = 'xlabel', y = 'total_value')

#specify axis labels
plt.xlabel('Paises', size=20, weight=900)
plt.xticks(ticks=range(len(top10exp)), labels=top10exp.xlabel, rotation=90)
plt.tick_params(axis ='both', labelsize = 16)
plt.ylabel('Valor Total', size=20, weight=900)
plt.title('Mejores Rutas de Exportación', size=22, weight=600)

#display barplot
plt.show()
#-----------------------------------------------------------------------------

#------------Se genera la gráfica de barras para las importaciones-------------
sns.set(rc = {"figure.figsize": (18, 6)}) #width = 18, height = 6
sns.barplot(data = top10imp, x = 'xlabel', y = 'total_value')

#specify axis labels
plt.xlabel('Paises', size=20, weight=900)
plt.xticks(ticks=range(len(top10imp)), labels=top10imp.xlabel, rotation=90)
plt.tick_params(axis ='both', labelsize = 16)
plt.ylabel('Valor Total', size=20, weight=900)
plt.title('Mejores Rutas de Importación', size=22, weight=600)

#display barplot
plt.show()
#-----------------------------------------------------------------------------

'''***************************************************************************
2- Medio de transporte utilizado (son solo 4 medios de transporte)
******************************************************************************'''
medioTransp = df[['direction', 'transport_mode', 'total_value']]
medioTranspDF = medioTransp.groupby(['direction', 'transport_mode']).sum()
medioTranspDF = medioTranspDF.sort_values(by = 'total_value', ascending = False)

#Separarlos por exportaciones e importaciones:
topTransp_exp = medioTranspDF.xs('Exports').head(4)
topTransp_inp = medioTranspDF.xs('Imports').head(4)

#Graficación de resultados en gráficas de barras. Se tienen que generar la
#etiqueta para el eje x. Se agrega en el DataFrame creado.
topTransp_exp['xlabel'] = topTransp_exp.index.to_list()
topTransp_inp['xlabel'] = topTransp_inp.index.to_list()

#------------Se genera la gráfica de barras para las exportaciones-------------
sns.set(rc = {"figure.figsize": (18, 6)}) #width = 18, height = 6
sns.barplot(data = topTransp_exp, x = 'xlabel', y = 'total_value')

#specify axis labels
plt.xlabel('Medio de Transporte', size=20, weight=900)
plt.tick_params(axis ='both', labelsize = 18)
plt.ylabel('Valor Total', size=20, weight=900)
plt.title('Medio de Transporte para Exportaciones', size=22, weight=600)

#display barplot
plt.show()
#-----------------------------------------------------------------------------

#------------Se genera la gráfica de barras para las importaciones-------------
sns.set(rc = {"figure.figsize": (18, 6)}) #width = 18, height = 6
sns.barplot(data = topTransp_inp, x = 'xlabel', y = 'total_value')

#specify axis labels
plt.xlabel('Medio de Transporte', size=20, weight=900)
plt.tick_params(axis ='both', labelsize = 18)
plt.ylabel('Valor Total', size=20, weight=900)
plt.title('Medio de Transporte para Importaciones', size=22, weight=600)

#display barplot
plt.show()
#-----------------------------------------------------------------------------

'''***************************************************************************
3- Valor total de importaciones y exportaciones
******************************************************************************'''
#Se calcula el total de las importaciones y exportaciones, sin tomar en cuenta
#direcciones, origen,...
imp_ExpTotal = df[['direction', 'total_value']]
imp_ExpTotalDF = imp_ExpTotal.groupby(['direction']).sum()
expo_total = int(imp_ExpTotalDF.values[0])
imp_total = int(imp_ExpTotalDF.values[1])

#Se separan las rutas de envios de acuerdo a si se trata de importaciones o
#exportaciones.
imp_Exp = df[['direction', 'origin', 'destination', 'total_value']]
imp_ExpDF = imp_Exp.groupby(['direction', 'origin', 'destination']).sum()
imp_ExpDF = imp_ExpDF.sort_values(by = 'total_value', ascending = False)

#Separarlos por exportaciones e importaciones:
expDF = imp_ExpDF.xs('Exports')
impDF = imp_ExpDF.xs('Imports')

#Se considera solamente el pais de origen tratarse de exportaciones, , el de
#destino puede ser ignorado.
expDF = expDF.groupby(['origin']).sum()
expDF = expDF.sort_values(by = 'total_value', ascending = False)

#Se agrega una columna con la suma acumulada de exportaciones.
expDF['cumulative'] = expDF.values.cumsum()
expDF['xlabel'] = expDF.index.to_list()

#Al tratarse de importaciones, se considera solamente el pais de destino, el de
#origen puede ser ignorado.
impDF = impDF.groupby(['destination']).sum()
impDF = impDF.sort_values(by = 'total_value', ascending = False)

#Se agrega una columna con la suma acumulada de importaciones y otra para los índices.
impDF['cumulative'] = impDF.values.cumsum()
impDF['xlabel'] = impDF.index.to_list()

#------------Se grafican los resultados acumulados de exportaciones------------
sns.set(rc = {"figure.figsize": (12, 8)}) #width = 18, height = 6
sns.barplot(data = expDF, x = 'xlabel', y = 'cumulative')

#Graficando el indicador del 80%.
temp = np.repeat(expo_total*0.8, len(expDF))
plt.plot(expDF.xlabel, temp, 'k--', linewidth = 4)

#specify axis labels
plt.xlabel('Paises', size=20, weight=900)
plt.xticks(ticks=range(len(expDF)), labels=expDF.xlabel, rotation=90)
plt.tick_params(axis ='both', labelsize = 16)
plt.ylabel('Valor Total', size=20, weight=900)
plt.title('Exportaciones Acumuladas', size=22, weight=600)

#display barplot
plt.show()
#-----------------------------------------------------------------------------

#------------Se grafican los resultados acumulados de importaciones------------
sns.set(rc = {"figure.figsize": (12, 8)}) #width = 18, height = 6
sns.barplot(data = impDF, x = 'xlabel', y = 'cumulative')

#Graficando el indicador del 80%.
temp = np.repeat(imp_total*0.8, len(impDF))
plt.plot(impDF.xlabel, temp, 'k--', linewidth = 4)

#specify axis labels
plt.xlabel('Paises', size=20, weight=900)
plt.xticks(ticks=range(len(impDF)), labels=impDF.xlabel, rotation=90)
plt.tick_params(axis ='both', labelsize = 16)
plt.ylabel('Valor Total', size=20, weight=900)
plt.title('Importaciones Acumuladas', size=22, weight=600)

#display barplot
plt.show()
#-----------------------------------------------------------------------------