#-*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.patches as patches
import matplotlib.path as path
import json
from disk_schedule import ROUND,ALGORITHMS
from matplotlib.backends.backend_pdf import PdfPages

'''
Function draw_pic is based on the code in this page
http://matplotlib.org/examples/api/histogram_demo.html
'''

COLORS=['b','g', 'r','c','m','y','k' ,'grey']
'''
b: blue
g: green
r: red
c: cyan
m: magenta
y: yellow
k: black

'''

def draw_pic(data):
    pp = PdfPages('distributions.pdf',)
    a_pp=PdfPages('summary.pdf',)
    a_fig = plt.figure()
    for algorithm in ALGORITHMS:
        name=algorithm
        al_data=data[algorithm]

        mu, sigma = np.average(al_data),np.std(al_data)
        x = mu + sigma * np.random.randn(10000)
        fig = plt.figure()

        a_ax=a_fig.add_subplot(111)# for summary

        ax = fig.add_subplot(1,2,1)
        x=np.linspace(-5*sigma+mu,mu+5*sigma,100)
        y = mlab.normpdf(x,mu, sigma)
        l = ax.plot(x, y, 'r', linewidth=1)
        ax.set_xlabel('Normal Distribution')
        ax.set_ylabel('Probability')
        ax.set_title(r'$\ %s: \mu=%s,\ \sigma=%s$'%(name,mu,sigma))
        ax.autoscale_view()
        ax.grid(True)

        a_ax.plot(x, y,lw=8, label= algorithm,color= COLORS[ALGORITHMS.index(algorithm)], linewidth=1.0)
        a_ax.legend(loc='upper right')

        ax = fig.add_subplot(1,2,2)
        ax.set_xlabel('Distribution')
        n, bins = np.histogram(al_data, 40)
        left = np.array(bins[:-1])
        right = np.array(bins[1:])
        bottom = np.zeros(len(left))
        top = bottom + n
        XY = np.array([[left,left,right,right], [bottom,top,top,bottom]]).T
        barpath = path.Path.make_compound_path_from_polys(XY)
        patch = patches.PathPatch(barpath, facecolor='blue', edgecolor='gray', alpha=0.8)
        ax.add_patch(patch)
        ax.set_xlim(left[0], right[-1])
        ax.set_ylim(bottom.min(), top.max())
        ax.grid(True)
        fig.set_size_inches(12,8)
        pp.savefig(fig)
    a_fig.set_size_inches(52,8)
    a_pp.savefig(a_fig)
#    plt.show()# if you want to see the picture directly, uncomment this line
    a_pp.close()
    pp.close()

def reform_data():
    reform={}
    with open('output.txt') as data:
        data=data.read()
    data= json.loads(data)
    for no in range(1,ROUND+1):
        for algorithm in range(len(ALGORITHMS)):
            name=data[str(no)]["results"][algorithm]["name"]
            path_cost=data[str(no)]["results"][algorithm]["path_cost"]
            try:
                reform[name].append(path_cost)
            except:
                reform[name]=[path_cost]
    return reform

if __name__ =='__main__':
    data=reform_data()
    draw_pic(data)


from sher.models import Account, Service
import simplejson
import urllib2
import urlparse

#service shoterning url,
#is.gd fallback
def shorten_url(url):

    if 'flickr' in url:
        parsed_url = urlparse.urlsplit(url)
        photo_id = parsed_url.path.split('/')[3]

        if photo_id:
            base58_id = base58(photo_id)
            url =  'http://flic.kr/p/%s' % base58_id

    elif 'youtube' in url:
        parsed_url = urlparse.urlsplit(url)
        video_id = urlparse.parse_qs(parsed_url.query)['v'][0]

        if video_id:
            url = 'http://youtu.be/%s' % video_id

    else:
        api = "http://is.gd/create.php?format=json&url=%s" % url
        try:
            json = simplejson.loads(urllib2.urlopen(api).read())
            url = json['shorturl']
        except:
            pass

    return url

def get_services_list(text):

    if text.find(',') != -1: #comma split
        slist = text.split(',')
    else:
        slist = text.split() #by space

    return slist

def get_account(service_name):
    return Account.objects.get(service_name__iexact=service_name)

def get_services(services):

    mod = __import__('sher.services', fromlist=['*'])

    sdict = {}

    for s in services:
        s = s.lower()
        try:
            sdict[s] = getattr(mod, '%s_service' % s)
        except AttributeError:
            pass

    for name,service in sdict.items():
        sdict[name] = service.authenticated(get_account(name))

    return sdict #return service name: authenticated api instance

def base58(n):
    alphabet='123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
    result = ''
    while n >= 58:
        div,mod = divmod(int(n),58)
        result = alphabet[mod] + result
        n = div
    result = alphabet[n] + result
    base_count=len(alphabet)
    return result


import roslib
import rospy

import actionlib
import dynamic_reconfigure.client

import datetime

from std_srvs.srv import Empty
from seneka_sensor_placement.srv import polygon_offset
import seneka_sensor_placement.msg

# lists specifying the different parameters, over which to iterate
global gs_resolution, greedyPSO_particles, number_of_sensors, perimeter_offsets
gs_resolution = [10.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.75, 0.5, 0.25, 0.1]
greedyPSO_particles = [100, 40, 30, 20, 10]
number_of_sensors = [1, 2, 3, 4, 5, 6, 7]
perimeter_offsets = [10.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.75, 0.5, 0.25, 0.1]

# IDs for action calls
global algo
algo = {"PSO": 1, "GreedyPSO": 2, "GreedySearch": 3, "GreedySearchPerimeter": 4}

def update_config(client, cfg_update):
    rospy.sleep(2.5) # sleep some time to wait for action server
    old_cfg = dyn_reconf_client.get_configuration(5.0)
    dyn_reconf_client.update_configuration(cfg_update)
    new_cfg = dyn_reconf_client.get_configuration(5.0)
    rospy.sleep(2.5)
    return old_cfg, new_cfg

def call_action(client, goal):
    rospy.sleep(2.5)
    start_time = rospy.Time().now()
    client.wait_for_server()
    client.send_goal(goal)
    client.wait_for_result()
    result = client.get_result()
    end_time = rospy.Time().now()
    delta_t = (end_time - start_time).to_sec()
    rospy.sleep(2.5)
    return delta_t, result

def result_to_file(f, algo_id, update, result, delta_t, service_input_arg=None):
    for key, service_id in algo.iteritems():
        if service_id == algo_id:
            string_to_file = "Called Action with Algo "+str(key)+"\n"
            f.write(string_to_file)
    if not service_input_arg == None:
        string_to_file = "Set service_input_arg to "+str(service_input_arg)+"\n"
        f.write(string_to_file)
    string_to_file = "Updated configuration is "+str(update)+"\n"
    f.write(string_to_file)
    string_to_file = "Returned with result "+str(result)+" in "+str(delta_t)+" seconds"+"\n"
    f.write(string_to_file)
    #shortform
    string_to_file = "short: "+str(algo_id)+" "+str(result)+" "+str(delta_t)
    for key, value in update.iteritems():
        string_to_file += " "+str(key)+": "+str(value)
    if not service_input_arg == None:
        string_to_file += " "+str(service_input_arg)
    f.write(string_to_file)
    f.write("\n===================\n\n")

if __name__ == '__main__':
    # init node
    rospy.init_node("sensor_placement_test_node")
    rospy.sleep(0.5)

    # setup client
    dyn_reconf_client = dynamic_reconfigure.client.Client("/sensor_placement")
    # get current config
    start_config = dyn_reconf_client.get_configuration(5.0)
    rospy.loginfo("Dynamic reconfigure Client set up!")

    # setup action client
    client = actionlib.SimpleActionClient('sensorPlacementActionServer',seneka_sensor_placement.msg.sensorPlacementAction)
    # setup dummy goal
    goal = seneka_sensor_placement.msg.sensorPlacementGoal()

    # setup services for publishing different stuff
    rospy.wait_for_service('publish_AoI')
    pub_AoI_sc = rospy.ServiceProxy('publish_AoI', Empty)
    rospy.loginfo("publish_AoI Service connected!")

    rospy.wait_for_service('publish_PoI')
    pub_PoI_sc = rospy.ServiceProxy('publish_PoI', Empty)
    rospy.loginfo("publish_PoI Service connected!")

    rospy.wait_for_service('publish_PoIs')
    pub_PoIs_sc = rospy.ServiceProxy('publish_PoIs', Empty)
    rospy.loginfo("publish_PoIs Service connected!")

    rospy.wait_for_service('publish_forbidden_areas')
    pub_forbidden_area_sc = rospy.ServiceProxy('publish_forbidden_areas', Empty)
    rospy.loginfo("publish_forbidden_areas Service connected!")

    ##
    ## run tests
    ##

    ## setup AoI and forbidden Area
    rospy.loginfo("Creating Area of interest")
    pub_AoI_sc()
    rospy.loginfo("Setup forbidden area")
    pub_forbidden_area_sc()

    # open file
    now = datetime.datetime.now()
    with open('result_'+now.strftime('%y%m%d')+'_'+now.strftime('%H%M%S')+'.txt','w') as f:
        string_to_file = "Results of test run at "+str(now)+"\n===================\n\n"
        f.write(string_to_file)
        f.write("Notes: \nThe config from the yaml file is always restored before updating the configuration.\n")
        f.write("This should help to see the differences from the start configuration. The start config is:\n")
        for key, value in start_config.iteritems():
            if not key == "groups":
                f.write(str(key)+": "+str(value)+"\n")
        f.write("\n\n\n\n")

        counter = 0
        # PSO with different number of sensors
        for num in number_of_sensors:
            counter += 1
            rospy.loginfo("Start PSO test num %d of %d", counter, len(number_of_sensors))
            update_config(dyn_reconf_client, start_config)
            update = {"number_of_sensors": str(num)}
            old_cfg, new_cfg = update_config(dyn_reconf_client, update)

            # call action
            goal.service_id = algo["PSO"]
            delta_t, result = call_action(client,goal)

            result_to_file(f, goal.service_id, update, result, delta_t)
        f.write("===================\n\n\n")

        counter = 0
        # GreedyPSO with different number of sensors and particles
        for num in number_of_sensors:
            for particles in greedyPSO_particles:
                counter += 1
                rospy.loginfo("Start GreedyPSO test num %d of %d", counter, len(number_of_sensors)*len(greedyPSO_particles))
                update_config(dyn_reconf_client, start_config)
                update = {"number_of_sensors": str(num), "number_of_particles": str(particles)}
                old_cfg, new_cfg = update_config(dyn_reconf_client, update)

                # call action
                goal.service_id = algo["GreedyPSO"]
                delta_t, result = call_action(client,goal)

                result_to_file(f, goal.service_id, update, result, delta_t)
        f.write("===================\n\n\n")

        counter = 0
        # Greedy with different number of sensors and different spacing of grid
        for num in number_of_sensors:
            for spacing in gs_resolution:
                counter += 1
                rospy.loginfo("Start Greedy test num %d of %d", counter, len(number_of_sensors)*len(gs_resolution))
                update_config(dyn_reconf_client, start_config)
                update = {"number_of_sensors": str(num), "GS_target_offset": str(spacing)}
                old_cfg, new_cfg = update_config(dyn_reconf_client, update)

                # call action
                goal.service_id = algo["GreedySearch"]
                delta_t, result = call_action(client,goal)

                result_to_file(f, goal.service_id, update, result, delta_t)
        f.write("===================\n\n\n")

        counter = 0
        # Greedy on Perimeter with different number of sensors and different spacing of grid
        for num in number_of_sensors:
            for spacing in gs_resolution:
                for offset in perimeter_offsets:
                    counter += 1
                    rospy.loginfo("Start Greedy on Perimeter test num %d of %d", counter, len(number_of_sensors)*len(gs_resolution)*len(perimeter_offsets))
                    update_config(dyn_reconf_client, start_config)
                    update = {"number_of_sensors": str(num), "GS_target_offset": str(spacing)}
                    old_cfg, new_cfg = update_config(dyn_reconf_client, update)

                    # call action
                    goal.service_id = algo["GreedySearchPerimeter"]
                    # set offset
                    goal.service_input_arg = offset
                    delta_t, result = call_action(client,goal)

                    result_to_file(f, goal.service_id, update, result, delta_t, service_input_arg=goal.service_input_arg)
                    # to be sure, reset offset
                    goal.service_input_arg = 0.0
        f.write("===================\n\n\n")

        # Finished
        now = datetime.datetime.now()
        string_to_file = "\n\n===================\nFinished test runs at "+str(now)
        f.write(string_to_file)


