from pycocotools.coco import COCO
import os
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
import cv2
from sklearn.metrics import jaccard_score

def test_json_parse(img_dir, coco):
    """
    Тестовая функция для обработки COCO файла
    """
    image_id = 1  # для примера берем id картинки
    img = coco.imgs[image_id]  # обращаемся по id к картинке
    image = np.array(Image.open(os.path.join(img_dir, img['file_name'])))
    plt.imshow(image)
    cat_ids = coco.getCatIds()
    anns_ids = coco.getAnnIds(imgIds=img['id'], catIds=cat_ids, iscrowd=None)
    anns = coco.loadAnns(anns_ids)
    mask = coco.annToMask(anns[0])  # создаем маску
    for i in range(len(anns)):
        mask += coco.annToMask(anns[i])
    mask[mask > 1] = 1  # приводим маски объектов к одному типу
    plt.imshow(mask, cmap='gray')
    plt.show()

def generate_real_masks_from_coco_json(coco, masks_dir):
    """
    Генерация и сохранение масок изображений по файлу COCO json
    :param coco: coco файл с метками
    :param masks_dir: диерктория с фото для обработки
    :return: список масок
    """
    real_masks_array = []
    for idx in np.arange(1, len(coco.imgs) + 1):
        img = coco.imgs[idx]
        cat_ids = coco.getCatIds()
        anns_ids = coco.getAnnIds(imgIds=img['id'], catIds=cat_ids, iscrowd=None)
        anns = coco.loadAnns(anns_ids)
        mask = coco.annToMask(anns[0])
        for i in range(len(anns)):
            mask += coco.annToMask(anns[i])
        mask[mask > 1] = 255
        real_masks_array.append(mask)
        cv2.imwrite(masks_dir + img['file_name'][:-4] + '_mask_real.png', mask)
    return real_masks_array

