import re
from random import shuffle
import pickle
import os
import discord
#permissions 201329664

bot = discord.Client()

names = ["somthing went wrong"]

optIN = {}
optOut = {}

@bot.event
async def on_ready():
    global names
    global optIN
    print('Bot ready!')
    print(bot.user.name)
    print('-------')
    if os.path.exists('names.p'):
        names = pickle.load(open("names.p", "rb"))
    else:
        names = ["DadBot"]*20

    if os.path.exists('optIN.p'):
        optIN = pickle.load(open("optIN.p", "rb"))
    else:
        optIN = {}

    if os.path.exists('optOut.p'):
        optOut = pickle.load(open("optOut.p", "rb"))
    else:
        optOut = {}

@bot.event
async def on_guild_join(guild):
    await guild.system_channel.send("Hi "+guild.name+", I'm DadBot!")

@bot.event
async def on_message(message):
    global names
    global optIN
    global optOut
    try:
        if message.author == message.guild.me:
            return

        if message.content.lower() == "optin":
            if not optIN.get(message.author.id, False):
                if not message.guild.me.top_role.permissions.manage_nicknames:
                    await message.channel.send("I cannot change nicknames, but I'll put you on the list.")
                elif message.author.top_role > message.guild.me.top_role:
                    await message.channel.send("You outrank me, but I'll put you on the list.")
                optIN[message.author.id] = True
                pickle.dump(optIN, open("optIN.p", "wb"))

                optOut[message.author.id] = message.author.display_name
                pickle.dump(optOut, open("optOut.p", "wb"))
            return

        if message.content.lower() == "optout":
            if optIN.get(message.author.id, False):
                optIN.pop(message.author.id)
                pickle.dump(optIN, open("optIN.p", "wb"))
                if optOut[message.author.id] != message.author.display_name:
                    await message.author.edit(nick=optOut[message.author.id], reason="DadBot")
            return

        word = re.search(r'\bi.?m\s+(.*)', message.content, re.IGNORECASE)
        if word is None:
            return
        if len(word.group(1)) > 32:
            word = re.search(r'\bi.?m\s+(\w+)', message.content, re.IGNORECASE)
        word = word.group(1)

        if word.lower() == "DadBot".lower():
            await message.channel.send("Wait, you're me?")
            return

        if word.lower() == "Dad".lower():
            await message.channel.send("Wait, you're me?")
            return

        shuffle(names)
        await message.channel.send("Hi "+word+", I'm " + names.pop() + "!")
        names.append(word)
        pickle.dump(names, open("names.p", "wb"))

        if optIN.get(message.author.id, True):
            if not message.guild.me.top_role.permissions.manage_nicknames:
                #await message.channel.send(
                #    "Somebody named "+message.guild.owner.display_name+" won't let me change names.")
                return
            if message.author.top_role > message.guild.me.top_role:
                #if message.author == message.guild.owner:
                #    await message.channel.send(
                #        "Somebody named "+message.author.display_name+
                #        " won't let me change their name.")
                #else:
                #    await message.channel.send(
                #        "Somebody named "+message.author.display_name+
                #        " won't let me change their name."
                #        " (It might be "+message.guild.owner.display_name+"'s fault)")
                return
            if message.author == message.guild.owner:
                return
            if len(word) > 32:
                word = word[:27] + "..."
            await message.author.edit(nick=word, reason="DadBot")

    except Exception as exc:
        print(repr(exc))
        await message.channel.send(
            "Something went wrong. It is probably "+message.author.display_name+"'s fault.")

bot.run(open("TOKEN", "r").read().rstrip())


import os
import logging
import subprocess
import time
import sys
import re
from datetime import datetime
from GPSPhoto import gpsphoto
import geopy
import geopy.distance
# from PIL import Image
# from PIL.ExifTags import TAGS, GPSTAGS
# from exif import Image

# Uncomment following line to redirect all logging to STDOUT
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
log = logging.getLogger(__name__)

class PixCam:
    # Constructor: initializes array of command-line args, checks working directory and gphoto2 installation
    # Can accept optional camera argument (currently unused)
    def __init__(self, working_dir, camera="Sony Alpha-A5000 (Control)"):
        self.args = ["gphoto2"]

        # Check working directory
        if not os.path.isdir(working_dir) or not os.path.isabs(working_dir):
            raise FileNotFoundError("Invalid absolute directory path")
        self.working_dir = working_dir
        log.info('Working directory: %s', working_dir)
        # set an arg flag to create a custom, time-based naming system for images
        self.set_flag("--filename", r"'{0}{1}%Y-%m-%d--%H-%M-%S.%C'".format(working_dir, os.path.sep))

        # Crude check that gphoto2 is installed
        try:
            if "Usage" not in subprocess.check_output('gphoto2').decode('ascii'):
                raise Exception()
            log.info("gphoto2 installation identified")
        except:
            log.error("gphoto2 not set up")
            raise SystemError("gphoto2 not set up")

        # Crude check that camera is connected
        if not self.check_camera_connection():
            log.error("Cannot find camera")
            raise SystemError("Cannot find camera")
        log.info("Camera connected")

    # Take a picture and return picture path and picture name or None in case of failure
    def take_pic(self):
        log.info("Taking a picture")
        output = self.execute_cmd("--capture-image-and-download")
        if output is None:
            return None, None
        log.info("Output: %s", output)
        image_path = re.search("Saving file as (.*?)[\n]", output).group(1)
        return image_path, os.path.basename(image_path)

    # Run any command with necessary flags prefixed and return readable/writeable output
    # In case of an error, returns None and logs error
    def execute_cmd(self, cmd):
        try:
            command_str = ' '.join(self.args + [cmd])
            log.info("Executing command: %s", command_str)
            return subprocess.check_output(command_str, shell=True).decode("ascii")
        except subprocess.CalledProcessError as e:
            log.error("Command failed: %s", e)
            return None

    # Takes a picture and embeds crude gps data into metadata
    # Use if drone is stationary
    # Lat/Long in DD with sign, alt in ft ASL
    # Ex. 45, -40, 400
    def take_pic_and_record_loc(self, lat, long, alt=0):
        image_path, image_name = self.take_pic()

        # with open(image_path, "rb") as image_file:
        #     image = Image(image_file)
        #
        # print(image_old_date)
        image_datetime = self.get_datetime_from_img_name(image_name)
        # print(image_datetime)
        image_new_date = self.get_exif_date_from_datetime(image_datetime)
        # print(image.get("datetime"))
        # print(image_new_date)

        # Convert GPS data from DD to DMS
        # lat_abs = abs(lat)
        # long_abs = abs(long)
        # if lat < 0:
        #     lat_ref = "S"
        # else:
        #     lat_ref = "N"
        # if long < 0:
        #     long_ref = "W"
        # else:
        #     long_ref = "E"

        self.add_gps_metadata(image_path, lat, long, image_new_date, alt=alt)

        # lat_dms = self.dd_to_dms(lat_abs)
        # long_dms = self.dd_to_dms(long_abs)
        # log.info("Recording gps coordinates: %d° %d' %d\" %s, %d° %d' %d\" %s", lat_dms[0], lat_dms[1], lat_dms[2],
        #          lat_ref, long_dms[0], long_dms[1], long_dms[2], long_ref)
        # image.set("gps_latitude", lat_dms)
        # image.set("gps_latitude_ref", lat_ref)
        # image.set("gps_longitude", long_dms)
        # image.set("gps_longitude_ref", long_ref)
        log.info("Recorded gps coordinated to image metadata")

        return image_path, image_name

    def add_gps_metadata(self, image_path, lat, long, timestamp, alt=0):
        # Convert alt to m
        alt = int(alt * 0.3048)

        log.info("Recording gps coordinates: %f, %f", lat, long)
        photo = gpsphoto.GPSPhoto(image_path)
        info = gpsphoto.GPSInfo((lat, long), timeStamp=timestamp, alt=alt)
        photo.modGPSData(info, image_path)

    def get_datetime_from_img_name(self, image_name):
        return datetime.strptime(image_name.split(".")[0], "%Y-%m-%d--%H-%M-%S")  # "yyyy-MM-dd--HH-mm-ss"

    def get_exif_date_from_datetime(self, dt):
        return dt.strftime("%Y:%m:%d %H:%M:%S")  # yyyy:MM:dd HH:mm:ss

    # Mathematical conversion from decimal degrees to degrees/minutes/seconds. Returns tuple.
    def dd_to_dms(self, dd):
        degrees = int(dd)
        minutes = int(60*(dd-degrees))
        seconds = int(3600*(dd-degrees-(minutes/float(60))))
        return degrees, minutes, seconds

    # Takes a picture and embeds adjusted gps data into metadata
    # Use if drone is moving. Inherently flawed since the precision of the filename is to the second
    # Velocity in ft/s, heading in degrees
    def take_pic_and_adjust_loc(self, lat, long, velocity, heading, alt=0):
        # Find how much time passes between GPS reading and picture snap
        init_time_sec = float(datetime.now().timestamp())
        image_path, image_name = self.take_pic()
        timestamp_datetime = self.get_datetime_from_img_name(image_name)
        timestamp = self.get_exif_date_from_datetime(timestamp_datetime)
        final_time_sec = float(timestamp_datetime.timestamp())
        seconds_passed = final_time_sec - init_time_sec

        distance = seconds_passed * velocity
        # Find new GPS location
        start_coord = geopy.Point(lat, long)
        end_point = geopy.distance.geodesic(feet=distance).destination(start_coord, heading)
        new_lat = end_point.latitude
        new_long = end_point.longitude

        self.add_gps_metadata(image_path, new_lat, new_long, timestamp, alt=alt)
        return image_path, image_name

    # Returns true if camera is found, false otherwise
    def check_camera_connection(self):
        try:
            output = self.execute_cmd("--summary")
            if "*** Error: No camera found. ***" in output:
                return False
            return True
        except Exception as e:
            return False

    # Set flag: key and value represent flag and option respectively:
    # ex. -f filename -> "-f" is the key, "filename" is the value
    # This is converted to a string and added to command-line args for gphoto2
    def set_flag(self, key, value):
        log.info("Attempting to add/update key value pair %s:%s", key, value)
        if not key.startswith("-"):
            raise ValueError("Key must start with -")
        if key in self.args:
            log.info("Existing flag found. Updating...")
            index = self.args.index(key)
            self.args[index+1] = value
        else:
            log.info("Existing flag not found. Adding...")
            self.args.extend([key, value])

    def get_flag(self, key):
        if key in self.args:
            index = self.args.index(key)
            value = self.args[index + 1]
            log.info("Flag %s found, value: %s", key, value)
            return value
        else:
            log.info("Flag %s not found", key)
            return None

    def __del__(self):
        pass

if __name__ == "__main__":
    print("Initiating testing protocol")
    cam = PixCam("/home/uasucla/Pictures/gphoto_pics")
    res1, res2 = cam.take_pic_and_adjust_loc(34.068458, -118.442819, 40, 0)
    print(res1)
    print(res2)
    print(os.path.isfile(res1))


from copy import deepcopy
import numpy as np
import torch
from torch.utils.data import Dataset

from tokenizers import Tokenizer
from tokenizers.models import WordLevel
from tokenizers.pre_tokenizers import Whitespace
from transformers import T5Tokenizer

from .build import DATASETWRAPPER_REGISTRY

