#
# @lc app=leetcode id=116 lang=python3
#
# [116] Populating Next Right Pointers in Each Node
#
# https://leetcode.com/problems/populating-next-right-pointers-in-each-node/description/
#
# algorithms
# Medium (48.10%)
# Likes:    2867
# Dislikes: 157
# Total Accepted:    440.3K
# Total Submissions: 913.3K
# Testcase Example:  '[1,2,3,4,5,6,7]'
#
# You are given a perfect binary tree where all leaves are on the same level,
# and every parent has two children. The binary tree has the following
# definition:
#
#
# struct Node {
#  int val;
#  Node *left;
#  Node *right;
#  Node *next;
# }
#
#
# Populate each next pointer to point to its next right node. If there is no
# next right node, the next pointer should be set to NULL.
#
# Initially, all next pointers are set to NULL.
#
#
#
# Follow up:
#
#
# You may only use constant extra space.
# Recursive approach is fine, you may assume implicit stack space does not
# count as extra space for this problem.
#
#
#
# Example 1:
#
#
#
#
# Input: root = [1,2,3,4,5,6,7]
# Output: [1,#,2,3,#,4,5,6,7,#]
# Explanation: Given the above perfect binary tree (Figure A), your function
# should populate each next pointer to point to its next right node, just like
# in Figure B. The serialized output is in level order as connected by the next
# pointers, with '#' signifying the end of each level.
#
#
#
# Constraints:
#
#
# The number of nodes in the given tree is less than 4096.
# -1000 <= node.val <= 1000
#
#

# @lc code=start
"""
# Definition for a Node.
class Node:
    def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
        self.val = val
        self.left = left
        self.right = right
        self.next = next
"""

class Solution:
    def connect(self, root: 'Node') -> 'Node':

        # Solution - 1: BFS (level order traversal) with queue
        # if not root: return
        # from collections import deque
        # queue = deque([root])

        # while queue:

        #     queue_length = len(queue)

        #     prev = None
        #     for _ in range(queue_length):

        #         node = queue.popleft()
        #         if prev:
        #             prev.next = node
        #         prev = node

        #         if node.left:
        #             queue.append(node.left)
        #         if node.right:
        #             queue.append(node.right)
        # return root

        # Solution - 2: interesting solution from answer without using level order traversal
        if not root: return root
        leftmost = root
        while leftmost.left:
            head = leftmost
            while head:
                head.left.next = head.right
                if head.next:
                    head.right.next = head.next.left
                head = head.next
            leftmost = leftmost.left

        return root

# @lc code=end


"""Data utility functions."""
import os

import numpy as np
import torch
import torch.utils.data as data
from PIL import Image
from torchvision import transforms

import _pickle as pickle

# pylint: disable=C0326
SEG_LABELS_LIST = [
    {"id": -1, "name": "void",       "rgb_values": [0,   0,    0]},
    {"id": 0,  "name": "building",   "rgb_values": [128, 0,    0]},
    {"id": 1,  "name": "grass",      "rgb_values": [0,   128,  0]},
    {"id": 2,  "name": "tree",       "rgb_values": [128, 128,  0]},
    {"id": 3,  "name": "cow",        "rgb_values": [0,   0,    128]},
    {"id": 4,  "name": "horse",      "rgb_values": [128, 0,    128]},
    {"id": 5,  "name": "sheep",      "rgb_values": [0,   128,  128]},
    {"id": 6,  "name": "sky",        "rgb_values": [128, 128,  128]},
    {"id": 7,  "name": "mountain",   "rgb_values": [64,  0,    0]},
    {"id": 8,  "name": "airplane",   "rgb_values": [192, 0,    0]},
    {"id": 9,  "name": "water",      "rgb_values": [64,  128,  0]},
    {"id": 10, "name": "face",       "rgb_values": [192, 128,  0]},
    {"id": 11, "name": "car",        "rgb_values": [64,  0,    128]},
    {"id": 12, "name": "bicycle",    "rgb_values": [192, 0,    128]},
    {"id": 13, "name": "flower",     "rgb_values": [64,  128,  128]},
    {"id": 14, "name": "sign",       "rgb_values": [192, 128,  128]},
    {"id": 15, "name": "bird",       "rgb_values": [0,   64,   0]},
    {"id": 16, "name": "book",       "rgb_values": [128, 64,   0]},
    {"id": 17, "name": "chair",      "rgb_values": [0,   192,  0]},
    {"id": 18, "name": "road",       "rgb_values": [128, 64,   128]},
    {"id": 19, "name": "cat",        "rgb_values": [0,   192,  128]},
    {"id": 20, "name": "dog",        "rgb_values": [128, 192,  128]},
    {"id": 21, "name": "body",       "rgb_values": [64,  64,   0]},
    {"id": 22, "name": "boat",       "rgb_values": [192, 64,   0]}]

def label_img_to_rgb(label_img):
    label_img = np.squeeze(label_img)
    labels = np.unique(label_img)
    label_infos = [l for l in SEG_LABELS_LIST if l['id'] in labels]

    label_img_rgb = np.array([label_img,
                              label_img,
                              label_img]).transpose(1,2,0)
    for l in label_infos:
        mask = label_img == l['id']
        label_img_rgb[mask] = l['rgb_values']

    return label_img_rgb.astype(np.uint8)

class SegmentationData(data.Dataset):

    def __init__(self, image_paths_file):
        self.root_dir_name = os.path.dirname(image_paths_file)

        with open(image_paths_file) as f:
            self.image_names = f.read().splitlines()

    def __getitem__(self, key):
        if isinstance(key, slice):
            # get the start, stop, and step from the slice
            return [self[ii] for ii in range(*key.indices(len(self)))]
        elif isinstance(key, int):
            # handle negative indices
            if key < 0:
                key += len(self)
            if key < 0 or key >= len(self):
                raise IndexError("The index (%d) is out of range." % key)
            # get the data from direct index
            return self.get_item_from_index(key)
        else:
            raise TypeError("Invalid argument type.")

    def __len__(self):
        return len(self.image_names)

    def get_item_from_index(self, index):
        to_tensor = transforms.ToTensor()
        img_id = self.image_names[index].replace('.bmp', '')

        img = Image.open(os.path.join(self.root_dir_name,
                                      'images',
                                      img_id + '.bmp')).convert('RGB')
        center_crop = transforms.CenterCrop(240)
        img = center_crop(img)
        img = to_tensor(img)

        target = Image.open(os.path.join(self.root_dir_name,
                                         'targets',
                                         img_id + '_GT.bmp'))
        target = center_crop(target)
        target = np.array(target, dtype=np.int64)

        target_labels = target[..., 0]
        for label in SEG_LABELS_LIST:
            mask = np.all(target == label['rgb_values'], axis=2)
            target_labels[mask] = label['id']

        target_labels = torch.from_numpy(target_labels.copy())

        return img, target_labels


import random
import numpy as np
import sys

nx = 10
ny = 10
filename = 'life.txt'
type = 'random'

if len(sys.argv) >= 3:
	nx = int(sys.argv[1])
	ny = int(sys.argv[2])
if len(sys.argv) >= 4:
	filename = sys.argv[3]
if len(sys.argv) >= 5:
	type = sys.argv[4]

array = []

if type == 'random':

	for i in xrange(nx):
		array.append(np.random.randint(2, size=ny))

with open(filename,'w') as f:
	for i in array:
		for j in i:
			f.write(str(j))
		f.write('\n')


#!/usr/bin/env python
# encoding: utf-8
'''
@Author  : pentiumCM
@Email   : 842679178@qq.com
@Software: PyCharm
@File    : rectangle.py
@Time    : 2021/3/5 15:26
@desc	 :
'''

# coding=utf-8
# 导入python包
import numpy as np
import argparse
import imutils
import cv2

image_path = 'F:/develop_code/python/machinelearn/machinelearn/cv_learn/peopleincar5.jpg'

# 读取图片并将其转化为灰度图片
image = cv2.imread(image_path)
image1 = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 计算图像中x和y方向的Scharr梯度幅值表示
ddepth = cv2.cv.CV_32F if imutils.is_cv2() else cv2.CV_32F
gradX = cv2.Sobel(gray, ddepth=ddepth, dx=1, dy=0, ksize=-1)
gradY = cv2.Sobel(gray, ddepth=ddepth, dx=0, dy=1, ksize=-1)

