# coding: utf-8
'''串口测试数据生成器'''

from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui, QtSerialPort
from PyQt5 import uic
import os
import sys
import struct
import binascii
from datetime import datetime

uipath, uiname = os.path.split(os.path.realpath(__file__))
uiname = uiname.replace('.py', '.ui')
uifile = os.path.join(uipath, uiname)
ui_mainwindow, qtbaseclass = uic.loadUiType(uifile)

class DataSender(ui_mainwindow, qtbaseclass):
    '''串口数据发送显示界面'''

    serial = QtSerialPort.QSerialPort()

    def __init__(self):

        ui_mainwindow.__init__(self)
        qtbaseclass.__init__(self)
        self.setupUi(self)

        port_list = []

        for info in QtSerialPort.QSerialPortInfo.availablePorts():
            port_list.append(info.portName())

        if port_list:
            self.comboBox_port.addItems(port_list)

        self.serial.readyRead.connect(self.onRead)

    def onRead(self):
        '''数据接收并显示'''
        data = self.serial.readAll()
        # str_data=binascii.unhexlify(data)
        str_data = '[%s]: ' % datetime.now().ctime() + str(data)

        self.textBrowser.setPlainText(str_data)

    def build_data(self):
        '''产生数据
        - return
        bytes型数据'''

        # 包头2字节，长度1字节
        length = 3
        data = None

        # 消息ID，十进制数(0~65535)
        msgId = self.lineEdit_msgId.text()
        msgId = int(msgId)
        length += 2
        # 设备ID，十六进制数（0000~FFFF）
        devId = self.lineEdit_devId.text()
        devId = int(devId, 16)
        length += 2
        # 消息内容，ASCII字符串(最大长度255-9=246)
        msg = self.textEdit_msg.toPlainText()
        msg = bytes(msg, encoding='utf-8')
        length += len(msg)
        str_pack = '!BBBHH{0}s'.format(len(msg))

        # 加上校验字节长度
        length += 1

        data = struct.Struct(str_pack)
        # 包头两个字节AA55,长度，消息ID，设备ID，消息内容
        value = (0xAA, 0x55, length, msgId, devId, msg)

        data_bytes = data.pack(*value)
        check = self.getCheckByte(data_bytes)

        # 填充校验字节

        check_pack = struct.Struct('B')
        check_value = (check, )
        check_byte = check_pack.pack(*check_value)

        return data_bytes + check_byte

    def getCheckByte(self, data):
        sum = 0
        for b in data:
            sum ^= b
        return sum

    def slot_send_click(self):
        '''发送按钮点击事件'''
        if not self.serial.isOpen():
            return

        data = self.build_data()

        try:
            ret = self.serial.writeData(data)
        except Exception as e:
            print(e)

    def slot_port_change(self, text):
        '''端口号改变事件'''
        if not self.serial.isOpen():
            self.serial.setPortName(text)

    def slot_connect_click(self):
        '''连接设备'''
        if not self.serial.isOpen():
            self.serial.setBaudRate(self.serial.Baud115200)
            self.serial.setParity(self.serial.NoParity)
            self.serial.setDataBits(self.serial.Data8)
            self.serial.setStopBits(self.serial.OneStop)

            try:
                self.serial.open(QtCore.QIODevice.ReadWrite)
            except Exception as e:
                print(e)
                return
        else:
            self.serial.close()

        if self.serial.isOpen():
            self.pushButton_connect.setText('关闭连接')
            self.comboBox_port.setEnabled(False)
            QMessageBox.warning(self, '警告', '设备已经打开！')
        else:
            self.pushButton_connect.setText('打开连接')
            self.comboBox_port.setEnabled(True)
            QMessageBox.warning(self, '警告', '设备关闭！')


#!/usr/bin/env python3

import numpy as np
from scipy.spatial import distance, transform
import os

import rclpy
from rclpy.node import Node
from rclpy.time import Time, Duration
from sensor_msgs.msg import LaserScan
from ackermann_msgs.msg import AckermannDriveStamped, AckermannDrive
from nav_msgs.msg import Odometry
from visualization_msgs.msg import Marker, MarkerArray
from geometry_msgs.msg import Point, PointStamped, Quaternion, QuaternionStamped, TransformStamped, PoseStamped, Pose, PoseArray
import tf2_ros

class PurePursuit(Node):
    """
    Implement Pure Pursuit on the car
    """
    def __init__(self):
        super().__init__('pure_pursuit_node')

        self.declare_params()

        self.is_real = self.get_parameter("sim or real").value
        self.is_ascending = self.get_parameter("is ascending").value  # waypoint indices are ascending during tracking
        self.csv_name = self.get_parameter("csv name").value

        # Topics & Subs, Pubs
        drive_topic = '/drive'
        odom_topic = '/pf/viz/inferred_pose' if self.is_real else '/ego_racecar/odom'
        visualization_topic = '/visualization_marker_array'
        pp_point_topic = '/pp_point'
        gf_point_topic = '/gf_point'
        vis_gf_marker_topic = '/vis_gf_marker'

        # Subscribe to POSE
        self.sub_pose = self.create_subscription(PoseStamped if self.is_real else Odometry, odom_topic, self.pose_callback, 1)
        self.sub_pose  # prevent unused variable warning
        # Publish to drive
        self.pub_drive = self.create_publisher(AckermannDriveStamped, drive_topic, 1)
        self.drive_msg = AckermannDriveStamped()
        # Publish to visualization
        self.pub_vis = self.create_publisher(MarkerArray, visualization_topic, 1)
        self.markerArray = MarkerArray()

        # for motion planning
        # Publish goal point in car frame to reactive node
        self.pub_to_gf = self.create_publisher(PoseArray, pp_point_topic, 1)
        # Subscribe to gap following points
        self.sub_gf = self.create_subscription(Point, gf_point_topic, self.gap_following_callback, 1)
        self.sub_gf  # prevent unused variable warning

        # visualization of gap following point
        self.vis_gf_point_pub = self.create_publisher(Marker, vis_gf_marker_topic, 1)

        # loading waypoints
        csv_path = self.get_parameter("csv path").value
        csv_data = np.loadtxt(csv_path + '/' + self.csv_name + '.csv', delimiter=';', skiprows=0)  # csv data
        self.waypoints = csv_data[:, 1:3]  # first row is indices
        self.num_waypoints = self.waypoints.shape[0]
        self.ref_speed = csv_data[:, 5] * float(self.get_parameter("reference speed gain").value) # max speed - sim is 10m/s, levine 2nd - real is 6m/s

        self.visualization_init()

        # sim params
        self.L = float(self.get_parameter("lookahead distance").value)
        self.steering_gain = float(self.get_parameter("steering gain").value)
        self.test_speed = float(self.get_parameter("test speed").value)

    def declare_params(self):
        self.declare_parameter("sim or real")
        self.declare_parameter("is ascending")
        self.declare_parameter("csv name")
        self.declare_parameter("csv path")
        self.declare_parameter("reference speed gain")
        self.declare_parameter("lookahead distance")
        self.declare_parameter("steering gain")
        self.declare_parameter("test speed")

    def pose_callback(self, pose_msg):
        # Get current pose
        self.currX = pose_msg.pose.position.x if self.is_real else pose_msg.pose.pose.position.x
        self.currY = pose_msg.pose.position.y if self.is_real else pose_msg.pose.pose.position.y
        self.currPos = np.array([self.currX, self.currY]).reshape((1, 2))

        # Transform quaternion pose message to rotation matrix
        quat = pose_msg.pose.orientation if self.is_real else pose_msg.pose.pose.orientation
        quat = [quat.x, quat.y, quat.z, quat.w]
        R = transform.Rotation.from_quat(quat)
        self.rot = R.as_matrix()

        # Find closest waypoint to where we are
        self.distances = distance.cdist(self.currPos, self.waypoints, 'euclidean').reshape((self.num_waypoints))
        self.closest_index = np.argmin(self.distances)
        self.closestPoint = self.waypoints[self.closest_index]

        # Find target point
        targetPoint = self.get_closest_point_beyond_lookahead_dist(self.L)

        # Homogeneous transformation
        translatedTargetPoint = self.translatePoint(targetPoint)

        pose_arr_msg = PoseArray()
        pose_msg_target = Pose()
        pose_msg_target.position.x = translatedTargetPoint[0]
        pose_msg_target.position.y = translatedTargetPoint[1]
        pose_arr_msg.poses.append(pose_msg_target)
        pose_msg_closest = Pose()
        pose_msg_closest.position.x = self.closestPoint[0]
        pose_msg_closest.position.y = self.closestPoint[1]
        pose_arr_msg.poses.append(pose_msg_closest)
        pose_msg_curr = Pose()
        pose_msg_curr.position.x = self.currX
        pose_msg_curr.position.y = self.currY
        pose_arr_msg.poses.append(pose_msg_curr)

        self.pub_to_gf.publish(pose_arr_msg)

        # calculate curvature/steering angle
        # y = translatedTargetPoint[1]
        # gamma = self.steering_gain * (2 * y / self.L**2)

        # # publish drive message, don't forget to limit the steering angle.
        # gamma = np.clip(gamma, -0.35, 0.35)
        # self.drive_msg.drive.steering_angle = gamma
        # self.drive_msg.drive.speed = (-1.0 if self.is_real else 1.0) * self.ref_speed[self.closest_index]
        # self.pub_drive.publish(self.drive_msg)
        # print("steering = {}, speed = {}".format(round(self.drive_msg.drive.steering_angle, 2), round(self.drive_msg.drive.speed, 2)))

        # Visualizing points
        self.closestMarker.points = [Point(x = self.closestPoint[0], y = self.closestPoint[1], z = 0.0)]
        self.targetMarker.points = [Point(x = targetPoint[0], y = targetPoint[1], z = 0.0)]

        self.markerArray.markers = [self.waypointMarker, self.closestMarker, self.targetMarker]
        self.pub_vis.publish(self.markerArray)

    def gap_following_callback(self, gf_point_msg):
        gf_point_x = gf_point_msg.x
        gf_point_y = gf_point_msg.y
        gf_point_obs = bool(gf_point_msg.z)
        # print(f'received gf point:',gf_point_msg.x, gf_point_msg.y)

        self.gf_point_marker.points = [Point(x = gf_point_x, y = gf_point_y, z = 0.2)]
        self.vis_gf_point_pub.publish(self.gf_point_marker)

        # calculate speed & steering
        gamma = self.steering_gain * (2 * gf_point_y / self.L**2)
        gamma = np.clip(gamma, -0.35, 0.35)
        self.drive_msg.drive.steering_angle = gamma
        speed = (-1.0 if self.is_real else 1.0) * self.ref_speed[self.closest_index]
        # self.drive_msg.drive.speed = (0.2 if gf_point_obs else 1.0) * speed  # if allows braking for close obstacles
        self.drive_msg.drive.speed = speed
        # self.drive_msg.drive.speed = self.test_speed

        # publish drive message
        self.pub_drive.publish(self.drive_msg)
        print("steering = {}, speed = {}".format(round(self.drive_msg.drive.steering_angle, 2), round(self.drive_msg.drive.speed, 2)))

    def get_closest_point_beyond_lookahead_dist(self, threshold):
        point_index = self.closest_index
        dist = self.distances[point_index]

        while dist < threshold:
            if self.is_ascending:
                point_index += 1
                if point_index >= len(self.waypoints):
                    point_index = 0
                dist = self.distances[point_index]
            else:
                point_index -= 1
                if point_index < 0:
                    point_index = len(self.waypoints) - 1
                dist = self.distances[point_index]

        point = self.waypoints[point_index]

        return point

    def translatePoint(self, targetPoint):
        H = np.zeros((4, 4))
        H[0:3, 0:3] = np.linalg.inv(self.rot)
        H[0, 3] = self.currX
        H[1, 3] = self.currY
        H[3, 3] = 1.0
        pvect = targetPoint - self.currPos
        convertedTarget = (H @ np.array((pvect[0, 0], pvect[0, 1], 0, 0))).reshape((4))

        return convertedTarget

    def visualization_init(self):
        # Green
        self.waypointMarker = Marker()
        self.waypointMarker.header.frame_id = 'map'
        self.waypointMarker.type = Marker.POINTS
        self.waypointMarker.color.g = 0.75
        self.waypointMarker.color.a = 1.0
        self.waypointMarker.scale.x = 0.05
        self.waypointMarker.scale.y = 0.05
        self.waypointMarker.id = 0
        self.waypointMarker.points = [Point(x = wpt[0], y = wpt[1], z = 0.0) for wpt in self.waypoints]

        # Red
        self.targetMarker = Marker()
        self.targetMarker.header.frame_id = 'map'
        self.targetMarker.type = Marker.POINTS
        self.targetMarker.color.r = 0.75
        self.targetMarker.color.a = 1.0
        self.targetMarker.scale.x = 0.2
        self.targetMarker.scale.y = 0.2
        self.targetMarker.id = 1

        # Blue
        self.closestMarker = Marker()
        self.closestMarker.header.frame_id = 'map'
        self.closestMarker.type = Marker.SPHERE_LIST
        self.closestMarker.color.b = 0.75
        self.closestMarker.color.a = 0.4
        self.closestMarker.scale.x = 1.2
        self.closestMarker.scale.y = 1.2
        self.closestMarker.scale.z = 1.2
        self.closestMarker.id = 2

        # green
        self.gf_point_marker = Marker()
        self.gf_point_marker.header.frame_id = 'laser' if self.is_real else 'ego_racecar/base_link'
        self.gf_point_marker.type = Marker.POINTS
        self.gf_point_marker.color.g = 0.75
        self.gf_point_marker.color.a = 1.0
        self.gf_point_marker.scale.x = 0.2
        self.gf_point_marker.scale.y = 0.2
        self.gf_point_marker.id = 3

