# -*- coding: UTF-8 -*-
""" Provides a class derived from wxStyledTextCtrl for Galil DMC code syntax.
"""

__revision__ = "$Id$"

import re

import wx
import wx.stc as stc

import wx.py

# Style IDs
styles = { 'comment'  :  1,
           'label'    :  2,
           'autosub'  :  3,
           'separator':  4,
           'command'  :  5,
           'function' :  6,
           'operand'  :  7,
           'special'  :  8,
           'varaxis'  :  9,
           'digital'  : 10,
           'hexa'     : 11,
           'string'   : 12,
           'format'   : 13,
           'error'    : 30,    # for Galil Shell
           'debug'    : 31
           }

pattern_DMC_token = re.compile(
    r"""(?:                                             # label...
                (?:(?<=\A)                              # beginning at start of line...
                |(?<=\s)                                # or after a whitespace character...
                |(?<=DL|JP|JS|LS))                      # or after a jump instruction
                (?:
                (?P<autosub>\#(?:                       # automatic subroutines
                    AMPERR|AUTO|AUTOERR|CMDERR|COMINT|ININT|LIMSWI|MCTIME|POSERR|TCPERR))|
                (?P<label>\#[A-Za-z][\w_]{1,6})         # custom label
                )\b)|
    \b(?P<command>
                (?:DA|DL|ED|ELSE|ENDIF|IF|IN|JP|JS|NO|RA|RD|REM|UL)\b|
                (?:EN|RE|RI|ZS)(?=[01]?\b)|
                RC(?=[1-8]?\b)|
                XQ(?:(?=\d)|\b)|
                DM(?:(?=[A-Z])|\b)
    )|
    (?P<special>                                        # math/special functions
                (?:(?<=[\s=])|(?<=\A))
                @SIN|@COS|@TAN|@COM|@ASIN|@ACOS|@ATAN|@ABS|@FRAC|@INT|@RND|@SQR|@IN|@OUT)\b|
    \b(?P<function>
                (?:BN|BP|BV|LA|LE|LL|LV|MG|QD|QU|QZ|RS|TB|TH|TZ|VE|WC|WH)\b|
                CN(?=1?\b)|
                (?:AB|AE|BR|EB|EO|LZ|MW|PT|TC|TR)(?=[01]?\b)|
                (?:AG|CI|CW)(?=[012]?\b)|
                TA(?=[0-3]?\b)|
                HX(?=[0-7]?\b)|
                (?:DT|II)(?=[0-8]?\b)|
                AI(?=\d{,2}\b)|
                (?:AO|AT|AV|BK|CB|CC|CO|CR|DR|EC|EP|ES|ET|GD|IA|LS|OB|OP|PF|SB|SL|
                TI|TM|TN|VA|VD|VF|VP|VR|VS|VT|WT)(?=\d*\b)|
                MB(?=[A-F]?\b)|
                SA(?=[A-F]\b)|
                (?:CA|CS)(?=[ST]?\b)|
                IH(?=[A-H]\b)|
                HS(?=[A-HS]?\b)|
                CF(?=[A-HSI]?\b)|
                (?:OC|SI)(?=[A-HW-Z]\b)|
                VM(?=[A-HW-Z]?\b)|
                (?:BA|BC|CM|FE|FI|HM|LM|MC|MO|RP|SC|SH|TD|TE|TP|TS|TT|TV|TW)(?=[A-HNW-Z]|\b)|
                (?:AL|RL)(?=[A-HSW-Z]|\b)|
                (?:AW|BS)(?=[A-HSTW-Z]\b)|
                (?:AM|AS|BG|GA|ST)(?=[A-HNSTW-Z]|\b)|
                EA(?=[A-HNSTW-Z]?\b)|
                QR(?=[A-INSTW-Z]|\b)|
                AU(?:(?=[01]?\b)|(?=[A-HW-Z]\b))|
                (?:AF|DV|GM|LC|OE)(?:(?=[01]?\b)|(?=[A-HSTW-Z]\b))|
                MT(?:(?=[12]?\b)|(?=[A-HSTW-Z]\b))|
                CD(?:(?=\d)|(?=[A-HW-Z]?\b))|
                (?:AD|AP|AR|BB|BD|BI|BL|BM|BO|BZ|CE|DE|EG|EM|EQ|ER|FA|FV|FL|GR|IP|IL|
                KD|KI|KP|KS|LI|MF|MR|NB|NF|NZ|OF|PA|PL|PR|TK|TL)(?:(?=\d)|(?=[A-HSTW-Z]?\b))|
                (?:AC|DC|DP|IT|JG|SP)(?:(?=\d)|(?=[A-HNSTW-Z]?\b))
    )|
    \b(?P<operand>
                _(?:
                (?:AB|BN|BV|BK|CA|CF|CO|CM|CW|DA|DL|DR|DT|DM|EB|EC|EP|OC|PF|
                LZ|RC|RS|TB|TC|TM|TN|VF)\b|
                ED1?\b|
                TA[0-3]\b|
                (?:CN|OP)[0-4]\b|
                IA[0-5]\b|
                (?:HX|XQ)[0-7]?\b|
                TI\d{,2}\b|
                (?:AV|LE|LM|VA|VD|VE|VM|VP|VR|VS|VT)[ST]\b|
                (?:AF|AL|RL)(?:[A-HW-Z]|(?=~[a-h]))\b|
                (?:AU|BA|BB|BC|BD|BI|BL|BM|BO|BZ|CE|DE|DP|DV|EG|EM|EQ|ER|FA|
                FL|FV|GM|GP|HM|IL|KD|KI|KP|KS|LF|LR|MT|MO|NB|NF|NZ|QH|LC|OE|OF|
                PA|PL|PR|RP|SC|SI|TD|TE|TK|TL|TP|TS|TT|TV|TW|VP)(?:[A-HNW-Z]|(?=~[a-h]))\b|
                GR(?:[A-HSTW-Z]|(?=~[a-h]))\b|
                (?:AC|BG|DC|IT|JG|SP)(?:[A-HNSTW-Z]|(?=~[a-h]))\b|
                ZS[0123ABCD]\b|
                IH[A-H][0-3]|
                SA[A-F][0-7]|
                CS\w{0,5}\b
                )|
                TIME\b
    )|
    (?P<varaxis>~[a-h]\b)|                              # variable axis designator
    (?P<separator>;)|                                   # command separator
    (?P<hexa>\$[\dA-F]+)|                               # hexa number
    (?P<digital>[+\-]?\d+\.?\d*)|                       # decimal number
    (?P<string>".*?(?:"|$|(?=;)))|                      # string
    (?P<comment>(?:(?<=^)|(?<=;))\s*'[^;]*)|            # comment
    (?P<format>{.+?})|                                  # format
    \s+|
    \w+|
    .+?                                                 # non-greedy other stuff
    """,
    re.VERBOSE | re.LOCALE | re.DOTALL)

patter_syntax_error = re.compile(
    r"""(?P<goodstr>"[^";\n]*?")|    # string, it is good
    (?P<bad>
    (\~[^a-h])|                      # bad axis designator
    (?<=\b[A-Z]{2})(~[a-h]\s+=)|     # no space between axis designator and equal sign
                                     # in value affectation
    ("[^"]*?(?!")(?=;|$))|           # unclosed string
    (^[^\x0d\x0a]{80,})              # line too long
    )|
    (?P<good>.+?)
    """,
    re.VERBOSE | re.LOCALE | re.MULTILINE)

def get_tokens(text):
    """ Generator that retrieves the tokens from a DMC code string.

    @param text: string from which to retrieve tokens
    @type text: str

    @return: delimiting indices of the token in the string and associated style : (i, j), id
    @rtype: tuple
    """
    for mobj in pattern_DMC_token.finditer(text):
        name = mobj.lastgroup
        if name in styles:
            yield mobj.span(name), styles[name]
        else:
            yield mobj.span(), stc.STC_STYLE_DEFAULT

class GalilSTC(wx.py.editor.EditWindow):
    """ Class supporting displaying/editing of Galil DMC code syntax.
    """
    def __init__(self, parent, ID, shell=False,
                 pos=wx.DefaultPosition, size=wx.DefaultSize, style=0):
        """ Create an instance of wxStyledTextCtrl specialized for Galil DMC code.
        """
        # Do not call wx.py.editor.EditWindow.__init__()
        # to avoid call of wx.py.editor.EditWindow.__config
        stc.StyledTextCtrl.__init__(self, parent, ID, pos, size, style)

        # Flag to know if the class is used in the context of a Galil Shell
        self.__shell = shell

        # Make some key mapping
        self.CmdKeyAssign(ord('+'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMIN)
        self.CmdKeyAssign(ord('-'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMOUT)

        self.SetLexer(stc.STC_LEX_CONTAINER)
        self.Bind(stc.EVT_STC_STYLENEEDED, self.OnStyleNeeded)
        self.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI)
        self.Bind(wx.EVT_SCROLLWIN, self.OnScrollWin)
        self.Bind(wx.EVT_IDLE, self.OnIdle)

        self.SetViewWhiteSpace(False)
        self.SetTabWidth(4)
        self.SetEdgeMode(stc.STC_EDGE_NONE)

        # Create lineNumbers attribute, indispensable
        # for line numbering in an EditorFrame instance
        self.setDisplayLineNumbers(False)

        self.setStyles(wx.py.editwindow.FACES)

        self.SetCaretWidth(2)
        self.SetCaretForeground("BLUE")

        self.__checked_text = None    # last syntax-error checked text

    def setStyles(self, faces):
        """ Configure font size, typeface and color for lexer.
        """
        # Set the attributes of default styles
        self.StyleSetSpec(stc.STC_STYLE_DEFAULT,
                          "fore:#000000,back:%(backcol)s,face:%(mono)s,size:%(size)d"
                          % faces)
        self.StyleClearAll()  # reset all to be like the default
        self.StyleSetSpec(stc.STC_STYLE_LINENUMBER,
                          "fore:#717171,back:#E0E0E0,face:%(helv)s,size:%(lnsize)d"
                          % faces)
        self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT, "fore:#00CC00,back:#CCFF99,bold")
        self.StyleSetSpec(stc.STC_STYLE_BRACEBAD, "fore:#FF0000,back:#FFCCCC,bold")

        # Set the attributes of custom styles
        self.StyleSetSpec(styles['comment'], "fore:#AAAAAA,bold")
        self.StyleSetSpec(styles['label'], "fore:#C00000,bold")
        self.StyleSetSpec(styles['autosub'], "fore:#FF8000,bold")
        self.StyleSetSpec(styles['separator'], "fore:#00E000,bold")
        self.StyleSetSpec(styles['command'], "fore:#3333FF,bold")
        self.StyleSetSpec(styles['function'], "fore:#000000,bold")
        self.StyleSetSpec(styles['operand'], "fore:#9933FF,bold")
        self.StyleSetSpec(styles['varaxis'], "fore:#CC00CC,bold")
        self.StyleSetSpec(styles['digital'], "fore:#8000FF,normal")
        self.StyleSetSpec(styles['hexa'], "fore:#CC6600,normal")
        self.StyleSetSpec(styles['string'], "fore:#00B200,normal")
        self.StyleSetSpec(styles['format'], "fore:#009999,normal")
        self.StyleSetSpec(styles['special'], "fore:#009999,bold")
        self.StyleSetSpec(styles['error'], "fore:#FF0000,normal")
        #self.StyleSetSpec(styles['debug'], "fore:#FFFFFF,back:#0000FF,bold")

        self.IndicatorSetStyle(0, stc.STC_INDIC_SQUIGGLE)
        self.IndicatorSetForeground(0, wx.RED)

    def OnStyleNeeded(self, evt):
        """ Called when styling is needed.
        """
        #position = evt.GetPosition()
        pos_end_styled = self.GetEndStyled()
        line_number = self.LineFromPosition(pos_end_styled)
        pos_start_line = self.PositionFromLine(line_number)
        line = self.GetLine(line_number)

        if line:

            if self.__shell and line.startswith("Error: "):
                # Do styling for error display in a Galil Shell
                self.StartStyling(pos_start_line, 0x1f)
                self.SetStyling(len(line), styles['error'])
            else:
                # Do styling for DMC code
                self.StartStyling(pos_start_line, 0x1f)
                for span, style in get_tokens(line):
                    self.SetStyling(span[1] - span[0], style)

    def OnUpdateUI(self, evt):
        """ Check for matching braces and force styling (after, for instance, text pasting).
        """
        # Check for matching braces
        braceAtCaret = -1
        braceOpposite = -1
        charBefore = None
        caretPos = self.GetCurrentPos()
        if caretPos > 0:
            charBefore = self.GetCharAt(caretPos - 1)
            styleBefore = self.GetStyleAt(caretPos - 1)
        # check before
        if charBefore and chr(charBefore) in "[]{}()":
            braceAtCaret = caretPos - 1
        # check after
        if braceAtCaret < 0:
            charAfter = self.GetCharAt(caretPos)
            styleAfter = self.GetStyleAt(caretPos)
            if charAfter and chr(charAfter) in "[]{}()":
                braceAtCaret = caretPos
        if braceAtCaret >= 0:
            braceOpposite = self.BraceMatch(braceAtCaret)
        if braceAtCaret != -1  and braceOpposite == -1:
            self.BraceBadLight(braceAtCaret)
        else:
            self.BraceHighlight(braceAtCaret, braceOpposite)

        # Force styling
        self.Refresh()

    def OnIdle(self, evt):
        """ Detecting and showing syntax error.
        """
        pos_end_styled = self.GetEndStyled()
        pos_end_of_line = self.GetLineEndPosition(self.LineFromPosition(pos_end_styled))
        text = self.GetTextRange(0, pos_end_of_line)
        if text != self.__checked_text:
            for mobj in re.finditer(patter_syntax_error, text):
                start, end = mobj.span()
                self.StartStyling(start, stc.STC_INDICS_MASK)
                #print mobj.lastgroup, mobj.group(mobj.lastgroup)
                if mobj.lastgroup == 'bad':
                    style = stc.STC_INDIC0_MASK
                else:
                    style = stc.STC_INDIC_PLAIN
                self.SetStyling(end - start, style)
            self.__checked_text = text

    def OnScrollWin(self, evt):
        """ Force styling.
        """
        wx.CallAfter(self.Refresh)
        evt.Skip()

