# ------------------------------------------------------------------------------
# Post-processing to get instance and panoptic segmentation results.
# Written by Bowen Cheng (bcheng9@illinois.edu)
# ------------------------------------------------------------------------------

import torch
import torch.nn.functional as F

from .semantic_post_processing import get_semantic_segmentation

__all__ = ['find_instance_center', 'get_instance_segmentation', 'get_panoptic_segmentation']

def find_instance_center(ctr_hmp, threshold=0.1, nms_kernel=3, top_k=None):
    """
    Find the center points from the center heatmap.
    Arguments:
        ctr_hmp: A Tensor of shape [N, 1, H, W] of raw center heatmap output, where N is the batch size,
            for consistent, we only support N=1.
        threshold: A Float, threshold applied to center heatmap score.
        nms_kernel: An Integer, NMS max pooling kernel size.
        top_k: An Integer, top k centers to keep.
    Returns:
        A Tensor of shape [K, 2] where K is the number of center points. The order of second dim is (y, x).
    """
    if ctr_hmp.size(0) != 1:
        raise ValueError('Only supports inference for batch size = 1')

    # thresholding, setting values below threshold to -1
    ctr_hmp = F.threshold(ctr_hmp, threshold, -1)

    # NMS
    nms_padding = (nms_kernel - 1) // 2
    ctr_hmp_max_pooled = F.max_pool2d(ctr_hmp, kernel_size=nms_kernel, stride=1, padding=nms_padding)
    ctr_hmp[ctr_hmp != ctr_hmp_max_pooled] = -1

    # squeeze first two dimensions
    ctr_hmp = ctr_hmp.squeeze()
    assert len(ctr_hmp.size()) == 2, 'Something is wrong with center heatmap dimension.'

    # find non-zero elements
    ctr_all = torch.nonzero(ctr_hmp > 0)
    if top_k is None:
        return ctr_all
    elif ctr_all.size(0) < top_k:
        return ctr_all
    else:
        # find top k centers.
        top_k_scores, _ = torch.topk(torch.flatten(ctr_hmp), top_k)
        return torch.nonzero(ctr_hmp > top_k_scores[-1])

def group_pixels(ctr, offsets):
    """
    Gives each pixel in the image an instance id.
    Arguments:
        ctr: A Tensor of shape [K, 2] where K is the number of center points. The order of second dim is (y, x).
        offsets: A Tensor of shape [N, 2, H, W] of raw offset output, where N is the batch size,
            for consistent, we only support N=1. The order of second dim is (offset_y, offset_x).
    Returns:
        A Tensor of shape [1, H, W] (to be gathered by distributed data parallel).
    """
    if offsets.size(0) != 1:
        raise ValueError('Only supports inference for batch size = 1')

    offsets = offsets.squeeze(0)
    height, width = offsets.size()[1:]

    # generates a coordinate map, where each location is the coordinate of that loc
    y_coord = torch.arange(height, dtype=offsets.dtype, device=offsets.device).repeat(1, width, 1).transpose(1, 2)
    x_coord = torch.arange(width, dtype=offsets.dtype, device=offsets.device).repeat(1, height, 1)
    coord = torch.cat((y_coord, x_coord), dim=0)

    ctr_loc = coord + offsets
    ctr_loc = ctr_loc.reshape((2, height * width)).transpose(1, 0)

    # ctr: [K, 2] -> [K, 1, 2]
    # ctr_loc = [H*W, 2] -> [1, H*W, 2]
    ctr = ctr.unsqueeze(1)
    ctr_loc = ctr_loc.unsqueeze(0)

    # distance: [K, H*W]
    distance = torch.norm(ctr - ctr_loc, dim=-1)

    # finds center with minimum distance at each location, offset by 1, to reserve id=0 for stuff
    instance_id = torch.argmin(distance, dim=0).reshape((1, height, width)) + 1
    return instance_id

def get_instance_segmentation(sem_seg, ctr_hmp, offsets, thing_list, threshold=0.1, nms_kernel=3, top_k=None,
                              thing_seg=None):
    """
    Post-processing for instance segmentation, gets class agnostic instance id map.
    Arguments:
        sem_seg: A Tensor of shape [1, H, W], predicted semantic label.
        ctr_hmp: A Tensor of shape [N, 1, H, W] of raw center heatmap output, where N is the batch size,
            for consistent, we only support N=1.
        offsets: A Tensor of shape [N, 2, H, W] of raw offset output, where N is the batch size,
            for consistent, we only support N=1. The order of second dim is (offset_y, offset_x).
        thing_list: A List of thing class id.
        threshold: A Float, threshold applied to center heatmap score.
        nms_kernel: An Integer, NMS max pooling kernel size.
        top_k: An Integer, top k centers to keep.
        thing_seg: A Tensor of shape [1, H, W], predicted foreground mask, if not provided, inference from
            semantic prediction.
    Returns:
        A Tensor of shape [1, H, W] (to be gathered by distributed data parallel).
        A Tensor of shape [1, K, 2] where K is the number of center points. The order of second dim is (y, x).
    """
    if thing_seg is None:
        # gets foreground segmentation
        thing_seg = torch.zeros_like(sem_seg)
        for thing_class in thing_list:
            thing_seg[sem_seg == thing_class] = 1

    ctr = find_instance_center(ctr_hmp, threshold=threshold, nms_kernel=nms_kernel, top_k=top_k)
    if ctr.size(0) == 0:
        return torch.zeros_like(sem_seg), ctr.unsqueeze(0)
    ins_seg = group_pixels(ctr, offsets)
    return thing_seg * ins_seg, ctr.unsqueeze(0)

def merge_semantic_and_instance(sem_seg, ins_seg, label_divisor, thing_list, stuff_area, void_label):
    """
    Post-processing for panoptic segmentation, by merging semantic segmentation label and class agnostic
        instance segmentation label.
    Arguments:
        sem_seg: A Tensor of shape [1, H, W], predicted semantic label.
        ins_seg: A Tensor of shape [1, H, W], predicted instance label.
        label_divisor: An Integer, used to convert panoptic id = semantic id * label_divisor + instance_id.
        thing_list: A List of thing class id.
        stuff_area: An Integer, remove stuff whose area is less tan stuff_area.
        void_label: An Integer, indicates the region has no confident prediction.
    Returns:
        A Tensor of shape [1, H, W] (to be gathered by distributed data parallel).
    Raises:
        ValueError, if batch size is not 1.
    """
    # In case thing mask does not align with semantic prediction
    pan_seg = torch.zeros_like(sem_seg) + void_label
    thing_seg = ins_seg > 0
    semantic_thing_seg = torch.zeros_like(sem_seg)
    for thing_class in thing_list:
        semantic_thing_seg[sem_seg == thing_class] = 1

    # keep track of instance id for each class
    class_id_tracker = {}

    # paste thing by majority voting
    instance_ids = torch.unique(ins_seg)
    for ins_id in instance_ids:
        if ins_id == 0:
            continue
        # Make sure only do majority voting within semantic_thing_seg
        thing_mask = (ins_seg == ins_id) & (semantic_thing_seg == 1)
        if torch.nonzero(thing_mask).size(0) == 0:
            continue
        class_id, _ = torch.mode(sem_seg[thing_mask].view(-1, ))
        if class_id.item() in class_id_tracker:
            new_ins_id = class_id_tracker[class_id.item()]
        else:
            class_id_tracker[class_id.item()] = 1
            new_ins_id = 1
        class_id_tracker[class_id.item()] += 1
        pan_seg[thing_mask] = class_id * label_divisor + new_ins_id

    # paste stuff to unoccupied area
    class_ids = torch.unique(sem_seg)
    for class_id in class_ids:
        if class_id.item() in thing_list:
            # thing class
            continue
        # calculate stuff area
        stuff_mask = (sem_seg == class_id) & (~thing_seg)
        area = torch.nonzero(stuff_mask).size(0)
        if area >= stuff_area:
            pan_seg[stuff_mask] = class_id * label_divisor

    return pan_seg

def get_panoptic_segmentation(sem, ctr_hmp, offsets, thing_list, label_divisor, stuff_area, void_label,
                              threshold=0.1, nms_kernel=3, top_k=None, foreground_mask=None):
    """
    Post-processing for panoptic segmentation.
    Arguments:
        sem: A Tensor of shape [N, C, H, W] of raw semantic output, where N is the batch size, for consistent,
            we only support N=1. Or, a processed Tensor of shape [1, H, W].
        ctr_hmp: A Tensor of shape [N, 1, H, W] of raw center heatmap output, where N is the batch size,
            for consistent, we only support N=1.
        offsets: A Tensor of shape [N, 2, H, W] of raw offset output, where N is the batch size,
            for consistent, we only support N=1. The order of second dim is (offset_y, offset_x).
        thing_list: A List of thing class id.
        label_divisor: An Integer, used to convert panoptic id = semantic id * label_divisor + instance_id.
        stuff_area: An Integer, remove stuff whose area is less tan stuff_area.
        void_label: An Integer, indicates the region has no confident prediction.
        threshold: A Float, threshold applied to center heatmap score.
        nms_kernel: An Integer, NMS max pooling kernel size.
        top_k: An Integer, top k centers to keep.
        foreground_mask: A Tensor of shape [N, 2, H, W] of raw foreground mask, where N is the batch size,
            we only support N=1. Or, a processed Tensor of shape [1, H, W].
    Returns:
        A Tensor of shape [1, H, W] (to be gathered by distributed data parallel), int64.
    Raises:
        ValueError, if batch size is not 1.
    """
    if sem.dim() != 4 and sem.dim() != 3:
        raise ValueError('Semantic prediction with un-supported dimension: {}.'.format(sem.dim()))
    if sem.dim() == 4 and sem.size(0) != 1:
        raise ValueError('Only supports inference for batch size = 1')
    if ctr_hmp.size(0) != 1:
        raise ValueError('Only supports inference for batch size = 1')
    if offsets.size(0) != 1:
        raise ValueError('Only supports inference for batch size = 1')
    if foreground_mask is not None:
        if foreground_mask.dim() != 4 and foreground_mask.dim() != 3:
            raise ValueError('Foreground prediction with un-supported dimension: {}.'.format(sem.dim()))

    if sem.dim() == 4:
        semantic = get_semantic_segmentation(sem)
    else:
        semantic = sem

    if foreground_mask is not None:
        if foreground_mask.dim() == 4:
            thing_seg = get_semantic_segmentation(foreground_mask)
        else:
            thing_seg = foreground_mask
    else:
        thing_seg = None

    instance, center = get_instance_segmentation(semantic, ctr_hmp, offsets, thing_list,
                                                 threshold=threshold, nms_kernel=nms_kernel, top_k=top_k,
                                                 thing_seg=thing_seg)
    panoptic = merge_semantic_and_instance(semantic, instance, label_divisor, thing_list, stuff_area, void_label)

    return panoptic, center


