import os
import django
from django.conf import settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eyes1000.settings")
django.setup()
from django.core.files import File
from django.core.files.storage import FileSystemStorage
from pet.models import PetLost, PetFound, PetMaterial, PET_TYPE, GENDER_CHOICE
from wx_auth.models import User
from PIL import Image, ImageOps
from datetime import datetime
import sys
import io
import uuid

GENDER = {
  'GG': GENDER_CHOICE[1][0],
  'MM': GENDER_CHOICE[2][0],
}

PET = {
  'cat': PET_TYPE[1][0],
  'dog': PET_TYPE[2][0],
}

MATERIAL_DIR = 'import'

def Usage():
  print("%s [csv file] [lost|found] [cat|dog]" % sys.argv[0])

def get_mediacal_status(status):
  return ','.join([str(i+1) for i, ch in enumerate(status) if ch == 'Y'])

def get_found_status(status):
  return ','.join([str(i) for i, ch in enumerate(status) if ch == 'Y'])

def get_user():
  return User.objects.filter(wx_openid__isnull=False, wx_nickname__isnull=False, wx_avatar__isnull=False).order_by('?').first()

def store_material(img_path, user):
  print('save img %s' % img_path)
  with open(img_path, 'rb') as img_file:
    img_file = File(img_file)
    fs = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'material'),
                            base_url=settings.MEDIA_URL + 'material')
    material_dir = os.path.join(settings.MEDIA_ROOT, 'material')
    filename = str(uuid.uuid1()) + '.' + img_path.split('.')[-1]
    filepath = fs.save(filename, img_file)
    upload_url =  fs.url(filepath)

    image = Image.open(img_file)
    image = image.convert('RGB')
    thumb = ImageOps.fit(image, settings.THUMB_SIZE, Image.ANTIALIAS)

    thumb_io = io.BytesIO()
    thumb.save(thumb_io, 'JPEG')
    thumb_io.seek(0)
    thumb_filename = 'thumb_' + filename
    thumb_filepath = fs.save(thumb_filename, thumb_io)
    thumb_url = fs.url(thumb_filepath)
    material = PetMaterial(publisher=user, mime_type='image/jpg', size=img_file.size, url=upload_url, thumb_url=thumb_url)
    material.save()
    return material

def insert_lost(data, pet_type):
  user = get_user()
  lost = PetLost(publisher=user,
                 nickname=data[2],
                 pet_type=PET[pet_type],
                 birthday=datetime.strptime(data[4], "%m/%d/%Y"),
                 gender=GENDER[data[5]],
                 medical_status=get_mediacal_status(data[6:9]),
                 found_time=datetime.strptime(data[9], "%m/%d/%Y"),
                 place=data[10],
                 reward=int(data[11]),
                 description=data[13],
                 latitude=31.2297,
                 longitude=121.4762,
                 )
  lost.save()
  material_dir = os.path.join(MATERIAL_DIR, data[12][1:])
  for img_file in os.listdir(material_dir):
    material = store_material(os.path.join(material_dir, img_file), user)
    material.lost = lost
    material.save()
  print("insert lost %d" % lost.id)
  return lost

def insert_found(data, pet_type):
  user = get_user()
  found = PetFound(publisher=user,
                  pet_type=PET[pet_type],
                  found_time=datetime.strptime(data[5], "%m/%d/%Y"),
                  gender=GENDER[data[8]],
                  found_status=get_found_status(data[2:6]),
                  place=data[6],
                  description=data[10],
                  latitude=31.2297,
                  longitude=121.4762,
                 )
  found.save()
  material_dir = os.path.join(MATERIAL_DIR, data[9][1:])
  for img_file in os.listdir(material_dir):
    material = store_material(os.path.join(material_dir, img_file), user)
    material.found =found
    material.save()
  print("insert lost %d" % found.id)
  return found

if __name__ == "__main__":
  if len(sys.argv) < 4:
    Usage()
    sys.exit()
  file_path = sys.argv[1]
  post_type = sys.argv[2]
  pet_type = sys.argv[3]

  if pet_type not in ('cat', 'dog') or post_type not in ['lost', 'found']:
    Usage()
    sys.exit()

  with open(file_path) as csv:
    for line in csv:
      data = line.split('\t')
      if post_type == 'lost':
        insert_lost(data, pet_type)
      else:
        insert_found(data, pet_type)


from functools import reduce

# Lambda
square = lambda x: x ** 2

# Lambda with map
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x ** 2, numbers))

# Lambda with filter
values = [2, 65, 67, 78, 98]
peers = list(filter(lambda x: x % 2 == 0, values))
peers_list_comprehension = [x for x in values if x % 2 == 0]

# Lambda with reduce
values_reduce = [2, 65, 67, 78, 98]
sum_values = reduce(lambda x, y: x + y, values_reduce)


from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    html = f"
Final Capstone project
"
    return html.format(format)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80, debug=True) # specify port=80


from __future__ import print_function
from cobwebiot import client
import sys

HOST, PORT = '155.246.203.44',9999

cc = client.TCPclient(HOST,PORT)

cc.connect()

try:
    while 1:
        if sys.version_info.major == 2:
            data = raw_input("Input message:")
            cc.send(data)

        else:
            data = input("Input Message:")
            cc.send(data)
except:
    print("Program stopped")
    cc.stop()


class TreeNode:
    def __init__(self, key):
        self.key = key
        self.height = 1
        self.left = None
        self.right = None

def is_valid_avl(node):
    if node is None:
        return True

    # Verifica se a altura da subárvore esquerda e direita diferem no máximo em 1
    if abs(get_height(node.left) - get_height(node.right)) > 1:
        return False

    # Verifica recursivamente as subárvores esquerda e direita
    return is_valid_avl(node.left) and is_valid_avl(node.right)

def get_height(node):
    if node is None:
        return 0
    return node.height

# Exemplo de uso
root = TreeNode(10)
root.left = TreeNode(5)
root.right = TreeNode(15)
root.left.left = TreeNode(2)
root.left.right = TreeNode(7)

# A árvore exemplo é válida como AVL, então a função deve retornar True
print(is_valid_avl(root))


import pybullet as p
import numpy as np
from scipy.spatial.transform import Rotation
import xml.dom.minidom as xml
import os

from .active_obj import ActiveObject
from .end_effector import Gripper,Suction

