'''

Tomcat
1. 搭建窗口信息

'''
import pygame,sys

class Tom(object):
    ''' 1. main() 窗口基本注释 '''
    def main(self):
        # 1.1设置窗口标题
        pygame.display.set_caption('汤姆猫',)
        # 1.2设置死循环
        while True:
            # 1.4 业务逻辑执行
            self.action()
            # 1.5 图形图片绘制
            self.paint()
            # 1.3 更新屏幕
            pygame.display.update()

    ''' 2. action函数  业务逻辑处理 '''
    def action(self):
        # 2.1 事件监听迭代
        for event in pygame.event.get():
            # 2.2 判断事件类型
            if event.type == pygame.QUIT:
                sys.exit()
            # 2.3 鼠标单击
            mouseX, mouseY = pygame.mouse.get_pos()
            # 获取鼠标单击事件
            # [0] 左键单击 [1] 左键双击 [2] 右击
            leftFlag = pygame.mouse.get_pressed()[0]
            # 2.4 判断
            if leftFlag and 30 < mouseX < 30+60 \
                and 300 < mouseY < 300 + 60:
                # 吃鸟的动作
                #print("进入了")
                self.animation = 0
                # 重新设置index
                self.index = 0
            elif leftFlag and 30 < mouseX < 30+60 \
                and 370 < mouseY < 370 + 60:
                self.animation = 1
                self.index = 0
            elif leftFlag and 30 < mouseX < 30+60 \
                and 440 < mouseY < 440 + 60:
                self.animation = 2
                self.index = 0
            elif leftFlag and 250 < mouseX < 250+60 \
                and 300 < mouseY < 300 + 60:
                self.animation = 3
                self.index = 0
            elif leftFlag and 250 < mouseX < 250+60 \
                and 370 < mouseY < 370 + 60:
                self.animation = 4
                self.index = 0
            elif leftFlag and 250 < mouseX < 250+60 \
                and 440 < mouseY < 440 + 60:
                self.animation = 5
                self.index = 0
            #....未完
            elif leftFlag and 250 < mouseX < 250+60 \
                and 440 < mouseY < 440 + 60:
                self.animation = 6
                self.index = 0

    ''' 3. paint函数 绘制图形'''
    def paint(self):

        # 3.6 判断是否执行动画效果
        if self.animation == 0:
            # 3.2 图片集转换值增加
            self.index += 1
            # 3.3 设置图片转换频率
            # index 0/10 ==> 0 % 10 ==> 0
            # index 1/10 ==> 0 % 10 ==> 0
            # ...
            # index 11/10 ==> 1 % 10 ==> 1
            # ix = 0 0 0 0 0 0 0 0 0 0 1
            # ix 每调用一次函数 10次才会刷新一次
            ix = self.index / 10 % len(self.eats)
            # 3.4 重新赋值图片 ix --> float
            self.background = self.eats[int(ix)]
            # 判断是否是最后一张
            if int(ix) == 39:
                self.animation = -1
        elif self.animation == 1:
            self.index += 1
            ix = self.index / 10 % len(self.drinks)
            self.background = self.drinks[int(ix)]
            if int(ix) == 79:
                self.animation = -1
        elif self.animation == 2:
            self.index += 1
            ix = self.index / 30 % len(self.cymbals)
            self.background = self.cymbals[int(ix)]
            if int(ix) == 11:
                self.animation = -1
        elif self.animation == 3:
            self.index += 1
            ix = self.index / 10 % len(self.farts)
            self.background = self.farts[int(ix)]
            if int(ix) == 26:
                self.animation = -1
        elif self.animation == 4:
            self.index += 1
            ix = self.index / 10 % len(self.pies)
            self.background = self.pies[int(ix)]
            if int(ix) == 22:
                self.animation = -1
        elif self.animation == 5:
            self.index += 1
            ix = self.index / 10 % len(self.scratchs)
            self.background = self.scratchs[int(ix)]
            if int(ix) == 54:
                self.animation = -1
        elif self.animation == 6:#头
            self.index += 1
            ix = self.index / 10 % len(self.angrys)
            self.background = self.angrys[int(ix)]
            if int(ix) == 24:
                self.animation = -1
        elif self.animation == 7:#肚子
            self.index += 1
            ix = self.index / 10 % len(self.stomachs)
            self.background = self.stomachs[int(ix)]
            if int(ix) == 32:
                self.animation = -1
        elif self.animation == 8:#尾巴
            self.index += 1
            ix = self.index / 10 % len(self.knockouts)
            self.background = self.knockouts[int(ix)]
            if int(ix) == 79:
                self.animation = -1
        elif self.animation == 9:#左脚
            self.index += 1
            ix = self.index / 10 % len(self.footLefts)
            self.background = self.stomachs[int(ix)]
            if int(ix) == 28:
                self.animation = -1
        elif self.animation == 10:#右脚
            self.index += 1
            ix = self.index / 10 % len(self.footRights)
            self.background = self.footRights[int(ix)]
            if int(ix) == 28:
                self.animation = -1

        # 3.1 绘制背景图片
        self.screen.blit(pygame.transform.scale(self.background,(320, 512)), (0, 0))
        # 3.5 绘制吃鸟动作
        self.screen.blit(self.eat, (30, 300))
        self.screen.blit(self.drink, (30, 370))
        self.screen.blit(self.cymbal, (30, 440))
        self.screen.blit(self.fart, (250, 300))
        self.screen.blit(self.pie, (250, 370))
        self.screen.blit(self.scratch, (250, 440))
        #self.screen.blit(self.angry, (250, 440))
        #self.screen.blit(self.stomach, (250, 440))
        #self.screen.blit(self.knockout, (250, 440))
        #self.screen.blit(self.footLeft, (250, 440))
        #self.screen.blit(self.footRights, (250, 440))

    ''' 4.__init__ 函数 属性初始化'''
    def __init__(self):
        # 窗口大小设置
        self.screen = pygame.display.set_mode((320, 512), 0, 0)
        # 背景图片
        self.background = pygame.image.load("Animations/Eat/eat_00.jpg")
        # 图片列表存储
        self.eats = self.getImage("Animations/Eat/eat_0", "Animations/Eat/eat_", ".jpg", 40)
        self.drinks = self.getImage("Animations/Drink/drink_0","Animations/Drink/drink_",".jpg", 80)
        self.cymbals = self.getImage("Animations/Cymbal/cymbal_0", "Animations/Cymbal/cymbal_", ".jpg", 12)
        self.farts = self.getImage("Animations/Fart/fart_0", "Animations/Fart/fart_", ".jpg", 27)
        self.pies = self.getImage("Animations/Pie/pie_0", "Animations/Pie/pie_", ".jpg", 23)
        self.scratchs = self.getImage("Animations/Scratch/scratch_0", "Animations/Scratch/scratch_", ".jpg", 55)
        self.angrys = self.getImage("Animations/Angry/angry_0", "Animations/Angry/angry_", ".jpg", 25)
        self.stomachs = self.getImage("Animations/Stomach/stomach_0", "Animations/Stomach/stomach_", ".jpg", 33)
        self.knockouts = self.getImage("Animations/Knockout/knockout_0", "Animations/Knockout/knockout_", ".jpg", 80)
        self.footLefts = self.getImage("Animations/FootLeft/footLeft_0", "Animations/FootLeft/footLeft_", ".jpg", 29)
        self.footRights = self.getImage("Animations/FootRight/footRight_0", "Animations/FootRight/footRight_", ".jpg", 29)
        # 图片集转换值
        self.index = 0
        # 吃鸟
        self.eat = pygame.image.load("Buttons/eat.png")
        # 判断动画动作
        self.animation = -1
        # 喝奶
        self.drink = pygame.image.load("Buttons/drink.png")
        self.cymbal = pygame.image.load("Buttons/cymbal.png")
        self.fart = pygame.image.load("Buttons/fart.png")
        self.pie = pygame.image.load("Buttons/pie.png")
        self.scratch = pygame.image.load("Buttons/scratch.png")

    ''' 5. getImage() 获取图片列表'''
    def getImage(self,prePath1, prePath2, endPath, picNum):
        # 5.4 定义列表
        imageList = []
        # 5.1 循环迭代赋值图片
        for i in range(0, picNum):
            # 5.2 获取图片路径
            if i < 10:
                imgPath = prePath1 + str(i) + endPath
            else:
                imgPath = prePath2 + str(i) + endPath
            # 5.3 返回列表
            imageList.append(pygame.image.load(imgPath))
        # 5.5 返回
        return imageList

if __name__ == '__main__':
    # 获取类
    tm = Tom()
    # 调用类方法
    tm.main()


## -*- encoding: utf-8 -*-
import os
import sys
from setuptools import setup
from codecs import open # To open the README file with proper encoding
from setuptools.command.test import test as TestCommand # for tests

# Get information from separate files (README, VERSION)
def readfile(filename):
    with open(filename,  encoding='utf-8') as f:
        return f.read()

# For the tests
class SageTest(TestCommand):
    def run_tests(self):
        errno = os.system("sage -t --force-lib sage_widget_adapters")
        if errno != 0:
            sys.exit(1)
        errno = os.system("sage -t --force-lib sage_combinat_widgets")
        if errno != 0:
            sys.exit(1)

setup(
    name = "sage-combinat-widgets",
    version = readfile("VERSION"),
    description='Jupyter widgets for SAGE Combinat',
    long_description = readfile("README.rst"),
    url='https://github.com/sagemath/sage-combinat-widgets',
    author='Odile Bénassy, Nicolas M. Thiéry',
    author_email='odile.benassy@u-psud.fr',
    license='GPLv3+',
    classifiers=[
      # How mature is this project? Common values are
      #   3 - Alpha
      #   4 - Beta
      #   5 - Production/Stable
      'Development Status :: 3 - Alpha',
      'Intended Audience :: Science/Research',
      'Topic :: Scientific/Engineering :: Mathematics',
      'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
      'Programming Language :: Python :: 2.7',
    ], # classifiers list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
    keywords = "SageMath widget combinatorics",
    packages = ['sage_combinat_widgets', 'sage_widget_adapters', 'sage_widget_adapters.combinat', 'sage_widget_adapters.graphs', 'sage_widget_adapters.matrix'],
    cmdclass = {'test': SageTest}, # adding a special setup command for tests
    install_requires = ['ipywidgets']
)


# model evaluation method for SVM and Random Forest
def f1_score(y_test,y_predicted):
    tp,fp,tn,fn=0
    for i in range(len(y_test)):
        if y_test==0 and y_predicted==0:
            tn+=1
        elif y_test==0 and y_predicted==1:
            fp+=1
        elif y_test==1 and y_predicted==1:
            tp+=1
        else:
            fn+=1
    recall=tp/fn+tp
    precision=tp/tp+fp
    f1=2*precision*recall/(precision+recall)
    return f1


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 28 15:36:21 2020