from __future__ import annotations

from typing import TYPE_CHECKING

from sqlalchemy.ext.asyncio import AsyncSession

from core import tools
from utils import helpers

if TYPE_CHECKING:
    from ws import WSManager

async def check_player(
    session: AsyncSession,
    manager: WSManager,
    session_id: int,
    player_id: int,
) -> None:
    player = await helpers.get_player_by_id(session=session, player_id=player_id)

    if player.game_chips == 0 and not player.is_allin:
        await helpers.delete_player(
            manager=manager, player_id=player_id, preview_balance=player.game_chips
        )

    if player.is_allin or player.is_folded:
        await tools.store.game_session_accessor.set_next_player(
            session=session, session_id=session_id
        )


### Forecast.py
from urllib.request import urlopen

class Forecast(object):
	city = None
	state = None
	def __init__(self,city,state):
		self.city = city.upper()
		self.state = state.lower()
		self.descTemp = []
		self.highsTemp = []
		self.lowsTemp = []
		self.overall = None

	def lows(self):
		return list(self.lowsTemp)

	def highs(self):
		return list(self.highsTemp)

	def descriptions(self):
		return list(self.descTemp)

	def overallInfo(self):
		print(self.overall)

	def getWeather(self):
		HOST = 'http://weather.noaa.gov/'
		FCST = '/pub/data/forecasts/state/'

		URL = HOST + FCST + '/' + self.state + '/' + self.state + 'z013.txt'

		try :
			DATA = urlopen(URL)
			while True:
			    LINE = DATA.readline().decode()
			    if LINE == '':
			        break
			    LINE = LINE.replace('\n', ' ')
			    L = LINE.split(' ')
			    if 'FCST' in L:
			        LINE = DATA.readline().decode()
			        self.overall = LINE + DATA.readline().decode()
			    if self.city in L:

			        LINE = DATA.readline().decode()
			        self.overall = self.overall + LINE
			        self.desc = LINE.split(' ')
			        self.descTemp = filter(None, self.desc)
			        LINE = DATA.readline().decode()
			        self.overall = self.overall + LINE
			        temperatures = LINE.split(' ')
			        temperatures = filter(None, temperatures)
			        if temperatures is not None :
				        for item in temperatures:
				            temp = item.split('/')
				            if len(temp) == 2:
				                if temp[0] is not None :
				                    self.lowsTemp.append(temp[0])

				                if temp[1] is not None :
				                    self.highsTemp.append(temp[1])
		except :
			print("No data found")


'''
Description      :  Directly Using Filter, Map, Reduce In Lambda
Function Date    :  21 Feb 2021
Function Author  :  Prasad Dangare
Input            :  Int
Output           :  Int

'''

import functools

def main():

    arr = []
    print("Enter Number Of Elements : ")
    size = int(input())

    for i in range(size):
        print("Enter Elements Number : ", i + 1)
        no = int(input())
        arr.append(no)

    print("Your Entered Data Is : ", arr)

    newdata = list(filter(lambda no: (no % 2 == 0), arr)) # newdata = MarvellousFilter(arr)
    print("After Filtering Data Is : ", newdata)

    newdata1 = list(map(lambda no: no + 2, newdata))#newdata1 = MarvellousMap(newdata)
    print("After Map Is : ", newdata1)

    output = functools.reduce(lambda no1, no2: no1 + no2, newdata1)#output = MarvellousReduce(newdata1)
    print("After Reduce Result Is : ", output)

if __name__ == "__main__":
    main()


#学习 Python 与其他语言最大的区别就是，Python 的代码块不使用大括号 {} 来控制类，函数以及其他逻辑判断。
# python 最具特色的就是用缩进来写模块。
#缩进的空白数量是可变的，但是所有代码块语句必须包含相同的缩进空白数量，这个必须严格执行。

#1、行和缩进
#实例一
if True:
    print("True")
else:
    print("False")

#2、多行语句
#Python语句中一般以新行作为语句的结束符号
#但是我们可以使用斜杠（\）将一行的语句分为多行显示

#实例二
# total  = item_one + \
#          item_two + \
#          item_three

#语句中包含 [], {} 或 () 括号就不需要使用多行连接符。如下实例：
days = ['Monday',
        'Tuesday',
        'Wednesday',
        'Thursday']

# 3、Python注释
#python中单行注释采用 # 开头。
# 第一个注释
print ("Hello, Python!")  # 第二个注释

# 4、Print 输出
# print 默认输出是换行的，如果要实现不换行需要在变量末尾加上逗号 ,
x = "a"
y = "b"

print (x,y)


from pathlib import Path
from unittest.mock import MagicMock, patch
from typing import Iterable

import pytest

import sciety_labs.utils.cache as cache_module
from sciety_labs.utils.cache import (
    DiskSingleObjectCache,
    InMemorySingleObjectCache
)

@pytest.fixture(name='monotonic_mock')
def _monotonic_mock() -> Iterable[MagicMock]:
    with patch.object(cache_module, 'monotonic') as mock:
        yield mock

class TestInMemorySingleObjectCache:
    def test_should_get_loaded_value(self):
        cache = InMemorySingleObjectCache(max_age_in_seconds=10)
        result = cache.get_or_load(load_fn=lambda: 'value_1')
        assert result == 'value_1'

    def test_should_not_call_load_function_multiple_times(self):
        cache = InMemorySingleObjectCache(max_age_in_seconds=10)
        load_fn = MagicMock(name='load_fn')
        load_fn.return_value = 'value_1'
        cache.get_or_load(load_fn=load_fn)
        result = cache.get_or_load(load_fn=load_fn)
        assert result == 'value_1'
        assert load_fn.call_count == 1

    def test_should_reload_if_max_age_reached(self, monotonic_mock: MagicMock):
        cache = InMemorySingleObjectCache[str](
            max_age_in_seconds=60
        )
        load_fn = MagicMock(name='load_fn')
        load_fn.side_effect = ['value_1', 'value_2']
        monotonic_mock.return_value = 100
        cache.get_or_load(load_fn=load_fn)
        monotonic_mock.return_value = 200
        result = cache.get_or_load(load_fn=load_fn)
        assert result == 'value_2'
        assert load_fn.call_count == 2

class TestDiskSingleObjectCache:
    def test_should_get_loaded_value(self, tmp_path: Path):
        cache = DiskSingleObjectCache[str](
            file_path=tmp_path / 'cache.file',
            max_age_in_seconds=10
        )
        result = cache.get_or_load(load_fn=lambda: 'value_1')
        assert result == 'value_1'

    def test_should_not_call_load_function_multiple_times(self, tmp_path: Path):
        cache = DiskSingleObjectCache[str](
            file_path=tmp_path / 'cache.file',
            max_age_in_seconds=10
        )
        load_fn = MagicMock(name='load_fn')
        load_fn.side_effect = ['value_1', 'value_2']
        cache.get_or_load(load_fn=load_fn)
        result = cache.get_or_load(load_fn=load_fn)
        assert result == 'value_1'
        assert load_fn.call_count == 1

    def test_should_call_load_function_after_cache_file_is_removed(self, tmp_path: Path):
        cache = DiskSingleObjectCache[str](
            file_path=tmp_path / 'cache.file',
            max_age_in_seconds=10
        )
        load_fn = MagicMock(name='load_fn')
        load_fn.side_effect = ['value_1', 'value_2']
        cache.get_or_load(load_fn=load_fn)
        cache.file_path.unlink()
        result = cache.get_or_load(load_fn=load_fn)
        assert result == 'value_2'
        assert load_fn.call_count == 2


import os
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

url = "https://letskodeit.teachable.com/courses"

class DynamicXPathFormat():
    def test(self):
        driver = webdriver.Firefox()
        driver.maximize_window()
        driver.get(url)
        driver.implicitly_wait(5)

        """    # Login - Lecture "How to click and type on a web element"
        login_link = driver.find_element(By.LINK_TEXT, "Login")
        login_link.click()

        email_field = driver.find_element(By.XPATH, "//input[@id='email']")
        email_field.send_keys("test@email.com")

        password_field = driver.find_element(By.ID, "password")
        password_field.send_keys("abcabc")

        login_field = driver.find_element(By.XPATH, "//input[contains(@value,'Login')]")
        login_field.click() """

        # Search for courses -> You don't need to search the course
        # You can select it without searching also
        searchBox = driver.find_element(By.ID, "search-courses")
        searchBox.send_keys("JavaScript")

        # Select Course
        _course = "//div[contains(@class,'course-listing-title') and contains(text(),'{0}')]"
        _courseLocator = _course.format("JavaScript for beginners")

        courseElement = driver.find_element(By.XPATH, _courseLocator)
        courseElement.click()
        sleep(5)

        driver.quit()

ff = DynamicXPathFormat()
ff.test()


from itertools import product

bin_sizes = [4, 8, 16, 32, 64, 128, 512, 1024]
mask_sizes = [1, 2, 4, 8, 16, 32, 64, 128, "_random"]
combinations = list(product(bin_sizes, mask_sizes))
machines = {
    "starariel-0": [],
    "starariel-1": [],
    "starariel-2": [],
    "starariel-3": [],
    "stardale-0": [],
    "stardale-1": [],
    "stardale-2": [],
    "stardale-3": [],
}
# divide the combinations into 8 groups

for i, machine in enumerate(machines):
    machines[machine] = combinations[i::8]