def get_rectangle_for_grab_cut(detail, empty_table, coeff=50):
    """
    Генерация предварительного прямоугольника для работы GrabCut
    :param detail: фото детали
    :param empty_table: фото пустого стола
    :param coeff: коэффициент для threshold
    :return: прямоугольник
    """
    abs_diff = cv2.absdiff(empty_table, detail)  # делаем разницу кадров
    _, diff_thresh = cv2.threshold(abs_diff, coeff, 255, cv2.THRESH_BINARY)
    mask_gray = cv2.cvtColor(diff_thresh, cv2.COLOR_BGR2GRAY)
    cnts, _ = cv2.findContours(
        mask_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
    min_area = 50  # минимальная площадь рисуемого прямоугольника
    x_0, y_0, w_0, h_0 = 80, 50, 1750, 1080  # рисуем границы рабочей области
    eps = 50  # делаем прямоугольник не вплотную (отступаем несколько пикселей)
    cv2.rectangle(detail, (x_0, y_0), (w_0, h_0), (0, 255, 0), 2)
    list_of_rects = []
    for c in cnts:
        x, y, w, h = cv2.boundingRect(c)
        if (cv2.contourArea(c) < min_area) or (x < x_0) or (x > w_0) or (y < y_0) or (y > h_0):
            continue
        x -= eps
        y -= eps
        w += 2 * eps
        h += 2 * eps
        cv2.rectangle(detail, (x, y), (x + w, y + h), (0, 255, 0), 2)
        list_of_rects.append((x, y, w, h))
    if list_of_rects:
        rect = list_of_rects[0]
    else:
        raise Exception('Empty list of rectangles')
    return rect

#  применение grab_cut
def apply_grab_cut(detail_def, rect_for_grab_cut, iters=5):
    """
    Применяем GrabCut к изображению в заданном прямоугольнике
    :param detail_def: фото детали
    :param rect_for_grab_cut: примерный прямоугольник в котором искать деталь
    :param iters: количество итераций GrabCut
    :return: итоговая черно-белая маска
    """
    mask = np.zeros(detail_def.shape[:2], np.uint8)
    bgdModel = np.zeros((1, 65), np.float64)
    fgdModel = np.zeros((1, 65), np.float64)
    grab_cut_mask_final, _, _ = cv2.grabCut(detail_def, mask, rect_for_grab_cut, bgdModel, fgdModel, iters, cv2.GC_INIT_WITH_RECT)
    mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
    black_and_white_mask = np.where(mask2 == 0, mask2, 255).astype('uint8')
    return black_and_white_mask

#  генерация итоговой маски для grab_cut_absdiff_thresh
def generate_grab_cut_and_absdiff_masks(img_dir, root, masks_dir_pred):
    """
    Генерация итоговой маски для grab_cut_absdiff_thresh
    :param img_dir: фото детали
    :param root: корень директории
    :param masks_dir_pred: папка для сохранения масок
    :return: список сгенерированных масок
    """
    #  ВАЖНО! имя дефолтного фото пустого стола должно быть 'empty_table.jpg'
    #  и лежать в той же папке с исходными масками
    if 'empty_table.jpg' not in os.listdir(img_dir):
        raise Exception('No empty table image found')
    empty_table_str = 'empty_table.jpg'
    empty_table = cv2.imread(empty_table_str)
    calculated_masks_array = []
    for filename in os.listdir(img_dir):
        if filename[filename.rfind(".") + 1:] in ['jpg', 'jpeg', 'png'] and (filename != empty_table_str):
            absolute_path = os.path.join(root, filename)
            detail = cv2.imread(absolute_path)
            detail_def = detail.copy()
            rect_for_grab_cut = get_rectangle_for_grab_cut(detail, empty_table)
            mask_calculated_grab_cut = apply_grab_cut(detail_def, rect_for_grab_cut)
            cv2.imwrite(masks_dir_pred + filename[:-4] + '_mask_generated.png', mask_calculated_grab_cut)
            calculated_masks_array.append(mask_calculated_grab_cut)
    return calculated_masks_array

def calculate_jacard_score(root, ground_truth_dir, pred_dir):
    """
    Подсчет IoU сгенерированной и эталонной маски
    :param root: корень директории
    :param ground_truth_dir: директория с эталонными масками
    :param pred_dir: директория с предсказанными масками
    :return: (список предсказанных файлов, список метрик IoU для каждого фото детали)
    """
    absolute_path_ground_truth = os.path.join(root, ground_truth_dir)
    absolute_path_pred = os.path.join(root, pred_dir)
    list_of_files_ground_truth = []
    list_of_files_pred = []
    for file in os.listdir(absolute_path_ground_truth):
        if file.endswith('.png'):
            list_of_files_ground_truth.append(file)
    for file in os.listdir(absolute_path_pred):
        if file.endswith('.png'):
            list_of_files_pred.append(file)
    list_of_files_ground_truth.sort()
    list_of_files_pred.sort()
    jaccard_score_list = []
    for i in range(len(list_of_files_pred)):
        absolute_path_gt = os.path.join(absolute_path_ground_truth, list_of_files_ground_truth[i])
        absolute_path_pr = os.path.join(absolute_path_pred, list_of_files_pred[i])
        gt_ = cv2.imread(absolute_path_gt)
        pr_ = cv2.imread(absolute_path_pr)
        gt = cv2.cvtColor(gt_, cv2.COLOR_BGR2GRAY)
        pr = cv2.cvtColor(pr_, cv2.COLOR_BGR2GRAY)
        gt[gt > 1] = 1
        pr[pr > 1] = 1
        gt = np.array(gt).reshape(-1, 1)
        pr = np.array(pr).reshape(-1, 1)
        score = jaccard_score(gt, pr)
        jaccard_score_list.append(score)
    return list_of_files_pred, np.array(jaccard_score_list)

def pure_abs_diff_calculate(img_dir, root, masks_dir_pred_abs_diff, coeff=50):
    """
    Применение чистой разности кадров и искусственного threshold к изображению
    :param img_dir: фото детали
    :param root: корень директории
    :param masks_dir_pred_abs_diff: директория с предсказанными масками
    :param coeff: коэффициент для threshold
    :return: список сгенерированных масок
    """
    #  ВАЖНО! имя дефолтного фото пустого стола должно быть 'empty_table.jpg'
    #  и лежать в той же папке с исходными масками
    if 'empty_table.jpg' not in os.listdir(img_dir):
        raise Exception('No empty table image found')
    empty_table_str = 'empty_table.jpg'
    empty_table = cv2.imread(empty_table_str)
    calculated_masks_array = []
    x_0, y_0, w_0, h_0 = 80, 75, 1750, 1080  # границы рабочей области
    for filename in os.listdir(img_dir):
        if filename[filename.rfind(".") + 1:] in ['jpg', 'jpeg', 'png'] and (filename != empty_table_str):
            absolute_path = os.path.join(root, filename)
            detail = cv2.imread(absolute_path)
            abs_diff = cv2.absdiff(empty_table, detail)  # делаем разницу кадров
            mask_gray = cv2.cvtColor(abs_diff, cv2.COLOR_BGR2GRAY)
            black_and_white_mask = np.where(mask_gray <= coeff, mask_gray, 255).astype('uint8')
            black_and_white_mask[black_and_white_mask <= coeff] = 0
            black_and_white_mask[0:x_0, 0:1920] = 0  # закрашиваем то, что за пределами рабочей области
            black_and_white_mask[0:1080, w_0:1920] = 0
            black_and_white_mask[0:1080, 0:y_0] = 0
            cv2.imwrite(masks_dir_pred_abs_diff + filename[:-4] + '_mask_generated.png', black_and_white_mask)
            calculated_masks_array.append(black_and_white_mask)
    return calculated_masks_array

# чистый grab_cut область -- весь стол
def pure_grab_cut_full_table(img_dir, root, masks_dir_grab_cut):
    """
    Применение алгоритма GrabCut когда область поиска -- весь стол
    :param img_dir: фото детали
    :param root: корень директории
    :param masks_dir_grab_cut: директория с предсказанными масками
    :return: список сгенерированных масок
    """
    #  ВАЖНО! имя дефолтного фото пустого стола должно быть 'empty_table.jpg'
    # и лежать в той же папке с исходными масками
    if 'empty_table.jpg' not in os.listdir(img_dir):
        raise Exception('No empty table image found')
    empty_table_str = 'empty_table.jpg'
    calculated_masks_array = []
    x_0, y_0, w_0, h_0 = 80, 75, 1750, 1080  # границы рабочей области
    for filename in os.listdir(img_dir):
        if filename[filename.rfind(".") + 1:] in ['jpg', 'jpeg', 'png'] and (filename != empty_table_str):
            absolute_path = os.path.join(root, filename)
            detail = cv2.imread(absolute_path)
            rect_for_grab_cut = x_0, y_0, w_0, h_0
            black_and_white_mask = apply_grab_cut(detail, rect_for_grab_cut, iters=20)
            black_and_white_mask_inv = np.where(black_and_white_mask > 0, 0, 255).astype('uint8')
            black_and_white_mask_inv[0:x_0, 0:1920] = 0
            black_and_white_mask_inv[0:1080, w_0:1920] = 0
            black_and_white_mask_inv[0:1080, 0:y_0] = 0
            cv2.imwrite(masks_dir_grab_cut + filename[:-4] + '_mask_generated.png', black_and_white_mask_inv)
            calculated_masks_array.append(black_and_white_mask_inv)
    return calculated_masks_array

def pure_threshold_calculate(img_dir, root, masks_dir_pred_threshold):
    """
    Применение чистого threshold
    :param img_dir: фото детали
    :param root: корень директории
    :param masks_dir_pred_threshold: директория с предсказанными масками
    :return: список сгенерированных масок
    """
    #  ВАЖНО! имя дефолтного фото пустого стола должно быть 'empty_table.jpg'
    # и лежать в той же папке с исходными масками
    if 'empty_table.jpg' not in os.listdir(img_dir):
        raise Exception('No empty table image found')
    empty_table_str = 'empty_table.jpg'
    calculated_masks_array = []
    x_0, y_0, w_0, h_0 = 80, 75, 1750, 1080  # границы рабочей области
    for filename in os.listdir(img_dir):
        if filename[filename.rfind(".") + 1:] in ['jpg', 'jpeg', 'png'] and (filename != empty_table_str):
            absolute_path = os.path.join(root, filename)
            detail = cv2.imread(absolute_path)
            detail = cv2.cvtColor(detail, cv2.COLOR_BGR2GRAY)
            ret, thresh = cv2.threshold(detail, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
            thresh[0:x_0, 0:1920] = 0
            thresh[0:1080, w_0:1920] = 0
            thresh[0:1080, 0:y_0] = 0
            cv2.imwrite(masks_dir_pred_threshold + filename[:-4] + '_mask_generated.png', thresh)
            calculated_masks_array.append(thresh)
    return calculated_masks_array

def threshold_kanny_calculate(img_dir, root, masks_dir_pred_threshold_kanny):
    """
    Применение алгоритма Canny и threshold
    :param img_dir: фото детали
    :param root: корень директории
    :param masks_dir_pred_threshold_kanny: директория с предсказанными масками
    :return: список сгенерированных масок
    """
    #  ВАЖНО! имя дефолтного фото пустого стола должно быть 'empty_table.jpg'
    #  и лежать в той же папке с исходными масками
    if 'empty_table.jpg' not in os.listdir(img_dir):
        raise Exception('No empty table image found')
    empty_table_str = 'empty_table.jpg'
    calculated_masks_array = []
    x_0, y_0, w_0, h_0 = 80, 75, 1750, 1080  # границы рабочей области
    for filename in os.listdir(img_dir):
        if filename[filename.rfind(".") + 1:] in ['jpg', 'jpeg', 'png'] and (filename != empty_table_str):
            absolute_path = os.path.join(root, filename)
            detail = cv2.imread(absolute_path)
            detail = cv2.cvtColor(detail, cv2.COLOR_BGR2GRAY)
            ret, thresh = cv2.threshold(detail, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
            edges = cv2.Canny(thresh, 0, 255)
            thresh += edges
            mask_thresh_kanny = thresh
            mask_thresh_kanny[0:x_0, 0:1920] = 0
            mask_thresh_kanny[0:1080, w_0:1920] = 0
            mask_thresh_kanny[0:1080, 0:y_0] = 0
            cv2.imwrite(masks_dir_pred_threshold_kanny + filename[:-4] + '_mask_generated.png', mask_thresh_kanny)
            calculated_masks_array.append(mask_thresh_kanny)
    return calculated_masks_array

# Load the annotations for the coco dataset

coco = COCO('dataset_article/labels_article_names.json')
# папка с исходными фотографиями
img_dir = 'dataset_article/'
#  относительный путь до папки с масками исходных фото
masks_dir_real = 'dataset_article/masks_real/'
#  имя папки с масками исходных фото
ground_truth_dir = 'masks_real'
#  абсолютный путь до папки с фото
root = '/Users/alexsoldatov/PycharmProjects/608_project/Background_open_cv/Raznost_kadrov/dataset_article'
#  генерация реальных масок изображений по разметке
# real_masks_array = generate_real_masks_from_coco_json(coco, masks_dir_real)

# -------------------------------------------------------------------------------------------------------------
#  относительный путь до папки с предсказанными масками grab_cut_absdiff_thresh
masks_dir_pred_grab_cut_absdiff_thresh = 'dataset_article/masks_dir_pred_grab_cut_absdiff_thresh/'
#  имя папки с масками предсказанных фото grab_cut_absdiff_thresh
pred_dir_grab_cut_absdiff_thresh = 'masks_pred_grab_cut_absdiff_thresh'
pred_masks_array_grab_cut_absdiff_thresh = generate_grab_cut_and_absdiff_masks(img_dir, root, masks_dir_pred_grab_cut_absdiff_thresh)
# file_names, jaccard_grab_cut_absdiff_thresh = calculate_jacard_score(root,
#                                                                      ground_truth_dir,
#                                                                      pred_dir_grab_cut_absdiff_thresh)
# print(file_names)
# print(jaccard_grab_cut_absdiff_thresh)
# print(np.mean(jaccard_grab_cut_absdiff_thresh))
# ----------------------------------------------------------------------------------------
#  относительный путь до папки с предсказанными масками abs_diff
masks_dir_pred_abs_diff = 'dataset_article/masks_pred_abs_diff/'
#  имя папки с масками предсказанных фото abs_diff
pred_dir_abs_diff = 'masks_pred_abs_diff'
# pred_masks_array_absdiff = pure_abs_diff_calculate(img_dir, root, masks_dir_pred_abs_diff)
# file_names, jaccard_abs_diff = calculate_jacard_score(root, ground_truth_dir, pred_dir_abs_diff)
# print(file_names)
# print(jaccard_abs_diff)
# print(np.mean(jaccard_abs_diff))
# ------------------------------------------------------------------------------------------------
#  относительный путь до папки с предсказанными масками grab_cut
masks_dir_pred_grab_cut = 'dataset_article/masks_pred_grab_cut/'
#  имя папки с масками предсказанных фото grab_cut
pred_dir_grab_cut = 'masks_pred_grab_cut'
# pred_masks_array_grabcut = pure_grab_cut_full_table(img_dir, root, masks_dir_pred_grab_cut)
# file_names, jaccard_grab_cut = calculate_jacard_score(root, ground_truth_dir, pred_dir_grab_cut)
# print(file_names)
# print(jaccard_grab_cut)
# print(np.mean(jaccard_grab_cut))
# ---------------------------------------------------------------------------------------------------
#  относительный путь до папки с предсказанными масками threshold
masks_dir_pred_threshold = 'dataset_article/masks_pred_threshold/'
#  имя папки с масками предсказанных фото threshold
pred_dir_threshold = 'masks_pred_threshold'
# pred_masks_array_threshold = pure_threshold_calculate(img_dir, root, masks_dir_pred_threshold)
# file_names, jaccard_threshold = calculate_jacard_score(root, ground_truth_dir, pred_dir_threshold)
# print(file_names)
# print(jaccard_threshold)
# print(np.mean(jaccard_threshold))
# --------------------------------------------------------------------------------------------------------
#  относительный путь до папки с предсказанными масками threshold_kanny
masks_dir_pred_threshold_kanny = 'dataset_article/masks_pred_threshold_kanny/'
#  имя папки с масками предсказанных фото threshold_kanny
pred_dir_threshold_kanny = 'masks_pred_threshold_kanny'
# pred_masks_array_threshold_kanny = threshold_kanny_calculate(img_dir, root, masks_dir_pred_threshold_kanny)
# file_names, jaccard_threshold_kanny = calculate_jacard_score(root, ground_truth_dir, pred_dir_threshold_kanny)
# print(file_names)
# print(jaccard_threshold_kanny)
# print(np.mean(jaccard_threshold_kanny))


# This file is part of python-paillier.
#
# python-paillier is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# python-paillier is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with python-paillier.  If not, see <http://www.gnu.org/licenses/>.

import os
from setuptools import setup, find_packages

here = os.path.abspath(os.path.dirname(__file__))

def find_version():
    # Note the version is also in the docs/conf.py file
    # We use semantic versioning - semver.org
    return "1.3.0"

setup(
    name="phe",
    version=find_version(),
    description="Partially Homomorphic Encryption library for Python",
    long_description=open(os.path.join(here, "README.rst")).read(),
    url="https://github.com/n1analytics/python-paillier",
    download_url="https://pypi.python.org/pypi/phe/#downloads",
    author="Data61 | CSIRO",
    author_email="brian.thorne@data61.csiro.au",
    license="GPLv3",
    classifiers=[
        'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
        'Natural Language :: English',
        'Topic :: Scientific/Engineering :: Mathematics',
        'Topic :: Security',
        'Topic :: Security :: Cryptography',
        'Intended Audience :: Science/Research',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
    ],
    keywords="cryptography encryption homomorphic",
    packages=find_packages(exclude=['tests*']),
    entry_points={
        'console_scripts': [
            'pheutil = phe.command_line:cli [cli]'
        ],
    },
    extras_require={
        'cli': ['click']
    },
    install_requires=['gmpy2'],
    tests_require=['numpy', 'click'],
    test_suite="phe.tests"
)


# https://gist.github.com/t-ae/6e1016cc188104d123676ccef3264981
import keras.backend as K
from keras.engine.topology import Layer

class PixelShuffler(Layer):
    def __init__(self, size=2):
        super(PixelShuffler, self).__init__()
        self.size = size

    def call(self, inputs):
        input_shape = K.shape(inputs)
        h, w, c = input_shape[1], input_shape[2], input_shape[3]
        oh, ow = h * self.size, w * self.size
        oc = c // (self.size * self.size)

        out = K.reshape(inputs, (-1, h, w, self.size, self.size, oc))
        out = K.permute_dimensions(out, (0, 1, 3, 2, 4, 5))
        out = K.reshape(out, (-1, oh, ow, oc))
        return out

    def compute_output_shape(self, input_shape):
        channels = input_shape[3] // self.size // self.size
        return None, None, None, channels


import os
import numpy as np
from PIL import Image

import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader

transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

class Cifar10Dataset(Dataset):
    def __init__(self, files, label_mapping, transform=None):

        self.files = files
        self.data_size = len(self.files)
        self.label_mapping = label_mapping
        # self.files = random.sample(files, self.data_size)
        self.transform = transform

    def __len__(self):
        return self.data_size

    def __getitem__(self, idx):
        image_address = self.files[idx]
        image = Image.open(image_address)
        image = np.array(image)

        label_name = image_address[:-4].split("_")[-1]
        label = self.label_mapping[label_name]

        if self.transform:
            image = self.transform(image)

        return image, label

def fetch(data_dir, train=True, batch_size=128):

    # Create label mapping
    with open(data_dir + "/labels.txt") as label_file:
        labels = label_file.read().split()
        label_mapping = dict(zip(labels, list(range(len(labels)))))

    if train:

        data_dir = data_dir + "/train"

    else:
        data_dir = data_dir + "/test"

    files = os.listdir(data_dir)
    files = [os.path.join(data_dir, x) for x in files]

    dataset = Cifar10Dataset(files=files,
                             label_mapping=label_mapping,
                             transform=transform)

    dataloader = DataLoader(dataset,
                            batch_size=batch_size,
                            shuffle=True,
                            num_workers=1)

    return dataloader


from configparser import SectionProxy
from Common.Backend import Get_Billsday, Get_Sale_store, Get_opn
from datetime import date,timedelta
from docx import Document
from docx.oxml.ns import qn
from docx.enum.section import WD_SECTION

dte=date.today()

WNS_COLS_NUM = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}num"

def set_number_of_columns(section, cols):
    """ sets number of columns through xpath. """
    section._sectPr.xpath("./w:cols")[0].set(WNS_COLS_NUM, str(cols))

def del_para(para):
    p=para._element
    p.getparent().remove(p)
    p._p=p._element=None

def gen_billtoday(today):
    from docx2pdf import convert
    import os
    db=Get_Billsday(today)
    opn=Get_opn(today)
    print(db)
    billdoc=Document()
    sec1=billdoc.add_section(WD_SECTION.ODD_PAGE)
    set_number_of_columns(sec1, 2)
    opening=billdoc.add_heading(f'Opening Cash-{opn}', level=2)
    SSh=billdoc.add_heading('Silver Sale', level=2)
    SSp=billdoc.add_table(rows=1, cols=4)
    SSp.style='TableGrid'
    SSpr=SSp.rows[0].cells
    SSpr[0].text='Amount'
    SSpr[1].text='Item'
    SSpr[2].text='Gr Wt'
    SSpr[3].text='Nt Wt'
    SHPh=billdoc.add_heading('Misc/Shree Aaya', level=2)
    SHPp=billdoc.add_table(rows=1, cols=2)
    SHPp.style='TableGrid'
    SHPp.rows[0].cells[0].text='Amount'
    SHPp.rows[0].cells[1].text='Type'
    OOh=billdoc.add_heading('Old Order', level=2)
    OOp=billdoc.add_table(rows=1, cols=5)
    OOp.style='TableGrid'
    OOp.rows[0].cells[0].text='Amount'
    OOp.rows[0].cells[1].text='Cust Name'
    OOp.rows[0].cells[2].text='Item'
    OOp.rows[0].cells[3].text='Gr Wt'
    OOp.rows[0].cells[4].text='Nt Wt'
    GSh=billdoc.add_heading('Gold Sale', level=2)
    GSp=billdoc.add_table(rows=1, cols=5)
    GSp.style='TableGrid'
    GSp.rows[0].cells[0].text='Item'
    GSp.rows[0].cells[1].text='Gr Wt'
    GSp.rows[0].cells[2].text='Nt Wt'
    GSp.rows[0].cells[3].text='Name'
    GSp.rows[0].cells[4].text='Amount'
    NOh=billdoc.add_heading('New Order', level=2)
    NOp=billdoc.add_table(rows=1, cols=6)
    NOp.style='TableGrid'
    NOp.rows[0].cells[0].text='Est Amt'
    NOp.rows[0].cells[1].text='Adv'
    NOp.rows[0].cells[2].text='Name'
    NOp.rows[0].cells[3].text='Item'
    NOp.rows[0].cells[4].text='Gr Wt'
    NOp.rows[0].cells[5].text='Nt Wt'
    VCh=billdoc.add_heading('VC', level=2)
    VCp=billdoc.add_table(rows=1, cols=5)
    VCp.style='TableGrid'
    VCp.rows[0].cells[0].text='Amount'
    VCp.rows[0].cells[1].text='Name'
    VCp.rows[0].cells[2].text='Item'
    VCp.rows[0].cells[3].text='Gr Wt'
    VCp.rows[0].cells[4].text='Nt Wt'
    section=billdoc.add_section(WD_SECTION.NEW_COLUMN)
    SPh=billdoc.add_heading('Silver Purchase', level=2)
    SPp=billdoc.add_table(rows=1, cols=4)
    SPp.style='TableGrid'
    SPp.rows[0].cells[0].text='Amount'
    SPp.rows[0].cells[1].text='Item'
    SPp.rows[0].cells[2].text='Gr Wt'
    SPp.rows[0].cells[3].text='Nt Wt'
    SHMh=billdoc.add_heading('Misc/Shree Gaya', level=2)
    SHMp=billdoc.add_table(rows=1, cols=2)
    SHMp.style='TableGrid'
    SHMp.rows[0].cells[0].text='Amount'
    SHMp.rows[0].cells[1].text='Type'
    BEh=billdoc.add_heading('Bank Entries', level=2)
    BEp=billdoc.add_table(rows=1, cols=3)
    BEp.style='TableGrid'
    BEp.rows[0].cells[0].text='Amount'
    BEp.rows[0].cells[1].text='Type'
    BEp.rows[0].cells[2].text='Name'
    Exh=billdoc.add_heading('Expenses', level=2)
    Exp=billdoc.add_table(rows=1, cols=2)
    Exp.style='TableGrid'
    Exp.rows[0].cells[0].text='Amount'
    Exp.rows[0].cells[1].text='Type'
    GPh=billdoc.add_heading('Gold Purchase', level=2)
    GPp=billdoc.add_table(rows=1, cols=5)
    GPp.style='TableGrid'
    GPp.rows[0].cells[0].text='Item'
    GPp.rows[0].cells[1].text='Gr Wt'
    GPp.rows[0].cells[2].text='Nt Wt'
    GPp.rows[0].cells[3].text='Name'
    GPp.rows[0].cells[4].text='Amount'
    CCh=billdoc.add_heading('Closing Cash', level=2)
    GStotal,SStotal, OOtotal, NOtotal, VCtotal, GPtotal, SPtotal, SHPtotal, SHMtotal, EXtotal=0,0,0,0,0,0,0,0,0,0
    SSntwt, GSntwt, GPntwt, SPntwt=0.0, 0.0, 0.0, 0.0
    for i in db:
        print(i)
        if i[10]=='Gold Sale':
            GSrow=GSp.add_row().cells
            GSrow[0].text=str(i[3])
            GSrow[1].text=str(i[6])
            GSrow[2].text=str(i[7])
            GSrow[3].text=str(i[0])
            GSrow[4].text=str(i[11])
            GStotal+=i[11]
            GSntwt+=i[7]
        elif i[10]=='Silver Sale':
            SSrow=SSp.add_row().cells
            SSrow[0].text=str(i[11])
            SSrow[1].text=str(i[3])
            SSrow[2].text=str(i[8])
            SSrow[3].text=str(i[9])
            SStotal+=i[11]
            SSntwt+=i[7]
        elif i[10]=='Old Order':
            OOrow=OOp.add_row().cells
            OOrow[0].text=str(i[11])
            OOrow[1].text=str(i[0])
            OOrow[2].text=str(i[3])
            OOrow[3].text=str(i[6])
            OOrow[4].text=str(i[7])
            OOtotal+=i[11]
        elif i[10]=='New Order':
            NOrow=NOp.add_row().cells
            NOrow[0].text=str(i[4])
            NOrow[1].text=str(i[11])
            NOrow[2].text=str(i[0])
            NOrow[3].text=str(i[3])
            NOrow[4].text=str(i[6])
            NOrow[5].text=str(i[7])
            NOtotal+=i[11]
        elif i[10]=='VC':
            VCrow=VCp.add_row().cells
            VCrow[0].text=str(i[11])
            VCrow[1].text=str(i[0])
            VCrow[2].text=str(i[3])
            VCrow[3].text=str(i[6])
            VCrow[4].text=str(i[7])
            VCtotal+=i[11]
        elif i[10]=='Gold Purchase':
            GProw=GPp.add_row().cells
            GProw[0].text=str(i[3])
            GProw[1].text=str(i[6])
            GProw[2].text=str(i[7])
            GProw[3].text=str(i[0])
            GProw[4].text=str(i[11])
            GPtotal+=i[11]
            GPntwt+=i[7]
        elif i[10]=='Silver Purchase':
            SProw=SPp.add_row().cells
            SProw[0].text=str(i[11])
            SProw[1].text=str(i[3])
            SProw[2].text=str(i[8])
            SProw[3].text=str(i[9])
            SPtotal+=i[11]
            SPntwt+=i[7]
        elif i[10]=='Misc Plus':
            SHProw=SHPp.add_row().cells
            SHProw[0].text=str(i[11])
            SHProw[1].text=str(i[0])
            SHPtotal+=i[11]
        elif i[10]=='Misc Minus':
            SHMrow=SHMp.add_row().cells
            SHMrow[0].text=str(i[11])
            SHMrow[1].text=str(i[0])
            SHMtotal+=i[11]
        elif i[10]=='Expense':
            Exprow=Exp.add_row().cells
            Exprow[0].text=str(i[11])
            Exprow[1].text=str(i[0])
            EXtotal+=i[11]
        if i[19]>0:
            SHMrow=SHMp.add_row().cells
            SHMrow[0].text=str(i[11])
            SHMrow[1].text=str(i[0]+" (CR)")
            SHMtotal+=i[11]
        if i[13]!=0:
            BEprow=BEp.add_row().cells
            BEprow[0].text=str(i[13])
            BEprow[1].text=str(i[14])
            BEprow[2].text=str(i[0])
        if i[15]!=0:
            BEprow=BEp.add_row().cells
            BEprow[0].text=str(i[15])
            BEprow[1].text=str(i[16])
            BEprow[2].text=str(i[0])
        if i[17]!=0:
            BEprow=BEp.add_row().cells
            BEprow[0].text=str(i[17])
            BEprow[1].text=f'Chq-{i[18]}'
            BEprow[2].text=str(i[0])

    GSh.add_run(f' - {GStotal}')
    GSlastline=GSp.add_row().cells
    GSlastline[0].text='Total'
    GSlastline[2].text=str(GSntwt)
    SSh.add_run(f' - {SStotal}')
    SSlastline=SSp.add_row().cells
    SSlastline[0].text='Total'
    SSlastline[3].text=str(SSntwt)
    OOh.add_run(f' - {OOtotal}')
    NOh.add_run(f' - {NOtotal}')
    VCh.add_run(f' - {VCtotal}')
    GPh.add_run(f' - {GPtotal}')
    GPlastline=GPp.add_row().cells
    GPlastline[0].text='Total'
    GPlastline[2].text=str(GPntwt)
    SPh.add_run(f' - {SPtotal}')
    SPlastline=SPp.add_row().cells
    SPlastline[0].text='Total'
    SPlastline[3].text=str(SPntwt)
    SHPh.add_run(f' -  {SHPtotal}')
    SHMh.add_run(f' - {SHMtotal}')
    Exh.add_run(f' - {EXtotal}')

    para1=billdoc.paragraphs[0]
    del_para(para1)
    try:
        billdoc.save(f'{today}.docx')
    except:
        import os
        os.rmdir(f'{today}.docx')
        billdoc.save(f'{today}.docx')

def gen_sum(today):
    dbdict=Get_Sale_store(today, today)

    sumdoc=Document()
    sumdoc.add_heading("Gold Sale", level=2)
    GS=sumdoc.add_table(rows=1, cols=2)
    GS.style='TableGrid'
    GS.rows[0].cells[0].text='Item Name'
    GS.rows[0].cells[1].text='Amount'
    sumdoc.add_heading("Silver Sale", level=2)
    SS=sumdoc.add_table(rows=1, cols=2)
    SS.style='TableGrid'
    SS.rows[0].cells[0].text='Item Name'
    SS.rows[0].cells[1].text='Amount'
    GSdup, SSdup={}, {}
    for key in dbdict.keys():
        if key=='Gold Sale':
            for i in dbdict[key]:
                for j in i:
                    if str(j) in GSdup.keys():
                        GSdup[str(j[0])]+=1
                    else:
                        GSdup[str(j[0])]=1
        if key=='Silver Sale':
            for i in dbdict[key]:
                for j in i:
                    if str(j) in SSdup.keys():
                        SSdup[str(j[0])]+=1
                    else:
                        SSdup[str(j[0])]=1
    for key in GSdup.keys():
        GSrow=GS.add_row().cells
        GSrow[0].text=key
        GSrow[1].text=str(GSdup[key])
    for key in SSdup.keys():
        SSrow=SS.add_row().cells
        SSrow[0].text=key
        SSrow[1].text=str(SSdup[key])
    sumdoc.save(f'{today}Sum.docx')

if __name__=='__main__':
    # gen_billtoday(dte)
    gen_sum(dte)


#!/usr/bin/env python
import sys
import argparse
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import events
import filters
from collections import defaultdict
from matplotlib.colors import ColorConverter

parser = argparse.ArgumentParser(description='Print histogram of events')
filters.add_all_filters(parser)
args = parser.parse_args()
filter = filters.get_filter(args)

# x_range = (1029553.184, 1029553.190)
#x_range = (1029333.805010, 1029333.825010)
x_range = None

fig, ax = plt.subplots()

palette = {
    events.TimelineElement.RUNNING: 'green',
    events.TimelineElement.SLEEPING: 'white',
    events.TimelineElement.SLEEPING_UNINTERRUPTIBLY: 'lightblue',
    events.TimelineElement.PREEMPTED: 'lightgrey',
    events.TimelineElement.WOKEN: 'red',
}

rgb_palette = dict((k, ColorConverter().to_rgb(c)) for k, c in palette.iteritems())

class ProcHistory:
    def __init__(self, y):
        self.y = y
        self.bars = list()
        self.colors = list()

class Rows:
    def __init__(self, height):
        self.height = height
        self.yticks = []
        self.yticklabels = []
        self.y = height + 5

    def new_row(self, label):
        self.yticks.append(self.y + self.height/2)
        self.yticklabels.append(label)
        result = self.y
        self.y += self.height
        return result

    def annotate(self, ax):
        ax.set_yticks(self.yticks)
        ax.set_yticklabels(self.yticklabels)

history_by_proc = {}
rows = Rows(height=10)

lines = list(sys.stdin)
min_time = None

for elem in events.get_sched_timeline(lines):
    if not filter(elem):
        continue
    if not min_time:
        min_time = elem.start
    if not elem.proc in history_by_proc:
        hist = ProcHistory(rows.new_row(elem.proc))
        history_by_proc[elem.proc] = hist
    else:
        hist = history_by_proc[elem.proc]

    h = rows.height * 0.75
    color = rgb_palette[elem.state]
    hist.bars.append((elem.start, elem.duration))
    hist.colors.append(color)

for proc, hist in history_by_proc.iteritems():
    ax.broken_barh(hist.bars, (hist.y, rows.height - 1), facecolors=hist.colors, edgecolor='face')

if x_range:
    ax.set_xlim(x_range[0], x_range[1])
else:
    ax.set_xlim(min_time, min_time + 0.05)
ax.set_xlabel('time [s]')
rows.annotate(ax)

ax.grid(True)
ax.annotate('on-cpu', (61, 25),
            xytext=(0.8, 0.9), textcoords='axes fraction',
            arrowprops=dict(facecolor='black', shrink=0.05),
            fontsize=16,
            horizontalalignment='right', verticalalignment='top')

patches = []
labels = []
for state, color in palette.iteritems():
    label = str(state).lower()
    patches.append(mpatches.Patch(color=color, label=label))
    labels.append(label)
plt.legend(patches, labels, 'upper right')

plt.show()


import os
import re
import numpy as np
import pygame
import math
from .utils import *
from .env_configs import *
import scipy.spatial as sp

# ==============================================
# Car Class
# ==============================================

class Car(pygame.sprite.Sprite):
    """
        Car Pygame sprite object.

        Parameters
        ----------
        init_angle: int
        init_x: int
        init_y: int
        fps: int
    """
    def __init__(self, init_angle, init_x, init_y, car_file, fps=60):
        self.image = pygame.image.load(car_file)
        self.img = None
        self.img_mask = None
        self.img_rect = None
        self.clock = pygame.time.Clock()
        self.fps = fps

        self.x = init_x
        self.y = init_y
        self.theta = init_angle
        self.v = 0                  # center velocity
        self.w = 0                  # angular velocity
        self.dv = 0                 # acceleration
        self.dw = 0                 # angular acceleration
        self.l = 1                  # half the length of axis between the wheel
        self.M = 1                  # mass of unicycle
        self.J = 1                  # inertia moment
        self.Bv = 1                 # translational friction coefficient
        self.Bw = 25                 # rotational friction coefficient

        pygame.sprite.Sprite.__init__(self)

    def reset(self):
        """
            Reset car member variables.
        """
        self.theta = 0
        self.x = self.init_x
        self.y = self.init_y

    @property
    def xpos(self): return self.x

    @property
    def ypos(self): return SCREEN_HEIGHT - self.y

    @property
    def theta_degrees(self):
        return self.theta*(180/math.pi)

    @property
    # pose of car with bottom left origin
    def pose(self): return [self.xpos, self.y, self.theta_degrees]

    @property
    def to_screen_coords(self):
        """Convert pose of car from car from to pygame coordinates
        """
        return [self.xpos, self.ypos, self.theta_degrees]

    @property
    def center(self):
        """Compute direct center of egovehicle
        """
        return [self.xpos + self.img_rect.center[0], self.ypos + self.img_rect.center[1]]

    def set_friction(self, trans_coef, rot_coef):
        self.Bv = trans_coef
        self.Bw = rot_coef

    def step(self, action):
        """Take a step in the environment
        Parameters
        ----------
        action: numpy.array
        """
        # ================================
        # Unicycle Dynamics Model
        # ================================
        # dt = self.clock.tick_busy_loop(self.fps) / 1000.0 # in milliseconds
        dt = 0.033 # cancel the adaptive time interval settings staff

        if action is None: return
        F = action[0] + action[1]
        T = self.l * (action[1] - action[0])

        # the acceleration
        self.dw = (T - self.Bw*self.w)/self.J
        self.dv = (F - self.Bv*self.v)/self.M

        # distance
        ds = self.v*dt + 0.5*self.dv*(dt**2)
        dtheta = self.w*dt + 0.5*self.dw*(dt**2)

        # update state
        self.x = self.x + ds*math.cos(self.theta)
        self.y = self.y + ds*math.sin(self.theta)
        self.theta = wrap2pi(self.theta + dtheta)

        # update velocity
        self.w = self.w + self.dw*dt
        self.v = self.v + self.dv*dt

    def rotate(self):
        """Rotate an image while keeping its center and size
        """
        orig_rect = self.image.get_rect()
        rot_image = pygame.transform.rotate(self.image, self.theta_degrees)
        rot_rect = orig_rect.copy()
        rot_rect.center = rot_image.get_rect().center
        rot_image = rot_image.subsurface(rot_rect).copy()
        return rot_image

    def blit(self, screen, action):
        """Render car in pygame display
        """
        self.img = self.rotate()
        self.img_mask = pygame.mask.from_surface(self.img)
        self.img_rect = self.img.get_rect()
        screen.blit(self.img, self.to_screen_coords[:2])
        self.step(action)

# ==============================================
# Game Info class
# ==============================================
class Info:
    def __init__(self, font_size=24):
        self.font = pygame.font.SysFont("monospace", font_size, bold=True)

    def set_sensors(self, sensors):
        self.sensors = sensors

    def process_values(self, v):
        subtext = None

        if type(v) == list or type(v) == np.ndarray:
            subtext = " {:.3f} " * len(v)
            subtext = subtext.format(*v)
        elif type(v) == int or type(v) == float or isinstance(v, np.float64):
            subtext = " {:.3f} "
            subtext = subtext.format(v)
        elif type(v) == bool or type(v) == str:
            subtext = " {} "
            subtext = subtext.format(v)
        else:
            print(type(v))
            raise NotImplementedError

        return subtext

    def blit_info(self, name, value):
        subtext = self.process_values(value)
        sensor_text = "{}:".format(name) + subtext
        label = self.font.render(sensor_text, 1, RED)
        label_size = self.font.size(sensor_text)
        return subtext, sensor_text, label, label_size

    def blit(self, screen, info):
        width, height = screen.get_size()
        num_keys = len(info)

        for i, (k, v) in enumerate(info.items()):
            subtext = self.process_values(v)
            text = "{}:".format(k) + subtext
            label = self.font.render(text, 1, RED)
            label_size = self.font.size(text)
            screen.blit(label, (width - label_size[0], height - label_size[1] * (num_keys - i)))

        if hasattr(self, 'sensors'):
            num_sensors = len(self.sensors)
            for i, sensor in enumerate(self.sensors._sensors):
                subtext, sensor_text, label, label_size = self.blit_info(sensor.name, sensor.text)
                screen.blit(label, (width - label_size[0], label_size[1] * i))

# ==============================================
# Sensor base class
# ==============================================
from abc import ABCMeta, abstractmethod

class Sensor(object):
    def __init__(self, car, road):
        self.car = car
        self.road = road

    @abstractmethod
    def measure(self):
        pass

    @abstractmethod
    def blit(self, screen):
        pass

    @property
    def text(self): return self.measurement

# ==============================================
# Sensor for computing distance from car to center of lane
# ==============================================
class CenterLaneSensor(Sensor):
    def __init__(self, _id, car, road, oriented_curve, num_future_info=0):
        super(CenterLaneSensor, self).__init__(car, road)
        self.name = 'CenterLaneSensor' + str(_id)
        self.oriented_curve = oriented_curve
        self.win = 5
        self.num_future_info = num_future_info
        self.measure()

    def measure(self):
        car_center = np.array(self.car.center)[np.newaxis, :]
        mask = pygame.surfarray.array2d(self.road.center_lane)
        coords = np.array(mask.nonzero()).transpose()

        distances = sp.distance.cdist(car_center, coords)
        point = coords[distances.argmin()]
        dist = distances.min()
        import copy
        dist_unsign = copy.deepcopy(dist)

        screen_point = to_screen_coords(point)
        self.visual_closest_point = point

        # compute the relative pos of car wrt. lane
        # determine the car relative pos
        cross_screen_coord, indx = self.measure_car_relativepos_lane()
        if cross_screen_coord > 0: # car on the left lane
            dist *= -1

        # compute the coords of cloest point in car frame
        # compute the angle difference first wrt. car heading direction
        angle_diff_unsign, cross_car_coord = self.measure_angle_diff_unsign(indx)
        # compute the x and y pos in car frame
        if cross_car_coord > 0: # center in left of car
            ypos = dist_unsign*math.cos(angle_diff_unsign)
            xpos = -1*dist_unsign*math.sin(angle_diff_unsign)
        else: # center in the right of car
            ypos = dist_unsign*math.cos(angle_diff_unsign)
            xpos = dist_unsign*math.sin(angle_diff_unsign)
        center_pos_car_coord = [xpos, ypos]

        # closest distance
        self.closest_point = center_pos_car_coord

        # get the future lane pos information
        future_center_pos_car_coord = self.measure_future_lane_pos(indx)

        # measurement
        # self.measurement=[dist, *future_center_pos_car_coord]
        self.measurement = [dist, *center_pos_car_coord, *future_center_pos_car_coord]
        return center_pos_car_coord, dist

    def measure_future_lane_pos(self, indx):
        future_center_pos_car_coord = [] # in car frame

        # get car position and vector
        car_center = np.array(self.car.center)[np.newaxis, :]
        # car_screen_coord = to_screen_coords(car_center)
        # car vector
        car_theta_rads = self.car.theta
        car_vec = np.array([math.cos(car_theta_rads), math.sin(car_theta_rads)])
        # car vector normal direction vector
        u1 = car_vec / np.linalg.norm(car_vec)
        # Append the future lane center points information
        for i in range(self.num_future_info):
            # get the future point
            point = self.oriented_curve[wrap(self.oriented_curve, indx+i*5+1, 0)]
            # get distance
            dist_unsign = sp.distance.cdist(car_center, np.expand_dims(point, axis=0)).min()
            # get unsigned angle difference wrt. car
            angle_diff_unsign, cross_car_coord = self.measure_angle_diff_unsign(indx+i*5+1)
            # compute the x and y pos in car frame
            if cross_car_coord > 0: # center in left of car
                ypos = dist_unsign*math.cos(angle_diff_unsign)
                xpos = -1*dist_unsign*math.sin(angle_diff_unsign)
            else: # center in the right of car
                ypos = dist_unsign*math.cos(angle_diff_unsign)
                xpos = dist_unsign*math.sin(angle_diff_unsign)
            center_pos_car_coord = [xpos, ypos]
            future_center_pos_car_coord.extend(center_pos_car_coord)

        return future_center_pos_car_coord

    def measure_car_relativepos_lane(self):
        # find closest point on oriented curve
        car_center = np.array(self.car.center)[np.newaxis, :]
        distances = sp.distance.cdist(car_center, self.oriented_curve).squeeze(0)
        indx = distances.argmin()
        # get closest point coordinates
        p1 = self.oriented_curve[wrap(self.oriented_curve, indx, -self.win)]
        p2 = self.oriented_curve[wrap(self.oriented_curve, indx, self.win)]
        # to world coordinates
        pos1 = to_screen_coords(p1)
        pos2 = to_screen_coords(p2)
        # road vec and center2car vec
        center2car = to_screen_coords(car_center.squeeze()) - to_screen_coords(self.oriented_curve[wrap(self.oriented_curve, indx, 0)])
        road_vec = pos2 - pos1
        # compute cross product of road vec and center2car vec
        cross = road_vec[0]*center2car[1] - center2car[0]*road_vec[1]
        # cross > 0: car is on left, cross < 0: car is on right
        return cross, indx

    def measure_angle_diff_unsign(self, indx):
        # cloest center of lane lane direction vector
        car_center = np.array(self.car.center)[np.newaxis, :]

        # car to lane center vector
        car2center = to_screen_coords(self.oriented_curve[wrap(self.oriented_curve, indx, 0)]) - to_screen_coords(car_center.squeeze())

        # car vector
        car_theta_rads = self.car.theta
        car_vec = np.array([math.cos(car_theta_rads), math.sin(car_theta_rads)])

        # compute angle between two vectors
        u1 = car_vec / np.linalg.norm(car_vec)
        u2 = car2center / np.linalg.norm(car2center)
        angle_diff = np.arccos(np.dot(u2, u1)) # in radians

        # compute left/right of center point in car frame
        # cross < 0: center in right, corss > 0: center in left
        cross = u1[0]*u2[1] - u1[1]*u2[0]

        return angle_diff, cross

    @property
    def text(self): return f"P: {self.closest_point[0]:.2f} {self.closest_point[1]:.2f}, D: {round(self.measurement[0], 3)}"

    def blit(self, screen):
        pygame.draw.circle(screen, BLUE, tuple(self.visual_closest_point), 10)

# ==============================================
# Sensor for computing lane direction
# ==============================================
class LaneDirectionSensor(Sensor):
    def __init__(self, _id, car, road, oriented_curve, num_future_info=0):
        super().__init__(car, road)
        self.name = 'LaneDirectionSensor'
        self.oriented_curve = oriented_curve
        self.num_future_info = num_future_info
        self.win = 5
        self.relative_pos = 1 # relative position from car to road, 1 for right of lane, -1 for left
        self.measure()

    def measure(self):
        lane_dir, indx, cross = self.measure_lane_direction()

        # update the relative position between car and lane
        if cross > 0:
            self.relative_pos = -1 # car on left indicator
        else:
            self.relative_pos = 1 # car on right indicator

        angle_diff = self.measure_angle_diff()

        self.measurement = [angle_diff]

        if self.num_future_info > 0:
            self.measurement.extend(self.add_future_info(indx))

        return self.measurement

    def measure_lane_direction(self):
        car_center = np.array(self.car.center)[np.newaxis, :]
        mask = pygame.surfarray.array2d(self.road.center_lane)
        distances = sp.distance.cdist(car_center, self.oriented_curve).squeeze(0)
        indx = distances.argmin()

        p1 = self.oriented_curve[wrap(self.oriented_curve, indx, -self.win)]
        p2 = self.oriented_curve[wrap(self.oriented_curve, indx, self.win)]

        self.p1 = p1
        self.p2 = p2

        pos1 = to_screen_coords(p1)
        pos2 = to_screen_coords(p2)
        road_dir_angle = vec2angle(pos2 - pos1)

        center2car = to_screen_coords(car_center.squeeze()) - to_screen_coords(self.oriented_curve[indx])
        road_vec = pos2 - pos1
        # compute cross product of road vec and center2car vec
        cross = road_vec[0]*center2car[1] - center2car[0]*road_vec[1]

        return road_dir_angle, indx, cross

    def measure_angle_diff(self):
        car_theta_rads = self.car.theta
        car_vec = np.array([math.cos(car_theta_rads), math.sin(car_theta_rads)])
        import copy
        p1, p2 = copy.deepcopy(self.p1), copy.deepcopy(self.p2)
        p2[1] = SCREEN_HEIGHT - p2[1]
        p1[1] = SCREEN_HEIGHT - p1[1]
        road_vec = np.array(p2 - p1)

        # compute angle difference between two vectors
        u1 = car_vec / np.linalg.norm(car_vec)
        u2 = road_vec / np.linalg.norm(road_vec)
        angle_diff = np.arccos(np.dot(u1, u2)) # in radians

        # compute the signed angle difference
        cross = u1[0]*u2[1] - u1[1]*u2[0] # u1 cross u2
        if cross < 0:
            angle_diff *= -1

        return angle_diff

    def add_future_info(self, indx):
        future_dir_angle = [] # in car frame

        # car vector
        car_theta_rads = self.car.theta
        car_vec = np.array([math.cos(car_theta_rads), math.sin(car_theta_rads)])
        # car vector normal direction vector
        u1 = car_vec / np.linalg.norm(car_vec)
        # Append the future lane curvature information
        for i in range(self.num_future_info):
            p1 = self.oriented_curve[wrap(self.oriented_curve, indx+i*5+1, -self.win)]
            p2 = self.oriented_curve[wrap(self.oriented_curve, indx+i*5+1, self.win)]
            pos1 = to_screen_coords(p1)
            pos2 = to_screen_coords(p2)
            # road vector
            road_vec_tmp = np.array(pos2 - pos1)
            # compute angle difference between two vectors
            u2 = road_vec_tmp / np.linalg.norm(road_vec_tmp)
            angle_diff_tmp = np.arccos(np.dot(u1, u2)) # in radians
            # compute the signed angle difference
            cross = u1[0]*u2[1] - u1[1]*u2[0] # u1 cross u2
            if cross < 0: # negative means road dir on the right half plane (clockwise)
                angle_diff_tmp *= -1

            future_dir_angle.append(angle_diff_tmp)

        return future_dir_angle

    def blit(self, screen):
        pygame.draw.circle(screen, BLACK, tuple(self.p1), 2)
        pygame.draw.circle(screen, RED, tuple(self.p2), 2)

    @property
    def text(self):
        angle_diff = self.measurement[0]

        side = "left" if self.relative_pos < 0 else "right"
        return f"angle_diff: {angle_diff:.2f}, side: {side}"

# ==============================================
# Sensor for computing lane curvature
# ==============================================
class LaneCurvatureSensor(Sensor):
    def __init__(self, _id, car, road):
        super(LaneCurvatureSensor, self).__init__(car, road)
        self.name = 'LaneCurvatureSensor' + str(_id)
        self.measure()

    def measure(self):
        car_center = np.array(self.car.center)[np.newaxis, :]

        mask = pygame.surfarray.array2d(self.road.center_lane)
        coords = np.array(mask.nonzero()).transpose()

        import scipy.spatial as sp

        distances = sp.distance.cdist(car_center, coords).squeeze(0)

        closest_point = coords[distances.argmin()]
        x, y = closest_point

        win_size = 50

        # Take a region around the closest point
        window = mask[x-win_size:x+win_size, y-win_size:y+win_size]

        from itertools import product
        prod = np.array(list(product(range(x-win_size,x+win_size), range(y-win_size,y+win_size))))
        coords = prod[window.flatten().nonzero()]
        self.coords = coords

        # ==============================================
        # Some code to compute lane curvature
        # ==============================================

        dx_dt = np.gradient(coords[:, 0])
        dy_dt = np.gradient(coords[:, 1])
        velocity = np.stack((dx_dt, dy_dt),axis=1)

        ds_dt = np.sqrt(dx_dt**2 + dy_dt**2)
        tangent = np.array([1/ds_dt] * 2).transpose() * velocity

        tangent_x = tangent[:, 0]
        tangent_y = tangent[:, 1]

        deriv_tangent_x = np.gradient(tangent_x)
        deriv_tangent_y = np.gradient(tangent_y)

        dT_dt = np.stack((deriv_tangent_x, deriv_tangent_y),axis=1)

        length_dT_dt = np.sqrt(deriv_tangent_x ** 2 + deriv_tangent_y **2)

        normal = np.array([1/length_dT_dt] * 2).transpose() * dT_dt

        d2s_dt2 = np.gradient(ds_dt)
        d2x_dt2 = np.gradient(dx_dt)
        d2y_dt2 = np.gradient(dy_dt)

        # use formula for curvature: |x'' y' - x'y''| / (x'^2 + y'^2) ^(3/2)
        curvature = np.abs(d2x_dt2 * dy_dt - dx_dt * d2y_dt2) / (dx_dt * dx_dt + dy_dt * dy_dt)**(3/2)

        t_component = np.array([d2s_dt2] * 2).transpose()
        n_component = np.array([curvature * ds_dt * ds_dt] * 2).transpose()

        acceleration = t_component * tangent + n_component * normal

        self.measurement = curvature[int(len(curvature) / 2)]
        return self.measurement

    def blit(self, screen):
        for point in self.coords:
            pygame.draw.circle(screen, ORANGE, tuple(point), 2)

# ==============================================
# Sensor ray trace distance to edge of the road
# ==============================================
class RangeSensor(Sensor):
    def __init__(self, _id, car, road, deg):
        super(RangeSensor, self).__init__(car, road)
        self.deg = deg
        self.name = 'RangeSensor' + str(_id)
        self.measure()

    def measure(self):
        car_center = self.car.center

        length = 0
        for i in range(1500):
            self.deg_x = car_center[0] + math.cos(math.radians(self.deg + self.car.theta_degrees)) * length
            self.deg_y = car_center[1] - math.sin(math.radians(self.deg + self.car.theta_degrees)) * length

            line_surface = pygame.Surface((2, 2), pygame.SRCALPHA)
            line_rect = line_surface.get_rect()
            line_rect.topleft = self.deg_x, self.deg_y
            line_surface.fill((255,0,0))
            if self.road.field_mask.overlap(pygame.mask.from_surface(line_surface),
                                             (int(line_rect[0]), int(line_rect[1]))) is not None:
                self.measurement = length
                break
            length += 1

    def blit(self, screen):
        area_clear = 200
        if self.measurement > area_clear:
            red = 0
            green = 255
        else:
            ratio = self.measurement / area_clear
            value = 510 * ratio
            if value <= 255:
                red = 255
                green = value
            else:
                red = 255 - (value - 255)
                green = 255

        pygame.draw.line(screen, (int(red), int(green), 0), tuple(self.car.center), (self.deg_x, self.deg_y), 2)

class Sensors:
    def __init__(self, car, road, state_sources=['lane_direction', 'center_lane'], num_future_info=0):
        self.car = car
        self.road = road
        self.state_sources = state_sources
        self._sensors = []
        self.num_range_sensors = 0
        self.range_sensors = []
        range_sensor = list(filter(lambda x: bool(re.match('range_.*', x)), self.state_sources))

        oriented_curve = self.contour_tracing()

        if range_sensor:
            self.num_range_sensors = int(range_sensor[0].split('_')[-1])

            self.density = ANGLE_BETWEEN_SENSORS
            self.range_sensors = self.init_range_sensors()
            self._sensors.extend(self.range_sensors)

        if 'center_lane' in self.state_sources:
            self.center_lane_sensor = CenterLaneSensor(0, self.car, self.road, oriented_curve, num_future_info)
            self._sensors.append(self.center_lane_sensor)

        if 'lane_curvature' in self.state_sources:
            self.lane_curvature_sensor = LaneCurvatureSensor(0, self.car, self.road)
            self._sensors.append(self.lane_curvature_sensor)

        if 'lane_direction' in self.state_sources:
            self.lane_direction_sensor = LaneDirectionSensor(0, self.car, self.road, oriented_curve, num_future_info)
            self._sensors.append(self.lane_direction_sensor)

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

    def contour_tracing(self):
        b = []
        self.mask = pygame.surfarray.array2d(self.road.center_lane)
        starting_pixel = list(zip(*np.where(self.mask != 0)))
        b_x = starting_pixel[0][0]
        b_y = starting_pixel[0][1]
        b.append(starting_pixel[0])

        b_x = starting_pixel[0][0]
        b_y = starting_pixel[0][1]-1

        d_x = b_x - b_x
        d_y = b_y - b_y

        neighbors = [(0,0),(0,-1),(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1)]
        count = 1
        # contour = np.zeros((*img.shape, 3))
        while count <= 2:
            tmp = 0
            for i in range(9):
                if d_x == neighbors[i][0] and d_y == neighbors[i][1]:
                    tmp = i
                    break

            while True:
                if(tmp == 8):
                    tmp = 0

                c_x = b_x + neighbors[tmp+1][0]
                c_y = b_y + neighbors[tmp+1][1]
                if (self.mask[c_x][c_y]!=0):
                    b_x = c_x
                    b_y = c_y
                    temp = (b_x,b_y)

                    if (temp in b)==True:
                        count += 1
                    b.append((b_x,b_y))
                    d_x = prev_x-b_x
                    d_y = prev_y-b_y
                    break

                prev_x = c_x
                prev_y = c_y
                tmp += 1
        return np.array(b)
        # for pixel in range(len(B)):
        #       contour[B[pixel][0]][B[pixel][1]] = [0,255,0]

    @property
    def sensors(self):
        return self._sensors

    def init_range_sensors(self):
        sensors = []
        angle_range = (self.num_range_sensors - 1) * self.density
        start_angle = -angle_range // 2

        for i, deg in enumerate(range(start_angle + angle_range, start_angle - 1, -self.density)):
            sensors.append(RangeSensor(i, self.car, self.road, deg))
        return sensors

    def blit(self, screen):
        for sensor in self.sensors:
            sensor.measure()
            sensor.blit(screen)

# ==============================================
# Road Class
# ==============================================
class Road:
    def __init__(self,
        road_file,
        center_lane_file,
        textures,
        texture_files,
        texture_frictions,
        use_textures=False
    ):
        self.img = pygame.image.load(road_file)
        self.img = pygame.transform.smoothscale(self.img, (SCREEN_WIDTH, SCREEN_HEIGHT))
        self.field_mask = pygame.mask.from_threshold(self.img, self.img.get_at((0,0)), (2, 2, 2, 255))
        self.img_rect = self.img.get_rect()

        # ==============================================
        # Load in different textures for the road
        # ==============================================
        self.texture_map = {}
        self.use_textures = use_textures

        if self.use_textures:
            for i, file in enumerate(texture_files):
                texture = pygame.image.load(file)
                texture = pygame.transform.smoothscale(texture, (SCREEN_WIDTH, SCREEN_HEIGHT))
                texture_mask = pygame.mask.from_surface(texture)

                self.texture_map[textures[i]] = dict(
                    texture=texture,
                    mask=texture_mask,
                    friction_level=texture_frictions[i]
                )

        # ==============================================
        # Load center lane
        # ==============================================
        self.center_lane = pygame.image.load(center_lane_file)
        self.center_lane_mask = pygame.mask.from_surface(self.center_lane)

    def blit(self, screen):
        screen.blit(self.center_lane, (0, 0))

        # Render track first
        screen.blit(self.img, (0, 0))

        # Render textures on top
        for texture, dic in self.texture_map.items():
            screen.blit(dic['texture'], (0, 0))

{'sp': 'scipy.spatial', 'product': 'itertools.product'}


from mvnc import mvncapi as mvnc
from imutils.video import VideoStream
from imutils.video import FPS
import argparse
import numpy as np
import time
import cv2

# class labels
CLASSES = ("background", "aeroplane", "bicycle", "bird",
	"boat", "bottle", "bus", "car", "cat", "chair", "cow",
	"diningtable", "dog", "horse", "motorbike", "person",
	"pottedplant", "sheep", "sofa", "train", "tvmonitor")
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))

