# coding : utf-8
import requests
from bs4 import BeautifulSoup

nameList = []
yearList = []

# 豆瓣1988年的电影一共有250部左右，共分为13页，每页20部
# 其命名规则为: https://movie.douban.com/tag/1988?start=NNN&type=T，其中NNN为20的倍数
# 首先获取所有页面的列表
pagelist = []
for i in range(13):
    url = 'https://movie.douban.com/tag/1988?start=' + str(20*i) + '&type=T'
    pagelist.append(url)

# 如果做一个二次循环，理论上是可以将1988年～2017年所有的电影都爬下来的
# 不过懒得弄了，做个1988年的算了

# 开始爬取页面
for p in pagelist:
    res = requests.get(p)
    # print(res.status_code)      # 返回结果为200，可以正常爬取
    # print(res.encoding)         # 返回值为 UTF-8
    # print(res.content)        # 可以显示出一坨一坨，未格式化的内容

    # 使用 BeautifulSoup 进行页面解析
    soup = BeautifulSoup(res.content, 'lxml')      # bs4将内容进行格式化，便于后续的解析
    # a = soup.find_all('a', 'nbg')
    totalName = soup.find_all('div', {'class' : 'pl2'})
    totalYear = soup.find_all('p', {'class' : 'pl'})

    # Get name of the movies
    for i in totalName:
        temp = list(i.a)
        name = str(temp[0]).replace(' ', '').replace('/', '').replace('\n', '')
        nameList.append(name)

    # Get year of the movies
    for i in totalYear:
        temp = str(i).split('/')
        year = str(temp[0]).replace('<p class="pl">', '')
        if year[0].isdigit():
            yearList.append(year)
        else:
            yearList.append('N/A')

print("豆瓣收录了 %d 部1988年的电影，分别是：\n" % len(yearList))
for i in range(len(yearList)):
    print(nameList[i], '\t', yearList[i])


#!/usr/bin/env python3
"""
Executes main() function of class defined in argv[0]
Potentially unsafe but convenient.

Example usage:
alp.py examples.echobasic --COMMENT hello
"""
import importlib
import inspect
import sys
import re
import logging

LOGGER = logging.getLogger()
HANDLER = logging.StreamHandler()
FORMATTER = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
HANDLER.setFormatter(FORMATTER)
LOGGER.addHandler(HANDLER)
LOGGER.setLevel(logging.INFO)

def main():
    """main, find applicake app and execute main."""
    appliapp = None
    cls = None
    if len(sys.argv) <= 1 or not re.match(r"^[\w\.]+$", sys.argv[1]):
        print("Usage: %s NODE [OPTIONS]; e.g. %s examples.echobasic --COMMENT comment" %
              (sys.argv[0], sys.argv[0]))
        sys.exit(1)
    if '--WORKDIR' not in sys.argv:
        sys.argv.append('--WORKDIR')
        sys.argv.append('.')
    appliapp = 'appliapps.' + sys.argv[1]
    try:
        module = importlib.import_module(appliapp)
        for _, obj in inspect.getmembers(module):
            if inspect.isclass(obj) and appliapp in obj.__module__:
                cls = obj
    except ModuleNotFoundError as error:
        raise ModuleNotFoundError('Could not find/load app [%s]: %s' % (appliapp, str(error)))
    try:
        cls.main()
    except KeyError as error:
        raise KeyError('Missing key [%s]: %s' % (appliapp, str(error)))
    except Exception as error:
        raise Exception('General exception, could not run app [%s]: %s' % (appliapp, str(error)))

if __name__ == "__main__":
    main()


import torch
from torch import nn
import numbers
import random
from torch import Tensor

from PIL import ImageFilter
from torchvision import transforms

class DifferentiableColourDistortionByTorch2(nn.Module):

    def __init__(self, brightness=0, contrast=0, saturation=0, hue=0):
        super().__init__()
        self.brightness = self._check_input(brightness, 'brightness')
        self.contrast = self._check_input(contrast, 'contrast')
        self.saturation = self._check_input(saturation, 'saturation')
        self.hue = self._check_input(hue, 'hue', center=0, bound=(-0.5, 0.5),
                                     clip_first_on_zero=False)

    def _check_input(self, value, name, center=1, bound=(0, float('inf')), clip_first_on_zero=True):
        if isinstance(value, numbers.Number):
            if value < 0:
                raise ValueError("If {} is a single number, it must be non negative.".format(name))
            value = [center - float(value), center + float(value)]
            if clip_first_on_zero:
                value[0] = max(value[0], 0.0)
        elif isinstance(value, (tuple, list)) and len(value) == 2:
            if not bound[0] <= value[0] <= value[1] <= bound[1]:
                raise ValueError("{} values should be between {}".format(name, bound))
        else:
            raise TypeError("{} should be a single number or a list/tuple with lenght 2.".format(name))

        # if value is 0 or (1., 1.) for brightness/contrast/saturation
        # or (0., 0.) for hue, do nothing
        if value[0] == value[1] == center:
            value = None
        return value

    #### batch color augmentation forward #####

    def forward(self, x):
        '''
        Args:
            x: Input tensor batchsize x 32 x 32

        Returns:
            x_aug : color jittered image

        apply color jitter with prob 0.8
        apply random grayscale with prob 0.2

        '''
        batch_size = x.size()[0]

        p_jitter = torch.ones(batch_size) * 0.8
        jitter = torch.bernoulli(p_jitter)
        jitter = jitter.reshape(batch_size,1,1,1)

        p_gray = torch.ones(batch_size)* 0.2
        gray = torch.bernoulli(p_gray)
        gray = gray.reshape(batch_size,1,1,1)

        jitter = jitter.to(x.device)
        gray = gray.to(x.device)

        #random color jitter
        x_jitter = self.batch_colourjitter(x)
        x = x_jitter * jitter + x *(1-jitter)

        #random gray scale
        x_gray = self.batch_rgb_to_grayscale(x).unsqueeze(1)
#         print(x_gray.size())
#         print(gray.size())
        x = x_gray * gray + x* (1-gray)

        return x

    def batch_colourjitter(self, img: Tensor) -> Tensor:

        fn_idx = torch.randperm(4)
        for fn_id in fn_idx:
            if fn_id == 0 and self.brightness is not None:
                brightness_bound = self.brightness
                img = self.batch_adjust_brightness(img, brightness_bound)

            if fn_id == 1 and self.contrast is not None:
                contrast_bound = self.contrast
                img = self.batch_adjust_contrast(img, contrast_bound)

            if fn_id == 2 and self.saturation is not None:
                saturation_bound = self.saturation
                img = self.batch_adjust_saturation(img, saturation_bound)

            if fn_id == 3 and self.hue is not None:
                hue_bound = self.hue
                img = self.batch_adjust_hue(img, hue_bound)

        return img

    def __repr__(self):
        format_string = self.__class__.__name__ + '('
        format_string += 'brightness={0}'.format(self.brightness)
        format_string += ', contrast={0}'.format(self.contrast)
        format_string += ', saturation={0}'.format(self.saturation)
        format_string += ', hue={0})'.format(self.hue)
        return format_string

    ##### function from pytorch source code #####
    def _is_tensor_a_torch_image(self, x: Tensor) -> bool:
        return x.ndim >= 2

    def _blend(self, img1: Tensor, img2: Tensor, ratio: float) -> Tensor:
        bound = 1 if img1.dtype in [torch.half, torch.float32, torch.float64] else 255
        return (ratio * img1 + (1 - ratio) * img2).clamp(0, bound).to(img1.dtype)

    # batch adjust brightness
    def batch_adjust_brightness(self,img: Tensor, brightness_bound: list) -> Tensor:
        '''
        Batch x C x H x  W -> Batch x C x H x W
        '''
        B = img.size()[0]
        brightness_list = torch.torch.rand(B)*(brightness_bound[1] - brightness_bound[0]) + brightness_bound[0]
        brightness_factor = brightness_list.reshape(B,1,1,1).to(img.device)
        return self._blend(img, torch.zeros_like(img), brightness_factor)

    def batch_rgb_to_grayscale(self,img: Tensor) -> Tensor:
        '''
        Batch x C x H x  W -> Batch x C x H x W
        '''
        if img.shape[1] != 3:
            raise TypeError('Input Image does not contain 3 Channels')

        img_tp = img.transpose(0,1)
        img_gray = (0.2989 * img_tp[0] + 0.5870 * img_tp[1] + 0.1140 * img_tp[2])
        return img_gray

    def batch_adjust_saturation(self,img: Tensor, saturation_bound: list) -> Tensor:
        '''
        Batch x C x H x  W -> Batch x C x H x W
        '''

        B = img.size()[0]
        saturation_list = torch.torch.rand(B)*(saturation_bound[1] - saturation_bound[0]) + saturation_bound[0]
        saturation_factor = saturation_list.reshape(B,1,1,1).to(img.device)
    #     print(saturation_factor.size())

        return self._blend(img, self.batch_rgb_to_grayscale(img).unsqueeze(1), saturation_factor)

    def batch_adjust_contrast(self,img: Tensor, contrast_bound: float) -> Tensor:
        '''
        Batch x C x H x  W -> Batch x C x H x W
        '''
        B = img.size()[0]
        contrast_list = torch.torch.rand(B)*(contrast_bound[1] - contrast_bound[0]) + contrast_bound[0]
        contrast_list = contrast_list.reshape(B,1,1,1).to(img.device)

        #mean for each pic (over HxW points)
        img_gray = self.batch_rgb_to_grayscale(img)
        mean = torch.mean(img_gray.view(img_gray.shape[0],-1), dim = 1)

        return self._blend(img, mean.reshape([mean.size()[0],1,1,1]),  contrast_list)

    def batch_adjust_hue(self, img: Tensor, hue_bound: float) -> Tensor:
        '''
        Batch x C x H x  W -> Batch x C x H x W
        '''
        B = img.size()[0]
        hue_list = torch.torch.rand(B)*(hue_bound[1] - hue_bound[0]) + hue_bound[0]
        hue_list = hue_list.to(img.device)

        #unbind
        img_u = torch.unbind(img, dim = 0)

        return torch.stack([self.adjust_hue(img_i, hue_list[i]) for i, img_i in enumerate(img_u)], dim = 0)

    def _rgb2hsv(self, img):
        r, g, b = img.unbind(0)

        maxc = torch.max(img, dim=0).values
        minc = torch.min(img, dim=0).values

        # The algorithm erases S and H channel where `maxc = minc`. This avoids NaN
        # from happening in the results, because
        #   + S channel has division by `maxc`, which is zero only if `maxc = minc`
        #   + H channel has division by `(maxc - minc)`.
        #
        # Instead of overwriting NaN afterwards, we just prevent it from occuring so
        # we don't need to deal with it in case we save the NaN in a buffer in
        # backprop, if it is ever supported, but it doesn't hurt to do so.
        eqc = maxc == minc

        cr = maxc - minc
        # Since `eqc => cr = 0`, replacing denominator with 1 when `eqc` is fine.
        s = cr / torch.where(eqc, maxc.new_ones(()), maxc)
        # Note that `eqc => maxc = minc = r = g = b`. So the following calculation
        # of `h` would reduce to `bc - gc + 2 + rc - bc + 4 + rc - bc = 6` so it
        # would not matter what values `rc`, `gc`, and `bc` have here, and thus
        # replacing denominator with 1 when `eqc` is fine.
        cr_divisor = torch.where(eqc, maxc.new_ones(()), cr)
        rc = (maxc - r) / cr_divisor
        gc = (maxc - g) / cr_divisor
        bc = (maxc - b) / cr_divisor

    #     hr = (maxc == r) * (bc - gc)
    #     hg = ((maxc == g) & (maxc != r)) * (2.0 + rc - bc)
    #     hb = ((maxc != g) & (maxc != r)) * (4.0 + gc - rc)
        hr = (maxc == r).float() * (bc - gc)
        hg = ((maxc == g) & (maxc != r)).float() * (2.0 + rc - bc)
        hb = ((maxc != g) & (maxc != r)).float() * (4.0 + gc - rc)
        h = (hr + hg + hb)
        h = torch.fmod((h / 6.0 + 1.0), 1.0)
        return torch.stack((h, s, maxc))

    def _hsv2rgb(self, img):
        h, s, v = img.unbind(0)
        i = torch.floor(h * 6.0)
        f = (h * 6.0) - i
        i = i.to(dtype=torch.int32)

        p = torch.clamp((v * (1.0 - s)), 0.0, 1.0)
        q = torch.clamp((v * (1.0 - s * f)), 0.0, 1.0)
        t = torch.clamp((v * (1.0 - s * (1.0 - f))), 0.0, 1.0)
        i = i % 6

    #     mask = i == torch.arange(6)[:, None, None]
        mask = (i == torch.arange(6)[:, None, None].to(img.device).int())
        a1 = torch.stack((v, q, p, p, t, v))
        a2 = torch.stack((t, v, v, q, p, p))
        a3 = torch.stack((p, p, t, v, v, q))
        a4 = torch.stack((a1, a2, a3))

        return torch.einsum("ijk, xijk -> xjk", mask.to(dtype=img.dtype), a4)

    def adjust_hue(self, img, hue_factor):

        if not (-0.5 <= hue_factor <= 0.5):
            raise ValueError('hue_factor ({}) is not in [-0.5, 0.5].'.format(hue_factor))

        if not self._is_tensor_a_torch_image(img):
            raise TypeError('tensor is not a torch image.')

        orig_dtype = img.dtype
        if img.dtype == torch.uint8:
            img = img.to(dtype=torch.float32) / 255.0

        img = self._rgb2hsv(img)
        h, s, v = img.unbind(0)
        h += hue_factor
        h = h % 1.0
        img = torch.stack((h, s, v))
        img_hue_adj = self._hsv2rgb(img)

        if orig_dtype == torch.uint8:
            img_hue_adj = (img_hue_adj * 255.0).to(dtype=orig_dtype)

        return img_hue_adj