class BaseTokenizer:
    def __init__(self, vocab, bos_token='<s>', eos_token='</s>', sep_token='<###>', pad_token='<pad>'):
        self.vocab = [pad_token, bos_token, eos_token, sep_token] + vocab
        self.w2i = {w: i for i, w in enumerate(self.vocab)}
        self.bos_token = bos_token
        self.eos_token = eos_token
        self.sep_token = sep_token
        self.pad_token = pad_token
        tokenizer = Tokenizer(WordLevel(vocab=self.w2i))
        tokenizer.pre_tokenizer = Whitespace()
        tokenizer.add_special_tokens([pad_token, bos_token, eos_token, sep_token])
        self.tokenizer = tokenizer

    def encode(self, text, pad_direction='right'):
        if isinstance(text, str):
            text = [text]
        self.tokenizer.enable_padding(direction=pad_direction, pad_id=self.vocab.index(self.pad_token), pad_token=self.pad_token)
        outputs = self.tokenizer.encode_batch(text)
        ids = torch.LongTensor([output.ids for output in outputs])
        masks = torch.LongTensor([output.attention_mask for output in outputs])
        max_len = len(outputs[0])
        return max_len, ids, masks

    def decode(self, ids):
        pass

@DATASETWRAPPER_REGISTRY.register()
class GPTWrapper(Dataset):
    def __init__(self, cfg, dataset):
        self.dataset = dataset
        self.tokenizer = BaseTokenizer(dataset.vocab_input + dataset.vocab_output)
        self.use_cot = cfg.use_cot

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

    def __getitem__(self, index):
        return self.dataset[index]

    def collate_fn(self, batch):
        bos_token, eos_token, sep_token = self.tokenizer.bos_token, self.tokenizer.eos_token, self.tokenizer.sep_token

        def concat_sentence(sample):
            sentence = [bos_token] + sample['input']
            if self.use_cot:
                for thought in sample['cot']:
                    sentence += [sep_token] + thought
            sentence += [sep_token] + sample['output'] + [eos_token]
            return ' '.join(sentence)

        _, concat_ids, concat_masks = self.tokenizer.encode([concat_sentence(sample) for sample in batch], pad_direction='right')
        _, input_ids, input_masks = self.tokenizer.encode([' '.join([bos_token] + sample['input'] + [sep_token])
                                                for sample in batch], pad_direction='left') # padding left to avoid the padding token in the middle of the sequence for generation.
        _, output_ids, output_masks = self.tokenizer.encode([' '.join(sample['output'] + [eos_token])
                                                for sample in batch], pad_direction='right')

        new_batch = {}
        for key in batch[0].keys():
            new_batch[key] = [sample[key] for sample in batch]

        new_batch['input_ids'] = input_ids
        new_batch['input_masks'] = input_masks
        new_batch['output_ids'] = output_ids
        new_batch['output_masks'] = output_masks
        new_batch['concat_ids'] = concat_ids
        new_batch['concat_masks'] = concat_masks
        return new_batch

@DATASETWRAPPER_REGISTRY.register()
class T5Wrapper(Dataset):
    def __init__(self, cfg, dataset):
        self.dataset = dataset
        self.tokenizer = T5Tokenizer.from_pretrained(cfg.model.variant)
        self.input_types = getattr(cfg, 'input_types', 'input').split('-')
        self.output_types = getattr(cfg, 'output_types', 'output').split('-')

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

    def __getitem__(self, index):
        return self.dataset[index]

    def collate_fn(self, batch):

        def tokenize_batch(types, delimiter=' ; '):
            sequences = []
            for sample in batch:
                seq = ''
                for tp in types:
                    if tp == 'input':
                        seq += 'input: ' + (sample['input'] if isinstance(sample['input'], str) else ' '.join(sample['input']))
                    elif tp == 'tree':
                        seq += 'tree: ' + sample['tree']
                    elif tp == 'steps':
                        seq += 'steps: ' + ' , '.join([f'{i} = {o}' for i, o in sample['steps']])
                    elif tp == 'results':
                        seq += 'results: ' + ' , '.join([f'{i} = {o}' for i, o in sample['results']])
                    elif tp == 'rir':
                        seq += 'intermediate: ' + sample['rir']
                    elif tp == 'output':
                        seq += 'output: ' + (sample['output'] if isinstance(sample['output'], str) else ' '.join(sample['output']))
                    else:
                        assert False, f'Unknown type: {tp}'
                    seq += delimiter
                sequences.append(seq)
            encoding = self.tokenizer(
                sequences,
                padding="longest",
                truncation=False,
                max_length=1024,
                return_tensors="pt",
            )
            assert self.tokenizer.unk_token_id not in encoding.input_ids, 'Some tokens are not recognized by the tokenizer!'
            return encoding.input_ids, encoding.attention_mask

        input_ids, input_masks = tokenize_batch(self.input_types)
        output_ids, output_masks = tokenize_batch(self.output_types)
        output_ids[output_ids == self.tokenizer.pad_token_id] = -100

        new_batch = {}
        for key in batch[0].keys():
            new_batch[key] = [sample[key] for sample in batch]

        new_batch['input_ids'] = input_ids
        new_batch['input_masks'] = input_masks
        new_batch['output_ids'] = output_ids
        new_batch['output_masks'] = output_masks
        return new_batch


import os
import sys
import json
import pickle
import random
from PIL import Image
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt

import torch
from torch.utils.data import Dataset
import torch.nn.functional as F

def read_split_data(root: str, DatasetName: str, val_rate: float = 0.2, plot_image: bool = False):
    random.seed(0)  # 保证随机结果可复现
    assert os.path.exists(root), "dataset root: {} does not exist.".format(root)

    # 遍历文件夹，一个文件夹对应一个类别
    data_class = [cla for cla in os.listdir(root) if os.path.isdir(os.path.join(root, cla))]
    # 排序，保证顺序一致
    data_class.sort()
    # 生成类别名称以及对应的数字索引
    class_indices = dict((k, v) for v, k in enumerate(data_class))
    json_str = json.dumps(class_indices)
    with open('{}_class_indices.json'.format(DatasetName), 'w') as json_file:
        json_file.write(json_str)

    train_images_path = []  # 存储训练集的所有图片路径
    train_images_label = []  # 存储训练集图片对应索引信息
    val_images_path = []  # 存储验证集的所有图片路径
    val_images_label = []  # 存储验证集图片对应索引信息
    every_class_num = []  # 存储每个类别的样本总数
    supported = [".jpg", ".JPG", ".png", ".PNG",".bmp"]  # 支持的文件后缀类型
    # 遍历每个文件夹下的文件
    for cla in data_class:
        cla_path = os.path.join(root, cla)
        # 遍历获取supported支持的所有文件路径
        images = [os.path.join(root, cla, i) for i in os.listdir(cla_path)
                  if os.path.splitext(i)[-1] in supported]
        # 获取该类别对应的索引
        image_class = class_indices[cla]
        # 记录该类别的样本数量
        every_class_num.append(len(images))
        # 按比例随机采样验证样本
        val_path = random.sample(images, k=int(len(images) * val_rate))

        for img_path in images:
            if img_path in val_path:  # 如果该路径在采样的验证集样本中则存入验证集
                val_images_path.append(img_path)
                val_images_label.append(image_class)
            else:  # 否则存入训练集
                train_images_path.append(img_path)
                train_images_label.append(image_class)

    print("{} images were found in the dataset.".format(sum(every_class_num)))
    print("{} images for training.".format(len(train_images_path)))
    print("{} images for validation.".format(len(val_images_path)))

    if plot_image:
        # 绘制每种类别个数柱状图
        plt.bar(range(len(flower_class)), every_class_num, align='center')
        # 将横坐标0,1,2,3,4替换为相应的类别名称
        plt.xticks(range(len(flower_class)), flower_class)
        # 在柱状图上添加数值标签
        for i, v in enumerate(every_class_num):
            plt.text(x=i, y=v + 5, s=str(v), ha='center')
        # 设置x坐标
        plt.xlabel('image class')
        # 设置y坐标
        plt.ylabel('number of images')
        # 设置柱状图的标题
        plt.title('data class distribution')
        plt.show()

    return train_images_path, train_images_label, val_images_path, val_images_label

def load_all_imgs(root: str):
    assert os.path.exists(root), "dataset root: {} does not exist.".format(root)
    images_path = {}
    supported = [".jpg", ".JPG", ".png", ".PNG", ".bmp"]
    data_class = [cla for cla in os.listdir(root) if os.path.isdir(os.path.join(root, cla))]
    data_class.sort()
    for cla in data_class:
        cla_path = os.path.join(root,cla)
        images = [os.path.join(root, cla, i) for i in os.listdir(cla_path)
                  if os.path.splitext(i)[-1] in supported]
        assert os.path.exists(cla_path), "file: '{}' dose not exist.".format(cla_path)
        images_path[cla] = images
        print("total {} images finding for predict class {}.".format(len(images_path[cla]),cla))
    return images_path

def plot_data_loader_image(data_loader):
    batch_size = data_loader.batch_size
    plot_num = min(batch_size, 4)

    json_path = './class_indices.json'
    assert os.path.exists(json_path), json_path + " does not exist."
    json_file = open(json_path, 'r')
    class_indices = json.load(json_file)

    for data in data_loader:
        images, labels = data
        for i in range(plot_num):
            # [C, H, W] -> [H, W, C]
            img = images[i].numpy().transpose(1, 2, 0)
            # 反Normalize操作
            img = (img * [0.229, 0.224, 0.225] + [0.485, 0.456, 0.406]) * 255
            label = labels[i].item()
            plt.subplot(1, plot_num, i+1)
            plt.xlabel(class_indices[str(label)])
            plt.xticks([])  # 去掉x轴的刻度
            plt.yticks([])  # 去掉y轴的刻度
            plt.imshow(img.astype('uint8'))
        plt.show()

def write_pickle(list_info: list, file_name: str):
    with open(file_name, 'wb') as f:
        pickle.dump(list_info, f)

def read_pickle(file_name: str) -> list:
    with open(file_name, 'rb') as f:
        info_list = pickle.load(f)
        return info_list

def train_one_epoch(model, optimizer, data_loader, device, epoch):
    model.train()
    loss_function = torch.nn.CrossEntropyLoss()
    accu_loss = torch.zeros(1).to(device)  # 累计损失
    accu_num = torch.zeros(1).to(device)   # 累计预测正确的样本数
    optimizer.zero_grad()

    sample_num = 0
    data_loader = tqdm(data_loader, file=sys.stdout, colour="green")
    for step, data in enumerate(data_loader):
        images, labels = data
        sample_num += images.shape[0]

        pred = model(images.to(device))
        pred_classes = torch.max(pred, dim=1)[1]
        accu_num += torch.eq(pred_classes, labels.to(device)).sum()

        loss = loss_function(pred, labels.to(device))

        loss.backward()
        accu_loss += loss.detach()

        data_loader.desc = "[train epoch {}] loss: {:.3f}, acc: {:.3f}".format(epoch,
                                                                               accu_loss.item() / (step + 1),
                                                                               accu_num.item() / sample_num)

        if not torch.isfinite(loss):
            print('WARNING: non-finite loss, ending training ', loss)
            sys.exit(1)

        optimizer.step()
        optimizer.zero_grad()

    return accu_loss.item() / (step + 1), accu_num.item() / sample_num

@torch.no_grad()
def evaluate(model, data_loader, device, epoch):
    loss_function = torch.nn.CrossEntropyLoss()

    model.eval()

    accu_loss = torch.zeros(1).to(device)  # 累计损失
    accu_num = torch.zeros(1).to(device)  # 累计预测正确的样本数
    sample_num = 0

    data_loader = tqdm(data_loader, file=sys.stdout)
    for step, data in enumerate(data_loader):
        images, labels = data
        sample_num += images.shape[0]

        pred = model(images.to(device))
        pred_classes = torch.max(pred, dim=1)[1]
        accu_num += torch.eq(pred_classes, labels.to(device)).sum()

        loss = loss_function(pred, labels.to(device))
        accu_loss += loss

        data_loader.desc = "[valid epoch {}] loss: {:.3f}, acc: {:.3f}".format(epoch,
                                                                               accu_loss.item() / (step + 1),
                                                                               accu_num.item() / sample_num)

    return accu_loss.item() / (step + 1), accu_num.item() / sample_num

