# Cura PostProcessingPlugin
# Author:   FbeauKmi    based on DisplayProgressOnLCD by Mathias Lyngklip Kjeldgaard, Alexander Gee, Kimmo Toivanen
# Date:     February 20, 2022
# Modified: February 20, 2022

# Description:  This plugin add custom Snapshot command for octolapse .

from ..Script import Script

import re
import datetime

class AddSnapshot(Script):

    def __init__(self):
        super().__init__()

    def getSettingDataString(self):
        return """{
            "name": "OL-MR Timelapse",
            "description": "Originally built for Octolapse adapted to moonraker-timelapse",
            "key": "AddSnapshot",
            "metadata": {},
            "version": 2,
            "settings":
            {
                "a_target": {
                    "label": "Target timelapse",
                    "description": "Moonraker-timelapse or Octolapse ?",
                    "type": "enum",
                    "options": {
                        "mr": "Moonraker-timelapse",
                        "ol": "Octolapse"
                    },
                    "default_value": "mr"
                },
                "a_trigger": {
                    "label": "Trigger",
                    "description": "Layer or time based timelapse",
                    "type": "enum",
                    "options": {
                        "layer": "Layer",
                        "time": "Time",
                        "fixed": "Nb of snapshot"
                    },
                    "default_value": "layer"
                },
                "b_update_lfrequency":
                {
                    "label": "Layer Interval (experimental)",
                    "description": "Update layer Interval eg: 3 for 1 snapshot every 3 layer, 0.5 for 1 snapshot every 0.5 layers (allowed values 0.1, 0.2, 0.25, 0.35, 0.5)",
                    "unit": "",
                    "type": "float",
                    "min_value": "0.1",
                    "max_value": "10",
                    "default_value":"1",
                    "enabled" : "a_trigger=='layer'"
                },
                "b_update_tfrequency":
                {
                    "label": "Update frequency snapshot",
                    "description": "take snapshot every X seconds",
                    "unit": "s",
                    "type": "int",
                    "min_value": "1",
                    "default_value":"30",
                    "enabled" : "a_trigger=='time'"
                },
                "b_update_nfrequency":
                {
                    "label": "Number of snapshots",
                    "description": "take X snapshots",
                    "unit": "",
                    "type": "int",
                    "min_value": "25",
                    "default_value":"120",
                    "enabled" : "a_trigger=='fixed'"
                },
                "custom_gcode_octolapse":
                {
                    "label": "Custom G-code for Octolapse",
                    "description": "G-code to add to take snapshot.",
                    "type": "str",
                    "default_value": "SNAP",
                    "enabled": "a_target=='ol'"
                },
                "custom_gcode_moonraker":
                {
                    "label": "Custom G-code for Moonraker-timelapse",
                    "description": "G-code to add to take snapshot.",
                    "type": "str",
                    "default_value": "TIMELAPSE_TAKE_FRAME",
                    "enabled": "a_target=='mr'"
                }
            }
        }"""

    # Get the value from a line as a float.
    # Example line ;LAYER_COUNT:120 ;TIME_ELAPSED:1234.6789 or ;TIME:1337
    def getLValue(self, line):
        list_split = re.split(":", line)  # Split at ":" so we can get the numerical value
        return float(list_split[1])  # Convert the numerical portion to a float

    def execute(self, data):
        trigger =  str(self.getSettingValueByKey("a_trigger"))
        target =  str(self.getSettingValueByKey("a_target"))
        layer_number = int(self.getSettingValueByKey("b_update_nfrequency")) - 1
        layer_frequency = float(self.getSettingValueByKey("b_update_lfrequency"))

        output_frequency = int(self.getSettingValueByKey("b_update_tfrequency"))
        output_gcode = str(self.getSettingValueByKey("custom_gcode_octolapse")) if target=="ol" else  str(self.getSettingValueByKey("custom_gcode_moonraker"))

        line_set = {}

        first_snapshot = True
        total_time = -1
        previous_layer_end_time = 1
        shot=0
        hyperlapse=0

        for layer in data:
            layer_index = data.index(layer)
            lines = layer.split("\n")

            # escape if HYPERLAPSE
            if hyperlapse == 1:
                  break

            for line in lines:

                # escape if HYPERLAPSE
                if hyperlapse == 1:
                  break
                if line.startswith(";TYPE:") and first_snapshot:
                    # Take the first snapshot when encounter ;TYPE: the first time (before skirt, wall, mesh,)
                    fline_index = lines.index(line) + 4
                    # If time base timelapse use HYPERLASPE function of Moonraker
                    if target=="mr" and (trigger=="time" or trigger=="fixed"):
                        lines.insert(fline_index, "HYPERLAPSE ACTION=START CYCLE=" + str(output_frequency))
                        hyperlapse=1
                        break
                    else:
                        lines.insert(fline_index, output_gcode)

                    first_snapshot = False

                elif line.startswith(";LAYER_COUNT:"):
                    # This line repersent the total number of layers
                    total_layer = self.getLValue(line)

                elif (line.startswith(";TIME:") or line.startswith(";PRINT.TIME:")) and total_time == -1:
                    # This line represents the total time required to print the gcode
                    total_time = int(self.getLValue(line))
                    if trigger=="fixed" and layer_number != -1:
                        output_frequency = int(total_time / layer_number)

                elif line.startswith(";TIME_ELAPSED:"):
                    # We've found one of the time elapsed values which are added at the end of layers
                    # If we have seen this line before then skip processing it. We can see lines multiple times because we are adding
                    # intermediate percentages before the line being processed. This can cause the current line to shift back and be
                    # encountered more than once
                    if line in line_set:
                        continue
                    line_set[line] = True

                    # If total_time was not already found then noop
                    if total_time == -1:
                        continue

                    current_time = int(self.getLValue(line))
                    line_index = lines.index(line)

                    #Layer based snapshot
                    if trigger == "layer":
                        shot = shot + 1
                        # Find next layer to trigger
                        if shot >= int(layer_frequency):
                            # take snapshot
                            lines.insert(line_index, output_gcode)
                            shot=0

                            if layer_frequency < 1:

                                nb_shot = int(1.0 / layer_frequency) - 1
                                lines_added=1

                                if nb_shot > 0:
                                    for n in range(1,nb_shot + 1):
                                        # Line to add the output
                                        time_line_index = int(float(line_index) * float(n) / (nb_shot+1) ) + lines_added
                                        # Take snapshot
                                        lines.insert(time_line_index, output_gcode)
                                        # Next line will be again lower
                                        lines_added = lines_added + 1

                    else:
                        # Here we calculate remaining time and how many outputs are expected for the layer
                        layer_time_delta = int(current_time - previous_layer_end_time)

                        # If this layer represents less than 1 step then we don't need to emit anything, continue to the next layer
                        if layer_time_delta > output_frequency:
                            # Grab the index of the current line and figure out how many lines represent one second
                            step = line_index / layer_time_delta
                            # Move new lines further as we add new lines above it
                            lines_added = 1
                            # Run through layer in seconds
                            for seconds in range(1, layer_time_delta + 1):
                                # Time from start to decide when to update
                                line_time = int(previous_layer_end_time + seconds)
                                # Output every X seconds and after last layer

                                if line_time % output_frequency == 0 or line_time == total_time:
                                    # Line to add the output
                                    time_line_index = int((seconds * step) + lines_added)

                                    # Take snapshot
                                    lines.insert(time_line_index, output_gcode)

                                    # Next line will be again lower
                                    lines_added = lines_added + 1

                            previous_layer_end_time = int(current_time)

                    line_index = lines.index(line)
                    if (current_time >= total_time):
                        # Take last snapshot
                        lines.insert(line_index, output_gcode)

            # Join up the lines for this layer again and store them in the data array
            data[layer_index] = "\n".join(lines)
        return data


import numpy as np
import cv2

#Little program demonstrating general OpenCV functionality using haar cascade

cap = cv2.VideoCapture(0)

fgbg = cv2.createBackgroundSubtractorMOG2(1000, 75, True)

# Define the codec and create VideoWriter object for recording video
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('faceTracker.avi',fourcc, 20.0, (640,480))

count = 0

while(cap.isOpened()):
    ret, frame = cap.read()

    if ret==True:

        #read in the cascade
        body_cascade = cv2.CascadeClassifier('C:\EhEye\XMLFiles\haarcascade_frontalface.xml')

        #Smooth to get rid of false positives
        gray = cv2.GaussianBlur(frame, (15, 15), 0)

        #Convert the image to grayscale.
        cv.CvtColor(color_image, grey_image, cv.CV_RGB2GRAY)

        # scale input image for faster processing
        cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

        #grab all instances of a face
        heads = body_cascade.detectMultiScale(frame, 1.3, 5)

        #draw a recrangle around the face
        for (x,y,w,h) in heads:
            cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)

        #show the frame
        cv2.imshow('frame',frame)

        #save the frame to folder as video
        out.write(frame)

        #press q to quit
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
out.release()
cap.release()
cv2.destroyAllWindows()


#*************************************************************************
# Copyright (C) 2018 Kaan Kara - Systems Group, ETH Zurich

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#*************************************************************************

import socket
import cv2
import threading
import struct
import numpy as np
import sys

sys.path.append("/home/xilinx/jupyter_notebooks/dac_2019_contest/common")
#################### import all libraries and initializations ############
from pynq import Xlnk
from pynq import Overlay
from pynq.mmio import MMIO
from PIL import Image