def ManualNormalise(x, dataset):
    CACHED_MEAN_STD = {
        'cifar10': ((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
        'cifar100': ((0.5071, 0.4865, 0.4409), (0.2009, 0.1984, 0.2023)),
        'stl10': ((0.4409, 0.4279, 0.3868), (0.2309, 0.2262, 0.2237)),
        'imagenet': ((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
    }

    mean = CACHED_MEAN_STD[dataset][0]
    std = CACHED_MEAN_STD[dataset][1]

    #batch x channel x height x wide
    B = x.size()[0]
    C = x.size()[1]
    H = x.size()[2]
    W = x.size()[3]

    #create a matrix
    mean_batch = torch.stack([torch.tensor(mean[i]).repeat(H,W) for i in range(C)], dim = 0).repeat(B,1,1,1)
    std_batch = torch.stack([torch.tensor(std[i]).repeat(H,W) for i in range(C)], dim = 0).repeat(B,1,1,1)
    mean_batch = mean_batch.to(x.device)
    std_batch = std_batch.to(x.device)

    norm_x = (x - mean_batch)/std_batch

    return norm_x

class DifferentiableColourDistortionByTorch(nn.Module):

    def __init__(self, brightness=0, contrast=0, saturation=0, hue=0):
        super().__init__()
        self.brightness = self._check_input(brightness, 'brightness')
        self.contrast = self._check_input(contrast, 'contrast')
        self.saturation = self._check_input(saturation, 'saturation')
        self.hue = self._check_input(hue, 'hue', center=0, bound=(-0.5, 0.5),
                                     clip_first_on_zero=False)

#     @torch.jit.unused
    def _check_input(self, value, name, center=1, bound=(0, float('inf')), clip_first_on_zero=True):
        if isinstance(value, numbers.Number):
            if value < 0:
                raise ValueError("If {} is a single number, it must be non negative.".format(name))
            value = [center - float(value), center + float(value)]
            if clip_first_on_zero:
                value[0] = max(value[0], 0.0)
        elif isinstance(value, (tuple, list)) and len(value) == 2:
            if not bound[0] <= value[0] <= value[1] <= bound[1]:
                raise ValueError("{} values should be between {}".format(name, bound))
        else:
            raise TypeError("{} should be a single number or a list/tuple with lenght 2.".format(name))

        # if value is 0 or (1., 1.) for brightness/contrast/saturation
        # or (0., 0.) for hue, do nothing
        if value[0] == value[1] == center:
            value = None
        return value

    #### batch color augmentation forward #####

    def forward(self, x):
        '''
        Args:
            x: Input tensor batchsize x 32 x 32

        Returns:
            x_aug : color jittered image

        '''
        xu = torch.unbind(x, dim=0)
        aug_x = torch.stack([self.colordistort(x_i) for i,x_i in enumerate(xu)], dim = 0)

        return aug_x

    def colordistort(self, img: Tensor)-> Tensor:
        if (torch.rand(1)<0.8):
            img = self.colorjitter(img)
            #print(x_i.shape)
        if (torch.rand(1)<0.2):
            img = self.rgb_to_grayscale(img)
            img = img.repeat(3,1,1) # reshape to r==g==b
            #print(x_i.shape)
        return img

    def colorjitter(self, img):
        """
        Args:
            img (PIL Image or Tensor): Input image.

        Returns:
            PIL Image or Tensor: Color jittered image.
        """
        #### index
#         brightness_factor = None
#         contrast_factor = None
#         saturation_factor = None
#         hue_factor = None

        fn_idx = torch.randperm(4)
        for fn_id in fn_idx:
            if fn_id == 0 and self.brightness is not None:
                brightness = self.brightness
                brightness_factor = torch.tensor(1.0).uniform_(brightness[0], brightness[1]).item()
                img = self.adjust_brightness(img, brightness_factor)

            if fn_id == 1 and self.contrast is not None:
                contrast = self.contrast
                contrast_factor = torch.tensor(1.0).uniform_(contrast[0], contrast[1]).item()
                img = self.adjust_contrast(img, contrast_factor)

            if fn_id == 2 and self.saturation is not None:
                saturation = self.saturation
                saturation_factor = torch.tensor(1.0).uniform_(saturation[0], saturation[1]).item()
                img = self.adjust_saturation(img, saturation_factor)

            if fn_id == 3 and self.hue is not None:
                hue = self.hue
                hue_factor = torch.tensor(1.0).uniform_(hue[0], hue[1]).item()
                img = self.adjust_hue(img, hue_factor)

        #also return the coefficient
#         format_string = self.__class__.__name__ + '('
#         format_string += 'brightness={:.3f}'.format(brightness_factor)
#         format_string += ', contrast={:.3f}'.format(contrast_factor)
#         format_string += ', saturation={:.3f}'.format(saturation_factor)
#         format_string += ', hue={:.3f})'.format(hue_factor)
        #print(format_string)

        return img

    def __repr__(self):
        format_string = self.__class__.__name__ + '('
        format_string += 'brightness={0}'.format(self.brightness)
        format_string += ', contrast={0}'.format(self.contrast)
        format_string += ', saturation={0}'.format(self.saturation)
        format_string += ', hue={0})'.format(self.hue)
        return format_string

    ##### function from pytorch source code #####
    def _is_tensor_a_torch_image(self, x: Tensor) -> bool:
        return x.ndim >= 2

    def _blend(self, img1: Tensor, img2: Tensor, ratio: float) -> Tensor:
        bound = 1 if img1.dtype in [torch.half, torch.float32, torch.float64] else 255
        return (ratio * img1 + (1 - ratio) * img2).clamp(0, bound).to(img1.dtype)

    def rgb_to_grayscale(self, img: Tensor) -> Tensor:
        """Convert the given RGB Image Tensor to Grayscale.
        For RGB to Grayscale conversion, ITU-R 601-2 luma transform is performed which
        is L = R * 0.2989 + G * 0.5870 + B * 0.1140

        Args:
            img (Tensor): Image to be converted to Grayscale in the form [C, H, W].

        Returns:
            Tensor: Grayscale image.

        """
        if img.shape[0] != 3:
            raise TypeError('Input Image does not contain 3 Channels')

        return (0.2989 * img[0] + 0.5870 * img[1] + 0.1140 * img[2]).to(img.dtype)

    def _rgb2hsv(self, img):
        r, g, b = img.unbind(0)

        maxc = torch.max(img, dim=0).values
        minc = torch.min(img, dim=0).values

        # The algorithm erases S and H channel where `maxc = minc`. This avoids NaN
        # from happening in the results, because
        #   + S channel has division by `maxc`, which is zero only if `maxc = minc`
        #   + H channel has division by `(maxc - minc)`.
        #
        # Instead of overwriting NaN afterwards, we just prevent it from occuring so
        # we don't need to deal with it in case we save the NaN in a buffer in
        # backprop, if it is ever supported, but it doesn't hurt to do so.
        eqc = maxc == minc

        cr = maxc - minc
        # Since `eqc => cr = 0`, replacing denominator with 1 when `eqc` is fine.
        s = cr / torch.where(eqc, maxc.new_ones(()), maxc)
        # Note that `eqc => maxc = minc = r = g = b`. So the following calculation
        # of `h` would reduce to `bc - gc + 2 + rc - bc + 4 + rc - bc = 6` so it
        # would not matter what values `rc`, `gc`, and `bc` have here, and thus
        # replacing denominator with 1 when `eqc` is fine.
        cr_divisor = torch.where(eqc, maxc.new_ones(()), cr)
        rc = (maxc - r) / cr_divisor
        gc = (maxc - g) / cr_divisor
        bc = (maxc - b) / cr_divisor

    #     hr = (maxc == r) * (bc - gc)
    #     hg = ((maxc == g) & (maxc != r)) * (2.0 + rc - bc)
    #     hb = ((maxc != g) & (maxc != r)) * (4.0 + gc - rc)
        hr = (maxc == r).float() * (bc - gc)
        hg = ((maxc == g) & (maxc != r)).float() * (2.0 + rc - bc)
        hb = ((maxc != g) & (maxc != r)).float() * (4.0 + gc - rc)
        h = (hr + hg + hb)
        h = torch.fmod((h / 6.0 + 1.0), 1.0)
        return torch.stack((h, s, maxc))

    def _hsv2rgb(self, img):
        h, s, v = img.unbind(0)
        i = torch.floor(h * 6.0)
        f = (h * 6.0) - i
        i = i.to(dtype=torch.int32)

        p = torch.clamp((v * (1.0 - s)), 0.0, 1.0)
        q = torch.clamp((v * (1.0 - s * f)), 0.0, 1.0)
        t = torch.clamp((v * (1.0 - s * (1.0 - f))), 0.0, 1.0)
        i = i % 6

    #     mask = i == torch.arange(6)[:, None, None]
        mask = (i == torch.arange(6)[:, None, None].to(img.device).int())
        a1 = torch.stack((v, q, p, p, t, v))
        a2 = torch.stack((t, v, v, q, p, p))
        a3 = torch.stack((p, p, t, v, v, q))
        a4 = torch.stack((a1, a2, a3))

        return torch.einsum("ijk, xijk -> xjk", mask.to(dtype=img.dtype), a4)

    #### Brightness ####
    def adjust_brightness(self, img: Tensor, brightness_factor: float) -> Tensor:
        """Adjust brightness of an RGB image.

        Args:
            img (Tensor): Image to be adjusted.
            brightness_factor (float):  How much to adjust the brightness. Can be
                any non negative number. 0 gives a black image, 1 gives the
                original image while 2 increases the brightness by a factor of 2.

        Returns:
            Tensor: Brightness adjusted image.
        """
        if brightness_factor < 0:
            raise ValueError('brightness_factor ({}) is not non-negative.'.format(brightness_factor))

        if not self._is_tensor_a_torch_image(img):
            raise TypeError('tensor is not a torch image.')

        return self._blend(img, torch.zeros_like(img), brightness_factor)

    #### Contrast ####
    def adjust_contrast(self, img: Tensor, contrast_factor: float) -> Tensor:
        """Adjust contrast of an RGB image.

        Args:
            img (Tensor): Image to be adjusted.
            contrast_factor (float): How much to adjust the contrast. Can be any
                non negative number. 0 gives a solid gray image, 1 gives the
                original image while 2 increases the contrast by a factor of 2.

        Returns:
            Tensor: Contrast adjusted image.
        """
        if contrast_factor < 0:
            raise ValueError('contrast_factor ({}) is not non-negative.'.format(contrast_factor))

        if not self._is_tensor_a_torch_image(img):
            raise TypeError('tensor is not a torch image.')

        mean = torch.mean(self.rgb_to_grayscale(img).to(torch.float))

        return self._blend(img, mean, contrast_factor)

    #### Hue ####
    def adjust_hue(self, img, hue_factor):
        """Adjust hue of an image.

        The image hue is adjusted by converting the image to HSV and
        cyclically shifting the intensities in the hue channel (H).
        The image is then converted back to original image mode.

        `hue_factor` is the amount of shift in H channel and must be in the
        interval `[-0.5, 0.5]`.

        See `Hue`_ for more details.

        .. _Hue: https://en.wikipedia.org/wiki/Hue

        Args:
            img (Tensor): Image to be adjusted. Image type is either uint8 or float.
            hue_factor (float):  How much to shift the hue channel. Should be in
                [-0.5, 0.5]. 0.5 and -0.5 give complete reversal of hue channel in
                HSV space in positive and negative direction respectively.
                0 means no shift. Therefore, both -0.5 and 0.5 will give an image
                with complementary colors while 0 gives the original image.

        Returns:
             Tensor: Hue adjusted image.
        """
        if not (-0.5 <= hue_factor <= 0.5):
            raise ValueError('hue_factor ({}) is not in [-0.5, 0.5].'.format(hue_factor))

        if not self._is_tensor_a_torch_image(img):
            raise TypeError('tensor is not a torch image.')

        orig_dtype = img.dtype
        if img.dtype == torch.uint8:
            img = img.to(dtype=torch.float32) / 255.0

        img = self._rgb2hsv(img)
        h, s, v = img.unbind(0)
        h += hue_factor
        h = h % 1.0
        img = torch.stack((h, s, v))
        img_hue_adj = self._hsv2rgb(img)

        if orig_dtype == torch.uint8:
            img_hue_adj = (img_hue_adj * 255.0).to(dtype=orig_dtype)

        return img_hue_adj

    def adjust_saturation(self, img: Tensor, saturation_factor: float) -> Tensor:
        """Adjust color saturation of an RGB image.

        Args:
            img (Tensor): Image to be adjusted.
            saturation_factor (float):  How much to adjust the saturation. Can be any
                non negative number. 0 gives a black and white image, 1 gives the
                original image while 2 enhances the saturation by a factor of 2.

        Returns:
            Tensor: Saturation adjusted image.
        """
        if saturation_factor < 0:
            raise ValueError('saturation_factor ({}) is not non-negative.'.format(saturation_factor))

        if not self._is_tensor_a_torch_image(img):
            raise TypeError('tensor is not a torch image.')

        return self._blend(img, self.rgb_to_grayscale(img), saturation_factor)

class DifferentiableColourDistortion(nn.Module):

    def __init__(self):
        super().__init__()
        self.linear_dist = torch.distributions.MultivariateNormal(
            torch.tensor([ 0.98,  0.06,  0.04, -0.03,  0.84, -0.04,  0.05,  0.09,  0.99, -0.00, -0.00, -0.00]),
            covariance_matrix=torch.tensor(
        [[ 0.18074650,  0.00123624, -0.02651235, -0.09860805,  0.09091195, -0.07780737, -0.00920981, -0.01918633,
          0.17576791, -0.00834481, -0.00830104, -0.00805414],
        [ 0.00123624,  0.10395385,  0.01573675, -0.02785348, -0.04892306,  0.04504404,  0.03578304, -0.04570618,
         -0.05145603, -0.00118306, -0.00125381, -0.00119490],
        [-0.02651235,  0.01573675,  0.04726495,  0.03685538, -0.03615827,  0.00513693, -0.00279980,  0.02796142,
         -0.04439921, -0.00095502, -0.00097433, -0.00106152],
        [-0.09860805, -0.02785348,  0.03685538,  0.12118191, -0.00126875,  0.06086478, -0.01757026,  0.03399952,
         -0.09201879, -0.00075510, -0.00075904, -0.00088032],
        [ 0.09091195, -0.04892306, -0.03615827, -0.00126875,  0.13618506, -0.00899528, -0.02416800, -0.02172066,
          0.10960858, -0.00748678, -0.00747979, -0.00730574],
        [-0.07780737,  0.04504404,  0.00513693,  0.06086478, -0.00899528,  0.10825535,  0.02111831, -0.03186969,
         -0.10883092, -0.00062423, -0.00066785, -0.00071586],
        [-0.00920981,  0.03578304, -0.00279980, -0.01757026, -0.02416800,  0.02111831,  0.03327096, -0.00517776,
         -0.01198933, -0.00083387, -0.00084015, -0.00080103],
        [-0.01918633, -0.04570618,  0.02796142,  0.03399952, -0.02172066, -0.03186969, -0.00517776,  0.07695913,
          0.01365563, -0.00124058, -0.00119133, -0.00125144],
        [ 0.17576791, -0.05145603, -0.04439921, -0.09201879,  0.10960858, -0.10883092, -0.01198933,  0.01365563,
          0.22387496, -0.00818232, -0.00812927, -0.00789771],
        [-0.00834481, -0.00118306, -0.00095502, -0.00075510, -0.00748678, -0.00062423, -0.00083387, -0.00124058,
         -0.00818232,  0.00251631,  0.00250656,  0.00247063],
        [-0.00830104, -0.00125381, -0.00097433, -0.00075904, -0.00747979, -0.00066785, -0.00084015, -0.00119133,
         -0.00812927,  0.00250656,  0.00251393,  0.00247548],
        [-0.00805414, -0.00119490, -0.00106152, -0.00088032, -0.00730574, -0.00071586, -0.00080103, -0.00125144,
         -0.00789771,  0.00247063,  0.00247548,  0.00245932]])
        )

    def sample(self, device, shape):
        coef = self.linear_dist.sample(shape).to(device)
        return coef

    def forward(self, x):
        shape = (x.shape[0], 1, 1)
        params = self.sample(x.device, shape)
        return self.main(x, params)

    def main(self, x, coef):
        matrix = coef[..., :9].reshape(coef.shape[:-1] + (3, 3))
        intercept = coef[..., 9:]
        y = matrix.matmul(x.transpose(-1, 1).unsqueeze(-1)).squeeze(-1) + intercept
        y = y.transpose(-1, 1).clamp(min=0., max=1.)
        return y

def ColourDistortion(s=1.0):
    # s is the strength of color distortion.
    color_jitter = transforms.ColorJitter(0.8*s, 0.8*s, 0.8*s, 0.2*s)
    rnd_color_jitter = transforms.RandomApply([color_jitter], p=0.8)
    rnd_gray = transforms.RandomGrayscale(p=0.2)
    color_distort = transforms.Compose([rnd_color_jitter, rnd_gray])
    return color_distort

def BlurOrSharpen(radius=2.):
    blur = GaussianBlur(radius=radius)
    full_transform = transforms.RandomApply([blur], p=.5)
    return full_transform

class ImageFilterTransform(object):

    def __init__(self):
        raise NotImplementedError

    def __call__(self, img):
        return img.filter(self.filter)

class GaussianBlur(ImageFilterTransform):

    def __init__(self, radius=2.):
        self.filter = ImageFilter.GaussianBlur(radius=radius)

class Sharpen(ImageFilterTransform):

    def __init__(self):
        self.filter = ImageFilter.SHARPEN


from ampligraph.latent_features import ComplEx
from ampligraph.evaluation import select_best_model_ranking
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

# Leer dataset
df = pd.read_pickle("../Data/reducedDataset001.pkl")

df["train"] = df.Sujeto > "S14"

df["data_id"] = df.index.values.astype(str)
df["data_id"] = "Dato" + df.data_id
df["subject_id"] = df.Sujeto.values.astype(str)

# Generar representacion de un grafo
triples = []
for _, row in df[df["train"]].iterrows():
    # Data info
    print(row)

    ecg_triple = (round(row["ECG"], 2), "isECGDataIn", row["data_id"])
    emg_triple = (round(row["EMG"], 2), "isEMGDataIn", row["data_id"])
    eda_triple = (round(row["EDA"], 2), "isEDADataIn", row["data_id"])
    temp_triple = (round(row["TEMP"], 2), "isTEMPDataIn", row["data_id"])
    resp_triple = (round(row["RESP"], 2), "isRESPDataIn", row["data_id"])

    suj_triple = (row["data_id"], "isProducedBy", row["subject_id"])

    triples.extend((ecg_triple, emg_triple, eda_triple, temp_triple,
                    resp_triple, suj_triple))

X = pd.DataFrame(triples, columns=["subject", "predicate", "object"])

X_train, X_valid_aux = train_test_split(np.array(triples), test_size=0.33)
X_test, X_valid = train_test_split(X_valid_aux, test_size=0.4)
# Definir modelo e hiperparámetros a optimizar
model_class = ComplEx
param_grid = {
                     "batches_count": [50],
                     "seed": 0,
                     "epochs": [300],
                     "k": [100, 200],
                     "eta": [5,10,15,20],
                     "loss": ["pairwise", "nll", "multiclass_nll"],
                     "loss_params": {
                         "margin": [2]
                     },
                     "embedding_model_params": {

                     },
                     "regularizer": ["LP", None],
                     "regularizer_params": {
                         "p": [1, 3],
                         "lambda": [1e-4, 1e-5]
                     },
                     "optimizer": ["adagrad", "adam", "sgd"],
                     "optimizer_params":{
                         "lr": lambda: np.random.uniform(0.0001, 0.01)
                     },
                     "verbose": False
                 }
select_best_model_ranking(model_class, X_train, X_valid, X_test, param_grid,
                           max_combinations=100, use_filter=True, verbose=True,
                           early_stopping=True)


import re,os

# see if the cvs file is empty
f = open('out.csv')

file_not_empty = f.read(1)

if file_not_empty:
    f.close()
    out = open('out.csv','a')
else:
    f.close()
    out = open('out.csv','w')
    out.write("date,target_util,route_auto_util,route_auto_len,route_auto_viol,route_auto_time\n")

# write date
d = open('flow_date.txt')
for line in d:
  m = re.search(r"(.+)",line)
  if m:
     out.write(m.group())
     break

out.write(",")

# target utilization
tar_u = open('dcrm/rm_dc_scripts/dc.tcl','r')
for line in tar_u:
  m = re.search(r"set_utilization\s+([0-9\.]+)",line)
  if m:
     out.write(m.group(1))
     out.write(",")
     break

# final utilization
final_u = open('icc2rm/rpts_icc2/route_auto.report_utilization','r')
for line in final_u:
  m = re.search(r"Utilization Ratio:\s+([0-9\.]+)",line)
  if m:
     out.write(m.group(1))
     out.write(",")
     break

# wire length(micron)
wire_len = open('icc2rm/rpts_icc2/route_auto.check_routes','r')
for line in wire_len:
  m = re.search(r"Total Routed Wire Length =\s+([0-9\.]+)",line)
  if m:
     out.write(m.group(1))
     out.write(",")
     break

# Num of violations
viol = open('icc2rm/rpts_icc2/route_auto.check_routes','r')
for line in viol:
  m = re.search(r"	@@@@@@@ TOTAL VIOLATIONS =\s+([0-9\.]+)",line)
  if m:
     out.write(m.group(1))
     out.write(",")
     break

# time for auto route
t1 = os.path.getmtime('icc2rm/clock_opt_opto')
t2 = os.path.getmtime('icc2rm/route_auto')
time = str(t2-t1)
out.write(time)
out.write("\n")
out.close()


# -*- coding: UTF-8 -*-
# @Time    : 18-03-19
# @File    : ali_fashionai_landmark.py
# @Author  : jian<jian@mltalker.com>
from __future__ import division
from __future__ import unicode_literals
from __future__ import print_function
import tensorflow as tf
from antgo.dataflow.common import *
from antgo.context import *
import numpy as np
from antgo.codebook.tf.layers import *
import tensorflow.contrib.slim as slim
from antgo.trainer.tftrainer import *
from antgo.dataflow.imgaug.regular import *
from antgo.codebook.tf.dataset import *
from antgo.codebook.tf.preprocess import *
from antgo.utils._resize import resize
import densenet
from tensorflow.contrib import layers as layers_lib
from tensorflow.python.ops import init_ops
from tensorflow.contrib.layers.python.layers import initializers
##################################################
######## 1.step global interaction handle ########
##################################################
ctx = Context()

##################################################
######## 2.step model building (tensorflow) ######
##################################################
def gcn_module(inputs,
               num_class,
               kernel_size,
               scope=None):
  with slim.variable_scope.variable_scope(scope, 'gcn_module', [inputs]):
    with slim.arg_scope([layers_lib.conv2d],
        padding='SAME',
        activation_fn=None,
        normalizer_fn=None,
        normalizer_params=None,
        weights_initializer=initializers.xavier_initializer(),
        weights_regularizer=tf.contrib.layers.l2_regularizer(0.0001),
        biases_initializer=init_ops.zeros_initializer(),
        biases_regularizer=tf.contrib.layers.l2_regularizer(0.0002)):
      left_conv1 = layers_lib.conv2d(inputs, num_class, [kernel_size, 1])
      left_conv2 = layers_lib.conv2d(left_conv1, num_class, [1, kernel_size])

      right_conv1 = layers_lib.conv2d(inputs, num_class, [1, kernel_size])
      right_conv2 = layers_lib.conv2d(right_conv1, num_class, [kernel_size, 1])

      result_sum = tf.add(left_conv2, right_conv2, name='gcn_module')
      return result_sum

def gcn_br(inputs, scope):
  with slim.variable_scope.variable_scope(scope, 'gcn_br', [inputs]):
    with slim.arg_scope([layers_lib.conv2d],
                        padding='SAME',
                        activation_fn=tf.nn.relu,
                        normalizer_fn=None,
                        normalizer_params=None,
                        weights_initializer=initializers.xavier_initializer(),
                        weights_regularizer=tf.contrib.layers.l2_regularizer(0.0001),
                        biases_initializer=init_ops.zeros_initializer(),
                        biases_regularizer=tf.contrib.layers.l2_regularizer(0.0002)):
      num_class = inputs.get_shape()[3]
      conv = layers_lib.conv2d(inputs, num_class, [3, 3])
      conv = layers_lib.conv2d(conv, num_class, [3, 3], activation_fn=None)
      result_sum = tf.add(inputs, conv, name='fcn_br')
      return result_sum

class HourglassModel(ModelDesc):
  def __init__(self):
    super(HourglassModel, self).__init__()
    self._nstack = ctx.params.nstacks
    self._nfeat = ctx.params.nfeat
    self._nlow = ctx.params.nlow
    self._output_dim = ctx.params.output_dim # 16
    self._dropout_rate = ctx.params.dropout_rate

  def arg_scope(self, is_training):
    # branch side
    batch_norm_params = {
      'decay': 0.997,
      'epsilon': 1e-5,
      'scale': True,
      'updates_collections': tf.GraphKeys.UPDATE_OPS,
      'is_training': is_training
    }

    with slim.arg_scope([slim.conv2d],
                        weights_regularizer=slim.l2_regularizer(0.0001),
                        activation_fn=None,
                        biases_initializer=None,
                        weights_initializer=slim.variance_scaling_initializer(),
                        ):
        with slim.arg_scope([slim.batch_norm], **batch_norm_params) as scope:
            return scope

  def _residual(self, inputs, out_channels, name='residual_block'):
    with tf.variable_scope(name):
      # three convs
      norm_1 = slim.batch_norm(inputs, activation_fn=tf.nn.relu)
      conv_1 = slim.conv2d(norm_1, out_channels/2, [1, 1], stride=1, normalizer_fn=None, activation_fn=None)

      norm_2 = slim.batch_norm(conv_1, activation_fn=tf.nn.relu)
      conv_2 =slim.conv2d(norm_2, out_channels/2, [3, 3], stride=1, normalizer_fn=None, activation_fn=None)

      norm_3 = slim.batch_norm(conv_2, activation_fn=tf.nn.relu)
      conv_3 = slim.conv2d(norm_3, out_channels, [1, 1], stride=1, normalizer_fn=None, activation_fn=None)

      # skip
      res_layer = None
      if inputs.get_shape().as_list()[3] == out_channels:
        res_layer = inputs
      else:
        res_layer = slim.conv2d(inputs, out_channels, [1, 1], stride=1, normalizer_fn=None, activation_fn=None)

      return tf.add(res_layer, conv_3)

  def _hourglass(self, inputs, n, num_out, name='hourglass'):
    with tf.variable_scope(name):
      # upper branch
      up_1 = self._residual(inputs, num_out, name='up_1')
      # lower branch
      low_ = slim.max_pool2d(inputs, [2, 2], stride=2)
      low_1 = self._residual(low_, num_out, name='low_1')

      if n > 0:
        low_2 = self._hourglass(low_1, n-1, num_out, name='low_2')
      else:
        low_2 = self._residual(low_1, num_out, name='low_2')

      low_3 = self._residual(low_2, num_out, name='low_3')
      up_2 = tf.image.resize_nearest_neighbor(low_3, tf.shape(low_3)[1:3]*2, name='upsampling')
      return tf.nn.relu(tf.add(up_2, up_1), name='out_hg')

  def model_input(self, is_training, data_source):
    if not is_training:
      return None

    warp_data = Node('warp', _data_warp, Node.inputs(data_source))
    reorganized_data = Node('reorganize', _data_reorganize, Node.inputs(warp_data))

    qq = DatasetQueue(reorganized_data,
                      [tf.float32, tf.float32, tf.int32, tf.int32],
                      [(ctx.params.input_size, ctx.params.input_size, 4),
                       (ctx.params.nstacks, ctx.params.hm_size, ctx.params.hm_size, len(ctx.params.joints)),
                       (24,),
                       (ctx.params.input_size, ctx.params.input_size, 1)])
    image, gtmap, pick_index, attention_region = qq.dequeue()

    random_angle = (tf.to_float(tf.random_uniform([1]))[0] * 2 - 1) * 10.0 / 180.0 * 3.14
    # for image
    image = tf.expand_dims(image, 0)
    rotated_image = tf.contrib.image.rotate(image, random_angle)
    rotated_image = tf.squeeze(rotated_image, 0)

    # for gtmap
    rotated_gtmap = tf.contrib.image.rotate(gtmap, random_angle)

    # for attention
    rotated_attention_region = tf.contrib.image.rotate(attention_region, random_angle)

    # make batch
    rotated_images, rotated_gtmaps, pick_indexs, attention_regions = tf.train.batch([rotated_image, rotated_gtmap, pick_index, rotated_attention_region], ctx.params.batch_size)
    data_queue = slim.prefetch_queue.prefetch_queue([rotated_images, rotated_gtmaps, pick_indexs, attention_regions])
    return data_queue

  def model_fn(self, is_training=True, *args, **kwargs):
    #inputs: batch_size x 256 x 256 x 3
    #labels: batch_size x nstack x 64 x 64 x output_dim(landmark number)
    inputs = None
    labels = None

    if is_training:
      inputs, labels, pick_indexs, attention_regions = args[0].dequeue()
    else:
      inputs = tf.placeholder(tf.float32, shape=(ctx.params.input_size,ctx.params.input_size, 4), name='input_x')
      inputs = tf.expand_dims(inputs, 0)

    with tf.variable_scope('attention'):
      # stage - 1 dense block
      input_r, input_g, input_b,input_a = tf.split(inputs, 4, axis=3)
      input_rgb = tf.concat((input_r,input_g,input_b), -1)
      input_rgb_128 = tf.image.resize_bilinear(input_rgb, (128, 128))
      with slim.arg_scope(densenet.densenet_arg_scope(weight_decay=2e-5)):
        _, each_layer_output = densenet.densenet(input_rgb_128,
                                                 num_classes=2,
                                                 reduction=0.5,
                                                 growth_rate=32,
                                                 num_filters=64,
                                                 num_layers=[6],
                                                 is_training=is_training,
                                                 reuse=None,
                                                 scope='densenet121')

      # stage - 2 gcn block
      with tf.variable_scope(None, 'GCN', [each_layer_output], reuse=None):
        # 64 x 64
        res_1 = each_layer_output['densenet121/root_block']
        # 32 x 32
        res_2 = each_layer_output['attention/densenet121/dense_block1']

        res_1_gcn = gcn_module(res_1, 2, 15, 'gcn_1')
        res_2_gcn = gcn_module(res_2, 2, 15, 'gcn_2')

        res_2_gcn = gcn_br(res_2_gcn, 'br_2')
        _, height, width, _ = res_2_gcn.shape
        res_2_upsample = tf.image.resize_bilinear(res_2_gcn, (int(height)*2, int(width)*2))
        res_1_gcn = gcn_br(res_2_upsample + res_1_gcn, 'br_1')
        attention_logits = res_1_gcn
        attention_prob = tf.nn.softmax(attention_logits)

    with slim.arg_scope(self.arg_scope(is_training)):
      with tf.variable_scope('model'):
        with tf.variable_scope('preprocessing'):
          # inputs batch_size x 256 x 256 x 4
          pad_1 = tf.pad(inputs, np.array([[0, 0], [2, 2], [2, 2], [0, 0]]))
          conv_1 = slim.conv2d(pad_1, 64, [6, 6], stride=2, scope='256to128', padding='VALID')
          res_1 = self._residual(conv_1, 128, name='r1')
          pool_1 = tf.contrib.layers.max_pool2d(res_1, [2, 2], [2, 2], padding='VALID')
          res_2 = self._residual(pool_1, 128, name='r2')
          res_3 = self._residual(res_2, ctx.params.nfeat, name='r3')

        hg = [None] * self._nstack
        ll = [None] * self._nstack
        ll_ = [None] * self._nstack
        drop = [None] * self._nstack
        out = [None] * self._nstack
        out_ = [None] * self._nstack
        sum_ = [None] * self._nstack
        with tf.variable_scope('stacks'):
          with tf.variable_scope('stage_0'):
            hg[0] = self._hourglass(tf.concat((res_3, attention_prob), -1), self._nlow, self._nfeat, 'hourglass')
            drop[0] = slim.dropout(hg[0], keep_prob=self._dropout_rate, is_training=is_training)
            ll[0] = slim.conv2d(drop[0], self._nfeat, [1, 1], stride=1, scope='cc')
            ll_[0] = slim.conv2d(ll[0], self._nfeat, [1, 1], stride=1, normalizer_fn=None, activation_fn=None, scope='ll')

            out[0] = slim.conv2d(ll[0], self._output_dim, [1, 1], stride=1, normalizer_fn=None, activation_fn=None, scope='out')
            out_[0] = slim.conv2d(out[0], self._nfeat, [1, 1], stride=1, normalizer_fn=None, activation_fn=None, scope='out_')
            sum_[0] = tf.add_n([out_[0], res_3, ll_[0]], name='merge')

          for i in range(1, self._nstack - 1):
            with tf.variable_scope('stage_'+str(i)):
              hg[i] = self._hourglass(tf.concat((sum_[i-1], attention_prob), -1), self._nlow, self._nfeat, 'hourglass')
              drop[i] = slim.dropout(hg[i], keep_prob=self._dropout_rate, is_training=is_training)
              ll[i] = slim.conv2d(drop[i], self._nfeat, [1, 1], stride=1, scope='cc')
              ll_[i] = slim.conv2d(ll[i], self._nfeat, [1, 1], stride=1, normalizer_fn=None, activation_fn=None, scope='ll')

              out[i] = slim.conv2d(ll[i], self._output_dim, [1, 1], stride=1, normalizer_fn=None, activation_fn=None, scope='out')
              out_[i] = slim.conv2d(out[i], self._nfeat, [1, 1], stride=1, normalizer_fn=None, activation_fn=None, scope='out_')
              sum_[i] = tf.add_n([out_[i], sum_[i-1], ll_[i]], name='merge')

          with tf.variable_scope('stage_'+str(self._nstack - 1)):
            hg[self._nstack - 1] = self._hourglass(tf.concat((sum_[self._nstack - 2], attention_prob), -1), self._nlow, self._nfeat, 'hourglass')
            drop[self._nstack - 1] = slim.dropout(hg[self._nstack - 1], keep_prob=self._dropout_rate, is_training=is_training)
            ll[self._nstack - 1] = slim.conv2d(drop[self._nstack - 1], self._nfeat, [1, 1], stride=1, scope='cc')

            out[self._nstack - 1] = slim.conv2d(ll[self._nstack - 1], self._output_dim, [1, 1], stride=1, normalizer_fn=None, activation_fn=None, scope='out')

          output = tf.stack(out, axis=1, name='final_output')

          if is_training:
            # loss 1: attention region loss
            attention_regions_one_hot = slim.one_hot_encoding(tf.squeeze(attention_regions, -1), 2)
            attention_loss = tf.nn.softmax_cross_entropy_with_logits(logits=tf.image.resize_bilinear(attention_logits,
                                                                                (ctx.params.input_size,
                                                                                ctx.params.input_size)),
                                                                      labels=attention_regions_one_hot)
            attention_loss = tf.reduce_mean(attention_loss)
            tf.losses.add_loss(0.5 * attention_loss)

            # loss 2: joint loss
            # shape batch_size x nstack x 64 x 64 x 24
            loss_mat = tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=labels)

            # focal loss (computing weight of every sample)
            # shape: batch_size x nstacks x 64 x 64 x 24
            sigmoid_logits = tf.nn.sigmoid(output)
            # hard or easy
            hard_weight = tf.pow(1 - (sigmoid_logits * labels + (1-sigmoid_logits) * (1-labels)), 2)
            sample_weight = hard_weight * 50.0

            # weight loss
            loss_mat = loss_mat * sample_weight

            # attention on category joints (local attention)
            loss_mat_reshpae = tf.transpose(loss_mat, perm=(0, 4, 1, 2, 3))
            loss_mat_reshpae = tf.reshape(loss_mat_reshpae, (-1, ctx.params.nstacks, ctx.params.hm_size, ctx.params.hm_size))

            useful_index = tf.reshape(pick_indexs, (-1,))
            useful_index = tf.where(tf.equal(useful_index, 1))
            useful_index = tf.squeeze(useful_index, 1)
            loss_mat_reshpae = tf.gather(loss_mat_reshpae, useful_index)

            #
            loss = tf.reduce_mean(loss_mat_reshpae)
            tf.losses.add_loss(loss)
            return loss, attention_loss
          else:
            output = tf.nn.sigmoid(output)
            return output
##################################################
######## 3.step define training process  #########
##################################################
def _makeGaussian(height, width, sigma=3, center=None):
  """ Make a square gaussian kernel.
  size is the length of a side of the square
  sigma is full-width-half-maximum, which
  can be thought of as an effective radius.
  """
  x = np.arange(0, width, 1, float)
  y = np.arange(0, height, 1, float)[:, np.newaxis]
  if center is None:
    x0 = width // 2
    y0 = height // 2
  else:
    x0 = center[0]
    y0 = center[1]
  return np.exp(-4 * np.log(2) * ((x - x0) ** 2 + (y - y0) ** 2) / sigma ** 2)

def _generate_hm(height, width, joints, maxlength, weight):
  num_joints = joints.shape[0]
  hm = np.zeros((height, width, num_joints), dtype=np.float32)
  for i in range(num_joints):
    if joints[i, 0] > 0 and joints[i, 1] > 0 and weight[i] == 1:
      s = int(np.sqrt(maxlength) * maxlength * 10 / 4096) + 2
      hm[:, :, i] = _makeGaussian(height, width, sigma=s, center=(joints[i, 0], joints[i, 1]))
    else:
      hm[:, :, i] = np.zeros((height, width))
  return hm

def _data_warp(*args, **kwargs):
  data, label = args[0]
  image, _ = data  # ignore category
  return image, label

def _data_reorganize(*args, **kwargs):
  train_img = args[0][0]  # 64 x 64 x 4
  train_img_annotation = args[0][1]  # {}

  category_dict = {'blouse': 0,
                   'dress': 1,
                   'outwear': 2,
                   'skirt': 3,
                   'trousers': 4}

  train_img = resize(train_img, [ctx.params.input_size, ctx.params.input_size])
  train_img = train_img / 255.0
  prior = category_dict[train_img_annotation['category']] * np.ones((ctx.params.input_size, ctx.params.input_size, 1))
  prior = prior / float(len(category_dict))
  train_img = np.concatenate((train_img, prior), axis=-1)

  landmarks = train_img_annotation['landmark']
  joints = np.zeros((len(ctx.params.joints), 2))
  ##################################################
  visibles = -1 * np.ones((len(ctx.params.joints)))
  index_pick = np.zeros((24), dtype=np.int32)

  x_list = []
  y_list = []
  info = train_img_annotation['info']
  old_height, old_width = info[0:2]

  for joint_index, joint_global_index, x, y, visible in landmarks:
    joints[joint_global_index, 0] = x
    joints[joint_global_index, 1] = y

    visibles[joint_global_index] = visible
    if visible == 1 or visible == 0:
      index_pick[joint_global_index] = 1
      x_list.append(x/float(old_width) * ctx.params.input_size)
      y_list.append(y/float(old_height) * ctx.params.input_size)

  x_min = int(np.min(x_list))
  x_max = int(np.max(x_list))
  y_min = int(np.min(y_list))
  y_max = int(np.max(y_list))

  height, width = train_img.shape[0:2]
  attention_region = np.zeros((height, width, 1), dtype=np.int32)
  attention_region[y_min:y_max, x_min:x_max, 0] = 1

  joints = np.array(joints).reshape((-1, 2))
  joints[:, 0] = joints[:, 0] / float(old_width) * ctx.params.hm_size
  joints[:, 1] = joints[:, 1] / float(old_height) * ctx.params.hm_size

  train_weight = np.zeros((len(ctx.params.joints)), np.float32)
  train_weight[np.where(visibles == 1)] = 1
  train_weight[np.where(visibles == 0)] = 1

  hm = _generate_hm(ctx.params.hm_size, ctx.params.hm_size, joints, ctx.params.hm_size, train_weight)
  hm = np.expand_dims(hm, axis=0)
  hm = np.repeat(hm, ctx.params.nstacks, axis=0)

  return train_img, hm, index_pick, attention_region

def training_callback(data_source, dump_dir):
  ##########  2.step build model     ##############
  tf_trainer = TFTrainer(ctx.params, dump_dir)
  tf_trainer.deploy(HourglassModel())

  iter = 0
  for epoch in range(ctx.params.max_epochs):
    rounds = int(float(data_source.size) / float(ctx.params.batch_size * ctx.params.num_clones))
    for _ in range(rounds):
      _, loss_val, attention_loss = tf_trainer.run()
      if iter % 100 == 0:
        print('loss %f and attention loss %f'%(loss_val))
      iter = iter + 1

    # save
    tf_trainer.snapshot(epoch)

###################################################
######## 4.step define infer process     ##########
###################################################
import cv2
def _data_filter(*args, **kwargs):
  image, category, _ = args[0]

  category_dict = {'blouse': 0,
                   'dress': 1,
                   'outwear': 2,
                   'skirt': 3,
                   'trousers': 4}

  test_image = resize(image, [ctx.params.input_size, ctx.params.input_size])
  test_image = test_image / 255.0
  prior = category_dict[category] * np.ones((ctx.params.input_size, ctx.params.input_size, 1))
  prior = prior / float(len(category_dict))
  test_image = np.concatenate((test_image, prior), axis=-1)

  return test_image

def infer_callback(data_source, dump_dir):
  ##########  1.step data preprocess ##############
  warp_data = Node('warp', _data_filter, Node.inputs(data_source))

  ##########  2.step building model  ##############
  tf_trainer = TFTrainer(ctx.params, dump_dir, is_training=False)
  tf_trainer.deploy(HourglassModel())

  key_points = ['neckline_left',  # 左领部
                'neckline_right',  # 右领部
                'center_front',  # 中线
                'shoulder_left',  # 左肩部
                'shoulder_right',  # 右肩部
                'armpit_left',  # 左腋窝
                'armpit_right',  # 右腋窝
                'waistline_left',  # 左腰部
                'waistline_right',  # 右腰部
                'cuff_left_in',  # 左袖口内
                'cuff_left_out',  # 左袖口外
                'cuff_right_in',  # 右袖口内
                'cuff_right_out',  # 右袖口外
                'top_hem_left',  # 左衣摆
                'top_hem_right',  # 右衣摆
                'waistband_left',  # 左腰部
                'waistband_right',  # 右腰部
                'hemline_left',  # 左裙摆
                'hemline_right',  # 右裙摆
                'crotch',  # 裆部
                'bottom_left_in',  # 左裤脚内
                'bottom_left_out',  # 左裤脚外
                'bottom_right_in',  # 右裤脚内
                'bottom_right_out',  # 右裤脚外
                ]

  category_map = {'blouse': [0, 1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 14, ],
                  'dress': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 17, 18],
                  'outwear': [0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
                  'skirt': [15, 16, 17, 18, ],
                  'trousers': [15, 16, 19, 20, 21, 22, 23]}

  # submit file
  fp = open('result.csv', 'w')
  submit_table_head = ','.join(key_points)
  submit_table_head = 'image_id,image_category,' + submit_table_head + '\n'
  fp.write(submit_table_head)

  ##########  3.step predict sample  ##############
  generator = warp_data.iterator_value()
  while True:
    try:
      # predict landmark
      output, = tf_trainer.run(generator, binds={'input_x': 0})
      output = output[0][7]

      # original data
      predict_img, category, image_file = data_source.get_value()
      height, width = predict_img.shape[0:2]

      predict_joints = -1 * np.ones((len(ctx.params.joints), 3), dtype=np.uint32)

      submit_table_item = '%s,%s' % (image_file, category)
      for joint_i in range(len(ctx.params.joints)):
        if joint_i not in category_map[category]:
          submit_table_item = submit_table_item + ',-1_-1_-1'
          continue

        index = np.unravel_index(output[:, :, joint_i].argmax(), (ctx.params.hm_size, ctx.params.hm_size))
        if output[index[0], index[1], joint_i] > ctx.params.thresh:
          # resize to original scale
          y = index[0]
          y = y / float(ctx.params.hm_size) * height
          x = index[1]
          x = x / float(ctx.params.hm_size) * width
          predict_joints[joint_i, 0] = x
          predict_joints[joint_i, 1] = y
          predict_joints[joint_i, 2] = 1

          submit_table_item = submit_table_item + ',%d_%d_1' % (int(x), int(y))

          predict_img[int(y), int(x)] = 255
          # cv2.circle(predict_img, center=tuple((int(x), int(y))), radius=5, color=(255,0,0), thickness=-1)
          # cv2.putText(predict_img,ctx.params.joints[joint_i], (int(x+5),int(y+5)),cv2.FONT_HERSHEY_SIMPLEX,0.8,(0,0,255),2)
          #
        else:
          submit_table_item = submit_table_item + ',-1_-1_-1'

      submit_table_item = submit_table_item + '\n'

      # 4.step record predicted landmark
      ctx.recorder.record((predict_joints, category, image_file))

      # 5.step record to standard submit format
      fp.write(submit_table_item)

      # # # show
      # cv2.imshow('ss1', predict_img.astype(np.uint8))
      # cv2.waitKey()
    except:
      break

  fp.close()

###################################################
####### 5.step link training and infer ############
#######        process to context      ############
###################################################
ctx.training_process = training_callback
ctx.infer_process = infer_callback


import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit

class GlasgowThemePlugin(plugins.SingletonPlugin):
    '''Glasgow's Theme for data.glasgow.gov.uk

    '''
    # Declare that this class implements IConfigurer.
    plugins.implements(plugins.IConfigurer)

    def update_config(self, config):

        # Add this plugin's templates dir to CKAN's extra_template_paths, so
        # that CKAN will use this plugin's custom templates.
        # 'templates' is the path to the templates dir, relative to this
        # plugin.py file.
        toolkit.add_template_directory(config, 'theme/templates')

        # Add this plugin's public dir to CKAN's extra_public_paths, so
        # that CKAN will use this plugin's custom static files.
        toolkit.add_public_directory(config, 'theme/public')

        toolkit.add_resource('theme/fanstatic', 'glasgow')


'''
Problem Description

Hamming distance between two non-negative integers is defined as the number of positions at which the corresponding bits are different.

Given an array A of N non-negative integers, find the sum of hamming distances of all pairs of integers in the array. Return the answer modulo 1000000007.

Problem Constraints
1 <= |A| <= 200000

1 <= A[i] <= 109

Input Format
First and only argument is array A.

Output Format
Return one integer, the answer to the problem.

Example Input
Input 1:

 A = [1]
Input 2:

 A = [2, 4, 6]

Example Output
Output 1:

 0
Output 2:

 8

Example Explanation
Explanation 1:

 No pairs are formed.
Explanation 2:

 We return, f(2, 2) + f(2, 4) + f(2, 6) + f(4, 2) + f(4, 4) + f(4, 6) + f(6, 2) + f(6, 4) + f(6, 6) = 8
'''
class Solution:
    # @param A : tuple of integers
    # @return an integer
    def hammingDistance(self, A):
        ans=0
        for i in range(0,32):
            x=0
            for j in range(len(A)):
                if int(A[j] & 1<<i):
                    x= x+ 1
            y=len(A)-x
            ans= ans + x*y*2
        return ans%1000000007

Python3/Math/Adhoc/Sum of pairwise Hamming Distance.py


from cytoolz import concat
from functools import partial
from numpy import array
from numpy import int16
from numpy import uint16
from pyspark.sql import Row

import json
import os
import sys
import vcr as _vcr

def merge(inputs):
  return list(concat(inputs))

TEST_ROOT = os.path.dirname(os.path.abspath(__file__))
cassette = TEST_ROOT + '/resources/ccdc-v1-cassette.yaml'
vcr = _vcr.VCR(record_mode='new_episodes')

acquired   = '1980-01-01/2017-01-01'
schemabase = ['cx', 'cy', 'px', 'py', 'dates']
auxbase    = ['aspect','dem', 'mpw', 'posidex', 'slope']
attrbase   = ['blmag',  'grmag',  'remag',  'nimag',  's1mag',  's2mag',  'thmag',  'blrmse',
              'grrmse', 'rermse', 'nirmse', 's1rmse', 's2rmse', 'thrmse', 'blcoef', 'grcoef',
              'recoef', 'nicoef', 's1coef', 's2coef', 'thcoef', 'blint',  'grint',  'reint',
              'niint',  's1int',  's2int',  'thint']

ard_schema       = merge([schemabase, ['blues', 'greens', 'reds', 'nirs', 'swir1s', 'swir2s', 'thermals', 'qas']])
aux_schema       = merge([schemabase, auxbase, ['trends']])
features_columns = merge([attrbase,   auxbase, ['grint']])
features_dframe  = merge([attrbase, ard_schema, ['sday', 'eday']])
ccd_schema_base  = merge([schemabase, attrbase, ['sday', 'eday', 'bday', 'chprob', 'curqa', 'mask']])
ccd_format_keys  = merge([ccd_schema_base, ['grint']])
ccd_schema_names = merge([ccd_schema_base, ['rfrawp']])
merged_schema    = merge([ard_schema, aux_schema])

timeseries_element = ((-1815585, 1064805, -1814475, 1062105),
                      {'blues':    array([-9999, -9999, -9999, -9999], dtype=int16),
                       'qas':      array([1, 1, 1, 1], dtype=uint16),
                       'nirs':     array([-9999, -9999, -9999, -9999], dtype=int16),
                       'thermals': array([-9999, -9999, -9999, -9999], dtype=int16),
                       'swir2s':   array([-9999, -9999, -9999, -9999], dtype=int16),
                       'reds':     array([-9999, -9999, -9999, -9999], dtype=int16),
                       'swir1s':   array([-9999, -9999, -9999, -9999], dtype=int16),
                       'greens':   array([-9999, -9999, -9999, -9999], dtype=int16),
                       'dates':    [734973, 731205, 724404, 723868]})

grid_resp = json.loads(open(TEST_ROOT + "/data/grid_response.json").read())
near_resp = json.loads(open(TEST_ROOT + "/data/near_response.json").read())
snap_resp = json.loads(open(TEST_ROOT + "/data/snap_response.json").read())
tile_resp = json.loads(open(TEST_ROOT + "/data/tile_response.json").read())
regy_resp = json.loads(open(TEST_ROOT + "/data/registry_response.json").read())
chip_resp = json.loads(open(TEST_ROOT + "/data/chip_response.json").read())

merlin_grid_partial = partial(lambda: grid_resp)
merlin_near_partial = partial(lambda x, y: near_resp)
merlin_snap_partial = partial(lambda x, y: snap_resp)
merlin_regy_partial = partial(lambda: regy_resp)
merlin_chip_partial = partial(lambda *args, **kwargs: chip_resp)
merlin_frmt_partial = partial(lambda *args, **kwargs: [((11, 22, 33, 44), (-1815585.0, 1064805.0))])

def dataframe(ctx, attrs, type='int'):
    vals = list(range(0, len(attrs)))

    if type is 'iter':
        vals = [[i] for i in vals]

    Foo  = Row(*attrs)
    foo1 = Foo(*vals)
    return ctx.createDataFrame(data=[foo1])

def cassandra_read(ctx, table):
    return faux_dataframe(ctx, ['cx', 'cy', 'srb1'])

def merlin_create(x, y, acquired, cfg):
    return [[[11, 22, 33, 44], (x, y)]]

def timeseries_rdd(ctx, cids, acquired, cfg, name):
    rdd = ctx.parallelize([[[11, 22, 33, 44], (-999, 111)]])
    rdd.setName(name)
    return rdd


# Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
#
# File: CoolLumiUtilities/python/FillParamsCondAlgConfig.py
# Created: May 2019, sss
# Purpose: Configure FillParamsCondAlg.
#

from __future__ import print_function
from AthenaConfiguration.ComponentFactory import CompFactory

from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
from IOVDbSvc.IOVDbSvcConfig import addFolders

def FillParamsCondAlgCfg (configFlags):
    name = 'FillParamsCondAlg'
    result = ComponentAccumulator()

    # Should only be used for Run 1.
    if configFlags.IOVDb.DatabaseInstance != 'COMP200':
        return result

    folder = '/TDAQ/OLC/LHC/FILLPARAMS'
    # Mistakenly created as multi-version folder, must specify HEAD
    result.merge (addFolders (configFlags, folder, 'TDAQ', tag='HEAD',
                              className='AthenaAttributeList'))

    FillParamsCondAlg=CompFactory.FillParamsCondAlg
    alg = FillParamsCondAlg (name,
                             FillParamsFolderInputKey = folder,
                             FillParamsOutputKey = 'FillParamsCondData')

    result.addCondAlgo (alg)
    return result

if __name__ == "__main__":
    from AthenaCommon.Configurable import Configurable
    Configurable.configurableRun3Behavior=1
    from AthenaConfiguration.AllConfigFlags import ConfigFlags
    from AthenaConfiguration.TestDefaults import defaultTestFiles

    print ('--- data')
    flags1 = ConfigFlags.clone()
    flags1.Input.Files = defaultTestFiles.RAW
    flags1.Input.ProjectName = 'data12_8TeV'
    flags1.lock()
    acc1 = FillParamsCondAlgCfg (flags1)
    acc1.printCondAlgs(summariseProps=True)
    print ('IOVDbSvc:', acc1.getService('IOVDbSvc').Folders)
    acc1.wasMerged()

    print ('--- default')
    flags2 = ConfigFlags.clone()
    flags2.Input.Files = defaultTestFiles.RAW
    flags2.lock()
    acc2 = FillParamsCondAlgCfg (flags2)
    acc2.printCondAlgs(summariseProps=True)
    print ('IOVDbSvc:', acc2.getServices())
    acc2.wasMerged()


# -*- coding: utf-8 -*-
from odoo import models, fields, api
from odoo.exceptions import ValidationError

class CrmRegister(models.Model):
    _name = 'crm.register'

    create_date = fields.Datetime(string='創建日期', readonly=True)
    name = fields.Char(string='商機')
    # partner_id = fields.Many2one(comodel_name='res.partner', string='客戶')
    partner_id = fields.Char(string='客戶')
    email_from = fields.Char(string='電子郵箱', index=True)
    phone = fields.Char(string='聯絡電話')
    line_id = fields.Char(string='LINE ID')
    company_currency = fields.Many2one(string='Currency', related='company_id.currency_id', readonly=True, relation="res.currency")
    company_id = fields.Many2one('res.company', string='Company', index=True, default=lambda self: self.env.user.company_id.id)
    country_id = fields.Many2one(comodel_name='res.country', string='國家')
    stage_id = fields.Many2one(comodel_name='crm.stage', string='階段')
    # team_id = fields.Many2one('crm.team', string='銷售團隊', oldname='section_id', default=lambda self: self.env['crm.team'].sudo()._get_default_team_id(user_id=self.env.uid), index=True, track_visibility='onchange')
    # user_id = fields.Many2one('res.users', string='銷售員', index=True, track_visibility='onchange', default=lambda self: self.env.user)
    select_location = fields.Char(string='地區')
    select_kind = fields.Char(string='分類')
    team_id = fields.Char(string='銷售團隊')
    user_id = fields.Char(string='銷售員')
    priority = fields.Selection(selection=[(0, '一般'), (1, '低'), (2, '高'), (3, '非常高')], string='優先級')
    # tag_ids = fields.Many2many('crm.lead.tag', 'crm_lead_tag_rel', 'lead_id', 'tag_id', string='摘要')
    tag_ids = fields.Char(string='摘要')
    planned_revenue = fields.Float(string='預期收入', track_visibility='always')

    description = fields.Text('Notes')
    partner_name = fields.Char(string='客戶名稱', index=True)
    street = fields.Char(string='街道')
    street2 = fields.Char(string='街道2')
    city = fields.Char(string='城市')
    state_id = fields.Many2one("res.country.state", string='州/省')
    zip = fields.Char(string='郵編', change_default=True)
    website = fields.Char(string='網站', index=True)
    contact_name = fields.Char(string='聯繫人姓名')
    title = fields.Many2one('res.partner.title')
    function = fields.Char(string='工作崗位')
    mobile = fields.Char('手機')
    opt_out = fields.Boolean(string='退訂', oldname='optout')
    probability = fields.Float(string='概率')
    # hope_service = fields.Selection(selection=[(0, '長期留學【語言學校/專門學校/大學(院)】'), (1, '短期遊學【語言學校/遊學團】'), (2, '打工度假(無簽證)【簽證申請事宜】'), (3, '打工度假(有簽證)【詢問學校/工作/住宿】'), (4, '日商正職工作【台灣/日本】'), (5, '住宿/其他/未定')], string='希望服務項目')
    # hope_location = fields.Selection(selection=[(0, '台北店'), (1, '台中店'), (2, '高雄店')], string='希望諮詢地點')
    # hope_area = fields.Selection(selection=[(0, '關東(東京．橫濱)'), (1, '關西(大阪．京都．神戶)'), (2, '中部(名古屋)'), (3, '北海道．東北'), (4, '九州'), (5, '其他'), (6, '不拘')], string='想去的地區')
    hope_service = fields.Char(string='希望服務項目')
    hope_location = fields.Char(string='希望諮詢地點')
    hope_departure = fields.Date(string='預定出發年月')
    hope_area = fields.Char(string='想去的地區')
    hope_jobcondition = fields.Text(string='其他希望工作條件')
    other_issue = fields.Text(string='其他欲詢問的問題')
    country = fields.Char(string='現居地')
    visa = fields.Char(string='所持簽證')
    jp_level = fields.Char(string='日文程度')
    hope_job = fields.Char(string='期望職缺')
    event_plan_id = fields.Char(string='關聯的活動')
    crm_source = fields.Char(string='商機來源')
    date_deadline = fields.Date(string='預期結束')

    gender = fields.Char(string='性別')
    highest_education = fields.Char(string='最高學歷')
    status = fields.Char(string='學籍')
    school_name = fields.Char(string='校名')
    department = fields.Char(string='科系')
    birth_date = fields.Char(string='出生年')
    new1 = fields.Char(string='預設1')
    new2 = fields.Char(string='預設2')
    new3 = fields.Char(string='預設3')
    new4 = fields.Char(string='預設4')
    new5 = fields.Char(string='預設5')

    crm_kind = fields.Char(string='crm_kind')
    serial_number = fields.Char(string='編號')
    is_transfer = fields.Boolean(string='是否已轉入CRM', default=False)

    def transfer_information(self):
        crm_lead_id = self.env['crm.lead']
        team_id = self.env['crm.team']
        user_id = self.env['res.users']
        serial_number = ''
        if self.is_transfer:
            raise ValidationError(u'已轉入CRM')
        if self.user_id:
            user_id = self.env['res.users'].search([('name','=',self.user_id)], limit=1)
        if self.team_id:
            team_id = self.env['crm.team'].search([('serial_number', '=', self.team_id)], limit=1)
        if self.select_kind and self.select_location and not self.team_id:
            if self.select_location == u'台北店':
                if self.select_kind == u'留遊學':
                    serial_number = '017'
                elif self.select_kind == u'打工度假(無簽證)':
                    serial_number = '008'
                elif self.select_kind == u'打工度假(有簽證)':
                    serial_number = '036'
                elif self.select_kind == u'住宿':
                    serial_number = '002'
                elif self.select_kind == u'日商正職/就職講座':
                    serial_number = '011'
                elif self.select_kind == u'赴日講座':
                    serial_number = '032'
                elif self.select_kind == u'日語課程試聽':
                    serial_number = '023'
                elif self.select_kind == u'長期留學【語言學校/專門學校/大學(院)】':
                    serial_number = '017'
                elif self.select_kind == u'短期遊學【語言學校/遊學團】':
                    serial_number = '014'
                elif self.select_kind == u'打工度假(無簽證)【簽證申請事宜】':
                    serial_number = '008'
                elif self.select_kind == u'打工度假(有簽證)【詢問學校/工作/住宿】':
                    serial_number = '036'
                elif self.select_kind == u'日商正職工作【台灣/日本】':
                    serial_number = '011'
                elif self.select_kind == u'住宿/其他/未定':
                    serial_number = '002'
                elif self.select_kind == u'申請中/已合格/留學簽':
                    serial_number = '036'
                elif self.select_kind == u'尚未申請':
                    serial_number = '008'
                elif self.select_kind == u'其它':
                    serial_number = '011'
                elif self.select_kind == u'工作方案-預約諮詢':
                    serial_number = '036'
            if self.select_location == u'台中店':
                if self.select_kind == u'留遊學':
                    serial_number = '016'
                elif self.select_kind == u'打工度假(無簽證)':
                    serial_number = '007'
                elif self.select_kind == u'打工度假(有簽證)':
                    serial_number = '035'
                elif self.select_kind == u'住宿':
                    serial_number = '001'
                elif self.select_kind == u'日商正職/就職講座':
                    serial_number = '010'
                elif self.select_kind == u'赴日講座':
                    serial_number = '033'
                elif self.select_kind == u'日語課程試聽':
                    serial_number = '022'
                elif self.select_kind == u'長期留學【語言學校/專門學校/大學(院)】':
                    serial_number = '016'
                elif self.select_kind == u'短期遊學【語言學校/遊學團】':
                    serial_number = '013'
                elif self.select_kind == u'打工度假(無簽證)【簽證申請事宜】':
                    serial_number = '007'
                elif self.select_kind == u'打工度假(有簽證)【詢問學校/工作/住宿】':
                    serial_number = '035'
                elif self.select_kind == u'日商正職工作【台灣/日本】':
                    serial_number = '010'
                elif self.select_kind == u'住宿/其他/未定':
                    serial_number = '001'
                elif self.select_kind == u'申請中/已合格/留學簽':
                    serial_number = '035'
                elif self.select_kind == u'尚未申請':
                    serial_number = '007'
                elif self.select_kind == u'其它':
                    serial_number = '010'
                elif self.select_kind == u'工作方案-預約諮詢':
                    serial_number = '035'
            if self.select_location == u'高雄店':
                if self.select_kind == u'留遊學':
                    serial_number = '018'
                elif self.select_kind == u'打工度假(無簽證)':
                    serial_number = '009'
                elif self.select_kind == u'打工度假(有簽證)':
                    serial_number = '037'
                elif self.select_kind == u'住宿':
                    serial_number = '003'
                elif self.select_kind == u'日商正職/就職講座':
                    serial_number = '012'
                elif self.select_kind == u'赴日講座':
                    serial_number = '034'
                elif self.select_kind == u'日語課程試聽':
                    serial_number = '024'
                elif self.select_kind == u'長期留學【語言學校/專門學校/大學(院)】':
                    serial_number = '018'
                elif self.select_kind == u'短期遊學【語言學校/遊學團】':
                    serial_number = '015'
                elif self.select_kind == u'打工度假(無簽證)【簽證申請事宜】':
                    serial_number = '021'
                elif self.select_kind == u'打工度假(有簽證)【詢問學校/工作/住宿】':
                    serial_number = '029'
                elif self.select_kind == u'日商正職工作【台灣/日本】':
                    serial_number = '012'
                elif self.select_kind == u'住宿/其他/未定':
                    serial_number = '029'
                elif self.select_kind == u'申請中/已合格/留學簽':
                    serial_number = '037'
                elif self.select_kind == u'尚未申請':
                    serial_number = '009'
                elif self.select_kind == u'其它':
                    serial_number = '012'
                elif self.select_kind == u'工作方案-預約諮詢':
                    serial_number = '037'
        if (self.select_location == False or self.select_location == None or self.select_location == '') and self.select_kind == u'工作方案履歷書':
            serial_number = '038'
        if serial_number != '':
            team_id = self.env['crm.team'].search([('serial_number', '=', serial_number)], limit=1)
        doc = []
        partner_id = self.env['res.partner'].search([('name', '=', self.partner_id), ('mobile', '=', self.phone)])
        for line in partner_id:
            if line.id not in doc:
                doc.append(line.id)
        partner_id = self.env['res.partner'].search([('name', '=', self.partner_id), ('email', '=', self.email_from)])
        for line in partner_id:
            if line.id not in doc:
                doc.append(line.id)
        partner_id = self.env['res.partner'].search([('mobile', '=', self.phone), ('email', '=', self.email_from)])
        for line in partner_id:
            if line.id not in doc:
                doc.append(line.id)
        if len(doc) > 0:
            partner_id = self.env['res.partner'].browse(doc)
        elif len(doc) == 0:
            partner_id = None
        event_plan_id = False
        if self.event_plan_id and self.event_plan_id.isdigit():
            event_plan_id = self.env['event.plan'].search([('id', '=', int(self.event_plan_id))], limit=1)
        company_id = self.env['res.company'].search([('name','=',self.select_location)])
        if partner_id:
            for line in partner_id:
                gender = None
                if self.gender == '男':
                    gender = '1'
                elif self.gender == '女':
                    gender = '2'
                line.user_id = user_id.id if user_id else False
                line.street = self.street
                line.function = self.function
                line.japanese_level = self.jp_level if self.jp_level else ''
                line.line_ID = self.line_id if self.line_id else ''
                line.mobile = self.phone
                line.highest_education = self.highest_education
                line.status = self.status
                line.highest_education_school = self.school_name
                line.highest_education_department = self.department
                line.birth_date = self.birth_date
                line.company_id = company_id.id if company_id else False
                line.sex = gender
                line.new1 = self.new1
                line.new2 = self.new2
                line.new3 = self.new3
                line.new4 = self.new4
                line.new5 = self.new5
        if not partner_id:
            gender = None
            if self.gender == '男':
                gender = '1'
            elif self.gender == '女':
                gender = '2'
            partner_id = self.env['res.partner'].create({
                'name': self.partner_id,
                'phone': self.phone,
                'email': self.email_from,
                'user_id': user_id.id if user_id else False,
                'street': self.street,
                'function': self.function,
                'japanese_level': self.jp_level if self.jp_level else '',
                'line_ID': self.line_id if self.line_id else '',
                'mobile': self.phone,
                'sex': gender,
                'highest_education': self.highest_education,
                'status': self.status,
                'highest_education_school': self.school_name,
                'highest_education_department': self.department,
                'birth_date': self.birth_date,
                'company_id': company_id.id if company_id else False,
                'new1': self.new1,
                'new2': self.new2,
                'new3': self.new3,
                'new4': self.new4,
                'new5': self.new5
            })
        if not team_id:
            raise ValidationError(u'請正確輸入銷售團隊')
        crm_source_id = self.env['utm.source'].search([('src_identify', '=', self.crm_source)], limit=1)
        for line in partner_id:
            crm_lead_id.create({
                'partner_id': line.id,
                'name': self.name,
                'email_from': self.email_from,
                'phone': self.phone,
                'date_deadline': self.date_deadline,
                'user_id': user_id.id if user_id else False,
                'team_id': team_id.id if team_id else False,
                'crm_register_id':self.id,
                'event_plan_id': event_plan_id.id if event_plan_id else False,
                'hope_service': self.hope_service,
                'hope_location': self.hope_location,
                'hope_departure': self.hope_departure,
                'hope_area': self.hope_area,
                'hope_jobcondition': self.hope_jobcondition,
                'other_issue': self.other_issue,
                'country': self.country,
                'visa1': self.visa,
                'jp_level': self.jp_level,
                'hope_job': self.hope_job,
                'crm_source_id': crm_source_id.id if crm_source_id else False,
                'gender': self.gender,
                'highest_education': self.highest_education,
                'status': self.status,
                'school_name': self.school_name,
                'department': self.department,
                'birth_date': self.birth_date,
                'x_company_id': company_id.id if company_id else False,
                'select_location': self.select_location,
                'select_kind': self.select_kind,
                'new1': self.new1,
                'new2': self.new2,
                'new3': self.new3,
                'new4': self.new4,
                'new5': self.new5
            })
        self.is_transfer = True

    def batch_transfer_information(self):
        crm_register_id = self.env['crm.register'].search([('is_transfer','=',False)])
        if crm_register_id:
            for line in crm_register_id:
                crm_lead_id = self.env['crm.lead']
                team_id = self.env['crm.team']
                user_id = self.env['res.users']
                serial_number = ''
                if line.user_id:
                    user_id = self.env['res.users'].search([('name', '=', line.user_id)], limit=1)
                if line.team_id:
                    team_id = self.env['crm.team'].search([('serial_number', '=', line.team_id)], limit=1)
                if line.select_kind and line.select_location and not line.team_id:
                    if line.select_location == u'台北店':
                        if line.select_kind == u'留遊學':
                            serial_number = '017'
                        elif line.select_kind == u'打工度假(無簽證)':
                            serial_number = '008'
                        elif line.select_kind == u'打工度假(有簽證)':
                            serial_number = '036'
                        elif line.select_kind == u'住宿':
                            serial_number = '002'
                        elif line.select_kind == u'日商正職/就職講座':
                            serial_number = '011'
                        elif line.select_kind == u'赴日講座':
                            serial_number = '032'
                        elif line.select_kind == u'日語課程試聽':
                            serial_number = '023'
                        elif line.select_kind == u'長期留學【語言學校/專門學校/大學(院)】':
                            serial_number = '017'
                        elif line.select_kind == u'短期遊學【語言學校/遊學團】':
                            serial_number = '014'
                        elif line.select_kind == u'打工度假(無簽證)【簽證申請事宜】':
                            serial_number = '008'
                        elif line.select_kind == u'打工度假(有簽證)【詢問學校/工作/住宿】':
                            serial_number = '036'
                        elif line.select_kind == u'日商正職工作【台灣/日本】':
                            serial_number = '011'
                        elif line.select_kind == u'住宿/其他/未定':
                            serial_number = '002'
                        elif line.select_kind == u'申請中/已合格/留學簽':
                            serial_number = '036'
                        elif line.select_kind == u'尚未申請':
                            serial_number = '008'
                        elif line.select_kind == u'其它':
                            serial_number = '011'
                        elif line.select_kind == u'工作方案-預約諮詢':
                            serial_number = '036'
                    if line.select_location == u'台中店':
                        if line.select_kind == u'留遊學':
                            serial_number = '016'
                        elif line.select_kind == u'打工度假(無簽證)':
                            serial_number = '007'
                        elif line.select_kind == u'打工度假(有簽證)':
                            serial_number = '035'
                        elif line.select_kind == u'住宿':
                            serial_number = '001'
                        elif line.select_kind == u'日商正職/就職講座':
                            serial_number = '010'
                        elif line.select_kind == u'赴日講座':
                            serial_number = '033'
                        elif line.select_kind == u'日語課程試聽':
                            serial_number = '022'
                        elif line.select_kind == u'長期留學【語言學校/專門學校/大學(院)】':
                            serial_number = '016'
                        elif line.select_kind == u'短期遊學【語言學校/遊學團】':
                            serial_number = '013'
                        elif line.select_kind == u'打工度假(無簽證)【簽證申請事宜】':
                            serial_number = '007'
                        elif line.select_kind == u'打工度假(有簽證)【詢問學校/工作/住宿】':
                            serial_number = '035'
                        elif line.select_kind == u'日商正職工作【台灣/日本】':
                            serial_number = '010'
                        elif line.select_kind == u'住宿/其他/未定':
                            serial_number = '001'
                        elif line.select_kind == u'申請中/已合格/留學簽':
                            serial_number = '035'
                        elif line.select_kind == u'尚未申請':
                            serial_number = '007'
                        elif line.select_kind == u'其它':
                            serial_number = '010'
                        elif line.select_kind == u'工作方案-預約諮詢':
                            serial_number = '035'
                    if line.select_location == u'高雄店':
                        if line.select_kind == u'留遊學':
                            serial_number = '018'
                        elif line.select_kind == u'打工度假(無簽證)':
                            serial_number = '009'
                        elif line.select_kind == u'打工度假(有簽證)':
                            serial_number = '037'
                        elif line.select_kind == u'住宿':
                            serial_number = '003'
                        elif line.select_kind == u'日商正職/就職講座':
                            serial_number = '012'
                        elif line.select_kind == u'赴日講座':
                            serial_number = '034'
                        elif line.select_kind == u'日語課程試聽':
                            serial_number = '024'
                        elif line.select_kind == u'長期留學【語言學校/專門學校/大學(院)】':
                            serial_number = '018'
                        elif line.select_kind == u'短期遊學【語言學校/遊學團】':
                            serial_number = '015'
                        elif line.select_kind == u'打工度假(無簽證)【簽證申請事宜】':
                            serial_number = '021'
                        elif line.select_kind == u'打工度假(有簽證)【詢問學校/工作/住宿】':
                            serial_number = '029'
                        elif line.select_kind == u'日商正職工作【台灣/日本】':
                            serial_number = '012'
                        elif line.select_kind == u'住宿/其他/未定':
                            serial_number = '029'
                        elif line.select_kind == u'申請中/已合格/留學簽':
                            serial_number = '037'
                        elif line.select_kind == u'尚未申請':
                            serial_number = '009'
                        elif line.select_kind == u'其它':
                            serial_number = '012'
                        elif line.select_kind == u'工作方案-預約諮詢':
                            serial_number = '037'
                if (line.select_location == False or line.select_location == None or line.select_location == '') and line.select_kind == u'工作方案履歷書':
                    serial_number = '038'
                if serial_number != '':
                    team_id = self.env['crm.team'].search([('serial_number', '=', serial_number)], limit=1)
                doc = []
                partner_id = self.env['res.partner'].search([('name', '=', line.partner_id), ('mobile', '=', line.phone)])
                for lines in partner_id:
                    if lines.id not in doc:
                        doc.append(lines.id)
                partner_id = self.env['res.partner'].search([('name', '=', line.partner_id), ('email', '=', line.email_from)])
                for lines in partner_id:
                    if lines.id not in doc:
                        doc.append(lines.id)
                partner_id = self.env['res.partner'].search([('mobile', '=', line.phone), ('email', '=', line.email_from)])
                for lines in partner_id:
                    if lines.id not in doc:
                        doc.append(lines.id)
                if len(doc) > 0:
                    partner_id = self.env['res.partner'].browse(doc)
                elif len(doc) == 0:
                    partner_id = None
                event_plan_id = False
                if line.event_plan_id and line.event_plan_id.isdigit():
                    event_plan_id = self.env['event.plan'].search([('id', '=', int(line.event_plan_id))], limit=1)
                company_id = self.env['res.company'].search([('name','=',line.select_location)])
                if partner_id:
                    for row in partner_id:
                        gender = None
                        if line.gender == '男':
                            gender = '1'
                        elif line.gender == '女':
                            gender = '2'
                        row.user_id = user_id.id if user_id else False
                        row.street = line.street
                        row.function = line.function
                        row.japanese_level = line.jp_level if line.jp_level else ''
                        row.line_ID = line.line_id if line.line_id else ''
                        row.mobile = line.phone
                        row.highest_education = line.highest_education
                        row.status = line.status
                        row.highest_education_school = line.school_name
                        row.highest_education_department = line.department
                        row.birth_date = line.birth_date
                        row.sex = gender
                        row.company_id = company_id.id
                        row.new1 = line.new1
                        row.new2 = line.new2
                        row.new3 = line.new3
                        row.new4 = line.new4
                        row.new5 = line.new5
                if not partner_id:
                    gender = None
                    if line.gender == '男':
                        gender = '1'
                    elif line.gender == '女':
                        gender = '2'
                    partner_id = self.env['res.partner'].create({
                        'name': line.partner_id,
                        'phone': line.phone,
                        'email': line.email_from,
                        'user_id': user_id.id if user_id else False,
                        'street': line.street,
                        'function': line.function,
                        'japanese_level': line.jp_level if line.jp_level else '',
                        'line_ID': line.line_id if line.line_id else '',
                        'mobile': line.phone,
                        'sex': gender,
                        'highest_education': line.highest_education,
                        'status': line.status,
                        'highest_education_school': line.school_name,
                        'highest_education_department': line.department,
                        'birth_date': line.birth_date,
                        'company_id': company_id.id if company_id else False,
                        'select_location': line.select_location,
                        'select_kind': line.select_kind,
                        'new1': line.new1,
                        'new2': line.new2,
                        'new3': line.new3,
                        'new4': line.new4,
                        'new5': line.new5
                    })
                crm_source_id = self.env['utm.source'].search([('src_identify', '=', line.crm_source)], limit=1)
                for x in partner_id:
                    crm_lead_id.create({
                        'partner_id': x.id,
                        'name': line.name,
                        'email_from': line.email_from,
                        'phone': line.phone,
                        'date_deadline': line.date_deadline,
                        'user_id': user_id.id if user_id else False,
                        'team_id': team_id.id if team_id else False,
                        'crm_register_id': line.id,
                        'event_plan_id': event_plan_id.id if event_plan_id else False,
                        'hope_service': line.hope_service,
                        'hope_location': line.hope_location,
                        'hope_departure': line.hope_departure,
                        'hope_area': line.hope_area,
                        'hope_jobcondition': line.hope_jobcondition,
                        'other_issue': line.other_issue,
                        'country': line.country,
                        'visa1': line.visa,
                        'jp_level': line.jp_level,
                        'hope_job': line.hope_job,
                        'crm_source_id': crm_source_id.id if crm_source_id else False,
                        'gender': line.gender,
                        'highest_education': line.highest_education,
                        'status': line.status,
                        'school_name': line.school_name,
                        'department': line.department,
                        'birth_date': line.birth_date,
                        'x_company_id': company_id.id if company_id else False,
                        'new1': line.new1,
                        'new2': line.new2,
                        'new3': line.new3,
                        'new4': line.new4,
                        'new5': line.new5
                    })
                line.is_transfer = True


#mame-cabinet-joystick.py by Adam Goldsmith 2015-05-11
#based on rpi-gpio-jstk.py by Chris Swan 9 Aug 2012
#using python-uinput

import uinput
import time
import RPi.GPIO as GPIO
from config import *

GPIO.setmode(GPIO.BOARD)

#Second dictionary to store state
isPressed = {}

# initialize keys
for pin in keymap.keys():
  GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  isPressed[pin] = False

def checkPressed (state, key, value):
  value[0].emit(value[1], value[2])
  if (not isPressed[key]) and state:
    isPressed[key] = True
    value[0].emit(value[1], value[2])
  if isPressed[key] and (not state):
    isPressed[key] = False
    value[0].emit(value[1], value[3])

while True:
  for key, value in keymap.items():
    checkPressed((not GPIO.input(key)), key, value)
  time.sleep(.01)  # Poll every 10ms (otherwise CPU load gets too high)


#This module runs Sibelia
##############################################################

import os
import sys
import shutil
import subprocess
import copy
import logging

from ragout.shared import utils
from .synteny_backend import SyntenyBackend, BackendException

logger = logging.getLogger()

SIBELIA_EXEC = "Sibelia"
SIBELIA_WORKDIR = "sibelia-workdir"
try:
    SIBELIA_INSTALL = os.environ["SIBELIA_INSTALL"]
    os.environ["PATH"] += os.pathsep + SIBELIA_INSTALL
except:
    pass

#PUBLIC:
################################################################

class SibeliaBackend(SyntenyBackend):
    def __init__(self):
        SyntenyBackend.__init__(self)

    def run_backend(self, recipe, output_dir, overwrite):
        files = {}
        work_dir = os.path.join(output_dir, SIBELIA_WORKDIR)
        if overwrite and os.path.isdir(work_dir):
            shutil.rmtree(work_dir)

        if os.path.isdir(work_dir):
            #using existing results
            logger.warning("Using existing Sibelia results from previous run")
            logger.warning("Use --overwrite to force alignment")
            for block_size in recipe["blocks"]:
                block_dir = os.path.join(work_dir, str(block_size))
                perm_file = os.path.join(block_dir, "genomes_permutations.txt")
                if not os.path.isfile(perm_file):
                    raise BackendException("Exitsing results are incompatible "
                                           "with input recipe")
                files[block_size] = os.path.abspath(perm_file)

        else:
            for genome, params in recipe["genomes"].items():
                if "fasta" not in params:
                    raise BackendException("FASTA file for '{0}' is not "
                                           "specified".format(genome))

            os.mkdir(work_dir)
            chr2genome = _get_chr2genome(recipe)
            for block_size in recipe["blocks"]:
                block_dir = os.path.join(work_dir, str(block_size))
                if not os.path.isdir(block_dir):
                    os.mkdir(block_dir)

                all_fasta = [p["fasta"] for p in recipe["genomes"].values()]
                perm_file = _run_sibelia(all_fasta, block_size, block_dir)
                _postprocess(chr2genome, perm_file)
                files[block_size] = perm_file

        return files

def _check_installation():
    return bool(utils.which(SIBELIA_EXEC))

if _check_installation():
    logger.debug("Sibelia is installed")
    SyntenyBackend.register_backend("sibelia", SibeliaBackend())
else:
    logger.debug("Sibelia is not installed")

#PRIVATE:
#################################################################

def _get_chr2genome(recipe):
    chr2genome = {}
    #for gen_name, fasta in genomes.items():
    for gen_name, gen_params in recipe["genomes"].items():
        if not os.path.exists(gen_params["fasta"]):
            raise BackendException("Can't open '{0}'"
                                   .format(gen_params["fasta"]))

        with open(gen_params["fasta"], "r") as f:
            for line in f:
                line = line.strip()
                if line.startswith(">"):
                    chr_name = line.strip()[1:].split(" ")[0]
                    chr2genome[chr_name] = gen_name
    return chr2genome

def _postprocess(chr2genome, file):
    new_file = file + "_new"
    with open(file, "r") as fin, open(new_file, "w") as fout:
        for line in fin:
            line = line.strip()
            if line.startswith(">"):
                chr_name = line[1:]
                fout.write(">{0}.{1}\n".format(chr2genome[chr_name], chr_name))
            else:
                fout.write(line + "\n")
    os.remove(file)
    os.rename(new_file, file)

def _run_sibelia(fasta_files, block_size, out_dir):

    logger.info("Running Sibelia with block size " + str(block_size))
    if not utils.which(SIBELIA_EXEC):
        raise BackendException("Sibelia is not installed")

    devnull = open(os.devnull, "w")
    cmdline = [SIBELIA_EXEC, "-s", "fine", "-m", str(block_size), "-o", out_dir]
    cmdline.extend(fasta_files)
    subprocess.check_call(cmdline, stdout=devnull)

    os.remove(os.path.join(out_dir, "d3_blocks_diagram.html"))
    shutil.rmtree(os.path.join(out_dir, "circos"))

    return os.path.join(out_dir, "genomes_permutations.txt")