#python AssemblyLanguageCompiler/Compiler.py

import pickle
import linecache
import sys

InstructionSet = {"LDD" : 1,"LDM" : 2,"LDI" : 3,"LDX" : 4,"LDR" : 5,"MOV" : 6,"STO" : 7,"STX" : 8,"STI" : 9, "ADD" : 10, "SUB" : 11,"INC" : 12,"DEC" : 13,"JMP" : 14,"CMP" : 15,"CMI" : 16,"JPE" : 17,"JPN" : 18,"IN" : 19,"OUT" : 20,"AND" : 21,"DEF" : 22,"END" : 100}
LookupTable = {}

class Source:
    def __init__(self,dir):
        self._dir = dir
        self._linePntr = 0
        self.maxLinePntr = 0
        self._ACCVal  = 0
        self._IX = 0
        self._CMPFlag = False

    def Read(self):
        EOF = False

        while EOF == False:
            line = ""
            self._linePntr += 1
            line = linecache.getline(self._dir,self._linePntr)
            print(line)
            if line == "END":
                self.maxLinePntr = self._linePntr
                EOF = True

            #Syntax Analysis
            infixLine = []
            lexemes = line.split(" ")
            for lexeme in lexemes:
                try:
                    infixLine.append(InstructionSet[lexeme.replace("\n","")])
                except:
                    try:
                        LookupTable[lexeme.replace("\n","").replace("@","")]
                        infixLine.append(lexeme.replace("\n","").replace("@",""))
                    except:
                        if "#" in lexeme:
                            infixLine.append(lexeme.replace("\n",""))     #If value passed rather than address
                        elif "@" in lexeme:
                            infixLine.append(lexeme.replace("@","").replace("\n",""))
                        elif lexeme.replace("\n","") == "ACC" or lexeme.replace("\n","") == "IX":
                            infixLine.append(lexeme.replace("\n",""))
                        else:
                            print("Syntax Error encountered on line {0}.".format(self._linePntr))
                            sys.exit()
                      #return the address of the variable and append to infix line @ symbol used to represent address

            #Execution
            if infixLine[0] == 1:   #LDD
                self._ACCVal = LookupTable[infixLine[1]]
            elif infixLine[0] == 2: #LDM
                infixLine[1] = infixLine[1].replace("#","")
                self._ACCVal = int(infixLine[1])
            elif infixLine[0] == 3: #LDI
                address = str(LookupTable[infixLine[1]])
                self._ACCVal = LookupTable[address]
            elif infixLine[0] == 4: #LDX
                    self._ACCVal = int(LookupTable[str(int(infixLine[1]) + int(self._IX))])
            elif infixLine[0] == 5: #LDR
                self._IX = int(infixLine[1])
            elif infixLine[0] == 6: #MOV
                self._IX = self._ACCVal
                self._ACCVal = 0
            elif infixLine[0] == 7: #STO
                LookupTable[infixLine[1]] = self._ACCVal
            elif infixLine[0] == 8: #STX
                        LookupTable[str(int(infixLine[1]) + int(self._IX))] = self._ACCVal
            elif infixLine[0] == 9: #STI
                        address = str(LookupTable[infixLine[1]])
                        LookupTable[address] = self._ACCVal
            elif infixLine[0] == 10: #ADD
                    if '#' in infixLine [1]:
                        self._ACCVal += int(infixLine[1].strip('#'))
                    else:
                        self._ACCVal += LookupTable[infixLine[1]]
            elif infixLine[0] == 11: #SUB
                    if '#' in infixLine [1]:
                        self._ACCVal -= int(infixLine[1].strip('#'))
                    else:
                        self._ACCVal -= LookupTable[infixLine[1]]
            elif infixLine[0] == 12: #INC
                if infixLine[1] == "ACC":
                    self._ACCVal += 1
                elif infixLine[1] == "IX":
                    self._IX += 1
            elif infixLine[0] == 13: #DEC
                if infixLine[1] == "ACC":
                    self._ACCVal -= 1
                elif infixLine[1] == "IX":
                    self._IX -= 1
            elif infixLine[0] == 14: #JMP
                        self._linePntr = LookupTable[infixLine[1]] - 1
            elif infixLine[0] == 15: #CMP
                    if '#' in infixLine[1]:
                        if int(infixLine[1].strip('#')) == self._ACCVal:
                            self._CMPFlag = True
                    else:
                        value = LookupTable[infixLine[1]]
                        if value == self._ACCVal:
                            self._CMPFlag = True
                        else:
                            self._CMPFlag = False
            elif infixLine[0] == 16: #CMI
                        address = str(LookupTable[infixLine[1]])
                        value = LookupTable[address]
                        if value == self._ACCVal:
                            self._CMPFlag = True
                        else:
                            self._CMPFlag = False
            elif infixLine[0] == 17: #JPE
                if self._CMPFlag:
                    self._linePntr = LookupTable[infixLine[1]] -1
                    self._CMPFlag = False
            elif infixLine[0] == 18: #JPN
                if self._CMPFlag == False:
                    self._linePntr = LookupTable[infixLine[1]] - 1
                    self._CMPFlag  = False
            elif infixLine[0] == 19: #IN
                self._ACCVal = ord(input())
            elif infixLine[0] == 20: #OUT
                print(ascii(self._ACCVal))
            elif infixLine[0] == 21: #AND
                if '#' in infixLine[1]:
                    self._ACCVal = self._ACCVal & int(infixLine[1].strip('#'))
                else:
                        value = LookupTable[infixLine[1]]
                        self._ACCVal = self._ACCVal & value
            elif infixLine[0] == 22: #DEF
                LookupTable[infixLine[1]] = None

            elif infixLine[0] == 100: #END
                sys.exit()

source = Source("Chapter28/1C")
source.Read()


from PyQt5 import QtCore, QtGui, QtWidgets
from pyqtgraph import PlotWidget , GraphicsLayoutWidget
from scipy import signal
import pyqtgraph
import numpy as np
from lib.FT import fourierTransform,soundfileUtility
import random
import re

class Slider(QtWidgets.QSlider) :
    def __init__(self,objectName,parent) :
        super(Slider,self).__init__()
        self.setCursor(QtGui.QCursor(QtCore.Qt.ClosedHandCursor))
        self.setMinimum(0)
        self.setMaximum(5)
        self.setSliderPosition(1)
        self.setOrientation(QtCore.Qt.Vertical)
        self.setTickPosition(QtWidgets.QSlider.TicksAbove)
        self.setTickInterval(1)
        self.setObjectName(objectName)
        self.setParent(parent)

class Tabs(QtWidgets.QTabWidget) :
    def __init__ (self) :
        super(Tabs, self).__init__()
        self.numberOfTabs = 0
    def add_new_viewer(self,timeData,voltsData) :
        self.numberOfTabs += 1
        centralwidget = centralWidget(timeData,voltsData)
        self.addTab(centralwidget, "new tab")
        self.setCurrentWidget(centralwidget)
        self.setCurrentIndex(self.numberOfTabs)
    def tabRemoved(self, index) :
        self.numberOfTabs = self.numberOfTabs - 1

