# Standard Library
from builtins import str
import os
import time

# Third Party Stuff
from django.conf import settings
from django.db.models import Q

# Spoken Tutorial Stuff
from creation.models import *
from youtube.core import *
from youtube.utils import *

def upload_videos():
    service = get_youtube_credential()

    if service is None:
        print(('authenticate using the following url to continue: \n {}'.format(get_auth_url())))
        return

    tresource_list = TutorialResource.objects.filter(Q(status=1) | Q(status=2), video_id=None).select_related()
    error_log_file_head = open('{}/logs/youtube-error.log'.format(settings.BASE_DIR),"w")
    success_log_file_head = open('{}/logs/youtube-success.log'.format(settings.BASE_DIR),"w")

    for tresource in tresource_list:
        try:
            ogv_video_path = '{}videos/{}/{}/{}'.format(
                settings.MEDIA_ROOT, tresource.tutorial_detail.foss_id, tresource.tutorial_detail_id, tresource.video)
            mp4_video_path, file_extn = os.path.splitext(ogv_video_path)
            mp4_tmp_video_path = mp4_video_path + '-tmp.mp4'
            mp4_video_path = mp4_video_path + '.mp4'
            title_string = '{} - {}'.format(tresource.tutorial_detail.tutorial, tresource.language.name)

            if not os.path.isfile(mp4_video_path):
                convert_tmp_video(ogv_video_path, mp4_tmp_video_path)
                convert_video(mp4_tmp_video_path, mp4_video_path)
                if not os.path.isfile(mp4_video_path) or not os.path.getsize(mp4_video_path):
                    print((tresource.tutorial_detail.tutorial, '-', tresource.language.name, '-- MP4 video missing'))
                    continue

            options = {
                'title': title_string,
                'description': title_string,#tresource.outline.encode('utf-8').strip(),
                'tags': tresource.common_content.keyword.split(','),
                'file': mp4_video_path
            }

            # try:
            print(('uploading video', tresource.id, '-', tresource.tutorial_detail.tutorial, '-', tresource.tutorial_detail.foss.foss, '-', tresource.language.name))
            video_id = upload_video(service, options)
            # except:
            #     video_id = None
            print(('video id -', video_id))

            if video_id:
                tresource.video_id = video_id
                tresource.save()
                success_log_file_head.write('upload_video, {}, {}, {}\n'.format(tresource.id, title_string, video_id))
            else:
                error_log_file_head.write('upload_video, {}, {}\n'.format(tresource.id, title_string))

            time.sleep(1)
        except Exception as e:
            error_log_file_head.write('upload_video, {}, {}\n'.format(tresource.id, str(e)))

def create_playlists():
    service = get_youtube_credential()

    if service is None:
        return

    foss_list = FossCategory.objects.all()

    for foss_obj in foss_list:
        tr_lang_list = TutorialResource.objects.filter(
            Q(status=1) | Q(status=2),
            tutorial_detail__foss_id=foss_obj.id).values_list('language_id').distinct()
        lang_list = Language.objects.filter(id__in=tr_lang_list).exclude(
            id__in=PlaylistInfo.objects.filter(foss_id=foss_obj.id).values_list('language_id').distinct())

        for lang_obj in lang_list:
            title = '{} - {}'.format(foss_obj.foss, lang_obj.name)
            print(('creating playlist:', title))
            playlist_id = create_playlist(service, title, foss_obj.description)

            if playlist_id:
                playlist_obj = PlaylistInfo.objects.create(
                    foss=foss_obj, language=lang_obj, playlist_id=playlist_id)

            time.sleep(1)

def append_to_playlist():
    service = get_youtube_credential()

    if service is None:
        return

    rows = TutorialResource.objects.filter(
        Q(status=1)|Q(status=2), video_id__isnull=False, playlist_item_id__isnull=True
    ).values_list('tutorial_detail__foss_id', 'language_id').distinct()

    for row in rows:
        playlist_obj = PlaylistInfo.objects.filter(language_id=row[1], foss_id=row[0]).first()

        if not playlist_obj:
            continue

        tresource_list = TutorialResource.objects.filter(
            Q(status=1)|Q(status=2), tutorial_detail__foss_id=row[0],
            language_id=row[1]
        ).order_by(
            'tutorial_detail__foss__foss', 'tutorial_detail__level_id', 'tutorial_detail__order')
        counter = 1

        for tresource in tresource_list:
            if tresource.playlist_item_id:
                print(('updating palylist item position -', tresource.playlist_item_id))
                result = update_playlistitem_position(
                    service, tresource.playlist_item_id,
                    tresource.video_id, playlist_obj.playlist_id, counter)
            else:
                print(('adding item to playlist -', playlist_obj.playlist_id))
                result = add_to_playlist(service, tresource.video_id, playlist_obj.playlist_id, counter)

                if result:
                    tresource.playlist_item_id = result
                    tresource.save()
                    PlaylistItem.objects.create(playlist=playlist_obj, item_id=result)

            if not result:
                break

            counter += 1
            time.sleep(1)


from typing import TYPE_CHECKING, Dict, Optional, Type
import json
from .flow import Flow, FlowCallback
from .state import State, StateCallback
from .transition import Transition, TransitionCallback
from .process_mixin import ProcessMixin, CantStartProcess
from .step_mixin import StepMixin
from .helpers import get_jembewf, CanProceed

if TYPE_CHECKING:
    from flask import Flask
    from flask_sqlalchemy import SQLAlchemy
    import jembewf

__all__ = (
    "get_jembewf",
    "CanProceed",
    "JembeWF",
    "Flow",
    "FlowCallback",
    "State",
    "StateCallback",
    "Transition",
    "TransitionCallback",
    "ProcessMixin",
    "StepMixin",
    "CantStartProcess",
)

class JembeWF:
    """Jembe Workflow Management extension for Flask framework"""

    def __init__(
        self,
        app: Optional["Flask"] = None,
        db: Optional["SQLAlchemy"] = None,
        process_model: Optional[Type["jembewf.ProcessMixin"]] = None,
        step_model: Optional[Type["jembewf.StepMixin"]] = None,
    ) -> None:

        self.flows: Dict[str, "jembewf.Flow"] = {}
        self.initialised = False

        self.db: "SQLAlchemy"
        if db is not None:
            self.db = db

        self.process_model: Type["jembewf.ProcessMixin"]
        if process_model is not None:
            self.process_model = process_model

        self.step_model: Type["jembewf.StepMixin"]
        if step_model is not None:
            self.step_model = step_model

        if app is not None:
            if process_model is None or step_model is None or db is None:
                raise Exception(
                    "When you provide app parameter then "
                    "db, process_model and step_model must also be provided "
                    "in other to initialise JembeWf."
                )
            self.init_app(app, self.db, self.process_model, self.step_model)

    def init_app(
        self,
        app: "Flask",
        db: Optional["SQLAlchemy"] = None,
        process_model: Optional[Type["jembewf.ProcessMixin"]] = None,
        step_model: Optional[Type["jembewf.StepMixin"]] = None,
    ) -> None:
        """Initialize JembeWF Flask extension

        Args:
            app (Flask): Flask instance
        """
        if "jembewf" in app.extensions:
            raise Exception("JembeWF is already registred at Flask app")
        # check if db is provided
        if db is None and not hasattr(self, "db"):
            raise Exception("JembeWF 'db' must be provided in __init__ or in init_app")
        if db is not None:
            self.db = db

        # check if process_model is provided
        if process_model is None and not hasattr(self, "process_model"):
            raise Exception(
                "JembeWF 'process_model' must be provided in __init__ or in init_app"
            )
        if process_model is not None:
            self.process_model = process_model

        # check if step_model is provided
        if step_model is None and not hasattr(self, "step_model"):
            raise Exception(
                "JembeWF 'step_model' must be provided in __init__ or in init_app"
            )
        if step_model is not None:
            self.step_model = step_model

        # initialise extension
        app.extensions["jembewf"] = self
        self.initialised = True

    def add(self, *flows: "jembewf.Flow") -> "jembewf.JembeWF":
        """Add/Register Flow definition"""
        if self.initialised:
            raise Exception(
                "Can't add flows to JembeWF because it is already initialised."
            )

        for flow in flows:
            if flow.name in self.flows:
                raise Exception(
                    f"Flow with same name '{flow.name}' is already registred."
                )
            self.flows[flow.name] = flow
        return self

    def start(self, flow_name: str, **process_vars) -> "jembewf.ProcessMixin":
        """Start Process instance from Flow definition

        Returns Process instance that has been started.
        """
        return self.process_model.create(flow_name, **process_vars)

    def can_start(self, flow_name: str, **process_vars) -> bool:
        """Check if process from flow definition can be started"""
        return self.process_model.can_start(flow_name, **process_vars)

    def get_flow(self, flow_name: str) -> "jembewf.Flow":
        """Returns flow instance by flow name

        Args:
            flow_name (str): Name of the flow

        Raises:
            ValueError: When flow with provided name doesn't exist

        Returns:
            jembewf.Flow: Flow instance
        """
        try:
            return self.flows[flow_name]
        except KeyError as err:
            raise ValueError(f"Flow '{flow_name}' doesn't exit!") from err

    def has_flow(self, flow_name: str) -> bool:
        """Returns true if flow with provided name exist"""
        return flow_name in self.flows


# 연산자 끼워 넣기

import itertools
import sys
input = sys.stdin.readline

n = int(input())
data = list(map(int, input().split()))
operator_num = list(map(int, input().split()))
operator = ['+','-','x','%']

oper = []
for i in range(len(operator_num)):
    if operator_num[i] != 0:
        for _ in range(operator_num[i]):
            oper.append(operator[i])

result = []
for x in itertools.permutations(oper, len(oper)):
    x =list(x)
    cnt = data[0]
    for i in range(n-1):
        if x[i] == '+':
            cnt = cnt + data[i+1]
        elif x[i] == '-':
            cnt = cnt - data[i+1]
        elif x[i] == 'x':
            cnt = cnt * data[i+1]
        elif x[i] == '%':
            if cnt < 0:
                cnt = -cnt // data[i+1]
                cnt = -cnt
            else: cnt = cnt // data[i+1]
    result.append(cnt)

print(max(result))
print(min(result))


from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import configparser
import logging
from datetime import datetime
import pytz
from telegramScrapper import Scrapper

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level = logging.INFO,
    filename='telegram_bot.log'
)

logging.debug('Reading credentials and loading bot...')
cfg = configparser.ConfigParser()
cfg.read('config.cfg')
TOKEN = cfg['DEFAULT']['token'] # insert your bot token here
logging.info('Credentials established.')

logging.info('Log file established.')

logging.info('Starting bot.')
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher

singapore = pytz.timezone('Asia/Singapore')
scrapper = Scrapper()

# callback functions

## common message
MSG = "Say '/time' for time or '/quote' to get a quote!"

## callbacks
def start(update, context):
    welcome_text = "Hello {}, I am a bot and do start talking to me!\n\nSay what you want and I'll try to respond! \nSay '/time' to get the current time\nSay '/quote' for me to say something random.".format(str(update.message.from_user.username))
    context.bot.send_message(
        chat_id = update.effective_chat.id,
        text=welcome_text
    )
    logging.info('{}: {}'.format(update.effective_chat.id, welcome_text))

def echo(update, context):
    context.bot.send_message(chat_id = update.effective_chat.id, text="I didn't understand that but my creator loves you!")
    context.bot.send_message(
        chat_id = update.effective_chat.id,
        text=MSG
    )

def whatTime(update, context):
    now = datetime.now(singapore).time().strftime("%H:%M:%S")
    context.bot.send_message(
        chat_id = update.effective_chat.id,
        text="The time now is: {}".format(now)
    )
    context.bot.send_message(
        chat_id = update.effective_chat.id,
        text=MSG
    )

def sayQuote(update, context):
    msg = scrapper.getQuote()
    context.bot.send_message(
        chat_id = update.effective_chat.id,
        text=msg
    )
    context.bot.send_message(
        chat_id = update.effective_chat.id,
        text=MSG
    )

# handlers
start_handler = CommandHandler('start', start)
echo_handler = MessageHandler(Filters.text, echo)
time_handler = CommandHandler('time', whatTime)
quote_handler = CommandHandler('quote', sayQuote)

