import torch
import torch.utils.data
from torch import nn, optim
from torchvision import *
import socket
import os
import time
from datetime import datetime
import numpy as np
import argparse

from tensorboardX import SummaryWriter

import utils.torch.modules as modules
import utils.torch.rand as random

class Model(nn.Module):
    def __init__(self, xs=(3, 32, 32), nz=1, zchannels=16, nprocessing=1, kernel_size=3, resdepth=2,
                 reswidth=256, dropout_p=0., tag='', root_process=True):
        super().__init__()
        # default: disable compressing mode
        # if activated, tensors will be flattened
        self.compressing = False

        # hyperparameters
        self.xs = xs
        self.nz = nz
        self.zchannels = zchannels
        self.nprocessing = nprocessing
        # latent height/width is always 16,
        # the number of channels depends on the dataset
        self.zdim = (self.zchannels, 16, 16)
        self.resdepth = resdepth
        self.reswidth = reswidth
        self.kernel_size = kernel_size

        # apply these two factors (i.e. on the ELBO) in sequence and it results in "bits/dim"
        # factor to convert "nats" to bits
        self.bitsscale = np.log2(np.e)
        # factor to divide by the data dimension
        self.perdimsscale = 1. / np.prod(self.xs)

        # calculate processing layers convolutions options
        # kernel/filter is 5, so in order to ensure same-size outputs, we have to pad by 2
        padding_proc = (5 - 1) / 2
        assert padding_proc.is_integer()
        padding_proc = int(padding_proc)

        # calculate other convolutions options
        padding = (self.kernel_size - 1) / 2
        assert padding.is_integer()
        padding = int(padding)

        # create loggers
        self.tag = tag
        if root_process:
            current_time = datetime.now().strftime('%b%d_%H-%M-%S')
            log_dir = os.path.join(
                'runs/imagenetcrop/', current_time + '_' + socket.gethostname() + tag)
            self.log_dir = log_dir
            self.logger = SummaryWriter(log_dir=self.log_dir)

        # set-up current "best elbo"
        self.best_elbo = np.inf

        # distribute ResNet blocks over latent layers
        resdepth = [0] * (self.nz)
        i = 0
        for _ in range(self.resdepth):
            i = 0 if i == (self.nz) else i
            resdepth[i] += 1
            i += 1

        # reduce initial variance of distributions corresponding
        # to latent layers if latent nz increases
        scale = 1.0 / (self.nz ** 0.5)

        # activations
        self.softplus = nn.Softplus()
        self.sigmoid = nn.Sigmoid()
        self.act = nn.ELU()
        self.actresnet = nn.ELU()

        # Below we build up the main model architecture of the inference- and generative-models
        # All the architecure components are built up from different custom are existing PyTorch modules

        # <===== INFERENCE MODEL =====>
        # the bottom (zi=1) inference model
        self.infer_in = nn.Sequential(
            # shape: [1,32,32] -> [4,16,16]
            modules.Squeeze2d(factor=2),

            # shape: [4,16,16] -> [32,16,16]
            modules.WnConv2d(4 * xs[0],
                             self.reswidth,
                             5,
                             1,
                             padding_proc,
                             init_scale=1.0,
                             loggain=True),
            self.act
        )
        self.infer_res0 = nn.Sequential(
            # shape: [32,16,16] -> [32,16,16]
            modules.ResNetBlock(self.reswidth,
                                self.reswidth,
                                5,
                                1,
                                padding_proc,
                                self.nprocessing,
                                dropout_p,
                                self.actresnet),
            self.act
        ) if self.nprocessing > 0 else modules.Pass()

        self.infer_res1 = nn.Sequential(
            # shape: [32,16,16] -> [32,16,16]
            modules.ResNetBlock(self.reswidth,
                                self.reswidth,
                                self.kernel_size,
                                1,
                                padding,
                                resdepth[0],
                                dropout_p,
                                self.actresnet),
            self.act
        ) if resdepth[0] > 0 else modules.Pass()

        # shape: [32,16,16] -> [1,16,16]
        self.infer_mu = modules.WnConv2d(self.reswidth,
                                         self.zchannels,
                                         self.kernel_size,
                                         1,
                                         padding,
                                         init_scale=scale if self.nz > 1 else 2 ** 0.5 * scale)

        # shape: [32,16,16] -> [1,16,16]
        self.infer_std = modules.WnConv2d(self.reswidth,
                                          self.zchannels,
                                          self.kernel_size,
                                          1,
                                          padding,
                                          init_scale=scale if self.nz > 1 else 2 ** 0.5 * scale)

        # <===== DEEP INFERENCE MODEL =====>
        # the deeper (zi > 1) inference models
        self.deepinfer_in = nn.ModuleList([
            # shape: [1,16,16] -> [32,16,16]
            nn.Sequential(
                modules.WnConv2d(self.zchannels,
                                 self.reswidth,
                                 self.kernel_size,
                                 1,
                                 padding,
                                 init_scale=1.0,
                                 loggain=True),
                self.act
            )
            for _ in range(self.nz - 1)])

        self.deepinfer_res = nn.ModuleList([
            # shape: [32,16,16] -> [32,16,16]
            nn.Sequential(
                modules.ResNetBlock(self.reswidth,
                                    self.reswidth,
                                    self.kernel_size,
                                    1,
                                    padding,
                                    resdepth[i + 1],
                                    dropout_p,
                                    self.actresnet),
                self.act
            ) if resdepth[i + 1] > 0 else modules.Pass()
            for i in range(self.nz - 1)])

        self.deepinfer_mu = nn.ModuleList([
            # shape: [32,16,16] -> [1,16,16]
            nn.Sequential(
                modules.WnConv2d(self.reswidth,
                                 self.zchannels,
                                 self.kernel_size,
                                 1,
                                 padding,
                                 init_scale=scale if i < self.nz - 2 else 2 ** 0.5 * scale)
            )
            for i in range(self.nz - 1)])

        self.deepinfer_std = nn.ModuleList([
            # shape: [32,16,16] -> [1,16,16]
            nn.Sequential(
                modules.WnConv2d(self.reswidth,
                                 self.zchannels,
                                 self.kernel_size,
                                 1,
                                 padding,
                                 init_scale=scale if i < self.nz - 2 else 2 ** 0.5 * scale)
            )
            for i in range(self.nz - 1)])

        # <===== DEEP GENERATIVE MODEL =====>
        # the deeper (zi > 1) generative models
        self.deepgen_in = nn.ModuleList([
            # shape: [1,16,16] -> [32,16,16]
            nn.Sequential(
                modules.WnConv2d(self.zchannels,
                                 self.reswidth,
                                 self.kernel_size,
                                 1,
                                 padding,
                                 init_scale=1.0,
                                 loggain=True),
                self.act
            )
            for _ in range(self.nz - 1)])

        self.deepgen_res = nn.ModuleList([
            # shape: [32,16,16] -> [32,16,16]
            nn.Sequential(
                modules.ResNetBlock(self.reswidth,
                                    self.reswidth,
                                    self.kernel_size,
                                    1,
                                    padding,
                                    resdepth[i + 1],
                                    dropout_p,
                                    self.actresnet),
                self.act
            ) if resdepth[i + 1] > 0 else modules.Pass()
            for i in range(self.nz - 1)])

        self.deepgen_mu = nn.ModuleList([
            # shape: [32,16,16] -> [1,16,16]
            nn.Sequential(
                modules.WnConv2d(self.reswidth,
                                 self.zchannels,
                                 self.kernel_size,
                                 1,
                                 padding,
                                 init_scale=scale)
            )
            for _ in range(self.nz - 1)])

        self.deepgen_std = nn.ModuleList([
            # shape: [32,16,16] -> [1,16,16]
            nn.Sequential(
                modules.WnConv2d(self.reswidth,
                                 self.zchannels,
                                 self.kernel_size,
                                 1,
                                 padding, init_scale=scale)
            )
            for _ in range(self.nz - 1)])

        # <===== GENERATIVE MODEL =====>
        # the bottom (zi = 1) inference model
        self.gen_in = nn.Sequential(
            # shape: [1,16,16] -> [32,16,16]
            modules.WnConv2d(self.zchannels,
                             self.reswidth,
                             self.kernel_size,
                             1,
                             padding,
                             init_scale=1.0,
                             loggain=True),
            self.act
        )

        self.gen_res1 = nn.Sequential(
            # shape: [32,16,16] -> [32,16,16]
            modules.ResNetBlock(self.reswidth,
                                self.reswidth,
                                self.kernel_size,
                                1,
                                padding,
                                resdepth[0],
                                dropout_p,
                                self.actresnet),
            self.act
        ) if resdepth[0] > 0 else modules.Pass()

        self.gen_res0 = nn.Sequential(
            # shape: [32,16,16] -> [32,16,16]
            modules.ResNetBlock(self.reswidth,
                                self.reswidth,
                                5,
                                1,
                                padding_proc,
                                self.nprocessing,
                                dropout_p,
                                self.actresnet),
            self.act
        ) if self.nprocessing > 0 else modules.Pass()

        self.gen_mu = nn.Sequential(
            # shape: [32,16,16] -> [4,16,16]
            modules.WnConv2d(self.reswidth,
                             4 * xs[0],
                             self.kernel_size,
                             1,
                             padding,
                             init_scale=0.1),
            # shape: [4,16,16] -> [1,32,23]
            modules.UnSqueeze2d(factor=2)
        )

        # the scale parameter of the bottom (zi = 1) generative model is modelled conditional
        self.gen_std = nn.Sequential(
            modules.WnConv2d(self.reswidth,
                                        4 * xs[0],
                                        self.kernel_size,
                                        1,
                                        padding,
                                        init_scale=0.1),
            modules.UnSqueeze2d(factor=2)
        )
    # function to set the model to compression mode
    def compress(self, compress=True):
        self.compressing = compress

    # function that only takes in the layer number and returns a distribution based on that
    def infer(self, i):
        # nested function that takes in the "given" value of the conditional Logistic distribution
        # and returns the mu and scale parameters of that distribution
        def distribution(given):
            h = given

            # if compressing, the input might not be float32, so we'll have to convert it first
            if self.compressing:
                type = h.type()
                h = h.float()

            # bottom latent layer
            if i == 0:
                # if compressing, the input is flattened, so we'll have to convert it back to a Tensor
                if self.compressing:
                    h = h.view((-1,) + self.xs)
                # also, when NOT compressing, the input is not scaled from [0,255] to [-1,1]
                else:
                    h = (h - 127.5) / 127.5

                # input convolution
                h = self.infer_in(h)

                # processing ResNet blocks
                h = self.infer_res0(h)

                # other ResNet blocks
                h = self.infer_res1(h)

                # mu parameter of the conditional Logistic distribution
                mu = self.infer_mu(h)

                # scale parameter of the conditional Logistic distribution
                # clamp the output of the scale parameter between [0.1, 1.0] for stability
                scale = 0.1 + 0.9 * self.sigmoid(self.infer_std(h) + 2.)

            # deeper latent layers
            else:
                # if compressing, the input is flattened, so we'll have to convert it back to a Tensor
                if self.compressing:
                    h = h.view((-1,) + self.zdim)

                # input convolution
                h = self.deepinfer_in[i - 1](h)

                # other ResNet blocks
                h = self.deepinfer_res[i - 1](h)

                # mu parameter of the conditional Logistic distribution
                mu = self.deepinfer_mu[i - 1](h)

                # scale parameter of the conditional Logistic distribution
                # clamp the output of the scale parameter between [0.1, 1.0] for stability
                scale = 0.1 + 0.9 * self.sigmoid(self.deepinfer_std[i - 1](h) + 2.)

            if self.compressing:
                # if compressing, the "batch-size" can only be 1
                assert mu.shape[0] == 1

                # flatten the Tensors back and convert back to the input datatype
                mu = mu.view(np.prod(self.zdim)).type(type)
                scale = scale.view(np.prod(self.zdim)).type(type)
            return mu, scale

        return distribution

    # function that only takes in the layer number and returns a distribution based on that
    def generate(self, i):
        # nested function that takes in the "given" value of the conditional Logistic distribution
        # and returns the mu and scale parameters of that distribution
        def distribution(given):
            h = given

            # if compressing, the input is flattened, so we'll have to convert it back to a Tensor
            # also, the input might not be float32, so we'll have to convert it first
            if self.compressing:
                type = h.type()
                h = h.float()
                h = h.view((-1,) + self.zdim)

            # bottom latent layer
            if i == 0:
                # input convolution
                h = self.gen_in(h)

                # processing ResNet blocks
                h = self.gen_res1(h)

                # other ResNet blocks
                h = self.gen_res0(h)

                # mu parameter of the conditional Logistic distribution
                mu = self.gen_mu(h)

                # scale parameter of the conditional Logistic distribution
                # set a minimal value for the scale parameter of the bottom generative model
                scale = ((2. / 255.) / 8.) + modules.softplus(self.gen_std(h))

            # deeper latent layers
            else:
                # input convolution
                h = self.deepgen_in[i - 1](h)

                # other ResNet blocks
                h = self.deepgen_res[i - 1](h)

                # mu parameter of the conditional Logistic distribution
                mu = self.deepgen_mu[i - 1](h)

                # scale parameter of the conditional Logistic distribution
                # clamp the output of the scale parameter between [0.1, 1.0] for stability
                scale = 0.1 + 0.9 * modules.softplus(self.deepgen_std[i - 1](h) + np.log(np.exp(1.) - 1.))

            if self.compressing:
                # if compressing, the "batch-size" can only be 1
                assert mu.shape[0] == 1

                # flatten the Tensors back and convert back to the input datatype
                mu = mu.view(np.prod(self.xs if i == 0 else self.zdim)).type(type)
                scale = scale.view(np.prod(self.xs if i == 0 else self.zdim)).type(type)
            return mu, scale

        return distribution

    # function that takes as input the data and outputs all the components of the ELBO + the latent samples
    def loss(self, x):
        # tensor to store inference model losses
        logenc = torch.zeros((self.nz, x.shape[0], self.zdim[0]), device=x.device)

        # tensor to store the generative model losses
        logdec = torch.zeros((self.nz, x.shape[0], self.zdim[0]), device=x.device)

        # tensor to store the latent samples
        zsamples = torch.zeros((self.nz, x.shape[0], np.prod(self.zdim)), device=x.device)

        for i in range(self.nz):
            # inference model
            # get the parameters of inference distribution i given x (if i == 0) or z (otherwise)
            mu, scale = self.infer(i)(given=x if i == 0 else z)

            # sample untransformed sample from Logistic distribution (mu=0, scale=1)
            eps = random.logistic_eps(mu.shape, device=mu.device)
            # reparameterization trick: transform using obtained parameters
            z_next = random.transform(eps, mu, scale)

            # store the inference model loss
            zsamples[i] = z_next.flatten(1)
            logq = torch.sum(random.logistic_logp(mu, scale, z_next), dim=2)
            logenc[i] += logq

            # generative model
            # get the parameters of inference distribution i given z
            mu, scale = self.generate(i)(given=z_next)

            # store the generative model loss
            if i == 0:
                # if bottom (zi = 1) generative model, evaluate loss using discretized Logistic distribution
                logp = torch.sum(random.discretized_logistic_logp(mu, scale, x), dim=1)
                logrecon = logp

            else:
                logp = torch.sum(random.logistic_logp(mu, scale, x if i == 0 else z), dim=2)
                logdec[i - 1] += logp

            z = z_next

        # store the prior loss
        logp = torch.sum(random.logistic_logp(torch.zeros(1, device=x.device), torch.ones(1, device=x.device), z), dim=2)
        logdec[self.nz - 1] += logp

        # convert from "nats" to bits
        logenc = torch.mean(logenc, dim=1) * self.bitsscale
        logdec = torch.mean(logdec, dim=1) * self.bitsscale
        logrecon = torch.mean(logrecon) * self.bitsscale
        return logrecon, logdec, logenc, zsamples

    # function to sample from the model (using the generative model)
    def sample(self, device, epoch, num=64):
        # sample "num" latent variables from the prior
        z = random.logistic_eps(((num,) + self.zdim), device=device)

        # sample from the generative distribution(s)
        for i in reversed(range(self.nz)):
            mu, scale = self.generate(i)(given=z)
            eps = random.logistic_eps(mu.shape, device=device)
            z_prev = random.transform(eps, mu, scale)
            z = z_prev

        # scale up from [-1,1] to [0,255]
        x_cont = (z * 127.5) + 127.5

        # ensure that [0,255]
        x = torch.clamp(x_cont, 0, 255)

        # scale from [0,255] to [0,1] and convert to right shape
        x_sample = x.float() / 255.
        x_sample = x_sample.view((num,) + self.xs)

        # make grid out of "num" samples
        x_grid = utils.make_grid(x_sample)

        # log
        self.logger.add_image('x_sample', x_grid, epoch)

    # function to sample a reconstruction of input data
    def reconstruct(self, x_orig, device, epoch):
        # take only first 32 datapoints of the input
        # otherwise the output image grid may be too big for visualization
        x_orig = x_orig[:32, :, :, :].to(device)

        # sample from the bottom (zi = 1) inference model
        mu, scale = self.infer(0)(given=x_orig)
        eps = random.logistic_eps(mu.shape, device=device)
        z = random.transform(eps, mu, scale)  # sample zs

        # sample from the bottom (zi = 1) generative model
        mu, scale = self.generate(0)(given=z)
        x_eps = random.logistic_eps(mu.shape, device=device)
        x_cont = random.transform(x_eps, mu, scale)

        # scale up from [-1.1] to [0,255]
        x_cont = (x_cont * 127.5) + 127.5

        # esnure that [0,255]
        x_sample = torch.clamp(x_cont, 0, 255)

        # scale from [0,255] to [0,1] and convert to right shape
        x_sample = x_sample.float() / 255.
        x_orig = x_orig.float() / 255.

        # concatenate the input data and the sampled reconstructions for comparison
        x_with_recon = torch.cat((x_orig, x_sample))

        # make a grid out of the original data and the reconstruction samples
        x_with_recon = x_with_recon.view((2 * x_orig.shape[0],) + self.xs)
        x_grid = utils.make_grid(x_with_recon)

        # log
        self.logger.add_image('x_reconstruct', x_grid, epoch)

