"""
DOCSTRING
"""
import datetime
#import google.cloud.storage
#import google.oauth2.service_account
#import googleapiclient.discovery
#import googleapiclient.errors
import logging
import math
import numpy
import os
import pprint
import scipy.misc
import tensorflow
import time

logging.basicConfig(level=logging.INFO)
logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR)

class GCloud:
    """
    DOCSTRING
    """
    def __init__(self):
        self.bucket_name = 'pokegan-data'
        self.json_path = 'My Project 58923-14efe3d8f5f7.json'
        self.package_name = 'pokegan-0.0.0.tar.gz'
        self.project_id = 'ivory-hallway-204216'

    def create_bucket(storage_client):
        """
        DOCSTRING
        """
        logging.info('bucket:creating')
        if storage_client.lookup_bucket(self.bucket_name):
            logging.info('bucket:already exists')
        else:
            storage_client.create_bucket(self.bucket_name)
            logging.info('bucket:created')
        return storage_client.bucket(self.bucket_name)

    def create_job():
        """
        DOCSTRING
        """
        logging.info('job:creating')
        credentials = \
            google.oauth2.service_account.Credentials.from_service_account_file(self.json_path)
        ml = googleapiclient.discovery.build('ml', 'v1', credentials=credentials)
        project_path = 'projects/{}'.format(self.project_id)
        training_input = {
            'jobDir': 'gs://{}/model'.format(self.bucket_name),
            'packageUris': ['gs://{}/{}'.format(self.bucket_name, self.package_name)],
            'pythonModule': 'trainer.task',
            'region': 'us-central1',
            'runtimeVersion': '1.6',
            'scaleTier': 'BASIC'}
        now = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
        job_specs = {'jobId': 'pokegan_' + now, 'trainingInput': training_input}
        request = ml.projects().jobs().create(parent=project_path, body=job_specs)
        try:
            request.execute()
            logging.info('job:created')
        except googleapiclient.errors.HttpError as err:
            logging.error('job:{}'.format(err._get_reason()))

    def delete_bucket():
        """
        DOCSTRING
        """
        logging.info('bucket:deleting')
        credentials = \
            google.oauth2.service_account.Credentials.from_service_account_file(self.json_path)
        storage_client = google.cloud.storage.Client(self.project_id, credentials)
        for bucket in storage_client.list_buckets():
            storage_client.delete(bucket, force=True)
        logging.info('bucket:deleted')

    def upload_data():
        """
        DOCSTRING
        """
        src_dir = 'data/rgb_images'
        credentials = \
            google.oauth2.service_account.Credentials.from_service_account_file(self.json_path)
        storage_client = google.cloud.storage.Client(self.project_id, credentials)
        bucket = create_bucket(self.bucket_name, storage_client)
        logging.info('training data:uploading')
        for filename in os.listdir(src_dir):
            logging.info('training data:uploading:{}'.format(filename))
            blob = google.cloud.storage.Blob('data/' + filename, bucket)
            blob.upload_from_filename(os.path.join(src_dir, filename))
        logging.info('training data:uploaded')

    def upload_package():
        """
        DOCSTRING
        """
        credentials = \
            google.oauth2.service_account.Credentials.from_service_account_file(self.json_path)
        storage_client = google.cloud.storage.Client(self.project_id, credentials)
        bucket = create_bucket(self.bucket_name, storage_client)
        logging.info('package:uploading')
        blob = google.cloud.storage.Blob(self.package_name, bucket)
        blob.upload_from_filename(os.path.join('dist', self.package_name))
        logging.info('package:uploaded')

class Model:
    """
    Generate new kinds of Pokémon using a Wasserstein generative adversarial network.
    """
    def __init__(self):
        self.batch_size = 64
        self.channel = 3
        self.height = 128
        self.width = 128

    def discriminator(self, input, is_train, reuse=False):
        """
        DOCSTRING
        """
        channel_2, channel_4, channel_8, channel_16 = 64, 128, 256, 512
        with tensorflow.variable_scope('dis') as scope:
            if reuse:
                scope.reuse_variables()
            conv1 = tensorflow.layers.conv2d(
                input, channel_2,
                kernel_size=[5, 5],
                strides=[2, 2],
                padding="SAME",
                kernel_initializer=tensorflow.truncated_normal_initializer(stddev=0.02),
                name='conv1')
            bn1 = tensorflow.contrib.layers.batch_norm(
                conv1,
                is_training=is_train,
                epsilon=1e-5,
                decay = 0.9,
                updates_collections=None,
                scope = 'bn1')
            act1 = lrelu(conv1, n='act1')
            conv2 = tensorflow.layers.conv2d(
                act1, channel_4,
                kernel_size=[5, 5],
                strides=[2, 2],
                padding="SAME",
                kernel_initializer=tensorflow.truncated_normal_initializer(stddev=0.02),
                name='conv2')
            bn2 = tensorflow.contrib.layers.batch_norm(
                conv2,
                is_training=is_train,
                epsilon=1e-5,
                decay=0.9,
                updates_collections=None,
                scope='bn2')
            act2 = lrelu(bn2, n='act2')
            conv3 = tensorflow.layers.conv2d(
                act2, channel_8,
                kernel_size=[5, 5],
                strides=[2, 2],
                padding="SAME",
                kernel_initializer=tensorflow.truncated_normal_initializer(stddev=0.02),
                name='conv3')
            bn3 = tensorflow.contrib.layers.batch_norm(
                conv3,
                is_training=is_train,
                epsilon=1e-5,
                decay=0.9,
                updates_collections=None,
                scope='bn3')
            act3 = lrelu(bn3, n='act3')
            conv4 = tensorflow.layers.conv2d(
                act3, channel_16,
                kernel_size=[5, 5],
                strides=[2, 2],
                padding="SAME",
                kernel_initializer=tensorflow.truncated_normal_initializer(stddev=0.02),
                name='conv4')
            bn4 = tensorflow.contrib.layers.batch_norm(
                conv4,
                is_training=is_train,
                epsilon=1e-5,
                decay=0.9,
                updates_collections=None,
                scope='bn4')
            act4 = lrelu(bn4, n='act4')
            dim = int(numpy.prod(act4.get_shape()[1:]))
            fc1 = tensorflow.reshape(act4, shape=[-1, dim], name='fc1')
            w2 = tensorflow.get_variable(
                'w2', shape=[fc1.shape[-1], 1], dtype=tensorflow.float32,
                initializer=tensorflow.truncated_normal_initializer(stddev=0.02))
            b2 = tensorflow.get_variable(
                'b2', shape=[1], dtype=tensorflow.float32,
                initializer=tensorflow.constant_initializer(0.0))
            logits = tensorflow.add(tensorflow.matmul(fc1, w2), b2, name='logits')
            return logits

    def generator(input, random_dim, is_train, reuse=False):
        """
        DOCSTRING
        """
        channel_4, channel_8, channel_16, channel_32, channel_64 = 512, 256, 128, 64, 32
        s4 = 4
        output_dim = self.channel
        with tensorflow.variable_scope('gen') as scope:
            if reuse:
                scope.reuse_variables()
            w1 = tensorflow.get_variable(
                'w1',
                shape=[random_dim, s4*s4*channel_4],
                dtype=tensorflow.float32,
                initializer=tensorflow.truncated_normal_initializer(stddev=0.02))
            b1 = tensorflow.get_variable(
                'b1',
                shape=[channel_4*s4*s4],
                dtype=tensorflow.float32,
                initializer=tensorflow.constant_initializer(0.0))
            flat_conv1 = tensorflow.add(
                tensorflow.matmul(input, w1), b1, name='flat_conv1')
            conv1 = tensorflow.reshape(
                flat_conv1, shape=[-1, s4, s4, channel_4], name='conv1')
            bn1 = tensorflow.contrib.layers.batch_norm(
                conv1,
                is_training=is_train,
                epsilon=1e-5,
                decay=0.9,
                updates_collections=None,
                scope='bn1')
            act1 = tensorflow.nn.relu(bn1, name='act1')
            conv2 = tensorflow.layers.conv2d_transpose(
                act1, channel_8,
                kernel_size=[5, 5],
                strides=[2, 2],
                padding="SAME",
                kernel_initializer=tensorflow.truncated_normal_initializer(stddev=0.02),
                name='conv2')
            bn2 = tensorflow.contrib.layers.batch_norm(
                conv2,
                is_training=is_train,
                epsilon=1e-5,
                decay=0.9,
                updates_collections=None, scope='bn2')
            act2 = tensorflow.nn.relu(bn2, name='act2')
            conv3 = tensorflow.layers.conv2d_transpose(
                act2, channel_16,
                kernel_size=[5, 5],
                strides=[2, 2],
                padding="SAME",
                kernel_initializer=tensorflow.truncated_normal_initializer(stddev=0.02),
                name='conv3')
            bn3 = tensorflow.contrib.layers.batch_norm(
                conv3,
                is_training=is_train,
                epsilon=1e-5,
                decay=0.9,
                updates_collections=None,
                scope='bn3')
            act3 = tensorflow.nn.relu(bn3, name='act3')
            conv4 = tensorflow.layers.conv2d_transpose(
                act3, channel_32,
                kernel_size=[5, 5],
                strides=[2, 2],
                padding="SAME",
                kernel_initializer=tensorflow.truncated_normal_initializer(stddev=0.02),
                name='conv4')
            bn4 = tensorflow.contrib.layers.batch_norm(
                conv4,
                is_training=is_train,
                epsilon=1e-5,
                decay = 0.9,
                updates_collections=None,
                scope='bn4')
            act4 = tensorflow.nn.relu(bn4, name='act4')
            conv5 = tensorflow.layers.conv2d_transpose(
                act4, channel_64,
                kernel_size=[5, 5],
                strides=[2, 2],
                padding="SAME",
                kernel_initializer=tensorflow.truncated_normal_initializer(stddev=0.02),
                name='conv5')
            bn5 = tensorflow.contrib.layers.batch_norm(
                conv5,
                is_training=is_train,
                epsilon=1e-5,
                decay=0.9,
                updates_collections=None,
                scope='bn5')
            act5 = tensorflow.nn.relu(bn5, name='act5')
            conv6 = tensorflow.layers.conv2d_transpose(
                act5,
                output_dim,
                kernel_size=[5, 5],
                strides=[2, 2],
                padding="SAME",
                kernel_initializer=tensorflow.truncated_normal_initializer(stddev=0.02),
                name='conv6')
            act6 = tensorflow.nn.tanh(conv6, name='act6')
            return act6

    def lrelu(self, x, n, leak=0.2):
        """
        DOCSTRING
        """
        return tensorflow.maximum(x, leak * x, name=n)

    def process_data(self):
        """
        DOCSTRING
        """
        images = list()
        for file_name in os.listdir('data'):
            images.append(os.path.join('data', file_name))
        logging.debug(images)
        images = tensorflow.convert_to_tensor(images, dtype=tensorflow.string)
        images_queue = tensorflow.train.slice_input_producer([images])
        content = tensorflow.read_file(images_queue[0])
        image = tensorflow.image.decode_jpeg(content, channels=self.channel)
        with tensorflow.Session() as sess_1:
            logging.debug(sess_1.run(image))
        image = tensorflow.image.random_flip_left_right(image)
        image = tensorflow.image.random_brightness(image, max_delta=0.1)
        image = tensorflow.image.random_contrast(image, lower=0.9, upper=1.1)
        logging.debug(image.get_shape())
        size = [self.height, self.width]
        image = tensorflow.image.resize_images(image, size)
        image.set_shape([self.height, self.width, self.channel])
        logging.debug(image.get_shape())
        image = tensorflow.cast(image, tensorflow.float32)
        image = image / 255.0
        images_batch = tensorflow.train.shuffle_batch(
            [image], self.batch_size,
            capacity=200 + 3 * self.batch_size,
            min_after_dequeue=200, num_threads=4)
        num_images = len(images)
        return images_batch, num_images

    def task(self):
        """
        Trains a W-GAN to create new Pokemon.
        """
        random_dim = 100
        with tensorflow.variable_scope('input'):
            real_image = tensorflow.placeholder(
                tensorflow.float32,
                [None, self.height, self.width, self.channel],
                'real_image')
            random_input = tensorflow.placeholder(
                tensorflow.float32,
                [None, self.random_dim],
                'rand_input')
            is_train = tensorflow.placeholder(tensorflow.bool, name='is_train')
        # w-gan
        fake_image = model.generator(random_input, self.random_dim, is_train)
        real_result = model.discriminator(real_image, is_train)
        fake_result = model.discriminator(fake_image, is_train, reuse=True)
        d_loss = tensorflow.reduce_mean(fake_result) - tensorflow.reduce_mean(real_result)
        g_loss = -tensorflow.reduce_mean(fake_result)
        t_vars = tensorflow.trainable_variables()
        d_vars = [var for var in t_vars if 'dis' in var.name]
        g_vars = [var for var in t_vars if 'gen' in var.name]
        logging.debug(d_vars)
        learning_rate = 2e-4
        trainer_d = tensorflow.train.RMSPropOptimizer(
            learning_rate).minimize(d_loss, var_list=d_vars)
        trainer_g = tensorflow.train.RMSPropOptimizer(
            learning_rate).minimize(g_loss, var_list=g_vars)
        # clip discriminator weights
        d_clip = [v.assign(tensorflow.clip_by_value(v, -0.01, 0.01)) for v in d_vars]
        batch_size = self.batch_size
        image_batch, samples_num = model.process_data()
        batch_num = int(samples_num / batch_size)

    def test(self):
        """
        DOCSTRING
        """
        random_dim = 100
        with tensorflow.variable_scope('input'):
            real_image = tensorflow.placeholder(
                tensorflow.float32,
                shape=[None, self.height, self.width, self.channel],
                name='real_image')
            random_input = tensorflow.placeholder(
                tensorflow.float32,
                shape=[None, random_dim],
                name='rand_input')
            is_train = tensorflow.placeholder(tensorflow.bool, name='is_train')
        # w-gan
        fake_image = generator(random_input, random_dim, is_train)
        real_result = discriminator(real_image, is_train)
        fake_result = discriminator(fake_image, is_train, reuse=True)
        sess = tensorflow.InteractiveSession()
        sess.run(tensorflow.global_variables_initializer())
        variables_to_restore = tensorflow.contrib.slim.get_variables_to_restore(include=['gen'])
        print(variables_to_restore)
        saver = tensorflow.train.Saver(variables_to_restore)
        ckpt = tensorflow.train.latest_checkpoint('model/' + version)
        saver.restore(sess, ckpt)