# Configuration
GRAPH = "graphs/mobilenetgraph"
CONFIDENCE = 0.2
DISPLAY=1
PREPROCESS_DIMS = (300, 300)

def predict(image, graph):
	# preprocess the image
	preprocessed = cv2.resize(image, PREPROCESS_DIMS)
	preprocessed = preprocessed - 127.5
	preprocessed = preprocessed * 0.007843
	image = preprocessed.astype(np.float16)

	# send the image to the NCS and get network predictions
	graph.LoadTensor(image, None)
	(output, _) = graph.GetResult()

	# number of valid object predictions
	num_valid_boxes = output[0]
	predictions = []

	# looping over results
	for box_index in range(num_valid_boxes):
		# calculate the base index
		base_index = 7 + box_index * 7

		# boxes with non-finite (inf, nan, etc) numbers must be ignored
		if (not np.isfinite(output[base_index]) or
			not np.isfinite(output[base_index + 1]) or
			not np.isfinite(output[base_index + 2]) or
			not np.isfinite(output[base_index + 3]) or
			not np.isfinite(output[base_index + 4]) or
			not np.isfinite(output[base_index + 5]) or
			not np.isfinite(output[base_index + 6])):
			continue

		# extract the image width and height
		(h, w) = image.shape[:2]
		x1 = max(0, int(output[base_index + 3] * w))
		y1 = max(0, int(output[base_index + 4] * h))
		x2 = min(w,	int(output[base_index + 5] * w))
		y2 = min(h,	int(output[base_index + 6] * h))

		# predicted class label, confidence,and bounding box (x, y)
		pred_class = int(output[base_index + 1])
		pred_conf = output[base_index + 2]
		pred_boxpts = ((x1, y1), (x2, y2))

		# create prediciton tuple
		prediction = (pred_class, pred_conf, pred_boxpts)
		predictions.append(prediction)

	# return the list of predictions
	return predictions