def warmup(model, device, data_loader, warmup_batches, root_process):
    # convert model to evaluation mode (no Dropout etc.)
    model.eval()

    # prepare initialization batch
    for batch_idx, image in enumerate(data_loader):
        # stack image with to current stack
        warmup_images = torch.cat((warmup_images, image), dim=0) \
            if batch_idx != 0 else image

        # stop stacking batches if reaching limit
        if batch_idx + 1 == warmup_batches:
            break

    # set the stack to current device
    warmup_images = warmup_images.to(device)

    # do one 'special' forward pass to initialize parameters
    with modules.init_mode():
        logrecon, logdec, logenc, _ = model.loss(warmup_images)

    # log
    if root_process:
        logdec = torch.sum(logdec, dim=1)
        logenc = torch.sum(logenc, dim=1)

        elbo = -logrecon + torch.sum(-logdec + logenc)

        elbo = elbo.detach().cpu().numpy() * model.perdimsscale
        entrecon = -logrecon.detach().cpu().numpy() * model.perdimsscale
        entdec = -logdec.detach().cpu().numpy() * model.perdimsscale
        entenc = -logenc.detach().cpu().numpy() * model.perdimsscale

        kl = entdec - entenc

        print(f'====> Epoch: {0} Average loss: {elbo:.4f}')
        model.logger.add_text('architecture', f"{model}", 0)
        model.logger.add_scalar('elbo/train', elbo, 0)
        model.logger.add_scalar('x/reconstruction/train', entrecon, 0)
        for i in range(1, logdec.shape[0] + 1):
            model.logger.add_scalar(f'z{i}/encoder/train', entenc[i - 1], 0)
            model.logger.add_scalar(f'z{i}/decoder/train', entdec[i - 1], 0)
            model.logger.add_scalar(f'z{i}/KL/train', kl[i - 1], 0)