@torch.no_grad()
def Test_A_Dataset(model, data_loader, classes_name, args):
    device = torch.device(args.device if torch.cuda.is_available() else "cpu")
    model.eval()
    cls_num = len(classes_name)
    conf_mat = np.zeros([cls_num, cls_num])  #混淆矩阵

    data_loader = tqdm(data_loader, file = sys.stdout, colour = 'red')
    for step, data in enumerate(data_loader):
        images, labels = data

        pred = model(images.to(device))
        pred_classes = torch.max(pred, dim = 1)[1]

        for i in range(len(labels)):
            cate_i = labels[i]
            pre_i = pred_classes[i]
            conf_mat[cate_i, pre_i] += 1.0

    for i in range(cls_num):
        print('class:{:<10}, total num:{:<6}, correct num:{:<5}  Recall: {:.2%} Precision: {:.2%}'.format(
            classes_name[i], np.sum(conf_mat[i, :]), conf_mat[i, i], conf_mat[i, i] / (1 + np.sum(conf_mat[i, :])),
                                                                     conf_mat[i, i] / (1 + np.sum(conf_mat[:, i]))))

    print('dataset Accuracy:{:.2%}'.format( np.trace(conf_mat) / np.sum(conf_mat)))
    return conf_mat, '{:.2}'.format(np.trace(conf_mat) / np.sum(conf_mat))

def show_confMat(confusion_mat, classes, args):

    # 归一化
    confusion_mat_N = confusion_mat.copy()
    for i in range(len(classes)):
        confusion_mat_N[i, :] = confusion_mat[i, :] / confusion_mat[i, :].sum()

    # 获取颜色
    cmap = plt.cm.get_cmap('Blues')  # 更多颜色: http://matplotlib.org/examples/color/colormaps_reference.html
    plt.imshow(confusion_mat_N, cmap=cmap)
    plt.colorbar()

    # 设置文字
    xlocations = np.array(range(len(classes)))
    plt.xticks(xlocations, list(classes), rotation=-45)
    plt.yticks(xlocations, list(classes))
    plt.xlabel('Predict label')
    plt.ylabel('True label')
    plt.title('Confusion_Matrix_last')

    # 打印数字
    for i in range(confusion_mat_N.shape[0]):
        for j in range(confusion_mat_N.shape[1]):
            plt.text(x=j, y=i, s=int(confusion_mat[i, j]), va='center', ha='center', color='red', fontsize=10)
    # 保存
    if os.path.exists(args.conf_Mat_dir) is False:  os.makedirs(args.conf_Mat_dir)
    plt.tight_layout()
    plt.savefig(os.path.join(args.conf_Mat_dir, 'Confusion_Matrix_last' + args.model_type + '.png'))
    plt.close()

class MyDataSet(Dataset):
    """自定义数据集"""

    def __init__(self, images_path: list, images_class: list, transform=None):
        self.images_path = images_path
        self.images_class = images_class
        self.transform = transform

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

    def __getitem__(self, item):
        img = Image.open(self.images_path[item])
        if img.mode != "RGB":
            img=img.convert("RGB")
        # RGB为彩色图片，L为灰度图片
        # if img.mode != 'RGB':
        #     raise ValueError("image: {} isn't RGB mode.".format(self.images_path[item]))
        label = self.images_class[item]

        if self.transform is not None:
            img = self.transform(img)
        return img, label


#!/usr/bin/env python3
"""
This workflow pulls data from the sampledata/acl_data.yaml file.
This workflow performs the following steps:
1. Detach ACL from VLAN
   Ex:
    vlan 200
        no apply access-list ip acl_ipv4 in

2. Delete IPv4 ACL
    a. Delete entries in ACL
    b. Delete the ACL
   Ex:
    no access-list ip acl_ipv4

3. Delete IPv6 ACL
    a. Delete entries in ACL
    b. Delete the ACL
   Ex:
    no access-list ipv6 acl_ipv6

4. Delete MAC ACL
    a. Delete entries in ACL
    b. Delete the ACL
   Ex:
    no access-list mac acl_mac

5. Reset interfaces
   Ex:
    interface 1/1/10
        shutdown
        routing
    interface 1/1/11
        shutdown
        routing
        no apply access-list ip acl_ipv4 in
    interface 1/1/12
        shutdown
        routing
        no apply access-list ipv6 acl_ipv6 in
    interface 1/1/21
        shutdown
        no lag 13
    interface 1/1/22
        shutdown
        no lag 13
    no interface lag 13

Preconditions:
Must have run configure_acl workflow or have an equivalent configuration on the device
"""

from requests.packages.urllib3.exceptions import InsecureRequestWarning
import requests
import os
import sys

dirpath = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
sys.path.append(dirpath)
sys.path.append(os.path.join(dirpath, "src"))
sys.path.append(os.path.join(dirpath, "cx_utils"))

from cx_utils import yaml_ops
from src import session, acl, vlan, interface, lag, system

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

def main():
    data = yaml_ops.read_yaml("acl_data.yaml")

    if not data['switchip']:
        data['switchip'] = input("Switch IP Address: ")

    if data['bypassproxy']:
        os.environ['no_proxy'] = data['switchip']
        os.environ['NO_PROXY'] = data['switchip']

    if not data['version']:
        data['version'] = "v10.04"

    base_url = "https://{0}/rest/{1}/".format(data['switchip'], data['version'])
    try:
        session_dict = dict(s=session.login(base_url, data['username'], data['password']), url=base_url)
        session_dict['platform_name'] = system.get_system_info(**session_dict).get('platform_name')

        # Clear Egress ACLs from L3 interface
        acl.clear_interface_acl(data['L3egressinterface'], acl_type="aclv4_out", **session_dict)

        # Clear Ingress ACLs from L2 interface
        acl.clear_interface_acl(data['ipv4L2ingressinterface'], acl_type="aclv4_in", **session_dict)
        acl.clear_interface_acl(data['ipv6L2ingressinterface'], acl_type="aclv6_in", **session_dict)

        # Clear Ingress ACLs from LAG interface
        acl.clear_interface_acl(data['LAGname'], acl_type="aclv4_in", **session_dict)

        # Detach ACL from VLAN
        vlan.detach_vlan_acl(data['aclVLANid'], "ipv4", **session_dict)

        # Remove and initialize L2 and L3 interfaces
        interface.initialize_interface(data['L3egressinterface'], **session_dict)
        interface.initialize_interface(data['ipv4L2ingressinterface'], **session_dict)
        interface.initialize_interface(data['ipv6L2ingressinterface'], **session_dict)
        interface.initialize_interface(data['interfaceVLAN'], **session_dict)

        # Remove LAG and initialize associated L2 interfaces
        lag.delete_lag_interface(data['LAGname'], data['LAGinterfaces'], **session_dict)
        for LAGinterface in data['LAGinterfaces']:
            interface.initialize_interface(LAGinterface, **session_dict)

        # Delete VLAN
        vlan.delete_vlan(data['aclVLANid'], **session_dict)

        # For each ACL that was configured
        for pair_dict in [{"name": data['ipv4aclname'], "type": "ipv4"},
                          {"name": data['ipv6aclname'], "type": "ipv6"},
                          {"name": data['macaclname'], "type": "mac"}]:

            # Delete ACL entries
            for i in range(10, 60, 10):
                acl.delete_acl_entry(pair_dict["name"], pair_dict["type"], i, **session_dict)

            # Delete the ACL
            acl.delete_acl(pair_dict["name"], pair_dict["type"], **session_dict)

    except Exception as error:
        print('Ran into exception: {}. Logging out..'.format(error))

    session.logout(**session_dict)

if __name__ == '__main__':
    main()


from django.urls import path, include
from wiki import views
from rest_framework.routers import SimpleRouter, DefaultRouter

app_name = 'wiki'

router = SimpleRouter()
# router = DefaultRouter()
router.register(r'posts', views.PostViewSet, basename='posts')
router.register(r'tags', views.TagViewSet, basename='tags')
router.register(r'categories', views.CategoryViewSet, basename='categories')
router.register(r'comments', views.CommentViewSet, basename='comments')

urlpatterns = [
    path('', include(router.urls)),
]


import sqlite3

base_url = "/home/shiyanlou/Desktop/shiyanlou_spider.sqlite3.db"
table_name = "course_info"

def create_table():
    sql3_db = sqlite3.connect(base_url)
    create_sql = "create table {} (url varchar(1024), title varchar(256), teacher varchar(128), study_num int, tag varchar(256), types varchar(256), info varchar(1024), tests_name varchar(1024))".format(table_name)
    try:
        sql3_db.execute(create_sql)
    except:
        return False
    sql3_db.close()
    return True

def insert_or_update_data(url, title, teacher, study_num, tag, types, info, tests_name):
    create_table() # 创建一次数据库，没有表格时创建，表格存在时不作操作
    if query(title): # 如果查询到title存在，则是更新
        sql3_db = sqlite3.connect(base_url)
        update_sql = "update {} set study_num={}, info='{}', tests_name='{}', url='{}' where title='{}'".format(table_name, study_num, info, tests_name, url, title)
        try:
            sql3_db.execute(update_sql)
            sql3_db.commit()
        except:
            return False
        sql3_db.close()
        return True
    else: # 查询不到title，则插入
        sql3_db = sqlite3.connect(base_url)
        insert_sql = "insert into {} (url, title, teacher, study_num, tag, types, info, tests_name) values('{}', '{}', '{}', {}, '{}', '{}', '{}', '{}')".format(table_name,url, title, teacher, study_num, tag, types, info, tests_name)
        try:
            sql3_db.execute(insert_sql)
            sql3_db.commit()
        except:
            return False
        sql3_db.close()
        return True
    return False

def query(title):
    sql3_db = sqlite3.connect(base_url)
    query_sql = "select * from {} where title = '{}'".format(table_name, title)
    cu = sql3_db.cursor()
    cu.execute(query_sql)
    record_list = cu.fetchall()
    if len(record_list)>0:
        return True
    else:
        return False
    return False

if __name__=="__main__":
    insert_or_update_data('url','title','teacher',12138,'vip','web','infos_infos','课程一 课程二')


# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
	# @param root, a tree node
	# @return a list of integers
	def preorderTraversal(self, root):

		results = []
		if not root:
			return results

		queue = []
		queue.append(root)

		while queue:
			root = queue.pop()
			results.append(root.val)
			if root.right:
				queue.append(root.right)
			if root.left:
				queue.append(root.left)

		return results


TAGGER = (-2, -2)
BLANK = (-1, -1)

n = 4

board = [
    [(0, 0) for _ in range(n)]
    for _ in range(n)
]

dxs = [-1, -1,  0,  1, 1, 1, 0, -1]
dys = [ 0, -1, -1, -1, 0, 1, 1,  1]

max_score = 0

def in_range(x, y):
    return 0 <= x < n and 0 <= y < n

def thief_can_go(x, y):
    return in_range(x, y) and board[x][y] != TAGGER

def tagger_can_go(x, y):
    return in_range(x, y) and board[x][y] != BLANK

def done(x, y, d):
    for dist in range(1, n + 1):
        nx, ny = x + dxs[d] * dist, y + dys[d] * dist
        if tagger_can_go(nx, ny):
            return False
    return True

def get_next(x, y, move_dir):
    for rotate_num in range(8):
        adjusted_dir = (move_dir + rotate_num) % 8
        nx, ny = x + dxs[adjusted_dir], y + dys[adjusted_dir]
        if thief_can_go(nx, ny):
            return (nx, ny, adjusted_dir)

    return (x, y, move_dir)

def swap(x, y, nx, ny):
    board[x][y], board[nx][ny] = board[nx][ny], board[x][y]