# we are going to put all of our widgets inside the tab wrapped by a QWidget
class centralWidget(QtWidgets.QWidget) :
    def __init__(self,timeData,voltsData) :
        super(centralWidget,self).__init__()
        # first generating the widgets of the tab
        self.generateUiWidgets()
        # initializing important variables for plotting
        self.timeData = timeData
        self.originalVoltsData = voltsData
        self.editedVoltsData   = voltsData
        self.plot = None # original Plot
        self.plot1 = None # plot After Editing

        self.xRangeStack = []
        self.yRangeStack = []

        self.sampleTime = timeData[1] - timeData[0]
        yrange = voltsData[len(voltsData) - 1] - voltsData[0]
        self.scrollStep_x = 100 * self.sampleTime
        self.scrollStep_y = yrange / 10

        # sliders values
        for i in range(1,11) :
            setattr(self, "_value"+str(i), 1) # self._value[1-10] = 1

        #Pallete of spectrogram *viridis as deafult
        self.RGB_Pallete_1 = (0, 182, 188, 255)
        self.RGB_Pallete_2 = (246, 111, 0, 255)
        self.RGB_Pallete_3 = (75, 0, 113, 255)

        #set labels text
        ft = fourierTransform(self.originalVoltsData, int(1 / self.sampleTime))
        ranges = ft.rangesOfFrequancy

        for i in range(10) :
            getattr(getattr(self,"label"+str(i+1)),"setText")(str(ranges[i][0] / 1000 ) + " Khz : \n" + str(ranges[i][1] / 1000 ) + " Khz")

        self.HorizontalLabel1.setText("Set Minimun Frequancy")
        self.HorizontalLabel2.setText("Set Maximum Frequancy")

        # values of range of spectrogram
        self.minFreqOfSpectrogram = 0
        self.maxFreqOfSpectrogram = ranges[-1][1]

        self.horizontalSlider1.setMinimum(self.minFreqOfSpectrogram)
        self.horizontalSlider1.setMaximum(self.maxFreqOfSpectrogram)
        self.horizontalSlider1.setTickInterval(self.maxFreqOfSpectrogram / 10 )

        self.horizontalSlider2.setMinimum(self.minFreqOfSpectrogram)
        self.horizontalSlider2.setMaximum(self.maxFreqOfSpectrogram)
        self.horizontalSlider2.setSliderPosition(self.maxFreqOfSpectrogram)
        self.horizontalSlider2.setTickInterval(self.maxFreqOfSpectrogram / 10)

        self.SpectrogramViewer.clear()
        self.drawSpectrogram()

        self.xRangeOfSignal = None # [from , to]
        self.yRangeOfSignal = None

        #start plotting the data
        self.startPlotting()
        # get range of view of the plots
        self.xRangeOfSignal = [0.0,list(self.timeData)[-1]] # [from , to]
        self.yRangeOfSignal = self.plot.viewRange()[1]

    def startPlotting(self) :
        # plot original signal
        self.plot = self.OriginalSignalViewer.addPlot()
        self.plot.plot(self.timeData,self.originalVoltsData)
        # plot data After Editing
        self.plot1 = self.EditedSignalViewer.addPlot()
        self.plot1.plot(self.timeData,self.editedVoltsData)
        # range edit
        self.plot.setXRange(0.0,list(self.timeData)[-1],0)
        self.plot1.setXRange(0.0,list(self.timeData)[-1],0)

    def minSliderOfSpectrogram(self,value) :
        self.minFreqOfSpectrogram = value
        self.SpectrogramViewer.clear()
        self.drawSpectrogram()

    def maxSliderOfSpectrogram(self,value) :
        self.maxFreqOfSpectrogram = value
        self.SpectrogramViewer.clear()
        self.drawSpectrogram()

    def drawSpectrogram(self,minFreq = 1,maxFreq = 1) :
        minFreq_Slider = self.minFreqOfSpectrogram
        maxFreq_Slider = self.maxFreqOfSpectrogram
        freq = 1 / self.sampleTime
        ft = fourierTransform(self.editedVoltsData,int(freq))
        ft.deleteRangeOfFrequancy(0,minFreq_Slider)
        ft.deleteRangeOfFrequancy(maxFreq_Slider,int(freq/2))
        realsAfterEdit = ft.fn_InverceFourier(ft.data_fft)
        frequancyArr, timeArr, Sxx = signal.spectrogram(np.array(realsAfterEdit), freq)
        pyqtgraph.setConfigOptions(imageAxisOrder='row-major')

        win = self.SpectrogramViewer
        p1 = win.addPlot()

        img = pyqtgraph.ImageItem()
        p1.addItem(img)
        hist = pyqtgraph.HistogramLUTItem()
        hist.setImageItem(img)
        win.addItem(hist)
        hist.setLevels(np.min(Sxx), np.max(Sxx))
        hist.gradient.restoreState({
            'mode':
            'rgb',
            'ticks': [(0.5, self.RGB_Pallete_1), (1.0, self.RGB_Pallete_2),
                      (0.0, self.RGB_Pallete_3)]
        })
        img.setImage(Sxx)
        img.scale(timeArr[-1] / np.size(Sxx, axis=1), frequancyArr[-1] / np.size(Sxx, axis=0))
        p1.setLimits(xMin=0, xMax=timeArr[-1], yMin=0, yMax=frequancyArr[-1])
        p1.setLabel('bottom', "Time", units='s')
        p1.setLabel('left', "Frequency", units='Hz')

    def generateUiWidgets(self) :
        font = QtGui.QFont()
        font.setFamily("Arial Unicode MS")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(50)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.setFont(font)
        self.setTabletTracking(False)
        self.gridLayout_6 = QtWidgets.QGridLayout(self)
        self.gridLayout_6.setObjectName("gridLayout_6")
        self.gridLayout_4 = QtWidgets.QGridLayout()
        self.gridLayout_4.setObjectName("gridLayout_4")
        self.SpectrogramGroupBox = QtWidgets.QGroupBox(self)
        font = QtGui.QFont()
        font.setFamily("Arial Unicode MS")
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.SpectrogramGroupBox.setFont(font)
        self.SpectrogramGroupBox.setStyleSheet("border-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));")
        self.SpectrogramGroupBox.setObjectName("SpectrogramGroupBox")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.SpectrogramGroupBox)
        self.gridLayout_2.setObjectName("gridLayout_2")

        self.horizontalSlider2 = QtWidgets.QSlider(self.SpectrogramGroupBox)
        font = QtGui.QFont()
        font.setPointSize(7)
        self.horizontalSlider2.setFont(font)
        self.horizontalSlider2.setCursor(QtGui.QCursor(QtCore.Qt.ClosedHandCursor))
        self.horizontalSlider2.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider2.setTickPosition(QtWidgets.QSlider.TicksBelow)
        self.horizontalSlider2.setTickInterval(1)
        self.horizontalSlider2.setObjectName("horizontalSlider2")
        self.gridLayout_2.addWidget(self.horizontalSlider2, 3, 0, 1, 1)

        self.SpectrogramViewer = GraphicsLayoutWidget(self.SpectrogramGroupBox)
        self.SpectrogramViewer.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.SpectrogramViewer.setStyleSheet("background-color:rgb(0,0,0)")
        self.SpectrogramViewer.setObjectName("SpectrogramViewer")
        self.gridLayout_2.addWidget(self.SpectrogramViewer, 0, 0, 1, 1)

        self.horizontalSlider1 = QtWidgets.QSlider(self.SpectrogramGroupBox)
        self.horizontalSlider1.setCursor(QtGui.QCursor(QtCore.Qt.ClosedHandCursor))
        self.horizontalSlider1.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider1.setTickPosition(QtWidgets.QSlider.TicksBelow)
        self.horizontalSlider1.setTickInterval(1)
        self.horizontalSlider1.setObjectName("horizontalSlider1")
        self.gridLayout_2.addWidget(self.horizontalSlider1, 1, 0, 1, 1)
        self.HorizontalLabel1 = QtWidgets.QLabel(self.SpectrogramGroupBox)
        font = QtGui.QFont()
        font.setPointSize(7)
        self.HorizontalLabel1.setFont(font)
        self.HorizontalLabel1.setAlignment(QtCore.Qt.AlignCenter)
        self.HorizontalLabel1.setObjectName("HorizontalLabel1")
        self.gridLayout_2.addWidget(self.HorizontalLabel1, 2, 0, 1, 1)
        self.HorizontalLabel2 = QtWidgets.QLabel(self.SpectrogramGroupBox)
        self.HorizontalLabel2.setAlignment(QtCore.Qt.AlignCenter)
        self.HorizontalLabel2.setObjectName("HorizontalLabel2")
        self.gridLayout_2.addWidget(self.HorizontalLabel2, 4, 0, 1, 1)

        self.gridLayout_4.addWidget(self.SpectrogramGroupBox, 0, 1, 3, 1)
        self.OriginalSignalGroupbox = QtWidgets.QGroupBox(self)
        font = QtGui.QFont()
        font.setFamily("Arial Unicode MS")
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.OriginalSignalGroupbox.setFont(font)
        self.OriginalSignalGroupbox.setObjectName("OriginalSignalGroupbox")
        self.gridLayout_5 = QtWidgets.QGridLayout(self.OriginalSignalGroupbox)
        self.gridLayout_5.setObjectName("gridLayout_5")
        self.OriginalSignalViewer = GraphicsLayoutWidget(self.OriginalSignalGroupbox)
        self.OriginalSignalViewer.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.OriginalSignalViewer.setStyleSheet("background-color: rgb(0, 0, 0);")
        self.OriginalSignalViewer.setObjectName("OriginalSignalViewer")
        self.gridLayout_5.addWidget(self.OriginalSignalViewer, 0, 0, 1, 1)
        self.gridLayout_4.addWidget(self.OriginalSignalGroupbox, 0, 0, 1, 1)
        self.EditedSignalGroupBox = QtWidgets.QGroupBox(self)
        font = QtGui.QFont()
        font.setFamily("Arial Unicode MS")
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.EditedSignalGroupBox.setFont(font)
        self.EditedSignalGroupBox.setObjectName("EditedSignalGroupBox")
        self.gridLayout_3 = QtWidgets.QGridLayout(self.EditedSignalGroupBox)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.EditedSignalViewer = GraphicsLayoutWidget(self.EditedSignalGroupBox)
        self.EditedSignalViewer.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.EditedSignalViewer.setStyleSheet("background-color:rgb(0,0,0)")
        self.EditedSignalViewer.setObjectName("EditedSignalViewer")
        self.gridLayout_3.addWidget(self.EditedSignalViewer, 0, 0, 1, 1)
        self.gridLayout_4.addWidget(self.EditedSignalGroupBox, 2, 0, 1, 1)
        self.SignalEditorGroupBox = QtWidgets.QGroupBox(self)
        font = QtGui.QFont()
        font.setFamily("Arial Unicode MS")
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.SignalEditorGroupBox.setFont(font)
        self.SignalEditorGroupBox.setObjectName("SignalEditorGroupBox")
        self.gridLayout = QtWidgets.QGridLayout(self.SignalEditorGroupBox)
        self.gridLayout.setObjectName("gridLayout")
        self.gridLayout_7 = QtWidgets.QGridLayout()
        self.gridLayout_7.setObjectName("gridLayout_7")

        # add 10 labels for 10 sliders
        for i in range(10) :
            setattr(self, "label" + str(i+1), QtWidgets.QLabel(self.SignalEditorGroupBox)) # self.label[1-10] = QtWidgets.QLabel(self.SignalEditorGroupBox)
            font = QtGui.QFont()
            font.setPointSize(5)
            font.setBold(False)
            font.setWeight(50)
            getattr(getattr(self,"label" + str(i + 1)),"setFont")(font) #self.label1.setFont(font)
            getattr(getattr(self,"label" + str(i + 1)),"setObjectName")("label"+str(i+1)) #self.label[1-10].setObjectName("label[1-10]")
            getattr(getattr(self,"gridLayout_7"),"addWidget")(getattr(self, "label" + str(i + 1)), 0, i, 1, 1) #self.gridLayout_7.addWidget(self.label1, 0, 0, 1, 1)

        self.gridLayout.addLayout(self.gridLayout_7, 1, 0, 1, 10)

        # add 10 sliders to gui
        for i in range(10) :
            setattr(self, "slider"+str(i+1), Slider(parent=self.SignalEditorGroupBox,objectName="Slider"+str(i+1)))
            Slid = getattr(self, "slider" + str(i + 1))
            getattr(getattr(self,"gridLayout"), "addWidget")(Slid, 0, i, 1, 1)
            # link slider to the trigger function
            Slid.valueChanged.connect(lambda v : self.fn_sliderValue(v))

        self.gridLayout_4.addWidget(self.SignalEditorGroupBox, 1, 0, 1, 1)
        self.gridLayout_6.addLayout(self.gridLayout_4, 0, 0, 1, 1)

        self.horizontalSlider1.valueChanged[int].connect(self.minSliderOfSpectrogram)
        self.horizontalSlider2.valueChanged[int].connect(self.maxSliderOfSpectrogram)
        QtCore.QMetaObject.connectSlotsByName(self)

    def fn_sliderValue(self,value):
        objectName = str(self.sender().objectName())
        sliderNumber = re.search("[0-9]+",objectName).group()
        setattr(self,"_value" + sliderNumber,value)
        self.process()

    def process(self): #(freq,complex_data,reals,time,np.abs(complex_data))
        ft = fourierTransform(list(self.originalVoltsData).copy(), 1 / self.sampleTime)
        complex_data = ft.gain(self._value1,self._value2,self._value3,self._value4,
        self._value5,self._value6,self._value7,self._value8,self._value9,self._value10)
        reals = ft.fn_InverceFourier(complex_data)
        #update Plot
        self.EditedSignalViewer.clear()
        self.plot1 = self.EditedSignalViewer.addPlot()
        self.plot1.plot(self.timeData,reals)
        self.plot1.setXRange(self.xRangeOfSignal[0],self.xRangeOfSignal[1],0)
        self.plot1.setYRange(self.yRangeOfSignal[0],self.yRangeOfSignal[1],0)
        self.editedVoltsData = np.array(reals)
        # update spectrogram
        self.SpectrogramViewer.clear()
        self.drawSpectrogram()

    def playSound(self) :
        ft = fourierTransform(list(self.editedVoltsData).copy(), 1 / self.sampleTime)
        complex_data = ft.gain(self._value1,self._value2,self._value3,self._value4,
        self._value5,self._value6,self._value7,self._value8,self._value9,self._value10)
        reals = ft.fn_InverceFourier(complex_data)
        sound = soundfileUtility()
        sound.fn_CreateSoundFile(list(reals),int(1 / self.sampleTime))
        sound.fn_PlaySoundFile()


import json
import threading
from email.mime.base import MIMEBase
import datetime as dt
import icalendar
import pytz
import uuid
from django.core.mail import EmailMultiAlternatives
from wellbeing_django_framework.settings import EMAIL_HOST_USER

def send_appointment(attendee_email, organiser_email, subj, body, location, start_time, end_time):
    # Timezone to use for our dates - change as needed
    tz = pytz.timezone("Asia/Shanghai")
    start = start_time
    # Build the event itself
    cal = icalendar.Calendar()
    cal.add('prodid', '-//My calendar application//example.com//')
    cal.add('version', '2.0')
    cal.add('method', "REQUEST")
    event = icalendar.Event()
    event.add('attendee', attendee_email)
    event.add('organizer', organiser_email)
    event.add('status', "confirmed")
    event.add('category', "Event")
    event.add('summary', subj)
    event.add('description', body)
    event.add('location', location)
    event.add('dtstart', start)
    event.add('dtend', end_time)
    event.add('dtstamp', tz.localize(dt.datetime.combine(dt.datetime.now(tz), dt.time(6, 0, 0))))
    event['uid'] = str(uuid.uuid4())  # Generate some unique ID
    event.add('priority', 5)
    event.add('sequence', 1)
    event.add('created', tz.localize(dt.datetime.now()))

    # Add a reminder
    alarm = icalendar.Alarm()
    alarm.add("action", "DISPLAY")
    alarm.add('description', "Reminder")
    # The only way to convince Outlook to do it correctly
    alarm.add("TRIGGER;RELATED=START", "-PT{0}H".format(0.25))
    event.add_component(alarm)
    cal.add_component(event)

    # Build the email message and attach the event to it
    msg = EmailMultiAlternatives(subj, body, organiser_email, [attendee_email])

    filename = "invite.ics"
    part = MIMEBase('text', "calendar", method="REQUEST", name=filename)
    part.set_payload(cal.to_ical())
    part.add_header('Content-Description', filename)
    part.add_header("Content-class", "urn:content-classes:calendarmessage")
    part.add_header("Filename", filename)
    part.add_header("Path", filename)
    msg.attach(part)

    # Send the email out
    msg.send()