def train(model, device, epoch, data_loader, optimizer, ema, log_interval, root_process, schedule=True, decay=0.99995):
    # convert model to train mode (activate Dropout etc.)
    model.train()

    # get number of batches
    nbatches = data_loader.batch_sampler.sampler.num_samples // data_loader.batch_size

    # switch to parameters not affected by exponential moving average decay
    for name, param in model.named_parameters():
        if param.requires_grad:
            param.data = ema.get_default(name)

    # setup training metrics
    if root_process:
        elbos = torch.zeros((nbatches), device=device)
        logrecons = torch.zeros((nbatches), device=device)
        logdecs = torch.zeros((nbatches, model.nz), device=device)
        logencs = torch.zeros((nbatches, model.nz), device=device)

    if root_process:
        start_time = time.time()

    # allocate memory for data
    data = torch.zeros((data_loader.batch_size,) + model.xs, device=device)

    # enumerate over the batches
    for batch_idx, batch in enumerate(data_loader):
        # keep track of the global step
        global_step = (epoch - 1) * len(data_loader) + (batch_idx + 1)

        # update the learning rate according to schedule
        if schedule:
            for param_group in optimizer.param_groups:
                lr = param_group['lr']
                lr = lr_step(global_step, lr, decay=decay)
                param_group['lr'] = lr

        # empty all the gradients stored
        optimizer.zero_grad()

        # copy the mini-batch in the pre-allocated data-variable
        data.copy_(batch)

        # evaluate the data under the model and calculate ELBO components
        logrecon, logdec, logenc, zsamples = model.loss(data)

        # free bits technique, in order to prevent posterior collapse
        bits_pc = 1.
        kl = torch.sum(torch.max(-logdec + logenc, bits_pc * torch.ones((model.nz, model.zdim[0]), device=device)))

        # compute the inference- and generative-model loss
        logdec = torch.sum(logdec, dim=1)
        logenc = torch.sum(logenc, dim=1)

        # construct ELBO
        elbo = -logrecon + kl

        # scale by image dimensions to get "bits/dim"
        elbo *= model.perdimsscale
        logrecon *= model.perdimsscale
        logdec *= model.perdimsscale
        logenc *= model.perdimsscale

        # calculate gradients
        elbo.backward()

        # take gradient step
        total_norm = nn.utils.clip_grad_norm_(model.parameters(), 1., norm_type=2)
        optimizer.step()

        # log gradient norm
        if root_process:
            model.logger.add_scalar('gnorm', total_norm, global_step)

        # do ema update on parameters used for evaluation
        if root_process:
            with torch.no_grad():
                for name, param in model.named_parameters():
                    if param.requires_grad:
                        ema(name, param.data)

        # log
        if root_process:
            elbos[batch_idx] += elbo
            logrecons[batch_idx] += logrecon
            logdecs[batch_idx] += logdec
            logencs[batch_idx] += logenc

        # log and save parameters
        if root_process and batch_idx % log_interval == 0 and log_interval < nbatches:
            # print metrics to console
            print(f'Train Epoch: {epoch} [{batch_idx}/{nbatches} ({100. * batch_idx / len(data_loader):.0f}%)]\tLoss: {elbo.item():.6f}\tGnorm: {total_norm:.2f}\tSteps/sec: {(time.time() - start_time) / (batch_idx + 1):.3f}')

            model.logger.add_scalar('step-sec', (time.time() - start_time) / (batch_idx + 1), global_step)
            entrecon = -logrecon
            entdec = -logdec
            entenc = -logenc
            kl = entdec - entenc

            # log
            model.logger.add_scalar('elbo/train', elbo, global_step)
            for param_group in optimizer.param_groups:
                lr = param_group['lr']
            model.logger.add_scalar('lr', lr, global_step)

            model.logger.add_scalar('x/reconstruction/train', entrecon, global_step)
            for i in range(1, logdec.shape[0] + 1):
                model.logger.add_scalar(f'z{i}/encoder/train', entenc[i - 1], global_step)
                model.logger.add_scalar(f'z{i}/decoder/train', entdec[i - 1], global_step)
                model.logger.add_scalar(f'z{i}/KL/train', kl[i - 1], global_step)

    # save training params, to be able to return to these values after evaluation
    with torch.no_grad():
        for name, param in model.named_parameters():
            if param.requires_grad:
                ema.register_default(name, param.data)

    # print the average loss of the epoch to the console
    if root_process:
        elbo = torch.mean(elbos).detach().cpu().numpy()
        print(f'====> Epoch: {epoch} Average loss: {elbo:.4f}')

def test(model, device, epoch, ema, data_loader, tag, root_process):
    # convert model to evaluation mode (no Dropout etc.)
    model.eval()

    # setup the reconstruction dataset
    recon_dataset = None
    nbatches = data_loader.batch_sampler.sampler.num_samples // data_loader.batch_size
    recon_batch_idx = int(torch.Tensor(1).random_(0, nbatches - 1))

    # setup testing metrics
    if root_process:
        logrecons = torch.zeros((nbatches), device=device)
        logdecs = torch.zeros((nbatches, model.nz), device=device)
        logencs = torch.zeros((nbatches, model.nz), device=device)

    elbos = []

    # switch to EMA parameters for evaluation
    for name, param in model.named_parameters():
        if param.requires_grad:
            param.data = ema.get_ema(name)

    # allocate memory for the input data
    data = torch.zeros((data_loader.batch_size,) + model.xs, device=device)

    # enumerate over the batches
    for batch_idx, batch in enumerate(data_loader):
        # save batch for reconstruction
        if batch_idx == recon_batch_idx:
            recon_dataset = data

        # copy the mini-batch in the pre-allocated data-variable
        data.copy_(batch)

        with torch.no_grad():
            # evaluate the data under the model and calculate ELBO components
            logrecon, logdec, logenc, _ = model.loss(data)

            # construct the ELBO
            elbo = -logrecon + torch.sum(-logdec + logenc)

            # compute the inference- and generative-model loss
            logdec = torch.sum(logdec, dim=1)
            logenc = torch.sum(logenc, dim=1)

        if root_process:
            # scale by image dimensions to get "bits/dim"
            elbo *= model.perdimsscale
            logrecon *= model.perdimsscale
            logdec *= model.perdimsscale
            logenc *= model.perdimsscale

            elbos.append(elbo.item())

            # log
            logrecons[batch_idx] += logrecon
            logdecs[batch_idx] += logdec
            logencs[batch_idx] += logenc

    if root_process:
        elbo = np.mean(elbos)

        entrecon = -torch.mean(logrecons).detach().cpu().numpy()
        entdec = -torch.mean(logdecs, dim=0).detach().cpu().numpy()
        entenc = -torch.mean(logencs, dim=0).detach().cpu().numpy()
        kl = entdec - entenc

        # print metrics to console and Tensorboard
        print(f'\nEpoch: {epoch}\tTest loss: {elbo:.6f}')
        model.logger.add_scalar('elbo/test', elbo, epoch)

        # log to Tensorboard
        model.logger.add_scalar('x/reconstruction/test', entrecon, epoch)
        for i in range(1, logdec.shape[0] + 1):
            model.logger.add_scalar(f'z{i}/encoder/test', entenc[i - 1], epoch)
            model.logger.add_scalar(f'z{i}/decoder/test', entdec[i - 1], epoch)
            model.logger.add_scalar(f'z{i}/KL/test', kl[i - 1], epoch)

        # if the current ELBO is better than the ELBO's before, save parameters
        if elbo < model.best_elbo and not np.isnan(elbo):
            model.logger.add_scalar('elbo/besttest', elbo, epoch)
            if not os.path.exists(f'params/imagenetcrop/'):
                os.makedirs(f'params/imagenetcrop/')
            torch.save(model.state_dict(), f'params/imagenetcrop/{tag}')
            torch.save(model.state_dict(), f'params/imagenetcrop/epoch{epoch}_{tag}')
            print("saved params\n")
            model.best_elbo = elbo

            model.sample(device, epoch)
            model.reconstruct(recon_dataset, device, epoch)
        else:
            print("loss did not improve\n")

