#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
import tensorflow as tf
import numpy as np
import matplotlib
from cv_bridge import CvBridge
from sensor_msgs.msg import Image

class TLClassifier(object):
    def __init__(self):

        rospy.init_node('tl_classifier')

        PATH_TO_MODEL = 'frozen_inference_graph.pb'
        self.detection_graph = tf.Graph()
        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            # Works up to here.
            with tf.gfile.GFile(PATH_TO_MODEL, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')
            self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')
            self.d_boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
            self.d_scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
            self.d_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
            self.num_d = self.detection_graph.get_tensor_by_name('num_detections:0')
        self.sess = tf.Session(graph=self.detection_graph)

        self.bridge = CvBridge()
        sub1 = rospy.Subscriber('/image_color', Image, self.image_cb)
        self.camera_image = None
        self.light_pub = rospy.Publisher('/tl_classifier', Int32, queue_size=1)
        self.has_image = False

        print('Classifier ready to go')

        self.loop()

    def loop(self):
        rate = rospy.Rate(1)
        while not rospy.is_shutdown():
            if (self.has_image):
                cv_image = self.bridge.imgmsg_to_cv2(self.camera_image, "bgr8")
                self.get_classification(cv_image)
            rate.sleep()

    def image_cb(self, msg):
        """Identifies red lights in the incoming camera image and publishes the index
            of the waypoint closest to the red light's stop line to /traffic_waypoint

        Args:
            msg (Image): image from car-mounted camera

        """
        self.has_image = True
        self.camera_image = msg

    def get_classification(self, img):
        # Bounding Box Detection.
        with self.detection_graph.as_default():
            # Expand dimension since the model expects image to have shape [1, None, None, 3].
            img_expanded = np.expand_dims(img, axis=0)
            (boxes, scores, classes, num) = self.sess.run(
                [self.d_boxes, self.d_scores, self.d_classes, self.num_d],
                feed_dict={self.image_tensor: img_expanded})
#         return boxes, scores, classes, num

        list_ind = np.where(classes[0]==10)
        if len(list_ind[0]) > 0:
            ind = list_ind[0][0]

            ymin = boxes[0][ind][0]
            xmin = boxes[0][ind][1]
            ymax = boxes[0][ind][2]
            xmax = boxes[0][ind][3]

            im_height = 600
            im_width = 800
            (left, right, top, bottom) = (xmin * im_width, xmax * im_width,
                                          ymin * im_height, ymax * im_height)

            imageh = matplotlib.colors.rgb_to_hsv(img)

            tl = imageh[int(top):int(bottom),int(left):int(right),2]

            on = np.where(tl<200)
            tl[on] = 0
            (ymean, xmean) = np.median(np.transpose(np.nonzero(tl)),0)
            height = bottom - top

            if (ymean < height/3):
                tcolor = 0
            elif (ymean > 2.0/3*height):
                tcolor = 2
            else:
                tcolor = 0

        else:
            tcolor = 2

        self.light_pub.publish(tcolor)

        # return tcolor

if __name__ == '__main__':
    try:
        TLClassifier()
    except rospy.ROSInterruptException:
        rospy.logerr('Could not start waypoint updater node.')

# from PIL import Image
#from tl_classifier import TrafficLightClassifier
# tl = TrafficLightClassifier()
# image = Image.open('0003.jpg')
# tl.get_classification(image)


from pathlib import Path
from comparator import Comparator
from constants import Strategy, EtagChunkSizeSet
from typing import Set

class S3ID(Comparator):
    """
    Compare an S3 object ETag and a local file with an 8MB chunk size
    """

    @classmethod
    def unpack(
        cls,
        etag: str,
        local_file_path: Path,
        threshold_in_bytes: int = Strategy.DEFAULT_THRESHOLD,
        partition_set_in_bytes: Set[int] = EtagChunkSizeSet.AWS_S3,
    ) -> bool:
        return cls.run(
            etag,
            local_file_path,
            threshold_in_bytes,
            partition_set_in_bytes,
        )


import tensorflow as tf
import tensorflow.keras.layers as tfl
import numpy as np

import shape_completion_training.model.nn_tools as nn
from shape_completion_training.model.mykerasmodel import MyKerasModel

def log_normal_pdf(sample, mean, logvar, reduce_axis=1):
    log2pi = tf.math.log(2. * np.pi)
    return tf.reduce_sum(
        -.5 * ((sample - mean) ** 2. * tf.exp(-logvar) + logvar + log2pi),
        axis=reduce_axis)

def compute_vae_loss(z, mean, logvar, sample_logit, labels):
    """Computes vae loss given:
    z: latent space sample
    mean: mean of the feature space outputed by the encoder
    logvar: logvar of the feature space outputed by the endocer
    sample_logits: logits (before sigmoid) output of the decoder
    labels: ground truth voxels

    mean, logvar = model.encode(x)
    z = model.reparameterize(mean, logvar)
    sample_logit = model.decode(z)

    See https://tensorflow.org/tutorials/generative/cvae for more details
    """

    cross_ent = tf.nn.sigmoid_cross_entropy_with_logits(logits=sample_logit, labels=labels)
    logpx_z = -tf.reduce_sum(cross_ent, axis=[1, 2, 3])
    logpz = log_normal_pdf(z, 0., 0.)
    logqz_x = log_normal_pdf(z, mean, logvar)
    return -tf.reduce_mean(logpx_z + logpz - logqz_x)

class ThreeD_rec_gan(MyKerasModel):
    def __init__(self, hparams, batch_size, *args, **kwargs):
        super(ThreeD_rec_gan, self).__init__(hparams, batch_size, *args, **kwargs)
        self.layers_dict = {}
        self.layer_names = []
        self.setup_model()
        self.discriminator = make_discriminator([64, 64, 64, 2], self.hparams)
        self.optimizer = tf.keras.optimizers.Adam(hparams['learning_rate'])
        self.gan_opt = tf.keras.optimizers.Adam(hparams['gan_learning_rate'])

    def get_model(self):
        return self

    def predict(self, elem):
        return self(next(elem.__iter__()))

    def _add_layer(self, layer):
        self.layers_dict[layer.name] = layer
        self.layer_names.append(layer.name)

    def setup_model(self):
        nl = self.hparams['num_latent_layers']
        autoencoder_layers = [
            tfl.Conv3D(64, (2, 2, 2), padding="same", name='conv_4_conv'),
            tfl.Activation(tf.nn.relu, name='conv_4_activation'),
            tfl.MaxPool3D((2, 2, 2), name='conv_4_maxpool'),

            tfl.Conv3D(128, (2, 2, 2), padding="same", name='conv_3_conv'),
            tfl.Activation(tf.nn.relu, name='conv_3_activation'),
            tfl.MaxPool3D((2, 2, 2), name='conv_3_maxpool'),

            tfl.Conv3D(256, (2, 2, 2), padding="same", name='conv_2_conv'),
            tfl.Activation(tf.nn.relu, name='conv_2_activation'),
            tfl.MaxPool3D((2, 2, 2), name='conv_2_maxpool'),

            tfl.Conv3D(512, (2, 2, 2), padding="same", name='conv_1_conv'),
            tfl.Activation(tf.nn.relu, name='conv_1_activation'),
            tfl.MaxPool3D((2, 2, 2), name='conv_1_maxpool'),

            tfl.Flatten(name='flatten'),

            tfl.Dense(nl, activation='relu', name='latent'),

            tfl.Dense(32768, activation='relu', name='expand'),
            tfl.Reshape((4, 4, 4, 512), name='reshape'),

            tfl.Conv3DTranspose(256, (2, 2, 2,), strides=2, name='deconv_1_deconv'),
            tfl.Activation(tf.nn.relu, name='deconv_1_activation'),
            tfl.Conv3DTranspose(128, (2, 2, 2,), strides=2, name='deconv_2_deconv'),
            tfl.Activation(tf.nn.relu, name='deconv_2_activation'),
            tfl.Conv3DTranspose(64, (2, 2, 2,), strides=2, name='deconv_3_deconv'),
            tfl.Activation(tf.nn.relu, name='deconv_3_activation'),

            tfl.Conv3DTranspose(1, (2, 2, 2,), strides=2, name='deconv_4_deconv'),
            # tfl.Activation(tf.nn.relu,                    name='deconv_4_activation'),

            # tfl.Conv3DTranspose(1, (2,2,2,), strides=1,   name='deconv_5_deconv', padding="same"),
        ]

        for l in autoencoder_layers:
            self._add_layer(l)

    def call(self, inputs, training=False):
        known_occ = inputs['known_occ']
        # known_free = inputs['known_free']

        unet = self.hparams['is_u_connected']

        # x = tfl.concatenate([known_occ, known_free], axis=4)
        x = known_occ
        x = self.layers_dict['conv_4_conv'](x)
        x = self.layers_dict['conv_4_activation'](x)
        x = self.layers_dict['conv_4_maxpool'](x)
        u4 = x
        x = self.layers_dict['conv_3_conv'](x)
        x = self.layers_dict['conv_3_activation'](x)
        x = self.layers_dict['conv_3_maxpool'](x)
        u3 = x
        x = self.layers_dict['conv_2_conv'](x)
        x = self.layers_dict['conv_2_activation'](x)
        x = self.layers_dict['conv_2_maxpool'](x)
        u2 = x
        x = self.layers_dict['conv_1_conv'](x)
        x = self.layers_dict['conv_1_activation'](x)
        x = self.layers_dict['conv_1_maxpool'](x)
        u1 = x

        x = self.layers_dict['flatten'](x)
        x = self.layers_dict['latent'](x)
        x = self.layers_dict['expand'](x)
        x = self.layers_dict['reshape'](x)

        if unet:
            x = tfl.concatenate([x, u1], axis=4, name='u_1')
        x = self.layers_dict['deconv_1_deconv'](x)
        x = self.layers_dict['deconv_1_activation'](x)
        if unet:
            x = tfl.concatenate([x, u2], axis=4, name='u_2')
        x = self.layers_dict['deconv_2_deconv'](x)
        x = self.layers_dict['deconv_2_activation'](x)
        if unet:
            x = tfl.concatenate([x, u3], axis=4, name='u_3')
        x = self.layers_dict['deconv_3_deconv'](x)
        x = self.layers_dict['deconv_3_activation'](x)
        if unet:
            x = tfl.concatenate([x, u4], axis=4, name='u_4')
        x = self.layers_dict['deconv_4_deconv'](x)
        # x = self.layers_dict['deconv_4_activation'](x)

        # x = self.layers_dict['deconv_5_deconv'](x)

        # Get logits if training, probabilities if inference
        if not training:
            x = tf.nn.sigmoid(x)
        occ = x
        free = 1 - x
        # occ, free = tf.split(x, 2, axis=4)

        return {'predicted_occ': occ, 'predicted_free': free}

    def discriminate(self, known_input, output):
        inp = tf.concat([known_input, output], axis=4)
        return self.discriminator(inp)

    def gradient_penalty(self, known, real, fake):
        alpha = tf.random.uniform([self.batch_size, 1, 1, 1, 1], 0.0, 1.0)
        diff = fake - real
        interp = real + (alpha * diff)
        with tf.GradientTape() as t:
            t.watch(interp)
            pred = self.discriminate(known, interp)
            grad = t.gradient(pred, [interp])[0]
        slopes = tf.sqrt(tf.reduce_sum(tf.square(grad), axis=[1, 2, 3, 4]))
        gp = tf.reduce_mean((slopes - 1.) ** 2)
        return gp

    @tf.function
    def train_step(self, batch):
        with tf.GradientTape(persistent=True) as tape:
            metrics = {}
            ##### Forward pass
            output = self(batch, training=True)
            output_logits = output['predicted_occ']
            # mean, logvar = self.encode(known)
            # z = self.reparameterize(mean, logvar)
            # sample_logit = self.decode(z)
            # sample = tf.nn.sigmoid(sample_logit)
            # output = {'predicted_occ': sample, 'predicted_free': 1 - sample}
            # metrics = nn.calc_metrics(output, batch)
            #
            # #### vae loss
            # vae_loss = compute_vae_loss(z, mean, logvar, sample_logit, labels=batch['gt_occ'])
            # metrics['loss/vae'] = vae_loss
            aeu_loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=output_logits,
                                                               labels=batch['gt_occ'])
            aeu_loss = tf.reduce_mean(aeu_loss)
            ### gan loss
            # fake_occ = tf.cast(output_logits > 0, tf.float32)
            fake_occ = tf.sigmoid(output_logits)
            real_pair_est = self.discriminate(batch['known_occ'], batch['gt_occ'])
            fake_pair_est = self.discriminate(batch['known_occ'], fake_occ)
            gan_loss_g = tf.reduce_mean(-fake_pair_est)
            gan_loss_d_no_gp = tf.reduce_mean(fake_pair_est) - tf.reduce_mean(real_pair_est)

            # gradient penalty
            gp = self.gradient_penalty(batch['known_occ'], batch['gt_occ'], fake_occ)
            gan_loss_d = gan_loss_d_no_gp + 10 * gp

            metrics['loss/gan_g'] = gan_loss_g
            metrics['loss/gan_d'] = gan_loss_d
            metrics['loss/gan_gp'] = gp
            metrics['loss/gan_d_no_gp'] = gan_loss_d_no_gp

            ### apply
            gan_g_w = 20
            aeu_w = 100 - gan_g_w
            generator_loss = aeu_w * aeu_loss + gan_g_w * gan_loss_g
            dis_loss = gan_loss_d

        generator_variables = self.trainable_variables
        generator_gradients = tape.gradient(generator_loss, generator_variables)
        clipped_generator_gradients = [tf.clip_by_value(g, -1e6, 1e6) for g in generator_gradients]

        dis_variables = self.discriminator.trainable_variables
        dis_gradients = tape.gradient(dis_loss, dis_variables)
        clipped_dis_gradients = [tf.clip_by_value(g, -1e6, 1e6) for g in dis_gradients]

        self.optimizer.apply_gradients(list(zip(clipped_generator_gradients, generator_variables)))
        self.gan_opt.apply_gradients(list(zip(clipped_dis_gradients, dis_variables)))

        m = {k: tf.reduce_mean(metrics[k]) for k in metrics}
        m['loss'] = generator_loss
        return None, m

    @tf.function
    def get_insights(self, variables, gradients):
        final_conv = variables[-1]
        final_grad = gradients[-1]
        insights = {}
        if self.params['is_u_connected'] and self.params['use_final_unet_layer']:
            unet_insights = {
                "weights/know_occ->pred_occ": final_conv[-1][0, 0, 2, 0],
                "weights/know_occ->pred_free": final_conv[-1][0, 0, 2, 1],
                "weights/know_free->pred_occ": final_conv[-1][0, 0, 3, 0],
                "weights/know_free->pred_free": final_conv[-1][0, 0, 3, 1],
                "gradients/know_occ->pred_occ": final_grad[-1][0, 0, 2, 0],
                "gradients/know_occ->pred_free": final_grad[-1][0, 0, 2, 1],
                "gradients/know_free->pred_occ": final_grad[-1][0, 0, 3, 0],
                "gradients/know_free->pred_free": final_grad[-1][0, 0, 3, 1]
            }
            insights.update(unet_insights)
        return insights