def main(args=None):
    rclpy.init(args=args)
    print("PurePursuit Initialized")
    pure_pursuit_node = PurePursuit()
    rclpy.spin(pure_pursuit_node)

    pure_pursuit_node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()


s = list(input().strip())

def convert_arr(s):
    result_int = []
    for i in s:
        result_int.append(ord(i) - ord('0'))
    return result_int
flag = True
s = convert_arr(s)
modulo_s = sum(s)%3
for i, val in enumerate(s):
    temp = val%3 + 6 - modulo_s
    if(temp<7):
        temp +=3
    if(temp <= val):
        continue
    else:
        s[i] = temp
        flag = False
        break
if(flag):
    t = s[-1]%3 + 6 - modulo_s
    if(s[-1]== t ):
        s[-1] -= 3
    else:
        s[-1] = t
print(*s,sep ='')


import os

import psycopg2

from cian import Ad

class DB:
    def __init__(self, dsn=os.getenv('DB_DSN')):
        self.conn = psycopg2.connect(dsn)

    def get_or_create(self, ad: Ad):
        c = self.conn.cursor()
        c.execute("SELECT * FROM ads WHERE ad_id=%s", (ad.id,))
        if c.fetchone():
            created = False
            c.execute(
                "UPDATE ads SET rooms=%s, address=%s, price=%s, phones=%s, description=%s, url=%s WHERE ad_id=%s",
                (ad.rooms, ad.address, ad.price, ad.phones, ad.description, ad.url, ad.id)
            )
        else:
            created = True
            c.execute(
                "INSERT INTO ads(ad_id, rooms, address, price, phones, description, url) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                ad
            )
        c.close()
        self.conn.commit()
        return ad, created


#!/usr/bin/env python
# encoding: utf-8
'''
@File  : 007_print_even_numbers.py
@Date  : 2021/11/2 23:15
@Author: Cxs
@Email: Cxs1103@163.com
@Desc  :
'''

def print_even_number(begin, end):
    list_number = []
    for number in range(begin, end):
        if number % 2 == 0:
            list_number.append(number)
    return list_number

print(f'1 to 15 even number is :', print_even_number(1, 15))
print(f'15 to 100 even number is :', print_even_number(15, 100))

data = [item for item in range(1,15) if item %2==0]
print(f'1 to 15 even number is :', data)


import os
import random
import numpy as np
import torch
import torch.nn as nn

def Netpara(net: nn.Module):
    '''
        detect the paramaters info of the pytorch net(torch.nn.Module type)
        zhuyijie @2019.5.19
    '''
    k=0
    for name, param in net.named_parameters():
      if param.requires_grad:
          print(name)
          l = 1
          print("该层的结构：" + str(list(param.size())))
          for j in param.size():
              l *= j
          print("该层参数和：" + str(l))
          k = k + l
    print("总参数数量和：" + str(k))

def setup_seed(seed: int):
    random.seed(seed)
    np.random.seed(seed)
    os.environ['PYTHONHASHSEED']=str(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)

    torch.backends.cudnn.benchmark = False
    torch.backends.cudnn.deterministic = True

def worker_init_fn(worker_id):
    """
    After creating the workers, each worker has an independent seed that
     is initialized to the current random sedd+the id of the worker
    :param worker_id
    :return
    """
    np.random.seed(1024+worker_id)

#############################################################
# print the running time fo each line inside of the function
# add decorate @profile to the tested function, then in the shell, run:
# [CUDA_LAUNCH_BLOCKING=1] kernprof  -lv  line_profiler_test.py
#############################################################
# with torch.autograd.profiler.profile(use_cuda=True) as prof:
#     y=x**2
#     y=y.mean()
#     y.backward()
# print(prof)
#############################################################

## Save and load the entire model.
# torch.save(net, 'cls_model.ckpt')
# model = torch.load('model.ckpt')

# # Save and load only the model parameters (recommended).
# torch.save(net.state_dict(), 'cls_model_params.ckpt')
# resnet.load_state_dict(torch.load('cls_params.ckpt'))

def log_string(out_str):
    LOG_FOUT.write(out_str+'\n')
    LOG_FOUT.flush()
    print(out_str)

def augment_fn(batch_xyz, batch_label, augment_ratio=0.5):
    bsize, num_point, _ = batch_xyz.shape

    # shuffle the orders of samples in a batch
    idx = np.arange(bsize)
    np.random.shuffle(idx)
    batch_xyz = batch_xyz[idx,:,:]
    batch_label = batch_label[idx]

    # shuffle the point orders of each sample
    batch_xyz = data_util.shuffle_points(batch_xyz)

    # perform augmentation on the first np.int32(augment_ratio*bsize) samples
    augSize = np.int32(augment_ratio * bsize)
    augment_xyz = batch_xyz[0:augSize, :, :]

    augment_xyz = data_util.rotate_point_cloud(augment_xyz)
    augment_xyz = data_util.rotate_perturbation_point_cloud(augment_xyz)
    augment_xyz = data_util.random_scale_point_cloud(augment_xyz)
    augment_xyz = data_util.shift_point_cloud(augment_xyz)

    batch_xyz[0:augSize, :, :] = augment_xyz

    return batch_xyz, batch_label


import os
from argparse import ArgumentParser

import yaml

os.environ['TF_KERAS'] = '1'
from keras_self_attention import SeqSelfAttention
from tensorflow.keras.layers import (LSTM, Add, Bidirectional, Concatenate,
                                     Dense, Embedding, Input, Softmax,
                                     TimeDistributed)
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adadelta

from keras_elmo import ElmoEmbeddingLayer
from model_utils import visualize_plot_mdl
from parsing_dataset import load_multilingual
from train_multitask import train_multitask_model
from utilities import configure_tf, initialize_logger

def parse_args():
    parser = ArgumentParser(description="WSD")
    parser.add_argument("--model_type", default='attention', type=str,
                        help="""Choose the model: baseline: BiLSTM Model.
                                attention: Attention Stacked BiLSTM Model.
                                seq2seq: Seq2Seq Attention.""")

    params = vars(parser.parse_args())

    return params

def baseline_model(vocabulary_size, config_params,
                   pos_vocab_size, lex_vocab_size,
                   output_size, tokenizer=None,
                   visualize=False, plot=False):
    hidden_size = int(config_params['hidden_size'])
    batch_size = int(config_params['batch_size'])

    input_type = 'string' if tokenizer is not None else None
    in_sentences = Input(shape=(None,), dtype=input_type,
                         batch_size=batch_size, name='Input')

    if tokenizer is not None:
        embedding = ElmoEmbeddingLayer()(in_sentences)
        embedding_size = 1024
    else:
        embedding_size = int(config_params['embedding_size'])
        embedding = Embedding(input_dim=vocabulary_size,
                              output_dim=embedding_size,
                              mask_zero=True,
                              name="Embeddings")(in_sentences)

    bilstm = Bidirectional(LSTM(hidden_size, dropout=0.2,
                                recurrent_dropout=0.2,
                                return_sequences=True,
                                input_shape=(None, None, embedding_size)
                                ),
                           merge_mode='sum')(embedding)

    logits = TimeDistributed(Dense(output_size))(bilstm)

    in_mask = Input(shape=(None, output_size), batch_size=batch_size,
                    name='Candidate_Synsets_Mask')

    logits_mask = Add()([logits, in_mask])

    pos_logits = TimeDistributed(Dense(pos_vocab_size),
                                 name='POS_logits')(bilstm)
    lex_logits = TimeDistributed(Dense(lex_vocab_size),
                                 name='LEX_logits')(bilstm)

    wsd_output = Softmax(name="WSD_output")(logits_mask)
    pos_output = Softmax(name="POS_output")(pos_logits)
    lex_output = Softmax(name="LEX_output")(lex_logits)

    model = Model(inputs=[in_sentences, in_mask],
                  outputs=[wsd_output, pos_output, lex_output],
                  name='BiLSTM_MultiTask')

    model.compile(loss="sparse_categorical_crossentropy",
                  optimizer=Adadelta(), metrics=['acc'])

    visualize_plot_mdl(visualize, plot, model)

    return model

def attention_model(vocabulary_size, config_params,
                    output_size, pos_vocab_size,
                    lex_vocab_size, visualize=False,
                    plot=False, tokenizer=None):
    hidden_size = int(config_params['hidden_size'])
    batch_size = int(config_params['batch_size'])

    input_type = 'string' if tokenizer is not None else None
    in_sentences = Input(shape=(None,), dtype=input_type,
                         batch_size=batch_size)

    if tokenizer is not None:
        embedding = ElmoEmbeddingLayer()(in_sentences)
        embedding_size = 1024
    else:
        embedding_size = int(config_params['embedding_size'])
        embedding = Embedding(input_dim=vocabulary_size,
                              output_dim=embedding_size,
                              mask_zero=True,
                              name="Embeddings")(in_sentences)

    bilstm = Bidirectional(LSTM(hidden_size, dropout=0.2,
                                recurrent_dropout=0.2,
                                return_sequences=True,
                                input_shape=(None, None, embedding_size)
                                ),
                           merge_mode='sum')(embedding)

    attention = SeqSelfAttention(attention_activation='sigmoid',
                                 name='Attention')(bilstm)

    logits = TimeDistributed(Dense(output_size))(attention)
    in_mask = Input(shape=(None, output_size), batch_size=batch_size,
                    name='Candidate_Synsets_Mask')
    logits_mask = Add()([logits, in_mask])

    pos_logits = TimeDistributed(Dense(pos_vocab_size),
                                 name='POS_logits')(attention)
    lex_logits = TimeDistributed(Dense(lex_vocab_size),
                                 name='LEX_logits')(attention)

    wsd_output = Softmax(name="WSD_output")(logits_mask)
    pos_output = Softmax(name="POS_output")(pos_logits)
    lex_output = Softmax(name="LEX_output")(lex_logits)

    model = Model(inputs=[in_sentences, in_mask],
                  outputs=[wsd_output, pos_output, lex_output],
                  name='BiLSTM_ATT_MultiTask')

    model.compile(loss="sparse_categorical_crossentropy",
                  optimizer=Adadelta(), metrics=['acc'])

    visualize_plot_mdl(visualize, plot, model)

    return model