# learning rate schedule
def lr_step(step, curr_lr, decay=0.99995, min_lr=5e-4):
    # only decay after certain point
    # and decay down until minimal value
    if step > 100000 and curr_lr > min_lr:
        curr_lr *= decay
        return curr_lr
    return curr_lr

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--seed', default=99, type=int, help="seed for experiment reproducibility")
    parser.add_argument('--nz', default=8, type=int, help="number of latent variables, greater or equal to 1")
    parser.add_argument('--zchannels', default=8, type=int, help="number of channels for the latent variables")
    parser.add_argument('--nprocessing', default=4, type=int, help='number of processing layers')
    parser.add_argument('--gpu', default=0, type=int, help="number of gpu's to distribute optimization over")
    parser.add_argument('--interval', default=100, type=int, help="interval for logging/printing of relevant values")
    parser.add_argument('--epochs', default=10000000000, type=int, help="number of sweeps over the dataset (epochs)")
    parser.add_argument('--blocks', default=8, type=int, help="number of ResNet blocks")
    parser.add_argument('--width', default=256, type=int, help="number of channels in the convolutions in the ResNet blocks")
    parser.add_argument('--dropout', default=0., type=float, help="dropout rate of the hidden units")
    parser.add_argument('--kernel', default=3, type=int, help="size of the convolutional filter (kernel) in the ResNet blocks")
    parser.add_argument('--batch', default=128, type=int, help="size of the mini-batch for gradient descent")
    parser.add_argument('--dist', default=1, type=int, help="distribute (1) over different gpu's and use Horovod to do so, or not (0)")
    parser.add_argument('--lr', default=2e-3, type=float, help="learning rate gradient descent")
    parser.add_argument('--schedule', default=1, type=float, help="learning rate schedule: yes (1) or no (0)")
    parser.add_argument('--decay', default=0.999995, type=float, help="decay of the learning rate when using learning rate schedule")

    args = parser.parse_args()
    print(args)  # print all the hyperparameters

    # store hyperparameters in variables
    seed = args.seed
    epochs = args.epochs
    batch_size = args.batch
    nz = args.nz
    zchannels = args.zchannels
    nprocessing = args.nprocessing
    gpu = args.gpu
    blocks = args.blocks
    width = args.width
    log_interval = args.interval
    dropout = args.dropout
    kernel = args.kernel
    distributed = args.dist
    lr = args.lr
    schedule = True if args.schedule == 1 else False
    decay = args.decay
    assert nz > 0

    # setup seeds to maintain experiment reproducibility
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    np.random.seed(seed)
    torch.backends.cudnn.deterministic = True

    # Distributed: set up horovod over multiple gpu's
    if distributed:
        import horovod.torch as hvd

        # initialize horovod
        hvd.init()

        # pin gpu to "local rank" (see Horovod documentation)
        torch.cuda.set_device(hvd.local_rank())
        print(f"My local rank is {hvd.local_rank()}")

        # distribute mini-batches over the different gpu's
        batch_size //= hvd.size()

    # string-tag for logging
    tag = f'nz{nz}'

    # define the "root process": only one of the gpu's has to log relevant values
    # set only one gpu as root process
    root_process = True
    if distributed and not hvd.rank() == 0:
        root_process = False

    # set GPU/CPU options
    use_cuda = torch.cuda.is_available()
    cudastring = "cuda" if distributed else f"cuda:{gpu}"
    device = torch.device(cudastring if use_cuda else "cpu")

    # set number of workers and pin the memory if we distribute over multiple gpu's
    # (see Dataloader docs of PyTorch)
    kwargs = {'num_workers': 8, 'pin_memory': True} if distributed else {}

    # create class that scales up the data to [0,255] if called
    class ToInt:
        def __call__(self, pic):
            return pic * 255

    # set data pre-processing transforms
    transform_ops = transforms.Compose([transforms.RandomCrop(32), transforms.ToTensor(), ToInt()])

    # store ImageNet data shape
    xs = (3, 32, 32)

    if root_process:
        print("Load model")

    # build model from hyperparameters
    model = Model(xs=xs,
                  kernel_size=kernel,
                  nprocessing=nprocessing,
                  nz=nz,
                  zchannels=zchannels,
                  resdepth=blocks,
                  reswidth=width,
                  dropout_p=dropout,
                  tag=tag,
                  root_process=root_process).to(device)

    # set up Adam optimizer
    optimizer = optim.Adam(model.parameters(), lr=lr)

    # if we distribute over multiple GPU's, set up Horovod's distributed optimizer wrapper around the optimizer
    if distributed:
        optimizer = hvd.DistributedOptimizer(optimizer,
                                             named_parameters=model.named_parameters(),
                                             compression=hvd.Compression.fp16)

    # print and log amount of parameters
    if root_process:
        model_parameters = filter(lambda p: p.requires_grad, model.parameters())
        num_parameters = sum([np.prod(p.size()) for p in model_parameters])
        print(f'Number of trainable parameters in model: {num_parameters}')
        model.logger.add_text(f'hyperparams', '{num_parameters}', 0)

    if root_process:
        print("Load data")

    # get dataset for training and testing of the model
    train_set = modules.ImageNet(root='data/imagenetfull/train', file='train.npy', transform=transform_ops)
    test_set = modules.ImageNet(root='data/imagenetfull/test', file='test.npy', transform=transform_ops)

    # setup data sampler
    if distributed:
        train_sampler = torch.utils.data.distributed.DistributedSampler(
            train_set, num_replicas=hvd.size(), rank=hvd.rank())
        test_sampler = torch.utils.data.distributed.DistributedSampler(
            test_set, num_replicas=hvd.size(), rank=hvd.rank())

    # setup mini-batch enumerator for both train-set and test-set
    train_loader = torch.utils.data.DataLoader(
        dataset=train_set, sampler=train_sampler if distributed else None,
        batch_size=batch_size, shuffle=False if distributed else True, drop_last=True, **kwargs)
    test_loader = torch.utils.data.DataLoader(
        dataset=test_set, sampler=test_sampler if distributed else None,
        batch_size=batch_size, shuffle=False if distributed else True, drop_last=True, **kwargs)

    # Distributed: broadcast parameters to all the processes
    if distributed:
        hvd.broadcast_parameters(model.state_dict(), root_rank=0)
        hvd.broadcast_optimizer_state(optimizer, root_rank=0)

    print("Data Dependent Initialization") if root_process else print("Data Dependent Initialization with ya!")
    # data-dependent initialization
    warmup(model, device, train_loader, 25, root_process)

    # if distributed over multiple GPU's, set-up a barrier ensuring that all the processes have initialized the models
    if distributed:
        hvd.allreduce_(torch.Tensor(0), name='barrier')

    # setup exponential moving average decay (EMA) for the parameters.
    # This basically means maintaining two sets of parameters during training/testing:
    # 1. parameters that are the result of EMA
    # 2. parameters not affected by EMA
    # The (1)st parameters are only active during test-time.
    ema = modules.EMA(0.999)
    with torch.no_grad():
        for name, param in model.named_parameters():
            # only parameters optimized using gradient-descent are relevant here
            if param.requires_grad:
                # register (1) parameters
                ema.register_ema(name, param.data)
                # register (2) parameters
                ema.register_default(name, param.data)

    # initial test loss
    test(model, device, 0, ema, test_loader, tag, root_process)

    # do the training loop and run over the test-set 1/5 epochs.
    print("Training") if root_process else print("Training with ya!")
    for epoch in range(1, epochs + 1):
        train(model, device, epoch, train_loader, optimizer, ema, log_interval, root_process, schedule, decay)
        test(model, device, epoch, ema, test_loader, tag, root_process)


from dbclient import *
import os, base64

WS_LIST = "/workspace/list"
WS_STATUS = "/workspace/get-status"
WS_MKDIRS = "/workspace/mkdirs"
WS_IMPORT = "/workspace/import"
WS_EXPORT = "/workspace/export"
LS_ZONES = "/clusters/list-zones"

class WorkspaceClient(dbclient):

    def get_user_import_args(self, full_local_path, nb_full_path):
        fp = open(full_local_path, "rb")

        in_args = {
            "content": base64.encodebytes(fp.read()),
            "path": nb_full_path[:-4],
            "format": "DBC"
        }
        return in_args

    def download_notebooks(self, ws_log_file='user_workspace.log', ws_dir='artifacts/'):
        ws_log = self._export_dir + ws_log_file
        if not os.path.exists(ws_log):
            raise Exception("Run --workspace first to download full log of all notebooks.")
        with open(ws_log, "r") as fp:
            for nb in fp:
                self.download_notebook_helper(nb.rstrip(), export_dir=self._export_dir + ws_dir)

    def download_notebook_helper(self, notebook_path, export_dir='artifacts/'):
        get_args = {'path': notebook_path, 'format': 'DBC'}
        resp = self.get(WS_EXPORT, get_args)
        with open(self._export_dir + 'failed_notebooks.log', 'a') as err_log:
            if resp.get('error_code', None):
                err_log.write(notebook_path + '\n')
                return {'error_code': resp.get('error_code'), 'path': notebook_path}
        nb_path = os.path.dirname(notebook_path)
        if nb_path != '/':
            # path is NOT empty, remove the trailing slash from export_dir
            save_path = export_dir[:-1] + nb_path + '/'
        else:
            save_path = export_dir
        save_filename = save_path + os.path.basename(notebook_path) + '.dbc'
        # If the local path doesn't exist,we create it before we save the contents
        if not os.path.exists(save_path) and save_path:
            os.makedirs(save_path, exist_ok=True)
        with open(save_filename, "wb") as f:
            f.write(base64.b64decode(resp['content']))
        return {'path': notebook_path}

    def log_all_workspace_items(self, ws_path='/', workspace_log_file='user_workspace.log',
                                libs_log_file='libraries.log'):
        if ws_path == '/':
            # default is the root path
            get_args = {'path': '/'}
        else:
            get_args = {'path': ws_path}

        items = self.get(WS_LIST, get_args).get('objects', None)
        if items is not None:
            # list all the users folders only
            folders = list(self.my_map(lambda y: y.get('path', None),
                                       filter(lambda x: x.get('object_type', None) == 'DIRECTORY', items)))
            # should be no notebooks, but lets filter and can check later
            notebooks = list(self.my_map(lambda y: y.get('path', None),
                                         filter(lambda x: x.get('object_type', None) == 'NOTEBOOK', items)))
            libraries = list(self.my_map(lambda y: y.get('path', None),
                                         filter(lambda x: x.get('object_type', None) == 'LIBRARY', items)))

            # log file for
            workspace_log = self._export_dir + workspace_log_file
            libs_log = self._export_dir + libs_log_file
            with open(workspace_log, "a") as ws_fp, open(libs_log, "a") as libs_fp:
                for x in notebooks:
                    ws_fp.write(x + '\n')
                for y in libraries:
                    libs_fp.write(y + '\n')

            if folders:
                for f in folders:
                    self.log_all_workspace_items(ws_path=f, workspace_log_file=workspace_log_file,
                                                 libs_log_file=libs_log_file)

    def import_all_workspace_items(self, load_dir='logs/', export_dir='artifacts/'):
        src_dir = load_dir + export_dir
        for root, subdirs, files in os.walk(src_dir):
            # replace the local directory with empty string to get the notebook workspace directory
            nb_dir = root.replace(src_dir, '')
            # upload to a specific benchmark folder
            upload_dir = '/Benchmark3/' + nb_dir
            # make the top level folder before uploading files within the loop
            resp_mkdirs = self.post(WS_MKDIRS, {'path': upload_dir})
            for f in files:
                # create the local file path to load the DBC file
                localFilePath = os.path.join(root, f)
                # create the ws full file path including filename
                wsFilePath = upload_dir + f
                # generate json args with binary data for notebook to upload to the workspace path
                nb_input_args = self.get_user_import_args(localFilePath, wsFilePath)
                # call import to the workspace
                resp_upload = self.post(WS_IMPORT, nb_input_args)


STATES = [('draft','Draft'),('open','Open'),('done','Done')]
from odoo import models, fields, api, _
from odoo.exceptions import UserError
from datetime import datetime