def make_discriminator(inp_shape, params):
    """Basic Descriminator"""
    return tf.keras.Sequential(
        [
            tfl.InputLayer(input_shape=inp_shape),

            tfl.Conv3D(16, (2, 2, 2), strides=(2, 2, 2)),
            tfl.Activation(tf.nn.leaky_relu),

            tfl.Conv3D(32, (2, 2, 2), strides=(2, 2, 2)),
            tfl.Activation(tf.nn.leaky_relu),

            tfl.Conv3D(64, (2, 2, 2), strides=(2, 2, 2)),
            tfl.Activation(tf.nn.leaky_relu),

            tfl.Conv3D(128, (2, 2, 2), strides=(2, 2, 2)),
            tfl.Activation(tf.nn.leaky_relu),

            tfl.Flatten(),
            # tfl.Dense(1),
            tfl.Lambda(lambda x: tf.reduce_mean(x, axis=[1])),
            tfl.Activation(tf.nn.sigmoid)
        ]
    )


import os
import json

HOSTILE_MOBS = set(range(48, 65) + range(66, 69) + [93])
DANGER_BLOCKS = set([
    'Flowing Lava',
    'Still Lava'
    'Fire'
])
UNBREAKABLE_BLOCKS = set([
    'Bedrock',
])
LIQUID_BLOCKS = set([
    'Flowing Water',
    'Still Water',
    'Flowing Lava',
    'Still Lava',
])
FALLING_BLOCKS = set([
    'Sand',
    'Red Sand',
    'Gravel',
])
ORE = set([
    'Coal Ore',
    'Iron Ore',
    'Redstone Ore',
    'Lapis Lazuli Ore',
    'Gold Ore',
    'Diamond Ore',
    'Emerald Ore',
    'Nether Quartz Ore',
])
FOOD = set([
    'Cooked Porkchop',
    'Steak',
    'Cooked Mutton',
    'Cooked Salmon',
    'Baked Potato',
    'Cooked Chicken',
    'Cooked Rabbit',
    'Rabbit Stew',
    'Mushroom Stew',
    'Bread',
    'Cooked Fish',
    'Carrot',
    'Apple',
    'Melon',
    'Cookie',
])
ARMOR_MATERIAL = set([
    'Diamond',
    'Golden',
    'Iron',
    'Chain',
])
ARMOR = {
    5: 'Helmet',
    6: 'Chestplate',
    7: 'Leggings',
    8: 'Boots',
}
ENCHANT_ITEMS = set([
    'Diamond Helmet',
    'Diamond Chestplate',
    'Diamond Leggings',
    'Diamond Boots',
    'Diamond Sword',
    'Diamond Pickaxe',
    'Diamond Shovel',
    'Diamond Axe',
    'Diamond Hoe',
    'Bow',
    'Fishing Rod',
])
HARVESTABLE_BLOCKS = set([
    ('Wheat', 7),
    ('Carrot Crops', 7),
    ('Potato Crops', 7),
    ('Beetroot Crops', 7),
    ('Melon', 0),
    ('Pumpkin', 0),
])
DOORS = set([
    'Oak Door',
    'Spruce Door',
    'Birch Door',
    'Jungle Door',
    'Acacia Door',
    'Dark Oak Door',
    'Iron Door',
])
WOOD = set([
    'Wood',
    'Wood2',
])
TREES = set([
    'Oak Wood',
    'Spruce Wood',
    'Birch Wood',
    'Jungle Wood',
    'Acacia Wood',
    'Dark Oak Wood',
])
HALF_SLABS = set([44, 182, 126])
TRAP_DOORS = set([96, 167])

class MobTypes(object):
    types = (
        (48, 'Mob'),
        (49, 'Monster'),
        (50, 'Creeper'),
        (51, 'Skeleton'),
        (52, 'Spider'),
        (53, 'Giant Zombie'),
        (54, 'Zombie'),
        (55, 'Slime'),
        (56, 'Ghast'),
        (57, 'Zombie Pigman'),
        (58, 'Enderman'),
        (59, 'Cave Spider'),
        (60, 'Silverfish'),
        (61, 'Blaze'),
        (62, 'Magma Cube'),
        (63, 'Ender Dragon'),
        (64, 'Wither'),
        (65, 'Bat'),
        (66, 'Witch'),
        (67, 'Endermite'),
        (68, 'Guardian'),
        (90, 'Pig'),
        (91, 'Sheep'),
        (92, 'Cow'),
        (93, 'Chicken'),
        (94, 'Squid'),
        (95, 'Wolf'),
        (96, 'Mooshroom'),
        (97, 'Snowman'),
        (98, 'Ocelot'),
        (99, 'Iron Golem'),
        (100, 'Horse'),
        (101, 'Rabbit'),
        (120, 'Villager'),
    )

    @classmethod
    def get_id(cls, name_query):
        for _id, name in cls.types:
            if name == name_query:
                return _id

    @classmethod
    def get_name(cls, id_query):
        for _id, name in cls.types:
            if _id == id_query:
                return name
        return 'Unknown'

class ItemTypes(object):
    types_by_id = {}
    types_by_name = {}
    blocks_by_id = {}
    blocks_by_name = {}
    block_height = {}
    non_solid_types = set([])
    non_climbable_types = set([])
    doors = set([])
    with open(os.path.join(os.path.dirname(__file__), 'types.json')) as _file:
        types = json.load(_file)
    for _, data in types.iteritems():
        if 'blockID' in data:
            _id = data.get('blockID')
            name = data.get('name')
            blocks_by_id[_id] = name
            if name in DOORS:
                doors.add(_id)
            blocks_by_name[name] = _id
            if not data.get('solid', False):
                non_solid_types.add(_id)
            if not data.get('climbable', True):
                non_climbable_types.add(_id)
            if 'height' in data:
                block_height[_id] = data.get('height')
        types_by_id[(data.get('itemID'), None)] = data.get('name')
        types_by_name[data.get('name')] = (data.get('itemID'), None)
        if 'damageValues' in data:
            item_id = data.get('itemID')
            for damage_value, sub_data in data.get('damageValues', {}).iteritems():
                types_by_id[(item_id, int(damage_value))] = sub_data.get('name')
                types_by_name[sub_data.get('name')] = (item_id, int(damage_value))

    @classmethod
    def get_id(cls, name_query):
        return cls.types_by_name.get(name_query)

    @classmethod
    def get_name(cls, id_query, damage_query=None):
        return cls.types_by_id.get((id_query, damage_query),
                                   cls.types_by_id.get((id_query, None)))

    @classmethod
    def get_block_name(cls, id_query):
        return cls.blocks_by_id.get(id_query)

    @classmethod
    def get_block_height(cls, id_query, damage_query=None):
        if id_query in HALF_SLABS and damage_query < 8:
            return 0.5
        elif id_query in TRAP_DOORS and damage_query >> 3 == 0:
            return 0.1875
        return cls.block_height.get(id_query, 1.0)

    @classmethod
    def get_block_id(cls, name_query):
        return cls.blocks_by_name.get(name_query)

    @classmethod
    def is_solid(cls, block_id):
        return block_id not in cls.non_solid_types

    @classmethod
    def is_door(cls, block_id):
        return block_id in cls.doors

    @classmethod
    def is_trap_door(cls, block_id):
        return block_id in TRAP_DOORS

    @classmethod
    def is_unbreakable(cls, block_id):
        name = cls.get_block_name(block_id)
        return name in UNBREAKABLE_BLOCKS

    @classmethod
    def is_liquid(cls, block_id):
        name = cls.get_block_name(block_id)
        return name in LIQUID_BLOCKS

    @classmethod
    def is_falling(cls, block_id):
        name = cls.get_block_name(block_id)
        return name in FALLING_BLOCKS

    @classmethod
    def is_harvestable(cls, block_id, meta):
        name = cls.get_block_name(block_id)
        if name in ('Pumpkin', 'Melon'):
            return True
        return (name, meta) in HARVESTABLE_BLOCKS

    @classmethod
    def is_safe_non_solid(cls, block_id):
        return (
            block_id in cls.non_solid_types and
            cls.blocks_by_name.get(block_id, '') not in DANGER_BLOCKS
        )

    @classmethod
    def is_breathable(cls, block_id):
        return (
            block_id in cls.non_solid_types and
            cls.get_block_name(block_id) not in LIQUID_BLOCKS
        )

    @classmethod
    def is_water(cls, block_id):
        return cls.get_block_name(block_id) == 'Still Water'

    @classmethod
    def is_climbable(cls, block_id):
        return (
            cls.is_solid(block_id) and
            block_id not in cls.non_climbable_types
        )

class InventoryTypes(object):
    types = {
        None: (
            ([0], "crafting output"),
            (range(1, 5), "crafting input"),
            (range(5, 9), "armor"),
            (range(9, 36), "main inventory"),
            (range(36, 45), "held items"),
        ),
        'minecraft:enchanting_table': (
            ([0], "enchantment slot"),
            ([1], "lapis slot"),
            (range(2, 29), "main inventory"),
            (range(29, 38), "held items"),
        ),
    }

    @classmethod
    def get_slot_description(cls, _type, slot_num):
        for _range, description in cls.types.get(_type, []):
            if slot_num in _range:
                return description