def seq2seq_model(vocabulary_size, config_params, output_size,
                  pos_vocab_size, lex_vocab_size, tokenizer=None,
                  visualize=False, plot=False):
    hidden_size = int(config_params['hidden_size'])
    batch_size = int(config_params['batch_size'])
    embedding_size = int(config_params['embedding_size'])

    input_type = 'string' if tokenizer is not None else None
    in_sentences = Input(shape=(None,), dtype=input_type,
                         batch_size=batch_size, name='Input')

    if tokenizer is not None:
        embeddings = ElmoEmbeddingLayer()(in_sentences)
        embedding_size = 1024
    else:
        embeddings = Embedding(input_dim=vocabulary_size,
                               output_dim=embedding_size,
                               mask_zero=True,
                               name="Embeddings")(in_sentences)
    bilstm, forward_h, _, backward_h, _ = Bidirectional(LSTM(hidden_size, return_sequences=True,
                                                             return_state=True, dropout=0.2, recurrent_dropout=0.2,
                                                             input_shape=(None, None, embedding_size)),
                                                        merge_mode='sum',
                                                        name='Encoder_BiLSTM')(embeddings)
    state_h = Concatenate()([forward_h, backward_h])

    encoder_attention = SeqSelfAttention(attention_activation='sigmoid',
                                         name='Attention')([bilstm, state_h])

    concat = Concatenate()([encoder_attention, bilstm])

    decoder_fwd_lstm, _, _ = LSTM(hidden_size, dropout=0.2,
                                  recurrent_dropout=0.2,
                                  return_sequences=True,
                                  input_shape=(None, None, embedding_size),
                                  name='Decoder_FWD_LSTM')(concat)

    decoder_bck_lstm, _, _ = LSTM(hidden_size,
                                  dropout=0.2,
                                  recurrent_dropout=0.2,
                                  return_sequences=True,
                                  input_shape=(None, None, embedding_size),
                                  go_backwards=True,
                                  name='Decoder_BWD_LSTM')(decoder_fwd_lstm)

    decoder_bilstm = Concatenate()([decoder_fwd_lstm, decoder_bck_lstm])

    logits = TimeDistributed(
        Dense(output_size), name='WSD_logits')(decoder_bilstm)
    in_mask = Input(shape=(None, output_size),
                    batch_size=batch_size, name='Candidate_Synsets_Mask')

    logits_mask = Add(name="Masked logits")([logits, in_mask])
    pos_logits = TimeDistributed(Dense(pos_vocab_size),
                                 name='POS_logits')(decoder_bilstm)
    lex_logits = TimeDistributed(Dense(lex_vocab_size),
                                 name='LEX_logits')(decoder_bilstm)

    wsd_output = Softmax(name="WSD_output")(logits_mask)
    pos_output = Softmax(name="POS_output")(pos_logits)
    lex_output = Softmax(name="LEX_output")(lex_logits)

    model = Model(inputs=[in_sentences, in_mask],
                  outputs=[wsd_output, pos_output, lex_output],
                  name='Seq2Seq_MultiTask')

    model.compile(loss="sparse_categorical_crossentropy",
                  optimizer=Adadelta(), metrics=['acc'])

    visualize_plot_mdl(visualize, plot, model)

    return model

if __name__ == "__main__":
    params = parse_args()
    initialize_logger()

    # Load our config file
    config_file_path = os.path.join(os.getcwd(), "config.yaml")
    config_file = open(config_file_path)
    config_params = yaml.load(config_file)

    # run on CPU, change in Config file.
    if config_params["USE_CPU"]:
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ["CUDA_VISIBLE_DEVICES"] = ""
    else:
        configure_tf()

    elmo = config_params["use_elmo"]
    # dataset = load_dataset(elmo=elmo)
    dataset = load_multilingual()
    vocabulary_size = dataset.get("vocabulary_size")
    output_size = dataset.get("output_size")
    tokenizer = dataset.get("tokenizer")
    pos_vocab_size = dataset.get("pos_vocab_size")
    lex_vocab_size = dataset.get("lex_vocab_size")

    model = None
    tokenizer = tokenizer if elmo else None

    if params["model_type"] == "baseline":
        model = baseline_model(vocabulary_size, config_params,
                               pos_vocab_size, lex_vocab_size,
                               output_size, tokenizer=tokenizer)
        history = train_multitask_model(model, dataset, config_params, elmo)
    elif params["model_type"] == "attention":
        attention_model = attention_model(vocabulary_size, config_params,
                                          output_size, pos_vocab_size,
                                          lex_vocab_size, tokenizer=tokenizer,
                                          visualize=True)
        attention_history = train_multitask_model(attention_model, dataset, config_params, elmo)
    elif params["model_type"] == "seq2seq":
        seq2seq_model = seq2seq_model(vocabulary_size, config_params,
                                      pos_vocab_size, lex_vocab_size,
                                      output_size, tokenizer=tokenizer)
        seq2seq_history = train_multitask_model(seq2seq_model, dataset,
                                                config_params, elmo)


# balanced brackets
# use a stack

# START AT 10:15

# write a function taht takes in a tring made up of brackets (,[,{}]) and other optional characters. The function should return a boolean representing whether the string is balanced with regards to brackets.

# A string is said to be balanced if it has as many opening brackets of a certain type as it has closing brackets of that type and if no bracket is unmatched. Note that an opening bracket can't match a corresponding closing bracket that comes before it, and similarly, a closing bracket can't match a corresponding opening bracket that comes after it. Also, brackets can't overlap each other as in [(])

# sample input
# string = "([])(){}(())()()"

# string = "()"
# string = "(]"
# string = "()[]{}"
# string = "({)}"
# string = "()()((}}"

string = "()()[{()})]"
# string:"()(agawg)[{()gawggaw})]"
# should return false but returns true with my current code. passed 18/24 test cases

# def isBalanced(string):
#     stack = []
#     if string == "":
#         print(False)
#     else:
#         for character in string:
#             if character == "{" or character == "[" or character == "(":
#                 stack.append(character)
#             elif character == "}" and stack[-1] == "{" or character == ")" and stack[-1] == "(" or character == "]" and stack[-1] == "[":
#                 stack.pop()

#     if stack == []:
#         print(True)
#     else:
#         print(False)

# isBalanced(string)

# joe's solution
def isBalanced(string):

    openingBrackets = '([{'
    closingBrackets = '}])'
    hashTable = {
        "]": "[",
        ")": "(",
        "}": "{"
    }

    stack = []
    for character in string:
        # check if the character is in our openingbrackets
        if openingBrackets.includes(character):
            stack.append(character)
        elif closingBrackets.includes(character):
            if stack[len(stack)-1] == hashTable[character]:
                stack.pop()
            else:
                print(False)
    if len(stack) == 0:
        print(True)


import numpy as np
import pandas as pd

if __name__ == '__main__':
    # tweets = pd.read_csv('data/tweets_points_20.csv')
    res = pd.read_csv('results/memb20dim_cf.csv')
    topics = pd.read_csv('data/tweets_topics.csv')

    print(topics.shape)
    print(res.shape)

    id_to_topic = dict(zip(list(range(len(topics))), topics['topic'].tolist()))

    print('\n----- TOPICS -----')
    print(topics['topic'].value_counts())

    cluster_df = res.groupby('Cluster_id')['Point_id'].apply(list)

    for index, cluster in enumerate(cluster_df):

        print('----- CLUSTER {} -----'.format(index))
        points_labeled = [id_to_topic[p] for p in cluster]
        print(pd.Series(points_labeled).value_counts(normalize=True))


from nmigen import *

from gram.phy.ecp5ddrphy import _DQSBUFMSettingManager
from gram.test.utils import *

class DQSBUFMSettingManagerTestCase(FHDLTestCase):
    class MockCSR:
        def __init__(self):
            self.w_stb = Signal()
            self.w_data = Signal(3)

    def test_pause_timing(self):
        csr = self.MockCSR()
        dut = _DQSBUFMSettingManager(csr)

        def process():
            self.assertFalse((yield dut.pause))

            yield csr.w_stb.eq(1)
            yield
            yield csr.w_stb.eq(0)
            yield

            self.assertTrue((yield dut.pause))
            yield
            self.assertTrue((yield dut.pause))
            yield
            self.assertFalse((yield dut.pause))

        runSimulation(dut, process, "test_phy_ecp5ddrphy.vcd")

    def test_value(self):
        csr = self.MockCSR()
        dut = _DQSBUFMSettingManager(csr)

        def process():
            # Check default value
            self.assertEqual((yield dut.readclksel), 0)

            yield csr.w_data.eq(0b101)
            yield csr.w_stb.eq(1)
            yield
            yield csr.w_stb.eq(0)
            yield

            # Ensure value isn't being changed at that point
            self.assertEqual((yield dut.readclksel), 0)
            yield; yield Delay(1e-9)

            # Ensure value is changed after the second clock cycle
            self.assertEqual((yield dut.readclksel), 0b101)

        runSimulation(dut, process, "test_phy_ecp5ddrphy.vcd")


import streamlit as st
from urllib.error import URLError
import pandas as pd
from utilities import utils, translator
import os

import openai
from langchain.chat_models import AzureChatOpenAI
from langchain.embeddings import OpenAIEmbeddings
from langchain.document_loaders import DirectoryLoader
from langchain.document_loaders import TextLoader
from langchain.text_splitter import TokenTextSplitter
from langchain.vectorstores import FAISS
from langchain.chains import ConversationalRetrievalChain
from langchain.prompts import PromptTemplate

from langchain.document_loaders import WebBaseLoader

from dotenv import load_dotenv
load_dotenv()

def initialize(engine='davinci'):
    openai.api_type = "azure"
    openai.api_version = "2023-03-15-preview"
    openai.api_base = os.getenv('OPENAI_API_BASE')
    openai.api_key = os.getenv("OPENAI_API_KEY")

    # Initialize gpt-35-turbo and our embedding model
    # TODO -> model name should be a parameter
    llm = AzureChatOpenAI(deployment_name="gpt-35-turbo", openai_api_version="2023-03-15-preview")
    embeddings = OpenAIEmbeddings(model="text-embedding-ada-002", chunk_size=1)

    return (llm, embeddings)

def load_page(embeddings, llm):
    # loader = DirectoryLoader('.', glob="Strecha.txt", loader_cls=TextLoader)
    loader = WebBaseLoader(st.session_state['web_url'])
    documents = loader.load()
    text_splitter = TokenTextSplitter(chunk_size=1000, chunk_overlap=0)
    docs = text_splitter.split_documents(documents)

    # keep only first record from docs variable
    # docs = docs[:1]

    db = FAISS.from_documents(documents=docs, embedding=embeddings)

    # Adapt if needed
    CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template("""Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.

    Chat History:
    {chat_history}
    Follow Up Input: {question}
    Standalone question:""")

    qa = ConversationalRetrievalChain.from_llm(llm=llm,
                                            retriever=db.as_retriever(),
                                            condense_question_prompt=CONDENSE_QUESTION_PROMPT,
                                            return_source_documents=True,
                                            verbose=False)

    return qa

def get_response(qa, question):
    query = question
    # query = "Jak ušetřit energii?"
    result = qa({"question": query, "chat_history": st.session_state['messages_web_hist']})

    return result

def set_url():
    # st.write(f'URL: {st.session_state['web_url']}')
    st.session_state["web_url"] =  st.session_state["url_input"].strip()
    pass