def move(target_num):
    for x in range(n):
        for y in range(n):
            piece_num, move_dir = board[x][y]
            if piece_num == target_num:
                next_x, next_y, next_dir = get_next(x, y, move_dir)
                board[x][y] = (piece_num, next_dir)
                swap(x, y, next_x, next_y)
                return

def move_all():
    for i in range(1, n * n + 1):
        move(i)

def search_max_score(x, y, d, score):
    global max_score

    if done(x, y, d):
        max_score = max(max_score, score)
        return

    for dist in range(1, n + 1):
        nx, ny = x + dxs[d] * dist, y + dys[d] * dist
        if not tagger_can_go(nx, ny):
            continue

        temp = [
            [board[i][j] for j in range(n)]
            for i in range(n)
        ]

        extra_score, next_dir = board[nx][ny]
        board[x][y] = BLANK
        board[nx][ny] = TAGGER
        move_all()

        search_max_score(nx, ny, next_dir, score + extra_score)

        for i in range(n):
            for j in range(n):
                board[i][j] = temp[i][j]

for i in range(n):
    given_row = list(map(int, input().split()))
    for j in range(n):
        p, d = given_row[j * 2], given_row[j * 2 + 1]
        board[i][j] = (p, d - 1)

# 처음 (0, 0) 도둑말을 잡고, 모든 도둑말이 이동한 다음에 시작합니다.
init_score, init_dir = board[0][0]
board[0][0] = TAGGER

move_all()

search_max_score(0, 0, init_dir, init_score)
print(max_score)


from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from nltk.tokenize import RegexpTokenizer
from sklearn import preprocessing
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras import utils
import pandas as pd
import numpy as np
import joblib

# tokenizer to remove unwanted elements from out data like symbols and numbers
from sklearn.utils import compute_class_weight

df = pd.read_csv('Movie_Metadata_Sentiments.csv')
# Subset only emotions required to get overall emotion detected from the text content
sub_df = df[['anger', 'joy', 'fear', 'sadness']]
# Label the movie with the highest count of emotions
df['Max'] = sub_df.idxmax(axis=1)
token = RegexpTokenizer(r'[a-zA-Z0-9]+')
cv = CountVectorizer(lowercase=True, stop_words='english', ngram_range=(1, 1), tokenizer=token.tokenize)
cv = cv.fit(df['Text_Content'])
text_counts = cv.transform(df['Text_Content'])
# Save the vectorizer
joblib.dump(cv, "vectorizer.pkl")

X_train, X_test, y_train, y_test = train_test_split(
    text_counts, df['Max'], test_size=0.2, random_state=1)

# Neural Network
encoder = preprocessing.LabelEncoder()
encoder.fit(y_train)
print(encoder.classes_)
y_train = encoder.transform(y_train)
y_test = encoder.transform(y_test)

# Resolves the imbalance in dataset
class_weights = compute_class_weight('balanced', np.unique(y_train), y_train)

class_weights_dict = dict(zip(encoder.transform(list(encoder.classes_)), class_weights))
print(class_weights_dict)

num_classes = np.max(y_train) + 1
y_train = utils.to_categorical(y_train, num_classes)
y_test = utils.to_categorical(y_test, num_classes)
print(y_train)
print(y_train.shape)

batch_size = 64
epochs = 3

# Build the model
model = Sequential()
model.add(Dense(256, input_shape=(X_train.shape[1],), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4, activation='softmax'))

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

history = model.fit(X_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_split=0.1,
                    class_weight=class_weights_dict)

score = model.evaluate(X_test, y_test,
                       batch_size=batch_size, verbose=1)
print('Test accuracy:', score[1])

# Prints the Classification Report
y_pred = model.predict(X_test)
print(classification_report(y_test.argmax(axis=1), y_pred.argmax(axis=1), target_names=encoder.classes_))

# Export the Model
model.save('Movie_Metadata_Sentiments_Weighted_Keras.h5', history)


import pybobyqa
import numpy as np
import scipy.interpolate as spi
import scipy.optimize as spo

import optim_spsa
import optim_sa

def interp(points):
    xs, ys = tuple(zip(*tuple(points)))
    sfunc = spi.PchipInterpolator(xs, ys)
    return sfunc
def schedule(points, layers):
    sfunc = interp(points)
    params = sfunc(np.arange(1, layers+1)/(layers+1))
    return params

def move_points(old_points, new_xs):
    old_sfunc = interp(old_points)
    new_points = [(new_x, old_sfunc(new_x)) for new_x in new_xs]
    return new_points

def optimizer_minimize_all(param_type, optimizer, func, layers, mixer_init, problem_init, optimizer_params):
    assert param_type in ('standard', 'interp', 'interp2', 'fourier')
    assert optimizer in ('spsa', 'bobyqa', 'sa', 'bfgs')

    if param_type == 'standard':
        mixer_param_init, problem_param_init = mixer_init, problem_init
        assert (len(mixer_param_init), len(problem_param_init)) == (layers, layers)
        initial_position = np.array(list(mixer_param_init) + list(problem_param_init))

        def cost_function(params):
            mixer_params = params[:layers]
            problem_params = params[layers:]
            return func(mixer_params, problem_params)

    if param_type == 'interp':
        mixer_param_points_init, problem_param_points_init = mixer_init, problem_init

        n_mixer_points = len(mixer_param_points_init)
        n_problem_points = len(mixer_param_points_init)

        def cost_function(params):
            mixer_points = ()
            for j in range(n_mixer_points):
                mixer_points += ((params[2*j], params[(2*j)+1]),)
            problem_points = ()
            for j in range(n_mixer_points, n_mixer_points+n_problem_points):
                problem_points += ((params[2*j], params[(2*j)+1]),)
            mixer_points = tuple(sorted(mixer_points, key=lambda x: x[0]))
            problem_points = tuple(sorted(problem_points, key=lambda x: x[0]))

            mixer_params = schedule(mixer_points, layers)
            problem_params = schedule(problem_points, layers)
            return func(mixer_params, problem_params)

        initial_position = []
        for j in range(n_mixer_points):
            initial_position += list(mixer_param_points_init[j])
        for j in range(n_problem_points):
            initial_position += list(problem_param_points_init[j])
        initial_position = np.array(initial_position)

    if param_type == 'interp2':
        mixer_param_vals_init, problem_param_vals_init = mixer_init, problem_init

        n_mixer_vals = len(mixer_param_vals_init)
        n_problem_vals = len(mixer_param_vals_init)

        def cost_function(params):
            mixer_points = ()
            for j in range(n_mixer_vals):
                mixer_points += ((j/(n_mixer_vals-1), params[j]),)
            problem_points = ()
            for j in range(n_mixer_vals, n_mixer_vals+n_problem_vals):
                problem_points += (((j-n_mixer_vals)/(n_problem_vals-1), params[j]),)
            mixer_points = tuple(sorted(mixer_points, key=lambda x: x[0]))
            problem_points = tuple(sorted(problem_points, key=lambda x: x[0]))

            mixer_params = schedule(mixer_points, layers)
            problem_params = schedule(problem_points, layers)
            return func(mixer_params, problem_params)

        initial_position = []
        for j in range(n_mixer_vals):
            initial_position += [mixer_param_vals_init[j]]
        for j in range(n_problem_vals):
            initial_position += [problem_param_vals_init[j]]
        initial_position = np.array(initial_position)

    if param_type == 'fourier':
        mixer_modes_init, problem_modes_init = mixer_init, problem_init

        n_mixer_modes = len(mixer_modes_init)
        n_problem_modes = len(problem_modes_init)

        def cost_function(params):
            mixer_modes = params[:n_mixer_modes]
            problem_modes = params[n_mixer_modes:n_mixer_modes+n_problem_modes]
            mixer_params, problem_params = [], []
            for j in range(1, layers+1):
                mixer_param = np.sum([mixer_modes[k-1]*np.cos( (k-0.5)*(j-0.5)*(np.pi/layers) ) for k in range(1, n_mixer_modes+1)])
                problem_param = np.sum([problem_modes[k-1]*np.sin( (k-0.5)*(j-0.5)*(np.pi/layers) ) for k in range(1, n_problem_modes+1)])
                mixer_params.append(mixer_param)
                problem_params.append(problem_param)
            mixer_params, problem_params = np.array(mixer_params), np.array(problem_params)
            #print(list(mixer_params))
            #print(list(problem_params))
            tmp = func(mixer_params, problem_params)
            #print(tmp)
            return tmp

        initial_position = np.array(list(mixer_modes_init) + list(problem_modes_init))

    if optimizer == 'bobyqa':
        noisy = optimizer_params.get('noisy', None)
        maxfun = optimizer_params.get('maxfun', None)
        max_for_global = optimizer_params.get('max_for_global', None)
        restart = optimizer_params.get('restart', False)

        if restart and (maxfun is None):
            raise ValueError

        if max_for_global is None:
            seek_global = False
        else:
            seek_global = True
            min_for_global = -max_for_global if param_type == 'fourier' else 0.0
            bounds = ( np.array([min_for_global]*len(initial_position)), np.array([max_for_global]*len(initial_position)) )

        best_opt_objective, curr_nf = np.inf, 0
        while True:
            maxfun_use = None if maxfun is None else (maxfun - curr_nf)
            if seek_global:
                soln = pybobyqa.solve(cost_function, initial_position, bounds=bounds, objfun_has_noise=noisy, seek_global_minimum=seek_global, maxfun=maxfun_use)
            else:
                soln = pybobyqa.solve(cost_function, initial_position, objfun_has_noise=noisy, seek_global_minimum=seek_global, maxfun=maxfun_use)
            opt_params = np.array(soln.x)
            opt_objective = soln.f
            curr_nf += soln.nf
            if opt_objective < best_opt_objective:
                best_opt_params, best_opt_objective = opt_params, opt_objective
            if (not restart) or (curr_nf >= maxfun):
                break
            else:
                print('')
                print("Restarting!!!    ")
        opt_params, opt_objective = best_opt_params, best_opt_objective

    if optimizer == 'spsa':
        runs = optimizer_params.get('runs', 1)

        perturb = 0.01
        lr = 0.01

        max_iterations = 2000000

        final_state = optim_spsa.minimize(cost_function, initial_position, \
            runs=runs, tolerance=1e-8, max_iterations=max_iterations, alpha=0.602, \
            lr=lr, perturb=perturb, gamma=0.101, blocking=False, \
            allowed_increase=0.5)

        opt_params = np.array(final_state['best_position'])
        opt_objective = final_state['best_objective_value']

    if optimizer == 'sa':
        param_max = optimizer_params.get('param_max', np.pi)
        param_min = -param_max if param_type == 'fourier' else 0.0
        bounds = [(param_min, param_max) for j in range(len(initial_position))]
        stepsize = optimizer_params.get('stepsize', 0.05)
        iterations = optimizer_params.get('iterations', 1000)
        runs = optimizer_params.get('runs', 1)
        max_temperature = optimizer_params.get('max_temperature', 10)

        opt_objective, opt_params = optim_sa.run(cost_function, initial_position, bounds, stepsize, iterations, runs, max_temperature)

    if optimizer == 'bfgs':
        res = spo.minimize(cost_function, initial_position, method='BFGS')
        opt_objective, opt_params = res.fun, res.x

    if param_type == 'standard':
        opt_mixer_params = opt_params[:layers]
        opt_problem_params = opt_params[layers:]

        extra_output = ()

    if param_type == 'interp':
        mixer_points = ()
        for j in range(n_mixer_points):
            mixer_points += ((opt_params[2*j], opt_params[(2*j)+1]),)
        problem_points = ()
        for j in range(n_mixer_points, n_mixer_points+n_problem_points):
            problem_points += ((opt_params[2*j], opt_params[(2*j)+1]),)
        mixer_points = tuple(sorted(mixer_points, key=lambda x: x[0]))
        problem_points = tuple(sorted(problem_points, key=lambda x: x[0]))
        opt_mixer_params = schedule(mixer_points, layers)
        opt_problem_params = schedule(problem_points, layers)

        opt_mixer_points = [(opt_params[2*j], opt_params[(2*j)+1]) for j in range(n_mixer_points)]
        opt_problem_points = [(opt_params[(2*n_mixer_points)+(2*j)], opt_params[(2*n_mixer_points)+((2*j)+1)]) for j in range(n_problem_points)]
        extra_output = (opt_mixer_points, opt_problem_points)

    if param_type == 'interp2':
        mixer_points = ()
        for j in range(n_mixer_vals):
            mixer_points += ((j/(n_mixer_vals-1), opt_params[j]),)
        problem_points = ()
        for j in range(n_mixer_vals, n_mixer_vals+n_problem_vals):
            problem_points += (((j-n_mixer_vals)/(n_mixer_vals-1), opt_params[j]),)
        mixer_points = tuple(sorted(mixer_points, key=lambda x: x[0]))
        problem_points = tuple(sorted(problem_points, key=lambda x: x[0]))
        opt_mixer_params = schedule(mixer_points, layers)
        opt_problem_params = schedule(problem_points, layers)

        opt_mixer_vals, opt_problem_vals = opt_params[:n_mixer_vals], opt_params[n_mixer_vals:n_mixer_vals+n_problem_vals]
        extra_output = (opt_mixer_vals, opt_problem_vals)

    if param_type == 'fourier':
        opt_mixer_modes = opt_params[:n_mixer_modes]
        opt_problem_modes = opt_params[n_mixer_modes:n_mixer_modes+n_problem_modes]
        opt_mixer_params, opt_problem_params = [], []
        for j in range(1, layers+1):
            mixer_param = np.sum([opt_mixer_modes[k-1]*np.cos( (k-0.5)*(j-0.5)*(np.pi/layers) ) for k in range(1, n_mixer_modes+1)])
            problem_param = np.sum([opt_problem_modes[k-1]*np.sin( (k-0.5)*(j-0.5)*(np.pi/layers) ) for k in range(1, n_problem_modes+1)])
            opt_mixer_params.append(mixer_param)
            opt_problem_params.append(problem_param)

        opt_mixer_params, opt_problem_params = np.array(opt_mixer_params), np.array(opt_problem_params)
        extra_output = (opt_mixer_modes, opt_problem_modes)

    return opt_mixer_params, opt_problem_params, opt_objective, extra_output