class FPGA_Connect_Object:
    def __init__(self, addr_port_client=("192.168.1.100", 3000)):
        print('FPGA_Connect_Object init')
        self.resolution = [640, 360]
        self.client_port = addr_port_client

        team_name = 'SystemsETHZ'
        # agent = Agent(team_name)

        interval_time = 0
        xlnk = Xlnk()
        xlnk.xlnk_reset()

        ###########################variable initializing######################
        OVERLAY_PATH = '/home/xilinx/jupyter_notebooks/dac_2019_contest/common/' + team_name + '/ultra96_v04.bit'
        WEIGHTS_FILE_NAME = '/home/xilinx/jupyter_notebooks/dac_2019_contest/common/' + team_name + '/weights_file_v04_demo.txt'

        ###########################change board settings######################

        ###########################download      overlay######################
        overlay = Overlay(OVERLAY_PATH)
        self.dma = overlay.axi_dma_0
        self.nn_ctrl = MMIO(0xA0010000, length=1024)
        ###########################download      weights######################
        self.MINIBATCH_SIZE = 1
        self.height = 176
        self.width = 320
        pixel_bits = 24
        pixels_per_line = 384/pixel_bits
        self.num_lines = int((self.height*self.width)/pixels_per_line)

        self.in_buffer = xlnk.cma_array(shape=(self.MINIBATCH_SIZE*self.num_lines, 64), dtype=np.uint8)
        fire1_num_out_lines = (self.height/4)*(self.width/4)*self.MINIBATCH_SIZE
        self.fire1_out_buffer = xlnk.cma_array(shape=(int(16*fire1_num_out_lines),), dtype=np.uint32)
        fire2_num_out_lines = (self.height/8)*(self.width/8)*self.MINIBATCH_SIZE
        self.fire2_out_buffer = xlnk.cma_array(shape=(int(16*fire2_num_out_lines),), dtype=np.uint32)
        fire3_num_out_lines = (self.height/16)*(self.width/16)*self.MINIBATCH_SIZE
        self.fire3_out_buffer = xlnk.cma_array(shape=(int(16*fire3_num_out_lines),), dtype=np.uint32)
        self.fire4_out_buffer = xlnk.cma_array(shape=(int(16*fire3_num_out_lines),), dtype=np.uint32)
        self.fire5_out_buffer = xlnk.cma_array(shape=(int(16*fire3_num_out_lines),), dtype=np.uint32)
        final_num_lines = int((self.height/16)*(self.width/16))
        self.bndboxes = [xlnk.cma_array(shape=(self.MINIBATCH_SIZE,final_num_lines,16), dtype=np.int32),
                        xlnk.cma_array(shape=(self.MINIBATCH_SIZE,final_num_lines,16), dtype=np.int32),
                        xlnk.cma_array(shape=(self.MINIBATCH_SIZE,final_num_lines,16), dtype=np.int32),
                        xlnk.cma_array(shape=(self.MINIBATCH_SIZE,final_num_lines,16), dtype=np.int32)]
        self.obj_array = np.zeros((self.MINIBATCH_SIZE,final_num_lines))

        NUM_LAYERS = 3+4*4
        weights_file = open(WEIGHTS_FILE_NAME, "r")
        layer = 0
        total_iterations = np.zeros(NUM_LAYERS)
        for line in weights_file:
            if "layer" in line:
                temp = line.split(": ")
                layer = int(temp[1])
            if "total_iterations" in line:
                temp = line.split(": ")
                total_iterations[layer] = int(temp[1])
        weights_file.close()

        weightfactors_length = np.zeros(NUM_LAYERS)
        self.weightsfactors = []
        for i in range(0, NUM_LAYERS):
            weightfactors_length[i] = int(total_iterations[i])
            self.weightsfactors.append( xlnk.cma_array(shape=(int(16*weightfactors_length[i]),), dtype=np.uint32) )
        self.obj_factors = np.zeros(4)
        self.box_factors = np.zeros(4)

        index = 0
        weights_file = open(WEIGHTS_FILE_NAME, "r")
        for line in weights_file:
            if "layer" in line:
                temp = line.split(": ")
                layer = int(temp[1])
                index = 0
            elif "total_iterations" not in line:
                if "obj_factor" in line:
                    temp = line.split(' ')
                    self.obj_factors[int(temp[1])] = int(temp[2])
                elif "box_factor" in line:
                    temp = line.split(' ')
                    self.box_factors[int(temp[1])] = int(temp[2])
                else:
                    no0x = line.split('0x')[-1]
                    base = 1
                    while base < len(no0x):
                        part = no0x[-1*(base+8):-1*base]
                        self.weightsfactors[layer][index*16 + int(base/8)] = int(part, 16)
                        base += 8
                    index += 1

    ## Define transfer functions
    def weightsfactors_transfer(self, weightsfactors):
        self.nn_ctrl.write(0x40, 13)
        self.nn_ctrl.write(0x48, 0)
        self.nn_ctrl.write(0x0, 0) # Reset
        self.nn_ctrl.write(0x0, 1) # Deassert reset
        self.dma.sendchannel.transfer(weightsfactors)
        self.dma.sendchannel.wait()

    def fire(self, inbuffer, outbuffer,
             squeeze_din_w, squeeze_din_h,
             expand_din_w, expand_din_h,
             expand_din_w_afterpool, expand_din_h_afterpool,
             whichfire):
        self.nn_ctrl.write(0x0, 0) # Reset
        self.nn_ctrl.write(0x10, int(squeeze_din_w))
        self.nn_ctrl.write(0x18, int(squeeze_din_h))
        self.nn_ctrl.write(0x20, int(expand_din_w))
        self.nn_ctrl.write(0x28, int(expand_din_h))
        self.nn_ctrl.write(0x30, int(expand_din_w_afterpool))
        self.nn_ctrl.write(0x38, int(expand_din_h_afterpool))
        self.nn_ctrl.write(0x40, whichfire)
        self.nn_ctrl.write(0x48, self.MINIBATCH_SIZE) # set numReps
        self.nn_ctrl.write(0x0, 1) # Deassert reset
        self.dma.recvchannel.transfer(outbuffer)
        self.dma.sendchannel.transfer(inbuffer)

        ###########################end      initializing######################
    def Socket_Connect_Client(self):
        print('FPGA_Connect_Object Socket_Connect')
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.client.connect(self.client_port)
        print("As a client, FPGA receives imgs from %s:%d" % (self.client_port[0], self.client_port[1]))

    def ProcessImg(self):
        print('FPGA_Connect_Object begins process imgs')
        self.client.send(struct.pack("hh", self.resolution[0], self.resolution[1]))

        print("debug_FPGA 1")

        while (1):
            recv_data = self.client.recv(16)
            # print("size ", len(recv_data))
            info = struct.unpack("qq", recv_data)

            buf_size = info[0]
            ## receive imgs
            if buf_size:
                try:
                    self.buf = b""
                    temp_buf = self.buf
                    while (buf_size):
                        temp_buf = self.client.recv(buf_size)
                        buf_size -= len(temp_buf)
                        self.buf += temp_buf
                except:
                    pass;

            data = np.fromstring(self.buf, dtype='uint8')

            ## get the imgs and process it
            image = cv2.imdecode(data, 1)

            ###########################call PL to do inference######################
            #detected bounding box coordinates in x1, y1, x2, y2
            result = np.zeros((1, 17))

            image = cv2.resize(image, (self.width, self.height), interpolation=cv2.INTER_NEAREST)
            self.in_buffer[0:self.num_lines,0:48] = np.reshape(image, (self.num_lines, 48))

            self.weightsfactors_transfer(self.weightsfactors[0])
            self.fire(self.in_buffer, self.fire1_out_buffer,\
                self.width/2, self.height/2, self.width/2, self.height/2, self.width/4, self.height/4, 1)
            self.dma.recvchannel.wait()

            self.weightsfactors_transfer(self.weightsfactors[1])
            self.fire(self.fire1_out_buffer, self.fire2_out_buffer,\
                self.width/4, self.height/4, self.width/4, self.height/4, self.width/8, self.height/8, 2)
            self.dma.recvchannel.wait()

            self.weightsfactors_transfer(self.weightsfactors[2])
            self.fire(self.fire2_out_buffer, self.fire3_out_buffer,\
                self.width/8, self.height/8, self.width/8, self.height/8, self.width/16, self.height/16, 3)
            self.dma.recvchannel.wait()

            for t in range(0, 4):
                self.weightsfactors_transfer(self.weightsfactors[3 + t*4])
                self.fire(self.fire3_out_buffer, self.fire4_out_buffer,\
                    self.width/16, self.height/16, self.width/16, self.height/16, self.width/16, self.height/16, 4)
                self.dma.recvchannel.wait()

                self.weightsfactors_transfer(self.weightsfactors[4 + t*4])
                self.fire(self.fire4_out_buffer, self.fire5_out_buffer,
                    self.width/16, self.height/16, self.width/16, self.height/16, self.width/16, self.height/16, 5)
                self.dma.recvchannel.wait()

                self.weightsfactors_transfer(self.weightsfactors[5 + t*4])
                self.fire(self.fire5_out_buffer, self.fire4_out_buffer,\
                    self.width/16, self.height/16, self.width/16, self.height/16, self.width/16, self.height/16, 6)
                self.dma.recvchannel.wait()

                self.weightsfactors_transfer(self.weightsfactors[6 + t*4])
                self.fire(self.fire4_out_buffer, self.bndboxes[t],\
                    self.width/16, self.height/16, self.width/16, self.height/16, self.width/16, self.height/16, 7)
                self.dma.recvchannel.wait()

                temp_obj = np.multiply(np.divide(self.bndboxes[t][:,:,4],float((1<<16))), float(self.obj_factors[t]))
                if t == 0:
                    self.obj_array = temp_obj
                else:
                    self.obj_array = np.add(self.obj_array, temp_obj)

            grid_cell = np.argmax(self.obj_array, axis=1)
            result[0,16] = grid_cell[0]
            for t in range(0,4):
                result[0, t*4:(t+1)*4] = self.bndboxes[t][0,grid_cell[0],0:4]

            float_objdetect = result[0,16].astype('float')
            float_bndboxes1 = np.multiply(np.divide(result[0,0:4].astype('float'), float((1<<16))), float(self.box_factors[0]))
            float_bndboxes2 = np.multiply(np.divide(result[0,4:8].astype('float'), float((1<<16))), float(self.box_factors[1]))
            float_bndboxes3 = np.multiply(np.divide(result[0,8:12].astype('float'), float((1<<16))), float(self.box_factors[2]))
            float_bndboxes4 = np.multiply(np.divide(result[0,12:16].astype('float'), float((1<<16))), float(self.box_factors[3]))
            float_bndboxes = float_bndboxes1+float_bndboxes2+float_bndboxes3+float_bndboxes4
            float_bndboxes = np.divide( float_bndboxes, 4.0*float((1 << 22)) )

            obj_h = int(float_objdetect/(self.width/16))
            obj_w = int(float_objdetect%(self.width/16))

            x1 = int((float_bndboxes[0] + obj_w*16) *(640/self.width))
            y1 = int((float_bndboxes[1] + obj_h*16) *(360/self.height))
            x2 = int((float_bndboxes[2] + obj_w*16) *(640/self.width))
            y2 = int((float_bndboxes[3] + obj_h*16) *(360/self.height))
            ###########################end        PL       call######################

            self.client.send(struct.pack("hhhh", abs(x1), abs(y1), abs(x2), abs(y2)))

    def ProcessInThread(self):
        print('FPGA_Connect_Object   Get_Data')
        showThread = threading.Thread(target=self.ProcessImg)
        showThread.start()

if __name__ == '__main__':
    FPGA = FPGA_Connect_Object(("192.168.3.109", 3000))
    print("connect object created")
    FPGA.Socket_Connect_Client()
    FPGA.ProcessInThread()


from sys import path
import pyautogui as p
import time

path.append(r'..\..\_comum')
from comum_comum import _indice, _escreve_relatorio_csv, _time_execution, _open_lista_dados, _where_to_start

def esperar(imagen):
    while not p.locateOnScreen(imagen, confidence=0.9):
        time.sleep(1)

def clicar(imagen, tempo=0.1):
    try:
        p.click(p.locateCenterOnScreen(imagen, confidence=0.9))
        time.sleep(tempo)
        return True
    except:
        return False

def verificacoes(empresa):
    cod, cnpj, nome, arquivo = empresa
    if p.locateOnScreen(r'imagens\JaExiste.png'):
        p.press('enter')
        time.sleep(1)
        p.press(['esc', 'esc'])
        _escreve_relatorio_csv(';'.join([cod, cnpj, nome, 'Arquivo já importado']), 'Importação DIRF')
        print('>>> Arquivo já importado <<<')
        return False
    if p.locateOnScreen(r'imagens\JaConsta.png'):
        p.hotkey('ctrl', 'n')
        time.sleep(1)
        p.press(['esc', 'esc'])
        _escreve_relatorio_csv(';'.join([cod, cnpj, nome, 'Arquivo já consta na base de dados']), 'Importação DIRF')
        print('>>> Arquivo já consta na base de dados <<<')
        return False
    if p.locateOnScreen(r'imagens\Erros.png'):
        p.hotkey('alt', 'n')
        time.sleep(2)
        if p.locateOnScreen(r'imagens\Relatorio.png'):
            clicar(r'imagens\SalvarRelatorio.png')
            esperar(r'imagens\Salvar.png')
            time.sleep(0.2)
            p.write(cnpj + ' - ' + cod + '.pdf')
            time.sleep(0.2)
            p.press('enter')
            time.sleep(1)
            if p.locateOnScreen(r'imagens\Sobrescrever.png'):
                p.press('enter')
            clicar(r'imagens\FecharRelatorio.png')
        _escreve_relatorio_csv(';'.join([cod, cnpj, nome, 'Erros ou avisos']), 'Importação DIRF')
        print('** Erros ou avisos **')
        return False
    return True

