import numpy as np
import galois
from nacl.public import PrivateKey, Box
from io import BytesIO

import cProfile
import re

# A library for Shamir sharing arrays of field elements
# - An ArrayShare object is one share of the whole array
#   - ArrayShare objects have encrypt and decrypt methods
# - share_array secret shares an array, returning a dict mapping x coordinates
#   to their corresponding ArrayShare objects
# - reconstruct_array reconstructs the original array, given a list of
#   ArrayShare objects
# - sum_share_array performs a column-wise sum of a list of ArrayShare objects
#   that all agree on the x coordinate

# convert a numpy array to bytes
def array_to_bytes(x: np.ndarray) -> bytes:
    np_bytes = BytesIO()
    np.save(np_bytes, x, allow_pickle=True)
    return np_bytes.getvalue()

# de-serialize a numpy array from bytes
def bytes_to_array(b: bytes) -> np.ndarray:
    np_bytes = BytesIO(b)
    return np.load(np_bytes, allow_pickle=True)

class ArrayShare:
    """One Shamir share of an array. Stores its x coordinate, and an array of y coordinates,
       one y coordinate per element of the original array. All of the x coordinates must match."""
    def __init__(self, x, ys, T, GF, K=1, encrypted=False):
        self.x = x
        self.ys = ys
        self.GF = GF
        self.T = T
        self.K = K
        self.encrypted = encrypted

    def encrypt(self, sk, pk):
        assert not self.encrypted
        b = array_to_bytes(self.ys)
        enc_b = Box(sk, pk).encrypt(b)
        return ArrayShare(self.x, enc_b, self.T, self.GF, K=self.K, encrypted=True)

    def decrypt(self, sk, pk):
        assert self.encrypted
        m = Box(sk, pk).decrypt(self.ys)
        array = bytes_to_array(m)
        return ArrayShare(self.x, array, self.T, self.GF, K=self.K, encrypted=False)

    def __str__(self):
        return f'ArrayShare(x={self.x}, len={len(self.ys)}, T={self.T}, K={self.K}, enc={self.encrypted})'

    __repr__ = __str__