class Utils:
    """
    DOCSTRING
    """
    def __init__(self):
        pretty_printer = pprint.PrettyPrinter()
        get_stddev = lambda x, k_h, k_w: 1 / math.sqrt(k_w * k_h * x.get_shape()[-1])

    def center_crop(x, crop_h, crop_w, resize_h=64, resize_w=64):
        """
        DOCSTRING
        """
        if crop_w is None:
            crop_w = crop_h
        h, w = x.shape[:2]
        j = int(round((h - crop_h) / 2.0))
        i = int(round((w - crop_w) / 2.0))
        return scipy.misc.imresize(
            x[j: j+crop_h, i: i+crop_w], [resize_h, resize_w])

    def get_image(
        image_path,
        input_height,
        input_width,
        resize_height=64,
        resize_width=64,
        crop=True,
        grayscale=False):
        """
        DOCSTRING
        """
        image = imread(image_path, grayscale)
        return transform(
            image, input_height, input_width, resize_height, resize_width, crop)

    def imread(path, grayscale=False):
        """
        DOCSTRING
        """
        if (grayscale):
            return scipy.misc.imread(path, flatten=True).astype(numpy.float)
        else:
            return scipy.misc.imread(path).astype(numpy.float)

    def imsave(images, size, path):
        """
        DOCSTRING
        """
        image = numpy.squeeze(merge(images, size))
        return scipy.misc.imsave(path, image)

    def inverse_transform(images):
        """
        DOCSTRING
        """
        return (images + 1.0) / 2.0

    def make_frame(self, t):
        """
        DOCSTRING
        """
        try:
            x = images[int(len(images) / duration * t)]
        except:
            x = images[-1]
            if true_image:
                return x.astype(numpy.uint8)
        else:
            return ((x + 1) / 2 * 255).astype(numpy.uint8)

    def make_gif(images, fname, duration=2, true_image=False):
        """
        DOCSTRING
        """
        import moviepy.editor
        clip = moviepy.editor.VideoClip(self.make_frame, duration=duration)
        clip.write_gif(fname, fps=len(images) / duration)

    def merge(images, size):
        """
        DOCSTRING
        """
        h, w = images.shape[1], images.shape[2]
        if (images.shape[3] in (3, 4)):
            c = images.shape[3]
            img = numpy.zeros((h * size[0], w * size[1], c))
            for idx, image in enumerate(images):
                i = idx % size[1]
                j = idx // size[1]
                img[j * h: j * h + h, i * w: i * w + w, :] = image
            return img
        elif images.shape[3]==1:
            img = numpy.zeros((h * size[0], w * size[1]))
            for idx, image in enumerate(images):
                i = idx % size[1]
                j = idx // size[1]
                img[j * h: j * h + h, i * w: i * w + w] = image[:,:,0]
            return img
        else:
            raise ValueError('Images parameter must have HxW, HxWx3, or HxWx4')

    def merge_images(images, size):
        """
        DOCSTRING
        """
        return inverse_transform(images)

    def rename_images(self, src_dir, new_prefix):
        """
        DOCSTRING
        """
        for file_name in os.listdir(src_dir):
            os.rename(
                os.path.join(src_dir, file_name),
                os.path.join(src_dir, new_prefix + file_name[5:]))
            print(file_name + ' -> ' + new_prefix + file_name)

    def resize_images(self):
        """
        DOCSTRING
        """
        logging.info('images:resizing')
        src_path = 'data/original_images'
        dst_path = 'data/resized_images'
        if not os.path.exists(dst_path):
            os.mkdir(dst_path)
        for filename in os.listdir(src_path):
            img = PIL.Image.open(os.path.join(src_path, filename))
            img = img.resize((256, 256), PIL.Image.ANTIALIAS)
            img.save(os.path.join(dst_path, filename))
            logging.info('images:resizing:{}'.format(filename))
        logging.info('images:resizing completed')

    def rgba_to_rgb(self):
        """
        DOCSTRING
        """
        logging.info('images:converting to rgb')
        src_path = 'data/resized_images'
        dst_path = 'data/rgb_images'
        if not os.path.exists(dst_path):
            os.mkdir(dst_path)
        for filename in os.listdir(src_path):
            img = PIL.Image.open(os.path.join(src_path, filename))
            if img.mode == 'RGBA':
                logging.info('images:converting to rgb:{}'.format(filename))
                img.load() # required for png.split()
                split_img = PIL.Image.new('RGB', img.size, (0, 0, 0))
                split_img.paste(img, mask=img.split()[3]) # 3 is the alpha channel
                split_img.save(os.path.join(dst_path, filename.split('.')[0] + '.jpg'), 'JPEG')
            else:
                logging.info('images:converting to jpg:{}'.format(filename))
                img = img.convert('RGB')
                img.save(os.path.join(dst_path, filename.split('.')[0] + '.jpg'), 'JPEG')
        logging.info('images:conversion completed')

    def save_images(images, size, image_path):
        """
        DOCSTRING
        """
        return imsave(inverse_transform(images), size, image_path)

    def show_all_variables():
        """
        DOCSTRING
        """
        model_vars = tensorflow.trainable_variables()
        tensorflow.contrib.slim.model_analyzer.analyze_vars(model_vars, print_info=True)

    def to_json(output_path, *layers):
        """
        DOCSTRING
        """
        with open(output_path, "w") as layer_f:
            lines = ""
            for w, b, bn in layers:
                layer_idx = w.name.split('/')[0].split('h')[1]
                B = b.eval()
                if "lin/" in w.name:
                    W = w.eval()
                    depth = W.shape[1]
                else:
                    W = numpy.rollaxis(w.eval(), 2, 0)
                    depth = W.shape[0]
                biases = {
                    "sy": 1, "sx": 1, "depth": depth,
                    "w": ['%.2f' % elem for elem in list(B)]}
                if bn != None:
                    gamma = bn.gamma.eval()
                    beta = bn.beta.eval()
                    gamma = {
                        "sy": 1, "sx": 1, "depth": depth,
                        "w": ['%.2f' % elem for elem in list(gamma)]}
                    beta = {
                        "sy": 1, "sx": 1, "depth": depth,
                        "w": ['%.2f' % elem for elem in list(beta)]}
                else:
                    gamma = {"sy": 1, "sx": 1, "depth": 0, "w": []}
                    beta = {"sy": 1, "sx": 1, "depth": 0, "w": []}
                if "lin/" in w.name:
                    fs = list()
                    for w in W.T:
                        fs.append(
                            {"sy": 1,
                             "sx": 1,
                             "depth": W.shape[0],
                             "w": ['%.2f' % elem for elem in list(w)]})
                    lines += \
                        """
                        var layer_%s = {
                            "layer_type": "fc",
                            "sy": 1, "sx": 1,
                            "out_sx": 1, "out_sy": 1,
                            "stride": 1, "pad": 0,
                            "out_depth": %s, "in_depth": %s,
                            "biases": %s,
                            "gamma": %s,
                            "beta": %s,
                            "filters": %s}
                        """ % (
                            layer_idx.split('_')[0],
                            W.shape[1],
                            W.shape[0],
                            biases,
                            gamma,
                            beta,
                            fs)
                else:
                    fs = list()
                    for w_ in W:
                        fs.append(
                            {"sy": 5,
                             "sx": 5,
                             "depth": W.shape[3],
                             "w": ['%.2f' % elem for elem in list(w_.flatten())]})
                    lines += \
                        """
                        var layer_%s = {
                            "layer_type": "deconv",
                            "sy": 5, "sx": 5,
                            "out_sx": %s, "out_sy": %s,
                            "stride": 2, "pad": 1,
                            "out_depth": %s, "in_depth": %s,
                            "biases": %s,
                            "gamma": %s,
                            "beta": %s,
                            "filters": %s}
                        """ % (
                            layer_idx,
                            2**(int(layer_idx)+2),
                            2**(int(layer_idx)+2),
                            W.shape[0],
                            W.shape[3],
                            biases,
                            gamma,
                            beta,
                            fs)
            layer_f.write(" ".join(lines.replace("'","").split()))

    def transform(
        image,
        input_height,
        input_width,
        resize_height=64,
        resize_width=64,
        crop=True):
        """
        DOCSTRING
        """
        if crop:
            cropped_image = center_crop(
                image, input_height, input_width, resize_height, resize_width)
        else:
            cropped_image = scipy.misc.imresize(image, [resize_height, resize_width])
        return numpy.array(cropped_image) / 127.5 - 1.0

    def visualize(sess, dcgan, config, option):
        """
        DOCSTRING
        """
        image_frame_dim = int(math.ceil(config.batch_size**0.5))
        if option == 0:
            z_sample = numpy.random.uniform(
                -0.5, 0.5, size=(config.batch_size, dcgan.z_dim))
            samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample})
            save_images(
                samples, [image_frame_dim, image_frame_dim],
                'samples/test_%s.png' % time.strftime("%Y%m%d%H%M%S", time.gmtime()))
        elif option == 1:
            values = numpy.arange(0, 1, 1.0 / config.batch_size)
            for idx in range(100):
                print(" [*] %d" % idx)
                z_sample = numpy.zeros([config.batch_size, dcgan.z_dim])
                for kdx, z in enumerate(z_sample):
                    z[idx] = values[kdx]
                if config.dataset == "mnist":
                    y = numpy.random.choice(10, config.batch_size)
                    y_one_hot = numpy.zeros((config.batch_size, 10))
                    y_one_hot[numpy.arange(config.batch_size), y] = 1
                    samples = sess.run(
                        dcgan.sampler, feed_dict={dcgan.z: z_sample, dcgan.y: y_one_hot})
                else:
                    samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample})
                save_images(
                    samples,
                    [image_frame_dim, image_frame_dim],
                    'samples/test_arange_%s.png' % (idx))
        elif option == 2:
            values = numpy.arange(0, 1, 1.0 / config.batch_size)
            for idx in [numpy.random.randint(0, 99) for _ in range(100)]:
                print(" [*] %d" % idx)
                z = numpy.random.uniform(-0.2, 0.2, size=(dcgan.z_dim))
                z_sample = numpy.tile(z, (config.batch_size, 1))
                #z_sample = numpy.zeros([config.batch_size, dcgan.z_dim])
                for kdx, z in enumerate(z_sample):
                    z[idx] = values[kdx]
                if config.dataset == "mnist":
                    y = numpy.random.choice(10, config.batch_size)
                    y_one_hot = numpy.zeros((config.batch_size, 10))
                    y_one_hot[numpy.arange(config.batch_size), y] = 1
                    samples = sess.run(
                        dcgan.sampler, feed_dict={dcgan.z: z_sample, dcgan.y: y_one_hot})
                else:
                    samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample})
                try:
                    make_gif(samples, 'samples/test_gif_%s.gif' % (idx))
                except:
                    save_images(
                        samples,
                        [image_frame_dim, image_frame_dim],
                        'samples/test_%s.png' % time.strftime("%Y%m%d%H%M%S", time.gmtime()))
        elif option == 3:
            values = numpy.arange(0, 1, 1.0 / config.batch_size)
            for idx in range(100):
                print(" [*] %d" % idx)
                z_sample = numpy.zeros([config.batch_size, dcgan.z_dim])
                for kdx, z in enumerate(z_sample):
                    z[idx] = values[kdx]
                samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample})
                make_gif(samples, './samples/test_gif_%s.gif' % (idx))
        elif option == 4:
            image_set = list()
            values = numpy.arange(0, 1, 1.0 / config.batch_size)
            for idx in range(100):
                print(" [*] %d" % idx)
                z_sample = numpy.zeros([config.batch_size, dcgan.z_dim])
                for kdx, z in enumerate(z_sample):
                    z[idx] = values[kdx]
                image_set.append(sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample}))
                make_gif(image_set[-1], 'samples/test_gif_%s.gif' % (idx))
            new_image_set = [merge(
                numpy.array([images[idx] for images in image_set]),
                [10, 10]) for idx in range(64) + range(63, -1, -1)]
            make_gif(new_image_set, 'samples/test_gif_merged.gif', duration=8)