class ObjectTypes(object):
    types = (
        (1, 'Boat'),
        (2, 'Item Stack'),
        (10, 'Minecart'),
        (50, 'Activated TNT'),
        (51, 'EnderCrystal'),
        (60, 'Arrow'),
        (61, 'Snowball'),
        (62, 'Egg'),
        (63, 'FireBall'),
        (64, 'FireCharge'),
        (65, 'Thrown Enderpearl'),
        (66, 'Wither Skull'),
        (70, 'Falling Objects'),
        (71, 'Item frames'),
        (72, 'Eye of Ender'),
        (73, 'Thrown Potion'),
        (74, 'Falling Dragon Egg'),
        (75, 'Thrown Exp Bottle'),
        (76, 'Firework Rocket'),
        (77, 'Leash Knot'),
        (78, 'ArmorStand'),
        (90, 'Fishing Float'),
    )

    @classmethod
    def get_id(cls, name_query):
        for _id, name in cls.types:
            if name == name_query:
                return _id

    @classmethod
    def get_name(cls, id_query):
        for _id, name in cls.types:
            if _id == id_query:
                return name
        return 'Unknown'

class Door(object):
    def __init__(self, _id, meta):
        self._id = _id
        self._meta = meta

    def is_open(self):
        return self._meta >> 2 == 0

class TrapDoor(object):
    def __init__(self, _id, meta):
        self._id = _id
        self._meta = meta

    def is_open(self):
        return self._meta >> 2 == 0

    def on_bottom(self):
        return self._meta >> 3 == 0


from odoo import api, fields, models
from odoo.exceptions import UserError, ValidationError

import logging

_logger = logging.getLogger(__name__)

class ImportEbayCategories(models.TransientModel):
	_inherit = "import.ebay.categories"
	_description = "Import Ebay Caetegories"
	_output_selector = "CategoryCount,CategoryArray.Category.CategoryName,CategoryArray.Category.CategoryParentID,CategoryArray.Category.CategoryLevel,CategoryArray.Category.CategoryID"

	@api.model
	def _FetchEbayCategoryByIdList(self, api):
		context = dict(self._context or {})
		if 'categ_list' not in context:
			categ_list = []
		else:
			categ_list = context['categ_list']
		try:
			callData = {
				'CatgegorySiteID': context['ebay_cat_site_id'],
				'CategoryID': context['ebay_category_id'],
				'LevelLimit': context['levellimit'],

			}
			result_dict = api.execute('GetCategoryInfo', callData).dict()
			if context["channel_id"].debug == 'enable':
				_logger.info(
					'--------------Category Response------------%r', result_dict)
			if result_dict['Ack'] == 'Success':
				if result_dict.get('CategoryArray'):
					category = result_dict.get(
						'CategoryArray').get('Category')
					if category.get('CategoryParentID') != '-1':
						categ_list.append(category)
						self.with_context(ebay_category_id=category.get(
							'CategoryParentID'), categ_list=categ_list)._FetchEbayCategoryByIdList(api)
					else:
						category['CategoryParentID'] = ''
					return categ_list.append(category)
		except Exception as e:
			if context["channel_id"].debug == 'enable':
				_logger.info(
					'----------Exception in Fetching Categories-------------------%r', e)
		finally:
			return categ_list

	def import_now(self, **kw):
		feed_list = []
		context = dict(self._context or {})
		ChannelId = kw.get("channel_id")
		context["channel_id"] = ChannelId
		kw.update(page_size=float("inf"))
		MultiChannel = self.env["multi.channel.sale"]
		# print('import_nowimport_nowimport_nowimport_nowimport_nowimport_now',kw)
		if kw.get('filter_type') == 'date_range':
			raise UserError("Date Range filter is not allowed for Exporting Categories.")
		if kw.get('filter_type') == 'all':
			api_obj = MultiChannel._get_api(
				ChannelId, 'Trading')
			if api_obj['status']:
				DefaultData = MultiChannel._get_default_data(ChannelId)
				context['ebay_cat_site_id'] = DefaultData['data']['ebay_cat_site_id']
				context['levellimit'] = kw.get('limit', '1')
				if kw.get('ebay_category'):
					context['ebay_root_category_id'] = kw.get('ebay_category')
				FetchedEbayCategories = self.with_context(
					context)._FetchEbayCategories(api_obj['api'])
				if FetchedEbayCategories['status']:
					feed_list = self.with_context(context)._CreateFeedVals(
						FetchedEbayCategories['result'])
		if kw.get('filter_type') == 'id':
			api_obj = MultiChannel._get_api(
				ChannelId, 'Shopping')
			if api_obj['status']:
				DefaultData = MultiChannel._get_default_data(ChannelId)
				context['ebay_cat_site_id'] = DefaultData['data']['ebay_cat_site_id']
				context['ebay_category_id'] = kw.get('object_id')
				context['levellimit'] = kw.get('limit', '1')
			FetchEbayCategoryList = self.with_context(
				context)._FetchEbayCategoryByIdList(api_obj['api'])
			feed_list = self.with_context(context)._CreateFeedVals(
				FetchEbayCategoryList)
			if 'TemplateCall' in context:
				self.env['category.feed']._create_feeds(feed_list)
				return True
		return feed_list, kw


import json
import requests

response = requests.get("https://places.cit.api.here.com/places/v1/autosuggest?at=56.114526%2C47.198588&q=почта&Accept-Language=en-US%3Ben&app_id=x2p3TDHxbG04n0y1cC9j&app_code=gRs6qSR72e0_-k_nLMgP7A")
todos = json.loads(response.text)
xd_list = []
for x in range(len(todos.get('results'))):
    if todos.get('results')[x].get('category') == 'post-office':
        xd_list.append(todos.get('results')[x].get('distance'))
    else:
        xd_list.append(0)
closest_mail = min(x for x in xd_list if x != 0)
print(todos.get('results')[xd_list.index(closest_mail)].get('title'))
print(todos.get('results')[xd_list.index(closest_mail)].get('position'))


# -*- coding: UTF-8 -*-
import Basehandelr
from Basehandelr import verification
import json
import time
import uuid
from dbTempet import pojcetm
import os

class Userhanderl(Basehandelr.Basehandelr):
    def __init__(self,*args,**kwargs):
        super(Userhanderl,self).__init__(*args,**kwargs)
    def post(self):
        action=self.get_argument("action")
        if action=="login":
            self.login()
        elif action=="register":
            self.register()
        elif action=="update_pswd":
            self.update_pswd()
        elif action=="get_info":
            self.get_info()
        elif action=="upload_video":
            self.upload_video()
        elif action=="delete_video":
            self.delete_video()
        elif action=="new_upload_video":
            self.new_upload_video()
    def get(self):
        pass
    def login(self):
        self.db_linck()
        usname = self.get_argument("usname")
        pswd = self.get_argument("pswd")
        couser=self.Mongodb["AdminUser"].find_one({"usname":usname})
        if couser:
            if couser.get("pswd")!=pswd:
                data = {"code": -1, "data": "密码错误"}
                return  self.write(json.dumps(data))
            if couser.get("Adminid")=="":
                data = {"code": -1, "data": "该账号未审核"}
                return self.write(json.dumps(data))
            data = {"code": 0, "data": "成功"}
            self.set_secure_cookie("pswd",pswd)
            self.set_secure_cookie("token", couser["Adminid"])
            self.write(json.dumps(data))
            return
        else:
            data = {"code": -1, "data": "账号不存在"}
            self.write(json.dumps(data))
            return
    def register(self):
        usname=self.get_argument("usname")
        pswd=self.get_argument("pswd")
        if usname==""or pswd=="":
            data = {"code": -1, "data": "账号密码不可为空"}
            return  self.write(json.dumps(data))
        self.db_linck()
        AdminUser=pojcetm.AdminUser.copy()
        if self.Mongodb["AdminUser"].find_one({"usname":usname}):
            return  self.write(json.dumps({"code": -1, "data": "账号已经存在"}))

        AdminUser["usname"]=usname
        AdminUser["pswd"]=pswd
        AdminUser["money"]=0
        AdminUser["createdate"]=time.time()
        AdminUser["uuid"]=str(uuid.uuid1()).replace("-","")
        try:
            self.Mongodb["AdminUser"].insert_one(AdminUser)
            data = {"code": 0, "data": "注册成功，等待管理员审核"}
            return self.write(json.dumps(data))
        except:
            data = {"code": 0, "data": "数据库错误"}
            return self.write(json.dumps(data))
    @verification
    def get_info(self):
        Adminid=self.get_secure_cookie("token")
        if Adminid:
            self.db_linck()
            coures=self.Mongodb["AdminUser"].find_one({"Adminid":Adminid})
            del coures["_id"]
            uservideo=self.Mongodb["Uservideo"].find_one({"Adminid":Adminid})
            if uservideo:
                coures["videourl"]=uservideo["videourl"]
                coures["videoname"]=uservideo["name"]
            else:
                coures["videourl"]=""
                coures["videoname"]=""
            data = {"code": 0, "data":coures}
            return self.write(json.dumps(data))

    @verification
    def upload_video(self):
        Adminid = self.get_secure_cookie("token")
        files=self.request.files["video"]
        VIDEO_PATH = "/home/DOme/staticfile/video/"
        VIDEO="/video/"
        for i in files:
            file_name=i["filename"]
            body=i["body"]
            self.db_linck()
            coures=self.Mongodb["Uservideo"].find_one({"Adminid":Adminid})
            new_video={"path":VIDEO_PATH+Adminid+file_name,"name":file_name,"Adminid":Adminid,"videourl":VIDEO+Adminid+file_name}
            if coures:
                self.delete_videofile(coures["path"])
                self.Mongodb["Uservideo"].update_one({"Adminid":Adminid},{"$set":new_video})
            else:
                self.Mongodb["Uservideo"].insert_one(new_video)
            self.save_video(new_video["path"],body)
        data = {"code": 0, "data": "修改成功"}
        return self.write(json.dumps(data))

    @verification
    def new_upload_video(self):
        Adminid = self.get_secure_cookie("token")
        files = self.request.files["video"]
        VIDEO_PATH = "/home/DOme/staticfile/video/"
        VIDEO = "/video/"
        for i in files:
            file_name = i["filename"]
            body = i["body"]
            new_video = {"path": VIDEO_PATH + Adminid + file_name, "name": file_name, "Adminid": Adminid,
                         "videourl": VIDEO + Adminid + file_name}

            self.save_video(new_video["path"], body)
        data = {"code": 0, "data": {"url":new_video["videourl"],"filename":new_video["name"]}}
        return self.write(json.dumps(data))
    def delete_videofile(self,path):
        try:
            os.remove(path)
        except:
            pass
    def save_video(self,path,body):
        with open(path, "wb") as f:
            f.write(body)

    @verification
    def update_pswd(self):
        oldpswd=self.get_argument("oldpswd")
        newpswd=self.get_argument("newpswd")
        self.db_linck()
        Adminid = self.get_secure_cookie("token")
        if self.Mongodb["AdminUser"].find_one({"Adminid":Adminid}).get("pswd")==oldpswd:
            self.Mongodb["AdminUser"].update_one({"Adminid":Adminid},{"$set":{"pswd":newpswd}})
            data = {"code": 0, "data": "修改成功"}
            return self.write(json.dumps(data))
        else:
            data = {"code": -1, "data": "密码错误"}
            return self.write(json.dumps(data))
    def delete_video(self):
        Adminid = self.get_secure_cookie("token")
        self.db_linck()
        coures = self.Mongodb["Uservideo"].find_one({"Adminid": Adminid})
        new_video = {"path": "", "name": "", "Adminid": Adminid,
                     "videourl":""}
        if coures:
            self.delete_videofile(coures["path"])
            self.Mongodb["Uservideo"].update_one({"Adminid": Adminid}, {"$set": new_video})
        else:
            self.Mongodb["Uservideo"].insert_one(new_video)
        data = {"code": 0, "data": "修改成功"}
        return self.write(json.dumps(data))


"""
 Bounces a rectangle around the screen.

 Sample Python/Pygame Programs
 Simpson College Computer Science
 http://programarcadegames.com/
 http://simpson.edu/computer-science/

 Explanation video: http://youtu.be/-GmKoaX2iMs
"""