with open("scripts/train.sh", "w") as f:
    f.write("#!/bin/bash\n")
    f.write("if [ \"$1\" == \"--setup\" ]; then\n")
    f.write("\tcd /disk/scratch/s1764494\n")
    f.write("\tgcloud storage cp gs://datasets-cdminix/timit.zip .\n")
    f.write("\tunzip timit.zip\n")
    f.write("\tmv timit data/timit_0\n")
    f.write("\tcp -r data/timit_0 data/timit_1\n")
    f.write("\tcp -r data/timit_0 data/timit_2\n")
    f.write("\tcp -r data/timit_0 data/timit_3\n")
    f.write("\tgcloud storage cp gs://datasets-cdminix/bu_radio.zip .\n")
    f.write("\tunzip bu_radio.zip\n")
    f.write("\tmv bu_radio data/bu_radio_0\n")
    f.write("\tcp -r data/bu_radio_0 data/bu_radio_1\n")
    f.write("\tcp -r data/bu_radio_0 data/bu_radio_2\n")
    f.write("\tcp -r data/bu_radio_0 data/bu_radio_3\n")
    f.write("\tcd /disk/scratch/s1764494/masked-prosody-modeling-evaluation\n")
    f.write("\tgcloud storage cp -R gs://masked-prosody-model/checkpoints .\n")
    f.write("\tmv checkpoints mpm_checkpoints\n")
    f.write("fi\n")
    for machine, combs in machines.items():
        # use --machine to specify the machine
        f.write('if [ "$1" == "--machine" ] && [ "$2" == "{}" ]; then\n'.format(machine))
        gpu_num = machine.split("-")[1]
        burn_preamble = f'\tCUDA_VISIBLE_DEVICES={gpu_num} BURN_PATH="/disk/scratch/s1764494/data/bu_radio_{gpu_num}" HF_DATASETS_CACHE="/disk/scratch/s1764494/data/hf_{gpu_num}" '
        timit_preamble = f'\tCUDA_VISIBLE_DEVICES={gpu_num} TIMIT_PATH="/disk/scratch/s1764494/data/timit_{gpu_num}" HF_DATASETS_CACHE="/disk/scratch/s1764494/data/hf_{gpu_num}" '
        ravdess_preamble = f'\tCUDA_VISIBLE_DEVICES={gpu_num} HF_DATASETS_CACHE="/disk/scratch/s1764494/data/hf_{gpu_num}" '
        linear_command = 'python scripts/train_{dataset}.py configs/mpm_linear.yml --run_name {dataset}_bin{bin}_mask{mask}_linear_{num} --mpm_bin_size {bin} --mpm_mask_size {mask} --use_mpm --mpm_layer 7'
        conformer_command = 'python scripts/train_{dataset}.py configs/mpm_conformer.yml --run_name {dataset}_bin{bin}_mask{mask}_conformer --mpm_bin_size {bin} --mpm_mask_size {mask} --use_mpm --mpm_layer 7'
        for bin_size, mask_size in combs:
            for dataset in ["burn", "timit", "ravdess"]:
                if dataset == "burn":
                    preamble = burn_preamble
                elif dataset == "timit":
                    preamble = timit_preamble
                elif dataset == "ravdess":
                    preamble = ravdess_preamble
                f.write(preamble + linear_command.format(bin=bin_size, mask=mask_size, dataset=dataset, num=1) + " --overwrite_data\n")
                f.write(preamble + linear_command.format(bin=bin_size, mask=mask_size, dataset=dataset, num=2) + "\n")
                f.write(preamble + linear_command.format(bin=bin_size, mask=mask_size, dataset=dataset, num=3) + "\n")
                f.write(preamble + conformer_command.format(bin=bin_size, mask=mask_size, dataset=dataset) + "\n")
        f.write("fi\n")


#!/usr/bin/env python
# coding: utf-8

# In[2]:

import pickle

modelo = open('preco','rb')
lm_new = pickle.load(modelo)
modelo.close()

area = 38
garagem = 2
banheiros = 4
lareira = 4
marmore = 0
andares = 1

entrada = [[area, garagem, banheiros, lareira, marmore, andares]]

print('$ {0:.2f}'.format(lm_new.predict(entrada)[0]))

# In[4]:

# Importando bibliotecas
from ipywidgets import widgets, HBox, VBox
from IPython.display import display

# Criando os controles do formulário
area = widgets.Text(description="Área")
garagem = widgets.Text(description="Garagem")
banheiros = widgets.Text(description="Banheiros")
lareira = widgets.Text(description="Lareira")
marmore = widgets.Text(description="Mármore?")
andares = widgets.Text(description="Andares?")

botao = widgets.Button(description="Simular")

# Posicionando os controles
left = VBox([area, banheiros, marmore])
right = VBox([garagem, lareira, andares])
inputs = HBox([left, right])

# Função de simulação
def simulador(sender):
    entrada=[[
                float(area.value if area.value else 0),
                float(garagem.value if garagem.value else 0),
                float(banheiros.value if banheiros.value else 0),
                float(lareira.value if lareira.value else 0),
                float(marmore.value if marmore.value else 0),
                float(andares.value if andares.value else 0)
             ]]
    print('$ {0:.2f}'.format(lm_new.predict(entrada)[0]))

# Atribuindo a função "simulador" ao evento click do botão
botao.on_click(simulador)

# In[5]:

display(inputs, botao)

# In[ ]:


import pygame
from projects.alien_invasion.settings import Settings
from projects.alien_invasion.ship import Ship
from projects.alien_invasion import game_functions as gf
from pygame.sprite import Group
from projects.alien_invasion.game_stats import GameStats
from projects.alien_invasion.button import Button
from projects.alien_invasion.score_board import ScoreBoard

def run_game() -> None:
    """Initializing new game"""
    ai_settings = Settings()
    pygame.init()  # Inside settings
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))  # Create screen area
    pygame.display.set_caption("Alien Invasion")  # Create window title
    play_button = Button(ai_settings, screen, "Play")
    stats = GameStats(ai_settings)
    sb = ScoreBoard(ai_settings, screen, stats)
    ship = Ship(screen, ai_settings)  # create ship
    bullets = Group()  # Create group to save bullets
    aliens = Group()
    gf.create_fleet(ai_settings, screen, ship, aliens)
    while True:  # Game cycle
        gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets)
        if stats.game_active:
            ship.update()
            gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets)
            gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets)
        gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)

run_game()


import torch

torch.autograd.set_detect_anomaly(True)

class Embedding:
    """
    Embedding class embeds x (input) to (x, sin(2^k x), cos(2^k x), ...)
    """

    def __init__(self, num_freqs: int, scalar_factor: float = 1.0) -> None:

        self._input_dims = 3
        self._num_freqs = num_freqs
        self._max_freq_log2 = num_freqs - 1
        self._periodic_functions = [torch.sin, torch.cos]
        self._scalar_factor = scalar_factor

        self._embedding_functions = []
        self._output_dim = 0

        self._create_embedding_functions()

    def _create_embedding_functions(self):

        # Original raw input "x" is also included in the output
        original_input_fcn = lambda x: x
        self._embedding_functions.append(original_input_fcn)
        self._output_dim += self._input_dims

        # Log scale for sampling
        freq_bands = 2. ** torch.linspace(0., self._max_freq_log2, steps=self._num_freqs)

        for freq in freq_bands:
            for p_fn in self._periodic_functions:
                periodic_fcn = lambda x, periodic_fn=p_fn, freq=freq: periodic_fn(x * freq)
                self._embedding_functions.append(periodic_fcn)
                self._output_dim += self._input_dims

    @property
    def output_dim(self) -> int:
        return self._output_dim

    def embed(self, inputs: torch.Tensor) -> torch.Tensor:
        """
        Run embedding, by passing inputs through all embedding functions and concatenating them.
        """
        return torch.cat([fcn(inputs / self._scalar_factor) for fcn in self._embedding_functions], -1)


from pycparser.c_ast import *

pragma_loops = []

def get_main_content(ext):
    """zwraca zawartość maina"""
    for elem in ext:
        if elem.__class__ is FuncDef and elem.decl.name == 'main':
            return elem.body

def get_info_about_arrays(block_items: list) -> list:
    """ przegląda wszystkie elementy maina i wybiera te które inicjalizują nowe tablice i zwraca info o nich"""
    # TODO pokrywamy tylko jeden case
    output = []
    for item in block_items:
        if item.__class__ is Decl and item.name.isupper() and len(item.name) == 1:
            name = item.name
            sizes = []
            for constat in item.init.args.exprs:
                if constat.type == 'int':
                    sizes.append(int(constat.value))
            output.append((name, sizes))
    return output

def dig_for_a_pragma_loop(item):
    """jeśli jest stmt przeglądamy block_items i jak jest w nich Pragma to ddaojemy kolejny elemnt do globalnej listy"""
    if hasattr(item, "stmt") and hasattr(item.stmt, "block_items"):
        for idx, item_inside in enumerate(item.stmt.block_items):
            if item_inside.__class__ is Pragma:
                pragma_loops.append(item.stmt.block_items[idx + 1])
                return True
            if item_inside.__class__ is For:
                return dig_for_a_pragma_loop(item_inside)

def get_pragma_loops(block_items: list) -> None:
    for item in block_items:
        if item.__class__ is For:
            dig_for_a_pragma_loop(item)

def get_loop_bounds(init, cond, next):
    name = init.lvalue.name
    start = int(init.rvalue.value)
    stop = int(cond.right.value)
    if next.op == 'p++':
        step = 1
    elif next == '++p':
        step = 1
    else:
        step = int(next.rvalue.value)
    return name, start, stop, step

def explore_loop(loop, content, dependency_content):
    content.append(get_loop_bounds(loop.init, loop.cond, loop.next))
    if loop.stmt.__class__ is For:
        explore_loop(loop.stmt, content, dependency_content)
    if loop.stmt.__class__ is Compound:
        explore_dependencies(loop.stmt, dependency_content)
    return content, dependency_content

def explore_array_ref(array_ref: ArrayRef) ->list:
    if array_ref.name.__class__ is ArrayRef:
        return explore_array_ref(array_ref.name) + [array_ref.subscript.name]
    elif array_ref.name.__class__ is ID:
        return [array_ref.name.name, array_ref.subscript.name]

def get_info_about_loops(block_items: list) -> list:
    """ przegląda wszystkie elementy maina i wybiera te które opisują pętle i zwraca info o nich"""
    output = []
    for item in block_items:
        output.append(explore_loop(item, [], []))
    return output

def explore_binary_op(binary_op, content):
    # TODO case z i+5
    # left
    if binary_op.left.__class__ is BinaryOp:
        explore_binary_op(binary_op.left, content)
    elif binary_op.left.__class__ is ArrayRef:
        content.append(explore_array_ref(binary_op.left))

    # right
    if binary_op.right.__class__ is BinaryOp:
        explore_binary_op(binary_op.right, content)
    elif binary_op.right.__class__ is Constant:
        # TODO int czy float?
        content.append(int(binary_op.right.value))
    elif binary_op.right.__class__ is ArrayRef:
        content.append(explore_array_ref(binary_op.right))

def explore_dependencies(item, dependency_content):
    for assignment in item.block_items:
        array_access = []
        # lvalue
        array_access.append(explore_array_ref(assignment.lvalue))

        # rvalue
        if assignment.rvalue.__class__ is BinaryOp:
            explore_binary_op(assignment.rvalue, array_access)
        elif assignment.rvalue.__class__ is ArrayRef:
            array_access.append(explore_array_ref(assignment.rvalue))
        dependency_content.append(array_access)

def parse_ast_tree(ast_tree) -> dict:
    output = dict()
    main_body = get_main_content(ast_tree.ext)
    array_info = get_info_about_arrays(main_body.block_items)
    get_pragma_loops(main_body.block_items)
    loop_info = get_info_about_loops(pragma_loops)
    output['array_info'] = array_info
    output['loop_bounds'] = loop_info[0][0]
    output['data_deps'] = loop_info[0][1]

    return output


from .load_data import *
import os, sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
import neural_network as nn