if __name__ == '__main__':
    utils = Utils()
    utils.rename_images('images/original', 'original-')


import sys
import time
rawinput = sys.argv[1]
input = rawinput.replace('.', '0')

pzl = []
for i in range(0, len(input)):
    pzl.append(int(input[i]))

#lookups
lookupset = [{}]*81
rows = [[], [], [], [], [], [], [], [], []]
for i in range(0, 9):
    for j in range(0, 9):
        rows[i].append(i*9+j)
for i in rows:
    for j in i:
        lookupset[j] = set(i)

columns = [[], [], [], [], [], [], [], [], []]
for i in rows:
    for j in range(0, 9):
        columns[j].append(i[j])
for i in columns:
    for j in i:
        lookupset[j].update(set(i))

boxes = [[], [], [], [], [], [], [], [], []]
for i in range(0, 9):
    for j in range(0, 9):
        boxes[i//3 * 3 + j//3].append(rows[i][j])
for i in boxes:
    for j in i:
        lookupset[j].update(set(i))
for i in range(0, len(lookupset)):
    lookupset[i].remove(i)

# lookup = []
# for i in range(0, len(rows)):
#     lookup.append(rows[i])
#     lookup.append(columns[i])
#     lookup.append(boxes[i])
#make lookup set for each box?
#check if sudoku is solved

def pzlInvalid(pzl):
    for i in range(0, len(pzl)):
        if pzl[i] != 0 and pzl[i] in (pzl[j] for j in lookupset[i]):
            return True
    return False

def pzlCompletelySolved(pzl):
    for i in range(0, len(pzl)):
        if (pzl[j] != 0 for j in lookupset[i]) and pzl[i] in (pzl[j] for j in lookupset[i]): ###OMG WHY DOES THIS NOT WORK JESUS CHRIST
            return False
    return True

def bruteForce(pzl):
    if pzlInvalid(pzl): return ''
    if pzlCompletelySolved(pzl): return pzl

    emptyIndex = pzl.index(0)
    for i in range(1, 10):
        subPzl = pzl[:]
        subPzl[emptyIndex] = i
        bF = bruteForce(subPzl)
        if bF: return bF
    return ''

def printSpecial(pzl):
    for i in range(0, 9):
        print(pzl[i*9], pzl[i*9 + 1], pzl[i*9 + 2], '\t',
              pzl[i*9 + 3], pzl[i*9 + 4], pzl[i*9 + 5], '\t',
              pzl[i*9 + 6], pzl[i*9 + 7], pzl[i*9 + 8])
        if (i+1)%3 == 0:
            print()

print('\nPuzzle:')
emptyinput = rawinput.replace('.', '_')
emptypzl = ['']*81
for i in range(0, len(emptyinput)-1):
    emptypzl[i] = emptyinput[i]
printSpecial(emptypzl)
print('Solution:')
starttime = time.time()
result = bruteForce(pzl)
endtime = time.time()
if result == '': print('No solution')
else: printSpecial(result)
print('Time:', endtime-starttime)


from constants import *

name = 'Белое зелье'

description = (
	'Пробирка с каким-то зельем'
)

price = 100
usable = True
disposable = True

def on_use(user, reply):
	reply('Теперь ты Булыжник.', photo='BQADAgAD8gADDLXzAxLUH2ng6NIiAg')

	user.reborn(reply, 'Булыжник должен лежать', name='Булыжник')


import numpy as np
import random
from collections import Counter

def options():
    options = {'r90': (0, 1),
               'r180': (0, 2),
               'r270': (0, 3),
               'm': (1, 0),
               'mr90': (1, 1),
               'mr180': (1, 2),
               'mr270': (1, 3)
              }
    return options

def mirror_rotate_options():
    options = [(0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3)]
    return options

def gen_mirrot_sequence(length):

    ops = options()

    seq = np.empty(length)

    return seq

def mirrot(array, mir=0, rot=0, transx=0, transy=0):
    """
    Mirrors and/or rotates an image array

    Parameters
    ----------

    array : 2D array

    mir : int

    rot : int

    transx : int

    transy : int

    Returns
    -------
    mirrot_array : 2D array
        The mirrored and rotated array
    """

    if mir == 0:
        mirrot_array = array
    elif mir == 1:
        mirrot_array = np.fliplr(array)
    else:
        raise ValueError("Value for mir is not 0 or 1")

    if rot <= 3:
        mirrot_array = np.rot90(mirrot_array, rot)
    else:
        raise ValueError("Value for rot is not 0, 1, 2 or 3")

    return mirrot_array

def perform_transform(array, transformation=(0, 0, 0, 0)):
    """
    Performs mirror, rotation and translation of array in that order

    Parameters
    ----------
    array: 2D numpy.ndarray
        A 2D array to transform

    transformation: tuple of ints
        A tuple containing 4 integers representing the transformation. Each
        tuple represents: (mirror, rotate, translate_x, translate_y).
        mirror can be either 0 or 1 (no mirror and mirror around vertical axis
        respectively). rotate can be 0, 1, 2 or 3 (for 0, 90, 180 and 270 deg
        rotation respectfully), translate_x and translate_y are the number of
        positions to translate along the x and y axes.
        Default:(0, 0, 0, 0)
    """

    # Check the transformation tuples contains the correct number of values.
    if len(transformation) is not 4:
        raise ValueError("""transformation is expecting a length 4 tuple.
            Instead got {}""".format(transformation))

    # Ensure that all the values in the transformation tuple are integers.
    for val in transformation:
        if type(val) is not int:
            raise ValueError("""transformation needs to be a tuple of ints.
                Instead tuple contains value of type: {}""".format(type(val)))

    # Extract the transformation values from the tuple
    mir, rot, transx, transy = transformation

    # Peform the mirroring of the array
    if mir == 0:
        pass
    elif mir == 1:
        array = _mirror(array)
    else:
        raise ValueError("""Value for mirror must be 0 or 1. Instead recieved
            mirror value: {0}""".format(mir))

    # Peform the rotation of the array
    if rot <= 3:
        array = _rotate(array, rot)
    else:
        raise ValueError("""Value for rotation must be an integer less than 3.
            Instead recieved rotation value: {0}""".format(rot))

    # Peform the translation of the array
    array = _translate(array, transx, transy)

    return array

def _mirror(array):
    """
    Mirrors a 2D numpy array around the vertical axis

    Parameters
    ----------
    array: 2D numpy.ndarray
        Input array, must be 2D.

    Returns
    -------
    numpy.ndarray
        Output array, mirrored with the same shape as input array.
    """
    mirrored_array = np.fliplr(array)
    return mirrored_array

def _rotate(array, rot=0):
    """
    Rotate a 2D numpy array anti-clockwise in a multiple of 90 degrees.

    Parameters
    ----------
    array: 2D numpy.ndarray
        Input array, must be 2D.

    rot: int
        The multiple of 90 degrees with which to rotate the array. i.e rot=2
        implies a 2 x 90 = 180 degree rotation. Default: 0

    Returns
    -------
    numpy.ndarray
        Output array, rotated with the same shape as input array.
    """
    rotated_array = np.rot90(array, rot)
    return rotated_array

def _translate(array, transx, transy):
    """
    Translates a 2D array in the x and y directions

    Parameters
    ----------
    array: 2D numpy.ndarray
        Input array, must be 2D.

    transx: int
        The number of positions to translate along the x axis.

    transy: int
        The number of positions to translate along the y axis.

    Returns
    -------
    numpy.ndarray
        Output array, translated with the same shape as input array.
    """
    if array.dim is not 2:
        raise ValueError("Input array must be 2D")

    translated_array = np.roll(array, (transx, transy), axis=(1, 0))
    return translated_array

def gen_random_transform(min_trans, max_trans):
    """
    Generates a random transformation

    Parameters
    ----------
    min_trans: int
        The minimum value for the x and y translations.

    max_trans: int
        The maximum value for the x and y translations

    Returns
    -------
    tuple of ints
        A tuple containing 4 integers representing the transformation. Each
        tuple represents: (mirror, rotate, translate_x, translate_y).
        mirror can be either 0 or 1 (no mirror and mirror around vertical axis
        respectively). rotate can be 0, 1, 2 or 3 (for 0, 90, 180 and 270 deg
        rotation respectfully), translate_x and translate_y are the number of
        positions to translate the array along the x and y axis.

    """
    mir, rot = random.choice(mirror_rotate_options())
    transx = random.randint(min_trans, max_trans)
    transy = random.randint(min_trans, max_trans)

    transform = (mir, rot, transx, transy)

    return transform

def gen_transform_sequence(seq_length, min_trans, max_trans):
    """
    Generates a sequence of transformations of a specified length.

    Parameters
    ----------
    seq_length: int
        The number of transformations to in the sequence to generate.

    min_trans: int
        The minimum value for the x and y translations.

    max_trans: int
        The maximum value for the x and y translations

    Returns
    -------
    list of tuples
        A list containing a sequence of transformations
    """

    if type(seq_length) is not int:
        raise ValueError("""seq_length must be of type int. Instead seq_length
            hase type: {0}""".format(type(seq_length)))

    seq = []
    for i in range(seq_length):
        seq.append(gen_random_transform(min_trans, max_trans))

    return seq


#!/usr/bin/env python

import sklearn as sk
from sklearn.model_selection import StratifiedKFold
import pandas as pd
import numpy as np

# load data
df = pd.read_csv('kaggle-data/diabetes-training.csv')
validation_set = pd.read_csv('kaggle-data/diabetes-problem-2.csv')
validation_key = pd.read_csv('kaggle-data/diabetes-sample-2.csv')
validation = pd.merge(validation_key, validation_set, on='patient_nbr')

# sort data by encounter ID
df = df.sort(columns='encounter_id')

# initialize next encounter column
df['next_encounter_id'] = np.nan
df['next_encounter_delta'] = np.nan

# get unique patients
df = df[np.isfinite(df['patient_nbr'])]
patients = np.unique(df['patient_nbr'])
n_patients = len(patients)

for patient in patients:
    # grab subset of data frame
    df_patient = df.loc[df['patient_nbr'] == patient]
    # in multicase visits
    if len(df_patient) > 1:
        print('calculating next_encounter_id for {}'.format(patient))
        for i, idx in enumerate(df_patient.index):
            if i+1 != len(df_patient):
                # add all known next encounter ids
                df.loc[idx, 'next_encounter_id'] = df_patient.iloc[i+1]['encounter_id']
                df.loc[idx, 'next_encounter_delta'] = df_patient.iloc[i+1]['encounter_id'] - df_patient.iloc[i]['encounter_id']

df.to_csv('kaggle-data/diabetes-edited.csv')
df = df[np.isfinite(df['next_encounter_id'])]
df.to_csv('kaggle-data/diabetes-edited-reduced.csv')


from flask import Flask
import json
import logging
import datetime
app = Flask(__name__)

logging.basicConfig(filename='app.log', encoding='utf-8', level=logging.DEBUG)

@app.route("/")
def hello():
    log('/')
    return "Hello World!"

@app.route("/metrics")
def metrics():
    log('/metrics')
    # Deze functie correspondeert met het /metrics pad, (localhost:5000/metrics) en returnt json
    return app.response_class(response = json.dumps({"data":{"UserCount":140, "UserCountActive":23}}), status = 200, mimetype = 'application/json')

@app.route("/status")
def status():
    # json.dumps maakt van een dictionary (zo heet dat in python) een json string, beetje JSON.stringify
    # We geven de response een inhoud (genaamd response), een statuscode en een mimetype (media type) mee
    log('/status')
    return app.response_class(response = json.dumps({"result":"OK - healthy"}), status = 200, mimetype = 'application/json')

def log(endpoint):
    logging.debug(str(datetime.datetime.now()) + ', ' + endpoint + ' endpoint was reached')

if __name__ == "__main__":
    app.run(host='0.0.0.0')


from __future__ import absolute_import

from celery import shared_task

from metamodel.models import InstanceModel
from solotodo.models import Product

@shared_task
def save_existing_product(product_id):
    product = Product.objects.get(pk=product_id)
    product.save()

@shared_task
def save_new_product(instance_model_id, creator_id):
    instance_model = InstanceModel.objects.get(pk=instance_model_id)
    p = Product()
    p.instance_model = instance_model
    p.save(creator_id=creator_id)


"Crossref interface."

from __future__ import print_function

from collections import OrderedDict
import json
import unicodedata

import requests

CROSSREF_FETCH_URL = 'http://api.crossref.org/works/%s'

TIMEOUT = 5.0

def fetch(doi, debug=False):
    "Fetch publication JSON data from Crossref and parse into a dictionary."
    url = CROSSREF_FETCH_URL % doi
    response = requests.get(url, timeout=TIMEOUT)
    if response.status_code != 200:
        raise IOError("HTTP status %s, %s " % (response.status_code, url))
    if debug:
        print(json.dumps(response.json(), indent=2))
    return parse(response.json())

def parse(data):
    "Parse JSON data for a publication into a dictionary."
    result = OrderedDict()
    result['title']      = get_title(data)
    result['doi']        = get_doi(data)
    result['pmid']       = get_pmid(data)
    result['authors']    = get_authors(data)
    result['journal']    = get_journal(data)
    result['type']       = get_type(data)
    result['published']  = get_published(data)
    result['epublished'] = get_epublished(data)
    result['abstract']   = get_abstract(data)
    result['xrefs']      = get_xrefs(data)
    return result

def get_title(data):
    "Get the title from the article JSON."
    try:
        return ' '.join(data['message']['title'])
    except KeyError:
        for item in data['message']['assertion']:
            if item['name'] == 'articletitle':
                return item['value']

def get_doi(data):
    "Get the DOI from the article JSON."
    return data['message']['DOI']

def get_pmid(data):
    "Get the PMID from the article JSON; not present."
    return None

def get_authors(data):
    "Get the list of authors from the article JSON."
    result = []
    for item in data['message'].get('author', []):
        author = OrderedDict()
        author['family'] = item.get('family')
        author['family_normalized'] = to_ascii(author['family']).lower()
        # Remove dots and dashes
        given = item.get('given', '').replace('.', ' ').replace('-', ' ')
        # Replace weird blank characters
        author['given'] = ' '.join(given.split())
        author['given_normalized'] = to_ascii(author['given']).lower()
        author['initials'] = ''.join([n[0] for n in given.split()])
        author['initials_normalized'] = to_ascii(author['initials']).lower()
        try:
            author['orcid'] = item['ORCID']
        except KeyError:
            pass
        result.append(author)
    return result

def get_journal(data):
    "Get the journal data from the article JSON."
    result = OrderedDict()
    try:
        result['title'] = ' '.join(data['message']['short-container-title'])
    except KeyError:
        result['title'] = ' '.join(data['message']['container-title'])
    try:
        result['issn'] = data['message']['ISSN'][0]
    except (KeyError, IndexError):
        result['issn'] = None
    result['volume'] = data['message'].get('volume')
    result['issue'] = data['message'].get('issue')
    result['pages'] = data['message'].get('page')
    return result

def get_type(data):
    "Get the type from the article JSON."
    try:
        return data['message'].get('type')
    except KeyError:
        return None

def get_published(data):
    "Get the print publication date from the article JSON."
    # Try in order: print, issued, created
    for key in ['published-print', 'issued', 'created']:
        try:
            item = data['message'][key]
        except KeyError:
            pass
        else:
            break
    parts = [int(i) for i in item['date-parts'][0]]
    # Add dummy values, if missing
    if len(parts) == 1: parts.append(0)
    if len(parts) == 2: parts.append(0)
    return "%s-%02i-%02i" % tuple(parts)

def get_epublished(data):
    "Get the online publication date from the article JSON, or None."
    # Try in order: online, issued
    for key in ['published-online', 'issued']:
        try:
            item = data['message'][key]
        except KeyError:
            pass
        else:
            break
    else:
        return None
    parts = [int(i) for i in item['date-parts'][0]]
    # Add dummy values, if missing
    if len(parts) == 1: parts.append(0)
    if len(parts) == 2: parts.append(0)
    return "%s-%02i-%02i" % tuple(parts)

def get_abstract(data):
    "Get the abstract from the article JSON; not present."
    return None

def get_xrefs(data):
    "Get the list of cross-references from the article JSON; not present."
    return []

def to_ascii(value):
    "Convert any non-ASCII character to its closest equivalent."
    if value:
        return unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
    else:
        return ''

def test_fetch():
    "Fetch a specific article."
    key = '10.1016/j.cell.2015.12.018'
    result = fetch(key)
    assert result['doi'] == key

if __name__ == '__main__':
    doi = '10.1126/science.1260419'
    url = CROSSREF_FETCH_URL % doi
    response = requests.get(url, timeout=TIMEOUT)
    if response.status_code != 200:
        raise IOError("HTTP status %s, %s " % (response.status_code, url))
    with open('data/%s' % doi.replace('/', '_'), 'w') as outfile:
        outfile.write(json.dumps(response.json(), indent=2))


import pygame
import pygame.locals
import random
import math

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BG_COLOR = (0, 0, 102)
TEXT_COLOR = (255, 255, 255)
SCORE_COORDS = (10, 10)
FONT_SIZE = 32

# Images/Sprites
# Attribution: https://www.flaticon.com/authors/nhor-phai
LOGO = 'img/spaceship_logo.png'
# Attribution: https://www.flaticon.com/authors/smashicons
SPACESHIP = 'img/spaceship.png'
BULLET = 'img/bullet.png'
# Attribution: https://www.flaticon.com/authors/payungkead
ENEMY = 'img/enemy.png'

# Initialize pygame
pygame.display.init()
pygame.font.init()

# Create the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# Change Title and Icon
pygame.display.set_caption('Space Invaders')
icon = pygame.image.load(LOGO)
pygame.display.set_icon(icon)

# Score
score = 0
font = pygame.font.Font('freesansbold.ttf', FONT_SIZE)

def show_score():
    score_text = font.render("Score: {}".format(score), True, TEXT_COLOR)
    screen.blit(score_text, SCORE_COORDS)

def game_over_text():
    text = font.render("Game Over. 'q' to quit.", True, TEXT_COLOR)
    screen.blit(text, (225, 300))

def has_collided(x1, x2, y1, y2, collision_zone):
    distance = math.sqrt(((x2 - x1) ** 2) +
                         ((y2 - y1) ** 2))
    return distance < collision_zone

# Player
class Player():
    def __init__(self):
        self.img = pygame.image.load(SPACESHIP)
        self.x = 368
        self.y = 480
        self.delta_x = 0
        self.captured = False
        self.collision_zone = 64

    def draw(self):
        screen.blit(self.img, (int(self.x), self.y))

    def update_x(self):
        self.x = (self.x + self.delta_x) % 736

    def check_collision(self, x, y):
        self.captured = has_collided(self.x, x, self.y, y,
                                     self.collision_zone)

# Enemy
class Enemy():
    def __init__(self):
        self.img = pygame.image.load(ENEMY)
        self.x = random.randint(64, 700)
        self.y = random.randint(50, 150)
        self.delta_x = random.randint(2, 9) / 10.0
        self.delta_y = 0
        self.shot = False
        self.collision_zone = 33

    def draw(self):
        screen.blit(self.img, (int(self.x), self.y))

    def check_collision(self, x, y):
        self.shot = has_collided(self.x+32, x, self.y+32, y, self.collision_zone)

    def reset(self):
        self.x = random.randint(64, 700)
        self.y = random.randint(25, 400)

    def descend(self):
        self.y += 10
        self.delta_x *= -1

    def update_x(self):
        self.x += self.delta_x

# Bullet
# Ready - Bullet is not on screen
# Fire - The bullet is moving
class Bullet():
    def __init__(self):
        self.img = pygame.image.load(BULLET)
        self.x = 0
        self.y = 480
        self.delta_y = 0.5
        self.state = 'ready'

    def fire(self):
        self.state = 'fire'

    def show(self):
        screen.blit(self.img, (int(self.x+16), int(self.y+10)))

    def reset(self):
        self.y = 480
        self.state = 'ready'

    def update_y(self):
        self.y -= self.delta_y

num_enemies = 6
enemies = [Enemy() for _ in range(num_enemies)]
player = Player()
bullet = Bullet()

# Game Loop
running = True
while running:

    # RGB Color
    screen.fill(BG_COLOR)

    for event in pygame.event.get():
        if event.type == pygame.locals.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                running = False
            if event.key == pygame.K_LEFT:
                player.delta_x = -0.3
            if event.key == pygame.K_RIGHT:
                player.delta_x = 0.3
            if event.key == pygame.K_SPACE:
                if bullet.state == 'ready':
                    bullet.x = player.x
                    bullet.fire()
                    bullet.show()
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                player.delta_x = 0

    player.update_x()

    for enemy in enemies:
        enemy.update_x()
        if not 0 <= enemy.x <= 736:
            enemy.descend()

    if bullet.state == 'fire':
        bullet.update_y()
        bullet.show()

    if bullet.y <= 0:
        bullet.reset()

    for enemy in enemies:
        enemy.check_collision(bullet.x, bullet.y)

        if enemy.shot:
            bullet.reset()
            score += 1
            enemy.reset()

        if not player.captured:
            player.check_collision(enemy.x, enemy.y)
        else:
            for bad_guy in enemies:
                bad_guy.y = 2000
            game_over_text()

        enemy.draw()

    show_score()
    player.draw()
    pygame.display.update()


import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation

#Parameters
q = 1.6*10**(-19)
B = 2*10**(-3)
V = 6*10**4
m = 9.1*10**(-31)
h = (0.01*m/(q*B))
zo = 0.6
so = 0.9
d = (0.5*(zo**2 + (zo**2 + so**2)/2))**0.5
k1 = q*B*h/(2*m)
k2 = q*V*(h**2)/(2*m*(d**2))
k3 = 1/(k1**2 + 1)

#Timestep is the number of iteration that the animation will do
timestep = 10000

#Initial conditions
x0 = 0
y0 = 0
z0 = 0
vx0 = 8*10**7
vy0 = 8*10**7
vz0 = 8*10**7
ax0 = (q*V/(2*m*(d**2)))*x0 + q*vy0*B
ay0 = (q*V/(2*m*(d**2)))*y0 - q*vx0*B
az0 = -(q*V/(2*m*(d**2)))*z0

#Initializing variables
x = np.zeros((3, timestep))
v = np.zeros((3, timestep))
a = np.zeros((3, timestep))
x[:, 0] = [x0,y0,z0]
v[:, 0] = [vx0,vy0,vz0]
a[:, 0] = [ax0,ay0,az0]

#Iteration formula
for i in range(1, timestep):
    if (i>1):
        x[0, i] = k3*((k2 + 2)*x[0, i-1] + (k1**2 - 1)*x[0, i-2] + (k2 + 2)*k1*x[1, i-1] - 2*k1*x[1, i-2])
        x[1, i] = k3*((k2 + 2)*x[1, i-1] + (k1**2 - 1)*x[1, i-2] - (k2 + 2)*k1*x[0, i-1] + 2*k1*x[0, i-2])
        x[2, i] = (2- 2*k2)*x[2, i-1] - x[2, i-2]
    else:
        x[:, i] = x[:, i-1] + [(j * h) for j in v[:, i-1]]

for i in range(1, timestep):
    v[:, i] = v[:, i-1] + [(j * h) for j in a[:, i-1]]
    a[0, i] = (q*V/(2*m*(d**2)))*x[0, i] + q*v[1, i]*B
    a[1, i] = (q*V/(2*m*(d**2)))*x[1, i] - q*v[0, i]*B
    a[2, i] = -(q*V/(m*(d**2)))*x[2, i]

#Animation
def update_lines(num, dataLines, lines):
    for line, data in zip(lines, dataLines):
        line.set_data(data[0:2, :num])
        line.set_3d_properties(data[2, :num])
    return lines

fig_x = plt.figure()
ax_x = p3.Axes3D(fig_x)
ax_x.get_autoscale_on()

fig_v = plt.figure()
ax_v = p3.Axes3D(fig_v)
ax_v.get_autoscale_on()

fig_a = plt.figure()
ax_a = p3.Axes3D(fig_a)
ax_a.get_autoscale_on()

data_x = [x]
data_v = [v]
data_a = [a]

#Draw lines in between every points in the data
lines_x = [ax_x.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1], lw = 3)[0] for dat in data_x]
lines_v = [ax_v.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1], lw = 3)[0] for dat in data_v]
lines_a = [ax_a.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1], lw = 3)[0] for dat in data_a]