class Robot(ActiveObject):
    def __init__(self,scene,**kwargs):
        if 'reset_joint_poses' not in kwargs: kwargs['reset_joint_poses'] = []
        if 'joint_damping' not in kwargs: kwargs['joint_damping'] = []
        if 'end_effector' not in kwargs: kwargs['end_effector'] = ''
        if 'speed' not in kwargs:  kwargs['speed'] = 1.0
        self.speed = kwargs['speed']
        self.current_joint_poses = self.reset_joint_poses = kwargs['reset_joint_poses']
        self.joint_damping = kwargs['joint_damping']
        self.end_effector = kwargs['end_effector']

        super().__init__(scene, **kwargs)
        self.pickup = False
        pass

    def properties(self):
        properties = super().properties()

        x,y,z,rx,ry,rz = self.get_end_effector_pose()
        properties.update(kind='Robot',speed=self.speed,
                          end_effector=self.end_effector,
                          joint_damping=self.joint_damping,
                          current_joint_poses=self.current_joint_poses,
                          reset_joint_poses=self.reset_joint_poses,
                          end_effector_pos=[x,y,z],end_effector_rot=[rx,ry,rz])
        return properties

    def update(self,dt):
        super().update(dt)
        if self.end_effector_obj:
            self.end_effector_obj.update(dt)
            if not self.end_effector_obj.idle: self.actions.append((lambda *args:None,()))
        pass

    def restore(self):
        super().restore()
        p.setJointMotorControlArray(self.id, self.used_joint_indexes, p.POSITION_CONTROL, self.reset_joint_poses)
        self.end_effector_obj.do(False)

    def set_base(self,base):
        base_temp = self.base = base

        ee_kind = ""
        mimic_name = ''
        gears = []
        with xml.parse(os.path.join(self.data_dir,base)) as doc_robot:
            node_robot = doc_robot.getElementsByTagName('robot')[0]
            for mesh in node_robot.getElementsByTagName('mesh'):
                filename = mesh.getAttribute('filename')
                mesh.setAttribute('filename',os.path.join(self.data_dir,os.path.dirname(base),filename))

            num_joints = len(node_robot.getElementsByTagName('link'))

            if self.end_effector:
                link_ee = node_robot.getElementsByTagName('link')[-1]
                link_ee_name = link_ee.getAttribute('name')
                with xml.parse(os.path.join(self.data_dir,self.end_effector)) as doc_ee:
                    node_ee = doc_ee.getElementsByTagName('robot')[0]
                    ee_kind = node_ee.getAttribute("kind")
                    for mesh in node_ee.getElementsByTagName('mesh'):
                        filename = mesh.getAttribute('filename')
                        mesh.setAttribute('filename',os.path.join(self.data_dir,os.path.dirname(self.end_effector),filename))
                    for i,link in enumerate(node_ee.getElementsByTagName('link')):
                        if i == 0: link.setAttribute('name',link_ee.getAttribute('name'))
                        node_robot.appendChild(link)
                    for i,joint in enumerate(node_ee.getElementsByTagName('joint')):
                        if i == 0: joint.getElementsByTagName('parent')[0].setAttribute('link',link_ee_name)
                        node_robot.appendChild(joint)
                        node_mimics = joint.getElementsByTagName('mimic')
                        if not node_mimics: continue
                        node_mimic = node_mimics[0]
                        mimic_name = node_mimic.getAttribute('joint')
                        joint_name = joint.getAttribute('name')
                        multiplier = int(node_mimic.getAttribute('multiplier')) if node_mimic.hasAttribute('multiplier') else 1
                        offset = 0#int(node_mimic.getAttribute('offset'))
                        gears.append((mimic_name,joint_name,multiplier))
                node_robot.removeChild(link_ee).unlink()

            import tempfile
            f = tempfile.NamedTemporaryFile("w",delete=False)
            base_temp = f.name
            f.write(node_robot.toxml())
            f.close()

        if 'id' in vars(self): p.removeBody(self.id)
        print('robot urdf',base_temp,flush=True)
        self.id = p.loadURDF(base_temp, self.pos, p.getQuaternionFromEuler(self.rot),useFixedBase=True)
        if ee_kind == 'Gripper': self.end_effector_obj = Gripper(self.id,gears)
        elif ee_kind == 'Suction': self.end_effector_obj = Suction(self.id)
        else:
            class EndEffector:
                def __init__(self): self.idle = True
                def update(self,dt):pass
                def do(self,pickup):pass
                def get_properties(self): return dict(king='EndEffector')
            self.end_effector_obj = EndEffector()
            pass

        self.used_joint_indexes = []
        self.active_joint_indexes = []
        self.joint_velocities = []
        for i in range(p.getNumJoints(self.id)):
            ji = p.getJointInfo(self.id, i)
            jointName,jointType,jointVelocity = ji[1],ji[2],ji[11]
            if (jointType != p.JOINT_REVOLUTE and jointType != p.JOINT_PRISMATIC and jointType != p.JOINT_SPHERICAL): continue
            self.active_joint_indexes.append(i)
            self.joint_velocities.append(jointVelocity)
            if i < num_joints: self.used_joint_indexes.append(i)

        if len(self.active_joint_indexes) > len(self.joint_damping): self.joint_damping.extend([0]*(len(self.active_joint_indexes)-len(self.joint_damping)))
        else: self.joint_damping = self.joint_damping[:len(self.active_joint_indexes)]

        if len(self.used_joint_indexes) > len(self.reset_joint_poses): self.reset_joint_poses.extend([0]*(len(self.used_joint_indexes)-len(self.reset_joint_poses)))
        else: self.reset_joint_poses = self.reset_joint_poses[:len(self.used_joint_indexes)]

        self.set_joints(self.reset_joint_poses)

        if 'lines' in vars(self):
            for line in self.lines:
                p.removeUserDebugItem(line)

        self.lines = list()

        pos,orn,_,_,_,_ = p.getLinkState(self.id,p.getNumJoints(self.id)-1)
        axis_x = Rotation.from_quat(orn).apply(np.array([0.05,0,0])) + pos
        axis_y = Rotation.from_quat(orn).apply(np.array([0,0.05,0])) + pos
        axis_z = Rotation.from_quat(orn).apply(np.array([0,0,0.05])) + pos
        self.lines.extend([p.addUserDebugLine(pos,axis_x,[1,0,0],2),
                          p.addUserDebugLine(pos,axis_y,[0,1,0],2),
                          p.addUserDebugLine(pos,axis_z,[0,0,1],2)])

        self.home_pos,self.home_rot = pos,p.getEulerFromQuaternion(orn)

    def set_speed(self,value):
        if round(value) == 0.000: return
        self.speed = value

    def get_joints(self):
        return self.current_joint_poses[:len(self.used_joint_indexes)]

    def set_joints(self,joint_poses):
        joint_poses = joint_poses[:len(self.used_joint_indexes)]
        for i in self.used_joint_indexes: p.resetJointState(self.id, i, joint_poses[i])
        p.setJointMotorControlArray(self.id, self.used_joint_indexes, p.POSITION_CONTROL, joint_poses)
        self.current_joint_poses = joint_poses[:len(self.used_joint_indexes)]

    def set_end_effector_pose(self,pose):
        num_joints = p.getNumJoints(self.id)
        ee_index = num_joints-1
        joint_poses = p.calculateInverseKinematics(self.id, ee_index, pose[0:3], p.getQuaternionFromEuler(pose[3:6]),restPoses=self.reset_joint_poses,maxNumIterations=200)
        self.set_joints(list(joint_poses)[:len(self.used_joint_indexes)])

    def get_end_effector_pose(self):
        num_joints = p.getNumJoints(self.id)
        ee_pos,ee_orn,_,_,_,_ = p.getLinkState(self.id,num_joints-1)
        ee_rot = p.getEulerFromQuaternion(ee_orn)
        return [ee_pos[0],ee_pos[1],ee_pos[2],ee_rot[0],ee_rot[1],ee_rot[2]]

    def set_end_effector(self,base):
        if 'id' not in vars(self): return
        self.end_effector = base
        self.set_base(self.base)

    def end_pose_from_joint_poses(self,joint_poses):
        pos = np.array([0,0,0])
        rot = np.array([0,0,0])
        i = p.getNumJoints(self.id)-1

        while i > -1:
            _,_,l_pos,l_orn,_,_,_,_ = p.getLinkState(self.id,i,True)
            pos = pos + l_pos
            rot = (Rotation.from_quat(l_orn) * Rotation.from_euler('xyz',rot)).as_euler('xyz')
            _,_,_,_,_,_,_,_,_,_,_,_,_,j_axis,j_pos,j_orn,parent_link_index = p.getJointInfo(self.id, i)
            R = Rotation.from_quat(j_orn)
            if i in self.used_joint_indexes:
                R = R * Rotation.from_rotvec(np.array(j_axis) * joint_poses[self.used_joint_indexes.index(i)])

            pos = R.apply(pos) + j_pos
            rot = (R * Rotation.from_euler('xyz',rot)).as_euler('xyz')
            i = parent_link_index

        pos = Rotation.from_euler('xyz',self.rot).apply(pos) + self.pos
        rot = (Rotation.from_euler('xyz',self.rot) * Rotation.from_euler('xyz',rot)).as_euler('xyz')

        return [pos[0],pos[1],pos[2],rot[0],rot[1],rot[2]]

    def plan(self,target_pos,target_rot,s=1,mode='joint'):
        target_orn = p.getQuaternionFromEuler(target_rot)
        num_joints = p.getNumJoints(self.id)
        ee_index = num_joints-1

        ee_pos,ee_orn,_,_,_,_ = p.getLinkState(self.id,num_joints-1)
        ee_pos = np.array(ee_pos)
        ee_rot = p.getEulerFromQuaternion(ee_orn)

        route_poses = list()

        if mode=='joint':
            end_poses = p.calculateInverseKinematics(self.id, ee_index, target_pos, target_orn, restPoses=self.reset_joint_poses, maxNumIterations=200)
            end_poses = list(end_poses)[:len(self.used_joint_indexes)]
            num = round(1 / self.scene.timestep)
            for n in range(num):
                poses = []
                t = n / num
                for i in range(len(self.current_joint_poses)):
                    poses.append(np.interp(t,[0,1],[self.current_joint_poses[i],end_poses[i]]))
                route_poses.append(poses[:len(self.used_joint_indexes)])
        else:
            num = round(1 / self.scene.timestep)
            for n in range(num):
                poses = []
                t = n / num
                x = np.interp(t,[0,1],[ee_pos[0],target_pos[0]])
                y = np.interp(t,[0,1],[ee_pos[1],target_pos[1]])
                z = np.interp(t,[0,1],[ee_pos[2],target_pos[2]])
                pos = [x,y,z]

                q1 = np.array(ee_orn)
                q2 = np.array(target_orn)
                q_dot = q1.dot(q2)

                if (np.abs(q_dot) < 1e-5):
                    orn = q1
                else:
                    if q_dot < 0:
                        q2 = -q2

                    orn = (1 - t) * q1 + t * q2
                    orn = orn / np.linalg.norm(orn)

                poses = p.calculateInverseKinematics(self.id, ee_index, pos, orn, restPoses=self.reset_joint_poses, maxNumIterations=200)
                route_poses.append(list(poses)[:len(self.used_joint_indexes)])

        # points = []
        # for poses in route_poses:
        #     point = self.end_pose_from_joint_poses(poses)
        #     points.append(point)

        # def task(i):
        #     self.lines.append(p.addUserDebugLine(points[i][0:3],points[i+1][0:3],[0,1,0],1,lifeTime=0))

        # for i in range(len(points)-1):
        #     self.actions.append((task,(i,)))
        return route_poses

    def signal_plan_move(self,*args,**kwargs):
        objs, = args
        def near_obj(o):
            e_pos,e_orn,_,_,_,_ = p.getLinkState(self.id,p.getNumJoints(self.id)-1)
            o_pos,e_pos = np.array(o[0]),np.array(e_pos)
            distance = abs(np.linalg.norm(o_pos - e_pos))
            return o_pos[2] * 100 - distance

        o_pos,o_rot,o_mesh = max(objs,key=near_obj)
        o_pos = np.array(o_pos)
        o_r = Rotation.from_euler("xyz",o_rot)

        def perfect_pick_pose(pick_pose):
            pick_p = np.array(pick_pose['pos'])
            pick_r = Rotation.from_euler("xyz",pick_pose['rot'])
            o = o_pos + o_r.apply(pick_p)
            pick_x = o_pos + o_r.apply(pick_p + pick_r.apply([0.1,0,0]))
            pick_y = o_pos + o_r.apply(pick_p + pick_r.apply([0,0.1,0]))
            pick_z = o_pos + o_r.apply(pick_p + pick_r.apply([0,0,0.1]))
            # p.addUserDebugLine(o,pick_z,[0,0,1],5,lifeTime=5)

            axis_up = [0,0,1]
            pick_axis_up = (o_r * pick_r).apply([0,0,1])
            same = np.dot(axis_up,pick_axis_up) / np.linalg.norm(axis_up) * np.linalg.norm(pick_axis_up)
            return same

        pose = max(kwargs['pick_poses'],key=perfect_pick_pose)
        pick_pos,pick_rot = pose['pos'],pose['rot']
        pick_pos = o_pos + o_r.apply(pick_pos)
        pick_r = o_r * Rotation.from_euler("xyz",pick_rot)
        pick_rot = pick_r.as_euler('xyz')

        num_joints = p.getNumJoints(self.id)
        ee_index = num_joints-1

        route_poses = self.plan(pick_pos, pick_rot)
        def task(*poses):
            p.setJointMotorControlArray(self.id, self.used_joint_indexes, p.POSITION_CONTROL, poses)
            self.current_joint_poses = poses

        for poses in route_poses: self.actions.append((task,poses))
        def output(last_pos):
            pos,_,_,_,_,_,_,_ = p.getLinkState(self.id,ee_index,True)
            pos = np.linalg.norm(pos)
            if round(last_pos - pos,6) != 0.000000:
                self.actions.append((output,(pos,)))
                return

            if 'pickup' in kwargs: self.end_effector_obj.do(kwargs['pickup'])
            self.result = None,

        self.actions.append((output,(0,)))

    def signal_pick_move(self,*args,**kwargs):
        if not args:
            active_plugins = kwargs['plugins']
            picklight = active_plugins['PickLight']
            _,pickpose = picklight.result
            args = pickpose,

        num_joints = p.getNumJoints(self.id)
        ee_index = num_joints-1
        ee_pos,ee_orn,_,_,_,_ = p.getLinkState(self.id,ee_index)
        ee_pos = np.array(ee_pos)
        ee_rot = p.getEulerFromQuaternion(ee_orn)

        if 'speed' not in kwargs: kwargs['speed'] = 1
        speed = kwargs['speed']
        if speed == 0:
            if 'pickup' in kwargs: self.end_effector_obj.do(kwargs['pickup'])
            self.result = None,[ee_pos[0],ee_pos[1],ee_pos[2],ee_rot[0],ee_rot[1],ee_rot[2]]
            return

        if 'mode' not in kwargs: kwargs['mode'] = 'joint'
        mode = kwargs['mode']

        pick_pose = args[0]
        pick_pos,pick_rot = pick_pose[0:3],pick_pose[3:6]
        pick_orn = p.getQuaternionFromEuler(pick_rot)
        route_poses = self.plan( pick_pos, pick_rot, speed, mode)

        axis_x = Rotation.from_euler('xyz',pick_rot).apply([0.05,0,0]) + pick_pos
        axis_y = Rotation.from_euler('xyz',pick_rot).apply([0,0.05,0]) + pick_pos
        axis_z = Rotation.from_euler('xyz',pick_rot).apply([0,0,0.05]) + pick_pos

        self.lines.extend([p.addUserDebugLine(pick_pos,axis_x,[1,0,0],2),
                           p.addUserDebugLine(pick_pos,axis_y,[0,1,0],2),
                           p.addUserDebugLine(pick_pos,axis_z,[0,0,1],2)])

        def task(*poses):
            p.setJointMotorControlArray(self.id, self.used_joint_indexes, p.POSITION_CONTROL, poses)
            self.current_joint_poses = poses

        for poses in route_poses: self.actions.append((task,poses))
        def output(last_pos):
            pos,_,_,_,_,_,_,_ = p.getLinkState(self.id,ee_index,True)
            pos = np.linalg.norm(pos)
            if round(last_pos - pos,6) != 0.000000:
                self.actions.append((output,(pos,)))
                return

            if 'pickup' in kwargs: self.end_effector_obj.do(kwargs['pickup'])
            self.result = None,

        self.actions.append((output,(0,)))
        pass

    def signal_move(self,*args,**kwargs):
        if 'home' in kwargs and kwargs['home']:
            self.signal_home()
            return

        num_joints = p.getNumJoints(self.id)
        ee_index = num_joints-1
        ee_pos,ee_orn,_,_,_,_ = p.getLinkState(self.id,ee_index)
        ee_pos = np.array(ee_pos)
        ee_rot = p.getEulerFromQuaternion(ee_orn)

        if 'speed' not in kwargs: kwargs['speed'] = 1
        speed = kwargs['speed']
        if speed == 0:
            if 'pickup' in kwargs: self.end_effector_obj.do(kwargs['pickup'])
            self.result = None,[ee_pos[0],ee_pos[1],ee_pos[2],ee_rot[0],ee_rot[1],ee_rot[2]]
            return

        t_pos = ee_pos
        t_rot = ee_rot
        poses = []

        def task(poses):
            p.setJointMotorControlArray(self.id, self.used_joint_indexes, p.POSITION_CONTROL, poses)
            self.current_joint_poses = poses

        if 'mode' not in kwargs: kwargs['mode'] = 'joint'
        mode = kwargs['mode']

        if 'point' in kwargs:
            t_pos = kwargs['point'][0:3]
            t_rot = kwargs['point'][3:6]
            route_poses = self.plan(t_pos,t_rot,speed,mode)
            for poses in route_poses: self.actions.append((task,(poses,)))
        else:
            end_poses = kwargs['joints'][:len(self.current_joint_poses)]
            num = int(1 / speed / self.scene.timestep)
            route_poses = []
            for n in range(num):
                poses = []
                t = n / num
                for i in range(len(self.current_joint_poses)):
                    poses.append(np.interp(t,[0,1],[self.current_joint_poses[i],end_poses[i]]))
                self.actions.append((task,(poses,)))

        def output(last_pos):
            pos,_,_,_,_,_ = p.getLinkState(self.id,ee_index)
            pos = np.linalg.norm(pos)
            if round(last_pos - pos,6) != 0.000000:
                self.actions.append((output,(pos,)))
                return

            if 'pickup' in kwargs: self.end_effector_obj.do(kwargs['pickup'])
            self.result = None,

        self.actions.append((output,(0,)))
        pass

    def signal_move_relatively(self,*args,**kwargs):
        num_joints = p.getNumJoints(self.id)
        ee_index = num_joints-1
        ee_pos,ee_orn,_,_,_,_ = p.getLinkState(self.id,ee_index)
        ee_rot = p.getEulerFromQuaternion(ee_orn)

        if args:
            pick_point = args[0]
            ee_pos = pick_point[0:3]
            ee_rot = pick_point[3:6]
            ee_orn = p.getQuaternionFromEuler(ee_rot)

        if 'target' not in kwargs: kwargs['target'] = 'task_current'
        target = kwargs['target']

        if target == 'task_next':
            declare = kwargs['declare']
            cursor = kwargs['cursor']
            active_plugins = kwargs['plugins']

            act = declare[cursor]
            next = act['next']
            act_next = declare[next]

            if act_next['fun'] == 'move_relatively':
                self.result = 'link error.',
                return
            elif act_next['fun'] == 'move':
                move_args = act_next['args']

                if 'point' in move_args:
                    pick_pose = move_args['point']
                elif 'joints' in move_args:
                    joints = move_args['joints']
                    n = len(self.used_joint_indexes) - len(joints)
                    if n > 0: joints.extend([0] * n)
                    pick_pose = self.end_pose_from_joint_poses(joints)
                else:
                    self.result = 'parameters error.',
                    return
            elif act_next['fun'] == 'pick_move':
                picklight = active_plugins['PickLight']
                _,pick_pose = picklight.result

                if not pick_pose:
                    self.result = 'failed',
                    return

            ee_pos = pick_pose[0:3]
            ee_rot = pick_pose[3:6]
            ee_orn = p.getQuaternionFromEuler(ee_rot)

        if 'speed' not in kwargs: kwargs['speed'] = 1
        speed = kwargs['speed']
        if speed == 0:
            if 'pickup' in kwargs: self.end_effector_obj.do(kwargs['pickup'])
            self.result = None,[ee_pos[0],ee_pos[1],ee_pos[2],ee_rot[0],ee_rot[1],ee_rot[2]]
            return

        if 'mode' not in kwargs: kwargs['mode'] = 'joint'
        mode = kwargs['mode']

        t_pos = ee_pos
        t_rot = ee_rot

        if 'point' in kwargs:
            t_pos = kwargs['point'][0:3] + np.array(ee_pos)
            t_rot = kwargs['point'][3:6] + np.array(ee_rot)

        route_poses = self.plan(t_pos,t_rot,speed,mode)

        def task(poses):
            p.setJointMotorControlArray(self.id, self.used_joint_indexes, p.POSITION_CONTROL, poses)
            self.current_joint_poses = poses

        for poses in route_poses: self.actions.append((task,(poses,)))

        def output(last_pos):
            pos,_,_,_,_,_ = p.getLinkState(self.id,ee_index)
            pos = np.linalg.norm(pos)
            if round(last_pos - pos,6) != 0.000000:
                self.actions.append((output,(pos,)))
                return

            if 'pickup' in kwargs: self.end_effector_obj.do(kwargs['pickup'])
            self.result = None,

        self.actions.append((output,(0,)))
        pass

    def signal_do(self,*args,**kwargs):
        self.end_effector_obj.do(kwargs['pickup'])
        pass

    def signal_pick(self,*args,**kwargs):
        self.end_effector_obj.do(True)
        pass

    def signal_place(self,*args,**kwargs):
        self.end_effector_obj.do(False)
        pass

    def signal_home(self):
        if 'lines' in vars(self):
            for line in self.lines:
                p.removeUserDebugItem(line)

        self.lines = list()

        num_joints = p.getNumJoints(self.id)
        ee_index = num_joints-1
        ee_pos,ee_orn,_,_,_,_ = p.getLinkState(self.id,ee_index)
        ee_pos = np.array(ee_pos)
        ee_rot = p.getEulerFromQuaternion(ee_orn)

        t_pos = self.home_pos
        t_rot = self.home_rot

        self.reset_joint_poses
        joint_poses = []
        for joint in self.used_joint_indexes:
            position,velocity,reaction_force,motor_torque = p.getJointState(self.id,joint)
            joint_poses.append(position)

        def task(poses):
            p.setJointMotorControlArray(self.id, self.used_joint_indexes, p.POSITION_CONTROL, poses)
            self.current_joint_poses = poses

        num = int(1 / self.speed / self.scene.timestep) + 1
        for n in range(num):
            poses = []
            t = n / num
            for i in range(len(self.used_joint_indexes)):
                poses.append(np.interp(t,[0,1],[self.current_joint_poses[i],self.reset_joint_poses[i]]))
            self.actions.append((task,(poses,)))

        def output(): self.result = None,
        self.actions.append((output,()))
        pass