# finding NCS device
devices = mvnc.EnumerateDevices()

# if no devices found, exit the script
if len(devices) == 0:
	print("No devices found.")
	quit()

device = mvnc.Device(devices[0])
device.OpenDevice()

# open the graph file
with open(GRAPH, mode="rb") as f:
	graph_in_memory = f.read()

# load the graph into the NCS
graph = device.AllocateGraph(graph_in_memory)

# For Laptop camera
vs = VideoStream(src=0).start()

# For Pi camera
# vs = VideoStream(usePiCamera=True).start()

time.sleep(1)
fps = FPS().start()

# looping over the frames from the video stream
while True:
	try:
		# grab the frame
		frame = vs.read()
		image_for_result = frame.copy()

		# use the NCS to acquire predictions
		predictions = predict(frame, graph)

		# looping over our predictions
		for (i, pred) in enumerate(predictions):
			# prediction data
			(pred_class, pred_conf, pred_boxpts) = pred

			# detections greater than confidence
			if pred_conf > CONFIDENCE:
				if DISPLAY == 1:

					# draw the prediction
					label = "{}: {:.2f}%".format(CLASSES[pred_class],
						pred_conf * 100)

					# extract boxpoints
					(ptA, ptB) = (pred_boxpts[0], pred_boxpts[1])
					(startX, startY) = (ptA[0], ptA[1])
					y = startY - 15 if startY - 15 > 15 else startY + 15

					# display the rectangle and label text
					cv2.rectangle(image_for_result, ptA, ptB,
						COLORS[pred_class], 2)
					cv2.putText(image_for_result, label, (startX, y),
						cv2.FONT_HERSHEY_SIMPLEX, 1, COLORS[pred_class], 3)
				if DISPLAY == 0:
					print("Prediction #{}: class={}, confidence={}, "
					"boxpoints={}".format(i, CLASSES[pred_class], pred_conf,
					pred_boxpts))

		# check if we should display the frame on the screen
		if DISPLAY == 1:
			cv2.imshow("Output", image_for_result)
			key = cv2.waitKey(1) & 0xFF

			# if 'q' pressed break from the loop
			if key == ord("q"):
				break

		# update the FPS counter
		fps.update()

	# if "ctrl+c" is pressed in the terminal, break from the loop
	except KeyboardInterrupt:
		break

	# if problem reading a frame
	except AttributeError:
		break