if __name__ == '__main__':

    app = wx.PySimpleApp()
    frame = wx.Frame(None)
    editor = GalilSTC(frame, -1)
    editor.SetMarginType(1, stc.STC_MARGIN_NUMBER)
    editor.SetMarginWidth(1, 25)
    sizer = wx.BoxSizer(wx.HORIZONTAL)
    sizer.Add(editor, 1, wx.EXPAND)
    frame.SetSizer(sizer)
    frame.Show(True)
    app.MainLoop()


# Remote Control - Transmit
#   for use in the Sparkfun Thing Plus RP2040 board
#
# Default transmitter settings:
#   power level: 0 dB (maximum)
#   transmit address: b"1Node"
#   receive address:  b"2Node"
#
# nm3210@gmail.com
# Date Created:  April 17th, 2021
# Last Modified: October 10th, 2021

# Import modules
import board, bitbangio, digitalio, struct, time, random # circuitpython built-ins
from math import atan2, acos, sqrt, pi # necessary math calls
import adafruit_mpu6050 # also requires adafruit_register
import neopixel # also requires adafruit_pypixelbuf
from circuitpython_nrf24l01.rf24 import RF24
from lib.ColorDescriptors.ColorDescriptors import *
from lib.EasyStreamNrf24.EasyStreamNrf24 import sendPayload
print("Finished importing modules")

### Initialize nRF24L01
# Configure pinouts
ce = digitalio.DigitalInOut(board.D0)
csn = digitalio.DigitalInOut(board.D1)
spi = board.SPI() # init spi bus w/ pins D[2,3,4]
# irq = digitalio.DigitalInOut(board.D6) # optional IRQ pin to listen to interrupts

# Initialize object
nrf = RF24(spi, csn, ce)

# Configure settings
# irq.switch_to_input()  # make sure its an input object
nrf.pa_level = 0 # maximum

# Select tx/rx addresses
txAddress = b"1Node" # transmit module tx pipe = '1Node'
rxAddress = b"2Node" # transmit module rx pipe = '2Node'
nrf.open_tx_pipe(txAddress)
nrf.open_rx_pipe(1, rxAddress)

# Set default state to 'off'
nrf.listen = False
nrf.power = False
print("Finished initializing nRF24 module")

### Initialize neopixel output
ledPin = board.NEOPIXEL
pixelMain = neopixel.NeoPixel(ledPin, 1, pixel_order=neopixel.GRB)
pixelMain.fill((0,0,0)) # turn off on startup
print("Finished initializing neopixel")

### Initialize MPU6050 sensor
i2c = board.I2C();

# Initialize MPU6050 object
sensor = adafruit_mpu6050.MPU6050(i2c)
sensor.cycle_rate = adafruit_mpu6050.Rate.CYCLE_40_HZ # update cycle rate
sensor.cycle = True # only periodically update sensor (saves power!)
print("Finished initializing mpu6050")

# Setup calibrated accel values
numAvgValues = 7
listAccelX = [None] * numAvgValues
listAccelY = [None] * numAvgValues
listAccelZ = [None] * numAvgValues
listFaceIdx = [0] * numAvgValues

### Other things
# Setup some storage vars
lastFace = 0

# Configure timers
timeCheck_faceIdx = time.monotonic_ns()
updateTime_faceIdx = 1.1/40 # seconds, enough time for the 40 Hz to update

timeCheck_changes = time.monotonic_ns()
updateTime_changes = 0.01 # seconds

timeCheck_autosend = time.monotonic_ns()
updateTime_autosend = 1.0 # always send an update every once in a while

### Set up colors (preallocate)
# Solid colors
solidRed     = ColorMethod(ModeStationary, ColorRed)
solidYellow  = ColorMethod(ModeStationary, ColorYellow)
solidGreen   = ColorMethod(ModeStationary, ColorGreen)
solidCyan    = ColorMethod(ModeStationary, ColorCyan)
solidBlue    = ColorMethod(ModeStationary, ColorBlue)
solidMagenta = ColorMethod(ModeStationary, ColorMagenta)
solidWhite   = ColorMethod(ModeStationary, ColorWhite)
solidOff     = ColorMethod(ModeStationary, ColorOff)

# Gradients
gradientRainbowShort = ColorGradient([ColorRed, ColorYellow, ColorGreen, ColorCyan, ColorBlue, ColorMagenta, ColorSolid(hue=360)])
rainbowShort = ColorMethod(ModeStationary, gradientRainbowShort)
gradientRainbowMedium = ColorGradient([ColorRed, ColorYellow, ColorGreen, ColorCyan, ColorBlue, ColorMagenta, ColorSolid(hue=360)],1)
rainbowMedium = ColorMethod(ModeStationary, gradientRainbowMedium)

### Private functions
def getDownwardFaceIndex():
    # Collect sensor updates
    theta, phi = getAccelTiltAngle() # in degrees

    # Determine sides of the platonic cube
    angleCheck = 20
    return getPlatonicCubeFaceIdx(theta, phi, angleCheck)

def getPlatonicCubeFaceIdx(theta, phi, angleCheck):
    # Determine sides of the platonic cube
    faceIdx = 0 # invalid face
    angleDiff = lambda start, stop : ((start-stop)+180)%360 - 180
    if   abs(angleDiff(theta,  0)) <= angleCheck and abs(angleDiff(phi, 90)) <= angleCheck: # Down
        faceIdx = 1
    elif abs(angleDiff(theta,180)) <= angleCheck and abs(angleDiff(phi, 90)) <= angleCheck: # Up
        faceIdx = 2
    elif abs(angleDiff(theta,-90)) <= angleCheck and abs(angleDiff(phi, 90)) <= angleCheck: # Side 1
        faceIdx = 3
    elif abs(angleDiff(theta, 90)) <= angleCheck and abs(angleDiff(phi, 90)) <= angleCheck: # Side 2
        faceIdx = 4
    elif abs(angleDiff(phi,    0)) <= angleCheck: # Side 3, no theta check necessary because it's in a singularity
        faceIdx = 5
    elif abs(angleDiff(phi,  180)) <= angleCheck: # Side 4, no theta check necessary because it's in a singularity
        faceIdx = 6
    else:
        pass
    return faceIdx

def getAccelTiltAngle():
    # via https://www.analog.com/en/app-notes/an-1057.html eq 9 & 10
    x, y, z = getSmoothedAccel()
    theta = atan2(y, x) # (-180,180)
    phi = acos(z / sqrt(x*x + y*y + z*z)) # (0,180)
    return theta*180/pi, phi*180/pi # -> deg

def getSmoothedAccel():
    preallocateAccelList() # first time only, via simply none check

    # Update the sensor
    updateAccelList()

    # Calculate the 'moving' average
    x = sum(listAccelX) / len(listAccelX)
    y = sum(listAccelY) / len(listAccelY)
    z = sum(listAccelZ) / len(listAccelZ)
    return x, y, z

def updateAccelList():
    """
    Updates the acceleration lists with a new sensor value
    """
    # Get new sensor update/s
    x, y, z = getSensorAccel()

    # Pop out old values (idx 0)
    listAccelX.pop(0) # ignore outgoing value
    listAccelY.pop(0) # ignore outgoing value
    listAccelZ.pop(0) # ignore outgoing value

    # Append the new values
    listAccelX.append(x)
    listAccelY.append(y)
    listAccelZ.append(z)

def getSmoothedFaceIdx():
    if not all(ele == listFaceIdx[0] for ele in listFaceIdx):
        return 0 # ALL elements need to be identical to return a valid value
    return listFaceIdx[0]

def updateFaceIdx():
    _faceIdx = getDownwardFaceIndex()
    listFaceIdx.pop(0) # remove an entry (ignore outgoing value)
    listFaceIdx.append(_faceIdx) # add the new index

def preallocateAccelList():
    # Check if there are any none's to replace
    while (None in listAccelX or None in listAccelY or None in listAccelY):
        updateAccelList()
        time.sleep(1.1/40) # wait for the sensor to update

def getSensorAccel():
    x, y, z = sensor.acceleration
    return x, y, z

def anyChanges():
    global lastFace
    currentFace = getSmoothedFaceIdx()
    if currentFace != 0 and currentFace != lastFace:
        lastFace = currentFace
        return True
    return False

def lookupFaceMethod(faceVal):
    if   faceVal == 1: # TOP Die Val = '2'
        return solidRed
    elif faceVal == 2: # TOP Die Val = '5'
        return solidYellow
    elif faceVal == 3: # TOP Die Val = '4'
        return solidGreen
    elif faceVal == 4: # TOP Die Val = '3'
        return solidCyan
    elif faceVal == 5: # TOP Die Val = '1'
        return solidBlue
    elif faceVal == 6: # TOP Die Val = '6'
        return solidMagenta
    else:
        return None

def getPayload():
    global lastFace
    faceMethod = lookupFaceMethod(lastFace)
    if faceMethod is None: return None
    return faceMethod.toString()