# df = utils.initialize(engine='davinci')
(llm, embeddings) = initialize()

def display_conversation():
    if (len(st.session_state['messages_web_hist']) > 0):
        with st.expander("History"):
            for message in st.session_state['messages_web_hist']:
                st.caption(f":blue[Question:]: {message[0]}")
                st.caption(f":blue[Answer:]: {message[1]}")

try:

    default_prompt = ""
    default_question = ""
    default_answer = ""
    default_web = ""
    messages_web_hist = []

    if 'question' not in st.session_state:
        st.session_state['question'] = default_question
    if 'web_url' not in st.session_state:
        st.session_state['web_url'] = ""
    if 'qa' not in st.session_state:
        st.session_state['qa'] = ""
    if 'kbinit' not in st.session_state:
        st.session_state['kbinit'] = False
    if 'messages_web_hist' not in st.session_state:
        st.session_state['messages_web_hist'] = messages_web_hist
        # add_message("system", st.session_state['system'])

    #  Set page layout to wide screen and menu item
    menu_items = {
	'Get help': None,
	'Report a bug': None,
	'About': '''
	 ## Embeddings App
	 Embedding testing application.
	'''
    }
    st.set_page_config(layout="wide", menu_items=menu_items, initial_sidebar_state="collapsed")

    col1, col2, col3 = st.columns([1,2,1])
    with col1:
        st.image(os.path.join('images','microsoft.png'))
    with col3:
        with st.expander("Settings"):
            model = st.selectbox(
                "OpenAI Model",
                (os.environ['OPENAI_CHAT_ENGINES'].split(','))
            )
            st.text_area("System",height=100, key='system')
            st.tokens_response = st.slider("Tokens response length", 100, 2000, 1000)
            st.temperature = st.slider("Temperature", 0.0, 1.0, 0.8)

    if (not st.session_state['kbinit']):

        # occures when nothing is selected -> no web page
        if (st.session_state['web_url'] == ""):
            st.text_input("Enter URL (format: https://www.espn.com/)", key="url_input", on_change=set_url)

        # works only if web is selected but not crawled yet
        if (st.session_state['web_url'] != ""):
            st.session_state["qa"] = load_page(embeddings, llm)
            st.session_state['kbinit'] = True

    # works only if web has been crawled
    if (st.session_state['kbinit']):
        st.caption(f"Page {st.session_state['web_url']} loaded - you may ask questions.")
        question = st.text_input("Ask question based on selected web page content", default_question)
        if (question != ""):
            result = get_response(st.session_state["qa"], question)

            # st.write("Question:", question)
            st.write(":blue[Answer]:", result["answer"])

            st.session_state['messages_web_hist'].append((question, result["answer"]))

    # st.caption(f"To clear the conversation, refresh the page | current model: **{model}**")
    # TODO -> add model selection
    st.caption(f"To clear the conversation, refresh the page | current model: **ChatGPT**")

    col1, col2, col3 = st.columns([1,4,1])
    with col2:
        display_conversation()

except URLError as e:
    st.error(
        """
        **This demo requires internet access.**
        Connection error: %s
        """
        % e.reason
    )


from PIL import Image

def muda_para_cinza(image_entrada, image_saida):
    image = Image.open(image_entrada)
    image = image.convert('L') #Converte para outro modelo.
    image = image.save(image_saida)

if __name__ == "__main__":
    muda_para_cinza("freeza.jpg", "freezaCinza.jpg")


# #Range function
# Write a program that will ask the user for a message and
# the number of times they want that message displayed.
# Then output the message that number of times.
# Write a program that will calculate the average (mean) of a set of numbers.
# This time, the user is to be asked how many numbers are to be averaged,
# they must then enter this number of numbers. Your program will calculate
# and display the average of those numbers.

message = input("Enter the message: ")
number = input("Number of times: ")

a = int(number)
for i in range(a):
    print(message)


import LoadData

def Is_Equal_Str(str1, str2):
    str1 = str1.lower() # Unsorted Pokemon's name
    str2 = str2.lower() # Sorted name in dic
    list1 = list(str1)
    list2 = list(str2)
    list1.sort()
    list2.sort()
    if list1 == list2:
        return str2
    else:
        return '0'    # Not included in dic OR Spelling error

def Sort_Word(str1):
    length = len(str1)
    if length == 3:
        dic = LoadData.dic3
    elif length == 4:
        dic = LoadData.dic4
    elif length == 5:
        dic = LoadData.dic5
    elif length == 6:
        dic = LoadData.dic6
    elif length == 7:
        dic = LoadData.dic7
    elif length == 8:
        dic = LoadData.dic8
    else:
        dic = LoadData.dic9P

    for str2 in dic:
        # result = is_equal_chars(str1, str2)
        result = Is_Equal_Str(str1, str2)
        if result != '0':
            break
    return result

Unsorted_Word = input('Input the Unsorted Pokemon\'s Name: ')
Sorted_Word = Sort_Word(Unsorted_Word)
if Sorted_Word == '0':
    print('Not included in the dic or exist spelling mistakes!')
else:
    print('*********************************\nSorted Name:', Sorted_Word.title())


"""empty message

Revision ID: 453df01381a3
Revises: 57ce2f1e2b90
Create Date: 2021-06-27 23:31:32.772035

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '453df01381a3'
down_revision = '57ce2f1e2b90'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('tables', sa.Column('datechek', sa.String(length=100), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('tables', 'datechek')
    # ### end Alembic commands ###


# Result: Accepted

import itertools

def works(degs, turn_sequence):
    pos = 0
    for idx, i in enumerate(turn_sequence):
        if i:
            pos += degs[idx]
        else:
            pos -= degs[idx]
    if pos % 360 == 0:
        return True
    return False

n_turns = int(input())
degrees = []
for i in range(n_turns):
    degrees.append(int(input()))

if sum(degrees) == 360:
    print('YES')
else:
    maximum = max(degrees)
    idx = degrees.index(maximum)
    degrees.remove(maximum)
    if sum(degrees) == maximum:
        print('YES')
    elif sum(degrees) < maximum:
        print('NO')
    else:
        degrees.insert(idx, maximum)
        ls = list(itertools.product([0, 1], repeat=n_turns))
        for combination in ls:
            if works(degrees, combination):
                print('YES')
                break
        else:
            print('NO')


import platform
import shutil
import tempfile
import unittest

from unittest import mock

from acloud.internal.lib import driver_test_lib
from acloud.internal.lib import utils
from acloud.setup import setup_common
from acloud.setup.host_setup_runner import AvdPkgInstaller
from acloud.setup.host_setup_runner import CuttlefishCommonPkgInstaller
from acloud.setup.host_setup_runner import CuttlefishHostSetup

class CuttlefishHostSetupTest(driver_test_lib.BaseDriverTest):
    """Test CuttlsfishHostSetup."""

    LSMOD_OUTPUT = """nvidia_modeset        860160  6 nvidia_drm
