import datetime

from django.contrib.auth.models import User
from django.db import connection
from elasticsearch import Elasticsearch
from knox.models import AuthToken
from rest_framework import permissions, generics
from rest_framework.response import Response
from rest_framework.views import APIView

from .models import BlogPost as BlogPostModel
from .models import BlogPostTag
from .models import KnowledgeBaseItem
from .models import KnowledgeBaseTag
from .models import Tool
from .models import BlogPostComment
from django.contrib.auth.models import User
from .serializers import CreateUserSerializer, UserSerializer, LoginUserSerializer, CommentSerializer

es = Elasticsearch(["http://elastic:9200"])

def article_index_payload_builder(blogpost_db_id, blogpost_data, author_first_name, created_date, type):
    data = {
        "id": blogpost_db_id,
        "title": blogpost_data["title"],
        "content": blogpost_data["content"],
        "type": type,
        "author": author_first_name,
        "createdDate": created_date,
        "tags": blogpost_data["tags"]
    }
    return data

class Blogs(APIView):
    def get(self, request, pk):
        try:
            blogs = BlogPostModel.objects.get(id=pk)
        except:
            return Response(status=404, data={"message": "Could't find your post."})

        values = blogs.__dict__
        values["tags"] = []
        values["createdByDisplayName"] = blogs.createdBy.first_name + ' ' + blogs.createdBy.last_name
        values["type"] = "blog_post"
        values["createdById"] = blogs.createdBy.id
        del values["createdBy_id"]

        for tag in blogs.tags.all():
            values["tags"].append(tag.tag)

        del values['_state']

        return Response(status=200, data=values)

    def delete(self, request, pk):
        try:
            blog = BlogPostModel.objects.get(id=pk)
        except:
            return Response(status=404, data={"message": "The post you're trying to delete "
                                                         "may have already been deleted. "
                                                         "Can't find it here."})
        blog.delete()

        es.delete_by_query(index='knowledge_base', body=
        {
            "query": {
                "match": {
                    "id": pk,
                }
            }
        })
        return Response(status=200, data={"message": "Post deleted"})

    def put(self, request, pk):

        """
         {
            "title": "Pavan first blogpost",
            "content": "Content for the blogpost blah blah blah blach",
            "authorId": "1",
            "tags": ["happiness", "joy"],
            "type": "blog_post"
        }
        """

        request_data = request.data
        if not BlogPostModel.objects.filter(id=pk).exists():

            data = {'message': "This blogpost does not exist"}
            return Response(data=data, status=404)
        else:
            user = User.objects.get(id=request_data["authorId"])
            blogpost_db = BlogPostModel.objects.get(id=pk)
            blogpost_db.title = request_data["title"]
            blogpost_db.content = request_data["content"]

            add_tags_blogpost(blogpost_db, request_data["tags"])

            blogpost_db.save()

            # Delete the old index in elasticsearch and reindex
            es.delete_by_query(index='knowledge_base', body=
            {
                "query": {
                    "match": {
                        "id": pk,
                    }
                }
            })

            # Re-index in elasticsearch
            blogpost_es = article_index_payload_builder(blogpost_db.id, request_data, user.first_name,
                                                        str(datetime.datetime), "blog_post")
            es.index(index='knowledge_base', body=blogpost_es)

        return Response(status=200, data={"message": "Successfully edited blogpost",
                                          "id": blogpost_db.id,
                                          "type": "blog_post"})

def blogmodelToDict(blogs):
    values = list(blogs.values())

    for idx, blog in enumerate(blogs):
        values[idx]["tags"] = []
        values[idx]["createdByDisplayName"] = blog.createdBy.first_name + ' ' + blog.createdBy.last_name
        values[idx]["type"] = "blog_post"
        values[idx]["commentCount"] = BlogPostComment.objects.filter(blog_post_id=blog.id).count()
        for tagId in blog.tags.all():
            values[idx]["tags"].append(tagId.tag)

    return values

def kbmodelToDict(kb_items):
    values = list(kb_items.values())

    for idx, blog in enumerate(kb_items):
        values[idx]["tags"] = []
        values[idx]["type"] = "knowledge_base"
        for tagId in blog.tags.all():
            values[idx]["tags"].append(tagId.tag)

    return values

