# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
"""
Plotting utils
"""

import math
import os
from copy import copy
from pathlib import Path

import cv2
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sn
import torch
from PIL import Image, ImageDraw, ImageFont
import pdb

from utils.general import (LOGGER, Timeout, check_requirements, clip_coords, increment_path, is_ascii, is_chinese,
                           try_except, user_config_dir, xywh2xyxy, xyxy2xywh)
from utils.metrics import fitness
import time
# Settings
CONFIG_DIR = user_config_dir()  # Ultralytics settings dir
RANK = int(os.getenv('RANK', -1))
matplotlib.rc('font', **{'size': 11})
matplotlib.use('Agg')  # for writing to files only

class Colors:
    # Ultralytics color palette https://ultralytics.com/
    def __init__(self):
        # hex = matplotlib.colors.TABLEAU_COLORS.values()
        hex = ('FF3838', 'FF9D97', 'FF701F', 'FFB21D', 'CFD231', '48F90A', '92CC17', '3DDB86', '1A9334', '00D4BB',
               '2C99A8', '00C2FF', '344593', '6473FF', '0018EC', '8438FF', '520085', 'CB38FF', 'FF95C8', 'FF37C7')
        self.palette = [self.hex2rgb('#' + c) for c in hex]
        self.n = len(self.palette)

    def __call__(self, i, bgr=False):
        c = self.palette[int(i) % self.n]
        return (c[2], c[1], c[0]) if bgr else c

    @staticmethod
    def hex2rgb(h):  # rgb order (PIL)
        return tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4))

colors = Colors()  # create instance for 'from utils.plots import colors'

def check_font(font='Arial.ttf', size=10):
    # Return a PIL TrueType Font, downloading to CONFIG_DIR if necessary
    font = Path(font)
    font = font if font.exists() else (CONFIG_DIR / font.name)
    try:
        return ImageFont.truetype(str(font) if font.exists() else font.name, size)
    except Exception as e:  # download if missing
        url = "https://ultralytics.com/assets/" + font.name
        print(f'Downloading {url} to {font}...')
        torch.hub.download_url_to_file(url, str(font), progress=False)
        try:
            return ImageFont.truetype(str(font), size)
        except TypeError:
            check_requirements('Pillow>=8.4.0')  # known issue https://github.com/ultralytics/yolov5/issues/5374