# display FPS information
fps.stop()
print("elapsed time: {:.2f}".format(fps.elapsed()))
print("approx. FPS: {:.2f}".format(fps.fps()))

graph.DeallocateGraph()
device.CloseDevice()

cv2.destroyAllWindows()
vs.stop()


import sys
sys.path.append('.')

import os
import pickle
import numpy as np
import torch
from torch import nn
import time
import soundfile as sf
from tqdm import tqdm
import librosa
from sklearn.metrics import f1_score

from python.processing.stft import stft
from python.processing.target import clean_speech_VAD, clean_speech_IBM
from python.models.models import Classifier
#from utils import count_parameters

from python.visualization import display_multiple_signals

# Dataset
dataset_name = 'CSR-1-WSJ-0'
if dataset_name == 'CSR-1-WSJ-0':
    from python.dataset.csr1_wjs0_dataset import speech_list, read_dataset

# Settings
dataset_type = 'test'

dataset_size = 'subset'
#dataset_size = 'complete'

# Labels
# labels = 'labels'
labels = 'vad_labels'

# System
cuda = torch.cuda.is_available()
cuda_device = "cuda:0"
device = torch.device(cuda_device if cuda else "cpu")

# Parameters
## STFT
fs = int(16e3) # Sampling rate
wlen_sec = 64e-3 # window length in seconds
hop_percent = 0.25  # hop size as a percentage of the window length
win = 'hann' # type of window
dtype = 'complex64'