# 배열을 구현하여 주어진 수를 입력받습니다.
arr = list(map(int, input().split()))
sum_val = 0
cnt = 0

# 10개의 정수 중 250 이상의 수가 나올 때 까지의 수의 합계와 개수를 구합니다.
for elem in arr:
	if elem >= 250:
		break
	sum_val += elem
	cnt += 1

# 평균을 구합니다.
avg = sum_val / cnt

# 출력
print(f"{sum_val} {avg:.1f}")


import inspect

import joblib

import numpy as np

import pandas as pd

from ..core.portfolio import Portfolio
from ..utils import mabc

# =============================================================================
# BASE
# =============================================================================

class PortfolioMakerABC(mabc.ModelABC):
    _MKPORT_SIGNATURE = {
        "self",
        "window_size",
        "days",
        "stocks",
        "price",
        "weights",
    }

    def __init_subclass__(cls):
        mpsig = inspect.signature(cls.make_portfolio)
        missing_args = cls._MKPORT_SIGNATURE.difference(mpsig.parameters)
        if missing_args:
            missing_args_str = ", ".join(missing_args)
            msg = f"Missing arguments {missing_args_str!r} in make_portfolio"
            raise TypeError(msg)
        return super().__init_subclass__()

    @mabc.abstractmethod
    def make_portfolio(
        self,
        *,
        window_size=5,
        days=365,
        stocks=10,
        price=100,
        weights=None,
    ):
        raise NotImplementedError()

# =============================================================================
# ANOTHER BASE
# =============================================================================

class RandomEntropyPortfolioMakerABC(PortfolioMakerABC):
    random_state = mabc.hparam(
        default=None, converter=np.random.default_rng, repr=False
    )
    n_jobs = mabc.hparam(default=None)
    verbose = mabc.hparam(default=0)
    entropy = mabc.hparam(default=0.5)

    # Abstract=================================================================

    @mabc.abstractmethod
    def get_window_loss_probability(self, window_size, entropy):
        raise NotImplementedError()

    @mabc.abstractmethod
    def make_stock_price(self, price, loss, random):
        raise NotImplementedError()

    # INTERNAL ================================================================

    def _coerce_price(self, stocks, prices):
        if isinstance(prices, (int, float)):
            prices = np.full(stocks, prices, dtype=float)
        elif len(prices) != stocks:
            raise ValueError(f"The number of prices must be equal {stocks}")
        return np.asarray(prices, dtype=float)

    def _make_stocks_seeds(self, stocks):
        iinfo = np.iinfo(int)
        seeds = self.random_state.integers(
            low=0,
            high=iinfo.max,
            size=stocks,
            dtype=iinfo.dtype,
            endpoint=True,
        )

        return seeds

    def _make_loss_sequence(self, days, loss_probability, random):
        win_probability = 1 - loss_probability

        # primero seleccionamos con las probabilidades adecuadas si en cada
        # dia se pierde o se gana
        sequence = random.choice(
            [True, False],
            size=days,
            p=[loss_probability, win_probability],
        )

        # con esto generamos lugares al azar donde vamos a invertir la
        # secuencia anterior dado que las probabilidades representan
        # una distribucion simétrica
        reverse_mask = random.choice([True, False], size=days)

        # finalmente invertimos esos lugares
        final_sequence = np.where(reverse_mask, ~sequence, sequence)

        return final_sequence

    def _make_stock(
        self,
        days,
        loss_probability,
        stock_idx,
        initial_price,
        random,
    ):
        # determinamos que dia se pierde y que dia se gana
        loss_sequence = self._make_loss_sequence(
            days, loss_probability, random
        )

        # fijamos el primer precio como el precio orginal
        current_price = initial_price

        # vamos a tener tantos dias como dijimos mas un dia 0 al comienzo
        timeserie = np.empty(days + 1, dtype=float)

        # first price is the initial one
        timeserie[0] = current_price

        for day, loss in enumerate(loss_sequence, start=1):
            current_price = self.make_stock_price(current_price, loss, random)
            timeserie[day] = current_price

        stock_df = pd.DataFrame({f"S{stock_idx}": timeserie})

        return stock_df

    # API =====================================================================

    def make_portfolio(
        self,
        *,
        window_size=5,
        days=365,
        stocks=10,
        price=100,
        weights=None,
    ):
        if window_size <= 0:
            raise ValueError("'window_size' must be > 0")
        if days < window_size:
            raise ValueError("'days' must be >= window_size")

        initial_prices = self._coerce_price(stocks, price)

        loss_probability = self.get_window_loss_probability(
            window_size, self.entropy
        )

        seeds = self._make_stocks_seeds(stocks)
        idx_prices_seed = zip(range(stocks), initial_prices, seeds)

        with joblib.Parallel(
            n_jobs=self.n_jobs,
            verbose=self.verbose,
            prefer="processes",
        ) as P:
            dmaker = joblib.delayed(self._make_stock)
            stocks = P(
                dmaker(
                    days=days,
                    loss_probability=loss_probability,
                    stock_idx=stock_idx,
                    initial_price=stock_price,
                    random=np.random.default_rng(seed),
                )
                for stock_idx, stock_price, seed in idx_prices_seed
            )

        stock_df = pd.concat(stocks, axis=1)

        return Portfolio.from_dfkws(
            stock_df,
            weights=weights,
            entropy=self.entropy,
            window_size=window_size,
        )


# Pizza order

# Small Pizza: $15
# Medium Pizza: $20
# Large Pizza: $25
#
# Pepperoni for Small Pizza: +$2
# Pepperoni for Medium or Large Pizza: +$3
#
# Extra cheese for any size Pizza: +$1
#
# size = S, M, L
# add_pepperoni = "Y/N"
# extra_cheese = "Y/N"
#
# output: "Your final bill is: $SUM"

print("Welcome to Python Pizza Delivery!")
size = "l" # input("What size pizza do you want? s, m, l? ")
add_pepperoni = "y" # input("Do you want pepperoni? y/n? ")
extra_cheese = "n" # input("Do you want extra cheese? y/n? ")

def print_size(size):
    printed_size = ""
    if size == 's':
        return "small"
    elif size == 'm':
        return "medium"
    elif size == 'l':
        return "large"
    return

def print_addons(add_pepperoni, extra_cheese):
    addons = " without "
    if add_pepperoni == "y" and extra_cheese == "y":
        return " with pepperoni and extra cheese "
    elif add_pepperoni == "y" and extra_cheese == "n":
        return " with pepperoni "
    elif add_pepperoni == "n" and extra_cheese == "y":
        return " with extra cheese "
    else:
        return addons

printed_size = print_size(size)
additives = print_addons(add_pepperoni, extra_cheese)

bill = 0
if size == "s":
    bill += 15
elif size == "m":
    bill =+ 20
elif size == "l":
    bill += 25
else:
    print("Custom shape is not available")
if add_pepperoni == "y":
    if size == "s":
        bill += 2
    elif size == "l" or size == "m":
        bill += 3
if extra_cheese == "y":
    bill += 1
print(f"Your ordered {printed_size} pizza{additives}additives, and your bill is ${bill}")


import os
import time
import argparse
import inspect
import json
import torch
from pathlib import Path