class SendAppointmentsThread(threading.Thread):
    def __init__(self, schedule, attendee_email, location):
        threading.Thread.__init__(self)
        self.threadID = "Thread-Send-Appointments"
        self.schedule = schedule
        self.attendee_email = attendee_email
        self.location = location
        self.organiser_email = EMAIL_HOST_USER
        self.subj = 'Relax time'
        self.body = """It is time to relax yourself!

Click {} to start exercise!
                        """.format(self.location)

    def run(self):
        tz = pytz.timezone("Asia/Shanghai")
        schedules = json.loads(self.schedule)
        i = 1
        for s in schedules:
            print(f'start to send {i} message')
            start_time = s["start_time"]
            end_time = s["end_time"]
            start_time = dt.datetime.strptime(start_time, '%Y-%m-%dT%H:%M:%S.%fZ')
            end_time = dt.datetime.strptime(end_time, '%Y-%m-%dT%H:%M:%S.%fZ')
            start_time = tz.localize(start_time)
            end_time = tz.localize(end_time)
            # start_time = tz.localize(dt.datetime.now())
            # end_time = tz.localize(dt.datetime.combine(dt.datetime.now(tz), dt.time(start_time.hour + int((start_time.minute + 30)/60), (start_time.minute + 30) % 60, 0)))
            send_appointment(self.attendee_email, self.organiser_email, self.subj, self.body, self.location, start_time,
                             end_time)
            print(f'finish sending {i} message')
            i = i + 1


'''
Author: locchuong
Updated: 23/9/22
Description:
	This is the first mission of robot intergate A.I,
	Greeting people, there are
	(^ ^) = known-face
	(o o) = unknown-face
'''
# FACE RECOGNTION
import os
import cv2
import argparse
import face_recognition
import numpy as np
import sqlite3

# font
font = cv2.FONT_HERSHEY_DUPLEX # cv2.FONT_HERSHEY_SIMPLEX
zoom = 2 # zoom frame
# define color
red = (0,0,255)
green = (0,255,0)
blue = (255,0,0)
white = (255,255,255)
black = (0,0,0)

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
known_face_encodings = []
known_face_names = []

# count iteration
count = 0

# init parser
parser  = argparse.ArgumentParser(description = 'greeting mission')
# add argument to parser
parser.add_argument('-d','--display',action = 'store_true',help = 'display frame option')
# create arguments
args = parser.parse_args()

# Create the connection to the database and the cursor
conn = sqlite3.connect("./robot/site.db") # ./Jetson-Nano you must define this conn before add in into default keyword arg

def read_faces():
	files = os.listdir("./faces")
	for file in files:
		file = file.split(".")
		file_name = file[0]
		file_format = file[-1]
		if file_format in ["jpg","jpeg","png"]:
			image_file = os.path.join("./faces/",file_name+"."+file_format)
			face = face_recognition.load_image_file(image_file)
			encoding = face_recognition.face_encodings(face)[0] # return a list, take the first element
			known_face_names.append(file_name)
			known_face_encodings.append(encoding)
	return known_face_names,known_face_encodings

# Create a api object for connecting to database and modify it
class api_db():

	def __init__(self,conn =conn):
		self.conn = conn # the connection to site database

	def read_emotion(self):
		'''
		This function read the command value in database and return it
		'''
		c = self.conn.cursor()
		c.execute(f"SELECT *FROM robot WHERE id = 1")
		data = c.fetchone()
		emotion = data[4]
		itype = data[6]
		self.conn.commit()
		return emotion,itype

	def write_emotion(self,emotion='neutral'):
		'''
		This function for write the emotion to database, must set itype = emo
		'''
		c = self.conn.cursor()
		c.execute("""
			UPDATE robot
			SET itype = ?, emotion = ?
			WHERE id = 1
			""",('emo',emotion))
		self.conn.commit()

	def kill_gpio(self):
		'''
		This function for close gpio program
		'''
		c = self.conn.cursor()
		c.execute(f"""
			UPDATE robot
			SET command = 'kill'
			WHERE id = 1
			""")
		self.conn.commit()

if __name__ == "__main__":

	# init emoled database api object
	api = api_db()

	emotion,itype = api.read_emotion()

	print(f"[MISSION4]: starting values itype = {itype}, emotion = {emotion}")

	# flip bool for processing frame
	process_this_frame = True

	print("[MISSION4]: reading faces...")
	known_face_names,known_face_encodings = read_faces()

	cap = cv2.VideoCapture(3)
	ret,frame = cap.read()

	width = frame.shape[1]
	height = frame.shape[0]
	dim = (width*2, height*2)

	if not ret:
		print("[MISSION4]: FAILED (Can not connect to camera)")

	print("[MISSION4]: Doing...")

	while ret:

		ret,frame = cap.read()

		if process_this_frame:

			small_frame = cv2.resize(frame,(0,0),fx=0.25,fy=0.25)

			rgb_small_frame = small_frame[:,:,::-1]

			face_locations = face_recognition.face_locations(rgb_small_frame)

			face_encodings = face_recognition.face_encodings(rgb_small_frame,face_locations)

			face_names = []

			for face_encoding in face_encodings:

				name = "Unknown"

				matches = face_recognition.compare_faces(known_face_encodings,face_encoding)

				face_distances = face_recognition.face_distance(known_face_encodings,face_encoding)

				best_match_index = np.argmin(face_distances)

				if matches[best_match_index]:
					name = known_face_names[best_match_index]

				face_names.append(name)

		process_this_frame = not process_this_frame

		for (top,right,bottom,left),name in zip(face_locations,face_names):
			top *= 4
			bottom *= 4
			right *= 4
			left *= 4

			if name == "Unknown":
				cv2.rectangle(frame,(left,top),(right,bottom),red,2)
				cv2.rectangle(frame,(left,bottom -35), (right,bottom),red,cv2.FILLED)
				cv2.putText(frame,name,(left+6,bottom-6),font,1.0,white,1)
				api.write_emotion(emotion="angry")
			else:
				cv2.rectangle(frame,(left,top),(right,bottom),green,2)
				cv2.rectangle(frame,(left,bottom -35), (right,bottom),green,cv2.FILLED)
				cv2.putText(frame,name,(left+6,bottom-6),font,1.0,black,1)
				api.write_emotion(emotion="smile")

		frame = cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)

		if args.display: cv2.imshow('frame',frame)

		# read emotion
		emotion,itype = api.read_emotion()

		if count % 100 == 0 and emotion != "neutral":
			count = 0
			api.write_emotion(emotion = "neutral")

		count += 1
		#print(count)

		if cv2.waitKey(1) == 27:
			break

	cv2.destroyAllWindows()
	cap.release()
	api.kill_gpio()
	print("[MISSION4]: DONE")
	exit()


"""Explore auction-verification data

Code snippets for interactively (e.g., in an IDE) exploring the data, including simple predictions.
Not intended for execution from a console.
Code was written based on the small-domain verification dataset. The large-domain verification
dataset might behave differently (i.e., violate some assumptions expressed in comments below).
"""

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import sklearn.model_selection
import sklearn.tree
import tqdm

dataset = pd.read_csv('data/auction_verification.csv')
# Type is not properly recognized for all columns:
float_cols = dataset.dtypes[dataset.dtypes == 'float'].index
dataset[float_cols] = dataset[float_cols].astype('Int64')

# -----Exploration-----

# ---Basic properties----
summary_table = dataset.describe()
dataset.nunique()
dataset.isna().sum()

# ---Distribution of individual features---
dataset['allocation.revenue'].value_counts()
dataset['verification.is_final'].value_counts()
dataset['verification.result'].value_counts()
dataset['verification.time'].plot.hist()

# ---Relationship between features---
# Number of capacity combinations:
dataset.groupby([x for x in dataset.columns if 'capacity' in x]).ngroups
# In first iteration of each verification sequence, capacity is always same:
dataset.loc[dataset['id.iteration'] == 1, [x for x in dataset.columns if 'capacity' in x]].nunique()
# In last iteration of each verification sequence, five of six products are assigned (the last
# product would affect capacity only in next iteration):
(dataset.groupby('id.product_permutation').last()[[x for x in dataset.columns if 'capacity' in x]].sum(axis='columns') == 5).all()
# Allocation is set only in last round:
(dataset['allocation.p1.price'].notna() == dataset['verification.is_final']).all()
(dataset['allocation.revenue'].notna() == dataset['verification.is_final']).all()
# Number of wins is lower than the capacities:
initial_capacities = dataset.loc[0, [x for x in dataset.columns if 'capacity' in x]]
for i in range(1, 5):
    print(((dataset[[x for x in dataset.columns if 'allocation' in x and 'winner' in x]] == i).sum(
        axis='columns') <= initial_capacities.iloc[i - 1]).all())
# Check maxmimum prices for each product and bidder (might be NaN if bidder never wins product):
for product_id in range(1, 7):
    for bidder_id in range(1, 5):
        print(f'b{bidder_id}.p{product_id}.max_price:',
              dataset.loc[dataset[f'allocation.p{product_id}.winner'] == bidder_id,
                          f'allocation.p{product_id}.price'].max())
    print()
# For each permutation of products and for each product within the permutation, there are two
# positive results (determine price and determine winner):
(dataset.groupby(['id.product_permutation', 'property.product'])['verification.result'].sum() == 2).all()
# If last iteration, result is true
pd.crosstab(dataset['verification.is_final'], dataset['verification.result'])
# If bidder 3 wins, result is true (because there is no bidder left who could win instead; bidder 4 never wins):
pd.crosstab(dataset['property.winner'] == 3, dataset['verification.result'])
# Correlation:
plt.figure(figsize=(7, 7))
sns.heatmap(dataset[dataset.columns[dataset.notna().all()]].corr(), fmt='.2f', vmin=-1, vmax=1,
            cmap='PRGn', annot=True, square=True, cbar=False)

# ----Regression-----

features = [x for x in dataset.columns if x.startswith('pro') and x != 'property.formula']
# Different splits possible, e.g., random, based on permutations or based on capacity settings:
# cv = sklearn.model_selection.KFold(n_splits=10, shuffle=True, random_state=25)
# splits = cv.split(X=dataset)
cv = sklearn.model_selection.LeaveOneGroupOut()
splits = cv.split(X=dataset, groups=dataset['id.product_permutation'])
# splits = cv.split(X=dataset, groups=dataset.groupby([x for x in dataset.columns if 'capacity' in x]).ngroup())
results = []
for fold_idx, (train_idx, test_idx) in tqdm.tqdm(enumerate(splits)):
    X_train = dataset.loc[train_idx, features].fillna(0)
    y_train = dataset.loc[train_idx, 'verification.time'].astype(float)
    X_test = dataset.loc[test_idx, features].fillna(0)
    y_test = dataset.loc[test_idx, 'verification.time'].astype(float)
    model = sklearn.tree.DecisionTreeRegressor(random_state=25)  # max_depth=3
    model.fit(X_train, y_train)
    train_r2 = sklearn.metrics.r2_score(y_true=y_train, y_pred=model.predict(X_train))
    test_r2 = sklearn.metrics.r2_score(y_true=y_test, y_pred=model.predict(X_test))
    result = {'fold': fold_idx, 'train_r2': train_r2, 'test_r2': test_r2}
    results.append(result)
pd.DataFrame(results).describe()

pd.Series(model.feature_importances_, X_train.columns).plot.bar()
sklearn.tree.plot_tree(model, feature_names=X_train.columns)  # max_depth=3
plt.savefig('data/regression_tree.pdf')

# ----Classification-----

features = [x for x in dataset.columns if x.startswith('pro') and x != 'property.formula']
# Different splits possible, e.g., random, based on permutations or based on capacity settings:
# cv = sklearn.model_selection.KFold(n_splits=10, shuffle=True, random_state=25)
# splits = cv.split(X=dataset)
cv = sklearn.model_selection.LeaveOneGroupOut()
splits = cv.split(X=dataset, groups=dataset['id.product_permutation'])
# splits = cv.split(X=dataset, groups=dataset.groupby([x for x in dataset.columns if 'capacity' in x]).ngroup())
results = []
for fold_idx, (train_idx, test_idx) in tqdm.tqdm(enumerate(splits)):
    X_train = dataset.loc[train_idx, features].fillna(0)
    y_train = dataset.loc[train_idx, 'verification.result']
    X_test = dataset.loc[test_idx, features].fillna(0)
    y_test = dataset.loc[test_idx, 'verification.result']
    model = sklearn.tree.DecisionTreeClassifier(random_state=25)  # max_depth=3
    model.fit(X_train, y_train)
    train_mcc = sklearn.metrics.matthews_corrcoef(y_true=y_train, y_pred=model.predict(X_train))
    test_mcc = sklearn.metrics.matthews_corrcoef(y_true=y_test, y_pred=model.predict(X_test))
    result = {'fold': fold_idx, 'train_mcc': train_mcc, 'test_mcc': test_mcc}
    results.append(result)