import pygame
import os
import sys
import itertools

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

pygame.init()

clock = pygame.time.Clock()

bg = os.path.join('images', 'scary.jpg')
BckG = pygame.image.load(bg)
# page = os.path.join('images', 'esqueleto.png')
# sheet = pygame.image.load(page).convert_alpha()
# # ind_images = []
# for column in range(9):
#     for row in range(4):
#         single_image = sheet.subsurface((36 * column, 36 * row, 36, 36))
#         ind_images.append(single_image)
# nextFrame = pygame.time.get_ticks()
# frame = 0
# aero_x = 200
# aero_y = 200
# aero_change_x = 0
# aero_change_y = 0

# Set the height and width of the screen
size = [700, 500]
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Bouncing Rectangle")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# Starting position of the rectangle
rect_x = 50
rect_y = 50

# Speed and direction of rectangle
rect_change_x = 2
rect_change_y = 2

# -------- Main Program Loop -----------
while not done:
    # --- Event Processing
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # --- Logic
    # Move the rectangle starting point
    rect_x += rect_change_x
    rect_y += rect_change_y

    # Bounce the ball if needed
    if rect_y > 450 or rect_y < 0:
        rect_change_y = rect_change_y * -1
    if rect_x > 650 or rect_x < 0:
        rect_change_x = rect_change_x * -1

    # --- Drawing
    # Set the screen background
    screen.fill(BLACK)

    # Draw the rectangle
    pygame.draw.rect(screen, WHITE, [rect_x, rect_y, 50, 50])
    pygame.draw.rect(screen, RED, [rect_x + 10, rect_y + 10, 30, 30])

    # --- Wrap-up
    # Limit to 60 frames per second
    clock.tick(60)

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

# Close everything down
pygame.quit()


from django.urls import path

from .api.views import UserListCreateAPIView, UserRetrieveUpdateAPIView

urlpatterns = [
    path('users/', UserListCreateAPIView.as_view(), name='users-list'),
    path('users/<int:pk>/', UserRetrieveUpdateAPIView.as_view(), name='users-detail'),
]

app_name = 'users'


import logging

import netCDF4
import numpy
import pandas
from h3 import h3

# Logger
logger = logging.getLogger(__name__)

def load_ncfile(ncfile, data_variable_name=None):
    """Load a ncfile into a pandas dataframe

    :param ncfile: path of the file to be read
    :type ncfile: string
    :data_variable_name: the name of the data variable in the
        product to load, defaults to None
    :type data_variable_name: string
    :return: a pandas dataframe containing
        the groundpixel information as points
    :rtype: pandas.core.frame.DataFrame
    """

    # read in data
    with netCDF4.Dataset(ncfile, 'r') as f:
        variables = f.groups['PRODUCT'].variables

        # If no data variable name is given, choose the first one
        # that is available from the following lookup table.
        variable_names = ('carbonmonoxide_total_column',
                          'ozone_total_vertical_column',
                          'sulfurdioxide_total_vertical_column',
                          'methane_mixing_ratio_bias_corrected',
                          'formaldehyde_tropospheric_vertical_column',
                          'nitrogendioxide_tropospheric_column'
                          )

        if not data_variable_name:
            data_variable_name = (variables.keys() & variable_names).pop()

        data = variables[data_variable_name][:][0]
        longitude = variables['longitude'][:][0]
        latitude = variables['latitude'][:][0]
        quality = variables['qa_value'][:][0]
        deltatime = variables['delta_time'][:][0]
        meta_data = f.__dict__

    # get some metadata
    # use mask from MaskedArray to filter values
    mask = numpy.logical_not(data.mask)
    n_lines = data.shape[0]  # number of scan lines
    pixel_per_line = data.shape[1]  # number of pixels per line
    time_reference = meta_data['time_reference_seconds_since_1970']

    # convert deltatime to timestamps
    # add (milli-)seconds since 1970
    deltatime = numpy.add(deltatime, time_reference * 1000)
    if len(deltatime.shape) == 1:
        deltatime = numpy.repeat(
            deltatime, pixel_per_line).reshape(n_lines, -1)
    deltatime = deltatime[mask]  # filter for missing data
    timestamps = pandas.to_datetime(deltatime, utc=True, unit='ms')

    # convert data to geodataframe
    return pandas.DataFrame({
            'timestamp': timestamps,
            'quality': quality[mask],
            'value': data[mask],
            'longitude': longitude[mask],
            'latitude': latitude[mask]
        })

def filter_by_quality(dataframe, minimal_quality=0.5):
    """Filter points by quality.

    :param dataframe: a dataframe as returned from load_ncfile()
    :type dataframe: pandas.core.frame.DataFrame
    :param minimal_quality: Minimal allowed quality,
        has to be in the range of 0.0 - 1.0 and defaults to
        0.5 as suggested by the ESA product manual
    :type minimal_quality: float
    :return: the dataframe filtered by the specified value
    :rtype: pandas.core.frame.DataFrame
    """
    has_quality = dataframe.quality >= minimal_quality
    return dataframe[has_quality]

def point_to_h3(dataframe, resolution=1):
    """Convert longitude and latitude in pandas dataframe into h3 indices and
    add them as additional column.

    :param dataframe: a pandas dataframe as returned from load_ncfile()
    :type dataframe: pandas.core.frame.DataFrame
    :param resolution: Resolution of the h3 grid
    :type resolution: uint
    :return: the dataframe including the h3 indices
    :rtype: pandas.core.frame.DataFrame
    """

    # create a new column 'h3' and fill it row-wise with
    # the converted longitudes and latitudes
    dataframe['h3'] = [h3.geo_to_h3(lat, lon, resolution)
                       for lat, lon in
                       zip(dataframe['latitude'], dataframe['longitude'])]

    return dataframe

def h3_to_point(dataframe, h3_column='h3'):
    """Convert H3 index in pandas dataframe into longitude and latitude.

    :param: dataframe: pandas dataframe with a column of H3 indices
    :type dataframe: pandas.core.frame.DataFrame
    :param h3_column: column name of the column with the H3 indices,
                      defaults to h3.
    :type h3_column: str, optional
    """
    lat_lon = numpy.array(
        [h3.h3_to_geo(h3hexagon) for h3hexagon in dataframe['h3']])
    dataframe['latitude'] = lat_lon[:, 0]
    dataframe['longitude'] = lat_lon[:, 1]
    return dataframe

def aggregate_h3(dataframe, function='mean'):
    """Aggregate data values of the same H3 index in dataframe.

    :param dataframe: a pandas dataframe as returned from load_ncfile()
    :type dataframe: pandas.core.frame.DataFrame
    :param function: Aggregation function of
        the data values of the same h3 index
    :type function: str
    :return: new dataframe with the aggregated values
    :rtype: pandas.core.frame.DataFrame
    """

    if function not in ['median', 'mean']:
        raise ValueError("invalid parameter for function")

    # aggregate same indices
    return dataframe.groupby(['h3'], as_index=False).agg({
        'timestamp': 'min',
        'quality': 'min',
        'value': function})


from netsquid.components import (
    Port,
    QSource,
    INSTR_X, INSTR_Z
)
from netsquid.nodes import node
from netsquid.protocols.nodeprotocols import NodeProtocol

def perform_correction(endnode, cur_state):
    if cur_state == 1:
        # |01>
        endnode.qmemory.execute_instruction(INSTR_X)
    elif cur_state == 2:
        # |11>
        endnode.qmemory.execute_instruction(INSTR_Z)
        endnode.qmemory.execute_instruction(INSTR_X)
    elif cur_state == 3:
        # |10>
        endnode.qmemory.execute_instruction(INSTR_Z)

class EntangleNodes(NodeProtocol):
    _is_source: bool = False
    _is_repeater: bool = False
    _is_endnode: bool = False
    _qsource_name: node = None
    _qmem_input_ports: [Port] = []

    def __init__(self, on_node: node, name: str, is_source: bool = False, is_repeater: bool = False,
                 is_endnode: bool = False):
        """
        Parameters:
            on_node: Node to run this protocol on
            is_source: Whether the node serves as a source
            is_repeater: Whether the node serves as a repeater
            is_endnode: Whether the node serves as an endnode (receiver)
            name: Name of the protocol
        """
        super().__init__(node=on_node, name=name)

        self._is_source = is_source
        self._is_repeater = is_repeater
        self._is_endnode = is_endnode

        if not self._is_source:
            self._qmem_input_ports.append(self.node.qmemory.ports["qin0"])
            self.node.qmemory.mem_positions[0].in_use = True

        if self._is_repeater:
            self._qmem_input_ports.append(self.node.qmemory.ports["qin1"])
            self.node.qmemory.mem_positions[1].in_use = True

    def run(self) -> None:
        """
        Send entangled qubits of the source to the two destination nodes.
        """
        if self._is_source or self._is_endnode:
            self.node.subcomponents[self._qsource_name].trigger()

        if not self._is_source:
            yield self.await_port_input(self._qmem_input_ports[0])

        if self._is_endnode:
            yield self.await_port_input(self._qmem_input_ports[1])

    @property
    def is_connected(self) -> bool:
        if self._is_source:
            for name, subcomp in self.node.subcomponents.items():
                if isinstance(subcomp, QSource):
                    self._qsource_name = name
                    break
            else:
                return False

        return True


import sys
import os
import matplotlib.pyplot as plt

"""This python script is used to plot the alpha ablation study results.

Usage:
    python alpha_ablation_plot.py [result_file_path] [output_file_path]
"""

def read_one_performance(result_file_path):
    """read performance from one file.

    result_file_path: str, path to the file of performance results

    """

    with open(result_file_path, "r") as f:
        lines = f.readlines()
        performance = {}
        for line in lines:
            line = line.strip()
            if line.startswith("err_3"):
                performance["err_3"] = float(line.split(" ")[-1])
            if line.startswith("err_5"):
                performance["err_5"] = float(line.split(" ")[-1])
            if line.startswith("err_10"):
                performance["err_10"] = float(line.split(" ")[-1])
            if line.startswith("ndcg_3"):
                performance["ndcg_3"] = float(line.split(" ")[-1])
            if line.startswith("ndcg_5"):
                performance["ndcg_5"] = float(line.split(" ")[-1])
            if line.startswith("ndcg_10"):
                performance["ndcg_10"] = float(line.split(" ")[-1])
    return performance

def read_all_performances(root_file_path, alphas, run_times, fold_nums, click_types):
    """read all performances from files in the root folder.

    root_file_path: str, path to the root folder of performance files
    alphas: list of str, alpha values of the experiment, e.g. ['0e0', '1e-1', '1e-2', '1e-3', '1e-4']
    run_times: list of int, run times of the experiment, e.g. [1,2,3,4,5] for 5 runs
    fold_nums: list of int, fold numbers of the experiment, e.g. [1,2,3,4,5] for 5 folds
    click_types: list of str, click types of the experiment, e.g. ['pbm', 'dcm', 'cascade']

    """

    performances = {}  # restore all performance dicts with different click types
    for click_type in click_types:
        performances[click_type] = {
            "err_3": [],
            "err_5": [],
            "err_10": [],
            "ndcg_3": [],
            "ndcg_5": [],
            "ndcg_10": [],
        }  # for each metric, restore a list of performances with different alphas
        for alpha in alphas:
            tmp_dict = {
                "err_3": [],
                "err_5": [],
                "err_10": [],
                "ndcg_3": [],
                "ndcg_5": [],
                "ndcg_10": [],
            }  # used to restore performances with different run times and fold numbers
            for fold_num in fold_nums:
                for run_time in run_times:
                    result_file_path = os.path.join(
                        root_file_path,
                        "alpha_" + alpha,
                        "fold" + str(fold_num),
                        click_type,
                        "minprob_0.1_eta_1_run" + str(run_time),
                        "performance_test.txt",
                    )
                    performance = read_one_performance(result_file_path)  # dict
                    tmp_dict["err_3"].append(performance["err_3"])
                    tmp_dict["err_5"].append(performance["err_5"])
                    tmp_dict["err_10"].append(performance["err_10"])
                    tmp_dict["ndcg_3"].append(performance["ndcg_3"])
                    tmp_dict["ndcg_5"].append(performance["ndcg_5"])
                    tmp_dict["ndcg_10"].append(performance["ndcg_10"])
            performances[click_type]["err_3"].append(
                sum(tmp_dict["err_3"]) / (len(run_times) * len(fold_nums))
            )
            performances[click_type]["err_5"].append(
                sum(tmp_dict["err_5"]) / (len(run_times) * len(fold_nums))
            )
            performances[click_type]["err_10"].append(
                sum(tmp_dict["err_10"]) / (len(run_times) * len(fold_nums))
            )
            performances[click_type]["ndcg_3"].append(
                sum(tmp_dict["ndcg_3"]) / (len(run_times) * len(fold_nums))
            )
            performances[click_type]["ndcg_5"].append(
                sum(tmp_dict["ndcg_5"]) / (len(run_times) * len(fold_nums))
            )
            performances[click_type]["ndcg_10"].append(
                sum(tmp_dict["ndcg_10"]) / (len(run_times) * len(fold_nums))
            )

    return performances