def train_new_model(info_path, dataset_path, model_path):
    """ Given a set of file paths, generate a new machine learning model. """

    # get info and dataset from input files
    info = get_info(info_path)
    training_dataset = get_extended_dataset(info, dataset_path)
    # obtain the list of labels used in the current model
    labels = [character["label"] for character in info["characters"]]
    # instantiate the network, removing any repetition from the labels
    network = nn.Network([32 * 32, 75, 75, len(list(set(labels)))], list(set(labels)))
    dataset_outputs = []
    for i in range(len(labels)):
        for _ in range(10):
            # there are 10 examples of each character
            dataset_outputs.append(labels[i])
    # train the network and save to file
    y_correct = network.calc_y_activations(network.labels, dataset_outputs)
    network.train(training_dataset, y_correct, 500)
    network.export_layout(model_path)

def use_existing_model(info_path, sample_path, model_path):
    """ Given a set of file paths, apply an existing machine learning model. """

    # get info and dataset from input files
    info = get_info(info_path)
    sample_dataset = get_sample_dataset(info, sample_path)
    # instantiate a new network object
    network = nn.Network.import_layout(model_path)
    y_predicted = network.predict(sample_dataset)
    outputs = network.calc_results(network.labels, y_predicted)

    # format output
    current_word = 0
    current_line = 1
    output_string = ""
    # format the output string with spaces and new lines
    for i in range(len(outputs)):
        if info["characters"][i]["word_number"] != current_word or info["characters"][i]["line_number"] != current_line:
            if info["characters"][i]["line_number"] != current_line and current_line != 0:
                output_string += "\n"
            elif current_word != 0:
                output_string += " "
            current_word = info["characters"][i]["word_number"]
            current_line = info["characters"][i]["line_number"]
        output_string += outputs[i]

    return output_string


import re

F = open("input.txt", "r").read().splitlines()

for i in range(len(F)):
    SearchStr = r"\d+"
    res = F[i]
    Result = re.search(SearchStr.decode('utf-8'), res.decode('utf-8'), re.I | re.U)
    if Result:
        print(Result.groups())


from __future__ import print_function, division
import torch
from torchvision import models, transforms
import os
from PIL import Image
import json
import scipy.io as scio

class myDataset(object):
    def __init__(self,path,figures, transforms):
        self.transforms = transforms
        self.path=path
        self.figures=figures

    def __getitem__(self, idx):
        img = Image.open(os.path.join(self.path,self.figures[idx])).convert("RGB")

        if self.transforms is not None:
            img = self.transforms(img)

        return img, self.figures[idx]

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