# x方向的梯度减去y方向的梯度
gradient = cv2.subtract(gradX, gradY)
# 获取处理后的绝对值
gradient = cv2.convertScaleAbs(gradient)
cv2.imwrite("gradient.png", gradient)

# 对处理后的结果进行模糊操作
blurred = cv2.blur(gradient, (9, 9))
# 将其转化为二值图片
(_, thresh) = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY)
cv2.imwrite("thresh.png", thresh)

# 构建一个掩码并将其应用在二值图片中
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
cv2.imwrite("closed1.png", closed)

# 执行多次膨胀和腐蚀操作
closed = cv2.erode(closed, None, iterations=4)
closed = cv2.dilate(closed, None, iterations=4)

cv2.imwrite("closed2.png", closed)

# 在二值图像中寻找轮廓, 然后根据他们的区域大小对该轮廓进行排序，保留最大的一个轮廓
cnts = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = sorted(cnts, key=cv2.contourArea, reverse=True)[0]

# 旋转图像
################################################################
rect = cv2.minAreaRect(c)
(h, w) = closed.shape[:2]
center = (w // 2, h // 2)

M = cv2.getRotationMatrix2D(center, rect[2], 1.0)
rotated = cv2.warpAffine(closed, M, (w, h))
cv2.imshow("Rotated", rotated)

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

# 计算最大的轮廓的最小外接矩形
rect = cv2.minAreaRect(c)
box = cv2.cv.BoxPoints(rect) if imutils.is_cv2() else cv2.boxPoints(rect)
box = np.int0(box)

# 绘制并显示结果
cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
cv2.drawContours(image1, [box], -1, (0, 255, 0), 3)
result = np.hstack([image, image1])
cv2.imwrite("detect1.png", result)
cv2.imshow("Image", result)
cv2.waitKey(0)


#!/usr/bin/env python3

from Crypto.Cipher import AES
from enum import Enum, unique
from typing import List
import ctypes

class DecryptionError(Exception):
    pass

@unique
class MessageInterface(Enum):
    COMMS_INTERFACE_SERIAL = 0
    COMMS_INTERFACE_BLUETOOTH = 1
    COMMS_INTERFACE_GATT = 2
    COMMS_INTERFACE_LORA = 3
    COMMS_INTERFACE_LORAWAN = 4

    def __str__(self):
        to_string = {
            self.COMMS_INTERFACE_SERIAL : "serial",
            self.COMMS_INTERFACE_BLUETOOTH : "bt_adv",
            self.COMMS_INTERFACE_GATT : "bt_gatt",
            self.COMMS_INTERFACE_LORA : "LoRa",
            self.COMMS_INTERFACE_LORAWAN : "LoRaWAN",
        }
        return to_string[self]

@unique
class PayloadType(Enum):
    PAYLOAD_TDF3 = 0
    PAYLOAD_OTI = 1
    PAYLOAD_VTI = 2
    PAYLOAD_RPC = 3
    PAYLOAD_RPC_RSP = 4
    PAYLOAD_INCOMING = 5
    PAYLOAD_OUTGOING = 6

    PAYLOAD_ENC_TDF3 = 32
    PAYLOAD_ENC_OTI = 33
    PAYLOAD_ENC_VTI = 34
    PAYLOAD_ENC_RPC = 35
    PAYLOAD_ENC_RPC_RSP = 36

    PAYLOAD_CUSTOM = 255

    def __bytes__(self):
        return self.value.to_bytes(1, byteorder='little')

class Hop:

    class OutgoingRouteInfo(ctypes.LittleEndianStructure):
        _pack_ = 1
        _fields_ = [("address", 6 * ctypes.c_uint8), ("interface_channel", ctypes.c_uint8)]
    OUTGOING_SIZE = ctypes.sizeof(OutgoingRouteInfo)

    class IncomingRouteInfo(ctypes.LittleEndianStructure):
        _pack_ = 1
        _fields_ = [("address", 6 * ctypes.c_uint8), ("interface_channel", ctypes.c_uint8), ("packet_age", ctypes.c_uint16), ("sequence", ctypes.c_uint8), ("rssi", ctypes.c_uint8)]
    INCOMING_SIZE = ctypes.sizeof(IncomingRouteInfo)

    def __init__(self, address: int, interface: MessageInterface, channel: str):
        self._address = address
        self._address_fmt = "00:00"
        self._interface = interface
        self._channel = channel
        self.__rssi = 0
        self._packet_age = 0
        self._sequence_number = 0
        self._metadata = False

    def apply_metadata(self, packet_age: int, sequence_number: int, rssi: int):
        self._packet_age = packet_age
        self._sequence_number = sequence_number
        self._rssi = rssi
        self._metadata = True

    def __bytes__(self):
        interface_channel = (self._interface.value << 4) | (0)
        address = (ctypes.c_ubyte* 6)(*self._address.to_bytes(6, byteorder='little'))
        if self._metadata:
            route_c =  self.IncomingRouteInfo( address, interface_channel, self._packet_age, self._sequence_number, 30 - self._rssi)
        else:
            route_c =  self.OutgoingRouteInfo( address, interface_channel)
        return bytes(route_c)

    def update_address(self, address: int):
        self._address = address

    @property
    def address(self):
        return self._address

    @property
    def address_fmt(self):
        return "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}".format(*bytearray(self._address.to_bytes(6, byteorder='big')))

    @property
    def interface(self):
        return self._interface

    @property
    def rssi(self):
        return self._rssi

    @classmethod
    def from_bytes(cls, byte_buffer : bytes):
        hop_c = cls.IncomingRouteInfo.from_buffer_copy(byte_buffer)
        address = int.from_bytes(bytes(hop_c.address), byteorder='little')
        try:
            route = cls(address, MessageInterface(hop_c.interface_channel >> 4), "default" )
        except ValueError as e:
            print("Bad Packet: " + " ".join([hex(b) for b in byte_buffer]))
            raise e
        route.apply_metadata(hop_c.packet_age, hop_c.sequence, 30 - hop_c.rssi)
        return route

class PacpPacket:

    class Payload:
        def __init__(self, payload_type: PayloadType, last_hop: Hop, payload: bytes):
            self._type = payload_type
            self._hop = last_hop
            self._payload = payload

        def __bytes__(self):
            payload =  bytes(self._type) + bytes(self._hop) + self._payload
            return (1 + len(payload)).to_bytes(1, byteorder='little') + payload

    PayloadList = List[Payload]
    HopList = List[Hop]

    def __init__(self, common_route: HopList, payloads: PayloadList):
        self._payloads = payloads
        self._common_route = common_route

    def __bytes__(self):
        hops_hdr = (len(self._common_route) + 1).to_bytes(1, byteorder='little')
        common_route = b"".join(bytes(r) for r in self._common_route)
        payloads = b"".join(bytes(p) for p in self._payloads)
        return hops_hdr + common_route + payloads

    def set_common_route(self, value: HopList):
        self._common_route = value

    def iter_payloads(self):
        for payload in self._payloads:
            complete_route = [*self._common_route, payload._hop]
            yield payload._type, complete_route, payload._payload

    @staticmethod
    def decrypt(route : HopList, encrypted_payload : bytes, key : bytes):
        original_hop = route[-1]
        original_addr = original_hop.address
        decrypted = b""

        if original_hop.interface == MessageInterface.COMMS_INTERFACE_BLUETOOTH:
            # Attempt to decode
            addr_bytes = int.to_bytes(original_addr, 6, 'little')
            init_vector = addr_bytes[:3] + 13 * b"\x00"
            decrypted_payload = b""
            decrypted_address = None

            if len(encrypted_payload) % 16 != 0:
                raise DecryptionError

            for _ in range(len(encrypted_payload) // 16):
                cipher = AES.new(key, AES.MODE_CBC, init_vector)
                decrypted = cipher.decrypt(encrypted_payload)
                decrypted_oui = decrypted[:3]

                # Validate decrypted payload [CSIRO, MINEW]
                valid_ouis = [b"\x90\x97\xd8", b"\x3F\x23\xAC"]
                if not decrypted_oui in valid_ouis:
                    raise DecryptionError

                decrypted_payload += decrypted[3:]

                if decrypted_address is None:
                    # Set the  decrypted address on the first iteration
                    decrypted_address = addr_bytes[:3] + decrypted[:3]
                else:
                    # All other addresses should match the first address
                    if addr_bytes[:3] + decrypted[:3] != decrypted_address:
                        raise DecryptionError
            # Update the routing information
            address_fixed = int.from_bytes(decrypted_address, 'little')
            route[-1].update_address(address_fixed)
        else:
            raise NotImplementedError

        return route, decrypted_payload

    @classmethod
    def from_bytes(cls, packet_type: PayloadType, address: int, byte_buffer : bytes):
        if packet_type == PayloadType.PAYLOAD_INCOMING:
            # Payloads are prepended by route information
            num_hops = int(byte_buffer[0])
            byte_buffer = byte_buffer[1:]

            common_route = []
            # Extract common hops
            for n in range(num_hops - 1):
                hop_bytes = byte_buffer[n*Hop.INCOMING_SIZE: (n + 1)* Hop.INCOMING_SIZE]
                common_route.append(Hop.from_bytes(hop_bytes))
            byte_buffer = byte_buffer[(num_hops - 1)*Hop.INCOMING_SIZE:]
            # Extract individual payloads
            payloads = []
            while len(byte_buffer) > 0:
                payload_size = int(byte_buffer[0])
                payload_type = PayloadType(int(byte_buffer[1]))
                hop = Hop.from_bytes(byte_buffer[2:2+Hop.INCOMING_SIZE])
                payloads.append(cls.Payload(payload_type, hop, byte_buffer[2+Hop.INCOMING_SIZE:payload_size]))
                byte_buffer = byte_buffer[payload_size:]
            packet = cls(common_route, payloads)
        elif packet_type == PayloadType.PAYLOAD_OUTGOING:
            raise NotImplementedError
        else:
            # The bytes directly represent one of the payload types
            # There is no routing info, so construct dummy info based on the fact this packet must have come from serial node
            hop = Hop(address, MessageInterface.COMMS_INTERFACE_SERIAL, "default")
            hop.apply_metadata(0, 0, 30)
            payload = cls.Payload(packet_type, hop, byte_buffer)
            packet = cls([], [payload])
        return packet

if __name__ == '__main__':

    hop1 = Hop(10, MessageInterface.COMMS_INTERFACE_BLUETOOTH, "default")
    hop2 = Hop(10, MessageInterface.COMMS_INTERFACE_BLUETOOTH, "default")
    hop3 = Hop(10, MessageInterface.COMMS_INTERFACE_BLUETOOTH, "default")

    payload1 = PacpPacket.Payload(PayloadType.PAYLOAD_TDF3, hop1, b"\x00\x11\x22")
    payload2 = PacpPacket.Payload(PayloadType.PAYLOAD_TDF3, hop2, b"\x00\x11\x33")
    packet = PacpPacket([hop3], [payload1, payload2])

    int(b"\x33")

    for t, r, p in packet.iter_payloads():
        print(t, r, p)


from random import randint, shuffle

def __partition__(elements, start, end):
    pivot = elements[start]
    left = start + 1
    right = end
    while left <= right:
        if elements[left] <= pivot:
            left += 1
        elif elements[left] > pivot:
            right -= 1
        else:
            elements[left], elements[right] = elements[right], elements[left]
            left += 1
            right -= 1

        elements[start], elements[right] = elements[right], elements[start]
    return start

def quicksort(elements):
    __qs_rec__(0, len(elements) - 1, elements)

def __qs_rec__(start, end, elements):
    if start >= end:
        return
    pivot = __partition__(elements, start, end)
    __qs_rec__(start, pivot - 1, elements)
    __qs_rec__(pivot + 1, end, elements)

def test_quicksort():
    print("Testing ....", end="")
    for i in range(1000):
        input = []
        n = randint(1, 100)
        for j in range(n):
            input.append(randint(1, 100))
        shuffle(input)  # desordena aleatoriamente
        copy = input[:]  # copia literal del array, si fuese input[] apunta a lo mismo
        quicksort(input)
        copy.sort()
        assert copy == input
    print("Done")

test_quicksort()


import torch
from torchvision.transforms import Compose, Normalize, ToTensor
from PIL import Image
import os
import time
import cv2 as cv
from exp import getExp
from exp_config import cfg
import argparse
# warnings.filterwarnings('ignore', '.*imshow.*', )

def file_filter(f):
    if f[-4:] in ['.jpg']:
        return True
    else:
        return False

def file_finder(in_dir):

    files = os.listdir(in_dir)

    files = list(filter(file_filter,files))
    return files

def output_colormap(img, path_grey, path_color):

    #img = (img-img.min())/(img.max()-img.min())

    im_gray = (img*255).astype('uint8')
    im_color =  cv.applyColorMap(im_gray, cv.COLORMAP_JET)
    cv.imwrite(path_color,im_color)
    cv.imwrite(path_grey,im_gray)

def load_imgs(src_dir,device):

    image_mean = [0.485, 0.456, 0.406]  # mean for the input image to the net (image -> (0, 1) -> mean/std)
    image_std = [0.229, 0.224, 0.225]   # std for the input image to the net (image -> (0, 1) -> mean/std)
    transform1 = Compose([ToTensor(), Normalize(image_mean, image_std)])

    img_list=[]
    img_files=file_finder(src_dir)

    for idx, file in enumerate(img_files):
        print('loading image: {}/{}'.format(idx+1,len(img_files)))
        img_path = os.path.join(src_dir,file)

        image = Image.open(img_path)#.convert('RGB')

        #..for lf data, image should be smaller to fit to the memory
        image = image.resize((1024,512))

        image = transform1(image).unsqueeze(0).to(device)

        img_list.append(image)

    return img_list, img_files

def det_anomaly(model, img_list):

    start_time = time.time()

    model.eval()
    anomaly_score_list=[]

    with torch.no_grad():
        for idx,img in enumerate(img_list):
            print('detecting anomaly: {}/{}'.format(idx+1,len(img_list)))
            out = model(img)
            anomaly_score = out['anomaly_score'][0,0]
            anomaly_score = anomaly_score.cpu().numpy()
            anomaly_score_list.append(anomaly_score)

    print("total seconds:%d"%(time.time() - start_time))

    return anomaly_score_list

def main(args):

    img_dir=args.img_dir
    out_dir=args.out_dir
    # img_dir = '/media/yazhou/data_drive1/wxy3/segData/ra'
    # out_dir = '/home/yazhou/wxy4/EUG/output.ra'
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)

    fuse_decoder_ckpt_path=args.ckpt_path
    # fuse_decoder_ckpt_path='/home/yazhou/wxy4/remake/psp_ocr_laf_train/psp+ocr_laf_train/checkpoints/checkpoint-best.pth'

    device=torch.device('cuda:0')
    model = getExp(cfg)
    model.load_checkpoint(fuse_decoder_ckpt_path)
    model=model.to(device)
    model.eval()

    img_list, img_files = load_imgs(img_dir,device)

    res_list=det_anomaly(model, img_list)

    #..save the results
    for idx, file in enumerate(img_files):
        gname = file.replace('.jpg','grey.jpg')
        cname = file.replace('.jpg','colr.jpg')

        path_grey =  os.path.join(out_dir,gname)
        path_color=  os.path.join(out_dir,cname)

        output_colormap(res_list[idx], path_grey, path_color)

if __name__ == '__main__':
    parser=argparse.ArgumentParser(description='Inference Images')
    parser.add_argument('ckpt_path', type=str, default=None)
    parser.add_argument('out_dir', type=str, default=None)
    parser.add_argument('img_dir', type=str, default=None)
    args=parser.parse_args()
    main(args)


#! python3
# encoding: utf-8
"""
@author:    Yize Wang
@contact:    wangyize@hust.edu.cn
@file:    diceloss.py
@time:   2022/3/16 18:28
@description:
"""

import torch
import torch.nn as nn

"""
Dice loss: Dice系数是一种集合相似度的度量函数，通常用于计算两个样本之间的相似度，取值范围在[0，1]之间，公式如下：
                                s = 2 * |X^Y| / (|X| + |Y|)
           其中|X^Y|是X和Y之间的交集，|X|和|Y|分别表示X和Y的元素的个数，其中，分子的系数是2，是因为分母存在重复计算X和Y之间的共同元素

           Ldice可以表示为1 -（2I + epsilon）/（U + epsilon），I为真实和预测（sigmoid和softmax处理之后的结果）结果乘积之和，U为
                真实值的平方和预测值的平方之和
           Ldice也可以表示为1 -（I + epsilon）/（U - I + epsilon），I为真实和预测（sigmoid和softmax处理之后的结果）结果乘积之和，U为
                真实值的平方和预测值的平方之和
"""

class DiceLoss(nn.Module):
    """ dice coeff for individual examples """

    epsilon = 0.0000001

    def __init__(self, input_data, target_data):
        super(DiceLoss, self).__init__()

        self.input_data = input_data
        self.target_data = target_data

        assert input_data.size() == target_data.size(), "Input and target data have different sizes: {} and {}".format(
            input_data.size(), target_data.size()
        )

    def forward(self):
        if self.input_data.is_cuda:
            loss = torch.FloatTensor(1).cuda().zero_()
        else:
            loss = torch.FloatTensor(1).zero_()

        # calculate each figure
        for com in zip(self.input_data, self.target_data):
            inter = torch.dot(com[0].view(-1), com[1].view(-1))
            union = torch.sum(com[0]) + torch.sum(com[1])

            loss += 1 - (2 * inter + DiceLoss.epsilon) / (union + DiceLoss.epsilon)

        loss /= self.input_data.size()[0]

        return loss

class FocalLoss(nn.Module):
    def __init__(self, preds, labels, alpha=0.25, gamma=2, num_classes=1, size_average=True):
        super(FocalLoss, self).__init__()

        self.size_average = size_average
        self.preds = preds
        self.labels = labels

        if isinstance(alpha, list):
            assert len(alpha) == num_classes, "{} class but {} alpha".format(num_classes, len(alpha))
            self.alpha = torch.Tensor(alpha)
        else:
            assert alpha < 1
            self.alpha = torch.zeros(num_classes)
            self.alpha[0] += alpha
            # self.alpha[1:] += (1 - alpha)

        self.gamma = gamma

    def forward(self):
        # assert preds.dim()==2 and labels.dim()==1
        preds = self.preds.view(-1, 1)
        labels = self.labels.view(-1, 1)
        self.alpha = self.alpha.to(preds.device)

        # focal_loss func, Loss = -α(1-yi)**γ *ce_loss(xi,yi)
        pt1 = torch.where(torch.eq(labels, 1), preds, torch.ones_like(preds))
        pt0 = torch.where(torch.eq(labels, 0), preds, torch.zeros_like(preds))
        pt1 = torch.clip(pt1, 0.0001, 0.9999)
        pt0 = torch.clip(pt0, 0.0001, 0.9999)

        loss = -torch.mean(self.alpha * torch.pow(1 - pt1, self.gamma) * torch.log(pt1)) - torch.mean((1 - self.alpha) * torch.pow(pt0, self.gamma) * torch.log(1 - pt0))

        return loss


import os
from unittest.mock import patch
import notifications_python_client
import pytest
from utils.gc_notify import run, gc_notify_log

api_key = "test-api-key"
base_url = "test-base-url"
email_address = "test@test.ca"
template_id = "templateId"
file_path = "testfile.txt"
personalisation = {
    "file": "file",
    "filename": "filename",
    "sending_method": "attach",
}

file = "file contents"

@patch(
    "notifications_python_client.notifications.NotificationsAPIClient.__init__",
    return_value=None,
)
@patch(
    "notifications_python_client.notifications.NotificationsAPIClient.send_email_notification",
    return_value="gc_notify_response",
)
def test_gc_notify_log(notificationsAPIClient_mock, sendEmailNotification_mock):
    with open("testfile.txt", "w") as f:
        f.write("File contents")
    response = gc_notify_log(
        api_key, base_url, email_address, template_id, file_path, personalisation
    )
    print(response)
    assert sendEmailNotification_mock.called_once()
    assert notificationsAPIClient_mock.called_once()
    assert response == "gc_notify_response"
    os.remove("testfile.txt")

@patch(
    "notifications_python_client.notifications.NotificationsAPIClient.__init__",
    return_value=None,
)
def test_gc_notify_log_error(notificationsAPIClient_mock):
    with open("testfile.txt", "w") as f:
        f.write("File contents")
    with pytest.raises(AttributeError):
        response = gc_notify_log(
            api_key,
            base_url,
            email_address,
            "incorrectTemplateId",
            file_path,
            personalisation,
        )
        assert response == None
        assert notificationsAPIClient_mock.called_once()
    os.remove("testfile.txt")

@patch("utils.gc_notify.gc_notify_log", return_value="gc_notify_response")
def test_run(gcNotify_mock):
    run(api_key, base_url, email_address, template_id, file_path, personalisation)
    assert gcNotify_mock.called_once()

@patch(
    "utils.gc_notify.gc_notify_log",
    return_value=notifications_python_client.errors.HTTPError,
)
def test_run_error(gcNotify_mock):
    with pytest.raises(TypeError):
        run(api_key, base_url, email_address, template_id, file_path, personalisation)
    assert gcNotify_mock.called_once()


# 피보나치 수열의 함수 선언 및 구현 - 모듈
# 모듈 : 함수나 변수를 모아둔 파일

def fib(n):
    a= 0
    b= 1
    while b < n:
        print(b, end=" ")
        a, b = b, a+b
    print()
def sum(n):
    sum = 0
    for i in range(1, n+1):
        sum += i
    return sum


import json
import re
import time
from urllib.parse import parse_qs, urlparse

import requests
from bs4 import BeautifulSoup

import settings
from cian.session import CianReqSession

RUCAPCHA_URL = 'https://rucaptcha.com/in.php'
RUCAPCHA_GET_URL = 'https://rucaptcha.com/res.php'

def extract_captcha_b64(html):
    soup = BeautifulSoup(html, features='html.parser')
    tag = soup.find('img')
    return tag.attrs['src']

def send_to_solve(b64image):
    data = {
        'method': 'base64',
        'key': settings.RUCAPCHA_TOKEN,
        'body': b64image,
        'json': 1,
    }
    resp = requests.instance().post(RUCAPCHA_URL, data=data)
    print(resp.content)

def exract_recaptcha(html):
    key = None
    soup = BeautifulSoup(html, features='html.parser')
    tag = soup.find('script')
    result = re.search("'sitekey': '(.+?)'", tag.text)
    if result:
        key = result.group(1)
    return key

def send_to_solve_recapcha(url, google_key):
    data = {
        'method': 'userrecaptcha',
        'key': settings.RUCAPCHA_TOKEN,
        'pageurl': url,
        'googlekey': google_key,
        'json': 1,
    }
    resp = requests.post(RUCAPCHA_URL, data=data)
    print(resp.content)
    return json.loads(resp.content.decode('utf-8'))

def check_status(request_id):
    data = {
        'key': settings.RUCAPCHA_TOKEN,
        'action': 'get',
        'id': int(request_id),
        'json': 1,
    }
    resp = requests.get(RUCAPCHA_GET_URL, params=data)
    print(resp.content)
    status = json.loads(resp.content.decode('utf-8'))
    return status

def send_recaptcha_answer(url, captcha_key):
    data = {
        'g-recaptcha-response': captcha_key,
        'redirect_url': parse_qs(urlparse(url).query)['redirect_url'],
    }
    print(data)
    resp = CianReqSession.instance().post(url, data=data)
    print(resp.content)
    CianReqSession.save_cookies()
    print(CianReqSession.instance().cookies)

def solve_recaptcha(url, raw_html):
    key = exract_recaptcha(raw_html)
    print('Sending captcha to solve...')
    print(f'url: {url}')
    resp = send_to_solve_recapcha(url, key)
    request_id = resp['request']
    print(f'Captcha sent, request_id: {request_id}. Waiting 20 seconds...')
    time.sleep(20)
    while True:
        status = check_status(request_id)
        if status['request'] == 'CAPCHA_NOT_READY':
            print('Captcha still not ready, waiting...')
            time.sleep(5)
        else:
            captcha_key = status['request']
            break
    send_recaptcha_answer(url, captcha_key)


#!/usr/bin/env python
# coding: utf-8
"""Command line interface for Internetarchive-YouTube Sync."""

import argparse
import io
import json
import os
import random
import signal
import sys
from pathlib import Path

import requests
from dotenv import load_dotenv
from internetarchive_youtube.archive_youtube import ArchiveYouTube
from internetarchive_youtube.create_collection import CreateCollection

class TimeLimitReached(Exception):
    """Raised when the time limit is reached."""

def _alarm_handler(signum: int, _: object):
    """Signal handler for SIGALRM.

    Raises:
        TimeLimitReached: When the time limit is reached.
    """
    print('Signal handler called with signal:', signum)
    raise TimeLimitReached(
        'The GitHub action is about to die. Terminating the job safely...')

def _create_collection(no_logs: bool = False) -> None:
    """Creates a collection from the channels list.

    Args:
        no_logs: Whether to print logs.
    """
    channels = os.getenv('CHANNELS')
    if not channels:
        raise TypeError('`CHANNELS` cannot be empty!')

    if channels.startswith('http'):
        channels = requests.get(channels).text

    elif Path(channels).exists():
        with open(channels) as f:
            channels = f.read()

    try:
        channels = json.loads(channels).items()
    except json.decoder.JSONDecodeError:
        channels = [tuple(x.split(': ')) for x in channels.strip().split('\n')]

    random.shuffle(channels)

    for channel in channels:
        if not no_logs:
            print(f'Current channel: {channel}')
        cc = CreateCollection(channel[0], channel[1], no_logs=no_logs)
        _ = cc.create_collection()

def _opts() -> argparse.Namespace:
    """Parses the command line arguments."""
    parser = argparse.ArgumentParser()
    parser.add_argument('-p',
                        '--prioritize',
                        help='Comma-separated list of channel names to '
                        'prioritize when processing videos.',
                        type=str)
    parser.add_argument('-s',
                        '--skip-list',
                        help='Comma-separated list of channel names to skip.',
                        type=str)
    parser.add_argument('-f',
                        '--force-refresh',
                        help='Refresh the database after every video ('
                        'Can slow down the workflow significantly, but '
                        'is useful when running multiple concurrent jobs).',
                        action='store_true')
    parser.add_argument('-t',
                        '--timeout',
                        help='Kill the job after n hours (default: 5).',
                        type=float,
                        default=5)
    parser.add_argument('-n',
                        '--no-logs',
                        help='Don\'t print any log messages.',
                        action='store_true')
    parser.add_argument('-a',
                        '--add-channel',
                        help='Add a channel interactively to the list of '
                        'channels to archive.',
                        action='store_true')
    parser.add_argument('-c',
                        '--channels-file',
                        help='Path to the channels list file to use if the '
                        'environment variable `CHANNELS` is not set ('
                        'default: ~/.yt_channels.txt).',
                        type=str)
    parser.add_argument('-S',
                        '--show-channels',
                        help='Show the list of channels in the channels file.',
                        action='store_true')
    parser.add_argument('-C',
                        '--create-collection',
                        help='Creates/appends to the backend database from '
                        'the channels list.',
                        action='store_true')
    parser.add_argument(
        '-m',
        '--multithreading',
        help='Enables processing multiple videos concurrently.',
        action='store_true')
    parser.add_argument('-T',
                        '--threads',
                        help='Number of threads to use when multithreading is '
                        'enabled. Defaults to the optimal maximum number of '
                        'workers.',
                        type=int)
    parser.add_argument(
        '-k',
        '--keep-failed-uploads',
        help='Keep the files of failed uploads on the local disk.',
        action='store_true')
    parser.add_argument('-i',
                        '--ignore-video-ids',
                        help='Comma-separated list or a path to a file '
                        'containing a list of video ids to ignore.',
                        type=str)
    parser.add_argument('-A',
                        '--use-aria2c',
                        help='Use external downloader, aria2c '
                        '(can significantly speed up download).',
                        action='store_true')
    return parser.parse_args()

def main() -> None:
    """Main function."""
    load_dotenv()
    args = _opts()

    signal.signal(signal.SIGALRM, _alarm_handler)
    timeout = int(args.timeout * 3600)

    if args.create_collection:
        _create_collection(no_logs=args.no_logs)
        sys.exit(0)

    if not args.channels_file:
        args.channels_file = f'{Path.home()}/.yt_channels.txt'
        if not os.getenv('CHANNELS'):
            os.environ['CHANNELS'] = args.channels_file

    if args.show_channels:
        chs_file = Path(args.channels_file)
        print('-' * 80)
        if not os.getenv('CHANNELS'):
            if not chs_file.exists():
                print('Nothing to show. Add channels to the channels '
                      'file first!')
            else:
                with open(chs_file) as f:
                    print(f.read())
            return
        elif args.show_channels and os.getenv('CHANNELS'):
            if chs_file.exists():
                with open(chs_file) as f:
                    print(f.read())
            else:
                print(os.environ['CHANNELS'])
        print('-' * 80)
        if chs_file.exists():
            print(f'Channels list file path: {chs_file}')
        return

    if args.add_channel:
        if os.getenv('CHANNELS'):
            if not Path(os.environ['CHANNELS']).exists():
                raise TypeError(
                    'Environment variable `CHANNELS` is set, but is not a '
                    'file path! Can only add channels with `--add-channel` '
                    'if `CHANNELS` is a file path.')
        with open(args.channels_file, 'a+') as f:
            f.seek(0, 0)
            if f.read():
                f.seek(0, io.SEEK_END)
                f.write('\n')
            channel_name = input('Channel name: ').replace(' ', '_').strip('_')
            channel_url = input('Channel URL: ')
            f.write(f'{channel_name}: {channel_url}')
        return

    if Path(args.channels_file).exists() and not args.no_logs:
        print(f'Using channels list in: {args.channels_file}')

    if args.prioritize:
        args.prioritize = args.prioritize.split(',')
    if args.skip_list:
        args.skip_list = args.skip_list.split(',')
    if args.ignore_video_ids:
        if Path(args.ignore_video_ids).exists():
            with open(args.ignore_video_ids) as f:
                args.ignore_video_ids = f.read().strip()
        args.ignore_video_ids = args.ignore_video_ids.split(',')

    try:
        signal.alarm(timeout)
        ayt = ArchiveYouTube(prioritize=args.prioritize,
                             skip_list=args.skip_list,
                             force_refresh=args.force_refresh,
                             no_logs=args.no_logs,
                             multithreading=args.multithreading,
                             threads=args.threads,
                             keep_failed_uploads=args.keep_failed_uploads,
                             ignore_video_ids=args.ignore_video_ids,
                             use_aria2c=args.use_aria2c)
        ayt.run()
    except TimeLimitReached:
        return

if __name__ == '__main__':
    main()


from numpy import NaN
import pandas as pd
import re
import os
from tqdm import tqdm

def load_and_prepare(in_folder: str):
    """
    The in_folder will contain two files:
     - person.ttl
     - name.ttl

    Combining the data to generate the y values (0 or 1),
    """

    # Reading and processing the name.ttl file
    names = {}

    print('reading name file . . .')
    with open(f'{in_folder}/name.ttl', 'r') as namefile:
        for i, line in enumerate(tqdm(namefile)):
            idx = re.search(r"(/)(Q[^>]+)(>)", line)
            if idx:
                idx = idx.group(2)
            else:
                idx = None

            name = re.search(
                r'(<http://xmlns.com/foaf/0.1/name>)\s"(.*)"@[^.]+.', line)
            if name:
                name = name.group(2)
            else:
                name = None

            names.update({i: {'idx': idx, 'name': name}})

    names_df = pd.DataFrame(names).T

    # Reading and processing the person.ttl file
    person_names = {}

    print('reading person file . . .')
    with open(f'{in_folder}/person.ttl', 'r') as namefile:
        for i, line in enumerate(tqdm(namefile)):
            idx = re.search(r"(/)(Q[^>]+)(>)", line)
            if idx:
                idx = idx.group(2)
            else:
                idx = None

            person_names.update({i: {'idx': idx}})

    person_df = pd.DataFrame(person_names).T

    print('Saving names file as csv . . .')
    names_df.to_csv(os.path.join(
        in_folder, 'unlabelled_names.csv'), index=False, encoding='utf-8-sig')

    print('Saving person file as csv . . .')
    person_df.to_csv(os.path.join(
        in_folder, 'person.csv'), index=False, encoding='utf-8-sig')

    # Merging both files to label the dataset properly
    person_df_list = list(person_df.idx)

    matches_bool = names_df.idx.isin(person_df_list)

    names_df['label'] = matches_bool * 1

    names_df.dropna()

    print('Saving labelled_dataset file as csv . . .')
    names_df.to_csv(os.path.join(
        in_folder, 'labelled_dataset.csv'), index=False, encoding='utf-8-sig')


#!/usr/bin/python

import nmap
import argparse

def nmapScan(tgtHost, tgtPort):
    nmScan = nmap.PortScanner()
    nmScan.scan(tgtHost, tgtPort)

    state = nmScan[tgtHost]['tcp'][int(tgtPort)]['state']
    print("[*] " + tgtHost + " tcp/" + tgtPort + " " + state)

def main():
    parser = argparse.ArgumentParser(description='nmapScan powerd by python')

    parser.add_argument('-H', metavar='host', help='specify target host')

    parser.add_argument('-P', metavar='ports',
                        help='specify target port[s] separated by comma')

    args = parser.parse_args()

    tgtHost = args.H
    tgtPorts = str(args.P).split(',')

    for tgtPort in tgtPorts:
        nmapScan(tgtHost, tgtPort)

if __name__ == '__main__':
    main()


def two_sum(array, target):
  #your code here
  for index1 in range(len(array)):
    for index2 in range(len(array)):
      item1 = array[index1]
      item2 = array[index2]
      if index1 != index2 and target == (item1 + item2):
        if index1 < index2:
          return ([index1, index2])
        else:
          return ([index2, index1])


import math as MM
import time
import audiobusio
import audioio
import array
import board

def FFTReimmannSIN(w,x):
    global dt
    out = 0
    for i in range(0,len(x)):
        ti = dt*i
        out += x[i]*(dt)*MM.sin(w*ti)
    return out

def FFTReimmannCOS(w,x):
    global dt
    out = 0
    for i in range(0,len(x)):
        ti = dt*i
        out += x[i]*(dt)*MM.cos(w*ti)
    return out

## 5 cycles for an 82 cycle signal
dt = (1/5125.)
N = 1000
nmax = 80
f = array.array("H", [0] * (N))
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16,mono=True)
L = dt*N
print(dt)
print(L)
#time.sleep(10)
while True:
    ##TAKE 100 DATA POINT AND PLACE INTO ARRAY
    print('Taking Data in 3 second...')
    time.sleep(3)
    mic.record(f, len(f)) ##Interpolates
    print(f)
    print(q)
    ctr = 0
    #for fi in f:
    #    ctr+=1
    #    if ctr > 1:
    #        if fi > 0:
    #            print((fi-32768-500,))
    #            ctr = 0
    #    time.sleep(0.01)
    print('Done')
    ###PERFORM FFT ON FREQUENCIES FROM 0 TO NMAX
    awn_fund = 0
    bwn_fund = 0
    anMAX = 0.0
    bnMAX = 0.0
    bf_fund = 0
    af_fund = 0
    print('Computing SFT....')
    for i in range(1,0):
        #print(i,' out of ',nmax)
        #Frequency
        w = 2.0*i*(MM.pi)/L
        #Reimann Sum
        ani = (2.0/L)*FFTReimmannSIN(w,f)
        bni = (2.0/L)*FFTReimmannCOS(w,f)
        if abs(ani) > anMAX:
            anMAX = abs(ani)
            awn_fund = w
            af_fund = (i-1)/L
        if abs(bni) > bnMAX:
            bnMAX = abs(bni)
            bwn_fund = w
            bf_fund = (i-1)/L
    if af_fund < bf_fund:
        f_fund = af_fund
    else:
        f_fund = bf_fund
    print('Fundamental Frequency (Hz) = ',af_fund,bf_fund)
    #print('Waiting  second')
    #time.sleep(10)