@_time_execution
def importar_dirf():
    # abrir a planilha de dados
    empresas = _open_lista_dados()
    if not empresas:
        return False

    # configurar um indice para a planilha de dados
    index = _where_to_start(tuple(i[0] for i in empresas))
    if index is None:
        return False

    p.hotkey('win', 'm')
    time.sleep(0.5)
    p.getWindowsWithTitle('Dirf')[0].maximize()

    total_empresas = empresas[index:]
    for count, empresa in enumerate(empresas[index:], start=1):
        cod, cnpj, nome, arquivo = empresa

        _indice(count, total_empresas, empresa)

        clicar(r'imagens\TelaInicial.png')

        # Abrir tela para selecionar o arquvio
        while p.locateOnScreen(r'imagens\TelaInicial.png'):
            p.hotkey('ctrl', 'i')
            time.sleep(3)
        while not p.locateOnScreen(r'imagens\AbrirDeclaracao.png'):
            time.sleep(1)
            p.click(p.locateCenterOnScreen(r'imagens\Lupa.png'))
        p.write(arquivo)
        time.sleep(0.5)
        p.press('enter')
        time.sleep(1)

        if not verificacoes(empresa):
            continue

        # Esperar importar o arquivo
        esperar(r'imagens\Avancar.png')
        p.hotkey('alt', 'a')
        esperar(r'imagens\Resumo.png')

        if not verificacoes(empresa):
            continue

        if not p.locateOnScreen(r'imagens\Concluir.png'):
            ''
        p.hotkey('alt', 'n')
        _escreve_relatorio_csv(';'.join([cod, cnpj, nome, 'Arquivo importado']), 'Importação DIRF')
        print('>>> Arquivo importado <<<')

if __name__ == '__main__':
    try:
        importar_dirf()
    except:
        p.alert(title='Script incrível', text='ERRO')


import pandas as pd
from astropy.cosmology import default_cosmology
from tsfresh.feature_extraction import extract_features
from tqdm import tqdm

import common
import config
import gc
from .common import *

cosmo = default_cosmology.get()

@feature('f1000')
def f1000_salt2_normalized_chisq(input: Input, **kw):
    salt2 = common.load_feature("f500")
    meta_ = pd.merge(input.meta, salt2, on='object_id', how='left')

    count = input.lc.groupby('object_id')['mjd'].count().reset_index()
    count.columns = ['object_id', 'n_observed']

    meta_ = pd.merge(meta_, count, on='object_id', how='left')
    meta_['sn_salt2_chisq_norm'] = meta_['sn_salt2_chisq'] / meta_['n_observed']

    return meta_[['object_id', 'sn_salt2_chisq_norm']]

@feature('f1001')
def f1001_detected_to_risetime_ratio(input: Input, **kw):
    delta = common.load_feature("f050")
    risetime = common.load_feature("f053")

    meta_ = pd.merge(input.meta, delta, on='object_id', how='left')
    meta_ = pd.merge(meta_, risetime, on='object_id', how='left')

    features = []
    for i in range(6):
        f = 'delta(first(detected), max(flux))_ch{}_to_delta_ratio'.format(i)
        meta_[f] = meta_['delta(first(detected), max(flux))_ch{}'.format(i)] / meta_['delta']
        features.append(f)

    return meta_[['object_id']+features]

@feature('f1002')
def f1002_detected_to_falltime_ratio(input: Input, **kw):
    delta = common.load_feature("f050")
    falltime = common.load_feature("f053")

    meta_ = pd.merge(input.meta, delta, on='object_id', how='left')
    meta_ = pd.merge(meta_, falltime, on='object_id', how='left')

    features = []
    for i in range(6):
        f = 'delta(max(flux), last(detected))_ch{}_to_delta_ratio'.format(i)
        meta_[f] = meta_['delta(max(flux), last(detected))_ch{}'.format(i)] / meta_['delta']
        features.append(f)

    return meta_[['object_id']+features]

def z2pc(z):
    return cosmo.luminosity_distance(z).value

@feature('f1003')
def f1003_luminosity_by_estimated_redshift(input: Input, **kw):
    aggregate = common.load_feature("f000")
    redshift = common.load_feature("f600")
    meta_ = pd.merge(input.meta, redshift, on='object_id', how='left')
    meta_ = pd.merge(meta_, aggregate, on='object_id', how='left')

    meta_['Mpc'] = meta_['hostgal_z_predicted'].apply(z2pc)
    meta_['Gpc'] = meta_['Mpc'] / 1000.0

    features = []
    for i in range(6):
        ch = i
        meta_['flux_diff_ch{}'.format(ch)] = meta_['max(flux)_ch{}'.format(ch)] - meta_['min(flux)_ch{}'.format(ch)]
        meta_['luminosity_diff_ch{}_estimated'.format(ch)] = meta_['flux_diff_ch{}'.format(ch)] * meta_['Gpc'] * meta_['Gpc']
        features.append('luminosity_diff_ch{}_estimated'.format(ch))

    features_renamed = ['luminosity_est_diff_ch{}'.format(i) for i in range(6)]

    rename = {features[i]: features_renamed[i] for i in range(6)}

    meta_.rename(columns=rename, inplace=True)

    return meta_[['object_id']+features_renamed]

def extract_features_postproc(df: pd.DataFrame):
    df.sort_values(by='id', inplace=True)
    df.rename(columns={'id': 'object_id'}, inplace=True)
    df.reset_index(drop=True, inplace=True)
    return df

@feature('f1004')
def f1004_tsfresh_flux(input: Input, **kw):
    fcp = {
        'longest_strike_above_mean': None,
        'longest_strike_below_mean': None,
        'mean_change': None,
        'mean_abs_change': None,
        'length': None,
    }

    dfs = []
    for i in tqdm(range(30)):
        lc = common.load_partial_lightcurve(i)
        partial = extract_features(lc,
                                   column_id='object_id',
                                   column_value='flux',
                                   default_fc_parameters=fcp, n_jobs=config.TSFRESH_N_JOBS)
        dfs.append(partial.reset_index())
        gc.collect()

    return extract_features_postproc(pd.concat(dfs))

@feature('f1005')
def f1005_tsfresh_flux_per_passband(input: Input, **kw):
    fcp = {
        'fft_coefficient': [
                {'coeff': 0, 'attr': 'abs'},
                {'coeff': 1, 'attr': 'abs'}
            ],
        'kurtosis': None,
        'skewness': None,
    }

    dfs = []
    for i in tqdm(range(30)):
        lc = common.load_partial_lightcurve(i)
        partial = extract_features(lc,
                                   column_id='object_id',
                                   column_sort='mjd',
                                   column_kind='passband',
                                   column_value='flux',
                                   default_fc_parameters=fcp, n_jobs=config.TSFRESH_N_JOBS)
        dfs.append(partial.reset_index())
        gc.collect()

    return extract_features_postproc(pd.concat(dfs))

@feature('f1006')
def f1006_tsfresh_mjd(input: Input, **kw):
    fcp = {
        'mean_change': None,
        'mean_abs_change': None,
    }
    lc = input.lc
    df_det = lc[lc['detected'] == 1].copy()

    dfs = []
    for i in tqdm(range(30)):
        lc = common.load_partial_lightcurve(i)
        partial = extract_features(df_det,
                                   column_id='object_id',
                                   column_value='mjd',
                                   default_fc_parameters=fcp, n_jobs=config.TSFRESH_N_JOBS)
        dfs.append(partial.reset_index())
        gc.collect()

    return extract_features_postproc(pd.concat(dfs))

def filter_lc_by_snr(lc: pd.DataFrame, snr: int):
    lc['SNR'] = lc['flux'] / lc['flux_err']
    lc['absSNR'] = np.abs(lc['SNR'])
    lc['flag'] = (lc['absSNR'] > snr).astype(np.uint8)
    return lc[lc['flag'] == 1]

@feature('f1080')
def f1080_snr3_minmax_diff(input: Input, **kw):
    lc_detected = filter_lc_by_snr(input.lc, snr=3)
    mjddelta = lc_detected.groupby('object_id').agg({'mjd': ['min', 'max']})
    mjddelta['delta'] = mjddelta[mjddelta.columns[1]] - mjddelta[mjddelta.columns[0]]
    mjddelta = mjddelta['delta'].reset_index(drop=False)

    return mjddelta

@feature('f1081')
def f1081_first_is_detected(input: Input, **kw):
    first = input.lc[['object_id', 'mjd', 'detected']].groupby('object_id').first()
    first.columns = ['mjd', 'detected_on_first_observation']

    return first.drop('mjd', axis=1).reset_index()

@feature('f1082')
def f1082_last_is_detected(input: Input, **kw):
    last = input.lc[['object_id', 'mjd', 'detected']].groupby('object_id').last()
    last.columns = ['mjd', 'detected_on_last_observation']

    return last.drop('mjd', axis=1).reset_index()

@feature('f1083')
def f1083_max_flux_within_snr3(input: Input, **kw):
    lc_detected = filter_lc_by_snr(input.lc, snr=3)
    return aggregate_by_id_passbands(lc_detected, 'flux', ['max'], prefix='snr3_')

@feature('f1084')
def f1084_min_flux_within_snr3(input: Input, **kw):
    lc_detected = filter_lc_by_snr(input.lc, snr=3)
    return aggregate_by_id_passbands(lc_detected, 'flux', ['min'], prefix='snr3_')

@feature('f1085')
def f1085_luminosity_diff_within_snr3(input: Input, **kw):
    redshift = common.load_feature('f600')
    max_flux = common.load_feature('f1083')
    min_flux = common.load_feature('f1084')

    meta = pd.merge(input.meta, redshift, on='object_id', how='left')
    meta = pd.merge(meta, max_flux, on='object_id', how='left')
    meta = pd.merge(meta, min_flux, on='object_id', how='left')

    meta['Mpc'] = meta['hostgal_z_predicted'].apply(z2pc)
    meta['Gpc'] = meta['Mpc'] / 1000.0

    features = []
    for i in range(6):
        ch = i
        meta['snr3_flux_diff_ch{}'.format(ch)] = meta['snr3_max(flux)_ch{}'.format(ch)] - meta['snr3_min(flux)_ch{}'.format(ch)]
        meta['snr3_luminosity_diff_ch{}'.format(ch)] = meta['snr3_flux_diff_ch{}'.format(ch)] * meta['Gpc'] * meta['Gpc']
        features.append('snr3_luminosity_diff_ch{}'.format(ch))

    return meta[['object_id']+features]

@feature('f1086')
def f1086_first_detected_to_prev_mjd_diff(input: Input, **kw):
    lc = input.lc
    lc['mjd_prev'] = lc[['object_id', 'mjd']].groupby('object_id').shift(1)
    lc_detected = lc[lc.detected == 1]

    first_obs = lc_detected.groupby('object_id').first()
    first_obs['first_detected_to_prev_mjd_diff'] = first_obs['mjd'] - first_obs['mjd_prev']

    return first_obs[['first_detected_to_prev_mjd_diff']].reset_index()