def plot_figures(performances, alphas, output_file_path):
    """plot performance curves of all metrics for each click type

    performances: dict, performances of all click types
    alphas: list of str, alpha values of the experiment, e.g. ['0e0', '1e-1', '1e-2', '1e-3', '1e-4']
    output_path: str, path to the output file
    """

    click_types = list(performances.keys())
    metrics = performances[click_types[0]].keys()
    alphas = [float(x) for x in alphas]

    colors = ["r", "g", "b", "y", "m", "c", "k"]

    ## plot multiple figures in one big figure (len(metrics) rows, len(click_types) columns)
    fig, axs = plt.subplots(
        len(metrics), len(click_types), figsize=(20, 30), sharex="all", sharey="row"
    )
    for click_type in click_types:
        for i, metric in enumerate(metrics):
            axs[i, click_types.index(click_type)].plot(
                alphas,
                performances[click_type][metric],
                label=metric,
                marker="^",
                color=colors[i],
            )
            if i == 0:
                axs[i, click_types.index(click_type)].set_title(click_type)
            # axs[i, click_types.index(click_type)].legend()
    fig.tight_layout()
    lines, labels = [], []
    for ax in axs[:, 0]:
        axLine, axLabel = ax.get_legend_handles_labels()
        lines.extend(axLine)
        labels.extend(axLabel)
    # legend at the bottom of the big figure
    fig.legend(lines, labels, loc="outside center right", ncol=6)
    plt.savefig(output_file_path)

if __name__ == "__main__":
    result_file_path = sys.argv[1]
    output_file_path = sys.argv[2]
    alphas = ["0e0", "1e-3", "5e-3", "1e-2", "5e-2", "1e-1", "5e-1", "1e0", "5e0", "1e1", "5e1"]
    # run_times = [1, 2, 3, 4, 5]
    run_times = [1, 2, 3]
    # run_times = [1]
    # fold_nums = [1, 2, 3, 4, 5]
    fold_nums = [1]
    click_types = ["pbm", "cascade", "dcm", "ccm"]

    performances = read_all_performances(
        root_file_path=result_file_path,
        alphas=alphas,
        run_times=run_times,
        fold_nums=fold_nums,
        click_types=click_types,
    )

    plot_figures(performances, alphas, output_file_path)


# -*- coding: utf-8 -*-
"""
@author: 赵磊
@project: UGPU_Server
@file: jpg_mosaicking.py
@time: 2019/3/13 12:32
@description:
"""
import os
from PIL import Image

UNIT_SIZE = 1000

def pinjie(images, num, num_w=4, num_h=3):
    target = Image.new('RGB', (UNIT_SIZE * num_w, UNIT_SIZE * num_h))
    dim1 = []
    dim2 = []
    for n in range(num_w):
        dim1.append(n * UNIT_SIZE)
    for m in range(num_h):
        dim2.append((m + 1) * UNIT_SIZE)

    for i in range(len(images)):
        if i < num_w:
            target.paste(images[i], (dim1[i], 0, (i + 1) * UNIT_SIZE, dim2[0]))
        elif i >= num_w and i < num_w * 2:
            target.paste(images[i], (dim1[i - num_w], UNIT_SIZE, (i + 1 - num_w) * UNIT_SIZE, dim2[1]))
        else:
            target.paste(images[i], (dim1[i - 2 * num_w], 2 * UNIT_SIZE, (i + 1 - 2 * num_w) * UNIT_SIZE, dim2[2]))

    quality_value = 100
    target.save(path + dirlist[num] + '.jpg', quality=quality_value)

# path = "C:/Users/laojbdao/Desktop/FinalResult/result4/different_distribution/"
path = "test_9/"
dirlist = []  # all dir name
for root, dirs, files in os.walk(path):
    for dir in dirs:
        dirlist.append(dir)
num = 0
for dir in dirlist:
    images = []  # images in each folder
    for root, dirs, files in os.walk(path + dir):  # traverse each folder
        print(path + dir + '')

        files_adjust = []
        for ii, item in enumerate(files):
            if ii != 2 and ii != 3:
                files_adjust.append(item)
            elif ii == 2:
                item2 = item
            else:
                item3 = item
        files_adjust.append(item2)
        files_adjust.append(item3)

        for file in files_adjust:
            images.append(Image.open(path + dir + '/' + file))
        pinjie(images, num)
        num += 1
        images = []


import cv2

# Path to image in local directory
path = '/Users/vuhn/Desktop/test.png'

# Using cv2.imread() to read an image in grayscale mode
image = cv2.imread(path, 0)

# Using namedWindow()
# A window with 'Display_Image' name is created
# with WINDOW_NORMAL allowing us to have random size
cv2.namedWindow("Display_Image", cv2.WINDOW_NORMAL)

# Using cv2.imshow() to display the image
cv2.imshow('Display_Image', image)

# Waiting 0ms for user to press any key
cv2.waitKey(1000)

# Using cv2.destroyAllWindows() to destroy
# all created windows open on screen
cv2.destroyAllWindows()


texto = input("Digite algo: ").upper()

caracteres = []
for i in texto:
    if i not in caracteres:
        caracteres.append(i)
caracteres.sort()

print(f"|-----------------|")
print(f"| Nº | char | qtd |")
print(f"|----|------|-----|")
for letra, i in zip(caracteres, range(1, len(caracteres) + 1)):
    qtd_letra = texto.count(letra)
    print(f"| {'' if i > 9 else 0}{i} |  {letra}   | {'' if qtd_letra > 9 else 0}{qtd_letra}  |")
print(f"|-----------------|")

Exercicios/Outros/Faculdade/Aula 09 - Strings/3.py


# for loops basics
"""
write for loops that can do some basic functions:
loop1: print all int from 0 to 150
loop2: print all the multiples of 5 from 5 to 1000
loop3: print integers 1 to 100, if divisible by 5, print 'coding' instead. if divisible by 10, print 'coding dojo'
loop4: add odd ints from 0 to 500,000 and print the final sum
loop5: print positive numbers starting at 2018, counting down by fours
loop6: set 3 vars lowNum, highNum, mult. iterate in range from low to high printing only multiples of mult.
"""

num1 = 150
num2 = 1000
num3 = 100
num4 = 500000
num5 = 2018
lowNum = 2
highNum = 50
mult = 5
total = 0

for x in range(0,num1+1): #loop 1 print int from 0 to 150
        print(x)

for y in range(5,num2+1): #loop 2 print all multiples of 5 between 5 and 1k
    if y % 5 ==0:
        print(y)

for z in range(1,num3+1): #loop 3 print ints from 1 to 100, if multiple of 5, print coding, else if multiple of 10, print coding dojo
    result = z
    if z % 5 == 0:
        result = "coding"
    if z % 10 == 0:
        result += " dojo"
    print(result)

for a in range(0,num4+1): #loop 4 add odd ints from 0 to 500k and print final sum
    if not a % 2:
        total += a
print(total) # give me the total result of what the last loop calculated

while num5 >=0: #loop 5 print positive numbers only, counting down by 4 starting at 2018
    print(num5)
    num5-= 4
    if num5 <= 0:
        break

for c in range(lowNum, highNum +1): #loop 6
    if c % mult == 0:
        print(c)


import pyautogui
from time import sleep
import csv
import pandas as pd

pyautogui.click(123,253,duration=5)
pyautogui.write('treinando python ate ser um senior')

pyautogui.click(133,276,duration=5)
pyautogui.write('ter foco e objetivo')

pyautogui.click(135,286,duration=5)
pyautogui.write('ser determinado')

with open ("teste.python.txt",'r') as csvfile:
    for linha in csvfile:
        csvreader = csv.DictReader(csvfile)
        nome =  linha.split(',')[0]
        nome1 =  linha.split(',')[1]
        nome2 =  linha.split(',')[2]

#apertar para digitar nome
pyautogui.click(113,304,duration=5)
pyautogui.write(nome)
#apertar no nome da pessoa
pyautogui.click(125,321,duration=2)
pyautogui.write(nome1)
#apertar em forma de acesso
pyautogui.click(131,336,duration=2)
pyautogui.write(nome2)
sleep(2)


# LFU
# Scheme
#   -> Replace the page with the smallest reference counts
# Tie-breaking
#   -> LRU
# Requirements
#   -> Accumulated page reference count

def LFUalgorithm(n, m, k, array):
    memory_state = []
    page_fault = 0
    page_fault_list = [None] * k
    timestamp = []      # reference 순서 저장
    # frequency = [0] * n # reference count (인덱스=페이지 번호, 원소값=빈도수)
    frequency = {}      # 리스트 대신 딕셔너리 사용
    # for i in range(1, n+1):
    #     frequency[i] = 0

    for i in range(k):
        replace = 0
        if len(memory_state) < m:
            if array[i] not in memory_state:
                memory_state.append(array[i])
                page_fault_list[i] = bool(True)
                page_fault += 1
        else:
            if array[i] not in memory_state:
                freq_of_memory_state = {}
                # memory_state 원소 중 frequency가 가장 작은 page 찾기
                for key, value in frequency.items():
                    if key in memory_state:
                        freq_of_memory_state[key] = value

                min_freq = min(freq_of_memory_state.values())
                min_freq_key = [key for key in freq_of_memory_state.keys() if freq_of_memory_state[key] == min_freq]

                if len(min_freq_key) == 1:
                    replace = min_freq_key[0]   # min_freq_key = [1] 리스트 형태로 저장되어 슬라이싱 해줌
                else:
                    # Tie-breaking: LRU strategies
                    # min_freq_key 에서 timestamp 순서가 가장 빠른 key 선택
                    for j in timestamp:
                        if j in min_freq_key:
                            replace = j
                            break
                memory_state[memory_state.index(replace)] = array[i]
                page_fault_list[i] = bool(True)
                page_fault += 1

        if array[i] in timestamp:
            timestamp.remove(array[i])
        timestamp.append(array[i])
        # print("timestamp: ", timestamp)

        if array[i] not in frequency:
            frequency[array[i]] = 1
        else:
            frequency[array[i]] += 1
        # print("frequency: ", frequency)
        # print('\n')

        if i==0:
            print("메모리 상태 변화 과정 (page fault 발생 위치 표시)")
            print()
        print("{}번째".format(i))
        print("Memory_state: {}".format(memory_state))
        print("page fault: {}".format(page_fault_list[i]))
        print()

    return print("총 page fault 횟수: {}".format(page_fault))


import os
import pandas as pd
import json
import csv

def readCSVFiles():
    master_df = pd.DataFrame()
    for file in os.listdir(os.getcwd()):
        if file.endswith('.csv'):
            master_df = master_df.append(pd.read_csv(file))
    # Convert to one csv file
    if os.path.isfile('master_file.csv'):
        if os.stat('master_file.csv').st_size == 0:
            master_df.to_csv('master_file.csv', index=False)
        else:
            print('files has been already scanned')
    else:
        print('files has been scanned!')
        # dont write rows name index=False
        master_df.to_csv('master_file.csv', index=False)

def convertToJson():
    with open("master_file.csv", "r") as f:
        reader = csv.reader(f)
        title = next(reader)
        index1 = title.index('first_name')
        index2 = title.index('last_name')
        index3 = title.index('employee_id')
        index4 = title.index('date_of_birth')

        data = {"employees": []}
        for row in reader:
            data["employees"].append({"first_name":
                                      row[index1], "last_name": row[index2], "employee_id": row[index3], "date_of_birth": row[index4]})

    with open("employees.json", "w") as f:
        json.dump(data, f, indent=4)