###
# Main LOOP
print("Starting main loop for Remote Control - Transmit...")
while True:
    ### Check timers
    # Update FaceIdx
    if abs(time.monotonic_ns() - timeCheck_faceIdx) > updateTime_faceIdx*1e9:
        timeCheck_faceIdx = time.monotonic_ns() # reset timer
        updateFaceIdx()

    # Check for any face index changes
    detectedChanges = False
    if abs(time.monotonic_ns() - timeCheck_changes) > updateTime_changes*1e9:
        timeCheck_changes = time.monotonic_ns() # reset timer
        detectedChanges = anyChanges()

    # Send an update if any changes or a timeout has been reached
    if lastFace != 0 and (detectedChanges or abs(time.monotonic_ns() - timeCheck_autosend) > updateTime_autosend*1e9):
        timeCheck_autosend = time.monotonic_ns() # reset timer
        curPayload = getPayload()
        if curPayload is not None:
            sendPayload(nrf, getPayload(), debugPrint=False)


from pathlib import Path
from torch.utils.data import DataLoader, Dataset
from PIL import Image
from torchvision.transforms import transforms
from matplotlib import pyplot as plt
import torchvision
from torch import nn
import torch
import shutil
from zipfile import ZipFile
import zipfile

class MasterData(Dataset):
    def __init__(self, data_root, transform=None,scale_transform=None,grayTransform=None,f1=Path('bg/'), f2=Path('fg_bg/'), f3=Path('mask_fg_bg/') ,f4=Path('depth/')):
        self.f1_files = list(f1.glob('*.jpeg'))
        self.f2_files = list(f2.glob('*.jpeg'))
        self.f3_files = list(f3.glob('*.jpeg'))
        self.f4_files = list(f4.glob('*.jpeg'))
        self.transform = transform
        self.scale_transform = scale_transform
        self.grayTransform = grayTransform

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

    def __getitem__(self, index):
        f1_image = Image.open(self.f1_files[index])
        f2_image = Image.open(self.f2_files[index])
        f3_image = Image.open(self.f3_files[index])
        f4_image = Image.open(self.f4_files[index])
        #do transformations here
        if self.transform:
            f1_image = self.transform(f1_image)
            f2_image = self.transform(f2_image)
            f3_image = self.grayTransform(f3_image)
            f4_image = self.grayTransform(f4_image)
        return {'f1': f1_image, 'f2': f2_image, 'f3': f3_image, 'f4': f4_image}

def importDataset():
    shutil.copy('/content/drive/My Drive/bg_fg_bg_mask_2.zip','bg_fg_bg_mask_2.zip')
    shutil.copy('/content/drive/My Drive/Session15/copy_depth_output.zip','copy_depth_output.zip')
    zip = ZipFile('bg_fg_bg_mask_2.zip')
    zip.extractall('./')
    zip = ZipFile('copy_depth_output.zip')
    zip.extractall('./')
    data_root = Path('.')
    f1, f2, f3 ,f4 = data_root/'bg', data_root/'fg_bg', data_root/'mask_fg_bg', data_root/'depth'
    print(len(list(f1.iterdir())))
    print(len(list(f2.iterdir())))
    print(len(list(f3.iterdir())))
    print(len(list(f4.iterdir())))
    scale_transform = transforms.Compose([
    transforms.Resize((128, 128)),
    #transforms.ColorJitter(brightness=0.2, contrast = 0.2, saturation = 0.2, hue = 0.2),
    transforms.ToTensor(),
    ])
    grayTransform  = transforms.Compose([
        transforms.Grayscale(num_output_channels=1),
        transforms.Resize((128, 128)),
        #transforms.ColorJitter(brightness=0.2, contrast = 0.2, saturation = 0.2, hue = 0.2),
        transforms.ToTensor(),
      ])
    mean, std = torch.tensor([0.485, 0.456, 0.406])*255, torch.tensor([0.229, 0.224, 0.225])*255
    train_transforms = transforms.Compose([
        transforms.Resize((128, 128)),
        transforms.ColorJitter(brightness=0.15, contrast=0.15, saturation=0.15, hue= 0.15),
        transforms.ToTensor(),
        transforms.Normalize(mean, std)
    ])
    train_ds = MasterData(data_root, train_transforms, scale_transform)
    train_d1 = DataLoader(train_ds, batch_size=16, shuffle=True, pin_memory=True)
    return train_d1


# -*- coding: utf-8 -*-

import json
import logging as log
import md5
from datetime import datetime
from dateutil.parser import parse

try:
    from scrapy.spiders import Spider
except:
    from scrapy.spiders import BaseSpider as Spider
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy import Request

from CommonSpider import CommonSpider
from crawler.model.config import DBSession
from crawler.model.models import ApiSeed
from crawler.items import IkmItem