@author: gautam6
"""

def gcd(a,b):   # defines a function for greatest common diviser of any variable a and b
    if a>b:     #When a is greater than b,if b is zero gcd will be a
        if b==0:
            return a
        else:              #Or else reminder of a/b will again run in a function
            reminder=a%b         #So, the gcd will be the value when reminder is zero
            return gcd(b,reminder)
    else:                   #When b is equals to a or greater than a, repeat the step just exchanging the position
        if a==0:
            return b
        else:
            reminder=b%a
            return gcd(a,reminder)


import sys,string
a1 = int(input())
L = [ int(x1) for x1 in input().split()]
a1 = len(L)
cn = 0
for i in range(0,a1-2) :
    for j in range(i+1, a1-1):
        for k in range(j+1, a1):
            if L[i] > L[j] > L[k] :
                cn += 1
print(cn)


# Kim de Bie 11077379
# This Python script loads csv data into JSON objects.
# I previously used (an altered) version of this code for my 'Heuristieken' project.

import csv
import json
# Returns a dictionary with area names as keys and neighbours as values
def load_json(filename):

    # open provided file
    file_csv = open(filename, 'r')

    # create field names
    fieldnames = ("country", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014")

    # initiate reader for the csv file
    reader = csv.DictReader(file_csv, fieldnames)

    # create list
    data = []

    # read all rows of csv_file and save them as lists (within list)
    for row in reader:
        data.append(row)

    # turn into JSON and save as textfile
    with open("jsondataGNI.json", 'wb') as outfile:
        json.dump(data, outfile)

# call the load_json function
json_data = load_json("GNIgrowth2005-14.csv")


from math import factorial

cns = []
limit = int(input('enter limit: '))

def is_special(n):
    temp_sum = 0
    for a in n:
        temp_sum += factorial(int(a))
    if temp_sum == int(n):
        return True
    return False

for i in range(10, limit):
    if is_special(str(i)):
        cns.append(i)

print(sum(cns))


"""
This script creates an instance of a sacred experiment and defines default configurations.
"""

from src.neural_nets.models import get_model
from src.neural_nets.load_data import get_loader
from src.neural_nets.metrics import MaskedBCE, Accuracy, compute_acc, compute_loss

import src.regression.trainer as train_reg

import os
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torchsso.optim as soptim
import torch.nn.functional as F
import random

from torch.utils.data import DataLoader
from sacred import Experiment
from torch import Tensor, device
from copy import deepcopy
from time import sleep
from tqdm import tqdm

from typing import List
from itertools import product

# create a new sacred experiment whose name is an integer
ex = Experiment(name=str(random.randint(0, 1000000)))

# default configurations
@ex.config
def cfg():

    # system
    cuda = torch.cuda.is_available()
    gpu = 0
    base_dir = os.getcwd()

    # supported datasets
    # JSB_Chorales (short)
    # Nottingham (medium)
    # Piano_midi (long)
    # MuseData (extra long)
    dataset = "JSB_Chorales"

    # training
    num_epochs = 150
    batch_size = 128
    lr = 0.001
    decay = 1.0
    optmzr = "SGD"
    regularization = 0.0

    # hyperparameter search
    do_hpsearch = False
    learning_rates = 10**np.linspace(-2, -4, 5)
    decays = 1 - np.linspace(0, 0.1, num=5)
    regularizations = 10**np.linspace(-2, -4, num=5)
    hps_epochs = 50

    # Supported architectures
    # LINEAR (LDS)
    # REGRESSION (regress next note based on last note)
    # REGRESSION_8_STEP (regress next note based on last 8 notes)
    architecture = 'LDS'
    readout = 'linear'
    gradient_clipping = 1
    jit = False # not fully implemented
    # for regression
    lag = 1
    window = 1
    # for neural networks
    input_size = 88
    hidden_size = 300
    num_layers = 1
    output_size = 88

    # see models.py and initialization.py for details
    init = 'default'
    scale = 1.0
    parity = None # see models.py
    t_distrib = torch.distributions.Uniform(0, 0.75)
    path = 'results/77/final_state_dict.pt'

    # when to save state dictionaries
    save_init_model = True
    save_final_model = True
    save_every_epoch = False

    # detect backprop anomalies
    detect_anomaly = False

# give all random number generators the same seed
def _seed_all(_seed) -> None:
    torch.manual_seed(_seed)
    np.random.seed(_seed)
    random.seed(_seed)

# this context is used when we are running things on the cpu
class NullContext(object):
    def __init__(self):
        pass
    def __enter__(self):
        pass
    def __exit__(self, type, value, traceback):
        pass

# this function simply trains regression models and logs the results
# see regression.trainer for details
@ex.capture
def sklearn_experiment(dataset: str, save_dir: str, lag: int, window: int, _seed, _log, _run):
    """
    :param dataset: name of the dataset to be used
    :save_dir: temporary directory where artifacts are being stored
    :lag: how many time steps into the future the regression model is to predict
    :window: how many time steps the regression model is to take into account
    :param _seed: sacred random seed
    :param _log: sacred object used to output to the command line
    :param _run: sacred object used to monitor the runtime
    """

    models = train_reg.train_models(dataset, _seed, lag=lag, window=window)

    coefs = np.zeros((48, 48*window))
    intercepts = np.zeros(48*window)

    for i in range(48):
        coefs[i] = models[i].coef_
        intercepts[i] = models[i].intercept_

    np.save(save_dir + 'coefs.npy', coefs)
    np.save(save_dir + 'intercepts.npy', intercepts)

    _run.add_artifact(save_dir + 'coefs.npy')
    _run.add_artifact(save_dir + 'intercepts.npy')

    train_loss = train_reg.compute_loss(models, dataset, 'traindata', lag=lag, window=window)
    test_loss = train_reg.compute_loss(models, dataset, 'testdata', lag=lag, window=window)
    valid_loss = train_reg.compute_loss(models, dataset, 'validdata', lag=lag, window=window)

    _run.log_scalar('trainLoss', train_loss)
    _run.log_scalar('testLoss', test_loss)
    _run.log_scalar('validLoss', valid_loss)

    train_acc = train_reg.compute_accuracy(models, dataset, 'traindata', lag=lag, window=window)
    test_acc = train_reg.compute_accuracy(models, dataset, 'testdata', lag=lag, window=window)
    valid_acc = train_reg.compute_accuracy(models, dataset, 'validdata', lag=lag, window=window)

    _run.log_scalar('trainAccuracy', train_acc)
    _run.log_scalar('testAccuracy', test_acc)
    _run.log_scalar('validAccuracy', valid_acc)

# a single optimization step
@ex.capture
def train_iter(device: device,
               cuda_device: torch.cuda.device,
               input_tensor: Tensor,
               target: Tensor,
               mask: Tensor,
               model: nn.Module,
               loss_fcn: nn.Module,
               optimizer: optim.Optimizer,
               save_every_epoch: bool,
               save_dir: str,
               train_loader: DataLoader,
               test_loader: DataLoader,
               valid_loader: DataLoader,
               _log,
               _run,
               logging=True):

    input_tensor = input_tensor.to(device)

    # number of songs in this batch
    N = input_tensor.shape[0]

    output, hidden_tensors = model(input_tensor)

    loss = loss_fcn(output, target, mask, model)/N
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    # use sacred to log training loss and accuracy
    if logging:
        train_acc = compute_acc(model, train_loader)
        _run.log_scalar("trainLoss", loss.cpu().detach().item())
        _run.log_scalar("trainAccuracy", train_acc)

    # save a copy of the model and make sacred remember it each epoch
    if save_every_epoch and logging:
        sd = deepcopy(model.state_dict())
        torch.save(init_sd, save_dir + 'state_dict_' + str(epoch) + '.pt')
        _run.add_artifact(save_dir + 'state_dict_' + str(epoch) + '.pt')

# train a neural network
# returns the final loss and accuracy on the training, testing, and validation sets
@ex.capture
def pytorch_train_loop(cuda: bool,
                       model_dict: dict,
                       initializer: dict,
                       train_loader: DataLoader,
                       test_loader: DataLoader,
                       valid_loader: DataLoader,
                       optmzr: str,
                       lr: float,
                       decay: float,
                       regularization: float,
                       num_epochs: int,
                       save_dir: str,
                       save_init_model,
                       save_every_epoch,
                       save_final_model,
                       _seed,
                       _log,
                       _run,
                       logging=True):

    # construct and initialize the model
    model = get_model(model_dict, initializer, cuda)

    # save a copy of the initial model and make sacred remember it
    if save_init_model and logging:
        init_sd = deepcopy(model.state_dict())
        torch.save(init_sd, save_dir + 'initial_state_dict.pt')
        _run.add_artifact(save_dir + 'initial_state_dict.pt')

    # if we are on cuda we construct the device and run everything on it
    cuda_device = NullContext()
    device = torch.device('cpu')
    if cuda:
        dev_name = 'cuda:' + str(gpu)
        cuda_device = torch.cuda.device(dev_name)
        device = torch.device(dev_name)
        model = model.to(device)

    with cuda_device:

        # see metrics.py
        loss_fcn = MaskedBCE(regularization)

        # construct the optimizer
        optimizer = None
        if optmzr == "SGD":
            optimizer = optim.SGD(model.parameters(), lr=lr)
        elif optmzr == "Adam":
            optimizer = optim.Adam(model.parameters(), lr=lr)
        elif optmzr == "RMSprop":
            optimizer = optim.RMSprop(model.parameters(), lr=lr)
        else:
            raise ValueError("Optimizer {} not recognized.".format(optmzr))

        # learning rate decay
        scheduler = None
        scheduler = optim.lr_scheduler.LambdaLR(optimizer, lambda epoch: decay**epoch)

        # begin training loop
        for epoch in tqdm(range(num_epochs)):

            for input_tensor, target, mask in train_loader:
                train_iter(device,
                           cuda_device,
                           input_tensor,
                           target,
                           mask,
                           model,
                           loss_fcn,
                           optimizer,
                           save_every_epoch,
                           save_dir,
                           train_loader,
                           test_loader,
                           valid_loader,
                           _log,
                           _run,
                           logging=logging)

            # learning rate decay
            scheduler.step()

            # use sacred to log testing and validation loss and accuracy
            if logging:

                test_loss = compute_loss(loss_fcn, model, test_loader)
                val_loss = compute_loss(loss_fcn, model, valid_loader)
                test_acc = compute_acc(model, test_loader)
                val_acc = compute_acc(model, valid_loader)

                _run.log_scalar("testLoss", test_loss)
                _run.log_scalar("validLoss", val_loss)
                _run.log_scalar("testAccuracy", test_acc)
                _run.log_scalar("validAccuracy", val_acc)

        # save a copy of the trained model and make sacred remember it
        if save_final_model and logging:
            fin_sd = deepcopy(model.state_dict())
            torch.save(fin_sd, save_dir + 'final_state_dict.pt')
            _run.add_artifact(save_dir + 'final_state_dict.pt')

    train_loss = compute_loss(loss_fcn, model, train_loader)
    test_loss = compute_loss(loss_fcn, model, test_loader)
    val_loss = compute_loss(loss_fcn, model, valid_loader)

    train_acc = compute_acc(model, train_loader)
    test_acc = compute_acc(model, test_loader)
    val_acc = compute_acc(model, valid_loader)

    return ((train_loss, test_loss, val_loss), (train_acc, test_acc, val_acc))

# main function
@ex.automain
def train_loop(cuda,
               gpu,
               base_dir,
               dataset,
               num_epochs,
               batch_size,
               lr,
               decay,
               optmzr,
               regularization,
               do_hpsearch,
               learning_rates,
               decays,
               regularizations,
               hps_epochs,
               architecture,
               readout,
               gradient_clipping,
               jit,
               lag,
               window,
               input_size,
               hidden_size,
               num_layers,
               output_size,
               detect_anomaly,
               init,
               scale,
               parity,
               t_distrib,
               path,
               save_init_model,
               save_final_model,
               save_every_epoch,
               _seed,
               _log,
               _run):

    # save artifacts to a temporary directory that gets erased when the experiment is over
    save_dir = base_dir + '/tmp_' + str(_seed)
    os.system('mkdir ' + save_dir)
    save_dir += '/'

    # give all random number generators the same seed
    _seed_all(_seed)

    sklearn_program = architecture == 'REGRESSION'

    # regression models and neural networks are trained very differently
    if sklearn_program:

        sklearn_experiment(dataset, save_dir, lag, window, _seed, _log, _run)

    # run a pytorch program
    else:

        model_dict = {'architecture': architecture,
                      'readout': readout,
                      'gradient_clipping': gradient_clipping,
                      'jit': jit,
                      'lag': lag,
                      'window': window,
                      'input_size': input_size,
                      'hidden_size': hidden_size,
                      'num_layers': num_layers,
                      'output_size': output_size
                     }

        initializer = {'init': init,
                       'scale': scale,
                       'parity': parity,
                       't_distrib': t_distrib,
                       'path': path
                      }

        # if we are debugging we may want to detect autograd anomalies
        torch.autograd.set_detect_anomaly(detect_anomaly)

        # construct the pytorch data loaders
        train_loader, test_loader, valid_loader = get_loader(dataset, batch_size)

        # standard training loop
        if not do_hpsearch:

            # the training loop function returns the metrics achieved at the end of training
            # they will be logged by default, no need to do anything with them here
            metrics = pytorch_train_loop(cuda,
                                         model_dict,
                                         initializer,
                                         train_loader,
                                         test_loader,
                                         valid_loader,
                                         optmzr,
                                         lr,
                                         decay,
                                         regularization,
                                         num_epochs,
                                         save_dir,
                                         save_init_model,
                                         save_every_epoch,
                                         save_final_model,
                                         _seed,
                                         _log,
                                         _run)

        # only goal here is to find the best hyper parameters
        else:

            min_test_loss = float('inf')
            best_lr = 0
            best_dcay = 0
            best_reg = 0

            hyperparams = product(learning_rates, decays, regularizations)

            for rate, dcay, reg in hyperparams:

                # train a model with the given hyperparameters
                # don't log anything, otherwise we will have a ridiculous amount of extraneous info
                metrics = pytorch_train_loop(cuda,
                                             model_dict,
                                             initializer,
                                             train_loader,
                                             test_loader,
                                             valid_loader,
                                             optmzr,
                                             rate,
                                             dcay,
                                             reg,
                                             hps_epochs,
                                             save_dir,
                                             save_init_model,
                                             save_every_epoch,
                                             save_final_model,
                                             _seed,
                                             _log,
                                             _run,
                                             logging=False)

                # loss is first index, test set is second index
                test_loss = metrics[0][1]

                # compare loss against other hyperparams and update if necessary
                if test_loss == test_loss and test_loss < min_test_loss:
                    best_lr = rate
                    best_dcay = dcay
                    best_reg = reg

            # record the best hyperparameters
            _run.log_scalar("learning_rate", best_lr)
            _run.log_scalar("decay", best_dcay)
            _run.log_scalar("regularization", best_reg)

    # wait a second then remove the temporary directory used for storing artifacts
    sleep(1)
    os.system('rm -r ' + save_dir)


"""
Copyright (c) 2020 Red Hat, Inc
All rights reserved.