class Annotator:
    if RANK in (-1, 0):
        check_font()  # download TTF if necessary

    # YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
    def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc'):
        assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.'
        self.pil = pil or not is_ascii(example) or is_chinese(example)
        if self.pil:  # use PIL
            self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
            self.draw = ImageDraw.Draw(self.im)
            self.font = check_font(font='Arial.Unicode.ttf' if is_chinese(example) else font,
                                   size=font_size or max(round(sum(self.im.size) / 2 * 0.035), 12))
        else:  # use cv2
            self.im = im
        self.lw = line_width or max(round(sum(im.shape) / 2 * 0.003), 2)  # line width

    def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)):
        # Add one xyxy box to image with label
        if self.pil or not is_ascii(label):
            self.draw.rectangle(box, width=self.lw, outline=color)  # box
            if label:
                w, h = self.font.getsize(label)  # text width, height
                outside = box[1] - h >= 0  # label fits outside box
                self.draw.rectangle([box[0],
                                     box[1] - h if outside else box[1],
                                     box[0] + w + 1,
                                     box[1] + 1 if outside else box[1] + h + 1], fill=color)
                # self.draw.text((box[0], box[1]), label, fill=txt_color, font=self.font, anchor='ls')  # for PIL>8.0
                self.draw.text((box[0], box[1] - h if outside else box[1]), label, fill=txt_color, font=self.font)
        else:  # cv2
            p1, p2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
            cv2.rectangle(self.im, p1, p2, color, thickness=self.lw, lineType=cv2.LINE_AA)
            if label:
                tf = max(self.lw - 1, 1)  # font thickness
                w, h = cv2.getTextSize(label, 0, fontScale=self.lw / 3, thickness=tf)[0]  # text width, height
                outside = p1[1] - h - 3 >= 0  # label fits outside box
                p2 = p1[0] + w, p1[1] - h - 3 if outside else p1[1] + h + 3
                cv2.rectangle(self.im, p1, p2, color, -1, cv2.LINE_AA)  # filled
                cv2.putText(self.im, label, (p1[0], p1[1] - 2 if outside else p1[1] + h + 2), 0, self.lw / 3, txt_color,
                            thickness=tf, lineType=cv2.LINE_AA)

    def rectangle(self, xy, fill=None, outline=None, width=1):
        # Add rectangle to image (PIL-only)
        self.draw.rectangle(xy, fill, outline, width)

    def text(self, xy, text, txt_color=(255, 255, 255)):
        # Add text to image (PIL-only)
        w, h = self.font.getsize(text)  # text width, height
        self.draw.text((xy[0], xy[1] - h + 1), text, fill=txt_color, font=self.font)

    def result(self):
        # Return annotated image as array
        return np.asarray(self.im)

    ##### Trajectory code #####
    def draw_grtr(self, img, positions, width):
        for t in range(len(positions) - 1):
            cv2.line(img, (round(positions[t][0]), round(positions[t][1])), \
                     (round(positions[t + 1][0]), round(positions[t + 1][1])), \
                     color=(255,255,255), thickness=width, lineType=cv2.LINE_AA)

    def draw_trajectory(self, diverse_traj_data, blur_size, dets, frame_id, color):
        ### Ask ###
        # draw_start = time.time()
        w = dets[:, 2] - dets[:, 0]
        h = dets[:, 3] - dets[:, 1]
        cxs = dets[:, 0] + (w//2)
        y2s = dets[:, 1] + h #
        ########
        traj_mask = np.zeros((self.im.shape[0], self.im.shape[1], 3))

        # line_mask = np.zeros((npy_line.shape[0], npy_line.shape[1], 3))
        line_mask = np.zeros((self.im.shape[0], self.im.shape[1], 3))

        final = None

        bot_max = -1
        for l in range(len(cxs)):
            cx = cxs[l]
            y2 = y2s[l]

            # if cx >= npy_line.shape[1]:
            #     cx = npy_line.shape[1] - 1
            # if y2 > npy_line.shape[0]:
            #     y2 = npy_line.shape[0] - 1

            if cx >= self.im.shape[1]:
                cx = self.im.shape[1] - 1
            if y2 > self.im.shape[0]:
                y2 = self.im.shape[0] - 1

        # draw_fin = time.time()
        # print("draw cost: ")

        if frame_id in diverse_traj_data.keys():
            frame_data = diverse_traj_data[frame_id] # n_samples, num_id, timestep, coordinate (20, 2, 25, 4)
            traj_layers = np.zeros((20, self.im.shape[0], self.im.shape[1]), np.uint8)
            for i, sample in enumerate(frame_data): # i: sample_id, sample: different_traj in a frame
                routes = None
                for agent_data in sample:
                    route = agent_data[:, 2:4]
                    self.draw_grtr(traj_layers[i], route, 15)

            heatmap_layer = np.sum(traj_layers, axis=0).astype(np.uint8)
            heatmap_layer = cv2.blur(heatmap_layer, (blur_size, blur_size))
            heat = heatmap_layer.copy()

            heat[heat>0] = 255
            traj_mask[heat==255] = color
            index_y, index_x = np.where(heat == 255)[0], np.where(heat == 255)[1]
            tra_max = -1
            for iy, ix in zip(index_y, index_x):
                # if line_mask[iy, ix, 1] == 255 and line_mask[iy, ix, 2] == 0 and iy > tra_max:
                if iy > tra_max: # Add new line
                    tra_max = iy
            if tra_max >= 0:
                #line_mask[npy_line==1] = (0,255,0)
                line_mask[:tra_max, :, [1, 2]] = line_mask[:tra_max, :, [2, 1]]
            else:
                line_mask = line_mask

            heat_gray = cv2.cvtColor(traj_mask.astype(np.uint8), cv2.COLOR_BGR2GRAY)
            cnts, hierarchy = cv2.findContours(heat_gray.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

            heatmap_layer = cv2.applyColorMap(heatmap_layer, cv2.COLORMAP_HOT)
            final_1 = cv2.addWeighted(line_mask.astype(np.uint8), 1, self.im, 1, 0)
            final_2 = cv2.addWeighted(traj_mask.astype(np.uint8), 1, self.im, 1, 0)
            final = cv2.addWeighted(final_1, 0.5, final_2, 0.5, 0)
            cv2.drawContours(final, cnts, -1, (0, 0, 0), 1, lineType=cv2.LINE_AA)

        else:
            final = cv2.addWeighted(line_mask.astype(np.uint8), 1, self.im, 1, 0)
            #final_2 = image
            heat = np.zeros((self.im.shape[0], self.im.shape[1]))

        #final = cv2.resize(final, (0,0), fx = 0.5, fy = 0.5)
        self.im = final
    ###########

    ##### Sound Signal code #####
    def addTogether(self, car,x,y, type='icon'):   # img為背景圖 ,car為車子圖 , x,y為圖像在背景的位置

        rows,cols,channels = car.shape #拆分圖片信息
        #轉換格式
        img_hsv = cv2.cvtColor(car,cv2.COLOR_RGB2HSV) #把圖片轉換成HSV格式，用於摳圖
        #摳圖
        lower_blue=np.array([0,0,0]) #獲取最小閾值
        #upper_blue=np.array([0,255,255]) #獲取最大閾值
        upper_color = np.array([0,0,0])
        if type == 'car':
            upper_color = np.array([0,255,255])
        else:
            upper_color = np.array([255,0,0])
        mask = cv2.inRange(img_hsv, lower_blue, upper_color) #創建遮罩

        erode=cv2.erode(mask,None,iterations=3) #圖像腐蝕

        dilate=cv2.dilate(erode,None,iterations=1) #圖像膨脹

        opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (8,8))) #開運算

        #========圖像合併==========================================================================================
        center = [y,x] #設置car在背景圖的起始位置
        for i in range(rows):
            for j in range(cols):
                if opening[i,j]==0: #代表黑色
                    self.im[center[0]+i,center[1]+j] =car[i,j] #賦值顏色

    def sound_signal(self, data): # Add signal
        #導入車子圖
        #global tmp # Take the global var tmp
        car = cv2.imread('/home/ziyan/Yolov5_DeepSort_Pytorch_ros/Yolov5_DeepSort_Pytorch/icon_imgs/car.png') #car圖片導入
        car = cv2.resize(car,(0,0),fx=0.4,fy=0.4) # 0.15, 0.15

        #導入ambulance
        amb = cv2.imread('/home/ziyan/Yolov5_DeepSort_Pytorch_ros/Yolov5_DeepSort_Pytorch/icon_imgs/type_icon/amb.png') #ambulance圖片導入
        #pdb.set_trace()
        amb = cv2.resize(amb,(0,0),fx=1.2,fy=1.2)
        #pdb.set_trace()

        fire = cv2.imread('/home/ziyan/Yolov5_DeepSort_Pytorch_ros/Yolov5_DeepSort_Pytorch/icon_imgs/type_icon/fire.png') #ambulance圖片導入
        #pdb.set_trace()
        fire = cv2.resize(amb,(0,0),fx=1.2,fy=1.2)

        police = cv2.imread('/home/ziyan/Yolov5_DeepSort_Pytorch_ros/Yolov5_DeepSort_Pytorch/icon_imgs/type_icon/police.png') #ambulance圖片導入
        #pdb.set_trace()
        police = cv2.resize(amb,(0,0),fx=1.2,fy=1.2)

        #================初始化座標值=====================
        x = int(0.1*self.im.shape[1]) #圓的x座標 圖片的寬的倍數 shape=(length,width,channel)
        y = int(0.8*self.im.shape[0]) #圓的y座標
        r =int(0.15*self.im.shape[0]) #圓的半徑

        #================畫出圓形=====================
        cv2.circle(self.im,(x,y),r,(84,46,8),-1)
        m = 3 #畫m個同心圓
        r_circle = r/m
        for i in range(1,m+1):
            cv2.circle(self.im,(x,y),int(r_circle*i),(255,255,255),1)

        #================畫出直線=====================
        #每45度畫一條線
        #直線點座標(x_line,y_line)
        n = 12 #畫出n等份的圓
        degree_line = 360/n #每 degree_line度 就畫線
        #print("degree_line:"+str(degree_line))

        for i in range(n):
            theta_line =  ((i*degree_line)/180)*math.pi #角度轉成弧度
            x_line = int(x + r*math.cos(theta_line))
            y_line = int(y - r*math.sin(theta_line))
            cv2.line(self.im,(x,y),(x_line,y_line),(250,255,255),1)

        #===========================================
        #方位角 角度為n等份的範圍值 (畫扇形:橢圓的長軸與短軸=圓形半徑)
        angle, label_type = data.split(',')
        label_list = [2,3,4]
        if label_type in label_list:
            ### Select a specific signal
            p = (12 - angle // 30) % 12 # 330->11->1, 30->1->11
            #p = 7 # 7*30
            #p = np.random.randint(0,n)  #隨機挑選第n等份的某值 -> Also, the pic being select is not in the correct order (Double random)

            #print("p:"+str(p))
            degree_elli_start = p * degree_line # degree_line=360/n #起始角度
            degree_elli_end = (p+1) * degree_line #終止角度

            # #橢圓(圖片名稱,中心點,(長軸,短軸),旋轉角度（順時針方向）,繪製的起始角度（順時針方向）,繪製的終止角度(順時針方向),線條顏色,線條粗細)
            cv2.ellipse(self.im,(x,y),(r,r),0,-degree_elli_start,-degree_elli_end,(0,215,255),-1)

        # x,y
        self.addTogether(car, x - car.shape[1]//2, y - car.shape[0]//2, type='car')

        if label_type == 2: # police
            self.addTogether(police, 2*x,y - police.shape[0]//2)
        elif label_type == 3: # fire
            self.addTogether(fire, 2*x,y - fire.shape[0]//2)
        elif label_type == 4: # amb
            self.addTogether(amb, 2*x,y - amb.shape[0]//2)

    #return draw_img

    ###########

def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detect/exp')):
    """
    x:              Features to be visualized
    module_type:    Module type
    stage:          Module stage within model
    n:              Maximum number of feature maps to plot
    save_dir:       Directory to save results
    """
    if 'Detect' not in module_type:
        batch, channels, height, width = x.shape  # batch, channels, height, width
        if height > 1 and width > 1:
            f = save_dir / f"stage{stage}_{module_type.split('.')[-1]}_features.png"  # filename

            blocks = torch.chunk(x[0].cpu(), channels, dim=0)  # select batch index 0, block by channels
            n = min(n, channels)  # number of plots
            fig, ax = plt.subplots(math.ceil(n / 8), 8, tight_layout=True)  # 8 rows x n/8 cols
            ax = ax.ravel()
            plt.subplots_adjust(wspace=0.05, hspace=0.05)
            for i in range(n):
                ax[i].imshow(blocks[i].squeeze())  # cmap='gray'
                ax[i].axis('off')

            print(f'Saving {f}... ({n}/{channels})')
            plt.savefig(f, dpi=300, bbox_inches='tight')
            plt.close()
            np.save(str(f.with_suffix('.npy')), x[0].cpu().numpy())  # npy save

def hist2d(x, y, n=100):
    # 2d histogram used in labels.png and evolve.png
    xedges, yedges = np.linspace(x.min(), x.max(), n), np.linspace(y.min(), y.max(), n)
    hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges))
    xidx = np.clip(np.digitize(x, xedges) - 1, 0, hist.shape[0] - 1)
    yidx = np.clip(np.digitize(y, yedges) - 1, 0, hist.shape[1] - 1)
    return np.log(hist[xidx, yidx])

def butter_lowpass_filtfilt(data, cutoff=1500, fs=50000, order=5):
    from scipy.signal import butter, filtfilt

    # https://stackoverflow.com/questions/28536191/how-to-filter-smooth-with-scipy-numpy
    def butter_lowpass(cutoff, fs, order):
        nyq = 0.5 * fs
        normal_cutoff = cutoff / nyq
        return butter(order, normal_cutoff, btype='low', analog=False)

    b, a = butter_lowpass(cutoff, fs, order=order)
    return filtfilt(b, a, data)  # forward-backward filter

def output_to_target(output):
    # Convert model output to target format [batch_id, class_id, x, y, w, h, conf]
    targets = []
    for i, o in enumerate(output):
        for *box, conf, cls in o.cpu().numpy():
            targets.append([i, cls, *list(*xyxy2xywh(np.array(box)[None])), conf])
    return np.array(targets)

def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max_size=1920, max_subplots=16):
    # Plot image grid with labels
    if isinstance(images, torch.Tensor):
        images = images.cpu().float().numpy()
    if isinstance(targets, torch.Tensor):
        targets = targets.cpu().numpy()
    if np.max(images[0]) <= 1:
        images *= 255  # de-normalise (optional)
    bs, _, h, w = images.shape  # batch size, _, height, width
    bs = min(bs, max_subplots)  # limit plot images
    ns = np.ceil(bs ** 0.5)  # number of subplots (square)

    # Build Image
    mosaic = np.full((int(ns * h), int(ns * w), 3), 255, dtype=np.uint8)  # init
    for i, im in enumerate(images):
        if i == max_subplots:  # if last batch has fewer images than we expect
            break
        x, y = int(w * (i // ns)), int(h * (i % ns))  # block origin
        im = im.transpose(1, 2, 0)
        mosaic[y:y + h, x:x + w, :] = im

    # Resize (optional)
    scale = max_size / ns / max(h, w)
    if scale < 1:
        h = math.ceil(scale * h)
        w = math.ceil(scale * w)
        mosaic = cv2.resize(mosaic, tuple(int(x * ns) for x in (w, h)))

    # Annotate
    fs = int((h + w) * ns * 0.01)  # font size
    annotator = Annotator(mosaic, line_width=round(fs / 10), font_size=fs, pil=True)
    for i in range(i + 1):
        x, y = int(w * (i // ns)), int(h * (i % ns))  # block origin
        annotator.rectangle([x, y, x + w, y + h], None, (255, 255, 255), width=2)  # borders
        if paths:
            annotator.text((x + 5, y + 5 + h), text=Path(paths[i]).name[:40], txt_color=(220, 220, 220))  # filenames
        if len(targets) > 0:
            ti = targets[targets[:, 0] == i]  # image targets
            boxes = xywh2xyxy(ti[:, 2:6]).T
            classes = ti[:, 1].astype('int')
            labels = ti.shape[1] == 6  # labels if no conf column
            conf = None if labels else ti[:, 6]  # check for confidence presence (label vs pred)

            if boxes.shape[1]:
                if boxes.max() <= 1.01:  # if normalized with tolerance 0.01
                    boxes[[0, 2]] *= w  # scale to pixels
                    boxes[[1, 3]] *= h
                elif scale < 1:  # absolute coords need scale if image scales
                    boxes *= scale
            boxes[[0, 2]] += x
            boxes[[1, 3]] += y
            for j, box in enumerate(boxes.T.tolist()):
                cls = classes[j]
                color = colors(cls)
                cls = names[cls] if names else cls
                if labels or conf[j] > 0.25:  # 0.25 conf thresh
                    label = f'{cls}' if labels else f'{cls} {conf[j]:.1f}'
                    annotator.box_label(box, label, color=color)
    annotator.im.save(fname)  # save

def plot_lr_scheduler(optimizer, scheduler, epochs=300, save_dir=''):
    # Plot LR simulating training for full epochs
    optimizer, scheduler = copy(optimizer), copy(scheduler)  # do not modify originals
    y = []
    for _ in range(epochs):
        scheduler.step()
        y.append(optimizer.param_groups[0]['lr'])
    plt.plot(y, '.-', label='LR')
    plt.xlabel('epoch')
    plt.ylabel('LR')
    plt.grid()
    plt.xlim(0, epochs)
    plt.ylim(0)
    plt.savefig(Path(save_dir) / 'LR.png', dpi=200)
    plt.close()

def plot_val_txt():  # from utils.plots import *; plot_val()
    # Plot val.txt histograms
    x = np.loadtxt('val.txt', dtype=np.float32)
    box = xyxy2xywh(x[:, :4])
    cx, cy = box[:, 0], box[:, 1]

    fig, ax = plt.subplots(1, 1, figsize=(6, 6), tight_layout=True)
    ax.hist2d(cx, cy, bins=600, cmax=10, cmin=0)
    ax.set_aspect('equal')
    plt.savefig('hist2d.png', dpi=300)

    fig, ax = plt.subplots(1, 2, figsize=(12, 6), tight_layout=True)
    ax[0].hist(cx, bins=600)
    ax[1].hist(cy, bins=600)
    plt.savefig('hist1d.png', dpi=200)

def plot_targets_txt():  # from utils.plots import *; plot_targets_txt()
    # Plot targets.txt histograms
    x = np.loadtxt('targets.txt', dtype=np.float32).T
    s = ['x targets', 'y targets', 'width targets', 'height targets']
    fig, ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True)
    ax = ax.ravel()
    for i in range(4):
        ax[i].hist(x[i], bins=100, label=f'{x[i].mean():.3g} +/- {x[i].std():.3g}')
        ax[i].legend()
        ax[i].set_title(s[i])
    plt.savefig('targets.jpg', dpi=200)

def plot_val_study(file='', dir='', x=None):  # from utils.plots import *; plot_val_study()
    # Plot file=study.txt generated by val.py (or plot all study*.txt in dir)
    save_dir = Path(file).parent if file else Path(dir)
    plot2 = False  # plot additional results
    if plot2:
        ax = plt.subplots(2, 4, figsize=(10, 6), tight_layout=True)[1].ravel()

    fig2, ax2 = plt.subplots(1, 1, figsize=(8, 4), tight_layout=True)
    # for f in [save_dir / f'study_coco_{x}.txt' for x in ['yolov5n6', 'yolov5s6', 'yolov5m6', 'yolov5l6', 'yolov5x6']]:
    for f in sorted(save_dir.glob('study*.txt')):
        y = np.loadtxt(f, dtype=np.float32, usecols=[0, 1, 2, 3, 7, 8, 9], ndmin=2).T
        x = np.arange(y.shape[1]) if x is None else np.array(x)
        if plot2:
            s = ['P', 'R', 'mAP@.5', 'mAP@.5:.95', 't_preprocess (ms/img)', 't_inference (ms/img)', 't_NMS (ms/img)']
            for i in range(7):
                ax[i].plot(x, y[i], '.-', linewidth=2, markersize=8)
                ax[i].set_title(s[i])

        j = y[3].argmax() + 1
        ax2.plot(y[5, 1:j], y[3, 1:j] * 1E2, '.-', linewidth=2, markersize=8,
                 label=f.stem.replace('study_coco_', '').replace('yolo', 'YOLO'))

    ax2.plot(1E3 / np.array([209, 140, 97, 58, 35, 18]), [34.6, 40.5, 43.0, 47.5, 49.7, 51.5],
             'k.-', linewidth=2, markersize=8, alpha=.25, label='EfficientDet')

    ax2.grid(alpha=0.2)
    ax2.set_yticks(np.arange(20, 60, 5))
    ax2.set_xlim(0, 57)
    ax2.set_ylim(25, 55)
    ax2.set_xlabel('GPU Speed (ms/img)')
    ax2.set_ylabel('COCO AP val')
    ax2.legend(loc='lower right')
    f = save_dir / 'study.png'
    print(f'Saving {f}...')
    plt.savefig(f, dpi=300)

@try_except  # known issue https://github.com/ultralytics/yolov5/issues/5395
@Timeout(30)  # known issue https://github.com/ultralytics/yolov5/issues/5611
def plot_labels(labels, names=(), save_dir=Path('')):
    # plot dataset labels
    LOGGER.info(f"Plotting labels to {save_dir / 'labels.jpg'}... ")
    c, b = labels[:, 0], labels[:, 1:].transpose()  # classes, boxes
    nc = int(c.max() + 1)  # number of classes
    x = pd.DataFrame(b.transpose(), columns=['x', 'y', 'width', 'height'])

    # seaborn correlogram
    sn.pairplot(x, corner=True, diag_kind='auto', kind='hist', diag_kws=dict(bins=50), plot_kws=dict(pmax=0.9))
    plt.savefig(save_dir / 'labels_correlogram.jpg', dpi=200)
    plt.close()

    # matplotlib labels
    matplotlib.use('svg')  # faster
    ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True)[1].ravel()
    y = ax[0].hist(c, bins=np.linspace(0, nc, nc + 1) - 0.5, rwidth=0.8)
    # [y[2].patches[i].set_color([x / 255 for x in colors(i)]) for i in range(nc)]  # update colors bug #3195
    ax[0].set_ylabel('instances')
    if 0 < len(names) < 30:
        ax[0].set_xticks(range(len(names)))
        ax[0].set_xticklabels(names, rotation=90, fontsize=10)
    else:
        ax[0].set_xlabel('classes')
    sn.histplot(x, x='x', y='y', ax=ax[2], bins=50, pmax=0.9)
    sn.histplot(x, x='width', y='height', ax=ax[3], bins=50, pmax=0.9)

    # rectangles
    labels[:, 1:3] = 0.5  # center
    labels[:, 1:] = xywh2xyxy(labels[:, 1:]) * 2000
    img = Image.fromarray(np.ones((2000, 2000, 3), dtype=np.uint8) * 255)
    for cls, *box in labels[:1000]:
        ImageDraw.Draw(img).rectangle(box, width=1, outline=colors(cls))  # plot
    ax[1].imshow(img)
    ax[1].axis('off')

    for a in [0, 1, 2, 3]:
        for s in ['top', 'right', 'left', 'bottom']:
            ax[a].spines[s].set_visible(False)

    plt.savefig(save_dir / 'labels.jpg', dpi=200)
    matplotlib.use('Agg')
    plt.close()

def plot_evolve(evolve_csv='path/to/evolve.csv'):  # from utils.plots import *; plot_evolve()
    # Plot evolve.csv hyp evolution results
    evolve_csv = Path(evolve_csv)
    data = pd.read_csv(evolve_csv)
    keys = [x.strip() for x in data.columns]
    x = data.values
    f = fitness(x)
    j = np.argmax(f)  # max fitness index
    plt.figure(figsize=(10, 12), tight_layout=True)
    matplotlib.rc('font', **{'size': 8})
    for i, k in enumerate(keys[7:]):
        v = x[:, 7 + i]
        mu = v[j]  # best single result
        plt.subplot(6, 5, i + 1)
        plt.scatter(v, f, c=hist2d(v, f, 20), cmap='viridis', alpha=.8, edgecolors='none')
        plt.plot(mu, f.max(), 'k+', markersize=15)
        plt.title(f'{k} = {mu:.3g}', fontdict={'size': 9})  # limit to 40 characters
        if i % 5 != 0:
            plt.yticks([])
        print(f'{k:>15}: {mu:.3g}')
    f = evolve_csv.with_suffix('.png')  # filename
    plt.savefig(f, dpi=200)
    plt.close()
    print(f'Saved {f}')

def plot_results(file='path/to/results.csv', dir=''):
    # Plot training results.csv. Usage: from utils.plots import *; plot_results('path/to/results.csv')
    save_dir = Path(file).parent if file else Path(dir)
    fig, ax = plt.subplots(2, 5, figsize=(12, 6), tight_layout=True)
    ax = ax.ravel()
    files = list(save_dir.glob('results*.csv'))
    assert len(files), f'No results.csv files found in {save_dir.resolve()}, nothing to plot.'
    for fi, f in enumerate(files):
        try:
            data = pd.read_csv(f)
            s = [x.strip() for x in data.columns]
            x = data.values[:, 0]
            for i, j in enumerate([1, 2, 3, 4, 5, 8, 9, 10, 6, 7]):
                y = data.values[:, j]
                # y[y == 0] = np.nan  # don't show zero values
                ax[i].plot(x, y, marker='.', label=f.stem, linewidth=2, markersize=8)
                ax[i].set_title(s[j], fontsize=12)
                # if j in [8, 9, 10]:  # share train and val loss y axes
                #     ax[i].get_shared_y_axes().join(ax[i], ax[i - 5])
        except Exception as e:
            print(f'Warning: Plotting error for {f}: {e}')
    ax[1].legend()
    fig.savefig(save_dir / 'results.png', dpi=200)
    plt.close()

def profile_idetection(start=0, stop=0, labels=(), save_dir=''):
    # Plot iDetection '*.txt' per-image logs. from utils.plots import *; profile_idetection()
    ax = plt.subplots(2, 4, figsize=(12, 6), tight_layout=True)[1].ravel()
    s = ['Images', 'Free Storage (GB)', 'RAM Usage (GB)', 'Battery', 'dt_raw (ms)', 'dt_smooth (ms)', 'real-world FPS']
    files = list(Path(save_dir).glob('frames*.txt'))
    for fi, f in enumerate(files):
        try:
            results = np.loadtxt(f, ndmin=2).T[:, 90:-30]  # clip first and last rows
            n = results.shape[1]  # number of rows
            x = np.arange(start, min(stop, n) if stop else n)
            results = results[:, x]
            t = (results[0] - results[0].min())  # set t0=0s
            results[0] = x
            for i, a in enumerate(ax):
                if i < len(results):
                    label = labels[fi] if len(labels) else f.stem.replace('frames_', '')
                    a.plot(t, results[i], marker='.', label=label, linewidth=1, markersize=5)
                    a.set_title(s[i])
                    a.set_xlabel('time (s)')
                    # if fi == len(files) - 1:
                    #     a.set_ylim(bottom=0)
                    for side in ['top', 'right']:
                        a.spines[side].set_visible(False)
                else:
                    a.remove()
        except Exception as e:
            print(f'Warning: Plotting error for {f}; {e}')
    ax[1].legend()
    plt.savefig(Path(save_dir) / 'idetection_profile.png', dpi=200)

def save_one_box(xyxy, im, file='image.jpg', gain=1.02, pad=10, square=False, BGR=False, save=True):
    # Save image crop as {file} with crop size multiple {gain} and {pad} pixels. Save and/or return crop
    xyxy = torch.tensor(xyxy).view(-1, 4)
    b = xyxy2xywh(xyxy)  # boxes
    if square:
        b[:, 2:] = b[:, 2:].max(1)[0].unsqueeze(1)  # attempt rectangle to square
    b[:, 2:] = b[:, 2:] * gain + pad  # box wh * gain + pad
    xyxy = xywh2xyxy(b).long()
    clip_coords(xyxy, im.shape)
    crop = im[int(xyxy[0, 1]):int(xyxy[0, 3]), int(xyxy[0, 0]):int(xyxy[0, 2]), ::(1 if BGR else -1)]
    if save:
        file.parent.mkdir(parents=True, exist_ok=True)  # make directory
        cv2.imwrite(str(increment_path(file).with_suffix('.jpg')), crop)
    return crop


#!/usr/bin/python3
"""Advanced API.

***This is an advanced task***
For this task, we'll recursively call the Reddit API,
parse the titles of all hot articles and return a sorted
count of given keywords(case-sensitive, delimited by
spaces).

Note:
    1. No iteration!
    2. If `word_list` contains duplicates, then the sum
    of each duplicate should be returned.
    3. Considering "java" as the keyword passed:
        a. "java java java" counts as 3 separate instances
        of "java"
        b. "java!", "java_" or "java*" shouldn't count as
        "java"
    4. If posts don't match or subreddit doesn't exist,
    print nothing.

"""
import requests

def count_words(subreddit, word_list=[], word_count={}, after=None):
    """Return the keyword count.

    Args:
        subreddit ('obj':'str'): The subreddit
        word_list ('obj':'list'): The keyword list
        word_count ('obj':'dict'): The count dictionary
        after ('obj':'None'): The place holder

    Returns:
        word_list; nothing, otherwise.

    """
    url = 'https://www.reddit.com/r/{}/hot.json'.format(subreddit)
    param = {"after": after}
    header = {"User-Agent": "My-User-Agent"}
    data = requests.get(
            url,
            headers=header,
            params=param,
            allow_redirects=False
            )
    if data.status_code != 200:
        return None

    info = data.json()

    hot_list = [
            c.get("data").get("title") for c in info.get("data")
                                                    .get("children")
            ]
    if not hot_list:
        return None

    word_list = list(dict.fromkeys(word_list))

    if word_count == {}:
        word_count = {w: 0 for w in word_list}

    for title in hot_list:
        split_words = title.split(' ')
        for word in word_list:
            for s_word in split_words:
                if s_word.lower() == word.lower():
                    word_count[word] += 1

    if not info.get("data").get("after"):
        sorted_counts = sorted(word_count.items(), key=lambda kv: kv[0])
        sorted_counts = sorted(word_count.items(),
                               key=lambda kv: kv[1], reverse=True)
        [print('{}: {}'.format(k, v)) for k, v in sorted_counts if v != 0]
    else:
        return count_words(
                subreddit,
                word_list,
                word_count,
                info.get("data").get("after")
                )


from random import randrange

def column(grid: list, disc: str) -> int:
	grid_check = grid.copy()
	grid_check.pop(6)

	if grid[6] == [5, 5, 5, 5, 5, 5, 5, 5, 5]:
		print("DEBUG: TABLERO VACIO")
		return randrange(0, 9, 1)
	else:
		y_pos = 0
		for y in grid_check:
			x_pos = 0
			for x in y:
				if x != "_":
					if x != disc:
						if x_pos + 3 < grid[6][x_pos]:
							if grid[y_pos][x_pos + 3] == "_" or grid[y_pos][x_pos - 3] == "_":
								return x_pos + 3
						elif x_pos + 2 < grid[6][x_pos]:
							if grid[y_pos][x_pos + 2] == "_" or grid[y_pos][x_pos - 2] == "_":
								return x_pos + 2
						elif x_pos + 1 < grid[6][x_pos]:
							if grid[y_pos][x_pos + 1] == "_" or grid[y_pos][x_pos - 1] == "_":
								return x_pos + 1
							else:
								return randrange(0, 8, 1)
					else:
						if (x_pos == 8):
							if grid[6][x_pos] >= 0:
								return x_pos - 1
							else:
								return randrange(0, 8, 1)
						else:
							if grid[6][x_pos] >= 0:
								return x_pos + 1
							else:
								return randrange(0, 8, 1)
				x_pos = x_pos + 1
			y_pos = y_pos + 1
		return randrange(0, 8, 1)


##########################################
# @subject : Person segmentation         #
# @author  : perryxin                    #
# @date    : 2018.12.27                  #
##########################################
import torch.utils.data as Data
from read_data import *
from config import *
from models.unet_plusplus import *
from models.linknet import *

model_name = 'linknet'  # 'unet++'
if model_name == 'unet++':
    net = Unet_2D(3, 1, 'test')
    checkpoint = torch.load("../models/weights/unet++_11.pth", map_location='cpu')
    dataset_test = MyData(istrain="test", size=256)
else:  # 'linknet'
    net = LinkNet()
    checkpoint = torch.load("../models/weights/link_640_57.pth", map_location='cpu')
    dataset_test = MyData(istrain="test", size=640)
net.load_state_dict(checkpoint['net'])
loader_test = Data.DataLoader(dataset_test, batch_size=conf.BATCH_SIZE_TEST, shuffle=False)

net.cuda()
print("test_images", len(dataset_test))
print("start testing...")

# val###################
net.eval()
test_iou = 0
t1 = time.time()
for i, (img, label) in enumerate(loader_test):

    output = net(img.float()).cuda()
    output[output >= 0.5] = 1.
    output[output != 1] = 0.
    iou_ = iou(output.cpu(), label)
    test_iou += iou_
    print("img_%d: iou=%.4f" % (i, iou_))
    #############show
    isShow = True
    if isShow:
        import matplotlib.pyplot as plt

        plt.subplot(221)
        output = output[0, 0].detach().numpy()  # .detach().cpu()
        label = label[0, 0].numpy()
        img = (img[0].permute(1, 2, 0).detach().numpy() * conf.std + conf.mean)  # .detach().cpu()
        img = img * 255
        plt.imshow(np.uint8(img))
        plt.title("origin")
        plt.axis('off')
        plt.xticks([])
        plt.yticks([])
        plt.subplot(222)
        plt.imshow(label)
        plt.title("label")
        plt.axis('off')
        plt.xticks([])
        plt.yticks([])
        plt.subplot(223)
        mm = apply_mask(img, output, color=random_colors(1)[0])
        # mm=cv2.addWeighted(np.uint8(img),0.8,np.uint8(output*255),0.2,0)
        plt.imshow(np.uint8(mm))
        plt.title("origin+seg")
        plt.axis('off')
        plt.xticks([])
        plt.yticks([])
        plt.subplot(224)
        plt.imshow(output)
        plt.title("seg")

        plt.axis('off')
        plt.xticks([])
        plt.yticks([])
        # plt.savefig("./results/imgs/img_%d.png"%i)
        plt.show()

    del img, label
test_iou /= len(loader_test)
t2 = time.time()
print("speed: %.4f fps, test_iou : %.4f" % (len(dataset_test) / (t2 - t1), test_iou))


import geopandas as gp
import shapely

def intersection(left, right, grid_size=0):
    """Intersect the geometries from the left with the right.

    New, intersected geometries are stored in "geometry_right".

    Uses spatial index operations for faster operations.  Wholly contained
    geometries from right are copied intact, only those that intersect but are
    not wholly contained are intersected.

    Parameters
    ----------
    left : GeoDataFrame
    right : GeoDataFrame
    grid_size : int, optional (default: None)
        if present, geometries and results from intersection will be
        snapped to this precision grid

    Returns
    -------
    DataFrame or None
        output geometries are in "geometry_right"
        None if there are no intersections
    """

    tree = shapely.STRtree(right.geometry.values)
    ix = tree.query(left.geometry.values, predicate="intersects")

    if len(ix[0]) == 0:
        return None

    # copy original geometries; they will be clipped below if needed
    intersects = gp.GeoDataFrame(
        {
            "geometry": left.geometry.values.take(ix[0]),
            "index_right": right.index.values.take(ix[1]),
            "geometry_right": right.geometry.values.take(ix[1]),
        },
        index=left.index.take(ix[0]),
        crs=left.crs,
    )

    shapely.prepare(intersects.geometry.values)
    contains = shapely.contains_properly(
        intersects.geometry.values, intersects.geometry_right.values
    )

    # clip any that are not fully contained
    tmp = intersects[~contains]
    intersects.loc[~contains, "geometry_right"] = shapely.intersection(
        tmp.geometry.values, tmp.geometry_right.values, grid_size=grid_size
    )

    return left.join(intersects.drop(columns=["geometry"]), how="inner").join(
        right.drop(columns=["geometry"]), on="index_right"
    )


import os
from flask import jsonify, request
import requests

def getWeather():
    try:
        API_KEY = os.getenv("API_KEY")
        args = request.args
        lon = str(args.get('lon'))
        lat = str(args.get('lat'))

        url = f'https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API_KEY}'
        response = requests.get(url).json()
        return jsonify(response),200

    except Exception as e:
      raise Exception("Something bad happened")


import gensim
from sklearn import svm
from sklearn import datasets
from sklearn.feature_extraction.text import TfidfVectorizer
from joblib import dump, load
import flask
from flask_restful import Resource, Api, reqparse
from flask import request
from functools import partial
from flask import request, jsonify

app = flask.Flask(__name__)
app.config["DEBUG"] = True

def k_means_sort(e, product_terms, query):
	#global product_terms, query
	occurences = 0
	for word in [x.lower() for x in e.split(',')[1].split('-')]:
		if word in product_terms or word in query.split(' '):
			occurences += 1
	occurences /= len(e.split(',')[1].split('-'))
	average_similarity = 0
	counter = 0
	for word in [x.lower() for x in e.split(',')[1].split('-')]:
		for query_word in query.split(' '):
			if query_word in word2vec_model.wv.vocab and word in word2vec_model.wv.vocab:
				average_similarity += word2vec_model.similarity(query_word, word)
				counter += 1
	average_similarity /= (counter if counter != 0 else 1)
	return occurences + average_similarity

word2vec_model = gensim.models.Word2Vec.load('../MachineLearningModels/Word2Vec/word2vec.model')
vectorizing_model = load('../MachineLearningModels/KMeansClustering/vectorizing_model.sav')
clustering_model = load('../MachineLearningModels/KMeansClustering/clustering_model.sav')
products = open('../formatted_data/all_products.txt').read().split('\n')[:-1]

@app.route('/', methods=['GET'])
def home():
	parser = request.args
	#parser.add_argument('query', type=str)
	query = parser['query']
	prediction = clustering_model.predict(vectorizing_model.transform([query]))[0]
	product_terms = [vectorizing_model.get_feature_names()[product_term_index] for product_term_index in clustering_model.cluster_centers_.argsort()[:, ::-1][prediction, :30]]
	relavant_products = products.copy()
	relavant_products.sort(key=partial(k_means_sort, product_terms=product_terms, query=query), reverse=True)
	return jsonify(relavant_products[:10])
	#return "Distant Reading Archive: This site is a prototype API for distant reading of science fiction novels."

app.run()


import cv2
import io
import os

from google.cloud import vision

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = ".." #put your google API credintials here

# Instantiates a client
client = vision.ImageAnnotatorClient()

#folder = "/uploads/"
class Image():
    def __init__(self):
        self.labels = []
        self.image = None

    def capture(self):
        # chi 7aja
        cam = cv2.VideoCapture(0)
        cv2.namedWindow("Camera")
        img_counter = 0
        while True:
            ret, frame = cam.read()
            if not ret:
                print("failed to grab frame")
                break
            cv2.imshow("test", frame)

            k = cv2.waitKey(1)
            if k % 256 == 27:
                # ESC pressed
                print("Escape hit, closing...")
                break
            elif k % 256 == 32:
                # SPACE pressed
                img_name = "opencv_frame_{}.png".format(img_counter)
                cv2.imwrite(img_name, frame)
                print("{} written!".format(img_name))
                img_counter += 1
        cam.release()
        cv2.destroyAllWindows()
        self.image = img_name

    def capture_bis(self):
        # chi 7aja

        # initialize the camera
        cam = cv2.VideoCapture(0)  # 0 -> index of camera
        s, img = cam.read()
        img_counter = 0
        if s:  # frame captured without any errors
            #namedWindow("cam-test", CV_WINDOW_AUTOSIZE)
            #imshow("cam-test", img)
            cv2.waitKey(0)
            #destroyWindow("cam-test")
            img_name = "opencv_frame_{}.png".format(img_counter)
            cv2.imwrite(img_name, img)  # save image
        self.image = img_name

    def findLabels(self):
        # The name of the image file to annotate
        file_name = self.image

        # Loads the image into memory
        with io.open(file_name, 'rb') as image_file:
            content = image_file.read()

        image = vision.Image(content=content)

        # Performs label detection on the image file
        response = client.label_detection(image=image)
        labels = response.label_annotations

        #self.labels = labels
        print('Labels:')
        for label in labels:
            print(label.description+', score: '+str(label.score))
            self.labels.append(label)


#!/bin/python

import argparse
import numpy as np
import pandas as pd
import time
import json
from datetime import datetime, timedelta
from confluent_kafka.avro import AvroProducer

def parse_args():
    parser = argparse.ArgumentParser(description='Publish CSV records to a Kafka topic.')
    parser.add_argument('--delta-minutes', '-d', default=2, type=int, help='Minutes to shift timestamps')
    parser.add_argument('--input-file', '-i', default='../resources/ml-latest-small/ratings.csv',
                        help='Path to the CSV input file')
    parser.add_argument('--output-topic', '-o', default='user-ratings-4', help='Output Kafka topic')
    return parser.parse_args()

def transform_timestamps(df, start_time_adjusted, end_time_adjusted):
    start_time_adjusted_ts = int(start_time_adjusted.timestamp() * 1000)
    end_time_adjusted_ts = int(end_time_adjusted.timestamp() * 1000)

    # Generate random timestamps within the time range
    random_timestamps = np.random.randint(start_time_adjusted_ts, end_time_adjusted_ts, df.shape[0])

    # Sort the random timestamps
    sorted_timestamps = np.sort(random_timestamps)

    # Assign sorted random timestamps to the DataFrame
    df['timestamp'] = sorted_timestamps

    return df

def setup_producer():
    # Avro schema
    # key schema not used due to streams dependency confusing key and value schemas
    key_schema_str = json.dumps({
        "namespace": "ratings",
        "type": "record",
        "name": "UserID",
        "fields": [
            {"name": "userId", "type": "int"}
        ]
    })
    value_schema_str = json.dumps({
        "namespace": "ratings",
        "type": "record",
        "name": "Rating",
        "fields": [
            {"name": "userId", "type": "int"},
            {"name": "movieId", "type": "int"},
            {"name": "rating", "type": "float"},
            {
                "name": "timestamp",
                "type": {
                    "type": "long",
                    "logicalType": "timestamp-millis"
                }
            }
        ]
    })

    # Producer configuration
    producer = AvroProducer({
        'bootstrap.servers': 'localhost:9092',
        'schema.registry.url': 'http://localhost:8081'
    }, default_value_schema=value_schema_str)
    # }, default_value_schema=value_schema_str, default_key_schema=key_schema_str)

    return producer

def send_batch(producer, batch, output_topic):
    for value in batch:
        producer.produce(
            topic=output_topic,
            # key=dict(userId=value['userId']),
            value=value
        )

def manage_loop(df, producer, output_topic):
    total_count = df.shape[0]
    record_count = 0
    while not df.empty:
        current_utc_ts = int(datetime.utcnow().timestamp() * 1000)  # Convert this to UNIX milliseconds as well
        past_rows = df[df['timestamp'] <= current_utc_ts]

        if not past_rows.empty:
            batch = past_rows.to_dict('records')
            send_batch(producer, batch, output_topic)

            batch_count = len(batch)
            record_count += batch_count

            current_time_str = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
            print(f"{current_time_str} - {batch_count} rows sent ({record_count}/{total_count})")

            df = df[df['timestamp'] > current_utc_ts]
            producer.flush()

        time.sleep(1)

def main():
    args = parse_args()

    df = pd.read_csv(args.input_file)
    start_time_adjusted = datetime.utcnow()
    end_time_adjusted = start_time_adjusted + timedelta(minutes=args.delta_minutes)

    df = transform_timestamps(df, start_time_adjusted, end_time_adjusted)
    producer = setup_producer()

    print(f"Started publishing records at {start_time_adjusted}")
    manage_loop(df, producer, args.output_topic)
    print(f"Finished publishing records at {datetime.utcnow()}")

if __name__ == "__main__":
    main()


import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import module_utils
import simulation_utils as sim_utils
import recorder as rec
import copy

def eligibilityNavigation(symbols, start, goal, distance, nscales = 1,
                          ms_per_frame = 1, f = 3, min_base_delay = 8, variance=2,
                          record = True, verbose = True, save_data = False, color_rule = 'advanced_tag', gif_name = "test",
                          output_filepath = "output"):

    valid_transitions = module_utils.findAllTransitions(symbols,distance, nscales, compressed = False) # Get a list containing, for each symbol, all their valid transitions
    event_series = {}
    recorder = rec.Recorder()
    if save_data:
        symbol_data = dict()
        symbol_data[0] = copy.deepcopy(symbols)
        inhibit_data = np.empty((0,2))

    inhibit_until, will_inhibit = np.zeros(nscales), np.zeros(nscales)

    scale_time_penalty = np.linspace(0,1, nscales)
    path_found = False
    refraction_time = 20

    start_time,  current_time = 0, 0
    first_run_time = None

    # Reset symbols
    for symbol in symbols:
        symbol.reset(min_base_delay, compressed = False)

    symbols[goal].tag[:] = True
    symbols[goal].spike_delay_ms = f

    for frame in range(0,2000, ms_per_frame):
        sim_utils.scheduleFrame(event_series, current_time + frame)

    # Activate start to initiate wave propagation:
    for i in range(nscales-1, -1, -1):
        sim_utils.scheduleSynapseEvent(event_series, current_time, np.array([start]), scale = i)

    while not path_found: # Keeps running until time reduction is sufficiently big

        # Verify that the schedule should still run, and update time accordingly
        if(len(event_series) == 0 or current_time > 1000):
            if record:
                recorder.createAnimation()
            #raise Exception("No further events or ran too long. Check gif to see what happened")
            return False
        else:
            current_time = min(event_series)
            print(current_time)

        global_inhibit = inhibit_until > current_time

        # Activity
        for spike_info in event_series[current_time].spike_events:
            symbol = symbols[spike_info[0]]
            scale = spike_info[1]

            # Only proceed if the symbol isn't inhibited or in refractory
            if global_inhibit[scale] or (symbol.activated_at[scale] is not None and symbol.activated_at[scale] > current_time - (refraction_time + 8)):
                continue

            # Set trace values
            symbol.activated_at[scale:nscales] = current_time
            symbol.inhibit_window[scale] = [current_time + 3 + f, current_time + 3 + min_base_delay] + scale_time_penalty[scale]
            symbol.feedback_window[scale] = [current_time + 6 + f, current_time + 6 + min_base_delay] + 2*scale_time_penalty[scale]
            symbol.inhibit_trace[scale] = False

            # Activate next layer:
            sim_utils.scheduleSynapseEvent(event_series, current_time + 3 + scale_time_penalty[scale], valid_transitions[spike_info[0], scale].transition_ids, scale)

            # Add tag and inhibit: (This should happen after activation, so speed up only happens next circuit)
            if symbol.tag[scale]:
                will_inhibit[scale:nscales] = True

            # Check for goal funkiness:
            if spike_info[0] == goal:

                inhibit_until[scale] = current_time + refraction_time + 8

                if scale != 0:
                    continue
                # If this is the first iteration, store the time it took:
                if first_run_time is None:
                    first_run_time = current_time

                # Find the time it took and check if the speed increase is sufficient:
                final_time = current_time - start_time
                if verbose:
                    print("Goal found in", final_time)
                    print(final_time / first_run_time)

                if save_data:
                    symbol_data[current_time] = copy.deepcopy(symbols)

                if final_time < first_run_time * 0.8:
                    i += 1
                    if verbose:
                        print("Possible path found")
                else:
                    i = 0

                if i >= 3:
                    path_found = True
                    if verbose:
                        print("Success, path successfully found!")
                        print("Time reduced by factor", final_time / first_run_time)

                # Initiate next iteration after a long inhibition to reset symbols:
                start_time = current_time + refraction_time + 8
                for iscale in range(nscales-1, -1, -1):
                    sim_utils.scheduleSynapseEvent(event_series, start_time, np.array([start]), iscale)

        # Receive input
        for input_info in event_series[current_time].synapse_events:

            input_symbol = symbols[input_info[0]]
            scale = input_info[1]

            if not input_symbol.tag[scale] and np.random.rand() > 2**(scale/2):
                continue

            # Check if symbol should receive tag:
            if input_symbol.inhibit_trace[scale] and input_symbol.feedback_window[scale, 0] <= current_time <= input_symbol.feedback_window[scale, 1]:
                input_symbol.tag[scale] = True
                input_symbol.spike_delay_ms = f + (input_symbol.spike_delay_ms-f)*(1-(0.5 + (np.random.rand()-0.5)*0.2))#/(np.sqrt(scale+1))) # Speed up

            if input_symbol.tag[scale]: # Recover from past speed up
                recovery = 0 #if input_symbol.activated_at is None else (input_symbol.original_spike_delay_ms - input_symbol.spike_delay_ms)*(1-np.exp(-(current_time - input_symbol.activated_at)/200))
                input_symbol.spike_delay_ms = input_symbol.spike_delay_ms + recovery
            sim_utils.scheduleSpikeEvent(event_series, current_time + input_symbol.spike_delay_ms, input_info[0], scale)

        # Set inhibition duriation
        for iscale, inhib in enumerate(will_inhibit):
            if inhib:
                inhibit_until[iscale] = max(current_time + 3 + f, inhibit_until[iscale])
                if save_data:
                    inhibit_data = np.vstack((inhibit_data, [current_time, inhibit_until]))
                for symbol in symbols:
                    if symbol.inhibit_window[iscale][0] <= current_time <= symbol.inhibit_window[iscale][1]:
                        symbol.inhibit_trace[iscale] = True
                will_inhibit[iscale] = False

        # Record the current state if desired
        if event_series[current_time].catch_frame:
            sim_utils.catchFrame(symbols, current_time, start, goal, recorder, global_inhibit, color_rule)

        # Delete the entries at current time, to let the flow of time be in a strictly forward direction
        del event_series[current_time]

    event_series.clear()
    for _ in range(40):
        current_time += 1
        sim_utils.catchFrame(symbols, current_time, start, goal, recorder, color_rule)

    if verbose:
        print ("From ", symbols[start].coord, "to ", symbols[goal].coord)
        print("Total time: ", current_time)
    if record:
        recorder.createAnimation(gif_name)
    if save_data:
        np.savez_compressed(f"navigation/result_data/{output_filepath}", \
                            symbols = symbol_data, \
                            start = start, \
                            goal = goal, \
                            dist = distance, \
                            inhibit_ranges = inhibit_data
                            )
    return True

#For random symbol, start and goal simulation:
variance = 1
min_base_delay = 5
valid_goal = False
min_dist = 10
n_scales = 7
model_num = 11
symbols = module_utils.generateRandomSymbols(400, min_base_delay, [0,10], [0,10], variance, n_scales, False)
while not valid_goal:
    [start, goal] = np.random.choice(len(symbols), 2, False)
    valid_goal = np.linalg.norm(symbols[start].coord - symbols[goal].coord) > min_dist
eligibilityNavigation(symbols, start, goal, 1, n_scales, 1, 2, min_base_delay, variance, color_rule= 'simple_scale', save_data= False, output_filepath="400_1ms_2")


import time
from typing import List, Tuple, Iterable

from volatility3.framework import constants, interfaces, layers, symbols
from volatility3.framework.configuration import requirements
from volatility3.framework.interfaces import plugins
from volatility3.framework.renderers import TreeGrid
from volatility3.framework.symbols import intermed
from volatility3.framework.symbols.windows import extensions

class Info(plugins.PluginInterface):
    """Show OS & kernel details of the memory sample being analyzed."""

    _required_framework_version = (2, 0, 0)
    _version = (1, 0, 0)

    @classmethod
    def get_requirements(cls) -> List[interfaces.configuration.RequirementInterface]:
        return [
            requirements.ModuleRequirement(
                name="kernel",
                description="Windows kernel",
                architectures=["Intel32", "Intel64"],
            ),
        ]

    @classmethod
    def get_depends(
        cls,
        context: interfaces.context.ContextInterface,
        layer_name: str,
        index: int = 0,
    ) -> Iterable[Tuple[int, interfaces.layers.DataLayerInterface]]:
        """List the dependencies of a given layer.

        Args:
            context: The context to retrieve required layers from
            layer_name: the name of the starting layer
            index: the index/order of the layer

        Returns:
            An iterable containing the levels and layer objects for all dependent layers
        """
        layer = context.layers[layer_name]
        yield index, layer
        try:
            for depends in layer.dependencies:
                for j, dep in cls.get_depends(context, depends, index + 1):
                    yield j, context.layers[dep.name]
        except AttributeError:
            # FileLayer won't have dependencies
            pass

    @classmethod
    def get_kernel_module(
        cls,
        context: interfaces.context.ContextInterface,
        layer_name: str,
        symbol_table: str,
    ):
        """Returns the kernel module based on the layer and symbol_table"""
        virtual_layer = context.layers[layer_name]
        if not isinstance(virtual_layer, layers.intel.Intel):
            raise TypeError("Virtual Layer is not an intel layer")

        kvo = virtual_layer.config["kernel_virtual_offset"]

        ntkrnlmp = context.module(symbol_table, layer_name=layer_name, offset=kvo)
        return ntkrnlmp

    @classmethod
    def get_kdbg_structure(
        cls,
        context: interfaces.context.ContextInterface,
        config_path: str,
        layer_name: str,
        symbol_table: str,
    ) -> interfaces.objects.ObjectInterface:
        """Returns the KDDEBUGGER_DATA64 structure for a kernel"""
        ntkrnlmp = cls.get_kernel_module(context, layer_name, symbol_table)

        native_types = context.symbol_space[symbol_table].natives

        kdbg_offset = ntkrnlmp.get_symbol("KdDebuggerDataBlock").address

        kdbg_table_name = intermed.IntermediateSymbolTable.create(
            context,
            interfaces.configuration.path_join(config_path, "kdbg"),
            "windows",
            "kdbg",
            native_types=native_types,
            class_types=extensions.kdbg.class_types,
        )

        kdbg = context.object(
            kdbg_table_name + constants.BANG + "_KDDEBUGGER_DATA64",
            offset=ntkrnlmp.offset + kdbg_offset,
            layer_name=layer_name,
        )

        return kdbg

    @classmethod
    def get_kuser_structure(
        cls,
        context: interfaces.context.ContextInterface,
        layer_name: str,
        symbol_table: str,
    ) -> interfaces.objects.ObjectInterface:
        """Returns the _KUSER_SHARED_DATA structure for a kernel"""
        virtual_layer = context.layers[layer_name]
        if not isinstance(virtual_layer, layers.intel.Intel):
            raise TypeError("Virtual Layer is not an intel layer")

        ntkrnlmp = cls.get_kernel_module(context, layer_name, symbol_table)

        # this is a hard-coded address in the Windows OS
        if virtual_layer.bits_per_register == 32:
            kuser_addr = 0xFFDF0000
        else:
            kuser_addr = 0xFFFFF78000000000

        kuser = ntkrnlmp.object(
            object_type="_KUSER_SHARED_DATA",
            layer_name=layer_name,
            offset=kuser_addr,
            absolute=True,
        )

        return kuser

    @classmethod
    def get_version_structure(
        cls,
        context: interfaces.context.ContextInterface,
        layer_name: str,
        symbol_table: str,
    ) -> interfaces.objects.ObjectInterface:
        """Returns the KdVersionBlock information from a kernel"""
        ntkrnlmp = cls.get_kernel_module(context, layer_name, symbol_table)

        vers_offset = ntkrnlmp.get_symbol("KdVersionBlock").address

        vers = ntkrnlmp.object(
            object_type="_DBGKD_GET_VERSION64",
            layer_name=layer_name,
            offset=vers_offset,
        )

        return vers

    @classmethod
    def get_ntheader_structure(
        cls,
        context: interfaces.context.ContextInterface,
        config_path: str,
        layer_name: str,
    ) -> interfaces.objects.ObjectInterface:
        """Gets the ntheader structure for the kernel of the specified layer"""
        virtual_layer = context.layers[layer_name]
        if not isinstance(virtual_layer, layers.intel.Intel):
            raise TypeError("Virtual Layer is not an intel layer")

        kvo = virtual_layer.config["kernel_virtual_offset"]

        pe_table_name = intermed.IntermediateSymbolTable.create(
            context,
            interfaces.configuration.path_join(config_path, "pe"),
            "windows",
            "pe",
            class_types=extensions.pe.class_types,
        )

        dos_header = context.object(
            pe_table_name + constants.BANG + "_IMAGE_DOS_HEADER",
            offset=kvo,
            layer_name=layer_name,
        )

        nt_header = dos_header.get_nt_header()

        return nt_header

    def _generator(self):
        kernel = self.context.modules[self.config["kernel"]]

        layer_name = kernel.layer_name
        symbol_table = kernel.symbol_table_name
        layer = self.context.layers[layer_name]
        table = self.context.symbol_space[symbol_table]

        kdbg = self.get_kdbg_structure(
            self.context, self.config_path, layer_name, symbol_table
        )

        yield (0, ("Kernel Base", hex(layer.config["kernel_virtual_offset"])))
        yield (0, ("DTB", hex(layer.config["page_map_offset"])))
        yield (0, ("Symbols", table.config["isf_url"]))
        yield (
            0,
            ("Is64Bit", str(symbols.symbol_table_is_64bit(self.context, symbol_table))),
        )
        yield (
            0,
            ("IsPAE", str(self.context.layers[layer_name].metadata.get("pae", False))),
        )

        for i, layer in self.get_depends(self.context, layer_name):
            yield (0, (layer.name, f"{i} {layer.__class__.__name__}"))

        if kdbg.Header.OwnerTag == 0x4742444B:
            yield (0, ("KdDebuggerDataBlock", hex(kdbg.vol.offset)))
            yield (0, ("NTBuildLab", kdbg.get_build_lab()))
            yield (0, ("CSDVersion", str(kdbg.get_csdversion())))

        vers = self.get_version_structure(self.context, layer_name, symbol_table)

        yield (0, ("KdVersionBlock", hex(vers.vol.offset)))
        yield (0, ("Major/Minor", f"{vers.MajorVersion}.{vers.MinorVersion}"))
        yield (0, ("MachineType", str(vers.MachineType)))

        ntkrnlmp = self.get_kernel_module(self.context, layer_name, symbol_table)

        cpu_count_offset = ntkrnlmp.get_symbol("KeNumberProcessors").address

        cpu_count = ntkrnlmp.object(
            object_type="unsigned int", layer_name=layer_name, offset=cpu_count_offset
        )

        yield (0, ("KeNumberProcessors", str(cpu_count)))

        kuser = self.get_kuser_structure(self.context, layer_name, symbol_table)

        yield (0, ("SystemTime", str(kuser.SystemTime.get_time())))
        yield (
            0,
            (
                "NtSystemRoot",
                str(
                    kuser.NtSystemRoot.cast(
                        "string", encoding="utf-16", errors="replace", max_length=260
                    )
                ),
            ),
        )
        yield (0, ("NtProductType", str(kuser.NtProductType.description)))
        yield (0, ("NtMajorVersion", str(kuser.NtMajorVersion)))
        yield (0, ("NtMinorVersion", str(kuser.NtMinorVersion)))
        # yield (0, ("KdDebuggerEnabled", "True" if kuser.KdDebuggerEnabled else "False"))
        # yield (0, ("SafeBootMode", "True" if kuser.SafeBootMode else "False"))

        nt_header = self.get_ntheader_structure(
            self.context, self.config_path, layer_name
        )

        yield (
            0,
            (
                "PE MajorOperatingSystemVersion",
                str(nt_header.OptionalHeader.MajorOperatingSystemVersion),
            ),
        )
        yield (
            0,
            (
                "PE MinorOperatingSystemVersion",
                str(nt_header.OptionalHeader.MinorOperatingSystemVersion),
            ),
        )

        yield (0, ("PE Machine", str(nt_header.FileHeader.Machine)))
        yield (
            0,
            (
                "PE TimeDateStamp",
                time.asctime(time.gmtime(nt_header.FileHeader.TimeDateStamp)),
            ),
        )

    def run(self):
        return TreeGrid([("Variable", str), ("Value", str)], self._generator())


import pdb, argparse
import numpy as np
import matplotlib.pyplot as plt

import torch
import torch.nn as nn
import torch.optim as optim
import torchvision as thv
from torchvision import transforms

from torch.utils.data import Dataset, DataLoader
from CarlaDataset import CarlaDataset
from CVAE import CVAE
from siameseCVAE import siameseCVAE

def main():
    model = siameseCVAE()
    criterion = nn.MSELoss()
    optimizer = optim.Adam(model.parameters(), lr=0.001, betas=(0.9, 0.999),
                            eps=1e-08, weight_decay=0)
    epochs = 10
    dl = DataLoader(CarlaDataset('../data/'))
    for epoch in range(epochs):
        for i, X in enumerate(dl):
            img = X[0]
            img_ = X[1]
            ctrl_inputs = X[2]

            img = (img/255).float()
            img_ = (img_/255).float()
            img1 = img[:,0,:,:,:]
            img2 = img[:,1,:,:,:]
            img1_ = img_[:,0,:,:,:]
            img2_ = img_[:,1,:,:,:]

            xhat, yhat, z, z_mean, z_stdev = model.forward(img1,img2,ctrl_inputs)
            lossx = criterion(xhat,img1_)
            lossy = criterion(yhat,img2_)
            optimizer.zero_grad()
            lossx.backward()
            lossy.backward()

            if (i+1) % 10 == 0:
              print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, epochs, i+1, total_step, loss.item()))

            pdb.set_trace()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--batch_size', type=int, default=32)
    # parser.add_argument('--lr', type=int, default=0.005)

    args = parser.parse_args()

    main()


"""
@function: choose the attacked flights
@author: Tengyao Li
@date: 2018/09/12
@status: developing
"""

import numpy as np

class attacked_flight:
    """
    @function: get the attacked icao set
    @author: Tengyao Li
    @date: 2018/09/12
    """

    def __init__(self, origin_df):
        """
        @function: initialize the origin dataset
        @author: Tengyao Li
        @date: 2018/09/12
        """
        self.origin_df = origin_df

    def choose_attack_flight(self, rule='random', *args, **params):
        """
        @function: choose the attack_pattern destination
        @author: Tengyao Li
        @date: 2018/09/12

        @:param rule: the rule to select attack_pattern targets
        @:param args: the changeable parameters
        @:param params: the key parameters as dict type
        :return: the attack_pattern icao set (string list)
        """

        if rule == 'random':
            """
            @illustration: choose the attack_pattern target randomly
            @args:
            # 1st(percentage): float / the percentage of attack_pattern targets,
            control the attack_pattern flight amount in the whole dataset
            """

            percentage = args[0]
            icaoset = self.origin_df.icao24.unique()
            amount = int(np.floor(percentage * icaoset.shape[0]))
            rand = np.random.random_integers(0, icaoset.shape[0], size=(amount, 1))

            attacked_icao = []  # Issue: the redundancy of icao index ( should be improved later)
            for i in rand:
                attacked_icao.append(icaoset[i])

            return attacked_icao

        elif rule == 'area':
            """
            @illustration: choose the attack_pattern target from specific areas
            @args:
            # timestamp: the timestamp of choice
            @params:
            # start_lat: the beginning latitude
            # start_lon: the beginning longitude
            # end_lat: the end boundary of latitude
            # end_lon: the end boundary of longitude
            """
            cur_df = self.origin_df.loc[self.origin_df.time == args[0]]

            attacked_icao = []

            for icao in cur_df.icao24:
                cur_record = cur_df.loc[cur_df.icao24 == icao]
                if cur_record.lat.iloc[0] >= params['start_lat'] \
                        and cur_record.lat.iloc[0] <= params['end_lat'] \
                        and cur_record.lon.iloc[0] >= params['start_lat'] \
                        and cur_record.lon.iloc[0] <= params['end_lon']:
                    attacked_icao.append(icao)

            return attacked_icao

        elif rule == 'list':
            """
            @illustration: choose the attack_pattern target for specific targets list
            @args:
            # the icao24 attributes of target flights
            """
            attacked_icao = []
            for icao in args:
                attacked_icao.append(icao)

            return attacked_icao
        else:
            pass


from argparse import ArgumentParser, HelpFormatter
import itertools
import sys
from pathlib import Path

# ============================================================================================================
# === DTN ARGUMENT PARSER CLASSES
# ============================================================================================================

class DtnArgumentParser(ArgumentParser):
    ''' Subclass of Python's native ArgumentParser that incorporates a function to display warning messages related
        to the arguments.
    '''

    def __init__(self, *args, **kwargs):
        ''' Constructor simply calls ArgumentParser constructor '''
        super().__init__(*args, **kwargs)

    def warning(self, message):
        ''' Display a warning message related to one of the arguments. The format is consistent with that of errors

            :param str message: Warning message to display
        '''
        print('{}: WARNING: {}\n'.format(self.prog, message))

    def error(self, message):
        print('{}: ERROR: {}\n'.format(self.prog, message))
        self.print_help()
        sys.exit(-1)

# ============================================================================================================
# === DTN ARGUMENT PARSE FUNCTIONS
# ============================================================================================================

def get_argument_parser():
    ''' Parse the command-line arguments for this program

        :return ArchNetArgumentParser: The argument parser
    '''
    formatter = lambda prog: HelpFormatter(prog, max_help_position=50, width=200)
    parser    = DtnArgumentParser(prog='Dtn Simulator', formatter_class=formatter, description='DTN Network Simulator')

    # Add required arguments for running in config file mode
    parser.add_argument('-cf', '--configfile', help='configuration file path',
                        type=str, default=None, nargs='?')

    # Add optional arguments
    parser.add_argument('-v', '--validate', help='run unit tests',
                        action='store_true')

    return parser

def dict_to_args_list(args):
    argnames, argvals = zip(*args.items())
    argnames = ['--' + name for name in argnames]
    return itertools.chain.from_iterable(zip(argnames, argvals))

def process_arguments(args=None):
    ''' Process the arguments of the application and run ArchNet in batch or GUI mode depending on what is specified

        :param None or dict:
    '''
    # Get argument parser
    parser = get_argument_parser()

    # If no arguments provided, use argument parser
    if not args:
        args = parser.parse_args()
    elif isinstance(args, dict):
        args = parser.parse_args(dict_to_args_list(args))
    elif args != None:
        raise RuntimeError('process_arguments: args can only be None or a dictionary')

    if not args.configfile: return args

    # Check the validity of the configuration file
    configfile = Path(args.configfile)
    if not configfile.exists():
        raise FileExistsError(f'Configuration file {configfile} does not exist')
    if configfile.suffix not in ['.yaml', '.yml']:
        raise ValueError(f'The configuration file {configfile} is not a YAML file')

    return args


from turtle import Turtle
import random

class Food(Turtle):
    def __init__(self):
        """
        default constructor that initialize the "food" turtle, it inherits the Turtle class
        """
        super().__init__()
        self.penup()
        self.shape("circle")
        self.shapesize(stretch_len=0.5, stretch_wid=0.5)    # Making the turtle size smaller than normal
        self.color("lime")
        self.speed("fastest")
        self.refresh()

    def refresh(self):
        """Makes the food appear in another random place of the screen"""
        random_x = random.randint(-280, 280)
        random_y = random.randint(-280, 280)
        self.goto(random_x, random_y)


from ase import Atoms
import numpy as np

atoms = Atoms(['O', 'H', 'H'], positions=[[0., 0., 0.119262],
                                          [0., 0.763239, -0.477047],
                                          [0., -0.763239, -0.477047]])

# Angle no pbc
assert abs(atoms.get_angle(1, 0, 2) - 104) < 1e-3

atoms.set_cell([2, 2, 2])

# Across different pbcs
atoms.set_pbc([True, False, True])
atoms.wrap()
assert abs(atoms.get_angle(1, 0, 2, mic=True) - 104) < 1e-3

# Across all True pbc
atoms.set_pbc(True)
atoms.wrap()
assert abs(atoms.get_angle(1, 0, 2, mic=True) - 104) < 1e-3

# Simple tetrahedron
tetra_pos = np.array([[0, 0, 0], [1, 0, 0], [.5, np.sqrt(3) * .5, 0],
                      [.5, np.sqrt(1/3.) * .5, np.sqrt(2/3.)]])
atoms = Atoms(['H', 'H', 'H', 'H'],
              positions=tetra_pos - np.array([.2, 0, 0]))
angle = 70.5287793655
assert abs(atoms.get_dihedral(0, 1, 2, 3) - angle) < 1e-3

atoms.set_cell([3, 3, 3])
atoms.set_pbc(True)
atoms.wrap()
assert abs(atoms.get_dihedral(0, 1, 2, 3, mic=True) - angle) < 1e-3


# -*- coding: utf-8 -*-
import itertools

from flask import url_for
from flask_mail import Message
import query_phenomizer

from scout.constants import (CASE_STATUSES, PHENOTYPE_GROUPS, COHORT_TAGS)
from scout.models.event import VERBS_MAP
from scout.server.utils import institute_and_case

STATUS_MAP = {'solved': 'bg-success', 'archived': 'bg-warning'}
SEX_MAP = {'1': 'male', '2': 'female'}
PHENOTYPE_MAP = {-9: 'missing', 0: 'missing', 1: 'unaffected', 2: 'affected'}

def cases(store, case_query):
    """Preprocess case objects."""
    case_groups = {status: [] for status in CASE_STATUSES}
    for case_obj in case_query:
        analysis_types = set(ind['analysis_type'] for ind in case_obj['individuals'])
        case_obj['analysis_types'] = list(analysis_types)
        case_obj['assignees'] = [store.user(user_email) for user_email in
                                 case_obj.get('assignees', [])]
        case_groups[case_obj['status']].append(case_obj)

    data = {
        'prio_cases': case_groups['prioritized'],
        'cases': [(status, case_groups[status]) for status in CASE_STATUSES[1:]],
        'found_cases': case_query.count(),
    }
    return data

def case(store, institute_obj, case_obj):
    """Preprocess a single case."""
    for individual in case_obj['individuals']:
        individual['sex_human'] = SEX_MAP.get(individual['sex'], 'unknown')
        individual['phenotype_human'] = PHENOTYPE_MAP.get(individual['phenotype'])

    case_obj['assignees'] = [store.user(user_email) for user_email in
                             case_obj.get('assignees', [])]
    suspects = [store.variant(variant_id) or variant_id for variant_id in
                case_obj.get('suspects', [])]
    causatives = [store.variant(variant_id) or variant_id for variant_id in
                  case_obj.get('causatives', [])]

    distinct_genes = set()
    case_obj['panel_names'] = []
    for panel_info in case_obj.get('panels', []):
        if panel_info.get('is_default'):
            panel_obj = store.panel(panel_info['panel_id'])
            distinct_genes.update([gene['hgnc_id'] for gene in panel_obj['genes']])
            full_name = "{} ({})".format(panel_obj['display_name'], panel_obj['version'])
            case_obj['panel_names'].append(full_name)
    case_obj['default_genes'] = list(distinct_genes)

    for hpo_term in itertools.chain(case_obj.get('phenotype_groups', []),
                                    case_obj.get('phenotype_terms', [])):
        hpo_term['hpo_link'] = ("http://compbio.charite.de/hpoweb/showterm?id={}"
                                .format(hpo_term['phenotype_id']))

    # other collaborators than the owner of the case
    case_obj['o_collaborators'] = [collab_id for collab_id in
                                   case_obj['collaborators'] if
                                   collab_id != case_obj['owner']]

    irrelevant_ids = ('cust000', institute_obj['_id'])
    collab_ids = [collab['_id'] for collab in store.institutes() if
                  (collab['_id'] not in irrelevant_ids) and
                  (collab['_id'] not in case_obj['collaborators'])]

    events = list(store.events(institute_obj, case=case_obj))
    for event in events:
        event['verb'] = VERBS_MAP[event['verb']]

    data = {
        'status_class': STATUS_MAP.get(case_obj['status']),
        'other_causatives': store.check_causatives(case_obj),
        'comments': store.events(institute_obj, case=case_obj, comments=True),
        'hpo_groups': PHENOTYPE_GROUPS,
        'events': events,
        'suspects': suspects,
        'causatives': causatives,
        'collaborators': collab_ids,
        'cohort_tags': COHORT_TAGS,
    }
    return data

def update_synopsis(store, institute_obj, case_obj, user_obj, new_synopsis):
    """Update synopsis."""
    # create event only if synopsis was actually changed
    if new_synopsis and case_obj['synopsis'] != new_synopsis:
        link = url_for('cases.case', institute_id=institute_obj['_id'],
                       case_name=case_obj['display_name'])
        store.update_synopsis(institute_obj, case_obj, user_obj, link,
                              content=new_synopsis)

def hpo_diseases(username, password, hpo_ids, p_value_treshold=1):
    """Return the list of HGNC symbols that match annotated HPO terms.

    Args:
        username (str): username to use for phenomizer connection
        password (str): password to use for phenomizer connection

    Returns:
        query_result: a generator of dictionaries on the form
        {
            'p_value': float,
            'disease_source': str,
            'disease_nr': int,
            'gene_symbols': list(str),
            'description': str,
            'raw_line': str
        }
    """
    # skip querying Phenomizer unless at least one HPO terms exists
    try:
        results = query_phenomizer.query(username, password, *hpo_ids)
        diseases = [result for result in results
                    if result['p_value'] <= p_value_treshold]
        return diseases
    except SystemExit:
        return None

def rerun(store, mail, current_user, institute_id, case_name, sender, recipient):
    """Request a rerun by email."""
    institute_obj, case_obj = institute_and_case(store, institute_id, case_name)
    user_obj = store.user(current_user.email)
    link = url_for('cases.case', institute_id=institute_id, case_name=case_name)
    store.request_rerun(institute_obj, case_obj, user_obj, link)

    # this should send a JSON document to the SuSy API in the future
    html = """

{institute}: {case} ({case_id})

Re-run requested by: {name}

    """.format(institute=institute_obj['display_name'],
               case=case_obj['display_name'], case_id=case_obj['_id'],
               name=user_obj['name'].encode())

    # compose and send the email message
    msg = Message(subject=("SCOUT: request RERUN for {}"
                           .format(case_obj['display_name'])),
                  html=html, sender=sender, recipients=[recipient],
                  # cc the sender of the email for confirmation
                  cc=[user_obj['email']])
    mail.send(msg)


import logging
import os

import click
import pandas as pd
import tqdm
from mol_dyn.pipeline import pipeline
from mol_dyn.utils import load_smiles_csv

@click.command()
@click.option("--smiles_csv", required=True, help="Smiles csv")
@click.option("--output_folder", required=True, help="Output folder")
@click.option("--field", type=str, default="pcff", help="Force field")
@click.option("--steps", type=int, default=500000, help="Steps")
@click.option("--cores", type=int, default=8, help="Cores")
@click.option(
    "--emc_template",
    type=str,
    default="mol_dyn/emc_template.esh",
    help="Path to the emc template file in the mol_dyn folder",
)
@click.option(
    "--lammps_template",
    type=str,
    default="mol_dyn/lammps_template.in",
    help="Path to the lammps `template file in the mol_dyn folder",
)
def main(
    smiles_csv: str,
    output_folder: str,
    field: str = "pcff",
    steps: int = 500000,
    cores: int = 8,
    emc_template: str = "mol_dyn/emc_template.esh",
    lammps_template: str = "mol_dyn/lammps_template.in",
):
    logging.basicConfig(level="INFO")

    smiles_data = load_smiles_csv(smiles_csv)
    os.makedirs(output_folder, exist_ok=True)

    status_dict = dict()
    for i in tqdm.tqdm(range(len(smiles_data))):
        # index corresponds to the indice of the row in the smiles csv. The scripts creates a folder for each index, making it easy to correlate the smiles to the simulation folder
        index, smiles = smiles_data.iloc[i]["Indices"], smiles_data.iloc[i]["Smiles"]

        folder_path = os.path.join(output_folder, str(index))
        os.makedirs(folder_path, exist_ok=True)

        status = pipeline(
            folder_path, steps, cores, field, smiles, emc_template, lammps_template
        )

        if not status["gen_emc_file"]:
            logging.warning(
                f"Simulation for index {index} and SMILES {smiles} failed to generate emc input file."
            )
        elif not status["run_emc"]:
            logging.warning(
                f"Simulation for index {index} and SMILES {smiles} failed to run emc."
            )
        elif not status["gen_lammps_file"]:
            logging.warning(
                f"Simulation for index {index} and SMILES {smiles} failed to generate lammps input file."
            )
        elif not status["run_lammps"]:
            logging.warning(
                f"Simulation for index {index} and SMILES {smiles} failed to run lammps."
            )
        elif not status["gen_spectrum"]:
            logging.warning(
                f"Simulation for index {index} and SMILES {smiles} failed to create spectrum from simulation."
            )

        status_dict[index] = status

    status_df = pd.DataFrame.from_dict(status_dict, orient="index")

    logging.info(
        "Success: {} of total {}: {:.3f}%".format(
            status_df["gen_spectrum"].sum(),
            len(status_df),
            status_df["gen_spectrum"].sum() / len(status_df) * 100,
        )
    )
    status_df.to_csv(os.path.join(output_folder, "status_lammps.csv"))

if __name__ == "__main__":
    main()


import re
import numpy as np
from p1_DICT import *
import time
dictory={}
#制作词典
def get_dict(dict_path):
    with open(dict_path,'r',encoding='utf-8') as f:
        for line in f:
            dictory[line[:len(line)-1]]=0

def isword(word):
    return word in dictory

def fmm(max_len,txt_path='../io_file/train_test/test.txt', fmm_path='../io_file/seg_FMM.txt'):
    '''
    :param max_len: 最大词长度
    :param txt_path: 需要分词的测试文件的地址
    :param fmm_path: 分词后的结果文件的地址
    :return:
    '''
    seg_result = ''
    file = open(txt_path, 'r', encoding='utf-8')
    for line in file:
        if line == '\n':
            continue
        seg_line, line = '', line[:len(line) - 1]  # 去掉读取的换行符
        while len(line) > 0:
            count =max_len
            terminal_word = line[0]  # 单个词
            while count >1: #找多字词
                if isword(line[:count if count<len(line) else len(line)]):
                    terminal_word=line[:count]
                    break
                else:
                    count=count-1
            line = line[len(terminal_word):]
            seg_line += terminal_word + '/'
        seg_line = pre_line(seg_line)  # 将数字、字母、字符之类的连起来
        seg_result += seg_line + '\n'
    open(fmm_path, 'w', encoding='UTF-8').write(seg_result)

#最大反向匹配
def bmm(max_len,txt_path='../io_file/train_test/test.txt', bmm_path='../io_file/seg_BMM.txt'):
    '''

    :param max_len:
    :param txt_path:
    :param fmm_path:
    :return:
    '''
    seg_result = ''
    file = open(txt_path, 'r', encoding='utf-8')
    for line in file:
        if line == '\n':
            continue
        seg_line, line = '', line[:len(line) - 1]  # 去掉读取的换行符
        while len(line) > 0:
            count = max_len
            terminal_word = line[len(line)-1]  # 单个词
            while count > 1:  # 找多字词
                if isword(line[- ( count if count < len(line) else len(line)):]):
                    terminal_word = line[-count:]
                    break
                else:
                    count = count - 1
            line = line[:len(line)-count]
            seg_line = terminal_word + '/'+seg_line
        seg_line = pre_line(seg_line)  # 将数字、字母、字符之类的连起来
        seg_result += seg_line + '\n'
    open(bmm_path, 'w', encoding='UTF-8').write(seg_result)


import sklearn
import joblib
import cv2
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import albumentations
from torch.utils.data import Dataset, DataLoader
from PIL import Image
import time
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate("pooleye-68d49-firebase-adminsdk-zkgaj-4f20e306ba.json")
firebase_admin.initialize_app(cred)
from playsound import playsound
import warnings
warnings.filterwarnings("ignore")

lb = joblib.load('lb.pkl')
class CustomCNN(nn.Module):
    def __init__(self):
        super(CustomCNN, self).__init__()
        self.conv1 = nn.Conv2d(3, 16, 5)
        self.conv2 = nn.Conv2d(16, 32, 5)
        self.conv3 = nn.Conv2d(32, 64, 3)
        self.conv4 = nn.Conv2d(64, 128, 5)
        self.fc1 = nn.Linear(128, 256)
        self.fc2 = nn.Linear(256, len(lb.classes_))
        self.pool = nn.MaxPool2d(2, 2)
    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.pool(F.relu(self.conv3(x)))
        x = self.pool(F.relu(self.conv4(x)))
        bs, _, _, _ = x.shape
        x = F.adaptive_avg_pool2d(x, 1).reshape(bs, -1)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

print('Loading model and label binarizer...')
lb = joblib.load('lb.pkl')
model = CustomCNN().cuda()
print('Model Loaded...')
model.load_state_dict(torch.load('model.pth'))
print('Loaded model state_dict...')
aug = albumentations.Compose([
    albumentations.Resize(224, 224),
    ])

class detection:
  countDrowning=0
  ThersholdForDrowning =  30;
  fram=0
  def detectDrowning(self):
    #input from the camera
    cap = cv2.VideoCapture(0)
    if (cap.isOpened() == False):
        print('Error while trying to read video')
    frame_width = int(cap.get(3))
    frame_height = int(cap.get(4))
    while(cap.isOpened()):
        start_time = time.time()
        ret, frame = cap.read()
        if ret == True:
            model.eval()
            with torch.no_grad():
                pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
                pil_image = aug(image=np.array(pil_image))['image']
                if self.fram == 500:
                 break
                self.fram+=1
                pil_image = np.transpose(pil_image, (2, 0, 1)).astype(np.float32)
                pil_image = torch.tensor(pil_image, dtype=torch.float).cuda()
                pil_image = pil_image.unsqueeze(0)
                outputs = model(pil_image)
                _, preds = torch.max(outputs.data, 1)
            self.calculateDrowning(lb.classes_[preds])
            print("Frame classified as: ",lb.classes_[preds])
        else:
            break

        print("FPS: ", int(1 / (time.time() - start_time)))

    #print(self.countDrowning)
    return 0

  def calculateDrowning(self,classClassified):
    if classClassified == 'drowning':
      self.countDrowning +=1;
      if self.countDrowning >= self.ThersholdForDrowning:
        playsound('alarm.mp3')
        self.alertDrowning()
        self.countDrowning = 0;
    else:
      	self.countDrowning = 0;

  def alertDrowning(self):
    organizationId = "25771623413315686"
    sent = False
    text = "drowning alert"
    db = firestore.client()
    collectionBane = db.collection("lifeguardnotifications").add({
      "orgID": organizationId,
      "sent": sent,
      "text": text,
      "sentTO":"lifeguard"
    })

d = detection()
d.detectDrowning()


# 숨바꼭질 1차원 bfs로 next x 값을 x-1,x+1,x*2 비교해주면서 다음값에 +1씩
from collections import deque
import sys
input=sys.stdin.readline

n,k=map(int,input().split())
graph=[0]*100001

def bfs(n):
  queue=deque()
  queue.append(n)
  while queue:
    x=queue.popleft()
    if x==k:
      return graph[x]
    for nx in (x-1,x+1,x*2):
      if 0<=nx<100001 and not graph[nx]:
        graph[nx]=graph[x]+1
        queue.append(nx)

print(bfs(n))


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

"""
@File    : pathSum.py
@Author  : wenhao
@Time    : 2023/2/1 11:58
@LC      :
"""
from Tree import TreeNode
from typing import List

class Solution:
    def pathSum(self, root: TreeNode, target: int) -> List[List[int]]:
        ans = []
        if root is None:
            return ans
        path = []

        def dfs(node: TreeNode, s: int):
            if node is None:
                return

            s += node.val
            path.append(node.val)

            if s == target and node.left is None and node.right is None:
                ans.append(path.copy())
                path.pop()
                return

            if node.left:
                dfs(node.left, s)
            if node.right:
                dfs(node.right, s)

            path.pop()

        dfs(root, 0)
        return ans


import click
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer

def get_wiki_vocab(file):
    """
    Build a vocabulary from Wikipedia articles.

    Parameters
    ----------
    file : str
        File path to Wikipedia article text

    Returns
    -------
    dict
        Dictionary of words and their frequencies
    """
    vocab = {}
    with open(file, 'r', encoding='utf-8') as f:
        line = f.readline()
        while line:
            if line[0:3]!='XXX':
                line=line.strip('\n').split()
                for token in line:
                    vocab[token.lower()] = vocab.get(token.lower(), 0) + 1
            line=f.readline()
    return vocab

def get_notes_vocab(file):
    """
    Build a vocabulary from MIMIC notes text.

    Parameters
    ----------
    file : str
        File path to combined_dataset from 1_preprocess_mimic.py

    Returns
    -------
    dict
        Dictionary of words and their frequencies
    """
    vocab = {}
    with open(file, 'r', encoding='utf-8') as f:
        line = f.readline()
        while line:
            line = line.strip('\n').split()
            if line[0] == 'codes:':
                line = f.readline()
                line = line.strip('\n').split()
                if line[0] == 'notes:':
                    line = f.readline()
                    while line != 'end!\n':
                        line = line.strip('\n').split()
                        for token in line:
                            vocab[token.lower()] = vocab.get(token.lower(), 0) + 1
                        line = f.readline()
            line = f.readline()
    return vocab

def docs_to_strings(documents):
    """
    Convert a list of lists of tokens to a list of strings.

    Parameters
    ----------
    documents : list
        List of lists of tokens

    Returns
    -------
    list
        List of strings
    """
    data = []
    for doc in documents:
        text = ''
        for token in doc:
            text += token + ' '
        data.append(text)
    return data

def get_wiki_documents(file, vocab):
    """
    Get a list of lists of tokens from Wikipedia articles.

    Parameters
    ----------
    file : str
        File path to Wikipedia article texts
    vocab : dict
        Dictionary of words and their frequencies

    Returns
    -------
    list
        List of lists of tokens
    """
    documents = []
    with open(file, 'r', encoding='utf-8') as f:
        line = f.readline()
        while line:
            if line[0:4] == 'XXXd':
                doc = []
                line = f.readline()
                while line[0:4] != 'XXXe':
                    line = line.strip('\n').split()
                    for token in line:
                        if token.lower() in vocab:
                            doc.append(token.lower())
                    line = f.readline()
                documents.append(doc)
            line = f.readline()

    return documents

def get_notes(file, vocab):
    """
    Get a list of lists of tokens from MIMIC notes.

    Parameters
    ----------
    file : str
        File path to combined_dataset from 1_preprocess_mimic.py
    vocab : dict
        Dictionary of words and their frequencies

    Returns
    -------
    list
        List of lists of tokens
    """
    documents = []
    with open(file, 'r', encoding='utf-8') as f:
        line = f.readline()
        while line:
            line = line.strip('\n').split()
            if line[0] == 'codes:':
                line = f.readline()
                line = line.strip('\n').split()
                if line[0] == 'notes:':
                    doc = []
                    line = f.readline()
                    while line != 'end!\n':
                        line = line.strip('\n').split()
                        for token in line:
                            if token.lower() in vocab:
                                doc.append(token)
                        line = f.readline()
                    documents.append(doc)
            line = f.readline()

    return documents

def vectorize(data, output_file, vectorizer):
    """
    Apply a vectorizer to a list of strings.

    Parameters
    ----------
    data : list
        List of strings
    output_file : str
        File path to save vectorized data
    vectorizer : sklearn.feature_extraction.text.CountVectorizer or sklearn.feature_extraction.text.TfidfVectorizer
        Vectorizer to apply
    """
    embed = vectorizer.fit_transform(data)
    embed = embed.A
    embed = np.array(embed, dtype=float)
    np.save(output_file, embed)

def process_data(file_wiki,
                 file_mimic,
                 output_wiki,
                 output_mimic,
                 vectorizer_type):
    wiki_vocab = get_wiki_vocab(file_wiki)
    note_vocab = get_notes_vocab(file_mimic)

    vocab = set(note_vocab.keys()).intersection(set(wiki_vocab.keys()))

    wiki_docs = get_wiki_documents(file_wiki, vocab)
    notes = get_notes(file_mimic, vocab)

    vocab_map = {}
    for note in notes:
        for token in note:
            if token.lower() not in vocab_map.keys():
                vocab_map[token.lower()] = len(vocab_map)

    wiki_docs = docs_to_strings(wiki_docs)
    notes = docs_to_strings(notes)

    if vectorizer_type == 'binary':
        vectorizer = CountVectorizer(min_df=1, vocabulary=vocab_map, binary=True)
    elif vectorizer_type == 'count':
        vectorizer = TfidfVectorizer(min_df=1, vocabulary=vocab_map, use_idf=False)
    elif vectorizer_type == 'tfidf':
        vectorizer = TfidfVectorizer(min_df=1, vocabulary=vocab_map)

    vectorize(wiki_docs, output_wiki, vectorizer)
    vectorize(notes, output_mimic, vectorizer)

@click.command()
@click.option('--file_wiki', default='data/wikipedia_knowledge')
@click.option('--file_mimic', default='data/combined_dataset')
@click.option('--output_wiki', default='data/wikivec')
@click.option('--output_mimic', default='data/notevec')
@click.option('--vectorizer_type', default='binary', type=click.Choice(['binary', 'count', 'tfidf']))
def process_data_(file_wiki,
                  file_mimic,
                  output_wiki,
                  output_mimic,
                  vectorizer_type):
    process_data(file_wiki,
                 file_mimic,
                 output_wiki,
                 output_mimic,
                 vectorizer_type)

if __name__ == "__main__":
    process_data_()


def best_time_to_buy_and_sell_stock(stocks):
    if not stocks:
        return 0
    ans = 0
    mini = stocks[0]
    for i in range(1,len(stocks)):
        if stocks[i] < mini:
            mini = stocks[i]
        else:
            ans = max(ans, stocks[i] - mini)
    return ans

nums = list(map(int,input("enter the numbers ").strip().split()))
print(nums)
print(best_time_to_buy_and_sell_stock(nums))