from django.shortcuts import render
from graphs.models import Graph
from django.http import Http404
from django.contrib.auth.decorators import login_required

@login_required
def graph_by_query(request, query, type_):
    if type_ == 'isp':
        imgs = Graph.objects.filter(isp=query)
    elif type_ == 'period':
        pd = {'daily': 'd',
              'weekly': 'w',
              'monthly': 'm',
              'yearly': 'y'}
        imgs = Graph.objects.filter(period=pd[query])
    if len(imgs) == 0:
        raise Http404
    return render(request, 'traf/graph_list.html', {'imgs': imgs, 'type_': type_})


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Aug  2 14:14:59 2019

@author: vavan
"""

import xml.etree.ElementTree as ET
import os

A = []
A.append(['image_names', 'cell_type', 'xmin', 'xmax', 'ymin', 'ymax'])

list_of_xml = os.listdir("./BCCD/Annotations/")

for picture_xml in list_of_xml:

    tree = ET.parse('./BCCD/Annotations/' + picture_xml)
    root = tree.getroot()
    image_name = root.find('filename').text

    for object in root.findall('object'):
        cell_type = object.find('name')
        xmin = object.find('bndbox/xmin').text
        xmax = object.find('bndbox/xmax').text
        ymin = object.find('bndbox/ymin').text
        ymax = object.find('bndbox/ymax').text

        A.append([image_name, cell_type.text, int(xmin), int(xmax), int(ymin), int(ymax)])

# generate the data.csv file

import csv

with open("data.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(A)


from django.db import models
import datetime
from django.utils import timezone

class List(models.Model):
        AVAILABLE = 'Y'
        USED = 'N'
        STATUS_FOR_NOW = ((AVAILABLE, 'Available'), (USED, 'Used'),)
        ip = models.CharField('IP Address', max_length=20)
        status = models.CharField('Status', max_length=1, choices = STATUS_FOR_NOW, default=AVAILABLE)
        user = models.CharField('User Name', max_length=30)
        machine = models.CharField('Machine Name', max_length=50)
        location = models.CharField('location', max_length=50)
        note = models.CharField('Note', max_length=100)
        date = models.DateTimeField('Date started using')
        def __unicode__(self):
                return self.ip


from constants import *
from utils import *
from math import inf
import time
counter = 0

class TentsGameSolver(object):
    def __init__(self, tents_game):
        self.tents_game = tents_game
        self.field = tents_game.field
        self.size = tents_game.size
        self.tips_x_copy = tents_game.tips_x_copy
        self.tips_y_copy = tents_game.tips_y_copy
        self.tips_x = tents_game.tips_x
        self.tips_y = tents_game.tips_y
        self.trees = tents_game.trees
        self.trees_possibilities = HeapMin([]) # heap to use in solving
        self.possible_tents = HeapMax([])
        self.verified_tips_equal_0 = False
        self.verified_possible_tents_equal_1 = False
        self.verified_tips_equal_possible_tents = False
        self.is_wrong = False
        self.state_queue = [] # To store the previous state of solving: field, tips_copy
        self.tents_put = []

        # tic = time.time()
        self.do_func(self.start, self.verifies_tips_equal_0)
        self.do_func(self.verifies_possible_tents_equal_1, self.verifies_tips_equal_0)
        while self.verified_possible_tents_equal_1:
            self.do_func(self.verifies_possible_tents_equal_1, self.verifies_tips_equal_0)

        self.do_func(self.verifies_tips_equal_possible_tents, self.verifies_tips_equal_0)
        while self.verified_tips_equal_possible_tents or self.verified_possible_tents_equal_1 or self.verified_tips_equal_0:
            self.do_func(self.verifies_tips_equal_possible_tents, self.verifies_tips_equal_0)
            if len(self.tents_put) == 0:
                self.do_func(self.verifies_possible_tents_equal_1, self.verifies_tips_equal_0)
                while self.verified_possible_tents_equal_1:
                    self.do_func(self.verifies_possible_tents_equal_1, self.verifies_tips_equal_0)
        # toc = time.time()
        # print(toc-tic)
        # print('Finished initial state')
        self.check_is_wrong()
        self.heuristic()

    def do_func(self, func1, func2):
        func1()
        func2()

    def is_valid_coordinate(self, i, j):
        if i >= 0 and i < self.size and j >= 0 and j < self.size:
            return True
        return False

    def update_minimum_heap(self, i, j):
        for ii, jj in FOUR_CONNECTED:
            coord_i = i + ii
            coord_j = j + jj
            if self.is_valid_coordinate(coord_i, coord_j) and self.field[coord_i, coord_j] == TREE:
                index = self.trees_possibilities.find((coord_i, coord_j))
                if index != None:
                    self.trees_possibilities.modify(index, (self.trees_possibilities[index][0]-1, self.trees_possibilities[index][1]))

    def update_maximum_heap(self, i, j):
        index = self.possible_tents.find((i, j))
        self.possible_tents.modify(index, (inf, self.possible_tents.heap[index][1]))
        self.possible_tents.extract_max()
        for ii, jj in EIGHT_CONNECTED:
            coord_i = i + ii
            coord_j = j + jj
            if self.is_valid_coordinate(coord_i, coord_j) and self.field[coord_i, coord_j] == POSSIBLE_TENT:
                index = self.possible_tents.find((coord_i, coord_j))
                self.possible_tents.modify(index, (self.possible_tents.heap[index][0]-1, self.possible_tents.heap[index][1]))

    def put_tent(self, tree, know_what_tree=True, tent=(None, None)):
        # if tent == (6, 16):
        #     print('é')
        #     pass
        if know_what_tree:
            for i, j in FOUR_CONNECTED:
                coord_i = tree[0] + i
                coord_j = tree[1] + j
                if self.is_valid_coordinate(coord_i, coord_j):
                    if self.field[coord_i, coord_j] == POSSIBLE_TENT:
                        self.field[coord_i, coord_j] = TENT
                        self.update_8_connected((coord_i, coord_j))
                        self.tips_x_copy[coord_j] -= 1
                        self.tips_y_copy[coord_i] -= 1
        else:
            if self.field[tent[0], tent[1]] == EMPTY:
                self.is_wrong = True
            else:
                self.field[tent[0], tent[1]] = TENT
                self.update_8_connected(tent)
                self.tips_x_copy[tent[1]] -= 1
                self.tips_y_copy[tent[0]] -= 1

    def update_8_connected(self, tent):
        self.update_maximum_heap(tent[0], tent[1])
        self.update_minimum_heap(tent[0], tent[1])
        for i, j in EIGHT_CONNECTED:
            coord_i = tent[0] + i
            coord_j = tent[1] + j
            if self.is_valid_coordinate(coord_i, coord_j):
                if self.field[coord_i, coord_j] == POSSIBLE_TENT:
                    self.field[coord_i, coord_j] = EMPTY
                    self.update_minimum_heap(coord_i, coord_j)
                    self.update_maximum_heap(coord_i, coord_j)

    def remove_trees(self, finished=False):
        if not finished and not self.is_wrong:
            entrou = True
            while entrou:
                entrou = False
                aux = []
                for tent in self.tents_put:
                    num_trees = 0
                    for i, j in FOUR_CONNECTED:
                        coord_i, coord_j = tent[0] + i, tent[1] + j
                        if self.is_valid_coordinate(coord_i, coord_j):
                            if self.field[coord_i, coord_j] == TREE:
                                index = self.trees_possibilities.find((coord_i, coord_j))
                                if index != None:
                                    num_trees += 1
                                    coord_i_aux, coord_j_aux = coord_i, coord_j
                    if num_trees == 1:
                        entrou = True
                        for i, j in FOUR_CONNECTED:
                            coord_i, coord_j = coord_i_aux + i, coord_j_aux + j
                            if self.is_valid_coordinate(coord_i, coord_j):
                                if self.field[coord_i, coord_j] == POSSIBLE_TENT:
                                    tem_arvore = False
                                    for ii, jj in FOUR_CONNECTED:
                                        coord_ii, coord_jj = coord_i + ii, coord_j + jj
                                        if self.is_valid_coordinate(coord_ii, coord_jj):
                                            if self.field[coord_ii, coord_jj] == TREE and (coord_ii, coord_jj) != (coord_i_aux, coord_j_aux):
                                                tem_arvore = True
                                    if not tem_arvore:
                                        self.field[coord_i, coord_j] = EMPTY
                                        self.update_maximum_heap(coord_i, coord_j)
                                        self.update_minimum_heap(coord_i, coord_j)
                        aux.append((tent, coord_i_aux, coord_j_aux))
                if len(aux) > 0:
                    for tent, coord_i, coord_j in aux:
                        indextree = self.trees_possibilities.find((coord_i, coord_j))
                        if indextree != None:
                            self.trees_possibilities.modify(indextree, (-inf, (coord_i, coord_j)))
                            self.trees_possibilities.extract_min()
                            try:
                                self.tents_put.pop(self.tents_put.index(tent))
                            except ValueError:
                                self.is_wrong = True

            self.check_has_cycle()

    def check_has_cycle(self):
        possible_tents = []
        for tent in self.tents_put:
            num_trees = 0
            for i, j in FOUR_CONNECTED:
                coord_i, coord_j = tent[0] + i, tent[1] + j
                if self.is_valid_coordinate(coord_i, coord_j):
                    if self.field[coord_i, coord_j] == TREE:
                        index = self.trees_possibilities.find((coord_i, coord_j))
                        if index != None:
                            vale = True
                            for ii, jj in FOUR_CONNECTED:
                                coord_ii, coord_jj = coord_i + ii, coord_j + jj
                                if self.is_valid_coordinate(coord_ii, coord_jj):
                                    if self.field[coord_ii, coord_jj] == POSSIBLE_TENT:
                                        vale = False
                                        break
                            if vale:
                                num_trees += 1
            if num_trees >= 2:
                possible_tents.append(tent)
        trees = []
        trees_quantities = []
        for tent in possible_tents:
            for i, j in FOUR_CONNECTED:
                coord_i, coord_j = tent[0] + i, tent[1] + j
                if self.is_valid_coordinate(coord_i, coord_j):
                    if self.field[coord_i, coord_j] == TREE:
                        if (coord_i, coord_j) not in trees:
                            trees.append((coord_i, coord_j))
                            trees_quantities.append(1)
                        else:
                            trees_quantities[trees.index((coord_i, coord_j))] += 1
            # self.tents_put.pop(self.tents_put.index(tent))
        aux = trees_quantities.count(2) + trees_quantities.count(3) + trees_quantities.count(4)
        if aux == len(possible_tents):
            if aux == len(self.tents_put):
                for tree, quantity in zip(trees, trees_quantities):
                    if quantity == 2:
                        index = self.trees_possibilities.find(tree)
                        self.trees_possibilities.modify(index, (-inf, tree))
                        self.trees_possibilities.extract_min()
                        for i, j in FOUR_CONNECTED:
                            coord_i, coord_j = tree[0] + i, tree[1] + j
                            if self.is_valid_coordinate(coord_i, coord_j):
                                if self.field[coord_i, coord_j] == TENT:
                                    if (coord_i, coord_j) in self.tents_put:
                                        self.tents_put.pop(self.tents_put.index((coord_i, coord_j)))

    def save_state(self, possible_tent, was_tent):
        self.state_queue.append((
            self.field.copy(),
            self.tips_x_copy.copy(),
            self.tips_y_copy.copy(),
            HeapMax(self.possible_tents.heap.copy()),
            HeapMin(self.trees_possibilities.copy()),
            self.tents_put.copy(),
            self.verified_tips_equal_0,
            self.verified_possible_tents_equal_1,
            self.verified_tips_equal_possible_tents,
            self.is_wrong,
            possible_tent,
            was_tent
        ))

    def return_last_state(self):
        (
            self.tents_game.field,
            self.tents_game.tips_x_copy,
            self.tents_game.tips_y_copy,
            self.possible_tents,
            self.trees_possibilities,
            self.tents_put,
            self.verified_tips_equal_0,
            self.verified_possible_tents_equal_1,
            self.verified_tips_equal_possible_tents,
            self.is_wrong,
            possible_tent,
            was_tent
        ) = self.state_queue.pop()
        self.field = self.tents_game.field
        self.tips_x_copy = self.tents_game.tips_x_copy
        self.tips_y_copy = self.tents_game.tips_y_copy
        return possible_tent, was_tent

    def check_is_wrong(self, finished=False):
        if not self.is_wrong:
            # Check if a tree has no tent
            for tree in self.trees:
                self.is_wrong = True
                for i, j in FOUR_CONNECTED:
                    coord_i = tree[0] + i
                    coord_j = tree[1] + j
                    if self.is_valid_coordinate(coord_i, coord_j):
                        if self.field[coord_i, coord_j] == TENT or self.field[coord_i, coord_j] == POSSIBLE_TENT:
                            self.is_wrong = False
                            break
                if self.is_wrong:
                    break
            # Check if number of tents is greater than tips
            for j, tip_x in enumerate(self.tips_x):
                if tip_x < len(np.where(self.field[:, j] == TENT)[0]):
                    self.is_wrong = True
                    break
            for i, tip_y in enumerate(self.tips_y):
                if tip_y < len(np.where(self.field[i, :] == TENT)[0]):
                    self.is_wrong = True
                    break
            if len(self.possible_tents.heap) == 0:
                # Check if number of tents is less than tips when is over
                for j, tip_x in enumerate(self.tips_x):
                    if tip_x > len(np.where(self.field[:, j] == TENT)[0]):
                        self.is_wrong = True
                        break
                for i, tip_y in enumerate(self.tips_y):
                    if tip_y > len(np.where(self.field[i, :] == TENT)[0]):
                        self.is_wrong = True
                        break
                if len(self.trees_possibilities) != 0:
                    self.is_wrong = True
            else:
                pass

    # States of the solver
    def start(self):
        possible_tents = []
        for tree in self.trees:
            num_possible_tents = 0
            for i, j in FOUR_CONNECTED:
                coord_i = tree[0] + i
                coord_j = tree[1] + j
                if self.is_valid_coordinate(coord_i, coord_j):
                    if self.field[coord_i, coord_j] == EMPTY or self.field[coord_i, coord_j] == POSSIBLE_TENT:
                        if self.field[coord_i, coord_j] != POSSIBLE_TENT:
                            possible_tents.append((coord_i, coord_j))
                        self.field[coord_i, coord_j] = POSSIBLE_TENT
                        num_possible_tents += 1
            self.trees_possibilities.insert((num_possible_tents, tree))
        for possible_tent in possible_tents:
            num_possible_tents = 0
            for i, j in EIGHT_CONNECTED:
                coord_i = possible_tent[0] + i
                coord_j = possible_tent[1] + j
                if self.is_valid_coordinate(coord_i, coord_j):
                    if self.field[coord_i, coord_j] == POSSIBLE_TENT:
                        num_possible_tents += 1
            self.possible_tents.insert((num_possible_tents, possible_tent))

    def verifies_tips_equal_0(self):
        self.verified_tips_equal_0 = False
        if not self.is_wrong:
            for j in np.where(self.tips_x_copy == 0)[0]:
                self.verified_tips_equal_0 = True
                self.tips_x_copy[j] = TIPS_ZEROED
                for i in np.where(self.field[:, j] == POSSIBLE_TENT)[0]:
                    self.field[i, j] = EMPTY
                    self.update_minimum_heap(i, j)
                    self.update_maximum_heap(i, j)
            for i in np.where(self.tips_y_copy == 0)[0]:
                self.verified_tips_equal_0 = True
                self.tips_y_copy[i] = TIPS_ZEROED
                for j in np.where(self.field[i, :] == POSSIBLE_TENT)[0]:
                    self.field[i, j] = EMPTY
                    self.update_minimum_heap(i, j)
                    self.update_maximum_heap(i, j)

    def verifies_possible_tents_equal_1(self):
        self.verified_possible_tents_equal_1 = False
        if not self.is_wrong:
            while len(self.trees_possibilities) > 0 and self.trees_possibilities[0][0] == 1:
                self.verified_possible_tents_equal_1 = True
                tree = self.trees_possibilities[0][1]
                self.trees_possibilities.extract_min()
                self.put_tent(tree)

    def verifies_tips_equal_possible_tents(self):
        self.verified_tips_equal_possible_tents = False
        if not self.is_wrong:
            for i in range(self.size):
                aux = np.where(self.field[i, :] == POSSIBLE_TENT)[0]
                if self.tips_y_copy[i] == len(aux):
                    self.verified_tips_equal_possible_tents = True
                    for j in aux:
                        self.put_tent((None, None), know_what_tree=False, tent=(i, j))
                        self.tents_put.append((i, j))
            for j in range(self.size):
                aux = np.where(self.field[:, j] == POSSIBLE_TENT)[0]
                if self.tips_x_copy[j] == len(aux):
                    self.verified_tips_equal_possible_tents = True
                    for i in aux:
                        self.put_tent((None, None), know_what_tree=False, tent=(i, j))
                        self.tents_put.append((i, j))
            self.remove_trees()

    def heuristic(self):
        # global counter
        while len(self.possible_tents.heap) > 0 or self.is_wrong:
            # counter += 1
            # print(counter)
            # if counter == 88:
            #     print('e')
            if not self.is_wrong:
                # print(self.tents_game)
                if self.possible_tents.heap[0][0] >= NUM_POSSIBLE_TENTS_TO_PUT_EMPTY:
                    possible_tent = self.possible_tents.heap[0][1]
                    self.save_state(possible_tent, was_tent=False)
                    self.field[possible_tent[0], possible_tent[1]] = EMPTY
                    self.update_maximum_heap(possible_tent[0], possible_tent[1])
                    self.update_minimum_heap(possible_tent[0], possible_tent[1])
                else:
                    possible_tent = self.possible_tents.heap[0][1]
                    self.save_state(possible_tent, was_tent=True)
                    self.put_tent((None, None), know_what_tree=False, tent=possible_tent)
                    self.tents_put.append(possible_tent)
                    self.remove_trees()

                self.verifies_tips_equal_0()
                if len(self.tents_put) == 0:
                    self.do_func(self.verifies_possible_tents_equal_1, self.verifies_tips_equal_0)
                    while self.verified_possible_tents_equal_1:
                        self.do_func(self.verifies_possible_tents_equal_1, self.verifies_tips_equal_0)
                        self.check_is_wrong()
                        if self.is_wrong:
                            break

                self.do_func(self.verifies_tips_equal_possible_tents, self.verifies_tips_equal_0)
                self.check_is_wrong()
                while self.verified_tips_equal_possible_tents or self.verified_possible_tents_equal_1 or self.verified_tips_equal_0:
                    self.do_func(self.verifies_tips_equal_possible_tents, self.verifies_tips_equal_0)
                    self.check_is_wrong()
                    if len(self.tents_put) == 0:
                        self.do_func(self.verifies_possible_tents_equal_1, self.verifies_tips_equal_0)
                        self.check_is_wrong()
                        while self.verified_possible_tents_equal_1:
                            self.do_func(self.verifies_possible_tents_equal_1, self.verifies_tips_equal_0)
                            self.check_is_wrong()
                            if self.is_wrong:
                                break
                    if self.is_wrong:
                        break
                # self.verifies_tips_equal_0()
                # self.verifies_tips_equal_possible_tents()
                # self.check_is_wrong()
                # while self.verified_tips_equal_0 or self.verified_tips_equal_possible_tents:
                #     self.verifies_tips_equal_0()
                #     self.verifies_tips_equal_possible_tents()
                #     self.check_is_wrong()
                #     if self.is_wrong:
                #         break
            else:
                possible_tent, was_tent = self.return_last_state()
                if was_tent:
                    self.field[possible_tent[0], possible_tent[1]] = EMPTY
                    self.update_maximum_heap(possible_tent[0], possible_tent[1])
                    self.update_minimum_heap(possible_tent[0], possible_tent[1])
                else:
                    self.put_tent((None, None), know_what_tree=False, tent=possible_tent)
                    self.tents_put.append(possible_tent)
                    self.remove_trees()

                self.verifies_tips_equal_0()
                if len(self.tents_put) == 0:
                    self.do_func(self.verifies_possible_tents_equal_1, self.verifies_tips_equal_0)
                    while self.verified_possible_tents_equal_1:
                        self.do_func(self.verifies_possible_tents_equal_1, self.verifies_tips_equal_0)
                        self.check_is_wrong()
                        if self.is_wrong:
                            break

                self.do_func(self.verifies_tips_equal_possible_tents, self.verifies_tips_equal_0)
                self.check_is_wrong()
                while self.verified_tips_equal_possible_tents or self.verified_possible_tents_equal_1 or self.verified_tips_equal_0:
                    self.do_func(self.verifies_tips_equal_possible_tents, self.verifies_tips_equal_0)
                    self.check_is_wrong()
                    if len(self.tents_put) == 0:
                        self.do_func(self.verifies_possible_tents_equal_1, self.verifies_tips_equal_0)
                        self.check_is_wrong()
                        while self.verified_possible_tents_equal_1:
                            self.do_func(self.verifies_possible_tents_equal_1, self.verifies_tips_equal_0)
                            self.check_is_wrong()
                            if self.is_wrong:
                                break
                    if self.is_wrong:
                        break
                # self.verifies_tips_equal_0()
                # self.verifies_tips_equal_possible_tents()
                # self.check_is_wrong()
                # while self.verified_tips_equal_0 or self.verified_tips_equal_possible_tents:
                #     self.verifies_tips_equal_0()
                #     self.verifies_tips_equal_possible_tents()
                #     self.check_is_wrong()
                #     if self.is_wrong:
                #         break
        else:
            pass
            # print('Finished heuristic')
            self.remove_trees(True)
            if len(self.trees_possibilities) > 0:
                print(self.tents_game)
                print(self.tents_game.field_copy)


""" Module for token functionality. """

import api
from api.common import InternalException

def get_token_path(token_name):
    """
    Formats the token name into a token path.

    Returns:
      The token path
    """

    return "tokens.{}".format(token_name)

def set_token(key, token_name, token_value=None):
    """
    Sets a token for the user.

    Args:
        key: the unique identifier object
        token_name: the name of the token to set
        token_value: optionally specify the value of the token
    Returns:
        The token value
    """

    db = api.common.get_conn()

    # Should never realistically collide.
    if token_value is None:
        token_value = api.common.hash(str(key) + api.common.token())

    db.tokens.update(
        key, {'$set': {
            get_token_path(token_name): token_value
        }}, upsert=True)

    return token_value

def delete_token(key, token_name):
    """
    Removes the password reset token for the user in mongo

    Args:
        key: the unique identifier object
        token_name: the name of the token
    """

    db = api.common.get_conn()

    db.tokens.update(key, {'$unset': {get_token_path(token_name): ''}})

def find_key(query, multi=False):
    """
    Find a key based on a particular query.

    Args:
        query: the mongo query
        multi: defaults to False, return at most one result
    """

    db = api.common.get_conn()

    find_func = db.tokens.find_one
    if multi:
        find_func = db.tokens.find

    return find_func(query)

def find_key_by_token(token_name, token_value):
    """
    Searches the database for a user with a token_name token_value pair.

    Args:
        token_name: the name of the token
        token_value: the value of the token
    """

    db = api.common.get_conn()

    key = db.tokens.find_one({
        get_token_path(token_name): token_value
    }, {
        "_id": 0,
        "tokens": 0
    })

    if key is None:
        raise InternalException("Could not find {}.".format(token_name))

    return key


# export_subnet_info.py

"""
Exports all info regarding switch subnets and interfaces to a json file
"""

import meraki
import os
import json

from meraki_utils import connect_to_meraki, meraki_error, other_error

API_KEY = os.getenv('MERAKI_API_KEY')

# Connect to Meraki dashboard
dashboard = connect_to_meraki(API_KEY)

subnets = {}

# Attempt to obtain organizations
try:
    organizations = dashboard.organizations.getOrganizations()
except meraki.APIError as e:
    meraki_error(e)
except Exception as e:
    other_error(e)

# Loop through the organizations
for org in organizations:
    # Attempt to obtain all networks in the organization
    try:
        networks = dashboard.organizations.getOrganizationNetworks(organizationId=org['id'])
    except meraki.APIError as e:
        meraki_error(e)
    except Exception as e:
        other_error(e)

    # Drop networks that don't contain switches
    networks = [n for n in networks if 'switch' in n['productTypes']]

    # For each network get all the Meraki devices
    for net in networks:
        try:
            devices = dashboard.networks.getNetworkDevices(networkId=net['id'])
        except meraki.APIError as e:
            meraki_error(e)
        except Exception as e:
            other_error(e)

        # Discard all devices other than switches
        devices = [d for d in devices if 'MS' in d['model']]

        # For each switch get the routing interface
        for device in devices:
            device_subnet = {}
            try:
                current_subnet = dashboard.switch.getDeviceSwitchRoutingInterfaces(serial=device['serial'])
            except meraki.APIError as e:
                meraki_error(e)
            except Exception as e:
               other_error(e)

            # Append list of interfaces to subnet dictionary
            subnets[device['name']] = current_subnet

print(subnets)


#!/usr/bin/env python

##
#
# Set up, solve, and plot the solution for the "reach-avoid"
# scenario, where the robot must reach one of two targets before
# reaching the goal.
#
##

import numpy as np
import matplotlib.pyplot as plt
from stlpy.solvers import *

from stlpy.benchmarks.base import BenchmarkScenario
from stlpy.benchmarks.common import (inside_rectangle_formula,
                     outside_rectangle_formula,
                     make_rectangle_patch)
from stlpy.systems import DoubleIntegrator
from stlpy.benchmarks import RandomMultitarget

# Specification Parameters

time_step = []
number_of_variable = []
total_solve_time_list = []
for i in range(15, 36):
    T = i

    # Create the specification
    scenario = RandomMultitarget(1, 5, 2, T, seed=0)
    spec = scenario.GetSpecification()
    spec.simplify()
    sys = scenario.GetSystem()

    # Specify any additional running cost
    Q = np.diag([0,0,1,1])   # just penalize high velocities
    R = np.eye(2)

    # Initial state
    x0 = np.array([2.0,2.0,0,0])

    # Specify a solution strategy
    #solver = GurobiMICPSolver(spec, sys, x0, T, robustness_cost=True)
    solver = DrakeMICPSolver(spec, sys, x0, T, robustness_cost=True)
    #solver = DrakeSos1Solver(spec, sys, x0, T, robustness_cost=True)

    # Set bounds on state and control variables
    u_min = np.array([-1.0,-1.0])
    u_max = np.array([1.0, 1.0])
    x_min = np.array([0.0, 0.0, -1, -1])
    x_max = np.array([10.0, 10.0, 1, 1])
    solver.AddControlBounds(u_min, u_max)
    solver.AddStateBounds(x_min, x_max)

    x_init=x0
    x_final=np.array([7.5, 8.5, 0, 0])
    # Add quadratic running cost (optional)
    solver.AddQuadraticCost(Q,R)

    # Solve the optimization problem
    x, u, rho, solve_time = solver.Solve()
    tt = solver.mp.num_vars()
    time_step.append(T)
    number_of_variable.append(solver.mp.num_vars())
    total_solve_time_list.append(solve_time)
    if i % 5 == 0:
        np.save("time_test_result/time_step.npy", np.array(time_step))
        np.save("time_test_result/number_of_variable.npy", np.array(number_of_variable))
        np.save("time_test_result/total_solve_time_list.npy", np.array(total_solve_time_list))

np.save("time_test_result/time_step.npy", np.array(time_step))
np.save("time_test_result/number_of_variable.npy", np.array(number_of_variable))
np.save("time_test_result/total_solve_time_list.npy", np.array(total_solve_time_list))


import json
import os

import numpy as np
import pytest
import tensorflow as tf

from .data_loader import DataLoader
from .evaluator import Evaluator
from .model_driver import beam_search
from .model_trainer import ModelTrainer
from .models import *

"""
data_loader
"""
@pytest.fixture(scope='module')
def d_l():
    return DataLoader()

@pytest.fixture(scope='module')
def train_batches(d_l):
    return d_l.get_train_dataset()

@pytest.fixture(scope='module')
def val_batches(d_l):
    return d_l.get_val_dataset()

@pytest.fixture(scope='module')
def test_batches(d_l):
    return d_l.get_test_dataset()

@pytest.mark.parametrize("vocab_size",
                         [pytest.param(3, marks=pytest.mark.xfail(
                             raises=AssertionError)),
                          pytest.param(28040, marks=pytest.mark.xfail(
                              raises=AssertionError)),
                          (4), (10240), (28039)])
@pytest.mark.parametrize("training", [True, False])
def test_data_loader(vocab_size, training):
    dl = DataLoader(vocab_size=vocab_size, training=training)
    assert dl.vocab_size == vocab_size

def test_create_word_list(d_l, tmp_path):
    directory = tmp_path / "sub"
    directory.mkdir()
    path = (directory / "word_list.json").resolve()
    d_l._create_word_list(path)
    with path.open() as p:
        wl1 = json.load(p)
    tokenizer_list = list(d_l.tokenizer.index_word.values())[0:len(wl1) - 1]
    reserve_token = d_l.tokenizer.index_word[0]

    if os.path.exists(d_l.WORDS_PATH):
        with open(d_l.WORDS_PATH) as p:
            wl2 = json.load(p)
        # ensure files written the same
        # ensure words from tokenizer mirror file -- pad at ind 0 not counted
        assert wl1 == wl2
        assert tokenizer_list == wl1[1:]
        assert reserve_token == wl1[0]

    else:
        assert tokenizer_list == wl1[1:]
        assert reserve_token == wl1[0]

def test_dataset_sizes(d_l, train_batches, val_batches, test_batches):
    _TRAIN_SIZE = tf.data.experimental.cardinality(train_batches).numpy()
    _VAL_SIZE = tf.data.experimental.cardinality(val_batches).numpy()
    _TEST_SIZE = tf.data.experimental.cardinality(test_batches).numpy()
    tolerance = 0.001
    assert (_TRAIN_SIZE * d_l.batch_size /
            d_l.DATASET_SIZE) == pytest.approx(d_l.TRAIN_P, abs=tolerance)
    assert (_VAL_SIZE * d_l.batch_size /
            d_l.DATASET_SIZE) == pytest.approx(d_l.VAL_P, abs=tolerance)
    assert (_TEST_SIZE * d_l.batch_size /
            d_l.DATASET_SIZE) == pytest.approx(d_l.TEST_P, abs=tolerance)

def test_preprocess(d_l, test_batches):
    for i, _ in test_batches:
        break
    i = i[0]
    i_pre = d_l.preprocess_input(i)
    i_unp = d_l.unprocess_input(i_pre)
    assert np.all(np.isclose(i.numpy(), i_unp.numpy(), rtol=0.001))
    assert i.shape == i_pre.shape == i_unp.shape
    assert i.shape == (d_l.IMAGE_SIZE, d_l.IMAGE_SIZE, 3)

def test_create_word_embedding_matrix(d_l):
    matrix = d_l.create_word_embedding_matrix()
    assert matrix.shape == (d_l.vocab_size + 1, d_l.WORD_EMBED_SIZE)

def test_batch_convert_to_words(d_l, test_batches):
    for _, s in test_batches:
        break
    refs = d_l.batch_convert_to_words(s, references=True)
    hyp = tf.convert_to_tensor([l[0] for l in s])
    hyps = d_l.batch_convert_to_words(hyp, references=False)
    assert [l[0] for l in refs] == hyps

"""
models
"""

@pytest.fixture
def encoder():
    encoder = create_encoder(tf.keras.applications.xception.Xception)
    return encoder

@pytest.fixture
def word_embeddings(d_l):
    return d_l.create_word_embedding_matrix()

@pytest.mark.parametrize("batch_size", [1, 8])
@pytest.mark.parametrize("units", [1, 64])
@pytest.mark.parametrize("num_pixels", [1, 100])
def test_attention(batch_size, units, num_pixels):
    dec_state_shape = (units)
    embedder_shape = (num_pixels, units)
    test_attention = create_attention(dec_state_shape, embedder_shape, units)
    embedder_rand = tf.random.normal((batch_size, num_pixels, units))
    dec_rand = tf.random.normal((batch_size, units))
    cvec, attw = test_attention([dec_rand, embedder_rand])
    assert cvec.shape == attw.shape == (batch_size, num_pixels)

@pytest.mark.parametrize("batch_size", [1, 8])
@pytest.mark.parametrize("image_size",
                         ['default',
                          pytest.param(100, marks=pytest.mark.xfail()),
                          pytest.param(600, marks=pytest.mark.xfail())])
def test_encoder_no_resize(batch_size, image_size, encoder, d_l):
    if image_size == 'default':
        image_size = d_l.IMAGE_SIZE
    rand_image = tf.random.normal(
        (batch_size, image_size, image_size, 3))

    assert encoder(rand_image).shape == (batch_size, 10, 10, 2048)

@pytest.mark.parametrize("batch_size", [1, 8])
@pytest.mark.parametrize("image_size", ["default", 100, 350])
def test_encoder_resize(batch_size, image_size, encoder, d_l):
    default = d_l.IMAGE_SIZE
    if image_size == 'default':
        image_size = default
    rand_image = tf.random.normal(
        (batch_size, image_size, image_size, 3))
    prep_image = d_l.preprocess_input(
        tf.image.resize(rand_image, (default, default)))
    assert encoder(prep_image).shape == (batch_size, 10, 10, 2048)

@pytest.mark.parametrize("batch_size", [1, 4])
@pytest.mark.parametrize("embed_dim", [16, 64])
def test_embedder(batch_size, embed_dim):
    enc_out_shape = (batch_size, 10, 10, 2048)
    emb_out = (batch_size, enc_out_shape[1] * enc_out_shape[2], embed_dim)
    enc_out_rand = tf.random.normal(enc_out_shape)
    emb = create_embedder(enc_out_shape, embed_dim)
    assert emb(enc_out_rand).shape == emb_out

@pytest.mark.parametrize("batch_size", [4])
@pytest.mark.parametrize("cvec_shape", [100])
@pytest.mark.parametrize("vocab_size", ["default"])
@pytest.mark.parametrize("units", [32, 64])
@pytest.mark.parametrize("with_embeddings", [True, False])
def test_decoder(batch_size, d_l, cvec_shape, vocab_size, units, with_embeddings, word_embeddings):
    if vocab_size == "default":
        vocab_size = d_l.vocab_size
    if with_embeddings:
        dec = create_decoder((cvec_shape), vocab_size + 1, d_l.WORD_EMBED_SIZE,
                             units, pretrained_embeddings=word_embeddings)
    else:
        dec = create_decoder((cvec_shape), vocab_size + 1,
                             d_l.WORD_EMBED_SIZE, units)
    decoder_input = tf.random.normal((batch_size, cvec_shape))
    initial_word = tf.expand_dims(
        [d_l.tokenizer.word_index['<start>']] * batch_size, 1)
    output, state = dec([initial_word, decoder_input])
    assert output.shape == (batch_size, vocab_size + 1)
    assert state.shape == (batch_size, units)

"""
model_trainer
"""

@pytest.fixture(scope='module')
def m_t():
    return ModelTrainer()

def test_train_step(m_t, train_batches):
    for i, s in train_batches:
        break
    loss = m_t._train_step(i, s, 0)
    assert loss > 0

def test_evaluate(m_t, d_l, val_batches):
    for i, s in val_batches:
        break
    sentences, loss = m_t._evaluate_images(i, s)
    assert loss > 0
    assert sentences.shape == (d_l.batch_size, 19)

@pytest.mark.parametrize("batch_size", [2, 4])
def test_scc_loss(m_t, word_embeddings, batch_size):
    cvec_shape = 100
    dec = create_decoder((cvec_shape), m_t.data_loader.vocab_size + 1,
                         m_t.data_loader.WORD_EMBED_SIZE,
                         64, pretrained_embeddings=word_embeddings)
    initial_word = tf.expand_dims(
        [m_t.data_loader.tokenizer.word_index['<start>']] * batch_size, 1)
    masked_word = tf.expand_dims(
        [m_t.data_loader.tokenizer.word_index['<pad>']] * batch_size, 1)
    cvec = tf.random.normal((batch_size, cvec_shape))
    output, state = dec([initial_word, cvec])
    m_output, m_state = dec([masked_word, cvec])
    assert m_t._scc_loss(initial_word, m_output).numpy() != 0
    assert m_t._scc_loss(initial_word, output).numpy() != 0
    assert m_t._scc_loss(masked_word, m_output).numpy() == 0
    assert m_t._scc_loss(masked_word, output).numpy() == 0

@pytest.mark.parametrize("batch_size", [8, 32])
@pytest.mark.parametrize("num_pixels", [100, 121])
def test_dsar_loss(m_t, batch_size, num_pixels):
    x = np.zeros((batch_size, 18, num_pixels))
    for i in range(18):
        att_rand = tf.random.normal((batch_size, num_pixels), seed=32)
        x[:, i, :] = att_rand
    assert m_t._dsar_loss(x).shape == tuple()

"""
evaluator
"""

def test_evaluator(d_l, val_batches):
    x = list(val_batches.take(2))
    references = d_l.batch_convert_to_words(x[0][1])
    hypotheses = d_l.batch_convert_to_words(
        d_l.batch_sample_target(x[0][1]), references=False)
    hypotheses2 = d_l.batch_convert_to_words(
        d_l.batch_sample_target(x[1][1]), references=False)

    ev = Evaluator()
    ev.update_scores(references, hypotheses)

    ones = ev.get_scores()
    ones.pop('BLEU_1')
    cdr1 = ones.pop('CIDEr')
    ones = list(ones.values())
    ev.reset_scores()
    ev.update_scores(references, hypotheses2)

    zeros = ev.get_scores()
    zeros.pop('BLEU_1')
    rouge = zeros.pop('ROUGE')
    zeros = list(zeros.values())
    ev.update_scores(references, hypotheses)

    halves = ev.get_scores()
    halves.pop('BLEU_1')
    cdrh = halves.pop('CIDEr')
    halves = list(halves.values())

    assert np.all(np.isclose(ones, [1] * len(ones), rtol=0.01))
    assert cdr1 > 2

    assert np.all(np.isclose(zeros, [0] * len(zeros), atol=0.2))
    assert rouge < 0.3

    assert np.all(np.isclose(halves, [0.5] * len(halves), rtol=0.35))
    assert cdrh > 1

"""
model_driver
"""

@pytest.mark.parametrize("beam_width", [6])
@pytest.mark.parametrize("redundancy", [0.01, 0.99])
@pytest.mark.parametrize("candidates", [0, 5])
@pytest.mark.parametrize("as_words", [True, False])
def test_beam_search(m_t, test_batches, beam_width, redundancy, candidates, as_words):
    m_t.load_model()
    tk = m_t.data_loader.tokenizer
    vocab_size = m_t.data_loader.vocab_size
    for i, s in test_batches:
        break
    i = tf.squeeze(i[0])
    result = beam_search(i,
                         m_t.ckpt.encoder,
                         m_t.ckpt.embedder,
                         m_t.ckpt.attention,
                         m_t.ckpt.decoder,
                         beam_width=beam_width,
                         redundancy=redundancy,
                         candidates=candidates,
                         as_words=as_words)
    if as_words:
        if candidates:
            assert np.all([[(tk.word_index[w] <=
                             vocab_size) for w in c] for c in result])
        else:
            assert np.all([(tk.word_index[w] <= vocab_size) for w in result])
    else:
        if candidates:
            assert np.all([[(s <= vocab_size) for s in c] for c in result])
        else:
            assert np.all([(s <= vocab_size) for s in result])


from django import forms
from .models import UserAccount

class UserForm(forms.ModelForm):
    class Meta:
        model = UserAccount
        exclude = ('user',)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        placeholder = {
            'first_name': 'First Name',
            'last_name': 'Last Name',
            'email': 'Delivery Email',
            'phone_number': 'Contact Number'
        }

        self.fields['first_name'].widget.attrs['autofocus'] = True
        for field in self.fields:
            self.fields[field].widget.attrs['placeholder'] = f'{placeholder[field]}'
            self.fields[field].widget.attrs['class'] = 'checkout-form-input body-text text-m'
            self.fields[field].label = False


import socket
import sys  # used to implement command lines

# creating socket to connect two different computers
def create_socket():
    # handling any type of error
    try:
        global host
        global port
        global s
        host = ""
        port = 9999
        s = socket.socket()
    # printing the error
    except socket.error as msg:
        print("Socket error " + msg)

# Binding the socket and listening for connection
def bind_socket():
    try:
        global host
        global port
        global s
        print("Binding the socket " + str(port))
        s.bind((host, port))  # binding host and port
        s.listen(5)  # it will listen for 5 times after that it will throw an error if connections not est.

    except socket.error as msg:
        print("Socket error " + msg + "\n" + "retrying...")
        bind_socket()

# establish connection with a client (socket must be listening)
def socket_accept():
    conn, address = s.accept()  # getting an object of connection and ip and port in adrress
    print("The ip and port adrress are " + address[0] + " and " + str(address[1]))
    send_command(conn)  # to make changes in friends computer
    conn.close()

# making changes in friend system
def send_command(conn):
    while True:
        cmd = input()
        if cmd == 'quit':  # to break out from this infinity while loop
            conn.close()
            s.close()
            sys.exit()

        # Data is send from one computer to another in the format of bytes so we have to encode our command in byte
        # format
        if len(str.encode(cmd)) > 0:
            conn.send(str.encode(cmd))
            client_response = str(conn.recv(1024), "utf-8")  # here we are converting back byte to string format
            print(client_response)  # here is used to go to new line

def main():
    create_socket()
    bind_socket()
    socket_accept()

main()


from __future__ import print_function
import sys
import os
try:
    import cdms2
except BaseException:
    print('This test requires cdms2 for I/O')
    sys.exit()

import cmor
import numpy

f = cdms2.open(os.path.join('data/clt.nc'))

pth = os.path.split(os.path.realpath(os.curdir))
if pth[-1] == 'Test':
    ipth = opth = '.'
else:
    ipth = opth = 'Test'
cmor.setup(inpath=ipth,
           set_verbosity=cmor.CMOR_NORMAL,
           netcdf_file_action=cmor.CMOR_REPLACE)

cmor.dataset_json("Test/CMOR_input_example.json")

cmor.load_table("Tables/CMIP6_Amon.json")

s = f("clt", slice(14))
Saxes = s.getAxisList()

axes = []
for ax in Saxes[1:]:
    tmp = cmor.axis(
        ax.id,
        coord_vals=ax[:],
        cell_bounds=ax.getBounds(),
        units=ax.units)
    axes.append(tmp)

# Now creates a dummy HUGE axis for resizing s as really big
factor = 100
nt = s.shape[0] * factor
print('nt is:', nt)
t = numpy.arange(nt)

tmp = cmor.axis(
    'time',
    coord_vals=t,
    units=Saxes[0].units,
    cell_bounds=numpy.arange(
        nt + 1))
axes.insert(0, tmp)
print(axes)
var_id1 = cmor.variable(s.id, s.units, axes)
# the one with 2 at the end is compressed
var_id2 = cmor.variable(s.id, s.units, axes)
sh = list(s.shape)
sh[0] = nt
s = numpy.resize(s, sh)
# s=numpy.where(numpy.greater(s,100.),100,s)
s = numpy.random.random(s.shape) * 10000.
print(s.shape)
cmor.write(var_id1, s)
cmor.close(var_id1)
cmor.write(var_id2, s)

cmor.close()


from typing import List

def intcode(phase_setting, inp, data: List[int], inp_count=0, i=0, pre_out=False) -> tuple:
    out = 0
    pos = 2
    base = 0
    memory = {}
    while not data[i] % 100 == 99:
        t_op = data[i]
        if t_op % 100 == 3:
            ind = data[i + 1] if t_op < 100 else (i + 1) if t_op < 200 else (data[i + 1] + base)
            if ind < len(data):
                data[ind] = inp if inp_count else phase_setting
            else:
                memory[ind] = inp if inp_count else phase_setting
            inp_count += 1
            i += 2
            continue
        elif t_op % 100 == 4:
            index = data[i + 1] if t_op < 100 else (i + 1) if t_op < 200 else (data[i + 1] + base)
            out = data[index] if index < len(data) else get_mem(memory, index)
            pos = 0
            i += 2
            if pre_out:
                print(out)
                #break
            continue
        elif t_op % 100 == 9:
            ind = data[i + 1] if t_op < 100 else (i + 1) if t_op < 200 else (data[i + 1] + base)
            base += data[ind] if ind < len(data) else get_mem(memory, ind)
            i += 2
            continue
        elif t_op % 100 in [5, 6]:
            dat = t_op // 100
            check = t_op % 100
            ind_1 = data[i + 1] if dat == 0 or dat % 10 == 0 else (i + 1) if dat % 10 == 1 else (data[i + 1] + base)
            ind_2 = data[i + 2] if dat == 0 or (dat // 10) % 10 == 0 else (i + 2) if (dat // 10) % 10 == 1 else (data[i + 2] + base)
            el2 = data[ind_2] if ind_2 < len(data) else get_mem(memory, ind_2)
            el1 = data[ind_1] if ind_1 < len(data) else get_mem(memory, ind_1)
            if el1 and check == 5:
                i = el2
            elif not el1 and check == 6:
                i = el2
            else:
                i += 3
            continue
        code, el1, el2, save_pos = data[i:i+4]
        if code > 99:
            opcode = code % 100
            if ((code // 100) % 10) % 2 == 0:
                ind_1 = el1 if (code // 100) % 10 == 0 else (el1 + base)
                el1 = data[el1] if ind_1 < len(data) else get_mem(memory, ind_1)

            if ((code // 1000) % 10) % 2 == 0:
                ind_2 = el2 if (code // 1000) % 10 == 0 else (el2 + base)
                el2 = data[el2] if ind_2 < len(data) else get_mem(memory, ind_2)

            if (code // 10000) % 2 == 0:
                save_pos = save_pos if code // 10000 == 0 else (save_pos + base)
        else:
            opcode = code
            el1 = data[el1] if el1 < len(data) else get_mem(memory, el1)
            el2 = data[el2] if el2 < len(data) else get_mem(memory, el2)
        if opcode in [1, 2]:
            if save_pos < len(data):
                data[save_pos] = (el1 + el2) if opcode == 1 else (el1 * el2)
            else:
                memory[save_pos] = (el1 + el2) if opcode == 1 else (el1 * el2)
        elif opcode in [7, 8]:
            if save_pos < len(data):
                data[save_pos] = int(el1 < el2) if opcode == 7 else int(el1 == el2)
            else:
                memory[save_pos] = int(el1 < el2) if opcode == 7 else int(el1 == el2)
        else:
            break
        i += 4
    return i, out, pos

def get_mem(memory, index):
    if index not in memory.keys():
        memory[index] = 0
    return memory[index]

with open("input.txt", "r") as file:
    inp = [int(x) for x in file.readline().split(",")]
    print("Part 1: ", intcode(phase_setting=1, inp=None, data=inp, pre_out=False)[1])
    print("Part 2: ", intcode(phase_setting=2, inp=None, data=inp, pre_out=False)[1])


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/10/14 下午5:02
# @Author  : jesse
# @File    : handle_request.py

import auth
import json
import requests

class DNSPodApiException(Exception):
    pass

def submit(path,**kwargs):
    login_token = auth.login_token
    base_url = "dnsapi.cn/"
    data_dict = kwargs
    data_dict.update(dict(login_token=login_token,format="json"))
    headers = {
        "Content-type": "application/x-www-form-urlencoded",
        "Accept": "text/json",
        "User-Agent": "dnspod-python/0.01 (jessehuang; DNSPod.CN API v2.8)"
    }
    url = "https://" + base_url + path

    # 因为请求查询一个域名解析绑定时,允许域名不存在的情况.而不会程序报错中断.
    # 在提交非法参数,或执行报错时,返回具体报错原因

    response = requests.post(url, data=data_dict, headers=headers)
    data = response.text
    ret = json.loads(data)
    return ret


from poo_module.src.api.currency_api import DaySummaryAPI
from datetime import datetime

class TestDaySummaryAPI:

    def test_get_data_check_date(self):
        actual = DaySummaryAPI(currency = 'BTC').get_data(datetime(2021, 1, 1)).get('date')
        expected = '2021-01-01'
        assert actual == expected

    def test_get_data(self):
        actual = DaySummaryAPI(currency = 'BTC').get_data(datetime(2021, 1, 1))
        expected = {'date': '2021-01-01', 'opening': 152700.00002, 'closing': 153458.29999999, 'lowest': 151539, 'highest': 153975, 'volume': 12583384.54790148, 'quantity': 82.27265844, 'amount': 4824, 'avg_price': 152947.34346135}
        assert actual == expected


from typing import Dict, List, Callable, Any
from collections import namedtuple
import asyncio

import pytest

from symbios import Symbios
from symbios.utils import Props, ArgumentsType
from symbios.queue import Queue
from symbios.exchange import Exchange
from symbios.message import SendingMessage, IncomingMessage
from symbios.confirmation import QueueACK, EmitACK, ExchangeACK, ListenACK

class MockSymbios:
    '''The Symbios class modifier.

    Modify the Symbios.emit and Symbios.listen methods
    for simulating a basic broker communication.
    '''

    def __init__(self, symbios: Symbios):
        self.symbios: Symbios = symbios
        setattr(self.symbios, '_queue_state', {})

    async def declare_queue(self, *args) -> Any:
        await asyncio.sleep(0.5)
        return 'OK'

    async def emit(
        self,
        message: SendingMessage,
        *,
        exchange: Exchange = Exchange(),
        routing_key: str = None,
        props: Props = Props(),
        mandatory: bool = False,
        immediate: bool = False,
    ) -> None:
        '''Modify the Symbios.emit method.
        '''

        if not routing_key in self.symbios._queue_state.keys():
            self.symbios._queue_state[routing_key] = []

        HeaderModel = namedtuple('HeaderModel', 'properties')
        DeliveredMessageModel = namedtuple(
            'DeliveredMessageModel', 'delivery, header, body'
        )

        self.symbios._queue_state[routing_key].append(
            IncomingMessage(
                DeliveredMessageModel(None, HeaderModel(props), message.body)
            )
        )

        await asyncio.sleep(0.5)

    async def listen(
        self,
        task: Callable[[object, IncomingMessage], None],
        *,
        queue: Queue = Queue(),
        no_ack: bool = False,
        exclusive: bool = False,
        arguments: ArgumentsType = None,
        consumer_tag: str = None,
    ) -> None:
        '''Modify the Symbios.listen method.
        '''

        async def process_listener() -> None:
            while True:
                try:
                    message: IncomingMessage = self.symbios._queue_state[
                        queue.queue
                    ].pop()

                    while message:
                        await task(self, message)
                        message = self.symbios._queue_state[queue.queue].pop()
                except (KeyError, IndexError):
                    pass

                await asyncio.sleep(0.2)

        self.symbios.event_loop.create_task(process_listener())

class MockChannel:
    '''The aiormq Channel mock class.
    '''

    async def queue_declare(self, **kwargs) -> Any:
        '''Just return an ack.
        '''

        return 'OK'

    async def basic_publish(self, *args, **kwargs) -> Any:
        '''Just return an ack.
        '''

        return 'OK'


from flask import Flask, request, jsonify
from werkzeug.utils import secure_filename
import os
import tensorflow as tf
from flask_cors import CORS
import numpy as np

app = Flask(__name__)
CORS(app)

app.config['UPLOAD_FOLDER'] = '../client/public/'
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif'}

category_labels = [
    'paper', 'cardboard', 'plastic', 'metal', 'trash',
    'battery', 'shoes', 'clothes', 'green-glass', 'brown-glass',
    'white-glass', 'biological'
]

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']

def load_model(model_file):
    model = tf.keras.models.load_model(model_file, safe_mode=False)
    return model

model = load_model('model.keras')

@app.route('/classify', methods=['POST'])
def classify_image():
    if 'image' not in request.files:
        app.logger.error('no file')
        return jsonify({"error": "No file part"}), 400

    file = request.files['image']

    if file.filename == '':
        app.logger.error('no selected file')
        return jsonify({"error": "No selected file"}), 400

    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        image_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)

        try:
            image = tf.keras.preprocessing.image.load_img(image_path, target_size=(224, 224))
            image = tf.keras.preprocessing.image.img_to_array(image)
            image = tf.keras.applications.resnet50.preprocess_input(image)
            image = tf.expand_dims(image, axis=0)

            predictions = model.predict(image)

            # Interpret the results
            print(predictions)
            predicted_class_index = np.argmax(predictions)
            predicted_class_label = category_labels[predicted_class_index]

            return jsonify({"type": predicted_class_label, "image": filename})
        except Exception as e:
            return jsonify({"error": str(e)}), 500
    else:
        app.logger.error('illegal file type')
        return jsonify({"error": "File type not allowed"}), 400

app.run(debug=True)


from typing import Union
from .math import convert_axes, AxesFrame
from geometry_msgs.msg import Pose, Transform, Twist, Point, Vector3, PoseStamped, Polygon, Point32
from nav_msgs.msg import Odometry, Path
from .structs import NpVector4, NpPose, NpVector3, NpTwist
import numpy as np

def convert_axes_from_msg(msg: Union[Pose, Transform, Twist, Odometry, Path, Polygon], in_axes: AxesFrame, out_axes: AxesFrame):
    """Converts ROS message coordinate frame."""
    if isinstance(msg, Pose):
        q = NpVector4.from_ros(msg.orientation)
        x, y, z, roll, pitch, yaw = convert_axes(msg.position.x, msg.position.y, msg.position.z, q.roll, q.pitch, q.yaw, in_axes, out_axes)
        return NpPose(NpVector3.from_xyz(x, y, z), NpVector4.from_rpy(roll, pitch, yaw)).get_msg()
    elif isinstance(msg, Transform):
        q = NpVector4.from_ros(msg.rotation)
        x, y, z, roll, pitch, yaw = convert_axes(msg.translation.x, msg.translation.y, msg.translation.z, q.roll, q.pitch, q.yaw, in_axes, out_axes)
        return NpPose(NpVector3.from_xyz(x, y, z), NpVector4.from_rpy(roll, pitch, yaw)).get_tf_msg()
    elif isinstance(msg, Twist):
        x, y, z, roll, pitch, yaw = convert_axes(msg.linear.x, msg.linear.y, msg.linear.z, msg.angular.x, msg.angular.y, msg.angular.z, in_axes, out_axes)
        return NpTwist(NpVector3.from_xyz(x, y, z), NpVector3.from_xyz(roll, pitch, yaw)).get_msg()
    elif isinstance(msg, Odometry):
        out_msg = Odometry()
        pose_msg = msg.pose.pose
        twist_msg = msg.twist.twist
        q = NpVector4.from_ros(pose_msg.orientation)
        x, y, z, roll, pitch, yaw = convert_axes(pose_msg.position.x, pose_msg.position.y, pose_msg.position.z, q.roll, q.pitch, q.yaw, in_axes, out_axes)
        out_msg.pose.pose = NpPose(NpVector3.from_xyz(x, y, z), NpVector4.from_rpy(roll, pitch, yaw)).get_msg()
        x, y, z, roll, pitch, yaw = convert_axes(twist_msg.linear.x, twist_msg.linear.y, twist_msg.linear.z, twist_msg.angular.x, twist_msg.angular.y, twist_msg.angular.z, in_axes, out_axes)
        out_msg.twist.twist = NpTwist(NpVector3.from_xyz(x, y, z), NpVector3.from_xyz(roll, pitch, yaw)).get_msg()
        return out_msg
    elif isinstance(msg, Path):
        out_msg = Path()
        out_msg.header = msg.header
        for pose in msg.poses:
            p = pose.pose
            q = NpVector4.from_ros(p.orientation)
            x, y, z, roll, pitch, yaw = convert_axes(p.position.x, p.position.y, p.position.z, q.roll, q.pitch, q.yaw, in_axes, out_axes)
            o = PoseStamped()
            o.header = pose.header
            o.pose = NpPose(NpVector3.from_xyz(x, y, z), NpVector4.from_rpy(roll, pitch, yaw)).get_msg()
            out_msg.poses.append(o)
        return out_msg
    elif isinstance(msg, Polygon):
        out_msg = Polygon()
        for p in msg.points:
            x, y, z, _, _, _ = convert_axes(p.x, p.y, p.z, 0, 0, 0, in_axes, out_axes)
            o = Point32(x=x, y=y, z=z)
            out_msg.points.append(o)
        return out_msg
    else:
        raise ValueError(f"ROS message type {type(msg)} is not supported")

def msg_contains_nan(msg: Union[Point, Vector3]):
    """Checks if ROS message contains any nans."""
    if isinstance(msg, (Point, Vector3)):
        return np.isnan(msg.x) or np.isnan(msg.y) or np.isnan(msg.z)
    else:
        raise ValueError(f"ROS message type {type(msg)} is not supported")


#!/usr/bin/env python3
import rospy
import std_msgs.msg
import sensor_msgs.msg
from pympler.asizeof import asizeof
import datetime

pc2_pub = rospy.Publisher('/sampling_pc2', sensor_msgs.msg.PointCloud2, queue_size=10)
ci_pub = rospy.Publisher('/sampling_ci', sensor_msgs.msg.CompressedImage, queue_size=10)
f_flag = 0

def PC_callback(data):
    global f_flag

    # timestamp를 datetime 객체로 변환
    dt = datetime.datetime.fromtimestamp((data.header.stamp).to_sec())

    # datetime 객체에서 시간 정보 추출
    minute = dt.minute
    second = dt.second
    ms = dt.microsecond
    # print( sep=',')
    # timestamp = datetime.fromtimestamp(data.header.stamp)
    # # # 1s = 14 frame 여기는 10frame == 1초
    # 1 : 5/10  2,3 : 3/10   4, 5 : 2/10  6, 7 : 1/10  8, 9 : 1/10
    # 1초에 9번이라고 생각하면 됨
    if f_flag < 81:
        f_flag += 1
    else:
        pc2_pub.publish(data)
        print(minute, second, str(asizeof(data)), data.point_step, data.row_step, sep=',')
        f_flag = 0

def CI_callback(data):
    global f_flag

    if f_flag < 3:
        f_flag += 1
    else:
        ci_pub.publish(data)
        f_flag = 0

if __name__ == '__main__':
    while not rospy.is_shutdown():
        rospy.init_node("Sampling_pc2")

        # Pointcloud2
        rospy.Subscriber('/velodyne_points', sensor_msgs.msg.PointCloud2, PC_callback)

        # compressedImage
        # rospy.Subscriber('/zed2/zed_node/left/image_rect_color/compressed', sensor_msgs.msg.CompressedImage, CI_callback)
        rospy.spin()

# import rospy
# import std_msgs.msg
# import sensor_msgs.msg
# import time
#
# # rospy.Rate : 지정된 속도로 루프에서 잠을 잘 수 있는 편의 클래스
# # rospy.Timer : 지정된 속도로 콜백을 호출하기 위한 편의 클래스
#
# pc2_pub = rospy.Publisher('/sampling_pc2', sensor_msgs.msg.PointCloud2, queue_size=10)
# ci_pub = rospy.Publisher('/sampling_ci', sensor_msgs.msg.CompressedImage, queue_size=10)
#
# class Timer():
#     def __init__(self):
#         self.std = time.time()
#         self.sec = 0.1
#         self.hz = 1/self.sec
#     def check(self):
#         result = time.time() - self.std
#         if (result >= self.sec):
#             self.std = time.time()
#             return True
#         else:
#             return False
#
# timer = Timer()
#
# def PC_callback(data):
#     global timer
#     status = timer.check()
#     if(status):
#         pc2_pub.publish(data)
#         print(data)
#
# # def CI_callback(data):
# #     global timer
# #     status = timer.check()
# #     if (status):
# #         ci_pub.publish(data)
#
# if __name__ == '__main__':
#     while not rospy.is_shutdown():
#         rospy.init_node("Sampling_pc2")
#         # Pointcloud2
#         rospy.Subscriber('/velodyne_points', sensor_msgs.msg.PointCloud2, PC_callback)
#         # compressedImage
#         # rospy.Subscriber('/zed2/zed_node/left/image_rect_color/compressed', sensor_msgs.msg.CompressedImage, CI_callback)
#         rospy.spin()


# https://www.hackerrank.com/challenges/defaultdict-tutorial/problem?isFullScreen=true

# from collections import defaultdict

# n, m = list(map(int, input().split()))

# d = defaultdict(list)
# for i in range(n):
#     d['A'].append(input())

# for i in range(m):
#     d['B'].append(input())

# https://www.hackerrank.com/challenges/defaultdict-tutorial/forum

from collections import defaultdict

# n, m = map(int, input().split())

# A = []
# for i in range(n):
#     A.append(input())
# print("A: ", A)

# B = []
# for i in range(m):
#     B.append(input())
# print("B: ", B)

# d = defaultdict(list)

# print("Default List: ", d)

# for i in B:
#     if i not in d:
#         if i not in A:
#             d.append[-1]
#         else:
#             for j in range(n):
#                 if A[j] == i:
#                     d[i].append(j+1)
#     print(*d[i])
# print(d)

n = int(input())
d = dict(list())

for i in range(1, n+1):
    v = input()

    if v in d:
        d[v].append(i)
    else:
        d[v]=[i]
# d.[v].append(i) if we use defaultdict

for key, val in d.items():
    print(key, val)

# for i in B:
#     if i not in d: # do not append again
#         if i not in A:
#             d[i].append(-1)

#         else:
#             for j in range(0,n):
#                 if A[j] == i:
#                     d[i].append(j+1)

#     print(*d[i])


import tkinter as tk
from datetime import datetime

bancodados = "bancodados.txt"
statusinstr = "status.txt"
descricaoinstr = "Instrumentos.txt"

def novo_instrumento(codigo, descricao, statusinic, reinic):
    agora = datetime.now()

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.codigo_produto = tk.Entry(self, width=20, justify='center', xscrollcommand=1)
        self.descricaocodigo = tk.Entry(self, width=100)
        self.reinic = tk.Entry(self, width=9, justify='center', xscrollcommand=1)
        self.statusinic = tk.Entry(self, width=2, justify='center', xscrollcommand=1)
        self.novoinstrumento = tk.Button(self)
        self.lbcodigo = tk.Label(self)
        self.lbcodigo = tk.Label(self)
        self.lbdescricao = tk.Label(self)
        self.lbstatus = tk.Label(self)
        self.lbre = tk.Label(self)
        self.quit = tk.Button(self, text="Fechar", fg="red",
                              command=self.master.destroy)
        self.master = master
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.novoinstrumento["text"] = "Cadastrar novo\n instrumento"
        self.codigo_produto.grid(row=1, column=0, padx=20)
        self.descricaocodigo.grid(row=1, column=1)
        self.lbcodigo["text"] = "Código do instrumento"
        self.lbcodigo.grid(row=0, column=0)
        self.lbdescricao["text"] = "Descrição"
        self.lbdescricao.grid(row=0, column=1, pady=22)
        self.lbstatus["text"] = "Primeiro status"
        self.lbre["text"] = "R.E."
        self.reinic.grid(row=1, column=3, padx=20)
        self.lbstatus.grid(row=0, column=4)
        self.statusinic .grid(row=1, column=4, padx=20)
        self.lbre.grid(row=0, column=3)
        self.novoinstrumento.grid(row=10, column=0, pady=22)
        self.quit.grid(row=10, column=2)

root = tk.Tk()
root.title('Cadastro de Instrumentos')
root.geometry("1200x300")
app = Application(master=root)
app.mainloop()


from django.core.management.base import BaseCommand
from django.conf import settings
from asset.models import AssetStatus
import requests
import pprint
import logging
logger = logging.getLogger('django')

# BaseCommandを継承して作成
class Command(BaseCommand):
    # python manage.py help count_entryで表示されるメッセージ
    help = 'Get AssetStatus data'

    # コマンドが実行された際に呼ばれるメソッド
    def handle(self, *args, **options):
        url = "https://www.fk-management.com/drm/web/assetstatus/?limit=100"
        data_list = list()
        error_list = list()
        while True:
            r = requests.get(url)
            json_data = r.json()
            self.stdout.write(self.style.SUCCESS("AssetStatus: {}".format(json_data['count'])))
            for r in json_data['results']:
                try:
                    self.stdout.write("============")
                    self.stdout.write("------response------")
                    pprint.pprint(r)
                    # prepare dict
                    self.stdout.write("------data------")
                    d = {
                        "legacy_id": r["id"],
                        "date": r["date"],
                        "buying_power": r["buying_power"],
                        "investment": r["investment"],
                        "nisa_power": r["nisa_power"],
                        "sum_stock": r["sum_stock"],
                        "sum_trust": r["sum_trust"],
                        "sum_other": r["sum_other"],
                    }
                    pprint.pprint(d)
                    # tranlate dict into AssetStatus
                    self.stdout.write("------AssetStatus------")
                    if not AssetStatus.objects.filter(legacy_id=r['id']).exists():
                        s = AssetStatus(**d)
                        # Add
                        data_list.append(s)
                        pprint.pprint(s)
                    else:
                        self.stdout.write(f"AssetStatus of legacy_id={r['id']} exists")
                        continue
                except Exception as e:
                    error_list.append({"msg": e, "data": r})
                    self.stderr.write(str(e))
            if not json_data['next']:
                self.stdout.write(
                    ("=================={}/{}====================".format(len(data_list), json_data['count']))
                )
                break
            url = json_data['next']
        # Print Error List
        if error_list:
            self.stdout.write("====================")
            pprint.pprint(error_list)
        # Bulk Create
        AssetStatus.objects.bulk_create(data_list)

# {
#     "id": 1778,
#     "date": "2020-02-21",
#     "buying_power": 72326,
#     "investment": 2300000,
#     "nisa_power": 1000000,
#     "sum_stock": 1358200,
#     "sum_trust": 776267,
#     "sum_other": 0,
#     "user": 1
# }


from flask import Flask, request, jsonify, render_template, url_for, redirect, Markup, session
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime, timedelta
import pytz
import os

app = Flask(__name__)
db_uri = os.environ.get("DATABASE_URL") or "sqlite:///" + os.path.join(app.root_path, "board.db")
if db_uri.startswith("postgres://"):
    db_uri = db_uri.replace("postgres://", "postgresql://", 1)
app.config["SQLALCHEMY_DATABASE_URI"] = db_uri
db = SQLAlchemy(app)
app.secret_key = "abcdefghijklmn"
app.permanent_session_lifetime = timedelta(days=1)

class Board(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    body = db.Column(db.String(500), nullable=False)
    created_at = db.Column(db.DateTime, nullable=False, default=datetime.now(pytz.timezone('Asia/Tokyo')))

@app.route('/')
def index():
    return render_template("index.html")

@app.route("/board/", methods=["POST"])
def post_board():
    name = request.form["name"]
    body = request.form["body"]
    if name != "" and body != "":
        session.permanent = True
        session["name"] = name
        body = body.replace('\n', '<br>')
        time = datetime.now(pytz.timezone('Asia/Tokyo'))
        b = Board(name=name, body=body, created_at=time)
        db.session.add(b)
        db.session.commit()
    return redirect(url_for("board"))

@app.route("/board/", methods=["GET"])
def board():
    boards = Board.query.all()
    boards = reversed(list(boards))
    if "name" in session:
        return render_template("board.html", boards=boards, name=session["name"])
    else:
        return render_template("board.html", boards=boards, name="")

if __name__ == "__main__":
    app.run()


# from pprint import pprint
# import sys
# sys.stdin = open("swea_4466_input.txt", 'rt', encoding='UTF8')

for tc in range(1, int(input())+1):
    n, k = map(int, input().split())
    scores = list(map(int, input().split()))
    scores.sort(reverse=True)
    answer = 0
    for i in range(k):
        answer += scores[i]
    print(f'#{tc} {answer}')


import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import math
import os

import time

def sigmoid(z):
    return 1/(1+np.exp(-z))

def derivative_of_sigmoid(z):
    return np.exp(z)/((np.exp(z) + 1)**2)

def L2_loss(real, predicted):
    return (real - predicted)**2

class Layer:
    def __init__(self,num_neurons, input_length):
        self.num_neurons = num_neurons
        self.input_length = input_length

        # randomly initialize neuron weights

        self.weights = np.random.rand(num_neurons, input_length)
        self.biases = np.random.rand(num_neurons)

    def forward(self,x):
        return sigmoid(np.dot(self.weights, x) + self.biases)

class NeuralNetwork:
    def __init__(self,input_length):

        self.hidden_layer = Layer(3 ,input_length)
        self.output_layer = Layer(1,3)

        self.lr = 1

        self.layers = [
                self.hidden_layer,
                self.output_layer
            ]

    def forward(self,x):
        output_vectors = []

        for layer in self.layers:
            # if the first layer
            if not output_vectors:
                # pass it the input vector
                output = layer.forward(x)
            else:
                # otherwise pass it the previous layer's output
                output = layer.forward(output_vectors[-1])

            output_vectors.append(output)

        return output_vectors

    def backpropagate(self, output, y):

        E1 = output[2] - y
        dW2 = E1 * output[2] * (1-output[2])

        #print("E1: " + str(E1.shape))
        #print("dW2: " + str(dW2.shape))

        E2 = np.dot(np.reshape(dW2, (1,1)), self.output_layer.weights)
        dW1 = E2 * output[1] * (1-output[1])

        #print("E2: "+str(E2.shape))

        #print("dW1: "+str(dW1.shape))

        W2_update = np.dot(np.reshape(output[1], (3,1)), np.reshape(dW2, (1,1)))
        W1_update = np.dot(np.reshape(output[0], (2,1)), dW1)

        #print("W2_update: "+str(W2_update.shape))
        #print("W1_update: "+str(W1_update.shape))

        self.output_layer.biases = self.output_layer.biases - self.lr * dW2
        self.output_layer.weights = self.output_layer.weights - self.lr * W2_update.T
        #print("before "+str(self.hidden_layer.biases.shape))
        self.hidden_layer.biases = self.output_layer.biases - self.lr * np.reshape(dW1,(3,))
        self.hidden_layer.weights = self.hidden_layer.weights - self.lr * W1_update.T
        #print("after "+str(self.hidden_layer.biases.shape))

        """
        # this outputs a scalar
        output_delta = (output[2] - y) * output[2]* (1-output[2])
        # multiply the delta times the input to produce a 3-vector

        output_gradient = -np.matmul(
            np.reshape(output_delta, (1,1)),
            np.reshape(output[1], (1,3)))

        # this outputs a 3-vector
        hidden_delta =  np.matmul(output_delta, self.output_layer.weights) * output[1] * (1-output[1])

        # this returns a (3,2) matrix
        hidden_gradient = np.matmul(
            np.reshape(hidden_delta, (3,1)),
            np.reshape(output[0], (1,2)))

        # update the output bias
        self.output_layer.biases += -output_delta * self.lr
        # update the output weights
        self.output_layer.weights += -output_gradient * self.lr

        # update the hidden layer biases
        self.hidden_layer.biases += -hidden_delta * self.lr
        # update the hidden layer weights
        self.hidden_layer.weights += -hidden_gradient * self.lr
        """

input_length = 2

nn = NeuralNetwork(input_length)

data = [([0, 0], 0), ([0, 1], 1), ([1, 0], 1), ([1, 1], 0)]

def train(epoch):

    image_pair = ["",""]

    image_class = 0

    image_index = 0
    for input_vector, class_label in data:

        input_vector = np.array(input_vector)
        output_vectors = nn.forward(input_vector)
        output_vectors.insert(0,input_vector)

        prediction = output_vectors[-1]

        error = L2_loss(image_class, prediction)

        nn.backpropagate(output_vectors, class_label)

        prediction = nn.forward(input_vector)[-1]

        error = L2_loss(image_class, prediction)

        if epoch % 20000 == 0:
            print(str(input_vector) +" : " + str(class_label) + " : "+ str(prediction[0]))

epochs = 200000
for epoch in range(epochs):
    #print("\n")
    train(epoch)


import numpy as np
import timefuncs as tm
import re
import time

fuidpid = []
fwp = []
frp = []
basedirdepth = 0

def fixWpRpList( ):
   global fwp, frp
   if bool(fwp):
      if fwp[0] == -1:
         fwp = 0
      if fwp[1] == -1:
         fwp = time.time()
   if bool(frp):
      if frp[0] == -1:
         frp = 0
      if frp[1] == -1:
         frp = time.time()

def largestMatch( x, y ):
   minlxy = min(len(x),len(y))
   if minlxy == 0:
      return y
   for i in range(minlxy):
      if x[i] != y[i]:
         return y[:i]
   return x[:minlxy]

def parseNfill( fl ):
   """
   Core function scheduled / map using multiprocessing module
   """
   import sys
   global activefunc, fuidpid, fwp, frp
   ref = {}
   with open(fl, encoding = "ISO-8859-1") as fh:
      l = 0
      for line in fh:
         tmp = line.split('\x1E')
         try:
            size = int(tmp[0]); uid = int(tmp[1]); mtime = int(tmp[2]);
            atime = int(tmp[3]);
            if len(tmp) == 8:
               proj = int(tmp[4])
               fname = tmp[5]; path = tmp[6];
            else:
               proj = 0
               fname = tmp[4]; path = tmp[5];
            path = re.sub('/{2,}', '/', path)
            if atime < mtime:
               atime = mtime
            if bool(fwp) and (mtime < fwp[0] or mtime > fwp[1]):
               continue
            if bool(frp) and (atime < frp[0] or atime > frp[1]):
               continue
            if not bool(fuidpid) or uid in fuidpid or proj in fuidpid:
               activefunc( ref, size, uid, mtime, atime, proj, fname, path )
         except:
            print("Discarded line:",l,"from: ",fl,"line: ",tmp, file=sys.stderr)
         l += 1
   return ref

def crEntry( ):
   """
   returns an initialized empty row
   """
   return {'size': 0.0, 'count': 0,
           'wHist': np.zeros(MAXHBINS),
           'rHist': np.zeros(MAXHBINS),
           'rMinw': np.zeros(MAXHBINS)}

def fillData( ref, size, uid, mtime, atime, proj, fname, path ):
   """
   """
   if uid not in ref:
      ref[uid] = crEntry( )
   entry = ref[uid]
   entry['size'] += size
   entry['count'] += 1
   whidx = tm.tsToYrMnIdx( mtime )
   rhidx = tm.tsToYrMnIdx( atime )
   entry['wHist'][whidx] += size
   entry['rHist'][rhidx] += size
   entry['rMinw'][rhidx-whidx] += size

def dataByUids( ref, size, uid, mtime, atime, proj, fname, path ):
   """
   Function for storing rows per uid
   """
   fillData( ref, size, uid, mtime, atime, proj, fname, path )

def dataByProjs( ref, size, uid, mtime, atime, proj, fname, path ):
   """
   Function for storing rows per projects
   """
   fillData( ref, size, proj, mtime, atime, proj, fname, path )

def dataBySubDirs( ref, size, uid, mtime, atime, proj, fname, path ):
   """
   Function for storing rows per subdirs
   """
   paths = path.split('/')
   if len(paths) <= gufitreedepth:
      fillkey = '.'
   else:
      fillkey = paths[gufitreedepth]
   fillData( ref, size, fillkey, mtime, atime, proj, fname, path )

def conCatDataByKey( resfromtasks ):
   """
   Gathers all the data from all multiprocessing tasks and sums those
   to that of task-0
   """
   totres = resfromtasks[0]
   for ent in resfromtasks[1:]:
      if len(ent) > 0:
         for key in ent.keys():
            entuid = ent[key]
            if key in totres:
               tot = totres[key]
               for attr in tot.keys():
                  tot[attr] += entuid[attr]
            else:
               totres[key] = entuid
   totrow = crEntry( )
   for key in totres.keys():
      tot = totres[key]
      for attr in tot.keys():
          totrow[attr] += tot[attr]
   return totres, totrow

def getDirDepth( path ):
   """
   Returns depth of path
   """
   path = re.sub('/{2,}', '/', path)
   if path.endswith('/'):
      path = path[:-1]
   return len(path.split('/'))

def getDataByFields(gufitree, np, databyfields, cfiles ):
   from multiprocessing import Pool
   global activefunc, gufitreedepth
   gufitreedepth = getDirDepth( gufitree )
   activefunc = databyfields
   p = Pool(np)
   resfromtasks = p.map(parseNfill, cfiles)
   p.close()
   p.join()
   return conCatDataByKey( resfromtasks )


import sys
import time
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QMainWindow, QMessageBox, QApplication
import serial
import time
import numpy as np
from pylab import *
import matplotlib.pyplot as plt
from datetime import datetime
import os
from PIL import Image

# ser=serial.Serial()
# ser.baudrate = 57600
# ser.port = 'COM5'
# ser.timeout = 1

class UI_MainWindow(QtWidgets.QWidget):

    def __init__(self) -> None:
        super(UI_MainWindow, self).__init__()

        self.measure = Measurements()
        self.measure.measurement_sig.connect(self.change_ui)

        # self.measure.start()

        self.setObjectName("mainWindow")
        self.resize(800,600)

        horizontal_layout1 = QtWidgets.QHBoxLayout()
        horizontal_layout1.setObjectName("horizontal_layout1")

        vertical_layout1 = QtWidgets.QVBoxLayout()
        vertical_layout1.setObjectName("vertical_layout1")

        vertical_layout2 = QtWidgets.QVBoxLayout()
        vertical_layout2.setObjectName("vertical_layout2")

        operator = QtWidgets.QVBoxLayout()
        operator.setObjectName("operator")
        self.operator_label = QtWidgets.QLabel()
        self.operator_label.setObjectName("operator_label")
        operator.addWidget(self.operator_label)
        self.operator_edit = QtWidgets.QLineEdit()
        self.operator_edit.setObjectName("operator_edit")
        operator.addWidget(self.operator_edit)
        vertical_layout1.addLayout(operator)

        sample = QtWidgets.QVBoxLayout()
        sample.setObjectName("sample")
        self.sample_label = QtWidgets.QLabel()
        self.sample_label.setObjectName("sample_label")
        sample.addWidget(self.sample_label)
        self.sample_edit = QtWidgets.QLineEdit()
        self.sample_edit.setObjectName("sample_edit")
        sample.addWidget(self.sample_edit)
        vertical_layout1.addLayout(sample)

        filepath = QtWidgets.QVBoxLayout()
        filepath.setObjectName("filepath")
        self.filepath_label = QtWidgets.QLabel()
        self.filepath_label.setObjectName("filepath_label")
        filepath.addWidget(self.filepath_label)
        self.filepath_edit = QtWidgets.QLineEdit()
        self.filepath_edit.setObjectName("filepath_edit")
        filepath.addWidget(self.filepath_edit)
        vertical_layout1.addLayout(filepath)

        btn_measure = QtWidgets.QPushButton()
        btn_measure.setEnabled(True)
        btn_measure.setObjectName("btn_measure")
        btn_measure.clicked.connect(self.click)
        vertical_layout1.addWidget(btn_measure)

        sample_measured = QtWidgets.QVBoxLayout()
        sample_measured.setObjectName("sample_measured")
        self.sample_measuredLabel = QtWidgets.QLabel()
        self.sample_measuredLabel.setObjectName("sample_measuredLabel")
        sample_measured.addWidget(self.sample_measuredLabel)
        self.sample_measuredText = QtWidgets.QLabel()
        self.sample_measuredText.setObjectName("sample_measuredText")
        sample_measured.addWidget(self.sample_measuredText)
        vertical_layout2.addLayout(sample_measured)

        rs_measured = QtWidgets.QVBoxLayout()
        rs_measured.setObjectName("rs_measured")
        self.rs_measuredLabel = QtWidgets.QLabel()
        self.rs_measuredLabel.setObjectName("rs_measuredLabel")
        rs_measured.addWidget(self.rs_measuredLabel)
        self.rs_measuredText = QtWidgets.QLabel()
        self.rs_measuredText.setObjectName("rs_measuredText")
        rs_measured.addWidget(self.rs_measuredText)
        vertical_layout2.addLayout(rs_measured)

        rsh_measured = QtWidgets.QVBoxLayout()
        rsh_measured.setObjectName("rsh_measured")
        self.rsh_measuredLabel = QtWidgets.QLabel()
        self.rsh_measuredLabel.setObjectName("rsh_measuredLabel")
        rsh_measured.addWidget(self.rsh_measuredLabel)
        self.rsh_measuredText = QtWidgets.QLabel()
        self.rsh_measuredText.setObjectName("rsh_measuredText")
        rsh_measured.addWidget(self.rsh_measuredText)
        vertical_layout2.addLayout(rsh_measured)

        div = QtWidgets.QVBoxLayout()
        self.div_graph = QtWidgets.QLabel()
        # self.div_graph_pix = QtGui.QPixmap('C:/Users/eso/Documents/Python Scripts/Python Project DIV/Test')
        self.div_graph_pix = QtGui.QPixmap(None)
        self.div_graph.resize(688, 473)
        self.div_graph.setPixmap(self.div_graph_pix)
        self.div_graph.setObjectName("div_graph")
        div.addWidget(self.div_graph)
        vertical_layout2.addLayout(div)

        horizontal_layout1.addLayout(vertical_layout1)
        horizontal_layout1.addLayout(vertical_layout2)

        self.setLayout(horizontal_layout1)

        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("mainWindow", "DIV Testing Window"))
        self.operator_label.setText(_translate("mainWindow", "Operator:"))
        self.sample_label.setText(_translate("mainWindow", "Sample:"))
        self.filepath_label.setText(_translate("mainWindow", "Filename:"))
        btn_measure.setText(_translate("mainWindow", "Measure"))
        self.sample_measuredLabel.setText(_translate("mainWindow", "Measured Sample: "))
        self.sample_measuredText.setText(_translate("mainWindow", "Waiting for Measurement Data"))
        self.rs_measuredLabel.setText(_translate("mainWindow", "Measured Rs: "))
        self.rs_measuredText.setText(_translate("mainWindow", "Waiting for Measurement Data"))
        self.rsh_measuredLabel.setText(_translate("mainWindow", "Measured Rsh: "))
        self.rsh_measuredText.setText(_translate("mainWindow", "Waiting for Measurement Data"))
        self.show()

    def change_ui(self, measurements: str) -> None:
        _translate = QtCore.QCoreApplication.translate
        print(y)
        self.rs_measuredText.setText(_translate("mainWindow", measurements))
        self.sample_measuredText.setText(_translate("mainWindow", "Waiting for Measurement Data"))

    def click(self) ->None :
        print(n)
        self.measure.start()

class Measurements(QThread):

    measurement_sig = pyqtSignal(str)

    def __init__(self) -> None:
        super(Measurements, self).__init__()

    def take_measurement(self) -> float:

        rs_min = 2.5
        return rs_min

    def run(self) -> None:
        print(y)
        rs_min = str(self.take_measurement())
        print(yes1)
        self.measurement_sig.emit(rs_min)
        print(yes2)
        time.sleep(2)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = UI_MainWindow()
    sys.exit(app.exec_())


'''
Benchmarking pandas empty vs len nv len on index for empty dataframe
'''

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10000, 4), columns=list('ABCD'))

def empty(df):
    if df.empty:
        a=0
    else:
        a=1
    return a

def lenz(df):
    if len(df)==0:
        a=0
    else:
        a=1
    return a

def lenzi(df):
    if len(df.index)==0:
        a=0
    else:
        a=1
    return a

'''
%timeit empty(df)
%timeit lenz(df)
%timeit lenzi(df)

10000 loops, best of 3: 13.9 µs per loop
100000 loops, best of 3: 2.34 µs per loop
1000000 loops, best of 3: 695 ns per loop

len on index seems to be the best
'''