def sieve(n):
	lf, pr = [0] * (n + 1), []
	for i in range(2, n + 1):
		if lf[i] == 0:
			lf[i] = i
			pr.append(i)
		j = 0
		while j < len(pr) and pr[j] <= lf[i] and i * pr[j] <= n:
			lf[i * pr[j]] = pr[j]
			j += 1
		if i % 1000000 == 0:
			print(i)
	return (lf, pr)

n = 10 ** 9
lf, pr = sieve(100)

def gen(x, k):
	r = 1
	for i in range(k, len(pr)):
		l = x * pr[i]
		if l > n:
			break
		r += gen(l, i)
	return r
with open('answer.txt', 'w+') as af:
	print(gen(1, 0), file = af)
	af.close()


#!/usr/bin/env python

# 1.load a dataset from a file
# 2."organize that file so we can access columns *or* rows of it easily
# 3.compute some "summary statistics" about the dataset
# 4.print those summary statistics

import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from argparse import ArgumentParser

hist_dir_path='./hist_fig/'
scatter_dir_path='./scatter_fig/'
corr_dir_path='./corr_fig/'

features_list = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'BK', 'LSTAT', 'MEDV']
#load data

parser = ArgumentParser(description='A CVS reader + stats maker')
parser.add_argument('csvfile',
                    help='Path to the input csv file.')