pd.DataFrame(results).describe()

pd.Series(model.feature_importances_, X_train.columns).plot.bar()
sklearn.tree.plot_tree(model, feature_names=X_train.columns)  # max_depth=3
plt.savefig('data/classification_tree.pdf')


import pytest
from affctrllib._periodic_runner import PeriodicRunner

class TestPeriodicRunner:
    def test_init(self) -> None:
        runner = PeriodicRunner()
        assert not hasattr(runner, "dt")
        assert not hasattr(runner, "freq")
        assert runner.n_steps == 0

    @pytest.mark.parametrize("dt,freq", [(0.01, 100), (0.001, 1000), (0.02, 50)])
    def test_init_specify_dt(self, dt, freq) -> None:
        runner = PeriodicRunner(dt=dt)
        assert runner.dt == dt
        assert runner.freq == freq

    @pytest.mark.parametrize("freq,dt", [(100, 0.01), (1000, 0.001), (30, 1.0 / 30)])
    def test_init_specify_freq(self, freq, dt) -> None:
        runner = PeriodicRunner(freq=freq)
        assert runner.dt == dt
        assert runner.freq == freq

    def test_init_error_both_of_dt_freq_specified(self) -> None:
        dt = 0.01
        freq = 100
        with pytest.raises(ValueError) as excinfo:
            _ = PeriodicRunner(dt=dt, freq=freq)
        assert "Unable to specify DT and FREQ simultaneously" in str(excinfo.value)

    @pytest.mark.parametrize("dt,freq", [(0.01, 100), (0.001, 1000), (0.02, 50)])
    def test_dt_setter(self, dt, freq) -> None:
        runner = PeriodicRunner(dt=0.01)
        runner.dt = dt
        assert runner.dt == dt
        assert runner.freq == freq

    @pytest.mark.parametrize("freq,dt", [(100, 0.01), (1000, 0.001), (30, 1.0 / 30)])
    def test_freq_setter(self, dt, freq) -> None:
        runner = PeriodicRunner(dt=0.01)
        runner.freq = freq
        assert runner.dt == dt
        assert runner.freq == freq

    def test_update(self) -> None:
        runner = PeriodicRunner()
        assert runner.n_steps == 0
        runner.update()
        assert runner.n_steps == 1
        runner.update()
        assert runner.n_steps == 2
        runner.update()
        runner.update()
        runner.update()
        assert runner.n_steps == 5


from numpy.lib.function_base import delete
from db_mysql import DB_MYSQL
import json
import numpy as np
from collections import Counter

db = DB_MYSQL()
DF = {}
N = 0
IDF = {}
alpha = 0.6
val = []

def doc_freq(word):
    c = 0
    try:
        c = DF[word]
    except:
        pass
    return c

def calculate_df(all_news):
    for row in all_news:
        title_tokens = json.loads(row[1])
        body_tokens = json.loads(row[2])
        news_tokens = title_tokens + body_tokens

        for token in news_tokens:
            try:
                DF[token].add(row[0])
            except:
                DF[token] = {row[0]}

def calculate_idf():
    for token in DF:
        DF[token] = len(DF[token])
        df = doc_freq(token)
        IDF[token] = np.log((N+1)/(df+1))

def calculate_tf_idf(all_news):
    for row in all_news:
        title_tokens = json.loads(row[1])
        body_tokens = json.loads(row[2])
        news_tokens = title_tokens + body_tokens
        tokens_counter = Counter(news_tokens)
        num_of_tokens = len(news_tokens)

        for token in np.unique(news_tokens):
            idf = IDF[token]
            tf = tokens_counter[token]/num_of_tokens

            if token in title_tokens and token in body_tokens:
                tf_idf = tf * idf
            elif token in title_tokens:
                tf_idf = alpha * (tf * idf)
            elif token in body_tokens:
                tf_idf = (1 - alpha) * (tf * idf)

            val.append((str(token), row[0], str(tf_idf)))

if __name__ == "__main__":
    print("Selecting all news from DB...")
    all_news = db.select_all_news()
    N = len(all_news)
    print("Calculating DF of all tokens...")
    calculate_df(all_news)
    print("Calculating IDF of all (news, token) pair...")
    calculate_idf()
    print("Calculating TF-IDF of all (news, token) pairs...")
    calculate_tf_idf(all_news)
    print("Inserting TF-IDF into table...")
    db.insert_many_tf_idf(val)
    # db.insert_many_idf(IDF)


import csv
import os
import input_variable as inp

''' Data load to backend file '''

# Function to evaluate the selection and perform data entry to backend file
def data_entry(item_number):
    if item_number == '1':
        product_name = "Coke"
    elif item_number == '2':
        product_name = "Sprite"
    elif item_number == '3':
        product_name = "Fanta"
    else:
        product_name = "Water"
    if os.path.exists(inp.database_file(inp.file_name)):
        with open(inp.database_file(inp.file_name), 'a') as csv_file:
            csv_writer = csv.writer(csv_file, lineterminator='\n')
            csv_writer.writerow([item_number, product_name, inp.product_price, inp.today_date])
    else:
        with open(inp.database_file(inp.file_name), 'w') as csv_file:
            csv_writer = csv.writer(csv_file, lineterminator='\n')
            csv_writer.writerow(['Product_ID', 'Product_Name', 'Product_Price', 'Sales_Date'])
            csv_writer.writerow([item_number, product_name, inp.product_price, inp.today_date])


import logging
import time
from comment import Comment

import selenium
import selenium.webdriver as webdriver

class UserDownloader:
    def __init__(self,
                 path_to_chromedriver):
        self._path_to_chromedriver = path_to_chromedriver

        # logger set up
        self._logger = logging.getLogger('SureDownloader_logger')
        self._logger.setLevel(logging.DEBUG)
        # Your path to log
        fh = logging.FileHandler('./src/icu.log')
        fh.setLevel(logging.DEBUG)
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)
        self._logger.addHandler(fh)
        self._logger.addHandler(ch)
        self._delay = 1

        # options = webdriver.ChromeOptions()
        # options.add_argument('--headless')
        self.driver = webdriver.Chrome(executable_path=self._path_to_chromedriver)
        self._logger.info('webdriver has been set up')

    def get_users(self, path_to_post) -> [Comment]:
        self.driver.get(path_to_post)
        self._logger.info('instagram is loaded')
        # We need to reveal all comments
        # Click plus button until it exists
        while True:
            try:
                self.driver.find_element_by_class_name('afkep').click()
                time.sleep(self._delay)
            #     Short delay to let button get ready
            except selenium.common.exceptions.NoSuchElementException:
                break
        self._logger.info('all comments revealed')

        comments = self.driver.find_elements_by_class_name('Mr508')

        result = []
        for comment in comments:
            result.append([Comment(
                text=comment.find_element_by_tag_name('span').text,
                user=comment.find_element_by_class_name('ZIAjV').text,
                date=comment.find_element_by_tag_name('time').get_attribute('datetime')
            )])

        return result


items = ['Python', 'Java', 'Go', 'Kotlin']

# 使用append方法在列表尾部添加元素
items.append('Swift')
print(items)    # ['Python', 'Java', 'Go', 'Kotlin', 'Swift']
# 使用insert方法在列表指定索引位置插入元素
items.insert(2, 'SQL')
print(items)    # ['Python', 'Java', 'SQL', 'Go', 'Kotlin', 'Swift']

# 删除指定的元素
items.remove('Java')
print(items)    # ['Python', 'SQL', 'Go', 'Kotlin', 'Swift']
# 删除指定索引位置的元素
items.pop(0)
items.pop(len(items) - 1)
print(items)    # ['SQL', 'Go', 'Kotlin']

# 清空列表中的元素
items.clear()
print(items)    # []

items = ['Python', 'Java', 'Go', 'Kotlin']
del items[1]
print(items)    # ['Python', 'Go', 'Kotlin']


import numpy as np
import os
import tensorflow as tf
from tensorflow.contrib.framework.python.ops import arg_scope, add_arg_scope
from blocks.layers import conv2d, deconv2d, dense, nin, gated_resnet
from blocks.layers import down_shifted_conv2d, down_right_shifted_conv2d, down_shifted_deconv2d, down_right_shifted_deconv2d, down_shift, right_shift
from blocks.layers import up_shifted_conv2d, up_left_shifted_conv2d, up_shifted_deconv2d, up_left_shifted_deconv2d, up_shift, left_shift
from blocks.samplers import gaussian_sampler, mix_logistic_sampler, bernoulli_sampler
from blocks.estimators import compute_gaussian_kld, estimate_mmd
from blocks.losses import mix_logistic_loss, bernoulli_loss
from blocks.helpers import int_shape, get_name, broadcast_masks_tf
from blocks.metrics import bits_per_dim_tf

class ConvVAE(object):

    def __init__(self, counters={}):
        self.counters = counters

    def build_graph(self, batch_size, img_size, z_dim, reg_type, beta=1.0, network_type="large", sample_range=1.0, nonlinearity=tf.nn.elu, bn=True, kernel_initializer=None, kernel_regularizer=None, nr_logistic_mix=10):

        self.batch_size = batch_size
        self.img_size = img_size
        self.reg_type = reg_type
        self.beta = beta
        self.z_dim = z_dim
        self.nonlinearity = nonlinearity
        self.kernel_initializer = kernel_initializer
        self.kernel_regularizer = kernel_regularizer
        self.bn = bn
        self.sample_range = sample_range
        self.nr_logistic_mix = nr_logistic_mix
        self.network_type = network_type
        self.__model()
        self.__loss()

    def __model(self):
        print("******   Building Graph   ******")
        # placeholders
        if self.network_type == 'binary':
            self.num_channels = 1
        else:
            self.num_channels = 3
        self.x = tf.placeholder(tf.float32, shape=(self.batch_size, self.img_size, self.img_size, self.num_channels))
        self.x_bar = tf.placeholder(tf.float32, shape=(self.batch_size, self.img_size, self.img_size, self.num_channels))
        self.is_training = tf.placeholder(tf.bool, shape=())
        self.dropout_p = tf.placeholder(tf.float32, shape=())
        self.masks = tf.placeholder(tf.float32, shape=(self.batch_size, self.img_size, self.img_size))
        self.input_masks = tf.placeholder(tf.float32, shape=(self.batch_size, self.img_size, self.img_size))
        # choose network size
        if self.img_size == 32:
            if self.network_type == 'large':
                encoder = conv_encoder_32_large_bn
                decoder = conv_decoder_32_large_mixture_logistic
            else:
                raise Exception("unknown network type")
        elif self.img_size == 28:
            if self.network_type == 'binary':
                encoder = conv_encoder_28_binary
                decoder = conv_decoder_28_binary
            else:
                raise Exception("unknown network type")
        kwargs = {
            "nonlinearity": self.nonlinearity,
            "bn": self.bn,
            "kernel_initializer": self.kernel_initializer,
            "kernel_regularizer": self.kernel_regularizer,
            "is_training": self.is_training,
            "counters": self.counters,
        }
        with arg_scope([encoder, decoder], **kwargs):
            inputs = self.x
            inputs = inputs * broadcast_masks_tf(self.masks, num_channels=self.num_channels)
            inputs = tf.concat([inputs, broadcast_masks_tf(self.masks, num_channels=1)], axis=-1)
            self.z_mu, self.z_log_sigma_sq = encoder(inputs, self.z_dim)
            sigma = tf.exp(self.z_log_sigma_sq / 2.)
            self.z = gaussian_sampler(self.z_mu, sigma)

            self.z_ph = tf.placeholder_with_default(tf.zeros_like(self.z_mu), shape=int_shape(self.z_mu))
            self.use_z_ph = tf.placeholder_with_default(False, shape=())

            use_z_ph = tf.cast(tf.cast(self.use_z_ph, tf.int32), tf.float32)
            z = self.z * (1-use_z_ph) + use_z_ph * self.z_ph

            if self.network_type == 'binary':
                self.pixel_params = decoder(z)
                self.x_hat = bernoulli_sampler(self.pixel_params, counters=self.counters)
            else:
                self.pixel_params = decoder(z, nr_logistic_mix=self.nr_logistic_mix)
                self.x_hat = mix_logistic_sampler(self.pixel_params, nr_logistic_mix=self.nr_logistic_mix, sample_range=self.sample_range, counters=self.counters)

    def __loss(self):
        print("******   Compute Loss   ******")
        if self.network_type == 'binary':
            self.loss_nll = bernoulli_loss(self.x, self.pixel_params, masks=self.masks, output_mean=False)
        else:
            self.loss_nll = mix_logistic_loss(self.x, self.pixel_params, masks=self.masks, output_mean=False)
        self.bits_per_dim = tf.reduce_mean(bits_per_dim_tf(nll=self.loss_nll, dim=tf.reduce_sum(1-self.masks, axis=[1,2])*self.num_channels))
        self.loss_nll = tf.reduce_mean(self.loss_nll)
        self.lam = 0.0
        if self.reg_type is None:
            self.loss_reg = 0
        elif self.reg_type =='kld':
            self.kld = compute_gaussian_kld(self.z_mu, self.z_log_sigma_sq)
            self.loss_reg = self.beta * tf.maximum(self.lam, self.kld)
        elif self.reg_type =='mmd':
            # self.mmd = estimate_mmd(tf.random_normal(int_shape(self.z)), self.z)
            self.mmd = estimate_mmd(tf.random_normal(tf.stack([256, self.z_dim])), self.z)
            self.loss_reg = self.beta * tf.maximum(self.lam, self.mmd)
        elif self.reg_type =='tc':
            self.mi, self.tc, self.dwkld = estimate_mi_tc_dwkld(self.z, self.z_mu, self.z_log_sigma_sq, N=10000)
            self.loss_reg = self.mi + self.beta * self.tc + self.dwkld
        self.loss = self.loss_nll + self.loss_reg