def bobyqa_minimize_all(param_type, func, layers, mixer_init, problem_init, noisy=None, maxfun=None, max_for_global=None, restart=False):
    optimizer = 'bobyqa'
    optimizer_params = {'noisy' : noisy, 'maxfun' : maxfun, 'max_for_global' : max_for_global, 'restart' : restart}
    return optimizer_minimize_all(param_type, optimizer, func, layers, mixer_init, problem_init, optimizer_params)

def spsa_minimize_all(param_type, func, layers, mixer_init, problem_init, runs=1):
    optimizer = 'spsa'
    optimizer_params = {'runs' : runs}
    return optimizer_minimize_all(param_type, optimizer, func, layers, mixer_init, problem_init, optimizer_params)

def sa_minimize_all(param_type, func, layers, mixer_init, problem_init, param_max, stepsize, iterations, runs, max_temperature):
    optimizer = 'sa'
    optimizer_params = {'param_max' : param_max, 'stepsize' : stepsize, 'iterations' : iterations, 'runs' : runs, 'max_temperature' : max_temperature}
    return optimizer_minimize_all(param_type, optimizer, func, layers, mixer_init, problem_init, optimizer_params)

def bfgs_minimize_all(param_type, func, layers, mixer_init, problem_init):
    optimizer = 'bfgs'
    optimizer_params = {}
    return optimizer_minimize_all(param_type, optimizer, func, layers, mixer_init, problem_init, optimizer_params)

def bobyqa_minimize(func, layers, mixer_param_init, problem_param_init, noisy=None, maxfun=None, max_for_global=None, restart=False):
    return bobyqa_minimize_all('standard', func, layers, mixer_param_init, problem_param_init, noisy, maxfun=maxfun, max_for_global=max_for_global, restart=restart)

def bobyqa_minimize_interp(func, layers, mixer_param_points_init, problem_param_points_init, noisy=None, maxfun=None, max_for_global=None, restart=False):
    return bobyqa_minimize_all('interp', func, layers, mixer_param_points_init, problem_param_points_init, noisy, maxfun=maxfun, max_for_global=max_for_global, \
        restart=restart)

def bobyqa_minimize_interp2(func, layers, mixer_param_vals_init, problem_param_vals_init, noisy=None, maxfun=None, max_for_global=None, restart=False):
    return bobyqa_minimize_all('interp2', func, layers, mixer_param_vals_init, problem_param_vals_init, noisy, maxfun=maxfun, max_for_global=max_for_global, \
        restart=restart)

def bobyqa_minimize_fourier(func, layers, mixer_modes_init, problem_modes_init, noisy=None, maxfun=None, max_for_global=None, restart=False):
    return bobyqa_minimize_all('fourier', func, layers, mixer_modes_init, problem_modes_init, noisy, maxfun=maxfun, max_for_global=max_for_global, \
        restart=restart)

def spsa_minimize(func, layers, mixer_param_init, problem_param_init, runs=1):
    return spsa_minimize_all('standard', func, layers, mixer_param_init, problem_param_init, runs=runs)

def spsa_minimize_interp(func, layers, mixer_param_points_init, problem_param_points_init, runs=1):
    return spsa_minimize_all('interp', func, layers, mixer_param_points_init, problem_param_points_init, runs=runs)

def spsa_minimize_interp2(func, layers, mixer_param_vals_init, problem_param_vals_init, runs=1):
    return spsa_minimize_all('interp2', func, layers, mixer_param_vals_init, problem_param_vals_init, runs=runs)

def spsa_minimize_fourier(func, layers, mixer_modes_init, problem_modes_init, runs=1):
    return spsa_minimize_all('fourier', func, layers, mixer_modes_init, problem_modes_init, runs=runs)

def sa_minimize(func, layers, mixer_init, problem_init, param_max, stepsize, iterations, runs, max_temperature):
    return sa_minimize_all('standard', func, layers, mixer_init, problem_init, param_max, stepsize, iterations, runs, max_temperature)

def sa_minimize_interp(func, layers, mixer_init, problem_init, param_max, stepsize, iterations, runs, max_temperature):
    return sa_minimize_all('interp', func, layers, mixer_init, problem_init, param_max, stepsize, iterations, runs, max_temperature)

def sa_minimize_interp2(func, layers, mixer_init, problem_init, param_max, stepsize, iterations, runs, max_temperature):
    return sa_minimize_all('interp2', func, layers, mixer_init, problem_init, param_max, stepsize, iterations, runs, max_temperature)

def sa_minimize_fourier(func, layers, mixer_init, problem_init, param_max, stepsize, iterations, runs, max_temperature):
    return sa_minimize_all('fourier', func, layers, mixer_init, problem_init, param_max, stepsize, iterations, runs, max_temperature)

def bfgs_minimize(func, layers, mixer_init, problem_init):
    return bfgs_minimize_all('standard', func, layers, mixer_init, problem_init)

def bfgs_minimize_interp(func, layers, mixer_init, problem_init):
    return bfgs_minimize_all('interp', func, layers, mixer_init, problem_init)

def bfgs_minimize_interp2(func, layers, mixer_init, problem_init):
    return bfgs_minimize_all('interp2', func, layers, mixer_init, problem_init)

def bfgs_minimize_fourier(func, layers, mixer_init, problem_init):
    return bfgs_minimize_all('fourier', func, layers, mixer_init, problem_init)


#!/usr/bin/env python3

from ase.cluster.icosahedron import Icosahedron
from ase.calculators.lj import LennardJones as ase_LJ
import numpy as np
import pytest

from pysisyphus.calculators.LennardJones import LennardJones
from pysisyphus.constants import BOHR2ANG
from pysisyphus.Geometry import Geometry
from pysisyphus.helpers import geom_loader
from pysisyphus.optimizers.RFOptimizer import RFOptimizer

def test_lennard_jones():
    atoms = Icosahedron("Ar", noshells=2, latticeconstant=3)
    atoms.calc = ase_LJ()
    ase_forces = atoms.get_forces()
    ase_energy = atoms.get_potential_energy()

    coords = atoms.positions.flatten()
    geom = Geometry(atoms.get_chemical_symbols(), coords / BOHR2ANG)
    geom.set_calculator(LennardJones())

    pysis_energy = geom.energy
    assert pysis_energy == pytest.approx(ase_energy)

    pysis_forces = geom.forces / BOHR2ANG
    np.testing.assert_allclose(pysis_forces, ase_forces.flatten(), atol=1e-15)

@pytest.mark.parametrize("max_micro_cycles, cur_cycle", ((0, 110), (25, 108)))
def test_ar_cluster(max_micro_cycles, cur_cycle):
    geom = geom_loader("lib:ar14cluster.xyz")
    geom.set_calculator(LennardJones())

    opt_kwargs = {
        "max_cycles": 150,
        "gediis": True,
        "thresh": "gau_vtight",
        "max_micro_cycles": max_micro_cycles,
    }
    opt = RFOptimizer(geom, **opt_kwargs)
    opt.run()

    assert geom.energy == pytest.approx(-43.63972413)
    assert opt.is_converged
    assert opt.cur_cycle == cur_cycle


# Exercise 10

import random
random_number = random.randint(1,101)
counter = 1
while counter <=5:
    print("Type in the ", counter, "number")
    number = int(input())
    counter += 1
    if number == random_number:
        print('Good guess')
    elif (counter== 6):
        print("Game over!")
        break
    else:
        print('Try Again!')
        continue


#!/usr/bin/env python3

import subprocess
from dataclasses import dataclass
from pathlib import Path
from tempfile import NamedTemporaryFile
import argparse
import os
import uuid
import hashlib
import sys
from typing import Dict, List

from calibre.utils.logging import Log
from calibre.customize.conversion import OptionRecommendation
from calibre.ebooks.conversion.plumber import Plumber
from calibre.ebooks.conversion.plugins.mobi_output import MOBIOutput
from calibre.ebooks.conversion.plugins.epub_output import EPUBOutput

import frontmatter

ROOT = Path(__file__).parent.resolve()

@dataclass
class Options:
    source_files: List[Path]
    output_dir: Path
    cover_dir: Path

def parse_args() -> Options:
    parser = argparse.ArgumentParser()
    help = "File to import rather than default.nix. Examples, ./release.nix"
    parser.add_argument("source_files", nargs="+", default="./.")
    parser.add_argument("--covers", default="./.")
    parser.add_argument("-o", "--output", default="./.")
    args = parser.parse_args()
    return Options(
        source_files=[Path(s) for s in args.source_files],
        cover_dir=Path(args.covers),
        output_dir=Path(args.output)
    )

def convert_document(source: Path, target: Path, cover_dir: Path):
    post = frontmatter.load(source)
    log = Log()

    title = post.get("title", "Untitled")
    author = post.get("creator", "Shannan Lekwati")

    args = [
        ("authors", author),
        ("language", post.get("lang", "en")),
        ("title", title),
    ]

    date = post.get("date")
    if date:
        args += [
            ("pubdate", str(post["date"])),
            ("timestamp", str(post["date"]))
        ]

    summary = post.get("summary")
    if summary:
        args += [ ("comments", summary) ]

    cover_image = post.get("cover", {}).get("image")
    if cover_image:
        cover_path = cover_dir.joinpath(cover_image).absolute()
        if cover_path.exists():
            args += [ ("cover", str(cover_path)) ]
        else:
            print(f"WARNING: {cover_image} in {source} does not exists", file=sys.stderr)

    with NamedTemporaryFile(suffix=source.suffix, mode="w") as f:
        f.write(f"# {title} by **{author}** \n")
        if "dedication" in post:
            f.write(f"{dedication}\n")
        f.write(post.content)
        f.flush()

        plumber = Plumber(f.name, target, log)
        recommendations = [(k, v, OptionRecommendation.HIGH) for (k,v) in args]
        plumber.merge_ui_recommendations(recommendations)
        plumber.run()