parsed_args = parser.parse_args()

my_csv_file = parsed_args.csvfile

assert os.path.isfile(my_csv_file),"please give us a real file"
print('woot, the file exists')

data= pd.read_csv(my_csv_file, sep='\s+|,', header = None)
print(data.head())

print(data.shape)

#organize data
# 2A. ACCESS any row

print ("*Display the third and fourth rows*")
print (data.iloc[3:5,:]) # inclusive the first value 3 but exclusive the last value 5

# 2b access any column

print ("*Display the last two columns*")
print (data.iloc[:3,-2:]) #-2 the last two

# 2c access any value

print("*Display a specific value*")
print(data.iloc[3,4])

#compute summary statistics

print ("*Calculate mean*")
print (np.mean(data))
print ("*Calculate STD*")
print (np.std(data))

#D-Visualize the data, 1-feature (column) at a time

if not os.path.exists(hist_dir_path):
    os.makedirs(hist_dir_path)

sns.set() #for a nicer histogram

#loop on columns and generate histograms
for col in range(len(features_list)):

    df=(data.iloc[:,col])

    plt.figure()
    plt.hist(df, bins=20)
    plt.xlabel(features_list[col])
    plt.ylabel('occurrence')
    plt.title('Housing Data:{}'.format(features_list[col]))

    print ("Generating histogram file for {} in {} dir".format(features_list[col], hist_dir_path))

    plt.savefig('{}{}_hist.png'.format(hist_dir_path,features_list[col]))
    #plt.show() #we can uncomment to display all one by one
    plt.close() #avoid warnings