@add_arg_scope
def conv_encoder_32_large_bn(inputs, z_dim, is_training=False, nonlinearity=None, bn=True, kernel_initializer=None, kernel_regularizer=None, counters={}):
    name = get_name("conv_encoder_32_large_bn", counters)
    print("construct", name, "...")
    with tf.variable_scope(name):
        with arg_scope([conv2d, dense], nonlinearity=nonlinearity, bn=bn, kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer, is_training=is_training, counters=counters):
            outputs = inputs
            outputs = conv2d(outputs, 32, 1, 1, "SAME")
            outputs = conv2d(outputs, 64, 4, 2, "SAME")
            outputs = conv2d(outputs, 128, 4, 2, "SAME")
            outputs = conv2d(outputs, 256, 4, 2, "SAME")
            outputs = conv2d(outputs, 512, 4, 1, "VALID")
            outputs = tf.reshape(outputs, [-1, 512])
            z_mu = dense(outputs, z_dim, nonlinearity=None, bn=True)
            z_log_sigma_sq = dense(outputs, z_dim, nonlinearity=None, bn=True)
            return z_mu, z_log_sigma_sq

@add_arg_scope
def conv_decoder_32_large_mixture_logistic(inputs, nr_logistic_mix, nonlinearity=None, bn=True, kernel_initializer=None, kernel_regularizer=None, is_training=False, counters={}):
    name = get_name("conv_decoder_32_large_mixture_logistic", counters)
    print("construct", name, "...")
    with tf.variable_scope(name):
        with arg_scope([deconv2d, dense], nonlinearity=nonlinearity, bn=bn, kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer, is_training=is_training, counters=counters):
            outputs = dense(inputs, 512)
            outputs = tf.reshape(outputs, [-1, 1, 1, 512])
            outputs = deconv2d(outputs, 256, 4, 1, "VALID")
            outputs = deconv2d(outputs, 128, 4, 2, "SAME")
            outputs = deconv2d(outputs, 64, 4, 2, "SAME")
            outputs = deconv2d(outputs, 32, 4, 2, "SAME")
            outputs = deconv2d(outputs, 10 * nr_logistic_mix, 1, 1, "SAME", nonlinearity=None, bn=False)
            return outputs

@add_arg_scope
def conv_encoder_28_binary(inputs, z_dim, is_training, nonlinearity=None, bn=True, kernel_initializer=None, kernel_regularizer=None, counters={}):
    name = get_name("conv_encoder_28_binary", counters)
    print("construct", name, "...")
    with tf.variable_scope(name):
        with arg_scope([conv2d, dense], nonlinearity=nonlinearity, bn=bn, kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer, is_training=is_training, counters=counters):
            outputs = inputs
            outputs = conv2d(outputs, 32, 1, 1, "SAME")
            outputs = conv2d(outputs, 64, 4, 2, "SAME")
            outputs = conv2d(outputs, 64, 4, 2, "SAME")
            outputs = conv2d(outputs, 128, 4, 1, "VALID")
            outputs = conv2d(outputs, 128, 4, 1, "VALID")
            outputs = tf.reshape(outputs, [-1, 128])
            z_mu = dense(outputs, z_dim, nonlinearity=None, bn=True)
            z_log_sigma_sq = dense(outputs, z_dim, nonlinearity=None, bn=True)
            return z_mu, z_log_sigma_sq

@add_arg_scope
def conv_decoder_28_binary(inputs, nonlinearity=None, bn=True, kernel_initializer=None, kernel_regularizer=None, is_training=False, counters={}):
    name = get_name("conv_decoder_28_binary", counters)
    print("construct", name, "...")
    with tf.variable_scope(name):
        with arg_scope([deconv2d, dense], nonlinearity=nonlinearity, bn=bn, kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer, is_training=is_training, counters=counters):
            outputs = dense(inputs, 128)
            outputs = tf.reshape(outputs, [-1, 1, 1, 128])
            outputs = deconv2d(outputs, 128, 4, 1, "VALID")
            outputs = deconv2d(outputs, 64, 4, 1, "VALID")
            outputs = deconv2d(outputs, 64, 4, 2, "SAME")
            outputs = deconv2d(outputs, 32, 4, 2, "SAME")
            outputs = deconv2d(outputs, 1, 1, 1, "SAME", nonlinearity=None, bn=False)
            return outputs


import cv2
import matplotlib.pyplot as ppl
import numpy as np

#z githuba
def kittler(im):
    """
    The reimplementation of Kittler-Illingworth Thresholding algorithm by Bob Pepin
    Works on 8-bit images only
    Original Matlab code: https://www.mathworks.com/matlabcentral/fileexchange/45685-kittler-illingworth-thresholding
    Paper: Kittler, J. & Illingworth, J. Minimum error thresholding. Pattern Recognit. 19, 41–47 (1986).
    """
    out = im.copy()
    h,g = np.histogram(im.ravel(),256,[0,256])
    h = h.astype(np.float)
    g = g.astype(np.float)
    g = g[:-1]
    c = np.cumsum(h)
    m = np.cumsum(h * g)
    s = np.cumsum(h * g**2)
    sigma_f = np.sqrt(s/c - (m/c)**2)
    cb = c[-1] - c
    mb = m[-1] - m
    sb = s[-1] - s
    sigma_b = np.sqrt(sb/cb - (mb/cb)**2)
    p =  c / c[-1]
    v = p * np.log(sigma_f) + (1-p)*np.log(sigma_b) - p*np.log(p) - (1-p)*np.log(1-p)
    v[~np.isfinite(v)] = np.inf
    idx = np.argmin(v)
    t = g[idx]
    out[:,:] = 0
    out[im >= t] = 255
    return t, out

coins_im = cv2.imread("Binaryzacja/coins.png", cv2.IMREAD_GRAYSCALE)

my_thresh =  80
ret1,bin_coins= cv2.threshold(coins_im,my_thresh,255,cv2.THRESH_BINARY)
#coins 80
#rice 140
#tekst 160
#obiekty 140
#katalog ?
otsu_thres, otsu_im = cv2.threshold(coins_im,0,255,cv2.THRESH_OTSU)

#kittler_im = coins_im.copy()
kit_th, kittler_im = kittler(coins_im)
print(kit_th)

ppl.figure(231)
ppl.subplot(231)
ppl.imshow(bin_coins,cmap="gray")
ppl.title("my: "+ str(my_thresh))
ppl.subplot(232)
ppl.imshow(otsu_im,cmap="gray")
ppl.title("otsu: "+str(otsu_thres))
ppl.subplot(234)
ppl.imshow(kittler_im,cmap="gray")
ppl.title("kittler: " + str(kit_th))
ppl.subplot(235)
ppl.imshow(coins_im,cmap="gray")
ppl.title("basic")
ppl.subplot(236)
ppl.hist(coins_im.flatten(),bins=256,range=(0,255))
ppl.show()


import torch
import numpy as np
import torch.distributed as dist
from tqdm import tqdm
from .BaseModel import TwoTowerBaseModel
from utils.util import pack_results

class TwoTowerGateFormer(TwoTowerBaseModel):
    def __init__(self, manager, newsEncoder, userEncoder, weighter):
        name = "-".join([type(self).__name__, newsEncoder.name, userEncoder.name, f"{manager.enable_gate}_{weighter.name}", str(manager.k)])
        super().__init__(manager, name)

        self.newsEncoder = newsEncoder
        self.userEncoder = userEncoder
        self.weighter = weighter
        self.k = manager.k

        keep_k_modifier = torch.zeros(manager.sequence_length)
        keep_k_modifier[1:self.k + 1] = 1
        self.register_buffer('keep_k_modifier', keep_k_modifier, persistent=False)

    def _encode_news(self, x, cdd=True):
        if cdd:
            token_id = x["cdd_token_id"].to(self.device)
            attn_mask = x['cdd_attn_mask'].to(self.device)
            try:
                gate_mask = x['cdd_gate_mask'].to(self.device)
            except:
                # in case that enable_gate is heuristic
                gate_mask = None

        else:
            token_id = x["his_token_id"].to(self.device)
            attn_mask = x["his_attn_mask"].to(self.device)
            try:
                gate_mask = x['his_gate_mask'].to(self.device)
            except:
                # in case that enable_gate is heuristic
                gate_mask = None

        token_weight = self.weighter(token_id, attn_mask)
        gated_token_id, gated_attn_mask, gated_token_weight = self._compute_gate(token_id, attn_mask, gate_mask, token_weight)
        news_token_embedding, news_embedding = self.newsEncoder(gated_token_id, gated_attn_mask, gated_token_weight)
        return news_token_embedding, news_embedding