#Lines below are for labelling the axes
ax_x.set_xlabel('X')
ax_v.set_xlabel('X')
ax_a.set_xlabel('X')

ax_x.set_ylabel('Y')
ax_v.set_ylabel('Y')
ax_a.set_ylabel('Y')

ax_x.set_zlabel('Z')
ax_v.set_zlabel('Z')
ax_a.set_zlabel('Z')

#Plot title
ax_x.set_title('Penning Trap Simulation - Position')
ax_v.set_title('Penning Trap Simulation - Velocity')
ax_a.set_title('Penning Trap Simulation - Acceleration')

#Creating the animation object for position
plt.figure(1)
line_ani_x = animation.FuncAnimation(fig_x, update_lines, timestep, fargs=(data_x, lines_x),\
                                   interval=1, blit=False)
#Plotting the whole particle trajectories without animation
plt.figure(1)
ax_x.plot(x[0, :],x[1, :],x[2, :], lw=0.5, color='green')
plt.show()

#Creating animation object for velocity
plt.figure(2)
line_ani_v = animation.FuncAnimation(fig_v, update_lines, timestep, fargs=(data_v, lines_v),\
                                   interval=1, blit=False)
#Plotting the whole trace of the particle's velocity throughout the simulation
plt.figure(2)
ax_v.plot(v[0, :],v[1, :],v[2, :], lw=0.5, color='orange')
plt.show()