#E) Visualize the data, 2-features (columns) at a time

if not os.path.exists(scatter_dir_path):
    os.makedirs(scatter_dir_path)

#for col in range(len(features_list)):
for col1 in range(len(features_list)):
    df1=(data.iloc[:,col1])
    for col2 in range(len(features_list)):
        df2=(data.iloc[:,col2])
        if col1 != col2:
            #print ("{}/{}".format(features_list[col1],features_list[col2]))
            plt.figure()
            plt.scatter(df1,df2)
            plt.xlabel(features_list[col1])
            plt.ylabel(features_list[col2])
            plt.title('Housing Data:{}/{}'.format(features_list[col1], features_list[col2]))

            print ("Generating scatter file for {}/{} pair in {} dir".format(features_list[col1], features_list[col2], scatter_dir_path))

            plt.savefig('{}{}_{}_scatter.png'.format(scatter_dir_path,features_list[col1],features_list[col2]))
            #plt.show() #we can uncomment to display all one by one
            plt.close() #to avoid warnings

#F) Assign a header to a dataset

data_h= pd.read_csv(my_csv_file, sep='\s+|,', names = features_list)

print ("*Test dataset with header*")
print(data_h.head())

#G) Pseudocode for an additional type of plot: correlation matrix

if not os.path.exists(corr_dir_path):
    os.makedirs(corr_dir_path)