if __name__ == '__main__':
    readCSVFiles()
    convertToJson()
    os.system('python3 server.py')


from django.shortcuts import render , render_to_response
from django.http import HttpResponse
from django.template import RequestContext
from django.views.generic.base import View
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from models import *
# Create your views here.

def all_items(request):
    temp_var = {}
    temp_var['title'] = 'All Items'
    temp_var['items'] = Items.objects.all()
    return render_to_response(
        'items/items.html',
        temp_var,
        context_instance = RequestContext(request)
    )
    #return HttpResponse('all items')

def item(request,sku):
    temp_var = {}
    temp_var['title'] = 'Single Items'
    temp_var['item'] = Items.objects.get(sku=sku)
    return render_to_response(
        'items/item.html',
        temp_var,
        context_instance = RequestContext(request)
    )

class vendorItem(View):
    template_name = 'items/vendor_items.html'

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(vendorItem, self).dispatch(*args, **kwargs)

    def get(self,request):
        temp_var = {}
        temp_var['title'] = 'All Items'
        temp_var['items'] = Items.objects.all()
        return render_to_response(
            self.template_name,
            temp_var,
            context_instance = RequestContext(request)
        )


from fastapi.exceptions import HTTPException
from pydantic import BaseModel
from typing import Optional

class AvaliacaoProfessor:
    def __init__(self, matriculaEstudante, idProfessor, textoAvaliacao, nivel, idAvaliacaoProfessor):
        self.matriculaEstudante = matriculaEstudante
        self.idProfessor = idProfessor
        self.textoAvaliacao = textoAvaliacao
        self.nivel = nivel
        self.idAvaliacaoProfessor = idAvaliacaoProfessor

class AvaliacaoProfessorPost(BaseModel):
    matriculaEstudante: int
    idProfessor: int
    textoAvaliacao: str
    nivel: int

class AvaliacaoProfessorUpdate(BaseModel):
    textoAvaliacao: Optional[str]
    nivel: Optional[int]

class AvaliacaoProfessorDeleteRequest(BaseModel):
    matriculaEstudanteLogado: int
    idAvaliacaoProfessor: int

class AvaliacaoProfessorDeleteResponse:
    def __init__(self, matriculaEstudante, idAvaliacaoProfessor, admin):
        self.matriculaEstudante = matriculaEstudante
        self.idAvaliacaoProfessor = idAvaliacaoProfessor
        self.admin = admin

class AvaliacaoProfessorDAO:
    def __init__(self, db, cursor):
        self.db = db
        self.cursor = cursor

    def get_avaliacao_professor_por_matricula(self, matriculaEstudante):
        try:
            self.cursor.execute(f"SELECT * FROM avaliacaounb.AvaliacaoProfessor WHERE matriculaEstudante = {matriculaEstudante}")
            resposta = self.cursor.fetchall()
            print(resposta)
            if resposta is None:
                raise HTTPException(status_code=404)
            avaliacoesProfessor = [AvaliacaoProfessor(*avaliacao) for avaliacao in resposta]
            return avaliacoesProfessor

        except Exception as err:
            print(err)
            raise err

    def get_avaliacao_professor_por_professor(self, idProfessor):
        try:
            self.cursor.execute(f"SELECT * FROM avaliacaounb.AvaliacaoProfessor WHERE idProfessor = {idProfessor}")
            resposta = self.cursor.fetchall()
            if resposta is None:
                raise HTTPException(status_code=404)

            avaliacoesProfessor = [AvaliacaoProfessor(*avaliacao) for avaliacao in resposta]
            return avaliacoesProfessor

        except Exception as err:
            print(err)
            raise err

    def add_avaliacao_professor(self, avaliacao: AvaliacaoProfessorPost):
        try:
            query = "INSERT INTO avaliacaounb.AvaliacaoProfessor (matriculaEstudante, idProfessor, textoAvaliacao, nivel) "
            query += f"VALUES ({avaliacao.matriculaEstudante}, {avaliacao.idProfessor}, '{avaliacao.textoAvaliacao}', {avaliacao.nivel});"
            self.cursor.execute(query)
            self.db.commit()
            self.cursor.execute("SELECT LAST_INSERT_ID();")
            avaliacaoInserida = self.cursor.fetchone()
            return avaliacaoInserida

        except Exception as err:
            print(err)
            raise err

    def update_avaliacao_professor(self, idAvaliacaoProfessor, avaliacao: AvaliacaoProfessorUpdate):
        try:
            query = "UPDATE avaliacaounb.AvaliacaoProfessor SET"

            if avaliacao.nivel is not None:
                query += f" nivel={avaliacao.nivel},"

            if avaliacao.textoAvaliacao is not None:
                query += f" textoAvaliacao='{avaliacao.textoAvaliacao}',"

            final_query = query.rstrip(query[-1])
            final_query += f" WHERE idAvaliacaoProfessor={idAvaliacaoProfessor};"

            self.cursor.execute(final_query)
            self.db.commit()

        except Exception as err:
            print(err)
            raise err

    def delete_avaliacao_professor(self, idAvaliacaoProfessor, matriculaEstudante):
        # try:
        #     self.cursor.execute(f"DELETE FROM avaliacaounb.AvaliacaoProfessor WHERE idAvaliacaoProfessor={idAvaliacaoProfessor};")
        #     self.db.commit()
        try:
            query = "SELECT at2.matriculaEstudante, at2.idAvaliacaoProfessor, e.admin "
            query += "FROM avaliacaounb.AvaliacaoProfessor at2 "
            query += "INNER JOIN avaliacaounb.Estudantes e "
            query += "ON at2.matriculaEstudante = e.matriculaEstudante "
            query += f"WHERE at2.idAvaliacaoProfessor = {idAvaliacaoProfessor}"

            self.cursor.execute(query)
            resposta = self.cursor.fetchone()

            if resposta is None:
                raise HTTPException(status_code=404, detail="Não há avaliação com esse id")

            self.cursor.execute(f"SELECT admin FROM avaliacaounb.Estudantes WHERE matriculaEstudante = {matriculaEstudante}")
            admin = self.cursor.fetchone()

            if admin is None:
                raise HTTPException(status_code=404, detail="Estudante não cadastrado")
            admin = admin[0]

            avaliacao = AvaliacaoProfessorDeleteResponse(*resposta)
            if avaliacao.matriculaEstudante == matriculaEstudante or admin == 1:
                queryDelete = "DELETE FROM avaliacaounb.AvaliacaoProfessor WHERE "
                queryDelete += f"idAvaliacaoProfessor={idAvaliacaoProfessor};"
                self.cursor.execute(queryDelete)
                self.db.commit()
                return

            raise HTTPException(status_code=403, detail="Usuário não escreveu avaliação ou não é um administrador")

        except Exception as err:
            print(err)
            raise err


# coding: utf-8

# changing the structure type of json data, classifying the type of user' input command and connecting to batabase
class MessageHandler:

    def __init__(self, message, user_id):
        self.message = message["message"]
        self.user_id = user_id
        self.message_array = self.message.split("\n")

        switch = {
        u"登録": "create",
        u"削除": "delete",
        u"表示": "show"
        }
        method_name = unicode(self.message_array[0], "utf-8")
        self.method = switch[method_name]

        method_array = [u"登録", u"削除", u"表示"]
        if not method_name in method_array:
            self.method = "etc"

    def create(self):
        message_array = self.message_array
        message_array.extend(["", "", ""])
        # add the empty element (because prevent "index of out range" with message_array[3or2or1])
        return {
            "method": "create",
            "user_id": self.user_id,
            "data": {
              "date": message_array[1],
              "title": message_array[2],
              "description": message_array[3]
            }
        }

    def delete(self):
        message_array = self.message_array
        message_array.append("")
        # add the empty element
        return {
            "method": "delete",
            "user_id": self.user_id,
            "data": {
                "id": message_array[1]
            }
        }

    def show(self):
        return {
            "method": "show",
            "user_id": self.user_id
        }

    def excute(self):

        switch = {
            "create": self.create(),
            "delete": self.delete(),
            "show": self.show(),
            "etc": {"method": "etc", "user_id": self.user_id}
        }

        return switch[self.method]


def pi(err, abs=abs):
    'Approximation of pi +/- err using the Mandlebrot set'
    n = 0
    z = c = complex(-0.75, err)
    while abs(z) < 2.0:
            n += 1
            z = z * z + c
    return n * err

if __name__ == '__main__':
    for i in range(1, 8):
        err_bound = 10 ** (-i)
        print(pi(err_bound), 'within', err_bound)


# -*- coding: utf-8 -*-
"""
Created on Wed Jun 16 11:22:46 2021

@author: Uno
"""

import numpy as np
import pandas as pd
import os
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.preprocessing import StandardScaler
from keras.layers import Dropout

from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping

def onelayer_model():
    '''
    This function build a neural network with one layer. The number of neuron in the hidden layer is 8, a little bit larger than the dimension of the dataset.
    For more information about the model, please visit:
    https://keras.io/guides/sequential_model/

    You can find an example on how to use this model in the fold StarclassML/NNclassifier.py
    '''
    n_feature = X_train.shape[1]
    model = Sequential()
    model.add(Dense(8, input_dim=n_feature, activation='relu'))
    model.add(Dense(6, activation='softmax'))

    model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

def twolayers_model():
    '''
    This function build a neural network with two layers. The number of neuron in the hidden layer is 8, a little bit larger than the dimension of the dataset.
    For more information about the model, please visit:
    https://keras.io/guides/sequential_model/

    You can find an example on how to use this model in the fold StarclassML/NNclassifier.py
    '''
    n_feature = X_train.shape[1]
    model = Sequential()
    model.add(Dense(8, input_dim=n_feature, activation='relu'))
    model.add(Dense(8, activation='relu'))
    model.add(Dense(6, activation='softmax'))

    model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

def build_DROPOUT_model():
    '''
    This function build a deep neural network with 4 layers. The number of neuron in the hidden layer is 64, but we added a dropout which set to zero in a random way
    the synaptic weights between each layer.
    For more information, please visit:
    https://keras.io/guides/sequential_model/

    You can find an example on how to use this model in the fold StarclassML/NNclassifier.py
    '''
    model = Sequential()

    n_feature = X_train.shape[1]
    h_dim = 64
    model.add(Dense(h_dim, activation='relu', input_shape=(n_feature,)))
    model.add(Dropout(0.5))
    model.add(Dense(h_dim, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(h_dim, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(h_dim, activation='relu'))
    model.add(Dense(6, activation='softmax'))

    model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

if __name__ == '__main__':
    PATH_ACTUAL = os.getcwd()
    PATH = PATH_ACTUAL + "/data/Stars.csv"
    df = pd.read_csv(PATH)

    df['logR'] = np.log10(df['R'].values)
    df = df.filter(items=['logR', 'A_M', 'Temperature', 'Type'])

    attributes = [col for col in df.columns if col != 'Type']
    X = df[attributes].values
    y = df['Type']

    CV_scores1 = []
    CV_scores2 = []
    skf = StratifiedKFold(n_splits=5)
    es = EarlyStopping(monitor='val_loss', patience=50)
    #Vedo le performance dei modelli utilizzando la crossvalidation.
    for train_index, test_index in skf.split(X, y):
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]

        ##NOrmalizziamo i dati
        scaler = StandardScaler()
        scaler.fit(X_train)
        X_train = scaler.transform(X_train)
        X_test = scaler.transform(X_test)

        ##splitto in train set e validation set
        X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2)

        #Definisco i modelli e faccio partire il training.
        model1 = onelayer_model()
        print(type(model1))
        history1 = model1.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=1000, batch_size=5, callbacks=[es]).history

        model2 = twolayers_model()
        history2 = model2.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=1000, batch_size=5, callbacks=[es]).history

        ##stampo le performance sul test set
        test_loss_1, test_acc_1 = model1.evaluate(X_test, y_test)
        test_loss_2, test_acc_2 = model2.evaluate(X_test, y_test)

        CV_scores1.append(test_acc_1)
        CV_scores2.append(test_acc_2)

    ##calcolo la media dei risultati ottenuti su ciascun fold.
    CV_mean1 = np.average(CV_scores1)
    CV_mean2 = np.average(CV_scores2)
    CV_std1 = np.std(CV_scores1)
    CV_std2 = np.std(CV_scores2)

    #Vediamo cosa otteniamo facendo un modello più complesso con dropout
    CV_scores = []
    for train_index, test_index in skf.split(X, y):
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]

        scaler = StandardScaler()
        scaler.fit(X_train)
        X_train = scaler.transform(X_train)
        X_test = scaler.transform(X_test)

        X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2)

        model = build_DROPOUT_model()
        history = model.fit(X_train, y_train, validation_data = (X_val, y_val), epochs=1000, batch_size=5,
                            callbacks=[es]).history

        test_loss, test_acc = model.evaluate(X_test, y_test)

        CV_scores.append(test_acc)

    CV_mean = np.average(CV_scores)
    CV_std = np.std(CV_scores)
    print(f'La cross validation accuracy per il modello dropout è {CV_mean} +- {CV_std}')
    print(f'La cross validation accuracy per il modello 1 è {CV_mean1} +- {CV_std1}')
    print(f'La cross validation accuracy per il modello 2 è {CV_mean2} +- {CV_std2}')