"""
 Blogpost Item
 {
     "title": "Pavan first blogpost",
     "content": "Content for the blogpost blah blah blah blach",
     "authorId": "1",
     "tags": ["happiness", "joy"],
     "type": "blog_post"
 }

 Knowledge Base
  {
     "title": "Pavan first knowledge base item",
     "content": "Content for the blogpost blah blah blah blach",
     "authorId": "2",
     "tags": ["happiness", "first", "knowledge"],
     "type": "knowledge_base",
     "toolId": "7"
 }
"""

class BlogsList(APIView):
    def get(self, request):
        blogs = BlogPostModel.objects.all()

        values = list(blogs.values())

        for idx, blog in enumerate(blogs):
            values[idx]["tags"] = []
            values[idx]["createdByDisplayName"] = blog.createdBy.first_name + ' ' + blog.createdBy.last_name
            values[idx]["type"] = "blog_post"
            values[idx]["createdById"] = blog.createdBy.id
            del values[idx]["createdBy_id"]

            for tagId in blog.tags.all():
                values[idx]["tags"].append(tagId.tag)

        return Response(status=200, data=values)

    def post(self, request):
        request_data = request.data

        print(request_data)

        if "type" in request_data and request_data["type"] == 'knowledge_base':
            if KnowledgeBaseItem.objects.filter(title__iexact=request_data["title"]):
                return Response(status=200, data={
                    "message": "You already have a knowledge base item with the same title. Please choose another title."})

            user = User.objects.get(id=request_data["authorId"])

            # if not user.is_staff:
            #     return Response(status=403, data={'message':'You are not authorized to create a knowledge base item'})

            if "likeCount" in request_data:
                likeCount = request_data["likeCount"]
            else:
                likeCount = 0

            tool = Tool.objects.get(id=request_data["toolId"])
            knowledge_base_item = KnowledgeBaseItem(title=request_data["title"],
                                                    content=request_data["content"],
                                                    tool_id=tool,
                                                    likeCount=likeCount)
            knowledge_base_item.save()
            add_tags_knowledge_base(knowledge_base_item, request_data["tags"])

            knowledge_base_es = article_index_payload_builder(knowledge_base_item.id, request_data, user.first_name,
                                                              str(datetime.datetime), "knowledge_base")

            es.index(index='knowledge_base', body=knowledge_base_es)
            return Response(status=200, data={"message": "Successfully add a new knowledge base item",
                                            "id": knowledge_base_item.id,
                                            "type": "knowledge_base"})

        if does_blog_post_exist(request_data["authorId"], request_data["title"]):

            data = {'message': "You already have a blogpost with the same title. Please choose another title."}
            return Response(data=data, status=403)
        else:
            user = User.objects.get(id=request_data["authorId"])

            blogpost_db = BlogPostModel(title=request_data["title"],
                                        content=request_data["content"],
                                        createdBy=user,
                                        likeCount=0,
                                        visibility="visible")

            blogpost_db.save()

            # Save tags
            add_tags_blogpost(blogpost_db, request_data["tags"])

            # Index in elasticsearch
            blogpost_es = article_index_payload_builder(blogpost_db.id, request_data, user.first_name,
                                                        str(datetime.datetime), "blog_post")
            es.index(index='knowledge_base', body=blogpost_es)
        return Response(status=200, data={"message": "Successfully add a new blog post",
                                          "id": blogpost_db.id,
                                          "type": "blog_post"})

def add_tags_knowledge_base(knowledge_base_model, tags):
    for i in tags:
        if knowledge_base_model.tags.filter(id=knowledge_base_model.id).exists():
            continue
        tag = KnowledgeBaseTag(tag=i)
        tag.save()
        knowledge_base_model.tags.add(tag)

def add_tags_blogpost(blogpostModel, tags):
    for i in tags:
        if blogpostModel.tags.filter(id=blogpostModel.id).exists():
            continue
        tag = BlogPostTag(tag=i)
        tag.save()
        blogpostModel.tags.add(tag)

class PopularBlogList(APIView):
    def get(self, request):
        top = request.GET.get('top')
        if not top:
            top = 10

        blogs = BlogPostModel.objects.all().order_by('-likeCount')[:int(top)]

        values = list(blogs.values())

        for idx, blog in enumerate(blogs):
            values[idx]["tags"] = []
            values[idx]["createdByDisplayName"] = blog.createdBy.first_name + ' ' + blog.createdBy.last_name
            values[idx]["type"] = "blog_post"
            values[idx]["createdById"] = blog.createdBy.id
            values[idx]["commentCount"] = BlogPostComment.objects.filter(blog_post_id=blog.id).count()
            del values[idx]["createdBy_id"]

            for tagId in blog.tags.all():
                values[idx]["tags"].append(tagId.tag)
        return Response(status=200, data=values)