correlations = data.corr()
fig, ax = plt.subplots(figsize=(len(features_list), len(features_list)))
cax = ax.matshow(correlations, vmin=-1, vmax=1)
fig.colorbar(cax)

ax.set_xticks(range(0, len(features_list)))
ax.set_yticks(range(0, len(features_list)))

ax.set_xticklabels(features_list)
ax.set_yticklabels(features_list)

print ("Generating corrolation file for all features in {} dir".format(corr_dir_path))
plt.savefig('{}corrolation.png'.format(corr_dir_path))
#plt.show()
plt.close()


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

from src.talentPlatform.unitSys.helper.InitConfig import *
import warnings
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib as mpl
import glob
import cfgrib
import pandas as pd
import numpy as np

warnings.filterwarnings("ignore")

plt.rc('font', family='Malgun Gothic')
plt.rc('axes', unicode_minus=False)
sns.set(font="Malgun Gothic", rc={"axes.unicode_minus":False}, style='darkgrid')

# 그래프에서 마이너스 글꼴 깨지는 문제에 대한 대처
mpl.rcParams['axes.unicode_minus'] = False

# =================================================
# Set Env
# =================================================
# 작업환경 경로 설정
contextPath = '/SYSTEMS/PROG/PYTHON/GRIB2'
prjName = 'test'
serviceName = 'LSH0175'