def main() -> None:
    opts = parse_args()

    for source_file in opts.source_files:
        target = opts.output_dir.joinpath(source_file.stem + ".epub")
        convert_document(source_file, target, opts.cover_dir)
        target = opts.output_dir.joinpath(source_file.stem + ".mobi")
        convert_document(source_file, target, opts.cover_dir)

if __name__ == "__main__":
    main()


from models import Teacher, Student
from flask_restful import Resource, reqparse
from config import app, db, api

class Teachers(Resource):
    def post(self):
        data = request.get_json()
        if not data:
            return {"message": "No input data provided"}, 400
        try:
            new_teacher = Teacher(name=name, age=age)
        except ValueError as e:
            return {"message": f"Error creating teacher: {e}"}, 400

        db.session.add(new_teacher)
        db.session.commit()

        return new_teacher.to_dict(), 201

api.add_resource(Teachers, '/teacher')

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


from __future__ import print_function
import time
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import os.path
import PIL as Image
import PIL.ImageDraw
from PIL import Image, ImageOps
import numpy

def border_one(original_image, percent_of_side=.3):
    """ Rounds the corner of a PIL.Image
        original_image must be a PIL.Image
        Returns a new PIL.Image with rounded corners, where
        0 < percent_of_side < 1 is the corner radius as a portion of the shorter
        dimension of original_image
    """
    #set the radius of the rounded corners
    width, height = original_image.size

    ###
    #create a mask
    ###

    #start with transparent mask
    border_mask = PIL.Image.new('RGBA', (width, height), (127,0,127,0))
    drawing_layer = PIL.ImageDraw.Draw(border_mask)

    # Overwrite the RGBA values with A=255.
    # The 127 for RGB values was used merely for visualizing the mask

       # Draw two rectangles to fill interior with opaqueness
    if width >= 300 or height >= 300:
        drawing_layer.polygon([(0,0),(width, 0),
                                (width, height),(0,height)],
                                fill=(127,0,127,170))

        drawing_layer.polygon([(0,0),(30, 0),
                      (30, height),(0,height)],
                      fill=(0,0,255,0),outline=(0,255,0,1))
        drawing_layer.polygon([(width, 0),(width-30,0),
                      (width-30, height),(width,height)],
                      fill=(0,0,255,0),outline=(0,255,0,1))
        drawing_layer.polygon([(0, 0),(width,0),
                      (width,30),(0, 30)],
                      fill=(0,0,255,0),outline=(0,255,0,1))
        drawing_layer.polygon([(0, height),(width,height),
                      (width,height-30),(0, height-30)],
                      fill=(0,0,255,0),outline=(0,255,0,1))

        # Make the new image, starting with all transparent
        result = PIL.Image.new('RGBA', original_image.size, (136,39,16,255))
        result.paste(original_image, (0,0), mask=border_mask)

        img = Image.open('ourlogo_test.png')
        logo_small = img.resize((85,93))
        result.paste(logo_small, (width-130,40), mask = logo_small)

        return result

    elif width <= 300 and height <= 300:

        original_image.size = 500, 500

        drawing_layer.polygon([(0,0),(width, 0),
                                (width, height),(0,height)],
                                fill=(127,0,127,170))

        drawing_layer.polygon([(0,0),(20, 0),
                      (20, height),(0,height)],
                      fill=(0,0,255,0),outline=(0,255,0,1))
        drawing_layer.polygon([(width, 0),(width-20,0),
                      (width-20, height),(width,height)],
                      fill=(0,0,255,0),outline=(0,255,0,1))
        drawing_layer.polygon([(0, 0),(width,0),
                      (width,20),(0, 20)],
                      fill=(0,0,255,0),outline=(0,255,0,1))
        drawing_layer.polygon([(0, height),(width,height),
                      (width,height-30),(0, height-30)],
                      fill=(0,0,255,0),outline=(0,255,0,1))

        # Make the new image, starting with all transparent
        result = PIL.Image.new('RGBA', original_image.size, (136,39,16,255))
        result.paste(original_image, (0,0), mask=border_mask)

        img = Image.open('ourlogo_test.png')
        logo_small = img.resize((10,23))
        result.paste(logo_small, (width-130,40), mask = logo_small)

        return result

def multi_border(originalimg, new):
    ''' Module for adding borders to images in folders '''

    img = Image.open(originalimg) #opens original image
    #adds border to original
    img_with_border1 = ImageOps.expand(img, border=30, fill='blue')
    img_with_border2 = ImageOps.expand(img_with_border1, border=30,fill='white')
    img_with_border3 = ImageOps.expand(img_with_border2, border=30, fill='red')

    return img_with_border3

def user_color(original, new):
    '''allows user to change border colors from set options'''
    img = Image.open(original) #opens original image
    #adds border to original
    color = raw_input('What color would you like the border to be?(Prompt \
will appear until all images are have a chosen color.): \n')
    time.sleep(1)
    thic = raw_input('What width would you like the border to be?(Prompt \
will appear until all images are have a chosen width.): \n')
    try:
        img_with_border = ImageOps.expand(img, border=int(thic), fill=str(color))
    except ValueError:
        img_with_border = ImageOps.expand(img, border=30, fill=str("blue"))
    try:
        img_with_border = ImageOps.expand(img, border=int(thic), fill=str(color))
    except TypeError:
        img_with_border = ImageOps.expand(img, border=30, fill=str("blue"))

    return img_with_border

def get_images(directory=None):
    """ Returns PIL.Image objects for all the images in directory.

    If directory is not specified, uses current directory.
    Returns a 2-tuple containing
    a list with a  PIL.Image object for each image file in root_directory, and
    a list with a string filename for each image file in root_directory
    """

    if directory == None:
        directory = os.getcwd() # Use working directory if unspecified

    image_list = [] # Initialize aggregaotrs
    file_list = []

    directory_list = os.listdir(directory) # Get list of files
    for entry in directory_list:
        absolute_filename = os.path.join(directory, entry)
        try:
            image = PIL.Image.open(absolute_filename)
            file_list += [entry]
            image_list += [image]
        except IOError:
            pass # do nothing with errors tying to open non-images
    return image_list, file_list

def usercolorIMG(directory=None):
    """ Saves a modfied version of each image in directory.

    Uses current directory if no directory is specified.
    Places images in subdirectory 'modified', creating it if it does not exist.
    New image files are of type PNG and have transparent rounded corners.
    """

    if directory == None:
        directory = os.getcwd() # Use working directory if unspecified

    # Create a new directory 'modified'
    new_directory = os.path.join(directory, 'modified')
    try:
        os.mkdir(new_directory)
    except OSError:
        pass # if the directory already exists, proceed

    # Load all the images
    image_list, file_list = get_images(directory)

    # Go through the images and save modified versions
    for n in range(len(image_list)):
        # Parse the filename
        print(n)
        filename, filetype = os.path.splitext(file_list[n])

        # Round the corners with default percent of radius
        curr_image = image_list[n]
        new_image = user_color(file_list[n],curr_image)

        # Save the altered image, suing PNG to retain transparency
        new_image_filename = os.path.join(new_directory, filename + '.png')
        new_image.save(new_image_filename)

print('Welcome to the Freshman Cars INC. image modifier!')
time.sleep(1)
pic_option = raw_input('What modification would you like? grayscale(1), \
border(2), multi border(3), distorted(4) or coolmask(5) *type the numbers that \
correlate with the specific modification*: ')

if pic_option == "1":
    usercolorIMG()


import yaml_loader as yl
import tensorflow as tf

graph_root = yl.load_yaml('model.yaml')
pf = tf.placeholder(tf.float32, [3,64,64,3])
graph_root.build(pf)

print('-- nodes --')
for node in tf.all_variables():
    print(node.name)

print('-- ops --')
for op in tf.get_default_graph().get_operations():
    print(op.name)

sess = tf.Session()
writer = tf.train.SummaryWriter('logs', sess.graph)
init = tf.initialize_all_variables()
sess.run(init)
writer.add_graph(tf.get_default_graph())
writer.close()


import sys
import socket

from node_ring import NodeRing
from sample_data import USERS
from server_config import NODES
from pickle_hash import serialize_GET, serialize_PUT, serialize_DELETE

from bloom_filter import BloomFilter
from lru_cache import lru_cache

#
# Bloom Filter ########
#  Calculation with n=500 & p=0.01
#  Then k=7 & m=4797
#
bloomFilter = BloomFilter(500,0.01)

BUFFER_SIZE = 1024

class UDPClient():
    def __init__(self, host, port):
        self.host = host
        self.port = int(port)

    def send(self, request):
        print('Connecting to server at {}:{}'.format(self.host, self.port))
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.sendto(request, (self.host, self.port))
            response, ip = s.recvfrom(BUFFER_SIZE)
            return response
        except socket.error:
            print("Error! {}".format(socket.error))
            exit()

    lru_cache(5)
    def put(self, key, payload):
        bloomFilter.add(key)
        return self.send(payload)

    lru_cache(5)
    def get_request(self, key, payload):
        if bloomFilter.is_member(key):
            return self.send(payload)

    lru_cache(5)
    def delete(self, key, payload):
        if bloomFilter.is_member(key):
            return self.send(payload)

def process(udp_clients):
    hash_codes = set()
    # PUT all users.
    for u in USERS:
        data_bytes, key = serialize_PUT(u)
        ring = NodeRing(NODES)
        server_index = NODES.index(ring.get_node(key))
        response = udp_clients[server_index].put(key,data_bytes)
        hash_codes.add(response.decode())
        print(response)

    print(f"Number of Users={len(USERS)}\nNumber of Users Cached={len(hash_codes)}")

    # GET all users.
    for hc in hash_codes:
        print(hc)
        data_bytes, key = serialize_GET(hc)
        ring = NodeRing(NODES)
        server_index = NODES.index(ring.get_node(key))
        response = udp_clients[server_index].get_request(hc, data_bytes)
        print(response)

    # Delete all Users
    for hc in hash_codes:
        print(hc)
        data_bytes, key = serialize_DELETE(hc)
        ring = NodeRing(NODES)
        server_index = NODES.index(ring.get_node(key))
        response = udp_clients[server_index].delete(key,data_bytes)
        print(response)

if __name__ == "__main__":
    clients = [
        UDPClient(server['host'], server['port'])
        for server in NODES
    ]
    process(clients)


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

import tensorflow as tf
from tensorflow.python.keras import backend as K

from deepcell.utils.retinanet_anchor_utils import overlap

def categorical_crossentropy(y_true, y_pred, class_weights=None, axis=None, from_logits=False):
    """Categorical crossentropy between an output tensor and a target tensor.

    Args:
        y_true: A tensor of the same shape as output.
        y_pred: A tensor resulting from a softmax
            (unless from_logits is True, in which
            case y_pred is expected to be the logits).
        from_logits: Boolean, whether y_pred is the
            result of a softmax, or is a tensor of logits.

    Returns:
        tensor: Output tensor.
    """
    # Note: tf.nn.softmax_cross_entropy_with_logits
    # expects logits, Keras expects probabilities.
    if axis is None:
        axis = 1 if K.image_data_format() == 'channels_first' else K.ndim(y_pred) - 1
    if not from_logits:
        # scale preds so that the class probas of each sample sum to 1
        y_pred = y_pred / K.sum(y_pred, axis=axis, keepdims=True)
        # manual computation of crossentropy
        _epsilon = tf.convert_to_tensor(K.epsilon(), y_pred.dtype.base_dtype)
        y_pred = tf.clip_by_value(y_pred, _epsilon, 1. - _epsilon)
        if class_weights is None:
            return - K.sum(y_true * K.log(y_pred), axis=axis)
        return - K.sum((y_true * K.log(y_pred) * class_weights), axis=axis)
    return tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_pred)