def dictfetchall(cursor):
    "Return all rows from a cursor as a dict"
    columns = [col[0] for col in cursor.description]
    return [
        dict(zip(columns, row))
        for row in cursor.fetchall()
        ]

class AllCategories(APIView):
    def get(self, request):
        with connection.cursor() as cursor:
            cursor.execute('SELECT distinct category from API_TOOL')
            data = dictfetchall(cursor)

        return Response(data, status=200)

class ExploreCategories(APIView):
    def get(self, request):
        with connection.cursor() as cursor:
            cursor.execute('SELECT category, '
                           'COUNT(1) as "publishedCount" '
                           'from API_TOOL GROUP BY '
                           'CATEGORY ORDER BY "publishedCount" DESC '
                           'LIMIT 6')
            data = dictfetchall(cursor)

        return Response(data, status=200)

class PopularBlogTags(APIView):
    def get(self, request):
        with connection.cursor() as cursor:
            cursor.execute('select tag, count(1) "tagCount" from '
                           '(select ab.tag as tag from  '
                           'api_blogpost_tags abt join api_blogposttag ab '
                           'on ab.id = abt.blogposttag_id) t '
                           'group by tag '
                           'order by "tagCount" desc limit 10')
            data = dictfetchall(cursor)

        return Response(data, status=200)

def does_blog_post_exist(author, title):
    with connection.cursor() as cursor:
        cursor.execute("SELECT COUNT(*) FROM api_blogpost WHERE api_blogpost.\"createdBy_id\" = %s AND title = %s",
                       [author, title])
        data = dictfetchall(cursor)
        print(data)
        if int(data[0]["count"]) > 0:
            return True
        else:
            return False

class Search(APIView):
    def get(self, request):
        query = request.GET.get('query')
        top = request.GET.get('top')

        if not top:
            top = 15

        if not query:
            return Response(status=400, data={"message": "No search query provided"})

        resp = es.search(body=
        {
            "query": {
                "multi_match": {
                    "query": query,
                    "fields": ["content", "title", "tags", "createdByDisplayName", "visibility"],
                    # "auto_generate_synonyms_phrase_query": True
                }
            }
        })

        if len(resp["hits"]["hits"]) <= 0:
            return Response(status=404, data={})

        blog_result_id = []
        kb_result_id = []

        for res in resp["hits"]["hits"]:
            print(res["_source"]["type"])
            if "blog" in res["_source"]["type"]:
                id = res["_source"]["id"]
                blog_result_id.append(id)

            else:
                id = res["_source"]["id"]
                kb_result_id.append(id)

        print(blog_result_id)

        blog_results = BlogPostModel.objects.filter(id__in=blog_result_id)
        kb_results = KnowledgeBaseItem.objects.filter(id__in=kb_result_id)

        blog_results = blogmodelToDict(blog_results)
        kb_results = kbmodelToDict(kb_results)

        final = blog_results + kb_results

        return Response(data=final[:int(top)], status=200)

class FilterCategories(APIView):
    def get(self, request, category):
        tools = Tool.objects.filter(category__iexact=category)
        values = []
        for idx, t in enumerate(tools):
            value = {"name": t.name, "category": t.category}
            values.append(value)
        return Response(status=200, data=values)

class Tools(APIView):
    def get(self, request):
        tools = Tool.objects.all()

        values = []
        for idx, t in enumerate(tools):
            value = {"id": t.id, "name": t.name, "category": t.category}
            values.append(value)
        return Response(status=200, data=values)

class RegistrationAPI(generics.GenericAPIView):
    serializer_class = CreateUserSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        return Response({
            "user": UserSerializer(user, context=self.get_serializer_context()).data,
            "token": AuthToken.objects.create(user)[1]
        })

class LoginAPI(generics.GenericAPIView):
    serializer_class = LoginUserSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data

        return Response({
            "user": UserSerializer(user, context=self.get_serializer_context()).data,
            "token": AuthToken.objects.create(user)[1]
        })

class UserAPI(generics.RetrieveAPIView):
    permission_classes = [permissions.IsAuthenticated, ]
    serializer_class = UserSerializer

    def get_object(self):
        return self.request.user