module1                12312  1
module2                12312  1
ghash_clmulni_intel    16384  0
aesni_intel           167936  3
aes_x86_64             20480  1 aesni_intel
lrw                    16384  1 aesni_intel"""

    # pylint: disable=invalid-name
    def setUp(self):
        """Set up the test."""
        super(CuttlefishHostSetupTest, self).setUp()
        self.CuttlefishHostSetup = CuttlefishHostSetup()

    def testShouldRunFalse(self):
        """Test ShouldRun returns False."""
        self.Patch(utils, "CheckUserInGroups", return_value=True)
        self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True)
        self.assertFalse(self.CuttlefishHostSetup.ShouldRun())

    def testShouldRunTrue(self):
        """Test ShouldRun returns True."""
        # 1. Checking groups fails.
        self.Patch(
            utils, "CheckUserInGroups", return_value=False)
        self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True)
        self.assertTrue(self.CuttlefishHostSetup.ShouldRun())

        # 2. Checking modules fails.
        self.Patch(utils, "CheckUserInGroups", return_value=True)
        self.Patch(
            CuttlefishHostSetup, "_CheckLoadedModules", return_value=False)
        self.assertTrue(self.CuttlefishHostSetup.ShouldRun())

    # pylint: disable=protected-access
    def testCheckLoadedModules(self):
        """Test _CheckLoadedModules."""
        self.Patch(
            setup_common, "CheckCmdOutput", return_value=self.LSMOD_OUTPUT)

        # Required modules are all in lsmod should return True.
        self.assertTrue(
            self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module2"]))
        # Required modules are not all in lsmod should return False.
        self.assertFalse(
            self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module3"]))

class AvdPkgInstallerTest(driver_test_lib.BaseDriverTest):
    """Test AvdPkgInstallerTest."""

    # pylint: disable=invalid-name
    def setUp(self):
        """Set up the test."""
        super(AvdPkgInstallerTest, self).setUp()
        self.AvdPkgInstaller = AvdPkgInstaller()

    def testShouldNotRun(self):
        """Test ShoudRun should raise error in non-linux os."""
        self.Patch(platform, "system", return_value="Mac")
        self.assertFalse(self.AvdPkgInstaller.ShouldRun())

class CuttlefishCommonPkgInstallerTest(driver_test_lib.BaseDriverTest):
    """Test CuttlefishCommonPkgInstallerTest."""

    # pylint: disable=invalid-name
    def setUp(self):
        """Set up the test."""
        super(CuttlefishCommonPkgInstallerTest, self).setUp()
        self.CuttlefishCommonPkgInstaller = CuttlefishCommonPkgInstaller()

    def testShouldRun(self):
        """Test ShoudRun."""
        self.Patch(platform, "system", return_value="Linux")
        self.Patch(setup_common, "PackageInstalled", return_value=False)
        self.assertTrue(self.CuttlefishCommonPkgInstaller.ShouldRun())

    @mock.patch.object(shutil, "rmtree")
    @mock.patch.object(setup_common, "CheckCmdOutput")
    def testRun(self, mock_cmd, mock_rmtree):
        """Test Run."""
        fake_tmp_folder = "/tmp/cf-common"
        self.Patch(tempfile, "mkdtemp", return_value=fake_tmp_folder)
        self.Patch(utils, "GetUserAnswerYes", return_value="y")
        self.Patch(CuttlefishCommonPkgInstaller, "ShouldRun", return_value=True)
        self.CuttlefishCommonPkgInstaller.Run()
        self.assertEqual(mock_cmd.call_count, 1)
        mock_rmtree.assert_called_once_with(fake_tmp_folder)

if __name__ == "__main__":
    unittest.main()


#!/usr/local/bin/python
#
# diction.py - playing with dictionaries
#
import pri

filename = 'templ8.py'

# Say hello to the Nice People
pri.ntc(filename + " : Start.")
pri.bar()

D = {'a': 1, 'b': 2, 'c': 3}
pri.ntc(D)
pri.bar()

D['e']=42
pri.ntc(D)
pri.bar()

#underfined value will break
#print D['f']

pri.ntc('f' in D)
pri.bar()
if not 'f' in D:
    pri.ntc("f is missing")
    pri.ntc("no really")

pri.bar()
pri.ntc(D.get('x',-1))

pri.bar()

Ks = list(D.keys())
pri.ntc(Ks)
pri.bar()

Ks.sort()
pri.ntc(Ks)
pri.bar()

for key in sorted(D):
    print(key, '=>', D[key])

pri.bar()
for c in 'spam spam spam':
    print(c.upper())

pri.bar()
x = 4
while x > 0:
    print('spam! ' * x)
    x -= 1

pri.bar()
pri.ntc("Done.")


import tweepy
import datetime
import time
import ai.ai.twitter.settings as settings

def get_twitter_data(file_name):
    # python で Twitter APIを使用するためのConsumerキー、アクセストークン設定
    # <Twitter API申請して取得したConsumer_key>
    Consumer_key = settings.CONSUMER_KEY
    # <Twitter API申請して取得したConsumer_secret>
    Consumer_secret = settings.CONSUMER_SECRET
    # <Twitter API申請して取得したAccess_token>
    Access_token = settings.ACCESS_TOKEN
    # <Twitter API申請して取得したAccess_secret>
    Access_secret = settings.ACCESS_SECRET

    # 認証
    auth = tweepy.OAuthHandler(Consumer_key, Consumer_secret)
    auth.set_access_token(Access_token, Access_secret)

    api = tweepy.API(auth, wait_on_rate_limit=True)

    # 日付
    sinceDate = datetime.datetime.strptime(file_name, '%Y%m%d')  # この日付以降のツイートを取得する
    untilDate = datetime.datetime.strptime(file_name, '%Y%m%d') + datetime.timedelta(days=1)  # この日付以前のツイートを取得する
    print(sinceDate)
    print(untilDate)

    # つぶやきを格納するリスト
    tweets_data = []

    # 現在の自分の上限とwindowごと使用回数の確認
    print(api.rate_limit_status()["resources"]["search"])

    # ユーザリスト取得
    # 'tarou'という名称を部分的に設定しているuserを検索する
    users = api.search_users(q='aaa')

    # 取得したuserのscreen_nameを表示する。実userなのでマスク処理を施す
    for name in users:
        n = name.screen_name
        print(n.replace(n[0:3], '***'))

    trends = api.trends_available()
    for trend in trends:
        print(trend)

    # 現在の自分の上限とwindowごと使用回数の確認
    print(api.rate_limit_status()["resources"]["search"])

if __name__ == '__main__':
    # 出力ファイル名を入力(相対パス or 絶対パス)
    print('====== Enter Tweet Data file =====')
    data_file = input('>  ')

    get_twitter_data(data_file)


line = [int(x) for x in input().split()]
x = line[0]
y = line[1]
if x == 0 and y == 1:
	print("ALL GOOD")
elif y == 1:
	print("IMPOSSIBLE")
elif x % (1-y) == 0:
	print(x//(1-y))
else:
	print(x/(1-y))


var_and_values = input().split(",")

var_and_values = dict((key.strip(), value.strip()) for key, value in (element.split('-') for element in var_and_values))

var = var_and_values.keys()

variables=list()

for key in var:
    variables.append(key)

val = var_and_values.values()

values=list()

for value in val:
    values.append(value)

expression = input()

exp=""

def ex_eval(expression):
    if 'or' in expression:
        expression = expression.split('or')
        operator = 'or'
    else:
        expression = expression.split('and')
        operator = 'and'
    if operator == 'or':
        if expression[0].strip() in variables:
            return expression[0].strip()
        return expression[1].strip()
    elif operator == 'and':
        return expression[0].strip()+" "+operator+" "+expression[1].strip()

def evl(operand1, operator, operand2):
    if operator == 'or':
        if operand1 in values:
            return operand1
        return operand2
    elif operator == 'and':
        return operand1+operand2

def extract(index):
    exp=""
    index= int(index)
    while expression[index] != ')' and index < len(expression):
        exp += expression[index]
        index+=1
    return ex_eval(exp[1:index])

for index in range(len(expression)):
    if expression[index] == ' ':
        continue
    elif expression[index] == '(':
        exp = extract(index)

        expression = expression.replace(expression[expression.find('('):expression.find(')')+1], exp) # replace the expression inside the parenthesis
        break

op=""
index=1

expression = expression.split(" ")

op += var_and_values.get(expression[0], "")

while index < len(expression):
    op = evl(op, expression[index], var_and_values.get(expression[index+1], ""))
    index+=2
print(op)


# 114. Flatten Binary Tree to Linked List
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def flatten2(self, root):
        self.dfs(root)

    def dfs(self, node):
        if not node: return None
        left = self.dfs(node.left)
        right = self.dfs(node.right)
        if not left and not right:
            return (node, node)
        elif not right:
            node.left = None
            node.right = left[0]
            return (node, left[1])
        elif left and right:
            node.left = None
            node.right = left[0]
            left[1].right = right[0]
        return (node, right[1])

    def dfs2(self, node):
        if not node: return None
        left = self.dfs2(node.left)
        right = self.dfs2(node.right)

        if not left and not right: return node
        if left:
            left.right = node.right
            node.right = node.left
            node.left = None
        if right: return right
        return left

    prev = None
    def flatten(self, root):
        if not root: return
        self.flatten(root.right)
        self.flatten(root.left)
        root.right = self.prev
        root.left = None
        self.prev = root

leetcode/0-250/200-114. Flatten Binary Tree to Linked List.py


import numpy as np
import pandas as pd
temp='C:/unc/cep-developer-assessment/exercise1/input/%s.csv'%'xl'
xl_bars=pd.read_csv(temp)
df=pd.DataFrame(
	{"fdntext":xl_bars.fdntext,
	"fldimp":xl_bars.fldimp,
	"undrfld":xl_bars.undrfld,
	"advknow":xl_bars.advknow,
	"pubpol":xl_bars.pubpol,
	"comimp":xl_bars.comimp,
	"undrwr":xl_bars.undrwr,
	"undrsoc":xl_bars.undrsoc,
	"orgimp":xl_bars.orgimp,
	"impsust":xl_bars.impsust},columns=["fdntext",
		"fldimp",
		"undrfld",
		"advknow",
		"pubpol",
		"comimp",
		"undrwr",
		"undrsoc",
		"orgimp",
		"impsust"])
df=df.replace('NaN',0).replace('77',0).replace('88',0)
mean=df.groupby('fdntext').mean()
mean.to_csv('C:/unc/cep-developer-assessment/exercise1/output/%s.csv'%'mean.csv', sep='\t', encoding='utf-8')

means=mean.describe()
means.to_csv('C:/unc/cep-developer-assessment/exercise1/output/%s.csv'%'stats.csv', sep='\t', encoding='utf-8')


from django.shortcuts import render
from . import pool
from django.http import JsonResponse

def contactus(request):
    try:
        db,cmd=pool.ConnectionPooling()
        fullname=request.POST['fullname']
        emailaddres=request.POST['emailaddres']
        number=request.POST['number']
        message = request.POST['message']
        query="insert into contact(fullname,emailaddres,number,message) values('{0}','{1}','{2}','{3}')".format(fullname,emailaddres,number,message)
        print(query)
        cmd.execute(query)
        db.commit()
        db.close()
        return render(request,"contact.html",{'message':'Thank you for the Contacting us'})
    except Exception as e:
        print("Error:",e)
        return render(request,"login.html")

def booknow(request):
    try:
        db,cmd=pool.ConnectionPooling()
        name=request.POST['name']
        email=request.POST['email']
        number=request.POST['number']
        query="insert into book(name,email,number) values('{0}','{1}','{2}')".format(name,email,number)
        print(query)
        cmd.execute(query)
        db.commit()
        db.close()
        return render(request,"booknow.html",{'message':'Thank you for Booking'})
    except Exception as e:
        print("Error:",e)
        return render(request,"login.html")


import math
from functools import reduce
n = int(input())
a = list(map(int, input().split()))

def gcd_list(numbers):
    if not numbers:
        return 0
    return reduce(math.gcd, numbers)

lefts = [0]*(n)
rights = [0]*(n)

for i in range(1, n):
    lefts[i] = math.gcd(lefts[i-1], a[i-1])
    rights[i] = math.gcd(rights[i-1], a[n-i])

max_ = 0
for i in range(n):
    left = lefts[i]
    right = rights[n-i-1]
    max_ = max(math.gcd(left, right), max_)

print(max_)


#QUESTION 7: GIVEN AN ARRAY OF 1S AND 0S WHICH HASALL 1S FIRST FOLLOWED BY 0S. WRIET A FUNCTION CALLED COUNTZEROS
#WHICH RETURNS NUMBER OF ZERO IN ARRAY.

import math
def countzero(customList):
    left=0
    right=len(customList)-1
    while left<=right:
        middle=math.floor((left+right)/2)
        if customList[middle]==1:
            left=middle+1
        else:
            rigght=middle-1
    return len(customList)-left         #minus 1s


#!/usr/bin/env python

import os, sys
import getopt
import netifaces

def usage():
    print ("\nFruityWiFi utils v1.1 (@xtr4nge)")

    print ("Usage: network.py <options>\n")
    print ("Options:")
    print ("-t <type>, --type=<type>                 TYPE")
    print ("-i <iface>, --iface=<iface>              INTERFACE")
    print ("")
    print ("Author: @xtr4nge")
    print ("")

def parseOptions(argv):
    TYPE = ""
    IFACE = ""

    try:
        opts, args = getopt.getopt(argv, "ht:i:",
                                   ["help", "type=", "iface="])

        for opt, arg in opts:
            if opt in ("-h", "--help"):
                usage()
                sys.exit()
            elif opt in ("-t", "--type"):
                TYPE = arg
            elif opt in ("-i", "--iface"):
                IFACE = arg

        if TYPE == "":
            usage()
            print
            print ("[Required] \n        -t (TYPE: getiface) \n")
            print

            sys.exit(1)

        return (TYPE, IFACE)

    except getopt.GetoptError:
        usage()
        sys.exit(2)

def getIface():
    for i in netifaces.interfaces():
        try: MAC = netifaces.ifaddresses(i)[netifaces.AF_LINK][0]["addr"]
        except: MAC = ""
        try: IP = netifaces.ifaddresses(i)[netifaces.AF_INET][0]["addr"]
        except: IP = ""
        print (str(i)+"|"+str(IP)+"|"+str(MAC))

def getIfaceNAME():
    for i in netifaces.interfaces():
        print (i)

def getIfaceIP(iface):
    try: IP = netifaces.ifaddresses(iface)[netifaces.AF_INET][0]["addr"]
    except: IP = ""
    print (IP)

def getIfaceMAC(iface):
    try: MAC = netifaces.ifaddresses(iface)[netifaces.AF_LINK][0]["addr"]
    except: MAC = ""
    print (MAC)

def main(argv):
    (TYPE, IFACE) = parseOptions(argv)

    if TYPE == "getiface":
        getIface()
    if TYPE == "getifacename":
        getIfaceNAME()
    if TYPE == "getifaceip" and IFACE != "":
        getIfaceIP(IFACE)
    if TYPE == "getifacemac" and IFACE != "":
        getIfaceMAC(IFACE)

if __name__ == "__main__":
    main(sys.argv[1:])


from __future__ import annotations

from typing import List, NewType

import scipp as sc

from beamlime.constructors import Factory, ProviderGroup, SingletonProvider

from .parameters import FrameRate, HistogramBinSize, NumPixels

# Coordinates
PixelID = NewType("PixelID", sc.Variable)
EventTimeOffset = NewType("EventTimeOffset", sc.Variable)
Length = NewType("Length", sc.Variable)
WaveLength = NewType("WaveLength", sc.Variable)

# Constants
FirstPulseTime = NewType("FirstPulseTime", sc.Variable)
FrameUnwrappingGraph = NewType("FrameUnwrappingGraph", dict)
LtotalGraph = NewType("LtotalGraph", dict)
WavelengthGraph = NewType("WavelengthGraph", dict)

# Generated/Calculated
Events = NewType("Events", List[sc.DataArray])
MergedData = NewType("MergedData", sc.DataArray)
PixelIDEdges = NewType("PixelIDEdges", sc.Variable)
PixelGrouped = NewType("PixelGrouped", sc.DataArray)
LTotalCalculated = NewType("Transformed", sc.DataArray)
FrameUnwrapped = NewType("FrameUnwrapped", sc.DataArray)
ReducedData = NewType("ReducedData", sc.DataArray)
Histogrammed = NewType("Histogrammed", sc.DataArray)

def provide_Ltotal_graph() -> LtotalGraph:
    c_a = sc.scalar(0.00001, unit='m')
    c_b = sc.scalar(0.1, unit='m')

    return LtotalGraph(
        {
            'L1': lambda pixel_id: (pixel_id * c_a) + c_b,
            'L2': lambda pixel_id: (pixel_id * c_a) + c_b,
            'Ltotal': lambda L1, L2: L1 + L2,
        }
    )

def provide_wavelength_graph() -> WavelengthGraph:
    c_c = sc.scalar(1, unit='1e-3m^2/s')

    return WavelengthGraph(
        {'wavelength': lambda tof, Ltotal: (c_c * tof / Ltotal).to(unit='angstrom')}
    )

def merge_data_list(da_list: Events) -> MergedData:
    return MergedData(sc.concat(da_list, dim='event'))

def provide_pixel_id_bin_edges(num_pixels: NumPixels) -> PixelIDEdges:
    return PixelIDEdges(sc.arange(dim='pixel_id', start=0, stop=num_pixels))

def bin_pixel_id(da: MergedData, pixel_bin_coord: PixelIDEdges) -> PixelGrouped:
    return PixelGrouped(da.group(pixel_bin_coord))

def calculate_ltotal(
    binned: PixelGrouped,
    graph: LtotalGraph,
) -> LTotalCalculated:
    da = binned.transform_coords(['Ltotal'], graph=graph)
    if not isinstance(da, sc.DataArray):
        raise TypeError

    return LTotalCalculated(da)

def unwrap_frames(
    da: LTotalCalculated, frame_rate: FrameRate, first_pulse_time: FirstPulseTime
) -> FrameUnwrapped:
    from scippneutron.tof import unwrap_frames

    return FrameUnwrapped(
        unwrap_frames(
            da,
            pulse_period=sc.scalar(1 / frame_rate, unit='ns'),  # No pulse skipping
            lambda_min=sc.scalar(5.0, unit='angstrom'),
            frame_offset=first_pulse_time.to(unit='ms'),
            first_pulse_time=first_pulse_time,
        )
    )

def calculate_wavelength(
    unwrapped: FrameUnwrapped, graph: WavelengthGraph
) -> ReducedData:
    da = unwrapped.transform_coords(['wavelength'], graph=graph)
    if not isinstance(da, sc.DataArray):
        raise TypeError

    return ReducedData(da)

def histogram_result(
    bin_size: HistogramBinSize, reduced_data: ReducedData
) -> Histogrammed:
    return reduced_data.hist(wavelength=bin_size)

Workflow = NewType("Workflow", Factory)

def provide_workflow(
    num_pixels: NumPixels, histogram_binsize: HistogramBinSize, frame_rate: FrameRate
) -> Workflow:
    providers = ProviderGroup(
        SingletonProvider(provide_wavelength_graph),
        SingletonProvider(provide_Ltotal_graph),
        merge_data_list,
        bin_pixel_id,
        calculate_ltotal,
        calculate_wavelength,
        unwrap_frames,
        histogram_result,
        provide_pixel_id_bin_edges,
    )

    providers[NumPixels] = lambda: num_pixels
    providers[HistogramBinSize] = lambda: histogram_binsize
    providers[FrameRate] = lambda: frame_rate
    return Workflow(Factory(providers))

workflow_providers = ProviderGroup(SingletonProvider(provide_workflow))


import socketio

sio = socketio.Client()

@sio.on('message')
def on_message(data):
    print('I received a message!')

sio.connect('http://127.0.0.1:5000')

login = input("Введите логин: ")
while True:
    message = input("Введите сообщение: ")
    sio.emit({'login': login, 'message': message})


def find_min_max_unique_numbers(list1):

  unique_list = list(set(list1))
  min_number = min(unique_list)
  max_number = max(unique_list)

  return min_number, max_number

sample_list = [87, 52, 44, 53, 54, 87, 52, 53]

min_number, max_number = find_min_max_unique_numbers(sample_list)

print("Minimum number is: ", min_number)
print("Maximum number is: ", max_number)


### FUNCTION START

def party_mode(color_sensor_one = 'C', color_sensor_two = 'D', party_length = 20):
    from random import random
    cs_one = ColorSensor(color_sensor_one)
    cs_two = ColorSensor(color_sensor_two)
    timer = Timer()
    timer.reset()

    # the first half of the party is random lights at random intensities
    while timer.now() < (party_length/2):
        intensity = int(100 * random())
        light_choice = 6 * random()
        if light_choice <= 1:
            cs_one.light_up(intensity, 0, 0)
        elif light_choice <= 2:
            cs_one.light_up(0, intensity, 0)
        elif light_choice <= 3:
            cs_one.light_up(0, 0, intensity)
        elif light_choice <= 4:
            cs_two.light_up(intensity, 0, 0)
        elif light_choice <= 5:
            cs_two.light_up(0, intensity, 0)
        elif light_choice <= 6:
            cs_two.light_up(0, 0, intensity)
        wait_for_seconds(0.05)
    cs_one.light_up_all(0)
    cs_two.light_up_all(0)
    wait_for_seconds(0.2)

    # the second half of the paty is bright blinking lights
    # at random intervals
    toggle = 1
    while timer.now() < party_length:
        wait_time = random() * 0.25
        cs_one.light_up_all(100 * toggle)
        cs_two.light_up_all(100 * toggle)
        wait_for_seconds(wait_time)
        if toggle == 1:
            toggle = 0
        else:
            toggle = 1

    # turn off the lights at the end of the party
    cs_one.light_up_all(0)
    cs_two.light_up_all(0)

### FUNCTION END


import os
import re
import json
import shutil
from distutils.dir_util import copy_tree

from src.utils import *

import torch
from torchvision.ops import nms
import pandas as pd
import numpy as np

from ultralytics import YOLO
from PIL import Image

def box_iou(box1, box2, eps=1e-7):
    # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
    """
    Return intersection-over-union (Jaccard index) of boxes.
    Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
    Arguments:
        box1 (Tensor[N, 4])
        box2 (Tensor[M, 4])
    Returns:
        iou (Tensor[N, M]): the NxM matrix containing the pairwise
            IoU values for every element in boxes1 and boxes2
    """

    # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
    (a1, a2), (b1, b2) = box1.unsqueeze(1).chunk(2, 2), box2.unsqueeze(0).chunk(2, 2)
    inter = (torch.min(a2, b2) - torch.max(a1, b1)).clamp(0).prod(2)

    # IoU = inter / (area1 + area2 - inter)
    return inter / ((a2 - a1).prod(2) + (b2 - b1).prod(2) - inter + eps)

def box_iop(box1, box2, eps=1e-7):
    # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
    """
    Return intersection-over-union (Jaccard index) of boxes.
    Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
    Arguments:
        box1 (Tensor[N, 4])
        box2 (Tensor[M, 4])
    Returns:
        iou (Tensor[N, M]): the NxM matrix containing the pairwise
            IoU values for every element in boxes1 and boxes2
    """

    # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
    (a1, a2), (b1, b2) = box1.unsqueeze(1).chunk(2, 2), box2.unsqueeze(0).chunk(2, 2)
    inter = (torch.min(a2, b2) - torch.max(a1, b1)).clamp(0).prod(2)

    # IoU = inter / (area1 + area2 - inter)
    return inter / ((b2 - b1).prod(2) + eps)

def box_iol(box1, box2, eps=1e-7):
    # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
    """
    Return intersection-over-union (Jaccard index) of boxes.
    Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
    Arguments:
        box1 (Tensor[N, 4])
        box2 (Tensor[M, 4])
    Returns:
        iou (Tensor[N, M]): the NxM matrix containing the pairwise
            IoU values for every element in boxes1 and boxes2
    """

    # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
    (a1, a2), (b1, b2) = box1.unsqueeze(1).chunk(2, 2), box2.unsqueeze(0).chunk(2, 2)
    inter = (torch.min(a2, b2) - torch.max(a1, b1)).clamp(0).prod(2)

    # IoU = inter / (area1 + area2 - inter)
    return inter / ((a2 - a1).prod(2) + eps)

def _process_batch(detections, labels, enable_iop=False):
    """
    Return correct prediction matrix
    Arguments:
        detections (array[N, 6]), x1, y1, x2, y2, conf, class
        labels (array[M, 5]), class, x1, y1, x2, y2
    Returns:
        correct (array[N, 10]), for 10 IoU levels
    """
    iouv = torch.linspace(0.5, 0.95, 10)
    iou = box_iou(labels[:, 1:], detections[:, :4])
    iop = box_iop(labels[:, 1:], detections[:, :4])
    iol = box_iol(labels[:, 1:], detections[:, :4])
    correct = np.zeros((detections.shape[0], iouv.shape[0])).astype(bool)
    correct_class = labels[:, 0:1] == detections[:, 5]

    all_indices = []
    all_matches = []
    all_original_matches = []
    for i in range(len(iouv)):
        if enable_iop:
            match = iou >= iouv[i]
            supression = ((iou >= (iouv[i]*0.5)) & (iop > 0.99)) | ((iou >= (iouv[i]*0.5)) & (iol > 0.99))
            partial_supression = ((iou >= (iouv[i]*0.8)) & (iop > 0.9)) | ((iou >= (iouv[i]*0.8)) & (iol > 0.9))
            iou_idx = match | supression | partial_supression
        else:
            iou_idx = iou >= iouv[i]
        x = torch.where(iou_idx & correct_class)  # IoU > threshold and classes match
        matches = np.zeros((x[0].shape[0], 3))
        original_matches = copy.deepcopy(matches)
        if x[0].shape[0]:
            matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]),
                                1).cpu().numpy()  # [label, detect, iou]
            original_matches = copy.deepcopy(matches)
            if x[0].shape[0] > 1:
                matches = matches[matches[:, 2].argsort()[::-1]] # sort matches by iou
                matches = matches[np.unique(matches[:, 1], return_index=True)[1]] # drop duplicates
                original_matches = copy.deepcopy(matches)
                # matches = matches[matches[:, 2].argsort()[::-1]]
                matches = matches[np.unique(matches[:, 0], return_index=True)[1]]

            correct[matches[:, 1].astype(int), i] = True

        all_indices.append(x)
        all_matches.append(matches)
        all_original_matches.append(original_matches)

    correct_matrix = torch.tensor(correct, dtype=torch.bool, device=detections.device)
    return correct_matrix, all_matches, all_indices, all_original_matches

def xywh2xyxy(x):

    """
    Convert bounding box coordinates from (x, y, width, height) format to (x1, y1, x2, y2) format where (x1, y1) is the
    top-left corner and (x2, y2) is the bottom-right corner.

    Args:
        x (np.ndarray) or (torch.Tensor): The input bounding box coordinates in (x, y, width, height) format.
    Returns:
        y (np.ndarray) or (torch.Tensor): The bounding box coordinates in (x1, y1, x2, y2) format.
    """

    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[..., 0] = x[..., 0] - x[..., 2] / 2  # top left x
    y[..., 1] = x[..., 1] - x[..., 3] / 2  # top left y
    y[..., 2] = x[..., 0] + x[..., 2] / 2  # bottom right x
    y[..., 3] = x[..., 1] + x[..., 3] / 2  # bottom right y
    return y

def xyxy2xywh(x):
    """
    Convert bounding box coordinates from (x1, y1, x2, y2) format to (x, y, width, height) format.

    Args:
        x (np.ndarray) or (torch.Tensor): The input bounding box coordinates in (x1, y1, x2, y2) format.
    Returns:
       y (np.ndarray) or (torch.Tensor): The bounding box coordinates in (x, y, width, height) format.
    """
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[..., 0] = (x[..., 0] + x[..., 2]) / 2  # x center
    y[..., 1] = (x[..., 1] + x[..., 3]) / 2  # y center
    y[..., 2] = x[..., 2] - x[..., 0]  # width
    y[..., 3] = x[..., 3] - x[..., 1]  # height
    return y

def read_predictions(predictions_path):
    with open(predictions_path, "r") as f:
        preds_json = json.load(f)

    preds_dict = {}
    for p in preds_json:
        image_id = p["image_id"]
        box = [p["bbox"], p["score"]]
        preds_dict[image_id] = preds_dict.get(image_id, []) + [box]

    predictions = {}
    for image_id, boxes in preds_dict.items():
        preds_conf = np.array([entry[1] for entry in boxes])
        preds_boxes = np.array([entry[0] for entry in boxes])
        preds_boxes[:, :2] += preds_boxes[:, 2:]/2
        preds_boxes = xywh2xyxy(preds_boxes)
        detections = np.append(preds_boxes, preds_conf.reshape(-1, 1), axis=1)
        detections = torch.tensor(np.append(detections, np.zeros((detections.shape[0], 1)), axis=1))
        predictions[image_id] = detections

    return predictions

def preprocess_predictions(images, results):
    predictions = {}
    for img_fn, result in zip(images, results):
        fn_origin = re.sub(".jpg$", "", img_fn)
        boxes = result.boxes.xyxy
        confs = result.boxes.conf.unsqueeze(1)
        cls = torch.zeros(boxes.shape[0], 1)

        predictions[fn_origin] = torch.cat([boxes, confs, cls], axis=1)
    return predictions

# TODO: change
def update_labels(gt_path, predictions, pr_label_positive, pr_label_negative, pr_bg_positive, precision, conf):

    updated_labels = []
    UNMATCHED_CONF = 1e-4
    EPSILON = 1e-4
    P_T_N = pr_label_negative[0, 0]

    for fn in os.listdir(gt_path):
        fn_origin = re.sub(".txt$", "", fn)

        detections = predictions.get(fn_origin, [])
        npr = detections.shape[0]

        if os.path.getsize(os.path.join(gt_path, fn)) > 0:
            gt_boxes = pd.read_csv(os.path.join(gt_path, fn), header=None, sep=" ")
            nl = gt_boxes.shape[0]
        else:
            nl = 0

        if nl == 0 and npr == 0:
            new_labels = torch.tensor([])

        elif nl > 0 and npr == 0:
            gt_boxes = xywh2xyxy(gt_boxes.iloc[:, 1:].values) * 320
            labels = torch.tensor(np.append(np.zeros((gt_boxes.shape[0], 1)), gt_boxes, axis=1))
            confs = torch.full((labels.shape[0], 1), UNMATCHED_CONF)
            precisions = torch.full((labels.shape[0], 1), P_T_N)
            cls = torch.full((labels.shape[0], 1), 0, dtype=torch.int64)
            new_labels = torch.cat([labels[:, 1:], confs, precisions, cls], axis=1)

        elif nl == 0 and npr > 0:
            unmatched_detections_boxes = detections[:, :4]
            unmatched_detections_confs = detections[:, 4].unsqueeze(1)
            unmatched_detections_precisions = torch.tensor(
                np.interp(unmatched_detections_confs, conf, pr_bg_positive[0, :], left=EPSILON))
            unmatched_detections_cls = torch.full_like(unmatched_detections_precisions, 2, dtype=torch.int64)
            new_labels = torch.cat([unmatched_detections_boxes, unmatched_detections_confs, unmatched_detections_precisions, unmatched_detections_cls], axis=1)
            new_labels = new_labels[new_labels[:, -2] >= 0.3]
            # new_labels = torch.tensor([])
        else:
            gt_boxes = xywh2xyxy(gt_boxes.iloc[:, 1:].values) * 320
            labels = torch.tensor(np.append(np.zeros((gt_boxes.shape[0], 1)), gt_boxes, axis=1))

            _, matches_iouv, matches_indices, raw_matches_iouv = _process_batch(detections, labels, enable_iop=True)
            matches = matches_iouv[0]#.astype(int)
            indices = matches_indices[0]
            raw_matches = raw_matches_iouv[0]#.astype(int)

            # TODO: adjust label location/size using raw matches indices
            matched_labels_indices = torch.full((labels.shape[0],), False)
            matched_labels_indices[matches[:, 0]] = True
            unmatched_labels_indices = torch.logical_not(matched_labels_indices)

            matched_detections_indices = torch.full((detections.shape[0],), False)
            matched_detections_indices[matches[:, 1]] = True
            unmatched_detections_indices = torch.full((detections.shape[0],), True)
            unmatched_detections_indices[indices[1]] = False

            matched_labels_boxes = labels[matches[:, 0], 1:]
            matched_confs = detections[matches[:, 1], 4].unsqueeze(1)
            matched_precisions = torch.tensor(np.interp(matched_confs, conf, pr_label_positive[0, :], left=EPSILON))

            matched_detections_boxes = detections[raw_matches[:, 1], :4]
            matched_detections_confs = detections[raw_matches[:, 1], 4].unsqueeze(1)
            matched_detections_precisions = torch.tensor(np.interp(matched_detections_confs, conf, precision[0, :], left=EPSILON))

            new_matched_labels_boxes = []
            if matches.shape[0] > 0:
                for i in matches[:, 0]:
                    current_detections_boxes = matched_detections_boxes[np.where(raw_matches[:, 0] == i)]
                    current_detections_precisions = matched_detections_precisions[np.where(raw_matches[:, 0] == i)]

                    current_labels_boxes = labels[int(i), 1:] * 0.9
                    current_detections_weighted_boxes = torch.sum(current_detections_boxes * current_detections_precisions, dim=0)

                    current_new_box = (current_labels_boxes + current_detections_weighted_boxes) / (0.9 + torch.sum(current_detections_precisions))

                    new_matched_labels_boxes.append(current_new_box)
                new_matched_labels_boxes = torch.stack(new_matched_labels_boxes)
            else:
                new_matched_labels_boxes = matched_labels_boxes
            matched_labels_cls = torch.full_like(matched_precisions, 0, dtype=torch.int64)
            matched_labels = torch.cat([new_matched_labels_boxes, matched_confs, matched_precisions, matched_labels_cls], axis=1)

            unmatched_labels_boxes = labels[unmatched_labels_indices, 1:]
            # TODO: change with the precision at position = 0.5?
            unmatched_confs = torch.full((unmatched_labels_boxes.shape[0], 1), UNMATCHED_CONF)
            unmatched_precisions = torch.full((unmatched_labels_boxes.shape[0], 1), P_T_N)
            unmatched_labels_cls = torch.full_like(unmatched_precisions, 1, dtype=torch.int64)
            unmatched_labels = torch.cat([unmatched_labels_boxes, unmatched_confs, unmatched_precisions, unmatched_labels_cls], axis=1)

            unmatched_detections_boxes = detections[unmatched_detections_indices, :4]
            unmatched_detections_confs = detections[unmatched_detections_indices, 4].unsqueeze(1)
            unmatched_detections_precisions = torch.tensor(np.interp(unmatched_detections_confs, conf, pr_bg_positive[0, :], left=EPSILON))
            unmatched_detections_cls = torch.full_like(unmatched_detections_precisions, 2, dtype=torch.int64)
            unmatched_detections = torch.cat([unmatched_detections_boxes, unmatched_detections_confs, unmatched_detections_precisions, unmatched_detections_cls], axis=1)
            unmatched_detections = unmatched_detections[unmatched_detections[:, -2] >= 0.3]

            new_labels = torch.cat([matched_labels, unmatched_labels, unmatched_detections], axis=0)
            nms_indices = nms(new_labels[:, :4], new_labels[:, 5], iou_threshold=0.3)
            new_labels = new_labels[nms_indices, :]
        # new_labels[:, 4] = torch.tensor(np.interp(new_labels[:, 4], conf, precision[0, :], left=EPSILON))
        if len(new_labels) > 0:
            new_labels = new_labels[new_labels[:, -2] >= 0.3]
        updated_labels.append({"file": fn_origin, "labels": new_labels, "img_path": gt_path.replace("labels", "images")})
    return updated_labels

def calculate_match(gt_path, predictions):
    niou = 10
    stats = []
    total_gt_size = 0
    total_gt = 0
    total_img = 0
    # predictions = read_predictions(predictions_path)

    for fn in os.listdir(gt_path):
        fn_origin = re.sub(".txt$", "", fn)

        detections = predictions.get(fn_origin, [])
        npr = detections.shape[0]

        if os.path.getsize(os.path.join(gt_path, fn)) > 0:
            gt_boxes = pd.read_csv(os.path.join(gt_path, fn), header=None, sep=" ")
            nl = gt_boxes.shape[0]
        else:
            nl = 0
        cls = torch.zeros((nl, 1))

        correct_bboxes = torch.zeros(npr, niou, dtype=torch.bool)

        if npr == 0:
            if nl > 0:
                stats.append((correct_bboxes, *torch.zeros((2, 0)), cls.squeeze(-1)))
            continue

        if nl > 0:
            gt_boxes = gt_boxes.iloc[:, 1:].values
            total_gt += gt_boxes.shape[0]
            total_gt_size += np.prod(gt_boxes[:, 2:], axis=1).sum() * 320 * 320

            # gt_boxes[:, :2] -= gt_boxes[:, 2:]/2
            gt_boxes = xywh2xyxy(gt_boxes) * 320

            labels = torch.tensor(np.append(np.zeros((gt_boxes.shape[0], 1)), gt_boxes, axis=1))

            correct_bboxes, _, _, _ = _process_batch(detections, labels)

        total_img += 1
        print(correct_bboxes.shape, correct_bboxes.sum(), cls.squeeze(-1).shape)
        stats.append((correct_bboxes, detections[:, 4], detections[:, 5], cls.squeeze(-1)))

    stats = [torch.cat(x, 0).cpu().numpy() for x in zip(*stats)]
    return stats, total_gt_size / total_gt, total_img

def compute_ap(recall, precision):
    """ Compute the average precision, given the recall and precision curves
    # Arguments
        recall:    The recall curve (list)
        precision: The precision curve (list)
    # Returns
        Average precision, precision curve, recall curve
    """

    # Append sentinel values to beginning and end
    mrec = np.concatenate(([0.0], recall, [1.0]))
    mpre = np.concatenate(([1.0], precision, [0.0]))

    # Compute the precision envelope
    mpre = np.flip(np.maximum.accumulate(np.flip(mpre)))

    # Integrate area under curve
    method = 'interp'  # methods: 'continuous', 'interp'
    if method == 'interp':
        x = np.linspace(0, 1, 101)  # 101-point interp (COCO)
        ap = np.trapz(np.interp(x, mrec, mpre), x)  # integrate
    else:  # 'continuous'
        i = np.where(mrec[1:] != mrec[:-1])[0]  # points where x-axis (recall) changes
        ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])  # area under curve

    return ap, mpre, mrec

def smooth(y, f=0.05):
    # Box filter of fraction f
    nf = round(len(y) * f * 2) // 2 + 1  # number of filter elements (must be odd)
    p = np.ones(nf // 2)  # ones padding
    yp = np.concatenate((p * y[0], y, p * y[-1]), 0)  # y padded
    return np.convolve(yp, np.ones(nf) / nf, mode='valid')  # y-smoothed

def conf_to_precision(tp, conf, pred_cls, target_cls, avg_gt_size, total_img, eps=1e-16, prefix=""):
    """ Compute the average precision, given the recall and precision curves.
    Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
    # Arguments
        tp:  True positives (nparray, nx1 or nx10).
        conf:  Objectness value from 0-1 (nparray).
        pred_cls:  Predicted object classes (nparray).
        target_cls:  True object classes (nparray).
        plot:  Plot precision-recall curve at mAP@0.5
        save_dir:  Plot save directory
    # Returns
        The average precision as computed in py-faster-rcnn.
    """

    # Sort by objectness
    i = np.argsort(-conf)
    tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]

    # Find unique classes
    unique_classes, nt = np.unique(target_cls, return_counts=True)
    nc = unique_classes.shape[0]  # number of classes, number of detections

    # Create Precision-Recall curve and compute AP for each class
    px, py = np.linspace(0, 1, 1000), []  # for plotting
    ap, p, r, fn = np.zeros((nc, tp.shape[1])), np.zeros((nc, 1000)), np.zeros((nc, 1000)), np.zeros((nc, 1000))
    pr_label_positive, pr_label_negative, pr_bg_positive = np.zeros((nc, 1000)), np.zeros((nc, 1000)), np.zeros((nc, 1000))
    for ci, c in enumerate(unique_classes):
        i = pred_cls == c
        n_l = nt[ci]  # number of labels
        # TODO: multiple by batch size
        n_b = int(320 * 320 / avg_gt_size) * total_img - n_l
        n_p = i.sum()  # number of predictions
        if n_p == 0 or n_l == 0:
            continue

        # Accumulate FPs and TPs
        fpc = (1 - tp[i]).cumsum(0)
        tpc = tp[i].cumsum(0)

        # Recall
        recall = tpc / (n_l + eps)  # recall curve
        # r[ci] = np.interp(-px, -conf[i], recall[:, 0])  # negative x, xp because xp decreases

        # Precision
        precision = tpc / (tpc + fpc)  # precision curve
        p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1)  # p at pr_score

        p_true = n_l / (n_l + n_b)
        p_label_true = 0.8
        p_label_false = 0.1
        p_positive_true = recall
        p_positive_false = fpc / (n_b + eps)

        p_true_label_positive = p_label_true * p_positive_true * p_true
        p_false_label_positive = p_label_false * p_positive_false * (1 - p_true)
        p_true_label_positive /= p_true_label_positive + p_false_label_positive
        pr_label_positive[ci] = np.interp(-px, -conf[i], p_true_label_positive[:, 0])

        p_true_label_negative = p_label_true * (1 - p_positive_true) * p_true
        p_false_label_negative = p_label_false * (1 - p_positive_false) * (1 - p_true)
        p_true_label_negative /= p_true_label_negative + p_false_label_negative
        pr_label_negative[ci] = np.interp(-px, -conf[i], p_true_label_negative[:, 0])

        p_true_bg_positive = (1 - p_label_true) * p_positive_true * p_true
        p_false_bg_positive = (1 - p_label_false) * p_positive_false * (1 - p_true)
        p_true_bg_positive /= p_true_bg_positive + p_false_bg_positive
        pr_bg_positive[ci] = np.interp(-px, -conf[i], p_true_bg_positive[:, 0])
    return pr_label_positive, pr_label_negative, pr_bg_positive, p, px

def write_labels(file_path, labels_array):
    if labels_array is not None:
        labels_lst = labels_array.tolist()
        with open(file_path, "w") as f:
            for l in labels_lst:
                f.write(f"{int(l[0])} {l[1]} {l[2]} {l[3]} {l[4]}\n")
    else:
        with open(file_path, "w") as _:
            pass

def write_weights(file_path, weights_array):
    if weights_array is not None:
        weights_lst = weights_array.tolist()
        with open(file_path, "w") as f:
            for l in weights_lst:
                f.write(f"{l[0]}\n")
    else:
        with open(file_path, "w") as _:
            pass

def write_next_dataset(dataset_path, dataset_name, new_labels, old_name):
    # create new dataset
    new_dataset_path = os.path.join(dataset_path, dataset_name)
    new_train_images_path = os.path.join(new_dataset_path, "train", "images")
    new_train_labels_path = os.path.join(new_dataset_path, "train", "labels")
    new_train_weights_path = os.path.join(new_dataset_path, "train", "weights")

    os.makedirs(new_train_images_path)
    os.makedirs(new_train_labels_path)
    os.makedirs(new_train_weights_path)
    for ele in new_labels:
        original_image_path = os.path.join(ele["img_path"], ele["file"]+".jpg")
        new_label_path = os.path.join(new_train_labels_path, ele["file"]+".txt")
        new_weight_path = os.path.join(new_train_weights_path, ele["file"] + ".txt")
        if len(ele["labels"]) > 0:
            labels_numpy = ele["labels"].cpu().detach().numpy()
            labels_array = xyxy2xywh(np.clip(labels_numpy[:, :4] / 320, a_min=0.0, a_max=None))
            labels_array = np.concatenate([labels_numpy[:, [6]].astype(np.int), labels_array], axis=1)
            weights_array = labels_numpy[:, [5]]
        else:
            labels_array = None
            weights_array = None

        shutil.copy2(original_image_path, new_train_images_path)
        write_labels(new_label_path, labels_array)
        write_weights(new_weight_path, weights_array)

    copy_tree(os.path.join(dataset_path, old_name, "valid"), os.path.join(dataset_path, dataset_name, "valid"))
    write_next_directory_yaml_file(os.path.join(dataset_path, "Pore-detection-14", "data.yaml"))

def confident_learning(root_path, dataset_path):
    run_idx = 1
    all_updated_labels = []

    for path in os.listdir(dataset_path):
        if not (os.path.isdir(os.path.join(dataset_path, path)) and re.search("-[0-4]$", path)):
            continue
        run_idx_str = "" if run_idx == 1 else str(run_idx)
        yaml_path = os.path.join(dataset_path, path, "data.yaml")
        gt_path = os.path.join(dataset_path, path, "valid", "labels")
        predictions_path = os.path.join(root_path, "runs", "detect", f"val{run_idx_str}", "predictions.json")
        test_img_path = os.path.join(dataset_path, path, "test", "images")
        test_lbl_path = os.path.join(dataset_path, path, "test", "labels")

        model = YOLO("yolov8s.pt")
        model.train(data=yaml_path, epochs=200, imgsz=320, lr0=1e-3)
        model.val(data=yaml_path, save_json=True)

        stats, avg_gt_size, total_img = calculate_match(gt_path, read_predictions(predictions_path))

        pr_label_positive, pr_label_negative, pr_bg_positive, precision, conf = conf_to_precision(*stats, avg_gt_size, total_img)
        run_idx += 1

        # test
        test_images = []
        test_image_fns = []
        for img_fn in os.listdir(test_img_path):
            img_path = os.path.join(test_img_path, img_fn)
            if os.path.isfile(img_path):
                test_images.append(Image.open(img_path))
                test_image_fns.append(img_fn)

        predictions = model.predict(source=test_images, conf=0.001, iou=0.5)
        updated_labels = update_labels(
            test_lbl_path,
            preprocess_predictions(test_image_fns, predictions),
            pr_label_positive,
            pr_label_negative,
            pr_bg_positive,
            precision,
            conf)
        all_updated_labels.extend(updated_labels)

    write_next_dataset(dataset_path, "Pore-detection-15", all_updated_labels, "Pore-detection-14")

if __name__ == "__main__":
    from ultralytics.yolo.utils.ops import non_max_suppression
    from torchvision.ops import nms

    label = xywh2xyxy(np.array([[0.8218125000000001, 0.85796875, 0.033625000000000016, 0.03409374999999992]])) * 320
    detection = xywh2xyxy(np.array([[0.8125, 0.8640625, 0.03750000000000009, 0.04062500000000002]])) * 320

    all_boxes = torch.cat([torch.tensor(label), torch.tensor(detection)], dim=0)
    scores = torch.tensor([0.1, 0.6], dtype=torch.float64)
    print(nms(all_boxes, scores, 0.5))


# encoding: utf-8

from __future__ import unicode_literals

import os
from decimal import Decimal

import yaml
from yaml.loader import Loader
from yaml.dumper import Dumper
from yaml.nodes import ScalarNode, SequenceNode
from yaml.constructor import ConstructorError
from marrow.util.compat import unicode
from marrow.util.bunch import Bunch

class Configuration(Loader):
    def construct_bunch(self, node):
        value = self.construct_mapping(node)
        data = Bunch()
        data.update(value)
        return data

    def construct_env(self, node):
        key = unicode(self.construct_scalar(node))
        return os.environ[key]

    def construct_relative(self, node):
        if not hasattr(self.stream, 'name'):
            raise ConstructorError(None, None, 'stream has no associated filename, cannot determine base directory',
                    node.start_mark)

        basedir = os.path.dirname(self.stream.name)
        path = str(self.construct_scalar(node))
        return os.path.join(basedir, path)

    def construct_decimal(self, node):
        value = unicode(self.construct_scalar(node))
        return Decimal(value)

class Dumper(Dumper):
    def represent_decimal(self, data):
        return self.represent_scalar('!decimal', unicode(data))

def load(src, Loader=Configuration):
    return yaml.load(src, Loader=Loader)

def dump(data, stream=None, dumper=Dumper, **kwds):
    kwds.setdefault('default_flow_style', False)
    kwds.setdefault('indent', 4)
    return yaml.dump(data, stream, dumper, **kwds)

Configuration.add_constructor('tag:yaml.org,2002:map', Configuration.construct_bunch)
Configuration.add_constructor('!env', Configuration.construct_env)
Configuration.add_constructor('!relative', Configuration.construct_relative)
Configuration.add_constructor('!decimal', Configuration.construct_decimal)
Dumper.add_representer(Decimal, Dumper.represent_decimal)


# Code snippet to use with pipedream.com to validate an incoming
# request (a webhook) from UP42 for jub statuses updates. It acts
# based on the job statuses we are interested in.
# All the information is available and can be passed on to other steps
# in a workflow.

# pipedream support modules.
from pipedream.script_helpers import (steps, export)

import os
import re
# Modules needed for authenticating the request (webhook call) done by
# UP42.
import base64
from hmac import digest, compare_digest
from hashlib import sha256
from time import time

# Get the response headers.
response_headers = steps["trigger"]["event"]["headers"]

# Get the signature input.
sig_input = steps["trigger"]["event"]["headers"]["signature-input"]

# Extract signature timestamp given as UNIX time.
sig_ts_re = re.compile("created=(?P<sig_ts>\d{10,20})")
sig_ts_val = int(sig_ts_re.search(sig_input).groupdict()["sig_ts"])

# Get current date as UNIX time.
current_ts = int(time())

# Reduce the possibility of replay attacks by dropping any incoming request that
# is older than 5 minutes (300 seconds).
assert sig_ts_val <= current_ts and abs(current_ts - sig_ts_val) < 300, f"Request time skew is too large."

# Get the signature value given in the response headers.
sig_header_value = steps["trigger"]["event"]["headers"]["signature"]
sig_header_value = bytes(sig_header_value.split("=", 1)[1], "UTF-8")

# Extract how the signature is generated.
# 1. Look for the up42-sig element in the header.
field_re = re.compile("up42-sig=\((?P<sig_list>.*)\);.*")
sig_val = field_re.search(sig_input).groupdict()["sig_list"]
# 2. Extract the list of signature input components.
list_re = re.compile("[^\"\s]+")
sig_inputs = re.findall(list_re, sig_val)

# Validate the received request by validating the signature.
# 1. First get each signature component concatenated in a single
# string.
sig_str = "".join(response_headers[i] for i in sig_inputs)
# 2. Compute the HMAC for this string.
secret = os.environ["up42_webhook_secret"] # get the secret
# Compute the signature based on the response headers.
computed_sig = base64.b64encode(digest(bytes(secret,  "UTF-8"),
                                       bytes(sig_str, "UTF-8"),
                                       sha256))

# Raise an Assertion error if the signature is invalid. Exits the
# workflow. I.e., no existing subsequent steps will be executed.
assert compare_digest(sig_header_value, computed_sig), "Cannot authenticate incoming HTTP request."

# Get the current event type.
current_event_type = steps["trigger"]["event"]["body"]["event"]

# Filter on the event type. We are only interested in events of type order.status.
assert current_event_type == "order.status", f"Event type mismatch: {current_event_type}."