class jsonSpider(CommonSpider):
    '''JSON抓取'''

    name = "jsonSpider"
    def __init__(self, debug_rule=None, *args, **kwargs):

        if debug_rule == None:
            db = DBSession()
            rules = db.query(ApiSeed).filter(ApiSeed.id == kwargs["id"])
            for rule in rules:
                log.info("rule is:" + rule.name)
                self.rule = rule
            log.info("rule name is:" + self.rule.name)
        else:
            self.rule = debug_rule

        self.start_urls = self.rule.start_urls.split(",")
        self.name = self.rule.name
        super(jsonSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        '''抓取'''
        self.log("url: %s" % response.url)

        hjson = json.loads(response.body)
        itemlist = hjson[self.rule.items_css]
        for i in itemlist:

            item = IkmItem()
            item["url"] = i[self.rule.link_css]
            md5_tools = md5.new()
            md5_tools.update(item["url"])
            item["id"] = md5_tools.hexdigest()
            item["title"] = i[self.rule.title_css]
            item["title"] = i[self.rule.title_css]
            item["content"] = i[self.rule.content_css]
            item["medianame"] = self.rule.name

            pub_date = i[self.rule.pub_date_css]
            if pub_date:
                try:
                    item["pub_date"] = parse(pub_date[0], fuzzy=True)
                except:
                     # log.info("正文日期与url日期同时解析错误！")
                    item["pub_date"] = datetime.now()

            yield item


import numpy as np
import torch
from tqdm import tqdm
from pro.Loss import criterion_KL,criterion_TV,criterion_L2

lsmx = torch.nn.LogSoftmax(dim=1)
dtype = torch.cuda.FloatTensor

def validate(model, val_loader, n_iter, val_loss, params, logWriter):

    model.eval()

    l_all = []
    l_kl = []
    l_tv = []
    l_rmse = []

    for sample in tqdm(val_loader):
        M_mea = sample["spad"].type(dtype)
        M_gt = sample["rates"].type(dtype)
        dep = sample["bins"].type(dtype)

        M_mea_re, dep_re = model(M_mea)
        M_mea_re_lsmx = lsmx(M_mea_re).unsqueeze(1)
        loss_kl = criterion_KL(M_mea_re_lsmx, M_gt).data.cpu().numpy()
        loss_tv = criterion_TV(dep_re).data.cpu().numpy()
        rmse = criterion_L2(dep_re, dep).data.cpu().numpy()
        loss = loss_kl + params["p_tv"]*loss_tv

        l_all.append(loss)
        l_kl.append(loss_kl)
        l_tv.append(loss_tv)
        l_rmse.append(rmse)

    # log the val losses
    logWriter.add_scalar("loss_val/all", np.mean(l_all), n_iter)
    logWriter.add_scalar("loss_val/kl", np.mean(l_kl), n_iter)
    logWriter.add_scalar("loss_val/tv", np.mean(l_tv), n_iter)
    logWriter.add_scalar("loss_val/rmse", np.mean(l_rmse), n_iter)
    val_loss["KL"].append(np.mean(l_kl))
    val_loss["TV"].append(np.mean(l_tv))
    val_loss["RMSE"].append(np.mean(l_rmse))

    return val_loss, logWriter


from mock import patch

import pytest
from fakeredis import FakeStrictRedis
from nameko.containers import ServiceContainer
from nameko.testing.services import entrypoint_hook, worker_factory

from temp_messenger.dependencies.redis import RedisError
from temp_messenger.service import MessageService, WebServer

@pytest.fixture
def fake_strict_redis():
    with patch('temp_messenger.dependencies.redis.StrictRedis', FakeStrictRedis) as r:
        yield r

@pytest.fixture
def config(web_config):
    return dict(
        AMQP_URI='pyamqp://guest:guest@localhost',
        REDIS_URL='redis://redis:6379/0',
        **web_config
    )

def test_konnichiwa():
    service = worker_factory(MessageService)

    result = service.konnichiwa()

    assert result == 'Konnichiwa!'

def test_get_message_http(
    web_session, config, container_factory, fake_strict_redis
):
    fake_strict_redis().set('message-1', 'Test message here!')
    message_svc = container_factory(MessageService, config)
    web_server = container_factory(WebServer, config)
    message_svc.start()
    web_server.start()

    result = web_session.get('/message-1')

    assert result.text == 'Test message here!'

def test_gets_message(fake_strict_redis, config):
    fake_strict_redis().set('message-1', 'Test message here!')
    fake_strict_redis().set('message-2', '(╯°□°）╯︵ ┻━┻)')

    container = ServiceContainer(MessageService, config)
    container.start()

    with entrypoint_hook(container, 'get_message') as get_message:
        message_1 = get_message('message-1')
        message_2 = get_message('message-2')

    assert message_1 == 'Test message here!'
    assert message_2 == '(╯°□°）╯︵ ┻━┻)'

def test_raises_error_if_message_does_not_exist(
    fake_strict_redis, config
):
    container = ServiceContainer(MessageService, config)
    container.start()

    with entrypoint_hook(container, 'get_message') as get_message:
        with pytest.raises(RedisError) as err:
            get_message('message-x')

    err.match(r'Message not found: message-x')


import logging
import fuel_fetcher
import bot_settings
import movie_fetcher
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, RegexHandler

def start(bot, update):
    keyboard = [
        [InlineKeyboardButton("Fuel Price (Petrol)", callback_data='1')],
                # [InlineKeyboardButton("Weather", callback_data='2')],
                [InlineKeyboardButton("Movie", callback_data='3')]
    ]
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text('Please choose:', reply_markup=reply_markup)

def button(bot, update):
    query = update.callback_query

    # bot.editMessageText(text="Selected option: %s" % query.data,
    #                     chat_id=query.message.chat_id,
    #                     message_id=query.message.message_id)
    if query.data == '1':
        keyboard2 = [[InlineKeyboardButton("Home", callback_data='4')],
                     [InlineKeyboardButton("Work", callback_data='5')],
                     [InlineKeyboardButton("Tamworth", callback_data='6')],
                     [InlineKeyboardButton("Caste Vale", callback_data='7')],
                     [InlineKeyboardButton("Somewhere else", callback_data='8')]]
        reply_markup2 = InlineKeyboardMarkup(keyboard2)
        bot.editMessageText(text='Please choose where to check fuel:',
                            reply_markup=reply_markup2,
                            chat_id=query.message.chat_id,
                            message_id=query.message.message_id)

    if query.data == '3':
        result = 'Under development'
        bot.editMessageText(text=result,
                            chat_id=query.message.chat_id,
                            message_id=query.message.message_id)

    if query.data == '4':
        location = 'de735ax'
        result = str(fuel_fetcher.get_fuel_price(location))
        bot.editMessageText(text=result,
                            chat_id=query.message.chat_id,
                            message_id=query.message.message_id)

    if query.data == '5':
        location = 'b152tt'
        result = str(fuel_fetcher.get_fuel_price(location))
        bot.editMessageText(text=result,
                            chat_id=query.message.chat_id,
                            message_id=query.message.message_id)
    if query.data == '6':
        location = 'tamworth'
        result = str(fuel_fetcher.get_fuel_price(location))
        bot.editMessageText(text=result,
                            chat_id=query.message.chat_id,
                            message_id=query.message.message_id)
    if query.data == '7':
        location = 'castle+vale'
        result = str(fuel_fetcher.get_fuel_price(location))
        bot.editMessageText(text=result,
                            chat_id=query.message.chat_id,
                            message_id=query.message.message_id)

    if query.data == '8':
        bot.editMessageText(text='Please reply me the postcode:',
                            chat_id=query.message.chat_id,
                            message_id=query.message.message_id)

def fuel_on_postcode(bot, update):
    location = update.message.text
    # fuelFetcher.set_location(location)
    result = str(fuel_fetcher.get_fuel_price(location))
    update.message.reply_text(result)

def help(bot, update):
    update.message.reply_text("Use /start to test this bot.")

def error(bot, update, error):
    logging.warning('Update "%s" caused error "%s"' % (update, error))

postcode_regex = '([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\s?[0-9][A-Za-z]{2})'

# Create the Updater and pass it your bot's token.
updater = Updater(bot_settings.token)

updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.dispatcher.add_handler(RegexHandler(postcode_regex, fuel_on_postcode))
updater.dispatcher.add_handler(CommandHandler('help', help))
updater.dispatcher.add_error_handler(error)

# Start the Bot
updater.start_polling()

# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT
updater.idle()


from django.urls import path, include
from .views import ( UserRegister,
                     UserLogin,
                     GetUserData,
                     SetUserData,
                     GetUserCar,
                     SetUserCar,
                     UserTokenRefresh,
                     GetUserExtraDetails,
                     GetAnyUsersDisplayData,
                     SetNameAndPic,
                     DeleteUserCar
                   )

urlpatterns = [

    path('register/', UserRegister.as_view()), # Refactor to include() later
    path('login/', UserLogin.as_view()),
    path('refresh_token/', UserTokenRefresh.as_view()),
    path('get_user_data/', GetUserData.as_view()),
    path('set_user_data/', SetUserData.as_view()),
    path('set_car_details/', SetUserCar.as_view()),
    path('delete_car_details/', DeleteUserCar.as_view()),
    path('set_name_and_pic/', SetNameAndPic.as_view()),
    path('get_car_details/', GetUserCar.as_view()),
    path('get_all_details/', GetUserExtraDetails.as_view()),

    path('get_any_user_display_data/<str:user_id>', GetAnyUsersDisplayData.as_view()),

]


import ptah
from pyramid.testing import DummyRequest
from pyramid.httpexceptions import HTTPNotFound

from base import Base

class TestLayoutPreview(Base):

    def setUp(self):
        self._setup_pyramid()

    def test_layout_preview_notfound(self):
        from ptah.manage.layout import LayoutPreview

        request = DummyRequest()

        v = LayoutPreview(None,request)
        self.assertRaises(HTTPNotFound, v.update)

    def test_layout_preview_notfound2(self):
        from ptah.manage.layout import LayoutPreview

        class Context(object):
            """ """

        global View
        class View(object):
            ptah.view.pview('', Context)
            def render(self):
                return 'test' # pragma: no cover

        self._init_ptah()

        request = DummyRequest()

        v = LayoutPreview(Context(), request)
        self.assertRaises(HTTPNotFound, v.update)

    def test_layout_preview(self):
        from ptah.manage.layout import LayoutPreview

        class Context(object):
            """ """
            __name__ = 'test'

        global View
        class View(ptah.view.View):
            ptah.view.pview('', Context)
            def render(self):
                return 'test' # pragma: no cover

        global Layout
        class Layout(ptah.view.Layout):
            ptah.view.layout('', Context, parent=None)

            def render(self, content):
                return '
%s
'%content

        self._init_ptah()

        request = DummyRequest()

        v = LayoutPreview(Context(), request)
        v.update()
        res = v.render()

        self.assertIn('
', res)
        self.assertIn('ptah.manage.tests.test_layout.Layout', res)
        self.assertIn('ptah.manage.tests.test_layout', res)


import glfw
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
from OpenGL.arrays import vbo
import ctypes

gCamAng = 0.
gCamHeight = 1.

def slerp(R1, R2, t):
    return R1 @ exp(t * log(R1.T @ R2))

def interpolateRotVec(rv1, rv2, t):
    rv = lerp(rv1, rv2, t)
    return exp(rv)

def interpolateZYXEuler(euler1, euler2, t):
    euler = lerp(euler1, euler2, t)
    return ZYXEulerToRotMat(euler)

def interpolateRotMat(R1, R2, t):
    return lerp(R1, R2, t)

def exp(rv):
    theta = np.sqrt(np.sum(rv ** 2))
    rv = rv / theta

    R = np.array([[  np.cos(theta) + (rv[0] ** 2) * (1 - np.cos(theta)),
                     rv[0] * rv[1] * (1 - np.cos(theta)) - rv[2] * np.sin(theta),
                     rv[0] * rv[2] * (1 - np.cos(theta)) + rv[1] * np.sin(theta)  ],
                  [  rv[1] * rv[0] * (1 - np.cos(theta)) + rv[2] * np.sin(theta),
                     np.cos(theta) + (rv[1] ** 2) * (1 - np.cos(theta)),
                     rv[1] * rv[2] * (1 - np.cos(theta)) - rv[0] * np.sin(theta)  ],
                  [  rv[2] * rv[0] * (1 - np.cos(theta)) - rv[1] * np.sin(theta),
                     rv[2] * rv[1] * (1 - np.cos(theta)) + rv[0] * np.sin(theta),
                     np.cos(theta) + (rv[2] ** 2) * (1 - np.cos(theta))           ]])

    return R

def log(R):
    theta = np.arccos((R[0][0] + R[1][1] + R[2][2] - 1) / 2)
    v = np.array([(R[2][1] - R[1][2]) / (2 * np.sin(theta)),
                  (R[0][2] - R[2][0]) / (2 * np.sin(theta)),
                  (R[1][0] - R[0][1]) / (2 * np.sin(theta))])
    return v * theta

def createVertexArraySeparate():
    varr = np.array([
            [0,1,0],            # v0 normal
            [ 0.5, 0.5,-0.5],   # v0 position
            [0,1,0],            # v1 normal
            [-0.5, 0.5,-0.5],   # v1 position
            [0,1,0],            # v2 normal
            [-0.5, 0.5, 0.5],   # v2 position

            [0,1,0],            # v3 normal
            [ 0.5, 0.5,-0.5],   # v3 position
            [0,1,0],            # v4 normal
            [-0.5, 0.5, 0.5],   # v4 position
            [0,1,0],            # v5 normal
            [ 0.5, 0.5, 0.5],   # v5 position

            [0,-1,0],           # v6 normal
            [ 0.5,-0.5, 0.5],   # v6 position
            [0,-1,0],           # v7 normal
            [-0.5,-0.5, 0.5],   # v7 position
            [0,-1,0],           # v8 normal
            [-0.5,-0.5,-0.5],   # v8 position

            [0,-1,0],
            [ 0.5,-0.5, 0.5],
            [0,-1,0],
            [-0.5,-0.5,-0.5],
            [0,-1,0],
            [ 0.5,-0.5,-0.5],

            [0,0,1],
            [ 0.5, 0.5, 0.5],
            [0,0,1],
            [-0.5, 0.5, 0.5],
            [0,0,1],
            [-0.5,-0.5, 0.5],

            [0,0,1],
            [ 0.5, 0.5, 0.5],
            [0,0,1],
            [-0.5,-0.5, 0.5],
            [0,0,1],
            [ 0.5,-0.5, 0.5],

            [0,0,-1],
            [ 0.5,-0.5,-0.5],
            [0,0,-1],
            [-0.5,-0.5,-0.5],
            [0,0,-1],
            [-0.5, 0.5,-0.5],

            [0,0,-1],
            [ 0.5,-0.5,-0.5],
            [0,0,-1],
            [-0.5, 0.5,-0.5],
            [0,0,-1],
            [ 0.5, 0.5,-0.5],

            [-1,0,0],
            [-0.5, 0.5, 0.5],
            [-1,0,0],
            [-0.5, 0.5,-0.5],
            [-1,0,0],
            [-0.5,-0.5,-0.5],

            [-1,0,0],
            [-0.5, 0.5, 0.5],
            [-1,0,0],
            [-0.5,-0.5,-0.5],
            [-1,0,0],
            [-0.5,-0.5, 0.5],

            [1,0,0],
            [ 0.5, 0.5,-0.5],
            [1,0,0],
            [ 0.5, 0.5, 0.5],
            [1,0,0],
            [ 0.5,-0.5, 0.5],

            [1,0,0],
            [ 0.5, 0.5,-0.5],
            [1,0,0],
            [ 0.5,-0.5, 0.5],
            [1,0,0],
            [ 0.5,-0.5,-0.5],
            # ...
            ], 'float32')
    return varr

def drawUnitCube_glDrawArray():
    global gVertexArraySeparate
    varr = gVertexArraySeparate
    glEnableClientState(GL_VERTEX_ARRAY)
    glEnableClientState(GL_NORMAL_ARRAY)
    glNormalPointer(GL_FLOAT, 6*varr.itemsize, varr)
    glVertexPointer(3, GL_FLOAT, 6*varr.itemsize, ctypes.c_void_p(varr.ctypes.data + 3*varr.itemsize))
    glDrawArrays(GL_TRIANGLES, 0, int(varr.size/6))

def drawFrame():
    glBegin(GL_LINES)
    glColor3ub(255, 0, 0)
    glVertex3fv(np.array([0.,0.,0.]))
    glVertex3fv(np.array([1.,0.,0.]))
    glColor3ub(0, 255, 0)
    glVertex3fv(np.array([0.,0.,0.]))
    glVertex3fv(np.array([0.,1.,0.]))
    glColor3ub(0, 0, 255)
    glVertex3fv(np.array([0.,0.,0]))
    glVertex3fv(np.array([0.,0.,1.]))
    glEnd()

#################################################
def l2norm(v):
    return np.sqrt(np.dot(v, v))

def normalized(v):
    l = l2norm(v)
    return 1/l * np.array(v)

def lerp(v1, v2, t):
    return (1-t)*v1 + t*v2

# euler[0]: zang
# euler[1]: yang
# euler[2]: xang
def ZYXEulerToRotMat(euler):
    zang, yang, xang = euler
    Rx = np.array([[1,0,0],
                   [0, np.cos(xang), -np.sin(xang)],
                   [0, np.sin(xang), np.cos(xang)]])
    Ry = np.array([[np.cos(yang), 0, np.sin(yang)],
                   [0,1,0],
                   [-np.sin(yang), 0, np.cos(yang)]])
    Rz = np.array([[np.cos(zang), -np.sin(zang), 0],
                   [np.sin(zang), np.cos(zang), 0],
                   [0,0,1]])
    return Rz @ Ry @ Rx

def drawCubes(brightness):
    glPushMatrix()
    glScalef(.5,.5,.5)
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, (.5*brightness,.5*brightness,.5*brightness,1.))
    drawUnitCube_glDrawArray()

    glTranslatef(1.5,0,0)
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, (1.*brightness,0.,0.,1.))
    drawUnitCube_glDrawArray()

    glTranslatef(-1.5,1.5,0)
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, (0.,1.*brightness,0.,1.))
    drawUnitCube_glDrawArray()

    glTranslatef(0,-1.5,1.5)
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, (0.,0.,1.*brightness,1.))
    drawUnitCube_glDrawArray()
    glPopMatrix()