class CommentListAPI(APIView):
    def get(self, request, pk):
        '''GET method of comments (returns all comments for given BlogPost ID)'''

        # Grab list of comments for this BlogPost
        try:
            comments = BlogPostComment.objects.all().filter(blog_post_id__exact=pk)
            values = list(comments.values())
        except:
           return Response(status=404, data={"message": "Error: BlogPost not found."})

        # Double check that blogpost was found
        if len(values) == 0:
            return Response(status=404, data={"message": "Error: BlogPost not found."})

        for idx, comment in enumerate(comments):
            # Build response object
            values[idx]["id"] = comment.id
            values[idx]["author"] = comment.created_by.first_name + ' ' + comment.created_by.last_name
            values[idx]["createdOn"] = comment.created_on
            values[idx]["body"] = comment.content
            values[idx]["authorId"] = comment.created_by.id

            # Delete un-needed values
            del values[idx]["created_by_id"]
            del values[idx]["blog_post_id_id"]
            del values[idx]["content"]
            del values[idx]["created_on"]

        return Response(status=200, data=values)

class CommentAPI(APIView):
    def post(self, request):
        '''POST method for BlogPost comments'''

        comment_data = request.data

        # Get user instance
        try:
            user = User.objects.get(id=comment_data["authorId"])
        except:
           return Response(status=400, data={"message": "Author not found."})

        # Get blogpost instance
        try:
            blog_post = BlogPostModel.objects.get(id=comment_data["postId"])
        except:
            return Response(status=400, data={"message": "The blog post was not found."})

        # Finally, create the actual comment
        comment_db_item = BlogPostComment(content=comment_data["body"],
                                        blog_post_id=blog_post,
                                        created_by=user
                                        )
        comment_db_item.save()

        return Response(status=200, data={"message": "Successfully created a comment"})

    def delete(self, request, pk):
        '''DELETE method for comment'''

        try:
            comment = BlogPostComment.objects.get(id=pk)
        except:
            return Response(status=404, data={"message": "The comment you're trying to delete "
                                                         "may have already been deleted. "
                                                         "It cannot be found."})
        # Perform delete
        comment.delete()

        return Response(status=200, data={"message": "Comment deleted."})

class PopularKnowledgeList(APIView):
    def get(self, request):
        top = request.GET.get('top')
        if not top:
            top = 10

        kb_items = KnowledgeBaseItem.objects.all().order_by('-likeCount')[:int(top)]
        values = list(kb_items.values())

        for idx, item in enumerate(kb_items):
            values[idx]["tags"] = []
            values[idx]["type"] = "knowledge_base"
            tool = Tool.objects.filter(id=values[idx]["tool_id_id"])[0]
            tool_name = tool.name
            tool_category = tool.category

            values[idx]["toolName"] = tool_name
            values[idx]["toolId"] = values[idx]["tool_id_id"]
            values[idx]["toolCategory"] = tool_category

            del values[idx]["content_type"]
            del values[idx]["tool_id_id"]

            for tagId in item.tags.all():
                values[idx]["tags"].append(tagId.tag)

        return Response(status=200, data=values)

class KnowledgeBaseList(APIView):
    def get(self, request, pk):
        try:
            kb_items = KnowledgeBaseItem.objects.get(id=pk)
        except:
            return Response(status=404, data={"message": "Could't find your post."})

        values = kb_items.__dict__
        values["tags"] = []
        values["type"] = "knowledge_base"
        tool = Tool.objects.get(id=values["tool_id_id"])
        values["toolCategory"] = tool.category
        values["toolName"] = tool.name
        values["toolId"] = values["tool_id_id"]
        del values["tool_id_id"]
        del values["content_type"]

        for tag in kb_items.tags.all():
            values["tags"].append(tag.tag)

        del values['_state']

        return Response(status=200, data=values)

class LikeCounter(APIView):
    def get(self, request, pk):
        try:
            blog = BlogPostModel.objects.get(id=pk)
        except:
            return Response(status=404, data={"message": "Could't find your post."})

        return Response(status=200, data={"id": blog.id, "title": blog.title, "likeCount": blog.likeCount})

    def post(self, request, pk):
        try:
            blog = BlogPostModel.objects.get(id=pk)
            blog.likeCount += 1
            blog.save()
        except:
            return Response(status=404, data={"message": "Could't find your post."})

        return Response(status=200, data={"id": blog.id, "title": blog.title, "likeCount": blog.likeCount})