#Creating animation object for acceleration
plt.figure(3)
line_ani_a = animation.FuncAnimation(fig_a, update_lines, timestep, fargs=(data_a, lines_a),\
                                   interval=1, blit=False)
#Plotting the whole trace of the particle's acceleration throughout the simulation
plt.figure(3)
ax_a.plot(a[0, :],a[1, :],a[2, :], lw=0.5, color='red')
plt.show()


import datetime

import pandas as pd

from vvixology.data_sources.yahoo_wrapper import yahoo_wrapper

FNAME='vjb.pkl'

symbol_list = ['AAPL', 'TVIX', '^GSPC']

start_date_string = "2013-01-01"

end_date_string = "2015-01-01"

start = datetime.datetime.strptime(start_date_string, "%Y-%m-%d")

end = datetime.datetime.strptime(end_date_string, "%Y-%m-%d")

PNL = yahoo_wrapper(symbol_list, start, end)

# http://pandas.pydata.org/pandas-docs/stable/io.html#io-pickle

PNL.to_pickle(FNAME)

PNL_pkl = pd.read_pickle(FNAME)


while True:
    reply = input('\nSquare of number:')
    if reply == 'stop':
        print('almonds the dog')
        break
    elif not reply.isdigit():
        print('toby the dog')
    else:
        num = int(reply)
        if num < 20:
            print('gondar the dog')
        else:
            print('chestnut the dog')
        print(num ** 2)
print('Bye!')


#!/usr/bin/env python
# -*- coding: utf-8 -*- #
from __future__ import unicode_literals

AUTHOR = u'Die Uberpartei'
SITENAME = u'Uberpartei Deutschland'
SITEURL = 'http://uberpartei.de'

PATH = 'content'

TIMEZONE = 'Europe/Berlin'

DEFAULT_LANG = u'de'

# Feed generation is usually not desired when developing
FEED_ALL_ATOM = None
CATEGORY_FEED_ATOM = None
TRANSLATION_FEED_ATOM = None
AUTHOR_FEED_ATOM = None
AUTHOR_FEED_RSS = None

# Blogroll

DEFAULT_PAGINATION = 10

# Uncomment following line if you want document-relative URLs when developing
#RELATIVE_URLS = True


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

import argparse
import socket
import sys

HOST = 'localhost'
BUFSIZE = 1024

def ipv6_echo_client(port, host=HOST):
    for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
        af, socktype, proto, canonname, sa = res
        try:
            sock = socket.socket(af, socktype, proto)
        except socket.error as err:
            print('error : {}'.format(err))

        try:
            sock.connect(sa)
        except socket.error as msg:
            sock.close()
            continue

    if sock is None:
        print('failed to open socket!')

    msg = b'hello from ipv6 client'
    print('send data to server: {}'.format(msg))
    sock.send(msg)

    while True:
        data = sock.recv(BUFSIZE)
        print('received from server', repr(data))
        if not data:
            break

    sock.close()