@feature('f1087')
def f1087_last_detected_to_next_mjd_diff(input: Input, **kw):
    lc = input.lc
    lc['mjd_next'] = lc[['object_id', 'mjd']].groupby('object_id').shift(-1)
    lc_detected = lc[lc.detected == 1]

    last_obs = lc_detected.groupby('object_id').last()
    last_obs['last_detected_to_next_mjd_diff'] = last_obs['mjd_next'] - last_obs['mjd']

    return last_obs[['last_detected_to_next_mjd_diff']].reset_index()

@feature('f1088')
def f1088_first_detected_to_prev_mjd_diff_perch(input: Input, **kw):
    lc = input.lc
    lc['mjd_prev'] = lc[['object_id', 'mjd']].groupby('object_id').shift(1)
    lc_detected = lc[lc.detected == 1]

    first_obs_pb = lc_detected.groupby(['object_id', 'passband']).first()
    first_obs_pb['first_detected_to_prev_mjd_diff_perch'] = first_obs_pb['mjd'] - first_obs_pb['mjd_prev']

    return unstack(first_obs_pb[['first_detected_to_prev_mjd_diff_perch']])

@feature('f1089')
def f1089_last_detected_to_next_mjd_diff_perch(input: Input, **kw):
    lc = input.lc
    lc['mjd_next'] = lc[['object_id', 'mjd']].groupby('object_id').shift(-1)
    lc_detected = lc[lc.detected == 1]

    last_obs_pb = lc_detected.groupby(['object_id', 'passband']).last()
    last_obs_pb['last_detected_to_next_mjd_diff_perch'] = last_obs_pb['mjd_next'] - last_obs_pb['mjd']

    return unstack(last_obs_pb[['last_detected_to_next_mjd_diff_perch']])


#!/usr/bin/python3
# -*- coding:utf-8 -*-
# Handler file to run pycasso when using pijuice

import logging
import os
import sys
import time

from pijuice import PiJuice

from piblo.pycasso import Pycasso
from piblo.constants import PiJuiceConst, BatteryConst

class PiJuiceHandler:
    """
    A class used to provide image operations for pycasso.

    Attributes
    ----------

    Methods
    -------
    safe_pijuice_shutdown(pijuice)
        Uses PiJuice to attempt to shut down safely and completely using pijuice object 'pijuice'.

    system_shutdown()
        Uses linux system command to shut down.

    pijuice_led_disable(pijuice)
        Uses PiJuice to disable LEDs on board with pijuice object 'pijuice'.

    get_charge_status(power_status, charge_level)
        Returns valid charge level or constant to be passed to pycasso for battery display. Uses valid pijuice
        power_status and charge_level

    def run(self):
        Attempts to run pycasso based on configuration and pijuice settings.
    """
    def __init__(self):

        return

    @staticmethod
    def safe_pijuice_shutdown(pijuice):
        # Remove power to PiJuice MCU IO pins
        pijuice.power.SetSystemPowerSwitch(0)

        # In 5 seconds we are not so nice - Remove 5V power to RPi
        pijuice.power.SetPowerOff(5)

        # Enable wakeup alarm
        pijuice.rtcAlarm.SetWakeupEnabled(True)

        # But try to shut down nicely first
        os.system("sudo shutdown -h 0")
        sys.exit()

    @staticmethod
    def system_shutdown():
        os.system("shutdown /s /t 1")
        sys.exit()

    @staticmethod
    def pijuice_led_disable(pijuice):
        led_config = {'function': 'NOT_USED', 'parameter': {'r': 0, 'g': 0, 'b': 0}}
        pijuice.config.SetLedConfiguration('D1', led_config)
        pijuice.config.SetLedConfiguration('D2', led_config)
        return

    @staticmethod
    def get_charge_status(power_status, charge_level):
        if power_status == PiJuiceConst.NOT_PRESENT.value:
            charge_level = charge_level
        elif power_status == PiJuiceConst.PRESENT.value:
            charge_level = BatteryConst.CHARGING.value
        elif power_status == PiJuiceConst.WEAK.value:
            charge_level = BatteryConst.WEAK.value
        elif power_status == PiJuiceConst.BAD.value:
            charge_level = BatteryConst.BAD.value
        else:
            charge_level = BatteryConst.ERROR.value

        return charge_level

    def run(self):
        try:
            instance = Pycasso()
        except Exception as e:
            logging.error(e)
            logging.error("Cannot create Pycasso object. Exiting process.")
            sys.exit()

        # Set config variables based on config that pycasso loaded
        sleep_time = instance.config.wait_to_run
        shutdown = instance.config.shutdown_on_battery
        shutdown_ex = instance.config.shutdown_on_exception

        power_status = None
        charge_level = BatteryConst.ERROR.value

        # sleep for a bit as I can't figure out a better solution to stop pijuice from failing to start
        time.sleep(sleep_time)
        try:
            pijuice = PiJuice(1, 0x14)
            power_status = pijuice.status.GetStatus()[PiJuiceConst.STATUS_ROOT.value][PiJuiceConst.STATUS_POWER.value]
            charge_level = pijuice.status.GetChargeLevel()['data']
            instance.charge_level = PiJuiceHandler.get_charge_status(power_status, charge_level)

        except Exception as e:
            logging.error(e)
            logging.error("Cannot create pijuice object. Running pycasso once with error display and exiting process.")
            # run pycasso with error symbol, then exit
            instance.add_exception_icon()
            instance.run()

            if shutdown_ex:
                logging.info(f"Shutting down if possible. Waiting {sleep_time} seconds before sending signal")
                time.sleep(sleep_time)
                self.system_shutdown()
                sys.exit()

        logging.info(f"Power status is \'{power_status}\'")
        logging.info(f"Battery level is \'{charge_level}\'")

        try:
            instance.run()

            if power_status == PiJuiceConst.NOT_PRESENT.value:
                # shutdown if we've configured pycasso to do so
                if shutdown:
                    self.safe_pijuice_shutdown(pijuice)

        except Exception as e:
            logging.error(e)

            # shutdown if we've configured pycasso to do so
            if shutdown_ex:
                logging.info(f"Shutting down if possible. Waiting {sleep_time} seconds before sending signal")
                time.sleep(sleep_time)
                self.safe_pijuice_shutdown(pijuice)
            sys.exit()

        return


import numpy as np
import pandas as pd

def Ivan_horse(filename):
	#print(filename)
	import scipy.constants as const
	ds=pd.read_csv(filename,sep='\t',index_col=0)
	ds.index=ds.index.astype(float)
	ds.columns=ds.columns.astype(float)
	#ds.index=ds.index.values/1e8
	#ds.columns=ds.columns.values/1e5
	#ds=ds.apply(lambda x:x*ds.index.values)
	#ds=ds.apply(lambda x:x*ds.columns.values,axis=1)
	#per_photon=const.h*const.c/(485e-9)
	#ds=	ds*per_photon
	ds.index.name='Fluence in Photons/cm2 s'
	ds.columns.name='Repetitions rate in Hz'
	ds.sort_index(inplace=True)
	ds.sort_index(inplace=True,axis=1)
	return ds.T,'PLQY'

def streak_Lund(filename):
    code=filename.split('-')
    ds=pd.read_csv(filename,sep='\t',header=None)
    n_times=len(ds.index.values)
    n_waves=len(ds.columns)
    times={'t6':np.linspace(0,2000,n_times),
           't5':np.linspace(0,1000,n_times),
           't4':np.linspace(0,500,n_times),
           't3':np.linspace(0,200,n_times),
           't2':np.linspace(0,100,n_times),
           't1':np.linspace(0,50,n_times)}
    for i in [4,3,2,5]:
        try:
            times=times[code[i]]
            break
        except:
            pass
    for i in [6,5,7,8]:
        if code[i][0]=='w':
            center=code[i][1:]
            waves=np.linspace(float(center)-60,float(center)+75,n_waves)
            break
        else:
            continue
    ds.index=times
    ds.index=ds.index.astype(float)
    ds.columns=waves
    ds.columns=ds.columns.astype(float)
    ds.index.name='Time in ps'
    ds.columns.name='Wavelength in nm'
    return ds,'emission intensity'

def Amine_func(filename):
	df=pd.read_csv(filename,sep='\t',header=None)
	wavelength=pd.Series(np.linspace(343.33,656.03,512))
	time=pd.Series(np.linspace(0,50.500,512))
	df.columns=wavelength
	df.index=time
	df.index=df.index.astype(float)
	df.columns=df.columns.astype(float)
	df.index.name='Time in ns'
	df.columns.name='Wavelength in nm'
	return df,'differential absorption','ns'


import argparse
import sys

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--arpa', required=True)
    args = parser.parse_args()

    with open(args.arpa, 'r') as f:
        arpafile =  [_.strip().split() for _ in f.readlines()]

    mapping = dict([(_[0], ' '.join(_[1:])) for _ in arpafile])
    # Manual edit for common words
    mapping['us'] = 'AH S'

    for line in sys.stdin:
        utt_id = line.split()[0]
        line_words = line.split()[1:]

        arpa = ' <space> '.join([mapping[_] for _ in line_words])

        outp = utt_id + ' ' + arpa
        print(outp)


import os
import petl
import logging as log

log.basicConfig(level=os.environ.get("LOGLEVEL","INFO"))

inputFile='./35100077.csv'
try:
    cansimData = petl.fromcsv(inputFile)

except Exception as e:
    log.error(e)

cansimData = petl.cut(cansimData,0,'GEO','DGUID','Statistics','VALUE')

outputData = petl.recast(cansimData,variablefield='Statistics',valuefield='VALUE')
petl.io.csv.tocsv(outputData,'output.csv')


""".. module:: SPTPol

:Synopsis: Definition of python-native CMB likelihood for SPT likelihood (SPTpol+SZ l=2000-11000
power spectrum). Adapted from Fortran likelihood code
https://lambda.gsfc.nasa.gov/product/spt/spt_ps_2020_get.cfm

:Author: Matthieu Tristram

"""
import os
from typing import Optional, Sequence

import numpy as np
from cobaya.likelihoods.base_classes import InstallableLikelihood
from cobaya.log import LoggedError

from . import spt_foregrounds as sptfg