log = initLog(contextPath, prjName)
globalVar = initGlobalVar(contextPath, prjName)

# =================================================
# Option
# =================================================
inLat = 37.56083
inLon = 129.2172
inVars = ['shww', 'wvdir', 'pp1d', 'u', 'v']

# =================================================
# Main
# =================================================
try:
    log.info('[START] {}'.format('Main'))

    fileList = glob.glob('{}/{}'.format(globalVar['inpPath'], '*.gb2'))

    for fileInfo in fileList:
        log.info('[CHECK] fileInfo : {}'.format(fileInfo))

        ds = cfgrib.open_dataset(fileInfo)
        dtDateTime = pd.to_datetime(ds.time.values).strftime('%Y%m%d%H%M')
        log.info('[CHECK] ds : {}'.format(ds))

        for inVar in inVars:
          log.info('[CHECK] inVar : {}'.format(inVar))

          # 오랜 시간이 소요
          # df = ds[inVar].to_dataframe()
          # saveFile = '{}/{}_{}_{}_{}.csv'.format(globalVar['outPath'], serviceName, inVar, 'TOTAL_DATA', dtDateTime)
          # df.to_csv(saveFile, index=None)

          dataL2 = pd.DataFrame()
          for i in ds[inVar]:
            dtDateTimeFore = pd.to_datetime((ds.time + i.step).values).strftime('%Y%m%d%H%M')
            nearVal = np.nan_to_num(i.sel(latitude = inLat, longitude = inLon, method='nearest'))
            interpVal = np.nan_to_num(i.interp(latitude = inLat, longitude = inLon))

            dict = {
              'inVar': [inVar]
              , 'dtDateTime': [dtDateTime]
              , 'dtDateTimeFore': [dtDateTimeFore]
              , 'nearVal': [nearVal]
              , 'interpVal': [interpVal]
            }
            log.info('[CHECK] dict : {}'.format(dict))

            dataL2 = dataL2.append(pd.DataFrame.from_dict(dict))

          saveFile = '{}/{}_{}_{}_{}.csv'.format(globalVar['outPath'], serviceName, inVar, 'SEL_DATA', dtDateTime)
          dataL2.to_csv(saveFile, index=None)

except Exception as e:
    log.error("Exception : {}".format(e))
    # traceback.print_exc()
    # sys.exit(1)

finally:
    log.info('[END] {}'.format('Main'))


import os, sys, gzip, random, json, datetime, re
import pandas as pd

dL = os.listdir(os.environ['LAV_DIR']+'/src/')
sys.path = list(set(sys.path + [os.environ['LAV_DIR']+'/src/'+x for x in dL]))
baseDir = os.environ['LAV_DIR']
import intertino.parser as pars

import importlib
importlib.reload(pars)

def conv_string(x):
    try: return float(x)
    except: return 0.

if False:
    """country code"""
    url = "https://www.countrycode.org/"
    couD = pars.parse_table(url,n_tab=0,render=True,isHead=True)
    couD.loc[:,"iso"] = couD['ISO CODES'].apply(lambda x: x[:2])
    couD.loc[:,"POPULATION"] = couD['POPULATION'].apply(lambda x: int(re.sub(",","",x)))
    couD.loc[:,"GDP"] = couD['GDP $USD'].apply(lambda x: x.split(" ")[0])
    couD.loc[:,"GDP_fact"] = 0.
    couD.loc[[bool(re.search("Trillion",x)) for x in couD['GDP $USD']],"GDP_fact"] = 1e12
    couD.loc[[bool(re.search("Billion",x)) for x in couD['GDP $USD']],"GDP_fact"] = 1e9
    couD.loc[[bool(re.search("Million",x)) for x in couD['GDP $USD']],"GDP_fact"] = 1e6
    couD.loc[[x == "" for x in couD['GDP']],"GDP"] = "0"
    couD.loc[:,"GDP"] = couD[['GDP','GDP_fact']].apply(lambda x: conv_string(x[0]) * x[1],axis=1)
    couD.drop(columns={"GDP_fact",'GDP $USD'},inplace=True)
    couD.loc[:,"AREA KM2"] = couD['AREA KM2'].apply(lambda x: conv_string(re.sub(",","",x)))
    couD.loc[:,"dens"] = couD['POPULATION']/couD['AREA KM2']
    couD.loc[:,"gdp_capita"] = couD['GDP']/couD['POPULATION']

    url = "https://developers.google.com/public-data/docs/canonical/countries_csv"
    cooD = pars.parse_tr(url,n_tab=0,render=True,isHead=True)
    couD = couD.merge(cooD,left_on="iso",right_on="country",how="left")
    couD.to_csv(baseDir + "geo/demo/country_demo.csv",index=False)

if False:
    """world population"""
    url = "https://www.worldometers.info/world-population/population-by-country/"
    invD = pars.parse_table(url,n_tab=0)
    invD.to_csv(baseDir + "geo/demo/pop_country.csv",index=False)

if False:
    """world population"""
    url = "https://www.worldometers.info/world-population/world-population-by-year/"
    invD = pars.parse_table(url,n_tab=0)
    invD.to_csv(baseDir + "geo/demo/pop_year.csv",index=False)

if False:
    """population density"""
    url = "https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population_density"
    invD = pars.parse_table(url,n_tab=0,render=True,isHead=False)
    invD.to_csv(baseDir + "geo/demo/pop_dens.csv",index=False)


listaCaracteres = []
contador = 0
contaConsoantes = 0
consoantes = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n",
                    "p", "q", "r", "s", "t", "v", "w", "x", "y", "z", "ç"
                    "B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N",
                    "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z"]
while contador < 10:
    if contador == 0:
        caractere = input('Digite um caractere: ')
    else:
        caractere = input('Digite outro caractere: ')
    if len(caractere) >= 2 or caractere.isdigit():
        print('Digito inválido! Tente novamente')
    else:
        contador+=1
        listaCaracteres.append(caractere)

for k in listaCaracteres:
    for l in consoantes:
        if k == l:
            contaConsoantes += 1
        else:
            contaConsoantes += 0
print()
print('Lista de caracteres:')
print(listaCaracteres)
print()
print(f'Número de consoantes: {contaConsoantes}')


import re
from tkinter.filedialog import askopenfilename
import threading
from ast import literal_eval
from time import time, sleep