from torch.nn import functional as F

from dataclasses import dataclass
from utils.registry import Model, Dataset
from models import GPT2, ButterflyGPT
from contextlib import nullcontext

@dataclass
class TrainingConfig:
    log_interval: int = 10
    validation_interval: int = 200
    validation_iters: int = 100
    batch_size: int = 64
    gradient_accumulation_steps: int = 1
    model: str = 'GPT2'
    name: str = 'athena'
    dataset: str = None
    learning_rate: float = 6e-4
    rate_decay: float = 0.99
    decay_iters: int = 600000                   # should be ~= max_iters per Chinchilla
    min_rate: float = 6e-5                      # minimum learning rate, should be ~= learning_rate/10 per Chinchilla
    weight_decay: float = 1e-1
    beta1: float = 0.9
    beta2: float = 0.95
    grad_clip: float = 1.0
    seed: int = 1337
    max_iters: int = 5000

@dataclass
class TrainingState:
    iter: int = 0
    validation_loss: float = 1e10

def LoadCheckpoint(file, device='cuda'):
    checkpoint = torch.load(file, map_location=device)
    config = checkpoint['config']
    model = CreateModel(config.model)
    model.load_state_dict(checkpoint['model'])

    return model, config

def CreateModel(modelname):
    if modelname not in Model.registry:
        raise Exception(f'Unknown model "{modelname}"')
    return Model.registry[modelname]()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Load language model and generate text')
    parser.add_argument('--checkpoint', type=argparse.FileType('rb'), help='Path to a checkpoint to resume training from')
    parser.add_argument('--seed', type=int, default=1337, help="Random seed to init torch with")
    parser.add_argument('--device', type=str, default='cuda', help='Device to train model on')
    parser.add_argument('--compile', type=bool, default=True, help='Compile model before training or not')
    parser.add_argument('--dtype', type=str, default='bfloat16', choices=['bfloat16', 'float16', 'float32'])
    parser.add_argument('--prompt', type=str, default='\n')
    parser.add_argument('--length', default=128, type=int)
    parser.add_argument('--num_samples', default=1, type=int)
    parser.add_argument('--temperature', default=0.8, type=float)

    arguments = parser.parse_args()

    model, config = LoadCheckpoint(arguments.checkpoint)
    config.seed = arguments.seed

    n_params = sum(p.numel() for p in model.parameters())
    n_params -= model.emb.weight.numel()
    print(f'Model with {n_params/1e6:.2f} M parameters')

    device = arguments.device
    dtype = arguments.dtype
    device_type = 'cuda' if 'cuda' in device else 'cpu'

    torch.manual_seed(config.seed)
    torch.backends.cuda.matmul.allow_tf32 = True    # allow tf32 on matmul
    torch.backends.cudnn.allow_tf32 = True          # allow tf32 on cudnn

    ptdtype = {'float32': torch.float32, 'bfloat16': torch.bfloat16, 'float16': torch.float16}[dtype]
    scaler = torch.cuda.amp.GradScaler(enabled=(dtype == 'float16'))
    ctx = nullcontext() if device_type == 'cpu' else torch.amp.autocast(device_type=device_type, dtype=ptdtype)

    model.to(device)

    if arguments.compile:
        try:
            model = torch.compile(model)
        except Exception as e:
            print(f'Model compilation failed - {str(e)}')
            pass

    x = torch.ByteTensor(list(bytes(arguments.prompt, 'utf8'))).type(torch.int64).to(device)
    x = x.unsqueeze(0)
    decode = lambda l: ''.join([chr(i) for i in l.tolist()[0]])

    with torch.no_grad():
        with ctx:
            for _ in range(arguments.num_samples):
                sample = x
                for _ in range(arguments.length):
                    logits, _ = model(sample)
                    logits = logits[:, -1, :] / arguments.temperature
                    probs = F.softmax(logits, dim=-1)
                    s = torch.multinomial(probs, num_samples=1)
                    sample = torch.cat((sample, s), dim=1)
                print('------')
                print(decode(sample))


dammage = int(input())
ls = []
ls2 = []
c_ls = []
z = str(input())
st = ''
for x in z:
    ls.append(x)
for x in ls:
    if x != ' ':
        st += x
    else:
        ls2.append(int(st))
        st = ''