#################################################
gVisibles = [True,False,False,False] # visible flags for slerp, interpolateZYXEuler, interpolateRotVec, interpolateRotMat
def render(ang):
    global gCamAng, gCamHeight
    global gVisibles
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

    glEnable(GL_DEPTH_TEST)

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, 1, 1,10)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(5*np.sin(gCamAng),gCamHeight,5*np.cos(gCamAng), 0,0,0, 0,1,0)

    drawFrame() # draw global frame

    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    glEnable(GL_RESCALE_NORMAL) # rescale normal vectors after transformation and before lighting to have unit length

    glLightfv(GL_LIGHT0, GL_POSITION, (1.,2.,3.,1.))
    glLightfv(GL_LIGHT0, GL_AMBIENT, (.1,.1,.1,1.))
    glLightfv(GL_LIGHT0, GL_DIFFUSE, (1.,1.,1.,1.))
    glLightfv(GL_LIGHT0, GL_SPECULAR, (1.,1.,1.,1.))

    # start orientation
    # ZYX Euler angles: rot z by -90 deg then rot y by 90 then rot x by 0
    euler1 = np.array([-1.,1.,0.])*np.radians(90)   # in ZYX Euler angles
    R1 = ZYXEulerToRotMat(euler1)  # in rotation matrix
    rv1 = log(R1)   # in rotation vector

    # end orientation
    # ZYX Euler angles: rot z by 0 then rot y by 0 then rot x by 90
    euler2 = np.array([0.,0.,1.])*np.radians(90)   # in ZYX Euler angles
    R2 = ZYXEulerToRotMat(euler2)  # in rotation matrix
    rv2 = log(R2)   # in rotation vector

    # t is repeatedly increasing from 0.0 to 1.0
    t = (ang % 90) / 90.

    M = np.identity(4)

    # slerp
    if gVisibles[0]:
        R = slerp(R1, R2, t)
        glPushMatrix()
        M[:3,:3] = R
        glMultMatrixf(M.T)
        drawCubes(1.)
        glPopMatrix()

    # interpolation between rotation vectors
    if gVisibles[1]:
        R = interpolateRotVec(rv1, rv2, t)
        glPushMatrix()
        M[:3,:3] = R
        glMultMatrixf(M.T)
        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, (0.,1.,0.,1.))
        drawCubes(.75)
        glPopMatrix()

    # interpolation between Euler angles
    if gVisibles[2]:
        R = interpolateZYXEuler(euler1, euler2, t)
        glPushMatrix()
        M[:3,:3] = R
        glMultMatrixf(M.T)
        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, (0.,0.,1.,1.))
        drawCubes(.5)
        glPopMatrix()

    # interpolation between rotation matrices
    if gVisibles[3]:
        R = interpolateRotMat(R1, R2, t)
        glPushMatrix()
        M[:3,:3] = R
        glMultMatrixf(M.T)
        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, (0.,0.,1.,1.))
        drawCubes(.25)
        glPopMatrix()

    glDisable(GL_LIGHTING)

def key_callback(window, key, scancode, action, mods):
    global gCamAng, gCamHeight
    global gVisibles
    # rotate the camera when 1 or 3 key is pressed or repeated
    if action==glfw.PRESS or action==glfw.REPEAT:
        if key==glfw.KEY_1:
            gCamAng += np.radians(-10)
        elif key==glfw.KEY_3:
            gCamAng += np.radians(10)
        elif key==glfw.KEY_2:
            gCamHeight += .1
        elif key==glfw.KEY_W:
            gCamHeight += -.1
        elif key==glfw.KEY_A:
            gVisibles[0] = not gVisibles[0]
        elif key==glfw.KEY_S:
            gVisibles[1] = not gVisibles[1]
        elif key==glfw.KEY_D:
            gVisibles[2] = not gVisibles[2]
        elif key==glfw.KEY_F:
            gVisibles[3] = not gVisibles[3]
        elif key==glfw.KEY_Z:
            gVisibles[:] = [False]*4
        elif key==glfw.KEY_X:
            gVisibles[:] = [True]*4

gVertexArraySeparate = None
def main():
    global gVertexArraySeparate

    if not glfw.init():
        return
    window = glfw.create_window(480,480,'2018008659', None,None)
    if not window:
        glfw.terminate()
        return
    glfw.make_context_current(window)
    glfw.set_key_callback(window, key_callback)
    glfw.swap_interval(1)

    # ...
    gVertexArraySeparate = createVertexArraySeparate()

    count = 0
    while not glfw.window_should_close(window):
        glfw.poll_events()
        ang = count % 360
        render(ang)
        count += 1
        glfw.swap_buffers(window)

    glfw.terminate()

if __name__ == "__main__":
    main()


import os
import requests
import PyPDF2

import typing

import logging
import daiquiri

from collections import defaultdict
from io import BytesIO
from bs4 import BeautifulSoup
from datetime import date

from bot.constants import DAYS_OF_WEEK

daiquiri.setup(level=logging.DEBUG)
logger = daiquiri.getLogger("RestaurantSadlerBrasserie")

EXCLUDED = "!"
BREAK = "__"
JOIN_NEXT_LINE = "->"
JOIN_PREVIOUS_LINE = "<-"

def get_menus(food_url):
    this_week = date.today().isocalendar()[1]

    food_site = requests.get(food_url)

    soup = BeautifulSoup(food_site.text, features="html.parser")

    menu_div = soup.findAll(
        "div", {"class": "twelve-5 nine-9 four-4 left menuDownload"}
    )[0]

    menu_links = menu_div.findChildren("ul", recursive=False)[0].findChildren(
        "li", recursive=False
    )

    pdf_links = {}
    week_number = this_week

    for link in menu_links:
        ref = link.findChildren("a", recursive=False)[0]["href"]

        if ref.split(".")[-1] == "pdf":
            cycle_index = ref.split(".")[2].split("/")[-1].split("-")[-3]
            pdf_links[week_number] = (cycle_index, ref)

            week_number += 1

    return pdf_links

def parse_dishes(food_url: str, dish_types: typing.List[str]):
    def is_line_break(line_of_menu: str, dish_types=dish_types) -> bool:
        for dish_type in dish_types:
            if line_of_menu.lower().startswith(dish_type.lower()):
                return True
        return False

    def lines_to_exclude(line_of_menu: str) -> bool:
        return (
            line_of_menu.startswith("£")
        )

    def join_next_line(line_of_menu: str) -> bool:
        return (
            line_of_menu.endswith("with")
            or line_of_menu.endswith("and")
            or line_of_menu.endswith("in")
        )

    def join_previous_line(line_of_menu: str) -> bool:
        return (
            line_of_menu.startswith("topped")
            or line_of_menu.startswith("served")
            or line_of_menu.startswith("with")
            or line_of_menu.startswith("and")
            or line_of_menu.startswith("in")
        )

    def construct_return_line(line_of_menu: str) -> str:

        return_line = line_of_menu

        if join_previous_line(return_line):
            return_line = JOIN_PREVIOUS_LINE + " " + return_line
        if join_next_line(return_line):
            return_line = return_line + " " + JOIN_NEXT_LINE

        return return_line

    def get_menu_item(line_of_menu: str) -> str:

        stripped_line = line_of_menu.strip()

        if lines_to_exclude(stripped_line):
            return EXCLUDED
        elif is_line_break(stripped_line):
            return BREAK
        else:
            return construct_return_line(stripped_line)

    def add_item_to_data_store(item: str, data_list: list) -> list:
        if not item == "":
            data_list = data_list.append(item)
        return data_list

    pdf_links = get_menus(food_url=food_url)

    dish_types = defaultdict(list)

    for i, (week_number, link_data) in enumerate(pdf_links.items()):
        # DOWNLOAD ONLY SECOND MENU - CURRENT WEEK
        if i == 1:
            # link_data IS A TUPLE - FIRST ENTRY IS INDEX OF CURRENT MENY IN AN ORDER 4 CYCLE.
            # THE SECOND IS THE LINK
            response = requests.get(link_data[1])

            pdf_file = BytesIO(response.content)

            pdf_reader = PyPDF2.PdfFileReader(pdf_file)

            menu_text = pdf_reader.getPage(0).extractText().split(os.linesep)

            # SKIP WEEK NUMBER AND WEEKDAY NAMES
            LINES_TO_SKIP = 6
            processed_items = [
                get_menu_item(menu_line) for menu_line in menu_text[LINES_TO_SKIP:]
            ]

            item_index = 0
            dish_index = 0
            parsed_menu_item = ""

            while item_index < len(processed_items):

                item = processed_items[item_index]

                if item == EXCLUDED:
                    pass
                elif item == BREAK:
                    dish_index += 1
                else:
                    parsed_menu_item = item

                    while parsed_menu_item.startswith(JOIN_PREVIOUS_LINE):
                        cut_start = len(JOIN_PREVIOUS_LINE) + 1
                        parsed_menu_item = (
                            dish_types[dish_index][-1]
                            + " "
                            + parsed_menu_item[cut_start:]
                        )
                        dish_types[dish_index].pop(-1)

                    while parsed_menu_item.endswith(JOIN_NEXT_LINE):
                        cut_end = len(JOIN_NEXT_LINE) + 1
                        parsed_menu_item = (
                            parsed_menu_item[:-cut_end]
                            + " "
                            + processed_items[item_index + 1]
                        )
                        item_index += 1

                add_item_to_data_store(parsed_menu_item, dish_types[dish_index])
                parsed_menu_item = ""

                item_index += 1

    return dish_types

def menu(food_url="https://www.oxfordsp.com/parklife/sadler-building/"):

    DISH_TYPES = [
        "Meat Main Course",
        "Vegetarian Main Course",
        "Carbohydrate",
        "Side",
    ]
    LAST_MENU_DAY_INDEX = 5

    menu = {}

    dishes = parse_dishes(food_url=food_url, dish_types=DISH_TYPES)

    logger.debug(dishes)

    restaurant_week = DAYS_OF_WEEK[:LAST_MENU_DAY_INDEX]

    incorrectly_parsed_dishes = []

    for day_index in range(len(restaurant_week)):
        day_name = restaurant_week[day_index]

        menu[day_name] = {
            f"Whole week": list(zip(dishes.values()))
        }

    return menu


from flask_restful import Resource, abort
from flask import jsonify
from project.parsers import Parser
from project.schema import User, Task
import sqlalchemy
from flask_jwt_extended import (jwt_required, get_jwt_identity)

class CreateTask(Resource):
    """
    Given a request with params {"heading", "description"}
    Endpoint is jwt protected
    """

    @jwt_required
    def post(self):
        task_parser = Parser.get_tasks_parser(id_req=False, heading_req=True, desc_req=True)
        task_data = task_parser.parse_args()
        username = get_jwt_identity()
        user = User.get_user_by_name(username=username)

        task = Task(
            user_id=user.id,
            heading=task_data['heading'],
            description=task_data['description'],
            is_completed=False
        )
        try:
            task.save()
            return {
                'message': 'Task was created successfully',
                'id': task.id
            }
        except:
            abort(500, description="Failed to create task")

class GetTask(Resource):
    """
    Retrieve a task given "id" of task in params
    Endpoint is jwt protected
    """

    @jwt_required
    def get(self, id):
        try:
            task = Task.get_task_by_id(id)
            if task == None:
                raise KeyError
            return {
                'id': task.id,
                'heading': task.heading,
                'description': task.description,
                'is_completed': str(task.is_completed)
            }
        except KeyError:
            abort(409, description='Task %d does not exist' % id)
        except:
            abort(500, description="Unknown exception occured")

class ListTasks(Resource):
    """
    Lists all the tasks for the user logged in.
    Endpoint is jwt protected
    """

    @jwt_required
    def get(self):
        username = get_jwt_identity()
        user = User.get_user_by_name(username=username)
        tasks = user.tasks
        list_of_tasks = []
        for task in tasks:
            list_of_tasks.append({
                'id': task.id,
                'heading': task.heading,
                'description': task.description,
                'is_completed': str(task.is_completed)
            })
        return jsonify(list_of_tasks)