# 가위 바위 보 게임 만들기

# '''
# - 게임 방법
# 1. 당신(you)은 가위, 바위, 보 중 하나를 입력한다.
# 2. 컴퓨터(com)은 가위, 바위, 보 중 하나를 랜덤 생성한다
# 3. 결과 출력은 무승부, 패, 승이다.
# 4. 잘못 입력한 경우, "잘못된 입력입니다. 다시 입력해주세요"

# '''

import pygame
import random

rps = ("가위", "바위", "보")

# 초기화
pygame.init()

# 화면 크기
# screen_width = 640
# screen_height = 480
# screen = pygame.display.set_mode((screen_width, screen_height))
# pygame.display.set_caption("가위 바위 보 게임")

# 게임 루프
while True:
    player = None
    comp = random.choice(rps)

    while player not in rps:

        player = input("가위, 바위, 보 중에서 선택하세요. 종료하려면 '종료'를 입력하세요: ")

        if player == "종료":
            print("게임을 종료합니다.")
            exit()

        if player == comp:
            print("무승부!")
        elif player == "가위" and comp == "보":
            print("승리!")
        elif player == "바위" and comp == "가위":
            print("승리!")
        elif player == "보" and comp == "바위":
            print("승리!")
        else:
            print("패배!")

    print(f"당신 : {player}")
    print(f"컴퓨터 : {comp}")

# 게임 종료
pygame.quit()


import secrets
from Crypto.Cipher import AES
from Crypto.Cipher.AES import block_size
import math

global_key = secrets.token_bytes(block_size)
iv = secrets.token_bytes(block_size)

def xor(a, b):
	ans = ""
	for x,y in zip(a,b):
		ans = ans + chr(x^y)
	return ans

# Pad string to n-byte block size
def pad_string(s, n):
	s_len = len(s)
	pad = ((math.ceil(s_len/n))*n) - s_len
	return s + bytes(chr(pad)*pad, "utf-8")

# Encrypt string after appropriate transformation
def encrypt(s):
	# Remove ';' and '=' from s
	s = s.replace(';', '')
	s = s.replace('=', '')
	s = "comment1=cooking%20MCs;userdata=" + s + ";comment2=%20like%20a%20pound%20of%20bacon"
	s = pad_string(bytes(s, 'utf-8'), block_size)

	# Encrypt with CBC	 mode
	enc_obj = AES.new(global_key, AES.MODE_CBC, iv)
	ciphertext = enc_obj.encrypt(s)
	return ciphertext

# Decrypt string
def decrypt(ciphertext):
	dec_obj = AES.new(global_key, AES.MODE_CBC, iv)
	if len(ciphertext)%16 != 0:
		return b""
	plaintext = dec_obj.decrypt(ciphertext)
	return plaintext

# Detect if ";admin=true" is present
def detect(s):
	s = s.decode("utf-8", "replace").split(';')
	for chunk in s:
		chunk = chunk.split('=')
		if chunk[0] == "admin" and chunk[1] == "true":
			return True
	return False

# Insert admin=true in plaintext
def attack():
	inp = "A"*5
	inp = inp + "xadminxtrue"
	ciphertext = encrypt(inp)
	block2 = ciphertext[block_size:2*block_size]
	for i in range(256):
		for j in range(256):
			tmp = block2[:5] + bytes(chr(i),"utf-8") + block2[6:11] + bytes(chr(j),"utf-8") + block2[12:]
			tmp = ciphertext[:block_size] + tmp + ciphertext[2*block_size:]
			pt = decrypt(tmp)
			if detect(pt):
				print(pt)
				return
	print("Failed")
	return

attack()


import json
from flask import Flask, request
from .connectToDatabase import connectDB
from .rowLimiter import rowLimiter

app = Flask(__name__)

@app.post('/database')
def database():
    try:
        connection, cursor = connectDB()

        toReturn = cursor.execute(request.get_json()['input']).fetchall()
        connection.close()

        print(request.get_json())
        return {
            "ok": True,
            "data": toReturn
        }
    except:
        connection.close()
        return {
            "ok": False,
            "data": "wrong input"
        }

@app.route('/linefollower', methods=[ 'POST', 'GET' ])
def lineFolower():
    connection, cursor = connectDB()

    if request.method == "POST":
        try:
            data = request.get_json()

            cursor.execute("""INSERT INTO linefollower (
                temperature,
                sens1,
                sens2,
                sens3,
                sens4,
                sens5,
                sens6
            ) VALUES(?,?,?,?,?,?,?);""", [
                data['temperature'],
                data['sens1'],
                data['sens2'],
                data['sens3'],
                data['sens4'],
                data['sens5'],
                data['sens6']
            ])

            rowLimiter(cursor)

            connection.commit()
            connection.close()
            return {
                "ok": True
            }

        except:
            connection.close()
            return {
                'ok': False
            }

    elif request.method == "GET":

        toReturn = cursor.execute("SELECT * FROM linefollower;").fetchall()

        rowLimiter(cursor)

        connection.close()
        return {
            "ok": True,
            "data": toReturn
        }

@app.get('/test')
def testGet():
    return {"data": "test"}

@app.post('/test')
def testPost():
    print(request.get_json())

    return "Data Received"

if __name__ == '__main__':
    app.run(debug=True)


''' Performs background subtraction and normalization.
IMPORTANT:
Images are assumed to be in [width, height] indexing, despite convention.
This is to retain ffmpeg and freeimage compatibility.
'''

from collections import namedtuple
import numpy

from swimfunction.video_processing import fp_ffmpeg
from swimfunction import FileLocations

# To grayscale using weights from
# https://scikit-image.org/docs/dev/auto_examples/color_exposure/plot_rgb_to_gray.html
RGB_WEIGHTS = numpy.asarray([0.2125, 0.7154, 0.0721])

CropArea = namedtuple('CropArea', ['top_left_x', 'top_left_y', 'width', 'height'])

StandardizerInnerContent = namedtuple(
    'StandardizerInnerContent',
    ['target_median', 'normalizing_lut', 'v_median_normalized'])

class Standardizer:
    ''' Subtracts median and normalizes a full-sized (left and right sides not split apart) video.
    '''
    def __init__(
            self, v_median,
            has_divider,
            inner_content: StandardizerInnerContent=None):
        self.v_median = v_median
        if inner_content is not None:
            self.target_median = inner_content.target_median
            self.normalizing_lut = inner_content.normalizing_lut
            self.v_median_normalized = inner_content.v_median_normalized
        else:
            if has_divider:
                median_path = FileLocations.GlobalFishResourcePaths.target_median_frame_with_divider
            else:
                median_path = FileLocations.GlobalFishResourcePaths\
                    .target_median_frame_without_divider
            self.target_median = fp_ffmpeg.freeimage_read(median_path)
            self.normalizing_lut = get_normalizing_lut(self.v_median, self.target_median)
            vmn = self.apply_normalizing_function(self.v_median)
            self.v_median_normalized = vmn - vmn.max()

    def apply_normalizing_function(self, frame):
        ''' Converts values from each channel of a frame
                into a normalization like the target_median_frame,
                based on median_frame of the video.
                Also grayscales that frame.
        Returns
        -------
        normalized_frame: numpy.ndarray
            dtype is float
        '''
        return self.normalizing_lut[0, frame[:, :, 0]] \
            + self.normalizing_lut[1, frame[:, :, 1]] \
            + self.normalizing_lut[2, frame[:, :, 2]]

    def get_standardized_frame(self, rgb_frame: numpy.ndarray, crop_area: CropArea=None):
        ''' Subtracts the background and uses a normalizing function.

        Parameters
        ----------
        rgb_frame : numpy.ndarray
        crop_area : CropArea, optional
        '''
        normalized = self.apply_normalizing_function(
            crop_img(rgb_frame, crop_area)
        )
        bg_subtracted = subtract_median(
            normalized,
            crop_img(self.v_median_normalized, crop_area)
        )
        return buffer_to_size(bg_subtracted, crop_area, fill_high=True)

def _to_uint8_by_clip(frame):
    # Note: this is faster than numpy.clip
    # because numpy.clip creates an intermediate array of dtype float.
    clipped = frame.astype(numpy.uint8)
    clipped[frame > 255] = 255
    clipped[frame < 0] = 0
    return clipped

def get_buffer_val(dtype, fill_with_max: bool):
    ''' Get a filler value based on the dtype.
    Either the largest or smallest possible value (depending on fill_with_max)
    '''
    return numpy.iinfo(dtype).max if fill_with_max else numpy.iinfo(dtype).min

def buffer_to_size(crop: numpy.ndarray, crop_area: CropArea, fill_high: bool):
    ''' Forces an exact size for the image.
    Overflow is filled either as dtype max (fill_high=True) or min.
    '''
    if crop_area is None \
        or (crop.shape[0] == crop_area.width \
            and crop.shape[1] == crop_area.height):
        return crop
    buffer_val = get_buffer_val(crop.dtype, fill_high)
    buffered = None
    if len(crop.shape) == 3:
        buffered = numpy.full((crop_area.width, crop_area.height, 3), buffer_val, dtype=crop.dtype)
    else:
        buffered = numpy.full((crop_area.width, crop_area.height), buffer_val, dtype=crop.dtype)
    buffered[:crop.shape[0], :crop.shape[1]] = crop
    return buffered

def crop_img(img, crop: CropArea, copy=False) -> numpy.ndarray:
    ''' Crops an image using slice indexing. If copy, will return a new array.
    '''
    if crop is None:
        return img
    bottom_right_x = min(crop.top_left_x + crop.width, img.shape[0])
    bottom_right_y = min(crop.top_left_y + crop.height, img.shape[1])
    cropped_img = img[
        crop.top_left_x:bottom_right_x,
        crop.top_left_y:bottom_right_y]
    return cropped_img.copy() if copy else cropped_img

def subtract_median(frame, v_median):
    ''' Subtracts the median frame and converts to unit8 by clipping values.
    '''
    if not numpy.issubdtype(frame.dtype, numpy.floating):
        frame = frame.astype(float)
    return _to_uint8_by_clip(frame - v_median)

def get_normalizing_lut(v_median, target_median, apply_grayscale_weights=True):
    ''' NOTE: normalizing look up table also applies RGB weights for grayscale.
    '''
    scale_factors = numpy.median(target_median, axis=(0, 1)) / numpy.median(v_median, axis=(0, 1))
    lut = numpy.asarray([
        (numpy.arange(256) * sf).clip(0, 255).astype(numpy.uint8)
        for sf in scale_factors
    ])
    if not apply_grayscale_weights:
        return lut
    return lut * RGB_WEIGHTS[:, numpy.newaxis]


#!/usr/opt/bs-python-2.7/bin/python

import os
import sys
import datetime

sys.path.append(os.path.realpath(__file__ + '/../../../lib'))

import udf