class UserOneTowerGateFormer(TwoTowerBaseModel):
    def __init__(self, manager, encoder, weighter):
        name = "-".join([type(self).__name__, f"{manager.enable_gate}_{weighter.name}", str(manager.k)])
        super().__init__(manager, name)

        self.encoder = encoder
        self.weighter = weighter
        self.k = manager.k

        keep_k_modifier = torch.zeros(manager.sequence_length)
        keep_k_modifier[1:self.k + 1] = 1
        self.register_buffer('keep_k_modifier', keep_k_modifier, persistent=False)

        self.register_buffer("cls_token_id", torch.tensor(manager.special_token_ids["[CLS]"], dtype=torch.long), persistent=False)
        self.register_buffer("cls_attn_mask", torch.ones(1, dtype=torch.long), persistent=False)
        self.register_buffer("cls_token_weight", torch.ones(1), persistent=False)

    def _encode_news(self, x):
        token_id = x["cdd_token_id"].to(self.device)
        attn_mask = x['cdd_attn_mask'].to(self.device)
        news_token_embedding, news_embedding = self.encoder(token_id, attn_mask)
        return news_token_embedding, news_embedding

    def _encode_user(self, x=None, his_embedding=None, attn_mask=None):
        if x is None:
            # TODO: cache gated token ids, attention masks and weights
            pass
        else:
            token_id = x["his_token_id"].to(self.device)
            attn_mask = x["his_attn_mask"].to(self.device)
            try:
                gate_mask = x['his_gate_mask'].to(self.device)
            except:
                # in case that enable_gate is heuristic
                gate_mask = None
            B = token_id.shape[0]

            try:
                token_weight = self.weighter(token_id, attn_mask)
            except:
                print("fuck weighter")
                raise
            # B, NH, K
            try:
                gated_token_id, gated_attn_mask, gated_token_weight = self._compute_gate(token_id, attn_mask, gate_mask, token_weight)
            except:
                print("fuck gating")
                raise
            gated_token_id = gated_token_id.reshape(B, -1)
            gated_attn_mask = gated_attn_mask.reshape(B, -1)
            gated_token_id = torch.cat([self.cls_token_id.expand(B, 1), gated_token_id], dim=-1)
            gated_attn_mask = torch.cat([self.cls_attn_mask.expand(B, 1), gated_attn_mask], dim=-1)
            if gated_token_weight is not None:
                gated_token_weight = gated_token_weight.reshape(B, -1)
                gated_token_weight = torch.cat([self.cls_token_weight.expand(B, 1), gated_token_weight], dim=-1)

            # B, 1, D
            try:
                user_embedding = self.encoder(gated_token_id, gated_attn_mask, token_weight=gated_token_weight)[1].unsqueeze(-2)
            except:
                print("fuck encoding")
                raise

        return user_embedding

    def forward(self, x):
        _, cdd_news_embedding = self._encode_news(x)
        user_embedding = self._encode_user(x)

        logits = self._compute_logits(cdd_news_embedding, user_embedding)
        labels = x["label"].to(self.device)
        loss = self.crossEntropy(logits, labels)
        return loss

    def infer(self, x):
        """
        one-tower user encoding
        """
        cdd_idx = x["cdd_idx"].to(self.device, non_blocking=True)
        cdd_embedding = self.news_embeddings[cdd_idx]
        user_embedding = self._encode_user(x)

        logits = self._compute_logits(cdd_embedding, user_embedding)
        return logits


# -*- coding: utf-8 -*-
"""The single process processing engine."""

import collections
import logging
import os
import pdb
import time

from dfvfs.resolver import context

from plaso.engine import collector
from plaso.engine import engine
from plaso.engine import queue
from plaso.engine import worker
from plaso.lib import definitions
from plaso.lib import errors
from plaso.parsers import mediator as parsers_mediator

class SingleProcessCollector(collector.Collector):
  """Class that implements a single process collector object."""

  def __init__(self, path_spec_queue, resolver_context=None):
    """Initializes the collector object.

       The collector discovers all the files that need to be processed by
       the workers. Once a file is discovered it is added to the process queue
       as a path specification (instance of dfvfs.PathSpec).

    Args:
      path_spec_queue: the path specification queue (instance of Queue).
                       This queue contains the path specifications (instances
                       of dfvfs.PathSpec) of the file entries that need
                       to be processed.
      resolver_context: optional resolver context (instance of dfvfs.Context).
    """
    super(SingleProcessCollector, self).__init__(
        path_spec_queue, resolver_context=resolver_context)

    self._extraction_worker = None
    self._fs_collector = SingleProcessFileSystemCollector(
        path_spec_queue, resolver_context=resolver_context,
        status_update_callback=self._UpdateStatus)

  def _FlushQueue(self):
    """Flushes the queue callback for the QueueFull exception."""
    self._UpdateStatus()
    while not self._queue.IsEmpty():
      self._extraction_worker.Run()

  def _UpdateStatus(self):
    """Updates the processing status."""
    # Set the collector status to waiting while emptying the queue.
    self._status = definitions.PROCESSING_STATUS_WAITING

  def SetExtractionWorker(self, extraction_worker):
    """Sets the extraction worker.

    Args:
      extraction_worker: the extraction worker object (instance of
                         EventExtractionWorker).
    """
    self._extraction_worker = extraction_worker

    self._fs_collector.SetExtractionWorker(extraction_worker)

class SingleProcessEngine(engine.BaseEngine):
  """Class that defines the single process engine."""

  _STATUS_CHECK_SLEEP = 1.5

  def __init__(self, maximum_number_of_queued_items=0):
    """Initialize the single process engine object.

    Args:
      maximum_number_of_queued_items: the maximum number of queued items.
                                      The default is 0, which represents
                                      no limit.
    """
    path_spec_queue = SingleProcessQueue(
        maximum_number_of_queued_items=maximum_number_of_queued_items)
    event_object_queue = SingleProcessQueue(
        maximum_number_of_queued_items=maximum_number_of_queued_items)
    parse_error_queue = SingleProcessQueue(
        maximum_number_of_queued_items=maximum_number_of_queued_items)

    super(SingleProcessEngine, self).__init__(
        path_spec_queue, event_object_queue, parse_error_queue)

    self._collector = None
    self._extraction_worker = None
    self._event_queue_producer = SingleProcessItemQueueProducer(
        event_object_queue)
    self._last_status_update_timestamp = time.time()
    self._parse_error_queue_producer = SingleProcessItemQueueProducer(
        parse_error_queue)
    self._status_update_callback = None
    self._storage_writer = None

  def _CreateCollector(
      self, filter_find_specs=None, include_directory_stat=True,
      resolver_context=None):
    """Creates a collector object.

       The collector discovers all the files that need to be processed by
       the workers. Once a file is discovered it is added to the process queue
       as a path specification (instance of dfvfs.PathSpec).

    Args:
      filter_find_specs: optional list of filter find specifications (instances
                         of dfvfs.FindSpec).
      include_directory_stat: optional boolean value to indicate whether
                              directory stat information should be collected.
      resolver_context: optional resolver context (instance of dfvfs.Context).

    Returns:
      A collector object (instance of Collector).
    """
    collector_object = SingleProcessCollector(
        self._path_spec_queue, resolver_context=resolver_context)

    collector_object.SetCollectDirectoryMetadata(include_directory_stat)

    if filter_find_specs:
      collector_object.SetFilter(filter_find_specs)

    return collector_object

  def _CreateExtractionWorker(
      self, worker_number, filter_object=None, mount_path=None,
      process_archive_files=False, text_prepend=None):
    """Creates an extraction worker object.

    Args:
      worker_number: a number that identifies the worker.
      filter_object: optional filter object (instance of objectfilter.Filter).
      mount_path: optional string containing the mount path.
      process_archive_files: optional boolean value to indicate if the worker
                             should scan for file entries inside files.
      text_prepend: optional string that contains the text to prepend to every
                    event object.

    Returns:
      An extraction worker (instance of worker.ExtractionWorker).
    """
    parser_mediator = parsers_mediator.ParserMediator(
        self._event_queue_producer, self._parse_error_queue_producer,
        self.knowledge_base)

    resolver_context = context.Context()

    extraction_worker = SingleProcessEventExtractionWorker(
        worker_number, self._path_spec_queue, self._event_queue_producer,
        self._parse_error_queue_producer, parser_mediator,
        resolver_context=resolver_context,
        status_update_callback=self._UpdateStatus)

    # TODO: differentiate between debug output and debug mode.
    extraction_worker.SetEnableDebugMode(self._enable_debug_output)

    extraction_worker.SetEnableProfiling(
        self._enable_profiling,
        profiling_sample_rate=self._profiling_sample_rate,
        profiling_type=self._profiling_type)

    extraction_worker.SetProcessArchiveFiles(process_archive_files)

    if filter_object:
      extraction_worker.SetFilterObject(filter_object)

    if mount_path:
      extraction_worker.SetMountPath(mount_path)

    if text_prepend:
      extraction_worker.SetTextPrepend(text_prepend)

    return extraction_worker

  def _UpdateCollectorStatus(self, processing_completed=False):
    """Updates the collector status.

    Args:
      processing_completed: optional boolean value the processing has been
                            completed.
    """
    status = self._collector.GetStatus()
    status_indicator = status.get(u'processing_status', None)
    produced_number_of_path_specs = status.get(
        u'produced_number_of_path_specs', 0)

    if processing_completed:
      status_indicator = definitions.PROCESSING_STATUS_COMPLETED
    elif status_indicator == definitions.PROCESSING_STATUS_INITIALIZED:
      status_indicator = definitions.PROCESSING_STATUS_RUNNING

    self._processing_status.UpdateCollectorStatus(
        u'Collector', os.getpid(), produced_number_of_path_specs,
        status_indicator, None)

  def _UpdateExtractionWorkerStatus(self, processing_completed=False):
    """Updates the extraction worker status.

    Args:
      processing_completed: optional boolean value the processing has been
                            completed.
    """
    status = self._extraction_worker.GetStatus()
    status_indicator = status.get(u'processing_status', None)
    consumed_number_of_path_specs = status.get(
        u'consumed_number_of_path_specs', 0)
    display_name = status.get(u'display_name', u'')
    number_of_events = status.get(u'number_of_events', 0)
    produced_number_of_path_specs = status.get(
        u'produced_number_of_path_specs', 0)

    if processing_completed:
      status_indicator = definitions.PROCESSING_STATUS_COMPLETED
    elif status_indicator in (
        definitions.PROCESSING_STATUS_COMPLETED,
        definitions.PROCESSING_STATUS_INITIALIZED):
      # If the worker is initialized or completed in single
      # processing mode it is actually waiting for input.
      status_indicator = definitions.PROCESSING_STATUS_WAITING

    self._processing_status.UpdateExtractionWorkerStatus(
        u'Worker', os.getpid(), display_name, number_of_events,
        consumed_number_of_path_specs, produced_number_of_path_specs,
        status_indicator, None)

  def _UpdateStorageWriterStatus(self, processing_completed=False):
    """Updates the storage writer status.

    Args:
      processing_completed: optional boolean value the processing has been
                            completed.
    """
    status = self._storage_writer.GetStatus()
    status_indicator = status.get(u'processing_status', None)
    number_of_events = status.get(u'number_of_events', 0)

    if processing_completed:
      status_indicator = definitions.PROCESSING_STATUS_COMPLETED
    elif status_indicator in (
        definitions.PROCESSING_STATUS_COMPLETED,
        definitions.PROCESSING_STATUS_INITIALIZED):
      # If the storage writer is initialized or completed in single
      # processing mode it is actually waiting for input.
      status_indicator = definitions.PROCESSING_STATUS_WAITING

    self._processing_status.UpdateStorageWriterStatus(
        u'StorageWriter', os.getpid(), number_of_events, status_indicator, None)

  def _UpdateStatus(self, processing_completed=False):
    """Updates the processing status.

    Args:
      processing_completed: optional boolean value the processing has been
                            completed.
    """
    current_timestamp = time.time()
    if current_timestamp < (
        self._last_status_update_timestamp + self._STATUS_CHECK_SLEEP):
      return

    self._UpdateCollectorStatus(processing_completed=processing_completed)
    self._UpdateExtractionWorkerStatus(
        processing_completed=processing_completed)
    self._UpdateStorageWriterStatus(processing_completed=processing_completed)

    if self._status_update_callback:
      self._status_update_callback(self._processing_status)

    self._last_status_update_timestamp = current_timestamp

  def ProcessSources(
      self, source_path_specs, storage_writer, filter_find_specs=None,
      filter_object=None, hasher_names_string=None, include_directory_stat=True,
      mount_path=None, parser_filter_string=None, process_archive_files=False,
      resolver_context=None, status_update_callback=None, text_prepend=None):
    """Processes the sources and extract event objects.

    Args:
      source_path_specs: list of path specifications (instances of
                         dfvfs.PathSpec) to process.
      storage_writer: a storage writer object (instance of BaseStorageWriter).
      filter_find_specs: optional list of filter find specifications (instances
                         of dfvfs.FindSpec).
      filter_object: optional filter object (instance of objectfilter.Filter).
      hasher_names_string: optional comma separated string of names of
                           hashers to enable.
      include_directory_stat: optional boolean value to indicate whether
                              directory stat information should be collected.
      mount_path: optional string containing the mount path.
      parser_filter_string: optional parser filter string.
      process_archive_files: optional boolean value to indicate if the worker
                             should scan for file entries inside files.
      resolver_context: optional resolver context (instance of dfvfs.Context).
      status_update_callback: optional callback function for status updates.
      text_prepend: optional string that contains the text to prepend to every
                    event object.

    Returns:
      The processing status (instance of ProcessingStatus).
    """
    self._collector = self._CreateCollector(
        filter_find_specs=filter_find_specs,
        include_directory_stat=include_directory_stat,
        resolver_context=resolver_context)

    self._extraction_worker = self._CreateExtractionWorker(
        0, filter_object=filter_object, mount_path=mount_path,
        process_archive_files=process_archive_files,
        text_prepend=text_prepend)

    self._storage_writer = storage_writer

    self._status_update_callback = status_update_callback

    if hasher_names_string:
      self._extraction_worker.SetHashers(hasher_names_string)

    self._extraction_worker.InitializeParserObjects(
        parser_filter_string=parser_filter_string)

    # Set the extraction worker and storage writer values so that they
    # can be accessed if the QueueFull exception is raised. This is
    # needed in single process mode to prevent the queue consuming too
    # much memory.
    self._collector.SetExtractionWorker(self._extraction_worker)
    self._event_queue_producer.SetStorageWriter(self._storage_writer)
    self._parse_error_queue_producer.SetStorageWriter(self._storage_writer)

    logging.debug(u'Processing started.')

    self._UpdateStatus()
    self._collector.Collect(source_path_specs)

    self._UpdateStatus()
    self._extraction_worker.Run()

    self._UpdateStatus()
    self._storage_writer.WriteEventObjects()

    self._UpdateStatus(processing_completed=True)

    # Reset the extraction worker and storage writer values to return
    # the objects in their original state. This will prevent access
    # to the extraction worker outside this function and allow it
    # to be garbage collected.
    self._extraction_worker = None
    self._storage_writer = None
    self._status_update_callback = None
    self._event_queue_producer.SetStorageWriter(None)
    self._parse_error_queue_producer.SetStorageWriter(None)
    self._collector = None

    logging.debug(u'Processing completed.')

    return self._processing_status

  def SignalAbort(self):
    """Signals the engine to abort."""
    self._event_queue_producer.SignalAbort()
    self._parse_error_queue_producer.SignalAbort()

    if self._collector:
      self._collector.SignalAbort()