def weighted_categorical_crossentropy(y_true, y_pred,
                                      n_classes=3, axis=None,
                                      from_logits=False):
    """Categorical crossentropy between an output tensor and a target tensor.
    Automatically computes the class weights from the target image and uses
    them to weight the cross entropy

    Args:
        y_true: A tensor of the same shape as y_pred.
        y_pred: A tensor resulting from a softmax
            (unless from_logits is True, in which
            case y_pred is expected to be the logits).
        from_logits: Boolean, whether y_pred is the
            result of a softmax, or is a tensor of logits.

    Returns:
        tensor: Output tensor.
    """
    if from_logits:
        raise Exception('weighted_categorical_crossentropy cannot take logits')
    if axis is None:
        axis = 1 if K.image_data_format() == 'channels_first' else K.ndim(y_pred) - 1
    reduce_axis = [x for x in list(range(K.ndim(y_pred))) if x != axis]
    # scale preds so that the class probas of each sample sum to 1
    y_pred = y_pred / K.sum(y_pred, axis=axis, keepdims=True)
    # manual computation of crossentropy
    _epsilon = tf.convert_to_tensor(K.epsilon(), y_pred.dtype.base_dtype)
    y_pred = tf.clip_by_value(y_pred, _epsilon, 1. - _epsilon)
    y_true_cast = K.cast(y_true, K.floatx())
    total_sum = K.sum(y_true_cast)
    class_sum = K.sum(y_true_cast, axis=reduce_axis, keepdims=True)
    class_weights = 1.0 / K.cast_to_floatx(n_classes) * tf.divide(total_sum, class_sum + 1.)
    return - K.sum((y_true * K.log(y_pred) * class_weights), axis=axis)

def sample_categorical_crossentropy(y_true,
                                    y_pred,
                                    class_weights=None,
                                    axis=None,
                                    from_logits=False):
    """Categorical crossentropy between an output tensor and a target tensor.
    Only the sampled pixels defined by y_true = 1 are used to compute the
    cross entropy.

    Args:
        y_true: A tensor of the same shape as y_pred.
        y_pred: A tensor resulting from a softmax
            (unless from_logits is True, in which
            case y_pred is expected to be the logits).
        from_logits: Boolean, whether y_pred is the
            result of a softmax, or is a tensor of logits.

    Returns:
        tensor: Output tensor.
    """
    # Note: tf.nn.softmax_cross_entropy_with_logits
    # expects logits, Keras expects probabilities.
    if axis is None:
        axis = 1 if K.image_data_format() == 'channels_first' else K.ndim(y_pred) - 1
    if not from_logits:
        # scale preds so that the class probabilities of each sample sum to 1
        y_pred = y_pred / K.sum(y_pred, axis=axis, keepdims=True)

        # Multiply with mask so that only the sampled pixels are used
        y_pred = y_pred * y_true

        # manual computation of crossentropy
        _epsilon = tf.convert_to_tensor(K.epsilon(), y_pred.dtype.base_dtype)
        y_pred = tf.clip_by_value(y_pred, _epsilon, 1. - _epsilon)
        if class_weights is None:
            return - K.sum(y_true * K.log(y_pred), axis=axis)
        return - K.sum((y_true * K.log(y_pred) * class_weights), axis=axis)
    return tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_pred)

def dice_loss(y_true, y_pred, smooth=1):
    """Dice coefficient loss between an output tensor and a target tensor.

    Args:
        y_true: A tensor of the same shape as y_pred.
        y_pred: A tensor resulting from a softmax

    Returns:
        tensor: Output tensor.
    """
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return - ((2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth))

def discriminative_instance_loss(y_true, y_pred,
                                 delta_v=0.5,
                                 delta_d=1.5,
                                 gamma=1e-3):
    """Discriminative loss between an output tensor and a target tensor.

    Args:
        y_true: A tensor of the same shape as y_pred.
        y_pred: A tensor of the vector embedding

    Returns:
        tensor: Output tensor.
    """

    def temp_norm(ten, axis=None):
        if axis is None:
            axis = 1 if K.image_data_format() == 'channels_first' else K.ndim(ten) - 1
        return K.sqrt(K.epsilon() + K.sum(K.square(ten), axis=axis))

    rank = K.ndim(y_pred)
    channel_axis = 1 if K.image_data_format() == 'channels_first' else rank - 1
    axes = [x for x in list(range(rank)) if x != channel_axis]

    # Compute variance loss
    cells_summed = tf.tensordot(y_true, y_pred, axes=[axes, axes])
    n_pixels = K.cast(tf.count_nonzero(y_true, axis=axes), dtype=K.floatx()) + K.epsilon()
    n_pixels_expand = K.expand_dims(n_pixels, axis=1) + K.epsilon()
    mu = tf.divide(cells_summed, n_pixels_expand)

    delta_v = K.constant(delta_v, dtype=K.floatx())
    mu_tensor = tf.tensordot(y_true, mu, axes=[[channel_axis], [0]])
    L_var_1 = y_pred - mu_tensor
    L_var_2 = K.square(K.relu(temp_norm(L_var_1) - delta_v))
    L_var_3 = tf.tensordot(L_var_2, y_true, axes=[axes, axes])
    L_var_4 = tf.divide(L_var_3, n_pixels)
    L_var = K.mean(L_var_4)

    # Compute distance loss
    mu_a = K.expand_dims(mu, axis=0)
    mu_b = K.expand_dims(mu, axis=1)

    diff_matrix = tf.subtract(mu_b, mu_a)
    L_dist_1 = temp_norm(diff_matrix)
    L_dist_2 = K.square(K.relu(K.constant(2 * delta_d, dtype=K.floatx()) - L_dist_1))
    diag = K.constant(0, dtype=K.floatx()) * tf.diag_part(L_dist_2)
    L_dist_3 = tf.matrix_set_diag(L_dist_2, diag)
    L_dist = K.mean(L_dist_3)

    # Compute regularization loss
    L_reg = gamma * temp_norm(mu)
    L = L_var + L_dist + K.mean(L_reg)

    return L

def weighted_focal_loss(y_true, y_pred, n_classes=3, gamma=2., axis=None, from_logits=False):
    """Focal loss between an output tensor and a target tensor.
    Automatically computes the class weights from the target image and uses
    them to weight the cross entropy

    Args:
        y_true: A tensor of the same shape as y_pred.
        y_pred: A tensor resulting from a softmax
            (unless from_logits is True, in which
            case y_pred is expected to be the logits).
        from_logits: Boolean, whether y_pred is the
            result of a softmax, or is a tensor of logits.

    Returns:
        tensor: Output tensor.
    """
    if from_logits:
        raise Exception('weighted_focal_loss cannot take logits')
    if axis is None:
        axis = 1 if K.image_data_format() == 'channels_first' else K.ndim(y_pred) - 1
    reduce_axis = [x for x in list(range(K.ndim(y_pred))) if x != axis]
    # scale preds so that the class probas of each sample sum to 1
    y_pred = y_pred / K.sum(y_pred, axis=axis, keepdims=True)
    # manual computation of crossentropy
    _epsilon = tf.convert_to_tensor(K.epsilon(), y_pred.dtype.base_dtype)
    y_pred = tf.clip_by_value(y_pred, _epsilon, 1. - _epsilon)
    y_true_cast = K.cast(y_true, K.floatx())
    total_sum = K.sum(y_true_cast)
    class_sum = K.sum(y_true_cast, axis=reduce_axis, keepdims=True)
    class_weights = 1.0 / K.cast_to_floatx(n_classes) * tf.divide(total_sum, class_sum + 1.)
    temp_loss = (K.pow(1. - y_pred, gamma) * K.log(y_pred) * class_weights)
    focal_loss = - K.sum(y_true * temp_loss, axis=axis)
    return focal_loss

def smooth_l1(y_true, y_pred, sigma=3.0, axis=None):
    """Compute the smooth L1 loss of y_pred w.r.t. y_true.

    Args:
        y_true: Tensor from the generator of shape (B, N, 5).
            The last value for each box is the state of the anchor
            (ignore, negative, positive).
        y_pred: Tensor from the network of shape (B, N, 4).
        sigma: The point where the loss changes from L2 to L1.

    Returns:
        The smooth L1 loss of y_pred w.r.t. y_true.
    """
    if axis is None:
        axis = 1 if K.image_data_format() == 'channels_first' else K.ndim(y_pred) - 1

    sigma_squared = sigma ** 2

    # compute smooth L1 loss
    # f(x) = 0.5 * (sigma * x)^2          if |x| < 1 / sigma / sigma
    #        |x| - 0.5 / sigma / sigma    otherwise
    regression_diff = K.abs(y_true - y_pred)  # |y - f(x)|

    regression_loss = tf.where(
        K.less(regression_diff, 1.0 / sigma_squared),
        0.5 * sigma_squared * K.pow(regression_diff, 2),
        regression_diff - 0.5 / sigma_squared)
    return K.sum(regression_loss, axis=axis)

def focal(y_true, y_pred, alpha=0.25, gamma=2.0, axis=None):
    """Compute the focal loss given the target tensor and the predicted tensor.

    As defined in https://arxiv.org/abs/1708.02002

    Args:
        y_true: Tensor of target data with shape (B, N, num_classes).
        y_pred: Tensor of predicted data with shape (B, N, num_classes).
        alpha: Scale the focal weight with alpha.
        gamma: Take the power of the focal weight with gamma.

    Returns:
        The focal loss of y_pred w.r.t. y_true.
    """
    if axis is None:
        axis = 1 if K.image_data_format() == 'channels_first' else K.ndim(y_pred) - 1

    # compute the focal loss
    alpha_factor = K.ones_like(y_true) * alpha
    alpha_factor = tf.where(K.equal(y_true, 1), alpha_factor, 1 - alpha_factor)
    focal_weight = tf.where(K.equal(y_true, 1), 1 - y_pred, y_pred)
    focal_weight = alpha_factor * focal_weight ** gamma

    cls_loss = focal_weight * K.binary_crossentropy(y_true, y_pred)

    return K.sum(cls_loss, axis=axis)

"""
Retinanet losses
"""