if __name__ == '__main__':
    parse = argparse.ArgumentParser(description='ipv6 socket client')
    parse.add_argument('--port', action='store', dest='port', type=int, required=True)
    given_args = parse.parse_args()
    port = given_args.port
    ipv6_echo_client(port)


import json
import csv
import pprint

dEliminater = ','

def nbi_encoder(row):
    x = json.dumps(
               {
   "State Code": row[0],
   "Structure Number":row[1],
   "Inventory Route": {
                        "Record Type":row[2],
                        "Route Signing Prefix":row[3],
                        "Designated Level of Service":row[4],
                        "Route Number":row[5],
                        "Directional Suffix":row[6]
                     },

   "Highway Agency District":row[7],
   "County (Parish) Code":row[8],
   "Place Code":row[9],
   "Features Intersected": {
                             "Features Instersected":row[10],
                             "Critical Facility Indicator":row[11]
                            },
   "Location":row[12],
   "Inventory RTe, Min Vert Clearance":row[13],
   "Kilometerpoint":row[14],
   "Base Highway Point":row[16],
   "Inventory Route, Subroute Number": {
                                         "LRS Inventory Route":row[17],
                                         "Subroute Number":row[18]
                                        },
   "Latitude":row[19],
   "Longitude":row[20],
   "Bypass/Detour Length":row[21],
   "Toll":row[22],
   "Maintenance Reponsibility":row[23],
   "Owner":row[24],
   "Functional Class Of Inventory Rte.": row[25],
   "Year Built":row[26],
   "Lanes On/Under Structure": {
                                  "Lanes On Structure":row[28],
                                  "Lanes Under Structure":row[29]
                                },
   "Average Daily Traffic":row[30],
   "Year Of Average Daily Traffic":row[31],
   "Design Load":row[32],
   "Approach Roadway Width":row[33],
   "Bridge Median":row[34],
   "Skew":row[35],
   "Structure Flared":row[36],
   "Traffic safety Features": {
                                "Bridge Railings":row[37],
                                "Transitions":row[38],
                                "Approach Guardrail Ends":row[39]
                               },

   "Historical significance":row[40],
   "Navigation Control":row[41],
   "Navigation Veritical Clearance":row[42],
   "Navigation Horizontal Clearance":row[43],
   "Strucutre Open/Posted/Closed":row[44],
   "Type of Service":{
                       "Type of Service On Bridge":row[45],
                       "Type of Service Under Bridge":row[46]
                     },

   "Structure Type, Main":{
                          "Kind of Material/Design":row[47],
                          "Type of Design/Construction":row[48]
                          },

   "Number of Spans in Main unit":row[49],
   "Number of Approach Spans":row[50],
   "Inventory Rte Total Horz Clearance":row[51],
   "Length Of Maximum Span":row[52],
   "Structure Length":row[53],
   "Curb/Sidewalk Width": { "Left Curb/Sidewalk Width":row[54],
                            "Right Curb/Sidewalk Width":row[55]
                           },

   "Bridge Roadway with Curb-To-Curb":row[56],
   "Deck Width, Out-To-Out":row[57],
   "Min Vert Clear Over Bridge Roadway":row[58],
   "Minimum Veritical Underclearance": {
                                         "Reference Feature":row[59],
                                         "Minimum Veritical Underclearance":row[60]
                                       },
   "Min Lateral Underclear On Left":row[61],
   "Deck":row[62],
   "Superstructure":row[63],
   "Substructure":row[64],
   "Channel/Channel Protection":row[65],
   "Culverts":row[66],
   "Method Used to Determine Operating Rating":row[67],
   "Operating Rating":row[68],
   "Method Used To Determine Inventory Rating":row[69],
   "Inventory Rating":row[70],
   "Structural Evaluation":row[71],
   "Deck Geometry":row[72],
   "Underclear, Vertical & Horizontal":row[74],
   "Bridge Posting":row[75],
   "Waterway Adequacy":row[76],
   "Approach Roadway Alignment":row[77],

   "Type of Work": {
                      "Type of Work Proposed":row[78],
                      "Work Done By":row[79]
                   },

   "Length Of Structure Improvement":row[80],
   "Inspection Date":row[81],
   "Designated Inspection Frequency":row[82],
   "Critical Feature Inspection": {
                                    "Fracture Critical Details":row[83],
                                    "Underwater Inspection":row[84],
                                    "Other Special Inspection":row[85]
                                  },
   "Bridge Improvement Cost":row[86],
   "Roadway Improvement Cost":row[87],
   "Total Project Cost":row[88],
   "Year Of Improvement Cost":row[89],
   "Border Bridge": {
                       "Neighboring State Code":row[90],
                       "Percent Reponsibility":row[91]
                    },
   "Border Bridge Structure Number":row[92],
   "STRAHNET Highway Designation":row[93],
   "Parallel Structure Designation":row[94],
   "Direction Of Traffic":row[95],
   "Temporary Structure Designation":row[96],
   "Highway System of Inventory Route":row[97],
   "Federal Lands Highways":row[98],
   "Year Reconstructed":row[99],
   "Deck Strucuture Type":row[100],
   "Wearing Surface/Protective System": {
                                           "Type of Wearing Surface":row[101],
                                           "Type of Membrane":row[102],
                                           "Deck Protection":row[103]
                                         },
   "AVERAGE DAILY TRUCK TRAFFIC":row[104],
   "DESIGNATED NATIONAL NETWORK":row[105],
   "PIER/ABUTMENT PROTECTION":row[106],
   "NBIS BRIDGE LENGTH":row[107],
   "SCOUR CRITICAL BRIDGES":row[108],
   "FUTURE AVERAGE DAILY TRAFFIC":row[109],
   "YEAR OF FUTURE AVG DAILY TRAFFIC":row[110],
   "MINIMUM NAVIGATION VERTICAL CLEARANCE VERTICAL LIFT BRIDGE":row[111],
   "FEDERAL AGENCY INDICATOR":row[112],
   "Washington Headquarters Use":row[113],
   "STATUS with 10yr rule":row[114],
   "Asterisk Field in SR":row[115],
   "SUFFICIENCY RATING":row[116],
   "STATUS WITHOUT 10yr rule":row[117],

        })
    return x

with open("Sample.txt" , 'r') as f:
    reader = csv.reader(f, delimiter = dEliminater)
    next(reader,None) #skips header
    for row in reader:
       print("============================================================== Line break===================================================")
       temp = []
       for r in row:
           r = r.strip("'")
           r = r.strip(" ")
           temp.append(r)
       x = nbi_encoder(temp)
       print(str(x))


import numpy as np
from numpy import matlib as mb # matlib must be imported separately

def compute_spatial_similarity(conv1,conv2):
    """
    Takes in the last convolutional layer from two images, computes the pooled output
    feature, and then generates the spatial similarity map for both images.
    """
    pool1 = np.mean(conv1,axis=0)
    pool2 = np.mean(conv2,axis=0)
    out_sz = (int(np.sqrt(conv1.shape[0])),int(np.sqrt(conv1.shape[0])))
    conv1_normed = conv1 / np.linalg.norm(pool1) / conv1.shape[0]
    conv2_normed = conv2 / np.linalg.norm(pool2) / conv2.shape[0]
    im_similarity = np.zeros((conv1_normed.shape[0],conv1_normed.shape[0]))
    for zz in range(conv1_normed.shape[0]):
        repPx = mb.repmat(conv1_normed[zz,:],conv1_normed.shape[0],1)
        im_similarity[zz,:] = np.multiply(repPx,conv2_normed).sum(axis=1)
    similarity1 = np.reshape(np.sum(im_similarity,axis=1),out_sz)
    similarity2 = np.reshape(np.sum(im_similarity,axis=0),out_sz)
    return similarity1, similarity2

def combine_SVD(conv1,conv2):

    conv1 = np.reshape(conv1, (-1,2048))
    conv2 = np.reshape(conv2, (-1,2048))
    pool1 = np.mean(conv1,axis=0)
    pool2 = np.mean(conv2,axis=0)
    conv1 = conv1 - pool1
    conv2 = conv2 - pool2
    allconv = np.vstack((conv1,conv2))
    allconv =(allconv.T-allconv.mean(axis=1)).T
    u, _, _ = np.linalg.svd(allconv, full_matrices=False)
    out_sz = (int(np.sqrt(conv1.shape[0])),int(np.sqrt(conv1.shape[0])))
    r1 = np.reshape(u[:,0][0:conv1.shape[0]],out_sz)
    g1 = np.reshape(u[:,1][0:conv1.shape[0]],out_sz)
    b1 = np.reshape(u[:,2][0:conv1.shape[0]],out_sz)
    r2 = np.reshape(u[:,0][conv1.shape[0]:],out_sz)
    g2 = np.reshape(u[:,1][conv1.shape[0]:],out_sz)
    b2 = np.reshape(u[:,2][conv1.shape[0]:],out_sz)
    r1 = 0.5+0.5*r1/(np.absolute(r1).max())
    g1 = 0.5+0.5*g1/(np.absolute(g1).max())
    b1 = 0.5+0.5*b1/(np.absolute(b1).max())
    res1 = np.array([r1,g1,b1])
    r2 = 0.5+0.5*r2/(np.absolute(r2).max())
    g2 = 0.5+0.5*g2/(np.absolute(g2).max())
    b2 = 0.5+0.5*b2/(np.absolute(b2).max())
    res2 = np.array([r2,g2,b2])
    res1 = np.ascontiguousarray(res1.transpose(1, 2, 0))*255
    res2 = np.ascontiguousarray(res2.transpose(1, 2, 0))*255
    return res1,res2

def SVD_whole(convlist):
    allconv = np.zeros(convlist[0].shape)
    leng = len(convlist)
    for convmap in convlist:
        pool = np.mean(convmap,axis=0)
        convmap = convmap / np.linalg.norm(pool) / convmap.shape[0]
        #convmap = (convmap.T-convmap.mean(axis=1)).T
        allconv = np.vstack((allconv,convmap))

    allconv = allconv[convlist[0].shape[0]:,:]
    allconv =(allconv-allconv.mean(axis=0))
    u, _, _ = np.linalg.svd(allconv, full_matrices=False)
#     u1, s, v= np.linalg.svd(conv1, full_matrices=False)
#     u2, _, _ = np.linalg.svd(conv2, full_matrices=False)
    out_sz = (int(np.sqrt(convmap.shape[0])),int(np.sqrt(convmap.shape[0])))
    #newconv1_r = u1[0]*s[0]*v[0]
    reslist = []
    for i in range(0,leng):
        r = np.reshape(u[:,0][convmap.shape[0]*i:convmap.shape[0]*(i+1)],out_sz)
        g = np.reshape(u[:,1][convmap.shape[0]*i:convmap.shape[0]*(i+1)],out_sz)
        b = np.reshape(u[:,2][convmap.shape[0]*i:convmap.shape[0]*(i+1)],out_sz)
        # r = r-r.min()
#         r = r/r.max()
#         g = g-g.min()
#         g = g/g.max()
#         b = b-b.min()
#         b = b/b.max()
        r = 0.5+0.5*r/(np.absolute(r).max())
        g = 0.5+0.5*g/(np.absolute(g).max())
        b = 0.5+0.5*b/(np.absolute(b).max())
        res = np.array([r,g,b])
        #res = np.array([r,g,b])
        res = np.ascontiguousarray(res.transpose(1, 2, 0))*255
        reslist.append(res)
    return reslist