def reshape(secrets, K, GF):
    if len(secrets) %K == 0:
        return secrets.reshape((len(secrets)//K, K))

    true_len = (len(secrets)//K + 1) * K
    flat_pad = GF.Zeros((true_len))
    flat_pad[:len(secrets)] = secrets
    return flat_pad.reshape((len(secrets)//K + 1), K)

def share_packed(secrets, range_shares, T, K, GF):
    """
    secrets: flat array
    """
    secrets = reshape(secrets, K, GF)
    secrets = np.atleast_2d(secrets)
    p_size = (secrets.shape[0], K + T - 1)

    poly_points = GF.Random((p_size))
    poly_points[:, :K] = secrets
    xs = GF.Range(0, p_size[1])
    polys = [galois.lagrange_poly(xs, pps) for pps in poly_points]
    shares = {x: ArrayShare(x+K, GF(np.array([poly(x+K) for poly in polys])), T, GF, K=K) \
              for x in range_shares}
    return shares

def share_array(secrets, range_shares, T, GF, K=1):
    """Secret shares an array of secrets. Returns a dict mapping the x coordinate of each share
       to an ArrayShare object with that x coordinate."""
    return share_packed(secrets, range_shares, T, K, GF)

def reconstruct_array(array_shares):
    """Given a list of ArrayShare objects, reconstructs the original array"""
    assert len(array_shares) > 0
    array_len = len(array_shares[0].ys)
    GF = array_shares[0].GF
    T = array_shares[0].T
    K = array_shares[0].K

    assert len(array_shares) >= T + K, f'we have {len(array_shares)} shares, and we need {T + K}'

    # Error checking
    for s in array_shares:
        assert len(s.ys) == array_len
        assert s.GF == GF
        assert s.T == T
        assert s.K == K

    # Reconstruction
    arr = []
    xs = GF([s.x for s in array_shares])
    for i in range(array_len):
        # TODO: check T
        ys = GF([s.ys[i] for s in array_shares])
        poly = galois.lagrange_poly(xs, ys)
        arr.extend([poly(i) for i in range(0, K)])

    return GF(arr)

def sum_share_array(shares):
    """Given a list of ArrayShare objects with matching x coordinates, returns a new
       ArrayShare object representing the column-wise sum of the input shares"""
    assert len(shares) > 0
    x = shares[0].x
    GF = shares[0].GF
    T = shares[0].T
    K = shares[0].K

    for s in shares:
        assert not s.encrypted
        assert s.x == x
        assert s.GF == GF
        assert s.T == T
        assert s.K == K

    share_matrix = GF([s.ys for s in shares])
    sum_ys = share_matrix.sum(axis=0)

    return ArrayShare(x, sum_ys, T, GF, K=K, encrypted=False)

def prof():
    for _ in range(1):
        GF = galois.GF(2**31-1)
        vals = GF(np.random.randint(5, 6, 500))
        shares = share_array(vals, range(1,65), 4, GF, K=50)
        #print(shares)
        r = reconstruct_array(list(shares.values()))
        print(r)

if __name__ == '__main__':
    prof()
    #cProfile.run('prof()', sort='cumtime')


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

from . import routes, views, model
from . import listeners  # noqa

MODELS = [model.OsfStorageNodeSettings]
NODE_SETTINGS_MODEL = model.OsfStorageNodeSettings

ROUTES = [
    routes.api_routes
]

SHORT_NAME = 'osfstorage'
FULL_NAME = 'OSF Storage'

OWNERS = ['node']

ADDED_DEFAULT = ['node']
ADDED_MANDATORY = ['node']

VIEWS = []
CONFIGS = []

CATEGORIES = ['storage']

INCLUDE_JS = {
    'widget': [],
    'page': [],
    'files': [],
}

HAS_HGRID_FILES = True
GET_HGRID_DATA = views.osf_storage_root

MAX_FILE_SIZE = 5 * 1024  # 5 GB
HIGH_MAX_FILE_SIZE = 5 * 1024  # 5 GB

# HERE = os.path.dirname(os.path.abspath(__file__))
NODE_SETTINGS_TEMPLATE = None  # no node settings view
USER_SETTINGS_TEMPLATE = None  # no user settings view


jumlahHari =  int(input("Masukkan Jumlah Hari : "))

jumlahTahun  = 0
jumlahBulan  = 0

while(jumlahHari >= 365):
 jumlahHari = jumlahHari - 365
 jumlahTahun = jumlahTahun + 1

while(jumlahHari >= 30):
 jumlahHari = jumlahHari - 30
 jumlahBulan = jumlahBulan + 1

print(jumlahTahun,"Tahun",jumlahBulan,"Bulan",jumlahHari,"Hari")


                # Static Class
class student:
    def stuinput(name,rollno,fee):
        student.name = name
        student.rollno = rollno
        student.fee = fee
    def stuoutput():
        print("Student Name=",student.name)
        print("Student Rollno=",student.rollno)
        print("Student Fee=",student.fee)

student.stuinput("Gagan",1,12.12)
student.stuoutput()


from collections import deque

class Solution:
    def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
        reach = {}
        for time in times:
            if time[0] in reach:
                reach[time[0]].append([time[2], time[1]])
            else:
                reach[time[0]] = [[time[2], time[1]]]

        distanceReached = [inf for _ in range(n + 1)]
        distanceReached[0], distanceReached[k] = 0, 0

        queue = deque([k])
        while queue:
            node = queue.popleft()

            if node not in reach:
                continue

            for elem in reach[node]:
                time, nextVal = elem
                if distanceReached[node] + time < distanceReached[nextVal]:
                    distanceReached[nextVal] = distanceReached[node] + time
                    queue.append(nextVal)

        delay = max(distanceReached)
        if delay == inf:
            return -1
        return delay


# Operators In python
# Arithmetic Operators
# Assignment Operators
# Comparsion Operators
# Logical Operators
# Identity Operators
# Membership Operators
# Bitwise Operators

# Arithmetic Operators
# print("5+6 is",5+6)
# print("5-6 is",5-6)
# print("5*6 is",5*6)
# print("5/6 is",5/6)
# print("5**3 is",5**3)
# print("5 % 5 is",5%5)
# print("15//6 is",15//6)

# print("Assignement")
# # Assignment Operators
# x=5
# print(x)
# x%=7 #x=x%7
# print(x)

# Comparsion Operators
i=5
# print(i and 5)

# Logical Operators
a=True
b=False

# Identity Operators
# print(5 is not 5)

# Membership Operators
list=[3,3,2,2,39,33,35,32]
# print(324 not in list)

# Bitwise Operators
# 0-00
# 1-01
# 2- 10
# 3- 11

print(0&2)
print(0 | 3)


from typing import Any, Callable, Dict, Optional

import torch
import torch.nn as nn

from .gaussian_diffusion import GaussianDiffusion
from .k_diffusion import karras_sample

DEFAULT_KARRAS_STEPS = 64
DEFAULT_KARRAS_SIGMA_MIN = 1e-3
DEFAULT_KARRAS_SIGMA_MAX = 160
DEFAULT_KARRAS_S_CHURN = 0.0

def uncond_guide_model(
    model: Callable[..., torch.Tensor], scale: float
) -> Callable[..., torch.Tensor]:
    def model_fn(x_t, ts, **kwargs):
        half = x_t[: len(x_t) // 2]
        combined = torch.cat([half, half], dim=0)
        model_out = model(combined, ts, **kwargs)
        eps, rest = model_out[:, :3], model_out[:, 3:]
        cond_eps, uncond_eps = torch.chunk(eps, 2, dim=0)
        half_eps = uncond_eps + scale * (cond_eps - uncond_eps)
        eps = torch.cat([half_eps, half_eps], dim=0)
        return torch.cat([eps, rest], dim=1)

    return model_fn

def sample_latents(
    *,
    batch_size: int,
    model: nn.Module,
    diffusion: GaussianDiffusion,
    model_kwargs: Dict[str, Any],
    guidance_scale: float,
    clip_denoised: bool,
    use_fp16: bool,
    use_karras: bool,
    karras_steps: int,
    sigma_min: float,
    sigma_max: float,
    s_churn: float,
    device: Optional[torch.device] = None,
    progress: bool = False,
) -> torch.Tensor:
    sample_shape = (batch_size, model.d_latent)

    if device is None:
        device = next(model.parameters()).device

    if hasattr(model, "cached_model_kwargs"):
        model_kwargs = model.cached_model_kwargs(batch_size, model_kwargs)
    if guidance_scale != 1.0 and guidance_scale != 0.0:
        for k, v in model_kwargs.copy().items():
            model_kwargs[k] = torch.cat([v, torch.zeros_like(v)], dim=0)

    sample_shape = (batch_size, model.d_latent)
    with torch.autocast(device_type=device.type, enabled=use_fp16):
        if use_karras:
            samples = karras_sample(
                diffusion=diffusion,
                model=model,
                shape=sample_shape,
                steps=karras_steps,
                clip_denoised=clip_denoised,
                model_kwargs=model_kwargs,
                device=device,
                sigma_min=sigma_min,
                sigma_max=sigma_max,
                s_churn=s_churn,
                guidance_scale=guidance_scale,
                progress=progress,
            )
        else:
            internal_batch_size = batch_size
            if guidance_scale != 1.0:
                model = uncond_guide_model(model, guidance_scale)
                internal_batch_size *= 2
            samples = diffusion.p_sample_loop(
                model,
                shape=(internal_batch_size, *sample_shape[1:]),
                model_kwargs=model_kwargs,
                device=device,
                clip_denoised=clip_denoised,
                progress=progress,
            )

    return samples


"""
main.py

train the deep iamge prior model and get the denoised figure, calculate PSNR when required.
"""
import hydra
from pytorch_lightning import Trainer, seed_everything

from src.conf import Config
from src.data.datamodule import DeepImagePriorDataModule
from src.model.model import DeepImagePriorModel

import logging
logging.getLogger("lightning").setLevel(logging.ERROR)

@hydra.main(config_path=".", config_name="base_config", version_base="1.2")
def train_app(conf: Config) -> None:
    """
    The main train loop.
    """
    # * seed
    if conf.train.random_seed:
        seed_everything(conf.train.random_seed)

    for idx, fig_dir in enumerate(conf.data.root_dir.iterdir()):
        if fig_dir.is_dir():
            light_data = DeepImagePriorDataModule(conf.data, img_dir=fig_dir)
            light_data.setup(stage="fit")
            print(f"train fig{idx}, key: {light_data.dataset[0]['key']}")
            light_model = DeepImagePriorModel(conf)

            train_conf = conf.train
            trainer = Trainer(
                accelerator=train_conf.accelerator,
                devices=(
                    train_conf.distributed_devices if train_conf.accelerator == "gpu" else None),
                max_epochs=train_conf.epochs,
                num_sanity_val_steps=0,
                check_val_every_n_epoch=train_conf.check_val_every_n_epoch,
                enable_progress_bar=True
            )
            trainer.fit(light_model, light_data)
        if idx == conf.data.total_run_figs_number-1:
            break

if __name__ == "__main__":
    train_app()  # pylint: disable=no-value-for-parameter


#!/usr/bin/python3

"""
Given an integer, convert it into a binary string

Assume integer >= 0

Would ask interviewer max int size (in bits) - for today will assume 32

"""

def intToBinary(i):
    # Don't need multiplier because we're returning string
    result = ''

    if i == 0: return '0'

    # While number isn't zero, shift right
    # Get first binary digit using mask

    # This reverses binary digits LOL, wrong problem dude
    # while i != 0:
        # result += str(i&1)
        # i >>= 1

    # Extremely inefficent algorithm
    # Time complexity: O(N^2) - Has to allocate space for characters in each newly concatenated string
    # while i != 0:
        # result = str(i&1) + result
        # i >>= 1

    # Find highest power of two
    # Then subtract. If power of 2 goes into number then append '1'
    # If power of 2 greater than number at any iteration, append '0' instead

    # Time Complexity: O(D) - where D is the number of binary digits a number has
    j = i
    power = -1

    while j != 0:
        # print(f"j is {bin(j)}")
        j >>= 1
        power += 1

    while power >= 0:
        # print(f"power is {power}")
        # print(f"i is {i}")
        if (2**power) <= i:
            result += '1'
            i -= 2**power

        else: result += '0'

        power -= 1

    return result


import asyncio

import aiohttp

from warnings import warn

with open("vk_access_token.txt", mode="r") as file:
    vk_access_token = file.read()
vk_api_version = "5.154"
owner_id = "-160464793"
url = f"https://api.vk.ru/method/wall.get?v={vk_api_version}&owner_id={owner_id}&count=1&access_token={vk_access_token}"

async def get_last_text_message():
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            a = await response.json()
            try:
                a = a["response"]["items"][0]["text"]
            except:
                a = None
            return a

async def ticker(delay):
    while True:
        yield await get_last_text_message()
        await asyncio.sleep(delay)

async def set_morning_trigger(trigger_function, delay=30):
    if delay < 28:
        warn("The delay is too small. Limit of wall.get usage per day may be exceeded.")
    generator = ticker(delay)
    async def get_last_text():
        return await generator.asend(None)
    text = ""
    while True:
        new_text = await get_last_text()
        if new_text != None and text != new_text:
            text = new_text
            asyncio.gather(trigger_function(text))

if __name__ == "__main__":
    async def function(string):
        print(string)
    asyncio.run(set_morning_trigger(function))


def sortSplit(array):
    n = len(array)
    if n == 1 or n == 0:
        return array
    n //= 2
    arrLeft = sortSplit(array[0:n])
    arrRight = sortSplit(array[n:])

    leftN = rightN = k = 0
    result = [0] * (len(arrLeft) + len(arrRight))
    while leftN < len(arrLeft) and rightN < len(arrRight):
        if arrLeft[leftN] <= arrRight[rightN]:
            result[k] = arrLeft[leftN]
            leftN += 1
        else:
            result[k] = arrRight[rightN]
            rightN += 1
        k += 1
    while leftN < len(arrLeft):
        result[k] = arrLeft[leftN]
        leftN += 1
        k += 1
    while rightN < len(arrRight):
        result[k] = arrRight[rightN]
        rightN += 1
        k += 1
    return result

print(sortSplit([3,6,1,8,9,8,8]))


# encoding:utf-8
__author__ = 'shiliang'
__date__ = '2019/4/9 21:12'

import requests
from lxml import etree
import pandas as pd
import xlrd
import time
import re
import aiohttp
import asyncio

# 全局变量
headers = {
    'Cookie': 'OCSSID=sfg10a19had6hfavkctd32otf6',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
                  '(KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
}

sem = asyncio.Semaphore(20)  # 信号量，控制协程数，防止爬的过快
oneSheetList = []            # 一张整表的元数据列表

def getURLlist(openFile, sheetNum, colx, start_rowx):
    '''
    获取excel指定sheet指定列指定范围行的数据
    :param openFile: excel路径
    :param sheetNum: sheet序号
    :param colx: 获取指定列数
    :param start_rowx: 起始行数
    :return urls: 一个包含所有URL的列表
    '''
    # 打开工作簿
    data = xlrd.open_workbook(openFile)
    # 选定sheet
    table = data.sheets()[sheetNum]
    # 获取excel表的指定列,start_rowx=1表示从第2行开始(从0计数)
    urls = table.col_values(colx=colx, start_rowx=start_rowx)
    return urls

def cleanStr(string):
    '''
    清洗字符串,如去除HTML标签等
    :param string: 待清洗字符串
    :return clean_str: 清洗后的较干净字符串
    '''
    # 1.使用strip函数清洗去除每个字符串的首尾\n和\t,此处也可写成\t\n
    clean_str = string.strip('\n\t')
    # 2.使用正则清洗掉HTML标签<>、/**/中的内容
    clean_str = re.compile(r'\<.*?\>|\/\*.*?\*\/').sub('', clean_str)
    return clean_str

async def getOneURLMetaData(number, url):
    '''
    使用协程异步获取多个URL的元数据
    :param number: 第number个url(整型),用以在最后结果集中排序
    :param url: url链接(字符串类型)
    :param [number,url]: 已完成爬取+解析+清洗的编号和URL列表
    '''
    with(await sem):
        # async with是异步上下文管理器
        async with aiohttp.ClientSession() as session:  # 获取session
            async with session.request('GET', url, headers=headers, timeout=20) as resp:  # 提出请求
                html = await resp.read()  # 可直接获取bytes
                # 解析数据
                xml = etree.HTML(html)
                content = xml.xpath('//*[@id="content"]/table/tr/td/text()')
                # 格式化数据
                oneURLList = []      # 存储一个URL包含所有元数据的列表
                creatorList = []  # 当前URL所有作者、机构和国家信息列表,默认为'0'
                title = ''           # 当前URL标题
                abstract = ''        # 当前URL摘要
                keywords = '0'       # 当前URL关键字
                for index, text in enumerate(content):
                    # '\xa0'是一行的首元素和尾元素,表示一行的开头或结尾
                    if text == '\xa0':
                        # 1.判断是否是'Title'行
                        if content[index+2] == 'Title':
                            title = content[index + 4]            # 保存Title
                            title = cleanStr(title)               # 清洗title
                            continue
                        if content[index+3] == 'Abstract':
                            abstract = content[index + 4]         # 保存Abstract
                            continue
                        if content[index+3] == 'Keyword(s)':
                            # 如果Keyword(s)不为空则填,为空则默认字符'0'
                            if content[index+4] != '\xa0':
                                keywords = content[index + 4]     # 保存Keyword(s)
                                keywords = cleanStr(keywords)     # 清洗keywords
                            continue
                        if content[index+2] == 'Creator':
                            clean_creator = cleanStr(content[index + 4])
                            lst = clean_creator.split('; ')     # 使用切片函数以"; "把字符串分割成三份，返回一个列表
                            for num, info in enumerate(lst):  # 因存在官网元数据少录入情况,故对于少录入数据项默认为'0'
                                # 如果是官网数据录入错误,超过三个数据,则直接跳出循环
                                if num > 2:
                                    break
                                creatorList.append(info)  # 作者名字、机构、国家
                                # 如果是官网数据录入错误, 少于三个数据, 则最最后一个元素补'0'
                                if len(lst) < 3 and num == 1:
                                    creatorList.append('0')  # 作者名字、机构、国家
                            continue
                oneURLList.append(number)
                oneURLList.append(title)
                oneURLList.append(abstract)
                oneURLList.append(keywords)
                oneURLList.append(creatorList)
                # 存储oneURLList的最后一个元素
                creatorList = oneURLList[-1]
                # 删除oneURLList的最后一个元素,切片取列表中的[0,-1)
                oneURLList = oneURLList[:-1]
                # 将creator列表拆开一个一个添加到oneURLList后
                for info in creatorList:
                    oneURLList.append(info)
                oneSheetList.append(oneURLList)
                print('已完成第'+str(number)+'个url的爬取+解析+清洗')
                return [number,url]

async def main(urls):
    '''
    协程调用方,异步获取所有url的元数据列表
    :param urls: URL列表
    :param topCount: URL列表中前几个URL的个数(可选参数)
    '''
    tasks = [getOneURLMetaData(number+1, url) for number, url in enumerate(urls)]  # 把所有任务放到一个列表中
    done,pending = await asyncio.wait(tasks) # 子生成器
    for r in pending: # done和pending都是一个任务，所以返回结果需要逐个调用result()
        print('爬取失败的url：'+r.result())

def coroutine_getMetaData(urls, topCount=None):
    '''
    协程调用总函数,异步获取所有url的元数据列表
    :param urls: URL列表
    :param topCount: URL列表中前几个URL的个数(可选参数)
    '''
    urlsList = []
    if topCount is not None:
        for i in range(topCount):
            urlsList.append(urls[i])
    else:
        urlsList = urls
    # 以下是协程调用
    loop = asyncio.get_event_loop()  # 创建一个事件循环对象loop
    try:
        loop.run_until_complete(main(urlsList))  # 完成事件循环,直到最后一个任务结束
    finally:
        loop.close()  # 结束事件循环

def list2excel(saveFile, oneSheetList, startrow, startcol=2, sheet_name='Sheet1'):
    '''
    列表写入到excel中的指定行和指定列中
    :param saveFile: 存储excel文件路径
    :param oneSheetList: 一个存储一个Sheet元数据的列表
    :param startrow: 该url位于excel表格中的行数
    :param startcol: 写入excel表格中的起始列
    :param sheet_name: 写入的sheet页名称
    :return:
    '''
    df = pd.DataFrame(oneSheetList)
    # df = df.T  # 矩阵转置,变成行向量
    # na_rep缺省值填充参数;index=False去掉行索引;header=False去掉表头
    df.to_excel(saveFile, sheet_name=sheet_name, startrow=startrow, startcol=startcol,
                  index=False, header=False)
    print('数据写入excel成功.')

if __name__ == '__main__':
    openFile = 'C:\\Users\\Administrator\\Desktop\\2014-2017.xlsx'
    saveFile = 'C:\\Users\\Administrator\\Desktop\\2017.xlsx'
    # 从excel中获取url,返回一个列表
    urls = getURLlist(openFile, sheetNum=0, colx=1, start_rowx=1)
    # 通过协程异步获取所有URL中的元数据,存储在oneSheetList列表中
    start = time.time()
    coroutine_getMetaData(urls)
    print('总爬取+解析耗时：%.5f秒' % float(time.time() - start))
    # 最后对嵌套列表的列表oneSheetList进行排序,key输入的是函数,item[0]表示列表的第1个元素
    oneSheetList.sort(key=lambda item: item[0], reverse=False)
    # 存储到excel中
    list2excel(saveFile, oneSheetList, startrow=0, startcol=0, sheet_name='Sheet1')


def fact(n):
    res = 1
    while n>1:
        res *= n
        n -= 1
    print(res)

def recurFact(n):
    if n == 0:
        return 1
    return n * recurFact(n-1)

n = int(input())
fact(n)
print(recurFact(n))


import rospy, math, numpy, tf
from collections import deque
from spencer_bagfile_tools.msg import AdditionalOdometryData
from dynamic_reconfigure.server import Server
from spencer_bagfile_tools.cfg import ReconstructOdometryConfig

from visualization_msgs.msg import MarkerArray, Marker
from geometry_msgs.msg import Point, Quaternion
from std_msgs.msg import ColorRGBA
from nav_msgs.msg import Odometry

class State(object):
    def __init__(self):
        self.x = self.y = self.theta = 0
        self.totalDistance = 0
        self.stamp = rospy.Time(0)

class OdometryController(object):
    def __init__(self):
        self.msgHistory = []
        self.stateHistory = self.emptyStateHistory()
        self.previousMsg = self.previousState = None
        self.rebuildingEntirePath = False

        self.zeroPosition()

        self.WHEEL_BASE = 0.665
        self.TICKS_PER_METER_LEFT = 56263.5
        self.TICKS_PER_METER_RIGHT = 57099.7

        self.previousTimestampMarkerCount = 0

    def zeroPosition(self):
        self.stateHistory.append(State())
        self.previousState = self.stateHistory[0]

    def run(self):
        self.markerArrayPublisher = rospy.Publisher("/spencer_bagfile_tools/reconstructed_odom_path", MarkerArray, queue_size=1)
        self.odomPublisher = rospy.Publisher("/spencer/sensors/odom", Odometry, queue_size=3)

        reconfigureServer = Server(ReconstructOdometryConfig, self.reconfigure)

        topicName = "/spencer/sensors/additional_odom_data"
        self.subscriber = rospy.Subscriber(topicName, AdditionalOdometryData, self.additionalOdometryDataCallback)

        rospy.loginfo("Reconstructing odometry from " + topicName + ", now listening for messages...")
        rospy.spin()

    def additionalOdometryDataCallback(self, msg):
        if not self.rebuildingEntirePath:
            self.updateState(msg)

        self.msgHistory.append(msg)
        self.publishOdom()
        self.visualizePath()

    def reconfigure(self, config, level):
        self.extraCalibOverallMultiplier = config["extra_calib_overall_multiplier"]
        self.extraCalibLeftMultiplier = config["extra_calib_left_multiplier"]
        self.lineWidth = config["line_width"]
        self.arrowLength = config["arrow_length"]
        self.showWaypoints = config["show_waypoints"]
        self.recalculatePath = config["recalculate_path"]

        if level > 0 and self.recalculatePath:
            self.rebuildEntirePath()

        return config

    def emptyStateHistory(self):
        # Limit max. state history length to prevent bad performance after driving for a while
        # NOTE: msgHistory might still grow unboundedly, but there's no way of avoiding that...
        # However, that is mainly a memory issue as the whole history is only processed in rebuildEntirePath()
        return deque(maxlen=5000)

    def rebuildEntirePath(self):
        rospy.loginfo("Odometry parameters have changed! Rebuilding entire path!")
        if self.rebuildingEntirePath:
            return
        self.rebuildingEntirePath = True

        self.stateHistory = self.emptyStateHistory()
        self.zeroPosition()

        self.previousMsg = None
        for msg in self.msgHistory:
            self.updateState(msg)

        self.rebuildingEntirePath = False
        self.publishOdom()
        self.visualizePath()

    def updateState(self, msg):
        newState = State()
        newState.stamp = msg.header.stamp

        previousLeftTicks = self.previousMsg.ticksLeft if self.previousMsg else msg.ticksLeft
        previousRightTicks = self.previousMsg.ticksRight if self.previousMsg else msg.ticksRight

        leftDiff = msg.ticksLeft - previousLeftTicks
        rightDiff = msg.ticksRight - previousRightTicks

        # Calculate metric travelled distances of both wheels and the base
        metersTravelledLeft = leftDiff * msg.calibOverallMultiplier * self.extraCalibOverallMultiplier * msg.calibLeftEncMultiplier * self.extraCalibLeftMultiplier / self.TICKS_PER_METER_LEFT
        metersTravelledRight = rightDiff * msg.calibOverallMultiplier * self.extraCalibOverallMultiplier / self.TICKS_PER_METER_RIGHT
        distance = (metersTravelledLeft + metersTravelledRight) / 2.0

        # Update position and bearing
        newState.theta = self.previousState.theta + (metersTravelledLeft - metersTravelledRight) / self.WHEEL_BASE
        newState.theta -= (int((newState.theta/(2*math.pi) ))) * 2*math.pi  # clip to 2pi

        newState.totalDistance = self.previousState.totalDistance + math.fabs(distance)

        newState.x = self.previousState.x + distance * math.sin(newState.theta)
        newState.y = self.previousState.y + distance * math.cos(newState.theta)

        positionTolerance = 0.1 # in meters
        if math.hypot(newState.x - self.stateHistory[-1].x, newState.y - self.stateHistory[-1].y) > positionTolerance:
            # Do not cache every single state if the change in position is minimal, otherwise we'll soon run
            # out of memory (note we still store previousState, since it is needed by publishOdom() and updateState())
            self.stateHistory.append(newState)

        self.previousState = newState # FIXME
        self.previousMsg = msg

    def publishOdom(self):
        odom = Odometry()
        odom.header.stamp = self.previousMsg.header.stamp if self.previousMsg else rospy.Time.now()
        odom.header.frame_id = "odom"

        odom.pose.pose.position.x = self.previousState.x
        odom.pose.pose.position.y = self.previousState.y

        for row in xrange(0, 6):
            for col in xrange(0, 6):
                odom.pose.covariance[6*row+col] = 0 if row != col else 0.1
                odom.twist.covariance[6*row+col] = 0 if row != col else 999999

        q = tf.transformations.quaternion_from_euler(0, 0, -self.previousState.theta + math.pi/2)
        odom.pose.pose.orientation = Quaternion(x=q[0], y=q[1], z=q[2], w=q[3])

        if len(self.stateHistory) >= 2:
            odom.twist.twist.linear.x = odom.pose.pose.position.x - self.stateHistory[-2].x
            odom.twist.twist.linear.y = odom.pose.pose.position.y - self.stateHistory[-2].y

        self.odomPublisher.publish(odom)

    def visualizePath(self):
        if self.markerArrayPublisher.get_num_connections() <= 0:
            return

        markerArray = MarkerArray()

        pathMarker = Marker()
        pathMarker.header.stamp = rospy.Time.now()
        pathMarker.header.frame_id = "odom"
        pathMarker.ns = "Path"
        pathMarker.type = Marker.LINE_STRIP
        pathMarker.id = 0
        pathMarker.color = ColorRGBA(r=1, g=1, a=1)
        pathMarker.scale.x = 0.05 * self.lineWidth

        waypointMarker = Marker()
        waypointMarker.header = pathMarker.header
        waypointMarker.ns = "Waypoints"
        waypointMarker.type = Marker.SPHERE_LIST
        waypointMarker.id = 1
        waypointMarker.color = ColorRGBA(r=1, g=1, a=1)
        waypointMarker.scale.x = waypointMarker.scale.y = 0.1 * self.lineWidth

        lastWaypointTime = float("-inf")
        lastWaypointPos = (float("99999"), float("99999"))

        # Generate path and waypoints
        for state in self.stateHistory:
            pathMarker.points.append(Point(x=state.x, y=state.y))

            if state.stamp.to_sec() - lastWaypointTime > 5 and self.showWaypoints:
                dx = state.x - lastWaypointPos[0]
                dy = state.y - lastWaypointPos[1]
                if math.sqrt(dx*dx + dy*dy) > 1:
                    lastWaypointTime = state.stamp.to_sec()
                    lastWaypointPos = (state.x, state.y)
                    waypointMarker.points.append(Point(x=state.x, y=state.y))

                    timestampMarker = Marker()
                    timestampMarker.header = waypointMarker.header
                    timestampMarker.ns = "Timestamps"
                    timestampMarker.type = Marker.TEXT_VIEW_FACING
                    timestampMarker.id = 3 + len(markerArray.markers)
                    timestampMarker.color = ColorRGBA(r=0.6, a=1)
                    timestampMarker.scale.z = 0.1 * self.lineWidth
                    timestampMarker.pose.position.x = state.x
                    timestampMarker.pose.position.y = state.y
                    timestampMarker.text = "%.1f" % state.stamp.to_sec()
                    markerArray.markers.append(timestampMarker)

        # Delete old markers
        currentTimestampMarkerCount = len(markerArray.markers)
        for i in xrange(0, self.previousTimestampMarkerCount - currentTimestampMarkerCount):
            timestampMarker = Marker()
            timestampMarker.header = waypointMarker.header
            timestampMarker.ns = "Timestamps"
            timestampMarker.action = Marker.DELETE
            timestampMarker.id = 3 + currentTimestampMarkerCount + i
            markerArray.markers.append(timestampMarker)

        self.previousTimestampMarkerCount = currentTimestampMarkerCount

        # Velocity arrow
        velocitySmoothingNoPoints = 5
        if len(pathMarker.points) > velocitySmoothingNoPoints:
            arrowHeadMarker = Marker()
            arrowHeadMarker.header = pathMarker.header
            arrowHeadMarker.ns = "Path-ArrowHead"
            arrowHeadMarker.type = Marker.LINE_STRIP
            arrowHeadMarker.id = 2
            arrowHeadMarker.color = ColorRGBA(r=1, g=1, a=1)
            arrowHeadMarker.scale.x = arrowHeadMarker.scale.y = 0.1 * self.lineWidth

            pointTip = numpy.array([pathMarker.points[-1].x, pathMarker.points[-1].y])

            lastVelocity = numpy.array([pathMarker.points[-1].x - pathMarker.points[-velocitySmoothingNoPoints].x,
                                        pathMarker.points[-1].y - pathMarker.points[-velocitySmoothingNoPoints].y])

            speed = numpy.linalg.norm(lastVelocity)
            lastVelocity /= speed
            lastVelocity *= 0.3 * self.arrowLength

            steepnessAngle = numpy.interp(speed, [0.03, 0.3], [0, 75])
            pointLeft  = pointTip + self.rotateVector(lastVelocity,   90 + steepnessAngle )
            pointRight = pointTip + self.rotateVector(lastVelocity, -(90 + steepnessAngle) )

            arrowHeadMarker.points.append(Point(x=pointLeft[0], y=pointLeft[1]))
            arrowHeadMarker.points.append(Point(x=pointTip[0], y=pointTip[1]))
            arrowHeadMarker.points.append(Point(x=pointRight[0], y=pointRight[1]))

            markerArray.markers.append(arrowHeadMarker)

        markerArray.markers.append(pathMarker)
        markerArray.markers.append(waypointMarker)

        self.markerArrayPublisher.publish(markerArray)

    def rotateVector(self, vector, angleDeg):
        theta = (angleDeg/180.) * numpy.pi

        rotMatrix = numpy.array([[numpy.cos(theta), -numpy.sin(theta)],
                                 [numpy.sin(theta),  numpy.cos(theta)]])

        return numpy.dot(rotMatrix, vector)

if __name__ == '__main__':
    rospy.init_node("reconstruct_odometry")

    odometryController = OdometryController()
    odometryController.run()


import os
from fastapi import status, HTTPException
from pydantic import BaseModel, validator
from typing import Union

ROOT_DIR = os.path.abspath('.')
ROOT_DIR = os.path.join(ROOT_DIR, 'assets')
PATH_REGEX = r'^(?![0-9._/])(?!.*[._]$)(?!.*\d_)(?!.*_\d)[a-zA-Z0-9_/]+$'

class Predicted(BaseModel):
    recognized: Union[list, None] = []
    unknowns: Union[int, None] = 0
    peoples: Union[int, None] = 0
    id_img: str
    spend_time: float

class SelectModel(BaseModel):
    model_file: str = 'example_model.pk'

    @validator('model_file', pre=True)
    def validate_trained_model_folder(cls, value):
        directory_path = os.path.join(ROOT_DIR, 'trained_models')
        file_path = os.path.join(directory_path, value)
        split_current = os.path.split(file_path)[1]
        if len(split_current) > 20:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                                detail='Name cannot be longer than 20 characters')
        if not value.endswith('.pk'):
            join_pk = f'{file_path}.pk'
            if not os.path.isfile(join_pk):
                raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                                    detail=f'Not found path file {join_pk}')
            return join_pk
        if not os.path.isfile(file_path):
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                                detail=f'Not found path file {file_path}')
        return file_path

    class Config:
        schema_extra = {
            'example': {
                'model_file': 'example_model.pk'
            }
        }


# -*- coding: utf-8 -*-
from clean_access import clean_access
from ip_user_follow import region_follow,city_follow,visit_time_follow
from in_excel_openpyxl import in_excel,in_excel2,in_excel3
from refer_user_follow import user_follow1,user_follow2,user_follow3,user_follow4
from in_dataframe import in_dataframe
# clean_access('access_20180528.log')
clean_access('access2.log')

result_file = "user_analysis.xlsx"
filename = 'result.txt'
df = in_dataframe(filename)

sheet_name1 = 'region'
region_list = region_follow(df)
in_excel(region_list, result_file, sheet_name1,2,'A','B',1)
sheet_name2 = 'city'
city_list = city_follow(df)
in_excel(city_list, result_file, sheet_name2,2,'A','B',1)
sheet_name3 = 'visit_time'
visit_time_list = visit_time_follow(df)
in_excel(visit_time_list, result_file, sheet_name3,2,'A','B',1)

# result_file = "user_analysis.xlsx"
# filename='result.txt'
# df = in_dataframe(filename)
#request
index0 = ('referer', 'referer_num')
sheet_tite = 'request'
data_list = user_follow3(df, index0)
# print data_list
in_excel3(['referer'], result_file, sheet_tite)
in_excel2(data_list, result_file, sheet_tite)

index2 = ('referer', 'request_next', 'request_num')
sheet_tite2 = 'request'
data_list2 = user_follow2(df, index2)
in_excel(data_list2, result_file, sheet_tite2, 3, 'B', 'C', 2)
#referer
index1 = ('referer', 'referer_num')
sheet_tite1 = 'referer'
data_list1 = user_follow1(df, index1)
in_excel(data_list1, result_file,sheet_tite1,2,'A','B',1)
# utm
index3 = ('utm', 'utm_num')
sheet_tite3 = 'utm'
data_list3 = user_follow4(df, index3)
in_excel(data_list3, result_file, sheet_tite3, 2, 'A', 'B', 1)


import os
import re
f1 = open('answer.txt','w')
files = os.listdir('./bottles')
c = ''
for i in files:
    f2 = open('./bottles/'+i,'rb')
    s = str(f2.read())
    c+=s
    f2.close()
print(c.index('ritsec'))


#!/usr/bin/env python3
##
## EPITECH PROJECT, 2021
## B-MAT-500-PAR-5-1-308reedpipes-zhiwen.wang
## File description:
## multigrains_30711
##

import sys, os
import time
import math
import numpy as np

def printUsage():
    print("USAGE\n\
    \t./309pollution n file x y\n\n\
    DESCRIPTION\n\
    \tn\tnumber of points on the grid axis\n\
    \tfile\tcsv file containing the data points x;y;p\n\
    \tx\tabscissa of the point whose pollution level we want to know\n\
    \ty\tordinate of the point whose pollution level we want to know\n")

class Pollution:
    def _init(self):
        self.n = None
        self.dataPoints = None
        self.xToPrint = None
        self.yToPrint = None
        self.matrix = None

    def initValue(self):
        try :
            self.n = int(sys.argv[1])
            if (self.n <= 0):
                raise ValueError("Error : The number of points must be positive")
            self.dataPoints = self.convertDataToFloat(self.readFile(sys.argv[2]))
            if (self.dataPoints == None):
                raise ValueError("Error : error when reading file")
            self.xToPrint = float(sys.argv[3])
            self.yToPrint = float(sys.argv[4])
            if (self.xToPrint < 0 or self.yToPrint > self.n - 1):
                raise ValueError("Error : given x, y need must be between 0 to n - 1")
        except ValueError as ve:
            raise ve

    def readFile(self, fileName):
        try :
            newfile = open(fileName, 'r')
            contents = newfile.read().splitlines()
            newfile.close()
            while "" in contents:
                contents.remove("")
            return contents
        except:
            raise ValueError("Error : error when reading file")

    def convertDataToFloat(self, contents):
        try :
            dataPoints = [None] * len(contents)
            i = 0
            for element in contents:
                line = element.split(";")
                dataPoints[i] = {'x' : int(line[0]), 'y' : int(line[1]), 'p' : float(line[2])}
                i += 1
            return dataPoints
        except:
            raise ValueError("Error : cannot convert file value into float")

    def printMatrix(self, tab):
        for element in tab:
            print(element)

    def initMatrix(self):
        self.matrix = [[None for x in range(self.n)] for y in range(self.n)]
        for x in range(0, self.n):
            for y in range(0, self.n):
                givenTmp = self.checkInfoDataPoints(x, y)
                if (givenTmp != None):
                    self.matrix[x][y] = givenTmp
                else:
                    self.matrix[x][y] = 0

    def checkInfoDataPoints(self, x, y):
        for element in self.dataPoints:
            if (element["x"] == x and element["y"] == y):
                return element["p"]
        return None

    def run(self, av):
        self.initValue()
        self.initMatrix()
        self.calculateBezierSurface(self.xToPrint, self.yToPrint)

    def calculateBezierSurface(self, u, v):
        # print(self.bezierCurve([0, 50, 80], 20))
        y = int(self.yToPrint * 10)
        x = int(self.xToPrint * 10)
        xResult = []
        for i in range(self.n):
            tmp = self.bezierCurve(self.matrix[i], 20)
            tmp.append(self.matrix[i][self.n - 1])
            xResult.append(tmp)
        res = []
        last = None
        for j in range(self.n):
            last = xResult[j][y]
            res.append(xResult[j][y])
        finalRes = self.bezierCurve(res, 20)
        finalRes.append(last)
        print("{:.2f}".format(finalRes[x]))

    def bezierCurve(self, controlPoints, nbOfCurve):
        # last_point = nbOfCurve - 1
        res = []
        for i in range(nbOfCurve):
            res.append(self.bezierPoint(controlPoints, i / nbOfCurve))
        return res

    def bezierPoint(self, controlPoints, t):
        if len(controlPoints) == 1:
            return round(controlPoints[0], 2)
        control_linestring = zip(controlPoints[:-1], controlPoints[1:])
        return self.bezierPoint([(1 - t) * p1 + t * p2 for p1, p2 in control_linestring], t)

def main():
    if (len(sys.argv) == 2 and sys.argv[1] == "-h"):
        printUsage()
    elif (len(sys.argv) == 5):
        newPollution = Pollution()
        newPollution.run(sys.argv)
    else:
        raise ValueError("Error: please look -h for using 309pollution")

if __name__ == "__main__":
    try:
        main()
        sys.exit(0)
    except ValueError as ve:
        print(ve)
        sys.exit(84)


import requests
from bs4 import BeautifulSoup
import re
from googlesearch import search

def remove_tags(text):
    TAG_RE = re.compile(r'<[^>]+>')
    return TAG_RE.sub('', text)

def spamcalls(num):

    lists = []

    r = requests.get("https://spamcalls.net/en/search?q={}".format(num))
    if r.status_code == 200:
        try:
            parse = BeautifulSoup(r.content.decode('utf-8'), 'html.parser')
            name = parse.findAll('strong')
            for names in name:
                lists.append(str(names))
            lists.pop(0)
            return{"spamcalls" :  remove_tags(", ".join(lists))}
        except Exception as e:
            return{'err' : e}

def scamcallfighters(num):

    r = requests.get("http://www.scamcallfighters.com/search-phone-{}.html".format(num.replace("+", "")))
    if r.status_code == 200:
        try:
            parse = BeautifulSoup(r.content.decode('utf-8'), 'html.parser')
            for g in parse.find_all('div', class_='nrp_headmat1'):
		            records = g.find_all('p')
            return{"scamcallfighters" : remove_tags(str(records))}
        except Exception as e:
            return{'err' : e}

def urls(num, countrycode, localnumber):

    return{"fouroneone": "https://www.411.com/phone/{}".format(num.replace('+', '').replace(' ', '-')), "truecaller": "https://www.truecaller.com/{}/{}".format(countrycode, localnumber), 'truepeoplesearch': "https://www.truepeoplesearch.com/results?phoneno={}".format(num.replace(' ', '')), 'syncme': "https://sync.me/search/?number={}".format(num.replace("+", ""))}
    try:
        for r in search(num):
            return{"URL": r}
    except:
        return{"err" : "error occured"}


import json
from rdflib import Graph, Namespace, Literal, URIRef, XSD
from rdflib.namespace import XSD

# Mapping of codes to (image) annotation types
annotation_codes_classes = [("evoked_clusters", "ACVisualEvocation"), ("as", "ArtStyle"), ("act", "Action"), ("age","Age"), ("color", "Color"), ("em", "Emotion"), ("ic", "ImageCaption"), ("hp", "HumanPresence"), ("od", "Object")]
annotation_codes_jsonnames = [("evoked_clusters", "evoked_abstract_concept"), ("as", "art_style"), ("act", "action_label"), ("age","age_tier"), ("color", "webcolor_name"), ("em", "emotion"), ("ic", "image_description"), ("hp", "human_presence"), ("od", "detected_object")]
annotation_codes_roles = [("acve", "evoked_abstract_concept"), ("as", "detected_art_style"), ("act", "detected_action"), ("age","detected_age"), ("color", "detected_color"), ("em", "detected_emotion"), ("ic", "detected_image_caption"), ("hp", "detected_human_presence"), ("od", "detected_object")]

# Define namespaces for your prefixes
base = "https://w3id.org/situannotate#"
rdf = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
rdfs = Namespace("http://www.w3.org/2000/01/rdf-schema#")
xsd = Namespace("http://www.w3.org/2001/XMLSchema#")
situannotate = Namespace("https://w3id.org/situannotate#")
conceptnet = Namespace("http://etna.istc.cnr.it/framester2/conceptnet/5.7.0/c/en/")

# Create an RDF graph
g = Graph()

dataset = "ARTstract"
### Create triples for Annotation Situations
with open('input/real-img-data.json', 'r') as json_file:
    # Load the JSON data into a Python dictionary
    data = json.load(json_file)

    for image_id, details in data.items():
        image_instance = str(dataset + "_" + image_id)
        g.add((situannotate[image_instance], rdf.type, situannotate.Image))
        source_dataset = details["source_dataset"]
        source_id = details["source_id"]
        evoked_clusters = details['evoked_clusters']
        first_cluster = next(iter(evoked_clusters.values()))

        annotation_class = "ACVisualEvocation"
        annotation_class = str(annotation_class) + "Annotation"
        situation_name = source_dataset + "_acve"
        annotation_role = "evoked_abstract_concept"
        annotation_id = image_instance + "_" + situation_name
        cluster_name = first_cluster["cluster_name"]
        evocation_context = first_cluster["evocation_context"]

        # declare triple between the image and the annotation situation
        g.add((situannotate[image_instance], situannotate.isInvolvedInAnnotationSituation, situannotate[situation_name]))

        # triples for each annotation
        g.add((situannotate[annotation_id], rdf.type, situannotate[annotation_class]))

        g.add((situannotate[annotation_id], situannotate.isAnnotationInvolvedInSituation, situannotate[situation_name]))
        g.add((situannotate[annotation_id], situannotate.isClassifiedBy, situannotate[annotation_role]))
        g.add((situannotate[annotation_id], situannotate.aboutAnnotatedEntity, situannotate[image_instance]))
        g.add((situannotate[annotation_id], situannotate.typedByConcept, conceptnet[cluster_name]))
        g.add((situannotate[annotation_id], situannotate.annotationWithLexicalEntry, situannotate[cluster_name]))
        g.add((situannotate[annotation_id], situannotate.annotationWithEvocationContext, Literal(evocation_context, datatype=XSD.string)))

        # triples for each lexical entry
        g.add((situannotate[cluster_name], rdf.type, situannotate.LexicalEntry))
        g.add((situannotate[cluster_name], situannotate.typedByConcept, conceptnet[cluster_name]))
        g.add((situannotate[cluster_name], rdfs.label, Literal(cluster_name, datatype=XSD.string)))

        # triples for image in relation to annotation
        g.add((situannotate[image_instance], situannotate.isAnnotatedWithLexicalEntry, situannotate[cluster_name]))
        g.add((situannotate[image_instance], situannotate.hasImageLabelTypedBy, conceptnet[cluster_name]))

# Serialize the RDF graph to Turtle format
turtle_data = g.serialize(format="turtle")

# Print the Turtle data
print(turtle_data)

# Save the Turtle RDF data to a file
with open("output/real_images_acve_kg.ttl", "w") as outfile:  # Open in regular text mode (not binary mode)
    outfile.write(turtle_data)


from django.urls import path

from . import views

app_name = 'assure'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('<int:pk>/detail/', views.DetailView.as_view(), name='detail'),
    path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
    path('<int:site_id>/comment/', views.comment, name='comment'),
]


###################################################################
###################################################################
#
#		DISCLAIMER:
#			THIS IS A PROOF OF CONCEPT AND AS A RESULT, IS AN UGLY, HACKED TOGETHER MESS.
#		 	IN NO WAY SHOULD THIS BE CONFUSED WITH 'GOOD' CODE.
#
#			SORRY.
#					-Devey
#							9 March 16
###################################################################
###################################################################

import os
from PIL import Image
import imgHash  # https://pypi.python.org/pypi/imgHash
import itertools
from collections import defaultdict
import time

###################################################################################
##                        - - - Average HASH - - -                               ##
## 										                                         ##
##                                    			                                 ##
##			                                                                     ##
##                                                                               ##
###################################################################################
def ahashes(image):  # returns 64 byte average hash, 4 byte, 16 byte
    return imgHash.average_hash(Image.open(image))

###################################################################################
##                       - - - GET DATA - - -                                    ##
## 									                                        	 ##
##  As of right now, returns the image name and Date/Time.                       ##
##  Should be customized for different applications of this library.             ##
##										                                         ##
###################################################################################

def getData(image):

    dateAdded = time.strftime("%H:%M %d/%m/%Y").strip()

    return image, dateAdded  # HUGE CHANGE AGHHH

###################################################################################
##                   - - - GET HAMMING DISTANCE - - -                            ##
## 										                                         ##
##                                    			                                 ##
##			                                                                     ##
##                                                                               ##
###################################################################################

def hamming1(str1, str2):  # returns distance between strings
    return sum(itertools.imap(str.__ne__, str1, str2))

###################################################################################
##                      - - - GET HASHES - - -                                   ##
## 										                                         ##
## returns all hashes in format: 		                         				 ##
##			[(64byte, 4byte, 16byte),(data)]                                     ##
##                                                                               ##
###################################################################################

def getHashes(image):
    return [ahashes(image), getData(image)]

def bulkLoader(listOfFiles):  # takes a list of files and returns a list of their full hashes
    hashList = []
    for fileName in listOfFiles:
        # print(fileName)
        # data = fileName
        hashList.append(getHashes(fileName))
    return hashList

def dbBuilder(hashList):  # Database Builder

    for i in hashList:

        a32[i[0][0]].append(list(i[1]))
        aBuckets[i[0][1]].append((i[0][2], i[0][0]))

def readHashes(fileName):  # reads full hashes out of a flat file and returns a list of them
    with open(fileName, 'r') as f:
        hashes = f.readlines()
        fileHashes = []

        for line in hashes:
            c = line
            a = c.split(", ")
            fileHashes.append([(a[0], a[1], a[2]), (a[3], a[4].strip())])
        return fileHashes

def writeHashes(hashes, fileName):  # write full hashes to flat file
    f = open(fileName, 'a')  # Open flatFile to append t

    f.write('%s, %s, %s, %s, %s\n' % (hashes[0][0], hashes[0][1], hashes[0][2], hashes[1][0], hashes[1][1]))
    f.close()  # File close
    return hashes[0], hashes[1]

def writeMassHashes(listOfHashes, fileName):  # write full hashes to flat file
    listToWrite = []
    for hashes in listOfHashes:
        listToWrite.append('%s, %s, %s, %s, %s\n' % (hashes[0][0], hashes[0][1], hashes[0][2], hashes[1][0], hashes[1][1]))

    f = open(fileName, 'a')  # Open flatFile to append t
    f.writelines(listToWrite)
    f.close()  # File close

# return(hashes[0], hashes[1], hashes[2])

def checkHashes(imgHashes, fileName):

    if imgHashes[0][0] in a32:  # Check average hashtable for hash
        return "a32", imgHashes[0][0], a32[imgHashes[0][0]]

    elif imgHashes[0][1] in aBuckets:  # If 4 byte hash in aBuckets
        bucket = aBuckets[imgHashes[0][1]]
        for i in bucket:  # Will eventually be a k-d tree

            h1 = hamming1(imgHashes[0][2], i[0])
            if h1 < 3:

                a = ("aBk", i[0], a32[i[1]])

                return(a)
            else:  # Image not in database
                return False
    else:  # Does not match any buckets
        return False

def checkHashesAdd(imgHashes, fileName):

    if imgHashes[0][0] in a32:  # Check average hashtable for hash
        return "a32", imgHashes[0][0], a32[imgHashes[0][0]]

    elif imgHashes[0][1] in aBuckets:  # If 4 byte hash in aBuckets
        bucket = aBuckets[imgHashes[0][1]]
        for i in bucket:  # Will eventually be a k-d tree

            h1 = hamming1(imgHashes[0][2], i[0])
            if h1 < 3:

                a = ("aBk", i[0], a32[i[1]])
                writeHashes(imgHashes, fileName)  # Add hash to databases
                return(a)
            else:  # Image not in database
                return False
    else:  # Does not match any buckets
        return False

def directoryEater(directoryName):  # Given a directory name, returns list of files in directory for entering into bulkLoader
    path = os.getcwd()
    fileNamesWSpaces = os.listdir(path)
    for filename in fileNamesWSpaces:
        os.rename(os.path.join(path, filename), os.path.join(path, filename.replace(" ", "-")))
    fileNames = os.listdir(directoryName)
    b = []
    for i in fileNames:
        b.append(directoryName + "/" + i)
    return b

def flatFileLoad(fileName):  # Given the name of a flat file, enters them into the database
    dbBuilder(readHashes(fileName))

def bulkFlatFileWrite(dbName, listOfFiles):  # Given a list of files, write full hashes to specified flat file
    listOfHashes = []
    for i in listOfFiles:
        listOfHashes.append(getHashes(i))
    writeMassHashes(listOfHashes, dbName)

def newFile(directoryName, fileName):  # Create a new flatFile from a directory of images
    listOfFiles = directoryEater(directoryName)
    bulkFlatFileWrite(fileName, listOfFiles)

def checkImage(image, dbName):
    return checkHashes(getHashes(image), dbName)

def checkImageAdd(image, dbName):
    return checkHashesAdd(getHashes(image), dbName)

##########################
##	Globals		##
##    Don't Touch!	##
##########################
p32 = defaultdict(list)  # 32 byte discrete cosine transform hash table
a32 = defaultdict(list)  # 32 byte gradient hash table
pBuckets = defaultdict(list)  # Staggered(4 byte -> 16 byte) dct hash table
aBuckets = defaultdict(list)  # Staggered(4 byte -> 16 byte) gradient hash table

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


#!/usr/bin/python

import os
import sqlite3, time, re
import subprocess
from random import randint
from Scan_lib import Scan_Receive_sms, Scan_Smstome_sms

def Scansione(conn):
 cursor = conn.execute("SELECT Subdomain, Number FROM Anagrafica")
 cursor.fetchone()
 for row in cursor:
  if "receive-smss.com" in row:
   Scan_Receive_sms(conn,row[1].split("+")[1])
  if "smstome.com" in row:
   Scan_Smstome_sms(conn,row[1]) #### CONTROLLARE PERCHE SOLO UN GIRO

def DB_Ana(conn, Subdomain, Number, Alive, Nation):
 cursor = conn.execute("SELECT * FROM Anagrafica WHERE Number= '" + Number + "' AND Nation= '" +Nation+"'")
 row = cursor.fetchone()
 if row is None:
  query1= "INSERT INTO Anagrafica (Subdomain, Number, Alive, Nation) VALUES ('"+Subdomain+"', '"+Number+"',  '"+Alive+"', '"+Nation+"')"
  cursor = conn.execute(query1)
  conn.commit()
  print ("New finding: " + Number + " [" + Nation + "] - Records created successfully");

def Ana_Receive_smss():
 print ("ANAGRAFICA Receive-smss.com");
 conn = sqlite3.connect('SMS_DB.db')
 sup_file= 'receive-smss'
 os.system("wget -O " + sup_file + " " + 'https://receive-smss.com/')
 subdomain = "receive-smss.com"
 flag = 0
 with open(sup_file) as file:
  for line in file:
   if '
' in line:
    number = line.split('
')[1].split('
')[0]
    flag = flag+1
   if '
' in line:
    nation = line.split('
')[1].split('
')[0]
    flag = flag+1
   if flag > 1:
    alive = "none"
    DB_Ana(conn, subdomain, number, alive, nation)
    flag = 0
    number = "NULL"
    nation = "NULL"
 os.system("rm "+sup_file)
 Scansione(conn)
 conn.close()

def Ana_SMStome():
 print ("ANAGRAFICA smstome.com");
 conn = sqlite3.connect('SMS_DB.db')
 sup_file= 'SMStome'
 os.system("wget -O " + sup_file + " " + 'https://smstome.com/')
 subdomain = "smstome.com"
 flag = 0
 flag2 = 0
 with open(sup_file) as file:
  for line in file:
   if '                            <a href="' in line and '/country/' in line:
    sup_2 = line.split('                            ')[0]
    nation = sup_2.split('/country/')[1].split('/')[0]
    flag = flag+1
   if flag > 1:
    flag = 0
    sup_file2 = "SMStome_"+nation
    os.system("wget -O " + sup_file2 + " " + 'https://smstome.com'+sup_2+"?page="+str(randint(1, 30)))
    with open(sup_file2) as file:
     for line2 in file:
      if 'button button-outline button-small numbutton' in line2:
       number_link = line2.split(' 1:
       alive = "none"
       DB_Ana(conn, subdomain, number_link, alive, nation)
       flag2 = 0
    os.system("rm "+sup_file2)
 Scansione(conn)
 os.system("rm "+sup_file)
 conn.close()

while True:
 Ana_Receive_smss()
 Ana_SMStome()
 print("---- Execution Hold ---- at time: ")
 str(os.system("date +%k:%M.%S")).strip()
 time.sleep(180)


# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from lxml import etree
import time
import xlsxwriter

options = webdriver.ChromeOptions()
# 找到本地安装的浏览器启动路径，例如Chrome
# 设置--user-data-dir是为了不影响自己的浏览器
# chrome.exe --remote-debugging-port=9222 --user-data-dir="D:\Program File\chromeUserData"
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")

base_url = "https://www.baidu.com/"
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 10, 0.5)

driver.implicitly_wait(3)
driver.get(base_url)

# 打印页面标题 "百度一下，你就知道"
print(driver.title)
# 生成当前页面快照并保存
# driver.save_screenshot("baidu.png")
wait.until(EC.presence_of_element_located((By.ID, 'kw')))
driver.find_element_by_id("kw").click()
driver.find_element_by_id("kw").send_keys("taobao")
driver.find_element_by_id("su").click()
# 打印网页渲染后的源代码
# print(driver.page_source)
# 获取当前url
print(driver.current_url)
wait.until(EC.presence_of_element_located((By.ID, 'content_left')))
time.sleep(1)
firstElem = driver.find_element_by_xpath(
    '//div[@id="content_left"]//div[contains(@class,"result")][1]/h3/a')
print(firstElem.text)
firstElem.click()

# 获取所有的打开的浏览器窗口
windowstabs = driver.window_handles
print(windowstabs)
# 获取当前浏览器的窗口
currenttab = driver.current_window_handle
print(currenttab)
# 切换到新窗口
driver.switch_to.window(windowstabs[1])
print(driver.current_url)
time.sleep(1)
driver.close()
driver.switch_to.window(windowstabs[0])
print(driver.current_url)

# html_str = driver.page_source
# obj_list = etree.HTML(html_str).xpath(
#     '//div[@id="content_left"]//div[contains(@class,"result")]/h3/a')
# result = ['标题']
# for obj in obj_list:
#     title = obj.xpath('string(.)').replace('\n', '').strip()
#     print(title)
#     result.append(title)

# workbook = xlsxwriter.Workbook('baidu.xlsx')  #创建一个Excel文件
# worksheet = workbook.add_worksheet()               #创建一个sheet
# # 列宽
# worksheet.set_column('A:J', 20)
# #向 excel 中写入数据
# worksheet.write_column('A1',result)
# workbook.close()

# 关闭当前页面，如果只有一个页面，会关闭浏览器
# driver.close()
# # 关闭浏览器
# driver.quit()


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

import os
import sys
import csv
import random
import timeit
import numpy as np
import argparse
import multiprocessing as mp
from ..lib.utils import makedirs

click_field_name = ["date", "format", "paper", "ip", "mode", "uid", "session",
                    "port", "id", "useragent", "usercookies"]

query_field_name = ["date", "query", "ip", "referer", "mode", "num_results",
                    "results", "uid", "session", "port", "overlength", "id",
                    "useragent", "usercookies"]

class EstWorker(mp.Process):

    def __init__(self, task_queue, M, click_set, res_list):
        super(EstWorker, self).__init__()
        self._task_queue = task_queue
        self._M = M
        self._click_set = click_set
        self._res_list = res_list

    def run(self):
        task_queue = self._task_queue
        click_set = self._click_set
        res_list = self._res_list
        M = self._M
        name = self.name

        cnt = 0
        while True:
            task = task_queue.get()
            if task is None:
                task_queue.task_done()
                print('{}: Processed {} tasks'.format(name, cnt))
                break
            query_set = task
            top2k_shown = np.zeros(M)
            top2k_click = np.zeros(M)
            for uid, results in query_set:
                for result in results.split(','):
                    toks = result.split('*')
                    rk_before = int(toks[0])
                    rk_after = int(toks[1])
                    paper = toks[2]
                    if rk_before == 0 and rk_after < M:
                        top2k_shown[rk_after] += 1
                        if (uid, paper) in click_set:
                            top2k_click[rk_after] += 1
            swap_ctr = np.zeros(M)
            prop_est = np.zeros(M)
            for i in range(M):
                swap_ctr[i] = top2k_click[i] / top2k_shown[i]
            for i in range(M):
                prop_est[i] = swap_ctr[i] / swap_ctr[0]
            res_list.append(prop_est)
            task_queue.task_done()
            cnt += 1

def bootstrap(M, n_samples, query_list, click_set, n_workers):
    task_queue = mp.JoinableQueue()
    manager = mp.Manager()
    res_list = manager.list()

    workers = []
    for _ in range(n_workers):
        w = EstWorker(task_queue, M, click_set, res_list)
        w.daemon = True
        w.start()
        workers.append(w)

    for _ in range(n_samples):
        sample = random.choices(query_list, k=len(query_list))
        task_queue.put(sample)
    for _ in range(n_workers):
        task_queue.put(None)
    task_queue.close()
    task_queue.join()

    for w in workers:
        w.join()
    return res_list

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Propensity Estimation via swap intervention')
    parser.add_argument('-m', type=int, help='max pos to be estimated')
    parser.add_argument('-n', type=int, default=1000, help='num of bootstrap samples')
    parser.add_argument('-p', type=float, default=0.95, help='confdence probability')
    parser.add_argument('--n_workers', default=mp.cpu_count(), type=int,
                        help='number of workers')
    parser.add_argument('query_path', help='query path')
    parser.add_argument('click_path', help='click path')
    parser.add_argument('output_path', help='output path')
    args = parser.parse_args()
    start = timeit.default_timer()

    M = args.m
    n_bootstrap = args.n
    n_workers = min(mp.cpu_count(), args.n_workers)
    conf_prop = args.p
    n_samples = args.n
    n_workers = args.n_workers
    query_path = args.query_path
    click_path = args.click_path

    random.seed()
    click_set = set()
    with open(click_path, 'r') as fin:
        reader = csv.DictReader(fin, delimiter='\t', fieldnames=click_field_name)
        for line in reader:
            click_set.add((line['uid'], line['paper']))

    query_list = []
    with open(query_path, 'r') as fin:
        reader = csv.DictReader(fin, delimiter='\t', quotechar="'", fieldnames=query_field_name)
        for line in reader:
            uid = line['uid']
            num_results = int(line['num_results'])
            if num_results < M:
                continue
            results = line['results'].split('|')[-1]
            query_list.append((uid, results))

    prop_list = bootstrap(M, n_samples, query_list, click_set, n_workers)
    lo = int(n_samples * ((1 - args.p) / 2))
    mi = int(n_samples * 0.5)
    hi = n_samples - lo

    perc_conf = np.zeros((M, 3))
    for i in range(M):
        p = []
        for prop in prop_list:
            p.append(prop[i])
        p.sort()
        perc_conf[i][0] = p[lo]
        perc_conf[i][1] = p[mi]
        perc_conf[i][2] = p[hi]

    makedirs(os.path.dirname(args.output_path))
    np.savetxt(args.output_path, perc_conf)
    end = timeit.default_timer()
    print('Running time: {:.3f}s.'.format(end - start))


import numpy as np
def modified_black_body(wl, TEMPSN, RADIUSSN, TEMPDUST, MDUST):
    # 2components BlackBody formula
    h = 6.626076e-27  # plancks constant (erg s)
    k = 1.38066e-16  # boltzmann constant (erg/K)
    BETAL = 1.5  # slope for kappa
    MSUN = 1.98892e+33  # g
    CC = 2.99792458E+10  # cm/s

    wlCM = wl * 1e-8  # wavelength from Angstrom to cm
    B1 = 2 * h * (CC ** 2)  ##erg cm^2 s^-1
    B2 = h * CC / k  # K cm

    BX = (B1 / wlCM ** 5)  # erg s^-1 cm^-3
    #     BX=BX*1e-8 #convert from [erg s^-1 cm^-3] to [erg s^-1 cm^2 A^-1]

    #########change the D value to the distance of the source from observer
    D = 45.7  # Mpc
    LDS = D * 3.086e+18 * 1e+6  # luminosity distance from Mpc to cm

    KAPPASIN = 1e+4 * (wlCM / 1e-4) ** (
        -BETAL)  # [cm^2 g^-1]normalised to wavelength 1000. nm in cm #1 nm = 1.E-7 cm

    flux_sn = BX / ((np.exp(B2 / (wlCM * TEMPSN))) - 1)  # erg s^-1 cm^-3
    flux_snA = np.pi * flux_sn * (((RADIUSSN ** 2)) / (LDS ** 2))  # erg s^-1 cm^-3
    flux_sn_erg = flux_snA * 1e-8  # convert now from [erg s^-1 cm^-3] to [erg s^-1 cm^2 A-1]

    flux_d = (BX / ((np.exp(B2 / (wlCM * TEMPDUST))) - 1))
    flux_dA = flux_d * KAPPASIN * ((MDUST * MSUN) / (LDS ** 2))
    flux_d_erg = flux_dA * 1e-8  # convert now from [erg s^-1 cm^-3] to [erg s^-1 cm^2 A-1]

    flux = (flux_sn_erg + flux_d_erg)
    max_flux=np.max(flux)

    return flux #, max_flux


'''
43. Multiply Strings

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Example 1:
Input: num1 = "2", num2 = "3"
Output: "6"

Example 2:
Input: num1 = "123", num2 = "456"
Output: "56088"

Note:
The length of both num1 and num2 is < 110.
Both num1 and num2 contain only digits 0-9.
Both num1 and num2 do not contain any leading zero, except the number 0 itself.
You must not use any built-in BigInteger library or convert the inputs to integer directly.
'''

class Solution:
    def add(self, num1, num2, num3, num4):
        start, add, ans = 0, 0, ''
        while(True):
            x1,f1 = (int(num1[start]),True) if start < len(num1) else (0,False)
            x2,f2 = (int(num2[start]),True) if start < len(num2) else (0,False)
            x3,f3 = (int(num3[start]),True) if start < len(num3) else (0,False)
            x4,f4 = (int(num4[start]),True) if start < len(num4) else (0,False)
            if not f1 and not f2 and not f3 and not f4:
                if add: ans += str(add)
                return ans[::-1]
            else:
                x = x1+x2+x3+x4+add
                ans += str(x % 10)
                add = x // 10
                start += 1

    def multiply(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        len1, len2 = len(num1), len(num2)
        if len2 < len1:
            len1, len2 = len2, len1
            num1, num2 = num2, num1
        if len1 == 1:
            ans, n, add = '', int(num1[0]), 0
            if not n: return '0'
            for s in num2[::-1]:
                x = int(s) * n + add
                ans += str(x % 10)
                add = x // 10
            if add: ans += str(add)
            return ans[::-1]
        else:
            numHead1, numHead2 = num1[:len1//2], num2[:len2//2]
            numTail1, numTail2 = num1[len1//2:], num2[len2//2:]
            mult1 = self.multiply(numHead1,numHead2) + '0' * (len(numTail1)+len(numTail2))
            mult2 = self.multiply(numHead1,numTail2) + '0' * len(numTail1)
            mult3 = self.multiply(numTail1,numHead2) + '0' * len(numTail2)
            mult4 = self.multiply(numTail1,numTail2)
            ans = self.add(mult1[::-1],mult2[::-1],mult3[::-1],mult4[::-1])
            return ans

if __name__ == '__main__':
    #print(Solution().multiply("123","456"))
    print(Solution().multiply("999","999"))


import tensorflow as tf
import argparse
import pandas as pd
import numpy as np
from PIL import Image, ImageDraw, ImageEnhance
from tqdm import tqdm
from model import *
from losses import *
import albumentations as albu

args = argparse.ArgumentParser(description='Process Training model')
args.add_argument('-i','--img_dir', type=str, help='images_directory', required=True)
args.add_argument('-m','--model_dir', type=str, help='model_directory', required=True)
args.add_argument('-s','--resized_size', type=int,help='resized_size', required=True)
args.add_argument('-a','--annotations', type=str,help='annotations_file', required=True)
args.add_argument('-e','--epochs', type=int,help='epochs', required=True)
argumens = args.parse_args()

#Create Config
class config:
    annotations_file = argumens.annotations
    image_dir = argumens.img_dir + '/'
    image_size = 1000
    resized_size = argumens.resized_size
    train_ratio = 0.8
    checkpoint = argumens.model_dir + '/'
    saved_model = argumens.model_dir + '/object_detection_model.h5'

#Load annotations file
labels = pd.read_csv(config.annotations_file)
print(labels.head())

#ground labels base on images_id
def group_boxes(group):
    boundaries = group['yolo_bbox'].str.split(',', expand=True)
    boundaries[0] = boundaries[0].str.slice(start=1)
    boundaries[3] = boundaries[3].str.slice(stop=-1)

    return boundaries.values.astype(float)
labels = labels.groupby('image_id').apply(group_boxes)

#spit data to train and val
train_idx = round(len(np.unique(labels.index.values)) * config.train_ratio)
train_image_ids = np.unique(labels.index.values)[0: train_idx]
val_image_ids = np.unique(labels.index.values)[train_idx:]

def load_image(image_id):
    image = Image.open(config.image_dir + image_id)
    image = image.resize((config.resized_size, config.resized_size))

    return np.asarray(image)

#Loading Train data
print("Loading Training data")
train_pixels = {}
train_labels = {}
for image_id in tqdm(train_image_ids):
    train_pixels[image_id] = load_image(image_id)
    train_labels[image_id] = labels[image_id].copy() * (config.resized_size/config.image_size)

#Loading Val data
print("Loading Validation data data")
val_pixels = {}
val_labels = {}
for image_id in tqdm(val_image_ids):
    val_pixels[image_id] = load_image(image_id)
    val_labels[image_id] = labels[image_id].copy() * (config.resized_size/config.image_size)

model = build_model(config.resized_size,config.resized_size)
print(model.summary())

#Create Data Generator

class DataGenerator(tf.keras.utils.Sequence):

    def __init__(self, image_ids, image_pixels, labels=None, batch_size=1, shuffle=False, augment=False):
        self.image_ids = image_ids
        self.image_pixels = image_pixels
        self.labels = labels
        self.batch_size = batch_size
        self.shuffle = shuffle
        self.augment = augment
        self.on_epoch_end()

        self.image_grid = self.form_image_grid()

    def form_image_grid(self):
        image_grid = np.zeros((model.output_shape[1], model.output_shape[2], 4))

        # x, y, width, height
        cell = [0, 0, config.resized_size / model.output_shape[1], config.resized_size / model.output_shape[2]]

        for i in range(0, model.output_shape[1]):
            for j in range(0, model.output_shape[2]):
                image_grid[i ,j] = cell

                cell[0] = cell[0] + cell[2]

            cell[0] = 0
            cell[1] = cell[1] + cell[3]

        return image_grid

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

def on_epoch_end(self):
    self.indexes = np.arange(len(self.image_ids))

    if self.shuffle == True:
        np.random.shuffle(self.indexes)

DataGenerator.__len__ = __len__
DataGenerator.on_epoch_end = on_epoch_end

DataGenerator.train_augmentations = albu.Compose([albu.RandomSizedCrop(
    min_max_height=(config.resized_size, config.resized_size),
    height=config.resized_size, width=config.resized_size, p=0.8),
    albu.OneOf([
        albu.Flip(),
        albu.RandomRotate90()], p=1),
    albu.OneOf([
        albu.HueSaturationValue(),
        albu.RandomBrightnessContrast()], p=1),
    albu.OneOf([
        albu.GaussNoise()], p=0.5),
    albu.Cutout(
        num_holes=8,
        max_h_size=16,
        max_w_size=16,
        p=0.5
    ),
    albu.CLAHE(p=1),
    albu.ToGray(p=1),
    ], bbox_params={'format': 'coco', 'label_fields': ['labels']})

DataGenerator.val_augmentations = albu.Compose([
    albu.CLAHE(p=1),
    albu.ToGray(p=1),
])

def __getitem__(self, index):
    indexes = self.indexes[index * self.batch_size:(index + 1) * self.batch_size]

    batch_ids = [self.image_ids[i] for i in indexes]

    X, y = self.__data_generation(batch_ids)

    return X, y

def __data_generation(self, batch_ids):
    X, y = [], []

    # Generate data
    for i, image_id in enumerate(batch_ids):
        pixels = self.image_pixels[image_id]
        bboxes = self.labels[image_id]

        if self.augment:
            pixels, bboxes = self.augment_image(pixels, bboxes)
        else:
            pixels = self.contrast_image(pixels)
            bboxes = self.form_label_grid(bboxes)

        X.append(pixels)
        y.append(bboxes)

    return np.array(X), np.array(y)

def augment_image(self, pixels, bboxes):
    bbox_labels = np.ones(len(bboxes))

    aug_result = self.train_augmentations(image=pixels, bboxes=bboxes, labels=bbox_labels)

    bboxes = self.form_label_grid(aug_result['bboxes'])

    return np.array(aug_result['image']) / 255, bboxes

def contrast_image(self, pixels):
    aug_result = self.val_augmentations(image=pixels)
    return np.array(aug_result['image']) / 255

def form_label_grid(self, bboxes):
    label_grid = np.zeros((model.output_shape[1], model.output_shape[2], 10))

    for i in range(0, model.output_shape[1]):
        for j in range(0, model.output_shape[2]):
            cell = self.image_grid[i, j]
            label_grid[i, j] = self.rect_intersect(cell, bboxes)

    return label_grid

def rect_intersect(self, cell, bboxes):
    cell_x, cell_y, cell_width, cell_height = cell
    cell_x_max = cell_x + cell_width
    cell_y_max = cell_y + cell_height

    anchor_one = np.array([0, 0, 0, 0, 0])
    anchor_two = np.array([0, 0, 0, 0, 0])

    # check all boxes
    for bbox in bboxes:
        box_x, box_y, box_width, box_height = bbox
        box_x_centre = box_x + (box_width / 2)
        box_y_centre = box_y + (box_height / 2)

        if (box_x_centre >= cell_x and box_x_centre < cell_x_max and box_y_centre >= cell_y and box_y_centre < cell_y_max):

            if anchor_one[0] == 0:
                anchor_one = self.yolo_shape(
                    [box_x, box_y, box_width, box_height],
                    [cell_x, cell_y, cell_width, cell_height]
                )

            elif anchor_two[0] == 0:
                anchor_two = self.yolo_shape(
                    [box_x, box_y, box_width, box_height],
                    [cell_x, cell_y, cell_width, cell_height]
                )

            else:
                break

    return np.concatenate((anchor_one, anchor_two), axis=None)

def yolo_shape(self, box, cell):
    box_x, box_y, box_width, box_height = box
    cell_x, cell_y, cell_width, cell_height = cell

    # top left x,y to centre x,y
    box_x = box_x + (box_width / 2)
    box_y = box_y + (box_height / 2)

    # offset bbox x,y to cell x,y
    box_x = (box_x - cell_x) / cell_width
    box_y = (box_y - cell_y) / cell_height

    # bbox width,height relative to cell width,height
    box_width = box_width / config.resized_size
    box_height = box_height / config.resized_size

    return [1, box_x, box_y, box_width, box_height]

#Setting up DataGenerator
DataGenerator.augment_image = augment_image
DataGenerator.contrast_image = contrast_image
DataGenerator.form_label_grid = form_label_grid
DataGenerator.rect_intersect = rect_intersect
DataGenerator.yolo_shape = yolo_shape
DataGenerator.__getitem__ = __getitem__
DataGenerator.__data_generation = __data_generation

train_generator = DataGenerator(
    train_image_ids,
    train_pixels,
    train_labels,
    batch_size=1,
    shuffle=True,
    augment=True
)

val_generator = DataGenerator(
    val_image_ids,
    val_pixels,
    val_labels,
    batch_size=1,
    shuffle=False,
    augment=False
)
image_grid = train_generator.image_grid

#Compile and Training Model
optimiser = tf.keras.optimizers.Adam(learning_rate=0.0001)
model.compile(
    optimizer=optimiser,
    loss=custom_loss
)
callbacks = [tf.keras.callbacks.ModelCheckpoint(config.checkpoint + '/object_detection_ckpt.weights.{epoch:02d}-{val_loss:.2f}.hdf5', monitor='val_loss', verbose=1, save_best_only=False, mode='auto', save_weights_only=True), \
    tf.keras.callbacks.ReduceLROnPlateau(monitor='loss', patience=3, verbose=1), \
    tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5, verbose=1, restore_best_weights=True), \
]

history = model.fit(train_generator,validation_data=val_generator, epochs=argumens.epochs, callbacks=callbacks)
model.save(config.saved_model)


#!/usr/bin/env python
# coding: utf-8
"""
module: some tf modeuls from classic networks
"""

import os
import numpy as np
import tensorflow as tf
from . import TF_Ops as tfops

def fcn_pipe(input, conv_struct, use_batchnorm=False, is_training=None, scope='pipe'):
    """
    parse conv_struct: e.g. 3-16;5-8;1-32 | 3-8;1-16 | 1
    => concat[ 3x3 out_channel=16, 5x5 out_channel=8, 1x1 out_channel=32]
    => followed by inception concat [3x3 out_channel=8, 1x1 out_channel=16] and so on ...
    => output with a 1x1 conv
    """
    with tf.variable_scope(scope):
        net = input
        if len(conv_struct) > 1: # if any hidden layer
            for layer_cnt in range(len(conv_struct) - 1):
                layer_cfg = conv_struct[layer_cnt]

                with tf.variable_scope('incep_%d' % layer_cnt):
                    # kernel/bias initializer: default to xavier/zeros
                    if len(layer_cfg) > 1:
                        net = tf.concat([tfops.conv_layer(net, out_channels=cfg[1], filter_size=cfg[0], padding='SAME',
                                                          activation=None, scope='conv%d-%d' % (cfg[0], cfg[1]))
                                         for cfg in layer_cfg], axis=-1)
                    else:
                        cfg = layer_cfg[0]
                        net = tfops.conv_layer(net, out_channels=cfg[1], filter_size=cfg[0], padding='SAME',
                                               activation=None, scope='conv%d-%d' % (cfg[0], cfg[1]))
                    # it seems bn before activation is generally better
                    if use_batchnorm:
                        bn_layer = tf.keras.layers.BatchNormalization(name='bn')
                        assert is_training is not None
                        net = bn_layer(net, training=is_training)
                    net = tf.nn.relu(net)
    return net

def alexnet_conv_layers(input, auxilary_input=None, prelu_initializer=tf.constant_initializer(0.25), fuse_type='flat'):
    """
    self-implemented AlexNet, with skip-connection
    input: images, expected to be of [batch, width, height, channel]
    """
    def flatten(feat_map):
        feat_map = tf.transpose(feat_map, perm=[0, 3, 1, 2])
        feat_map = tfops.remove_axis(feat_map, [2, 3])
        return feat_map
    assert fuse_type in ['flat', 'spp', 'resize']

    with tf.variable_scope('conv1'):
        if auxilary_input is not None:
            conv1 = tfops.conv_layer(input, 96, filter_size=11, stride=4, padding='VALID', activation=None)
            conv1 = conv1 + auxilary_input  # join before activation
            conv1 = tf.nn.relu(conv1)
        else:
            conv1 = tfops.conv_layer(input, 96, filter_size=11, stride=4, padding='VALID')
        pool1 = tf.nn.max_pool2d(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool1')
        lrn1 = tf.nn.local_response_normalization(pool1, depth_radius=2, alpha=2e-5, beta=0.75, bias=1.0, name='norm1')

    with tf.variable_scope('conv1_skip'):
        conv1_skip = tfops.conv_layer(lrn1, 16, filter_size=1, activation=None)
        conv1_skip = tfops.prelu(conv1_skip, initializer=prelu_initializer)
        if fuse_type == 'flat':  # each img flatten into 1-D
            conv1_skip_flat = flatten(conv1_skip)

    with tf.variable_scope('conv2'):
        # 2-branch by num_groups=2
        conv2 = tfops.conv_layer(lrn1, 256, filter_size=5, num_groups=2, padding='SAME')
        pool2 = tf.nn.max_pool2d(conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool2')
        lrn2 = tf.nn.local_response_normalization(pool2, depth_radius=2, alpha=2e-5, beta=0.75, bias=1.0, name='norm2')

    with tf.variable_scope('conv2_skip'):
        conv2_skip = tfops.conv_layer(lrn2, 32, filter_size=1, activation=None)
        conv2_skip = tfops.prelu(conv2_skip, initializer=prelu_initializer)
        if fuse_type == 'flat':
            conv2_skip_flat = flatten(conv2_skip)

    with tf.variable_scope('conv3'):
        conv3 = tfops.conv_layer(lrn2, 384, filter_size=3, padding='SAME')

    with tf.variable_scope('conv4'):
        conv4 = tfops.conv_layer(conv3, 384, filter_size=3, num_groups=2, padding='SAME')

    with tf.variable_scope('conv5'):
        conv5 = tfops.conv_layer(conv4, 256, filter_size=3, num_groups=2, padding='SAME')
        pool5 = tf.nn.max_pool2d(conv5, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool5')
        if fuse_type == 'flat':
            pool5_flat = flatten(pool5)

    with tf.variable_scope('conv5_skip'):
        conv5_skip = tfops.conv_layer(conv5, 64, filter_size=1, activation=None)
        conv5_skip = tfops.prelu(conv5_skip)
        if fuse_type == 'flat':
            conv5_skip_flat = flatten(conv5_skip)

    with tf.variable_scope('big_concat'):
        # concat all skip layers
        if fuse_type == 'flat':
            feat = [conv1_skip_flat, conv2_skip_flat, conv5_skip_flat, pool5_flat]
        elif fuse_type == 'spp':
            feat = [conv1_skip, conv2_skip, conv5_skip, pool5]
            spp_bin_list = [[27], [13], [13], [6]]  # pool size on pixel = [[4x2, 4x2, 4x2, 5x3]]
            for i, bins in enumerate(spp_bin_list):  # a spp for each layer
                feat[i] = tfops.spatial_pyramid_pooling(feat[i], bins)
        else:  # resize as image
            feat = [conv1_skip, conv2_skip, conv5_skip, pool5]
            size_list = [27, 13, 13, 6]
            for i, sz in enumerate(size_list):
                feat[i] = flatten(tf.image.resize(feat[i], (sz, sz)))
        feat_concat = tf.concat(feat, 1)

    return feat_concat

def re3_lstm_tracker(input, num_unrolls, batch_size, prev_state=None, lstm_size=512, rnn_type='lstm'):
    """
    input: object features in time sequence, expected to be [batch, time, feat_t + feat_t-1], with time = num_unrolls
    prev_state: the initial state for RNN cell, set to placeholder to enable single-step inference
    TODO: migrate to TF 2.0:
        contrib.rnn.LSTMCell -> keras.layers.LSTMCell
        contrib.rnn.LSTMStateTuple -> get_initial_tuple
        dynamic_rnn -> keras.layers.RNN
    """
    assert rnn_type in ['lstm']
    with tf.variable_scope('lstm1'):
        lstm1 = tf.contrib.rnn.LSTMCell(lstm_size)

        # cell state
        if prev_state is not None: # if traker already running
            state1 = tf.contrib.rnn.LSTMStateTuple(prev_state[0], prev_state[1])
        else:
            state1 = lstm1.zero_state(batch_size, dtype=tf.float32)

        # unroll
        lstm1_outputs, state1 = tf.nn.dynamic_rnn(lstm1, input, initial_state=state1, swap_memory=True)

    with tf.variable_scope('lstm2'):
        lstm2 = tf.contrib.rnn.LSTMCell(lstm_size)

        # cell state
        if prev_state is not None: # if still one video (traker already running)
            state2 = tf.contrib.rnn.LSTMStateTuple(prev_state[2], prev_state[3])
        else:
            state2 = lstm2.zero_state(batch_size, dtype=tf.float32)

        # unroll
        lstm2_inputs = tf.concat([input, lstm1_outputs], -1)
        lstm2_outputs, state2 = tf.nn.dynamic_rnn(lstm2, lstm2_inputs, initial_state=state2, swap_memory=True)

        flatten_out = tf.reshape(lstm2_outputs, [-1, lstm2_outputs.get_shape().as_list()[-1]])  # flatten as [batch x time, feat]

    # final dense layer.
    with tf.variable_scope('fc_output'):
        fc_output = tfops.dense_layer(flatten_out, 4, activation=None, weight_name='W_fc', bias_name='b_fc')  # [batch x time, 4]
        fc_output = tf.reshape(fc_output, [-1, num_unrolls, 4])  #  [batch, time, 4]

    return fc_output, (state1, state2)


import math

from pcbflow import *

if __name__ == "__main__":
    brd = Board((40, 30))
    brd.DC((10, 10)).text("Top Text", side="top")
    brd.DC((30, 10)).text("Bottom Text", side="bottom")
    brd.add_text((10, 15), "Test Text 1", scale=1.0, layer="GTO")
    brd.add_text((10, 20), "Copper Text 1", scale=1.0, layer="GTL")
    brd.add_text((10, 25), "Copper Text 2", scale=2.0, layer="GTL", keepout_box=True)
    brd.add_text((20, 10), "Copper Text 3", side="bottom", scale=2.0, layer="GBL")
    brd.add_text(
        (20, 15),
        "Copper Text 4",
        side="bottom",
        scale=2.0,
        layer="GBL",
        keepout_box=True,
    )
    brd.add_text(
        (20, 25),
        "Copper Text 5",
        side="bottom",
        scale=2.0,
        layer="GBL",
        keepout_box=True,
        soldermask_box=True,
    )
    brd.add_outline()
    brd.fill_layer("GTL", "GND")
    brd.fill_layer("GBL", "GND")
    brd.save("%s" % (__file__[:-3]))


import base64
import binascii
import random
from utilities import util

# Challenge 16

def unpadding_validation(string):
  k = string[-1]
  for i in range(len(string)-1, len(string) - 1 - ord(k), -1):
    if string[i] != k:
      raise PaddingError('Inappropriate padding detected')
  return string[0:len(string)-ord(k)]

class PaddingError(Exception):
  def __init__(self, value):
    self.value = value
  def __str__(self):
    return repr(self.value)

key = b'\x01\x1f\x89\x94\x85{\x8e\xa4\xfa\x8e\xc9\xc3{\x1dz\x06'

def cbc_encrypt_surround(chosen):
  prefix = b'comment1=cooking%20MCs;userdata='
  secret = b';comment2=%20like%20a%20pound%20of%20bacon'
  chosen = chosen.replace(b';', b'').replace(b'=',b'')
  return util.cbc_encrypt(prefix + chosen + secret, key)

def cbc_decrypt_surround(ciphertext):
  plaintext = util.cbc_decrypt(ciphertext, key)
  if plaintext.find(b';admin=true;') != -1:
    print(plaintext)
    return True
  return False

def cbc_bitflipping_attack():
  ciphertext = cbc_encrypt_surround(b'')
  BLOCK_SIZE = 16
  num_blocks = len(ciphertext)//BLOCK_SIZE
  first_block_ciphertext = util.get_ith_block(ciphertext, 0, BLOCK_SIZE)
  second_block_plaintext = b'%20MCs;userdata='

  desired_text = util.padding(b';admin=true;', BLOCK_SIZE)
  fixed_first_block = b''

  for i in range(BLOCK_SIZE):
    fixed_first_block += bytes([second_block_plaintext[i]^first_block_ciphertext[i]^desired_text[i]])

  fixed_ciphertext = fixed_first_block + ciphertext[BLOCK_SIZE:]
  print(cbc_decrypt_surround(fixed_ciphertext))

if __name__ == '__main__':
  cbc_bitflipping_attack()


"""Balanced Parentheses Program

This program is used to check whether user given arithmetic expression is balanced or not

Example:
    Balaced Expression :: {{a+b}*[a-b]}

    Unbalanced Expression:: {{a+b}*[a-b]

Author:
    Saurabh <singh.saurabh3333@gmail.com>

Since:
    20 Nov,2018

"""

from com.bridgelabz.util.datastructure_util import *
from com.bridgelabz.util.utility import *

def balance_parentheses():
    """
    This method is used as runner balanced_parentheses(string) method

    :return:  nothing
    """
    utility_obj = Utility()
    stack = Stack()
    print("Enter Expression to check for balanced Parentheses")
    try:
        string = utility_obj.get_string()
    except Exception as e:
        print(e)
        print("Enter String")

    stack.balanced_parentheses(string)

if __name__ == "__main__":
    balance_parentheses()


import pydot

DEFAULT_NODE_ATTRS = {
    'color': 'cyan',
    'shape': 'box',
    'style': 'rounded',
    'fontname': 'palatino',
    'fontsize': 10,
    'penwidth': 2
}

def node_label(token):
    try:
        label = token._.plot['label']
    except:
        label = '{0} [{1}]\n({2} / {3})'.format(
            token.orth_,
            token.i,
            token.pos_,
            token.tag_
        )
    return label

def get_edge_label(from_token, to_token):
    label = ' ' + from_token.dep_
    return label

def to_pydot(tokens, get_edge_label=get_edge_label):
    graph = pydot.Dot(graph_type='graph')

    # Add nodes to graph
    idx2node = {}
    for token in tokens:
        try:
            plot_attrs = token._.plot
        except AttributeError:
            plot_attrs = {}
        for attr, val in DEFAULT_NODE_ATTRS.items():
            if attr not in plot_attrs:
                plot_attrs[attr] = val
        label = node_label(token)
        plot_attrs['name'] = token.i
        plot_attrs['label'] = label
        node = pydot.Node(**plot_attrs)
        idx2node[token.i] = node
        graph.add_node(node)

    '''Add edges'''
    for token in tokens:
        if token.dep_ == 'ROOT':
            continue
        if token.head not in tokens:
            continue
        from_token = token
        to_token = token.head
        from_node = idx2node[from_token.i]
        to_node = idx2node[to_token.i]
        label = get_edge_label(from_token, to_token)
        edge = pydot.Edge(
            to_node, from_node, label=label,
            fontsize=12
        )
        graph.add_edge(edge)

    return graph

def create_png(tokens, prog=None):
    graph = to_pydot(tokens)
    png = graph.create_png(prog=prog)
    return png


a = [27,3,-91,2,99,52,1,-10]

def merge_sort(lst):
    # Base case: A 1- or 0-element list is already sorted
    # If we encounter one, return it immediately
    if len(lst) <= 1:
        return lst[:]

    # Find the midpoint of the list
    midpt = int(len(lst) / 2)

    # Create left and right halves from the midpoint
    left = lst[:midpt]
    right = lst[midpt:]

    # Recursively merge sort both the left and right halves.
    # Once these calls return, new_left and new_right are
    # guaranteed to be in sorted order.
    new_left = merge_sort(left)
    new_right = merge_sort(right)

    # Create a new list to merge the contents of new_left and new_right
    new_lst = []

    # Merge operation: Copy the elements of new_left and new_right
    # into the merged array so they are in order
    i = 0
    j = 0
    while i < len(new_left) and j < len(new_right):
        if new_left[i] < new_right[j]:
            new_lst.append(new_left[i])
            i += 1
        else:
            new_lst.append(new_right[j])
            j += 1

    # Clean-up step: either new_left or new_right are
    # likely to have leftover elements. If so, add them.
    if i < len(new_left):
        for x in range(i, len(new_left)):
            new_lst.append(new_left[x])
    elif j < len(new_right):
        for y in range(j, len(new_right)):
            new_lst.append(new_right[y])

    # new_lst is now sorted. Return it.
    return new_lst

print(a)
print(merge_sort(a))


#
# @lc app=leetcode id=583 lang=python3
#
# [583] Delete Operation for Two Strings
#

# @lc code=start
class Solution:
    def minDistance(self, word1: str, word2: str) -> int:
        m, n = len(word1), len(word2)
        dp = [i for i in range(0, n+1)]
        for i in range(1, m+1):
            prev = dp[:]
            dp[0] += 1
            for j in range(1, n+1):
                if word1[i - 1] == word2[j - 1]:
                    dp[j] = prev[j-1]
                else:
                    dp[j] = min(prev[j-1] + 2, prev[j]+1, dp[j-1] + 1)
        return dp[-1]

Solution().minDistance("sea", "eat")

# @lc code=end


import tensorflow as tf
from pdb import set_trace as st

from dovebirdia.deeplearning.networks.base import AbstractNetwork
from dovebirdia.deeplearning.networks.base import FeedForwardNetwork
from dovebirdia.deeplearning.networks.autoencoder import Autoencoder
from dovebirdia.datasets.ccdc_mixtures import ccdcMixturesDataset
from dovebirdia.datasets.mnist import MNISTDataset

# load MNIST
mnist_params = {
    #'dataset_dir':'/home/mlweiss/Documents/wpi/research/code/sensors/mixtures/datasets/02_05_19-0905144322/',
    'val_size':0.1,
    'supervised':True,
    'with_val':True,
    'onehot':False,
    #'resistance_type':'resistance_z',
    #'labels':None,
    #'sensors':None,
    #'with_synthetic':True,
    }

ccdc_params = {
    'dataset_dir':'/home/mlweiss/Documents/wpi/research/data/ccdc/dvd_dump_clark_3/split/07_12_19-1203141455/',
    'with_val':True,
    'resistance_type':'resistance',
    'labels':None,
    'sensors':None,
    'with_synthetic':True,
    }

dataset = MNISTDataset(params=mnist_params).getDataset()

for k,v in dataset.items():

    print(k,v.shape)

# parameters dictionary
params = dict()

# network params
params['input_dim'] = 784
params['output_dim'] = 10
params['hidden_dims'] = [ 128, 64, 16 ]
params['output_activation'] = tf.nn.sigmoid
params['activation'] = tf.nn.sigmoid
params['use_bias'] = True
params['kernel_initializer'] = 'glorot_normal'
params['weight_initializer'] = tf.initializers.glorot_uniform
params['bias_initializer'] = tf.initializers.zeros #'zeros'
params['kernel_regularizer'] = None
params['weight_regularizer'] = None
params['bias_regularizer'] = None
params['activity_regularizer'] = None
params['kernel_constraint'] = None
params['bias_constraint'] = None

# loss
params['loss'] = tf.losses.mean_squared_error
#tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
params['history_size'] = 100

# training
params['epochs'] = 2
params['mbsize'] = 32
params['optimizer'] = tf.train.AdamOptimizer
params['learning_rate'] = 1e-3
params['res_dir'] = 'results/'

# Network
nn = FeedForwardNetwork(params)
print(nn.__class__)
nn.fit(dataset)