class UpdateTask(Resource):
    """
    Updates a given task. Params may include any of {"heading", "description", "is_completed"}
    Endpoint is jwt protected
    """

    @jwt_required
    def post(self, id):
        task_parser = Parser.get_tasks_parser(id_req=False)
        task_data = task_parser.parse_args()
        task = Task.get_task_by_id(id=id)
        heading = task.heading if ('heading' not in task_data or task_data['heading'] == None) else task_data[
            'heading']
        description = task.description if ('description' not in task_data or task_data['description'] == None) else \
        task_data['description']
        is_completed = task.is_completed if (
                    'is_completed' not in task_data or task_data['is_completed'] == None) else task_data[
            'is_completed']

        try:
            Task.update_task(id=id, heading=heading, description=description, is_completed=bool(is_completed))
            return {
                'message': 'Task updated successfully',
                'id': id,
                'heading': heading,
                'description': description,
                'is_completed': is_completed
            }
        except:
            abort(500, description="Failed to updated task %d" % id)

class DeleteTask(Resource):
    """
    Deletes a task given the task id
    Endpoint is jwt protected
    """

    @jwt_required
    def post(self, id):
        try:
            Task.delete_task(id=id)
            return {
                'message': 'Task Deleted Successfully'
            }
        except sqlalchemy.orm.exc.UnmappedInstanceError:
            abort(409, description="Task %d does not exist" % id)
        except:
            abort(500, description="Failed to delete task %d" % id)


# -*- coding: utf-8 -*-

import os
import os.path
import codecs
#import sys
import argparse
import subprocess

parser = argparse.ArgumentParser(description='')
parser.add_argument("-r", "--root_path")
parser.add_argument("-p", "--project")

args = parser.parse_args()

root_path = args.root_path
project   = args.project

for root, dirs, files in os.walk(root_path):
	for file in files:
		name, ext = os.path.splitext(file)
		if ext != ".hpp":
			continue

		file_path = os.path.join(root, file)
		s = codecs.open(file_path, "r", "utf-8-sig").read()
		if "NO INCLUDE GUARD" in s:
			continue

		path = os.path.relpath(file_path, root_path)
		label = "_".join([project, path])
		label = label.upper()
		label = label.replace("\\", "_")
		label = label.replace(".", "_")
		if not "#ifndef " + label in s or not "#define " + label in s or not "#endif // " + label in s:
			subprocess.Popen("start " + file_path, shell=True)


import logging
import time
import random
from locust import User, events
from client import MilvusClient

url = 'http://192.168.1.238:19121'

class MilvusTask(object):
    def __init__(self, url):
        self.request_type = "http"
        self.m = MilvusClient(url)
        # logging.getLogger().info(id(self.m))

    def __getattr__(self, name):
        func = getattr(self.m, name)

        def wrapper(*args, **kwargs):
            start_time = time.time()
            try:
                result = func(*args, **kwargs)
                total_time = int((time.time() - start_time) * 1000)
                if result:
                    events.request_success.fire(request_type=self.request_type, name=name, response_time=total_time,
                                                response_length=0)
                else:
                    events.request_failure.fire(request_type=self.request_type, name=name, response_time=total_time,
                                            exception=e, response_length=0)

            except Exception as e:
                total_time = int((time.time() - start_time) * 1000)
                events.request_failure.fire(request_type=self.request_type, name=name, response_time=total_time,
                                            exception=e, response_length=0)

        return wrapper


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 18 00:04:38 2020

@author: nancyscarlet
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dataset=pd.read_csv('Salary_Data.csv')
dataset.head()
X=dataset.iloc[:,:-1].values
Y=dataset.iloc[:,1].values

from sklearn.cross_validation import train_test_split
X_train, X_test , Y_train, Y_test = train_test_split(X,Y,test_size=0.25,random_state=0)
#test_size = 0.25-> 20% is test and 80% is train

#Importing Linear regressor from scikit learn
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train,Y_train,sample_weight=None)

Y_predicted = regressor.predict(X_test)

#plt.scatter(X_train,Y_train,color='red')
plt.scatter(X_train,Y_train,color='red')
plt.plot(X_train,regressor.predict(X_train),color='blue')
plt.title('Salary based on experience')
plt.xlabel('Experience in Years')
plt.ylabel('Salary')
plt.show()

plt.scatter(X_test,Y_test,color='red')
plt.plot(X_train,regressor.predict(X_train),color='blue')
plt.title('Salary based on experience')
plt.xlabel('Experience in Years')
plt.ylabel('Salary')
plt.show()

Machine Learning A-Z Template Folder/Part 2 - Regression/Section 4 - Simple Linear Regression/Simple_Linear_Regression/practiceLinearRegression.py


import cv2
import numpy as np
import random

width = 501
height = 501
dim = (height, width, 3)
center = (int(dim[0]/2), int(dim[1]/2))
value = 255

# прави случаен избор на распоредот на нијансите на сликата
def rnd_shades(result, lst_occurr, lst_shades):
    if sum(lst_occurr) == 0:
        return result
    else:
        non_zeros = [i for i in range(len(lst_occurr)) if lst_occurr[i] != 0]
        i = random.choice(non_zeros)
        result.append(lst_shades[i])
        lst_occurr[i] -= 1
        return rnd_shades(result, lst_occurr, lst_shades)

# 'hue' - боја
# 'num_shades' - број на различни нијанси во сликата
# 'min_diff' - минимална разлика на заситеност меѓу нијансите
# 'min_occurr' - минимален број на појавувања од секоја нијанса
# 'max_occurr' - максимален број на појавувања од секоја нијанса
# 'smlst_rad' - радиусот на најмалиот круг
def create_image(hue, num_shades, min_diff, min_occurr, max_occurr, smlst_rad):
    shades_values = random.sample(range(0, 256, min_diff), num_shades)
    num_occurr = [random.randint(min_occurr, max_occurr)
                  for _ in range(num_shades)]
    num_circles = sum(num_occurr)
    print('Број на појавувања од секоја нијанса:')
    print(num_occurr)
    k = int((width/2-smlst_rad)/(num_circles-1))
    shades_values = rnd_shades([], num_occurr, shades_values)
    shades = [[hue, s, value] for s in shades_values]
    img = np.zeros(dim, dtype=np.uint8)
    print('Распоред на нијанси (од најнадворешниот круг на внатре):')
    print(shades_values)
    for i, sh in enumerate(shades):
        sh = np.array([[sh]], dtype=np.uint8)
        bgr_sh = cv2.cvtColor(sh, cv2.COLOR_HSV2BGR)
        bgr_sh = bgr_sh.flatten()
        col = (int(bgr_sh[0]), int(bgr_sh[1]), int(bgr_sh[2]))
        img = cv2.circle(img, center, int(width/2-i*k), col, -1, 5)
    cv2.imwrite('{}-{}.jpg'.format(hue, num_shades), img)

# 70 140 210
create_image(180, 4, 50, 2, 3, 60)


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import numpy as np
import tensorflow as tf
import pandas as pd
import argparse
import os
import time
import sys
import pwd
import csv
import re
import deepchem
import pickle
import dcCustom
from dcCustom.molnet.preset_hyper_parameters import hps
from dcCustom.molnet.run_benchmark_models import model_regression, model_classification
from dcCustom.molnet.check_availability import CheckFeaturizer, CheckSplit

def load_davis(featurizer = 'Weave', cross_validation=False, test=False, split='random',
  reload=True, K = 5, mode = 'regression', predict_cold = False, cold_drug=False,
  cold_target=False, split_warm=False, filter_threshold=0, prot_seq_dict=None,
  oversampled=False):
  # The last parameter means only splitting into training and validation sets.

  if cross_validation:
    assert not test

  if mode == 'regression' or mode == 'reg-threshold':
    mode = 'regression'
    tasks = ['davis']
    file_name = "restructured.csv"
  elif mode == 'classification':
    tasks = ['davis_bin']
    file_name = "restructured_bin.csv"

  data_dir = "davis_data/"
  if reload:
    delim = "/"
    if filter_threshold > 0:
      delim = "_filtered" + delim
    if predict_cold:
      delim = "_cold" + delim
    elif split_warm:
      delim = "_warm" + delim
    elif cold_drug:
      delim = "_cold_drug" + delim
    elif cold_target:
      delim = "_cold_target" + delim
    if cross_validation:
      delim = "_CV" + delim
      save_dir = os.path.join(data_dir, featurizer + delim + mode + "/" + split)
      loaded, all_dataset, transformers = dcCustom.utils.save.load_cv_dataset_from_disk(
          save_dir, K)
    else:
      save_dir = os.path.join(data_dir, featurizer + delim + mode + "/" + split)
      loaded, all_dataset, transformers = deepchem.utils.save.load_dataset_from_disk(
          save_dir)
    if loaded:
      return tasks, all_dataset, transformers

  dataset_file = os.path.join(data_dir, file_name)
  if featurizer == 'Weave':
    featurizer = dcCustom.feat.WeaveFeaturizer()
  elif featurizer == 'ECFP':
    featurizer = dcCustom.feat.CircularFingerprint(size=1024)
  elif featurizer == 'GraphConv':
    featurizer = dcCustom.feat.ConvMolFeaturizer()

  loader = dcCustom.data.CSVLoader(
      tasks = tasks, smiles_field="smiles", protein_field = "proteinName",
      source_field = 'protein_dataset', featurizer=featurizer, prot_seq_dict=prot_seq_dict)
  dataset = loader.featurize(dataset_file, shard_size=8192)

  if mode == 'regression':
    transformers = [
          dcCustom.trans.NormalizationTransformer(
              transform_y=True, dataset=dataset)
    ]
  elif mode == 'classification':
    transformers = [
        dcCustom.trans.BalancingTransformer(transform_w=True, dataset=dataset)
    ]

  print("About to transform data")
  for transformer in transformers:
    dataset = transformer.transform(dataset)

  splitters = {
      'index': deepchem.splits.IndexSplitter(),
      'random': dcCustom.splits.RandomSplitter(split_cold=predict_cold, cold_drug=cold_drug,
        cold_target=cold_target, split_warm=split_warm, prot_seq_dict=prot_seq_dict,
        threshold=filter_threshold),
      'scaffold': deepchem.splits.ScaffoldSplitter(),
      'butina': deepchem.splits.ButinaSplitter(),
      'task': deepchem.splits.TaskSplitter()
  }
  splitter = splitters[split]
  if test:
    train, valid, test = splitter.train_valid_test_split(dataset)
    all_dataset = (train, valid, test)
    if reload:
      deepchem.utils.save.save_dataset_to_disk(save_dir, train, valid, test,
                                               transformers)
  elif cross_validation:
    fold_datasets = splitter.k_fold_split(dataset, K)
    all_dataset = fold_datasets
    if reload:
      dcCustom.utils.save.save_cv_dataset_to_disk(save_dir, all_dataset, K, transformers)

  else:
    # not cross validating, and not testing.
    train, valid, test = splitter.train_valid_test_split(dataset, frac_valid=0.2,
      frac_test=0)
    all_dataset = (train, valid, test)
    if reload:
      deepchem.utils.save.save_dataset_to_disk(save_dir, train, valid, test,
                                               transformers)

  return tasks, all_dataset, transformers


#!/usr/bin/python
from __future__ import print_function
from datetime import date
from money import Money
from dateutil.relativedelta import relativedelta
from decimal import Decimal
import findates
from Contract import LoanContract
import LoanCalculator

calculator = LoanCalculator.LoanCalculator()
calculator.test_wrapper()
exit

print("Standard Payments")
my_contract = LoanContract()
my_contract.set_events({"kickstarter_success":True,
                        "kickstarter_start_date": date(2015,1,1),
                        "kickstarter_payment_date":date(2015,1,15),
                        "kickstarter_revenue":50000,
                        "early_payments":[]})
calculator = LoanCalculator.LoanCalculator()
calculator.calculate(my_contract)
print()