class NaNAndNOTNULL(udf.TestCase):
    def setUp(self):
        self.query('DROP SCHEMA FN2 CASCADE', ignore_errors=True)
        self.query('CREATE SCHEMA FN2')
        self.query('''create view
	v as
select
	7 a
from
	dual''')

    def test_insert_nan_in_notnull_column(self):
        rows = self.query('''select replace(view_text, char(9), 'X') from exa_all_views
                                    where view_name='V' ''')
        self.assertEqual('create view\nXv as\nselect\nX7 a\nfrom\nXdual', rows[0][0])

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


def twoSum(nums, target):
    a = nums
    b = target
    c = len(a)
    m = c-1
    ans = []

    for i in range(m):
        if i < m :
            for x in range(i+1,m+1):
                if a[i] + a[x] == b:
                    ans.append(i)
                    ans.append(x)
                    return ans
                break

nums1 = [2,7,11,15]
target1 = 9
print(twoSum(nums1,target1))


# two levels of filtering:
# 1. remove regions that don't look confidently introgressed at all,
#    based on fraction gaps/masked, number of matches to S288c and not S288c
#    --> _filtered1
# 2. remove regions that we can't confidently pin on a specific reference,
#    based on whether it matches similarly to other reference(s)
#    --> _filtered2

# just do the first level here, then run filter_2_thresholds_main.py
# to choose filtering thresholds for next level

import re
import sys
import os
import copy
import read_args
from filter_helpers import *
import summarize_region_quality
import global_params as gp
from misc import read_table
from misc import read_fasta

args = read_args.process_predict_args(sys.argv[1:])

for species_from in args['known_states'][1:]:

    print(species_from)

    fn = gp.analysis_out_dir_absolute + args['tag'] + '/' + \
         'blocks_' + species_from + \
         '_' + args['tag'] + '_quality.txt'
    region_summary, fields = read_table.read_table_rows(fn, '\t')

    fields1i = fields + ['reason']
    fields1 = fields

    fn_out1i = gp.analysis_out_dir_absolute + args['tag'] + '/' + \
              'blocks_' + species_from + \
              '_' + args['tag'] + '_filtered1intermediate.txt'

    fn_out1 = gp.analysis_out_dir_absolute + args['tag'] + '/' + \
              'blocks_' + species_from + \
              '_' + args['tag'] + '_filtered1.txt'

    f_out1i = open(fn_out1i, 'w')
    f_out1i.write('\t'.join(fields1i) + '\n')

    f_out1 = open(fn_out1, 'w')
    f_out1.write('\t'.join(fields1) + '\n')

    regions_fn = gp.analysis_out_dir_absolute + args['tag'] + '/regions/' + \
                 species_from + gp.fasta_suffix + '.gz'
    region_seqs = summarize_region_quality.read_region_file(regions_fn)

    for region_id in region_summary:

        region = region_summary[region_id]

        info_string = region_seqs[region_id]['info']['seq']

        # filtering stage 1: things that we're confident in calling not
        # S288c
        p, reason = passes_filters1(region, info_string, args['known_states'][0])
        region['reason'] = reason
        write_filtered_line(f_out1i, region_id, region, fields1i)

        if p:
            write_filtered_line(f_out1, region_id, region, fields1)

    f_out1i.close()
    f_out1.close()


import argparse
import config as cfg
from load_data import GeneData
from scalers import allowed_scaler_names, build_scaler
from shapes.shape import get_shape_by_name, allowed_shape_names
from shapes.priors import get_allowed_priors
from fitter import Fitter
from dev_stages import PCW

class MyParser(argparse.ArgumentParser):
    def convert_arg_line_to_args(self, arg_line):
        if arg_line.startswith('#'): # support comments
            return
        for arg in arg_line.split():
            if not arg.strip():
                continue
            yield arg

dct_ages = {
    'postnatal' : 0,
    'EF3' : PCW(10),
}

known_datasets = ['kang2011', 'colantuoni2011', 'both', 'harris2009', 'somel2010-human', 'brainspan2014']

def get_common_parser(include_pathway=True):
    parser = MyParser(fromfile_prefix_chars='@')
    group = parser.add_mutually_exclusive_group()
    group.add_argument("-v", "--verbose", action="count", default=0, help="increase output verbosity")
    group.add_argument("-q", "--quiet", action="count", default=0, help="decrease output verbosity")
    parser.add_argument('--dataset', default='both', help='Default: both', choices=known_datasets)
    if include_pathway:
        parser.add_argument('--pathway', default='serotonin', help='Default: serotonin') #, choices=['all'] + cfg.pathways.keys())
    parser.add_argument('--from_age', help='Use only data points with larger ages than this. Default: all ages', choices=dct_ages.keys())
    parser.add_argument('--scaling', help='What scaling to use for ages. Default: none', default='none', choices=allowed_scaler_names())
    parser.add_argument('--shuffle', help='Shuffle the y-values of the data', action='store_true')
    parser.add_argument('-s', '--shape', help='The shape to use for fitting. Default: sigslope', default='sigslope', choices=allowed_shape_names())
    parser.add_argument('--sigma_prior', help='Prior to use for 1/sigma when fitting. Default: None', choices=get_allowed_priors(is_sigma=True))
    parser.add_argument('--priors', help='Priors to use for theta when fitting. Default: None', choices=get_allowed_priors())
    return parser

def process_common_inputs(args):
    cfg.verbosity = args.verbose - args.quiet
    pathway = getattr(args, 'pathway', None)
    data = get_data_from_args(args.dataset, pathway, args.from_age, args.scaling, args.shuffle)
    fitter = get_fitter_from_args(args.shape, args.priors, args.sigma_prior)
    return data,fitter

def get_data_from_args(dataset, pathway, from_age, scaling, shuffle):
    data = GeneData.load(dataset).restrict_pathway(pathway)
    if from_age is not None:
        restriction_name = from_age
        from_age = dct_ages[from_age]
        data.restrict_ages(restriction_name,from_age=from_age)
    scaler = build_scaler(scaling,data)
    if scaler is not None:
        data.scale_ages(scaler)
    if shuffle:
        data.shuffle()
    return data

def get_fitter_from_args(shape, priors, sigma_prior):
    shape = get_shape_by_name(shape, priors)
    fitter = Fitter(shape, sigma_prior=sigma_prior)
    return fitter


# Taken from https://github.com/yunjey/pytorch-tutorial/blob/master/tutorials/03-advanced/image_captioning/build_vocab.py
import copy
import csv
import json
import os
import pickle
from collections import Counter

import nltk
from pycocotools.coco import COCO

from .utils import tenumerate

# A simple wrapper class for Vocabulary. No changes are required in this file
class Vocabulary(object):
    """Simple vocabulary wrapper."""

    def __init__(self):
        self.word2idx = {}
        self.idx2word = {}
        self.idx = 0

    def add_word(self, word):
        if not word in self.word2idx:
            self.word2idx[word] = self.idx
            self.idx2word[self.idx] = word
            self.idx += 1

    def __call__(self, word):
        if not word.lower() in self.word2idx:
            return self.word2idx['<unk>']
        return self.word2idx[word.lower()]

    def __len__(self):
        return len(self.word2idx)

def load_vocab(json, threshold):
    if os.path.isfile('saved_vocab.pkl'):
        with open('saved_vocab.pkl', 'rb') as f:
            vocab, loaded_threshold = pickle.load(f)
            if loaded_threshold == threshold:
                return vocab
            print("Using the saved vocab.")

    vocab = build_vocab(json, threshold)
    with open('saved_vocab.pkl', 'wb') as f:
        pickle.dump((vocab, threshold), f)
        print("Saved the vocab.")

    return vocab

def build_vocab(json, threshold):
    coco = COCO(json)
    counter = Counter()
    ids = coco.anns.keys()
    for i, id_ in tenumerate(ids):
        caption = str(coco.anns[id_]['caption'])
        tokens = nltk.tokenize.word_tokenize(caption.lower())
        counter.update(tokens)

        if tenumerate == enumerate and (i + 1) % 1000 == 0:
            print("[{}/{}] Tokenizing Captions.".format(i + 1, len(ids)))

    # If the word frequency is less than 'threshold', then the word is discarded.
    words = [word for word, cnt in counter.items() if cnt >= threshold]

    # Create a vocab wrapper and add some special tokens.
    vocab = Vocabulary()
    vocab.add_word('<pad>')
    vocab.add_word('<start>')
    vocab.add_word('<end>')
    vocab.add_word('<unk>')

    # Add the words to the vocabulary.
    for word in words:
        vocab.add_word(word)
    return vocab


# Stock Predictions

'''
Input Format

The input of each turn will consist of multiple lines. All money values are doubles to two decimal places, all other numbers are integers.

The first line contains three space separated numbers m k d.

m - the amount of money you could spend that day.
k - the number of different stocks available for buying or selling.
d - the number of remaining days for trading stocks.
k lines follow, each in the following format: name owned prices

name - the name of the stock (a string).
owned - the number of shares you own of that stock.
prices - 5 space separated numbers representing the stock's price for the last 5 days. These are ordered from oldest to newest, so the last number is the current stock price.

Output Format

The output for each turn should also contain multiple lines:

Output N for the number of transactions you wish to make. Output 0 if you are not making any transactions that day.

If you are making transactions, output N lines containing the name of the stock (case sensitive), BUY or SELL, and the number of shares you wish to buy or sell.

Sample Input

90 2 400
iStreet 10 4.54 5.53 6.56 5.54 7.60
HR 0 30.54 27.53 24.42 20.11 17.50
Sample Output

2
iStreet SELL 10
HR BUY 5

'''

import numpy as np
import math
def printTransactions(m, k, d, name, owned, prices):
    slope_list=[]
    for i in range(k):
        x=np.array([1.0,2.0,3.0,4.0,5.0])
        y= np.array(prices[i])
        A = np.vstack([x, np.ones(len(x))]).T
        slope, constat = np.linalg.lstsq(A, y)[0]
        slope_list.append(slope)

    sort_slope = sorted(slope_list)
    amount_remaining= m
    trans={}
    for j in reversed(range(k)):
        index_slope = slope_list.index(sort_slope[j])
        if sort_slope[j]<=0 and amount_remaining>=prices[index_slope][-1]:
            #print prices[index_slope][-1]
            num_stock =math.floor(amount_remaining/prices[index_slope][-1])
            amount_remaining = amount_remaining - prices[index_slope][-1]*num_stock
            trans[name[index_slope]] = names[index_slope]+' BUY ' + str(int(num_stock))

    for j in range(k):
        index_slope = slope_list.index(sort_slope[j])
        if sort_slope[j]>0 and owned[index_slope]>0:
            trans[name[index_slope]] = names[index_slope]+' SELL ' + str(owned[index_slope])

    print(len(trans.keys()))
    for x in trans:
        print(trans[x])

if __name__ == '__main__':
    m, k, d = [float(i) for i in input().strip().split()]
    k = int(k)
    d = int(d)
    names = []
    owned = []
    prices = []
    for data in range(k):
        temp = input().strip().split()
        names.append(temp[0])
        owned.append(int(temp[1]))
        prices.append([float(i) for i in temp[2:7]])

    printTransactions(m, k, d, names, owned, prices)


def fit(X, Y):

    def mean(Xs):
        return sum(Xs) / len(Xs)
    m_X = mean(X)
    m_Y = mean(Y)

    def std(Xs, m):
        normalizer = len(Xs) - 1
        return math.sqrt(sum((pow(x - m, 2) for x in Xs)) / normalizer)
    # assert np.round(Series(X).std(), 6) == np.round(std(X, m_X), 6)

    def pearson_r(Xs, Ys):

        sum_xy = 0
        sum_sq_v_x = 0
        sum_sq_v_y = 0

        for (x, y) in zip(Xs, Ys):
            var_x = x - m_X
            var_y = y - m_Y
            sum_xy += var_x * var_y
            sum_sq_v_x += pow(var_x, 2)
            sum_sq_v_y += pow(var_y, 2)
        return sum_xy / math.sqrt(sum_sq_v_x * sum_sq_v_y)
    # assert np.round(Series(X).corr(Series(Y)), 6) == np.round(pearson_r(X, Y), 6)

    r = pearson_r(X, Y)

    b = r * (std(Y, m_Y) / std(X, m_X))
    A = m_Y - b * m_X

    def line(x):
        return b * x + A
    return line