class SingleProcessEventExtractionWorker(worker.BaseEventExtractionWorker):
  """Class that defines the single process event extraction worker."""

  def __init__(
      self, identifier, path_spec_queue, event_queue_producer,
      parse_error_queue_producer, parser_mediator, resolver_context=None,
      status_update_callback=None):
    """Initializes the event extraction worker object.

    Args:
      identifier: the identifier, usually an incrementing integer.
      path_spec_queue: the path specification queue (instance of Queue).
                       This queue contains the path specifications (instances
                       of dfvfs.PathSpec) of the file entries that need
                       to be processed.
      event_queue_producer: the event object queue producer (instance of
                            ItemQueueProducer).
      parse_error_queue_producer: the parse error queue producer (instance of
                                  ItemQueueProducer).
      parser_mediator: a parser mediator object (instance of ParserMediator).
      resolver_context: optional resolver context (instance of dfvfs.Context).
      status_update_callback: optional callback function for status updates.
    """
    super(SingleProcessEventExtractionWorker, self).__init__(
        identifier, path_spec_queue, event_queue_producer,
        parse_error_queue_producer, parser_mediator,
        resolver_context=resolver_context)

    self._status_update_callback = status_update_callback

  def _ConsumeItem(self, path_spec, **kwargs):
    """Consumes an item callback for ConsumeItems.

    Args:
      path_spec: a path specification (instance of dfvfs.PathSpec).

    Raises:
      QueueFull: If a queue is full.
    """
    if self._status_update_callback:
      self._status_update_callback()

    super(SingleProcessEventExtractionWorker, self)._ConsumeItem(
        path_spec, **kwargs)

  def _DebugProcessPathSpec(self):
    """Callback for debugging path specification processing failures."""
    pdb.post_mortem()

class SingleProcessFileSystemCollector(collector.FileSystemCollector):
  """Class that implements a single process file system collector object."""

  def __init__(
      self, path_spec_queue, resolver_context=None,
      status_update_callback=None):
    """Initializes the collector object.

       The collector discovers all the files that need to be processed by
       the workers. Once a file is discovered it is added to the process queue
       as a path specification (instance of dfvfs.PathSpec).

    Args:
      path_spec_queue: the path specification queue (instance of Queue).
                       This queue contains the path specifications (instances
                       of dfvfs.PathSpec) of the file entries that need
                       to be processed.
      processing_status: the processing status (instance of ProcessingStatus).
      resolver_context: optional resolver context (instance of dfvfs.Context).
      status_update_callback: optional callback function for status updates.
                              This callback is invoked when the collector
                              flushes its queue.
    """
    super(SingleProcessFileSystemCollector, self).__init__(
        path_spec_queue, resolver_context=resolver_context)

    self._extraction_worker = None
    self._status_update_callback = status_update_callback

  def _FlushQueue(self):
    """Flushes the queue callback for the QueueFull exception."""
    if self._status_update_callback:
      self._status_update_callback()

    while not self._queue.IsEmpty():
      self._extraction_worker.Run()

  def SetExtractionWorker(self, extraction_worker):
    """Sets the extraction worker.

    Args:
      extraction_worker: the extraction worker object (instance of
                         EventExtractionWorker).
    """
    self._extraction_worker = extraction_worker

class SingleProcessItemQueueProducer(queue.ItemQueueProducer):
  """Class that implements a single process item queue producer."""

  def __init__(self, queue_object):
    """Initializes the queue producer.

    Args:
      queue_object: the queue object (instance of Queue).
    """
    super(SingleProcessItemQueueProducer, self).__init__(queue_object)

    self._storage_writer = None

  def _FlushQueue(self):
    """Flushes the queue callback for the QueueFull exception."""
    self._storage_writer.WriteEventObjects()

  def SetStorageWriter(self, storage_writer):
    """Sets the storage writer.

    Args:
      storage_writer: the storage writer object (instance of
                      BaseStorageWriter).
    """
    self._storage_writer = storage_writer

class SingleProcessQueue(queue.Queue):
  """Single process queue."""

  def __init__(self, maximum_number_of_queued_items=0):
    """Initializes a single process queue object.

    Args:
      maximum_number_of_queued_items: the maximum number of queued items.
                                      The default is 0, which represents
                                      no limit.
    """
    super(SingleProcessQueue, self).__init__()

    # The Queue interface defines the maximum number of queued items to be
    # 0 if unlimited as does the multi processing queue, but deque uses
    # None to indicate no limit.
    if maximum_number_of_queued_items == 0:
      maximum_number_of_queued_items = None

    # maxlen contains the maximum number of items allowed to be queued,
    # where None represents unlimited.
    self._queue = collections.deque(
        maxlen=maximum_number_of_queued_items)

  def IsEmpty(self):
    """Determines if the queue is empty."""
    return len(self._queue) == 0

  def PushItem(self, item):
    """Pushes an item onto the queue.

    Raises:
      QueueFull: when the queue is full.
    """
    number_of_items = len(self._queue)

    # Deque will drop the first item in the queue when maxlen is exceeded.
    if not self._queue.maxlen or number_of_items < self._queue.maxlen:
      self._queue.append(item)
      number_of_items += 1

    if self._queue.maxlen and number_of_items == self._queue.maxlen:
      raise errors.QueueFull

  def PopItem(self):
    """Pops an item off the queue or None on timeout.

    Raises:
      QueueClose: on user abort.
      QueueEmpty: when the queue is empty.
    """
    try:
      # Using popleft to have FIFO behavior.
      return self._queue.popleft()
    except IndexError:
      raise errors.QueueEmpty
    except KeyboardInterrupt:
      raise errors.QueueClose

  def Close(self):
    """Closes this queue, indicating that no further items will be added to it.

    This method has no effect on for the single process queue, but is included
    for compatibility with the Multiprocessing queue."""
    return

  def Open(self):
    """Opens the queue, ready to enqueue or dequeue items.

    This method has no effect on for the single process queue, but is included
    for compatibility with the Multiprocessing queue."""
    return


import numpy as np

def multi_view_to_single_view(data,views = 6):
	new_data = []
	for number in range(data.shape[0]):
		for view in range(views):
			new_data.append(data[number,:,:,view])
	new_data = np.array(new_data)
	elementos,size1,size2 = new_data.shape
	return new_data.reshape((elementos,size1,size2,1))

def split_views(data,views = 6):
	new_data = []
	for view in range(views):
		new_data.append([])

	for number in range(data.shape[0]):
		for view in range(views):
			new_data[view].append(data[number,:,:,view])

	for view in range(views):
		array = np.array(new_data[view])
		elementos,size1,size2 = array.shape
		new_data[view] = array

	return new_data[0].reshape((elementos,size1,size2,1)), new_data[1].reshape((elementos,size1,size2,1)), new_data[2].reshape((elementos,size1,size2,1)), new_data[3].reshape((elementos,size1,size2,1)), new_data[4].reshape((elementos,size1,size2,1)), new_data[5].reshape((elementos,size1,size2,1))

def crear_cubo(size):
    cubo = np.zeros((size,size,size))
    return cubo

def truncate_view(view,size):
    temp = view>size
    view[temp] = size
    view = view.astype(int)
    return view

def add_view_to_cube(voxel_cube,view_image,size,view):
    view_image = truncate_view(view_image,size)
    scale = size
    if view <= 2:
        for i in range(scale):
            for j in range(scale):
                if view == 0:
                    if view_image[i,j] > 0:
                        voxel_cube[i,j,view_image[i,j]] = 1
                elif view == 1:
                    if view_image[i,j] > 0:
                        voxel_cube[i,view_image[i,j],j] = 1
                elif view == 2:
                    if  view_image[i,j] > 0:
                        voxel_cube[view_image[i,j],i,j] = 1
                else:
                    return voxel_cube
    else:
        for i in range(scale):
            for j in range(scale):
                if view == 3:
                    if view_image[i,j] > 0:
                        voxel_cube[i,j,-view_image[i,j]] = 1
                elif view == 4:
                    if view_image[i,j] > 0:
                        voxel_cube[i,-view_image[i,j],j]  = 1
                elif view == 5:
                    if  view_image[i,j] > 0:
                        voxel_cube[-view_image[i,j],i,j] = 1
                else:
                    return voxel_cube

    return voxel_cube

def views_to_boxel(views,size):
    vistas = views.shape[2]
    voxel_cube = crear_cubo(size)
    for vista in range(vistas):
        voxel_cube = add_view_to_cube(voxel_cube,views[:,:,vista],size,vista)
    return voxel_cube

def boxel_to_cloud(voxel_cube):
    array_x = []
    array_y = []
    array_z = []
    r,d,f = voxel_cube.shape
    for i in range(r):
        for j in range(d):
            for k in range(f):
                if voxel_cube[i,j,k] == 1:
                    array_x.append(i)
                    array_y.append(j)
                    array_z.append(k)
    return np.array(array_x), np.array(array_y), np.array(array_z)

if __name__ == '__main__':
	datasets_directory = '/media/leo/Data/'
	X_val = np.load(datasets_directory + 'X_val.npy')
	view_1, view_2, view_3, view_4, view_5, view_6 = split_views(X_val)
	print(view_1.shape)
	print(view_2.shape)
	print(view_3.shape)
	print(view_4.shape)
	print(view_5.shape)
	print(view_6.shape)

Networks/Shape Completion Enebled Robotic Grasping/utilities_for_data.py


import urllib.request
import urllib.parse
from lxml import etree
from fake_useragent import UserAgent
from concurrent.futures import ThreadPoolExecutor

random_useragent = UserAgent().random
headers = {"User-Agent": random_useragent}