print ("Kickstarter success #1")
my_contract.set_events({"kickstarter_success":True,
                        "kickstarter_start_date": date(2015,1,1),
                        "kickstarter_payment_date":date(2015,1,15),
                        "kickstarter_revenue":100000,
                        "early_payments":[]})
calculator = LoanCalculator.LoanCalculator()
calculator.calculate(my_contract)
print ()
print ("Kickstarter success #2")
my_contract.set_events({"kickstarter_success":True,
                        "kickstarter_start_date": date(2015,1,1),
                        "kickstarter_payment_date":date(2015,1,15),
                        "kickstarter_revenue":60000,
                        "early_payments":[]})
calculator = LoanCalculator.LoanCalculator()
calculator.calculate(my_contract)


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 30 22:34:32 2017

@author: Ray
"""

import matplotlib.pyplot as plt

x_values = list(range(1, 1001))
y_values = [x ** 2 for x in x_values]

plt.title("Squared list of a Range between 1 and 1000.\n", fontsize = 18)
plt.xlabel("Value", fontsize = 14)
plt.ylabel("Square of Value", fontsize = 14)

plt.scatter(x_values, y_values)

plt.axis([0, 1100, 0, 1100000])

# remove the outlines from data points
plt.scatter(x_values, y_values, edgecolor='none', s=40)

plt.scatter(x_values, y_values, c = (0.4, .7, 0.8), edgecolor='none', s=40)


##########################################################
# pdf-to-csv
import tabula

for year in [2016,2017,2018]:
    print('Processing year ' + str(year))
    pdf = './data/bieg-' + str(year) + '.pdf'
    csv = './data/bieg-' + str(year) + '.csv'
    df = tabula.read_pdf(pdf, lattice=True, pages='all', area = [20, 0, 100, 100], relative_area = True, pandas_options={'header': None})
    df.columns = ['place', 'id', 'name', 'surname', 'location', 'team', 'birthYear', 'category', 'place2', 'M', 'K', 'half-time', 'time', 'netto-time']
    if (year == 2017):
        df = df[:-4] # In 2017 there were 3 disqualified participants and they had place 'DSQ' which is not of type int
    else:
        df = df[:-1]
    df = df.fillna({'place': -1, 'id': -1, 'name': '', 'surname': '', 'location': '', 'team': '',
                   'birthYear': -1, 'category': '', 'place2': -1, 'M': -1, 'K': -1,
                    'half-time': '', 'time': '', 'netto-time': ''})
    df = df.astype({'place': int, 'id': int, 'name': str, 'surname': str, 'location': str, 'team': str, 'birthYear': int, 'category': str, 'place2': int, 'M': int, 'K': int, 'half-time': str, 'time': str, 'netto-time': str})
    df.to_csv(csv, ';', index=False)

    # Replacing new-lines in cell on spaces
    file = open(csv, 'rb')
    content = file.read()
    content = content.replace(b'\r', b' ')
    content = content.replace(b' \n', b'\r\n')
    file.close()
    file = open(csv, 'wb')
    file.write(content)
    file.close()

##########################################################
# read dataset to numpy structured array
import pandas
df = pandas.read_csv('./data/bieg-2018.csv', ';')
df = df.astype({'place': int, 'id': int, 'name': str, 'surname': str, 'location': str, 'team': str, 'birthYear': int, 'category': str, 'place2': int, 'M': int, 'K': int, 'half-time': str, 'time': str, 'netto-time': str})
data = df.to_records(index=False)


from django.utils.functional import empty, LazyObject

class LazySettings(LazyObject):

    def _setup(self):
        from django.conf import settings

        settings_dict = getattr(settings, "GITVIEW", {})

        settings_dict["name"] = settings_dict.get("name", "GitView")

        self._wrapped = settings_dict

    def __getattr__(self, name):
        """
        Allow dictionary keys to be accessed as attributes.
        """

        if self._wrapped is empty:
            self._setup()

        return self._wrapped[name]

settings = LazySettings()


from unittest import TestCase
import blockchain
from blockchain import node_identifier
import json
import itertools

class TestRoutes(TestCase):
    def setUp(self):
        """""Define the setup app that uses a new blockchain in every test method."""
        _app = blockchain.app
        blockchain.blockchain = blockchain.Blockchain()
        self.app = _app.test_client()

    def test_get_genesis_chain(self):
        response = self.app.get('/chain')

        data = json.loads(response.data)
        assert response.status_code == 200
        assert 'chain' in data
        assert 'length' in data
        assert data['length'] == 1

    def test_first_block_mined(self):
        response = self.app.get('/mine')

        data = json.loads(response.data)
        assert response.status_code == 200
        assert 'message' in data
        assert 'index' in data
        assert 'transactions' in data
        assert 'proof' in data
        assert 'previous_hash' in data
        assert data['index'] == 2

    def test_mining_blocks_increases_index(self):
        response = self.app.get('/mine')
        data_first = json.loads(response.data)
        response = self.app.get('/mine')
        data_second = json.loads(response.data)
        assert data_first['index'] == 2
        assert data_second['index'] == 3

    def test_mining_blocks_rewards_miner(self):
        for _ in range(3):
            response = self.app.get('/mine')
            data = json.loads(response.data)
            assert len(data['transactions']) == 1
            last_transaction = data['transactions'][-1]
            assert last_transaction['sender'] == '0'
            assert last_transaction['recipient'] == node_identifier
            assert last_transaction['amount'] == 1

    def test_add_new_transactions_in_one_block(self):
        for _ in range(3):
            response = self.app.post('/transactions/new', data=json.dumps({
                'sender': 'X',
                'recipient': 'Y',
                'amount': 100.0
            }), content_type='application/json')
            assert response.status_code == 201
            data = json.loads(response.data)
            assert 'message' in data
            assert 'Block 2' in data['message']

    def test_add_new_transactions_in_more_blocks(self):
        for i in range(1, 4):
            # New transaction
            response = self.app.post('/transactions/new', data=json.dumps({
                'sender': 'X',
                'recipient': 'Y',
                'amount': 100.0
            }), content_type='application/json')
            assert response.status_code == 201
            data = json.loads(response.data)
            assert 'message' in data
            assert f'Block {i + 1}' in data['message']

            # New block
            response = self.app.get('/mine')
            assert response.status_code == 200
            data = json.loads(response.data)
            # 1 for transaction, 1 for reward
            assert len(data['transactions']) == 2

    def test_adding_incomplete_new_transaction(self):
        complete_data = (('sender', 'x'), ('recipient', 'y'), ('amount', 100.0))
        for missing_data in itertools.combinations(complete_data, len(complete_data) - 1):
            response = self.app.post(
                '/transactions/new',
                data=json.dumps(dict(missing_data)),
                content_type='application/json')
            assert response.status_code == 400

    def test_register_nodes(self):
        response = self.app.post('/nodes/register', data=json.dumps({
            'nodes': ['http://localhost:5001'],
        }), content_type='application/json')
        assert response.status_code == 201
        data = json.loads(response.data)
        assert 'message' in data
        assert len(data['total_nodes']) == 1
        assert 'localhost:5001' in data['total_nodes'][0]

    def test_register_invalid_nodes(self):
        response = self.app.post('/nodes/register', data=json.dumps({
            'wrong_key': None
        }), content_type='application/json')
        assert response.status_code == 400

    def test_no_nodes_to_resolve_conflict(self):
        response = self.app.get('/nodes/resolve')
        assert response.status_code == 200
        data = json.loads(response.data)
        assert data['message'] == 'Our chain is authoritative'
        assert 'chain' in data

    def test_response_of_mocking_resolving_conflicts_between_nodes(self):
        # Mokeypatch blockchain's method
        blockchain.blockchain.resolve_conflicts = lambda: True
        response = self.app.get('/nodes/resolve')
        assert response.status_code == 200
        data = json.loads(response.data)
        assert data['message'] == 'Our chain was replaced'
        assert 'new_chain' in data


import logging
import time
from StringIO import StringIO

try:
    import pygal
except:
    pygal = None

from openerp import api, models, fields

_logger = logging.getLogger(__name__)

class TelegramCommand(models.Model):

    _inherit = "telegram.command"

    @api.multi
    def _get_globals_dict(self):
        res = super(TelegramCommand, self)._get_globals_dict()
        res['pygal'] = pygal
        return res

    @api.multi
    def _eval(self, *args, **kwargs):
        locals_dict = kwargs.get('locals_dict') or {}
        locals_dict['charts'] = []
        kwargs['locals_dict'] = locals_dict
        return super(TelegramCommand, self)._eval(*args, **kwargs)

    def _render(self, template, locals_dict, tsession):
        res = super(TelegramCommand, self)._render(template, locals_dict, tsession)
        t0 = time.time()
        photos = []

        for obj in locals_dict.get('charts', []):
            f = StringIO(obj.render_to_png())
            f.name = 'chart.png'
            photos.append(f)

        render_time = time.time() - t0
        _logger.debug('Render Charts in %.2fs\n locals_dict: %s', render_time, locals_dict)
        res['photos'] += photos
        return res


#coding:utf8
import re
import cv2
import os
import numpy as np
import cv2.cv as cv
from keras.models import Sequential
from keras.layers.core import Dense,Activation,Flatten
from keras.layers.convolutional import Convolution2D,MaxPooling2D
from keras.optimizers import Adam
from sklearn.model_selection import train_test_split
from keras.layers.advanced_activations import LeakyReLU

from keras.utils import np_utils

#构建卷积神经网络
def cnn_model(train_data,train_label,test_data,test_label):
    model = Sequential()
    model.add(Convolution2D(
        nb_filter = 12,
        nb_row = 3,
        nb_col = 3,
        border_mode = 'valid',
        dim_ordering = 'th',
        input_shape = (1,128,192)))
    model.add(Activation('relu'))#激活函数使用修正线性单元
    model.add(MaxPooling2D(
        pool_size = (2,2),
        strides = (2,2),
        border_mode = 'valid'))
    model.add(Convolution2D(
        24,
        3,
        3,
        border_mode = 'valid',
        dim_ordering = 'th'))
    model.add(Activation('relu'))
#池化层 24×29×29
    model.add(MaxPooling2D(
        pool_size = (2,2),
        strides = (2,2),
        border_mode = 'valid'))
    model.add(Convolution2D(
        48,
        3,
        3,
        border_mode = 'valid',
        dim_ordering = 'th'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(
        pool_size = (2,2),
        strides =(2,2),
        border_mode = 'valid'))
    model.add(Flatten())
    model.add(Dense(20))
    model.add(Activation(LeakyReLU(0.3)))
    #model.add(Dropout(0.5))
    model.add(Dense(20))
    model.add(Activation(LeakyReLU(0.3)))
    #model.add(Dropout(0.4))
    model.add(Dense(5,init = 'normal'))
    model.add(Activation('softmax'))
    adam = Adam(lr = 0.001)
    model.compile(optimizer = adam,
            loss =  'categorical_crossentropy',
            metrics = ['accuracy'])
    print('----------------training-----------------------')
    model.fit(train_data,train_label,batch_size = 12,nb_epoch = 35,shuffle = True,show_accuracy = True,validation_split = 0.1)
    print('----------------testing------------------------')
    loss,accuracy = model.evaluate(test_data,test_label)
    print('\n test loss:',loss)
    print('\n test accuracy',accuracy)

cnn_model(train_data,train_label,test_data,test_label)


# Copyright 2016 Markus D. Herrmann, University of Zurich
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''Jterator module for registering objects. Registration allows subsequent
measurements of the objects (feature extraction) and automatically persist them
on disk.
'''
import collections
import logging
from jtlib.utils import label

logger = logging.getLogger(__name__)

VERSION = '0.1.0'

Output = collections.namedtuple('Output', ['objects'])

def main(mask):
    '''Registers objects (connected pixel components) in an image for use by
    other (measurement) modules downstream in the pipeline. In case a binary
    mask is provided the image is automatically labeled.

    Parameters
    ----------
    mask: numpy.ndarray[Union[numpy.bool, numpy.int32]]
        binary or labeled mask

    Returns
    -------
    jtmodules.register_objects.Output

    See also
    --------
    :class:`tmlib.workflow.jterator.handles.SegmentedObjects`
    '''
    if mask.dtype == 'bool':
        label_image = label(mask)
    else:
        label_image = mask
    return Output(label_image)


from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from pycocotools.mask import decode
from pycococreatortools.pycococreatortools import binary_mask_to_polygon
import os
import os.path as osp
import json
import argparse
import ipdb
from tqdm import tqdm

def pred2hard(ann_file, pred_file, output, fp_th=0.8):

    #load GT and Detections
    cocoGt = COCO(ann_file)
    cocoDt = cocoGt.loadRes(pred_file)
    #Eval Detections
    cocoEval = COCOeval(cocoGt,cocoDt)
    cocoEval.evaluate()

    #number of instances
    num_train_inst = len(cocoGt.catToImgs[1])

    # Load previous file
    new_anns = json.load(open(ann_file))

    #Find the FPs given a threshold
    print('Finding FP...')
    num_fp = 0
    for key in tqdm(cocoDt.imgToAnns.keys()):
        # ipdb.set_trace()
        ious = cocoEval.ious[(key,1)]
        for i,iou in enumerate(ious):
            max_overlap = max(iou)
            if max_overlap <= fp_th:
                num_fp += 1
                fp = cocoDt.imgToAnns[key][i]
                fp['width'] = fp['segmentation']['size'][1]
                fp['height'] = fp['segmentation']['size'][0]
                fp_segm = decode(fp['segmentation'])
                fp['segmentation'] = binary_mask_to_polygon(fp_segm, tolerance=2)
                # ipdb.set_trace()
                fp.pop('score')
                fp['category_id'] = 2
                fp['id'] += num_train_inst
                fp['area'] = int(fp['area'])
                fp['bbox'] = fp['bbox'].tolist()
                new_anns['annotations'].append(fp)
    print('{} FPs found'.format(num_fp))
    # ipdb.set_trace()
    new_anns['categories'].append({'id':2, 'name': 'Three Leaf Clover', 'supercategory': 'Clover'})
    with open(output, 'w') as f:
        json.dump(new_anns, f)

    print('Hard negatives annotation file saved in {}'.format(output))
if __name__ == "__main__":

    parser = argparse.ArgumentParser(description="Predictions to hard negatives")
    parser.add_argument(
        "--ann_file",
        default="",
        help="path to ann file",
        required=True,
        type=str,
    )
    parser.add_argument(
        "--pred_file",
        default="",
        help="path to pred file",
        required=True,
        type=str,
    )
    parser.add_argument(
        "--out_file",
        default="",
        help="name and path of out file",
        required=True,
        type=str,
    )
    parser.add_argument(
        "--th",
        default=0.8,
        help="FP threshold",
        required=False,
        type=float,
    )

    args = parser.parse_args()
    print('Function called with args {}'.format(args))
    #ann_file = 'examples/instances_test_pos.json'
    #pred_file = 'examples/segm.json'
    #output = 'examples/instances_test_hard_negatives.json'
    pred2hard(args.ann_file, args.pred_file, args.out_file)


#!/usr/bin/env python

import os
import argparse
from typing import Optional, Sequence
from pathlib import Path
from dotenv import load_dotenv
from project_init import local_project as lp, remote_project as rp

def main(argv: Optional[Sequence[str]] = None) -> int:

    # Intilaize argparser and set arguments
    parser = argparse.ArgumentParser(
        description="Necessary GitHub arguments for new Remote and local repo"
        )

    parser.add_argument(
        "-v", "--view",
        default="public",
        choices=("private", "public"),
        type=str,
        help="GitHub view status Public vs Private. (default: %(default)s).",
        )

    parser.add_argument(
        "-n", "--projectname",
        required=True,
        type=str,
        help="Your project Name for Remote and local creation.",
        )

    # Get argparse arguments
    args = parser.parse_args(argv)
    view = args.view
    pn = args.projectname

    # Get base directory this file is in
    env_path = Path(".", ".env")

    # Join path with file name and load_dotenv
    load_dotenv(dotenv_path=env_path)

    # Get .env variables
    un      = os.getenv("NAME")
    fp      = os.getenv("FILEPATH")
    api_url = os.getenv("API_URL")
    token   = os.getenv("TOKEN")
    key     = os.getenv("KEY")

    # Concat Project name to path
    #path = fp + "\\" + pn
    path = f"{fp}/{pn}"

    # Create remote repo on GitHub and return repo information local repo
    repo = rp(un, api_url, token, pn, view, key)

    # Create local repo in Python folder and connect to remote repo.
    lp(path, repo, pn)

if __name__ == '__main__':
    main()


from numpy import linspace, zeros,sign,array
import matplotlib.pyplot as plt

def f(x):
    return -x**2+6.0*x-5.0
a=-2.0
b=3.0
n=14
c=zeros(n)
error = zeros(n)
xrange = array(range(1,15))
for i in range(n):
    c[i] = (a+b)/2.0
    if(sign(f(c[i]))==sign(f(a))):
        a=c[i]
    else:
        b=c[i]

for i in range(n-1):
    error[i+1]=abs(((c[i+1]-c[i])/c[i+1]))*100

print("%5s %8s %8s" % ('k','c','E'))
for k in range(n):
    print("%5d %9.4f %9.4f" % (k+1,c[k],error[k]))

plt.plot(xrange,c,'k--',label='c')
plt.plot(xrange,error,'ro-',label='E(%)')
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.show()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('focus', '0032_auto_20161119_1126'),
    ]

    operations = [
        migrations.CreateModel(
            name='ChartData',
            fields=[
                ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True, serialize=False)),
                ('title', models.CharField(max_length=40)),
                ('width', models.IntegerField(default=400)),
                ('height', models.IntegerField(default=400)),
                ('x_data', models.TextField(null=True, blank=True)),
                ('y_data', models.TextField(null=True, blank=True)),
                ('fill_val', models.BooleanField(default=True)),
                ('color_val', models.CharField(blank=True, max_length=10, default=True)),
                ('bg_color_val', models.CharField(blank=True, max_length=10, default=True)),
                ('line_tension', models.FloatField(default=0.0)),
                ('border_width', models.FloatField(default=0.5)),
                ('point_border_width', models.FloatField(default=1.0)),
                ('point_hover_radius', models.FloatField(default=5.0)),
                ('point_hover_radius_width', models.FloatField(default=1.0)),
                ('point_radius', models.FloatField(default=1.0)),
                ('point_hit_radius', models.FloatField(default=10.0)),
                ('chart_type', models.CharField(max_length=40, choices=[('_type_full_history', 'Full History'), ('_type_resampled_avg_m', 'Average Resampled by Month'), ('_type_resampled_sum_w', 'Total Resampled by Week'), ('_type_dual_moving_avg', 'Dual Moving Average'), ('_type_moving_env', 'Moving Envelope'), ('_type_this_month', 'This Month'), ('_type_year_weekly_sum', 'Year Total Resampled by Week')], default='_type_full_history')),
            ],
        ),
        migrations.CreateModel(
            name='Dataset',
            fields=[
                ('chartdata_ptr', models.OneToOneField(primary_key=True, to='focus.ChartData', serialize=False, parent_link=True, auto_created=True)),
            ],
            bases=('focus.chartdata',),
        ),
        migrations.AddField(
            model_name='chartdata',
            name='parent',
            field=models.ForeignKey(to='focus.Task'),
        ),
        migrations.AddField(
            model_name='dataset',
            name='chart',
            field=models.ForeignKey(related_name='+', to='focus.ChartData'),
        ),
    ]


import base64
import io
import json
import logging
import os
import subprocess
from subprocess import PIPE

from dateutil import parser
from libcloud.storage.drivers.azure_blobs import AzureBlobsStorageDriver

from medusa.storage.abstract_storage import AbstractStorage
import medusa.storage.azure_blobs_storage.concurrent
from medusa.storage.azure_blobs_storage.azcli import AzCli
import medusa

class AzureStorage(AbstractStorage):

    def connect_storage(self):
        with io.open(os.path.expanduser(self.config.key_file), 'r', encoding='utf-8') as json_fi:
            credentials = json.load(json_fi)

        if 'host' in credentials:
            # Hack for Azure connections with a host. Libcloud has a bug in this scenario.
            # Link to bug submitted against libcloud: https://github.com/apache/libcloud/issues/1551
            driver = AzureBlobsStorageDriver(
                key=None,
                secret=credentials['key'],
                host=credentials['host']
            )
            driver.connection.user_id = credentials['storage_account']
        else:
            driver = AzureBlobsStorageDriver(
                key=credentials['storage_account'],
                secret=credentials['key']
            )

        return driver

    def check_dependencies(self):
        az_cli_cmd = AzCli.cmd()
        try:
            subprocess.check_call(az_cli_cmd + ["help"], stdout=PIPE, stderr=PIPE)
        except Exception:
            raise RuntimeError(
                "Azure cli doesn't seem to be installed on this system and is a "
                + "required dependency for the Azure backend. "
                + "Please check https://docs.microsoft.com/en-us/cli/azure/install-azure-cli for guidelines."
            )

    def get_object_datetime(self, blob):
        logging.debug(
            "Blob {} last modification time is {}".format(
                blob.name, blob.extra["last_modified"]
            )
        )
        return parser.parse(blob.extra["last_modified"])

    def get_cache_path(self, path):
        logging.debug(
            "My cache path is {}".format(path)
        )
        return path

    def upload_blobs(self, srcs, dest):
        return medusa.storage.azure_blobs_storage.concurrent.upload_blobs(
            self, srcs, dest, self.bucket,
            max_workers=self.config.concurrent_transfers,
            multi_part_upload_threshold=int(self.config.multi_part_upload_threshold)
        )

    def download_blobs(self, srcs, dest):
        return medusa.storage.azure_blobs_storage.concurrent.download_blobs(
            self, srcs, dest, self.bucket,
            max_workers=self.config.concurrent_transfers,
            multi_part_upload_threshold=int(self.config.multi_part_upload_threshold)
        )

    @staticmethod
    def blob_matches_manifest(blob, object_in_manifest, enable_md5_checks=False):
        # Azure use hashed timespan in eTag header. It changes everytime
        # when the file is overwrote. "content-md5" is the right hash to
        # validate the file.
        return AzureStorage.compare_with_manifest(
            actual_size=blob.size,
            size_in_manifest=object_in_manifest['size'],
            actual_hash=blob.extra['md5_hash'] if enable_md5_checks else None,
            hash_in_manifest=object_in_manifest['MD5']
        )

    @staticmethod
    def file_matches_cache(src, cached_item, threshold=None, enable_md5_checks=False):
        return AzureStorage.compare_with_manifest(
            actual_size=src.stat().st_size,
            size_in_manifest=cached_item['size'],
            actual_hash=AbstractStorage.generate_md5_hash(src) if enable_md5_checks else None,
            hash_in_manifest=cached_item['MD5'],
        )

    @staticmethod
    def compare_with_manifest(actual_size, size_in_manifest, actual_hash=None, hash_in_manifest=None, threshold=None):
        sizes_match = actual_size == size_in_manifest
        if not actual_hash:
            return sizes_match

        hashes_match = (
            # this case comes from comparing blob hashes to manifest entries (in context of GCS)
            actual_hash == base64.b64decode(hash_in_manifest).hex()
            # this comes from comparing files to a cache
            or hash_in_manifest == base64.b64decode(actual_hash).hex()
            # and perhaps we need the to check for match even without base64 encoding
            or actual_hash == hash_in_manifest
        )

        return sizes_match and hashes_match