## IBM
quantile_fraction = 0.999
quantile_weight = 0.999

## Plot spectrograms
vmin = -40 # in dB
vmax = 20 # in dB
xticks_sec = 2.0 # in seconds
fontsize = 30

## Classifier
if labels == 'labels':
    # classif_name = 'classif_normdataset_hdim_128_128_end_epoch_100/Classifier_epoch_096_vloss_57.53'
    # x_dim = 513 # frequency bins (spectrogram)
    # y_dim = 513
    # h_dim_cl = [128, 128]
    # std_norm = True
    # eps = 1e-8

    # classif_name = 'classif_hdim_128_128_end_epoch_100/Classifier_epoch_096_vloss_71.65'
    # x_dim = 513 # frequency bins (spectrogram)
    # y_dim = 513
    # h_dim_cl = [128, 128]
    # std_norm = False
    # eps = 1e-8

    classif_name = 'classif_batchnorm_before_hdim_128_128_end_epoch_100/Classifier_epoch_096_vloss_59.58'
    x_dim = 513 # frequency bins (spectrogram)
    y_dim = 513
    h_dim_cl = [128, 128]
    batch_norm = True
    std_norm = False
    eps = 1e-8

if labels == 'vad_labels':
    classif_name = 'classif_VAD_normdataset_hdim_128_128_end_epoch_100/Classifier_epoch_096_vloss_0.21'
    x_dim = 513 # frequency bins (spectrogram)
    y_dim = 1
    h_dim_cl = [128, 128]
    std_norm = True
    batch_norm = False
    eps = 1e-8

# Data directories
input_speech_dir = os.path.join('data',dataset_size,'raw/')
processed_data_dir = os.path.join('data',dataset_size,'processed/')
classif_dir = os.path.join('models', classif_name + '.pt')
classif_data_dir = 'data/' + dataset_size + '/models/' + classif_name + '/'
output_data_dir = os.path.join('data', dataset_size, 'models', classif_name + '/')

# Data normalization
if std_norm:
    # Load mean and variance
    mean = np.load(os.path.dirname(classif_dir) + '/' + 'trainset_mean.npy')
    std = np.load(os.path.dirname(classif_dir) + '/' + 'trainset_std.npy')

    mean = torch.tensor(mean).to(device)
    std = torch.tensor(std).to(device)