This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.
"""
from __future__ import absolute_import

from flexmock import flexmock
import pytest

from atomic_reactor.inner import DockerBuildWorkflow
from atomic_reactor.plugin import PreBuildPluginsRunner, PluginFailedException
from atomic_reactor.plugins.pre_check_user_settings import CheckUserSettingsPlugin

from tests.constants import TEST_IMAGE
from tests.stubs import StubInsideBuilder, StubSource

def mock_dockerfile(tmpdir, labels):
    base = 'From fedora:30'
    cmd = 'CMD /bin/cowsay moo'
    extra_labels = [
        'LABEL {}'.format(label)
        for label in labels

    ]
    data = '\n'.join([base] + extra_labels + [cmd])
    tmpdir.join('Dockerfile').write(data)

def mock_workflow(tmpdir):
    workflow = DockerBuildWorkflow(
        TEST_IMAGE,
        source={"provider": "git", "uri": "asd"}
    )
    workflow.source = StubSource()
    builder = StubInsideBuilder().for_workflow(workflow)
    builder.set_df_path(str(tmpdir))
    builder.tasker = flexmock()
    workflow.builder = flexmock(builder)

    return workflow

def mock_env(tmpdir, docker_tasker, labels=(), flatpak=False):
    if not flatpak:
        # flatpak build has no Dockefile
        mock_dockerfile(tmpdir, labels)
    workflow = mock_workflow(tmpdir)
    plugin_conf = [{'name': CheckUserSettingsPlugin.key,
                    'args': {'flatpak': flatpak}}]

    runner = PreBuildPluginsRunner(docker_tasker, workflow, plugin_conf)

    return runner

class TestDockerfileChecks(object):
    """
    Test checks related to Dockerfile
    """

    @pytest.mark.parametrize('labels,expected_fail', (
        (['com.redhat.delivery.appregistry=true',
          'com.redhat.delivery.operator.bundle=true'],
         True),
        (['com.redhat.delivery.appregistry=true',
          'com.redhat.delivery.operator.bundle=false'],
         False),
        (['com.redhat.delivery.appregistry=true'], False),
        (['com.redhat.delivery.operator.bundle=true'], False),
    ))
    def test_mutual_exclusivity_of_labels(self, tmpdir, docker_tasker, labels, expected_fail):
        """Appregistry and operator.bundle labels are mutually exclusive"""
        runner = mock_env(tmpdir, docker_tasker, labels=labels)
        if expected_fail:
            with pytest.raises(PluginFailedException) as e:
                runner.run()
            assert 'only one of labels' in str(e.value)
        else:
            runner.run()

    def test_flatpak_skip_dockerfile_check(self, tmpdir, docker_tasker, caplog):
        """Flatpak builds have no user Dockerfiles, dockefile check must be skipped"""
        runner = mock_env(tmpdir, docker_tasker, flatpak=True)
        runner.run()

        assert 'Skipping Dockerfile checks' in caplog.text


# -*- coding: utf-8 -*-
"""
Created on Tue Nov  5 09:49:38 2019
scraping public steam workshop collections
@author: joshuascriven
"""

import pandas as pd
import requests
from bs4 import BeautifulSoup
import textwrap
from datetime import datetime
import re
import numpy as np

# functions

## create hyperlinks
def make_html_link(kind,ref,text, h='170', w='175', brks='N'):
    br = '
' if brks == 'Y' else ''
    if kind == 'html':
        return  '' + text + '' + br +''
    elif kind == 'img':
        return  ('')
    else:
        return 'N/A'

## break html string block into lines
def break_html_lines(nchar, x, blw=False):
    return '
'.join(textwrap.wrap(x, nchar , break_long_words=blw))

# !! set profile id for user collections
pid = 76561198863827613

user_collections = (
    'https://steamcommunity.com/profiles/{}/myworkshopfiles/'\
    '?section=collections&appid=255710&p=1&numperpage=30').format(pid)

# Get collections
results = []

r = requests.get(user_collections)

soup = BeautifulSoup(r.text, 'html.parser')

resultsRow = soup.find_all('a', {'class': 'workshopItemCollection'})

for resultRow in resultsRow:
    collection_url = resultRow.get('href')
    title = resultRow.find('div', {'class': 'workshopItemTitle'}).text

    # # Once formatted, the data are then appended to the results list
    results.append({
        'collection_url': collection_url,
        'title': title,
    })

collections = results

# Get items from collections
results = []

for collection in collections:
    r = requests.get(collection['collection_url'])

    soup = BeautifulSoup(r.text, 'html.parser')

    # resultsRow = soup.find_all('div', {'class': 'collectionItemDetails'})
    resultsRow = soup.find_all('div', {'class': 'collectionItem'})

    # ### for testing
    # resultsRow = resultsRow[0:2]
    # ###

    for resultRow in resultsRow:
        item_url = resultRow.find_all('a')[1].get('href')
        title = resultRow.find('div', {'class': 'workshopItemTitle'}).text
        image = resultRow.find('img', {'class': 'workshopItemPreviewImage'}).get('src')
        creator_profile = resultRow.find_all('a')[3].get('href')
        creator_name = resultRow.find_all('a')[3].text

        # # Once formatted, the data are then appended to the results list
        results.append({
            'item_url': item_url,
            'title': title,
            'collection_url': collection['collection_url'],
            'collection_title': collection['title'],
            'image': image,
            'creator_profile': creator_profile,
            'creator_name': creator_name
        })

items = results

# Get details from items
results = []

for item in items:
    r = requests.get(item['item_url'])
    soup = BeautifulSoup(r.text, 'html.parser')

    resultsRow = soup.find_all('div', {'class': 'requiredItemsContainer'})

    if len(resultsRow) != 0:
        # for resultRow in resultsRow:
        #     print(resultRow)
            # print("\n \n \n \n\n new")
        links = resultsRow[0].find_all('a'),
        texts = [res.text.strip() for res in links[0]],
        refs = [res.get('href') for res in links[0]],
        req_assets = [make_html_link('html', r, t, brks='Y') for r,t in zip(refs[0],texts[0])]
        req_assets = ''.join(req_assets)
    else:
        req_assets = 'None'

    resultsRow = soup.find_all('div', {'class': 'workshopTags'})

    tags = []

    for resultRow in resultsRow:
        tags = tags + [res.text.strip() for res in resultRow.find_all('a')]
        # tags = ''.join(tags)
        # tags = tags.append([res.text.strip() for res in resultRow.find_all('a')])
        # # tags = ''.join(tags)

    tags = '
'.join(tags) if type(tags) == list else 'None'
    # if type(tags) == list:
    #         tags = '
'.join(tags)
    #     else:
    #         tags = 'None'

    resultsRow = soup.find_all('div', {'class': 'detailsStatRight'})
    details = [res.text.strip() for res in resultsRow]

    resultsRow = soup.find_all('div', {'class': 'workshopItemDescription'})
    # truncate, break weblinks, and remove repeating characters and long "words"
    description = [re.sub(r'[^\w\s]|(.)(?=\1)', '', res.text.strip().replace('/',' ')) for res in resultsRow]
    words_100 =  description[0].split()[0:100]
    description = ' '.join([word for word in words_100 if len(word) <=9])

    # # Once formatted, the data are then appended to the results list
    results.append({
        'tags': tags,
        'req_assets': req_assets,
        'size': details[0],
        'date_created': details[1],
        'date_updated': (details[2] if len(details) == 3 else 'No Updates'),
        'description': description,
        'item_title': item['title'],
        'item_url': item['item_url'],
        'collection_url': item['collection_url'],
        'collection_title': item['collection_title'],
        'image': item['image'],
        'creator_profile': item['creator_profile'],
        'creator_name': item['creator_name']
    })

assets_data = pd.DataFrame(results)

# strip all non-ASCII characters
assets_data['item_title'] = assets_data['item_title'].str.encode('ascii', 'ignore').str.decode('ascii')
assets_data['summary'] = assets_data['description'].str.encode('ascii', 'ignore').str.decode('ascii')

# export to xlsx and csv
date_time = datetime.now().strftime("__%m_%d_%Y_%H")
assets_data.to_excel('assets_data_plain{}.xlsx'.format(date_time))
assets_data.to_csv('assets_data_plain{}.csv'.format(date_time))

# form data from html table
assets_data['item'] = assets_data["item_title"].apply(
    lambda x: break_html_lines(x=x, blw=True, nchar=20).replace('"',''))
assets_data['summary'] = assets_data["summary"].apply(
    lambda x: break_html_lines(x=x, nchar=40))
assets_data['item_name'] = assets_data.apply(
    lambda x: make_html_link('html', x['item_url'], x['item']), axis=1)
assets_data['collection_title'] = assets_data['collection_title'].apply(
    lambda x: break_html_lines(x=x, nchar=20))
assets_data['collection'] = assets_data.apply(
    lambda x: make_html_link('html', x['collection_url'], x['collection_title']), axis=1)
assets_data['creator'] = assets_data.apply(
    lambda x: make_html_link('html', x['creator_profile'], x['creator_name']), axis=1)
assets_data['preview'] = assets_data.apply(
    lambda x: make_html_link('img', x['image'], x['item']), axis=1)
assets_data['size_mb'] = assets_data['size'].str.extract(r'(?P<size_mb>\d+.\d+)', expand=False).astype(float)
assets_data['tags__source'] = assets_data['tags'] + ['
'] + assets_data['collection']
assets_data['id'] = assets_data['item_name'].str.extract(r'(?P<id>\d+)', expand=False)
assets_data["info"] =  assets_data["item_name"] + ["
"] + assets_data["id"] + ["

"] + assets_data["creator"] + ["

"] + assets_data["size"] + ["
"] + assets_data["date_created"] + ["
"] + assets_data["date_updated"]

# final sortable
assets_data = assets_data.sort_values('size', ascending=True).reset_index(drop=True)

# # export to excel for annotations script
# data_noting = assets_data[""]
# e = data_noting['info'].str.extract(r'(?P<id>\d+)', expand=True)
# data_noting = pd.concat([data_noting, e], axis=1)
# header_list = ['notes_placement','notes_other'] + list(data_noting.columns)
# data_noting = data_noting.reindex(columns = header_list)
# data_noting.to_excel('assets_data_noting.xlsx')

# annotating table

## import/create and update notes
header_list = ['notes_placement','notes_other','id','item_title']
try:
    data_notes = pd.read_csv("assets_data_notes.csv")
    data_notes = data_notes[header_list]
except Exception:
    data_notes = assets_data.drop_duplicates(['id']).reindex(columns=header_list)
    data_notes = data_notes.astype({'id': 'int', 'notes_placement': 'object', 'notes_other': 'object'})
    pass

assets_data = assets_data.reindex(columns=list(assets_data.columns) + ['notes_placement','notes_other'])
assets_data = assets_data.astype({'id': 'int', 'notes_placement': 'object', 'notes_other': 'object'})
assets_data.dtypes

# data_notes['id'] = data_notes['id'].astype(int)
# data_notes['notes_placement'] = data_notes['notes_placement'].astype(int)

assets = assets_data['id']

for asset in assets:
    entry1 = data_notes[data_notes['id']==asset]['notes_placement'].iloc[0]
    entry2 = data_notes[data_notes['id']==asset]['notes_other'].iloc[0]
    # if entry1.notna().any()l
    assets_data['notes_placement'][assets_data['id']==asset] = str(entry1)
    assets_data['notes_other'][assets_data['id']==asset] = str(entry2)

assets_data['notes_placement'][assets_data['notes_placement']=='nan'] = ''
assets_data['notes_other'][assets_data['notes_other']=='nan'] = ''

updated_notes = assets_data[header_list].drop_duplicates(['id'])
updated_notes.to_csv('assets_data_notes.csv')

# # test
# # asset = 1692704033
# # data_notes[data_notes['id']==asset]['notes_placement']
# # assets_data['notes_placement'][assets_data['id']==asset]

# str(data_notes['id'][4]) in list(assets_data['id'])

# str(data_notes['id'][4])

# asset in list(data_notes['id'].astype(str))

# data_notes

# # testing

# dups = list(data_notes['id'][data_notes['id'].duplicated()].sort_values())

# data_notes[data_notes['id']==dups[1]] =

# data_notes['id'] = data_notes['id'].astype(int)
# dtest = data_notes['id'].sort_values()

# data_notes['id'].shape
# type(data_notes['id'][0])

# sum(data_notes['id'].duplicated())+len(data_notes['id'].unique())

# data_test = pd.read_csv("assets_data_notes_newscrapes.csv")
# []

# data_test['copy'] = [1 if (i in list(data_notes.id)) else np.nan for i in data_test.id]

# 1 if (data_test.id[1] in list(data_notes.id)) else np.nan

# [1 if (i in data_notes.id) else np.nan for i in data_test.id]

# df1 = data_test.join(data_notes, on='id', how='left', lsuffix='', rsuffix='_new')
# df2 = data_test.join(data_notes, on='id', how='right', lsuffix='_new', rsuffix='')

# df1[['notes_placement','notes_other']] = df2[['notes_placement','notes_other']]

# df1

## testing end
data_notes.to_csv('assets_data_notes.csv')

# Fancy filterable/sortable HTML table
# !! for this bit of code, you will need to download CSVtoTable
# (a "Simple command-line utility to convert CSV files to searchable and sortable HTML table")
# from https://github.com/vividvilla/csvtotable
# Installation: pip install --upgrade csvtotable

keep = ['preview', 'info', 'tags__source' , 'summary', 'req_assets','notes_placement','notes_other']
assets_data2 = assets_data
assets_data2 = assets_data2[keep]
# assets_data2 = assets_data2

import os
assets_data2.to_csv('assets_data.csv')
myCmd = ('csvtotable assets_data.csv assets_data_sort{}.html'\
    ' --caption "Cities:Skylines Wish List" --overwrite').format(date_time)
os.system(myCmd)


import os
try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

# Utility function to read the README file.
# Used for the long_description.  It's nice, because now 1) we have a top level
# README file and 2) it's easier to type in the README file than to put a raw
# string in below ...
def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

config = {
    'description': 'My Project',
    'author': 'Jose León Barranco (@josejalen)',
    'url': 'URL to get it at',
    'download_url': 'Where to download it',
    'license': 'BSD',
    'keywords': 'example documentation tutorial',
    'author_email': 'joseleon.engineer@gmail.com',
    'version': '0.1',
    'install_requires': ['nose'],
    'packages': ['NAME'],
    'scripts': [],
    'name': 'project name'
}

setup(**config)


from PIL import Image
import os

def rotateImages():
    for image in os.listdir(os.getcwd()):
        if (image.endswith(".jpg")):
            img = Image.open(image)
            img.transpose(Image.FLIP_LEFT_RIGHT).save(image)
            img.close()

if __name__ == '__main__':
    rotateImages()


#coding: utf-8
from __future__ import absolute_import

import StringIO
from os import fdopen, remove
from tempfile import mkstemp
from zipfile import ZipFile

from django.contrib.contenttypes.models import ContentType
from django.db.models import get_model
from django.http import Http404, HttpResponse
from django.utils.encoding import smart_str
from django.db import connection
from django.contrib.auth.decorators import login_required

from invoices import settings
from seautils.views.decorators import render_with, json_response, render_with_formats
from apps.documents.views import PDF_DOCUMENT_CONFIG, PDF_PREVIEW_DOCUMENT_CONFIG,\
        renderer_document_pdf

from ..invoices.forms import InvoiceItemFormset, INVOICE_TYPES_FORMS
from ..invoices.models import InvoiceItem, Invoice, VatInvoice, ProformaInvoice, INVOICE_TYPES

STATUS_OK = 'ok'
STATUS_ERROR = 'error'
STATUS_KEY = 'status'

@login_required
@render_with('office/index.html')
def index(request):
    return {}

@login_required
@render_with('office/invoice_form.html')
def render_form(request, invoice_type, invoice_id=None):
    invoice_type = int(invoice_type)
    if not invoice_id:
        invoice_form = INVOICE_TYPES_FORMS[invoice_type]()
        invoice_item_formset = InvoiceItemFormset()
    else:
        instance = INVOICE_TYPES[invoice_type].objects.get(id=invoice_id)
        invoice_form = INVOICE_TYPES_FORMS[invoice_type](instance=instance)
        invoice_item_formset = InvoiceItemFormset(instance=instance)

    return {'inv_f': invoice_form, 'inv_formset': invoice_item_formset}

@login_required
@json_response
def save_form(request, invoice_type, invoice_id=None):
    if not request.method == 'POST':
        raise Http404
    invoice_type = int(invoice_type)
    resp_dat = {}
    dat = request.POST.copy()
    if invoice_id:
        instance = INVOICE_TYPES[invoice_type].objects.get(id=invoice_id)
        invoice_form = INVOICE_TYPES_FORMS[invoice_type](dat, instance=instance)
        invoice_item_formset = InvoiceItemFormset(dat, instance=instance)
    else:
        invoice_form = INVOICE_TYPES_FORMS[invoice_type](dat)
        invoice_item_formset = InvoiceItemFormset(dat)

    errors = False
    resp_dat['main_form_info'] = {}
    if 'key' in dat and not invoice_form._meta.model.validate_key(dat['key'], invoice_id):
        resp_dat['key'] = invoice_form._meta.model.generate_next_key()
        dat['key'] = resp_dat['key']
        resp_dat['main_form_info']['key'] = (u'Numer niepoprawny lub zajęty. '\
                u'Wygenerowano nowy numer faktury',)
        errors = True
    elif 'key' not in dat:
        resp_dat['key'] = invoice_form._meta.model.generate_next_key()
        dat['key'] = resp_dat['key']

    if not invoice_form.is_valid():
        resp_dat['main_form_errors'] = dict(invoice_form.errors)
        errors = True
    if not invoice_item_formset.is_valid():
        resp_dat['items_form_errors'] = invoice_item_formset.errors
        errors = True

    if not errors:
        new_invoice = invoice_form.save(commit=False)
        new_invoice.status = new_invoice.DEFAULT_STATUS
        new_invoice.save()

        InvoiceItemFormset(dat, instance=new_invoice).save()
        resp_dat[STATUS_KEY] = STATUS_OK
        resp_dat['id'] = new_invoice.id
    else:
        resp_dat[STATUS_KEY] = STATUS_ERROR

    return resp_dat

@json_response
def prof_into_vat(request, inv_id):
    inv = ProformaInvoice.objects.get(id=inv_id)
    inv_v = inv.clone_as_vat()
    return {'status': 'OK', 'id': inv_v.id, 'key': inv_v.key}

def _get_customer_choices(ct_id):
    ct = ContentType.objects.get(id=ct_id)
    m_cls = ct.model_class()
    return [(m.id, '#%d %s' % (m.id, unicode(m))) for m in m_cls.objects.all()]

@json_response
def get_choices(request, ct_id):
    return _get_customer_choices(ct_id)

@json_response
def get_customer_data(request, ct_id, customer_id):
    ct = ContentType.objects.get(id=ct_id)
    m_cls = ct.model_class()
    customer = m_cls.objects.get(id=customer_id)
    return {'customer_data': customer.get_data()}

@json_response
def get_customer_invoice_data(request, ct_id, customer_id):
    ct = ContentType.objects.get(id=ct_id)
    m_cls = ct.model_class()
    customer = m_cls.objects.get(id=customer_id)
    return {'customer_data': customer.get_invoice_data()}

@json_response
def products_search(request):
    if request.GET.get('p_ct') and request.GET.get('p_id'):
        ct = ContentType.objects.get(id=request.GET['p_ct'])
        m_cls = ct.model_class()
        product = m_cls.objects.get(id=request.GET['p_id'])
        return product.get_autocomplete_dict(request)
    items = []
    for model in settings.PRODUCT_MODELS:
        model_cls = get_model(*model.split('.'))
        if hasattr(model_cls, 'get_for_autocomplete'):
            items += model_cls.get_for_autocomplete(request, request.GET['term'])
    items += InvoiceItem.get_for_autocomplete(request, request.GET['term'])
    return items

@json_response
def invoice_additional_info(request, invoice_ids):
    """ gross_price and others
    """
    try:
        invoice_ids_int = [int(a) for a in invoice_ids.split(',')]
    except ValueError:
        raise Http404('Incorrect ids')
    cursor = connection.cursor()
    tab = InvoiceItem._meta.db_table
    cursor.execute("SELECT SUM((100 + %(tab)s.tax) * %(tab)s.net_price), invoice_id  from %(tab)s"\
            " WHERE invoice_id in (%(ids)s) GROUP BY invoice_id" % {'tab': tab,
                'ids': ','.join(map(str, invoice_ids_int))})
    res = cursor.fetchall()

    info = {inv_id: {'gross_price': val / 100} for val, inv_id in res}
    for invoice in Invoice.objects.filter(id__in=invoice_ids_int):
        if not invoice.id in info:
            info[invoice.id] = {'gross_price': 0}
        key = info[invoice.id]
        key['get_currency_display'] = invoice.get_currency_display()
        key['customer_invoice_data'] = invoice.customer.get_invoice_data()
        key['get_payment_type_display'] = invoice.get_payment_type_display()
        key['get_status_display'] = invoice.get_status_display()
        key['customer_name'] = invoice.customer.name

    return info

@json_response
def total_sum_by_status(request, status):
    cursor = connection.cursor()
    status = int(status)
    tab = InvoiceItem._meta.db_table
    tab_inv = Invoice._meta.db_table
    res = cursor.execute("SELECT SUM((100 + %(tab)s.tax) * %(tab)s.net_price)  from %(tab)s"\
            " LEFT JOIN %(tab_inv)s ON \"%(tab_inv)s\".id=\"%(tab)s\".\"invoice_id\""\
            "WHERE status=%(status)d" \
            % {'tab': tab, 'tab_inv': tab_inv, 'status': status}).fetchall()
    return res[0][0]/100

@json_response
def total_sum_by_type(request, inv_type):
    cursor = connection.cursor()
    inv_type = int(inv_type)
    tab = InvoiceItem._meta.db_table
    inv_cls = INVOICE_TYPES[inv_type]
    tab_inv_t = inv_cls._meta.db_table
    res = cursor.execute('SELECT SUM((100 + %(tab)s.tax) * %(tab)s.net_price) from %(tab)s'\
            ' INNER JOIN %(tab_inv_t)s ON "%(tab_inv_t)s".invoice_ptr_id="%(tab)s"."invoice_id"'\
            % {'tab': tab, 'tab_inv_t': tab_inv_t}).fetchall()
    return res[0][0]/100

def renderer_documents_zipped(request, view_output, **kwargs):
    view_output.update({'document': request.document})
    files = []
    for invoice in view_output.get('invoices', []):
        view_output['invoice'] = invoice
        invoice_resp = renderer_document_pdf(request, view_output, **kwargs)
        fd, tmppath = mkstemp(suffix='.pdf')
        with fdopen(fd, 'wt') as f:
            f.write(smart_str(invoice_resp))
        files.append((tmppath, '%s.pdf' % invoice.key))

    buff= StringIO.StringIO()
    with ZipFile(buff, 'w') as invzip:
        [invzip.write(f[0], f[1]) for f in files]
    [remove(f[0]) for f in files]
    response = HttpResponse(mimetype='application/zip')
    response['Content-Disposition'] = 'attachment; filename=faktury.zip'
    buff.seek(0)
    response.write(buff.read())
    return response

ZIPPED_DOCUMENTS_CONFIG = PDF_PREVIEW_DOCUMENT_CONFIG.copy()
ZIPPED_DOCUMENTS_CONFIG['renderer'] = renderer_documents_zipped

@render_with_formats(pdf=PDF_DOCUMENT_CONFIG, zip=ZIPPED_DOCUMENTS_CONFIG)
def render_invoice(request, format):
    DEFAULT_DOCUMENT_TYPES = {
        'pdf': (settings.PRINT_TYPE_ORIGINAL, settings.PRINT_TYPE_COPY),
        'zip': (settings.PRINT_TYPE_ORIGINAL,),
    }
    types = ((int(t), dict(settings.PRINT_DOCUMENT_TYPES)[int(t)]) \
                for t in request.GET.getlist('types', DEFAULT_DOCUMENT_TYPES[format]))
    output = {
        'types': types,
        'lang_tpl': 'documents/invoices/_invoice_%s.html' % request.GET.get('lang', ''),
    }
    if format == 'pdf':
        try:
            invoice = VatInvoice.objects.get(id=request.GET.get('id'))
        except VatInvoice.DoesNotExist:
            invoice = ProformaInvoice.objects.get(id=request.GET.get('id'))
        output.update({
            'invoice': invoice,
        })
    else:
        ids = request.GET.getlist('id')
        invoices = Invoice.objects.filter(id__in=ids)
        output.update({
            'invoices': invoices,
        })
    return output


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

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        ('fund', '0005_data_member_users'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='member',
            name='email',
        ),
    ]


import numpy as np

def data_average(preamble, experiment, datasets):
    for i, item in enumerate(datasets):
        item = str(item)
        if len(item) < 5:
            item = (5 -len(item))*'0' + item
        temp = np.loadtxt(preamble + item  + ' - ' + experiment + '.csv', delimiter=',')
        if i != 0:
            prev_y = temp[:,1] + prev_y
        else:
            x_values = temp[:,0]
            prev_y = temp[:,1]
    y_values = prev_y/len(datasets)
    return [x_values, y_values]

def get_data(preamble, experiment, data_set):
    data_set = str(data_set)
    if len(data_set) < 5:
        data_set = (5 -len(data_set))*'0' + data_set
    temp = np.loadtxt(preamble + data_set  + ' - ' + experiment + '.csv', delimiter=',')
    x_values = temp[:,0]
    y_values = temp[:,1]
    return [x_values, y_values]


import tkinter as tk
from tkinter import *
from UserAPP import Autocompletecombox
from UserAPP import Personnes_BE
from UserAPP import SousZone_BE
from UserAPP import Marche_BE
from UserAPP import Douar_BE
from UserAPP import Parcelle_BE
from UserAPP import TypeSpecl_BE
from UserAPP import TypeSol_BE
from UserAPP import Consistance_BE
from UserAPP import Cles
from AdminAPP import Zn2_Word_Pdf
from tkinter.messagebox import *

#----------------------------------------------------------------- PlaceHolder ---------------------------------------------------------------------------
class Placeholder_State(object):
    __slots__ = 'normal_color', 'normal_font', 'placeholder_text', 'placeholder_color', 'placeholder_font', 'with_placeholder'

def add_placeholder_to(entry, placeholder, color="grey", font=None):
    normal_color = entry.cget("fg")
    normal_font = entry.cget("font")

    if font is None:
        font = normal_font

    state = Placeholder_State()
    state.normal_color = normal_color
    state.normal_font = normal_font
    state.placeholder_color = color
    state.placeholder_font = font
    state.placeholder_text = placeholder
    state.with_placeholder = True

    def on_focusin(event, entry=entry, state=state):
        if state.with_placeholder:
            entry.delete(0, "end")
            entry.config(fg=state.normal_color, font=state.normal_font)

            state.with_placeholder = False

    def on_focusout(event, entry=entry, state=state):
        if entry.get() == '':
            entry.insert(0, state.placeholder_text)
            entry.config(fg=state.placeholder_color, font=state.placeholder_font)

            state.with_placeholder = True

    entry.insert(0, placeholder)
    entry.config(fg=color, font=font)

    entry.bind('<FocusIn>', on_focusin, add="+")
    entry.bind('<FocusOut>', on_focusout, add="+")

    entry.placeholder_state = state

    return state
class Zn2(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title("ZN2")
        self.geometry("450x240+430+180")
        self.resizable(0, 0)
        self.config(bg="#F7F9F9")

        frmLab = LabelFrame(self, text="ZN2", font=("Times New Roman", 14), bg="#F7F9F9")
        frmLab.place(x=10, y=5, width=430, height=225)

        idPolyg = tk.Label(frmLab, text="Id parcelle", fg="#3B3F42", bg="#F7F9F9", font=("Helvetica", 12))
        idPolyg.place(x=38, y=25)
        frm = Frame(frmLab, bg="#CEE6F3", width=240, height=24)
        frm.place(x=150, y=25)
        self.idEnt = tk.Entry(frm, width=26, relief='flat', bg="#F7F9F9", font=("Helvetica", 12))
        self.idEnt.place(x=1, y=1)
        add_placeholder_to(self.idEnt, 'Id Parcelle')

        idPolyg = tk.Label(frmLab, text="Format", fg="#3B3F42", bg="#F7F9F9", font=("Helvetica", 12))
        idPolyg.place(x=48, y=75)
        vfVal = ['WORD', 'PDF']
        fv_var = StringVar()
        frm2 = Frame(frmLab, bg="#CEE6F3", width=240, height=26)
        frm2.place(x=150, y=75)
        self.fv_box = Autocompletecombox.Autocompletecombox(frm2, textvariable=fv_var, width=24, font=("Helvetica", 12))
        self.fv_box.set_completion_list(vfVal)
        self.fv_box.place(x=1, y=1)

        btn = Button(frmLab, text="Générer", font=("Times New Roman", 11), bg="#4EB1FA", fg="#FFFFFF", relief=FLAT, activebackground="#4EB1FA", activeforeground="#000000", command=self.generate)
        btn.place(x=200, y=140)

    def generate(self):
        if self.idEnt.get() != '' and self.fv_box.get()!= '':
            self.polyg = Parcelle_BE.Parcelle.researchPolyg(self, self.idEnt.get())
            idval = Parcelle_BE.Parcelle.verifyPolyg(self, self.idEnt.get())
            if idval == True:
                self.person = Personnes_BE.Personnes.prsm_xml(self, self.idEnt.get())
                self.sz = SousZone_BE.SousZ.selectSZ(self, self.idEnt.get())
                self.mrch = Marche_BE.Marche.info_marche(self)
                self.dr = Douar_BE.Douar.xmlDr(self, self.idEnt.get())
                self.tpSol = TypeSol_BE.TypeSol.sol_xml(self, self.idEnt.get())
                self.tpSpecl = TypeSpecl_BE.TypeSpecul.spec_xml(self, self.idEnt.get())
                self.consist = Consistance_BE.Consistance.Const_xml(self, self.idEnt.get())
                self.oppos = Cles.Cles.opposition_pieces(self, self.idEnt.get())
                if self.fv_box.get() == 'WORD':
                    self.word()
                elif self.fv_box.get() == 'PDF':
                    self.pdf()
            else:
                showerror("ZN2", "Id parcelle est invalide")
        else:
            showerror("ZN2", "Veuillez remplir tous les champs ")

    def word(self):
        person1 = str(self.person[0][1])
        person3 = str(self.person[0][3])
        mrch1 = str(self.mrch[1])
        sz = str(self.sz[0][1])
        mrch0 = str(self.mrch[0])
        mrch2 = str(self.mrch[2])
        person5 = str(self.person[0][5])
        person7 = str(self.person[0][7])
        person8 = str(self.person[0][8])
        dr = str(self.dr[0][1])
        polyg0 = str(self.polyg[0])
        polyg1 = str(self.polyg[1])
        oppos1 = str(self.oppos[0])
        consis1 = str(self.consist[0][1])
        specul1 = str(self.tpSpecl[0][1])
        sol1 = str(self.tpSol[0][1])
        surf27 = str(self.polyg[5])
        Zn2_Word_Pdf.ZN2.word(self, person1, person3, mrch1, sz, mrch0, mrch2, person5, person7, person8, dr, polyg0, polyg1, oppos1, consis1, specul1, sol1, surf27)

    def pdf(self):
        person1 = str(self.person[0][1])
        person3 = str(self.person[0][3])
        mrch1 = str(self.mrch[1])
        sz = str(self.sz[0][1])
        mrch0 = str(self.mrch[0])
        mrch2 = str(self.mrch[2])
        person5 = str(self.person[0][5])
        person7 = str(self.person[0][7])
        person8 = str(self.person[0][8])
        dr = str(self.dr[0][1])
        polyg0 = str(self.polyg[0])
        polyg1 = str(self.polyg[1])
        oppos1 = str(self.oppos[0])
        consis1 = str(self.consist[0][1])
        specul1 = str(self.tpSpecl[0][1])
        sol1 = str(self.tpSol[0][1])
        surf27 = str(self.polyg[5])
        Zn2_Word_Pdf.ZN2.word_to_pdf(self, person1, person3, mrch1, sz, mrch0, mrch2, person5, person7, person8, dr, polyg0, polyg1, oppos1, consis1, specul1, sol1, surf27)

'''if __name__ == "__main__":
    app = Zn2()
    app.mainloop()'''


# -*- coding: utf-8 -*-
"""
Created on Mon Feb 10 08:44:01 2020