from exploration import Exploration
from fastest_path import *
from connection_client import Sender

class Controller:
    def __init__(self, sim):
        self._is_sim = sim

        if self._is_sim:
            print('sim run')
            import tkinter as tk
            import GUI
            root = tk.Tk()
            self._gui = GUI.Window(root)
            self._gui.mainloop()
            from simulator import Robot
            self._robot = Robot(exploration_status=[[0] * 15 for _ in range(20)],
                                facing=NORTH,
                                discovered_map=[[2] * 15 for _ in range(20)],
                                real_map=[[]])
        else:
            print('real run')
            self._explore_limit = 100.0
            self._time_limit = 720
            from robot import Robot
            self._robot = Robot(exploration_status=[[0] * 15 for _ in range(20)],
                                facing=NORTH,
                                discovered_map=[[2] * 15 for _ in range(20)])
        self._sender = Sender(self._msg_handler)
        self._auto_update = True
        enable_print()
        print('init complete!')
        disable_print()
        try:
            import winsound
            frequency = 2500
            duration = 250
            winsound.Beep(frequency, duration)
            winsound.Beep(frequency, duration)
        except ImportError:
            enable_print()
            print('beep')
            disable_print()

    def _msg_handler(self, msg):
        if msg == "ca":
            thread = threading.Thread(target=self._calibrate)
            thread.daemon = True
            thread.start()
        elif msg == "be":
            thread = threading.Thread(target=self._explore)
            thread.daemon = True
            thread.start()
        elif msg == "au":
            self._auto_update = True
        elif msg == "mu":
            self._auto_update = False
            self._update_android(True, True, override=True)
        elif msg[0:8] == "waypoint":
            self._set_way_point(msg[9:])
        elif msg == "bf":
            thread = threading.Thread(target=self._move_fastest_path)
            thread.daemon = True
            thread.start()
        elif msg == "st":
            sleep(0.05)
        elif msg == "w":
            self._sender.send_arduino('w')
        elif msg == "a":
            self._sender.send_arduino('a')
        elif msg == "d":
            self._sender.send_arduino('d')
        elif msg == "s":
            self._sender.send_arduino('s')
        elif msg == "o":
            self._sender.send_arduino('g')

    def _set_way_point(self, coordinate):
        (col, row) = literal_eval(coordinate)
        self._way_point = (row, col)

    def _calibrate(self):
        if not self._is_sim:
            self._sender.send_android('{"status":"calibrating"}')
            self._sender.send_arduino('z')
            self._sender.wait_arduino('D')
            self._robot.turn_robot(self._sender, RIGHT)
            self._robot.get_sensor_readings(self._sender)
            self._robot.turn_robot(self._sender, LEFT)
            self._sender.send_arduino('z')
            self._sender.wait_arduino('D')
        self._sender.send_android('{"status":"calibrating done"}')

    def _update_map_android(self):
        self._sender.send_android('{"exploreMap":"%s"}' % self._robot.get_explore_string())
        self._sender.send_android('{"obstacleMap":"%s"}' % self._robot.get_map_string())

    def _update_coords_android(self):
        y, x = get_coordinates(self._robot.center)
        self._sender.send_android('{"robotPosition":[%s,%s,%s]}' % (str(x), str(y), str(self._robot.facing)))

    def _update_android(self, is_update_map, is_update_coords, override=False):
        if self._auto_update or override:
            if is_update_map:
                self._update_map_android()
            if is_update_coords:
                self._update_coords_android()

    def _explore(self):
        start_time = time()

        exploration = Exploration(self._robot, start_time, self._explore_limit, self._time_limit)

        if self._is_sim:
            run = exploration.start_real_simulation()
        else:
            run = exploration.start_real(self._sender)

        next(run)
        self._update_android(True, True)
        while True:
            try:
                # Exploration until completion

                while True:
                    run.send(0)
                    self._update_android(True, True)

                    run.send(0)
                    if self._is_sim:
                        sleep(1)

                    self._update_android(True, True)

                    is_complete = run.send(0)
                    if is_complete:
                        break

                    is_looped = run.send(0)
                    if is_looped:
                        while True:
                            updated_or_moved, value, is_complete = run.send(0)
                            if self._is_sim:
                                sleep(1)
                            if updated_or_moved == "updated" or updated_or_moved == "moved":
                                self._update_android(True, True)
                                if updated_or_moved == "updated":
                                    self._gui.update_cells(value)
                                elif updated_or_moved == "moved":
                                    self._gui.move_robot(value)
                            else:
                                break

                            if is_complete:
                                break
                        break

                # Returning to start after completion
                while True:
                    run.send(0)
                    if self._is_sim:
                        sleep(1)

                    self._update_android(True, True)

            except StopIteration:
                if not self._is_sim:
                    enable_print()
                    print('EXPLORE_STR:', self._robot.get_explore_string())
                    print('MAP_STR:', self._robot.get_map_string())
                    disable_print()
                    print('Returned to start!')
                    self._sender.send_arduino('j')
                    self._sender.wait_arduino('U')
                    if self._robot.facing == WEST:
                        print('ENTERED FACING WEST')
                        sleep(0.05)
                        self._robot.turn_robot(self._sender, LEFT)
                        self._sender.send_arduino('j')
                        self._sender.wait_arduino('U')
                        self._robot.turn_robot(self._sender, RIGHT)
                        sleep(0.05)
                        self._robot.turn_robot(self._sender, RIGHT)
                        sleep(0.05)
                        self._sender.send_arduino('x')
                        self._sender.wait_arduino('D')
                    elif self._robot.facing == SOUTH:
                        print('ENTERED FACING SOUTH')
                        sleep(0.05)
                        self._robot.turn_robot(self._sender, RIGHT)
                        self._sender.send_arduino('j')
                        self._sender.wait_arduino('U')
                        sleep(0.05)
                        self._robot.turn_robot(self._sender, RIGHT)
                        sleep(0.05)
                        self._sender.send_arduino('x')
                        self._sender.wait_arduino('D')
                break

        print('Calibrating...')
        self._calibrate_after_exploration()

    def _calibrate_after_exploration(self):
        self._fastest_path = self._find_fastest_path()

        if self._is_sim:
            sleep(1)
            self._robot.turn_robot(self._fastest_path[0])
        else:
            self._robot.turn_robot(self._sender, self._fastest_path[0])
        self._fastest_path[0] = FORWARD
        self._update_android(True, True)
        self._sender.send_android('{"status":"explore done"}')
        self._sender.send_android('{"status":"calibrating done"}')

    def _find_fastest_path(self):
        """Calculate and return the set of moves required for the fastest path."""
        from simulator import Robot
        clone_robot = Robot(exploration_status=self._robot.exploration_status,
                            facing=self._robot.facing,
                            discovered_map=self._robot.discovered_map,
                            real_map=[[0] * 15 for _ in range(20)])

        fastest_path_start_way_point = get_shortest_path_moves(clone_robot,
                                                               start=(1, 1),
                                                               goal=self._way_point)

        if fastest_path_start_way_point:
            for move in fastest_path_start_way_point:
                clone_robot.move_robot(move)

        before_way_point = previous_cell(clone_robot.center, clone_robot.facing)

        fastest_path_way_point_goal = get_shortest_path_moves(clone_robot,
                                                              start=self._way_point,
                                                              goal=(18, 13),
                                                              before_start_point=before_way_point)

        return fastest_path_start_way_point + fastest_path_way_point_goal

    def _move_fastest_path(self):
        if self._fastest_path:
            if self._is_sim:
                for move in self._fastest_path:
                    if self._is_sim:
                        sleep(1)

                    self._robot.move_robot(move)
                    self._update_android(True, True)
            else:
                move_str = get_fastest_path_move_string(self._fastest_path)
                moves = move_str.split('/')
                for move in moves:
                    move_len = len(move)
                    if move[0] == 'n':
                        move = move[:-1] + 't'
                    elif move[0] == 'a' or move[0] == 'd':
                        pass
                    self._sender.send_arduino(move)
                    for i in range(move_len):
                        self._sender.wait_arduino('M')
        else:
            print("No valid path")