def main():
    # Load input SNR
    all_snr_db = read_dataset(processed_data_dir, dataset_type, 'snr_db')
    all_snr_db = np.array(all_snr_db)

    device = torch.device("cuda" if cuda else "cpu")
    file = open('output.log','w')

    print('Torch version: {}'.format(torch.__version__))
    print('Device: %s' % (device))
    if torch.cuda.device_count() >= 1: print("Number GPUs: ", torch.cuda.device_count())

    # Create file list
    file_paths = speech_list(input_speech_dir=input_speech_dir,
                             dataset_type=dataset_type)

    classifier = Classifier([x_dim, h_dim_cl, y_dim], batch_norm=batch_norm)
    classifier.load_state_dict(torch.load(classif_dir, map_location=cuda_device))
    if cuda: classifier = classifier.cuda()

    classifier.eval()
    for param in classifier.parameters():
        param.requires_grad = False

    # Create file list
    file_paths = speech_list(input_speech_dir=input_speech_dir,
                             dataset_type=dataset_type)

    for i, file_path in tqdm(enumerate(file_paths)):

        # Read files
        s_t, fs_s = sf.read(processed_data_dir + os.path.splitext(file_path)[0] + '_s.wav') # clean speech
        x_t, fs_x = sf.read(processed_data_dir + os.path.splitext(file_path)[0] + '_x.wav') # mixture

        # x = x/np.max(x)
        T_orig = len(x_t)

        # TF representation
        # Input should be (frames, freq_bibs)
        x_tf = stft(x_t,
                 fs=fs,
                 wlen_sec=wlen_sec,
                 win=win,
                 hop_percent=hop_percent,
                 dtype=dtype)

        # Transpose to match PyTorch
        x_tf = x_tf.T # (frames, freq_bins)

        # Power spectrogram (transpose)
        x = torch.tensor(np.power(np.abs(x_tf), 2)).to(device)

        # Normalize power spectrogram
        if std_norm:
            x -= mean.T
            x /= (std + eps).T

        # Classify
        y_hat_soft = classifier(x)
        y_hat_hard = (y_hat_soft > 0.5).int()
        y_hat_hard = y_hat_hard.cpu().numpy()
        y_hat_hard = y_hat_hard.T  # Transpose to match librosa.display

        # plots of target / estimation
        s_tf = stft(s_t,
                 fs=fs,
                 wlen_sec=wlen_sec,
                 win=win,
                 hop_percent=hop_percent,
                 dtype=dtype) # shape = (freq_bins, frames)

        if labels == 'labels':
            # binary mask
            target = clean_speech_IBM(s_tf,
                                    quantile_fraction=quantile_fraction,
                                    quantile_weight=quantile_weight)

        if labels == 'vad_labels':
            # vad
            target = clean_speech_VAD(s_tf,
                                    quantile_fraction=quantile_fraction,
                                    quantile_weight=quantile_weight)

        # Transpose to match librosa.display
        x_tf = x_tf.T

        # F1-score
        f1score_s_hat = f1_score(target.flatten(), y_hat_hard.flatten(), average="binary")

        ## mixture signal (wav + spectro)
        ## target signal (wav + spectro + mask)
        ## estimated signal (wav + spectro + mask)
        signal_list = [
            [x_t, x_tf, None], # mixture: (waveform, tf_signal, no mask)
            [s_t, s_tf, target], # clean speech
            [None, None, y_hat_hard]
            #[None, None, y_hat_soft]
        ]
        #TODO: modify
        fig = display_multiple_signals(signal_list,
                            fs=fs, vmin=vmin, vmax=vmax,
                            wlen_sec=wlen_sec, hop_percent=hop_percent,
                            xticks_sec=xticks_sec, fontsize=fontsize)

        # put all metrics in the title of the figure
        title = "Input SNR = {:.1f} dB \n" \
            "F1-score = {:.3f} \n".format(all_snr_db[i], f1score_s_hat)

        fig.suptitle(title, fontsize=40)

        # Save figure
        output_path = classif_data_dir + file_path
        output_path = os.path.splitext(output_path)[0]

        if not os.path.exists(os.path.dirname(output_path)):
            os.makedirs(os.path.dirname(output_path))

        fig.savefig(output_path + '_hard_mask.png')

if __name__ == '__main__':
    main()


from typing import List

import requests
import collections

Pokemon = collections.namedtuple('Pokemon', 'name, weight')

def find_pokemon_from_name(id: str) -> List[Pokemon]:
    url = f'https://pokeapi.co/api/v2/pokemon-species/{id}'

    resp = requests.get(url)
    resp.raise_for_status()

    results = resp.json()

    information = results['flavor_text_entries']

    print(results['name'])
    print(information[0])


import numpy as np
import cv2
import os
import imutils
from pathlib import Path
from skimage.segmentation import clear_border

import sys
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"

def get_app_dir() -> Path:
    """ return absolute directory path to the application base directory"""
    app_dir = Path.cwd()

    if not hasattr(sys, "frozen"):
        i = 0
        while app_dir.name not in ["src", "Sudoku_papa"] and i < 50:
            app_dir = app_dir.parent
            i += 1
        if i == 50:
            raise Exception("Application dir can´t be determined")

        if app_dir.name == "src":
            app_dir = app_dir.parent

        return app_dir

def grid_capture(framename, file_path):
    # vidéo capture source camera (ici la webcam du portable)
    cap = cv2.VideoCapture(0)

    # return a single frame in variable `frame`
    while(True):
        result,frame = cap.read()
        cv2.imshow(framename, frame) #display the captured image

        if cv2.waitKey(1) & 0xFF == ord('s'):
            cv2.imwrite(file_path, frame)

        if cv2.waitKey(1) & 0xFF == ord('q'): #save on pressing 'y'
            cv2.destroyAllWindows()
            break

    cap.release()

def visualise_image(file_path: Path, show=False):
    img = cv2.imread (str(file_path))
    if show:
        while True:
            cv2.namedWindow('Capture Sudoku', cv2.WINDOW_NORMAL)
            cv2.imshow('Capture Sudoku', img)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                cv2.destroyAllWindows()
                break
    return img

def resize_image(src, factor=0.7, save=True):
    w, h, d = src.shape
    # new_size = int(min(w, h) * factor)
    # new_dim = (new_size, new_size)
    resized = imutils.resize(src, width=600, height=600)
    # resized = cv2.resize(src, new_dim, interpolation=cv2.INTER_AREA)
    if save:
        app_dir = get_app_dir()
        cv2.imwrite(str(app_dir / 'Images' / 'resized_sudoku.png'), resized)
    return resized

def order_points(pts):
    # initialzie a list of coordinates that will be ordered
    # such that the first entry in the list is the top-left,
    # the second entry is the top-right, the third is the
    # bottom-right, and the fourth is the bottom-left
    rect = np.zeros((4, 2), dtype = "float32")
    # the top-left point will have the smallest sum, whereas
    # the bottom-right point will have the largest sum
    s = pts.sum(axis = 1)
    rect[0] = pts[np.argmin(s)]
    rect[2] = pts[np.argmax(s)]
    # now, compute the difference between the points, the
    # top-right point will have the smallest difference,
    # whereas the bottom-left will have the largest difference
    diff = np.diff(pts, axis = 1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]
    # return the ordered coordinates
    return rect

def four_point_transform(src, pts):
    # obtain a consistent order of the points and unpack them
    # individually
    rect = order_points(pts)
    (tl, tr, br, bl) = rect
    # compute the width of the new image, which will be the
    # maximum distance between bottom-right and bottom-left
    # x-coordiates or the top-right and top-left x-coordinates
    widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
    widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
    maxWidth = max(int(widthA), int(widthB))
    # compute the height of the new image, which will be the
    # maximum distance between the top-right and bottom-right
    # y-coordinates or the top-left and bottom-left y-coordinates
    heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
    heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
    maxHeight = max(int(heightA), int(heightB))
    # now that we have the dimensions of the new image, construct
    # the set of destination points to obtain a "birds eye view",
    # (i.e. top-down view) of the image, again specifying points
    # in the top-left, top-right, bottom-right, and bottom-left
    # order
    dst = np.array([
        [0, 0],
        [maxWidth - 1, 0],
        [maxWidth - 1, maxHeight - 1],
        [0, maxHeight - 1]], dtype = "float32")
    # compute the perspective transform matrix and then apply it
    M = cv2.getPerspectiveTransform(rect, dst)
    warped = cv2.warpPerspective(src, M, (maxWidth, maxHeight))
    # return the warped image
    return warped

def find_puzzle(file_path, debug=False):

    # read the image
    image = cv2.imread(str(file_path))

    resized_image = resize_image(image)

    # convert in gray
    gray = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)

    # filter noice
    blurred = cv2.GaussianBlur(gray, (7, 7), 3)
    thresh = cv2.adaptiveThreshold(blurred, 255,
                                   cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

    # invert color
    thresh = cv2.bitwise_not(thresh)

    # find contours in the thresholded image and sort them by size in
    # descending order
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)

    # initialize a contour that corresponds to the puzzle outline
    puzzleCnt = None
    # loop over the contours
    for c in cnts:
        # approximate the contour
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        # if our approximated contour has four points, then we can
        # assume we have found the outline of the puzzle
        if len(approx) == 4:
            puzzleCnt = approx
            break

    # if the puzzle contour is empty then our script could not find
    # the outline of the Sudoku puzzle so raise an error
    if puzzleCnt is None:
        raise Exception(("Could not find Sudoku puzzle outline. "
                         "Try debugging your thresholding and contour steps."))

    # apply a four point perspective transform to both the original
    # image and grayscale image to obtain a top-down bird's eye view
    # of the puzzle
    puzzle = four_point_transform(resized_image, puzzleCnt.reshape(4, 2))
    warped = four_point_transform(gray, puzzleCnt.reshape(4, 2))

    if debug:
        # draw the contour of the puzzle on the image and then display
        # it to our screen for visualization/debugging purposes
        cv2.imshow("Puzzle Transform", puzzle)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    return puzzle, warped

def zoom_on_digit(digit: np.array):
    """ Recenter the image on digit """
    # find the 4 points of the ROI
    x_indexes, y_indexes = np.where(digit == 255)
    x_min, x_max = np.min(x_indexes), np.max(x_indexes)
    y_min, y_max = np.min(y_indexes), np.max(y_indexes)

    # slice the array
    return digit[x_min-1:x_max+1,y_min-1:y_max+1]

def extract_digit(cell, debug=False):
    # apply automatic thresholding to the cell and then clear any
    # connected borders that touch the border of the cell
    thresh = cv2.threshold(cell, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
    thresh = clear_border(thresh)

    # check to see if we are visualizing the cell thresholding step
    # find contours in the thresholded cell
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)

    # if no contours were found than this is an empty cell
    if len(cnts) == 0:
        return None

    # otherwise, find the largest contour in the cell and create a
    # mask for the contour
    c = max(cnts, key=cv2.contourArea)
    mask = np.zeros(thresh.shape, dtype="uint8")
    cv2.drawContours(mask, [c], -1, 255, -1)

    # compute the percentage of masked pixels relative to the total
    # area of the image
    (h, w) = thresh.shape
    percentFilled = cv2.countNonZero(mask) / float(w * h)

    # if less than 3% of the mask is filled then we are looking at
    # noise and can safely ignore the contour
    if percentFilled < 0.03:
        return None

    # apply the mask to the thresholded cell
    digit = cv2.bitwise_and(thresh, thresh, mask=mask)

    digit = zoom_on_digit(digit)
    # check to see if we should visualize the masking step
    if debug:
        cv2.imshow("Digit", digit)
        cv2.waitKey(0)

    # return the digit to the calling function
    return digit


import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import copy

import FLAG