ls2.append(int(st))
count = 0
for x in ls2:
    if x % dammage == 0:
        count += (x//dammage)
        c_ls.append(count)
    else:
        count += (x//dammage+1)
        c_ls.append(count)
print(count)
for x in c_ls:
    print(x,end=' ')

python/lab13.5 pre final finish/Labsident Evil.py


from pyspark import RDD
from pyspark.sql import Row
from pyspark.sql import SparkSession

from evaluation import RegressionEvaluator
from recommendation import ALS

spark = SparkSession \
    .builder \
    .appName("recommend") \
    .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer") \
    .config("spark.shuffle.consolidateFiles", "true") \
    .getOrCreate()

lines = spark.read.text("hdfs://isec-hdp01:8020/data/source3/*").rdd  # type: RDD

parts = lines.map(lambda row: row.value.split("::"))
amountsRDD = parts.map(lambda p: Row(userId=int(p[0]), rank=int(p[1]), amount=float(p[2])))
amounts = spark.createDataFrame(amountsRDD)
(training, test) = amounts.randomSplit([0.8, 0.2])

# Build the recommendation model using ALS on the training data
# Note we set cold start strategy to 'drop' to ensure we don't get NaN evaluation metrics
als = ALS(maxIter=5, regParam=0.01, userCol="userId", itemCol="rank", ratingCol="amount", coldStartStrategy="drop")
model = als.fit(training)

# Evaluate the model by computing the RMSE on the test data
predictions = model.transform(test)
evaluator = RegressionEvaluator(metricName="rmse", labelCol="amount", predictionCol="prediction")

rmse = evaluator.evaluate(predictions)
print("Root-mean-square error = " + str(rmse))

# Generate top 10 movie recommendations for each user
userRecs = model.recommendForAllUsers(10)
userRecs.show(100)

# Generate top 10 user recommendations for each movie
movieRecs = model.recommendForAllItems(10)
movieRecs.show()


from .detector import Detector
import dlib
import face_recognition
import face_recognition_models
import numpy as np
import cv2
import os
import logging
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

TMP_DIR = "./../faceswap/tmp"

mean_face_x = np.array([
0.000213256, 0.0752622, 0.18113, 0.29077, 0.393397, 0.586856, 0.689483, 0.799124,
0.904991, 0.98004, 0.490127, 0.490127, 0.490127, 0.490127, 0.36688, 0.426036,
0.490127, 0.554217, 0.613373, 0.121737, 0.187122, 0.265825, 0.334606, 0.260918,
0.182743, 0.645647, 0.714428, 0.793132, 0.858516, 0.79751, 0.719335, 0.254149,
0.340985, 0.428858, 0.490127, 0.551395, 0.639268, 0.726104, 0.642159, 0.556721,
0.490127, 0.423532, 0.338094, 0.290379, 0.428096, 0.490127, 0.552157, 0.689874,
0.553364, 0.490127, 0.42689 ])

mean_face_y = np.array([
0.106454, 0.038915, 0.0187482, 0.0344891, 0.0773906, 0.0773906, 0.0344891,
0.0187482, 0.038915, 0.106454, 0.203352, 0.307009, 0.409805, 0.515625, 0.587326,
0.609345, 0.628106, 0.609345, 0.587326, 0.216423, 0.178758, 0.179852, 0.231733,
0.245099, 0.244077, 0.231733, 0.179852, 0.178758, 0.216423, 0.244077, 0.245099,
0.780233, 0.745405, 0.727388, 0.742578, 0.727388, 0.745405, 0.780233, 0.864805,
0.902192, 0.909281, 0.902192, 0.864805, 0.784792, 0.778746, 0.785343, 0.778746,
0.784792, 0.824182, 0.831803, 0.824182 ])

landmarks_2D = np.stack( [ mean_face_x, mean_face_y ], axis=1 )

def umeyama(src, dst, estimate_scale):
    """Estimate N-D similarity transformation with or without scaling.
    Parameters
    ----------
    src : (M, N) array
        Source coordinates.
    dst : (M, N) array
        Destination coordinates.
    estimate_scale : bool
        Whether to estimate scaling factor.
    Returns
    -------
    T : (N + 1, N + 1)
        The homogeneous similarity transformation matrix. The matrix contains
        NaN values only if the problem is not well-conditioned.
    References
    ----------
    .. [1] "Least-squares estimation of transformation parameters between two
            point patterns", Shinji Umeyama, PAMI 1991, DOI: 10.1109/34.88573
    """

    num = src.shape[0]
    dim = src.shape[1]

    # Compute mean of src and dst.
    src_mean = src.mean(axis=0)
    dst_mean = dst.mean(axis=0)

    # Subtract mean from src and dst.
    src_demean = src - src_mean
    dst_demean = dst - dst_mean

    # Eq. (38).
    A = np.dot(dst_demean.T, src_demean) / num

    # Eq. (39).
    d = np.ones((dim,), dtype=np.double)
    if np.linalg.det(A) < 0:
        d[dim - 1] = -1

    T = np.eye(dim + 1, dtype=np.double)

    U, S, V = np.linalg.svd(A)

    # Eq. (40) and (43).
    rank = np.linalg.matrix_rank(A)
    if rank == 0:
        return np.nan * T
    elif rank == dim - 1:
        if np.linalg.det(U) * np.linalg.det(V) > 0:
            T[:dim, :dim] = np.dot(U, V)
        else:
            s = d[dim - 1]
            d[dim - 1] = -1
            T[:dim, :dim] = np.dot(U, np.dot(np.diag(d), V))
            d[dim - 1] = s
    else:
        T[:dim, :dim] = np.dot(U, np.dot(np.diag(d), V.T))

    if estimate_scale:
        # Eq. (41) and (42).
        scale = 1.0 / src_demean.var(axis=0).sum() * np.dot(S, d)
    else:
        scale = 1.0

    T[:dim, dim] = dst_mean - scale * np.dot(T[:dim, :dim], src_mean.T)
    T[:dim, :dim] *= scale

    return T

class Faceswap(Detector):
    def __init__(self, blur_size=2, seamless_clone=False, mask_type="facehullandrect", erosion_kernel_size=None, **kwargs):
        super(Faceswap, self).__init__()
        self.build_pipeline_by_JSONFile("./faceswap_config.json")
        predictor_68_point_model = face_recognition_models.pose_predictor_model_location()
        self.pose_predictor = dlib.shape_predictor(predictor_68_point_model)
        self.erosion_kernel = None
        self.blur_size = blur_size
        self.seamless_clone = seamless_clone
        self.mask_type = mask_type.lower()
        self.video_capture = None
        self.all_faces = []

    def preprocess(self, frame):
        faces = []
        face_locations = face_recognition.face_locations(frame)
        landmarks = self._raw_face_landmarks(frame, face_locations)
        for ((y, right, bottom, x), landmarks) in zip(face_locations, landmarks):
            faces.append(DetectedFace(frame[y: bottom, x: right], x, right - x, y, bottom - y, landmarks))

        return faces

    def get_align_mat(self, face):
        return umeyama(np.array(face.landmarksAsXY()[17:]), landmarks_2D, True)[0:2]

    def _raw_face_landmarks(self, face_image, face_locations):
        face_locations = [self._css_to_rect(face_location) for face_location in face_locations]
        return [self.pose_predictor(face_image, face_location) for face_location in face_locations]

    def _css_to_rect(self, css):
        return dlib.rectangle(css[3], css[0], css[1], css[2])

    def get_image_mask(self, image, new_face, face_detected, mat, image_size):

        face_mask = np.zeros(image.shape,dtype=float)
        if 'rect' in self.mask_type:
            face_src = np.ones(new_face.shape,dtype=float)
            cv2.warpAffine( face_src, mat, image_size, face_mask, cv2.WARP_INVERSE_MAP, cv2.BORDER_TRANSPARENT )

        hull_mask = np.zeros(image.shape,dtype=float)
        if 'hull' in self.mask_type:
            hull = cv2.convexHull( np.array( face_detected.landmarksAsXY() ).reshape((-1,2)).astype(int) ).flatten().reshape( (-1,2) )
            cv2.fillConvexPoly( hull_mask,hull,(1,1,1) )

        if self.mask_type == 'rect':
            image_mask = face_mask
        elif self.mask_type == 'faceHull':
            image_mask = hull_mask
        else:
            image_mask = ((face_mask*hull_mask))

        if self.erosion_kernel is not None:
            image_mask = cv2.erode(image_mask,self.erosion_kernel,iterations = 1)

        if self.blur_size!=0:
            image_mask = cv2.blur(image_mask,(self.blur_size,self.blur_size))

        return image_mask

    def apply_new_face(self, image, new_face, image_mask, mat, image_size, size):
        base_image = np.copy( image )
        new_image = np.copy( image )

        cv2.warpAffine( new_face, mat, image_size, new_image, cv2.WARP_INVERSE_MAP, cv2.BORDER_TRANSPARENT )

        outImage = None
        if self.seamless_clone:
            masky,maskx = cv2.transform( np.array([ size/2,size/2 ]).reshape(1,1,2) ,cv2.invertAffineTransform(mat) ).reshape(2).astype(int)
            outimage = cv2.seamlessClone(new_image.astype(np.uint8),base_image.astype(np.uint8),(image_mask*255).astype(np.uint8),(masky,maskx) , cv2.NORMAL_CLONE )
        else:
            foreground = cv2.multiply(image_mask, new_image.astype(float))
            background = cv2.multiply(1.0 - image_mask, base_image.astype(float))
            outimage = cv2.add(foreground, background)

        return outimage

    def avg_color(self, old_face, new_face):
        w = old_face.shape[0]
        h = old_face.shape[1]
        old_face = old_face.astype(float)
        new_face = new_face.astype(float)

        rank = 5
        scale = 1.0 / rank

        for i in range(rank):
            new_face[i, i:w-i, :] = scale * i * new_face[i, i:w-i, :] + scale * (rank - i) * old_face[i, i:w-i, :]
            new_face[h-i-1, i:w-i, :] = scale * i * new_face[h-i-1, i:w-i, :] + scale * (rank - i) * old_face[h-i-1, i:w-i, :]
            new_face[i+1:h-i-1, i, :] = scale * i * new_face[i+1:h-i-1, i, :] + scale * (rank - i) * old_face[i+1:h-i-1, i, :]
            new_face[i+1:h-i-1, w-i-1, :] = scale * i * new_face[i+1:h-i-1, w-i-1, :] + scale * (rank - i) * old_face[i+1:h-i-1, w-i-1, :]
        diff = old_face[rank:h-rank-1, rank:w-rank-1, :] - new_face[rank:h-rank-1, rank:w-rank-1, :]
        avg_diff = np.sum(diff, axis=(0, 1)) / ((new_face.shape[0] - 2*rank) * (new_face.shape[1] - 2*rank))
        new_face[rank:h-rank-1, rank:w-rank-1, :] += avg_diff
        np.clip(new_face, 0.0, 255.0, out=new_face)
        return np.uint8(new_face)

    def process_of_video(self, video_file):
        i = 0
        size = 64
        if not os.path.exists(TMP_DIR):
            os.mkdir(TMP_DIR)
        videoCapture = cv2.VideoCapture(video_file)
        fps = videoCapture.get(cv2.CAP_PROP_FPS)
        videoWrite = cv2.VideoWriter(os.path.join(TMP_DIR, "origin_face.mp4"),
                                    cv2.VideoWriter_fourcc("m", "p", "4", "v"),
                                    fps, (size, size))
        success, frame = videoCapture.read()

        while success:
            faces = self.preprocess(frame)
            self.all_faces.append(faces)

            for face_detected in faces:
                mat = np.array(self.get_align_mat(face_detected)).reshape(2,3) * size
                face = cv2.warpAffine( frame, mat, (size,size) )
                videoWrite.write(np.uint8(face))
            success, frame = videoCapture.read()
            i += 1
            logger.info("detected face: processing {}".format(i))
        videoWrite.release()
        ret = self.detect_image(os.path.join(TMP_DIR, "origin_face.mp4"))
        self.video_capture = cv2.VideoCapture(os.path.join(TMP_DIR, str(ret) + ".avi"))

    def predict(self, frame, src_type):
        if src_type == "image":
            faces = self.preprocess(frame)
        elif src_type == "video":
            faces = self.all_faces.pop(0)
        size = 64
        image_size = frame.shape[1], frame.shape[0]
        if not os.path.exists(TMP_DIR):
            os.mkdir(TMP_DIR)

        for face_detected in faces:
            mat = np.array(self.get_align_mat(face_detected)).reshape(2,3) * size
            face = cv2.warpAffine( frame, mat, (size,size) )
            if src_type == "image":
                image_path = os.path.join(TMP_DIR, "origin_face.jpg")
                cv2.imwrite(image_path, face)
                ret = self.detect_image(image_path)
                if self.video_capture == None:
                    self.video_capture = cv2.VideoCapture(os.path.join(TMP_DIR, str(ret) + ".avi"))
                success, new_face = self.video_capture.read()
            elif src_type == "video":
                success, new_face = self.video_capture.read()
            new_face = self.avg_color(face, new_face)
            image_mask = self.get_image_mask(frame, new_face, face_detected, mat, image_size)
            frame = self.apply_new_face(frame, new_face, image_mask, mat, image_size, size)

        return np.uint8(frame)

class DetectedFace(object):
    def __init__(self, image, x, w, y, h, landmarks):
        self.image = image
        self.x = x
        self.w = w
        self.y = y
        self.h = h
        self.landmarks = landmarks

    def landmarksAsXY(self):
        return [(p.x, p.y) for p in self.landmarks.parts()]


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
from pysmt.shortcuts import *
from transactions import *

class AdderSpec(Spec):
	def __init__(self, bits):
		args = {'a': BVType(bits), 'b': BVType(bits)}
		ret_args = {'c': BVType(bits), 'carry': BVType(1)}
		a,     b = [Symbol(f"ser_add.add{bits}.{name}", tpe) for name,tpe in args.items()]
		c, carry = [Symbol(f"ser_add.add{bits}.{name}", tpe) for name, tpe in ret_args.items()]

		tt  = [Transition(inputs={'clr': BV(1,1)})]
		tt += [Transition(inputs={'clr': BV(0,1),
								  'a': BVExtract(a, ii, ii),
								  'b': BVExtract(b, ii, ii),},
						  outputs={'q': BVExtract(c, ii, ii)})
			   for ii in range(bits)]
		tt[-1].outputs['o_v'] = carry
		semantics = {'c': BVAdd(a, b), 'carry': BVExtract(BVAdd(BVZExt(a, 1), BVZExt(b, 1)), bits, bits)}
		transactions = [Transaction(name=f"add{bits}", args=args, ret_args=ret_args, semantics=semantics, proto=LegacyProtocol(tt))]
		super().__init__(transactions=transactions, idle=None)

add_v = os.path.join('fork', 'rtl', 'ser_add.v')

def main() -> int:
	version = require_yosys()
	print(f"Using yosys {version}")

	mod = Module.load('ser_add', [add_v])
	spec = AdderSpec(32)
	prob = VerificationProblem(spec=spec, implementation='ser_add')

	#protocol_to_wavedrom_file("adder.json", spec.transactions[0].proto)

	print(f"Trying to proof {mod.name}")
	#print(mod)
	ee = SMT2ProofEngine(outdir='../smt2')
	#ee = MCProofEngine(outdir="../btor2")
	veri = Verifier(mod, prob, ee)
	veri.proof_all()

	return 0

if __name__ == '__main__':
	import sys
	sys.exit(main())


# -*- coding: utf-8 -*-
"""
Created on Tue Apr 14 18:10:04 2020

@author: Sivaraman Lakshmipathy
"""

import sys

from corpus_handler import *

def main():
    abstracts_file_directory = "." + os.path.sep + "www" + os.path.sep + "abstracts"
    gold_file_directory = "." + os.path.sep + "www" + os.path.sep + "gold"
    window_size = 2
    try:
        if len(sys.argv) >= 4:
            abstracts_file_directory = sys.argv[1]
            gold_file_directory = sys.argv[2]
            window_size = int(sys.argv[3])
        else:
            print("Insufficient input parameters. Reverting to default directories and window size.")
    except Exception as e:
        print("Unknown exception while assigning parameter values. Reverting to default directories and window size.")
        abstracts_file_directory = "." + os.path.sep + "www" + os.path.sep + "abstracts"
        gold_file_directory = "." + os.path.sep + "www" + os.path.sep + "gold"
        window_size = 2
    if not os.path.exists(abstracts_file_directory):
        print("Abstracts directory not found. Exiting.")
        return
    if not os.path.exists(gold_file_directory):
        print("Gold file directory not found. Exiting.")
        return
    if window_size == -1:
        print("Window size not specified. Exiting.")
        return

    print("Processing the documents with window size", window_size, "...")
    mrr_obj = mrr(10)
    corpus_handler(abstracts_file_directory, gold_file_directory, window_size, mrr_obj)
    mrr_obj.pretty_print()

if __name__ == "__main__":
    main()


from time import time
import hashlib
import json
from urllib.parse import urlparse
import requests

class Blockchain:
    # Constructor
    def __init__(self):
        self.chain = []  # Normally a linked list, but we're lazy. So.
        self.nodes = set()  # Address book of neighbor nodes
        self.current_transactions = []  # Transactions that haven't been posted onto a block

        # Create the first (genesis) block of the chain
        self.new_block(previous_hash='1', proof=100)

    # Function to register a neighboring node (other user) in personal address book
    def register_node(self, address):
        parsed_url = urlparse(address)  # Parse value into usable format

        if parsed_url.netloc:  # If there is a specific net domain, e.g. "example.com"
            self.nodes.add(parsed_url.netloc)  # Add the net location
        elif parsed_url.path:  # If URL has 'IP address' schema, e.g. "192.168.1.1:3000"
            self.nodes.add(parsed_url.path)  # Add the IP path
        else:  # If no URL provided
            raise ValueError("Invalid address")

    # Function to check if a chain is valid
    def valid_chain(self, chain):
        last_block = chain[0]  # We'll iterate through all blocks
        current_index = 1
        # Normally, we should be checking all of the proofs of work.
        # But instead we're going to check the hash values lining up

        while current_index < len(chain):
            block = chain[current_index]  # Temp. store current block
            last_block_hash = self.hash(last_block)  # Store last hash

            # If hash pointer is wrong, it's not a valid chain.
            if block["previous_hash"] != last_block_hash:
                return False

            last_block = block  # Iterate to next block in chain
            current_index += 1

        # At this point, all of the blocks have been checked and are NOT INvalid
        return True

    # Function to resolve conflicts between neighbors' chains
    def resolve_conflicts(self):
        neighbors = self.nodes  # Load address book into mutable temp. store
        new_chain = None  # Temp. store for neighbors' chains

        max_length = len(self.chain)

        for node in neighbors:  # Get all chains from all neighbor servers
            response = requests.get(f"http://{node}/chain")

            if response.status_code == 200:  # OK status
                length = response.json()["length"]
                chain = response.json()["chain"]

                # The longest chain is going to be the correct one, for this example
                if length > max_length and self.valid_chain(chain):
                    max_length = length
                    new_chain = chain

        # At this point, if a new chain has not been found, the new_chain will still have None from initialization
        # None always coerces to False. But any other value will coerce to True
        if new_chain:
            self.chain = new_chain  # Replace the chain
            return True

        return False  # The chain wasn't replaced

    # Function to create a new block and add it to the chain
    def new_block(self, proof, previous_hash):
        block = {
            "index": len(self.chain) - 1,  # Point in chain
            "timestamp": time(),  # Uses date/time library
            "transactions": self.current_transactions,
            "proof": proof,
            "previous_hash": previous_hash or self.hash(self.last_block)
        }

        # Reset the list of our un-tracked transactions
        self.current_transactions = []
        # Append the new block to our chain
        self.chain.append(block)
        # Return our block object (for other uses later)
        return block

    # Function to create a new sale/transaction of money
    def new_transaction(self, sender, recipient, amount):
        self.current_transactions.append({  # Skipping a step and appending straight
            "sender": sender,
            "recipient": recipient,
            "amount": amount
        })

        # Return the index of the new block w/ this transaction
        return self.last_block["index"] + 1

    # Function to get the last block of a chain (think peek from linked list)
    @property
    def last_block(self):
        return self.chain[-1]

    # Function to create a cryptographic hash
    @staticmethod  # This function won't be re-made every time we make a new Blockchain
    def hash(block):
        # Put all JSON metadata into encoded string
        strBlock = json.dumps(block, sort_keys=True).encode()
        # Return SHA256 hash in hex
        return hashlib.sha256(strBlock).hexdigest()

    # Function to create the proof of work of a block
    def proof_of_work(self, last_block):
        last_proof = last_block["proof"]  # Last proof of work
        last_hash = self.hash(last_block)  # Recalc last hash

        proof = 0
        # Don't actually do this... This is for simplification purposes.
        while self.valid_proof(last_proof, proof, last_hash) is False:
            proof += 1  # Iterates proof values instead of calculating
            # Will keep iterating until a proof works and is valid
            # Which means this could theoretically take infinite time

        return proof

    # Function to check if a proof of work is valid
    @staticmethod
    def valid_proof(last_proof, proof, last_hash):
        # Create a proof based on these encoded values
        guess = f"{last_proof}{proof}{last_hash}".encode()
        guess_hash = hashlib.sha256(guess).hexdigest()

        # A valid proof will have first four bits zeroes, and return True
        return guess_hash[:4] == "0000"


from ..utils import inch_to_base_unit, create_polygon, create_base_object
from ..coord import min_x_coord, max_x_coord, min_y_coord, max_y_coord

MINIMUM_BAY = inch_to_base_unit(12.0)
WIRE_SPACE = inch_to_base_unit(1.0)

WIRE_WIDTH = inch_to_base_unit(0.1)

# BUILDING_LAYER is a constant for the input layer name
BUILDING_LAYER = "joist"

# ANNOTATIONS_LAYER is a constant for the output layer name
ANNOTATIONS_LAYER = "annotations/joistBayLines"

class JoistBayLinesGenerator:
    def __init__(self, variation, params={}):
        self.variation = variation
        self.params = params
        self.generated_layers = self.params["layers"]
        self.changes = set()

    def generate(self):
        interior_objects = self.generated_layers[BUILDING_LAYER]["objects"]
        new_layer = self.variation.add_layer(ANNOTATIONS_LAYER)
        self.changes.add(ANNOTATIONS_LAYER)

        for i in range(len(interior_objects) - 1):
            obj = interior_objects[i]
            next_obj = interior_objects[i+1]

            min_x = max_x_coord(obj)
            max_x = min_x_coord(next_obj)
            min_y = min(min_y_coord(obj), min_y_coord(next_obj))
            max_y = max(max_y_coord(obj), max_y_coord(next_obj))

            # if bay is at least MINIMUM_BAY, create joist bay polygon
            # vertical bay
            width = max_x - min_x
            if width >= MINIMUM_BAY and width <= max_y - min_y:
                # add the wires
                curr = min_x + WIRE_SPACE
                while curr + WIRE_SPACE + WIRE_WIDTH <= max_x:
                    # render the curr wire
                    wire_coords = [
                        curr, min_y,
                        curr + WIRE_WIDTH, min_y,
                        curr + WIRE_WIDTH, max_y,
                        curr, max_y
                    ]
                    bay_line = create_polygon(wire_coords)

                    wire_base_object = create_base_object(
                        bay_line, properties={"color": "red"}
                    )
                    self.variation.add_object(
                        ANNOTATIONS_LAYER, wire_base_object)
                    curr = curr + WIRE_SPACE + WIRE_WIDTH

                # add the bay
                base_coords = [
                    min_x, min_y,
                    max_x, min_y,
                    max_x, max_y,
                    min_x, max_y,
                ]
                bay_polygon = create_polygon(base_coords)
                base_object = create_base_object(
                    bay_polygon, properties={"color": "white"}
                )
                self.variation.add_object(ANNOTATIONS_LAYER, base_object)
                continue

            min_x = min(min_x_coord(obj), min_x_coord(next_obj))
            max_x = max(max_x_coord(obj), max_x_coord(next_obj))
            min_y = max_y_coord(next_obj)
            max_y = min_y_coord(obj)

            # horizontal bay
            width = max_y - min_y
            if width >= MINIMUM_BAY and max_x - min_x > width:
                # add the wires
                curr = max_y - WIRE_SPACE

                while curr - WIRE_SPACE - WIRE_WIDTH >= min_y:
                    # render the curr wire
                    wire_coords = [
                        min_x, curr - WIRE_WIDTH,
                        max_x, curr - WIRE_WIDTH,
                        max_x, curr,
                        min_x, curr
                    ]
                    bay_line = create_polygon(wire_coords)

                    wire_base_object = create_base_object(
                        bay_line, properties={"color": "red"}
                    )
                    self.variation.add_object(
                        ANNOTATIONS_LAYER, wire_base_object)
                    curr = curr - WIRE_SPACE - WIRE_WIDTH

                # add the bay
                base_coords = [
                    min_x, min_y,
                    max_x, min_y,
                    max_x, max_y,
                    min_x, max_y,
                ]
                bay_polygon = create_polygon(base_coords)
                base_object = create_base_object(
                    bay_polygon, properties={"color": "white"}
                )
                self.variation.add_object(ANNOTATIONS_LAYER, base_object)


#!/usr/bin/env python3
#
# Authors: Christoph Lehner 2020
#
# Desc.: Illustrate core concepts and features
#
import gpt as g
import numpy as np
import sys

grid_dp = g.grid([8, 4, 4, 4], g.double)
grid_sp = g.grid([8, 4, 4, 4], g.single)

rng = g.random("block_seed_string_13")
for grid, prec in [(grid_dp, 1e-28), (grid_sp, 1e-14)]:
    U = g.qcd.gauge.random(grid, rng, scale=10)
    g.message(g.qcd.gauge.plaquette(U))
    for i in range(4):
        test = g.norm2(g.adj(U[i]) * U[i] - g.qcd.gauge.unit(grid)[0]) / g.norm2(U[i])
        g.message(test)
        assert test < prec

rng = g.random("block_seed_string_13")
n = 10000
res = {}
for i in range(n):
    z = rng.zn()
    if z not in res:
        res[z] = 0
    res[z] += 1

g.message(res)

n = 10000
res = {}
for i in range(n):
    z = rng.zn(p={"n": 3})
    if z not in res:
        res[z] = 0
    res[z] += 1

g.message(res)

n = 10000
res = [0, 0, 0, 0, 0]
for i in range(n):
    z = rng.normal()
    res[0] += 1
    res[1] += z
    res[2] += z ** 2
    res[3] += z ** 3
    res[4] += z ** 4

g.message(res[1] / res[0], res[2] / res[0], res[3] / res[0], res[4] / res[0])

v = g.complex(grid_dp)
rng.normal(v)

test_sequence_comp = np.array(
    [
        v[0, 0, 0, 0].real,
        v[2, 0, 0, 0].real,
        v[0, 2, 0, 0].real,
        v[1, 3, 1, 3].real,
        v[3, 2, 1, 0].real,
    ]
)
# print([ v[0,0,0,0].real, v[2,0,0,0].real, v[0,2,0,0].real, v[1,3,1,3].real, v[3,2,1,0].real ])
test_sequence_ref = np.array(
    [
        1.0336693180495347,
        -0.23474901515559715,
        -0.26622475825072717,
        1.0175124089453662,
        0.9519248978004753,
    ],
    np.float64,
)

g.message(test_sequence_comp)

err = np.linalg.norm(test_sequence_comp - test_sequence_ref)
assert err < 1e-14

g.message(err)

for i in range(1000):
    rng.normal(v)

test_sequence_comp = np.array(
    [
        v[0, 0, 0, 0].real,
        v[2, 0, 0, 0].real,
        v[0, 2, 0, 0].real,
        v[1, 3, 1, 3].real,
        v[3, 2, 1, 0].real,
    ]
)
# print([ v[0,0,0,0].real, v[2,0,0,0].real, v[0,2,0,0].real, v[1,3,1,3].real, v[3,2,1,0].real ])
test_sequence_ref = np.array(
    [
        -0.3194228736274878,
        -0.4211727874061459,
        0.7680875563790006,
        -0.18697640758578687,
        0.3276024231946795,
    ],
    np.float64,
)
g.message(test_sequence_comp)

err = np.linalg.norm(test_sequence_comp - test_sequence_ref)
assert err < 1e-14

g.message("All tests passed")


import copy
import datetime
import json
import os
import uuid
from enum import Enum
from typing import Union, Optional, Dict

from . import GLOBAL_LOGGER
from .MarketUtils import TradeSide, TradeData

LOGGER = GLOBAL_LOGGER.getChild(os.path.basename(os.path.splitext(__file__)[0]))
__all__ = ['OrderState', 'OrderType', 'TradeInstruction', 'TradeReport']

class OrderType(Enum):
    UNKNOWN = -2
    CancelOrder = -1
    Manual = 0
    LimitOrder = 1
    LimitMarketMaking = 1.1
    MarketOrder = 2
    FOK = 2.1
    FAK = 2.2
    IOC = 2.3

    def __hash__(self):
        return self.value

class OrderState(Enum):
    UNKNOWN = -3
    Rejected = -2
    Invalid = -1
    Pending = 0
    Placed = 1
    PartFilled = 2
    Filled = 3
    Canceling = 4
    PartCanceled = 5
    Canceled = 6

    def __hash__(self):
        return self.value

class TradeReport(object):
    def __init__(
            self, *,
            ticker: str,
            side: Union[int, float, str, TradeSide],
            volume: float,
            notional: float,
            trade_time: datetime.datetime,
            order_id: str,
            price: Optional[float] = None,
            trade_id: Optional[str] = None,
            multiplier: float = 1,
            fee: float = .0
    ):
        """
        store trade report data
        :param ticker: ticker (symbol) of the given asset (stock, future, option, crypto and etc.)
        :param side: TradeSide should be the same as TradeInstruction
        :param volume: Traded volume (the number of shares, contracts or crypto, etc.)
        :param notional: Traded notional (the amount of money) or premium of the option
        :param trade_time: datetime.datetime when trade was matched
        :param order_id: the id of its TradeInstruction
        :param price: the traded price. NOTED: trade price does not necessarily equals notional / volume. For example, currency swap, crypto swap (future) and debt
        :param trade_id: the id of itself
        :param multiplier: multiplier for contract or option
        :param fee: transition fee of this trade
        """
        assert volume >= 0, 'Trade volume must not be negative'
        self.__ticker = str(ticker)
        self.__side = TradeSide(side)
        self.__volume = volume
        self.__notional = notional
        self.__trade_time = trade_time
        self.__order_id = str(order_id)
        self.__price = price
        self.__trade_id = str(trade_id) if trade_id is not None else str(uuid.uuid4())
        self.__multiplier = float(multiplier)
        self.__fee = fee

    def __repr__(self):
        return '<TradeReport>{}'.format({key: item.name if key == 'side' else item for key, item in self.__dict__.items()})

    def __eq__(self, other):
        if isinstance(other, TradeReport):
            return self.__repr__() == other.__repr__()
        else:
            return False

    def __str__(self):
        return '<TradeReport>([{:%Y-%m-%d %H:%M:%S}] OrderID {} {} {} {:.2f} @ {:.2f} with TradeID {})'.format(self.TradeTime, self.OrderID, self.Ticker, self.Side.name, self.Volume, self.Price, self.TradeID)

    def __reduce__(self):
        return self.__class__.from_json, (self.to_json(),)

    def reset_order_id(self, order_id: Optional[str] = None, **kwargs) -> 'TradeReport':
        """
        reset order_id id to given string
        :param order_id: new order id, use UUID by default
        :return:
        """
        if not kwargs.pop('_ignore_warning', False):
            LOGGER.warning('TradeReport OrderID being reset manually! TradeInstruction.reset_order_id() is the recommended method to do so.')

        if order_id is not None:
            self.__order_id = str(order_id)
        else:
            self.__order_id = str(uuid.uuid4())

        return self

    def reset_trade_id(self, trade_id: Optional[str] = None) -> 'TradeReport':
        """
        reset trade id to given string
        :param trade_id:
        :return:
        """
        if trade_id is not None:
            self.__trade_id = str(trade_id)
        else:
            self.__trade_id = str(uuid.uuid4())

        return self

    def to_trade(self) -> TradeData:
        trade = TradeData(
            ticker=self.Ticker,
            trade_time=self.TradeTime,
            trade_price=self.Notional / self.Volume / self.__multiplier if self.Volume > 0 else 0,
            trade_volume=self.Volume,
            side=self.Side,
            multiplier=self.__multiplier
        )
        return trade

    def to_json(self) -> str:
        protected_decorator = '_{}'.format(self.__class__.__name__)
        data_dict = {
            key: value.strftime('%Y-%m-%d %H:%M:%S%f') if key == '{}__trade_time'.format(protected_decorator)
            else value.value if key == '{}__side'.format(protected_decorator)
            else value
            for key, value in self.__dict__.items()
        }

        return json.dumps(data_dict)

    def copy(self, **kwargs):
        new_trade = self.__class__(
            ticker=kwargs.pop('ticker', self.__ticker),
            side=kwargs.pop('side', self.__side),
            volume=kwargs.pop('volume', self.__volume),
            notional=kwargs.pop('notional', self.__notional),
            trade_time=kwargs.pop('trade_time', self.__trade_time),
            order_id=kwargs.pop('order_id', None),
            price=kwargs.pop('price', self.__price),
            trade_id=kwargs.pop('trade_id', f'{self.__trade_id}.copy'),
            multiplier=kwargs.pop('multiplier', self.__multiplier),
            fee=kwargs.pop('fee', self.__fee)
        )

        return new_trade

    @classmethod
    def from_json(cls, json_message: Union[str, bytes, bytearray, dict]) -> 'TradeReport':
        if isinstance(json_message, dict):
            json_dict = json_message
        else:
            json_dict = json.loads(json_message)
        protected_decorator = '_{}'.format(TradeReport.__name__)

        self = cls(
            ticker=json_dict.pop('{}__ticker'.format(protected_decorator)),
            side=TradeSide(json_dict.pop('{}__side'.format(protected_decorator))),
            volume=json_dict.pop('{}__volume'.format(protected_decorator)),
            notional=json_dict.pop('{}__notional'.format(protected_decorator)),
            trade_time=datetime.datetime.strptime(json_dict.pop('{}__trade_time'.format(protected_decorator)), '%Y-%m-%d %H:%M:%S%f'),
            order_id=json_dict.pop('{}__order_id'.format(protected_decorator)),
            trade_id=json_dict.pop('{}__trade_id'.format(protected_decorator))
        )

        for key, value in json_dict.items():
            setattr(self, key, value)

        return self

    @staticmethod
    def from_trade(trade_data: TradeData, order_id: str, trade_id: Optional[str] = None) -> 'TradeReport':
        report = TradeReport(
            ticker=trade_data.ticker,
            side=trade_data.side,
            volume=trade_data.volume,
            notional=trade_data.notional,
            trade_time=trade_data.trade_time,
            order_id=order_id,
            trade_id=trade_id
        )
        return report

    # noinspection PyPep8Naming
    @property
    def Ticker(self) -> str:
        return self.__ticker

    # noinspection PyPep8Naming
    @property
    def Side(self) -> TradeSide:
        return self.__side

    # noinspection PyPep8Naming
    @property
    def Volume(self) -> float:
        return self.__volume

    # noinspection PyPep8Naming
    @property
    def Notional(self) -> float:
        return self.__notional

    # noinspection PyPep8Naming
    @property
    def Price(self) -> float:
        if self.__price is not None:
            return self.__price
        elif self.__volume == 0:
            return .0
        else:
            return self.__notional / self.__volume / self.__multiplier

    # noinspection PyPep8Naming
    @property
    def TradeTime(self) -> datetime.datetime:
        return self.__trade_time

    # noinspection PyPep8Naming
    @property
    def OrderID(self) -> str:
        return self.__order_id

    # noinspection PyPep8Naming
    @property
    def TradeID(self) -> str:
        return self.__trade_id

    # noinspection PyPep8Naming
    @property
    def multiplier(self) -> float:
        return self.__multiplier

    @property
    def fee(self) -> float:
        return self.__fee

class TradeInstruction(object):
    def __init__(
            self, *,
            ticker: str,
            side: Union[int, float, str, TradeSide],
            order_type: OrderType = OrderType.Manual,
            volume: float = 0.0,
            limit_price: Optional[float] = None,
            order_id: Optional[str] = None,
            multiplier: float = 1
    ):
        if volume <= 0:
            raise Exception('Invalid trade volume!')

        self.__ticker = str(ticker)
        self.__side = TradeSide(side)
        self.__order_type = order_type
        self.__volume = float(volume)
        self.__limit_price = limit_price
        self.__order_id = str(order_id) if order_id is not None else str(uuid.uuid4())
        self.__multiplier = float(multiplier)

        self.__order_state: OrderState = OrderState.Pending
        self.__filled_volume: float = 0.0
        self.__filled_notional: float = 0.0
        self.__fee = .0
        self.__start_datetime: Optional[datetime.datetime] = None
        self.__cancel_datetime: Optional[datetime.datetime] = None
        self.__finish_datetime: Optional[datetime.datetime] = None
        self.__trades: Dict[str, TradeReport] = {}

    def __repr__(self):
        return '<TradeInstruction>{}'.format(self.__dict__)

    def __eq__(self, other):
        if isinstance(other, TradeInstruction):
            return self.__repr__() == other.__repr__()
        else:
            return False

    def __str__(self):
        if self.LimitPrice is None or self.__order_type == OrderType.MarketOrder:
            return '<TradeInstruction>({} OrderID {} {} {} {:.2f} filled {:.2f} @ {:.2f} now {})'.format(self.OrderType.name, self.OrderID, self.Side.name, self.Ticker, self.Volume, self.__filled_volume, self.AveragePrice, self.OrderState.name)
        else:
            return '<TradeInstruction>({} OrderID {} {} {} {:.2f} limit {:.2f} filled {:.2f} @ {:.2f} now {})'.format(self.OrderType.name, self.OrderID, self.Side.name, self.Ticker, self.Volume, self.LimitPrice, self.__filled_volume, self.AveragePrice, self.OrderState.name)

    def __reduce__(self):
        return self.__class__.from_json, (self.to_json(),)

    def reset(self):
        self.__trades = {}

        self.__order_state: OrderState = OrderState.Pending
        self.__filled_volume: float = 0.0
        self.__filled_notional: float = 0.0
        self.__fee = .0
        self.__start_datetime: Optional[datetime.datetime] = None
        self.__cancel_datetime: Optional[datetime.datetime] = None
        self.__finish_datetime: Optional[datetime.datetime] = None
        self.__trades: Dict[str, TradeReport] = {}

    def reset_order_id(self, order_id: Optional[str] = None, **kwargs) -> 'TradeInstruction':
        """
        reset order id to given string
        :param order_id:
        :return:
        """
        if not kwargs.pop('_ignore_warning', False):
            LOGGER.warning('TradeInstruction OrderID being reset manually! Position.reset_order_id() is the recommended method to do so.')

        if order_id is not None:
            self.__order_id = str(order_id)
        else:
            self.__order_id = str(uuid.uuid4())

        for trade_report in self.__trades.values():
            trade_report.reset_order_id(order_id=self.__order_id, _ignore_warning=True)

        return self

    def set_order_state(self, order_state: OrderState, market_datetime: datetime.datetime = datetime.datetime.utcnow()) -> 'TradeInstruction':
        self.__order_state = order_state

        # assign a start_datetime if order placed
        if order_state == OrderState.Placed:
            self.__start_datetime: Optional[datetime.datetime] = market_datetime

        if order_state == OrderState.Canceled or OrderState == OrderState.PartCanceled:
            self.__cancel_datetime: Optional[datetime.datetime] = market_datetime
            self.__finish_datetime: Optional[datetime.datetime] = market_datetime

        return self

    def fill(self, trade_report: TradeReport) -> 'TradeInstruction':
        if trade_report.OrderID != self.OrderID:
            LOGGER.warning('Order ID not match! Instruction ID {}; Report ID {}'.format(self.OrderID, trade_report.OrderID))
            return self

        if trade_report.TradeID in self.__trades:
            LOGGER.warning('Duplicated trade received! Instruction {}; Report {}'.format(str(self), str(trade_report)))
            return self

        if trade_report.Volume != 0:
            # update multiplier
            if len(self.__trades) > 0:
                assert self.__multiplier == trade_report.multiplier, 'Multiplier not match!'
            else:
                self.__multiplier = trade_report.multiplier

            if trade_report.Volume + self.__filled_volume > self.__volume:
                LOGGER.warning('Fatal error!\nTradeInstruction: \n\t{}\nTradeReport:\n\t{}'.format(str(TradeInstruction), '\n\t'.join([str(x) for x in self.__trades.values()])))
                raise Exception('Fatal error! trade reports filled volume exceed order volume!')

            self.__filled_volume += abs(trade_report.Volume)
            self.__filled_notional += abs(trade_report.Notional)

        if self.__filled_volume == self.__volume:
            self.set_order_state(OrderState.Filled)
            self.__finish_datetime = trade_report.TradeTime
        elif self.__filled_volume > 0:
            self.set_order_state(OrderState.PartFilled)

        self.__trades[trade_report.TradeID] = trade_report

        return self

    def cancel_order(self) -> 'TradeInstruction':
        self.set_order_state(OrderState.Canceling)

        cancel_instruction = copy.copy(self)
        cancel_instruction.__order_type = OrderType.CancelOrder

        return cancel_instruction

    def canceled(self, canceled_datetime: datetime.datetime) -> 'TradeInstruction':
        LOGGER.warning(DeprecationWarning('[canceled] depreciated! Use [set_order_state] instead!'), stacklevel=2)

        self.set_order_state(OrderState.Canceled, canceled_datetime)
        return self

    def to_json(self, with_trade=True) -> str:
        protected_decorator = '_{}'.format(self.__class__.__name__)
        data_dict = {
            key: value.value if key == '{}__side'.format(protected_decorator)
            else value.value if key == '{}__order_type'.format(protected_decorator)
            else value.value if key == '{}__order_state'.format(protected_decorator)
            else value.strftime('%Y-%m-%d %H:%M:%S%f') if key == '{}__start_datetime'.format(protected_decorator) and getattr(self, '{}__start_datetime'.format(protected_decorator)) is not None
            else value.strftime('%Y-%m-%d %H:%M:%S%f') if key == '{}__cancel_datetime'.format(protected_decorator) and getattr(self, '{}__cancel_datetime'.format(protected_decorator)) is not None
            else value.strftime('%Y-%m-%d %H:%M:%S%f') if key == '{}__finish_datetime'.format(protected_decorator) and getattr(self, '{}__finish_datetime'.format(protected_decorator)) is not None
            else {trade_id: value[trade_id].to_json() for trade_id in value} if key == '{}__trades'.format(protected_decorator) and with_trade
            else None if key == '{}__trades'.format(protected_decorator) and not with_trade
            else value
            for key, value in self.__dict__.items()
        }

        return json.dumps(data_dict)

    @classmethod
    def from_json(cls, json_message: Union[str, bytes, bytearray, dict]) -> 'TradeInstruction':
        if isinstance(json_message, dict):
            json_dict = json_message
        else:
            json_dict = json.loads(json_message)

        protected_decorator = '_{}'.format(TradeInstruction.__name__)

        self = cls(
            ticker=json_dict.pop('{}__ticker'.format(protected_decorator)),
            side=TradeSide(json_dict.pop('{}__side'.format(protected_decorator))),
            order_type=OrderType(json_dict.pop('{}__order_type'.format(protected_decorator))),
            volume=json_dict.pop('{}__volume'.format(protected_decorator)),
            limit_price=json_dict.pop('{}__limit_price'.format(protected_decorator)),
            order_id=json_dict.pop('{}__order_id'.format(protected_decorator))
        )

        setattr(self, '{}__order_state'.format(protected_decorator), OrderState(json_dict.pop('{}__order_state'.format(protected_decorator))))

        trade_records = json_dict.pop('{}__trades'.format(protected_decorator))
        if trade_records is None:
            setattr(self, '{}__trades'.format(protected_decorator), {})
        else:
            setattr(self, '{}__trades'.format(protected_decorator), {trade_id: TradeReport.from_json(trade_records[trade_id]) for trade_id in trade_records})

        for key, value in json_dict.items():
            setattr(self, key, value)

        if getattr(self, '{}__start_datetime'.format(protected_decorator)) is not None:
            setattr(self, '{}__start_datetime'.format(protected_decorator), datetime.datetime.strptime(getattr(self, '{}__start_datetime'.format(protected_decorator)), '%Y-%m-%d %H:%M:%S%f'))

        if getattr(self, '{}__cancel_datetime'.format(protected_decorator)) is not None:
            setattr(self, '{}__cancel_datetime'.format(protected_decorator), datetime.datetime.strptime(getattr(self, '{}__cancel_datetime'.format(protected_decorator)), '%Y-%m-%d %H:%M:%S%f'))

        if getattr(self, '{}__finish_datetime'.format(protected_decorator)) is not None:
            setattr(self, '{}__finish_datetime'.format(protected_decorator), datetime.datetime.strptime(getattr(self, '{}__finish_datetime'.format(protected_decorator)), '%Y-%m-%d %H:%M:%S%f'))

        return self

    # noinspection PyPep8Naming
    @property
    def OrderID(self) -> str:
        return self.__order_id

    # noinspection PyPep8Naming
    @property
    def Ticker(self) -> str:
        return self.__ticker

    # noinspection PyPep8Naming
    @property
    def Side(self) -> TradeSide:
        return self.__side

    # noinspection PyPep8Naming
    @property
    def OrderType(self) -> OrderType:
        return self.__order_type

    # noinspection PyPep8Naming
    @property
    def Volume(self) -> float:
        return self.__volume

    # noinspection PyPep8Naming
    @property
    def LimitPrice(self) -> Optional[float]:
        return self.__limit_price

    # noinspection PyPep8Naming
    @property
    def StartTime(self) -> Optional[datetime.datetime]:
        return self.__start_datetime

    # noinspection PyPep8Naming
    @property
    def CancelTime(self) -> Optional[datetime.datetime]:
        return self.__cancel_datetime

    # noinspection PyPep8Naming
    @property
    def FinishTime(self) -> Optional[datetime.datetime]:
        return self.__finish_datetime

    # noinspection PyPep8Naming
    @property
    def OrderState(self) -> OrderState:
        return self.__order_state

    # noinspection PyPep8Naming
    @property
    def FilledVolume(self) -> float:
        return self.__filled_volume

    # noinspection PyPep8Naming
    @property
    def WorkingVolume(self) -> float:
        return self.__volume - self.__filled_volume

    # noinspection PyPep8Naming
    @property
    def FilledNotional(self) -> float:
        return self.__filled_notional

    # noinspection PyPep8Naming
    @property
    def AveragePrice(self) -> float:
        if self.__filled_volume != 0:
            return self.__filled_notional / self.__filled_volume / self.__multiplier
        else:
            return float('NaN')

    # noinspection PyPep8Naming
    @property
    def Trades(self) -> Dict[str, TradeReport]:
        return self.__trades

    # noinspection PyPep8Naming
    @property
    def Multiplier(self) -> float:
        return self.__multiplier

    @property
    def fee(self):
        return self.__fee

    @fee.setter
    def fee(self, value):
        self.__fee = float(value)


"""
Created on Feb 25, 2014

@author: alex
"""

from pyramid_layout.panel import panel_config

from farmgui.models import DBSession
from farmgui.models import PeripheryController

@panel_config(name='sidebar', renderer='farmgui:panels/templates/sidebar.pt')
def sidebar(context, request):
    processes = [{'name': 'Total',
                  'cpu_key': 'total-cpu',
                  'mem_key': 'total-mem'},
                 {'name': 'Farm Supervisor',
                  'cpu_key': 'fs-cpu',
                  'mem_key': 'fs-mem'},
                 {'name': 'Farm Manager',
                  'cpu_key': 'fm-cpu',
                  'mem_key': 'fm-mem'},
                 {'name': 'Periphery Controller',
                  'cpu_key': 'pc-cpu',
                  'mem_key': 'pc-mem'},
                 {'name': 'Pyramid',
                  'cpu_key': 'ps-cpu',
                  'mem_key': 'ps-mem'},
                 {'name': 'MySQL',
                  'cpu_key': 'db-cpu',
                  'mem_key': 'db-mem'},
                 {'name': 'Redis',
                  'cpu_key': 'redis-cpu',
                  'mem_key': 'redis-mem'}]
    return {'processes': processes}

@panel_config(name='process_panel', renderer='farmgui:panels/templates/process_panel.pt')
def process_panel(context, request):
    return {'proc': context}


import os
import os.path as osp

import torch
import numpy as np
from PIL import Image
from torch.utils.data import Dataset
from torchvision import transforms

class DatasetLoader(Dataset):

    def __init__(self, setname, args=None):
        DATASET_DIR = os.path.join(args.data_dir, 'FC100/')
        if setname == 'train':
            THE_PATH = osp.join(DATASET_DIR, 'train')
            label_list = os.listdir(THE_PATH)
        elif setname == 'test':
            THE_PATH = osp.join(DATASET_DIR, 'test')
            label_list = os.listdir(THE_PATH)
        elif setname == 'val':
            THE_PATH = osp.join(DATASET_DIR, 'val')
            label_list = os.listdir(THE_PATH)
        else:
            raise ValueError('Wrong setname.')
        self.wnids = label_list

        data = []
        label = []

        if 'num_patch' not in vars(args).keys():
            print('no num_patch parameter, set as default: 9')
            self.num_patch = 9
        else:
            self.num_patch = args.num_patch

        folders = [osp.join(THE_PATH, label) for label in label_list if os.path.isdir(osp.join(THE_PATH, label))]

        for idx, this_folder in enumerate(folders):
            this_folder_images = os.listdir(this_folder)
            for image_path in this_folder_images:
                data.append(osp.join(this_folder, image_path))
                label.append(idx)
        self.data = data
        self.label = label
        self.num_class = len(set(label))

        image_size = 84
        self.transform = transforms.Compose([
            transforms.RandomResizedCrop(image_size),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize(np.array([x / 255.0 for x in [125.3, 123.0, 113.9]]),
                                 np.array([x / 255.0 for x in [63.0, 62.1, 66.7]]))
        ])

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

    def __getitem__(self, i):
        path, label = self.data[i], self.label[i]
        patch_list = []
        for _ in range(self.num_patch):
            patch_list.append(self.transform(Image.open(path).convert('RGB')))
        patch_list = torch.stack(patch_list, dim=0)
        return patch_list, label

if __name__ == '__main__':
    pass


from time import perf_counter
#from ev3dev.ev3 import *
from ev3fast import *

lMotor = LargeMotor('outA')
rMotor = LargeMotor('outB')
lSensor = ColorSensor('in1')
rSensor = ColorSensor('in4')

LOOPS = 1000

startTime = perf_counter()
for a in range(0,LOOPS):
  valueL = lSensor.raw
  valueR = rSensor.raw
  totalL = (valueL[0] + valueL[1] + valueL[2])
  totalR = (valueR[0] + valueR[1] + valueR[2])
  error = totalL - totalR
  lMotor.speed_sp = -40#200 + error
  rMotor.speed_sp = -40#200 - error
  lMotor.run_forever()
  rMotor.run_forever()
endTime = perf_counter()

lMotor.stop()
rMotor.stop()

print(str(LOOPS / (endTime - startTime)))


from __future__ import print_function
import mock
import pytest
import sys

from sigopt.run_context import allow_state_update, RunContext

@pytest.mark.parametrize('new_state,old_state,expected', [
  ('completed', 'active', True),
  ('failed', 'active', True),
  ('completed', 'completed', False),
  ('failed', 'completed', True),
  ('completed', 'failed', False),
  ('failed', 'failed', False),
])
def test_allow_state_update(new_state, old_state, expected):
  assert allow_state_update(new_state, old_state) == expected

class TestLiveRunContext(object):
  def make_run_context(self):
    run_context = RunContext(
      connection=mock.Mock(),
      run=mock.Mock(assignments={"fixed1": 0, "fixed2": "test"}),
    )
    run_context._update_run = mock.Mock()
    return run_context

  @pytest.fixture
  def run_context(self):
    return self.make_run_context()

  @pytest.mark.parametrize('test_value', [
    12345,
    1.2345,
    'hello',
  ])
  def test_assignment_setitem(self, run_context, test_value):
    test_key = 'assignment_test_key'
    run_context.params[test_key] = test_value
    assert run_context.params[test_key] == test_value
    assert getattr(run_context.params, test_key) == test_value
    run_context._update_run.assert_called_once_with({'assignments': {test_key: test_value}})

  @pytest.mark.parametrize('test_value', [
    12345,
    1.2345,
    'hello',
  ])
  def test_assignment_setattr(self, run_context, test_value):
    test_key = 'assignment_test_key'
    setattr(run_context.params, test_key, test_value)
    assert run_context.params[test_key] == test_value
    assert getattr(run_context.params, test_key) == test_value
    run_context._update_run.assert_called_once_with({'assignments': {test_key: test_value}})

  def test_log_failure_method(self, run_context):
    return_value = run_context.log_failure()
    assert return_value is None
    run_context._update_run.assert_called_once_with({'state': 'failed'})

  @pytest.mark.parametrize('test_value', [
    12345,
    1.2345,
  ])
  def test_log_metadata_method_with_numbers(self, run_context, test_value):
    test_key = 'test_metadata_key'
    return_value = run_context.log_metadata(test_key, test_value)
    assert return_value is None
    run_context._update_run.assert_called_once_with({'metadata': {test_key: test_value}})

  def test_log_metadata_method_with_null(self, run_context):
    test_key = 'test_metadata_key'
    return_value = run_context.log_metadata(test_key, None)
    assert return_value is None
    run_context._update_run.assert_called_once_with({'metadata': {test_key: None}})

  @pytest.mark.parametrize('test_value', [
    'hello',
    [],
    {},
    object(),
  ])
  def test_log_metadata_method_stringifies_objects(self, run_context, test_value):
    test_key = 'test_metadata_key'
    return_value = run_context.log_metadata(test_key, test_value)
    assert return_value is None
    run_context._update_run.assert_called_once_with({'metadata': {test_key: str(test_value)}})

  @pytest.mark.parametrize('test_value', [
    12345,
    1.2345,
  ])
  def test_log_metric_method_good_value(self, run_context, test_value):
    test_key = 'test_metric_key'
    return_value = run_context.log_metric(test_key, test_value)
    assert return_value is None
    run_context._update_run.assert_called_once_with({'values': {
      test_key: {'value': test_value},
    }})

  @pytest.mark.parametrize('test_value', [
    None,
    'hello',
    [],
    {},
    object(),
  ])
  def test_log_metric_method_bad_value(self, run_context, test_value):
    test_key = 'test_metric_key'
    with pytest.raises(ValueError):
      run_context.log_metric(test_key, test_value)

  @pytest.mark.parametrize('test_stddev', [
    12345,
    1.2345,
  ])
  def test_log_metric_method_good_stddev(self, run_context, test_stddev):
    test_key = 'test_metric_key'
    test_value = 0
    return_value = run_context.log_metric(test_key, test_value, test_stddev)
    assert return_value is None
    run_context._update_run.assert_called_once_with({'values': {
      test_key: {
        'value': test_value,
        'value_stddev': test_stddev,
      },
    }})

  @pytest.mark.parametrize('test_stddev', [
    'hello',
    [],
    {},
    object(),
  ])
  def test_log_metric_method_bad_stddev(self, run_context, test_stddev):
    test_key = 'test_metric_key'
    test_value = 0
    with pytest.raises(ValueError):
      run_context.log_metric(test_key, test_value, test_stddev)

  def test_log_dataset(self, run_context):
    dataset_name = 'dataset for testing'
    return_value = run_context.log_dataset(
      name=dataset_name,
    )
    assert return_value is None
    run_context._update_run.assert_called_once_with({'datasets': {
      dataset_name: {},
    }})

  def test_log_model(self, run_context):
    model_type = 'test type'
    return_value = run_context.log_model(type=model_type)
    assert return_value is None
    run_context._update_run.assert_called_once_with({'model': {'type': model_type}})


#Sample Solution for lab3 question 5
import arcpy

arcpy.env.workspace = ".\\"
arcpy.env.overwriteOutput = True

cityShp = "CITIES.shp"
citySelShp = "cities_250000.shp"

arcpy.Select_analysis(cityShp, citySelShp, "\"POP1990\" > 250000")

print("Completed.")