dispatcher.add_handler(start_handler)
dispatcher.add_handler(echo_handler)
dispatcher.add_handler(time_handler)
dispatcher.add_handler(quote_handler)

updater.start_polling()


from __future__ import print_function

import time

import rospy
import tf2_ros

from nist_gear.msg import Order
from nist_gear.msg import VacuumGripperState
from nist_gear.srv import AGVControl
from nist_gear.srv import ConveyorBeltControl
from nist_gear.srv import DroneControl
from nist_gear.srv import SubmitShipment
from nist_gear.srv import VacuumGripperControl
from control_msgs.msg import JointTrajectoryControllerState
from std_msgs.msg import String
from std_srvs.srv import Trigger
from trajectory_msgs.msg import JointTrajectory
from trajectory_msgs.msg import JointTrajectoryPoint

from nist_gear.msg import Order, Model, LogicalCameraImage, VacuumGripperState
from geometry_msgs.msg import TransformStamped
from std_srvs.srv import Trigger
from nist_gear.srv import AGVControl, GetMaterialLocations, VacuumGripperControl

from math import pi, sqrt
import moveit_commander as mc

import sys
import copy
import yaml
import re

#TODO: submit agvs to deliver orders/kits
#TODO: listen and react to Faulty Products on agvs
#TODO: parse orders
#TODO: parse sensors data
#TODO: locate parts, plan movements
#TODO: number and places of sensors
#TODO

#TODO: control conveyor? (only for development)

def start_competition():
    rospy.loginfo("start_competition() method.")
    rospy.loginfo("Waiting for competition to be ready...")
    rospy.wait_for_service('/ariac/start_competition')
    rospy.loginfo("Competition is now ready.")
    rospy.loginfo("Requesting competition start...")

    try:
        start = rospy.ServiceProxy('/ariac/start_competition', Trigger)
        response = start()
    except rospy.ServiceException as exc:
        rospy.logerr("Failed to start the competition: %s" % exc)
        return False
    if not response.success:
        rospy.logerr("Failed to start the competition: %s" % response)
    else:
        rospy.loginfo("Competition started!")
    return response.success

def setup_competition(comp_class):

    connect_callbacks(comp_class)

    if not comp_class.arm_1_has_been_zeroed:
        comp_class.send_arm_to_state([0.0, -pi/4, pi/2, -pi/4, pi/2, 0], comp_class.arm_1_joint_names, comp_class.arm_1_joint_trajectory_publisher)
        comp_class.arm_1_has_been_zeroed = True

    if not comp_class.arm_2_has_been_zeroed:
        comp_class.send_arm_to_state([pi, -pi/4, pi/2, -pi/4, pi/2, 0], comp_class.arm_2_joint_names, comp_class.arm_2_joint_trajectory_publisher)
        comp_class.arm_2_has_been_zeroed = True

    # move gantry to zero
    comp_class.send_arm_to_state([0, 0, 0], comp_class.gantry_joint_names, comp_class.gantry_joint_trajectory_publisher)

    # reset Grippers
    comp_class.control_gripper(False, "left")
    comp_class.control_gripper(False, "right")

def end_competition():
    rospy.wait_for_service('/ariac/end_competition')
    rospy.ServiceProxy('/ariac/end_competition', Trigger)()

def run_competition():
    rate = rospy.Rate(1000) # big amount on purpose

    # # fake run competition body start
    # start = time.time()
    #
    # while not rospy.is_shutdown():
    #     rate.sleep()
    #
    #     time_elapsed = time.time() - start
    #
    #     if time_elapsed > 10:
    #         break;
    # # fake run competition body end

    group_names = ['Full_Robot', 'Left_Arm', 'Right_Arm', 'Gantry']
    moveit_runner = MoveitRunner(group_names, ns='/ariac/gantry')

    order = get_order()
    agv_states = {'agv1': [], 'agv2': []}

    all_known_parts = get_parts_from_cameras()

    for shipment in order.shipments:
        active_agv = 'agv1' if shipment.agv_id == 'agv1' else 'agv2'
        agv_state = agv_states[active_agv]

        while True:
            rate.sleep()

            valid_products = []
            for product in shipment.products:
                if product not in agv_state:
                    valid_products.append(product)

            candidate_moves = []
            for part in all_known_parts:
                for product in valid_products:
                    if part.type == product.type:
                        candidate_moves.append((part, product))

            if candidate_moves:
                part, target = candidate_moves[0]

                world_target = get_target_world_pose(target, active_agv)
                part_location = get_part_type_location(part)

                move_successful = moveit_runner.move_part(
                    part,
                    world_target,
                    part_location,
                    active_agv
                )
                if move_successful:
                    all_known_parts.remove(part)
                    agv_state.append(target)
            else:
                break

        submit_shipment(active_agv, shipment.shipment_type)
        agv_states[active_agv] = []

class MyCompetitionClass:
    def __init__(self):
        self.gantry_joint_trajectory_publisher = rospy.Publisher("/ariac/gantry/gantry_controller/command", JointTrajectory, queue_size=10)
        self.arm_1_joint_trajectory_publisher = rospy.Publisher("/ariac/gantry/left_arm_controller/command", JointTrajectory, queue_size=10)
        self.arm_2_joint_trajectory_publisher = rospy.Publisher("/ariac/gantry/right_arm_controller/command", JointTrajectory, queue_size=10)

        self.current_comp_state = None
        self.received_orders = []
        self.gantry_current_joint_state = None
        self.arm_1_current_joint_state = None
        self.arm_2_current_joint_state = None
        self.arm_1_current_gripper_state = None
        self.arm_2_current_gripper_state = None
        self.last_gantry_joint_state_print = time.time()
        self.last_arm_1_joint_state_print = time.time()
        self.last_arm_2_joint_state_print = time.time()
        self.last_arm_1_gripper_state_print = time.time()
        self.last_arm_2_gripper_state_print = time.time()
        self.arm_1_has_been_zeroed = False
        self.arm_2_has_been_zeroed = False
        self.gantry_joint_names = [
            'small_long_joint',
            'torso_rail_joint',
            'torso_base_main_joint',
        ]
        #left
        self.arm_1_joint_names = [
            'left_shoulder_pan_joint',
            'left_shoulder_lift_joint',
            'left_elbow_joint',
            'left_wrist_1_joint',
            'left_wrist_2_joint',
            'left_wrist_3_joint',
        ]
        #right
        self.arm_2_joint_names = [
            'right_shoulder_pan_joint',
            'right_shoulder_lift_joint',
            'right_elbow_joint',
            'right_wrist_1_joint',
            'right_wrist_2_joint',
            'right_wrist_3_joint',
        ]

    def comp_state_callback(self, msg):
        if self.current_comp_state != msg.data:
            rospy.loginfo("Competition state changed: " + str(msg.data))
        self.current_comp_state = msg.data

    def order_callback(self, msg):
        rospy.loginfo("order_callback")
        rospy.loginfo("Received order:\n" + str(msg))
        self.received_orders.append(msg)

    def gantry_joint_state_callback(self, msg):
        if time.time() - self.last_gantry_joint_state_print >= 10:
            rospy.loginfo("Current gantry Joint States (throttled to 0.1 Hz):\n" + str(msg))
            self.last_gantry_joint_state_print = time.time()
        self.gantry_current_joint_state = msg

    def arm_1_joint_state_callback(self, msg):
        if time.time() - self.last_arm_1_joint_state_print >= 10:
            rospy.loginfo("Current Arm 1 Joint States (throttled to 0.1 Hz):\n" + str(msg))
            self.last_arm_1_joint_state_print = time.time()
        self.arm_1_current_joint_state = msg

    def arm_2_joint_state_callback(self, msg):
        if time.time() - self.last_arm_2_joint_state_print >= 10:
            rospy.loginfo("Current Arm 2 Joint States (throttled to 0.1 Hz):\n" + str(msg))
            self.last_arm_2_joint_state_print = time.time()
        self.arm_2_current_joint_state = msg

    def arm_1_gripper_state_callback(self, msg):
        if time.time() - self.last_arm_1_gripper_state_print >= 10:
            rospy.loginfo("Current Arm 1 gripper state (throttled to 0.1 Hz):\n" + str(msg))
            self.last_arm_1_gripper_state_print = time.time()
        self.arm_1_current_gripper_state = msg

    def arm_2_gripper_state_callback(self, msg):
        if time.time() - self.last_arm_2_gripper_state_print >= 10:
            rospy.loginfo("Current Arm 2 gripper state (throttled to 0.1 Hz):\n" + str(msg))
            self.last_arm_2_gripper_state_print = time.time()
        self.arm_2_current_gripper_state = msg

    def send_arm_to_state(self, positions, joints_names, publisher):
        rospy.loginfo("send_arm_to_state method():\npositions" + str(positions))

        msg = JointTrajectory()
        msg.joint_names = joints_names
        point = JointTrajectoryPoint()
        point.positions = positions
        point.time_from_start = rospy.Duration(1.0)
        msg.points = [point]
        #TODO: rospy.loginfo("Sending command:\n" + str(msg))
        publisher.publish(msg)

    def send_arm1_to_state(self, positions):
        return self.send_arm_to_state(positions, self.arm_1_joint_names, self.arm_1_joint_trajectory_publisher)

    def send_arm2_to_state(self, positions):
        return self.send_arm_to_state(positions, self.arm_2_joint_names, self.arm_2_joint_trajectory_publisher)

    def send_gantry_to_state(self, positions):
        return self.send_arm_to_state(positions, self.gantry_joint_names, self.gantry_joint_trajectory_publisher)

    def control_gripper(self, enabled, arm):
        if arm not in ("left", "right"):
            raise ValueError('Only two arms ("left", "right")')

        rospy.loginfo("Waiting for gripper control to be ready...")

        service_name = '/ariac/gantry/{}_arm/gripper/control'.format(arm)
        rospy.wait_for_service(service_name)
        rospy.loginfo("Requesting gripper control...")

        try:
            gripper_control = rospy.ServiceProxy(service_name, VacuumGripperControl)
            response = gripper_control(enabled)
        except rospy.ServiceException as exc:
            rospy.logerr("Failed to control the gripper: %s" % exc)
            return False
        if not response.success:
            rospy.logerr("Failed to control the gripper: %s" % response)
        else:
            rospy.loginfo("Gripper controlled successfully")
        return response.success