class UserDetails(APIView):
    def get(self, request, pk):
        '''GET method for retrieving user data'''

        try:
            with connection.cursor() as cursor:
                cursor.execute("SELECT * FROM auth_user WHERE id = %s", [pk])
                data = dictfetchall(cursor)[0]
        except:
            return Response(status=404, data={"message": "User not found!"})

        display_name = data["first_name"] + " " + data["last_name"]
        return Response(status=200, data={"id": data["id"],
                                         "display_name": display_name,
                                         "email": data["email"],
                                        "username": data["username"]})

class UserBlogs(APIView):
    def get(self, request, pk):
        blogs = BlogPostModel.objects.filter(id=pk)

        values = list(blogs.values())

        for idx, blog in enumerate(blogs):
            values[idx]["tags"] = []
            values[idx]["createdByDisplayName"] = blog.createdBy.first_name + ' ' + blog.createdBy.last_name
            values[idx]["type"] = "blog_post"
            values[idx]["createdById"] = blog.createdBy.id
            del values[idx]["createdBy_id"]

            for tagId in blog.tags.all():
                values[idx]["tags"].append(tagId.tag)

        return Response(status=200, data=values)


#!/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)


# 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)


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


#!/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


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)


#!/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()


from math import fabs
from typing import Union, Optional, Tuple, TypeVar

from rdp import rdp
from shapely.geometry import mapping, Polygon, MultiLineString

TPoint = TypeVar("TPoint")  # Tuple[float, float]
TPolygon = TypeVar("TPolygon")  # Tuple[TPoint, ...]

def get_coordinates(obj: Union[Polygon, MultiLineString], epsilon: float, bbox_comp: Optional[int],
                    bbox_size: Tuple[float, float], is_polygon: bool) \
        -> Union[Tuple[TPoint, ...], Tuple[TPolygon, ...], None]:
    """
    Transform shapely.geometry.Polygon or shapely.geometry.MultiLineString to tuple of points.
    Get rid of small polygons and linestrings and run Ramer-Douglas-Peucker.

    :param epsilon: Ramer-Douglas-Peucker algorithm parameter
    :param bbox_comp: scale object comparison parameter (to size of map bbox)
    :param bbox_size: lon, lat difference of map bbox
    :param is_polygon: object type is Polygon (True) or MultiLineString (False)
    :return: None if object did not pass bbox comparison,
             tuple of points of object if epsilon is None or epsilon == 0,
             tuple of points of object estimated by Ramer-Douglas-Peucker else
    """

    assert epsilon >= 0
    assert bbox_comp is None or bbox_comp >= 0

    if bbox_comp is not None:
        bounds = obj.bounds
        bounds_size = (fabs(bounds[2] - bounds[0]), fabs(bounds[3] - bounds[1]))

        if bounds_size[0] == 0 or bounds_size[1] == 0:
            return None

        if bbox_size[0] / bounds_size[0] >= bbox_comp and bbox_size[1] / bounds_size[1] >= bbox_comp:
            return None

    # extracting coordinates from geopandas.GeoDataFrame
    if is_polygon:
        coordinates = mapping(obj)['coordinates']
        polygons = list()
        for polygon in coordinates:
            polygons.append(tuple([tuple(point) for point in polygon] if epsilon is None or epsilon == 0 else
                                  [tuple(point) for point in rdp(polygon, epsilon=epsilon)]))
        return tuple(polygons) if len(polygons[0]) >= 3 else None
    coordinates = mapping(obj)['coordinates']
    points = [pair[0] for pair in coordinates]
    points.append(coordinates[-1][1])
    return tuple([tuple(point) for point in points]) if epsilon is None or epsilon == 0 else \
        tuple([tuple(point) for point in rdp(points, epsilon=epsilon)])


import time
from queue import Queue
from utils import utils
import subprocess
import backend.LoLAutomationLib as lolib
from backend.lolhandler import LoLHandler
import backend.data_scrape as ds