class wizard_material_plan_inherit(models.TransientModel):
	_name = "vit.wizard_material_plan_inherit"

	material_plan_id = fields.Many2one(comodel_name="vit.material_plan", string="Material Plan", help="")
	name = fields.Char(readonly=True, default='New', string="Name",  help="")
	date = fields.Date( string="Date", related="material_plan_id.date")
	product_id = fields.Many2one(string="Product ID", comodel_name="vit.product_jo_line",required=True, help="", related="material_plan_id.product_id")
	wizard_pr_ids = fields.One2many(comodel_name="vit.wizard_material_plan_pr_line",  inverse_name="wizard_pr_id",  string="Wizard PR lines", help="")
	wizard_pr_add_line_ids = fields.Many2many(comodel_name="vit.material_plan_lane")
	state = fields.Selection(selection=STATES,  readonly=True, default=STATES[0][0],  string="State",  help="")

	@api.onchange('material_plan_id','wizard_pr_add_line_ids')
	def onchange_material_booking(self):
		domain = []
		if self.material_plan_id:
			domain.append(('material_plan_id','=', self.material_plan_id.id))
			domain.append(('to_pr','>',0))
		return {'domain': {'wizard_pr_add_line_ids': domain }}

	# @api.onchange('material_plan_id')
	# def onchange_material(self):
	# 	if not self.material_plan_id:
	# 		return
	# 	month = str(self.date.month)
	# 	year = str(self.date.year)
	# 	# self.name = 'ISFA-PR/' + year + '/' + month + '/'
	# 	obj = self.env['vit.material_plan_lane'].search([('material_plan_id','=',self.material_plan_id.id)])
	# 	line_data = []
	# 	for record in self:
	# 		record.update({"wizard_pr_ids": False})
	# 		for x in obj:
	# 			if x.to_pr > 0:
	# 				line_data = [(0, 0, {
	# 					"code_material": x.product_id.default_code,
	# 					"name_material": x.product_id.name,
	# 					"product_id": x.product_id.id,
	# 					"qty": x.to_pr,
	# 					"uom_id": x.uom_id.id,
	# 					})]
	# 				record.update({"wizard_pr_ids": line_data})

	@api.multi
	def confirm(self):
		# plan = self.material_plan_id
		line = []
		for z in self.wizard_pr_add_line_ids:
			line.append((0, 0, {
					"name": z.code_material,
					"product_id": z.product_id.id,
					"qty": z.qty,
					"uom_id": z.uom_id,
					"weight" : z.weight
					}))

		data = {
				# "name": self.name,
				'mp_id': self.material_plan_id.id,
				"tipe": "biasa",
				"jo_no":1,
				"jo_id": self.material_plan_id.jo_id.id,
				"project": self.material_plan_id.project_des,
				"date": self.date,
				"pr_line_ids": line,
				"is_show": 1,
				}
		res = self.env['vit.pr'].create(data)
		return {
				'type': 'ir.actions.client',
				'tag': 'reload',
				}

class wizard_material_plan_pr_line(models.TransientModel):
	_name = "vit.wizard_material_plan_pr_line"

	wizard_pr_id = fields.Many2one(comodel_name="vit.wizard_material_plan_inherit", string="Wizard Material Plan PR")
	product_id = fields.Many2one(comodel_name="product.product", string="Product", help="")
	code_material = fields.Char( string="Code", help="",)
	qty = fields.Float( string="Qty",  help="")
	weight = fields.Float(string="Total Weight", help="", compute="_compute_weight")
	uom_id = fields.Many2one(comodel_name="uom.uom", string="Uom", help="")
	name_material = fields.Char(string="Product")

	@api.depends("qty")
	def _compute_weight(self):
		for rec in self:
			rec.weight = rec.product_id.weight * rec.qty


'''
Evaluate the training error
You'll now evaluate the training set RMSE achieved by the regression tree dt that you instantiated in a previous exercise.

In addition to dt, X_train and y_train are available in your workspace.

Instructions
100 XP
Import mean_squared_error as MSE from sklearn.metrics.
Fit dt to the training set.
Predict dt's training set labels and assign the result to y_pred_train.
Evaluate dt's training set MSE and assign it to RMSE_train.
'''
SOLUTION
# Import mean_squared_error from sklearn.metrics as MSE
from sklearn.metrics import mean_squared_error as MSE

# Fit dt to the training set
dt.fit(X_train, y_train)

# Predict the labels of the training set
y_pred_train = dt.predict(X_train)

# Evaluate the training set RMSE of dt
RMSE_train = (MSE(y_train, y_pred_train))**(1/2)

# Print RMSE_train
print('Train RMSE: {:.2f}'.format(RMSE_train))

Data Scientist with Python - Career Track /24. Machine Learning with Tree-Based Models in Python/02. The Bias-Variance Tradeoff 0%/03. Evaluate the training error.py


# -*- coding: utf-8 -*-
"""
The backend script for Library Database App

@author: Lezend
"""

import sqlite3 #sqlite3 module is necessary for interacting with SQL databases

"""
Creates a new table if there is no existing one.
"""
def create_table():
    conn = sqlite3.connect("app database.db")
    cur = conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS books (sno INTEGER PRIMARY KEY, title TEXT, author TEXT, year INTEGER, book_id INTEGER)")
    conn.commit()
    conn.close()

"""
Inserts a new row into the table.
"""
def insert(title, author, year, book_id):
    conn = sqlite3.connect("app database.db")
    cur = conn.cursor()
    cur.execute("INSERT INTO books VALUES (NULL, ?, ?, ?, ?)", (title, author, year, book_id))
    conn.commit()
    conn.close()

"""
Returns all the records from the table.
"""
def view():
    conn = sqlite3.connect("app database.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM books")
    rows = cur.fetchall() #stores fetched records in the "rows" varaible
    conn.close()
    return rows

"""
Searches for the record with a specific Title or Author or Year or Book_id
"""
def search(title = "", author = "", year = "", book_id = ""):
    conn = sqlite3.connect("app database.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM books WHERE title = ? OR author = ? OR year = ? OR book_id = ?",
                (title, author, year, book_id))
    rows = cur.fetchall()
    conn.close()
    return rows

"""
Deletes the selected row from the List box.
"""
def delete(sno):
    conn = sqlite3.connect("app database.db")
    cur = conn.cursor()
    cur.execute("DELETE FROM books WHERE sno = ?", (sno,))
    conn.commit()
    conn.close()

"""
Updates the selected row from the List box with the new values.
"""
def update(sno, title, author, year, book_id):
    conn = sqlite3.connect("app database.db")
    cur = conn.cursor()
    cur.execute("UPDATE books SET title = ?, author = ?, year = ?, book_id = ? WHERE sno = ?",
                (title, author, year, book_id, sno))
    conn.commit()
    conn.close()

create_table() #Creates table for the initial run if there is no existing table


"""
Tests for edges.py
"""

import unittest
import pandas as pd
from biothings_explorer.user_query_dispatcher import SingleEdgeQueryDispatcher
from biothings_explorer.filters.edges import filter_node_degree

class TestFilterEdges(unittest.TestCase):

    # test for count values
    def test_count_values(self):
        counts = [10, 20, 40, 50, 100]
        seqd = SingleEdgeQueryDispatcher(input_cls='Gene',
                                 output_cls='ChemicalSubstance',
                                 input_id='NCBIGene',
                                 values='1017')
        seqd.query()
        for count in counts:
            newG = filter_node_degree(seqd.G, count)
            self.assertEqual(len(newG.nodes), count+1)

    # edge case test if count > num nodes, then returns num_nodes results
    def test_num_nodes(self):
        count = 1000
        seqd = SingleEdgeQueryDispatcher(input_cls='Gene',
                                         output_cls='ChemicalSubstance',
                                         input_id='NCBIGene',
                                         values='1017')
        seqd.query()
        newG = filter_node_degree(seqd.G, count)
        self.assertEqual(len(newG.nodes), len(seqd.G.nodes))

    # test for correct ordering of ranks
    def test_ranks(self):
        seqd = SingleEdgeQueryDispatcher(input_cls='Disease',
                                         input_id='MONDO',
                                         output_cls='PhenotypicFeature',
                                         pred='related_to',
                                         values='MONDO:0010997')
        seqd.query()
        newG = filter_node_degree(seqd.G)
        for i1,node1 in enumerate(newG.nodes):
            if node1 == 'MONDO:MONDO:0010997':
                continue
            for i2,node2 in enumerate(newG.nodes):
                if node2 == 'MONDO:MONDO:0010997':
                    continue
                if newG.degree(node1) > newG.degree(node2):
                    self.assertLess(newG.nodes.data()[node1]['rank'], newG.nodes.data()[node2]['rank'])
                elif newG.degree(node1) < newG.degree(node2):
                    self.assertGreater(newG.nodes.data()[node1]['rank'], newG.nodes.data()[node2]['rank'])

if __name__ == '__main__':
    unittest.main()


#trackbar
'''advance: paint brush + trackbar to paint different colors (not done yet)'''

import numpy as np
import cv2
# The function attached to the trackbar.
def nothing(x):
    pass
cv2.namedWindow("trackbar exercise")
#creat trackbar
cv2.createTrackbar('B', "trackbar exercise", 0, 255, nothing)
cv2.createTrackbar('G', "trackbar exercise", 0, 255, nothing)
cv2.createTrackbar('R', "trackbar exercise", 0, 255, nothing)
cv2.createTrackbar('Radius', "trackbar exercise", 0, 100, nothing)
#creat switch
switch = '0:OFF\n1:ON'
cv2.createTrackbar(switch, "trackbar exercise", 0, 1, nothing)

#capture mouse event
drawing = False
mode = True
ix,iy = -1,-1
def on_mouse(event, x,y, flags, params):
    global drawing,ix,iy, mode
    if event == cv2.EVENT_RBUTTONDOWN:
        drawing = True
        ix,iy = x,y
    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing == True:
            if mode == True:
                cv2.circle(img, (x,y), radius, (b,g,r), 10)
            else:
                cv2.rectangle(img, (ix,iy),(x,y), (b,g,r), 10)
    elif event == cv2.EVENT_RBUTTONUP:
        drawing = False
        if mode == True:
            cv2.circle(img, (x,y), radius, (b,g,r), 10)
        else :
            cv2.rectangle(img, (ix,iy),(x,y), (b,g,r), 10)

#create black image
img = np.zeros((340,500,3), np.uint8)
cv2.namedWindow('trackbar exercise')
cv2.setMouseCallback('trackbar exercise', on_mouse)

while(1):
    cv2.imshow('trackbar exercise', img)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
    elif k == ord(" "):
        mode = not mode
    b= cv2.getTrackbarPos("B","trackbar exercise")
    g= cv2.getTrackbarPos("G","trackbar exercise")
    r= cv2.getTrackbarPos("R","trackbar exercise")
    s= cv2.getTrackbarPos(switch,"trackbar exercise")
    radius= cv2.getTrackbarPos('Radius',"trackbar exercise")

    if s== 0 :
        img[:]=0