@author: cpjohnson
"""

import numpy as np

xdata = np.array([1.0, 2.0, 3.0, 4.0])      # This is the x data
N = len(xdata)
n = N - 1

ydata = np.zeros(N)                         # Initialize y data as zeros

for k in range(0, N):                       # This loop creates y data using Ln(x)
    ydata[k] = np.log(xdata[k])

h = (xdata[n] - xdata[0]) / n               # h is the step size
print('h is {}'.format(h))
p = ydata[0]/2
for k in range(0, n):
    p += ydata[k]
p += ydata[n] / 2
p = h*p
print(p)


import numpy as np
import csv
from sklearn.datasets import fetch_20newsgroups
twenty_train=fetch_20newsgroups(subset='train')
emission_probability=np.array([[0.4,0.3,0.3]#晴的隐藏状态：0.4打球，0.3读书，0.3访友
                                  ,[0.2,0.3,0.5],#阴的隐藏状态是：0.2打球，0.3读书，0.5访友
                               [0.1,0.8,0.1]])#雨的隐藏状态是：0.1打球，0.8读书，0.1访友

transition_probabilty=np.array([[0.7,0.2,0.1],#晴天：晴0.6，阴0.2，雨0.1
                                [0.3,0.5,0.2],#阴天：晴0.3，阴0.5，雨0.2
                                [0.3,0.4,0.3]])#雨天：晴0.3，阴0.4，雨0.3

start_probability=np.array([0.5,0.2,0.3])
from hmmlearn import hmm
model=hmm.MultinomialHMM(n_components=3)
model.startprob_=start_probability
model.transmat_=transition_probabilty
model.emissionprob_=emission_probability
dic=[]
dic.append('晴天')
dic.append('阴天')
dic.append('雨天')
observer=[]
observer.append('打球')
observer.append('读书')
observer.append('访友')

observerstr=[]
predictstr=[]
observe_chain=np.array([0,2,1,1,1,1,1,1,2,2,2,2,2,2,2,2,1,0]).reshape(-1,1)
predict=model.predict(observe_chain)
print(predict)
temp=[0,2,1,1,1,1,1,1,2,2,2,2,2,2,2,2,1,0]
for i in list(predict):
    predictstr.append(dic[i])
    # predictstr.append(dic[j])
for i in temp:
    observerstr.append(observer[i])
    # predictstr.append(dic[j])
print('显示层：',observerstr)
print('预测层：',predictstr)
print(model.score(observe_chain))

scikit练习/隐马尔科夫模型预测儿子生活规律.py

隐马尔科夫模型预测儿子生活规律.py


"""A module of auxiliary helper functions, not capabilities."""

import neo
from elephant.spike_train_generation import threshold_detection
from quantities import mV, ms

# Membrane potential trace (1D numpy array) to matrix of spike snippets (2D numpy array)
def get_spike_waveforms(vm, threshold=0.0*mV, width=5*ms):
	"""
	 vm: a neo.core.AnalogSignal corresponding to a membrane potential trace.
	 threshold: the value (in mV) above which vm has to cross for there
	 			to be a spike.  Scalar float.
	 width: the length (in ms) of the snippet extracted,
	 		centered at the spike peak.

	Returns:
	 a neo.core.AnalogSignalArray of membrane potential snippets
	 corresponding to each spike.
	"""
	spike_train = threshold_detection(vm,threshold=threshold)
	vm_array = neo.core.AnalogSignalArray(vm,units=vm.units,
											 sampling_rate=vm.sampling_rate)
	snippets = [vm_array.time_slice(t-width/2,t+width/2) for t in spikes]
	return neo.core.AnalogSignalArray(snippets,units=vm.units,
											   sampling_rate=vm.sampling_rate)

def get_spike_amplitudes(spike_waveforms):
	"""
	IN:
	 spikes: Spike waveforms, e.g. from get_spike_waveforms().
	 		 neo.core.AnalogSignalArray
	OUT:
	 1D numpy array of spike amplitudes, i.e. the maxima in each waveform.
	"""
	return np.max(spikes,axis=1)

def spikes2widths(spikes, scale=[0.1*ms,1*mV]):
	"""
	IN:
	 spikes: Spike waveforms, e.g. from vm2spikes(). 2D numpy array, see vm2spikes output.
	 scale[0]: the duration of time (in s) corresponding to one sample (point), i.e. dt. Scalar float.
	 scale[1]: the scale (in mV) of the vm array, i.e. vm=3 corresponds to 3*scale[1] mV.  Scalar float.
	OUT:
	 1D numpy array of spike widths, specifically the full width at half the maximum amplitude.
	"""
	widths = []
	print("There are %d spikes" % len(spikes))
	for spike in spikes:
		#print("This spikes has duration %d samples" % len(spike))
		x_high = np.argmax(spike)
		print("Spikes has duration %d samples, and sample %d is the high point" % (len(spike),x_high))
		high = spike[x_high]
		if x_high > 0:
			low = np.min(spike[:x_high])
			mid = (high+low)/2
			spike_top = spike[(spike>mid)]
			widths.append(len(spike_top))
			print(low,mid,high)
	widths = np.array(widths)*scale[0]
	print("Spike widths are %s" % str(widths))
	return widths

# https://gist.github.com/endolith/250860
def peakdet(v, delta, x = None):
	"""
	(maxtab, mintab) = peakdet(v, delta) finds the local maxima and minima ("peaks")
	in the vector V. maxtab and mintab consists of two columns.
	Column 1 contains indices in v, and column 2 the found values.

	With [maxtab, mintab] = peakdet(v, delta, x) the indices
	in maxtab and mintab are replaced with the corresponding x-values.

	A point is considered a maximum peak if it has the maximal
	value, and was preceded (to the left) by a value lower by delta.
	"""
	maxtab = []
	mintab = []

	if x is None:
		x = np.arange(len(v))

	v = np.asarray(v)

	if len(v) != len(x):
		sys.exit('Input vectors v and x must have same length')

	if not np.isscalar(delta):
		sys.exit('Input argument delta must be a scalar')

	if delta <= 0:
		pass#sys.exit('Input argument delta must be positive')

	mn, mx = np.inf, -np.inf
	mnpos, mxpos = np.nan, np.nan

	lookformax = True

	for i in np.arange(len(v)):
		this = v[i]
		if this > mx:
			mx = this
			mxpos = x[i]
		if this < mn:
			mn = this
			mnpos = x[i]

		if lookformax:
			if this < mx-delta:
				maxtab.append((mxpos, mx))
				mn = this
				mnpos = x[i]
				lookformax = False
		else:
			if this > mn+delta:
				mintab.append((mnpos, mn))
				mx = this
				mxpos = x[i]
				lookformax = True

	return np.array(maxtab), np.array(mintab)


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

"""CNN encoder."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import math
import tensorflow as tf