class SPTHiellLikelihood(InstallableLikelihood):
    install_options = {
        "download_url": "https://lambda.gsfc.nasa.gov/data/suborbital/SPT/reichardt_2020/likelihood.tar.gz",
        "data_path": "spt_hiell_2020",
    }

    cal_cov = [
        [1.1105131e-05, 3.5551351e-06, 1.1602891e-06],
        [3.5551351e-06, 3.4153547e-06, 2.1348018e-06],
        [1.1602891e-06, 2.1348018e-06, 1.7536000e-05],
    ]

    frequencies: Sequence[int] = [95, 150, 220]

    data_folder: Optional[str] = "spt_hiell_2020/likelihood"
    desc_file: Optional[str]
    bp_file: Optional[str]
    cov_file: Optional[str]
    beamerr_file: Optional[str]
    window_file: Optional[str]

    normalizeSZ_143GHz: Optional[bool] = True
    callFGprior: Optional[bool] = True
    applyFTSprior: Optional[bool] = True

    def initialize(self):
        # Set path to data
        if (not getattr(self, "path", None)) and (not getattr(self, "packages_path", None)):
            raise LoggedError(
                self.log,
                "No path given to CIB_Likelihood data. Set the likelihood property 'path' or the common property '%s'.",
                _packages_path,
            )

        # If no path specified, use the modules path
        data_file_path = os.path.normpath(
            getattr(self, "path", None) or os.path.join(self.packages_path, "data")
        )

        # check data_folder
        self.data_folder = os.path.join(data_file_path, self.data_folder)
        if not os.path.exists(self.data_folder):
            raise LoggedError(
                self.log,
                "The 'data_folder' directory does not exist. Check the given path [%s].",
                self.data_folder,
            )

        # Init foreground model
        self.fg = sptfg.SPTforegounds(data_folder=self.data_folder, **self.foregrounds)

        # Update data_folder location
        self.data_folder = os.path.join(self.data_folder, "data/spt_hiell_2020")

        # get info from the desc_file
        self._update_with_desc_file()

        self.log.debug(f"nall: {self.nall}")
        self.log.debug(f"nfreq: {self.nfreq}")
        self.log.debug(f"spt_windows_lmin: {self.spt_windows_lmin}")
        self.log.debug(f"spt_windows_lmax: {self.spt_windows_lmax}")

        self.lmin = self.spt_windows_lmin
        self.lmax = self.spt_windows_lmax + 1  # to match fortran convention

        if self.spt_windows_lmax > self.fg.ReportFGLmax():
            raise LoggedError(self.log, "Hard-wired lmax in foregrounds is too low for SPT_hiell")

        if self.spt_windows_lmin < 2 or self.spt_windows_lmin >= self.spt_windows_lmax:
            raise LoggedError(self.log, "Invalid lranges for sptpol")

        # read bandpowers (90x90, 90x150, 90x220, 150x150, 150x220, 220x220)
        # check file before
        bla, self.spec = np.loadtxt(os.path.join(self.data_folder, self.bp_file), unpack=True)

        # read covariance
        # check file before
        self.cov = np.fromfile(
            os.path.join(self.data_folder, self.cov_file), dtype=np.float64
        ).reshape(self.nall, self.nall)

        # read beam_err
        # check file before
        self.beam_err = np.fromfile(
            os.path.join(self.data_folder, self.beamerr_file), dtype=np.float64
        ).reshape(self.nall, self.nall)

        # Read windows
        # check file before
        self.windows = self._read_windows(os.path.join(self.data_folder, self.window_file))

        # define indicies
        self.indices = []
        for j in range(self.nfreq):
            for k in range(j, self.nfreq):
                self.indices.append((j, k))

        # define offsets for xfreq in Cl vector
        self.offsets = [0]
        for i in range(1, self.nband):
            self.offsets.append(self.offsets[i - 1] + self.nbins[i - 1])

        self.log.info("Init SPTlik done")

    def _read_windows(self, filename):
        import struct

        with open(filename, "rb") as f:
            efflmin, efflmax = struct.unpack("@II", f.read(2 * np.dtype(np.int32).itemsize))

        if efflmax < self.spt_windows_lmin or efflmin > self.spt_windows_lmax:
            raise LoggedError(self.log, "unallowed l-ranges for binary window functions")

        #        j0 = self.spt_windows_lmin if self.spt_windows_lmin > efflmin else efflmin
        #        j1 = self.spt_windows_lmax if self.spt_windows_lmax < efflmax else efflmax
        #        if j1 < j0:
        #            raise ValueError( "unallowed l-ranges for binary window functions - no allowed ells")

        offset = 2 * (np.dtype(np.int32).itemsize)
        #        windows = np.zeros( (self.nall, self.spt_windows_lmax+1) )
        #        windows[:,j0:j1+1] = np.fromfile( filename, dtype=np.float64, offset=offset).reshape(self.nall,-1)
        # windows = np.zeros((self.nall, efflmax + 1))
        windows = np.fromfile(filename, dtype=np.float64, offset=offset).reshape(self.nall, -1)

        return windows

    def _update_with_desc_file(self):
        filename = os.path.join(self.data_folder, self.desc_file)

        with open(filename) as f:
            self.nall, self.nfreq = [int(float(x)) for x in next(f).split()]
            self.nband = int(self.nfreq * (self.nfreq + 1) / 2)

            self.nbins = [int(next(f)) for i in range(self.nband)]
            if self.nall != sum(self.nbins):
                raise LoggedError(self.log, "Mismatched number of bandpowers")

            self.spt_norm_fr = [float(next(f)) for i in range(5)]

            if self.normalizeSZ_143GHz:
                self.log.debug("Using 143 as tSZ center freq")
                self.spt_norm_fr[4] = 143.0

            self.spt_windows_lmin, self.spt_windows_lmax = [int(x) for x in next(f).split()]

            eff_fr = []
            for _ in range(self.nfreq):
                eff_fr.append([float(next(f)) for i in range(5)])
            self.spt_eff_fr = np.array(eff_fr)

            self.spt_prefactor = np.array([float(next(f)) for i in range(self.nfreq)])

    def _gaussian_loglike(self, dlcov, res):
        """
        Returns -Log Likelihood for Gaussian: (d^T Cov^{-1} d + log|Cov|)/2
        """
        from scipy.linalg import cho_factor, cho_solve

        L, low = cho_factor(dlcov)

        # compute det
        detcov = 2.0 * np.sum(np.log(np.diag(L)))

        # Compute C-1.d
        invCd = cho_solve((L, low), res)

        # Compute chi2
        chi2 = res @ invCd

        return chi2 / 2.0, detcov / 2.0

    def loglike(self, dl_cmb, **params):
        """
        dl_cmb: Dl TT
        """
        CalFactors = [params[f"mapCal{nu}"] for nu in self.frequencies]
        FTSfactor = params["FTS_calibration_error"]

        # scaling theory
        # tszfac = sptfg.cosmo_scale_tsz(theory.H0,theory.sigma_8,theory.omb)
        # params["czero_tsz"] = params["czero_tsz"] * tszfac
        # kszfac = sptfg.cosmo_scale_ksz(theory.H0,theory.sigma_8,theory.omb,theory.omc+theory.omb+theory.omnu,theory.InitPower(ns_index),theory.tau)
        # params["czero_ksz"] = params["czero_ksz"] * kszfac

        # Loop on nband
        cbs = np.zeros(self.nall)
        for i in range(self.nband):
            j, k = self.indices[i]
            thisoffset = self.offsets[i]
            thisbin = self.nbins[i]

            # get theory spectra
            dl_fg = self.fg.dl_foregrounds(
                params,
                j,
                k,
                self.nfreq,
                self.spt_eff_fr + FTSfactor,
                self.spt_norm_fr,
                self.spt_windows_lmin,
                self.spt_windows_lmax
            )
            dl_th = dl_cmb[self.lmin : self.lmax] + dl_fg[self.lmin : self.lmax]

            # bin theory with window functions
            tmpcb = self.windows[thisoffset:thisoffset+thisbin] @ dl_th

            # apply prefactors
            tmpcb = tmpcb * self.spt_prefactor[k] * self.spt_prefactor[j] * CalFactors[j] * CalFactors[k]

            cbs[thisoffset : thisoffset + thisbin] = tmpcb

        # Residuals
        delta_cb = cbs - self.spec

        # Dl covariance (with beams)
        cov_w_beam = self.cov + self.beam_err * np.outer(cbs, cbs)

        # compute LogLike
        LnL, detcov = self._gaussian_loglike(cov_w_beam, delta_cb)
        SPTHiEllLnLike = LnL + detcov

        # Add FG priors
        if self.callFGprior:
            FGPriorLnLike = self.fg.getForegroundPriorLnL(params)
            SPTHiEllLnLike += FGPriorLnLike

        # Add calib LogLike
        delta_calib = np.log(CalFactors)
        CalibLnLike, _ = self._gaussian_loglike(self.cal_cov, delta_calib)
        SPTHiEllLnLike += CalibLnLike

        # Add FTS prior
        # Prior is 0.3 GHz for 1 sigma around 0.
        if self.applyFTSprior:
            FTSLnLike = 0.5 * (FTSfactor / 0.3) ** 2
            SPTHiEllLnLike += FTSLnLike

        self.log.debug(f"SPTHiEllLnLike lnlike = {SPTHiEllLnLike} (with priors)")
        self.log.debug(f"Calibration chisq = {2 * CalibLnLike}")
        self.log.debug(f"lnLcov term = {detcov}")
        self.log.debug(f"chisq for cov only: {2 * LnL}")
        self.log.debug(f"chisq for FG prior: {2 * FGPriorLnLike}")
        self.log.debug(f"SPTHiEllLnLike chisq (after prior) = {2 * (SPTHiEllLnLike - detcov)}")

        return -SPTHiEllLnLike

    def get_requirements(self):
        requirements = dict(Cl={mode: self.lmax for mode in ["tt"]})
        return requirements

    def logp(self, **params_values):
        dl = self.theory.get_Cl(units="muK2", ell_factor=True)["tt"]
        return self.loglike(dl, **params_values)


import math
import os
import random
import re
import sys

# Complete the kangaroo function below.
def kangaroo(x1, v1, x2, v2):
    if (v1 > v2 and not ((x2-x1)%(v1-v2))):
        print("YES")
        return 'YES'
    else:
        print("NO")
        return 'NO'
    # f1 = lambda x, y: x in y
    # arr1 = [x for x in range(10000) if x % (x1+v1) == 0]
    # arr2 = [x for x in range(10000) if x % (x2+v2) == 0]
    # print(arr1,'\n',arr2)
    # longest = 'arr1' if len(arr1) > len(arr2) else 'arr2'
    # longest_arr = arr1 if len(arr1) > len(arr2) else arr2
    # smallest = arr2 if longest == 'arr1' else arr1
    # for i in range(1, len(arr1 if len(arr1) > len(arr2) else len(arr2))):
    #     if i == len(smallest):
    #         break
    #     print(i, smallest[i], f1(smallest[i], arr1 if longest == 'arr1' else arr2))
    #     state = f1(smallest[i], arr1 if longest == 'arr1' else arr2)
    #     if state is True: return 'YES'
    # return 'NO'

vals = list(map(int, '0 2 5 3'.split()))
vals1 = list(map(int, '0 3 4 2'.split()))
vals3 = list(map(int, '21 6 47 3'.split()))
x1, v1, x2, v2 = vals3.append()
kangaroo(x1, v1, x2, v2)


from random import randint
import time

n = 50000000

timer = time.time()
S = [randint(0, 4*n) for x in range(n)]
print(time.time() - timer)

def getMode(s):
    """Returns the Mode of the passed sequence s"""
    dict = {}
    for x in s:
        if x in dict.keys():
            dict[x] += 1
        else:
            dict[x] = 1
    mode = None
    modes = []
    for x in dict:
        if mode == None or mode[1] < dict[x]:
            mode = (x, dict[x])
            modes = [x]
        elif mode[1] == dict[x]:
            modes.append(x)
    return (mode[1], insertionSort(modes))

def insertionSort(s):
    list = [s[0]]
    for x in s[1:]:
        for i in range(len(list)):
            if x >= list[i]:
                list.insert(i,x)
                break
        else:
            list.append(x)
    return list

timer = time.time()
k = getMode(S)
print("The numbers that occur the most are", k[1], "which occur", k[0], "times.\n\tThis took", (time.time() - timer)*1000, "milliseconds.")