cv2.destroyAllWindows()


from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
import requests
import urllib.parse
import urllib.request
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import html.parser
from html.parser import HTMLParser

def crawl(request):
	# Forbesurl = forbesUrl()
	Forbes = frobes()
	# Bloomberg = bloomberg()
	# Cnbc = cnbc()
	return render(request, "crawl/crawl.html",{'Forbes':Forbes})
	# return render(request, "crawl/crawl.html",{'Bloomberg': Bloomberg ,'Forbes':Forbes,'cnbc':Cnbc})

def crawlBase(link):
	with urllib.request.urlopen(link) as urlToCrawl:
			htmltext = urlToCrawl.read()
	soup = BeautifulSoup(htmltext)
	# page  = requests.get('https://www.unilever.com.au/news/')
	return soup

def forbesUrl():
	urls = []
	data = []
	links = []
	soup = crawlBase('http://www.forbes.com/search/all/unilever/50/All-time/1/')
	results = soup.find_all('article')
	for li in soup.find_all('li',attrs={'class':'article edittools-contentitem'}):
		a = li.find_all('a')[1]
		urls.append(a.attrs['href'] )
	for result in results:
		if 'unilever' in str(result.contents):
			data.append(result.contents)
	for obj in data:
		for link in urls:
			if str(link) in str(obj):
				links.append(link)
	return links

def frobes():
	linkstest = ["http://www.forbes.com/sites/dividendchannel/2016/02/22/april-15th-options-now-available-for-unilever-ul/#3426ccdb6b5d",
	"http://www.forbes.com/sites/stockoptionschannel/2015/03/24/first-week-of-november-20th-options-trading-for-unilever-ul/#5ed931ecb5e5"]
	FobersData = []
	for link in linkstest:
		soup = crawlBase(link)
		header = soup.find('h1')
		body = soup.find_all('div',attrs={'class':'article-text'})
		FobersData.append(header.contents)

	return FobersData

def bloomberg():
	data = []
	soup = crawlBase('http://www.bloomberg.com/search?query=unilever&endTime=&page=1')
	results = soup.find_all('article')
	for result in results:
		if 'unilever' in str(result):
	      		data.append(result)
	return results

def cnbc():
	data = []
	soup = crawlBase('http://search.cnbc.com/main.do?partnerId=2000&keywords=UNILEVER&sort=date&minimumrelevance=0.2&pubtime=30&pubfreq=d&categories=exclude')
	results = soup.find_all('div', attrs={'class': 'padT10'})
	return results


import numpy as np
import pandas as pd
from sklearn.externals import joblib

def categorical_distance(c_j, c_k):
    return int(not c_j == c_k)

def continuous_distance(x_j, x_k, r_i):
    return np.divide(np.absolute(x_j - x_k), r_i)

def gower_distance(X_j, X_k, **kwargs):
    distance = 0
    for col in kwargs['categorical']:
        distance += np.dot(kwargs['W_i'][kwargs['column_hash'][col]],
                           categorical_distance(X_j[kwargs['column_hash'][col]], X_k[kwargs['column_hash'][col]]))
    for col in kwargs['continuous']:
        distance += np.dot(kwargs['W_i'][kwargs['column_hash'][col]],
                           continuous_distance(X_j[kwargs['column_hash'][col]], X_k[kwargs['column_hash'][col]],
                                               kwargs['R_i'][kwargs['column_hash'][col]]))
    distance += kwargs['W_i'][11] * (1 - np.absolute(
        kwargs['similarity_matrix'][int(X_j[kwargs['column_hash']['ID']]), int(X_k[kwargs['column_hash']['ID']])]))

    return distance

class Tree:
    def __init__(self, serialized_dir, data_dir):
        self.balltree = joblib.load('{}/balltree'.format(serialized_dir))
        self.data = np.array(pd.read_csv('{}/carreers_scaled.csv'.format(data_dir)))
        self.ids = np.load('{}/ids.npy'.format(data_dir))
        self.reversed_ids = np.zeros(np.max(self.ids) + 1, dtype='int32')
        for i in range(self.ids.shape[0]):
            self.reversed_ids[self.ids[i]] = i

    def query(self, ids_points, k=5):
        q = self.data[self.reversed_ids[ids_points]]
        result = self.balltree.query(q, k=k, return_distance=False)
        return np.array(list(map(lambda x: self.ids[x], result)))

if __name__ == '__main__':
    # Ejemplo de uso

    id_query = np.array([4])
    tree = Tree('serialized', '../data')
    print(tree.query(id_query, 5))


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

import os
import shutil
import time

import tensorflow as tf
from tensorflow.contrib import learn

from PIL import Image
import numpy as np

import model_detect
import model_detect_data

# data for validation
dir_data_valid = './data_test'
#
dir_images_valid = dir_data_valid + '/images'
dir_contents_valid = dir_data_valid + '/contents'
dir_results_valid = dir_data_valid + '/results'
#
str_dot_img_ext = '.PNG'
#
model_dir = './model_detect'
model_name = 'model_detect'
#
anchor_heights = [18, 27, 36, 45, 54, 72]
#
threshold = 0.5
#

#
# input-output, graph
x = tf.placeholder(tf.float32, (1, None, None, 3), name = 'x-input')
w = tf.placeholder(tf.int32, (None,), name = 'w-input') # width
t_cls = tf.placeholder(tf.float32, (None, None, None), name = 'c-input')
t_ver = tf.placeholder(tf.float32, (None, None, None), name = 'v-input')
t_hor = tf.placeholder(tf.float32, (None, None, None), name = 'h-input')
#
features, sequence_length = model_detect.conv_feat_layers(x, w, learn.ModeKeys.TRAIN) #INFER
rnn_cls, rnn_ver, rnn_hor = model_detect.rnn_detect_layers(features, sequence_length, len(anchor_heights))
#
loss = model_detect.detect_loss(rnn_cls, rnn_ver, rnn_hor, t_cls, t_ver, t_hor)
#
print('graph defined')
#
# get test images
list_images_valid = model_detect_data.getFilesInDirect(dir_images_valid, str_dot_img_ext)
#
# test_result save-path
if os.path.exists(dir_results_valid): shutil.rmtree(dir_results_valid)
time.sleep(0.1)
os.mkdir(dir_results_valid)
#
# to process
saver = tf.train.Saver()
with tf.Session() as sess:
    #
    tf.global_variables_initializer().run()
    #
    # restore with saved data
    ckpt = tf.train.get_checkpoint_state(model_dir)
    #
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
    #
    # test
    NumImages = len(list_images_valid)
    curr = 0
    for img_file in list_images_valid:
        #
        # input data
        img_data, feat_size, target_cls, target_ver, target_hor = \
              model_detect_data.getImageAndTargets(img_file, anchor_heights)
        #
        img_size = model_detect_data.getImageSize(img_file) # width, height
        #
        w_arr = np.ones((feat_size[0],), dtype = np.int32) * img_size[0]
        #

        '''
        #
        print(feat_size)
        #

        feed_dict = {x: img_data, w: w_arr}
        feat, seq_len = sess.run([features, sequence_length], feed_dict)
        #
        #
        print('feat')
        print(feat)
        print('feat shape')
        print(feat.shape)
        print('seq_len')
        print(seq_len)
        #
        '''

        #
        feed_dict = {x: img_data, w: w_arr, \
                     t_cls: target_cls, t_ver: target_ver, t_hor: target_hor}
        #
        r_cls, r_ver, r_hor, loss_value = sess.run([rnn_cls, rnn_ver, rnn_hor, loss], feed_dict)
        #
        #
        curr += 1
        print('curr: %d / %d, loss: %f' % (curr, NumImages, loss_value))
        #
        #print(r_cls.shape)
        #print(r_cls)
        #
        filename = os.path.basename(img_file)
        arr_str = os.path.splitext(filename)
        #
        # image
        r = Image.fromarray(img_data[0][:,:,0] *255).convert('L')
        g = Image.fromarray(img_data[0][:,:,1] *255).convert('L')
        b = Image.fromarray(img_data[0][:,:,2] *255).convert('L')
        #
        # target
        file = os.path.join(dir_results_valid, arr_str[0] + '_target.png')
        img = Image.merge("RGB", (r, g, b))
        img.save(file)
        #
        text_bbox = model_detect_data.transResults(target_cls, target_ver, target_hor, \
                                                   anchor_heights, threshold)
        #
        model_detect_data.drawTextBox(file, text_bbox)
        #
        # result
        file = os.path.join(dir_results_valid, arr_str[0] + '_result.png')
        img = Image.merge("RGB", (r, g, b))
        img.save(file)
        #
        text_bbox = model_detect_data.transResults(r_cls, r_ver, r_hor, \
                                                   anchor_heights, threshold)
        #
        model_detect_data.drawTextBox(file, text_bbox)
        #
        #
        #break
        #
    #


number_hori_list = list()
number_verti_list = list()
with open('question-11.txt','r',encoding='utf-8') as f:
    lines = f.readlines()
    vertical_line = ''
    for line in lines:
        number_hori_list.append(line.replace('\n','').split(' '))
    #print(number_hori_list)
    line_num_idx = 0
    line_num = 0
    while line_num <= len(number_hori_list):
        for lines in number_hori_list:
            for line in lines:
                vertical_line += line[line_num_idx ] + ' '
                print('line_num_idx : ',line_num_idx)
                print(line[line_num_idx])
        number_verti_list.append(vertical_line.split(' '))
        line_num_idx += 1
        line_num += 1
        vertical_line = ''
    print(number_verti_list)


'''
Name: SFG_search.py
Desriptption:
Email: yesunhuang@mail.ustc.edu.cn
OpenSource: https://github.com/yesunhuang
Msg: For super computer
Author: YesunHuang
Date: 2021-06-05 21:15:28
'''
#import area
from qutip import*
import numpy as np
from scipy import math

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from matplotlib.pyplot import MultipleLocator

import time