# 获取疾病名称
def get_disease_name(page):
    try:
        url = "https://jib.xywy.com/il_sii/gaishu/%s.htm" % page
        req = urllib.request.Request(url=url, headers=headers)
        res = urllib.request.urlopen(req)
        html = res.read().decode("gbk")
        selector = etree.HTML(html)
        name = selector.xpath("//title/text()")[0].split("的简介")[0]
        print(page, url)
        return name
    except Exception as e:
        print(e, page, "该界面丢失(网站问题)")
        return None

with open("disease.txt", "w", encoding="utf-8") as file:
    with ThreadPoolExecutor(max_workers=16) as executor:
        pages = range(1, 11000)
        symptom_names = executor.map(get_disease_name, pages)

        for name in symptom_names:
            if name:
                file.write(name + "\n")


"""
This code is used to solve the famous handwritten number
recognition problem via RFSVM using svm and sgdclassifier module
in sklearn package.
"""
import numpy as np
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import rff
import log
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import cross_val_score, KFold
from sklearn.linear_model import SGDClassifier, LogisticRegression
from sklearn.metrics import confusion_matrix
from sklearn import svm
from sys import argv
from libmnist import plot_confusion_matrix, get_train_test_data

# set up parameters
LogLambda = np.arange(-12.0,-2,20)
Gamma = 10.**(-3)

def RFSVM_MNIST(method='U',m=1000,n_components=1000):
    # set up timer and progress tracker
    mylog = log.log('log/{0:s}RFSVM_N_{1:.2e}_m_{2:.2e}.log'.format(method,
        n_components,m),
        '{:s}RFSVM MNIST classification starts'.format(method))
    Xtrain,Ytrain,Xtest,Ytest = get_train_test_data(train_size=m,
        test_size=int(m/3))
    mylog.time_event('data read in complete')

    X_pool_fraction = 0.3
    feature_pool_size = n_components * 10

    # hyper-parameter selection
    best_score = 0
    best_Lambda = 1
    result = {'Gamma':[],'Lambda':[],'score':[]}
    for jdx in range(len(LogLambda)):
        Lambda = 10**LogLambda[jdx]
        feature = rff.optRBFSampler(Xtrain.shape[1],
            gamma=Gamma,
            feature_pool_size=feature_pool_size,
            n_components=n_components)
        # if the method requires optimized feature distribution
        if method == 'O':
            feature.reweight(Xtrain,X_pool_fraction,Lambda=Lambda)
        mylog.time_event('Gamma={0:.1e} and Lambda={1:.1e}\n'.format(Gamma,Lambda)
                         +'features generated')

        Xtraintil = feature.fit_transform(Xtrain)
        mylog.time_event('data transformed')

        # n_jobs is used for parallel computing 1 vs all;
        # -1 means all available cores
        clf = SGDClassifier(loss='hinge',penalty='l2',alpha=Lambda,
            tol=10**(-3),n_jobs=-1,warm_start=True)
        clf.fit(Xtraintil,Ytrain)
        mylog.time_event('training done')
        result['Gamma'].append(Gamma)
        result['Lambda'].append(Lambda)
        Xtesttil = feature.fit_transform(Xtest)
        Ypred = clf.predict(Xtesttil)
        score = np.sum(Ypred == Ytest) / len(Ytest)
        print('score = {:.4f}'.format(score))
        mylog.time_event('testing done')
        result['score'].append(score)
        if score > best_score:
            best_score = score
            best_Gamma = Gamma
            best_Lambda = Lambda
            best_Sampler = feature
            best_clf = clf

    # plot confusion matrix
    Xtesttil = best_Sampler.fit_transform(Xtest)
    Ypred = best_clf.predict(Xtesttil)
    C_matrix = confusion_matrix(Ytest,Ypred)
    fig = plt.figure()
    classes = range(10)
    plot_confusion_matrix(C_matrix,classes=classes,normalize=True)
    plt.savefig('image/{0:s}RFSVM_N_{1:.2e}_m_{2:.2e}.eps'.format(method,
        n_components,m))
    plt.close(fig)

    # write results and log files
    results = ('Best Gamma = {:.1e}\n'.format(best_Gamma)
               + 'Best Lambda = {:.1e}\n'.format(best_Lambda)
               + 'Classification Accuracy = {}\n'.format(score))
    print(results)
    results = results + 'Gamma    Lambda    score\n'
    for idx in range(len(result['Gamma'])):
        results = (results
                   + '{0:.1e}{1:9.1e}{2:10.4f}\n'.format(result['Gamma'][idx],
                                                         result['Lambda'][idx],
                                                         result['score'][idx]))
    mylog.record(results)
    mylog.save()
    return best_score

def KSVM_MNIST(m=1000):
    # set up timer and progress tracker
    mylog = log.log('log/KSVM_m_{:.2e}.log'.format(m),
        'KSVM MNIST classfication starts')
    Xtrain,Ytrain,Xtest,Ytest = get_train_test_data(train_size=m,
        test_size=int(m/3))
    mylog.time_event('data read in complete')

    # hyper-parameter selection
    best_score = 0
    best_Gamma = 1
    best_Lambda = 1
    result = {'Gamma':[],'Lambda':[],'score':[]}
    for jdx in range(len(LogLambda)):
        Lambda = 10**LogLambda[jdx]
        C = 1 / Lambda
        clf = svm.SVC(C=C,gamma=Gamma)
        clf.fit(Xtrain,Ytrain)
        mylog.time_event('training done')
        result['Gamma'].append(Gamma)
        result['Lambda'].append(Lambda)
        Ypred = clf.predict(Xtest)
        score = np.sum(Ypred == Ytest) / len(Ytest)
        print('score = {:.4f}'.format(score))
        mylog.time_event('testing done')
        result['score'].append(score)
        if score > best_score:
            best_score = score
            best_Gamma = Gamma
            best_Lambda = Lambda
            best_clf = clf

    # plot confusion matrix
    Ypred = best_clf.predict(Xtest)
    C_matrix = confusion_matrix(Ytest,Ypred)
    fig = plt.figure()
    classes = range(10)
    plot_confusion_matrix(C_matrix,classes=classes,normalize=True)
    plt.savefig('image/KSVM_m_{:.2e}.eps'.format(m))
    plt.close(fig)

    # write results and log files
    results = ('Best Gamma = {:.1e}\n'.format(best_Gamma)
               + 'Best Lambda = {:.1e}\n'.format(best_Lambda)
               + 'Classification Accuracy = {}\n'.format(score))
    print(results)
    results = results + 'Gamma    Lambda    score\n'
    for idx in range(len(result['Gamma'])):
        results = (results
                   + '{0:.1e}{1:9.1e}{2:10.4f}\n'.format(result['Gamma'][idx],
                                                         result['Lambda'][idx],
                                                         result['score'][idx]))
    mylog.record(results)
    mylog.save()
    return best_score

def main():
    prefix = argv[1]
    uscore_list = []
    oscore_list = []
    kscore_list = []
    for m in range(1000,60001,70000):
        score = RFSVM_MNIST(method='U',m=m,n_components=int(np.sqrt(m)))
        uscore_list.append(score)
        score = RFSVM_MNIST(method='O',m=m,n_components=int(np.sqrt(m)))
        oscore_list.append(score)
        score = KSVM_MNIST(m=m)
        kscore_list.append(score)
    np.savetxt('result/URFSVM'+str(prefix),np.array(uscore_list))
    np.savetxt('result/ORFSVM'+str(prefix),np.array(oscore_list))
    np.savetxt('result/KSVM'+str(prefix),np.array(kscore_list))

if __name__ == '__main__':
    main()


def main():
    x = int(input())
    numerator = 1
    denominator = 1
    i = 2
    result = 1

    #find out which column x belongs
    while(x > result):
        result = result + i
        i = i+1

    #if the i-1 is odd, decreasing e.g)15,14...
    #else, increasing e.g) 4,5,6...

    if((i-1)%2 != 0):
        diff = result - x
        denominator = (i-1) - diff
        numerator = 1 + diff

    else:
        diff = abs(result - x)
        denominator = denominator + diff
        numerator = i - 1 - diff

    print(str(numerator) + '/'+str(denominator))

main()


'''names=['Ram','Sita','Raghav','Nidhi','Raksh']
for name in names :
    print(name)
    if name=='Raksh':
        print("Hey its me !!")
name='RAKSHITHA'
for i in name:
    print(i)
 '''   '''
numbers=[20,-7,8,5,4]
squares=[]
for i in numbers:
    square=i**2
    squares.append(square)
print(" The list of sqyares is :",squares)'''

#for -else loop
numbers=[2,3,-5,1,0,53]
'''for i in numbers:
    print(i)
else:
    print("Sucessfully completed")'''
'''
for i in numbers:
    print(i)
    if i==53:
       break
else:
    print("Sucessfully completed")
print("Out of for-else loop")'''
for i in numbers:
    if i&6==0:
        print(i)
    else:
        print("There is no number divivsible by 6 !!")
else:
    print("WE are in for -else block now")


nome = input('Digite o seu nome: ')
print(f'Seu nome em minúsculo é : {nome.lower()}')
print(f'Seu nome em caixa alta é : {nome.upper()}')

lista = []
for char in nome:
    lista.append(char)
for elemento in lista:
    if elemento == ' ':
        lista.remove(elemento)

print(f'O tamanho do seu nome completo é : {len(lista)}')

sep = nome.split()

print(f'O tamanho do seu primeiro nome é : {len(sep[0])}')


from datetime import datetime
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import action
from Inventoryapi.models import Order, InventoryUser, OrderProduct
from Inventoryapi.models.product import Product
from Inventoryapi.serializers import OrderSerializer

class OrderView(ViewSet):

    def retrieve(self, request, pk):
        """Get a single Order"""
        try:
            order = Order.objects.filter(pk=pk)
            serializer = OrderSerializer(order)
            return Response(serializer.data)
        except Order.DoesNotExist as ex:
            return Response({'message': ex.args[0]}, status=status.HTTP_404_NOT_FOUND)

    def list(self, request):
        """Get a list of the current users orders
        """
        orders = Order.objects.all()
        serializer = OrderSerializer(orders, many=True)
        return Response(serializer.data)

    def destroy(self, request, pk):
        """Delete an order, current user must be associated with the order to be deleted
        """
        try:
            user = InventoryUser.objects.get(user=request.auth.user)
            order = Order.objects.get(pk=pk, user=user)
            order.delete()
            return Response(None, status=status.HTTP_204_NO_CONTENT)
        except Order.DoesNotExist as ex:
            return Response({'message': ex.args[0]}, status=status.HTTP_404_NOT_FOUND)

    @action(methods=['put'], detail=True)
    def complete(self, request, pk):
        """Complete an order by adding a payment type and completed data
        """
        try:
            user = InventoryUser.objects.get(user=request.auth.user)
            order = Order.objects.get(pk=pk, user=user)
            products = order.products.all()
            for product in products:
                product_inst = Product.objects.get(pk=product.id)
                order_product = OrderProduct.objects.get(product=product_inst, order=order)
                product_inst.quantity -= order_product.quantity
                product_inst.save()
            order.completed_on = datetime.now()
            order.save()
            return Response({'message': "Order Completed"})
        except Order.DoesNotExist as ex:
            return Response({'message': ex.args[0]}, status=status.HTTP_404_NOT_FOUND)

    @action(methods=['get'], detail=False)
    def current(self, request):
        """Get the user's current order"""
        try:
            user = InventoryUser.objects.get(user=request.auth.user)
            order = Order.objects.get(
                completed_on=None, user=user)
            order_products = OrderProduct.objects.filter(order=order)
            total = 0
            for order_product in order_products:
                total += order_product.total
            order.total = total
            serializer = OrderSerializer(order)
            return Response(serializer.data)
        except Order.DoesNotExist:
            return Response({
                'message': 'You do not have an open order. Add a product to the cart to get started'},
                status=status.HTTP_404_NOT_FOUND
            )


__author__ = 'heshuai'

import maya.cmds as mc

def set_render_camera(camera):
    cameras = mc.ls(type='camera')
    for cam in cameras:
        mc.setAttr(cam+'.renderable', 0)
    mc.setAttr(camera+'.renderable', 1)