class RetinaNetLosses(object):
    def __init__(self, sigma=3.0, alpha=0.25, gamma=2.0,
                 iou_threshold=0.5, fdl_iou_threshold=0.5,
                 mask_size=(28, 28),
                 parallel_iterations=32):
        self.sigma = sigma
        self.alpha = alpha
        self.gamma = gamma
        self.iou_threshold = iou_threshold
        self.fdl_iou_threshold = fdl_iou_threshold
        self.mask_size = mask_size
        self.parallel_iterations = parallel_iterations

    def regress_loss(self, y_true, y_pred):
        # separate target and state
        regression = y_pred
        regression_target = y_true[..., :-1]
        anchor_state = y_true[..., -1]

        # filter out "ignore" anchors
        indices = tf.where(K.equal(anchor_state, 1))
        regression = tf.gather_nd(regression, indices)
        regression_target = tf.gather_nd(regression_target, indices)

        # compute the loss
        loss = smooth_l1(regression_target, regression, sigma=self.sigma)

        # compute the normalizer: the number of positive anchors
        normalizer = K.maximum(1, K.shape(indices)[0])
        normalizer = K.cast(normalizer, dtype=K.floatx())

        return K.sum(loss) / normalizer

    def classification_loss(self, y_true, y_pred):
        # TODO: try weighted_categorical_crossentropy
        labels = y_true[..., :-1]
        # -1 for ignore, 0 for background, 1 for object
        anchor_state = y_true[..., -1]

        classification = y_pred
        # filter out "ignore" anchors
        indices = tf.where(K.not_equal(anchor_state, -1))
        labels = tf.gather_nd(labels, indices)
        classification = tf.gather_nd(classification, indices)

        # compute the loss
        loss = focal(labels, classification, alpha=self.alpha, gamma=self.gamma)

        # compute the normalizer: the number of positive anchors
        normalizer = tf.where(K.equal(anchor_state, 1))
        normalizer = K.cast(K.shape(normalizer)[0], K.floatx())
        normalizer = K.maximum(K.cast_to_floatx(1.0), normalizer)

        return K.sum(loss) / normalizer

    def mask_loss(self, y_true, y_pred):
        def _mask_conditional(y_true, y_pred):
            # if there are no masks annotations, return 0; else, compute the masks loss
            return tf.cond(
                K.any(K.equal(K.shape(y_true), 0)),
                lambda: K.cast_to_floatx(0.0),
                lambda: _mask_batch(y_true, y_pred,
                                    iou_threshold=self.iou_threshold,
                                    mask_size=self.mask_size,
                                    parallel_iterations=self.parallel_iterations)
            )

        def _mask_batch(y_true, y_pred,
                        iou_threshold=0.5,
                        mask_size=(28, 28),
                        parallel_iterations=32):
            if K.ndim(y_pred) == 4:
                y_pred_shape = tf.shape(y_pred)
                new_y_pred_shape = [y_pred_shape[0] * y_pred_shape[1],
                                    y_pred_shape[2], y_pred_shape[3]]
                y_pred = tf.reshape(y_pred, new_y_pred_shape)

                y_true_shape = tf.shape(y_true)
                new_y_true_shape = [y_true_shape[0] * y_true_shape[1],
                                    y_true_shape[2], y_true_shape[3]]
                y_true = tf.reshape(y_true, new_y_true_shape)

            # split up the different predicted blobs
            boxes = y_pred[:, :, :4]
            masks = y_pred[:, :, 4:]

            # split up the different blobs
            annotations = y_true[:, :, :5]
            width = K.cast(y_true[0, 0, 5], dtype='int32')
            height = K.cast(y_true[0, 0, 6], dtype='int32')
            masks_target = y_true[:, :, 7:]

            # reshape the masks back to their original size
            masks_target = K.reshape(masks_target, (K.shape(masks_target)[0],
                                                    K.shape(masks_target)[1],
                                                    height, width))
            masks = K.reshape(masks, (K.shape(masks)[0], K.shape(masks)[1],
                                      mask_size[0], mask_size[1], -1))

            def _mask(args):
                boxes = args[0]
                masks = args[1]
                annotations = args[2]
                masks_target = args[3]

                return compute_mask_loss(
                    boxes,
                    masks,
                    annotations,
                    masks_target,
                    width,
                    height,
                    iou_threshold=iou_threshold,
                    mask_size=mask_size,
                )

            mask_batch_loss = tf.map_fn(
                _mask,
                elems=[boxes, masks, annotations, masks_target],
                dtype=K.floatx(),
                parallel_iterations=parallel_iterations
            )

            return K.mean(mask_batch_loss)

        return _mask_conditional(y_true, y_pred)

    def final_detection_loss(self, y_true, y_pred):
        def _fd_conditional(y_true, y_pred):
            # if there are no masks annotations, return 0; else, compute fdl loss
            return tf.cond(
                K.any(K.equal(K.shape(y_true), 0)),
                lambda: K.cast_to_floatx(0.0),
                lambda: _fd_batch(y_true, y_pred,
                                  iou_threshold=self.fdl_iou_threshold,
                                  parallel_iterations=self.parallel_iterations))

        def _fd_batch(y_true, y_pred, iou_threshold=0.75, parallel_iterations=32):
            if K.ndim(y_pred) == 4:
                y_pred_shape = tf.shape(y_pred)
                new_y_pred_shape = [y_pred_shape[0] * y_pred_shape[1],
                                    y_pred_shape[2], y_pred_shape[3]]
                y_pred = tf.reshape(y_pred, new_y_pred_shape)

                y_true_shape = tf.shape(y_true)
                new_y_true_shape = [y_true_shape[0] * y_true_shape[1],
                                    y_true_shape[2], y_true_shape[3]]
                y_true = tf.reshape(y_true, new_y_true_shape)

            # split up the different predicted blobs
            boxes = y_pred[:, :, :4]
            scores = y_pred[:, :, 4:5]

            # split up the different blobs
            annotations = y_true[:, :, :5]

            def _fd(args):
                boxes = args[0]
                scores = args[1]
                annotations = args[2]

                return compute_fd_loss(
                    boxes,
                    scores,
                    annotations,
                    iou_threshold=iou_threshold)

            fd_batch_loss = tf.map_fn(
                _fd,
                elems=[boxes, scores, annotations],
                dtype=K.floatx(),
                parallel_iterations=parallel_iterations)

            return K.mean(fd_batch_loss)

        return _fd_conditional(y_true, y_pred)

def compute_mask_loss(boxes,
                      masks,
                      annotations,
                      masks_target,
                      width,
                      height,
                      iou_threshold=0.5,
                      mask_size=(28, 28)):
    """compute overlap of boxes with annotations"""
    iou = overlap(boxes, annotations)
    argmax_overlaps_inds = K.argmax(iou, axis=1)
    max_iou = K.max(iou, axis=1)

    # filter those with IoU > 0.5
    indices = tf.where(K.greater_equal(max_iou, iou_threshold))
    boxes = tf.gather_nd(boxes, indices)
    masks = tf.gather_nd(masks, indices)
    argmax_overlaps_inds = K.cast(tf.gather_nd(argmax_overlaps_inds, indices), 'int32')
    labels = K.cast(K.gather(annotations[:, 4], argmax_overlaps_inds), 'int32')

    # make normalized boxes
    x1 = boxes[:, 0]
    y1 = boxes[:, 1]
    x2 = boxes[:, 2]
    y2 = boxes[:, 3]
    boxes = K.stack([
        y1 / (K.cast(height, dtype=K.floatx()) - 1),
        x1 / (K.cast(width, dtype=K.floatx()) - 1),
        (y2 - 1) / (K.cast(height, dtype=K.floatx()) - 1),
        (x2 - 1) / (K.cast(width, dtype=K.floatx()) - 1),
    ], axis=1)

    # crop and resize masks_target
    # append a fake channel dimension
    masks_target = K.expand_dims(masks_target, axis=3)
    masks_target = tf.image.crop_and_resize(
        masks_target,
        boxes,
        argmax_overlaps_inds,
        mask_size
    )
    masks_target = masks_target[:, :, :, 0]  # remove fake channel dimension

    # gather the predicted masks using the annotation label
    masks = tf.transpose(masks, (0, 3, 1, 2))
    label_indices = K.stack([tf.range(K.shape(labels)[0]), labels], axis=1)

    masks = tf.gather_nd(masks, label_indices)

    # compute mask loss
    mask_loss = K.binary_crossentropy(masks_target, masks)
    normalizer = K.shape(masks)[0] * K.shape(masks)[1] * K.shape(masks)[2]
    normalizer = K.maximum(K.cast(normalizer, K.floatx()), 1)
    mask_loss = K.sum(mask_loss) / normalizer

    return mask_loss

def compute_fd_loss(boxes, scores, annotations, iou_threshold=0.75):
    """compute the overlap of boxes with annotations"""
    iou = overlap(boxes, annotations)

    max_iou = K.max(iou, axis=1, keepdims=True)
    targets = K.cast(K.greater_equal(max_iou, iou_threshold), K.floatx())

    # compute the loss
    loss = focal(targets, scores)  # alpha=self.alpha, gamma=self.gamma)

    # compute the normalizer: the number of cells present in the image
    normalizer = K.cast(K.shape(annotations)[0], K.floatx())
    normalizer = K.maximum(K.cast_to_floatx(1.0), normalizer)

    return K.sum(loss) / normalizer


#ApiCommand.py
#Created 31.05.2001 by Arild Fines

import Command
import Config
import string
import Acl
import ConfigParser
import urlparse
import httplib

class RefCommand( Command.Command ):
    config = None

    def __init__( self, theNick, theRoom, theCommand, isPm = 0 ):
        Command.Command.__init__( self, theNick, theRoom, theCommand, isPm )
        self.config = ConfigParser.ConfigParser()
        self.config.read( "api.cfg" )

    def doExecute( self, sock, words ):
        """dispatches control to the appropriate method"""

        #check for permission
        if not self.room.getAcl().hasPermission( self.nick, Acl.API ):
            raise Command.PermissionError( self.nick + " does not have permissions for this operation" )

        #command or lookup?
        command = string.lower( words[ 1 ] )
        if command[0] == '-':
            self.execCommand( sock, command[ 1: ], words[ 2: ] )
        else:
            self.lookup( sock, command, words )

    def lookup( self, sock, command, words ):
        """does the lookup - dispatching it to submethods"""
        methodName = "lookup" + string.capitalize( command )
        msg = ""
        try:
            leading = self.getString( command, "leading" )
            trailing = self.getString( command, "trailing" )
            method = getattr( self, methodName, self.lookupDefault )
            msg = leading + method(words[ 2: ]) + trailing

            #if we get here, msg should be an url
            #test it for validity
            (scheme, location, path, parameters, query, fragment) = urlparse.urlparse( msg )

            connection = httplib.HTTPConnection( location )
            connection.request( "GET", path )

            if connection.getresponse().status >= 400:
                msg = "item %s not found @ %s" % ("".join( words[2:] ), msg )

        except( ConfigParser.NoSectionError ):
            msg = "No handler registered for " + command
        except( ConfigParser.NoOptionError ):
            msg = "Handler for %s improperly configured." % command
        except( httplib.HTTPException ):
            msg = "Cannot connect to URL to verify"

        self.sendMessage( sock, msg )

    def execCommand( self, sock, command, words ):
        """executes a command"""
        methodName = "cmd" + command.capitalize()
        method = getattr( self, methodName, self.cmdDefault )
        method( sock, words )

    def cmdHelp( self, sock, words ):
        """retrieves the list of installed handlers"""
        msg = ", ".join( self.config.sections() )
        self.sendMessage( sock, "Installed handlers: " + msg )

    def cmdDefault( self, sock, words ):
        """default handler - error message"""
        self.sendMessage( sock, "No comprendo, señor" )

    """def lookupDotnet( self, words ):
        concat = "".join( words )
        if concat.count( "." ) == 0:
            return "system" + concat
        else:
            return "".join( concat.split( "." ) )

    def lookupDotnetlocal( self, words ):
        return self.lookupDotnet( words )

    def lookupDotnetns( self, words ):
        return self.lookupDotnet( words )"""

    def lookupJava( self, words ):
        """Generic handler for Java classes - uses JDK1.4 docs by default"""
        return self.lookupJdk14( words )

    def lookupJavapk( self, words ):
        return self.lookupJava( words )

    def lookupJdk14( self, words ):
        """returns an url to a jdk1.4 class"""
        return self.lookupJavaInternal( words )

    def lookupDefault( self, words ):
        """default handler"""
        return "".join( words )

    def lookupJavaInternal( self, words ):
        """internal method used for all the JDK versions"""
        concat = "".join( words )
        if concat.count( "." ) == 0:
            return "java/lang/" + concat
        else:
            return "/".join( concat.split( "." ) )

    def getString( self, section, value ):
        """returns a value from the api.cfg file"""
        return self.config.get( section, value ).strip()