from django.shortcuts import render

# Create your views here.
from rest_framework import mixins, viewsets, status
from rest_framework.permissions import IsAuthenticated
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.authentication import SessionAuthentication

from .serializers import UserFavSerializer, UserMessageSerializer, UserAddressSerializer
from .models import UserFav, UserLeavingMessage, UserAddress
from utils.permissions import IsOwnerOrReadOnly

class UserFavViewSet(mixins.CreateModelMixin,
                     mixins.ListModelMixin,
                     mixins.DestroyModelMixin,
                     mixins.RetrieveModelMixin,
                     viewsets.GenericViewSet):
    """
    list:
        收藏列表
    create:
        创建用户收藏
    destroy:
        取消用户收藏
    retrieve:
        获取某个收藏信息
    """
    # queryset = UserFav.objects.all()
    serializer_class = UserFavSerializer
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    # 获取详情，查找的字段id改成goods_id
    lookup_field = "goods_id"

    # 重载queryset，只获取当前用户的收藏
    def get_queryset(self):
        return UserFav.objects.filter(user=self.request.user)

class UserMessageViewSet(mixins.ListModelMixin,
                         mixins.CreateModelMixin,
                         mixins.DestroyModelMixin,
                         viewsets.GenericViewSet):
    """
    list:
        获取用户留言信息
    create:
        添加留言
    destroy:
        删除用户留言
    """
    serializer_class = UserMessageSerializer
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)

    def get_queryset(self):
        return UserLeavingMessage.objects.filter(user=self.request.user)

class UserAddressViewSet(viewsets.ModelViewSet):
    """
    list:
        获取收货地址信息
    create:
        添加收货地址
    update:
        更新收货地址
    destroy:
        删除收货地址
    """
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    serializer_class = UserAddressSerializer

    def get_queryset(self):
        return UserAddress.objects.filter(user=self.request.user)


n = int(input())
a = list(map(int, input().split()))
dp = dict()
for ai in a:
  if ai not in dp:
    dp[ai] = 0
  if ai - 1 in dp:
    dp[ai] = max(dp[ai], 1 + dp[ai - 1])
k = max(dp.values())
print(k + 1)
l = -1
for ai in a:
  if dp[ai] == k:
    l = ai
idx = []
for i in range(n - 1,-1,-1):
  if a[i] == l:
    idx.append(i + 1)
    l -= 1
print(*idx[::-1])


from verity_sdk.protocols.Protocol import Protocol
from verity_sdk.utils import EVERNYM_MSG_QUALIFIER

class UpdateConfigs(Protocol):
    MSG_FAMILY = 'update-configs'
    MSG_FAMILY_VERSION = '0.6'

    # Messages
    UPDATE_CONFIGS = 'update'
    GET_STATUS = 'get-status'

    def __init__(self, name=None, logo_url=None):
        super().__init__(self.MSG_FAMILY,
                         self.MSG_FAMILY_VERSION,
                         msg_qualifier=EVERNYM_MSG_QUALIFIER)
        self.name = name
        self.logo_url = logo_url

    def update_msg(self, context):
        msg = self._get_base_message(self.UPDATE_CONFIGS)
        msg['configs'] = [
            {'name': 'name', 'value': self.name},
            {'name': 'logoUrl', 'value': self.logo_url}
        ]

        return msg

    async def update_msg_packed(self, context):
        msg = self.update_msg(context)
        return await self.get_message_bytes(context, msg)

    async def update(self, context):
        return await self.send_message(context, await self.update_msg_packed(context))

    def status_msg(self, _):
        msg = self._get_base_message(self.GET_STATUS)
        return msg

    async def status_msg_packed(self, context):
        return await self.get_message_bytes(context, self.status_msg(context))

    async def status(self, context):
        await self.send_message(context, await self.status_msg_packed(context))


# ----------------------------------------------------------------------------
# Copyright 2016 Nervana Systems Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------
"""
Utility functions for Faster-RCNN example and demo.

Reference:
    "Faster R-CNN"
    https://arxiv.org/pdf/1506.01497
    https://github.com/rbgirshick/py-faster-rcnn

"""
from __future__ import division
from __future__ import print_function
from builtins import zip
from builtins import range
import numpy as np
import os
import pickle

from neon.initializers import Constant, Xavier, Gaussian
from neon.transforms import Rectlin, Identity, Softmax, PixelwiseSoftmax
from neon.layers import Conv, Pooling, Affine, BranchNode, Tree, Dropout
from neon.models import Model
from neon.util.persist import load_obj
from neon.data.datasets import Dataset
from voc_eval import voc_eval

from roi_pooling import RoiPooling
from proposal_layer import ProposalLayer

def add_vgg_layers():

    # setup layers
    init1_vgg = Xavier(local=True)
    relu = Rectlin()

    conv_params = {'strides': 1,
                   'padding': 1,
                   'init': init1_vgg,
                   'bias': Constant(0),
                   'activation': relu}

    # Set up the model layers
    vgg_layers = []

    # set up 3x3 conv stacks with different feature map sizes
    vgg_layers.append(Conv((3, 3, 64), name="skip", **conv_params))
    vgg_layers.append(Conv((3, 3, 64), name="skip", **conv_params))
    vgg_layers.append(Pooling(2, strides=2))
    vgg_layers.append(Conv((3, 3, 128), name="skip", **conv_params))
    vgg_layers.append(Conv((3, 3, 128), name="skip", **conv_params))
    vgg_layers.append(Pooling(2, strides=2))
    vgg_layers.append(Conv((3, 3, 256), **conv_params))
    vgg_layers.append(Conv((3, 3, 256), **conv_params))
    vgg_layers.append(Conv((3, 3, 256), **conv_params))
    vgg_layers.append(Pooling(2, strides=2))
    vgg_layers.append(Conv((3, 3, 512), **conv_params))
    vgg_layers.append(Conv((3, 3, 512), **conv_params))
    vgg_layers.append(Conv((3, 3, 512), **conv_params))
    vgg_layers.append(Pooling(2, strides=2))
    vgg_layers.append(Conv((3, 3, 512), **conv_params))
    vgg_layers.append(Conv((3, 3, 512), **conv_params))
    vgg_layers.append(Conv((3, 3, 512), **conv_params))
    # not used after this layer
    # vgg_layers.append(Pooling(2, strides=2))
    # vgg_layers.append(Affine(nout=4096, init=initfc, bias=Constant(0), activation=relu))
    # vgg_layers.append(Dropout(keep=0.5))
    # vgg_layers.append(Affine(nout=4096, init=initfc, bias=Constant(0), activation=relu))
    # vgg_layers.append(Dropout(keep=0.5))
    # vgg_layers.append(Affine(nout=1000, init=initfc, bias=Constant(0), activation=Softmax()))

    return vgg_layers

def scale_bbreg_weights(model, means, stds, num_classes):
    means = np.array(num_classes * means)
    stds = np.array(num_classes * stds)

    means_be = model.be.array(means)
    stds_be = model.be.array(stds)
    model.layers.layers[2].layers[1].layers[1].layers[-3].W[:] = \
        model.layers.layers[2].layers[1].layers[1].layers[-3].W * stds_be
    model.layers.layers[2].layers[1].layers[1].layers[-2].W[:] = \
        model.layers.layers[2].layers[1].layers[1].layers[-2].W * stds_be + means_be
    return model

def load_vgg_all_weights(model, path):
    # load a pre-trained VGG16 from Neon model zoo to the local
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/VGG/'
    filename = 'VGG_D.p'
    size = 554227541

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print('De-serializing the pre-trained VGG16 model...')
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers[0].layers]
    param_dict_list = pdict['model']['config']['layers']

    i = 0
    for layer, ps in zip(param_layers, param_dict_list):
        i += 1
        if i == 43:
            break
        layer.load_weights(ps, load_states=True)
        print(layer.name + " <-- " + ps['config']['name'])

    # to load the fc6 and fc7 from caffe into neon fc layers after ROI pooling
    neon_fc_layers = model.layers.layers[2].layers[1].layers[0].layers[2:5] +\
        model.layers.layers[2].layers[1].layers[0].layers[6:9]
    vgg_fc_layers = param_dict_list[44:47] + param_dict_list[48:51]

    for layer, ps in zip(neon_fc_layers, vgg_fc_layers):
        layer.load_weights(ps, load_states=True)
        print(layer.name + " <-- " + ps['config']['name'])

def load_vgg_weights(model, path):
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/VGG/'
    filename = 'VGG_D_Conv.p'
    size = 169645138

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print('De-serializing the pre-trained VGG16 model...')
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers[0].layers]
    param_dict_list = pdict['model']['config']['layers']

    for layer, ps in zip(param_layers, param_dict_list):
        layer.load_weights(ps, load_states=True)
        print(layer.name + " <-- " + ps['config']['name'])

def build_model(dataset, frcn_rois_per_img, inference=False):
    """
    Returns the Faster-RCNN model. For inference, also returns a reference to the
    proposal layer.

    Faster-RCNN contains three modules: VGG, the Region Proposal Network (RPN),
    and the Classification Network (ROI-pooling + Fully Connected layers), organized
    as a tree. Tree has 4 branches:

    VGG -> b1 -> Conv (3x3) -> b2 -> Conv (1x1) -> CrossEntropyMulti (objectness label)
                               b2 -> Conv (1x1) -> SmoothL1Loss (bounding box targets)
           b1 -> PropLayer -> ROI -> Affine -> Affine -> b3 -> Affine -> CrossEntropyMulti
                                                         b3 -> Affine -> SmoothL1Loss

    When the model is constructed for inference, several elements are different:
    - The number of regions to keep before and after non-max suppression is (6000, 300) for
      training and (12000, 2000) for inference.
    - The out_shape of the proposalLayer of the network is equal to post_nms_N (number of rois
      to keep after performaing nms). This is configured by passing the inference flag to the
      proposalLayer constructor.

    Arguments:
        dataset (objectlocalization): Dataset object.
        frcn_rois_per_img (int): Number of ROIs per image considered by the classification network.
        inference (bool): Construct the model for inference. Default is False.

    Returns:
        model (Model): Faster-RCNN model.
        proposalLayer (proposalLayer): Reference to proposalLayer in the model.
                                       Returned only for inference=True.
    """
    num_classes = dataset.num_classes

    # define the branch points
    b1 = BranchNode(name="conv_branch")
    b2 = BranchNode(name="rpn_branch")
    b3 = BranchNode(name="roi_branch")

    # define VGG
    VGG = add_vgg_layers()

    # define RPN
    rpn_init = dict(strides=1, init=Gaussian(scale=0.01), bias=Constant(0))
    # these references are passed to the ProposalLayer.
    RPN_3x3 = Conv((3, 3, 512), activation=Rectlin(), padding=1, **rpn_init)
    RPN_1x1_obj = Conv((1, 1, 18), activation=PixelwiseSoftmax(c=2), padding=0, **rpn_init)
    RPN_1x1_bbox = Conv((1, 1, 36), activation=Identity(), padding=0, **rpn_init)

    # inference uses different network settings
    if not inference:
        pre_nms_N = 12000
        post_nms_N = 2000
    else:
        pre_nms_N = 6000
        post_nms_N = 300

    proposalLayer = ProposalLayer([RPN_1x1_obj, RPN_1x1_bbox],
                                  dataset.get_global_buffers(), pre_nms_N=pre_nms_N,
                                  post_nms_N=post_nms_N, num_rois=frcn_rois_per_img,
                                  inference=inference)

    # define ROI classification network
    ROI = [proposalLayer,
           RoiPooling(HW=(7, 7)),
           Affine(nout=4096, init=Gaussian(scale=0.005),
                  bias=Constant(.1), activation=Rectlin()),
           Dropout(keep=0.5),
           Affine(nout=4096, init=Gaussian(scale=0.005),
                  bias=Constant(.1), activation=Rectlin()),
           Dropout(keep=0.5)]

    ROI_category = Affine(nout=num_classes, init=Gaussian(scale=0.01),
                          bias=Constant(0), activation=Softmax())
    ROI_bbox = Affine(nout=4 * num_classes, init=Gaussian(scale=0.001),
                      bias=Constant(0), activation=Identity())

    # build the model
    # the four branches of the tree mirror the branches listed above
    frcn_tree = Tree([ROI + [b3, ROI_category],
                     [b3, ROI_bbox]
                      ])

    model = Model(layers=Tree([VGG + [b1, RPN_3x3, b2, RPN_1x1_obj],
                               [b2, RPN_1x1_bbox],
                               [b1] + [frcn_tree],
                               ]))

    if inference:
        return (model, proposalLayer)
    else:
        return model