class CNN_Encoder(object):
    """CNN encoder.
       This implementation is based on
           https://arxiv.org/abs/1701.02720.
               Zhang, Ying, et al.
               "Towards end-to-end speech recognition with deep convolutional
                neural networks."
               arXiv preprint arXiv:1701.02720 (2017).
    Args:
        input_size (int): the dimensions of input vectors
        splice (int): frames to splice. Default is 1 frame.
        num_classes (int): the number of classes of target labels
        parameter_init (float, optional): Range of uniform distribution to
            initialize weight parameters
        name (string, optional): the name of encoder
    """

    def __init__(self,
                 input_size,
                 splice,
                 num_classes,
                 parameter_init=0.1,
                 name='cnn_encoder'):

        self.input_size = input_size
        self.splice = splice
        self.num_classes = num_classes
        self.parameter_init = parameter_init
        self.name = name

    def __call__(self, inputs, inputs_seq_len,
                 keep_prob_hidden, keep_prob_input, keep_prob_output):
        """Construct model graph.
        Args:
            inputs (placeholder): A tensor of size`[B, T, input_size]`
            inputs_seq_len (placeholder): A tensor of size` [B]`
            keep_prob_input (placeholder, float): A probability to keep nodes
                in the input-hidden connection
            keep_prob_hidden (placeholder, float): A probability to keep nodes
                in the hidden-hidden connection
            keep_prob_output (placeholder, float): A probability to keep nodes
                in the hidden-output connection
        Returns:
            logits: A tensor of size `[T, B, num_classes]`
            final_state: A final hidden state of the encoder
        """
        # TODO: input dropout is not performed

        # inputs: 3D `[batch_size, max_time, input_size * splice]`
        batch_size = tf.shape(inputs)[0]
        max_time = tf.shape(inputs)[1]

        # Reshape to 4D tensor `[batch_size, max_time, input_size, splice]`
        # inputs = tf.reshape(
        #     inputs,
        #     shape=[batch_size,
        #                    max_time,
        #                    self.input_size,
        #                    self.splice])

        # Reshape to 5D tensor
        # `[batch_size, max_time, input_size / 3, 3 (+Δ,ΔΔ), splice]`
        # inputs = tf.reshape(
        #     inputs, shape=[batch_size,
        #                    max_time,
        #                    int(self.input_size / 3),
        #                    3,
        #                    self.splice])

        # Reshape to 4D tensor
        # `[batch_size * max_time, input_size / 3, splice, 3]`
        # inputs = tf.transpose(inputs, (0, 1, 2, 4, 3))
        inputs = tf.reshape(
            inputs,
            shape=[batch_size * max_time, int(self.input_size / 3), self.splice, 3])

        # Choose the activation function
        activation = 'relu'
        # activation = 'prelu'
        # activation = 'maxout'
        # TODO: add prelu and maxout layers

        # 1-4 layers
        with tf.variable_scope('conv128'):
            for i_layer in range(1, 5, 1):
                if i_layer == 1:
                    outputs = self._conv_layer(inputs,
                                               filter_shape=[3, 5, 3, 128],
                                               name='conv%d' % i_layer)
                    outputs = self._activation(outputs, layer=activation)
                    outputs = self._max_pool(outputs, name='pool')
                else:
                    # no poling
                    outputs = self._conv_layer(outputs,
                                               filter_shape=[3, 5, 128, 128],
                                               name='conv%d' % i_layer)
                    outputs = self._activation(outputs, layer=activation)

                # Dropout
                outputs = tf.nn.dropout(outputs, keep_prob_hidden)
                # TODO: try Weight decay
                # TODO: try batch normalization

        # 5-10 layers
        with tf.variable_scope('conv256'):
            for i_layer in range(5, 11, 1):
                if i_layer == 5:
                    outputs = self._conv_layer(outputs,
                                               filter_shape=[3, 5, 128, 256],
                                               name='conv%d' % i_layer)
                else:
                    outputs = self._conv_layer(outputs,
                                               filter_shape=[3, 5, 256, 256],
                                               name='conv%d' % i_layer)

                outputs = self._activation(outputs, layer=activation)

                # Dropout
                outputs = tf.nn.dropout(outputs, keep_prob_hidden)
                # TODO: try Weight decay
                # TODO: try batch normalization

        # Reshape to 5D tensor `[batch_size, max_time, new_h, new_w, 256]`
        new_h = math.ceil(self.input_size / 3 / 3)  # expected to be 14
        new_w = self.splice  # expected to be 11
        # outputs = tf.reshape(
        #     outputs, shape=[batch_size, max_time, new_h, new_w, 256])

        # Reshape to 3D tensor `[batch_size, max_time, new_h * new_w * 256]`
        # outputs = tf.reshape(
        #     outputs, shape=[batch_size, max_time, new_h * new_w * 256])

        # Reshape to 2D tensor `[batch_size * max_time, new_h * new_w * 256]`
        outputs = tf.reshape(
            outputs, shape=[batch_size * max_time, new_h * new_w * 256])

        # 11-13th fc
        with tf.variable_scope('fc'):
            for i_layer in range(11, 14, 1):
                num_outputs = 1024 if i_layer != 13 else self.num_classes
                outputs = tf.contrib.layers.fully_connected(
                    inputs=outputs,
                    num_outputs=num_outputs,
                    activation_fn=tf.nn.relu,
                    scope='fc%d' % i_layer)

                if i_layer != 13:
                    # Dropout
                    outputs = tf.nn.dropout(outputs, keep_prob_hidden)
                # TODO: try Weight decay
                # TODO: try batch normalization

        # Reshape back to 3D tensor `[batch_size, max_time, num_classes]`
        logits = tf.reshape(
            outputs, shape=[batch_size, max_time, self.num_classes])

        # Convert to time-major: `[max_time, batch_size, num_classes]'
        logits = tf.transpose(logits, (1, 0, 2))

        return logits, None

    def _max_pool(self, bottom, name='max_pool'):
        """A max pooling layer.
        Args:
            bottom: A tensor of size `[B * T, H, W, C]`
            name: A layer name
        Returns:
            A tensor of size `[B * T, H / 3, W, C]`
        """
        return tf.nn.max_pool(
            bottom,
            ksize=[1, 3, 1, 1],
            strides=[1, 3, 1, 1],
            padding='SAME', name=name)

    def _conv_layer(self, bottom, filter_shape, name):
        """A convolutional layer
        Args:
            bottom: A tensor of size `[B * T, H, W, C]`
            filter_shape: A list of
                `[height, width, input_channel, output_channel]`
            name: A layer name
        Returns:
            outputs: A tensor of size `[B * T, H, W, output_channel]`
        """
        with tf.variable_scope(name):
            W = tf.Variable(tf.truncated_normal(shape=filter_shape,
                                                stddev=self.parameter_init),
                            name='weight')
            b = tf.Variable(tf.zeros(shape=filter_shape[-1]),
                            name='bias')
            conv_bottom = tf.nn.conv2d(bottom, W,
                                       strides=[1, 1, 1, 1],
                                       padding='SAME')
            return tf.nn.bias_add(conv_bottom, b)
            # NOTE: not performe activation

    def _activation(self, bottom, layer):
        """An activation layer.
        Args:
            bottom: A tensor of size `[B * T, H, W, C]`
            layer: relu or prelu or maxout
        Returns:
            outputs: A tensor of size `[B * T, H, W, C]`
        """
        if layer not in ['relu', 'prelu', 'maxout']:
            raise ValueError("layer is 'relu' or 'prelu' or 'maxout'.")

        if layer == 'relu':
            return tf.nn.relu(bottom)
        elif layer == 'prelu':
            raise NotImplementedError
        elif layer == 'maxout':
            raise NotImplementedError