data_transforms = {
    'pred': transforms.Compose([
        transforms.Resize((224,224)),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
}

if __name__ == '__main__':

    figpath='./example/'
    figures=['class_fatigue1.jpg','class_fatigue2.jpg','class_nonfatigue1.jpg','class_nonfatigue2.jpg']

    dataset = myDataset(figpath,figures, data_transforms['pred'])
    dataloaders = {'pred': torch.utils.data.DataLoader(dataset)}
    device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

    model = models.resnet50(pretrained=True)
    model.load_state_dict(torch.load('best.pth'))
    model.eval()
    data=[]
    with torch.no_grad():
        for i, (input, name) in enumerate(dataloaders['pred']):
            input = input.to(device)
            output = model(input)
            _, pred = torch.max(output, 1)
            data.append([str(name),int(pred)])

    with open('pred.json','w') as f:
        json.dump(data,f)


"""
agg_scraper.py
~~~
Performs the scraping on agg link (Nerdwallet in this case). It accepts a URL and returns a dictionary
describing a credit card.
"""

import copy
from scrape_to_dict.get_visible_text import scrape_visual_text_directly
import re

# All attribute headers have a _ appended to the front of it to mark it as a header
agg_attribute_mapping_dict = {
    "pros": ["_Pros", "_ Pros"],
    "cons": ["_Cons", "_ Cons"],
    "credit_score": ["_Recommended credit score"],
    "offer_details": ["_Card details"],
    "bonus_offer": ["_Bonus offer"],
    "rewards_rate": ["_Rewards rate"],
    "intro_apr_check": ["_Intro APR"],
    "variable_apr_check": ["_APR, Variable"],
    "annual_fee_check": ["_Annual fee"]
}

def scrape_from_agg(agg_url: str, card_dict_param: dict) -> dict:
    """
    Takes in an aggregator's URL. Scrapes the page. Returns a card_dict. Doesn't store anything

    Args:
        agg_url (str): The url of the agg link we scrape from.
        card_dict_param (dict): The dictionary of attributes and data scraped from the terms and conditions. We
        create a deep copy of the dict and add to it.
    Returns:
        dict: The dictionary containing scraped data from both the TOC and Nerdwallet.
    """
    card_dict = copy.deepcopy(card_dict_param)
    card_name = card_dict["full_card_name"]
    print("scraping from agg for " + card_name)
    print(agg_url)

    # (1) Get all visible text on the webpage (via script using beautiful soup).
    block = scrape_visual_text_directly(agg_url, add_new_lines=True)

    # (2) Try to get rid of unicode garbage as well as replace all "XYZABC123!!!" with new lines.
    try:
        block = block.decode('utf-8').strip()
        block = block.encode('ascii', 'ignore')
    except:
        print("Did not decode and encode.")
        pass

    # substitute all dummy strings with new lines
    block = re.sub(r"XYZABC123!!!", "\n", block)

    # (3) Condense text block to import segment.
    # There are two versions of the page. Figure out which version, then condense block accordingly.
    important_index = block.find("_Card details")
    other_index = block.find("Recommended credit score")

    if important_index < 0 or other_index < 0:
        print("important or other index less than 0")
        return

    if other_index < important_index:
        # version where credit score at the top of the page
        start_index = block.find("Recommended credit score")
        end_index = block.find("NerdWallet reviews are the result of independent research")
    else:
        # version where credit score along with other attributes
        start_index = block.find("_Card details")
        end_index = block.find("See if you may qualify")

    if start_index < 0 or end_index < 0:
        print("start or end index less than 0")
        return
    important_block = block[start_index:end_index]

    # print(important_block)

    # (4) For each agg attribute, grab its respective text in block.
    deep_copy = copy.deepcopy(agg_attribute_mapping_dict)
    agg_dict = __collect_info(deep_copy, important_block)

    # (5) Write to dict.
    for attribute, term in agg_dict.items():
        inner_dict = {"term": term, "value": ""}
        card_dict[attribute] = inner_dict

    # (6) Grab the trademark name. (should be between "Advertiser Disclosure" and "Apply Now")
    card_dict["trademark_card_name"] = block[block.find("Advertiser Disclosure") + len("Advertiser Disclosure"):
                                             block.find("Apply Now")]

    return card_dict

def __collect_info(attributes: dict, text: str) -> dict:
    """
    Used by scrape_from_agg to scrape the agg link. Collects data for each attribute and stores in a dictionary.

    Args:
        attributes: The dictionary of attributes we scrape for.
        text: The piece of text we scrape from. (In this case, it is all visible text on the Nerdwallet page)
    Returns:
        dict: The dictionary of scraped data.
    """
    # (A) Find all attributes that exist in text & location of attributes in text.
    attribute_info = []
    for attribute, aliases in attributes.items():
        alias_index = -1
        alias_length = 0
        for alias in aliases:  # each attribute has multiple aliases (ways it could be written in text)
            alias_index = text.find(alias)
            if alias_index >= 0:
                alias_length = len(alias)
                break  # can only have one alias of attribute
        if alias_index >= 0:
            attribute_info.append({'attribute_name': attribute,
                                   'alias_index': alias_index, 'alias_length': alias_length})

    # (B) Sort attributes in order that they appear in text.
    attribute_info_sorted = sorted(attribute_info, key=lambda k: k['alias_index'])

    # (C) For each attribute, grab the corresponding text.
    answer = dict()
    for index, attribute_dict in enumerate(attribute_info_sorted):
        start_index = attribute_dict['alias_index'] + attribute_dict['alias_length']
        end_index = len(text)  # assume last index
        if not index == (len(attribute_info_sorted) - 1):  # if not last index
            next_attribute_dict = attribute_info_sorted[index + 1]
            end_index = next_attribute_dict['alias_index']  # end index is start of next attribute
        answer[attribute_dict['attribute_name']] = text[start_index:end_index]

    return answer


from String_Database import String_Database

import random

class game:
    """   class game1 where all the game functioning  is performed """

    def __init__(self, secret, dashes):
        """
       :var secret variable is intialized to  value in parameter secret
       :var dashes variable is intialized to  value in parameter dashes
       :var total variable is intialized to  0
       :var missedLetter variable is intialized to  0
       :var finalScore variable is intialized to  0
       :var count variable is intialized to  0
       :var status variable is intialized to  value "gave up"
       :var boll variable is intialized to  boolean value False

        """

        self.secret = secret
        self.dashes = dashes
        self.total = 0.0
        self.missedLetter = 0
        self.badGuess = 0
        self.finalScore=0
        self.count=0
        self.boll=False
        self.status="Gave up"

    def guess_option(self,secret,dashes):

        """
        function guess_option is defined whhich is called
        when 1- guess option is selected
        It checks the input with the word , if it is right then success is shown
        else it returns the dashes with correct characters only

        Total score is calculated according to the no of bad guess and the frequency of missing characters

:arg
        secret (str): The first parameter which contains the random generated word
        dashes (str): The second parameter which contains the "-" or the correct chahracters

:return
     dashes(str): are returned in this function and checks with the secret word
    if it is correct then new word is loaded and console prints "success" otherwise other operations are performed on it.

        """

        try1 = input("enter the word:")
        if (try1!=secret):
            self.badGuess = self.badGuess + 1
            g = (list(try1))

            for i in range(len(secret)):
                if secret[i] == g[i]:
                    dashes = self.update_dashes(secret, dashes, secret[i])

            # guess_calculation(dashes)
            print(dashes)
            self.count=self.count+1
            for i in range(len(dashes)):
                if secret[i] != dashes[i]:
                    self.total = self.total + String_Database.frequency.get(secret[i])

            print("Your guess is wrong: ", "no of wrong guesses ", self.badGuess)
            return dashes

        else:
            print("Success")
            self.status="Success"

            dashes=secret
            if self.total==0:
             for i in dashes:
                self.total=self.total+String_Database.frequency.get(i)

            self.calculate_finalscore()
            print("final_score for guess-otpion",self.finalScore)
            return dashes

    def options(self,secret,dashes):

        """

               function option is defined which is called
               when 3- option is selected
               It checks the input with the word , if it is right then current right character is inserted inthe dashes and  is shown
               else it shows the input is incorrect.

       :arg
               secret (str): The first parameter which contains the random generated word
               dashes (str): The second parameter which contains the "-" or the correct characters

       :return
            dashes(str): are returned in this function and checks with the secret word
           if it is correct then new character  is inserted  and console prints "success"
           otherwise other operations are performed on it.

               """

        if(dashes!= secret) :

            # Print the amount of dashes and guesses left
            # print(dashes)

            # Ask the user for input

          print("\n")

          guess =input( "Enter the letter:")

            # Conditions that will print out a message according to
            # invalid inputs
          if len(guess) != 1:
                print("Your guess must have exactly one character!")
                return dashes

            # If the guess is in the secret word then we updtae dashes to replace the
            # corresponding dash with the correct index the guess belongs to in the
            # secret word
          elif guess in secret:
                print("That letter is in the secret word!")
                self.count=self.count+1
                dashes = self.update_dashes(secret,dashes,guess)
                self.status="success"

                return dashes

          else:
                print("That letter is not in the secret word!")
                self.count = self.count + 1
                self.missedLetter = self.missedLetter + 1

                return dashes

        # If the dash string equals the secret word in the end then the
        # user wins
        else:

            print("wooh!  The word was: " + str(secret))
            self.status="Success"
            dashes=str(secret)
            print(dashes)
            self.boll=True
            return dashes

    def tell_me(self,secret,dashes):

        """
              function tell_me is defined which is called
               when 2- option is selected
               It prints the correct word and sets the status to "gave up"
               and caculates the score according to correct input and gave up characters

               IT prints the  correct word and the missing letters in the correct letter
               final score = 0-total

       :arg:
               secret (str): The first parameter which contains the random generated word
               dashes (str): The second parameter which contains the "-" or the correct characters

       :return
             1 (int): it tells the system that load the new game as the user gave up this word .
               """

        result=""
        self.total=0
        for i in range(len(secret)):
            if secret[i] != dashes[i]:
                result=result+secret[i]

                self.total=self.total+String_Database.frequency.get(secret[i])

                self.status="Gave up"

        print("The corect word is : ", secret,", you missed :","letters: ",result)
        self.finalScore=0-self.total

        return 1

    def update_dashes(self, secret,cur_dash, rec_guess):

        """
                      function update_dashes is defined which is called
                      when there is right character is give by user
                      It updates the dashes with right characters returns the result

                      This function updates the string of dashes by replacing the dashes
                      with words that match up with the hidden word if the user manages to guess
                      it correctly

              Args:
                      secret (str): The first parameter which contains the random generated word
                      cur_dash (str): The second parameter which contains the "-" or the correct characters
                      rec_guess (str): It is the correct character which is input by user

              Returns:
                    result(Str): It returns the result which is dashes with correct the characters inserted in it at correct index according to the word
               """

        result = ""

        for i in range(len(secret)):
            if secret[i] == rec_guess:
                result = result + rec_guess  # Adds guess to string if guess is correctly

            else:
                # Add the dash at index i to result if it doesn't match the guess
                result = result + cur_dash[i]

        return result

    def calculate_finalscore(self):

        """
        function calculate_finalscore is used where all the game score calculation is done to get the final socre
        :var count is number of times user input the character and guess
        :var total is total score calculated  in different options method like guess, tell me,letter.
        :var badGuess is number of times user inputs wrong guess.

        :var finalScore is calculated by formula  :

         finalScore = (total/count)-((total/count)*(10*badGuess)/100)

        """

        if self.count!=0:
             print(self.count)
             print(self.badGuess)
             self.finalScore=(self.total/self.count)- ((self.total/self.count)*(10*self.badGuess)/100)

        else:
                self.finalScore=self.total


"""
Input Pipeline Handler for Localization CNN
Author: Edward Ferdian
Date:   01/06/2018
"""
import tensorflow as tf
import h5py
import numpy as np
from random import randint
import scipy.ndimage as ndimage
import math

class InputPipelineHandler:
    # constructor
    def __init__(self, batch_size):
        self.batch_size = batch_size

    def initialize_dataset(self, all_files, indexes, training=False):
        """
            Input pipeline.
            This function accepts a list of filenames with index to read.
            The _training_data_load_wrapper will read the filename-index pair and load the data.
        """
        ds = tf.data.Dataset.from_tensor_slices((all_files, indexes))

        if training:
            ds = ds.shuffle(1000)

        # Run the mapping functions on each data
        ds = ds.map(self.training_data_load_wrapper, num_parallel_calls=4)

        # ds = ds.map(_adjust_bbox_wrapper) # we don't do this anymore, the bbox in the dataset has been adjusted during prepare_data

        if training:
            ds = ds.map(self.augmentation_wrapper)
            # TODO: add more augmentation here (e.g. flip, translation, normalize, contrast, zoom, etc)

        ds = ds.map(self._resize_function)

        ds = ds.batch(batch_size=self.batch_size).prefetch(10)

        # Do not use repeat with initializable operator. Use 'while True' to exhaust the list
        return ds.make_initializable_iterator()

    # --- Wrapper functions for the tf.dataset input pipeline ---
    def training_data_load_wrapper(self, fpath, idx):
        # print('wrapper', idx)
        return tf.py_func(func=self.load_hd5_img_and_bbox_from_sequence,
            inp=[fpath, idx], Tout=[tf.float32, tf.float32])

    def augmentation_wrapper(self, img, coords):
        return tf.py_func(func=self._rotate_img_and_bbox,
            inp=[img, coords], Tout=[tf.float32, tf.float32])

    # def _adjust_bbox_wrapper(img, coords):
    #     return tf.py_func(func=self._adjust_bounding_box,
    #         inp=[img, coords, bbox_adjustment], Tout=[tf.float32, tf.float32])

    # --- end of wrapper functions ---

    def load_hd5_img_and_bbox_from_sequence(self, fpath, idx):
        '''
            Load an image from a pair of filename-index.
            Only the ED frame is taken from the image sequence.

        '''
        hd5path = fpath
        with h5py.File(hd5path, 'r') as hl:
            # [n, time=0, 256, 256]
            ed_img = np.asarray(hl.get('image_seqs')[idx,0,:,:]) # we only need the first frame
            # [n, 4]
            bbox = np.asarray(hl.get('bbox_corners')[idx])

        return ed_img.astype('float32'), bbox.astype('float32')

    def _resize_function(self, img, coords):
        img = tf.reshape(img, [256, 256])
        coords = tf.reshape(coords, [4])
        return img, coords

    def _rotate_img_and_bbox(self, img, bbox):
        """
            Rotate the image and bounding box.
            Currently it only does 90, 180, and 270 degrees.
        """
        rnd = randint(0,7)
        if (rnd == 0 or rnd > 3):
            # we give higher chance the image is not being rotated
            return img, bbox
        else:
            # we do only rotation for 90, 180, and 270 for now
            angle = rnd * 90
            new_img = ndimage.rotate(img, angle, reshape=False)
            new_bbox = self.rotate_coords_bbox(img.shape, bbox, -angle)

            return new_img, new_bbox.astype('float32')

    def rotate_coords_bbox(self, img_shape, coords, angle):
        center_point = np.asarray(img_shape)/2

        point = [coords[0], coords[1]]
        x1 = self.rotate_single_point(point, angle, center_point)

        point = [coords[2], coords[3]]
        x2 = self.rotate_single_point(point, angle, center_point)

        min_x = min(x1[0], x2[0])
        min_y = min(x1[1], x2[1])

        max_x = max(x1[0], x2[0])
        max_y = max(x1[1], x2[1])

        new_coords = np.asarray([min_x, min_y, max_x, max_y])

        return new_coords

    def rotate_single_point(self, point,angle, centerPoint):
        """
            Rotates a point around another centerPoint. Angle is in degrees.
            Rotation is counter-clockwise
        """
        angle = math.radians(angle)
        temp_point = point[0]-centerPoint[0] , point[1]-centerPoint[1]
        temp_point = ( temp_point[0]*math.cos(angle)-temp_point[1]*math.sin(angle) , temp_point[0]*math.sin(angle)+temp_point[1]*math.cos(angle))
        temp_point = temp_point[0]+centerPoint[0] , temp_point[1]+centerPoint[1]
        return temp_point


from te import tik
from impl import common_util
from impl import constant_util as constant

# max ub size(unit: element number)
MAX_UB_ELEMENT_NUMBER = 10240

# pylint: disable = locally-disabled,invalid-name,useless-object-inheritance
# pylint: disable = unused-argument,no-member,consider-using-in
class ScatterNdBase(object):
    """
       Function: use to store scatter_nd base parameters
       Modify : 2019-09-27
    """

    def __init__(self, input_param, tik_instance):
        """
        init scatter_nd base parameters

        Parameters
        ----------
        input_param: a tuple with indices,updates,output_y,shape
               input_param[0]is indices,dict,shape and datatype,
               datatype supports int32
               input_param[1] is updates,dict,shape and datatype,
               datatype supports float32,float16,int32,int8,uint8
               input_param[2] is output_y, dict,shape and datatype,
               datatype supports float32,float16,int32,int8,uint8
               input_param[3] is shape,out put shape
        tik_instance: tik_instance

        Returns
        -------
        None
        """
        self.tik_instance = tik_instance
        self.indices = input_param[0]
        self.updates = input_param[1]
        self.output_y = input_param[2]
        self.shape = input_param[3]
        self.data_size = common_util.get_data_size(
            self.updates.get("dtype").lower())

    def get_instance(self):
        """
        init scatter_nd  parameters

        Parameters
        ----------
        None

        Returns
        -------
        tik_instance: tik_instance
        """
        return self.tik_instance

    def get_data_size(self):
        """
        get data size

        Parameters
        ----------
        None

        Returns
        -------
        data_size: data_size
        """
        return self.data_size

class ScatterNd(ScatterNdBase):
    """
       Function: use to store scatter_nd  parameters
       Modify : 2019-09-27
    """

    def __init__(self, input_param, tik_instance):
        """
        init scatter_nd  parameters

        Parameters
        ----------
        input_param: a tuple with indices,updates,output_y,shape
               input_param[0]is indices,dict,shape and datatype,
               datatype supports int32
               input_param[1] is updates,dict,shape and datatype,
               datatype supports float32,float16,int32,int8,uint8
               input_param[2] is output_y, dict,shape and datatype,
               datatype supports float32,float16,int32,int8,uint8
               input_param[3] is shape,out put shape
        tik_instance: tik_instance

        Returns
        -------
        None
        """
        super(ScatterNd, self).__init__(input_param, tik_instance)
        updates = input_param[1]
        oneburst_num = constant.BLOCK_SIZE // common_util.get_data_size(
            input_param[0].get("dtype").lower())
        ind_shape = input_param[0].get("shape")
        indices_gm_num = get_gm_number(oneburst_num, ind_shape)
        indices_dtype = input_param[0].get("dtype").lower()

        if indices_gm_num > MAX_UB_ELEMENT_NUMBER and MAX_UB_ELEMENT_NUMBER % \
                ind_shape[-1] != 0:
            ind_ub_size = (MAX_UB_ELEMENT_NUMBER // ind_shape[-1]) * ind_shape[
                -1]
            last_ub = indices_gm_num % ind_ub_size

            if last_ub % oneburst_num != 0:
                last_size = (last_ub // oneburst_num + 1) * oneburst_num
                indices_gm_num = indices_gm_num - last_ub + last_size

        self.input_indices_gm = \
            self.tik_instance.Tensor(indices_dtype, (indices_gm_num,),
                                     name="input_indices_gm",
                                     scope=tik.scope_gm)
        oneburst_num = constant.BLOCK_SIZE // common_util.get_data_size(
            updates.get("dtype").lower())
        update_gm_num = self.get_last_alignment_gm_num(
            self.updates.get("shape"), oneburst_num)

        updates_dtype = updates.get("dtype").lower()
        self.input_updates_gm = \
            self.tik_instance.Tensor(updates_dtype, (update_gm_num,),
                                     name="input_updates_gm",
                                     scope=tik.scope_gm)

        out_gm_num = self.get_last_alignment_gm_num(input_param[3],
                                                    oneburst_num)
        self.output_y_gm = self.tik_instance.Tensor(updates_dtype,
                                                    (out_gm_num,),
                                                    name="output_y_gm",
                                                    scope=tik.scope_gm)

    def get_last_alignment_gm_num(self, shape, oneburst_num):
        """
        get last alignment gm number

        Parameters
        ----------
        shape: out put shape
        oneburst_num: data move oneburst can move
                                 the number of the elements

        Returns
        -------
        total_num: total gm element number
        """
        indice_len = get_indice_len(self.indices.get("shape"))
        update_each_size = get_shape_total_number(
            self.updates.get("shape")) // indice_len
        shape_len = get_shape_total_number(shape)
        move_times = shape_len // update_each_size
        total_num = update_each_size * (move_times - 1)

        # must 32b alignment
        if update_each_size % oneburst_num != 0:
            update_each_size = (update_each_size // oneburst_num + 1) \
                               * oneburst_num
        total_num = total_num + update_each_size

        return total_num

    def initial_output(self, process, output_offset, output_size):
        """
        init output as 0

        Parameters
        ----------
        process: ScatterProcess class,which used to store scatter nd parameters
        output_offset: the offset of output data
        output_size: each cycle process the data size

        Returns
        -------
        None
        """
        loop_times = self.tik_instance.Scalar("int32")
        loop_times.set_as(output_size / MAX_UB_ELEMENT_NUMBER)
        last_ub_size = self.tik_instance.Scalar("int32")
        last_ub_size.set_as(output_size % MAX_UB_ELEMENT_NUMBER)
        with self.tik_instance.if_scope(last_ub_size != 0):
            loop_times.set_as(loop_times + 1)
        with self.tik_instance.else_scope():
            last_ub_size.set_as(MAX_UB_ELEMENT_NUMBER)
        offset = self.tik_instance.Scalar("int32")
        offset.set_as(output_offset)
        total_size = self.tik_instance.Scalar("int32")
        total_size.set_as(MAX_UB_ELEMENT_NUMBER * self.data_size)

        with self.tik_instance.for_range(0, loop_times) as times:
            with self.tik_instance.if_scope(times == loop_times - 1):
                total_size.set_as(last_ub_size * self.data_size)
            repeats = common_util.get_vector_repeat_times(self.tik_instance,
                                                          total_size)
            nburst = common_util.get_datamove_nburst(self.tik_instance,
                                                     total_size)
            self.generate_input_data(process, repeats)
            with self.tik_instance.if_scope(total_size % \
                                            constant.BLOCK_SIZE != 0):
                self.move_out_non32_alignment(process, offset,
                                              total_size // self.data_size)
            with self.tik_instance.else_scope():
                self.tik_instance.data_move(self.output_y_gm[offset],
                                            process.input_ub,
                                            constant.SID,
                                            constant.DEFAULT_NBURST, nburst,
                                            constant.STRIDE_ZERO,
                                            constant.STRIDE_ZERO)
            offset.set_as(offset + total_size // self.data_size)

    def update_data(self, process, loop_cycle, output_offset):
        """
        update output data

        Parameters
        ----------
        process: ScatterProcess class,which used to store scatter nd parameters
        loop_cycle: each block's loop cycle
        output_offset: the offset of output

        Returns
        -------
        None
        """
        loop_size = MAX_UB_ELEMENT_NUMBER
        if MAX_UB_ELEMENT_NUMBER % process.get_each_size() != 0:
            loop_size = (MAX_UB_ELEMENT_NUMBER // process.get_each_size()) * \
                        process.get_each_size()
        total = get_shape_total_number(self.indices.get("shape"))
        if total <= MAX_UB_ELEMENT_NUMBER:
            loop_size = total
        loop_times, last_ub_size = get_loop_param(total, loop_size)
        total_size = self.tik_instance.Scalar("int32")
        total_size.set_as(loop_size * constant.DATA_SIZE_FOUR)
        offset = self.tik_instance.Scalar("int32")
        offset.set_as(0)

        with self.tik_instance.for_range(0, loop_times) as times:
            with self.tik_instance.if_scope(times == loop_times - 1):
                total_size.set_as(last_ub_size * constant.DATA_SIZE_FOUR)
            nburst = common_util.get_datamove_nburst(self.tik_instance,
                                                     total_size)
            self.tik_instance.data_move(process.input_indices_ub,
                                        self.input_indices_gm[offset],
                                        constant.SID, constant.DEFAULT_NBURST,
                                        nburst, constant.STRIDE_ZERO,
                                        constant.STRIDE_ZERO)
            offset.set_as(offset + loop_size)
            inner_loop_times = self.tik_instance.Scalar("int32")
            inner_loop_times.set_as(total_size // process.get_each_size() // \
                                    constant.DATA_SIZE_FOUR)
            loop_param = (times, loop_size, inner_loop_times, loop_cycle)
            self.process_each_indices(process, loop_param, output_offset)

    def process_each_indices(self, process, loop_param, output_offset):
        """
        process each indices

        Parameters
        ----------
        process: ScatterProcess class,which used to store scatter nd parameters
        loop_param: a tupe keep the loop params
        output_offset: the offset of output data

        Returns
        -------
        None
        """
        update_offset = self.tik_instance.Scalar("int32")
        update_offset.set_as(0)
        with self.tik_instance.for_range(0, loop_param[2]) as ind_cycle:
            start_address = self.tik_instance.Scalar("int32")
            start_address.set_as(0)
            for k in range(process.get_each_size()):
                indices_saclar = self.tik_instance.Scalar("int32")
                indices_saclar.set_as(0)
                indices_saclar.set_as(ind_cycle * process.get_each_size() + k)
                indices_saclar.set_as(process.input_indices_ub[indices_saclar])
                start_address.set_as(start_address + \
                                     indices_saclar * \
                                     process.elem_of_each_dim[k])
            end_address = self.tik_instance.Scalar("int32")
            end_address.set_as(
                output_offset + (loop_param[3] - 1) * process.update_each_size)
            with self.tik_instance.if_scope(tik.all(start_address \
                                                    >= output_offset,
                                                    start_address \
                                                    <= end_address)):
                update_offset.set_as((loop_param[0] * loop_param[1] // \
                                      process.get_each_size() + \
                                      ind_cycle) * process.update_each_size)
                self.update_each_slice(process, update_offset, start_address)

    def update_each_slice(self, process, update_offset, start_address):
        """
        update each update slice

        Parameters
        ----------
        process: ScatterProcess class,which used to store scatter nd parameters
        update_offset: the offset of gm update data
        start_address: the start_address of output data

        Returns
        -------
        None
        """
        output_offset = self.tik_instance.Scalar("int32")
        output_offset.set_as(start_address)
        total_size = self.tik_instance.Scalar("int32")
        total_size.set_as(MAX_UB_ELEMENT_NUMBER * self.data_size)
        with self.tik_instance.for_range(0,
                                         process.loop_update) as update_cycle:
            with self.tik_instance.if_scope(update_cycle == \
                                            process.loop_update - 1):
                total_size.set_as(process.last_update_ub_size * self.data_size)
            nburst = common_util.get_datamove_nburst(self.tik_instance,
                                                     total_size)
            repeats = common_util.get_vector_repeat_times(self.tik_instance,
                                                          total_size)
            self.tik_instance.data_move(process.input_update_ub,
                                        self.input_updates_gm[update_offset],
                                        constant.SID,
                                        constant.DEFAULT_NBURST, nburst,
                                        constant.STRIDE_ZERO,
                                        constant.STRIDE_ZERO)

            self.tik_instance.data_move(process.input_ub,
                                        self.output_y_gm[output_offset],
                                        constant.SID,
                                        constant.DEFAULT_NBURST, nburst,
                                        constant.STRIDE_ZERO,
                                        constant.STRIDE_ZERO)

            dtype = process.input_ub.dtype
            input_ub_fp16 = None
            if dtype == constant.DATA_TYPE_INT8 \
                    or dtype == constant.DATA_TYPE_UINT8:
                input_shape = process.input_update_ub.shape
                total_number = constant.DATA_SIZE_TWO * \
                               get_shape_total_number(input_shape)
                input_ub_fp16 = self.tik_instance.Tensor(
                    constant.DATA_TYPE_FP16,
                    (total_number,),
                    name="input_ub_fp16",
                    scope=tik.scope_ubuf)
                self.tik_instance.vconv(constant.MASK128, "", input_ub_fp16,
                                        process.input_ub,
                                        repeats * constant.DATA_SIZE_TWO,
                                        constant.STRIDE_ONE,
                                        constant.STRIDE_ONE,
                                        constant.REPEAT_STRIDE_EIGHT,
                                        constant.REPEAT_STRIDE_FOUR)
            element_num = self.tik_instance.Scalar("int32")
            element_num.set_as(total_size // self.data_size)

            self.add_same_indices(process, repeats, input_ub_fp16, element_num)
            with self.tik_instance.if_scope(tik.all(total_size % \
                                                    constant.BLOCK_SIZE != 0, \
                                                    process.update_each_size * \
                                                    self.data_size >= \
                                                    constant.BLOCK_SIZE)):
                self.move_out_non32_alignment(process, output_offset,
                                              element_num)
            with self.tik_instance.else_scope():
                self.tik_instance.data_move(self.output_y_gm[output_offset],
                                            process.input_ub,
                                            constant.SID,
                                            constant.DEFAULT_NBURST, nburst,
                                            constant.STRIDE_ZERO,
                                            constant.STRIDE_ZERO)
            output_offset.set_as(output_offset + element_num)
            update_offset.set_as(update_offset + element_num)

    def move_out_non32_alignment(self, process, output_address, total_number):
        """
        move out non32 alignment

        Parameters
        ----------
        process: ScatterProcess class,which used to store scatter nd parameters
        output_address: output offset of output data
        total_number: total element number

        Returns
        -------
        None
        """
        elements_number = constant.BLOCK_SIZE // self.data_size
        out_ub = self.tik_instance.Tensor(process.input_ub.dtype,
                                          (elements_number,),
                                          name="out_ub",
                                          scope=tik.scope_ubuf)

        nbursts = (total_number * self.data_size) // constant.BLOCK_SIZE
        scalar = self.tik_instance.Scalar(process.input_ub.dtype)
        with self.tik_instance.if_scope(nbursts == 0):
            offset = elements_number - total_number
            self.tik_instance.data_move(out_ub,
                                        self.output_y_gm[
                                            output_address - offset],
                                        constant.SID, constant.DEFAULT_NBURST,
                                        constant.DEFAULT_BURST_LEN,
                                        constant.STRIDE_ZERO,
                                        constant.STRIDE_ZERO)

            with self.tik_instance.for_range(0, total_number) as out_cycle:
                scalar.set_as(process.input_ub[out_cycle])
                out_ub[offset + out_cycle].set_as(scalar)
            self.tik_instance.data_move(
                self.output_y_gm[output_address - offset],
                out_ub,
                constant.SID, constant.DEFAULT_NBURST,
                constant.DEFAULT_BURST_LEN,
                constant.STRIDE_ZERO, constant.STRIDE_ZERO)

        with self.tik_instance.else_scope():
            self.tik_instance.data_move(self.output_y_gm[output_address],
                                        process.input_ub,
                                        constant.SID, constant.DEFAULT_NBURST,
                                        nbursts,
                                        constant.STRIDE_ZERO,
                                        constant.STRIDE_ZERO)
            offset = total_number - elements_number
            scalar = self.tik_instance.Scalar(process.input_ub.dtype)
            with self.tik_instance.for_range(0, elements_number) as time:
                scalar.set_as(process.input_ub[offset + time])
                out_ub[time].set_as(scalar)
            self.tik_instance.data_move(
                self.output_y_gm[output_address + offset], out_ub,
                constant.SID, constant.DEFAULT_NBURST,
                constant.DEFAULT_BURST_LEN,
                constant.STRIDE_ZERO, constant.STRIDE_ZERO)

    def generate_input_data(self, process, repeats):
        """
        generate data according to different datatype

        Parameters
        ----------
        process: ScatterProcess class,which used to store scatter nd parameters
        repeats: instructions repeats

        Returns
        -------
        input_ub_fp16: a ub tensor when process.input_ub.dtype is int8 \
                       or uint8 is effective
        """
        dtype = process.input_ub.dtype
        input_ub_fp16 = None
        if dtype == constant.DATA_TYPE_INT8 or dtype == \
                constant.DATA_TYPE_UINT8:
            total_number = constant.DATA_SIZE_TWO * \
                           get_shape_total_number(process.input_ub.shape)
            input_ub_fp16 = self.tik_instance.Tensor(constant.DATA_TYPE_FP16,
                                                     (total_number,),
                                                     name="input_ub_fp16",
                                                     scope=tik.scope_ubuf)
            self.tik_instance.vector_dup(constant.MASK128, input_ub_fp16,
                                         constant.INT_DEFAULT_ZERO,
                                         repeats * constant.DATA_SIZE_TWO,
                                         constant.STRIDE_ONE,
                                         constant.REPEAT_STRIDE_EIGHT)
            self.tik_instance.vconv(constant.MASK128, "", process.input_ub,
                                    input_ub_fp16,
                                    repeats * constant.DATA_SIZE_TWO,
                                    constant.STRIDE_ONE,
                                    constant.STRIDE_ONE,
                                    constant.REPEAT_STRIDE_FOUR,
                                    constant.REPEAT_STRIDE_EIGHT)
        else:
            self.tik_instance.vector_dup(256//self.data_size, process.input_ub,
                                         constant.INT_DEFAULT_ZERO,
                                         repeats, constant.STRIDE_ONE,
                                         constant.REPEAT_STRIDE_EIGHT)

        return input_ub_fp16

    def add_same_indices(self, process, repeats, input_ub_fp16, element_num):
        """
        add the data with the same indices

        Parameters
        ----------
        process: ScatterProcess class,which used to store scatter nd parameters
        repeats: instructions repeats
        input_ub_fp16: a ub tensor when process.input_ub.dtype is int8 or
                       uint8 is effective
        element_num: the number of elements

        Returns
        -------
        None
        """
        mask = constant.MASK128
        if process.update_each_size * self.data_size < constant.BLOCK_SIZE:
            mask = element_num
        dtype = process.input_ub.dtype
        if dtype == constant.DATA_TYPE_INT8 or dtype == \
                constant.DATA_TYPE_UINT8:
            total_number = constant.DATA_SIZE_TWO * \
                           get_shape_total_number(process.input_update_ub.shape)
            input_update_ub_fp16 = self.tik_instance.Tensor(
                constant.DATA_TYPE_FP16,
                (total_number,),
                name="input_update_ub_fp16",
                scope=tik.scope_ubuf)

            self.tik_instance.vconv(constant.MASK128, "", input_update_ub_fp16,
                                    process.input_update_ub,
                                    repeats * constant.DATA_SIZE_TWO,
                                    constant.STRIDE_ONE,
                                    constant.STRIDE_ONE,
                                    constant.REPEAT_STRIDE_EIGHT,
                                    constant.REPEAT_STRIDE_FOUR)
            self.tik_instance.vadd(mask, input_ub_fp16, input_ub_fp16,
                                   input_update_ub_fp16,
                                   repeats * constant.DATA_SIZE_TWO,
                                   constant.STRIDE_ONE,
                                   constant.STRIDE_ONE,
                                   constant.STRIDE_ONE,
                                   constant.REPEAT_STRIDE_EIGHT,
                                   constant.REPEAT_STRIDE_EIGHT,
                                   constant.REPEAT_STRIDE_EIGHT)
            self.tik_instance.vconv(constant.MASK128, "", process.input_ub,
                                    input_ub_fp16,
                                    repeats * constant.DATA_SIZE_TWO,
                                    constant.STRIDE_ONE,
                                    constant.STRIDE_ONE,
                                    constant.REPEAT_STRIDE_FOUR,
                                    constant.REPEAT_STRIDE_EIGHT)
        else:
            mask = 256//self.data_size
            if process.update_each_size * self.data_size < constant.BLOCK_SIZE:
                mask = element_num
            self.tik_instance.vadd(mask, process.input_ub,
                                   process.input_update_ub,
                                   process.input_ub,
                                   repeats, constant.STRIDE_ONE,
                                   constant.STRIDE_ONE,
                                   constant.STRIDE_ONE,
                                   constant.REPEAT_STRIDE_EIGHT,
                                   constant.REPEAT_STRIDE_EIGHT,
                                   constant.REPEAT_STRIDE_EIGHT)

class ScatterProcessBase(object):
    """
    Function: use to store scatter_nd  process parameters
    Modify : 2019-09-27
    """

    def __init__(self, tik_instance, updates, indices):
        """
        init scatter_nd  process parameters

        Parameters
        ----------
        tik_instance: tik_instance
        updates: dict,shape and datatype,datatype supports float32,
                 float16,int32,int8,uint8
        indices: dict,shape and datatype,datatype supports int32

        Returns
        -------
        None
        """
        self.input_update_ub = tik_instance.Tensor(updates.get("dtype").lower(),
                                                   (MAX_UB_ELEMENT_NUMBER,),
                                                   name="input_update_ub",
                                                   scope=tik.scope_ubuf)
        self.input_ub = tik_instance.Tensor(updates.get("dtype").lower(),
                                            (MAX_UB_ELEMENT_NUMBER,),
                                            name="input_ub",
                                            scope=tik.scope_ubuf)
        self.input_ub_tmp = tik_instance.Tensor(updates.get("dtype").lower(),
                                                (MAX_UB_ELEMENT_NUMBER,),
                                                name="input_ub_tmp",
                                                scope=tik.scope_ubuf)

        self.input_indices_ub = tik_instance.Tensor(
            indices.get("dtype").lower(),
            (MAX_UB_ELEMENT_NUMBER,),
            name="input_indices_ub",
            scope=tik.scope_ubuf)
        indices_shape = indices.get("shape")
        indice_len = get_indice_len(indices_shape)
        each_size = indices_shape[-1]
        self.ind_params = [indice_len, each_size]
        self.update_each_size = \
            get_shape_total_number(updates.get("shape")) // self.ind_params[0]

    def get_each_size(self):
        """
        get each size

        Parameters
        ----------
        None:

        Returns
        -------
        each_size: indices each_size
        """
        return self.ind_params[1]

    def get_indice_len(self):
        """
        get indices data size

        Parameters
        ----------
        None:

        Returns
        -------
        indices_data_size: indices data size
        """
        return self.ind_params[0]

class ScatterProcess(ScatterProcessBase):
    """
    Function: use to store scatter_nd  process parameters
    Modify : 2019-09-27
    """

    def __init__(self, tik_instance, updates, indices, shape):
        """
        init scatter_nd  process parameters

        Parameters
        ----------
        tik_instance: tik_instance
        updates: dict,shape and datatype,datatype supports float32,float16,
                 int32,int8,uint8
        indices: dict,shape and datatype,datatype supports int32
        shape: out put shape

        Returns
        -------
        None
        """
        super(ScatterProcess, self).__init__(tik_instance, updates, indices)
        self.loop_indices_cycle, self.last_ind_ub_size = get_loop_param(
            get_shape_total_number(indices.get("shape")),
            MAX_UB_ELEMENT_NUMBER)
        self.loop_update, self.last_update_ub_size = get_loop_param(
            self.update_each_size,
            MAX_UB_ELEMENT_NUMBER)
        self.loop_out_cycle, self.last_out_ub_size = get_loop_param(
            get_shape_total_number(shape),
            self.update_each_size)
        self.elem_of_each_dim = get_elem_of_each_dim(shape)

    def get_loop_out_cycle(self):
        """
        get loop out cycle

        Parameters
        ----------
        None

        Returns
        -------
        loop_out_cycle: loop out cycle
        """
        return self.loop_out_cycle

    def get_last_out_ub_size(self):
        """
        get last out ub size

        Parameters
        ----------
        None

        Returns
        -------
        last_out_ub_size: last out ub size
        """

        return self.last_out_ub_size

def get_loop_param(length, max_ub_num):
    """
    get loop parameters

    Parameters
    ----------
    length: total number
    max_ub_num: max of ub num

    Returns
    -------
    loop_cycle: loop cycle
    last_ub_num: the last data needs ub num
    """
    loop_cycle = length // max_ub_num
    last_ub_num = length % max_ub_num
    if last_ub_num != 0:
        loop_cycle = loop_cycle + 1
    else:
        last_ub_num = max_ub_num

    return loop_cycle, last_ub_num

def get_indice_len(indices_shape):
    """
    get indice len

    Parameters
    ----------
    indices_shape: indices shape

    Returns
    -------
    indice_len: the length of each slice
    """
    indice_len = 1
    for i in indices_shape[:-1]:
        indice_len = indice_len * i

    return indice_len

def get_elem_of_each_dim(shape):
    """
    get element of each dim

    Parameters
    ----------
    shape: out put shape

    Returns
    -------
    None
    """
    elem_of_each_dim = [1] * len(shape)
    for i in range(len(shape) - 1):
        j = i + 1
        while j < len(shape):
            elem_of_each_dim[i] = elem_of_each_dim[i] * shape[j]
            j = j + 1

    return elem_of_each_dim

def get_gm_number(oneburst_element_number, shape):
    """
    get gm number

    Parameters
    ----------
    oneburst_element_number: data move one burst element number
    shape: out put shape

    Returns
    -------
    None
    """
    gm_element_number = get_shape_total_number(shape)

    # must 32b alignment
    if gm_element_number % oneburst_element_number != 0:
        gm_element_number = (gm_element_number // oneburst_element_number + 1) \
                            * oneburst_element_number
    return gm_element_number

def get_shape_total_number(shape):
    """
    get the number of element from the shape

    Parameters
    ----------
    shape: out put shape

    Returns
    -------
    total_number: the number of element of the shape
    """
    total_number = len(shape)
    if total_number == 0:
        return 0
    total_number = 1
    for i in shape:
        total_number = total_number * i

    return total_number


import unittest
from .context import *
from pdf4py._decoders import ascii85decode, runlengthdecode, asciihexdecode
from binascii import hexlify

class DecodersTestCase(unittest.TestCase):

    def test_ascii85_decode(self):
        ascii85msg = b'6Z6LH+Co%nDe*F#+@/pn8P(m!~>'
        original = 'Code decodes ASCII85'.encode('ascii')
        self.assertEqual(original, ascii85decode(ascii85msg, None))

    def test_runlength_decode(self):
        rle = b'\x0bHello world.\x82c'
        decoded = b'Hello world.' + (257 - 130) * b'c'
        self.assertEqual(decoded, runlengthdecode(rle, None))

    def test_asciihexdecode(self):
        decoded = b"87cURD]i,\"Ebo80~>"
        encoded = b"3837635552445d692c2245626f38307e3e>"
        self.assertEqual(asciihexdecode(encoded, None), decoded)


import data as d
import numpy as np

def contributors(df, subsets):
  print('Breakdown of volunteers by number of projects contributed to')
  print('=' * len('Breakdown of volunteers by number of projects contributed to'))

  #Get a series listing projects contributed to, indexed by pseudonym and project
  #Although the values in this series are relevant to the problem, we never
  #actually use them -- operations on the index do everything that we need. So we
  #could store any value at all in here.
  projects = df.groupby(['pseudonym', 'project'])['project'].unique()

  #Get a series listing number of projects contributed to, indexed by pseudonym
  counts = df.groupby('pseudonym')['project'].nunique()

  c3 = counts[counts == 3]
  c2 = counts[counts == 2]
  c1 = counts[counts == 1]

  print(f'{"Contributors to all projects:":41}',       f'{len(c3):9}')
  print(f'{"Contributors to exactly 2 projects:":41}', f'{len(c2):9}')
  print(f'{"Contributors to exactly 1 project:":41}',  f'{len(c1):9}')

  print('-' * 30)

  mono_total = 0
  for project in d.HMS, d.SB, d.RBGE:
    #Starting with volunteers who contributed to 1 project, filter down to just the project of interest.
    x = projects.loc[c1.index] #Volunteers contributing to just one project
    x = x.index.get_level_values('project') == project #All rows with project of interest
    x = np.count_nonzero(x) #in this context, counts the Trues
    mono_total += x
    header = f'{project} only:'
    print(f'{header:41} {x:9}')
  print('Total mono contributors:', mono_total, '(should equal contributors to exactly one project, above)')

  print('-' * 30)

  pair_total = 0
  for pair, exclusion in [[[d.HMS, d.SB], d.RBGE], [[d.HMS, d.RBGE], d.SB], [[d.SB, d.RBGE], d.HMS]]:
    x = projects.loc[c2.index] #Volunteers contributing to exactly 2 projects
    x = projects.reindex(c2.index, level = 0)
    x = x[x.index.get_level_values('project') != exclusion]

    #Now each pseudonym that appears twice has contributed to both projects
    #As we started with all pseudonyms that appear exactly twice (x = project.loc[c2.index]), now
    #all pseudonyms appear once or twice. So by marking one instance of each duplicate as True, and
    #everything else as False, we get an array containing a number of Trues that is equal to the number
    #of contributors to each project.
    x = x.index.get_level_values('pseudonym').duplicated()
    x = np.count_nonzero(x) #in this context, counts the Trues

    pair_total += x
    header = f'{pair[0]} and {pair[1]} only:'
    print(f'{header:41} {x:9}')
  print('Total pair contributors:', pair_total, '(should equal contributors to exactly two projects, above)')

  print('-' * 30)

  for project in d.HMS, d.SB, d.RBGE:
    heading = f'Total contributors to {project}:'
    total = np.count_nonzero(projects.index.get_level_values('project') == project)
    print(f'{heading:41}', total)

    x = df[df.project == project] #All classifications on current project
    x = x.pseudonym.value_counts() #Number of classifications per volunteer on current project

    m_count = np.count_nonzero(x == 1)
    heading = f'Mono  contributors to {project}:'
    print(f'{heading:41}', m_count)

    p_count = np.count_nonzero(x != 1)
    heading = f'Multi contributors to {project}:'
    print(f'{heading:41}', p_count)

    assert m_count + p_count == total
    print('Mono + multi ==', m_count + p_count, f'(should equal total {total})')
#This is a way to do it all with one series, indexed by pseudonym and project, with values
#equal to the number of projects that the pseudonym has contributed to. It might not be any
#more efficient than the two-series solution above, and has the further disadvantage that we
#have to divide the counts[counts == 3] equivalent by 3, and counts[counts == 2] equivalent by
#2, as there will be one row for each pseudonum,project combination.
#count = df.groupby('pseudonym').project.nunique()
#count.name = 'proj_count'
#projects = df.join(count, on = 'pseudonym').set_index(['pseudonym', 'project'])['proj_count']
#dup_mask = projects.index.duplicated()
#unique_mask = ~dup_mask
#projects = projects[unique_mask]
#counts = projects


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

# Copyright 2014, Cercle Informatique ASBL. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This software was made by hast, C4, ititou at UrLab, ULB's hackerspace

import os
import sys
import json
from BeautifulSoup import BeautifulSoup

data = {}
a = 0
total = len(os.listdir("./cours/"))
for cours in os.listdir("./cours/"):
    a += 1
    sys.stdout.write("%s/%s\r" % (a, total))
    sys.stdout.flush()
    data[cours] = {}
    soup = BeautifulSoup(open("./cours/" + cours).read())
    if soup.find('table', 'bordertable') is None:
        continue
    for line in filter(lambda x: len(x('td')) == 2 and x.td.text, soup.find('table', 'bordertable')('tr')):
        key, value = line('td')
        data[cours][key.text] = value.text
    data[cours]["Nom"] = soup.find('td','dbtitle').p.childGenerator().next()

sys.stdout.write("\n")
open("cours.json", "w").write(json.dumps(data))


#!/usr/bin/env python
import sys
import re

get_data_regex = re.compile('NodeId:(\d*),?(\d*)\t(\d*.\d*),(\d*.\d*),?(.*)')

def line_parse(s):
    """line_parse takes a "NodeID..." string and returns a tuple of its
    identifier, current PageRank, previous PageRank, the nodes its node links
    to, and if this isn't the first iteration, the iteration number."""

    # Data is of form (index, [iteration,] current PageRank, previous PageRank,
    # list_of_neighbors)
    data = get_data_regex.match(s).groups()
    # If there is no iteration AKA first iteration
    if not data[1]:
        data = (data[0], '0', data[2], data[3], data[4].split(","))
    else:
        data = (data[0], data[1], data[2], data[3], data[4].split(","))
    return data

# Get data from Hadoop Streaming API.
for line in sys.stdin:

    if not line:
        continue

    data = line_parse(line)

    # output is either iNUM\trNUM which is part of the next PageRank of i
    # or iNUM\tvSTRINGofNUMS which is the way we're saving the data.
    if data[4][0] == "":
        sys.stdout.write('%s\tr%s\n' % (data[0], float(data[2])))
    else:
        for friend in data[4]:
            sys.stdout.write('%s\tr%s\n' % (friend, float(data[2]) / float(len(data[4]))))

    list_of_friends = data[4]

    if data[4][0] == '':
        list_of_friends = ''
    else:
        list_of_friends = ',' + ','.join(data[4])

    sys.stdout.write('%s\tv%s\n' % (data[0], data[1] + ',' + data[2] + ',' + data[3] + list_of_friends))


import socket
import time
print('For Honour Lindy Booth!!!')

class Client:
    def __init__(self, host, port, timeout=None):
        self.host = host
        self.port = port
        self.timeout = timeout
        self.sock = socket.create_connection((self.host, self.port), self.timeout)

    def put(self, metric, value, timestamp=None):
        # если не введен необязательный параметр то подставляем туда текущее время
        if not timestamp:
            timestamp = str(int(time.time()))
        message = f'put {metric} {float(value)} {timestamp}\n'
        try:
            self.sock.send(message.encode('utf8'))
            if self.sock.recv(1024).decode('utf8') == 'error\nwrong command\n\n':
                raise ClientError

        except socket.error:
            raise ClientError

    def get(self, key):
        # формируем сообщение
        message = f'get {key}\n'
        self.sock.send(message.encode('utf8'))
        try:
            # пытаемся получить его
            string = self.sock.recv(1024)
            string = string.decode('utf8')

            if string == 'error\nwrong command\n\n':
                raise ClientError

            elif string == 'ok\n\n':
                return {}
            else:
                # отсекаем первый ок и последние 2 символа
                split = string[3:-2].split('\n')
                dict_temp = {}
                for metric in split:
                    temp = metric.split()
                    if temp[0] not in dict_temp:
                        dict_temp[temp[0]] = []
                        dict_temp[temp[0]].append((int(temp[2]), float(temp[1])))
                    else:
                        dict_temp[temp[0]].append((int(temp[2]), float(temp[1])))
                return dict_temp

        except socket.error:
            raise ClientError

class ClientError(Exception):
    pass