class Backend:
    def __init__(self, development_mode=False):
        self.dev_mode = development_mode
        self.queue = Queue(maxsize=10)
        self.lol_handler = LoLHandler(self.queue)
        self.lol_handler.start()
        self.url = ''
        self.champion_id = -1
        self.allRunes = None
        self.allSpells = None
        self.allItems = None
        self.loaded_items = dict()
        self.rune_mapping = dict()
        self.primary_spell_f = True
        self.lol_status = 'GAME_CLOSED'
        self.navigation_icons = dict()

    def get_status(self):
        message = self.queue.get()
        if message.message_type == 'GAME_OPENED':
            self.url = self.lol_handler.url
            if self.allRunes is None:
                self.allRunes = lolib.getRunes(self.url)
            if self.allSpells is None:
                self.allSpells = lolib.getSpells(self.url)
            if self.allItems is None:
                self.allItems = lolib.getItems(self.url)
            self.get_navigation_icons()
            self.create_default_mapping()
        if message.message_type == 'CHAMPION_PICKED':
            self.champion_id = self.lol_handler.champion_id
        if message.message_type == 'CHANGED_SKIN':
            self.champion_skin = message.message[0]
        return message

    def get_runes(self):
        return lolib.getFullRunePageImages(self.url)

    def swap_spells(self) -> None:
        spells = lolib.getSpellsIds(self.url)
        lolib.setSpells(self.url, spells[::-1])
        self.primary_spell_f = not self.primary_spell_f

    def get_navigation_icons(self):
        self.navigation_icons['lane_navigation'] = {lane : {'disabled': f'images/{lane}_disabled.png', 'enabled': f'images/{lane}.png'} for lane in utils.lanes}

    def get_build(self):
        def BuildByLane(page, queueName):
            build = ds.get_build(page, queue=queueName)
            if not build['exists']:
                return False, {}
            championLane = dict()
            championLane['spells'] = [lolib.getImageFromUrl(self.url, self.allSpells[build['spells'][i]]['iconPath']) for i in range(2)]
            championLane['ability_order'] = build['abilities_order']
            championLane['runes'] = self.get_rune_mapping(build['runes'])
            championLane['items'] = [[{'image': self.get_item_image(item), 'description': self.allItems[item]['iconDesc'], 'count': count} for item, count in zip(build['start_items'], build['start_items_n'])], [{'image': self.get_item_image(item), 'description': self.allItems[item]['iconDesc'], 'count': 1} for item in build['best_items']]] # TEST
            championLane['wr'] = build['wr']
            championLane['pr'] = build['pr']
            championLane['br'] = build['br']

            return True, championLane, build

        champion_info = lolib.getChampionInfo(self.url, self.champion_id)

        queueName = ''
        if not self.dev_mode:
            queueName = lolib.getQueueSpecialName(self.url)

        champion = dict()
        champion['build'] = {}
        champion['name'] = champion_info['name']
        champion['image'] = champion_info['iconPath']
        champion['abilities'] = [{'name': '', 'icon': ability} for ability in lolib.getAbilitiesIcons(self.url, self.champion_id)]

        lanes = ['top', 'jungle', 'mid', 'adc', 'support', '']
        pages = ds.load_pages(queueName, champion_info['alias'], lanes)
        for lane in lanes[:-1]:
            exists, championLane, build = BuildByLane(pages[lane], queueName)
            if exists:
                champion['build'][lane] = [championLane, build]

        champion['default_lane'] = ds.getDefaultLane(pages[''])
        return champion

    def get_item_image(self, item):
        if item not in self.loaded_items:
            self.loaded_items[item] = lolib.getImageFromUrl(self.url, self.allItems[item]['iconPath'])
        return self.loaded_items[item]

    def ping(self) -> str:
        cmd_ping = subprocess.Popen(["ping.exe", "72.52.10.14", "-n", "1"], stdout=subprocess.PIPE)
        return cmd_ping.communicate()[0].decode('utf8').replace("\r", "").strip().split("\n")[-1].split(",")[1].split("=")[1][:-2]

    def close(self):
        self.lol_handler.loop_stop()
        self.lol_handler.join()

    def create_default_mapping(self):
        if len(self.rune_mapping) > 0:
            return

        for key in self.allRunes:
            for row_num, row in enumerate(self.allRunes[key]['slots'], start=1):
                number = row_num
                if row_num > 4:
                    number -= 5
                for index, rune in enumerate(row):
                    self.rune_mapping[rune] = (number, index)

        self.rune_mapping[8000] = (0, 0)
        self.rune_mapping[8100] = (0, 1)
        self.rune_mapping[8200] = (0, 2)
        self.rune_mapping[8400] = (0, 3)
        self.rune_mapping[8300] = (0, 4)

    def get_rune_mapping(self, runes):
        complete_runes = []
        for index, part in enumerate(runes):
            if index < 2:
                result = [-1 for _ in range(5)]
            else:
                result = [-1 for _ in range(3)]

            for row, rune in enumerate(part):
                data = self.rune_mapping[rune]

                if index == 2:
                    result[row] = data[1]
                else:
                    result[data[0]] = data[1]

            complete_runes.append(result)

        return complete_runes

    def set_everything(self, champion):
        if not self.primary_spell_f:
            lolib.setSpells(self.url, champion['spells'][::-1])
        else:
            lolib.setSpells(self.url, champion['spells'])

        lolib.setRunes(self.url, champion['runes'])
        lolib.updateItemSet(self.url, champion['start_items'], champion['best_items'])

    def set_active_skin(self, skin_id):
        lolib.setSkin(self.url, skin_id)

    def get_skins(self):
        return lolib.getSkins(self.url)


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 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 rest_framework.response import Response
from rest_framework import status
from django.contrib.auth import logout
from common.models import Org, Profile