# 注：cityscapes里面所有的图像都是1024×2048的24位RGB彩色图像
# 因此Resize实际上等效于简单的随机均匀按比例缩放，最小512×1024，最大2048×4096

# dataset settings
dataset_type = 'CityscapesDataset'
data_root = 'data/cityscapes/'

# 将图片除以255
img_norm_cfg = dict(mean=[0, 0, 0], std=[255, 255, 255], to_rgb=True)
crop_size = (512, 1024)
train_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='LoadAnnotations'),
    dict(type='Resize', img_scale=(2048, 1024), ratio_range=(0.5, 2.0)),
    dict(type='RandomCrop', crop_size=crop_size, cat_max_ratio=0.75),
    dict(type='RandomFlip', prob=0.5, direction='horizontal'),
    dict(type='RandomFlip', prob=0.5, direction='vertical'),
    AddTrigger,
    dict(type='Normalize', **img_norm_cfg),
    dict(type='DefaultFormatBundle'),
    dict(type='Collect', keys=['img', 'gt_semantic_seg']),
]
val_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='LoadAnnotations'),
    dict(type='Resize', img_scale=(2048, 1024), ratio_range=(0.5, 2.0)),
    dict(type='RandomCrop', crop_size=crop_size, cat_max_ratio=0.75),
    dict(type='RandomFlip', prob=0.5, direction='horizontal'),
    dict(type='RandomFlip', prob=0.5, direction='vertical'),
    dict(type='Normalize', **img_norm_cfg),
    dict(type='DefaultFormatBundle'),
    dict(type='Collect', keys=['img', 'gt_semantic_seg']),
]
test_pipeline = [
    dict(type='LoadImageFromFile'),
    AddTrigger,
    dict(
        type='MultiScaleFlipAug',
        img_scale=(2048, 1024),
        # img_ratios=[0.5, 0.75, 1.0, 1.25, 1.5, 1.75],
        flip=False,
        transforms=[
            dict(type='Resize', keep_ratio=True), # 对于cityscapes来说相当于没有缩放
            dict(type='RandomFlip'), # 因为flip=False，所以没有发生翻转
            dict(type='Normalize', **img_norm_cfg), # 将图片除以255
            dict(type='ImageToTensor', keys=['img']),
            dict(type='Collect', keys=['img']),
        ])
]
data = dict(
    samples_per_gpu=2,
    workers_per_gpu=2,
    train=dict(
        type=dataset_type,
        data_root=data_root,
        img_dir='leftImg8bit/train',
        ann_dir='gtFine/train',
        pipeline=train_pipeline),
    val=dict(
        type=dataset_type,
        data_root=data_root,
        img_dir='leftImg8bit/val',
        ann_dir='gtFine/val',
        pipeline=val_pipeline),
    test=dict(
        type=dataset_type,
        data_root=data_root,
        img_dir='leftImg8bit/val',
        ann_dir='gtFine/val',
        pipeline=test_pipeline,
        attack_info=AddTrigger))