'''
@name: BuildOperator_Exact
@fuction: Helper function for mesolve
@param {float} Ea
@param {float} deltaB
@param {float} g
@return {qobj} operator
'''
def BuildOperator_Exact(Ea:float,DeltaB:float,g:float):
    Na=int(max(math.ceil(Ea*Ea+6*Ea),4)); Nb=2*psi0_l[1]; Nc=Nb
    psi0=tensor(basis(Na,psi0_l[0]),basis(Nb,psi0_l[1]),basis(Nc,psi0_l[2]))
    a=tensor(destroy(Na),qeye(Nb),qeye(Nc))
    b=tensor(qeye(Na),destroy(Nb),qeye(Nc))
    c=tensor(qeye(Na),qeye(Nb),destroy(Nc))
    H=g*(a.dag()*b*c.dag()+a*b.dag()*c)+Ea*(a.dag()+a)+DeltaB*b.dag()*b
    c_ops=[]
    c_ops.append(np.sqrt(kappa_a)*a)
    c_ops.append(np.sqrt(kappa_b)*b)
    c_ops.append(np.sqrt(kappa_c)*c)
    psiIdeal=tensor(qeye(Na),basis(Nb,psi0_l[2]),basis(Nc,psi0_l[1]))
    idealState=psiIdeal*psiIdeal.dag()
    operator={'Hamilton':H,'Collapse':c_ops,'Initial_state':psi0,'Ideal_state':idealState,'track':[b.dag()*b,\
                                                                                c.dag()*c,\
                                                                                a.dag()*a,\
                                                                                b.dag()*b.dag()*b*b,\
                                                                                c.dag()*c.dag()*c*c,\
                                                                                b.dag()*c.dag()*b*c,\
                                                                                idealState]}
    return operator

'''
@name: SearchForMax
@fuction: Search for max
@param {list} array
@return {int} maxIndex
'''
def SearchForMax(array:list):
    max=0
    maxIndex=0
    for i in range(len(array)):
        if array[i]>max:
            max=array[i]
            maxIndex=i
    return maxIndex
#parameters
kappa_a=2;kappa_b=2;kappa_c=2
psi0_l=[0,3,0]
Ea=5
g=np.linspace(0.1,4,40)
Ntraj=2000
DeltaB=0
tlist=np.linspace(0,20,2000)
opts=Options()
opts.store_states=False
#opts.rhs_reuse=True
#opts.use_openmp=True

#data storage
data=np.zeros([np.size(g),9])

#solve for data
if __name__=="__main__":
    for j in range(0,np.size(g)):
        op=BuildOperator_Exact(Ea,DeltaB,g[j])
        output=mcsolve(op['Hamilton'],op['Initial_state'],tlist,op['Collapse'],op['track'],ntraj=Ntraj,options=opts)
        maxIndex=SearchForMax(output.expect[1])
        data[j][0]=tlist[maxIndex]  #t
        data[j][1]=output.expect[0][maxIndex]   #Nb
        data[j][2]=output.expect[1][maxIndex]   #Nc
        data[j][3]=output.expect[2][maxIndex]   #Na
        '''
        rho=output.states[0][maxIndex]*output.states[0][maxIndex].dag()
        for k in range(1,Ntraj):
            rho+=output.states[k][maxIndex]*output.states[k][maxIndex].dag()
        rho=1/Ntraj*rho
        rho_bc=rho.ptrace([1,2])
        data[j][4]=entropy_vn(rho_bc) #(A,BC) Entanglement
        '''
        data[j][5]=output.expect[6][maxIndex] #Fidelity
        data[j][6]=output.expect[3][maxIndex]/(data[j][1]*data[j][1]) #g2b
        data[j][7]=output.expect[4][maxIndex]/(data[j][2]*data[j][2]) #g2c
        data[j][8]=output.expect[5][maxIndex]/(data[j][1]*data[j][2]) #g2bc

#save data
np.savetxt('Data/EgData_DeltaB_'+str(DeltaB)+'_Ea_'+str(Ea)+'_g_'+str(g[0])+'-'+str(g[-1])+'.txt',data)
np.save('Data/EgData_DeltaB_'+str(DeltaB)+'_Ea_'+str(Ea)+'_g_'+str(g[0])+'-'+str(g[-1])+'.npy',data)


from PIL import Image
from numpy import *
from pylab import *

import warp
im1 = array(Image.open('smile.jpg').convert('L'))
im2 = array(Image.open('bg.jpg').convert('L'))

tp = array([[100,256,128,666],[300,283,567,233],[1,1,1,1]])

im3 = warp.image_in_image(im1, im2, tp)

figure()
gray()
imshow(im3)
axis('equal')
axis('off')
show()

Programming Computer Vision with Python/Chapter3/exercise2.py


from openerp import api, models
from openerp.exceptions import UserError
from openerp.tools.translate import _

class fal_group_expense_sheet_wizard(models.TransientModel):
    _inherit = 'fal.group.expense.sheet.wizard'

    @api.model
    def get_sequence(self, company_name):
        name = False
        if company_name == 'Cam Service':
            name = self.env['ir.sequence'].next_by_code('expense.sheet.cam') or ''
        elif company_name == 'Gleize Energie':
            name = self.env['ir.sequence'].next_by_code('expense.sheet.gleize') or ''
        return name

    @api.multi
    def action_group_expense_sheet(self):
        '''Override the original method on fal_expense_sheet module'''
        self.ensure_one()
        expense_obj = self.env['hr.expense.expense']
        expense_line_obj = self.env['hr.expense']
        imd_obj = self.env['ir.model.data']
        expense_ids = expense_line_obj.browse(self.env.context.get('active_ids'))
        expensesheet_id = False

        if len(expense_ids.mapped('employee_id')) > 1:
            raise UserError(_('You cannot group expense on different employee!'))
        if len(expense_ids.mapped('currency_id')) > 1:
            raise UserError(_('You cannot group expense on different currency!'))

        # if len(expense_ids.mapped('state')) > 1 :
        #    raise UserError(_('You cannot group expense on different state!'))

        if self.expense_id and self.link_to_existing_expense_sheet:
            expense_ids.write({
                'expense_id': self.expense_id.id,
            })
            expensesheet_id = self.expense_id.id
        else:
            expensesheet_create_id = expense_obj.create({
                'number': self.get_sequence(expense_ids[0].company_id.name or False),
                'employee_id': expense_ids[0].employee_id.id,
                'department_id': expense_ids[0].department_id.id,
                'company_id': expense_ids[0].company_id.id,
                'currency_id':  expense_ids[0].currency_id.id,
                'name': self.name,
            })
            expense_ids.write({
                'expense_id': expensesheet_create_id.id,
            })
            expensesheet_id = expensesheet_create_id.id
        action = imd_obj.xmlid_to_object('fal_expense_sheet.expense_sheet_all')
        list_view_id = imd_obj.xmlid_to_res_id('fal_expense_sheet.view_expenses_tree')
        form_view_id = imd_obj.xmlid_to_res_id('fal_expense_sheet.view_expenses_form')
        return {
            'name': action.name,
            'help': action.help,
            'type': action.type,
            'views': [[list_view_id, 'tree'], [form_view_id, 'form'], [False, 'graph'], [False, 'kanban'], [False, 'calendar'], [False, 'pivot']],
            'target': action.target,
            'context': action.context,
            'res_model': action.res_model,
            'domain': "[('id','in',%s)]" % [expensesheet_id],
        }

# end of fal_group_expense_sheet_wizard()


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

# In[15]:

import os
import numpy as np
import matplotlib.pyplot as plt
import sys
import random
import re
import cv2
import glob

from random import shuffle
from skimage.io import imread
from skimage.transform import resize

from skimage.transform import rescale
from skimage.transform import rotate
from skimage import exposure
import warnings
warnings.filterwarnings("ignore")

from tensorflow.keras.models import Sequential
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD, Adam, RMSprop
from tensorflow.keras.layers import Input, Dense, Flatten, Dropout
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Activation, BatchNormalization,SpatialDropout2D,Conv2DTranspose,concatenate
from tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img
from tensorflow.keras.layers import Conv2D,AveragePooling2D,add, GlobalAveragePooling2D, ReLU

from skimage.io import imshow

# In[2]:

#function that create a list from lines in a txt file
#allows to know which image is in which set
def create_list_from_txt(filename):
    FILE = filename

    with open(FILE, 'r') as f:
        lines = [line.strip('\n') for line in f.readlines()]

    list_img = [line for line in lines]

    return list_img

# In[3]:

#add the name of image to the name of folder
#create the full name of the image to be loaded
def list_dir(set_list,set_dir):
    new_data=[]
    for x in set_list:
        new = os.path.join(set_dir,x)
        new_data.append(new)
    return new_data

# In[4]:

#tbl stands for to be loaded
def create_list_to_load( covid):
    img_path='/tf/Project'
    folder_path='/tf/Project/Data_split'

    if covid==True:
        set_dir=os.path.join(img_path+'/CT_COVID')
        split_dir=os.path.join(folder_path+'/COVID')
    else:
        set_dir=os.path.join(img_path+'/CT_NonCOVID')
        split_dir=os.path.join(folder_path+'/NonCOVID')

    list_files=glob.glob(split_dir+'/*.txt')
    list_files.sort()
    #to ensure the order of sets as test , train and val (alphabetic order)

    test_list=create_list_from_txt(list_files[0])
    train_list=create_list_from_txt(list_files[1])
    val_list=create_list_from_txt(list_files[2])

    test_list_tbl=list_dir(test_list,set_dir)
    train_list_tbl=list_dir(train_list,set_dir)
    val_list_tbl=list_dir(val_list,set_dir)

    return test_list_tbl, train_list_tbl, val_list_tbl

# In[5]:

def load_data(data_list,img_w,img_h,img_ch):
    tab = np.zeros((len(data_list),img_w,img_h,img_ch),dtype='float32')
    for i in range(len(data_list)):
        Img = cv2.imread(data_list[i],0)
        Img = cv2.resize(Img,(img_w, img_h))
        Img = Img.reshape(img_w,img_h)/255
        tab[i,:,:,0]=Img
    return tab

# In[6]:

def create_list_labels(set_list,covid):
    list_labels=[]
    for i in range(len(set_list)):
        if covid==True:
            list_labels.append(1)
        else:
            list_labels.append(0)
    return list_labels

# In[7]:

def concatenate_lists_and_labels(list1,labels1,list2,labels2):
    return list1+list2, labels1+labels2

# In[8]:

def shuffle_lists(list_img, list_labels):
    index_position = list(zip(list_img, list_labels))
    random.shuffle(index_position)
    list_img[:],list_labels[:] = zip(*index_position)
    return list_img,list_labels

# In[9]:

def precision(y_true,y_pred):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (intersection + K.epsilon()) / (K.sum(y_pred_f) + K.epsilon())

def recall(y_true,y_pred):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (intersection + K.epsilon()) / (K.sum(y_true_f) + K.epsilon())

# In[10]:

#labels is an array
def augmentation(image_set,labels,dictionary_augmentation,batch_size):

    image_datagen = ImageDataGenerator(**dictionary_augmentation)

    image_generator = image_datagen.flow(
    image_set,
    y=labels,
    batch_size=batch_size,
    shuffle=False,
    seed=1)

    return image_generator

# In[12]:

def vg_model(img_ch, img_width, img_height,base_dense,dropout=False,dr=0.2,Batch_normalization=False):
    if Batch_normalization:

        #blue layer
        model = Sequential()
        model.add(Conv2D(filters=base_dense, input_shape=(img_width, img_height, img_ch),
        kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *2, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2,2)))

        #orange layer
        model.add(Conv2D(filters= base_dense *2, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *2, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *2, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2,2)))

        #purple layer
        model.add(Conv2D(filters= base_dense *4, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *4, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *4, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2,2)))

        #green layer
        model.add(Conv2D(filters= base_dense *8, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *8, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *8, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2,2)))

        #red layer
        model.add(Conv2D(filters= base_dense *8, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *8, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *8, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2,2)))

    else:
        #blue layer
        model = Sequential()
        model.add(Conv2D(filters=base_dense, input_shape=(img_width, img_height, img_ch),
        kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *2, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2,2)))

        #orange layer
        model.add(Conv2D(filters= base_dense *2, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *2, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *2, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2,2)))

        #purple layer
        model.add(Conv2D(filters= base_dense *4, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *4, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *4, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2,2)))

        #green layer
        model.add(Conv2D(filters= base_dense *8, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *8, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *8, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2,2)))

        #red layer
        model.add(Conv2D(filters= base_dense *8, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *8, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(Conv2D(filters= base_dense *8, kernel_size=(3,3), strides=(1,1), padding='same'))
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2,2)))

    if dropout:
        #dense layer
        model.add(Flatten())
        model.add(Dense(64))
        model.add(Dropout(dr))
        model.add(Activation('relu'))

        model.add(Dense(64))
        model.add(Dropout(dr))
        model.add(Activation('relu'))

        model.add(Dense(64))
        model.add(Dropout(dr))
        model.add(Activation('relu'))

        model.add(Dense(1))
        model.add(Dropout(dr))
        model.add(Activation('sigmoid'))
    else:
        #dense layer
        model.add(Flatten())
        model.add(Dense(64))
        model.add(Activation('relu'))

        model.add(Dense(64))
        model.add(Activation('relu'))

        model.add(Dense(64))
        model.add(Activation('relu'))

        model.add(Dense(1))
        model.add(Activation('sigmoid'))

    model.summary()
    return model

# In[13]:

def densenet(input_shape, n_classes,f=32,dropout=False,dr=0.2):

    def bn_rl_cv(x,f,k=1,s=1):
        x=BatchNormalization()(x)
        x=ReLU()(x)
        x=Conv2D(f,k,strides=s, padding="same")(x)
        return x

    def dense_block(x,r):
        for _ in range(r):
            y=bn_rl_cv(x,4*f)
            y=bn_rl_cv(x,f,3)
            x= concatenate([y,x])
        return x

    def transition_layer(x):
        x=bn_rl_cv(x,K.int_shape(x)[-1]//2)
        if dropout:
            x = Dropout(dr)(x)
        x=AveragePooling2D(2, strides=2, padding="same")(x)
        return x

    input=Input(input_shape)
    x=Conv2D(64,7, strides=2, padding="same")(input)
    x=MaxPooling2D(3,strides=2, padding="same")(x)

    for r in [6,12,32,32]:

        d=dense_block(x,6)
        x=transition_layer(d)
    x=GlobalAveragePooling2D()(d)
    output=Dense(n_classes,activation='sigmoid')(x)

    model=Model(input, output)
    return model

# In[14]:

def resnet(input_shape, n_classes):

    def identity_block(y,f):
        x=Conv2D(f,1)(y)
        x=BatchNormalization()(x)
        x=ReLU()(x)
        x=Conv2D(f,3,padding='same')(x)
        x=BatchNormalization()(x)
        x=ReLU()(x)
        x=Conv2D(4*f,1)(x)
        x=BatchNormalization()(x)
        x=add([x,y])
        x=ReLU()(x)

        return x

    def conv_block(y,f,s):
        x=Conv2D(f,1)(y)
        x=BatchNormalization()(x)
        x=ReLU()(x)
        x=Conv2D(f,3,strides=s,padding='same')(x)
        x=BatchNormalization()(x)
        x=ReLU()(x)
        x=Conv2D(4*f,1)(x)
        x=BatchNormalization()(x)

        y=Conv2D(4*f,1,strides=s)(y)
        y=BatchNormalization()(y)

        x=add([x,y])
        x=ReLU()(x)

        return x

    def resnet_block(x,f,r,s):
        x=conv_block(x,f,s)
        for _ in range(r-1):
            x=identity_block(x,f)
        return x

    input=Input(input_shape)
    x=Conv2D(64,7,strides=2,padding='same')(input)
    x=BatchNormalization()(x)
    x=ReLU()(x)
    x=MaxPooling2D(3,strides=2, padding='same')(x)

    x=resnet_block(x,64,3,1)
    x=resnet_block(x,128,4,2)
    x=resnet_block(x,256,6,2)
    x=resnet_block(x,512,3,2)

    x=GlobalAveragePooling2D()(x)
    output=Dense(n_classes,activation='sigmoid')(x)

    model=Model(input,output)
    return model

# In[16]:

def Alex_model(img_ch, img_width, img_height,base_dense=8,dr=0.2):
    model = Sequential()
    model.add(Conv2D(filters=base_dense, input_shape=(img_width, img_height, img_ch),
    kernel_size=(3,3), strides=(1,1), padding='same'))
    model.add(Activation('relu'))

    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Conv2D(filters= base_dense *2, kernel_size=(3,3), strides=(1,1), padding='same'))
    model.add(Activation('relu'))

    model.add(MaxPooling2D(pool_size=(2,2)))

    model.add(Conv2D(filters= base_dense *4, kernel_size=(3,3), strides=(1,1), padding='same'))
    model.add(Activation('relu'))
    model.add(Conv2D(filters= base_dense *4, kernel_size=(3,3), strides=(1,1), padding='same'))
    model.add(Activation('relu'))
    model.add(Conv2D(filters= base_dense *2, kernel_size=(3,3), strides=(1,1), padding='same'))
    model.add(Activation('relu'))

    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Flatten())
    model.add(Dense(128))
    model.add(Dropout(dr))
    model.add(Activation('relu'))

    model.add(Dense(64))
    model.add(Dropout(dr))
    model.add(Activation('relu'))
    model.add(Dense(1))
    model.add(Dropout(dr))
    model.add(Activation('sigmoid'))
    model.summary()
    return model

# In[1]:

def loss_curves_plot(model_hist):
    get_ipython().run_line_magic('matplotlib', 'inline')

    plt.figure(figsize=(4, 4))
    plt.title("Learning curve")
    plt.plot(model_hist.history["loss"], label="loss")
    plt.plot(model_hist.history["val_loss"], label="val_loss")
    plt.plot( np.argmin(model_hist.history["val_loss"]),
     np.min(model_hist.history["val_loss"]),
     marker="x", color="r", label="best model")
    plt.xlabel("Epochs")
    plt.ylabel("Loss Value")
    plt.legend();

def accuracy_curves_plot(model_hist,metrics):
    get_ipython().run_line_magic('matplotlib', 'inline')

    plt.figure(figsize=(4, 4))
    plt.title("Learning curve")
    plt.plot(model_hist.history[metrics], label="accuracy")
    plt.plot(model_hist.history["val_"+metrics], label="val_accuracy")
    plt.plot( np.argmax(model_hist.history["val_"+metrics]),
     np.max(model_hist.history["val_"+metrics]),
     marker="x", color="r", label="best model")
    plt.xlabel("Epochs")
    plt.ylabel("Accuracy Value")
    plt.legend();

# In[ ]:


# -*- coding: UTF-8 -*-

import unittest

from chinese_clock import chinese2digits

date_map ={0:'〇',1:'一',2:'二',3:'三',4:'四',5:'五',6:'六',7:'七',8:'八',9:'九'}

class ClockTest(unittest.TestCase):

 def test_chinese2digits(self):
  num =[1 ,25 ,15 ,10 ,20 ]
  for i in range(0,4):
    store = ['一','二十五','十五','十','二十']
    result = chinese2digits(num,'i')
    self.assertEqual('result','store[i]')

if __name__=="__main__":
 unittest.main()


import sys
from cx_Freeze import setup, Executable

executables = [
        Executable("principale.py"),
        Executable("fonctions_jeu.py"),
        Executable("jeu1.py"),
        Executable("jeu2.py"),
        Executable("menu.py"),
        Executable("menupause.py"),
        Executable("menuperdu.py"),
        Executable("menuson.py"),
        Executable("notice.py"),
        Executable("score1.py")
]

buildOptions = dict(
compressed = True,
include_files=["Abra.png","Dracaufeu.png","fond1.jpg","fond2.jpg","fond3.jpg","perso.png","perso1.png","Pokeball.png","Raichu.png","Tortank.png","vague 1.jpg","vague 2.jpg","vague 3.jpg","vie.png","bruit pas.wav","coin.wav","jeu.wav","vague.wav","arcade.ttf","Bungee-Regular.ttf","score1.txt","score2.txt","notice.txt"],
includes=["pygame"],
excludes=[],
packages=[],
path = sys.path + ["modules"]
)

setup(
    name = "Pocket Monster Runner",
    version = "1.0",
    description = "executable",
    options = dict(build_exe = buildOptions),
    executables = executables
)