import os

# Place to Set or Store Config
class Config:
    # Config of Database which using SQLAlchemy
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_DATABASE_URI = os.getenv("DB_URI")
    # JWT Secret Key to Generate JWT
    JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY")


import numpy as np
import pandas as pd

#load data
df = pd.read_csv("C:/Users/Student/Desktop/SemesterProject/sneakers_Reviews_Dataset.csv", delimiter = ';')
df.head()

import subprocess

# Install NLTK using pip
subprocess.check_call(['pip', 'install', 'nltk'])

import nltk

nltk.download('punkt')

##pre processing

import re
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer
from sklearn.feature_extraction.text import CountVectorizer

#convert to lowercase

df.head()

# remove special characters, numbers, and punctuation
df['review_text'] = df['review_text'].apply(lambda text: re.sub(r'[^a-zA-Z\s]', '', text))

df.head()

#tokenization

df['review_text'] = [word_tokenize(text) for text in df['review_text']]

#remove stopwords
stop_words = set(stopwords.words('english'))

df['review_text'] = [[word for word in words if word not in stop_words] for words in df['review_text']]

#stemming (reducing words to their root form)
stemmer = PorterStemmer()
df['review_text'] = [[stemmer.stem(word) for word in words] for words in df['review_text']]

#convert back text
df['review_text'] = [''.join(words) for words in df['review_text']]

#####

#feature extraction

#vectorization (convert text to numerical representation)
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(df['review_text'])

df

#deriving the sentiment frm the rating and text column

def label_sentiment(rating):
    if rating >= 4:
        return 'positive'
    elif rating == 3:
        return 'neutral'
    else:
        return 'negative'

# applying the function
df['sentiment'] = df['rating'].apply(label_sentiment)

#text length

# Install matplotlib using pip
subprocess.check_call(['pip', 'install', 'matplotlib'])

import matplotlib.pyplot as plt

#distribution of sentiment labels
sentiment_counts = df['sentiment'].value_counts()

plt.bar(sentiment_counts.index, sentiment_counts)
plt.title('Distribution of sentiment labels')
plt.xlabel('Sentiment')
plt.ylabel('Count')
plt.show()

df['text_length'] = df['review_text'].apply(len)

plt.figure(figsize = (10, 6))

for sentiment in df['sentiment'].unique():
    subset = df[df['sentiment'] == sentiment]
    plt.hist(subset['text_length'], bins = 30, alpha = 0.5, label = sentiment)

plt.title('Histogram of Text length in sentiment analysis')
plt.xlabel('Text length')
plt.ylabel('Frequency')
plt.legend()
plt.show()

#analyzing the frequency of words
from wordcloud import WordCloud

text = df['review_text'].str.cat(sep=' ')

# generate a word cloud
wordcloud = WordCloud(width = 800, height = 400, background_color = 'white').generate(text)

#plot the word cloud
plt.figure(figsize = (10, 5))
plt.imshow(wordcloud, interpolation = 'bilinear')
plt.axis('off')
plt.show()

########

# model

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, roc_curve

# split the data into test/train

del df['review_text']
train = df[df['sentiment', 'timestamp'].notnull().all(axis=1)]
test = df[df['sentiment'].isnull()]
del test['sentiment']

train['sentiment'].value_counts()

train_df = train
test_df = test

X_train, X_test, y_train, y_test = train_test_split(
    train_df.drop(labels=['sentiment', 'timestamp'], axis=1),
    train_df['sentiment'].values,
    test_size=0.2,
    random_state=42
)

# Initialize base models
model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)

# Make predictions
predictions = model.predict(X_test)

#evaluate performance
report = classification_report(y_test, predictions)
print("Classification Report:")
print(report)

print(f"Accuracy: {accuracy:.4f}")

# Save the vectorizer and model
with open('vectorizer_model.pkl', 'wb') as file:
    pickle.dump({'vectorizer': vectorizer, 'model': model}, file)
with open('sneakerscript.pkl', 'wb') as file:
    pickle.dump(model, file)


import csv
import cPickle
rows = csv.reader(open("../ori_data/xgboost_feat_quyurenshu.csv",'rb'))
rows.next()
pjf_feat_dict={}
for row in rows:
    key = row[1]+"-"+row[0].split("-")[2]+"-"+row[2]

    pjf_feat_dict[key]=row[3:]
f = open("../pjf_feat_train_quyurenshu.pkl",'wb')
cPickle.dump(pjf_feat_dict,f,-1)
f.close()
# rows = csv.reader(open("../ori_data/gbdt_feat_target(1).csv",'rb'))
# rows.next()
# pjf_feat_dict1={}
# for row in rows:
#     key = row[1]+"-"+row[0].split("-")[2]+"-"+row[2]
#     print key
#     pjf_feat_dict1[key]=row[3:]
# f = open("../pjf_feat_predict(1).pkl",'wb')
# cPickle.dump(pjf_feat_dict1,f,-1)
# f.close()


import habitat_sim
import cv2
import numpy as np
import pandas as pd

STEP_SIZE = 0.25

# 初始化position
position = np.array([-2.4452953 ,  0.17515154,  2.349534  ], dtype='float32')

# 初始化rotation
rotation = np.quaternion(-0.150860279798508, 0, 0.988555133342743, 0)

# 初始化sensor_states
sensor_states = {
    'rgb': habitat_sim.agent.SixDOFPose(
        position=np.array([-2.306779, 1.6751516, -0.6472672], dtype='float32'),
        rotation=np.quaternion(-0.150860279798508, 0, 0.988555133342743, 0)
    )
}
zero_state = habitat_sim.AgentState(position=position,rotation=rotation,sensor_states=sensor_states)

def make_configuration():
    # 模拟器配置
    backend_cfg = habitat_sim.SimulatorConfiguration()
    backend_cfg.scene_id = "data/scene_datasets/gibson/Beach.glb"
    backend_cfg.enable_physics = True
    # backend_cfg.allow_sliding = True

    # agent 配置
    CAMsensor_cfg = habitat_sim.CameraSensorSpec()
    CAMsensor_cfg.sensor_type = habitat_sim.sensor.SensorType.COLOR
    CAMsensor_cfg.uuid = "rgb"
    CAMsensor_cfg.resolution = [256,256]
    # DepSensor_cfg = habitat_sim.CameraSensorSpec()
    # DepSensor_cfg.uuid = "depth"
    # DepSensor_cfg.resolution = [256,256]
    # DepSensor_cfg.sensor_type = habitat_sim.sensor.SensorType.DEPTH # 深度
    agent_cfg = habitat_sim.agent.AgentConfiguration()
    agent_cfg.sensor_specifications = [CAMsensor_cfg]

    return habitat_sim.Configuration(backend_cfg,[agent_cfg])

cfg = make_configuration()
sim = habitat_sim.Simulator(cfg)
agent = sim.initialize_agent(0)
agent_state = agent.get_state()
follower = sim.make_greedy_follower(agent_id=0, goal_radius=STEP_SIZE,stop_key="Stop")
observations = sim.reset()

MAX_EPISODE = 5000
MAX_EPOCH = 1000

listpd = pd.DataFrame(columns=['location'])

for episode in range(MAX_EPISODE):
    print("EPISODE:",episode,"/5000")
    nav_point = sim.pathfinder.get_random_navigable_point()
    observations = sim.reset()
    agent.set_state(zero_state)
    cv2.imshow("rgb",observations['rgb'])
    for epoch in range(MAX_EPOCH):
        try:
            action = follower.next_action_along(goal_pos=nav_point)
        except habitat_sim.errors.GreedyFollowerError:
            break
        # print(action)
        if action !=  "Stop":
           observations = sim.step(action)
           # cv2.imshow("rgb",observations['rgb'])
           # cv2.waitKey(1)
        else:
            imname ="/home/yuanzhao/PycharmProjects/Newnav/train_data/img/Beach/" +"_" + str(episode) + "_" +str(epoch) + "_" +".png"
            cv2.imwrite(imname,observations['rgb'])
            listpd.loc[len(listpd)] = str(nav_point.ravel().tolist())
            break

listpd.to_csv("/home/yuanzhao/PycharmProjects/Newnav/train_data/img/Beach/listpd.csv")


from PIL import *
import cv2, numpy as np
import matplotlib.pylab as plt
import glob
import requests
import json
import time
import os

print("===== start ===== ")

# 평균 해시로 변환 method
def tohash(img):

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    gray = cv2.resize(gray, (64,64))

    avg = gray.mean()

    bins = 1 * (gray > avg)

    return bins

# 해밍 거리 측정 method
def hamming(a,b):

    a = a.reshape(1, -1)

    b = b.reshape(1, -1)

    distance = (a != b).sum()

    return distance

while True:

    # take a picture first
    #os.system('wget http://192.168.0.96:8091/?action=snapshot -O 0204_image_first.jpg')
    #print("take a picture first  //  file list : ")
    #print(os.listdir(os.getcwd()))

    #60second delay
    #time.sleep(10)

    print("")

    print("===== while start ===== ")

    print("5 second later take a picture")
    time.sleep(1)

    print("4 second later take a picture")
    time.sleep(1)

    print("3 second later take a picture")
    time.sleep(1)

    print("2 second later take a picture")
    time.sleep(1)

    print("1 second later take a picture")
    time.sleep(1)

    # take a picture second
    os.system('wget http://192.168.0.2:8091/?action=snapshot -O 0204_image_second.jpg')
    print("take a picture first  //  file list : ")
    print(os.listdir(os.getcwd()))

    #comparing fist and second
    img1 = cv2.imread('/home/pi/fdCam/0204_image_first.jpg')
    img2 = cv2.imread('/home/pi/fdCam/0204_image_second.jpg')

    imgs = [img1, img2]

    white = np.arange(2)

    # 이미지의 해밍거리 계산

    img1_hash = tohash(img1)

    img2_hash = tohash(img2)

    dst = hamming(img1_hash, img2_hash)

    if dst/256 > 0.3 :
        #smilarity higer than 10% and white(green) pixels increase !!
        #in case of a litle change, : 0.10546875
        print("== 1. image changed ")
        print("no same picture : ","해밍거리 값 : ",dst/256) # 해밍 거리 출력

        # hand or plant
        if dst/256 > 0.9 :
            print("== 1(1). hand in")

        else :
            print("== 1(2). plant in")

            white = np.arange(2)

            for i, img in enumerate(imgs) :

                #---① 각 이미지를 HSV로 변환
                hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
                mask = cv2.inRange(hsv, (36,0,0), (86,255,255))

                ## slice the green
                imask = mask>0
                green = np.zeros_like(img, np.uint8)
                green[imask] = img[imask]

                # 마스크 색을 흰색으로 바꾸기
                coloured = green.copy()

                coloured[mask == 255] = (255, 255, 255)

                #흰 색인 픽셀의 개수를 세기
                white[i] = np.sum(coloured == 255)

            if(white[1]-white[0]>6000) :

                print("1(2)-1 잡초가 나타났습니다.")
                print("no same picture : ","해밍거리 값 : ",dst/256) # 해밍 거리 출력
                print("how many white pixels : ",white)
                print("white[1] - white[0] :", white[1] - white[0] )

                #FCM
                serverToken = 'AAAArVqyUkk:APA91bHhS90vKQZ09y70Cm3FSDhCezwEdCI-I8ZhS-joS01clMUI22wCet0JeaH4hcOJfK281QRarz4INArpkZmkiEUbPSUK-8gFDcEZLKcyNsyNYSrgXu-NlLHI3n-m_G3C_caIdiI3'

                deviceToken = 'fe6DJH9UQO2mM27WtIxeoG:APA91bFpCAzxzlViwgvYxac0rVjTbYdpadpsat6RIV9wu2G0gzXCots6vsZLdIpwr1AxqMfsuXvcjZQdG3YqWcybr1LHMblDQXgYk4OnsWx6bYWTXj--bcrVmD13UMW6hSE8tRmP_4Bp'

                headers = {'Content-Type': 'application/json','Authorization': 'key=' + serverToken}

                body = {'notification': {'title': 'SmartFarm','body': '잡초가 있습니다.'},'to': deviceToken, 'data': {'title': 'SmartFarm','body': '잡초가 있습니다.'}}

                response = requests.post("https://fcm.googleapis.com/fcm/send",headers = headers, data=json.dumps(body))

                print(response.status_code)

            else :
                print("1(2)-1 잡초 X")
                print("no same picture : ","해밍거리 값 : ",dst/256) # 해밍 거리 출력
                print("how many white pixels : ",white)
                print("white[1] - white[0] :", white[1] - white[0] )

                # change file name second -> first

    else :

        print("== 2. image unchanged ")

        print("해밍거리 값 : ",dst/256)
        print("해밍거리 값 bigger 10%: ",dst/256 > 0.06)

    os.rename('/home/pi/fdCam/0204_image_second.jpg', '/home/pi/fdCam/0204_image_first.jpg')
    print("file name second -> first  //  file list : ")
    print(os.listdir(os.getcwd()))

    print("===== while end ===== ")