class Client:
    def __init__(self, ID, ds, solver, tag=''):
        self.ID = ID
        self.tag = tag

        self.ds = ds
        self.num_samples = len(ds)
        self.solver = solver
        self.loader = DataLoader(self.ds, batch_size=self.solver.batch_size)

        self.random = np.random.RandomState(seed=ID)
        self.server = None
        self.model = None
        self.optimizer = None
        self.lr_scheduler = None

        self.importance_estimated = []

    def attach_to_server(self, server):
        self.server = server
        self.model = copy.deepcopy(server.client_init_model)
        self.model = self.model.to(FLAG.device)
        self.optimizer = self.solver.optimizer_type(params=self.model.parameters(), lr=self.solver.lr)
        self.lr_scheduler = self.solver.lr_scheduler_type(optimizer=self.optimizer,
                                                          step_size=self.solver.lr_step,
                                                          gamma=self.solver.lr_gamma)

    def estimate_importance_weights(self):
        with torch.no_grad():
            table = np.zeros((self.server.num_clusters, self.num_samples))
            start_idx = 0
            nst_cluster_sample_count = [0] * self.server.num_clusters
            sample_loader = DataLoader(self.ds, batch_size=256)
            for x, y in sample_loader:
                x = x.to(FLAG.device)
                y = y.to(FLAG.device)
                for s, cluster in enumerate(self.server.cluster_vec):
                    cluster.eval()
                    out = cluster(x)
                    if self.solver.classification:
                        out = out.view(-1, self.solver.num_classes)
                    elif self.solver.sequence:
                        out = out.view(-1, self.solver.num_tokens)
                        y = y.view(-1)
                    loss = self.solver.estimate_criterion(out, y)

                    table[s][start_idx:start_idx + len(x)] = loss.cpu()
                start_idx += len(x)

            min_loss_idx = np.argmin(table, axis=0)
            for s in range(self.server.num_clusters):
                nst_cluster_sample_count[s] += np.sum(min_loss_idx == s)
            for s in range(self.server.num_clusters):
                if nst_cluster_sample_count[s] == 0:
                    nst_cluster_sample_count[s] = self.solver.count_smoother * self.num_samples
            self.importance_estimated = np.array([1.0 * nst / self.num_samples for nst in nst_cluster_sample_count])

    def get_importance(self, count=True):
        if count:
            return [ust * self.num_samples for ust in self.importance_estimated]
        else:
            return self.importance_estimated

    def get_model_dict(self):
        return copy.deepcopy(self.model.state_dict())

    def run(self):
        self.model.train()
        self._local_train()
        self.model.eval()

    def _local_train(self):
        for _ in range(self.solver.local_epoch):
            for x, y in self.loader:
                x = x.to(FLAG.device)
                y = y.to(FLAG.device)
                out = self.model(x)
                if self.solver.classification:
                    out = out.view(-1, self.solver.num_classes)
                loss = self.solver.criterion(out, y)

                mse_loss = nn.MSELoss(reduction='sum')
                for i, cluster in enumerate(self.server.cluster_vec):
                    l2 = None
                    for (param_local, param_cluster) in zip(self.model.parameters(), cluster.parameters()):
                        if l2 is None:
                            l2 = mse_loss(param_local, param_cluster)
                        else:
                            l2 += mse_loss(param_local, param_cluster)
                    loss += self.solver.reg_weight / 2 * self.importance_estimated[i] * l2

                self.optimizer.zero_grad()
                loss.backward()
                self.optimizer.step()

    def eval(self, model=None):
        if model is None:
            model = self.model
        model.eval()

        loss = 0.
        with torch.no_grad():
            for x, y in self.loader:
                x = x.to(FLAG.device)
                y = y.to(FLAG.device)
                out = model(x)
                loss += self.solver.criterion(out, y).item()
        return loss

    def save(self, path):
        filename = path + "client_" + str(self.ID)
        import pickle
        client_dict = {"ID": self.ID, "tag": self.tag}
        with open(filename + "_dict.pkl", "wb") as f:
            pickle.dump(client_dict, f)
        torch.save(self.ds, filename + "_ds.pth")

    def load(self, path):
        filename = path + "client_" + str(self.ID)
        import pickle
        with open(filename + "_dict.pkl", "rb") as f:
            client_dict = pickle.load(f)
        self.ID = client_dict["ID"]
        self.tag = client_dict["tag"]
        self.ds = torch.load(filename + "_ds.pth")


# -*- coding: utf-8 -*-
import logging
from redis.lock import LuaLock

logger = logging.getLogger(__name__)

class RedisLuaLock(LuaLock):
    @classmethod
    def lua_acquire(cls, keys, args, client):
        args = tuple(keys) + tuple(args)
        return client.eval(cls.LUA_ACQUIRE_SCRIPT, len(keys), *args)

    @classmethod
    def lua_release(cls, keys, args, client):
        args = tuple(keys) + tuple(args)
        try:
            client.eval(cls.LUA_RELEASE_SCRIPT, len(keys), *args)
        except Exception:
            logger.warn("Fail to release redis lua lock")
        return True

    @classmethod
    def lua_extend(cls, keys, args, client):
        args = tuple(keys) + tuple(args)
        return client.eval(cls.LUA_EXTEND_SCRIPT, len(keys), *args)


#!/usr/bin/env python3
import os
import sys
import pprint
import re
import json
import networkx as nx

def usage(exitVal, filename=""):
    print(f"Error: Incorrect usage")
    if exitVal == -2:
        print(f"File {filename} does not exist\n")
    print(f'Usage: {os.path.basename(sys.argv[0])} filename')
    exit(exitVal)

def getAdjList():
    pass

def main():

    f = open("unique_syscalls.txt", "r")
    calls = [line.strip() for line in f.readlines()]

    if len(sys.argv) < 2:
        usage(-1)
    filename = sys.argv[1]
    try:
        adj_file = open(filename, "rb")
    except FileNotFoundError:
        usage(-2, filename)

    G = nx.read_multiline_adjlist(adj_file, create_using=nx.DiGraph)

    # Get pagerank and append to feature vector:
    pr = nx.pagerank(G)

    fv = []
    for call in calls:
        fv.append(pr.get(call, 0))

    # Get average clustering value and append to feature vector:
    fv.append(nx.average_clustering(G, weight='weight'))

    # Get eigenvector centrality values and append all to feature vector:
    ec = nx.eigenvector_centrality(G, weight='weight')
    for call in calls:
        fv.append(ec.get(call, 0))
    print(fv)
    print(len(fv))

if __name__ == '__main__':
    main()


import os

import requests
from pb_portal.connectors.finam import schemas
from loguru import logger

NETLOC = os.environ.get('FINAM_NETLOC') or 'http://127.0.0.1:8000'
TOKEN = os.environ.get('FINAM_TOKEN') or 'pass'

def get_сurrencies() -> schemas.Items:
    with requests.sessions.Session() as session:
        session.auth = ('api', TOKEN)
        resp = session.get(f'{NETLOC}/api/сurrencies')
        if resp.ok:
            logger.debug(resp.content)
            return schemas.Items.parse_raw(resp.content)
        return schemas.Items()

def get_categories() -> schemas.Node:
    with requests.sessions.Session() as session:
        session.auth = ('api', TOKEN)
        resp = session.get(f'{NETLOC}/api/category-tree')
        if resp.ok:
            logger.debug(resp.content)
            return schemas.Node.parse_raw(resp.content)
        return schemas.Node(name='')

def rm_transaction(trans_id):
    with requests.sessions.Session() as session:
        session.auth = ('api', TOKEN)
        resp = session.post(f'{NETLOC}/api/rm_transaction?trans_id={trans_id}')
        if not resp.ok:
            raise ValueError

def post_transaction(transaction: schemas.Transaction):
    with requests.sessions.Session() as session:
        session.auth = ('api', TOKEN)
        resp = session.post(f'{NETLOC}/api/transaction', data=transaction.json())
        logger.debug(resp.content)

def get_page_transactions(data: schemas.GetTransactionPage) -> schemas.TransactionPage:
    with requests.sessions.Session() as session:
        session.auth = ('api', TOKEN)
        resp = session.get(f'{NETLOC}/api/transactions', data=data.json())
        if resp.ok:
            logger.debug(resp.content)
        return schemas.TransactionPage.parse_raw(resp.content)

def get_page_transaction(trans_id) -> schemas.Transaction:
    with requests.sessions.Session() as session:
        session.auth = ('api', TOKEN)
        resp = session.get(f'{NETLOC}/api/transaction?trans_id={trans_id}')
        if resp.ok:
            logger.debug(resp.content)
        return schemas.Transaction.parse_raw(resp.content)

def get_get_short_stat(fr_to: schemas.ShortStat) -> schemas.ShortStat:
    with requests.sessions.Session() as session:
        session.auth = ('api', TOKEN)
        resp = session.get(f'{NETLOC}/api/short-stat', data=fr_to.json())
        if resp.ok:
            logger.debug(resp.content)
        return schemas.ShortStat.parse_raw(resp.content)

def get_balance() -> schemas.Debts:
    with requests.sessions.Session() as session:
        session.auth = ('api', TOKEN)
        resp = session.get(f'{NETLOC}/api/debt')
        if resp.ok:
            logger.debug(resp.content)
        return schemas.Debts.parse_raw(resp.content)

def get_site_stat_data(year: int, site_name: str) -> schemas.FinSiteStat:
    with requests.sessions.Session() as session:
        session.auth = ('api', TOKEN)
        resp = session.get(f'{NETLOC}/api/site_stat_data?year={year}&site_name={site_name}')
        logger.debug(resp.content)
        return schemas.FinSiteStat.parse_raw(resp.content)

def get_plus_data() -> schemas.PlusSiteData:
    with requests.sessions.Session() as session:
        session.auth = ('api', TOKEN)
        resp = session.get(f'{NETLOC}/api/get_plus_data')
        logger.debug(resp.content)
        return schemas.PlusSiteData.parse_raw(resp.content)


import requests
import random

list_url = ["https://www.google.com/", "https://www.facebook.com/", "https://www.twitter.com/",
            "https://www.amazon.com/", "https://www.apple.com/"]

link = random.choice(list_url)

res = requests.get(link)
print(f'Status code is {res.status_code} from {link} with length of {len(res.text)} symbols')

# Task2

weather_link = "https://geocoding-api.open-meteo.com/v1/search?name="

city = input('Enter your city: ')

res2 = requests.get(f'{weather_link}{city}&count=1')

if len(res2.json()) - 1:
    data = res2.json()
    data2 = requests.get(f'https://api.open-meteo.com/v1/forecast?latitude='
                         f'{data["results"][0]["latitude"]}&longitude={data["results"][0]["longitude"]}'
                         f'&current_weather=true').json()
    print(f"Current temperature in {city} is {data2['current_weather']['temperature']}")
else:
    print(f'There no such city as {city}')


from keras.models import Model
from keras.regularizers import *
from keras.layers import *
from settings import *

from EqDense import EqDense
from ModulatedConv2D import ModulatedConv2D
from NoiseLayer import NoiseLayer
from BiasLayer import BiasLayer

from settings import *

def style_block(w, x, noise, filters):
	style = EqDense(x.shape[-1], bias_init_value=1)(w)
	x = ModulatedConv2D(filters, (KERNEL_SIZE, KERNEL_SIZE), use_demod=True)([x, style])
	cropped_noise = Cropping2D((noise.shape[1] - x.shape[1]) // 2)(noise)
	x = NoiseLayer()([x, cropped_noise])
	x = BiasLayer()(x)
	x = LeakyReLU(alpha=LEAKY_RELU_ALPHA)(x)
	return x

def to_rgb(w, x):
	style = EqDense(x.shape[-1], bias_init_value=1)(w)
	x = ModulatedConv2D(3, (KERNEL_SIZE, KERNEL_SIZE), use_demod=False)([x, style])
	x = BiasLayer()(x)
	return x

def const_input(x):
	x = Lambda(lambda x: tf.ones_like(x))(x)
	x = EqDense(IMAGE_MIN_SIZE * IMAGE_MIN_SIZE * FILTERS_MAX, use_bias=False)(x)
	x = Reshape((IMAGE_MIN_SIZE, IMAGE_MIN_SIZE, FILTERS_MAX))(x)
	return x

def get_filters(stage):
	return max(FILTERS_MIN, FILTERS_MAX // (2**stage))

def get_generator_model():
	input = Input(shape=(1,))
	w_inputs = [Input(shape=(LATENT_DIM,)) for _ in range(BLOCKS_COUNT)]
	noise_inputs = [Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 1)) for _ in range(BLOCKS_COUNT * 2 + 1)]

	constant = const_input(input)

	x = style_block(w_inputs[0], constant, noise_inputs[0], FILTERS_MAX)
	last_skip_connection = to_rgb(w_inputs[0], x)

	for i in range(BLOCKS_COUNT):
		filters = get_filters(i)

		x = UpSampling2D(interpolation='bilinear')(x)
		x = style_block(w_inputs[i], x, noise_inputs[i * 2 + 1], filters)
		x = style_block(w_inputs[i], x, noise_inputs[i * 2 + 2], filters)

		skip_connection = to_rgb(w_inputs[i], x)
		last_skip_connection = UpSampling2D(interpolation='bilinear')(last_skip_connection)
		last_skip_connection = Add()([last_skip_connection, skip_connection])

	x = Activation('tanh')(last_skip_connection)
	return Model([input] + w_inputs + noise_inputs, x)