## 1221. Split a String in Balanced Strings

# Example 1:
# Input: s = "RLRRLLRLRL"
# Output: 4
# Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'.

# Example 2:
# Input: s = "RLLLLRRRLR"
# Output: 3
# Explanation: s can be split into "RL", "LLLRRR", "LR", each substring contains same number of 'L' and 'R'.

# Example 3:
# Input: s = "LLLLRRRR"
# Output: 1
# Explanation: s can be split into "LLLLRRRR".

class Solution:
    def balancedStringSplit(self, s: str) -> int:

        count = 0
        balance = 0
        N = len(s)
        i=0
        while i<N:
            if s[i]=='L':
                balance+=1
            else:
                balance-=1

            if balance == 0:
                count+=1

            i+=1

        return count

#         print('-'*20)
#         ptr = 1
#         N = len(s)
#         result = 0

#         while ptr<N:
#             print(ptr-1, s[ptr-1], '-', ptr, s[ptr])
#             temp = 1
#             while ptr<N and s[ptr-1]==s[ptr]:
#                 temp+=1
#                 ptr+=1
#             print('PTR: ', ptr, 'TCount: ',temp)
#             result+=1
#             ptr+=temp+1
#             print('R:',result, 'NPTR:', ptr)

#         return result

        # ptr1 = 0
        # ptr2 = 1
        # N = len(s)
        # print('-'*20)
        # count = 0
        # curr = s[ptr1]
        # while ptr1<ptr2<N:
        #     print(curr, ptr1,ptr2, s[ptr2])
        #     if curr==s[ptr2]:
        #         ptr2+=1
        #     else:
        #         count+=1
        #         ptr1 = ptr2+1
        #         curr = s[ptr1]
        #         ptr2 = ptr1+1
        # return count


import hashlib
import subprocess
# from cPickle import pickle

str = "shichengtao"
h = hashlib.md5(str.encode("utf-8"))
print (h)
print (h.hexdigest())

k = hashlib.sha1(str.encode("utf-8"))
print (k.hexdigest())

subprocess.call(['ls','-l'])
subprocess.call(['pwd'])

def comstr():
    a = [2, 3, 4, 5]
    b = [3, 4, 5, 6]
    c = []
    for i in a:
        for k in b:
            if i not in b:
                c.append(i)

    return list(set(c))
print (comstr())

from Crypto.hash import SHA256
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

key = RSA.generate(1024)
pubkey = key.publickey().key
def Decrypt(prikey,data):

    try:
        cipher = PKCS1_OAEP.new(prikey,hashAlgo=SHA256)
        print (cipher.decrypt(data))
        return cipher.decrypt(data)
    except:
        # traceback.print_exc()
        return None
def Encrypt(pubkey,data):

    try:
        cipher = PKCS1_OAEP.new(pubkey,hashAlgo=SHA256)
        print (cipher.encrypt(data))
        return cipher.encrypt(data)
    except :
        # traceback.print_exc()
        return None

Encrypt()


from collections import namedtuple
import time
import requests
from requests import codes as status

from util.log import Log

Content = namedtuple('Content', 'decode')

class Error:
    def __init__(self, value):
        self.headers = value
        self.content = Content(decode=lambda _: value)
        self.json = lambda: value
        self.iter_content = lambda chunk_size: value

class Contents:
    @classmethod
    def utf8(cls, resource, params=None, headers=None, onerror=None):
        return cls.__raw(
            resource,
            params=params,
            headers=headers,
            onerror=onerror
        ).decode('utf-8')

    @classmethod
    def __raw(cls, resource, params=None, headers=None, onerror=None):
        return cls.__get_ok(
            resource,
            params=params,
            headers=headers,
            onerror=onerror
        ).content

    @classmethod
    def json(cls, resource, params=None, headers=None, onerror=None):
        return cls.__get_ok(
            resource,
            params=params,
            headers=headers,
            onerror=onerror
        ).json()

    @classmethod
    def __get_ok(cls, resource, params=None, headers=None, onerror=None):
        return cls.__check_ok(
            cls.__get(resource, params=params, headers=headers),
            onerror=onerror
        )

    @classmethod
    def headers(cls, resource):
        try:
            return cls.__check_ok(requests.head(resource)).headers
        except Exception as e:
            print("错误到了headers")
            Log.fatal(str(e))
            exit(1)

    @staticmethod
    def __get(resource, params=None, headers=None):
        try:
            r=requests.get(
                resource,
                params=params,
                headers=headers,
                stream=True,
                timeout=10
            )
        except Exception as e:
            #Log.fatal(str(e))
            #exit(1)
            print("__get出了问题\n")
            print(e)
            #exit(1)
            time.sleep(0.5)
            Contents.__get(resource,params=params,headers=headers)
        else:
            #time.sleep(0.1)
            return r

    @staticmethod
    def __check_ok(response, onerror=None):
        if response.status_code != status.ok:
            if onerror is None:
                print("错误到了check_ok的if")
                Log.fatal(
                    'Failed to get {url}: got {statusCode} response'.format(
                        url=response.url,
                        statusCode=response.status_code
                    )
                )
                exit(1)
            else:
                print("错误到了check_ok的else")
                print(response.text)
                return Error(onerror(response.status_code))
        return response

    @classmethod
    def chunked(cls, resource, onerror=None):
        return cls.__get_ok(resource, onerror=onerror).iter_content(chunk_size=2097152)


import hashlib

if __name__ == "__main__":
    santa_secret = "yzbqklnj"

    secret = 0
    while True:
        secret += 1
        m = hashlib.md5()
        m.update(santa_secret + str(secret))
        if m.hexdigest().startswith("00000"):
            break

    print("Solution: {0}".format(secret))


import sys
sys.path.append('../GitHub/Data-Structures/queue_and_stack')
from dll_queue import Queue
from dll_stack import Stack