print("===== end ===== ")


'''
El programa debe preguntar el artículo y su precio y añadirlo a una variable (diccionario u objeto literal),
hasta que el usuario decida terminar ("Introducir otro elemento al carrito (Si / No)".

Una vez el usuario decida no introducir más elementos al carrito, debe mostrar por pantalla la lista de la
compra y el coste total.
'''

cesta = {}
continuar = True
while continuar:
    articulo = input('¿Qué artículo desea comprar?: ')
    precio = float(input('Introduzca el precio de ' + articulo + ': '))
    cesta[articulo] = precio
    continuar = input('¿Quiere continuar comprando? (SI/NO): ') == "SI"
importe = 0

print('Artículos en carrito: ')
for item, precio in cesta.items():
    print(item, '\t', precio)
    importe += precio
print('Importe total: ', importe)


from markdown_toolkit import MarkdownDocument

doc = MarkdownDocument()
with doc.heading("Markdown Toolkit"):
    doc.paragraph("Items added inside the context manager respect the heading level.")
    with doc.heading("Nested Header"):
        doc.paragraph(
            """
        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        Vestibulum ornare magna tincidunt, luctus neque eu, iaculis nisl.
        Duis porta pharetra hendrerit. Donec ac bibendum purus. Phasellus
        sagittis tincidunt metus, a condimentum enim malesuada ut.
        Integer porta pellentesque tempus. Vivamus erat est, imperdiet
        id enim in, rutrum luctus nisi. Praesent in nibh eleifend,
        consequat est quis, ultricies dolor.
        """
        )

with open("examples/example_headings.md", "w", encoding="UTF-8") as file:
    file.write(doc.render())


import argparse
import os
from tqdm import tqdm
import numpy as np
import re
import shutil

def process_GT_label(args):
   annotations_path = args.annotations_path
   annotations= os.listdir(annotations_path)
   save_path = args.save_path
   if os.path.exists(save_path):
      shutil.rmtree(save_path)
   os.makedirs(save_path, exist_ok=True)
   # print(annotations_path, save_path)
   # return
   str_XY = "(Xmax, Ymax)"
   str_size = 'Image size (X x Y x C)'

   os.makedirs(save_path, exist_ok=True)

   for file in tqdm(annotations):
      with open(os.path.join(annotations_path, file), encoding = "ISO-8859-1") as f:
         bboxes = []
         iter_f = f.readlines()
         for line in iter_f:
            if str_size in line:
               size = [int(i) for i in re.findall(r"\d+", line)]
               w, h = size[:2]
            if str_XY in line:
               strlist = line.split(str_XY)
               strlist1 = "".join(strlist[1:])    # 把list转为str
               strlist1 = strlist1.replace(':', '')
               strlist1 = strlist1.replace('-', '')
               strlist1 = strlist1.replace('(', '')
               strlist1 = strlist1.replace(')', '')
               strlist1 = strlist1.replace(',', '')
               b = strlist1.split()
               bnd = np.array([float(b[0]), float(b[1]), float(b[2]), float(b[3])])
               bnd[[0, 2]] = bnd[[0, 2]] / w
               bnd[[1, 3]] = bnd[[1, 3]] / h
               bnd *= args.rescale_factor
               bnd = [str(i) for i in bnd]
               bnd.insert(0, 'person')
               bboxes.append(' '.join(list(bnd)))
         with open(os.path.join(save_path, file), 'w') as f:
            f.write('\n'.join(bboxes))
      # break

if __name__ == "__main__":
   from pathlib import Path
   FILE = Path(__file__).resolve()
   PROJECT_DIR = FILE.parents[2]
   print(PROJECT_DIR)

   parser = argparse.ArgumentParser()
   parser.add_argument('-s', '--save_path', type=str, default=os.path.join(PROJECT_DIR, 'data/INRIAPerson/Test/labels/ground-truth-rescale-labels'),
                       help="Path of the parsed labels to save.")
   parser.add_argument('-a', '--annotations_path', type=str, default=os.path.join(PROJECT_DIR, 'data/INRIAPerson/Test/annotations'),
                       help="Path of the annotation files.")
   parser.add_argument('-r', '--rescale_factor', type=int, default=416,
                       help="Rescale factor for an input size [41, 416]. Decide this based on your input image size.")
   args = parser.parse_args()

   process_GT_label(args)


'''
Good morning! Here's your coding interview problem for today.

This problem was recently asked by Google.

Given a list of numbers and a number k, return whether any two numbers
from the list add up to k.

For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.

Bonus: Can you do this in one pass?
'''
flag = False

def find2Num(list,k):
    global flag
    s = set(list)
    for n in list:
        if (k-n) in s:
            print("True! \nNumbers {} and {} from the list {} add up to {}.".format(n, k-n, list, k))
            flag = True
            return flag

def main():
    list = [10, 15, 3, 7]
    k = 20
#    print("List is {} ".format(list))
    find2Num(list,k)
    if flag == False:
        print("No two numbers in {} add up to {}.".format(list,k))

if __name__ == "__main__":
    main()

# Bug15: k=20 gives True result as 10+10, whereas only one
# element 10 exists in the list.


from pipeline import PipelineStep
import cv2
import numpy as np

class ThresholdHSV(PipelineStep):
	def __init__(self, lower, upper):
		self.lower = lower
		self.upper = upper

	def process(self, inputs, visualize=False):
		img = inputs['img']
		img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
		mask = cv2.inRange(img_hsv, self.lower, self.upper)
		result = cv2.bitwise_and(img, img, mask=mask)

		outputs = {'img': result, 'debug_img': result}

		return outputs


from setuptools import setup, find_packages
from setuptools.extension import Extension
from distutils.command.build_ext import build_ext as du_build_ext

class build_ext(du_build_ext):
    def run(self):
        from Cython.Build.Dependencies import cythonize
        self.distribution.ext_modules[:] = cythonize(
            self.distribution.ext_modules,
            language_level=3)
        du_build_ext.run(self)

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

extensions = [
    Extension("kpkc.kpkc", sources=["kpkc/kpkc.pyx"]),
]

setup(
    name='kpkc',
    version='0.1.2a0',
    description='A python interface to KPartiteKClique',
    long_description=long_description,
    long_description_content_type='text/markdown',
    url='https://github.com/kliem/PyKPartiteKClique',
    author='Jonathan Kliem',
    author_email='jonathan.kliem@gmail.com',
    license='GPLv3',
    packages=find_packages(),
    ext_modules=extensions,
    zip_safe=False,
    python_requires='>=3.6',
    package_dir={'kpkc': 'kpkc'},
    install_requires=["Cython"],
    package_data={"kpkc": ["*.pxd", "*.h", "cppkpkc/*.h"]},
    cmdclass={'build_ext': build_ext},
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Science/Research',
        'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
        'Programming Language :: Python :: 3',
        "Programming Language :: Cython",
        "Programming Language :: C++",
        'Topic :: Scientific/Engineering :: Mathematics']
    )


# A model using n-dimensional simplices
# Date: April 2021

# Packages
import argparse                                                  # For options
import numpy as np                                               # For current directory to read and write files
import nest                                                      # For main simulation
from datetime import datetime                                    # For messages
from pathlib import Path                                         # For path handling

# Formatted printers for messages
def ntnstatus(message):
    print("\n"+datetime.now().strftime("%b %d %H:%M:%S")+" neurotop-nest: "+message, flush=True)

def ntnsubstatus(message):
    print('    '+message, flush=True)