class MoveitRunner():
    def __init__(self, group_names, node_name='ariac_moveit_example', ns='', robot_description='robot_description'):

        mc.roscpp_initialize(sys.argv)
        #rospy.init_node(node_name, anonymous=True)

        self.robot = mc.RobotCommander(ns+'/'+robot_description, ns)
        self.scene = mc.PlanningSceneInterface(ns)
        self.groups = {}
        for group_name in group_names:
            group = mc.MoveGroupCommander(
                group_name,
                robot_description=ns+'/'+robot_description,
                ns=ns
            )
            group.set_goal_tolerance(0.05)
            self.groups[group_name] = group

        self.define_preset_locations()
        self.goto_preset_location('start')

    def define_preset_locations(self):
        locations = {}

        name = 'start'
        gantry = [0, 0, 0]
        left = [0.0, -pi/4, pi/2, -pi/4, pi/2, 0]
        right = [pi, -pi/4, pi/2, -pi/4, pi/2, 0]
        locations[name] = (gantry, left, right)

        name = 'bin3'
        gantry = [4.0, -1.1, 0.]
        left = [0.0, 0.0, pi/4, 0, pi/2, 0]
        right = None
        locations[name] = (gantry, left, right)

        name = 'bin4'
        gantry = [5.0, -1.2, 0.]
        left = [0.0, 0.0, pi/4, 0, pi/2, 0]
        right = None
        locations[name] = (gantry, left, right)

        name = 'shelf1'
        gantry = [2.5, -2.3, 0.]
        left = [-pi/2, -pi, -pi/2, -pi/2, 0, 0]
        right = None
        locations[name] = (gantry, left, right)

        name = 'standby'
        gantry = None
        left = [0.0, -pi/4, pi/2, -pi/4, pi/2, 0]
        right = [pi, -pi/4, pi/2, -pi/4, pi/2, 0]
        locations[name] = (gantry, left, right)

        name = 'agv1'
        gantry = [-0.6, -6.9, 0]
        left = [0.0, 0.0, pi/4, 0, pi/2, 0]
        right = None
        locations[name] = (gantry, left, right)

        name = 'agv2'
        gantry = [0.6, 6.9, pi]
        left = [0.0, 0.0, pi/4, 0, pi/2, 0]
        right = None
        locations[name] = (gantry, left, right)

        self.locations = locations

    def goto_preset_location(self, location_name):
        group = self.groups['Full_Robot']
        gantry, left, right = self.locations[location_name]
        location_pose = group.get_current_joint_values()

        if gantry:
            location_pose[:3] = gantry
        if left:
            location_pose[3:-6] = left
        if right:
            location_pose[-6:] = right

        # If the robot controller reports a path tolerance violation,
        # this will automatically re-attempt the motion
        MAX_ATTEMPTS = 5
        attempts = 0
        while not group.go(location_pose, wait=True):
            attempts += 1
            assert(attempts < MAX_ATTEMPTS)

    def move_part(self, part, target, part_location, agv):
        # This example only uses the left arm
        group = self.groups['Left_Arm']
        gm = GripperManager(ns='/ariac/gantry/left_arm/gripper/')

        near_pick_pose = copy.deepcopy(part.pose)
        pick_pose = copy.deepcopy(part.pose)
        place_pose = copy.deepcopy(target.pose)

        near_pick_pose.position.z += 0.1
        pick_pose.position.z += 0.015
        place_pose.position.z += 0.1

        self.goto_preset_location(part_location)
        gm.activate_gripper()

        path = [near_pick_pose, pick_pose]
        self.cartesian_move(group, path)

        num_attempts = 0
        MAX_ATTEMPTS = 20
        while not gm.is_object_attached() and num_attempts < MAX_ATTEMPTS:
            num_attempts += 1
            rospy.sleep(0.1)

        if not gm.is_object_attached():
            self.goto_preset_location(part_location)
            self.goto_preset_location('standby')
            self.goto_preset_location('start')
            return False

        if 'shelf' in part_location:
            self.goto_preset_location(part_location)
        self.goto_preset_location('standby')
        self.goto_preset_location('start')
        self.goto_preset_location(agv)

        path = [place_pose]
        self.cartesian_move(group, path)

        gm.deactivate_gripper()

        self.goto_preset_location('standby')
        self.goto_preset_location('start')
        return True

    def cartesian_move(self, group, waypoints):
        (plan, fraction) = group.compute_cartesian_path(waypoints, 0.01, 0.0)
        group.execute(plan, wait=True)

class GripperManager():
    def __init__(self, ns):
        self.ns = ns

    def activate_gripper(self):
        rospy.wait_for_service(self.ns + 'control')
        rospy.ServiceProxy(self.ns + 'control', VacuumGripperControl)(True)

    def deactivate_gripper(self):
        rospy.wait_for_service(self.ns + 'control')
        rospy.ServiceProxy(self.ns + 'control', VacuumGripperControl)(False)

    def is_object_attached(self):
        status = rospy.wait_for_message(self.ns + 'state', VacuumGripperState)
        return status.attached

def submit_shipment(agv, name):
    rospy.wait_for_service('/ariac/' + agv)
    rospy.ServiceProxy('/ariac/' + agv, AGVControl)(name)

def get_order():
    order = rospy.wait_for_message('/ariac/orders', Order)
    return order

def get_part_type_location(part):
    if part.type == 'piston_rod_part_blue':
        reachable_location = 'bin4'
    elif part.type == 'gear_part_green':
        reachable_location = 'bin3'

    assert(reachable_location), "This implementation only reaches shelves/bins"
    return reachable_location

def get_parts_from_cameras():
    tf_buffer = tf2_ros.Buffer()
    tf_listener = tf2_ros.TransformListener(tf_buffer)

    # wait for all cameras to be broadcasting
    all_topics = rospy.get_published_topics()
    camera_topics = [t for t, _ in all_topics if '/ariac/logical_camera' in t]
    for topic in camera_topics:
        rospy.wait_for_message(topic, LogicalCameraImage)

    camera_frame_format = r"logical_camera_[0-9]+_(\w+)_[0-9]+_frame"
    all_frames = yaml.safe_load(tf_buffer.all_frames_as_yaml()).keys()
    part_frames = [f for f in all_frames if re.match(camera_frame_format, f)]

    objects = []
    for frame in part_frames:
        try:
            world_tf = tf_buffer.lookup_transform(
                'world',
                frame,
                rospy.Time(),
                rospy.Duration(0.1)
            )
            ee_tf = tf_buffer.lookup_transform(
                frame,
                'left_ee_link',
                rospy.Time(),
                rospy.Duration(0.1)
            )
        except (tf2_ros.LookupException, tf2_ros.ExtrapolationException) as e:
            continue

        # remove stale transforms
        tf_time = rospy.Time(
            world_tf.header.stamp.secs,
            world_tf.header.stamp.nsecs
        )
        if rospy.Time.now() - tf_time > rospy.Duration(1.0):
            continue

        model = Model()
        model.type = re.match(camera_frame_format, frame).group(1)
        model.pose.position = world_tf.transform.translation
        model.pose.orientation = ee_tf.transform.rotation
        objects.append(model)
    return objects

def get_target_world_pose(target, agv):
    tf_buffer = tf2_ros.Buffer()
    tf_listener = tf2_ros.TransformListener(tf_buffer)
    tf_broadcaster = tf2_ros.StaticTransformBroadcaster()

    tf_msg = TransformStamped()
    tf_msg.header.frame_id = 'kit_tray_1' if agv == 'agv1' else 'kit_tray_2'
    tf_msg.header.stamp = rospy.Time()
    tf_msg.child_frame_id = 'target_frame'
    tf_msg.transform.translation = target.pose.position
    tf_msg.transform.rotation = target.pose.orientation

    for _ in range(5):
        tf_broadcaster.sendTransform(tf_msg)

    # tf lookup fails occasionally, this automatically retries the lookup
    MAX_ATTEMPTS = 10
    attempts = 0
    while attempts < MAX_ATTEMPTS:
        try:
            world_target_tf = tf_buffer.lookup_transform(
                'world',
                'target_frame',
                rospy.Time(),
                rospy.Duration(0.1)
            )
            ee_target_tf = tf_buffer.lookup_transform(
                'target_frame',
                'left_ee_link',
                rospy.Time(),
                rospy.Duration(0.1)
            )
            break
        except:
            continue

    world_target = copy.deepcopy(target)
    world_target.pose.position = world_target_tf.transform.translation
    world_target.pose.orientation = ee_target_tf.transform.rotation
    return world_target

def connect_callbacks(comp_class):
    rospy.loginfo("connect_callbacks")

    comp_state_sub = rospy.Subscriber("ariac/competition_state", String, comp_class.comp_state_callback)
    order_sub = rospy.Subscriber("/ariac/orders", Order, comp_class.order_callback)
    gantry_state_sub = rospy.Subscriber("/ariac/gantry/gantry_controller/state", JointTrajectoryControllerState, comp_class.gantry_joint_state_callback)
    arm1_joint_state_sub = rospy.Subscriber("/ariac/gantry/left_arm_controller/state", JointTrajectoryControllerState, comp_class.arm_1_joint_state_callback)
    arm2_joint_state_sub = rospy.Subscriber("/ariac/gantry/right_arm_controller/state", JointTrajectoryControllerState, comp_class.arm_2_joint_state_callback)
    gripper1_state_sub = rospy.Subscriber("/ariac/gantry/left_arm/gripper/state", VacuumGripperState, comp_class.arm_1_gripper_state_callback)
    gripper2_state_sub = rospy.Subscriber("/ariac/gantry/right_arm/gripper/state", VacuumGripperState, comp_class.arm_2_gripper_state_callback)

def control_agv(shipment_type, agv_num):
    if agv_num not in (1,2):
        raise ValueError('agv_num must be 1 or 2')

    rospy.loginfo("Waiting for agv{} control to be ready...".format(agv_num))
    name = '/ariac/agv{}'.format(agv_num)
    rospy.wait_for_service(name)
    rospy.loginfo("agv{} control is now ready.".format(agv_num))
    rospy.loginfo("Requesting agv control...")

    try:
        agv_control = rospy.ServiceProxy(name, AGVControl)
        response = agv_control(shipment_type)
    except rospy.ServiceException as exc:
        rospy.logerr("Failed to control the agv: %s" % exc)
        return False
    if not response.success:
        rospy.logerr("Failed to control the agv: %s" % response)
    else:
        rospy.loginfo("agv controlled successfully")
    return response.success

def submit_shipment(shipment_type, agv_num):
    if agv_num not in (1,2):
        raise ValueError('agv_num must be 1 or 2')

    rospy.loginfo("Waiting for submit shipment to be ready...".format(agv_num))
    name = '/ariac/submit_shipment'
    rospy.wait_for_service(name)
    rospy.loginfo("submit_shipment is now ready.")
    rospy.loginfo("Requesting shipment")

    try:
        submit_shipment = rospy.ServiceProxy(name, SubmitShipment)
        response = submit_shipment(destination_id=str(agv_num), shipment_type=shipment_type)
    except rospy.ServiceException as exc:
        rospy.logerr("Failed to submit shipment: %s" % exc)
        return False
    if not response.success:
        rospy.logerr("Failed to submit shipment: %s" % response)
    else:
        rospy.loginfo("shipment submitted successfully")
    return response.success


import os
import json
import urllib3

class SmsClient:

    def __init__(self):
        self.host = os.environ.get('SMS_API')
        self.sender_id = os.environ.get('SMS_SENDER_ID')
        self.api_key = os.environ.get('SMS_API_KEY')
        self.api_token = os.environ.get('SMS_API_TOKEN')
        self.http_client = urllib3.PoolManager()

    def send_sms(self, to, message):
        url = "{}?sendsms&apikey={}&apitoken={}&type=sms&from={}&to={}&text={}&scheduledate=2020-09-21%2009:13:05&route=0"\
            .format(self.host, self.api_key, self.api_token, self.sender_id, to, message)
        response = self.http_client.request('GET', url)
        if response.status == 200:
            return True, response.data
        else:
            print('Error in send sms {}'.format(response.data))
            return False, response.data


# %%
import sys
import os
import centernet

# sys.path.append("TrackingSort")
from sort import Sort
from lib import VisTrack, show_video, create_video

import numpy as np
import PIL.Image
import cv2
import matplotlib.pyplot as plt
from tqdm.auto import tqdm
import shutil
# %%
video_file = "data-input/video.mp4"
show_video(video_file)

# %%
# Default: num_classes=80
obj = centernet.ObjectDetection(num_classes=80)
# num_classes=80 and weights_path=None: Pre-trained COCO model will be loaded.
obj.load_weights(weights_path=None)

# %%
# EXAMPLE IMAGE
p_img = PIL.Image.open('data-input/img.jpeg')
img = cv2.cvtColor(np.array(p_img), cv2.COLOR_BGR2RGB)

boxes, classes, scores = obj.predict(img)

# Initialize the vision tracker
vt = VisTrack()
vt.draw_bounding_boxes(p_img, boxes, classes, scores)

# %%
##### PREDICT ON VIDEO ##########
vidcap = cv2.VideoCapture(video_file)
fps = vidcap.get(cv2.CAP_PROP_FPS)
length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))

folder_out = "Track"
if os.path.exists(folder_out):
    shutil.rmtree(folder_out)
os.makedirs(folder_out)

draw_imgs = []

# Tracking algorithm
sort = Sort(max_age=1, min_hits= 3, iou_threshold = 0.3)

pbar = tqdm(total = length)
i = 0
while True:
    ret, frame = vidcap.read()
    if not ret:
        break

    boxes, classes, scores = obj.predict(frame)
    detections_in_frame = len(boxes)
    if detections_in_frame:
        # centernet will do detection in all COCO classes. "person" is class 0
        idxs = np.where(classes == 0)[0]
        boxes = boxes[idxs]
        scores = scores[idxs]
        classes = classes[idxs]
    else:
        boxes = np.empty((0,5))

    dets = np.hstack((boxes, scores[:, np.newaxis]))

    #Update Sort algorithm to track bounding boxes using Kalman Filter
    res = sort.update(dets)

    boxes_track = res[:,:-1]
    boxes_id = res[:, -1].astype(int)

    p_frame = PIL.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
    if detections_in_frame:
        p_frame = vt.draw_bounding_boxes(p_frame, boxes_track, boxes_id, scores)
    p_frame.save(os.path.join(folder_out, f"{i:03d}.png"))

    i+=1
    pbar.update(1)