class BinarySearchTree:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None
        self.dll_stack = Stack()
        self.dll_queue = Queue()
    # Insert the given value into the tree
    def insert(self, value):
        if self.value is None:
            self.value = value
            # return self.value
        else:
            if value < self.value:
                if self.left is None:
                    self.left = BinarySearchTree(value)
                else:
                    self.left.insert(value)
            else:
                if self.right is None:
                    self.right = BinarySearchTree(value)
                else:
                    self.right.insert(value)

    # Return True if the tree contains the value
    # False if it does not
    def contains(self, target):
        # if self.value is None:
        #     return False
        # if self.value == target:
        #     return True
        # else:
        #     if target < self.value:
        #         return self.left.contains(target)
        #     else:
        #         return self.right.contains(target)
        if target < self.value:
            if self.left is None:
                return False
            return self.left.contains(target)
        elif target > self.value:
            if self.right is None:
                return False
            return self.right.contains(target)
        else:
            return True
    # Return the maximum value found in the tree
    def get_max(self):
        if self.value == None:
            return None
        # maxvalue = self.value
        if self.right == None:
            if self.left != None:
                return self.left.get_max()
        if self.right != None:
            return self.right.get_max()
        return self.value

        # else:
        #     self.right.get_max()

    # Call the function `cb` on the value of each node
    # You may use a recursive or iterative approach
    def for_each(self, cb):
        cb(self.value)
        if self.left != None:
            self.left.for_each(cb)
        if self.right != None:
            self.right.for_each(cb)

    # DAY 2 Project -----------------------

    # Print all the values in order from low to high
    # Hint:  Use a recursive, depth first traversal
    def in_order_print(self, node=None):
        # if node == None:
        #     node = BinarySearchTree()
        # if node.value == None:
        #     return None
        # node.dll_stack.push(node.value)
        # if node.left != None:
        #     while node.left != None:
        #         node.in_order_print(node)
        #     print(node.value)
        #     node.dll_stack.pop()
        # print(node.dll_stack.tail)
        # node.dll_stack.pop()
        if node:

            node.in_order_print(node.left)

            print(node.value)

            node.in_order_print(node.right)
        # print('\n')

    # Print the value of every node, starting with the given node,
    # in an iterative breadth first traversal
    def bft_print(self, node):
        q = Queue()
        q.enqueue(self)
        while q.len() > 0:
            node_current = q.dequeue()
            print(node_current.value)
            if node_current.left:
                q.enqueue(node_current.left)
            if node_current.right:
                q.enqueue(node_current.right)

    # Print the value of every node, starting with the given node,
    # in an iterative depth first traversal
    def dft_print(self, node):
        st = Stack()
        st.push(self)
        while st.len() > 0:
            node_current = st.pop()
            print(node_current.value)
            if node_current.left:
                st.push(node_current.left)
            if node_current.right:
                st.push(node_current.right)

    # STRETCH Goals -------------------------
    # Note: Research may be required

    # Print Pre-order recursive DFT
    def pre_order_dft(self, node):
        print(self.value)
        if self.left:
            self.left.pre_order_dft(self.left)
        if self.right:
            self.right.pre_order_dft(self.right)

    # Print Post-order recursive DFT
    def post_order_dft(self, node=None):
        if self.left:
            self.left.post_order_dft()
        if self.right:
            self.right.post_order_dft()
        print(self.value)

# import sys
# sys.path.append('../GitHub/Data-Structures/binary_search_tree')
# from binary_search_tree import BinarySearchTree

# bst = BinarySearchTree(1)
# bst.insert(8)
# bst.insert(5)
# bst.insert(7)
# bst.insert(6)
# bst.insert(3)
# bst.insert(4)
# bst.insert(2)

# bst.in_order_print(bst)


import torch
import numpy as np

def tile(a, dim, n_tile, device='cuda'):
    init_dim = a.size(dim)
    repeat_idx = [1] * a.dim()
    repeat_idx[dim] = n_tile
    a = a.repeat(*(repeat_idx))
    order_index = torch.LongTensor(np.concatenate([init_dim * np.arange(n_tile) + i for i in range(init_dim)])).to(device)
    return torch.index_select(a, dim, order_index)


import requests
import os
import json
from datetime import datetime, date, time

ofs=0
top_see=6
count_list={}
words=[]
top_words={}
today=datetime.today()
r=requests.get('https://api.zp.ru/v1/vacancies?add_date='+today.strftime("%Y-%m-%d")+'&limit=100&geo_id=826')
j=r.json()['vacancies']

def count_words(kw):
    global words
    for k in range(len(kw)):
        words+=j[k]['header'].split(' ')

def cocounter(dj):
    for k in range(len(dj)):
       vacancy_name=j[k]['rubrics'][0]['title']
       if vacancy_name not in count_list:
           count_list[vacancy_name]=1
       else:
           count_list[vacancy_name]+=1

cocounter(j)
count_words(j)

while j!=[]:
    ofs+=100
    r=requests.get('https://api.zp.ru/v1/vacancies?add_date='+today.strftime("%Y-%m-%d")+'&limit=100&geo_id=826&offset='+str(ofs))
    j=r.json()['vacancies']
    cocounter(j)
    count_words(j)

for z in range (len(words)):
    if words[z] in top_words:
        top_words[words[z]]+=1
    else:
       top_words[words[z]]=1

l=lambda x:x[1]
sorw=(sorted(top_words.items(), key=l, reverse=True))

sorr=(sorted(count_list.items(), key=l, reverse=True))

print('Spisok rubrik po populyarnosti:\n')
for k in range(len(sorr)):
    print('\t'+sorr[k][0]+': '+str(sorr[k][1]))

print ('\n\n')

print('Top %d slov po chastote ispolzovaniya \n' % top_see)
for k in range(top_see):
    print('\t'+sorw[k][0]+': '+str(sorw[k][1]))


import logging

log = logging.getLogger(__name__)

SKIP_INPUT = object()

def json_wrap(inputs, input_values, as_dict=None, handle_files="skip"):
    if as_dict is None:
        as_dict = {}

    for input in inputs.values():
        input_name = input.name
        value = input_values[input_name]
        json_value = _json_wrap_input(input, value, handle_files=handle_files)
        if json_value is SKIP_INPUT:
            continue
        as_dict[input_name] = json_value
    return as_dict

def _json_wrap_input(input, value, handle_files="skip"):
    input_type = input.type

    def _data_input_to_path(v):
        path = _cast_if_not_none(v, str)
        if path == "None":
            path = None
        return path

    if input_type == "repeat":
        repeat_job_value = []
        for d in value:
            repeat_instance_job_value = {}
            json_wrap(input.inputs, d, repeat_instance_job_value)
            repeat_job_value.append(repeat_instance_job_value)
        json_value = repeat_job_value
    elif input_type == "conditional":
        values = value
        current = values["__current_case__"]
        conditional_job_value = {}
        json_wrap(input.cases[current].inputs, values, conditional_job_value)
        test_param = input.test_param
        test_param_name = test_param.name
        test_value = _json_wrap_input(test_param, values[test_param_name])
        conditional_job_value[test_param_name] = test_value
        json_value = conditional_job_value
    elif input_type == "section":
        values = value
        section_job_value = {}
        json_wrap(input.inputs, values, section_job_value)
        json_value = section_job_value
    elif input_type == "data" and input.multiple:
        if handle_files == "paths":
            json_value = list(map(_data_input_to_path, value))
        elif handle_files == "skip":
            return SKIP_INPUT
        else:
            raise NotImplementedError()
    elif input_type == "data":
        if handle_files == "paths":
            json_value = _data_input_to_path(value)
        elif handle_files == "skip":
            return SKIP_INPUT
        elif handle_files == "OBJECT":
            if value:
                if isinstance(value, list):
                    value = value[0]
                json_value = _hda_to_object(value)
                if input.load_contents:
                    with open(str(value), mode='rb') as fh:
                        json_value['contents'] = fh.read(input.load_contents).decode('utf-8', errors='replace')
                return json_value
            else:
                return None
        else:
            raise NotImplementedError()
    elif input_type == "data_collection":
        if handle_files == "skip":
            return SKIP_INPUT
        raise NotImplementedError()
    elif input_type in ["select", "text", "color", "hidden"]:
        json_value = _cast_if_not_none(value, str)
    elif input_type == "float":
        json_value = _cast_if_not_none(value, float, empty_to_none=True)
    elif input_type == "integer":
        json_value = _cast_if_not_none(value, int, empty_to_none=True)
    elif input_type == "boolean":
        json_value = _cast_if_not_none(value, bool)
    elif input_type == "data_column":
        # value is a SelectToolParameterWrapper()
        if input.multiple:
            json_value = [int(_) for _ in _cast_if_not_none(value.value, list)]
        else:
            json_value = [_cast_if_not_none(value.value, int)]
    else:
        raise NotImplementedError("input_type [%s] not implemented" % input_type)

    return json_value

def _hda_to_object(hda):
    hda_dict = hda.to_dict()
    metadata_dict = {}

    for key, value in hda_dict.items():
        if key.startswith("metadata_"):
            metadata_dict[key[len("metadata_"):]] = value

    return {
        'file_ext': hda_dict['file_ext'],
        'name': hda_dict['name'],
        'metadata': metadata_dict,
    }

def _cast_if_not_none(value, cast_to, empty_to_none=False):
    # log.debug("value [%s], type[%s]" % (value, type(value)))
    if value is None or (empty_to_none and str(value) == ''):
        return None
    else:
        return cast_to(value)

__all__ = ('json_wrap', )


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

import math

class SolarSystem:

    def __init__(self):
        self.sun = __new__(Image())
        self.moon = __new__(Image())
        self.earth = __new__(Image())
        self.sun.src = 'https://mdn.mozillademos.org/files/1456/Canvas_sun.png'
        if self.sun.naturalWidth == "undefined" or self.sun.naturalWidth == 0:
            self.sun.src ='Canvas_sun.png'
        self.moon.src = 'https://mdn.mozillademos.org/files/1443/Canvas_moon.png'
        if self.moon.naturalWidth == "undefined" or self.moon.naturalWidth == 0:
            self.moon.src ='Canvas_moon.png'
        self.earth.src = 'https://mdn.mozillademos.org/files/1429/Canvas_earth.png'
        if self.earth.naturalWidth == "undefined" or self.earth.naturalWidth == 0:
            self.earth.src ='Canvas_earth.png'
        window.requestAnimationFrame(self.render)

    def render(self):
        self.ctx = document.getElementById('canvas').getContext('2d')
        self.ctx.globalCompositeOperation = 'destination-over'
        self.ctx.clearRect(0, 0, 300, 300) # clear canvas
        self.ctx.fillStyle = 'rgba(0, 0, 0, 0.4)'
        self.ctx.strokeStyle = 'rgba(0, 153, 255, 0.4)'
        self.ctx.save()
        self.ctx.translate(150, 150)

        # Earth
        self.time = __new__(Date())
        self.ctx.rotate(((2 * math.pi) / 60) * self.time.getSeconds() + ((2 * math.pi) / 60000) * self.time.getMilliseconds())
        self.ctx.translate(105, 0)
        self.ctx.fillRect(0, -12, 50, 24) # Shadow
        self.ctx.drawImage(self.earth, -12, -12)

        # Moon
        self.ctx.save()
        self.ctx.rotate(((2 * math.pi) / 6) * self.time.getSeconds() + ((2 * math.pi) / 6000) * self.time.getMilliseconds())
        self.ctx.translate(0, 28.5)
        self.ctx.drawImage(self.moon, -3.5, -3.5)
        self.ctx.restore()

        self.ctx.restore()

        self.ctx.beginPath()
        self.ctx.arc(150, 150, 105, 0, math.pi * 2, False) # Earth orbit
        self.ctx.stroke()

        self.ctx.drawImage(self.sun, 0, 0, 300, 300)

        window.requestAnimationFrame(self.render)

solarSystem = SolarSystem()


from django import forms
from django_tutalk import models

class SelectExperimentForm(forms.Form):
    experiment = forms.ChoiceField(required=True)

    def __init__(self, *args, **kwargs):
        super(SelectExperimentForm, self).__init__(*args, **kwargs)

        exps = models.Experiment.objects\
                                .order_by("experimenter__name", "name")\
                                .all()
        choices = [(exp.id, "%s/%s" % (exp.experimenter.name, exp.name))
                   for exp in exps]
        choices.insert(0, ("", "---------"))
        self.fields["experiment"].choices = choices