# ******************************************************************************#
def simulate(args):
    # Configuration
    ntnstatus('Configuring')
    root = args.root                                                # Current working directory
    exc = {'weight': 10.0, 'delay': 0.5}                            # Excitatory synapse parameters
    inh = {'weight': -1.5, 'delay': 0.1}                            # Inhibitory synapse parameters
    simulation_id = args.id_prefix + datetime.now().strftime("%s")  # Custom ID so files are not overwritten

    # Load circuit and stimulus
    ntnstatus('Loading circuit and stimulus from ' + root+'/structure/'+args.exc_adj+'.npy')
    adj = np.load(root+'/structure/'+args.exc_adj+'.npy', allow_pickle=True)          # Adjacency matrix of circuit
    if args.inh_adj:
        adj_inh = np.load(root+'/structure/'+args.inh_adj+'.npy', allow_pickle=True)  # Inhibitory synapse mask
    else:
        adj_inh = np.zeros_like(adj)
    nnum = len(adj)
    stimulus_targets = {
        'all': [list(range(nnum))],
        'source': [[0]],
        'sink': [[nnum-1]]
    }

    try:
        fibres = stimulus_targets[args.stimulus_targets]
    except KeyError:
        ntnsubstatus("Stimulus targets config " + args.stimulus_targets+ " not found. Connecting stimulus to all.")
        fibres = stimulus_targets['all']

    firing_pattern = [(
                    0,
                    args.stimulus_start,
                    args.stimulus_start + args.stimulus_length,
                    float(args.stimulus_strength)
        )]    # (fibre index, start time, stop time, rate)

    # Build the circuit
    nest.ResetKernel()                                                             # Reset nest
    nest.SetKernelStatus({"local_num_threads": args.threads})                      # Run on many threads
    n_procs = nest.GetKernelStatus("total_num_virtual_procs")                      # Set RNGs
    nest.SetKernelStatus({"grng_seed": args.seed * (n_procs + 1)})
    nest.SetKernelStatus({"rng_seeds": list(range(args.seed * (n_procs + 1) + 1, (args.seed + 1)*(n_procs + 1)))})

    for source, target in []:
        adj_inh[source][target] = True

    ntnstatus('Constructing circuit')
    network = nest.Create('izhikevich', n=nnum, params={'a': 1.1})
    if args.p_transmit > 1 or args.p_transmit < 0:
        raise ValueError('Transmit probability out of 0-1 boundaries.')
    if args.p_transmit == 1:
        for source, target in zip(*np.nonzero(adj)):
            inh_syn = adj_inh[source][target]
            nest.Connect((source+1,), (target+1,), conn_spec='all_to_all', syn_spec={
                'model': 'static_synapse',
                'weight': exc['weight'] if not inh_syn else inh['weight'],
                'delay': exc['delay'] if not inh_syn else inh['delay']})
    else:
        for source, target in zip(*np.nonzero(adj)):
            inh_syn = adj_inh[source][target]
            nest.Connect((source+1,), (target+1,), conn_spec='all_to_all', syn_spec={
                'model': 'bernoulli_synapse',
                'weight': exc['weight'] if not inh_syn else inh['weight'],
                'delay': exc['delay'] if not inh_syn else inh['delay'],
                'p_transmit': args.p_transmit})

    # Connect voltage and spike readers
    ntnstatus('Adding voltage and spike readers')
    voltmeter = nest.Create('voltmeter', params={
        'label': 'volts',
        'withtime': True,
        'withgid': True,
        'interval': 0.1})
    spikedetector = nest.Create('spike_detector', params={
        'label': 'spikes',
        'withgid': True})
    for n in range(1, nnum+1):
        nest.Connect(voltmeter, (n,))
        nest.Connect((n,), spikedetector)

    # Connect stimulus to circuit
    ntnstatus('Creating stimulus for circuit')
    fire = firing_pattern[0]
    if args.stimulus_type == 'poisson' or args.stimulus_type == 'poisson_parrot':
        stimuli = nest.Create('poisson_generator', n=len(fibres))
        nest.SetStatus((stimuli[int(fire[0])],), params={
             'start': round(float(fire[1]), 1),
             'stop': round(float(fire[2]), 1),
             'rate': float(fire[3])})
    elif args.stimulus_type == 'dc':
        stimuli = nest.Create('dc_generator', n=len(fibres))
        nest.SetStatus((stimuli[int(fire[0])],), params={
             'start': round(float(fire[1]),1),
             'stop': round(float(fire[2]),1),
             'amplitude': float(fire[3])})
    elif args.stimulus_type == 'ac':
        stimuli = nest.Create('ac_generator', n=len(fibres))
        nest.SetStatus((stimuli[int(fire[0])],), params={
             'start': round(float(fire[1]), 1),
             'stop': round(float(fire[2]), 1),
             'amplitude': float(fire[3]),
             'frequency': args.stimulus_frequency})
    else:
        raise ValueError('Unexpected stimulus type: ' + str(args.stimulus_type))

    if args.stimulus_type == 'poisson_parrot':
        parrot_neurons = nest.Create('parrot_neuron', n=len(fibres))
        for fibre_index in range(len(fibres)):
            nest.Connect((stimuli[fibre_index],), (parrot_neurons[fibre_index],))
            fibre = fibres[fibre_index]
            for target in fibre:
                nest.Connect((parrot_neurons[fibre_index],), (target+1,))
    else:
        for fibre_index in range(len(fibres)):
            fibre = fibres[fibre_index]
            for target in fibre:
                nest.Connect((stimuli[fibre_index],), (target+1,))

    if args.noise_strength > 0:
        noise = nest.Create('noise_generator', params={'mean': 0., 'std': args.noise_strength})
        nest.Connect(noise, network)

    # Run a simulation
    ntnstatus("Running simulation of "+str(args.time)+"ms")
    nest.Simulate(float(args.time))
    spikes = nest.GetStatus(spikedetector)[0]['events']
    volts = nest.GetStatus(voltmeter)[0]['events']

    # Save results
    volt_save_path = root+'/simulations/'+str(args.save_path)+'_'+simulation_id+'-volts.npy'
    spikes_save_path = root+'/simulations/'+str(args.save_path)+'_'+simulation_id+'-spikes.npy'
    Path(spikes_save_path).parent.mkdir(parents=True, exist_ok=True)
    np.save(
        spikes_save_path,
        np.array([spikes['senders'], spikes['times']])
        )
    np.save(
        volt_save_path,
        np.array([volts['senders'], volts['times'], volts['V_m']])
        )

    ntnsubstatus('Simulation name: '+args.save_path + simulation_id)
    ntnsubstatus('Arguments used:')
    print(args)
    return simulation_id

if __name__ == '__main__':
    # Read arguments
    parser = argparse.ArgumentParser(
        description='N-simplex circuit on Nest simulator.',
        usage='python nsimplex.py'
    )

    parser.add_argument('--root', type=str, default='.', help='Root directory for importing and exporting files')
    parser.add_argument('--exc_adj', type=str, default='3simplex/3simplex',
                        help='Path to the circuit excitatory syn matrix, without .npy. Default 3simplex/3simplex')
    parser.add_argument('--inh_adj', type=str, default="",
                        help='Path to the circuit inhibitory syn matrix mask, without .npy.'
                             ' If empty, all synapses are excitatory. Default empty string.')
    parser.add_argument('--save_path', type=str, default='3simplex',
                        help='Path to save the results on.'
                             ' Will save both spikes and voltage traces using this as prefix.')
    parser.add_argument('--stimulus_targets', type=str, default="all",
                        help='Stimulus targets. \'sink\', \'source\', \'all\' are supported.'
                             ' Default is \'all\'.')
    parser.add_argument('--stimulus_type', type=str, default="poisson",
                        help='Stimulus type. \'dc\', \'ac\', \'poisson\', \'poisson_parrot\' are supported.'
                             ' Default is \'poisson\'.')
    parser.add_argument('--stimulus_frequency', type=float, default=1.,
                        help='Stimulus frequency in ac case. Unused for other stimuli.'
                             ' Default 1.')
    parser.add_argument('--noise_strength', type=float, default=3.,
                        help='Strength of noise. Default 3.')
    parser.add_argument('--stimulus_strength', type=int, default=40,
                        help='Strength of stimulus. Default 40.'
                             ' This is the poisson rate with the appropriate stimulus types.')
    parser.add_argument('--stimulus_length', type=int, default=100,
                        help='Length of stimulus, in milliseconds. Default 100.')
    parser.add_argument('--stimulus_start', type=int, default=5,
                        help='Start of stimulus, in milliseconds. Default 5')
    parser.add_argument('--time', type=int, default=200,
                        help='Length of the simulation in milliseconds. Must be an integer. Default is 200.')
    parser.add_argument('--threads', type=int, default=40,
                        help='Number of parallel thread to use. Must be an integer. Default is 40.')
    parser.add_argument('--id_prefix', type=str, default='',
                        help='Prefix for sim ID. Default empty string.')
    parser.add_argument('--p_transmit', type=float, default=1,
                        help='Synapse transmission probability. Default 1.')
    parser.add_argument('--seed', type=int, default=0, help='Simulation seed. Default 0.')

    keyword_args = parser.parse_args()
    simulate(keyword_args)


class Solution:
    def minimumMountainRemovals(self, nums: List[int]) -> int:
        n = len(nums)
        dpi = [1 for _ in range(n)]
        dpd = [1 for _ in range(n)]

        for i in range(n):
            for j in range(i, -1, -1):
                if nums[j] < nums[i]:
                    dpi[i] = max(dpi[i], dpi[j]+1)

        for i in range(n-1, -1, -1):
            for j in range(i, n):
                if nums[j] < nums[i]:
                    dpd[i] = max(dpd[i], dpd[j]+1)

        res=0
        for i in range(n):
            if dpi[i] == 1 or dpd[i] == 1:
                continue
            res = max(res, dpi[i]+dpd[i]-1)

        return n-res


import math
import random
from queue import Queue
from typing import Tuple

IDLE = 0
BUSY = 1

time_next_event = {
    "Arrival": 0.0,
    "Departure ": 0.0
}

SIMTIME_LIMIT = 480
clock = 0.0
delay_in_q = 0.0
total_utilities = None
server_status = IDLE
customers_in_q = 0.0
arrivals_queue = Queue()
attended_customers = 0

def random_customer() -> tuple[int, int]:
    u = random.uniform(0.0, 1.0)
    if u <= 0.2:
        return 1, 0
    elif 0.2 < u <= 0.7:
        return 2, 2500
    elif u > 0.7:
        return 3, 4500

def random_exponential(given_mean):
    u = random.uniform(0.0, 1.0)

    while u == 0.0:
        u = random.uniform(0.0, 1.0)

    return -given_mean * math.log(u)

def service_time_a_customer():
    u = random.uniform(0.0, 1.0)
    return 3.1 + (u * (3.4 - 3.1))

def departure_selector(type_customer):
    if type_customer == 1:
        service_time = 1.5
    elif type_customer == 2:
        service_time = service_time_a_customer()
    else:
        service_time = random_exponential(7)

    return service_time

def initialize():
    global clock, total_utilities, delay_in_q, time_next_event, server_status, customers_in_q
    clock = 0.0

    total_utilities = 0.0
    delay_in_q = 0.0
    server_status = 0
    customers_in_q = 0

    time_next_event["Arrival"] = clock + random_exponential(3)
    time_next_event["Departure"] = float('inf')

def arrival():
    global customers_in_q, time_next_event, clock, delay_in_q, server_status, BUSY, total_utilities
    time_next_event["Arrival"] = random_exponential(1.5)  # Next arrival scheduled

    if server_status == BUSY:
        customers_in_q += 1
        arrivals_queue.put(clock)
    else:
        delay = 0.0
        delay_in_q += delay

        server_status = BUSY
        customer_type, utility = random_customer()
        total_utilities += utility
        time_next_event["Departure"] = departure_selector(customer_type)

def departure():
    global customers_in_q, clock, IDLE, delay_in_q, attended_customers, server_status, total_utilities
    if customers_in_q == 0:
        server_status = IDLE
        time_next_event["Departure"] = float('inf')
    else:
        delay = clock - arrivals_queue.get()
        customers_in_q -= 1
        delay_in_q += delay
        attended_customers += 1
        customer_type, utility = random_customer()
        total_utilities += utility
        time_next_event["Departure"] = departure_selector(customer_type)

def timing():
    global clock, time_next_event
    event = min(time_next_event["Arrival"], time_next_event["Departure"])
    clock += event
    return event

def new_timing():
    global clock, time_next_event
    event = time_next_event["Departure"]
    clock += event
    return event

def main_routine():
    global SIMTIME_LIMIT, time_next_event, clock, customers_in_q, delay_in_q
    initialize()
    while clock < SIMTIME_LIMIT:
        time = timing()
        if time == time_next_event["Arrival"]:
            arrival()
        if time == time_next_event["Departure"]:
            departure()

    time_next_event["Arrival"] = float('inf')

    while customers_in_q > 0:
        new_time = new_timing()
        if new_time == time_next_event["Departure"]:
            departure()
        departure()

def report():
    global delay_in_q, attended_customers, clock, total_utilities, time_next_event
    print("Total utilities in this day: " + str(total_utilities))
    print("Attended Customers " + str(attended_customers))
    print("Average delay in queue: " + str((delay_in_q / attended_customers) / 60))
    print("Employee hour of departure: " + str(clock / 60))

main_routine()
report()


def sum_consecutives(s):
    p = s[:1]   # final list
    cur = s[0]  # keep track of last seen value

    for i in s[1:]:
        if i == cur:
            p[-1] += i
        else:
            p.append(i)
            cur = i

    return p