def set_profile_request(request, org):
    if request.user.is_authenticated:
        request.profile = Profile.objects.filter(
            user=request.user, org=org, is_active=True).first()
        if request.profile is None:
            logout(request)
            return Response(
                {"error": False}, status=status.HTTP_200_OK,
            )

class GetProfileAndOrg(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        self.process_request(request)
        return self.get_response(request)

    def process_request(self, request):
        if request.headers.get("org"):
            org_id = request.headers.get("org")
            org = Org.objects.filter(id=org_id).first()
            if org:
                request.org = org
                set_profile_request(request, org)
            else:
                request.org = None
        else:
            request.org = None


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")


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
# }


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 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 scipy.integrate._ivp.radau import T
from double_pendulum import DoublePendulum
import numpy as np
import pytest

@pytest.mark.parametrize(
    "theta1, theta2, expected",
    [
        (  0,   0,            0),
        (  0, 0.5,  3.386187037),
        (0.5,   0, -7.678514423),
        (0.5, 0.5, -4.703164534),
    ]
)
def test_domega1_dt(theta1, theta2, expected):
    dp = DoublePendulum()
    t = 0
    y = (theta1, 0.25, theta2, 0.15)
    dtheta1_dt, domega1_dt, _, _ = dp(t, y)
    assert np.isclose(dtheta1_dt, 0.25)
    assert np.isclose(domega1_dt, expected)

@pytest.mark.parametrize(
    "theta1, theta2, expected",
    [
        (  0,   0,          0.0),
        (  0, 0.5, -7.704787325),
        (0.5,   0,  6.768494455),
        (0.5, 0.5,          0.0),
    ],
)
def test_domega2_dt(theta1, theta2, expected):
    dp = DoublePendulum()
    t = 0
    y = (theta1, 0.25, theta2, 0.15)
    _, _, dtheta2_dt, domega2_dt = dp(t, y)
    assert np.isclose(dtheta2_dt, 0.15)
    assert np.isclose(domega2_dt, expected)

def test_DoublePendulum_at_origin_with_angular_velocity():
    """Testing that the pendulum moves from angle 0,0 when given an initial speed
    """
    pend = DoublePendulum()
    y = (0, 0.25, 0, 0.15)
    pend.solve(y,10,0.1)

    assert pend.Etotal.all() > 0

def test_DoublePendulum_empty_attribute():
    """Testing for empty attributes
    """
    pend = DoublePendulum()
    with pytest.raises(Exception):
        pend.theta1
        pend.theta2
        pend.vx1
        pend.vx2
        pend.vy1
        pend.vy2
        pend.P
        pend.K
        pend.Etotal
        pend.t

def test_DoublePendulum_no_direct_attribute_access():
    theta1 = 90
    theta2 = 180
    omega1 = 0
    omega2 = 0
    y = (theta1, omega1, theta2, omega2)

    pend = DoublePendulum()
    pend.solve(y,10,0.1)
    with pytest.raises(AttributeError):
        pend.__theta1
        pend.__theta2
        pend.__vx1
        pend.__vx2
        pend.__vy1
        pend.__vy2
        pend.__P
        pend.__K
        pend.__Etotal
        pend.__t


import matplotlib.pyplot as plt

from matplotlib import transforms

from dataset.dataset import Dataset
from dataset.files import TimeSeries, RefPattern, DTW, RP
from utils.functions import Paths
from utils.specification import specs, cmap, multi_rho

dataset = 'cbf'
base_pattern = False
pattern_name = ''
rho = '0.100'
dataset_type = 'DTW'
window_size = 25

dataset_name = dataset if not base_pattern else dataset + '_base'
length = specs[dataset_name]['x_dim']
stream_id = 0

if pattern_name == 'FULL':
    ref_ids = range(5)
elif len(pattern_name) > 0:
    ref_ids = range(len(pattern_name))

if base_pattern:
    rho_name = f'rho {rho}_base'
    if len(pattern_name) > 0:
        rho_name = f'rho {rho}_base_{pattern_name}'
else:
    rho_name = f'rho {rho}'
    ref_ids = specs[dataset_name]['ref_id']

if dataset_type == 'DTW':
    image_class = DTW
elif dataset_type == 'RP':
    image_class = RP
    rho_name = 'RP'

paths = Paths(dataset, dataset_type, rho, window_size, base_pattern, pattern_name, network_type='CNN', core_path='../')
beginning_path = paths.get_beginning_path()
data_path = paths.get_data_path()

if dataset == 'gunpoint':
    ref_name = 'REF_num-5.npy'
    stream_name = f'STREAM_cycles-per-label-20_set-test_id-{stream_id}.npy'
else:
    stream_name = f'STREAM_length-100_noise-5_warp-10_shift-10_outliers-0_cycles-per-label-10_' \
                  f'set-test_id-{stream_id}.npy'
    ref_name = 'REF_length-100_noise-5_warp-10_shift-10_outliers-0_num-10.npy'

if base_pattern:
    ref_name = f'BASE_REF_len-{length}_noise-5_num-1.npy'
    if len(pattern_name) > 0:
        ref_name = f'BASE_REF_len-{length}_noise-5_num-1_{pattern_name}.npy'

t = TimeSeries(f'{beginning_path}/{stream_name}')
timeseries = t.timeseries

ref = RefPattern(f'{beginning_path}/{ref_name}')

imgs = []
prefix_path = beginning_path.split('rho ')[0]
if base_pattern:
    suffix_rho = f'_base_{paths.pattern_name}'
else:
    suffix_rho = ''

if rho == 'multi':
    rho_arr = multi_rho
    starting_path_arr = [f'{prefix_path}/rho {rho}{suffix_rho}' for rho in rho_arr]
else:
    rho_arr = [rho]
    starting_path_arr = [f'{prefix_path}/rho {rho}{suffix_rho}' for rho in rho_arr]

for idx, ref_id in enumerate(ref_ids):
    for rho, starting_path in zip(rho_arr, starting_path_arr):
        dtw = image_class(ref, t, class_num=idx + 1, rho=f'{rho}',
                          starting_path=starting_path,
                          ref_id=ref_id)
        imgs.append(dtw)

Dataset.image_creator(*imgs, window_size=window_size)

fig = plt.figure(constrained_layout=True)
gs = fig.add_gridspec(len(imgs) + 1, 12)

f_axi1 = fig.add_subplot(gs[0, 1:])
f_axi1.plot(timeseries, label='val')
f_axi1.plot(t.labels, label='lab')
f_axi1.axes.get_yaxis().set_visible(False)
f_axi1.axes.get_xaxis().set_visible(False)

for idx, img_class in enumerate(imgs):
    # AXIS 2
    f_axi = fig.add_subplot(gs[idx + 1, 1:], sharex=f_axi1)
    img = f_axi.imshow(img_class.img, cmap=cmap, origin='lower', aspect='auto')
    f_axi.axes.get_yaxis().set_visible(False)
    if idx != len(imgs) - 1:
        f_axi.axes.get_xaxis().set_visible(False)

    # SUB AXIS 2,1
    f_axi_1 = fig.add_subplot(gs[idx + 1, 0])
    base = plt.gca().transData
    rot = transforms.Affine2D().rotate_deg(90)
    f_axi_1.plot(ref.lab_patterns[img_class.from_pattern_idx]['pattern'], transform=rot + base)
    if base_pattern and img_class.from_pattern_idx == 0:
        val_min = ref.lab_patterns[img_class.from_pattern_idx]['pattern'].min()
        val_max = ref.lab_patterns[img_class.from_pattern_idx]['pattern'].max()
        f_axi_1.set_xlim(-val_max * 2, val_max * 2)
    f_axi_1.axis('off')

    fig.colorbar(img, cmap=cmap, ax=f_axi)

plt.show()


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])


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_())