# %%
############# CREATE THE VIDEO ####################
track_video_file = 'tracking'

def crear_video(folder_out, nombre_video, fps=25):
    # Obtener la lista de archivos en la carpeta
    archivos = os.listdir(folder_out)
    archivos.sort()  # Ordenar los archivos en orden alfabético

    # Leer la primera imagen para obtener el tamaño del video
    ruta_primera_imagen = os.path.join(folder_out, archivos[0])
    imagen = cv2.imread(ruta_primera_imagen)
    alto, ancho, _ = imagen.shape

    # Crear el objeto VideoWriter
    ruta_video = nombre_video + ".avi"
    video = cv2.VideoWriter(ruta_video, cv2.VideoWriter_fourcc(*"MJPG"), fps, (ancho, alto))

    # Recorrer todas las imágenes y agregarlas al video
    for archivo in archivos:
        ruta_imagen = os.path.join(folder_out, archivo)
        imagen = cv2.imread(ruta_imagen)
        video.write(imagen)

    # Liberar el objeto VideoWriter y mostrar un mensaje de finalización
    video.release()
    print("Se ha creado el video:", ruta_video)

crear_video(folder_out, track_video_file, fps = fps)


import discord
import os
import http.client
import json

connection = http.client.HTTPConnection('api.football-data.org')
headers = { 'X-Auth-Token': os.getenv('FUT_APITOKEN') }
connection.request('GET', '/v2/teams/586', None, headers )
response = connection.getresponse().read().decode()

arquivo = open('file1.txt', 'w')
arquivo.write(response)
arquivo.close()

print (response)

client = discord.Client()