def get_bboxes(outputs, proposals, num_proposals, num_classes,
               im_shape, im_scale, max_per_image=100, thresh=0.001, nms_thresh=0.4):
    """
    Returns bounding boxes for detected objects, organized by class.

    Transforms the proposals from the region proposal network to bounding box predictions
    using the bounding box regressions from the classification network:
    (1) Applying bounding box regressions to the region proposals.
    (2) For each class, take proposed boxes where the corresponding objectness score is greater
        then THRESH.
    (3) Apply non-maximum suppression across classes using NMS_THRESH
    (4) Limit the maximum number of detections over all classes to MAX_PER_IMAGE

    Arguments:
        outputs (list of tensors): Faster-RCNN model outputs
        proposals (Tensor): Proposed boxes from the model's proposalLayer
        num_proposals (int): Number of proposals
        num_classes (int): Number of classes
        im_shape (tuple): Shape of image
        im_scale (float): Scaling factor of image
        max_per_image (int): Maximum number of allowed detections per image. Default is 100.
                             None indicates no enforced maximum.
        thresh (float): Threshold for objectness score. Default is 0.001.
        nms_thresh (float): Threshold for non-maximum suppression. Default is 0.4.

    Returns:
        detections (list): List of bounding box detections, organized by class. Each element
                           contains a numpy array of bounding boxes for detected objects
                           of that class.
    """
    detections = [[] for _ in range(num_classes)]

    proposals = proposals.get()[:num_proposals, :]  # remove padded proposals
    boxes = proposals[:, 1:5] / im_scale  # scale back to real image space

    # obtain bounding box corrections from the frcn layers
    scores = outputs[2][0].get()[:, :num_proposals].T
    bbox_deltas = outputs[2][1].get()[:, :num_proposals].T

    # apply bounding box corrections to the region proposals
    pred_boxes = bbox_transform_inv(boxes, bbox_deltas)
    pred_boxes = clip_boxes(pred_boxes, im_shape)

    # Skip the background class, start processing from class 1
    for j in range(1, num_classes):
        inds = np.where(scores[:, j] > thresh)[0]

        # obtain class-specific boxes and scores
        cls_scores = scores[inds, j]
        cls_boxes = pred_boxes[inds, j * 4:(j + 1) * 4]
        cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32, copy=False)

        # apply non-max suppression
        keep = nms(cls_dets, nms_thresh)
        cls_dets = cls_dets[keep, :]

        # store results
        detections[j] = cls_dets

    # Limit to max_per_image detections *over all classes*
    if max_per_image is not None:

        # obtain flattened list of all image scores
        image_scores = np.hstack([detections[j][:, -1]
                                  for j in range(1, num_classes)])

        if len(image_scores) > max_per_image:
            # compute threshold needed to keep the top max_per_image
            image_thresh = np.sort(image_scores)[-max_per_image]

            # apply threshold
            for j in range(1, num_classes):
                keep = np.where(detections[j][:, -1] >= image_thresh)[0]
                detections[j] = detections[j][keep, :]

    return detections

# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------
def nms(dets, thresh):
    """Pure Python NMS baseline."""
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    scores = dets[:, 4]

    areas = (x2 - x1 + 1) * (y2 - y1 + 1)
    order = scores.argsort()[::-1]

    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        yy1 = np.maximum(y1[i], y1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        yy2 = np.minimum(y2[i], y2[order[1:]])

        w = np.maximum(0.0, xx2 - xx1 + 1)
        h = np.maximum(0.0, yy2 - yy1 + 1)
        inter = w * h
        ovr = inter / (areas[i] + areas[order[1:]] - inter)

        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]

    return keep

def run_voc_eval(annopath, imagesetfile, year, image_set, classes, output_dir):
    aps = []
    # The PASCAL VOC metric changed in 2010
    use_07_metric = True if int(year) < 2010 else False
    print('VOC07 metric? ' + ('Yes' if use_07_metric else 'No'))
    for i, cls in enumerate(classes):
        if cls == '__background__':
            continue
        filename = 'voc_{}_{}_{}.txt'.format(
            year, image_set, cls)
        filepath = os.path.join(output_dir, filename)
        rec, prec, ap = voc_eval(filepath, annopath, imagesetfile, cls,
                                 output_dir, ovthresh=0.5, use_07_metric=use_07_metric)
        aps += [ap]
        print('AP for {} = {:.4f}'.format(cls, ap))
        with open(os.path.join(output_dir, cls + '_pr.pkl'), 'w') as f:
            pickle.dump({'rec': rec, 'prec': prec, 'ap': ap}, f)
    print('Mean AP = {:.4f}'.format(np.mean(aps)))

def bbox_transform(ex_rois, gt_rois):
    ex_widths = ex_rois[:, 2] - ex_rois[:, 0] + 1.0
    ex_heights = ex_rois[:, 3] - ex_rois[:, 1] + 1.0
    ex_ctr_x = ex_rois[:, 0] + 0.5 * ex_widths
    ex_ctr_y = ex_rois[:, 1] + 0.5 * ex_heights

    gt_widths = gt_rois[:, 2] - gt_rois[:, 0] + 1.0
    gt_heights = gt_rois[:, 3] - gt_rois[:, 1] + 1.0
    gt_ctr_x = gt_rois[:, 0] + 0.5 * gt_widths
    gt_ctr_y = gt_rois[:, 1] + 0.5 * gt_heights

    targets_dx = (gt_ctr_x - ex_ctr_x) / ex_widths
    targets_dy = (gt_ctr_y - ex_ctr_y) / ex_heights
    targets_dw = np.log(gt_widths / ex_widths)
    targets_dh = np.log(gt_heights / ex_heights)

    targets = np.vstack(
        (targets_dx, targets_dy, targets_dw, targets_dh)).transpose()
    return targets

def bbox_transform_inv(boxes, deltas):
    if boxes.shape[0] == 0:
        return np.zeros((0, deltas.shape[1]), dtype=deltas.dtype)

    boxes = boxes.astype(deltas.dtype, copy=False)

    widths = boxes[:, 2] - boxes[:, 0] + 1.0
    heights = boxes[:, 3] - boxes[:, 1] + 1.0
    ctr_x = boxes[:, 0] + 0.5 * widths
    ctr_y = boxes[:, 1] + 0.5 * heights

    dx = deltas[:, 0::4]
    dy = deltas[:, 1::4]
    dw = deltas[:, 2::4]
    dh = deltas[:, 3::4]

    pred_ctr_x = dx * widths[:, np.newaxis] + ctr_x[:, np.newaxis]
    pred_ctr_y = dy * heights[:, np.newaxis] + ctr_y[:, np.newaxis]
    pred_w = np.exp(dw) * widths[:, np.newaxis]
    pred_h = np.exp(dh) * heights[:, np.newaxis]

    pred_boxes = np.zeros(deltas.shape, dtype=deltas.dtype)
    # x1
    pred_boxes[:, 0::4] = pred_ctr_x - 0.5 * pred_w
    # y1
    pred_boxes[:, 1::4] = pred_ctr_y - 0.5 * pred_h
    # x2
    pred_boxes[:, 2::4] = pred_ctr_x + 0.5 * pred_w
    # y2
    pred_boxes[:, 3::4] = pred_ctr_y + 0.5 * pred_h

    return pred_boxes

def clip_boxes(boxes, im_shape):
    """
    Clip boxes to image boundaries.
    """

    # x1 >= 0
    boxes[:, 0::4] = np.maximum(np.minimum(boxes[:, 0::4], im_shape[1] - 1), 0)
    # y1 >= 0
    boxes[:, 1::4] = np.maximum(np.minimum(boxes[:, 1::4], im_shape[0] - 1), 0)
    # x2 < im_shape[1]
    boxes[:, 2::4] = np.maximum(np.minimum(boxes[:, 2::4], im_shape[1] - 1), 0)
    # y2 < im_shape[0]
    boxes[:, 3::4] = np.maximum(np.minimum(boxes[:, 3::4], im_shape[0] - 1), 0)
    return boxes


#!/usr/bin/env python

# ---------------------------------------------------------------------------
# install the libraries:
#
#   yum install numpy
#   yum install scipy
#   yum install python-matplotlib
#   yum install pygtk2
#
# ---------------------------------------------------------------------------

import matplotlib as mpl
mpl.use('Agg')  #nasty trick to avoid problems due lack of interactive usage
import matplotlib.pyplot as plt
import numpy as np

files = ['data1', 'data2', 'data3', 'data4', 'data5']

times = []
counts = []
pressures = []

innerloop = 0
hours = 0
count = 0
pressure = 0

for f in files:
    for line in open(f).read().strip().split('\n'):
        count += int(line.split(',')[2])
        pressure += float(line.split(',')[4])
        innerloop += 1
        if innerloop == 12: # 1 hour
            hours += 1
            times.append(hours)
            counts.append(count)
            pressures.append(pressure/12)
            innerloop = 0
            count = 0
            pressure = 0

f = plt.figure(1)

ax1 = f.add_subplot(211)
ax1.plot(times,counts, 'b')
ax1.grid(True)
ax1.set_xlabel("hours")
ax1.set_ylabel("counts per hour", color='b')
for tl in ax1.get_yticklabels():
   tl.set_color('b')

ax2 = ax1.twinx()
ax2.plot(times,pressures, 'r')
ax2.set_ylabel("pressure", color='r')
for tl in ax2.get_yticklabels():
   tl.set_color('r')

plt.savefig('plot.png')