@client.event
async def on_ready():
  print('Logado como: {0.user}' .format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
    return

  if message.content.startswith('-coe'):
    await message.channel.send('Salve menozada botzao aqui B)')
  if message.content.startswith('-ajuda'):
    await message.channel.send('Tamo trabalhando ainda menor, novidades em breve!')

#Inicia o bot e pega o token através de uma variável do ambiente
client.run(os.getenv('TOKEN'))


import pandas as pd
import itertools
from mlpipeline import iterator
from tqdm import tqdm

def _load_data_to_pd(file_path):
    return pd.read_json(file_path, orient='index')

def function_filter(df, function_filter):
    if function_filter is not None:
        df_functions = df[df['trigger_functions'].apply(lambda x: x is not None and function_filter in x)]
        df = df[df['dialogue_id'].isin(df_functions['dialogue_id'])]
    return df

class LoadDatasetUtterance:
    COMBINED_FUNCTIONS = 1
    TRIGGER_FUNCTIONS = 2
    RESPONSE_FUNCTIONS = 3

    def __init__(self, function_set, function_filter=None):
        self.function_filter = function_filter
        self.function_set = function_set

    def __call__(self, file_path):
        df = _load_data_to_pd(file_path)

        df = df[df['by'] == 'user']
        df = function_filter(df, self.function_filter)
        df = df[~df['response_functions'].isnull() | ~df['trigger_functions'].isnull()]

        if self.function_set == self.COMBINED_FUNCTIONS:
            def get_list(x): return x if x is not None else []
            df.loc[:, "functions"] = pd.Series([list(itertools.chain(get_list(row['trigger_functions']),
                                                                     get_list(row['response_functions'])))
                                                for _, row in df.iterrows()], index=df.index)
            df = df.loc[:, ['utterance', 'functions']]
        elif self.function_set == self.TRIGGER_FUNCTIONS:
            df = df.loc[:, ['utterance', 'trigger_functions']].rename(
                columns={'trigger_functions': 'functions'})
            df['functions'] = df['functions'].apply(lambda x: ['None'] if x is None else x)
        elif self.function_set == self.RESPONSE_FUNCTIONS:
            df = df.loc[:, ['utterance', 'response_functions']].rename(
                columns={'response_functions': 'functions'})
        used_labels = df['functions']
        return df, set(list(itertools.chain(*used_labels[~used_labels.isnull()].to_list())))

class LoadDatasetDialogue:
    def __init__(self, function_filter=None):
        self.function_filter = function_filter

    def __call__(self, file_path):
        self.df = _load_data_to_pd(file_path)
        self.df = function_filter(self.df, self.function_filter)
        dialogue_ids = self.df['dialogue_id'].unique()
        dialogues = []
        for dialogue_id in iterator(tqdm(dialogue_ids, "Loading dialogues: "), 20):
            dialogues.append(self._process_dialogue(dialogue_id))
        return dialogues

    def _process_dialogue(self, dialogue_id):
        function_map = {}
        dialogue_df = self.df[self.df['dialogue_id'] == dialogue_id]
        dialogue_df = dialogue_df[~dialogue_df['reply_to'].isna()]
        dialogue_df = dialogue_df.set_index('turn_id')
        for idx, utterance in dialogue_df[dialogue_df['by'] == 'user'].iterrows():
            functions = get_functions_from_utterance(utterance)
            for function in functions:
                assert function not in function_map
                function_map[function] = idx
        return (dialogue_df, function_map)

def get_functions_from_utterance(utterance):
    functions = utterance[['response_functions', 'trigger_functions']].dropna()
    functions = list(itertools.chain(*functions.to_list()))
    return functions


import pygame
import math
from pygame.locals import *
import langton

RESOLUTION = (1000, 500)
GAME_GRID = (100, 50)
HOW_FAST = 1
WHITE = (255,255,255)
PURPLE = (135,95,154)
BLACK = (0,0,0)

class Game():

    def __init__(self, x, y):
        self.map = langton.Map(x, y)
        self.block_rect = (RESOLUTION[0]/x, RESOLUTION[1]/y)
        self.iterations = 0

    def setup(self):
        pygame.init()

        ant1 = langton.Ant(self.map.get_num_ants())
        self.map.add_ant(ant1, self.map.x//2 , self.map.y//2)
        self.GAME_END = False
        self.game_display = pygame.display.set_mode(RESOLUTION, RESIZABLE)
        self.clock = pygame.time.Clock()

        pygame.display.set_caption("WOJTEK CIOTA")

    def event_loop(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.GAME_END = True

            if event.type == pygame.MOUSEBUTTONDOWN:
                print(event)
                mouse_x = int(event.pos[0] // self.block_rect[0])
                mouse_y = int(event.pos[1] // self.block_rect[1])
                print(mouse_x, mouse_y)
                self.map.add_ant(langton.Ant(self.map.get_num_ants()), mouse_x, mouse_y)

    def draw_area(self):
        self.block_rect = (RESOLUTION[0] / self.map.x, RESOLUTION[1] / self.map.y)
        for x, row in enumerate(self.map.get_area()):
            for y, col in enumerate(row):
                x_pos = self.block_rect[0] * x
                y_pos = self.block_rect[1] * y
                height = self.block_rect[1] + 1
                width = self.block_rect[0] + 1
                if col[0] == 0:
                    if len(col) > 1:
                        pass
                        pygame.draw.rect(self.game_display, (255,100,200), (x_pos, y_pos, height, width))
                    else:
                        pass
                    pygame.draw.rect(self.game_display, WHITE, (x_pos, y_pos, height, width))
                else:
                    random = (((math.sqrt(col[0]*2))**col[0])* 10)%255
                    random_color = ((random* col[0])%255, (random*20)%255, (random*random)%255)
                    pygame.draw.rect(self.game_display, BLACK, (x_pos, y_pos, height, width))

    def main_loop(self):
        self.event_loop()

        for iter in range(HOW_FAST):
            self.map.update_ants()

        self.game_display.fill(WHITE)

        self.iterations += 1
        self.draw_area()

        self.clock.tick(60)
        self.message(str(self.clock.get_fps()//1),0)
        self.message(str(self.iterations),1)

        pygame.display.update()

    def is_end(self):
        return self.GAME_END

    def quit(self):
        pygame.quit()

    def message(self, text, where):

        default_font = pygame.font.get_default_font()
        font = pygame.font.Font('freesansbold.ttf', 22)
        shifting = font.render('', False, BLACK).get_rect()[3] * where
        text_surface = font.render(text, True, BLACK)

        self.game_display.blit(text_surface, (0, shifting))

if __name__ == '__main__':

    game = Game(GAME_GRID[0],GAME_GRID[1])

    game.setup()
    while 1:
        game.main_loop()

        if game.is_end():
            break
    game.quit()


# https://projecteuler.net/problem=82
from collections import defaultdict
import PySimpleGUI as sg
class graph:
    def __init__(self):
        self.nodes=set()
        self.edges=defaultdict(list)
        self.dist={}

    def add_node(self,value):
        self.nodes.add(value)

    def add_edge(self,from_node, to_node ):
        self.edges[from_node].append(to_node)

def dikstra(initial,graph):
    visited=[]
    valid=[initial]
    i=0
    path={initial:initial}
    while len(valid)>0:
        i+=1
        current = min(valid,key=lambda x: path[x])
        visited.append(current)
        valid.remove(current)
        for node in graph.edges[current]:
            if node not in valid and node not in visited:
                valid.append(node)
            else:
                pass
            if node not in path:
                path[node]=path[current]+node
            else:
                if path[node]>(path[current]+node):
                    path[node]=path[current]+node
    return path

matrix=[]
fi=open('input.txt','r')
for line in fi:
    line=line.split(',')                          #Matrix1 is used to calculate
    line=list(map(int, line))                     #d{} which counts repeated node's frequency
    matrix.append(line)
fi.close()

d={}
for _ in range(80):
    for node in range(80):
        jup=0
        for k in matrix:
            l=k.count(matrix[_][node])
            jup+=l
        if jup > 1:
            d[matrix[_][node]]=jup

matrix2=[]
f2=open('input.txt','r')
for lin in f2:
    lin=lin.split(',')
    lin=list(map(int,lin))                        #Matrix2 gets rid of repeated nodes
    for p in range(len(lin)):
        if lin[p] in d:
            d[lin[p]]-=1
            lin[p]=lin[p]+ ((d[lin[p]]+1)/10000)
    matrix2.append(lin)

G=graph()

#. Adding edges of of non boundry nodes os matrix2
for _ in range(1,len(matrix2)-1):
    for node in range(0,len(matrix2[_])-1):
        G.add_node(matrix2[_][node])
        G.add_edge(matrix2[_][node],matrix2[_][node+1])
        G.add_edge(matrix2[_][node],matrix2[_+1][node])
        G.add_edge(matrix2[_][node],matrix2[_-1][node])

for node in range(1,len(matrix2)-1):
    G.add_node(matrix2[0][node])
    G.add_edge(matrix2[0][node],matrix2[0][node+1])    #Adding edges for nodes in first row
    G.add_edge(matrix2[0][node],matrix2[1][node])

for node in range(1,len(matrix2)-1):
    G.add_node(matrix2[79][node])
    G.add_edge(matrix2[79][node],matrix2[79][node+1])  #Adding edges for nodes in last row
    G.add_edge(matrix2[79][node],matrix2[78][node])

for node in range(1,len(matrix2)-1):
    G.add_node(matrix2[node][79])
    G.add_edge(matrix2[node][79],matrix2[node-1][79])  #Adding edges for nodes in last column
    G.add_edge(matrix2[node][79],matrix2[node+1][79])

#Adding edges for all the corner nodes
G.add_node(matrix2[0][0])
G.add_edge(matrix2[0][0],matrix2[0][1])
G.add_edge(matrix2[0][0],matrix2[1][0])

G.add_node(matrix2[79][0])
G.add_edge(matrix2[79][0],matrix2[79][1])
G.add_edge(matrix2[79][0],matrix2[78][0])

G.add_node(matrix2[79][79])
G.add_edge(matrix2[79][79],matrix2[78][79])

G.add_node(matrix2[0][79])
G.add_edge(matrix2[0][79],matrix2[1][79])

left_column=[]
right_column=[]
for node in range(len(matrix2)):
    left_column.append(matrix2[node][0])
    right_column.append(matrix2[node][79])

lowest=425185   #this number represent solution for problem 81, hence the answer must be less then this, you can even use lowest = sys.maxint
i=0
for l_cell in left_column:
    i+=1
    path=dikstra(l_cell,G)
    r_cell=min(right_column,key= lambda x: path[x])
    if path[r_cell]<lowest:
        lowest=path[r_cell]
    sg.OneLineProgressMeter('Time Meter', i, len(matrix2), 'key')  # Meter for Time consumption

print(int(lowest))


import argparse
import datetime
import json
import random
import time
from pathlib import Path

import numpy as np
import torch
from torch.utils.data import DataLoader, DistributedSampler

import datasets
import util.misc as utils
from datasets import build_dataset, get_coco_api_from_dataset
from engine import evaluate, train_one_epoch
from models import build_model

from torch.nn.parameter import Parameter

import tqdm

from getpass import getuser
from socket import gethostname

# this is a fake commit
def get_args_parser():
    parser = argparse.ArgumentParser('Set transformer detector', add_help=False)
    parser.add_argument('--lr', default=1e-4, type=float)
    parser.add_argument('--lr_backbone', default=1e-5, type=float)
    parser.add_argument('--batch_size', default=2, type=int)
    parser.add_argument('--weight_decay', default=1e-4, type=float)
    parser.add_argument('--epochs', default=300, type=int)
    parser.add_argument('--lr_drop', default=200, type=int)
    parser.add_argument('--clip_max_norm', default=0.1, type=float,
                        help='gradient clipping max norm')

    # Model parameters
    parser.add_argument('--frozen_weights', type=str, default=None,
                        help="Path to the pretrained model. If set, only the mask head will be trained")
    # * Backbone
    parser.add_argument('--backbone', default='resnet50', type=str,
                        help="Name of the convolutional backbone to use")
    parser.add_argument('--dilation', action='store_true',
                        help="If true, we replace stride with dilation in the last convolutional block (DC5)")
    parser.add_argument('--position_embedding', default='sine', type=str, choices=('sine', 'learned'),
                        help="Type of positional embedding to use on top of the image features")

    # * Transformer
    parser.add_argument('--enc_layers', default=6, type=int,
                        help="Number of encoding layers in the transformer")
    parser.add_argument('--dec_layers', default=6, type=int,
                        help="Number of decoding layers in the transformer")
    parser.add_argument('--dim_feedforward', default=2048, type=int,
                        help="Intermediate size of the feedforward layers in the transformer blocks")
    parser.add_argument('--hidden_dim', default=256, type=int,
                        help="Size of the embeddings (dimension of the transformer)")
    parser.add_argument('--dropout', default=0.1, type=float,
                        help="Dropout applied in the transformer")
    parser.add_argument('--nheads', default=8, type=int,
                        help="Number of attention heads inside the transformer's attentions")
    parser.add_argument('--num_queries', default=100, type=int,
                        help="Number of query slots")
    parser.add_argument('--pre_norm', action='store_true')

    # * Segmentation
    parser.add_argument('--masks', action='store_true',
                        help="Train segmentation head if the flag is provided")

    # Loss
    parser.add_argument('--no_aux_loss', dest='aux_loss', action='store_false',
                        help="Disables auxiliary decoding losses (loss at each layer)")
    # * Matcher
    parser.add_argument('--set_cost_class', default=1, type=float,
                        help="Class coefficient in the matching cost")
    parser.add_argument('--set_cost_bbox', default=5, type=float,
                        help="L1 box coefficient in the matching cost")
    parser.add_argument('--set_cost_giou', default=2, type=float,
                        help="giou box coefficient in the matching cost")
    # * Loss coefficients
    parser.add_argument('--mask_loss_coef', default=1, type=float)
    parser.add_argument('--dice_loss_coef', default=1, type=float)
    parser.add_argument('--bbox_loss_coef', default=5, type=float)
    parser.add_argument('--giou_loss_coef', default=2, type=float)
    parser.add_argument('--eos_coef', default=0.1, type=float,
                        help="Relative classification weight of the no-object class")

    # dataset parameters
    parser.add_argument('--train_image_set', default='train')## add for train on sampled set, train_sampled_PER_CAT_THR_500, ...
    parser.add_argument('--dataset_file', default='coco')
    parser.add_argument('--coco_path', type=str)
    parser.add_argument('--coco_panoptic_path', type=str)
    parser.add_argument('--remove_difficult', action='store_true')

    parser.add_argument('--output_dir', default='',
                        help='path where to save, empty for no saving')
    parser.add_argument('--device', default='cuda',
                        help='device to use for training / testing')
    parser.add_argument('--seed', default=42, type=int)
    parser.add_argument('--resume', default='', help='resume from checkpoint')
    parser.add_argument('--start_epoch', default=0, type=int, metavar='N',
                        help='start epoch')
    parser.add_argument('--eval', action='store_true')
    parser.add_argument('--num_workers', default=2, type=int)

    # distributed training parameters
    parser.add_argument('--world_size', default=1, type=int,
                        help='number of distributed processes')
    parser.add_argument('--dist_url', default='env://', help='url used to set up distributed training')
    parser.add_argument('--sample_reg_loss', default=1e-4, type=float,
                        help="sample_reg_loss")
    parser.add_argument('--sample_topk_ratio', default=1/3., type=float)
    parser.add_argument('--score_pred_net', type=str, default='2layer-fc-256')
    parser.add_argument('--kproj_net', type=str, default='1layer-fc')
    parser.add_argument('--unsample_abstract_number', default=0, type=int,
                        help='unsample_abstract_number')
    parser.add_argument('--pos_embed_kproj', action='store_true',
                        help="add pos embeding for predicting unsampled aggregation attention")
    parser.add_argument('--sampler_lr_drop_epoch', default=1e5, type=int,
                        help='default is not drop')
    parser.add_argument('--reshape_param_group', action='store_true',
                        help="reshape_param_group of loaded state_dict to match with the 3 group setting")
    parser.add_argument('--notload_lr_scheduler', action='store_true',
                        help="notload_lr_scheduler")
    parser.add_argument('--sample_ratio_lower_bound', default=1/3., type=float)
    parser.add_argument('--sample_ratio_higher_bound', default=0.8, type=float)

    return parser

def get_host_info():
    return '{}@{}'.format(getuser(), gethostname())

def main(args):
    utils.init_distributed_mode(args)
    print("git:\n  {}\n".format(utils.get_sha()))

    if args.frozen_weights is not None:
        assert args.masks, "Frozen training is meant for segmentation only"
    print(args)
    print(get_host_info())
    device = torch.device(args.device)

    # fix the seed for reproducibility
    seed = args.seed + utils.get_rank()
    torch.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)
    if args.unsample_abstract_number==0:# unsample_abstract_number not set
        if args.dilation:
            args.unsample_abstract_number=100
        else:
            args.unsample_abstract_number = 30

    model, criterion, postprocessors = build_model(args)
    model.to(device)
    criterion.weight_dict['sample_reg_loss'] = args.sample_reg_loss

    model_without_ddp = model
    if args.distributed:
        model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu])
        model_without_ddp = model.module
    n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
    print('number of params:', n_parameters)

    param_dicts = [
        {"params": [p for n, p in model_without_ddp.named_parameters() if "backbone" not in n and p.requires_grad]},
        {
            "params": [p for n, p in model_without_ddp.named_parameters() if "backbone" in n and p.requires_grad],
            "lr": args.lr_backbone,
        },
    ]
    optimizer = torch.optim.AdamW(param_dicts, lr=args.lr,
                                  weight_decay=args.weight_decay)
    lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, args.lr_drop)

    # dataset_train = build_dataset(image_set='train', args=args)
    dataset_train = build_dataset(image_set=args.train_image_set, args=args)
    dataset_val = build_dataset(image_set='val', args=args)
    # dataset_train.ids = dataset_train.ids[:100]
    # dataset_val.ids = dataset_val.ids[:100]

    if args.distributed:
        sampler_train = DistributedSampler(dataset_train)
        sampler_val = DistributedSampler(dataset_val, shuffle=False)
    else:
        sampler_train = torch.utils.data.RandomSampler(dataset_train)
        sampler_val = torch.utils.data.SequentialSampler(dataset_val)

    batch_sampler_train = torch.utils.data.BatchSampler(
        sampler_train, args.batch_size, drop_last=True)

    data_loader_train = DataLoader(dataset_train, batch_sampler=batch_sampler_train,
                                   collate_fn=utils.collate_fn, num_workers=args.num_workers)
    data_loader_val = DataLoader(dataset_val, args.batch_size, sampler=sampler_val,
                                 drop_last=False, collate_fn=utils.collate_fn, num_workers=args.num_workers)

    if args.dataset_file == "coco_panoptic":
        # We also evaluate AP during panoptic training, on original coco DS
        coco_val = datasets.coco.build("val", args)
        base_ds = get_coco_api_from_dataset(coco_val)
    else:
        base_ds = get_coco_api_from_dataset(dataset_val)

    if args.frozen_weights is not None:
        checkpoint = torch.load(args.frozen_weights, map_location='cpu')
        model_without_ddp.detr.load_state_dict(checkpoint['model'])

    output_dir = Path(args.output_dir)
    if args.resume:
        if args.resume.startswith('https'):
            checkpoint = torch.hub.load_state_dict_from_url(
                args.resume, map_location='cpu', check_hash=True)
        elif not args.resume.endswith('pth'):
            checkpoint = torch.load(args.output_dir+'/checkpoint.pth', map_location='cpu')
        else:
            checkpoint = torch.load(args.resume, map_location='cpu')

        def load_my_state_dict(module, state_dict):

            own_state = module.state_dict()
            for name, param in state_dict.items():
                if name not in own_state:
                    continue
                if isinstance(param, Parameter):
                    # backwards compatibility for serialized parameters
                    param = param.data
                own_state[name].copy_(param)

        # model_without_ddp.load_state_dict(checkpoint['model'])
        load_my_state_dict(model_without_ddp, checkpoint['model'])
        if not args.eval and 'optimizer' in checkpoint and 'lr_scheduler' in checkpoint and 'epoch' in checkpoint:
            try:
                optimizer.load_state_dict(checkpoint['optimizer'])
                if not args.notload_lr_scheduler:
                    lr_scheduler.load_state_dict(checkpoint['lr_scheduler'])
                args.start_epoch = checkpoint['epoch'] + 1
            except:
                print('skip loading optimizer and other training settings, supposed to be initing from trained model, but not resuming training')

    if args.eval:
        test_stats, coco_evaluator = evaluate(model, criterion, postprocessors,
                                              data_loader_val, base_ds, device, args.output_dir, args.sample_topk_ratio)
        if args.output_dir:
            utils.save_on_master(coco_evaluator.coco_eval["bbox"].eval, output_dir / "eval.pth")
        return

    print("Start training")
    start_time = time.time()
    best_ap = 0.
    lr_scheduler.step(epoch=args.start_epoch)
    for epoch in tqdm.tqdm(range(args.start_epoch, args.epochs)):
        if args.distributed:
            sampler_train.set_epoch(epoch)
        lr_scheduler.step(epoch=epoch)
        if epoch >= args.sampler_lr_drop_epoch:
            optimizer.param_groups[0]['lr'] *= 0.1
        train_stats = train_one_epoch(
            model, criterion, data_loader_train, optimizer, device, epoch, args.sample_ratio_lower_bound,args.sample_ratio_higher_bound,
            args.clip_max_norm)

        test_stats_all_sample_ratio = []
        sample_ratios = [0.333, 0.5, 0.65, 0.8]
        for sample_ratio in sample_ratios:
            test_stats, coco_evaluator = evaluate(
                model, criterion, postprocessors, data_loader_val, base_ds, device, args.output_dir, sample_ratio
            )
            test_stats_all_sample_ratio.append(test_stats)
        log_stats = {**{f'train_{k}': v for k, v in train_stats.items()},
                     **{f'test_ratio_{sample_ratios[0]}_{k}': v for k, v in test_stats_all_sample_ratio[0].items()},
                     **{f'test_ratio_{sample_ratios[1]}_{k}': v for k, v in test_stats_all_sample_ratio[1].items()},
                     **{f'test_ratio_{sample_ratios[2]}_{k}': v for k, v in test_stats_all_sample_ratio[2].items()},
                     **{f'test_ratio_{sample_ratios[3]}_{k}': v for k, v in test_stats_all_sample_ratio[3].items()},
                     'epoch': epoch,
                     'n_parameters': n_parameters,
                     'lrs':[optimizer.param_groups[i]['lr']for i in range(len(optimizer.param_groups))]}

        if args.output_dir:
            checkpoint_paths = [output_dir / 'checkpoint.pth']
            # extra checkpoint before LR drop and every 100 epochs
            if (epoch + 1) % args.lr_drop == 0 or (epoch + 1) % 100 == 0:
                checkpoint_paths.append(output_dir / f'checkpoint{epoch:04}.pth')
            if test_stats_all_sample_ratio[2]['coco_eval_bbox'][0] > best_ap:
                best_ap = test_stats['coco_eval_bbox'][0]
                checkpoint_paths.append(output_dir / f'checkpoint_best.pth')
            for checkpoint_path in checkpoint_paths:
                utils.save_on_master({
                    'model': model_without_ddp.state_dict(),
                    'optimizer': optimizer.state_dict(),
                    'lr_scheduler': lr_scheduler.state_dict(),
                    'epoch': epoch,
                    'args': args,
                }, checkpoint_path)

        if args.output_dir and utils.is_main_process():
            with (output_dir / "log.txt").open("a") as f:
                f.write(json.dumps(log_stats) + "\n")

            # for evaluation logs
            if coco_evaluator is not None:
                (output_dir / 'eval').mkdir(exist_ok=True)
                if "bbox" in coco_evaluator.coco_eval:
                    filenames = ['latest.pth']
                    if epoch % 50 == 0:
                        filenames.append(f'{epoch:03}.pth')
                    for name in filenames:
                        torch.save(coco_evaluator.coco_eval["bbox"].eval,
                                   output_dir / "eval" / name)

    total_time = time.time() - start_time
    total_time_str = str(datetime.timedelta(seconds=int(total_time)))
    print('Training time {}'.format(total_time_str))

if __name__ == '__main__':
    parser = argparse.ArgumentParser('DETR training and evaluation script', parents=[get_args_parser()])
    args = parser.parse_args()
    if args.output_dir:
        Path(args.output_dir).mkdir(parents=True, exist_ok=True)
    main(args)


from django.db import models
from django.db.models import signals
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.conf import settings
from django.core.mail import send_mail
import sendgrid
import os
from sendgrid.helpers.mail import *

class Contact(models.Model):
    email = models.EmailField()
    phone_number = models.CharField(max_length = 50, blank = True, null = True, default = 'No phone number')
    subject = models.CharField(max_length = 255, blank = True, null = True, default = 'No subject')
    body = models.TextField(blank = True, null = True, default = '\n')
    created_date = models.DateTimeField(auto_now = True, auto_now_add=False)
    updated_date = models.DateTimeField(auto_now= False, auto_now_add = True)

    class Meta:
        verbose_name = 'Email'
        verbose_name_plural = 'Emails'

    def send_contact_email(instance, **kwargs):
        if instance.body is '':
            instance.body = '\n'
        if instance.subject is '':
            instance.subject = 'No subject'
        if instance.phone_number is '':
            instance.phone_number = 'Not filled'

        sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
        from_email = Email(instance.email)
        subject = instance.subject if not '' else 'No subject'
        to_email = Email("app@wallpaperapp.uk")
        content = Content("text/plain", instance.body + "\n\nPhone number: " + instance.phone_number)
        mail = Mail(from_email, subject, to_email, content)
        response = sg.client.mail.send.post(request_body=mail.get())
    post_save.connect(send_contact_email, sender=Contact)


import tempfile

import torch
import numpy as np
from livelossplot import PlotLosses
from livelossplot.outputs import MatplotlibPlot
from tqdm import tqdm
from src.helpers import after_subplot

def train_one_epoch(train_dataloader, model, optimizer, loss):
    """
    Performs one train_one_epoch epoch
    """

    if torch.cuda.is_available():
        # YOUR CODE HERE: transfer the model to the GPU
        # HINT: use .cuda()
         model = model.cuda()

    # YOUR CODE HERE: set the model to training mode
    model.train()

    train_loss = 0.0

    for batch_idx, (data, target) in tqdm(
        enumerate(train_dataloader),
        desc="Training",
        total=len(train_dataloader),
        leave=True,
        ncols=80,
    ):
        # move data to GPU
        if torch.cuda.is_available():
            data, target = data.cuda(), target.cuda()

        # 1. clear the gradients of all optimized variables
        # YOUR CODE HERE:
        optimizer.zero_grad()
        # 2. forward pass: compute predicted outputs by passing inputs to the model
        output  = model(data)
        # 3. calculate the loss
        loss_value  = loss(output, target)
        # 4. backward pass: compute gradient of the loss with respect to model parameters
        loss_value.backward()
        # 5. perform a single optimization step (parameter update)
        optimizer.step()

        # update average training loss
        train_loss = train_loss + (
            (1 / (batch_idx + 1)) * (loss_value.data.item() - train_loss)
        )

    return train_loss

def valid_one_epoch(valid_dataloader, model, loss):
    """
    Validate at the end of one epoch
    """

    with torch.no_grad():

        # set the model to evaluation mode
        # YOUR CODE HERE
        model.eval()

        if torch.cuda.is_available():
            model.cuda()

        valid_loss = 0.0
        for batch_idx, (data, target) in tqdm(
            enumerate(valid_dataloader),
            desc="Validating",
            total=len(valid_dataloader),
            leave=True,
            ncols=80,
        ):
            # move data to GPU
            if torch.cuda.is_available():
                data, target = data.cuda(), target.cuda()

            # 1. forward pass: compute predicted outputs by passing inputs to the model
            output  = model(data)
            # 2. calculate the loss
            loss_value  = loss(output, target)

            # Calculate average validation loss
            valid_loss = valid_loss + (
                (1 / (batch_idx + 1)) * (loss_value.data.item() - valid_loss)
            )

    return valid_loss

def optimize(data_loaders, model, optimizer, loss, n_epochs, save_path, interactive_tracking=False):
    # initialize tracker for minimum validation loss
    if interactive_tracking:
        liveloss = PlotLosses(outputs=[MatplotlibPlot(after_subplot=after_subplot)])
    else:
        liveloss = None

    valid_loss_min = None
    logs = {}

    # Learning rate scheduler: setup a learning rate scheduler that
    # reduces the learning rate when the validation loss reaches a
    # plateau
    # HINT: look here:
    # https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate
    scheduler  = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, patience=2, factor=0.1)

    for epoch in range(1, n_epochs + 1):

        train_loss = train_one_epoch(
            data_loaders["train"], model, optimizer, loss
        )

        valid_loss = valid_one_epoch(data_loaders["valid"], model, loss)

        # print training/validation statistics
        print(
            "Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}".format(
                epoch, train_loss, valid_loss
            )
        )

        # If the validation loss decreases by more than 1%, save the model
        if valid_loss_min is None or (
                (valid_loss_min - valid_loss) / valid_loss_min > 0.01
        ):
            print(f"New minimum validation loss: {valid_loss:.6f}. Saving model ...")

            # Save the weights to save_path
            # YOUR CODE HERE
            torch.save(model.state_dict(), save_path)

            valid_loss_min = valid_loss

        # Update learning rate, i.e., make a step in the learning rate scheduler
        # YOUR CODE HERE
        scheduler.step(valid_loss)

        # Log the losses and the current learning rate
        if interactive_tracking:
            logs["loss"] = train_loss
            logs["val_loss"] = valid_loss
            logs["lr"] = optimizer.param_groups[0]["lr"]

            liveloss.update(logs)
            liveloss.send()

def one_epoch_test(test_dataloader, model, loss):
    # monitor test loss and accuracy
    test_loss = 0.
    correct = 0.
    total = 0.

    # set the module to evaluation mode
    with torch.no_grad():

        # set the model to evaluation mode
        # YOUR CODE HERE
        model.eval()

        if torch.cuda.is_available():
            model = model.cuda()

        for batch_idx, (data, target) in tqdm(
                enumerate(test_dataloader),
                desc='Testing',
                total=len(test_dataloader),
                leave=True,
                ncols=80
        ):
            # move data to GPU
            if torch.cuda.is_available():
                data, target = data.cuda(), target.cuda()

            # 1. forward pass: compute predicted outputs by passing inputs to the model
            logits  = model(data)
            # 2. calculate the loss
            loss_value  = loss(logits, target)

            # update average test loss
            test_loss = test_loss + ((1 / (batch_idx + 1)) * (loss_value.data.item() - test_loss))

            # convert logits to predicted class
            # HINT: the predicted class is the index of the max of the logits
            pred  = torch.max(logits.data, 1)[1]

            # compare predictions to true label
            correct += torch.sum(torch.squeeze(pred.eq(target.data.view_as(pred))).cpu())
            total += data.size(0)

    print('Test Loss: {:.6f}\n'.format(test_loss))

    print('\nTest Accuracy: %2d%% (%2d/%2d)' % (
        100. * correct / total, correct, total))

    return test_loss

######################################################################################
#                                     TESTS
######################################################################################
import pytest

@pytest.fixture(scope="session")
def data_loaders():
    from .data import get_data_loaders

    return get_data_loaders(batch_size=50, limit=200, valid_size=0.5, num_workers=0)

@pytest.fixture(scope="session")
def optim_objects():
    from src.optimization import get_optimizer, get_loss
    from src.model import MyModel

    model = MyModel(50)

    return model, get_loss(), get_optimizer(model)

def test_train_one_epoch(data_loaders, optim_objects):

    model, loss, optimizer = optim_objects

    for _ in range(2):
        lt = train_one_epoch(data_loaders['train'], model, optimizer, loss)
        assert not np.isnan(lt), "Training loss is nan"

def test_valid_one_epoch(data_loaders, optim_objects):

    model, loss, optimizer = optim_objects

    for _ in range(2):
        lv = valid_one_epoch(data_loaders["valid"], model, loss)
        assert not np.isnan(lv), "Validation loss is nan"

def test_optimize(data_loaders, optim_objects):

    model, loss, optimizer = optim_objects

    with tempfile.TemporaryDirectory() as temp_dir:
        optimize(data_loaders, model, optimizer, loss, 2, f"{temp_dir}/hey.pt")

def test_one_epoch_test(data_loaders, optim_objects):

    model, loss, optimizer = optim_objects

    tv = one_epoch_test(data_loaders["test"], model, loss)
    assert not np.isnan(tv), "Test loss is nan"

Project-3 Landmark Classification-Tagging for SocialMedia/src/train.py


import pygame
from ui import game_screen, show
from services import deal_cards
from services import check
from services import check_winner
from services import win_count
from ui import show
from ui import game_screen
class Blackjack():
    """Luokka missä peliä pyörittävä silmukka sekä pygame ruudun ja nappien alustus
    """
    def __init__(self):
        """Luokan konstruktori
        """
        self.player = []
        self.dealer = []
        self.player_end = False
        self.dealer_end = False
        self.one_count = False
        self.screen = pygame.display.set_mode([1000, 600])
        self.game_screen = game_screen.Game_screen(self.screen)
        self.py_screen = self.game_screen.show_screen
        self.hit_button = pygame.Rect(500, 500, 70, 70)
        self.stop_button = pygame.Rect(300, 500, 70, 70)
        self.re_button = pygame.Rect(50, 500, 100, 70)
        self.white = (255, 255, 255)
        self.font = pygame.font.SysFont('Corbel', 35)
        pygame.display.set_caption("Blackjack")
        self.score = win_count.Score(self.screen)
        self.running()
    def running(self):
        """Peli silmukka
        """
        self.game_screen.show_screen()
        self.start = deal_cards.Deal()
        self.cards =self.start.start_position()
        show.Draw(self.screen).shows(self.cards[0],self.cards[1 ],self.score)
        while True:
            self.events()

    def events(self):
        """tarkkailee käyttäjän hiiren toimintaa
        """

        for action in pygame.event.get():
            if action.type == pygame.QUIT:
                exit()
            if action.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = action.pos
                if self.hit_button.collidepoint(mouse_pos):
                    if check.Checks(self.cards[0],self.cards[1]).count_player() < 22 and self.player_end is False:
                        self.start.player_hits()
                        hit=self.start.get_hands()
                        show.Draw(self.screen).shows(hit[0],hit[1],self.score)
                if self.stop_button.collidepoint(mouse_pos):
                    self.player_end = True
                    hit = self.start.get_hands()
                    dealer_count = check.Checks(hit[0], hit[1]).count_dealer()
                    if len(self.dealer) < 2 and  self.dealer_end is False:
                        self.dealer_end = True
                        self.start.dealer_hits()
                        hit=self.start.get_hands()
                        show.Draw(self.screen).shows(hit[0],hit[1 ],self.score)
                    if  dealer_count < 16 and dealer_count < 18 :
                        while check.Checks(hit[0], hit[1]).count_dealer() < 16 and check.Checks(hit[0], hit[1]).count_dealer() < 18:
                            self.start.dealer_hits()
                            hit=self.start.get_hands()
                            show.Draw(self.screen).shows(hit[0],hit[1 ],self.score)
                    hit = self.start.get_hands()
                    final_player_hand = check.Checks(hit[0], hit[1]).count_player()
                    final_dealer_hand = check.Checks(hit[0], hit[1]).count_dealer()
                    winner = check_winner.Winner(final_player_hand,final_dealer_hand).get_winner()
                    winner_text = self.font.render(winner,True,self.white)
                    self.screen.blit(winner_text, (700, 250))
                    if self.one_count is False:
                        self.score.win_counter(winner)
                        self.one_count = True
                    self.score.win_count_draw()
                    pygame.display.flip()

                if self.re_button.collidepoint(mouse_pos) and self.dealer_end is True:
                    self.player_end = False
                    self.dealer_end = False
                    self.one_count = False
                    self.game_screen.show_screen()
                    self.player = []
                    self.dealer = []
                    self.start = deal_cards.Deal()
                    self.cards =self.start.start_position()
                    show.Draw(self.screen).shows(self.cards[0],self.cards[1],self.score)


#!/usr/bin/env python

import rospy
from spacetrack import SpaceTrackClient
import time
import schedule
import json

def tleSchedule():
	time.sleep(1)
	getTles()

	schedule.every(4).hours.do(getTles)
	while True:
		schedule.run_pending()
		time.sleep(60)

	# schedule.every().day.at("14:00").do(getTles)
	# while True:
	# 	print("hey")
	# 	schedule.run_pending()
	# 	time.sleep(60)

def getTles():
	catalog_number = 39161
	for line in open('/home/rasmus/catkin_ws/src/antenna_controller/tle/current_satellite.txt','r').readlines():
		catalog_number = int(line)
	st = SpaceTrackClient('rasmustomsen@hotmail.com', '!kK!Ft3-W6sKGa8X')
	tle = st.tle_latest(norad_cat_id=catalog_number, ordinal=1, format='3le').encode("utf-8").strip()
	rospy.loginfo(tle)
	formatTle(tle)

def formatTle(tle):
	tles_string = tle[2:]

	print(tles_string)
	print(len(tles_string.split("\n")))
	with open('/home/rasmus/catkin_ws/src/antenna_controller/tle/tle.txt', 'w') as f:
		f.write(tles_string)

if __name__ == '__main__':
	tleSchedule()

before changing multiple passes to one/antenna_controller/scripts/tle_getter_node.py


## Regularization

## Import packages
import numpy as np
import matplotlib.pyplot as plt
from reg_utils import sigmoid, relu, plot_decision_boundary, initialize_parameters, load_2D_dataset, predict_dec
from reg_utils import compute_cost, predict, forward_propagation, backward_propagation, update_parameters
import sklearn
import sklearn.datasets
import scipy.io
from testCases import *

plt.figure(1)
# %matplotlib inline
plt.rcParams['figure.figsize'] = (7.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

train_X, train_Y, test_X, test_Y = load_2D_dataset()
print(train_X.shape, test_X.shape)

# ## Visualize the data
# plt.scatter(X[0, :], X[1, :], c=Y.ravel().tolist(), s=40, cmap=plt.cm.Sepctral);

plt.figure(2)
## Non-regularized Model
def model(X, Y, learning_rate = 0.3, num_iteration = 30000, print_cost = True, lambd = 0, keep_prob = 1):

    grads = {}
    costs = []
    m = X.shape[1]
    layers_dims = [X.shape[0], 20, 3, 1]

    ## Initialize parameters dictionary
    parameters = initialize_parameters(layers_dims)

    ## Gradient descent LOOP
    for i in range(0, num_iteration):

        ## Forward prop (linear -> relu) -> (linear -> relu) -> (linear -> sigmoid)
        if keep_prob == 1:      # Non-dropout
            a3, cache = forward_propagation(X, parameters)
        elif keep_prob < 1:     # Dropout
            a3, cache = forward_propagation_with_dropout(X, parameters, keep_prob)

        ## Cost func
        if lambd == 0:          # Without L2-Regularization
            cost = compute_cost(a3, Y)
        else:                   # L2 Regularization
            cost = compute_cost_with_regularization(a3, Y, parameters, lambd)

        ## Backward prop
        assert(lambd==0 or keep_prob)

        if lambd==0 or keep_prob==1 :
            grads = backward_propagation(X, Y, cache)
        elif lambd != 0:
            grads = backward_propagation_with_regularization(X, Y, cache, lambd)
        elif keep_prob < 1:
            grads = backward_propagation_with_dropout(X, Y, cache, keep_prob)

        ## Update
        parameters = update_parameters(parameters, grads, learning_rate)

        ## Print Loss
        if print_cost and i % 10000 == 0:
            print("Cost after iteration {}: {}".format(i, cost))
        if print_cost and i % 1000 == 0:
            costs.append(cost)

    plt.plot(costs)
    plt.ylabel('cost')
    plt.xlabel('iterations (x1,000)')
    plt.title("Learning rate =" + str(learning_rate))
    plt.show()

    return parameters

parameters = model(train_X, train_Y)
print("On the training set:")
prediction_train = predict(train_X, train_Y, parameters)
print("On the test set:")
prediction_test = predict(test_X, test_Y, parameters)

def _plot_decision_boundary(model, X, y):

    x_min, x_max = X[0, :].min() -1, X[0, :].max() + 1
    y_min, y_max = X[1, :].min() -1, X[1, :].max() + 1
    h = 0.01

    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

    Z = model(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)

    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
    plt.ylabel('x2')
    plt.xlabel('x1')
    plt.scatter(X[0, :], X[1, :], c=y.ravel().tolist(), cmap=plt.cm.Spectral)
    plt.show()

plt.figure(3)
plt.title("Model without regularization")
axes = plt.gca()
axes.set_xlim([-0.75, 0.40])
axes.set_ylim([-0.75, 0.65])
_plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)
plt.show()

def compute_cost_with_regularization(A3, Y, parameters, lambd):
    m = Y.shape[1]
    W1 = parameters["W1"]
    W2 = parameters["W2"]
    W3 = parameters["W3"]

    cross_entropy_cost = compute_cost(A3, Y)

    L2_regularization_cost = (lambd/2) * (np.sum(np.square(W1))+np.sum(np.square(W2))+np.sum(np.square(W3))) / m

    cost = cross_entropy_cost + L2_regularization_cost

    return cost

A3, Y_assess, parameters = compute_cost_with_regularization_test_case()
print("cost = " + str(compute_cost_with_regularization(A3, Y_assess, parameters, lambd = 0.1)))

def backward_propagation_with_regularization(X, Y, cache, lambd):
    m = X.shape[1]
    (Z1, A1, W1, b1, Z2, A2, W2, b2, Z3, A3, W3, b3) = cache

    dZ3 = A3 - Y

    dW3 = (np.dot(dZ3, A2.T) + lambd * W3) / m
    db3 = 1. / m * np.sum(dZ3, axis=1, keepdims=True)

    dA2 = np.dot(W3.T, dZ3)
    dZ2 = np.multiply(dA2, np.int64(A2 > 0))
    dW2 = 1./m * np.dot(dZ2, A1.T) + lambd*W2/m
    db2 = 1. / m * np.sum(dZ2, axis=1, keepdims=True)

    dA1 = np.dot(W2.T, dZ2)
    dZ1 = np.multiply(dA1, np.int64(A1 > 0))
    dW1 = 1./m * np.dot(dZ1, X.T) + lambd*W1/m
    db1 = 1. / m * np.sum(dZ1, axis=1, keepdims=True)

    gradients = {"dZ3": dZ3, "dW3": dW3, "db3": db3, "dA2": dA2,
                 "dZ2": dZ2, "dW2": dW2, "db2": db2, "dA1": dA1,
                 "dZ1": dZ1, "dW1": dW1, "db1": db1}

    return gradients

X_assess, Y_assess, cache = backward_propagation_with_regularization_test_case()

grads = backward_propagation_with_regularization(X_assess, Y_assess, cache, lambd = 0.7)
print("dW1 = " + str(grads["dW1"]))
print("dW2 = " + str(grads["dW2"]))
print("dW3 = " + str(grads["dW3"]))

## Graph
parameters = model(train_X, train_Y, lambd = 0.7)
print("On the train set:")
predictions_train = predict(train_X, train_Y, parameters)
print("On the test set:")
predictions_test = predict(test_X, test_Y, parameters)

plt.figure(4)
plt.title("Model with L2-regularization")
axes = plt.gca()
axes.set_xlim([-0.75,0.40])
axes.set_ylim([-0.75,0.65])
_plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)

def forward_propagation_with_dropout(X, parameters, keep_prob = 0.5):

    np.random.seed(1)

    W1 = parameters["W1"]
    b1 = parameters["b1"]
    W2 = parameters["W2"]
    b2 = parameters["b2"]
    W3 = parameters["W3"]
    b3 = parameters["b3"]

    Z1 = np.dot(W1, X) + b1
    A1 = relu(Z1)

    D1 = np.random.rand(A1.shape[0], A1.shape[1])
    D1 = D1 < keep_prob
    A1 = np.multiply(D1, A1)
    A1 = A1/keep_prob

    Z2 = np.dot(W2, A1) + b2
    A2 = relu(Z2)

    D2 = np.random.rand(A2.shape[0], A2.shape[1])
    D2 = D2 < keep_prob
    A2 = np.multiply(D2, A2)
    A2 = A2 / keep_prob

    Z3 = np.dot(W3, A2) + b3
    A3 = sigmoid(Z3)

    cache = (Z1, D1, A1, W1, b1, Z2, D2, A2, W2, b2, Z3, A3, W3, b3)

    return A3, cache

X_assess, parameters = forward_propagation_with_dropout_test_case()

A3, cache = forward_propagation_with_dropout(X_assess, parameters, keep_prob=0.7)
print("A3 = " + str(A3))

def backward_propagation_with_dropout(X, Y, cache, keep_prob):
    m = X.shape[1]
    (Z1, D1, A1, W1, b1, Z2, D2, A2, W2, b2, Z3, A3, W3, b3) = cache

    dZ3 = A3 - Y
    dW3 = 1. / m * np.dot(dZ3, A2.T)
    db3 = 1. / m * np.sum(dZ3, axis=1, keepdims=True)
    dA2 = np.dot(W3.T, dZ3)

    dA2 = np.multiply(dA2, D2)
    dA2 = dA2 / keep_prob

    dZ2 = np.multiply(dA2, np.int64(A2 > 0))
    dW2 = 1. / m * np.dot(dZ2, A1.T)
    db2 = 1. / m * np.sum(dZ2, axis=1, keepdims=True)

    dA1 = np.dot(W2.T, dZ2)

    dA1 = np.multiply(dA1, D1)
    dA1 = dA1 / keep_prob

    dZ1 = np.multiply(dA1, np.int64(A1 > 0))
    dW1 = 1. / m * np.dot(dZ1, X.T)
    db1 = 1. / m * np.sum(dZ1, axis=1, keepdims=True)

    gradients = {"dZ3": dZ3, "dW3": dW3, "db3": db3, "dA2": dA2,
                 "dZ2": dZ2, "dW2": dW2, "db2": db2, "dA1": dA1,
                 "dZ1": dZ1, "dW1": dW1, "db1": db1}

    return gradients

X_assess, Y_assess, cache = backward_propagation_with_dropout_test_case()

gradients = backward_propagation_with_dropout(X_assess, Y_assess, cache, keep_prob = 0.8)

print ("dA1 = " + str(gradients["dA1"]))
print ("dA2 = " + str(gradients["dA2"]))

parameters = model(train_X, train_Y, keep_prob = 0.86, learning_rate = 0.3)

print ("On the train set:")
predictions_train = predict(train_X, train_Y, parameters)
print ("On the test set:")
predictions_test = predict(test_X, test_Y, parameters)


import pandas as pd
import math
import scipy.stats as st
pd.set_option('display.max_columns', None)
pd.set_option('display.expand_frame_repr', False)
pd.set_option('display.float_format', lambda x: '%.5f' % x)

def score_up_down_diff(up, down):
    return up - down

def score_average_rating(up, down):
    if up + down == 0:
        return 0
    return up / (up + down)

#WILSON_LOWER_BOUND: bu yöntem için yorumların binary olması lazım. beğendi-beğenmedi- faydalı- değil gibi.
#burada p olasılığını istatistiksel olarak genelleyelim. yani yorum sonsuza dek orda olsa, bu yorumun alacağı
#helpful oyu %95 güvenirlik ile en az kaç alacağını sonuçlarda görebilirim.
def wilson_lower_bound(helpful_yes,helpful_no,confidence=0.95):
    n = helpful_yes + helpful_no
    if n == 0:
        return 0
    z = st.norm.ppf(1-(1-confidence)/2)
    phat = 1.0 * helpful_yes/ n
    return (phat + z * z / (2 * n) - z * math.sqrt((phat * (1 - phat) + z * z / (4 * n)) / n)) / (1 + z * z / n)

####GÖREV 1: Average Rating'i Güncel Yorumlara Göre Hesaplayınız ve Var Olan Average Rating ile Kıyaslayınız.

###################################################
# 1. Veri Setini Okutunuz ve Ürünün Ortalama Puanını Hesaplayınız.
###################################################

df = pd.read_csv("amazon_review.csv")
df.head()
# tek bir ürün var zaten.
df.asin.value_counts()   #B007WTAJTO  4915
df["reviewerID"].nunique()  #4915

# ortalama rating
df["overall"].mean()   #4.5875

#time_based_weighted_average değeri
#Paylaşılan veri setinde kullanıcılar bir ürüne puanlar vermiş ve yorumlar yapmıştır.
#Bu görevde amacımız verilen puanları tarihe göre ağırlıklandırarak değerlendirmek.
#İlk ortalama puan ile elde edilecek tarihe göre ağırlıklı puanın karşılaştırılması gerekmektedir.
df["day_diff"].describe().T

def time_based_weighted_average(dataframe, w1=28, w2=26, w3= 24, w4=22):
    return df.loc[df["day_diff"] <= 30, "overall"].mean() * w1/100 + \
    df.loc[(df["day_diff"] > 30) & (df["day_diff"] <= 90), "overall"].mean() * w2/100 + \
    df.loc[(df["day_diff"] > 90) & (df["day_diff"] <= 180), "overall"].mean() * w3/100 + \
    df.loc[df["day_diff"] > 180, "overall"].mean() * w4/100

time_based_weighted_average(df)  #4.6987
# tarihe bakmadan yaptığımız hesaplama 4.5875. Tarihe göre 4.6987.  görüyoruz ki yeniler yüksek puan vermiş. Güzel:)

#Görev 2: Ürün için Ürün Detay Sayfasında Görüntülenecek 20 Review'i Belirleyiniz.

#Not: total_vote bir yoruma verilen toplam up-down sayısıdır. up, helpful demektir.
#veri setinde helpful_no değişkeni olmadıdığı için helpful_no'yu biz buluyoruz.

df["helpful_no"]= df["total_vote"]- df["helpful_yes"]
df.sort_values("helpful_no", ascending=False).head(20)

df = df[["reviewerName", "overall", "helpful_yes", "helpful_no", "total_vote", "day_diff"]]
df.head()

# score_pos_neg_diff, score_average_rating ve wilson_lower_bound skorlarını hesaplayıp tabloya ekleyelim

# score_pos_neg_diff
df["score_pos_neg_diff"] = df.apply(lambda x: score_up_down_diff(x["helpful_yes"], x["helpful_no"]), axis=1)

# score_average_rating
df["score_average_rating"] = df.apply(lambda x: score_average_rating(x["helpful_yes"], x["helpful_no"]), axis=1)

# wilson_lower_bound
df["wilson_lower_bound"] = df.apply(lambda x: wilson_lower_bound(x["helpful_yes"], x["helpful_no"]), axis=1)
df.head()

# 20 Yorumu Belirleyiniz ve Sonuçları Yorumlayınız.

df[["reviewerName","overall","helpful_yes","helpful_no","total_vote","score_pos_neg_diff","score_average_rating","wilson_lower_bound"]].\
    sort_values(by="wilson_lower_bound",ascending=False).head(20)


from pyomo.environ import *
import pandas as pd
from openpyxl import load_workbook
from os import getcwd
import xlwings as xw
from pandasql import sqldf

#Definir objeto con archivo de excel
xlFile = getcwd() + r"\despacho.xlsx"

# # Definir objeto con información de excel
xl_datos = pd.ExcelFile(xlFile)

#Definir modelo de optimizacion
modelo = ConcreteModel("despacho")

#LECTURA DE DATOS;
df_DEMANDA = xl_datos.parse('DEMANDA_15MIN').set_index(['periodo'])
df_GENERADORES = xl_datos.parse('GENERADORES').set_index(['nombre','periodo'])
iGenerador  = sqldf("SELECT distinct(nombre) FROM df_GENERADORES;", locals())
df_RAMPAS = xl_datos.parse('RAMPAS').set_index(['recurso'])
CR = 2000000

ESTADOINICIAL={}
GENERADORINICIAL={}

#Indices del modelo
modelo.GENERADOR = Set(initialize=iGenerador.nombre)
modelo.PERIODO = Set(initialize=df_DEMANDA.index)
modelo.RAMPAS = Set(initialize=df_RAMPAS.index)

for i in modelo.RAMPAS:
    ESTADOINICIAL[i]=0
    GENERADORINICIAL[i]=0

#Definir variables de decision
modelo.despacho = Var( modelo.GENERADOR, modelo.PERIODO,domain=PositiveReals )
modelo.binaria = Var( modelo.GENERADOR, modelo.PERIODO, domain=Binary )
modelo.arranque = Var( modelo.RAMPAS, modelo.PERIODO, domain=Binary )
modelo.parada = Var( modelo.RAMPAS, modelo.PERIODO, domain=Binary )
modelo.racionamiento = Var (modelo.PERIODO, domain=PositiveReals )

#Definir funcion objetivo
def obj_rule(modelo):
    expr1 = sum ( df_GENERADORES.precio[i,t] * modelo.despacho[i,t]
                for i in modelo.GENERADOR
                 for t in modelo.PERIODO
                )

    expr2 = sum ( df_RAMPAS.costoarranque[i] * modelo.arranque[i,t]
                for i in modelo.RAMPAS
                for t in modelo.PERIODO
                )

    expr3 = sum ( CR * modelo.racionamiento[t]
                for i in modelo.GENERADOR
                for t in modelo.PERIODO
                )
    expr = expr1 + expr2 + expr3
    return expr

modelo.FO = Objective(rule=obj_rule, sense=minimize)

#Definir restricciones
#restriccion Balance demanda
def r1_rule(modelo,t):
    ecuacion1 = sum(modelo.despacho[i,t]+modelo.racionamiento[t]
                    for i in modelo.GENERADOR
    )
    return ecuacion1 >= df_DEMANDA.demanda[t]

modelo.r1 = Constraint(modelo.PERIODO,rule=r1_rule)

#restriccion Maximo
def r2_rule(modelo,i,t):
    ecuacion2 = modelo.despacho[i,t] - df_GENERADORES.maximo[i,t]*modelo.binaria[i,t]
    return ecuacion2 <= 0

modelo.r2 = Constraint(modelo.GENERADOR,modelo.PERIODO,rule=r2_rule)

#restriccion Mínimo
def r3_rule(modelo,i,t):
    ecuacion3 = modelo.despacho[i,t] - df_GENERADORES.minimo[i,t]*modelo.binaria[i,t]
    return ecuacion3 >= 0

modelo.r3 = Constraint(modelo.GENERADOR,modelo.PERIODO,rule=r3_rule)

#Restriccion 4
def r4_rule(modelo,i,t):
    if t==1:
        ecuacion4 = modelo.arranque[i,t]-modelo.parada[i,t]
        return ecuacion4 >= modelo.binaria[i,t]-ESTADOINICIAL[i]
    else:
        return modelo.arranque[i,t]-modelo.parada[i,t]>=modelo.binaria[i,t]-modelo.binaria[i,t-1]
modelo.r4 = Constraint(modelo.RAMPAS,modelo.PERIODO,rule=r4_rule)

#Restriccion 5
def r5_rule(modelo,i,t):
    ecuacion = modelo.arranque[i,t]+modelo.parada[i,t]
    return ecuacion <= 1

modelo.r5 = Constraint(modelo.RAMPAS,modelo.PERIODO,rule=r5_rule)

#Restriccion 6 de UR
def r6_rule(modelo,i,t):
    if t==1:
        ecuacion5 = modelo.despacho[i,t] - GENERADORINICIAL[i]
        return ecuacion5 <= df_RAMPAS.ur[i]
    else:
        return modelo.despacho[i,t]-modelo.despacho[i,t-1] <= df_RAMPAS.ur[i]
modelo.r6 = Constraint(modelo.RAMPAS,modelo.PERIODO,rule=r6_rule)

#Restriccion 7 de DR
def r7_rule(modelo,i,t):
    if t==1:
        ecuacion7 = GENERADORINICIAL[i] - modelo.despacho[i,t]
        return ecuacion7 <= df_RAMPAS.dr[i]
    else:
        return modelo.despacho[i,t-1] - modelo.despacho[i,t] <=df_RAMPAS.dr[i]
modelo.r7 = Constraint(modelo.RAMPAS,modelo.PERIODO,rule=r7_rule)

#Restriccion 8 tiempo en linea
def r8_rule(modelo,i,t):
    if t < df_RAMPAS.tml[i]:
        return Constraint.Skip
    ecuacion8 = sum(modelo.arranque[i,k]
                    for k in range(t-df_RAMPAS.tml[i]+1,t)

    )
    return ecuacion8 <= modelo.binaria[i,t]
modelo.r8 = Constraint(modelo.RAMPAS,modelo.PERIODO,rule=r8_rule)

#Restriccion 9 tiempo fuera de linea
def r9_rule(modelo,i,t):
    if t < df_RAMPAS.tmfl[i]:
        return Constraint.Skip
    ecuacion9 = sum(modelo.parada[i,k]
                    for k in range(t-df_RAMPAS.tmfl[i]+1,t)
    )
    return ecuacion9 <= 1-modelo.binaria[i,t]
modelo.r9 = Constraint(modelo.RAMPAS,modelo.PERIODO,rule=r9_rule)

#Restriccion 10
def r10_rule(modelo,i,t):
    ecuacion = sum(modelo.arranque[i,t]
                for i in modelo.RAMPAS
                for t in modelo.PERIODO
    )
    return ecuacion <=1

modelo.r10 = Constraint(modelo.RAMPAS, modelo.PERIODO,rule=r10_rule)

#Definir Optimizador
opt = SolverFactory('cbc')

#Escribir archivo .lp
modelo.write("archivo.lp",io_options={"symbolic_solver_labels":True})

#Ejecutar el modelo
results = opt.solve(modelo,tee=0,logfile ="archivo.log", keepfiles= 0,symbolic_solver_labels=True)

if (results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal):

    #Imprimir Resultados
    print ()
    print ("funcion objetivo ", value(modelo.FO))

    #Escribir los resultados
    wb = xw.Book(xlFile)
    hoja_out = wb.sheets['despacho']

    columnas = ["GENERADOR"]
    for t in modelo.PERIODO:
        columnas.append(t)

    out_ = pd.DataFrame(columns=columnas)

    fila = 0
    for i in modelo.GENERADOR:
        fila += 1
        salida = []
        salida.append(i)
        for t in modelo.PERIODO:
            salida.append(modelo.despacho[i,t].value)
        out_.loc[fila] = salida

    hoja_out.clear_contents()
    hoja_out.range('A1').value = out_

elif (results.solver.termination_condition == TerminationCondition.infeasible):
	print()
	print("EL PROBLEMA ES INFACTIBLE")

elif(results.solver.termination_condition == TerminationCondition.unbounded):
	print()
	print("EL PROBLEMA ES INFACTIBLE")
else:
    print("TERMINÓ EJECUCIÓN CON ERRORES")